@@ -43,6 +43,7 @@ - (void)setViewController:(FlutterViewController*)viewController {
4343@end
4444
4545@interface FlutterViewControllerTest : XCTestCase
46+ @property (nonatomic , strong ) id mockEngine;
4647@end
4748
4849// The following conditional compilation defines an API 13 concept on earlier API targets so that
@@ -67,68 +68,64 @@ - (void)dispatchPresses:(NSSet<UIPress*>*)presses;
6768
6869@implementation FlutterViewControllerTest
6970
71+ - (void )setUp {
72+ self.mockEngine = OCMClassMock ([FlutterEngine class ]);
73+ }
74+
75+ - (void )tearDown {
76+ // We stop mocking here to avoid retain cycles that stop
77+ // FlutterViewControllers from deallocing.
78+ [self .mockEngine stopMocking ];
79+ self.mockEngine = nil ;
80+ }
81+
7082- (void )testViewDidDisappearDoesntPauseEngineWhenNotTheViewController {
71- id engine = OCMClassMock ([FlutterEngine class ]);
7283 id lifecycleChannel = OCMClassMock ([FlutterBasicMessageChannel class ]);
73- OCMStub ([engine lifecycleChannel ]).andReturn (lifecycleChannel);
74- FlutterViewController* viewControllerA = [[FlutterViewController alloc ] initWithEngine: engine
75- nibName: nil
76- bundle: nil ];
77- FlutterViewController* viewControllerB = [[FlutterViewController alloc ] initWithEngine: engine
78- nibName: nil
79- bundle: nil ];
84+ OCMStub ([self .mockEngine lifecycleChannel ]).andReturn (lifecycleChannel);
85+ FlutterViewController* viewControllerA =
86+ [[FlutterViewController alloc ] initWithEngine: self .mockEngine nibName: nil bundle: nil ];
87+ FlutterViewController* viewControllerB =
88+ [[FlutterViewController alloc ] initWithEngine: self .mockEngine nibName: nil bundle: nil ];
8089 id viewControllerMock = OCMPartialMock (viewControllerA);
8190 OCMStub ([viewControllerMock surfaceUpdated: NO ]);
82- OCMStub ([engine viewController ]).andReturn (viewControllerB);
91+ OCMStub ([self .mockEngine viewController ]).andReturn (viewControllerB);
8392 [viewControllerA viewDidDisappear: NO ];
8493 OCMReject ([lifecycleChannel sendMessage: @" AppLifecycleState.paused" ]);
8594 OCMReject ([viewControllerMock surfaceUpdated: [OCMArg any ]]);
8695}
8796
8897- (void )testViewDidDisappearDoesPauseEngineWhenIsTheViewController {
89- id engine = OCMClassMock ([FlutterEngine class ]);
9098 id lifecycleChannel = OCMClassMock ([FlutterBasicMessageChannel class ]);
91- OCMStub ([engine lifecycleChannel ]).andReturn (lifecycleChannel);
92- FlutterViewController* viewController = [[FlutterViewController alloc ] initWithEngine: engine
93- nibName: nil
94- bundle: nil ];
99+ OCMStub ([self .mockEngine lifecycleChannel ]).andReturn (lifecycleChannel);
100+ FlutterViewController* viewController =
101+ [[FlutterViewController alloc ] initWithEngine: self .mockEngine nibName: nil bundle: nil ];
95102 id viewControllerMock = OCMPartialMock (viewController);
96103 OCMStub ([viewControllerMock surfaceUpdated: NO ]);
97- OCMStub ([engine viewController ]).andReturn (viewController);
104+ OCMStub ([self .mockEngine viewController ]).andReturn (viewController);
98105 [viewController viewDidDisappear: NO ];
99106 OCMVerify ([lifecycleChannel sendMessage: @" AppLifecycleState.paused" ]);
100107 OCMVerify ([viewControllerMock surfaceUpdated: NO ]);
101108}
102109
103110- (void )testBinaryMessenger {
104- __weak FlutterViewController* weakVC;
105- @autoreleasepool {
106- id engine = OCMClassMock ([FlutterEngine class ]);
107- FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: engine
108- nibName: nil
109- bundle: nil ];
110- XCTAssertNotNil (vc);
111- weakVC = vc;
112- id messenger = OCMProtocolMock (@protocol (FlutterBinaryMessenger));
113- OCMStub ([engine binaryMessenger ]).andReturn (messenger);
114- XCTAssertEqual (vc.binaryMessenger , messenger);
115- OCMVerify ([engine binaryMessenger ]);
116- // This had to be added to make sure the view controller is deleted.
117- [engine stopMocking ];
118- }
119- XCTAssertNil (weakVC);
111+ FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: self .mockEngine
112+ nibName: nil
113+ bundle: nil ];
114+ XCTAssertNotNil (vc);
115+ id messenger = OCMProtocolMock (@protocol (FlutterBinaryMessenger));
116+ OCMStub ([self .mockEngine binaryMessenger ]).andReturn (messenger);
117+ XCTAssertEqual (vc.binaryMessenger , messenger);
118+ OCMVerify ([self .mockEngine binaryMessenger ]);
120119}
121120
122121#pragma mark - Platform Brightness
123122
124123- (void )testItReportsLightPlatformBrightnessByDefault {
125124 // Setup test.
126- id engine = OCMClassMock ([FlutterEngine class ]);
127-
128125 id settingsChannel = OCMClassMock ([FlutterBasicMessageChannel class ]);
129- OCMStub ([engine settingsChannel ]).andReturn (settingsChannel);
126+ OCMStub ([self .mockEngine settingsChannel ]).andReturn (settingsChannel);
130127
131- FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: engine
128+ FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: self .mockEngine
132129 nibName: nil
133130 bundle: nil ];
134131
@@ -141,18 +138,15 @@ - (void)testItReportsLightPlatformBrightnessByDefault {
141138 }]]);
142139
143140 // Clean up mocks
144- [engine stopMocking ];
145141 [settingsChannel stopMocking ];
146142}
147143
148144- (void )testItReportsPlatformBrightnessWhenViewWillAppear {
149145 // Setup test.
150- id engine = OCMClassMock ([FlutterEngine class ]);
151-
152146 id settingsChannel = OCMClassMock ([FlutterBasicMessageChannel class ]);
153- OCMStub ([engine settingsChannel ]).andReturn (settingsChannel);
147+ OCMStub ([self .mockEngine settingsChannel ]).andReturn (settingsChannel);
154148
155- FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: engine
149+ FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: self .mockEngine
156150 nibName: nil
157151 bundle: nil ];
158152
@@ -165,7 +159,6 @@ - (void)testItReportsPlatformBrightnessWhenViewWillAppear {
165159 }]]);
166160
167161 // Clean up mocks
168- [engine stopMocking ];
169162 [settingsChannel stopMocking ];
170163}
171164
@@ -177,12 +170,10 @@ - (void)testItReportsDarkPlatformBrightnessWhenTraitCollectionRequestsIt {
177170 }
178171
179172 // Setup test.
180- id engine = OCMClassMock ([FlutterEngine class ]);
181-
182173 id settingsChannel = OCMClassMock ([FlutterBasicMessageChannel class ]);
183- OCMStub ([engine settingsChannel ]).andReturn (settingsChannel);
174+ OCMStub ([self .mockEngine settingsChannel ]).andReturn (settingsChannel);
184175
185- FlutterViewController* realVC = [[FlutterViewController alloc ] initWithEngine: engine
176+ FlutterViewController* realVC = [[FlutterViewController alloc ] initWithEngine: self .mockEngine
186177 nibName: nil
187178 bundle: nil ];
188179 id mockTraitCollection =
@@ -205,7 +196,6 @@ - (void)testItReportsDarkPlatformBrightnessWhenTraitCollectionRequestsIt {
205196
206197 // Clean up mocks
207198 [partialMockVC stopMocking ];
208- [engine stopMocking ];
209199 [settingsChannel stopMocking ];
210200 [mockTraitCollection stopMocking ];
211201}
@@ -228,12 +218,10 @@ - (void)testItReportsNormalPlatformContrastByDefault {
228218 }
229219
230220 // Setup test.
231- id engine = OCMClassMock ([FlutterEngine class ]);
232-
233221 id settingsChannel = OCMClassMock ([FlutterBasicMessageChannel class ]);
234- OCMStub ([engine settingsChannel ]).andReturn (settingsChannel);
222+ OCMStub ([self .mockEngine settingsChannel ]).andReturn (settingsChannel);
235223
236- FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: engine
224+ FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: self .mockEngine
237225 nibName: nil
238226 bundle: nil ];
239227
@@ -246,7 +234,6 @@ - (void)testItReportsNormalPlatformContrastByDefault {
246234 }]]);
247235
248236 // Clean up mocks
249- [engine stopMocking ];
250237 [settingsChannel stopMocking ];
251238}
252239
@@ -258,12 +245,10 @@ - (void)testItReportsPlatformContrastWhenViewWillAppear {
258245 }
259246
260247 // Setup test.
261- id engine = OCMClassMock ([FlutterEngine class ]);
262-
263248 id settingsChannel = OCMClassMock ([FlutterBasicMessageChannel class ]);
264- OCMStub ([engine settingsChannel ]).andReturn (settingsChannel);
249+ OCMStub ([self .mockEngine settingsChannel ]).andReturn (settingsChannel);
265250
266- FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: engine
251+ FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: self .mockEngine
267252 nibName: nil
268253 bundle: nil ];
269254
@@ -276,7 +261,6 @@ - (void)testItReportsPlatformContrastWhenViewWillAppear {
276261 }]]);
277262
278263 // Clean up mocks
279- [engine stopMocking ];
280264 [settingsChannel stopMocking ];
281265}
282266
@@ -288,12 +272,10 @@ - (void)testItReportsHighContrastWhenTraitCollectionRequestsIt {
288272 }
289273
290274 // Setup test.
291- id engine = OCMClassMock ([FlutterEngine class ]);
292-
293275 id settingsChannel = OCMClassMock ([FlutterBasicMessageChannel class ]);
294- OCMStub ([engine settingsChannel ]).andReturn (settingsChannel);
276+ OCMStub ([self .mockEngine settingsChannel ]).andReturn (settingsChannel);
295277
296- FlutterViewController* realVC = [[FlutterViewController alloc ] initWithEngine: engine
278+ FlutterViewController* realVC = [[FlutterViewController alloc ] initWithEngine: self .mockEngine
297279 nibName: nil
298280 bundle: nil ];
299281 id mockTraitCollection = [self fakeTraitCollectionWithContrast: UIAccessibilityContrastHigh];
@@ -315,7 +297,6 @@ - (void)testItReportsHighContrastWhenTraitCollectionRequestsIt {
315297
316298 // Clean up mocks
317299 [partialMockVC stopMocking ];
318- [engine stopMocking ];
319300 [settingsChannel stopMocking ];
320301 [mockTraitCollection stopMocking ];
321302}
@@ -470,16 +451,14 @@ - (void)orientationTestWithOrientationUpdate:(UIInterfaceOrientationMask)mask
470451 currentOrientation : (UIInterfaceOrientation)currentOrientation
471452 didChangeOrientation : (BOOL )didChange
472453 resultingOrientation : (UIInterfaceOrientation)resultingOrientation {
473- id engine = OCMClassMock ([FlutterEngine class ]);
474-
475454 id deviceMock = OCMPartialMock ([UIDevice currentDevice ]);
476455 if (!didChange) {
477456 OCMReject ([deviceMock setValue: [OCMArg any ] forKey: @" orientation" ]);
478457 } else {
479458 OCMExpect ([deviceMock setValue: @(resultingOrientation) forKey: @" orientation" ]);
480459 }
481460
482- FlutterViewController* realVC = [[FlutterViewController alloc ] initWithEngine: engine
461+ FlutterViewController* realVC = [[FlutterViewController alloc ] initWithEngine: self .mockEngine
483462 nibName: nil
484463 bundle: nil ];
485464 id mockApplication = OCMClassMock ([UIApplication class ]);
@@ -488,7 +467,6 @@ - (void)orientationTestWithOrientationUpdate:(UIInterfaceOrientationMask)mask
488467
489468 [realVC performOrientationUpdate: mask];
490469 OCMVerifyAll (deviceMock);
491- [engine stopMocking ];
492470 [deviceMock stopMocking ];
493471 [mockApplication stopMocking ];
494472}
@@ -544,17 +522,15 @@ - (void)testHideOverlay {
544522}
545523
546524- (void )testNotifyLowMemory {
547- id engine = OCMClassMock ([FlutterEngine class ]);
548- FlutterViewController* viewController = [[FlutterViewController alloc ] initWithEngine: engine
549- nibName: nil
550- bundle: nil ];
551- OCMStub ([engine viewController ]).andReturn (viewController);
525+ FlutterViewController* viewController =
526+ [[FlutterViewController alloc ] initWithEngine: self .mockEngine nibName: nil bundle: nil ];
527+ OCMStub ([self .mockEngine viewController ]).andReturn (viewController);
552528 id viewControllerMock = OCMPartialMock (viewController);
553529 OCMStub ([viewControllerMock surfaceUpdated: NO ]);
554530
555531 [viewController beginAppearanceTransition: NO animated: NO ];
556532 [viewController endAppearanceTransition ];
557- OCMVerify ([engine notifyLowMemory ]);
533+ OCMVerify ([self .mockEngine notifyLowMemory ]);
558534}
559535
560536- (void )testValidKeyUpEvent API_AVAILABLE(ios(13.4 )) {
@@ -564,12 +540,10 @@ - (void)testValidKeyUpEvent API_AVAILABLE(ios(13.4)) {
564540 return ;
565541 }
566542
567- id engine = OCMClassMock ([FlutterEngine class ]);
568-
569543 id keyEventChannel = OCMClassMock ([FlutterBasicMessageChannel class ]);
570- OCMStub ([engine keyEventChannel ]).andReturn (keyEventChannel);
544+ OCMStub ([self .mockEngine keyEventChannel ]).andReturn (keyEventChannel);
571545
572- FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: engine
546+ FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: self .mockEngine
573547 nibName: nil
574548 bundle: nil ];
575549
@@ -594,7 +568,6 @@ - (void)testValidKeyUpEvent API_AVAILABLE(ios(13.4)) {
594568 }]]);
595569
596570 // Clean up mocks
597- [engine stopMocking ];
598571 [keyEventChannel stopMocking ];
599572}
600573
@@ -605,12 +578,10 @@ - (void)testValidKeyDownEvent API_AVAILABLE(ios(13.4)) {
605578 return ;
606579 }
607580
608- id engine = OCMClassMock ([FlutterEngine class ]);
609-
610581 id keyEventChannel = OCMClassMock ([FlutterBasicMessageChannel class ]);
611- OCMStub ([engine keyEventChannel ]).andReturn (keyEventChannel);
582+ OCMStub ([self .mockEngine keyEventChannel ]).andReturn (keyEventChannel);
612583
613- FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: engine
584+ FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: self .mockEngine
614585 nibName: nil
615586 bundle: nil ];
616587
@@ -635,7 +606,6 @@ - (void)testValidKeyDownEvent API_AVAILABLE(ios(13.4)) {
635606 }]]);
636607
637608 // Clean up mocks
638- [engine stopMocking ];
639609 [keyEventChannel stopMocking ];
640610}
641611
@@ -646,12 +616,10 @@ - (void)testIgnoredKeyEvents API_AVAILABLE(ios(13.4)) {
646616 return ;
647617 }
648618
649- id engine = OCMClassMock ([FlutterEngine class ]);
650-
651619 id keyEventChannel = OCMClassMock ([FlutterBasicMessageChannel class ]);
652- OCMStub ([engine keyEventChannel ]).andReturn (keyEventChannel);
620+ OCMStub ([self .mockEngine keyEventChannel ]).andReturn (keyEventChannel);
653621
654- FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: engine
622+ FlutterViewController* vc = [[FlutterViewController alloc ] initWithEngine: self .mockEngine
655623 nibName: nil
656624 bundle: nil ];
657625
@@ -674,7 +642,6 @@ - (void)testIgnoredKeyEvents API_AVAILABLE(ios(13.4)) {
674642 OCMVerify (never (), [keyEventChannel sendMessage: [OCMArg any ]]);
675643
676644 // Clean up mocks
677- [engine stopMocking ];
678645 [keyEventChannel stopMocking ];
679646}
680647
0 commit comments