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 "content/test/layout_browsertest.h" | 5 #include "content/test/layout_browsertest.h" |
| 6 | 6 |
| 7 #include <sstream> | |
| 8 | |
| 7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 8 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 10 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 11 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 14 #include "base/run_loop.h" | |
| 12 #include "base/scoped_temp_dir.h" | 15 #include "base/scoped_temp_dir.h" |
| 13 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
| 14 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 15 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 16 #include "content/browser/web_contents/web_contents_impl.h" | 19 #include "content/browser/web_contents/web_contents_impl.h" |
| 17 #include "content/public/common/content_paths.h" | 20 #include "content/public/common/content_paths.h" |
| 18 #include "content/public/test/browser_test_utils.h" | 21 #include "content/public/test/browser_test_utils.h" |
| 19 #include "content/shell/shell.h" | 22 #include "content/shell/shell.h" |
| 20 #include "content/shell/shell_switches.h" | 23 #include "content/shell/shell_switches.h" |
| 24 #include "content/shell/webkit_test_runner_host.h" | |
| 21 #include "content/test/content_browser_test_utils.h" | 25 #include "content/test/content_browser_test_utils.h" |
| 22 #include "content/test/layout_test_http_server.h" | 26 #include "content/test/layout_test_http_server.h" |
| 23 #include "net/base/net_util.h" | 27 #include "net/base/net_util.h" |
| 24 | 28 |
| 25 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
| 26 static const char kPlatformName[] = "chromium-win"; | 30 static const char kPlatformName[] = "chromium-win"; |
| 27 #elif defined(OS_MACOSX) | 31 #elif defined(OS_MACOSX) |
| 28 static const char kPlatformName[] = "chromium-mac"; | 32 static const char kPlatformName[] = "chromium-mac"; |
| 29 #elif defined(OS_LINUX) | 33 #elif defined(OS_LINUX) |
| 30 static const char kPlatformName[] = "chromium-linux"; | 34 static const char kPlatformName[] = "chromium-linux"; |
| 31 #elif defined(OS_OPENBSD) | 35 #elif defined(OS_OPENBSD) |
| 32 static const char kPlatformName[] = "chromium-openbsd"; | 36 static const char kPlatformName[] = "chromium-openbsd"; |
| 33 #else | 37 #else |
| 34 #error No known OS defined | 38 #error No known OS defined |
| 35 #endif | 39 #endif |
| 36 | 40 |
| 37 namespace { | 41 namespace { |
| 38 | 42 |
| 39 size_t FindInsertPosition(const std::string& html) { | |
| 40 size_t tag_start = html.find("<html"); | |
| 41 if (tag_start == std::string::npos) | |
| 42 return 0; | |
| 43 size_t tag_end = html.find(">", tag_start); | |
| 44 if (tag_end == std::string::npos) | |
| 45 return 0; | |
| 46 return tag_end + 1; | |
| 47 } | |
| 48 | |
| 49 bool ReadExpectedResult(const FilePath& result_dir_path, | 43 bool ReadExpectedResult(const FilePath& result_dir_path, |
| 50 const std::string test_case_file_name, | 44 const std::string test_case_file_name, |
| 51 std::string* expected_result_value) { | 45 std::string* expected_result_value) { |
| 52 FilePath expected_result_path(result_dir_path); | 46 FilePath expected_result_path(result_dir_path); |
| 53 expected_result_path = expected_result_path.AppendASCII(test_case_file_name); | 47 expected_result_path = expected_result_path.AppendASCII(test_case_file_name); |
| 54 expected_result_path = expected_result_path.InsertBeforeExtension( | 48 expected_result_path = expected_result_path.InsertBeforeExtension( |
| 55 FILE_PATH_LITERAL("-expected")); | 49 FILE_PATH_LITERAL("-expected")); |
| 56 expected_result_path = | 50 expected_result_path = |
| 57 expected_result_path.ReplaceExtension(FILE_PATH_LITERAL("txt")); | 51 expected_result_path.ReplaceExtension(FILE_PATH_LITERAL("txt")); |
| 58 return file_util::ReadFileToString( | 52 return file_util::ReadFileToString( |
| 59 expected_result_path, expected_result_value); | 53 expected_result_path, expected_result_value); |
| 60 } | 54 } |
| 61 | 55 |
| 62 void ScrapeResultFromBrowser(content::Shell* window, std::string* actual_text) { | 56 } // namespace |
| 63 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | |
| 64 window->web_contents()->GetRenderViewHost(), | |
| 65 L"", | |
| 66 L"window.domAutomationController.send(document.body.innerText);", | |
| 67 actual_text)); | |
| 68 } | |
| 69 | |
| 70 static const std::string preamble = | |
| 71 "\n<script>\n" | |
| 72 "function TestRunner() {\n" | |
| 73 " this.wait_until_done_ = false;\n" | |
| 74 " this.dumpAsText = function () {};\n" | |
| 75 " this.waitUntilDone = function () {\n" | |
| 76 " this.wait_until_done_ = true;\n" | |
| 77 " }\n" | |
| 78 " this.notifyDone = function () {\n" | |
| 79 " document.title = 'done';\n" | |
| 80 " }\n" | |
| 81 " this.overridePreference = function () {}\n" | |
| 82 " this.OnEvent = function () {\n" | |
| 83 " if (!testRunner.wait_until_done_)\n" | |
| 84 " testRunner.notifyDone();\n" | |
| 85 " }\n" | |
| 86 " this.workerThreadCount = 0; \n" | |
| 87 "}\n" | |
| 88 "window.testRunner = new TestRunner();\n" | |
| 89 "window.addEventListener('load', testRunner.OnEvent, false);\n" | |
| 90 "</script>"; | |
| 91 | |
| 92 } | |
| 93 | 57 |
| 94 InProcessBrowserLayoutTest::InProcessBrowserLayoutTest( | 58 InProcessBrowserLayoutTest::InProcessBrowserLayoutTest( |
| 95 const FilePath& test_parent_dir, const FilePath& test_case_dir) | 59 const FilePath& test_parent_dir, const FilePath& test_case_dir) |
| 96 : test_parent_dir_(test_parent_dir), test_case_dir_(test_case_dir), | 60 : test_parent_dir_(test_parent_dir), test_case_dir_(test_case_dir), |
| 97 port_(-2) { | 61 port_(-2) { |
| 98 } | 62 } |
| 99 | 63 |
| 100 InProcessBrowserLayoutTest::InProcessBrowserLayoutTest( | 64 InProcessBrowserLayoutTest::InProcessBrowserLayoutTest( |
| 101 const FilePath& test_parent_dir, const FilePath& test_case_dir, int port) | 65 const FilePath& test_parent_dir, const FilePath& test_case_dir, int port) |
| 102 : test_parent_dir_(test_parent_dir), test_case_dir_(test_case_dir), | 66 : test_parent_dir_(test_parent_dir), test_case_dir_(test_case_dir), |
| 103 port_(port) { | 67 port_(port) { |
| 104 } | 68 } |
| 105 | 69 |
| 106 InProcessBrowserLayoutTest::~InProcessBrowserLayoutTest() { | 70 InProcessBrowserLayoutTest::~InProcessBrowserLayoutTest() { |
| 107 if (test_http_server_.get()) | 71 if (test_http_server_.get()) |
| 108 CHECK(test_http_server_->Stop()); | 72 CHECK(test_http_server_->Stop()); |
| 109 } | 73 } |
| 110 | 74 |
| 111 void InProcessBrowserLayoutTest::SetUpInProcessBrowserTestFixture() { | 75 void InProcessBrowserLayoutTest::SetUpInProcessBrowserTestFixture() { |
| 112 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); | |
| 113 FilePath src_dir; | 76 FilePath src_dir; |
| 114 ASSERT_TRUE(PathService::Get(content::DIR_LAYOUT_TESTS, &src_dir)); | 77 ASSERT_TRUE(PathService::Get(content::DIR_LAYOUT_TESTS, &src_dir)); |
| 115 our_original_layout_test_dir_ = | 78 FilePath absolute_parent_dir = src_dir.Append(test_parent_dir_); |
| 116 src_dir.Append(test_parent_dir_).Append(test_case_dir_); | 79 ASSERT_TRUE(file_util::DirectoryExists(absolute_parent_dir)); |
| 117 ASSERT_TRUE(file_util::DirectoryExists(our_original_layout_test_dir_)); | 80 layout_test_dir_ = absolute_parent_dir.Append(test_case_dir_); |
| 118 our_layout_test_temp_dir_ = scoped_temp_dir_.path() | 81 ASSERT_TRUE(file_util::DirectoryExists(layout_test_dir_)); |
| 119 .AppendASCII("LayoutTests").Append(test_parent_dir_); | |
| 120 ASSERT_TRUE(file_util::CreateDirectory( | |
| 121 our_layout_test_temp_dir_.Append(test_case_dir_))); | |
| 122 file_util::CopyDirectory( | |
| 123 our_original_layout_test_dir_.AppendASCII("resources"), | |
| 124 our_layout_test_temp_dir_.Append(test_case_dir_).AppendASCII("resources"), | |
| 125 true /*recursive*/); | |
| 126 | 82 |
| 127 // Gets the file path to rebased expected result directory for the current | 83 // Gets the file path to rebased expected result directory for the current |
| 128 // platform. | 84 // platform. |
| 129 // $LayoutTestRoot/platform/chromium_***/... | 85 // $LayoutTestRoot/platform/chromium_***/... |
| 130 rebase_result_dir_ = src_dir.AppendASCII("platform"); | 86 rebase_result_dir_ = src_dir.AppendASCII("platform"); |
| 131 rebase_result_dir_ = rebase_result_dir_.AppendASCII(kPlatformName); | 87 rebase_result_dir_ = rebase_result_dir_.AppendASCII(kPlatformName); |
| 132 rebase_result_dir_ = rebase_result_dir_.Append(test_parent_dir_); | 88 rebase_result_dir_ = rebase_result_dir_.Append(test_parent_dir_); |
| 133 rebase_result_dir_ = rebase_result_dir_.Append(test_case_dir_); | 89 rebase_result_dir_ = rebase_result_dir_.Append(test_case_dir_); |
| 134 | 90 |
| 135 // Generic chromium expected results. Not OS-specific. For example, | 91 // Generic chromium expected results. Not OS-specific. For example, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 149 Append(test_parent_dir_). | 105 Append(test_parent_dir_). |
| 150 Append(test_case_dir_); | 106 Append(test_case_dir_); |
| 151 #endif | 107 #endif |
| 152 | 108 |
| 153 if (port_ != -2) { | 109 if (port_ != -2) { |
| 154 // Layout tests expect that the server points at the | 110 // Layout tests expect that the server points at the |
| 155 // LayoutTests\http\tests directory. | 111 // LayoutTests\http\tests directory. |
| 156 if (port_ == -1) | 112 if (port_ == -1) |
| 157 port_ = base::RandInt(1024, 65535); | 113 port_ = base::RandInt(1024, 65535); |
| 158 test_http_server_.reset(new LayoutTestHttpServer( | 114 test_http_server_.reset(new LayoutTestHttpServer( |
| 159 our_layout_test_temp_dir_, port_)); | 115 absolute_parent_dir, port_)); |
| 160 ASSERT_TRUE(test_http_server_->Start()); | 116 ASSERT_TRUE(test_http_server_->Start()); |
| 161 } | 117 } |
| 162 } | 118 } |
| 163 | 119 |
| 120 void InProcessBrowserLayoutTest::SetUpCommandLine(CommandLine* command_line) { | |
| 121 command_line->AppendSwitch(switches::kDumpRenderTree); | |
| 122 } | |
| 123 | |
| 124 void InProcessBrowserLayoutTest::SetUpOnMainThread() { | |
| 125 test_controller_.reset(new content::WebKitTestController); | |
| 126 } | |
| 127 | |
| 164 void InProcessBrowserLayoutTest::RunLayoutTest( | 128 void InProcessBrowserLayoutTest::RunLayoutTest( |
| 165 const std::string& test_case_file_name) { | 129 const std::string& test_case_file_name) { |
| 166 FilePath file_path; | 130 GURL url = net::FilePathToFileURL( |
| 167 WriteModifiedFile(test_case_file_name, &file_path); | 131 layout_test_dir_.AppendASCII(test_case_file_name)); |
| 168 GURL url = net::FilePathToFileURL(file_path); | |
| 169 RunLayoutTestInternal(test_case_file_name, url); | 132 RunLayoutTestInternal(test_case_file_name, url); |
| 170 } | 133 } |
| 171 | 134 |
| 172 void InProcessBrowserLayoutTest::RunHttpLayoutTest( | 135 void InProcessBrowserLayoutTest::RunHttpLayoutTest( |
| 173 const std::string& test_case_file_name) { | 136 const std::string& test_case_file_name) { |
| 174 DCHECK(test_http_server_.get()); | 137 DCHECK(test_http_server_.get()); |
| 175 FilePath file_path; | |
| 176 WriteModifiedFile(test_case_file_name, &file_path); | |
| 177 | |
| 178 GURL url(StringPrintf( | 138 GURL url(StringPrintf( |
| 179 "http://127.0.0.1:%d/%s/%s", port_, test_case_dir_.MaybeAsASCII().c_str(), | 139 "http://127.0.0.1:%d/%s/%s", port_, test_case_dir_.MaybeAsASCII().c_str(), |
| 180 test_case_file_name.c_str())); | 140 test_case_file_name.c_str())); |
| 181 RunLayoutTestInternal(test_case_file_name, url); | 141 RunLayoutTestInternal(test_case_file_name, url); |
| 182 } | 142 } |
| 183 | 143 |
| 184 void InProcessBrowserLayoutTest::RunLayoutTestInternal( | 144 void InProcessBrowserLayoutTest::RunLayoutTestInternal( |
| 185 const std::string& test_case_file_name, const GURL& url) { | 145 const std::string& test_case_file_name, const GURL& url) { |
| 146 std::stringstream result; | |
| 147 scoped_ptr<content::WebKitTestResultPrinter> printer( | |
| 148 new content::WebKitTestResultPrinter(&result, NULL)); | |
| 149 printer->set_capture_text_only(true); | |
| 150 test_controller_->set_printer(printer.release()); | |
| 151 | |
| 186 LOG(INFO) << "Navigating to URL " << url << " and blocking."; | 152 LOG(INFO) << "Navigating to URL " << url << " and blocking."; |
| 187 const string16 expected_title = ASCIIToUTF16("done"); | 153 ASSERT_TRUE(test_controller_->PrepareForLayoutTest(url, false, "")); |
| 188 content::TitleWatcher title_watcher(shell()->web_contents(), expected_title); | 154 base::RunLoop run_loop; |
| 189 content::NavigateToURL(shell(), url); | 155 run_loop.Run(); |
| 190 LOG(INFO) << "Navigation completed, now waiting for title."; | 156 LOG(INFO) << "Navigation completed."; |
| 191 string16 final_title = title_watcher.WaitAndGetTitle(); | 157 ASSERT_TRUE(test_controller_->ResetAfterLayoutTest()); |
| 192 EXPECT_EQ(expected_title, final_title); | |
| 193 | 158 |
| 194 std::string actual_text; | 159 std::string actual_text = result.str();; |
| 195 ScrapeResultFromBrowser(shell(), &actual_text); | |
| 196 ReplaceSubstringsAfterOffset(&actual_text, 0, "\r", ""); | |
|
dgrogan
2012/09/25 18:04:31
Looks like we now need this on the expected_text
| |
| 197 TrimString(actual_text, "\n", &actual_text); | |
| 198 | 160 |
| 199 std::string expected_text; | 161 std::string expected_text; |
| 200 // Reads the expected result. First try to read from rebase directory. | 162 // Reads the expected result. First try to read from rebase directory. |
| 201 // If failed, read from original directory. | 163 // If failed, read from original directory. |
| 202 if (!ReadExpectedResult(rebase_result_dir_, | 164 if (!ReadExpectedResult(rebase_result_dir_, |
| 203 test_case_file_name, | 165 test_case_file_name, |
| 204 &expected_text) && | 166 &expected_text) && |
| 205 !ReadExpectedResult(rebase_result_chromium_dir_, | 167 !ReadExpectedResult(rebase_result_chromium_dir_, |
| 206 test_case_file_name, | 168 test_case_file_name, |
| 207 &expected_text)) { | 169 &expected_text)) { |
| 208 if (rebase_result_win_dir_.empty() || | 170 if (rebase_result_win_dir_.empty() || |
| 209 !ReadExpectedResult(rebase_result_win_dir_, | 171 !ReadExpectedResult(rebase_result_win_dir_, |
| 210 test_case_file_name, | 172 test_case_file_name, |
| 211 &expected_text)) | 173 &expected_text)) |
| 212 ReadExpectedResult(our_original_layout_test_dir_, | 174 ReadExpectedResult(layout_test_dir_, |
| 213 test_case_file_name, | 175 test_case_file_name, |
| 214 &expected_text); | 176 &expected_text); |
| 215 } | 177 } |
| 216 ASSERT_TRUE(!expected_text.empty()); | 178 ASSERT_TRUE(!expected_text.empty()); |
| 217 ReplaceSubstringsAfterOffset(&expected_text, 0, "\r", ""); | |
| 218 TrimString(expected_text, "\n", &expected_text); | |
| 219 | 179 |
| 220 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 180 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 221 if (command_line->HasSwitch(switches::kOutputLayoutTestDifferences)) { | 181 if (command_line->HasSwitch(switches::kOutputLayoutTestDifferences)) { |
| 222 EXPECT_EQ(expected_text, actual_text) << | 182 EXPECT_EQ(expected_text, actual_text) << |
| 223 SaveResults(expected_text, actual_text); | 183 SaveResults(expected_text, actual_text); |
| 224 } else { | 184 } else { |
| 225 EXPECT_EQ(expected_text, actual_text); | 185 EXPECT_EQ(expected_text, actual_text); |
| 226 } | 186 } |
| 227 } | 187 } |
| 228 | 188 |
| 229 void InProcessBrowserLayoutTest::AddResourceForLayoutTest( | |
| 230 const FilePath& parent_dir, | |
| 231 const FilePath& resource_name) { | |
| 232 FilePath source; | |
| 233 ASSERT_TRUE(PathService::Get(content::DIR_LAYOUT_TESTS, &source)); | |
| 234 source = source.Append(parent_dir); | |
| 235 source = source.Append(resource_name); | |
| 236 | |
| 237 ASSERT_TRUE(file_util::PathExists(source)); | |
| 238 | |
| 239 FilePath dest_parent_dir = scoped_temp_dir_.path(). | |
| 240 AppendASCII("LayoutTests").Append(parent_dir); | |
| 241 ASSERT_TRUE(file_util::CreateDirectory(dest_parent_dir)); | |
| 242 FilePath dest = dest_parent_dir.Append(resource_name); | |
| 243 | |
| 244 if (file_util::DirectoryExists(source)) { | |
| 245 ASSERT_TRUE(file_util::CreateDirectory(dest.DirName())); | |
| 246 ASSERT_TRUE(file_util::CopyDirectory(source, dest, true)); | |
| 247 } else { | |
| 248 ASSERT_TRUE(file_util::CopyFile(source, dest)); | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 void InProcessBrowserLayoutTest::WriteModifiedFile( | |
| 253 const std::string& test_case_file_name, FilePath* test_path) { | |
| 254 FilePath path_to_single_test = | |
| 255 our_original_layout_test_dir_.AppendASCII(test_case_file_name); | |
| 256 std::string test_html; | |
| 257 ASSERT_TRUE(file_util::ReadFileToString(path_to_single_test, &test_html)); | |
| 258 | |
| 259 size_t insertion_position = FindInsertPosition(test_html); | |
| 260 test_html.insert(insertion_position, preamble); | |
| 261 *test_path = our_layout_test_temp_dir_.Append(test_case_dir_); | |
| 262 *test_path = test_path->AppendASCII(test_case_file_name); | |
| 263 ASSERT_TRUE(file_util::WriteFile(*test_path, | |
| 264 &test_html.at(0), | |
| 265 static_cast<int>(test_html.size()))); | |
| 266 } | |
| 267 | |
| 268 std::string InProcessBrowserLayoutTest::SaveResults(const std::string& expected, | 189 std::string InProcessBrowserLayoutTest::SaveResults(const std::string& expected, |
| 269 const std::string& actual) { | 190 const std::string& actual) { |
| 270 FilePath cwd; | 191 FilePath cwd; |
| 271 EXPECT_TRUE(file_util::CreateNewTempDirectory(FILE_PATH_LITERAL(""), &cwd)); | 192 EXPECT_TRUE(file_util::CreateNewTempDirectory(FILE_PATH_LITERAL(""), &cwd)); |
| 272 FilePath expected_filename = cwd.Append(FILE_PATH_LITERAL("expected.txt")); | 193 FilePath expected_filename = cwd.Append(FILE_PATH_LITERAL("expected.txt")); |
| 273 FilePath actual_filename = cwd.Append(FILE_PATH_LITERAL("actual.txt")); | 194 FilePath actual_filename = cwd.Append(FILE_PATH_LITERAL("actual.txt")); |
| 274 EXPECT_NE(-1, file_util::WriteFile(expected_filename, | 195 EXPECT_NE(-1, file_util::WriteFile(expected_filename, |
| 275 expected.c_str(), | 196 expected.c_str(), |
| 276 expected.size())); | 197 expected.size())); |
| 277 EXPECT_NE(-1, file_util::WriteFile(actual_filename, | 198 EXPECT_NE(-1, file_util::WriteFile(actual_filename, |
| 278 actual.c_str(), | 199 actual.c_str(), |
| 279 actual.size())); | 200 actual.size())); |
| 280 return StringPrintf("Wrote %"PRFilePath" %"PRFilePath, | 201 return StringPrintf("Wrote %"PRFilePath" %"PRFilePath, |
| 281 expected_filename.value().c_str(), | 202 expected_filename.value().c_str(), |
| 282 actual_filename.value().c_str()); | 203 actual_filename.value().c_str()); |
| 283 } | 204 } |
| OLD | NEW |