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

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

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

Powered by Google App Engine
This is Rietveld 408576698