@@ -131,23 +131,20 @@ TEST_F(WindowsTest, LaunchHeadlessEngine) {
131131 ASSERT_NE (engine, nullptr );
132132
133133 std::string view_ids;
134- bool signaled = false ;
134+ fml::AutoResetWaitableEvent latch ;
135135 context.AddNativeFunction (
136136 " SignalStringValue" , CREATE_NATIVE_ENTRY ([&](Dart_NativeArguments args) {
137137 auto handle = Dart_GetNativeArgument (args, 0 );
138138 ASSERT_FALSE (Dart_IsError (handle));
139139 view_ids = tonic::DartConverter<std::string>::FromDart (handle);
140- signaled = true ;
140+ latch. Signal () ;
141141 }));
142142
143143 ViewControllerPtr controller{builder.Run ()};
144144 ASSERT_NE (controller, nullptr );
145145
146- while (!signaled) {
147- PumpMessage ();
148- }
149-
150146 // Verify a headless app has the implicit view.
147+ latch.Wait ();
151148 EXPECT_EQ (view_ids, " View IDs: [0]" );
152149}
153150
@@ -217,18 +214,16 @@ TEST_F(WindowsTest, VerifyNativeFunction) {
217214 WindowsConfigBuilder builder (context);
218215 builder.SetDartEntrypoint (" verifyNativeFunction" );
219216
220- bool signaled = false ;
217+ fml::AutoResetWaitableEvent latch ;
221218 auto native_entry =
222- CREATE_NATIVE_ENTRY ([&](Dart_NativeArguments args) { signaled = true ; });
219+ CREATE_NATIVE_ENTRY ([&](Dart_NativeArguments args) { latch. Signal () ; });
223220 context.AddNativeFunction (" Signal" , native_entry);
224221
225222 ViewControllerPtr controller{builder.Run ()};
226223 ASSERT_NE (controller, nullptr );
227224
228225 // Wait until signal has been called.
229- while (!signaled) {
230- PumpMessage ();
231- }
226+ latch.Wait ();
232227}
233228
234229// Verify that native functions that pass parameters can be registered and
@@ -239,21 +234,19 @@ TEST_F(WindowsTest, VerifyNativeFunctionWithParameters) {
239234 builder.SetDartEntrypoint (" verifyNativeFunctionWithParameters" );
240235
241236 bool bool_value = false ;
242- bool signaled = false ;
237+ fml::AutoResetWaitableEvent latch ;
243238 auto native_entry = CREATE_NATIVE_ENTRY ([&](Dart_NativeArguments args) {
244239 auto handle = Dart_GetNativeBooleanArgument (args, 0 , &bool_value);
245240 ASSERT_FALSE (Dart_IsError (handle));
246- signaled = true ;
241+ latch. Signal () ;
247242 });
248243 context.AddNativeFunction (" SignalBoolValue" , native_entry);
249244
250245 ViewControllerPtr controller{builder.Run ()};
251246 ASSERT_NE (controller, nullptr );
252247
253248 // Wait until signalBoolValue has been called.
254- while (!signaled) {
255- PumpMessage ();
256- }
249+ latch.Wait ();
257250 EXPECT_TRUE (bool_value);
258251}
259252
@@ -264,22 +257,20 @@ TEST_F(WindowsTest, PlatformExecutable) {
264257 builder.SetDartEntrypoint (" readPlatformExecutable" );
265258
266259 std::string executable_name;
267- bool signaled = false ;
260+ fml::AutoResetWaitableEvent latch ;
268261 auto native_entry = CREATE_NATIVE_ENTRY ([&](Dart_NativeArguments args) {
269262 auto handle = Dart_GetNativeArgument (args, 0 );
270263 ASSERT_FALSE (Dart_IsError (handle));
271264 executable_name = tonic::DartConverter<std::string>::FromDart (handle);
272- signaled = true ;
265+ latch. Signal () ;
273266 });
274267 context.AddNativeFunction (" SignalStringValue" , native_entry);
275268
276269 ViewControllerPtr controller{builder.Run ()};
277270 ASSERT_NE (controller, nullptr );
278271
279272 // Wait until signalStringValue has been called.
280- while (!signaled) {
281- PumpMessage ();
282- }
273+ latch.Wait ();
283274 EXPECT_EQ (executable_name, " flutter_windows_unittests.exe" );
284275}
285276
@@ -291,28 +282,26 @@ TEST_F(WindowsTest, VerifyNativeFunctionWithReturn) {
291282 builder.SetDartEntrypoint (" verifyNativeFunctionWithReturn" );
292283
293284 bool bool_value_to_return = true ;
294- int count = 2 ;
285+ fml::CountDownLatch latch ( 2 ) ;
295286 auto bool_return_entry = CREATE_NATIVE_ENTRY ([&](Dart_NativeArguments args) {
296287 Dart_SetBooleanReturnValue (args, bool_value_to_return);
297- --count ;
288+ latch. CountDown () ;
298289 });
299290 context.AddNativeFunction (" SignalBoolReturn" , bool_return_entry);
300291
301292 bool bool_value_passed = false ;
302293 auto bool_pass_entry = CREATE_NATIVE_ENTRY ([&](Dart_NativeArguments args) {
303294 auto handle = Dart_GetNativeBooleanArgument (args, 0 , &bool_value_passed);
304295 ASSERT_FALSE (Dart_IsError (handle));
305- --count ;
296+ latch. CountDown () ;
306297 });
307298 context.AddNativeFunction (" SignalBoolValue" , bool_pass_entry);
308299
309300 ViewControllerPtr controller{builder.Run ()};
310301 ASSERT_NE (controller, nullptr );
311302
312303 // Wait until signalBoolReturn and signalBoolValue have been called.
313- while (count > 0 ) {
314- PumpMessage ();
315- }
304+ latch.Wait ();
316305 EXPECT_TRUE (bool_value_passed);
317306}
318307
@@ -625,10 +614,10 @@ TEST_F(WindowsTest, AddRemoveView) {
625614 WindowsConfigBuilder builder (context);
626615 builder.SetDartEntrypoint (" onMetricsChangedSignalViewIds" );
627616
628- bool ready = false ;
617+ fml::AutoResetWaitableEvent ready_latch ;
629618 context.AddNativeFunction (
630- " Signal" ,
631- CREATE_NATIVE_ENTRY ( [&](Dart_NativeArguments args) { ready = true ; }));
619+ " Signal" , CREATE_NATIVE_ENTRY (
620+ [&](Dart_NativeArguments args) { ready_latch. Signal () ; }));
632621
633622 context.AddNativeFunction (
634623 " SignalStringValue" , CREATE_NATIVE_ENTRY ([&](Dart_NativeArguments args) {
@@ -643,9 +632,7 @@ TEST_F(WindowsTest, AddRemoveView) {
643632 ViewControllerPtr first_controller{builder.Run ()};
644633 ASSERT_NE (first_controller, nullptr );
645634
646- while (!ready) {
647- PumpMessage ();
648- }
635+ ready_latch.Wait ();
649636
650637 // Create a second view.
651638 FlutterDesktopEngineRef engine =
@@ -683,6 +670,7 @@ TEST_F(WindowsTest, EngineId) {
683670 WindowsConfigBuilder builder (context);
684671 builder.SetDartEntrypoint (" testEngineId" );
685672
673+ fml::AutoResetWaitableEvent latch;
686674 std::optional<int64_t > engineId;
687675 context.AddNativeFunction (
688676 " NotifyEngineId" , CREATE_NATIVE_ENTRY ([&](Dart_NativeArguments args) {
@@ -691,15 +679,17 @@ TEST_F(WindowsTest, EngineId) {
691679 const auto handle = tonic::DartConverter<int64_t >::FromDart (argument);
692680 engineId = handle;
693681 }
682+ latch.Signal ();
694683 }));
695684 // Create the implicit view.
696685 ViewControllerPtr first_controller{builder.Run ()};
697686 ASSERT_NE (first_controller, nullptr );
698687
699- while (!engineId.has_value ()) {
700- PumpMessage ();
688+ latch.Wait ();
689+ EXPECT_TRUE (engineId.has_value ());
690+ if (!engineId.has_value ()) {
691+ return ;
701692 }
702-
703693 auto engine = FlutterDesktopViewControllerGetEngine (first_controller.get ());
704694 EXPECT_EQ (engine, FlutterDesktopEngineForId (*engineId));
705695}
0 commit comments