Flutter Engine
The Flutter Engine
FlutterViewControllerInitialRouteTest.m
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 <Flutter/Flutter.h>
6#import <XCTest/XCTest.h>
7
8#import "AppDelegate.h"
9
11
13@property(nonatomic, strong) FlutterViewController* flutterViewController;
14@end
15
16// This test needs to be in its own file with only one test method because dart:ui
17// window's defaultRouteName can only be set once per VM.
19
20- (void)setUp {
21 [super setUp];
22 self.continueAfterFailure = NO;
23}
24
25- (void)tearDown {
26 if (self.flutterViewController) {
27 XCTestExpectation* vcDismissed = [self expectationWithDescription:@"dismiss"];
28 [self.flutterViewController dismissViewControllerAnimated:NO
29 completion:^{
30 [vcDismissed fulfill];
31 }];
32 [self waitForExpectationsWithTimeout:10.0 handler:nil];
33 }
34 [super tearDown];
35}
36
38 self.flutterViewController =
39 [[FlutterViewController alloc] initWithProject:nil
40 initialRoute:@"myCustomInitialRoute"
41 nibName:nil
42 bundle:nil];
43
44 NSObject<FlutterBinaryMessenger>* binaryMessenger = self.flutterViewController.binaryMessenger;
45 __weak typeof(binaryMessenger) weakBinaryMessenger = binaryMessenger;
46
47 FlutterBinaryMessengerConnection waitingForStatusConnection = [binaryMessenger
48 setMessageHandlerOnChannel:@"waiting_for_status"
49 binaryMessageHandler:^(NSData* message, FlutterBinaryReply reply) {
51 methodChannelWithName:@"driver"
52 binaryMessenger:weakBinaryMessenger
54 [channel invokeMethod:@"set_scenario" arguments:@{@"name" : @"initial_route_reply"}];
55 }];
56
57 XCTestExpectation* customInitialRouteSet =
58 [self expectationWithDescription:@"Custom initial route was set on the Dart side"];
59 FlutterBinaryMessengerConnection initialRoutTestChannelConnection =
60 [binaryMessenger setMessageHandlerOnChannel:@"initial_route_test_channel"
61 binaryMessageHandler:^(NSData* message, FlutterBinaryReply reply) {
62 NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:message
63 options:0
64 error:nil];
65 NSString* initialRoute = dict[@"method"];
66 if ([initialRoute isEqualToString:@"myCustomInitialRoute"]) {
67 [customInitialRouteSet fulfill];
68 } else {
69 XCTFail(@"Expected initial route to be set to "
70 @"myCustomInitialRoute. Was set to %@ instead",
71 initialRoute);
72 }
73 }];
74
75 AppDelegate* appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
76 UIViewController* rootVC = appDelegate.window.rootViewController;
77 [rootVC presentViewController:self.flutterViewController animated:NO completion:nil];
78
79 [self waitForExpectationsWithTimeout:30.0 handler:nil];
80
81 [binaryMessenger cleanUpConnection:waitingForStatusConnection];
82 [binaryMessenger cleanUpConnection:initialRoutTestChannelConnection];
83}
84
85@end
int64_t FlutterBinaryMessengerConnection
UIWindow * window
Definition: AppDelegate.h:12
instancetype sharedInstance()
instancetype methodChannelWithName:binaryMessenger:codec:(NSString *name,[binaryMessenger] NSObject< FlutterBinaryMessenger > *messenger,[codec] NSObject< FlutterMethodCodec > *codec)