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

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

Issue 1307753002: [Offline pages] Adding capability to free up space used by Offline pages (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Converting jlongarray to std::vector<int64> 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
« no previous file with comments | « components/offline_pages/offline_page_model.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
11 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/single_thread_task_runner.h" 14 #include "base/single_thread_task_runner.h"
15 #include "base/strings/string16.h" 15 #include "base/strings/string16.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "base/time/time.h"
17 #include "components/offline_pages/offline_page_item.h" 18 #include "components/offline_pages/offline_page_item.h"
18 #include "components/offline_pages/offline_page_metadata_store.h" 19 #include "components/offline_pages/offline_page_metadata_store.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h" 21 #include "url/gurl.h"
21 22
22 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; 23 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult;
23 using DeletePageResult = offline_pages::OfflinePageModel::DeletePageResult; 24 using DeletePageResult = offline_pages::OfflinePageModel::DeletePageResult;
24 25
25 namespace offline_pages { 26 namespace offline_pages {
26 27
27 namespace { 28 namespace {
28 const GURL kTestUrl("http://example.com"); 29 const GURL kTestUrl("http://example.com");
29 const int64 kTestPageBookmarkId1 = 1234LL; 30 const int64 kTestPageBookmarkId1 = 1234LL;
30 const GURL kTestUrl2("http://other.page.com"); 31 const GURL kTestUrl2("http://other.page.com");
31 const int64 kTestPageBookmarkId2 = 5678LL; 32 const int64 kTestPageBookmarkId2 = 5678LL;
32 const int64 kTestFileSize = 876543LL; 33 const int64 kTestFileSize = 876543LL;
33 34
34 class OfflinePageTestStore : public OfflinePageMetadataStore { 35 class OfflinePageTestStore : public OfflinePageMetadataStore {
35 public: 36 public:
36 enum class TestScenario { 37 enum class TestScenario {
37 SUCCESSFUL, 38 SUCCESSFUL,
38 WRITE_FAILED, 39 WRITE_FAILED,
39 LOAD_FAILED, 40 LOAD_FAILED,
40 REMOVE_FAILED, 41 REMOVE_FAILED,
41 }; 42 };
42 43
43 explicit OfflinePageTestStore( 44 explicit OfflinePageTestStore(
44 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); 45 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
46 explicit OfflinePageTestStore(const OfflinePageTestStore& other_store);
45 ~OfflinePageTestStore() override; 47 ~OfflinePageTestStore() override;
46 48
47 // OfflinePageMetadataStore overrides: 49 // OfflinePageMetadataStore overrides:
48 void Load(const LoadCallback& callback) override; 50 void Load(const LoadCallback& callback) override;
49 void AddOfflinePage(const OfflinePageItem& offline_page, 51 void AddOfflinePage(const OfflinePageItem& offline_page,
50 const UpdateCallback& callback) override; 52 const UpdateCallback& callback) override;
51 void RemoveOfflinePages(const std::vector<int64>& bookmark_ids, 53 void RemoveOfflinePages(const std::vector<int64>& bookmark_ids,
52 const UpdateCallback& callback) override; 54 const UpdateCallback& callback) override;
53 const OfflinePageItem& last_saved_page() const { return last_saved_page_; } 55 const OfflinePageItem& last_saved_page() const { return last_saved_page_; }
54 56
55 void set_test_scenario(TestScenario scenario) { scenario_ = scenario; }; 57 void set_test_scenario(TestScenario scenario) { scenario_ = scenario; };
56 58
57 const std::vector<OfflinePageItem>& offline_pages() const { 59 const std::vector<OfflinePageItem>& offline_pages() const {
58 return offline_pages_; 60 return offline_pages_;
59 } 61 }
60 62
61 private: 63 private:
62 OfflinePageItem last_saved_page_; 64 OfflinePageItem last_saved_page_;
63 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 65 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
64 TestScenario scenario_; 66 TestScenario scenario_;
65 67
66 std::vector<OfflinePageItem> offline_pages_; 68 std::vector<OfflinePageItem> offline_pages_;
67 69
68 DISALLOW_COPY_AND_ASSIGN(OfflinePageTestStore); 70 DISALLOW_ASSIGN(OfflinePageTestStore);
69 }; 71 };
70 72
71 OfflinePageTestStore::OfflinePageTestStore( 73 OfflinePageTestStore::OfflinePageTestStore(
72 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) 74 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
73 : task_runner_(task_runner), 75 : task_runner_(task_runner),
74 scenario_(TestScenario::SUCCESSFUL) { 76 scenario_(TestScenario::SUCCESSFUL) {
75 } 77 }
76 78
79 OfflinePageTestStore::OfflinePageTestStore(
80 const OfflinePageTestStore& other_store)
81 : task_runner_(other_store.task_runner_),
82 scenario_(other_store.scenario_),
83 offline_pages_(other_store.offline_pages_) {}
84
77 OfflinePageTestStore::~OfflinePageTestStore() { 85 OfflinePageTestStore::~OfflinePageTestStore() {
78 } 86 }
79 87
80 void OfflinePageTestStore::Load(const LoadCallback& callback) { 88 void OfflinePageTestStore::Load(const LoadCallback& callback) {
81 if (scenario_ != TestScenario::LOAD_FAILED) { 89 if (scenario_ != TestScenario::LOAD_FAILED) {
82 task_runner_->PostTask( 90 task_runner_->PostTask(
83 FROM_HERE, base::Bind(callback, true, offline_pages_)); 91 FROM_HERE, base::Bind(callback, true, offline_pages_));
84 } else { 92 } else {
85 task_runner_->PostTask( 93 task_runner_->PostTask(
86 FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>())); 94 FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>()));
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 void SetUp() override; 170 void SetUp() override;
163 void TearDown() override; 171 void TearDown() override;
164 172
165 // OfflinePageModel::Observer implementation. 173 // OfflinePageModel::Observer implementation.
166 void OfflinePageModelLoaded(OfflinePageModel* model) override; 174 void OfflinePageModelLoaded(OfflinePageModel* model) override;
167 175
168 // OfflinePageModel callbacks. 176 // OfflinePageModel callbacks.
169 void OnSavePageDone(SavePageResult result); 177 void OnSavePageDone(SavePageResult result);
170 void OnDeletePageDone(DeletePageResult result); 178 void OnDeletePageDone(DeletePageResult result);
171 179
180 // OfflinePageMetadataStore callbacks.
181 void OnStoreUpdateDone(bool /* success */);
182
172 scoped_ptr<OfflinePageTestArchiver> BuildArchiver( 183 scoped_ptr<OfflinePageTestArchiver> BuildArchiver(
173 const GURL& url, 184 const GURL& url,
174 OfflinePageArchiver::ArchiverResult result); 185 OfflinePageArchiver::ArchiverResult result);
175 scoped_ptr<OfflinePageMetadataStore> BuildStore(); 186 scoped_ptr<OfflinePageMetadataStore> BuildStore();
176 scoped_ptr<OfflinePageModel> BuildModel(); 187 scoped_ptr<OfflinePageModel> BuildModel(
188 scoped_ptr<OfflinePageMetadataStore> store);
189 void ResetModel();
177 190
178 // Utility methods. 191 // Utility methods.
179 void PumpLoop(); 192 void PumpLoop();
180 void ResetResults(); 193 void ResetResults();
181 194
182 scoped_refptr<base::SingleThreadTaskRunner> task_runner() { 195 scoped_refptr<base::SingleThreadTaskRunner> task_runner() {
183 return message_loop_.task_runner(); 196 return message_loop_.task_runner();
184 } 197 }
185 198
186 OfflinePageModel* model() { return model_.get(); } 199 OfflinePageModel* model() { return model_.get(); }
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 OfflinePageModelTest::OfflinePageModelTest() 263 OfflinePageModelTest::OfflinePageModelTest()
251 : last_save_result_(SavePageResult::CANCELLED), 264 : last_save_result_(SavePageResult::CANCELLED),
252 last_delete_result_(DeletePageResult::CANCELLED) { 265 last_delete_result_(DeletePageResult::CANCELLED) {
253 } 266 }
254 267
255 OfflinePageModelTest::~OfflinePageModelTest() { 268 OfflinePageModelTest::~OfflinePageModelTest() {
256 } 269 }
257 270
258 void OfflinePageModelTest::SetUp() { 271 void OfflinePageModelTest::SetUp() {
259 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 272 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
260 model_ = BuildModel().Pass(); 273 model_ = BuildModel(BuildStore().Pass()).Pass();
261 model_->AddObserver(this); 274 model_->AddObserver(this);
262 PumpLoop(); 275 PumpLoop();
263 } 276 }
264 277
265 void OfflinePageModelTest::TearDown() { 278 void OfflinePageModelTest::TearDown() {
266 model_->RemoveObserver(this); 279 model_->RemoveObserver(this);
267 } 280 }
268 281
269 void OfflinePageModelTest::OfflinePageModelLoaded(OfflinePageModel* model) { 282 void OfflinePageModelTest::OfflinePageModelLoaded(OfflinePageModel* model) {
270 ASSERT_EQ(model_.get(), model); 283 ASSERT_EQ(model_.get(), model);
271 run_loop_->Quit(); 284 run_loop_->Quit();
272 } 285 }
273 286
274 void OfflinePageModelTest::OnSavePageDone( 287 void OfflinePageModelTest::OnSavePageDone(
275 OfflinePageModel::SavePageResult result) { 288 OfflinePageModel::SavePageResult result) {
276 run_loop_->Quit(); 289 run_loop_->Quit();
277 last_save_result_ = result; 290 last_save_result_ = result;
278 } 291 }
279 292
280 void OfflinePageModelTest::OnDeletePageDone(DeletePageResult result) { 293 void OfflinePageModelTest::OnDeletePageDone(DeletePageResult result) {
281 run_loop_->Quit(); 294 run_loop_->Quit();
282 last_delete_result_ = result; 295 last_delete_result_ = result;
283 } 296 }
284 297
298 void OfflinePageModelTest::OnStoreUpdateDone(bool /* success - ignored */) {
299 run_loop_->Quit();
300 }
301
285 scoped_ptr<OfflinePageTestArchiver> OfflinePageModelTest::BuildArchiver( 302 scoped_ptr<OfflinePageTestArchiver> OfflinePageModelTest::BuildArchiver(
286 const GURL& url, 303 const GURL& url,
287 OfflinePageArchiver::ArchiverResult result) { 304 OfflinePageArchiver::ArchiverResult result) {
288 return scoped_ptr<OfflinePageTestArchiver>(new OfflinePageTestArchiver( 305 return scoped_ptr<OfflinePageTestArchiver>(new OfflinePageTestArchiver(
289 this, url, temp_dir_.path(), result, task_runner())); 306 this, url, temp_dir_.path(), result, task_runner()));
290 } 307 }
291 308
292 scoped_ptr<OfflinePageMetadataStore> OfflinePageModelTest::BuildStore() { 309 scoped_ptr<OfflinePageMetadataStore> OfflinePageModelTest::BuildStore() {
293 return scoped_ptr<OfflinePageMetadataStore>( 310 return scoped_ptr<OfflinePageMetadataStore>(
294 new OfflinePageTestStore(task_runner())); 311 new OfflinePageTestStore(task_runner()));
295 } 312 }
296 313
297 scoped_ptr<OfflinePageModel> OfflinePageModelTest::BuildModel() { 314 scoped_ptr<OfflinePageModel> OfflinePageModelTest::BuildModel(
315 scoped_ptr<OfflinePageMetadataStore> store) {
298 return scoped_ptr<OfflinePageModel>( 316 return scoped_ptr<OfflinePageModel>(
299 new OfflinePageModel(BuildStore().Pass(), task_runner())); 317 new OfflinePageModel(store.Pass(), task_runner()));
318 }
319
320 void OfflinePageModelTest::ResetModel() {
321 model_->RemoveObserver(this);
322 OfflinePageTestStore* old_store = GetStore();
323 scoped_ptr<OfflinePageMetadataStore> new_store(
324 new OfflinePageTestStore(*old_store));
325 model_ = BuildModel(new_store.Pass()).Pass();
326 model_->AddObserver(this);
327 PumpLoop();
300 } 328 }
301 329
302 void OfflinePageModelTest::PumpLoop() { 330 void OfflinePageModelTest::PumpLoop() {
303 run_loop_.reset(new base::RunLoop()); 331 run_loop_.reset(new base::RunLoop());
304 run_loop_->Run(); 332 run_loop_->Run();
305 } 333 }
306 334
307 void OfflinePageModelTest::ResetResults() { 335 void OfflinePageModelTest::ResetResults() {
308 last_save_result_ = SavePageResult::CANCELLED; 336 last_save_result_ = SavePageResult::CANCELLED;
309 last_delete_result_ = DeletePageResult::CANCELLED; 337 last_delete_result_ = DeletePageResult::CANCELLED;
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 674
647 page = model()->GetPageByOfflineURL(offline_url); 675 page = model()->GetPageByOfflineURL(offline_url);
648 EXPECT_TRUE(page); 676 EXPECT_TRUE(page);
649 EXPECT_EQ(kTestUrl, page->url); 677 EXPECT_EQ(kTestUrl, page->url);
650 EXPECT_EQ(kTestPageBookmarkId1, page->bookmark_id); 678 EXPECT_EQ(kTestPageBookmarkId1, page->bookmark_id);
651 679
652 page = model()->GetPageByOfflineURL(GURL("http://foo")); 680 page = model()->GetPageByOfflineURL(GURL("http://foo"));
653 EXPECT_FALSE(page); 681 EXPECT_FALSE(page);
654 } 682 }
655 683
684 // Test that model returns pages that are older than 30 days as candidates for
685 // clean up, hence the numbers in time delta.
686 TEST_F(OfflinePageModelTest, GetPagesToCleanUp) {
687 base::Time now = base::Time::Now();
688 OfflinePageItem page_1(
689 GURL(kTestUrl), kTestPageBookmarkId1,
690 base::FilePath(FILE_PATH_LITERAL("/test/location/page1.mhtml")),
691 kTestFileSize, now - base::TimeDelta::FromDays(40));
692 GetStore()->AddOfflinePage(
693 page_1,
694 base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr()));
695 PumpLoop();
696
697 OfflinePageItem page_2(
698 GURL(kTestUrl2), kTestPageBookmarkId2,
699 base::FilePath(FILE_PATH_LITERAL("/test/location/page2.mhtml")),
700 kTestFileSize, now - base::TimeDelta::FromDays(31));
701 GetStore()->AddOfflinePage(
702 page_2,
703 base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr()));
704 PumpLoop();
705
706 OfflinePageItem page_3(
707 GURL("http://test.xyz"), 42,
708 base::FilePath(FILE_PATH_LITERAL("/test/location/page3.mhtml")),
709 kTestFileSize, now - base::TimeDelta::FromDays(29));
710 GetStore()->AddOfflinePage(
711 page_3,
712 base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr()));
713 PumpLoop();
714
715 ResetModel();
716
717 // Only page_1 and page_2 are expected to be picked up by the model as page_3
718 // has not been in the store long enough.
719 std::vector<OfflinePageItem> pages_to_clean_up = model()->GetPagesToCleanUp();
720 EXPECT_EQ(2UL, pages_to_clean_up.size());
721 EXPECT_EQ(kTestUrl, pages_to_clean_up[0].url);
722 EXPECT_EQ(kTestPageBookmarkId1, pages_to_clean_up[0].bookmark_id);
723 EXPECT_EQ(kTestUrl2, pages_to_clean_up[1].url);
724 EXPECT_EQ(kTestPageBookmarkId2, pages_to_clean_up[1].bookmark_id);
725 }
726
656 } // namespace offline_pages 727 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_model.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698