OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/test/layout_browsertest.h" | |
6 | |
7 #include <sstream> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "base/file_util.h" | |
11 #include "base/files/file_path.h" | |
12 #include "base/files/scoped_temp_dir.h" | |
13 #include "base/path_service.h" | |
14 #include "base/rand_util.h" | |
15 #include "base/run_loop.h" | |
16 #include "base/string_util.h" | |
17 #include "base/stringprintf.h" | |
18 #include "base/utf_string_conversions.h" | |
19 #include "content/browser/web_contents/web_contents_impl.h" | |
20 #include "content/public/common/content_paths.h" | |
21 #include "content/public/test/browser_test_utils.h" | |
22 #include "content/public/test/layouttest_support.h" | |
23 #include "content/shell/common/shell_switches.h" | |
24 #include "content/shell/shell.h" | |
25 #include "content/shell/webkit_test_controller.h" | |
26 #include "content/test/content_browser_test_utils.h" | |
27 #include "content/test/layout_test_http_server.h" | |
28 #include "net/base/net_util.h" | |
29 | |
30 #if defined(OS_WIN) | |
31 static const char kPlatformName[] = "chromium-win"; | |
32 #elif defined(OS_MACOSX) | |
33 static const char kPlatformName[] = "chromium-mac"; | |
34 #elif defined(OS_LINUX) | |
35 static const char kPlatformName[] = "chromium-linux"; | |
36 #elif defined(OS_OPENBSD) | |
37 static const char kPlatformName[] = "chromium-openbsd"; | |
38 #elif defined(OS_ANDROID) | |
39 static const char kPlatformName[] = "chromium-android"; | |
40 #else | |
41 #error No known OS defined | |
42 #endif | |
43 | |
44 namespace content { | |
45 namespace { | |
46 | |
47 bool ReadExpectedResult(const base::FilePath& result_dir_path, | |
48 const std::string test_case_file_name, | |
49 std::string* expected_result_value) { | |
50 base::FilePath expected_result_path(result_dir_path); | |
51 expected_result_path = expected_result_path.AppendASCII(test_case_file_name); | |
52 expected_result_path = expected_result_path.InsertBeforeExtension( | |
53 FILE_PATH_LITERAL("-expected")); | |
54 expected_result_path = | |
55 expected_result_path.ReplaceExtension(FILE_PATH_LITERAL("txt")); | |
56 return file_util::ReadFileToString( | |
57 expected_result_path, expected_result_value); | |
58 } | |
59 | |
60 } // namespace | |
61 | |
62 InProcessBrowserLayoutTest::InProcessBrowserLayoutTest( | |
63 const base::FilePath& test_parent_dir, const base::FilePath& test_case_dir) | |
64 : test_parent_dir_(test_parent_dir), test_case_dir_(test_case_dir), | |
65 port_(-2) { | |
66 } | |
67 | |
68 InProcessBrowserLayoutTest::InProcessBrowserLayoutTest( | |
69 const base::FilePath& test_parent_dir, | |
70 const base::FilePath& test_case_dir, | |
71 int port) | |
72 : test_parent_dir_(test_parent_dir), test_case_dir_(test_case_dir), | |
73 port_(port) { | |
74 } | |
75 | |
76 InProcessBrowserLayoutTest::~InProcessBrowserLayoutTest() { | |
77 if (test_http_server_) | |
78 CHECK(test_http_server_->Stop()); | |
79 } | |
80 | |
81 void InProcessBrowserLayoutTest::SetUpInProcessBrowserTestFixture() { | |
82 EnableBrowserLayoutTestMode(); | |
83 | |
84 base::FilePath src_dir; | |
85 ASSERT_TRUE(PathService::Get(DIR_LAYOUT_TESTS, &src_dir)); | |
86 base::FilePath absolute_parent_dir = src_dir.Append(test_parent_dir_); | |
87 ASSERT_TRUE(file_util::DirectoryExists(absolute_parent_dir)); | |
88 layout_test_dir_ = absolute_parent_dir.Append(test_case_dir_); | |
89 ASSERT_TRUE(file_util::DirectoryExists(layout_test_dir_)); | |
90 | |
91 // Gets the file path to rebased expected result directory for the current | |
92 // platform. | |
93 // $LayoutTestRoot/platform/chromium_***/... | |
94 rebase_result_dir_ = src_dir.AppendASCII("platform"); | |
95 rebase_result_dir_ = rebase_result_dir_.AppendASCII(kPlatformName); | |
96 rebase_result_dir_ = rebase_result_dir_.Append(test_parent_dir_); | |
97 rebase_result_dir_ = rebase_result_dir_.Append(test_case_dir_); | |
98 | |
99 // Generic chromium expected results. Not OS-specific. For example, | |
100 // v8-specific differences go here. | |
101 // chrome/test/data/layout_tests/LayoutTests/platform/chromium/... | |
102 rebase_result_chromium_dir_ = src_dir.AppendASCII("platform"). | |
103 AppendASCII("chromium"). | |
104 Append(test_parent_dir_). | |
105 Append(test_case_dir_); | |
106 | |
107 // Gets the file path to rebased expected result directory under the | |
108 // win32 platform. This is used by other non-win32 platform to use the same | |
109 // rebased expected results. | |
110 #if !defined(OS_WIN) | |
111 rebase_result_win_dir_ = src_dir.AppendASCII("platform"). | |
112 AppendASCII("chromium-win"). | |
113 Append(test_parent_dir_). | |
114 Append(test_case_dir_); | |
115 #endif | |
116 | |
117 if (port_ != -2) { | |
118 // Layout tests expect that the server points at the | |
119 // LayoutTests\http\tests directory. | |
120 if (port_ == -1) | |
121 port_ = base::RandInt(1024, 65535); | |
122 test_http_server_.reset(new LayoutTestHttpServer( | |
123 absolute_parent_dir, port_)); | |
124 ASSERT_TRUE(test_http_server_->Start()); | |
125 } | |
126 } | |
127 | |
128 void InProcessBrowserLayoutTest::SetUpCommandLine(CommandLine* command_line) { | |
129 command_line->AppendSwitch(switches::kDumpRenderTree); | |
130 } | |
131 | |
132 void InProcessBrowserLayoutTest::SetUpOnMainThread() { | |
133 test_controller_.reset(new WebKitTestController); | |
134 } | |
135 | |
136 void InProcessBrowserLayoutTest::RunLayoutTest( | |
137 const std::string& test_case_file_name) { | |
138 GURL url = net::FilePathToFileURL( | |
139 layout_test_dir_.AppendASCII(test_case_file_name)); | |
140 RunLayoutTestInternal(test_case_file_name, url); | |
141 } | |
142 | |
143 void InProcessBrowserLayoutTest::RunHttpLayoutTest( | |
144 const std::string& test_case_file_name) { | |
145 DCHECK(test_http_server_.get()); | |
146 GURL url(base::StringPrintf( | |
147 "http://127.0.0.1:%d/%s/%s", port_, test_case_dir_.MaybeAsASCII().c_str(), | |
148 test_case_file_name.c_str())); | |
149 RunLayoutTestInternal(test_case_file_name, url); | |
150 } | |
151 | |
152 void InProcessBrowserLayoutTest::RunLayoutTestInternal( | |
153 const std::string& test_case_file_name, const GURL& url) { | |
154 std::stringstream result; | |
155 scoped_ptr<WebKitTestResultPrinter> printer( | |
156 new WebKitTestResultPrinter(&result, NULL)); | |
157 printer->set_capture_text_only(true); | |
158 test_controller_->set_printer(printer.release()); | |
159 | |
160 LOG(INFO) << "Navigating to URL " << url << " and blocking."; | |
161 ASSERT_TRUE(test_controller_->PrepareForLayoutTest( | |
162 url, layout_test_dir_, false, std::string())); | |
163 base::RunLoop run_loop; | |
164 run_loop.Run(); | |
165 LOG(INFO) << "Navigation completed."; | |
166 ASSERT_TRUE(test_controller_->ResetAfterLayoutTest()); | |
167 | |
168 std::string actual_text = result.str();; | |
169 | |
170 std::string expected_text; | |
171 // Reads the expected result. First try to read from rebase directory. | |
172 // If failed, read from original directory. | |
173 if (!ReadExpectedResult(rebase_result_dir_, | |
174 test_case_file_name, | |
175 &expected_text) && | |
176 !ReadExpectedResult(rebase_result_chromium_dir_, | |
177 test_case_file_name, | |
178 &expected_text)) { | |
179 if (rebase_result_win_dir_.empty() || | |
180 !ReadExpectedResult(rebase_result_win_dir_, | |
181 test_case_file_name, | |
182 &expected_text)) | |
183 ReadExpectedResult(layout_test_dir_, | |
184 test_case_file_name, | |
185 &expected_text); | |
186 } | |
187 ASSERT_TRUE(!expected_text.empty()); | |
188 | |
189 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
190 if (command_line->HasSwitch(switches::kOutputLayoutTestDifferences)) { | |
191 EXPECT_EQ(expected_text, actual_text) << | |
192 SaveResults(expected_text, actual_text); | |
193 } else { | |
194 EXPECT_EQ(expected_text, actual_text); | |
195 } | |
196 } | |
197 | |
198 std::string InProcessBrowserLayoutTest::SaveResults(const std::string& expected, | |
199 const std::string& actual) { | |
200 base::FilePath cwd; | |
201 EXPECT_TRUE(file_util::CreateNewTempDirectory( | |
202 base::FilePath::StringType(), &cwd)); | |
203 base::FilePath expected_filename = | |
204 cwd.Append(FILE_PATH_LITERAL("expected.txt")); | |
205 base::FilePath actual_filename = cwd.Append(FILE_PATH_LITERAL("actual.txt")); | |
206 EXPECT_NE(-1, file_util::WriteFile(expected_filename, | |
207 expected.c_str(), | |
208 expected.size())); | |
209 EXPECT_NE(-1, file_util::WriteFile(actual_filename, | |
210 actual.c_str(), | |
211 actual.size())); | |
212 return base::StringPrintf("Wrote %"PRFilePath" %"PRFilePath, | |
213 expected_filename.value().c_str(), | |
214 actual_filename.value().c_str()); | |
215 } | |
216 | |
217 } // namespace content | |
OLD | NEW |