| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_storage_manager.h" | 5 #include "components/offline_pages/offline_page_storage_manager.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/test/simple_test_clock.h" | 13 #include "base/test/simple_test_clock.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "components/offline_pages/archive_manager.h" | 15 #include "components/offline_pages/archive_manager.h" |
| 16 #include "components/offline_pages/client_policy_controller.h" | 16 #include "components/offline_pages/client_policy_controller.h" |
| 17 #include "components/offline_pages/offline_page_item.h" | 17 #include "components/offline_pages/offline_page_item.h" |
| 18 #include "components/offline_pages/offline_page_storage_manager.h" | 18 #include "components/offline_pages/offline_page_model_impl.h" |
| 19 #include "components/offline_pages/offline_page_types.h" | 19 #include "components/offline_pages/offline_page_types.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 using LifetimePolicy = offline_pages::LifetimePolicy; | 22 using LifetimePolicy = offline_pages::LifetimePolicy; |
| 23 using ClearStorageResult = | 23 using ClearStorageResult = |
| 24 offline_pages::OfflinePageStorageManager::ClearStorageResult; | 24 offline_pages::OfflinePageStorageManager::ClearStorageResult; |
| 25 using StorageStats = offline_pages::ArchiveManager::StorageStats; | 25 using StorageStats = offline_pages::ArchiveManager::StorageStats; |
| 26 | 26 |
| 27 namespace offline_pages { | 27 namespace offline_pages { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 const char kBookmarkNamespace[] = "bookmark"; | |
| 31 const char kLastNNamespace[] = "last_n"; | |
| 32 const GURL kTestUrl("http://example.com"); | 30 const GURL kTestUrl("http://example.com"); |
| 33 const base::FilePath::CharType kFilePath[] = FILE_PATH_LITERAL("/data"); | 31 const base::FilePath::CharType kFilePath[] = FILE_PATH_LITERAL("/data"); |
| 34 const int64_t kTestFileSize = 500 * (1 << 10); | 32 const int64_t kTestFileSize = 500 * (1 << 10); |
| 35 const int64_t kFreeSpaceNormal = 100 * (1 << 20); | 33 const int64_t kFreeSpaceNormal = 100 * (1 << 20); |
| 36 | 34 |
| 37 enum TestOptions { | 35 enum TestOptions { |
| 38 DEFAULT = 1 << 0, | 36 DEFAULT = 1 << 0, |
| 39 EXPIRE_FAILURE = 1 << 1, | 37 EXPIRE_FAILURE = 1 << 1, |
| 40 DELETE_FAILURE = 1 << 2, | 38 DELETE_FAILURE = 1 << 2, |
| 41 EXPIRE_AND_DELETE_FAILURES = EXPIRE_FAILURE | DELETE_FAILURE, | 39 EXPIRE_AND_DELETE_FAILURES = EXPIRE_FAILURE | DELETE_FAILURE, |
| 42 }; | 40 }; |
| 43 | 41 |
| 44 struct PageSettings { | 42 struct PageSettings { |
| 45 std::string name_space; | 43 std::string name_space; |
| 46 int fresh_pages_count; | 44 int fresh_pages_count; |
| 47 int expired_pages_count; | 45 int expired_pages_count; |
| 48 }; | 46 }; |
| 49 } // namespace | 47 } // namespace |
| 50 | 48 |
| 51 class StorageManagerTestClient : public OfflinePageStorageManager::Client { | 49 class OfflinePageTestModel : public OfflinePageModelImpl { |
| 52 public: | 50 public: |
| 53 StorageManagerTestClient(std::vector<PageSettings> page_settings, | 51 OfflinePageTestModel(std::vector<PageSettings> page_settings, |
| 54 base::SimpleTestClock* clock, | 52 base::SimpleTestClock* clock, |
| 55 TestOptions options = TestOptions::DEFAULT) | 53 TestOptions options = TestOptions::DEFAULT) |
| 56 : policy_controller_(new ClientPolicyController()), | 54 : policy_controller_(new ClientPolicyController()), |
| 57 clock_(clock), | 55 clock_(clock), |
| 58 options_(options), | 56 options_(options), |
| 59 next_offline_id_(0) { | 57 next_offline_id_(0) { |
| 60 for (const auto& setting : page_settings) | 58 for (const auto& setting : page_settings) |
| 61 AddPages(setting); | 59 AddPages(setting); |
| 62 } | 60 } |
| 63 | 61 |
| 64 ~StorageManagerTestClient() override; | 62 ~OfflinePageTestModel() override; |
| 65 | 63 |
| 66 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override { | 64 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override { |
| 67 MultipleOfflinePageItemResult pages; | 65 MultipleOfflinePageItemResult pages; |
| 68 for (const auto& id_page_pair : pages_) | 66 for (const auto& id_page_pair : pages_) |
| 69 pages.push_back(id_page_pair.second); | 67 pages.push_back(id_page_pair.second); |
| 70 callback.Run(pages); | 68 callback.Run(pages); |
| 71 } | 69 } |
| 72 | 70 |
| 73 void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, | 71 void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, |
| 74 const DeletePageCallback& callback) override; | 72 const DeletePageCallback& callback) override; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 94 | 92 |
| 95 std::unique_ptr<ClientPolicyController> policy_controller_; | 93 std::unique_ptr<ClientPolicyController> policy_controller_; |
| 96 | 94 |
| 97 base::SimpleTestClock* clock_; | 95 base::SimpleTestClock* clock_; |
| 98 | 96 |
| 99 TestOptions options_; | 97 TestOptions options_; |
| 100 | 98 |
| 101 int64_t next_offline_id_; | 99 int64_t next_offline_id_; |
| 102 }; | 100 }; |
| 103 | 101 |
| 104 void StorageManagerTestClient::ExpirePages( | 102 void OfflinePageTestModel::ExpirePages( |
| 105 const std::vector<int64_t>& offline_ids, | 103 const std::vector<int64_t>& offline_ids, |
| 106 const base::Time& expiration_time, | 104 const base::Time& expiration_time, |
| 107 const base::Callback<void(bool)>& callback) { | 105 const base::Callback<void(bool)>& callback) { |
| 108 for (const auto id : offline_ids) | 106 for (const auto id : offline_ids) |
| 109 pages_.at(id).expiration_time = expiration_time; | 107 pages_.at(id).expiration_time = expiration_time; |
| 110 if (options_ & TestOptions::EXPIRE_FAILURE) { | 108 if (options_ & TestOptions::EXPIRE_FAILURE) { |
| 111 callback.Run(false); | 109 callback.Run(false); |
| 112 return; | 110 return; |
| 113 } | 111 } |
| 114 callback.Run(true); | 112 callback.Run(true); |
| 115 } | 113 } |
| 116 | 114 |
| 117 void StorageManagerTestClient::DeletePagesByOfflineId( | 115 void OfflinePageTestModel::DeletePagesByOfflineId( |
| 118 const std::vector<int64_t>& offline_ids, | 116 const std::vector<int64_t>& offline_ids, |
| 119 const DeletePageCallback& callback) { | 117 const DeletePageCallback& callback) { |
| 120 if (options_ & TestOptions::DELETE_FAILURE) { | 118 if (options_ & TestOptions::DELETE_FAILURE) { |
| 121 callback.Run(DeletePageResult::STORE_FAILURE); | 119 callback.Run(DeletePageResult::STORE_FAILURE); |
| 122 return; | 120 return; |
| 123 } | 121 } |
| 124 for (const auto id : offline_ids) { | 122 for (const auto id : offline_ids) { |
| 125 removed_pages_.push_back(pages_.at(id)); | 123 removed_pages_.push_back(pages_.at(id)); |
| 126 pages_.erase(id); | 124 pages_.erase(id); |
| 127 } | 125 } |
| 128 callback.Run(DeletePageResult::SUCCESS); | 126 callback.Run(DeletePageResult::SUCCESS); |
| 129 } | 127 } |
| 130 | 128 |
| 131 int64_t StorageManagerTestClient::GetTotalSize() const { | 129 int64_t OfflinePageTestModel::GetTotalSize() const { |
| 132 int64_t res = 0; | 130 int64_t res = 0; |
| 133 for (const auto& id_page_pair : pages_) { | 131 for (const auto& id_page_pair : pages_) { |
| 134 if (!id_page_pair.second.IsExpired()) | 132 if (!id_page_pair.second.IsExpired()) |
| 135 res += id_page_pair.second.file_size; | 133 res += id_page_pair.second.file_size; |
| 136 } | 134 } |
| 137 return res; | 135 return res; |
| 138 } | 136 } |
| 139 | 137 |
| 140 StorageManagerTestClient::~StorageManagerTestClient() {} | 138 OfflinePageTestModel::~OfflinePageTestModel() {} |
| 141 | 139 |
| 142 void StorageManagerTestClient::AddPages(const PageSettings& setting) { | 140 void OfflinePageTestModel::AddPages(const PageSettings& setting) { |
| 143 std::string name_space = setting.name_space; | 141 std::string name_space = setting.name_space; |
| 144 int fresh_pages_count = setting.fresh_pages_count; | 142 int fresh_pages_count = setting.fresh_pages_count; |
| 145 int expired_pages_count = setting.expired_pages_count; | 143 int expired_pages_count = setting.expired_pages_count; |
| 146 base::Time now = clock()->Now(); | 144 base::Time now = clock()->Now(); |
| 147 // Fresh pages. | 145 // Fresh pages. |
| 148 for (int i = 0; i < fresh_pages_count; i++) { | 146 for (int i = 0; i < fresh_pages_count; i++) { |
| 149 OfflinePageItem page = | 147 OfflinePageItem page = |
| 150 OfflinePageItem(kTestUrl, next_offline_id_, | 148 OfflinePageItem(kTestUrl, next_offline_id_, |
| 151 ClientId(name_space, std::to_string(next_offline_id_)), | 149 ClientId(name_space, std::to_string(next_offline_id_)), |
| 152 base::FilePath(kFilePath), kTestFileSize); | 150 base::FilePath(kFilePath), kTestFileSize); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 181 void SetValues(ArchiveManager::StorageStats stats) { stats_ = stats; } | 179 void SetValues(ArchiveManager::StorageStats stats) { stats_ = stats; } |
| 182 | 180 |
| 183 private: | 181 private: |
| 184 StorageStats stats_; | 182 StorageStats stats_; |
| 185 }; | 183 }; |
| 186 | 184 |
| 187 class OfflinePageStorageManagerTest : public testing::Test { | 185 class OfflinePageStorageManagerTest : public testing::Test { |
| 188 public: | 186 public: |
| 189 OfflinePageStorageManagerTest(); | 187 OfflinePageStorageManagerTest(); |
| 190 OfflinePageStorageManager* manager() { return manager_.get(); } | 188 OfflinePageStorageManager* manager() { return manager_.get(); } |
| 191 StorageManagerTestClient* client() { return client_.get(); } | 189 OfflinePageTestModel* model() { return model_.get(); } |
| 192 ClientPolicyController* policy_controller() { | 190 ClientPolicyController* policy_controller() { |
| 193 return policy_controller_.get(); | 191 return policy_controller_.get(); |
| 194 } | 192 } |
| 195 TestArchiveManager* test_archive_manager() { return archive_manager_.get(); } | 193 TestArchiveManager* test_archive_manager() { return archive_manager_.get(); } |
| 196 void OnPagesCleared(size_t pages_cleared_count, ClearStorageResult result); | 194 void OnPagesCleared(size_t pages_cleared_count, ClearStorageResult result); |
| 197 void Initialize(const std::vector<PageSettings>& settings, | 195 void Initialize(const std::vector<PageSettings>& settings, |
| 198 StorageStats stats = {kFreeSpaceNormal, 0}, | 196 StorageStats stats = {kFreeSpaceNormal, 0}, |
| 199 TestOptions options = TestOptions::DEFAULT); | 197 TestOptions options = TestOptions::DEFAULT); |
| 200 void TryClearPages(); | 198 void TryClearPages(); |
| 201 | 199 |
| 202 // testing::Test | 200 // testing::Test |
| 203 void TearDown() override; | 201 void TearDown() override; |
| 204 | 202 |
| 205 base::SimpleTestClock* clock() { return clock_; } | 203 base::SimpleTestClock* clock() { return clock_; } |
| 206 int last_cleared_page_count() const { | 204 int last_cleared_page_count() const { |
| 207 return static_cast<int>(last_cleared_page_count_); | 205 return static_cast<int>(last_cleared_page_count_); |
| 208 } | 206 } |
| 209 int total_cleared_times() const { return total_cleared_times_; } | 207 int total_cleared_times() const { return total_cleared_times_; } |
| 210 ClearStorageResult last_clear_storage_result() const { | 208 ClearStorageResult last_clear_storage_result() const { |
| 211 return last_clear_storage_result_; | 209 return last_clear_storage_result_; |
| 212 } | 210 } |
| 213 | 211 |
| 214 private: | 212 private: |
| 215 std::unique_ptr<OfflinePageStorageManager> manager_; | 213 std::unique_ptr<OfflinePageStorageManager> manager_; |
| 216 std::unique_ptr<StorageManagerTestClient> client_; | 214 std::unique_ptr<OfflinePageTestModel> model_; |
| 217 std::unique_ptr<ClientPolicyController> policy_controller_; | 215 std::unique_ptr<ClientPolicyController> policy_controller_; |
| 218 std::unique_ptr<TestArchiveManager> archive_manager_; | 216 std::unique_ptr<TestArchiveManager> archive_manager_; |
| 219 | 217 |
| 220 base::SimpleTestClock* clock_; | 218 base::SimpleTestClock* clock_; |
| 221 | 219 |
| 222 size_t last_cleared_page_count_; | 220 size_t last_cleared_page_count_; |
| 223 int total_cleared_times_; | 221 int total_cleared_times_; |
| 224 ClearStorageResult last_clear_storage_result_; | 222 ClearStorageResult last_clear_storage_result_; |
| 225 }; | 223 }; |
| 226 | 224 |
| 227 OfflinePageStorageManagerTest::OfflinePageStorageManagerTest() | 225 OfflinePageStorageManagerTest::OfflinePageStorageManagerTest() |
| 228 : policy_controller_(new ClientPolicyController()), | 226 : policy_controller_(new ClientPolicyController()), |
| 229 last_cleared_page_count_(0), | 227 last_cleared_page_count_(0), |
| 230 total_cleared_times_(0), | 228 total_cleared_times_(0), |
| 231 last_clear_storage_result_(ClearStorageResult::SUCCESS) {} | 229 last_clear_storage_result_(ClearStorageResult::SUCCESS) {} |
| 232 | 230 |
| 233 void OfflinePageStorageManagerTest::Initialize( | 231 void OfflinePageStorageManagerTest::Initialize( |
| 234 const std::vector<PageSettings>& page_settings, | 232 const std::vector<PageSettings>& page_settings, |
| 235 StorageStats stats, | 233 StorageStats stats, |
| 236 TestOptions options) { | 234 TestOptions options) { |
| 237 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); | 235 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); |
| 238 clock_ = clock.get(); | 236 clock_ = clock.get(); |
| 239 clock_->SetNow(base::Time::Now()); | 237 clock_->SetNow(base::Time::Now()); |
| 240 client_.reset(new StorageManagerTestClient(page_settings, clock_, options)); | 238 model_.reset(new OfflinePageTestModel(page_settings, clock_, options)); |
| 241 | 239 |
| 242 if (stats.free_disk_space == 0) | 240 if (stats.free_disk_space == 0) |
| 243 stats.free_disk_space = kFreeSpaceNormal; | 241 stats.free_disk_space = kFreeSpaceNormal; |
| 244 if (stats.total_archives_size == 0) { | 242 if (stats.total_archives_size == 0) { |
| 245 stats.total_archives_size = client_->GetTotalSize(); | 243 stats.total_archives_size = model_->GetTotalSize(); |
| 246 } | 244 } |
| 247 archive_manager_.reset(new TestArchiveManager(stats)); | 245 archive_manager_.reset(new TestArchiveManager(stats)); |
| 248 manager_.reset(new OfflinePageStorageManager( | 246 manager_.reset(new OfflinePageStorageManager( |
| 249 client_.get(), policy_controller(), archive_manager_.get())); | 247 model_.get(), policy_controller(), archive_manager_.get())); |
| 250 manager_->SetClockForTesting(std::move(clock)); | 248 manager_->SetClockForTesting(std::move(clock)); |
| 251 } | 249 } |
| 252 | 250 |
| 253 void OfflinePageStorageManagerTest::TryClearPages() { | 251 void OfflinePageStorageManagerTest::TryClearPages() { |
| 254 manager()->ClearPagesIfNeeded(base::Bind( | 252 manager()->ClearPagesIfNeeded(base::Bind( |
| 255 &OfflinePageStorageManagerTest::OnPagesCleared, base::Unretained(this))); | 253 &OfflinePageStorageManagerTest::OnPagesCleared, base::Unretained(this))); |
| 256 } | 254 } |
| 257 | 255 |
| 258 void OfflinePageStorageManagerTest::TearDown() { | 256 void OfflinePageStorageManagerTest::TearDown() { |
| 259 manager_.reset(); | 257 manager_.reset(); |
| 260 client_.reset(); | 258 model_.reset(); |
| 261 } | 259 } |
| 262 | 260 |
| 263 void OfflinePageStorageManagerTest::OnPagesCleared(size_t pages_cleared_count, | 261 void OfflinePageStorageManagerTest::OnPagesCleared(size_t pages_cleared_count, |
| 264 ClearStorageResult result) { | 262 ClearStorageResult result) { |
| 265 last_cleared_page_count_ = pages_cleared_count; | 263 last_cleared_page_count_ = pages_cleared_count; |
| 266 total_cleared_times_++; | 264 total_cleared_times_++; |
| 267 last_clear_storage_result_ = result; | 265 last_clear_storage_result_ = result; |
| 268 } | 266 } |
| 269 | 267 |
| 270 TEST_F(OfflinePageStorageManagerTest, TestClearPagesLessThanLimit) { | 268 TEST_F(OfflinePageStorageManagerTest, TestClearPagesLessThanLimit) { |
| 271 Initialize(std::vector<PageSettings>( | 269 Initialize(std::vector<PageSettings>( |
| 272 {{kBookmarkNamespace, 1, 1}, {kLastNNamespace, 1, 1}})); | 270 {{kBookmarkNamespace, 1, 1}, {kLastNNamespace, 1, 1}})); |
| 273 clock()->Advance(base::TimeDelta::FromMinutes(30)); | 271 clock()->Advance(base::TimeDelta::FromMinutes(30)); |
| 274 TryClearPages(); | 272 TryClearPages(); |
| 275 EXPECT_EQ(2, last_cleared_page_count()); | 273 EXPECT_EQ(2, last_cleared_page_count()); |
| 276 EXPECT_EQ(1, total_cleared_times()); | 274 EXPECT_EQ(1, total_cleared_times()); |
| 277 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 275 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 278 EXPECT_EQ(0, static_cast<int>(client()->GetRemovedPages().size())); | 276 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 279 } | 277 } |
| 280 | 278 |
| 281 TEST_F(OfflinePageStorageManagerTest, TestClearPagesMoreThanLimit) { | 279 TEST_F(OfflinePageStorageManagerTest, TestClearPagesMoreThanLimit) { |
| 282 Initialize(std::vector<PageSettings>( | 280 Initialize(std::vector<PageSettings>( |
| 283 {{kBookmarkNamespace, 10, 15}, {kLastNNamespace, 5, 30}})); | 281 {{kBookmarkNamespace, 10, 15}, {kLastNNamespace, 5, 30}})); |
| 284 clock()->Advance(base::TimeDelta::FromMinutes(30)); | 282 clock()->Advance(base::TimeDelta::FromMinutes(30)); |
| 285 TryClearPages(); | 283 TryClearPages(); |
| 286 EXPECT_EQ(45, last_cleared_page_count()); | 284 EXPECT_EQ(45, last_cleared_page_count()); |
| 287 EXPECT_EQ(1, total_cleared_times()); | 285 EXPECT_EQ(1, total_cleared_times()); |
| 288 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 286 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 289 EXPECT_EQ(0, static_cast<int>(client()->GetRemovedPages().size())); | 287 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 290 } | 288 } |
| 291 | 289 |
| 292 TEST_F(OfflinePageStorageManagerTest, TestClearPagesMoreFreshPages) { | 290 TEST_F(OfflinePageStorageManagerTest, TestClearPagesMoreFreshPages) { |
| 293 Initialize(std::vector<PageSettings>( | 291 Initialize(std::vector<PageSettings>( |
| 294 {{kBookmarkNamespace, 30, 0}, {kLastNNamespace, 100, 1}}), | 292 {{kBookmarkNamespace, 30, 0}, {kLastNNamespace, 100, 1}}), |
| 295 {500 * (1 << 20), 0}); | 293 {500 * (1 << 20), 0}); |
| 296 clock()->Advance(base::TimeDelta::FromMinutes(30)); | 294 clock()->Advance(base::TimeDelta::FromMinutes(30)); |
| 297 TryClearPages(); | 295 TryClearPages(); |
| 298 int last_n_page_limit = static_cast<int>(policy_controller() | 296 int last_n_page_limit = static_cast<int>(policy_controller() |
| 299 ->GetPolicy(kLastNNamespace) | 297 ->GetPolicy(kLastNNamespace) |
| 300 .lifetime_policy.page_limit); | 298 .lifetime_policy.page_limit); |
| 301 EXPECT_EQ(1 + (100 - last_n_page_limit), last_cleared_page_count()); | 299 EXPECT_EQ(1 + (100 - last_n_page_limit), last_cleared_page_count()); |
| 302 EXPECT_EQ(1, total_cleared_times()); | 300 EXPECT_EQ(1, total_cleared_times()); |
| 303 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 301 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 304 EXPECT_EQ(0, static_cast<int>(client()->GetRemovedPages().size())); | 302 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 305 } | 303 } |
| 306 | 304 |
| 307 TEST_F(OfflinePageStorageManagerTest, TestDeletionFailed) { | 305 TEST_F(OfflinePageStorageManagerTest, TestDeletionFailed) { |
| 308 Initialize(std::vector<PageSettings>( | 306 Initialize(std::vector<PageSettings>( |
| 309 {{kBookmarkNamespace, 10, 10}, {kLastNNamespace, 10, 10}}), | 307 {{kBookmarkNamespace, 10, 10}, {kLastNNamespace, 10, 10}}), |
| 310 {kFreeSpaceNormal, 0}, TestOptions::DELETE_FAILURE); | 308 {kFreeSpaceNormal, 0}, TestOptions::DELETE_FAILURE); |
| 311 TryClearPages(); | 309 TryClearPages(); |
| 312 EXPECT_EQ(20, last_cleared_page_count()); | 310 EXPECT_EQ(20, last_cleared_page_count()); |
| 313 EXPECT_EQ(1, total_cleared_times()); | 311 EXPECT_EQ(1, total_cleared_times()); |
| 314 EXPECT_EQ(ClearStorageResult::DELETE_FAILURE, last_clear_storage_result()); | 312 EXPECT_EQ(ClearStorageResult::DELETE_FAILURE, last_clear_storage_result()); |
| 315 EXPECT_EQ(0, static_cast<int>(client()->GetRemovedPages().size())); | 313 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 316 } | 314 } |
| 317 | 315 |
| 318 TEST_F(OfflinePageStorageManagerTest, TestRemoveFromStoreFailure) { | 316 TEST_F(OfflinePageStorageManagerTest, TestRemoveFromStoreFailure) { |
| 319 Initialize(std::vector<PageSettings>({{kBookmarkNamespace, 10, 10}}), {0, 0}, | 317 Initialize(std::vector<PageSettings>({{kBookmarkNamespace, 10, 10}}), {0, 0}, |
| 320 TestOptions::EXPIRE_FAILURE); | 318 TestOptions::EXPIRE_FAILURE); |
| 321 clock()->Advance(base::TimeDelta::FromMinutes(30)); | 319 clock()->Advance(base::TimeDelta::FromMinutes(30)); |
| 322 TryClearPages(); | 320 TryClearPages(); |
| 323 EXPECT_EQ(10, last_cleared_page_count()); | 321 EXPECT_EQ(10, last_cleared_page_count()); |
| 324 EXPECT_EQ(1, total_cleared_times()); | 322 EXPECT_EQ(1, total_cleared_times()); |
| 325 EXPECT_EQ(ClearStorageResult::EXPIRE_FAILURE, last_clear_storage_result()); | 323 EXPECT_EQ(ClearStorageResult::EXPIRE_FAILURE, last_clear_storage_result()); |
| 326 EXPECT_EQ(0, static_cast<int>(client()->GetRemovedPages().size())); | 324 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 327 } | 325 } |
| 328 | 326 |
| 329 TEST_F(OfflinePageStorageManagerTest, TestBothFailure) { | 327 TEST_F(OfflinePageStorageManagerTest, TestBothFailure) { |
| 330 Initialize(std::vector<PageSettings>({{kBookmarkNamespace, 10, 10}}), {0, 0}, | 328 Initialize(std::vector<PageSettings>({{kBookmarkNamespace, 10, 10}}), {0, 0}, |
| 331 TestOptions::EXPIRE_AND_DELETE_FAILURES); | 329 TestOptions::EXPIRE_AND_DELETE_FAILURES); |
| 332 clock()->Advance(base::TimeDelta::FromMinutes(30)); | 330 clock()->Advance(base::TimeDelta::FromMinutes(30)); |
| 333 TryClearPages(); | 331 TryClearPages(); |
| 334 EXPECT_EQ(ClearStorageResult::EXPIRE_AND_DELETE_FAILURES, | 332 EXPECT_EQ(ClearStorageResult::EXPIRE_AND_DELETE_FAILURES, |
| 335 last_clear_storage_result()); | 333 last_clear_storage_result()); |
| 336 } | 334 } |
| 337 | 335 |
| 338 TEST_F(OfflinePageStorageManagerTest, TestStorageTimeInterval) { | 336 TEST_F(OfflinePageStorageManagerTest, TestStorageTimeInterval) { |
| 339 Initialize(std::vector<PageSettings>( | 337 Initialize(std::vector<PageSettings>( |
| 340 {{kBookmarkNamespace, 10, 10}, {kLastNNamespace, 10, 10}})); | 338 {{kBookmarkNamespace, 10, 10}, {kLastNNamespace, 10, 10}})); |
| 341 clock()->Advance(base::TimeDelta::FromMinutes(30)); | 339 clock()->Advance(base::TimeDelta::FromMinutes(30)); |
| 342 TryClearPages(); | 340 TryClearPages(); |
| 343 EXPECT_EQ(20, last_cleared_page_count()); | 341 EXPECT_EQ(20, last_cleared_page_count()); |
| 344 EXPECT_EQ(1, total_cleared_times()); | 342 EXPECT_EQ(1, total_cleared_times()); |
| 345 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 343 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 346 EXPECT_EQ(0, static_cast<int>(client()->GetRemovedPages().size())); | 344 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 347 | 345 |
| 348 // Advance clock so we go over the gap, but no expired pages. | 346 // Advance clock so we go over the gap, but no expired pages. |
| 349 clock()->Advance(kClearStorageInterval + base::TimeDelta::FromMinutes(1)); | 347 clock()->Advance(kClearStorageInterval + base::TimeDelta::FromMinutes(1)); |
| 350 TryClearPages(); | 348 TryClearPages(); |
| 351 EXPECT_EQ(0, last_cleared_page_count()); | 349 EXPECT_EQ(0, last_cleared_page_count()); |
| 352 EXPECT_EQ(2, total_cleared_times()); | 350 EXPECT_EQ(2, total_cleared_times()); |
| 353 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 351 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 354 EXPECT_EQ(0, static_cast<int>(client()->GetRemovedPages().size())); | 352 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 355 | 353 |
| 356 // Advance clock so we are still in the gap, should be unnecessary. | 354 // Advance clock so we are still in the gap, should be unnecessary. |
| 357 clock()->Advance(kClearStorageInterval - base::TimeDelta::FromMinutes(1)); | 355 clock()->Advance(kClearStorageInterval - base::TimeDelta::FromMinutes(1)); |
| 358 TryClearPages(); | 356 TryClearPages(); |
| 359 EXPECT_EQ(0, last_cleared_page_count()); | 357 EXPECT_EQ(0, last_cleared_page_count()); |
| 360 EXPECT_EQ(3, total_cleared_times()); | 358 EXPECT_EQ(3, total_cleared_times()); |
| 361 EXPECT_EQ(ClearStorageResult::UNNECESSARY, last_clear_storage_result()); | 359 EXPECT_EQ(ClearStorageResult::UNNECESSARY, last_clear_storage_result()); |
| 362 EXPECT_EQ(0, static_cast<int>(client()->GetRemovedPages().size())); | 360 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 363 } | 361 } |
| 364 | 362 |
| 365 TEST_F(OfflinePageStorageManagerTest, TestTwoStepExpiration) { | 363 TEST_F(OfflinePageStorageManagerTest, TestTwoStepExpiration) { |
| 366 Initialize(std::vector<PageSettings>({{kBookmarkNamespace, 10, 10}})); | 364 Initialize(std::vector<PageSettings>({{kBookmarkNamespace, 10, 10}})); |
| 367 clock()->Advance(base::TimeDelta::FromMinutes(30)); | 365 clock()->Advance(base::TimeDelta::FromMinutes(30)); |
| 368 TryClearPages(); | 366 TryClearPages(); |
| 369 EXPECT_EQ(10, last_cleared_page_count()); | 367 EXPECT_EQ(10, last_cleared_page_count()); |
| 370 EXPECT_EQ(1, total_cleared_times()); | 368 EXPECT_EQ(1, total_cleared_times()); |
| 371 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 369 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 372 EXPECT_EQ(0, static_cast<int>(client()->GetRemovedPages().size())); | 370 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 373 | 371 |
| 374 clock()->Advance(kRemovePageItemInterval + base::TimeDelta::FromDays(1)); | 372 clock()->Advance(kRemovePageItemInterval + base::TimeDelta::FromDays(1)); |
| 375 TryClearPages(); | 373 TryClearPages(); |
| 376 EXPECT_EQ(10, last_cleared_page_count()); | 374 EXPECT_EQ(10, last_cleared_page_count()); |
| 377 EXPECT_EQ(2, total_cleared_times()); | 375 EXPECT_EQ(2, total_cleared_times()); |
| 378 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 376 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 379 EXPECT_EQ(10, static_cast<int>(client()->GetRemovedPages().size())); | 377 EXPECT_EQ(10, static_cast<int>(model()->GetRemovedPages().size())); |
| 380 } | 378 } |
| 381 | 379 |
| 382 TEST_F(OfflinePageStorageManagerTest, TestClearMultipleTimes) { | 380 TEST_F(OfflinePageStorageManagerTest, TestClearMultipleTimes) { |
| 383 Initialize(std::vector<PageSettings>( | 381 Initialize(std::vector<PageSettings>( |
| 384 {{kBookmarkNamespace, 30, 0}, {kLastNNamespace, 100, 1}}), | 382 {{kBookmarkNamespace, 30, 0}, {kLastNNamespace, 100, 1}}), |
| 385 {500 * (1 << 20), 0}); | 383 {500 * (1 << 20), 0}); |
| 386 clock()->Advance(base::TimeDelta::FromMinutes(30)); | 384 clock()->Advance(base::TimeDelta::FromMinutes(30)); |
| 387 LifetimePolicy policy = | 385 LifetimePolicy policy = |
| 388 policy_controller()->GetPolicy(kLastNNamespace).lifetime_policy; | 386 policy_controller()->GetPolicy(kLastNNamespace).lifetime_policy; |
| 389 int last_n_page_limit = static_cast<int>(policy.page_limit); | 387 int last_n_page_limit = static_cast<int>(policy.page_limit); |
| 390 | 388 |
| 391 TryClearPages(); | 389 TryClearPages(); |
| 392 EXPECT_EQ(1 + (100 - last_n_page_limit), last_cleared_page_count()); | 390 EXPECT_EQ(1 + (100 - last_n_page_limit), last_cleared_page_count()); |
| 393 EXPECT_EQ(1, total_cleared_times()); | 391 EXPECT_EQ(1, total_cleared_times()); |
| 394 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 392 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 395 EXPECT_EQ(0, static_cast<int>(client()->GetRemovedPages().size())); | 393 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 396 | 394 |
| 397 // Advance the clock by expiration period of last_n namespace, should be | 395 // Advance the clock by expiration period of last_n namespace, should be |
| 398 // expiring all pages left in the namespace. | 396 // expiring all pages left in the namespace. |
| 399 clock()->Advance(policy.expiration_period); | 397 clock()->Advance(policy.expiration_period); |
| 400 TryClearPages(); | 398 TryClearPages(); |
| 401 EXPECT_EQ(last_n_page_limit, last_cleared_page_count()); | 399 EXPECT_EQ(last_n_page_limit, last_cleared_page_count()); |
| 402 EXPECT_EQ(2, total_cleared_times()); | 400 EXPECT_EQ(2, total_cleared_times()); |
| 403 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 401 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 404 EXPECT_EQ(0, static_cast<int>(client()->GetRemovedPages().size())); | 402 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 405 | 403 |
| 406 // Only 1 ms passes and no changes in pages, so no need to clear page. | 404 // Only 1 ms passes and no changes in pages, so no need to clear page. |
| 407 clock()->Advance(base::TimeDelta::FromMilliseconds(1)); | 405 clock()->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 408 TryClearPages(); | 406 TryClearPages(); |
| 409 EXPECT_EQ(0, last_cleared_page_count()); | 407 EXPECT_EQ(0, last_cleared_page_count()); |
| 410 EXPECT_EQ(3, total_cleared_times()); | 408 EXPECT_EQ(3, total_cleared_times()); |
| 411 EXPECT_EQ(ClearStorageResult::UNNECESSARY, last_clear_storage_result()); | 409 EXPECT_EQ(ClearStorageResult::UNNECESSARY, last_clear_storage_result()); |
| 412 EXPECT_EQ(0, static_cast<int>(client()->GetRemovedPages().size())); | 410 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 413 | 411 |
| 414 // Adding more fresh pages to make it go over limit. | 412 // Adding more fresh pages to make it go over limit. |
| 415 clock()->Advance(base::TimeDelta::FromMinutes(5)); | 413 clock()->Advance(base::TimeDelta::FromMinutes(5)); |
| 416 client()->AddPages({kBookmarkNamespace, 400, 0}); | 414 model()->AddPages({kBookmarkNamespace, 400, 0}); |
| 417 int64_t total_size_before = client()->GetTotalSize(); | 415 int64_t total_size_before = model()->GetTotalSize(); |
| 418 int64_t available_space = 300 * (1 << 20); // 300 MB | 416 int64_t available_space = 300 * (1 << 20); // 300 MB |
| 419 test_archive_manager()->SetValues({available_space, total_size_before}); | 417 test_archive_manager()->SetValues({available_space, total_size_before}); |
| 420 EXPECT_GE(total_size_before, | 418 EXPECT_GE(total_size_before, |
| 421 kOfflinePageStorageLimit * (available_space + total_size_before)); | 419 kOfflinePageStorageLimit * (available_space + total_size_before)); |
| 422 TryClearPages(); | 420 TryClearPages(); |
| 423 EXPECT_LE(total_size_before * kOfflinePageStorageClearThreshold, | 421 EXPECT_LE(total_size_before * kOfflinePageStorageClearThreshold, |
| 424 client()->GetTotalSize()); | 422 model()->GetTotalSize()); |
| 425 EXPECT_EQ(4, total_cleared_times()); | 423 EXPECT_EQ(4, total_cleared_times()); |
| 426 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 424 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 427 EXPECT_EQ(0, static_cast<int>(client()->GetRemovedPages().size())); | 425 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 428 int expired_page_count = last_cleared_page_count(); | 426 int expired_page_count = last_cleared_page_count(); |
| 429 | 427 |
| 430 // After more days, all pages should be expired and . | 428 // After more days, all pages should be expired and . |
| 431 clock()->Advance(kRemovePageItemInterval + base::TimeDelta::FromDays(1)); | 429 clock()->Advance(kRemovePageItemInterval + base::TimeDelta::FromDays(1)); |
| 432 TryClearPages(); | 430 TryClearPages(); |
| 433 EXPECT_EQ(0, client()->GetTotalSize()); | 431 EXPECT_EQ(0, model()->GetTotalSize()); |
| 434 EXPECT_EQ(5, total_cleared_times()); | 432 EXPECT_EQ(5, total_cleared_times()); |
| 435 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 433 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 436 // Number of removed pages should be the ones expired above and all the pages | 434 // Number of removed pages should be the ones expired above and all the pages |
| 437 // initially created for last_n namespace. | 435 // initially created for last_n namespace. |
| 438 EXPECT_EQ(expired_page_count + 101, | 436 EXPECT_EQ(expired_page_count + 101, |
| 439 static_cast<int>(client()->GetRemovedPages().size())); | 437 static_cast<int>(model()->GetRemovedPages().size())); |
| 440 } | 438 } |
| 441 | 439 |
| 442 } // namespace offline_pages | 440 } // namespace offline_pages |
| OLD | NEW |