Flutter Engine
 
Loading...
Searching...
No Matches
dart_plugin_registrant_unittests.cc
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
6
7#include <cstdlib>
8#include "flutter/fml/paths.h"
15
16// CREATE_NATIVE_ENTRY is leaky by design
17// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
18
19namespace flutter {
20namespace testing {
21
22const std::string kKernelFileName = "plugin_registrant_kernel_blob.bin";
23const std::string kElfFileName = "plugin_registrant_app_elf_snapshot.so";
24
25class DartIsolateTest : public FixtureTest {
26 public:
28
29 void OverrideDartPluginRegistrant(const std::string& override_value) {
30 dart_plugin_registrant_library_ = override_value;
33 }
34
35 void SetUp() override {
36 std::string source_path = GetSourcePath();
37 if (source_path[0] != '/') {
38 // On windows we need an extra '/' prefix.
39 source_path = "/" + source_path;
40 }
41 std::string registrant_uri = std::string("file://") + source_path +
42 "flutter/runtime/fixtures/dart_tool/"
43 "flutter_build/dart_plugin_registrant.dart";
44 OverrideDartPluginRegistrant(registrant_uri);
45 }
46
47 void TearDown() override {
49 }
50
52};
53
54TEST_F(DartIsolateTest, DartPluginRegistrantIsPresent) {
55#if defined(OS_FUCHSIA) && (FLUTTER_RUNTIME_MODE != FLUTTER_RUNTIME_MODE_DEBUG)
56 GTEST_SKIP() << "Dart_LoadELF is not implemented on Fuchsia.";
57#else
58 ASSERT_FALSE(DartVMRef::IsInstanceRunning());
59
60 std::vector<std::string> messages;
62
63 AddNativeCallback(
64 "PassMessage",
65 CREATE_NATIVE_ENTRY(([&latch, &messages](Dart_NativeArguments args) {
67 Dart_GetNativeArgument(args, 0));
68 messages.push_back(message);
69 latch.Signal();
70 })));
71
72 auto settings = CreateSettingsForFixture();
73 auto did_throw_exception = false;
74 settings.unhandled_exception_callback = [&](const std::string& error,
75 const std::string& stack_trace) {
76 did_throw_exception = true;
77 return true;
78 };
79
80 auto vm_ref = DartVMRef::Create(settings);
81 auto thread = CreateNewThread();
82 TaskRunners task_runners(GetCurrentTestName(), //
83 thread, //
84 thread, //
85 thread, //
86 thread //
87 );
88
89 auto kernel_path =
91 auto isolate =
92 RunDartCodeInIsolate(vm_ref, settings, task_runners,
93 "mainForPluginRegistrantTest", {}, kernel_path);
94
95 ASSERT_TRUE(isolate);
96 ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running);
97
98 latch.Wait();
99
100 ASSERT_EQ(messages.size(), 1u);
101 ASSERT_EQ(messages[0], "_PluginRegistrant.register() was called");
102#endif
103}
104
105TEST_F(DartIsolateTest, DartPluginRegistrantFromBackgroundIsolate) {
106#if defined(OS_FUCHSIA) && (FLUTTER_RUNTIME_MODE != FLUTTER_RUNTIME_MODE_DEBUG)
107 GTEST_SKIP() << "Dart_LoadELF is not implemented on Fuchsia.";
108#else
109 ASSERT_FALSE(DartVMRef::IsInstanceRunning());
110
111 std::vector<std::string> messages;
113
114 AddNativeCallback(
115 "PassMessage",
116 CREATE_NATIVE_ENTRY(([&latch, &messages](Dart_NativeArguments args) {
118 Dart_GetNativeArgument(args, 0));
119 messages.push_back(message);
120 latch.Signal();
121 })));
122
123 auto settings = CreateSettingsForFixture();
124 auto did_throw_exception = false;
125 settings.unhandled_exception_callback = [&](const std::string& error,
126 const std::string& stack_trace) {
127 did_throw_exception = true;
128 return true;
129 };
130
131 auto vm_ref = DartVMRef::Create(settings);
132 auto thread = CreateNewThread();
133 TaskRunners task_runners(GetCurrentTestName(), //
134 thread, //
135 thread, //
136 thread, //
137 thread //
138 );
139
140 auto kernel_path =
142 auto isolate = RunDartCodeInIsolate(
143 vm_ref, settings, task_runners,
144 "callDartPluginRegistrantFromBackgroundIsolate", {}, kernel_path);
145
146 ASSERT_TRUE(isolate);
147 ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running);
148
149 latch.Wait();
150
151 ASSERT_EQ(messages.size(), 1u);
152 ASSERT_EQ(messages[0],
153 "_PluginRegistrant.register() was called on background isolate");
154#endif
155}
156
157TEST_F(DartIsolateTest, DartPluginRegistrantNotFromBackgroundIsolate) {
158#if defined(OS_FUCHSIA) && (FLUTTER_RUNTIME_MODE != FLUTTER_RUNTIME_MODE_DEBUG)
159 GTEST_SKIP() << "Dart_LoadELF is not implemented on Fuchsia.";
160#else
161 ASSERT_FALSE(DartVMRef::IsInstanceRunning());
162
163 std::vector<std::string> messages;
165
166 AddNativeCallback(
167 "PassMessage",
168 CREATE_NATIVE_ENTRY(([&latch, &messages](Dart_NativeArguments args) {
170 Dart_GetNativeArgument(args, 0));
171 messages.push_back(message);
172 latch.Signal();
173 })));
174
175 auto settings = CreateSettingsForFixture();
176 auto did_throw_exception = false;
177 settings.unhandled_exception_callback = [&](const std::string& error,
178 const std::string& stack_trace) {
179 did_throw_exception = true;
180 return true;
181 };
182
183 auto vm_ref = DartVMRef::Create(settings);
184 auto thread = CreateNewThread();
185 TaskRunners task_runners(GetCurrentTestName(), //
186 thread, //
187 thread, //
188 thread, //
189 thread //
190 );
191
192 auto kernel_path =
194 auto isolate = RunDartCodeInIsolate(
195 vm_ref, settings, task_runners,
196 "dontCallDartPluginRegistrantFromBackgroundIsolate", {}, kernel_path);
197
198 ASSERT_TRUE(isolate);
199 ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running);
200
201 latch.Wait();
202
203 ASSERT_EQ(messages.size(), 1u);
204 ASSERT_EQ(
205 messages[0],
206 "_PluginRegistrant.register() was not called on background isolate");
207#endif
208}
209
210TEST_F(DartIsolateTest, DartPluginRegistrantWhenRegisteringBackgroundIsolate) {
211#if defined(OS_FUCHSIA) && (FLUTTER_RUNTIME_MODE != FLUTTER_RUNTIME_MODE_DEBUG)
212 GTEST_SKIP() << "Dart_LoadELF is not implemented on Fuchsia.";
213#else
214 ASSERT_FALSE(DartVMRef::IsInstanceRunning());
215
216 std::vector<std::string> messages;
218
219 AddNativeCallback(
220 "PassMessage",
221 CREATE_NATIVE_ENTRY(([&latch, &messages](Dart_NativeArguments args) {
223 Dart_GetNativeArgument(args, 0));
224 messages.push_back(message);
225 latch.Signal();
226 })));
227
228 auto settings = CreateSettingsForFixture();
229 auto did_throw_exception = false;
230 settings.unhandled_exception_callback = [&](const std::string& error,
231 const std::string& stack_trace) {
232 did_throw_exception = true;
233 return true;
234 };
235
236 auto vm_ref = DartVMRef::Create(settings);
237 auto thread = CreateNewThread();
238 TaskRunners task_runners(GetCurrentTestName(), //
239 thread, //
240 thread, //
241 thread, //
242 thread //
243 );
244
245 auto kernel_path =
247 auto isolate = RunDartCodeInIsolate(
248 vm_ref, settings, task_runners,
249 "registerBackgroundIsolateCallsDartPluginRegistrant", {}, kernel_path);
250
251 ASSERT_TRUE(isolate);
252 ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running);
253
254 latch.Wait();
255
256 ASSERT_EQ(messages.size(), 1u);
257 ASSERT_EQ(messages[0],
258 "_PluginRegistrant.register() was called on background isolate");
259#endif
260}
261
262} // namespace testing
263} // namespace flutter
264
265// NOLINTEND(clang-analyzer-core.StackAddressEscape)
static DartVMRef Create(const Settings &settings, fml::RefPtr< const DartSnapshot > vm_snapshot=nullptr, fml::RefPtr< const DartSnapshot > isolate_snapshot=nullptr)
static bool IsInstanceRunning()
void OverrideDartPluginRegistrant(const std::string &override_value)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
G_BEGIN_DECLS GBytes * message
const uint8_t uint32_t uint32_t GError ** error
std::string GetCurrentTestName()
Gets the name of the currently running test. This is useful in generating logs or assets based on tes...
Definition testing.cc:14
TEST_F(DisplayListTest, Defaults)
std::unique_ptr< AutoIsolateShutdown > RunDartCodeInIsolate(DartVMRef &vm_ref, const Settings &settings, const TaskRunners &task_runners, std::string entrypoint, const std::vector< std::string > &args, const std::string &kernel_file_path, fml::WeakPtr< IOManager > io_manager, std::unique_ptr< PlatformConfiguration > platform_configuration)
const char * GetFixturesPath()
Returns the directory containing the test fixture for the target if this target has fixtures configur...
const char * GetSourcePath()
const char * dart_plugin_registrant_library_override
std::string JoinPaths(std::initializer_list< std::string > components)
Definition paths.cc:14
#define CREATE_NATIVE_ENTRY(native_entry)