Flutter Engine
The Flutter Engine
FlutterEngineTestUtils.mm
Go to the documentation of this file.
2// Copyright 2013 The Flutter Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5
6#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTestUtils.h"
7
8#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
9#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
10
11#include "flutter/testing/testing.h"
12
13/**
14 * Fake pasteboard implementation to allow tests to work in environments without a real
15 * pasteboard.
16 */
18@end
19
20@implementation FakePasteboard {
21 NSString* _result;
22}
23
24- (NSInteger)clearContents {
25 size_t changeCount = (_result != nil) ? 1 : 0;
26 _result = nil;
27 return changeCount;
28}
29
30- (NSString*)stringForType:(NSPasteboardType)dataType {
31 return _result;
32}
33
34- (BOOL)setString:(NSString*)string forType:(NSPasteboardType)dataType {
35 _result = string;
36 return YES;
37}
38
39@end
40
41namespace flutter::testing {
42
43FlutterEngineTest::FlutterEngineTest() = default;
44
45void FlutterEngineTest::SetUp() {
46 native_resolver_ = std::make_shared<TestDartNativeResolver>();
47 NSString* fixtures = @(testing::GetFixturesPath());
48 project_ = [[FlutterDartProject alloc]
49 initWithAssetsPath:fixtures
50 ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
51 project_.rootIsolateCreateCallback = FlutterEngineTest::IsolateCreateCallback;
52 engine_ = [[FlutterEngine alloc] initWithName:@"test"
53 project:project_
54 allowHeadlessExecution:true];
55}
56
57void FlutterEngineTest::TearDown() {
58 [engine_ shutDownEngine];
59 engine_ = nil;
60 native_resolver_.reset();
61}
62
63void FlutterEngineTest::ShutDownEngine() {
64 [engine_ shutDownEngine];
65 engine_ = nil;
66}
67
68void FlutterEngineTest::IsolateCreateCallback(void* user_data) {
69 native_resolver_->SetNativeResolverForIsolate();
70}
71
72void FlutterEngineTest::AddNativeCallback(const char* name, Dart_NativeFunction function) {
73 native_resolver_->AddNativeCallback({name}, function);
74}
75
76id CreateMockFlutterEngine(NSString* pasteboardString) {
77 NSString* fixtures = @(testing::GetFixturesPath());
78 FlutterDartProject* project = [[FlutterDartProject alloc]
79 initWithAssetsPath:fixtures
80 ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
81 FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test"
82 project:project
83 allowHeadlessExecution:true];
84
85 FakePasteboard* pasteboardMock = [[FakePasteboard alloc] init];
86 [pasteboardMock setString:pasteboardString forType:NSPasteboardTypeString];
87 engine.pasteboard = pasteboardMock;
88 id engineMock = OCMPartialMock(engine);
89 return engineMock;
90}
91
92MockFlutterEngineTest::MockFlutterEngineTest() = default;
93
94void MockFlutterEngineTest::SetUp() {
95 engine_mock_ = CreateMockFlutterEngine(@"");
96}
97
98void MockFlutterEngineTest::TearDown() {
99 [engine_mock_ shutDownEngine];
100 [engine_mock_ stopMocking];
101 engine_mock_ = nil;
102}
103
104void MockFlutterEngineTest::ShutDownEngine() {
105 [engine_mock_ shutDownEngine];
106 engine_mock_ = nil;
107}
108
109} // namespace flutter::testing
void(* Dart_NativeFunction)(Dart_NativeArguments arguments)
Definition: dart_api.h:3207
FlutterEngine engine
Definition: main.cc:68
Dart_NativeFunction function
Definition: fuchsia.cc:51
BOOL setString:forType:(NSString *string, [forType] NSPasteboardType dataType)
FlutterPasteboard * pasteboard
const char * GetFixturesPath()
Returns the directory containing the test fixture for the target if this target has fixtures configur...
id CreateMockFlutterEngine(NSString *pasteboardString)
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
void * user_data
int BOOL
Definition: windows_types.h:37