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 
6 
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:
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 
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  root.actions = static_cast<FlutterSemanticsAction>(0);
106  root.text_selection_base = -1;
107  root.text_selection_extent = -1;
108  root.label = "root";
109  root.hint = "";
110  root.value = "";
111  root.increased_value = "";
112  root.decreased_value = "";
113  root.tooltip = "";
114  root.child_count = 0;
115  root.custom_accessibility_actions_count = 0;
116  bridge->AddFlutterSemanticsNodeUpdate(root);
117 
118  bridge->CommitUpdates();
119  auto platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
120 
121  // Creates a targeted event.
122  ui::AXTree tree;
123  ui::AXNode ax_node(&tree, nullptr, 0, 0);
124  ui::AXNodeData node_data;
125  node_data.id = 0;
126  ax_node.SetData(node_data);
127  std::vector<ui::AXEventIntent> intent;
128  ui::AXEventGenerator::EventParams event_params(ui::AXEventGenerator::Event::CHILDREN_CHANGED,
129  ax::mojom::EventFrom::kNone, intent);
130  ui::AXEventGenerator::TargetedEvent targeted_event(&ax_node, event_params);
131 
132  bridge->OnAccessibilityEvent(targeted_event);
133 
134  ASSERT_EQ(bridge->actual_notifications.size(), 1u);
135  auto target = bridge->actual_notifications.find([NSAccessibilityCreatedNotification UTF8String]);
136  ASSERT_NE(target, bridge->actual_notifications.end());
137  EXPECT_EQ(target->second, expectedTarget);
138  [engine shutDownEngine];
139 }
140 
141 // Flutter used to assume that the accessibility root had ID 0.
142 // In a multi-view world, each view has its own accessibility root
143 // with a globally unique node ID.
144 //
145 // node1
146 // |
147 // node2
148 TEST_F(AccessibilityBridgeMacWindowTest, NonZeroRootNodeId) {
149  FlutterViewController* viewController = CreateTestViewController();
150  FlutterEngine* engine = viewController.engine;
151  NSWindow* expectedTarget = GetWindow();
152  expectedTarget.contentView = viewController.view;
153 
154  // Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
155  // can query semantics information from.
156  engine.semanticsEnabled = YES;
157  auto bridge = std::static_pointer_cast<AccessibilityBridgeMacSpy>(
158  viewController.accessibilityBridge.lock());
159 
160  FlutterSemanticsNode2 node1;
161  FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
162  std::vector<int32_t> node1_children{2};
163  node1.id = 1;
164  node1.flags2 = &flags;
165  node1.actions = static_cast<FlutterSemanticsAction>(0);
166  node1.text_selection_base = -1;
167  node1.text_selection_extent = -1;
168  node1.label = "node1";
169  node1.hint = "";
170  node1.value = "";
171  node1.increased_value = "";
172  node1.decreased_value = "";
173  node1.tooltip = "";
174  node1.child_count = node1_children.size();
175  node1.children_in_traversal_order = node1_children.data();
176  node1.children_in_hit_test_order = node1_children.data();
177  node1.custom_accessibility_actions_count = 0;
178 
179  FlutterSemanticsNode2 node2;
180  node2.id = 2;
181  node2.flags2 = &flags;
182  node2.actions = static_cast<FlutterSemanticsAction>(0);
183  node2.text_selection_base = -1;
184  node2.text_selection_extent = -1;
185  node2.label = "node2";
186  node2.hint = "";
187  node2.value = "";
188  node2.increased_value = "";
189  node2.decreased_value = "";
190  node2.tooltip = "";
191  node2.child_count = 0;
192  node2.custom_accessibility_actions_count = 0;
193 
194  bridge->AddFlutterSemanticsNodeUpdate(node1);
195  bridge->AddFlutterSemanticsNodeUpdate(node2);
196  bridge->CommitUpdates();
197 
198  // Look up the root node delegate.
199  auto root_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
200  ASSERT_TRUE(root_delegate);
201  ASSERT_EQ(root_delegate->GetChildCount(), 1);
202 
203  // Look up the child node delegate.
204  auto child_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(2).lock();
205  ASSERT_TRUE(child_delegate);
206  ASSERT_EQ(child_delegate->GetChildCount(), 0);
207 
208  // Ensure a node with ID 0 does not exist.
209  auto invalid_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
210  ASSERT_FALSE(invalid_delegate);
211 
212  [engine shutDownEngine];
213 }
214 
215 TEST_F(AccessibilityBridgeMacTest, DoesNotSendAccessibilityCreateNotificationWhenHeadless) {
216  FlutterViewController* viewController = CreateTestViewController();
217  FlutterEngine* engine = viewController.engine;
218 
219  // Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
220  // can query semantics information from.
221  engine.semanticsEnabled = YES;
222  auto bridge = std::static_pointer_cast<AccessibilityBridgeMacSpy>(
223  viewController.accessibilityBridge.lock());
224  FlutterSemanticsNode2 root;
225  FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
226  root.id = 0;
227  root.flags2 = &flags;
228  root.actions = static_cast<FlutterSemanticsAction>(0);
229  root.text_selection_base = -1;
230  root.text_selection_extent = -1;
231  root.label = "root";
232  root.hint = "";
233  root.value = "";
234  root.increased_value = "";
235  root.decreased_value = "";
236  root.tooltip = "";
237  root.child_count = 0;
238  root.custom_accessibility_actions_count = 0;
239  bridge->AddFlutterSemanticsNodeUpdate(root);
240 
241  bridge->CommitUpdates();
242  auto platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
243 
244  // Creates a targeted event.
245  ui::AXTree tree;
246  ui::AXNode ax_node(&tree, nullptr, 0, 0);
247  ui::AXNodeData node_data;
248  node_data.id = 0;
249  ax_node.SetData(node_data);
250  std::vector<ui::AXEventIntent> intent;
251  ui::AXEventGenerator::EventParams event_params(ui::AXEventGenerator::Event::CHILDREN_CHANGED,
252  ax::mojom::EventFrom::kNone, intent);
253  ui::AXEventGenerator::TargetedEvent targeted_event(&ax_node, event_params);
254 
255  bridge->OnAccessibilityEvent(targeted_event);
256 
257  // Does not send any notification if the engine is headless.
258  EXPECT_EQ(bridge->actual_notifications.size(), 0u);
259  [engine shutDownEngine];
260 }
261 
262 TEST_F(AccessibilityBridgeMacTest, DoesNotSendAccessibilityCreateNotificationWhenNoWindow) {
263  FlutterViewController* viewController = CreateTestViewController();
264  FlutterEngine* engine = viewController.engine;
265 
266  // Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
267  // can query semantics information from.
268  engine.semanticsEnabled = YES;
269  auto bridge = std::static_pointer_cast<AccessibilityBridgeMacSpy>(
270  viewController.accessibilityBridge.lock());
271  FlutterSemanticsNode2 root;
272  FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
273  root.id = 0;
274  root.flags2 = &flags;
275  root.actions = static_cast<FlutterSemanticsAction>(0);
276  root.text_selection_base = -1;
277  root.text_selection_extent = -1;
278  root.label = "root";
279  root.hint = "";
280  root.value = "";
281  root.increased_value = "";
282  root.decreased_value = "";
283  root.tooltip = "";
284  root.child_count = 0;
285  root.custom_accessibility_actions_count = 0;
286  bridge->AddFlutterSemanticsNodeUpdate(root);
287 
288  bridge->CommitUpdates();
289  auto platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
290 
291  // Creates a targeted event.
292  ui::AXTree tree;
293  ui::AXNode ax_node(&tree, nullptr, 0, 0);
294  ui::AXNodeData node_data;
295  node_data.id = 0;
296  ax_node.SetData(node_data);
297  std::vector<ui::AXEventIntent> intent;
298  ui::AXEventGenerator::EventParams event_params(ui::AXEventGenerator::Event::CHILDREN_CHANGED,
299  ax::mojom::EventFrom::kNone, intent);
300  ui::AXEventGenerator::TargetedEvent targeted_event(&ax_node, event_params);
301 
302  bridge->OnAccessibilityEvent(targeted_event);
303 
304  // Does not send any notification if the flutter view is not attached to a NSWindow.
305  EXPECT_EQ(bridge->actual_notifications.size(), 0u);
306  [engine shutDownEngine];
307 }
308 
309 } // 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...
TEST_F(AccessibilityBridgeMacWindowTest, SendsAccessibilityCreateNotificationFlutterViewWindow)