| Flutter macOS Embedder |

FlutterEngineTestUtils.mm
Go to the documentation of this file.
1
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 */
17 @interface FakePasteboard : FlutterPasteboard
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
41 namespace flutter::testing {
42
43 FlutterEngineTest::FlutterEngineTest() = default;
44
45 void 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
57 void FlutterEngineTest::TearDown() {
58 [engine_ shutDownEngine];
59 engine_ = nil;
60 native_resolver_.reset();
61 }
62
63 void FlutterEngineTest::ShutDownEngine() {
64 [engine_ shutDownEngine];
65 engine_ = nil;
66 }
67
68 void FlutterEngineTest::IsolateCreateCallback(void* user_data) {
69 native_resolver_->SetNativeResolverForIsolate();
70 }
71
72 void FlutterEngineTest::AddNativeCallback(const char* name, Dart_NativeFunction function) {
73 native_resolver_->AddNativeCallback({name}, function);
74 }
75
76 id 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
92 MockFlutterEngineTest::MockFlutterEngineTest() = default;
93
94 void MockFlutterEngineTest::SetUp() {
95 engine_mock_ = CreateMockFlutterEngine(@"");
96 }
97
98 void MockFlutterEngineTest::TearDown() {
99 [engine_mock_ shutDownEngine];
100 [engine_mock_ stopMocking];
101 engine_mock_ = nil;
102 }
103
104 void MockFlutterEngineTest::ShutDownEngine() {
105 [engine_mock_ shutDownEngine];
106 engine_mock_ = nil;
107 }
108
109 } // namespace flutter::testing
FlutterDartProject_Internal.h
FlutterEngine_Internal.h
FlutterEngineTestUtils.h
FlutterDartProject
Definition: FlutterDartProject.mm:24
FlutterPasteboard
Definition: FlutterEngine.mm:292
-[FlutterPasteboard clearContents]
NSInteger clearContents()
Definition: FlutterEngine.mm:294
-[FlutterPasteboard setString:forType:]
BOOL setString:forType:(NSString *string,[forType] NSPasteboardType dataType)
FakePasteboard
Definition: FlutterEngineTestUtils.mm:18
FlutterEngine
Definition: FlutterEngine.h:32
-[FlutterEngine shutDownEngine]
void shutDownEngine()
Definition: FlutterEngine.mm:1320
flutter::testing
Definition: AccessibilityBridgeMacTest.mm:13
flutter::testing::CreateMockFlutterEngine
id CreateMockFlutterEngine(NSString *pasteboardString)
Definition: FlutterEngineTestUtils.mm:76
user_data
void * user_data
Definition: texture_registrar_unittests.cc:27