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

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

Issue 1751603002: [Downloads] Rework how hashes are calculated for download files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase on top of https://codereview.chromium.org/1781983002 since that's going in first. Created 4 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
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 "content/browser/download/download_item_impl.h" 5 #include "content/browser/download/download_item_impl.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <iterator> 9 #include <iterator>
10 #include <queue> 10 #include <queue>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/feature_list.h" 14 #include "base/feature_list.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/run_loop.h" 16 #include "base/run_loop.h"
17 #include "base/stl_util.h" 17 #include "base/stl_util.h"
18 #include "base/threading/thread.h" 18 #include "base/threading/thread.h"
19 #include "content/browser/byte_stream.h" 19 #include "content/browser/byte_stream.h"
20 #include "content/browser/download/download_create_info.h" 20 #include "content/browser/download/download_create_info.h"
21 #include "content/browser/download/download_destination_observer.h"
21 #include "content/browser/download/download_file_factory.h" 22 #include "content/browser/download/download_file_factory.h"
22 #include "content/browser/download/download_item_impl_delegate.h" 23 #include "content/browser/download/download_item_impl_delegate.h"
23 #include "content/browser/download/download_request_handle.h" 24 #include "content/browser/download/download_request_handle.h"
24 #include "content/browser/download/mock_download_file.h" 25 #include "content/browser/download/mock_download_file.h"
25 #include "content/public/browser/browser_thread.h" 26 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/download_destination_observer.h"
27 #include "content/public/browser/download_interrupt_reasons.h" 27 #include "content/public/browser/download_interrupt_reasons.h"
28 #include "content/public/browser/download_url_parameters.h" 28 #include "content/public/browser/download_url_parameters.h"
29 #include "content/public/common/content_features.h" 29 #include "content/public/common/content_features.h"
30 #include "content/public/test/mock_download_item.h" 30 #include "content/public/test/mock_download_item.h"
31 #include "content/public/test/mock_download_item.h" 31 #include "content/public/test/mock_download_item.h"
32 #include "content/public/test/test_browser_context.h" 32 #include "content/public/test/test_browser_context.h"
33 #include "content/public/test/test_browser_context.h" 33 #include "content/public/test/test_browser_context.h"
34 #include "content/public/test/test_browser_thread_bundle.h" 34 #include "content/public/test/test_browser_thread_bundle.h"
35 #include "content/public/test/web_contents_tester.h" 35 #include "content/public/test/web_contents_tester.h"
36 #include "crypto/secure_hash.h"
36 #include "testing/gmock/include/gmock/gmock.h" 37 #include "testing/gmock/include/gmock/gmock.h"
37 #include "testing/gtest/include/gtest/gtest.h" 38 #include "testing/gtest/include/gtest/gtest.h"
38 39
39 using ::testing::DoAll; 40 using ::testing::DoAll;
40 using ::testing::NiceMock; 41 using ::testing::NiceMock;
41 using ::testing::Property; 42 using ::testing::Property;
42 using ::testing::Return; 43 using ::testing::Return;
44 using ::testing::ReturnRefOfCopy;
43 using ::testing::SaveArg; 45 using ::testing::SaveArg;
44 using ::testing::StrictMock; 46 using ::testing::StrictMock;
45 using ::testing::WithArg; 47 using ::testing::WithArg;
46 using ::testing::_; 48 using ::testing::_;
47 49
48 const int kDownloadChunkSize = 1000; 50 const int kDownloadChunkSize = 1000;
49 const int kDownloadSpeed = 1000; 51 const int kDownloadSpeed = 1000;
50 const base::FilePath::CharType kDummyTargetPath[] = 52 const base::FilePath::CharType kDummyTargetPath[] =
51 FILE_PATH_LITERAL("/testpath"); 53 FILE_PATH_LITERAL("/testpath");
52 const base::FilePath::CharType kDummyIntermediatePath[] = 54 const base::FilePath::CharType kDummyIntermediatePath[] =
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 DownloadItem::DownloadState last_state_; 180 DownloadItem::DownloadState last_state_;
179 bool removed_; 181 bool removed_;
180 bool destroyed_; 182 bool destroyed_;
181 bool updated_; 183 bool updated_;
182 int interrupt_count_; 184 int interrupt_count_;
183 int resume_count_; 185 int resume_count_;
184 }; 186 };
185 187
186 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on 188 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on
187 // the UI thread. Should only be used as the action for 189 // the UI thread. Should only be used as the action for
188 // MockDownloadFile::Rename as follows: 190 // MockDownloadFile::RenameAndUniquify as follows:
189 // EXPECT_CALL(download_file, Rename*(_,_)) 191 // EXPECT_CALL(download_file, RenameAndUniquify(_,_))
190 // .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 192 // .WillOnce(ScheduleRenameAndUniquifyCallback(
191 // new_path)); 193 // DOWNLOAD_INTERRUPT_REASON_NONE, new_path));
192 ACTION_P2(ScheduleRenameCallback, interrupt_reason, new_path) { 194 ACTION_P2(ScheduleRenameAndUniquifyCallback, interrupt_reason, new_path) {
193 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 195 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
194 base::Bind(arg1, interrupt_reason, new_path)); 196 base::Bind(arg1, interrupt_reason, new_path));
195 } 197 }
196 198
199 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on
200 // the UI thread. Should only be used as the action for
201 // MockDownloadFile::RenameAndAnnotate as follows:
202 // EXPECT_CALL(download_file, RenameAndAnnotate(_,_,_,_,_))
203 // .WillOnce(ScheduleRenameAndAnnotateCallback(
204 // DOWNLOAD_INTERRUPT_REASON_NONE, new_path));
205 ACTION_P2(ScheduleRenameAndAnnotateCallback, interrupt_reason, new_path) {
206 BrowserThread::PostTask(BrowserThread::UI,
207 FROM_HERE,
208 base::Bind(arg4, interrupt_reason, new_path));
209 }
210
197 // Schedules a task to invoke a callback that's bound to the specified 211 // Schedules a task to invoke a callback that's bound to the specified
198 // parameter. 212 // parameter.
199 // E.g.: 213 // E.g.:
200 // 214 //
201 // EXPECT_CALL(foo, Bar(1, _)) 215 // EXPECT_CALL(foo, Bar(1, _))
202 // .WithArg<1>(ScheduleCallbackWithParam(0)); 216 // .WithArg<1>(ScheduleCallbackWithParam(0));
203 // 217 //
204 // .. will invoke the second argument to Bar with 0 as the parameter. 218 // .. will invoke the second argument to Bar with 0 as the parameter.
205 ACTION_P(ScheduleCallbackWithParam, param) { 219 ACTION_P(ScheduleCallbackWithParam, param) {
206 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 220 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
207 base::Bind(arg0, param)); 221 base::Bind(arg0, param));
208 } 222 }
209 223
224 // Schedules a task to invoke a closure.
225 // E.g.:
226 //
227 // EXPECT_CALL(foo, Bar(1, _))
228 // .WillOnce(ScheduleClosure(some_closure));
210 ACTION_P(ScheduleClosure, closure) { 229 ACTION_P(ScheduleClosure, closure) {
211 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); 230 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure);
212 } 231 }
213 232
233 const char kTestData1[] = {'M', 'a', 'r', 'y', ' ', 'h', 'a', 'd',
234 ' ', 'a', ' ', 'l', 'i', 't', 't', 'l',
235 'e', ' ', 'l', 'a', 'm', 'b', '.'};
236
237 // SHA256 hash of TestData1
238 const uint8_t kHashOfTestData1[] = {
239 0xd2, 0xfc, 0x16, 0xa1, 0xf5, 0x1a, 0x65, 0x3a, 0xa0, 0x19, 0x64,
240 0xef, 0x9c, 0x92, 0x33, 0x36, 0xe1, 0x06, 0x53, 0xfe, 0xc1, 0x95,
241 0xf4, 0x93, 0x45, 0x8b, 0x3b, 0x21, 0x89, 0x0e, 0x1b, 0x97};
242
214 } // namespace 243 } // namespace
215 244
216 class DownloadItemTest : public testing::Test { 245 class DownloadItemTest : public testing::Test {
217 public: 246 public:
218 DownloadItemTest() 247 DownloadItemTest()
219 : delegate_(), next_download_id_(DownloadItem::kInvalidId + 1) { 248 : delegate_(), next_download_id_(DownloadItem::kInvalidId + 1) {
220 base::FeatureList::ClearInstanceForTesting(); 249 base::FeatureList::ClearInstanceForTesting();
221 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList); 250 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList);
222 feature_list->InitializeFromCommandLine(features::kDownloadResumption.name, 251 feature_list->InitializeFromCommandLine(features::kDownloadResumption.name,
223 std::string()); 252 std::string());
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)).Times(0); 299 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)).Times(0);
271 } 300 }
272 301
273 // Only create a DownloadFile if the request was successful. 302 // Only create a DownloadFile if the request was successful.
274 if (create_info_->result == DOWNLOAD_INTERRUPT_REASON_NONE) { 303 if (create_info_->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
275 mock_download_file = new StrictMock<MockDownloadFile>; 304 mock_download_file = new StrictMock<MockDownloadFile>;
276 download_file.reset(mock_download_file); 305 download_file.reset(mock_download_file);
277 EXPECT_CALL(*mock_download_file, Initialize(_)) 306 EXPECT_CALL(*mock_download_file, Initialize(_))
278 .WillOnce(ScheduleCallbackWithParam(DOWNLOAD_INTERRUPT_REASON_NONE)); 307 .WillOnce(ScheduleCallbackWithParam(DOWNLOAD_INTERRUPT_REASON_NONE));
279 EXPECT_CALL(*mock_download_file, FullPath()) 308 EXPECT_CALL(*mock_download_file, FullPath())
280 .WillRepeatedly(Return(base::FilePath())); 309 .WillRepeatedly(ReturnRefOfCopy(base::FilePath()));
281 } 310 }
282 311
283 scoped_ptr<MockRequestHandle> request_handle( 312 scoped_ptr<MockRequestHandle> request_handle(
284 new NiceMock<MockRequestHandle>); 313 new NiceMock<MockRequestHandle>);
285 item->Start(std::move(download_file), std::move(request_handle), 314 item->Start(std::move(download_file), std::move(request_handle),
286 *create_info_); 315 *create_info_);
287 RunAllPendingInMessageLoops(); 316 RunAllPendingInMessageLoops();
288 317
289 // So that we don't have a function writing to a stack variable 318 // So that we don't have a function writing to a stack variable
290 // lying around if the above failed. 319 // lying around if the above failed.
(...skipping 13 matching lines...) Expand all
304 // that was added to the DownloadItem. 333 // that was added to the DownloadItem.
305 MockDownloadFile* DoIntermediateRename(DownloadItemImpl* item, 334 MockDownloadFile* DoIntermediateRename(DownloadItemImpl* item,
306 DownloadDangerType danger_type) { 335 DownloadDangerType danger_type) {
307 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 336 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
308 EXPECT_TRUE(item->GetTargetFilePath().empty()); 337 EXPECT_TRUE(item->GetTargetFilePath().empty());
309 DownloadItemImplDelegate::DownloadTargetCallback callback; 338 DownloadItemImplDelegate::DownloadTargetCallback callback;
310 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 339 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
311 base::FilePath target_path(kDummyTargetPath); 340 base::FilePath target_path(kDummyTargetPath);
312 base::FilePath intermediate_path(kDummyIntermediatePath); 341 base::FilePath intermediate_path(kDummyIntermediatePath);
313 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 342 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
314 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 343 .WillOnce(ScheduleRenameAndUniquifyCallback(
315 intermediate_path)); 344 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path));
316 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 345 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
317 danger_type, intermediate_path); 346 danger_type, intermediate_path);
318 RunAllPendingInMessageLoops(); 347 RunAllPendingInMessageLoops();
319 return download_file; 348 return download_file;
320 } 349 }
321 350
322 void DoDestinationComplete(DownloadItemImpl* item, 351 void DoDestinationComplete(DownloadItemImpl* item,
323 MockDownloadFile* download_file) { 352 MockDownloadFile* download_file) {
324 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) 353 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _))
325 .WillOnce(Return(true)); 354 .WillOnce(Return(true));
326 base::FilePath final_path(kDummyTargetPath); 355 base::FilePath final_path(kDummyTargetPath);
327 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) 356 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _))
328 .WillOnce( 357 .WillOnce(ScheduleRenameAndAnnotateCallback(
329 ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, final_path)); 358 DOWNLOAD_INTERRUPT_REASON_NONE, final_path));
330 EXPECT_CALL(*download_file, FullPath()) 359 EXPECT_CALL(*download_file, FullPath())
331 .WillRepeatedly(Return(base::FilePath(kDummyTargetPath))); 360 .WillRepeatedly(ReturnRefOfCopy(base::FilePath(kDummyTargetPath)));
332 EXPECT_CALL(*download_file, Detach()); 361 EXPECT_CALL(*download_file, Detach());
333 362
334 item->DestinationObserverAsWeakPtr()->DestinationCompleted(""); 363 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
364 0, scoped_ptr<crypto::SecureHash>());
335 RunAllPendingInMessageLoops(); 365 RunAllPendingInMessageLoops();
336 } 366 }
337 367
338 // Cleanup a download item (specifically get rid of the DownloadFile on it). 368 // Cleanup a download item (specifically get rid of the DownloadFile on it).
339 // The item must be in the expected state. 369 // The item must be in the expected state.
340 void CleanupItem(DownloadItemImpl* item, 370 void CleanupItem(DownloadItemImpl* item,
341 MockDownloadFile* download_file, 371 MockDownloadFile* download_file,
342 DownloadItem::DownloadState expected_state) { 372 DownloadItem::DownloadState expected_state) {
343 EXPECT_EQ(expected_state, item->GetState()); 373 EXPECT_EQ(expected_state, item->GetState());
344 374
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 // void CompleteDelayedDownload(); 418 // void CompleteDelayedDownload();
389 // set_* mutators 419 // set_* mutators
390 420
391 TEST_F(DownloadItemTest, NotificationAfterUpdate) { 421 TEST_F(DownloadItemTest, NotificationAfterUpdate) {
392 DownloadItemImpl* item = CreateDownloadItem(); 422 DownloadItemImpl* item = CreateDownloadItem();
393 MockDownloadFile* file = 423 MockDownloadFile* file =
394 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 424 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
395 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 425 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
396 TestDownloadItemObserver observer(item); 426 TestDownloadItemObserver observer(item);
397 427
398 item->DestinationUpdate(kDownloadChunkSize, kDownloadSpeed, std::string()); 428 item->DestinationUpdate(kDownloadChunkSize, kDownloadSpeed);
399 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 429 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
400 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed()); 430 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed());
401 CleanupItem(item, file, DownloadItem::IN_PROGRESS); 431 CleanupItem(item, file, DownloadItem::IN_PROGRESS);
402 } 432 }
403 433
404 TEST_F(DownloadItemTest, NotificationAfterCancel) { 434 TEST_F(DownloadItemTest, NotificationAfterCancel) {
405 DownloadItemImpl* user_cancel = CreateDownloadItem(); 435 DownloadItemImpl* user_cancel = CreateDownloadItem();
406 DownloadItemImplDelegate::DownloadTargetCallback target_callback; 436 DownloadItemImplDelegate::DownloadTargetCallback target_callback;
407 MockDownloadFile* download_file = 437 MockDownloadFile* download_file =
408 CallDownloadItemStart(user_cancel, &target_callback); 438 CallDownloadItemStart(user_cancel, &target_callback);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 DownloadItemImpl* item = CreateDownloadItem(); 473 DownloadItemImpl* item = CreateDownloadItem();
444 MockDownloadFile* download_file = 474 MockDownloadFile* download_file =
445 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 475 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
446 EXPECT_CALL(*download_file, Cancel()); 476 EXPECT_CALL(*download_file, Cancel());
447 TestDownloadItemObserver observer(item); 477 TestDownloadItemObserver observer(item);
448 478
449 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_,_)) 479 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_,_))
450 .Times(0); 480 .Times(0);
451 481
452 item->DestinationObserverAsWeakPtr()->DestinationError( 482 item->DestinationObserverAsWeakPtr()->DestinationError(
453 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 483 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED,
484 0,
485 scoped_ptr<crypto::SecureHash>());
454 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 486 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
455 } 487 }
456 488
457 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { 489 TEST_F(DownloadItemTest, NotificationAfterDestroyed) {
458 DownloadItemImpl* item = CreateDownloadItem(); 490 DownloadItemImpl* item = CreateDownloadItem();
459 TestDownloadItemObserver observer(item); 491 TestDownloadItemObserver observer(item);
460 492
461 DestroyDownloadItem(item); 493 DestroyDownloadItem(item);
462 ASSERT_TRUE(observer.download_destroyed()); 494 ASSERT_TRUE(observer.download_destroyed());
463 } 495 }
464 496
465 // Test that a download is resumed automatcially after a continuable interrupt. 497 // Test that a download is resumed automatcially after a continuable interrupt.
466 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { 498 TEST_F(DownloadItemTest, ContinueAfterInterrupted) {
467 DownloadItemImpl* item = CreateDownloadItem(); 499 DownloadItemImpl* item = CreateDownloadItem();
468 TestDownloadItemObserver observer(item); 500 TestDownloadItemObserver observer(item);
469 MockDownloadFile* download_file = 501 MockDownloadFile* download_file =
470 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 502 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
471 503
472 // Interrupt the download, using a continuable interrupt. 504 // Interrupt the download, using a continuable interrupt.
473 EXPECT_CALL(*download_file, FullPath()) 505 EXPECT_CALL(*download_file, FullPath())
474 .WillOnce(Return(base::FilePath())); 506 .WillOnce(ReturnRefOfCopy(base::FilePath()));
475 EXPECT_CALL(*download_file, Detach()); 507 EXPECT_CALL(*download_file, Detach());
476 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) 508 EXPECT_CALL(*mock_delegate(), GetBrowserContext())
477 .WillRepeatedly(Return(browser_context())); 509 .WillRepeatedly(Return(browser_context()));
478 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)).Times(1); 510 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)).Times(1);
479 item->DestinationObserverAsWeakPtr()->DestinationError( 511 item->DestinationObserverAsWeakPtr()->DestinationError(
480 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); 512 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR,
513 0,
514 scoped_ptr<crypto::SecureHash>());
481 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 515 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
482 // Since the download is resumed automatically, the interrupt count doesn't 516 // Since the download is resumed automatically, the interrupt count doesn't
483 // increase. 517 // increase.
484 ASSERT_EQ(0, observer.interrupt_count()); 518 ASSERT_EQ(0, observer.interrupt_count());
485 519
486 // Test expectations verify that ResumeInterruptedDownload() is called (by way 520 // Test expectations verify that ResumeInterruptedDownload() is called (by way
487 // of MockResumeInterruptedDownload) after the download is interrupted. But 521 // of MockResumeInterruptedDownload) after the download is interrupted. But
488 // the mock doesn't follow through with the resumption. 522 // the mock doesn't follow through with the resumption.
489 // ResumeInterruptedDownload() being called is sufficient for verifying that 523 // ResumeInterruptedDownload() being called is sufficient for verifying that
490 // the automatic resumption was triggered. 524 // the automatic resumption was triggered.
491 RunAllPendingInMessageLoops(); 525 RunAllPendingInMessageLoops();
492 526
493 // The download item is currently in RESUMING_INTERNAL state, which maps to 527 // The download item is currently in RESUMING_INTERNAL state, which maps to
494 // IN_PROGRESS. 528 // IN_PROGRESS.
495 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); 529 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS);
496 } 530 }
497 531
498 // Test that automatic resumption doesn't happen after a non-continuable 532 // Test that automatic resumption doesn't happen after a non-continuable
499 // interrupt. 533 // interrupt.
500 TEST_F(DownloadItemTest, RestartAfterInterrupted) { 534 TEST_F(DownloadItemTest, RestartAfterInterrupted) {
501 DownloadItemImpl* item = CreateDownloadItem(); 535 DownloadItemImpl* item = CreateDownloadItem();
502 TestDownloadItemObserver observer(item); 536 TestDownloadItemObserver observer(item);
503 MockDownloadFile* download_file = 537 MockDownloadFile* download_file =
504 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 538 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
505 539
506 // Interrupt the download, using a restartable interrupt. 540 // Interrupt the download, using a restartable interrupt.
507 EXPECT_CALL(*download_file, Cancel()); 541 EXPECT_CALL(*download_file, Cancel());
508 item->DestinationObserverAsWeakPtr()->DestinationError( 542 item->DestinationObserverAsWeakPtr()->DestinationError(
509 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 543 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED,
544 0,
545 scoped_ptr<crypto::SecureHash>());
510 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 546 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
511 // Should not try to auto-resume. 547 // Should not try to auto-resume.
512 ASSERT_EQ(1, observer.interrupt_count()); 548 ASSERT_EQ(1, observer.interrupt_count());
513 ASSERT_EQ(0, observer.resume_count()); 549 ASSERT_EQ(0, observer.resume_count());
514 RunAllPendingInMessageLoops(); 550 RunAllPendingInMessageLoops();
515 551
516 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); 552 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED);
517 } 553 }
518 554
519 // Check we do correct cleanup for RESUME_MODE_INVALID interrupts. 555 // Check we do correct cleanup for RESUME_MODE_INVALID interrupts.
520 TEST_F(DownloadItemTest, UnresumableInterrupt) { 556 TEST_F(DownloadItemTest, UnresumableInterrupt) {
521 DownloadItemImpl* item = CreateDownloadItem(); 557 DownloadItemImpl* item = CreateDownloadItem();
522 TestDownloadItemObserver observer(item); 558 TestDownloadItemObserver observer(item);
523 MockDownloadFile* download_file = 559 MockDownloadFile* download_file =
524 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 560 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
525 561
526 // Fail final rename with unresumable reason. 562 // Fail final rename with unresumable reason.
527 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 563 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
528 .WillOnce(Return(true)); 564 .WillOnce(Return(true));
529 EXPECT_CALL(*download_file, 565 EXPECT_CALL(*download_file,
530 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _)) 566 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _))
531 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED, 567 .WillOnce(ScheduleRenameAndAnnotateCallback(
532 base::FilePath())); 568 DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED, base::FilePath()));
533 EXPECT_CALL(*download_file, Cancel()); 569 EXPECT_CALL(*download_file, Cancel());
534 570
535 // Complete download to trigger final rename. 571 // Complete download to trigger final rename.
536 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 572 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
573 0, scoped_ptr<crypto::SecureHash>());
537 RunAllPendingInMessageLoops(); 574 RunAllPendingInMessageLoops();
538 575
539 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 576 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
540 // Should not try to auto-resume. 577 // Should not try to auto-resume.
541 ASSERT_EQ(1, observer.interrupt_count()); 578 ASSERT_EQ(1, observer.interrupt_count());
542 ASSERT_EQ(0, observer.resume_count()); 579 ASSERT_EQ(0, observer.resume_count());
543 580
544 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); 581 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED);
545 } 582 }
546 583
(...skipping 16 matching lines...) Expand all
563 .Times(DownloadItemImpl::kMaxAutoResumeAttempts); 600 .Times(DownloadItemImpl::kMaxAutoResumeAttempts);
564 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { 601 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) {
565 SCOPED_TRACE(::testing::Message() << "Iteration " << i); 602 SCOPED_TRACE(::testing::Message() << "Iteration " << i);
566 603
567 mock_download_file = new NiceMock<MockDownloadFile>; 604 mock_download_file = new NiceMock<MockDownloadFile>;
568 download_file.reset(mock_download_file); 605 download_file.reset(mock_download_file);
569 mock_request_handle = new NiceMock<MockRequestHandle>; 606 mock_request_handle = new NiceMock<MockRequestHandle>;
570 request_handle.reset(mock_request_handle); 607 request_handle.reset(mock_request_handle);
571 608
572 ON_CALL(*mock_download_file, FullPath()) 609 ON_CALL(*mock_download_file, FullPath())
573 .WillByDefault(Return(base::FilePath())); 610 .WillByDefault(ReturnRefOfCopy(base::FilePath()));
574 611
575 // Copied key parts of DoIntermediateRename & CallDownloadItemStart 612 // Copied key parts of DoIntermediateRename & CallDownloadItemStart
576 // to allow for holding onto the request handle. 613 // to allow for holding onto the request handle.
577 item->Start(std::move(download_file), std::move(request_handle), 614 item->Start(std::move(download_file), std::move(request_handle),
578 *create_info()); 615 *create_info());
579 RunAllPendingInMessageLoops(); 616 RunAllPendingInMessageLoops();
580 617
581 base::FilePath target_path(kDummyTargetPath); 618 base::FilePath target_path(kDummyTargetPath);
582 base::FilePath intermediate_path(kDummyIntermediatePath); 619 base::FilePath intermediate_path(kDummyIntermediatePath);
583 if (i == 0) { 620 if (i == 0) {
584 // RenameAndUniquify is only called the first time. In all the subsequent 621 // RenameAndUniquify is only called the first time. In all the subsequent
585 // iterations, the intermediate file already has the correct name, hence 622 // iterations, the intermediate file already has the correct name, hence
586 // no rename is necessary. 623 // no rename is necessary.
587 EXPECT_CALL(*mock_download_file, RenameAndUniquify(intermediate_path, _)) 624 EXPECT_CALL(*mock_download_file, RenameAndUniquify(intermediate_path, _))
588 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 625 .WillOnce(ScheduleRenameAndUniquifyCallback(
589 intermediate_path)); 626 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path));
590 } 627 }
591 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 628 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
592 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 629 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
593 RunAllPendingInMessageLoops(); 630 RunAllPendingInMessageLoops();
594 631
595 // Use a continuable interrupt. 632 // Use a continuable interrupt.
596 item->DestinationObserverAsWeakPtr()->DestinationError( 633 item->DestinationObserverAsWeakPtr()->DestinationError(
597 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); 634 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR,
635 0,
636 scoped_ptr<crypto::SecureHash>());
598 637
599 RunAllPendingInMessageLoops(); 638 RunAllPendingInMessageLoops();
600 ::testing::Mock::VerifyAndClearExpectations(mock_download_file); 639 ::testing::Mock::VerifyAndClearExpectations(mock_download_file);
601 } 640 }
602 641
603 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 642 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
604 EXPECT_EQ(1, observer.interrupt_count()); 643 EXPECT_EQ(1, observer.interrupt_count());
605 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); 644 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED);
606 } 645 }
607 646
(...skipping 16 matching lines...) Expand all
624 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); 663 EXPECT_EQ(kContentDisposition, item->GetContentDisposition());
625 EXPECT_EQ(kFirstETag, item->GetETag()); 664 EXPECT_EQ(kFirstETag, item->GetETag());
626 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); 665 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime());
627 EXPECT_EQ(kFirstURL, item->GetURL().spec()); 666 EXPECT_EQ(kFirstURL, item->GetURL().spec());
628 667
629 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); 668 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _));
630 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) 669 EXPECT_CALL(*mock_delegate(), GetBrowserContext())
631 .WillRepeatedly(Return(browser_context())); 670 .WillRepeatedly(Return(browser_context()));
632 EXPECT_CALL(*download_file, Detach()); 671 EXPECT_CALL(*download_file, Detach());
633 item->DestinationObserverAsWeakPtr()->DestinationError( 672 item->DestinationObserverAsWeakPtr()->DestinationError(
634 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); 673 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR,
674 0,
675 scoped_ptr<crypto::SecureHash>());
635 RunAllPendingInMessageLoops(); 676 RunAllPendingInMessageLoops();
636 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 677 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
637 678
638 // Now change the create info. The changes should not cause the DownloadItem 679 // Now change the create info. The changes should not cause the DownloadItem
639 // to be updated. 680 // to be updated.
640 const char kSecondContentDisposition[] = "attachment; filename=bar"; 681 const char kSecondContentDisposition[] = "attachment; filename=bar";
641 const char kSecondETag[] = "123"; 682 const char kSecondETag[] = "123";
642 const char kSecondLastModified[] = "Today"; 683 const char kSecondLastModified[] = "Today";
643 const char kSecondURL[] = "http://example.com/another-download"; 684 const char kSecondURL[] = "http://example.com/another-download";
644 create_info()->content_disposition = kSecondContentDisposition; 685 create_info()->content_disposition = kSecondContentDisposition;
(...skipping 22 matching lines...) Expand all
667 create_info()->url_chain.push_back(GURL("http://example.com/a")); 708 create_info()->url_chain.push_back(GURL("http://example.com/a"));
668 create_info()->url_chain.push_back(GURL("http://example.com/b")); 709 create_info()->url_chain.push_back(GURL("http://example.com/b"));
669 create_info()->url_chain.push_back(GURL("http://example.com/c")); 710 create_info()->url_chain.push_back(GURL("http://example.com/c"));
670 711
671 DownloadItemImpl* item = CreateDownloadItem(); 712 DownloadItemImpl* item = CreateDownloadItem();
672 TestDownloadItemObserver observer(item); 713 TestDownloadItemObserver observer(item);
673 MockDownloadFile* download_file = 714 MockDownloadFile* download_file =
674 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 715 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
675 716
676 // Interrupt the download, using a continuable interrupt. 717 // Interrupt the download, using a continuable interrupt.
677 EXPECT_CALL(*download_file, FullPath()).WillOnce(Return(base::FilePath())); 718 EXPECT_CALL(*download_file, FullPath())
719 .WillOnce(ReturnRefOfCopy(base::FilePath()));
678 EXPECT_CALL(*download_file, Detach()); 720 EXPECT_CALL(*download_file, Detach());
679 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) 721 EXPECT_CALL(*mock_delegate(), GetBrowserContext())
680 .WillRepeatedly(Return(browser_context())); 722 .WillRepeatedly(Return(browser_context()));
681 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload( 723 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(
682 Property(&DownloadUrlParameters::url, 724 Property(&DownloadUrlParameters::url,
683 GURL("http://example.com/c")), 725 GURL("http://example.com/c")),
684 _)) 726 _))
685 .Times(1); 727 .Times(1);
686 item->DestinationObserverAsWeakPtr()->DestinationError( 728 item->DestinationObserverAsWeakPtr()->DestinationError(
687 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); 729 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR,
730 0,
731 scoped_ptr<crypto::SecureHash>());
688 732
689 // Test expectations verify that ResumeInterruptedDownload() is called (by way 733 // Test expectations verify that ResumeInterruptedDownload() is called (by way
690 // of MockResumeInterruptedDownload) after the download is interrupted. But 734 // of MockResumeInterruptedDownload) after the download is interrupted. But
691 // the mock doesn't follow through with the resumption. 735 // the mock doesn't follow through with the resumption.
692 // ResumeInterruptedDownload() being called is sufficient for verifying that 736 // ResumeInterruptedDownload() being called is sufficient for verifying that
693 // the resumption was triggered. 737 // the resumption was triggered.
694 RunAllPendingInMessageLoops(); 738 RunAllPendingInMessageLoops();
695 739
696 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS. 740 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS.
697 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); 741 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS);
(...skipping 13 matching lines...) Expand all
711 ASSERT_TRUE(observer.download_removed()); 755 ASSERT_TRUE(observer.download_removed());
712 } 756 }
713 757
714 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { 758 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) {
715 // Setting to NOT_DANGEROUS does not trigger a notification. 759 // Setting to NOT_DANGEROUS does not trigger a notification.
716 DownloadItemImpl* safe_item = CreateDownloadItem(); 760 DownloadItemImpl* safe_item = CreateDownloadItem();
717 MockDownloadFile* download_file = 761 MockDownloadFile* download_file =
718 DoIntermediateRename(safe_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 762 DoIntermediateRename(safe_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
719 TestDownloadItemObserver safe_observer(safe_item); 763 TestDownloadItemObserver safe_observer(safe_item);
720 764
721 safe_item->OnAllDataSaved(std::string()); 765 safe_item->OnAllDataSaved(0, scoped_ptr<crypto::SecureHash>());
722 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated()); 766 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated());
723 safe_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 767 safe_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
724 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated()); 768 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated());
725 CleanupItem(safe_item, download_file, DownloadItem::IN_PROGRESS); 769 CleanupItem(safe_item, download_file, DownloadItem::IN_PROGRESS);
726 770
727 // Setting to unsafe url or unsafe file should trigger a notification. 771 // Setting to unsafe url or unsafe file should trigger a notification.
728 DownloadItemImpl* unsafeurl_item = 772 DownloadItemImpl* unsafeurl_item =
729 CreateDownloadItem(); 773 CreateDownloadItem();
730 download_file = 774 download_file =
731 DoIntermediateRename(unsafeurl_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 775 DoIntermediateRename(unsafeurl_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
732 TestDownloadItemObserver unsafeurl_observer(unsafeurl_item); 776 TestDownloadItemObserver unsafeurl_observer(unsafeurl_item);
733 777
734 unsafeurl_item->OnAllDataSaved(std::string()); 778 unsafeurl_item->OnAllDataSaved(0, scoped_ptr<crypto::SecureHash>());
735 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); 779 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated());
736 unsafeurl_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); 780 unsafeurl_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_URL);
737 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); 781 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated());
738 782
739 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) 783 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _))
740 .WillOnce(Return(true)); 784 .WillOnce(Return(true));
741 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)); 785 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _));
742 unsafeurl_item->ValidateDangerousDownload(); 786 unsafeurl_item->ValidateDangerousDownload();
743 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated()); 787 EXPECT_TRUE(unsafeurl_observer.CheckAndResetDownloadUpdated());
744 CleanupItem(unsafeurl_item, download_file, DownloadItem::IN_PROGRESS); 788 CleanupItem(unsafeurl_item, download_file, DownloadItem::IN_PROGRESS);
745 789
746 DownloadItemImpl* unsafefile_item = 790 DownloadItemImpl* unsafefile_item =
747 CreateDownloadItem(); 791 CreateDownloadItem();
748 download_file = 792 download_file =
749 DoIntermediateRename(unsafefile_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 793 DoIntermediateRename(unsafefile_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
750 TestDownloadItemObserver unsafefile_observer(unsafefile_item); 794 TestDownloadItemObserver unsafefile_observer(unsafefile_item);
751 795
752 unsafefile_item->OnAllDataSaved(std::string()); 796 unsafefile_item->OnAllDataSaved(0, scoped_ptr<crypto::SecureHash>());
753 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); 797 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated());
754 unsafefile_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 798 unsafefile_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
755 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); 799 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated());
756 800
757 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) 801 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _))
758 .WillOnce(Return(true)); 802 .WillOnce(Return(true));
759 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)); 803 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _));
760 unsafefile_item->ValidateDangerousDownload(); 804 unsafefile_item->ValidateDangerousDownload();
761 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated()); 805 EXPECT_TRUE(unsafefile_observer.CheckAndResetDownloadUpdated());
762 CleanupItem(unsafefile_item, download_file, DownloadItem::IN_PROGRESS); 806 CleanupItem(unsafefile_item, download_file, DownloadItem::IN_PROGRESS);
763 } 807 }
764 808
765 // DownloadItemImpl::OnDownloadTargetDetermined will schedule a task to run 809 // DownloadItemImpl::OnDownloadTargetDetermined will schedule a task to run
766 // DownloadFile::Rename(). Once the rename 810 // DownloadFile::Rename(). Once the rename
767 // completes, DownloadItemImpl receives a notification with the new file 811 // completes, DownloadItemImpl receives a notification with the new file
768 // name. Check that observers are updated when the new filename is available and 812 // name. Check that observers are updated when the new filename is available and
769 // not before. 813 // not before.
770 TEST_F(DownloadItemTest, NotificationAfterOnDownloadTargetDetermined) { 814 TEST_F(DownloadItemTest, NotificationAfterOnDownloadTargetDetermined) {
771 DownloadItemImpl* item = CreateDownloadItem(); 815 DownloadItemImpl* item = CreateDownloadItem();
772 DownloadItemImplDelegate::DownloadTargetCallback callback; 816 DownloadItemImplDelegate::DownloadTargetCallback callback;
773 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 817 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
774 TestDownloadItemObserver observer(item); 818 TestDownloadItemObserver observer(item);
775 base::FilePath target_path(kDummyTargetPath); 819 base::FilePath target_path(kDummyTargetPath);
776 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); 820 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x"));
777 base::FilePath new_intermediate_path( 821 base::FilePath new_intermediate_path(
778 target_path.InsertBeforeExtensionASCII("y")); 822 target_path.InsertBeforeExtensionASCII("y"));
779 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 823 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
780 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 824 .WillOnce(ScheduleRenameAndUniquifyCallback(
781 new_intermediate_path)); 825 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path));
782 826
783 // Currently, a notification would be generated if the danger type is anything 827 // Currently, a notification would be generated if the danger type is anything
784 // other than NOT_DANGEROUS. 828 // other than NOT_DANGEROUS.
785 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 829 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
786 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 830 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
787 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); 831 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated());
788 RunAllPendingInMessageLoops(); 832 RunAllPendingInMessageLoops();
789 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); 833 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
790 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); 834 EXPECT_EQ(new_intermediate_path, item->GetFullPath());
791 835
(...skipping 29 matching lines...) Expand all
821 TEST_F(DownloadItemTest, DisplayName) { 865 TEST_F(DownloadItemTest, DisplayName) {
822 DownloadItemImpl* item = CreateDownloadItem(); 866 DownloadItemImpl* item = CreateDownloadItem();
823 DownloadItemImplDelegate::DownloadTargetCallback callback; 867 DownloadItemImplDelegate::DownloadTargetCallback callback;
824 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 868 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
825 base::FilePath target_path( 869 base::FilePath target_path(
826 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); 870 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar"));
827 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); 871 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x"));
828 EXPECT_EQ(FILE_PATH_LITERAL(""), 872 EXPECT_EQ(FILE_PATH_LITERAL(""),
829 item->GetFileNameToReportUser().value()); 873 item->GetFileNameToReportUser().value());
830 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) 874 EXPECT_CALL(*download_file, RenameAndUniquify(_, _))
831 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 875 .WillOnce(ScheduleRenameAndUniquifyCallback(
832 intermediate_path)); 876 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path));
833 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 877 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
834 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 878 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
835 RunAllPendingInMessageLoops(); 879 RunAllPendingInMessageLoops();
836 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), 880 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"),
837 item->GetFileNameToReportUser().value()); 881 item->GetFileNameToReportUser().value());
838 item->SetDisplayName(base::FilePath(FILE_PATH_LITERAL("new.name"))); 882 item->SetDisplayName(base::FilePath(FILE_PATH_LITERAL("new.name")));
839 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), 883 EXPECT_EQ(FILE_PATH_LITERAL("new.name"),
840 item->GetFileNameToReportUser().value()); 884 item->GetFileNameToReportUser().value());
841 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); 885 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
842 } 886 }
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
925 TEST_F(DownloadItemTest, CallbackAfterRename) { 969 TEST_F(DownloadItemTest, CallbackAfterRename) {
926 DownloadItemImpl* item = CreateDownloadItem(); 970 DownloadItemImpl* item = CreateDownloadItem();
927 DownloadItemImplDelegate::DownloadTargetCallback callback; 971 DownloadItemImplDelegate::DownloadTargetCallback callback;
928 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 972 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
929 base::FilePath final_path( 973 base::FilePath final_path(
930 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); 974 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar"));
931 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 975 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
932 base::FilePath new_intermediate_path( 976 base::FilePath new_intermediate_path(
933 final_path.InsertBeforeExtensionASCII("y")); 977 final_path.InsertBeforeExtensionASCII("y"));
934 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 978 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
935 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 979 .WillOnce(ScheduleRenameAndUniquifyCallback(
936 new_intermediate_path)); 980 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path));
937 981
938 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 982 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
939 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 983 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
940 RunAllPendingInMessageLoops(); 984 RunAllPendingInMessageLoops();
941 // All the callbacks should have happened by now. 985 // All the callbacks should have happened by now.
942 ::testing::Mock::VerifyAndClearExpectations(download_file); 986 ::testing::Mock::VerifyAndClearExpectations(download_file);
943 mock_delegate()->VerifyAndClearExpectations(); 987 mock_delegate()->VerifyAndClearExpectations();
944 988
945 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 989 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
946 .WillOnce(Return(true)); 990 .WillOnce(Return(true));
947 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _)) 991 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _, _, _, _))
948 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 992 .WillOnce(ScheduleRenameAndAnnotateCallback(
949 final_path)); 993 DOWNLOAD_INTERRUPT_REASON_NONE, final_path));
950 EXPECT_CALL(*download_file, FullPath()) 994 EXPECT_CALL(*download_file, FullPath())
951 .WillOnce(Return(base::FilePath())); 995 .WillOnce(ReturnRefOfCopy(base::FilePath()));
952 EXPECT_CALL(*download_file, Detach()); 996 EXPECT_CALL(*download_file, Detach());
953 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 997 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
998 0, scoped_ptr<crypto::SecureHash>());
954 RunAllPendingInMessageLoops(); 999 RunAllPendingInMessageLoops();
955 ::testing::Mock::VerifyAndClearExpectations(download_file); 1000 ::testing::Mock::VerifyAndClearExpectations(download_file);
956 mock_delegate()->VerifyAndClearExpectations(); 1001 mock_delegate()->VerifyAndClearExpectations();
957 } 1002 }
958 1003
959 // Test that the delegate is invoked after the download file is renamed and the 1004 // Test that the delegate is invoked after the download file is renamed and the
960 // download item is in an interrupted state. 1005 // download item is in an interrupted state.
961 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) { 1006 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) {
962 DownloadItemImpl* item = CreateDownloadItem(); 1007 DownloadItemImpl* item = CreateDownloadItem();
963 DownloadItemImplDelegate::DownloadTargetCallback callback; 1008 DownloadItemImplDelegate::DownloadTargetCallback callback;
964 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 1009 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
965 base::FilePath final_path( 1010 base::FilePath final_path(
966 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); 1011 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar"));
967 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 1012 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
968 base::FilePath new_intermediate_path( 1013 base::FilePath new_intermediate_path(
969 final_path.InsertBeforeExtensionASCII("y")); 1014 final_path.InsertBeforeExtensionASCII("y"));
970 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1015 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
971 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1016 .WillOnce(ScheduleRenameAndUniquifyCallback(
972 new_intermediate_path)); 1017 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path));
973 EXPECT_CALL(*download_file, Cancel()) 1018 EXPECT_CALL(*download_file, Cancel())
974 .Times(1); 1019 .Times(1);
975 1020
976 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1021 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
977 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 1022 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
978 RunAllPendingInMessageLoops(); 1023 RunAllPendingInMessageLoops();
979 // All the callbacks should have happened by now. 1024 // All the callbacks should have happened by now.
980 ::testing::Mock::VerifyAndClearExpectations(download_file); 1025 ::testing::Mock::VerifyAndClearExpectations(download_file);
981 mock_delegate()->VerifyAndClearExpectations(); 1026 mock_delegate()->VerifyAndClearExpectations();
982 } 1027 }
983 1028
984 TEST_F(DownloadItemTest, Interrupted) { 1029 TEST_F(DownloadItemTest, Interrupted) {
985 DownloadItemImpl* item = CreateDownloadItem(); 1030 DownloadItemImpl* item = CreateDownloadItem();
986 MockDownloadFile* download_file = 1031 MockDownloadFile* download_file =
987 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1032 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
988 1033
989 const DownloadInterruptReason reason( 1034 const DownloadInterruptReason reason(
990 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); 1035 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED);
991 1036
992 // Confirm interrupt sets state properly. 1037 // Confirm interrupt sets state properly.
993 EXPECT_CALL(*download_file, Cancel()); 1038 EXPECT_CALL(*download_file, Cancel());
994 item->DestinationObserverAsWeakPtr()->DestinationError(reason); 1039 item->DestinationObserverAsWeakPtr()->DestinationError(
1040 reason, 0, scoped_ptr<crypto::SecureHash>());
995 RunAllPendingInMessageLoops(); 1041 RunAllPendingInMessageLoops();
996 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1042 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
997 EXPECT_EQ(reason, item->GetLastReason()); 1043 EXPECT_EQ(reason, item->GetLastReason());
998 1044
999 // Cancel should kill it. 1045 // Cancel should kill it.
1000 item->Cancel(true); 1046 item->Cancel(true);
1001 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); 1047 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState());
1002 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); 1048 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason());
1003 } 1049 }
1004 1050
1005 // Destination errors that occur before the intermediate rename shouldn't cause 1051 // Destination errors that occur before the intermediate rename shouldn't cause
1006 // the download to be marked as interrupted until after the intermediate rename. 1052 // the download to be marked as interrupted until after the intermediate rename.
1007 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename_Restart) { 1053 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename_Restart) {
1008 DownloadItemImpl* item = CreateDownloadItem(); 1054 DownloadItemImpl* item = CreateDownloadItem();
1009 DownloadItemImplDelegate::DownloadTargetCallback callback; 1055 DownloadItemImplDelegate::DownloadTargetCallback callback;
1010 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 1056 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
1011 item->DestinationObserverAsWeakPtr()->DestinationError( 1057 item->DestinationObserverAsWeakPtr()->DestinationError(
1012 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 1058 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED,
1059 0,
1060 scoped_ptr<crypto::SecureHash>());
1013 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1061 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1014 1062
1015 base::FilePath final_path( 1063 base::FilePath final_path(
1016 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); 1064 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar"));
1017 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 1065 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
1018 base::FilePath new_intermediate_path( 1066 base::FilePath new_intermediate_path(
1019 final_path.InsertBeforeExtensionASCII("y")); 1067 final_path.InsertBeforeExtensionASCII("y"));
1020 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1068 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
1021 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 1069 .WillOnce(ScheduleRenameAndUniquifyCallback(
1022 new_intermediate_path)); 1070 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path));
1023 EXPECT_CALL(*download_file, Cancel()) 1071 EXPECT_CALL(*download_file, Cancel())
1024 .Times(1); 1072 .Times(1);
1025 1073
1026 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1074 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1027 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 1075 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
1028 RunAllPendingInMessageLoops(); 1076 RunAllPendingInMessageLoops();
1029 // All the callbacks should have happened by now. 1077 // All the callbacks should have happened by now.
1030 ::testing::Mock::VerifyAndClearExpectations(download_file); 1078 ::testing::Mock::VerifyAndClearExpectations(download_file);
1031 mock_delegate()->VerifyAndClearExpectations(); 1079 mock_delegate()->VerifyAndClearExpectations();
1032 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1080 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1033 EXPECT_TRUE(item->GetFullPath().empty()); 1081 EXPECT_TRUE(item->GetFullPath().empty());
1034 EXPECT_EQ(final_path, item->GetTargetFilePath()); 1082 EXPECT_EQ(final_path, item->GetTargetFilePath());
1035 } 1083 }
1036 1084
1037 // As above. But if the download can be resumed by continuing, then the 1085 // As above. But if the download can be resumed by continuing, then the
1038 // intermediate path should be retained when the download is interrupted after 1086 // intermediate path should be retained when the download is interrupted after
1039 // the intermediate rename succeeds. 1087 // the intermediate rename succeeds.
1040 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename_Continue) { 1088 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename_Continue) {
1041 DownloadItemImpl* item = CreateDownloadItem(); 1089 DownloadItemImpl* item = CreateDownloadItem();
1042 DownloadItemImplDelegate::DownloadTargetCallback callback; 1090 DownloadItemImplDelegate::DownloadTargetCallback callback;
1043 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 1091 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
1044 item->DestinationObserverAsWeakPtr()->DestinationError( 1092 item->DestinationObserverAsWeakPtr()->DestinationError(
1045 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED); 1093 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED,
1094 0,
1095 scoped_ptr<crypto::SecureHash>());
1046 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1096 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1047 1097
1048 base::FilePath final_path( 1098 base::FilePath final_path(
1049 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); 1099 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar"));
1050 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 1100 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
1051 base::FilePath new_intermediate_path( 1101 base::FilePath new_intermediate_path(
1052 final_path.InsertBeforeExtensionASCII("y")); 1102 final_path.InsertBeforeExtensionASCII("y"));
1053 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1103 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
1054 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 1104 .WillOnce(ScheduleRenameAndUniquifyCallback(
1055 new_intermediate_path)); 1105 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path));
1056 EXPECT_CALL(*download_file, FullPath()) 1106 EXPECT_CALL(*download_file, FullPath())
1057 .WillOnce(Return(base::FilePath(new_intermediate_path))); 1107 .WillOnce(ReturnRefOfCopy(base::FilePath(new_intermediate_path)));
1058 EXPECT_CALL(*download_file, Detach()); 1108 EXPECT_CALL(*download_file, Detach());
1059 1109
1060 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1110 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1061 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 1111 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
1062 RunAllPendingInMessageLoops(); 1112 RunAllPendingInMessageLoops();
1063 // All the callbacks should have happened by now. 1113 // All the callbacks should have happened by now.
1064 ::testing::Mock::VerifyAndClearExpectations(download_file); 1114 ::testing::Mock::VerifyAndClearExpectations(download_file);
1065 mock_delegate()->VerifyAndClearExpectations(); 1115 mock_delegate()->VerifyAndClearExpectations();
1066 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1116 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1067 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); 1117 EXPECT_EQ(new_intermediate_path, item->GetFullPath());
1068 EXPECT_EQ(final_path, item->GetTargetFilePath()); 1118 EXPECT_EQ(final_path, item->GetTargetFilePath());
1069 } 1119 }
1070 1120
1071 // As above. If the intermediate rename fails, then the interrupt reason should 1121 // As above. If the intermediate rename fails, then the interrupt reason should
1072 // be set to the destination error and the intermediate path should be empty. 1122 // be set to the file error and the intermediate path should be empty.
1073 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename_Failed) { 1123 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename_Failed) {
1074 DownloadItemImpl* item = CreateDownloadItem(); 1124 DownloadItemImpl* item = CreateDownloadItem();
1075 DownloadItemImplDelegate::DownloadTargetCallback callback; 1125 DownloadItemImplDelegate::DownloadTargetCallback callback;
1076 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 1126 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
1077 item->DestinationObserverAsWeakPtr()->DestinationError( 1127 item->DestinationObserverAsWeakPtr()->DestinationError(
1078 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED); 1128 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED,
1129 0,
1130 scoped_ptr<crypto::SecureHash>());
1079 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1131 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1080 1132
1081 base::FilePath final_path( 1133 base::FilePath final_path(
1082 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); 1134 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar"));
1083 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 1135 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
1084 base::FilePath new_intermediate_path( 1136 base::FilePath new_intermediate_path(
1085 final_path.InsertBeforeExtensionASCII("y")); 1137 final_path.InsertBeforeExtensionASCII("y"));
1086 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1138 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
1087 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1139 .WillOnce(ScheduleRenameAndUniquifyCallback(
1088 new_intermediate_path)); 1140 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path));
1089 EXPECT_CALL(*download_file, Cancel()) 1141 EXPECT_CALL(*download_file, Cancel())
1090 .Times(1); 1142 .Times(1);
1091 1143
1092 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1144 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1093 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 1145 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
1094 RunAllPendingInMessageLoops(); 1146 RunAllPendingInMessageLoops();
1095 // All the callbacks should have happened by now. 1147 // All the callbacks should have happened by now.
1096 ::testing::Mock::VerifyAndClearExpectations(download_file); 1148 ::testing::Mock::VerifyAndClearExpectations(download_file);
1097 mock_delegate()->VerifyAndClearExpectations(); 1149 mock_delegate()->VerifyAndClearExpectations();
1098 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1150 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1099 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); 1151 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, item->GetLastReason());
1100 EXPECT_TRUE(item->GetFullPath().empty()); 1152 EXPECT_TRUE(item->GetFullPath().empty());
1101 EXPECT_EQ(final_path, item->GetTargetFilePath()); 1153 EXPECT_EQ(final_path, item->GetTargetFilePath());
1102 } 1154 }
1103 1155
1104 TEST_F(DownloadItemTest, Canceled) { 1156 TEST_F(DownloadItemTest, Canceled) {
1105 DownloadItemImpl* item = CreateDownloadItem(); 1157 DownloadItemImpl* item = CreateDownloadItem();
1106 DownloadItemImplDelegate::DownloadTargetCallback target_callback; 1158 DownloadItemImplDelegate::DownloadTargetCallback target_callback;
1107 MockDownloadFile* download_file = 1159 MockDownloadFile* download_file =
1108 CallDownloadItemStart(item, &target_callback); 1160 CallDownloadItemStart(item, &target_callback);
1109 1161
(...skipping 13 matching lines...) Expand all
1123 1175
1124 TEST_F(DownloadItemTest, DestinationUpdate) { 1176 TEST_F(DownloadItemTest, DestinationUpdate) {
1125 DownloadItemImpl* item = CreateDownloadItem(); 1177 DownloadItemImpl* item = CreateDownloadItem();
1126 MockDownloadFile* file = 1178 MockDownloadFile* file =
1127 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1179 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1128 base::WeakPtr<DownloadDestinationObserver> as_observer( 1180 base::WeakPtr<DownloadDestinationObserver> as_observer(
1129 item->DestinationObserverAsWeakPtr()); 1181 item->DestinationObserverAsWeakPtr());
1130 TestDownloadItemObserver observer(item); 1182 TestDownloadItemObserver observer(item);
1131 1183
1132 EXPECT_EQ(0l, item->CurrentSpeed()); 1184 EXPECT_EQ(0l, item->CurrentSpeed());
1133 EXPECT_EQ("", item->GetHashState());
1134 EXPECT_EQ(0l, item->GetReceivedBytes()); 1185 EXPECT_EQ(0l, item->GetReceivedBytes());
1135 EXPECT_EQ(0l, item->GetTotalBytes()); 1186 EXPECT_EQ(0l, item->GetTotalBytes());
1136 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); 1187 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated());
1137 item->SetTotalBytes(100l); 1188 item->SetTotalBytes(100l);
1138 EXPECT_EQ(100l, item->GetTotalBytes()); 1189 EXPECT_EQ(100l, item->GetTotalBytes());
1139 1190
1140 as_observer->DestinationUpdate(10, 20, "deadbeef"); 1191 as_observer->DestinationUpdate(10, 20);
1141 EXPECT_EQ(20l, item->CurrentSpeed()); 1192 EXPECT_EQ(20l, item->CurrentSpeed());
1142 EXPECT_EQ("deadbeef", item->GetHashState());
1143 EXPECT_EQ(10l, item->GetReceivedBytes()); 1193 EXPECT_EQ(10l, item->GetReceivedBytes());
1144 EXPECT_EQ(100l, item->GetTotalBytes()); 1194 EXPECT_EQ(100l, item->GetTotalBytes());
1145 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); 1195 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
1146 1196
1147 as_observer->DestinationUpdate(200, 20, "livebeef"); 1197 as_observer->DestinationUpdate(200, 20);
1148 EXPECT_EQ(20l, item->CurrentSpeed()); 1198 EXPECT_EQ(20l, item->CurrentSpeed());
1149 EXPECT_EQ("livebeef", item->GetHashState());
1150 EXPECT_EQ(200l, item->GetReceivedBytes()); 1199 EXPECT_EQ(200l, item->GetReceivedBytes());
1151 EXPECT_EQ(0l, item->GetTotalBytes()); 1200 EXPECT_EQ(0l, item->GetTotalBytes());
1152 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); 1201 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
1153 1202
1154 CleanupItem(item, file, DownloadItem::IN_PROGRESS); 1203 CleanupItem(item, file, DownloadItem::IN_PROGRESS);
1155 } 1204 }
1156 1205
1157 TEST_F(DownloadItemTest, DestinationError) { 1206 TEST_F(DownloadItemTest, DestinationError_NoRestartRequired) {
1158 DownloadItemImpl* item = CreateDownloadItem(); 1207 DownloadItemImpl* item = CreateDownloadItem();
1159 MockDownloadFile* download_file = 1208 MockDownloadFile* download_file =
1160 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1209 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1161 base::WeakPtr<DownloadDestinationObserver> as_observer( 1210 base::WeakPtr<DownloadDestinationObserver> as_observer(
1162 item->DestinationObserverAsWeakPtr()); 1211 item->DestinationObserverAsWeakPtr());
1163 TestDownloadItemObserver observer(item); 1212 TestDownloadItemObserver observer(item);
1164 1213
1165 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1214 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1166 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason()); 1215 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason());
1167 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); 1216 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated());
1168 1217
1169 EXPECT_CALL(*download_file, Cancel()); 1218 scoped_ptr<crypto::SecureHash> hash(
1219 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
1220 hash->Update(kTestData1, sizeof(kTestData1));
1221
1222 EXPECT_CALL(*download_file, Detach());
1170 as_observer->DestinationError( 1223 as_observer->DestinationError(
1171 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); 1224 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 0, std::move(hash));
1172 mock_delegate()->VerifyAndClearExpectations(); 1225 mock_delegate()->VerifyAndClearExpectations();
1173 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); 1226 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
1174 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1227 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1175 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, 1228 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason());
1176 item->GetLastReason()); 1229 EXPECT_EQ(
1230 std::string(std::begin(kHashOfTestData1), std::end(kHashOfTestData1)),
1231 item->GetHash());
1232 }
1233 TEST_F(DownloadItemTest, DestinationError_RestartRequired) {
1234 DownloadItemImpl* item = CreateDownloadItem();
1235 MockDownloadFile* download_file =
1236 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1237 base::WeakPtr<DownloadDestinationObserver> as_observer(
1238 item->DestinationObserverAsWeakPtr());
1239 TestDownloadItemObserver observer(item);
1240
1241 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1242 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason());
1243 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated());
1244
1245 scoped_ptr<crypto::SecureHash> hash(
1246 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
1247 hash->Update(kTestData1, sizeof(kTestData1));
1248
1249 EXPECT_CALL(*download_file, Cancel());
1250 as_observer->DestinationError(
1251 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0, std::move(hash));
1252 mock_delegate()->VerifyAndClearExpectations();
1253 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
1254 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1255 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, item->GetLastReason());
1256 EXPECT_EQ(std::string(), item->GetHash());
1177 } 1257 }
1178 1258
1179 TEST_F(DownloadItemTest, DestinationCompleted) { 1259 TEST_F(DownloadItemTest, DestinationCompleted) {
1180 DownloadItemImpl* item = CreateDownloadItem(); 1260 DownloadItemImpl* item = CreateDownloadItem();
1181 MockDownloadFile* download_file = 1261 MockDownloadFile* download_file =
1182 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1262 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1183 base::WeakPtr<DownloadDestinationObserver> as_observer( 1263 base::WeakPtr<DownloadDestinationObserver> as_observer(
1184 item->DestinationObserverAsWeakPtr()); 1264 item->DestinationObserverAsWeakPtr());
1185 TestDownloadItemObserver observer(item); 1265 TestDownloadItemObserver observer(item);
1186 1266
1187 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1267 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1188 EXPECT_EQ("", item->GetHash()); 1268 EXPECT_EQ("", item->GetHash());
1189 EXPECT_EQ("", item->GetHashState());
1190 EXPECT_FALSE(item->AllDataSaved()); 1269 EXPECT_FALSE(item->AllDataSaved());
1191 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); 1270 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated());
1192 1271
1193 as_observer->DestinationUpdate(10, 20, "deadbeef"); 1272 as_observer->DestinationUpdate(10, 20);
1194 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); 1273 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
1195 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); // Confirm reset. 1274 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); // Confirm reset.
1196 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1275 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1197 EXPECT_EQ("", item->GetHash()); 1276 EXPECT_EQ("", item->GetHash());
1198 EXPECT_EQ("deadbeef", item->GetHashState());
1199 EXPECT_FALSE(item->AllDataSaved()); 1277 EXPECT_FALSE(item->AllDataSaved());
1200 1278
1279 scoped_ptr<crypto::SecureHash> hash(
1280 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
1281 hash->Update(kTestData1, sizeof(kTestData1));
1282
1201 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)); 1283 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _));
1202 as_observer->DestinationCompleted("livebeef"); 1284 as_observer->DestinationCompleted(10, std::move(hash));
1203 mock_delegate()->VerifyAndClearExpectations(); 1285 mock_delegate()->VerifyAndClearExpectations();
1204 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1286 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1205 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); 1287 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
1206 EXPECT_EQ("livebeef", item->GetHash()); 1288 EXPECT_EQ(
1207 EXPECT_EQ("", item->GetHashState()); 1289 std::string(std::begin(kHashOfTestData1), std::end(kHashOfTestData1)),
1290 item->GetHash());
1208 EXPECT_TRUE(item->AllDataSaved()); 1291 EXPECT_TRUE(item->AllDataSaved());
1209 1292
1210 // Even though the DownloadItem receives a DestinationCompleted() event, 1293 // Even though the DownloadItem receives a DestinationCompleted() event,
1211 // target determination hasn't completed, hence the download item is stuck in 1294 // target determination hasn't completed, hence the download item is stuck in
1212 // TARGET_PENDING. 1295 // TARGET_PENDING.
1213 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); 1296 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
1214 } 1297 }
1215 1298
1216 TEST_F(DownloadItemTest, EnabledActionsForNormalDownload) { 1299 TEST_F(DownloadItemTest, EnabledActionsForNormalDownload) {
1217 DownloadItemImpl* item = CreateDownloadItem(); 1300 DownloadItemImpl* item = CreateDownloadItem();
1218 MockDownloadFile* download_file = 1301 MockDownloadFile* download_file =
1219 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1302 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1220 1303
1221 // InProgress 1304 // InProgress
1222 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1305 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1223 ASSERT_FALSE(item->GetTargetFilePath().empty()); 1306 ASSERT_FALSE(item->GetTargetFilePath().empty());
1224 EXPECT_TRUE(item->CanShowInFolder()); 1307 EXPECT_TRUE(item->CanShowInFolder());
1225 EXPECT_TRUE(item->CanOpenDownload()); 1308 EXPECT_TRUE(item->CanOpenDownload());
1226 1309
1227 // Complete 1310 // Complete
1228 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) 1311 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _))
1229 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 1312 .WillOnce(ScheduleRenameAndAnnotateCallback(
1230 base::FilePath(kDummyTargetPath))); 1313 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath)));
1231 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1314 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
1232 .WillOnce(Return(true)); 1315 .WillOnce(Return(true));
1233 EXPECT_CALL(*download_file, FullPath()) 1316 EXPECT_CALL(*download_file, FullPath())
1234 .WillOnce(Return(base::FilePath())); 1317 .WillOnce(ReturnRefOfCopy(base::FilePath()));
1235 EXPECT_CALL(*download_file, Detach()); 1318 EXPECT_CALL(*download_file, Detach());
1236 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 1319 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
1320 0, scoped_ptr<crypto::SecureHash>());
1237 RunAllPendingInMessageLoops(); 1321 RunAllPendingInMessageLoops();
1238 1322
1239 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState()); 1323 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState());
1240 EXPECT_TRUE(item->CanShowInFolder()); 1324 EXPECT_TRUE(item->CanShowInFolder());
1241 EXPECT_TRUE(item->CanOpenDownload()); 1325 EXPECT_TRUE(item->CanOpenDownload());
1242 } 1326 }
1243 1327
1244 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { 1328 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) {
1245 DownloadItemImpl* item = CreateDownloadItem(); 1329 DownloadItemImpl* item = CreateDownloadItem();
1246 MockDownloadFile* download_file = 1330 MockDownloadFile* download_file =
1247 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1331 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1248 item->SetIsTemporary(true); 1332 item->SetIsTemporary(true);
1249 1333
1250 // InProgress Temporary 1334 // InProgress Temporary
1251 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1335 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1252 ASSERT_FALSE(item->GetTargetFilePath().empty()); 1336 ASSERT_FALSE(item->GetTargetFilePath().empty());
1253 ASSERT_TRUE(item->IsTemporary()); 1337 ASSERT_TRUE(item->IsTemporary());
1254 EXPECT_FALSE(item->CanShowInFolder()); 1338 EXPECT_FALSE(item->CanShowInFolder());
1255 EXPECT_FALSE(item->CanOpenDownload()); 1339 EXPECT_FALSE(item->CanOpenDownload());
1256 1340
1257 // Complete Temporary 1341 // Complete Temporary
1258 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1342 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
1259 .WillOnce(Return(true)); 1343 .WillOnce(Return(true));
1260 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) 1344 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _))
1261 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 1345 .WillOnce(ScheduleRenameAndAnnotateCallback(
1262 base::FilePath(kDummyTargetPath))); 1346 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath)));
1263 EXPECT_CALL(*download_file, FullPath()) 1347 EXPECT_CALL(*download_file, FullPath())
1264 .WillOnce(Return(base::FilePath())); 1348 .WillOnce(ReturnRefOfCopy(base::FilePath()));
1265 EXPECT_CALL(*download_file, Detach()); 1349 EXPECT_CALL(*download_file, Detach());
1266 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 1350 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
1351 0, scoped_ptr<crypto::SecureHash>());
1267 RunAllPendingInMessageLoops(); 1352 RunAllPendingInMessageLoops();
1268 1353
1269 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState()); 1354 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState());
1270 EXPECT_FALSE(item->CanShowInFolder()); 1355 EXPECT_FALSE(item->CanShowInFolder());
1271 EXPECT_FALSE(item->CanOpenDownload()); 1356 EXPECT_FALSE(item->CanOpenDownload());
1272 } 1357 }
1273 1358
1274 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { 1359 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) {
1275 DownloadItemImpl* item = CreateDownloadItem(); 1360 DownloadItemImpl* item = CreateDownloadItem();
1276 MockDownloadFile* download_file = 1361 MockDownloadFile* download_file =
1277 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1362 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1278 1363
1279 EXPECT_CALL(*download_file, Cancel()); 1364 EXPECT_CALL(*download_file, Cancel());
1280 item->DestinationObserverAsWeakPtr()->DestinationError( 1365 item->DestinationObserverAsWeakPtr()->DestinationError(
1281 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 1366 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED,
1367 0,
1368 scoped_ptr<crypto::SecureHash>());
1282 RunAllPendingInMessageLoops(); 1369 RunAllPendingInMessageLoops();
1283 1370
1284 ASSERT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1371 ASSERT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1285 ASSERT_FALSE(item->GetTargetFilePath().empty()); 1372 ASSERT_FALSE(item->GetTargetFilePath().empty());
1286 EXPECT_FALSE(item->CanShowInFolder()); 1373 EXPECT_FALSE(item->CanShowInFolder());
1287 EXPECT_TRUE(item->CanOpenDownload()); 1374 EXPECT_TRUE(item->CanOpenDownload());
1288 } 1375 }
1289 1376
1290 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) { 1377 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) {
1291 DownloadItemImpl* item = CreateDownloadItem(); 1378 DownloadItemImpl* item = CreateDownloadItem();
(...skipping 15 matching lines...) Expand all
1307 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) { 1394 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) {
1308 // Test to confirm that if we have a callback that returns true, 1395 // Test to confirm that if we have a callback that returns true,
1309 // we complete immediately. 1396 // we complete immediately.
1310 DownloadItemImpl* item = CreateDownloadItem(); 1397 DownloadItemImpl* item = CreateDownloadItem();
1311 MockDownloadFile* download_file = 1398 MockDownloadFile* download_file =
1312 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1399 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1313 1400
1314 // Drive the delegate interaction. 1401 // Drive the delegate interaction.
1315 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1402 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
1316 .WillOnce(Return(true)); 1403 .WillOnce(Return(true));
1317 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 1404 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
1405 0, scoped_ptr<crypto::SecureHash>());
1318 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1406 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1319 EXPECT_FALSE(item->IsDangerous()); 1407 EXPECT_FALSE(item->IsDangerous());
1320 1408
1321 // Make sure the download can complete. 1409 // Make sure the download can complete.
1322 EXPECT_CALL(*download_file, 1410 EXPECT_CALL(*download_file,
1323 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _)) 1411 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _))
1324 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 1412 .WillOnce(ScheduleRenameAndAnnotateCallback(
1325 base::FilePath(kDummyTargetPath))); 1413 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath)));
1326 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) 1414 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _))
1327 .WillOnce(Return(true)); 1415 .WillOnce(Return(true));
1328 EXPECT_CALL(*download_file, FullPath()) 1416 EXPECT_CALL(*download_file, FullPath())
1329 .WillOnce(Return(base::FilePath())); 1417 .WillOnce(ReturnRefOfCopy(base::FilePath()));
1330 EXPECT_CALL(*download_file, Detach()); 1418 EXPECT_CALL(*download_file, Detach());
1331 RunAllPendingInMessageLoops(); 1419 RunAllPendingInMessageLoops();
1332 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); 1420 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState());
1333 } 1421 }
1334 1422
1335 // Just delaying completion. 1423 // Just delaying completion.
1336 TEST_F(DownloadItemTest, CompleteDelegate_BlockOnce) { 1424 TEST_F(DownloadItemTest, CompleteDelegate_BlockOnce) {
1337 // Test to confirm that if we have a callback that returns true, 1425 // Test to confirm that if we have a callback that returns true,
1338 // we complete immediately. 1426 // we complete immediately.
1339 DownloadItemImpl* item = CreateDownloadItem(); 1427 DownloadItemImpl* item = CreateDownloadItem();
1340 MockDownloadFile* download_file = 1428 MockDownloadFile* download_file =
1341 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1429 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1342 1430
1343 // Drive the delegate interaction. 1431 // Drive the delegate interaction.
1344 base::Closure delegate_callback; 1432 base::Closure delegate_callback;
1345 base::Closure copy_delegate_callback; 1433 base::Closure copy_delegate_callback;
1346 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1434 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
1347 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), 1435 .WillOnce(DoAll(SaveArg<1>(&delegate_callback),
1348 Return(false))) 1436 Return(false)))
1349 .WillOnce(Return(true)); 1437 .WillOnce(Return(true));
1350 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 1438 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
1439 0, scoped_ptr<crypto::SecureHash>());
1351 ASSERT_FALSE(delegate_callback.is_null()); 1440 ASSERT_FALSE(delegate_callback.is_null());
1352 copy_delegate_callback = delegate_callback; 1441 copy_delegate_callback = delegate_callback;
1353 delegate_callback.Reset(); 1442 delegate_callback.Reset();
1354 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1443 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1355 copy_delegate_callback.Run(); 1444 copy_delegate_callback.Run();
1356 ASSERT_TRUE(delegate_callback.is_null()); 1445 ASSERT_TRUE(delegate_callback.is_null());
1357 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1446 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1358 EXPECT_FALSE(item->IsDangerous()); 1447 EXPECT_FALSE(item->IsDangerous());
1359 1448
1360 // Make sure the download can complete. 1449 // Make sure the download can complete.
1361 EXPECT_CALL(*download_file, 1450 EXPECT_CALL(*download_file,
1362 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _)) 1451 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _))
1363 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 1452 .WillOnce(ScheduleRenameAndAnnotateCallback(
1364 base::FilePath(kDummyTargetPath))); 1453 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath)));
1365 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) 1454 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _))
1366 .WillOnce(Return(true)); 1455 .WillOnce(Return(true));
1367 EXPECT_CALL(*download_file, FullPath()) 1456 EXPECT_CALL(*download_file, FullPath())
1368 .WillOnce(Return(base::FilePath())); 1457 .WillOnce(ReturnRefOfCopy(base::FilePath()));
1369 EXPECT_CALL(*download_file, Detach()); 1458 EXPECT_CALL(*download_file, Detach());
1370 RunAllPendingInMessageLoops(); 1459 RunAllPendingInMessageLoops();
1371 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); 1460 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState());
1372 } 1461 }
1373 1462
1374 // Delay and set danger. 1463 // Delay and set danger.
1375 TEST_F(DownloadItemTest, CompleteDelegate_SetDanger) { 1464 TEST_F(DownloadItemTest, CompleteDelegate_SetDanger) {
1376 // Test to confirm that if we have a callback that returns true, 1465 // Test to confirm that if we have a callback that returns true,
1377 // we complete immediately. 1466 // we complete immediately.
1378 DownloadItemImpl* item = CreateDownloadItem(); 1467 DownloadItemImpl* item = CreateDownloadItem();
1379 MockDownloadFile* download_file = 1468 MockDownloadFile* download_file =
1380 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1469 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1381 1470
1382 // Drive the delegate interaction. 1471 // Drive the delegate interaction.
1383 base::Closure delegate_callback; 1472 base::Closure delegate_callback;
1384 base::Closure copy_delegate_callback; 1473 base::Closure copy_delegate_callback;
1385 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1474 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
1386 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), 1475 .WillOnce(DoAll(SaveArg<1>(&delegate_callback),
1387 Return(false))) 1476 Return(false)))
1388 .WillOnce(Return(true)); 1477 .WillOnce(Return(true));
1389 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 1478 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
1479 0, scoped_ptr<crypto::SecureHash>());
1390 ASSERT_FALSE(delegate_callback.is_null()); 1480 ASSERT_FALSE(delegate_callback.is_null());
1391 copy_delegate_callback = delegate_callback; 1481 copy_delegate_callback = delegate_callback;
1392 delegate_callback.Reset(); 1482 delegate_callback.Reset();
1393 EXPECT_FALSE(item->IsDangerous()); 1483 EXPECT_FALSE(item->IsDangerous());
1394 item->OnContentCheckCompleted( 1484 item->OnContentCheckCompleted(
1395 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 1485 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1396 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1486 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1397 copy_delegate_callback.Run(); 1487 copy_delegate_callback.Run();
1398 ASSERT_TRUE(delegate_callback.is_null()); 1488 ASSERT_TRUE(delegate_callback.is_null());
1399 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1489 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1400 EXPECT_TRUE(item->IsDangerous()); 1490 EXPECT_TRUE(item->IsDangerous());
1401 1491
1402 // Make sure the download doesn't complete until we've validated it. 1492 // Make sure the download doesn't complete until we've validated it.
1403 EXPECT_CALL(*download_file, 1493 EXPECT_CALL(*download_file,
1404 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _)) 1494 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _))
1405 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 1495 .WillOnce(ScheduleRenameAndAnnotateCallback(
1406 base::FilePath(kDummyTargetPath))); 1496 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath)));
1407 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) 1497 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _))
1408 .WillOnce(Return(true)); 1498 .WillOnce(Return(true));
1409 EXPECT_CALL(*download_file, FullPath()) 1499 EXPECT_CALL(*download_file, FullPath())
1410 .WillOnce(Return(base::FilePath())); 1500 .WillOnce(ReturnRefOfCopy(base::FilePath()));
1411 EXPECT_CALL(*download_file, Detach()); 1501 EXPECT_CALL(*download_file, Detach());
1412 RunAllPendingInMessageLoops(); 1502 RunAllPendingInMessageLoops();
1413 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1503 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1414 EXPECT_TRUE(item->IsDangerous()); 1504 EXPECT_TRUE(item->IsDangerous());
1415 1505
1416 item->ValidateDangerousDownload(); 1506 item->ValidateDangerousDownload();
1417 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_USER_VALIDATED, item->GetDangerType()); 1507 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_USER_VALIDATED, item->GetDangerType());
1418 RunAllPendingInMessageLoops(); 1508 RunAllPendingInMessageLoops();
1419 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); 1509 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState());
1420 } 1510 }
1421 1511
1422 // Just delaying completion twice. 1512 // Just delaying completion twice.
1423 TEST_F(DownloadItemTest, CompleteDelegate_BlockTwice) { 1513 TEST_F(DownloadItemTest, CompleteDelegate_BlockTwice) {
1424 // Test to confirm that if we have a callback that returns true, 1514 // Test to confirm that if we have a callback that returns true,
1425 // we complete immediately. 1515 // we complete immediately.
1426 DownloadItemImpl* item = CreateDownloadItem(); 1516 DownloadItemImpl* item = CreateDownloadItem();
1427 MockDownloadFile* download_file = 1517 MockDownloadFile* download_file =
1428 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1518 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1429 1519
1430 // Drive the delegate interaction. 1520 // Drive the delegate interaction.
1431 base::Closure delegate_callback; 1521 base::Closure delegate_callback;
1432 base::Closure copy_delegate_callback; 1522 base::Closure copy_delegate_callback;
1433 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1523 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
1434 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), 1524 .WillOnce(DoAll(SaveArg<1>(&delegate_callback),
1435 Return(false))) 1525 Return(false)))
1436 .WillOnce(DoAll(SaveArg<1>(&delegate_callback), 1526 .WillOnce(DoAll(SaveArg<1>(&delegate_callback),
1437 Return(false))) 1527 Return(false)))
1438 .WillOnce(Return(true)); 1528 .WillOnce(Return(true));
1439 item->DestinationObserverAsWeakPtr()->DestinationCompleted(std::string()); 1529 item->DestinationObserverAsWeakPtr()->DestinationCompleted(
1530 0, scoped_ptr<crypto::SecureHash>());
1440 ASSERT_FALSE(delegate_callback.is_null()); 1531 ASSERT_FALSE(delegate_callback.is_null());
1441 copy_delegate_callback = delegate_callback; 1532 copy_delegate_callback = delegate_callback;
1442 delegate_callback.Reset(); 1533 delegate_callback.Reset();
1443 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1534 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1444 copy_delegate_callback.Run(); 1535 copy_delegate_callback.Run();
1445 ASSERT_FALSE(delegate_callback.is_null()); 1536 ASSERT_FALSE(delegate_callback.is_null());
1446 copy_delegate_callback = delegate_callback; 1537 copy_delegate_callback = delegate_callback;
1447 delegate_callback.Reset(); 1538 delegate_callback.Reset();
1448 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1539 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1449 copy_delegate_callback.Run(); 1540 copy_delegate_callback.Run();
1450 ASSERT_TRUE(delegate_callback.is_null()); 1541 ASSERT_TRUE(delegate_callback.is_null());
1451 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1542 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1452 EXPECT_FALSE(item->IsDangerous()); 1543 EXPECT_FALSE(item->IsDangerous());
1453 1544
1454 // Make sure the download can complete. 1545 // Make sure the download can complete.
1455 EXPECT_CALL(*download_file, 1546 EXPECT_CALL(*download_file,
1456 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _)) 1547 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _))
1457 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 1548 .WillOnce(ScheduleRenameAndAnnotateCallback(
1458 base::FilePath(kDummyTargetPath))); 1549 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath)));
1459 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) 1550 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _))
1460 .WillOnce(Return(true)); 1551 .WillOnce(Return(true));
1461 EXPECT_CALL(*download_file, FullPath()) 1552 EXPECT_CALL(*download_file, FullPath())
1462 .WillOnce(Return(base::FilePath())); 1553 .WillOnce(ReturnRefOfCopy(base::FilePath()));
1463 EXPECT_CALL(*download_file, Detach()); 1554 EXPECT_CALL(*download_file, Detach());
1464 RunAllPendingInMessageLoops(); 1555 RunAllPendingInMessageLoops();
1465 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); 1556 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState());
1466 } 1557 }
1467 1558
1468 TEST_F(DownloadItemTest, StealDangerousDownload) { 1559 TEST_F(DownloadItemTest, StealDangerousDownload) {
1469 DownloadItemImpl* item = CreateDownloadItem(); 1560 DownloadItemImpl* item = CreateDownloadItem();
1470 MockDownloadFile* download_file = 1561 MockDownloadFile* download_file =
1471 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 1562 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1472 ASSERT_TRUE(item->IsDangerous()); 1563 ASSERT_TRUE(item->IsDangerous());
1473 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt")); 1564 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt"));
1474 base::FilePath returned_path; 1565 base::FilePath returned_path;
1475 1566
1476 EXPECT_CALL(*download_file, FullPath()) 1567 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path));
1477 .WillOnce(Return(full_path));
1478 EXPECT_CALL(*download_file, Detach()); 1568 EXPECT_CALL(*download_file, Detach());
1479 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); 1569 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
1480 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); 1570 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1481 item->StealDangerousDownload( 1571 item->StealDangerousDownload(
1482 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, 1572 base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1483 weak_ptr_factory.GetWeakPtr(), 1573 weak_ptr_factory.GetWeakPtr(),
1484 base::Unretained(&returned_path))); 1574 base::Unretained(&returned_path)));
1485 RunAllPendingInMessageLoops(); 1575 RunAllPendingInMessageLoops();
1486 EXPECT_EQ(full_path, returned_path); 1576 EXPECT_EQ(full_path, returned_path);
1487 } 1577 }
1488 1578
1489 TEST_F(DownloadItemTest, StealInterruptedDangerousDownload) { 1579 TEST_F(DownloadItemTest, StealInterruptedDangerousDownload) {
1490 base::FilePath returned_path; 1580 base::FilePath returned_path;
1491 DownloadItemImpl* item = CreateDownloadItem(); 1581 DownloadItemImpl* item = CreateDownloadItem();
1492 MockDownloadFile* download_file = 1582 MockDownloadFile* download_file =
1493 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 1583 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1494 base::FilePath full_path = item->GetFullPath(); 1584 base::FilePath full_path = item->GetFullPath();
1495 EXPECT_FALSE(full_path.empty()); 1585 EXPECT_FALSE(full_path.empty());
1496 EXPECT_CALL(*download_file, FullPath()) 1586 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path));
1497 .WillOnce(Return(full_path));
1498 EXPECT_CALL(*download_file, Detach()); 1587 EXPECT_CALL(*download_file, Detach());
1499 item->DestinationObserverAsWeakPtr()->DestinationError( 1588 item->DestinationObserverAsWeakPtr()->DestinationError(
1500 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED); 1589 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED,
1590 0,
1591 scoped_ptr<crypto::SecureHash>());
1501 ASSERT_TRUE(item->IsDangerous()); 1592 ASSERT_TRUE(item->IsDangerous());
1502 1593
1503 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); 1594 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
1504 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); 1595 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1505 item->StealDangerousDownload( 1596 item->StealDangerousDownload(
1506 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, 1597 base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1507 weak_ptr_factory.GetWeakPtr(), 1598 weak_ptr_factory.GetWeakPtr(),
1508 base::Unretained(&returned_path))); 1599 base::Unretained(&returned_path)));
1509 RunAllPendingInMessageLoops(); 1600 RunAllPendingInMessageLoops();
1510 EXPECT_EQ(full_path, returned_path); 1601 EXPECT_EQ(full_path, returned_path);
1511 } 1602 }
1512 1603
1513 TEST_F(DownloadItemTest, StealInterruptedNonResumableDangerousDownload) { 1604 TEST_F(DownloadItemTest, StealInterruptedNonResumableDangerousDownload) {
1514 base::FilePath returned_path; 1605 base::FilePath returned_path;
1515 DownloadItemImpl* item = CreateDownloadItem(); 1606 DownloadItemImpl* item = CreateDownloadItem();
1516 MockDownloadFile* download_file = 1607 MockDownloadFile* download_file =
1517 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 1608 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1518 EXPECT_CALL(*download_file, Cancel()); 1609 EXPECT_CALL(*download_file, Cancel());
1519 item->DestinationObserverAsWeakPtr()->DestinationError( 1610 item->DestinationObserverAsWeakPtr()->DestinationError(
1520 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); 1611 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED,
1612 0,
1613 scoped_ptr<crypto::SecureHash>());
1521 ASSERT_TRUE(item->IsDangerous()); 1614 ASSERT_TRUE(item->IsDangerous());
1522 1615
1523 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); 1616 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
1524 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); 1617 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1525 item->StealDangerousDownload( 1618 item->StealDangerousDownload(
1526 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, 1619 base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1527 weak_ptr_factory.GetWeakPtr(), 1620 weak_ptr_factory.GetWeakPtr(),
1528 base::Unretained(&returned_path))); 1621 base::Unretained(&returned_path)));
1529 RunAllPendingInMessageLoops(); 1622 RunAllPendingInMessageLoops();
1530 EXPECT_TRUE(returned_path.empty()); 1623 EXPECT_TRUE(returned_path.empty());
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1597 // above. 1690 // above.
1598 const int kEventCount = 4; 1691 const int kEventCount = 4;
1599 1692
1600 // The following functions help us with currying the calls to 1693 // The following functions help us with currying the calls to
1601 // DownloadDestinationObserver. If std::bind was allowed along with 1694 // DownloadDestinationObserver. If std::bind was allowed along with
1602 // std::placeholders, it is possible to avoid these functions, but currently 1695 // std::placeholders, it is possible to avoid these functions, but currently
1603 // Chromium doesn't allow using std::bind for good reasons. 1696 // Chromium doesn't allow using std::bind for good reasons.
1604 void DestinationUpdateInvoker( 1697 void DestinationUpdateInvoker(
1605 int64_t bytes_so_far, 1698 int64_t bytes_so_far,
1606 int64_t bytes_per_sec, 1699 int64_t bytes_per_sec,
1607 const std::string& hash_state,
1608 base::WeakPtr<DownloadDestinationObserver> observer) { 1700 base::WeakPtr<DownloadDestinationObserver> observer) {
1609 DVLOG(20) << "DestinationUpdate(bytes_so_far:" << bytes_so_far 1701 DVLOG(20) << "DestinationUpdate(bytes_so_far:" << bytes_so_far
1610 << ", bytes_per_sec:" << bytes_per_sec 1702 << ", bytes_per_sec:" << bytes_per_sec
1611 << ", hash_state:" << hash_state << ") observer:" << !!observer; 1703 << ") observer:" << !!observer;
1612 if (observer) 1704 if (observer)
1613 observer->DestinationUpdate(bytes_so_far, bytes_per_sec, hash_state); 1705 observer->DestinationUpdate(bytes_so_far, bytes_per_sec);
1614 } 1706 }
1615 1707
1616 void DestinationErrorInvoker( 1708 void DestinationErrorInvoker(
1617 DownloadInterruptReason reason, 1709 DownloadInterruptReason reason,
1710 int64_t bytes_so_far,
1618 base::WeakPtr<DownloadDestinationObserver> observer) { 1711 base::WeakPtr<DownloadDestinationObserver> observer) {
1619 DVLOG(20) << "DestinationError(reason:" 1712 DVLOG(20) << "DestinationError(reason:"
1620 << DownloadInterruptReasonToString(reason) 1713 << DownloadInterruptReasonToString(reason)
1621 << ") observer:" << !!observer; 1714 << ", bytes_so_far:" << bytes_so_far << ") observer:" << !!observer;
1622 if (observer) 1715 if (observer)
1623 observer->DestinationError(reason); 1716 observer->DestinationError(
1717 reason, bytes_so_far, scoped_ptr<crypto::SecureHash>());
1624 } 1718 }
1625 1719
1626 void DestinationCompletedInvoker( 1720 void DestinationCompletedInvoker(
1627 const std::string& final_hash, 1721 int64_t total_bytes,
1628 base::WeakPtr<DownloadDestinationObserver> observer) { 1722 base::WeakPtr<DownloadDestinationObserver> observer) {
1629 DVLOG(20) << "DestinationComplete(final_hash:" << final_hash 1723 DVLOG(20) << "DestinationComplete(total_bytes:" << total_bytes
1630 << ") observer:" << !!observer; 1724 << ") observer:" << !!observer;
1631 if (observer) 1725 if (observer)
1632 observer->DestinationCompleted(final_hash); 1726 observer->DestinationCompleted(total_bytes,
1727 scoped_ptr<crypto::SecureHash>());
1633 } 1728 }
1634 1729
1635 // Given a set of observations (via the range |begin|..|end|), constructs a list 1730 // Given a set of observations (via the range |begin|..|end|), constructs a list
1636 // of EventLists such that: 1731 // of EventLists such that:
1637 // 1732 //
1638 // * There are exactly |event_count| ObservationSets in each EventList. 1733 // * There are exactly |event_count| ObservationSets in each EventList.
1639 // 1734 //
1640 // * Each ObservationList in each EventList contains a subrange (possibly empty) 1735 // * Each ObservationList in each EventList contains a subrange (possibly empty)
1641 // of observations from the input range, in the same order as the input range. 1736 // of observations from the input range, in the same order as the input range.
1642 // 1737 //
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1679 // 2. Immediately after the DownloadTargetCallback is invoked. 1774 // 2. Immediately after the DownloadTargetCallback is invoked.
1680 // 3. Immediately after the intermediate file is renamed. 1775 // 3. Immediately after the intermediate file is renamed.
1681 // 1776 //
1682 // We are going to take a couple of sets of DownloadDestinationObserver events 1777 // We are going to take a couple of sets of DownloadDestinationObserver events
1683 // and distribute them into the three events described above. And then we are 1778 // and distribute them into the three events described above. And then we are
1684 // going to invoke the observations while a DownloadItemImpl is carefully 1779 // going to invoke the observations while a DownloadItemImpl is carefully
1685 // stepped through its stages. 1780 // stepped through its stages.
1686 1781
1687 std::vector<EventList> GenerateSuccessfulEventLists() { 1782 std::vector<EventList> GenerateSuccessfulEventLists() {
1688 std::vector<CurriedObservation> all_observations; 1783 std::vector<CurriedObservation> all_observations;
1689 all_observations.push_back( 1784 all_observations.push_back(base::Bind(&DestinationUpdateInvoker, 100, 100));
1690 base::Bind(&DestinationUpdateInvoker, 100, 100, "abc")); 1785 all_observations.push_back(base::Bind(&DestinationUpdateInvoker, 200, 100));
1691 all_observations.push_back( 1786 all_observations.push_back(base::Bind(&DestinationCompletedInvoker, 200));
1692 base::Bind(&DestinationUpdateInvoker, 200, 100, "def"));
1693 all_observations.push_back(
1694 base::Bind(&DestinationCompletedInvoker, "final-hash-1"));
1695 return DistributeObservationsIntoEvents(all_observations.begin(), 1787 return DistributeObservationsIntoEvents(all_observations.begin(),
1696 all_observations.end(), kEventCount); 1788 all_observations.end(), kEventCount);
1697 } 1789 }
1698 1790
1699 std::vector<EventList> GenerateFailingEventLists() { 1791 std::vector<EventList> GenerateFailingEventLists() {
1700 std::vector<CurriedObservation> all_observations; 1792 std::vector<CurriedObservation> all_observations;
1701 all_observations.push_back( 1793 all_observations.push_back(base::Bind(&DestinationUpdateInvoker, 100, 100));
1702 base::Bind(&DestinationUpdateInvoker, 100, 100, "abc"));
1703 all_observations.push_back(base::Bind( 1794 all_observations.push_back(base::Bind(
1704 &DestinationErrorInvoker, DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); 1795 &DestinationErrorInvoker, DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 100));
1705 return DistributeObservationsIntoEvents(all_observations.begin(), 1796 return DistributeObservationsIntoEvents(all_observations.begin(),
1706 all_observations.end(), kEventCount); 1797 all_observations.end(), kEventCount);
1707 } 1798 }
1708 1799
1709 class DownloadItemDestinationUpdateRaceTest 1800 class DownloadItemDestinationUpdateRaceTest
1710 : public DownloadItemTest, 1801 : public DownloadItemTest,
1711 public ::testing::WithParamInterface<EventList> { 1802 public ::testing::WithParamInterface<EventList> {
1712 public: 1803 public:
1713 DownloadItemDestinationUpdateRaceTest() 1804 DownloadItemDestinationUpdateRaceTest()
1714 : DownloadItemTest(), 1805 : DownloadItemTest(),
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
1874 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameSucceeds) { 1965 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameSucceeds) {
1875 // We expect either that the download will fail (in which case the request and 1966 // We expect either that the download will fail (in which case the request and
1876 // the download file will be cancelled), or it will succeed (in which case the 1967 // the download file will be cancelled), or it will succeed (in which case the
1877 // DownloadFile will Detach()). It depends on the list of observations that 1968 // DownloadFile will Detach()). It depends on the list of observations that
1878 // are given to us. 1969 // are given to us.
1879 EXPECT_CALL(*file_, Cancel()).Times(::testing::AnyNumber()); 1970 EXPECT_CALL(*file_, Cancel()).Times(::testing::AnyNumber());
1880 EXPECT_CALL(*request_handle_, CancelRequest()).Times(::testing::AnyNumber()); 1971 EXPECT_CALL(*request_handle_, CancelRequest()).Times(::testing::AnyNumber());
1881 EXPECT_CALL(*file_, Detach()).Times(::testing::AnyNumber()); 1972 EXPECT_CALL(*file_, Detach()).Times(::testing::AnyNumber());
1882 1973
1883 EXPECT_CALL(*file_, FullPath()) 1974 EXPECT_CALL(*file_, FullPath())
1884 .WillRepeatedly(Return(base::FilePath(kDummyIntermediatePath))); 1975 .WillRepeatedly(ReturnRefOfCopy(base::FilePath(kDummyIntermediatePath)));
1885 1976
1886 // Intermediate rename loop is not used immediately, but let's set up the 1977 // Intermediate rename loop is not used immediately, but let's set up the
1887 // DownloadFile expectations since we are about to transfer its ownership to 1978 // DownloadFile expectations since we are about to transfer its ownership to
1888 // the DownloadItem. 1979 // the DownloadItem.
1889 base::RunLoop intermediate_rename_loop; 1980 base::RunLoop intermediate_rename_loop;
1890 DownloadFile::RenameCompletionCallback intermediate_rename_callback; 1981 DownloadFile::RenameCompletionCallback intermediate_rename_callback;
1891 EXPECT_CALL(*file_, RenameAndUniquify(_, _)) 1982 EXPECT_CALL(*file_, RenameAndUniquify(_, _))
1892 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback), 1983 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback),
1893 ScheduleClosure(intermediate_rename_loop.QuitClosure()))); 1984 ScheduleClosure(intermediate_rename_loop.QuitClosure())));
1894 1985
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1952 2043
1953 item_->Cancel(true); 2044 item_->Cancel(true);
1954 RunAllPendingInMessageLoops(); 2045 RunAllPendingInMessageLoops();
1955 } 2046 }
1956 2047
1957 TEST(MockDownloadItem, Compiles) { 2048 TEST(MockDownloadItem, Compiles) {
1958 MockDownloadItem mock_item; 2049 MockDownloadItem mock_item;
1959 } 2050 }
1960 2051
1961 } // namespace content 2052 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/download_item_impl_delegate.cc ('k') | content/browser/download/download_manager_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698