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 |
| 143 // Creates mock versions for OfflinePageModel, RequestCoordinator and their |
| 144 // dependencies, then passes them to DownloadUIAdapter for testing. |
| 145 // Note that initially the OfflienPageModel is not "loaded". PumpLoop() will |
| 146 // load it, firing ItemsLoaded callback to the Adapter. Hence some tests |
| 147 // start from PumpLoop() right away if they don't need to test this. |
128 class DownloadUIAdapterTest : public testing::Test, | 148 class DownloadUIAdapterTest : public testing::Test, |
129 public DownloadUIAdapter::Observer { | 149 public DownloadUIAdapter::Observer { |
130 public: | 150 public: |
131 DownloadUIAdapterTest(); | 151 DownloadUIAdapterTest(); |
132 ~DownloadUIAdapterTest() override; | 152 ~DownloadUIAdapterTest() override; |
133 | 153 |
134 // testing::Test | 154 // testing::Test |
135 void SetUp() override; | 155 void SetUp() override; |
136 | 156 |
137 // DownloadUIAdapter::Observer | 157 // DownloadUIAdapter::Observer |
138 void ItemsLoaded() override; | 158 void ItemsLoaded() override; |
139 void ItemAdded(const DownloadUIItem& item) override; | 159 void ItemAdded(const DownloadUIItem& item) override; |
140 void ItemUpdated(const DownloadUIItem& item) override; | 160 void ItemUpdated(const DownloadUIItem& item) override; |
141 void ItemDeleted(const std::string& guid) override; | 161 void ItemDeleted(const std::string& guid) override; |
142 | 162 |
143 // Runs until all of the tasks that are not delayed are gone from the task | 163 // Runs until all of the tasks that are not delayed are gone from the task |
144 // queue. | 164 // queue. |
145 void PumpLoop(); | 165 void PumpLoop(); |
146 | 166 |
| 167 int64_t AddRequest(const GURL& url, const ClientId& client_id); |
| 168 |
| 169 RequestCoordinator* request_coordinator() { |
| 170 return request_coordinator_taco_->request_coordinator(); |
| 171 } |
| 172 |
147 bool items_loaded; | 173 bool items_loaded; |
148 std::vector<std::string> added_guids, updated_guids, deleted_guids; | 174 std::vector<std::string> added_guids, updated_guids, deleted_guids; |
149 std::unique_ptr<MockOfflinePageModel> model; | 175 std::unique_ptr<MockOfflinePageModel> model; |
| 176 DownloadUIAdapterDelegate* adapter_delegate; |
| 177 std::unique_ptr<DownloadUIAdapter> adapter; |
| 178 OfflinerStub* offliner_stub; |
150 | 179 |
151 private: | 180 private: |
| 181 std::unique_ptr<RequestCoordinatorStubTaco> request_coordinator_taco_; |
152 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | 182 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
| 183 base::ThreadTaskRunnerHandle task_runner_handle_; |
153 }; | 184 }; |
154 | 185 |
155 DownloadUIAdapterTest::DownloadUIAdapterTest() | 186 DownloadUIAdapterTest::DownloadUIAdapterTest() |
156 : items_loaded(false), task_runner_(new base::TestMockTimeTaskRunner) {} | 187 : items_loaded(false), |
| 188 task_runner_(new base::TestMockTimeTaskRunner), |
| 189 task_runner_handle_(task_runner_) {} |
157 | 190 |
158 DownloadUIAdapterTest::~DownloadUIAdapterTest() {} | 191 DownloadUIAdapterTest::~DownloadUIAdapterTest() {} |
159 | 192 |
160 void DownloadUIAdapterTest::SetUp() { | 193 void DownloadUIAdapterTest::SetUp() { |
161 model.reset(new MockOfflinePageModel(task_runner_.get())); | 194 model = base::MakeUnique<MockOfflinePageModel>(task_runner_.get()); |
162 model->adapter->AddObserver(this); | 195 std::unique_ptr<DownloadUIAdapterDelegate> delegate = |
| 196 base::MakeUnique<DownloadUIAdapterDelegate>(); |
| 197 adapter_delegate = delegate.get(); |
| 198 request_coordinator_taco_ = base::MakeUnique<RequestCoordinatorStubTaco>(); |
| 199 |
| 200 std::unique_ptr<OfflinerStub> offliner = base::MakeUnique<OfflinerStub>(); |
| 201 offliner_stub = offliner.get(); |
| 202 request_coordinator_taco_->SetOffliner(std::move(offliner)); |
| 203 |
| 204 request_coordinator_taco_->CreateRequestCoordinator(); |
| 205 adapter = base::MakeUnique<DownloadUIAdapter>( |
| 206 model.get(), request_coordinator_taco_->request_coordinator(), |
| 207 std::move(delegate)); |
| 208 |
| 209 adapter->AddObserver(this); |
163 } | 210 } |
164 | 211 |
165 void DownloadUIAdapterTest::ItemsLoaded() { | 212 void DownloadUIAdapterTest::ItemsLoaded() { |
166 items_loaded = true; | 213 items_loaded = true; |
167 } | 214 } |
168 | 215 |
169 void DownloadUIAdapterTest::ItemAdded(const DownloadUIItem& item) { | 216 void DownloadUIAdapterTest::ItemAdded(const DownloadUIItem& item) { |
170 added_guids.push_back(item.guid); | 217 added_guids.push_back(item.guid); |
171 } | 218 } |
172 | 219 |
173 void DownloadUIAdapterTest::ItemUpdated(const DownloadUIItem& item) { | 220 void DownloadUIAdapterTest::ItemUpdated(const DownloadUIItem& item) { |
174 updated_guids.push_back(item.guid); | 221 updated_guids.push_back(item.guid); |
175 } | 222 } |
176 | 223 |
177 void DownloadUIAdapterTest::ItemDeleted(const std::string& guid) { | 224 void DownloadUIAdapterTest::ItemDeleted(const std::string& guid) { |
178 deleted_guids.push_back(guid); | 225 deleted_guids.push_back(guid); |
179 } | 226 } |
180 | 227 |
181 void DownloadUIAdapterTest::PumpLoop() { | 228 void DownloadUIAdapterTest::PumpLoop() { |
182 task_runner_->RunUntilIdle(); | 229 task_runner_->RunUntilIdle(); |
183 } | 230 } |
184 | 231 |
| 232 int64_t DownloadUIAdapterTest::AddRequest(const GURL& url, |
| 233 const ClientId& client_id) { |
| 234 return request_coordinator()->SavePageLater( |
| 235 url, client_id, /* user_requested */ true, |
| 236 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER); |
| 237 } |
| 238 |
185 TEST_F(DownloadUIAdapterTest, InitialLoad) { | 239 TEST_F(DownloadUIAdapterTest, InitialLoad) { |
186 EXPECT_NE(nullptr, model->adapter); | 240 EXPECT_NE(nullptr, adapter.get()); |
| 241 model->AddInitialPage(); |
187 EXPECT_FALSE(items_loaded); | 242 EXPECT_FALSE(items_loaded); |
188 PumpLoop(); | 243 PumpLoop(); |
189 EXPECT_TRUE(items_loaded); | 244 EXPECT_TRUE(items_loaded); |
190 const DownloadUIItem* item = model->adapter->GetItem(kTestGuid1); | 245 const DownloadUIItem* item = adapter->GetItem(kTestGuid1); |
191 EXPECT_NE(nullptr, item); | 246 EXPECT_NE(nullptr, item); |
192 } | 247 } |
193 | 248 |
194 TEST_F(DownloadUIAdapterTest, InitialItemConversion) { | 249 TEST_F(DownloadUIAdapterTest, InitialItemConversion) { |
| 250 model->AddInitialPage(); |
195 EXPECT_EQ(1UL, model->pages.size()); | 251 EXPECT_EQ(1UL, model->pages.size()); |
196 EXPECT_EQ(kTestGuid1, model->pages[kTestOfflineId1].client_id.id); | 252 EXPECT_EQ(kTestGuid1, model->pages[kTestOfflineId1].client_id.id); |
197 PumpLoop(); | 253 PumpLoop(); |
198 const DownloadUIItem* item = model->adapter->GetItem(kTestGuid1); | 254 const DownloadUIItem* item = adapter->GetItem(kTestGuid1); |
199 EXPECT_EQ(kTestGuid1, item->guid); | 255 EXPECT_EQ(kTestGuid1, item->guid); |
200 EXPECT_EQ(kTestUrl, item->url.spec()); | 256 EXPECT_EQ(kTestUrl, item->url.spec()); |
| 257 EXPECT_EQ(DownloadUIItem::DownloadState::COMPLETE, item->download_state); |
| 258 EXPECT_EQ(0, item->download_progress_bytes); |
201 EXPECT_EQ(kTestFilePath, item->target_path); | 259 EXPECT_EQ(kTestFilePath, item->target_path); |
202 EXPECT_EQ(kTestCreationTime, item->start_time); | 260 EXPECT_EQ(kTestCreationTime, item->start_time); |
203 EXPECT_EQ(kFileSize, item->total_bytes); | 261 EXPECT_EQ(kFileSize, item->total_bytes); |
204 EXPECT_EQ(kTestTitle, item->title); | 262 EXPECT_EQ(kTestTitle, item->title); |
205 } | 263 } |
206 | 264 |
207 TEST_F(DownloadUIAdapterTest, ItemDeletedAdded) { | 265 TEST_F(DownloadUIAdapterTest, ItemDeletedAdded) { |
| 266 model->AddInitialPage(); |
208 PumpLoop(); | 267 PumpLoop(); |
209 // Add page, notify adapter. | 268 // Add page, notify adapter. |
210 OfflinePageItem page(GURL(kTestUrl), kTestOfflineId2, kTestClientId2, | 269 OfflinePageItem page(GURL(kTestUrl), kTestOfflineId2, kTestClientId2, |
211 base::FilePath(kTestFilePath), kFileSize, | 270 base::FilePath(kTestFilePath), kFileSize, |
212 kTestCreationTime); | 271 kTestCreationTime); |
213 model->AddPageAndNotifyAdapter(page); | 272 model->AddPageAndNotifyAdapter(page); |
214 PumpLoop(); | 273 PumpLoop(); |
215 EXPECT_EQ(1UL, added_guids.size()); | 274 EXPECT_EQ(1UL, added_guids.size()); |
216 EXPECT_EQ(kTestGuid2, added_guids[0]); | 275 EXPECT_EQ(kTestGuid2, added_guids[0]); |
217 // Remove pages, notify adapter. | 276 // Remove pages, notify adapter. |
218 model->DeletePageAndNotifyAdapter(kTestGuid1); | 277 model->DeletePageAndNotifyAdapter(kTestGuid1); |
219 model->DeletePageAndNotifyAdapter(kTestGuid2); | 278 model->DeletePageAndNotifyAdapter(kTestGuid2); |
220 PumpLoop(); | 279 PumpLoop(); |
221 EXPECT_EQ(2UL, deleted_guids.size()); | 280 EXPECT_EQ(2UL, deleted_guids.size()); |
222 EXPECT_EQ(kTestGuid1, deleted_guids[0]); | 281 EXPECT_EQ(kTestGuid1, deleted_guids[0]); |
223 EXPECT_EQ(kTestGuid2, deleted_guids[1]); | 282 EXPECT_EQ(kTestGuid2, deleted_guids[1]); |
224 } | 283 } |
225 | 284 |
226 TEST_F(DownloadUIAdapterTest, ItemWithWrongNamespace) { | 285 TEST_F(DownloadUIAdapterTest, NotVisibleItem) { |
| 286 model->AddInitialPage(); |
227 PumpLoop(); | 287 PumpLoop(); |
| 288 adapter_delegate->is_visible = false; |
228 OfflinePageItem page1( | 289 OfflinePageItem page1( |
229 GURL(kTestUrl), kTestOfflineId2, kTestClientIdOtherNamespace, | 290 GURL(kTestUrl), kTestOfflineId2, kTestClientIdOtherNamespace, |
230 base::FilePath(kTestFilePath), kFileSize, kTestCreationTime); | 291 base::FilePath(kTestFilePath), kFileSize, kTestCreationTime); |
231 model->AddPageAndNotifyAdapter(page1); | 292 model->AddPageAndNotifyAdapter(page1); |
232 PumpLoop(); | 293 PumpLoop(); |
233 // Should not add the page with wrong namespace. | 294 // 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()); | 295 EXPECT_EQ(0UL, added_guids.size()); |
243 } | 296 } |
244 | 297 |
| 298 TEST_F(DownloadUIAdapterTest, TemporarilyNotVisibleItem) { |
| 299 adapter_delegate->is_temporarily_hidden = true; |
| 300 model->AddInitialPage(); |
| 301 PumpLoop(); |
| 302 // Initial Item should be invisible in the collection now. |
| 303 EXPECT_EQ(nullptr, adapter->GetItem(kTestGuid1)); |
| 304 EXPECT_EQ(0UL, adapter->GetAllItems().size()); |
| 305 EXPECT_EQ(0UL, added_guids.size()); |
| 306 EXPECT_EQ(0UL, deleted_guids.size()); |
| 307 |
| 308 adapter_delegate->is_temporarily_hidden = false; |
| 309 // Notify adapter about visibility change for the clientId of initial page. |
| 310 adapter->TemporaryHiddenStatusChanged(kTestClientId1); |
| 311 PumpLoop(); |
| 312 |
| 313 // There should be OnAdded simulated. |
| 314 EXPECT_EQ(1UL, added_guids.size()); |
| 315 EXPECT_EQ(0UL, deleted_guids.size()); |
| 316 // Also the item should be visible in the collection of items now. |
| 317 EXPECT_NE(nullptr, adapter->GetItem(kTestGuid1)); |
| 318 EXPECT_EQ(1UL, adapter->GetAllItems().size()); |
| 319 |
| 320 // Switch visibility back to hidden |
| 321 adapter_delegate->is_temporarily_hidden = true; |
| 322 adapter->TemporaryHiddenStatusChanged(kTestClientId1); |
| 323 // There should be OnDeleted fired. |
| 324 EXPECT_EQ(1UL, added_guids.size()); |
| 325 EXPECT_EQ(1UL, deleted_guids.size()); |
| 326 // Also the item should be visible in the collection of items now. |
| 327 EXPECT_EQ(nullptr, adapter->GetItem(kTestGuid1)); |
| 328 EXPECT_EQ(0UL, adapter->GetAllItems().size()); |
| 329 } |
| 330 |
245 TEST_F(DownloadUIAdapterTest, ItemAdded) { | 331 TEST_F(DownloadUIAdapterTest, ItemAdded) { |
| 332 model->AddInitialPage(); |
246 PumpLoop(); | 333 PumpLoop(); |
247 // Clear the initial page and replace it with updated one. | 334 // Clear the initial page and replace it with updated one. |
248 model->pages.clear(); | 335 model->pages.clear(); |
249 // Add a new page which did not exist before. | 336 // Add a new page which did not exist before. |
250 OfflinePageItem page2(GURL(kTestUrl), kTestOfflineId2, kTestClientId2, | 337 OfflinePageItem page2(GURL(kTestUrl), kTestOfflineId2, kTestClientId2, |
251 base::FilePath(kTestFilePath), kFileSize, | 338 base::FilePath(kTestFilePath), kFileSize, |
252 kTestCreationTime); | 339 kTestCreationTime); |
253 model->AddPageAndNotifyAdapter(page2); | 340 model->AddPageAndNotifyAdapter(page2); |
254 PumpLoop(); | 341 PumpLoop(); |
255 EXPECT_EQ(1UL, added_guids.size()); | 342 EXPECT_EQ(1UL, added_guids.size()); |
256 EXPECT_EQ(kTestGuid2, added_guids[0]); | 343 EXPECT_EQ(kTestGuid2, added_guids[0]); |
257 // TODO(dimich): we currently don't report updated items since OPM doesn't | 344 // 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 | 345 // have support for that. Add as needed, this will have to be updated when |
259 // support is added. | 346 // support is added. |
260 EXPECT_EQ(0UL, updated_guids.size()); | 347 EXPECT_EQ(0UL, updated_guids.size()); |
261 } | 348 } |
262 | 349 |
263 TEST_F(DownloadUIAdapterTest, NoHangingLoad) { | 350 TEST_F(DownloadUIAdapterTest, NoHangingLoad) { |
264 EXPECT_NE(nullptr, model->adapter); | 351 model->AddInitialPage(); |
| 352 EXPECT_NE(nullptr, adapter.get()); |
265 EXPECT_FALSE(items_loaded); | 353 EXPECT_FALSE(items_loaded); |
266 // Removal of last observer causes cache unload of not-yet-loaded cache. | 354 // Removal of last observer causes cache unload of not-yet-loaded cache. |
267 model->adapter->RemoveObserver(this); | 355 adapter->RemoveObserver(this); |
268 // This will complete async fetch of items, but... | 356 // This will complete async fetch of items, but... |
269 PumpLoop(); | 357 PumpLoop(); |
270 // items should not be loaded when there is no observers! | 358 // items should not be loaded when there is no observers! |
271 EXPECT_FALSE(items_loaded); | 359 EXPECT_FALSE(items_loaded); |
272 // This should not crash. | 360 // This should not crash. |
273 model->adapter->AddObserver(this); | 361 adapter->AddObserver(this); |
| 362 } |
| 363 |
| 364 TEST_F(DownloadUIAdapterTest, LoadExistingRequest) { |
| 365 AddRequest(GURL(kTestUrl), kTestClientId1); |
| 366 PumpLoop(); |
| 367 EXPECT_TRUE(items_loaded); |
| 368 const DownloadUIItem* item = adapter->GetItem(kTestGuid1); |
| 369 EXPECT_NE(nullptr, item); |
| 370 } |
| 371 |
| 372 TEST_F(DownloadUIAdapterTest, AddRequest) { |
| 373 PumpLoop(); |
| 374 EXPECT_TRUE(items_loaded); |
| 375 EXPECT_EQ(0UL, added_guids.size()); |
| 376 AddRequest(GURL(kTestUrl), kTestClientId1); |
| 377 PumpLoop(); |
| 378 EXPECT_EQ(1UL, added_guids.size()); |
| 379 EXPECT_EQ(kTestClientId1.id, added_guids[0]); |
| 380 const DownloadUIItem* item = adapter->GetItem(kTestGuid1); |
| 381 EXPECT_NE(nullptr, item); |
| 382 } |
| 383 |
| 384 TEST_F(DownloadUIAdapterTest, RemoveRequest) { |
| 385 int64_t id = AddRequest(GURL(kTestUrl), kTestClientId1); |
| 386 PumpLoop(); |
| 387 // No added requests, the initial one is loaded. |
| 388 EXPECT_EQ(0UL, added_guids.size()); |
| 389 EXPECT_NE(nullptr, adapter->GetItem(kTestGuid1)); |
| 390 EXPECT_EQ(0UL, deleted_guids.size()); |
| 391 |
| 392 std::vector<int64_t> requests_to_remove = {id}; |
| 393 request_coordinator()->RemoveRequests( |
| 394 requests_to_remove, |
| 395 base::Bind( |
| 396 [](int64_t id, const MultipleItemStatuses& statuses) { |
| 397 EXPECT_EQ(1UL, statuses.size()); |
| 398 EXPECT_EQ(id, statuses[0].first); |
| 399 EXPECT_EQ(ItemActionStatus::SUCCESS, statuses[0].second); |
| 400 }, |
| 401 id)); |
| 402 PumpLoop(); |
| 403 |
| 404 EXPECT_EQ(0UL, added_guids.size()); |
| 405 EXPECT_EQ(1UL, deleted_guids.size()); |
| 406 EXPECT_EQ(kTestClientId1.id, deleted_guids[0]); |
| 407 EXPECT_EQ(nullptr, adapter->GetItem(kTestGuid1)); |
| 408 } |
| 409 |
| 410 TEST_F(DownloadUIAdapterTest, RequestBecomesPage) { |
| 411 // This will cause requests to be 'offlined' all the way and removed. |
| 412 offliner_stub->enable_callback(true); |
| 413 AddRequest(GURL(kTestUrl), kTestClientId1); |
| 414 PumpLoop(); |
| 415 |
| 416 const DownloadUIItem* item = adapter->GetItem(kTestGuid1); |
| 417 EXPECT_NE(nullptr, item); |
| 418 // The item is still IN_PROGRESS, since we did not delete it when |
| 419 // request is competed successfully, waiting for the page with the |
| 420 // same client_id to come in. |
| 421 EXPECT_EQ(DownloadUIItem::DownloadState::IN_PROGRESS, item->download_state); |
| 422 // Add a new saved page with the same client id. |
| 423 // This simulates what happens when the request is completed. |
| 424 // It should not fire and OnAdded or OnDeleted, just OnUpdated. |
| 425 OfflinePageItem page(GURL(kTestUrl), kTestOfflineId1, kTestClientId1, |
| 426 base::FilePath(kTestFilePath), kFileSize, |
| 427 kTestCreationTime); |
| 428 model->AddPageAndNotifyAdapter(page); |
| 429 PumpLoop(); |
| 430 |
| 431 // No added or deleted items, the one existing item should be 'updated'. |
| 432 EXPECT_EQ(0UL, added_guids.size()); |
| 433 EXPECT_EQ(0UL, deleted_guids.size()); |
| 434 |
| 435 EXPECT_GE(updated_guids.size(), 1UL); |
| 436 std::string last_updated_guid = updated_guids[updated_guids.size() - 1]; |
| 437 item = adapter->GetItem(last_updated_guid); |
| 438 EXPECT_NE(nullptr, item); |
| 439 EXPECT_EQ(DownloadUIItem::DownloadState::COMPLETE, item->download_state); |
274 } | 440 } |
275 | 441 |
276 } // namespace offline_pages | 442 } // namespace offline_pages |
OLD | NEW |