Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
FlutterViewControllerTest.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
12@interface FlutterViewControllerTest : XCTestCase
13@property(nonatomic, strong) FlutterViewController* flutterViewController;
14@end
15
16@implementation FlutterViewControllerTest
17
18- (void)setUp {
19 [super setUp];
20 self.continueAfterFailure = NO;
21}
22
23- (void)tearDown {
24 if (self.flutterViewController) {
25 XCTestExpectation* vcDismissed = [self expectationWithDescription:@"dismiss"];
26 [self.flutterViewController dismissViewControllerAnimated:NO
27 completion:^{
28 [vcDismissed fulfill];
29 }];
30 [self waitForExpectationsWithTimeout:10.0 handler:nil];
31 }
32 [super tearDown];
33}
34
36 XCTestExpectation* firstFrameRendered = [self expectationWithDescription:@"firstFrameRendered"];
37
38 FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
39 [engine runWithEntrypoint:nil];
40 self.flutterViewController = [[FlutterViewController alloc] initWithEngine:engine
41 nibName:nil
42 bundle:nil];
43
44 XCTAssertFalse(self.flutterViewController.isDisplayingFlutterUI);
45
46 XCTestExpectation* displayingFlutterUIExpectation =
47 [self keyValueObservingExpectationForObject:self.flutterViewController
48 keyPath:@"displayingFlutterUI"
49 expectedValue:@YES];
50 displayingFlutterUIExpectation.assertForOverFulfill = YES;
51
52 [self.flutterViewController setFlutterViewDidRenderCallback:^{
53 [firstFrameRendered fulfill];
54 }];
55
56 AppDelegate* appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
57 UIViewController* rootVC = appDelegate.window.rootViewController;
58 [rootVC presentViewController:self.flutterViewController animated:NO completion:nil];
59
60 [self waitForExpectationsWithTimeout:30.0 handler:nil];
61}
62
63- (void)testDrawLayer {
64 XCTestExpectation* firstFrameRendered = [self expectationWithDescription:@"firstFrameRendered"];
65 XCTestExpectation* imageRendered = [self expectationWithDescription:@"imageRendered"];
66
67 FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
68 [engine runWithEntrypoint:nil];
69 [engine.binaryMessenger
70 setMessageHandlerOnChannel:@"waiting_for_status"
71 binaryMessageHandler:^(NSData* _Nullable message, FlutterBinaryReply _Nonnull reply) {
73 methodChannelWithName:@"driver"
74 binaryMessenger:engine.binaryMessenger
76 [channel invokeMethod:@"set_scenario" arguments:@{@"name" : @"solid_blue"}];
77 }];
78
79 self.flutterViewController = [[FlutterViewController alloc] initWithEngine:engine
80 nibName:nil
81 bundle:nil];
82
83 XCTAssertFalse(self.flutterViewController.isDisplayingFlutterUI);
84
85 [self.flutterViewController setFlutterViewDidRenderCallback:^{
86 [firstFrameRendered fulfill];
87 }];
88
89 AppDelegate* appDelegate = (AppDelegate*)UIApplication.sharedApplication.delegate;
90 UIViewController* rootVC = appDelegate.window.rootViewController;
91 [rootVC presentViewController:self.flutterViewController animated:NO completion:nil];
92
93 CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
94
95 __block dispatch_block_t callback;
96 callback = ^{
97 size_t width = 300u;
98 CGContextRef context =
99 CGBitmapContextCreate(nil, width, width, 8, 4 * width, color_space,
100 kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
101 [appDelegate.window.layer renderInContext:context];
102 uint32_t* image_data = (uint32_t*)CGBitmapContextGetData(context);
103 if (image_data[20] == 0xFF0000FF) {
104 [imageRendered fulfill];
105 return;
106 }
107
108 CGContextRelease(context);
109
110 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(),
111 callback);
112 };
113 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(),
114 callback);
115
116 [self waitForExpectationsWithTimeout:30.0 handler:nil];
117
118 CGColorSpaceRelease(color_space);
119}
120
121@end
FlutterEngine engine
Definition main.cc:68
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
UIWindow * window
Definition AppDelegate.h:12
BOOL runWithEntrypoint:(nullable NSString *entrypoint)
FlutterBinaryMessengerConnection setMessageHandlerOnChannel:binaryMessageHandler:(NSString *channel, [binaryMessageHandler] FlutterBinaryMessageHandler handler)
instancetype sharedInstance()
instancetype methodChannelWithName:binaryMessenger:codec:(NSString *name,[binaryMessenger] NSObject< FlutterBinaryMessenger > *messenger,[codec] NSObject< FlutterMethodCodec > *codec)
FlutterViewController * flutterViewController
int32_t width
#define NSEC_PER_SEC
Definition timerfd.cc:35