Flutter iOS Embedder
FlutterPluginAppLifeCycleDelegateTest.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #import <OCMock/OCMock.h>
6 #import <XCTest/XCTest.h>
7 
13 
15 
16 // --- Test Category to avoid modifying the original production source code ---
18 - (void)removeDelegate:(NSObject<FlutterApplicationLifeCycleDelegate>*)delegate;
19 @end
20 
22 - (void)removeDelegate:(NSObject<FlutterApplicationLifeCycleDelegate>*)delegate {
23  // Access the private _delegates member via Key-Value Coding (KVC)
24  NSPointerArray* delegates = [self valueForKey:@"_delegates"];
25  for (NSUInteger i = 0; i < delegates.count; i++) {
26  if ([delegates pointerAtIndex:i] == (__bridge void*)delegate) {
27  [delegates removePointerAtIndex:i];
28  break;
29  }
30  }
31 }
32 @end
33 // -----------------------------------------------------------------------
34 
35 @protocol TestFlutterPluginWithSceneEvents <NSObject,
38 @end
39 
41 @end
42 
44 - (BOOL)application:(UIApplication*)application
45  didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
46  return NO;
47 }
48 
49 - (BOOL)application:(UIApplication*)application
50  openURL:(NSURL*)url
51  options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options {
52  return YES;
53 }
54 
55 - (BOOL)application:(UIApplication*)application
56  continueUserActivity:(NSUserActivity*)userActivity
57  restorationHandler:(void (^)(NSArray*))restorationHandler {
58  return YES;
59 }
60 
61 - (BOOL)application:(UIApplication*)application
62  performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem
63  completionHandler:(void (^)(BOOL succeeded))completionHandler
64  API_AVAILABLE(ios(9.0)) {
65  return YES;
66 }
67 @end
68 
70 @end
71 
72 @implementation FakePlugin
73 - (BOOL)application:(UIApplication*)application
74  openURL:(NSURL*)url
75  options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options {
76  return YES;
77 }
78 
79 - (BOOL)application:(UIApplication*)application
80  continueUserActivity:(NSUserActivity*)userActivity
81  restorationHandler:(void (^)(NSArray*))restorationHandler {
82  return YES;
83 }
84 
85 - (BOOL)application:(UIApplication*)application
86  performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem
87  completionHandler:(void (^)(BOOL succeeded))completionHandler
88  API_AVAILABLE(ios(9.0)) {
89  return YES;
90 }
91 
92 - (BOOL)application:(UIApplication*)application
93  didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
94  return YES;
95 }
96 
97 - (BOOL)application:(UIApplication*)application
98  willFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
99  return YES;
100 }
101 @end
102 
103 /**
104  * A mock plugin that simulates behavior causing a mutation crash.
105  * This represents a "downstream" mutation where a plugin adds or removes
106  * delegates during a lifecycle notification loop.
107  */
109 @property(nonatomic, weak) FlutterPluginAppLifeCycleDelegate* lifecycleDelegate;
110 @property(nonatomic, assign) BOOL shouldAdd; // YES = Add, NO = Remove
111 @end
112 
113 @implementation MutatingPlugin
114 - (BOOL)application:(UIApplication*)application
115  didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
116  if (self.shouldAdd) {
117  // Case 1: Add a new delegate during the loop over _delegates
118  [self.lifecycleDelegate addDelegate:[[FakePlugin alloc] init]];
119  } else {
120  // Case 2: Remove itself during the loop over _delegates via TestUtils category
121  [(id)self.lifecycleDelegate removeDelegate:self];
122  }
123  return YES;
124 }
125 @end
126 
128 @end
129 
131 
132 - (void)testCreate {
134  XCTAssertNotNil(delegate);
135 }
136 
137 - (void)testSceneWillConnectFallback {
139  id plugin = [[FakePlugin alloc] init];
140  id mockPlugin = OCMPartialMock(plugin);
141  [delegate addDelegate:mockPlugin];
142 
143  id mockOptions = OCMClassMock([UISceneConnectionOptions class]);
144  id mockShortcutItem = OCMClassMock([UIApplicationShortcutItem class]);
145  OCMStub([mockOptions shortcutItem]).andReturn(mockShortcutItem);
146  OCMStub([mockOptions sourceApplication]).andReturn(@"bundle_id");
147  id urlContext = OCMClassMock([UIOpenURLContext class]);
148  NSURL* url = [NSURL URLWithString:@"http://example.com"];
149  OCMStub([urlContext URL]).andReturn(url);
150  NSSet<UIOpenURLContext*>* urlContexts = [NSSet setWithObjects:urlContext, nil];
151  OCMStub([mockOptions URLContexts]).andReturn(urlContexts);
152 
153  NSDictionary<UIApplicationOpenURLOptionsKey, id>* expectedApplicationOptions = @{
154  UIApplicationLaunchOptionsShortcutItemKey : mockShortcutItem,
155  UIApplicationLaunchOptionsSourceApplicationKey : @"bundle_id",
156  UIApplicationLaunchOptionsURLKey : url,
157  };
158 
159  [delegate sceneWillConnectFallback:mockOptions];
160  OCMVerify([mockPlugin application:[UIApplication sharedApplication]
161  didFinishLaunchingWithOptions:expectedApplicationOptions]);
162 }
163 
164 - (void)testSceneWillConnectFallbackSkippedSupportsScenes {
166  id plugin = [[FakeTestFlutterPluginWithSceneEvents alloc] init];
167  id mockPlugin = OCMPartialMock(plugin);
168  [delegate addDelegate:mockPlugin];
169 
170  id mockOptions = OCMClassMock([UISceneConnectionOptions class]);
171  id mockShortcutItem = OCMClassMock([UIApplicationShortcutItem class]);
172  OCMStub([mockOptions shortcutItem]).andReturn(mockShortcutItem);
173  OCMStub([mockOptions sourceApplication]).andReturn(@"bundle_id");
174  id urlContext = OCMClassMock([UIOpenURLContext class]);
175  NSURL* url = [NSURL URLWithString:@"http://example.com"];
176  OCMStub([urlContext URL]).andReturn(url);
177  NSSet<UIOpenURLContext*>* urlContexts = [NSSet setWithObjects:urlContext, nil];
178  OCMStub([mockOptions URLContexts]).andReturn(urlContexts);
179 
180  [delegate sceneWillConnectFallback:mockOptions];
181  OCMReject([mockPlugin application:[OCMArg any] didFinishLaunchingWithOptions:[OCMArg any]]);
182 }
183 
184 - (void)testSceneWillConnectFallbackSkippedNoOptions {
186  id plugin = [[FakePlugin alloc] init];
187  id mockPlugin = OCMPartialMock(plugin);
188  [delegate addDelegate:mockPlugin];
189 
190  id mockOptions = OCMClassMock([UISceneConnectionOptions class]);
191 
192  [delegate sceneWillConnectFallback:mockOptions];
193  OCMReject([mockPlugin application:[OCMArg any] didFinishLaunchingWithOptions:[OCMArg any]]);
194 }
195 
196 - (void)testDidEnterBackground {
197  XCTNSNotificationExpectation* expectation = [[XCTNSNotificationExpectation alloc]
198  initWithName:UIApplicationDidEnterBackgroundNotification];
200  id mockApplication = OCMClassMock([FlutterSharedApplication class]);
201  OCMStub([mockApplication hasSceneDelegate]).andReturn(NO);
202  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
203  [delegate addDelegate:plugin];
204  [[NSNotificationCenter defaultCenter]
205  postNotificationName:UIApplicationDidEnterBackgroundNotification
206  object:nil];
207 
208  [self waitForExpectations:@[ expectation ] timeout:5.0];
209  OCMVerify([plugin applicationDidEnterBackground:[UIApplication sharedApplication]]);
210 }
211 
212 - (void)testDidEnterBackgroundWithUIScene {
213  XCTNSNotificationExpectation* expectation = [[XCTNSNotificationExpectation alloc]
214  initWithName:UIApplicationDidEnterBackgroundNotification];
216  id mockApplication = OCMClassMock([FlutterSharedApplication class]);
217  OCMStub([mockApplication hasSceneDelegate]).andReturn(YES);
218  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
219  [delegate addDelegate:plugin];
220  [[NSNotificationCenter defaultCenter]
221  postNotificationName:UIApplicationDidEnterBackgroundNotification
222  object:nil];
223 
224  [self waitForExpectations:@[ expectation ] timeout:5.0];
225  OCMReject([plugin applicationDidEnterBackground:[OCMArg any]]);
226 }
227 
228 - (void)testSceneDidEnterBackgroundFallback {
230  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
231  [delegate addDelegate:plugin];
232 
233  [delegate sceneDidEnterBackgroundFallback];
234  OCMVerify([plugin applicationDidEnterBackground:[UIApplication sharedApplication]]);
235 }
236 
237 - (void)testUnnecessarySceneDidEnterBackgroundFallback {
239  id plugin = OCMProtocolMock(@protocol(TestFlutterPluginWithSceneEvents));
240  [delegate addDelegate:plugin];
241 
242  [delegate sceneDidEnterBackgroundFallback];
243  OCMReject([plugin applicationDidEnterBackground:[OCMArg any]]);
244 }
245 
246 - (void)testWillEnterForeground {
247  XCTNSNotificationExpectation* expectation = [[XCTNSNotificationExpectation alloc]
248  initWithName:UIApplicationWillEnterForegroundNotification];
249 
251  id mockApplication = OCMClassMock([FlutterSharedApplication class]);
252  OCMStub([mockApplication hasSceneDelegate]).andReturn(NO);
253  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
254  [delegate addDelegate:plugin];
255  [[NSNotificationCenter defaultCenter]
256  postNotificationName:UIApplicationWillEnterForegroundNotification
257  object:nil];
258  [self waitForExpectations:@[ expectation ] timeout:5.0];
259  OCMVerify([plugin applicationWillEnterForeground:[UIApplication sharedApplication]]);
260 }
261 
262 - (void)testWillEnterForegroundWithUIScene {
263  XCTNSNotificationExpectation* expectation = [[XCTNSNotificationExpectation alloc]
264  initWithName:UIApplicationWillEnterForegroundNotification];
266  id mockApplication = OCMClassMock([FlutterSharedApplication class]);
267  OCMStub([mockApplication hasSceneDelegate]).andReturn(YES);
268  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
269  [delegate addDelegate:plugin];
270  [[NSNotificationCenter defaultCenter]
271  postNotificationName:UIApplicationWillEnterForegroundNotification
272  object:nil];
273 
274  [self waitForExpectations:@[ expectation ] timeout:5.0];
275  OCMReject([plugin applicationWillEnterForeground:[OCMArg any]]);
276 }
277 
278 - (void)testSceneWillEnterForegroundFallback {
280  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
281  [delegate addDelegate:plugin];
282 
283  [delegate sceneWillEnterForegroundFallback];
284  OCMVerify([plugin applicationWillEnterForeground:[UIApplication sharedApplication]]);
285 }
286 
287 - (void)testUnnecessarySceneWillEnterForegroundFallback {
289  id plugin = OCMProtocolMock(@protocol(TestFlutterPluginWithSceneEvents));
290  [delegate addDelegate:plugin];
291 
292  [delegate sceneWillEnterForegroundFallback];
293  OCMReject([plugin applicationWillEnterForeground:[OCMArg any]]);
294 }
295 
296 - (void)testWillResignActive {
297  XCTNSNotificationExpectation* expectation =
298  [[XCTNSNotificationExpectation alloc] initWithName:UIApplicationWillResignActiveNotification];
299 
301  id mockApplication = OCMClassMock([FlutterSharedApplication class]);
302  OCMStub([mockApplication hasSceneDelegate]).andReturn(NO);
303  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
304  [delegate addDelegate:plugin];
305  [[NSNotificationCenter defaultCenter]
306  postNotificationName:UIApplicationWillResignActiveNotification
307  object:nil];
308  [self waitForExpectations:@[ expectation ] timeout:5.0];
309  OCMVerify([plugin applicationWillResignActive:[UIApplication sharedApplication]]);
310 }
311 
312 - (void)testWillResignActiveWithUIScene {
313  XCTNSNotificationExpectation* expectation =
314  [[XCTNSNotificationExpectation alloc] initWithName:UIApplicationWillResignActiveNotification];
316  id mockApplication = OCMClassMock([FlutterSharedApplication class]);
317  OCMStub([mockApplication hasSceneDelegate]).andReturn(YES);
318  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
319  [delegate addDelegate:plugin];
320  [[NSNotificationCenter defaultCenter]
321  postNotificationName:UIApplicationWillResignActiveNotification
322  object:nil];
323 
324  [self waitForExpectations:@[ expectation ] timeout:5.0];
325  OCMReject([plugin applicationWillResignActive:[OCMArg any]]);
326 }
327 
328 - (void)testSceneWillResignActiveFallback {
330  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
331  [delegate addDelegate:plugin];
332 
333  [delegate sceneWillResignActiveFallback];
334  OCMVerify([plugin applicationWillResignActive:[UIApplication sharedApplication]]);
335 }
336 
337 - (void)testUnnecessarySceneWillResignActiveFallback {
339  id plugin = OCMProtocolMock(@protocol(TestFlutterPluginWithSceneEvents));
340  [delegate addDelegate:plugin];
341 
342  [delegate sceneWillResignActiveFallback];
343  OCMReject([plugin applicationWillResignActive:[OCMArg any]]);
344 }
345 
346 - (void)testDidBecomeActive {
347  XCTNSNotificationExpectation* expectation =
348  [[XCTNSNotificationExpectation alloc] initWithName:UIApplicationDidBecomeActiveNotification];
349 
351  id mockApplication = OCMClassMock([FlutterSharedApplication class]);
352  OCMStub([mockApplication hasSceneDelegate]).andReturn(NO);
353  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
354  [delegate addDelegate:plugin];
355  [[NSNotificationCenter defaultCenter]
356  postNotificationName:UIApplicationDidBecomeActiveNotification
357  object:nil];
358  [self waitForExpectations:@[ expectation ] timeout:5.0];
359  OCMVerify([plugin applicationDidBecomeActive:[UIApplication sharedApplication]]);
360 }
361 
362 - (void)testDidBecomeActiveWithUIScene {
363  XCTNSNotificationExpectation* expectation =
364  [[XCTNSNotificationExpectation alloc] initWithName:UIApplicationDidBecomeActiveNotification];
366  id mockApplication = OCMClassMock([FlutterSharedApplication class]);
367  OCMStub([mockApplication hasSceneDelegate]).andReturn(YES);
368  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
369  [delegate addDelegate:plugin];
370  [[NSNotificationCenter defaultCenter]
371  postNotificationName:UIApplicationDidBecomeActiveNotification
372  object:nil];
373 
374  [self waitForExpectations:@[ expectation ] timeout:5.0];
375  OCMReject([plugin applicationDidBecomeActive:[OCMArg any]]);
376 }
377 
378 - (void)testSceneDidBecomeActiveFallback {
380  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
381  [delegate addDelegate:plugin];
382 
383  [delegate sceneDidBecomeActiveFallback];
384  OCMVerify([plugin applicationDidBecomeActive:[UIApplication sharedApplication]]);
385 }
386 
387 - (void)testUnnecessarySceneDidBecomeActiveFallback {
389  id plugin = OCMProtocolMock(@protocol(TestFlutterPluginWithSceneEvents));
390  [delegate addDelegate:plugin];
391 
392  [delegate sceneDidBecomeActiveFallback];
393  OCMReject([plugin applicationDidBecomeActive:[OCMArg any]]);
394 }
395 
396 - (void)testSceneFallbackOpenURLContexts {
398  id plugin = [[FakePlugin alloc] init];
399  id mockPlugin = OCMPartialMock(plugin);
400  [delegate addDelegate:mockPlugin];
401 
402  id urlContext = OCMClassMock([UIOpenURLContext class]);
403  NSURL* url = [NSURL URLWithString:@"http://example.com"];
404  OCMStub([urlContext URL]).andReturn(url);
405  NSSet<UIOpenURLContext*>* urlContexts = [NSSet setWithObjects:urlContext, nil];
406 
407  NSDictionary<UIApplicationOpenURLOptionsKey, id>* expectedApplicationOptions = @{
408  UIApplicationOpenURLOptionsOpenInPlaceKey : @(NO),
409  };
410 
411  [delegate sceneFallbackOpenURLContexts:urlContexts];
412  OCMVerify([mockPlugin application:[UIApplication sharedApplication]
413  openURL:url
414  options:expectedApplicationOptions]);
415 }
416 
417 - (void)testConvertURLOptions {
419  id plugin = [[FakePlugin alloc] init];
420  id mockPlugin = OCMPartialMock(plugin);
421  [delegate addDelegate:mockPlugin];
422 
423  NSString* bundleId = @"app.bundle.id";
424  id annotation = @{@"key" : @"value"};
425  id eventAttribution = OCMClassMock([UIEventAttribution class]);
426 
427  UIOpenURLContext* urlContext = OCMClassMock([UIOpenURLContext class]);
428  NSURL* url = [NSURL URLWithString:@"http://example.com"];
429  OCMStub([urlContext URL]).andReturn(url);
430  id sceneOptions = OCMClassMock([UISceneOpenURLOptions class]);
431  OCMStub([sceneOptions sourceApplication]).andReturn(bundleId);
432  OCMStub([sceneOptions annotation]).andReturn(annotation);
433  OCMStub([sceneOptions openInPlace]).andReturn(YES);
434  OCMStub([sceneOptions eventAttribution]).andReturn(eventAttribution);
435 
436  OCMStub([urlContext options]).andReturn(sceneOptions);
437  NSSet<UIOpenURLContext*>* urlContexts = [NSSet setWithObjects:urlContext, nil];
438 
439  [delegate sceneFallbackOpenURLContexts:urlContexts];
440 
441  NSDictionary<UIApplicationOpenURLOptionsKey, id>* expectedApplicationOptions = @{
442  UIApplicationOpenURLOptionsSourceApplicationKey : bundleId,
443  UIApplicationOpenURLOptionsAnnotationKey : annotation,
444  UIApplicationOpenURLOptionsOpenInPlaceKey : @(YES),
445  UIApplicationOpenURLOptionsEventAttributionKey : eventAttribution,
446  };
447 
448  OCMVerify([mockPlugin application:[UIApplication sharedApplication]
449  openURL:url
450  options:expectedApplicationOptions]);
451 }
452 
453 - (void)testUnnecessarySceneFallbackOpenURLContexts {
455  id plugin = [[FakeTestFlutterPluginWithSceneEvents alloc] init];
456  id mockPlugin = OCMPartialMock(plugin);
457  [delegate addDelegate:mockPlugin];
458 
459  id urlContext = OCMClassMock([UIOpenURLContext class]);
460  NSSet<UIOpenURLContext*>* urlContexts = [NSSet setWithObjects:urlContext, nil];
461 
462  [delegate sceneFallbackOpenURLContexts:urlContexts];
463  OCMReject([mockPlugin application:[OCMArg any] openURL:[OCMArg any] options:[OCMArg any]]);
464 }
465 
466 - (void)testSceneFallbackContinueUserActivity {
468  id plugin = [[FakePlugin alloc] init];
469  id mockPlugin = OCMPartialMock(plugin);
470  [delegate addDelegate:mockPlugin];
471 
472  id userActivity = OCMClassMock([NSUserActivity class]);
473 
474  [delegate sceneFallbackContinueUserActivity:userActivity];
475  OCMVerify([mockPlugin application:[UIApplication sharedApplication]
476  continueUserActivity:userActivity
477  restorationHandler:[OCMArg any]]);
478 }
479 
480 - (void)testUnnecessarySceneFallbackContinueUserActivity {
482  id plugin = [[FakeTestFlutterPluginWithSceneEvents alloc] init];
483  id mockPlugin = OCMPartialMock(plugin);
484  [delegate addDelegate:mockPlugin];
485 
486  id userActivity = OCMClassMock([NSUserActivity class]);
487 
488  [delegate sceneFallbackContinueUserActivity:userActivity];
489  OCMReject([mockPlugin application:[UIApplication sharedApplication]
490  continueUserActivity:userActivity
491  restorationHandler:[OCMArg any]]);
492 }
493 
494 - (void)testSceneFallbackPerformActionForShortcutItem {
496  FakePlugin* plugin = [[FakePlugin alloc] init];
497  FakePlugin* mockPlugin = OCMPartialMock(plugin);
498  [delegate addDelegate:mockPlugin];
499 
500  id shortcut = OCMClassMock([UIApplicationShortcutItem class]);
501  id handler = ^(BOOL succeeded) {
502  };
503 
504  [delegate sceneFallbackPerformActionForShortcutItem:shortcut completionHandler:handler];
505  OCMVerify([mockPlugin application:[UIApplication sharedApplication]
506  performActionForShortcutItem:shortcut
507  completionHandler:handler]);
508 }
509 
510 - (void)testUnnecessarySceneFallbackPerformActionForShortcutItem {
514  FakeTestFlutterPluginWithSceneEvents* mockPlugin = OCMPartialMock(plugin);
515  [delegate addDelegate:mockPlugin];
516 
517  id shortcut = OCMClassMock([UIApplicationShortcutItem class]);
518  [delegate sceneFallbackPerformActionForShortcutItem:shortcut
519  completionHandler:^(BOOL succeeded){
520  }];
521  OCMReject([mockPlugin application:[OCMArg any]
522  performActionForShortcutItem:[OCMArg any]
523  completionHandler:[OCMArg any]]);
524 }
525 
526 - (void)testWillTerminate {
527  XCTNSNotificationExpectation* expectation =
528  [[XCTNSNotificationExpectation alloc] initWithName:UIApplicationWillTerminateNotification];
529 
531  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
532  [delegate addDelegate:plugin];
533  [[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillTerminateNotification
534  object:nil];
535  [self waitForExpectations:@[ expectation ] timeout:5.0];
536  OCMVerify([plugin applicationWillTerminate:[UIApplication sharedApplication]]);
537 }
538 
539 - (void)testReleasesPluginOnDealloc {
540  __weak id<FlutterApplicationLifeCycleDelegate> weakPlugin;
541  __weak FlutterPluginAppLifeCycleDelegate* weakDelegate;
542  @autoreleasepool {
543  FakePlugin* fakePlugin = [[FakePlugin alloc] init];
544  weakPlugin = fakePlugin;
546  [delegate addDelegate:fakePlugin];
547  weakDelegate = delegate;
548  }
549  XCTAssertNil(weakPlugin);
550  XCTAssertNil(weakDelegate);
551 }
552 
553 - (void)testApplicationWillFinishLaunchingSceneFallbackForwards {
555  id plugin = [[FakePlugin alloc] init];
556  id mockPlugin = OCMPartialMock(plugin);
557  [delegate addDelegate:mockPlugin];
558  id mockApplication = OCMClassMock([UIApplication class]);
559  NSDictionary* options = @{};
560 
561  [delegate sceneFallbackWillFinishLaunchingApplication:mockApplication];
562  OCMVerify(times(1), [mockPlugin application:mockApplication
563  willFinishLaunchingWithOptions:options]);
564 }
565 
566 - (void)testApplicationWillFinishLaunchingSceneFallbackNoForwardAfterWillLaunch {
568  id plugin = [[FakePlugin alloc] init];
569  id mockPlugin = OCMPartialMock(plugin);
570  [delegate addDelegate:mockPlugin];
571  id mockApplication = OCMClassMock([UIApplication class]);
572  NSDictionary* options = @{@"key" : @"value"};
573 
574  [delegate application:mockApplication willFinishLaunchingWithOptions:options];
575  [delegate sceneFallbackWillFinishLaunchingApplication:mockApplication];
576  OCMVerify(times(1), [mockPlugin application:mockApplication
577  willFinishLaunchingWithOptions:options]);
578 }
579 
580 - (void)testApplicationWillFinishLaunchingSceneFallbackNoForwardAfterDidLaunch {
582  id plugin = [[FakePlugin alloc] init];
583  id mockPlugin = OCMPartialMock(plugin);
584  [delegate addDelegate:mockPlugin];
585  id mockApplication = OCMClassMock([UIApplication class]);
586  NSDictionary* options = @{@"key" : @"value"};
587 
588  [delegate application:mockApplication didFinishLaunchingWithOptions:options];
589  [delegate sceneFallbackWillFinishLaunchingApplication:mockApplication];
590  OCMVerify(times(0), [mockPlugin application:mockApplication
591  willFinishLaunchingWithOptions:options]);
592 }
593 
594 - (void)testApplicationDidFinishLaunchingSceneFallbackForwards {
596  id plugin = [[FakePlugin alloc] init];
597  id mockPlugin = OCMPartialMock(plugin);
598  [delegate addDelegate:mockPlugin];
599  id mockApplication = OCMClassMock([UIApplication class]);
600  NSDictionary* options = @{};
601 
602  [delegate sceneFallbackDidFinishLaunchingApplication:mockApplication];
603  OCMVerify(times(1), [mockPlugin application:mockApplication
604  didFinishLaunchingWithOptions:options]);
605 }
606 
607 - (void)testApplicationDidFinishLaunchingSceneFallbackNoForward {
609  id plugin = [[FakePlugin alloc] init];
610  id mockPlugin = OCMPartialMock(plugin);
611  [delegate addDelegate:mockPlugin];
612  id mockApplication = OCMClassMock([UIApplication class]);
613  NSDictionary* options = @{@"key" : @"value"};
614 
615  [delegate application:mockApplication didFinishLaunchingWithOptions:options];
616  [delegate sceneFallbackDidFinishLaunchingApplication:mockApplication];
617  OCMVerify(times(1), [mockPlugin application:mockApplication
618  didFinishLaunchingWithOptions:options]);
619 }
620 
621 - (void)testCanAddDelegateDuringEnumeration {
623  MutatingPlugin* mutatingPlugin = [[MutatingPlugin alloc] init];
624  mutatingPlugin.lifecycleDelegate = delegate;
625  mutatingPlugin.shouldAdd = YES; // Add Mode
626 
627  [delegate addDelegate:mutatingPlugin];
628 
629  // Validation that [_delegates allObjects] (snapshotting) prevents NSGenericException crash
630  // when a plugin adds another plugin during the dispatch loop.
631  BOOL result = [delegate application:[UIApplication sharedApplication]
633 
634  XCTAssertTrue(result);
635 }
636 
637 - (void)testCanRemoveSelfDuringEnumeration {
639  MutatingPlugin* mutatingPlugin = [[MutatingPlugin alloc] init];
640  mutatingPlugin.lifecycleDelegate = delegate;
641  mutatingPlugin.shouldAdd = NO; // Delete Mode
642 
643  [delegate addDelegate:mutatingPlugin];
644 
645  // Validation that [_delegates allObjects] (snapshotting) prevents crash
646  // when a plugin removes itself via removeDelegate during the dispatch loop.
647  BOOL result = [delegate application:[UIApplication sharedApplication]
649 
650  XCTAssertTrue(result);
651 }
652 
653 @end
UITextSmartQuotesType smartQuotesType API_AVAILABLE(ios(11.0))
BOOL application:didFinishLaunchingWithOptions:(UIApplication *application,[didFinishLaunchingWithOptions] NSDictionary *launchOptions)
BOOL application:willFinishLaunchingWithOptions:(UIApplication *application,[willFinishLaunchingWithOptions] NSDictionary *launchOptions)
void addDelegate:(NSObject< FlutterApplicationLifeCycleDelegate > *delegate)
FlutterPluginAppLifeCycleDelegate * lifecycleDelegate