Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/command_line.h" | |
| 7 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 10 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
| 11 #include "base/test/test_file_util.h" | 12 #include "base/test/test_file_util.h" |
| 12 #include "chrome/app/chrome_command_ids.h" | 13 #include "chrome/app/chrome_command_ids.h" |
| 13 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 14 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 14 #include "chrome/browser/download/download_history.h" | 15 #include "chrome/browser/download/download_history.h" |
| 15 #include "chrome/browser/download/download_prefs.h" | 16 #include "chrome/browser/download/download_prefs.h" |
| 16 #include "chrome/browser/download/download_service.h" | 17 #include "chrome/browser/download/download_service.h" |
| 17 #include "chrome/browser/download/download_service_factory.h" | 18 #include "chrome/browser/download/download_service_factory.h" |
| 18 #include "chrome/browser/download/save_package_file_picker.h" | 19 #include "chrome/browser/download/save_package_file_picker.h" |
| 19 #include "chrome/browser/net/url_request_mock_util.h" | 20 #include "chrome/browser/net/url_request_mock_util.h" |
| 21 #include "chrome/browser/prefs/pref_member.h" | |
| 22 #include "chrome/browser/prefs/pref_service.h" | |
| 20 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/browser.h" | 24 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/browser/ui/browser_window.h" | 25 #include "chrome/browser/ui/browser_window.h" |
| 23 #include "chrome/browser/ui/webui/chromeos/active_downloads_ui.h" | 26 #include "chrome/browser/ui/webui/chromeos/active_downloads_ui.h" |
| 24 #include "chrome/common/chrome_paths.h" | 27 #include "chrome/common/chrome_paths.h" |
| 28 #include "chrome/common/chrome_switches.h" | |
| 29 #include "chrome/common/pref_names.h" | |
| 25 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
| 26 #include "chrome/test/base/in_process_browser_test.h" | 31 #include "chrome/test/base/in_process_browser_test.h" |
| 27 #include "chrome/test/base/ui_test_utils.h" | 32 #include "chrome/test/base/ui_test_utils.h" |
| 28 #include "content/public/browser/download_item.h" | 33 #include "content/public/browser/download_item.h" |
| 29 #include "content/public/browser/download_manager.h" | 34 #include "content/public/browser/download_manager.h" |
| 30 #include "content/public/browser/download_persistent_store_info.h" | 35 #include "content/public/browser/download_persistent_store_info.h" |
| 31 #include "content/public/browser/notification_service.h" | 36 #include "content/public/browser/notification_service.h" |
| 32 #include "content/public/browser/notification_types.h" | 37 #include "content/public/browser/notification_types.h" |
| 33 #include "content/public/browser/web_contents.h" | 38 #include "content/public/browser/web_contents.h" |
| 34 #include "content/test/net/url_request_mock_http_job.h" | 39 #include "content/test/net/url_request_mock_http_job.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 47 static const char* kAppendedExtension = | 52 static const char* kAppendedExtension = |
| 48 #if defined(OS_WIN) | 53 #if defined(OS_WIN) |
| 49 ".htm"; | 54 ".htm"; |
| 50 #else | 55 #else |
| 51 ".html"; | 56 ".html"; |
| 52 #endif | 57 #endif |
| 53 | 58 |
| 54 } // namespace | 59 } // namespace |
| 55 | 60 |
| 56 class SavePageBrowserTest : public InProcessBrowserTest { | 61 class SavePageBrowserTest : public InProcessBrowserTest { |
| 62 public: | |
| 63 SavePageBrowserTest() {} | |
| 64 virtual ~SavePageBrowserTest(); | |
| 65 | |
| 57 protected: | 66 protected: |
| 58 void SetUp() OVERRIDE { | 67 void SetUp() OVERRIDE { |
| 59 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); | 68 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); |
| 60 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); | 69 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); |
| 61 InProcessBrowserTest::SetUp(); | 70 InProcessBrowserTest::SetUp(); |
| 62 } | 71 } |
| 63 | 72 |
| 64 void SetUpOnMainThread() OVERRIDE { | 73 void SetUpOnMainThread() OVERRIDE { |
| 74 browser()->profile()->GetPrefs()->SetFilePath( | |
| 75 prefs::kDownloadDefaultDirectory, save_dir_.path()); | |
| 65 BrowserThread::PostTask( | 76 BrowserThread::PostTask( |
| 66 BrowserThread::IO, FROM_HERE, | 77 BrowserThread::IO, FROM_HERE, |
| 67 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); | 78 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| 68 } | 79 } |
| 69 | 80 |
| 70 GURL NavigateToMockURL(const std::string& prefix) { | 81 GURL NavigateToMockURL(const std::string& prefix) { |
| 71 GURL url = URLRequestMockHTTPJob::GetMockUrl( | 82 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
| 72 FilePath(kTestDir).AppendASCII(prefix + ".htm")); | 83 FilePath(kTestDir).AppendASCII(prefix + ".htm")); |
| 73 ui_test_utils::NavigateToURL(browser(), url); | 84 ui_test_utils::NavigateToURL(browser(), url); |
| 74 return url; | 85 return url; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 info.state == DownloadItem::COMPLETE; | 188 info.state == DownloadItem::COMPLETE; |
| 178 } | 189 } |
| 179 | 190 |
| 180 GURL url_; | 191 GURL url_; |
| 181 FilePath path_; | 192 FilePath path_; |
| 182 int64 num_files_; | 193 int64 num_files_; |
| 183 }; | 194 }; |
| 184 | 195 |
| 185 void CheckDownloadHistory(const GURL& url, | 196 void CheckDownloadHistory(const GURL& url, |
| 186 const FilePath& path, | 197 const FilePath& path, |
| 187 int64 num_files_) { | 198 int64 num_files) { |
| 188 QueryDownloadHistory(); | 199 QueryDownloadHistory(); |
| 189 | 200 |
| 190 EXPECT_NE(std::find_if(history_entries_.begin(), history_entries_.end(), | 201 std::vector<DownloadPersistentStoreInfo>::iterator found = |
| 191 DownloadPersistentStoreInfoMatch(url, path, num_files_)), | 202 std::find_if(history_entries_.begin(), history_entries_.end(), |
| 192 history_entries_.end()); | 203 DownloadPersistentStoreInfoMatch(url, path, num_files)); |
| 204 | |
| 205 if (found == history_entries_.end()) { | |
| 206 LOG(ERROR) << "Missing url=" << url.spec() | |
| 207 << " path=" << path.value() | |
| 208 << " received=" << num_files; | |
| 209 for (size_t index = 0; index < history_entries_.size(); ++index) { | |
| 210 LOG(ERROR) << "History@" << index << ": url=" | |
| 211 << history_entries_[index].url.spec() | |
| 212 << " path=" << history_entries_[index].path.value() | |
| 213 << " received=" << history_entries_[index].received_bytes | |
| 214 << " total=" << history_entries_[index].total_bytes | |
| 215 << " state=" << history_entries_[index].state; | |
| 216 } | |
| 217 EXPECT_TRUE(false); | |
| 218 } | |
| 193 } | 219 } |
| 194 | 220 |
| 195 std::vector<DownloadPersistentStoreInfo> history_entries_; | 221 std::vector<DownloadPersistentStoreInfo> history_entries_; |
| 196 | 222 |
| 197 // Path to directory containing test data. | 223 // Path to directory containing test data. |
| 198 FilePath test_dir_; | 224 FilePath test_dir_; |
| 199 | 225 |
| 200 // Temporary directory we will save pages to. | 226 // Temporary directory we will save pages to. |
| 201 ScopedTempDir save_dir_; | 227 ScopedTempDir save_dir_; |
| 228 | |
| 229 private: | |
| 230 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest); | |
| 202 }; | 231 }; |
| 203 | 232 |
| 233 SavePageBrowserTest::~SavePageBrowserTest() { | |
| 234 } | |
| 235 | |
| 204 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { | 236 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { |
| 205 GURL url = NavigateToMockURL("a"); | 237 GURL url = NavigateToMockURL("a"); |
| 206 | 238 |
| 207 FilePath full_file_name, dir; | 239 FilePath full_file_name, dir; |
| 208 GetDestinationPaths("a", &full_file_name, &dir); | 240 GetDestinationPaths("a", &full_file_name, &dir); |
| 209 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 241 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
| 210 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 242 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
| 211 | 243 |
| 212 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 244 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
| 213 | 245 |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 362 content::NotificationService::AllSources()); | 394 content::NotificationService::AllSources()); |
| 363 browser()->SavePage(); | 395 browser()->SavePage(); |
| 364 observer.Wait(); | 396 observer.Wait(); |
| 365 | 397 |
| 366 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 398 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| 367 | 399 |
| 368 EXPECT_TRUE(file_util::DieFileDie(full_file_name, false)); | 400 EXPECT_TRUE(file_util::DieFileDie(full_file_name, false)); |
| 369 EXPECT_TRUE(file_util::DieFileDie(dir, true)); | 401 EXPECT_TRUE(file_util::DieFileDie(dir, true)); |
| 370 } | 402 } |
| 371 #endif | 403 #endif |
| 404 | |
| 405 class SavePageAsMHTMLBrowserTest : public SavePageBrowserTest { | |
| 406 public: | |
| 407 SavePageAsMHTMLBrowserTest() {} | |
| 408 virtual ~SavePageAsMHTMLBrowserTest(); | |
| 409 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
| 410 command_line->AppendSwitch(switches::kSavePageAsMHTML); | |
|
Randy Smith (Not in Mondays)
2012/04/18 18:00:55
Don't you need to switch this back at the end of t
benjhayden
2012/04/18 18:19:02
It doesn't look like any other tests do that, and
Randy Smith (Not in Mondays)
2012/04/24 17:23:54
Actually, now that I get my memory system working,
| |
| 411 } | |
| 412 | |
| 413 private: | |
| 414 DISALLOW_COPY_AND_ASSIGN(SavePageAsMHTMLBrowserTest); | |
| 415 }; | |
| 416 | |
| 417 SavePageAsMHTMLBrowserTest::~SavePageAsMHTMLBrowserTest() { | |
| 418 } | |
| 419 | |
| 420 IN_PROC_BROWSER_TEST_F(SavePageAsMHTMLBrowserTest, SavePageAsMHTML) { | |
| 421 GURL url = NavigateToMockURL("b"); | |
| 422 FilePath download_dir = DownloadPrefs::FromDownloadManager( | |
| 423 GetDownloadManager())->download_path(); | |
| 424 FilePath full_file_name = download_dir.AppendASCII(std::string( | |
| 425 "Test page for saving page feature.mhtml")); | |
| 426 SavePackageFilePicker::SetShouldPromptUser(false); | |
| 427 ui_test_utils::WindowedNotificationObserver observer( | |
| 428 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | |
| 429 content::NotificationService::AllSources()); | |
| 430 browser()->SavePage(); | |
| 431 observer.Wait(); | |
| 432 CheckDownloadUI(full_file_name); | |
| 433 CheckDownloadHistory(url, full_file_name, 1); | |
| 434 | |
| 435 EXPECT_TRUE(file_util::PathExists(full_file_name)); | |
| 436 int64 actual_file_size = -1; | |
| 437 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); | |
| 438 EXPECT_EQ(2760, actual_file_size); | |
| 439 } | |
| OLD | NEW |