Flutter macOS Embedder
FlutterPlatformNodeDelegateMacTest.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 #include "flutter/testing/testing.h"
5 
14 
16 #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
17 #include "flutter/third_party/accessibility/ax/ax_action_data.h"
18 
19 namespace flutter::testing {
20 
21 namespace {
22 // Returns a view controller configured for the text fixture resource configuration.
23 FlutterViewController* CreateTestViewController() {
24  NSString* fixtures = @(testing::GetFixturesPath());
25  FlutterDartProject* project = [[FlutterDartProject alloc]
26  initWithAssetsPath:fixtures
27  ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
28  return [[FlutterViewController alloc] initWithProject:project];
29 }
30 } // namespace
31 
33  FlutterViewController* viewController = CreateTestViewController();
34  FlutterEngine* engine = viewController.engine;
35  [viewController loadView];
36  engine.semanticsEnabled = YES;
37  auto bridge = viewController.accessibilityBridge.lock();
38  // Initialize ax node data.
39  FlutterSemanticsNode2 root;
40  FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
41  root.id = 0;
42  root.flags2 = &flags;
43  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
44  root.actions = static_cast<FlutterSemanticsAction>(0);
45  root.text_selection_base = -1;
46  root.text_selection_extent = -1;
47  root.label = "accessibility";
48  root.hint = "";
49  root.value = "";
50  root.increased_value = "";
51  root.decreased_value = "";
52  root.tooltip = "";
53  root.child_count = 0;
54  root.custom_accessibility_actions_count = 0;
55  root.identifier = "";
56  bridge->AddFlutterSemanticsNodeUpdate(root);
57 
58  bridge->CommitUpdates();
59 
60  auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
61  // Verify the accessibility attribute matches.
62  NSAccessibilityElement* native_accessibility =
63  root_platform_node_delegate->GetNativeViewAccessible();
64  ASSERT_NE(native_accessibility, nil);
65  EXPECT_TRUE([native_accessibility.accessibilityValue isEqualToString:@"accessibility"]);
66  EXPECT_TRUE(
67  [native_accessibility.accessibilityRole isEqualToString:NSAccessibilityStaticTextRole]);
68  EXPECT_EQ([native_accessibility.accessibilityChildren count], 0u);
69  [engine shutDownEngine];
70 }
71 
72 TEST(FlutterPlatformNodeDelegateMac, SelectableTextHasCorrectSemantics) {
73  FlutterViewController* viewController = CreateTestViewController();
74  FlutterEngine* engine = viewController.engine;
75  [viewController loadView];
76  engine.semanticsEnabled = YES;
77  auto bridge = viewController.accessibilityBridge.lock();
78  // Initialize ax node data.
79  FlutterSemanticsNode2 root;
80  FlutterSemanticsFlags flags = FlutterSemanticsFlags{.is_text_field = true, .is_read_only = true};
81  root.id = 0;
82  root.flags2 = &flags;
83  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
84  root.actions = static_cast<FlutterSemanticsAction>(0);
85  root.text_selection_base = 1;
86  root.text_selection_extent = 3;
87  root.label = "";
88  root.hint = "";
89  // Selectable text store its text in value
90  root.value = "selectable text";
91  root.increased_value = "";
92  root.decreased_value = "";
93  root.tooltip = "";
94  root.child_count = 0;
95  root.custom_accessibility_actions_count = 0;
96  root.identifier = "";
97  bridge->AddFlutterSemanticsNodeUpdate(root);
98 
99  bridge->CommitUpdates();
100 
101  auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
102  // Verify the accessibility attribute matches.
103  NSAccessibilityElement* native_accessibility =
104  root_platform_node_delegate->GetNativeViewAccessible();
105  ASSERT_NE(native_accessibility, nil);
106  EXPECT_TRUE([native_accessibility.accessibilityValue isEqualToString:@"selectable text"]);
107  EXPECT_TRUE(
108  [native_accessibility.accessibilityRole isEqualToString:NSAccessibilityStaticTextRole]);
109  EXPECT_EQ([native_accessibility.accessibilityChildren count], 0u);
110  NSRange selection = native_accessibility.accessibilitySelectedTextRange;
111  EXPECT_EQ(selection.location, 1u);
112  EXPECT_EQ(selection.length, 2u);
113  EXPECT_TRUE([native_accessibility.accessibilitySelectedText isEqualToString:@"el"]);
114 }
115 
116 TEST(FlutterPlatformNodeDelegateMac, SelectableTextWithoutSelectionReturnZeroRange) {
117  FlutterViewController* viewController = CreateTestViewController();
118  FlutterEngine* engine = viewController.engine;
119  [viewController loadView];
120  engine.semanticsEnabled = YES;
121  auto bridge = viewController.accessibilityBridge.lock();
122  // Initialize ax node data.
123  FlutterSemanticsNode2 root;
124  FlutterSemanticsFlags flags = FlutterSemanticsFlags{.is_text_field = true, .is_read_only = true};
125  root.id = 0;
126  root.flags2 = &flags;
127  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
128  root.actions = static_cast<FlutterSemanticsAction>(0);
129  root.text_selection_base = -1;
130  root.text_selection_extent = -1;
131  root.label = "";
132  root.hint = "";
133  // Selectable text store its text in value
134  root.value = "selectable text";
135  root.increased_value = "";
136  root.decreased_value = "";
137  root.tooltip = "";
138  root.child_count = 0;
139  root.custom_accessibility_actions_count = 0;
140  root.identifier = "";
141  bridge->AddFlutterSemanticsNodeUpdate(root);
142 
143  bridge->CommitUpdates();
144 
145  auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
146  // Verify the accessibility attribute matches.
147  NSAccessibilityElement* native_accessibility =
148  root_platform_node_delegate->GetNativeViewAccessible();
149  ASSERT_NE(native_accessibility, nil);
150  NSRange selection = native_accessibility.accessibilitySelectedTextRange;
151  EXPECT_TRUE(selection.location == NSNotFound);
152  EXPECT_EQ(selection.length, 0u);
153 }
154 
155 // MOCK_ENGINE_PROC is leaky by design
156 // NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
157 
159  FlutterViewController* viewController = CreateTestViewController();
160  FlutterEngine* engine = viewController.engine;
161 
162  // Attach the view to a NSWindow.
163  NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
164  styleMask:NSBorderlessWindowMask
165  backing:NSBackingStoreBuffered
166  defer:NO];
167  window.contentView = viewController.view;
168 
169  engine.semanticsEnabled = YES;
170  auto bridge = viewController.accessibilityBridge.lock();
171  // Initialize ax node data.
172  FlutterSemanticsNode2 root;
173  FlutterSemanticsFlags flags = FlutterSemanticsFlags{};
174  root.flags2 = &flags;
175  root.id = 0;
176  root.label = "root";
177  root.hint = "";
178  root.value = "";
179  root.increased_value = "";
180  root.decreased_value = "";
181  root.tooltip = "";
182  root.child_count = 1;
183  int32_t children[] = {1};
184  root.children_in_traversal_order = children;
185  root.custom_accessibility_actions_count = 0;
186  root.identifier = "";
187  bridge->AddFlutterSemanticsNodeUpdate(root);
188 
189  FlutterSemanticsNode2 child1;
190  FlutterSemanticsFlags child_flags = FlutterSemanticsFlags{};
191  child1.flags2 = &child_flags;
192  child1.id = 1;
193  child1.label = "child 1";
194  child1.hint = "";
195  child1.value = "";
196  child1.increased_value = "";
197  child1.decreased_value = "";
198  child1.tooltip = "";
199  child1.child_count = 0;
200  child1.custom_accessibility_actions_count = 0;
201  child1.identifier = "";
202  bridge->AddFlutterSemanticsNodeUpdate(child1);
203 
204  bridge->CommitUpdates();
205 
206  auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
207 
208  // Set up embedder API mock.
209  FlutterSemanticsAction called_action;
210  uint64_t called_id;
211 
212  engine.embedderAPI.DispatchSemanticsAction = MOCK_ENGINE_PROC(
213  DispatchSemanticsAction,
214  ([&called_id, &called_action](auto engine, uint64_t id, FlutterSemanticsAction action,
215  const uint8_t* data, size_t data_length) {
216  called_id = id;
217  called_action = action;
218  return kSuccess;
219  }));
220 
221  // Performs an AXAction.
222  ui::AXActionData action_data;
223  action_data.action = ax::mojom::Action::kDoDefault;
224  root_platform_node_delegate->AccessibilityPerformAction(action_data);
225 
226  EXPECT_EQ(called_action, FlutterSemanticsAction::kFlutterSemanticsActionTap);
227  EXPECT_EQ(called_id, 1u);
228 
229  [engine setViewController:nil];
230  [engine shutDownEngine];
231 }
232 
233 // NOLINTEND(clang-analyzer-core.StackAddressEscape)
234 
235 TEST(FlutterPlatformNodeDelegateMac, TextFieldUsesFlutterTextField) {
236  FlutterViewController* viewController = CreateTestViewController();
237  FlutterEngine* engine = viewController.engine;
238  [viewController loadView];
239 
240  // Unit test localization is unnecessary.
241  // NOLINTNEXTLINE(clang-analyzer-optin.osx.cocoa.localizability.NonLocalizedStringChecker)
242  engine.textInputPlugin.string = @"textfield";
243  // Creates a NSWindow so that the native text field can become first responder.
244  NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
245  styleMask:NSBorderlessWindowMask
246  backing:NSBackingStoreBuffered
247  defer:NO];
248  window.contentView = viewController.view;
249  engine.semanticsEnabled = YES;
250 
251  auto bridge = viewController.accessibilityBridge.lock();
252  // Initialize ax node data.
253  FlutterSemanticsNode2 root;
254  FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
255  FlutterSemanticsFlags child_flags = FlutterSemanticsFlags{.is_text_field = true};
256  root.id = 0;
257  root.flags2 = &flags;
258  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
259  root.actions = static_cast<FlutterSemanticsAction>(0);
260  root.label = "root";
261  root.hint = "";
262  root.value = "";
263  root.increased_value = "";
264  root.decreased_value = "";
265  root.tooltip = "";
266  root.child_count = 1;
267  int32_t children[] = {1};
268  root.children_in_traversal_order = children;
269  root.custom_accessibility_actions_count = 0;
270  root.identifier = "";
271  root.rect = {0, 0, 100, 100}; // LTRB
272  root.transform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
273  bridge->AddFlutterSemanticsNodeUpdate(root);
274 
275  double rectSize = 50;
276  double transformFactor = 0.5;
277 
278  FlutterSemanticsNode2 child1;
279  child1.id = 1;
280  child1.flags2 = &child_flags;
281  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
282  child1.actions = static_cast<FlutterSemanticsAction>(0);
283  child1.label = "";
284  child1.hint = "";
285  child1.value = "textfield";
286  child1.increased_value = "";
287  child1.decreased_value = "";
288  child1.tooltip = "";
289  child1.text_selection_base = -1;
290  child1.text_selection_extent = -1;
291  child1.child_count = 0;
292  child1.custom_accessibility_actions_count = 0;
293  child1.identifier = "";
294  child1.rect = {0, 0, rectSize, rectSize}; // LTRB
295  child1.transform = {transformFactor, 0, 0, 0, transformFactor, 0, 0, 0, 1};
296  bridge->AddFlutterSemanticsNodeUpdate(child1);
297 
298  bridge->CommitUpdates();
299 
300  auto child_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
301  // Verify the accessibility attribute matches.
302  id native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
303  EXPECT_EQ([native_accessibility isKindOfClass:[FlutterTextField class]], YES);
304  FlutterTextField* native_text_field = (FlutterTextField*)native_accessibility;
305 
306  NSView* view = viewController.flutterView;
307  CGRect scaledBounds = [view convertRectToBacking:view.bounds];
308  CGSize scaledSize = scaledBounds.size;
309  double pixelRatio = view.bounds.size.width == 0 ? 1 : scaledSize.width / view.bounds.size.width;
310 
311  double expectedFrameSize = rectSize * transformFactor / pixelRatio;
312  EXPECT_EQ(NSEqualRects(native_text_field.frame, NSMakeRect(0, 600 - expectedFrameSize,
313  expectedFrameSize, expectedFrameSize)),
314  YES);
315 
316  [native_text_field startEditing];
317  EXPECT_EQ([native_text_field.stringValue isEqualToString:@"textfield"], YES);
318 }
319 
320 // A disabled (but otherwise editable) text field must be exposed as static text
321 // rather than a `FlutterTextField`. This exercises the `!IsReadOnlyOrDisabled()`
322 // guard in `Init`: the `GetData()` override rewrites the role to `kStaticText`,
323 // but the node retains its `kEditableRoot` attribute, so `GetData().IsTextField()`
324 // still returns true. The restriction check is what keeps a disabled field from
325 // being instantiated as an editable `FlutterTextField`.
326 TEST(FlutterPlatformNodeDelegateMac, DisabledTextFieldDoesNotUseFlutterTextField) {
327  FlutterViewController* viewController = CreateTestViewController();
328  FlutterEngine* engine = viewController.engine;
329  [viewController loadView];
330 
331  // Creates a NSWindow so that the native accessibility element has a hosting view.
332  NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
333  styleMask:NSBorderlessWindowMask
334  backing:NSBackingStoreBuffered
335  defer:NO];
336  window.contentView = viewController.view;
337  engine.semanticsEnabled = YES;
338 
339  auto bridge = viewController.accessibilityBridge.lock();
340  // Initialize ax node data.
341  FlutterSemanticsNode2 root = {};
342  FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
343  // A text field that is editable (not read-only) but disabled.
344  FlutterSemanticsFlags child_flags =
345  FlutterSemanticsFlags{.is_enabled = FlutterTristate::kFlutterTristateFalse,
346  .is_text_field = true,
347  .is_read_only = false};
348  root.id = 0;
349  root.flags2 = &flags;
350  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
351  root.actions = static_cast<FlutterSemanticsAction>(0);
352  root.label = "root";
353  root.hint = "";
354  root.value = "";
355  root.increased_value = "";
356  root.decreased_value = "";
357  root.tooltip = "";
358  root.child_count = 1;
359  int32_t children[] = {1};
360  root.children_in_traversal_order = children;
361  root.custom_accessibility_actions_count = 0;
362  root.identifier = "";
363  root.rect = {0, 0, 100, 100}; // LTRB
364  root.transform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
365  bridge->AddFlutterSemanticsNodeUpdate(root);
366 
367  FlutterSemanticsNode2 child1 = {};
368  child1.id = 1;
369  child1.flags2 = &child_flags;
370  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
371  child1.actions = static_cast<FlutterSemanticsAction>(0);
372  child1.label = "";
373  child1.hint = "";
374  child1.value = "disabled textfield";
375  child1.increased_value = "";
376  child1.decreased_value = "";
377  child1.tooltip = "";
378  child1.text_selection_base = -1;
379  child1.text_selection_extent = -1;
380  child1.child_count = 0;
381  child1.custom_accessibility_actions_count = 0;
382  child1.identifier = "";
383  child1.rect = {0, 0, 50, 50}; // LTRB
384  child1.transform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
385  bridge->AddFlutterSemanticsNodeUpdate(child1);
386 
387  bridge->CommitUpdates();
388 
389  auto child_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
390  // The disabled text field must not be backed by a `FlutterTextField`.
391  id native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
392  EXPECT_FALSE([native_accessibility isKindOfClass:[FlutterTextField class]]);
393  EXPECT_TRUE(
394  [[native_accessibility accessibilityRole] isEqualToString:NSAccessibilityStaticTextRole]);
395 
396  [engine shutDownEngine];
397 }
398 
399 TEST(FlutterPlatformNodeDelegateMac, ChangingFlagsUpdatesNativeViewAccessible) {
400  FlutterViewController* viewController = CreateTestViewController();
401  FlutterEngine* engine = viewController.engine;
402  [viewController loadView];
403 
404  // Creates a NSWindow so that the native text field can become first responder.
405  NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
406  styleMask:NSBorderlessWindowMask
407  backing:NSBackingStoreBuffered
408  defer:NO];
409  window.contentView = viewController.view;
410  engine.semanticsEnabled = YES;
411 
412  auto bridge = viewController.accessibilityBridge.lock();
413  // Initialize ax node data.
414  FlutterSemanticsNode2 root;
415  root.id = 0;
416  FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
417  root.flags2 = &flags;
418  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
419  root.actions = static_cast<FlutterSemanticsAction>(0);
420  root.label = "root";
421  root.hint = "";
422  root.value = "";
423  root.increased_value = "";
424  root.decreased_value = "";
425  root.tooltip = "";
426  root.child_count = 1;
427  int32_t children[] = {1};
428  root.children_in_traversal_order = children;
429  root.custom_accessibility_actions_count = 0;
430  root.identifier = "";
431  root.rect = {0, 0, 100, 100}; // LTRB
432  root.transform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
433  bridge->AddFlutterSemanticsNodeUpdate(root);
434 
435  double rectSize = 50;
436  double transformFactor = 0.5;
437 
438  FlutterSemanticsNode2 child1;
439  FlutterSemanticsFlags child_flags = FlutterSemanticsFlags{0};
440  child1.flags2 = &child_flags;
441  child1.id = 1;
442  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
443  child1.actions = static_cast<FlutterSemanticsAction>(0);
444  child1.label = "";
445  child1.hint = "";
446  child1.value = "textfield";
447  child1.increased_value = "";
448  child1.decreased_value = "";
449  child1.tooltip = "";
450  child1.text_selection_base = -1;
451  child1.text_selection_extent = -1;
452  child1.child_count = 0;
453  child1.custom_accessibility_actions_count = 0;
454  child1.identifier = "";
455  child1.rect = {0, 0, rectSize, rectSize}; // LTRB
456  child1.transform = {transformFactor, 0, 0, 0, transformFactor, 0, 0, 0, 1};
457  bridge->AddFlutterSemanticsNodeUpdate(child1);
458 
459  bridge->CommitUpdates();
460 
461  auto child_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
462  // Verify the accessibility attribute matches.
463  id native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
464  EXPECT_TRUE([[native_accessibility className] isEqualToString:@"AXPlatformNodeCocoa"]);
465 
466  // Converting child to text field should produce `FlutterTextField` native view accessible.
467 
468  FlutterSemanticsFlags child_flags_updated_1 = FlutterSemanticsFlags{.is_text_field = true};
469  child1.flags2 = &child_flags_updated_1;
470  bridge->AddFlutterSemanticsNodeUpdate(child1);
471  bridge->CommitUpdates();
472 
473  native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
474  EXPECT_TRUE([native_accessibility isKindOfClass:[FlutterTextField class]]);
475 
476  FlutterSemanticsFlags child_flags_updated_2 = FlutterSemanticsFlags{.is_text_field = false};
477  child1.flags2 = &child_flags_updated_2;
478  bridge->AddFlutterSemanticsNodeUpdate(child1);
479  bridge->CommitUpdates();
480 
481  native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
482  EXPECT_TRUE([[native_accessibility className] isEqualToString:@"AXPlatformNodeCocoa"]);
483 }
484 
485 } // namespace flutter::testing
TEST(FlutterAppDelegateTest, DoesNotCallDelegatesWithoutHandler)