Flutter iOS Embedder
FlutterAppDelegateTest.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 @interface FlutterAppDelegateTest : XCTestCase
17 @property(strong) FlutterAppDelegate* appDelegate;
18 
19 @property(strong) id mockMainBundle;
20 @property(strong) id mockNavigationChannel;
21 
22 // Retain callback until the tests are done.
23 // https://github.com/flutter/flutter/issues/74267
24 @property(strong) id mockEngineFirstFrameCallback;
25 @end
26 
27 @implementation FlutterAppDelegateTest
28 
29 - (void)setUp {
30  [super setUp];
31 
32  id mockMainBundle = OCMClassMock([NSBundle class]);
33  OCMStub([mockMainBundle mainBundle]).andReturn(mockMainBundle);
34  self.mockMainBundle = mockMainBundle;
35 
37  self.appDelegate = appDelegate;
38 
40  FlutterMethodChannel* navigationChannel = OCMClassMock([FlutterMethodChannel class]);
41  self.mockNavigationChannel = navigationChannel;
42 
43  FlutterEngine* engine = OCMClassMock([FlutterEngine class]);
44  OCMStub([engine navigationChannel]).andReturn(navigationChannel);
45  OCMStub([viewController engine]).andReturn(engine);
46 
47  id mockEngineFirstFrameCallback = [OCMArg invokeBlockWithArgs:@NO, nil];
48  self.mockEngineFirstFrameCallback = mockEngineFirstFrameCallback;
49  OCMStub([engine waitForFirstFrame:3.0 callback:mockEngineFirstFrameCallback]);
51  return viewController;
52  };
53 }
54 
55 - (void)tearDown {
56  // Explicitly stop mocking the NSBundle class property.
57  [self.mockMainBundle stopMocking];
58  [super tearDown];
59 }
60 
61 - (void)testLaunchUrl {
62  OCMStub([self.mockMainBundle objectForInfoDictionaryKey:@"FlutterDeepLinkingEnabled"])
63  .andReturn(@YES);
64 
65  OCMStub([self.mockNavigationChannel
66  invokeMethod:@"pushRouteInformation"
67  arguments:@{@"location" : @"http://myApp/custom/route?query=test"}])
68  .andReturn(@YES);
69 
70  BOOL result =
71  [self.appDelegate application:[UIApplication sharedApplication]
72  openURL:[NSURL URLWithString:@"http://myApp/custom/route?query=test"]
73  options:@{}];
74 
75  XCTAssertTrue(result);
76  OCMVerifyAll(self.mockNavigationChannel);
77 }
78 
79 - (void)testLaunchUrlWithDeepLinkingNotSet {
80  OCMStub([self.mockMainBundle objectForInfoDictionaryKey:@"FlutterDeepLinkingEnabled"])
81  .andReturn(nil);
82 
83  BOOL result =
84  [self.appDelegate application:[UIApplication sharedApplication]
85  openURL:[NSURL URLWithString:@"http://myApp/custom/route?query=test"]
86  options:@{}];
87  XCTAssertFalse(result);
88  OCMReject([self.mockNavigationChannel invokeMethod:OCMOCK_ANY arguments:OCMOCK_ANY]);
89 }
90 
91 - (void)testLaunchUrlWithDeepLinkingDisabled {
92  OCMStub([self.mockMainBundle objectForInfoDictionaryKey:@"FlutterDeepLinkingEnabled"])
93  .andReturn(@NO);
94 
95  BOOL result =
96  [self.appDelegate application:[UIApplication sharedApplication]
97  openURL:[NSURL URLWithString:@"http://myApp/custom/route?query=test"]
98  options:@{}];
99  XCTAssertFalse(result);
100  OCMReject([self.mockNavigationChannel invokeMethod:OCMOCK_ANY arguments:OCMOCK_ANY]);
101 }
102 
103 - (void)testLaunchUrlWithQueryParameterAndFragment {
104  OCMStub([self.mockMainBundle objectForInfoDictionaryKey:@"FlutterDeepLinkingEnabled"])
105  .andReturn(@YES);
106  OCMStub([self.mockNavigationChannel
107  invokeMethod:@"pushRouteInformation"
108  arguments:@{@"location" : @"http://myApp/custom/route?query=test#fragment"}])
109  .andReturn(@YES);
110  BOOL result = [self.appDelegate
111  application:[UIApplication sharedApplication]
112  openURL:[NSURL URLWithString:@"http://myApp/custom/route?query=test#fragment"]
113  options:@{}];
114  XCTAssertTrue(result);
115  OCMVerifyAll(self.mockNavigationChannel);
116 }
117 
118 - (void)testLaunchUrlWithFragmentNoQueryParameter {
119  OCMStub([self.mockMainBundle objectForInfoDictionaryKey:@"FlutterDeepLinkingEnabled"])
120  .andReturn(@YES);
121  OCMStub([self.mockNavigationChannel
122  invokeMethod:@"pushRouteInformation"
123  arguments:@{@"location" : @"http://myApp/custom/route#fragment"}])
124  .andReturn(@YES);
125  BOOL result =
126  [self.appDelegate application:[UIApplication sharedApplication]
127  openURL:[NSURL URLWithString:@"http://myApp/custom/route#fragment"]
128  options:@{}];
129  XCTAssertTrue(result);
130  OCMVerifyAll(self.mockNavigationChannel);
131 }
132 
133 - (void)testReleasesWindowOnDealloc {
134  __weak UIWindow* weakWindow;
135  @autoreleasepool {
136  id mockWindow = OCMClassMock([UIWindow class]);
138  appDelegate.window = mockWindow;
139  weakWindow = mockWindow;
140  XCTAssertNotNil(weakWindow);
141  [mockWindow stopMocking];
142  mockWindow = nil;
143  appDelegate = nil;
144  }
145  // App delegate has released the window.
146  XCTAssertNil(weakWindow);
147 }
148 
149 #pragma mark - Deep linking
150 
151 - (void)testUniversalLinkPushRouteInformation {
152  OCMStub([self.mockMainBundle objectForInfoDictionaryKey:@"FlutterDeepLinkingEnabled"])
153  .andReturn(@YES);
154  OCMStub([self.mockNavigationChannel
155  invokeMethod:@"pushRouteInformation"
156  arguments:@{@"location" : @"http://myApp/custom/route?query=test"}])
157  .andReturn(@YES);
158  NSUserActivity* userActivity = [[NSUserActivity alloc] initWithActivityType:@"com.example.test"];
159  userActivity.webpageURL = [NSURL URLWithString:@"http://myApp/custom/route?query=test"];
160  BOOL result = [self.appDelegate
161  application:[UIApplication sharedApplication]
162  continueUserActivity:userActivity
163  restorationHandler:^(NSArray<id<UIUserActivityRestoring>>* __nullable restorableObjects){
164  }];
165  XCTAssertTrue(result);
166  OCMVerifyAll(self.mockNavigationChannel);
167 }
168 
169 @end
FlutterEngine
Definition: FlutterEngine.h:61
FlutterAppDelegateTest::mockEngineFirstFrameCallback
id mockEngineFirstFrameCallback
Definition: FlutterAppDelegateTest.mm:24
FlutterAppDelegateTest
Definition: FlutterAppDelegateTest.mm:16
FlutterViewController
Definition: FlutterViewController.h:56
FlutterMethodChannel
Definition: FlutterChannels.h:220
FlutterEngine.h
FlutterEngine_Test.h
FlutterAppDelegateTest::appDelegate
FlutterAppDelegate * appDelegate
Definition: FlutterAppDelegateTest.mm:17
FlutterAppDelegateTest::mockMainBundle
id mockMainBundle
Definition: FlutterAppDelegateTest.mm:19
viewController
FlutterViewController * viewController
Definition: FlutterTextInputPluginTest.mm:92
FlutterAppDelegate::window
FlutterAppLifeCycleProvider UIWindow * window
Definition: FlutterAppDelegate.h:30
FlutterAppDelegate
Definition: FlutterAppDelegate.h:27
FlutterAppDelegate.h
FlutterAppDelegateTest::mockNavigationChannel
id mockNavigationChannel
Definition: FlutterAppDelegateTest.mm:20
engine
id engine
Definition: FlutterTextInputPluginTest.mm:89
FlutterAppDelegate::rootFlutterViewControllerGetter
FlutterViewController *(^ rootFlutterViewControllerGetter)(void)
FlutterAppDelegate_Test.h
FLUTTER_ASSERT_ARC
Definition: FlutterChannelKeyResponder.mm:13
FlutterViewController.h