Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: content/browser/background_fetch/background_fetch_job_controller_unittest.cc

Issue 2774343002: Hook up BackgroundFetchServiceImpl::Fetch() to start a fetch (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 "content/browser/background_fetch/background_fetch_job_controller.h" 5 #include "content/browser/background_fetch/background_fetch_job_controller.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 145
146 void SetUp() override { 146 void SetUp() override {
147 // The download_manager_ ownership is given to the BrowserContext, and the 147 // The download_manager_ ownership is given to the BrowserContext, and the
148 // BrowserContext will take care of deallocating it. 148 // BrowserContext will take care of deallocating it.
149 BrowserContext::SetDownloadManagerForTesting(&browser_context_, 149 BrowserContext::SetDownloadManagerForTesting(&browser_context_,
150 download_manager_); 150 download_manager_);
151 } 151 }
152 152
153 void TearDown() override { job_controller_->Shutdown(); } 153 void TearDown() override { job_controller_->Shutdown(); }
154 154
155 void InitializeJobController() { 155 void InitializeJobController(
156 const BackgroundFetchRegistrationId& registration_id) {
156 job_controller_ = base::MakeUnique<BackgroundFetchJobController>( 157 job_controller_ = base::MakeUnique<BackgroundFetchJobController>(
157 kJobGuid, &browser_context_, 158 registration_id, &browser_context_,
158 BrowserContext::GetDefaultStoragePartition(&browser_context_), 159 BrowserContext::GetDefaultStoragePartition(&browser_context_),
159 data_manager_.get(), 160 data_manager_.get(),
160 base::BindOnce(&BackgroundFetchJobControllerTest::DidCompleteJob, 161 base::BindOnce(&BackgroundFetchJobControllerTest::DidCompleteJob,
161 base::Unretained(this))); 162 base::Unretained(this)));
162 } 163 }
163 164
164 void DidCompleteJob() { did_complete_job_ = true; } 165 void DidCompleteJob(const BackgroundFetchRegistrationId& registration_id) {
166 did_complete_job_ = true;
harkness 2017/03/27 15:35:12 optional: DCHECK that registration_id is equal to
Peter Beverloo 2017/03/27 15:42:55 Done.
167 }
165 168
166 void StartProcessing() { 169 void StartProcessing() {
167 base::RunLoop run_loop; 170 base::RunLoop run_loop;
168 BrowserThread::PostTask( 171 BrowserThread::PostTask(
169 BrowserThread::IO, FROM_HERE, 172 BrowserThread::IO, FROM_HERE,
170 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, 173 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO,
171 base::Unretained(this), run_loop.QuitClosure())); 174 base::Unretained(this), run_loop.QuitClosure()));
172 run_loop.Run(); 175 run_loop.Run();
173 } 176 }
174 177
(...skipping 20 matching lines...) Expand all
195 private: 198 private:
196 bool did_complete_job_ = false; 199 bool did_complete_job_ = false;
197 TestBrowserThreadBundle thread_bundle_; 200 TestBrowserThreadBundle thread_bundle_;
198 TestBrowserContext browser_context_; 201 TestBrowserContext browser_context_;
199 std::unique_ptr<FakeBackgroundFetchDataManager> data_manager_; 202 std::unique_ptr<FakeBackgroundFetchDataManager> data_manager_;
200 std::unique_ptr<BackgroundFetchJobController> job_controller_; 203 std::unique_ptr<BackgroundFetchJobController> job_controller_;
201 MockDownloadManagerWithCallback* download_manager_; 204 MockDownloadManagerWithCallback* download_manager_;
202 }; 205 };
203 206
204 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { 207 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) {
205 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), 208 BackgroundFetchRegistrationId registration_id(
206 kServiceWorkerRegistrationId); 209 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag);
210
207 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); 211 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid);
208 request_info.set_state(DownloadItem::DownloadState::IN_PROGRESS); 212 request_info.set_state(DownloadItem::DownloadState::IN_PROGRESS);
209 data_manager()->set_next_request(&request_info); 213 data_manager()->set_next_request(&request_info);
210 InitializeJobController(); 214 InitializeJobController(registration_id);
211 215
212 EXPECT_CALL(*(download_manager()), 216 EXPECT_CALL(*(download_manager()),
213 DownloadUrlMock(::testing::Pointee(::testing::Property( 217 DownloadUrlMock(::testing::Pointee(::testing::Property(
214 &DownloadUrlParameters::url, GURL(kTestUrl))))) 218 &DownloadUrlParameters::url, GURL(kTestUrl)))))
215 .Times(1); 219 .Times(1);
216 220
217 StartProcessing(); 221 StartProcessing();
218 222
219 // Get the pending download from the download manager. 223 // Get the pending download from the download manager.
220 auto& download_items = download_manager()->download_items(); 224 auto& download_items = download_manager()->download_items();
221 ASSERT_EQ(1U, download_items.size()); 225 ASSERT_EQ(1U, download_items.size());
222 FakeDownloadItem* item = download_items[0].get(); 226 FakeDownloadItem* item = download_items[0].get();
223 227
224 // Update the observer with no actual change. 228 // Update the observer with no actual change.
225 ItemObserver()->OnDownloadUpdated(item); 229 ItemObserver()->OnDownloadUpdated(item);
226 EXPECT_EQ(DownloadItem::DownloadState::IN_PROGRESS, request_info.state()); 230 EXPECT_EQ(DownloadItem::DownloadState::IN_PROGRESS, request_info.state());
227 EXPECT_FALSE(did_complete_job()); 231 EXPECT_FALSE(did_complete_job());
228 232
229 // Update the item to be completed then update the observer. The JobController 233 // Update the item to be completed then update the observer. The JobController
230 // should update the JobData that the request is complete. 234 // should update the JobData that the request is complete.
231 item->SetState(DownloadItem::DownloadState::COMPLETE); 235 item->SetState(DownloadItem::DownloadState::COMPLETE);
232 ItemObserver()->OnDownloadUpdated(item); 236 ItemObserver()->OnDownloadUpdated(item);
233 237
234 EXPECT_EQ(DownloadItem::DownloadState::COMPLETE, request_info.state()); 238 EXPECT_EQ(DownloadItem::DownloadState::COMPLETE, request_info.state());
235 EXPECT_TRUE(did_complete_job()); 239 EXPECT_TRUE(did_complete_job());
236 } 240 }
237 241
238 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { 242 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) {
239 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), 243 BackgroundFetchRegistrationId registration_id(
240 kServiceWorkerRegistrationId); 244 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag);
245
241 std::vector<BackgroundFetchRequestInfo> request_infos; 246 std::vector<BackgroundFetchRequestInfo> request_infos;
242 for (int i = 0; i < 10; i++) { 247 for (int i = 0; i < 10; i++) {
243 request_infos.emplace_back(GURL(kTestUrl), base::IntToString(i)); 248 request_infos.emplace_back(GURL(kTestUrl), base::IntToString(i));
244 } 249 }
245 data_manager()->set_next_request(&request_infos[0]); 250 data_manager()->set_next_request(&request_infos[0]);
246 InitializeJobController(); 251 InitializeJobController(registration_id);
247 252
248 EXPECT_CALL(*(download_manager()), 253 EXPECT_CALL(*(download_manager()),
249 DownloadUrlMock(::testing::Pointee(::testing::Property( 254 DownloadUrlMock(::testing::Pointee(::testing::Property(
250 &DownloadUrlParameters::url, GURL(kTestUrl))))) 255 &DownloadUrlParameters::url, GURL(kTestUrl)))))
251 .Times(10); 256 .Times(10);
252 257
253 StartProcessing(); 258 StartProcessing();
254 259
255 // Get one of the pending downloads from the download manager. 260 // Get one of the pending downloads from the download manager.
256 auto& download_items = download_manager()->download_items(); 261 auto& download_items = download_manager()->download_items();
(...skipping 23 matching lines...) Expand all
280 // see that there are no more requests and mark the job as done. 285 // see that there are no more requests and mark the job as done.
281 ASSERT_EQ(10U, download_items.size()); 286 ASSERT_EQ(10U, download_items.size());
282 item = download_items[9].get(); 287 item = download_items[9].get();
283 item->SetState(DownloadItem::DownloadState::COMPLETE); 288 item->SetState(DownloadItem::DownloadState::COMPLETE);
284 ItemObserver()->OnDownloadUpdated(item); 289 ItemObserver()->OnDownloadUpdated(item);
285 290
286 EXPECT_TRUE(did_complete_job()); 291 EXPECT_TRUE(did_complete_job());
287 } 292 }
288 293
289 TEST_F(BackgroundFetchJobControllerTest, UpdateStorageState) { 294 TEST_F(BackgroundFetchJobControllerTest, UpdateStorageState) {
290 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), 295 BackgroundFetchRegistrationId registration_id(
291 kServiceWorkerRegistrationId); 296 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag);
297
292 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); 298 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid);
293 request_info.set_state(DownloadItem::DownloadState::IN_PROGRESS); 299 request_info.set_state(DownloadItem::DownloadState::IN_PROGRESS);
294 data_manager()->set_next_request(&request_info); 300 data_manager()->set_next_request(&request_info);
295 InitializeJobController(); 301 InitializeJobController(registration_id);
296 302
297 EXPECT_CALL(*(download_manager()), 303 EXPECT_CALL(*(download_manager()),
298 DownloadUrlMock(::testing::Pointee(::testing::Property( 304 DownloadUrlMock(::testing::Pointee(::testing::Property(
299 &DownloadUrlParameters::url, GURL(kTestUrl))))) 305 &DownloadUrlParameters::url, GURL(kTestUrl)))))
300 .Times(1); 306 .Times(1);
301 307
302 StartProcessing(); 308 StartProcessing();
303 309
304 // Get the pending download from the download manager. 310 // Get the pending download from the download manager.
305 auto& download_items = download_manager()->download_items(); 311 auto& download_items = download_manager()->download_items();
306 ASSERT_EQ(1U, download_items.size()); 312 ASSERT_EQ(1U, download_items.size());
307 FakeDownloadItem* item = download_items[0].get(); 313 FakeDownloadItem* item = download_items[0].get();
308 314
309 item->SetTargetFilePath(base::FilePath(kFileName)); 315 item->SetTargetFilePath(base::FilePath(kFileName));
310 item->SetReceivedBytes(123); 316 item->SetReceivedBytes(123);
311 item->SetState(DownloadItem::DownloadState::COMPLETE); 317 item->SetState(DownloadItem::DownloadState::COMPLETE);
312 318
313 // Trigger the observer. The JobController should update the JobData that the 319 // Trigger the observer. The JobController should update the JobData that the
314 // request is complete and should fill in storage state. 320 // request is complete and should fill in storage state.
315 ItemObserver()->OnDownloadUpdated(item); 321 ItemObserver()->OnDownloadUpdated(item);
316 322
317 EXPECT_EQ(123, request_info.received_bytes()); 323 EXPECT_EQ(123, request_info.received_bytes());
318 EXPECT_TRUE(data_manager()->IsComplete(kJobGuid)); 324 EXPECT_TRUE(data_manager()->IsComplete(kJobGuid));
319 EXPECT_TRUE(did_complete_job()); 325 EXPECT_TRUE(did_complete_job());
320 } 326 }
321 327
322 } // namespace content 328 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698