Chromium Code Reviews| 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 <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 // The flow of the test is as outlined below. | 43 // The flow of the test is as outlined below. |
| 44 // 1. Load an html file from chrome/test/data/accessibility. | 44 // 1. Load an html file from chrome/test/data/accessibility. |
| 45 // 2. Read the expectation. | 45 // 2. Read the expectation. |
| 46 // 3. Browse to the page and serialize the platform specific tree into a human | 46 // 3. Browse to the page and serialize the platform specific tree into a human |
| 47 // readable string. | 47 // readable string. |
| 48 // 4. Perform a comparison between actual and expected and fail if they do not | 48 // 4. Perform a comparison between actual and expected and fail if they do not |
| 49 // exactly match. | 49 // exactly match. |
| 50 class DumpAccessibilityTreeTest : public InProcessBrowserTest { | 50 class DumpAccessibilityTreeTest : public InProcessBrowserTest { |
| 51 public: | 51 public: |
| 52 // Utility helper that does a comment aware equality check. | 52 // Utility helper that does a comment aware equality check. |
| 53 bool EqualsWithComments(std::string& expected, std::string& actual) { | 53 // Returns array of lines from expected file which are different. |
| 54 std::vector<int> DiffLines(std::string& expected, std::string& actual) { | |
| 55 std::vector<int> diff_lines; | |
| 54 std::vector<std::string> actual_lines, expected_lines; | 56 std::vector<std::string> actual_lines, expected_lines; |
| 55 int actual_lines_count = Tokenize(actual, "\n", &actual_lines); | 57 int actual_lines_count = Tokenize(actual, "\n", &actual_lines); |
| 56 int expected_lines_count = Tokenize(expected, "\n", &expected_lines); | 58 int expected_lines_count = Tokenize(expected, "\n", &expected_lines); |
| 57 int i = actual_lines_count - 1, j = expected_lines_count - 1; | 59 int i = 0, j = 0; |
| 58 while (i >= 0 && j >= 0) { | 60 boolean already_inside_diff_block = false; |
| 59 if (expected_lines[j].size() > 0 && | 61 while (i < actual_lines_count || j < expected_lines_count) { |
|
David Tseng
2012/04/02 23:29:38
Shouldn't this be i < actual_lines_count && j < ex
aaronlevbugs
2012/04/03 02:30:50
It was as odd way of ensuring same size files. New
| |
| 60 expected_lines[j][0] == kCommentToken) { | 62 if (j < expected_lines_count && |
| 61 --j; | 63 (expected_lines[j].size() == 0 || |
| 64 expected_lines[j][0] == kCommentToken)) { | |
| 65 // Skip comment lines and blank lines in expected output | |
|
David Tseng
2012/04/02 23:29:38
nit: end the sentence with a period.
aaronlevbugs
2012/04/03 02:30:50
Done.
| |
| 66 ++j; | |
| 62 continue; | 67 continue; |
| 63 } | 68 } |
| 64 | 69 |
| 65 if (actual_lines[i] != expected_lines[j]) | 70 if (i >= actual_lines_count || |
| 66 return false; | 71 j >= expected_lines_count || |
| 67 --i; | 72 actual_lines[i] != expected_lines[j]) { |
| 68 --j; | 73 if (!already_inside_diff_block) // Avoid redundant error reporting |
|
David Tseng
2012/04/02 23:29:38
How is this redundant? This would only pick up the
aaronlevbugs
2012/04/03 02:30:50
That's the idea, so that if one file has an extra
aaronlevbugs
2012/04/03 02:30:50
New patch doesn't need this.
| |
| 74 diff_lines.push_back(j); | |
| 75 already_inside_diff_block = true; | |
| 76 } | |
| 77 else | |
|
David Tseng
2012/04/02 23:29:38
Please use the following conventions for multi-lin
aaronlevbugs
2012/04/03 02:30:50
Done.
| |
| 78 already_inside_diff_block = false; | |
| 79 ++i; | |
| 80 ++j; | |
| 69 } | 81 } |
| 70 | 82 |
| 71 // Actual file has been fully checked. | 83 // Actual file has been fully checked. |
| 72 return i < 0; | 84 return diff_lines; |
| 73 } | 85 } |
| 74 | 86 |
| 75 DumpAccessibilityTreeHelper helper_; | 87 DumpAccessibilityTreeHelper helper_; |
| 76 }; | 88 }; |
| 77 | 89 |
| 78 IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, | 90 IN_PROC_BROWSER_TEST_F(DumpAccessibilityTreeTest, |
| 79 PlatformTreeDifferenceTest) { | 91 PlatformTreeDifferenceTest) { |
| 80 RenderWidgetHostViewPort* host_view = static_cast<RenderWidgetHostViewPort*>( | 92 RenderWidgetHostViewPort* host_view = static_cast<RenderWidgetHostViewPort*>( |
| 81 browser()->GetSelectedWebContents()->GetRenderWidgetHostView()); | 93 browser()->GetSelectedWebContents()->GetRenderWidgetHostView()); |
| 82 RenderWidgetHost* host = host_view->GetRenderWidgetHost(); | 94 RenderWidgetHost* host = host_view->GetRenderWidgetHost(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 99 // Grab all HTML files. | 111 // Grab all HTML files. |
| 100 file_util::FileEnumerator file_enumerator(test_path, | 112 file_util::FileEnumerator file_enumerator(test_path, |
| 101 false, | 113 false, |
| 102 file_util::FileEnumerator::FILES, | 114 file_util::FileEnumerator::FILES, |
| 103 FILE_PATH_LITERAL("*.html")); | 115 FILE_PATH_LITERAL("*.html")); |
| 104 | 116 |
| 105 // TODO(dtseng): Make each of these a gtest with script. | 117 // TODO(dtseng): Make each of these a gtest with script. |
| 106 FilePath html_file(file_enumerator.Next()); | 118 FilePath html_file(file_enumerator.Next()); |
| 107 ASSERT_FALSE(html_file.empty()); | 119 ASSERT_FALSE(html_file.empty()); |
| 108 do { | 120 do { |
| 109 printf("Testing %s\n", html_file.BaseName().MaybeAsASCII().c_str()); | |
| 110 | |
| 111 std::string html_contents; | 121 std::string html_contents; |
| 112 file_util::ReadFileToString(html_file, &html_contents); | 122 file_util::ReadFileToString(html_file, &html_contents); |
| 113 | 123 |
| 114 // Read the expected file. | 124 // Read the expected file. |
| 115 std::string expected_contents_raw; | 125 std::string expected_contents_raw; |
| 116 FilePath expected_file = | 126 FilePath expected_file = |
| 117 FilePath(html_file.RemoveExtension().value() + | 127 FilePath(html_file.RemoveExtension().value() + |
| 118 helper_.GetExpectedFileSuffix()); | 128 helper_.GetExpectedFileSuffix()); |
| 119 file_util::ReadFileToString( | 129 file_util::ReadFileToString( |
| 120 expected_file, | 130 expected_file, |
| 121 &expected_contents_raw); | 131 &expected_contents_raw); |
| 122 | 132 |
| 123 // Tolerate Windows-style line endings (\r\n) in the expected file: | 133 // Tolerate Windows-style line endings (\r\n) in the expected file: |
| 124 // normalize by deleting all \r from the file (if any) to leave only \n. | 134 // normalize by deleting all \r from the file (if any) to leave only \n. |
| 125 std::string expected_contents; | 135 std::string expected_contents; |
| 126 RemoveChars(expected_contents_raw, "\r", &expected_contents); | 136 RemoveChars(expected_contents_raw, "\r", &expected_contents); |
| 127 | 137 |
| 138 if (!expected_contents.compare(0, 6, "#<skip")) { | |
|
David Tseng
2012/04/02 23:29:38
Define this string literal as a constant along wit
aaronlevbugs
2012/04/03 02:30:50
Done.
| |
| 139 printf("Skipping %s\n", html_file.BaseName().MaybeAsASCII().c_str()); | |
| 140 continue; | |
| 141 } | |
| 142 | |
| 143 printf("Testing %s\n", html_file.BaseName().MaybeAsASCII().c_str()); | |
| 144 | |
| 128 // Load the page. | 145 // Load the page. |
| 129 ui_test_utils::WindowedNotificationObserver tree_updated_observer( | 146 ui_test_utils::WindowedNotificationObserver tree_updated_observer( |
| 130 content::NOTIFICATION_RENDER_VIEW_HOST_ACCESSIBILITY_TREE_UPDATED, | 147 content::NOTIFICATION_RENDER_VIEW_HOST_ACCESSIBILITY_TREE_UPDATED, |
| 131 content::NotificationService::AllSources()); | 148 content::NotificationService::AllSources()); |
| 132 string16 html_contents16; | 149 string16 html_contents16; |
| 133 html_contents16 = UTF8ToUTF16(html_contents); | 150 html_contents16 = UTF8ToUTF16(html_contents); |
| 134 GURL url(UTF8ToUTF16(kUrlPreamble) + html_contents16); | 151 GURL url(UTF8ToUTF16(kUrlPreamble) + html_contents16); |
| 135 browser()->OpenURL(OpenURLParams( | 152 browser()->OpenURL(OpenURLParams( |
| 136 url, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); | 153 url, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); |
| 137 | 154 |
| 138 // Wait for the tree. | 155 // Wait for the tree. |
| 139 tree_updated_observer.Wait(); | 156 tree_updated_observer.Wait(); |
| 140 | 157 |
| 141 // Perform a diff (or write the initial baseline). | 158 // Perform a diff (or write the initial baseline). |
| 142 string16 actual_contents_utf16; | 159 string16 actual_contents_utf16; |
| 143 helper_.DumpAccessibilityTree( | 160 helper_.DumpAccessibilityTree( |
| 144 host_view->GetBrowserAccessibilityManager()->GetRoot(), | 161 host_view->GetBrowserAccessibilityManager()->GetRoot(), |
| 145 &actual_contents_utf16); | 162 &actual_contents_utf16); |
| 146 std::string actual_contents = UTF16ToUTF8(actual_contents_utf16); | 163 std::string actual_contents = UTF16ToUTF8(actual_contents_utf16); |
| 147 EXPECT_TRUE(EqualsWithComments(expected_contents, actual_contents)); | 164 std::vector<int> diff_lines = DiffLines(expected_contents, actual_contents); |
| 148 if (expected_contents != actual_contents) { | 165 boolean is_different = diff_lines.size() > 0; |
| 166 EXPECT_FALSE(is_different); | |
| 167 if (is_different) { | |
| 149 printf("*** EXPECTED: ***\n%s\n", expected_contents.c_str()); | 168 printf("*** EXPECTED: ***\n%s\n", expected_contents.c_str()); |
| 150 printf("*** ACTUAL: ***\n%s\n", actual_contents.c_str()); | 169 printf("==> Line #s for diffs: "); |
| 170 for (int i = 0; i < static_cast<int>(diff_lines.size()); i++) | |
|
David Tseng
2012/04/02 23:29:38
nit: ++i
aaronlevbugs
2012/04/03 02:30:50
Done.
| |
| 171 printf(" %d", diff_lines[i]); | |
|
David Tseng
2012/04/02 23:29:38
Would it be useful to print the expected lines tha
aaronlevbugs
2012/04/03 02:30:50
New patch shows all expected lines and marks those
| |
| 172 printf("\n*** ACTUAL: ***\n%s\n", actual_contents.c_str()); | |
| 151 } | 173 } |
| 152 | 174 |
| 153 if (!file_util::PathExists(expected_file)) { | 175 if (!file_util::PathExists(expected_file)) { |
| 154 FilePath actual_file = | 176 FilePath actual_file = |
| 155 FilePath(html_file.RemoveExtension().value() + | 177 FilePath(html_file.RemoveExtension().value() + |
| 156 helper_.GetActualFileSuffix()); | 178 helper_.GetActualFileSuffix()); |
| 157 | 179 |
| 158 EXPECT_TRUE(file_util::WriteFile( | 180 EXPECT_TRUE(file_util::WriteFile( |
| 159 actual_file, actual_contents.c_str(), actual_contents.size())); | 181 actual_file, actual_contents.c_str(), actual_contents.size())); |
| 160 | 182 |
| 161 ADD_FAILURE() << "No expectation found. Create it by doing:\n" | 183 ADD_FAILURE() << "No expectation found. Create it by doing:\n" |
| 162 << "mv " << actual_file.LossyDisplayName() << " " | 184 << "mv " << actual_file.LossyDisplayName() << " " |
| 163 << expected_file.LossyDisplayName(); | 185 << expected_file.LossyDisplayName(); |
| 164 } | 186 } |
| 165 } while (!(html_file = file_enumerator.Next()).empty()); | 187 } while (!(html_file = file_enumerator.Next()).empty()); |
| 166 } | 188 } |
| 167 | 189 |
| OLD | NEW |