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

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: rebase Created 3 years, 8 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), registration_id));
162 } 163 }
163 164
164 void DidCompleteJob() { did_complete_job_ = true; } 165 void DidCompleteJob(
166 const BackgroundFetchRegistrationId& registration_id,
167 const BackgroundFetchRegistrationId& original_registration_id) {
168 ASSERT_EQ(registration_id, original_registration_id);
169 did_complete_job_ = true;
170 }
165 171
166 void StartProcessing() { 172 void StartProcessing() {
167 base::RunLoop run_loop; 173 base::RunLoop run_loop;
168 BrowserThread::PostTask( 174 BrowserThread::PostTask(
169 BrowserThread::IO, FROM_HERE, 175 BrowserThread::IO, FROM_HERE,
170 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, 176 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO,
171 base::Unretained(this), run_loop.QuitClosure())); 177 base::Unretained(this), run_loop.QuitClosure()));
172 run_loop.Run(); 178 run_loop.Run();
173 } 179 }
174 180
(...skipping 20 matching lines...) Expand all
195 private: 201 private:
196 bool did_complete_job_ = false; 202 bool did_complete_job_ = false;
197 TestBrowserThreadBundle thread_bundle_; 203 TestBrowserThreadBundle thread_bundle_;
198 TestBrowserContext browser_context_; 204 TestBrowserContext browser_context_;
199 std::unique_ptr<FakeBackgroundFetchDataManager> data_manager_; 205 std::unique_ptr<FakeBackgroundFetchDataManager> data_manager_;
200 std::unique_ptr<BackgroundFetchJobController> job_controller_; 206 std::unique_ptr<BackgroundFetchJobController> job_controller_;
201 MockDownloadManagerWithCallback* download_manager_; 207 MockDownloadManagerWithCallback* download_manager_;
202 }; 208 };
203 209
204 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { 210 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) {
205 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), 211 BackgroundFetchRegistrationId registration_id(
206 kServiceWorkerRegistrationId); 212 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag);
213
207 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); 214 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid);
208 request_info.set_state(DownloadItem::DownloadState::IN_PROGRESS); 215 request_info.set_state(DownloadItem::DownloadState::IN_PROGRESS);
209 data_manager()->set_next_request(&request_info); 216 data_manager()->set_next_request(&request_info);
210 InitializeJobController(); 217 InitializeJobController(registration_id);
211 218
212 EXPECT_CALL(*(download_manager()), 219 EXPECT_CALL(*(download_manager()),
213 DownloadUrlMock(::testing::Pointee(::testing::Property( 220 DownloadUrlMock(::testing::Pointee(::testing::Property(
214 &DownloadUrlParameters::url, GURL(kTestUrl))))) 221 &DownloadUrlParameters::url, GURL(kTestUrl)))))
215 .Times(1); 222 .Times(1);
216 223
217 StartProcessing(); 224 StartProcessing();
218 225
219 // Get the pending download from the download manager. 226 // Get the pending download from the download manager.
220 auto& download_items = download_manager()->download_items(); 227 auto& download_items = download_manager()->download_items();
221 ASSERT_EQ(1U, download_items.size()); 228 ASSERT_EQ(1U, download_items.size());
222 FakeDownloadItem* item = download_items[0].get(); 229 FakeDownloadItem* item = download_items[0].get();
223 230
224 // Update the observer with no actual change. 231 // Update the observer with no actual change.
225 ItemObserver()->OnDownloadUpdated(item); 232 ItemObserver()->OnDownloadUpdated(item);
226 EXPECT_EQ(DownloadItem::DownloadState::IN_PROGRESS, request_info.state()); 233 EXPECT_EQ(DownloadItem::DownloadState::IN_PROGRESS, request_info.state());
227 EXPECT_FALSE(did_complete_job()); 234 EXPECT_FALSE(did_complete_job());
228 235
229 // Update the item to be completed then update the observer. The JobController 236 // Update the item to be completed then update the observer. The JobController
230 // should update the JobData that the request is complete. 237 // should update the JobData that the request is complete.
231 item->SetState(DownloadItem::DownloadState::COMPLETE); 238 item->SetState(DownloadItem::DownloadState::COMPLETE);
232 ItemObserver()->OnDownloadUpdated(item); 239 ItemObserver()->OnDownloadUpdated(item);
233 240
234 EXPECT_EQ(DownloadItem::DownloadState::COMPLETE, request_info.state()); 241 EXPECT_EQ(DownloadItem::DownloadState::COMPLETE, request_info.state());
235 EXPECT_TRUE(did_complete_job()); 242 EXPECT_TRUE(did_complete_job());
236 } 243 }
237 244
238 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { 245 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) {
239 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), 246 BackgroundFetchRegistrationId registration_id(
240 kServiceWorkerRegistrationId); 247 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag);
248
241 std::vector<BackgroundFetchRequestInfo> request_infos; 249 std::vector<BackgroundFetchRequestInfo> request_infos;
242 for (int i = 0; i < 10; i++) { 250 for (int i = 0; i < 10; i++) {
243 request_infos.emplace_back(GURL(kTestUrl), base::IntToString(i)); 251 request_infos.emplace_back(GURL(kTestUrl), base::IntToString(i));
244 } 252 }
245 data_manager()->set_next_request(&request_infos[0]); 253 data_manager()->set_next_request(&request_infos[0]);
246 InitializeJobController(); 254 InitializeJobController(registration_id);
247 255
248 EXPECT_CALL(*(download_manager()), 256 EXPECT_CALL(*(download_manager()),
249 DownloadUrlMock(::testing::Pointee(::testing::Property( 257 DownloadUrlMock(::testing::Pointee(::testing::Property(
250 &DownloadUrlParameters::url, GURL(kTestUrl))))) 258 &DownloadUrlParameters::url, GURL(kTestUrl)))))
251 .Times(10); 259 .Times(10);
252 260
253 StartProcessing(); 261 StartProcessing();
254 262
255 // Get one of the pending downloads from the download manager. 263 // Get one of the pending downloads from the download manager.
256 auto& download_items = download_manager()->download_items(); 264 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. 288 // see that there are no more requests and mark the job as done.
281 ASSERT_EQ(10U, download_items.size()); 289 ASSERT_EQ(10U, download_items.size());
282 item = download_items[9].get(); 290 item = download_items[9].get();
283 item->SetState(DownloadItem::DownloadState::COMPLETE); 291 item->SetState(DownloadItem::DownloadState::COMPLETE);
284 ItemObserver()->OnDownloadUpdated(item); 292 ItemObserver()->OnDownloadUpdated(item);
285 293
286 EXPECT_TRUE(did_complete_job()); 294 EXPECT_TRUE(did_complete_job());
287 } 295 }
288 296
289 TEST_F(BackgroundFetchJobControllerTest, UpdateStorageState) { 297 TEST_F(BackgroundFetchJobControllerTest, UpdateStorageState) {
290 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), 298 BackgroundFetchRegistrationId registration_id(
291 kServiceWorkerRegistrationId); 299 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag);
300
292 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); 301 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid);
293 request_info.set_state(DownloadItem::DownloadState::IN_PROGRESS); 302 request_info.set_state(DownloadItem::DownloadState::IN_PROGRESS);
294 data_manager()->set_next_request(&request_info); 303 data_manager()->set_next_request(&request_info);
295 InitializeJobController(); 304 InitializeJobController(registration_id);
296 305
297 EXPECT_CALL(*(download_manager()), 306 EXPECT_CALL(*(download_manager()),
298 DownloadUrlMock(::testing::Pointee(::testing::Property( 307 DownloadUrlMock(::testing::Pointee(::testing::Property(
299 &DownloadUrlParameters::url, GURL(kTestUrl))))) 308 &DownloadUrlParameters::url, GURL(kTestUrl)))))
300 .Times(1); 309 .Times(1);
301 310
302 StartProcessing(); 311 StartProcessing();
303 312
304 // Get the pending download from the download manager. 313 // Get the pending download from the download manager.
305 auto& download_items = download_manager()->download_items(); 314 auto& download_items = download_manager()->download_items();
306 ASSERT_EQ(1U, download_items.size()); 315 ASSERT_EQ(1U, download_items.size());
307 FakeDownloadItem* item = download_items[0].get(); 316 FakeDownloadItem* item = download_items[0].get();
308 317
309 item->SetTargetFilePath(base::FilePath(kFileName)); 318 item->SetTargetFilePath(base::FilePath(kFileName));
310 item->SetReceivedBytes(123); 319 item->SetReceivedBytes(123);
311 item->SetState(DownloadItem::DownloadState::COMPLETE); 320 item->SetState(DownloadItem::DownloadState::COMPLETE);
312 321
313 // Trigger the observer. The JobController should update the JobData that the 322 // Trigger the observer. The JobController should update the JobData that the
314 // request is complete and should fill in storage state. 323 // request is complete and should fill in storage state.
315 ItemObserver()->OnDownloadUpdated(item); 324 ItemObserver()->OnDownloadUpdated(item);
316 325
317 EXPECT_EQ(123, request_info.received_bytes()); 326 EXPECT_EQ(123, request_info.received_bytes());
318 EXPECT_TRUE(data_manager()->IsComplete(kJobGuid)); 327 EXPECT_TRUE(data_manager()->IsComplete(kJobGuid));
319 EXPECT_TRUE(did_complete_job()); 328 EXPECT_TRUE(did_complete_job());
320 } 329 }
321 330
322 } // namespace content 331 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698