| 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
8 #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
9 #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h"
10
11 FLUTTER_ASSERT_ARC
12
13 @interface FakePlugin : NSObject <FlutterApplicationLifeCycleDelegate>
14 @end
15
16 @implementation FakePlugin
17 @end
18
19 @interface FlutterPluginAppLifeCycleDelegateTest : XCTestCase
20 @end
21
22 @implementation FlutterPluginAppLifeCycleDelegateTest
23
24 - (void)testCreate {
25 FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
26 XCTAssertNotNil(delegate);
27 }
28
29 - (void)testDidEnterBackground {
30 XCTNSNotificationExpectation* expectation = [[XCTNSNotificationExpectation alloc]
31 initWithName:UIApplicationDidEnterBackgroundNotification];
32 FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
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
47 FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
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
61 FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
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
75 FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
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
89 FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
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;
104 FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init];
105 [delegate addDelegate:fakePlugin];
106 weakDelegate = delegate;
107 }
108 XCTAssertNil(weakPlugin);
109 XCTAssertNil(weakDelegate);
110 }
111
112 @end
FlutterMacros.h
FlutterPluginAppLifeCycleDelegate.h
FakePlugin
Definition: FlutterPluginAppLifeCycleDelegateTest.mm:13
FlutterPluginAppLifeCycleDelegate
Definition: FlutterPluginAppLifeCycleDelegate.h:16
-[FlutterPluginAppLifeCycleDelegate addDelegate:]
void addDelegate:(NSObject< FlutterApplicationLifeCycleDelegate > *delegate)
Definition: FlutterPluginAppLifeCycleDelegate.mm:92
FlutterPluginAppLifeCycleDelegateTest
Definition: FlutterPluginAppLifeCycleDelegateTest.mm:20
FLUTTER_ASSERT_ARC
Definition: FlutterChannelKeyResponder.mm:13
FlutterApplicationLifeCycleDelegate-p
Definition: FlutterPlugin.h:25
FlutterPlugin-p
Definition: FlutterPlugin.h:189