| 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 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 // BrowserContext will take care of deallocating it. | 149 // BrowserContext will take care of deallocating it. |
| 150 BrowserContext::SetDownloadManagerForTesting(&browser_context_, | 150 BrowserContext::SetDownloadManagerForTesting(&browser_context_, |
| 151 download_manager_); | 151 download_manager_); |
| 152 } | 152 } |
| 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, &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), registration_id)); |
| 164 } | 164 } |
| 165 | 165 |
| 166 void DidCompleteJob( | 166 void DidCompleteJob( |
| 167 const BackgroundFetchRegistrationId& original_registration_id, |
| 167 const BackgroundFetchRegistrationId& registration_id, | 168 const BackgroundFetchRegistrationId& registration_id, |
| 168 const BackgroundFetchRegistrationId& original_registration_id) { | 169 bool aborted_by_developer) { |
| 169 ASSERT_EQ(registration_id, original_registration_id); | 170 ASSERT_EQ(registration_id, original_registration_id); |
| 170 did_complete_job_ = true; | 171 did_complete_job_ = true; |
| 172 did_abort_by_developer_ = aborted_by_developer; |
| 171 } | 173 } |
| 172 | 174 |
| 173 void StartProcessing() { | 175 void StartProcessing() { |
| 174 base::RunLoop run_loop; | 176 base::RunLoop run_loop; |
| 175 BrowserThread::PostTask( | 177 BrowserThread::PostTask( |
| 176 BrowserThread::IO, FROM_HERE, | 178 BrowserThread::IO, FROM_HERE, |
| 177 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, | 179 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, |
| 178 base::Unretained(this), run_loop.QuitClosure())); | 180 base::Unretained(this), run_loop.QuitClosure())); |
| 179 run_loop.Run(); | 181 run_loop.Run(); |
| 180 } | 182 } |
| 181 | 183 |
| 182 void StartProcessingOnIO(const base::Closure& closure) { | 184 void StartProcessingOnIO(const base::Closure& closure) { |
| 183 job_controller_->StartProcessing(); | 185 job_controller_->StartProcessing(); |
| 184 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); | 186 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); |
| 185 } | 187 } |
| 186 | 188 |
| 187 BackgroundFetchJobController* job_controller() { | 189 BackgroundFetchJobController* job_controller() { |
| 188 return job_controller_.get(); | 190 return job_controller_.get(); |
| 189 } | 191 } |
| 190 MockDownloadManagerWithCallback* download_manager() { | 192 MockDownloadManagerWithCallback* download_manager() { |
| 191 return download_manager_; | 193 return download_manager_; |
| 192 } | 194 } |
| 193 | 195 |
| 194 DownloadItem::Observer* ItemObserver() const { return job_controller_.get(); } | 196 DownloadItem::Observer* ItemObserver() const { return job_controller_.get(); } |
| 195 | 197 |
| 196 bool did_complete_job() const { return did_complete_job_; } | 198 bool did_complete_job() const { return did_complete_job_; } |
| 199 bool did_abort_by_developer() const { return did_abort_by_developer_; } |
| 197 | 200 |
| 198 FakeBackgroundFetchDataManager* data_manager() const { | 201 FakeBackgroundFetchDataManager* data_manager() const { |
| 199 return data_manager_.get(); | 202 return data_manager_.get(); |
| 200 } | 203 } |
| 201 | 204 |
| 202 private: | 205 private: |
| 203 bool did_complete_job_ = false; | 206 bool did_complete_job_ = false; |
| 207 bool did_abort_by_developer_ = false; |
| 204 TestBrowserThreadBundle thread_bundle_; | 208 TestBrowserThreadBundle thread_bundle_; |
| 205 TestBrowserContext browser_context_; | 209 TestBrowserContext browser_context_; |
| 206 std::unique_ptr<FakeBackgroundFetchDataManager> data_manager_; | 210 std::unique_ptr<FakeBackgroundFetchDataManager> data_manager_; |
| 207 std::unique_ptr<BackgroundFetchJobController> job_controller_; | 211 std::unique_ptr<BackgroundFetchJobController> job_controller_; |
| 208 MockDownloadManagerWithCallback* download_manager_; | 212 MockDownloadManagerWithCallback* download_manager_; |
| 209 }; | 213 }; |
| 210 | 214 |
| 211 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { | 215 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { |
| 212 BackgroundFetchRegistrationId registration_id( | 216 BackgroundFetchRegistrationId registration_id( |
| 213 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); | 217 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 237 EXPECT_EQ(DownloadItem::DownloadState::IN_PROGRESS, request_info.state()); | 241 EXPECT_EQ(DownloadItem::DownloadState::IN_PROGRESS, request_info.state()); |
| 238 EXPECT_FALSE(did_complete_job()); | 242 EXPECT_FALSE(did_complete_job()); |
| 239 | 243 |
| 240 // Update the item to be completed then update the observer. The JobController | 244 // Update the item to be completed then update the observer. The JobController |
| 241 // should update the JobData that the request is complete. | 245 // should update the JobData that the request is complete. |
| 242 item->SetState(DownloadItem::DownloadState::COMPLETE); | 246 item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 243 ItemObserver()->OnDownloadUpdated(item); | 247 ItemObserver()->OnDownloadUpdated(item); |
| 244 | 248 |
| 245 EXPECT_EQ(DownloadItem::DownloadState::COMPLETE, request_info.state()); | 249 EXPECT_EQ(DownloadItem::DownloadState::COMPLETE, request_info.state()); |
| 246 EXPECT_TRUE(did_complete_job()); | 250 EXPECT_TRUE(did_complete_job()); |
| 251 EXPECT_FALSE(did_abort_by_developer()); |
| 247 } | 252 } |
| 248 | 253 |
| 249 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { | 254 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { |
| 250 BackgroundFetchRegistrationId registration_id( | 255 BackgroundFetchRegistrationId registration_id( |
| 251 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); | 256 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); |
| 252 | 257 |
| 253 std::vector<BackgroundFetchRequestInfo> request_infos; | 258 std::vector<BackgroundFetchRequestInfo> request_infos; |
| 254 for (int i = 0; i < 10; i++) { | 259 for (int i = 0; i < 10; i++) { |
| 255 ServiceWorkerHeaderMap headers; | 260 ServiceWorkerHeaderMap headers; |
| 256 ServiceWorkerFetchRequest request(GURL(kTestUrl), "GET", headers, | 261 ServiceWorkerFetchRequest request(GURL(kTestUrl), "GET", headers, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 EXPECT_FALSE(did_complete_job()); | 297 EXPECT_FALSE(did_complete_job()); |
| 293 | 298 |
| 294 // Finally, update the last request to be complete. The JobController should | 299 // Finally, update the last request to be complete. The JobController should |
| 295 // see that there are no more requests and mark the job as done. | 300 // see that there are no more requests and mark the job as done. |
| 296 ASSERT_EQ(10U, download_items.size()); | 301 ASSERT_EQ(10U, download_items.size()); |
| 297 item = download_items[9].get(); | 302 item = download_items[9].get(); |
| 298 item->SetState(DownloadItem::DownloadState::COMPLETE); | 303 item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 299 ItemObserver()->OnDownloadUpdated(item); | 304 ItemObserver()->OnDownloadUpdated(item); |
| 300 | 305 |
| 301 EXPECT_TRUE(did_complete_job()); | 306 EXPECT_TRUE(did_complete_job()); |
| 307 EXPECT_FALSE(did_abort_by_developer()); |
| 302 } | 308 } |
| 303 | 309 |
| 304 TEST_F(BackgroundFetchJobControllerTest, UpdateStorageState) { | 310 TEST_F(BackgroundFetchJobControllerTest, UpdateStorageState) { |
| 305 BackgroundFetchRegistrationId registration_id( | 311 BackgroundFetchRegistrationId registration_id( |
| 306 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); | 312 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); |
| 307 | 313 |
| 308 ServiceWorkerHeaderMap headers; | 314 ServiceWorkerHeaderMap headers; |
| 309 ServiceWorkerFetchRequest request(GURL(kTestUrl), "GET", headers, Referrer(), | 315 ServiceWorkerFetchRequest request(GURL(kTestUrl), "GET", headers, Referrer(), |
| 310 false /* is_reload */); | 316 false /* is_reload */); |
| 311 BackgroundFetchRequestInfo request_info(request); | 317 BackgroundFetchRequestInfo request_info(request); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 329 item->SetReceivedBytes(123); | 335 item->SetReceivedBytes(123); |
| 330 item->SetState(DownloadItem::DownloadState::COMPLETE); | 336 item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 331 | 337 |
| 332 // Trigger the observer. The JobController should update the JobData that the | 338 // Trigger the observer. The JobController should update the JobData that the |
| 333 // request is complete and should fill in storage state. | 339 // request is complete and should fill in storage state. |
| 334 ItemObserver()->OnDownloadUpdated(item); | 340 ItemObserver()->OnDownloadUpdated(item); |
| 335 | 341 |
| 336 EXPECT_EQ(123, request_info.received_bytes()); | 342 EXPECT_EQ(123, request_info.received_bytes()); |
| 337 EXPECT_TRUE(data_manager()->IsComplete(kJobGuid)); | 343 EXPECT_TRUE(data_manager()->IsComplete(kJobGuid)); |
| 338 EXPECT_TRUE(did_complete_job()); | 344 EXPECT_TRUE(did_complete_job()); |
| 345 EXPECT_FALSE(did_abort_by_developer()); |
| 339 } | 346 } |
| 340 | 347 |
| 341 } // namespace content | 348 } // namespace content |
| OLD | NEW |