| 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" |
| 11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/guid.h" | 13 #include "base/guid.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "content/browser/background_fetch/background_fetch_data_manager.h" | 17 #include "content/browser/background_fetch/background_fetch_data_manager.h" |
| 18 #include "content/browser/background_fetch/background_fetch_job_info.h" | 18 #include "content/browser/background_fetch/background_fetch_job_info.h" |
| 19 #include "content/browser/background_fetch/background_fetch_request_info.h" | 19 #include "content/browser/background_fetch/background_fetch_request_info.h" |
| 20 #include "content/common/service_worker/service_worker_types.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/test/fake_download_item.h" | 22 #include "content/public/test/fake_download_item.h" |
| 22 #include "content/public/test/mock_download_manager.h" | 23 #include "content/public/test/mock_download_manager.h" |
| 23 #include "content/public/test/test_browser_context.h" | 24 #include "content/public/test/test_browser_context.h" |
| 24 #include "content/public/test/test_browser_thread_bundle.h" | 25 #include "content/public/test/test_browser_thread_bundle.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 27 |
| 27 namespace content { | 28 namespace content { |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 TestBrowserThreadBundle thread_bundle_; | 198 TestBrowserThreadBundle thread_bundle_; |
| 198 TestBrowserContext browser_context_; | 199 TestBrowserContext browser_context_; |
| 199 std::unique_ptr<FakeBackgroundFetchDataManager> data_manager_; | 200 std::unique_ptr<FakeBackgroundFetchDataManager> data_manager_; |
| 200 std::unique_ptr<BackgroundFetchJobController> job_controller_; | 201 std::unique_ptr<BackgroundFetchJobController> job_controller_; |
| 201 MockDownloadManagerWithCallback* download_manager_; | 202 MockDownloadManagerWithCallback* download_manager_; |
| 202 }; | 203 }; |
| 203 | 204 |
| 204 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { | 205 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { |
| 205 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), | 206 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), |
| 206 kServiceWorkerRegistrationId); | 207 kServiceWorkerRegistrationId); |
| 207 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); | 208 ServiceWorkerHeaderMap headers; |
| 209 ServiceWorkerFetchRequest request(GURL(kTestUrl), "GET", headers, Referrer(), |
| 210 false /* is_reload */); |
| 211 BackgroundFetchRequestInfo request_info(request); |
| 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(); |
| 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(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 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 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), |
| 240 kServiceWorkerRegistrationId); | 244 kServiceWorkerRegistrationId); |
| 241 std::vector<BackgroundFetchRequestInfo> request_infos; | 245 std::vector<BackgroundFetchRequestInfo> request_infos; |
| 242 for (int i = 0; i < 10; i++) { | 246 for (int i = 0; i < 10; i++) { |
| 243 request_infos.emplace_back(GURL(kTestUrl), base::IntToString(i)); | 247 ServiceWorkerHeaderMap headers; |
| 248 ServiceWorkerFetchRequest request(GURL(kTestUrl), "GET", headers, |
| 249 Referrer(), false /* is_reload */); |
| 250 request_infos.emplace_back(request); |
| 244 } | 251 } |
| 245 data_manager()->set_next_request(&request_infos[0]); | 252 data_manager()->set_next_request(&request_infos[0]); |
| 246 InitializeJobController(); | 253 InitializeJobController(); |
| 247 | 254 |
| 248 EXPECT_CALL(*(download_manager()), | 255 EXPECT_CALL(*(download_manager()), |
| 249 DownloadUrlMock(::testing::Pointee(::testing::Property( | 256 DownloadUrlMock(::testing::Pointee(::testing::Property( |
| 250 &DownloadUrlParameters::url, GURL(kTestUrl))))) | 257 &DownloadUrlParameters::url, GURL(kTestUrl))))) |
| 251 .Times(10); | 258 .Times(10); |
| 252 | 259 |
| 253 StartProcessing(); | 260 StartProcessing(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 282 item = download_items[9].get(); | 289 item = download_items[9].get(); |
| 283 item->SetState(DownloadItem::DownloadState::COMPLETE); | 290 item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 284 ItemObserver()->OnDownloadUpdated(item); | 291 ItemObserver()->OnDownloadUpdated(item); |
| 285 | 292 |
| 286 EXPECT_TRUE(did_complete_job()); | 293 EXPECT_TRUE(did_complete_job()); |
| 287 } | 294 } |
| 288 | 295 |
| 289 TEST_F(BackgroundFetchJobControllerTest, UpdateStorageState) { | 296 TEST_F(BackgroundFetchJobControllerTest, UpdateStorageState) { |
| 290 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), | 297 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), |
| 291 kServiceWorkerRegistrationId); | 298 kServiceWorkerRegistrationId); |
| 292 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); | 299 ServiceWorkerHeaderMap headers; |
| 300 ServiceWorkerFetchRequest request(GURL(kTestUrl), "GET", headers, Referrer(), |
| 301 false /* is_reload */); |
| 302 BackgroundFetchRequestInfo request_info(request); |
| 293 request_info.set_state(DownloadItem::DownloadState::IN_PROGRESS); | 303 request_info.set_state(DownloadItem::DownloadState::IN_PROGRESS); |
| 294 data_manager()->set_next_request(&request_info); | 304 data_manager()->set_next_request(&request_info); |
| 295 InitializeJobController(); | 305 InitializeJobController(); |
| 296 | 306 |
| 297 EXPECT_CALL(*(download_manager()), | 307 EXPECT_CALL(*(download_manager()), |
| 298 DownloadUrlMock(::testing::Pointee(::testing::Property( | 308 DownloadUrlMock(::testing::Pointee(::testing::Property( |
| 299 &DownloadUrlParameters::url, GURL(kTestUrl))))) | 309 &DownloadUrlParameters::url, GURL(kTestUrl))))) |
| 300 .Times(1); | 310 .Times(1); |
| 301 | 311 |
| 302 StartProcessing(); | 312 StartProcessing(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 313 // Trigger the observer. The JobController should update the JobData that the | 323 // Trigger the observer. The JobController should update the JobData that the |
| 314 // request is complete and should fill in storage state. | 324 // request is complete and should fill in storage state. |
| 315 ItemObserver()->OnDownloadUpdated(item); | 325 ItemObserver()->OnDownloadUpdated(item); |
| 316 | 326 |
| 317 EXPECT_EQ(123, request_info.received_bytes()); | 327 EXPECT_EQ(123, request_info.received_bytes()); |
| 318 EXPECT_TRUE(data_manager()->IsComplete(kJobGuid)); | 328 EXPECT_TRUE(data_manager()->IsComplete(kJobGuid)); |
| 319 EXPECT_TRUE(did_complete_job()); | 329 EXPECT_TRUE(did_complete_job()); |
| 320 } | 330 } |
| 321 | 331 |
| 322 } // namespace content | 332 } // namespace content |
| OLD | NEW |