| 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 "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 const char kTestUrl[] = "http://www.example.com/example.html"; | 25 const char kTestUrl[] = "http://www.example.com/example.html"; |
| 26 const char kTag[] = "testTag"; | 26 const char kTag[] = "testTag"; |
| 27 | 27 |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 namespace content { | 30 namespace content { |
| 31 | 31 |
| 32 class BackgroundFetchJobControllerTest : public ::testing::Test { | 32 class BackgroundFetchJobControllerTest : public ::testing::Test { |
| 33 public: | 33 public: |
| 34 BackgroundFetchJobControllerTest() | 34 BackgroundFetchJobControllerTest() |
| 35 : job_controller_( | 35 : download_manager_(new MockDownloadManager()) {} |
| 36 &browser_context_, | |
| 37 BrowserContext::GetDefaultStoragePartition(&browser_context_)), | |
| 38 download_manager_(new MockDownloadManager()) {} | |
| 39 ~BackgroundFetchJobControllerTest() override = default; | 36 ~BackgroundFetchJobControllerTest() override = default; |
| 40 | 37 |
| 41 void SetUp() override { | 38 void SetUp() override { |
| 42 // The download_manager_ ownership is given to the BrowserContext, and the | 39 // The download_manager_ ownership is given to the BrowserContext, and the |
| 43 // BrowserContext will take care of deallocating it. | 40 // BrowserContext will take care of deallocating it. |
| 44 BrowserContext::SetDownloadManagerForTesting(&browser_context_, | 41 BrowserContext::SetDownloadManagerForTesting(&browser_context_, |
| 45 download_manager_); | 42 download_manager_); |
| 46 } | 43 } |
| 47 | 44 |
| 48 void ProcessJob(const std::string& job_guid, | 45 void InitializeJobController( |
| 49 BackgroundFetchJobData* job_data) { | 46 std::unique_ptr<BackgroundFetchJobData> job_data) { |
| 47 job_controller_ = base::MakeUnique<BackgroundFetchJobController>( |
| 48 kJobGuid, &browser_context_, |
| 49 BrowserContext::GetDefaultStoragePartition(&browser_context_), |
| 50 std::move(job_data)); |
| 51 } |
| 52 |
| 53 void StartProcessing() { |
| 50 base::RunLoop run_loop; | 54 base::RunLoop run_loop; |
| 51 BrowserThread::PostTask( | 55 BrowserThread::PostTask( |
| 52 BrowserThread::IO, FROM_HERE, | 56 BrowserThread::IO, FROM_HERE, |
| 53 base::Bind(&BackgroundFetchJobControllerTest::ProcessJobOnIO, | 57 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, |
| 54 base::Unretained(this), job_guid, job_data, | 58 base::Unretained(this), run_loop.QuitClosure())); |
| 55 run_loop.QuitClosure())); | |
| 56 run_loop.Run(); | 59 run_loop.Run(); |
| 57 } | 60 } |
| 58 | 61 |
| 59 void ProcessJobOnIO(const std::string& job_guid, | 62 void StartProcessingOnIO(const base::Closure& closure) { |
| 60 BackgroundFetchJobData* job_data, | 63 job_controller_->StartProcessing(); |
| 61 const base::Closure& closure) { | |
| 62 job_controller_.ProcessJob(job_guid, job_data); | |
| 63 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); | 64 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); |
| 64 } | 65 } |
| 65 | 66 |
| 66 BackgroundFetchJobController* job_controller() { return &job_controller_; } | 67 BackgroundFetchJobController* job_controller() { |
| 68 return job_controller_.get(); |
| 69 } |
| 67 MockDownloadManager* download_manager() { return download_manager_; } | 70 MockDownloadManager* download_manager() { return download_manager_; } |
| 68 | 71 |
| 69 private: | 72 private: |
| 70 TestBrowserThreadBundle thread_bundle_; | 73 TestBrowserThreadBundle thread_bundle_; |
| 71 TestBrowserContext browser_context_; | 74 TestBrowserContext browser_context_; |
| 72 BackgroundFetchJobController job_controller_; | 75 std::unique_ptr<BackgroundFetchJobController> job_controller_; |
| 73 MockDownloadManager* download_manager_; | 76 MockDownloadManager* download_manager_; |
| 74 }; | 77 }; |
| 75 | 78 |
| 76 TEST_F(BackgroundFetchJobControllerTest, StartDownload) { | 79 TEST_F(BackgroundFetchJobControllerTest, StartDownload) { |
| 77 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), | 80 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), |
| 78 kServiceWorkerRegistrationId); | 81 kServiceWorkerRegistrationId); |
| 79 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); | 82 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); |
| 80 std::vector<BackgroundFetchRequestInfo> request_infos{request_info}; | 83 std::vector<BackgroundFetchRequestInfo> request_infos{request_info}; |
| 81 | 84 |
| 82 // Get a JobData to give to the JobController. The JobController then gets | 85 // Get a JobData to give to the JobController. The JobController then gets |
| 83 // the BackgroundFetchRequestInfos from the JobData. | 86 // the BackgroundFetchRequestInfos from the JobData. |
| 84 BackgroundFetchJobData job_data(request_infos); | 87 std::unique_ptr<BackgroundFetchJobData> job_data = |
| 88 base::MakeUnique<BackgroundFetchJobData>(request_infos); |
| 89 InitializeJobController(std::move(job_data)); |
| 85 | 90 |
| 86 EXPECT_CALL(*(download_manager()), | 91 EXPECT_CALL(*(download_manager()), |
| 87 DownloadUrlMock(::testing::Pointee(::testing::Property( | 92 DownloadUrlMock(::testing::Pointee(::testing::Property( |
| 88 &DownloadUrlParameters::url, GURL(kTestUrl))))) | 93 &DownloadUrlParameters::url, GURL(kTestUrl))))) |
| 89 .Times(1); | 94 .Times(1); |
| 90 | 95 |
| 91 ProcessJob(kJobGuid, &job_data); | 96 StartProcessing(); |
| 92 } | 97 } |
| 93 | 98 |
| 94 } // namespace content | 99 } // namespace content |
| OLD | NEW |