AppLifecycleTests.m 13.1 KB
Newer Older
X
xster 已提交
1 2 3 4 5 6 7 8
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Flutter/Flutter.h>
#import <XCTest/XCTest.h>
#import "ScreenBeforeFlutter.h"

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
@interface XCAppLifecycleTestExpectation : XCTestExpectation

- (instancetype)initForLifecycle:(NSString*)expectedLifecycle forStep:(NSString*)step;
@property(nonatomic, readonly, copy) NSString* expectedLifecycle;

@end

@implementation XCAppLifecycleTestExpectation

@synthesize expectedLifecycle = _expectedLifecycle;
- (instancetype)initForLifecycle:(NSString*)expectedLifecycle forStep:(NSString*)step {
  // The step is here because the callbacks into the handler which checks these expectations isn't
  // synchronous with the executions in the test, so it's hard to find the cause in the test
  // otherwise.
  self = [super initWithDescription:[NSString stringWithFormat:@"Expected state %@ during step %@",
                                                               expectedLifecycle, step]];
  _expectedLifecycle = [expectedLifecycle copy];
  return self;
}

@end

X
xster 已提交
31 32 33 34 35 36 37 38 39 40
@interface AppLifecycleTests : XCTestCase
@end

@implementation AppLifecycleTests

- (void)setUp {
  [super setUp];
  self.continueAfterFailure = NO;
}

D
Dan Field 已提交
41 42 43
// TODD(dnfield): Unskip this when https://github.com/flutter/flutter/issues/40817
// is resolved.
- (void)skip_testDismissedFlutterViewControllerNotRespondingToApplicationLifecycle {
X
xster 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  XCTestExpectation* engineStartedExpectation = [self expectationWithDescription:@"Engine started"];

  // Let the engine finish booting (at the end of which the channels are properly set-up) before
  // moving onto the next step of showing the next view controller.
  ScreenBeforeFlutter* rootVC = [[ScreenBeforeFlutter alloc] initWithEngineRunCompletion:^void() {
    [engineStartedExpectation fulfill];
  }];

  [self waitForExpectationsWithTimeout:5 handler:nil];

  UIApplication* application = UIApplication.sharedApplication;
  application.delegate.window.rootViewController = rootVC;
  FlutterEngine* engine = rootVC.engine;

  NSMutableArray* lifecycleExpectations = [NSMutableArray arrayWithCapacity:10];

60 61 62 63 64 65 66 67 68 69 70
  // Expected sequence from showing the FlutterViewController is inactive and resumed.
  [lifecycleExpectations addObjectsFromArray:@[
    [[XCAppLifecycleTestExpectation alloc] initForLifecycle:@"AppLifecycleState.inactive"
                                                    forStep:@"showing a FlutterViewController"],
    [[XCAppLifecycleTestExpectation alloc] initForLifecycle:@"AppLifecycleState.resumed"
                                                    forStep:@"showing a FlutterViewController"]
  ]];

  // Holding onto this FlutterViewController is consequential here. Since a released
  // FlutterViewController wouldn't keep listening to the application lifecycle events and produce
  // false positives for the application lifecycle tests further below.
X
xster 已提交
71 72 73 74
  FlutterViewController* flutterVC = [rootVC showFlutter];
  [engine.lifecycleChannel setMessageHandler:^(id message, FlutterReply callback) {
    if (lifecycleExpectations.count == 0) {
      XCTFail(@"Unexpected lifecycle transition: %@", message);
75 76 77 78 79 80 81
      return;
    }
    XCAppLifecycleTestExpectation* nextExpectation = [lifecycleExpectations objectAtIndex:0];
    if (![[nextExpectation expectedLifecycle] isEqualToString:message]) {
      XCTFail(@"Expected lifecycle %@ but instead received %@", [nextExpectation expectedLifecycle],
              message);
      return;
X
xster 已提交
82
    }
83 84

    [nextExpectation fulfill];
X
xster 已提交
85 86 87
    [lifecycleExpectations removeObjectAtIndex:0];
  }];

88 89
  // The expectations list isn't dequeued by the message handler yet.
  [self waitForExpectations:lifecycleExpectations timeout:5 enforceOrder:YES];
X
xster 已提交
90 91

  // Now dismiss the FlutterViewController again and expect another inactive and paused.
92 93 94 95 96 97 98
  [lifecycleExpectations addObjectsFromArray:@[
    [[XCAppLifecycleTestExpectation alloc] initForLifecycle:@"AppLifecycleState.inactive"
                                                    forStep:@"dismissing a FlutterViewController"],
    [[XCAppLifecycleTestExpectation alloc]
        initForLifecycle:@"AppLifecycleState.paused"
                 forStep:@"dismissing a FlutterViewController"]
  ]];
X
xster 已提交
99
  [flutterVC dismissViewControllerAnimated:NO completion:nil];
100
  [self waitForExpectations:lifecycleExpectations timeout:5 enforceOrder:YES];
X
xster 已提交
101 102 103 104 105 106

  // Now put the app in the background (while the engine is still running) and bring it back to
  // the foreground. Granted, we're not winning any awards for hyper-realism but at least we're
  // checking that we aren't observing the UIApplication notifications and double registering
  // for AppLifecycleState events.

107 108
  // These operations are synchronous so if they trigger any lifecycle events, they should trigger
  // failures in the message handler immediately.
X
xster 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  [[NSNotificationCenter defaultCenter]
      postNotificationName:UIApplicationWillResignActiveNotification
                    object:nil];
  [[NSNotificationCenter defaultCenter]
      postNotificationName:UIApplicationDidEnterBackgroundNotification
                    object:nil];
  [[NSNotificationCenter defaultCenter]
      postNotificationName:UIApplicationWillEnterForegroundNotification
                    object:nil];
  [[NSNotificationCenter defaultCenter]
      postNotificationName:UIApplicationDidBecomeActiveNotification
                    object:nil];

  // There's no timing latch for our semi-fake background-foreground cycle so launch the
  // FlutterViewController again to check the complete event list again.
124 125 126 127 128 129 130 131 132 133 134

  // Expect only lifecycle events from showing the FlutterViewController again, not from any
  // backgrounding/foregrounding.
  [lifecycleExpectations addObjectsFromArray:@[
    [[XCAppLifecycleTestExpectation alloc]
        initForLifecycle:@"AppLifecycleState.inactive"
                 forStep:@"showing a FlutterViewController a second time after backgrounding"],
    [[XCAppLifecycleTestExpectation alloc]
        initForLifecycle:@"AppLifecycleState.resumed"
                 forStep:@"showing a FlutterViewController a second time after backgrounding"]
  ]];
X
xster 已提交
135
  flutterVC = [rootVC showFlutter];
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  [self waitForExpectations:lifecycleExpectations timeout:5 enforceOrder:YES];

  // Dismantle.
  [engine.lifecycleChannel setMessageHandler:nil];
  [flutterVC dismissViewControllerAnimated:NO completion:nil];
  [engine setViewController:nil];
}

