| 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 download_manager_); | 86 download_manager_); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void TearDown() override { job_controller_->Shutdown(); } | 89 void TearDown() override { job_controller_->Shutdown(); } |
| 90 | 90 |
| 91 void InitializeJobController( | 91 void InitializeJobController( |
| 92 std::unique_ptr<BackgroundFetchJobData> job_data) { | 92 std::unique_ptr<BackgroundFetchJobData> job_data) { |
| 93 job_controller_ = base::MakeUnique<BackgroundFetchJobController>( | 93 job_controller_ = base::MakeUnique<BackgroundFetchJobController>( |
| 94 kJobGuid, &browser_context_, | 94 kJobGuid, &browser_context_, |
| 95 BrowserContext::GetDefaultStoragePartition(&browser_context_), | 95 BrowserContext::GetDefaultStoragePartition(&browser_context_), |
| 96 std::move(job_data)); | 96 std::move(job_data), |
| 97 base::BindOnce(&BackgroundFetchJobControllerTest::DidCompleteJob, |
| 98 base::Unretained(this))); |
| 97 } | 99 } |
| 98 | 100 |
| 101 void DidCompleteJob() { did_complete_job_ = true; } |
| 102 |
| 99 void StartProcessing() { | 103 void StartProcessing() { |
| 100 base::RunLoop run_loop; | 104 base::RunLoop run_loop; |
| 101 BrowserThread::PostTask( | 105 BrowserThread::PostTask( |
| 102 BrowserThread::IO, FROM_HERE, | 106 BrowserThread::IO, FROM_HERE, |
| 103 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, | 107 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, |
| 104 base::Unretained(this), run_loop.QuitClosure())); | 108 base::Unretained(this), run_loop.QuitClosure())); |
| 105 run_loop.Run(); | 109 run_loop.Run(); |
| 106 } | 110 } |
| 107 | 111 |
| 108 void StartProcessingOnIO(const base::Closure& closure) { | 112 void StartProcessingOnIO(const base::Closure& closure) { |
| 109 job_controller_->StartProcessing(); | 113 job_controller_->StartProcessing(); |
| 110 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); | 114 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); |
| 111 } | 115 } |
| 112 | 116 |
| 113 BackgroundFetchJobController* job_controller() { | 117 BackgroundFetchJobController* job_controller() { |
| 114 return job_controller_.get(); | 118 return job_controller_.get(); |
| 115 } | 119 } |
| 116 MockDownloadManagerWithCallback* download_manager() { | 120 MockDownloadManagerWithCallback* download_manager() { |
| 117 return download_manager_; | 121 return download_manager_; |
| 118 } | 122 } |
| 119 | 123 |
| 120 DownloadItem::Observer* ItemObserver() const { return job_controller_.get(); } | 124 DownloadItem::Observer* ItemObserver() const { return job_controller_.get(); } |
| 121 | 125 |
| 126 bool did_complete_job() const { return did_complete_job_; } |
| 127 |
| 122 private: | 128 private: |
| 129 bool did_complete_job_ = false; |
| 123 TestBrowserThreadBundle thread_bundle_; | 130 TestBrowserThreadBundle thread_bundle_; |
| 124 TestBrowserContext browser_context_; | 131 TestBrowserContext browser_context_; |
| 125 std::unique_ptr<BackgroundFetchJobController> job_controller_; | 132 std::unique_ptr<BackgroundFetchJobController> job_controller_; |
| 126 MockDownloadManagerWithCallback* download_manager_; | 133 MockDownloadManagerWithCallback* download_manager_; |
| 127 }; | 134 }; |
| 128 | 135 |
| 129 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { | 136 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { |
| 130 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), | 137 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), |
| 131 kServiceWorkerRegistrationId); | 138 kServiceWorkerRegistrationId); |
| 132 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); | 139 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 147 StartProcessing(); | 154 StartProcessing(); |
| 148 | 155 |
| 149 // Get one of the pending downloads from the download manager. | 156 // Get one of the pending downloads from the download manager. |
| 150 auto& download_items = download_manager()->download_items(); | 157 auto& download_items = download_manager()->download_items(); |
| 151 ASSERT_EQ(1U, download_items.size()); | 158 ASSERT_EQ(1U, download_items.size()); |
| 152 FakeDownloadItem* item = download_items[0].get(); | 159 FakeDownloadItem* item = download_items[0].get(); |
| 153 | 160 |
| 154 // Update the observer with no actual change. | 161 // Update the observer with no actual change. |
| 155 ItemObserver()->OnDownloadUpdated(item); | 162 ItemObserver()->OnDownloadUpdated(item); |
| 156 EXPECT_FALSE(job_data->IsComplete()); | 163 EXPECT_FALSE(job_data->IsComplete()); |
| 164 EXPECT_FALSE(did_complete_job()); |
| 157 | 165 |
| 158 // Update the item to be completed then update the observer. The JobController | 166 // Update the item to be completed then update the observer. The JobController |
| 159 // should update the JobData that the request is complete. | 167 // should update the JobData that the request is complete. |
| 160 item->SetState(DownloadItem::DownloadState::COMPLETE); | 168 item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 161 ItemObserver()->OnDownloadUpdated(item); | 169 ItemObserver()->OnDownloadUpdated(item); |
| 162 EXPECT_TRUE(job_data->IsComplete()); | 170 EXPECT_TRUE(job_data->IsComplete()); |
| 171 |
| 172 EXPECT_TRUE(did_complete_job()); |
| 163 } | 173 } |
| 164 | 174 |
| 165 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { | 175 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { |
| 166 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), | 176 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), |
| 167 kServiceWorkerRegistrationId); | 177 kServiceWorkerRegistrationId); |
| 168 std::vector<BackgroundFetchRequestInfo> request_infos; | 178 std::vector<BackgroundFetchRequestInfo> request_infos; |
| 169 for (int i = 0; i < 10; i++) { | 179 for (int i = 0; i < 10; i++) { |
| 170 request_infos.emplace_back(GURL(kTestUrl), base::IntToString(i)); | 180 request_infos.emplace_back(GURL(kTestUrl), base::IntToString(i)); |
| 171 } | 181 } |
| 172 | 182 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 196 | 206 |
| 197 for (size_t i = 0; i < 9; i++) { | 207 for (size_t i = 0; i < 9; i++) { |
| 198 // Update the next item to be completed then update the observer. | 208 // Update the next item to be completed then update the observer. |
| 199 ASSERT_EQ(i + 1, download_items.size()); | 209 ASSERT_EQ(i + 1, download_items.size()); |
| 200 item = download_items[i].get(); | 210 item = download_items[i].get(); |
| 201 item->SetState(DownloadItem::DownloadState::COMPLETE); | 211 item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 202 ItemObserver()->OnDownloadUpdated(item); | 212 ItemObserver()->OnDownloadUpdated(item); |
| 203 EXPECT_FALSE(job_data->IsComplete()); | 213 EXPECT_FALSE(job_data->IsComplete()); |
| 204 } | 214 } |
| 205 EXPECT_FALSE(job_data->HasRequestsRemaining()); | 215 EXPECT_FALSE(job_data->HasRequestsRemaining()); |
| 216 EXPECT_FALSE(did_complete_job()); |
| 206 | 217 |
| 207 // Finally, update the last request to be complete. The JobController should | 218 // Finally, update the last request to be complete. The JobController should |
| 208 // see that there are no more requests and mark the job as done. | 219 // see that there are no more requests and mark the job as done. |
| 209 ASSERT_EQ(10U, download_items.size()); | 220 ASSERT_EQ(10U, download_items.size()); |
| 210 item = download_items[9].get(); | 221 item = download_items[9].get(); |
| 211 item->SetState(DownloadItem::DownloadState::COMPLETE); | 222 item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 212 ItemObserver()->OnDownloadUpdated(item); | 223 ItemObserver()->OnDownloadUpdated(item); |
| 213 EXPECT_TRUE(job_data->IsComplete()); | 224 EXPECT_TRUE(job_data->IsComplete()); |
| 225 |
| 226 EXPECT_TRUE(did_complete_job()); |
| 214 } | 227 } |
| 215 | 228 |
| 216 } // namespace content | 229 } // namespace content |
| OLD | NEW |