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 engine.semanticsEnabled = YES;
36 auto bridge = viewController.accessibilityBridge.lock();
38 FlutterSemanticsNode2 root;
39 FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
43 root.actions =
static_cast<FlutterSemanticsAction
>(0);
44 root.text_selection_base = -1;
45 root.text_selection_extent = -1;
46 root.label =
"accessibility";
49 root.increased_value =
"";
50 root.decreased_value =
"";
53 root.custom_accessibility_actions_count = 0;
54 bridge->AddFlutterSemanticsNodeUpdate(root);
56 bridge->CommitUpdates();
58 auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
60 NSAccessibilityElement* native_accessibility =
61 root_platform_node_delegate->GetNativeViewAccessible();
62 std::string value = [native_accessibility.accessibilityValue UTF8String];
63 EXPECT_TRUE(value ==
"accessibility");
64 EXPECT_EQ(native_accessibility.accessibilityRole, NSAccessibilityStaticTextRole);
65 EXPECT_EQ([native_accessibility.accessibilityChildren count], 0u);
66 [engine shutDownEngine];
72 engine.semanticsEnabled = YES;
73 auto bridge = viewController.accessibilityBridge.lock();
75 FlutterSemanticsNode2 root;
76 FlutterSemanticsFlags flags = FlutterSemanticsFlags{.is_text_field =
true, .is_read_only =
true};
80 root.actions =
static_cast<FlutterSemanticsAction
>(0);
81 root.text_selection_base = 1;
82 root.text_selection_extent = 3;
86 root.value =
"selectable text";
87 root.increased_value =
"";
88 root.decreased_value =
"";
91 root.custom_accessibility_actions_count = 0;
92 bridge->AddFlutterSemanticsNodeUpdate(root);
94 bridge->CommitUpdates();
96 auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
98 NSAccessibilityElement* native_accessibility =
99 root_platform_node_delegate->GetNativeViewAccessible();
100 std::string value = [native_accessibility.accessibilityValue UTF8String];
101 EXPECT_EQ(value,
"selectable text");
102 EXPECT_EQ(native_accessibility.accessibilityRole, NSAccessibilityStaticTextRole);
103 EXPECT_EQ([native_accessibility.accessibilityChildren count], 0u);
104 NSRange selection = native_accessibility.accessibilitySelectedTextRange;
105 EXPECT_EQ(selection.location, 1u);
106 EXPECT_EQ(selection.length, 2u);
107 std::string selected_text = [native_accessibility.accessibilitySelectedText UTF8String];
108 EXPECT_EQ(selected_text,
"el");
114 engine.semanticsEnabled = YES;
115 auto bridge = viewController.accessibilityBridge.lock();
117 FlutterSemanticsNode2 root;
118 FlutterSemanticsFlags flags = FlutterSemanticsFlags{.is_text_field =
true, .is_read_only =
true};
120 root.flags2 = &flags;
122 root.actions =
static_cast<FlutterSemanticsAction
>(0);
123 root.text_selection_base = -1;
124 root.text_selection_extent = -1;
128 root.value =
"selectable text";
129 root.increased_value =
"";
130 root.decreased_value =
"";
132 root.child_count = 0;
133 root.custom_accessibility_actions_count = 0;
134 bridge->AddFlutterSemanticsNodeUpdate(root);
136 bridge->CommitUpdates();
138 auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
140 NSAccessibilityElement* native_accessibility =
141 root_platform_node_delegate->GetNativeViewAccessible();
142 NSRange selection = native_accessibility.accessibilitySelectedTextRange;
143 EXPECT_TRUE(selection.location == NSNotFound);
144 EXPECT_EQ(selection.length, 0u);
155 NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
156 styleMask:NSBorderlessWindowMask
157 backing:NSBackingStoreBuffered
159 window.contentView = viewController.view;
161 engine.semanticsEnabled = YES;
162 auto bridge = viewController.accessibilityBridge.lock();
164 FlutterSemanticsNode2 root;
165 FlutterSemanticsFlags flags = FlutterSemanticsFlags{};
166 root.flags2 = &flags;
171 root.increased_value =
"";
172 root.decreased_value =
"";
174 root.child_count = 1;
175 int32_t children[] = {1};
176 root.children_in_traversal_order = children;
177 root.custom_accessibility_actions_count = 0;
178 bridge->AddFlutterSemanticsNodeUpdate(root);
180 FlutterSemanticsNode2 child1;
181 FlutterSemanticsFlags child_flags = FlutterSemanticsFlags{};
182 child1.flags2 = &child_flags;
184 child1.label =
"child 1";
187 child1.increased_value =
"";
188 child1.decreased_value =
"";
190 child1.child_count = 0;
191 child1.custom_accessibility_actions_count = 0;
192 bridge->AddFlutterSemanticsNodeUpdate(child1);
194 bridge->CommitUpdates();
196 auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
199 FlutterSemanticsAction called_action;
202 engine.embedderAPI.DispatchSemanticsAction = MOCK_ENGINE_PROC(
203 DispatchSemanticsAction,
204 ([&called_id, &called_action](
auto engine, uint64_t
id, FlutterSemanticsAction action,
205 const uint8_t* data,
size_t data_length) {
207 called_action = action;
212 ui::AXActionData action_data;
213 action_data.action = ax::mojom::Action::kDoDefault;
214 root_platform_node_delegate->AccessibilityPerformAction(action_data);
216 EXPECT_EQ(called_action, FlutterSemanticsAction::kFlutterSemanticsActionTap);
217 EXPECT_EQ(called_id, 1u);
219 [engine setViewController:nil];
220 [engine shutDownEngine];
228 [viewController loadView];
232 engine.textInputPlugin.string =
@"textfield";
234 NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
235 styleMask:NSBorderlessWindowMask
236 backing:NSBackingStoreBuffered
238 window.contentView = viewController.view;
239 engine.semanticsEnabled = YES;
241 auto bridge = viewController.accessibilityBridge.lock();
243 FlutterSemanticsNode2 root;
244 FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
245 FlutterSemanticsFlags child_flags = FlutterSemanticsFlags{.is_text_field =
true};
247 root.flags2 = &flags;
249 root.actions =
static_cast<FlutterSemanticsAction
>(0);
253 root.increased_value =
"";
254 root.decreased_value =
"";
256 root.child_count = 1;
257 int32_t children[] = {1};
258 root.children_in_traversal_order = children;
259 root.custom_accessibility_actions_count = 0;
260 root.rect = {0, 0, 100, 100};
261 root.transform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
262 bridge->AddFlutterSemanticsNodeUpdate(root);
264 double rectSize = 50;
265 double transformFactor = 0.5;
267 FlutterSemanticsNode2 child1;
269 child1.flags2 = &child_flags;
271 child1.actions =
static_cast<FlutterSemanticsAction
>(0);
274 child1.value =
"textfield";
275 child1.increased_value =
"";
276 child1.decreased_value =
"";
278 child1.text_selection_base = -1;
279 child1.text_selection_extent = -1;
280 child1.child_count = 0;
281 child1.custom_accessibility_actions_count = 0;
282 child1.rect = {0, 0, rectSize, rectSize};
283 child1.transform = {transformFactor, 0, 0, 0, transformFactor, 0, 0, 0, 1};
284 bridge->AddFlutterSemanticsNodeUpdate(child1);
286 bridge->CommitUpdates();
288 auto child_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
290 id native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
291 EXPECT_EQ([native_accessibility isKindOfClass:[
FlutterTextField class]], YES);
294 NSView* view = viewController.flutterView;
295 CGRect scaledBounds = [view convertRectToBacking:view.bounds];
296 CGSize scaledSize = scaledBounds.size;
297 double pixelRatio = view.bounds.size.width == 0 ? 1 : scaledSize.width / view.bounds.size.width;
299 double expectedFrameSize = rectSize * transformFactor / pixelRatio;
300 EXPECT_EQ(NSEqualRects(native_text_field.frame, NSMakeRect(0, 600 - expectedFrameSize,
301 expectedFrameSize, expectedFrameSize)),
304 [native_text_field startEditing];
305 EXPECT_EQ([native_text_field.stringValue isEqualToString:
@"textfield"], YES);
311 [viewController loadView];
314 NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
315 styleMask:NSBorderlessWindowMask
316 backing:NSBackingStoreBuffered
318 window.contentView = viewController.view;
319 engine.semanticsEnabled = YES;
321 auto bridge = viewController.accessibilityBridge.lock();
323 FlutterSemanticsNode2 root;
325 FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
326 root.flags2 = &flags;
328 root.actions =
static_cast<FlutterSemanticsAction
>(0);
332 root.increased_value =
"";
333 root.decreased_value =
"";
335 root.child_count = 1;
336 int32_t children[] = {1};
337 root.children_in_traversal_order = children;
338 root.custom_accessibility_actions_count = 0;
339 root.rect = {0, 0, 100, 100};
340 root.transform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
341 bridge->AddFlutterSemanticsNodeUpdate(root);
343 double rectSize = 50;
344 double transformFactor = 0.5;
346 FlutterSemanticsNode2 child1;
347 FlutterSemanticsFlags child_flags = FlutterSemanticsFlags{0};
348 child1.flags2 = &child_flags;
351 child1.actions =
static_cast<FlutterSemanticsAction
>(0);
354 child1.value =
"textfield";
355 child1.increased_value =
"";
356 child1.decreased_value =
"";
358 child1.text_selection_base = -1;
359 child1.text_selection_extent = -1;
360 child1.child_count = 0;
361 child1.custom_accessibility_actions_count = 0;
362 child1.rect = {0, 0, rectSize, rectSize};
363 child1.transform = {transformFactor, 0, 0, 0, transformFactor, 0, 0, 0, 1};
364 bridge->AddFlutterSemanticsNodeUpdate(child1);
366 bridge->CommitUpdates();
368 auto child_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
370 id native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
371 EXPECT_TRUE([[native_accessibility className] isEqualToString:
@"AXPlatformNodeCocoa"]);
375 FlutterSemanticsFlags child_flags_updated_1 = FlutterSemanticsFlags{.is_text_field =
true};
376 child1.flags2 = &child_flags_updated_1;
377 bridge->AddFlutterSemanticsNodeUpdate(child1);
378 bridge->CommitUpdates();
380 native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
383 FlutterSemanticsFlags child_flags_updated_2 = FlutterSemanticsFlags{.is_text_field =
false};
384 child1.flags2 = &child_flags_updated_2;
385 bridge->AddFlutterSemanticsNodeUpdate(child1);
386 bridge->CommitUpdates();
388 native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
389 EXPECT_TRUE([[native_accessibility className] isEqualToString:
@"AXPlatformNodeCocoa"]);
TEST(FlutterAppDelegateTest, DoesNotCallDelegatesWithoutHandler)