- (void)testVisibleFlutterViewControllerRespondsToApplicationLifecycle {
  XCTestExpectation* engineStartedExpectation = [self expectationWithDescription:@"Engine started"];

  // Let the engine finish booting (at the end of which the channels are properly set-up) before
  // moving onto the next step of showing the next view controller.
  ScreenBeforeFlutter* rootVC = [[ScreenBeforeFlutter alloc] initWithEngineRunCompletion:^void() {
    [engineStartedExpectation fulfill];
  }];

  [self waitForExpectationsWithTimeout:5 handler:nil];

  UIApplication* application = UIApplication.sharedApplication;
  application.delegate.window.rootViewController = rootVC;
  FlutterEngine* engine = rootVC.engine;

  NSMutableArray* lifecycleExpectations = [NSMutableArray arrayWithCapacity:10];

  // Expected sequence from showing the FlutterViewController is inactive and resumed.
  [lifecycleExpectations addObjectsFromArray:@[
    [[XCAppLifecycleTestExpectation alloc] initForLifecycle:@"AppLifecycleState.inactive"
                                                    forStep:@"showing a FlutterViewController"],
    [[XCAppLifecycleTestExpectation alloc] initForLifecycle:@"AppLifecycleState.resumed"
                                                    forStep:@"showing a FlutterViewController"]
  ]];

  FlutterViewController* flutterVC = [rootVC showFlutter];
  [engine.lifecycleChannel setMessageHandler:^(id message, FlutterReply callback) {
    if (lifecycleExpectations.count == 0) {
      XCTFail(@"Unexpected lifecycle transition: %@", message);
      return;
    }
    XCAppLifecycleTestExpectation* nextExpectation = [lifecycleExpectations objectAtIndex:0];
    if (![[nextExpectation expectedLifecycle] isEqualToString:message]) {
      XCTFail(@"Expected lifecycle %@ but instead received %@", [nextExpectation expectedLifecycle],
              message);
      return;
    }

    [nextExpectation fulfill];
    [lifecycleExpectations removeObjectAtIndex:0];
  }];

  [self waitForExpectations:lifecycleExpectations timeout:5];

  // Now put the FlutterViewController into background.
  [lifecycleExpectations addObjectsFromArray:@[
    [[XCAppLifecycleTestExpectation alloc]
        initForLifecycle:@"AppLifecycleState.inactive"
                 forStep:@"putting FlutterViewController to the background"],
    [[XCAppLifecycleTestExpectation alloc]
        initForLifecycle:@"AppLifecycleState.paused"
                 forStep:@"putting FlutterViewController to the background"]
  ]];
  [[NSNotificationCenter defaultCenter]
      postNotificationName:UIApplicationWillResignActiveNotification
                    object:nil];
  [[NSNotificationCenter defaultCenter]
      postNotificationName:UIApplicationDidEnterBackgroundNotification
                    object:nil];
  [self waitForExpectations:lifecycleExpectations timeout:5];

  // Now restore to foreground
  [lifecycleExpectations addObjectsFromArray:@[
    [[XCAppLifecycleTestExpectation alloc]
        initForLifecycle:@"AppLifecycleState.inactive"
                 forStep:@"putting FlutterViewController back to foreground"],
    [[XCAppLifecycleTestExpectation alloc]
        initForLifecycle:@"AppLifecycleState.resumed"
                 forStep:@"putting FlutterViewController back to foreground"]
  ]];
  [[NSNotificationCenter defaultCenter]
      postNotificationName:UIApplicationWillEnterForegroundNotification
                    object:nil];
  [[NSNotificationCenter defaultCenter]
      postNotificationName:UIApplicationDidBecomeActiveNotification
                    object:nil];
X
xster 已提交
220
  [self waitForExpectations:lifecycleExpectations timeout:5];
221 222 223 224 225

  // Dismantle.
  [engine.lifecycleChannel setMessageHandler:nil];
  [flutterVC dismissViewControllerAnimated:NO completion:nil];
  [engine setViewController:nil];
X
xster 已提交
226
}
227

