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 "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/rand_util.h" |
10 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
| 12 #include "base/stringprintf.h" |
11 #include "base/string_util.h" | 13 #include "base/string_util.h" |
12 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
| 17 #include "chrome/test/base/layout_test_http_server.h" |
15 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
16 #include "content/browser/tab_contents/tab_contents.h" | 19 #include "content/browser/tab_contents/tab_contents.h" |
17 #include "content/public/common/content_paths.h" | 20 #include "content/public/common/content_paths.h" |
18 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
19 | 22 |
| 23 #if defined(OS_WIN) |
| 24 static const char kPlatformName[] = "chromium-win"; |
| 25 #elif defined(OS_MACOSX) |
| 26 static const char kPlatformName[] = "chromium-mac"; |
| 27 #elif defined(OS_LINUX) |
| 28 static const char kPlatformName[] = "chromium-linux"; |
| 29 #elif defined(OS_OPENBSD) |
| 30 static const char kPlatformName[] = "chromium-openbsd"; |
| 31 #else |
| 32 #error No known OS defined |
| 33 #endif |
| 34 |
20 namespace { | 35 namespace { |
21 | 36 |
22 size_t FindInsertPosition(const std::string& html) { | 37 size_t FindInsertPosition(const std::string& html) { |
23 size_t tag_start = html.find("<html"); | 38 size_t tag_start = html.find("<html"); |
24 if (tag_start == std::string::npos) | 39 if (tag_start == std::string::npos) |
25 return 0; | 40 return 0; |
26 size_t tag_end = html.find(">", tag_start); | 41 size_t tag_end = html.find(">", tag_start); |
27 if (tag_end == std::string::npos) | 42 if (tag_end == std::string::npos) |
28 return 0; | 43 return 0; |
29 return tag_end + 1; | 44 return tag_end + 1; |
30 } | 45 } |
31 | 46 |
32 void ReadExpectedResult(const FilePath& result_dir_path, | 47 bool ReadExpectedResult(const FilePath& result_dir_path, |
33 const std::string test_case_file_name, | 48 const std::string test_case_file_name, |
34 std::string* expected_result_value) { | 49 std::string* expected_result_value) { |
35 FilePath expected_result_path(result_dir_path); | 50 FilePath expected_result_path(result_dir_path); |
36 expected_result_path = expected_result_path.AppendASCII(test_case_file_name); | 51 expected_result_path = expected_result_path.AppendASCII(test_case_file_name); |
37 expected_result_path = expected_result_path.InsertBeforeExtension( | 52 expected_result_path = expected_result_path.InsertBeforeExtension( |
38 FILE_PATH_LITERAL("-expected")); | 53 FILE_PATH_LITERAL("-expected")); |
39 expected_result_path = | 54 expected_result_path = |
40 expected_result_path.ReplaceExtension(FILE_PATH_LITERAL("txt")); | 55 expected_result_path.ReplaceExtension(FILE_PATH_LITERAL("txt")); |
41 std::string raw_result; | 56 return file_util::ReadFileToString( |
42 EXPECT_TRUE(file_util::ReadFileToString(expected_result_path, &raw_result)); | 57 expected_result_path, expected_result_value); |
43 TrimString(raw_result, "\n", expected_result_value); | |
44 } | 58 } |
45 | 59 |
46 void ScrapeResultFromBrowser(Browser* browser, std::string* actual_text) { | 60 void ScrapeResultFromBrowser(Browser* browser, std::string* actual_text) { |
47 DOMElementProxyRef doc = ui_test_utils::GetActiveDOMDocument(browser); | 61 DOMElementProxyRef doc = ui_test_utils::GetActiveDOMDocument(browser); |
48 DOMElementProxyRef body = | 62 DOMElementProxyRef body = |
49 doc->FindElement(DOMElementProxy::By::XPath("//body")); | 63 doc->FindElement(DOMElementProxy::By::XPath("//body")); |
50 ASSERT_TRUE(body.get()); | 64 ASSERT_TRUE(body.get()); |
51 ASSERT_TRUE(body->GetInnerText(actual_text)); | 65 ASSERT_TRUE(body->GetInnerText(actual_text)); |
52 } | 66 } |
53 | 67 |
54 static const std::string preamble = | 68 static const std::string preamble = |
55 "\n<script>\n" | 69 "\n<script>\n" |
56 "function LayoutTestController() {\n" | 70 "function LayoutTestController() {\n" |
57 " this.dumpAsText = function () {};\n" | 71 " this.dumpAsText = function () {};\n" |
58 " this.waitUntilDone = function () {};\n" | 72 " this.waitUntilDone = function () {};\n" |
59 " this.notifyDone = function () {\n" | 73 " this.notifyDone = function () {\n" |
60 " document.title = 'done';\n" | 74 " document.title = 'done';\n" |
61 " }\n" | 75 " }\n" |
| 76 " this.overridePreference = function () {}\n" |
| 77 " this.workerThreadCount = 0; \n" |
62 "}\n" | 78 "}\n" |
63 "window.layoutTestController = new LayoutTestController();\n" | 79 "window.layoutTestController = new LayoutTestController();\n" |
64 "</script>"; | 80 "</script>"; |
65 | 81 |
66 } | 82 } |
67 | 83 |
68 InProcessBrowserLayoutTest::InProcessBrowserLayoutTest( | 84 InProcessBrowserLayoutTest::InProcessBrowserLayoutTest( |
69 const FilePath relative_layout_test_path) | 85 const FilePath& test_parent_dir, const FilePath& test_case_dir) |
70 : original_relative_path_(relative_layout_test_path) { | 86 : test_parent_dir_(test_parent_dir), test_case_dir_(test_case_dir), |
| 87 port_(-2) { |
71 EnableDOMAutomation(); | 88 EnableDOMAutomation(); |
72 } | 89 } |
73 | 90 |
74 InProcessBrowserLayoutTest::~InProcessBrowserLayoutTest() {} | 91 InProcessBrowserLayoutTest::InProcessBrowserLayoutTest( |
| 92 const FilePath& test_parent_dir, const FilePath& test_case_dir, int port) |
| 93 : test_parent_dir_(test_parent_dir), test_case_dir_(test_case_dir), |
| 94 port_(port) { |
| 95 EnableDOMAutomation(); |
| 96 } |
| 97 |
| 98 InProcessBrowserLayoutTest::~InProcessBrowserLayoutTest() { |
| 99 if (test_http_server_.get()) |
| 100 CHECK(test_http_server_->Stop()); |
| 101 } |
75 | 102 |
76 void InProcessBrowserLayoutTest::SetUpInProcessBrowserTestFixture() { | 103 void InProcessBrowserLayoutTest::SetUpInProcessBrowserTestFixture() { |
77 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); | 104 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
78 FilePath ThirdParty_WebKit_LayoutTests; | 105 FilePath src_dir; |
79 ASSERT_TRUE(PathService::Get(content::DIR_LAYOUT_TESTS, | 106 ASSERT_TRUE(PathService::Get(content::DIR_LAYOUT_TESTS, &src_dir)); |
80 &ThirdParty_WebKit_LayoutTests)); | |
81 our_original_layout_test_dir_ = | 107 our_original_layout_test_dir_ = |
82 ThirdParty_WebKit_LayoutTests.Append(original_relative_path_); | 108 src_dir.Append(test_parent_dir_).Append(test_case_dir_); |
83 ASSERT_TRUE(file_util::DirectoryExists(our_original_layout_test_dir_)); | 109 ASSERT_TRUE(file_util::DirectoryExists(our_original_layout_test_dir_)); |
84 our_layout_test_temp_dir_ = scoped_temp_dir_.path() | 110 our_layout_test_temp_dir_ = scoped_temp_dir_.path() |
85 .AppendASCII("LayoutTests").Append(original_relative_path_); | 111 .AppendASCII("LayoutTests").Append(test_parent_dir_); |
86 ASSERT_TRUE(file_util::CreateDirectory(our_layout_test_temp_dir_)); | 112 ASSERT_TRUE(file_util::CreateDirectory( |
| 113 our_layout_test_temp_dir_.Append(test_case_dir_))); |
87 file_util::CopyDirectory( | 114 file_util::CopyDirectory( |
88 our_original_layout_test_dir_.AppendASCII("resources"), | 115 our_original_layout_test_dir_.AppendASCII("resources"), |
89 our_layout_test_temp_dir_.AppendASCII("resources"), | 116 our_layout_test_temp_dir_.Append(test_case_dir_).AppendASCII("resources"), |
90 true /*recursive*/); | 117 true /*recursive*/); |
| 118 |
| 119 // Gets the file path to rebased expected result directory for the current |
| 120 // platform. |
| 121 // $LayoutTestRoot/platform/chromium_***/... |
| 122 rebase_result_dir_ = src_dir.AppendASCII("platform"); |
| 123 rebase_result_dir_ = rebase_result_dir_.AppendASCII(kPlatformName); |
| 124 rebase_result_dir_ = rebase_result_dir_.Append(test_parent_dir_); |
| 125 rebase_result_dir_ = rebase_result_dir_.Append(test_case_dir_); |
| 126 |
| 127 // Generic chromium expected results. Not OS-specific. For example, |
| 128 // v8-specific differences go here. |
| 129 // chrome/test/data/layout_tests/LayoutTests/platform/chromium/... |
| 130 rebase_result_chromium_dir_ = src_dir.AppendASCII("platform"). |
| 131 AppendASCII("chromium"). |
| 132 Append(test_parent_dir_). |
| 133 Append(test_case_dir_); |
| 134 |
| 135 // Gets the file path to rebased expected result directory under the |
| 136 // win32 platform. This is used by other non-win32 platform to use the same |
| 137 // rebased expected results. |
| 138 #if !defined(OS_WIN) |
| 139 rebase_result_win_dir_ = src_dir.AppendASCII("platform"). |
| 140 AppendASCII("chromium-win"). |
| 141 Append(test_parent_dir_). |
| 142 Append(test_case_dir_); |
| 143 #endif |
| 144 |
| 145 if (port_ != -2) { |
| 146 // Layout tests expect that the server points at the |
| 147 // LayoutTests\http\tests directory. |
| 148 if (port_ == -1) |
| 149 port_ = base::RandInt(1024, 65535); |
| 150 test_http_server_.reset(new LayoutTestHttpServer( |
| 151 our_layout_test_temp_dir_, port_)); |
| 152 ASSERT_TRUE(test_http_server_->Start()); |
| 153 } |
91 } | 154 } |
92 | 155 |
93 void InProcessBrowserLayoutTest::RunLayoutTest( | 156 void InProcessBrowserLayoutTest::RunLayoutTest( |
94 const std::string& test_case_file_name) { | 157 const std::string& test_case_file_name) { |
95 GURL test_url; | 158 FilePath file_path; |
96 WriteModifiedFile(test_case_file_name, &test_url); | 159 WriteModifiedFile(test_case_file_name, &file_path); |
| 160 GURL url = net::FilePathToFileURL(file_path); |
| 161 RunLayoutTestInternal(test_case_file_name, url); |
| 162 } |
97 | 163 |
98 LOG(INFO) << "Navigating to URL " << test_url << " and blocking."; | 164 void InProcessBrowserLayoutTest::RunHttpLayoutTest( |
| 165 const std::string& test_case_file_name) { |
| 166 DCHECK(test_http_server_.get()); |
| 167 FilePath file_path; |
| 168 WriteModifiedFile(test_case_file_name, &file_path); |
| 169 |
| 170 GURL url(StringPrintf( |
| 171 "http://127.0.0.1:%d/%s/%s", port_, test_case_dir_.MaybeAsASCII().c_str(), |
| 172 test_case_file_name.c_str())); |
| 173 RunLayoutTestInternal(test_case_file_name, url); |
| 174 } |
| 175 |
| 176 void InProcessBrowserLayoutTest::RunLayoutTestInternal( |
| 177 const std::string& test_case_file_name, const GURL& url) { |
| 178 LOG(INFO) << "Navigating to URL " << url << " and blocking."; |
99 const string16 expected_title = ASCIIToUTF16("done"); | 179 const string16 expected_title = ASCIIToUTF16("done"); |
100 ui_test_utils::TitleWatcher title_watcher( | 180 ui_test_utils::TitleWatcher title_watcher( |
101 browser()->GetSelectedWebContents(), expected_title); | 181 browser()->GetSelectedWebContents(), expected_title); |
102 ui_test_utils::NavigateToURL(browser(), test_url); | 182 ui_test_utils::NavigateToURL(browser(), url); |
103 string16 final_title = title_watcher.WaitAndGetTitle(); | 183 string16 final_title = title_watcher.WaitAndGetTitle(); |
104 EXPECT_EQ(expected_title, final_title); | 184 EXPECT_EQ(expected_title, final_title); |
105 | 185 |
106 std::string actual_text; | 186 std::string actual_text; |
107 ScrapeResultFromBrowser(browser(), &actual_text); | 187 ScrapeResultFromBrowser(browser(), &actual_text); |
| 188 ReplaceSubstringsAfterOffset(&actual_text, 0, "\r", ""); |
| 189 TrimString(actual_text, "\n", &actual_text); |
108 | 190 |
109 std::string expected_text; | 191 std::string expected_text; |
110 ReadExpectedResult(our_original_layout_test_dir_, test_case_file_name, | 192 // Reads the expected result. First try to read from rebase directory. |
111 &expected_text); | 193 // If failed, read from original directory. |
| 194 if (!ReadExpectedResult(rebase_result_dir_, |
| 195 test_case_file_name, |
| 196 &expected_text) && |
| 197 !ReadExpectedResult(rebase_result_chromium_dir_, |
| 198 test_case_file_name, |
| 199 &expected_text)) { |
| 200 if (rebase_result_win_dir_.empty() || |
| 201 !ReadExpectedResult(rebase_result_win_dir_, |
| 202 test_case_file_name, |
| 203 &expected_text)) |
| 204 ReadExpectedResult(our_original_layout_test_dir_, |
| 205 test_case_file_name, |
| 206 &expected_text); |
| 207 } |
| 208 ASSERT_TRUE(!expected_text.empty()); |
| 209 ReplaceSubstringsAfterOffset(&expected_text, 0, "\r", ""); |
| 210 TrimString(expected_text, "\n", &expected_text); |
112 | 211 |
113 EXPECT_EQ(expected_text, actual_text); | 212 EXPECT_EQ(expected_text, actual_text); |
114 } | 213 } |
115 | 214 |
116 void InProcessBrowserLayoutTest::AddResourceForLayoutTest( | 215 void InProcessBrowserLayoutTest::AddResourceForLayoutTest( |
117 const FilePath& parent_dir, | 216 const FilePath& parent_dir, |
118 const FilePath& resource_name) { | 217 const FilePath& resource_name) { |
119 FilePath source; | 218 FilePath source; |
120 ASSERT_TRUE(PathService::Get(content::DIR_LAYOUT_TESTS, &source)); | 219 ASSERT_TRUE(PathService::Get(content::DIR_LAYOUT_TESTS, &source)); |
121 source = source.Append(parent_dir); | 220 source = source.Append(parent_dir); |
122 source = source.Append(resource_name); | 221 source = source.Append(resource_name); |
123 | 222 |
124 ASSERT_TRUE(file_util::PathExists(source)); | 223 ASSERT_TRUE(file_util::PathExists(source)); |
125 | 224 |
126 FilePath dest_parent_dir = scoped_temp_dir_.path(). | 225 FilePath dest_parent_dir = scoped_temp_dir_.path(). |
127 AppendASCII("LayoutTests").Append(parent_dir); | 226 AppendASCII("LayoutTests").Append(parent_dir); |
128 ASSERT_TRUE(file_util::CreateDirectory(dest_parent_dir)); | 227 ASSERT_TRUE(file_util::CreateDirectory(dest_parent_dir)); |
129 FilePath dest = dest_parent_dir.Append(resource_name); | 228 FilePath dest = dest_parent_dir.Append(resource_name); |
130 | 229 |
131 if (file_util::DirectoryExists(source)) { | 230 if (file_util::DirectoryExists(source)) { |
| 231 ASSERT_TRUE(file_util::CreateDirectory(dest.DirName())); |
132 ASSERT_TRUE(file_util::CopyDirectory(source, dest, true)); | 232 ASSERT_TRUE(file_util::CopyDirectory(source, dest, true)); |
133 } else { | 233 } else { |
134 ASSERT_TRUE(file_util::CopyFile(source, dest)); | 234 ASSERT_TRUE(file_util::CopyFile(source, dest)); |
135 } | 235 } |
136 } | 236 } |
137 | 237 |
138 void InProcessBrowserLayoutTest::WriteModifiedFile( | 238 void InProcessBrowserLayoutTest::WriteModifiedFile( |
139 const std::string& test_case_file_name, GURL* test_url) { | 239 const std::string& test_case_file_name, FilePath* test_path) { |
140 FilePath path_to_single_test = | 240 FilePath path_to_single_test = |
141 our_original_layout_test_dir_.AppendASCII(test_case_file_name); | 241 our_original_layout_test_dir_.AppendASCII(test_case_file_name); |
142 std::string test_html; | 242 std::string test_html; |
143 ASSERT_TRUE(file_util::ReadFileToString(path_to_single_test, &test_html)); | 243 ASSERT_TRUE(file_util::ReadFileToString(path_to_single_test, &test_html)); |
144 | 244 |
145 size_t insertion_position = FindInsertPosition(test_html); | 245 size_t insertion_position = FindInsertPosition(test_html); |
146 test_html.insert(insertion_position, preamble); | 246 test_html.insert(insertion_position, preamble); |
147 FilePath new_test_file_path(our_layout_test_temp_dir_); | 247 *test_path = our_layout_test_temp_dir_.Append(test_case_dir_); |
148 new_test_file_path = new_test_file_path.AppendASCII(test_case_file_name); | 248 *test_path = test_path->AppendASCII(test_case_file_name); |
149 ASSERT_TRUE(file_util::WriteFile(new_test_file_path, | 249 ASSERT_TRUE(file_util::WriteFile(*test_path, |
150 &test_html.at(0), | 250 &test_html.at(0), |
151 static_cast<int>(test_html.size()))); | 251 static_cast<int>(test_html.size()))); |
152 *test_url = net::FilePathToFileURL(new_test_file_path); | |
153 } | 252 } |
OLD | NEW |