Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / content / renderer / accessibility / renderer_accessibility_browsertest.cc
blobc20ee53636cb4b83e44927596976a8019421d063
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/accessibility_messages.h"
8 #include "content/common/frame_messages.h"
9 #include "content/common/site_isolation_policy.h"
10 #include "content/common/view_message_enums.h"
11 #include "content/public/common/content_switches.h"
12 #include "content/public/test/render_view_test.h"
13 #include "content/renderer/accessibility/renderer_accessibility.h"
14 #include "content/renderer/render_frame_impl.h"
15 #include "content/renderer/render_view_impl.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/WebKit/public/platform/WebSize.h"
18 #include "third_party/WebKit/public/web/WebAXObject.h"
19 #include "third_party/WebKit/public/web/WebDocument.h"
20 #include "third_party/WebKit/public/web/WebView.h"
21 #include "ui/accessibility/ax_node_data.h"
23 using blink::WebAXObject;
24 using blink::WebDocument;
26 namespace content {
28 class TestRendererAccessibility : public RendererAccessibility {
29 public:
30 explicit TestRendererAccessibility(RenderFrameImpl* render_frame)
31 : RendererAccessibility(render_frame) {
34 void SendPendingAccessibilityEvents() {
35 RendererAccessibility::SendPendingAccessibilityEvents();
39 class RendererAccessibilityTest : public RenderViewTest {
40 public:
41 RendererAccessibilityTest() {}
43 RenderViewImpl* view() {
44 return static_cast<RenderViewImpl*>(view_);
47 RenderFrameImpl* frame() {
48 return static_cast<RenderFrameImpl*>(view()->GetMainRenderFrame());
51 void SetUp() override {
52 RenderViewTest::SetUp();
53 sink_ = &render_thread_->sink();
56 void TearDown() override {
57 #if defined(LEAK_SANITIZER)
58 // Do this before shutting down V8 in RenderViewTest::TearDown().
59 // http://crbug.com/328552
60 __lsan_do_leak_check();
61 #endif
62 RenderViewTest::TearDown();
65 void SetMode(AccessibilityMode mode) {
66 frame()->OnSetAccessibilityMode(mode);
69 void GetAllAccEvents(
70 std::vector<AccessibilityHostMsg_EventParams>* param_list) {
71 const IPC::Message* message =
72 sink_->GetUniqueMessageMatching(AccessibilityHostMsg_Events::ID);
73 ASSERT_TRUE(message);
74 base::Tuple<std::vector<AccessibilityHostMsg_EventParams>, int> param;
75 AccessibilityHostMsg_Events::Read(message, &param);
76 *param_list = base::get<0>(param);
79 void GetLastAccEvent(
80 AccessibilityHostMsg_EventParams* params) {
81 std::vector<AccessibilityHostMsg_EventParams> param_list;
82 GetAllAccEvents(&param_list);
83 ASSERT_GE(param_list.size(), 1U);
84 *params = param_list[0];
87 int CountAccessibilityNodesSentToBrowser() {
88 AccessibilityHostMsg_EventParams event;
89 GetLastAccEvent(&event);
90 return event.update.nodes.size();
93 protected:
94 IPC::TestSink* sink_;
96 DISALLOW_COPY_AND_ASSIGN(RendererAccessibilityTest);
100 TEST_F(RendererAccessibilityTest, SendFullAccessibilityTreeOnReload) {
101 // The job of RendererAccessibility is to serialize the
102 // accessibility tree built by WebKit and send it to the browser.
103 // When the accessibility tree changes, it tries to send only
104 // the nodes that actually changed or were reparented. This test
105 // ensures that the messages sent are correct in cases when a page
106 // reloads, and that internal state is properly garbage-collected.
107 std::string html =
108 "<body>"
109 " <div role='group' id='A'>"
110 " <div role='group' id='A1'></div>"
111 " <div role='group' id='A2'></div>"
112 " </div>"
113 "</body>";
114 LoadHTML(html.c_str());
116 // Creating a RendererAccessibility should sent the tree to the browser.
117 scoped_ptr<TestRendererAccessibility> accessibility(
118 new TestRendererAccessibility(frame()));
119 accessibility->SendPendingAccessibilityEvents();
120 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
122 // If we post another event but the tree doesn't change,
123 // we should only send 1 node to the browser.
124 sink_->ClearMessages();
125 WebDocument document = view()->GetWebView()->mainFrame()->document();
126 WebAXObject root_obj = document.accessibilityObject();
127 accessibility->HandleAXEvent(
128 root_obj,
129 ui::AX_EVENT_LAYOUT_COMPLETE);
130 accessibility->SendPendingAccessibilityEvents();
131 EXPECT_EQ(1, CountAccessibilityNodesSentToBrowser());
133 // Make sure it's the root object that was updated.
134 AccessibilityHostMsg_EventParams event;
135 GetLastAccEvent(&event);
136 EXPECT_EQ(root_obj.axID(), event.update.nodes[0].id);
139 // If we reload the page and send a event, we should send
140 // all 4 nodes to the browser. Also double-check that we didn't
141 // leak any of the old BrowserTreeNodes.
142 LoadHTML(html.c_str());
143 document = view()->GetWebView()->mainFrame()->document();
144 root_obj = document.accessibilityObject();
145 sink_->ClearMessages();
146 accessibility->HandleAXEvent(
147 root_obj,
148 ui::AX_EVENT_LAYOUT_COMPLETE);
149 accessibility->SendPendingAccessibilityEvents();
150 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
152 // Even if the first event is sent on an element other than
153 // the root, the whole tree should be updated because we know
154 // the browser doesn't have the root element.
155 LoadHTML(html.c_str());
156 document = view()->GetWebView()->mainFrame()->document();
157 root_obj = document.accessibilityObject();
158 sink_->ClearMessages();
159 const WebAXObject& first_child = root_obj.childAt(0);
160 accessibility->HandleAXEvent(
161 first_child,
162 ui::AX_EVENT_LIVE_REGION_CHANGED);
163 accessibility->SendPendingAccessibilityEvents();
164 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
167 // http://crbug.com/253537
168 #if defined(OS_ANDROID)
169 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
170 DISABLED_AccessibilityMessagesQueueWhileSwappedOut
171 #else
172 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
173 AccessibilityMessagesQueueWhileSwappedOut
174 #endif
176 TEST_F(RendererAccessibilityTest,
177 MAYBE_AccessibilityMessagesQueueWhileSwappedOut) {
178 // This test breaks down in --site-per-process, as swapping out destroys
179 // the main frame and it cannot be further navigated.
180 // TODO(nasko): Figure out what this behavior looks like when swapped out
181 // no longer exists.
182 if (SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
183 return;
185 std::string html =
186 "<body>"
187 " <p>Hello, world.</p>"
188 "</body>";
189 LoadHTML(html.c_str());
190 static const int kProxyRoutingId = 13;
192 // Creating a RendererAccessibility should send the tree to the browser.
193 scoped_ptr<TestRendererAccessibility> accessibility(
194 new TestRendererAccessibility(frame()));
195 accessibility->SendPendingAccessibilityEvents();
196 EXPECT_EQ(5, CountAccessibilityNodesSentToBrowser());
198 // Post a "value changed" event, but then swap out
199 // before sending it. It shouldn't send the event while
200 // swapped out.
201 sink_->ClearMessages();
202 WebDocument document = view()->GetWebView()->mainFrame()->document();
203 WebAXObject root_obj = document.accessibilityObject();
204 accessibility->HandleAXEvent(
205 root_obj,
206 ui::AX_EVENT_VALUE_CHANGED);
207 view()->GetMainRenderFrame()->OnSwapOut(kProxyRoutingId, true,
208 content::FrameReplicationState());
209 accessibility->SendPendingAccessibilityEvents();
210 EXPECT_FALSE(sink_->GetUniqueMessageMatching(
211 AccessibilityHostMsg_Events::ID));
213 // Navigate, so we're not swapped out anymore. Now we should
214 // send accessibility events again. Note that the
215 // message that was queued up before will be quickly discarded
216 // because the element it was referring to no longer exists,
217 // so the event here is from loading this new page.
218 CommonNavigationParams common_params;
219 RequestNavigationParams request_params;
220 common_params.url = GURL("data:text/html,<p>Hello, again.</p>");
221 common_params.navigation_type = FrameMsg_Navigate_Type::NORMAL;
222 common_params.transition = ui::PAGE_TRANSITION_TYPED;
223 request_params.current_history_list_length = 1;
224 request_params.current_history_list_offset = 0;
225 request_params.pending_history_list_offset = 1;
226 request_params.page_id = -1;
227 frame()->OnNavigate(common_params, StartNavigationParams(), request_params);
228 accessibility->SendPendingAccessibilityEvents();
229 EXPECT_TRUE(sink_->GetUniqueMessageMatching(
230 AccessibilityHostMsg_Events::ID));
233 TEST_F(RendererAccessibilityTest, HideAccessibilityObject) {
234 // Test RendererAccessibility and make sure it sends the
235 // proper event to the browser when an object in the tree
236 // is hidden, but its children are not.
237 std::string html =
238 "<body>"
239 " <div role='group' id='A'>"
240 " <div role='group' id='B'>"
241 " <div role='group' id='C' style='visibility:visible'>"
242 " </div>"
243 " </div>"
244 " </div>"
245 "</body>";
246 LoadHTML(html.c_str());
248 scoped_ptr<TestRendererAccessibility> accessibility(
249 new TestRendererAccessibility(frame()));
250 accessibility->SendPendingAccessibilityEvents();
251 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
253 WebDocument document = view()->GetWebView()->mainFrame()->document();
254 WebAXObject root_obj = document.accessibilityObject();
255 WebAXObject node_a = root_obj.childAt(0);
256 WebAXObject node_b = node_a.childAt(0);
257 WebAXObject node_c = node_b.childAt(0);
259 // Hide node 'B' ('C' stays visible).
260 ExecuteJavaScriptForTests(
261 "document.getElementById('B').style.visibility = 'hidden';");
262 // Force layout now.
263 ExecuteJavaScriptForTests("document.getElementById('B').offsetLeft;");
265 // Send a childrenChanged on 'A'.
266 sink_->ClearMessages();
267 accessibility->HandleAXEvent(
268 node_a,
269 ui::AX_EVENT_CHILDREN_CHANGED);
271 accessibility->SendPendingAccessibilityEvents();
272 AccessibilityHostMsg_EventParams event;
273 GetLastAccEvent(&event);
274 ASSERT_EQ(2U, event.update.nodes.size());
276 // RendererAccessibility notices that 'C' is being reparented,
277 // so it clears the subtree rooted at 'A', then updates 'A' and then 'C'.
278 EXPECT_EQ(node_a.axID(), event.update.node_id_to_clear);
279 EXPECT_EQ(node_a.axID(), event.update.nodes[0].id);
280 EXPECT_EQ(node_c.axID(), event.update.nodes[1].id);
281 EXPECT_EQ(2, CountAccessibilityNodesSentToBrowser());
284 TEST_F(RendererAccessibilityTest, ShowAccessibilityObject) {
285 // Test RendererAccessibility and make sure it sends the
286 // proper event to the browser when an object in the tree
287 // is shown, causing its own already-visible children to be
288 // reparented to it.
289 std::string html =
290 "<body>"
291 " <div role='group' id='A'>"
292 " <div role='group' id='B' style='visibility:hidden'>"
293 " <div role='group' id='C' style='visibility:visible'>"
294 " </div>"
295 " </div>"
296 " </div>"
297 "</body>";
298 LoadHTML(html.c_str());
300 scoped_ptr<TestRendererAccessibility> accessibility(
301 new TestRendererAccessibility(frame()));
302 accessibility->SendPendingAccessibilityEvents();
303 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
305 // Show node 'B', then send a childrenChanged on 'A'.
306 ExecuteJavaScriptForTests(
307 "document.getElementById('B').style.visibility = 'visible';");
308 ExecuteJavaScriptForTests("document.getElementById('B').offsetLeft;");
310 sink_->ClearMessages();
311 WebDocument document = view()->GetWebView()->mainFrame()->document();
312 WebAXObject root_obj = document.accessibilityObject();
313 WebAXObject node_a = root_obj.childAt(0);
314 WebAXObject node_b = node_a.childAt(0);
315 WebAXObject node_c = node_b.childAt(0);
317 accessibility->HandleAXEvent(
318 node_a,
319 ui::AX_EVENT_CHILDREN_CHANGED);
321 accessibility->SendPendingAccessibilityEvents();
322 AccessibilityHostMsg_EventParams event;
323 GetLastAccEvent(&event);
325 ASSERT_EQ(3U, event.update.nodes.size());
326 EXPECT_EQ(node_a.axID(), event.update.node_id_to_clear);
327 EXPECT_EQ(node_a.axID(), event.update.nodes[0].id);
328 EXPECT_EQ(node_b.axID(), event.update.nodes[1].id);
329 EXPECT_EQ(node_c.axID(), event.update.nodes[2].id);
330 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
333 TEST_F(RendererAccessibilityTest, DetachAccessibilityObject) {
334 // Test RendererAccessibility and make sure it sends the
335 // proper event to the browser when an object in the tree
336 // is detached, but its children are not. This can happen when
337 // a layout occurs and an anonymous render block is no longer needed.
338 std::string html =
339 "<body aria-label='Body'>"
340 "<span>1</span><span style='display:block'>2</span>"
341 "</body>";
342 LoadHTML(html.c_str());
344 scoped_ptr<TestRendererAccessibility> accessibility(
345 new TestRendererAccessibility(frame()));
346 accessibility->SendPendingAccessibilityEvents();
347 EXPECT_EQ(7, CountAccessibilityNodesSentToBrowser());
349 // Initially, the accessibility tree looks like this:
351 // Document
352 // +--Body
353 // +--Anonymous Block
354 // +--Static Text "1"
355 // +--Inline Text Box "1"
356 // +--Static Text "2"
357 // +--Inline Text Box "2"
358 WebDocument document = view()->GetWebView()->mainFrame()->document();
359 WebAXObject root_obj = document.accessibilityObject();
360 WebAXObject body = root_obj.childAt(0);
361 WebAXObject anonymous_block = body.childAt(0);
362 WebAXObject text_1 = anonymous_block.childAt(0);
363 WebAXObject text_2 = body.childAt(1);
365 // Change the display of the second 'span' back to inline, which causes the
366 // anonymous block to be destroyed.
367 ExecuteJavaScriptForTests(
368 "document.querySelectorAll('span')[1].style.display = 'inline';");
369 // Force layout now.
370 ExecuteJavaScriptForTests("document.body.offsetLeft;");
372 // Send a childrenChanged on the body.
373 sink_->ClearMessages();
374 accessibility->HandleAXEvent(
375 body,
376 ui::AX_EVENT_CHILDREN_CHANGED);
378 accessibility->SendPendingAccessibilityEvents();
380 // Afterwards, the accessibility tree looks like this:
382 // Document
383 // +--Body
384 // +--Static Text "1"
385 // +--Inline Text Box "1"
386 // +--Static Text "2"
387 // +--Inline Text Box "2"
389 // We just assert that there are now four nodes in the
390 // accessibility tree and that only three nodes needed
391 // to be updated (the body, the static text 1, and
392 // the static text 2).
394 AccessibilityHostMsg_EventParams event;
395 GetLastAccEvent(&event);
396 ASSERT_EQ(5U, event.update.nodes.size());
398 EXPECT_EQ(body.axID(), event.update.nodes[0].id);
399 EXPECT_EQ(text_1.axID(), event.update.nodes[1].id);
400 // The third event is to update text_2, but its id changes
401 // so we don't have a test expectation for it.
404 TEST_F(RendererAccessibilityTest, EventOnObjectNotInTree) {
405 // Test RendererAccessibility and make sure it doesn't send anything
406 // if we get a notification from Blink for an object that isn't in the
407 // tree, like the scroll area that's the parent of the main document,
408 // which we don't expose.
409 std::string html = "<body><input></body>";
410 LoadHTML(html.c_str());
412 scoped_ptr<TestRendererAccessibility> accessibility(
413 new TestRendererAccessibility(frame()));
414 accessibility->SendPendingAccessibilityEvents();
415 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
417 WebDocument document = view()->GetWebView()->mainFrame()->document();
418 WebAXObject root_obj = document.accessibilityObject();
419 WebAXObject scroll_area = root_obj.parentObject();
420 EXPECT_EQ(blink::WebAXRoleScrollArea, scroll_area.role());
422 // Try to fire a message on the scroll area, and assert that we just
423 // ignore it.
424 sink_->ClearMessages();
425 accessibility->HandleAXEvent(scroll_area,
426 ui::AX_EVENT_VALUE_CHANGED);
428 accessibility->SendPendingAccessibilityEvents();
430 const IPC::Message* message =
431 sink_->GetUniqueMessageMatching(AccessibilityHostMsg_Events::ID);
432 ASSERT_TRUE(message);
433 base::Tuple<std::vector<AccessibilityHostMsg_EventParams>, int> param;
434 AccessibilityHostMsg_Events::Read(message, &param);
435 ASSERT_EQ(0U, base::get<0>(param).size());
438 TEST_F(RendererAccessibilityTest, TextSelectionShouldSendRoot) {
439 // A text selection change in a text field will be reflected in attributes
440 // of the root node. Verify that the root node is updated as the result
441 // of a text change event.
442 std::string html =
443 "<body>"
444 " <div role='group'>"
445 " <input id='input' type='text' value='hello there'>"
446 " </div>"
447 "</body>";
448 LoadHTML(html.c_str());
450 scoped_ptr<TestRendererAccessibility> accessibility(
451 new TestRendererAccessibility(frame()));
452 accessibility->SendPendingAccessibilityEvents();
453 sink_->ClearMessages();
455 WebDocument document = view()->GetWebView()->mainFrame()->document();
456 WebAXObject root_obj = document.accessibilityObject();
457 WebAXObject input_obj =
458 document.getElementById("input").accessibilityObject();
459 ASSERT_EQ(blink::WebAXRoleTextField, input_obj.role());
460 ExecuteJavaScriptForTests("document.getElementById('input').focus();");
461 accessibility->HandleAXEvent(
462 input_obj,
463 ui::AX_EVENT_TEXT_SELECTION_CHANGED);
464 accessibility->SendPendingAccessibilityEvents();
465 std::vector<AccessibilityHostMsg_EventParams> all_events;
466 GetAllAccEvents(&all_events);
467 EXPECT_EQ(2U, all_events.size());
468 bool had_root_update = false, had_input_update = false;
469 for (auto i = all_events.begin(); i != all_events.end(); ++i) {
470 ASSERT_EQ(ui::AX_EVENT_TEXT_SELECTION_CHANGED, i->event_type);
471 ASSERT_EQ(1U, i->update.nodes.size());
472 if (root_obj.axID() == i->update.nodes[0].id)
473 had_root_update = true;
474 if (input_obj.axID() == i->update.nodes[0].id)
475 had_input_update = true;
477 ASSERT_TRUE(had_root_update);
478 ASSERT_TRUE(had_input_update);
480 } // namespace content