| 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 |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 Initialize(std::vector<PageSettings>( | 345 Initialize(std::vector<PageSettings>( |
| 346 {{kBookmarkNamespace, 10, 10}, {kLastNNamespace, 10, 10}})); | 346 {{kBookmarkNamespace, 10, 10}, {kLastNNamespace, 10, 10}})); |
| 347 clock()->Advance(base::TimeDelta::FromMinutes(30)); | 347 clock()->Advance(base::TimeDelta::FromMinutes(30)); |
| 348 TryClearPages(); | 348 TryClearPages(); |
| 349 EXPECT_EQ(20, last_cleared_page_count()); | 349 EXPECT_EQ(20, last_cleared_page_count()); |
| 350 EXPECT_EQ(1, total_cleared_times()); | 350 EXPECT_EQ(1, total_cleared_times()); |
| 351 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 351 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 352 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); | 352 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 353 | 353 |
| 354 // Advance clock so we go over the gap, but no expired pages. | 354 // Advance clock so we go over the gap, but no expired pages. |
| 355 clock()->Advance(kClearStorageInterval + base::TimeDelta::FromMinutes(1)); | 355 clock()->Advance(constants::kClearStorageInterval + |
| 356 base::TimeDelta::FromMinutes(1)); |
| 356 TryClearPages(); | 357 TryClearPages(); |
| 357 EXPECT_EQ(0, last_cleared_page_count()); | 358 EXPECT_EQ(0, last_cleared_page_count()); |
| 358 EXPECT_EQ(2, total_cleared_times()); | 359 EXPECT_EQ(2, total_cleared_times()); |
| 359 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 360 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 360 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); | 361 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 361 | 362 |
| 362 // Advance clock so we are still in the gap, should be unnecessary. | 363 // Advance clock so we are still in the gap, should be unnecessary. |
| 363 clock()->Advance(kClearStorageInterval - base::TimeDelta::FromMinutes(1)); | 364 clock()->Advance(constants::kClearStorageInterval - |
| 365 base::TimeDelta::FromMinutes(1)); |
| 364 TryClearPages(); | 366 TryClearPages(); |
| 365 EXPECT_EQ(0, last_cleared_page_count()); | 367 EXPECT_EQ(0, last_cleared_page_count()); |
| 366 EXPECT_EQ(3, total_cleared_times()); | 368 EXPECT_EQ(3, total_cleared_times()); |
| 367 EXPECT_EQ(ClearStorageResult::UNNECESSARY, last_clear_storage_result()); | 369 EXPECT_EQ(ClearStorageResult::UNNECESSARY, last_clear_storage_result()); |
| 368 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); | 370 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 369 } | 371 } |
| 370 | 372 |
| 371 TEST_F(OfflinePageStorageManagerTest, TestTwoStepExpiration) { | 373 TEST_F(OfflinePageStorageManagerTest, TestTwoStepExpiration) { |
| 372 Initialize(std::vector<PageSettings>({{kBookmarkNamespace, 10, 10}})); | 374 Initialize(std::vector<PageSettings>({{kBookmarkNamespace, 10, 10}})); |
| 373 clock()->Advance(base::TimeDelta::FromMinutes(30)); | 375 clock()->Advance(base::TimeDelta::FromMinutes(30)); |
| 374 TryClearPages(); | 376 TryClearPages(); |
| 375 EXPECT_EQ(10, last_cleared_page_count()); | 377 EXPECT_EQ(10, last_cleared_page_count()); |
| 376 EXPECT_EQ(1, total_cleared_times()); | 378 EXPECT_EQ(1, total_cleared_times()); |
| 377 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 379 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 378 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); | 380 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 379 | 381 |
| 380 clock()->Advance(kRemovePageItemInterval + base::TimeDelta::FromDays(1)); | 382 clock()->Advance(constants::kRemovePageItemInterval + |
| 383 base::TimeDelta::FromDays(1)); |
| 381 TryClearPages(); | 384 TryClearPages(); |
| 382 EXPECT_EQ(10, last_cleared_page_count()); | 385 EXPECT_EQ(10, last_cleared_page_count()); |
| 383 EXPECT_EQ(2, total_cleared_times()); | 386 EXPECT_EQ(2, total_cleared_times()); |
| 384 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 387 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 385 EXPECT_EQ(10, static_cast<int>(model()->GetRemovedPages().size())); | 388 EXPECT_EQ(10, static_cast<int>(model()->GetRemovedPages().size())); |
| 386 } | 389 } |
| 387 | 390 |
| 388 TEST_F(OfflinePageStorageManagerTest, TestClearMultipleTimes) { | 391 TEST_F(OfflinePageStorageManagerTest, TestClearMultipleTimes) { |
| 389 Initialize(std::vector<PageSettings>({{kBookmarkNamespace, 30, 0}, | 392 Initialize(std::vector<PageSettings>({{kBookmarkNamespace, 30, 0}, |
| 390 {kLastNNamespace, 100, 1}, | 393 {kLastNNamespace, 100, 1}, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 416 EXPECT_EQ(3, total_cleared_times()); | 419 EXPECT_EQ(3, total_cleared_times()); |
| 417 EXPECT_EQ(ClearStorageResult::UNNECESSARY, last_clear_storage_result()); | 420 EXPECT_EQ(ClearStorageResult::UNNECESSARY, last_clear_storage_result()); |
| 418 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); | 421 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 419 | 422 |
| 420 // Adding more fresh pages to make it go over limit. | 423 // Adding more fresh pages to make it go over limit. |
| 421 clock()->Advance(base::TimeDelta::FromMinutes(5)); | 424 clock()->Advance(base::TimeDelta::FromMinutes(5)); |
| 422 model()->AddPages({kBookmarkNamespace, 400, 0}); | 425 model()->AddPages({kBookmarkNamespace, 400, 0}); |
| 423 int64_t total_size_before = model()->GetTotalSize(); | 426 int64_t total_size_before = model()->GetTotalSize(); |
| 424 int64_t available_space = 300 * (1 << 20); // 300 MB | 427 int64_t available_space = 300 * (1 << 20); // 300 MB |
| 425 test_archive_manager()->SetValues({available_space, total_size_before}); | 428 test_archive_manager()->SetValues({available_space, total_size_before}); |
| 426 EXPECT_GE(total_size_before, | 429 EXPECT_GE(total_size_before, constants::kOfflinePageStorageLimit * |
| 427 kOfflinePageStorageLimit * (available_space + total_size_before)); | 430 (available_space + total_size_before)); |
| 428 TryClearPages(); | 431 TryClearPages(); |
| 429 EXPECT_LE(total_size_before * kOfflinePageStorageClearThreshold, | 432 EXPECT_LE(total_size_before * constants::kOfflinePageStorageClearThreshold, |
| 430 model()->GetTotalSize()); | 433 model()->GetTotalSize()); |
| 431 EXPECT_EQ(4, total_cleared_times()); | 434 EXPECT_EQ(4, total_cleared_times()); |
| 432 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 435 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 433 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); | 436 EXPECT_EQ(0, static_cast<int>(model()->GetRemovedPages().size())); |
| 434 int expired_page_count = last_cleared_page_count(); | 437 int expired_page_count = last_cleared_page_count(); |
| 435 | 438 |
| 436 // After more days, all pages should be expired and . | 439 // After more days, all pages should be expired and . |
| 437 clock()->Advance(kRemovePageItemInterval + base::TimeDelta::FromDays(1)); | 440 clock()->Advance(constants::kRemovePageItemInterval + |
| 441 base::TimeDelta::FromDays(1)); |
| 438 TryClearPages(); | 442 TryClearPages(); |
| 439 EXPECT_EQ(0, model()->GetTotalSize()); | 443 EXPECT_EQ(0, model()->GetTotalSize()); |
| 440 EXPECT_EQ(5, total_cleared_times()); | 444 EXPECT_EQ(5, total_cleared_times()); |
| 441 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); | 445 EXPECT_EQ(ClearStorageResult::SUCCESS, last_clear_storage_result()); |
| 442 // Number of removed pages should be the ones expired above and all the pages | 446 // Number of removed pages should be the ones expired above and all the pages |
| 443 // initially created for last_n namespace. | 447 // initially created for last_n namespace. |
| 444 EXPECT_EQ(expired_page_count + 101, | 448 EXPECT_EQ(expired_page_count + 101, |
| 445 static_cast<int>(model()->GetRemovedPages().size())); | 449 static_cast<int>(model()->GetRemovedPages().size())); |
| 446 } | 450 } |
| 447 | 451 |
| 448 } // namespace offline_pages | 452 } // namespace offline_pages |
| OLD | NEW |