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.
9 #include "base/path_service.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "content/browser/accessibility/accessibility_event_recorder.h"
14 #include "content/browser/accessibility/accessibility_tree_formatter.h"
15 #include "content/browser/accessibility/browser_accessibility.h"
16 #include "content/browser/accessibility/browser_accessibility_manager.h"
17 #include "content/browser/accessibility/dump_accessibility_browsertest_base.h"
18 #include "content/browser/web_contents/web_contents_impl.h"
19 #include "content/public/common/content_paths.h"
20 #include "content/shell/browser/shell.h"
21 #include "content/test/accessibility_browser_test_utils.h"
25 typedef AccessibilityTreeFormatter::Filter Filter
;
27 // Tests that the right platform-specific accessibility events are fired
28 // in response to things that happen in a web document.
30 // Similar to DumpAccessibilityTree in that each test consists of a
31 // single HTML file, possibly with a few special directives in comments,
32 // and then expectation files in text format for each platform.
34 // While DumpAccessibilityTree just loads the document and then
35 // prints out a text representation of the accessibility tree,
36 // DumpAccessibilityEvents loads the document, then executes the
37 // JavaScript function "go()", then it records and dumps all accessibility
38 // events generated as a result of that "go" function executing.
40 // How each event is dumped is platform-specific, but should be of the
45 // ...where <event> is the name of the event, and <node> is a description
46 // of the node the event fired on, such as the node's role and name.
48 // As with DumpAccessibilityTree, DumpAccessibilityEvents takes the events
49 // dumped from that particular html file and compares it to the expectation
50 // file in the same directory (for example, test-name-expected-win.txt)
51 // and the test fails if they don't agree.
53 // Currently it's not possible to test for accessibility events that
54 // don't fire immediately (i.e. within the call scope of the call to "go()");
55 // the test framework calls "go()" and then sends a sentinel event signaling
56 // the end of the test; anything received after that is too late.
57 class DumpAccessibilityEventsTest
: public DumpAccessibilityTestBase
{
59 void AddDefaultFilters(std::vector
<Filter
>* filters
) override
{
62 std::vector
<std::string
> Dump() override
;
64 void OnDiffFailed() override
;
65 void RunEventTest(const base::FilePath::CharType
* file_path
);
68 base::string16 initial_tree_
;
69 base::string16 final_tree_
;
72 std::vector
<std::string
> DumpAccessibilityEventsTest::Dump() {
73 WebContentsImpl
* web_contents
= static_cast<WebContentsImpl
*>(
74 shell()->web_contents());
75 scoped_ptr
<AccessibilityEventRecorder
> event_recorder(
76 AccessibilityEventRecorder::Create(
77 web_contents
->GetRootBrowserAccessibilityManager()));
79 // Save a copy of the accessibility tree (as a text dump); we'll
80 // log this for the user later if the test fails.
81 initial_tree_
= DumpUnfilteredAccessibilityTreeAsString();
83 // Create a waiter that waits for any one accessibility event.
84 // This will ensure that after calling the go() function, we
85 // block until we've received an accessibility event generated as
86 // a result of this function.
87 scoped_ptr
<AccessibilityNotificationWaiter
> waiter
;
88 waiter
.reset(new AccessibilityNotificationWaiter(
89 shell(), AccessibilityModeComplete
, ui::AX_EVENT_NONE
));
92 web_contents
->GetMainFrame()->ExecuteJavaScript(
93 base::ASCIIToUTF16("go()"));
95 // Wait for at least one accessibility event generated in response to
97 waiter
->WaitForNotification();
99 // More than one accessibility event could have been generated.
100 // To make sure we've received all accessibility events, add a
101 // sentinel by calling AccessibilityHitTest and waiting for a HOVER
102 // event in response.
103 waiter
.reset(new AccessibilityNotificationWaiter(
104 shell(), AccessibilityModeComplete
, ui::AX_EVENT_HOVER
));
105 BrowserAccessibilityManager
* manager
=
106 web_contents
->GetRootBrowserAccessibilityManager();
107 manager
->delegate()->AccessibilityHitTest(gfx::Point(0, 0));
108 waiter
->WaitForNotification();
110 // Save a copy of the final accessibility tree (as a text dump); we'll
111 // log this for the user later if the test fails.
112 final_tree_
= DumpUnfilteredAccessibilityTreeAsString();
114 // Dump the event logs, running them through any filters specified
116 std::vector
<std::string
> event_logs
= event_recorder
->event_logs();
117 std::vector
<std::string
> result
;
118 for (size_t i
= 0; i
< event_logs
.size(); ++i
) {
119 if (AccessibilityTreeFormatter::MatchesFilters(
120 filters_
, base::UTF8ToUTF16(event_logs
[i
]), true)) {
121 result
.push_back(event_logs
[i
]);
127 void DumpAccessibilityEventsTest::OnDiffFailed() {
129 printf("Initial accessibility tree (after load complete):\n");
130 printf("%s\n", base::UTF16ToUTF8(initial_tree_
).c_str());
132 printf("Final accessibility tree after events fired:\n");
133 printf("%s\n", base::UTF16ToUTF8(final_tree_
).c_str());
137 void DumpAccessibilityEventsTest::RunEventTest(
138 const base::FilePath::CharType
* file_path
) {
139 base::FilePath dir_test_data
;
140 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA
, &dir_test_data
));
141 base::FilePath
test_path(dir_test_data
.AppendASCII("accessibility")
142 .AppendASCII("event"));
143 ASSERT_TRUE(base::PathExists(test_path
)) << test_path
.LossyDisplayName();
145 base::FilePath event_file
= test_path
.Append(base::FilePath(file_path
));
146 RunTest(event_file
, "accessibility/event");
149 // TODO(dmazzoni): port these tests to run on all platforms.
150 #if defined(OS_WIN) || defined(OS_MACOSX)
152 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
153 AccessibilityEventsAddAlert
) {
154 RunEventTest(FILE_PATH_LITERAL("add-alert.html"));
157 // http://crbug.com/447962 - change that makes this test pass will land soon.
158 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
159 DISABLED_AccessibilityEventsAddChild
) {
160 RunEventTest(FILE_PATH_LITERAL("add-child.html"));
163 // http://crbug.com/447962 - change that makes this test pass will land soon.
164 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
165 DISABLED_AccessibilityEventsAddHiddenAttribute
) {
166 RunEventTest(FILE_PATH_LITERAL("add-hidden-attribute.html"));
169 // http://crbug.com/447962 - change that makes this test pass will land soon.
170 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
171 DISABLED_AccessibilityEventsAddHiddenAttributeSubtree
) {
172 RunEventTest(FILE_PATH_LITERAL("add-hidden-attribute-subtree.html"));
175 // http://crbug.com/447962 - change that makes this test pass will land soon.
176 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
177 DISABLED_AccessibilityEventsAddSubtree
) {
178 RunEventTest(FILE_PATH_LITERAL("add-subtree.html"));
181 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
182 AccessibilityEventsCheckedStateChanged
) {
183 RunEventTest(FILE_PATH_LITERAL("checked-state-changed.html"));
186 // http://crbug.com/447962 - change that makes this test pass will land soon.
187 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
188 DISABLED_AccessibilityEventsCSSDisplay
) {
189 RunEventTest(FILE_PATH_LITERAL("css-display.html"));
192 // http://crbug.com/447962 - change that makes this test pass will land soon.
193 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
194 DISABLED_AccessibilityEventsCSSVisibility
) {
195 RunEventTest(FILE_PATH_LITERAL("css-visibility.html"));
198 // http://crbug.com/447962 - change that makes this test pass will land soon.
199 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
200 DISABLED_AccessibilityEventsDescriptionChange
) {
201 RunEventTest(FILE_PATH_LITERAL("description-change.html"));
204 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
205 AccessibilityEventsInputTypeTextValueChanged
) {
206 RunEventTest(FILE_PATH_LITERAL("input-type-text-value-changed.html"));
209 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
210 AccessibilityEventsNameChange
) {
211 RunEventTest(FILE_PATH_LITERAL("name-change.html"));
214 // http://crbug.com/447962 - change that makes this test pass will land soon.
215 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
216 DISABLED_AccessibilityEventsRemoveChild
) {
217 RunEventTest(FILE_PATH_LITERAL("remove-child.html"));
220 // http://crbug.com/447962 - change that makes this test pass will land soon.
221 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
222 DISABLED_AccessibilityEventsRemoveHiddenAttribute
) {
223 RunEventTest(FILE_PATH_LITERAL("remove-hidden-attribute.html"));
226 // http://crbug.com/447962 - change that makes this test pass will land soon.
227 IN_PROC_BROWSER_TEST_F(
228 DumpAccessibilityEventsTest
,
229 DISABLED_AccessibilityEventsRemoveHiddenAttributeSubtree
) {
230 RunEventTest(FILE_PATH_LITERAL("remove-hidden-attribute-subtree.html"));
233 // http://crbug.com/447962 - change that makes this test pass will land soon.
234 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
235 DISABLED_AccessibilityEventsRemoveSubtree
) {
236 RunEventTest(FILE_PATH_LITERAL("remove-subtree.html"));
239 // http://crbug.com/447962 - change that makes this test pass will land soon.
240 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
241 DISABLED_AccessibilityEventsTextChanged
) {
242 RunEventTest(FILE_PATH_LITERAL("text-changed.html"));
245 #endif // defined(OS_WIN)
247 } // namespace content