1 // Copyright (c) 2014 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 "content/browser/frame_host/render_frame_host_impl.h"
6 #include "content/browser/web_contents/web_contents_impl.h"
7 #include "content/public/browser/ax_event_notification_details.h"
8 #include "content/public/test/browser_test_utils.h"
9 #include "content/public/test/content_browser_test.h"
10 #include "content/public/test/content_browser_test_utils.h"
11 #include "content/shell/browser/shell.h"
12 #include "content/test/accessibility_browser_test_utils.h"
13 #include "ui/accessibility/ax_node.h"
14 #include "ui/accessibility/ax_tree.h"
18 class AccessibilityIpcErrorBrowserTest
: public ContentBrowserTest
{
20 AccessibilityIpcErrorBrowserTest() {}
23 // Convenience method to get the value of a particular AXNode
24 // attribute as a UTF-8 string.
25 std::string
GetAttr(const ui::AXNode
* node
,
26 const ui::AXStringAttribute attr
) {
27 const ui::AXNodeData
& data
= node
->data();
28 for (size_t i
= 0; i
< data
.string_attributes
.size(); ++i
) {
29 if (data
.string_attributes
[i
].first
== attr
)
30 return data
.string_attributes
[i
].second
;
35 DISALLOW_COPY_AND_ASSIGN(AccessibilityIpcErrorBrowserTest
);
38 IN_PROC_BROWSER_TEST_F(AccessibilityIpcErrorBrowserTest
,
39 ResetBrowserAccessibilityManager
) {
40 // Create a data url and load it.
41 const char url_str
[] =
43 "<div aria-live='polite'>"
44 " <p id='p1'>Paragraph One</p>"
45 " <p id='p2'>Paragraph Two</p>"
47 "<button id='button'>Button</button>";
49 NavigateToURL(shell(), url
);
51 // Simulate a condition where the RFH can't create a
52 // BrowserAccessibilityManager - like if there's no view.
53 RenderFrameHostImpl
* frame
= static_cast<RenderFrameHostImpl
*>(
54 shell()->web_contents()->GetMainFrame());
55 frame
->set_no_create_browser_accessibility_manager_for_testing(true);
56 ASSERT_EQ(nullptr, frame
->GetOrCreateBrowserAccessibilityManager());
59 // Enable accessibility (passing AccessibilityModeComplete to
60 // AccessibilityNotificationWaiter does this automatically) and wait for
62 AccessibilityNotificationWaiter
waiter(
63 shell(), AccessibilityModeComplete
, ui::AX_EVENT_LAYOUT_COMPLETE
);
64 waiter
.WaitForNotification();
67 // Make sure we still didn't create a BrowserAccessibilityManager.
68 // This means that at least one accessibility IPC was lost.
69 ASSERT_EQ(nullptr, frame
->GetOrCreateBrowserAccessibilityManager());
71 // Now create a BrowserAccessibilityManager, simulating what would happen
72 // if the RFH's view is created now - but then disallow recreating the
73 // BrowserAccessibilityManager so that we can test that this one gets
75 frame
->set_no_create_browser_accessibility_manager_for_testing(false);
76 ASSERT_TRUE(frame
->GetOrCreateBrowserAccessibilityManager() != nullptr);
77 frame
->set_no_create_browser_accessibility_manager_for_testing(true);
80 // Hide one of the elements on the page, and wait for an accessibility
81 // notification triggered by the hide.
82 AccessibilityNotificationWaiter
waiter(
83 shell(), AccessibilityModeComplete
, ui::AX_EVENT_LIVE_REGION_CHANGED
);
84 ASSERT_TRUE(ExecuteScript(
85 shell()->web_contents(),
86 "document.getElementById('p1').style.display = 'none';"));
87 waiter
.WaitForNotification();
90 // Show that accessibility was reset because the frame doesn't have a
91 // BrowserAccessibilityManager anymore.
92 ASSERT_EQ(nullptr, frame
->browser_accessibility_manager());
94 // Finally, allow creating a new accessibility manager and
95 // ensure that we didn't kill the renderer; we can still send it messages.
96 frame
->set_no_create_browser_accessibility_manager_for_testing(false);
97 const ui::AXTree
* tree
= nullptr;
99 AccessibilityNotificationWaiter
waiter(
100 shell(), AccessibilityModeComplete
, ui::AX_EVENT_FOCUS
);
101 ASSERT_TRUE(ExecuteScript(
102 shell()->web_contents(),
103 "document.getElementById('button').focus();"));
104 waiter
.WaitForNotification();
105 tree
= &waiter
.GetAXTree();
108 // Get the accessibility tree, ensure it reflects the final state of the
110 const ui::AXNode
* root
= tree
->root();
112 // Use this for debugging if the test fails.
113 VLOG(1) << tree
->ToString();
115 EXPECT_EQ(ui::AX_ROLE_ROOT_WEB_AREA
, root
->data().role
);
116 ASSERT_EQ(2, root
->child_count());
118 const ui::AXNode
* live_region
= root
->ChildAtIndex(0);
119 ASSERT_EQ(1, live_region
->child_count());
120 EXPECT_EQ(ui::AX_ROLE_DIV
, live_region
->data().role
);
122 const ui::AXNode
* para
= live_region
->ChildAtIndex(0);
123 EXPECT_EQ(ui::AX_ROLE_PARAGRAPH
, para
->data().role
);
125 const ui::AXNode
* button_container
= root
->ChildAtIndex(1);
126 EXPECT_EQ(ui::AX_ROLE_GROUP
, button_container
->data().role
);
127 ASSERT_EQ(1, button_container
->child_count());
129 const ui::AXNode
* button
= button_container
->ChildAtIndex(0);
130 EXPECT_EQ(ui::AX_ROLE_BUTTON
, button
->data().role
);
131 EXPECT_TRUE(button
->data().state
>> ui::AX_STATE_FOCUSED
& 1);
134 IN_PROC_BROWSER_TEST_F(AccessibilityIpcErrorBrowserTest
,
135 MultipleBadAccessibilityIPCsKillsRenderer
) {
136 // Create a data url and load it.
137 const char url_str
[] =
139 "<button id='button'>Button</button>";
141 NavigateToURL(shell(), url
);
142 RenderFrameHostImpl
* frame
= static_cast<RenderFrameHostImpl
*>(
143 shell()->web_contents()->GetMainFrame());
146 // Enable accessibility (passing AccessibilityModeComplete to
147 // AccessibilityNotificationWaiter does this automatically) and wait for
149 AccessibilityNotificationWaiter
waiter(
150 shell(), AccessibilityModeComplete
, ui::AX_EVENT_LAYOUT_COMPLETE
);
151 waiter
.WaitForNotification();
154 // Construct a bad accessibility message that BrowserAccessibilityManager
156 std::vector
<AXEventNotificationDetails
> bad_accessibility_event_list
;
157 bad_accessibility_event_list
.push_back(AXEventNotificationDetails());
158 bad_accessibility_event_list
[0].update
.node_id_to_clear
= -2;
160 // We should be able to reset accessibility |max_iterations-1| times
161 // (see render_frame_host_impl.cc - kMaxAccessibilityResets),
162 // but the subsequent time the renderer should be killed.
163 int max_iterations
= RenderFrameHostImpl::kMaxAccessibilityResets
;
165 for (int iteration
= 0; iteration
< max_iterations
; iteration
++) {
166 // Send the browser accessibility the bad message.
167 BrowserAccessibilityManager
* manager
=
168 frame
->GetOrCreateBrowserAccessibilityManager();
169 manager
->OnAccessibilityEvents(bad_accessibility_event_list
);
171 // Now the frame should have deleted the BrowserAccessibilityManager.
172 ASSERT_EQ(nullptr, frame
->browser_accessibility_manager());
174 if (iteration
== max_iterations
- 1)
177 AccessibilityNotificationWaiter
waiter(
178 shell(), AccessibilityModeComplete
, ui::AX_EVENT_LOAD_COMPLETE
);
179 waiter
.WaitForNotification();
182 // Wait for the renderer to be killed.
183 if (frame
->IsRenderFrameLive()) {
184 RenderProcessHostWatcher
render_process_watcher(
185 frame
->GetProcess(), RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT
);
186 render_process_watcher
.Wait();
188 ASSERT_FALSE(frame
->IsRenderFrameLive());
191 } // namespace content