Flutter iOS Embedder
FlutterEngineGroupTest.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 #include "flutter/fml/synchronization/sync_switch.h"
11 
13 
14 @interface FlutterEngineGroup ()
15 - (FlutterEngine*)makeEngine;
16 @end
17 
18 @interface FlutterEngineGroupTest : XCTestCase
19 @end
20 
21 @implementation FlutterEngineGroupTest
22 
23 - (void)testMake {
24  FlutterEngineGroup* group = [[FlutterEngineGroup alloc] initWithName:@"foo" project:nil];
26  XCTAssertNotNil(engine);
27 }
28 
29 - (void)testSpawn {
30  FlutterEngineGroup* group = [[FlutterEngineGroup alloc] initWithName:@"foo" project:nil];
31  FlutterEngine* spawner = [group makeEngineWithEntrypoint:nil libraryURI:nil];
32  spawner.isGpuDisabled = YES;
33  FlutterEngine* spawnee = [group makeEngineWithEntrypoint:nil libraryURI:nil];
34 
35  // Verify engines exist.
36  XCTAssertNotNil(spawner);
37  XCTAssertNotNil(spawnee);
38 
39  // Verify engine properties match.
40  XCTAssertEqual(&spawner.threadHost, &spawnee.threadHost);
41  XCTAssertEqual(spawner.isGpuDisabled, spawnee.isGpuDisabled);
42 
43  // Verify the shell came up with GPU disabled.
44  BOOL gpuDisabled = NO;
45  [spawnee shell].GetIsGpuDisabledSyncSwitch()->Execute(
46  fml::SyncSwitch::Handlers().SetIfTrue([&] { gpuDisabled = YES; }));
47  XCTAssertTrue(gpuDisabled);
48 }
49 
50 // Verifies that the Dart VM, isolate group snapshot, and task runners are shared between engines.
51 - (void)testSpawnSharesDartVMAndTaskRunners {
52  FlutterEngineGroup* group = [[FlutterEngineGroup alloc] initWithName:@"foo" project:nil];
53  FlutterEngine* spawner = [group makeEngineWithEntrypoint:nil libraryURI:nil];
54  FlutterEngine* spawnee = [group makeEngineWithEntrypoint:nil libraryURI:nil];
55  XCTAssertNotNil(spawner);
56  XCTAssertNotNil(spawnee);
57 
58  // A single Dart VM backs every engine in the group.
59  XCTAssertEqual([spawner shell].GetDartVM(), [spawnee shell].GetDartVM());
60 
61  // The engine-managed threads are shared; only the Dart isolate differs.
62  const flutter::TaskRunners& spawnerRunners = [spawner shell].GetTaskRunners();
63  const flutter::TaskRunners& spawneeRunners = [spawnee shell].GetTaskRunners();
64  XCTAssertEqual(spawnerRunners.GetRasterTaskRunner(), spawneeRunners.GetRasterTaskRunner());
65  XCTAssertEqual(spawnerRunners.GetIOTaskRunner(), spawneeRunners.GetIOTaskRunner());
66  XCTAssertEqual(spawnerRunners.GetPlatformTaskRunner(), spawneeRunners.GetPlatformTaskRunner());
67 
68  // Platform and UI are merged on iOS, so this holds for the spawned engine too.
69  XCTAssertEqual(spawneeRunners.GetUITaskRunner(), spawneeRunners.GetPlatformTaskRunner());
70 }
71 
72 - (void)testDeleteLastEngine {
73  __weak FlutterEngine* weakSpawner;
74  FlutterEngineGroup* group = [[FlutterEngineGroup alloc] initWithName:@"foo" project:nil];
75  @autoreleasepool {
76  FlutterEngine* spawner = [group makeEngineWithEntrypoint:nil libraryURI:nil];
77  weakSpawner = spawner;
78  XCTAssertNotNil(spawner);
79  XCTAssertNotNil(weakSpawner);
80  }
81  XCTAssertNil(weakSpawner);
82 
83  FlutterEngine* spawnee = [group makeEngineWithEntrypoint:nil libraryURI:nil];
84  XCTAssertNotNil(spawnee);
85 }
86 
87 - (void)testCustomEntrypoint {
88  FlutterEngineGroup* group = OCMPartialMock([[FlutterEngineGroup alloc] initWithName:@"foo"
89  project:nil]);
90  FlutterEngine* mockEngine = OCMClassMock([FlutterEngine class]);
91  OCMStub([group makeEngine]).andReturn(mockEngine);
92  OCMStub([mockEngine spawnWithEntrypoint:[OCMArg any]
93  libraryURI:[OCMArg any]
94  initialRoute:[OCMArg any]
95  entrypointArgs:[OCMArg any]])
96  .andReturn(OCMClassMock([FlutterEngine class]));
97  FlutterEngine* spawner = [group makeEngineWithEntrypoint:@"firstEntrypoint"
98  libraryURI:@"firstLibraryURI"];
99  XCTAssertNotNil(spawner);
100  OCMVerify([spawner runWithEntrypoint:@"firstEntrypoint"
101  libraryURI:@"firstLibraryURI"
102  initialRoute:nil
103  entrypointArgs:nil]);
104 
105  FlutterEngine* spawnee = [group makeEngineWithEntrypoint:@"secondEntrypoint"
106  libraryURI:@"secondLibraryURI"];
107  XCTAssertNotNil(spawnee);
108  OCMVerify([spawner spawnWithEntrypoint:@"secondEntrypoint"
109  libraryURI:@"secondLibraryURI"
110  initialRoute:nil
111  entrypointArgs:nil]);
112 }
113 
114 - (void)testCustomInitialRoute {
115  FlutterEngineGroup* group = OCMPartialMock([[FlutterEngineGroup alloc] initWithName:@"foo"
116  project:nil]);
117  FlutterEngine* mockEngine = OCMClassMock([FlutterEngine class]);
118  OCMStub([group makeEngine]).andReturn(mockEngine);
119  OCMStub([mockEngine spawnWithEntrypoint:[OCMArg any]
120  libraryURI:[OCMArg any]
121  initialRoute:[OCMArg any]
122  entrypointArgs:[OCMArg any]])
123  .andReturn(OCMClassMock([FlutterEngine class]));
124  FlutterEngine* spawner = [group makeEngineWithEntrypoint:nil libraryURI:nil initialRoute:@"foo"];
125  XCTAssertNotNil(spawner);
126  OCMVerify([spawner runWithEntrypoint:nil libraryURI:nil initialRoute:@"foo" entrypointArgs:nil]);
127 
128  FlutterEngine* spawnee = [group makeEngineWithEntrypoint:nil libraryURI:nil initialRoute:@"bar"];
129  XCTAssertNotNil(spawnee);
130  OCMVerify([spawner spawnWithEntrypoint:nil
131  libraryURI:nil
132  initialRoute:@"bar"
133  entrypointArgs:nil]);
134 }
135 
136 - (void)testCustomEntrypointArgs {
137  FlutterEngineGroup* group = OCMPartialMock([[FlutterEngineGroup alloc] initWithName:@"foo"
138  project:nil]);
139  FlutterEngine* mockEngine = OCMClassMock([FlutterEngine class]);
140  OCMStub([group makeEngine]).andReturn(mockEngine);
141  OCMStub([mockEngine spawnWithEntrypoint:[OCMArg any]
142  libraryURI:[OCMArg any]
143  initialRoute:[OCMArg any]
144  entrypointArgs:[OCMArg any]])
145  .andReturn(OCMClassMock([FlutterEngine class]));
146  FlutterEngineGroupOptions* firstOptions = [[FlutterEngineGroupOptions alloc] init];
147  NSArray* firstEntrypointArgs = @[ @"foo", @"first" ];
148  firstOptions.entrypointArgs = firstEntrypointArgs;
149  FlutterEngine* spawner = [group makeEngineWithOptions:firstOptions];
150  XCTAssertNotNil(spawner);
151  OCMVerify([spawner runWithEntrypoint:nil
152  libraryURI:nil
153  initialRoute:nil
154  entrypointArgs:firstEntrypointArgs]);
155 
156  NSArray* secondEntrypointArgs = @[ @"bar", @"second" ];
157  FlutterEngineGroupOptions* secondOptions = [[FlutterEngineGroupOptions alloc] init];
158  secondOptions.entrypointArgs = secondEntrypointArgs;
159  FlutterEngine* spawnee = [group makeEngineWithOptions:secondOptions];
160  XCTAssertNotNil(spawnee);
161  OCMVerify([spawner spawnWithEntrypoint:nil
162  libraryURI:nil
163  initialRoute:nil
164  entrypointArgs:secondEntrypointArgs]);
165 }
166 
167 - (void)testReleasesProjectOnDealloc {
168  __weak FlutterDartProject* weakProject;
169  @autoreleasepool {
170  FlutterDartProject* mockProject = OCMClassMock([FlutterDartProject class]);
171  FlutterEngineGroup* group = [[FlutterEngineGroup alloc] initWithName:@"foo"
172  project:mockProject];
173  XCTAssertNotNil(group);
174  weakProject = mockProject;
175  XCTAssertNotNil(weakProject);
176  group = nil;
177  mockProject = nil;
178  }
179  XCTAssertNil(weakProject);
180 }
181 
182 @end
FlutterEngine * makeEngineWithOptions:(nullable FlutterEngineGroupOptions *NS_SWIFT_UI_ACTOR)
FlutterEngine * makeEngineWithEntrypoint:libraryURI:(nullable NSString *entrypoint,[libraryURI] nullable NSString *NS_SWIFT_UI_ACTOR)
FlutterEngine * makeEngineWithEntrypoint:libraryURI:initialRoute:(nullable NSString *entrypoint,[libraryURI] nullable NSString *libraryURI,[initialRoute] nullable NSString *NS_SWIFT_UI_ACTOR)
NSArray< NSString * > * entrypointArgs
const flutter::ThreadHost & threadHost()
flutter::Shell & shell()