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

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

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