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/test/render_view_test.h"
10 #include "content/renderer/accessibility/renderer_accessibility.h"
11 #include "content/renderer/render_frame_impl.h"
12 #include "content/renderer/render_view_impl.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/public/platform/WebSize.h"
15 #include "third_party/WebKit/public/web/WebAXObject.h"
16 #include "third_party/WebKit/public/web/WebDocument.h"
17 #include "third_party/WebKit/public/web/WebView.h"
18 #include "ui/accessibility/ax_node_data.h"
20 using blink::WebAXObject
;
21 using blink::WebDocument
;
25 class TestRendererAccessibility
: public RendererAccessibility
{
27 explicit TestRendererAccessibility(RenderFrameImpl
* render_frame
)
28 : RendererAccessibility(render_frame
) {
31 void SendPendingAccessibilityEvents() {
32 RendererAccessibility::SendPendingAccessibilityEvents();
36 class RendererAccessibilityTest
: public RenderViewTest
{
38 RendererAccessibilityTest() {}
40 RenderViewImpl
* view() {
41 return static_cast<RenderViewImpl
*>(view_
);
44 RenderFrameImpl
* frame() {
45 return static_cast<RenderFrameImpl
*>(view()->GetMainRenderFrame());
48 void SetUp() override
{
49 RenderViewTest::SetUp();
50 sink_
= &render_thread_
->sink();
53 void SetMode(AccessibilityMode mode
) {
54 frame()->OnSetAccessibilityMode(mode
);
58 AccessibilityHostMsg_EventParams
* params
) {
59 const IPC::Message
* message
=
60 sink_
->GetUniqueMessageMatching(AccessibilityHostMsg_Events::ID
);
62 Tuple
<std::vector
<AccessibilityHostMsg_EventParams
>, int> param
;
63 AccessibilityHostMsg_Events::Read(message
, ¶m
);
64 ASSERT_GE(get
<0>(param
).size(), 1U);
65 *params
= get
<0>(param
)[0];
68 int CountAccessibilityNodesSentToBrowser() {
69 AccessibilityHostMsg_EventParams event
;
70 GetLastAccEvent(&event
);
71 return event
.update
.nodes
.size();
77 DISALLOW_COPY_AND_ASSIGN(RendererAccessibilityTest
);
81 TEST_F(RendererAccessibilityTest
, SendFullAccessibilityTreeOnReload
) {
82 // The job of RendererAccessibility is to serialize the
83 // accessibility tree built by WebKit and send it to the browser.
84 // When the accessibility tree changes, it tries to send only
85 // the nodes that actually changed or were reparented. This test
86 // ensures that the messages sent are correct in cases when a page
87 // reloads, and that internal state is properly garbage-collected.
90 " <div role='group' id='A'>"
91 " <div role='group' id='A1'></div>"
92 " <div role='group' id='A2'></div>"
95 LoadHTML(html
.c_str());
97 // Creating a RendererAccessibility should sent the tree to the browser.
98 scoped_ptr
<TestRendererAccessibility
> accessibility(
99 new TestRendererAccessibility(frame()));
100 accessibility
->SendPendingAccessibilityEvents();
101 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
103 // If we post another event but the tree doesn't change,
104 // we should only send 1 node to the browser.
105 sink_
->ClearMessages();
106 WebDocument document
= view()->GetWebView()->mainFrame()->document();
107 WebAXObject root_obj
= document
.accessibilityObject();
108 accessibility
->HandleAXEvent(
110 ui::AX_EVENT_LAYOUT_COMPLETE
);
111 accessibility
->SendPendingAccessibilityEvents();
112 EXPECT_EQ(1, CountAccessibilityNodesSentToBrowser());
114 // Make sure it's the root object that was updated.
115 AccessibilityHostMsg_EventParams event
;
116 GetLastAccEvent(&event
);
117 EXPECT_EQ(root_obj
.axID(), event
.update
.nodes
[0].id
);
120 // If we reload the page and send a event, we should send
121 // all 4 nodes to the browser. Also double-check that we didn't
122 // leak any of the old BrowserTreeNodes.
123 LoadHTML(html
.c_str());
124 document
= view()->GetWebView()->mainFrame()->document();
125 root_obj
= document
.accessibilityObject();
126 sink_
->ClearMessages();
127 accessibility
->HandleAXEvent(
129 ui::AX_EVENT_LAYOUT_COMPLETE
);
130 accessibility
->SendPendingAccessibilityEvents();
131 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
133 // Even if the first event is sent on an element other than
134 // the root, the whole tree should be updated because we know
135 // the browser doesn't have the root element.
136 LoadHTML(html
.c_str());
137 document
= view()->GetWebView()->mainFrame()->document();
138 root_obj
= document
.accessibilityObject();
139 sink_
->ClearMessages();
140 const WebAXObject
& first_child
= root_obj
.childAt(0);
141 accessibility
->HandleAXEvent(
143 ui::AX_EVENT_LIVE_REGION_CHANGED
);
144 accessibility
->SendPendingAccessibilityEvents();
145 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
148 // http://crbug.com/253537
149 #if defined(OS_ANDROID)
150 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
151 DISABLED_AccessibilityMessagesQueueWhileSwappedOut
153 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
154 AccessibilityMessagesQueueWhileSwappedOut
157 TEST_F(RendererAccessibilityTest
,
158 MAYBE_AccessibilityMessagesQueueWhileSwappedOut
) {
161 " <p>Hello, world.</p>"
163 LoadHTML(html
.c_str());
164 static const int kProxyRoutingId
= 13;
166 // Creating a RendererAccessibility should send the tree to the browser.
167 scoped_ptr
<TestRendererAccessibility
> accessibility(
168 new TestRendererAccessibility(frame()));
169 accessibility
->SendPendingAccessibilityEvents();
170 EXPECT_EQ(5, CountAccessibilityNodesSentToBrowser());
172 // Post a "value changed" event, but then swap out
173 // before sending it. It shouldn't send the event while
175 sink_
->ClearMessages();
176 WebDocument document
= view()->GetWebView()->mainFrame()->document();
177 WebAXObject root_obj
= document
.accessibilityObject();
178 accessibility
->HandleAXEvent(
180 ui::AX_EVENT_VALUE_CHANGED
);
181 view()->GetMainRenderFrame()->OnSwapOut(kProxyRoutingId
, true,
182 content::FrameReplicationState());
183 accessibility
->SendPendingAccessibilityEvents();
184 EXPECT_FALSE(sink_
->GetUniqueMessageMatching(
185 AccessibilityHostMsg_Events::ID
));
187 // Navigate, so we're not swapped out anymore. Now we should
188 // send accessibility events again. Note that the
189 // message that was queued up before will be quickly discarded
190 // because the element it was referring to no longer exists,
191 // so the event here is from loading this new page.
192 FrameMsg_Navigate_Params nav_params
;
193 nav_params
.common_params
.url
= GURL("data:text/html,<p>Hello, again.</p>");
194 nav_params
.common_params
.navigation_type
= FrameMsg_Navigate_Type::NORMAL
;
195 nav_params
.common_params
.transition
= ui::PAGE_TRANSITION_TYPED
;
196 nav_params
.current_history_list_length
= 1;
197 nav_params
.current_history_list_offset
= 0;
198 nav_params
.pending_history_list_offset
= 1;
199 nav_params
.page_id
= -1;
200 nav_params
.commit_params
.browser_navigation_start
=
201 base::TimeTicks::FromInternalValue(1);
202 frame()->OnNavigate(nav_params
);
203 accessibility
->SendPendingAccessibilityEvents();
204 EXPECT_TRUE(sink_
->GetUniqueMessageMatching(
205 AccessibilityHostMsg_Events::ID
));
208 TEST_F(RendererAccessibilityTest
, HideAccessibilityObject
) {
209 // Test RendererAccessibility and make sure it sends the
210 // proper event to the browser when an object in the tree
211 // is hidden, but its children are not.
214 " <div role='group' id='A'>"
215 " <div role='group' id='B'>"
216 " <div role='group' id='C' style='visibility:visible'>"
221 LoadHTML(html
.c_str());
223 scoped_ptr
<TestRendererAccessibility
> accessibility(
224 new TestRendererAccessibility(frame()));
225 accessibility
->SendPendingAccessibilityEvents();
226 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
228 WebDocument document
= view()->GetWebView()->mainFrame()->document();
229 WebAXObject root_obj
= document
.accessibilityObject();
230 WebAXObject node_a
= root_obj
.childAt(0);
231 WebAXObject node_b
= node_a
.childAt(0);
232 WebAXObject node_c
= node_b
.childAt(0);
234 // Hide node 'B' ('C' stays visible).
236 "document.getElementById('B').style.visibility = 'hidden';");
238 ExecuteJavaScript("document.getElementById('B').offsetLeft;");
240 // Send a childrenChanged on 'A'.
241 sink_
->ClearMessages();
242 accessibility
->HandleAXEvent(
244 ui::AX_EVENT_CHILDREN_CHANGED
);
246 accessibility
->SendPendingAccessibilityEvents();
247 AccessibilityHostMsg_EventParams event
;
248 GetLastAccEvent(&event
);
249 ASSERT_EQ(2U, event
.update
.nodes
.size());
251 // RendererAccessibility notices that 'C' is being reparented,
252 // so it clears the subtree rooted at 'A', then updates 'A' and then 'C'.
253 EXPECT_EQ(node_a
.axID(), event
.update
.node_id_to_clear
);
254 EXPECT_EQ(node_a
.axID(), event
.update
.nodes
[0].id
);
255 EXPECT_EQ(node_c
.axID(), event
.update
.nodes
[1].id
);
256 EXPECT_EQ(2, CountAccessibilityNodesSentToBrowser());
259 TEST_F(RendererAccessibilityTest
, ShowAccessibilityObject
) {
260 // Test RendererAccessibility and make sure it sends the
261 // proper event to the browser when an object in the tree
262 // is shown, causing its own already-visible children to be
266 " <div role='group' id='A'>"
267 " <div role='group' id='B' style='visibility:hidden'>"
268 " <div role='group' id='C' style='visibility:visible'>"
273 LoadHTML(html
.c_str());
275 scoped_ptr
<TestRendererAccessibility
> accessibility(
276 new TestRendererAccessibility(frame()));
277 accessibility
->SendPendingAccessibilityEvents();
278 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
280 // Show node 'B', then send a childrenChanged on 'A'.
282 "document.getElementById('B').style.visibility = 'visible';");
283 ExecuteJavaScript("document.getElementById('B').offsetLeft;");
285 sink_
->ClearMessages();
286 WebDocument document
= view()->GetWebView()->mainFrame()->document();
287 WebAXObject root_obj
= document
.accessibilityObject();
288 WebAXObject node_a
= root_obj
.childAt(0);
289 WebAXObject node_b
= node_a
.childAt(0);
290 WebAXObject node_c
= node_b
.childAt(0);
292 accessibility
->HandleAXEvent(
294 ui::AX_EVENT_CHILDREN_CHANGED
);
296 accessibility
->SendPendingAccessibilityEvents();
297 AccessibilityHostMsg_EventParams event
;
298 GetLastAccEvent(&event
);
300 ASSERT_EQ(3U, event
.update
.nodes
.size());
301 EXPECT_EQ(node_a
.axID(), event
.update
.node_id_to_clear
);
302 EXPECT_EQ(node_a
.axID(), event
.update
.nodes
[0].id
);
303 EXPECT_EQ(node_b
.axID(), event
.update
.nodes
[1].id
);
304 EXPECT_EQ(node_c
.axID(), event
.update
.nodes
[2].id
);
305 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
308 TEST_F(RendererAccessibilityTest
, DetachAccessibilityObject
) {
309 // Test RendererAccessibility and make sure it sends the
310 // proper event to the browser when an object in the tree
311 // is detached, but its children are not. This can happen when
312 // a layout occurs and an anonymous render block is no longer needed.
314 "<body aria-label='Body'>"
315 "<span>1</span><span style='display:block'>2</span>"
317 LoadHTML(html
.c_str());
319 scoped_ptr
<TestRendererAccessibility
> accessibility(
320 new TestRendererAccessibility(frame()));
321 accessibility
->SendPendingAccessibilityEvents();
322 EXPECT_EQ(7, CountAccessibilityNodesSentToBrowser());
324 // Initially, the accessibility tree looks like this:
328 // +--Anonymous Block
329 // +--Static Text "1"
330 // +--Inline Text Box "1"
331 // +--Static Text "2"
332 // +--Inline Text Box "2"
333 WebDocument document
= view()->GetWebView()->mainFrame()->document();
334 WebAXObject root_obj
= document
.accessibilityObject();
335 WebAXObject body
= root_obj
.childAt(0);
336 WebAXObject anonymous_block
= body
.childAt(0);
337 WebAXObject text_1
= anonymous_block
.childAt(0);
338 WebAXObject text_2
= body
.childAt(1);
340 // Change the display of the second 'span' back to inline, which causes the
341 // anonymous block to be destroyed.
343 "document.querySelectorAll('span')[1].style.display = 'inline';");
345 ExecuteJavaScript("document.body.offsetLeft;");
347 // Send a childrenChanged on the body.
348 sink_
->ClearMessages();
349 accessibility
->HandleAXEvent(
351 ui::AX_EVENT_CHILDREN_CHANGED
);
353 accessibility
->SendPendingAccessibilityEvents();
355 // Afterwards, the accessibility tree looks like this:
359 // +--Static Text "1"
360 // +--Inline Text Box "1"
361 // +--Static Text "2"
362 // +--Inline Text Box "2"
364 // We just assert that there are now four nodes in the
365 // accessibility tree and that only three nodes needed
366 // to be updated (the body, the static text 1, and
367 // the static text 2).
369 AccessibilityHostMsg_EventParams event
;
370 GetLastAccEvent(&event
);
371 ASSERT_EQ(5U, event
.update
.nodes
.size());
373 EXPECT_EQ(body
.axID(), event
.update
.nodes
[0].id
);
374 EXPECT_EQ(text_1
.axID(), event
.update
.nodes
[1].id
);
375 // The third event is to update text_2, but its id changes
376 // so we don't have a test expectation for it.
379 TEST_F(RendererAccessibilityTest
, EventOnObjectNotInTree
) {
380 // Test RendererAccessibility and make sure it doesn't send anything
381 // if we get a notification from Blink for an object that isn't in the
382 // tree, like the scroll area that's the parent of the main document,
383 // which we don't expose.
384 std::string html
= "<body><input></body>";
385 LoadHTML(html
.c_str());
387 scoped_ptr
<TestRendererAccessibility
> accessibility(
388 new TestRendererAccessibility(frame()));
389 accessibility
->SendPendingAccessibilityEvents();
390 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
392 WebDocument document
= view()->GetWebView()->mainFrame()->document();
393 WebAXObject root_obj
= document
.accessibilityObject();
394 WebAXObject scroll_area
= root_obj
.parentObject();
395 EXPECT_EQ(blink::WebAXRoleScrollArea
, scroll_area
.role());
397 // Try to fire a message on the scroll area, and assert that we just
399 sink_
->ClearMessages();
400 accessibility
->HandleAXEvent(scroll_area
,
401 ui::AX_EVENT_VALUE_CHANGED
);
403 accessibility
->SendPendingAccessibilityEvents();
405 const IPC::Message
* message
=
406 sink_
->GetUniqueMessageMatching(AccessibilityHostMsg_Events::ID
);
407 ASSERT_TRUE(message
);
408 Tuple
<std::vector
<AccessibilityHostMsg_EventParams
>, int> param
;
409 AccessibilityHostMsg_Events::Read(message
, ¶m
);
410 ASSERT_EQ(0U, get
<0>(param
).size());
413 } // namespace content