-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
accessibility_win_browsertest.cc
5561 lines (4856 loc) · 221 KB
/
accessibility_win_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 <objbase.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <wrl/client.h>
#include <memory>
#include <vector>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/process/process_handle.h"
#include "base/strings/pattern.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/win/scoped_bstr.h"
#include "base/win/scoped_safearray.h"
#include "base/win/scoped_variant.h"
#include "build/build_config.h"
#include "content/browser/accessibility/accessibility_browsertest.h"
#include "content/browser/accessibility/browser_accessibility_manager_win.h"
#include "content/browser/accessibility/browser_accessibility_state_impl.h"
#include "content/browser/accessibility/browser_accessibility_win.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_view_aura.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/browser/web_contents/web_contents_view_aura.h"
#include "content/public/browser/ax_inspect_factory.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/accessibility_notification_waiter.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "net/base/escape.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/iaccessible2/ia2_api_all.h"
#include "third_party/isimpledom/ISimpleDOMNode.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/accessibility/accessibility_switches.h"
#include "ui/accessibility/ax_event_generator.h"
#include "ui/accessibility/platform/ax_fragment_root_win.h"
#include "ui/accessibility/platform/inspect/ax_inspect_utils_win.h"
#include "ui/accessibility/platform/uia_registrar_win.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
namespace content {
using ui::IAccessible2RoleToString;
using ui::IAccessibleRoleToString;
using ui::IAccessibleStateToString;
namespace {
// AccessibilityWinBrowserTest ------------------------------------------------
class AccessibilityWinBrowserTest : public AccessibilityBrowserTest {
public:
AccessibilityWinBrowserTest();
AccessibilityWinBrowserTest(const AccessibilityWinBrowserTest&) = delete;
AccessibilityWinBrowserTest& operator=(const AccessibilityWinBrowserTest&) =
delete;
~AccessibilityWinBrowserTest() override;
protected:
class AccessibleChecker;
std::string PrintAXTree() const;
void SetUpInputField(Microsoft::WRL::ComPtr<IAccessibleText>* input_text);
void SetUpScrollableInputField(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text);
void SetUpScrollableInputTypeSearchField(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text);
void SetUpSingleCharInputField(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text);
void SetUpSingleCharInputFieldWithPlaceholder(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text);
void SetUpSingleCharTextarea(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text);
void SetUpSingleCharContenteditable(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text);
void SetUpSingleCharRtlInputField(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text);
void SetUpTextareaField(
Microsoft::WRL::ComPtr<IAccessibleText>* textarea_text);
void SetUpSampleParagraph(
Microsoft::WRL::ComPtr<IAccessibleText>* accessible_text,
ui::AXMode accessibility_mode = ui::kAXModeComplete);
void SetUpSampleParagraphInScrollableDocument(
Microsoft::WRL::ComPtr<IAccessibleText>* accessible_text,
ui::AXMode accessibility_mode = ui::kAXModeComplete);
void SetUpVeryTallPadding(
Microsoft::WRL::ComPtr<IAccessibleText>* accessible_text,
ui::AXMode accessibility_mode = ui::kAXModeComplete);
void SetUpVeryTallMargin(
Microsoft::WRL::ComPtr<IAccessibleText>* accessible_text,
ui::AXMode accessibility_mode = ui::kAXModeComplete);
void SetUpSampleParagraphInScrollableEditable(
Microsoft::WRL::ComPtr<IAccessibleText>* accessible_text);
BrowserAccessibility* FindNode(ax::mojom::Role role,
const std::string& name_or_value);
BrowserAccessibilityManager* GetManager();
static Microsoft::WRL::ComPtr<IAccessible> GetAccessibleFromVariant(
IAccessible* parent,
VARIANT* var);
static HRESULT QueryIAccessible2(IAccessible* accessible,
IAccessible2** accessible2);
static void FindNodeInAccessibilityTree(IAccessible* node,
int32_t expected_role,
const std::wstring& expected_name,
int32_t depth,
bool* found);
static void CheckTextAtOffset(
const Microsoft::WRL::ComPtr<IAccessibleText>& object,
LONG offset,
IA2TextBoundaryType boundary_type,
LONG expected_start_offset,
LONG expected_end_offset,
const std::wstring& expected_text);
static std::vector<base::win::ScopedVariant> GetAllAccessibleChildren(
IAccessible* element);
private:
void SetUpInputFieldHelper(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text);
void SetUpSampleParagraphHelper(
Microsoft::WRL::ComPtr<IAccessibleText>* accessible_text);
BrowserAccessibility* FindNodeInSubtree(BrowserAccessibility& node,
ax::mojom::Role role,
const std::string& name_or_value);
};
AccessibilityWinBrowserTest::AccessibilityWinBrowserTest() = default;
AccessibilityWinBrowserTest::~AccessibilityWinBrowserTest() = default;
std::string AccessibilityWinBrowserTest::PrintAXTree() const {
std::unique_ptr<ui::AXTreeFormatter> formatter(
AXInspectFactory::CreatePlatformFormatter());
DCHECK(formatter);
formatter->set_show_ids(true);
formatter->SetPropertyFilters(
{ui::AXPropertyFilter("*", ui::AXPropertyFilter::ALLOW)});
return formatter->Format(GetRootAccessibilityNode(shell()->web_contents()));
}
// Loads a page with an input text field and places sample text in it. Also,
// places the caret before the last character.
void AccessibilityWinBrowserTest::SetUpInputField(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text) {
ASSERT_NE(nullptr, input_text);
LoadInputField();
SetUpInputFieldHelper(input_text);
}
// Loads a page with an input text field and places sample text in it that
// overflows its width. Also, places the caret before the last character.
void AccessibilityWinBrowserTest::SetUpScrollableInputField(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text) {
ASSERT_NE(nullptr, input_text);
LoadInitialAccessibilityTreeFromHtml(
std::string(
R"HTML(<!DOCTYPE html>
<html>
<body>
<input type="text" style="width: 150px;" value=")HTML") +
net::EscapeForHTML(InputContentsString()) + std::string(R"HTML(">
</body>
</html>)HTML"));
SetUpInputFieldHelper(input_text);
}
// Loads a page with an input text field and places sample text in it that
// overflows its width. Also, places the caret before the last character.
void AccessibilityWinBrowserTest::SetUpScrollableInputTypeSearchField(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text) {
ASSERT_NE(nullptr, input_text);
LoadInitialAccessibilityTreeFromHtml(
std::string(
R"HTML(<!DOCTYPE html>
<html>
<body>
<input type="search" style="width: 150px;" value=")HTML") +
net::EscapeForHTML(InputContentsString()) + std::string(R"HTML(">
</body>
</html>)HTML"));
SetUpInputFieldHelper(input_text);
}
// Loads a page with an input text field and places a single character in it.
// Also tests with padding, in order to ensure character extent of empty field
// does not erroneously include padding.
void AccessibilityWinBrowserTest::SetUpSingleCharInputField(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text) {
ASSERT_NE(nullptr, input_text);
LoadInitialAccessibilityTreeFromHtml(std::string(
R"HTML(<!DOCTYPE html>
<html>
<body>
<form>
<input type="text" value="x" style="padding:3px">
</form>
</body>
</html>)HTML"));
SetUpInputFieldHelper(input_text);
}
void AccessibilityWinBrowserTest::SetUpSingleCharInputFieldWithPlaceholder(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text) {
ASSERT_NE(nullptr, input_text);
LoadInitialAccessibilityTreeFromHtml(std::string(
R"HTML(<!DOCTYPE html>
<html>
<body>
<form>
<input type="text" value="x" placeholder="placeholder">
</form>
</body>
</html>)HTML"));
SetUpInputFieldHelper(input_text);
}
void AccessibilityWinBrowserTest::SetUpSingleCharTextarea(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text) {
ASSERT_NE(nullptr, input_text);
LoadInitialAccessibilityTreeFromHtml(std::string(
R"HTML(<!DOCTYPE html>
<html>
<body>
<form>
<textarea rows="3" cols="10">x</textarea>
</form>
</body>
</html>)HTML"));
SetUpInputFieldHelper(input_text);
}
// Loads a page with a right-to-left input text field and places a single
// character in it.
void AccessibilityWinBrowserTest::SetUpSingleCharRtlInputField(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text) {
ASSERT_NE(nullptr, input_text);
LoadInitialAccessibilityTreeFromHtml(std::string(
R"HTML(<!DOCTYPE html>
<html>
<body>
<form>
<input type="text" id="textField" name="name" dir="rtl" value="x">
</form>
</body>
</html>)HTML"));
SetUpInputFieldHelper(input_text);
}
void AccessibilityWinBrowserTest::SetUpInputFieldHelper(
Microsoft::WRL::ComPtr<IAccessibleText>* input_text) {
Microsoft::WRL::ComPtr<IAccessible> document(GetRendererAccessible());
std::vector<base::win::ScopedVariant> document_children =
GetAllAccessibleChildren(document.Get());
ASSERT_EQ(1u, document_children.size());
Microsoft::WRL::ComPtr<IAccessible2> div;
ASSERT_HRESULT_SUCCEEDED(QueryIAccessible2(
GetAccessibleFromVariant(document.Get(), document_children[0].AsInput())
.Get(),
&div));
std::vector<base::win::ScopedVariant> div_children =
GetAllAccessibleChildren(div.Get());
ASSERT_LT(0u, div_children.size());
// The input field is always the last child.
Microsoft::WRL::ComPtr<IAccessible2> input;
ASSERT_HRESULT_SUCCEEDED(QueryIAccessible2(
GetAccessibleFromVariant(div.Get(),
div_children[div_children.size() - 1].AsInput())
.Get(),
&input));
LONG input_role = 0;
ASSERT_HRESULT_SUCCEEDED(input->role(&input_role));
ASSERT_EQ(ROLE_SYSTEM_TEXT, input_role);
// Retrieve the IAccessibleText interface for the field.
input_text->Reset();
ASSERT_HRESULT_SUCCEEDED(input.As(input_text));
// Set the caret before the last character.
AccessibilityNotificationWaiter waiter(
shell()->web_contents(), ui::kAXModeComplete,
ax::mojom::Event::kTextSelectionChanged);
std::wstring caret_offset =
base::NumberToWString(InputContentsString().size() - 1);
ExecuteScript(base::WideToUTF16(
base::StrCat({L"let textField = document.querySelector('input,textarea');"
L"textField.focus();"
L"textField.setSelectionRange(",
caret_offset, L",", caret_offset,
L");"
L"textField.scrollLeft = 1000;"})));
waiter.WaitForNotification();
}
// Loads a page with a textarea text field and places sample text in it. Also,
// places the caret before the last character.
void AccessibilityWinBrowserTest::SetUpTextareaField(
Microsoft::WRL::ComPtr<IAccessibleText>* textarea_text) {
ASSERT_NE(nullptr, textarea_text);
LoadTextareaField();
// Retrieve the IAccessible interface for the web page.
Microsoft::WRL::ComPtr<IAccessible> document(GetRendererAccessible());
std::vector<base::win::ScopedVariant> document_children =
GetAllAccessibleChildren(document.Get());
ASSERT_EQ(1u, document_children.size());
Microsoft::WRL::ComPtr<IAccessible2> section;
ASSERT_HRESULT_SUCCEEDED(QueryIAccessible2(
GetAccessibleFromVariant(document.Get(), document_children[0].AsInput())
.Get(),
§ion));
std::vector<base::win::ScopedVariant> section_children =
GetAllAccessibleChildren(section.Get());
ASSERT_EQ(1u, section_children.size());
// Find the textarea text field.
Microsoft::WRL::ComPtr<IAccessible2> textarea;
ASSERT_HRESULT_SUCCEEDED(QueryIAccessible2(
GetAccessibleFromVariant(section.Get(), section_children[0].AsInput())
.Get(),
&textarea));
LONG textarea_role = 0;
ASSERT_HRESULT_SUCCEEDED(textarea->role(&textarea_role));
ASSERT_EQ(ROLE_SYSTEM_TEXT, textarea_role);
// Retrieve the IAccessibleText interface for the field.
textarea_text->Reset();
ASSERT_HRESULT_SUCCEEDED(textarea.As(textarea_text));
// Set the caret before the last character.
AccessibilityNotificationWaiter waiter(
shell()->web_contents(), ui::kAXModeComplete,
ax::mojom::Event::kTextSelectionChanged);
std::wstring caret_offset =
base::NumberToWString(InputContentsString().size() - 1);
ExecuteScript(base::WideToUTF16(
base::StrCat({L"var textField = document.querySelector('textarea');"
L"textField.focus();"
L"textField.setSelectionRange(",
caret_offset, L",", caret_offset, L");"})));
waiter.WaitForNotification();
}
// Loads a page with a paragraph of sample text.
void AccessibilityWinBrowserTest::SetUpSampleParagraph(
Microsoft::WRL::ComPtr<IAccessibleText>* accessible_text,
ui::AXMode accessibility_mode) {
LoadSampleParagraph(accessibility_mode);
SetUpSampleParagraphHelper(accessible_text);
}
// Loads a page with a paragraph of sample text which is below the
// bottom of the screen.
void AccessibilityWinBrowserTest::SetUpSampleParagraphInScrollableDocument(
Microsoft::WRL::ComPtr<IAccessibleText>* accessible_text,
ui::AXMode accessibility_mode) {
LoadSampleParagraphInScrollableDocument(accessibility_mode);
SetUpSampleParagraphHelper(accessible_text);
}
// Loads a page with a paragraph of sample text which is below the
// bottom of the screen, where the bounds of the paragraph
// are longer than one screenful.
void AccessibilityWinBrowserTest::SetUpVeryTallPadding(
Microsoft::WRL::ComPtr<IAccessibleText>* accessible_text,
ui::AXMode accessibility_mode) {
LoadInitialAccessibilityTreeFromHtml(
R"HTML(<!DOCTYPE html>
<html>
<body>
<p style="padding-top:200vh; padding-bottom:200vh">ABC<br/>DEF</p>
</body>
</html>)HTML",
accessibility_mode);
SetUpSampleParagraphHelper(accessible_text);
}
// Loads a page with a paragraph of sample text which is below the
// bottom of the screen, where the bounds of the paragraph
// are longer than one screenful.
void AccessibilityWinBrowserTest::SetUpVeryTallMargin(
Microsoft::WRL::ComPtr<IAccessibleText>* accessible_text,
ui::AXMode accessibility_mode) {
LoadInitialAccessibilityTreeFromHtml(
R"HTML(<!DOCTYPE html>
<html>
<body>
<p style="margin-top:200vh; margin-bottom:200vh">ABC<br/>DEF</p>
</body>
</html>)HTML",
accessibility_mode);
SetUpSampleParagraphHelper(accessible_text);
}
void AccessibilityWinBrowserTest::SetUpSampleParagraphInScrollableEditable(
Microsoft::WRL::ComPtr<IAccessibleText>* accessible_text) {
LoadSampleParagraphInScrollableEditable();
SetUpSampleParagraphHelper(accessible_text);
}
void AccessibilityWinBrowserTest::SetUpSampleParagraphHelper(
Microsoft::WRL::ComPtr<IAccessibleText>* accessible_text) {
ASSERT_NE(nullptr, accessible_text);
// Retrieve the IAccessible interface for the web page.
Microsoft::WRL::ComPtr<IAccessible> document(GetRendererAccessible());
std::vector<base::win::ScopedVariant> document_children =
GetAllAccessibleChildren(document.Get());
ASSERT_EQ(1u, document_children.size());
Microsoft::WRL::ComPtr<IAccessible2> paragraph;
ASSERT_HRESULT_SUCCEEDED(QueryIAccessible2(
GetAccessibleFromVariant(document.Get(), document_children[0].AsInput())
.Get(),
¶graph));
LONG paragraph_role = 0;
ASSERT_HRESULT_SUCCEEDED(paragraph->role(¶graph_role));
ASSERT_EQ(IA2_ROLE_PARAGRAPH, paragraph_role);
accessible_text->Reset();
ASSERT_HRESULT_SUCCEEDED(paragraph.As(accessible_text));
}
// Retrieve the accessibility node, starting from the root node, that matches
// the accessibility role, name or value.
BrowserAccessibility* AccessibilityWinBrowserTest::FindNode(
ax::mojom::Role role,
const std::string& name_or_value) {
BrowserAccessibility* root = GetManager()->GetRoot();
CHECK(root);
return FindNodeInSubtree(*root, role, name_or_value);
}
// Retrieve the browser accessibility manager object for the current web
// contents.
BrowserAccessibilityManager* AccessibilityWinBrowserTest::GetManager() {
WebContentsImpl* web_contents =
static_cast<WebContentsImpl*>(shell()->web_contents());
return web_contents->GetOrCreateRootBrowserAccessibilityManager();
}
// Retrieve the accessibility node in the subtree that matches the accessibility
// role, name or value.
BrowserAccessibility* AccessibilityWinBrowserTest::FindNodeInSubtree(
BrowserAccessibility& node,
ax::mojom::Role role,
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 (node.GetRole() == role &&
(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), role, name_or_value);
if (result)
return result;
}
return nullptr;
}
// Static helpers ------------------------------------------------
Microsoft::WRL::ComPtr<IAccessible>
AccessibilityWinBrowserTest::GetAccessibleFromVariant(IAccessible* parent,
VARIANT* var) {
Microsoft::WRL::ComPtr<IAccessible> ptr;
switch (V_VT(var)) {
case VT_DISPATCH: {
IDispatch* dispatch = V_DISPATCH(var);
if (dispatch)
dispatch->QueryInterface(IID_PPV_ARGS(&ptr));
break;
}
case VT_I4: {
Microsoft::WRL::ComPtr<IDispatch> dispatch;
HRESULT hr = parent->get_accChild(*var, &dispatch);
EXPECT_TRUE(SUCCEEDED(hr));
if (dispatch.Get())
dispatch.As(&ptr);
break;
}
}
return ptr;
}
HRESULT AccessibilityWinBrowserTest::QueryIAccessible2(
IAccessible* accessible,
IAccessible2** accessible2) {
// IA2 Spec dictates that IServiceProvider should be used instead of
// QueryInterface when retrieving IAccessible2.
Microsoft::WRL::ComPtr<IServiceProvider> service_provider;
HRESULT hr = accessible->QueryInterface(IID_PPV_ARGS(&service_provider));
return SUCCEEDED(hr)
? service_provider->QueryService(IID_IAccessible2, accessible2)
: hr;
}
// Recursively search through all of the descendants reachable from an
// IAccessible node and return true if we find one with the given role
// and name.
void AccessibilityWinBrowserTest::FindNodeInAccessibilityTree(
IAccessible* node,
int32_t expected_role,
const std::wstring& expected_name,
int32_t depth,
bool* found) {
base::win::ScopedBstr name_bstr;
base::win::ScopedVariant childid_self(CHILDID_SELF);
node->get_accName(childid_self, name_bstr.Receive());
std::wstring name(name_bstr.Get(), name_bstr.Length());
base::win::ScopedVariant role;
node->get_accRole(childid_self, role.Receive());
ASSERT_EQ(VT_I4, role.type());
// Print the accessibility tree as we go, because if this test fails
// on the bots, this is really helpful in figuring out why.
for (int i = 0; i < depth; i++)
printf(" ");
printf("role=%s name=%s\n",
base::WideToUTF8(IAccessibleRoleToString(V_I4(role.ptr()))).c_str(),
base::WideToUTF8(name).c_str());
if (expected_role == V_I4(role.ptr()) && expected_name == name) {
*found = true;
return;
}
std::vector<base::win::ScopedVariant> children =
GetAllAccessibleChildren(node);
for (size_t i = 0; i < children.size(); ++i) {
Microsoft::WRL::ComPtr<IAccessible> child_accessible(
GetAccessibleFromVariant(node, children[i].AsInput()));
if (child_accessible) {
FindNodeInAccessibilityTree(child_accessible.Get(), expected_role,
expected_name, depth + 1, found);
if (*found)
return;
}
}
}
// Ensures that the text and the start and end offsets retrieved using
// get_textAtOffset match the expected values.
void AccessibilityWinBrowserTest::CheckTextAtOffset(
const Microsoft::WRL::ComPtr<IAccessibleText>& object,
LONG offset,
IA2TextBoundaryType boundary_type,
LONG expected_start_offset,
LONG expected_end_offset,
const std::wstring& expected_text) {
::testing::Message message;
message << "While checking for \'" << expected_text << "\' at "
<< expected_start_offset << '-' << expected_end_offset << '.';
SCOPED_TRACE(message);
LONG start_offset = 0;
LONG end_offset = 0;
base::win::ScopedBstr text;
HRESULT hr = object->get_textAtOffset(offset, boundary_type, &start_offset,
&end_offset, text.Receive());
EXPECT_EQ(S_OK, hr);
EXPECT_EQ(expected_start_offset, start_offset);
EXPECT_EQ(expected_end_offset, end_offset);
EXPECT_STREQ(expected_text.c_str(), text.Get());
}
std::vector<base::win::ScopedVariant>
AccessibilityWinBrowserTest::GetAllAccessibleChildren(IAccessible* element) {
LONG child_count = 0;
HRESULT hr = element->get_accChildCount(&child_count);
EXPECT_EQ(S_OK, hr);
if (child_count <= 0)
return std::vector<base::win::ScopedVariant>();
std::unique_ptr<VARIANT[]> children_array(new VARIANT[child_count]);
LONG obtained_count = 0;
hr = AccessibleChildren(element, 0, child_count, children_array.get(),
&obtained_count);
EXPECT_EQ(S_OK, hr);
EXPECT_EQ(child_count, obtained_count);
std::vector<base::win::ScopedVariant> children(
static_cast<size_t>(child_count));
for (size_t i = 0; i < children.size(); i++)
children[i].Reset(children_array[i]);
return children;
}
// AccessibleChecker ----------------------------------------------------------
class AccessibilityWinBrowserTest::AccessibleChecker {
public:
// This constructor can be used if the IA2 role will be the same as the MSAA
// role.
AccessibleChecker(const std::wstring& expected_name,
int32_t expected_role,
const std::wstring& expected_value);
AccessibleChecker(const std::wstring& expected_name,
int32_t expected_role,
int32_t expected_ia2_role,
const std::wstring& expected_value);
// Append an AccessibleChecker that verifies accessibility information for
// a child IAccessible. Order is important.
void AppendExpectedChild(AccessibleChecker* expected_child);
// Check that the name and role of the given IAccessible instance and its
// descendants match the expected names and roles that this object was
// initialized with.
void CheckAccessible(IAccessible* accessible);
// Set the expected value for this AccessibleChecker.
void SetExpectedValue(const std::wstring& expected_value);
// Set the expected state for this AccessibleChecker.
void SetExpectedState(LONG expected_state);
private:
typedef std::vector<AccessibleChecker*> AccessibleCheckerVector;
void CheckAccessibleName(IAccessible* accessible);
void CheckAccessibleRole(IAccessible* accessible);
void CheckIA2Role(IAccessible* accessible);
void CheckAccessibleValue(IAccessible* accessible);
void CheckAccessibleState(IAccessible* accessible);
void CheckAccessibleChildren(IAccessible* accessible);
// Expected accessible name. Checked against IAccessible::get_accName.
std::wstring name_;
// Expected accessible role. Checked against IAccessible::get_accRole.
int32_t role_;
// Expected IAccessible2 role. Checked against IAccessible2::role.
int32_t ia2_role_;
// Expected accessible value. Checked against IAccessible::get_accValue.
std::wstring value_;
// Expected accessible state. Checked against IAccessible::get_accState.
LONG state_;
// Expected accessible children. Checked using IAccessible::get_accChildCount
// and ::AccessibleChildren.
AccessibleCheckerVector children_;
};
AccessibilityWinBrowserTest::AccessibleChecker::AccessibleChecker(
const std::wstring& expected_name,
int32_t expected_role,
const std::wstring& expected_value)
: name_(expected_name),
role_(expected_role),
ia2_role_(expected_role),
value_(expected_value),
state_(-1) {}
AccessibilityWinBrowserTest::AccessibleChecker::AccessibleChecker(
const std::wstring& expected_name,
int32_t expected_role,
int32_t expected_ia2_role,
const std::wstring& expected_value)
: name_(expected_name),
role_(expected_role),
ia2_role_(expected_ia2_role),
value_(expected_value),
state_(-1) {}
void AccessibilityWinBrowserTest::AccessibleChecker::AppendExpectedChild(
AccessibleChecker* expected_child) {
children_.push_back(expected_child);
}
void AccessibilityWinBrowserTest::AccessibleChecker::CheckAccessible(
IAccessible* accessible) {
SCOPED_TRACE("While checking " +
base::WideToUTF8(IAccessibleRoleToString(role_)));
CheckAccessibleName(accessible);
CheckAccessibleRole(accessible);
CheckIA2Role(accessible);
CheckAccessibleValue(accessible);
CheckAccessibleState(accessible);
CheckAccessibleChildren(accessible);
}
void AccessibilityWinBrowserTest::AccessibleChecker::SetExpectedValue(
const std::wstring& expected_value) {
value_ = expected_value;
}
void AccessibilityWinBrowserTest::AccessibleChecker::SetExpectedState(
LONG expected_state) {
state_ = expected_state;
}
void AccessibilityWinBrowserTest::AccessibleChecker::CheckAccessibleName(
IAccessible* accessible) {
base::win::ScopedBstr name;
base::win::ScopedVariant childid_self(CHILDID_SELF);
HRESULT hr = accessible->get_accName(childid_self, name.Receive());
if (name_.empty()) {
// If the object doesn't have name S_FALSE should be returned.
EXPECT_EQ(S_FALSE, hr);
} else {
// Test that the correct string was returned.
EXPECT_EQ(S_OK, hr);
EXPECT_EQ(name_, std::wstring(name.Get(), name.Length()));
}
}
void AccessibilityWinBrowserTest::AccessibleChecker::CheckAccessibleRole(
IAccessible* accessible) {
base::win::ScopedVariant role;
base::win::ScopedVariant childid_self(CHILDID_SELF);
HRESULT hr = accessible->get_accRole(childid_self, role.Receive());
ASSERT_EQ(S_OK, hr);
ASSERT_EQ(VT_I4, role.type());
ASSERT_EQ(role_, V_I4(role.ptr()))
<< "Expected role: " << IAccessibleRoleToString(role_)
<< "\nGot role: " << IAccessibleRoleToString(V_I4(role.ptr()));
}
void AccessibilityWinBrowserTest::AccessibleChecker::CheckIA2Role(
IAccessible* accessible) {
Microsoft::WRL::ComPtr<IAccessible2> accessible2;
HRESULT hr = QueryIAccessible2(accessible, &accessible2);
ASSERT_EQ(S_OK, hr);
LONG ia2_role = 0;
hr = accessible2->role(&ia2_role);
ASSERT_EQ(S_OK, hr);
EXPECT_EQ(ia2_role_, ia2_role)
<< "Expected ia2 role: " << IAccessible2RoleToString(ia2_role_)
<< "\nGot ia2 role: " << IAccessible2RoleToString(ia2_role);
}
void AccessibilityWinBrowserTest::AccessibleChecker::CheckAccessibleValue(
IAccessible* accessible) {
// Don't check the value if if's a DOCUMENT role, because the value
// is supposed to be the url (and we don't keep track of that in the
// test expectations).
base::win::ScopedVariant role;
base::win::ScopedVariant childid_self(CHILDID_SELF);
HRESULT hr = accessible->get_accRole(childid_self, role.Receive());
ASSERT_EQ(S_OK, hr);
if (role.type() == VT_I4 && V_I4(role.ptr()) == ROLE_SYSTEM_DOCUMENT)
return;
// Get the value.
base::win::ScopedBstr value;
hr = accessible->get_accValue(childid_self, value.Receive());
EXPECT_EQ(S_OK, hr);
// Test that the correct string was returned.
EXPECT_EQ(value_, std::wstring(value.Get(), value.Length()));
}
void AccessibilityWinBrowserTest::AccessibleChecker::CheckAccessibleState(
IAccessible* accessible) {
if (state_ < 0)
return;
base::win::ScopedVariant state;
base::win::ScopedVariant childid_self(CHILDID_SELF);
HRESULT hr = accessible->get_accState(childid_self, state.Receive());
EXPECT_EQ(S_OK, hr);
ASSERT_EQ(VT_I4, state.type());
LONG obj_state = V_I4(state.ptr());
// Avoid flakiness. The "offscreen" state depends on whether the browser
// window is frontmost or not, and "hottracked" depends on whether the
// mouse cursor happens to be over the element.
obj_state &= ~(STATE_SYSTEM_OFFSCREEN | STATE_SYSTEM_HOTTRACKED);
EXPECT_EQ(state_, obj_state)
<< "Expected state: " << IAccessibleStateToString(state_)
<< "\nGot state: " << IAccessibleStateToString(obj_state);
}
void AccessibilityWinBrowserTest::AccessibleChecker::CheckAccessibleChildren(
IAccessible* parent) {
std::vector<base::win::ScopedVariant> obtained_children =
GetAllAccessibleChildren(parent);
size_t child_count = obtained_children.size();
ASSERT_EQ(child_count, children_.size());
AccessibleCheckerVector::iterator child_checker;
std::vector<base::win::ScopedVariant>::iterator child;
for (child_checker = children_.begin(), child = obtained_children.begin();
child_checker != children_.end() && child != obtained_children.end();
++child_checker, ++child) {
Microsoft::WRL::ComPtr<IAccessible> child_accessible(
GetAccessibleFromVariant(parent, child->AsInput()));
ASSERT_TRUE(child_accessible.Get());
(*child_checker)->CheckAccessible(child_accessible.Get());
}
}
// Helper class that listens to native Windows events using
// AccessibilityEventRecorder, and blocks until the pretty-printed
// event string matches the given match pattern.
class NativeWinEventWaiter {
public:
NativeWinEventWaiter(BrowserAccessibilityManager* manager,
const std::string& match_pattern)
: event_recorder_(
AXInspectFactory::CreatePlatformRecorder(manager,
base::GetCurrentProcId())),
match_pattern_(match_pattern) {
event_recorder_->ListenToEvents(base::BindRepeating(
&NativeWinEventWaiter::OnEvent, base::Unretained(this)));
}
void OnEvent(const std::string& event_str) {
DLOG(INFO) << "Got event " + event_str;
if (base::MatchPattern(event_str, match_pattern_))
run_loop_.Quit();
}
void Wait() { run_loop_.Run(); }
private:
std::unique_ptr<ui::AXEventRecorder> event_recorder_;
std::string match_pattern_;
base::RunLoop run_loop_;
};
// Helper class that reproduces a specific crash when UIA parent navigation
// is performed during the destruction of its WebContents.
class WebContentsUIAParentNavigationInDestroyedWatcher
: public WebContentsObserver {
public:
explicit WebContentsUIAParentNavigationInDestroyedWatcher(
WebContents* web_contents,
IUIAutomationElement* root,
IUIAutomationTreeWalker* tree_walker)
: WebContentsObserver(web_contents),
root_(root),
tree_walker_(tree_walker) {
CHECK(web_contents);
}
~WebContentsUIAParentNavigationInDestroyedWatcher() override {}
// Waits until the WebContents is destroyed.
void Wait() { run_loop_.Run(); }
private:
// Overridden WebContentsObserver methods.
void WebContentsDestroyed() override {
// Test navigating to the parent node via UIA.
Microsoft::WRL::ComPtr<IUIAutomationElement> parent;
tree_walker_->GetParentElement(root_.Get(), &parent);
// The original bug resulted in a crash when making this call.
parent.Get();
run_loop_.Quit();
}
Microsoft::WRL::ComPtr<IUIAutomationElement> root_;
Microsoft::WRL::ComPtr<IUIAutomationTreeWalker> tree_walker_;
base::RunLoop run_loop_;
};
} // namespace
//
// Tests ----------------------------------------------------------------------
//
IN_PROC_BROWSER_TEST_F(AccessibilityWinBrowserTest,
TestAlwaysFireFocusEventAfterNavigationComplete) {
ASSERT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
// Users of Jaws or NVDA screen readers might not realize that the virtual
// buffer has been loaded, if focus hasn't been placed in the document after
// navigating to a new page. If a focus event is not received by the screen
// reader, it might "think" that focus is outside the web contents.
//
// We can't use "LoadInitialAccessibilityTreeFromHtml" because it waits for
// the "kLoadComplete" event, and the "kFocus" and "kLoadComplete" events are
// not guaranteed to be sent in the same order every time, neither do we need
// to enforce such an ordering. However, we do need to ensure that at the
// point when the "kFocus" event is sent, the root object is present.
AccessibilityNotificationWaiter waiter(
shell()->web_contents(), ui::kAXModeComplete,
ui::AXEventGenerator::Event::FOCUS_CHANGED);
GURL html_data_url("data:text/html,<p>Hello world.</p>");
ASSERT_TRUE(NavigateToURL(shell(), html_data_url));
waiter.WaitForNotification();
// Check that at least the root of the page has indeed loaded and that it is
// focused.
Microsoft::WRL::ComPtr<IAccessible> document(GetRendererAccessible());
ASSERT_TRUE(document);
base::win::ScopedVariant focus;
base::win::ScopedVariant childid_self(CHILDID_SELF);
ASSERT_HRESULT_SUCCEEDED(document->get_accFocus(focus.Receive()));
EXPECT_EQ(VT_I4, focus.type());
EXPECT_EQ(CHILDID_SELF, V_I4(focus.ptr()));
}
IN_PROC_BROWSER_TEST_F(AccessibilityWinBrowserTest, TestBusyAccessibilityTree) {
ASSERT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
// The initial accessible returned should have state STATE_SYSTEM_BUSY while
// the accessibility tree is being requested from the renderer.
AccessibleChecker document1_checker(std::wstring(), ROLE_SYSTEM_DOCUMENT,
std::wstring());
document1_checker.SetExpectedState(STATE_SYSTEM_READONLY |
STATE_SYSTEM_FOCUSABLE |
STATE_SYSTEM_FOCUSED | STATE_SYSTEM_BUSY);
document1_checker.CheckAccessible(GetRendererAccessible());
}
// Periodically failing. See crbug.com/145537
IN_PROC_BROWSER_TEST_F(AccessibilityWinBrowserTest,
DISABLED_TestNotificationActiveDescendantChanged) {
LoadInitialAccessibilityTreeFromHtml(
"<ul tabindex='-1' role='radiogroup' aria-label='ul'>"
"<li id='li'>li</li></ul>");
// Check the browser's copy of the renderer accessibility tree.
AccessibleChecker list_marker_checker(L"\x2022", ROLE_SYSTEM_TEXT,
std::wstring());
AccessibleChecker static_text_checker(L"li", ROLE_SYSTEM_TEXT,
std::wstring());
AccessibleChecker list_item_checker(std::wstring(), ROLE_SYSTEM_LISTITEM,
std::wstring());
list_item_checker.SetExpectedState(STATE_SYSTEM_READONLY);
AccessibleChecker radio_group_checker(L"ul", ROLE_SYSTEM_GROUPING,
IA2_ROLE_SECTION, std::wstring());
radio_group_checker.SetExpectedState(STATE_SYSTEM_FOCUSABLE);
AccessibleChecker document_checker(std::wstring(), ROLE_SYSTEM_DOCUMENT,
std::wstring());
list_item_checker.AppendExpectedChild(&list_marker_checker);
list_item_checker.AppendExpectedChild(&static_text_checker);
radio_group_checker.AppendExpectedChild(&list_item_checker);
document_checker.AppendExpectedChild(&radio_group_checker);
document_checker.CheckAccessible(GetRendererAccessible());
// Set focus to the radio group.
auto waiter = std::make_unique<AccessibilityNotificationWaiter>(
shell()->web_contents(), ui::kAXModeComplete, ax::mojom::Event::kFocus);
ExecuteScript(u"document.body.children[0].focus();");
waiter->WaitForNotification();
// Check that the accessibility tree of the browser has been updated.
radio_group_checker.SetExpectedState(STATE_SYSTEM_FOCUSABLE |
STATE_SYSTEM_FOCUSED);
document_checker.CheckAccessible(GetRendererAccessible());
// Set the active descendant of the radio group
waiter = std::make_unique<AccessibilityNotificationWaiter>(
shell()->web_contents(), ui::kAXModeComplete, ax::mojom::Event::kFocus);
ExecuteScript(
u"document.body.children[0].setAttribute('aria-activedescendant', 'li')");
waiter->WaitForNotification();