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

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

Issue 12389028: Rename DumpAccesibilityTreeHelper to AccessibilityTreeFormatter, pull into content/browser. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add USE_AURA to conditions for stub implementations of AccessibilityTreeFormatter methods" Created 7 years, 9 months 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <set> 5 #include <set>
6 #include <string> 6 #include <string>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/path_service.h" 11 #include "base/path_service.h"
12 #include "base/string_util.h" 12 #include "base/string_util.h"
13 #include "base/string16.h" 13 #include "base/string16.h"
14 #include "base/strings/string_split.h" 14 #include "base/strings/string_split.h"
15 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
16 #include "content/browser/accessibility/accessibility_tree_formatter.h"
16 #include "content/browser/accessibility/browser_accessibility.h" 17 #include "content/browser/accessibility/browser_accessibility.h"
17 #include "content/browser/accessibility/browser_accessibility_manager.h" 18 #include "content/browser/accessibility/browser_accessibility_manager.h"
18 #include "content/browser/accessibility/dump_accessibility_tree_helper.h"
19 #include "content/browser/renderer_host/render_view_host_impl.h" 19 #include "content/browser/renderer_host/render_view_host_impl.h"
20 #include "content/port/browser/render_widget_host_view_port.h" 20 #include "content/port/browser/render_widget_host_view_port.h"
21 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/content_paths.h" 22 #include "content/public/common/content_paths.h"
23 #include "content/public/test/test_utils.h" 23 #include "content/public/test/test_utils.h"
24 #include "content/shell/shell.h" 24 #include "content/shell/shell.h"
25 #include "content/test/content_browser_test.h" 25 #include "content/test/content_browser_test.h"
26 #include "content/test/content_browser_test_utils.h" 26 #include "content/test/content_browser_test_utils.h"
27 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
28 28
29 namespace content { 29 namespace content {
30 30
31 namespace { 31 namespace {
32 32
33 const char kCommentToken = '#'; 33 const char kCommentToken = '#';
34 const char kMarkSkipFile[] = "#<skip"; 34 const char kMarkSkipFile[] = "#<skip";
35 const char kMarkEndOfFile[] = "<-- End-of-file -->"; 35 const char kMarkEndOfFile[] = "<-- End-of-file -->";
36 const char kSignalDiff[] = "*"; 36 const char kSignalDiff[] = "*";
37 37
38 } // namespace 38 } // namespace
39 39
40 typedef DumpAccessibilityTreeHelper::Filter Filter; 40 typedef AccessibilityTreeFormatter::Filter Filter;
41 41
42 // This test takes a snapshot of the platform BrowserAccessibility tree and 42 // This test takes a snapshot of the platform BrowserAccessibility tree and
43 // tests it against an expected baseline. 43 // tests it against an expected baseline.
44 // 44 //
45 // The flow of the test is as outlined below. 45 // The flow of the test is as outlined below.
46 // 1. Load an html file from chrome/test/data/accessibility. 46 // 1. Load an html file from chrome/test/data/accessibility.
47 // 2. Read the expectation. 47 // 2. Read the expectation.
48 // 3. Browse to the page and serialize the platform specific tree into a human 48 // 3. Browse to the page and serialize the platform specific tree into a human
49 // readable string. 49 // readable string.
50 // 4. Perform a comparison between actual and expected and fail if they do not 50 // 4. Perform a comparison between actual and expected and fail if they do not
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 } 84 }
85 85
86 void ParseFilters(const std::string& test_html, 86 void ParseFilters(const std::string& test_html,
87 std::vector<Filter>* filters) { 87 std::vector<Filter>* filters) {
88 std::vector<std::string> lines; 88 std::vector<std::string> lines;
89 base::SplitString(test_html, '\n', &lines); 89 base::SplitString(test_html, '\n', &lines);
90 for (std::vector<std::string>::const_iterator iter = lines.begin(); 90 for (std::vector<std::string>::const_iterator iter = lines.begin();
91 iter != lines.end(); 91 iter != lines.end();
92 ++iter) { 92 ++iter) {
93 const std::string& line = *iter; 93 const std::string& line = *iter;
94 const std::string& allow_empty_str = helper_.GetAllowEmptyString(); 94 const std::string& allow_empty_str =
95 const std::string& allow_str = helper_.GetAllowString(); 95 AccessibilityTreeFormatter::GetAllowEmptyString();
96 const std::string& deny_str = helper_.GetDenyString(); 96 const std::string& allow_str =
97 AccessibilityTreeFormatter::GetAllowString();
98 const std::string& deny_str =
99 AccessibilityTreeFormatter::GetDenyString();
97 if (StartsWithASCII(line, allow_empty_str, true)) { 100 if (StartsWithASCII(line, allow_empty_str, true)) {
98 filters->push_back( 101 filters->push_back(
99 Filter(UTF8ToUTF16(line.substr(allow_empty_str.size())), 102 Filter(UTF8ToUTF16(line.substr(allow_empty_str.size())),
100 Filter::ALLOW_EMPTY)); 103 Filter::ALLOW_EMPTY));
101 } else if (StartsWithASCII(line, allow_str, true)) { 104 } else if (StartsWithASCII(line, allow_str, true)) {
102 filters->push_back(Filter(UTF8ToUTF16(line.substr(allow_str.size())), 105 filters->push_back(Filter(UTF8ToUTF16(line.substr(allow_str.size())),
103 Filter::ALLOW)); 106 Filter::ALLOW));
104 } else if (StartsWithASCII(line, deny_str, true)) { 107 } else if (StartsWithASCII(line, deny_str, true)) {
105 filters->push_back(Filter(UTF8ToUTF16(line.substr(deny_str.size())), 108 filters->push_back(Filter(UTF8ToUTF16(line.substr(deny_str.size())),
106 Filter::DENY)); 109 Filter::DENY));
107 } 110 }
108 } 111 }
109 } 112 }
110 113
111 void RunTest(const base::FilePath::CharType* file_path); 114 void RunTest(const base::FilePath::CharType* file_path);
112
113 DumpAccessibilityTreeHelper helper_;
114 }; 115 };
115 116
116 void DumpAccessibilityTreeTest::RunTest( 117 void DumpAccessibilityTreeTest::RunTest(
117 const base::FilePath::CharType* file_path) { 118 const base::FilePath::CharType* file_path) {
118 NavigateToURL(shell(), GURL("about:blank")); 119 NavigateToURL(shell(), GURL("about:blank"));
119 RenderWidgetHostViewPort* host_view = static_cast<RenderWidgetHostViewPort*>( 120 RenderWidgetHostViewPort* host_view = static_cast<RenderWidgetHostViewPort*>(
120 shell()->web_contents()->GetRenderWidgetHostView()); 121 shell()->web_contents()->GetRenderWidgetHostView());
121 RenderWidgetHostImpl* host = 122 RenderWidgetHostImpl* host =
122 RenderWidgetHostImpl::From(host_view->GetRenderWidgetHost()); 123 RenderWidgetHostImpl::From(host_view->GetRenderWidgetHost());
123 RenderViewHostImpl* view_host = static_cast<RenderViewHostImpl*>(host); 124 RenderViewHostImpl* view_host = static_cast<RenderViewHostImpl*>(host);
124 view_host->set_save_accessibility_tree_for_testing(true); 125 view_host->set_save_accessibility_tree_for_testing(true);
125 view_host->SetAccessibilityMode(AccessibilityModeComplete); 126 view_host->SetAccessibilityMode(AccessibilityModeComplete);
126 127
127 // Setup test paths. 128 // Setup test paths.
128 base::FilePath dir_test_data; 129 base::FilePath dir_test_data;
129 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &dir_test_data)); 130 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &dir_test_data));
130 base::FilePath test_path( 131 base::FilePath test_path(
131 dir_test_data.Append(FILE_PATH_LITERAL("accessibility"))); 132 dir_test_data.Append(FILE_PATH_LITERAL("accessibility")));
132 ASSERT_TRUE(file_util::PathExists(test_path)) 133 ASSERT_TRUE(file_util::PathExists(test_path))
133 << test_path.LossyDisplayName(); 134 << test_path.LossyDisplayName();
134 135
135 base::FilePath html_file = test_path.Append(base::FilePath(file_path)); 136 base::FilePath html_file = test_path.Append(base::FilePath(file_path));
136 // Output the test path to help anyone who encounters a failure and needs 137 // Output the test path to help anyone who encounters a failure and needs
137 // to know where to look. 138 // to know where to look.
138 printf("Testing: %s\n", html_file.MaybeAsASCII().c_str()); 139 printf("Testing: %s\n", html_file.MaybeAsASCII().c_str());
139 140
140 std::string html_contents; 141 std::string html_contents;
141 file_util::ReadFileToString(html_file, &html_contents); 142 file_util::ReadFileToString(html_file, &html_contents);
142 143
143 // Parse filters in the test file.
144 std::vector<Filter> filters;
145 AddDefaultFilters(&filters);
146 ParseFilters(html_contents, &filters);
147 helper_.SetFilters(filters);
148
149 // Read the expected file. 144 // Read the expected file.
150 std::string expected_contents_raw; 145 std::string expected_contents_raw;
151 base::FilePath expected_file = 146 base::FilePath expected_file =
152 base::FilePath(html_file.RemoveExtension().value() + 147 base::FilePath(html_file.RemoveExtension().value() +
153 helper_.GetExpectedFileSuffix()); 148 AccessibilityTreeFormatter::GetExpectedFileSuffix());
154 file_util::ReadFileToString( 149 file_util::ReadFileToString(expected_file, &expected_contents_raw);
155 expected_file,
156 &expected_contents_raw);
157 150
158 // Tolerate Windows-style line endings (\r\n) in the expected file: 151 // Tolerate Windows-style line endings (\r\n) in the expected file:
159 // normalize by deleting all \r from the file (if any) to leave only \n. 152 // normalize by deleting all \r from the file (if any) to leave only \n.
160 std::string expected_contents; 153 std::string expected_contents;
161 RemoveChars(expected_contents_raw, "\r", &expected_contents); 154 RemoveChars(expected_contents_raw, "\r", &expected_contents);
162 155
163 if (!expected_contents.compare(0, strlen(kMarkSkipFile), kMarkSkipFile)) { 156 if (!expected_contents.compare(0, strlen(kMarkSkipFile), kMarkSkipFile)) {
164 printf("Skipping this test on this platform.\n"); 157 printf("Skipping this test on this platform.\n");
165 return; 158 return;
166 } 159 }
167 160
168 // Load the page. 161 // Load the page.
169 string16 html_contents16; 162 string16 html_contents16;
170 html_contents16 = UTF8ToUTF16(html_contents); 163 html_contents16 = UTF8ToUTF16(html_contents);
171 GURL url = GetTestUrl("accessibility", 164 GURL url = GetTestUrl("accessibility",
172 html_file.BaseName().MaybeAsASCII().c_str()); 165 html_file.BaseName().MaybeAsASCII().c_str());
173 scoped_refptr<MessageLoopRunner> loop_runner(new MessageLoopRunner); 166 scoped_refptr<MessageLoopRunner> loop_runner(new MessageLoopRunner);
174 view_host->SetAccessibilityLoadCompleteCallbackForTesting( 167 view_host->SetAccessibilityLoadCompleteCallbackForTesting(
175 loop_runner->QuitClosure()); 168 loop_runner->QuitClosure());
176 NavigateToURL(shell(), url); 169 NavigateToURL(shell(), url);
177 170
178 // Wait for the tree. 171 // Wait for the tree.
179 loop_runner->Run(); 172 loop_runner->Run();
180 173
174 AccessibilityTreeFormatter formatter(
175 host_view->GetBrowserAccessibilityManager()->GetRoot());
176
177 // Parse filters in the test file.
178 std::vector<Filter> filters;
179 AddDefaultFilters(&filters);
180 ParseFilters(html_contents, &filters);
181 formatter.SetFilters(filters);
182
181 // Perform a diff (or write the initial baseline). 183 // Perform a diff (or write the initial baseline).
182 string16 actual_contents_utf16; 184 string16 actual_contents_utf16;
183 helper_.DumpAccessibilityTree( 185 formatter.FormatAccessibilityTree(&actual_contents_utf16);
184 host_view->GetBrowserAccessibilityManager()->GetRoot(),
185 &actual_contents_utf16);
186 std::string actual_contents = UTF16ToUTF8(actual_contents_utf16); 186 std::string actual_contents = UTF16ToUTF8(actual_contents_utf16);
187 std::vector<std::string> actual_lines, expected_lines; 187 std::vector<std::string> actual_lines, expected_lines;
188 Tokenize(actual_contents, "\n", &actual_lines); 188 Tokenize(actual_contents, "\n", &actual_lines);
189 Tokenize(expected_contents, "\n", &expected_lines); 189 Tokenize(expected_contents, "\n", &expected_lines);
190 // Marking the end of the file with a line of text ensures that 190 // Marking the end of the file with a line of text ensures that
191 // file length differences are found. 191 // file length differences are found.
192 expected_lines.push_back(kMarkEndOfFile); 192 expected_lines.push_back(kMarkEndOfFile);
193 actual_lines.push_back(kMarkEndOfFile); 193 actual_lines.push_back(kMarkEndOfFile);
194 194
195 std::vector<int> diff_lines = DiffLines(expected_lines, actual_lines); 195 std::vector<int> diff_lines = DiffLines(expected_lines, actual_lines);
(...skipping 16 matching lines...) Expand all
212 expected_lines[line].c_str()); 212 expected_lines[line].c_str());
213 } 213 }
214 printf("\nActual\n"); 214 printf("\nActual\n");
215 printf("------\n"); 215 printf("------\n");
216 printf("%s\n", actual_contents.c_str()); 216 printf("%s\n", actual_contents.c_str());
217 } 217 }
218 218
219 if (!file_util::PathExists(expected_file)) { 219 if (!file_util::PathExists(expected_file)) {
220 base::FilePath actual_file = 220 base::FilePath actual_file =
221 base::FilePath(html_file.RemoveExtension().value() + 221 base::FilePath(html_file.RemoveExtension().value() +
222 helper_.GetActualFileSuffix()); 222 AccessibilityTreeFormatter::GetActualFileSuffix());
223 223
224 EXPECT_TRUE(file_util::WriteFile( 224 EXPECT_TRUE(file_util::WriteFile(
225 actual_file, actual_contents.c_str(), actual_contents.size())); 225 actual_file, actual_contents.c_str(), actual_contents.size()));
226 226
227 ADD_FAILURE() << "No expectation found. Create it by doing:\n" 227 ADD_FAILURE() << "No expectation found. Create it by doing:\n"
228 << "mv " << actual_file.LossyDisplayName() << " " 228 << "mv " << actual_file.LossyDisplayName() << " "
229 << expected_file.LossyDisplayName(); 229 << expected_file.LossyDisplayName();
230 } 230 }
231 } 231 }
232 232
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 435
436 IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityUl) { 436 IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityUl) {
437 RunTest(FILE_PATH_LITERAL("ul.html")); 437 RunTest(FILE_PATH_LITERAL("ul.html"));
438 } 438 }
439 439
440 IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityWbr) { 440 IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityWbr) {
441 RunTest(FILE_PATH_LITERAL("wbr.html")); 441 RunTest(FILE_PATH_LITERAL("wbr.html"));
442 } 442 }
443 443
444 } // namespace content 444 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698