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

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

Powered by Google App Engine
This is Rietveld 408576698