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(our_layout_test_temp_dir_)); |
113 ASSERT_TRUE(file_util::CreateDirectory( | |
dgrogan
2012/03/28 22:08:13
I think you can coalesce the CreateDirectory lines
jam
2012/03/28 23:01:27
Done.
| |
114 our_layout_test_temp_dir_.Append(test_case_dir_))); | |
87 file_util::CopyDirectory( | 115 file_util::CopyDirectory( |
88 our_original_layout_test_dir_.AppendASCII("resources"), | 116 our_original_layout_test_dir_.AppendASCII("resources"), |
89 our_layout_test_temp_dir_.AppendASCII("resources"), | 117 our_layout_test_temp_dir_.Append(test_case_dir_).AppendASCII("resources"), |
90 true /*recursive*/); | 118 true /*recursive*/); |
119 | |
120 // Gets the file path to rebased expected result directory for the current | |
121 // platform. | |
122 // $LayoutTestRoot/platform/chromium_***/... | |
123 rebase_result_dir_ = src_dir.AppendASCII("platform"); | |
124 rebase_result_dir_ = rebase_result_dir_.AppendASCII(kPlatformName); | |
125 rebase_result_dir_ = rebase_result_dir_.Append(test_parent_dir_); | |
126 rebase_result_dir_ = rebase_result_dir_.Append(test_case_dir_); | |
127 | |
128 // Generic chromium expected results. Not OS-specific. For example, | |
129 // v8-specific differences go here. | |
130 // chrome/test/data/layout_tests/LayoutTests/platform/chromium/... | |
131 rebase_result_chromium_dir_ = src_dir.AppendASCII("platform"). | |
132 AppendASCII("chromium"). | |
133 Append(test_parent_dir_). | |
134 Append(test_case_dir_); | |
135 | |
136 // Gets the file path to rebased expected result directory under the | |
137 // win32 platform. This is used by other non-win32 platform to use the same | |
138 // rebased expected results. | |
139 #if !defined(OS_WIN) | |
140 rebase_result_win_dir_ = src_dir.AppendASCII("platform"). | |
141 AppendASCII("chromium-win"). | |
142 Append(test_parent_dir_). | |
143 Append(test_case_dir_); | |
144 #endif | |
145 | |
146 if (port_ != -2) { | |
147 // Layout tests expect that the server points at the | |
148 // LayoutTests\http\tests directory. | |
149 if (port_ == -1) | |
150 port_ = base::RandInt(1024, 65535); | |
151 test_http_server_.reset(new LayoutTestHttpServer( | |
152 our_layout_test_temp_dir_, port_)); | |
153 ASSERT_TRUE(test_http_server_->Start()); | |
154 } | |
91 } | 155 } |
92 | 156 |
93 void InProcessBrowserLayoutTest::RunLayoutTest( | 157 void InProcessBrowserLayoutTest::RunLayoutTest( |
94 const std::string& test_case_file_name) { | 158 const std::string& test_case_file_name) { |
95 GURL test_url; | 159 FilePath file_path; |
96 WriteModifiedFile(test_case_file_name, &test_url); | 160 WriteModifiedFile(test_case_file_name, &file_path); |
161 GURL url = net::FilePathToFileURL(file_path); | |
162 RunLayoutTestInternal(test_case_file_name, url); | |
163 } | |
97 | 164 |
98 LOG(INFO) << "Navigating to URL " << test_url << " and blocking."; | 165 void InProcessBrowserLayoutTest::RunHttpLayoutTest( |
166 const std::string& test_case_file_name) { | |
167 DCHECK(test_http_server_.get()); | |
168 FilePath file_path; | |
169 WriteModifiedFile(test_case_file_name, &file_path); | |
170 | |
171 GURL url(StringPrintf( | |
172 "http://127.0.0.1:%d/%s/%s", port_, test_case_dir_.MaybeAsASCII().c_str(), | |
173 test_case_file_name.c_str())); | |
174 RunLayoutTestInternal(test_case_file_name, url); | |
175 } | |
176 | |
177 void InProcessBrowserLayoutTest::RunLayoutTestInternal( | |
178 const std::string& test_case_file_name, const GURL& url) { | |
179 LOG(INFO) << "Navigating to URL " << url << " and blocking."; | |
99 const string16 expected_title = ASCIIToUTF16("done"); | 180 const string16 expected_title = ASCIIToUTF16("done"); |
100 ui_test_utils::TitleWatcher title_watcher( | 181 ui_test_utils::TitleWatcher title_watcher( |
101 browser()->GetSelectedWebContents(), expected_title); | 182 browser()->GetSelectedWebContents(), expected_title); |
102 ui_test_utils::NavigateToURL(browser(), test_url); | 183 ui_test_utils::NavigateToURL(browser(), url); |
103 string16 final_title = title_watcher.WaitAndGetTitle(); | 184 string16 final_title = title_watcher.WaitAndGetTitle(); |
104 EXPECT_EQ(expected_title, final_title); | 185 EXPECT_EQ(expected_title, final_title); |
105 | 186 |
106 std::string actual_text; | 187 std::string actual_text; |
107 ScrapeResultFromBrowser(browser(), &actual_text); | 188 ScrapeResultFromBrowser(browser(), &actual_text); |
189 ReplaceSubstringsAfterOffset(&actual_text, 0, "\r", ""); | |
190 TrimString(actual_text, "\n", &actual_text); | |
108 | 191 |
109 std::string expected_text; | 192 std::string expected_text; |
110 ReadExpectedResult(our_original_layout_test_dir_, test_case_file_name, | 193 // Reads the expected result. First try to read from rebase directory. |
111 &expected_text); | 194 // If failed, read from original directory. |
195 if (!ReadExpectedResult(rebase_result_dir_, | |
196 test_case_file_name, | |
197 &expected_text) && | |
198 !ReadExpectedResult(rebase_result_chromium_dir_, | |
199 test_case_file_name, | |
200 &expected_text)) { | |
201 if (rebase_result_win_dir_.empty() || | |
202 !ReadExpectedResult(rebase_result_win_dir_, | |
203 test_case_file_name, | |
204 &expected_text)) | |
205 ReadExpectedResult(our_original_layout_test_dir_, | |
206 test_case_file_name, | |
207 &expected_text); | |
208 } | |
209 ASSERT_TRUE(!expected_text.empty()); | |
210 ReplaceSubstringsAfterOffset(&expected_text, 0, "\r", ""); | |
211 TrimString(expected_text, "\n", &expected_text); | |
112 | 212 |
113 EXPECT_EQ(expected_text, actual_text); | 213 EXPECT_EQ(expected_text, actual_text); |
114 } | 214 } |
115 | 215 |
116 void InProcessBrowserLayoutTest::AddResourceForLayoutTest( | 216 void InProcessBrowserLayoutTest::AddResourceForLayoutTest( |
117 const FilePath& parent_dir, | 217 const FilePath& parent_dir, |
118 const FilePath& resource_name) { | 218 const FilePath& resource_name) { |
119 FilePath source; | 219 FilePath source; |
120 ASSERT_TRUE(PathService::Get(content::DIR_LAYOUT_TESTS, &source)); | 220 ASSERT_TRUE(PathService::Get(content::DIR_LAYOUT_TESTS, &source)); |
121 source = source.Append(parent_dir); | 221 source = source.Append(parent_dir); |
122 source = source.Append(resource_name); | 222 source = source.Append(resource_name); |
123 | 223 |
124 ASSERT_TRUE(file_util::PathExists(source)); | 224 ASSERT_TRUE(file_util::PathExists(source)); |
125 | 225 |
126 FilePath dest_parent_dir = scoped_temp_dir_.path(). | 226 FilePath dest_parent_dir = scoped_temp_dir_.path(). |
127 AppendASCII("LayoutTests").Append(parent_dir); | 227 AppendASCII("LayoutTests").Append(parent_dir); |
128 ASSERT_TRUE(file_util::CreateDirectory(dest_parent_dir)); | 228 ASSERT_TRUE(file_util::CreateDirectory(dest_parent_dir)); |
129 FilePath dest = dest_parent_dir.Append(resource_name); | 229 FilePath dest = dest_parent_dir.Append(resource_name); |
130 | 230 |
131 if (file_util::DirectoryExists(source)) { | 231 if (file_util::DirectoryExists(source)) { |
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 |