| OLD | NEW |
| 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" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 DumpAccessibilityTreeHelper::GetAllowEmptyString(); |
| 96 const std::string& deny_str = helper_.GetDenyString(); | 96 const std::string& allow_str = |
| 97 DumpAccessibilityTreeHelper::GetAllowString(); |
| 98 const std::string& deny_str = |
| 99 DumpAccessibilityTreeHelper::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 DumpAccessibilityTreeHelper::GetExpectedFileSuffix()); |
| 154 file_util::ReadFileToString( | 149 file_util::ReadFileToString( |
| 155 expected_file, | 150 expected_file, |
| 156 &expected_contents_raw); | 151 &expected_contents_raw); |
| 157 | 152 |
| 158 // Tolerate Windows-style line endings (\r\n) in the expected file: | 153 // 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. | 154 // normalize by deleting all \r from the file (if any) to leave only \n. |
| 160 std::string expected_contents; | 155 std::string expected_contents; |
| 161 RemoveChars(expected_contents_raw, "\r", &expected_contents); | 156 RemoveChars(expected_contents_raw, "\r", &expected_contents); |
| 162 | 157 |
| 163 if (!expected_contents.compare(0, strlen(kMarkSkipFile), kMarkSkipFile)) { | 158 if (!expected_contents.compare(0, strlen(kMarkSkipFile), kMarkSkipFile)) { |
| 164 printf("Skipping this test on this platform.\n"); | 159 printf("Skipping this test on this platform.\n"); |
| 165 return; | 160 return; |
| 166 } | 161 } |
| 167 | 162 |
| 168 // Load the page. | 163 // Load the page. |
| 169 WindowedNotificationObserver tree_updated_observer( | 164 WindowedNotificationObserver tree_updated_observer( |
| 170 NOTIFICATION_ACCESSIBILITY_LOAD_COMPLETE, | 165 NOTIFICATION_ACCESSIBILITY_LOAD_COMPLETE, |
| 171 NotificationService::AllSources()); | 166 NotificationService::AllSources()); |
| 172 string16 html_contents16; | 167 string16 html_contents16; |
| 173 html_contents16 = UTF8ToUTF16(html_contents); | 168 html_contents16 = UTF8ToUTF16(html_contents); |
| 174 GURL url = GetTestUrl("accessibility", | 169 GURL url = GetTestUrl("accessibility", |
| 175 html_file.BaseName().MaybeAsASCII().c_str()); | 170 html_file.BaseName().MaybeAsASCII().c_str()); |
| 176 NavigateToURL(shell(), url); | 171 NavigateToURL(shell(), url); |
| 177 | 172 |
| 178 // Wait for the tree. | 173 // Wait for the tree. |
| 179 tree_updated_observer.Wait(); | 174 tree_updated_observer.Wait(); |
| 175 DumpAccessibilityTreeHelper helper( |
| 176 host_view->GetBrowserAccessibilityManager()->GetRoot()); |
| 177 |
| 178 // Parse filters in the test file. |
| 179 std::vector<Filter> filters; |
| 180 AddDefaultFilters(&filters); |
| 181 ParseFilters(html_contents, &filters); |
| 182 helper.SetFilters(filters); |
| 180 | 183 |
| 181 // Perform a diff (or write the initial baseline). | 184 // Perform a diff (or write the initial baseline). |
| 182 string16 actual_contents_utf16; | 185 string16 actual_contents_utf16; |
| 183 helper_.DumpAccessibilityTree( | 186 helper.FormatAccessibilityTree(&actual_contents_utf16); |
| 184 host_view->GetBrowserAccessibilityManager()->GetRoot(), | |
| 185 &actual_contents_utf16); | |
| 186 std::string actual_contents = UTF16ToUTF8(actual_contents_utf16); | 187 std::string actual_contents = UTF16ToUTF8(actual_contents_utf16); |
| 187 std::vector<std::string> actual_lines, expected_lines; | 188 std::vector<std::string> actual_lines, expected_lines; |
| 188 Tokenize(actual_contents, "\n", &actual_lines); | 189 Tokenize(actual_contents, "\n", &actual_lines); |
| 189 Tokenize(expected_contents, "\n", &expected_lines); | 190 Tokenize(expected_contents, "\n", &expected_lines); |
| 190 // Marking the end of the file with a line of text ensures that | 191 // Marking the end of the file with a line of text ensures that |
| 191 // file length differences are found. | 192 // file length differences are found. |
| 192 expected_lines.push_back(kMarkEndOfFile); | 193 expected_lines.push_back(kMarkEndOfFile); |
| 193 actual_lines.push_back(kMarkEndOfFile); | 194 actual_lines.push_back(kMarkEndOfFile); |
| 194 | 195 |
| 195 std::vector<int> diff_lines = DiffLines(expected_lines, actual_lines); | 196 std::vector<int> diff_lines = DiffLines(expected_lines, actual_lines); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 212 expected_lines[line].c_str()); | 213 expected_lines[line].c_str()); |
| 213 } | 214 } |
| 214 printf("\nActual\n"); | 215 printf("\nActual\n"); |
| 215 printf("------\n"); | 216 printf("------\n"); |
| 216 printf("%s\n", actual_contents.c_str()); | 217 printf("%s\n", actual_contents.c_str()); |
| 217 } | 218 } |
| 218 | 219 |
| 219 if (!file_util::PathExists(expected_file)) { | 220 if (!file_util::PathExists(expected_file)) { |
| 220 base::FilePath actual_file = | 221 base::FilePath actual_file = |
| 221 base::FilePath(html_file.RemoveExtension().value() + | 222 base::FilePath(html_file.RemoveExtension().value() + |
| 222 helper_.GetActualFileSuffix()); | 223 DumpAccessibilityTreeHelper::GetActualFileSuffix()); |
| 223 | 224 |
| 224 EXPECT_TRUE(file_util::WriteFile( | 225 EXPECT_TRUE(file_util::WriteFile( |
| 225 actual_file, actual_contents.c_str(), actual_contents.size())); | 226 actual_file, actual_contents.c_str(), actual_contents.size())); |
| 226 | 227 |
| 227 ADD_FAILURE() << "No expectation found. Create it by doing:\n" | 228 ADD_FAILURE() << "No expectation found. Create it by doing:\n" |
| 228 << "mv " << actual_file.LossyDisplayName() << " " | 229 << "mv " << actual_file.LossyDisplayName() << " " |
| 229 << expected_file.LossyDisplayName(); | 230 << expected_file.LossyDisplayName(); |
| 230 } | 231 } |
| 231 } | 232 } |
| 232 | 233 |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 | 436 |
| 436 IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityUl) { | 437 IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityUl) { |
| 437 RunTest(FILE_PATH_LITERAL("ul.html")); | 438 RunTest(FILE_PATH_LITERAL("ul.html")); |
| 438 } | 439 } |
| 439 | 440 |
| 440 IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityWbr) { | 441 IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, AccessibilityWbr) { |
| 441 RunTest(FILE_PATH_LITERAL("wbr.html")); | 442 RunTest(FILE_PATH_LITERAL("wbr.html")); |
| 442 } | 443 } |
| 443 | 444 |
| 444 } // namespace content | 445 } // namespace content |
| OLD | NEW |