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 "content/common/frame_messages.h"
7 #include "content/common/view_message_enums.h"
8 #include "content/public/test/render_view_test.h"
9 #include "content/renderer/accessibility/renderer_accessibility_complete.h"
10 #include "content/renderer/render_view_impl.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/WebKit/public/platform/WebSize.h"
13 #include "third_party/WebKit/public/web/WebAXObject.h"
14 #include "third_party/WebKit/public/web/WebDocument.h"
15 #include "third_party/WebKit/public/web/WebView.h"
16 #include "ui/accessibility/ax_node_data.h"
18 using blink::WebAXObject
;
19 using blink::WebDocument
;
23 class TestRendererAccessibilityComplete
: public RendererAccessibilityComplete
{
25 explicit TestRendererAccessibilityComplete(RenderViewImpl
* render_view
)
26 : RendererAccessibilityComplete(render_view
) {
29 void SendPendingAccessibilityEvents() {
30 RendererAccessibilityComplete::SendPendingAccessibilityEvents();
34 class RendererAccessibilityTest
: public RenderViewTest
{
36 RendererAccessibilityTest() {}
38 RenderViewImpl
* view() {
39 return static_cast<RenderViewImpl
*>(view_
);
42 RenderFrameImpl
* frame() {
43 return static_cast<RenderFrameImpl
*>(view()->GetMainRenderFrame());
46 virtual void SetUp() {
47 RenderViewTest::SetUp();
48 sink_
= &render_thread_
->sink();
51 void SetMode(AccessibilityMode mode
) {
52 view()->OnSetAccessibilityMode(mode
);
56 AccessibilityHostMsg_EventParams
* params
) {
57 const IPC::Message
* message
=
58 sink_
->GetUniqueMessageMatching(AccessibilityHostMsg_Events::ID
);
60 Tuple1
<std::vector
<AccessibilityHostMsg_EventParams
> > param
;
61 AccessibilityHostMsg_Events::Read(message
, ¶m
);
62 ASSERT_GE(param
.a
.size(), 1U);
66 int CountAccessibilityNodesSentToBrowser() {
67 AccessibilityHostMsg_EventParams event
;
68 GetLastAccEvent(&event
);
69 return event
.update
.nodes
.size();
75 DISALLOW_COPY_AND_ASSIGN(RendererAccessibilityTest
);
79 TEST_F(RendererAccessibilityTest
, EditableTextModeFocusEvents
) {
80 // This is not a test of true web accessibility, it's a test of
81 // a mode used on Windows 8 in Metro mode where an extremely simplified
82 // accessibility tree containing only the current focused node is
84 SetMode(AccessibilityModeEditableTextOnly
);
86 // Set a minimum size and give focus so simulated events work.
87 view()->webwidget()->resize(blink::WebSize(500, 500));
88 view()->webwidget()->setFocus(true);
93 " <textarea></textarea>"
94 " <p contentEditable>Editable</p>"
95 " <div tabindex=0 role=textbox>Textbox</div>"
96 " <button>Button</button>"
100 // Load the test page.
101 LoadHTML(html
.c_str());
103 // We should have sent a message to the browser with the initial focus
106 SCOPED_TRACE("Initial focus on document");
107 AccessibilityHostMsg_EventParams event
;
108 GetLastAccEvent(&event
);
109 EXPECT_EQ(event
.event_type
,
110 ui::AX_EVENT_LAYOUT_COMPLETE
);
111 EXPECT_EQ(event
.id
, 1);
112 EXPECT_EQ(event
.update
.nodes
.size(), 2U);
113 EXPECT_EQ(event
.update
.nodes
[0].id
, 1);
114 EXPECT_EQ(event
.update
.nodes
[0].role
,
115 ui::AX_ROLE_ROOT_WEB_AREA
);
116 EXPECT_EQ(event
.update
.nodes
[0].state
,
117 (1U << ui::AX_STATE_READ_ONLY
) |
118 (1U << ui::AX_STATE_FOCUSABLE
) |
119 (1U << ui::AX_STATE_FOCUSED
));
120 EXPECT_EQ(event
.update
.nodes
[0].child_ids
.size(), 1U);
123 // Now focus the input element, and check everything again.
125 SCOPED_TRACE("input");
126 sink_
->ClearMessages();
127 ExecuteJavaScript("document.querySelector('input').focus();");
128 AccessibilityHostMsg_EventParams event
;
129 GetLastAccEvent(&event
);
130 EXPECT_EQ(event
.event_type
,
132 EXPECT_EQ(event
.id
, 3);
133 EXPECT_EQ(event
.update
.nodes
[0].id
, 1);
134 EXPECT_EQ(event
.update
.nodes
[0].role
,
135 ui::AX_ROLE_ROOT_WEB_AREA
);
136 EXPECT_EQ(event
.update
.nodes
[0].state
,
137 (1U << ui::AX_STATE_READ_ONLY
) |
138 (1U << ui::AX_STATE_FOCUSABLE
));
139 EXPECT_EQ(event
.update
.nodes
[0].child_ids
.size(), 1U);
140 EXPECT_EQ(event
.update
.nodes
[1].id
, 3);
141 EXPECT_EQ(event
.update
.nodes
[1].role
,
143 EXPECT_EQ(event
.update
.nodes
[1].state
,
144 (1U << ui::AX_STATE_FOCUSABLE
) |
145 (1U << ui::AX_STATE_FOCUSED
));
148 // Check other editable text nodes.
150 SCOPED_TRACE("textarea");
151 sink_
->ClearMessages();
152 ExecuteJavaScript("document.querySelector('textarea').focus();");
153 AccessibilityHostMsg_EventParams event
;
154 GetLastAccEvent(&event
);
155 EXPECT_EQ(event
.id
, 4);
156 EXPECT_EQ(event
.update
.nodes
[1].state
,
157 (1U << ui::AX_STATE_FOCUSABLE
) |
158 (1U << ui::AX_STATE_FOCUSED
));
162 SCOPED_TRACE("contentEditable");
163 sink_
->ClearMessages();
164 ExecuteJavaScript("document.querySelector('p').focus();");
165 AccessibilityHostMsg_EventParams event
;
166 GetLastAccEvent(&event
);
167 EXPECT_EQ(event
.id
, 5);
168 EXPECT_EQ(event
.update
.nodes
[1].state
,
169 (1U << ui::AX_STATE_FOCUSABLE
) |
170 (1U << ui::AX_STATE_FOCUSED
));
174 SCOPED_TRACE("role=textarea");
175 sink_
->ClearMessages();
176 ExecuteJavaScript("document.querySelector('div').focus();");
177 AccessibilityHostMsg_EventParams event
;
178 GetLastAccEvent(&event
);
179 EXPECT_EQ(event
.id
, 6);
180 EXPECT_EQ(event
.update
.nodes
[1].state
,
181 (1U << ui::AX_STATE_FOCUSABLE
) |
182 (1U << ui::AX_STATE_FOCUSED
));
185 // Try focusing things that aren't editable text.
187 SCOPED_TRACE("button");
188 sink_
->ClearMessages();
189 ExecuteJavaScript("document.querySelector('button').focus();");
190 AccessibilityHostMsg_EventParams event
;
191 GetLastAccEvent(&event
);
192 EXPECT_EQ(event
.id
, 7);
193 EXPECT_EQ(event
.update
.nodes
[1].state
,
194 (1U << ui::AX_STATE_FOCUSABLE
) |
195 (1U << ui::AX_STATE_FOCUSED
) |
196 (1U << ui::AX_STATE_READ_ONLY
));
200 SCOPED_TRACE("link");
201 sink_
->ClearMessages();
202 ExecuteJavaScript("document.querySelector('a').focus();");
203 AccessibilityHostMsg_EventParams event
;
204 GetLastAccEvent(&event
);
205 EXPECT_EQ(event
.id
, 8);
206 EXPECT_EQ(event
.update
.nodes
[1].state
,
207 (1U << ui::AX_STATE_FOCUSABLE
) |
208 (1U << ui::AX_STATE_FOCUSED
) |
209 (1U << ui::AX_STATE_READ_ONLY
));
214 SCOPED_TRACE("Back to document.");
215 sink_
->ClearMessages();
216 ExecuteJavaScript("document.activeElement.blur()");
217 AccessibilityHostMsg_EventParams event
;
218 GetLastAccEvent(&event
);
219 EXPECT_EQ(event
.id
, 1);
223 TEST_F(RendererAccessibilityTest
, SendFullAccessibilityTreeOnReload
) {
224 // The job of RendererAccessibilityComplete is to serialize the
225 // accessibility tree built by WebKit and send it to the browser.
226 // When the accessibility tree changes, it tries to send only
227 // the nodes that actually changed or were reparented. This test
228 // ensures that the messages sent are correct in cases when a page
229 // reloads, and that internal state is properly garbage-collected.
232 " <div role='group' id='A'>"
233 " <div role='group' id='A1'></div>"
234 " <div role='group' id='A2'></div>"
237 LoadHTML(html
.c_str());
239 // Creating a RendererAccessibilityComplete should sent the tree
241 scoped_ptr
<TestRendererAccessibilityComplete
> accessibility(
242 new TestRendererAccessibilityComplete(view()));
243 accessibility
->SendPendingAccessibilityEvents();
244 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
246 // If we post another event but the tree doesn't change,
247 // we should only send 1 node to the browser.
248 sink_
->ClearMessages();
249 WebDocument document
= view()->GetWebView()->mainFrame()->document();
250 WebAXObject root_obj
= document
.accessibilityObject();
251 accessibility
->HandleAXEvent(
253 ui::AX_EVENT_LAYOUT_COMPLETE
);
254 accessibility
->SendPendingAccessibilityEvents();
255 EXPECT_EQ(1, CountAccessibilityNodesSentToBrowser());
257 // Make sure it's the root object that was updated.
258 AccessibilityHostMsg_EventParams event
;
259 GetLastAccEvent(&event
);
260 EXPECT_EQ(root_obj
.axID(), event
.update
.nodes
[0].id
);
263 // If we reload the page and send a event, we should send
264 // all 4 nodes to the browser. Also double-check that we didn't
265 // leak any of the old BrowserTreeNodes.
266 LoadHTML(html
.c_str());
267 document
= view()->GetWebView()->mainFrame()->document();
268 root_obj
= document
.accessibilityObject();
269 sink_
->ClearMessages();
270 accessibility
->HandleAXEvent(
272 ui::AX_EVENT_LAYOUT_COMPLETE
);
273 accessibility
->SendPendingAccessibilityEvents();
274 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
276 // Even if the first event is sent on an element other than
277 // the root, the whole tree should be updated because we know
278 // the browser doesn't have the root element.
279 LoadHTML(html
.c_str());
280 document
= view()->GetWebView()->mainFrame()->document();
281 root_obj
= document
.accessibilityObject();
282 sink_
->ClearMessages();
283 const WebAXObject
& first_child
= root_obj
.childAt(0);
284 accessibility
->HandleAXEvent(
286 ui::AX_EVENT_LIVE_REGION_CHANGED
);
287 accessibility
->SendPendingAccessibilityEvents();
288 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
291 // http://crbug.com/253537
292 #if defined(OS_ANDROID)
293 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
294 DISABLED_AccessibilityMessagesQueueWhileSwappedOut
296 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
297 AccessibilityMessagesQueueWhileSwappedOut
300 TEST_F(RendererAccessibilityTest
,
301 MAYBE_AccessibilityMessagesQueueWhileSwappedOut
) {
304 " <p>Hello, world.</p>"
306 LoadHTML(html
.c_str());
308 // Creating a RendererAccessibilityComplete should send the tree
310 scoped_ptr
<TestRendererAccessibilityComplete
> accessibility(
311 new TestRendererAccessibilityComplete(view()));
312 accessibility
->SendPendingAccessibilityEvents();
313 EXPECT_EQ(5, CountAccessibilityNodesSentToBrowser());
315 // Post a "value changed" event, but then swap out
316 // before sending it. It shouldn't send the event while
318 sink_
->ClearMessages();
319 WebDocument document
= view()->GetWebView()->mainFrame()->document();
320 WebAXObject root_obj
= document
.accessibilityObject();
321 accessibility
->HandleAXEvent(
323 ui::AX_EVENT_VALUE_CHANGED
);
324 view()->main_render_frame()->OnSwapOut();
325 accessibility
->SendPendingAccessibilityEvents();
326 EXPECT_FALSE(sink_
->GetUniqueMessageMatching(
327 AccessibilityHostMsg_Events::ID
));
329 // Navigate, so we're not swapped out anymore. Now we should
330 // send accessibility events again. Note that the
331 // message that was queued up before will be quickly discarded
332 // because the element it was referring to no longer exists,
333 // so the event here is from loading this new page.
334 FrameMsg_Navigate_Params nav_params
;
335 nav_params
.url
= GURL("data:text/html,<p>Hello, again.</p>");
336 nav_params
.navigation_type
= FrameMsg_Navigate_Type::NORMAL
;
337 nav_params
.transition
= PAGE_TRANSITION_TYPED
;
338 nav_params
.current_history_list_length
= 1;
339 nav_params
.current_history_list_offset
= 0;
340 nav_params
.pending_history_list_offset
= 1;
341 nav_params
.page_id
= -1;
342 frame()->OnNavigate(nav_params
);
343 accessibility
->SendPendingAccessibilityEvents();
344 EXPECT_TRUE(sink_
->GetUniqueMessageMatching(
345 AccessibilityHostMsg_Events::ID
));
348 TEST_F(RendererAccessibilityTest
, HideAccessibilityObject
) {
349 // Test RendererAccessibilityComplete and make sure it sends the
350 // proper event to the browser when an object in the tree
351 // is hidden, but its children are not.
354 " <div role='group' id='A'>"
355 " <div role='group' id='B'>"
356 " <div role='group' id='C' style='visibility:visible'>"
361 LoadHTML(html
.c_str());
363 scoped_ptr
<TestRendererAccessibilityComplete
> accessibility(
364 new TestRendererAccessibilityComplete(view()));
365 accessibility
->SendPendingAccessibilityEvents();
366 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
368 WebDocument document
= view()->GetWebView()->mainFrame()->document();
369 WebAXObject root_obj
= document
.accessibilityObject();
370 WebAXObject node_a
= root_obj
.childAt(0);
371 WebAXObject node_b
= node_a
.childAt(0);
372 WebAXObject node_c
= node_b
.childAt(0);
374 // Hide node 'B' ('C' stays visible).
376 "document.getElementById('B').style.visibility = 'hidden';");
378 ExecuteJavaScript("document.getElementById('B').offsetLeft;");
380 // Send a childrenChanged on 'A'.
381 sink_
->ClearMessages();
382 accessibility
->HandleAXEvent(
384 ui::AX_EVENT_CHILDREN_CHANGED
);
386 accessibility
->SendPendingAccessibilityEvents();
387 AccessibilityHostMsg_EventParams event
;
388 GetLastAccEvent(&event
);
389 ASSERT_EQ(2U, event
.update
.nodes
.size());
391 // RendererAccessibilityComplete notices that 'C' is being reparented,
392 // so it clears the subtree rooted at 'A', then updates 'A' and then 'C'.
393 EXPECT_EQ(node_a
.axID(), event
.update
.node_id_to_clear
);
394 EXPECT_EQ(node_a
.axID(), event
.update
.nodes
[0].id
);
395 EXPECT_EQ(node_c
.axID(), event
.update
.nodes
[1].id
);
396 EXPECT_EQ(2, CountAccessibilityNodesSentToBrowser());
399 TEST_F(RendererAccessibilityTest
, ShowAccessibilityObject
) {
400 // Test RendererAccessibilityComplete and make sure it sends the
401 // proper event to the browser when an object in the tree
402 // is shown, causing its own already-visible children to be
406 " <div role='group' id='A'>"
407 " <div role='group' id='B' style='visibility:hidden'>"
408 " <div role='group' id='C' style='visibility:visible'>"
413 LoadHTML(html
.c_str());
415 scoped_ptr
<TestRendererAccessibilityComplete
> accessibility(
416 new TestRendererAccessibilityComplete(view()));
417 accessibility
->SendPendingAccessibilityEvents();
418 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
420 // Show node 'B', then send a childrenChanged on 'A'.
422 "document.getElementById('B').style.visibility = 'visible';");
423 ExecuteJavaScript("document.getElementById('B').offsetLeft;");
425 sink_
->ClearMessages();
426 WebDocument document
= view()->GetWebView()->mainFrame()->document();
427 WebAXObject root_obj
= document
.accessibilityObject();
428 WebAXObject node_a
= root_obj
.childAt(0);
429 WebAXObject node_b
= node_a
.childAt(0);
430 WebAXObject node_c
= node_b
.childAt(0);
432 accessibility
->HandleAXEvent(
434 ui::AX_EVENT_CHILDREN_CHANGED
);
436 accessibility
->SendPendingAccessibilityEvents();
437 AccessibilityHostMsg_EventParams event
;
438 GetLastAccEvent(&event
);
440 ASSERT_EQ(3U, event
.update
.nodes
.size());
441 EXPECT_EQ(node_a
.axID(), event
.update
.node_id_to_clear
);
442 EXPECT_EQ(node_a
.axID(), event
.update
.nodes
[0].id
);
443 EXPECT_EQ(node_b
.axID(), event
.update
.nodes
[1].id
);
444 EXPECT_EQ(node_c
.axID(), event
.update
.nodes
[2].id
);
445 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
448 TEST_F(RendererAccessibilityTest
, DetachAccessibilityObject
) {
449 // Test RendererAccessibilityComplete and make sure it sends the
450 // proper event to the browser when an object in the tree
451 // is detached, but its children are not. This can happen when
452 // a layout occurs and an anonymous render block is no longer needed.
454 "<body aria-label='Body'>"
455 "<span>1</span><span style='display:block'>2</span>"
457 LoadHTML(html
.c_str());
459 scoped_ptr
<TestRendererAccessibilityComplete
> accessibility(
460 new TestRendererAccessibilityComplete(view()));
461 accessibility
->SendPendingAccessibilityEvents();
462 EXPECT_EQ(7, CountAccessibilityNodesSentToBrowser());
464 // Initially, the accessibility tree looks like this:
468 // +--Anonymous Block
469 // +--Static Text "1"
470 // +--Inline Text Box "1"
471 // +--Static Text "2"
472 // +--Inline Text Box "2"
473 WebDocument document
= view()->GetWebView()->mainFrame()->document();
474 WebAXObject root_obj
= document
.accessibilityObject();
475 WebAXObject body
= root_obj
.childAt(0);
476 WebAXObject anonymous_block
= body
.childAt(0);
477 WebAXObject text_1
= anonymous_block
.childAt(0);
478 WebAXObject text_2
= body
.childAt(1);
480 // Change the display of the second 'span' back to inline, which causes the
481 // anonymous block to be destroyed.
483 "document.querySelectorAll('span')[1].style.display = 'inline';");
485 ExecuteJavaScript("document.body.offsetLeft;");
487 // Send a childrenChanged on the body.
488 sink_
->ClearMessages();
489 accessibility
->HandleAXEvent(
491 ui::AX_EVENT_CHILDREN_CHANGED
);
493 accessibility
->SendPendingAccessibilityEvents();
495 // Afterwards, the accessibility tree looks like this:
499 // +--Static Text "1"
500 // +--Inline Text Box "1"
501 // +--Static Text "2"
502 // +--Inline Text Box "2"
504 // We just assert that there are now four nodes in the
505 // accessibility tree and that only three nodes needed
506 // to be updated (the body, the static text 1, and
507 // the static text 2).
509 AccessibilityHostMsg_EventParams event
;
510 GetLastAccEvent(&event
);
511 ASSERT_EQ(5U, event
.update
.nodes
.size());
513 EXPECT_EQ(body
.axID(), event
.update
.nodes
[0].id
);
514 EXPECT_EQ(text_1
.axID(), event
.update
.nodes
[1].id
);
515 // The third event is to update text_2, but its id changes
516 // so we don't have a test expectation for it.
519 } // namespace content