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