C
chunhtai 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
- (void)testFlutterViewControllerDetachingSendsApplicationLifecycle {
  XCTestExpectation* engineStartedExpectation = [self expectationWithDescription:@"Engine started"];

  // Let the engine finish booting (at the end of which the channels are properly set-up) before
  // moving onto the next step of showing the next view controller.
  ScreenBeforeFlutter* rootVC = [[ScreenBeforeFlutter alloc] initWithEngineRunCompletion:^void() {
    [engineStartedExpectation fulfill];
  }];

  [self waitForExpectationsWithTimeout:5 handler:nil];

  UIApplication* application = UIApplication.sharedApplication;
  application.delegate.window.rootViewController = rootVC;
  FlutterEngine* engine = rootVC.engine;

  NSMutableArray* lifecycleExpectations = [NSMutableArray arrayWithCapacity:10];

  // Expected sequence from showing the FlutterViewController is inactive and resumed.
  [lifecycleExpectations addObjectsFromArray:@[
    [[XCAppLifecycleTestExpectation alloc] initForLifecycle:@"AppLifecycleState.inactive"
                                                    forStep:@"showing a FlutterViewController"],
    [[XCAppLifecycleTestExpectation alloc] initForLifecycle:@"AppLifecycleState.resumed"
                                                    forStep:@"showing a FlutterViewController"]
  ]];
  // At the end of Flutter VC, we want to make sure it deallocs and sends detached signal.
  // Using autoreleasepool will guarantee that.
  FlutterViewController* flutterVC;
  @autoreleasepool {
    flutterVC = [rootVC showFlutter];
    [engine.lifecycleChannel setMessageHandler:^(id message, FlutterReply callback) {
      if (lifecycleExpectations.count == 0) {
        XCTFail(@"Unexpected lifecycle transition: %@", message);
        return;
      }
      XCAppLifecycleTestExpectation* nextExpectation = [lifecycleExpectations objectAtIndex:0];
      if (![[nextExpectation expectedLifecycle] isEqualToString:message]) {
        XCTFail(@"Expected lifecycle %@ but instead received %@",
                [nextExpectation expectedLifecycle], message);
        return;
      }

      [nextExpectation fulfill];
      [lifecycleExpectations removeObjectAtIndex:0];
    }];

    [self waitForExpectations:lifecycleExpectations timeout:5];

    // Starts dealloc flutter VC.
    [lifecycleExpectations addObjectsFromArray:@[
      [[XCAppLifecycleTestExpectation alloc] initForLifecycle:@"AppLifecycleState.inactive"
                                                      forStep:@"detaching a FlutterViewController"],
      [[XCAppLifecycleTestExpectation alloc] initForLifecycle:@"AppLifecycleState.paused"
                                                      forStep:@"detaching a FlutterViewController"],
      [[XCAppLifecycleTestExpectation alloc]
          initForLifecycle:@"AppLifecycleState.detached"
                   forStep:@"detaching a FlutterViewController"]
    ]];
    [flutterVC dismissViewControllerAnimated:NO completion:nil];
    flutterVC = nil;
  }
  [self waitForExpectations:lifecycleExpectations timeout:5];

  [engine.lifecycleChannel setMessageHandler:nil];
  [engine setViewController:nil];
}

X
xster 已提交
294
@end