4 #include "flutter/testing/testing.h"
16 #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
17 #include "flutter/third_party/accessibility/ax/ax_action_data.h"
24 NSString* fixtures = @(testing::GetFixturesPath());
26 initWithAssetsPath:fixtures
27 ICUDataPath:[fixtures stringByAppendingString:
@"/icudtl.dat"]];
35 [viewController loadView];
36 engine.semanticsEnabled = YES;
37 auto bridge = viewController.accessibilityBridge.lock();
39 FlutterSemanticsNode2 root;
40 FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
44 root.actions =
static_cast<FlutterSemanticsAction
>(0);
45 root.text_selection_base = -1;
46 root.text_selection_extent = -1;
47 root.label =
"accessibility";
50 root.increased_value =
"";
51 root.decreased_value =
"";
54 root.custom_accessibility_actions_count = 0;
56 bridge->AddFlutterSemanticsNodeUpdate(root);
58 bridge->CommitUpdates();
60 auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
62 NSAccessibilityElement* native_accessibility =
63 root_platform_node_delegate->GetNativeViewAccessible();
64 ASSERT_NE(native_accessibility, nil);
65 EXPECT_TRUE([native_accessibility.accessibilityValue isEqualToString:
@"accessibility"]);
67 [native_accessibility.accessibilityRole isEqualToString:NSAccessibilityStaticTextRole]);
68 EXPECT_EQ([native_accessibility.accessibilityChildren count], 0u);
69 [engine shutDownEngine];
75 [viewController loadView];
76 engine.semanticsEnabled = YES;
77 auto bridge = viewController.accessibilityBridge.lock();
79 FlutterSemanticsNode2 root;
80 FlutterSemanticsFlags flags = FlutterSemanticsFlags{.is_text_field =
true, .is_read_only =
true};
84 root.actions =
static_cast<FlutterSemanticsAction
>(0);
85 root.text_selection_base = 1;
86 root.text_selection_extent = 3;
90 root.value =
"selectable text";
91 root.increased_value =
"";
92 root.decreased_value =
"";
95 root.custom_accessibility_actions_count = 0;
97 bridge->AddFlutterSemanticsNodeUpdate(root);
99 bridge->CommitUpdates();
101 auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
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"]);
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"]);
119 [viewController loadView];
120 engine.semanticsEnabled = YES;
121 auto bridge = viewController.accessibilityBridge.lock();
123 FlutterSemanticsNode2 root;
124 FlutterSemanticsFlags flags = FlutterSemanticsFlags{.is_text_field =
true, .is_read_only =
true};
126 root.flags2 = &flags;
128 root.actions =
static_cast<FlutterSemanticsAction
>(0);
129 root.text_selection_base = -1;
130 root.text_selection_extent = -1;
134 root.value =
"selectable text";
135 root.increased_value =
"";
136 root.decreased_value =
"";
138 root.child_count = 0;
139 root.custom_accessibility_actions_count = 0;
140 root.identifier =
"";
141 bridge->AddFlutterSemanticsNodeUpdate(root);
143 bridge->CommitUpdates();
145 auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
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);
163 NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
164 styleMask:NSBorderlessWindowMask
165 backing:NSBackingStoreBuffered
167 window.contentView = viewController.view;
169 engine.semanticsEnabled = YES;
170 auto bridge = viewController.accessibilityBridge.lock();
172 FlutterSemanticsNode2 root;
173 FlutterSemanticsFlags flags = FlutterSemanticsFlags{};
174 root.flags2 = &flags;
179 root.increased_value =
"";
180 root.decreased_value =
"";
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);
189 FlutterSemanticsNode2 child1;
190 FlutterSemanticsFlags child_flags = FlutterSemanticsFlags{};
191 child1.flags2 = &child_flags;
193 child1.label =
"child 1";
196 child1.increased_value =
"";
197 child1.decreased_value =
"";
199 child1.child_count = 0;
200 child1.custom_accessibility_actions_count = 0;
201 child1.identifier =
"";
202 bridge->AddFlutterSemanticsNodeUpdate(child1);
204 bridge->CommitUpdates();
206 auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
209 FlutterSemanticsAction called_action;
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) {
217 called_action = action;
222 ui::AXActionData action_data;
223 action_data.action = ax::mojom::Action::kDoDefault;
224 root_platform_node_delegate->AccessibilityPerformAction(action_data);
226 EXPECT_EQ(called_action, FlutterSemanticsAction::kFlutterSemanticsActionTap);
227 EXPECT_EQ(called_id, 1u);
229 [engine setViewController:nil];
230 [engine shutDownEngine];
238 [viewController loadView];
242 engine.textInputPlugin.string =
@"textfield";
244 NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
245 styleMask:NSBorderlessWindowMask
246 backing:NSBackingStoreBuffered
248 window.contentView = viewController.view;
249 engine.semanticsEnabled = YES;
251 auto bridge = viewController.accessibilityBridge.lock();
253 FlutterSemanticsNode2 root;
254 FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
255 FlutterSemanticsFlags child_flags = FlutterSemanticsFlags{.is_text_field =
true};
257 root.flags2 = &flags;
259 root.actions =
static_cast<FlutterSemanticsAction
>(0);
263 root.increased_value =
"";
264 root.decreased_value =
"";
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};
272 root.transform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
273 bridge->AddFlutterSemanticsNodeUpdate(root);
275 double rectSize = 50;
276 double transformFactor = 0.5;
278 FlutterSemanticsNode2 child1;
280 child1.flags2 = &child_flags;
282 child1.actions =
static_cast<FlutterSemanticsAction
>(0);
285 child1.value =
"textfield";
286 child1.increased_value =
"";
287 child1.decreased_value =
"";
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};
295 child1.transform = {transformFactor, 0, 0, 0, transformFactor, 0, 0, 0, 1};
296 bridge->AddFlutterSemanticsNodeUpdate(child1);
298 bridge->CommitUpdates();
300 auto child_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
302 id native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
303 EXPECT_EQ([native_accessibility isKindOfClass:[
FlutterTextField class]], YES);
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;
311 double expectedFrameSize = rectSize * transformFactor / pixelRatio;
312 EXPECT_EQ(NSEqualRects(native_text_field.frame, NSMakeRect(0, 600 - expectedFrameSize,
313 expectedFrameSize, expectedFrameSize)),
316 [native_text_field startEditing];
317 EXPECT_EQ([native_text_field.stringValue isEqualToString:
@"textfield"], YES);
329 [viewController loadView];
332 NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
333 styleMask:NSBorderlessWindowMask
334 backing:NSBackingStoreBuffered
336 window.contentView = viewController.view;
337 engine.semanticsEnabled = YES;
339 auto bridge = viewController.accessibilityBridge.lock();
341 FlutterSemanticsNode2 root = {};
342 FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
344 FlutterSemanticsFlags child_flags =
345 FlutterSemanticsFlags{.is_enabled = FlutterTristate::kFlutterTristateFalse,
346 .is_text_field =
true,
347 .is_read_only =
false};
349 root.flags2 = &flags;
351 root.actions =
static_cast<FlutterSemanticsAction
>(0);
355 root.increased_value =
"";
356 root.decreased_value =
"";
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};
364 root.transform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
365 bridge->AddFlutterSemanticsNodeUpdate(root);
367 FlutterSemanticsNode2 child1 = {};
369 child1.flags2 = &child_flags;
371 child1.actions =
static_cast<FlutterSemanticsAction
>(0);
374 child1.value =
"disabled textfield";
375 child1.increased_value =
"";
376 child1.decreased_value =
"";
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};
384 child1.transform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
385 bridge->AddFlutterSemanticsNodeUpdate(child1);
387 bridge->CommitUpdates();
389 auto child_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
391 id native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
392 EXPECT_FALSE([native_accessibility isKindOfClass:[
FlutterTextField class]]);
394 [[native_accessibility accessibilityRole] isEqualToString:NSAccessibilityStaticTextRole]);
396 [engine shutDownEngine];
402 [viewController loadView];
405 NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
406 styleMask:NSBorderlessWindowMask
407 backing:NSBackingStoreBuffered
409 window.contentView = viewController.view;
410 engine.semanticsEnabled = YES;
412 auto bridge = viewController.accessibilityBridge.lock();
414 FlutterSemanticsNode2 root;
416 FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
417 root.flags2 = &flags;
419 root.actions =
static_cast<FlutterSemanticsAction
>(0);
423 root.increased_value =
"";
424 root.decreased_value =
"";
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};
432 root.transform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
433 bridge->AddFlutterSemanticsNodeUpdate(root);
435 double rectSize = 50;
436 double transformFactor = 0.5;
438 FlutterSemanticsNode2 child1;
439 FlutterSemanticsFlags child_flags = FlutterSemanticsFlags{0};
440 child1.flags2 = &child_flags;
443 child1.actions =
static_cast<FlutterSemanticsAction
>(0);
446 child1.value =
"textfield";
447 child1.increased_value =
"";
448 child1.decreased_value =
"";
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};
456 child1.transform = {transformFactor, 0, 0, 0, transformFactor, 0, 0, 0, 1};
457 bridge->AddFlutterSemanticsNodeUpdate(child1);
459 bridge->CommitUpdates();
461 auto child_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
463 id native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
464 EXPECT_TRUE([[native_accessibility className] isEqualToString:
@"AXPlatformNodeCocoa"]);
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();
473 native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
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();
481 native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
482 EXPECT_TRUE([[native_accessibility className] isEqualToString:
@"AXPlatformNodeCocoa"]);
TEST(FlutterAppDelegateTest, DoesNotCallDelegatesWithoutHandler)