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

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: 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 DELETE_FAILURE = 1 << 1,
39 };
40
41 struct PageSettings {
42 std::string name_space;
43 int fresh_pages_count;
44 int expired_pages_count;
45 };
46 } // namespace
47
48 class OfflinePageTestModel : public OfflinePageModelImpl {
49 public:
50 OfflinePageTestModel(std::vector<PageSettings> page_settings,
51 base::SimpleTestClock* clock,
52 TestOptions options = TestOptions::DEFAULT)
53 : policy_controller_(new ClientPolicyController()),
54 clock_(clock),
55 options_(options),
56 next_offline_id_(0) {
57 for (const auto& setting : page_settings)
58 AddPages(setting);
59 }
60
61 ~OfflinePageTestModel() override;
62
63 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override {
64 MultipleOfflinePageItemResult pages;
65 for (const auto& id_page_pair : pages_)
66 pages.push_back(id_page_pair.second);
67 callback.Run(pages);
68 }
69
70 void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids,
71 const DeletePageCallback& callback) override;
72
73 void AddPages(const PageSettings& setting);
74
75 // The |removed_pages_| would not be cleared in during a test, so the number
76 // of removed pages will be accumulative in a single test case.
77 const std::vector<OfflinePageItem>& GetRemovedPages() {
78 return removed_pages_;
79 }
80
81 int64_t GetTotalSize() const;
82
83 base::SimpleTestClock* clock() { return clock_; }
84
85 private:
86 std::map<int64_t, OfflinePageItem> pages_;
87
88 std::vector<OfflinePageItem> removed_pages_;
89
90 std::unique_ptr<ClientPolicyController> policy_controller_;
91
92 base::SimpleTestClock* clock_;
93
94 TestOptions options_;
95
96 int64_t next_offline_id_;
97 };
98
99 void OfflinePageTestModel::DeletePagesByOfflineId(
100 const std::vector<int64_t>& offline_ids,
101 const DeletePageCallback& callback) {
102 if (options_ & TestOptions::DELETE_FAILURE) {
103 callback.Run(DeletePageResult::STORE_FAILURE);
104 return;
105 }
106 for (const auto id : offline_ids) {
107 removed_pages_.push_back(pages_.at(id));
108 pages_.erase(id);
109 }
110 callback.Run(DeletePageResult::SUCCESS);
111 }
112
113 int64_t OfflinePageTestModel::GetTotalSize() const {
114 int64_t res = 0;
115 for (const auto& id_page_pair : pages_)
116 res += id_page_pair.second.file_size;
117 return res;
118 }
119
120 OfflinePageTestModel::~OfflinePageTestModel() {}
121
122 void OfflinePageTestModel::AddPages(const PageSettings& setting) {
123 std::string name_space = setting.name_space;
124 int fresh_pages_count = setting.fresh_pages_count;
125 int expired_pages_count = setting.expired_pages_count;
126 base::Time now = clock()->Now();
127 // Fresh pages.
128 for (int i = 0; i < fresh_pages_count; i++) {
129 OfflinePageItem page =
130 OfflinePageItem(kTestUrl, next_offline_id_,
131 ClientId(name_space, std::to_string(next_offline_id_)),
132 base::FilePath(kFilePath), kTestFileSize);
133 page.last_access_time = now;
134 pages_[next_offline_id_] = page;
135 next_offline_id_++;
136 }
137 // Expired pages.
138 for (int i = 0; i < expired_pages_count; i++) {
139 OfflinePageItem page =
140 OfflinePageItem(kTestUrl, next_offline_id_,
141 ClientId(name_space, std::to_string(next_offline_id_)),
142 base::FilePath(kFilePath), kTestFileSize);
143 page.last_access_time = now -
144 policy_controller_->GetPolicy(name_space)
145 .lifetime_policy.expiration_period;
146 pages_[next_offline_id_] = page;
147 next_offline_id_++;
148 }
149 }
150
151 class TestArchiveManager : public ArchiveManager {
152 public:
153 explicit TestArchiveManager(StorageStats stats) : stats_(stats) {}
154
155 void GetStorageStats(const base::Callback<
156 void(const ArchiveManager::StorageStats& storage_stats)>&
157 callback) const override {
158 callback.Run(stats_);
159 }
160
161 void SetValues(ArchiveManager::StorageStats stats) { stats_ = stats; }
162
163 private:
164 StorageStats stats_;
165 };
166
167 class OfflinePageStorageManagerTest : public testing::Test {
168 public:
169 OfflinePageStorageManagerTest();
170 OfflinePageStorageManager* manager() { return manager_.get(); }
171 OfflinePageTestModel* model() { return model_.get(); }
172 ClientPolicyController* policy_controller() {
173 return policy_controller_.get();
174 }
175 TestArchiveManager* test_archive_manager() { return archive_manager_.get(); }
176 void OnPagesCleared(size_t pages_cleared_count, ClearStorageResult result);
177 void Initialize(const std::vector<PageSettings>& settings,
178 StorageStats stats = {kFreeSpaceNormal, 0},
179 TestOptions options = TestOptions::DEFAULT);
180 void TryClearPages();
181
182 // testing::Test
183 void TearDown() override;
184
185 base::SimpleTestClock* clock() { return clock_; }
186 int last_cleared_page_count() const {
187 return static_cast<int>(last_cleared_page_count_);
188 }
189 int total_cleared_times() const { return total_cleared_times_; }
190 ClearStorageResult last_clear_storage_result() const {
191 return last_clear_storage_result_;
192 }
193
194 private:
195 std::unique_ptr<OfflinePageStorageManager> manager_;
196 std::unique_ptr<OfflinePageTestModel> model_;
197 std::unique_ptr<ClientPolicyController> policy_controller_;
198 std::unique_ptr<TestArchiveManager> archive_manager_;
199
200 base::SimpleTestClock* clock_;
201
202 size_t last_cleared_page_count_;
203 int total_cleared_times_;
204 ClearStorageResult last_clear_storage_result_;
205 };
206
207 OfflinePageStorageManagerTest::OfflinePageStorageManagerTest()
208 : policy_controller_(new ClientPolicyController()),
209 last_cleared_page_count_(0),
210 total_cleared_times_(0),
211 last_clear_storage_result_(ClearStorageResult::SUCCESS) {}
212
213 void OfflinePageStorageManagerTest::Initialize(
214 const std::vector<PageSettings>& page_settings,
215 StorageStats stats,
216 TestOptions options) {
217 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock());
218 clock_ = clock.get();
219 clock_->SetNow(base::Time::Now());
220 model_.reset(new OfflinePageTestModel(page_settings, clock_, options));
221
222 if (stats.free_disk_space == 0)
223 stats.free_disk_space = kFreeSpaceNormal;
224 if (stats.total_archives_size == 0) {
225 stats.total_archives_size = model_->GetTotalSize();
226 }
227 archive_manager_.reset(new TestArchiveManager(stats));
228 manager_.reset(new OfflinePageStorageManager(
229 model_.get(), policy_controller(), archive_manager_.get()));
230 manager_->SetClockForTesting(std::move(clock));
231 }
232
233 void OfflinePageStorageManagerTest::TryClearPages() {
234 manager()->ClearPagesIfNeeded(base::Bind(
235 &OfflinePageStorageManagerTest::OnPagesCleared, base::Unretained(this)));
236 }
237
238 void OfflinePageStorageManagerTest::TearDown() {
239 manager_.reset();
240 model_.reset();
241 }
242
243 void OfflinePageStorageManagerTest::OnPagesCleared(size_t pages_cleared_count,
244 ClearStorageResult result) {
245 last_cleared_page_count_ = pages_cleared_count;
246 total_cleared_times_++;
247 last_clear_storage_result_ = result;
248 }
249
250 TEST_F(OfflinePageStorageManagerTest, TestClearPagesLessThanLimit) {
251 Initialize(std::vector<PageSettings>(
252 {{kBookmarkNamespace, 1, 1}, {kLastNNamespace, 1, 1}}));
253 clock()->Advance(base::TimeDelta::FromMinutes(30));
254 TryClearPages();
255 EXPECT_EQ(2, last_cleared_page_count());
256 EXPECT_EQ(1, total_cleared_times());
257 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result());
258 EXPECT_EQ(2, static_cast<int>(model()->GetRemovedPages().size()));
259 }
260
261 TEST_F(OfflinePageStorageManagerTest, TestClearPagesMoreThanLimit) {
262 Initialize(std::vector<PageSettings>(
263 {{kBookmarkNamespace, 10, 15}, {kLastNNamespace, 5, 30}}));
264 clock()->Advance(base::TimeDelta::FromMinutes(30));
265 TryClearPages();
266 EXPECT_EQ(45, last_cleared_page_count());
267 EXPECT_EQ(1, total_cleared_times());
268 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result());
269 EXPECT_EQ(45, static_cast<int>(model()->GetRemovedPages().size()));
270 }
271
272 TEST_F(OfflinePageStorageManagerTest, TestClearPagesMoreFreshPages) {
273 Initialize(std::vector<PageSettings>(
274 {{kBookmarkNamespace, 30, 0}, {kLastNNamespace, 100, 1}}),
275 {1000 * (1 << 20), 0});
276 clock()->Advance(base::TimeDelta::FromMinutes(30));
277 TryClearPages();
278 EXPECT_EQ(1, last_cleared_page_count());
279 EXPECT_EQ(1, total_cleared_times());
280 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result());
281 EXPECT_EQ(1, static_cast<int>(model()->GetRemovedPages().size()));
282 }
283
284 TEST_F(OfflinePageStorageManagerTest, TestDeleteAsyncPages) {
285 Initialize(std::vector<PageSettings>({{kAsyncNamespace, 20, 0}}));
286 clock()->Advance(base::TimeDelta::FromDays(367));
287 TryClearPages();
288 EXPECT_EQ(0, last_cleared_page_count());
289 EXPECT_EQ(1, total_cleared_times());
290 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result());
291 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size()));
292 }
293
294 TEST_F(OfflinePageStorageManagerTest, TestDeletionFailed) {
295 Initialize(std::vector<PageSettings>(
296 {{kBookmarkNamespace, 10, 10}, {kLastNNamespace, 10, 10}}),
297 {kFreeSpaceNormal, 0}, TestOptions::DELETE_FAILURE);
298 TryClearPages();
299 EXPECT_EQ(20, last_cleared_page_count());
300 EXPECT_EQ(1, total_cleared_times());
301 EXPECT_EQ(ClearStorageResult::DELETE_FAILURE, last_clear_storage_result());
302 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size()));
303 }
304
305 TEST_F(OfflinePageStorageManagerTest, TestStorageTimeInterval) {
306 Initialize(std::vector<PageSettings>(
307 {{kBookmarkNamespace, 10, 10}, {kLastNNamespace, 10, 10}}));
308 clock()->Advance(base::TimeDelta::FromMinutes(30));
309 TryClearPages();
310 EXPECT_EQ(20, last_cleared_page_count());
311 EXPECT_EQ(1, total_cleared_times());
312 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result());
313 EXPECT_EQ(20, static_cast<int>(model()->GetRemovedPages().size()));
314
315 // Advance clock so we go over the gap, but no expired pages.
316 clock()->Advance(constants::kClearStorageInterval +
317 base::TimeDelta::FromMinutes(1));
318 TryClearPages();
319 EXPECT_EQ(0, last_cleared_page_count());
320 EXPECT_EQ(2, total_cleared_times());
321 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result());
322 EXPECT_EQ(20, static_cast<int>(model()->GetRemovedPages().size()));
323
324 // Advance clock so we are still in the gap, should be unnecessary.
325 clock()->Advance(constants::kClearStorageInterval -
326 base::TimeDelta::FromMinutes(1));
327 TryClearPages();
328 EXPECT_EQ(0, last_cleared_page_count());
329 EXPECT_EQ(3, total_cleared_times());
330 EXPECT_EQ(ClearStorageResult::UNNECESSARY, last_clear_storage_result());
331 EXPECT_EQ(20, static_cast<int>(model()->GetRemovedPages().size()));
332 }
333
334 TEST_F(OfflinePageStorageManagerTest, TestClearMultipleTimes) {
335 Initialize(std::vector<PageSettings>({{kBookmarkNamespace, 30, 0},
336 {kLastNNamespace, 100, 1},
337 {kAsyncNamespace, 40, 0}}),
338 {1000 * (1 << 20), 0});
339 clock()->Advance(base::TimeDelta::FromMinutes(30));
340 LifetimePolicy policy =
341 policy_controller()->GetPolicy(kLastNNamespace).lifetime_policy;
342
343 TryClearPages();
344 EXPECT_EQ(1, last_cleared_page_count());
345 EXPECT_EQ(1, total_cleared_times());
346 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result());
347 EXPECT_EQ(1, static_cast<int>(model()->GetRemovedPages().size()));
348
349 // Advance the clock by expiration period of last_n namespace, should be
350 // expiring all pages left in the namespace.
351 clock()->Advance(policy.expiration_period);
352 TryClearPages();
353 EXPECT_EQ(100, last_cleared_page_count());
354 EXPECT_EQ(2, total_cleared_times());
355 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result());
356 EXPECT_EQ(101, static_cast<int>(model()->GetRemovedPages().size()));
357
358 // Only 1 ms passes and no changes in pages, so no need to clear page.
359 clock()->Advance(base::TimeDelta::FromMilliseconds(1));
360 TryClearPages();
361 EXPECT_EQ(0, last_cleared_page_count());
362 EXPECT_EQ(3, total_cleared_times());
363 EXPECT_EQ(ClearStorageResult::UNNECESSARY, last_clear_storage_result());
364 EXPECT_EQ(101, static_cast<int>(model()->GetRemovedPages().size()));
365
366 // Adding more fresh pages to make it go over limit.
367 clock()->Advance(base::TimeDelta::FromMinutes(5));
368 model()->AddPages({kBookmarkNamespace, 400, 0});
369 int64_t total_size_before = model()->GetTotalSize();
370 int64_t available_space = 300 * (1 << 20); // 300 MB
371 test_archive_manager()->SetValues({available_space, total_size_before});
372 EXPECT_GE(total_size_before, constants::kOfflinePageStorageLimit *
373 (available_space + total_size_before));
374 TryClearPages();
375 EXPECT_LE(total_size_before * constants::kOfflinePageStorageClearThreshold,
376 model()->GetTotalSize());
377 EXPECT_EQ(4, total_cleared_times());
378 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result());
379 int deleted_page_total = last_cleared_page_count() + 101;
380 EXPECT_EQ(deleted_page_total,
381 static_cast<int>(model()->GetRemovedPages().size()));
382
383 // After more days, all pages should be deleted.
384 clock()->Advance(base::TimeDelta::FromDays(30));
385 TryClearPages();
386 EXPECT_EQ(0, model()->GetTotalSize());
387 EXPECT_EQ(5, total_cleared_times());
388 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result());
389 // Number of removed pages should be all the pages initially created plus 400
390 // more pages added above for bookmark namespace.
391 EXPECT_EQ(171 + 400, static_cast<int>(model()->GetRemovedPages().size()));
392 }
393
394 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_storage_manager.cc ('k') | components/offline_pages/offline_page_test_archiver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698