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

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

Powered by Google App Engine
This is Rietveld 408576698