From ef3bc43183aa79cdd1e5024930b85b8c2bcfc775 Mon Sep 17 00:00:00 2001 From: yaakovschectman <109111084+yaakovschectman@users.noreply.github.com> Date: Thu, 15 Sep 2022 18:02:12 -0400 Subject: [PATCH] Communicate switch status to semantics on Windows (flutter/engine#36177) * Switch semantics * Test native role --- .../platform/common/accessibility_bridge.cc | 7 ++ .../windows/flutter_windows_view_unittests.cc | 101 +++++++++++++++++- .../ax/platform/ax_platform_node_win.cc | 2 +- 3 files changed, 107 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/shell/platform/common/accessibility_bridge.cc b/engine/src/flutter/shell/platform/common/accessibility_bridge.cc index 0c83110402..3a050a5a41 100644 --- a/engine/src/flutter/shell/platform/common/accessibility_bridge.cc +++ b/engine/src/flutter/shell/platform/common/accessibility_bridge.cc @@ -385,6 +385,13 @@ void AccessibilityBridge::SetIntAttributesFromFlutterUpdate( : flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsChecked ? ax::mojom::CheckedState::kTrue : ax::mojom::CheckedState::kFalse)); + } else if (node_data.role == ax::mojom::Role::kToggleButton) { + node_data.AddIntAttribute( + ax::mojom::IntAttribute::kCheckedState, + static_cast( + flags & FlutterSemanticsFlag::kFlutterSemanticsFlagIsToggled + ? ax::mojom::CheckedState::kTrue + : ax::mojom::CheckedState::kFalse)); } } diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_view_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_view_unittests.cc index 58b5cc7015..766d36af89 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view_unittests.cc @@ -685,7 +685,7 @@ TEST(FlutterWindowsViewTest, CheckboxNativeState) { VARIANT varchild = {}; varchild.vt = VT_I4; - // Verify the checkbox is checked. + // Verify the checkbox is unchecked. varchild.lVal = CHILDID_SELF; VARIANT native_state = {}; ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state))); @@ -716,7 +716,7 @@ TEST(FlutterWindowsViewTest, CheckboxNativeState) { VARIANT varchild = {}; varchild.vt = VT_I4; - // Verify the checkbox is checked. + // Verify the checkbox is mixed. varchild.lVal = CHILDID_SELF; VARIANT native_state = {}; ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state))); @@ -724,5 +724,102 @@ TEST(FlutterWindowsViewTest, CheckboxNativeState) { } } +// Ensure that switches have their toggle status set apropriately +TEST(FlutterWindowsViewTest, SwitchNativeState) { + std::unique_ptr engine = GetTestEngine(); + EngineModifier modifier(engine.get()); + modifier.embedder_api().UpdateSemanticsEnabled = + [](FLUTTER_API_SYMBOL(FlutterEngine) engine, bool enabled) { + return kSuccess; + }; + + auto window_binding_handler = + std::make_unique<::testing::NiceMock>(); + FlutterWindowsView view(std::move(window_binding_handler)); + view.SetEngine(std::move(engine)); + + // Enable semantics to instantiate accessibility bridge. + view.OnUpdateSemanticsEnabled(true); + + auto bridge = view.GetEngine()->accessibility_bridge().lock(); + ASSERT_TRUE(bridge); + + FlutterSemanticsNode root{sizeof(FlutterSemanticsNode), 0}; + root.id = 0; + root.label = "root"; + root.hint = ""; + root.value = ""; + root.increased_value = ""; + root.decreased_value = ""; + root.child_count = 0; + root.custom_accessibility_actions_count = 0; + root.flags = static_cast( + FlutterSemanticsFlag::kFlutterSemanticsFlagHasToggledState | + FlutterSemanticsFlag::kFlutterSemanticsFlagIsToggled); + bridge->AddFlutterSemanticsNodeUpdate(&root); + + bridge->CommitUpdates(); + + { + auto root_node = bridge + ->GetFlutterPlatformNodeDelegateFromID( + AccessibilityBridge::kRootNodeId) + .lock(); + EXPECT_EQ(root_node->GetData().role, ax::mojom::Role::kToggleButton); + EXPECT_EQ(root_node->GetData().GetCheckedState(), + ax::mojom::CheckedState::kTrue); + + // Get the IAccessible for the root node. + IAccessible* native_view = root_node->GetNativeViewAccessible(); + ASSERT_TRUE(native_view != nullptr); + + // Look up against the node itself (not one of its children). + VARIANT varchild = {}; + varchild.vt = VT_I4; + + varchild.lVal = CHILDID_SELF; + VARIANT varrole = {}; + + // Verify the role of the switch is CHECKBUTTON + ASSERT_EQ(native_view->get_accRole(varchild, &varrole), S_OK); + ASSERT_EQ(varrole.lVal, ROLE_SYSTEM_CHECKBUTTON); + + // Verify the switch is pressed. + VARIANT native_state = {}; + ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state))); + EXPECT_TRUE(native_state.lVal & STATE_SYSTEM_PRESSED); + } + + // Test unpressed too. + root.flags = static_cast( + FlutterSemanticsFlag::kFlutterSemanticsFlagHasToggledState); + bridge->AddFlutterSemanticsNodeUpdate(&root); + bridge->CommitUpdates(); + + { + auto root_node = bridge + ->GetFlutterPlatformNodeDelegateFromID( + AccessibilityBridge::kRootNodeId) + .lock(); + EXPECT_EQ(root_node->GetData().role, ax::mojom::Role::kToggleButton); + EXPECT_EQ(root_node->GetData().GetCheckedState(), + ax::mojom::CheckedState::kFalse); + + // Get the IAccessible for the root node. + IAccessible* native_view = root_node->GetNativeViewAccessible(); + ASSERT_TRUE(native_view != nullptr); + + // Look up against the node itself (not one of its children). + VARIANT varchild = {}; + varchild.vt = VT_I4; + + // Verify the switch is not pressed. + varchild.lVal = CHILDID_SELF; + VARIANT native_state = {}; + ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state))); + EXPECT_FALSE(native_state.lVal & STATE_SYSTEM_PRESSED); + } +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_win.cc b/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_win.cc index f56c0a3737..1e649877a8 100644 --- a/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_win.cc +++ b/engine/src/flutter/third_party/accessibility/ax/platform/ax_platform_node_win.cc @@ -3421,7 +3421,7 @@ int AXPlatformNodeWin::MSAARole() { return ROLE_SYSTEM_TITLEBAR; case ax::mojom::Role::kToggleButton: - return ROLE_SYSTEM_PUSHBUTTON; + return ROLE_SYSTEM_CHECKBUTTON; case ax::mojom::Role::kTextField: case ax::mojom::Role::kSearchBox: