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/core/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/string_number_conversions.h" | |
19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
20 #include "base/test/test_mock_time_task_runner.h" | 21 #include "base/test/test_mock_time_task_runner.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
22 #include "base/time/time.h" | 23 #include "base/time/time.h" |
24 #include "components/offline_pages/core/background/offliner_stub.h" | |
25 #include "components/offline_pages/core/background/request_coordinator_stub_taco .h" | |
23 #include "components/offline_pages/core/client_namespace_constants.h" | 26 #include "components/offline_pages/core/client_namespace_constants.h" |
24 #include "components/offline_pages/core/client_policy_controller.h" | 27 #include "components/offline_pages/core/client_policy_controller.h" |
25 #include "components/offline_pages/core/stub_offline_page_model.h" | 28 #include "components/offline_pages/core/stub_offline_page_model.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
27 | 30 |
28 namespace offline_pages { | 31 namespace offline_pages { |
29 | 32 |
30 namespace { | 33 namespace { |
31 // Constants for a test OfflinePageItem. | 34 // Constants for a test OfflinePageItem. |
32 static const int kTestOfflineId1 = 1; | 35 static const int kTestOfflineId1 = 1; |
33 static const int kTestOfflineId2 = 2; | 36 static const int kTestOfflineId2 = 2; |
34 static const int kTestOfflineId3 = 3; | |
35 static const char kTestUrl[] = "http://foo.com/bar.mhtml"; | 37 static const char kTestUrl[] = "http://foo.com/bar.mhtml"; |
36 static const char kTestGuid1[] = "cccccccc-cccc-4ccc-0ccc-ccccccccccc1"; | 38 static const char kTestGuid1[] = "cccccccc-cccc-4ccc-0ccc-ccccccccccc1"; |
37 static const char kTestGuid2[] = "cccccccc-cccc-4ccc-0ccc-ccccccccccc2"; | 39 static const char kTestGuid2[] = "cccccccc-cccc-4ccc-0ccc-ccccccccccc2"; |
38 static const char kTestBadGuid[] = "ccccccc-cccc-0ccc-0ccc-ccccccccccc0"; | 40 static const char kTestBadGuid[] = "ccccccc-cccc-0ccc-0ccc-ccccccccccc0"; |
39 static const ClientId kTestClientIdOtherNamespace(kLastNNamespace, kTestGuid1); | 41 static const ClientId kTestClientIdOtherNamespace(kLastNNamespace, kTestGuid1); |
40 static const ClientId kTestClientIdOtherGuid(kLastNNamespace, kTestBadGuid); | 42 static const ClientId kTestClientIdOtherGuid(kLastNNamespace, kTestBadGuid); |
41 static const ClientId kTestClientId1(kAsyncNamespace, kTestGuid1); | 43 static const ClientId kTestClientId1(kAsyncNamespace, kTestGuid1); |
42 static const ClientId kTestClientId2(kAsyncNamespace, kTestGuid2); | 44 static const ClientId kTestClientId2(kAsyncNamespace, kTestGuid2); |
43 static const base::FilePath kTestFilePath = | 45 static const base::FilePath kTestFilePath = |
44 base::FilePath(FILE_PATH_LITERAL("foo/bar.mhtml")); | 46 base::FilePath(FILE_PATH_LITERAL("foo/bar.mhtml")); |
45 static const int kFileSize = 1000; | 47 static const int kFileSize = 1000; |
46 static const base::Time kTestCreationTime = base::Time::Now(); | 48 static const base::Time kTestCreationTime = base::Time::Now(); |
47 static const base::string16 kTestTitle = base::ASCIIToUTF16("test title"); | 49 static const base::string16 kTestTitle = base::ASCIIToUTF16("test title"); |
48 } // namespace | 50 } // namespace |
49 | 51 |
52 // Mock DownloadUIAdapter::Delegate | |
53 class DownloadUIAdapterDelegate : public DownloadUIAdapter::Delegate { | |
54 public: | |
55 DownloadUIAdapterDelegate() {} | |
56 ~DownloadUIAdapterDelegate() override {} | |
57 | |
58 // DownloadUIAdapter::Delegate | |
59 bool IsVisibleInUI(const ClientId& client_id) override { return is_visible; } | |
60 bool IsTemporarilyHiddenInUI(const ClientId& client_id) override { | |
61 return is_temporarily_hidden; | |
62 } | |
63 | |
64 bool is_visible = true; | |
65 bool is_temporarily_hidden = false; | |
66 }; | |
67 | |
50 // Mock OfflinePageModel for testing the SavePage calls. | 68 // Mock OfflinePageModel for testing the SavePage calls. |
51 class MockOfflinePageModel : public StubOfflinePageModel { | 69 class MockOfflinePageModel : public StubOfflinePageModel { |
52 public: | 70 public: |
53 MockOfflinePageModel(base::TestMockTimeTaskRunner* task_runner) | 71 MockOfflinePageModel(base::TestMockTimeTaskRunner* task_runner) |
54 : observer_(nullptr), | 72 : observer_(nullptr), |
55 task_runner_(task_runner), | 73 task_runner_(task_runner), |
56 policy_controller_(new ClientPolicyController()) { | 74 policy_controller_(new ClientPolicyController()) {} |
57 adapter.reset(new DownloadUIAdapter(this)); | 75 |
58 // Add one page. | 76 ~MockOfflinePageModel() override {} |
77 | |
78 void AddInitialPage() { | |
59 OfflinePageItem page(GURL(kTestUrl), kTestOfflineId1, kTestClientId1, | 79 OfflinePageItem page(GURL(kTestUrl), kTestOfflineId1, kTestClientId1, |
60 kTestFilePath, kFileSize, kTestCreationTime); | 80 kTestFilePath, kFileSize, kTestCreationTime); |
61 page.title = kTestTitle; | 81 page.title = kTestTitle; |
62 pages[kTestOfflineId1] = page; | 82 pages[kTestOfflineId1] = page; |
63 } | 83 } |
64 | 84 |
65 ~MockOfflinePageModel() override {} | |
66 | |
67 // OfflinePageModel overrides. | 85 // OfflinePageModel overrides. |
68 void AddObserver(Observer* observer) override { | 86 void AddObserver(Observer* observer) override { |
69 EXPECT_TRUE(observer != nullptr); | 87 EXPECT_TRUE(observer != nullptr); |
70 observer_ = observer; | 88 observer_ = observer; |
71 } | 89 } |
72 | 90 |
73 void RemoveObserver(Observer* observer) override { | 91 void RemoveObserver(Observer* observer) override { |
74 EXPECT_TRUE(observer != nullptr); | 92 EXPECT_TRUE(observer != nullptr); |
75 EXPECT_EQ(observer, observer_); | 93 EXPECT_EQ(observer, observer_); |
76 observer_ = nullptr; | 94 observer_ = nullptr; |
(...skipping 27 matching lines...) Expand all Loading... | |
104 void AddPageAndNotifyAdapter(const OfflinePageItem& page) { | 122 void AddPageAndNotifyAdapter(const OfflinePageItem& page) { |
105 EXPECT_EQ(pages.end(), pages.find(page.offline_id)); | 123 EXPECT_EQ(pages.end(), pages.find(page.offline_id)); |
106 pages[page.offline_id] = page; | 124 pages[page.offline_id] = page; |
107 observer_->OfflinePageAdded(this, page); | 125 observer_->OfflinePageAdded(this, page); |
108 } | 126 } |
109 | 127 |
110 ClientPolicyController* GetPolicyController() override { | 128 ClientPolicyController* GetPolicyController() override { |
111 return policy_controller_.get(); | 129 return policy_controller_.get(); |
112 } | 130 } |
113 | 131 |
114 // Normally, OfflinePageModel owns this adapter, so lets test it this way. | |
115 std::unique_ptr<DownloadUIAdapter> adapter; | |
116 | |
117 std::map<int64_t, OfflinePageItem> pages; | 132 std::map<int64_t, OfflinePageItem> pages; |
118 | 133 |
119 private: | 134 private: |
120 OfflinePageModel::Observer* observer_; | 135 OfflinePageModel::Observer* observer_; |
121 base::TestMockTimeTaskRunner* task_runner_; | 136 base::TestMockTimeTaskRunner* task_runner_; |
122 // Normally owned by OfflinePageModel. | 137 // Normally owned by OfflinePageModel. |
123 std::unique_ptr<ClientPolicyController> policy_controller_; | 138 std::unique_ptr<ClientPolicyController> policy_controller_; |
124 | 139 |
125 DISALLOW_COPY_AND_ASSIGN(MockOfflinePageModel); | 140 DISALLOW_COPY_AND_ASSIGN(MockOfflinePageModel); |
126 }; | 141 }; |
127 | 142 |
128 class DownloadUIAdapterTest : public testing::Test, | 143 class DownloadUIAdapterTest : public testing::Test, |
dewittj
2017/01/30 20:59:34
Any chance you could document what is set up in Se
Dmitry Titov
2017/02/08 00:45:08
Done.
| |
129 public DownloadUIAdapter::Observer { | 144 public DownloadUIAdapter::Observer { |
130 public: | 145 public: |
131 DownloadUIAdapterTest(); | 146 DownloadUIAdapterTest(); |
132 ~DownloadUIAdapterTest() override; | 147 ~DownloadUIAdapterTest() override; |
133 | 148 |
134 // testing::Test | 149 // testing::Test |
135 void SetUp() override; | 150 void SetUp() override; |
136 | 151 |
137 // DownloadUIAdapter::Observer | 152 // DownloadUIAdapter::Observer |
138 void ItemsLoaded() override; | 153 void ItemsLoaded() override; |
139 void ItemAdded(const DownloadUIItem& item) override; | 154 void ItemAdded(const DownloadUIItem& item) override; |
140 void ItemUpdated(const DownloadUIItem& item) override; | 155 void ItemUpdated(const DownloadUIItem& item) override; |
141 void ItemDeleted(const std::string& guid) override; | 156 void ItemDeleted(const std::string& guid) override; |
142 | 157 |
143 // Runs until all of the tasks that are not delayed are gone from the task | 158 // Runs until all of the tasks that are not delayed are gone from the task |
144 // queue. | 159 // queue. |
145 void PumpLoop(); | 160 void PumpLoop(); |
146 | 161 |
162 int64_t AddRequest(const GURL& url, const ClientId& client_id); | |
163 | |
164 RequestCoordinator* request_coordinator() { | |
165 return request_coordinator_taco_->request_coordinator(); | |
166 } | |
167 | |
147 bool items_loaded; | 168 bool items_loaded; |
148 std::vector<std::string> added_guids, updated_guids, deleted_guids; | 169 std::vector<std::string> added_guids, updated_guids, deleted_guids; |
149 std::unique_ptr<MockOfflinePageModel> model; | 170 std::unique_ptr<MockOfflinePageModel> model; |
171 DownloadUIAdapterDelegate* adapter_delegate; | |
172 std::unique_ptr<DownloadUIAdapter> adapter; | |
173 OfflinerStub* offliner_stub; | |
150 | 174 |
151 private: | 175 private: |
176 std::unique_ptr<RequestCoordinatorStubTaco> request_coordinator_taco_; | |
152 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | 177 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
178 base::ThreadTaskRunnerHandle task_runner_handle_; | |
153 }; | 179 }; |
154 | 180 |
155 DownloadUIAdapterTest::DownloadUIAdapterTest() | 181 DownloadUIAdapterTest::DownloadUIAdapterTest() |
156 : items_loaded(false), task_runner_(new base::TestMockTimeTaskRunner) {} | 182 : items_loaded(false), |
183 task_runner_(new base::TestMockTimeTaskRunner), | |
184 task_runner_handle_(task_runner_) {} | |
157 | 185 |
158 DownloadUIAdapterTest::~DownloadUIAdapterTest() {} | 186 DownloadUIAdapterTest::~DownloadUIAdapterTest() {} |
159 | 187 |
160 void DownloadUIAdapterTest::SetUp() { | 188 void DownloadUIAdapterTest::SetUp() { |
161 model.reset(new MockOfflinePageModel(task_runner_.get())); | 189 model = base::MakeUnique<MockOfflinePageModel>(task_runner_.get()); |
162 model->adapter->AddObserver(this); | 190 std::unique_ptr<DownloadUIAdapterDelegate> delegate = |
191 base::MakeUnique<DownloadUIAdapterDelegate>(); | |
192 adapter_delegate = delegate.get(); | |
193 request_coordinator_taco_ = base::MakeUnique<RequestCoordinatorStubTaco>(); | |
194 | |
195 std::unique_ptr<OfflinerStub> offliner = base::MakeUnique<OfflinerStub>(); | |
196 offliner_stub = offliner.get(); | |
197 request_coordinator_taco_->SetOffliner(std::move(offliner)); | |
198 | |
199 request_coordinator_taco_->CreateRequestCoordinator(); | |
200 adapter = base::MakeUnique<DownloadUIAdapter>( | |
201 model.get(), request_coordinator_taco_->request_coordinator(), | |
202 std::move(delegate)); | |
203 | |
204 adapter->AddObserver(this); | |
163 } | 205 } |
164 | 206 |
165 void DownloadUIAdapterTest::ItemsLoaded() { | 207 void DownloadUIAdapterTest::ItemsLoaded() { |
166 items_loaded = true; | 208 items_loaded = true; |
167 } | 209 } |
168 | 210 |
169 void DownloadUIAdapterTest::ItemAdded(const DownloadUIItem& item) { | 211 void DownloadUIAdapterTest::ItemAdded(const DownloadUIItem& item) { |
170 added_guids.push_back(item.guid); | 212 added_guids.push_back(item.guid); |
171 } | 213 } |
172 | 214 |
173 void DownloadUIAdapterTest::ItemUpdated(const DownloadUIItem& item) { | 215 void DownloadUIAdapterTest::ItemUpdated(const DownloadUIItem& item) { |
174 updated_guids.push_back(item.guid); | 216 updated_guids.push_back(item.guid); |
175 } | 217 } |
176 | 218 |
177 void DownloadUIAdapterTest::ItemDeleted(const std::string& guid) { | 219 void DownloadUIAdapterTest::ItemDeleted(const std::string& guid) { |
178 deleted_guids.push_back(guid); | 220 deleted_guids.push_back(guid); |
179 } | 221 } |
180 | 222 |
181 void DownloadUIAdapterTest::PumpLoop() { | 223 void DownloadUIAdapterTest::PumpLoop() { |
182 task_runner_->RunUntilIdle(); | 224 task_runner_->RunUntilIdle(); |
183 } | 225 } |
184 | 226 |
227 int64_t DownloadUIAdapterTest::AddRequest(const GURL& url, | |
228 const ClientId& client_id) { | |
229 return request_coordinator()->SavePageLater( | |
230 url, client_id, /* user_requested */ true, | |
231 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER); | |
232 } | |
233 | |
185 TEST_F(DownloadUIAdapterTest, InitialLoad) { | 234 TEST_F(DownloadUIAdapterTest, InitialLoad) { |
186 EXPECT_NE(nullptr, model->adapter); | 235 EXPECT_NE(nullptr, adapter.get()); |
236 model->AddInitialPage(); | |
187 EXPECT_FALSE(items_loaded); | 237 EXPECT_FALSE(items_loaded); |
188 PumpLoop(); | 238 PumpLoop(); |
189 EXPECT_TRUE(items_loaded); | 239 EXPECT_TRUE(items_loaded); |
190 const DownloadUIItem* item = model->adapter->GetItem(kTestGuid1); | 240 const DownloadUIItem* item = adapter->GetItem(kTestGuid1); |
191 EXPECT_NE(nullptr, item); | 241 EXPECT_NE(nullptr, item); |
192 } | 242 } |
193 | 243 |
194 TEST_F(DownloadUIAdapterTest, InitialItemConversion) { | 244 TEST_F(DownloadUIAdapterTest, InitialItemConversion) { |
245 model->AddInitialPage(); | |
195 EXPECT_EQ(1UL, model->pages.size()); | 246 EXPECT_EQ(1UL, model->pages.size()); |
196 EXPECT_EQ(kTestGuid1, model->pages[kTestOfflineId1].client_id.id); | 247 EXPECT_EQ(kTestGuid1, model->pages[kTestOfflineId1].client_id.id); |
197 PumpLoop(); | 248 PumpLoop(); |
198 const DownloadUIItem* item = model->adapter->GetItem(kTestGuid1); | 249 const DownloadUIItem* item = adapter->GetItem(kTestGuid1); |
199 EXPECT_EQ(kTestGuid1, item->guid); | 250 EXPECT_EQ(kTestGuid1, item->guid); |
200 EXPECT_EQ(kTestUrl, item->url.spec()); | 251 EXPECT_EQ(kTestUrl, item->url.spec()); |
252 EXPECT_EQ(DownloadUIItem::DownloadState::COMPLETE, item->download_state); | |
253 EXPECT_EQ(0, item->download_progress_bytes); | |
201 EXPECT_EQ(kTestFilePath, item->target_path); | 254 EXPECT_EQ(kTestFilePath, item->target_path); |
202 EXPECT_EQ(kTestCreationTime, item->start_time); | 255 EXPECT_EQ(kTestCreationTime, item->start_time); |
203 EXPECT_EQ(kFileSize, item->total_bytes); | 256 EXPECT_EQ(kFileSize, item->total_bytes); |
204 EXPECT_EQ(kTestTitle, item->title); | 257 EXPECT_EQ(kTestTitle, item->title); |
205 } | 258 } |
206 | 259 |
207 TEST_F(DownloadUIAdapterTest, ItemDeletedAdded) { | 260 TEST_F(DownloadUIAdapterTest, ItemDeletedAdded) { |
261 model->AddInitialPage(); | |
208 PumpLoop(); | 262 PumpLoop(); |
209 // Add page, notify adapter. | 263 // Add page, notify adapter. |
210 OfflinePageItem page(GURL(kTestUrl), kTestOfflineId2, kTestClientId2, | 264 OfflinePageItem page(GURL(kTestUrl), kTestOfflineId2, kTestClientId2, |
211 base::FilePath(kTestFilePath), kFileSize, | 265 base::FilePath(kTestFilePath), kFileSize, |
212 kTestCreationTime); | 266 kTestCreationTime); |
213 model->AddPageAndNotifyAdapter(page); | 267 model->AddPageAndNotifyAdapter(page); |
214 PumpLoop(); | 268 PumpLoop(); |
215 EXPECT_EQ(1UL, added_guids.size()); | 269 EXPECT_EQ(1UL, added_guids.size()); |
216 EXPECT_EQ(kTestGuid2, added_guids[0]); | 270 EXPECT_EQ(kTestGuid2, added_guids[0]); |
217 // Remove pages, notify adapter. | 271 // Remove pages, notify adapter. |
218 model->DeletePageAndNotifyAdapter(kTestGuid1); | 272 model->DeletePageAndNotifyAdapter(kTestGuid1); |
219 model->DeletePageAndNotifyAdapter(kTestGuid2); | 273 model->DeletePageAndNotifyAdapter(kTestGuid2); |
220 PumpLoop(); | 274 PumpLoop(); |
221 EXPECT_EQ(2UL, deleted_guids.size()); | 275 EXPECT_EQ(2UL, deleted_guids.size()); |
222 EXPECT_EQ(kTestGuid1, deleted_guids[0]); | 276 EXPECT_EQ(kTestGuid1, deleted_guids[0]); |
223 EXPECT_EQ(kTestGuid2, deleted_guids[1]); | 277 EXPECT_EQ(kTestGuid2, deleted_guids[1]); |
224 } | 278 } |
225 | 279 |
226 TEST_F(DownloadUIAdapterTest, ItemWithWrongNamespace) { | 280 TEST_F(DownloadUIAdapterTest, NotVisibleItem) { |
281 model->AddInitialPage(); | |
227 PumpLoop(); | 282 PumpLoop(); |
283 adapter_delegate->is_visible = false; | |
228 OfflinePageItem page1( | 284 OfflinePageItem page1( |
229 GURL(kTestUrl), kTestOfflineId2, kTestClientIdOtherNamespace, | 285 GURL(kTestUrl), kTestOfflineId2, kTestClientIdOtherNamespace, |
230 base::FilePath(kTestFilePath), kFileSize, kTestCreationTime); | 286 base::FilePath(kTestFilePath), kFileSize, kTestCreationTime); |
231 model->AddPageAndNotifyAdapter(page1); | 287 model->AddPageAndNotifyAdapter(page1); |
232 PumpLoop(); | 288 PumpLoop(); |
233 // Should not add the page with wrong namespace. | 289 // Should not add the page. |
234 EXPECT_EQ(0UL, added_guids.size()); | |
235 | |
236 OfflinePageItem page2(GURL(kTestUrl), kTestOfflineId3, kTestClientIdOtherGuid, | |
237 base::FilePath(kTestFilePath), kFileSize, | |
238 kTestCreationTime); | |
239 model->AddPageAndNotifyAdapter(page2); | |
240 PumpLoop(); | |
241 // Should not add the page with wrong guid. | |
242 EXPECT_EQ(0UL, added_guids.size()); | 290 EXPECT_EQ(0UL, added_guids.size()); |
243 } | 291 } |
244 | 292 |
293 TEST_F(DownloadUIAdapterTest, TemporarilyNotVisibleItem) { | |
294 adapter_delegate->is_temporarily_hidden = true; | |
295 model->AddInitialPage(); | |
296 PumpLoop(); | |
297 // Initial Item should be invisible in the collection now. | |
298 EXPECT_EQ(nullptr, adapter->GetItem(kTestGuid1)); | |
299 EXPECT_EQ(0UL, adapter->GetAllItems().size()); | |
300 EXPECT_EQ(0UL, added_guids.size()); | |
301 EXPECT_EQ(0UL, deleted_guids.size()); | |
302 | |
303 adapter_delegate->is_temporarily_hidden = false; | |
304 // Notify adapter about visibility change for the clientId of initial page. | |
305 adapter->TemporaryHiddenStatusChanged(kTestClientId1); | |
306 PumpLoop(); | |
307 | |
308 // There should be OnAdded simulated. | |
309 EXPECT_EQ(1UL, added_guids.size()); | |
310 EXPECT_EQ(0UL, deleted_guids.size()); | |
311 // Also the item should be visible in the collection of items now. | |
312 EXPECT_NE(nullptr, adapter->GetItem(kTestGuid1)); | |
313 EXPECT_EQ(1UL, adapter->GetAllItems().size()); | |
314 | |
315 // Switch visibility back to hidden | |
316 adapter_delegate->is_temporarily_hidden = true; | |
317 adapter->TemporaryHiddenStatusChanged(kTestClientId1); | |
318 // There should be OnDeleted fired. | |
319 EXPECT_EQ(1UL, added_guids.size()); | |
320 EXPECT_EQ(1UL, deleted_guids.size()); | |
321 // Also the item should be visible in the collection of items now. | |
322 EXPECT_EQ(nullptr, adapter->GetItem(kTestGuid1)); | |
323 EXPECT_EQ(0UL, adapter->GetAllItems().size()); | |
324 } | |
325 | |
245 TEST_F(DownloadUIAdapterTest, ItemAdded) { | 326 TEST_F(DownloadUIAdapterTest, ItemAdded) { |
327 model->AddInitialPage(); | |
246 PumpLoop(); | 328 PumpLoop(); |
247 // Clear the initial page and replace it with updated one. | 329 // Clear the initial page and replace it with updated one. |
248 model->pages.clear(); | 330 model->pages.clear(); |
249 // Add a new page which did not exist before. | 331 // Add a new page which did not exist before. |
250 OfflinePageItem page2(GURL(kTestUrl), kTestOfflineId2, kTestClientId2, | 332 OfflinePageItem page2(GURL(kTestUrl), kTestOfflineId2, kTestClientId2, |
251 base::FilePath(kTestFilePath), kFileSize, | 333 base::FilePath(kTestFilePath), kFileSize, |
252 kTestCreationTime); | 334 kTestCreationTime); |
253 model->AddPageAndNotifyAdapter(page2); | 335 model->AddPageAndNotifyAdapter(page2); |
254 PumpLoop(); | 336 PumpLoop(); |
255 EXPECT_EQ(1UL, added_guids.size()); | 337 EXPECT_EQ(1UL, added_guids.size()); |
256 EXPECT_EQ(kTestGuid2, added_guids[0]); | 338 EXPECT_EQ(kTestGuid2, added_guids[0]); |
257 // TODO(dimich): we currently don't report updated items since OPM doesn't | 339 // TODO(dimich): we currently don't report updated items since OPM doesn't |
258 // have support for that. Add as needed, this will have to be updated when | 340 // have support for that. Add as needed, this will have to be updated when |
259 // support is added. | 341 // support is added. |
260 EXPECT_EQ(0UL, updated_guids.size()); | 342 EXPECT_EQ(0UL, updated_guids.size()); |
261 } | 343 } |
262 | 344 |
263 TEST_F(DownloadUIAdapterTest, NoHangingLoad) { | 345 TEST_F(DownloadUIAdapterTest, NoHangingLoad) { |
264 EXPECT_NE(nullptr, model->adapter); | 346 model->AddInitialPage(); |
347 EXPECT_NE(nullptr, adapter.get()); | |
265 EXPECT_FALSE(items_loaded); | 348 EXPECT_FALSE(items_loaded); |
266 // Removal of last observer causes cache unload of not-yet-loaded cache. | 349 // Removal of last observer causes cache unload of not-yet-loaded cache. |
267 model->adapter->RemoveObserver(this); | 350 adapter->RemoveObserver(this); |
268 // This will complete async fetch of items, but... | 351 // This will complete async fetch of items, but... |
269 PumpLoop(); | 352 PumpLoop(); |
270 // items should not be loaded when there is no observers! | 353 // items should not be loaded when there is no observers! |
271 EXPECT_FALSE(items_loaded); | 354 EXPECT_FALSE(items_loaded); |
272 // This should not crash. | 355 // This should not crash. |
273 model->adapter->AddObserver(this); | 356 adapter->AddObserver(this); |
357 } | |
358 | |
359 TEST_F(DownloadUIAdapterTest, LoadExistingRequest) { | |
360 AddRequest(GURL(kTestUrl), kTestClientId1); | |
361 PumpLoop(); | |
362 EXPECT_TRUE(items_loaded); | |
363 const DownloadUIItem* item = adapter->GetItem(kTestGuid1); | |
364 EXPECT_NE(nullptr, item); | |
365 } | |
366 | |
367 TEST_F(DownloadUIAdapterTest, AddRequest) { | |
368 PumpLoop(); | |
369 EXPECT_TRUE(items_loaded); | |
370 EXPECT_EQ(0UL, added_guids.size()); | |
371 AddRequest(GURL(kTestUrl), kTestClientId1); | |
372 PumpLoop(); | |
373 EXPECT_EQ(1UL, added_guids.size()); | |
374 EXPECT_EQ(kTestClientId1.id, added_guids[0]); | |
375 const DownloadUIItem* item = adapter->GetItem(kTestGuid1); | |
376 EXPECT_NE(nullptr, item); | |
377 } | |
378 | |
379 TEST_F(DownloadUIAdapterTest, RemoveRequest) { | |
380 int64_t id = AddRequest(GURL(kTestUrl), kTestClientId1); | |
381 PumpLoop(); | |
382 // No added requests, the initial one is loaded. | |
383 EXPECT_EQ(0UL, added_guids.size()); | |
384 EXPECT_NE(nullptr, adapter->GetItem(kTestGuid1)); | |
385 EXPECT_EQ(0UL, deleted_guids.size()); | |
386 | |
387 std::vector<int64_t> requests_to_remove = {id}; | |
388 request_coordinator()->RemoveRequests( | |
389 requests_to_remove, | |
390 base::Bind( | |
391 [](int64_t id, const MultipleItemStatuses& statuses) { | |
392 EXPECT_EQ(1UL, statuses.size()); | |
393 EXPECT_EQ(id, statuses[0].first); | |
394 EXPECT_EQ(ItemActionStatus::SUCCESS, statuses[0].second); | |
395 }, | |
396 id)); | |
397 PumpLoop(); | |
398 | |
399 EXPECT_EQ(0UL, added_guids.size()); | |
400 EXPECT_EQ(1UL, deleted_guids.size()); | |
401 EXPECT_EQ(kTestClientId1.id, deleted_guids[0]); | |
402 EXPECT_EQ(nullptr, adapter->GetItem(kTestGuid1)); | |
403 } | |
404 | |
405 TEST_F(DownloadUIAdapterTest, RequestBecomesPage) { | |
406 // This will cause requests to be 'offlined' all the way and removed. | |
407 offliner_stub->enable_callback(true); | |
408 AddRequest(GURL(kTestUrl), kTestClientId1); | |
409 PumpLoop(); | |
410 | |
411 const DownloadUIItem* item = adapter->GetItem(kTestGuid1); | |
412 EXPECT_NE(nullptr, item); | |
413 // The item is still IN_PROGRESS, since we did not delete it when | |
414 // request is competed successfully, waiting for the page with the | |
415 // same client_id to come in. | |
416 EXPECT_EQ(DownloadUIItem::DownloadState::IN_PROGRESS, item->download_state); | |
417 // Add a new saved page with the same client id. | |
418 // This simulates what happens when the request is completed. | |
419 // It should not fire and OnAdded or OnDeleted, just OnUpdated. | |
420 OfflinePageItem page(GURL(kTestUrl), kTestOfflineId1, kTestClientId1, | |
421 base::FilePath(kTestFilePath), kFileSize, | |
422 kTestCreationTime); | |
423 model->AddPageAndNotifyAdapter(page); | |
424 PumpLoop(); | |
425 | |
426 // No added or deleted items, the one existing item should be 'updated'. | |
427 EXPECT_EQ(0UL, added_guids.size()); | |
428 EXPECT_EQ(0UL, deleted_guids.size()); | |
429 | |
430 EXPECT_GE(updated_guids.size(), 1UL); | |
431 std::string last_updated_guid = updated_guids[updated_guids.size() - 1]; | |
432 item = adapter->GetItem(last_updated_guid); | |
433 EXPECT_NE(nullptr, item); | |
434 EXPECT_EQ(DownloadUIItem::DownloadState::COMPLETE, item->download_state); | |
274 } | 435 } |
275 | 436 |
276 } // namespace offline_pages | 437 } // namespace offline_pages |
OLD | NEW |