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/view_message_enums.h"
9 #include "content/public/common/content_switches.h"
10 #include "content/public/test/render_view_test.h"
11 #include "content/renderer/accessibility/renderer_accessibility.h"
12 #include "content/renderer/render_frame_impl.h"
13 #include "content/renderer/render_view_impl.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/WebKit/public/platform/WebSize.h"
16 #include "third_party/WebKit/public/web/WebAXObject.h"
17 #include "third_party/WebKit/public/web/WebDocument.h"
18 #include "third_party/WebKit/public/web/WebView.h"
19 #include "ui/accessibility/ax_node_data.h"
21 using blink::WebAXObject
;
22 using blink::WebDocument
;
26 class TestRendererAccessibility
: public RendererAccessibility
{
28 explicit TestRendererAccessibility(RenderFrameImpl
* render_frame
)
29 : RendererAccessibility(render_frame
) {
32 void SendPendingAccessibilityEvents() {
33 RendererAccessibility::SendPendingAccessibilityEvents();
37 class RendererAccessibilityTest
: public RenderViewTest
{
39 RendererAccessibilityTest() {}
41 RenderViewImpl
* view() {
42 return static_cast<RenderViewImpl
*>(view_
);
45 RenderFrameImpl
* frame() {
46 return static_cast<RenderFrameImpl
*>(view()->GetMainRenderFrame());
49 void SetUp() override
{
50 RenderViewTest::SetUp();
51 sink_
= &render_thread_
->sink();
54 void TearDown() override
{
55 #if defined(LEAK_SANITIZER)
56 // Do this before shutting down V8 in RenderViewTest::TearDown().
57 // http://crbug.com/328552
58 __lsan_do_leak_check();
60 RenderViewTest::TearDown();
63 void SetMode(AccessibilityMode mode
) {
64 frame()->OnSetAccessibilityMode(mode
);
68 AccessibilityHostMsg_EventParams
* params
) {
69 const IPC::Message
* message
=
70 sink_
->GetUniqueMessageMatching(AccessibilityHostMsg_Events::ID
);
72 base::Tuple
<std::vector
<AccessibilityHostMsg_EventParams
>, int> param
;
73 AccessibilityHostMsg_Events::Read(message
, ¶m
);
74 ASSERT_GE(base::get
<0>(param
).size(), 1U);
75 *params
= base::get
<0>(param
)[0];
78 int CountAccessibilityNodesSentToBrowser() {
79 AccessibilityHostMsg_EventParams event
;
80 GetLastAccEvent(&event
);
81 return event
.update
.nodes
.size();
87 DISALLOW_COPY_AND_ASSIGN(RendererAccessibilityTest
);
91 TEST_F(RendererAccessibilityTest
, SendFullAccessibilityTreeOnReload
) {
92 // The job of RendererAccessibility is to serialize the
93 // accessibility tree built by WebKit and send it to the browser.
94 // When the accessibility tree changes, it tries to send only
95 // the nodes that actually changed or were reparented. This test
96 // ensures that the messages sent are correct in cases when a page
97 // reloads, and that internal state is properly garbage-collected.
100 " <div role='group' id='A'>"
101 " <div role='group' id='A1'></div>"
102 " <div role='group' id='A2'></div>"
105 LoadHTML(html
.c_str());
107 // Creating a RendererAccessibility should sent the tree to the browser.
108 scoped_ptr
<TestRendererAccessibility
> accessibility(
109 new TestRendererAccessibility(frame()));
110 accessibility
->SendPendingAccessibilityEvents();
111 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
113 // If we post another event but the tree doesn't change,
114 // we should only send 1 node to the browser.
115 sink_
->ClearMessages();
116 WebDocument document
= view()->GetWebView()->mainFrame()->document();
117 WebAXObject root_obj
= document
.accessibilityObject();
118 accessibility
->HandleAXEvent(
120 ui::AX_EVENT_LAYOUT_COMPLETE
);
121 accessibility
->SendPendingAccessibilityEvents();
122 EXPECT_EQ(1, CountAccessibilityNodesSentToBrowser());
124 // Make sure it's the root object that was updated.
125 AccessibilityHostMsg_EventParams event
;
126 GetLastAccEvent(&event
);
127 EXPECT_EQ(root_obj
.axID(), event
.update
.nodes
[0].id
);
130 // If we reload the page and send a event, we should send
131 // all 4 nodes to the browser. Also double-check that we didn't
132 // leak any of the old BrowserTreeNodes.
133 LoadHTML(html
.c_str());
134 document
= view()->GetWebView()->mainFrame()->document();
135 root_obj
= document
.accessibilityObject();
136 sink_
->ClearMessages();
137 accessibility
->HandleAXEvent(
139 ui::AX_EVENT_LAYOUT_COMPLETE
);
140 accessibility
->SendPendingAccessibilityEvents();
141 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
143 // Even if the first event is sent on an element other than
144 // the root, the whole tree should be updated because we know
145 // the browser doesn't have the root element.
146 LoadHTML(html
.c_str());
147 document
= view()->GetWebView()->mainFrame()->document();
148 root_obj
= document
.accessibilityObject();
149 sink_
->ClearMessages();
150 const WebAXObject
& first_child
= root_obj
.childAt(0);
151 accessibility
->HandleAXEvent(
153 ui::AX_EVENT_LIVE_REGION_CHANGED
);
154 accessibility
->SendPendingAccessibilityEvents();
155 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
158 // http://crbug.com/253537
159 #if defined(OS_ANDROID)
160 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
161 DISABLED_AccessibilityMessagesQueueWhileSwappedOut
163 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
164 AccessibilityMessagesQueueWhileSwappedOut
167 TEST_F(RendererAccessibilityTest
,
168 MAYBE_AccessibilityMessagesQueueWhileSwappedOut
) {
169 // This test breaks down in --site-per-process, as swapping out destroys
170 // the main frame and it cannot be further navigated.
171 // TODO(nasko): Figure out what this behavior looks like when swapped out
173 if (RenderFrameProxy::IsSwappedOutStateForbidden()) {
178 " <p>Hello, world.</p>"
180 LoadHTML(html
.c_str());
181 static const int kProxyRoutingId
= 13;
183 // Creating a RendererAccessibility should send the tree to the browser.
184 scoped_ptr
<TestRendererAccessibility
> accessibility(
185 new TestRendererAccessibility(frame()));
186 accessibility
->SendPendingAccessibilityEvents();
187 EXPECT_EQ(5, CountAccessibilityNodesSentToBrowser());
189 // Post a "value changed" event, but then swap out
190 // before sending it. It shouldn't send the event while
192 sink_
->ClearMessages();
193 WebDocument document
= view()->GetWebView()->mainFrame()->document();
194 WebAXObject root_obj
= document
.accessibilityObject();
195 accessibility
->HandleAXEvent(
197 ui::AX_EVENT_VALUE_CHANGED
);
198 view()->GetMainRenderFrame()->OnSwapOut(kProxyRoutingId
, true,
199 content::FrameReplicationState());
200 accessibility
->SendPendingAccessibilityEvents();
201 EXPECT_FALSE(sink_
->GetUniqueMessageMatching(
202 AccessibilityHostMsg_Events::ID
));
204 // Navigate, so we're not swapped out anymore. Now we should
205 // send accessibility events again. Note that the
206 // message that was queued up before will be quickly discarded
207 // because the element it was referring to no longer exists,
208 // so the event here is from loading this new page.
209 CommonNavigationParams common_params
;
210 RequestNavigationParams request_params
;
211 common_params
.url
= GURL("data:text/html,<p>Hello, again.</p>");
212 common_params
.navigation_type
= FrameMsg_Navigate_Type::NORMAL
;
213 common_params
.transition
= ui::PAGE_TRANSITION_TYPED
;
214 request_params
.current_history_list_length
= 1;
215 request_params
.current_history_list_offset
= 0;
216 request_params
.pending_history_list_offset
= 1;
217 request_params
.page_id
= -1;
218 frame()->OnNavigate(common_params
, StartNavigationParams(), request_params
);
219 accessibility
->SendPendingAccessibilityEvents();
220 EXPECT_TRUE(sink_
->GetUniqueMessageMatching(
221 AccessibilityHostMsg_Events::ID
));
224 TEST_F(RendererAccessibilityTest
, HideAccessibilityObject
) {
225 // Test RendererAccessibility and make sure it sends the
226 // proper event to the browser when an object in the tree
227 // is hidden, but its children are not.
230 " <div role='group' id='A'>"
231 " <div role='group' id='B'>"
232 " <div role='group' id='C' style='visibility:visible'>"
237 LoadHTML(html
.c_str());
239 scoped_ptr
<TestRendererAccessibility
> accessibility(
240 new TestRendererAccessibility(frame()));
241 accessibility
->SendPendingAccessibilityEvents();
242 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
244 WebDocument document
= view()->GetWebView()->mainFrame()->document();
245 WebAXObject root_obj
= document
.accessibilityObject();
246 WebAXObject node_a
= root_obj
.childAt(0);
247 WebAXObject node_b
= node_a
.childAt(0);
248 WebAXObject node_c
= node_b
.childAt(0);
250 // Hide node 'B' ('C' stays visible).
251 ExecuteJavaScriptForTests(
252 "document.getElementById('B').style.visibility = 'hidden';");
254 ExecuteJavaScriptForTests("document.getElementById('B').offsetLeft;");
256 // Send a childrenChanged on 'A'.
257 sink_
->ClearMessages();
258 accessibility
->HandleAXEvent(
260 ui::AX_EVENT_CHILDREN_CHANGED
);
262 accessibility
->SendPendingAccessibilityEvents();
263 AccessibilityHostMsg_EventParams event
;
264 GetLastAccEvent(&event
);
265 ASSERT_EQ(2U, event
.update
.nodes
.size());
267 // RendererAccessibility notices that 'C' is being reparented,
268 // so it clears the subtree rooted at 'A', then updates 'A' and then 'C'.
269 EXPECT_EQ(node_a
.axID(), event
.update
.node_id_to_clear
);
270 EXPECT_EQ(node_a
.axID(), event
.update
.nodes
[0].id
);
271 EXPECT_EQ(node_c
.axID(), event
.update
.nodes
[1].id
);
272 EXPECT_EQ(2, CountAccessibilityNodesSentToBrowser());
275 TEST_F(RendererAccessibilityTest
, ShowAccessibilityObject
) {
276 // Test RendererAccessibility and make sure it sends the
277 // proper event to the browser when an object in the tree
278 // is shown, causing its own already-visible children to be
282 " <div role='group' id='A'>"
283 " <div role='group' id='B' style='visibility:hidden'>"
284 " <div role='group' id='C' style='visibility:visible'>"
289 LoadHTML(html
.c_str());
291 scoped_ptr
<TestRendererAccessibility
> accessibility(
292 new TestRendererAccessibility(frame()));
293 accessibility
->SendPendingAccessibilityEvents();
294 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
296 // Show node 'B', then send a childrenChanged on 'A'.
297 ExecuteJavaScriptForTests(
298 "document.getElementById('B').style.visibility = 'visible';");
299 ExecuteJavaScriptForTests("document.getElementById('B').offsetLeft;");
301 sink_
->ClearMessages();
302 WebDocument document
= view()->GetWebView()->mainFrame()->document();
303 WebAXObject root_obj
= document
.accessibilityObject();
304 WebAXObject node_a
= root_obj
.childAt(0);
305 WebAXObject node_b
= node_a
.childAt(0);
306 WebAXObject node_c
= node_b
.childAt(0);
308 accessibility
->HandleAXEvent(
310 ui::AX_EVENT_CHILDREN_CHANGED
);
312 accessibility
->SendPendingAccessibilityEvents();
313 AccessibilityHostMsg_EventParams event
;
314 GetLastAccEvent(&event
);
316 ASSERT_EQ(3U, event
.update
.nodes
.size());
317 EXPECT_EQ(node_a
.axID(), event
.update
.node_id_to_clear
);
318 EXPECT_EQ(node_a
.axID(), event
.update
.nodes
[0].id
);
319 EXPECT_EQ(node_b
.axID(), event
.update
.nodes
[1].id
);
320 EXPECT_EQ(node_c
.axID(), event
.update
.nodes
[2].id
);
321 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
324 TEST_F(RendererAccessibilityTest
, DetachAccessibilityObject
) {
325 // Test RendererAccessibility and make sure it sends the
326 // proper event to the browser when an object in the tree
327 // is detached, but its children are not. This can happen when
328 // a layout occurs and an anonymous render block is no longer needed.
330 "<body aria-label='Body'>"
331 "<span>1</span><span style='display:block'>2</span>"
333 LoadHTML(html
.c_str());
335 scoped_ptr
<TestRendererAccessibility
> accessibility(
336 new TestRendererAccessibility(frame()));
337 accessibility
->SendPendingAccessibilityEvents();
338 EXPECT_EQ(7, CountAccessibilityNodesSentToBrowser());
340 // Initially, the accessibility tree looks like this:
344 // +--Anonymous Block
345 // +--Static Text "1"
346 // +--Inline Text Box "1"
347 // +--Static Text "2"
348 // +--Inline Text Box "2"
349 WebDocument document
= view()->GetWebView()->mainFrame()->document();
350 WebAXObject root_obj
= document
.accessibilityObject();
351 WebAXObject body
= root_obj
.childAt(0);
352 WebAXObject anonymous_block
= body
.childAt(0);
353 WebAXObject text_1
= anonymous_block
.childAt(0);
354 WebAXObject text_2
= body
.childAt(1);
356 // Change the display of the second 'span' back to inline, which causes the
357 // anonymous block to be destroyed.
358 ExecuteJavaScriptForTests(
359 "document.querySelectorAll('span')[1].style.display = 'inline';");
361 ExecuteJavaScriptForTests("document.body.offsetLeft;");
363 // Send a childrenChanged on the body.
364 sink_
->ClearMessages();
365 accessibility
->HandleAXEvent(
367 ui::AX_EVENT_CHILDREN_CHANGED
);
369 accessibility
->SendPendingAccessibilityEvents();
371 // Afterwards, the accessibility tree looks like this:
375 // +--Static Text "1"
376 // +--Inline Text Box "1"
377 // +--Static Text "2"
378 // +--Inline Text Box "2"
380 // We just assert that there are now four nodes in the
381 // accessibility tree and that only three nodes needed
382 // to be updated (the body, the static text 1, and
383 // the static text 2).
385 AccessibilityHostMsg_EventParams event
;
386 GetLastAccEvent(&event
);
387 ASSERT_EQ(5U, event
.update
.nodes
.size());
389 EXPECT_EQ(body
.axID(), event
.update
.nodes
[0].id
);
390 EXPECT_EQ(text_1
.axID(), event
.update
.nodes
[1].id
);
391 // The third event is to update text_2, but its id changes
392 // so we don't have a test expectation for it.
395 TEST_F(RendererAccessibilityTest
, EventOnObjectNotInTree
) {
396 // Test RendererAccessibility and make sure it doesn't send anything
397 // if we get a notification from Blink for an object that isn't in the
398 // tree, like the scroll area that's the parent of the main document,
399 // which we don't expose.
400 std::string html
= "<body><input></body>";
401 LoadHTML(html
.c_str());
403 scoped_ptr
<TestRendererAccessibility
> accessibility(
404 new TestRendererAccessibility(frame()));
405 accessibility
->SendPendingAccessibilityEvents();
406 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
408 WebDocument document
= view()->GetWebView()->mainFrame()->document();
409 WebAXObject root_obj
= document
.accessibilityObject();
410 WebAXObject scroll_area
= root_obj
.parentObject();
411 EXPECT_EQ(blink::WebAXRoleScrollArea
, scroll_area
.role());
413 // Try to fire a message on the scroll area, and assert that we just
415 sink_
->ClearMessages();
416 accessibility
->HandleAXEvent(scroll_area
,
417 ui::AX_EVENT_VALUE_CHANGED
);
419 accessibility
->SendPendingAccessibilityEvents();
421 const IPC::Message
* message
=
422 sink_
->GetUniqueMessageMatching(AccessibilityHostMsg_Events::ID
);
423 ASSERT_TRUE(message
);
424 base::Tuple
<std::vector
<AccessibilityHostMsg_EventParams
>, int> param
;
425 AccessibilityHostMsg_Events::Read(message
, ¶m
);
426 ASSERT_EQ(0U, base::get
<0>(param
).size());
429 } // namespace content