Flutter iOS Embedder
FlutterPlatformPluginTest.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/InternalFlutterSwiftCommon/InternalFlutterSwiftCommon.h"
15 
17 
18 @interface FlutterPlatformPluginTest : XCTestCase
19 @end
20 
21 @interface FlutterPlatformPlugin ()
22 - (BOOL)isLiveTextInputAvailable;
23 - (void)searchWeb:(NSString*)searchTerm;
24 - (void)showLookUpViewController:(NSString*)term;
25 - (void)showShareViewController:(NSString*)content;
26 - (void)playSystemSound:(NSString*)soundType;
27 - (void)vibrateHapticFeedback:(NSString*)feedbackType;
28 - (void)showTranslateViewControllerForTerm:(NSString*)term;
29 @end
30 
31 @interface UIViewController ()
32 - (void)presentViewController:(UIViewController*)viewControllerToPresent
33  animated:(BOOL)flag
34  completion:(void (^)(void))completion;
35 @end
36 
37 @implementation FlutterPlatformPluginTest
38 
39 - (void)testTranslateInvoked {
40  if (@available(iOS 17.4, *)) {
41  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
42  [engine runWithEntrypoint:nil];
43 
44  FlutterViewController* engineViewController =
45  [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
46  FlutterViewController* mockEngineViewController = OCMPartialMock(engineViewController);
47 
48  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
49 
50  FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"Translate.invoke"
51  arguments:@"Test"];
52  __block BOOL resultCalled = NO;
53  [plugin handleMethodCall:methodCall
54  result:^(id result) {
55  resultCalled = YES;
56  }];
57  XCTAssertTrue(resultCalled);
58  OCMVerify([mockEngineViewController
59  addChildViewController:[OCMArg isKindOfClass:[FlutterTranslateViewController class]]]);
60  }
61 }
62 
63 - (void)testTranslateNotInvokedWhenUnavailable {
64  if (!@available(iOS 17.4, *)) {
65  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
66  [engine runWithEntrypoint:nil];
67 
68  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
69 
70  FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"Translate.invoke"
71  arguments:@"Test"];
72  __block BOOL resultCalled = NO;
73  [plugin handleMethodCall:methodCall
74  result:^(id result) {
75  XCTAssertEqualObjects(result, FlutterMethodNotImplemented);
76  resultCalled = YES;
77  }];
78  XCTAssertTrue(resultCalled);
79  }
80 }
81 
82 - (void)testTranslateInvokedWithEmptyTermDoesNotAddChildViewController {
83  if (@available(iOS 17.4, *)) {
84  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
85  [engine runWithEntrypoint:nil];
86 
87  FlutterViewController* engineViewController =
88  [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
89  FlutterViewController* mockEngineViewController = OCMPartialMock(engineViewController);
90 
91  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
92 
93  OCMReject([mockEngineViewController
94  addChildViewController:[OCMArg isKindOfClass:[FlutterTranslateViewController class]]]);
95 
96  FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"Translate.invoke"
97  arguments:@""];
98  __block BOOL resultCalled = NO;
99  [plugin handleMethodCall:methodCall
100  result:^(id result) {
101  resultCalled = YES;
102  }];
103  XCTAssertTrue(resultCalled);
104  }
105 }
106 
107 - (void)testSearchWebInvokedWithEscapedTerm {
108  id mockApplication = OCMClassMock([UIApplication class]);
109  OCMStub([mockApplication sharedApplication]).andReturn(mockApplication);
110 
111  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
112  [engine runWithEntrypoint:nil];
113 
114  XCTestExpectation* invokeExpectation =
115  [self expectationWithDescription:@"Web search launched with escaped search term"];
116 
117  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
118  FlutterPlatformPlugin* mockPlugin = OCMPartialMock(plugin);
119 
120  FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"SearchWeb.invoke"
121  arguments:@"Testing Word!"];
122 
123  FlutterResult result = ^(id result) {
124  OCMVerify([mockPlugin searchWeb:@"Testing Word!"]);
125  OCMVerify([mockApplication openURL:[NSURL URLWithString:@"x-web-search://?Testing%20Word!"]
126  options:@{}
127  completionHandler:nil]);
128  [invokeExpectation fulfill];
129  };
130 
131  [mockPlugin handleMethodCall:methodCall result:result];
132  [self waitForExpectationsWithTimeout:1 handler:nil];
133  [mockApplication stopMocking];
134 }
135 
136 - (void)testSearchWebSkippedIfAppExtension {
137  id mockBundle = OCMPartialMock([NSBundle mainBundle]);
138  OCMStub([mockBundle objectForInfoDictionaryKey:@"NSExtension"]).andReturn(@{
139  @"NSExtensionPointIdentifier" : @"com.apple.share-services"
140  });
141  id mockApplication = OCMClassMock([UIApplication class]);
142  OCMStub([mockApplication sharedApplication]).andReturn(mockApplication);
143  OCMReject([mockApplication openURL:OCMOCK_ANY options:OCMOCK_ANY completionHandler:OCMOCK_ANY]);
144  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
145  [engine runWithEntrypoint:nil];
146 
147  XCTestExpectation* invokeExpectation =
148  [self expectationWithDescription:@"Web search launched with non escaped search term"];
149 
150  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
151  FlutterPlatformPlugin* mockPlugin = OCMPartialMock(plugin);
152 
153  FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"SearchWeb.invoke"
154  arguments:@"Test"];
155 
156  FlutterResult result = ^(id result) {
157  OCMVerify([mockPlugin searchWeb:@"Test"]);
158 
159  [invokeExpectation fulfill];
160  };
161 
162  [mockPlugin handleMethodCall:methodCall result:result];
163  [self waitForExpectationsWithTimeout:1 handler:nil];
164  [mockBundle stopMocking];
165  [mockApplication stopMocking];
166 }
167 
168 - (void)testLookUpCallInitiated {
169  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
170  [engine runWithEntrypoint:nil];
171 
172  XCTestExpectation* presentExpectation =
173  [self expectationWithDescription:@"Look Up view controller presented"];
174 
175  FlutterViewController* engineViewController = [[FlutterViewController alloc] initWithEngine:engine
176  nibName:nil
177  bundle:nil];
178  FlutterViewController* mockEngineViewController = OCMPartialMock(engineViewController);
179 
180  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
181  FlutterPlatformPlugin* mockPlugin = OCMPartialMock(plugin);
182 
183  FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"LookUp.invoke"
184  arguments:@"Test"];
185  FlutterResult result = ^(id result) {
186  OCMVerify([mockEngineViewController
187  presentViewController:[OCMArg isKindOfClass:[UIReferenceLibraryViewController class]]
188  animated:YES
189  completion:nil]);
190  [presentExpectation fulfill];
191  };
192  [mockPlugin handleMethodCall:methodCall result:result];
193  [self waitForExpectationsWithTimeout:2 handler:nil];
194 }
195 
196 - (void)testShareScreenInvoked {
197  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
198  [engine runWithEntrypoint:nil];
199 
200  XCTestExpectation* presentExpectation =
201  [self expectationWithDescription:@"Share view controller presented"];
202 
203  FlutterViewController* engineViewController = [[FlutterViewController alloc] initWithEngine:engine
204  nibName:nil
205  bundle:nil];
206  FlutterViewController* mockEngineViewController = OCMPartialMock(engineViewController);
207  OCMStub([mockEngineViewController
208  presentViewController:[OCMArg isKindOfClass:[UIActivityViewController class]]
209  animated:YES
210  completion:nil]);
211 
212  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
213  FlutterPlatformPlugin* mockPlugin = OCMPartialMock(plugin);
214 
215  FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"Share.invoke"
216  arguments:@"Test"];
217  FlutterResult result = ^(id result) {
218  OCMVerify([mockEngineViewController
219  presentViewController:[OCMArg isKindOfClass:[UIActivityViewController class]]
220  animated:YES
221  completion:nil]);
222  [presentExpectation fulfill];
223  };
224  [mockPlugin handleMethodCall:methodCall result:result];
225  [self waitForExpectationsWithTimeout:1 handler:nil];
226 }
227 
228 - (void)testShareScreenInvokedOnIPad {
229  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
230  [engine runWithEntrypoint:nil];
231 
232  XCTestExpectation* presentExpectation =
233  [self expectationWithDescription:@"Share view controller presented on iPad"];
234 
235  FlutterViewController* engineViewController = [[FlutterViewController alloc] initWithEngine:engine
236  nibName:nil
237  bundle:nil];
238  FlutterViewController* mockEngineViewController = OCMPartialMock(engineViewController);
239  OCMStub([mockEngineViewController
240  presentViewController:[OCMArg isKindOfClass:[UIActivityViewController class]]
241  animated:YES
242  completion:nil]);
243 
244  id mockTraitCollection = OCMClassMock([UITraitCollection class]);
245  OCMStub([mockTraitCollection userInterfaceIdiom]).andReturn(UIUserInterfaceIdiomPad);
246 
247  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
248  FlutterPlatformPlugin* mockPlugin = OCMPartialMock(plugin);
249 
250  FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"Share.invoke"
251  arguments:@"Test"];
252  FlutterResult result = ^(id result) {
253  OCMVerify([mockEngineViewController
254  presentViewController:[OCMArg isKindOfClass:[UIActivityViewController class]]
255  animated:YES
256  completion:nil]);
257  [presentExpectation fulfill];
258  };
259  [mockPlugin handleMethodCall:methodCall result:result];
260  [self waitForExpectationsWithTimeout:1 handler:nil];
261 }
262 
263 - (void)testClipboardHasCorrectStrings {
264  [UIPasteboard generalPasteboard].string = nil;
265  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
266  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
267 
268  XCTestExpectation* setStringExpectation = [self expectationWithDescription:@"setString"];
269  FlutterResult resultSet = ^(id result) {
270  [setStringExpectation fulfill];
271  };
272  FlutterMethodCall* methodCallSet =
273  [FlutterMethodCall methodCallWithMethodName:@"Clipboard.setData"
274  arguments:@{@"text" : @"some string"}];
275  [plugin handleMethodCall:methodCallSet result:resultSet];
276  [self waitForExpectationsWithTimeout:1 handler:nil];
277 
278  XCTestExpectation* hasStringsExpectation = [self expectationWithDescription:@"hasStrings"];
279  FlutterResult result = ^(id result) {
280  XCTAssertTrue([result[@"value"] boolValue]);
281  [hasStringsExpectation fulfill];
282  };
283  FlutterMethodCall* methodCall =
284  [FlutterMethodCall methodCallWithMethodName:@"Clipboard.hasStrings" arguments:nil];
285  [plugin handleMethodCall:methodCall result:result];
286  [self waitForExpectationsWithTimeout:1 handler:nil];
287 
288  XCTestExpectation* getDataExpectation = [self expectationWithDescription:@"getData"];
289  FlutterResult getDataResult = ^(id result) {
290  XCTAssertEqualObjects(result[@"text"], @"some string");
291  [getDataExpectation fulfill];
292  };
293  FlutterMethodCall* methodCallGetData =
294  [FlutterMethodCall methodCallWithMethodName:@"Clipboard.getData" arguments:@"text/plain"];
295  [plugin handleMethodCall:methodCallGetData result:getDataResult];
296  [self waitForExpectationsWithTimeout:1 handler:nil];
297 }
298 
299 - (void)testClipboardSetDataToNullDoNotCrash {
300  [UIPasteboard generalPasteboard].string = nil;
301  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
302  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
303 
304  XCTestExpectation* setStringExpectation = [self expectationWithDescription:@"setData"];
305  FlutterResult resultSet = ^(id result) {
306  [setStringExpectation fulfill];
307  };
308  FlutterMethodCall* methodCallSet =
309  [FlutterMethodCall methodCallWithMethodName:@"Clipboard.setData"
310  arguments:@{@"text" : [NSNull null]}];
311  [plugin handleMethodCall:methodCallSet result:resultSet];
312 
313  XCTestExpectation* getDataExpectation = [self expectationWithDescription:@"getData"];
314  FlutterResult result = ^(id result) {
315  XCTAssertEqualObjects(result[@"text"], @"null");
316  [getDataExpectation fulfill];
317  };
318  FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"Clipboard.getData"
319  arguments:@"text/plain"];
320  [plugin handleMethodCall:methodCall result:result];
321  [self waitForExpectationsWithTimeout:1 handler:nil];
322 }
323 
324 - (void)testPopSystemNavigator {
325  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
326  [engine runWithEntrypoint:nil];
327  FlutterViewController* flutterViewController =
328  [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
329  UINavigationController* navigationController =
330  [[UINavigationController alloc] initWithRootViewController:flutterViewController];
331  UITabBarController* tabBarController = [[UITabBarController alloc] init];
332  tabBarController.viewControllers = @[ navigationController ];
333  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
334 
335  id navigationControllerMock = OCMPartialMock(navigationController);
336  OCMStub([navigationControllerMock popViewControllerAnimated:YES]);
337  // Set some string to the pasteboard.
338  XCTestExpectation* navigationPopCalled = [self expectationWithDescription:@"SystemNavigator.pop"];
339  FlutterResult resultSet = ^(id result) {
340  [navigationPopCalled fulfill];
341  };
342  FlutterMethodCall* methodCallSet =
343  [FlutterMethodCall methodCallWithMethodName:@"SystemNavigator.pop" arguments:@(YES)];
344  [plugin handleMethodCall:methodCallSet result:resultSet];
345  [self waitForExpectationsWithTimeout:1 handler:nil];
346  OCMVerify([navigationControllerMock popViewControllerAnimated:YES]);
347 
348  [flutterViewController deregisterNotifications];
349 }
350 
351 - (void)testWhetherDeviceHasLiveTextInputInvokeCorrectly {
352  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
353  XCTestExpectation* invokeExpectation =
354  [self expectationWithDescription:@"isLiveTextInputAvailableInvoke"];
355  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
356  FlutterPlatformPlugin* mockPlugin = OCMPartialMock(plugin);
357  FlutterMethodCall* methodCall =
358  [FlutterMethodCall methodCallWithMethodName:@"LiveText.isLiveTextInputAvailable"
359  arguments:nil];
360  FlutterResult result = ^(id result) {
361  OCMVerify([mockPlugin isLiveTextInputAvailable]);
362  [invokeExpectation fulfill];
363  };
364  [mockPlugin handleMethodCall:methodCall result:result];
365  [self waitForExpectationsWithTimeout:1 handler:nil];
366 }
367 
368 - (void)testSystemSoundPlay {
369  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
370  XCTestExpectation* invokeExpectation =
371  [self expectationWithDescription:@"SystemSound.play invoked"];
372  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
373  FlutterPlatformPlugin* mockPlugin = OCMPartialMock(plugin);
374 
375  FlutterMethodCall* methodCall =
376  [FlutterMethodCall methodCallWithMethodName:@"SystemSound.play"
377  arguments:@"SystemSoundType.click"];
378  FlutterResult result = ^(id result) {
379  OCMVerify([mockPlugin playSystemSound:@"SystemSoundType.click"]);
380  [invokeExpectation fulfill];
381  };
382  [mockPlugin handleMethodCall:methodCall result:result];
383  [self waitForExpectationsWithTimeout:1 handler:nil];
384 }
385 
386 - (void)testHapticFeedbackVibrate {
387  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
388  XCTestExpectation* invokeExpectation =
389  [self expectationWithDescription:@"HapticFeedback.vibrate invoked"];
390  FlutterPlatformPlugin* plugin = [[FlutterPlatformPlugin alloc] initWithEngine:engine];
391  FlutterPlatformPlugin* mockPlugin = OCMPartialMock(plugin);
392 
393  FlutterMethodCall* methodCall =
394  [FlutterMethodCall methodCallWithMethodName:@"HapticFeedback.vibrate"
395  arguments:@"HapticFeedbackType.lightImpact"];
396  FlutterResult result = ^(id result) {
397  OCMVerify([mockPlugin vibrateHapticFeedback:@"HapticFeedbackType.lightImpact"]);
398  [invokeExpectation fulfill];
399  };
400  [mockPlugin handleMethodCall:methodCall result:result];
401  [self waitForExpectationsWithTimeout:1 handler:nil];
402 }
403 
404 - (void)testViewControllerBasedStatusBarHiddenUpdate {
405  id bundleMock = OCMPartialMock([NSBundle mainBundle]);
406  OCMStub([bundleMock objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"])
407  .andReturn(@YES);
408  {
409  // Enabling system UI overlays to update status bar.
410  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
411  [engine runWithEntrypoint:nil];
412  FlutterViewController* flutterViewController =
413  [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
414  XCTAssertFalse(flutterViewController.prefersStatusBarHidden);
415 
416  // Update to hidden.
417  FlutterPlatformPlugin* plugin = [engine platformPlugin];
418 
419  XCTestExpectation* enableSystemUIOverlaysCalled =
420  [self expectationWithDescription:@"setEnabledSystemUIOverlays"];
421  FlutterResult resultSet = ^(id result) {
422  [enableSystemUIOverlaysCalled fulfill];
423  };
424  FlutterMethodCall* methodCallSet =
425  [FlutterMethodCall methodCallWithMethodName:@"SystemChrome.setEnabledSystemUIOverlays"
426  arguments:@[ @"SystemUiOverlay.bottom" ]];
427  [plugin handleMethodCall:methodCallSet result:resultSet];
428  [self waitForExpectationsWithTimeout:1 handler:nil];
429  XCTAssertTrue(flutterViewController.prefersStatusBarHidden);
430 
431  // Update to shown.
432  XCTestExpectation* enableSystemUIOverlaysCalled2 =
433  [self expectationWithDescription:@"setEnabledSystemUIOverlays"];
434  FlutterResult resultSet2 = ^(id result) {
435  [enableSystemUIOverlaysCalled2 fulfill];
436  };
437  FlutterMethodCall* methodCallSet2 =
438  [FlutterMethodCall methodCallWithMethodName:@"SystemChrome.setEnabledSystemUIOverlays"
439  arguments:@[ @"SystemUiOverlay.top" ]];
440  [plugin handleMethodCall:methodCallSet2 result:resultSet2];
441  [self waitForExpectationsWithTimeout:1 handler:nil];
442  XCTAssertFalse(flutterViewController.prefersStatusBarHidden);
443 
444  [flutterViewController deregisterNotifications];
445  }
446  {
447  // Enable system UI mode to update status bar.
448  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
449  [engine runWithEntrypoint:nil];
450  FlutterViewController* flutterViewController =
451  [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
452  XCTAssertFalse(flutterViewController.prefersStatusBarHidden);
453 
454  // Update to hidden.
455  FlutterPlatformPlugin* plugin = [engine platformPlugin];
456 
457  XCTestExpectation* enableSystemUIModeCalled =
458  [self expectationWithDescription:@"setEnabledSystemUIMode"];
459  FlutterResult resultSet = ^(id result) {
460  [enableSystemUIModeCalled fulfill];
461  };
462  FlutterMethodCall* methodCallSet =
463  [FlutterMethodCall methodCallWithMethodName:@"SystemChrome.setEnabledSystemUIMode"
464  arguments:@"SystemUiMode.immersive"];
465  [plugin handleMethodCall:methodCallSet result:resultSet];
466  [self waitForExpectationsWithTimeout:1 handler:nil];
467  XCTAssertTrue(flutterViewController.prefersStatusBarHidden);
468 
469  // Update to shown.
470  XCTestExpectation* enableSystemUIModeCalled2 =
471  [self expectationWithDescription:@"setEnabledSystemUIMode"];
472  FlutterResult resultSet2 = ^(id result) {
473  [enableSystemUIModeCalled2 fulfill];
474  };
475  FlutterMethodCall* methodCallSet2 =
476  [FlutterMethodCall methodCallWithMethodName:@"SystemChrome.setEnabledSystemUIMode"
477  arguments:@"SystemUiMode.edgeToEdge"];
478  [plugin handleMethodCall:methodCallSet2 result:resultSet2];
479  [self waitForExpectationsWithTimeout:1 handler:nil];
480  XCTAssertFalse(flutterViewController.prefersStatusBarHidden);
481 
482  [flutterViewController deregisterNotifications];
483  }
484  [bundleMock stopMocking];
485 }
486 
487 - (void)testStatusBarHiddenUpdate {
488  id bundleMock = OCMPartialMock([NSBundle mainBundle]);
489  OCMStub([bundleMock objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"])
490  .andReturn(@NO);
491  id mockApplication = OCMClassMock([UIApplication class]);
492  OCMStub([mockApplication sharedApplication]).andReturn(mockApplication);
493 
494  // Enabling system UI overlays to update status bar.
495  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
496  [engine runWithEntrypoint:nil];
497  FlutterViewController* flutterViewController =
498  [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
499 
500  // Update the visibility of the status bar to hidden.
501  FlutterPlatformPlugin* plugin = [engine platformPlugin];
502 
503  XCTestExpectation* systemOverlaysBottomExpectation =
504  [self expectationWithDescription:@"setEnabledSystemUIOverlays"];
505  FlutterResult systemOverlaysBottomResult = ^(id result) {
506  [systemOverlaysBottomExpectation fulfill];
507  };
508  FlutterMethodCall* setSystemOverlaysBottomCall =
509  [FlutterMethodCall methodCallWithMethodName:@"SystemChrome.setEnabledSystemUIOverlays"
510  arguments:@[ @"SystemUiOverlay.bottom" ]];
511  [plugin handleMethodCall:setSystemOverlaysBottomCall result:systemOverlaysBottomResult];
512  [self waitForExpectationsWithTimeout:1 handler:nil];
513  OCMVerify([mockApplication setStatusBarHidden:YES]);
514 
515  // Update the visibility of the status bar to shown.
516  XCTestExpectation* systemOverlaysTopExpectation =
517  [self expectationWithDescription:@"setEnabledSystemUIOverlays"];
518  FlutterResult systemOverlaysTopResult = ^(id result) {
519  [systemOverlaysTopExpectation fulfill];
520  };
521  FlutterMethodCall* setSystemOverlaysTopCall =
522  [FlutterMethodCall methodCallWithMethodName:@"SystemChrome.setEnabledSystemUIOverlays"
523  arguments:@[ @"SystemUiOverlay.top" ]];
524  [plugin handleMethodCall:setSystemOverlaysTopCall result:systemOverlaysTopResult];
525  [self waitForExpectationsWithTimeout:1 handler:nil];
526  OCMVerify([mockApplication setStatusBarHidden:NO]);
527 
528  [flutterViewController deregisterNotifications];
529  [mockApplication stopMocking];
530  [bundleMock stopMocking];
531 }
532 
533 - (void)testStatusBarHiddenNotUpdatedInAppExtension {
534  id bundleMock = OCMPartialMock([NSBundle mainBundle]);
535  OCMStub([bundleMock objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"])
536  .andReturn(@NO);
537  OCMStub([bundleMock objectForInfoDictionaryKey:@"NSExtension"]).andReturn(@{
538  @"NSExtensionPointIdentifier" : @"com.apple.share-services"
539  });
540  id mockApplication = OCMClassMock([UIApplication class]);
541  OCMStub([mockApplication sharedApplication]).andReturn(mockApplication);
542  OCMReject([mockApplication setStatusBarHidden:OCMOCK_ANY]);
543 
544  // Enabling system UI overlays to update status bar.
545  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
546  [engine runWithEntrypoint:nil];
547  FlutterViewController* flutterViewController =
548  [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
549 
550  // Update the visibility of the status bar to hidden does not occur with extensions.
551  FlutterPlatformPlugin* plugin = [engine platformPlugin];
552 
553  XCTestExpectation* systemOverlaysBottomExpectation =
554  [self expectationWithDescription:@"setEnabledSystemUIOverlays"];
555  FlutterResult systemOverlaysBottomResult = ^(id result) {
556  [systemOverlaysBottomExpectation fulfill];
557  };
558  FlutterMethodCall* setSystemOverlaysBottomCall =
559  [FlutterMethodCall methodCallWithMethodName:@"SystemChrome.setEnabledSystemUIOverlays"
560  arguments:@[ @"SystemUiOverlay.bottom" ]];
561  [plugin handleMethodCall:setSystemOverlaysBottomCall result:systemOverlaysBottomResult];
562  [self waitForExpectationsWithTimeout:1 handler:nil];
563  OCMReject([mockApplication setStatusBarHidden:YES]);
564 
565  // Update the visibility of the status bar to shown does not occur with extensions.
566  XCTestExpectation* systemOverlaysTopExpectation =
567  [self expectationWithDescription:@"setEnabledSystemUIOverlays"];
568  FlutterResult systemOverlaysTopResult = ^(id result) {
569  [systemOverlaysTopExpectation fulfill];
570  };
571  FlutterMethodCall* setSystemOverlaysTopCall =
572  [FlutterMethodCall methodCallWithMethodName:@"SystemChrome.setEnabledSystemUIOverlays"
573  arguments:@[ @"SystemUiOverlay.top" ]];
574  [plugin handleMethodCall:setSystemOverlaysTopCall result:systemOverlaysTopResult];
575  [self waitForExpectationsWithTimeout:1 handler:nil];
576  OCMReject([mockApplication setStatusBarHidden:NO]);
577 
578  [flutterViewController deregisterNotifications];
579  [mockApplication stopMocking];
580  [bundleMock stopMocking];
581 }
582 
583 - (void)testStatusBarStyle {
584  id bundleMock = OCMPartialMock([NSBundle mainBundle]);
585  OCMStub([bundleMock objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"])
586  .andReturn(@NO);
587  id mockApplication = OCMClassMock([UIApplication class]);
588  OCMStub([mockApplication sharedApplication]).andReturn(mockApplication);
589 
590  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
591  [engine runWithEntrypoint:nil];
592  FlutterViewController* flutterViewController =
593  [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
594  XCTAssertFalse(flutterViewController.prefersStatusBarHidden);
595 
596  FlutterPlatformPlugin* plugin = [engine platformPlugin];
597 
598  XCTestExpectation* enableSystemUIModeCalled =
599  [self expectationWithDescription:@"setSystemUIOverlayStyle"];
600  FlutterResult resultSet = ^(id result) {
601  [enableSystemUIModeCalled fulfill];
602  };
603  FlutterMethodCall* methodCallSet =
604  [FlutterMethodCall methodCallWithMethodName:@"SystemChrome.setSystemUIOverlayStyle"
605  arguments:@{@"statusBarBrightness" : @"Brightness.dark"}];
606  [plugin handleMethodCall:methodCallSet result:resultSet];
607  [self waitForExpectationsWithTimeout:1 handler:nil];
608 
609  OCMVerify([mockApplication setStatusBarStyle:UIStatusBarStyleLightContent]);
610 
611  [flutterViewController deregisterNotifications];
612  [mockApplication stopMocking];
613  [bundleMock stopMocking];
614 }
615 
616 - (void)testStatusBarStyleNotUpdatedInAppExtension {
617  id bundleMock = OCMPartialMock([NSBundle mainBundle]);
618  OCMStub([bundleMock objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"])
619  .andReturn(@NO);
620  OCMStub([bundleMock objectForInfoDictionaryKey:@"NSExtension"]).andReturn(@{
621  @"NSExtensionPointIdentifier" : @"com.apple.share-services"
622  });
623  id mockApplication = OCMClassMock([UIApplication class]);
624  OCMStub([mockApplication sharedApplication]).andReturn(mockApplication);
625  OCMReject([mockApplication setStatusBarHidden:OCMOCK_ANY]);
626 
627  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
628  [engine runWithEntrypoint:nil];
629  FlutterViewController* flutterViewController =
630  [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
631  XCTAssertFalse(flutterViewController.prefersStatusBarHidden);
632 
633  FlutterPlatformPlugin* plugin = [engine platformPlugin];
634 
635  XCTestExpectation* enableSystemUIModeCalled =
636  [self expectationWithDescription:@"setSystemUIOverlayStyle"];
637  FlutterResult resultSet = ^(id result) {
638  [enableSystemUIModeCalled fulfill];
639  };
640  FlutterMethodCall* methodCallSet =
641  [FlutterMethodCall methodCallWithMethodName:@"SystemChrome.setSystemUIOverlayStyle"
642  arguments:@{@"statusBarBrightness" : @"Brightness.dark"}];
643  [plugin handleMethodCall:methodCallSet result:resultSet];
644  [self waitForExpectationsWithTimeout:1 handler:nil];
645 
646  [flutterViewController deregisterNotifications];
647  [mockApplication stopMocking];
648  [bundleMock stopMocking];
649 }
650 
651 @end
void(^ FlutterResult)(id _Nullable result)
BOOL runWithEntrypoint:(nullable NSString *NS_SWIFT_UI_ACTOR)
instancetype methodCallWithMethodName:arguments:(NSString *method,[arguments] id _Nullable arguments)
void handleMethodCall:result:(FlutterMethodCall *call,[result] FlutterResult result)