@@ -40,24 +40,25 @@ - (void)setUp {
40
40
self.continueAfterFailure = NO ;
41
41
}
42
42
43
- - (void )ensureApplicationIsInActiveState {
44
- // Since this test checks for view controller and application lifecycles, the starting point
45
- // must be well defined.
46
-
47
- // Make this check after setting the root view controller since it seems to just hang if
48
- // done in setUp.
43
+ - (NSArray *)initialPresentLifecycles {
44
+ NSMutableArray * expectations =
45
+ [NSMutableArray arrayWithObject: [[XCAppLifecycleTestExpectation alloc ]
46
+ initForLifecycle: @" AppLifecycleState.inactive"
47
+ forStep: @" showing a FlutterViewController" ]];
48
+
49
+ // If the test is the very first test to run in the test target, the UIApplication may
50
+ // be in inactive state. Haven't found a good way to force it to go to active state.
51
+ // So just account for it in the initial lifecycle events with an extra resumed since
52
+ // the FlutterViewController tracks all view controller and application lifecycle events.
49
53
if ([UIApplication sharedApplication ].applicationState != UIApplicationStateActive) {
50
- NSLog (@" Initial application state is currently %zd . Waiting for active." ,
51
- [UIApplication sharedApplication ].applicationState );
52
- // Don't have to inspect the notification since the typed notification itself indicates the
53
- // desired state.
54
- // [self expectationForNotification:UIApplicationDidBecomeActiveNotification
55
- // object:self
56
- // handler:nil];
57
- [self keyValueObservingExpectationForObject: [UIApplication sharedApplication ] keyPath: @" applicationState" expectedValue: nil ];
58
- [self waitForExpectationsWithTimeout: 30.0 handler: nil ];
54
+ [expectations addObject: [[XCAppLifecycleTestExpectation alloc ]
55
+ initForLifecycle: @" AppLifecycleState.resumed"
56
+ forStep: @" showing a FlutterViewController" ]];
59
57
}
60
- XCTAssertEqual ([UIApplication sharedApplication ].applicationState , UIApplicationStateActive);
58
+ [expectations addObject: [[XCAppLifecycleTestExpectation alloc ]
59
+ initForLifecycle: @" AppLifecycleState.resumed"
60
+ forStep: @" showing a FlutterViewController" ]];
61
+ return expectations;
61
62
}
62
63
63
64
- (void )testDismissedFlutterViewControllerNotRespondingToApplicationLifecycle {
@@ -73,17 +74,10 @@ - (void)testDismissedFlutterViewControllerNotRespondingToApplicationLifecycle {
73
74
application.delegate .window .rootViewController = rootVC;
74
75
FlutterEngine* engine = rootVC.engine ;
75
76
76
- [self ensureApplicationIsInActiveState ];
77
-
78
77
NSMutableArray * lifecycleExpectations = [NSMutableArray arrayWithCapacity: 10 ];
79
78
80
79
// Expected sequence from showing the FlutterViewController is inactive and resumed.
81
- [lifecycleExpectations addObjectsFromArray: @[
82
- [[XCAppLifecycleTestExpectation alloc ] initForLifecycle: @" AppLifecycleState.inactive"
83
- forStep: @" showing a FlutterViewController" ],
84
- [[XCAppLifecycleTestExpectation alloc ] initForLifecycle: @" AppLifecycleState.resumed"
85
- forStep: @" showing a FlutterViewController" ]
86
- ]];
80
+ [lifecycleExpectations addObjectsFromArray: [self initialPresentLifecycles ]];
87
81
88
82
[engine.lifecycleChannel setMessageHandler: ^(id message, FlutterReply callback) {
89
83
if (lifecycleExpectations.count == 0 ) {
@@ -217,17 +211,10 @@ - (void)testVisibleFlutterViewControllerRespondsToApplicationLifecycle {
217
211
application.delegate .window .rootViewController = rootVC;
218
212
FlutterEngine* engine = rootVC.engine ;
219
213
220
- [self ensureApplicationIsInActiveState ];
221
-
222
214
NSMutableArray * lifecycleExpectations = [NSMutableArray arrayWithCapacity: 10 ];
223
215
224
216
// Expected sequence from showing the FlutterViewController is inactive and resumed.
225
- [lifecycleExpectations addObjectsFromArray: @[
226
- [[XCAppLifecycleTestExpectation alloc ] initForLifecycle: @" AppLifecycleState.inactive"
227
- forStep: @" showing a FlutterViewController" ],
228
- [[XCAppLifecycleTestExpectation alloc ] initForLifecycle: @" AppLifecycleState.resumed"
229
- forStep: @" showing a FlutterViewController" ]
230
- ]];
217
+ [lifecycleExpectations addObjectsFromArray: [self initialPresentLifecycles ]];
231
218
232
219
[engine.lifecycleChannel setMessageHandler: ^(id message, FlutterReply callback) {
233
220
if (lifecycleExpectations.count == 0 ) {
@@ -327,17 +314,10 @@ - (void)testFlutterViewControllerDetachingSendsApplicationLifecycle {
327
314
application.delegate .window .rootViewController = rootVC;
328
315
FlutterEngine* engine = rootVC.engine ;
329
316
330
- [self ensureApplicationIsInActiveState ];
331
-
332
317
NSMutableArray * lifecycleExpectations = [NSMutableArray arrayWithCapacity: 10 ];
333
318
334
319
// Expected sequence from showing the FlutterViewController is inactive and resumed.
335
- [lifecycleExpectations addObjectsFromArray: @[
336
- [[XCAppLifecycleTestExpectation alloc ] initForLifecycle: @" AppLifecycleState.inactive"
337
- forStep: @" showing a FlutterViewController" ],
338
- [[XCAppLifecycleTestExpectation alloc ] initForLifecycle: @" AppLifecycleState.resumed"
339
- forStep: @" showing a FlutterViewController" ]
340
- ]];
320
+ [lifecycleExpectations addObjectsFromArray: [self initialPresentLifecycles ]];
341
321
342
322
[engine.lifecycleChannel setMessageHandler: ^(id message, FlutterReply callback) {
343
323
if (lifecycleExpectations.count == 0 ) {
0 commit comments