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};
42 root.actions =
static_cast<FlutterSemanticsAction
>(0);
43 root.text_selection_base = -1;
44 root.text_selection_extent = -1;
45 root.label =
"accessibility";
48 root.increased_value =
"";
49 root.decreased_value =
"";
52 root.custom_accessibility_actions_count = 0;
53 bridge->AddFlutterSemanticsNodeUpdate(root);
55 bridge->CommitUpdates();
57 auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
59 NSAccessibilityElement* native_accessibility =
60 root_platform_node_delegate->GetNativeViewAccessible();
61 std::string value = [native_accessibility.accessibilityValue UTF8String];
62 EXPECT_TRUE(value ==
"accessibility");
63 EXPECT_EQ(native_accessibility.accessibilityRole, NSAccessibilityStaticTextRole);
64 EXPECT_EQ([native_accessibility.accessibilityChildren count], 0u);
65 [engine shutDownEngine];
71 engine.semanticsEnabled = YES;
72 auto bridge = viewController.accessibilityBridge.lock();
74 FlutterSemanticsNode2 root;
75 FlutterSemanticsFlags flags = FlutterSemanticsFlags{.is_text_field =
true, .is_read_only =
true};
78 root.actions =
static_cast<FlutterSemanticsAction
>(0);
79 root.text_selection_base = 1;
80 root.text_selection_extent = 3;
84 root.value =
"selectable text";
85 root.increased_value =
"";
86 root.decreased_value =
"";
89 root.custom_accessibility_actions_count = 0;
90 bridge->AddFlutterSemanticsNodeUpdate(root);
92 bridge->CommitUpdates();
94 auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
96 NSAccessibilityElement* native_accessibility =
97 root_platform_node_delegate->GetNativeViewAccessible();
98 std::string value = [native_accessibility.accessibilityValue UTF8String];
99 EXPECT_EQ(value,
"selectable text");
100 EXPECT_EQ(native_accessibility.accessibilityRole, NSAccessibilityStaticTextRole);
101 EXPECT_EQ([native_accessibility.accessibilityChildren count], 0u);
102 NSRange selection = native_accessibility.accessibilitySelectedTextRange;
103 EXPECT_EQ(selection.location, 1u);
104 EXPECT_EQ(selection.length, 2u);
105 std::string selected_text = [native_accessibility.accessibilitySelectedText UTF8String];
106 EXPECT_EQ(selected_text,
"el");
112 engine.semanticsEnabled = YES;
113 auto bridge = viewController.accessibilityBridge.lock();
115 FlutterSemanticsNode2 root;
116 FlutterSemanticsFlags flags = FlutterSemanticsFlags{.is_text_field =
true, .is_read_only =
true};
118 root.flags2 = &flags;
119 root.actions =
static_cast<FlutterSemanticsAction
>(0);
120 root.text_selection_base = -1;
121 root.text_selection_extent = -1;
125 root.value =
"selectable text";
126 root.increased_value =
"";
127 root.decreased_value =
"";
129 root.child_count = 0;
130 root.custom_accessibility_actions_count = 0;
131 bridge->AddFlutterSemanticsNodeUpdate(root);
133 bridge->CommitUpdates();
135 auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
137 NSAccessibilityElement* native_accessibility =
138 root_platform_node_delegate->GetNativeViewAccessible();
139 NSRange selection = native_accessibility.accessibilitySelectedTextRange;
140 EXPECT_TRUE(selection.location == NSNotFound);
141 EXPECT_EQ(selection.length, 0u);
152 NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
153 styleMask:NSBorderlessWindowMask
154 backing:NSBackingStoreBuffered
156 window.contentView = viewController.view;
158 engine.semanticsEnabled = YES;
159 auto bridge = viewController.accessibilityBridge.lock();
161 FlutterSemanticsNode2 root;
162 FlutterSemanticsFlags flags = FlutterSemanticsFlags{};
163 root.flags2 = &flags;
168 root.increased_value =
"";
169 root.decreased_value =
"";
171 root.child_count = 1;
172 int32_t children[] = {1};
173 root.children_in_traversal_order = children;
174 root.custom_accessibility_actions_count = 0;
175 bridge->AddFlutterSemanticsNodeUpdate(root);
177 FlutterSemanticsNode2 child1;
178 FlutterSemanticsFlags child_flags = FlutterSemanticsFlags{};
179 child1.flags2 = &child_flags;
181 child1.label =
"child 1";
184 child1.increased_value =
"";
185 child1.decreased_value =
"";
187 child1.child_count = 0;
188 child1.custom_accessibility_actions_count = 0;
189 bridge->AddFlutterSemanticsNodeUpdate(child1);
191 bridge->CommitUpdates();
193 auto root_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
196 FlutterSemanticsAction called_action;
199 engine.embedderAPI.DispatchSemanticsAction = MOCK_ENGINE_PROC(
200 DispatchSemanticsAction,
201 ([&called_id, &called_action](
auto engine, uint64_t
id, FlutterSemanticsAction action,
202 const uint8_t* data,
size_t data_length) {
204 called_action = action;
209 ui::AXActionData action_data;
210 action_data.action = ax::mojom::Action::kDoDefault;
211 root_platform_node_delegate->AccessibilityPerformAction(action_data);
213 EXPECT_EQ(called_action, FlutterSemanticsAction::kFlutterSemanticsActionTap);
214 EXPECT_EQ(called_id, 1u);
216 [engine setViewController:nil];
217 [engine shutDownEngine];
225 [viewController loadView];
229 engine.textInputPlugin.string =
@"textfield";
231 NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
232 styleMask:NSBorderlessWindowMask
233 backing:NSBackingStoreBuffered
235 window.contentView = viewController.view;
236 engine.semanticsEnabled = YES;
238 auto bridge = viewController.accessibilityBridge.lock();
240 FlutterSemanticsNode2 root;
241 FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
242 FlutterSemanticsFlags child_flags = FlutterSemanticsFlags{.is_text_field =
true};
244 root.flags2 = &flags;
245 root.actions =
static_cast<FlutterSemanticsAction
>(0);
249 root.increased_value =
"";
250 root.decreased_value =
"";
252 root.child_count = 1;
253 int32_t children[] = {1};
254 root.children_in_traversal_order = children;
255 root.custom_accessibility_actions_count = 0;
256 root.rect = {0, 0, 100, 100};
257 root.transform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
258 bridge->AddFlutterSemanticsNodeUpdate(root);
260 double rectSize = 50;
261 double transformFactor = 0.5;
263 FlutterSemanticsNode2 child1;
265 child1.flags2 = &child_flags;
266 child1.actions =
static_cast<FlutterSemanticsAction
>(0);
269 child1.value =
"textfield";
270 child1.increased_value =
"";
271 child1.decreased_value =
"";
273 child1.text_selection_base = -1;
274 child1.text_selection_extent = -1;
275 child1.child_count = 0;
276 child1.custom_accessibility_actions_count = 0;
277 child1.rect = {0, 0, rectSize, rectSize};
278 child1.transform = {transformFactor, 0, 0, 0, transformFactor, 0, 0, 0, 1};
279 bridge->AddFlutterSemanticsNodeUpdate(child1);
281 bridge->CommitUpdates();
283 auto child_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
285 id native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
286 EXPECT_EQ([native_accessibility isKindOfClass:[
FlutterTextField class]], YES);
289 NSView* view = viewController.flutterView;
290 CGRect scaledBounds = [view convertRectToBacking:view.bounds];
291 CGSize scaledSize = scaledBounds.size;
292 double pixelRatio = view.bounds.size.width == 0 ? 1 : scaledSize.width / view.bounds.size.width;
294 double expectedFrameSize = rectSize * transformFactor / pixelRatio;
295 EXPECT_EQ(NSEqualRects(native_text_field.frame, NSMakeRect(0, 600 - expectedFrameSize,
296 expectedFrameSize, expectedFrameSize)),
299 [native_text_field startEditing];
300 EXPECT_EQ([native_text_field.stringValue isEqualToString:
@"textfield"], YES);
306 [viewController loadView];
309 NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
310 styleMask:NSBorderlessWindowMask
311 backing:NSBackingStoreBuffered
313 window.contentView = viewController.view;
314 engine.semanticsEnabled = YES;
316 auto bridge = viewController.accessibilityBridge.lock();
318 FlutterSemanticsNode2 root;
320 FlutterSemanticsFlags flags = FlutterSemanticsFlags{0};
321 root.flags2 = &flags;
322 root.actions =
static_cast<FlutterSemanticsAction
>(0);
326 root.increased_value =
"";
327 root.decreased_value =
"";
329 root.child_count = 1;
330 int32_t children[] = {1};
331 root.children_in_traversal_order = children;
332 root.custom_accessibility_actions_count = 0;
333 root.rect = {0, 0, 100, 100};
334 root.transform = {1, 0, 0, 0, 1, 0, 0, 0, 1};
335 bridge->AddFlutterSemanticsNodeUpdate(root);
337 double rectSize = 50;
338 double transformFactor = 0.5;
340 FlutterSemanticsNode2 child1;
341 FlutterSemanticsFlags child_flags = FlutterSemanticsFlags{0};
342 child1.flags2 = &child_flags;
344 child1.actions =
static_cast<FlutterSemanticsAction
>(0);
347 child1.value =
"textfield";
348 child1.increased_value =
"";
349 child1.decreased_value =
"";
351 child1.text_selection_base = -1;
352 child1.text_selection_extent = -1;
353 child1.child_count = 0;
354 child1.custom_accessibility_actions_count = 0;
355 child1.rect = {0, 0, rectSize, rectSize};
356 child1.transform = {transformFactor, 0, 0, 0, transformFactor, 0, 0, 0, 1};
357 bridge->AddFlutterSemanticsNodeUpdate(child1);
359 bridge->CommitUpdates();
361 auto child_platform_node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
363 id native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
364 EXPECT_TRUE([[native_accessibility className] isEqualToString:
@"AXPlatformNodeCocoa"]);
368 FlutterSemanticsFlags child_flags_updated_1 = FlutterSemanticsFlags{.is_text_field =
true};
369 child1.flags2 = &child_flags_updated_1;
370 bridge->AddFlutterSemanticsNodeUpdate(child1);
371 bridge->CommitUpdates();
373 native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
376 FlutterSemanticsFlags child_flags_updated_2 = FlutterSemanticsFlags{.is_text_field =
false};
377 child1.flags2 = &child_flags_updated_2;
378 bridge->AddFlutterSemanticsNodeUpdate(child1);
379 bridge->CommitUpdates();
381 native_accessibility = child_platform_node_delegate->GetNativeViewAccessible();
382 EXPECT_TRUE([[native_accessibility className] isEqualToString:
@"AXPlatformNodeCocoa"]);
TEST(FlutterAppDelegateTest, DoesNotCallDelegatesWithoutHandler)