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

Side by Side Diff: chrome/browser/download/download_history_unittest.cc

Issue 248713004: [Downloads] Add real observers to MockDownloadItem. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <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
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
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 DownloadedByExtension(&item(index), by_extension_id, by_extension_name); 427 new DownloadedByExtension(&item(index), by_extension_id, by_extension_name);
442 #endif 428 #endif
443 429
444 std::vector<content::DownloadItem*> items; 430 std::vector<content::DownloadItem*> items;
445 for (size_t i = 0; i < items_.size(); ++i) { 431 for (size_t i = 0; i < items_.size(); ++i) {
446 items.push_back(&item(i)); 432 items.push_back(&item(i));
447 } 433 }
448 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)) 434 EXPECT_CALL(*manager_.get(), GetAllDownloads(_))
449 .WillRepeatedly(SetArgPointee<0>(items)); 435 .WillRepeatedly(SetArgPointee<0>(items));
450 } 436 }
451 437
452 private: 438 private:
453 base::MessageLoopForUI loop_; 439 base::MessageLoopForUI loop_;
454 content::TestBrowserThread ui_thread_; 440 content::TestBrowserThread ui_thread_;
455 std::vector<StrictMockDownloadItem*> items_; 441 std::vector<StrictMockDownloadItem*> items_;
456 scoped_ptr<content::MockDownloadManager> manager_; 442 scoped_ptr<content::MockDownloadManager> manager_;
457 FakeHistoryAdapter* history_; 443 FakeHistoryAdapter* history_;
458 scoped_ptr<DownloadHistory> download_history_; 444 scoped_ptr<DownloadHistory> download_history_;
459 content::DownloadManager::Observer* manager_observer_; 445 content::DownloadManager::Observer* manager_observer_;
460 content::DownloadItem::Observer* item_observer_;
461 size_t download_created_index_; 446 size_t download_created_index_;
462 447
463 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryTest); 448 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryTest);
464 }; 449 };
465 450
466 } // anonymous namespace 451 } // anonymous namespace
467 452
468 // Test loading an item from the database, changing it, saving it back, removing 453 // Test loading an item from the database, changing it, saving it back, removing
469 // it. 454 // it.
470 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Load) { 455 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Load) {
471 // Load a download from history, create the item, OnDownloadCreated, 456 // Load a download from history, create the item, OnDownloadCreated,
472 // OnDownloadUpdated, OnDownloadRemoved. 457 // OnDownloadUpdated, OnDownloadRemoved.
473 history::DownloadRow info; 458 history::DownloadRow info;
474 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), 459 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"),
475 "http://example.com/bar.pdf", 460 "http://example.com/bar.pdf",
476 "http://example.com/referrer.html", 461 "http://example.com/referrer.html",
477 &info); 462 &info);
478 { 463 {
479 scoped_ptr<InfoVector> infos(new InfoVector()); 464 scoped_ptr<InfoVector> infos(new InfoVector());
480 infos->push_back(info); 465 infos->push_back(info);
481 ExpectWillQueryDownloads(infos.Pass()); 466 ExpectWillQueryDownloads(infos.Pass());
482 ExpectNoDownloadCreated(); 467 ExpectNoDownloadCreated();
483 } 468 }
484 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); 469 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0)));
485 470
486 // Pretend that something changed on the item. 471 // Pretend that something changed on the item.
487 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); 472 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true));
488 item_observer()->OnDownloadUpdated(&item(0)); 473 item(0).UpdateObservers();
489 info.opened = true; 474 info.opened = true;
490 ExpectDownloadUpdated(info); 475 ExpectDownloadUpdated(info);
491 476
492 // Pretend that the user removed the item. 477 // Pretend that the user removed the item.
493 IdSet ids; 478 IdSet ids;
494 ids.insert(info.id); 479 ids.insert(info.id);
495 item_observer()->OnDownloadRemoved(&item(0)); 480 item(0).UpdateObserversOnRemoved();
496 ExpectDownloadsRemoved(ids); 481 ExpectDownloadsRemoved(ids);
497 } 482 }
498 483
499 // Test creating an item, saving it to the database, changing it, saving it 484 // Test creating an item, saving it to the database, changing it, saving it
500 // back, removing it. 485 // back, removing it.
501 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Create) { 486 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Create) {
502 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, 487 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated,
503 // OnDownloadRemoved. 488 // OnDownloadRemoved.
504 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); 489 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector()));
505 490
506 history::DownloadRow info; 491 history::DownloadRow info;
507 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), 492 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"),
508 "http://example.com/bar.pdf", 493 "http://example.com/bar.pdf",
509 "http://example.com/referrer.html", 494 "http://example.com/referrer.html",
510 &info); 495 &info);
511 496
512 // Pretend the manager just created |item|. 497 // Pretend the manager just created |item|.
513 CallOnDownloadCreated(0); 498 CallOnDownloadCreated(0);
514 ExpectDownloadCreated(info); 499 ExpectDownloadCreated(info);
515 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); 500 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0)));
516 501
517 // Pretend that something changed on the item. 502 // Pretend that something changed on the item.
518 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); 503 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true));
519 item_observer()->OnDownloadUpdated(&item(0)); 504 item(0).UpdateObservers();
520 info.opened = true; 505 info.opened = true;
521 ExpectDownloadUpdated(info); 506 ExpectDownloadUpdated(info);
522 507
523 // Pretend that the user removed the item. 508 // Pretend that the user removed the item.
524 IdSet ids; 509 IdSet ids;
525 ids.insert(info.id); 510 ids.insert(info.id);
526 item_observer()->OnDownloadRemoved(&item(0)); 511 item(0).UpdateObserversOnRemoved();
527 ExpectDownloadsRemoved(ids); 512 ExpectDownloadsRemoved(ids);
528 } 513 }
529 514
530 // Test that changes to persisted fields in a DownloadItem triggers database 515 // Test that changes to persisted fields in a DownloadItem triggers database
531 // updates. 516 // updates.
532 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Update) { 517 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Update) {
533 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); 518 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector()));
534 519
535 history::DownloadRow info; 520 history::DownloadRow info;
536 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), 521 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"),
537 "http://example.com/bar.pdf", 522 "http://example.com/bar.pdf",
538 "http://example.com/referrer.html", 523 "http://example.com/referrer.html",
539 &info); 524 &info);
540 CallOnDownloadCreated(0); 525 CallOnDownloadCreated(0);
541 ExpectDownloadCreated(info); 526 ExpectDownloadCreated(info);
542 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); 527 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0)));
543 528
544 base::FilePath new_path(FILE_PATH_LITERAL("/foo/baz.txt")); 529 base::FilePath new_path(FILE_PATH_LITERAL("/foo/baz.txt"));
545 base::Time new_time(base::Time::Now()); 530 base::Time new_time(base::Time::Now());
546 std::string new_etag("new etag"); 531 std::string new_etag("new etag");
547 std::string new_last_modifed("new last modified"); 532 std::string new_last_modifed("new last modified");
548 533
549 // current_path 534 // current_path
550 EXPECT_CALL(item(0), GetFullPath()).WillRepeatedly(ReturnRefOfCopy(new_path)); 535 EXPECT_CALL(item(0), GetFullPath()).WillRepeatedly(ReturnRefOfCopy(new_path));
551 info.current_path = new_path; 536 info.current_path = new_path;
552 item_observer()->OnDownloadUpdated(&item(0)); 537 item(0).UpdateObservers();
553 ExpectDownloadUpdated(info); 538 ExpectDownloadUpdated(info);
554 539
555 // target_path 540 // target_path
556 EXPECT_CALL(item(0), GetTargetFilePath()) 541 EXPECT_CALL(item(0), GetTargetFilePath())
557 .WillRepeatedly(ReturnRefOfCopy(new_path)); 542 .WillRepeatedly(ReturnRefOfCopy(new_path));
558 info.target_path = new_path; 543 info.target_path = new_path;
559 item_observer()->OnDownloadUpdated(&item(0)); 544 item(0).UpdateObservers();
560 ExpectDownloadUpdated(info); 545 ExpectDownloadUpdated(info);
561 546
562 // end_time 547 // end_time
563 EXPECT_CALL(item(0), GetEndTime()).WillRepeatedly(Return(new_time)); 548 EXPECT_CALL(item(0), GetEndTime()).WillRepeatedly(Return(new_time));
564 info.end_time = new_time; 549 info.end_time = new_time;
565 item_observer()->OnDownloadUpdated(&item(0)); 550 item(0).UpdateObservers();
566 ExpectDownloadUpdated(info); 551 ExpectDownloadUpdated(info);
567 552
568 // received_bytes 553 // received_bytes
569 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(101)); 554 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(101));
570 info.received_bytes = 101; 555 info.received_bytes = 101;
571 item_observer()->OnDownloadUpdated(&item(0)); 556 item(0).UpdateObservers();
572 ExpectDownloadUpdated(info); 557 ExpectDownloadUpdated(info);
573 558
574 // total_bytes 559 // total_bytes
575 EXPECT_CALL(item(0), GetTotalBytes()).WillRepeatedly(Return(102)); 560 EXPECT_CALL(item(0), GetTotalBytes()).WillRepeatedly(Return(102));
576 info.total_bytes = 102; 561 info.total_bytes = 102;
577 item_observer()->OnDownloadUpdated(&item(0)); 562 item(0).UpdateObservers();
578 ExpectDownloadUpdated(info); 563 ExpectDownloadUpdated(info);
579 564
580 // etag 565 // etag
581 EXPECT_CALL(item(0), GetETag()).WillRepeatedly(ReturnRefOfCopy(new_etag)); 566 EXPECT_CALL(item(0), GetETag()).WillRepeatedly(ReturnRefOfCopy(new_etag));
582 info.etag = new_etag; 567 info.etag = new_etag;
583 item_observer()->OnDownloadUpdated(&item(0)); 568 item(0).UpdateObservers();
584 ExpectDownloadUpdated(info); 569 ExpectDownloadUpdated(info);
585 570
586 // last_modified 571 // last_modified
587 EXPECT_CALL(item(0), GetLastModifiedTime()) 572 EXPECT_CALL(item(0), GetLastModifiedTime())
588 .WillRepeatedly(ReturnRefOfCopy(new_last_modifed)); 573 .WillRepeatedly(ReturnRefOfCopy(new_last_modifed));
589 info.last_modified = new_last_modifed; 574 info.last_modified = new_last_modifed;
590 item_observer()->OnDownloadUpdated(&item(0)); 575 item(0).UpdateObservers();
591 ExpectDownloadUpdated(info); 576 ExpectDownloadUpdated(info);
592 577
593 // state 578 // state
594 EXPECT_CALL(item(0), GetState()) 579 EXPECT_CALL(item(0), GetState())
595 .WillRepeatedly(Return(content::DownloadItem::INTERRUPTED)); 580 .WillRepeatedly(Return(content::DownloadItem::INTERRUPTED));
596 info.state = content::DownloadItem::INTERRUPTED; 581 info.state = content::DownloadItem::INTERRUPTED;
597 item_observer()->OnDownloadUpdated(&item(0)); 582 item(0).UpdateObservers();
598 ExpectDownloadUpdated(info); 583 ExpectDownloadUpdated(info);
599 584
600 // danger_type 585 // danger_type
601 EXPECT_CALL(item(0), GetDangerType()) 586 EXPECT_CALL(item(0), GetDangerType())
602 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT)); 587 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT));
603 info.danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT; 588 info.danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT;
604 item_observer()->OnDownloadUpdated(&item(0)); 589 item(0).UpdateObservers();
605 ExpectDownloadUpdated(info); 590 ExpectDownloadUpdated(info);
606 591
607 // interrupt_reason 592 // interrupt_reason
608 EXPECT_CALL(item(0), GetLastReason()) 593 EXPECT_CALL(item(0), GetLastReason())
609 .WillRepeatedly(Return(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED)); 594 .WillRepeatedly(Return(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED));
610 info.interrupt_reason = content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; 595 info.interrupt_reason = content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED;
611 item_observer()->OnDownloadUpdated(&item(0)); 596 item(0).UpdateObservers();
612 ExpectDownloadUpdated(info); 597 ExpectDownloadUpdated(info);
613 598
614 // opened 599 // opened
615 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); 600 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true));
616 info.opened = true; 601 info.opened = true;
617 item_observer()->OnDownloadUpdated(&item(0)); 602 item(0).UpdateObservers();
618 ExpectDownloadUpdated(info); 603 ExpectDownloadUpdated(info);
619 } 604 }
620 605
621 // Test creating a new item, saving it, removing it by setting it Temporary, 606 // Test creating a new item, saving it, removing it by setting it Temporary,
622 // changing it without saving it back because it's Temporary, clearing 607 // changing it without saving it back because it's Temporary, clearing
623 // IsTemporary, saving it back, changing it, saving it back because it isn't 608 // IsTemporary, saving it back, changing it, saving it back because it isn't
624 // Temporary anymore. 609 // Temporary anymore.
625 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Temporary) { 610 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Temporary) {
626 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, 611 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated,
627 // OnDownloadRemoved. 612 // OnDownloadRemoved.
628 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); 613 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector()));
629 614
630 history::DownloadRow info; 615 history::DownloadRow info;
631 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), 616 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"),
632 "http://example.com/bar.pdf", 617 "http://example.com/bar.pdf",
633 "http://example.com/referrer.html", 618 "http://example.com/referrer.html",
634 &info); 619 &info);
635 620
636 // Pretend the manager just created |item|. 621 // Pretend the manager just created |item|.
637 CallOnDownloadCreated(0); 622 CallOnDownloadCreated(0);
638 ExpectDownloadCreated(info); 623 ExpectDownloadCreated(info);
639 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); 624 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0)));
640 625
641 // Pretend the item was marked temporary. DownloadHistory should remove it 626 // Pretend the item was marked temporary. DownloadHistory should remove it
642 // from history and start ignoring it. 627 // from history and start ignoring it.
643 EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(true)); 628 EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(true));
644 item_observer()->OnDownloadUpdated(&item(0)); 629 item(0).UpdateObservers();
645 IdSet ids; 630 IdSet ids;
646 ids.insert(info.id); 631 ids.insert(info.id);
647 ExpectDownloadsRemoved(ids); 632 ExpectDownloadsRemoved(ids);
648 633
649 // Change something that would make DownloadHistory call UpdateDownload if the 634 // Change something that would make DownloadHistory call UpdateDownload if the
650 // item weren't temporary. 635 // item weren't temporary.
651 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(4200)); 636 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(4200));
652 item_observer()->OnDownloadUpdated(&item(0)); 637 item(0).UpdateObservers();
653 ExpectNoDownloadUpdated(); 638 ExpectNoDownloadUpdated();
654 639
655 // Changing a temporary item back to a non-temporary item should make 640 // Changing a temporary item back to a non-temporary item should make
656 // DownloadHistory call CreateDownload. 641 // DownloadHistory call CreateDownload.
657 EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(false)); 642 EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(false));
658 item_observer()->OnDownloadUpdated(&item(0)); 643 item(0).UpdateObservers();
659 info.received_bytes = 4200; 644 info.received_bytes = 4200;
660 ExpectDownloadCreated(info); 645 ExpectDownloadCreated(info);
661 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); 646 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0)));
662 647
663 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); 648 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100));
664 item_observer()->OnDownloadUpdated(&item(0)); 649 item(0).UpdateObservers();
665 info.received_bytes = 100; 650 info.received_bytes = 100;
666 ExpectDownloadUpdated(info); 651 ExpectDownloadUpdated(info);
667 } 652 }
668 653
669 // Test removing downloads while they're still being added. 654 // Test removing downloads while they're still being added.
670 TEST_F(DownloadHistoryTest, DownloadHistoryTest_RemoveWhileAdding) { 655 TEST_F(DownloadHistoryTest, DownloadHistoryTest_RemoveWhileAdding) {
671 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); 656 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector()));
672 657
673 history::DownloadRow info; 658 history::DownloadRow info;
674 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), 659 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"),
675 "http://example.com/bar.pdf", 660 "http://example.com/bar.pdf",
676 "http://example.com/referrer.html", 661 "http://example.com/referrer.html",
677 &info); 662 &info);
678 663
679 // Instruct CreateDownload() to not callback to DownloadHistory immediately, 664 // Instruct CreateDownload() to not callback to DownloadHistory immediately,
680 // but to wait for FinishCreateDownload(). 665 // but to wait for FinishCreateDownload().
681 set_slow_create_download(true); 666 set_slow_create_download(true);
682 667
683 // Pretend the manager just created |item|. 668 // Pretend the manager just created |item|.
684 CallOnDownloadCreated(0); 669 CallOnDownloadCreated(0);
685 ExpectDownloadCreated(info); 670 ExpectDownloadCreated(info);
686 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); 671 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0)));
687 672
688 // Call OnDownloadRemoved before calling back to DownloadHistory::ItemAdded(). 673 // Call OnDownloadRemoved before calling back to DownloadHistory::ItemAdded().
689 // Instead of calling RemoveDownloads() immediately, DownloadHistory should 674 // Instead of calling RemoveDownloads() immediately, DownloadHistory should
690 // add the item's id to removed_while_adding_. Then, ItemAdded should 675 // add the item's id to removed_while_adding_. Then, ItemAdded should
691 // immediately remove the item's record from history. 676 // immediately remove the item's record from history.
692 item_observer()->OnDownloadRemoved(&item(0)); 677 item(0).UpdateObserversOnRemoved();
693 EXPECT_CALL(manager(), GetDownload(item(0).GetId())) 678 EXPECT_CALL(manager(), GetDownload(item(0).GetId()))
694 .WillRepeatedly(Return(static_cast<content::DownloadItem*>(NULL))); 679 .WillRepeatedly(Return(static_cast<content::DownloadItem*>(NULL)));
695 ExpectNoDownloadsRemoved(); 680 ExpectNoDownloadsRemoved();
696 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); 681 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0)));
697 682
698 // Now callback to DownloadHistory::ItemAdded(), and expect a call to 683 // Now callback to DownloadHistory::ItemAdded(), and expect a call to
699 // RemoveDownloads() for the item that was removed while it was being added. 684 // RemoveDownloads() for the item that was removed while it was being added.
700 FinishCreateDownload(); 685 FinishCreateDownload();
701 IdSet ids; 686 IdSet ids;
702 ids.insert(info.id); 687 ids.insert(info.id);
(...skipping 22 matching lines...) Expand all
725 ExpectNoDownloadCreated(); 710 ExpectNoDownloadCreated();
726 } 711 }
727 712
728 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); 713 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0)));
729 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(1))); 714 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(1)));
730 715
731 // Pretend that the user removed both items. 716 // Pretend that the user removed both items.
732 IdSet ids; 717 IdSet ids;
733 ids.insert(info0.id); 718 ids.insert(info0.id);
734 ids.insert(info1.id); 719 ids.insert(info1.id);
735 item_observer()->OnDownloadRemoved(&item(0)); 720 item(0).UpdateObserversOnRemoved();
736 item_observer()->OnDownloadRemoved(&item(1)); 721 item(1).UpdateObserversOnRemoved();
737 ExpectDownloadsRemoved(ids); 722 ExpectDownloadsRemoved(ids);
738 } 723 }
739 724
740 // Test what happens when HistoryService/CreateDownload::CreateDownload() fails. 725 // Test what happens when HistoryService/CreateDownload::CreateDownload() fails.
741 TEST_F(DownloadHistoryTest, DownloadHistoryTest_CreateFailed) { 726 TEST_F(DownloadHistoryTest, DownloadHistoryTest_CreateFailed) {
742 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, 727 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated,
743 // OnDownloadRemoved. 728 // OnDownloadRemoved.
744 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); 729 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector()));
745 730
746 history::DownloadRow info; 731 history::DownloadRow info;
747 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), 732 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"),
748 "http://example.com/bar.pdf", 733 "http://example.com/bar.pdf",
749 "http://example.com/referrer.html", 734 "http://example.com/referrer.html",
750 &info); 735 &info);
751 736
752 FailCreateDownload(); 737 FailCreateDownload();
753 // Pretend the manager just created |item|. 738 // Pretend the manager just created |item|.
754 CallOnDownloadCreated(0); 739 CallOnDownloadCreated(0);
755 ExpectDownloadCreated(info); 740 ExpectDownloadCreated(info);
756 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); 741 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0)));
757 742
758 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); 743 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100));
759 item_observer()->OnDownloadUpdated(&item(0)); 744 item(0).UpdateObservers();
760 info.received_bytes = 100; 745 info.received_bytes = 100;
761 ExpectDownloadCreated(info); 746 ExpectDownloadCreated(info);
762 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); 747 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0)));
763 } 748 }
764 749
765 TEST_F(DownloadHistoryTest, DownloadHistoryTest_UpdateWhileAdding) { 750 TEST_F(DownloadHistoryTest, DownloadHistoryTest_UpdateWhileAdding) {
766 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, 751 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated,
767 // OnDownloadRemoved. 752 // OnDownloadRemoved.
768 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); 753 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector()));
769 754
770 history::DownloadRow info; 755 history::DownloadRow info;
771 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"), 756 InitBasicItem(FILE_PATH_LITERAL("/foo/bar.pdf"),
772 "http://example.com/bar.pdf", 757 "http://example.com/bar.pdf",
773 "http://example.com/referrer.html", 758 "http://example.com/referrer.html",
774 &info); 759 &info);
775 760
776 // Instruct CreateDownload() to not callback to DownloadHistory immediately, 761 // Instruct CreateDownload() to not callback to DownloadHistory immediately,
777 // but to wait for FinishCreateDownload(). 762 // but to wait for FinishCreateDownload().
778 set_slow_create_download(true); 763 set_slow_create_download(true);
779 764
780 // Pretend the manager just created |item|. 765 // Pretend the manager just created |item|.
781 CallOnDownloadCreated(0); 766 CallOnDownloadCreated(0);
782 ExpectDownloadCreated(info); 767 ExpectDownloadCreated(info);
783 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); 768 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0)));
784 769
785 // Pretend that something changed on the item. 770 // Pretend that something changed on the item.
786 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); 771 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true));
787 item_observer()->OnDownloadUpdated(&item(0)); 772 item(0).UpdateObservers();
788 773
789 FinishCreateDownload(); 774 FinishCreateDownload();
790 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); 775 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0)));
791 776
792 // ItemAdded should call OnDownloadUpdated, which should detect that the item 777 // ItemAdded should call OnDownloadUpdated, which should detect that the item
793 // changed while it was being added and call UpdateDownload immediately. 778 // changed while it was being added and call UpdateDownload immediately.
794 info.opened = true; 779 info.opened = true;
795 ExpectDownloadUpdated(info); 780 ExpectDownloadUpdated(info);
796 } 781 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698