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 manager_->RemoveObserver(this); | |
benjhayden
2012/08/21 20:00:24
Why do this here?
Randy Smith (Not in Mondays)
2012/08/21 22:00:37
We have no interest in hearing anything from the D
| |
113 manager_ = NULL; | |
114 } | |
115 | |
116 void ManagerGoingDownload(DownloadManager* manager) { | |
117 manager_->RemoveObserver(this); | |
118 manager_ = NULL; | |
119 if (waiting_) | |
120 MessageLoopForUI::current()->Quit(); | |
121 } | |
122 | |
123 bool waiting_; | |
124 DownloadManager* manager_; | |
125 DownloadItem* created_item_; | |
126 | |
127 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); | |
128 }; | |
129 | |
68 class SavePageBrowserTest : public InProcessBrowserTest { | 130 class SavePageBrowserTest : public InProcessBrowserTest { |
69 public: | 131 public: |
70 SavePageBrowserTest() {} | 132 SavePageBrowserTest() {} |
71 virtual ~SavePageBrowserTest(); | 133 virtual ~SavePageBrowserTest(); |
72 | 134 |
73 protected: | 135 protected: |
74 void SetUp() OVERRIDE { | 136 void SetUp() OVERRIDE { |
75 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); | 137 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); |
76 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); | 138 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); |
77 InProcessBrowserTest::SetUp(); | 139 InProcessBrowserTest::SetUp(); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
225 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 287 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
226 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. | 288 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. |
227 | 289 |
228 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 290 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
229 EXPECT_FALSE(file_util::PathExists(dir)); | 291 EXPECT_FALSE(file_util::PathExists(dir)); |
230 EXPECT_TRUE(file_util::ContentsEqual( | 292 EXPECT_TRUE(file_util::ContentsEqual( |
231 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), | 293 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
232 full_file_name)); | 294 full_file_name)); |
233 } | 295 } |
234 | 296 |
297 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) { | |
298 GURL url = NavigateToMockURL("a"); | |
benjhayden
2012/08/21 20:00:24
I'm just curious. Why "GURL url = ..." instead of
Randy Smith (Not in Mondays)
2012/08/21 22:00:37
I don't think there's any difference at the C++ le
| |
299 DownloadManager* manager(GetDownloadManager()); | |
300 std::vector<DownloadItem*> downloads; | |
301 manager->SearchDownloads(string16(), &downloads); | |
302 ASSERT_EQ(0u, downloads.size()); | |
303 | |
304 FilePath full_file_name, dir; | |
305 GetDestinationPaths("a", &full_file_name, &dir); | |
306 DownloadItemCreatedObserver creation_observer(manager); | |
307 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | |
308 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | |
309 DownloadItem* item = creation_observer.WaitForNewDownloadItem(); | |
310 item->Cancel(true); | |
311 | |
312 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package. | |
313 // Currently it's ignored. | |
314 EXPECT_EQ(url, WaitForSavePackageToFinish()); | |
315 | |
316 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | |
317 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. | |
318 | |
319 EXPECT_TRUE(file_util::PathExists(full_file_name)); | |
320 EXPECT_FALSE(file_util::PathExists(dir)); | |
321 EXPECT_TRUE(file_util::ContentsEqual( | |
322 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), | |
323 full_file_name)); | |
324 } | |
325 | |
326 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyTabDestroy) { | |
327 GURL url = NavigateToMockURL("a"); | |
328 DownloadManager* manager(GetDownloadManager()); | |
329 std::vector<DownloadItem*> downloads; | |
330 manager->SearchDownloads(string16(), &downloads); | |
331 ASSERT_EQ(0u, downloads.size()); | |
332 | |
333 FilePath full_file_name, dir; | |
334 GetDestinationPaths("a", &full_file_name, &dir); | |
335 DownloadItemCreatedObserver creation_observer(manager); | |
336 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | |
337 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | |
338 DownloadItem* item = creation_observer.WaitForNewDownloadItem(); | |
339 | |
340 // Close the tab; does this cancel the download? | |
341 GetCurrentTab()->Close(); | |
342 EXPECT_TRUE(item->IsCancelled()); | |
343 | |
344 EXPECT_FALSE(file_util::PathExists(full_file_name)); | |
benjhayden
2012/08/21 20:00:24
What happens if you WaitForSavePackageToFinish()?
Randy Smith (Not in Mondays)
2012/08/21 22:00:37
Well, I didn't try it, but a tab close should resu
benjhayden
2012/08/22 13:23:13
If you think it would help your DFM CL. If not, th
Randy Smith (Not in Mondays)
2012/08/22 14:50:43
Nope; from the DFM perspective, the question is "D
| |
345 EXPECT_FALSE(file_util::PathExists(dir)); | |
346 } | |
347 | |
235 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { | 348 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { |
236 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | 349 FilePath file_name(FILE_PATH_LITERAL("a.htm")); |
237 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( | 350 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( |
238 FilePath(kTestDir).Append(file_name)); | 351 FilePath(kTestDir).Append(file_name)); |
239 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( | 352 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( |
240 FilePath(kTestDir).Append(file_name)); | 353 FilePath(kTestDir).Append(file_name)); |
241 ui_test_utils::NavigateToURL(browser(), view_source_url); | 354 ui_test_utils::NavigateToURL(browser(), view_source_url); |
242 | 355 |
243 FilePath full_file_name, dir; | 356 FilePath full_file_name, dir; |
244 GetDestinationPaths("a", &full_file_name, &dir); | 357 GetDestinationPaths("a", &full_file_name, &dir); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
408 content::NotificationService::AllSources()); | 521 content::NotificationService::AllSources()); |
409 chrome::SavePage(browser()); | 522 chrome::SavePage(browser()); |
410 observer.Wait(); | 523 observer.Wait(); |
411 CheckDownloadHistory(url, full_file_name, -1); | 524 CheckDownloadHistory(url, full_file_name, -1); |
412 | 525 |
413 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 526 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
414 int64 actual_file_size = -1; | 527 int64 actual_file_size = -1; |
415 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); | 528 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); |
416 EXPECT_LE(kFileSizeMin, actual_file_size); | 529 EXPECT_LE(kFileSizeMin, actual_file_size); |
417 } | 530 } |
OLD | NEW |