| 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 <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <unordered_map> | 9 #include <unordered_map> |
| 10 | 10 |
| 11 #include "base/guid.h" | 11 #include "base/guid.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "content/browser/background_fetch/background_fetch_constants.h" | 14 #include "content/browser/background_fetch/background_fetch_constants.h" |
| 15 #include "content/browser/background_fetch/background_fetch_data_manager.h" | 15 #include "content/browser/background_fetch/background_fetch_data_manager.h" |
| 16 #include "content/browser/background_fetch/background_fetch_registration_id.h" | 16 #include "content/browser/background_fetch/background_fetch_registration_id.h" |
| 17 #include "content/browser/background_fetch/background_fetch_test_base.h" | 17 #include "content/browser/background_fetch/background_fetch_test_base.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/download_item.h" | 19 #include "content/public/browser/download_item.h" |
| 20 #include "content/public/browser/storage_partition.h" |
| 20 #include "content/public/test/fake_download_item.h" | 21 #include "content/public/test/fake_download_item.h" |
| 21 #include "content/public/test/mock_download_manager.h" | 22 #include "content/public/test/mock_download_manager.h" |
| 23 #include "net/url_request/url_request_context_getter.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 23 | 25 |
| 24 using testing::_; | 26 using testing::_; |
| 25 | 27 |
| 26 namespace content { | 28 namespace content { |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 29 const char kExampleTag[] = "my-example-tag"; | 31 const char kExampleTag[] = "my-example-tag"; |
| 30 | 32 |
| 31 // Use the basic MockDownloadManager, but override it so that it implements the | 33 // Use the basic MockDownloadManager, but override it so that it implements the |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 154 |
| 153 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE); | 155 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE); |
| 154 ASSERT_GE(out_initial_requests->size(), 1u); | 156 ASSERT_GE(out_initial_requests->size(), 1u); |
| 155 ASSERT_LE(out_initial_requests->size(), | 157 ASSERT_LE(out_initial_requests->size(), |
| 156 kMaximumBackgroundFetchParallelRequests); | 158 kMaximumBackgroundFetchParallelRequests); |
| 157 } | 159 } |
| 158 | 160 |
| 159 // Creates a new BackgroundFetchJobController instance. | 161 // Creates a new BackgroundFetchJobController instance. |
| 160 std::unique_ptr<BackgroundFetchJobController> CreateJobController( | 162 std::unique_ptr<BackgroundFetchJobController> CreateJobController( |
| 161 const BackgroundFetchRegistrationId& registration_id) { | 163 const BackgroundFetchRegistrationId& registration_id) { |
| 164 StoragePartition* storage_partition = |
| 165 BrowserContext::GetDefaultStoragePartition(browser_context()); |
| 166 |
| 162 return base::MakeUnique<BackgroundFetchJobController>( | 167 return base::MakeUnique<BackgroundFetchJobController>( |
| 163 registration_id, BackgroundFetchOptions(), download_manager_, | 168 registration_id, BackgroundFetchOptions(), &data_manager_, |
| 164 BrowserContext::GetDefaultStoragePartition(browser_context()), | 169 download_manager_, |
| 165 &data_manager_, | 170 make_scoped_refptr(storage_partition->GetURLRequestContext()), |
| 166 base::BindOnce(&BackgroundFetchJobControllerTest::DidCompleteJob, | 171 base::BindOnce(&BackgroundFetchJobControllerTest::DidCompleteJob, |
| 167 base::Unretained(this))); | 172 base::Unretained(this))); |
| 168 } | 173 } |
| 169 | 174 |
| 170 protected: | 175 protected: |
| 171 BackgroundFetchDataManager data_manager_; | 176 BackgroundFetchDataManager data_manager_; |
| 172 MockDownloadManagerWithCallback* download_manager_; // BrowserContext owned | 177 MockDownloadManagerWithCallback* download_manager_; // BrowserContext owned |
| 173 bool did_complete_job_ = false; | 178 bool did_complete_job_ = false; |
| 174 | 179 |
| 175 // Closure that will be invoked when the JobController has completed all | 180 // Closure that will be invoked when the JobController has completed all |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 | 232 |
| 228 // Allows for proper initialization of the FakeDownloadItems. | 233 // Allows for proper initialization of the FakeDownloadItems. |
| 229 base::RunLoop().RunUntilIdle(); | 234 base::RunLoop().RunUntilIdle(); |
| 230 | 235 |
| 231 std::vector<FakeDownloadItem*> download_items = | 236 std::vector<FakeDownloadItem*> download_items = |
| 232 download_manager_->GetDownloadItems(); | 237 download_manager_->GetDownloadItems(); |
| 233 ASSERT_EQ(download_items.size(), 1u); | 238 ASSERT_EQ(download_items.size(), 1u); |
| 234 | 239 |
| 235 // Mark the single download item as finished, completing the job. | 240 // Mark the single download item as finished, completing the job. |
| 236 { | 241 { |
| 242 base::RunLoop run_loop; |
| 243 job_completed_closure_ = run_loop.QuitClosure(); |
| 244 |
| 237 FakeDownloadItem* item = download_items[0]; | 245 FakeDownloadItem* item = download_items[0]; |
| 238 | 246 |
| 239 EXPECT_EQ(DownloadItem::DownloadState::IN_PROGRESS, item->GetState()); | 247 EXPECT_EQ(DownloadItem::DownloadState::IN_PROGRESS, item->GetState()); |
| 240 | 248 |
| 241 item->SetState(DownloadItem::DownloadState::COMPLETE); | 249 item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 242 item->NotifyDownloadUpdated(); | 250 item->NotifyDownloadUpdated(); |
| 251 |
| 252 run_loop.Run(); |
| 243 } | 253 } |
| 244 | 254 |
| 245 EXPECT_EQ(controller->state(), | 255 EXPECT_EQ(controller->state(), |
| 246 BackgroundFetchJobController::State::COMPLETED); | 256 BackgroundFetchJobController::State::COMPLETED); |
| 247 EXPECT_TRUE(did_complete_job_); | 257 EXPECT_TRUE(did_complete_job_); |
| 248 } | 258 } |
| 249 | 259 |
| 250 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { | 260 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { |
| 251 BackgroundFetchRegistrationId registration_id; | 261 BackgroundFetchRegistrationId registration_id; |
| 252 std::vector<BackgroundFetchRequestInfo> initial_requests; | 262 std::vector<BackgroundFetchRequestInfo> initial_requests; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 | 308 |
| 299 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( | 309 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( |
| 300 ®istration_id, &initial_requests, | 310 ®istration_id, &initial_requests, |
| 301 {{"https://example.com/sad_cat.png", "GET"}})); | 311 {{"https://example.com/sad_cat.png", "GET"}})); |
| 302 | 312 |
| 303 std::unique_ptr<BackgroundFetchJobController> controller = | 313 std::unique_ptr<BackgroundFetchJobController> controller = |
| 304 CreateJobController(registration_id); | 314 CreateJobController(registration_id); |
| 305 | 315 |
| 306 EXPECT_EQ(controller->state(), | 316 EXPECT_EQ(controller->state(), |
| 307 BackgroundFetchJobController::State::INITIALIZED); | 317 BackgroundFetchJobController::State::INITIALIZED); |
| 308 EXPECT_CALL(*download_manager_, DownloadUrlMock(_)).Times(1); | |
| 309 | 318 |
| 310 controller->Start(initial_requests /* deliberate copy */); | 319 // Start the set of |initial_requests|, and abort them immediately after. |
| 311 EXPECT_EQ(controller->state(), BackgroundFetchJobController::State::FETCHING); | 320 { |
| 321 base::RunLoop run_loop; |
| 322 job_completed_closure_ = run_loop.QuitClosure(); |
| 312 | 323 |
| 313 controller->Abort(); | 324 controller->Start(initial_requests /* deliberate copy */); |
| 325 EXPECT_EQ(controller->state(), |
| 326 BackgroundFetchJobController::State::FETCHING); |
| 327 |
| 328 controller->Abort(); |
| 329 |
| 330 run_loop.Run(); |
| 331 } |
| 314 | 332 |
| 315 // TODO(peter): Verify that the issued download items have had their state | 333 // TODO(peter): Verify that the issued download items have had their state |
| 316 // updated to be cancelled as well. | 334 // updated to be cancelled as well. |
| 317 | 335 |
| 318 EXPECT_EQ(controller->state(), BackgroundFetchJobController::State::ABORTED); | 336 EXPECT_EQ(controller->state(), BackgroundFetchJobController::State::ABORTED); |
| 319 EXPECT_TRUE(did_complete_job_); | 337 EXPECT_TRUE(did_complete_job_); |
| 320 } | 338 } |
| 321 | 339 |
| 322 } // namespace | 340 } // namespace |
| 323 } // namespace content | 341 } // namespace content |
| OLD | NEW |