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 <sstream> |
| 6 |
5 #include "base/bind.h" | 7 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
8 #include "base/file_path.h" | 10 #include "base/file_path.h" |
9 #include "base/file_util.h" | 11 #include "base/file_util.h" |
10 #include "base/path_service.h" | 12 #include "base/path_service.h" |
11 #include "base/scoped_temp_dir.h" | 13 #include "base/scoped_temp_dir.h" |
12 #include "base/test/test_file_util.h" | 14 #include "base/test/test_file_util.h" |
13 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
14 #include "chrome/browser/api/prefs/pref_member.h" | 16 #include "chrome/browser/api/prefs/pref_member.h" |
15 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 17 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
16 #include "chrome/browser/download/download_history.h" | 18 #include "chrome/browser/download/download_history.h" |
17 #include "chrome/browser/download/download_prefs.h" | 19 #include "chrome/browser/download/download_prefs.h" |
18 #include "chrome/browser/download/download_service.h" | 20 #include "chrome/browser/download/download_service.h" |
19 #include "chrome/browser/download/download_service_factory.h" | 21 #include "chrome/browser/download/download_service_factory.h" |
| 22 #include "chrome/browser/history/download_persistent_store_info.h" |
| 23 #include "chrome/browser/history/history_service_factory.h" |
20 #include "chrome/browser/net/url_request_mock_util.h" | 24 #include "chrome/browser/net/url_request_mock_util.h" |
21 #include "chrome/browser/prefs/pref_service.h" | 25 #include "chrome/browser/prefs/pref_service.h" |
22 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
24 #include "chrome/browser/ui/browser_commands.h" | 28 #include "chrome/browser/ui/browser_commands.h" |
25 #include "chrome/browser/ui/browser_tabstrip.h" | 29 #include "chrome/browser/ui/browser_tabstrip.h" |
26 #include "chrome/browser/ui/browser_window.h" | 30 #include "chrome/browser/ui/browser_window.h" |
27 #include "chrome/common/chrome_paths.h" | 31 #include "chrome/common/chrome_paths.h" |
28 #include "chrome/common/chrome_switches.h" | 32 #include "chrome/common/chrome_switches.h" |
29 #include "chrome/common/pref_names.h" | 33 #include "chrome/common/pref_names.h" |
30 #include "chrome/common/url_constants.h" | 34 #include "chrome/common/url_constants.h" |
31 #include "chrome/test/base/in_process_browser_test.h" | 35 #include "chrome/test/base/in_process_browser_test.h" |
32 #include "chrome/test/base/ui_test_utils.h" | 36 #include "chrome/test/base/ui_test_utils.h" |
33 #include "content/public/browser/download_item.h" | 37 #include "content/public/browser/download_item.h" |
34 #include "content/public/browser/download_manager.h" | 38 #include "content/public/browser/download_manager.h" |
35 #include "content/public/browser/download_persistent_store_info.h" | |
36 #include "content/public/browser/notification_service.h" | 39 #include "content/public/browser/notification_service.h" |
37 #include "content/public/browser/notification_types.h" | 40 #include "content/public/browser/notification_types.h" |
38 #include "content/public/browser/web_contents.h" | 41 #include "content/public/browser/web_contents.h" |
39 #include "content/public/test/test_utils.h" | 42 #include "content/public/test/test_utils.h" |
40 #include "content/test/net/url_request_mock_http_job.h" | 43 #include "content/test/net/url_request_mock_http_job.h" |
41 #include "testing/gtest/include/gtest/gtest.h" | 44 #include "testing/gtest/include/gtest/gtest.h" |
42 | 45 |
43 #if defined(OS_CHROMEOS) | 46 #if defined(OS_CHROMEOS) |
44 #include "chrome/browser/download/save_package_file_picker_chromeos.h" | 47 #include "chrome/browser/download/save_package_file_picker_chromeos.h" |
45 #else | 48 #else |
46 #include "chrome/browser/download/save_package_file_picker.h" | 49 #include "chrome/browser/download/save_package_file_picker.h" |
47 #endif | 50 #endif |
48 | 51 |
49 using content::BrowserContext; | 52 using content::BrowserContext; |
50 using content::BrowserThread; | 53 using content::BrowserThread; |
51 using content::DownloadItem; | 54 using content::DownloadItem; |
52 using content::DownloadManager; | 55 using content::DownloadManager; |
53 using content::DownloadPersistentStoreInfo; | |
54 using content::WebContents; | 56 using content::WebContents; |
55 | 57 |
56 namespace { | 58 namespace { |
57 | 59 |
| 60 class DownloadPersistedObserver : public DownloadHistory::Observer { |
| 61 public: |
| 62 typedef base::Callback<bool(const DownloadPersistentStoreInfo&)> |
| 63 PersistedFilter; |
| 64 |
| 65 DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter) |
| 66 : profile_(profile), |
| 67 filter_(filter) { |
| 68 DownloadServiceFactory::GetForProfile(profile)-> |
| 69 GetDownloadManagerDelegate()->AddHistoryObserver(this); |
| 70 } |
| 71 |
| 72 virtual ~DownloadPersistedObserver() { |
| 73 DownloadService* service = DownloadServiceFactory::GetForProfile(profile_); |
| 74 if (service && service->GetDownloadManagerDelegate()) |
| 75 service->GetDownloadManagerDelegate()->RemoveHistoryObserver(this); |
| 76 } |
| 77 |
| 78 bool WaitForPersisted() { |
| 79 if (persisted_) |
| 80 return true; |
| 81 waiting_ = true; |
| 82 content::RunMessageLoop(); |
| 83 waiting_ = false; |
| 84 return persisted_; |
| 85 } |
| 86 |
| 87 virtual void OnDownloadStored(DownloadItem* item, |
| 88 const DownloadPersistentStoreInfo& info) { |
| 89 persisted_ = filter_.Run(info); |
| 90 if (persisted_ && waiting_) |
| 91 MessageLoopForUI::current()->Quit(); |
| 92 } |
| 93 |
| 94 private: |
| 95 Profile* profile_; |
| 96 DownloadItem* item_; |
| 97 PersistedFilter filter_; |
| 98 bool waiting_; |
| 99 bool persisted_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); |
| 102 }; |
| 103 |
58 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); | 104 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); |
59 | 105 |
60 static const char* kAppendedExtension = | 106 static const char* kAppendedExtension = |
61 #if defined(OS_WIN) | 107 #if defined(OS_WIN) |
62 ".htm"; | 108 ".htm"; |
63 #else | 109 #else |
64 ".html"; | 110 ".html"; |
65 #endif | 111 #endif |
66 | 112 |
67 void NullFunction() { | 113 void NullFunction() { |
(...skipping 30 matching lines...) Expand all Loading... |
98 waiting_ = true; | 144 waiting_ = true; |
99 content::RunMessageLoop(); | 145 content::RunMessageLoop(); |
100 waiting_ = false; | 146 waiting_ = false; |
101 } | 147 } |
102 | 148 |
103 *items_seen = items_seen_; | 149 *items_seen = items_seen_; |
104 return; | 150 return; |
105 } | 151 } |
106 | 152 |
107 private: | 153 private: |
108 | |
109 // DownloadManager::Observer | 154 // DownloadManager::Observer |
110 virtual void OnDownloadCreated( | 155 virtual void OnDownloadCreated( |
111 DownloadManager* manager, DownloadItem* item) OVERRIDE { | 156 DownloadManager* manager, DownloadItem* item) OVERRIDE { |
112 DCHECK_EQ(manager, manager_); | 157 DCHECK_EQ(manager, manager_); |
113 items_seen_.push_back(item); | 158 items_seen_.push_back(item); |
114 | 159 |
115 if (waiting_) | 160 if (waiting_) |
116 MessageLoopForUI::current()->Quit(); | 161 MessageLoopForUI::current()->Quit(); |
117 } | 162 } |
118 | 163 |
119 virtual void ManagerGoingDown(DownloadManager* manager) OVERRIDE { | 164 virtual void ManagerGoingDown(DownloadManager* manager) OVERRIDE { |
120 manager_->RemoveObserver(this); | 165 manager_->RemoveObserver(this); |
121 manager_ = NULL; | 166 manager_ = NULL; |
122 if (waiting_) | 167 if (waiting_) |
123 MessageLoopForUI::current()->Quit(); | 168 MessageLoopForUI::current()->Quit(); |
124 } | 169 } |
125 | 170 |
126 bool waiting_; | 171 bool waiting_; |
127 DownloadManager* manager_; | 172 DownloadManager* manager_; |
128 std::vector<DownloadItem*> items_seen_; | 173 std::vector<DownloadItem*> items_seen_; |
129 | 174 |
130 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); | 175 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); |
131 }; | 176 }; |
132 | 177 |
133 class DownloadPersistedObserver : public DownloadItem::Observer { | 178 bool DownloadStoredProperly( |
134 public: | 179 const GURL& expected_url, |
135 explicit DownloadPersistedObserver(DownloadItem* item) | 180 const FilePath& expected_path, |
136 : waiting_(false), item_(item) { | 181 int64 num_files, |
137 item->AddObserver(this); | 182 DownloadItem::DownloadState expected_state, |
138 } | 183 const DownloadPersistentStoreInfo& info) { |
139 | 184 if (info.path != expected_path) |
140 ~DownloadPersistedObserver() { | 185 return false; |
141 if (item_) | 186 if (info.url != expected_url) |
142 item_->RemoveObserver(this); | 187 return false; |
143 } | 188 if ((num_files >= 0) && (info.received_bytes != num_files)) |
144 | 189 return false; |
145 // Wait for download item to get the persisted bit set. | 190 if (info.state != expected_state) |
146 // Note that this class provides no protection against the download | 191 return false; |
147 // being destroyed between creation and return of WaitForPersisted(); | 192 return true; |
148 // the caller must guarantee that in some other fashion. | 193 } |
149 void WaitForPersisted() { | |
150 // In combination with OnDownloadDestroyed() below, verify the | |
151 // above interface contract. | |
152 DCHECK(item_); | |
153 | |
154 if (item_->IsPersisted()) | |
155 return; | |
156 | |
157 waiting_ = true; | |
158 content::RunMessageLoop(); | |
159 waiting_ = false; | |
160 | |
161 return; | |
162 } | |
163 | |
164 private: | |
165 // DownloadItem::Observer | |
166 virtual void OnDownloadUpdated(DownloadItem* item) OVERRIDE { | |
167 DCHECK_EQ(item, item_); | |
168 | |
169 if (waiting_ && item->IsPersisted()) | |
170 MessageLoopForUI::current()->Quit(); | |
171 } | |
172 | |
173 virtual void OnDownloadDestroyed(DownloadItem* item) OVERRIDE { | |
174 if (item != item_) | |
175 return; | |
176 | |
177 item_->RemoveObserver(this); | |
178 item_ = NULL; | |
179 } | |
180 | |
181 bool waiting_; | |
182 DownloadItem* item_; | |
183 | |
184 DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); | |
185 }; | |
186 | 194 |
187 class SavePageBrowserTest : public InProcessBrowserTest { | 195 class SavePageBrowserTest : public InProcessBrowserTest { |
188 public: | 196 public: |
189 SavePageBrowserTest() {} | 197 SavePageBrowserTest() {} |
190 virtual ~SavePageBrowserTest(); | 198 virtual ~SavePageBrowserTest(); |
191 | 199 |
192 protected: | 200 protected: |
193 void SetUp() OVERRIDE { | 201 void SetUp() OVERRIDE { |
194 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); | 202 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); |
195 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); | 203 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); |
(...skipping 24 matching lines...) Expand all Loading... |
220 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm"); | 228 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm"); |
221 *dir = save_dir_.path().AppendASCII(prefix + "_files"); | 229 *dir = save_dir_.path().AppendASCII(prefix + "_files"); |
222 } | 230 } |
223 | 231 |
224 WebContents* GetCurrentTab() const { | 232 WebContents* GetCurrentTab() const { |
225 WebContents* current_tab = chrome::GetActiveWebContents(browser()); | 233 WebContents* current_tab = chrome::GetActiveWebContents(browser()); |
226 EXPECT_TRUE(current_tab); | 234 EXPECT_TRUE(current_tab); |
227 return current_tab; | 235 return current_tab; |
228 } | 236 } |
229 | 237 |
230 bool WaitForSavePackageToFinish(GURL* url_at_finish) const { | 238 // Returns true if and when there was a single download created, and its url |
| 239 // is |expected_url|. |
| 240 bool WaitForSavePackageToFinish(const GURL& expected_url) const { |
231 content::WindowedNotificationObserver observer( | 241 content::WindowedNotificationObserver observer( |
232 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 242 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
233 content::NotificationService::AllSources()); | 243 content::NotificationService::AllSources()); |
234 observer.Wait(); | 244 observer.Wait(); |
235 | 245 |
236 // Generally, there should only be one download item created | 246 // Generally, there should only be one download item created |
237 // in all of these tests. Wait for it, and wait for it to | 247 // in all of these tests. Wait for it, and wait for it to |
238 // be persisted. | 248 // be persisted. |
239 std::vector<DownloadItem*> items; | 249 std::vector<DownloadItem*> items; |
240 item_creation_observer_->WaitForDownloadItem(&items); | 250 item_creation_observer_->WaitForDownloadItem(&items); |
241 | 251 |
242 EXPECT_EQ(1u, items.size()); | 252 EXPECT_EQ(1u, items.size()); |
243 if (1u != items.size()) | 253 if (1u != items.size()) |
244 return false; | 254 return false; |
245 DownloadItem* download_item(items[0]); | 255 DownloadItem* download_item(items[0]); |
246 | 256 |
247 // Note on synchronization: | 257 return ((expected_url == download_item->GetOriginalUrl()) && |
248 // | 258 (expected_url == content::Details<DownloadItem>( |
249 // For each Save Page As operation, we create a corresponding shell | 259 observer.details()).ptr()->GetOriginalUrl())); |
250 // DownloadItem to display progress to the user. That DownloadItem | 260 } |
251 // goes through its own state transitions, including being persisted | |
252 // out to the history database, and the download shelf is not shown | |
253 // until after the persistence occurs. Save Package completion (and | |
254 // marking the DownloadItem as completed) occurs asynchronously from | |
255 // persistence. Thus if we want to examine either UI state or DB | |
256 // state, we need to wait until both the save package operation is | |
257 // complete and the relevant download item has been persisted. | |
258 DownloadPersistedObserver(download_item).WaitForPersisted(); | |
259 | 261 |
260 *url_at_finish = content::Details<DownloadItem>(observer.details()).ptr()-> | 262 // Note on synchronization: |
261 GetOriginalUrl(); | 263 // |
262 return true; | 264 // For each Save Page As operation, we create a corresponding shell |
263 } | 265 // DownloadItem to display progress to the user. That DownloadItem goes |
| 266 // through its own state transitions, including being persisted out to the |
| 267 // history database, and the download shelf is not shown until after the |
| 268 // persistence occurs. Save Package completion (and marking the DownloadItem |
| 269 // as completed) occurs asynchronously from persistence. Thus if we want to |
| 270 // examine either UI state or DB state, we need to wait until both the save |
| 271 // package operation is complete and the relevant download item has been |
| 272 // persisted. |
264 | 273 |
265 DownloadManager* GetDownloadManager() const { | 274 DownloadManager* GetDownloadManager() const { |
266 DownloadManager* download_manager = | 275 DownloadManager* download_manager = |
267 BrowserContext::GetDownloadManager(browser()->profile()); | 276 BrowserContext::GetDownloadManager(browser()->profile()); |
268 EXPECT_TRUE(download_manager); | 277 EXPECT_TRUE(download_manager); |
269 return download_manager; | 278 return download_manager; |
270 } | 279 } |
271 | 280 |
272 void QueryDownloadHistory() { | |
273 // Query the history system. | |
274 ChromeDownloadManagerDelegate* delegate = | |
275 static_cast<ChromeDownloadManagerDelegate*>( | |
276 GetDownloadManager()->GetDelegate()); | |
277 delegate->download_history()->Load( | |
278 base::Bind(&SavePageBrowserTest::OnQueryDownloadEntriesComplete, | |
279 base::Unretained(this))); | |
280 | |
281 // Run message loop until a quit message is sent from | |
282 // OnQueryDownloadEntriesComplete(). | |
283 content::RunMessageLoop(); | |
284 } | |
285 | |
286 void OnQueryDownloadEntriesComplete( | |
287 std::vector<DownloadPersistentStoreInfo>* entries) { | |
288 history_entries_ = *entries; | |
289 | |
290 // Indicate thet we have received the history and can continue. | |
291 MessageLoopForUI::current()->Quit(); | |
292 } | |
293 | |
294 struct DownloadPersistentStoreInfoMatch | |
295 : public std::unary_function<DownloadPersistentStoreInfo, bool> { | |
296 | |
297 DownloadPersistentStoreInfoMatch(const GURL& url, | |
298 const FilePath& path, | |
299 int64 num_files, | |
300 DownloadItem::DownloadState state) | |
301 : url_(url), | |
302 path_(path), | |
303 num_files_(num_files), | |
304 state_(state) {} | |
305 | |
306 bool operator() (const DownloadPersistentStoreInfo& info) const { | |
307 return info.url == url_ && | |
308 info.path == path_ && | |
309 // For non-MHTML save packages, received_bytes is actually the | |
310 // number of files. | |
311 ((num_files_ < 0) || | |
312 (info.received_bytes == num_files_)) && | |
313 info.total_bytes == 0 && | |
314 info.state == state_; | |
315 } | |
316 | |
317 GURL url_; | |
318 FilePath path_; | |
319 int64 num_files_; | |
320 DownloadItem::DownloadState state_; | |
321 }; | |
322 | |
323 void CheckDownloadHistory(const GURL& url, | |
324 const FilePath& path, | |
325 int64 num_files, | |
326 DownloadItem::DownloadState state) { | |
327 // Make sure the relevant download item made it into the history. | |
328 std::vector<DownloadItem*> downloads; | |
329 GetDownloadManager()->GetAllDownloads(&downloads); | |
330 ASSERT_EQ(1u, downloads.size()); | |
331 | |
332 QueryDownloadHistory(); | |
333 | |
334 std::vector<DownloadPersistentStoreInfo>::iterator found = | |
335 std::find_if(history_entries_.begin(), history_entries_.end(), | |
336 DownloadPersistentStoreInfoMatch(url, path, num_files, | |
337 state)); | |
338 | |
339 if (found == history_entries_.end()) { | |
340 LOG(ERROR) << "Missing url=" << url.spec() | |
341 << " path=" << path.value() | |
342 << " received=" << num_files; | |
343 for (size_t index = 0; index < history_entries_.size(); ++index) { | |
344 LOG(ERROR) << "History@" << index << ": url=" | |
345 << history_entries_[index].url.spec() | |
346 << " path=" << history_entries_[index].path.value() | |
347 << " received=" << history_entries_[index].received_bytes | |
348 << " total=" << history_entries_[index].total_bytes | |
349 << " state=" << history_entries_[index].state; | |
350 } | |
351 EXPECT_TRUE(false); | |
352 } | |
353 } | |
354 | |
355 std::vector<DownloadPersistentStoreInfo> history_entries_; | |
356 | |
357 // Path to directory containing test data. | 281 // Path to directory containing test data. |
358 FilePath test_dir_; | 282 FilePath test_dir_; |
359 | 283 |
360 // Temporary directory we will save pages to. | 284 // Temporary directory we will save pages to. |
361 ScopedTempDir save_dir_; | 285 ScopedTempDir save_dir_; |
362 | 286 |
363 private: | 287 private: |
364 scoped_ptr<DownloadItemCreatedObserver> item_creation_observer_; | 288 scoped_ptr<DownloadItemCreatedObserver> item_creation_observer_; |
365 | 289 |
366 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest); | 290 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest); |
367 }; | 291 }; |
368 | 292 |
369 SavePageBrowserTest::~SavePageBrowserTest() { | 293 SavePageBrowserTest::~SavePageBrowserTest() { |
370 } | 294 } |
371 | 295 |
372 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { | 296 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { |
373 GURL url = NavigateToMockURL("a"); | 297 GURL url = NavigateToMockURL("a"); |
374 | 298 |
375 FilePath full_file_name, dir; | 299 FilePath full_file_name, dir; |
376 GetDestinationPaths("a", &full_file_name, &dir); | 300 GetDestinationPaths("a", &full_file_name, &dir); |
| 301 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
| 302 &DownloadStoredProperly, |
| 303 url, full_file_name, 1, DownloadItem::COMPLETE)); |
377 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 304 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
378 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 305 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
379 | 306 |
380 GURL output_url; | 307 ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
381 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); | 308 persisted.WaitForPersisted(); |
382 EXPECT_EQ(url, output_url); | |
383 | 309 |
384 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 310 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
385 // a.htm is 1 file. | |
386 CheckDownloadHistory(url, full_file_name, 1, DownloadItem::COMPLETE); | |
387 | 311 |
388 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 312 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
389 EXPECT_FALSE(file_util::PathExists(dir)); | 313 EXPECT_FALSE(file_util::PathExists(dir)); |
390 EXPECT_TRUE(file_util::ContentsEqual( | 314 EXPECT_TRUE(file_util::ContentsEqual( |
391 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), | 315 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
392 full_file_name)); | 316 full_file_name)); |
393 } | 317 } |
394 | 318 |
395 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) { | 319 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) { |
396 GURL url = NavigateToMockURL("a"); | 320 GURL url = NavigateToMockURL("a"); |
397 DownloadManager* manager(GetDownloadManager()); | 321 DownloadManager* manager(GetDownloadManager()); |
398 std::vector<DownloadItem*> downloads; | 322 std::vector<DownloadItem*> downloads; |
399 manager->GetAllDownloads(&downloads); | 323 manager->GetAllDownloads(&downloads); |
400 ASSERT_EQ(0u, downloads.size()); | 324 ASSERT_EQ(0u, downloads.size()); |
401 | 325 |
402 FilePath full_file_name, dir; | 326 FilePath full_file_name, dir; |
403 GetDestinationPaths("a", &full_file_name, &dir); | 327 GetDestinationPaths("a", &full_file_name, &dir); |
| 328 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
| 329 &DownloadStoredProperly, |
| 330 url, full_file_name, -1, DownloadItem::CANCELLED)); |
404 DownloadItemCreatedObserver creation_observer(manager); | 331 DownloadItemCreatedObserver creation_observer(manager); |
405 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 332 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
406 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 333 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
407 std::vector<DownloadItem*> items; | 334 std::vector<DownloadItem*> items; |
408 creation_observer.WaitForDownloadItem(&items); | 335 creation_observer.WaitForDownloadItem(&items); |
409 ASSERT_TRUE(items.size() == 1); | 336 ASSERT_TRUE(items.size() == 1); |
410 items[0]->Cancel(true); | 337 items[0]->Cancel(true); |
411 | 338 |
412 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package. | 339 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package. |
413 // Currently it's ignored. | 340 // Currently it's ignored. |
414 GURL output_url; | 341 ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
415 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); | |
416 EXPECT_EQ(url, output_url); | |
417 | 342 |
418 // -1 to disable number of files check; we don't update after cancel, and | 343 // -1 to disable number of files check; we don't update after cancel, and |
419 // we don't know when the single file completed in relationship to | 344 // we don't know when the single file completed in relationship to |
420 // the cancel. | 345 // the cancel. |
421 CheckDownloadHistory(url, full_file_name, -1, DownloadItem::CANCELLED); | 346 persisted.WaitForPersisted(); |
422 | 347 |
423 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 348 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
424 | 349 |
425 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 350 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
426 EXPECT_FALSE(file_util::PathExists(dir)); | 351 EXPECT_FALSE(file_util::PathExists(dir)); |
427 EXPECT_TRUE(file_util::ContentsEqual( | 352 EXPECT_TRUE(file_util::ContentsEqual( |
428 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), | 353 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
429 full_file_name)); | 354 full_file_name)); |
430 } | 355 } |
431 | 356 |
(...skipping 26 matching lines...) Expand all Loading... |
458 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { | 383 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { |
459 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | 384 FilePath file_name(FILE_PATH_LITERAL("a.htm")); |
460 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( | 385 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( |
461 FilePath(kTestDir).Append(file_name)); | 386 FilePath(kTestDir).Append(file_name)); |
462 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( | 387 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( |
463 FilePath(kTestDir).Append(file_name)); | 388 FilePath(kTestDir).Append(file_name)); |
464 ui_test_utils::NavigateToURL(browser(), view_source_url); | 389 ui_test_utils::NavigateToURL(browser(), view_source_url); |
465 | 390 |
466 FilePath full_file_name, dir; | 391 FilePath full_file_name, dir; |
467 GetDestinationPaths("a", &full_file_name, &dir); | 392 GetDestinationPaths("a", &full_file_name, &dir); |
| 393 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
| 394 &DownloadStoredProperly, |
| 395 actual_page_url, full_file_name, 1, DownloadItem::COMPLETE)); |
468 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 396 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
469 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 397 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
470 | 398 |
471 GURL output_url; | 399 ASSERT_TRUE(WaitForSavePackageToFinish(actual_page_url)); |
472 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); | |
473 EXPECT_EQ(actual_page_url, output_url); | |
474 | 400 |
475 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 401 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
476 // a.htm is 1 file. | 402 persisted.WaitForPersisted(); |
477 CheckDownloadHistory(actual_page_url, full_file_name, 1, | |
478 DownloadItem::COMPLETE); | |
479 | 403 |
480 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 404 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
481 EXPECT_FALSE(file_util::PathExists(dir)); | 405 EXPECT_FALSE(file_util::PathExists(dir)); |
482 EXPECT_TRUE(file_util::ContentsEqual( | 406 EXPECT_TRUE(file_util::ContentsEqual( |
483 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | 407 test_dir_.Append(FilePath(kTestDir)).Append(file_name), |
484 full_file_name)); | 408 full_file_name)); |
485 } | 409 } |
486 | 410 |
487 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { | 411 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { |
488 GURL url = NavigateToMockURL("b"); | 412 GURL url = NavigateToMockURL("b"); |
489 | 413 |
490 FilePath full_file_name, dir; | 414 FilePath full_file_name, dir; |
491 GetDestinationPaths("b", &full_file_name, &dir); | 415 GetDestinationPaths("b", &full_file_name, &dir); |
| 416 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
| 417 &DownloadStoredProperly, |
| 418 url, full_file_name, 3, DownloadItem::COMPLETE)); |
492 ASSERT_TRUE(GetCurrentTab()->SavePage( | 419 ASSERT_TRUE(GetCurrentTab()->SavePage( |
493 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); | 420 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); |
494 | 421 |
495 GURL output_url; | 422 ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
496 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); | |
497 EXPECT_EQ(url, output_url); | |
498 | 423 |
499 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 424 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
500 // b.htm is 3 files. | 425 persisted.WaitForPersisted(); |
501 CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE); | |
502 | 426 |
503 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 427 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
504 EXPECT_TRUE(file_util::PathExists(dir)); | 428 EXPECT_TRUE(file_util::PathExists(dir)); |
505 EXPECT_TRUE(file_util::TextContentsEqual( | 429 EXPECT_TRUE(file_util::TextContentsEqual( |
506 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), | 430 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), |
507 full_file_name)); | 431 full_file_name)); |
508 EXPECT_TRUE(file_util::ContentsEqual( | 432 EXPECT_TRUE(file_util::ContentsEqual( |
509 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 433 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
510 dir.AppendASCII("1.png"))); | 434 dir.AppendASCII("1.png"))); |
511 EXPECT_TRUE(file_util::ContentsEqual( | 435 EXPECT_TRUE(file_util::ContentsEqual( |
512 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), | 436 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), |
513 dir.AppendASCII("1.css"))); | 437 dir.AppendASCII("1.css"))); |
514 } | 438 } |
515 | 439 |
516 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { | 440 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { |
517 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); | 441 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); |
518 EXPECT_FALSE(chrome::CanSavePage(browser())); | 442 EXPECT_FALSE(chrome::CanSavePage(browser())); |
519 } | 443 } |
520 | 444 |
521 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { | 445 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { |
522 GURL url = NavigateToMockURL("b"); | 446 GURL url = NavigateToMockURL("b"); |
523 | 447 |
524 FilePath full_file_name = save_dir_.path().AppendASCII( | 448 FilePath full_file_name = save_dir_.path().AppendASCII( |
525 std::string("Test page for saving page feature") + kAppendedExtension); | 449 std::string("Test page for saving page feature") + kAppendedExtension); |
526 FilePath dir = save_dir_.path().AppendASCII( | 450 FilePath dir = save_dir_.path().AppendASCII( |
527 "Test page for saving page feature_files"); | 451 "Test page for saving page feature_files"); |
| 452 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
| 453 &DownloadStoredProperly, |
| 454 url, full_file_name, 3, DownloadItem::COMPLETE)); |
528 ASSERT_TRUE(GetCurrentTab()->SavePage( | 455 ASSERT_TRUE(GetCurrentTab()->SavePage( |
529 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); | 456 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); |
530 | 457 |
531 GURL output_url; | 458 ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
532 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); | |
533 EXPECT_EQ(url, output_url); | |
534 | 459 |
535 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 460 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
536 // b.htm is 3 files. | 461 persisted.WaitForPersisted(); |
537 CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE); | |
538 | 462 |
539 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 463 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
540 EXPECT_TRUE(file_util::PathExists(dir)); | 464 EXPECT_TRUE(file_util::PathExists(dir)); |
541 EXPECT_TRUE(file_util::TextContentsEqual( | 465 EXPECT_TRUE(file_util::TextContentsEqual( |
542 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), | 466 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), |
543 full_file_name)); | 467 full_file_name)); |
544 EXPECT_TRUE(file_util::ContentsEqual( | 468 EXPECT_TRUE(file_util::ContentsEqual( |
545 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 469 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
546 dir.AppendASCII("1.png"))); | 470 dir.AppendASCII("1.png"))); |
547 EXPECT_TRUE(file_util::ContentsEqual( | 471 EXPECT_TRUE(file_util::ContentsEqual( |
548 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), | 472 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), |
549 dir.AppendASCII("1.css"))); | 473 dir.AppendASCII("1.css"))); |
550 } | 474 } |
551 | 475 |
552 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { | 476 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { |
553 GURL url = NavigateToMockURL("a"); | 477 GURL url = NavigateToMockURL("a"); |
554 | 478 |
555 FilePath full_file_name, dir; | 479 FilePath full_file_name, dir; |
556 GetDestinationPaths("a", &full_file_name, &dir); | 480 GetDestinationPaths("a", &full_file_name, &dir); |
557 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 481 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
558 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 482 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
559 | 483 |
560 GURL output_url; | 484 ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
561 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); | |
562 EXPECT_EQ(url, output_url); | |
563 | 485 |
564 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 486 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
565 // a.htm is 1 file. | |
566 CheckDownloadHistory(url, full_file_name, 1, DownloadItem::COMPLETE); | |
567 | 487 |
568 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1); | 488 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1); |
569 | 489 |
570 // Should not be in history. | 490 // TODO(benjhayden) Should not be in history. |
571 QueryDownloadHistory(); | |
572 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(), | |
573 DownloadPersistentStoreInfoMatch( | |
574 url, full_file_name, 1, DownloadItem::COMPLETE)), | |
575 history_entries_.end()); | |
576 | 491 |
577 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 492 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
578 EXPECT_FALSE(file_util::PathExists(dir)); | 493 EXPECT_FALSE(file_util::PathExists(dir)); |
579 EXPECT_TRUE(file_util::ContentsEqual( | 494 EXPECT_TRUE(file_util::ContentsEqual( |
580 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), | 495 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
581 full_file_name)); | 496 full_file_name)); |
582 } | 497 } |
583 | 498 |
584 // This tests that a webpage with the title "test.exe" is saved as | 499 // This tests that a webpage with the title "test.exe" is saved as |
585 // "test.exe.htm". | 500 // "test.exe.htm". |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 FilePath full_file_name = download_dir.AppendASCII(std::string( | 552 FilePath full_file_name = download_dir.AppendASCII(std::string( |
638 "Test page for saving page feature.mhtml")); | 553 "Test page for saving page feature.mhtml")); |
639 #if defined(OS_CHROMEOS) | 554 #if defined(OS_CHROMEOS) |
640 SavePackageFilePickerChromeOS::SetShouldPromptUser(false); | 555 SavePackageFilePickerChromeOS::SetShouldPromptUser(false); |
641 #else | 556 #else |
642 SavePackageFilePicker::SetShouldPromptUser(false); | 557 SavePackageFilePicker::SetShouldPromptUser(false); |
643 #endif | 558 #endif |
644 content::WindowedNotificationObserver observer( | 559 content::WindowedNotificationObserver observer( |
645 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 560 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
646 content::NotificationService::AllSources()); | 561 content::NotificationService::AllSources()); |
| 562 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
| 563 &DownloadStoredProperly, |
| 564 url, full_file_name, -1, DownloadItem::COMPLETE)); |
647 chrome::SavePage(browser()); | 565 chrome::SavePage(browser()); |
648 observer.Wait(); | 566 observer.Wait(); |
649 CheckDownloadHistory(url, full_file_name, -1, DownloadItem::COMPLETE); | 567 persisted.WaitForPersisted(); |
650 | 568 |
651 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 569 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
652 int64 actual_file_size = -1; | 570 int64 actual_file_size = -1; |
653 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); | 571 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); |
654 EXPECT_LE(kFileSizeMin, actual_file_size); | 572 EXPECT_LE(kFileSizeMin, actual_file_size); |
655 } | 573 } |
OLD | NEW |