Flutter Engine
 
Loading...
Searching...
No Matches
AccessibilityBridgeMacTest.mm
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
12
13namespace flutter::testing {
14
15namespace {
16
17class AccessibilityBridgeMacSpy : public AccessibilityBridgeMac {
18 public:
20
21 AccessibilityBridgeMacSpy(__weak FlutterEngine* flutter_engine,
22 __weak FlutterViewController* view_controller)
23 : AccessibilityBridgeMac(flutter_engine, view_controller) {}
24
25 std::unordered_map<std::string, gfx::NativeViewAccessible> actual_notifications;
26
27 private:
28 void DispatchMacOSNotification(gfx::NativeViewAccessible native_node,
29 NSAccessibilityNotificationName mac_notification) override {
30 actual_notifications[[mac_notification UTF8String]] = native_node;
31 }
32};
33
34} // namespace
35} // namespace flutter::testing
36
38- (std::shared_ptr<flutter::AccessibilityBridgeMac>)createAccessibilityBridgeWithEngine:
39 (nonnull FlutterEngine*)engine;
40@end
41
43- (std::shared_ptr<flutter::AccessibilityBridgeMac>)createAccessibilityBridgeWithEngine:
44 (nonnull FlutterEngine*)engine {
45 return std::make_shared<flutter::testing::AccessibilityBridgeMacSpy>(engine, self);
46}
47@end
48
49namespace flutter::testing {
50
51namespace {
52
53// Returns an engine configured for the text fixture resource configuration.
54FlutterViewController* CreateTestViewController() {
55 NSString* fixtures = @(testing::GetFixturesPath());
56 FlutterDartProject* project = [[FlutterDartProject alloc]
57 initWithAssetsPath:fixtures
58 ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
59 return [[AccessibilityBridgeTestViewController alloc] initWithProject:project];
60}
61
62// Test fixture that instantiates and re-uses a single NSWindow across multiple tests.
63//
64// Works around: http://www.openradar.me/FB13291861
65class AccessibilityBridgeMacWindowTest : public AutoreleasePoolTest {
66 public:
67 AccessibilityBridgeMacWindowTest() {
68 if (!gWindow_) {
69 gWindow_ = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
70 styleMask:NSBorderlessWindowMask
71 backing:NSBackingStoreBuffered
72 defer:NO];
73 }
74 }
75
76 NSWindow* GetWindow() const { return gWindow_; }
77
78 private:
79 static NSWindow* gWindow_;
80 FML_DISALLOW_COPY_AND_ASSIGN(AccessibilityBridgeMacWindowTest);
81};
82
83NSWindow* AccessibilityBridgeMacWindowTest::gWindow_ = nil;
84
85// Test-specific name for AutoreleasePoolTest fixture.
86using AccessibilityBridgeMacTest = AutoreleasePoolTest;
87
88} // namespace
89
90TEST_F(AccessibilityBridgeMacWindowTest, SendsAccessibilityCreateNotificationFlutterViewWindow) {
91 FlutterViewController* viewController = CreateTestViewController();
93 NSWindow* expectedTarget = GetWindow();
94 expectedTarget.contentView = viewController.view;
95
96 // Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
97 // can query semantics information from.
98 engine.semanticsEnabled = YES;
99 auto bridge = std::static_pointer_cast<AccessibilityBridgeMacSpy>(
100 viewController.accessibilityBridge.lock());
103 root.id = 0;
104 root.flags2 = &flags;
105 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
106 root.actions = static_cast<FlutterSemanticsAction>(0);
107 root.text_selection_base = -1;
108 root.text_selection_extent = -1;
109 root.label = "root";
110 root.hint = "";
111 root.value = "";
112 root.increased_value = "";
113 root.decreased_value = "";
114 root.tooltip = "";
115 root.child_count = 0;
117 root.identifier = "";
118 bridge->AddFlutterSemanticsNodeUpdate(root);
119
120 bridge->CommitUpdates();
121 auto platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
122
123 // Creates a targeted event.
124 ui::AXTree tree;
125 ui::AXNode ax_node(&tree, nullptr, 0, 0);
126 ui::AXNodeData node_data;
127 node_data.id = 0;
128 ax_node.SetData(node_data);
129 std::vector<ui::AXEventIntent> intent;
132 ui::AXEventGenerator::TargetedEvent targeted_event(&ax_node, event_params);
133
134 bridge->OnAccessibilityEvent(targeted_event);
135
136 ASSERT_EQ(bridge->actual_notifications.size(), 1u);
137 auto target = bridge->actual_notifications.find([NSAccessibilityCreatedNotification UTF8String]);
138 ASSERT_NE(target, bridge->actual_notifications.end());
139 EXPECT_EQ(target->second, expectedTarget);
140 [engine shutDownEngine];
141}
142
143// Flutter used to assume that the accessibility root had ID 0.
144// In a multi-view world, each view has its own accessibility root
145// with a globally unique node ID.
146//
147// node1
148// |
149// node2
150TEST_F(AccessibilityBridgeMacWindowTest, NonZeroRootNodeId) {
151 FlutterViewController* viewController = CreateTestViewController();
153 NSWindow* expectedTarget = GetWindow();
154 expectedTarget.contentView = viewController.view;
155
156 // Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
157 // can query semantics information from.
158 engine.semanticsEnabled = YES;
159 auto bridge = std::static_pointer_cast<AccessibilityBridgeMacSpy>(
160 viewController.accessibilityBridge.lock());
161
164 std::vector<int32_t> node1_children{2};
165 node1.id = 1;
166 node1.flags2 = &flags;
167 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
168 node1.actions = static_cast<FlutterSemanticsAction>(0);
169 node1.text_selection_base = -1;
170 node1.text_selection_extent = -1;
171 node1.label = "node1";
172 node1.hint = "";
173 node1.value = "";
174 node1.increased_value = "";
175 node1.decreased_value = "";
176 node1.tooltip = "";
177 node1.child_count = node1_children.size();
178 node1.children_in_traversal_order = node1_children.data();
179 node1.children_in_hit_test_order = node1_children.data();
181 node1.identifier = "";
182
184 node2.id = 2;
185 node2.flags2 = &flags;
186 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
187 node2.actions = static_cast<FlutterSemanticsAction>(0);
188 node2.text_selection_base = -1;
189 node2.text_selection_extent = -1;
190 node2.label = "node2";
191 node2.hint = "";
192 node2.value = "";
193 node2.increased_value = "";
194 node2.decreased_value = "";
195 node2.tooltip = "";
196 node2.child_count = 0;
198 node2.identifier = "";
199
200 bridge->AddFlutterSemanticsNodeUpdate(node1);
201 bridge->AddFlutterSemanticsNodeUpdate(node2);
202 bridge->CommitUpdates();
203
204 // Look up the root node delegate.
205 auto root_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
206 ASSERT_TRUE(root_delegate);
207 ASSERT_EQ(root_delegate->GetChildCount(), 1);
208
209 // Look up the child node delegate.
210 auto child_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(2).lock();
211 ASSERT_TRUE(child_delegate);
212 ASSERT_EQ(child_delegate->GetChildCount(), 0);
213
214 // Ensure a node with ID 0 does not exist.
215 auto invalid_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
216 ASSERT_FALSE(invalid_delegate);
217
218 [engine shutDownEngine];
219}
220
221TEST_F(AccessibilityBridgeMacTest, DoesNotSendAccessibilityCreateNotificationWhenHeadless) {
222 FlutterViewController* viewController = CreateTestViewController();
224
225 // Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
226 // can query semantics information from.
227 engine.semanticsEnabled = YES;
228 auto bridge = std::static_pointer_cast<AccessibilityBridgeMacSpy>(
229 viewController.accessibilityBridge.lock());
232 root.id = 0;
233 root.flags2 = &flags;
234 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
235 root.actions = static_cast<FlutterSemanticsAction>(0);
236 root.text_selection_base = -1;
237 root.text_selection_extent = -1;
238 root.label = "root";
239 root.hint = "";
240 root.value = "";
241 root.increased_value = "";
242 root.decreased_value = "";
243 root.tooltip = "";
244 root.child_count = 0;
246 root.identifier = "";
247 bridge->AddFlutterSemanticsNodeUpdate(root);
248
249 bridge->CommitUpdates();
250 auto platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
251
252 // Creates a targeted event.
253 ui::AXTree tree;
254 ui::AXNode ax_node(&tree, nullptr, 0, 0);
255 ui::AXNodeData node_data;
256 node_data.id = 0;
257 ax_node.SetData(node_data);
258 std::vector<ui::AXEventIntent> intent;
261 ui::AXEventGenerator::TargetedEvent targeted_event(&ax_node, event_params);
262
263 bridge->OnAccessibilityEvent(targeted_event);
264
265 // Does not send any notification if the engine is headless.
266 EXPECT_EQ(bridge->actual_notifications.size(), 0u);
267 [engine shutDownEngine];
268}
269
270TEST_F(AccessibilityBridgeMacTest, DoesNotSendAccessibilityCreateNotificationWhenNoWindow) {
271 FlutterViewController* viewController = CreateTestViewController();
273
274 // Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
275 // can query semantics information from.
276 engine.semanticsEnabled = YES;
277 auto bridge = std::static_pointer_cast<AccessibilityBridgeMacSpy>(
278 viewController.accessibilityBridge.lock());
281 root.id = 0;
282 root.flags2 = &flags;
283 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
284 root.actions = static_cast<FlutterSemanticsAction>(0);
285 root.text_selection_base = -1;
286 root.text_selection_extent = -1;
287 root.label = "root";
288 root.hint = "";
289 root.value = "";
290 root.increased_value = "";
291 root.decreased_value = "";
292 root.tooltip = "";
293 root.child_count = 0;
295 root.identifier = "";
296 bridge->AddFlutterSemanticsNodeUpdate(root);
297
298 bridge->CommitUpdates();
299 auto platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
300
301 // Creates a targeted event.
302 ui::AXTree tree;
303 ui::AXNode ax_node(&tree, nullptr, 0, 0);
304 ui::AXNodeData node_data;
305 node_data.id = 0;
306 ax_node.SetData(node_data);
307 std::vector<ui::AXEventIntent> intent;
310 ui::AXEventGenerator::TargetedEvent targeted_event(&ax_node, event_params);
311
312 bridge->OnAccessibilityEvent(targeted_event);
313
314 // Does not send any notification if the flutter view is not attached to a NSWindow.
315 EXPECT_EQ(bridge->actual_notifications.size(), 0u);
316 [engine shutDownEngine];
317}
318
319} // namespace flutter::testing
std::unordered_map< std::string, gfx::NativeViewAccessible > actual_notifications
void OnAccessibilityEvent(ui::AXEventGenerator::TargetedEvent targeted_event) override
Handle accessibility events generated due to accessibility tree changes. These events are needed to b...
void SetData(const AXNodeData &src)
Definition ax_node.cc:373
FlutterSemanticsAction
Definition embedder.h:115
FlutterEngine engine
Definition main.cc:84
uint32_t * target
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
FlutterViewController * viewController
TEST_F(DisplayListTest, Defaults)
const char * GetFixturesPath()
Returns the directory containing the test fixture for the target if this target has fixtures configur...
UnimplementedNativeViewAccessible * NativeViewAccessible
Definition ref_ptr.h:261
const char * identifier
Definition embedder.h:1714
const char * increased_value
Definition embedder.h:1650
const char * tooltip
A textual tooltip attached to the node.
Definition embedder.h:1677
size_t custom_accessibility_actions_count
The number of custom accessibility action associated with this node.
Definition embedder.h:1669
const int32_t * children_in_traversal_order
Array of child node IDs in traversal order. Has length child_count.
Definition embedder.h:1665
const int32_t * children_in_hit_test_order
Array of child node IDs in hit test order. Has length child_count.
Definition embedder.h:1667
int32_t text_selection_extent
The position at which the text selection terminates.
Definition embedder.h:1625
FlutterSemanticsAction actions
The set of semantics actions applicable to this node.
Definition embedder.h:1621
int32_t id
The unique identifier for this node.
Definition embedder.h:1613
size_t child_count
The number of children this node has.
Definition embedder.h:1663
const char * decreased_value
Definition embedder.h:1653
const char * label
A textual description of the node.
Definition embedder.h:1643
int32_t text_selection_base
The position at which the text selection originates.
Definition embedder.h:1623
const char * hint
A brief description of the result of performing an action on the node.
Definition embedder.h:1645
FlutterSemanticsFlags * flags2
Definition embedder.h:1705
const char * value
A textual description of the current value of the node.
Definition embedder.h:1647