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 
10 
12 
14 @end
15 
16 @implementation FakePlugin
17 @end
18 
20 @end
21 
23 
24 - (void)testCreate {
26  XCTAssertNotNil(delegate);
27 }
28 
29 - (void)testDidEnterBackground {
30  XCTNSNotificationExpectation* expectation = [[XCTNSNotificationExpectation alloc]
31  initWithName:UIApplicationDidEnterBackgroundNotification];
33  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
34  [delegate addDelegate:plugin];
35  [[NSNotificationCenter defaultCenter]
36  postNotificationName:UIApplicationDidEnterBackgroundNotification
37  object:nil];
38 
39  [self waitForExpectations:@[ expectation ] timeout:5.0];
40  OCMVerify([plugin applicationDidEnterBackground:[UIApplication sharedApplication]]);
41 }
42 
43 - (void)testWillEnterForeground {
44  XCTNSNotificationExpectation* expectation = [[XCTNSNotificationExpectation alloc]
45  initWithName:UIApplicationWillEnterForegroundNotification];
46 
48  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
49  [delegate addDelegate:plugin];
50  [[NSNotificationCenter defaultCenter]
51  postNotificationName:UIApplicationWillEnterForegroundNotification
52  object:nil];
53  [self waitForExpectations:@[ expectation ] timeout:5.0];
54  OCMVerify([plugin applicationWillEnterForeground:[UIApplication sharedApplication]]);
55 }
56 
57 - (void)testWillResignActive {
58  XCTNSNotificationExpectation* expectation =
59  [[XCTNSNotificationExpectation alloc] initWithName:UIApplicationWillResignActiveNotification];
60 
62  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
63  [delegate addDelegate:plugin];
64  [[NSNotificationCenter defaultCenter]
65  postNotificationName:UIApplicationWillResignActiveNotification
66  object:nil];
67  [self waitForExpectations:@[ expectation ] timeout:5.0];
68  OCMVerify([plugin applicationWillResignActive:[UIApplication sharedApplication]]);
69 }
70 
71 - (void)testDidBecomeActive {
72  XCTNSNotificationExpectation* expectation =
73  [[XCTNSNotificationExpectation alloc] initWithName:UIApplicationDidBecomeActiveNotification];
74 
76  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
77  [delegate addDelegate:plugin];
78  [[NSNotificationCenter defaultCenter]
79  postNotificationName:UIApplicationDidBecomeActiveNotification
80  object:nil];
81  [self waitForExpectations:@[ expectation ] timeout:5.0];
82  OCMVerify([plugin applicationDidBecomeActive:[UIApplication sharedApplication]]);
83 }
84 
85 - (void)testWillTerminate {
86  XCTNSNotificationExpectation* expectation =
87  [[XCTNSNotificationExpectation alloc] initWithName:UIApplicationWillTerminateNotification];
88 
90  id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
91  [delegate addDelegate:plugin];
92  [[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillTerminateNotification
93  object:nil];
94  [self waitForExpectations:@[ expectation ] timeout:5.0];
95  OCMVerify([plugin applicationWillTerminate:[UIApplication sharedApplication]]);
96 }
97 
98 - (void)testReleasesPluginOnDealloc {
99  __weak id<FlutterApplicationLifeCycleDelegate> weakPlugin;
100  __weak FlutterPluginAppLifeCycleDelegate* weakDelegate;
101  @autoreleasepool {
102  FakePlugin* fakePlugin = [[FakePlugin alloc] init];
103  weakPlugin = fakePlugin;
105  [delegate addDelegate:fakePlugin];
106  weakDelegate = delegate;
107  }
108  XCTAssertNil(weakPlugin);
109  XCTAssertNil(weakDelegate);
110 }
111 
112 @end
void addDelegate:(NSObject< FlutterApplicationLifeCycleDelegate > *delegate)