OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
jam
2012/03/16 21:42:11
this file (and the .h) should be in content/test
dgrogan
2012/03/16 22:18:35
Done.
| |
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 "base/file_path.h" | |
6 #include "base/file_util.h" | |
7 #include "base/path_service.h" | |
8 #include "base/scoped_temp_dir.h" | |
9 #include "base/string_util.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "chrome/browser/ui/browser.h" | |
12 #include "chrome/common/chrome_paths.h" | |
13 #include "chrome/test/base/in_process_browser_test.h" | |
14 #include "chrome/test/base/ui_test_utils.h" | |
15 #include "content/browser/in_process_webkit/layout_browsertest.h" | |
jam
2012/03/16 21:42:11
nit: this should be first
dgrogan
2012/03/16 22:18:35
Done.
| |
16 #include "content/browser/tab_contents/tab_contents.h" | |
17 #include "net/base/net_util.h" | |
18 | |
19 namespace { | |
20 | |
21 size_t FindInsertPosition(const std::string& html) { | |
22 size_t tag_start = html.find("<html"); | |
23 if (tag_start == std::string::npos) | |
24 return 0; | |
25 size_t tag_end = html.find(">", tag_start); | |
26 if (tag_end == std::string::npos) | |
27 return 0; | |
28 return tag_end + 1; | |
29 } | |
30 | |
31 void ReadExpectedResult(const FilePath& result_dir_path, | |
32 const std::string test_case_file_name, | |
33 std::string* expected_result_value) { | |
34 FilePath expected_result_path(result_dir_path); | |
35 expected_result_path = expected_result_path.AppendASCII(test_case_file_name); | |
36 expected_result_path = expected_result_path.InsertBeforeExtension( | |
37 FILE_PATH_LITERAL("-expected")); | |
38 expected_result_path = | |
39 expected_result_path.ReplaceExtension(FILE_PATH_LITERAL("txt")); | |
40 std::string raw_result; | |
41 EXPECT_TRUE(file_util::ReadFileToString(expected_result_path, &raw_result)); | |
42 TrimString(raw_result, "\n", expected_result_value); | |
43 } | |
44 | |
45 void ScrapeResultFromBrowser(Browser* browser, std::string* actual_text) { | |
46 DOMElementProxyRef doc = ui_test_utils::GetActiveDOMDocument(browser); | |
47 DOMElementProxyRef body = | |
48 doc->FindElement(DOMElementProxy::By::XPath("//body")); | |
49 ASSERT_TRUE(body.get()); | |
50 ASSERT_TRUE(body->GetInnerText(actual_text)); | |
51 } | |
52 | |
53 static const std::string preamble = | |
54 "\n<script>\n" | |
55 "function LayoutTestController() {\n" | |
56 " this.dumpAsText = function () {};\n" | |
57 " this.waitUntilDone = function () {};\n" | |
58 " this.notifyDone = function () {\n" | |
59 " document.title = 'done';\n" | |
60 " }\n" | |
61 "}\n" | |
62 "window.layoutTestController = new LayoutTestController();\n" | |
63 "</script>"; | |
64 | |
65 } | |
66 | |
67 InProcessBrowserLayoutTest::InProcessBrowserLayoutTest( | |
68 const FilePath relative_layout_test_path) | |
69 : original_relative_path_(relative_layout_test_path) { | |
70 EnableDOMAutomation(); | |
71 } | |
72 | |
73 InProcessBrowserLayoutTest::~InProcessBrowserLayoutTest() {} | |
74 | |
75 void InProcessBrowserLayoutTest::SetUpInProcessBrowserTestFixture() { | |
76 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); | |
77 FilePath ThirdParty_WebKit_LayoutTests; | |
78 ASSERT_TRUE(PathService::Get(chrome::DIR_LAYOUT_TESTS, | |
79 &ThirdParty_WebKit_LayoutTests)); | |
80 our_original_layout_test_dir_ = | |
81 ThirdParty_WebKit_LayoutTests.Append(original_relative_path_); | |
82 ASSERT_TRUE(file_util::DirectoryExists(our_original_layout_test_dir_)); | |
83 our_layout_test_temp_dir_ = scoped_temp_dir_.path() | |
84 .AppendASCII("LayoutTests").Append(original_relative_path_); | |
85 ASSERT_TRUE(file_util::CreateDirectory(our_layout_test_temp_dir_)); | |
86 file_util::CopyDirectory( | |
87 our_original_layout_test_dir_.AppendASCII("resources"), | |
88 our_layout_test_temp_dir_.AppendASCII("resources"), | |
89 true /*recursive*/); | |
90 } | |
91 | |
92 void InProcessBrowserLayoutTest::RunLayoutTest( | |
93 const std::string& test_case_file_name) { | |
94 GURL test_url; | |
95 WriteModifiedFile(test_case_file_name, &test_url); | |
96 | |
97 LOG(INFO) << "Navigating to URL " << test_url << " and blocking."; | |
98 const string16 expected_title = ASCIIToUTF16("done"); | |
99 ui_test_utils::TitleWatcher title_watcher( | |
100 browser()->GetSelectedWebContents(), expected_title); | |
101 ui_test_utils::NavigateToURL(browser(), test_url); | |
102 string16 final_title = title_watcher.WaitAndGetTitle(); | |
103 EXPECT_EQ(expected_title, final_title); | |
104 | |
105 std::string actual_text; | |
106 ScrapeResultFromBrowser(browser(), &actual_text); | |
107 | |
108 std::string expected_text; | |
109 ReadExpectedResult(our_original_layout_test_dir_, test_case_file_name, | |
110 &expected_text); | |
111 | |
112 EXPECT_EQ(expected_text, actual_text); | |
113 } | |
114 | |
115 void InProcessBrowserLayoutTest::AddResourceForLayoutTest( | |
116 const FilePath& parent_dir, | |
117 const FilePath& resource_name) { | |
118 FilePath source; | |
119 ASSERT_TRUE(PathService::Get(chrome::DIR_LAYOUT_TESTS, &source)); | |
120 source = source.Append(parent_dir); | |
121 source = source.Append(resource_name); | |
122 | |
123 ASSERT_TRUE(file_util::PathExists(source)); | |
124 | |
125 FilePath dest_parent_dir = scoped_temp_dir_.path(). | |
126 AppendASCII("LayoutTests").Append(parent_dir); | |
127 ASSERT_TRUE(file_util::CreateDirectory(dest_parent_dir)); | |
128 FilePath dest = dest_parent_dir.Append(resource_name); | |
129 | |
130 if (file_util::DirectoryExists(source)) { | |
131 ASSERT_TRUE(file_util::CopyDirectory(source, dest, true)); | |
132 } else { | |
133 ASSERT_TRUE(file_util::CopyFile(source, dest)); | |
134 } | |
135 } | |
136 | |
137 void InProcessBrowserLayoutTest::WriteModifiedFile( | |
138 const std::string& test_case_file_name, GURL* test_url) { | |
139 FilePath path_to_single_test = | |
140 our_original_layout_test_dir_.AppendASCII(test_case_file_name); | |
141 std::string test_html; | |
142 ASSERT_TRUE(file_util::ReadFileToString(path_to_single_test, &test_html)); | |
143 | |
144 size_t insertion_position = FindInsertPosition(test_html); | |
145 test_html.insert(insertion_position, preamble); | |
146 FilePath new_test_file_path(our_layout_test_temp_dir_); | |
147 new_test_file_path = new_test_file_path.AppendASCII(test_case_file_name); | |
148 ASSERT_TRUE(file_util::WriteFile(new_test_file_path, | |
149 &test_html.at(0), | |
150 static_cast<int>(test_html.size()))); | |
151 *test_url = net::FilePathToFileURL(new_test_file_path); | |
152 } | |
OLD | NEW |