1
1
// Copyright 2013 The Flutter Authors. All rights reserved.
2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
- // FLUTTER_NOLINT
5
4
6
5
#include " flutter/fml/mapping.h"
7
6
#include " flutter/fml/synchronization/count_down_latch.h"
19
18
namespace flutter {
20
19
namespace testing {
21
20
22
- using DartIsolateTest = FixtureTest;
21
+ class DartIsolateTest : public FixtureTest {
22
+ public:
23
+ DartIsolateTest () {}
24
+
25
+ void Wait () { latch_.Wait (); }
26
+
27
+ void Signal () { latch_.Signal (); }
28
+
29
+ private:
30
+ fml::AutoResetWaitableEvent latch_;
31
+
32
+ FML_DISALLOW_COPY_AND_ASSIGN (DartIsolateTest);
33
+ };
23
34
24
35
TEST_F (DartIsolateTest, RootIsolateCreationAndShutdown) {
25
36
ASSERT_FALSE (DartVMRef::IsInstanceRunning ());
@@ -153,11 +164,10 @@ TEST_F(DartIsolateTest, CanRunDartCodeCodeSynchronously) {
153
164
154
165
TEST_F (DartIsolateTest, CanRegisterNativeCallback) {
155
166
ASSERT_FALSE (DartVMRef::IsInstanceRunning ());
156
- fml::AutoResetWaitableEvent latch;
157
167
AddNativeCallback (" NotifyNative" ,
158
- CREATE_NATIVE_ENTRY (([&latch ](Dart_NativeArguments args) {
168
+ CREATE_NATIVE_ENTRY (([this ](Dart_NativeArguments args) {
159
169
FML_LOG (ERROR) << " Hello from Dart!" ;
160
- latch. Signal ();
170
+ Signal ();
161
171
})));
162
172
const auto settings = CreateSettingsForFixture ();
163
173
auto vm_ref = DartVMRef::Create (settings);
@@ -173,7 +183,7 @@ TEST_F(DartIsolateTest, CanRegisterNativeCallback) {
173
183
" canRegisterNativeCallback" , {}, GetFixturesPath ());
174
184
ASSERT_TRUE (isolate);
175
185
ASSERT_EQ (isolate->get ()->GetPhase (), DartIsolate::Phase::Running);
176
- latch. Wait ();
186
+ Wait ();
177
187
}
178
188
179
189
TEST_F (DartIsolateTest, CanSaveCompilationTrace) {
@@ -182,12 +192,11 @@ TEST_F(DartIsolateTest, CanSaveCompilationTrace) {
182
192
GTEST_SKIP ();
183
193
return ;
184
194
}
185
- fml::AutoResetWaitableEvent latch;
186
195
AddNativeCallback (" NotifyNative" ,
187
- CREATE_NATIVE_ENTRY (([&latch ](Dart_NativeArguments args) {
196
+ CREATE_NATIVE_ENTRY (([this ](Dart_NativeArguments args) {
188
197
ASSERT_TRUE (tonic::DartConverter<bool >::FromDart (
189
198
Dart_GetNativeArgument (args, 0 )));
190
- latch. Signal ();
199
+ Signal ();
191
200
})));
192
201
193
202
const auto settings = CreateSettingsForFixture ();
@@ -205,31 +214,52 @@ TEST_F(DartIsolateTest, CanSaveCompilationTrace) {
205
214
ASSERT_TRUE (isolate);
206
215
ASSERT_EQ (isolate->get ()->GetPhase (), DartIsolate::Phase::Running);
207
216
208
- latch. Wait ();
217
+ Wait ();
209
218
}
210
219
211
- TEST_F (DartIsolateTest, CanLaunchSecondaryIsolates) {
212
- fml::CountDownLatch latch (3 );
213
- fml::AutoResetWaitableEvent child_shutdown_latch;
214
- fml::AutoResetWaitableEvent root_isolate_shutdown_latch;
220
+ class DartSecondaryIsolateTest : public FixtureTest {
221
+ public:
222
+ DartSecondaryIsolateTest () : latch_(3 ) {}
223
+
224
+ void LatchCountDown () { latch_.CountDown (); }
225
+
226
+ void LatchWait () { latch_.Wait (); }
227
+
228
+ void ChildShutdownSignal () { child_shutdown_latch_.Signal (); }
229
+
230
+ void ChildShutdownWait () { child_shutdown_latch_.Wait (); }
231
+
232
+ void RootIsolateShutdownSignal () { root_isolate_shutdown_latch_.Signal (); }
233
+
234
+ bool RootIsolateIsSignaled () {
235
+ return root_isolate_shutdown_latch_.IsSignaledForTest ();
236
+ }
237
+
238
+ private:
239
+ fml::CountDownLatch latch_;
240
+ fml::AutoResetWaitableEvent child_shutdown_latch_;
241
+ fml::AutoResetWaitableEvent root_isolate_shutdown_latch_;
242
+
243
+ FML_DISALLOW_COPY_AND_ASSIGN (DartSecondaryIsolateTest);
244
+ };
245
+
246
+ TEST_F (DartSecondaryIsolateTest, CanLaunchSecondaryIsolates) {
215
247
AddNativeCallback (" NotifyNative" ,
216
- CREATE_NATIVE_ENTRY (([&latch ](Dart_NativeArguments args) {
217
- latch. CountDown ();
248
+ CREATE_NATIVE_ENTRY (([this ](Dart_NativeArguments args) {
249
+ LatchCountDown ();
218
250
})));
219
251
AddNativeCallback (
220
- " PassMessage" , CREATE_NATIVE_ENTRY (([&latch ](Dart_NativeArguments args) {
252
+ " PassMessage" , CREATE_NATIVE_ENTRY (([this ](Dart_NativeArguments args) {
221
253
auto message = tonic::DartConverter<std::string>::FromDart (
222
254
Dart_GetNativeArgument (args, 0 ));
223
255
ASSERT_EQ (" Hello from code is secondary isolate." , message);
224
- latch. CountDown ();
256
+ LatchCountDown ();
225
257
})));
226
258
auto settings = CreateSettingsForFixture ();
227
- settings.root_isolate_shutdown_callback = [&root_isolate_shutdown_latch]() {
228
- root_isolate_shutdown_latch.Signal ();
229
- };
230
- settings.isolate_shutdown_callback = [&child_shutdown_latch]() {
231
- child_shutdown_latch.Signal ();
259
+ settings.root_isolate_shutdown_callback = [this ]() {
260
+ RootIsolateShutdownSignal ();
232
261
};
262
+ settings.isolate_shutdown_callback = [this ]() { ChildShutdownSignal (); };
233
263
auto vm_ref = DartVMRef::Create (settings);
234
264
auto thread = CreateNewThread ();
235
265
TaskRunners task_runners (GetCurrentTestName (), //
@@ -243,19 +273,18 @@ TEST_F(DartIsolateTest, CanLaunchSecondaryIsolates) {
243
273
GetFixturesPath ());
244
274
ASSERT_TRUE (isolate);
245
275
ASSERT_EQ (isolate->get ()->GetPhase (), DartIsolate::Phase::Running);
246
- child_shutdown_latch. Wait (); // wait for child isolate to shutdown first
247
- ASSERT_FALSE (root_isolate_shutdown_latch. IsSignaledForTest ());
248
- latch. Wait (); // wait for last NotifyNative called by main isolate
276
+ ChildShutdownWait (); // wait for child isolate to shutdown first
277
+ ASSERT_FALSE (RootIsolateIsSignaled ());
278
+ LatchWait (); // wait for last NotifyNative called by main isolate
249
279
// root isolate will be auto-shutdown
250
280
}
251
281
252
282
TEST_F (DartIsolateTest, CanRecieveArguments) {
253
- fml::AutoResetWaitableEvent latch;
254
283
AddNativeCallback (" NotifyNative" ,
255
- CREATE_NATIVE_ENTRY (([&latch ](Dart_NativeArguments args) {
284
+ CREATE_NATIVE_ENTRY (([this ](Dart_NativeArguments args) {
256
285
ASSERT_TRUE (tonic::DartConverter<bool >::FromDart (
257
286
Dart_GetNativeArgument (args, 0 )));
258
- latch. Signal ();
287
+ Signal ();
259
288
})));
260
289
261
290
const auto settings = CreateSettingsForFixture ();
@@ -273,7 +302,7 @@ TEST_F(DartIsolateTest, CanRecieveArguments) {
273
302
ASSERT_TRUE (isolate);
274
303
ASSERT_EQ (isolate->get ()->GetPhase (), DartIsolate::Phase::Running);
275
304
276
- latch. Wait ();
305
+ Wait ();
277
306
}
278
307
279
308
} // namespace testing
0 commit comments