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

Unified 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, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/offline_pages/offline_page_model.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/offline_pages/offline_page_model_unittest.cc
diff --git a/components/offline_pages/offline_page_model_unittest.cc b/components/offline_pages/offline_page_model_unittest.cc
index 047c02d179ff571e896870971841b495aaf71bae..973191ef4c388dc877777effed682ff95312a1ec 100644
--- a/components/offline_pages/offline_page_model_unittest.cc
+++ b/components/offline_pages/offline_page_model_unittest.cc
@@ -14,6 +14,7 @@
#include "base/single_thread_task_runner.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
+#include "base/time/time.h"
#include "components/offline_pages/offline_page_item.h"
#include "components/offline_pages/offline_page_metadata_store.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -42,6 +43,7 @@ class OfflinePageTestStore : public OfflinePageMetadataStore {
explicit OfflinePageTestStore(
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
+ explicit OfflinePageTestStore(const OfflinePageTestStore& other_store);
~OfflinePageTestStore() override;
// OfflinePageMetadataStore overrides:
@@ -65,7 +67,7 @@ class OfflinePageTestStore : public OfflinePageMetadataStore {
std::vector<OfflinePageItem> offline_pages_;
- DISALLOW_COPY_AND_ASSIGN(OfflinePageTestStore);
+ DISALLOW_ASSIGN(OfflinePageTestStore);
};
OfflinePageTestStore::OfflinePageTestStore(
@@ -74,6 +76,12 @@ OfflinePageTestStore::OfflinePageTestStore(
scenario_(TestScenario::SUCCESSFUL) {
}
+OfflinePageTestStore::OfflinePageTestStore(
+ const OfflinePageTestStore& other_store)
+ : task_runner_(other_store.task_runner_),
+ scenario_(other_store.scenario_),
+ offline_pages_(other_store.offline_pages_) {}
+
OfflinePageTestStore::~OfflinePageTestStore() {
}
@@ -169,11 +177,16 @@ class OfflinePageModelTest
void OnSavePageDone(SavePageResult result);
void OnDeletePageDone(DeletePageResult result);
+ // OfflinePageMetadataStore callbacks.
+ void OnStoreUpdateDone(bool /* success */);
+
scoped_ptr<OfflinePageTestArchiver> BuildArchiver(
const GURL& url,
OfflinePageArchiver::ArchiverResult result);
scoped_ptr<OfflinePageMetadataStore> BuildStore();
- scoped_ptr<OfflinePageModel> BuildModel();
+ scoped_ptr<OfflinePageModel> BuildModel(
+ scoped_ptr<OfflinePageMetadataStore> store);
+ void ResetModel();
// Utility methods.
void PumpLoop();
@@ -257,7 +270,7 @@ OfflinePageModelTest::~OfflinePageModelTest() {
void OfflinePageModelTest::SetUp() {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
- model_ = BuildModel().Pass();
+ model_ = BuildModel(BuildStore().Pass()).Pass();
model_->AddObserver(this);
PumpLoop();
}
@@ -282,6 +295,10 @@ void OfflinePageModelTest::OnDeletePageDone(DeletePageResult result) {
last_delete_result_ = result;
}
+void OfflinePageModelTest::OnStoreUpdateDone(bool /* success - ignored */) {
+ run_loop_->Quit();
+}
+
scoped_ptr<OfflinePageTestArchiver> OfflinePageModelTest::BuildArchiver(
const GURL& url,
OfflinePageArchiver::ArchiverResult result) {
@@ -294,9 +311,20 @@ scoped_ptr<OfflinePageMetadataStore> OfflinePageModelTest::BuildStore() {
new OfflinePageTestStore(task_runner()));
}
-scoped_ptr<OfflinePageModel> OfflinePageModelTest::BuildModel() {
+scoped_ptr<OfflinePageModel> OfflinePageModelTest::BuildModel(
+ scoped_ptr<OfflinePageMetadataStore> store) {
return scoped_ptr<OfflinePageModel>(
- new OfflinePageModel(BuildStore().Pass(), task_runner()));
+ new OfflinePageModel(store.Pass(), task_runner()));
+}
+
+void OfflinePageModelTest::ResetModel() {
+ model_->RemoveObserver(this);
+ OfflinePageTestStore* old_store = GetStore();
+ scoped_ptr<OfflinePageMetadataStore> new_store(
+ new OfflinePageTestStore(*old_store));
+ model_ = BuildModel(new_store.Pass()).Pass();
+ model_->AddObserver(this);
+ PumpLoop();
}
void OfflinePageModelTest::PumpLoop() {
@@ -653,4 +681,47 @@ TEST_F(OfflinePageModelTest, GetPageByOfflineURL) {
EXPECT_FALSE(page);
}
+// Test that model returns pages that are older than 30 days as candidates for
+// clean up, hence the numbers in time delta.
+TEST_F(OfflinePageModelTest, GetPagesToCleanUp) {
+ base::Time now = base::Time::Now();
+ OfflinePageItem page_1(
+ GURL(kTestUrl), kTestPageBookmarkId1,
+ base::FilePath(FILE_PATH_LITERAL("/test/location/page1.mhtml")),
+ kTestFileSize, now - base::TimeDelta::FromDays(40));
+ GetStore()->AddOfflinePage(
+ page_1,
+ base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr()));
+ PumpLoop();
+
+ OfflinePageItem page_2(
+ GURL(kTestUrl2), kTestPageBookmarkId2,
+ base::FilePath(FILE_PATH_LITERAL("/test/location/page2.mhtml")),
+ kTestFileSize, now - base::TimeDelta::FromDays(31));
+ GetStore()->AddOfflinePage(
+ page_2,
+ base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr()));
+ PumpLoop();
+
+ OfflinePageItem page_3(
+ GURL("http://test.xyz"), 42,
+ base::FilePath(FILE_PATH_LITERAL("/test/location/page3.mhtml")),
+ kTestFileSize, now - base::TimeDelta::FromDays(29));
+ GetStore()->AddOfflinePage(
+ page_3,
+ base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr()));
+ PumpLoop();
+
+ ResetModel();
+
+ // Only page_1 and page_2 are expected to be picked up by the model as page_3
+ // has not been in the store long enough.
+ std::vector<OfflinePageItem> pages_to_clean_up = model()->GetPagesToCleanUp();
+ EXPECT_EQ(2UL, pages_to_clean_up.size());
+ EXPECT_EQ(kTestUrl, pages_to_clean_up[0].url);
+ EXPECT_EQ(kTestPageBookmarkId1, pages_to_clean_up[0].bookmark_id);
+ EXPECT_EQ(kTestUrl2, pages_to_clean_up[1].url);
+ EXPECT_EQ(kTestPageBookmarkId2, pages_to_clean_up[1].bookmark_id);
+}
+
} // namespace offline_pages
« 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