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
content / browser / accessibility / hit_testing_browsertest.cc [blame]
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/accessibility/hit_testing_browsertest.h"
#include "base/check.h"
#include "base/command_line.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "build/chromecast_buildflags.h"
#include "build/chromeos_buildflags.h"
#include "content/browser/accessibility/accessibility_tree_formatter_blink.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/ax_inspect_factory.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.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/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "net/dns/mock_host_resolver.h"
#include "ui/accessibility/ax_clipping_behavior.h"
#include "ui/accessibility/ax_coordinate_system.h"
#include "ui/accessibility/ax_node_id_forward.h"
#include "ui/accessibility/platform/ax_platform_node_base.h"
#include "ui/accessibility/platform/ax_platform_tree_manager.h"
#include "ui/accessibility/platform/browser_accessibility.h"
#include "ui/accessibility/platform/browser_accessibility_manager.h"
#include "ui/display/display_switches.h"
#include "ui/gfx/geometry/vector2d_conversions.h"
namespace content {
using ui::AXTreeFormatter;
#define EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(css_point, expected_node, \
hit_node) \
SCOPED_TRACE(GetScopedTrace(css_point)); \
EXPECT_EQ(expected_node->GetId(), hit_node->GetId());
AccessibilityHitTestingBrowserTest::AccessibilityHitTestingBrowserTest() =
default;
AccessibilityHitTestingBrowserTest::~AccessibilityHitTestingBrowserTest() =
default;
void AccessibilityHitTestingBrowserTest::SetUpCommandLine(
base::CommandLine* command_line) {
auto device_scale_factor = GetParam();
command_line->AppendSwitchASCII(
switches::kForceDeviceScaleFactor,
base::StringPrintf("%.2f", device_scale_factor));
}
std::string AccessibilityHitTestingBrowserTest::TestPassToString::operator()(
const ::testing::TestParamInfo<double>& info) const {
auto device_scale_factor = info.param;
std::string name = base::StringPrintf("ZoomFactor%g", device_scale_factor);
// The test harness only allows alphanumeric characters and underscores
// in param names.
std::string sanitized_name;
base::ReplaceChars(name, ".", "_", &sanitized_name);
return sanitized_name;
}
ui::BrowserAccessibilityManager*
AccessibilityHitTestingBrowserTest::GetRootBrowserAccessibilityManager() {
WebContentsImpl* web_contents =
static_cast<WebContentsImpl*>(shell()->web_contents());
return web_contents->GetRootBrowserAccessibilityManager();
}
float AccessibilityHitTestingBrowserTest::GetDeviceScaleFactor() {
return GetRootBrowserAccessibilityManager()->device_scale_factor();
}
float AccessibilityHitTestingBrowserTest::GetPageScaleFactor() {
return GetRootBrowserAccessibilityManager()->GetPageScaleFactor();
}
gfx::Rect
AccessibilityHitTestingBrowserTest::GetViewBoundsInScreenCoordinates() {
return GetRootBrowserAccessibilityManager()
->GetViewBoundsInScreenCoordinates();
}
// http://www.chromium.org/developers/design-documents/blink-coordinate-spaces
// Device scale factor gets applied going from
// CSS to page pixels, i.e. before view offset.
gfx::Point AccessibilityHitTestingBrowserTest::CSSToFramePoint(
gfx::Point css_point) {
gfx::Point page_point;
page_point = ScaleToRoundedPoint(css_point, GetDeviceScaleFactor());
gfx::Point frame_point = page_point - scroll_offset_.OffsetFromOrigin();
return frame_point;
}
gfx::Point AccessibilityHitTestingBrowserTest::FrameToCSSPoint(
gfx::Point frame_point) {
gfx::Point page_point = frame_point + scroll_offset_.OffsetFromOrigin();
gfx::Point css_point;
css_point = ScaleToRoundedPoint(page_point, 1.0 / GetDeviceScaleFactor());
return css_point;
}
gfx::Point AccessibilityHitTestingBrowserTest::CSSToPhysicalPixelPoint(
gfx::Point css_point) {
gfx::Point frame_point = CSSToFramePoint(css_point);
gfx::Point viewport_point =
gfx::ScaleToRoundedPoint(frame_point, GetPageScaleFactor());
gfx::Rect screen_view_bounds = GetViewBoundsInScreenCoordinates();
gfx::Point screen_point =
viewport_point + screen_view_bounds.OffsetFromOrigin();
gfx::Point physical_pixel_point;
physical_pixel_point = screen_point;
return physical_pixel_point;
}
ui::BrowserAccessibility*
AccessibilityHitTestingBrowserTest::HitTestAndWaitForResultWithEvent(
const gfx::Point& point,
ax::mojom::Event event_to_fire) {
ui::BrowserAccessibilityManager* manager =
GetRootBrowserAccessibilityManager();
AccessibilityNotificationWaiter event_waiter(
shell()->web_contents(), ui::kAXModeComplete, event_to_fire);
ui::AXActionData action_data;
action_data.action = ax::mojom::Action::kHitTest;
action_data.target_point = CSSToFramePoint(point);
action_data.hit_test_event_to_fire = event_to_fire;
manager->delegate()->AccessibilityHitTest(CSSToFramePoint(point),
event_to_fire, 0, {});
EXPECT_TRUE(event_waiter.WaitForNotification());
ui::BrowserAccessibilityManager* target_manager =
event_waiter.event_browser_accessibility_manager();
int event_target_id = event_waiter.event_target_id();
ui::BrowserAccessibility* hit_node =
target_manager->GetFromID(event_target_id);
return hit_node;
}
ui::BrowserAccessibility*
AccessibilityHitTestingBrowserTest::HitTestAndWaitForResult(
const gfx::Point& point) {
return HitTestAndWaitForResultWithEvent(point, ax::mojom::Event::kHover);
}
ui::BrowserAccessibility*
AccessibilityHitTestingBrowserTest::AsyncHitTestAndWaitForCallback(
const gfx::Point& point) {
ui::BrowserAccessibilityManager* manager =
GetRootBrowserAccessibilityManager();
gfx::Point target_point = CSSToFramePoint(point);
base::RunLoop run_loop;
ui::AXPlatformTreeManager* hit_manager = nullptr;
ui::AXNodeID hit_node_id = ui::kInvalidAXNodeID;
auto callback = [&](ui::AXPlatformTreeManager* manager,
ui::AXNodeID node_id) {
hit_manager = manager;
hit_node_id = node_id;
run_loop.QuitClosure().Run();
};
manager->delegate()->AccessibilityHitTest(
target_point, ax::mojom::Event::kNone, 0,
base::BindLambdaForTesting(callback));
run_loop.Run();
ui::BrowserAccessibility* hit_node =
static_cast<ui::BrowserAccessibilityManager*>(hit_manager)
->GetFromID(hit_node_id);
return hit_node;
}
ui::BrowserAccessibility*
AccessibilityHitTestingBrowserTest::CallCachingAsyncHitTest(
const gfx::Point& page_point) {
gfx::Point screen_point = CSSToPhysicalPixelPoint(page_point);
// Each call to CachingAsyncHitTest results in at least one HOVER
// event received. Block until we receive it. CachingAsyncHitTestNearestLeaf
// will call CachingAsyncHitTest.
AccessibilityNotificationWaiter hover_waiter(
shell()->web_contents(), ui::kAXModeComplete, ax::mojom::Event::kHover);
ui::BrowserAccessibility* result =
GetRootBrowserAccessibilityManager()->CachingAsyncHitTest(screen_point);
EXPECT_TRUE(hover_waiter.WaitForNotification());
return result;
}
ui::BrowserAccessibility*
AccessibilityHitTestingBrowserTest::CallNearestLeafNode(
const gfx::Point& page_point) {
gfx::Point screen_point = CSSToPhysicalPixelPoint(page_point);
ui::BrowserAccessibilityManager* manager =
static_cast<WebContentsImpl*>(shell()->web_contents())
->GetRootBrowserAccessibilityManager();
// Each call to CachingAsyncHitTest results in at least one HOVER
// event received. Block until we receive it. CachingAsyncHitTest
// will call CachingAsyncHitTest.
AccessibilityNotificationWaiter hover_waiter(
shell()->web_contents(), ui::kAXModeComplete, ax::mojom::Event::kHover);
ui::AXPlatformNodeBase* platform_node = nullptr;
if (manager->GetBrowserAccessibilityRoot()->GetAXPlatformNode()) {
platform_node =
static_cast<ui::AXPlatformNodeBase*>(
manager->GetBrowserAccessibilityRoot()->GetAXPlatformNode())
->NearestLeafToPoint(screen_point);
}
EXPECT_TRUE(hover_waiter.WaitForNotification());
if (platform_node) {
return ui::BrowserAccessibility::FromAXPlatformNodeDelegate(
platform_node->GetDelegate());
}
return nullptr;
}
void AccessibilityHitTestingBrowserTest::SynchronizeThreads() {
MainThreadFrameObserver observer(shell()
->web_contents()
->GetRenderWidgetHostView()
->GetRenderWidgetHost());
observer.Wait();
}
void AccessibilityHitTestingBrowserTest::SimulatePinchZoom(
float desired_page_scale) {
RenderFrameSubmissionObserver observer(shell()->web_contents());
AccessibilityNotificationWaiter accessibility_waiter(
shell()->web_contents(), ui::AXMode(),
ax::mojom::Event::kLocationChanged);
const gfx::Rect contents_rect = shell()->web_contents()->GetContainerBounds();
const gfx::Point pinch_position(contents_rect.x(), contents_rect.y());
SimulateGesturePinchSequence(shell()->web_contents(), pinch_position,
desired_page_scale,
blink::WebGestureDevice::kTouchscreen);
// Wait for the gesture to be reflected, then make a note of the new scale
// factor and any scroll offset that may have been introduced.
observer.WaitForPageScaleFactor(desired_page_scale, 0);
const cc::RenderFrameMetadata& render_frame_metadata =
observer.LastRenderFrameMetadata();
DCHECK(render_frame_metadata.page_scale_factor == desired_page_scale);
if (render_frame_metadata.root_scroll_offset) {
scroll_offset_ =
gfx::ToRoundedPoint(render_frame_metadata.root_scroll_offset.value());
} else {
scroll_offset_ = gfx::Point();
}
// Ensure we get an accessibility update reflecting the new scale factor.
// TODO(crbug.com/40844856): Investigate why this does not return true.
ASSERT_TRUE(accessibility_waiter.WaitForNotification());
}
std::string
AccessibilityHitTestingBrowserTest::FormatHitTestAccessibilityTree() {
std::unique_ptr<AXTreeFormatter> accessibility_tree_formatter =
AXInspectFactory::CreateBlinkFormatter();
accessibility_tree_formatter->set_show_ids(true);
accessibility_tree_formatter->SetPropertyFilters(
{{"name=*", ui::AXPropertyFilter::ALLOW},
{"location=*", ui::AXPropertyFilter::ALLOW},
{"size=*", ui::AXPropertyFilter::ALLOW}});
std::string accessibility_tree;
return accessibility_tree_formatter->Format(GetRootAndAssertNonNull());
}
std::string AccessibilityHitTestingBrowserTest::GetScopedTrace(
gfx::Point css_point) {
std::stringstream string_stream;
string_stream << std::endl
<< "View bounds: "
<< GetRootBrowserAccessibilityManager()
->GetViewBoundsInScreenCoordinates()
.ToString()
<< " Page scale: " << GetPageScaleFactor()
<< " Scroll offset: " << scroll_offset_.ToString() << std::endl
<< "Test point CSS: " << css_point.ToString()
<< " Frame: " << CSSToFramePoint(css_point).ToString()
<< " Physical: "
<< CSSToPhysicalPixelPoint(css_point).ToString() << std::endl
<< "Accessibility tree: " << std::endl
<< FormatHitTestAccessibilityTree();
return string_stream.str();
}
class AccessibilityHitTestingCrossProcessBrowserTest
: public AccessibilityHitTestingBrowserTest {
public:
AccessibilityHitTestingCrossProcessBrowserTest() {}
~AccessibilityHitTestingCrossProcessBrowserTest() override {}
void SetUpCommandLine(base::CommandLine* command_line) override {
IsolateAllSitesForTesting(command_line);
AccessibilityHitTestingBrowserTest::SetUpCommandLine(command_line);
}
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
SetupCrossSiteRedirector(embedded_test_server());
ASSERT_TRUE(embedded_test_server()->Start());
}
};
INSTANTIATE_TEST_SUITE_P(
All,
AccessibilityHitTestingBrowserTest,
::testing::Values(1, 2),
AccessibilityHitTestingBrowserTest::TestPassToString());
INSTANTIATE_TEST_SUITE_P(
All,
AccessibilityHitTestingCrossProcessBrowserTest,
::testing::Values(1, 2),
AccessibilityHitTestingBrowserTest::TestPassToString());
#if defined(THREAD_SANITIZER)
// TODO(crbug.com/40775546): Times out flakily on TSAN builds.
#define MAYBE_CachingAsyncHitTest DISABLED_CachingAsyncHitTest
#else
#define MAYBE_CachingAsyncHitTest CachingAsyncHitTest
#endif
IN_PROC_BROWSER_TEST_P(AccessibilityHitTestingBrowserTest,
MAYBE_CachingAsyncHitTest) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
AccessibilityNotificationWaiter waiter(shell()->web_contents(),
ui::kAXModeComplete,
ax::mojom::Event::kLoadComplete);
GURL url(embedded_test_server()->GetURL(
"/accessibility/hit_testing/simple_rectangles.html"));
EXPECT_TRUE(NavigateToURL(shell(), url));
ASSERT_TRUE(waiter.WaitForNotification());
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"rectA");
// Test a hit on a rect in the main frame.
{
gfx::Point rect_2_point(49, 20);
ui::BrowserAccessibility* hit_node = CallCachingAsyncHitTest(rect_2_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rect2");
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_2_point, expected_node, hit_node);
}
// Test a hit on a rect in the iframe.
{
gfx::Point rect_b_point(79, 79);
ui::BrowserAccessibility* hit_node = CallCachingAsyncHitTest(rect_b_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rectB");
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_b_point, expected_node, hit_node);
}
}
#if defined(THREAD_SANITIZER)
// TODO(crbug.com/40775516): Times out flakily on TSAN builds.
#define MAYBE_HitTest DISABLED_HitTest
#else
#define MAYBE_HitTest HitTest
#endif
IN_PROC_BROWSER_TEST_P(AccessibilityHitTestingBrowserTest, MAYBE_HitTest) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
AccessibilityNotificationWaiter waiter(shell()->web_contents(),
ui::kAXModeComplete,
ax::mojom::Event::kLoadComplete);
GURL url(embedded_test_server()->GetURL(
"/accessibility/hit_testing/simple_rectangles.html"));
EXPECT_TRUE(NavigateToURL(shell(), url));
ASSERT_TRUE(waiter.WaitForNotification());
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"rectA");
// Test a hit on a rect in the main frame.
{
gfx::Point rect_2_point(49, 20);
ui::BrowserAccessibility* hit_node = HitTestAndWaitForResult(rect_2_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rect2");
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_2_point, expected_node, hit_node);
// Try callback API.
hit_node = AsyncHitTestAndWaitForCallback(rect_2_point);
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_2_point, expected_node, hit_node);
}
// Test a hit on a rect in the iframe.
{
gfx::Point rect_b_point(79, 79);
ui::BrowserAccessibility* hit_node = HitTestAndWaitForResult(rect_b_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rectB");
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_b_point, expected_node, hit_node);
// Try callback API.
hit_node = AsyncHitTestAndWaitForCallback(rect_b_point);
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_b_point, expected_node, hit_node);
// Test with a different event.
hit_node = HitTestAndWaitForResultWithEvent(rect_b_point,
ax::mojom::Event::kAlert);
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_b_point, expected_node, hit_node);
}
}
// Web popups don't exist on Android, so this test doesn't have to be run on
// this platform.
#if !BUILDFLAG(IS_ANDROID)
// crbug.com/1317505: Flaky on Lacros and Linux Wayland
#if BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(IS_LINUX)
#define MAYBE_HitTestInPopup DISABLED_HitTestInPopup
#else
#define MAYBE_HitTestInPopup HitTestInPopup
#endif
IN_PROC_BROWSER_TEST_P(AccessibilityHitTestingBrowserTest,
MAYBE_HitTestInPopup) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
AccessibilityNotificationWaiter waiter(shell()->web_contents(),
ui::kAXModeComplete,
ax::mojom::Event::kLoadComplete);
GURL url(embedded_test_server()->GetURL(
"/accessibility/hit_testing/input-color-with-popup-open.html"));
EXPECT_TRUE(NavigateToURL(shell(), url));
ASSERT_TRUE(waiter.WaitForNotification());
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"color picker");
AccessibilityNotificationWaiter click_waiter(
shell()->web_contents(), ui::kAXModeComplete, ax::mojom::Event::kClicked);
auto* input = FindNode(ax::mojom::Role::kColorWell, "color picker");
ASSERT_TRUE(input);
ui::AXActionData action_data;
action_data.action = ax::mojom::Action::kDoDefault;
input->AccessibilityPerformAction(action_data);
ASSERT_TRUE(click_waiter.WaitForNotification());
auto* popup_root = GetRootBrowserAccessibilityManager()->GetPopupRoot();
ASSERT_NE(nullptr, popup_root);
auto* format_toggler =
FindNode(ax::mojom::Role::kSpinButton, "Format toggler");
ASSERT_TRUE(format_toggler);
gfx::Rect bounds = format_toggler->GetBoundsRect(
ui::AXCoordinateSystem::kFrame, ui::AXClippingBehavior::kUnclipped);
auto* hit_node =
HitTestAndWaitForResult(FrameToCSSPoint(bounds.CenterPoint()));
ASSERT_EQ(hit_node, format_toggler);
}
#endif
IN_PROC_BROWSER_TEST_P(AccessibilityHitTestingBrowserTest,
HitTestOutsideDocumentBoundsReturnsRoot) {
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
// Load the page.
AccessibilityNotificationWaiter waiter(shell()->web_contents(),
ui::kAXModeComplete,
ax::mojom::Event::kLoadComplete);
const char url_str[] =
"data:text/html,"
"<!doctype html>"
"<html><head><title>Accessibility Test</title></head>"
"<body>"
"<a href='#'>"
"This is some text in a link"
"</a>"
"</body></html>";
GURL url(url_str);
EXPECT_TRUE(NavigateToURL(shell(), url));
ASSERT_TRUE(waiter.WaitForNotification());
gfx::Point out_of_bounds_point(-1, -1);
ui::BrowserAccessibility* hit_node =
HitTestAndWaitForResult(out_of_bounds_point);
ASSERT_TRUE(hit_node != nullptr);
ASSERT_EQ(ax::mojom::Role::kRootWebArea, hit_node->GetRole());
// Try callback API.
hit_node = AsyncHitTestAndWaitForCallback(out_of_bounds_point);
ASSERT_TRUE(hit_node != nullptr);
ASSERT_EQ(ax::mojom::Role::kRootWebArea, hit_node->GetRole());
}
IN_PROC_BROWSER_TEST_P(AccessibilityHitTestingCrossProcessBrowserTest,
HitTestingInCrossProcessIframeWithScrolling) {
GURL url_a(embedded_test_server()->GetURL(
"a.com", "/accessibility/hit_testing/simple_rectangles.html"));
GURL url_b(embedded_test_server()->GetURL(
"b.com",
"/accessibility/hit_testing/simple_rectangles_scrolling_iframe.html"));
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
AccessibilityNotificationWaiter waiter(shell()->web_contents(),
ui::kAXModeComplete,
ax::mojom::Event::kLoadComplete);
EXPECT_TRUE(NavigateToURL(shell(), url_a));
ASSERT_TRUE(waiter.WaitForNotification());
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"rectA");
auto* web_contents = static_cast<WebContentsImpl*>(shell()->web_contents());
FrameTreeNode* root = web_contents->GetPrimaryFrameTree().root();
ASSERT_EQ(1U, root->child_count());
FrameTreeNode* child = root->child_at(0);
EXPECT_TRUE(NavigateToURLFromRenderer(child, url_b));
EXPECT_EQ(url_b, child->current_url());
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"rectF");
EXPECT_EQ(
" Site A ------------ proxies for B\n"
" +--Site B ------- proxies for A\n"
"Where A = http://a.com/\n"
" B = http://b.com/",
DepictFrameTree(*root));
// Before scrolling.
{
gfx::Point rect_b_point(79, 79);
ui::BrowserAccessibility* hit_node = HitTestAndWaitForResult(rect_b_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rectB");
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_b_point, expected_node, hit_node);
// Try callback API.
hit_node = AsyncHitTestAndWaitForCallback(rect_b_point);
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_b_point, expected_node, hit_node);
}
// Scroll div up 100px.
int scroll_delta = 100;
std::string scroll_string = base::StringPrintf(
"window.scrollTo(0, %d); window.scrollY;", scroll_delta);
EXPECT_NEAR(
EvalJs(child->current_frame_host(), scroll_string).ExtractDouble(),
static_cast<double>(scroll_delta), 1.0);
// After scrolling.
{
gfx::Point rect_g_point(79, 89);
ui::BrowserAccessibility* hit_node = HitTestAndWaitForResult(rect_g_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rectG");
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_g_point, expected_node, hit_node);
// Try callback API.
hit_node = AsyncHitTestAndWaitForCallback(rect_g_point);
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_g_point, expected_node, hit_node);
}
}
IN_PROC_BROWSER_TEST_P(AccessibilityHitTestingBrowserTest,
CachingAsyncHitTestMissesElement) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
AccessibilityNotificationWaiter waiter(shell()->web_contents(),
ui::kAXModeComplete,
ax::mojom::Event::kLoadComplete);
GURL url(embedded_test_server()->GetURL(
"/accessibility/hit_testing/simple_rectangles_with_curtain.html"));
EXPECT_TRUE(NavigateToURL(shell(), url));
ASSERT_TRUE(waiter.WaitForNotification());
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"rectA");
// For each point we try, the first time we call CachingAsyncHitTest it
// should FAIL and return the wrong object, because this test page has
// been designed to confound local synchronous hit testing using
// z-indexes. However, calling CachingAsyncHitTest a second time should
// return the correct result (since CallCachingAsyncHitTest waits for the
// HOVER event to be received).
// Test a hit on a rect in the main frame.
{
// First call should land on the wrong element.
gfx::Point rect_2_point(49, 20);
ui::BrowserAccessibility* hit_node = CallCachingAsyncHitTest(rect_2_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rect2");
EXPECT_NE(expected_node->GetName(), hit_node->GetName());
// Call again and we should get the correct element.
hit_node = CallCachingAsyncHitTest(rect_2_point);
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_2_point, expected_node, hit_node);
}
// Test a hit on a rect in the iframe.
{
// First call should land on the wrong element.
gfx::Point rect_b_point(79, 79);
ui::BrowserAccessibility* hit_node = CallCachingAsyncHitTest(rect_b_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rectB");
EXPECT_NE(expected_node->GetName(), hit_node->GetName());
// Call again and we should get the correct element.
hit_node = CallCachingAsyncHitTest(rect_b_point);
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_b_point, expected_node, hit_node);
}
}
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_MAC)
// Fails flakily with compared ID differences. TODO(crbug.com/40715277):
// Re-enable this test.
IN_PROC_BROWSER_TEST_P(AccessibilityHitTestingBrowserTest,
DISABLED_CachingAsyncHitTest_WithPinchZoom) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
AccessibilityNotificationWaiter waiter(shell()->web_contents(),
ui::kAXModeComplete,
ax::mojom::Event::kLoadComplete);
GURL url(embedded_test_server()->GetURL(
"/accessibility/hit_testing/simple_rectangles.html"));
EXPECT_TRUE(NavigateToURL(shell(), url));
SynchronizeThreads();
// TODO(crbug.com/40844856): Investigate why this does not return
// true.
ASSERT_TRUE(waiter.WaitForNotification());
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"rectA");
// Apply pinch zoom.
SimulatePinchZoom(1.25f);
// Test a hit on a rect in the main frame.
{
gfx::Point rect_2_point(49, 20);
ui::BrowserAccessibility* hit_node = CallCachingAsyncHitTest(rect_2_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rect2");
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_2_point, expected_node, hit_node);
}
// Test a hit on a rect in the iframe.
{
gfx::Point rect_b_point(79, 79);
ui::BrowserAccessibility* hit_node = CallCachingAsyncHitTest(rect_b_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rectB");
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_b_point, expected_node, hit_node);
}
}
// TODO(crbug.com/40775545): Times out flakily on TSAN builds.
// TODO(crbug.com/40919503): Times out flakily on ASan builds.
// TODO(crbug.com/40921699): Times out flakily on win-asan.
IN_PROC_BROWSER_TEST_P(AccessibilityHitTestingBrowserTest,
DISABLED_HitTest_WithPinchZoom) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
AccessibilityNotificationWaiter waiter(shell()->web_contents(),
ui::kAXModeComplete,
ax::mojom::Event::kLoadComplete);
GURL url(embedded_test_server()->GetURL(
"/accessibility/hit_testing/simple_rectangles.html"));
EXPECT_TRUE(NavigateToURL(shell(), url));
SynchronizeThreads();
// TODO(crbug.com/40844856): Investigate why this does not return
// true.
ASSERT_TRUE(waiter.WaitForNotification());
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"rectA");
// Apply pinch zoom.
SimulatePinchZoom(1.25f);
// Test a hit on a rect in the main frame.
{
gfx::Point rect_2_point(49, 20);
ui::BrowserAccessibility* hit_node = HitTestAndWaitForResult(rect_2_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rect2");
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_2_point, expected_node, hit_node);
// Try callback API.
hit_node = AsyncHitTestAndWaitForCallback(rect_2_point);
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_2_point, expected_node, hit_node);
}
// Test a hit on a rect in the iframe.
{
gfx::Point rect_b_point(79, 79);
ui::BrowserAccessibility* hit_node = HitTestAndWaitForResult(rect_b_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rectB");
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_b_point, expected_node, hit_node);
// Try callback API.
hit_node = AsyncHitTestAndWaitForCallback(rect_b_point);
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_b_point, expected_node, hit_node);
}
}
// Timeouts on Linux. TODO(crbug.com/40692703): Enable this test.
IN_PROC_BROWSER_TEST_P(
AccessibilityHitTestingBrowserTest,
DISABLED_CachingAsyncHitTestMissesElement_WithPinchZoom) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
AccessibilityNotificationWaiter waiter(shell()->web_contents(),
ui::kAXModeComplete,
ax::mojom::Event::kLoadComplete);
GURL url(embedded_test_server()->GetURL(
"/accessibility/hit_testing/simple_rectangles_with_curtain.html"));
EXPECT_TRUE(NavigateToURL(shell(), url));
ASSERT_TRUE(waiter.WaitForNotification());
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"rectA");
// Apply pinch zoom.
SimulatePinchZoom(1.25f);
// For each point we try, the first time we call CachingAsyncHitTest it
// should FAIL and return the wrong object, because this test page has
// been designed to confound local synchronous hit testing using
// z-indexes. However, calling CachingAsyncHitTest a second time should
// return the correct result (since CallCachingAsyncHitTest waits for the
// HOVER event to be received).
// Test a hit on a rect in the main frame.
{
// First call should land on the wrong element.
gfx::Point rect_2_point(49, 20);
ui::BrowserAccessibility* hit_node = CallCachingAsyncHitTest(rect_2_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rect2");
EXPECT_NE(expected_node->GetName(), hit_node->GetName());
// Call again and we should get the correct element.
hit_node = CallCachingAsyncHitTest(rect_2_point);
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_2_point, expected_node, hit_node);
}
// Test a hit on a rect in the iframe.
{
// First call should land on the wrong element.
gfx::Point rect_b_point(79, 79);
ui::BrowserAccessibility* hit_node = CallCachingAsyncHitTest(rect_b_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rectB");
EXPECT_NE(expected_node->GetName(), hit_node->GetName());
// Call again and we should get the correct element.
hit_node = CallCachingAsyncHitTest(rect_b_point);
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_b_point, expected_node, hit_node);
}
}
#endif // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_MAC)
// GetAXPlatformNode is currently only supported on windows and linux (excluding
// Chrome OS or Chromecast)
#if BUILDFLAG(IS_WIN) || (BUILDFLAG(IS_LINUX) && !BUILDFLAG(IS_CASTOS))
IN_PROC_BROWSER_TEST_P(AccessibilityHitTestingBrowserTest,
NearestLeafInIframes) {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL)));
AccessibilityNotificationWaiter waiter(shell()->web_contents(),
ui::kAXModeComplete,
ax::mojom::Event::kLoadComplete);
GURL url(embedded_test_server()->GetURL(
"/accessibility/hit_testing/text_ranges.html"));
EXPECT_TRUE(NavigateToURL(shell(), url));
ASSERT_TRUE(waiter.WaitForNotification());
WaitForAccessibilityTreeToContainNodeWithName(shell()->web_contents(),
"rectA");
// Test a hit on text in the main frame.
{
gfx::Point rect_2_point(70, 20);
ui::BrowserAccessibility* hit_node = CallNearestLeafNode(rect_2_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kStaticText, "2");
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_2_point, expected_node, hit_node);
}
// Test a hit on text in the iframe.
{
gfx::Point rect_b_point(100, 100);
ui::BrowserAccessibility* hit_node = CallNearestLeafNode(rect_b_point);
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kStaticText, "B");
EXPECT_ACCESSIBILITY_HIT_TEST_RESULT(rect_b_point, expected_node, hit_node);
}
}
#endif
} // namespace content