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 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 // BrowserContext will take care of deallocating it. | 119 // BrowserContext will take care of deallocating it. |
120 BrowserContext::SetDownloadManagerForTesting(browser_context(), | 120 BrowserContext::SetDownloadManagerForTesting(browser_context(), |
121 download_manager_); | 121 download_manager_); |
122 } | 122 } |
123 | 123 |
124 // Creates a new Background Fetch registration, whose id will be stored in | 124 // Creates a new Background Fetch registration, whose id will be stored in |
125 // the |*registration_id|, and registers it with the DataManager for the | 125 // the |*registration_id|, and registers it with the DataManager for the |
126 // included |request_data|. Should be wrapped in ASSERT_NO_FATAL_FAILURE(). | 126 // included |request_data|. Should be wrapped in ASSERT_NO_FATAL_FAILURE(). |
127 void CreateRegistrationForRequests( | 127 void CreateRegistrationForRequests( |
128 BackgroundFetchRegistrationId* registration_id, | 128 BackgroundFetchRegistrationId* registration_id, |
129 std::vector<BackgroundFetchRequestInfo>* out_initial_requests, | 129 std::vector<scoped_refptr<BackgroundFetchRequestInfo>>* |
| 130 out_initial_requests, |
130 std::map<std::string /* url */, std::string /* method */> request_data) { | 131 std::map<std::string /* url */, std::string /* method */> request_data) { |
131 DCHECK(registration_id); | 132 DCHECK(registration_id); |
132 DCHECK(out_initial_requests); | 133 DCHECK(out_initial_requests); |
133 | 134 |
134 ASSERT_TRUE(CreateRegistrationId(kExampleTag, registration_id)); | 135 ASSERT_TRUE(CreateRegistrationId(kExampleTag, registration_id)); |
135 | 136 |
136 std::vector<ServiceWorkerFetchRequest> requests; | 137 std::vector<ServiceWorkerFetchRequest> requests; |
137 requests.reserve(request_data.size()); | 138 requests.reserve(request_data.size()); |
138 | 139 |
139 for (const auto& pair : request_data) { | 140 for (const auto& pair : request_data) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 MockDownloadManagerWithCallback* download_manager_; // BrowserContext owned | 178 MockDownloadManagerWithCallback* download_manager_; // BrowserContext owned |
178 bool did_complete_job_ = false; | 179 bool did_complete_job_ = false; |
179 | 180 |
180 // Closure that will be invoked when the JobController has completed all | 181 // Closure that will be invoked when the JobController has completed all |
181 // available jobs. Enables use of a run loop for deterministic waits. | 182 // available jobs. Enables use of a run loop for deterministic waits. |
182 base::OnceClosure job_completed_closure_; | 183 base::OnceClosure job_completed_closure_; |
183 | 184 |
184 private: | 185 private: |
185 void DidCreateRegistration( | 186 void DidCreateRegistration( |
186 blink::mojom::BackgroundFetchError* out_error, | 187 blink::mojom::BackgroundFetchError* out_error, |
187 std::vector<BackgroundFetchRequestInfo>* out_initial_requests, | 188 std::vector<scoped_refptr<BackgroundFetchRequestInfo>>* |
| 189 out_initial_requests, |
188 const base::Closure& quit_closure, | 190 const base::Closure& quit_closure, |
189 blink::mojom::BackgroundFetchError error, | 191 blink::mojom::BackgroundFetchError error, |
190 std::vector<BackgroundFetchRequestInfo> initial_requests) { | 192 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests) { |
191 DCHECK(out_error); | 193 DCHECK(out_error); |
192 DCHECK(out_initial_requests); | 194 DCHECK(out_initial_requests); |
193 | 195 |
194 *out_error = error; | 196 *out_error = error; |
195 *out_initial_requests = std::move(initial_requests); | 197 *out_initial_requests = std::move(initial_requests); |
196 | 198 |
197 quit_closure.Run(); | 199 quit_closure.Run(); |
198 } | 200 } |
199 | 201 |
200 void DidCompleteJob(BackgroundFetchJobController* controller) { | 202 void DidCompleteJob(BackgroundFetchJobController* controller) { |
201 DCHECK(controller); | 203 DCHECK(controller); |
202 EXPECT_TRUE( | 204 EXPECT_TRUE( |
203 controller->state() == BackgroundFetchJobController::State::ABORTED || | 205 controller->state() == BackgroundFetchJobController::State::ABORTED || |
204 controller->state() == BackgroundFetchJobController::State::COMPLETED); | 206 controller->state() == BackgroundFetchJobController::State::COMPLETED); |
205 | 207 |
206 did_complete_job_ = true; | 208 did_complete_job_ = true; |
207 | 209 |
208 if (job_completed_closure_) | 210 if (job_completed_closure_) |
209 std::move(job_completed_closure_).Run(); | 211 std::move(job_completed_closure_).Run(); |
210 } | 212 } |
211 | 213 |
212 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchJobControllerTest); | 214 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchJobControllerTest); |
213 }; | 215 }; |
214 | 216 |
215 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { | 217 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { |
216 BackgroundFetchRegistrationId registration_id; | 218 BackgroundFetchRegistrationId registration_id; |
217 std::vector<BackgroundFetchRequestInfo> initial_requests; | 219 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests; |
218 | 220 |
219 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( | 221 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( |
220 ®istration_id, &initial_requests, | 222 ®istration_id, &initial_requests, |
221 {{"https://example.com/funny_cat.png", "GET"}})); | 223 {{"https://example.com/funny_cat.png", "GET"}})); |
222 | 224 |
223 std::unique_ptr<BackgroundFetchJobController> controller = | 225 std::unique_ptr<BackgroundFetchJobController> controller = |
224 CreateJobController(registration_id); | 226 CreateJobController(registration_id); |
225 | 227 |
226 EXPECT_EQ(controller->state(), | 228 EXPECT_EQ(controller->state(), |
227 BackgroundFetchJobController::State::INITIALIZED); | 229 BackgroundFetchJobController::State::INITIALIZED); |
(...skipping 24 matching lines...) Expand all Loading... |
252 run_loop.Run(); | 254 run_loop.Run(); |
253 } | 255 } |
254 | 256 |
255 EXPECT_EQ(controller->state(), | 257 EXPECT_EQ(controller->state(), |
256 BackgroundFetchJobController::State::COMPLETED); | 258 BackgroundFetchJobController::State::COMPLETED); |
257 EXPECT_TRUE(did_complete_job_); | 259 EXPECT_TRUE(did_complete_job_); |
258 } | 260 } |
259 | 261 |
260 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { | 262 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { |
261 BackgroundFetchRegistrationId registration_id; | 263 BackgroundFetchRegistrationId registration_id; |
262 std::vector<BackgroundFetchRequestInfo> initial_requests; | 264 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests; |
263 | 265 |
264 // This test should always issue more requests than the number of allowed | 266 // This test should always issue more requests than the number of allowed |
265 // parallel requests. That way we ensure testing the iteration behaviour. | 267 // parallel requests. That way we ensure testing the iteration behaviour. |
266 ASSERT_GT(5u, kMaximumBackgroundFetchParallelRequests); | 268 ASSERT_GT(5u, kMaximumBackgroundFetchParallelRequests); |
267 | 269 |
268 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( | 270 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( |
269 ®istration_id, &initial_requests, | 271 ®istration_id, &initial_requests, |
270 {{"https://example.com/funny_cat.png", "GET"}, | 272 {{"https://example.com/funny_cat.png", "GET"}, |
271 {"https://example.com/scary_cat.png", "GET"}, | 273 {"https://example.com/scary_cat.png", "GET"}, |
272 {"https://example.com/crazy_cat.png", "GET"}, | 274 {"https://example.com/crazy_cat.png", "GET"}, |
(...skipping 24 matching lines...) Expand all Loading... |
297 run_loop.Run(); | 299 run_loop.Run(); |
298 } | 300 } |
299 | 301 |
300 EXPECT_EQ(controller->state(), | 302 EXPECT_EQ(controller->state(), |
301 BackgroundFetchJobController::State::COMPLETED); | 303 BackgroundFetchJobController::State::COMPLETED); |
302 EXPECT_TRUE(did_complete_job_); | 304 EXPECT_TRUE(did_complete_job_); |
303 } | 305 } |
304 | 306 |
305 TEST_F(BackgroundFetchJobControllerTest, AbortJob) { | 307 TEST_F(BackgroundFetchJobControllerTest, AbortJob) { |
306 BackgroundFetchRegistrationId registration_id; | 308 BackgroundFetchRegistrationId registration_id; |
307 std::vector<BackgroundFetchRequestInfo> initial_requests; | 309 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests; |
308 | 310 |
309 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( | 311 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( |
310 ®istration_id, &initial_requests, | 312 ®istration_id, &initial_requests, |
311 {{"https://example.com/sad_cat.png", "GET"}})); | 313 {{"https://example.com/sad_cat.png", "GET"}})); |
312 | 314 |
313 std::unique_ptr<BackgroundFetchJobController> controller = | 315 std::unique_ptr<BackgroundFetchJobController> controller = |
314 CreateJobController(registration_id); | 316 CreateJobController(registration_id); |
315 | 317 |
316 EXPECT_EQ(controller->state(), | 318 EXPECT_EQ(controller->state(), |
317 BackgroundFetchJobController::State::INITIALIZED); | 319 BackgroundFetchJobController::State::INITIALIZED); |
(...skipping 14 matching lines...) Expand all Loading... |
332 | 334 |
333 // TODO(peter): Verify that the issued download items have had their state | 335 // TODO(peter): Verify that the issued download items have had their state |
334 // updated to be cancelled as well. | 336 // updated to be cancelled as well. |
335 | 337 |
336 EXPECT_EQ(controller->state(), BackgroundFetchJobController::State::ABORTED); | 338 EXPECT_EQ(controller->state(), BackgroundFetchJobController::State::ABORTED); |
337 EXPECT_TRUE(did_complete_job_); | 339 EXPECT_TRUE(did_complete_job_); |
338 } | 340 } |
339 | 341 |
340 } // namespace | 342 } // namespace |
341 } // namespace content | 343 } // namespace content |
OLD | NEW |