1 // Copyright (c) 2011 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 "base/file_path.h"
6 #include "base/file_util.h"
7 #include "base/memory/scoped_temp_dir.h"
8 #include "base/path_service.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/net/url_request_mock_http_job.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/download/download_tab_helper.h"
14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/url_constants.h"
17 #include "chrome/test/in_process_browser_test.h"
18 #include "chrome/test/ui_test_utils.h"
19 #include "content/browser/tab_contents/tab_contents.h"
20 #include "content/common/notification_service.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace {
24
25 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page");
26
27 static const char* kAppendedExtension =
28 #if defined(OS_WIN)
29 ".htm";
30 #else
31 ".html";
32 #endif
33
34 class SavePageBrowserTest : public InProcessBrowserTest {
35 protected:
SetUp()36 void SetUp() {
37 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_));
38 ASSERT_TRUE(save_dir_.CreateUniqueTempDir());
39 InProcessBrowserTest::SetUp();
40 }
41
WaitForSavePackageToFinish()42 GURL WaitForSavePackageToFinish() {
43 ui_test_utils::TestNotificationObserver observer;
44 ui_test_utils::RegisterAndWait(&observer,
45 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
46 NotificationService::AllSources());
47 return *Details<GURL>(observer.details()).ptr();
48 }
49
50 // Path to directory containing test data.
51 FilePath test_dir_;
52
53 // Temporary directory we will save pages to.
54 ScopedTempDir save_dir_;
55 };
56
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest,SaveHTMLOnly)57 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
58 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
59 GURL url = URLRequestMockHTTPJob::GetMockUrl(
60 FilePath(kTestDir).Append(file_name));
61 ui_test_utils::NavigateToURL(browser(), url);
62
63 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
64 ASSERT_TRUE(current_tab);
65
66 FilePath full_file_name = save_dir_.path().Append(file_name);
67 FilePath dir = save_dir_.path().AppendASCII("a_files");
68 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage(
69 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML));
70
71 EXPECT_EQ(url, WaitForSavePackageToFinish());
72
73 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
74 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
75
76 EXPECT_TRUE(file_util::PathExists(full_file_name));
77 EXPECT_FALSE(file_util::PathExists(dir));
78 EXPECT_TRUE(file_util::ContentsEqual(
79 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
80 full_file_name));
81 }
82
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest,SaveViewSourceHTMLOnly)83 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) {
84 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
85 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl(
86 FilePath(kTestDir).Append(file_name));
87 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl(
88 FilePath(kTestDir).Append(file_name));
89 ui_test_utils::NavigateToURL(browser(), view_source_url);
90
91 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
92 ASSERT_TRUE(current_tab);
93
94 FilePath full_file_name = save_dir_.path().Append(file_name);
95 FilePath dir = save_dir_.path().AppendASCII("a_files");
96
97 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage(
98 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML));
99
100 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish());
101
102 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
103 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
104
105 EXPECT_TRUE(file_util::PathExists(full_file_name));
106 EXPECT_FALSE(file_util::PathExists(dir));
107 EXPECT_TRUE(file_util::ContentsEqual(
108 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
109 full_file_name));
110 }
111
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest,SaveCompleteHTML)112 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) {
113 FilePath file_name(FILE_PATH_LITERAL("b.htm"));
114 GURL url = URLRequestMockHTTPJob::GetMockUrl(
115 FilePath(kTestDir).Append(file_name));
116 ui_test_utils::NavigateToURL(browser(), url);
117
118 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
119 ASSERT_TRUE(current_tab);
120
121 FilePath full_file_name = save_dir_.path().Append(file_name);
122 FilePath dir = save_dir_.path().AppendASCII("b_files");
123 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage(
124 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML));
125
126 EXPECT_EQ(url, WaitForSavePackageToFinish());
127
128 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
129 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
130
131 EXPECT_TRUE(file_util::PathExists(full_file_name));
132 EXPECT_TRUE(file_util::PathExists(dir));
133 EXPECT_TRUE(file_util::TextContentsEqual(
134 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
135 full_file_name));
136 EXPECT_TRUE(file_util::ContentsEqual(
137 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
138 dir.AppendASCII("1.png")));
139 EXPECT_TRUE(file_util::ContentsEqual(
140 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
141 dir.AppendASCII("1.css")));
142 }
143
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest,NoSave)144 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
145 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
146 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE));
147 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE));
148 }
149
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest,FileNameFromPageTitle)150 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
151 FilePath file_name(FILE_PATH_LITERAL("b.htm"));
152
153 GURL url = URLRequestMockHTTPJob::GetMockUrl(
154 FilePath(kTestDir).Append(file_name));
155 ui_test_utils::NavigateToURL(browser(), url);
156
157 FilePath full_file_name = save_dir_.path().AppendASCII(
158 std::string("Test page for saving page feature") + kAppendedExtension);
159 FilePath dir = save_dir_.path().AppendASCII(
160 "Test page for saving page feature_files");
161
162 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
163 ASSERT_TRUE(current_tab);
164
165 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage(
166 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML));
167
168 EXPECT_EQ(url, WaitForSavePackageToFinish());
169
170 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
171 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
172
173 EXPECT_TRUE(file_util::PathExists(full_file_name));
174 EXPECT_TRUE(file_util::PathExists(dir));
175 EXPECT_TRUE(file_util::TextContentsEqual(
176 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
177 full_file_name));
178 EXPECT_TRUE(file_util::ContentsEqual(
179 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
180 dir.AppendASCII("1.png")));
181 EXPECT_TRUE(file_util::ContentsEqual(
182 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
183 dir.AppendASCII("1.css")));
184 }
185
186 } // namespace
187