| 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 23 matching lines...) Expand all Loading... |
| 34 class BackgroundFetchJobControllerTest : public BackgroundFetchTestBase { | 34 class BackgroundFetchJobControllerTest : public BackgroundFetchTestBase { |
| 35 public: | 35 public: |
| 36 BackgroundFetchJobControllerTest() : data_manager_(browser_context()) {} | 36 BackgroundFetchJobControllerTest() : data_manager_(browser_context()) {} |
| 37 ~BackgroundFetchJobControllerTest() override = default; | 37 ~BackgroundFetchJobControllerTest() override = default; |
| 38 | 38 |
| 39 // Creates a new Background Fetch registration, whose id will be stored in | 39 // Creates a new Background Fetch registration, whose id will be stored in |
| 40 // the |*registration_id|, and registers it with the DataManager for the | 40 // the |*registration_id|, and registers it with the DataManager for the |
| 41 // included |request_data|. Should be wrapped in ASSERT_NO_FATAL_FAILURE(). | 41 // included |request_data|. Should be wrapped in ASSERT_NO_FATAL_FAILURE(). |
| 42 void CreateRegistrationForRequests( | 42 void CreateRegistrationForRequests( |
| 43 BackgroundFetchRegistrationId* registration_id, | 43 BackgroundFetchRegistrationId* registration_id, |
| 44 std::vector<scoped_refptr<BackgroundFetchRequestInfo>>* | |
| 45 out_initial_requests, | |
| 46 std::map<std::string /* url */, std::string /* method */> request_data) { | 44 std::map<std::string /* url */, std::string /* method */> request_data) { |
| 47 DCHECK(registration_id); | 45 DCHECK(registration_id); |
| 48 DCHECK(out_initial_requests); | |
| 49 | 46 |
| 50 ASSERT_TRUE(CreateRegistrationId(kExampleTag, registration_id)); | 47 ASSERT_TRUE(CreateRegistrationId(kExampleTag, registration_id)); |
| 51 | 48 |
| 52 std::vector<ServiceWorkerFetchRequest> requests; | 49 std::vector<ServiceWorkerFetchRequest> requests; |
| 53 requests.reserve(request_data.size()); | 50 requests.reserve(request_data.size()); |
| 54 | 51 |
| 55 for (const auto& pair : request_data) { | 52 for (const auto& pair : request_data) { |
| 56 requests.emplace_back(GURL(pair.first), pair.second /* method */, | 53 requests.emplace_back(GURL(pair.first), pair.second /* method */, |
| 57 ServiceWorkerHeaderMap(), Referrer(), | 54 ServiceWorkerHeaderMap(), Referrer(), |
| 58 false /* is_reload */); | 55 false /* is_reload */); |
| 59 } | 56 } |
| 60 | 57 |
| 61 blink::mojom::BackgroundFetchError error; | 58 blink::mojom::BackgroundFetchError error; |
| 62 | 59 |
| 63 base::RunLoop run_loop; | 60 base::RunLoop run_loop; |
| 64 data_manager_.CreateRegistration( | 61 data_manager_.CreateRegistration( |
| 65 *registration_id, requests, BackgroundFetchOptions(), | 62 *registration_id, requests, BackgroundFetchOptions(), |
| 66 base::BindOnce(&BackgroundFetchJobControllerTest::DidCreateRegistration, | 63 base::BindOnce(&BackgroundFetchJobControllerTest::DidCreateRegistration, |
| 67 base::Unretained(this), &error, out_initial_requests, | 64 base::Unretained(this), &error, run_loop.QuitClosure())); |
| 68 run_loop.QuitClosure())); | |
| 69 run_loop.Run(); | 65 run_loop.Run(); |
| 70 | 66 |
| 71 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE); | 67 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE); |
| 72 ASSERT_GE(out_initial_requests->size(), 1u); | |
| 73 ASSERT_LE(out_initial_requests->size(), | |
| 74 kMaximumBackgroundFetchParallelRequests); | |
| 75 | 68 |
| 76 // Provide fake responses for the given |request_data| pairs. | 69 // Provide fake responses for the given |request_data| pairs. |
| 77 for (const auto& pair : request_data) { | 70 for (const auto& pair : request_data) { |
| 78 CreateRequestWithProvidedResponse( | 71 CreateRequestWithProvidedResponse( |
| 79 pair.second, pair.first, | 72 pair.second, pair.first, |
| 80 TestResponseBuilder(200 /* response_code */).Build()); | 73 TestResponseBuilder(200 /* response_code */).Build()); |
| 81 } | 74 } |
| 82 } | 75 } |
| 83 | 76 |
| 84 // Creates a new BackgroundFetchJobController instance. | 77 // Creates a new BackgroundFetchJobController instance. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 100 | 93 |
| 101 protected: | 94 protected: |
| 102 BackgroundFetchDataManager data_manager_; | 95 BackgroundFetchDataManager data_manager_; |
| 103 bool did_complete_job_ = false; | 96 bool did_complete_job_ = false; |
| 104 | 97 |
| 105 // Closure that will be invoked when the JobController has completed all | 98 // Closure that will be invoked when the JobController has completed all |
| 106 // available jobs. Enables use of a run loop for deterministic waits. | 99 // available jobs. Enables use of a run loop for deterministic waits. |
| 107 base::OnceClosure job_completed_closure_; | 100 base::OnceClosure job_completed_closure_; |
| 108 | 101 |
| 109 private: | 102 private: |
| 110 void DidCreateRegistration( | 103 void DidCreateRegistration(blink::mojom::BackgroundFetchError* out_error, |
| 111 blink::mojom::BackgroundFetchError* out_error, | 104 const base::Closure& quit_closure, |
| 112 std::vector<scoped_refptr<BackgroundFetchRequestInfo>>* | 105 blink::mojom::BackgroundFetchError error) { |
| 113 out_initial_requests, | |
| 114 const base::Closure& quit_closure, | |
| 115 blink::mojom::BackgroundFetchError error, | |
| 116 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests) { | |
| 117 DCHECK(out_error); | 106 DCHECK(out_error); |
| 118 DCHECK(out_initial_requests); | |
| 119 | 107 |
| 120 *out_error = error; | 108 *out_error = error; |
| 121 *out_initial_requests = std::move(initial_requests); | |
| 122 | 109 |
| 123 quit_closure.Run(); | 110 quit_closure.Run(); |
| 124 } | 111 } |
| 125 | 112 |
| 126 void DidCompleteJob(BackgroundFetchJobController* controller) { | 113 void DidCompleteJob(BackgroundFetchJobController* controller) { |
| 127 DCHECK(controller); | 114 DCHECK(controller); |
| 128 EXPECT_TRUE( | 115 EXPECT_TRUE( |
| 129 controller->state() == BackgroundFetchJobController::State::ABORTED || | 116 controller->state() == BackgroundFetchJobController::State::ABORTED || |
| 130 controller->state() == BackgroundFetchJobController::State::COMPLETED); | 117 controller->state() == BackgroundFetchJobController::State::COMPLETED); |
| 131 | 118 |
| 132 did_complete_job_ = true; | 119 did_complete_job_ = true; |
| 133 | 120 |
| 134 if (job_completed_closure_) | 121 if (job_completed_closure_) |
| 135 std::move(job_completed_closure_).Run(); | 122 std::move(job_completed_closure_).Run(); |
| 136 } | 123 } |
| 137 | 124 |
| 138 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchJobControllerTest); | 125 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchJobControllerTest); |
| 139 }; | 126 }; |
| 140 | 127 |
| 141 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { | 128 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { |
| 142 BackgroundFetchRegistrationId registration_id; | 129 BackgroundFetchRegistrationId registration_id; |
| 143 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests; | |
| 144 | 130 |
| 145 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( | 131 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( |
| 146 ®istration_id, &initial_requests, | 132 ®istration_id, {{"https://example.com/funny_cat.png", "GET"}})); |
| 147 {{"https://example.com/funny_cat.png", "GET"}})); | |
| 148 | 133 |
| 149 std::unique_ptr<BackgroundFetchJobController> controller = | 134 std::unique_ptr<BackgroundFetchJobController> controller = |
| 150 CreateJobController(registration_id); | 135 CreateJobController(registration_id); |
| 151 | 136 |
| 152 EXPECT_EQ(controller->state(), | 137 EXPECT_EQ(controller->state(), |
| 153 BackgroundFetchJobController::State::INITIALIZED); | 138 BackgroundFetchJobController::State::INITIALIZED); |
| 154 | 139 |
| 155 controller->Start(initial_requests /* deliberate copy */); | 140 controller->Start(); |
| 156 EXPECT_EQ(controller->state(), BackgroundFetchJobController::State::FETCHING); | 141 EXPECT_EQ(controller->state(), BackgroundFetchJobController::State::FETCHING); |
| 157 | 142 |
| 158 // Mark the single download item as finished, completing the job. | 143 // Mark the single download item as finished, completing the job. |
| 159 { | 144 { |
| 160 base::RunLoop run_loop; | 145 base::RunLoop run_loop; |
| 161 job_completed_closure_ = run_loop.QuitClosure(); | 146 job_completed_closure_ = run_loop.QuitClosure(); |
| 162 | 147 |
| 163 run_loop.Run(); | 148 run_loop.Run(); |
| 164 } | 149 } |
| 165 | 150 |
| 166 EXPECT_EQ(controller->state(), | 151 EXPECT_EQ(controller->state(), |
| 167 BackgroundFetchJobController::State::COMPLETED); | 152 BackgroundFetchJobController::State::COMPLETED); |
| 168 EXPECT_TRUE(did_complete_job_); | 153 EXPECT_TRUE(did_complete_job_); |
| 169 } | 154 } |
| 170 | 155 |
| 171 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { | 156 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { |
| 172 BackgroundFetchRegistrationId registration_id; | 157 BackgroundFetchRegistrationId registration_id; |
| 173 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests; | |
| 174 | 158 |
| 175 // This test should always issue more requests than the number of allowed | 159 // This test should always issue more requests than the number of allowed |
| 176 // parallel requests. That way we ensure testing the iteration behaviour. | 160 // parallel requests. That way we ensure testing the iteration behaviour. |
| 177 ASSERT_GT(5u, kMaximumBackgroundFetchParallelRequests); | 161 ASSERT_GT(5u, kMaximumBackgroundFetchParallelRequests); |
| 178 | 162 |
| 179 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( | 163 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( |
| 180 ®istration_id, &initial_requests, | 164 ®istration_id, {{"https://example.com/funny_cat.png", "GET"}, |
| 181 {{"https://example.com/funny_cat.png", "GET"}, | 165 {"https://example.com/scary_cat.png", "GET"}, |
| 182 {"https://example.com/scary_cat.png", "GET"}, | 166 {"https://example.com/crazy_cat.png", "GET"}, |
| 183 {"https://example.com/crazy_cat.png", "GET"}, | 167 {"https://example.com/silly_cat.png", "GET"}, |
| 184 {"https://example.com/silly_cat.png", "GET"}, | 168 {"https://example.com/happy_cat.png", "GET"}})); |
| 185 {"https://example.com/happy_cat.png", "GET"}})); | |
| 186 | 169 |
| 187 std::unique_ptr<BackgroundFetchJobController> controller = | 170 std::unique_ptr<BackgroundFetchJobController> controller = |
| 188 CreateJobController(registration_id); | 171 CreateJobController(registration_id); |
| 189 | 172 |
| 190 EXPECT_EQ(controller->state(), | 173 EXPECT_EQ(controller->state(), |
| 191 BackgroundFetchJobController::State::INITIALIZED); | 174 BackgroundFetchJobController::State::INITIALIZED); |
| 192 | 175 |
| 193 // Continue spinning until the Job Controller has completed all the requests. | 176 // Continue spinning until the Job Controller has completed all the requests. |
| 194 // The Download Manager has been told to automatically mark them as finished. | 177 // The Download Manager has been told to automatically mark them as finished. |
| 195 { | 178 { |
| 196 base::RunLoop run_loop; | 179 base::RunLoop run_loop; |
| 197 job_completed_closure_ = run_loop.QuitClosure(); | 180 job_completed_closure_ = run_loop.QuitClosure(); |
| 198 | 181 |
| 199 controller->Start(initial_requests /* deliberate copy */); | 182 controller->Start(); |
| 200 EXPECT_EQ(controller->state(), | 183 EXPECT_EQ(controller->state(), |
| 201 BackgroundFetchJobController::State::FETCHING); | 184 BackgroundFetchJobController::State::FETCHING); |
| 202 | 185 |
| 203 run_loop.Run(); | 186 run_loop.Run(); |
| 204 } | 187 } |
| 205 | 188 |
| 206 EXPECT_EQ(controller->state(), | 189 EXPECT_EQ(controller->state(), |
| 207 BackgroundFetchJobController::State::COMPLETED); | 190 BackgroundFetchJobController::State::COMPLETED); |
| 208 EXPECT_TRUE(did_complete_job_); | 191 EXPECT_TRUE(did_complete_job_); |
| 209 } | 192 } |
| 210 | 193 |
| 211 TEST_F(BackgroundFetchJobControllerTest, AbortJob) { | 194 TEST_F(BackgroundFetchJobControllerTest, AbortJob) { |
| 212 BackgroundFetchRegistrationId registration_id; | 195 BackgroundFetchRegistrationId registration_id; |
| 213 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests; | |
| 214 | 196 |
| 215 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( | 197 ASSERT_NO_FATAL_FAILURE(CreateRegistrationForRequests( |
| 216 ®istration_id, &initial_requests, | 198 ®istration_id, {{"https://example.com/sad_cat.png", "GET"}})); |
| 217 {{"https://example.com/sad_cat.png", "GET"}})); | |
| 218 | 199 |
| 219 std::unique_ptr<BackgroundFetchJobController> controller = | 200 std::unique_ptr<BackgroundFetchJobController> controller = |
| 220 CreateJobController(registration_id); | 201 CreateJobController(registration_id); |
| 221 | 202 |
| 222 EXPECT_EQ(controller->state(), | 203 EXPECT_EQ(controller->state(), |
| 223 BackgroundFetchJobController::State::INITIALIZED); | 204 BackgroundFetchJobController::State::INITIALIZED); |
| 224 | 205 |
| 225 // Start the set of |initial_requests|, and abort them immediately after. | 206 // Start the first few requests, and abort them immediately after. |
| 226 { | 207 { |
| 227 base::RunLoop run_loop; | 208 base::RunLoop run_loop; |
| 228 job_completed_closure_ = run_loop.QuitClosure(); | 209 job_completed_closure_ = run_loop.QuitClosure(); |
| 229 | 210 |
| 230 controller->Start(initial_requests /* deliberate copy */); | 211 controller->Start(); |
| 231 EXPECT_EQ(controller->state(), | 212 EXPECT_EQ(controller->state(), |
| 232 BackgroundFetchJobController::State::FETCHING); | 213 BackgroundFetchJobController::State::FETCHING); |
| 233 | 214 |
| 234 controller->Abort(); | 215 controller->Abort(); |
| 235 | 216 |
| 236 run_loop.Run(); | 217 run_loop.Run(); |
| 237 } | 218 } |
| 238 | 219 |
| 239 // TODO(peter): Verify that the issued download items have had their state | 220 // TODO(peter): Verify that the issued download items have had their state |
| 240 // updated to be cancelled as well. | 221 // updated to be cancelled as well. |
| 241 | 222 |
| 242 EXPECT_EQ(controller->state(), BackgroundFetchJobController::State::ABORTED); | 223 EXPECT_EQ(controller->state(), BackgroundFetchJobController::State::ABORTED); |
| 243 EXPECT_TRUE(did_complete_job_); | 224 EXPECT_TRUE(did_complete_job_); |
| 244 } | 225 } |
| 245 | 226 |
| 246 } // namespace | 227 } // namespace |
| 247 } // namespace content | 228 } // namespace content |
| OLD | NEW |