| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <set> | 5 #include <set> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "chrome/browser/download/download_history.h" | 10 #include "chrome/browser/download/download_history.h" |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 DISALLOW_COPY_AND_ASSIGN(FakeHistoryAdapter); | 191 DISALLOW_COPY_AND_ASSIGN(FakeHistoryAdapter); |
| 192 }; | 192 }; |
| 193 | 193 |
| 194 class DownloadHistoryTest : public testing::Test { | 194 class DownloadHistoryTest : public testing::Test { |
| 195 public: | 195 public: |
| 196 DownloadHistoryTest() | 196 DownloadHistoryTest() |
| 197 : ui_thread_(content::BrowserThread::UI, &loop_), | 197 : ui_thread_(content::BrowserThread::UI, &loop_), |
| 198 manager_(new content::MockDownloadManager()), | 198 manager_(new content::MockDownloadManager()), |
| 199 history_(NULL), | 199 history_(NULL), |
| 200 manager_observer_(NULL), | 200 manager_observer_(NULL), |
| 201 item_observer_(NULL), | |
| 202 download_created_index_(0) {} | 201 download_created_index_(0) {} |
| 203 virtual ~DownloadHistoryTest() { | 202 virtual ~DownloadHistoryTest() { |
| 204 STLDeleteElements(&items_); | 203 STLDeleteElements(&items_); |
| 205 } | 204 } |
| 206 | 205 |
| 207 protected: | 206 protected: |
| 208 virtual void TearDown() OVERRIDE { | 207 virtual void TearDown() OVERRIDE { |
| 209 download_history_.reset(); | 208 download_history_.reset(); |
| 210 } | 209 } |
| 211 | 210 |
| 212 content::MockDownloadManager& manager() { return *manager_.get(); } | 211 content::MockDownloadManager& manager() { return *manager_.get(); } |
| 213 content::MockDownloadItem& item(size_t index) { return *items_[index]; } | 212 content::MockDownloadItem& item(size_t index) { return *items_[index]; } |
| 214 | 213 |
| 215 void SetManagerObserver( | 214 void SetManagerObserver( |
| 216 content::DownloadManager::Observer* manager_observer) { | 215 content::DownloadManager::Observer* manager_observer) { |
| 217 manager_observer_ = manager_observer; | 216 manager_observer_ = manager_observer; |
| 218 } | 217 } |
| 219 content::DownloadManager::Observer* manager_observer() { | 218 content::DownloadManager::Observer* manager_observer() { |
| 220 return manager_observer_; | 219 return manager_observer_; |
| 221 } | 220 } |
| 222 | 221 |
| 223 // Relies on the same object observing all download items. | |
| 224 void SetItemObserver( | |
| 225 content::DownloadItem::Observer* item_observer) { | |
| 226 item_observer_ = item_observer; | |
| 227 } | |
| 228 content::DownloadItem::Observer* item_observer() { | |
| 229 return item_observer_; | |
| 230 } | |
| 231 | |
| 232 void ExpectWillQueryDownloads(scoped_ptr<InfoVector> infos) { | 222 void ExpectWillQueryDownloads(scoped_ptr<InfoVector> infos) { |
| 233 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 223 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 234 CHECK(infos.get()); | 224 CHECK(infos.get()); |
| 235 EXPECT_CALL(manager(), AddObserver(_)).WillOnce(WithArg<0>(Invoke( | 225 EXPECT_CALL(manager(), AddObserver(_)).WillOnce(WithArg<0>(Invoke( |
| 236 this, &DownloadHistoryTest::SetManagerObserver))); | 226 this, &DownloadHistoryTest::SetManagerObserver))); |
| 237 EXPECT_CALL(manager(), RemoveObserver(_)); | 227 EXPECT_CALL(manager(), RemoveObserver(_)); |
| 238 download_created_index_ = 0; | 228 download_created_index_ = 0; |
| 239 for (size_t index = 0; index < infos->size(); ++index) { | 229 for (size_t index = 0; index < infos->size(); ++index) { |
| 240 content::MockDownloadManager::CreateDownloadItemAdapter adapter( | 230 content::MockDownloadManager::CreateDownloadItemAdapter adapter( |
| 241 infos->at(index).id, | 231 infos->at(index).id, |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 EXPECT_CALL(item(index), GetDangerType()) | 415 EXPECT_CALL(item(index), GetDangerType()) |
| 426 .WillRepeatedly(Return(danger_type)); | 416 .WillRepeatedly(Return(danger_type)); |
| 427 EXPECT_CALL(item(index), GetLastReason()) | 417 EXPECT_CALL(item(index), GetLastReason()) |
| 428 .WillRepeatedly(Return(interrupt_reason)); | 418 .WillRepeatedly(Return(interrupt_reason)); |
| 429 EXPECT_CALL(item(index), GetOpened()).WillRepeatedly(Return(opened)); | 419 EXPECT_CALL(item(index), GetOpened()).WillRepeatedly(Return(opened)); |
| 430 EXPECT_CALL(item(index), GetTargetDisposition()) | 420 EXPECT_CALL(item(index), GetTargetDisposition()) |
| 431 .WillRepeatedly( | 421 .WillRepeatedly( |
| 432 Return(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE)); | 422 Return(content::DownloadItem::TARGET_DISPOSITION_OVERWRITE)); |
| 433 EXPECT_CALL(manager(), GetDownload(id)) | 423 EXPECT_CALL(manager(), GetDownload(id)) |
| 434 .WillRepeatedly(Return(&item(index))); | 424 .WillRepeatedly(Return(&item(index))); |
| 435 EXPECT_CALL(item(index), AddObserver(_)) | |
| 436 .WillOnce(WithArg<0>( | |
| 437 Invoke(this, &DownloadHistoryTest::SetItemObserver))); | |
| 438 EXPECT_CALL(item(index), RemoveObserver(_)); | |
| 439 EXPECT_CALL(item(index), IsTemporary()).WillRepeatedly(Return(false)); | 425 EXPECT_CALL(item(index), IsTemporary()).WillRepeatedly(Return(false)); |
| 440 #if !defined(OS_ANDROID) | 426 #if !defined(OS_ANDROID) |
| 441 new extensions::DownloadedByExtension( | 427 new extensions::DownloadedByExtension( |
| 442 &item(index), by_extension_id, by_extension_name); | 428 &item(index), by_extension_id, by_extension_name); |
| 443 #endif | 429 #endif |
| 444 | 430 |
| 445 std::vector<content::DownloadItem*> items; | 431 std::vector<content::DownloadItem*> items; |
| 446 for (size_t i = 0; i < items_.size(); ++i) { | 432 for (size_t i = 0; i < items_.size(); ++i) { |
| 447 items.push_back(&item(i)); | 433 items.push_back(&item(i)); |
| 448 } | 434 } |
| 449 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)) | 435 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)) |
| 450 .WillRepeatedly(SetArgPointee<0>(items)); | 436 .WillRepeatedly(SetArgPointee<0>(items)); |
| 451 } | 437 } |
| 452 | 438 |
| 453 private: | 439 private: |
| 454 base::MessageLoopForUI loop_; | 440 base::MessageLoopForUI loop_; |
| 455 content::TestBrowserThread ui_thread_; | 441 content::TestBrowserThread ui_thread_; |
| 456 std::vector<StrictMockDownloadItem*> items_; | 442 std::vector<StrictMockDownloadItem*> items_; |
| 457 scoped_ptr<content::MockDownloadManager> manager_; | 443 scoped_ptr<content::MockDownloadManager> manager_; |
| 458 FakeHistoryAdapter* history_; | 444 FakeHistoryAdapter* history_; |
| 459 scoped_ptr<DownloadHistory> download_history_; | 445 scoped_ptr<DownloadHistory> download_history_; |
| 460 content::DownloadManager::Observer* manager_observer_; | 446 content::DownloadManager::Observer* manager_observer_; |
| 461 content::DownloadItem::Observer* item_observer_; | |
| 462 size_t download_created_index_; | 447 size_t download_created_index_; |
| 463 | 448 |
| 464 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryTest); | 449 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryTest); |
| 465 }; | 450 }; |
| 466 | 451 |
| 467 } // namespace | 452 } // namespace |
| 468 | 453 |
| 469 // Test loading an item from the database, changing it, saving it back, removing | 454 // Test loading an item from the database, changing it, saving it back, removing |
| 470 // it. | 455 // it. |
| 471 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Load) { | 456 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Load) { |
| 472 // Load a download from history, create the item, OnDownloadCreated, | 457 // Load a download from history, create the item, OnDownloadCreated, |
| 473 // OnDownloadUpdated, OnDownloadRemoved. | 458 // OnDownloadUpdated, OnDownloadRemoved. |
| 474 history::DownloadRow info; | 459 history::DownloadRow info; |
| 475 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 460 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
| 476 "http://example.com/bar.pdf", | 461 "http://example.com/bar.pdf", |
| 477 "http://example.com/referrer.html", | 462 "http://example.com/referrer.html", |
| 478 &info); | 463 &info); |
| 479 { | 464 { |
| 480 scoped_ptr<InfoVector> infos(new InfoVector()); | 465 scoped_ptr<InfoVector> infos(new InfoVector()); |
| 481 infos->push_back(info); | 466 infos->push_back(info); |
| 482 ExpectWillQueryDownloads(infos.Pass()); | 467 ExpectWillQueryDownloads(infos.Pass()); |
| 483 ExpectNoDownloadCreated(); | 468 ExpectNoDownloadCreated(); |
| 484 } | 469 } |
| 485 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 470 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 486 | 471 |
| 487 // Pretend that something changed on the item. | 472 // Pretend that something changed on the item. |
| 488 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); | 473 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); |
| 489 item_observer()->OnDownloadUpdated(&item(0)); | 474 item(0).NotifyObserversDownloadUpdated(); |
| 490 info.opened = true; | 475 info.opened = true; |
| 491 ExpectDownloadUpdated(info); | 476 ExpectDownloadUpdated(info); |
| 492 | 477 |
| 493 // Pretend that the user removed the item. | 478 // Pretend that the user removed the item. |
| 494 IdSet ids; | 479 IdSet ids; |
| 495 ids.insert(info.id); | 480 ids.insert(info.id); |
| 496 item_observer()->OnDownloadRemoved(&item(0)); | 481 item(0).NotifyObserversDownloadRemoved(); |
| 497 ExpectDownloadsRemoved(ids); | 482 ExpectDownloadsRemoved(ids); |
| 498 } | 483 } |
| 499 | 484 |
| 500 // Test creating an item, saving it to the database, changing it, saving it | 485 // Test creating an item, saving it to the database, changing it, saving it |
| 501 // back, removing it. | 486 // back, removing it. |
| 502 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Create) { | 487 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Create) { |
| 503 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | 488 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
| 504 // OnDownloadRemoved. | 489 // OnDownloadRemoved. |
| 505 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 490 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); |
| 506 | 491 |
| 507 history::DownloadRow info; | 492 history::DownloadRow info; |
| 508 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 493 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
| 509 "http://example.com/bar.pdf", | 494 "http://example.com/bar.pdf", |
| 510 "http://example.com/referrer.html", | 495 "http://example.com/referrer.html", |
| 511 &info); | 496 &info); |
| 512 | 497 |
| 513 // Pretend the manager just created |item|. | 498 // Pretend the manager just created |item|. |
| 514 CallOnDownloadCreated(0); | 499 CallOnDownloadCreated(0); |
| 515 ExpectDownloadCreated(info); | 500 ExpectDownloadCreated(info); |
| 516 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 501 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 517 | 502 |
| 518 // Pretend that something changed on the item. | 503 // Pretend that something changed on the item. |
| 519 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); | 504 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); |
| 520 item_observer()->OnDownloadUpdated(&item(0)); | 505 item(0).NotifyObserversDownloadUpdated(); |
| 521 info.opened = true; | 506 info.opened = true; |
| 522 ExpectDownloadUpdated(info); | 507 ExpectDownloadUpdated(info); |
| 523 | 508 |
| 524 // Pretend that the user removed the item. | 509 // Pretend that the user removed the item. |
| 525 IdSet ids; | 510 IdSet ids; |
| 526 ids.insert(info.id); | 511 ids.insert(info.id); |
| 527 item_observer()->OnDownloadRemoved(&item(0)); | 512 item(0).NotifyObserversDownloadRemoved(); |
| 528 ExpectDownloadsRemoved(ids); | 513 ExpectDownloadsRemoved(ids); |
| 529 } | 514 } |
| 530 | 515 |
| 531 // Test that changes to persisted fields in a DownloadItem triggers database | 516 // Test that changes to persisted fields in a DownloadItem triggers database |
| 532 // updates. | 517 // updates. |
| 533 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Update) { | 518 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Update) { |
| 534 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 519 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); |
| 535 | 520 |
| 536 history::DownloadRow info; | 521 history::DownloadRow info; |
| 537 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 522 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
| 538 "http://example.com/bar.pdf", | 523 "http://example.com/bar.pdf", |
| 539 "http://example.com/referrer.html", | 524 "http://example.com/referrer.html", |
| 540 &info); | 525 &info); |
| 541 CallOnDownloadCreated(0); | 526 CallOnDownloadCreated(0); |
| 542 ExpectDownloadCreated(info); | 527 ExpectDownloadCreated(info); |
| 543 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 528 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 544 | 529 |
| 545 base::FilePath new_path(FILE_PATH_LITERAL("/foo/baz.txt")); | 530 base::FilePath new_path(FILE_PATH_LITERAL("/foo/baz.txt")); |
| 546 base::Time new_time(base::Time::Now()); | 531 base::Time new_time(base::Time::Now()); |
| 547 std::string new_etag("new etag"); | 532 std::string new_etag("new etag"); |
| 548 std::string new_last_modifed("new last modified"); | 533 std::string new_last_modifed("new last modified"); |
| 549 | 534 |
| 550 // current_path | 535 // current_path |
| 551 EXPECT_CALL(item(0), GetFullPath()).WillRepeatedly(ReturnRefOfCopy(new_path)); | 536 EXPECT_CALL(item(0), GetFullPath()).WillRepeatedly(ReturnRefOfCopy(new_path)); |
| 552 info.current_path = new_path; | 537 info.current_path = new_path; |
| 553 item_observer()->OnDownloadUpdated(&item(0)); | 538 item(0).NotifyObserversDownloadUpdated(); |
| 554 ExpectDownloadUpdated(info); | 539 ExpectDownloadUpdated(info); |
| 555 | 540 |
| 556 // target_path | 541 // target_path |
| 557 EXPECT_CALL(item(0), GetTargetFilePath()) | 542 EXPECT_CALL(item(0), GetTargetFilePath()) |
| 558 .WillRepeatedly(ReturnRefOfCopy(new_path)); | 543 .WillRepeatedly(ReturnRefOfCopy(new_path)); |
| 559 info.target_path = new_path; | 544 info.target_path = new_path; |
| 560 item_observer()->OnDownloadUpdated(&item(0)); | 545 item(0).NotifyObserversDownloadUpdated(); |
| 561 ExpectDownloadUpdated(info); | 546 ExpectDownloadUpdated(info); |
| 562 | 547 |
| 563 // end_time | 548 // end_time |
| 564 EXPECT_CALL(item(0), GetEndTime()).WillRepeatedly(Return(new_time)); | 549 EXPECT_CALL(item(0), GetEndTime()).WillRepeatedly(Return(new_time)); |
| 565 info.end_time = new_time; | 550 info.end_time = new_time; |
| 566 item_observer()->OnDownloadUpdated(&item(0)); | 551 item(0).NotifyObserversDownloadUpdated(); |
| 567 ExpectDownloadUpdated(info); | 552 ExpectDownloadUpdated(info); |
| 568 | 553 |
| 569 // received_bytes | 554 // received_bytes |
| 570 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(101)); | 555 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(101)); |
| 571 info.received_bytes = 101; | 556 info.received_bytes = 101; |
| 572 item_observer()->OnDownloadUpdated(&item(0)); | 557 item(0).NotifyObserversDownloadUpdated(); |
| 573 ExpectDownloadUpdated(info); | 558 ExpectDownloadUpdated(info); |
| 574 | 559 |
| 575 // total_bytes | 560 // total_bytes |
| 576 EXPECT_CALL(item(0), GetTotalBytes()).WillRepeatedly(Return(102)); | 561 EXPECT_CALL(item(0), GetTotalBytes()).WillRepeatedly(Return(102)); |
| 577 info.total_bytes = 102; | 562 info.total_bytes = 102; |
| 578 item_observer()->OnDownloadUpdated(&item(0)); | 563 item(0).NotifyObserversDownloadUpdated(); |
| 579 ExpectDownloadUpdated(info); | 564 ExpectDownloadUpdated(info); |
| 580 | 565 |
| 581 // etag | 566 // etag |
| 582 EXPECT_CALL(item(0), GetETag()).WillRepeatedly(ReturnRefOfCopy(new_etag)); | 567 EXPECT_CALL(item(0), GetETag()).WillRepeatedly(ReturnRefOfCopy(new_etag)); |
| 583 info.etag = new_etag; | 568 info.etag = new_etag; |
| 584 item_observer()->OnDownloadUpdated(&item(0)); | 569 item(0).NotifyObserversDownloadUpdated(); |
| 585 ExpectDownloadUpdated(info); | 570 ExpectDownloadUpdated(info); |
| 586 | 571 |
| 587 // last_modified | 572 // last_modified |
| 588 EXPECT_CALL(item(0), GetLastModifiedTime()) | 573 EXPECT_CALL(item(0), GetLastModifiedTime()) |
| 589 .WillRepeatedly(ReturnRefOfCopy(new_last_modifed)); | 574 .WillRepeatedly(ReturnRefOfCopy(new_last_modifed)); |
| 590 info.last_modified = new_last_modifed; | 575 info.last_modified = new_last_modifed; |
| 591 item_observer()->OnDownloadUpdated(&item(0)); | 576 item(0).NotifyObserversDownloadUpdated(); |
| 592 ExpectDownloadUpdated(info); | 577 ExpectDownloadUpdated(info); |
| 593 | 578 |
| 594 // state | 579 // state |
| 595 EXPECT_CALL(item(0), GetState()) | 580 EXPECT_CALL(item(0), GetState()) |
| 596 .WillRepeatedly(Return(content::DownloadItem::INTERRUPTED)); | 581 .WillRepeatedly(Return(content::DownloadItem::INTERRUPTED)); |
| 597 info.state = content::DownloadItem::INTERRUPTED; | 582 info.state = content::DownloadItem::INTERRUPTED; |
| 598 item_observer()->OnDownloadUpdated(&item(0)); | 583 item(0).NotifyObserversDownloadUpdated(); |
| 599 ExpectDownloadUpdated(info); | 584 ExpectDownloadUpdated(info); |
| 600 | 585 |
| 601 // danger_type | 586 // danger_type |
| 602 EXPECT_CALL(item(0), GetDangerType()) | 587 EXPECT_CALL(item(0), GetDangerType()) |
| 603 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT)); | 588 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT)); |
| 604 info.danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT; | 589 info.danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT; |
| 605 item_observer()->OnDownloadUpdated(&item(0)); | 590 item(0).NotifyObserversDownloadUpdated(); |
| 606 ExpectDownloadUpdated(info); | 591 ExpectDownloadUpdated(info); |
| 607 | 592 |
| 608 // interrupt_reason | 593 // interrupt_reason |
| 609 EXPECT_CALL(item(0), GetLastReason()) | 594 EXPECT_CALL(item(0), GetLastReason()) |
| 610 .WillRepeatedly(Return(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED)); | 595 .WillRepeatedly(Return(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED)); |
| 611 info.interrupt_reason = content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; | 596 info.interrupt_reason = content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; |
| 612 item_observer()->OnDownloadUpdated(&item(0)); | 597 item(0).NotifyObserversDownloadUpdated(); |
| 613 ExpectDownloadUpdated(info); | 598 ExpectDownloadUpdated(info); |
| 614 | 599 |
| 615 // opened | 600 // opened |
| 616 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); | 601 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); |
| 617 info.opened = true; | 602 info.opened = true; |
| 618 item_observer()->OnDownloadUpdated(&item(0)); | 603 item(0).NotifyObserversDownloadUpdated(); |
| 619 ExpectDownloadUpdated(info); | 604 ExpectDownloadUpdated(info); |
| 620 } | 605 } |
| 621 | 606 |
| 622 // Test creating a new item, saving it, removing it by setting it Temporary, | 607 // Test creating a new item, saving it, removing it by setting it Temporary, |
| 623 // changing it without saving it back because it's Temporary, clearing | 608 // changing it without saving it back because it's Temporary, clearing |
| 624 // IsTemporary, saving it back, changing it, saving it back because it isn't | 609 // IsTemporary, saving it back, changing it, saving it back because it isn't |
| 625 // Temporary anymore. | 610 // Temporary anymore. |
| 626 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Temporary) { | 611 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Temporary) { |
| 627 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | 612 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
| 628 // OnDownloadRemoved. | 613 // OnDownloadRemoved. |
| 629 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 614 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); |
| 630 | 615 |
| 631 history::DownloadRow info; | 616 history::DownloadRow info; |
| 632 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 617 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
| 633 "http://example.com/bar.pdf", | 618 "http://example.com/bar.pdf", |
| 634 "http://example.com/referrer.html", | 619 "http://example.com/referrer.html", |
| 635 &info); | 620 &info); |
| 636 | 621 |
| 637 // Pretend the manager just created |item|. | 622 // Pretend the manager just created |item|. |
| 638 CallOnDownloadCreated(0); | 623 CallOnDownloadCreated(0); |
| 639 ExpectDownloadCreated(info); | 624 ExpectDownloadCreated(info); |
| 640 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 625 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 641 | 626 |
| 642 // Pretend the item was marked temporary. DownloadHistory should remove it | 627 // Pretend the item was marked temporary. DownloadHistory should remove it |
| 643 // from history and start ignoring it. | 628 // from history and start ignoring it. |
| 644 EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(true)); | 629 EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(true)); |
| 645 item_observer()->OnDownloadUpdated(&item(0)); | 630 item(0).NotifyObserversDownloadUpdated(); |
| 646 IdSet ids; | 631 IdSet ids; |
| 647 ids.insert(info.id); | 632 ids.insert(info.id); |
| 648 ExpectDownloadsRemoved(ids); | 633 ExpectDownloadsRemoved(ids); |
| 649 | 634 |
| 650 // Change something that would make DownloadHistory call UpdateDownload if the | 635 // Change something that would make DownloadHistory call UpdateDownload if the |
| 651 // item weren't temporary. | 636 // item weren't temporary. |
| 652 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(4200)); | 637 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(4200)); |
| 653 item_observer()->OnDownloadUpdated(&item(0)); | 638 item(0).NotifyObserversDownloadUpdated(); |
| 654 ExpectNoDownloadUpdated(); | 639 ExpectNoDownloadUpdated(); |
| 655 | 640 |
| 656 // Changing a temporary item back to a non-temporary item should make | 641 // Changing a temporary item back to a non-temporary item should make |
| 657 // DownloadHistory call CreateDownload. | 642 // DownloadHistory call CreateDownload. |
| 658 EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(false)); | 643 EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(false)); |
| 659 item_observer()->OnDownloadUpdated(&item(0)); | 644 item(0).NotifyObserversDownloadUpdated(); |
| 660 info.received_bytes = 4200; | 645 info.received_bytes = 4200; |
| 661 ExpectDownloadCreated(info); | 646 ExpectDownloadCreated(info); |
| 662 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 647 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 663 | 648 |
| 664 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); | 649 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); |
| 665 item_observer()->OnDownloadUpdated(&item(0)); | 650 item(0).NotifyObserversDownloadUpdated(); |
| 666 info.received_bytes = 100; | 651 info.received_bytes = 100; |
| 667 ExpectDownloadUpdated(info); | 652 ExpectDownloadUpdated(info); |
| 668 } | 653 } |
| 669 | 654 |
| 670 // Test removing downloads while they're still being added. | 655 // Test removing downloads while they're still being added. |
| 671 TEST_F(DownloadHistoryTest, DownloadHistoryTest_RemoveWhileAdding) { | 656 TEST_F(DownloadHistoryTest, DownloadHistoryTest_RemoveWhileAdding) { |
| 672 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 657 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); |
| 673 | 658 |
| 674 history::DownloadRow info; | 659 history::DownloadRow info; |
| 675 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 660 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
| 676 "http://example.com/bar.pdf", | 661 "http://example.com/bar.pdf", |
| 677 "http://example.com/referrer.html", | 662 "http://example.com/referrer.html", |
| 678 &info); | 663 &info); |
| 679 | 664 |
| 680 // Instruct CreateDownload() to not callback to DownloadHistory immediately, | 665 // Instruct CreateDownload() to not callback to DownloadHistory immediately, |
| 681 // but to wait for FinishCreateDownload(). | 666 // but to wait for FinishCreateDownload(). |
| 682 set_slow_create_download(true); | 667 set_slow_create_download(true); |
| 683 | 668 |
| 684 // Pretend the manager just created |item|. | 669 // Pretend the manager just created |item|. |
| 685 CallOnDownloadCreated(0); | 670 CallOnDownloadCreated(0); |
| 686 ExpectDownloadCreated(info); | 671 ExpectDownloadCreated(info); |
| 687 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); | 672 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); |
| 688 | 673 |
| 689 // Call OnDownloadRemoved before calling back to DownloadHistory::ItemAdded(). | 674 // Call OnDownloadRemoved before calling back to DownloadHistory::ItemAdded(). |
| 690 // Instead of calling RemoveDownloads() immediately, DownloadHistory should | 675 // Instead of calling RemoveDownloads() immediately, DownloadHistory should |
| 691 // add the item's id to removed_while_adding_. Then, ItemAdded should | 676 // add the item's id to removed_while_adding_. Then, ItemAdded should |
| 692 // immediately remove the item's record from history. | 677 // immediately remove the item's record from history. |
| 693 item_observer()->OnDownloadRemoved(&item(0)); | 678 item(0).NotifyObserversDownloadRemoved(); |
| 694 EXPECT_CALL(manager(), GetDownload(item(0).GetId())) | 679 EXPECT_CALL(manager(), GetDownload(item(0).GetId())) |
| 695 .WillRepeatedly(Return(static_cast<content::DownloadItem*>(NULL))); | 680 .WillRepeatedly(Return(static_cast<content::DownloadItem*>(NULL))); |
| 696 ExpectNoDownloadsRemoved(); | 681 ExpectNoDownloadsRemoved(); |
| 697 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); | 682 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); |
| 698 | 683 |
| 699 // Now callback to DownloadHistory::ItemAdded(), and expect a call to | 684 // Now callback to DownloadHistory::ItemAdded(), and expect a call to |
| 700 // RemoveDownloads() for the item that was removed while it was being added. | 685 // RemoveDownloads() for the item that was removed while it was being added. |
| 701 FinishCreateDownload(); | 686 FinishCreateDownload(); |
| 702 IdSet ids; | 687 IdSet ids; |
| 703 ids.insert(info.id); | 688 ids.insert(info.id); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 726 ExpectNoDownloadCreated(); | 711 ExpectNoDownloadCreated(); |
| 727 } | 712 } |
| 728 | 713 |
| 729 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 714 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 730 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(1))); | 715 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(1))); |
| 731 | 716 |
| 732 // Pretend that the user removed both items. | 717 // Pretend that the user removed both items. |
| 733 IdSet ids; | 718 IdSet ids; |
| 734 ids.insert(info0.id); | 719 ids.insert(info0.id); |
| 735 ids.insert(info1.id); | 720 ids.insert(info1.id); |
| 736 item_observer()->OnDownloadRemoved(&item(0)); | 721 item(0).NotifyObserversDownloadRemoved(); |
| 737 item_observer()->OnDownloadRemoved(&item(1)); | 722 item(1).NotifyObserversDownloadRemoved(); |
| 738 ExpectDownloadsRemoved(ids); | 723 ExpectDownloadsRemoved(ids); |
| 739 } | 724 } |
| 740 | 725 |
| 741 // Test what happens when HistoryService/CreateDownload::CreateDownload() fails. | 726 // Test what happens when HistoryService/CreateDownload::CreateDownload() fails. |
| 742 TEST_F(DownloadHistoryTest, DownloadHistoryTest_CreateFailed) { | 727 TEST_F(DownloadHistoryTest, DownloadHistoryTest_CreateFailed) { |
| 743 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | 728 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
| 744 // OnDownloadRemoved. | 729 // OnDownloadRemoved. |
| 745 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 730 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); |
| 746 | 731 |
| 747 history::DownloadRow info; | 732 history::DownloadRow info; |
| 748 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 733 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
| 749 "http://example.com/bar.pdf", | 734 "http://example.com/bar.pdf", |
| 750 "http://example.com/referrer.html", | 735 "http://example.com/referrer.html", |
| 751 &info); | 736 &info); |
| 752 | 737 |
| 753 FailCreateDownload(); | 738 FailCreateDownload(); |
| 754 // Pretend the manager just created |item|. | 739 // Pretend the manager just created |item|. |
| 755 CallOnDownloadCreated(0); | 740 CallOnDownloadCreated(0); |
| 756 ExpectDownloadCreated(info); | 741 ExpectDownloadCreated(info); |
| 757 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); | 742 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); |
| 758 | 743 |
| 759 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); | 744 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); |
| 760 item_observer()->OnDownloadUpdated(&item(0)); | 745 item(0).NotifyObserversDownloadUpdated(); |
| 761 info.received_bytes = 100; | 746 info.received_bytes = 100; |
| 762 ExpectDownloadCreated(info); | 747 ExpectDownloadCreated(info); |
| 763 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 748 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 764 } | 749 } |
| 765 | 750 |
| 766 TEST_F(DownloadHistoryTest, DownloadHistoryTest_UpdateWhileAdding) { | 751 TEST_F(DownloadHistoryTest, DownloadHistoryTest_UpdateWhileAdding) { |
| 767 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | 752 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
| 768 // OnDownloadRemoved. | 753 // OnDownloadRemoved. |
| 769 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 754 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); |
| 770 | 755 |
| 771 history::DownloadRow info; | 756 history::DownloadRow info; |
| 772 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), | 757 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), |
| 773 "http://example.com/bar.pdf", | 758 "http://example.com/bar.pdf", |
| 774 "http://example.com/referrer.html", | 759 "http://example.com/referrer.html", |
| 775 &info); | 760 &info); |
| 776 | 761 |
| 777 // Instruct CreateDownload() to not callback to DownloadHistory immediately, | 762 // Instruct CreateDownload() to not callback to DownloadHistory immediately, |
| 778 // but to wait for FinishCreateDownload(). | 763 // but to wait for FinishCreateDownload(). |
| 779 set_slow_create_download(true); | 764 set_slow_create_download(true); |
| 780 | 765 |
| 781 // Pretend the manager just created |item|. | 766 // Pretend the manager just created |item|. |
| 782 CallOnDownloadCreated(0); | 767 CallOnDownloadCreated(0); |
| 783 ExpectDownloadCreated(info); | 768 ExpectDownloadCreated(info); |
| 784 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); | 769 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); |
| 785 | 770 |
| 786 // Pretend that something changed on the item. | 771 // Pretend that something changed on the item. |
| 787 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); | 772 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); |
| 788 item_observer()->OnDownloadUpdated(&item(0)); | 773 item(0).NotifyObserversDownloadUpdated(); |
| 789 | 774 |
| 790 FinishCreateDownload(); | 775 FinishCreateDownload(); |
| 791 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 776 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 792 | 777 |
| 793 // ItemAdded should call OnDownloadUpdated, which should detect that the item | 778 // ItemAdded should call OnDownloadUpdated, which should detect that the item |
| 794 // changed while it was being added and call UpdateDownload immediately. | 779 // changed while it was being added and call UpdateDownload immediately. |
| 795 info.opened = true; | 780 info.opened = true; |
| 796 ExpectDownloadUpdated(info); | 781 ExpectDownloadUpdated(info); |
| 797 } | 782 } |
| OLD | NEW |