Flutter Engine
 
Loading...
Searching...
No Matches
accessibility_bridge_windows_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <comdef.h>
8#include <comutil.h>
9#include <oleacc.h>
10
11#include <vector>
12
13#include "flutter/fml/macros.h"
22#include "gmock/gmock.h"
23#include "gtest/gtest.h"
24
25namespace flutter {
26namespace testing {
27
28namespace {
29using ::testing::NiceMock;
30
31// A structure representing a Win32 MSAA event targeting a specified node.
32struct MsaaEvent {
33 std::shared_ptr<FlutterPlatformNodeDelegateWindows> node_delegate;
35};
36
37// Accessibility bridge delegate that captures events dispatched to the OS.
38class AccessibilityBridgeWindowsSpy : public AccessibilityBridgeWindows {
39 public:
41
42 explicit AccessibilityBridgeWindowsSpy(FlutterWindowsEngine* engine,
43 FlutterWindowsView* view)
44 : AccessibilityBridgeWindows(view) {}
45
46 void DispatchWinAccessibilityEvent(
47 std::shared_ptr<FlutterPlatformNodeDelegateWindows> node_delegate,
49 dispatched_events_.push_back({node_delegate, event_type});
50 }
51
52 void SetFocus(std::shared_ptr<FlutterPlatformNodeDelegateWindows>
53 node_delegate) override {
54 focused_nodes_.push_back(std::move(node_delegate));
55 }
56
57 void ResetRecords() {
58 dispatched_events_.clear();
59 focused_nodes_.clear();
60 }
61
62 const std::vector<MsaaEvent>& dispatched_events() const {
63 return dispatched_events_;
64 }
65
66 const std::vector<int32_t> focused_nodes() const {
67 std::vector<int32_t> ids;
68 std::transform(focused_nodes_.begin(), focused_nodes_.end(),
69 std::back_inserter(ids),
70 [](std::shared_ptr<FlutterPlatformNodeDelegate> node) {
71 return node->GetAXNode()->id();
72 });
73 return ids;
74 }
75
76 protected:
77 std::weak_ptr<FlutterPlatformNodeDelegate> GetFocusedNode() override {
78 return focused_nodes_.back();
79 }
80
81 private:
82 std::vector<MsaaEvent> dispatched_events_;
83 std::vector<std::shared_ptr<FlutterPlatformNodeDelegate>> focused_nodes_;
84
85 FML_DISALLOW_COPY_AND_ASSIGN(AccessibilityBridgeWindowsSpy);
86};
87
88// A FlutterWindowsView whose accessibility bridge is an
89// AccessibilityBridgeWindowsSpy.
90class FlutterWindowsViewSpy : public FlutterWindowsView {
91 public:
92 FlutterWindowsViewSpy(FlutterWindowsEngine* engine,
93 std::unique_ptr<WindowBindingHandler> handler)
94 : FlutterWindowsView(kImplicitViewId, engine, std::move(handler)) {}
95
96 protected:
97 virtual std::shared_ptr<AccessibilityBridgeWindows>
98 CreateAccessibilityBridge() override {
99 return std::make_shared<AccessibilityBridgeWindowsSpy>(GetEngine(), this);
100 }
101
102 private:
103 FML_DISALLOW_COPY_AND_ASSIGN(FlutterWindowsViewSpy);
104};
105
106// Returns an engine instance configured with dummy project path values, and
107// overridden methods for sending platform messages, so that the engine can
108// respond as if the framework were connected.
109std::unique_ptr<FlutterWindowsEngine> GetTestEngine() {
110 FlutterDesktopEngineProperties properties = {};
111 properties.assets_path = L"C:\\foo\\flutter_assets";
112 properties.icu_data_path = L"C:\\foo\\icudtl.dat";
113 properties.aot_library_path = L"C:\\foo\\aot.so";
114 FlutterProjectBundle project(properties);
115 auto engine = std::make_unique<FlutterWindowsEngine>(project);
116
117 EngineModifier modifier(engine.get());
118 modifier.embedder_api().UpdateSemanticsEnabled =
119 [](FLUTTER_API_SYMBOL(FlutterEngine) engine, bool enabled) {
120 return kSuccess;
121 };
122
124 std::make_shared<MockKeyResponseController>());
125
126 engine->Run();
127 return engine;
128}
129
130// Populates the AXTree associated with the specified bridge with test data.
131//
132// node0
133// / \
134// node1 node2
135// / \
136// node3 node4
137//
138// node0 and node2 are grouping nodes. node1 and node2 are static text nodes.
139// node4 is a static text node with no text, and hence has the "ignored" state.
140void PopulateAXTree(std::shared_ptr<AccessibilityBridge> bridge) {
141 // Add node 0: root.
143 auto empty_flags = FlutterSemanticsFlags{};
144 std::vector<int32_t> node0_children{1, 2};
145 node0.child_count = node0_children.size();
146 node0.children_in_traversal_order = node0_children.data();
147 node0.children_in_hit_test_order = node0_children.data();
148 node0.flags2 = &empty_flags;
149
150 // Add node 1: text child of node 0.
152 node1.label = "prefecture";
153 node1.value = "Kyoto";
154 node1.flags2 = &empty_flags;
155
156 // Add node 2: subtree child of node 0.
158 std::vector<int32_t> node2_children{3, 4};
159 node2.child_count = node2_children.size();
160 node2.children_in_traversal_order = node2_children.data();
161 node2.children_in_hit_test_order = node2_children.data();
162 node2.flags2 = &empty_flags;
163
164 // Add node 3: text child of node 2.
166 node3.label = "city";
167 node3.value = "Uji";
168 node3.flags2 = &empty_flags;
169
170 // Add node 4: text child (with no text) of node 2.
172 node4.flags2 = &empty_flags;
173
174 bridge->AddFlutterSemanticsNodeUpdate(node0);
175 bridge->AddFlutterSemanticsNodeUpdate(node1);
176 bridge->AddFlutterSemanticsNodeUpdate(node2);
177 bridge->AddFlutterSemanticsNodeUpdate(node3);
178 bridge->AddFlutterSemanticsNodeUpdate(node4);
179 bridge->CommitUpdates();
180}
181
182ui::AXNode* AXNodeFromID(std::shared_ptr<AccessibilityBridge> bridge,
183 int32_t id) {
184 auto node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(id).lock();
185 return node_delegate ? node_delegate->GetAXNode() : nullptr;
186}
187
188std::shared_ptr<AccessibilityBridgeWindowsSpy> GetAccessibilityBridgeSpy(
189 FlutterWindowsView& view) {
190 return std::static_pointer_cast<AccessibilityBridgeWindowsSpy>(
191 view.accessibility_bridge().lock());
192}
193
194void ExpectWinEventFromAXEvent(int32_t node_id,
196 ax::mojom::Event expected_event) {
197 auto engine = GetTestEngine();
198 FlutterWindowsViewSpy view{
199 engine.get(), std::make_unique<NiceMock<MockWindowBindingHandler>>()};
200 EngineModifier modifier{engine.get()};
201 modifier.SetImplicitView(&view);
202 view.OnUpdateSemanticsEnabled(true);
203
204 auto bridge = GetAccessibilityBridgeSpy(view);
205 PopulateAXTree(bridge);
206
207 bridge->ResetRecords();
208 bridge->OnAccessibilityEvent({AXNodeFromID(bridge, node_id),
209 {ax_event, ax::mojom::EventFrom::kNone, {}}});
210 ASSERT_EQ(bridge->dispatched_events().size(), 1);
211 EXPECT_EQ(bridge->dispatched_events()[0].event_type, expected_event);
212}
213
214void ExpectWinEventFromAXEventOnFocusNode(int32_t node_id,
216 ax::mojom::Event expected_event,
217 int32_t focus_id) {
218 auto engine = GetTestEngine();
219 FlutterWindowsViewSpy view{
220 engine.get(), std::make_unique<NiceMock<MockWindowBindingHandler>>()};
221 EngineModifier modifier{engine.get()};
222 modifier.SetImplicitView(&view);
223 view.OnUpdateSemanticsEnabled(true);
224
225 auto bridge = GetAccessibilityBridgeSpy(view);
226 PopulateAXTree(bridge);
227
228 bridge->ResetRecords();
229 auto focus_delegate =
230 bridge->GetFlutterPlatformNodeDelegateFromID(focus_id).lock();
231 bridge->SetFocus(std::static_pointer_cast<FlutterPlatformNodeDelegateWindows>(
232 focus_delegate));
233 bridge->OnAccessibilityEvent({AXNodeFromID(bridge, node_id),
234 {ax_event, ax::mojom::EventFrom::kNone, {}}});
235 ASSERT_EQ(bridge->dispatched_events().size(), 1);
236 EXPECT_EQ(bridge->dispatched_events()[0].event_type, expected_event);
237 EXPECT_EQ(bridge->dispatched_events()[0].node_delegate->GetAXNode()->id(),
238 focus_id);
239}
240
241} // namespace
242
244 auto engine = GetTestEngine();
245 FlutterWindowsViewSpy view{
246 engine.get(), std::make_unique<NiceMock<MockWindowBindingHandler>>()};
247 EngineModifier modifier{engine.get()};
248 modifier.SetImplicitView(&view);
249 view.OnUpdateSemanticsEnabled(true);
250
251 auto bridge = view.accessibility_bridge().lock();
252 PopulateAXTree(bridge);
253
254 auto node0_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
255 auto node1_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
256 EXPECT_EQ(node0_delegate->GetNativeViewAccessible(),
257 node1_delegate->GetParent());
258}
259
260TEST(AccessibilityBridgeWindows, GetParentOnRootRetunsNullptr) {
261 auto engine = GetTestEngine();
262 FlutterWindowsViewSpy view{
263 engine.get(), std::make_unique<NiceMock<MockWindowBindingHandler>>()};
264 EngineModifier modifier{engine.get()};
265 modifier.SetImplicitView(&view);
266 view.OnUpdateSemanticsEnabled(true);
267
268 auto bridge = view.accessibility_bridge().lock();
269 PopulateAXTree(bridge);
270
271 auto node0_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
272 ASSERT_TRUE(node0_delegate->GetParent() == nullptr);
273}
274
275TEST(AccessibilityBridgeWindows, DispatchAccessibilityAction) {
276 auto engine = GetTestEngine();
277 FlutterWindowsViewSpy view{
278 engine.get(), std::make_unique<NiceMock<MockWindowBindingHandler>>()};
279 EngineModifier modifier{engine.get()};
280 modifier.SetImplicitView(&view);
281 view.OnUpdateSemanticsEnabled(true);
282
283 auto bridge = view.accessibility_bridge().lock();
284 PopulateAXTree(bridge);
285
287 modifier.embedder_api().SendSemanticsAction = MOCK_ENGINE_PROC(
288 SendSemanticsAction,
289 ([&actual_action](FLUTTER_API_SYMBOL(FlutterEngine) engine,
290 const FlutterSendSemanticsActionInfo* info) {
291 actual_action = info->action;
292 return kSuccess;
293 }));
294
297 EXPECT_EQ(actual_action, kFlutterSemanticsActionCopy);
298}
299
300TEST(AccessibilityBridgeWindows, OnAccessibilityEventAlert) {
301 ExpectWinEventFromAXEvent(0, ui::AXEventGenerator::Event::ALERT,
303}
304
305TEST(AccessibilityBridgeWindows, OnAccessibilityEventChildrenChanged) {
306 ExpectWinEventFromAXEvent(0, ui::AXEventGenerator::Event::CHILDREN_CHANGED,
308}
309
310TEST(AccessibilityBridgeWindows, OnAccessibilityEventFocusChanged) {
311 auto engine = GetTestEngine();
312 FlutterWindowsViewSpy view{
313 engine.get(), std::make_unique<NiceMock<MockWindowBindingHandler>>()};
314 EngineModifier modifier{engine.get()};
315 modifier.SetImplicitView(&view);
316 view.OnUpdateSemanticsEnabled(true);
317
318 auto bridge = GetAccessibilityBridgeSpy(view);
319 PopulateAXTree(bridge);
320
321 bridge->ResetRecords();
322 bridge->OnAccessibilityEvent({AXNodeFromID(bridge, 1),
325 {}}});
326 ASSERT_EQ(bridge->dispatched_events().size(), 1);
327 EXPECT_EQ(bridge->dispatched_events()[0].event_type,
329
330 ASSERT_EQ(bridge->focused_nodes().size(), 1);
331 EXPECT_EQ(bridge->focused_nodes()[0], 1);
332}
333
334TEST(AccessibilityBridgeWindows, OnAccessibilityEventIgnoredChanged) {
335 // Static test nodes with no text, hint, or scrollability are ignored.
336 ExpectWinEventFromAXEvent(4, ui::AXEventGenerator::Event::IGNORED_CHANGED,
338}
339
340TEST(AccessibilityBridgeWindows, OnAccessibilityImageAnnotationChanged) {
341 ExpectWinEventFromAXEvent(
344}
345
346TEST(AccessibilityBridgeWindows, OnAccessibilityLiveRegionChanged) {
347 ExpectWinEventFromAXEvent(1, ui::AXEventGenerator::Event::LIVE_REGION_CHANGED,
349}
350
351TEST(AccessibilityBridgeWindows, OnAccessibilityNameChanged) {
352 ExpectWinEventFromAXEvent(1, ui::AXEventGenerator::Event::NAME_CHANGED,
354}
355
361
362TEST(AccessibilityBridgeWindows, OnAccessibilityVScrollPosChanged) {
363 ExpectWinEventFromAXEvent(
366}
367
368TEST(AccessibilityBridgeWindows, OnAccessibilitySelectedChanged) {
369 ExpectWinEventFromAXEvent(1, ui::AXEventGenerator::Event::SELECTED_CHANGED,
371}
372
373TEST(AccessibilityBridgeWindows, OnAccessibilitySelectedChildrenChanged) {
374 ExpectWinEventFromAXEvent(
377}
378
379TEST(AccessibilityBridgeWindows, OnAccessibilitySubtreeCreated) {
380 ExpectWinEventFromAXEvent(0, ui::AXEventGenerator::Event::SUBTREE_CREATED,
382}
383
384TEST(AccessibilityBridgeWindows, OnAccessibilityValueChanged) {
385 ExpectWinEventFromAXEvent(1, ui::AXEventGenerator::Event::VALUE_CHANGED,
387}
388
389TEST(AccessibilityBridgeWindows, OnAccessibilityStateChanged) {
390 ExpectWinEventFromAXEvent(
393}
394
395TEST(AccessibilityBridgeWindows, OnDocumentSelectionChanged) {
396 ExpectWinEventFromAXEventOnFocusNode(
399}
400
401} // namespace testing
402} // namespace flutter
std::shared_ptr< FlutterPlatformNodeDelegateWindows > node_delegate
ax::mojom::Event event_type
void DispatchAccessibilityAction(AccessibilityNodeId target, FlutterSemanticsAction action, fml::MallocMapping data) override
Dispatch accessibility action back to the Flutter framework. These actions are generated in the nativ...
void OnAccessibilityEvent(ui::AXEventGenerator::TargetedEvent targeted_event) override
Handle accessibility events generated due to accessibility tree changes. These events are needed to b...
#define FLUTTER_API_SYMBOL(symbol)
Definition embedder.h:67
FlutterSemanticsAction
Definition embedder.h:115
@ kFlutterSemanticsActionCopy
Copy the current selection to the clipboard.
Definition embedder.h:148
@ kFlutterSemanticsActionTap
Definition embedder.h:118
FlutterEngine engine
Definition main.cc:84
FlView * view
const gchar FlBinaryMessengerMessageHandler handler
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
void MockEmbedderApiForKeyboard(EngineModifier &modifier, std::shared_ptr< MockKeyResponseController > response_controller)
TEST(NativeAssetsManagerTest, NoAvailableAssets)
constexpr int64_t kImplicitViewId
Definition ref_ptr.h:261
#define MOCK_ENGINE_PROC(proc, mock_impl)
size_t child_count
The number of children this node has.
Definition embedder.h:1663
const char * label
A textual description of the node.
Definition embedder.h:1643
FlutterSemanticsFlags * flags2
Definition embedder.h:1705
FlutterSemanticsAction action
The semantics action.
Definition embedder.h:2780