Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: content/browser/download/download_browsertest.cc

Issue 11366121: Split DownloadFile::Rename into RenameAndUniquify and RenameAndAnnotate. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync to LKGR. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | content/browser/download/download_file.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // This file contains download browser tests that are known to be runnable 5 // This file contains download browser tests that are known to be runnable
6 // in a pure content context. Over time tests should be migrated here. 6 // in a pure content context. Over time tests should be migrated here.
7 7
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/scoped_temp_dir.h" 10 #include "base/scoped_temp_dir.h"
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 const GURL& referrer_url, 79 const GURL& referrer_url,
80 bool calculate_hash, 80 bool calculate_hash,
81 scoped_ptr<ByteStreamReader> stream, 81 scoped_ptr<ByteStreamReader> stream,
82 const net::BoundNetLog& bound_net_log, 82 const net::BoundNetLog& bound_net_log,
83 scoped_ptr<PowerSaveBlocker> power_save_blocker, 83 scoped_ptr<PowerSaveBlocker> power_save_blocker,
84 base::WeakPtr<DownloadDestinationObserver> observer, 84 base::WeakPtr<DownloadDestinationObserver> observer,
85 base::WeakPtr<DownloadFileWithDelayFactory> owner); 85 base::WeakPtr<DownloadFileWithDelayFactory> owner);
86 86
87 virtual ~DownloadFileWithDelay(); 87 virtual ~DownloadFileWithDelay();
88 88
89 // Wraps DownloadFileImpl::Rename and intercepts the return callback, 89 // Wraps DownloadFileImpl::Rename* and intercepts the return callback,
90 // storing it in the factory that produced this object for later 90 // storing it in the factory that produced this object for later
91 // retrieval. 91 // retrieval.
92 virtual void Rename(const FilePath& full_path, 92 virtual void RenameAndUniquify(
93 bool overwrite_existing_file, 93 const FilePath& full_path,
94 const RenameCompletionCallback& callback) OVERRIDE; 94 const RenameCompletionCallback& callback) OVERRIDE;
95 95 virtual void RenameAndAnnotate(
96 // Wraps DownloadFileImpl::Detach and intercepts the return callback, 96 const FilePath& full_path,
97 // storing it in the factory that produced this object for later 97 const RenameCompletionCallback& callback) OVERRIDE;
98 // retrieval.
99 virtual void Detach(const DetachCompletionCallback& callback) OVERRIDE;
100 98
101 private: 99 private:
102 static void RenameCallbackWrapper( 100 static void RenameCallbackWrapper(
103 DownloadFileWithDelayFactory* factory, 101 DownloadFileWithDelayFactory* factory,
104 const RenameCompletionCallback& original_callback, 102 const RenameCompletionCallback& original_callback,
105 DownloadInterruptReason reason, 103 DownloadInterruptReason reason,
106 const FilePath& path); 104 const FilePath& path);
107 105
108 static void DetachCallbackWrapper(
109 DownloadFileWithDelayFactory* factory,
110 const DetachCompletionCallback& original_callback,
111 DownloadInterruptReason interrupt_reason);
112
113 // This variable may only be read on the FILE thread, and may only be 106 // This variable may only be read on the FILE thread, and may only be
114 // indirected through (e.g. methods on DownloadFileWithDelayFactory called) 107 // indirected through (e.g. methods on DownloadFileWithDelayFactory called)
115 // on the UI thread. This is because after construction, 108 // on the UI thread. This is because after construction,
116 // DownloadFileWithDelay lives on the file thread, but 109 // DownloadFileWithDelay lives on the file thread, but
117 // DownloadFileWithDelayFactory is purely a UI thread object. 110 // DownloadFileWithDelayFactory is purely a UI thread object.
118 base::WeakPtr<DownloadFileWithDelayFactory> owner_; 111 base::WeakPtr<DownloadFileWithDelayFactory> owner_;
119 112
120 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay); 113 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay);
121 }; 114 };
122 115
123 // All routines on this class must be called on the UI thread. 116 // All routines on this class must be called on the UI thread.
124 class DownloadFileWithDelayFactory : public DownloadFileFactory { 117 class DownloadFileWithDelayFactory : public DownloadFileFactory {
125 public: 118 public:
126 DownloadFileWithDelayFactory(); 119 DownloadFileWithDelayFactory();
127 virtual ~DownloadFileWithDelayFactory(); 120 virtual ~DownloadFileWithDelayFactory();
128 121
129 // DownloadFileFactory interface. 122 // DownloadFileFactory interface.
130 virtual DownloadFile* CreateFile( 123 virtual DownloadFile* CreateFile(
131 scoped_ptr<DownloadSaveInfo> save_info, 124 scoped_ptr<DownloadSaveInfo> save_info,
132 const FilePath& default_download_directory, 125 const FilePath& default_download_directory,
133 const GURL& url, 126 const GURL& url,
134 const GURL& referrer_url, 127 const GURL& referrer_url,
135 bool calculate_hash, 128 bool calculate_hash,
136 scoped_ptr<ByteStreamReader> stream, 129 scoped_ptr<ByteStreamReader> stream,
137 const net::BoundNetLog& bound_net_log, 130 const net::BoundNetLog& bound_net_log,
138 base::WeakPtr<DownloadDestinationObserver> observer) OVERRIDE; 131 base::WeakPtr<DownloadDestinationObserver> observer) OVERRIDE;
139 132
140 void AddRenameCallback(base::Closure callback); 133 void AddRenameCallback(base::Closure callback);
141 void AddDetachCallback(base::Closure callback);
142 void GetAllRenameCallbacks(std::vector<base::Closure>* results); 134 void GetAllRenameCallbacks(std::vector<base::Closure>* results);
143 void GetAllDetachCallbacks(std::vector<base::Closure>* results);
144 135
145 // Do not return until either GetAllRenameCallbacks() or 136 // Do not return until GetAllRenameCallbacks() will return a non-empty list.
146 // GetAllDetachCallbacks() will return a non-empty list.
147 void WaitForSomeCallback(); 137 void WaitForSomeCallback();
148 138
149 private: 139 private:
150 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_; 140 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_;
151 std::vector<base::Closure> rename_callbacks_; 141 std::vector<base::Closure> rename_callbacks_;
152 std::vector<base::Closure> detach_callbacks_;
153 bool waiting_; 142 bool waiting_;
154 143
155 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory); 144 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory);
156 }; 145 };
157 146
158 DownloadFileWithDelay::DownloadFileWithDelay( 147 DownloadFileWithDelay::DownloadFileWithDelay(
159 scoped_ptr<DownloadSaveInfo> save_info, 148 scoped_ptr<DownloadSaveInfo> save_info,
160 const FilePath& default_download_directory, 149 const FilePath& default_download_directory,
161 const GURL& url, 150 const GURL& url,
162 const GURL& referrer_url, 151 const GURL& referrer_url,
163 bool calculate_hash, 152 bool calculate_hash,
164 scoped_ptr<ByteStreamReader> stream, 153 scoped_ptr<ByteStreamReader> stream,
165 const net::BoundNetLog& bound_net_log, 154 const net::BoundNetLog& bound_net_log,
166 scoped_ptr<PowerSaveBlocker> power_save_blocker, 155 scoped_ptr<PowerSaveBlocker> power_save_blocker,
167 base::WeakPtr<DownloadDestinationObserver> observer, 156 base::WeakPtr<DownloadDestinationObserver> observer,
168 base::WeakPtr<DownloadFileWithDelayFactory> owner) 157 base::WeakPtr<DownloadFileWithDelayFactory> owner)
169 : DownloadFileImpl( 158 : DownloadFileImpl(
170 save_info.Pass(), default_download_directory, url, referrer_url, 159 save_info.Pass(), default_download_directory, url, referrer_url,
171 calculate_hash, stream.Pass(), bound_net_log, 160 calculate_hash, stream.Pass(), bound_net_log,
172 power_save_blocker.Pass(), observer), 161 power_save_blocker.Pass(), observer),
173 owner_(owner) {} 162 owner_(owner) {}
174 163
175 DownloadFileWithDelay::~DownloadFileWithDelay() {} 164 DownloadFileWithDelay::~DownloadFileWithDelay() {}
176 165
177 void DownloadFileWithDelay::Rename(const FilePath& full_path, 166 void DownloadFileWithDelay::RenameAndUniquify(
178 bool overwrite_existing_file, 167 const FilePath& full_path, const RenameCompletionCallback& callback) {
179 const RenameCompletionCallback& callback) {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
181 DownloadFileImpl::Rename( 169 DownloadFileImpl::RenameAndUniquify(
182 full_path, overwrite_existing_file, 170 full_path, base::Bind(DownloadFileWithDelay::RenameCallbackWrapper,
183 base::Bind(DownloadFileWithDelay::RenameCallbackWrapper, 171 owner_, callback));
184 owner_, callback));
185 } 172 }
186 173
187 void DownloadFileWithDelay::Detach(const DetachCompletionCallback& callback) { 174 void DownloadFileWithDelay::RenameAndAnnotate(
175 const FilePath& full_path, const RenameCompletionCallback& callback) {
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
189 DownloadFileImpl::Detach( 177 DownloadFileImpl::RenameAndAnnotate(
190 base::Bind(DownloadFileWithDelay::DetachCallbackWrapper, 178 full_path, base::Bind(DownloadFileWithDelay::RenameCallbackWrapper,
191 owner_, callback)); 179 owner_, callback));
192 } 180 }
193 181
194 // static 182 // static
195 void DownloadFileWithDelay::RenameCallbackWrapper( 183 void DownloadFileWithDelay::RenameCallbackWrapper(
196 DownloadFileWithDelayFactory* factory, 184 DownloadFileWithDelayFactory* factory,
197 const RenameCompletionCallback& original_callback, 185 const RenameCompletionCallback& original_callback,
198 DownloadInterruptReason reason, 186 DownloadInterruptReason reason,
199 const FilePath& path) { 187 const FilePath& path) {
200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
201 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); 189 factory->AddRenameCallback(base::Bind(original_callback, reason, path));
202 } 190 }
203 191
204 // static
205 void DownloadFileWithDelay::DetachCallbackWrapper(
206 DownloadFileWithDelayFactory* factory,
207 const DetachCompletionCallback& original_callback,
208 DownloadInterruptReason interrupt_reason) {
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
210 factory->AddDetachCallback(base::Bind(original_callback, interrupt_reason));
211 }
212
213 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() 192 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory()
214 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), 193 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
215 waiting_(false) {} 194 waiting_(false) {}
216 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} 195 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {}
217 196
218 DownloadFile* DownloadFileWithDelayFactory::CreateFile( 197 DownloadFile* DownloadFileWithDelayFactory::CreateFile(
219 scoped_ptr<DownloadSaveInfo> save_info, 198 scoped_ptr<DownloadSaveInfo> save_info,
220 const FilePath& default_download_directory, 199 const FilePath& default_download_directory,
221 const GURL& url, 200 const GURL& url,
222 const GURL& referrer_url, 201 const GURL& referrer_url,
(...skipping 11 matching lines...) Expand all
234 psb.Pass(), observer, weak_ptr_factory_.GetWeakPtr()); 213 psb.Pass(), observer, weak_ptr_factory_.GetWeakPtr());
235 } 214 }
236 215
237 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { 216 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) {
238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
239 rename_callbacks_.push_back(callback); 218 rename_callbacks_.push_back(callback);
240 if (waiting_) 219 if (waiting_)
241 MessageLoopForUI::current()->Quit(); 220 MessageLoopForUI::current()->Quit();
242 } 221 }
243 222
244 void DownloadFileWithDelayFactory::AddDetachCallback(base::Closure callback) {
245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
246 detach_callbacks_.push_back(callback);
247 if (waiting_)
248 MessageLoopForUI::current()->Quit();
249 }
250
251 void DownloadFileWithDelayFactory::GetAllRenameCallbacks( 223 void DownloadFileWithDelayFactory::GetAllRenameCallbacks(
252 std::vector<base::Closure>* results) { 224 std::vector<base::Closure>* results) {
253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
254 results->swap(rename_callbacks_); 226 results->swap(rename_callbacks_);
255 } 227 }
256 228
257 void DownloadFileWithDelayFactory::GetAllDetachCallbacks(
258 std::vector<base::Closure>* results) {
259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
260 results->swap(detach_callbacks_);
261 }
262
263 void DownloadFileWithDelayFactory::WaitForSomeCallback() { 229 void DownloadFileWithDelayFactory::WaitForSomeCallback() {
264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
265 231
266 if (rename_callbacks_.empty() && detach_callbacks_.empty()) { 232 if (rename_callbacks_.empty()) {
267 waiting_ = true; 233 waiting_ = true;
268 RunMessageLoop(); 234 RunMessageLoop();
269 waiting_ = false; 235 waiting_ = false;
270 } 236 }
271 } 237 }
272 238
273 bool WasPersisted(DownloadItem* item) { 239 bool WasPersisted(DownloadItem* item) {
274 return item->IsPersisted(); 240 return item->IsPersisted();
275 } 241 }
276 242
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 new PowerSaveBlocker( 310 new PowerSaveBlocker(
345 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, 311 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
346 "Download in progress")); 312 "Download in progress"));
347 return new CountingDownloadFile( 313 return new CountingDownloadFile(
348 save_info.Pass(), default_downloads_directory, url, referrer_url, 314 save_info.Pass(), default_downloads_directory, url, referrer_url,
349 calculate_hash, stream.Pass(), bound_net_log, 315 calculate_hash, stream.Pass(), bound_net_log,
350 psb.Pass(), observer); 316 psb.Pass(), observer);
351 } 317 }
352 }; 318 };
353 319
320 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate {
321 public:
322 TestShellDownloadManagerDelegate()
323 : delay_download_open_(false) {}
324
325 virtual bool ShouldOpenDownload(
326 DownloadItem* item,
327 const DownloadOpenDelayedCallback& callback) OVERRIDE {
328 if (delay_download_open_) {
329 delayed_callbacks_.push_back(callback);
330 return false;
331 }
332 return true;
333 }
334
335 void SetDelayedOpen(bool delay) {
336 delay_download_open_ = delay;
337 }
338
339 void GetDelayedCallbacks(
340 std::vector<DownloadOpenDelayedCallback>* callbacks) {
341 callbacks->swap(delayed_callbacks_);
342 }
343 private:
344 virtual ~TestShellDownloadManagerDelegate() {}
345
346 bool delay_download_open_;
347 std::vector<DownloadOpenDelayedCallback> delayed_callbacks_;
348 };
349
354 } // namespace 350 } // namespace
355 351
356 class DownloadContentTest : public ContentBrowserTest { 352 class DownloadContentTest : public ContentBrowserTest {
357 protected: 353 protected:
358 virtual void SetUpOnMainThread() OVERRIDE { 354 virtual void SetUpOnMainThread() OVERRIDE {
359 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); 355 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir());
360 356
361 ShellDownloadManagerDelegate* delegate = 357 TestShellDownloadManagerDelegate* delegate =
362 static_cast<ShellDownloadManagerDelegate*>( 358 new TestShellDownloadManagerDelegate();
363 shell()->web_contents()->GetBrowserContext()
364 ->GetDownloadManagerDelegate());
365 delegate->SetDownloadBehaviorForTesting(downloads_directory_.path()); 359 delegate->SetDownloadBehaviorForTesting(downloads_directory_.path());
360 DownloadManager* manager = DownloadManagerForShell(shell());
361 manager->SetDelegate(delegate);
362 delegate->SetDownloadManager(manager);
366 363
367 BrowserThread::PostTask( 364 BrowserThread::PostTask(
368 BrowserThread::IO, FROM_HERE, 365 BrowserThread::IO, FROM_HERE,
369 base::Bind(&URLRequestSlowDownloadJob::AddUrlHandler)); 366 base::Bind(&URLRequestSlowDownloadJob::AddUrlHandler));
370 FilePath mock_base(GetTestFilePath("download", "")); 367 FilePath mock_base(GetTestFilePath("download", ""));
371 BrowserThread::PostTask( 368 BrowserThread::PostTask(
372 BrowserThread::IO, FROM_HERE, 369 BrowserThread::IO, FROM_HERE,
373 base::Bind(&URLRequestMockHTTPJob::AddUrlHandler, mock_base)); 370 base::Bind(&URLRequestMockHTTPJob::AddUrlHandler, mock_base));
374 } 371 }
375 372
373 TestShellDownloadManagerDelegate* GetDownloadManagerDelegate(
374 DownloadManager* manager) {
375 return static_cast<TestShellDownloadManagerDelegate*>(
376 manager->GetDelegate());
377 }
378
376 // Create a DownloadTestObserverTerminal that will wait for the 379 // Create a DownloadTestObserverTerminal that will wait for the
377 // specified number of downloads to finish. 380 // specified number of downloads to finish.
378 DownloadTestObserver* CreateWaiter( 381 DownloadTestObserver* CreateWaiter(
379 Shell* shell, int num_downloads) { 382 Shell* shell, int num_downloads) {
380 DownloadManager* download_manager = DownloadManagerForShell(shell); 383 DownloadManager* download_manager = DownloadManagerForShell(shell);
381 return new DownloadTestObserverTerminal(download_manager, num_downloads, 384 return new DownloadTestObserverTerminal(download_manager, num_downloads,
382 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); 385 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
383 } 386 }
384 387
385 // Create a DownloadTestObserverInProgress that will wait for the 388 // Create a DownloadTestObserverInProgress that will wait for the
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 download_manager->SetDownloadFileFactoryForTesting( 554 download_manager->SetDownloadFileFactoryForTesting(
552 scoped_ptr<DownloadFileFactory>(file_factory).Pass()); 555 scoped_ptr<DownloadFileFactory>(file_factory).Pass());
553 556
554 // Create a download 557 // Create a download
555 FilePath file(FILE_PATH_LITERAL("download-test.lib")); 558 FilePath file(FILE_PATH_LITERAL("download-test.lib"));
556 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); 559 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file));
557 560
558 // Wait until the first (intermediate file) rename and execute the callback. 561 // Wait until the first (intermediate file) rename and execute the callback.
559 file_factory->WaitForSomeCallback(); 562 file_factory->WaitForSomeCallback();
560 std::vector<base::Closure> callbacks; 563 std::vector<base::Closure> callbacks;
561 file_factory->GetAllDetachCallbacks(&callbacks);
562 ASSERT_TRUE(callbacks.empty());
563 file_factory->GetAllRenameCallbacks(&callbacks); 564 file_factory->GetAllRenameCallbacks(&callbacks);
564 ASSERT_EQ(1u, callbacks.size()); 565 ASSERT_EQ(1u, callbacks.size());
565 callbacks[0].Run(); 566 callbacks[0].Run();
566 callbacks.clear(); 567 callbacks.clear();
567 568
568 // Wait until the second (final) rename callback is posted. 569 // Wait until the second (final) rename callback is posted.
569 file_factory->WaitForSomeCallback(); 570 file_factory->WaitForSomeCallback();
570 file_factory->GetAllDetachCallbacks(&callbacks);
571 ASSERT_TRUE(callbacks.empty());
572 file_factory->GetAllRenameCallbacks(&callbacks); 571 file_factory->GetAllRenameCallbacks(&callbacks);
573 ASSERT_EQ(1u, callbacks.size()); 572 ASSERT_EQ(1u, callbacks.size());
574 573
575 // Cancel it. 574 // Cancel it.
576 std::vector<DownloadItem*> items; 575 std::vector<DownloadItem*> items;
577 download_manager->GetAllDownloads(&items); 576 download_manager->GetAllDownloads(&items);
578 ASSERT_EQ(1u, items.size()); 577 ASSERT_EQ(1u, items.size());
579 items[0]->Cancel(true); 578 items[0]->Cancel(true);
580 RunAllPendingInMessageLoop(); 579 RunAllPendingInMessageLoop();
581 580
582 // Check state. 581 // Check state.
583 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); 582 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState());
584 583
585 // Run final rename callback. 584 // Run final rename callback.
586 callbacks[0].Run(); 585 callbacks[0].Run();
587 callbacks.clear(); 586 callbacks.clear();
588 587
589 // Check state. 588 // Check state.
590 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); 589 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState());
591 } 590 }
592 591
593 // Try to cancel just after we release the download file, by delaying 592 // Try to cancel just after we release the download file, by delaying
594 // release. 593 // in ShouldOpenDownload.
595 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtRelease) { 594 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtRelease) {
595 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell()));
596
597 // Mark delegate for delayed open.
598 GetDownloadManagerDelegate(download_manager)->SetDelayedOpen(true);
599
596 // Setup new factory. 600 // Setup new factory.
597 DownloadFileWithDelayFactory* file_factory = 601 DownloadFileWithDelayFactory* file_factory =
598 new DownloadFileWithDelayFactory(); 602 new DownloadFileWithDelayFactory();
599 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell()));
600 download_manager->SetDownloadFileFactoryForTesting( 603 download_manager->SetDownloadFileFactoryForTesting(
601 scoped_ptr<DownloadFileFactory>(file_factory).Pass()); 604 scoped_ptr<DownloadFileFactory>(file_factory).Pass());
602 605
603 // Create a download 606 // Create a download
604 FilePath file(FILE_PATH_LITERAL("download-test.lib")); 607 FilePath file(FILE_PATH_LITERAL("download-test.lib"));
605 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); 608 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file));
606 609
607 // Wait until the first (intermediate file) rename and execute the callback. 610 // Wait until the first (intermediate file) rename and execute the callback.
608 file_factory->WaitForSomeCallback(); 611 file_factory->WaitForSomeCallback();
609 std::vector<base::Closure> callbacks; 612 std::vector<base::Closure> callbacks;
610 file_factory->GetAllDetachCallbacks(&callbacks);
611 ASSERT_TRUE(callbacks.empty());
612 file_factory->GetAllRenameCallbacks(&callbacks); 613 file_factory->GetAllRenameCallbacks(&callbacks);
613 ASSERT_EQ(1u, callbacks.size()); 614 ASSERT_EQ(1u, callbacks.size());
614 callbacks[0].Run(); 615 callbacks[0].Run();
615 callbacks.clear(); 616 callbacks.clear();
616 617
617 // Wait until the second (final) rename callback is posted. 618 // Wait until the second (final) rename callback is posted.
618 file_factory->WaitForSomeCallback(); 619 file_factory->WaitForSomeCallback();
619 file_factory->GetAllDetachCallbacks(&callbacks);
620 ASSERT_TRUE(callbacks.empty());
621 file_factory->GetAllRenameCallbacks(&callbacks); 620 file_factory->GetAllRenameCallbacks(&callbacks);
622 ASSERT_EQ(1u, callbacks.size()); 621 ASSERT_EQ(1u, callbacks.size());
623 622
624 // Call it. 623 // Call it.
625 callbacks[0].Run(); 624 callbacks[0].Run();
626 callbacks.clear(); 625 callbacks.clear();
627 626
628 // Confirm download still IN_PROGRESS. 627 // Confirm download still IN_PROGRESS (internal state COMPLETING).
629 std::vector<DownloadItem*> items; 628 std::vector<DownloadItem*> items;
630 download_manager->GetAllDownloads(&items); 629 download_manager->GetAllDownloads(&items);
631 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); 630 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState());
632 631
633 // Cancel the download; confirm cancel fails. 632 // Cancel the download; confirm cancel fails.
634 ASSERT_EQ(1u, items.size()); 633 ASSERT_EQ(1u, items.size());
635 items[0]->Cancel(true); 634 items[0]->Cancel(true);
636 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); 635 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState());
637 636
638 // Confirm detach callback and run it. 637 // Need to complete open test.
639 file_factory->WaitForSomeCallback(); 638 std::vector<DownloadOpenDelayedCallback> delayed_callbacks;
640 file_factory->GetAllRenameCallbacks(&callbacks); 639 GetDownloadManagerDelegate(download_manager)->GetDelayedCallbacks(
641 ASSERT_TRUE(callbacks.empty()); 640 &delayed_callbacks);
642 file_factory->GetAllDetachCallbacks(&callbacks); 641 ASSERT_EQ(1u, delayed_callbacks.size());
643 ASSERT_EQ(1u, callbacks.size()); 642 delayed_callbacks[0].Run(true);
644 callbacks[0].Run();
645 callbacks.clear();
646 643
647 // *Now* the download should be complete. 644 // *Now* the download should be complete.
648 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); 645 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState());
649 } 646 }
650 647
651 // Try to shutdown with a download in progress to make sure shutdown path 648 // Try to shutdown with a download in progress to make sure shutdown path
652 // works properly. 649 // works properly.
653 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownInProgress) { 650 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownInProgress) {
654 // Create a download that won't complete. 651 // Create a download that won't complete.
655 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); 652 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1));
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 EXPECT_CALL(item_observer, OnDownloadDestroyed(items[0])) 684 EXPECT_CALL(item_observer, OnDownloadDestroyed(items[0]))
688 .WillOnce(Return()); 685 .WillOnce(Return());
689 } 686 }
690 DownloadManagerForShell(shell())->Shutdown(); 687 DownloadManagerForShell(shell())->Shutdown();
691 items.clear(); 688 items.clear();
692 } 689 }
693 690
694 // Try to shutdown just after we release the download file, by delaying 691 // Try to shutdown just after we release the download file, by delaying
695 // release. 692 // release.
696 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownAtRelease) { 693 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownAtRelease) {
694 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell()));
695
696 // Mark delegate for delayed open.
697 GetDownloadManagerDelegate(download_manager)->SetDelayedOpen(true);
698
697 // Setup new factory. 699 // Setup new factory.
698 DownloadFileWithDelayFactory* file_factory = 700 DownloadFileWithDelayFactory* file_factory =
699 new DownloadFileWithDelayFactory(); 701 new DownloadFileWithDelayFactory();
700 DownloadManagerForShell(shell())->SetDownloadFileFactoryForTesting( 702 download_manager->SetDownloadFileFactoryForTesting(
701 scoped_ptr<DownloadFileFactory>(file_factory).Pass()); 703 scoped_ptr<DownloadFileFactory>(file_factory).Pass());
702 704
703 // Create a download 705 // Create a download
704 FilePath file(FILE_PATH_LITERAL("download-test.lib")); 706 FilePath file(FILE_PATH_LITERAL("download-test.lib"));
705 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); 707 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file));
706 708
707 // Wait until the first (intermediate file) rename and execute the callback. 709 // Wait until the first (intermediate file) rename and execute the callback.
708 file_factory->WaitForSomeCallback(); 710 file_factory->WaitForSomeCallback();
709 std::vector<base::Closure> callbacks; 711 std::vector<base::Closure> callbacks;
710 file_factory->GetAllDetachCallbacks(&callbacks);
711 ASSERT_TRUE(callbacks.empty());
712 file_factory->GetAllRenameCallbacks(&callbacks); 712 file_factory->GetAllRenameCallbacks(&callbacks);
713 ASSERT_EQ(1u, callbacks.size()); 713 ASSERT_EQ(1u, callbacks.size());
714 callbacks[0].Run(); 714 callbacks[0].Run();
715 callbacks.clear(); 715 callbacks.clear();
716 716
717 // Wait until the second (final) rename callback is posted. 717 // Wait until the second (final) rename callback is posted.
718 file_factory->WaitForSomeCallback(); 718 file_factory->WaitForSomeCallback();
719 file_factory->GetAllDetachCallbacks(&callbacks);
720 ASSERT_TRUE(callbacks.empty());
721 file_factory->GetAllRenameCallbacks(&callbacks); 719 file_factory->GetAllRenameCallbacks(&callbacks);
722 ASSERT_EQ(1u, callbacks.size()); 720 ASSERT_EQ(1u, callbacks.size());
723 721
724 // Call it. 722 // Call it.
725 callbacks[0].Run(); 723 callbacks[0].Run();
726 callbacks.clear(); 724 callbacks.clear();
727 725
728 // Confirm download isn't complete yet. 726 // Confirm download isn't complete yet.
729 std::vector<DownloadItem*> items; 727 std::vector<DownloadItem*> items;
730 DownloadManagerForShell(shell())->GetAllDownloads(&items); 728 DownloadManagerForShell(shell())->GetAllDownloads(&items);
731 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); 729 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState());
732 730
733 // Cancel the download; confirm cancel fails anyway. 731 // Cancel the download; confirm cancel fails anyway.
734 ASSERT_EQ(1u, items.size()); 732 ASSERT_EQ(1u, items.size());
735 items[0]->Cancel(true); 733 items[0]->Cancel(true);
736 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); 734 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState());
737 RunAllPendingInMessageLoop(); 735 RunAllPendingInMessageLoop();
738 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); 736 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState());
739 737
740 // Get the detach callback that should have been produced by the above. 738 MockDownloadItemObserver observer;
741 file_factory->WaitForSomeCallback(); 739 items[0]->AddObserver(&observer);
742 file_factory->GetAllRenameCallbacks(&callbacks); 740 EXPECT_CALL(observer, OnDownloadDestroyed(items[0]));
743 ASSERT_TRUE(callbacks.empty());
744 file_factory->GetAllDetachCallbacks(&callbacks);
745 ASSERT_EQ(1u, callbacks.size());
746 741
747 // Shutdown the download manager. Mostly this is confirming a lack of 742 // Shutdown the download manager. Mostly this is confirming a lack of
748 // crashes. 743 // crashes.
749 DownloadManagerForShell(shell())->Shutdown(); 744 DownloadManagerForShell(shell())->Shutdown();
750
751 // Run the detach callback; shouldn't cause any problems.
752 callbacks[0].Run();
753 callbacks.clear();
754 } 745 }
755 746
756 } // namespace content 747 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/browser/download/download_file.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698