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

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

Issue 148133007: [Downloads] Always call DM::StartDownload() for explicit downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months 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
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 #include "base/callback.h" 5 #include "base/callback.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
8 #include "base/stl_util.h" 9 #include "base/stl_util.h"
9 #include "base/threading/thread.h" 10 #include "base/threading/thread.h"
10 #include "content/browser/byte_stream.h" 11 #include "content/browser/byte_stream.h"
11 #include "content/browser/download/download_create_info.h" 12 #include "content/browser/download/download_create_info.h"
12 #include "content/browser/download/download_file_factory.h" 13 #include "content/browser/download/download_file_factory.h"
13 #include "content/browser/download/download_item_impl.h" 14 #include "content/browser/download/download_item_impl.h"
14 #include "content/browser/download/download_item_impl_delegate.h" 15 #include "content/browser/download/download_item_impl_delegate.h"
15 #include "content/browser/download/download_request_handle.h" 16 #include "content/browser/download/download_request_handle.h"
16 #include "content/browser/download/mock_download_file.h" 17 #include "content/browser/download/mock_download_file.h"
18 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/download_destination_observer.h" 19 #include "content/public/browser/download_destination_observer.h"
18 #include "content/public/browser/download_interrupt_reasons.h" 20 #include "content/public/browser/download_interrupt_reasons.h"
19 #include "content/public/browser/download_url_parameters.h" 21 #include "content/public/browser/download_url_parameters.h"
22 #include "content/public/browser/site_instance.h"
23 #include "content/public/browser/web_contents.h"
20 #include "content/public/common/content_switches.h" 24 #include "content/public/common/content_switches.h"
21 #include "content/public/test/mock_download_item.h" 25 #include "content/public/test/mock_download_item.h"
22 #include "content/public/test/test_browser_thread.h" 26 #include "content/public/test/test_browser_context.h"
27 #include "content/public/test/test_browser_thread_bundle.h"
28 #include "content/public/test/web_contents_tester.h"
23 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
25 31
26 using ::testing::_; 32 using ::testing::_;
27 using ::testing::NiceMock; 33 using ::testing::NiceMock;
28 using ::testing::Property; 34 using ::testing::Property;
29 using ::testing::Return; 35 using ::testing::Return;
30 using ::testing::SaveArg; 36 using ::testing::SaveArg;
31 using ::testing::StrictMock; 37 using ::testing::StrictMock;
32 38
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 class MockRequestHandle : public DownloadRequestHandleInterface { 91 class MockRequestHandle : public DownloadRequestHandleInterface {
86 public: 92 public:
87 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); 93 MOCK_CONST_METHOD0(GetWebContents, WebContents*());
88 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); 94 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*());
89 MOCK_CONST_METHOD0(PauseRequest, void()); 95 MOCK_CONST_METHOD0(PauseRequest, void());
90 MOCK_CONST_METHOD0(ResumeRequest, void()); 96 MOCK_CONST_METHOD0(ResumeRequest, void());
91 MOCK_CONST_METHOD0(CancelRequest, void()); 97 MOCK_CONST_METHOD0(CancelRequest, void());
92 MOCK_CONST_METHOD0(DebugString, std::string()); 98 MOCK_CONST_METHOD0(DebugString, std::string());
93 }; 99 };
94 100
95 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on 101 // Schedules a task to invoke the DownloadFile::RenameCompletionCallback with
96 // the UI thread. Should only be used as the action for 102 // |new_path| on the UI thread. Should only be used as the action for
97 // MockDownloadFile::Rename as follows: 103 // MockDownloadFile::Rename as follows:
104 //
98 // EXPECT_CALL(download_file, Rename*(_,_)) 105 // EXPECT_CALL(download_file, Rename*(_,_))
99 // .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 106 // .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
100 // new_path)); 107 // new_path));
101 ACTION_P2(ScheduleRenameCallback, interrupt_reason, new_path) { 108 ACTION_P2(ScheduleRenameCallback, interrupt_reason, new_path) {
102 BrowserThread::PostTask( 109 BrowserThread::PostTask(
103 BrowserThread::UI, FROM_HERE, 110 BrowserThread::UI, FROM_HERE,
104 base::Bind(arg1, interrupt_reason, new_path)); 111 base::Bind(arg1, interrupt_reason, new_path));
105 } 112 }
106 113
114 // Schedules a task to invoke DownloadFile::InitializedCallback with
115 // |interrupt_reason| on the UI thread. Should be used as the action for
116 // MockDownloadFile::Initialize as follows:
117 // EXPECT_CALL(download_file, Initialize(_))
118 // .WillOnce(ScheduleInitializeCallback(DOWNLOAD_INTERRUPT_REASON_NONE));
119 ACTION_P(ScheduleInitializeCallback, interrupt_reason) {
120 BrowserThread::PostTask(
121 BrowserThread::UI, FROM_HERE, base::Bind(arg0, interrupt_reason));
122 }
123
107 } // namespace 124 } // namespace
108 125
109 class DownloadItemTest : public testing::Test { 126 class DownloadItemTest : public testing::Test {
110 public: 127 public:
111 class MockObserver : public DownloadItem::Observer { 128 class MockObserver : public DownloadItem::Observer {
112 public: 129 public:
113 explicit MockObserver(DownloadItem* item) 130 explicit MockObserver(DownloadItem* item)
114 : item_(item), 131 : item_(item),
115 last_state_(item->GetState()), 132 last_state_(item->GetState()),
116 removed_(false), 133 removed_(false),
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 DownloadItem* item_; 203 DownloadItem* item_;
187 DownloadItem::DownloadState last_state_; 204 DownloadItem::DownloadState last_state_;
188 bool removed_; 205 bool removed_;
189 bool destroyed_; 206 bool destroyed_;
190 bool updated_; 207 bool updated_;
191 int interrupt_count_; 208 int interrupt_count_;
192 int resume_count_; 209 int resume_count_;
193 }; 210 };
194 211
195 DownloadItemTest() 212 DownloadItemTest()
196 : ui_thread_(BrowserThread::UI, &loop_), 213 : delegate_(),
197 file_thread_(BrowserThread::FILE, &loop_), 214 next_download_id_(DownloadItem::kInvalidId + 1) {
198 delegate_() { 215 create_info_.reset(new DownloadCreateInfo());
216 create_info_->save_info =
217 scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo());
218 create_info_->save_info->prompt_for_save_location = false;
219 create_info_->url_chain.push_back(GURL());
220 create_info_->etag = "SomethingToSatisfyResumption";
199 } 221 }
200 222
201 ~DownloadItemTest() { 223 ~DownloadItemTest() {
202 } 224 }
203 225
204 virtual void SetUp() { 226 virtual void SetUp() {
205 } 227 }
206 228
207 virtual void TearDown() { 229 virtual void TearDown() {
208 ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunUntilIdle(); 230 base::RunLoop run_loop;
231 run_loop.RunUntilIdle();
209 STLDeleteElements(&allocated_downloads_); 232 STLDeleteElements(&allocated_downloads_);
210 allocated_downloads_.clear(); 233 allocated_downloads_.clear();
211 } 234 }
212 235
213 // This class keeps ownership of the created download item; it will 236 // This class keeps ownership of the created download item; it will
214 // be torn down at the end of the test unless DestroyDownloadItem is 237 // be torn down at the end of the test unless DestroyDownloadItem is
215 // called. 238 // called.
216 DownloadItemImpl* CreateDownloadItem() { 239 DownloadItemImpl* CreateDownloadItem() {
217 // Normally, the download system takes ownership of info, and is 240 create_info_->download_id = ++next_download_id_;
218 // responsible for deleting it. In these unit tests, however, we 241 DownloadItemImpl* download = new DownloadItemImpl(&delegate_,
219 // don't call the function that deletes it, so we do so ourselves. 242 create_info_->download_id,
220 scoped_ptr<DownloadCreateInfo> info_; 243 *create_info_,
221 244 net::BoundNetLog());
222 info_.reset(new DownloadCreateInfo());
223 static uint32 next_id = DownloadItem::kInvalidId + 1;
224 info_->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo());
225 info_->save_info->prompt_for_save_location = false;
226 info_->url_chain.push_back(GURL());
227 info_->etag = "SomethingToSatisfyResumption";
228
229 DownloadItemImpl* download =
230 new DownloadItemImpl(
231 &delegate_, next_id++, *(info_.get()), net::BoundNetLog());
232 allocated_downloads_.insert(download); 245 allocated_downloads_.insert(download);
233 return download; 246 return download;
234 } 247 }
235 248
236 // Add DownloadFile to DownloadItem 249 // Add DownloadFile to DownloadItem
237 MockDownloadFile* AddDownloadFileToDownloadItem( 250 MockDownloadFile* AddDownloadFileToDownloadItem(
238 DownloadItemImpl* item, 251 DownloadItemImpl* item,
239 DownloadItemImplDelegate::DownloadTargetCallback *callback) { 252 DownloadItemImplDelegate::DownloadTargetCallback *callback) {
240 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); 253 MockDownloadFile* mock_download_file = NULL;
241 scoped_ptr<DownloadFile> download_file(mock_download_file); 254 scoped_ptr<DownloadFile> download_file;
242 EXPECT_CALL(*mock_download_file, Initialize(_));
243 if (callback) { 255 if (callback) {
244 // Save the callback. 256 // Save the callback.
245 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 257 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
246 .WillOnce(SaveArg<1>(callback)); 258 .WillOnce(SaveArg<1>(callback));
247 } else { 259 } else {
248 // Drop it on the floor. 260 // Drop it on the floor.
249 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); 261 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _));
250 } 262 }
251 263
252 scoped_ptr<DownloadRequestHandleInterface> request_handle( 264 // Only create a DownloadFile if the request was successful.
265 if (create_info_->interrupt_reason == DOWNLOAD_INTERRUPT_REASON_NONE) {
266 mock_download_file = new StrictMock<MockDownloadFile>;
267 download_file.reset(mock_download_file);
268 EXPECT_CALL(*mock_download_file, Initialize(_))
269 .WillOnce(ScheduleInitializeCallback(DOWNLOAD_INTERRUPT_REASON_NONE));
270 EXPECT_CALL(*mock_download_file, FullPath())
271 .WillRepeatedly(Return(base::FilePath()));
272 }
273
274 scoped_ptr<MockRequestHandle> request_handle(
253 new NiceMock<MockRequestHandle>); 275 new NiceMock<MockRequestHandle>);
254 item->Start(download_file.Pass(), request_handle.Pass()); 276 EXPECT_CALL(*request_handle, GetWebContents())
255 loop_.RunUntilIdle(); 277 .WillRepeatedly(Return(GetWebContents()));
278 item->Start(download_file.Pass(),
279 request_handle.PassAs<DownloadRequestHandleInterface>(),
280 *create_info_);
281 base::RunLoop().RunUntilIdle();
256 282
257 // So that we don't have a function writing to a stack variable 283 // So that we don't have a function writing to a stack variable
258 // lying around if the above failed. 284 // lying around if the above failed.
259 mock_delegate()->VerifyAndClearExpectations(); 285 mock_delegate()->VerifyAndClearExpectations();
260 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_)) 286 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_))
261 .WillRepeatedly(Return()); 287 .WillRepeatedly(Return());
262 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_)) 288 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_))
263 .WillRepeatedly(Return(false)); 289 .WillRepeatedly(Return(false));
264 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _)) 290 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _))
265 .WillRepeatedly(Return(true)); 291 .WillRepeatedly(Return(true));
(...skipping 26 matching lines...) Expand all
292 // Cleanup a download item (specifically get rid of the DownloadFile on it). 318 // Cleanup a download item (specifically get rid of the DownloadFile on it).
293 // The item must be in the expected state. 319 // The item must be in the expected state.
294 void CleanupItem(DownloadItemImpl* item, 320 void CleanupItem(DownloadItemImpl* item,
295 MockDownloadFile* download_file, 321 MockDownloadFile* download_file,
296 DownloadItem::DownloadState expected_state) { 322 DownloadItem::DownloadState expected_state) {
297 EXPECT_EQ(expected_state, item->GetState()); 323 EXPECT_EQ(expected_state, item->GetState());
298 324
299 if (expected_state == DownloadItem::IN_PROGRESS) { 325 if (expected_state == DownloadItem::IN_PROGRESS) {
300 EXPECT_CALL(*download_file, Cancel()); 326 EXPECT_CALL(*download_file, Cancel());
301 item->Cancel(true); 327 item->Cancel(true);
302 loop_.RunUntilIdle(); 328 base::RunLoop().RunUntilIdle();
303 } 329 }
304 } 330 }
305 331
306 // Destroy a previously created download item. 332 // Destroy a previously created download item.
307 void DestroyDownloadItem(DownloadItem* item) { 333 void DestroyDownloadItem(DownloadItem* item) {
308 allocated_downloads_.erase(item); 334 allocated_downloads_.erase(item);
309 delete item; 335 delete item;
310 } 336 }
311 337
312 void RunAllPendingInMessageLoops() { 338 void RunAllPendingInMessageLoops() {
313 loop_.RunUntilIdle(); 339 base::RunLoop().RunUntilIdle();
314 } 340 }
315 341
316 MockDelegate* mock_delegate() { 342 MockDelegate* mock_delegate() {
317 return &delegate_; 343 return &delegate_;
318 } 344 }
319 345
320 void OnDownloadFileAcquired(base::FilePath* return_path, 346 void OnDownloadFileAcquired(base::FilePath* return_path,
321 const base::FilePath& path) { 347 const base::FilePath& path) {
322 *return_path = path; 348 *return_path = path;
323 } 349 }
324 350
351 DownloadCreateInfo* create_info() {
352 return create_info_.get();
353 }
354
355 virtual WebContents* GetWebContents() {
356 return NULL;
357 }
358
325 private: 359 private:
326 base::MessageLoopForUI loop_;
327 TestBrowserThread ui_thread_; // UI thread
328 TestBrowserThread file_thread_; // FILE thread
329 StrictMock<MockDelegate> delegate_; 360 StrictMock<MockDelegate> delegate_;
330 std::set<DownloadItem*> allocated_downloads_; 361 std::set<DownloadItem*> allocated_downloads_;
362 scoped_ptr<DownloadCreateInfo> create_info_;
363 uint32 next_download_id_;
364 TestBrowserThreadBundle thread_bundle_;
365 };
366
367 // Test fixture for resumption tests. These currently require a WebContents().
368 // Therefore a TestBrowserContext() and a test WebContents() need to be created
369 // for these tests.
370 class DownloadItemTestWithResumption : public DownloadItemTest {
371 public:
372 virtual void SetUp() OVERRIDE {
373 CommandLine::ForCurrentProcess()->AppendSwitch(
374 switches::kEnableDownloadResumption);
375 browser_context_.reset(new TestBrowserContext);
376 scoped_refptr<SiteInstance> site_instance(
377 SiteInstance::Create(browser_context_.get()));
378 web_contents_.reset(WebContentsTester::CreateTestWebContents(
379 browser_context_.get(), site_instance.get()));
380 DownloadItemTest::SetUp();
381 }
382
383 virtual void TearDown() OVERRIDE {
384 web_contents_.reset();
385 browser_context_.reset();
386 DownloadItemTest::TearDown();
387 }
388
389 virtual WebContents* GetWebContents() OVERRIDE {
390 return web_contents_.get();
391 }
392
393 private:
394 scoped_ptr<TestBrowserContext> browser_context_;
395 scoped_ptr<WebContents> web_contents_;
331 }; 396 };
332 397
333 // Tests to ensure calls that change a DownloadItem generate an update to 398 // Tests to ensure calls that change a DownloadItem generate an update to
334 // observers. 399 // observers.
335 // State changing functions not tested: 400 // State changing functions not tested:
336 // void OpenDownload(); 401 // void OpenDownload();
337 // void ShowDownloadInShell(); 402 // void ShowDownloadInShell();
338 // void CompleteDelayedDownload(); 403 // void CompleteDelayedDownload();
339 // set_* mutators 404 // set_* mutators
340 405
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 } 466 }
402 467
403 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { 468 TEST_F(DownloadItemTest, NotificationAfterDestroyed) {
404 DownloadItemImpl* item = CreateDownloadItem(); 469 DownloadItemImpl* item = CreateDownloadItem();
405 MockObserver observer(item); 470 MockObserver observer(item);
406 471
407 DestroyDownloadItem(item); 472 DestroyDownloadItem(item);
408 ASSERT_TRUE(observer.CheckDestroyed()); 473 ASSERT_TRUE(observer.CheckDestroyed());
409 } 474 }
410 475
411 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { 476 // Test that a download is resumed automatcially after a continuable interrupt.
412 CommandLine::ForCurrentProcess()->AppendSwitch( 477 TEST_F(DownloadItemTestWithResumption, ContinueAfterInterrupted) {
413 switches::kEnableDownloadResumption);
414
415 DownloadItemImpl* item = CreateDownloadItem(); 478 DownloadItemImpl* item = CreateDownloadItem();
416 MockObserver observer(item); 479 MockObserver observer(item);
417 DownloadItemImplDelegate::DownloadTargetCallback callback; 480 DownloadItemImplDelegate::DownloadTargetCallback callback;
418 MockDownloadFile* download_file = 481 MockDownloadFile* download_file =
419 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 482 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
420 483
421 // Interrupt the download, using a continuable interrupt. 484 // Interrupt the download, using a continuable interrupt.
422 EXPECT_CALL(*download_file, FullPath()) 485 EXPECT_CALL(*download_file, FullPath())
423 .WillOnce(Return(base::FilePath())); 486 .WillOnce(Return(base::FilePath()));
424 EXPECT_CALL(*download_file, Detach()); 487 EXPECT_CALL(*download_file, Detach());
488 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _));
425 item->DestinationObserverAsWeakPtr()->DestinationError( 489 item->DestinationObserverAsWeakPtr()->DestinationError(
426 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); 490 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
427 ASSERT_TRUE(observer.CheckUpdated()); 491 ASSERT_TRUE(observer.CheckUpdated());
428 // Should attempt to auto-resume. Because we don't have a mock WebContents, 492 // Should attempt to auto-resume. This is verified by the
429 // ResumeInterruptedDownload() will abort early, with another interrupt, 493 // MockResumeInterruptedDownload() test expectation above.
430 // which will be ignored.
431 ASSERT_EQ(1, observer.GetInterruptCount()); 494 ASSERT_EQ(1, observer.GetInterruptCount());
432 ASSERT_EQ(0, observer.GetResumeCount()); 495 ASSERT_EQ(0, observer.GetResumeCount());
433 RunAllPendingInMessageLoops(); 496 RunAllPendingInMessageLoops();
434 497
435 CleanupItem(item, download_file, DownloadItem::INTERRUPTED); 498 CleanupItem(item, download_file, DownloadItem::INTERRUPTED);
436 } 499 }
437 500
438 // Same as above, but with a non-continuable interrupt. 501 // Test that automatic resumption doesn't happen after a non-continuable
439 TEST_F(DownloadItemTest, RestartAfterInterrupted) { 502 // interrupt.
440 CommandLine::ForCurrentProcess()->AppendSwitch( 503 TEST_F(DownloadItemTestWithResumption, RestartAfterInterrupted) {
441 switches::kEnableDownloadResumption);
442
443 DownloadItemImpl* item = CreateDownloadItem(); 504 DownloadItemImpl* item = CreateDownloadItem();
444 MockObserver observer(item); 505 MockObserver observer(item);
445 DownloadItemImplDelegate::DownloadTargetCallback callback; 506 DownloadItemImplDelegate::DownloadTargetCallback callback;
446 MockDownloadFile* download_file = 507 MockDownloadFile* download_file =
447 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 508 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
448 509
449 // Interrupt the download, using a restartable interrupt. 510 // Interrupt the download, using a restartable interrupt.
450 EXPECT_CALL(*download_file, Cancel()); 511 EXPECT_CALL(*download_file, Cancel());
451 item->DestinationObserverAsWeakPtr()->DestinationError( 512 item->DestinationObserverAsWeakPtr()->DestinationError(
452 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 513 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
(...skipping 30 matching lines...) Expand all
483 RunAllPendingInMessageLoops(); 544 RunAllPendingInMessageLoops();
484 545
485 ASSERT_TRUE(observer.CheckUpdated()); 546 ASSERT_TRUE(observer.CheckUpdated());
486 // Should not try to auto-resume. 547 // Should not try to auto-resume.
487 ASSERT_EQ(1, observer.GetInterruptCount()); 548 ASSERT_EQ(1, observer.GetInterruptCount());
488 ASSERT_EQ(0, observer.GetResumeCount()); 549 ASSERT_EQ(0, observer.GetResumeCount());
489 550
490 CleanupItem(item, download_file, DownloadItem::INTERRUPTED); 551 CleanupItem(item, download_file, DownloadItem::INTERRUPTED);
491 } 552 }
492 553
493 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) { 554 // Test that automatic resumption only happens a limited number of times. The
494 CommandLine::ForCurrentProcess()->AppendSwitch( 555 // limit is currently hardcoded as DownloadItemImpl::kMaxAutoResumeAttempts.
495 switches::kEnableDownloadResumption); 556 TEST_F(DownloadItemTestWithResumption, LimitRestartsAfterInterrupted) {
557 DownloadItemImpl* item = CreateDownloadItem();
558 MockObserver observer(item);
559 base::FilePath target_path(kDummyPath);
560 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x"));
561
562 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) {
563 DVLOG(20) << "Loop iteration " << i;
564 DownloadItemImplDelegate::DownloadTargetCallback callback;
565 MockDownloadFile* download_file =
566 AddDownloadFileToDownloadItem(item, &callback);
567 EXPECT_FALSE(callback.is_null());
568 if (i == 0)
569 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
570 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
571 intermediate_path));
572 else
573 EXPECT_EQ(intermediate_path, item->GetFullPath());
574
575 callback.Run(target_path,
576 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
577 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
578 intermediate_path);
579 RunAllPendingInMessageLoops();
580 ASSERT_EQ(i, observer.GetResumeCount());
581 EXPECT_CALL(*download_file, Detach());
582 // FILE_TRANSIENT_ERROR is a continuable interrupt that causes the download
583 // to be resumed automatically. However, it should only attempt to resume if
584 // the number of conesecutive automatic resumptions is less than
585 // kMaxAutoResumeAttempts.
586 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _))
587 .Times(i < DownloadItemImpl::kMaxAutoResumeAttempts ? 1 : 0);
588 item->DestinationObserverAsWeakPtr()->DestinationError(
589 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
590 ASSERT_EQ(i + 1, observer.GetInterruptCount());
591 }
592 EXPECT_EQ(DownloadItemImpl::kMaxAutoResumeAttempts,
593 observer.GetResumeCount());
594 }
595
596 // If the download attempts to resume and the resumption request fails, the
597 // subsequent Start() call shouldn't update the origin state (URL redirect
598 // chains, Content-Disposition, download URL, etc..)
599 TEST_F(DownloadItemTestWithResumption,
600 FailedResumptionDoesntUpdateOriginState) {
601 const char* kContentDisposition = "attachment; filename=foo";
602 const char* kFirstETag = "ABC";
603 const char* kFirstLastModified = "Yesterday";
604 const char* kFirstURL = "http://www.example.com/download";
605 create_info()->content_disposition = kContentDisposition;
606 create_info()->etag = kFirstETag;
607 create_info()->last_modified = kFirstLastModified;
608 create_info()->url_chain.push_back(GURL(kFirstURL));
496 609
497 DownloadItemImpl* item = CreateDownloadItem(); 610 DownloadItemImpl* item = CreateDownloadItem();
498 base::WeakPtr<DownloadDestinationObserver> as_observer( 611 MockDownloadFile* download_file =
499 item->DestinationObserverAsWeakPtr()); 612 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
500 MockObserver observer(item); 613 EXPECT_EQ(kContentDisposition, item->GetContentDisposition());
501 MockDownloadFile* mock_download_file(NULL); 614 EXPECT_EQ(kFirstETag, item->GetETag());
502 scoped_ptr<DownloadFile> download_file; 615 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime());
503 MockRequestHandle* mock_request_handle(NULL); 616 EXPECT_EQ(kFirstURL, item->GetURL().spec());
504 scoped_ptr<DownloadRequestHandleInterface> request_handle;
505 DownloadItemImplDelegate::DownloadTargetCallback callback;
506 617
507 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 618 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _));
508 .WillRepeatedly(SaveArg<1>(&callback)); 619 EXPECT_CALL(*download_file, Detach());
509 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { 620 item->DestinationObserverAsWeakPtr()->DestinationError(
510 DVLOG(20) << "Loop iteration " << i; 621 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
622 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
511 623
512 mock_download_file = new NiceMock<MockDownloadFile>; 624 // Now change the create info. The changes should not cause the DownloadItem
513 download_file.reset(mock_download_file); 625 // to be updated.
514 mock_request_handle = new NiceMock<MockRequestHandle>; 626 const char* kSecondContentDisposition = "attachment; filename=bar";
515 request_handle.reset(mock_request_handle); 627 const char* kSecondETag = "123";
628 const char* kSecondLastModified = "Today";
629 const char* kSecondURL = "http://example.com/another-download";
630 create_info()->content_disposition = kSecondContentDisposition;
631 create_info()->etag = kSecondETag;
632 create_info()->last_modified = kSecondLastModified;
633 create_info()->url_chain.clear();
634 create_info()->url_chain.push_back(GURL(kSecondURL));
635 create_info()->interrupt_reason = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED;
516 636
517 ON_CALL(*mock_download_file, FullPath()) 637 // The following ends up calling DownloadItem::Start(), but shouldn't result
518 .WillByDefault(Return(base::FilePath())); 638 // in an origin update.
639 DownloadItemImplDelegate::DownloadTargetCallback download_target_callback;
640 download_file =
641 AddDownloadFileToDownloadItem(item, &download_target_callback);
519 642
520 // It's too complicated to set up a WebContents instance that would cause 643 EXPECT_EQ(kContentDisposition, item->GetContentDisposition());
521 // the MockDownloadItemDelegate's ResumeInterruptedDownload() function 644 EXPECT_EQ(kFirstETag, item->GetETag());
522 // to be callled, so we simply verify that GetWebContents() is called. 645 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime());
523 if (i < (DownloadItemImpl::kMaxAutoResumeAttempts - 1)) { 646 EXPECT_EQ(kFirstURL, item->GetURL().spec());
524 EXPECT_CALL(*mock_request_handle, GetWebContents()) 647 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
525 .WillRepeatedly(Return(static_cast<WebContents*>(NULL))); 648 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason());
526 }
527
528 // Copied key parts of DoIntermediateRename & AddDownloadFileToDownloadItem
529 // to allow for holding onto the request handle.
530 item->Start(download_file.Pass(), request_handle.Pass());
531 RunAllPendingInMessageLoops();
532 if (i == 0) {
533 // Target determination is only done the first time through.
534 base::FilePath target_path(kDummyPath);
535 base::FilePath intermediate_path(
536 target_path.InsertBeforeExtensionASCII("x"));
537 EXPECT_CALL(*mock_download_file, RenameAndUniquify(intermediate_path, _))
538 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
539 intermediate_path));
540 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
541 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
542 RunAllPendingInMessageLoops();
543 }
544 ASSERT_EQ(i, observer.GetResumeCount());
545
546 // Use a continuable interrupt.
547 item->DestinationObserverAsWeakPtr()->DestinationError(
548 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
549
550 ASSERT_EQ(i + 1, observer.GetInterruptCount());
551 ::testing::Mock::VerifyAndClearExpectations(mock_download_file);
552 }
553
554 CleanupItem(item, mock_download_file, DownloadItem::INTERRUPTED);
555 } 649 }
556 650
557 TEST_F(DownloadItemTest, NotificationAfterRemove) { 651 TEST_F(DownloadItemTest, NotificationAfterRemove) {
558 DownloadItemImpl* item = CreateDownloadItem(); 652 DownloadItemImpl* item = CreateDownloadItem();
559 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); 653 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL);
560 EXPECT_CALL(*download_file, Cancel()); 654 EXPECT_CALL(*download_file, Cancel());
561 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); 655 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
562 MockObserver observer(item); 656 MockObserver observer(item);
563 657
564 item->Remove(); 658 item->Remove();
565 ASSERT_TRUE(observer.CheckUpdated()); 659 ASSERT_TRUE(observer.CheckUpdated());
566 ASSERT_TRUE(observer.CheckRemoved()); 660 ASSERT_TRUE(observer.CheckRemoved());
567 } 661 }
568 662
569 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { 663 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) {
570 // Setting to NOT_DANGEROUS does not trigger a notification. 664 // Setting to NOT_DANGEROUS does not trigger a notification.
571 DownloadItemImpl* safe_item = CreateDownloadItem(); 665 DownloadItemImpl* safe_item = CreateDownloadItem();
572 MockObserver safe_observer(safe_item); 666 MockObserver safe_observer(safe_item);
573 667
574 safe_item->OnAllDataSaved(std::string()); 668 safe_item->OnAllDataSaved(std::string());
575 EXPECT_TRUE(safe_observer.CheckUpdated()); 669 EXPECT_TRUE(safe_observer.CheckUpdated());
576 safe_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 670 safe_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
577 EXPECT_TRUE(safe_observer.CheckUpdated()); 671 EXPECT_TRUE(safe_observer.CheckUpdated());
578 672
579 // Setting to unsafe url or unsafe file should trigger a notification. 673 // Setting to unsafe url or unsafe file should trigger a notification.
580 DownloadItemImpl* unsafeurl_item = 674 DownloadItemImpl* unsafeurl_item = CreateDownloadItem();
581 CreateDownloadItem();
582 MockObserver unsafeurl_observer(unsafeurl_item); 675 MockObserver unsafeurl_observer(unsafeurl_item);
583 676
584 unsafeurl_item->OnAllDataSaved(std::string()); 677 unsafeurl_item->OnAllDataSaved(std::string());
585 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); 678 EXPECT_TRUE(unsafeurl_observer.CheckUpdated());
586 unsafeurl_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); 679 unsafeurl_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_URL);
587 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); 680 EXPECT_TRUE(unsafeurl_observer.CheckUpdated());
588 681
589 unsafeurl_item->ValidateDangerousDownload(); 682 unsafeurl_item->ValidateDangerousDownload();
590 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); 683 EXPECT_TRUE(unsafeurl_observer.CheckUpdated());
591 684
592 DownloadItemImpl* unsafefile_item = 685 DownloadItemImpl* unsafefile_item = CreateDownloadItem();
593 CreateDownloadItem();
594 MockObserver unsafefile_observer(unsafefile_item); 686 MockObserver unsafefile_observer(unsafefile_item);
595 687
596 unsafefile_item->OnAllDataSaved(std::string()); 688 unsafefile_item->OnAllDataSaved(std::string());
597 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); 689 EXPECT_TRUE(unsafefile_observer.CheckUpdated());
598 unsafefile_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 690 unsafefile_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
599 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); 691 EXPECT_TRUE(unsafefile_observer.CheckUpdated());
600 692
601 unsafefile_item->ValidateDangerousDownload(); 693 unsafefile_item->ValidateDangerousDownload();
602 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); 694 EXPECT_TRUE(unsafefile_observer.CheckUpdated());
603 } 695 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { 728 TEST_F(DownloadItemTest, NotificationAfterTogglePause) {
637 DownloadItemImpl* item = CreateDownloadItem(); 729 DownloadItemImpl* item = CreateDownloadItem();
638 MockObserver observer(item); 730 MockObserver observer(item);
639 MockDownloadFile* mock_download_file(new MockDownloadFile); 731 MockDownloadFile* mock_download_file(new MockDownloadFile);
640 scoped_ptr<DownloadFile> download_file(mock_download_file); 732 scoped_ptr<DownloadFile> download_file(mock_download_file);
641 scoped_ptr<DownloadRequestHandleInterface> request_handle( 733 scoped_ptr<DownloadRequestHandleInterface> request_handle(
642 new NiceMock<MockRequestHandle>); 734 new NiceMock<MockRequestHandle>);
643 735
644 EXPECT_CALL(*mock_download_file, Initialize(_)); 736 EXPECT_CALL(*mock_download_file, Initialize(_));
645 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)); 737 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _));
646 item->Start(download_file.Pass(), request_handle.Pass()); 738 item->Start(download_file.Pass(), request_handle.Pass(), *create_info());
647 739
648 item->Pause(); 740 item->Pause();
649 ASSERT_TRUE(observer.CheckUpdated()); 741 ASSERT_TRUE(observer.CheckUpdated());
650 742
651 ASSERT_TRUE(item->IsPaused()); 743 ASSERT_TRUE(item->IsPaused());
652 744
653 item->Resume(); 745 item->Resume();
654 ASSERT_TRUE(observer.CheckUpdated()); 746 ASSERT_TRUE(observer.CheckUpdated());
655 747
656 RunAllPendingInMessageLoops(); 748 RunAllPendingInMessageLoops();
(...skipping 26 matching lines...) Expand all
683 775
684 // Test to make sure that Start method calls DF initialize properly. 776 // Test to make sure that Start method calls DF initialize properly.
685 TEST_F(DownloadItemTest, Start) { 777 TEST_F(DownloadItemTest, Start) {
686 MockDownloadFile* mock_download_file(new MockDownloadFile); 778 MockDownloadFile* mock_download_file(new MockDownloadFile);
687 scoped_ptr<DownloadFile> download_file(mock_download_file); 779 scoped_ptr<DownloadFile> download_file(mock_download_file);
688 DownloadItemImpl* item = CreateDownloadItem(); 780 DownloadItemImpl* item = CreateDownloadItem();
689 EXPECT_CALL(*mock_download_file, Initialize(_)); 781 EXPECT_CALL(*mock_download_file, Initialize(_));
690 scoped_ptr<DownloadRequestHandleInterface> request_handle( 782 scoped_ptr<DownloadRequestHandleInterface> request_handle(
691 new NiceMock<MockRequestHandle>); 783 new NiceMock<MockRequestHandle>);
692 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); 784 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _));
693 item->Start(download_file.Pass(), request_handle.Pass()); 785 item->Start(download_file.Pass(), request_handle.Pass(), *create_info());
694 RunAllPendingInMessageLoops(); 786 RunAllPendingInMessageLoops();
695 787
696 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); 788 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS);
697 } 789 }
698 790
791 // Handling of downloads initiated via a failed request. In this case, Start()
792 // will get called with a DownloadCreateInfo with a non-zero interrupt_reason.
793 TEST_F(DownloadItemTest, StartFailedDownload) {
794 create_info()->interrupt_reason = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED;
795 DownloadItemImpl* item = CreateDownloadItem();
796 // The download is considered to be IN_PROGRESS even though there's an
797 // interrupt reason. It will transition to INTERRUPTED once Start() is called.
798 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
799
800 // DownloadFile and DownloadRequestHandleInterface objects aren't created for
801 // failed downloads.
802 scoped_ptr<DownloadFile> null_download_file;
803 scoped_ptr<DownloadRequestHandleInterface> null_request_handle;
804 DownloadItemImplDelegate::DownloadTargetCallback download_target_callback;
805 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
806 .WillOnce(SaveArg<1>(&download_target_callback));
807 item->Start(
808 null_download_file.Pass(), null_request_handle.Pass(), *create_info());
809 RunAllPendingInMessageLoops();
810
811 // The DownloadItemImpl should attempt to determine a target path even if the
812 // download was interrupted.
813 ASSERT_FALSE(download_target_callback.is_null());
814 ASSERT_EQ(DownloadItem::INTERRUPTED, item->GetState());
815 base::FilePath target_path(FILE_PATH_LITERAL("foo"));
816 download_target_callback.Run(target_path,
817 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
818 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
819 target_path);
820 RunAllPendingInMessageLoops();
821
822 EXPECT_EQ(target_path, item->GetTargetFilePath());
823 CleanupItem(item, NULL, DownloadItem::INTERRUPTED);
824 }
825
699 // Test that the delegate is invoked after the download file is renamed. 826 // Test that the delegate is invoked after the download file is renamed.
700 TEST_F(DownloadItemTest, CallbackAfterRename) { 827 TEST_F(DownloadItemTest, CallbackAfterRename) {
701 DownloadItemImpl* item = CreateDownloadItem(); 828 DownloadItemImpl* item = CreateDownloadItem();
702 DownloadItemImplDelegate::DownloadTargetCallback callback; 829 DownloadItemImplDelegate::DownloadTargetCallback callback;
703 MockDownloadFile* download_file = 830 MockDownloadFile* download_file =
704 AddDownloadFileToDownloadItem(item, &callback); 831 AddDownloadFileToDownloadItem(item, &callback);
705 base::FilePath final_path(base::FilePath(kDummyPath).AppendASCII("foo.bar")); 832 base::FilePath final_path(base::FilePath(kDummyPath).AppendASCII("foo.bar"));
706 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 833 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
707 base::FilePath new_intermediate_path( 834 base::FilePath new_intermediate_path(
708 final_path.InsertBeforeExtensionASCII("y")); 835 final_path.InsertBeforeExtensionASCII("y"));
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
805 ::testing::Mock::VerifyAndClearExpectations(download_file); 932 ::testing::Mock::VerifyAndClearExpectations(download_file);
806 mock_delegate()->VerifyAndClearExpectations(); 933 mock_delegate()->VerifyAndClearExpectations();
807 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 934 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
808 EXPECT_TRUE(item->GetFullPath().empty()); 935 EXPECT_TRUE(item->GetFullPath().empty());
809 EXPECT_EQ(final_path, item->GetTargetFilePath()); 936 EXPECT_EQ(final_path, item->GetTargetFilePath());
810 } 937 }
811 938
812 // As above. But if the download can be resumed by continuing, then the 939 // As above. But if the download can be resumed by continuing, then the
813 // intermediate path should be retained when the download is interrupted after 940 // intermediate path should be retained when the download is interrupted after
814 // the intermediate rename succeeds. 941 // the intermediate rename succeeds.
815 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename_Continue) { 942 TEST_F(DownloadItemTestWithResumption,
816 CommandLine::ForCurrentProcess()->AppendSwitch( 943 InterruptedBeforeIntermediateRename_Continue) {
817 switches::kEnableDownloadResumption);
818 DownloadItemImpl* item = CreateDownloadItem(); 944 DownloadItemImpl* item = CreateDownloadItem();
819 DownloadItemImplDelegate::DownloadTargetCallback callback; 945 DownloadItemImplDelegate::DownloadTargetCallback callback;
820 MockDownloadFile* download_file = 946 MockDownloadFile* download_file =
821 AddDownloadFileToDownloadItem(item, &callback); 947 AddDownloadFileToDownloadItem(item, &callback);
822 item->DestinationObserverAsWeakPtr()->DestinationError( 948 item->DestinationObserverAsWeakPtr()->DestinationError(
823 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED); 949 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED);
824 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 950 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
825 951
826 base::FilePath final_path(base::FilePath(kDummyPath).AppendASCII("foo.bar")); 952 base::FilePath final_path(base::FilePath(kDummyPath).AppendASCII("foo.bar"));
827 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 953 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
(...skipping 12 matching lines...) Expand all
840 // All the callbacks should have happened by now. 966 // All the callbacks should have happened by now.
841 ::testing::Mock::VerifyAndClearExpectations(download_file); 967 ::testing::Mock::VerifyAndClearExpectations(download_file);
842 mock_delegate()->VerifyAndClearExpectations(); 968 mock_delegate()->VerifyAndClearExpectations();
843 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 969 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
844 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); 970 EXPECT_EQ(new_intermediate_path, item->GetFullPath());
845 EXPECT_EQ(final_path, item->GetTargetFilePath()); 971 EXPECT_EQ(final_path, item->GetTargetFilePath());
846 } 972 }
847 973
848 // As above. If the intermediate rename fails, then the interrupt reason should 974 // As above. If the intermediate rename fails, then the interrupt reason should
849 // be set to the destination error and the intermediate path should be empty. 975 // be set to the destination error and the intermediate path should be empty.
850 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename_Failed) { 976 TEST_F(DownloadItemTestWithResumption,
851 CommandLine::ForCurrentProcess()->AppendSwitch( 977 InterruptedBeforeIntermediateRename_Failed) {
852 switches::kEnableDownloadResumption);
853 DownloadItemImpl* item = CreateDownloadItem(); 978 DownloadItemImpl* item = CreateDownloadItem();
854 DownloadItemImplDelegate::DownloadTargetCallback callback; 979 DownloadItemImplDelegate::DownloadTargetCallback callback;
855 MockDownloadFile* download_file = 980 MockDownloadFile* download_file =
856 AddDownloadFileToDownloadItem(item, &callback); 981 AddDownloadFileToDownloadItem(item, &callback);
857 item->DestinationObserverAsWeakPtr()->DestinationError( 982 item->DestinationObserverAsWeakPtr()->DestinationError(
858 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED); 983 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED);
859 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 984 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
860 985
861 base::FilePath final_path(base::FilePath(kDummyPath).AppendASCII("foo.bar")); 986 base::FilePath final_path(base::FilePath(kDummyPath).AppendASCII("foo.bar"));
862 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 987 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
1240 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); 1365 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
1241 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); 1366 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1242 item->StealDangerousDownload( 1367 item->StealDangerousDownload(
1243 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, 1368 base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1244 weak_ptr_factory.GetWeakPtr(), 1369 weak_ptr_factory.GetWeakPtr(),
1245 base::Unretained(&returned_path))); 1370 base::Unretained(&returned_path)));
1246 RunAllPendingInMessageLoops(); 1371 RunAllPendingInMessageLoops();
1247 EXPECT_EQ(full_path, returned_path); 1372 EXPECT_EQ(full_path, returned_path);
1248 } 1373 }
1249 1374
1250 TEST_F(DownloadItemTest, StealInterruptedDangerousDownload) { 1375 TEST_F(DownloadItemTestWithResumption, StealInterruptedDangerousDownload) {
1251 CommandLine::ForCurrentProcess()->AppendSwitch(
1252 switches::kEnableDownloadResumption);
1253 base::FilePath returned_path; 1376 base::FilePath returned_path;
1254 DownloadItemImpl* item = CreateDownloadItem(); 1377 DownloadItemImpl* item = CreateDownloadItem();
1255 MockDownloadFile* download_file = 1378 MockDownloadFile* download_file =
1256 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 1379 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1257 base::FilePath full_path = item->GetFullPath(); 1380 base::FilePath full_path = item->GetFullPath();
1258 EXPECT_FALSE(full_path.empty()); 1381 EXPECT_FALSE(full_path.empty());
1259 EXPECT_CALL(*download_file, FullPath()) 1382 EXPECT_CALL(*download_file, FullPath())
1260 .WillOnce(Return(full_path)); 1383 .WillOnce(Return(full_path));
1261 EXPECT_CALL(*download_file, Detach()); 1384 EXPECT_CALL(*download_file, Detach());
1262 item->DestinationObserverAsWeakPtr()->DestinationError( 1385 item->DestinationObserverAsWeakPtr()->DestinationError(
1263 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED); 1386 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED);
1264 ASSERT_TRUE(item->IsDangerous()); 1387 ASSERT_TRUE(item->IsDangerous());
1265 1388
1266 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); 1389 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
1267 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); 1390 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1268 item->StealDangerousDownload( 1391 item->StealDangerousDownload(
1269 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, 1392 base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1270 weak_ptr_factory.GetWeakPtr(), 1393 weak_ptr_factory.GetWeakPtr(),
1271 base::Unretained(&returned_path))); 1394 base::Unretained(&returned_path)));
1272 RunAllPendingInMessageLoops(); 1395 RunAllPendingInMessageLoops();
1273 EXPECT_EQ(full_path, returned_path); 1396 EXPECT_EQ(full_path, returned_path);
1274 } 1397 }
1275 1398
1276 TEST_F(DownloadItemTest, StealInterruptedNonResumableDangerousDownload) { 1399 TEST_F(DownloadItemTestWithResumption,
1277 CommandLine::ForCurrentProcess()->AppendSwitch( 1400 StealInterruptedNonResumableDangerousDownload) {
1278 switches::kEnableDownloadResumption);
1279 base::FilePath returned_path; 1401 base::FilePath returned_path;
1280 DownloadItemImpl* item = CreateDownloadItem(); 1402 DownloadItemImpl* item = CreateDownloadItem();
1281 MockDownloadFile* download_file = 1403 MockDownloadFile* download_file =
1282 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 1404 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1283 EXPECT_CALL(*download_file, Cancel()); 1405 EXPECT_CALL(*download_file, Cancel());
1284 item->DestinationObserverAsWeakPtr()->DestinationError( 1406 item->DestinationObserverAsWeakPtr()->DestinationError(
1285 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 1407 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
1286 ASSERT_TRUE(item->IsDangerous()); 1408 ASSERT_TRUE(item->IsDangerous());
1287 1409
1288 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); 1410 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
1289 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); 1411 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1290 item->StealDangerousDownload( 1412 item->StealDangerousDownload(
1291 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, 1413 base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1292 weak_ptr_factory.GetWeakPtr(), 1414 weak_ptr_factory.GetWeakPtr(),
1293 base::Unretained(&returned_path))); 1415 base::Unretained(&returned_path)));
1294 RunAllPendingInMessageLoops(); 1416 RunAllPendingInMessageLoops();
1295 EXPECT_TRUE(returned_path.empty()); 1417 EXPECT_TRUE(returned_path.empty());
1296 } 1418 }
1297 1419
1298 TEST(MockDownloadItem, Compiles) { 1420 TEST(MockDownloadItem, Compiles) {
1299 MockDownloadItem mock_item; 1421 MockDownloadItem mock_item;
1300 } 1422 }
1301 1423
1302 } // namespace content 1424 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698