1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
content / browser / accessibility / browser_accessibility_unittest.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/accessibility/platform/browser_accessibility.h"
#include "build/build_config.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/ax_enums.mojom-shared.h"
#include "ui/accessibility/ax_node_position.h"
#include "ui/accessibility/platform/browser_accessibility_manager.h"
#include "ui/accessibility/platform/test_ax_node_id_delegate.h"
#include "ui/accessibility/platform/test_ax_platform_tree_manager_delegate.h"
#if BUILDFLAG(IS_ANDROID)
#include "content/browser/accessibility/browser_accessibility_manager_android.h"
#endif
namespace content {
namespace {
ui::BrowserAccessibilityManager* CreateBrowserAccessibilityManager(
const ui::AXTreeUpdate& initial_tree,
ui::AXNodeIdDelegate& node_id_delegate,
ui::AXPlatformTreeManagerDelegate* delegate) {
#if BUILDFLAG(IS_ANDROID)
return content::BrowserAccessibilityManagerAndroid::Create(
initial_tree, node_id_delegate, delegate);
#else
return ui::BrowserAccessibilityManager::Create(initial_tree, node_id_delegate,
delegate);
#endif
}
} // namespace
using RetargetEventType = ui::AXTreeManager::RetargetEventType;
class BrowserAccessibilityTest : public ::testing::Test {
public:
BrowserAccessibilityTest();
BrowserAccessibilityTest(const BrowserAccessibilityTest&) = delete;
BrowserAccessibilityTest& operator=(const BrowserAccessibilityTest&) = delete;
~BrowserAccessibilityTest() override;
protected:
std::unique_ptr<ui::TestAXPlatformTreeManagerDelegate>
test_browser_accessibility_delegate_;
ui::TestAXNodeIdDelegate node_id_delegate_;
private:
void SetUp() override;
BrowserTaskEnvironment task_environment_;
};
BrowserAccessibilityTest::BrowserAccessibilityTest() {}
BrowserAccessibilityTest::~BrowserAccessibilityTest() = default;
void BrowserAccessibilityTest::SetUp() {
test_browser_accessibility_delegate_ =
std::make_unique<ui::TestAXPlatformTreeManagerDelegate>();
}
TEST_F(BrowserAccessibilityTest, TestCanFireEvents) {
ui::AXNodeData text1;
text1.id = 111;
text1.role = ax::mojom::Role::kStaticText;
text1.SetName("One two three.");
ui::AXNodeData para1;
para1.id = 11;
para1.role = ax::mojom::Role::kParagraph;
para1.child_ids.push_back(text1.id);
ui::AXNodeData root;
root.id = 1;
root.role = ax::mojom::Role::kRootWebArea;
root.child_ids.push_back(para1.id);
std::unique_ptr<ui::BrowserAccessibilityManager> manager(
CreateBrowserAccessibilityManager(
MakeAXTreeUpdateForTesting(root, para1, text1), node_id_delegate_,
test_browser_accessibility_delegate_.get()));
ui::BrowserAccessibility* root_obj = manager->GetBrowserAccessibilityRoot();
EXPECT_FALSE(root_obj->IsLeaf());
EXPECT_TRUE(root_obj->CanFireEvents());
ui::BrowserAccessibility* para_obj = root_obj->PlatformGetChild(0);
EXPECT_TRUE(para_obj->CanFireEvents());
#if BUILDFLAG(IS_ANDROID)
EXPECT_TRUE(para_obj->IsLeaf());
#else
EXPECT_FALSE(para_obj->IsLeaf());
#endif
ui::BrowserAccessibility* text_obj = manager->GetFromID(111);
EXPECT_TRUE(text_obj->IsLeaf());
#if !BUILDFLAG(IS_ANDROID)
EXPECT_TRUE(text_obj->CanFireEvents());
#endif
ui::BrowserAccessibility* retarget =
manager->RetargetBrowserAccessibilityForEvents(
text_obj, RetargetEventType::RetargetEventTypeBlinkHover);
EXPECT_TRUE(retarget->CanFireEvents());
manager.reset();
}
#if BUILDFLAG(IS_WIN) || BUILDFLAG(USE_ATK)
TEST_F(BrowserAccessibilityTest, PlatformChildIterator) {
// (i) => node is ignored
// Parent Tree
// 1
// |__________
// | | |
// 2(i) 3 4
// |__________________________________
// | | | |
// 5 6 7(i) 8(i)
// | | |________
// | | | |
// Child Tree 9(i) 10(i) 11
// | |____
// | | |
// 12(i) 13 14
// Child Tree
// 1
// |_________
// | | |
// 2 3 4
// |
// 5
ui::AXTreeID parent_tree_id = ui::AXTreeID::CreateNewAXTreeID();
ui::AXTreeID child_tree_id = ui::AXTreeID::CreateNewAXTreeID();
ui::AXTreeUpdate parent_tree_update;
parent_tree_update.tree_data.tree_id = parent_tree_id;
parent_tree_update.has_tree_data = true;
parent_tree_update.root_id = 1;
parent_tree_update.nodes.resize(14);
parent_tree_update.nodes[0].id = 1;
parent_tree_update.nodes[0].child_ids = {2, 3, 4};
parent_tree_update.nodes[1].id = 2;
parent_tree_update.nodes[1].child_ids = {5, 6, 7, 8};
parent_tree_update.nodes[1].AddState(ax::mojom::State::kIgnored);
parent_tree_update.nodes[2].id = 3;
parent_tree_update.nodes[3].id = 4;
parent_tree_update.nodes[4].id = 5;
parent_tree_update.nodes[4].AddChildTreeId(child_tree_id);
parent_tree_update.nodes[5].id = 6;
parent_tree_update.nodes[5].child_ids = {9};
parent_tree_update.nodes[6].id = 7;
parent_tree_update.nodes[6].child_ids = {10, 11};
parent_tree_update.nodes[6].AddState(ax::mojom::State::kIgnored);
parent_tree_update.nodes[7].id = 8;
parent_tree_update.nodes[7].AddState(ax::mojom::State::kIgnored);
parent_tree_update.nodes[8].id = 9;
parent_tree_update.nodes[8].child_ids = {12};
parent_tree_update.nodes[8].AddState(ax::mojom::State::kIgnored);
parent_tree_update.nodes[9].id = 10;
parent_tree_update.nodes[9].child_ids = {13, 14};
parent_tree_update.nodes[9].AddState(ax::mojom::State::kIgnored);
parent_tree_update.nodes[10].id = 11;
parent_tree_update.nodes[11].id = 12;
parent_tree_update.nodes[11].AddState(ax::mojom::State::kIgnored);
parent_tree_update.nodes[12].id = 13;
parent_tree_update.nodes[13].id = 14;
ui::AXTreeUpdate child_tree_update;
child_tree_update.tree_data.tree_id = child_tree_id;
child_tree_update.tree_data.parent_tree_id = parent_tree_id;
child_tree_update.has_tree_data = true;
child_tree_update.root_id = 1;
child_tree_update.nodes.resize(5);
child_tree_update.nodes[0].id = 1;
child_tree_update.nodes[0].child_ids = {2, 3, 4};
child_tree_update.nodes[1].id = 2;
child_tree_update.nodes[2].id = 3;
child_tree_update.nodes[2].child_ids = {5};
child_tree_update.nodes[3].id = 4;
child_tree_update.nodes[4].id = 5;
std::unique_ptr<ui::BrowserAccessibilityManager> parent_manager(
CreateBrowserAccessibilityManager(parent_tree_update, node_id_delegate_,
nullptr));
std::unique_ptr<ui::BrowserAccessibilityManager> child_manager(
CreateBrowserAccessibilityManager(child_tree_update, node_id_delegate_,
nullptr));
ui::BrowserAccessibility* root_obj =
parent_manager->GetBrowserAccessibilityRoot();
// Test traversal
// PlatformChildren(root_obj) = {5, 6, 13, 15, 11, 3, 4}
ui::BrowserAccessibility::PlatformChildIterator platform_iterator =
root_obj->PlatformChildrenBegin();
EXPECT_EQ(5, platform_iterator->GetId());
EXPECT_EQ(nullptr, platform_iterator->PlatformGetPreviousSibling());
EXPECT_EQ(1u, platform_iterator->PlatformChildCount());
// Test Child-Tree Traversal
ui::BrowserAccessibility* child_tree_root =
platform_iterator->PlatformGetFirstChild();
EXPECT_EQ(1, child_tree_root->GetId());
ui::BrowserAccessibility::PlatformChildIterator child_tree_iterator =
child_tree_root->PlatformChildrenBegin();
EXPECT_EQ(2, child_tree_iterator->GetId());
++child_tree_iterator;
EXPECT_EQ(3, child_tree_iterator->GetId());
++child_tree_iterator;
EXPECT_EQ(4, child_tree_iterator->GetId());
++platform_iterator;
EXPECT_EQ(6, platform_iterator->GetId());
++platform_iterator;
EXPECT_EQ(13, platform_iterator->GetId());
++platform_iterator;
EXPECT_EQ(14, platform_iterator->GetId());
--platform_iterator;
EXPECT_EQ(13, platform_iterator->GetId());
--platform_iterator;
EXPECT_EQ(6, platform_iterator->GetId());
++platform_iterator;
EXPECT_EQ(13, platform_iterator->GetId());
++platform_iterator;
EXPECT_EQ(14, platform_iterator->GetId());
++platform_iterator;
EXPECT_EQ(11, platform_iterator->GetId());
++platform_iterator;
EXPECT_EQ(3, platform_iterator->GetId());
++platform_iterator;
EXPECT_EQ(4, platform_iterator->GetId());
++platform_iterator;
EXPECT_EQ(root_obj->PlatformChildrenEnd(), platform_iterator);
// test empty list
// PlatformChildren(3) = {}
ui::BrowserAccessibility* node2 = parent_manager->GetFromID(3);
platform_iterator = node2->PlatformChildrenBegin();
EXPECT_EQ(node2->PlatformChildrenEnd(), platform_iterator);
// empty list from ignored node
// PlatformChildren(8) = {}
ui::BrowserAccessibility* node8 = parent_manager->GetFromID(8);
platform_iterator = node8->PlatformChildrenBegin();
EXPECT_EQ(node8->PlatformChildrenEnd(), platform_iterator);
// non-empty list from ignored node
// PlatformChildren(10) = {13, 15}
ui::BrowserAccessibility* node10 = parent_manager->GetFromID(10);
platform_iterator = node10->PlatformChildrenBegin();
EXPECT_EQ(13, platform_iterator->GetId());
// Two UnignoredChildIterators from the same parent at the same position
// should be equivalent, even in end position.
platform_iterator = root_obj->PlatformChildrenBegin();
ui::BrowserAccessibility::PlatformChildIterator platform_iterator2 =
root_obj->PlatformChildrenBegin();
auto end = root_obj->PlatformChildrenEnd();
while (platform_iterator != end) {
ASSERT_EQ(platform_iterator, platform_iterator2);
++platform_iterator;
++platform_iterator2;
}
ASSERT_EQ(platform_iterator, platform_iterator2);
}
#endif // BUILDFLAG(IS_WIN) || BUILDFLAG(USE_ATK)
TEST_F(BrowserAccessibilityTest, GetInnerTextRangeBoundsRect) {
ui::AXNodeData root;
root.id = 1;
root.role = ax::mojom::Role::kRootWebArea;
root.relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
ui::AXNodeData static_text;
static_text.id = 2;
static_text.role = ax::mojom::Role::kStaticText;
static_text.SetName("Hello, world.");
static_text.relative_bounds.bounds = gfx::RectF(100, 100, 29, 18);
root.child_ids.push_back(2);
ui::AXNodeData inline_text1;
inline_text1.id = 3;
inline_text1.role = ax::mojom::Role::kInlineTextBox;
inline_text1.SetName("Hello, ");
inline_text1.relative_bounds.bounds = gfx::RectF(100, 100, 29, 9);
inline_text1.SetTextDirection(ax::mojom::WritingDirection::kLtr);
std::vector<int32_t> character_offsets1;
character_offsets1.push_back(6);
character_offsets1.push_back(11);
character_offsets1.push_back(16);
character_offsets1.push_back(21);
character_offsets1.push_back(26);
character_offsets1.push_back(29);
character_offsets1.push_back(29);
inline_text1.AddIntListAttribute(
ax::mojom::IntListAttribute::kCharacterOffsets, character_offsets1);
static_text.child_ids.push_back(3);
ui::AXNodeData inline_text2;
inline_text2.id = 4;
inline_text2.role = ax::mojom::Role::kInlineTextBox;
inline_text2.SetName("world.");
inline_text2.relative_bounds.bounds = gfx::RectF(100, 109, 28, 9);
inline_text2.SetTextDirection(ax::mojom::WritingDirection::kLtr);
std::vector<int32_t> character_offsets2;
character_offsets2.push_back(5);
character_offsets2.push_back(10);
character_offsets2.push_back(15);
character_offsets2.push_back(20);
character_offsets2.push_back(25);
character_offsets2.push_back(28);
inline_text2.AddIntListAttribute(
ax::mojom::IntListAttribute::kCharacterOffsets, character_offsets2);
static_text.child_ids.push_back(4);
std::unique_ptr<ui::BrowserAccessibilityManager>
browser_accessibility_manager(CreateBrowserAccessibilityManager(
MakeAXTreeUpdateForTesting(root, static_text, inline_text1,
inline_text2),
node_id_delegate_, test_browser_accessibility_delegate_.get()));
ui::BrowserAccessibility* root_accessible =
browser_accessibility_manager->GetBrowserAccessibilityRoot();
ASSERT_NE(nullptr, root_accessible);
ui::BrowserAccessibility* static_text_accessible =
root_accessible->PlatformGetChild(0);
ASSERT_NE(nullptr, static_text_accessible);
#if BUILDFLAG(IS_ANDROID)
// Android disallows getting inner text from root accessibility nodes.
EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
root_accessible
->GetInnerTextRangeBoundsRect(
0, 1, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
#else
// Validate the bounding box of 'H' from root.
EXPECT_EQ(gfx::Rect(100, 100, 6, 9).ToString(),
root_accessible
->GetInnerTextRangeBoundsRect(
0, 1, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
#endif
// Validate the bounding box of 'H' from static text.
EXPECT_EQ(gfx::Rect(100, 100, 6, 9).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
0, 1, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
// Validate the bounding box of 'Hello' from static text.
EXPECT_EQ(gfx::Rect(100, 100, 26, 9).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
0, 5, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
// Validate the bounding box of 'Hello, world.' from static text.
EXPECT_EQ(gfx::Rect(100, 100, 29, 18).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
0, 13, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
#if BUILDFLAG(IS_ANDROID)
// Android disallows getting inner text from root accessibility nodes.
EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
root_accessible
->GetInnerTextRangeBoundsRect(
0, 13, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
#else
// Validate the bounding box of 'Hello, world.' from root.
EXPECT_EQ(gfx::Rect(100, 100, 29, 18).ToString(),
root_accessible
->GetInnerTextRangeBoundsRect(
0, 13, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
#endif
}
TEST_F(BrowserAccessibilityTest, GetInnerTextRangeBoundsRectPlainTextField) {
// Text area with 'Hello' text
// rootWebArea
// ++textField
// ++++genericContainer
// ++++++staticText
// ++++++++inlineTextBox
ui::AXNodeData root;
root.id = 1;
root.role = ax::mojom::Role::kRootWebArea;
root.relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
ui::AXNodeData textarea;
textarea.id = 2;
textarea.role = ax::mojom::Role::kTextField;
textarea.SetValue("Hello");
textarea.relative_bounds.bounds = gfx::RectF(100, 100, 150, 20);
root.child_ids.push_back(2);
ui::AXNodeData container;
container.id = 3;
container.role = ax::mojom::Role::kGenericContainer;
container.relative_bounds.bounds = textarea.relative_bounds.bounds;
textarea.child_ids.push_back(3);
ui::AXNodeData static_text;
static_text.id = 4;
static_text.role = ax::mojom::Role::kStaticText;
static_text.SetName("Hello");
static_text.relative_bounds.bounds = gfx::RectF(100, 100, 50, 10);
container.child_ids.push_back(4);
ui::AXNodeData inline_text1;
inline_text1.id = 5;
inline_text1.role = ax::mojom::Role::kInlineTextBox;
inline_text1.SetName("Hello");
inline_text1.relative_bounds.bounds = gfx::RectF(100, 100, 50, 10);
inline_text1.AddIntListAttribute(
ax::mojom::IntListAttribute::kCharacterOffsets, {10, 20, 30, 40, 50});
inline_text1.SetTextDirection(ax::mojom::WritingDirection::kLtr);
static_text.child_ids.push_back(5);
std::unique_ptr<ui::BrowserAccessibilityManager>
browser_accessibility_manager(CreateBrowserAccessibilityManager(
MakeAXTreeUpdateForTesting(root, textarea, container, static_text,
inline_text1),
node_id_delegate_, test_browser_accessibility_delegate_.get()));
ui::BrowserAccessibility* root_accessible =
browser_accessibility_manager->GetBrowserAccessibilityRoot();
ASSERT_NE(nullptr, root_accessible);
ui::BrowserAccessibility* textarea_accessible =
root_accessible->PlatformGetChild(0);
ASSERT_NE(nullptr, textarea_accessible);
// Validate the bounds of 'ell'.
EXPECT_EQ(gfx::Rect(110, 100, 30, 10),
textarea_accessible->GetInnerTextRangeBoundsRect(
1, 4, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped));
}
TEST_F(BrowserAccessibilityTest, GetInnerTextRangeBoundsRectMultiElement) {
ui::AXNodeData root;
root.id = 1;
root.role = ax::mojom::Role::kRootWebArea;
root.relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
ui::AXNodeData static_text;
static_text.id = 2;
static_text.role = ax::mojom::Role::kStaticText;
static_text.SetName("ABC");
static_text.relative_bounds.bounds = gfx::RectF(0, 20, 33, 9);
root.child_ids.push_back(2);
ui::AXNodeData inline_text1;
inline_text1.id = 3;
inline_text1.role = ax::mojom::Role::kInlineTextBox;
inline_text1.SetName("ABC");
inline_text1.relative_bounds.bounds = gfx::RectF(0, 20, 33, 9);
inline_text1.SetTextDirection(ax::mojom::WritingDirection::kLtr);
std::vector<int32_t> character_offsets{10, 21, 33};
inline_text1.AddIntListAttribute(
ax::mojom::IntListAttribute::kCharacterOffsets, character_offsets);
static_text.child_ids.push_back(3);
ui::AXNodeData static_text2;
static_text2.id = 4;
static_text2.role = ax::mojom::Role::kStaticText;
static_text2.SetName("ABC");
static_text2.relative_bounds.bounds = gfx::RectF(10, 40, 33, 9);
root.child_ids.push_back(4);
ui::AXNodeData inline_text2;
inline_text2.id = 5;
inline_text2.role = ax::mojom::Role::kInlineTextBox;
inline_text2.SetName("ABC");
inline_text2.relative_bounds.bounds = gfx::RectF(10, 40, 33, 9);
inline_text2.SetTextDirection(ax::mojom::WritingDirection::kLtr);
inline_text2.AddIntListAttribute(
ax::mojom::IntListAttribute::kCharacterOffsets, character_offsets);
static_text2.child_ids.push_back(5);
std::unique_ptr<ui::BrowserAccessibilityManager>
browser_accessibility_manager(CreateBrowserAccessibilityManager(
MakeAXTreeUpdateForTesting(root, static_text, inline_text1,
static_text2, inline_text2),
node_id_delegate_, test_browser_accessibility_delegate_.get()));
ui::BrowserAccessibility* root_accessible =
browser_accessibility_manager->GetBrowserAccessibilityRoot();
ASSERT_NE(nullptr, root_accessible);
ui::BrowserAccessibility* static_text_accessible =
root_accessible->PlatformGetChild(0);
ASSERT_NE(nullptr, static_text_accessible);
ui::BrowserAccessibility* static_text_accessible2 =
root_accessible->PlatformGetChild(1);
ASSERT_NE(nullptr, static_text_accessible);
// Validate the bounds of 'ABC' on the first line.
EXPECT_EQ(gfx::Rect(0, 20, 33, 9).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
0, 3, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
// Validate the bounds of only 'AB' on the first line.
EXPECT_EQ(gfx::Rect(0, 20, 21, 9).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
0, 2, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
// Validate the bounds of only 'BC' on the first line.
EXPECT_EQ(gfx::Rect(10, 20, 23, 9).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
1, 3, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
// Validate the bounds of 'ABC' on the second line.
EXPECT_EQ(gfx::Rect(10, 40, 33, 9).ToString(),
static_text_accessible2
->GetInnerTextRangeBoundsRect(
0, 3, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
#if BUILDFLAG(IS_ANDROID)
// Android disallows getting inner text from accessibility root nodes.
EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
root_accessible
->GetInnerTextRangeBoundsRect(
0, 6, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
// Android disallows getting inner text from accessibility root nodes.
EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
root_accessible
->GetInnerTextRangeBoundsRect(
2, 4, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
#else
// Validate the bounds of 'ABCABC' from both lines.
EXPECT_EQ(gfx::Rect(0, 20, 43, 29).ToString(),
root_accessible
->GetInnerTextRangeBoundsRect(
0, 6, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
// Validate the bounds of 'CA' from both lines.
EXPECT_EQ(gfx::Rect(10, 20, 23, 29).ToString(),
root_accessible
->GetInnerTextRangeBoundsRect(
2, 4, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
#endif
}
TEST_F(BrowserAccessibilityTest, GetInnerTextRangeBoundsRectBiDi) {
// In this example, we assume that the string "123abc" is rendered with "123"
// going left-to-right and "abc" going right-to-left. In other words,
// on-screen it would look like "123cba".
ui::AXNodeData root;
root.id = 1;
root.role = ax::mojom::Role::kRootWebArea;
root.relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
ui::AXNodeData static_text;
static_text.id = 2;
static_text.role = ax::mojom::Role::kStaticText;
static_text.SetName("123abc");
static_text.relative_bounds.bounds = gfx::RectF(100, 100, 60, 20);
root.child_ids.push_back(2);
ui::AXNodeData inline_text1;
inline_text1.id = 3;
inline_text1.role = ax::mojom::Role::kInlineTextBox;
inline_text1.SetName("123");
inline_text1.relative_bounds.bounds = gfx::RectF(100, 100, 30, 20);
inline_text1.SetTextDirection(ax::mojom::WritingDirection::kLtr);
std::vector<int32_t> character_offsets1;
character_offsets1.push_back(10); // 0
character_offsets1.push_back(20); // 1
character_offsets1.push_back(30); // 2
inline_text1.AddIntListAttribute(
ax::mojom::IntListAttribute::kCharacterOffsets, character_offsets1);
static_text.child_ids.push_back(3);
ui::AXNodeData inline_text2;
inline_text2.id = 4;
inline_text2.role = ax::mojom::Role::kInlineTextBox;
inline_text2.SetName("abc");
inline_text2.relative_bounds.bounds = gfx::RectF(130, 100, 30, 20);
inline_text2.SetTextDirection(ax::mojom::WritingDirection::kRtl);
std::vector<int32_t> character_offsets2;
character_offsets2.push_back(10);
character_offsets2.push_back(20);
character_offsets2.push_back(30);
inline_text2.AddIntListAttribute(
ax::mojom::IntListAttribute::kCharacterOffsets, character_offsets2);
static_text.child_ids.push_back(4);
std::unique_ptr<ui::BrowserAccessibilityManager>
browser_accessibility_manager(CreateBrowserAccessibilityManager(
MakeAXTreeUpdateForTesting(root, static_text, inline_text1,
inline_text2),
node_id_delegate_, test_browser_accessibility_delegate_.get()));
ui::BrowserAccessibility* root_accessible =
browser_accessibility_manager->GetBrowserAccessibilityRoot();
ASSERT_NE(nullptr, root_accessible);
ui::BrowserAccessibility* static_text_accessible =
root_accessible->PlatformGetChild(0);
ASSERT_NE(nullptr, static_text_accessible);
EXPECT_EQ(gfx::Rect(100, 100, 60, 20).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
0, 6, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
EXPECT_EQ(gfx::Rect(100, 100, 10, 20).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
0, 1, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
EXPECT_EQ(gfx::Rect(100, 100, 30, 20).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
0, 3, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
EXPECT_EQ(gfx::Rect(150, 100, 10, 20).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
3, 4, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
EXPECT_EQ(gfx::Rect(130, 100, 30, 20).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
3, 6, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
// This range is only two characters, but because of the direction switch
// the bounds are as wide as four characters.
EXPECT_EQ(gfx::Rect(120, 100, 40, 20).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
2, 4, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
}
TEST_F(BrowserAccessibilityTest, GetInnerTextRangeBoundsRectScrolledWindow) {
ui::AXNodeData root;
root.id = 1;
root.role = ax::mojom::Role::kRootWebArea;
root.AddIntAttribute(ax::mojom::IntAttribute::kScrollX, 25);
root.AddIntAttribute(ax::mojom::IntAttribute::kScrollY, 50);
root.relative_bounds.bounds = gfx::RectF(0, 0, 800, 600);
ui::AXNodeData static_text;
static_text.id = 2;
static_text.role = ax::mojom::Role::kStaticText;
static_text.SetName("ABC");
static_text.relative_bounds.bounds = gfx::RectF(100, 100, 16, 9);
root.child_ids.push_back(2);
ui::AXNodeData inline_text;
inline_text.id = 3;
inline_text.role = ax::mojom::Role::kInlineTextBox;
inline_text.SetName("ABC");
inline_text.relative_bounds.bounds = gfx::RectF(100, 100, 16, 9);
inline_text.SetTextDirection(ax::mojom::WritingDirection::kLtr);
std::vector<int32_t> character_offsets1;
character_offsets1.push_back(6); // 0
character_offsets1.push_back(11); // 1
character_offsets1.push_back(16); // 2
inline_text.AddIntListAttribute(
ax::mojom::IntListAttribute::kCharacterOffsets, character_offsets1);
static_text.child_ids.push_back(3);
std::unique_ptr<ui::BrowserAccessibilityManager>
browser_accessibility_manager(CreateBrowserAccessibilityManager(
MakeAXTreeUpdateForTesting(root, static_text, inline_text),
node_id_delegate_, test_browser_accessibility_delegate_.get()));
browser_accessibility_manager
->SetUseRootScrollOffsetsWhenComputingBoundsForTesting(true);
ui::BrowserAccessibility* root_accessible =
browser_accessibility_manager->GetBrowserAccessibilityRoot();
ASSERT_NE(nullptr, root_accessible);
ui::BrowserAccessibility* static_text_accessible =
root_accessible->PlatformGetChild(0);
ASSERT_NE(nullptr, static_text_accessible);
if (browser_accessibility_manager
->UseRootScrollOffsetsWhenComputingBounds()) {
EXPECT_EQ(gfx::Rect(75, 50, 16, 9).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
0, 3, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
} else {
EXPECT_EQ(gfx::Rect(100, 100, 16, 9).ToString(),
static_text_accessible
->GetInnerTextRangeBoundsRect(
0, 3, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped)
.ToString());
}
}
TEST_F(BrowserAccessibilityTest, GetAuthorUniqueId) {
ui::AXNodeData root;
root.id = 1;
root.role = ax::mojom::Role::kRootWebArea;
root.AddStringAttribute(ax::mojom::StringAttribute::kHtmlId, "my_html_id");
std::unique_ptr<ui::BrowserAccessibilityManager>
browser_accessibility_manager(CreateBrowserAccessibilityManager(
MakeAXTreeUpdateForTesting(root), node_id_delegate_,
test_browser_accessibility_delegate_.get()));
ASSERT_NE(nullptr, browser_accessibility_manager.get());
ui::BrowserAccessibility* root_accessible =
browser_accessibility_manager->GetBrowserAccessibilityRoot();
ASSERT_NE(nullptr, root_accessible);
ASSERT_EQ(u"my_html_id", root_accessible->GetAuthorUniqueId());
}
TEST_F(BrowserAccessibilityTest, NextWordPositionWithHypertext) {
// Build a tree simulating an INPUT control with placeholder text.
ui::AXNodeData root;
root.id = 1;
ui::AXNodeData input;
input.id = 2;
ui::AXNodeData text_container;
text_container.id = 3;
ui::AXNodeData static_text;
static_text.id = 4;
ui::AXNodeData inline_text;
inline_text.id = 5;
root.role = ax::mojom::Role::kRootWebArea;
root.child_ids = {input.id};
input.role = ax::mojom::Role::kTextField;
input.AddState(ax::mojom::State::kEditable);
input.SetName("Search the web");
input.child_ids = {text_container.id};
text_container.role = ax::mojom::Role::kGenericContainer;
text_container.child_ids = {static_text.id};
static_text.role = ax::mojom::Role::kStaticText;
static_text.SetName("Search the web");
static_text.child_ids = {inline_text.id};
inline_text.role = ax::mojom::Role::kInlineTextBox;
inline_text.SetName("Search the web");
inline_text.AddIntListAttribute(ax::mojom::IntListAttribute::kWordStarts,
{0, 7, 11});
inline_text.AddIntListAttribute(ax::mojom::IntListAttribute::kWordEnds,
{6, 10, 14});
std::unique_ptr<ui::BrowserAccessibilityManager>
browser_accessibility_manager(CreateBrowserAccessibilityManager(
MakeAXTreeUpdateForTesting(root, input, text_container, static_text,
inline_text),
node_id_delegate_, test_browser_accessibility_delegate_.get()));
ASSERT_NE(nullptr, browser_accessibility_manager.get());
ui::BrowserAccessibility* root_accessible =
browser_accessibility_manager->GetBrowserAccessibilityRoot();
ASSERT_NE(nullptr, root_accessible);
ASSERT_NE(0u, root_accessible->InternalChildCount());
ui::BrowserAccessibility* input_accessible =
root_accessible->InternalGetChild(0);
ASSERT_NE(nullptr, input_accessible);
// Create a text position at offset 0 in the input control
ui::BrowserAccessibility::AXPosition position =
input_accessible->CreateTextPositionAt(0);
// On platforms that expose IA2 or ATK hypertext, moving by word should work
// the same as if the value of the text field is equal to the placeholder
// text.
//
// This is because visually the placeholder text appears in the text field in
// the same location as its value, and the user should be able to read it
// using standard screen reader commands, such as "read current word" and
// "read current line". Only once the user starts typing should the
// placeholder disappear.
ui::BrowserAccessibility::AXPosition next_word_start =
position->CreateNextWordStartPosition(
{ui::AXBoundaryBehavior::kCrossBoundary,
ui::AXBoundaryDetection::kDontCheckInitialPosition});
if (position->MaxTextOffset() == 0) {
EXPECT_TRUE(next_word_start->IsNullPosition());
} else {
EXPECT_EQ(
"TextPosition anchor_id=2 text_offset=7 affinity=downstream "
"annotated_text=Search <t>he web",
next_word_start->ToString());
}
ui::BrowserAccessibility::AXPosition next_word_end =
position->CreateNextWordEndPosition(
{ui::AXBoundaryBehavior::kCrossBoundary,
ui::AXBoundaryDetection::kDontCheckInitialPosition});
if (position->MaxTextOffset() == 0) {
EXPECT_TRUE(next_word_end->IsNullPosition());
} else {
EXPECT_EQ(
"TextPosition anchor_id=2 text_offset=6 affinity=downstream "
"annotated_text=Search< >the web",
next_word_end->ToString());
}
}
TEST_F(BrowserAccessibilityTest, GetIndexInParent) {
ui::AXNodeData root;
root.id = 1;
root.role = ax::mojom::Role::kRootWebArea;
root.child_ids = {2};
ui::AXNodeData static_text;
static_text.id = 2;
static_text.role = ax::mojom::Role::kStaticText;
static_text.SetName("ABC");
std::unique_ptr<ui::BrowserAccessibilityManager>
browser_accessibility_manager(CreateBrowserAccessibilityManager(
MakeAXTreeUpdateForTesting(root, static_text), node_id_delegate_,
test_browser_accessibility_delegate_.get()));
ASSERT_NE(nullptr, browser_accessibility_manager.get());
ui::BrowserAccessibility* root_accessible =
browser_accessibility_manager->GetBrowserAccessibilityRoot();
ASSERT_NE(nullptr, root_accessible);
// Should be nullopt for kRootWebArea since it doesn't have a calculated
// index.
EXPECT_FALSE(root_accessible->GetIndexInParent().has_value());
ui::BrowserAccessibility* child_accessible =
root_accessible->InternalGetChild(0);
ASSERT_NE(nullptr, child_accessible);
// Returns the index calculated in AXNode.
EXPECT_EQ(0u, child_accessible->GetIndexInParent());
}
TEST_F(BrowserAccessibilityTest, CreatePositionAt) {
ui::AXNodeData root_1;
root_1.id = 1;
root_1.role = ax::mojom::Role::kRootWebArea;
root_1.child_ids = {2};
ui::AXNodeData gc_2;
gc_2.id = 2;
gc_2.role = ax::mojom::Role::kGenericContainer;
gc_2.child_ids = {3};
ui::AXNodeData text_3;
text_3.id = 3;
text_3.role = ax::mojom::Role::kStaticText;
text_3.SetName("text");
std::unique_ptr<ui::BrowserAccessibilityManager>
browser_accessibility_manager(CreateBrowserAccessibilityManager(
MakeAXTreeUpdateForTesting(root_1, gc_2, text_3), node_id_delegate_,
test_browser_accessibility_delegate_.get()));
ASSERT_NE(nullptr, browser_accessibility_manager.get());
ui::BrowserAccessibility* gc_accessible =
browser_accessibility_manager->GetBrowserAccessibilityRoot()
->PlatformGetChild(0);
ASSERT_NE(nullptr, gc_accessible);
ui::BrowserAccessibility::AXPosition pos = gc_accessible->CreatePositionAt(0);
EXPECT_TRUE(pos->IsTreePosition());
ASSERT_EQ(1U, gc_accessible->InternalChildCount());
#if BUILDFLAG(IS_ANDROID)
// On Android, nodes with only static text can drop their children.
ASSERT_EQ(0U, gc_accessible->PlatformChildCount());
#else
ASSERT_EQ(1U, gc_accessible->PlatformChildCount());
ui::BrowserAccessibility* text_accessible =
gc_accessible->PlatformGetChild(0);
ASSERT_NE(nullptr, text_accessible);
pos = text_accessible->CreatePositionAt(0);
EXPECT_TRUE(pos->IsTextPosition());
#endif // BUILDFLAG(IS_ANDROID)
}
} // namespace content