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  actual_notifications([NSMutableDictionary dictionary]) {}
25 
26  NSMutableDictionary<NSAccessibilityNotificationName, id>* actual_notifications;
27 
28  private:
29  void DispatchMacOSNotification(gfx::NativeViewAccessible native_node,
30  NSAccessibilityNotificationName mac_notification) override {
31  actual_notifications[mac_notification] = native_node;
32  }
33 };
34 
35 } // namespace
36 } // namespace flutter::testing
37 
39 - (std::shared_ptr<flutter::AccessibilityBridgeMac>)createAccessibilityBridgeWithEngine:
40  (nonnull FlutterEngine*)engine;
41 @end
42 
44 - (std::shared_ptr<flutter::AccessibilityBridgeMac>)createAccessibilityBridgeWithEngine:
45  (nonnull FlutterEngine*)engine {
46  return std::make_shared<flutter::testing::AccessibilityBridgeMacSpy>(engine, self);
47 }
48 @end
49 
50 namespace flutter::testing {
51 
52 namespace {
53 
54 // Returns an engine configured for the text fixture resource configuration.
55 FlutterViewController* CreateTestViewController() {
56  NSString* fixtures = @(testing::GetFixturesPath());
57  FlutterDartProject* project = [[FlutterDartProject alloc]
58  initWithAssetsPath:fixtures
59  ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
60  return [[AccessibilityBridgeTestViewController alloc] initWithProject:project];
61 }
62 
63 // Test fixture that instantiates and re-uses a single NSWindow across multiple tests.
64 //
65 // Works around: http://www.openradar.me/FB13291861
66 class AccessibilityBridgeMacWindowTest : public AutoreleasePoolTest {
67  public:
68  AccessibilityBridgeMacWindowTest() {
69  if (!gWindow_) {
70  gWindow_ = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
71  styleMask:NSBorderlessWindowMask
72  backing:NSBackingStoreBuffered
73  defer:NO];
74  }
75  }
76 
77  NSWindow* GetWindow() const { return gWindow_; }
78 
79  private:
80  static NSWindow* gWindow_;
81  FML_DISALLOW_COPY_AND_ASSIGN(AccessibilityBridgeMacWindowTest);
82 };
83 
84 NSWindow* AccessibilityBridgeMacWindowTest::gWindow_ = nil;
85 
86 // Test-specific name for AutoreleasePoolTest fixture.
87 using AccessibilityBridgeMacTest = AutoreleasePoolTest;
88 
89 } // namespace
90 
91 TEST_F(AccessibilityBridgeMacWindowTest, SendsAccessibilityCreateNotificationFlutterViewWindow) {
92  FlutterViewController* viewController = CreateTestViewController();
93  FlutterEngine* engine = viewController.engine;
94  NSWindow* expectedTarget = GetWindow();
95  expectedTarget.contentView = viewController.view;
96 
97  // Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
98  // can query semantics information from.
99  engine.semanticsEnabled = YES;
100  auto bridge = std::static_pointer_cast<AccessibilityBridgeMacSpy>(
101  viewController.accessibilityBridge.lock());
102  FlutterSemanticsNode2 root;
103  FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
104  root.id = 0;
105  root.flags2 = &flags;
106  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
107  root.actions = static_cast<FlutterSemanticsAction>(0);
108  root.text_selection_base = -1;
109  root.text_selection_extent = -1;
110  root.label = "root";
111  root.hint = "";
112  root.value = "";
113  root.increased_value = "";
114  root.decreased_value = "";
115  root.tooltip = "";
116  root.child_count = 0;
117  root.custom_accessibility_actions_count = 0;
118  root.identifier = "";
119  bridge->AddFlutterSemanticsNodeUpdate(root);
120 
121  bridge->CommitUpdates();
122  auto platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
123 
124  // Creates a targeted event.
125  ui::AXTree tree;
126  ui::AXNode ax_node(&tree, nullptr, 0, 0);
127  ui::AXNodeData node_data;
128  node_data.id = 0;
129  ax_node.SetData(node_data);
130  std::vector<ui::AXEventIntent> intent;
131  ui::AXEventGenerator::EventParams event_params(ui::AXEventGenerator::Event::CHILDREN_CHANGED,
132  ax::mojom::EventFrom::kNone, intent);
133  ui::AXEventGenerator::TargetedEvent targeted_event(&ax_node, event_params);
134 
135  bridge->OnAccessibilityEvent(targeted_event);
136 
137  ASSERT_EQ(bridge->actual_notifications.count, 1u);
138  id target = bridge->actual_notifications[NSAccessibilityCreatedNotification];
139  ASSERT_NE(target, nil);
140  EXPECT_EQ(target, expectedTarget);
141  [engine shutDownEngine];
142 }
143 
144 // Flutter used to assume that the accessibility root had ID 0.
145 // In a multi-view world, each view has its own accessibility root
146 // with a globally unique node ID.
147 //
148 // node1
149 // |
150 // node2
151 TEST_F(AccessibilityBridgeMacWindowTest, NonZeroRootNodeId) {
152  FlutterViewController* viewController = CreateTestViewController();
153  FlutterEngine* engine = viewController.engine;
154  NSWindow* expectedTarget = GetWindow();
155  expectedTarget.contentView = viewController.view;
156 
157  // Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
158  // can query semantics information from.
159  engine.semanticsEnabled = YES;
160  auto bridge = std::static_pointer_cast<AccessibilityBridgeMacSpy>(
161  viewController.accessibilityBridge.lock());
162 
163  FlutterSemanticsNode2 node1;
164  FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
165  std::vector<int32_t> node1_children{2};
166  node1.id = 1;
167  node1.flags2 = &flags;
168  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
169  node1.actions = static_cast<FlutterSemanticsAction>(0);
170  node1.text_selection_base = -1;
171  node1.text_selection_extent = -1;
172  node1.label = "node1";
173  node1.hint = "";
174  node1.value = "";
175  node1.increased_value = "";
176  node1.decreased_value = "";
177  node1.tooltip = "";
178  node1.child_count = node1_children.size();
179  node1.children_in_traversal_order = node1_children.data();
180  node1.children_in_hit_test_order = node1_children.data();
181  node1.custom_accessibility_actions_count = 0;
182  node1.identifier = "";
183 
184  FlutterSemanticsNode2 node2;
185  node2.id = 2;
186  node2.flags2 = &flags;
187  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
188  node2.actions = static_cast<FlutterSemanticsAction>(0);
189  node2.text_selection_base = -1;
190  node2.text_selection_extent = -1;
191  node2.label = "node2";
192  node2.hint = "";
193  node2.value = "";
194  node2.increased_value = "";
195  node2.decreased_value = "";
196  node2.tooltip = "";
197  node2.child_count = 0;
198  node2.custom_accessibility_actions_count = 0;
199  node2.identifier = "";
200 
201  bridge->AddFlutterSemanticsNodeUpdate(node1);
202  bridge->AddFlutterSemanticsNodeUpdate(node2);
203  bridge->CommitUpdates();
204 
205  // Look up the root node delegate.
206  auto root_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
207  ASSERT_TRUE(root_delegate);
208  ASSERT_EQ(root_delegate->GetChildCount(), 1);
209 
210  // Look up the child node delegate.
211  auto child_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(2).lock();
212  ASSERT_TRUE(child_delegate);
213  ASSERT_EQ(child_delegate->GetChildCount(), 0);
214 
215  // Ensure a node with ID 0 does not exist.
216  auto invalid_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
217  ASSERT_FALSE(invalid_delegate);
218 
219  [engine shutDownEngine];
220 }
221 
222 TEST_F(AccessibilityBridgeMacTest, DoesNotSendAccessibilityCreateNotificationWhenHeadless) {
223  FlutterViewController* viewController = CreateTestViewController();
224  FlutterEngine* engine = viewController.engine;
225 
226  // Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
227  // can query semantics information from.
228  engine.semanticsEnabled = YES;
229  auto bridge = std::static_pointer_cast<AccessibilityBridgeMacSpy>(
230  viewController.accessibilityBridge.lock());
231  FlutterSemanticsNode2 root;
232  FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
233  root.id = 0;
234  root.flags2 = &flags;
235  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
236  root.actions = static_cast<FlutterSemanticsAction>(0);
237  root.text_selection_base = -1;
238  root.text_selection_extent = -1;
239  root.label = "root";
240  root.hint = "";
241  root.value = "";
242  root.increased_value = "";
243  root.decreased_value = "";
244  root.tooltip = "";
245  root.child_count = 0;
246  root.custom_accessibility_actions_count = 0;
247  root.identifier = "";
248  bridge->AddFlutterSemanticsNodeUpdate(root);
249 
250  bridge->CommitUpdates();
251  auto platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
252 
253  // Creates a targeted event.
254  ui::AXTree tree;
255  ui::AXNode ax_node(&tree, nullptr, 0, 0);
256  ui::AXNodeData node_data;
257  node_data.id = 0;
258  ax_node.SetData(node_data);
259  std::vector<ui::AXEventIntent> intent;
260  ui::AXEventGenerator::EventParams event_params(ui::AXEventGenerator::Event::CHILDREN_CHANGED,
261  ax::mojom::EventFrom::kNone, intent);
262  ui::AXEventGenerator::TargetedEvent targeted_event(&ax_node, event_params);
263 
264  bridge->OnAccessibilityEvent(targeted_event);
265 
266  // Does not send any notification if the engine is headless.
267  EXPECT_EQ(bridge->actual_notifications.count, 0u);
268  [engine shutDownEngine];
269 }
270 
271 TEST_F(AccessibilityBridgeMacTest, DoesNotSendAccessibilityCreateNotificationWhenNoWindow) {
272  FlutterViewController* viewController = CreateTestViewController();
273  FlutterEngine* engine = viewController.engine;
274 
275  // Setting up bridge so that the AccessibilityBridgeMacDelegateSpy
276  // can query semantics information from.
277  engine.semanticsEnabled = YES;
278  auto bridge = std::static_pointer_cast<AccessibilityBridgeMacSpy>(
279  viewController.accessibilityBridge.lock());
280  FlutterSemanticsNode2 root;
281  FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
282  root.id = 0;
283  root.flags2 = &flags;
284  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
285  root.actions = static_cast<FlutterSemanticsAction>(0);
286  root.text_selection_base = -1;
287  root.text_selection_extent = -1;
288  root.label = "root";
289  root.hint = "";
290  root.value = "";
291  root.increased_value = "";
292  root.decreased_value = "";
293  root.tooltip = "";
294  root.child_count = 0;
295  root.custom_accessibility_actions_count = 0;
296  root.identifier = "";
297  bridge->AddFlutterSemanticsNodeUpdate(root);
298 
299  bridge->CommitUpdates();
300  auto platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
301 
302  // Creates a targeted event.
303  ui::AXTree tree;
304  ui::AXNode ax_node(&tree, nullptr, 0, 0);
305  ui::AXNodeData node_data;
306  node_data.id = 0;
307  ax_node.SetData(node_data);
308  std::vector<ui::AXEventIntent> intent;
309  ui::AXEventGenerator::EventParams event_params(ui::AXEventGenerator::Event::CHILDREN_CHANGED,
310  ax::mojom::EventFrom::kNone, intent);
311  ui::AXEventGenerator::TargetedEvent targeted_event(&ax_node, event_params);
312 
313  bridge->OnAccessibilityEvent(targeted_event);
314 
315  // Does not send any notification if the flutter view is not attached to a NSWindow.
316  EXPECT_EQ(bridge->actual_notifications.count, 0u);
317  [engine shutDownEngine];
318 }
319 
320 } // namespace flutter::testing
NSMutableDictionary< NSAccessibilityNotificationName, id > * 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)