| 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 void PumpLoop(); | 116 void PumpLoop(); |
| 117 // Fast-forwards virtual time by |delta|, causing tasks with a remaining | 117 // Fast-forwards virtual time by |delta|, causing tasks with a remaining |
| 118 // delay less than or equal to |delta| to be executed. | 118 // delay less than or equal to |delta| to be executed. |
| 119 void FastForwardBy(base::TimeDelta delta); | 119 void FastForwardBy(base::TimeDelta delta); |
| 120 void ResetResults(); | 120 void ResetResults(); |
| 121 | 121 |
| 122 OfflinePageTestStore* GetStore(); | 122 OfflinePageTestStore* GetStore(); |
| 123 | 123 |
| 124 MultipleOfflinePageItemResult GetAllPages(); | 124 MultipleOfflinePageItemResult GetAllPages(); |
| 125 | 125 |
| 126 void SavePage(const GURL& url, ClientId client_id); | 126 // Returns the offline ID of the saved page. |
| 127 std::pair<SavePageResult, int64_t> SavePage(const GURL& url, |
| 128 ClientId client_id); |
| 127 | 129 |
| 128 void SavePageWithArchiverResult(const GURL& url, | 130 void SavePageWithArchiverResult(const GURL& url, |
| 129 ClientId client_id, | 131 ClientId client_id, |
| 130 OfflinePageArchiver::ArchiverResult result); | 132 OfflinePageArchiver::ArchiverResult result); |
| 131 | 133 |
| 132 void DeletePage(int64_t offline_id, | 134 void DeletePage(int64_t offline_id, |
| 133 const OfflinePageModel::DeletePageCallback& callback) { | 135 const OfflinePageModel::DeletePageCallback& callback) { |
| 134 std::vector<int64_t> offline_ids; | 136 std::vector<int64_t> offline_ids; |
| 135 offline_ids.push_back(offline_id); | 137 offline_ids.push_back(offline_id); |
| 136 model()->DeletePagesByOfflineId(offline_ids, callback); | 138 model()->DeletePagesByOfflineId(offline_ids, callback); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 last_delete_result_ = DeletePageResult::CANCELLED; | 303 last_delete_result_ = DeletePageResult::CANCELLED; |
| 302 last_archiver_path_.clear(); | 304 last_archiver_path_.clear(); |
| 303 last_pages_exist_result_.clear(); | 305 last_pages_exist_result_.clear(); |
| 304 last_cleared_pages_count_ = 0; | 306 last_cleared_pages_count_ = 0; |
| 305 } | 307 } |
| 306 | 308 |
| 307 OfflinePageTestStore* OfflinePageModelTest::GetStore() { | 309 OfflinePageTestStore* OfflinePageModelTest::GetStore() { |
| 308 return static_cast<OfflinePageTestStore*>(model()->GetStoreForTesting()); | 310 return static_cast<OfflinePageTestStore*>(model()->GetStoreForTesting()); |
| 309 } | 311 } |
| 310 | 312 |
| 311 void OfflinePageModelTest::SavePage(const GURL& url, ClientId client_id) { | 313 std::pair<SavePageResult, int64_t> OfflinePageModelTest::SavePage( |
| 314 const GURL& url, |
| 315 ClientId client_id) { |
| 312 SavePageWithArchiverResult( | 316 SavePageWithArchiverResult( |
| 313 url, client_id, | 317 url, client_id, |
| 314 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED); | 318 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED); |
| 319 return std::make_pair(last_save_result_, last_save_offline_id_); |
| 315 } | 320 } |
| 316 | 321 |
| 317 void OfflinePageModelTest::SavePageWithArchiverResult( | 322 void OfflinePageModelTest::SavePageWithArchiverResult( |
| 318 const GURL& url, | 323 const GURL& url, |
| 319 ClientId client_id, | 324 ClientId client_id, |
| 320 OfflinePageArchiver::ArchiverResult result) { | 325 OfflinePageArchiver::ArchiverResult result) { |
| 321 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver(url, result)); | 326 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver(url, result)); |
| 322 model()->SavePage( | 327 model()->SavePage( |
| 323 url, client_id, std::move(archiver), | 328 url, client_id, std::move(archiver), |
| 324 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr())); | 329 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr())); |
| (...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 976 PumpLoop(); | 981 PumpLoop(); |
| 977 | 982 |
| 978 std::vector<OfflinePageItem> offline_pages = GetAllPages(); | 983 std::vector<OfflinePageItem> offline_pages = GetAllPages(); |
| 979 | 984 |
| 980 EXPECT_EQ(1UL, offline_pages.size()); | 985 EXPECT_EQ(1UL, offline_pages.size()); |
| 981 EXPECT_EQ(1UL, GetStore()->GetAllPages().size()); | 986 EXPECT_EQ(1UL, GetStore()->GetAllPages().size()); |
| 982 EXPECT_EQ(2, last_cleared_pages_count()); | 987 EXPECT_EQ(2, last_cleared_pages_count()); |
| 983 EXPECT_EQ(DeletePageResult::SUCCESS, last_clear_page_result()); | 988 EXPECT_EQ(DeletePageResult::SUCCESS, last_clear_page_result()); |
| 984 } | 989 } |
| 985 | 990 |
| 991 TEST_F(OfflinePageModelTest, GetBestPage) { |
| 992 // We will save 3 pages - two for the same URL, and one for a different URL. |
| 993 // Correct behavior will pick the most recently saved page for the correct |
| 994 // URL. |
| 995 std::pair<SavePageResult, int64_t> saved_pages[3]; |
| 996 saved_pages[0] = SavePage(kTestUrl, kTestClientId1); |
| 997 saved_pages[1] = SavePage(kTestUrl, kTestClientId1); |
| 998 saved_pages[2] = SavePage(kTestUrl2, kTestClientId2); |
| 999 |
| 1000 for (const auto& save_result : saved_pages) { |
| 1001 ASSERT_EQ(OfflinePageModel::SavePageResult::SUCCESS, |
| 1002 std::get<0>(save_result)); |
| 1003 } |
| 1004 |
| 1005 const OfflinePageItem* offline_page = |
| 1006 model()->MaybeGetBestPageForOnlineURL(kTestUrl); |
| 1007 ASSERT_TRUE(nullptr != offline_page); |
| 1008 |
| 1009 EXPECT_EQ(std::get<1>(saved_pages[1]), offline_page->offline_id); |
| 1010 } |
| 1011 |
| 986 TEST(CommandLineFlagsTest, OfflineBookmarks) { | 1012 TEST(CommandLineFlagsTest, OfflineBookmarks) { |
| 987 // Disabled by default. | 1013 // Disabled by default. |
| 988 EXPECT_FALSE(offline_pages::IsOfflineBookmarksEnabled()); | 1014 EXPECT_FALSE(offline_pages::IsOfflineBookmarksEnabled()); |
| 989 | 1015 |
| 990 // Check if feature is correctly enabled by command-line flag. | 1016 // Check if feature is correctly enabled by command-line flag. |
| 991 base::FeatureList::ClearInstanceForTesting(); | 1017 base::FeatureList::ClearInstanceForTesting(); |
| 992 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | 1018 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 993 feature_list->InitializeFromCommandLine( | 1019 feature_list->InitializeFromCommandLine( |
| 994 offline_pages::kOfflineBookmarksFeature.name, ""); | 1020 offline_pages::kOfflineBookmarksFeature.name, ""); |
| 995 base::FeatureList::SetInstance(std::move(feature_list)); | 1021 base::FeatureList::SetInstance(std::move(feature_list)); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1036 std::unique_ptr<base::FeatureList> feature_list2(new base::FeatureList); | 1062 std::unique_ptr<base::FeatureList> feature_list2(new base::FeatureList); |
| 1037 feature_list2->InitializeFromCommandLine( | 1063 feature_list2->InitializeFromCommandLine( |
| 1038 std::string(offline_pages::kOfflineBookmarksFeature.name) + "," + | 1064 std::string(offline_pages::kOfflineBookmarksFeature.name) + "," + |
| 1039 offline_pages::kOfflinePagesBackgroundLoadingFeature.name, | 1065 offline_pages::kOfflinePagesBackgroundLoadingFeature.name, |
| 1040 ""); | 1066 ""); |
| 1041 base::FeatureList::SetInstance(std::move(feature_list2)); | 1067 base::FeatureList::SetInstance(std::move(feature_list2)); |
| 1042 EXPECT_TRUE(offline_pages::IsOfflinePagesBackgroundLoadingEnabled()); | 1068 EXPECT_TRUE(offline_pages::IsOfflinePagesBackgroundLoadingEnabled()); |
| 1043 } | 1069 } |
| 1044 | 1070 |
| 1045 } // namespace offline_pages | 1071 } // namespace offline_pages |
| OLD | NEW |