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, |
| 183 std::stringstream* errors, |
| 184 const DownloadPersistentStoreInfo& info) { |
| 185 if (info.path != expected_path) { |
| 186 *errors << "Expected path " << expected_path.value() |
| 187 << " but received " << info.path.value() << std::endl; |
| 188 return false; |
138 } | 189 } |
139 | 190 if (info.url != expected_url) { |
140 ~DownloadPersistedObserver() { | 191 *errors << "Expected url " << expected_url.spec() |
141 if (item_) | 192 << " but received " << info.url.spec() << std::endl; |
142 item_->RemoveObserver(this); | 193 return false; |
143 } | 194 } |
144 | 195 if (num_files >= 0 && |
145 // Wait for download item to get the persisted bit set. | 196 info.received_bytes != num_files) { |
146 // Note that this class provides no protection against the download | 197 *errors << "Expected received_bytes " << num_files |
147 // being destroyed between creation and return of WaitForPersisted(); | 198 << " but received " << info.received_bytes << std::endl; |
148 // the caller must guarantee that in some other fashion. | |
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 } | 199 } |
163 | 200 if (info.state != expected_state) { |
164 private: | 201 *errors << "Expected state " << expected_state |
165 // DownloadItem::Observer | 202 << " but received " << info.state << std::endl; |
166 virtual void OnDownloadUpdated(DownloadItem* item) OVERRIDE { | 203 return false; |
167 DCHECK_EQ(item, item_); | |
168 | |
169 if (waiting_ && item->IsPersisted()) | |
170 MessageLoopForUI::current()->Quit(); | |
171 } | 204 } |
172 | 205 return true; |
173 virtual void OnDownloadDestroyed(DownloadItem* item) OVERRIDE { | 206 } |
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 | 207 |
187 class SavePageBrowserTest : public InProcessBrowserTest { | 208 class SavePageBrowserTest : public InProcessBrowserTest { |
188 public: | 209 public: |
189 SavePageBrowserTest() {} | 210 SavePageBrowserTest() {} |
190 virtual ~SavePageBrowserTest(); | 211 virtual ~SavePageBrowserTest(); |
191 | 212 |
192 protected: | 213 protected: |
193 void SetUp() OVERRIDE { | 214 void SetUp() OVERRIDE { |
194 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); | 215 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); |
195 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); | 216 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); |
(...skipping 24 matching lines...) Expand all Loading... |
220 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm"); | 241 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm"); |
221 *dir = save_dir_.path().AppendASCII(prefix + "_files"); | 242 *dir = save_dir_.path().AppendASCII(prefix + "_files"); |
222 } | 243 } |
223 | 244 |
224 WebContents* GetCurrentTab() const { | 245 WebContents* GetCurrentTab() const { |
225 WebContents* current_tab = chrome::GetActiveWebContents(browser()); | 246 WebContents* current_tab = chrome::GetActiveWebContents(browser()); |
226 EXPECT_TRUE(current_tab); | 247 EXPECT_TRUE(current_tab); |
227 return current_tab; | 248 return current_tab; |
228 } | 249 } |
229 | 250 |
230 bool WaitForSavePackageToFinish(GURL* url_at_finish) const { | 251 // Returns true if and when there was a single download created, and its url |
| 252 // is |expected_url|. |
| 253 bool WaitForSavePackageToFinish(const GURL& expected_url) const { |
231 content::WindowedNotificationObserver observer( | 254 content::WindowedNotificationObserver observer( |
232 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 255 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
233 content::NotificationService::AllSources()); | 256 content::NotificationService::AllSources()); |
234 observer.Wait(); | 257 observer.Wait(); |
235 | 258 |
236 // Generally, there should only be one download item created | 259 // Generally, there should only be one download item created |
237 // in all of these tests. Wait for it, and wait for it to | 260 // in all of these tests. Wait for it, and wait for it to |
238 // be persisted. | 261 // be persisted. |
239 std::vector<DownloadItem*> items; | 262 std::vector<DownloadItem*> items; |
240 item_creation_observer_->WaitForDownloadItem(&items); | 263 item_creation_observer_->WaitForDownloadItem(&items); |
241 | 264 |
242 EXPECT_EQ(1u, items.size()); | 265 EXPECT_EQ(1u, items.size()); |
243 if (1u != items.size()) | 266 if (1u != items.size()) |
244 return false; | 267 return false; |
245 DownloadItem* download_item(items[0]); | 268 DownloadItem* download_item(items[0]); |
246 | 269 |
| 270 return ((expected_url == download_item->GetOriginalUrl()) && |
| 271 (expected_url == content::Details<DownloadItem>( |
| 272 observer.details()).ptr()->GetOriginalUrl())); |
| 273 } |
| 274 |
| 275 void WaitForPersisted( |
| 276 const GURL& expected_url, |
| 277 const FilePath& expected_path, |
| 278 int64 num_files, |
| 279 DownloadItem::DownloadState state) { |
| 280 errors_.reset(new std::stringstream); |
247 // Note on synchronization: | 281 // Note on synchronization: |
248 // | 282 // |
249 // For each Save Page As operation, we create a corresponding shell | 283 // For each Save Page As operation, we create a corresponding shell |
250 // DownloadItem to display progress to the user. That DownloadItem | 284 // DownloadItem to display progress to the user. That DownloadItem |
251 // goes through its own state transitions, including being persisted | 285 // goes through its own state transitions, including being persisted |
252 // out to the history database, and the download shelf is not shown | 286 // out to the history database, and the download shelf is not shown |
253 // until after the persistence occurs. Save Package completion (and | 287 // until after the persistence occurs. Save Package completion (and |
254 // marking the DownloadItem as completed) occurs asynchronously from | 288 // marking the DownloadItem as completed) occurs asynchronously from |
255 // persistence. Thus if we want to examine either UI state or DB | 289 // 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 | 290 // state, we need to wait until both the save package operation is |
257 // complete and the relevant download item has been persisted. | 291 // complete and the relevant download item has been persisted. |
258 DownloadPersistedObserver(download_item).WaitForPersisted(); | 292 DownloadPersistedObserver(browser()->profile(), base::Bind( |
259 | 293 &DownloadStoredProperly, |
260 *url_at_finish = content::Details<DownloadItem>(observer.details()).ptr()-> | 294 expected_url, |
261 GetOriginalUrl(); | 295 expected_path, |
262 return true; | 296 num_files, |
| 297 state, |
| 298 errors_.get())) |
| 299 .WaitForPersisted(); |
| 300 errors_.reset(); |
263 } | 301 } |
264 | 302 |
265 DownloadManager* GetDownloadManager() const { | 303 DownloadManager* GetDownloadManager() const { |
266 DownloadManager* download_manager = | 304 DownloadManager* download_manager = |
267 BrowserContext::GetDownloadManager(browser()->profile()); | 305 BrowserContext::GetDownloadManager(browser()->profile()); |
268 EXPECT_TRUE(download_manager); | 306 EXPECT_TRUE(download_manager); |
269 return download_manager; | 307 return download_manager; |
270 } | 308 } |
271 | 309 |
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. | 310 // Path to directory containing test data. |
358 FilePath test_dir_; | 311 FilePath test_dir_; |
359 | 312 |
360 // Temporary directory we will save pages to. | 313 // Temporary directory we will save pages to. |
361 ScopedTempDir save_dir_; | 314 ScopedTempDir save_dir_; |
362 | 315 |
363 private: | 316 private: |
364 scoped_ptr<DownloadItemCreatedObserver> item_creation_observer_; | 317 scoped_ptr<DownloadItemCreatedObserver> item_creation_observer_; |
| 318 scoped_ptr<std::stringstream> errors_; |
365 | 319 |
366 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest); | 320 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest); |
367 }; | 321 }; |
368 | 322 |
369 SavePageBrowserTest::~SavePageBrowserTest() { | 323 SavePageBrowserTest::~SavePageBrowserTest() { |
| 324 if (errors_.get()) { |
| 325 LOG(ERROR) << errors_->str(); |
| 326 } |
370 } | 327 } |
371 | 328 |
372 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { | 329 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { |
373 GURL url = NavigateToMockURL("a"); | 330 GURL url = NavigateToMockURL("a"); |
374 | 331 |
375 FilePath full_file_name, dir; | 332 FilePath full_file_name, dir; |
376 GetDestinationPaths("a", &full_file_name, &dir); | 333 GetDestinationPaths("a", &full_file_name, &dir); |
377 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 334 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
378 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 335 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
379 | 336 |
380 GURL output_url; | 337 ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
381 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); | 338 WaitForPersisted(url, full_file_name, 1, DownloadItem::COMPLETE); |
382 EXPECT_EQ(url, output_url); | |
383 | 339 |
384 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 340 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
385 // a.htm is 1 file. | |
386 CheckDownloadHistory(url, full_file_name, 1, DownloadItem::COMPLETE); | |
387 | 341 |
388 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 342 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
389 EXPECT_FALSE(file_util::PathExists(dir)); | 343 EXPECT_FALSE(file_util::PathExists(dir)); |
390 EXPECT_TRUE(file_util::ContentsEqual( | 344 EXPECT_TRUE(file_util::ContentsEqual( |
391 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), | 345 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
392 full_file_name)); | 346 full_file_name)); |
393 } | 347 } |
394 | 348 |
395 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) { | 349 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) { |
396 GURL url = NavigateToMockURL("a"); | 350 GURL url = NavigateToMockURL("a"); |
397 DownloadManager* manager(GetDownloadManager()); | 351 DownloadManager* manager(GetDownloadManager()); |
398 std::vector<DownloadItem*> downloads; | 352 std::vector<DownloadItem*> downloads; |
399 manager->GetAllDownloads(&downloads); | 353 manager->GetAllDownloads(&downloads); |
400 ASSERT_EQ(0u, downloads.size()); | 354 ASSERT_EQ(0u, downloads.size()); |
401 | 355 |
402 FilePath full_file_name, dir; | 356 FilePath full_file_name, dir; |
403 GetDestinationPaths("a", &full_file_name, &dir); | 357 GetDestinationPaths("a", &full_file_name, &dir); |
404 DownloadItemCreatedObserver creation_observer(manager); | 358 DownloadItemCreatedObserver creation_observer(manager); |
405 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 359 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
406 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 360 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
407 std::vector<DownloadItem*> items; | 361 std::vector<DownloadItem*> items; |
408 creation_observer.WaitForDownloadItem(&items); | 362 creation_observer.WaitForDownloadItem(&items); |
409 ASSERT_TRUE(items.size() == 1); | 363 ASSERT_TRUE(items.size() == 1); |
410 items[0]->Cancel(true); | 364 items[0]->Cancel(true); |
411 | 365 |
412 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package. | 366 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package. |
413 // Currently it's ignored. | 367 // Currently it's ignored. |
414 GURL output_url; | 368 ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
415 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); | |
416 EXPECT_EQ(url, output_url); | |
417 | 369 |
418 // -1 to disable number of files check; we don't update after cancel, and | 370 // -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 | 371 // we don't know when the single file completed in relationship to |
420 // the cancel. | 372 // the cancel. |
421 CheckDownloadHistory(url, full_file_name, -1, DownloadItem::CANCELLED); | 373 WaitForPersisted(url, full_file_name, -1, DownloadItem::CANCELLED); |
422 | 374 |
423 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 375 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
424 | 376 |
425 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 377 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
426 EXPECT_FALSE(file_util::PathExists(dir)); | 378 EXPECT_FALSE(file_util::PathExists(dir)); |
427 EXPECT_TRUE(file_util::ContentsEqual( | 379 EXPECT_TRUE(file_util::ContentsEqual( |
428 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), | 380 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
429 full_file_name)); | 381 full_file_name)); |
430 } | 382 } |
431 | 383 |
(...skipping 29 matching lines...) Expand all Loading... |
461 FilePath(kTestDir).Append(file_name)); | 413 FilePath(kTestDir).Append(file_name)); |
462 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( | 414 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( |
463 FilePath(kTestDir).Append(file_name)); | 415 FilePath(kTestDir).Append(file_name)); |
464 ui_test_utils::NavigateToURL(browser(), view_source_url); | 416 ui_test_utils::NavigateToURL(browser(), view_source_url); |
465 | 417 |
466 FilePath full_file_name, dir; | 418 FilePath full_file_name, dir; |
467 GetDestinationPaths("a", &full_file_name, &dir); | 419 GetDestinationPaths("a", &full_file_name, &dir); |
468 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 420 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
469 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 421 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
470 | 422 |
471 GURL output_url; | 423 ASSERT_TRUE(WaitForSavePackageToFinish(actual_page_url)); |
472 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); | |
473 EXPECT_EQ(actual_page_url, output_url); | |
474 | 424 |
475 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 425 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
476 // a.htm is 1 file. | 426 WaitForPersisted(actual_page_url, full_file_name, 1, DownloadItem::COMPLETE); |
477 CheckDownloadHistory(actual_page_url, full_file_name, 1, | |
478 DownloadItem::COMPLETE); | |
479 | 427 |
480 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 428 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
481 EXPECT_FALSE(file_util::PathExists(dir)); | 429 EXPECT_FALSE(file_util::PathExists(dir)); |
482 EXPECT_TRUE(file_util::ContentsEqual( | 430 EXPECT_TRUE(file_util::ContentsEqual( |
483 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | 431 test_dir_.Append(FilePath(kTestDir)).Append(file_name), |
484 full_file_name)); | 432 full_file_name)); |
485 } | 433 } |
486 | 434 |
487 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { | 435 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { |
488 GURL url = NavigateToMockURL("b"); | 436 GURL url = NavigateToMockURL("b"); |
489 | 437 |
490 FilePath full_file_name, dir; | 438 FilePath full_file_name, dir; |
491 GetDestinationPaths("b", &full_file_name, &dir); | 439 GetDestinationPaths("b", &full_file_name, &dir); |
492 ASSERT_TRUE(GetCurrentTab()->SavePage( | 440 ASSERT_TRUE(GetCurrentTab()->SavePage( |
493 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); | 441 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); |
494 | 442 |
495 GURL output_url; | 443 ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
496 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); | |
497 EXPECT_EQ(url, output_url); | |
498 | 444 |
499 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 445 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
500 // b.htm is 3 files. | 446 WaitForPersisted(url, full_file_name, 3, DownloadItem::COMPLETE); |
501 CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE); | |
502 | 447 |
503 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 448 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
504 EXPECT_TRUE(file_util::PathExists(dir)); | 449 EXPECT_TRUE(file_util::PathExists(dir)); |
505 EXPECT_TRUE(file_util::TextContentsEqual( | 450 EXPECT_TRUE(file_util::TextContentsEqual( |
506 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), | 451 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), |
507 full_file_name)); | 452 full_file_name)); |
508 EXPECT_TRUE(file_util::ContentsEqual( | 453 EXPECT_TRUE(file_util::ContentsEqual( |
509 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 454 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
510 dir.AppendASCII("1.png"))); | 455 dir.AppendASCII("1.png"))); |
511 EXPECT_TRUE(file_util::ContentsEqual( | 456 EXPECT_TRUE(file_util::ContentsEqual( |
512 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), | 457 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), |
513 dir.AppendASCII("1.css"))); | 458 dir.AppendASCII("1.css"))); |
514 } | 459 } |
515 | 460 |
516 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { | 461 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { |
517 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); | 462 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); |
518 EXPECT_FALSE(chrome::CanSavePage(browser())); | 463 EXPECT_FALSE(chrome::CanSavePage(browser())); |
519 } | 464 } |
520 | 465 |
521 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { | 466 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { |
522 GURL url = NavigateToMockURL("b"); | 467 GURL url = NavigateToMockURL("b"); |
523 | 468 |
524 FilePath full_file_name = save_dir_.path().AppendASCII( | 469 FilePath full_file_name = save_dir_.path().AppendASCII( |
525 std::string("Test page for saving page feature") + kAppendedExtension); | 470 std::string("Test page for saving page feature") + kAppendedExtension); |
526 FilePath dir = save_dir_.path().AppendASCII( | 471 FilePath dir = save_dir_.path().AppendASCII( |
527 "Test page for saving page feature_files"); | 472 "Test page for saving page feature_files"); |
528 ASSERT_TRUE(GetCurrentTab()->SavePage( | 473 ASSERT_TRUE(GetCurrentTab()->SavePage( |
529 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); | 474 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); |
530 | 475 |
531 GURL output_url; | 476 ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
532 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); | |
533 EXPECT_EQ(url, output_url); | |
534 | 477 |
535 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 478 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
536 // b.htm is 3 files. | 479 WaitForPersisted(url, full_file_name, 3, DownloadItem::COMPLETE); |
537 CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE); | |
538 | 480 |
539 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 481 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
540 EXPECT_TRUE(file_util::PathExists(dir)); | 482 EXPECT_TRUE(file_util::PathExists(dir)); |
541 EXPECT_TRUE(file_util::TextContentsEqual( | 483 EXPECT_TRUE(file_util::TextContentsEqual( |
542 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), | 484 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), |
543 full_file_name)); | 485 full_file_name)); |
544 EXPECT_TRUE(file_util::ContentsEqual( | 486 EXPECT_TRUE(file_util::ContentsEqual( |
545 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 487 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
546 dir.AppendASCII("1.png"))); | 488 dir.AppendASCII("1.png"))); |
547 EXPECT_TRUE(file_util::ContentsEqual( | 489 EXPECT_TRUE(file_util::ContentsEqual( |
548 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), | 490 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), |
549 dir.AppendASCII("1.css"))); | 491 dir.AppendASCII("1.css"))); |
550 } | 492 } |
551 | 493 |
552 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { | 494 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { |
553 GURL url = NavigateToMockURL("a"); | 495 GURL url = NavigateToMockURL("a"); |
554 | 496 |
555 FilePath full_file_name, dir; | 497 FilePath full_file_name, dir; |
556 GetDestinationPaths("a", &full_file_name, &dir); | 498 GetDestinationPaths("a", &full_file_name, &dir); |
557 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, | 499 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
558 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); | 500 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
559 | 501 |
560 GURL output_url; | 502 ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
561 ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); | |
562 EXPECT_EQ(url, output_url); | |
563 | 503 |
564 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 504 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
565 // a.htm is 1 file. | |
566 CheckDownloadHistory(url, full_file_name, 1, DownloadItem::COMPLETE); | |
567 | 505 |
568 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1); | 506 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1); |
569 | 507 |
570 // Should not be in history. | 508 // 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 | 509 |
577 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 510 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
578 EXPECT_FALSE(file_util::PathExists(dir)); | 511 EXPECT_FALSE(file_util::PathExists(dir)); |
579 EXPECT_TRUE(file_util::ContentsEqual( | 512 EXPECT_TRUE(file_util::ContentsEqual( |
580 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), | 513 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
581 full_file_name)); | 514 full_file_name)); |
582 } | 515 } |
583 | 516 |
584 // This tests that a webpage with the title "test.exe" is saved as | 517 // This tests that a webpage with the title "test.exe" is saved as |
585 // "test.exe.htm". | 518 // "test.exe.htm". |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 #if defined(OS_CHROMEOS) | 572 #if defined(OS_CHROMEOS) |
640 SavePackageFilePickerChromeOS::SetShouldPromptUser(false); | 573 SavePackageFilePickerChromeOS::SetShouldPromptUser(false); |
641 #else | 574 #else |
642 SavePackageFilePicker::SetShouldPromptUser(false); | 575 SavePackageFilePicker::SetShouldPromptUser(false); |
643 #endif | 576 #endif |
644 content::WindowedNotificationObserver observer( | 577 content::WindowedNotificationObserver observer( |
645 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 578 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
646 content::NotificationService::AllSources()); | 579 content::NotificationService::AllSources()); |
647 chrome::SavePage(browser()); | 580 chrome::SavePage(browser()); |
648 observer.Wait(); | 581 observer.Wait(); |
649 CheckDownloadHistory(url, full_file_name, -1, DownloadItem::COMPLETE); | 582 WaitForPersisted(url, full_file_name, -1, DownloadItem::COMPLETE); |
650 | 583 |
651 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 584 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
652 int64 actual_file_size = -1; | 585 int64 actual_file_size = -1; |
653 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); | 586 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); |
654 EXPECT_LE(kFileSizeMin, actual_file_size); | 587 EXPECT_LE(kFileSizeMin, actual_file_size); |
655 } | 588 } |
OLD | NEW |