| OLD | NEW |
| 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 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 | 153 |
| 154 void TearDown() override { job_controller_->Shutdown(); } | 154 void TearDown() override { job_controller_->Shutdown(); } |
| 155 | 155 |
| 156 void InitializeJobController( | 156 void InitializeJobController( |
| 157 const BackgroundFetchRegistrationId& registration_id) { | 157 const BackgroundFetchRegistrationId& registration_id) { |
| 158 job_controller_ = base::MakeUnique<BackgroundFetchJobController>( | 158 job_controller_ = base::MakeUnique<BackgroundFetchJobController>( |
| 159 registration_id, BackgroundFetchOptions(), &browser_context_, | 159 registration_id, BackgroundFetchOptions(), &browser_context_, |
| 160 BrowserContext::GetDefaultStoragePartition(&browser_context_), | 160 BrowserContext::GetDefaultStoragePartition(&browser_context_), |
| 161 data_manager_.get(), | 161 data_manager_.get(), |
| 162 base::BindOnce(&BackgroundFetchJobControllerTest::DidCompleteJob, | 162 base::BindOnce(&BackgroundFetchJobControllerTest::DidCompleteJob, |
| 163 base::Unretained(this), registration_id)); | 163 base::Unretained(this))); |
| 164 } | 164 } |
| 165 | 165 |
| 166 void DidCompleteJob( | 166 void DidCompleteJob(BackgroundFetchJobController* controller) { |
| 167 const BackgroundFetchRegistrationId& original_registration_id, | 167 DCHECK(controller); |
| 168 const BackgroundFetchRegistrationId& registration_id, | 168 EXPECT_TRUE( |
| 169 bool aborted_by_developer) { | 169 controller->state() == BackgroundFetchJobController::State::ABORTED || |
| 170 ASSERT_EQ(registration_id, original_registration_id); | 170 controller->state() == BackgroundFetchJobController::State::COMPLETED); |
| 171 did_complete_job_ = true; | |
| 172 did_abort_by_developer_ = aborted_by_developer; | |
| 173 } | 171 } |
| 174 | 172 |
| 175 void StartProcessing() { | 173 void StartProcessing() { |
| 176 base::RunLoop run_loop; | 174 base::RunLoop run_loop; |
| 177 BrowserThread::PostTask( | 175 BrowserThread::PostTask( |
| 178 BrowserThread::IO, FROM_HERE, | 176 BrowserThread::IO, FROM_HERE, |
| 179 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, | 177 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, |
| 180 base::Unretained(this), run_loop.QuitClosure())); | 178 base::Unretained(this), run_loop.QuitClosure())); |
| 181 run_loop.Run(); | 179 run_loop.Run(); |
| 182 } | 180 } |
| 183 | 181 |
| 184 void StartProcessingOnIO(const base::Closure& closure) { | 182 void StartProcessingOnIO(const base::Closure& closure) { |
| 185 job_controller_->StartProcessing(); | 183 job_controller_->StartProcessing(); |
| 186 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); | 184 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); |
| 187 } | 185 } |
| 188 | 186 |
| 189 BackgroundFetchJobController* job_controller() { | 187 BackgroundFetchJobController* job_controller() { |
| 190 return job_controller_.get(); | 188 return job_controller_.get(); |
| 191 } | 189 } |
| 190 |
| 191 BackgroundFetchJobController::State state() const { |
| 192 return job_controller_->state(); |
| 193 } |
| 194 |
| 192 MockDownloadManagerWithCallback* download_manager() { | 195 MockDownloadManagerWithCallback* download_manager() { |
| 193 return download_manager_; | 196 return download_manager_; |
| 194 } | 197 } |
| 195 | 198 |
| 196 DownloadItem::Observer* ItemObserver() const { return job_controller_.get(); } | 199 DownloadItem::Observer* ItemObserver() const { return job_controller_.get(); } |
| 197 | 200 |
| 198 bool did_complete_job() const { return did_complete_job_; } | |
| 199 bool did_abort_by_developer() const { return did_abort_by_developer_; } | |
| 200 | |
| 201 FakeBackgroundFetchDataManager* data_manager() const { | 201 FakeBackgroundFetchDataManager* data_manager() const { |
| 202 return data_manager_.get(); | 202 return data_manager_.get(); |
| 203 } | 203 } |
| 204 | 204 |
| 205 private: | 205 private: |
| 206 bool did_complete_job_ = false; | |
| 207 bool did_abort_by_developer_ = false; | |
| 208 TestBrowserThreadBundle thread_bundle_; | 206 TestBrowserThreadBundle thread_bundle_; |
| 209 TestBrowserContext browser_context_; | 207 TestBrowserContext browser_context_; |
| 210 std::unique_ptr<FakeBackgroundFetchDataManager> data_manager_; | 208 std::unique_ptr<FakeBackgroundFetchDataManager> data_manager_; |
| 211 std::unique_ptr<BackgroundFetchJobController> job_controller_; | 209 std::unique_ptr<BackgroundFetchJobController> job_controller_; |
| 212 MockDownloadManagerWithCallback* download_manager_; | 210 MockDownloadManagerWithCallback* download_manager_; |
| 213 }; | 211 }; |
| 214 | 212 |
| 215 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { | 213 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { |
| 216 BackgroundFetchRegistrationId registration_id( | 214 BackgroundFetchRegistrationId registration_id( |
| 217 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); | 215 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 232 StartProcessing(); | 230 StartProcessing(); |
| 233 | 231 |
| 234 // Get the pending download from the download manager. | 232 // Get the pending download from the download manager. |
| 235 auto& download_items = download_manager()->download_items(); | 233 auto& download_items = download_manager()->download_items(); |
| 236 ASSERT_EQ(1U, download_items.size()); | 234 ASSERT_EQ(1U, download_items.size()); |
| 237 FakeDownloadItem* item = download_items[0].get(); | 235 FakeDownloadItem* item = download_items[0].get(); |
| 238 | 236 |
| 239 // Update the observer with no actual change. | 237 // Update the observer with no actual change. |
| 240 ItemObserver()->OnDownloadUpdated(item); | 238 ItemObserver()->OnDownloadUpdated(item); |
| 241 EXPECT_EQ(DownloadItem::DownloadState::IN_PROGRESS, request_info.state()); | 239 EXPECT_EQ(DownloadItem::DownloadState::IN_PROGRESS, request_info.state()); |
| 242 EXPECT_FALSE(did_complete_job()); | 240 EXPECT_EQ(BackgroundFetchJobController::State::FETCHING, state()); |
| 243 | 241 |
| 244 // Update the item to be completed then update the observer. The JobController | 242 // Update the item to be completed then update the observer. The JobController |
| 245 // should update the JobData that the request is complete. | 243 // should update the JobData that the request is complete. |
| 246 item->SetState(DownloadItem::DownloadState::COMPLETE); | 244 item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 247 ItemObserver()->OnDownloadUpdated(item); | 245 ItemObserver()->OnDownloadUpdated(item); |
| 248 | 246 |
| 249 EXPECT_EQ(DownloadItem::DownloadState::COMPLETE, request_info.state()); | 247 EXPECT_EQ(DownloadItem::DownloadState::COMPLETE, request_info.state()); |
| 250 EXPECT_TRUE(did_complete_job()); | 248 EXPECT_EQ(BackgroundFetchJobController::State::COMPLETED, state()); |
| 251 EXPECT_FALSE(did_abort_by_developer()); | |
| 252 } | 249 } |
| 253 | 250 |
| 254 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { | 251 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { |
| 255 BackgroundFetchRegistrationId registration_id( | 252 BackgroundFetchRegistrationId registration_id( |
| 256 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); | 253 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); |
| 257 | 254 |
| 258 std::vector<BackgroundFetchRequestInfo> request_infos; | 255 std::vector<BackgroundFetchRequestInfo> request_infos; |
| 259 for (int i = 0; i < 10; i++) { | 256 for (int i = 0; i < 10; i++) { |
| 260 ServiceWorkerHeaderMap headers; | 257 ServiceWorkerHeaderMap headers; |
| 261 ServiceWorkerFetchRequest request(GURL(kTestUrl), "GET", headers, | 258 ServiceWorkerFetchRequest request(GURL(kTestUrl), "GET", headers, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 287 if (i < 9) | 284 if (i < 9) |
| 288 data_manager()->set_next_request(&request_infos[i + 1]); | 285 data_manager()->set_next_request(&request_infos[i + 1]); |
| 289 | 286 |
| 290 // Update the next item to be completed then update the observer. | 287 // Update the next item to be completed then update the observer. |
| 291 ASSERT_EQ(i + 1, download_items.size()); | 288 ASSERT_EQ(i + 1, download_items.size()); |
| 292 item = download_items[i].get(); | 289 item = download_items[i].get(); |
| 293 item->SetState(DownloadItem::DownloadState::COMPLETE); | 290 item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 294 ItemObserver()->OnDownloadUpdated(item); | 291 ItemObserver()->OnDownloadUpdated(item); |
| 295 EXPECT_EQ(DownloadItem::DownloadState::COMPLETE, request_infos[i].state()); | 292 EXPECT_EQ(DownloadItem::DownloadState::COMPLETE, request_infos[i].state()); |
| 296 } | 293 } |
| 297 EXPECT_FALSE(did_complete_job()); | 294 |
| 295 EXPECT_EQ(BackgroundFetchJobController::State::FETCHING, state()); |
| 298 | 296 |
| 299 // Finally, update the last request to be complete. The JobController should | 297 // Finally, update the last request to be complete. The JobController should |
| 300 // see that there are no more requests and mark the job as done. | 298 // see that there are no more requests and mark the job as done. |
| 301 ASSERT_EQ(10U, download_items.size()); | 299 ASSERT_EQ(10U, download_items.size()); |
| 302 item = download_items[9].get(); | 300 item = download_items[9].get(); |
| 303 item->SetState(DownloadItem::DownloadState::COMPLETE); | 301 item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 304 ItemObserver()->OnDownloadUpdated(item); | 302 ItemObserver()->OnDownloadUpdated(item); |
| 305 | 303 |
| 306 EXPECT_TRUE(did_complete_job()); | 304 EXPECT_EQ(BackgroundFetchJobController::State::COMPLETED, state()); |
| 307 EXPECT_FALSE(did_abort_by_developer()); | |
| 308 } | 305 } |
| 309 | 306 |
| 310 TEST_F(BackgroundFetchJobControllerTest, UpdateStorageState) { | 307 TEST_F(BackgroundFetchJobControllerTest, UpdateStorageState) { |
| 311 BackgroundFetchRegistrationId registration_id( | 308 BackgroundFetchRegistrationId registration_id( |
| 312 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); | 309 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); |
| 313 | 310 |
| 314 ServiceWorkerHeaderMap headers; | 311 ServiceWorkerHeaderMap headers; |
| 315 ServiceWorkerFetchRequest request(GURL(kTestUrl), "GET", headers, Referrer(), | 312 ServiceWorkerFetchRequest request(GURL(kTestUrl), "GET", headers, Referrer(), |
| 316 false /* is_reload */); | 313 false /* is_reload */); |
| 317 BackgroundFetchRequestInfo request_info(request); | 314 BackgroundFetchRequestInfo request_info(request); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 334 item->SetTargetFilePath(base::FilePath(kFileName)); | 331 item->SetTargetFilePath(base::FilePath(kFileName)); |
| 335 item->SetReceivedBytes(123); | 332 item->SetReceivedBytes(123); |
| 336 item->SetState(DownloadItem::DownloadState::COMPLETE); | 333 item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 337 | 334 |
| 338 // Trigger the observer. The JobController should update the JobData that the | 335 // Trigger the observer. The JobController should update the JobData that the |
| 339 // request is complete and should fill in storage state. | 336 // request is complete and should fill in storage state. |
| 340 ItemObserver()->OnDownloadUpdated(item); | 337 ItemObserver()->OnDownloadUpdated(item); |
| 341 | 338 |
| 342 EXPECT_EQ(123, request_info.received_bytes()); | 339 EXPECT_EQ(123, request_info.received_bytes()); |
| 343 EXPECT_TRUE(data_manager()->IsComplete(kJobGuid)); | 340 EXPECT_TRUE(data_manager()->IsComplete(kJobGuid)); |
| 344 EXPECT_TRUE(did_complete_job()); | 341 EXPECT_EQ(BackgroundFetchJobController::State::COMPLETED, state()); |
| 345 EXPECT_FALSE(did_abort_by_developer()); | |
| 346 } | 342 } |
| 347 | 343 |
| 348 } // namespace content | 344 } // namespace content |
| OLD | NEW |