forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcross_platform_accessibility_browsertest.cc
1663 lines (1467 loc) · 65.4 KB
/
cross_platform_accessibility_browsertest.cc
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
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <stdint.h>
#include <string>
#include <unordered_set>
#include <vector>
#include "base/macros.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/timer/elapsed_timer.h"
#include "build/build_config.h"
#include "build/chromecast_buildflags.h"
#include "content/browser/accessibility/browser_accessibility.h"
#include "content/browser/accessibility/browser_accessibility_manager.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/test/accessibility_notification_waiter.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "net/base/escape.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/common/features.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/accessibility/accessibility_switches.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_tree.h"
#include "ui/accessibility/ax_tree_id.h"
#if defined(OS_WIN)
#include "base/win/atl.h"
#include "base/win/scoped_com_initializer.h"
#include "ui/base/win/atl_module.h"
#endif
using ::testing::ElementsAre;
using ::testing::Pair;
#if defined(NDEBUG) && !defined(ADDRESS_SANITIZER) && \
!defined(LEAK_SANITIZER) && !defined(MEMORY_SANITIZER) && \
!defined(THREAD_SANITIZER) && !defined(UNDEFINED_SANITIZER) && \
!defined(OS_ANDROID)
#define IS_FAST_BUILD
constexpr int kDelayForDeferredUpdatesAfterPageLoad = 150;
#endif
namespace content {
class CrossPlatformAccessibilityBrowserTest : public ContentBrowserTest {
public:
CrossPlatformAccessibilityBrowserTest() = default;
~CrossPlatformAccessibilityBrowserTest() override = default;
// Make sure each node in the tree has a unique id.
void RecursiveAssertUniqueIds(const ui::AXNode* node,
std::unordered_set<int>* ids) const {
ASSERT_TRUE(ids->find(node->id()) == ids->end());
ids->insert(node->id());
for (const auto* child : node->children())
RecursiveAssertUniqueIds(child, ids);
}
void SetUp() override;
void SetUpOnMainThread() override;
void SetUpCommandLine(base::CommandLine* command_line) override {
ContentBrowserTest::SetUpCommandLine(command_line);
// kDisableAXMenuList is true on Chrome OS by default. Make it consistent
// for these cross-platform tests.
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kDisableAXMenuList, "false");
}
protected:
// Choose which feature flags to enable or disable.
virtual void ChooseFeatures(std::vector<base::Feature>* enabled_features,
std::vector<base::Feature>* disabled_features);
void ExecuteScript(const char* script) {
shell()->web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(
base::ASCIIToUTF16(script), base::NullCallback());
}
void LoadInitialAccessibilityTreeFromHtml(const std::string& html) {
AccessibilityNotificationWaiter waiter(shell()->web_contents(),
ui::kAXModeComplete,
ax::mojom::Event::kLoadComplete);
GURL html_data_url(
net::EscapeExternalHandlerValue("data:text/html," + html));
ASSERT_TRUE(NavigateToURL(shell(), html_data_url));
waiter.WaitForNotification();
}
void LoadInitialAccessibilityTreeFromHtmlFilePath(
const std::string& html_file_path) {
if (!embedded_test_server()->Started())
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(embedded_test_server()->Started());
AccessibilityNotificationWaiter waiter(shell()->web_contents(),
ui::kAXModeComplete,
ax::mojom::Event::kLoadComplete);
ASSERT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL(html_file_path)));
waiter.WaitForNotification();
}
BrowserAccessibilityManager* GetManager() const {
WebContentsImpl* web_contents =
static_cast<WebContentsImpl*>(shell()->web_contents());
return web_contents->GetRootBrowserAccessibilityManager();
}
const ui::AXTree& GetAXTree() const {
const ui::AXTree* ax_tree = GetManager()->ax_tree();
EXPECT_NE(nullptr, ax_tree);
return *ax_tree;
}
BrowserAccessibility* FindNode(const std::string& name_or_value) {
return FindNodeInSubtree(*GetManager()->GetRoot(), name_or_value);
}
BrowserAccessibility* FindNodeInSubtree(BrowserAccessibility& node,
const std::string& name_or_value) {
const std::string& name =
node.GetStringAttribute(ax::mojom::StringAttribute::kName);
// Note that in the case of a text field,
// "BrowserAccessibility::GetValueForControl" has the added functionality
// of computing the value of an ARIA text box from its inner text.
//
// <div contenteditable="true" role="textbox">Hello world.</div>
// Will expose no HTML value attribute, but some screen readers, such as
// Jaws, VoiceOver and Talkback, require one to be computed.
const std::string& value = base::UTF16ToUTF8(node.GetValueForControl());
if (name == name_or_value || value == name_or_value) {
return &node;
}
for (unsigned int i = 0; i < node.PlatformChildCount(); ++i) {
BrowserAccessibility* result =
FindNodeInSubtree(*node.PlatformGetChild(i), name_or_value);
if (result)
return result;
}
return nullptr;
}
std::string GetAttr(const ui::AXNode* node,
const ax::mojom::StringAttribute attr);
int GetIntAttr(const ui::AXNode* node, const ax::mojom::IntAttribute attr);
bool GetBoolAttr(const ui::AXNode* node, const ax::mojom::BoolAttribute attr);
private:
base::test::ScopedFeatureList scoped_feature_list_;
#if defined(OS_WIN)
std::unique_ptr<base::win::ScopedCOMInitializer> com_initializer_;
#endif
DISALLOW_COPY_AND_ASSIGN(CrossPlatformAccessibilityBrowserTest);
};
void CrossPlatformAccessibilityBrowserTest::SetUp() {
std::vector<base::Feature> enabled_features;
std::vector<base::Feature> disabled_features;
ChooseFeatures(&enabled_features, &disabled_features);
scoped_feature_list_.InitWithFeatures(enabled_features, disabled_features);
// The <input type="color"> popup tested in
// AccessibilityInputColorWithPopupOpen requires the ability to read pixels
// from a Canvas, so we need to be able to produce pixel output.
EnablePixelOutput();
ContentBrowserTest::SetUp();
}
void CrossPlatformAccessibilityBrowserTest::ChooseFeatures(
std::vector<base::Feature>* enabled_features,
std::vector<base::Feature>* disabled_features) {
enabled_features->emplace_back(
features::kEnableAccessibilityExposeHTMLElement);
}
void CrossPlatformAccessibilityBrowserTest::SetUpOnMainThread() {
#if defined(OS_WIN)
com_initializer_ = std::make_unique<base::win::ScopedCOMInitializer>();
ui::win::CreateATLModuleIfNeeded();
#endif
}
// Convenience method to get the value of a particular AXNode
// attribute as a UTF-8 string.
std::string CrossPlatformAccessibilityBrowserTest::GetAttr(
const ui::AXNode* node,
const ax::mojom::StringAttribute attr) {
const ui::AXNodeData& data = node->data();
for (size_t i = 0; i < data.string_attributes.size(); ++i) {
if (data.string_attributes[i].first == attr)
return data.string_attributes[i].second;
}
return std::string();
}
// Convenience method to get the value of a particular AXNode
// integer attribute.
int CrossPlatformAccessibilityBrowserTest::GetIntAttr(
const ui::AXNode* node,
const ax::mojom::IntAttribute attr) {
const ui::AXNodeData& data = node->data();
for (size_t i = 0; i < data.int_attributes.size(); ++i) {
if (data.int_attributes[i].first == attr)
return data.int_attributes[i].second;
}
return -1;
}
// Convenience method to get the value of a particular AXNode
// boolean attribute.
bool CrossPlatformAccessibilityBrowserTest::GetBoolAttr(
const ui::AXNode* node,
const ax::mojom::BoolAttribute attr) {
const ui::AXNodeData& data = node->data();
for (size_t i = 0; i < data.bool_attributes.size(); ++i) {
if (data.bool_attributes[i].first == attr)
return data.bool_attributes[i].second;
}
return false;
}
namespace {
// Convenience method to find a node by its role value.
BrowserAccessibility* FindNodeByRole(BrowserAccessibility* root,
ax::mojom::Role role) {
if (root->GetRole() == role)
return root;
for (uint32_t i = 0; i < root->InternalChildCount(); ++i) {
BrowserAccessibility* child = root->InternalGetChild(i);
DCHECK(child);
if (BrowserAccessibility* result = FindNodeByRole(child, role))
return result;
}
return nullptr;
}
} // namespace
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
WebpageAccessibility) {
const std::string url_str(R"HTML(
<!DOCTYPE html>
<html>
<head>
<title>Accessibility Test</title>
</head>
<body>
<input type="button" value="push">
<input type="checkbox">
</body>
</html>)HTML");
LoadInitialAccessibilityTreeFromHtml(url_str);
const ui::AXTree& tree = GetAXTree();
const ui::AXNode* root = tree.root();
// Check properties of the tree.
EXPECT_EQ(net::EscapeExternalHandlerValue("data:text/html," + url_str),
tree.data().url);
EXPECT_EQ("Accessibility Test", tree.data().title);
EXPECT_EQ("html", tree.data().doctype);
EXPECT_EQ("text/html", tree.data().mimetype);
// Check properties of the root element of the tree.
EXPECT_EQ("Accessibility Test",
GetAttr(root, ax::mojom::StringAttribute::kName));
EXPECT_EQ(ax::mojom::Role::kRootWebArea, root->data().role);
// Check properties of the BODY element.
ASSERT_EQ(1u, root->GetUnignoredChildCount());
const ui::AXNode* body = root->GetUnignoredChildAtIndex(0);
EXPECT_EQ(ax::mojom::Role::kGenericContainer, body->data().role);
EXPECT_EQ("body", GetAttr(body, ax::mojom::StringAttribute::kHtmlTag));
EXPECT_EQ("block", GetAttr(body, ax::mojom::StringAttribute::kDisplay));
// Check properties of the two children of the BODY element.
ASSERT_EQ(2u, body->GetUnignoredChildCount());
const ui::AXNode* button = body->GetUnignoredChildAtIndex(0);
EXPECT_EQ(ax::mojom::Role::kButton, button->data().role);
EXPECT_EQ("input", GetAttr(button, ax::mojom::StringAttribute::kHtmlTag));
EXPECT_EQ("push", GetAttr(button, ax::mojom::StringAttribute::kName));
EXPECT_EQ("inline-block",
GetAttr(button, ax::mojom::StringAttribute::kDisplay));
EXPECT_THAT(button->data().html_attributes,
ElementsAre(Pair("type", "button"), Pair("value", "push")));
const ui::AXNode* checkbox = body->GetUnignoredChildAtIndex(1);
EXPECT_EQ(ax::mojom::Role::kCheckBox, checkbox->data().role);
EXPECT_EQ("input", GetAttr(checkbox, ax::mojom::StringAttribute::kHtmlTag));
EXPECT_EQ("inline-block",
GetAttr(checkbox, ax::mojom::StringAttribute::kDisplay));
EXPECT_THAT(checkbox->data().html_attributes,
ElementsAre(Pair("type", "checkbox")));
}
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
UnselectedEditableTextAccessibility) {
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body>
<input value="Hello, world.">
</body>
</html>)HTML");
const ui::AXTree& tree = GetAXTree();
const ui::AXNode* root = tree.root();
ASSERT_EQ(1u, root->GetUnignoredChildCount());
const ui::AXNode* body = root->GetUnignoredChildAtIndex(0);
ASSERT_EQ(1u, body->GetUnignoredChildCount());
const ui::AXNode* text = body->GetUnignoredChildAtIndex(0);
EXPECT_EQ(ax::mojom::Role::kTextField, text->data().role);
EXPECT_STREQ("input",
GetAttr(text, ax::mojom::StringAttribute::kHtmlTag).c_str());
EXPECT_EQ(0, GetIntAttr(text, ax::mojom::IntAttribute::kTextSelStart));
EXPECT_EQ(0, GetIntAttr(text, ax::mojom::IntAttribute::kTextSelEnd));
EXPECT_STREQ("Hello, world.", text->GetValueForControl().c_str());
// TODO(dmazzoni): as soon as more accessibility code is cross-platform,
// this code should test that the accessible info is dynamically updated
// if the selection or value changes.
}
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
SelectedEditableTextAccessibility) {
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body onload="document.body.children[0].select();">
<input value="Hello, world.">
</body>
</html>)HTML");
const ui::AXTree& tree = GetAXTree();
const ui::AXNode* root = tree.root();
ASSERT_EQ(1u, root->GetUnignoredChildCount());
const ui::AXNode* body = root->GetUnignoredChildAtIndex(0);
ASSERT_EQ(1u, body->GetUnignoredChildCount());
const ui::AXNode* text = body->GetUnignoredChildAtIndex(0);
EXPECT_EQ(ax::mojom::Role::kTextField, text->data().role);
EXPECT_STREQ("input",
GetAttr(text, ax::mojom::StringAttribute::kHtmlTag).c_str());
EXPECT_EQ(0, GetIntAttr(text, ax::mojom::IntAttribute::kTextSelStart));
EXPECT_EQ(13, GetIntAttr(text, ax::mojom::IntAttribute::kTextSelEnd));
EXPECT_STREQ("Hello, world.", text->GetValueForControl().c_str());
}
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
MultipleInheritanceAccessibility2) {
// Here's a html snippet where Blink puts the same node as a child
// of two different parents. Instead of checking the exact output, just
// make sure that no id is reused in the resulting tree.
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<script>
document.writeln('<q><section></section></q><q><li>');
setTimeout(function() {
document.close();
}, 1);
</script>
</html>)HTML");
const ui::AXTree& tree = GetAXTree();
const ui::AXNode* root = tree.root();
std::unordered_set<int> ids;
RecursiveAssertUniqueIds(root, &ids);
}
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
IframeAccessibility) {
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body>
<button>Button 1</button>
<iframe srcdoc="
<!DOCTYPE html>
<html>
<body>
<button>Button 2</button>
</body>
</html>
"></iframe>
<button>Button 3</button>
</body>
</html>)HTML");
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"Button 2");
const ui::AXTree& tree = GetAXTree();
const ui::AXNode* root = tree.root();
ASSERT_EQ(1u, root->children().size());
const ui::AXNode* html_element = root->children()[0];
ASSERT_EQ(1u, html_element->GetUnignoredChildCount());
const ui::AXNode* body = html_element->children()[0];
ASSERT_EQ(3u, body->GetUnignoredChildCount());
const ui::AXNode* button1 = body->GetUnignoredChildAtIndex(0);
EXPECT_EQ(ax::mojom::Role::kButton, button1->data().role);
EXPECT_STREQ("Button 1",
GetAttr(button1, ax::mojom::StringAttribute::kName).c_str());
const ui::AXNode* iframe = body->GetUnignoredChildAtIndex(1);
EXPECT_STREQ("iframe",
GetAttr(iframe, ax::mojom::StringAttribute::kHtmlTag).c_str());
// Iframes loaded via the "srcdoc" attribute, (or the now deprecated method of
// "src=data:text/html,..."), create a new origin context and are thus loaded
// into a separate accessibility tree. (See "out-of-process cross-origin
// iframes in Chromium documentation.)
ASSERT_EQ(0u, iframe->children().size());
const ui::AXTreeID iframe_tree_id = AXTreeID::FromString(
GetAttr(iframe, ax::mojom::StringAttribute::kChildTreeId));
const BrowserAccessibilityManager* iframe_manager =
BrowserAccessibilityManager::FromID(iframe_tree_id);
ASSERT_NE(nullptr, iframe_manager);
const ui::AXNode* sub_document = iframe_manager->GetRootAsAXNode();
EXPECT_EQ(ax::mojom::Role::kRootWebArea, sub_document->data().role);
ASSERT_EQ(1u, sub_document->children().size());
const ui::AXNode* sub_html_element = sub_document->children()[0];
ASSERT_EQ(1u, sub_html_element->GetUnignoredChildCount());
const ui::AXNode* sub_body = sub_html_element->children()[0];
ASSERT_EQ(1u, sub_body->GetUnignoredChildCount());
const ui::AXNode* button2 = sub_body->GetUnignoredChildAtIndex(0);
EXPECT_EQ(ax::mojom::Role::kButton, button2->data().role);
EXPECT_STREQ("Button 2",
GetAttr(button2, ax::mojom::StringAttribute::kName).c_str());
const ui::AXNode* button3 = body->GetUnignoredChildAtIndex(2);
EXPECT_EQ(ax::mojom::Role::kButton, button3->data().role);
EXPECT_STREQ("Button 3",
GetAttr(button3, ax::mojom::StringAttribute::kName).c_str());
}
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
PlatformIframeAccessibility) {
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body>
<button>Button 1</button>
<iframe srcdoc="
<!DOCTYPE html>
<html>
<body>
<button>Button 2</button>
</body>
</html>
"></iframe>
<button>Button 3</button>
</body>
</html>)HTML");
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"Button 2");
const BrowserAccessibility* root = GetManager()->GetRoot();
ASSERT_EQ(1U, root->PlatformChildCount());
const BrowserAccessibility* body = root->PlatformGetChild(0);
ASSERT_EQ(3U, body->PlatformChildCount());
const BrowserAccessibility* button1 = body->PlatformGetChild(0);
EXPECT_EQ(ax::mojom::Role::kButton, button1->GetData().role);
EXPECT_STREQ(
"Button 1",
GetAttr(button1->node(), ax::mojom::StringAttribute::kName).c_str());
const BrowserAccessibility* iframe = body->PlatformGetChild(1);
EXPECT_STREQ(
"iframe",
GetAttr(iframe->node(), ax::mojom::StringAttribute::kHtmlTag).c_str());
EXPECT_EQ(1U, iframe->PlatformChildCount());
const BrowserAccessibility* sub_document = iframe->PlatformGetChild(0);
EXPECT_EQ(ax::mojom::Role::kRootWebArea, sub_document->GetData().role);
ASSERT_EQ(1U, sub_document->PlatformChildCount());
const BrowserAccessibility* sub_body = sub_document->PlatformGetChild(0);
ASSERT_EQ(1U, sub_body->PlatformChildCount());
const BrowserAccessibility* button2 = sub_body->PlatformGetChild(0);
EXPECT_EQ(ax::mojom::Role::kButton, button2->GetData().role);
EXPECT_STREQ(
"Button 2",
GetAttr(button2->node(), ax::mojom::StringAttribute::kName).c_str());
const BrowserAccessibility* button3 = body->PlatformGetChild(2);
EXPECT_EQ(ax::mojom::Role::kButton, button3->GetData().role);
EXPECT_STREQ(
"Button 3",
GetAttr(button3->node(), ax::mojom::StringAttribute::kName).c_str());
}
// Android's text representation is different, so disable the test there.
#if !defined(OS_ANDROID)
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
AXNodePositionTreeBoundary) {
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body>Text before iframe<iframe srcdoc="
<!DOCTYPE html>
<html>
<body>Text in iframe
</body>
</html>">
</iframe>Text after iframe</body>
</html>)HTML");
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"Text in iframe");
const BrowserAccessibility* root = GetManager()->GetRoot();
ASSERT_NE(root, nullptr);
const BrowserAccessibility* body = root->PlatformGetChild(0);
ASSERT_NE(body, nullptr);
const BrowserAccessibility* text_before_iframe =
FindNode("Text before iframe");
ASSERT_NE(text_before_iframe, nullptr);
const BrowserAccessibility* iframe = body->PlatformGetChild(1);
ASSERT_NE(iframe, nullptr);
const BrowserAccessibility* sub_document = iframe->PlatformGetChild(0);
ASSERT_NE(sub_document, nullptr);
const BrowserAccessibility* sub_body = sub_document->PlatformGetChild(0);
ASSERT_NE(sub_body, nullptr);
const BrowserAccessibility* text_in_iframe = FindNode("Text in iframe");
ASSERT_NE(text_in_iframe, nullptr);
const BrowserAccessibility* text_after_iframe = FindNode("Text after iframe");
ASSERT_NE(text_after_iframe, nullptr);
// Start at the beginning of the document. Anchor IDs can vary across
// platforms and test runs, so only check text offsets and tree IDs. In this
// case, the tree ID of position should match test_position since a tree
// boundary is not crossed.
ui::AXNodePosition::AXPositionInstance position =
text_before_iframe->CreateTextPositionAt(1);
EXPECT_EQ(position->text_offset(), 1);
EXPECT_FALSE(position->AtStartOfAXTree());
EXPECT_FALSE(position->AtEndOfAXTree());
ui::AXNodePosition::AXPositionInstance test_position =
position->CreatePositionAtStartOfAXTree();
EXPECT_EQ(test_position->tree_id(), position->tree_id());
EXPECT_EQ(test_position->text_offset(), 0);
EXPECT_TRUE(test_position->AtStartOfAXTree());
EXPECT_FALSE(test_position->AtEndOfAXTree());
test_position = position->CreatePositionAtEndOfAXTree();
EXPECT_EQ(test_position->tree_id(), position->tree_id());
EXPECT_EQ(test_position->text_offset(), 17);
EXPECT_FALSE(test_position->AtStartOfAXTree());
EXPECT_TRUE(test_position->AtEndOfAXTree());
// Test inside iframe.
position = text_in_iframe->CreateTextPositionAt(3);
EXPECT_EQ(position->text_offset(), 3);
EXPECT_NE(test_position->tree_id(), position->tree_id());
EXPECT_FALSE(position->AtStartOfAXTree());
EXPECT_FALSE(position->AtEndOfAXTree());
test_position = position->CreatePositionAtStartOfAXTree();
EXPECT_TRUE(test_position->AtStartOfAXTree());
EXPECT_FALSE(test_position->AtEndOfAXTree());
EXPECT_EQ(test_position->tree_id(), position->tree_id());
EXPECT_EQ(test_position->text_offset(), 0);
test_position = position->CreatePositionAtEndOfAXTree();
EXPECT_EQ(test_position->tree_id(), position->tree_id());
EXPECT_EQ(test_position->text_offset(), 14);
EXPECT_FALSE(test_position->AtStartOfAXTree());
EXPECT_TRUE(test_position->AtEndOfAXTree());
// Test after iframe.
position = text_after_iframe->CreateTextPositionAt(3);
EXPECT_FALSE(position->AtStartOfAXTree());
EXPECT_FALSE(position->AtEndOfAXTree());
EXPECT_NE(test_position->tree_id(), position->tree_id());
test_position = position->CreatePositionAtStartOfAXTree();
EXPECT_EQ(test_position->tree_id(), position->tree_id());
EXPECT_EQ(test_position->text_offset(), 0);
EXPECT_TRUE(test_position->AtStartOfAXTree());
EXPECT_FALSE(test_position->AtEndOfAXTree());
test_position = position->CreatePositionAtEndOfAXTree();
EXPECT_EQ(test_position->tree_id(), position->tree_id());
EXPECT_EQ(test_position->text_offset(), 17);
EXPECT_FALSE(test_position->AtStartOfAXTree());
EXPECT_TRUE(test_position->AtEndOfAXTree());
}
#endif
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
PlatformIterator) {
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body>
<button>Button 1</button>
<iframe srcdoc="
<!DOCTYPE html>
<html>
<body>
<button>Button 2</button>
<button>Button 3</button>
</body>
</html>">
</iframe>
<button>Button 4</button>
</body>
</html>)HTML");
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"Button 2");
const BrowserAccessibility* root = GetManager()->GetRoot();
BrowserAccessibility::PlatformChildIterator it =
root->PlatformChildrenBegin();
EXPECT_EQ(ax::mojom::Role::kGenericContainer, (*it).GetData().role);
it = (*it).PlatformChildrenBegin();
EXPECT_STREQ(
"Button 1",
GetAttr((*it).node(), ax::mojom::StringAttribute::kName).c_str());
++it;
EXPECT_STREQ(
"iframe",
GetAttr((*it).node(), ax::mojom::StringAttribute::kHtmlTag).c_str());
EXPECT_EQ(1U, (*it).PlatformChildCount());
auto iframe_iterator = (*it).PlatformChildrenBegin();
EXPECT_EQ(ax::mojom::Role::kRootWebArea, (*iframe_iterator).GetData().role);
iframe_iterator = (*iframe_iterator).PlatformChildrenBegin();
EXPECT_EQ(ax::mojom::Role::kGenericContainer,
(*iframe_iterator).GetData().role);
iframe_iterator = (*iframe_iterator).PlatformChildrenBegin();
EXPECT_STREQ("Button 2", GetAttr((*iframe_iterator).node(),
ax::mojom::StringAttribute::kName)
.c_str());
++iframe_iterator;
EXPECT_STREQ("Button 3", GetAttr((*iframe_iterator).node(),
ax::mojom::StringAttribute::kName)
.c_str());
++it;
EXPECT_STREQ(
"Button 4",
GetAttr((*it).node(), ax::mojom::StringAttribute::kName).c_str());
}
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
DuplicateChildrenAccessibility) {
// Here's another html snippet where WebKit has a parent node containing
// two duplicate child nodes. Instead of checking the exact output, just
// make sure that no id is reused in the resulting tree.
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body>
<em>
<code >
<h4 >
</em>
</body>
</html>)HTML");
const ui::AXTree& tree = GetAXTree();
const ui::AXNode* root = tree.root();
std::unordered_set<int> ids;
RecursiveAssertUniqueIds(root, &ids);
}
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest, WritableElement) {
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body>
<div role="textbox" tabindex="0">
Some text
</div>
</body>
</html>)HTML");
const ui::AXTree& tree = GetAXTree();
const ui::AXNode* root = tree.root();
ASSERT_EQ(1u, root->GetUnignoredChildCount());
const ui::AXNode* textbox = root->GetUnignoredChildAtIndex(0);
EXPECT_TRUE(textbox->data().HasAction(ax::mojom::Action::kSetValue));
}
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
AriaSortDirection) {
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body>
<table>
<tr>
<th scope="row" aria-sort="ascending">row header 1</th>
<th scope="row" aria-sort="descending">row header 2</th>
<th scope="col" aria-sort="custom">col header 1</th>
<th scope="col" aria-sort="none">col header 2</th>
<th scope="col">col header 3</th>
</tr>
</table>
</body>
</html>)HTML");
const ui::AXTree& tree = GetAXTree();
const ui::AXNode* root = tree.root();
const ui::AXNode* table = root->GetUnignoredChildAtIndex(0);
EXPECT_EQ(ax::mojom::Role::kTable, table->data().role);
EXPECT_EQ(1u, table->GetUnignoredChildCount());
const ui::AXNode* row = table->GetUnignoredChildAtIndex(0);
EXPECT_EQ(5u, row->GetUnignoredChildCount());
const ui::AXNode* header1 = row->GetUnignoredChildAtIndex(0);
const ui::AXNode* header2 = row->GetUnignoredChildAtIndex(1);
const ui::AXNode* header3 = row->GetUnignoredChildAtIndex(2);
const ui::AXNode* header4 = row->GetUnignoredChildAtIndex(3);
const ui::AXNode* header5 = row->GetUnignoredChildAtIndex(4);
EXPECT_EQ(static_cast<int>(ax::mojom::SortDirection::kAscending),
GetIntAttr(header1, ax::mojom::IntAttribute::kSortDirection));
EXPECT_EQ(static_cast<int>(ax::mojom::SortDirection::kDescending),
GetIntAttr(header2, ax::mojom::IntAttribute::kSortDirection));
EXPECT_EQ(static_cast<int>(ax::mojom::SortDirection::kOther),
GetIntAttr(header3, ax::mojom::IntAttribute::kSortDirection));
EXPECT_EQ(-1, GetIntAttr(header4, ax::mojom::IntAttribute::kSortDirection));
EXPECT_EQ(-1, GetIntAttr(header5, ax::mojom::IntAttribute::kSortDirection));
}
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
LocalizedLandmarkType) {
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body>
<header aria-label="header"></header>
<aside aria-label="aside"></aside>
<footer aria-label="footer"></footer>
<form aria-label="form"></form>
<main aria-label="main"></main>
<nav aria-label="nav"></nav>
<section></section>
<section aria-label="section"></section>
<div role="banner" aria-label="banner"></div>
<div role="complementary" aria-label="complementary"></div>
<div role="contentinfo" aria-label="contentinfo"></div>
<div role="form" aria-label="role_form"></div>
<div role="main" aria-label="role_main"></div>
<div role="navigation" aria-label="role_nav"></div>
<div role="region"></div>
<div role="region" aria-label="region"></div>
<div role="search" aria-label="search"></div>
</body>
</html>)HTML");
BrowserAccessibility* root = GetManager()->GetRoot();
ASSERT_NE(nullptr, root);
ASSERT_EQ(17u, root->PlatformChildCount());
auto TestLocalizedLandmarkType =
[root](int child_index, ax::mojom::Role expected_role,
const std::string& expected_name,
const base::string16& expected_localized_landmark_type = {}) {
BrowserAccessibility* node = root->PlatformGetChild(child_index);
ASSERT_NE(nullptr, node);
EXPECT_EQ(expected_role, node->GetRole());
EXPECT_EQ(expected_name,
node->GetStringAttribute(ax::mojom::StringAttribute::kName));
EXPECT_EQ(expected_localized_landmark_type,
node->GetLocalizedStringForLandmarkType());
};
// For testing purposes, assume we get en-US localized strings.
TestLocalizedLandmarkType(0, ax::mojom::Role::kHeader, "header",
base::ASCIIToUTF16("banner"));
TestLocalizedLandmarkType(1, ax::mojom::Role::kComplementary, "aside",
base::ASCIIToUTF16("complementary"));
TestLocalizedLandmarkType(2, ax::mojom::Role::kFooter, "footer",
base::ASCIIToUTF16("content information"));
TestLocalizedLandmarkType(3, ax::mojom::Role::kForm, "form");
TestLocalizedLandmarkType(4, ax::mojom::Role::kMain, "main");
TestLocalizedLandmarkType(5, ax::mojom::Role::kNavigation, "nav");
TestLocalizedLandmarkType(6, ax::mojom::Role::kSection, "");
TestLocalizedLandmarkType(7, ax::mojom::Role::kSection, "section",
base::ASCIIToUTF16("region"));
TestLocalizedLandmarkType(8, ax::mojom::Role::kBanner, "banner",
base::ASCIIToUTF16("banner"));
TestLocalizedLandmarkType(9, ax::mojom::Role::kComplementary, "complementary",
base::ASCIIToUTF16("complementary"));
TestLocalizedLandmarkType(10, ax::mojom::Role::kContentInfo, "contentinfo",
base::ASCIIToUTF16("content information"));
TestLocalizedLandmarkType(11, ax::mojom::Role::kForm, "role_form");
TestLocalizedLandmarkType(12, ax::mojom::Role::kMain, "role_main");
TestLocalizedLandmarkType(13, ax::mojom::Role::kNavigation, "role_nav");
TestLocalizedLandmarkType(14, ax::mojom::Role::kRegion, "");
TestLocalizedLandmarkType(15, ax::mojom::Role::kRegion, "region",
base::ASCIIToUTF16("region"));
TestLocalizedLandmarkType(16, ax::mojom::Role::kSearch, "search");
}
// TODO(https://crbug.com/1020456) re-enable when crashing on linux is resolved.
#if defined(OS_LINUX) || defined(OS_CHROMEOS)
#define MAYBE_LocalizedRoleDescription DISABLED_LocalizedRoleDescription
#else
#define MAYBE_LocalizedRoleDescription LocalizedRoleDescription
#endif
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
MAYBE_LocalizedRoleDescription) {
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body>
<article></article>
<audio controls></audio>
<details></details>
<figure></figure>
<footer></footer>
<header></header>
<input>
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="tel">
<input type="url">
<input type="week">
<mark></mark>
<meter></meter>
<output></output>
<section></section>
<section aria-label="section"></section>
<time></time>
<div role="contentinfo" aria-label="contentinfo"></div>
</body>
</html>)HTML");
BrowserAccessibility* root = GetManager()->GetRoot();
ASSERT_NE(nullptr, root);
ASSERT_EQ(21u, root->PlatformChildCount());
auto TestLocalizedRoleDescription =
[root](int child_index,
const base::string16& expected_localized_role_description = {}) {
BrowserAccessibility* node = root->PlatformGetChild(child_index);
ASSERT_NE(nullptr, node);
EXPECT_EQ(expected_localized_role_description,
node->GetLocalizedStringForRoleDescription());
};
// For testing purposes, assume we get en-US localized strings.
TestLocalizedRoleDescription(0, base::ASCIIToUTF16("article"));
TestLocalizedRoleDescription(1, base::ASCIIToUTF16("audio"));
TestLocalizedRoleDescription(2, base::ASCIIToUTF16("details"));
TestLocalizedRoleDescription(3, base::ASCIIToUTF16("figure"));
TestLocalizedRoleDescription(4, base::ASCIIToUTF16("footer"));
TestLocalizedRoleDescription(5, base::ASCIIToUTF16("header"));
TestLocalizedRoleDescription(6, base::ASCIIToUTF16(""));
TestLocalizedRoleDescription(7, base::ASCIIToUTF16("color picker"));
TestLocalizedRoleDescription(8, base::ASCIIToUTF16("date picker"));
TestLocalizedRoleDescription(
9, base::ASCIIToUTF16("local date and time picker"));
TestLocalizedRoleDescription(10, base::ASCIIToUTF16("email"));
TestLocalizedRoleDescription(11, base::ASCIIToUTF16("telephone"));
TestLocalizedRoleDescription(12, base::ASCIIToUTF16("url"));
TestLocalizedRoleDescription(13, base::ASCIIToUTF16("week picker"));
TestLocalizedRoleDescription(14, base::ASCIIToUTF16("highlight"));
TestLocalizedRoleDescription(15, base::ASCIIToUTF16("meter"));
TestLocalizedRoleDescription(16, base::ASCIIToUTF16("output"));
TestLocalizedRoleDescription(17, base::ASCIIToUTF16(""));
TestLocalizedRoleDescription(18, base::ASCIIToUTF16("section"));
TestLocalizedRoleDescription(19, base::ASCIIToUTF16("time"));
TestLocalizedRoleDescription(20, base::ASCIIToUTF16("content information"));
}
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
GetStyleNameAttributeAsLocalizedString) {
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body>
<p>text <mark>mark text</mark></p>
</body>
</html>)HTML");
BrowserAccessibility* root = GetManager()->GetRoot();
ASSERT_NE(nullptr, root);
ASSERT_EQ(1u, root->PlatformChildCount());
auto TestGetStyleNameAttributeAsLocalizedString =
[](BrowserAccessibility* node, ax::mojom::Role expected_role,
const base::string16& expected_localized_style_name_attribute = {}) {
ASSERT_NE(nullptr, node);
EXPECT_EQ(expected_role, node->GetRole());
EXPECT_EQ(expected_localized_style_name_attribute,
node->GetStyleNameAttributeAsLocalizedString());
};
// For testing purposes, assume we get en-US localized strings.
BrowserAccessibility* para_node = root->PlatformGetChild(0);
ASSERT_EQ(2u, para_node->PlatformChildCount());
TestGetStyleNameAttributeAsLocalizedString(para_node,
ax::mojom::Role::kParagraph);
BrowserAccessibility* text_node = para_node->PlatformGetChild(0);
ASSERT_EQ(0u, text_node->PlatformChildCount());
TestGetStyleNameAttributeAsLocalizedString(text_node,
ax::mojom::Role::kStaticText);
BrowserAccessibility* mark_node = para_node->PlatformGetChild(1);
TestGetStyleNameAttributeAsLocalizedString(mark_node, ax::mojom::Role::kMark,
base::ASCIIToUTF16("highlight"));
// Android doesn't always have a child in this case.
if (mark_node->PlatformChildCount() > 0u) {
BrowserAccessibility* mark_text_node = mark_node->PlatformGetChild(0);
ASSERT_EQ(0u, mark_text_node->PlatformChildCount());
TestGetStyleNameAttributeAsLocalizedString(mark_text_node,
ax::mojom::Role::kStaticText,
base::ASCIIToUTF16("highlight"));
}
}
IN_PROC_BROWSER_TEST_F(CrossPlatformAccessibilityBrowserTest,
TooltipStringAttributeMutuallyExclusiveOfNameFromTitle) {
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body>
<input type="text" title="title">
<input type="text" title="title" aria-labelledby="inputlabel">
<div id="inputlabel">aria-labelledby</div>
</body>
</html>)HTML");
const ui::AXTree& tree = GetAXTree();
const ui::AXNode* root = tree.root();
const ui::AXNode* input1 = root->GetUnignoredChildAtIndex(0);
const ui::AXNode* input2 = root->GetUnignoredChildAtIndex(1);
EXPECT_EQ(static_cast<int>(ax::mojom::NameFrom::kTitle),
GetIntAttr(input1, ax::mojom::IntAttribute::kNameFrom));
EXPECT_STREQ("title",
GetAttr(input1, ax::mojom::StringAttribute::kName).c_str());
EXPECT_STREQ("",
GetAttr(input1, ax::mojom::StringAttribute::kTooltip).c_str());
EXPECT_EQ(static_cast<int>(ax::mojom::NameFrom::kRelatedElement),
GetIntAttr(input2, ax::mojom::IntAttribute::kNameFrom));
EXPECT_STREQ("aria-labelledby",
GetAttr(input2, ax::mojom::StringAttribute::kName).c_str());
EXPECT_STREQ("title",
GetAttr(input2, ax::mojom::StringAttribute::kTooltip).c_str());
}
IN_PROC_BROWSER_TEST_F(
CrossPlatformAccessibilityBrowserTest,
PlaceholderStringAttributeMutuallyExclusiveOfNameFromPlaceholder) {
LoadInitialAccessibilityTreeFromHtml(R"HTML(
<!DOCTYPE html>
<html>
<body>
<fieldset>
<input type="text" placeholder="placeholder">
<input type="text" placeholder="placeholder" aria-label="label">
</fieldset>
</body>
</html>)HTML");
const ui::AXTree& tree = GetAXTree();