Add long running gmail memory benchmark for background tab.
[chromium-blink-merge.git] / content / browser / accessibility / dump_accessibility_browsertest_base.cc
blobf86f5d4f9a2f441aaef253374557288c85a7142a
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 (base::StartsWith(line, allow_empty_str,
106 base::CompareCase::SENSITIVE)) {
107 filters->push_back(
108 Filter(base::UTF8ToUTF16(line.substr(allow_empty_str.size())),
109 Filter::ALLOW_EMPTY));
110 } else if (base::StartsWith(line, allow_str,
111 base::CompareCase::SENSITIVE)) {
112 filters->push_back(Filter(base::UTF8ToUTF16(
113 line.substr(allow_str.size())),
114 Filter::ALLOW));
115 } else if (base::StartsWith(line, deny_str,
116 base::CompareCase::SENSITIVE)) {
117 filters->push_back(Filter(base::UTF8ToUTF16(
118 line.substr(deny_str.size())),
119 Filter::DENY));
120 } else if (base::StartsWith(line, wait_str,
121 base::CompareCase::SENSITIVE)) {
122 *wait_for = line.substr(wait_str.size());
127 void DumpAccessibilityTestBase::RunTest(
128 const base::FilePath file_path, const char* file_dir) {
129 // Disable the "hot tracked" state (set when the mouse is hovering over
130 // an object) because it makes test output change based on the mouse position.
131 BrowserAccessibilityStateImpl::GetInstance()->
132 set_disable_hot_tracking_for_testing(true);
134 NavigateToURL(shell(), GURL(url::kAboutBlankURL));
136 // Output the test path to help anyone who encounters a failure and needs
137 // to know where to look.
138 printf("Testing: %s\n", file_path.MaybeAsASCII().c_str());
140 std::string html_contents;
141 base::ReadFileToString(file_path, &html_contents);
143 // Read the expected file.
144 std::string expected_contents_raw;
145 base::FilePath expected_file =
146 base::FilePath(file_path.RemoveExtension().value() +
147 AccessibilityTreeFormatter::GetExpectedFileSuffix());
148 base::ReadFileToString(expected_file, &expected_contents_raw);
150 // Tolerate Windows-style line endings (\r\n) in the expected file:
151 // normalize by deleting all \r from the file (if any) to leave only \n.
152 std::string expected_contents;
153 base::RemoveChars(expected_contents_raw, "\r", &expected_contents);
155 if (!expected_contents.compare(0, strlen(kMarkSkipFile), kMarkSkipFile)) {
156 printf("Skipping this test on this platform.\n");
157 return;
160 // Parse filters and other directives in the test file.
161 std::string wait_for;
162 AddDefaultFilters(&filters_);
163 ParseHtmlForExtraDirectives(html_contents, &filters_, &wait_for);
165 // Load the page.
166 base::string16 html_contents16;
167 html_contents16 = base::UTF8ToUTF16(html_contents);
168 GURL url = GetTestUrl(file_dir, file_path.BaseName().MaybeAsASCII().c_str());
170 // If there's a @WAIT-FOR directive, set up an accessibility notification
171 // waiter that returns on any event; we'll stop when we get the text we're
172 // waiting for, or time out. Otherwise just wait specifically for
173 // the "load complete" event.
174 scoped_ptr<AccessibilityNotificationWaiter> waiter;
175 if (!wait_for.empty()) {
176 waiter.reset(new AccessibilityNotificationWaiter(
177 shell(), AccessibilityModeComplete, ui::AX_EVENT_NONE));
178 } else {
179 waiter.reset(new AccessibilityNotificationWaiter(
180 shell(), AccessibilityModeComplete, ui::AX_EVENT_LOAD_COMPLETE));
183 // Load the test html.
184 NavigateToURL(shell(), url);
186 // Wait for notifications. If there's a @WAIT-FOR directive, break when
187 // the text we're waiting for appears in the dump, otherwise break after
188 // the first notification, which will be a load complete.
189 do {
190 waiter->WaitForNotification();
191 if (!wait_for.empty()) {
192 base::string16 tree_dump = DumpUnfilteredAccessibilityTreeAsString();
193 if (base::UTF16ToUTF8(tree_dump).find(wait_for) != std::string::npos)
194 wait_for.clear();
196 } while (!wait_for.empty());
198 // Call the subclass to dump the output.
199 std::vector<std::string> actual_lines = Dump();
201 // Perform a diff (or write the initial baseline).
202 std::vector<std::string> expected_lines = base::SplitString(
203 expected_contents, "\n", base::KEEP_WHITESPACE,
204 base::SPLIT_WANT_NONEMPTY);
205 // Marking the end of the file with a line of text ensures that
206 // file length differences are found.
207 expected_lines.push_back(kMarkEndOfFile);
208 actual_lines.push_back(kMarkEndOfFile);
209 std::string actual_contents = base::JoinString(actual_lines, "\n");
211 std::vector<int> diff_lines = DiffLines(expected_lines, actual_lines);
212 bool is_different = diff_lines.size() > 0;
213 EXPECT_FALSE(is_different);
214 if (is_different) {
215 OnDiffFailed();
217 // Mark the expected lines which did not match actual output with a *.
218 printf("* Line Expected\n");
219 printf("- ---- --------\n");
220 for (int line = 0, diff_index = 0;
221 line < static_cast<int>(expected_lines.size());
222 ++line) {
223 bool is_diff = false;
224 if (diff_index < static_cast<int>(diff_lines.size()) &&
225 diff_lines[diff_index] == line) {
226 is_diff = true;
227 ++diff_index;
229 printf("%1s %4d %s\n", is_diff? kSignalDiff : "", line + 1,
230 expected_lines[line].c_str());
232 printf("\nActual\n");
233 printf("------\n");
234 printf("%s\n", actual_contents.c_str());
237 if (!base::PathExists(expected_file)) {
238 base::FilePath actual_file =
239 base::FilePath(file_path.RemoveExtension().value() +
240 AccessibilityTreeFormatter::GetActualFileSuffix());
242 EXPECT_TRUE(base::WriteFile(
243 actual_file, actual_contents.c_str(), actual_contents.size()));
245 ADD_FAILURE() << "No expectation found. Create it by doing:\n"
246 << "mv " << actual_file.LossyDisplayName() << " "
247 << expected_file.LossyDisplayName();
251 } // namespace content