Reset accessibility if it gets out of sync.
[chromium-blink-merge.git] / content / renderer / accessibility / renderer_accessibility_browsertest.cc
blob18c9a59ee6fac9d7a764030da086f123c8126f6b
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_complete.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;
23 namespace content {
25 class TestRendererAccessibilityComplete : public RendererAccessibilityComplete {
26 public:
27 explicit TestRendererAccessibilityComplete(RenderFrameImpl* render_frame)
28 : RendererAccessibilityComplete(render_frame) {
31 void SendPendingAccessibilityEvents() {
32 RendererAccessibilityComplete::SendPendingAccessibilityEvents();
36 class RendererAccessibilityTest : public RenderViewTest {
37 public:
38 RendererAccessibilityTest() {}
40 RenderViewImpl* view() {
41 return static_cast<RenderViewImpl*>(view_);
44 RenderFrameImpl* frame() {
45 return static_cast<RenderFrameImpl*>(view()->GetMainRenderFrame());
48 virtual void SetUp() {
49 RenderViewTest::SetUp();
50 sink_ = &render_thread_->sink();
53 void SetMode(AccessibilityMode mode) {
54 frame()->OnSetAccessibilityMode(mode);
57 void GetLastAccEvent(
58 AccessibilityHostMsg_EventParams* params) {
59 const IPC::Message* message =
60 sink_->GetUniqueMessageMatching(AccessibilityHostMsg_Events::ID);
61 ASSERT_TRUE(message);
62 Tuple2<std::vector<AccessibilityHostMsg_EventParams>, int> param;
63 AccessibilityHostMsg_Events::Read(message, &param);
64 ASSERT_GE(param.a.size(), 1U);
65 *params = param.a[0];
68 int CountAccessibilityNodesSentToBrowser() {
69 AccessibilityHostMsg_EventParams event;
70 GetLastAccEvent(&event);
71 return event.update.nodes.size();
74 protected:
75 IPC::TestSink* sink_;
77 DISALLOW_COPY_AND_ASSIGN(RendererAccessibilityTest);
81 TEST_F(RendererAccessibilityTest, SendFullAccessibilityTreeOnReload) {
82 // The job of RendererAccessibilityComplete 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.
88 std::string html =
89 "<body>"
90 " <div role='group' id='A'>"
91 " <div role='group' id='A1'></div>"
92 " <div role='group' id='A2'></div>"
93 " </div>"
94 "</body>";
95 LoadHTML(html.c_str());
97 // Creating a RendererAccessibilityComplete should sent the tree
98 // to the browser.
99 scoped_ptr<TestRendererAccessibilityComplete> accessibility(
100 new TestRendererAccessibilityComplete(frame()));
101 accessibility->SendPendingAccessibilityEvents();
102 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
104 // If we post another event but the tree doesn't change,
105 // we should only send 1 node to the browser.
106 sink_->ClearMessages();
107 WebDocument document = view()->GetWebView()->mainFrame()->document();
108 WebAXObject root_obj = document.accessibilityObject();
109 accessibility->HandleAXEvent(
110 root_obj,
111 ui::AX_EVENT_LAYOUT_COMPLETE);
112 accessibility->SendPendingAccessibilityEvents();
113 EXPECT_EQ(1, CountAccessibilityNodesSentToBrowser());
115 // Make sure it's the root object that was updated.
116 AccessibilityHostMsg_EventParams event;
117 GetLastAccEvent(&event);
118 EXPECT_EQ(root_obj.axID(), event.update.nodes[0].id);
121 // If we reload the page and send a event, we should send
122 // all 4 nodes to the browser. Also double-check that we didn't
123 // leak any of the old BrowserTreeNodes.
124 LoadHTML(html.c_str());
125 document = view()->GetWebView()->mainFrame()->document();
126 root_obj = document.accessibilityObject();
127 sink_->ClearMessages();
128 accessibility->HandleAXEvent(
129 root_obj,
130 ui::AX_EVENT_LAYOUT_COMPLETE);
131 accessibility->SendPendingAccessibilityEvents();
132 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
134 // Even if the first event is sent on an element other than
135 // the root, the whole tree should be updated because we know
136 // the browser doesn't have the root element.
137 LoadHTML(html.c_str());
138 document = view()->GetWebView()->mainFrame()->document();
139 root_obj = document.accessibilityObject();
140 sink_->ClearMessages();
141 const WebAXObject& first_child = root_obj.childAt(0);
142 accessibility->HandleAXEvent(
143 first_child,
144 ui::AX_EVENT_LIVE_REGION_CHANGED);
145 accessibility->SendPendingAccessibilityEvents();
146 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
149 // http://crbug.com/253537
150 #if defined(OS_ANDROID)
151 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
152 DISABLED_AccessibilityMessagesQueueWhileSwappedOut
153 #else
154 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
155 AccessibilityMessagesQueueWhileSwappedOut
156 #endif
158 TEST_F(RendererAccessibilityTest,
159 MAYBE_AccessibilityMessagesQueueWhileSwappedOut) {
160 std::string html =
161 "<body>"
162 " <p>Hello, world.</p>"
163 "</body>";
164 LoadHTML(html.c_str());
165 static const int kProxyRoutingId = 13;
167 // Creating a RendererAccessibilityComplete should send the tree
168 // to the browser.
169 scoped_ptr<TestRendererAccessibilityComplete> accessibility(
170 new TestRendererAccessibilityComplete(frame()));
171 accessibility->SendPendingAccessibilityEvents();
172 EXPECT_EQ(5, CountAccessibilityNodesSentToBrowser());
174 // Post a "value changed" event, but then swap out
175 // before sending it. It shouldn't send the event while
176 // swapped out.
177 sink_->ClearMessages();
178 WebDocument document = view()->GetWebView()->mainFrame()->document();
179 WebAXObject root_obj = document.accessibilityObject();
180 accessibility->HandleAXEvent(
181 root_obj,
182 ui::AX_EVENT_VALUE_CHANGED);
183 view()->GetMainRenderFrame()->OnSwapOut(kProxyRoutingId);
184 accessibility->SendPendingAccessibilityEvents();
185 EXPECT_FALSE(sink_->GetUniqueMessageMatching(
186 AccessibilityHostMsg_Events::ID));
188 // Navigate, so we're not swapped out anymore. Now we should
189 // send accessibility events again. Note that the
190 // message that was queued up before will be quickly discarded
191 // because the element it was referring to no longer exists,
192 // so the event here is from loading this new page.
193 FrameMsg_Navigate_Params nav_params;
194 nav_params.common_params.url = GURL("data:text/html,<p>Hello, again.</p>");
195 nav_params.common_params.navigation_type = FrameMsg_Navigate_Type::NORMAL;
196 nav_params.common_params.transition = ui::PAGE_TRANSITION_TYPED;
197 nav_params.current_history_list_length = 1;
198 nav_params.current_history_list_offset = 0;
199 nav_params.pending_history_list_offset = 1;
200 nav_params.page_id = -1;
201 nav_params.commit_params.browser_navigation_start =
202 base::TimeTicks::FromInternalValue(1);
203 frame()->OnNavigate(nav_params);
204 accessibility->SendPendingAccessibilityEvents();
205 EXPECT_TRUE(sink_->GetUniqueMessageMatching(
206 AccessibilityHostMsg_Events::ID));
209 TEST_F(RendererAccessibilityTest, HideAccessibilityObject) {
210 // Test RendererAccessibilityComplete and make sure it sends the
211 // proper event to the browser when an object in the tree
212 // is hidden, but its children are not.
213 std::string html =
214 "<body>"
215 " <div role='group' id='A'>"
216 " <div role='group' id='B'>"
217 " <div role='group' id='C' style='visibility:visible'>"
218 " </div>"
219 " </div>"
220 " </div>"
221 "</body>";
222 LoadHTML(html.c_str());
224 scoped_ptr<TestRendererAccessibilityComplete> accessibility(
225 new TestRendererAccessibilityComplete(frame()));
226 accessibility->SendPendingAccessibilityEvents();
227 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
229 WebDocument document = view()->GetWebView()->mainFrame()->document();
230 WebAXObject root_obj = document.accessibilityObject();
231 WebAXObject node_a = root_obj.childAt(0);
232 WebAXObject node_b = node_a.childAt(0);
233 WebAXObject node_c = node_b.childAt(0);
235 // Hide node 'B' ('C' stays visible).
236 ExecuteJavaScript(
237 "document.getElementById('B').style.visibility = 'hidden';");
238 // Force layout now.
239 ExecuteJavaScript("document.getElementById('B').offsetLeft;");
241 // Send a childrenChanged on 'A'.
242 sink_->ClearMessages();
243 accessibility->HandleAXEvent(
244 node_a,
245 ui::AX_EVENT_CHILDREN_CHANGED);
247 accessibility->SendPendingAccessibilityEvents();
248 AccessibilityHostMsg_EventParams event;
249 GetLastAccEvent(&event);
250 ASSERT_EQ(2U, event.update.nodes.size());
252 // RendererAccessibilityComplete notices that 'C' is being reparented,
253 // so it clears the subtree rooted at 'A', then updates 'A' and then 'C'.
254 EXPECT_EQ(node_a.axID(), event.update.node_id_to_clear);
255 EXPECT_EQ(node_a.axID(), event.update.nodes[0].id);
256 EXPECT_EQ(node_c.axID(), event.update.nodes[1].id);
257 EXPECT_EQ(2, CountAccessibilityNodesSentToBrowser());
260 TEST_F(RendererAccessibilityTest, ShowAccessibilityObject) {
261 // Test RendererAccessibilityComplete and make sure it sends the
262 // proper event to the browser when an object in the tree
263 // is shown, causing its own already-visible children to be
264 // reparented to it.
265 std::string html =
266 "<body>"
267 " <div role='group' id='A'>"
268 " <div role='group' id='B' style='visibility:hidden'>"
269 " <div role='group' id='C' style='visibility:visible'>"
270 " </div>"
271 " </div>"
272 " </div>"
273 "</body>";
274 LoadHTML(html.c_str());
276 scoped_ptr<TestRendererAccessibilityComplete> accessibility(
277 new TestRendererAccessibilityComplete(frame()));
278 accessibility->SendPendingAccessibilityEvents();
279 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
281 // Show node 'B', then send a childrenChanged on 'A'.
282 ExecuteJavaScript(
283 "document.getElementById('B').style.visibility = 'visible';");
284 ExecuteJavaScript("document.getElementById('B').offsetLeft;");
286 sink_->ClearMessages();
287 WebDocument document = view()->GetWebView()->mainFrame()->document();
288 WebAXObject root_obj = document.accessibilityObject();
289 WebAXObject node_a = root_obj.childAt(0);
290 WebAXObject node_b = node_a.childAt(0);
291 WebAXObject node_c = node_b.childAt(0);
293 accessibility->HandleAXEvent(
294 node_a,
295 ui::AX_EVENT_CHILDREN_CHANGED);
297 accessibility->SendPendingAccessibilityEvents();
298 AccessibilityHostMsg_EventParams event;
299 GetLastAccEvent(&event);
301 ASSERT_EQ(3U, event.update.nodes.size());
302 EXPECT_EQ(node_a.axID(), event.update.node_id_to_clear);
303 EXPECT_EQ(node_a.axID(), event.update.nodes[0].id);
304 EXPECT_EQ(node_b.axID(), event.update.nodes[1].id);
305 EXPECT_EQ(node_c.axID(), event.update.nodes[2].id);
306 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
309 TEST_F(RendererAccessibilityTest, DetachAccessibilityObject) {
310 // Test RendererAccessibilityComplete and make sure it sends the
311 // proper event to the browser when an object in the tree
312 // is detached, but its children are not. This can happen when
313 // a layout occurs and an anonymous render block is no longer needed.
314 std::string html =
315 "<body aria-label='Body'>"
316 "<span>1</span><span style='display:block'>2</span>"
317 "</body>";
318 LoadHTML(html.c_str());
320 scoped_ptr<TestRendererAccessibilityComplete> accessibility(
321 new TestRendererAccessibilityComplete(frame()));
322 accessibility->SendPendingAccessibilityEvents();
323 EXPECT_EQ(7, CountAccessibilityNodesSentToBrowser());
325 // Initially, the accessibility tree looks like this:
327 // Document
328 // +--Body
329 // +--Anonymous Block
330 // +--Static Text "1"
331 // +--Inline Text Box "1"
332 // +--Static Text "2"
333 // +--Inline Text Box "2"
334 WebDocument document = view()->GetWebView()->mainFrame()->document();
335 WebAXObject root_obj = document.accessibilityObject();
336 WebAXObject body = root_obj.childAt(0);
337 WebAXObject anonymous_block = body.childAt(0);
338 WebAXObject text_1 = anonymous_block.childAt(0);
339 WebAXObject text_2 = body.childAt(1);
341 // Change the display of the second 'span' back to inline, which causes the
342 // anonymous block to be destroyed.
343 ExecuteJavaScript(
344 "document.querySelectorAll('span')[1].style.display = 'inline';");
345 // Force layout now.
346 ExecuteJavaScript("document.body.offsetLeft;");
348 // Send a childrenChanged on the body.
349 sink_->ClearMessages();
350 accessibility->HandleAXEvent(
351 body,
352 ui::AX_EVENT_CHILDREN_CHANGED);
354 accessibility->SendPendingAccessibilityEvents();
356 // Afterwards, the accessibility tree looks like this:
358 // Document
359 // +--Body
360 // +--Static Text "1"
361 // +--Inline Text Box "1"
362 // +--Static Text "2"
363 // +--Inline Text Box "2"
365 // We just assert that there are now four nodes in the
366 // accessibility tree and that only three nodes needed
367 // to be updated (the body, the static text 1, and
368 // the static text 2).
370 AccessibilityHostMsg_EventParams event;
371 GetLastAccEvent(&event);
372 ASSERT_EQ(5U, event.update.nodes.size());
374 EXPECT_EQ(body.axID(), event.update.nodes[0].id);
375 EXPECT_EQ(text_1.axID(), event.update.nodes[1].id);
376 // The third event is to update text_2, but its id changes
377 // so we don't have a test expectation for it.
380 TEST_F(RendererAccessibilityTest, EventOnObjectNotInTree) {
381 // Test RendererAccessibilityComplete and make sure it doesn't send anything
382 // if we get a notification from Blink for an object that isn't in the
383 // tree, like the scroll area that's the parent of the main document,
384 // which we don't expose.
385 std::string html = "<body><input></body>";
386 LoadHTML(html.c_str());
388 scoped_ptr<TestRendererAccessibilityComplete> accessibility(
389 new TestRendererAccessibilityComplete(frame()));
390 accessibility->SendPendingAccessibilityEvents();
391 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
393 WebDocument document = view()->GetWebView()->mainFrame()->document();
394 WebAXObject root_obj = document.accessibilityObject();
395 WebAXObject scroll_area = root_obj.parentObject();
396 EXPECT_EQ(blink::WebAXRoleScrollArea, scroll_area.role());
398 // Try to fire a message on the scroll area, and assert that we just
399 // ignore it.
400 sink_->ClearMessages();
401 accessibility->HandleAXEvent(scroll_area,
402 ui::AX_EVENT_VALUE_CHANGED);
404 accessibility->SendPendingAccessibilityEvents();
406 const IPC::Message* message =
407 sink_->GetUniqueMessageMatching(AccessibilityHostMsg_Events::ID);
408 ASSERT_TRUE(message);
409 Tuple2<std::vector<AccessibilityHostMsg_EventParams>, int> param;
410 AccessibilityHostMsg_Events::Read(message, &param);
411 ASSERT_EQ(0U, param.a.size());
414 } // namespace content