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 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
158 AccessibilityEventsAddChild
) {
159 RunEventTest(FILE_PATH_LITERAL("add-child.html"));
162 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
163 AccessibilityEventsAddHiddenAttribute
) {
164 RunEventTest(FILE_PATH_LITERAL("add-hidden-attribute.html"));
167 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
168 AccessibilityEventsAddHiddenAttributeSubtree
) {
169 RunEventTest(FILE_PATH_LITERAL("add-hidden-attribute-subtree.html"));
172 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
173 AccessibilityEventsAddSubtree
) {
174 RunEventTest(FILE_PATH_LITERAL("add-subtree.html"));
177 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
178 AccessibilityEventsCheckedStateChanged
) {
179 RunEventTest(FILE_PATH_LITERAL("checked-state-changed.html"));
182 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
183 AccessibilityEventsCSSDisplay
) {
184 RunEventTest(FILE_PATH_LITERAL("css-display.html"));
187 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
188 AccessibilityEventsCSSVisibility
) {
189 RunEventTest(FILE_PATH_LITERAL("css-visibility.html"));
192 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
193 AccessibilityEventsDescriptionChange
) {
194 RunEventTest(FILE_PATH_LITERAL("description-change.html"));
197 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
198 AccessibilityEventsInnerHtmlChange
) {
199 RunEventTest(FILE_PATH_LITERAL("inner-html-change.html"));
202 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
203 AccessibilityEventsInputTypeTextValueChanged
) {
204 RunEventTest(FILE_PATH_LITERAL("input-type-text-value-changed.html"));
207 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
208 AccessibilityEventsNameChange
) {
209 RunEventTest(FILE_PATH_LITERAL("name-change.html"));
212 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
213 AccessibilityEventsRemoveChild
) {
214 RunEventTest(FILE_PATH_LITERAL("remove-child.html"));
217 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
218 AccessibilityEventsRemoveHiddenAttribute
) {
219 RunEventTest(FILE_PATH_LITERAL("remove-hidden-attribute.html"));
222 IN_PROC_BROWSER_TEST_F(
223 DumpAccessibilityEventsTest
,
224 AccessibilityEventsRemoveHiddenAttributeSubtree
) {
225 RunEventTest(FILE_PATH_LITERAL("remove-hidden-attribute-subtree.html"));
228 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
229 AccessibilityEventsRemoveSubtree
) {
230 RunEventTest(FILE_PATH_LITERAL("remove-subtree.html"));
233 IN_PROC_BROWSER_TEST_F(DumpAccessibilityEventsTest
,
234 AccessibilityEventsTextChanged
) {
235 RunEventTest(FILE_PATH_LITERAL("text-changed.html"));
238 #endif // defined(OS_WIN)
240 } // namespace content