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

Unified Diff: components/offline_pages/offline_page_storage_manager_unittest.cc

Issue 1970953002: [Offline Pages] Adding more expiration logic. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@interface
Patch Set: More comments. Created 4 years, 7 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_storage_manager.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_storage_manager_unittest.cc
diff --git a/components/offline_pages/offline_page_storage_manager_unittest.cc b/components/offline_pages/offline_page_storage_manager_unittest.cc
index d1e0621f0fe6091e5c4a1782b307a3c7e194ceb9..3f4890c4bf8f88b5068e89571b094a4ea998f3ff 100644
--- a/components/offline_pages/offline_page_storage_manager_unittest.cc
+++ b/components/offline_pages/offline_page_storage_manager_unittest.cc
@@ -8,6 +8,7 @@
#include "base/bind.h"
#include "base/files/file_path.h"
+#include "base/test/simple_test_clock.h"
#include "base/time/time.h"
#include "components/offline_pages/client_policy_controller.h"
#include "components/offline_pages/offline_page_item.h"
@@ -18,30 +19,35 @@
namespace offline_pages {
namespace {
-const char kTestClientNamespace[] = "CLIENT_NAMESPACE";
+const char kBookmarkNamespace[] = "bookmark";
+const char kLastNNamespace[] = "last_n";
const GURL kTestUrl("http://example.com");
-const GURL kTestUrl2("http://other.page.com");
const base::FilePath::CharType kFilePath[] = FILE_PATH_LITERAL("TEST_FILEPATH");
-const ClientId kTestClientId1(kTestClientNamespace, "1234");
-const ClientId kTestClientId2(kTestClientNamespace, "5678");
const int64_t kTestFileSize = 876543LL;
-const int64_t kOfflineId = 1234LL;
-const int64_t kOfflineId2 = 2345LL;
+
+enum TestOptions {
+ DEFAULT = 1 << 0,
+ DELETE_FAILED = 1 << 1,
+};
+
+struct PageSettings {
+ std::string name_space;
+ int fresh_pages_count;
+ int expired_pages_count;
+};
} // namespace
class StorageManagerTestClient : public OfflinePageStorageManager::Client {
public:
- StorageManagerTestClient() {
- // Manually adding pages for the fake model.
- // Only the first page is expired.
- pages_.clear();
- pages_.push_back(OfflinePageItem(kTestUrl, kOfflineId, kTestClientId1,
- base::FilePath(kFilePath), kTestFileSize));
- pages_.push_back(OfflinePageItem(kTestUrl2, kOfflineId2, kTestClientId2,
- base::FilePath(kFilePath), kTestFileSize));
- base::Time now = base::Time::Now();
- pages_[0].last_access_time = now - base::TimeDelta::FromDays(10);
- pages_[1].last_access_time = now;
+ StorageManagerTestClient(std::vector<PageSettings> page_settings,
+ base::SimpleTestClock* clock,
+ TestOptions options = TestOptions::DEFAULT)
+ : policy_controller_(new ClientPolicyController()),
+ clock_(clock),
+ options_(options),
+ next_offline_id_(0) {
+ for (const auto& setting : page_settings)
+ AddPages(setting);
}
void GetAllPages(const MultipleOfflinePageItemCallback& callback) override {
@@ -50,25 +56,74 @@ class StorageManagerTestClient : public OfflinePageStorageManager::Client {
void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids,
const DeletePageCallback& callback) override {
- callback.Run(DeletePageResult::SUCCESS);
+ if (options_ & TestOptions::DELETE_FAILED) {
+ callback.Run(DeletePageResult::STORE_FAILURE);
+ } else {
+ callback.Run(DeletePageResult::SUCCESS);
+ }
}
+ base::SimpleTestClock* clock() { return clock_; }
+
private:
+ void AddPages(const PageSettings& setting);
+
std::vector<OfflinePageItem> pages_;
std::unique_ptr<ClientPolicyController> policy_controller_;
+
+ base::SimpleTestClock* clock_;
+
+ TestOptions options_;
+
+ int64_t next_offline_id_;
};
+void StorageManagerTestClient::AddPages(const PageSettings& setting) {
+ std::string name_space = setting.name_space;
+ int fresh_pages_count = setting.fresh_pages_count;
+ int expired_pages_count = setting.expired_pages_count;
+ base::Time now = clock()->Now();
+ // Fresh pages.
+ for (int i = 0; i < fresh_pages_count; i++) {
+ OfflinePageItem page =
+ OfflinePageItem(kTestUrl, next_offline_id_,
+ ClientId(name_space, std::to_string(next_offline_id_)),
+ base::FilePath(kFilePath), kTestFileSize);
+ next_offline_id_++;
+ page.last_access_time = now;
+ pages_.push_back(page);
+ }
+ // Expired pages.
+ for (int i = 0; i < expired_pages_count; i++) {
+ OfflinePageItem page =
+ OfflinePageItem(kTestUrl, next_offline_id_,
+ ClientId(name_space, std::to_string(next_offline_id_)),
+ base::FilePath(kFilePath), kTestFileSize);
+ next_offline_id_++;
+ page.last_access_time = now -
+ policy_controller_->GetPolicy(name_space)
+ .lifetime_policy.expiration_period;
+ pages_.push_back(page);
+ }
+}
+
class OfflinePageStorageManagerTest : public testing::Test {
public:
OfflinePageStorageManagerTest();
OfflinePageStorageManager* manager() { return manager_.get(); }
+ OfflinePageStorageManager::Client* client() { return client_.get(); }
+ ClientPolicyController* policy_controller() {
+ return policy_controller_.get();
+ }
void OnPagesCleared(int pages_cleared_count, DeletePageResult result);
+ void Initialize(const std::vector<PageSettings>& settings,
+ TestOptions options = TestOptions::DEFAULT);
// testing::Test
- void SetUp() override;
void TearDown() override;
+ base::SimpleTestClock* clock() { return clock_; }
int last_cleared_page_count() const { return last_cleared_page_count_; }
DeletePageResult last_delete_page_result() const {
return last_delete_page_result_;
@@ -77,19 +132,28 @@ class OfflinePageStorageManagerTest : public testing::Test {
private:
std::unique_ptr<OfflinePageStorageManager> manager_;
std::unique_ptr<OfflinePageStorageManager::Client> client_;
+ std::unique_ptr<ClientPolicyController> policy_controller_;
+
+ base::SimpleTestClock* clock_;
int last_cleared_page_count_;
DeletePageResult last_delete_page_result_;
};
OfflinePageStorageManagerTest::OfflinePageStorageManagerTest()
- : last_cleared_page_count_(0),
+ : policy_controller_(new ClientPolicyController()),
+ last_cleared_page_count_(0),
last_delete_page_result_(DeletePageResult::SUCCESS) {}
-void OfflinePageStorageManagerTest::SetUp() {
- client_.reset(new StorageManagerTestClient());
- manager_.reset(new OfflinePageStorageManager(client_.get(),
- new ClientPolicyController()));
+void OfflinePageStorageManagerTest::Initialize(
+ const std::vector<PageSettings>& page_settings,
+ TestOptions options) {
+ std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock());
+ clock_ = clock.get();
+ clock_->SetNow(base::Time::Now());
+ client_.reset(new StorageManagerTestClient(page_settings, clock_, options));
+ manager_.reset(new OfflinePageStorageManager(client(), policy_controller()));
+ manager_->SetClockForTesting(std::move(clock));
}
void OfflinePageStorageManagerTest::TearDown() {
@@ -103,11 +167,36 @@ void OfflinePageStorageManagerTest::OnPagesCleared(int pages_cleared_count,
last_delete_page_result_ = result;
}
-TEST_F(OfflinePageStorageManagerTest, TestClearPages) {
+TEST_F(OfflinePageStorageManagerTest, TestClearPagesLessThanLimit) {
+ Initialize(std::vector<PageSettings>(
+ {{kBookmarkNamespace, 1, 1}, {kLastNNamespace, 1, 1}}));
+ clock()->Advance(base::TimeDelta::FromMinutes(30));
+ manager()->ClearPagesIfNeeded(base::Bind(
+ &OfflinePageStorageManagerTest::OnPagesCleared, base::Unretained(this)));
+ EXPECT_EQ(2, last_cleared_page_count());
+ EXPECT_EQ(DeletePageResult::SUCCESS, last_delete_page_result());
+}
+
+TEST_F(OfflinePageStorageManagerTest, TestClearPagesMoreThanLimit) {
+ Initialize(std::vector<PageSettings>(
+ {{kBookmarkNamespace, 10, 15}, {kLastNNamespace, 5, 30}}));
+ clock()->Advance(base::TimeDelta::FromMinutes(30));
manager()->ClearPagesIfNeeded(base::Bind(
&OfflinePageStorageManagerTest::OnPagesCleared, base::Unretained(this)));
- EXPECT_EQ(1, last_cleared_page_count());
+ EXPECT_EQ(45, last_cleared_page_count());
EXPECT_EQ(DeletePageResult::SUCCESS, last_delete_page_result());
}
+TEST_F(OfflinePageStorageManagerTest, TestClearPagesMoreFreshPages) {
+ Initialize(std::vector<PageSettings>(
+ {{kBookmarkNamespace, 30, 0}, {kLastNNamespace, 100, 1}}));
+ clock()->Advance(base::TimeDelta::FromMinutes(30));
+ manager()->ClearPagesIfNeeded(base::Bind(
+ &OfflinePageStorageManagerTest::OnPagesCleared, base::Unretained(this)));
+ int last_n_page_limit = policy_controller()
+ ->GetPolicy(kLastNNamespace)
+ .lifetime_policy.page_limit;
+ EXPECT_EQ(1 + (100 - last_n_page_limit), last_cleared_page_count());
+ EXPECT_EQ(DeletePageResult::SUCCESS, last_delete_page_result());
+}
} // namespace offline_pages
« no previous file with comments | « components/offline_pages/offline_page_storage_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698