OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_path.h" | 5 #include "base/file_path.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
9 #include "chrome/app/chrome_command_ids.h" | 9 #include "chrome/app/chrome_command_ids.h" |
| 10 #include "chrome/browser/download/download_history.h" |
10 #include "chrome/browser/download/download_item.h" | 11 #include "chrome/browser/download/download_item.h" |
| 12 #include "chrome/browser/download/download_manager.h" |
| 13 #include "chrome/browser/history/download_history_info.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
13 #include "chrome/browser/ui/webui/active_downloads_ui.h" | 17 #include "chrome/browser/ui/webui/active_downloads_ui.h" |
14 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
15 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
16 #include "chrome/test/base/in_process_browser_test.h" | 20 #include "chrome/test/base/in_process_browser_test.h" |
17 #include "chrome/test/base/ui_test_utils.h" | 21 #include "chrome/test/base/ui_test_utils.h" |
18 #include "content/browser/net/url_request_mock_http_job.h" | 22 #include "content/browser/net/url_request_mock_http_job.h" |
19 #include "content/browser/tab_contents/tab_contents.h" | 23 #include "content/browser/tab_contents/tab_contents.h" |
20 #include "content/common/content_notification_types.h" | 24 #include "content/common/content_notification_types.h" |
21 #include "content/common/notification_service.h" | 25 #include "content/common/notification_service.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
23 | 27 |
24 namespace { | |
25 | |
26 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); | 28 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); |
27 | 29 |
28 static const char* kAppendedExtension = | 30 static const char* kAppendedExtension = |
29 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
30 ".htm"; | 32 ".htm"; |
31 #else | 33 #else |
32 ".html"; | 34 ".html"; |
33 #endif | 35 #endif |
34 | 36 |
35 class SavePageBrowserTest : public InProcessBrowserTest { | 37 class SavePageBrowserTest : public InProcessBrowserTest { |
36 protected: | 38 protected: |
37 void SetUp() { | 39 void SetUp() { |
38 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); | 40 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); |
39 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); | 41 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); |
40 InProcessBrowserTest::SetUp(); | 42 InProcessBrowserTest::SetUp(); |
41 } | 43 } |
42 | 44 |
43 GURL WaitForSavePackageToFinish() { | 45 GURL WaitForSavePackageToFinish() const { |
44 ui_test_utils::TestNotificationObserver observer; | 46 ui_test_utils::TestNotificationObserver observer; |
45 ui_test_utils::RegisterAndWait(&observer, | 47 ui_test_utils::RegisterAndWait(&observer, |
46 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 48 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
47 NotificationService::AllSources()); | 49 NotificationService::AllSources()); |
48 return *Details<GURL>(observer.details()).ptr(); | 50 return Details<DownloadItem>(observer.details()).ptr()->original_url(); |
49 } | 51 } |
50 | 52 |
51 void CheckDownloadUI(const FilePath& download_path) { | |
52 #if defined(OS_CHROMEOS) | 53 #if defined(OS_CHROMEOS) |
| 54 const ActiveDownloadsUI::DownloadList& GetDownloads() const { |
53 Browser* popup = ActiveDownloadsUI::GetPopup(); | 55 Browser* popup = ActiveDownloadsUI::GetPopup(); |
54 EXPECT_TRUE(popup); | 56 EXPECT_TRUE(popup); |
55 ActiveDownloadsUI* downloads_ui = static_cast<ActiveDownloadsUI*>( | 57 ActiveDownloadsUI* downloads_ui = static_cast<ActiveDownloadsUI*>( |
56 popup->GetSelectedTabContents()->web_ui()); | 58 popup->GetSelectedTabContents()->web_ui()); |
57 ASSERT_TRUE(downloads_ui); | 59 EXPECT_TRUE(downloads_ui); |
58 const ActiveDownloadsUI::DownloadList& downloads = | 60 return downloads_ui->GetDownloads(); |
59 downloads_ui->GetDownloads(); | 61 } |
| 62 #endif |
| 63 |
| 64 void CheckDownloadUI(const FilePath& download_path) const { |
| 65 #if defined(OS_CHROMEOS) |
| 66 const ActiveDownloadsUI::DownloadList& downloads = GetDownloads(); |
60 EXPECT_EQ(downloads.size(), 1U); | 67 EXPECT_EQ(downloads.size(), 1U); |
61 | 68 |
62 bool found = false; | 69 bool found = false; |
63 for (size_t i = 0; i < downloads.size(); ++i) { | 70 for (size_t i = 0; i < downloads.size(); ++i) { |
64 if (downloads[i]->full_path() == download_path) { | 71 if (downloads[i]->full_path() == download_path) { |
65 found = true; | 72 found = true; |
66 break; | 73 break; |
67 } | 74 } |
68 } | 75 } |
69 EXPECT_TRUE(found); | 76 EXPECT_TRUE(found); |
70 #else | 77 #else |
71 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 78 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
72 #endif | 79 #endif |
73 } | 80 } |
74 | 81 |
| 82 DownloadManager* GetDownloadManager() const { |
| 83 DownloadManager* download_manager = |
| 84 browser()->profile()->GetDownloadManager(); |
| 85 EXPECT_TRUE(download_manager); |
| 86 return download_manager; |
| 87 } |
| 88 |
| 89 void QueryDownloadHistory() { |
| 90 // Query the history system. |
| 91 GetDownloadManager()->download_history()->Load( |
| 92 NewCallback(this, |
| 93 &SavePageBrowserTest::OnQueryDownloadEntriesComplete)); |
| 94 |
| 95 // Run message loop until a quit message is sent from |
| 96 // OnQueryDownloadEntriesComplete(). |
| 97 ui_test_utils::RunMessageLoop(); |
| 98 } |
| 99 |
| 100 void OnQueryDownloadEntriesComplete( |
| 101 std::vector<DownloadHistoryInfo>* entries) { |
| 102 history_entries_ = *entries; |
| 103 |
| 104 // Indicate thet we have received the history and can continue. |
| 105 MessageLoopForUI::current()->Quit(); |
| 106 } |
| 107 |
| 108 struct DownloadHistoryInfoMatch |
| 109 : public std::unary_function<DownloadHistoryInfo, bool> { |
| 110 |
| 111 DownloadHistoryInfoMatch(const GURL& url, |
| 112 const FilePath& path, |
| 113 int64 num_files) |
| 114 : url_(url), |
| 115 path_(path), |
| 116 num_files_(num_files) { |
| 117 } |
| 118 |
| 119 bool operator() (const DownloadHistoryInfo& info) const { |
| 120 return info.url == url_ && |
| 121 info.path == path_ && |
| 122 // For save packages, received bytes is actually the number of files. |
| 123 info.received_bytes == num_files_ && |
| 124 info.total_bytes == 0 && |
| 125 info.state == DownloadItem::COMPLETE; |
| 126 } |
| 127 |
| 128 GURL url_; |
| 129 FilePath path_; |
| 130 int64 num_files_; |
| 131 }; |
| 132 |
| 133 void CheckDownloadHistory(const GURL& url, |
| 134 const FilePath& path, |
| 135 int64 num_files_) { |
| 136 QueryDownloadHistory(); |
| 137 |
| 138 EXPECT_NE(std::find_if(history_entries_.begin(), history_entries_.end(), |
| 139 DownloadHistoryInfoMatch(url, path, num_files_)), |
| 140 history_entries_.end()); |
| 141 } |
| 142 |
| 143 std::vector<DownloadHistoryInfo> history_entries_; |
| 144 |
75 // Path to directory containing test data. | 145 // Path to directory containing test data. |
76 FilePath test_dir_; | 146 FilePath test_dir_; |
77 | 147 |
78 // Temporary directory we will save pages to. | 148 // Temporary directory we will save pages to. |
79 ScopedTempDir save_dir_; | 149 ScopedTempDir save_dir_; |
80 }; | 150 }; |
81 | 151 |
82 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { | 152 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { |
83 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | 153 FilePath file_name(FILE_PATH_LITERAL("a.htm")); |
84 GURL url = URLRequestMockHTTPJob::GetMockUrl( | 154 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
85 FilePath(kTestDir).Append(file_name)); | 155 FilePath(kTestDir).Append(file_name)); |
86 ui_test_utils::NavigateToURL(browser(), url); | 156 ui_test_utils::NavigateToURL(browser(), url); |
87 | 157 |
88 TabContents* current_tab = browser()->GetSelectedTabContents(); | 158 TabContents* current_tab = browser()->GetSelectedTabContents(); |
89 ASSERT_TRUE(current_tab); | 159 ASSERT_TRUE(current_tab); |
90 | 160 |
91 FilePath full_file_name = save_dir_.path().Append(file_name); | 161 FilePath full_file_name = save_dir_.path().Append(file_name); |
92 FilePath dir = save_dir_.path().AppendASCII("a_files"); | 162 FilePath dir = save_dir_.path().AppendASCII("a_files"); |
93 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, | 163 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, |
94 SavePackage::SAVE_AS_ONLY_HTML)); | 164 SavePackage::SAVE_AS_ONLY_HTML)); |
95 | 165 |
96 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 166 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
97 | 167 |
98 CheckDownloadUI(full_file_name); | 168 CheckDownloadUI(full_file_name); |
| 169 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. |
99 | 170 |
100 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 171 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
101 EXPECT_FALSE(file_util::PathExists(dir)); | 172 EXPECT_FALSE(file_util::PathExists(dir)); |
102 EXPECT_TRUE(file_util::ContentsEqual( | 173 EXPECT_TRUE(file_util::ContentsEqual( |
103 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | 174 test_dir_.Append(FilePath(kTestDir)).Append(file_name), |
104 full_file_name)); | 175 full_file_name)); |
105 } | 176 } |
106 | 177 |
107 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { | 178 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { |
108 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | 179 FilePath file_name(FILE_PATH_LITERAL("a.htm")); |
109 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( | 180 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( |
110 FilePath(kTestDir).Append(file_name)); | 181 FilePath(kTestDir).Append(file_name)); |
111 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( | 182 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( |
112 FilePath(kTestDir).Append(file_name)); | 183 FilePath(kTestDir).Append(file_name)); |
113 ui_test_utils::NavigateToURL(browser(), view_source_url); | 184 ui_test_utils::NavigateToURL(browser(), view_source_url); |
114 | 185 |
115 TabContents* current_tab = browser()->GetSelectedTabContents(); | 186 TabContents* current_tab = browser()->GetSelectedTabContents(); |
116 ASSERT_TRUE(current_tab); | 187 ASSERT_TRUE(current_tab); |
117 | 188 |
118 FilePath full_file_name = save_dir_.path().Append(file_name); | 189 FilePath full_file_name = save_dir_.path().Append(file_name); |
119 FilePath dir = save_dir_.path().AppendASCII("a_files"); | 190 FilePath dir = save_dir_.path().AppendASCII("a_files"); |
120 | 191 |
121 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, | 192 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, |
122 SavePackage::SAVE_AS_ONLY_HTML)); | 193 SavePackage::SAVE_AS_ONLY_HTML)); |
123 | 194 |
124 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish()); | 195 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish()); |
125 | 196 |
126 CheckDownloadUI(full_file_name); | 197 CheckDownloadUI(full_file_name); |
| 198 CheckDownloadHistory(actual_page_url, full_file_name, 1); // a.htm is 1 file. |
127 | 199 |
128 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 200 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
129 EXPECT_FALSE(file_util::PathExists(dir)); | 201 EXPECT_FALSE(file_util::PathExists(dir)); |
130 EXPECT_TRUE(file_util::ContentsEqual( | 202 EXPECT_TRUE(file_util::ContentsEqual( |
131 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | 203 test_dir_.Append(FilePath(kTestDir)).Append(file_name), |
132 full_file_name)); | 204 full_file_name)); |
133 } | 205 } |
134 | 206 |
135 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { | 207 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { |
136 FilePath file_name(FILE_PATH_LITERAL("b.htm")); | 208 FilePath file_name(FILE_PATH_LITERAL("b.htm")); |
137 GURL url = URLRequestMockHTTPJob::GetMockUrl( | 209 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
138 FilePath(kTestDir).Append(file_name)); | 210 FilePath(kTestDir).Append(file_name)); |
139 ui_test_utils::NavigateToURL(browser(), url); | 211 ui_test_utils::NavigateToURL(browser(), url); |
140 | 212 |
141 TabContents* current_tab = browser()->GetSelectedTabContents(); | 213 TabContents* current_tab = browser()->GetSelectedTabContents(); |
142 ASSERT_TRUE(current_tab); | 214 ASSERT_TRUE(current_tab); |
143 | 215 |
144 FilePath full_file_name = save_dir_.path().Append(file_name); | 216 FilePath full_file_name = save_dir_.path().Append(file_name); |
145 FilePath dir = save_dir_.path().AppendASCII("b_files"); | 217 FilePath dir = save_dir_.path().AppendASCII("b_files"); |
146 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, | 218 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, |
147 SavePackage::SAVE_AS_COMPLETE_HTML)); | 219 SavePackage::SAVE_AS_COMPLETE_HTML)); |
148 | 220 |
149 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 221 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
150 | 222 |
151 CheckDownloadUI(full_file_name); | 223 CheckDownloadUI(full_file_name); |
| 224 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files. |
152 | 225 |
153 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 226 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
154 EXPECT_TRUE(file_util::PathExists(dir)); | 227 EXPECT_TRUE(file_util::PathExists(dir)); |
155 EXPECT_TRUE(file_util::TextContentsEqual( | 228 EXPECT_TRUE(file_util::TextContentsEqual( |
156 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), | 229 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), |
157 full_file_name)); | 230 full_file_name)); |
158 EXPECT_TRUE(file_util::ContentsEqual( | 231 EXPECT_TRUE(file_util::ContentsEqual( |
159 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 232 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
160 dir.AppendASCII("1.png"))); | 233 dir.AppendASCII("1.png"))); |
161 EXPECT_TRUE(file_util::ContentsEqual( | 234 EXPECT_TRUE(file_util::ContentsEqual( |
(...skipping 21 matching lines...) Expand all Loading... |
183 | 256 |
184 TabContents* current_tab = browser()->GetSelectedTabContents(); | 257 TabContents* current_tab = browser()->GetSelectedTabContents(); |
185 ASSERT_TRUE(current_tab); | 258 ASSERT_TRUE(current_tab); |
186 | 259 |
187 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, | 260 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, |
188 SavePackage::SAVE_AS_COMPLETE_HTML)); | 261 SavePackage::SAVE_AS_COMPLETE_HTML)); |
189 | 262 |
190 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 263 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
191 | 264 |
192 CheckDownloadUI(full_file_name); | 265 CheckDownloadUI(full_file_name); |
| 266 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files. |
193 | 267 |
194 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 268 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
195 EXPECT_TRUE(file_util::PathExists(dir)); | 269 EXPECT_TRUE(file_util::PathExists(dir)); |
196 EXPECT_TRUE(file_util::TextContentsEqual( | 270 EXPECT_TRUE(file_util::TextContentsEqual( |
197 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), | 271 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), |
198 full_file_name)); | 272 full_file_name)); |
199 EXPECT_TRUE(file_util::ContentsEqual( | 273 EXPECT_TRUE(file_util::ContentsEqual( |
200 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 274 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
201 dir.AppendASCII("1.png"))); | 275 dir.AppendASCII("1.png"))); |
202 EXPECT_TRUE(file_util::ContentsEqual( | 276 EXPECT_TRUE(file_util::ContentsEqual( |
203 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), | 277 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), |
204 dir.AppendASCII("1.css"))); | 278 dir.AppendASCII("1.css"))); |
205 } | 279 } |
206 | 280 |
207 } // namespace | 281 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { |
| 282 FilePath file_name(FILE_PATH_LITERAL("a.htm")); |
| 283 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
| 284 FilePath(kTestDir).Append(file_name)); |
| 285 ui_test_utils::NavigateToURL(browser(), url); |
| 286 |
| 287 TabContents* current_tab = browser()->GetSelectedTabContents(); |
| 288 ASSERT_TRUE(current_tab); |
| 289 |
| 290 FilePath full_file_name = save_dir_.path().Append(file_name); |
| 291 FilePath dir = save_dir_.path().AppendASCII("a_files"); |
| 292 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, |
| 293 SavePackage::SAVE_AS_ONLY_HTML)); |
| 294 |
| 295 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
| 296 |
| 297 CheckDownloadUI(full_file_name); |
| 298 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. |
| 299 |
| 300 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1); |
| 301 |
| 302 #if defined(OS_CHROMEOS) |
| 303 EXPECT_EQ(GetDownloads().size(), 0U); |
| 304 #endif |
| 305 |
| 306 // Should not be in history. |
| 307 QueryDownloadHistory(); |
| 308 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(), |
| 309 DownloadHistoryInfoMatch(url, full_file_name, 1)), |
| 310 history_entries_.end()); |
| 311 |
| 312 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| 313 EXPECT_FALSE(file_util::PathExists(dir)); |
| 314 EXPECT_TRUE(file_util::ContentsEqual( |
| 315 test_dir_.Append(FilePath(kTestDir)).Append(file_name), |
| 316 full_file_name)); |
| 317 } |
OLD | NEW |