| 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/net/url_request_mock_util.h" | 19 #include "chrome/browser/net/url_request_mock_util.h" |
| 20 #include "chrome/browser/prefs/pref_member.h" |
| 21 #include "chrome/browser/prefs/pref_service.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/browser/ui/browser_window.h" | 24 #include "chrome/browser/ui/browser_window.h" |
| 23 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
| 26 #include "chrome/common/chrome_switches.h" |
| 27 #include "chrome/common/pref_names.h" |
| 24 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
| 25 #include "chrome/test/base/in_process_browser_test.h" | 29 #include "chrome/test/base/in_process_browser_test.h" |
| 26 #include "chrome/test/base/ui_test_utils.h" | 30 #include "chrome/test/base/ui_test_utils.h" |
| 27 #include "content/public/browser/download_item.h" | 31 #include "content/public/browser/download_item.h" |
| 28 #include "content/public/browser/download_manager.h" | 32 #include "content/public/browser/download_manager.h" |
| 29 #include "content/public/browser/download_persistent_store_info.h" | 33 #include "content/public/browser/download_persistent_store_info.h" |
| 30 #include "content/public/browser/notification_service.h" | 34 #include "content/public/browser/notification_service.h" |
| 31 #include "content/public/browser/notification_types.h" | 35 #include "content/public/browser/notification_types.h" |
| 32 #include "content/public/browser/web_contents.h" | 36 #include "content/public/browser/web_contents.h" |
| 33 #include "content/test/net/url_request_mock_http_job.h" | 37 #include "content/test/net/url_request_mock_http_job.h" |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 35 | 39 |
| 40 #if defined(OS_CHROMEOS) |
| 41 #include "chrome/browser/download/save_package_file_picker_chromeos.h" |
| 42 #else |
| 43 #include "chrome/browser/download/save_package_file_picker.h" |
| 44 #endif |
| 45 |
| 36 using content::BrowserThread; | 46 using content::BrowserThread; |
| 37 using content::DownloadItem; | 47 using content::DownloadItem; |
| 38 using content::DownloadManager; | 48 using content::DownloadManager; |
| 39 using content::DownloadPersistentStoreInfo; | 49 using content::DownloadPersistentStoreInfo; |
| 40 using content::WebContents; | 50 using content::WebContents; |
| 41 | 51 |
| 42 namespace { | 52 namespace { |
| 43 | 53 |
| 44 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); | 54 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); |
| 45 | 55 |
| 46 static const char* kAppendedExtension = | 56 static const char* kAppendedExtension = |
| 47 #if defined(OS_WIN) | 57 #if defined(OS_WIN) |
| 48 ".htm"; | 58 ".htm"; |
| 49 #else | 59 #else |
| 50 ".html"; | 60 ".html"; |
| 51 #endif | 61 #endif |
| 52 | 62 |
| 53 } // namespace | 63 } // namespace |
| 54 | 64 |
| 55 class SavePageBrowserTest : public InProcessBrowserTest { | 65 class SavePageBrowserTest : public InProcessBrowserTest { |
| 66 public: |
| 67 SavePageBrowserTest() {} |
| 68 virtual ~SavePageBrowserTest(); |
| 69 |
| 56 protected: | 70 protected: |
| 57 void SetUp() OVERRIDE { | 71 void SetUp() OVERRIDE { |
| 58 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); | 72 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); |
| 59 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); | 73 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); |
| 60 InProcessBrowserTest::SetUp(); | 74 InProcessBrowserTest::SetUp(); |
| 61 } | 75 } |
| 62 | 76 |
| 63 void SetUpOnMainThread() OVERRIDE { | 77 void SetUpOnMainThread() OVERRIDE { |
| 78 browser()->profile()->GetPrefs()->SetFilePath( |
| 79 prefs::kDownloadDefaultDirectory, save_dir_.path()); |
| 64 BrowserThread::PostTask( | 80 BrowserThread::PostTask( |
| 65 BrowserThread::IO, FROM_HERE, | 81 BrowserThread::IO, FROM_HERE, |
| 66 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); | 82 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| 67 } | 83 } |
| 68 | 84 |
| 69 GURL NavigateToMockURL(const std::string& prefix) { | 85 GURL NavigateToMockURL(const std::string& prefix) { |
| 70 GURL url = URLRequestMockHTTPJob::GetMockUrl( | 86 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
| 71 FilePath(kTestDir).AppendASCII(prefix + ".htm")); | 87 FilePath(kTestDir).AppendASCII(prefix + ".htm")); |
| 72 ui_test_utils::NavigateToURL(browser(), url); | 88 ui_test_utils::NavigateToURL(browser(), url); |
| 73 return url; | 89 return url; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 info.state == DownloadItem::COMPLETE; | 163 info.state == DownloadItem::COMPLETE; |
| 148 } | 164 } |
| 149 | 165 |
| 150 GURL url_; | 166 GURL url_; |
| 151 FilePath path_; | 167 FilePath path_; |
| 152 int64 num_files_; | 168 int64 num_files_; |
| 153 }; | 169 }; |
| 154 | 170 |
| 155 void CheckDownloadHistory(const GURL& url, | 171 void CheckDownloadHistory(const GURL& url, |
| 156 const FilePath& path, | 172 const FilePath& path, |
| 157 int64 num_files_) { | 173 int64 num_files) { |
| 158 QueryDownloadHistory(); | 174 QueryDownloadHistory(); |
| 159 | 175 |
| 160 EXPECT_NE(std::find_if(history_entries_.begin(), history_entries_.end(), | 176 std::vector<DownloadPersistentStoreInfo>::iterator found = |
| 161 DownloadPersistentStoreInfoMatch(url, path, num_files_)), | 177 std::find_if(history_entries_.begin(), history_entries_.end(), |
| 162 history_entries_.end()); | 178 DownloadPersistentStoreInfoMatch(url, path, num_files)); |
| 179 |
| 180 if (found == history_entries_.end()) { |
| 181 LOG(ERROR) << "Missing url=" << url.spec() |
| 182 << " path=" << path.value() |
| 183 << " received=" << num_files; |
| 184 for (size_t index = 0; index < history_entries_.size(); ++index) { |
| 185 LOG(ERROR) << "History@" << index << ": url=" |
| 186 << history_entries_[index].url.spec() |
| 187 << " path=" << history_entries_[index].path.value() |
| 188 << " received=" << history_entries_[index].received_bytes |
| 189 << " total=" << history_entries_[index].total_bytes |
| 190 << " state=" << history_entries_[index].state; |
| 191 } |
| 192 EXPECT_TRUE(false); |
| 193 } |
| 163 } | 194 } |
| 164 | 195 |
| 165 std::vector<DownloadPersistentStoreInfo> history_entries_; | 196 std::vector<DownloadPersistentStoreInfo> history_entries_; |
| 166 | 197 |
| 167 // Path to directory containing test data. | 198 // Path to directory containing test data. |
| 168 FilePath test_dir_; | 199 FilePath test_dir_; |
| 169 | 200 |
| 170 // Temporary directory we will save pages to. | 201 // Temporary directory we will save pages to. |
| 171 ScopedTempDir save_dir_; | 202 ScopedTempDir save_dir_; |
| 203 |
| 204 private: |
| 205 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest); |
| 172 }; | 206 }; |
| 173 | 207 |
| 208 SavePageBrowserTest::~SavePageBrowserTest() { |
| 209 } |
| 210 |
| 174 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { | 211 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { |
| 175 GURL url = NavigateToMockURL("a"); | 212 GURL url = NavigateToMockURL("a"); |
| 176 | 213 |
| 177 FilePath full_file_name, dir; | 214 FilePath full_file_name, dir; |
| 178 GetDestinationPaths("a", &full_file_name, &dir); | 215 GetDestinationPaths("a", &full_file_name, &dir); |
| 179 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 216 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
| 180 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 217 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
| 181 | 218 |
| 182 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 219 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
| 183 | 220 |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 content::NotificationService::AllSources()); | 365 content::NotificationService::AllSources()); |
| 329 browser()->SavePage(); | 366 browser()->SavePage(); |
| 330 observer.Wait(); | 367 observer.Wait(); |
| 331 | 368 |
| 332 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 369 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| 333 | 370 |
| 334 EXPECT_TRUE(file_util::DieFileDie(full_file_name, false)); | 371 EXPECT_TRUE(file_util::DieFileDie(full_file_name, false)); |
| 335 EXPECT_TRUE(file_util::DieFileDie(dir, true)); | 372 EXPECT_TRUE(file_util::DieFileDie(dir, true)); |
| 336 } | 373 } |
| 337 #endif | 374 #endif |
| 375 |
| 376 class SavePageAsMHTMLBrowserTest : public SavePageBrowserTest { |
| 377 public: |
| 378 SavePageAsMHTMLBrowserTest() {} |
| 379 virtual ~SavePageAsMHTMLBrowserTest(); |
| 380 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 381 command_line->AppendSwitch(switches::kSavePageAsMHTML); |
| 382 } |
| 383 |
| 384 private: |
| 385 DISALLOW_COPY_AND_ASSIGN(SavePageAsMHTMLBrowserTest); |
| 386 }; |
| 387 |
| 388 SavePageAsMHTMLBrowserTest::~SavePageAsMHTMLBrowserTest() { |
| 389 } |
| 390 |
| 391 IN_PROC_BROWSER_TEST_F(SavePageAsMHTMLBrowserTest, SavePageAsMHTML) { |
| 392 static const int64 kFileSize = 2759; |
| 393 GURL url = NavigateToMockURL("b"); |
| 394 FilePath download_dir = DownloadPrefs::FromDownloadManager( |
| 395 GetDownloadManager())->download_path(); |
| 396 FilePath full_file_name = download_dir.AppendASCII(std::string( |
| 397 "Test page for saving page feature.mhtml")); |
| 398 #if defined(OS_CHROMEOS) |
| 399 SavePackageFilePickerChromeOS::SetShouldPromptUser(false); |
| 400 #else |
| 401 SavePackageFilePicker::SetShouldPromptUser(false); |
| 402 #endif |
| 403 ui_test_utils::WindowedNotificationObserver observer( |
| 404 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| 405 content::NotificationService::AllSources()); |
| 406 browser()->SavePage(); |
| 407 observer.Wait(); |
| 408 CheckDownloadHistory(url, full_file_name, kFileSize); |
| 409 |
| 410 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| 411 int64 actual_file_size = -1; |
| 412 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); |
| 413 EXPECT_EQ(kFileSize, actual_file_size); |
| 414 } |
| OLD | NEW |