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.
5 #include "content/test/layout_browsertest.h"
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/shell/shell.h"
23 #include "content/shell/shell_switches.h"
24 #include "content/shell/webkit_test_controller.h"
25 #include "content/test/content_browser_test_utils.h"
26 #include "content/test/layout_test_http_server.h"
27 #include "net/base/net_util.h"
30 static const char kPlatformName
[] = "chromium-win";
31 #elif defined(OS_MACOSX)
32 static const char kPlatformName
[] = "chromium-mac";
33 #elif defined(OS_LINUX)
34 static const char kPlatformName
[] = "chromium-linux";
35 #elif defined(OS_OPENBSD)
36 static const char kPlatformName
[] = "chromium-openbsd";
37 #elif defined(OS_ANDROID)
38 static const char kPlatformName
[] = "chromium-android";
40 #error No known OS defined
46 bool ReadExpectedResult(const base::FilePath
& result_dir_path
,
47 const std::string test_case_file_name
,
48 std::string
* expected_result_value
) {
49 base::FilePath
expected_result_path(result_dir_path
);
50 expected_result_path
= expected_result_path
.AppendASCII(test_case_file_name
);
51 expected_result_path
= expected_result_path
.InsertBeforeExtension(
52 FILE_PATH_LITERAL("-expected"));
53 expected_result_path
=
54 expected_result_path
.ReplaceExtension(FILE_PATH_LITERAL("txt"));
55 return file_util::ReadFileToString(
56 expected_result_path
, expected_result_value
);
61 InProcessBrowserLayoutTest::InProcessBrowserLayoutTest(
62 const base::FilePath
& test_parent_dir
, const base::FilePath
& test_case_dir
)
63 : test_parent_dir_(test_parent_dir
), test_case_dir_(test_case_dir
),
67 InProcessBrowserLayoutTest::InProcessBrowserLayoutTest(
68 const base::FilePath
& test_parent_dir
,
69 const base::FilePath
& test_case_dir
,
71 : test_parent_dir_(test_parent_dir
), test_case_dir_(test_case_dir
),
75 InProcessBrowserLayoutTest::~InProcessBrowserLayoutTest() {
76 if (test_http_server_
.get())
77 CHECK(test_http_server_
->Stop());
80 void InProcessBrowserLayoutTest::SetUpInProcessBrowserTestFixture() {
81 base::FilePath src_dir
;
82 ASSERT_TRUE(PathService::Get(DIR_LAYOUT_TESTS
, &src_dir
));
83 base::FilePath absolute_parent_dir
= src_dir
.Append(test_parent_dir_
);
84 ASSERT_TRUE(file_util::DirectoryExists(absolute_parent_dir
));
85 layout_test_dir_
= absolute_parent_dir
.Append(test_case_dir_
);
86 ASSERT_TRUE(file_util::DirectoryExists(layout_test_dir_
));
88 // Gets the file path to rebased expected result directory for the current
90 // $LayoutTestRoot/platform/chromium_***/...
91 rebase_result_dir_
= src_dir
.AppendASCII("platform");
92 rebase_result_dir_
= rebase_result_dir_
.AppendASCII(kPlatformName
);
93 rebase_result_dir_
= rebase_result_dir_
.Append(test_parent_dir_
);
94 rebase_result_dir_
= rebase_result_dir_
.Append(test_case_dir_
);
96 // Generic chromium expected results. Not OS-specific. For example,
97 // v8-specific differences go here.
98 // chrome/test/data/layout_tests/LayoutTests/platform/chromium/...
99 rebase_result_chromium_dir_
= src_dir
.AppendASCII("platform").
100 AppendASCII("chromium").
101 Append(test_parent_dir_
).
102 Append(test_case_dir_
);
104 // Gets the file path to rebased expected result directory under the
105 // win32 platform. This is used by other non-win32 platform to use the same
106 // rebased expected results.
108 rebase_result_win_dir_
= src_dir
.AppendASCII("platform").
109 AppendASCII("chromium-win").
110 Append(test_parent_dir_
).
111 Append(test_case_dir_
);
115 // Layout tests expect that the server points at the
116 // LayoutTests\http\tests directory.
118 port_
= base::RandInt(1024, 65535);
119 test_http_server_
.reset(new LayoutTestHttpServer(
120 absolute_parent_dir
, port_
));
121 ASSERT_TRUE(test_http_server_
->Start());
125 void InProcessBrowserLayoutTest::SetUpCommandLine(CommandLine
* command_line
) {
126 command_line
->AppendSwitch(switches::kDumpRenderTree
);
129 void InProcessBrowserLayoutTest::SetUpOnMainThread() {
130 test_controller_
.reset(new WebKitTestController
);
133 void InProcessBrowserLayoutTest::RunLayoutTest(
134 const std::string
& test_case_file_name
) {
135 GURL url
= net::FilePathToFileURL(
136 layout_test_dir_
.AppendASCII(test_case_file_name
));
137 RunLayoutTestInternal(test_case_file_name
, url
);
140 void InProcessBrowserLayoutTest::RunHttpLayoutTest(
141 const std::string
& test_case_file_name
) {
142 DCHECK(test_http_server_
.get());
143 GURL
url(StringPrintf(
144 "http://127.0.0.1:%d/%s/%s", port_
, test_case_dir_
.MaybeAsASCII().c_str(),
145 test_case_file_name
.c_str()));
146 RunLayoutTestInternal(test_case_file_name
, url
);
149 void InProcessBrowserLayoutTest::RunLayoutTestInternal(
150 const std::string
& test_case_file_name
, const GURL
& url
) {
151 std::stringstream result
;
152 scoped_ptr
<WebKitTestResultPrinter
> printer(
153 new WebKitTestResultPrinter(&result
, NULL
));
154 printer
->set_capture_text_only(true);
155 test_controller_
->set_printer(printer
.release());
157 LOG(INFO
) << "Navigating to URL " << url
<< " and blocking.";
159 test_controller_
->PrepareForLayoutTest(url
, layout_test_dir_
, false, ""));
160 base::RunLoop run_loop
;
162 LOG(INFO
) << "Navigation completed.";
163 ASSERT_TRUE(test_controller_
->ResetAfterLayoutTest());
165 std::string actual_text
= result
.str();;
167 std::string expected_text
;
168 // Reads the expected result. First try to read from rebase directory.
169 // If failed, read from original directory.
170 if (!ReadExpectedResult(rebase_result_dir_
,
173 !ReadExpectedResult(rebase_result_chromium_dir_
,
176 if (rebase_result_win_dir_
.empty() ||
177 !ReadExpectedResult(rebase_result_win_dir_
,
180 ReadExpectedResult(layout_test_dir_
,
184 ASSERT_TRUE(!expected_text
.empty());
186 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
187 if (command_line
->HasSwitch(switches::kOutputLayoutTestDifferences
)) {
188 EXPECT_EQ(expected_text
, actual_text
) <<
189 SaveResults(expected_text
, actual_text
);
191 EXPECT_EQ(expected_text
, actual_text
);
195 std::string
InProcessBrowserLayoutTest::SaveResults(const std::string
& expected
,
196 const std::string
& actual
) {
198 EXPECT_TRUE(file_util::CreateNewTempDirectory(FILE_PATH_LITERAL(""), &cwd
));
199 base::FilePath expected_filename
= cwd
.Append(
200 FILE_PATH_LITERAL("expected.txt"));
201 base::FilePath actual_filename
= cwd
.Append(FILE_PATH_LITERAL("actual.txt"));
202 EXPECT_NE(-1, file_util::WriteFile(expected_filename
,
205 EXPECT_NE(-1, file_util::WriteFile(actual_filename
,
208 return StringPrintf("Wrote %"PRFilePath
" %"PRFilePath
,
209 expected_filename
.value().c_str(),
210 actual_filename
.value().c_str());
213 } // namespace content