Add ICU message format support
[chromium-blink-merge.git] / content / renderer / accessibility / renderer_accessibility_browsertest.cc
bloba0b65509c49630faa18a089183ffc321ccee2b36
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 GetLastAccEvent(
70 AccessibilityHostMsg_EventParams* params) {
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 ASSERT_GE(base::get<0>(param).size(), 1U);
77 *params = base::get<0>(param)[0];
80 int CountAccessibilityNodesSentToBrowser() {
81 AccessibilityHostMsg_EventParams event;
82 GetLastAccEvent(&event);
83 return event.update.nodes.size();
86 protected:
87 IPC::TestSink* sink_;
89 DISALLOW_COPY_AND_ASSIGN(RendererAccessibilityTest);
93 TEST_F(RendererAccessibilityTest, SendFullAccessibilityTreeOnReload) {
94 // The job of RendererAccessibility is to serialize the
95 // accessibility tree built by WebKit and send it to the browser.
96 // When the accessibility tree changes, it tries to send only
97 // the nodes that actually changed or were reparented. This test
98 // ensures that the messages sent are correct in cases when a page
99 // reloads, and that internal state is properly garbage-collected.
100 std::string html =
101 "<body>"
102 " <div role='group' id='A'>"
103 " <div role='group' id='A1'></div>"
104 " <div role='group' id='A2'></div>"
105 " </div>"
106 "</body>";
107 LoadHTML(html.c_str());
109 // Creating a RendererAccessibility should sent the tree to the browser.
110 scoped_ptr<TestRendererAccessibility> accessibility(
111 new TestRendererAccessibility(frame()));
112 accessibility->SendPendingAccessibilityEvents();
113 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
115 // If we post another event but the tree doesn't change,
116 // we should only send 1 node to the browser.
117 sink_->ClearMessages();
118 WebDocument document = view()->GetWebView()->mainFrame()->document();
119 WebAXObject root_obj = document.accessibilityObject();
120 accessibility->HandleAXEvent(
121 root_obj,
122 ui::AX_EVENT_LAYOUT_COMPLETE);
123 accessibility->SendPendingAccessibilityEvents();
124 EXPECT_EQ(1, CountAccessibilityNodesSentToBrowser());
126 // Make sure it's the root object that was updated.
127 AccessibilityHostMsg_EventParams event;
128 GetLastAccEvent(&event);
129 EXPECT_EQ(root_obj.axID(), event.update.nodes[0].id);
132 // If we reload the page and send a event, we should send
133 // all 4 nodes to the browser. Also double-check that we didn't
134 // leak any of the old BrowserTreeNodes.
135 LoadHTML(html.c_str());
136 document = view()->GetWebView()->mainFrame()->document();
137 root_obj = document.accessibilityObject();
138 sink_->ClearMessages();
139 accessibility->HandleAXEvent(
140 root_obj,
141 ui::AX_EVENT_LAYOUT_COMPLETE);
142 accessibility->SendPendingAccessibilityEvents();
143 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
145 // Even if the first event is sent on an element other than
146 // the root, the whole tree should be updated because we know
147 // the browser doesn't have the root element.
148 LoadHTML(html.c_str());
149 document = view()->GetWebView()->mainFrame()->document();
150 root_obj = document.accessibilityObject();
151 sink_->ClearMessages();
152 const WebAXObject& first_child = root_obj.childAt(0);
153 accessibility->HandleAXEvent(
154 first_child,
155 ui::AX_EVENT_LIVE_REGION_CHANGED);
156 accessibility->SendPendingAccessibilityEvents();
157 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
160 // http://crbug.com/253537
161 #if defined(OS_ANDROID)
162 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
163 DISABLED_AccessibilityMessagesQueueWhileSwappedOut
164 #else
165 #define MAYBE_AccessibilityMessagesQueueWhileSwappedOut \
166 AccessibilityMessagesQueueWhileSwappedOut
167 #endif
169 TEST_F(RendererAccessibilityTest,
170 MAYBE_AccessibilityMessagesQueueWhileSwappedOut) {
171 // This test breaks down in --site-per-process, as swapping out destroys
172 // the main frame and it cannot be further navigated.
173 // TODO(nasko): Figure out what this behavior looks like when swapped out
174 // no longer exists.
175 if (SiteIsolationPolicy::IsSwappedOutStateForbidden()) {
176 return;
178 std::string html =
179 "<body>"
180 " <p>Hello, world.</p>"
181 "</body>";
182 LoadHTML(html.c_str());
183 static const int kProxyRoutingId = 13;
185 // Creating a RendererAccessibility should send the tree to the browser.
186 scoped_ptr<TestRendererAccessibility> accessibility(
187 new TestRendererAccessibility(frame()));
188 accessibility->SendPendingAccessibilityEvents();
189 EXPECT_EQ(5, CountAccessibilityNodesSentToBrowser());
191 // Post a "value changed" event, but then swap out
192 // before sending it. It shouldn't send the event while
193 // swapped out.
194 sink_->ClearMessages();
195 WebDocument document = view()->GetWebView()->mainFrame()->document();
196 WebAXObject root_obj = document.accessibilityObject();
197 accessibility->HandleAXEvent(
198 root_obj,
199 ui::AX_EVENT_VALUE_CHANGED);
200 view()->GetMainRenderFrame()->OnSwapOut(kProxyRoutingId, true,
201 content::FrameReplicationState());
202 accessibility->SendPendingAccessibilityEvents();
203 EXPECT_FALSE(sink_->GetUniqueMessageMatching(
204 AccessibilityHostMsg_Events::ID));
206 // Navigate, so we're not swapped out anymore. Now we should
207 // send accessibility events again. Note that the
208 // message that was queued up before will be quickly discarded
209 // because the element it was referring to no longer exists,
210 // so the event here is from loading this new page.
211 CommonNavigationParams common_params;
212 RequestNavigationParams request_params;
213 common_params.url = GURL("data:text/html,<p>Hello, again.</p>");
214 common_params.navigation_type = FrameMsg_Navigate_Type::NORMAL;
215 common_params.transition = ui::PAGE_TRANSITION_TYPED;
216 request_params.current_history_list_length = 1;
217 request_params.current_history_list_offset = 0;
218 request_params.pending_history_list_offset = 1;
219 request_params.page_id = -1;
220 frame()->OnNavigate(common_params, StartNavigationParams(), request_params);
221 accessibility->SendPendingAccessibilityEvents();
222 EXPECT_TRUE(sink_->GetUniqueMessageMatching(
223 AccessibilityHostMsg_Events::ID));
226 TEST_F(RendererAccessibilityTest, HideAccessibilityObject) {
227 // Test RendererAccessibility and make sure it sends the
228 // proper event to the browser when an object in the tree
229 // is hidden, but its children are not.
230 std::string html =
231 "<body>"
232 " <div role='group' id='A'>"
233 " <div role='group' id='B'>"
234 " <div role='group' id='C' style='visibility:visible'>"
235 " </div>"
236 " </div>"
237 " </div>"
238 "</body>";
239 LoadHTML(html.c_str());
241 scoped_ptr<TestRendererAccessibility> accessibility(
242 new TestRendererAccessibility(frame()));
243 accessibility->SendPendingAccessibilityEvents();
244 EXPECT_EQ(4, CountAccessibilityNodesSentToBrowser());
246 WebDocument document = view()->GetWebView()->mainFrame()->document();
247 WebAXObject root_obj = document.accessibilityObject();
248 WebAXObject node_a = root_obj.childAt(0);
249 WebAXObject node_b = node_a.childAt(0);
250 WebAXObject node_c = node_b.childAt(0);
252 // Hide node 'B' ('C' stays visible).
253 ExecuteJavaScriptForTests(
254 "document.getElementById('B').style.visibility = 'hidden';");
255 // Force layout now.
256 ExecuteJavaScriptForTests("document.getElementById('B').offsetLeft;");
258 // Send a childrenChanged on 'A'.
259 sink_->ClearMessages();
260 accessibility->HandleAXEvent(
261 node_a,
262 ui::AX_EVENT_CHILDREN_CHANGED);
264 accessibility->SendPendingAccessibilityEvents();
265 AccessibilityHostMsg_EventParams event;
266 GetLastAccEvent(&event);
267 ASSERT_EQ(2U, event.update.nodes.size());
269 // RendererAccessibility notices that 'C' is being reparented,
270 // so it clears the subtree rooted at 'A', then updates 'A' and then 'C'.
271 EXPECT_EQ(node_a.axID(), event.update.node_id_to_clear);
272 EXPECT_EQ(node_a.axID(), event.update.nodes[0].id);
273 EXPECT_EQ(node_c.axID(), event.update.nodes[1].id);
274 EXPECT_EQ(2, CountAccessibilityNodesSentToBrowser());
277 TEST_F(RendererAccessibilityTest, ShowAccessibilityObject) {
278 // Test RendererAccessibility and make sure it sends the
279 // proper event to the browser when an object in the tree
280 // is shown, causing its own already-visible children to be
281 // reparented to it.
282 std::string html =
283 "<body>"
284 " <div role='group' id='A'>"
285 " <div role='group' id='B' style='visibility:hidden'>"
286 " <div role='group' id='C' style='visibility:visible'>"
287 " </div>"
288 " </div>"
289 " </div>"
290 "</body>";
291 LoadHTML(html.c_str());
293 scoped_ptr<TestRendererAccessibility> accessibility(
294 new TestRendererAccessibility(frame()));
295 accessibility->SendPendingAccessibilityEvents();
296 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
298 // Show node 'B', then send a childrenChanged on 'A'.
299 ExecuteJavaScriptForTests(
300 "document.getElementById('B').style.visibility = 'visible';");
301 ExecuteJavaScriptForTests("document.getElementById('B').offsetLeft;");
303 sink_->ClearMessages();
304 WebDocument document = view()->GetWebView()->mainFrame()->document();
305 WebAXObject root_obj = document.accessibilityObject();
306 WebAXObject node_a = root_obj.childAt(0);
307 WebAXObject node_b = node_a.childAt(0);
308 WebAXObject node_c = node_b.childAt(0);
310 accessibility->HandleAXEvent(
311 node_a,
312 ui::AX_EVENT_CHILDREN_CHANGED);
314 accessibility->SendPendingAccessibilityEvents();
315 AccessibilityHostMsg_EventParams event;
316 GetLastAccEvent(&event);
318 ASSERT_EQ(3U, event.update.nodes.size());
319 EXPECT_EQ(node_a.axID(), event.update.node_id_to_clear);
320 EXPECT_EQ(node_a.axID(), event.update.nodes[0].id);
321 EXPECT_EQ(node_b.axID(), event.update.nodes[1].id);
322 EXPECT_EQ(node_c.axID(), event.update.nodes[2].id);
323 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
326 TEST_F(RendererAccessibilityTest, DetachAccessibilityObject) {
327 // Test RendererAccessibility and make sure it sends the
328 // proper event to the browser when an object in the tree
329 // is detached, but its children are not. This can happen when
330 // a layout occurs and an anonymous render block is no longer needed.
331 std::string html =
332 "<body aria-label='Body'>"
333 "<span>1</span><span style='display:block'>2</span>"
334 "</body>";
335 LoadHTML(html.c_str());
337 scoped_ptr<TestRendererAccessibility> accessibility(
338 new TestRendererAccessibility(frame()));
339 accessibility->SendPendingAccessibilityEvents();
340 EXPECT_EQ(7, CountAccessibilityNodesSentToBrowser());
342 // Initially, the accessibility tree looks like this:
344 // Document
345 // +--Body
346 // +--Anonymous Block
347 // +--Static Text "1"
348 // +--Inline Text Box "1"
349 // +--Static Text "2"
350 // +--Inline Text Box "2"
351 WebDocument document = view()->GetWebView()->mainFrame()->document();
352 WebAXObject root_obj = document.accessibilityObject();
353 WebAXObject body = root_obj.childAt(0);
354 WebAXObject anonymous_block = body.childAt(0);
355 WebAXObject text_1 = anonymous_block.childAt(0);
356 WebAXObject text_2 = body.childAt(1);
358 // Change the display of the second 'span' back to inline, which causes the
359 // anonymous block to be destroyed.
360 ExecuteJavaScriptForTests(
361 "document.querySelectorAll('span')[1].style.display = 'inline';");
362 // Force layout now.
363 ExecuteJavaScriptForTests("document.body.offsetLeft;");
365 // Send a childrenChanged on the body.
366 sink_->ClearMessages();
367 accessibility->HandleAXEvent(
368 body,
369 ui::AX_EVENT_CHILDREN_CHANGED);
371 accessibility->SendPendingAccessibilityEvents();
373 // Afterwards, the accessibility tree looks like this:
375 // Document
376 // +--Body
377 // +--Static Text "1"
378 // +--Inline Text Box "1"
379 // +--Static Text "2"
380 // +--Inline Text Box "2"
382 // We just assert that there are now four nodes in the
383 // accessibility tree and that only three nodes needed
384 // to be updated (the body, the static text 1, and
385 // the static text 2).
387 AccessibilityHostMsg_EventParams event;
388 GetLastAccEvent(&event);
389 ASSERT_EQ(5U, event.update.nodes.size());
391 EXPECT_EQ(body.axID(), event.update.nodes[0].id);
392 EXPECT_EQ(text_1.axID(), event.update.nodes[1].id);
393 // The third event is to update text_2, but its id changes
394 // so we don't have a test expectation for it.
397 TEST_F(RendererAccessibilityTest, EventOnObjectNotInTree) {
398 // Test RendererAccessibility and make sure it doesn't send anything
399 // if we get a notification from Blink for an object that isn't in the
400 // tree, like the scroll area that's the parent of the main document,
401 // which we don't expose.
402 std::string html = "<body><input></body>";
403 LoadHTML(html.c_str());
405 scoped_ptr<TestRendererAccessibility> accessibility(
406 new TestRendererAccessibility(frame()));
407 accessibility->SendPendingAccessibilityEvents();
408 EXPECT_EQ(3, CountAccessibilityNodesSentToBrowser());
410 WebDocument document = view()->GetWebView()->mainFrame()->document();
411 WebAXObject root_obj = document.accessibilityObject();
412 WebAXObject scroll_area = root_obj.parentObject();
413 EXPECT_EQ(blink::WebAXRoleScrollArea, scroll_area.role());
415 // Try to fire a message on the scroll area, and assert that we just
416 // ignore it.
417 sink_->ClearMessages();
418 accessibility->HandleAXEvent(scroll_area,
419 ui::AX_EVENT_VALUE_CHANGED);
421 accessibility->SendPendingAccessibilityEvents();
423 const IPC::Message* message =
424 sink_->GetUniqueMessageMatching(AccessibilityHostMsg_Events::ID);
425 ASSERT_TRUE(message);
426 base::Tuple<std::vector<AccessibilityHostMsg_EventParams>, int> param;
427 AccessibilityHostMsg_Events::Read(message, &param);
428 ASSERT_EQ(0U, base::get<0>(param).size());
431 } // namespace content