| 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_impl.h" | 5 #include "components/offline_pages/offline_page_model_impl.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 20 matching lines...) Expand all Loading... |
| 31 #include "components/offline_pages/offline_page_storage_manager.h" | 31 #include "components/offline_pages/offline_page_storage_manager.h" |
| 32 #include "components/offline_pages/offline_page_test_archiver.h" | 32 #include "components/offline_pages/offline_page_test_archiver.h" |
| 33 #include "components/offline_pages/offline_page_test_store.h" | 33 #include "components/offline_pages/offline_page_test_store.h" |
| 34 #include "components/offline_pages/offline_page_types.h" | 34 #include "components/offline_pages/offline_page_types.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 36 #include "url/gurl.h" | 36 #include "url/gurl.h" |
| 37 | 37 |
| 38 namespace offline_pages { | 38 namespace offline_pages { |
| 39 | 39 |
| 40 namespace { | 40 namespace { |
| 41 const char kTestClientNamespace[] = "CLIENT_NAMESPACE"; | 41 const char kTestClientNamespace[] = "default"; |
| 42 const char kUserRequestedNamespace[] = "download"; | 42 const char kUserRequestedNamespace[] = "download"; |
| 43 const GURL kTestUrl("http://example.com"); | 43 const GURL kTestUrl("http://example.com"); |
| 44 const GURL kTestUrl2("http://other.page.com"); | 44 const GURL kTestUrl2("http://other.page.com"); |
| 45 const GURL kTestUrl3("http://test.xyz"); | 45 const GURL kTestUrl3("http://test.xyz"); |
| 46 const GURL kTestUrl4("http://page.net"); | 46 const GURL kTestUrl4("http://page.net"); |
| 47 const GURL kFileUrl("file:///foo"); | 47 const GURL kFileUrl("file:///foo"); |
| 48 const GURL kTestUrlWithFragment("http://example.com#frag"); | 48 const GURL kTestUrlWithFragment("http://example.com#frag"); |
| 49 const GURL kTestUrl2WithFragment("http://other.page.com#frag"); | 49 const GURL kTestUrl2WithFragment("http://other.page.com#frag"); |
| 50 const GURL kTestUrl2WithFragment2("http://other.page.com#frag2"); | 50 const GURL kTestUrl2WithFragment2("http://other.page.com#frag2"); |
| 51 const ClientId kTestClientId1(kTestClientNamespace, "1234"); | 51 const ClientId kTestClientId1(kTestClientNamespace, "1234"); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 82 | 82 |
| 83 // OfflinePageTestArchiver::Observer implementation. | 83 // OfflinePageTestArchiver::Observer implementation. |
| 84 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; | 84 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; |
| 85 | 85 |
| 86 // OfflinePageModel callbacks. | 86 // OfflinePageModel callbacks. |
| 87 void OnSavePageDone(SavePageResult result, int64_t offline_id); | 87 void OnSavePageDone(SavePageResult result, int64_t offline_id); |
| 88 void OnDeletePageDone(DeletePageResult result); | 88 void OnDeletePageDone(DeletePageResult result); |
| 89 void OnCheckPagesExistOfflineDone(const CheckPagesExistOfflineResult& result); | 89 void OnCheckPagesExistOfflineDone(const CheckPagesExistOfflineResult& result); |
| 90 void OnGetOfflineIdsForClientIdDone(MultipleOfflineIdResult* storage, | 90 void OnGetOfflineIdsForClientIdDone(MultipleOfflineIdResult* storage, |
| 91 const MultipleOfflineIdResult& result); | 91 const MultipleOfflineIdResult& result); |
| 92 void OnGetSingleOfflinePageItemResult(const OfflinePageItem** storage, | 92 void OnGetSingleOfflinePageItemResult( |
| 93 const OfflinePageItem* result); | 93 std::unique_ptr<OfflinePageItem>* storage, |
| 94 const OfflinePageItem* result); |
| 94 void OnGetMultipleOfflinePageItemsResult( | 95 void OnGetMultipleOfflinePageItemsResult( |
| 95 MultipleOfflinePageItemResult* storage, | 96 MultipleOfflinePageItemResult* storage, |
| 96 const MultipleOfflinePageItemResult& result); | 97 const MultipleOfflinePageItemResult& result); |
| 97 void OnPagesExpired(bool result); | 98 void OnPagesExpired(bool result); |
| 98 | 99 |
| 99 // OfflinePageMetadataStore callbacks. | 100 // OfflinePageMetadataStore callbacks. |
| 100 void OnStoreUpdateDone(bool /* success */); | 101 void OnStoreUpdateDone(bool /* success */); |
| 101 | 102 |
| 102 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( | 103 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( |
| 103 const GURL& url, | 104 const GURL& url, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 offline_ids.push_back(offline_id); | 137 offline_ids.push_back(offline_id); |
| 137 model()->DeletePagesByOfflineId(offline_ids, callback); | 138 model()->DeletePagesByOfflineId(offline_ids, callback); |
| 138 } | 139 } |
| 139 | 140 |
| 140 bool HasPages(std::string name_space); | 141 bool HasPages(std::string name_space); |
| 141 | 142 |
| 142 CheckPagesExistOfflineResult CheckPagesExistOffline(std::set<GURL>); | 143 CheckPagesExistOfflineResult CheckPagesExistOffline(std::set<GURL>); |
| 143 | 144 |
| 144 MultipleOfflineIdResult GetOfflineIdsForClientId(const ClientId& client_id); | 145 MultipleOfflineIdResult GetOfflineIdsForClientId(const ClientId& client_id); |
| 145 | 146 |
| 146 const OfflinePageItem* GetPageByOfflineId(int64_t offline_id); | 147 std::unique_ptr<OfflinePageItem> GetPageByOfflineId(int64_t offline_id); |
| 147 | 148 |
| 148 MultipleOfflinePageItemResult GetPagesByOnlineURL(const GURL& online_url); | 149 MultipleOfflinePageItemResult GetPagesByOnlineURL(const GURL& online_url); |
| 149 | 150 |
| 150 OfflinePageModelImpl* model() { return model_.get(); } | 151 OfflinePageModelImpl* model() { return model_.get(); } |
| 151 | 152 |
| 152 int64_t last_save_offline_id() const { return last_save_offline_id_; } | 153 int64_t last_save_offline_id() const { return last_save_offline_id_; } |
| 153 | 154 |
| 154 SavePageResult last_save_result() const { return last_save_result_; } | 155 SavePageResult last_save_result() const { return last_save_result_; } |
| 155 | 156 |
| 156 DeletePageResult last_delete_result() const { return last_delete_result_; } | 157 DeletePageResult last_delete_result() const { return last_delete_result_; } |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 PumpLoop(); | 372 PumpLoop(); |
| 372 return result; | 373 return result; |
| 373 } | 374 } |
| 374 | 375 |
| 375 void OfflinePageModelImplTest::OnGetOfflineIdsForClientIdDone( | 376 void OfflinePageModelImplTest::OnGetOfflineIdsForClientIdDone( |
| 376 MultipleOfflineIdResult* storage, | 377 MultipleOfflineIdResult* storage, |
| 377 const MultipleOfflineIdResult& result) { | 378 const MultipleOfflineIdResult& result) { |
| 378 *storage = result; | 379 *storage = result; |
| 379 } | 380 } |
| 380 | 381 |
| 381 const OfflinePageItem* OfflinePageModelImplTest::GetPageByOfflineId( | 382 std::unique_ptr<OfflinePageItem> OfflinePageModelImplTest::GetPageByOfflineId( |
| 382 int64_t offline_id) { | 383 int64_t offline_id) { |
| 383 const OfflinePageItem* result = nullptr; | 384 std::unique_ptr<OfflinePageItem> result = nullptr; |
| 384 model()->GetPageByOfflineId( | 385 model()->GetPageByOfflineId( |
| 385 offline_id, | 386 offline_id, |
| 386 base::Bind(&OfflinePageModelImplTest::OnGetSingleOfflinePageItemResult, | 387 base::Bind(&OfflinePageModelImplTest::OnGetSingleOfflinePageItemResult, |
| 387 AsWeakPtr(), base::Unretained(&result))); | 388 AsWeakPtr(), base::Unretained(&result))); |
| 388 PumpLoop(); | 389 PumpLoop(); |
| 389 return result; | 390 return result; |
| 390 } | 391 } |
| 391 | 392 |
| 392 void OfflinePageModelImplTest::OnGetSingleOfflinePageItemResult( | 393 void OfflinePageModelImplTest::OnGetSingleOfflinePageItemResult( |
| 393 const OfflinePageItem** storage, | 394 std::unique_ptr<OfflinePageItem>* storage, |
| 394 const OfflinePageItem* result) { | 395 const OfflinePageItem* result) { |
| 395 *storage = result; | 396 if (result == nullptr) { |
| 397 storage->reset(nullptr); |
| 398 return; |
| 399 } |
| 400 |
| 401 *storage = base::MakeUnique<OfflinePageItem>(*result); |
| 396 } | 402 } |
| 397 | 403 |
| 398 void OfflinePageModelImplTest::OnGetMultipleOfflinePageItemsResult( | 404 void OfflinePageModelImplTest::OnGetMultipleOfflinePageItemsResult( |
| 399 MultipleOfflinePageItemResult* storage, | 405 MultipleOfflinePageItemResult* storage, |
| 400 const MultipleOfflinePageItemResult& result) { | 406 const MultipleOfflinePageItemResult& result) { |
| 401 *storage = result; | 407 *storage = result; |
| 402 } | 408 } |
| 403 | 409 |
| 404 void OfflinePageModelImplTest::OnPagesExpired(bool result) { | 410 void OfflinePageModelImplTest::OnPagesExpired(bool result) { |
| 405 last_expire_page_result_ = result; | 411 last_expire_page_result_ = result; |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 EXPECT_EQ(DeletePageResult::STORE_FAILURE, last_delete_result()); | 774 EXPECT_EQ(DeletePageResult::STORE_FAILURE, last_delete_result()); |
| 769 } | 775 } |
| 770 | 776 |
| 771 TEST_F(OfflinePageModelImplTest, DetectThatOfflineCopyIsMissing) { | 777 TEST_F(OfflinePageModelImplTest, DetectThatOfflineCopyIsMissing) { |
| 772 // Save a page. | 778 // Save a page. |
| 773 SavePage(kTestUrl, kTestClientId1); | 779 SavePage(kTestUrl, kTestClientId1); |
| 774 int64_t offline_id = last_save_offline_id(); | 780 int64_t offline_id = last_save_offline_id(); |
| 775 | 781 |
| 776 ResetResults(); | 782 ResetResults(); |
| 777 | 783 |
| 778 const OfflinePageItem* page = GetPageByOfflineId(offline_id); | 784 std::unique_ptr<OfflinePageItem> page = GetPageByOfflineId(offline_id); |
| 779 | 785 |
| 780 // Delete the offline copy of the page. | 786 // Delete the offline copy of the page. |
| 781 base::DeleteFile(page->file_path, false); | 787 base::DeleteFile(page->file_path, false); |
| 782 | 788 |
| 783 // Resetting the model will cause a consistency check. | 789 // Resetting the model will cause a consistency check. |
| 784 ResetModel(); | 790 ResetModel(); |
| 785 | 791 |
| 786 PumpLoop(); | 792 PumpLoop(); |
| 787 | 793 |
| 788 // Check if the page has been expired. | 794 // Check if the page has been expired. |
| 789 EXPECT_EQ(0UL, GetAllPages().size()); | 795 EXPECT_EQ(0UL, GetAllPages().size()); |
| 790 } | 796 } |
| 791 | 797 |
| 792 TEST_F(OfflinePageModelImplTest, DetectThatOfflineCopyIsMissingAfterLoad) { | 798 TEST_F(OfflinePageModelImplTest, DetectThatOfflineCopyIsMissingAfterLoad) { |
| 793 // Save a page. | 799 // Save a page. |
| 794 SavePage(kTestUrl, kTestClientId1); | 800 SavePage(kTestUrl, kTestClientId1); |
| 795 PumpLoop(); | 801 PumpLoop(); |
| 796 int64_t offline_id = last_save_offline_id(); | 802 int64_t offline_id = last_save_offline_id(); |
| 797 | 803 |
| 798 ResetResults(); | 804 ResetResults(); |
| 799 | 805 |
| 800 const OfflinePageItem* page = GetPageByOfflineId(offline_id); | 806 std::unique_ptr<OfflinePageItem> page = GetPageByOfflineId(offline_id); |
| 801 // Delete the offline copy of the page and check the metadata. | 807 // Delete the offline copy of the page and check the metadata. |
| 802 base::DeleteFile(page->file_path, false); | 808 base::DeleteFile(page->file_path, false); |
| 803 // Reseting the model should trigger the metadata consistency check as well. | 809 // Reseting the model should trigger the metadata consistency check as well. |
| 804 ResetModel(); | 810 ResetModel(); |
| 805 PumpLoop(); | 811 PumpLoop(); |
| 806 | 812 |
| 807 // Check if the page has been expired. | 813 // Check if the page has been expired. |
| 808 EXPECT_EQ(0UL, GetAllPages().size()); | 814 EXPECT_EQ(0UL, GetAllPages().size()); |
| 809 } | 815 } |
| 810 | 816 |
| 811 TEST_F(OfflinePageModelImplTest, DetectThatHeadlessPageIsDeleted) { | 817 TEST_F(OfflinePageModelImplTest, DetectThatHeadlessPageIsDeleted) { |
| 812 // Save a page. | 818 // Save a page. |
| 813 SavePage(kTestUrl, kTestClientId1); | 819 SavePage(kTestUrl, kTestClientId1); |
| 814 PumpLoop(); | 820 PumpLoop(); |
| 815 int64_t offline_id = last_save_offline_id(); | 821 int64_t offline_id = last_save_offline_id(); |
| 816 | 822 |
| 817 ResetResults(); | 823 ResetResults(); |
| 818 const OfflinePageItem* page = GetPageByOfflineId(offline_id); | 824 std::unique_ptr<OfflinePageItem> page = GetPageByOfflineId(offline_id); |
| 819 base::FilePath path = page->file_path; | 825 base::FilePath path = page->file_path; |
| 820 EXPECT_TRUE(base::PathExists(path)); | 826 EXPECT_TRUE(base::PathExists(path)); |
| 821 GetStore()->ClearAllPages(); | 827 GetStore()->ClearAllPages(); |
| 822 | 828 |
| 823 EXPECT_TRUE(base::PathExists(path)); | 829 EXPECT_TRUE(base::PathExists(path)); |
| 824 // Since we've manually changed the store, we have to reload the model to | 830 // Since we've manually changed the store, we have to reload the model to |
| 825 // actually refresh the in-memory copy in model. Otherwise GetAllPages() would | 831 // actually refresh the in-memory copy in model. Otherwise GetAllPages() would |
| 826 // still have the page we saved above. | 832 // still have the page we saved above. |
| 827 ResetModel(); | 833 ResetModel(); |
| 828 PumpLoop(); | 834 PumpLoop(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 874 EXPECT_EQ(DeletePageResult::SUCCESS, last_delete_result()); | 880 EXPECT_EQ(DeletePageResult::SUCCESS, last_delete_result()); |
| 875 EXPECT_EQ(1u, store->GetAllPages().size()); | 881 EXPECT_EQ(1u, store->GetAllPages().size()); |
| 876 } | 882 } |
| 877 | 883 |
| 878 TEST_F(OfflinePageModelImplTest, GetPageByOfflineId) { | 884 TEST_F(OfflinePageModelImplTest, GetPageByOfflineId) { |
| 879 SavePage(kTestUrl, kTestClientId1); | 885 SavePage(kTestUrl, kTestClientId1); |
| 880 int64_t offline1 = last_save_offline_id(); | 886 int64_t offline1 = last_save_offline_id(); |
| 881 SavePage(kTestUrl2, kTestClientId2); | 887 SavePage(kTestUrl2, kTestClientId2); |
| 882 int64_t offline2 = last_save_offline_id(); | 888 int64_t offline2 = last_save_offline_id(); |
| 883 | 889 |
| 884 const OfflinePageItem* page = GetPageByOfflineId(offline1); | 890 std::unique_ptr<OfflinePageItem> page = GetPageByOfflineId(offline1); |
| 885 EXPECT_TRUE(page); | 891 ASSERT_NE(nullptr, page); |
| 886 EXPECT_EQ(kTestUrl, page->url); | 892 EXPECT_EQ(kTestUrl, page->url); |
| 887 EXPECT_EQ(kTestClientId1, page->client_id); | 893 EXPECT_EQ(kTestClientId1, page->client_id); |
| 888 EXPECT_EQ(kTestFileSize, page->file_size); | 894 EXPECT_EQ(kTestFileSize, page->file_size); |
| 889 | 895 |
| 890 page = GetPageByOfflineId(offline2); | 896 page = GetPageByOfflineId(offline2); |
| 891 EXPECT_TRUE(page); | 897 ASSERT_NE(nullptr, page); |
| 892 EXPECT_EQ(kTestUrl2, page->url); | 898 EXPECT_EQ(kTestUrl2, page->url); |
| 893 EXPECT_EQ(kTestClientId2, page->client_id); | 899 EXPECT_EQ(kTestClientId2, page->client_id); |
| 894 EXPECT_EQ(kTestFileSize, page->file_size); | 900 EXPECT_EQ(kTestFileSize, page->file_size); |
| 895 | 901 |
| 896 page = GetPageByOfflineId(-42); | 902 page = GetPageByOfflineId(-42); |
| 897 EXPECT_FALSE(page); | 903 ASSERT_EQ(nullptr, page); |
| 898 } | 904 } |
| 899 | 905 |
| 900 TEST_F(OfflinePageModelImplTest, GetPagesByOnlineURL) { | 906 TEST_F(OfflinePageModelImplTest, GetPagesByOnlineURL) { |
| 901 SavePage(kTestUrl, kTestClientId1); | 907 SavePage(kTestUrl, kTestClientId1); |
| 902 SavePage(kTestUrl2, kTestClientId2); | 908 SavePage(kTestUrl2, kTestClientId2); |
| 903 | 909 |
| 904 MultipleOfflinePageItemResult pages = GetPagesByOnlineURL(kTestUrl2); | 910 MultipleOfflinePageItemResult pages = GetPagesByOnlineURL(kTestUrl2); |
| 905 EXPECT_EQ(1U, pages.size()); | 911 EXPECT_EQ(1U, pages.size()); |
| 906 EXPECT_EQ(kTestUrl2, pages[0].url); | 912 EXPECT_EQ(kTestUrl2, pages[0].url); |
| 907 EXPECT_EQ(kTestClientId2, pages[0].client_id); | 913 EXPECT_EQ(kTestClientId2, pages[0].client_id); |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1238 // Check if feature is correctly enabled by command-line flag. | 1244 // Check if feature is correctly enabled by command-line flag. |
| 1239 scoped_feature_list.reset(new base::test::ScopedFeatureList); | 1245 scoped_feature_list.reset(new base::test::ScopedFeatureList); |
| 1240 scoped_feature_list->InitFromCommandLine( | 1246 scoped_feature_list->InitFromCommandLine( |
| 1241 std::string(kOfflineBookmarksFeature.name) + "," + | 1247 std::string(kOfflineBookmarksFeature.name) + "," + |
| 1242 kOfflinePagesSharingFeature.name, | 1248 kOfflinePagesSharingFeature.name, |
| 1243 ""); | 1249 ""); |
| 1244 EXPECT_TRUE(offline_pages::IsOfflinePagesSharingEnabled()); | 1250 EXPECT_TRUE(offline_pages::IsOfflinePagesSharingEnabled()); |
| 1245 } | 1251 } |
| 1246 | 1252 |
| 1247 } // namespace offline_pages | 1253 } // namespace offline_pages |
| OLD | NEW |