| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_model.h" | 5 #include "components/offline_pages/offline_page_model.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
| 23 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 24 #include "components/offline_pages/client_policy_controller.h" | 24 #include "components/offline_pages/client_policy_controller.h" |
| 25 #include "components/offline_pages/offline_page_client_policy.h" | 25 #include "components/offline_pages/offline_page_client_policy.h" |
| 26 #include "components/offline_pages/offline_page_feature.h" | 26 #include "components/offline_pages/offline_page_feature.h" |
| 27 #include "components/offline_pages/offline_page_item.h" | 27 #include "components/offline_pages/offline_page_item.h" |
| 28 #include "components/offline_pages/offline_page_storage_manager.h" | 28 #include "components/offline_pages/offline_page_storage_manager.h" |
| 29 #include "components/offline_pages/offline_page_test_archiver.h" | 29 #include "components/offline_pages/offline_page_test_archiver.h" |
| 30 #include "components/offline_pages/offline_page_test_store.h" | 30 #include "components/offline_pages/offline_page_test_store.h" |
| 31 #include "components/offline_pages/offline_page_types.h" | 31 #include "components/offline_pages/offline_page_types.h" |
| 32 #include "components/prefs/testing_pref_service.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 34 | 35 |
| 35 namespace offline_pages { | 36 namespace offline_pages { |
| 36 | 37 |
| 37 namespace { | 38 namespace { |
| 38 const char kTestClientNamespace[] = "CLIENT_NAMESPACE"; | 39 const char kTestClientNamespace[] = "CLIENT_NAMESPACE"; |
| 39 const GURL kTestUrl("http://example.com"); | 40 const GURL kTestUrl("http://example.com"); |
| 40 const GURL kTestUrl2("http://other.page.com"); | 41 const GURL kTestUrl2("http://other.page.com"); |
| 41 const GURL kTestUrl3("http://test.xyz"); | 42 const GURL kTestUrl3("http://test.xyz"); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 void OnCheckPagesExistOfflineDone(const CheckPagesExistOfflineResult& result); | 81 void OnCheckPagesExistOfflineDone(const CheckPagesExistOfflineResult& result); |
| 81 void OnClearAllDone(); | 82 void OnClearAllDone(); |
| 82 void OnGetOfflineIdsForClientIdDone(MultipleOfflineIdResult* storage, | 83 void OnGetOfflineIdsForClientIdDone(MultipleOfflineIdResult* storage, |
| 83 const MultipleOfflineIdResult& result); | 84 const MultipleOfflineIdResult& result); |
| 84 void OnGetSingleOfflinePageItemResult( | 85 void OnGetSingleOfflinePageItemResult( |
| 85 SingleOfflinePageItemResult* storage, | 86 SingleOfflinePageItemResult* storage, |
| 86 const SingleOfflinePageItemResult& result); | 87 const SingleOfflinePageItemResult& result); |
| 87 void OnGetMultipleOfflinePageItemsResult( | 88 void OnGetMultipleOfflinePageItemsResult( |
| 88 MultipleOfflinePageItemResult* storage, | 89 MultipleOfflinePageItemResult* storage, |
| 89 const MultipleOfflinePageItemResult& result); | 90 const MultipleOfflinePageItemResult& result); |
| 90 void OnClearPageByStorageManager(int pages_cleared, DeletePageResult result); | |
| 91 | 91 |
| 92 // OfflinePageMetadataStore callbacks. | 92 // OfflinePageMetadataStore callbacks. |
| 93 void OnStoreUpdateDone(bool /* success */); | 93 void OnStoreUpdateDone(bool /* success */); |
| 94 | 94 |
| 95 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( | 95 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( |
| 96 const GURL& url, | 96 const GURL& url, |
| 97 OfflinePageArchiver::ArchiverResult result); | 97 OfflinePageArchiver::ArchiverResult result); |
| 98 std::unique_ptr<OfflinePageMetadataStore> BuildStore(); | 98 std::unique_ptr<OfflinePageMetadataStore> BuildStore(); |
| 99 std::unique_ptr<OfflinePageModel> BuildModel( | 99 std::unique_ptr<OfflinePageModel> BuildModel( |
| 100 std::unique_ptr<OfflinePageMetadataStore> store); | 100 std::unique_ptr<OfflinePageMetadataStore> store); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 } | 259 } |
| 260 | 260 |
| 261 std::unique_ptr<OfflinePageMetadataStore> OfflinePageModelTest::BuildStore() { | 261 std::unique_ptr<OfflinePageMetadataStore> OfflinePageModelTest::BuildStore() { |
| 262 return std::unique_ptr<OfflinePageMetadataStore>( | 262 return std::unique_ptr<OfflinePageMetadataStore>( |
| 263 new OfflinePageTestStore(base::ThreadTaskRunnerHandle::Get())); | 263 new OfflinePageTestStore(base::ThreadTaskRunnerHandle::Get())); |
| 264 } | 264 } |
| 265 | 265 |
| 266 std::unique_ptr<OfflinePageModel> OfflinePageModelTest::BuildModel( | 266 std::unique_ptr<OfflinePageModel> OfflinePageModelTest::BuildModel( |
| 267 std::unique_ptr<OfflinePageMetadataStore> store) { | 267 std::unique_ptr<OfflinePageMetadataStore> store) { |
| 268 return std::unique_ptr<OfflinePageModel>(new OfflinePageModel( | 268 return std::unique_ptr<OfflinePageModel>(new OfflinePageModel( |
| 269 std::move(store), temp_dir_.path(), base::ThreadTaskRunnerHandle::Get())); | 269 std::move(store), temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), |
| 270 new TestingPrefServiceSimple())); |
| 270 } | 271 } |
| 271 | 272 |
| 272 void OfflinePageModelTest::ResetModel() { | 273 void OfflinePageModelTest::ResetModel() { |
| 273 model_->RemoveObserver(this); | 274 model_->RemoveObserver(this); |
| 274 OfflinePageTestStore* old_store = GetStore(); | 275 OfflinePageTestStore* old_store = GetStore(); |
| 275 std::unique_ptr<OfflinePageMetadataStore> new_store( | 276 std::unique_ptr<OfflinePageMetadataStore> new_store( |
| 276 new OfflinePageTestStore(*old_store)); | 277 new OfflinePageTestStore(*old_store)); |
| 277 model_ = BuildModel(std::move(new_store)); | 278 model_ = BuildModel(std::move(new_store)); |
| 278 model_->AddObserver(this); | 279 model_->AddObserver(this); |
| 279 PumpLoop(); | 280 PumpLoop(); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 const SingleOfflinePageItemResult& result) { | 382 const SingleOfflinePageItemResult& result) { |
| 382 *storage = result; | 383 *storage = result; |
| 383 } | 384 } |
| 384 | 385 |
| 385 void OfflinePageModelTest::OnGetMultipleOfflinePageItemsResult( | 386 void OfflinePageModelTest::OnGetMultipleOfflinePageItemsResult( |
| 386 MultipleOfflinePageItemResult* storage, | 387 MultipleOfflinePageItemResult* storage, |
| 387 const MultipleOfflinePageItemResult& result) { | 388 const MultipleOfflinePageItemResult& result) { |
| 388 *storage = result; | 389 *storage = result; |
| 389 } | 390 } |
| 390 | 391 |
| 391 void OfflinePageModelTest::OnClearPageByStorageManager( | |
| 392 int pages_cleared, | |
| 393 DeletePageResult result) { | |
| 394 last_cleared_pages_count_ = pages_cleared; | |
| 395 last_clear_page_result_ = result; | |
| 396 } | |
| 397 | |
| 398 base::Optional<OfflinePageItem> OfflinePageModelTest::GetPagesByOnlineURL( | 392 base::Optional<OfflinePageItem> OfflinePageModelTest::GetPagesByOnlineURL( |
| 399 const GURL& online_url) { | 393 const GURL& online_url) { |
| 400 MultipleOfflinePageItemResult result; | 394 MultipleOfflinePageItemResult result; |
| 401 model()->GetPagesByOnlineURL( | 395 model()->GetPagesByOnlineURL( |
| 402 online_url, | 396 online_url, |
| 403 base::Bind(&OfflinePageModelTest::OnGetMultipleOfflinePageItemsResult, | 397 base::Bind(&OfflinePageModelTest::OnGetMultipleOfflinePageItemsResult, |
| 404 AsWeakPtr(), base::Unretained(&result))); | 398 AsWeakPtr(), base::Unretained(&result))); |
| 405 PumpLoop(); | 399 PumpLoop(); |
| 406 if (result.size() > 0) | 400 if (result.size() > 0) |
| 407 return base::make_optional(result[0]); | 401 return base::make_optional(result[0]); |
| (...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 936 | 930 |
| 937 std::set<int64_t> id_set; | 931 std::set<int64_t> id_set; |
| 938 for (size_t i = 0; i < ids.size(); i++) { | 932 for (size_t i = 0; i < ids.size(); i++) { |
| 939 id_set.insert(ids[i]); | 933 id_set.insert(ids[i]); |
| 940 } | 934 } |
| 941 | 935 |
| 942 EXPECT_TRUE(id_set.find(offline1) != id_set.end()); | 936 EXPECT_TRUE(id_set.find(offline1) != id_set.end()); |
| 943 EXPECT_TRUE(id_set.find(offline2) != id_set.end()); | 937 EXPECT_TRUE(id_set.find(offline2) != id_set.end()); |
| 944 } | 938 } |
| 945 | 939 |
| 946 TEST_F(OfflinePageModelTest, ClearPagesFromOneSource) { | |
| 947 base::Time now = base::Time::Now(); | |
| 948 base::TimeDelta expiration_period = model() | |
| 949 ->GetPolicyController() | |
| 950 ->GetPolicy(kTestClientNamespace) | |
| 951 .lifetime_policy.expiration_period; | |
| 952 | |
| 953 SavePage(kTestUrl, kTestClientId1); | |
| 954 GetStore()->UpdateLastAccessTime( | |
| 955 last_save_offline_id(), | |
| 956 now - base::TimeDelta::FromDays(10) - expiration_period); | |
| 957 SavePage(kTestUrl2, kTestClientId2); | |
| 958 GetStore()->UpdateLastAccessTime( | |
| 959 last_save_offline_id(), | |
| 960 now - base::TimeDelta::FromDays(1) - expiration_period); | |
| 961 SavePage(kTestUrl3, kTestClientId3); | |
| 962 GetStore()->UpdateLastAccessTime(last_save_offline_id(), now); | |
| 963 | |
| 964 ResetModel(); | |
| 965 | |
| 966 // Only first two pages are expired. | |
| 967 model()->GetStorageManager()->ClearPagesIfNeeded(base::Bind( | |
| 968 &OfflinePageModelTest::OnClearPageByStorageManager, AsWeakPtr())); | |
| 969 | |
| 970 PumpLoop(); | |
| 971 | |
| 972 std::vector<OfflinePageItem> offline_pages = GetAllPages(); | |
| 973 | |
| 974 EXPECT_EQ(1UL, offline_pages.size()); | |
| 975 EXPECT_EQ(1UL, GetStore()->GetAllPages().size()); | |
| 976 EXPECT_EQ(2, last_cleared_pages_count()); | |
| 977 EXPECT_EQ(DeletePageResult::SUCCESS, last_clear_page_result()); | |
| 978 } | |
| 979 | |
| 980 TEST_F(OfflinePageModelTest, GetBestPage) { | 940 TEST_F(OfflinePageModelTest, GetBestPage) { |
| 981 // We will save 3 pages - two for the same URL, and one for a different URL. | 941 // We will save 3 pages - two for the same URL, and one for a different URL. |
| 982 // Correct behavior will pick the most recently saved page for the correct | 942 // Correct behavior will pick the most recently saved page for the correct |
| 983 // URL. | 943 // URL. |
| 984 std::pair<SavePageResult, int64_t> saved_pages[3]; | 944 std::pair<SavePageResult, int64_t> saved_pages[3]; |
| 985 saved_pages[0] = SavePage(kTestUrl, kTestClientId1); | 945 saved_pages[0] = SavePage(kTestUrl, kTestClientId1); |
| 986 saved_pages[1] = SavePage(kTestUrl, kTestClientId1); | 946 saved_pages[1] = SavePage(kTestUrl, kTestClientId1); |
| 987 saved_pages[2] = SavePage(kTestUrl2, kTestClientId2); | 947 saved_pages[2] = SavePage(kTestUrl2, kTestClientId2); |
| 988 | 948 |
| 989 for (const auto& save_result : saved_pages) { | 949 for (const auto& save_result : saved_pages) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1051 std::unique_ptr<base::FeatureList> feature_list2(new base::FeatureList); | 1011 std::unique_ptr<base::FeatureList> feature_list2(new base::FeatureList); |
| 1052 feature_list2->InitializeFromCommandLine( | 1012 feature_list2->InitializeFromCommandLine( |
| 1053 std::string(offline_pages::kOfflineBookmarksFeature.name) + "," + | 1013 std::string(offline_pages::kOfflineBookmarksFeature.name) + "," + |
| 1054 offline_pages::kOfflinePagesBackgroundLoadingFeature.name, | 1014 offline_pages::kOfflinePagesBackgroundLoadingFeature.name, |
| 1055 ""); | 1015 ""); |
| 1056 base::FeatureList::SetInstance(std::move(feature_list2)); | 1016 base::FeatureList::SetInstance(std::move(feature_list2)); |
| 1057 EXPECT_TRUE(offline_pages::IsOfflinePagesBackgroundLoadingEnabled()); | 1017 EXPECT_TRUE(offline_pages::IsOfflinePagesBackgroundLoadingEnabled()); |
| 1058 } | 1018 } |
| 1059 | 1019 |
| 1060 } // namespace offline_pages | 1020 } // namespace offline_pages |
| OLD | NEW |