OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/offline_pages/background/request_queue.h" | 5 #include "components/offline_pages/background/request_queue.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/test/test_simple_task_runner.h" | 10 #include "base/test/test_simple_task_runner.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 void SetUp() override; | 42 void SetUp() override; |
43 | 43 |
44 void PumpLoop(); | 44 void PumpLoop(); |
45 | 45 |
46 // Callback for adding requests. | 46 // Callback for adding requests. |
47 void AddRequestDone(AddRequestResult result, const SavePageRequest& request); | 47 void AddRequestDone(AddRequestResult result, const SavePageRequest& request); |
48 // Callback for getting requests. | 48 // Callback for getting requests. |
49 void GetRequestsDone(GetRequestsResult result, | 49 void GetRequestsDone(GetRequestsResult result, |
50 const std::vector<SavePageRequest>& requests); | 50 const std::vector<SavePageRequest>& requests); |
51 // Callback for removing request. | 51 // Callback for removing request. |
52 void RemoveRequestDone( | 52 void RemoveRequestsDone( |
53 const RequestQueue::UpdateMultipleRequestResults& results); | 53 const RequestQueue::UpdateMultipleRequestResults& results, |
| 54 const std::vector<SavePageRequest>& requests); |
| 55 |
| 56 void UpdateMultipleRequestsDone( |
| 57 const RequestQueue::UpdateMultipleRequestResults& results, |
| 58 const std::vector<SavePageRequest>& requests); |
54 | 59 |
55 void UpdateRequestDone(UpdateRequestResult result); | 60 void UpdateRequestDone(UpdateRequestResult result); |
56 | 61 |
57 RequestQueue* queue() { return queue_.get(); } | 62 RequestQueue* queue() { return queue_.get(); } |
58 | 63 |
59 AddRequestResult last_add_result() const { return last_add_result_; } | 64 AddRequestResult last_add_result() const { return last_add_result_; } |
60 SavePageRequest* last_added_request() { | 65 SavePageRequest* last_added_request() { |
61 return last_added_request_.get(); | 66 return last_added_request_.get(); |
62 } | 67 } |
63 | 68 |
64 const RequestQueue::UpdateMultipleRequestResults& last_remove_results() | 69 const RequestQueue::UpdateMultipleRequestResults& last_remove_results() |
65 const { | 70 const { |
66 return last_remove_results_; | 71 return last_remove_results_; |
67 } | 72 } |
68 | 73 |
| 74 const RequestQueue::UpdateMultipleRequestResults& |
| 75 last_multiple_update_results() const { |
| 76 return last_multiple_update_results_; |
| 77 } |
| 78 |
69 UpdateRequestResult last_update_result() const { return last_update_result_; } | 79 UpdateRequestResult last_update_result() const { return last_update_result_; } |
70 | 80 |
71 GetRequestsResult last_get_requests_result() const { | 81 GetRequestsResult last_get_requests_result() const { |
72 return last_get_requests_result_; | 82 return last_get_requests_result_; |
73 } | 83 } |
74 const std::vector<SavePageRequest>& last_requests() const { | 84 const std::vector<SavePageRequest>& last_requests() const { |
75 return last_requests_; | 85 return last_requests_; |
76 } | 86 } |
77 | 87 |
78 private: | 88 private: |
79 AddRequestResult last_add_result_; | 89 AddRequestResult last_add_result_; |
80 std::unique_ptr<SavePageRequest> last_added_request_; | 90 std::unique_ptr<SavePageRequest> last_added_request_; |
81 RequestQueue::UpdateMultipleRequestResults last_remove_results_; | 91 RequestQueue::UpdateMultipleRequestResults last_remove_results_; |
| 92 RequestQueue::UpdateMultipleRequestResults last_multiple_update_results_; |
82 UpdateRequestResult last_update_result_; | 93 UpdateRequestResult last_update_result_; |
83 | 94 |
84 GetRequestsResult last_get_requests_result_; | 95 GetRequestsResult last_get_requests_result_; |
85 std::vector<SavePageRequest> last_requests_; | 96 std::vector<SavePageRequest> last_requests_; |
86 | 97 |
87 std::unique_ptr<RequestQueue> queue_; | 98 std::unique_ptr<RequestQueue> queue_; |
88 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 99 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
89 base::ThreadTaskRunnerHandle task_runner_handle_; | 100 base::ThreadTaskRunnerHandle task_runner_handle_; |
90 }; | 101 }; |
91 | 102 |
(...skipping 22 matching lines...) Expand all Loading... |
114 last_added_request_.reset(new SavePageRequest(request)); | 125 last_added_request_.reset(new SavePageRequest(request)); |
115 } | 126 } |
116 | 127 |
117 void RequestQueueTest::GetRequestsDone( | 128 void RequestQueueTest::GetRequestsDone( |
118 GetRequestsResult result, | 129 GetRequestsResult result, |
119 const std::vector<SavePageRequest>& requests) { | 130 const std::vector<SavePageRequest>& requests) { |
120 last_get_requests_result_ = result; | 131 last_get_requests_result_ = result; |
121 last_requests_ = requests; | 132 last_requests_ = requests; |
122 } | 133 } |
123 | 134 |
124 void RequestQueueTest::RemoveRequestDone( | 135 void RequestQueueTest::RemoveRequestsDone( |
125 const RequestQueue::UpdateMultipleRequestResults& results) { | 136 const RequestQueue::UpdateMultipleRequestResults& results, |
| 137 const std::vector<SavePageRequest>& requests) { |
126 last_remove_results_ = results; | 138 last_remove_results_ = results; |
| 139 last_requests_ = requests; |
| 140 } |
| 141 |
| 142 void RequestQueueTest::UpdateMultipleRequestsDone( |
| 143 const RequestQueue::UpdateMultipleRequestResults& results, |
| 144 const std::vector<SavePageRequest>& requests) { |
| 145 last_multiple_update_results_ = results; |
| 146 last_requests_ = requests; |
127 } | 147 } |
128 | 148 |
129 void RequestQueueTest::UpdateRequestDone(UpdateRequestResult result) { | 149 void RequestQueueTest::UpdateRequestDone(UpdateRequestResult result) { |
130 last_update_result_ = result; | 150 last_update_result_ = result; |
131 } | 151 } |
132 | 152 |
133 TEST_F(RequestQueueTest, GetRequestsEmpty) { | 153 TEST_F(RequestQueueTest, GetRequestsEmpty) { |
134 queue()->GetRequests( | 154 queue()->GetRequests( |
135 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | 155 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); |
136 PumpLoop(); | 156 PumpLoop(); |
(...skipping 23 matching lines...) Expand all Loading... |
160 base::Time creation_time = base::Time::Now(); | 180 base::Time creation_time = base::Time::Now(); |
161 SavePageRequest request( | 181 SavePageRequest request( |
162 kRequestId, kUrl, kClientId, creation_time, kUserRequested); | 182 kRequestId, kUrl, kClientId, creation_time, kUserRequested); |
163 queue()->AddRequest(request, base::Bind(&RequestQueueTest::AddRequestDone, | 183 queue()->AddRequest(request, base::Bind(&RequestQueueTest::AddRequestDone, |
164 base::Unretained(this))); | 184 base::Unretained(this))); |
165 PumpLoop(); | 185 PumpLoop(); |
166 ASSERT_EQ(kRequestId, last_added_request()->request_id()); | 186 ASSERT_EQ(kRequestId, last_added_request()->request_id()); |
167 | 187 |
168 std::vector<int64_t> remove_requests; | 188 std::vector<int64_t> remove_requests; |
169 remove_requests.push_back(kRequestId); | 189 remove_requests.push_back(kRequestId); |
170 queue()->RemoveRequests( | 190 queue()->RemoveRequests(remove_requests, |
171 remove_requests, | 191 base::Bind(&RequestQueueTest::RemoveRequestsDone, |
172 base::Bind(&RequestQueueTest::RemoveRequestDone, base::Unretained(this))); | 192 base::Unretained(this))); |
173 PumpLoop(); | 193 PumpLoop(); |
174 ASSERT_EQ(1ul, last_remove_results().size()); | 194 ASSERT_EQ(1ul, last_remove_results().size()); |
175 ASSERT_EQ(UpdateRequestResult::SUCCESS, last_remove_results().at(0).second); | 195 ASSERT_EQ(UpdateRequestResult::SUCCESS, last_remove_results().at(0).second); |
176 | 196 |
177 queue()->GetRequests( | 197 queue()->GetRequests( |
178 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | 198 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); |
179 PumpLoop(); | 199 PumpLoop(); |
180 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | 200 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); |
181 ASSERT_EQ(0ul, last_requests().size()); | 201 ASSERT_EQ(0ul, last_requests().size()); |
182 } | 202 } |
(...skipping 10 matching lines...) Expand all Loading... |
193 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time, | 213 SavePageRequest request2(kRequestId2, kUrl2, kClientId2, creation_time, |
194 kUserRequested); | 214 kUserRequested); |
195 queue()->AddRequest(request2, base::Bind(&RequestQueueTest::AddRequestDone, | 215 queue()->AddRequest(request2, base::Bind(&RequestQueueTest::AddRequestDone, |
196 base::Unretained(this))); | 216 base::Unretained(this))); |
197 PumpLoop(); | 217 PumpLoop(); |
198 ASSERT_EQ(kRequestId2, last_added_request()->request_id()); | 218 ASSERT_EQ(kRequestId2, last_added_request()->request_id()); |
199 | 219 |
200 std::vector<int64_t> remove_requests; | 220 std::vector<int64_t> remove_requests; |
201 remove_requests.push_back(kRequestId); | 221 remove_requests.push_back(kRequestId); |
202 remove_requests.push_back(kRequestId2); | 222 remove_requests.push_back(kRequestId2); |
203 queue()->RemoveRequests( | 223 queue()->RemoveRequests(remove_requests, |
204 remove_requests, | 224 base::Bind(&RequestQueueTest::RemoveRequestsDone, |
205 base::Bind(&RequestQueueTest::RemoveRequestDone, base::Unretained(this))); | 225 base::Unretained(this))); |
206 PumpLoop(); | 226 PumpLoop(); |
207 ASSERT_EQ(2ul, last_remove_results().size()); | 227 ASSERT_EQ(2ul, last_remove_results().size()); |
208 ASSERT_EQ(UpdateRequestResult::SUCCESS, last_remove_results().at(0).second); | 228 ASSERT_EQ(UpdateRequestResult::SUCCESS, last_remove_results().at(0).second); |
209 ASSERT_EQ(UpdateRequestResult::SUCCESS, last_remove_results().at(1).second); | 229 ASSERT_EQ(UpdateRequestResult::SUCCESS, last_remove_results().at(1).second); |
210 ASSERT_EQ(kRequestId, last_remove_results().at(0).first); | 230 ASSERT_EQ(kRequestId, last_remove_results().at(0).first); |
211 ASSERT_EQ(kRequestId2, last_remove_results().at(1).first); | 231 ASSERT_EQ(kRequestId2, last_remove_results().at(1).first); |
212 | 232 |
213 queue()->GetRequests( | 233 queue()->GetRequests( |
214 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | 234 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); |
215 PumpLoop(); | 235 PumpLoop(); |
(...skipping 17 matching lines...) Expand all Loading... |
233 PumpLoop(); | 253 PumpLoop(); |
234 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | 254 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); |
235 ASSERT_EQ(1ul, last_requests().size()); | 255 ASSERT_EQ(1ul, last_requests().size()); |
236 | 256 |
237 std::vector<int64_t> request_ids; | 257 std::vector<int64_t> request_ids; |
238 request_ids.push_back(kRequestId); | 258 request_ids.push_back(kRequestId); |
239 | 259 |
240 // Pause the request. | 260 // Pause the request. |
241 queue()->ChangeRequestsState( | 261 queue()->ChangeRequestsState( |
242 request_ids, SavePageRequest::RequestState::PAUSED, | 262 request_ids, SavePageRequest::RequestState::PAUSED, |
243 base::Bind(&RequestQueueTest::UpdateRequestDone, base::Unretained(this))); | 263 base::Bind(&RequestQueueTest::UpdateMultipleRequestsDone, |
| 264 base::Unretained(this))); |
244 PumpLoop(); | 265 PumpLoop(); |
245 ASSERT_EQ(UpdateRequestResult::SUCCESS, last_update_result()); | 266 ASSERT_EQ(1ul, last_multiple_update_results().size()); |
| 267 ASSERT_EQ(UpdateRequestResult::SUCCESS, |
| 268 last_multiple_update_results().at(0).second); |
246 | 269 |
247 queue()->GetRequests( | 270 queue()->GetRequests( |
248 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | 271 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); |
249 PumpLoop(); | 272 PumpLoop(); |
250 | 273 |
251 // Verify the request is paused. | 274 // Verify the request is paused. |
252 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | 275 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); |
253 ASSERT_EQ(1ul, last_requests().size()); | 276 ASSERT_EQ(1ul, last_requests().size()); |
254 ASSERT_EQ(SavePageRequest::RequestState::PAUSED, | 277 ASSERT_EQ(SavePageRequest::RequestState::PAUSED, |
255 last_requests().front().request_state()); | 278 last_requests().front().request_state()); |
256 | 279 |
257 // Resume the request. | 280 // Resume the request. |
258 queue()->ChangeRequestsState( | 281 queue()->ChangeRequestsState( |
259 request_ids, SavePageRequest::RequestState::AVAILABLE, | 282 request_ids, SavePageRequest::RequestState::AVAILABLE, |
260 base::Bind(&RequestQueueTest::UpdateRequestDone, base::Unretained(this))); | 283 base::Bind(&RequestQueueTest::UpdateMultipleRequestsDone, |
| 284 base::Unretained(this))); |
261 PumpLoop(); | 285 PumpLoop(); |
262 ASSERT_EQ(UpdateRequestResult::SUCCESS, last_update_result()); | 286 ASSERT_EQ(1ul, last_multiple_update_results().size()); |
| 287 ASSERT_EQ(UpdateRequestResult::SUCCESS, |
| 288 last_multiple_update_results().at(0).second); |
263 | 289 |
264 queue()->GetRequests( | 290 queue()->GetRequests( |
265 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | 291 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); |
266 PumpLoop(); | 292 PumpLoop(); |
267 | 293 |
268 // Verify the request is no longer paused. | 294 // Verify the request is no longer paused. |
269 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | 295 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); |
270 ASSERT_EQ(1ul, last_requests().size()); | 296 ASSERT_EQ(1ul, last_requests().size()); |
271 ASSERT_EQ(SavePageRequest::RequestState::AVAILABLE, | 297 ASSERT_EQ(SavePageRequest::RequestState::AVAILABLE, |
272 last_requests().front().request_state()); | 298 last_requests().front().request_state()); |
(...skipping 17 matching lines...) Expand all Loading... |
290 ASSERT_EQ(request2.request_id(), last_added_request()->request_id()); | 316 ASSERT_EQ(request2.request_id(), last_added_request()->request_id()); |
291 | 317 |
292 queue()->GetRequests( | 318 queue()->GetRequests( |
293 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | 319 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); |
294 PumpLoop(); | 320 PumpLoop(); |
295 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | 321 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); |
296 ASSERT_EQ(2ul, last_requests().size()); | 322 ASSERT_EQ(2ul, last_requests().size()); |
297 | 323 |
298 std::vector<int64_t> remove_requests; | 324 std::vector<int64_t> remove_requests; |
299 remove_requests.push_back(request1.request_id()); | 325 remove_requests.push_back(request1.request_id()); |
300 queue()->RemoveRequests( | 326 queue()->RemoveRequests(remove_requests, |
301 remove_requests, | 327 base::Bind(&RequestQueueTest::RemoveRequestsDone, |
302 base::Bind(&RequestQueueTest::RemoveRequestDone, base::Unretained(this))); | 328 base::Unretained(this))); |
303 PumpLoop(); | 329 PumpLoop(); |
304 ASSERT_EQ(1ul, last_remove_results().size()); | 330 ASSERT_EQ(1ul, last_remove_results().size()); |
305 ASSERT_EQ(kRequestId, last_remove_results().at(0).first); | 331 ASSERT_EQ(kRequestId, last_remove_results().at(0).first); |
306 ASSERT_EQ(UpdateRequestResult::SUCCESS, last_remove_results().at(0).second); | 332 ASSERT_EQ(UpdateRequestResult::SUCCESS, last_remove_results().at(0).second); |
307 | 333 |
308 queue()->GetRequests( | 334 queue()->GetRequests( |
309 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); | 335 base::Bind(&RequestQueueTest::GetRequestsDone, base::Unretained(this))); |
310 PumpLoop(); | 336 PumpLoop(); |
311 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); | 337 ASSERT_EQ(GetRequestsResult::SUCCESS, last_get_requests_result()); |
312 ASSERT_EQ(1ul, last_requests().size()); | 338 ASSERT_EQ(1ul, last_requests().size()); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 | 378 |
353 // Try to update request1 when only request2 is in the queue. | 379 // Try to update request1 when only request2 is in the queue. |
354 queue()->UpdateRequest( | 380 queue()->UpdateRequest( |
355 request1, | 381 request1, |
356 base::Bind(&RequestQueueTest::UpdateRequestDone, base::Unretained(this))); | 382 base::Bind(&RequestQueueTest::UpdateRequestDone, base::Unretained(this))); |
357 PumpLoop(); | 383 PumpLoop(); |
358 ASSERT_EQ(UpdateRequestResult::REQUEST_DOES_NOT_EXIST, last_update_result()); | 384 ASSERT_EQ(UpdateRequestResult::REQUEST_DOES_NOT_EXIST, last_update_result()); |
359 } | 385 } |
360 | 386 |
361 } // namespace offline_pages | 387 } // namespace offline_pages |
OLD | NEW |