Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a42b2d9

Browse files
Fix a race in the EmbedderA11yTest.A11yTreeIsConsistent tests (#37488)
1 parent b42ed69 commit a42b2d9

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

shell/platform/embedder/tests/embedder_a11y_unittests.cc

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ TEST_F(EmbedderA11yTest, A11yTreeIsConsistent) {
4646

4747
auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext);
4848

49-
fml::AutoResetWaitableEvent latch;
49+
fml::AutoResetWaitableEvent signal_native_latch;
5050

5151
// Called by the Dart text fixture on the UI thread to signal that the C++
5252
// unittest should resume.
5353
context.AddNativeCallback(
54-
"SignalNativeTest", CREATE_NATIVE_ENTRY(([&latch](Dart_NativeArguments) {
55-
latch.Signal();
54+
"SignalNativeTest",
55+
CREATE_NATIVE_ENTRY(([&signal_native_latch](Dart_NativeArguments) {
56+
signal_native_latch.Signal();
5657
})));
5758

5859
// Called by test fixture on UI thread to pass data back to this test.
@@ -120,14 +121,15 @@ TEST_F(EmbedderA11yTest, A11yTreeIsConsistent) {
120121
ASSERT_TRUE(engine.is_valid());
121122

122123
// Wait for initial NotifySemanticsEnabled(false).
124+
fml::AutoResetWaitableEvent notify_semantics_enabled_latch;
123125
notify_semantics_enabled_callback = [&](Dart_NativeArguments args) {
124126
bool enabled = true;
125127
auto handle = Dart_GetNativeBooleanArgument(args, 0, &enabled);
126128
ASSERT_FALSE(Dart_IsError(handle));
127129
ASSERT_FALSE(enabled);
128-
latch.Signal();
130+
notify_semantics_enabled_latch.Signal();
129131
};
130-
latch.Wait();
132+
notify_semantics_enabled_latch.Wait();
131133

132134
// Prepare to NotifyAccessibilityFeatures call
133135
fml::AutoResetWaitableEvent notify_features_latch;
@@ -140,39 +142,42 @@ TEST_F(EmbedderA11yTest, A11yTreeIsConsistent) {
140142
};
141143

142144
// Enable semantics. Wait for NotifySemanticsEnabled(true).
145+
fml::AutoResetWaitableEvent notify_semantics_enabled_latch_2;
143146
notify_semantics_enabled_callback = [&](Dart_NativeArguments args) {
144147
bool enabled = false;
145148
auto handle = Dart_GetNativeBooleanArgument(args, 0, &enabled);
146149
ASSERT_FALSE(Dart_IsError(handle));
147150
ASSERT_TRUE(enabled);
148-
latch.Signal();
151+
notify_semantics_enabled_latch_2.Signal();
149152
};
150153
auto result = FlutterEngineUpdateSemanticsEnabled(engine.get(), true);
151154
ASSERT_EQ(result, FlutterEngineResult::kSuccess);
152-
latch.Wait();
155+
notify_semantics_enabled_latch_2.Wait();
153156

154157
// Wait for initial accessibility features (reduce_motion == false)
155158
notify_features_latch.Wait();
156159

157160
// Set accessibility features: (reduce_motion == true)
161+
fml::AutoResetWaitableEvent notify_features_latch_2;
158162
notify_accessibility_features_callback = [&](Dart_NativeArguments args) {
159163
bool enabled = false;
160164
auto handle = Dart_GetNativeBooleanArgument(args, 0, &enabled);
161165
ASSERT_FALSE(Dart_IsError(handle));
162166
ASSERT_TRUE(enabled);
163-
latch.Signal();
167+
notify_features_latch_2.Signal();
164168
};
165169
result = FlutterEngineUpdateAccessibilityFeatures(
166170
engine.get(), kFlutterAccessibilityFeatureReduceMotion);
167171
ASSERT_EQ(result, FlutterEngineResult::kSuccess);
168-
latch.Wait();
172+
notify_features_latch_2.Wait();
169173

170174
// Wait for UpdateSemantics callback on platform (current) thread.
171-
latch.Wait();
175+
signal_native_latch.Wait();
172176
fml::MessageLoop::GetCurrent().RunExpiredTasksNow();
173177
semantics_update_latch.Wait();
174178

175179
// Dispatch a tap to semantics node 42. Wait for NotifySemanticsAction.
180+
fml::AutoResetWaitableEvent notify_semantics_action_latch;
176181
notify_semantics_action_callback = [&](Dart_NativeArguments args) {
177182
int64_t node_id = 0;
178183
Dart_GetNativeIntegerArgument(args, 0, &node_id);
@@ -192,36 +197,38 @@ TEST_F(EmbedderA11yTest, A11yTreeIsConsistent) {
192197
dart_int = Dart_ListGetAt(semantic_args, 1);
193198
Dart_IntegerToInt64(dart_int, &data);
194199
ASSERT_EQ(1, data);
195-
latch.Signal();
200+
notify_semantics_action_latch.Signal();
196201
};
197202
std::vector<uint8_t> bytes({2, 1});
198203
result = FlutterEngineDispatchSemanticsAction(
199204
engine.get(), 42, kFlutterSemanticsActionTap, &bytes[0], bytes.size());
200205
ASSERT_EQ(result, FlutterEngineResult::kSuccess);
201-
latch.Wait();
206+
notify_semantics_action_latch.Wait();
202207

203208
// Disable semantics. Wait for NotifySemanticsEnabled(false).
209+
fml::AutoResetWaitableEvent notify_semantics_enabled_latch_3;
204210
notify_semantics_enabled_callback = [&](Dart_NativeArguments args) {
205211
bool enabled = true;
206212
Dart_GetNativeBooleanArgument(args, 0, &enabled);
207213
ASSERT_FALSE(enabled);
208-
latch.Signal();
214+
notify_semantics_enabled_latch_3.Signal();
209215
};
210216
result = FlutterEngineUpdateSemanticsEnabled(engine.get(), false);
211217
ASSERT_EQ(result, FlutterEngineResult::kSuccess);
212-
latch.Wait();
218+
notify_semantics_enabled_latch_3.Wait();
213219
}
214220

215221
TEST_F(EmbedderA11yTest, A11yTreeIsConsistentUsingLegacyCallbacks) {
216222
auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext);
217223

218-
fml::AutoResetWaitableEvent latch;
224+
fml::AutoResetWaitableEvent signal_native_latch;
219225

220226
// Called by the Dart text fixture on the UI thread to signal that the C++
221227
// unittest should resume.
222228
context.AddNativeCallback(
223-
"SignalNativeTest", CREATE_NATIVE_ENTRY(([&latch](Dart_NativeArguments) {
224-
latch.Signal();
229+
"SignalNativeTest",
230+
CREATE_NATIVE_ENTRY(([&signal_native_latch](Dart_NativeArguments) {
231+
signal_native_latch.Signal();
225232
})));
226233

227234
// Called by test fixture on UI thread to pass data back to this test.
@@ -310,14 +317,15 @@ TEST_F(EmbedderA11yTest, A11yTreeIsConsistentUsingLegacyCallbacks) {
310317
ASSERT_TRUE(engine.is_valid());
311318

312319
// Wait for initial NotifySemanticsEnabled(false).
320+
fml::AutoResetWaitableEvent notify_semantics_enabled_latch;
313321
notify_semantics_enabled_callback = [&](Dart_NativeArguments args) {
314322
bool enabled = true;
315323
auto handle = Dart_GetNativeBooleanArgument(args, 0, &enabled);
316324
ASSERT_FALSE(Dart_IsError(handle));
317325
ASSERT_FALSE(enabled);
318-
latch.Signal();
326+
notify_semantics_enabled_latch.Signal();
319327
};
320-
latch.Wait();
328+
notify_semantics_enabled_latch.Wait();
321329

322330
// Prepare to NotifyAccessibilityFeatures call
323331
fml::AutoResetWaitableEvent notify_features_latch;
@@ -330,35 +338,37 @@ TEST_F(EmbedderA11yTest, A11yTreeIsConsistentUsingLegacyCallbacks) {
330338
};
331339

332340
// Enable semantics. Wait for NotifySemanticsEnabled(true).
341+
fml::AutoResetWaitableEvent notify_semantics_enabled_latch_2;
333342
notify_semantics_enabled_callback = [&](Dart_NativeArguments args) {
334343
bool enabled = false;
335344
auto handle = Dart_GetNativeBooleanArgument(args, 0, &enabled);
336345
ASSERT_FALSE(Dart_IsError(handle));
337346
ASSERT_TRUE(enabled);
338-
latch.Signal();
347+
notify_semantics_enabled_latch_2.Signal();
339348
};
340349
auto result = FlutterEngineUpdateSemanticsEnabled(engine.get(), true);
341350
ASSERT_EQ(result, FlutterEngineResult::kSuccess);
342-
latch.Wait();
351+
notify_semantics_enabled_latch_2.Wait();
343352

344353
// Wait for initial accessibility features (reduce_motion == false)
345354
notify_features_latch.Wait();
346355

347356
// Set accessibility features: (reduce_motion == true)
357+
fml::AutoResetWaitableEvent notify_features_latch_2;
348358
notify_accessibility_features_callback = [&](Dart_NativeArguments args) {
349359
bool enabled = false;
350360
auto handle = Dart_GetNativeBooleanArgument(args, 0, &enabled);
351361
ASSERT_FALSE(Dart_IsError(handle));
352362
ASSERT_TRUE(enabled);
353-
latch.Signal();
363+
notify_features_latch_2.Signal();
354364
};
355365
result = FlutterEngineUpdateAccessibilityFeatures(
356366
engine.get(), kFlutterAccessibilityFeatureReduceMotion);
357367
ASSERT_EQ(result, FlutterEngineResult::kSuccess);
358-
latch.Wait();
368+
notify_features_latch_2.Wait();
359369

360370
// Wait for UpdateSemantics callback on platform (current) thread.
361-
latch.Wait();
371+
signal_native_latch.Wait();
362372
fml::MessageLoop::GetCurrent().RunExpiredTasksNow();
363373
semantics_node_latch.Wait();
364374
semantics_action_latch.Wait();
@@ -368,6 +378,7 @@ TEST_F(EmbedderA11yTest, A11yTreeIsConsistentUsingLegacyCallbacks) {
368378
ASSERT_EQ(1, action_batch_end_count);
369379

370380
// Dispatch a tap to semantics node 42. Wait for NotifySemanticsAction.
381+
fml::AutoResetWaitableEvent notify_semantics_action_latch;
371382
notify_semantics_action_callback = [&](Dart_NativeArguments args) {
372383
int64_t node_id = 0;
373384
Dart_GetNativeIntegerArgument(args, 0, &node_id);
@@ -387,24 +398,25 @@ TEST_F(EmbedderA11yTest, A11yTreeIsConsistentUsingLegacyCallbacks) {
387398
dart_int = Dart_ListGetAt(semantic_args, 1);
388399
Dart_IntegerToInt64(dart_int, &data);
389400
ASSERT_EQ(1, data);
390-
latch.Signal();
401+
notify_semantics_action_latch.Signal();
391402
};
392403
std::vector<uint8_t> bytes({2, 1});
393404
result = FlutterEngineDispatchSemanticsAction(
394405
engine.get(), 42, kFlutterSemanticsActionTap, &bytes[0], bytes.size());
395406
ASSERT_EQ(result, FlutterEngineResult::kSuccess);
396-
latch.Wait();
407+
notify_semantics_action_latch.Wait();
397408

398409
// Disable semantics. Wait for NotifySemanticsEnabled(false).
410+
fml::AutoResetWaitableEvent notify_semantics_enabled_latch_3;
399411
notify_semantics_enabled_callback = [&](Dart_NativeArguments args) {
400412
bool enabled = true;
401413
Dart_GetNativeBooleanArgument(args, 0, &enabled);
402414
ASSERT_FALSE(enabled);
403-
latch.Signal();
415+
notify_semantics_enabled_latch_3.Signal();
404416
};
405417
result = FlutterEngineUpdateSemanticsEnabled(engine.get(), false);
406418
ASSERT_EQ(result, FlutterEngineResult::kSuccess);
407-
latch.Wait();
419+
notify_semantics_enabled_latch_3.Wait();
408420
}
409421

410422
} // namespace testing

0 commit comments

Comments
 (0)