1 // Copyright 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/accessibility/dump_accessibility_browsertest_base.h"
11 #include "base/path_service.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "content/browser/accessibility/accessibility_tree_formatter.h"
17 #include "content/browser/accessibility/browser_accessibility.h"
18 #include "content/browser/accessibility/browser_accessibility_manager.h"
19 #include "content/browser/web_contents/web_contents_impl.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/content_paths.h"
22 #include "content/public/common/url_constants.h"
23 #include "content/public/test/content_browser_test.h"
24 #include "content/public/test/content_browser_test_utils.h"
25 #include "content/shell/browser/shell.h"
26 #include "content/test/accessibility_browser_test_utils.h"
32 const char kCommentToken
= '#';
33 const char kMarkSkipFile
[] = "#<skip";
34 const char kMarkEndOfFile
[] = "<-- End-of-file -->";
35 const char kSignalDiff
[] = "*";
39 typedef AccessibilityTreeFormatter::Filter Filter
;
41 DumpAccessibilityTestBase::DumpAccessibilityTestBase() {
44 DumpAccessibilityTestBase::~DumpAccessibilityTestBase() {
48 DumpAccessibilityTestBase::DumpUnfilteredAccessibilityTreeAsString() {
49 WebContentsImpl
* web_contents
= static_cast<WebContentsImpl
*>(
50 shell()->web_contents());
51 AccessibilityTreeFormatter
formatter(
52 web_contents
->GetRootBrowserAccessibilityManager()->GetRoot());
53 std::vector
<Filter
> filters
;
54 filters
.push_back(Filter(base::ASCIIToUTF16("*"), Filter::ALLOW
));
55 formatter
.SetFilters(filters
);
56 formatter
.set_show_ids(true);
57 base::string16 ax_tree_dump
;
58 formatter
.FormatAccessibilityTree(&ax_tree_dump
);
62 std::vector
<int> DumpAccessibilityTestBase::DiffLines(
63 const std::vector
<std::string
>& expected_lines
,
64 const std::vector
<std::string
>& actual_lines
) {
65 int actual_lines_count
= actual_lines
.size();
66 int expected_lines_count
= expected_lines
.size();
67 std::vector
<int> diff_lines
;
69 while (i
< actual_lines_count
&& j
< expected_lines_count
) {
70 if (expected_lines
[j
].size() == 0 ||
71 expected_lines
[j
][0] == kCommentToken
) {
72 // Skip comment lines and blank lines in expected output.
77 if (actual_lines
[i
] != expected_lines
[j
])
78 diff_lines
.push_back(j
);
83 // Actual file has been fully checked.
87 void DumpAccessibilityTestBase::ParseHtmlForExtraDirectives(
88 const std::string
& test_html
,
89 std::vector
<Filter
>* filters
,
90 std::string
* wait_for
) {
91 std::vector
<std::string
> lines
;
92 base::SplitString(test_html
, '\n', &lines
);
93 for (std::vector
<std::string
>::const_iterator iter
= lines
.begin();
96 const std::string
& line
= *iter
;
97 const std::string
& allow_empty_str
=
98 AccessibilityTreeFormatter::GetAllowEmptyString();
99 const std::string
& allow_str
=
100 AccessibilityTreeFormatter::GetAllowString();
101 const std::string
& deny_str
=
102 AccessibilityTreeFormatter::GetDenyString();
103 const std::string
& wait_str
= "@WAIT-FOR:";
104 if (StartsWithASCII(line
, allow_empty_str
, true)) {
106 Filter(base::UTF8ToUTF16(line
.substr(allow_empty_str
.size())),
107 Filter::ALLOW_EMPTY
));
108 } else if (StartsWithASCII(line
, allow_str
, true)) {
109 filters
->push_back(Filter(base::UTF8ToUTF16(
110 line
.substr(allow_str
.size())),
112 } else if (StartsWithASCII(line
, deny_str
, true)) {
113 filters
->push_back(Filter(base::UTF8ToUTF16(
114 line
.substr(deny_str
.size())),
116 } else if (StartsWithASCII(line
, wait_str
, true)) {
117 *wait_for
= line
.substr(wait_str
.size());
122 void DumpAccessibilityTestBase::RunTest(
123 const base::FilePath file_path
, const char* file_dir
) {
124 NavigateToURL(shell(), GURL(url::kAboutBlankURL
));
126 // Output the test path to help anyone who encounters a failure and needs
127 // to know where to look.
128 printf("Testing: %s\n", file_path
.MaybeAsASCII().c_str());
130 std::string html_contents
;
131 base::ReadFileToString(file_path
, &html_contents
);
133 // Read the expected file.
134 std::string expected_contents_raw
;
135 base::FilePath expected_file
=
136 base::FilePath(file_path
.RemoveExtension().value() +
137 AccessibilityTreeFormatter::GetExpectedFileSuffix());
138 base::ReadFileToString(expected_file
, &expected_contents_raw
);
140 // Tolerate Windows-style line endings (\r\n) in the expected file:
141 // normalize by deleting all \r from the file (if any) to leave only \n.
142 std::string expected_contents
;
143 base::RemoveChars(expected_contents_raw
, "\r", &expected_contents
);
145 if (!expected_contents
.compare(0, strlen(kMarkSkipFile
), kMarkSkipFile
)) {
146 printf("Skipping this test on this platform.\n");
150 // Parse filters and other directives in the test file.
151 std::string wait_for
;
152 AddDefaultFilters(&filters_
);
153 ParseHtmlForExtraDirectives(html_contents
, &filters_
, &wait_for
);
156 base::string16 html_contents16
;
157 html_contents16
= base::UTF8ToUTF16(html_contents
);
158 GURL url
= GetTestUrl(file_dir
, file_path
.BaseName().MaybeAsASCII().c_str());
160 // If there's a @WAIT-FOR directive, set up an accessibility notification
161 // waiter that returns on any event; we'll stop when we get the text we're
162 // waiting for, or time out. Otherwise just wait specifically for
163 // the "load complete" event.
164 scoped_ptr
<AccessibilityNotificationWaiter
> waiter
;
165 if (!wait_for
.empty()) {
166 waiter
.reset(new AccessibilityNotificationWaiter(
167 shell(), AccessibilityModeComplete
, ui::AX_EVENT_NONE
));
169 waiter
.reset(new AccessibilityNotificationWaiter(
170 shell(), AccessibilityModeComplete
, ui::AX_EVENT_LOAD_COMPLETE
));
173 // Load the test html.
174 NavigateToURL(shell(), url
);
176 // Wait for notifications. If there's a @WAIT-FOR directive, break when
177 // the text we're waiting for appears in the dump, otherwise break after
178 // the first notification, which will be a load complete.
180 waiter
->WaitForNotification();
181 if (!wait_for
.empty()) {
182 base::string16 tree_dump
= DumpUnfilteredAccessibilityTreeAsString();
183 if (base::UTF16ToUTF8(tree_dump
).find(wait_for
) != std::string::npos
)
186 } while (!wait_for
.empty());
188 // Call the subclass to dump the output.
189 std::vector
<std::string
> actual_lines
= Dump();
191 // Perform a diff (or write the initial baseline).
192 std::vector
<std::string
> expected_lines
;
193 Tokenize(expected_contents
, "\n", &expected_lines
);
194 // Marking the end of the file with a line of text ensures that
195 // file length differences are found.
196 expected_lines
.push_back(kMarkEndOfFile
);
197 actual_lines
.push_back(kMarkEndOfFile
);
198 std::string actual_contents
= JoinString(actual_lines
, "\n");
200 std::vector
<int> diff_lines
= DiffLines(expected_lines
, actual_lines
);
201 bool is_different
= diff_lines
.size() > 0;
202 EXPECT_FALSE(is_different
);
206 // Mark the expected lines which did not match actual output with a *.
207 printf("* Line Expected\n");
208 printf("- ---- --------\n");
209 for (int line
= 0, diff_index
= 0;
210 line
< static_cast<int>(expected_lines
.size());
212 bool is_diff
= false;
213 if (diff_index
< static_cast<int>(diff_lines
.size()) &&
214 diff_lines
[diff_index
] == line
) {
218 printf("%1s %4d %s\n", is_diff
? kSignalDiff
: "", line
+ 1,
219 expected_lines
[line
].c_str());
221 printf("\nActual\n");
223 printf("%s\n", actual_contents
.c_str());
226 if (!base::PathExists(expected_file
)) {
227 base::FilePath actual_file
=
228 base::FilePath(file_path
.RemoveExtension().value() +
229 AccessibilityTreeFormatter::GetActualFileSuffix());
231 EXPECT_TRUE(base::WriteFile(
232 actual_file
, actual_contents
.c_str(), actual_contents
.size()));
234 ADD_FAILURE() << "No expectation found. Create it by doing:\n"
235 << "mv " << actual_file
.LossyDisplayName() << " "
236 << expected_file
.LossyDisplayName();
240 } // namespace content