| 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_coordinator.h" | 5 #include "components/offline_pages/background/request_coordinator.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "components/offline_pages/background/request_queue.h" | 22 #include "components/offline_pages/background/request_queue.h" |
| 23 #include "components/offline_pages/background/request_queue_in_memory_store.h" | 23 #include "components/offline_pages/background/request_queue_in_memory_store.h" |
| 24 #include "components/offline_pages/background/save_page_request.h" | 24 #include "components/offline_pages/background/save_page_request.h" |
| 25 #include "components/offline_pages/background/scheduler.h" | 25 #include "components/offline_pages/background/scheduler.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 27 |
| 28 namespace offline_pages { | 28 namespace offline_pages { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 // put test constants here | 31 // put test constants here |
| 32 const GURL kUrl("http://universe.com/everything"); | 32 const GURL kUrl1("http://universe.com/everything"); |
| 33 const ClientId kClientId("bookmark", "42"); | 33 const GURL kUrl2("http://universe.com/toinfinityandbeyond"); |
| 34 const std::string kClientNamespace("bookmark"); |
| 35 const std::string kId1("42"); |
| 36 const std::string kId2("life*universe+everything"); |
| 37 const ClientId kClientId1(kClientNamespace, kId1); |
| 38 const ClientId kClientId2(kClientNamespace, kId2); |
| 39 const int kRequestId1(1); |
| 34 const int kRequestId2(2); | 40 const int kRequestId2(2); |
| 35 const GURL kUrl2("http://universe.com/toinfinityandbeyond"); | |
| 36 const ClientId kClientId2("bookmark", "43"); | |
| 37 const int kRequestId(1); | |
| 38 const long kTestTimeoutSeconds = 1; | 41 const long kTestTimeoutSeconds = 1; |
| 39 const long kTestTimeBudgetSeconds = 200; | 42 const long kTestTimeBudgetSeconds = 200; |
| 40 const int kBatteryPercentageHigh = 75; | 43 const int kBatteryPercentageHigh = 75; |
| 41 const bool kPowerRequired = true; | 44 const bool kPowerRequired = true; |
| 42 const bool kUserRequested = true; | 45 const bool kUserRequested = true; |
| 43 const int kAttemptCount = 1; | 46 const int kAttemptCount = 1; |
| 44 } // namespace | 47 } // namespace |
| 45 | 48 |
| 46 class SchedulerStub : public Scheduler { | 49 class SchedulerStub : public Scheduler { |
| 47 public: | 50 public: |
| (...skipping 20 matching lines...) Expand all Loading... |
| 68 | 71 |
| 69 private: | 72 private: |
| 70 bool schedule_called_; | 73 bool schedule_called_; |
| 71 bool unschedule_called_; | 74 bool unschedule_called_; |
| 72 TriggerConditions conditions_; | 75 TriggerConditions conditions_; |
| 73 }; | 76 }; |
| 74 | 77 |
| 75 class OfflinerStub : public Offliner { | 78 class OfflinerStub : public Offliner { |
| 76 public: | 79 public: |
| 77 OfflinerStub() | 80 OfflinerStub() |
| 78 : request_(kRequestId, kUrl, kClientId, base::Time::Now(), | 81 : request_(kRequestId1, kUrl1, kClientId1, base::Time::Now(), |
| 79 kUserRequested), | 82 kUserRequested), |
| 80 enable_callback_(false), | 83 enable_callback_(false), |
| 81 cancel_called_(false) {} | 84 cancel_called_(false) {} |
| 82 | 85 |
| 83 bool LoadAndSave(const SavePageRequest& request, | 86 bool LoadAndSave(const SavePageRequest& request, |
| 84 const CompletionCallback& callback) override { | 87 const CompletionCallback& callback) override { |
| 85 callback_ = callback; | 88 callback_ = callback; |
| 86 request_ = request; | 89 request_ = request; |
| 87 // Post the callback on the run loop. | 90 // Post the callback on the run loop. |
| 88 if (enable_callback_) { | 91 if (enable_callback_) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 void PumpLoop(); | 137 void PumpLoop(); |
| 135 | 138 |
| 136 RequestCoordinator* coordinator() { | 139 RequestCoordinator* coordinator() { |
| 137 return coordinator_.get(); | 140 return coordinator_.get(); |
| 138 } | 141 } |
| 139 | 142 |
| 140 bool is_busy() { | 143 bool is_busy() { |
| 141 return coordinator_->is_busy(); | 144 return coordinator_->is_busy(); |
| 142 } | 145 } |
| 143 | 146 |
| 144 // Empty callback function | 147 // Empty callback function. |
| 145 void EmptyCallbackFunction(bool result) { | 148 void EmptyCallbackFunction(bool result) { |
| 146 } | 149 } |
| 147 | 150 |
| 148 // Callback function which releases a wait for it. | 151 // Callback function which releases a wait for it. |
| 149 void WaitingCallbackFunction(bool result) { | 152 void WaitingCallbackFunction(bool result) { |
| 150 waiter_.Signal(); | 153 waiter_.Signal(); |
| 151 } | 154 } |
| 152 | 155 |
| 153 // Callback for Add requests | 156 // Callback for Add requests. |
| 154 void AddRequestDone(RequestQueue::AddRequestResult result, | 157 void AddRequestDone(RequestQueue::AddRequestResult result, |
| 155 const SavePageRequest& request); | 158 const SavePageRequest& request); |
| 156 | 159 |
| 157 // Callback for getting requests. | 160 // Callback for getting requests. |
| 158 void GetRequestsDone(RequestQueue::GetRequestsResult result, | 161 void GetRequestsDone(RequestQueue::GetRequestsResult result, |
| 159 const std::vector<SavePageRequest>& requests); | 162 const std::vector<SavePageRequest>& requests); |
| 160 | 163 |
| 164 // Callback for getting request statuses. |
| 165 void GetQueuedRequestsDone(const std::vector<ClientId>& client_ids); |
| 166 |
| 161 void SendOfflinerDoneCallback(const SavePageRequest& request, | 167 void SendOfflinerDoneCallback(const SavePageRequest& request, |
| 162 Offliner::RequestStatus status); | 168 Offliner::RequestStatus status); |
| 163 | 169 |
| 164 RequestQueue::GetRequestsResult last_get_requests_result() const { | 170 RequestQueue::GetRequestsResult last_get_requests_result() const { |
| 165 return last_get_requests_result_; | 171 return last_get_requests_result_; |
| 166 } | 172 } |
| 167 | 173 |
| 168 const std::vector<SavePageRequest>& last_requests() const { | 174 const std::vector<SavePageRequest>& last_requests() const { |
| 169 return last_requests_; | 175 return last_requests_; |
| 170 } | 176 } |
| 171 | 177 |
| 178 const std::vector<ClientId>& last_client_ids() const { |
| 179 return last_client_ids_; |
| 180 } |
| 181 |
| 172 void EnableOfflinerCallback(bool enable) { | 182 void EnableOfflinerCallback(bool enable) { |
| 173 offliner_->enable_callback(enable); | 183 offliner_->enable_callback(enable); |
| 174 } | 184 } |
| 175 | 185 |
| 176 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { | 186 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { |
| 177 coordinator_->SetOfflinerTimeoutForTest(timeout); | 187 coordinator_->SetOfflinerTimeoutForTest(timeout); |
| 178 } | 188 } |
| 179 | 189 |
| 180 void SetDeviceConditionsForTest(DeviceConditions device_conditions) { | 190 void SetDeviceConditionsForTest(DeviceConditions device_conditions) { |
| 181 coordinator_->SetDeviceConditionsForTest(device_conditions); | 191 coordinator_->SetDeviceConditionsForTest(device_conditions); |
| 182 } | 192 } |
| 183 | 193 |
| 184 void WaitForCallback() { | 194 void WaitForCallback() { |
| 185 waiter_.Wait(); | 195 waiter_.Wait(); |
| 186 } | 196 } |
| 187 | 197 |
| 188 void AdvanceClockBy(base::TimeDelta delta) { | 198 void AdvanceClockBy(base::TimeDelta delta) { |
| 189 task_runner_->FastForwardBy(delta); | 199 task_runner_->FastForwardBy(delta); |
| 190 } | 200 } |
| 191 | 201 |
| 192 Offliner::RequestStatus last_offlining_status() const { | 202 Offliner::RequestStatus last_offlining_status() const { |
| 193 return coordinator_->last_offlining_status_; | 203 return coordinator_->last_offlining_status_; |
| 194 } | 204 } |
| 195 | 205 |
| 196 bool OfflinerWasCanceled() const { return offliner_->cancel_called(); } | 206 bool OfflinerWasCanceled() const { return offliner_->cancel_called(); } |
| 197 | 207 |
| 198 private: | 208 private: |
| 199 RequestQueue::GetRequestsResult last_get_requests_result_; | 209 RequestQueue::GetRequestsResult last_get_requests_result_; |
| 200 std::vector<SavePageRequest> last_requests_; | 210 std::vector<SavePageRequest> last_requests_; |
| 211 std::vector<ClientId> last_client_ids_; |
| 201 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | 212 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
| 202 base::ThreadTaskRunnerHandle task_runner_handle_; | 213 base::ThreadTaskRunnerHandle task_runner_handle_; |
| 203 std::unique_ptr<RequestCoordinator> coordinator_; | 214 std::unique_ptr<RequestCoordinator> coordinator_; |
| 204 OfflinerStub* offliner_; | 215 OfflinerStub* offliner_; |
| 205 base::WaitableEvent waiter_; | 216 base::WaitableEvent waiter_; |
| 206 }; | 217 }; |
| 207 | 218 |
| 208 RequestCoordinatorTest::RequestCoordinatorTest() | 219 RequestCoordinatorTest::RequestCoordinatorTest() |
| 209 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE), | 220 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE), |
| 210 task_runner_(new base::TestMockTimeTaskRunner), | 221 task_runner_(new base::TestMockTimeTaskRunner), |
| (...skipping 23 matching lines...) Expand all Loading... |
| 234 task_runner_->RunUntilIdle(); | 245 task_runner_->RunUntilIdle(); |
| 235 } | 246 } |
| 236 | 247 |
| 237 void RequestCoordinatorTest::GetRequestsDone( | 248 void RequestCoordinatorTest::GetRequestsDone( |
| 238 RequestQueue::GetRequestsResult result, | 249 RequestQueue::GetRequestsResult result, |
| 239 const std::vector<SavePageRequest>& requests) { | 250 const std::vector<SavePageRequest>& requests) { |
| 240 last_get_requests_result_ = result; | 251 last_get_requests_result_ = result; |
| 241 last_requests_ = requests; | 252 last_requests_ = requests; |
| 242 } | 253 } |
| 243 | 254 |
| 255 void RequestCoordinatorTest::GetQueuedRequestsDone( |
| 256 const std::vector<ClientId>& client_ids) { |
| 257 last_client_ids_ = client_ids; |
| 258 waiter_.Signal(); |
| 259 } |
| 260 |
| 244 void RequestCoordinatorTest::AddRequestDone( | 261 void RequestCoordinatorTest::AddRequestDone( |
| 245 RequestQueue::AddRequestResult result, | 262 RequestQueue::AddRequestResult result, |
| 246 const SavePageRequest& request) {} | 263 const SavePageRequest& request) {} |
| 247 | 264 |
| 248 void RequestCoordinatorTest::SendOfflinerDoneCallback( | 265 void RequestCoordinatorTest::SendOfflinerDoneCallback( |
| 249 const SavePageRequest& request, Offliner::RequestStatus status) { | 266 const SavePageRequest& request, Offliner::RequestStatus status) { |
| 250 // Using the fact that the test class is a friend, call to the callback | 267 // Using the fact that the test class is a friend, call to the callback |
| 251 coordinator_->OfflinerDoneCallback(request, status); | 268 coordinator_->OfflinerDoneCallback(request, status); |
| 252 } | 269 } |
| 253 | 270 |
| 254 TEST_F(RequestCoordinatorTest, StartProcessingWithNoRequests) { | 271 TEST_F(RequestCoordinatorTest, StartProcessingWithNoRequests) { |
| 255 DeviceConditions device_conditions(false, 75, | 272 DeviceConditions device_conditions(false, 75, |
| 256 net::NetworkChangeNotifier::CONNECTION_3G); | 273 net::NetworkChangeNotifier::CONNECTION_3G); |
| 257 base::Callback<void(bool)> callback = | 274 base::Callback<void(bool)> callback = |
| 258 base::Bind( | 275 base::Bind( |
| 259 &RequestCoordinatorTest::EmptyCallbackFunction, | 276 &RequestCoordinatorTest::EmptyCallbackFunction, |
| 260 base::Unretained(this)); | 277 base::Unretained(this)); |
| 261 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); | 278 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); |
| 262 } | 279 } |
| 263 | 280 |
| 264 TEST_F(RequestCoordinatorTest, StartProcessingWithRequestInProgress) { | 281 TEST_F(RequestCoordinatorTest, StartProcessingWithRequestInProgress) { |
| 265 // Put the request on the queue. | 282 // Put the request on the queue. |
| 266 EXPECT_TRUE(coordinator()->SavePageLater(kUrl, kClientId, kUserRequested)); | 283 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested)); |
| 267 | 284 |
| 268 // Set up for the call to StartProcessing by building arguments. | 285 // Set up for the call to StartProcessing by building arguments. |
| 269 DeviceConditions device_conditions(false, 75, | 286 DeviceConditions device_conditions(false, 75, |
| 270 net::NetworkChangeNotifier::CONNECTION_3G); | 287 net::NetworkChangeNotifier::CONNECTION_3G); |
| 271 base::Callback<void(bool)> callback = | 288 base::Callback<void(bool)> callback = |
| 272 base::Bind(&RequestCoordinatorTest::EmptyCallbackFunction, | 289 base::Bind(&RequestCoordinatorTest::EmptyCallbackFunction, |
| 273 base::Unretained(this)); | 290 base::Unretained(this)); |
| 274 | 291 |
| 275 // Ensure that the forthcoming request does not finish - we simulate it being | 292 // Ensure that the forthcoming request does not finish - we simulate it being |
| 276 // in progress by asking it to skip making the completion callback. | 293 // in progress by asking it to skip making the completion callback. |
| 277 EnableOfflinerCallback(false); | 294 EnableOfflinerCallback(false); |
| 278 | 295 |
| 279 // Sending the request to the offliner should make it busy. | 296 // Sending the request to the offliner should make it busy. |
| 280 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); | 297 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); |
| 281 PumpLoop(); | 298 PumpLoop(); |
| 282 EXPECT_TRUE(is_busy()); | 299 EXPECT_TRUE(is_busy()); |
| 283 | 300 |
| 284 // Now trying to start processing on another request should return false. | 301 // Now trying to start processing on another request should return false. |
| 285 EXPECT_FALSE(coordinator()->StartProcessing(device_conditions, callback)); | 302 EXPECT_FALSE(coordinator()->StartProcessing(device_conditions, callback)); |
| 286 } | 303 } |
| 287 | 304 |
| 288 TEST_F(RequestCoordinatorTest, SavePageLater) { | 305 TEST_F(RequestCoordinatorTest, SavePageLater) { |
| 289 EXPECT_TRUE(coordinator()->SavePageLater(kUrl, kClientId, kUserRequested)); | 306 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested)); |
| 290 | 307 |
| 291 // Expect that a request got placed on the queue. | 308 // Expect that a request got placed on the queue. |
| 292 coordinator()->queue()->GetRequests( | 309 coordinator()->queue()->GetRequests( |
| 293 base::Bind(&RequestCoordinatorTest::GetRequestsDone, | 310 base::Bind(&RequestCoordinatorTest::GetRequestsDone, |
| 294 base::Unretained(this))); | 311 base::Unretained(this))); |
| 295 | 312 |
| 296 // Wait for callbacks to finish, both request queue and offliner. | 313 // Wait for callbacks to finish, both request queue and offliner. |
| 297 PumpLoop(); | 314 PumpLoop(); |
| 298 | 315 |
| 299 // Check the request queue is as expected. | 316 // Check the request queue is as expected. |
| 300 EXPECT_EQ(1UL, last_requests().size()); | 317 EXPECT_EQ(1UL, last_requests().size()); |
| 301 EXPECT_EQ(kUrl, last_requests()[0].url()); | 318 EXPECT_EQ(kUrl1, last_requests()[0].url()); |
| 302 EXPECT_EQ(kClientId, last_requests()[0].client_id()); | 319 EXPECT_EQ(kClientId1, last_requests()[0].client_id()); |
| 303 | 320 |
| 304 // Expect that the scheduler got notified. | 321 // Expect that the scheduler got notified. |
| 305 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( | 322 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( |
| 306 coordinator()->scheduler()); | 323 coordinator()->scheduler()); |
| 307 EXPECT_TRUE(scheduler_stub->schedule_called()); | 324 EXPECT_TRUE(scheduler_stub->schedule_called()); |
| 308 EXPECT_EQ(coordinator() | 325 EXPECT_EQ(coordinator() |
| 309 ->GetTriggerConditionsForUserRequest() | 326 ->GetTriggerConditionsForUserRequest() |
| 310 .minimum_battery_percentage, | 327 .minimum_battery_percentage, |
| 311 scheduler_stub->conditions()->minimum_battery_percentage); | 328 scheduler_stub->conditions()->minimum_battery_percentage); |
| 312 } | 329 } |
| 313 | 330 |
| 314 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { | 331 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { |
| 315 // Add a request to the queue, wait for callbacks to finish. | 332 // Add a request to the queue, wait for callbacks to finish. |
| 316 offline_pages::SavePageRequest request( | 333 offline_pages::SavePageRequest request( |
| 317 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested); | 334 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
| 318 coordinator()->queue()->AddRequest( | 335 coordinator()->queue()->AddRequest( |
| 319 request, | 336 request, |
| 320 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 337 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 321 base::Unretained(this))); | 338 base::Unretained(this))); |
| 322 PumpLoop(); | 339 PumpLoop(); |
| 323 | 340 |
| 324 // We need to give a callback to the request. | 341 // We need to give a callback to the request. |
| 325 base::Callback<void(bool)> callback = | 342 base::Callback<void(bool)> callback = |
| 326 base::Bind( | 343 base::Bind( |
| 327 &RequestCoordinatorTest::EmptyCallbackFunction, | 344 &RequestCoordinatorTest::EmptyCallbackFunction, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 347 | 364 |
| 348 // We should not find any requests in the queue anymore. | 365 // We should not find any requests in the queue anymore. |
| 349 // RequestPicker should *not* have tried to start an additional job, | 366 // RequestPicker should *not* have tried to start an additional job, |
| 350 // because the request queue is empty now. | 367 // because the request queue is empty now. |
| 351 EXPECT_EQ(0UL, last_requests().size()); | 368 EXPECT_EQ(0UL, last_requests().size()); |
| 352 } | 369 } |
| 353 | 370 |
| 354 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { | 371 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { |
| 355 // Add a request to the queue, wait for callbacks to finish. | 372 // Add a request to the queue, wait for callbacks to finish. |
| 356 offline_pages::SavePageRequest request( | 373 offline_pages::SavePageRequest request( |
| 357 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested); | 374 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
| 358 coordinator()->queue()->AddRequest( | 375 coordinator()->queue()->AddRequest( |
| 359 request, | 376 request, |
| 360 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 377 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 361 base::Unretained(this))); | 378 base::Unretained(this))); |
| 362 PumpLoop(); | 379 PumpLoop(); |
| 363 | 380 |
| 364 // Add second request to the queue to check handling when first fails. | 381 // Add second request to the queue to check handling when first fails. |
| 365 offline_pages::SavePageRequest request2( | 382 offline_pages::SavePageRequest request2( |
| 366 kRequestId2, kUrl2, kClientId2, base::Time::Now(), kUserRequested); | 383 kRequestId2, kUrl2, kClientId2, base::Time::Now(), kUserRequested); |
| 367 coordinator()->queue()->AddRequest( | 384 coordinator()->queue()->AddRequest( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 // Verify retry count was incremented for first request. | 421 // Verify retry count was incremented for first request. |
| 405 const SavePageRequest& found_request = last_requests().front(); | 422 const SavePageRequest& found_request = last_requests().front(); |
| 406 EXPECT_EQ(1L, found_request.attempt_count()); | 423 EXPECT_EQ(1L, found_request.attempt_count()); |
| 407 } | 424 } |
| 408 | 425 |
| 409 // This tests a StopProcessing call before we have actually started the | 426 // This tests a StopProcessing call before we have actually started the |
| 410 // prerenderer. | 427 // prerenderer. |
| 411 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingImmediately) { | 428 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingImmediately) { |
| 412 // Add a request to the queue, wait for callbacks to finish. | 429 // Add a request to the queue, wait for callbacks to finish. |
| 413 offline_pages::SavePageRequest request( | 430 offline_pages::SavePageRequest request( |
| 414 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested); | 431 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
| 415 coordinator()->queue()->AddRequest( | 432 coordinator()->queue()->AddRequest( |
| 416 request, | 433 request, |
| 417 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 434 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 418 base::Unretained(this))); | 435 base::Unretained(this))); |
| 419 PumpLoop(); | 436 PumpLoop(); |
| 420 | 437 |
| 421 DeviceConditions device_conditions(false, 75, | 438 DeviceConditions device_conditions(false, 75, |
| 422 net::NetworkChangeNotifier::CONNECTION_3G); | 439 net::NetworkChangeNotifier::CONNECTION_3G); |
| 423 base::Callback<void(bool)> callback = | 440 base::Callback<void(bool)> callback = |
| 424 base::Bind( | 441 base::Bind( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 438 last_offlining_status()); | 455 last_offlining_status()); |
| 439 | 456 |
| 440 // Since offliner was not started, it will not have seen cancel call. | 457 // Since offliner was not started, it will not have seen cancel call. |
| 441 EXPECT_FALSE(OfflinerWasCanceled()); | 458 EXPECT_FALSE(OfflinerWasCanceled()); |
| 442 } | 459 } |
| 443 | 460 |
| 444 // This tests a StopProcessing call after the prerenderer has been started. | 461 // This tests a StopProcessing call after the prerenderer has been started. |
| 445 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingLater) { | 462 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingLater) { |
| 446 // Add a request to the queue, wait for callbacks to finish. | 463 // Add a request to the queue, wait for callbacks to finish. |
| 447 offline_pages::SavePageRequest request( | 464 offline_pages::SavePageRequest request( |
| 448 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested); | 465 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
| 449 coordinator()->queue()->AddRequest( | 466 coordinator()->queue()->AddRequest( |
| 450 request, | 467 request, |
| 451 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 468 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 452 base::Unretained(this))); | 469 base::Unretained(this))); |
| 453 PumpLoop(); | 470 PumpLoop(); |
| 454 | 471 |
| 455 // Ensure the start processing request stops before the completion callback. | 472 // Ensure the start processing request stops before the completion callback. |
| 456 EnableOfflinerCallback(false); | 473 EnableOfflinerCallback(false); |
| 457 | 474 |
| 458 DeviceConditions device_conditions(false, 75, | 475 DeviceConditions device_conditions(false, 75, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 477 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, | 494 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, |
| 478 last_offlining_status()); | 495 last_offlining_status()); |
| 479 | 496 |
| 480 // Since offliner was started, it will have seen cancel call. | 497 // Since offliner was started, it will have seen cancel call. |
| 481 EXPECT_TRUE(OfflinerWasCanceled()); | 498 EXPECT_TRUE(OfflinerWasCanceled()); |
| 482 } | 499 } |
| 483 | 500 |
| 484 TEST_F(RequestCoordinatorTest, PrerendererTimeout) { | 501 TEST_F(RequestCoordinatorTest, PrerendererTimeout) { |
| 485 // Build a request to use with the pre-renderer, and put it on the queue. | 502 // Build a request to use with the pre-renderer, and put it on the queue. |
| 486 offline_pages::SavePageRequest request( | 503 offline_pages::SavePageRequest request( |
| 487 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested); | 504 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
| 488 coordinator()->queue()->AddRequest( | 505 coordinator()->queue()->AddRequest( |
| 489 request, | 506 request, |
| 490 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 507 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 491 base::Unretained(this))); | 508 base::Unretained(this))); |
| 492 PumpLoop(); | 509 PumpLoop(); |
| 493 | 510 |
| 494 // Set up for the call to StartProcessing. | 511 // Set up for the call to StartProcessing. |
| 495 DeviceConditions device_conditions( | 512 DeviceConditions device_conditions( |
| 496 !kPowerRequired, kBatteryPercentageHigh, | 513 !kPowerRequired, kBatteryPercentageHigh, |
| 497 net::NetworkChangeNotifier::CONNECTION_3G); | 514 net::NetworkChangeNotifier::CONNECTION_3G); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 520 // tasks too soon. | 537 // tasks too soon. |
| 521 WaitForCallback(); | 538 WaitForCallback(); |
| 522 PumpLoop(); | 539 PumpLoop(); |
| 523 | 540 |
| 524 EXPECT_TRUE(OfflinerWasCanceled()); | 541 EXPECT_TRUE(OfflinerWasCanceled()); |
| 525 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, | 542 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, |
| 526 last_offlining_status()); | 543 last_offlining_status()); |
| 527 } | 544 } |
| 528 | 545 |
| 529 TEST_F(RequestCoordinatorTest, TimeBudgetExceeded) { | 546 TEST_F(RequestCoordinatorTest, TimeBudgetExceeded) { |
| 530 // Build a request to use with the pre-renderer, and put it on the queue. | 547 // Build two requests to use with the pre-renderer, and put it on the queue. |
| 531 offline_pages::SavePageRequest request1( | 548 offline_pages::SavePageRequest request1( |
| 532 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested); | 549 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
| 533 offline_pages::SavePageRequest request2( | 550 offline_pages::SavePageRequest request2( |
| 534 kRequestId + 1, kUrl, kClientId, base::Time::Now(), kUserRequested); | 551 kRequestId1 + 1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
| 535 request2.set_attempt_count(kAttemptCount); | 552 request2.set_attempt_count(kAttemptCount); |
| 536 coordinator()->queue()->AddRequest( | 553 coordinator()->queue()->AddRequest( |
| 537 request1, | 554 request1, |
| 538 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 555 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 539 base::Unretained(this))); | 556 base::Unretained(this))); |
| 540 coordinator()->queue()->AddRequest( | 557 coordinator()->queue()->AddRequest( |
| 541 request1, | 558 request1, // TODO(petewil): This is a bug, should be request2. |
| 542 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 559 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 543 base::Unretained(this))); | 560 base::Unretained(this))); |
| 544 PumpLoop(); | 561 PumpLoop(); |
| 545 | 562 |
| 546 // Set up for the call to StartProcessing. | 563 // Set up for the call to StartProcessing. |
| 547 DeviceConditions device_conditions( | 564 DeviceConditions device_conditions( |
| 548 !kPowerRequired, kBatteryPercentageHigh, | 565 !kPowerRequired, kBatteryPercentageHigh, |
| 549 net::NetworkChangeNotifier::CONNECTION_3G); | 566 net::NetworkChangeNotifier::CONNECTION_3G); |
| 550 base::Callback<void(bool)> callback = | 567 base::Callback<void(bool)> callback = |
| 551 base::Bind(&RequestCoordinatorTest::WaitingCallbackFunction, | 568 base::Bind(&RequestCoordinatorTest::WaitingCallbackFunction, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 565 // Verify the request gets removed from the queue, and wait for callbacks. | 582 // Verify the request gets removed from the queue, and wait for callbacks. |
| 566 coordinator()->queue()->GetRequests( | 583 coordinator()->queue()->GetRequests( |
| 567 base::Bind(&RequestCoordinatorTest::GetRequestsDone, | 584 base::Bind(&RequestCoordinatorTest::GetRequestsDone, |
| 568 base::Unretained(this))); | 585 base::Unretained(this))); |
| 569 PumpLoop(); | 586 PumpLoop(); |
| 570 | 587 |
| 571 // We should find one request in the queue. | 588 // We should find one request in the queue. |
| 572 EXPECT_EQ(1UL, last_requests().size()); | 589 EXPECT_EQ(1UL, last_requests().size()); |
| 573 } | 590 } |
| 574 | 591 |
| 592 TEST_F(RequestCoordinatorTest, GetQueuedRequests) { |
| 593 // Add two requests to the queue. |
| 594 offline_pages::SavePageRequest request1(kRequestId1, kUrl1, kClientId1, |
| 595 base::Time::Now(), kUserRequested); |
| 596 offline_pages::SavePageRequest request2(kRequestId1 + 1, kUrl2, kClientId2, |
| 597 base::Time::Now(), kUserRequested); |
| 598 coordinator()->queue()->AddRequest( |
| 599 request1, base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 600 base::Unretained(this))); |
| 601 coordinator()->queue()->AddRequest( |
| 602 request2, base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 603 base::Unretained(this))); |
| 604 PumpLoop(); |
| 605 |
| 606 // Start the async status fetching. |
| 607 coordinator()->GetQueuedRequests( |
| 608 kClientNamespace, |
| 609 base::Bind(&RequestCoordinatorTest::GetQueuedRequestsDone, |
| 610 base::Unretained(this))); |
| 611 PumpLoop(); |
| 612 |
| 613 // Wait for async get to finish. |
| 614 WaitForCallback(); |
| 615 PumpLoop(); |
| 616 |
| 617 // Check that the statuses found in the callback match what we expect. |
| 618 EXPECT_EQ(2UL, last_client_ids().size()); |
| 619 EXPECT_EQ(kId1, last_client_ids().at(0).id); |
| 620 EXPECT_EQ(kId2, last_client_ids().at(1).id); |
| 621 } |
| 622 |
| 575 } // namespace offline_pages | 623 } // namespace offline_pages |
| OLD | NEW |