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/downloads/download_ui_adapter.h" | 5 #include "components/offline_pages/core/downloads/download_ui_adapter.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "base/test/test_mock_time_task_runner.h" | 20 #include "base/test/test_mock_time_task_runner.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 #include "components/offline_pages/client_namespace_constants.h" | 23 #include "components/offline_pages/core/client_namespace_constants.h" |
24 #include "components/offline_pages/client_policy_controller.h" | 24 #include "components/offline_pages/core/client_policy_controller.h" |
25 #include "components/offline_pages/stub_offline_page_model.h" | 25 #include "components/offline_pages/core/stub_offline_page_model.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
27 | 27 |
28 namespace offline_pages { | 28 namespace offline_pages { |
29 | 29 |
30 namespace { | 30 namespace { |
31 // Constants for a test OfflinePageItem. | 31 // Constants for a test OfflinePageItem. |
32 static const int kTestOfflineId1 = 1; | 32 static const int kTestOfflineId1 = 1; |
33 static const int kTestOfflineId2 = 2; | 33 static const int kTestOfflineId2 = 2; |
34 static const int kTestOfflineId3 = 3; | 34 static const int kTestOfflineId3 = 3; |
35 static const char kTestUrl[] = "http://foo.com/bar.mhtml"; | 35 static const char kTestUrl[] = "http://foo.com/bar.mhtml"; |
36 static const char kTestGuid1[] = "cccccccc-cccc-4ccc-0ccc-ccccccccccc1"; | 36 static const char kTestGuid1[] = "cccccccc-cccc-4ccc-0ccc-ccccccccccc1"; |
37 static const char kTestGuid2[] = "cccccccc-cccc-4ccc-0ccc-ccccccccccc2"; | 37 static const char kTestGuid2[] = "cccccccc-cccc-4ccc-0ccc-ccccccccccc2"; |
38 static const char kTestBadGuid[] = "ccccccc-cccc-0ccc-0ccc-ccccccccccc0"; | 38 static const char kTestBadGuid[] = "ccccccc-cccc-0ccc-0ccc-ccccccccccc0"; |
39 static const ClientId kTestClientIdOtherNamespace(kLastNNamespace, kTestGuid1); | 39 static const ClientId kTestClientIdOtherNamespace(kLastNNamespace, kTestGuid1); |
40 static const ClientId kTestClientIdOtherGuid(kLastNNamespace, kTestBadGuid); | 40 static const ClientId kTestClientIdOtherGuid(kLastNNamespace, kTestBadGuid); |
41 static const ClientId kTestClientId1(kAsyncNamespace, kTestGuid1); | 41 static const ClientId kTestClientId1(kAsyncNamespace, kTestGuid1); |
42 static const ClientId kTestClientId2(kAsyncNamespace, kTestGuid2); | 42 static const ClientId kTestClientId2(kAsyncNamespace, kTestGuid2); |
43 static const base::FilePath kTestFilePath = | 43 static const base::FilePath kTestFilePath = |
44 base::FilePath(FILE_PATH_LITERAL("foo/bar.mhtml")); | 44 base::FilePath(FILE_PATH_LITERAL("foo/bar.mhtml")); |
45 static const int kFileSize = 1000; | 45 static const int kFileSize = 1000; |
46 static const base::Time kTestCreationTime = base::Time::Now(); | 46 static const base::Time kTestCreationTime = base::Time::Now(); |
47 static const base::string16 kTestTitle = base::ASCIIToUTF16("test title"); | 47 static const base::string16 kTestTitle = base::ASCIIToUTF16("test title"); |
48 } // namespace | 48 } // namespace |
49 | 49 |
50 // Mock OfflinePageModel for testing the SavePage calls. | 50 // Mock OfflinePageModel for testing the SavePage calls. |
51 class MockOfflinePageModel : public StubOfflinePageModel { | 51 class MockOfflinePageModel : public StubOfflinePageModel { |
52 public: | 52 public: |
53 MockOfflinePageModel(base::TestMockTimeTaskRunner* task_runner) | 53 MockOfflinePageModel(base::TestMockTimeTaskRunner* task_runner) |
54 : observer_(nullptr), | 54 : observer_(nullptr), |
55 task_runner_(task_runner), | 55 task_runner_(task_runner), |
56 policy_controller_(new ClientPolicyController()) { | 56 policy_controller_(new ClientPolicyController()) { |
57 adapter.reset(new DownloadUIAdapter(this)); | 57 adapter.reset(new DownloadUIAdapter(this)); |
58 // Add one page. | 58 // Add one page. |
59 OfflinePageItem page(GURL(kTestUrl), | 59 OfflinePageItem page(GURL(kTestUrl), kTestOfflineId1, kTestClientId1, |
60 kTestOfflineId1, | 60 kTestFilePath, kFileSize, kTestCreationTime); |
61 kTestClientId1, | |
62 kTestFilePath, | |
63 kFileSize, | |
64 kTestCreationTime); | |
65 page.title = kTestTitle; | 61 page.title = kTestTitle; |
66 pages[kTestOfflineId1] = page; | 62 pages[kTestOfflineId1] = page; |
67 } | 63 } |
68 | 64 |
69 ~MockOfflinePageModel() override {} | 65 ~MockOfflinePageModel() override {} |
70 | 66 |
71 // OfflinePageModel overrides. | 67 // OfflinePageModel overrides. |
72 void AddObserver(Observer* observer) override { | 68 void AddObserver(Observer* observer) override { |
73 EXPECT_TRUE(observer != nullptr); | 69 EXPECT_TRUE(observer != nullptr); |
74 observer_ = observer; | 70 observer_ = observer; |
75 } | 71 } |
76 | 72 |
77 void RemoveObserver(Observer* observer) override { | 73 void RemoveObserver(Observer* observer) override { |
78 EXPECT_TRUE(observer != nullptr); | 74 EXPECT_TRUE(observer != nullptr); |
79 EXPECT_EQ(observer, observer_); | 75 EXPECT_EQ(observer, observer_); |
80 observer_ = nullptr; | 76 observer_ = nullptr; |
81 } | 77 } |
82 | 78 |
83 // PostTask instead of just running callback to simpulate the real class. | 79 // PostTask instead of just running callback to simpulate the real class. |
84 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override { | 80 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override { |
85 task_runner_->PostTask( | 81 task_runner_->PostTask(FROM_HERE, |
86 FROM_HERE, base::Bind(&MockOfflinePageModel::GetAllPagesImpl, | 82 base::Bind(&MockOfflinePageModel::GetAllPagesImpl, |
87 base::Unretained(this), callback)); | 83 base::Unretained(this), callback)); |
88 } | 84 } |
89 | 85 |
90 void GetAllPagesImpl(const MultipleOfflinePageItemCallback& callback) { | 86 void GetAllPagesImpl(const MultipleOfflinePageItemCallback& callback) { |
91 std::vector<OfflinePageItem> result; | 87 std::vector<OfflinePageItem> result; |
92 for (const auto& page : pages) | 88 for (const auto& page : pages) |
93 result.push_back(page.second); | 89 result.push_back(page.second); |
94 callback.Run(result); | 90 callback.Run(result); |
95 } | 91 } |
96 | 92 |
97 void DeletePageAndNotifyAdapter(const std::string& guid) { | 93 void DeletePageAndNotifyAdapter(const std::string& guid) { |
98 for(const auto& page : pages) { | 94 for (const auto& page : pages) { |
99 if (page.second.client_id.id == guid) { | 95 if (page.second.client_id.id == guid) { |
100 observer_->OfflinePageDeleted(page.second.offline_id, | 96 observer_->OfflinePageDeleted(page.second.offline_id, |
101 page.second.client_id); | 97 page.second.client_id); |
102 pages.erase(page.first); | 98 pages.erase(page.first); |
103 return; | 99 return; |
104 } | 100 } |
105 } | 101 } |
106 } | 102 } |
107 | 103 |
108 void AddPageAndNotifyAdapter(const OfflinePageItem& page) { | 104 void AddPageAndNotifyAdapter(const OfflinePageItem& page) { |
(...skipping 13 matching lines...) Expand all Loading... |
122 | 118 |
123 private: | 119 private: |
124 OfflinePageModel::Observer* observer_; | 120 OfflinePageModel::Observer* observer_; |
125 base::TestMockTimeTaskRunner* task_runner_; | 121 base::TestMockTimeTaskRunner* task_runner_; |
126 // Normally owned by OfflinePageModel. | 122 // Normally owned by OfflinePageModel. |
127 std::unique_ptr<ClientPolicyController> policy_controller_; | 123 std::unique_ptr<ClientPolicyController> policy_controller_; |
128 | 124 |
129 DISALLOW_COPY_AND_ASSIGN(MockOfflinePageModel); | 125 DISALLOW_COPY_AND_ASSIGN(MockOfflinePageModel); |
130 }; | 126 }; |
131 | 127 |
132 class DownloadUIAdapterTest | 128 class DownloadUIAdapterTest : public testing::Test, |
133 : public testing::Test, | 129 public DownloadUIAdapter::Observer { |
134 public DownloadUIAdapter::Observer { | |
135 public: | 130 public: |
136 DownloadUIAdapterTest(); | 131 DownloadUIAdapterTest(); |
137 ~DownloadUIAdapterTest() override; | 132 ~DownloadUIAdapterTest() override; |
138 | 133 |
139 // testing::Test | 134 // testing::Test |
140 void SetUp() override; | 135 void SetUp() override; |
141 | 136 |
142 // DownloadUIAdapter::Observer | 137 // DownloadUIAdapter::Observer |
143 void ItemsLoaded() override; | 138 void ItemsLoaded() override; |
144 void ItemAdded(const DownloadUIItem& item) override; | 139 void ItemAdded(const DownloadUIItem& item) override; |
145 void ItemUpdated(const DownloadUIItem& item) override; | 140 void ItemUpdated(const DownloadUIItem& item) override; |
146 void ItemDeleted(const std::string& guid) override; | 141 void ItemDeleted(const std::string& guid) override; |
147 | 142 |
148 // Runs until all of the tasks that are not delayed are gone from the task | 143 // Runs until all of the tasks that are not delayed are gone from the task |
149 // queue. | 144 // queue. |
150 void PumpLoop(); | 145 void PumpLoop(); |
151 | 146 |
152 bool items_loaded; | 147 bool items_loaded; |
153 std::vector<std::string> added_guids, updated_guids, deleted_guids; | 148 std::vector<std::string> added_guids, updated_guids, deleted_guids; |
154 std::unique_ptr<MockOfflinePageModel> model; | 149 std::unique_ptr<MockOfflinePageModel> model; |
155 | 150 |
156 private: | 151 private: |
157 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | 152 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
158 }; | 153 }; |
159 | 154 |
160 DownloadUIAdapterTest::DownloadUIAdapterTest() | 155 DownloadUIAdapterTest::DownloadUIAdapterTest() |
161 : items_loaded(false), | 156 : items_loaded(false), task_runner_(new base::TestMockTimeTaskRunner) {} |
162 task_runner_(new base::TestMockTimeTaskRunner) { | |
163 } | |
164 | 157 |
165 DownloadUIAdapterTest::~DownloadUIAdapterTest() { | 158 DownloadUIAdapterTest::~DownloadUIAdapterTest() {} |
166 } | |
167 | 159 |
168 void DownloadUIAdapterTest::SetUp() { | 160 void DownloadUIAdapterTest::SetUp() { |
169 model.reset(new MockOfflinePageModel(task_runner_.get())); | 161 model.reset(new MockOfflinePageModel(task_runner_.get())); |
170 model->adapter->AddObserver(this); | 162 model->adapter->AddObserver(this); |
171 } | 163 } |
172 | 164 |
173 void DownloadUIAdapterTest::ItemsLoaded() { | 165 void DownloadUIAdapterTest::ItemsLoaded() { |
174 items_loaded = true; | 166 items_loaded = true; |
175 } | 167 } |
176 | 168 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 EXPECT_EQ(kTestUrl, item->url.spec()); | 200 EXPECT_EQ(kTestUrl, item->url.spec()); |
209 EXPECT_EQ(kTestFilePath, item->target_path); | 201 EXPECT_EQ(kTestFilePath, item->target_path); |
210 EXPECT_EQ(kTestCreationTime, item->start_time); | 202 EXPECT_EQ(kTestCreationTime, item->start_time); |
211 EXPECT_EQ(kFileSize, item->total_bytes); | 203 EXPECT_EQ(kFileSize, item->total_bytes); |
212 EXPECT_EQ(kTestTitle, item->title); | 204 EXPECT_EQ(kTestTitle, item->title); |
213 } | 205 } |
214 | 206 |
215 TEST_F(DownloadUIAdapterTest, ItemDeletedAdded) { | 207 TEST_F(DownloadUIAdapterTest, ItemDeletedAdded) { |
216 PumpLoop(); | 208 PumpLoop(); |
217 // Add page, notify adapter. | 209 // Add page, notify adapter. |
218 OfflinePageItem page(GURL(kTestUrl), | 210 OfflinePageItem page(GURL(kTestUrl), kTestOfflineId2, kTestClientId2, |
219 kTestOfflineId2, | 211 base::FilePath(kTestFilePath), kFileSize, |
220 kTestClientId2, | |
221 base::FilePath(kTestFilePath), | |
222 kFileSize, | |
223 kTestCreationTime); | 212 kTestCreationTime); |
224 model->AddPageAndNotifyAdapter(page); | 213 model->AddPageAndNotifyAdapter(page); |
225 PumpLoop(); | 214 PumpLoop(); |
226 EXPECT_EQ(1UL, added_guids.size()); | 215 EXPECT_EQ(1UL, added_guids.size()); |
227 EXPECT_EQ(kTestGuid2, added_guids[0]); | 216 EXPECT_EQ(kTestGuid2, added_guids[0]); |
228 // Remove pages, notify adapter. | 217 // Remove pages, notify adapter. |
229 model->DeletePageAndNotifyAdapter(kTestGuid1); | 218 model->DeletePageAndNotifyAdapter(kTestGuid1); |
230 model->DeletePageAndNotifyAdapter(kTestGuid2); | 219 model->DeletePageAndNotifyAdapter(kTestGuid2); |
231 PumpLoop(); | 220 PumpLoop(); |
232 EXPECT_EQ(2UL, deleted_guids.size()); | 221 EXPECT_EQ(2UL, deleted_guids.size()); |
233 EXPECT_EQ(kTestGuid1, deleted_guids[0]); | 222 EXPECT_EQ(kTestGuid1, deleted_guids[0]); |
234 EXPECT_EQ(kTestGuid2, deleted_guids[1]); | 223 EXPECT_EQ(kTestGuid2, deleted_guids[1]); |
235 } | 224 } |
236 | 225 |
237 TEST_F(DownloadUIAdapterTest, ItemWithWrongNamespace) { | 226 TEST_F(DownloadUIAdapterTest, ItemWithWrongNamespace) { |
238 PumpLoop(); | 227 PumpLoop(); |
239 OfflinePageItem page1(GURL(kTestUrl), | 228 OfflinePageItem page1( |
240 kTestOfflineId2, | 229 GURL(kTestUrl), kTestOfflineId2, kTestClientIdOtherNamespace, |
241 kTestClientIdOtherNamespace, | 230 base::FilePath(kTestFilePath), kFileSize, kTestCreationTime); |
242 base::FilePath(kTestFilePath), | |
243 kFileSize, | |
244 kTestCreationTime); | |
245 model->AddPageAndNotifyAdapter(page1); | 231 model->AddPageAndNotifyAdapter(page1); |
246 PumpLoop(); | 232 PumpLoop(); |
247 // Should not add the page with wrong namespace. | 233 // Should not add the page with wrong namespace. |
248 EXPECT_EQ(0UL, added_guids.size()); | 234 EXPECT_EQ(0UL, added_guids.size()); |
249 | 235 |
250 OfflinePageItem page2(GURL(kTestUrl), | 236 OfflinePageItem page2(GURL(kTestUrl), kTestOfflineId3, kTestClientIdOtherGuid, |
251 kTestOfflineId3, | 237 base::FilePath(kTestFilePath), kFileSize, |
252 kTestClientIdOtherGuid, | |
253 base::FilePath(kTestFilePath), | |
254 kFileSize, | |
255 kTestCreationTime); | 238 kTestCreationTime); |
256 model->AddPageAndNotifyAdapter(page2); | 239 model->AddPageAndNotifyAdapter(page2); |
257 PumpLoop(); | 240 PumpLoop(); |
258 // Should not add the page with wrong guid. | 241 // Should not add the page with wrong guid. |
259 EXPECT_EQ(0UL, added_guids.size()); | 242 EXPECT_EQ(0UL, added_guids.size()); |
260 } | 243 } |
261 | 244 |
262 TEST_F(DownloadUIAdapterTest, ItemUpdated) { | 245 TEST_F(DownloadUIAdapterTest, ItemUpdated) { |
263 PumpLoop(); | 246 PumpLoop(); |
264 // Clear the initial page and replace it with updated one. | 247 // Clear the initial page and replace it with updated one. |
265 model->pages.clear(); | 248 model->pages.clear(); |
266 // Add page with the same offline_id/guid, notify adapter. | 249 // Add page with the same offline_id/guid, notify adapter. |
267 // This should generate 'updated' notification. | 250 // This should generate 'updated' notification. |
268 OfflinePageItem page1(GURL(kTestUrl), | 251 OfflinePageItem page1(GURL(kTestUrl), kTestOfflineId1, kTestClientId1, |
269 kTestOfflineId1, | 252 base::FilePath(kTestFilePath), kFileSize, |
270 kTestClientId1, | |
271 base::FilePath(kTestFilePath), | |
272 kFileSize, | |
273 kTestCreationTime); | 253 kTestCreationTime); |
274 // Add a new page which did not exist before. | 254 // Add a new page which did not exist before. |
275 OfflinePageItem page2(GURL(kTestUrl), | 255 OfflinePageItem page2(GURL(kTestUrl), kTestOfflineId2, kTestClientId2, |
276 kTestOfflineId2, | 256 base::FilePath(kTestFilePath), kFileSize, |
277 kTestClientId2, | |
278 base::FilePath(kTestFilePath), | |
279 kFileSize, | |
280 kTestCreationTime); | 257 kTestCreationTime); |
281 model->AddPageAndNotifyAdapter(page1); | 258 model->AddPageAndNotifyAdapter(page1); |
282 model->AddPageAndNotifyAdapter(page2); | 259 model->AddPageAndNotifyAdapter(page2); |
283 PumpLoop(); | 260 PumpLoop(); |
284 EXPECT_EQ(1UL, added_guids.size()); | 261 EXPECT_EQ(1UL, added_guids.size()); |
285 EXPECT_EQ(kTestGuid2, added_guids[0]); | 262 EXPECT_EQ(kTestGuid2, added_guids[0]); |
286 // TODO(dimich): we currently don't report updated items since OPM doesn't | 263 // TODO(dimich): we currently don't report updated items since OPM doesn't |
287 // have support for that. Add as needed, this will have to be updated when | 264 // have support for that. Add as needed, this will have to be updated when |
288 // support is added. | 265 // support is added. |
289 EXPECT_EQ(0UL, updated_guids.size()); | 266 EXPECT_EQ(0UL, updated_guids.size()); |
290 } | 267 } |
291 | 268 |
292 TEST_F(DownloadUIAdapterTest, NoHangingLoad) { | 269 TEST_F(DownloadUIAdapterTest, NoHangingLoad) { |
293 EXPECT_NE(nullptr, model->adapter); | 270 EXPECT_NE(nullptr, model->adapter); |
294 EXPECT_FALSE(items_loaded); | 271 EXPECT_FALSE(items_loaded); |
295 // Removal of last observer causes cache unload of not-yet-loaded cache. | 272 // Removal of last observer causes cache unload of not-yet-loaded cache. |
296 model->adapter->RemoveObserver(this); | 273 model->adapter->RemoveObserver(this); |
297 // This will complete async fetch of items, but... | 274 // This will complete async fetch of items, but... |
298 PumpLoop(); | 275 PumpLoop(); |
299 // items should not be loaded when there is no observers! | 276 // items should not be loaded when there is no observers! |
300 EXPECT_FALSE(items_loaded); | 277 EXPECT_FALSE(items_loaded); |
301 // This should not crash. | 278 // This should not crash. |
302 model->adapter->AddObserver(this); | 279 model->adapter->AddObserver(this); |
303 } | 280 } |
304 | 281 |
305 } // namespace offline_pages | 282 } // namespace offline_pages |
OLD | NEW |