Disable PDF tests that are failing after combining PDF plugin into chrome.
[chromium-blink-merge.git] / content / browser / accessibility / dump_accessibility_browsertest_base.cc
blobd09b852ff3473ad8de2e64389b01af20a54dedd2
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"
7 #include <set>
8 #include <string>
9 #include <vector>
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/accessibility/browser_accessibility_state_impl.h"
20 #include "content/browser/web_contents/web_contents_impl.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/content_paths.h"
23 #include "content/public/common/url_constants.h"
24 #include "content/public/test/content_browser_test.h"
25 #include "content/public/test/content_browser_test_utils.h"
26 #include "content/shell/browser/shell.h"
27 #include "content/test/accessibility_browser_test_utils.h"
29 namespace content {
31 namespace {
33 const char kCommentToken = '#';
34 const char kMarkSkipFile[] = "#<skip";
35 const char kMarkEndOfFile[] = "<-- End-of-file -->";
36 const char kSignalDiff[] = "*";
38 } // namespace
40 typedef AccessibilityTreeFormatter::Filter Filter;
42 DumpAccessibilityTestBase::DumpAccessibilityTestBase() {
45 DumpAccessibilityTestBase::~DumpAccessibilityTestBase() {
48 base::string16
49 DumpAccessibilityTestBase::DumpUnfilteredAccessibilityTreeAsString() {
50 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
51 shell()->web_contents());
52 AccessibilityTreeFormatter formatter(
53 web_contents->GetRootBrowserAccessibilityManager()->GetRoot());
54 std::vector<Filter> filters;
55 filters.push_back(Filter(base::ASCIIToUTF16("*"), Filter::ALLOW));
56 formatter.SetFilters(filters);
57 formatter.set_show_ids(true);
58 base::string16 ax_tree_dump;
59 formatter.FormatAccessibilityTree(&ax_tree_dump);
60 return ax_tree_dump;
63 std::vector<int> DumpAccessibilityTestBase::DiffLines(
64 const std::vector<std::string>& expected_lines,
65 const std::vector<std::string>& actual_lines) {
66 int actual_lines_count = actual_lines.size();
67 int expected_lines_count = expected_lines.size();
68 std::vector<int> diff_lines;
69 int i = 0, j = 0;
70 while (i < actual_lines_count && j < expected_lines_count) {
71 if (expected_lines[j].size() == 0 ||
72 expected_lines[j][0] == kCommentToken) {
73 // Skip comment lines and blank lines in expected output.
74 ++j;
75 continue;
78 if (actual_lines[i] != expected_lines[j])
79 diff_lines.push_back(j);
80 ++i;
81 ++j;
84 // Actual file has been fully checked.
85 return diff_lines;
88 void DumpAccessibilityTestBase::ParseHtmlForExtraDirectives(
89 const std::string& test_html,
90 std::vector<Filter>* filters,
91 std::string* wait_for) {
92 std::vector<std::string> lines;
93 base::SplitString(test_html, '\n', &lines);
94 for (std::vector<std::string>::const_iterator iter = lines.begin();
95 iter != lines.end();
96 ++iter) {
97 const std::string& line = *iter;
98 const std::string& allow_empty_str =
99 AccessibilityTreeFormatter::GetAllowEmptyString();
100 const std::string& allow_str =
101 AccessibilityTreeFormatter::GetAllowString();
102 const std::string& deny_str =
103 AccessibilityTreeFormatter::GetDenyString();
104 const std::string& wait_str = "@WAIT-FOR:";
105 if (StartsWithASCII(line, allow_empty_str, true)) {
106 filters->push_back(
107 Filter(base::UTF8ToUTF16(line.substr(allow_empty_str.size())),
108 Filter::ALLOW_EMPTY));
109 } else if (StartsWithASCII(line, allow_str, true)) {
110 filters->push_back(Filter(base::UTF8ToUTF16(
111 line.substr(allow_str.size())),
112 Filter::ALLOW));
113 } else if (StartsWithASCII(line, deny_str, true)) {
114 filters->push_back(Filter(base::UTF8ToUTF16(
115 line.substr(deny_str.size())),
116 Filter::DENY));
117 } else if (StartsWithASCII(line, wait_str, true)) {
118 *wait_for = line.substr(wait_str.size());
123 void DumpAccessibilityTestBase::RunTest(
124 const base::FilePath file_path, const char* file_dir) {
125 // Disable the "hot tracked" state (set when the mouse is hovering over
126 // an object) because it makes test output change based on the mouse position.
127 BrowserAccessibilityStateImpl::GetInstance()->
128 set_disable_hot_tracking_for_testing(true);
130 NavigateToURL(shell(), GURL(url::kAboutBlankURL));
132 // Output the test path to help anyone who encounters a failure and needs
133 // to know where to look.
134 printf("Testing: %s\n", file_path.MaybeAsASCII().c_str());
136 std::string html_contents;
137 base::ReadFileToString(file_path, &html_contents);
139 // Read the expected file.
140 std::string expected_contents_raw;
141 base::FilePath expected_file =
142 base::FilePath(file_path.RemoveExtension().value() +
143 AccessibilityTreeFormatter::GetExpectedFileSuffix());
144 base::ReadFileToString(expected_file, &expected_contents_raw);
146 // Tolerate Windows-style line endings (\r\n) in the expected file:
147 // normalize by deleting all \r from the file (if any) to leave only \n.
148 std::string expected_contents;
149 base::RemoveChars(expected_contents_raw, "\r", &expected_contents);
151 if (!expected_contents.compare(0, strlen(kMarkSkipFile), kMarkSkipFile)) {
152 printf("Skipping this test on this platform.\n");
153 return;
156 // Parse filters and other directives in the test file.
157 std::string wait_for;
158 AddDefaultFilters(&filters_);
159 ParseHtmlForExtraDirectives(html_contents, &filters_, &wait_for);
161 // Load the page.
162 base::string16 html_contents16;
163 html_contents16 = base::UTF8ToUTF16(html_contents);
164 GURL url = GetTestUrl(file_dir, file_path.BaseName().MaybeAsASCII().c_str());
166 // If there's a @WAIT-FOR directive, set up an accessibility notification
167 // waiter that returns on any event; we'll stop when we get the text we're
168 // waiting for, or time out. Otherwise just wait specifically for
169 // the "load complete" event.
170 scoped_ptr<AccessibilityNotificationWaiter> waiter;
171 if (!wait_for.empty()) {
172 waiter.reset(new AccessibilityNotificationWaiter(
173 shell(), AccessibilityModeComplete, ui::AX_EVENT_NONE));
174 } else {
175 waiter.reset(new AccessibilityNotificationWaiter(
176 shell(), AccessibilityModeComplete, ui::AX_EVENT_LOAD_COMPLETE));
179 // Load the test html.
180 NavigateToURL(shell(), url);
182 // Wait for notifications. If there's a @WAIT-FOR directive, break when
183 // the text we're waiting for appears in the dump, otherwise break after
184 // the first notification, which will be a load complete.
185 do {
186 waiter->WaitForNotification();
187 if (!wait_for.empty()) {
188 base::string16 tree_dump = DumpUnfilteredAccessibilityTreeAsString();
189 if (base::UTF16ToUTF8(tree_dump).find(wait_for) != std::string::npos)
190 wait_for.clear();
192 } while (!wait_for.empty());
194 // Call the subclass to dump the output.
195 std::vector<std::string> actual_lines = Dump();
197 // Perform a diff (or write the initial baseline).
198 std::vector<std::string> expected_lines;
199 Tokenize(expected_contents, "\n", &expected_lines);
200 // Marking the end of the file with a line of text ensures that
201 // file length differences are found.
202 expected_lines.push_back(kMarkEndOfFile);
203 actual_lines.push_back(kMarkEndOfFile);
204 std::string actual_contents = JoinString(actual_lines, "\n");
206 std::vector<int> diff_lines = DiffLines(expected_lines, actual_lines);
207 bool is_different = diff_lines.size() > 0;
208 EXPECT_FALSE(is_different);
209 if (is_different) {
210 OnDiffFailed();
212 // Mark the expected lines which did not match actual output with a *.
213 printf("* Line Expected\n");
214 printf("- ---- --------\n");
215 for (int line = 0, diff_index = 0;
216 line < static_cast<int>(expected_lines.size());
217 ++line) {
218 bool is_diff = false;
219 if (diff_index < static_cast<int>(diff_lines.size()) &&
220 diff_lines[diff_index] == line) {
221 is_diff = true;
222 ++diff_index;
224 printf("%1s %4d %s\n", is_diff? kSignalDiff : "", line + 1,
225 expected_lines[line].c_str());
227 printf("\nActual\n");
228 printf("------\n");
229 printf("%s\n", actual_contents.c_str());
232 if (!base::PathExists(expected_file)) {
233 base::FilePath actual_file =
234 base::FilePath(file_path.RemoveExtension().value() +
235 AccessibilityTreeFormatter::GetActualFileSuffix());
237 EXPECT_TRUE(base::WriteFile(
238 actual_file, actual_contents.c_str(), actual_contents.size()));
240 ADD_FAILURE() << "No expectation found. Create it by doing:\n"
241 << "mv " << actual_file.LossyDisplayName() << " "
242 << expected_file.LossyDisplayName();
246 } // namespace content