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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); | 48 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); |
49 explicit OfflinePageTestStore(const OfflinePageTestStore& other_store); | 49 explicit OfflinePageTestStore(const OfflinePageTestStore& other_store); |
50 ~OfflinePageTestStore() override; | 50 ~OfflinePageTestStore() override; |
51 | 51 |
52 // OfflinePageMetadataStore overrides: | 52 // OfflinePageMetadataStore overrides: |
53 void Load(const LoadCallback& callback) override; | 53 void Load(const LoadCallback& callback) override; |
54 void AddOrUpdateOfflinePage(const OfflinePageItem& offline_page, | 54 void AddOrUpdateOfflinePage(const OfflinePageItem& offline_page, |
55 const UpdateCallback& callback) override; | 55 const UpdateCallback& callback) override; |
56 void RemoveOfflinePages(const std::vector<int64>& bookmark_ids, | 56 void RemoveOfflinePages(const std::vector<int64>& bookmark_ids, |
57 const UpdateCallback& callback) override; | 57 const UpdateCallback& callback) override; |
| 58 void Reset(const ResetCallback& callback) override; |
58 | 59 |
59 void UpdateLastAccessTime(int64 bookmark_id, | 60 void UpdateLastAccessTime(int64 bookmark_id, |
60 const base::Time& last_access_time); | 61 const base::Time& last_access_time); |
61 | 62 |
62 const OfflinePageItem& last_saved_page() const { return last_saved_page_; } | 63 const OfflinePageItem& last_saved_page() const { return last_saved_page_; } |
63 | 64 |
64 void set_test_scenario(TestScenario scenario) { scenario_ = scenario; }; | 65 void set_test_scenario(TestScenario scenario) { scenario_ = scenario; }; |
65 | 66 |
66 const std::vector<OfflinePageItem>& offline_pages() const { | 67 const std::vector<OfflinePageItem>& offline_pages() const { |
67 return offline_pages_; | 68 return offline_pages_; |
(...skipping 18 matching lines...) Expand all Loading... |
86 OfflinePageTestStore::OfflinePageTestStore( | 87 OfflinePageTestStore::OfflinePageTestStore( |
87 const OfflinePageTestStore& other_store) | 88 const OfflinePageTestStore& other_store) |
88 : task_runner_(other_store.task_runner_), | 89 : task_runner_(other_store.task_runner_), |
89 scenario_(other_store.scenario_), | 90 scenario_(other_store.scenario_), |
90 offline_pages_(other_store.offline_pages_) {} | 91 offline_pages_(other_store.offline_pages_) {} |
91 | 92 |
92 OfflinePageTestStore::~OfflinePageTestStore() { | 93 OfflinePageTestStore::~OfflinePageTestStore() { |
93 } | 94 } |
94 | 95 |
95 void OfflinePageTestStore::Load(const LoadCallback& callback) { | 96 void OfflinePageTestStore::Load(const LoadCallback& callback) { |
| 97 OfflinePageMetadataStore::LoadStatus load_status; |
96 if (scenario_ != TestScenario::LOAD_FAILED) { | 98 if (scenario_ != TestScenario::LOAD_FAILED) { |
97 task_runner_->PostTask( | 99 load_status = OfflinePageMetadataStore::LOAD_SUCCEEDED; |
98 FROM_HERE, base::Bind(callback, true, offline_pages_)); | |
99 } else { | 100 } else { |
100 task_runner_->PostTask( | 101 load_status = OfflinePageMetadataStore::STORE_LOAD_FAILED; |
101 FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>())); | 102 offline_pages_.clear(); |
102 } | 103 } |
| 104 task_runner_->PostTask( |
| 105 FROM_HERE, base::Bind(callback, load_status, offline_pages_)); |
103 } | 106 } |
104 | 107 |
105 void OfflinePageTestStore::AddOrUpdateOfflinePage( | 108 void OfflinePageTestStore::AddOrUpdateOfflinePage( |
106 const OfflinePageItem& offline_page, const UpdateCallback& callback) { | 109 const OfflinePageItem& offline_page, const UpdateCallback& callback) { |
107 last_saved_page_ = offline_page; | 110 last_saved_page_ = offline_page; |
108 bool result = scenario_ != TestScenario::WRITE_FAILED; | 111 bool result = scenario_ != TestScenario::WRITE_FAILED; |
109 if (result) { | 112 if (result) { |
110 offline_pages_.push_back(offline_page); | 113 offline_pages_.push_back(offline_page); |
111 } | 114 } |
112 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); | 115 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); |
(...skipping 11 matching lines...) Expand all Loading... |
124 offline_pages_.erase(iter); | 127 offline_pages_.erase(iter); |
125 result = true; | 128 result = true; |
126 break; | 129 break; |
127 } | 130 } |
128 } | 131 } |
129 } | 132 } |
130 | 133 |
131 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); | 134 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); |
132 } | 135 } |
133 | 136 |
| 137 void OfflinePageTestStore::Reset(const ResetCallback& callback) { |
| 138 offline_pages_.clear(); |
| 139 task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); |
| 140 } |
| 141 |
134 void OfflinePageTestStore:: UpdateLastAccessTime( | 142 void OfflinePageTestStore:: UpdateLastAccessTime( |
135 int64 bookmark_id, const base::Time& last_access_time) { | 143 int64 bookmark_id, const base::Time& last_access_time) { |
136 for (auto& offline_page : offline_pages_) { | 144 for (auto& offline_page : offline_pages_) { |
137 if (offline_page.bookmark_id == bookmark_id) { | 145 if (offline_page.bookmark_id == bookmark_id) { |
138 offline_page.last_access_time = last_access_time; | 146 offline_page.last_access_time = last_access_time; |
139 return; | 147 return; |
140 } | 148 } |
141 } | 149 } |
142 } | 150 } |
143 | 151 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 void TearDown() override; | 196 void TearDown() override; |
189 | 197 |
190 // OfflinePageModel::Observer implementation. | 198 // OfflinePageModel::Observer implementation. |
191 void OfflinePageModelLoaded(OfflinePageModel* model) override; | 199 void OfflinePageModelLoaded(OfflinePageModel* model) override; |
192 void OfflinePageModelChanged(OfflinePageModel* model) override; | 200 void OfflinePageModelChanged(OfflinePageModel* model) override; |
193 void OfflinePageDeleted(int64 bookmark_id) override; | 201 void OfflinePageDeleted(int64 bookmark_id) override; |
194 | 202 |
195 // OfflinePageModel callbacks. | 203 // OfflinePageModel callbacks. |
196 void OnSavePageDone(SavePageResult result); | 204 void OnSavePageDone(SavePageResult result); |
197 void OnDeletePageDone(DeletePageResult result); | 205 void OnDeletePageDone(DeletePageResult result); |
| 206 void OnClearAllDone(); |
198 | 207 |
199 // OfflinePageMetadataStore callbacks. | 208 // OfflinePageMetadataStore callbacks. |
200 void OnStoreUpdateDone(bool /* success */); | 209 void OnStoreUpdateDone(bool /* success */); |
201 | 210 |
202 scoped_ptr<OfflinePageTestArchiver> BuildArchiver( | 211 scoped_ptr<OfflinePageTestArchiver> BuildArchiver( |
203 const GURL& url, | 212 const GURL& url, |
204 OfflinePageArchiver::ArchiverResult result); | 213 OfflinePageArchiver::ArchiverResult result); |
205 scoped_ptr<OfflinePageMetadataStore> BuildStore(); | 214 scoped_ptr<OfflinePageMetadataStore> BuildStore(); |
206 scoped_ptr<OfflinePageModel> BuildModel( | 215 scoped_ptr<OfflinePageModel> BuildModel( |
207 scoped_ptr<OfflinePageMetadataStore> store); | 216 scoped_ptr<OfflinePageMetadataStore> store); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 | 327 |
319 void OfflinePageModelTest::OnSavePageDone( | 328 void OfflinePageModelTest::OnSavePageDone( |
320 OfflinePageModel::SavePageResult result) { | 329 OfflinePageModel::SavePageResult result) { |
321 last_save_result_ = result; | 330 last_save_result_ = result; |
322 } | 331 } |
323 | 332 |
324 void OfflinePageModelTest::OnDeletePageDone(DeletePageResult result) { | 333 void OfflinePageModelTest::OnDeletePageDone(DeletePageResult result) { |
325 last_delete_result_ = result; | 334 last_delete_result_ = result; |
326 } | 335 } |
327 | 336 |
| 337 void OfflinePageModelTest::OnClearAllDone() { |
| 338 base::RunLoop().RunUntilIdle(); |
| 339 } |
| 340 |
328 void OfflinePageModelTest::OnStoreUpdateDone(bool /* success - ignored */) { | 341 void OfflinePageModelTest::OnStoreUpdateDone(bool /* success - ignored */) { |
329 } | 342 } |
330 | 343 |
331 scoped_ptr<OfflinePageTestArchiver> OfflinePageModelTest::BuildArchiver( | 344 scoped_ptr<OfflinePageTestArchiver> OfflinePageModelTest::BuildArchiver( |
332 const GURL& url, | 345 const GURL& url, |
333 OfflinePageArchiver::ArchiverResult result) { | 346 OfflinePageArchiver::ArchiverResult result) { |
334 return scoped_ptr<OfflinePageTestArchiver>(new OfflinePageTestArchiver( | 347 return scoped_ptr<OfflinePageTestArchiver>(new OfflinePageTestArchiver( |
335 this, url, temp_dir_.path(), result, task_runner())); | 348 this, url, temp_dir_.path(), result, task_runner())); |
336 } | 349 } |
337 | 350 |
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
881 | 894 |
882 TEST_F(OfflinePageModelTest, CanSavePage) { | 895 TEST_F(OfflinePageModelTest, CanSavePage) { |
883 EXPECT_TRUE(OfflinePageModel::CanSavePage(GURL("http://foo"))); | 896 EXPECT_TRUE(OfflinePageModel::CanSavePage(GURL("http://foo"))); |
884 EXPECT_TRUE(OfflinePageModel::CanSavePage(GURL("https://foo"))); | 897 EXPECT_TRUE(OfflinePageModel::CanSavePage(GURL("https://foo"))); |
885 EXPECT_FALSE(OfflinePageModel::CanSavePage(GURL("file:///foo"))); | 898 EXPECT_FALSE(OfflinePageModel::CanSavePage(GURL("file:///foo"))); |
886 EXPECT_FALSE(OfflinePageModel::CanSavePage(GURL("data:image/png;base64,ab"))); | 899 EXPECT_FALSE(OfflinePageModel::CanSavePage(GURL("data:image/png;base64,ab"))); |
887 EXPECT_FALSE(OfflinePageModel::CanSavePage(GURL("chrome://version"))); | 900 EXPECT_FALSE(OfflinePageModel::CanSavePage(GURL("chrome://version"))); |
888 EXPECT_FALSE(OfflinePageModel::CanSavePage(GURL("chrome-native://newtab/"))); | 901 EXPECT_FALSE(OfflinePageModel::CanSavePage(GURL("chrome-native://newtab/"))); |
889 } | 902 } |
890 | 903 |
| 904 TEST_F(OfflinePageModelTest, ClearAll) { |
| 905 scoped_ptr<OfflinePageTestArchiver> archiver( |
| 906 BuildArchiver(kTestUrl, |
| 907 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED) |
| 908 .Pass()); |
| 909 model()->SavePage( |
| 910 kTestUrl, kTestPageBookmarkId1, archiver.Pass(), |
| 911 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr())); |
| 912 PumpLoop(); |
| 913 |
| 914 scoped_ptr<OfflinePageTestArchiver> archiver2( |
| 915 BuildArchiver(kTestUrl2, |
| 916 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED) |
| 917 .Pass()); |
| 918 model()->SavePage( |
| 919 kTestUrl2, kTestPageBookmarkId2, archiver2.Pass(), |
| 920 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr())); |
| 921 PumpLoop(); |
| 922 |
| 923 const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages(); |
| 924 EXPECT_EQ(2UL, offline_pages.size()); |
| 925 EXPECT_EQ(2UL, GetStore()->offline_pages().size()); |
| 926 base::FilePath archiver_path = offline_pages[0].file_path; |
| 927 EXPECT_TRUE(base::PathExists(archiver_path)); |
| 928 |
| 929 // ClearAll should delete all the files and wipe out both cache and store. |
| 930 model()->ClearAll( |
| 931 base::Bind(&OfflinePageModelTest::OnClearAllDone, AsWeakPtr())); |
| 932 PumpLoop(); |
| 933 EXPECT_EQ(0UL, model()->GetAllPages().size()); |
| 934 EXPECT_EQ(0UL, GetStore()->offline_pages().size()); |
| 935 EXPECT_FALSE(base::PathExists(archiver_path)); |
| 936 |
| 937 // The model should reload the store after the reset. All model operations |
| 938 // should continue to work. |
| 939 scoped_ptr<OfflinePageTestArchiver> archiver3( |
| 940 BuildArchiver(kTestUrl2, |
| 941 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED) |
| 942 .Pass()); |
| 943 model()->SavePage( |
| 944 kTestUrl2, kTestPageBookmarkId2, archiver3.Pass(), |
| 945 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr())); |
| 946 PumpLoop(); |
| 947 EXPECT_EQ(1UL, model()->GetAllPages().size()); |
| 948 EXPECT_EQ(1UL, GetStore()->offline_pages().size()); |
| 949 } |
| 950 |
891 } // namespace offline_pages | 951 } // namespace offline_pages |
OLD | NEW |