| 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 13 matching lines...) Expand all Loading... |
| 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 kUrl("http://universe.com/everything"); |
| 33 const ClientId kClientId("bookmark", "42"); | 33 const ClientId kClientId("bookmark", "42"); |
| 34 const int kRequestId2(2); |
| 35 const GURL kUrl2("http://universe.com/toinfinityandbeyond"); |
| 36 const ClientId kClientId2("bookmark", "43"); |
| 34 const int kRequestId(1); | 37 const int kRequestId(1); |
| 35 const long kTestTimeoutSeconds = 1; | 38 const long kTestTimeoutSeconds = 1; |
| 36 const long kTestTimeBudgetSeconds = 200; | 39 const long kTestTimeBudgetSeconds = 200; |
| 37 const int kBatteryPercentageHigh = 75; | 40 const int kBatteryPercentageHigh = 75; |
| 38 const bool kPowerRequired = true; | 41 const bool kPowerRequired = true; |
| 39 const bool kUserRequested = true; | 42 const bool kUserRequested = true; |
| 40 const int kAttemptCount = 1; | 43 const int kAttemptCount = 1; |
| 41 } // namespace | 44 } // namespace |
| 42 | 45 |
| 43 class SchedulerStub : public Scheduler { | 46 class SchedulerStub : public Scheduler { |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { | 354 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { |
| 352 // Add a request to the queue, wait for callbacks to finish. | 355 // Add a request to the queue, wait for callbacks to finish. |
| 353 offline_pages::SavePageRequest request( | 356 offline_pages::SavePageRequest request( |
| 354 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested); | 357 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested); |
| 355 coordinator()->queue()->AddRequest( | 358 coordinator()->queue()->AddRequest( |
| 356 request, | 359 request, |
| 357 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 360 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 358 base::Unretained(this))); | 361 base::Unretained(this))); |
| 359 PumpLoop(); | 362 PumpLoop(); |
| 360 | 363 |
| 364 // Add second request to the queue to check handling when first fails. |
| 365 offline_pages::SavePageRequest request2( |
| 366 kRequestId2, kUrl2, kClientId2, base::Time::Now(), kUserRequested); |
| 367 coordinator()->queue()->AddRequest( |
| 368 request2, |
| 369 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 370 base::Unretained(this))); |
| 371 PumpLoop(); |
| 372 |
| 361 // We need to give a callback to the request. | 373 // We need to give a callback to the request. |
| 362 base::Callback<void(bool)> callback = | 374 base::Callback<void(bool)> callback = |
| 363 base::Bind( | 375 base::Bind( |
| 364 &RequestCoordinatorTest::EmptyCallbackFunction, | 376 &RequestCoordinatorTest::EmptyCallbackFunction, |
| 365 base::Unretained(this)); | 377 base::Unretained(this)); |
| 366 coordinator()->SetProcessingCallbackForTest(callback); | 378 coordinator()->SetProcessingCallbackForTest(callback); |
| 367 | 379 |
| 368 // Set up device conditions for the test. | 380 // Set up device conditions for the test. |
| 369 DeviceConditions device_conditions( | 381 DeviceConditions device_conditions( |
| 370 false, 75, net::NetworkChangeNotifier::CONNECTION_3G); | 382 false, 75, net::NetworkChangeNotifier::CONNECTION_3G); |
| 371 SetDeviceConditionsForTest(device_conditions); | 383 SetDeviceConditionsForTest(device_conditions); |
| 372 | 384 |
| 373 // Call the OfflinerDoneCallback to simulate the request failed, wait | 385 // Call the OfflinerDoneCallback to simulate the request failed, wait |
| 374 // for callbacks. | 386 // for callbacks. |
| 375 EnableOfflinerCallback(true); | 387 EnableOfflinerCallback(true); |
| 376 SendOfflinerDoneCallback(request, | 388 SendOfflinerDoneCallback(request, |
| 377 Offliner::RequestStatus::PRERENDERING_FAILED); | 389 Offliner::RequestStatus::PRERENDERING_FAILED); |
| 378 // There will be one request left in the queue after the prerender fails, stop | |
| 379 // processing now so that it will remain in the queue for us to check. This | |
| 380 // won't affect the offliner done callback other than preventing | |
| 381 // TryNextRequest from doing anything. | |
| 382 coordinator()->StopProcessing(); | |
| 383 | |
| 384 PumpLoop(); | 390 PumpLoop(); |
| 385 | 391 |
| 386 // Verify the request is not removed from the queue, and wait for callbacks. | 392 // TODO(dougarnett): Consider injecting mock RequestPicker for this test |
| 393 // and verifying that there is no attempt to pick another request following |
| 394 // this failure code. |
| 395 |
| 396 // Verify neither request is removed from the queue; wait for callbacks. |
| 387 coordinator()->queue()->GetRequests( | 397 coordinator()->queue()->GetRequests( |
| 388 base::Bind(&RequestCoordinatorTest::GetRequestsDone, | 398 base::Bind(&RequestCoordinatorTest::GetRequestsDone, |
| 389 base::Unretained(this))); | 399 base::Unretained(this))); |
| 390 PumpLoop(); | 400 PumpLoop(); |
| 391 | 401 |
| 392 // Still one request in the queue. | 402 // Still two requests in the queue. |
| 393 EXPECT_EQ(1UL, last_requests().size()); | 403 EXPECT_EQ(2UL, last_requests().size()); |
| 394 // Verify retry count was incremented. | 404 // Verify retry count was incremented for first request. |
| 395 const SavePageRequest& found_request = last_requests().front(); | 405 const SavePageRequest& found_request = last_requests().front(); |
| 396 EXPECT_EQ(1L, found_request.attempt_count()); | 406 EXPECT_EQ(1L, found_request.attempt_count()); |
| 397 } | 407 } |
| 398 | 408 |
| 399 // This tests a StopProcessing call before we have actually started the | 409 // This tests a StopProcessing call before we have actually started the |
| 400 // prerenderer. | 410 // prerenderer. |
| 401 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingImmediately) { | 411 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingImmediately) { |
| 402 // Add a request to the queue, wait for callbacks to finish. | 412 // Add a request to the queue, wait for callbacks to finish. |
| 403 offline_pages::SavePageRequest request( | 413 offline_pages::SavePageRequest request( |
| 404 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested); | 414 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 // which won't time out immediately, so the watchdog thread doesn't kill valid | 519 // which won't time out immediately, so the watchdog thread doesn't kill valid |
| 510 // tasks too soon. | 520 // tasks too soon. |
| 511 WaitForCallback(); | 521 WaitForCallback(); |
| 512 PumpLoop(); | 522 PumpLoop(); |
| 513 | 523 |
| 514 EXPECT_TRUE(OfflinerWasCanceled()); | 524 EXPECT_TRUE(OfflinerWasCanceled()); |
| 515 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, | 525 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, |
| 516 last_offlining_status()); | 526 last_offlining_status()); |
| 517 } | 527 } |
| 518 | 528 |
| 519 | |
| 520 TEST_F(RequestCoordinatorTest, TimeBudgetExceeded) { | 529 TEST_F(RequestCoordinatorTest, TimeBudgetExceeded) { |
| 521 // Build a request to use with the pre-renderer, and put it on the queue. | 530 // Build a request to use with the pre-renderer, and put it on the queue. |
| 522 offline_pages::SavePageRequest request1( | 531 offline_pages::SavePageRequest request1( |
| 523 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested); | 532 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested); |
| 524 offline_pages::SavePageRequest request2( | 533 offline_pages::SavePageRequest request2( |
| 525 kRequestId + 1, kUrl, kClientId, base::Time::Now(), kUserRequested); | 534 kRequestId + 1, kUrl, kClientId, base::Time::Now(), kUserRequested); |
| 526 request2.set_attempt_count(kAttemptCount); | 535 request2.set_attempt_count(kAttemptCount); |
| 527 coordinator()->queue()->AddRequest( | 536 coordinator()->queue()->AddRequest( |
| 528 request1, | 537 request1, |
| 529 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 538 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 557 coordinator()->queue()->GetRequests( | 566 coordinator()->queue()->GetRequests( |
| 558 base::Bind(&RequestCoordinatorTest::GetRequestsDone, | 567 base::Bind(&RequestCoordinatorTest::GetRequestsDone, |
| 559 base::Unretained(this))); | 568 base::Unretained(this))); |
| 560 PumpLoop(); | 569 PumpLoop(); |
| 561 | 570 |
| 562 // We should find one request in the queue. | 571 // We should find one request in the queue. |
| 563 EXPECT_EQ(1UL, last_requests().size()); | 572 EXPECT_EQ(1UL, last_requests().size()); |
| 564 } | 573 } |
| 565 | 574 |
| 566 } // namespace offline_pages | 575 } // namespace offline_pages |
| OLD | NEW |