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
35- (void)testFirstFrameCallback {
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
75 codec:[FlutterJSONMethodCodec sharedInstance]];
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 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(), ^{
95 size_t width = 300u;
96 CGContextRef context =
97 CGBitmapContextCreate(nil, width, width, 8, 4 * width, color_space,
98 kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
99 [appDelegate.window.layer renderInContext:context];
100 uint32_t* image_data = (uint32_t*)CGBitmapContextGetData(context);
101 if (image_data[20] == 0xFF0000FF) {
102 [imageRendered fulfill];
103 return;
104 }
105 CGContextRelease(context);
106 });
107
108 [self waitForExpectationsWithTimeout:30.0 handler:nil];
109
110 CGColorSpaceRelease(color_space);
111}
112
113@end
FlutterEngine engine
Definition main.cc:84
UIWindow * window
Definition AppDelegate.h:15
BOOL runWithEntrypoint:(nullable NSString *entrypoint)
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