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/command_line.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
30 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
31 #include "chrome/test/base/in_process_browser_test.h" | 31 #include "chrome/test/base/in_process_browser_test.h" |
32 #include "chrome/test/base/ui_test_utils.h" | 32 #include "chrome/test/base/ui_test_utils.h" |
33 #include "content/public/browser/download_item.h" | 33 #include "content/public/browser/download_item.h" |
34 #include "content/public/browser/download_manager.h" | 34 #include "content/public/browser/download_manager.h" |
35 #include "content/public/browser/download_persistent_store_info.h" | 35 #include "content/public/browser/download_persistent_store_info.h" |
36 #include "content/public/browser/notification_service.h" | 36 #include "content/public/browser/notification_service.h" |
37 #include "content/public/browser/notification_types.h" | 37 #include "content/public/browser/notification_types.h" |
38 #include "content/public/browser/web_contents.h" | 38 #include "content/public/browser/web_contents.h" |
| 39 #include "content/public/test/test_utils.h" |
39 #include "content/test/net/url_request_mock_http_job.h" | 40 #include "content/test/net/url_request_mock_http_job.h" |
40 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
41 | 42 |
42 #if defined(OS_CHROMEOS) | 43 #if defined(OS_CHROMEOS) |
43 #include "chrome/browser/download/save_package_file_picker_chromeos.h" | 44 #include "chrome/browser/download/save_package_file_picker_chromeos.h" |
44 #else | 45 #else |
45 #include "chrome/browser/download/save_package_file_picker.h" | 46 #include "chrome/browser/download/save_package_file_picker.h" |
46 #endif | 47 #endif |
47 | 48 |
48 using content::BrowserContext; | 49 using content::BrowserContext; |
49 using content::BrowserThread; | 50 using content::BrowserThread; |
50 using content::DownloadItem; | 51 using content::DownloadItem; |
51 using content::DownloadManager; | 52 using content::DownloadManager; |
52 using content::DownloadPersistentStoreInfo; | 53 using content::DownloadPersistentStoreInfo; |
53 using content::WebContents; | 54 using content::WebContents; |
54 | 55 |
55 namespace { | 56 namespace { |
56 | 57 |
57 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); | 58 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); |
58 | 59 |
59 static const char* kAppendedExtension = | 60 static const char* kAppendedExtension = |
60 #if defined(OS_WIN) | 61 #if defined(OS_WIN) |
61 ".htm"; | 62 ".htm"; |
62 #else | 63 #else |
63 ".html"; | 64 ".html"; |
64 #endif | 65 #endif |
65 | 66 |
66 } // namespace | 67 } // namespace |
67 | 68 |
| 69 // Loosely based on logic in DownloadTestObserver. |
| 70 class DownloadItemCreatedObserver : public DownloadManager::Observer { |
| 71 public: |
| 72 explicit DownloadItemCreatedObserver(DownloadManager* manager) |
| 73 : waiting_(false), manager_(manager), created_item_(NULL) { |
| 74 manager->AddObserver(this); |
| 75 } |
| 76 |
| 77 ~DownloadItemCreatedObserver() { |
| 78 if (manager_) |
| 79 manager_->RemoveObserver(this); |
| 80 } |
| 81 |
| 82 // Wait for the first download item created after object creation. |
| 83 // Note that this class provides no protection against the download |
| 84 // being destroyed between creation and return of WaitForNewDownloadItem(); |
| 85 // the caller must guarantee that in some other fashion. |
| 86 DownloadItem* WaitForNewDownloadItem() { |
| 87 if (!manager_) { |
| 88 // The manager went away before we were asked to wait; return |
| 89 // what we have, even if it's null. |
| 90 return created_item_; |
| 91 } |
| 92 |
| 93 if (!created_item_) { |
| 94 waiting_ = true; |
| 95 content::RunMessageLoop(); |
| 96 waiting_ = false; |
| 97 } |
| 98 return created_item_; |
| 99 } |
| 100 |
| 101 private: |
| 102 |
| 103 // DownloadManager::Observer |
| 104 void OnDownloadCreated(DownloadManager* manager, DownloadItem* item) { |
| 105 DCHECK_EQ(manager, manager_); |
| 106 if (!created_item_) |
| 107 created_item_ = item; |
| 108 |
| 109 if (waiting_) |
| 110 MessageLoopForUI::current()->Quit(); |
| 111 } |
| 112 |
| 113 void ManagerGoingDownload(DownloadManager* manager) { |
| 114 manager_->RemoveObserver(this); |
| 115 manager_ = NULL; |
| 116 if (waiting_) |
| 117 MessageLoopForUI::current()->Quit(); |
| 118 } |
| 119 |
| 120 bool waiting_; |
| 121 DownloadManager* manager_; |
| 122 DownloadItem* created_item_; |
| 123 |
| 124 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); |
| 125 }; |
| 126 |
68 class SavePageBrowserTest : public InProcessBrowserTest { | 127 class SavePageBrowserTest : public InProcessBrowserTest { |
69 public: | 128 public: |
70 SavePageBrowserTest() {} | 129 SavePageBrowserTest() {} |
71 virtual ~SavePageBrowserTest(); | 130 virtual ~SavePageBrowserTest(); |
72 | 131 |
73 protected: | 132 protected: |
74 void SetUp() OVERRIDE { | 133 void SetUp() OVERRIDE { |
75 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); | 134 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); |
76 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); | 135 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); |
77 InProcessBrowserTest::SetUp(); | 136 InProcessBrowserTest::SetUp(); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 284 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
226 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. | 285 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. |
227 | 286 |
228 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 287 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
229 EXPECT_FALSE(file_util::PathExists(dir)); | 288 EXPECT_FALSE(file_util::PathExists(dir)); |
230 EXPECT_TRUE(file_util::ContentsEqual( | 289 EXPECT_TRUE(file_util::ContentsEqual( |
231 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), | 290 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
232 full_file_name)); | 291 full_file_name)); |
233 } | 292 } |
234 | 293 |
| 294 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) { |
| 295 GURL url = NavigateToMockURL("a"); |
| 296 DownloadManager* manager(GetDownloadManager()); |
| 297 std::vector<DownloadItem*> downloads; |
| 298 manager->SearchDownloads(string16(), &downloads); |
| 299 ASSERT_EQ(0u, downloads.size()); |
| 300 |
| 301 FilePath full_file_name, dir; |
| 302 GetDestinationPaths("a", &full_file_name, &dir); |
| 303 DownloadItemCreatedObserver creation_observer(manager); |
| 304 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
| 305 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
| 306 DownloadItem* item = creation_observer.WaitForNewDownloadItem(); |
| 307 item->Cancel(true); |
| 308 |
| 309 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package. |
| 310 // Currently it's ignored. |
| 311 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
| 312 |
| 313 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| 314 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. |
| 315 |
| 316 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| 317 EXPECT_FALSE(file_util::PathExists(dir)); |
| 318 EXPECT_TRUE(file_util::ContentsEqual( |
| 319 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
| 320 full_file_name)); |
| 321 } |
| 322 |
| 323 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyTabDestroy) { |
| 324 GURL url = NavigateToMockURL("a"); |
| 325 DownloadManager* manager(GetDownloadManager()); |
| 326 std::vector<DownloadItem*> downloads; |
| 327 manager->SearchDownloads(string16(), &downloads); |
| 328 ASSERT_EQ(0u, downloads.size()); |
| 329 |
| 330 FilePath full_file_name, dir; |
| 331 GetDestinationPaths("a", &full_file_name, &dir); |
| 332 DownloadItemCreatedObserver creation_observer(manager); |
| 333 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
| 334 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
| 335 DownloadItem* item = creation_observer.WaitForNewDownloadItem(); |
| 336 |
| 337 // Close the tab; does this cancel the download? |
| 338 GetCurrentTab()->Close(); |
| 339 EXPECT_TRUE(item->IsCancelled()); |
| 340 |
| 341 EXPECT_FALSE(file_util::PathExists(full_file_name)); |
| 342 EXPECT_FALSE(file_util::PathExists(dir)); |
| 343 } |
| 344 |
235 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { | 345 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { |
236 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | 346 FilePath file_name(FILE_PATH_LITERAL("a.htm")); |
237 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( | 347 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( |
238 FilePath(kTestDir).Append(file_name)); | 348 FilePath(kTestDir).Append(file_name)); |
239 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( | 349 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( |
240 FilePath(kTestDir).Append(file_name)); | 350 FilePath(kTestDir).Append(file_name)); |
241 ui_test_utils::NavigateToURL(browser(), view_source_url); | 351 ui_test_utils::NavigateToURL(browser(), view_source_url); |
242 | 352 |
243 FilePath full_file_name, dir; | 353 FilePath full_file_name, dir; |
244 GetDestinationPaths("a", &full_file_name, &dir); | 354 GetDestinationPaths("a", &full_file_name, &dir); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 content::NotificationService::AllSources()); | 518 content::NotificationService::AllSources()); |
409 chrome::SavePage(browser()); | 519 chrome::SavePage(browser()); |
410 observer.Wait(); | 520 observer.Wait(); |
411 CheckDownloadHistory(url, full_file_name, -1); | 521 CheckDownloadHistory(url, full_file_name, -1); |
412 | 522 |
413 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 523 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
414 int64 actual_file_size = -1; | 524 int64 actual_file_size = -1; |
415 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); | 525 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); |
416 EXPECT_LE(kFileSizeMin, actual_file_size); | 526 EXPECT_LE(kFileSizeMin, actual_file_size); |
417 } | 527 } |
OLD | NEW |