[Windows] Refactor the a11y announcement test (flutter/engine#50888)
This refactors the Windows accessibility announcement test. This will make it easier to add a headless variant of this test as part of https://github.com/flutter/flutter/issues/143765. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -43,8 +43,22 @@ void hiPlatformChannels() {
|
||||
});
|
||||
}
|
||||
|
||||
/// Returns a future that completes when
|
||||
/// `PlatformDispatcher.instance.onSemanticsEnabledChanged` fires.
|
||||
Future<void> get semanticsChanged {
|
||||
final Completer<void> semanticsChanged = Completer<void>();
|
||||
ui.PlatformDispatcher.instance.onSemanticsEnabledChanged =
|
||||
semanticsChanged.complete;
|
||||
return semanticsChanged.future;
|
||||
}
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void alertPlatformChannel() async {
|
||||
void sendAccessibilityAnnouncement() async {
|
||||
// Wait until semantics are enabled.
|
||||
if (!ui.PlatformDispatcher.instance.semanticsEnabled) {
|
||||
await semanticsChanged;
|
||||
}
|
||||
|
||||
// Serializers for data types are in the framework, so this will be hardcoded.
|
||||
const int valueMap = 13, valueString = 7;
|
||||
// Corresponds to:
|
||||
@@ -78,13 +92,11 @@ void alertPlatformChannel() async {
|
||||
]);
|
||||
final ByteData byteData = data.buffer.asByteData();
|
||||
|
||||
final Completer<ByteData?> enabled = Completer<ByteData?>();
|
||||
ui.PlatformDispatcher.instance.sendPlatformMessage('semantics', ByteData(0), (ByteData? reply){
|
||||
enabled.complete(reply);
|
||||
});
|
||||
await enabled.future;
|
||||
|
||||
ui.PlatformDispatcher.instance.sendPlatformMessage('flutter/accessibility', byteData, (ByteData? _){});
|
||||
ui.PlatformDispatcher.instance.sendPlatformMessage(
|
||||
'flutter/accessibility',
|
||||
byteData,
|
||||
(ByteData? _) => signal(),
|
||||
);
|
||||
}
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "flutter/shell/platform/windows/testing/mock_windows_proc_table.h"
|
||||
#include "flutter/shell/platform/windows/testing/test_keyboard.h"
|
||||
#include "flutter/shell/platform/windows/testing/windows_test.h"
|
||||
#include "flutter/shell/platform/windows/testing/windows_test_config_builder.h"
|
||||
#include "flutter/third_party/accessibility/ax/platform/ax_platform_node_win.h"
|
||||
#include "fml/synchronization/waitable_event.h"
|
||||
#include "gmock/gmock.h"
|
||||
@@ -27,6 +28,7 @@
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
|
||||
using ::testing::NiceMock;
|
||||
using ::testing::Return;
|
||||
|
||||
class FlutterWindowsEngineTest : public WindowsTest {};
|
||||
@@ -636,44 +638,41 @@ class MockFlutterWindowsView : public FlutterWindowsView {
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(MockFlutterWindowsView);
|
||||
};
|
||||
|
||||
TEST_F(FlutterWindowsEngineTest, AlertPlatformMessage) {
|
||||
FlutterWindowsEngineBuilder builder{GetContext()};
|
||||
builder.SetDartEntrypoint("alertPlatformChannel");
|
||||
// Verify the view is notified of accessibility announcements.
|
||||
TEST_F(FlutterWindowsEngineTest, AccessibilityAnnouncement) {
|
||||
auto& context = GetContext();
|
||||
WindowsConfigBuilder builder{context};
|
||||
builder.SetDartEntrypoint("sendAccessibilityAnnouncement");
|
||||
|
||||
bool done = false;
|
||||
auto native_entry =
|
||||
CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { done = true; });
|
||||
context.AddNativeFunction("Signal", native_entry);
|
||||
|
||||
EnginePtr engine{builder.RunHeadless()};
|
||||
ASSERT_NE(engine, nullptr);
|
||||
|
||||
auto engine = builder.Build();
|
||||
auto window_binding_handler =
|
||||
std::make_unique<::testing::NiceMock<MockWindowBindingHandler>>();
|
||||
ui::AXPlatformNodeDelegateBase parent_delegate;
|
||||
AlertPlatformNodeDelegate delegate(parent_delegate);
|
||||
AlertPlatformNodeDelegate delegate{parent_delegate};
|
||||
|
||||
auto window_binding_handler =
|
||||
std::make_unique<NiceMock<MockWindowBindingHandler>>();
|
||||
EXPECT_CALL(*window_binding_handler, GetAlertDelegate)
|
||||
.WillRepeatedly(Return(&delegate));
|
||||
MockFlutterWindowsView view(engine.get(), std::move(window_binding_handler));
|
||||
.WillOnce(Return(&delegate));
|
||||
|
||||
EngineModifier modifier(engine.get());
|
||||
auto windows_engine = reinterpret_cast<FlutterWindowsEngine*>(engine.get());
|
||||
MockFlutterWindowsView view{windows_engine,
|
||||
std::move(window_binding_handler)};
|
||||
EngineModifier modifier{windows_engine};
|
||||
modifier.SetImplicitView(&view);
|
||||
modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; };
|
||||
|
||||
auto binary_messenger =
|
||||
std::make_unique<BinaryMessengerImpl>(engine->messenger());
|
||||
binary_messenger->SetMessageHandler(
|
||||
"semantics", [&engine](const uint8_t* message, size_t message_size,
|
||||
BinaryReply reply) {
|
||||
engine->UpdateSemanticsEnabled(true);
|
||||
char response[] = "";
|
||||
reply(reinterpret_cast<uint8_t*>(response), 0);
|
||||
});
|
||||
windows_engine->UpdateSemanticsEnabled(true);
|
||||
|
||||
bool did_call = false;
|
||||
EXPECT_CALL(view, NotifyWinEventWrapper)
|
||||
.WillOnce([&did_call](ui::AXPlatformNodeWin* node,
|
||||
ax::mojom::Event event) { did_call = true; });
|
||||
|
||||
engine->UpdateSemanticsEnabled(true);
|
||||
engine->Run();
|
||||
EXPECT_CALL(view, NotifyWinEventWrapper).Times(1);
|
||||
|
||||
// Rely on timeout mechanism in CI.
|
||||
while (!did_call) {
|
||||
engine->task_runner()->ProcessTasks();
|
||||
while (!done) {
|
||||
windows_engine->task_runner()->ProcessTasks();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user