| Flutter macOS Embedder |

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
5 #import "flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMac.h"
6
7 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
8 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
9 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
10 #include "flutter/testing/autoreleasepool_test.h"
11 #include "flutter/testing/testing.h"
12
13 namespace flutter::testing {
14
15 namespace {
16
17 class AccessibilityBridgeMacSpy : public AccessibilityBridgeMac {
18 public:
19 using AccessibilityBridgeMac::OnAccessibilityEvent;
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
37 @interface AccessibilityBridgeTestViewController : FlutterViewController
38 - (std::shared_ptr<flutter::AccessibilityBridgeMac>)createAccessibilityBridgeWithEngine:
39 (nonnull FlutterEngine*)engine;
40 @end
41
42 @implementation AccessibilityBridgeTestViewController
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
49 namespace flutter::testing {
50
51 namespace {
52
53 // Returns an engine configured for the text fixture resource configuration.
54 FlutterViewController* 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
65 class 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
83 NSWindow* AccessibilityBridgeMacWindowTest::gWindow_ = nil;
84
85 // Test-specific name for AutoreleasePoolTest fixture.
86 using AccessibilityBridgeMacTest = AutoreleasePoolTest;
87
88 } // namespace
89
90 TEST_F(AccessibilityBridgeMacWindowTest, SendsAccessibilityCreateNotificationFlutterViewWindow) {
91 FlutterViewController* viewController = CreateTestViewController();
92 FlutterEngine* engine = viewController.engine;
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());
101 FlutterSemanticsNode2 root;
102 FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
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;
116 root.custom_accessibility_actions_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;
130 ui::AXEventGenerator::EventParams event_params(ui::AXEventGenerator::Event::CHILDREN_CHANGED,
131 ax::mojom::EventFrom::kNone, 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
150 TEST_F(AccessibilityBridgeMacWindowTest, NonZeroRootNodeId) {
151 FlutterViewController* viewController = CreateTestViewController();
152 FlutterEngine* engine = viewController.engine;
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
162 FlutterSemanticsNode2 node1;
163 FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
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();
180 node1.custom_accessibility_actions_count = 0;
181 node1.identifier = "";
182
183 FlutterSemanticsNode2 node2;
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;
197 node2.custom_accessibility_actions_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
221 TEST_F(AccessibilityBridgeMacTest, DoesNotSendAccessibilityCreateNotificationWhenHeadless) {
222 FlutterViewController* viewController = CreateTestViewController();
223 FlutterEngine* engine = viewController.engine;
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());
230 FlutterSemanticsNode2 root;
231 FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
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;
245 root.custom_accessibility_actions_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;
259 ui::AXEventGenerator::EventParams event_params(ui::AXEventGenerator::Event::CHILDREN_CHANGED,
260 ax::mojom::EventFrom::kNone, 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
270 TEST_F(AccessibilityBridgeMacTest, DoesNotSendAccessibilityCreateNotificationWhenNoWindow) {
271 FlutterViewController* viewController = CreateTestViewController();
272 FlutterEngine* engine = viewController.engine;
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());
279 FlutterSemanticsNode2 root;
280 FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
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;
294 root.custom_accessibility_actions_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;
308 ui::AXEventGenerator::EventParams event_params(ui::AXEventGenerator::Event::CHILDREN_CHANGED,
309 ax::mojom::EventFrom::kNone, 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
AccessibilityBridgeMac.h
actual_notifications
std::unordered_map< std::string, gfx::NativeViewAccessible > actual_notifications
Definition: AccessibilityBridgeMacTest.mm:25
FlutterDartProject_Internal.h
FlutterEngine_Internal.h
FlutterViewController_Internal.h
FlutterDartProject
Definition: FlutterDartProject.mm:39
flutter::AccessibilityBridgeMac
Definition: AccessibilityBridgeMac.h:28
flutter::AccessibilityBridgeMac::OnAccessibilityEvent
void OnAccessibilityEvent(ui::AXEventGenerator::TargetedEvent targeted_event) override
Handle accessibility events generated due to accessibility tree changes. These events are needed to b...
Definition: AccessibilityBridgeMac.mm:27
AccessibilityBridgeTestViewController
Definition: AccessibilityBridgeMacTest.mm:38
FlutterEngine
Definition: FlutterEngine.h:32
-[FlutterEngine shutDownEngine]
void shutDownEngine()
Definition: FlutterEngine.mm:1339
FlutterViewController
Definition: FlutterViewController.h:73
FlutterViewController::engine
FlutterEngine * engine
Definition: FlutterViewController.h:78
flutter::testing
Definition: AccessibilityBridgeMacTest.mm:13
flutter::testing::TEST_F
TEST_F(AccessibilityBridgeMacWindowTest, SendsAccessibilityCreateNotificationFlutterViewWindow)
Definition: AccessibilityBridgeMacTest.mm:90