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
content / browser / accessibility / hit_testing_win_browsertest.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_ACCESSIBILITY_HIT_TESTING_WIN_BROWSERTEST_H_
#define CONTENT_BROWSER_ACCESSIBILITY_HIT_TESTING_WIN_BROWSERTEST_H_
#include <objbase.h>
#include <wrl/client.h>
#include "base/test/scoped_feature_list.h"
#include "base/win/scoped_variant.h"
#include "content/browser/accessibility/hit_testing_browsertest.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_utils.h"
#include "content/shell/browser/shell.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/accessibility/platform/browser_accessibility.h"
#include "ui/accessibility/platform/browser_accessibility_manager.h"
#include <uiautomation.h>
using Microsoft::WRL::ComPtr;
namespace content {
#define EXPECT_ACCESSIBILITY_WIN_HIT_TEST_RESULT(css_point, expected_unknown, \
hit_unknown) \
SCOPED_TRACE(GetScopedTrace(css_point)); \
EXPECT_EQ(expected_unknown, hit_unknown);
class AccessibilityHitTestingWinBrowserTest
: public AccessibilityHitTestingBrowserTest {
public:
ComPtr<IAccessible> GetWebContentRootIAccessible() {
ComPtr<IAccessible> content_root;
GetRootBrowserAccessibilityManager()
->GetBrowserAccessibilityRoot()
->GetNativeViewAccessible()
->QueryInterface(IID_PPV_ARGS(&content_root));
return content_root;
}
ComPtr<IRawElementProviderFragmentRoot> GetWebContentFragmentRoot() {
ComPtr<IRawElementProviderFragment> content_root;
GetWebContentRootIAccessible().As(&content_root);
ComPtr<IRawElementProviderFragmentRoot> fragment_root;
content_root->get_FragmentRoot(&fragment_root);
return fragment_root;
}
ComPtr<ITextProvider> GetWebContentRootTextProvider() {
ComPtr<IRawElementProviderSimple> content_root;
GetWebContentRootIAccessible().As(&content_root);
ComPtr<ITextProvider> text_provider;
content_root->GetPatternProvider(UIA_TextPatternId, &text_provider);
return text_provider;
}
private:
base::test::ScopedFeatureList scoped_feature_list_{::features::kUiaProvider};
};
INSTANTIATE_TEST_SUITE_P(
All,
AccessibilityHitTestingWinBrowserTest,
::testing::Values(1, 2),
AccessibilityHitTestingBrowserTest::TestPassToString());
IN_PROC_BROWSER_TEST_P(AccessibilityHitTestingWinBrowserTest, AccHitTest) {
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");
ComPtr<IAccessible> root_accessible = GetWebContentRootIAccessible();
// Test a hit on a rect in the main frame.
{
gfx::Point rect_2_point(49, 20);
gfx::Point rect_2_point_physical = CSSToPhysicalPixelPoint(rect_2_point);
base::win::ScopedVariant hit_variant;
ASSERT_HRESULT_SUCCEEDED(root_accessible->accHitTest(
rect_2_point_physical.x(), rect_2_point_physical.y(),
hit_variant.Receive()));
ASSERT_EQ(hit_variant.type(), VT_DISPATCH);
ComPtr<IAccessible> hit_accessible;
ASSERT_HRESULT_SUCCEEDED(hit_variant.ptr()->pdispVal->QueryInterface(
IID_PPV_ARGS(&hit_accessible)));
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rect2");
ComPtr<IAccessible> expected_accessible;
ASSERT_HRESULT_SUCCEEDED(
expected_node->GetNativeViewAccessible()->QueryInterface(
IID_PPV_ARGS(&expected_accessible)));
EXPECT_ACCESSIBILITY_WIN_HIT_TEST_RESULT(
rect_2_point, expected_accessible.Get(), hit_accessible.Get());
}
// Test a hit on a rect in the iframe.
{
gfx::Point rect_b_point(79, 79);
gfx::Point rect_b_point_physical = CSSToPhysicalPixelPoint(rect_b_point);
base::win::ScopedVariant hit_variant;
ASSERT_HRESULT_SUCCEEDED(root_accessible->accHitTest(
rect_b_point_physical.x(), rect_b_point_physical.y(),
hit_variant.Receive()));
ASSERT_EQ(hit_variant.type(), VT_DISPATCH);
ComPtr<IAccessible> hit_accessible;
ASSERT_HRESULT_SUCCEEDED(hit_variant.ptr()->pdispVal->QueryInterface(
IID_PPV_ARGS(&hit_accessible)));
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rectB");
ComPtr<IAccessible> expected_accessible;
ASSERT_HRESULT_SUCCEEDED(
expected_node->GetNativeViewAccessible()->QueryInterface(
IID_PPV_ARGS(&expected_accessible)));
EXPECT_ACCESSIBILITY_WIN_HIT_TEST_RESULT(
rect_b_point, expected_accessible.Get(), hit_accessible.Get());
}
}
IN_PROC_BROWSER_TEST_P(AccessibilityHitTestingWinBrowserTest,
ElementProviderFromPoint) {
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");
ComPtr<IRawElementProviderFragmentRoot> fragment_root =
GetWebContentFragmentRoot();
// Test a hit on a rect in the main frame.
{
gfx::Point rect_2_point(49, 20);
gfx::Point rect_2_point_physical = CSSToPhysicalPixelPoint(rect_2_point);
ComPtr<IRawElementProviderFragment> hit_fragment;
ASSERT_HRESULT_SUCCEEDED(fragment_root->ElementProviderFromPoint(
rect_2_point_physical.x(), rect_2_point_physical.y(), &hit_fragment));
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rect2");
ComPtr<IRawElementProviderFragment> expected_fragment;
ASSERT_HRESULT_SUCCEEDED(
expected_node->GetNativeViewAccessible()->QueryInterface(
IID_PPV_ARGS(&expected_fragment)));
EXPECT_ACCESSIBILITY_WIN_HIT_TEST_RESULT(
rect_2_point, expected_fragment.Get(), hit_fragment.Get());
}
// Test a hit on a rect in the iframe.
{
gfx::Point rect_b_point(79, 79);
gfx::Point rect_b_point_physical = CSSToPhysicalPixelPoint(rect_b_point);
ComPtr<IRawElementProviderFragment> hit_fragment;
ASSERT_HRESULT_SUCCEEDED(fragment_root->ElementProviderFromPoint(
rect_b_point_physical.x(), rect_b_point_physical.y(), &hit_fragment));
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rectB");
ComPtr<IRawElementProviderFragment> expected_fragment;
ASSERT_HRESULT_SUCCEEDED(
expected_node->GetNativeViewAccessible()->QueryInterface(
IID_PPV_ARGS(&expected_fragment)));
EXPECT_EQ(hit_fragment.Get(), expected_fragment.Get());
EXPECT_ACCESSIBILITY_WIN_HIT_TEST_RESULT(
rect_b_point, expected_fragment.Get(), hit_fragment.Get());
}
}
IN_PROC_BROWSER_TEST_P(AccessibilityHitTestingWinBrowserTest,
TextProviderRangeFromPoint) {
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");
ComPtr<ITextProvider> text_provider = GetWebContentRootTextProvider();
// Test a hit on a text in the main frame.
{
gfx::Point rect_2_point(70, 20);
gfx::Point rect_2_point_physical = CSSToPhysicalPixelPoint(rect_2_point);
UiaPoint uia_point;
uia_point.x = rect_2_point_physical.x();
uia_point.y = rect_2_point_physical.y();
ComPtr<ITextRangeProvider> hit_text_range;
ASSERT_HRESULT_SUCCEEDED(
text_provider->RangeFromPoint(uia_point, &hit_text_range));
ASSERT_HRESULT_SUCCEEDED(
hit_text_range->ExpandToEnclosingUnit(TextUnit_Character));
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rect2");
ComPtr<IRawElementProviderSimple> expected_provider;
ASSERT_HRESULT_SUCCEEDED(
expected_node->GetNativeViewAccessible()->QueryInterface(
IID_PPV_ARGS(&expected_provider)));
ComPtr<ITextRangeProvider> expected_text_range;
ASSERT_HRESULT_SUCCEEDED(text_provider->RangeFromChild(
expected_provider.Get(), &expected_text_range));
SCOPED_TRACE(GetScopedTrace(rect_2_point));
BOOL compare_result;
ASSERT_HRESULT_SUCCEEDED(
hit_text_range->Compare(expected_text_range.Get(), &compare_result));
EXPECT_TRUE(compare_result);
}
// Test a hit on a text in the iframe.
{
gfx::Point rect_b_point(100, 100);
gfx::Point rect_b_point_physical = CSSToPhysicalPixelPoint(rect_b_point);
UiaPoint uia_point;
uia_point.x = rect_b_point_physical.x();
uia_point.y = rect_b_point_physical.y();
ComPtr<ITextRangeProvider> hit_text_range;
ASSERT_HRESULT_SUCCEEDED(
text_provider->RangeFromPoint(uia_point, &hit_text_range));
ASSERT_HRESULT_SUCCEEDED(
hit_text_range->ExpandToEnclosingUnit(TextUnit_Character));
ui::BrowserAccessibility* expected_node =
FindNode(ax::mojom::Role::kGenericContainer, "rectB");
ComPtr<IRawElementProviderSimple> expected_provider;
ASSERT_HRESULT_SUCCEEDED(
expected_node->GetNativeViewAccessible()->QueryInterface(
IID_PPV_ARGS(&expected_provider)));
ComPtr<ITextRangeProvider> expected_text_range;
ASSERT_HRESULT_SUCCEEDED(text_provider->RangeFromChild(
expected_provider.Get(), &expected_text_range));
SCOPED_TRACE(GetScopedTrace(rect_b_point));
BOOL compare_result;
ASSERT_HRESULT_SUCCEEDED(
hit_text_range->Compare(expected_text_range.Get(), &compare_result));
EXPECT_TRUE(compare_result);
}
}
} // namespace content
#endif // CONTENT_BROWSER_ACCESSIBILITY_HIT_TESTING_WIN_BROWSERTEST_H_