1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/strings/utf_string_conversions.h"
6 #include "base/time/time.h"
7 #include "content/common/frame_messages.h"
8 #include "content/common/site_isolation_policy.h"
9 #include "content/common/view_message_enums.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/public/test/render_view_test.h"
12 #include "content/renderer/accessibility/renderer_accessibility.h"
13 #include "content/renderer/render_frame_impl.h"
14 #include "content/renderer/render_view_impl.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/public/platform/WebSize.h"
17 #include "third_party/WebKit/public/web/WebAXObject.h"
18 #include "third_party/WebKit/public/web/WebDocument.h"
19 #include "third_party/WebKit/public/web/WebView.h"
20 #include "ui/accessibility/ax_node_data.h"
22 using blink::WebAXObject
;
23 using blink::WebDocument
;
27 class TestRendererAccessibility
: public RendererAccessibility
{
29 explicit TestRendererAccessibility(RenderFrameImpl
* render_frame
)
30 : RendererAccessibility(render_frame
) {
33 void SendPendingAccessibilityEvents() {
34 RendererAccessibility::SendPendingAccessibilityEvents();
38 class RendererAccessibilityTest
: public RenderViewTest
{
40 RendererAccessibilityTest() {}
42 RenderViewImpl
* view() {
43 return static_cast<RenderViewImpl
*>(view_
);
46 RenderFrameImpl
* frame() {
47 return static_cast<RenderFrameImpl
*>(view()->GetMainRenderFrame());
50 void SetUp() override
{
51 RenderViewTest::SetUp();
52 sink_
= &render_thread_
->sink();
55 void TearDown() override
{
56 #if defined(LEAK_SANITIZER)
57 // Do this before shutting down V8 in RenderViewTest::TearDown().
58 // http://crbug.com/328552
59 __lsan_do_leak_check();
61 RenderViewTest::TearDown();
64 void SetMode(AccessibilityMode mode
) {
65 frame()->OnSetAccessibilityMode(mode
);
69 AccessibilityHostMsg_EventParams
* params
) {
70 const IPC::Message
* message
=
71 sink_
->GetUniqueMessageMatching(AccessibilityHostMsg_Events::ID
);
73 base::Tuple
<std::vector
<AccessibilityHostMsg_EventParams
>, int> param
;
74 AccessibilityHostMsg_Events::Read(message
, ¶m
);
75 ASSERT_GE(base::get
<0>(param
).size(), 1U);
76 *params
= base::get
<0>(param
)[0];
79 int CountAccessibilityNodesSentToBrowser() {
80 AccessibilityHostMsg_EventParams event
;
81 GetLastAccEvent(&event
);
82 return event
.update
.nodes
.size();
88 DISALLOW_COPY_AND_ASSIGN(RendererAccessibilityTest
);
92 TEST_F(RendererAccessibilityTest
, SendFullAccessibilityTreeOnReload
) {
93 // The job of RendererAccessibility is to serialize the
94 // accessibility tree built by WebKit and send it to the browser.
95 // When the accessibility tree changes, it tries to send only
96 // the nodes that actually changed or were reparented. This test
97 // ensures that the messages sent are correct in cases when a page
98 // reloads, and that internal state is properly garbage-collected.
101 " <div role='group' id='A'>"
102 " <div role='group' id='A1'></div>"
103 " <div role='group' id='A2'></div>"
106 LoadHTML(html
.c_str());
108 // Creating a RendererAccessibility should sent the tree to the browser.
109 scoped_ptr
<TestRendererAccessibility
> accessibility(
110 new TestRendererAccessibility(frame()));
111 accessibility
->SendPendingAccessibilityEvents();
112 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
114 // If we post another event but the tree doesn't change,
115 // we should only send 1 node to the browser.
116 sink_
->ClearMessages();
117 WebDocument document
= view()->GetWebView()->mainFrame()->document();
118 WebAXObject root_obj
= document
.accessibilityObject();
119 accessibility
->HandleAXEvent(
121 ui::AX_EVENT_LAYOUT_COMPLETE
);
122 accessibility
->SendPendingAccessibilityEvents();
123 EXPECT_EQ(1, CountAccessibilityNodesSentToBrowser());
125 // Make sure it's the root object that was updated.
126 AccessibilityHostMsg_EventParams event
;
127 GetLastAccEvent(&event
);
128 EXPECT_EQ(root_obj
.axID(), event
.update
.nodes
[0].id
);
131 // If we reload the page and send a event, we should send
132 // all 4 nodes to the browser. Also double-check that we didn't
133 // leak any of the old BrowserTreeNodes.
134 LoadHTML(html
.c_str());
135 document
= view()->GetWebView()->mainFrame()->document();
136 root_obj
= document
.accessibilityObject();
137 sink_
->ClearMessages();
138 accessibility
->HandleAXEvent(
140 ui::AX_EVENT_LAYOUT_COMPLETE
);
141 accessibility
->SendPendingAccessibilityEvents();
142 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
144 // Even if the first event is sent on an element other than
145 // the root, the whole tree should be updated because we know
146 // the browser doesn't have the root element.
147 LoadHTML(html
.c_str());
148 document
= view()->GetWebView()->mainFrame()->document();
149 root_obj
= document
.accessibilityObject();
150 sink_
->ClearMessages();
151 const WebAXObject
& first_child
= root_obj
.childAt(0);
152 accessibility
->HandleAXEvent(
154 ui::AX_EVENT_LIVE_REGION_CHANGED
);
155 accessibility
->SendPendingAccessibilityEvents();
156 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
159 // http://crbug.com/253537
160 #if defined(OS_ANDROID)
161 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
162 DISABLED_AccessibilityMessagesQueueWhileSwappedOut
164 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
165 AccessibilityMessagesQueueWhileSwappedOut
168 TEST_F(RendererAccessibilityTest
,
169 MAYBE_AccessibilityMessagesQueueWhileSwappedOut
) {
170 // This test breaks down in --site-per-process, as swapping out destroys
171 // the main frame and it cannot be further navigated.
172 // TODO(nasko): Figure out what this behavior looks like when swapped out
174 if (SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
179 " <p>Hello, world.</p>"
181 LoadHTML(html
.c_str());
182 static const int kProxyRoutingId
= 13;
184 // Creating a RendererAccessibility should send the tree to the browser.
185 scoped_ptr
<TestRendererAccessibility
> accessibility(
186 new TestRendererAccessibility(frame()));
187 accessibility
->SendPendingAccessibilityEvents();
188 EXPECT_EQ(5, CountAccessibilityNodesSentToBrowser());
190 // Post a "value changed" event, but then swap out
191 // before sending it. It shouldn't send the event while
193 sink_
->ClearMessages();
194 WebDocument document
= view()->GetWebView()->mainFrame()->document();
195 WebAXObject root_obj
= document
.accessibilityObject();
196 accessibility
->HandleAXEvent(
198 ui::AX_EVENT_VALUE_CHANGED
);
199 view()->GetMainRenderFrame()->OnSwapOut(kProxyRoutingId
, true,
200 content::FrameReplicationState());
201 accessibility
->SendPendingAccessibilityEvents();
202 EXPECT_FALSE(sink_
->GetUniqueMessageMatching(
203 AccessibilityHostMsg_Events::ID
));
205 // Navigate, so we're not swapped out anymore. Now we should
206 // send accessibility events again. Note that the
207 // message that was queued up before will be quickly discarded
208 // because the element it was referring to no longer exists,
209 // so the event here is from loading this new page.
210 CommonNavigationParams common_params
;
211 RequestNavigationParams request_params
;
212 common_params
.url
= GURL("data:text/html,<p>Hello, again.</p>");
213 common_params
.navigation_type
= FrameMsg_Navigate_Type::NORMAL
;
214 common_params
.transition
= ui::PAGE_TRANSITION_TYPED
;
215 request_params
.current_history_list_length
= 1;
216 request_params
.current_history_list_offset
= 0;
217 request_params
.pending_history_list_offset
= 1;
218 request_params
.page_id
= -1;
219 frame()->OnNavigate(common_params
, StartNavigationParams(), request_params
);
220 accessibility
->SendPendingAccessibilityEvents();
221 EXPECT_TRUE(sink_
->GetUniqueMessageMatching(
222 AccessibilityHostMsg_Events::ID
));
225 TEST_F(RendererAccessibilityTest
, HideAccessibilityObject
) {
226 // Test RendererAccessibility and make sure it sends the
227 // proper event to the browser when an object in the tree
228 // is hidden, but its children are not.
231 " <div role='group' id='A'>"
232 " <div role='group' id='B'>"
233 " <div role='group' id='C' style='visibility:visible'>"
238 LoadHTML(html
.c_str());
240 scoped_ptr
<TestRendererAccessibility
> accessibility(
241 new TestRendererAccessibility(frame()));
242 accessibility
->SendPendingAccessibilityEvents();
243 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
245 WebDocument document
= view()->GetWebView()->mainFrame()->document();
246 WebAXObject root_obj
= document
.accessibilityObject();
247 WebAXObject node_a
= root_obj
.childAt(0);
248 WebAXObject node_b
= node_a
.childAt(0);
249 WebAXObject node_c
= node_b
.childAt(0);
251 // Hide node 'B' ('C' stays visible).
252 ExecuteJavaScriptForTests(
253 "document.getElementById('B').style.visibility = 'hidden';");
255 ExecuteJavaScriptForTests("document.getElementById('B').offsetLeft;");
257 // Send a childrenChanged on 'A'.
258 sink_
->ClearMessages();
259 accessibility
->HandleAXEvent(
261 ui::AX_EVENT_CHILDREN_CHANGED
);
263 accessibility
->SendPendingAccessibilityEvents();
264 AccessibilityHostMsg_EventParams event
;
265 GetLastAccEvent(&event
);
266 ASSERT_EQ(2U, event
.update
.nodes
.size());
268 // RendererAccessibility notices that 'C' is being reparented,
269 // so it clears the subtree rooted at 'A', then updates 'A' and then 'C'.
270 EXPECT_EQ(node_a
.axID(), event
.update
.node_id_to_clear
);
271 EXPECT_EQ(node_a
.axID(), event
.update
.nodes
[0].id
);
272 EXPECT_EQ(node_c
.axID(), event
.update
.nodes
[1].id
);
273 EXPECT_EQ(2, CountAccessibilityNodesSentToBrowser());
276 TEST_F(RendererAccessibilityTest
, ShowAccessibilityObject
) {
277 // Test RendererAccessibility and make sure it sends the
278 // proper event to the browser when an object in the tree
279 // is shown, causing its own already-visible children to be
283 " <div role='group' id='A'>"
284 " <div role='group' id='B' style='visibility:hidden'>"
285 " <div role='group' id='C' style='visibility:visible'>"
290 LoadHTML(html
.c_str());
292 scoped_ptr
<TestRendererAccessibility
> accessibility(
293 new TestRendererAccessibility(frame()));
294 accessibility
->SendPendingAccessibilityEvents();
295 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
297 // Show node 'B', then send a childrenChanged on 'A'.
298 ExecuteJavaScriptForTests(
299 "document.getElementById('B').style.visibility = 'visible';");
300 ExecuteJavaScriptForTests("document.getElementById('B').offsetLeft;");
302 sink_
->ClearMessages();
303 WebDocument document
= view()->GetWebView()->mainFrame()->document();
304 WebAXObject root_obj
= document
.accessibilityObject();
305 WebAXObject node_a
= root_obj
.childAt(0);
306 WebAXObject node_b
= node_a
.childAt(0);
307 WebAXObject node_c
= node_b
.childAt(0);
309 accessibility
->HandleAXEvent(
311 ui::AX_EVENT_CHILDREN_CHANGED
);
313 accessibility
->SendPendingAccessibilityEvents();
314 AccessibilityHostMsg_EventParams event
;
315 GetLastAccEvent(&event
);
317 ASSERT_EQ(3U, event
.update
.nodes
.size());
318 EXPECT_EQ(node_a
.axID(), event
.update
.node_id_to_clear
);
319 EXPECT_EQ(node_a
.axID(), event
.update
.nodes
[0].id
);
320 EXPECT_EQ(node_b
.axID(), event
.update
.nodes
[1].id
);
321 EXPECT_EQ(node_c
.axID(), event
.update
.nodes
[2].id
);
322 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
325 TEST_F(RendererAccessibilityTest
, DetachAccessibilityObject
) {
326 // Test RendererAccessibility and make sure it sends the
327 // proper event to the browser when an object in the tree
328 // is detached, but its children are not. This can happen when
329 // a layout occurs and an anonymous render block is no longer needed.
331 "<body aria-label='Body'>"
332 "<span>1</span><span style='display:block'>2</span>"
334 LoadHTML(html
.c_str());
336 scoped_ptr
<TestRendererAccessibility
> accessibility(
337 new TestRendererAccessibility(frame()));
338 accessibility
->SendPendingAccessibilityEvents();
339 EXPECT_EQ(7, CountAccessibilityNodesSentToBrowser());
341 // Initially, the accessibility tree looks like this:
345 // +--Anonymous Block
346 // +--Static Text "1"
347 // +--Inline Text Box "1"
348 // +--Static Text "2"
349 // +--Inline Text Box "2"
350 WebDocument document
= view()->GetWebView()->mainFrame()->document();
351 WebAXObject root_obj
= document
.accessibilityObject();
352 WebAXObject body
= root_obj
.childAt(0);
353 WebAXObject anonymous_block
= body
.childAt(0);
354 WebAXObject text_1
= anonymous_block
.childAt(0);
355 WebAXObject text_2
= body
.childAt(1);
357 // Change the display of the second 'span' back to inline, which causes the
358 // anonymous block to be destroyed.
359 ExecuteJavaScriptForTests(
360 "document.querySelectorAll('span')[1].style.display = 'inline';");
362 ExecuteJavaScriptForTests("document.body.offsetLeft;");
364 // Send a childrenChanged on the body.
365 sink_
->ClearMessages();
366 accessibility
->HandleAXEvent(
368 ui::AX_EVENT_CHILDREN_CHANGED
);
370 accessibility
->SendPendingAccessibilityEvents();
372 // Afterwards, the accessibility tree looks like this:
376 // +--Static Text "1"
377 // +--Inline Text Box "1"
378 // +--Static Text "2"
379 // +--Inline Text Box "2"
381 // We just assert that there are now four nodes in the
382 // accessibility tree and that only three nodes needed
383 // to be updated (the body, the static text 1, and
384 // the static text 2).
386 AccessibilityHostMsg_EventParams event
;
387 GetLastAccEvent(&event
);
388 ASSERT_EQ(5U, event
.update
.nodes
.size());
390 EXPECT_EQ(body
.axID(), event
.update
.nodes
[0].id
);
391 EXPECT_EQ(text_1
.axID(), event
.update
.nodes
[1].id
);
392 // The third event is to update text_2, but its id changes
393 // so we don't have a test expectation for it.
396 TEST_F(RendererAccessibilityTest
, EventOnObjectNotInTree
) {
397 // Test RendererAccessibility and make sure it doesn't send anything
398 // if we get a notification from Blink for an object that isn't in the
399 // tree, like the scroll area that's the parent of the main document,
400 // which we don't expose.
401 std::string html
= "<body><input></body>";
402 LoadHTML(html
.c_str());
404 scoped_ptr
<TestRendererAccessibility
> accessibility(
405 new TestRendererAccessibility(frame()));
406 accessibility
->SendPendingAccessibilityEvents();
407 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
409 WebDocument document
= view()->GetWebView()->mainFrame()->document();
410 WebAXObject root_obj
= document
.accessibilityObject();
411 WebAXObject scroll_area
= root_obj
.parentObject();
412 EXPECT_EQ(blink::WebAXRoleScrollArea
, scroll_area
.role());
414 // Try to fire a message on the scroll area, and assert that we just
416 sink_
->ClearMessages();
417 accessibility
->HandleAXEvent(scroll_area
,
418 ui::AX_EVENT_VALUE_CHANGED
);
420 accessibility
->SendPendingAccessibilityEvents();
422 const IPC::Message
* message
=
423 sink_
->GetUniqueMessageMatching(AccessibilityHostMsg_Events::ID
);
424 ASSERT_TRUE(message
);
425 base::Tuple
<std::vector
<AccessibilityHostMsg_EventParams
>, int> param
;
426 AccessibilityHostMsg_Events::Read(message
, ¶m
);
427 ASSERT_EQ(0U, base::get
<0>(param
).size());
430 } // namespace content