Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: content/browser/accessibility/dump_accessibility_browsertest_base.cc

Issue 787813006: Pull out a base class from DumpAccessibilityTree (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fix_legacy_outerwidth
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
David Tseng 2014/12/17 01:03:32 No (c) (and other instances).
dmazzoni 2014/12/17 07:04:50 Done.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/accessibility/dump_accessibility_browsertest_base.h"
6
7 #include <set>
8 #include <string>
9 #include <vector>
10
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"
27
28 namespace content {
29
30 namespace {
31
32 const char kCommentToken = '#';
33 const char kMarkSkipFile[] = "#<skip";
34 const char kMarkEndOfFile[] = "<-- End-of-file -->";
35 const char kSignalDiff[] = "*";
36
37 } // namespace
38
39 typedef AccessibilityTreeFormatter::Filter Filter;
40
41 base::string16
42 DumpAccessibilityTestBase::DumpUnfilteredAccessibilityTreeAsString() {
43 WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
44 shell()->web_contents());
45 AccessibilityTreeFormatter formatter(
46 web_contents->GetRootBrowserAccessibilityManager()->GetRoot());
47 std::vector<Filter> filters;
48 filters.push_back(Filter(base::ASCIIToUTF16("*"), Filter::ALLOW));
49 formatter.SetFilters(filters);
50 base::string16 ax_tree_dump;
51 formatter.FormatAccessibilityTree(&ax_tree_dump);
52 return ax_tree_dump;
53 }
54
55 std::vector<int> DumpAccessibilityTestBase::DiffLines(
56 const std::vector<std::string>& expected_lines,
57 const std::vector<std::string>& actual_lines) {
58 int actual_lines_count = actual_lines.size();
59 int expected_lines_count = expected_lines.size();
60 std::vector<int> diff_lines;
61 int i = 0, j = 0;
62 while (i < actual_lines_count && j < expected_lines_count) {
63 if (expected_lines[j].size() == 0 ||
64 expected_lines[j][0] == kCommentToken) {
65 // Skip comment lines and blank lines in expected output.
66 ++j;
67 continue;
68 }
69
70 if (actual_lines[i] != expected_lines[j])
71 diff_lines.push_back(j);
72 ++i;
73 ++j;
74 }
75
76 // Actual file has been fully checked.
77 return diff_lines;
78 }
79
80 void DumpAccessibilityTestBase::ParseHtmlForExtraDirectives(
81 const std::string& test_html,
82 std::vector<Filter>* filters,
83 std::string* wait_for) {
84 std::vector<std::string> lines;
85 base::SplitString(test_html, '\n', &lines);
86 for (std::vector<std::string>::const_iterator iter = lines.begin();
87 iter != lines.end();
88 ++iter) {
89 const std::string& line = *iter;
90 const std::string& allow_empty_str =
91 AccessibilityTreeFormatter::GetAllowEmptyString();
92 const std::string& allow_str =
93 AccessibilityTreeFormatter::GetAllowString();
94 const std::string& deny_str =
95 AccessibilityTreeFormatter::GetDenyString();
96 const std::string& wait_str = "@WAIT-FOR:";
97 if (StartsWithASCII(line, allow_empty_str, true)) {
98 filters->push_back(
99 Filter(base::UTF8ToUTF16(line.substr(allow_empty_str.size())),
100 Filter::ALLOW_EMPTY));
101 } else if (StartsWithASCII(line, allow_str, true)) {
102 filters->push_back(Filter(base::UTF8ToUTF16(
103 line.substr(allow_str.size())),
104 Filter::ALLOW));
105 } else if (StartsWithASCII(line, deny_str, true)) {
106 filters->push_back(Filter(base::UTF8ToUTF16(
107 line.substr(deny_str.size())),
108 Filter::DENY));
109 } else if (StartsWithASCII(line, wait_str, true)) {
110 *wait_for = line.substr(wait_str.size());
111 }
112 }
113 }
114
115 void DumpAccessibilityTestBase::RunTest(
116 const base::FilePath::CharType* file_path) {
117 NavigateToURL(shell(), GURL(url::kAboutBlankURL));
118
119 // Setup test paths.
120 base::FilePath dir_test_data;
121 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &dir_test_data));
122 base::FilePath test_path(
123 dir_test_data.Append(FILE_PATH_LITERAL("accessibility")));
124 ASSERT_TRUE(base::PathExists(test_path))
125 << test_path.LossyDisplayName();
126
127 base::FilePath html_file = test_path.Append(base::FilePath(file_path));
128 // Output the test path to help anyone who encounters a failure and needs
129 // to know where to look.
130 printf("Testing: %s\n", html_file.MaybeAsASCII().c_str());
131
132 std::string html_contents;
133 base::ReadFileToString(html_file, &html_contents);
134
135 // Read the expected file.
136 std::string expected_contents_raw;
137 base::FilePath expected_file =
138 base::FilePath(html_file.RemoveExtension().value() +
139 AccessibilityTreeFormatter::GetExpectedFileSuffix());
140 base::ReadFileToString(expected_file, &expected_contents_raw);
141
142 // Tolerate Windows-style line endings (\r\n) in the expected file:
143 // normalize by deleting all \r from the file (if any) to leave only \n.
144 std::string expected_contents;
145 base::RemoveChars(expected_contents_raw, "\r", &expected_contents);
146
147 if (!expected_contents.compare(0, strlen(kMarkSkipFile), kMarkSkipFile)) {
148 printf("Skipping this test on this platform.\n");
149 return;
150 }
151
152 // Parse filters and other directives in the test file.
153 std::string wait_for;
154 AddDefaultFilters(&filters_);
155 ParseHtmlForExtraDirectives(html_contents, &filters_, &wait_for);
156
157 // Load the page.
158 base::string16 html_contents16;
159 html_contents16 = base::UTF8ToUTF16(html_contents);
160 GURL url = GetTestUrl("accessibility",
161 html_file.BaseName().MaybeAsASCII().c_str());
162
163 // If there's a @WAIT-FOR directive, set up an accessibility notification
164 // waiter that returns on any event; we'll stop when we get the text we're
165 // waiting for, or time out. Otherwise just wait specifically for
166 // the "load complete" event.
167 scoped_ptr<AccessibilityNotificationWaiter> waiter;
168 if (!wait_for.empty()) {
169 waiter.reset(new AccessibilityNotificationWaiter(
170 shell(), AccessibilityModeComplete, ui::AX_EVENT_NONE));
171 } else {
172 waiter.reset(new AccessibilityNotificationWaiter(
173 shell(), AccessibilityModeComplete, ui::AX_EVENT_LOAD_COMPLETE));
174 }
175
176 // Load the test html.
177 NavigateToURL(shell(), url);
178
179 // Wait for notifications. If there's a @WAIT-FOR directive, break when
180 // the text we're waiting for appears in the dump, otherwise break after
181 // the first notification, which will be a load complete.
182 do {
183 waiter->WaitForNotification();
184 if (!wait_for.empty()) {
185 base::string16 tree_dump = DumpUnfilteredAccessibilityTreeAsString();
186 if (base::UTF16ToUTF8(tree_dump).find(wait_for) != std::string::npos)
187 wait_for.clear();
188 }
189 } while (!wait_for.empty());
190
191 // Call the subclass to dump the output.
192 std::vector<std::string> actual_lines = Dump();
193
194 // Perform a diff (or write the initial baseline).
195 std::vector<std::string> expected_lines;
196 Tokenize(expected_contents, "\n", &expected_lines);
197 // Marking the end of the file with a line of text ensures that
198 // file length differences are found.
199 expected_lines.push_back(kMarkEndOfFile);
200 actual_lines.push_back(kMarkEndOfFile);
201 std::string actual_contents = JoinString(actual_lines, "\n");
202
203 std::vector<int> diff_lines = DiffLines(expected_lines, actual_lines);
204 bool is_different = diff_lines.size() > 0;
205 EXPECT_FALSE(is_different);
206 if (is_different) {
207 AdditionalErrorLogging();
208
209 // Mark the expected lines which did not match actual output with a *.
210 printf("* Line Expected\n");
211 printf("- ---- --------\n");
212 for (int line = 0, diff_index = 0;
213 line < static_cast<int>(expected_lines.size());
214 ++line) {
215 bool is_diff = false;
216 if (diff_index < static_cast<int>(diff_lines.size()) &&
217 diff_lines[diff_index] == line) {
218 is_diff = true;
219 ++diff_index;
220 }
221 printf("%1s %4d %s\n", is_diff? kSignalDiff : "", line + 1,
222 expected_lines[line].c_str());
223 }
224 printf("\nActual\n");
225 printf("------\n");
226 printf("%s\n", actual_contents.c_str());
227 }
228
229 if (!base::PathExists(expected_file)) {
230 base::FilePath actual_file =
231 base::FilePath(html_file.RemoveExtension().value() +
232 AccessibilityTreeFormatter::GetActualFileSuffix());
233
234 EXPECT_TRUE(base::WriteFile(
235 actual_file, actual_contents.c_str(), actual_contents.size()));
236
237 ADD_FAILURE() << "No expectation found. Create it by doing:\n"
238 << "mv " << actual_file.LossyDisplayName() << " "
239 << expected_file.LossyDisplayName();
240 }
241 }
242
243 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698