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

Side by Side Diff: components/offline_pages/offline_page_model_unittest.cc

Issue 1345043002: Update access info when an offline page is being visited (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Some more fixes Created 5 years, 3 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/offline_pages/offline_page_model.h" 5 #include "components/offline_pages/offline_page_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
(...skipping 30 matching lines...) Expand all
41 REMOVE_FAILED, 41 REMOVE_FAILED,
42 }; 42 };
43 43
44 explicit OfflinePageTestStore( 44 explicit OfflinePageTestStore(
45 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); 45 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
46 explicit OfflinePageTestStore(const OfflinePageTestStore& other_store); 46 explicit OfflinePageTestStore(const OfflinePageTestStore& other_store);
47 ~OfflinePageTestStore() override; 47 ~OfflinePageTestStore() override;
48 48
49 // OfflinePageMetadataStore overrides: 49 // OfflinePageMetadataStore overrides:
50 void Load(const LoadCallback& callback) override; 50 void Load(const LoadCallback& callback) override;
51 void AddOfflinePage(const OfflinePageItem& offline_page, 51 void AddOrUpdateOfflinePage(const OfflinePageItem& offline_page,
52 const UpdateCallback& callback) override; 52 const UpdateCallback& callback) override;
53 void RemoveOfflinePages(const std::vector<int64>& bookmark_ids, 53 void RemoveOfflinePages(const std::vector<int64>& bookmark_ids,
54 const UpdateCallback& callback) override; 54 const UpdateCallback& callback) override;
55 const OfflinePageItem& last_saved_page() const { return last_saved_page_; } 55 const OfflinePageItem& last_saved_page() const { return last_saved_page_; }
56 56
57 void set_test_scenario(TestScenario scenario) { scenario_ = scenario; }; 57 void set_test_scenario(TestScenario scenario) { scenario_ = scenario; };
58 58
59 const std::vector<OfflinePageItem>& offline_pages() const { 59 const std::vector<OfflinePageItem>& offline_pages() const {
60 return offline_pages_; 60 return offline_pages_;
61 } 61 }
62 62
(...skipping 25 matching lines...) Expand all
88 void OfflinePageTestStore::Load(const LoadCallback& callback) { 88 void OfflinePageTestStore::Load(const LoadCallback& callback) {
89 if (scenario_ != TestScenario::LOAD_FAILED) { 89 if (scenario_ != TestScenario::LOAD_FAILED) {
90 task_runner_->PostTask( 90 task_runner_->PostTask(
91 FROM_HERE, base::Bind(callback, true, offline_pages_)); 91 FROM_HERE, base::Bind(callback, true, offline_pages_));
92 } else { 92 } else {
93 task_runner_->PostTask( 93 task_runner_->PostTask(
94 FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>())); 94 FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>()));
95 } 95 }
96 } 96 }
97 97
98 void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page, 98 void OfflinePageTestStore::AddOrUpdateOfflinePage(
99 const UpdateCallback& callback) { 99 const OfflinePageItem& offline_page, const UpdateCallback& callback) {
100 last_saved_page_ = offline_page; 100 last_saved_page_ = offline_page;
101 bool result = scenario_ != TestScenario::WRITE_FAILED; 101 bool result = scenario_ != TestScenario::WRITE_FAILED;
102 if (result) { 102 if (result) {
103 offline_pages_.push_back(offline_page); 103 offline_pages_.push_back(offline_page);
104 } 104 }
105 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); 105 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result));
106 } 106 }
107 107
108 void OfflinePageTestStore::RemoveOfflinePages( 108 void OfflinePageTestStore::RemoveOfflinePages(
109 const std::vector<int64>& bookmark_ids, 109 const std::vector<int64>& bookmark_ids,
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 363
364 ResetResults(); 364 ResetResults();
365 365
366 const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages(); 366 const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages();
367 367
368 EXPECT_EQ(1UL, offline_pages.size()); 368 EXPECT_EQ(1UL, offline_pages.size());
369 EXPECT_EQ(kTestUrl, offline_pages[0].url); 369 EXPECT_EQ(kTestUrl, offline_pages[0].url);
370 EXPECT_EQ(kTestPageBookmarkId1, offline_pages[0].bookmark_id); 370 EXPECT_EQ(kTestPageBookmarkId1, offline_pages[0].bookmark_id);
371 EXPECT_EQ(archiver_path, offline_pages[0].file_path); 371 EXPECT_EQ(archiver_path, offline_pages[0].file_path);
372 EXPECT_EQ(kTestFileSize, offline_pages[0].file_size); 372 EXPECT_EQ(kTestFileSize, offline_pages[0].file_size);
373 EXPECT_EQ(0, offline_pages[0].access_count);
373 } 374 }
374 375
375 TEST_F(OfflinePageModelTest, SavePageOfflineArchiverCancelled) { 376 TEST_F(OfflinePageModelTest, SavePageOfflineArchiverCancelled) {
376 scoped_ptr<OfflinePageTestArchiver> archiver( 377 scoped_ptr<OfflinePageTestArchiver> archiver(
377 BuildArchiver(kTestUrl, 378 BuildArchiver(kTestUrl,
378 OfflinePageArchiver::ArchiverResult::ERROR_CANCELED) 379 OfflinePageArchiver::ArchiverResult::ERROR_CANCELED)
379 .Pass()); 380 .Pass());
380 model()->SavePage( 381 model()->SavePage(
381 kTestUrl, kTestPageBookmarkId1, archiver.Pass(), 382 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
382 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr())); 383 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 497
497 ResetResults(); 498 ResetResults();
498 499
499 const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages(); 500 const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages();
500 501
501 EXPECT_EQ(2UL, offline_pages.size()); 502 EXPECT_EQ(2UL, offline_pages.size());
502 EXPECT_EQ(kTestUrl, offline_pages[0].url); 503 EXPECT_EQ(kTestUrl, offline_pages[0].url);
503 EXPECT_EQ(kTestPageBookmarkId1, offline_pages[0].bookmark_id); 504 EXPECT_EQ(kTestPageBookmarkId1, offline_pages[0].bookmark_id);
504 EXPECT_EQ(archiver_path, offline_pages[0].file_path); 505 EXPECT_EQ(archiver_path, offline_pages[0].file_path);
505 EXPECT_EQ(kTestFileSize, offline_pages[0].file_size); 506 EXPECT_EQ(kTestFileSize, offline_pages[0].file_size);
507 EXPECT_EQ(0, offline_pages[0].access_count);
506 EXPECT_EQ(kTestUrl2, offline_pages[1].url); 508 EXPECT_EQ(kTestUrl2, offline_pages[1].url);
507 EXPECT_EQ(kTestPageBookmarkId2, offline_pages[1].bookmark_id); 509 EXPECT_EQ(kTestPageBookmarkId2, offline_pages[1].bookmark_id);
508 EXPECT_EQ(archiver_path2, offline_pages[1].file_path); 510 EXPECT_EQ(archiver_path2, offline_pages[1].file_path);
509 EXPECT_EQ(kTestFileSize, offline_pages[1].file_size); 511 EXPECT_EQ(kTestFileSize, offline_pages[1].file_size);
512 EXPECT_EQ(0, offline_pages[1].access_count);
513 }
514
515 TEST_F(OfflinePageModelTest, MarkPageAccessed) {
516 scoped_ptr<OfflinePageTestArchiver> archiver(
517 BuildArchiver(kTestUrl,
518 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
519 .Pass());
520 model()->SavePage(
521 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
522 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
523 PumpLoop();
524
525 // This will increase access_count by one.
526 model()->MarkPageAccessed(kTestPageBookmarkId1);
527 base::RunLoop().RunUntilIdle();
528
529 const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages();
530
531 EXPECT_EQ(1UL, offline_pages.size());
532 EXPECT_EQ(kTestUrl, offline_pages[0].url);
533 EXPECT_EQ(kTestPageBookmarkId1, offline_pages[0].bookmark_id);
534 EXPECT_EQ(kTestFileSize, offline_pages[0].file_size);
535 EXPECT_EQ(1, offline_pages[0].access_count);
510 } 536 }
511 537
512 TEST_F(OfflinePageModelTest, GetAllPagesStoreEmpty) { 538 TEST_F(OfflinePageModelTest, GetAllPagesStoreEmpty) {
513 const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages(); 539 const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages();
514 540
515 EXPECT_EQ(0UL, offline_pages.size()); 541 EXPECT_EQ(0UL, offline_pages.size());
516 } 542 }
517 543
518 TEST_F(OfflinePageModelTest, GetAllPagesStoreFailure) { 544 TEST_F(OfflinePageModelTest, GetAllPagesStoreFailure) {
519 GetStore()->set_test_scenario( 545 GetStore()->set_test_scenario(
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
685 } 711 }
686 712
687 // Test that model returns pages that are older than 30 days as candidates for 713 // Test that model returns pages that are older than 30 days as candidates for
688 // clean up, hence the numbers in time delta. 714 // clean up, hence the numbers in time delta.
689 TEST_F(OfflinePageModelTest, GetPagesToCleanUp) { 715 TEST_F(OfflinePageModelTest, GetPagesToCleanUp) {
690 base::Time now = base::Time::Now(); 716 base::Time now = base::Time::Now();
691 OfflinePageItem page_1( 717 OfflinePageItem page_1(
692 GURL(kTestUrl), kTestPageBookmarkId1, 718 GURL(kTestUrl), kTestPageBookmarkId1,
693 base::FilePath(FILE_PATH_LITERAL("/test/location/page1.mhtml")), 719 base::FilePath(FILE_PATH_LITERAL("/test/location/page1.mhtml")),
694 kTestFileSize, now - base::TimeDelta::FromDays(40)); 720 kTestFileSize, now - base::TimeDelta::FromDays(40));
695 GetStore()->AddOfflinePage( 721 GetStore()->AddOrUpdateOfflinePage(
696 page_1, 722 page_1,
697 base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr())); 723 base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr()));
698 PumpLoop(); 724 PumpLoop();
699 725
700 OfflinePageItem page_2( 726 OfflinePageItem page_2(
701 GURL(kTestUrl2), kTestPageBookmarkId2, 727 GURL(kTestUrl2), kTestPageBookmarkId2,
702 base::FilePath(FILE_PATH_LITERAL("/test/location/page2.mhtml")), 728 base::FilePath(FILE_PATH_LITERAL("/test/location/page2.mhtml")),
703 kTestFileSize, now - base::TimeDelta::FromDays(31)); 729 kTestFileSize, now - base::TimeDelta::FromDays(31));
704 GetStore()->AddOfflinePage( 730 GetStore()->AddOrUpdateOfflinePage(
705 page_2, 731 page_2,
706 base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr())); 732 base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr()));
707 PumpLoop(); 733 PumpLoop();
708 734
709 OfflinePageItem page_3( 735 OfflinePageItem page_3(
710 GURL("http://test.xyz"), 42, 736 GURL("http://test.xyz"), 42,
711 base::FilePath(FILE_PATH_LITERAL("/test/location/page3.mhtml")), 737 base::FilePath(FILE_PATH_LITERAL("/test/location/page3.mhtml")),
712 kTestFileSize, now - base::TimeDelta::FromDays(29)); 738 kTestFileSize, now - base::TimeDelta::FromDays(29));
713 GetStore()->AddOfflinePage( 739 GetStore()->AddOrUpdateOfflinePage(
714 page_3, 740 page_3,
715 base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr())); 741 base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr()));
716 PumpLoop(); 742 PumpLoop();
717 743
718 ResetModel(); 744 ResetModel();
719 745
720 // Only page_1 and page_2 are expected to be picked up by the model as page_3 746 // Only page_1 and page_2 are expected to be picked up by the model as page_3
721 // has not been in the store long enough. 747 // has not been in the store long enough.
722 std::vector<OfflinePageItem> pages_to_clean_up = model()->GetPagesToCleanUp(); 748 std::vector<OfflinePageItem> pages_to_clean_up = model()->GetPagesToCleanUp();
723 EXPECT_EQ(2UL, pages_to_clean_up.size()); 749 EXPECT_EQ(2UL, pages_to_clean_up.size());
724 EXPECT_EQ(kTestUrl, pages_to_clean_up[0].url); 750 EXPECT_EQ(kTestUrl, pages_to_clean_up[0].url);
725 EXPECT_EQ(kTestPageBookmarkId1, pages_to_clean_up[0].bookmark_id); 751 EXPECT_EQ(kTestPageBookmarkId1, pages_to_clean_up[0].bookmark_id);
726 EXPECT_EQ(kTestUrl2, pages_to_clean_up[1].url); 752 EXPECT_EQ(kTestUrl2, pages_to_clean_up[1].url);
727 EXPECT_EQ(kTestPageBookmarkId2, pages_to_clean_up[1].bookmark_id); 753 EXPECT_EQ(kTestPageBookmarkId2, pages_to_clean_up[1].bookmark_id);
728 } 754 }
729 755
730 } // namespace offline_pages 756 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_model.cc ('k') | components/offline_pages/proto/offline_pages.proto » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698