| 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 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 base::Unretained(this)); | 470 base::Unretained(this)); |
| 471 coordinator()->SetProcessingCallbackForTest(callback); | 471 coordinator()->SetProcessingCallbackForTest(callback); |
| 472 | 472 |
| 473 // Set up device conditions for the test. | 473 // Set up device conditions for the test. |
| 474 DeviceConditions device_conditions( | 474 DeviceConditions device_conditions( |
| 475 false, 75, net::NetworkChangeNotifier::CONNECTION_3G); | 475 false, 75, net::NetworkChangeNotifier::CONNECTION_3G); |
| 476 SetDeviceConditionsForTest(device_conditions); | 476 SetDeviceConditionsForTest(device_conditions); |
| 477 | 477 |
| 478 // Call the OfflinerDoneCallback to simulate the page being completed, wait | 478 // Call the OfflinerDoneCallback to simulate the page being completed, wait |
| 479 // for callbacks. | 479 // for callbacks. |
| 480 EnableOfflinerCallback(true); | |
| 481 SendOfflinerDoneCallback(request, Offliner::RequestStatus::SAVED); | 480 SendOfflinerDoneCallback(request, Offliner::RequestStatus::SAVED); |
| 482 PumpLoop(); | 481 PumpLoop(); |
| 483 | 482 |
| 484 // Verify the request gets removed from the queue, and wait for callbacks. | 483 // Verify the request gets removed from the queue, and wait for callbacks. |
| 485 coordinator()->queue()->GetRequests( | 484 coordinator()->queue()->GetRequests( |
| 486 base::Bind(&RequestCoordinatorTest::GetRequestsDone, | 485 base::Bind(&RequestCoordinatorTest::GetRequestsDone, |
| 487 base::Unretained(this))); | 486 base::Unretained(this))); |
| 488 PumpLoop(); | 487 PumpLoop(); |
| 489 | 488 |
| 490 // We should not find any requests in the queue anymore. | 489 // We should not find any requests in the queue anymore. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 base::Unretained(this)); | 524 base::Unretained(this)); |
| 526 coordinator()->SetProcessingCallbackForTest(callback); | 525 coordinator()->SetProcessingCallbackForTest(callback); |
| 527 | 526 |
| 528 // Set up device conditions for the test. | 527 // Set up device conditions for the test. |
| 529 DeviceConditions device_conditions( | 528 DeviceConditions device_conditions( |
| 530 false, 75, net::NetworkChangeNotifier::CONNECTION_3G); | 529 false, 75, net::NetworkChangeNotifier::CONNECTION_3G); |
| 531 SetDeviceConditionsForTest(device_conditions); | 530 SetDeviceConditionsForTest(device_conditions); |
| 532 | 531 |
| 533 // Call the OfflinerDoneCallback to simulate the request failed, wait | 532 // Call the OfflinerDoneCallback to simulate the request failed, wait |
| 534 // for callbacks. | 533 // for callbacks. |
| 535 EnableOfflinerCallback(true); | |
| 536 SendOfflinerDoneCallback(request, | 534 SendOfflinerDoneCallback(request, |
| 537 Offliner::RequestStatus::PRERENDERING_FAILED); | 535 Offliner::RequestStatus::PRERENDERING_FAILED); |
| 538 PumpLoop(); | 536 PumpLoop(); |
| 539 | 537 |
| 540 // TODO(dougarnett): Consider injecting mock RequestPicker for this test | 538 // TODO(dougarnett): Consider injecting mock RequestPicker for this test |
| 541 // and verifying that there is no attempt to pick another request following | 539 // and verifying that there is no attempt to pick another request following |
| 542 // this failure code. | 540 // this failure code. |
| 543 | 541 |
| 544 // Verify neither request is removed from the queue; wait for callbacks. | |
| 545 coordinator()->queue()->GetRequests( | 542 coordinator()->queue()->GetRequests( |
| 546 base::Bind(&RequestCoordinatorTest::GetRequestsDone, | 543 base::Bind(&RequestCoordinatorTest::GetRequestsDone, |
| 547 base::Unretained(this))); | 544 base::Unretained(this))); |
| 548 PumpLoop(); | 545 PumpLoop(); |
| 549 | 546 |
| 550 // Now just one request in the queue since failed request removed | 547 // Now just one request in the queue since failed request removed |
| 551 // (for single attempt policy). | 548 // (for single attempt policy). |
| 552 EXPECT_EQ(1UL, last_requests().size()); | 549 EXPECT_EQ(1UL, last_requests().size()); |
| 553 // Check that the observer got the notification that we failed (and the | 550 // Check that the observer got the notification that we failed (and the |
| 554 // subsequent notification that the request was removed). | 551 // subsequent notification that the request was removed). |
| 555 EXPECT_TRUE(observer().completed_called()); | 552 EXPECT_TRUE(observer().completed_called()); |
| 556 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::RETRY_COUNT_EXCEEDED, | 553 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::RETRY_COUNT_EXCEEDED, |
| 557 observer().last_status()); | 554 observer().last_status()); |
| 558 } | 555 } |
| 559 | 556 |
| 557 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailedNoRetryFailure) { |
| 558 // Add a request to the queue, wait for callbacks to finish. |
| 559 offline_pages::SavePageRequest request(kRequestId1, kUrl1, kClientId1, |
| 560 base::Time::Now(), kUserRequested); |
| 561 request.MarkAttemptStarted(base::Time::Now()); |
| 562 coordinator()->queue()->AddRequest( |
| 563 request, base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 564 base::Unretained(this))); |
| 565 PumpLoop(); |
| 566 |
| 567 // Add second request to the queue to check handling when first fails. |
| 568 offline_pages::SavePageRequest request2(kRequestId2, kUrl2, kClientId2, |
| 569 base::Time::Now(), kUserRequested); |
| 570 coordinator()->queue()->AddRequest( |
| 571 request2, base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 572 base::Unretained(this))); |
| 573 PumpLoop(); |
| 574 |
| 575 // We need to give a callback to the request. |
| 576 base::Callback<void(bool)> callback = base::Bind( |
| 577 &RequestCoordinatorTest::EmptyCallbackFunction, base::Unretained(this)); |
| 578 coordinator()->SetProcessingCallbackForTest(callback); |
| 579 |
| 580 // Set up device conditions for the test. |
| 581 DeviceConditions device_conditions(false, 75, |
| 582 net::NetworkChangeNotifier::CONNECTION_3G); |
| 583 SetDeviceConditionsForTest(device_conditions); |
| 584 |
| 585 // Call the OfflinerDoneCallback to simulate the request failed, wait |
| 586 // for callbacks. |
| 587 SendOfflinerDoneCallback( |
| 588 request, Offliner::RequestStatus::PRERENDERING_FAILED_NO_RETRY); |
| 589 PumpLoop(); |
| 590 |
| 591 // TODO(dougarnett): Consider injecting mock RequestPicker for this test |
| 592 // and verifying that there is as attempt to pick another request following |
| 593 // this non-retryable failure code. |
| 594 |
| 595 coordinator()->queue()->GetRequests(base::Bind( |
| 596 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this))); |
| 597 PumpLoop(); |
| 598 |
| 599 // Now just one request in the queue since non-retryable failure. |
| 600 EXPECT_EQ(1UL, last_requests().size()); |
| 601 // Check that the observer got the notification that we failed (and the |
| 602 // subsequent notification that the request was removed). |
| 603 EXPECT_TRUE(observer().completed_called()); |
| 604 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::PRERENDER_FAILURE, |
| 605 observer().last_status()); |
| 606 } |
| 607 |
| 560 TEST_F(RequestCoordinatorTest, OfflinerDoneForegroundCancel) { | 608 TEST_F(RequestCoordinatorTest, OfflinerDoneForegroundCancel) { |
| 561 // Add a request to the queue, wait for callbacks to finish. | 609 // Add a request to the queue, wait for callbacks to finish. |
| 562 offline_pages::SavePageRequest request( | 610 offline_pages::SavePageRequest request( |
| 563 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); | 611 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
| 564 request.MarkAttemptStarted(base::Time::Now()); | 612 request.MarkAttemptStarted(base::Time::Now()); |
| 565 coordinator()->queue()->AddRequest( | 613 coordinator()->queue()->AddRequest( |
| 566 request, base::Bind(&RequestCoordinatorTest::AddRequestDone, | 614 request, base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 567 base::Unretained(this))); | 615 base::Unretained(this))); |
| 568 PumpLoop(); | 616 PumpLoop(); |
| 569 | 617 |
| 570 // We need to give a callback to the request. | 618 // We need to give a callback to the request. |
| 571 base::Callback<void(bool)> callback = base::Bind( | 619 base::Callback<void(bool)> callback = base::Bind( |
| 572 &RequestCoordinatorTest::EmptyCallbackFunction, base::Unretained(this)); | 620 &RequestCoordinatorTest::EmptyCallbackFunction, base::Unretained(this)); |
| 573 coordinator()->SetProcessingCallbackForTest(callback); | 621 coordinator()->SetProcessingCallbackForTest(callback); |
| 574 | 622 |
| 575 // Set up device conditions for the test. | 623 // Set up device conditions for the test. |
| 576 DeviceConditions device_conditions(false, 75, | 624 DeviceConditions device_conditions(false, 75, |
| 577 net::NetworkChangeNotifier::CONNECTION_3G); | 625 net::NetworkChangeNotifier::CONNECTION_3G); |
| 578 SetDeviceConditionsForTest(device_conditions); | 626 SetDeviceConditionsForTest(device_conditions); |
| 579 | 627 |
| 580 // Call the OfflinerDoneCallback to simulate the request failed, wait | 628 // Call the OfflinerDoneCallback to simulate the request failed, wait |
| 581 // for callbacks. | 629 // for callbacks. |
| 582 EnableOfflinerCallback(true); | |
| 583 SendOfflinerDoneCallback(request, | 630 SendOfflinerDoneCallback(request, |
| 584 Offliner::RequestStatus::FOREGROUND_CANCELED); | 631 Offliner::RequestStatus::FOREGROUND_CANCELED); |
| 585 PumpLoop(); | 632 PumpLoop(); |
| 586 | 633 |
| 587 // Verify the request is not removed from the queue, and wait for callbacks. | 634 // Verify the request is not removed from the queue, and wait for callbacks. |
| 588 coordinator()->queue()->GetRequests(base::Bind( | 635 coordinator()->queue()->GetRequests(base::Bind( |
| 589 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this))); | 636 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this))); |
| 590 PumpLoop(); | 637 PumpLoop(); |
| 591 | 638 |
| 592 // Request no longer in the queue (for single attempt policy). | 639 // Request no longer in the queue (for single attempt policy). |
| (...skipping 17 matching lines...) Expand all Loading... |
| 610 &RequestCoordinatorTest::EmptyCallbackFunction, base::Unretained(this)); | 657 &RequestCoordinatorTest::EmptyCallbackFunction, base::Unretained(this)); |
| 611 coordinator()->SetProcessingCallbackForTest(callback); | 658 coordinator()->SetProcessingCallbackForTest(callback); |
| 612 | 659 |
| 613 // Set up device conditions for the test. | 660 // Set up device conditions for the test. |
| 614 DeviceConditions device_conditions(false, 75, | 661 DeviceConditions device_conditions(false, 75, |
| 615 net::NetworkChangeNotifier::CONNECTION_3G); | 662 net::NetworkChangeNotifier::CONNECTION_3G); |
| 616 SetDeviceConditionsForTest(device_conditions); | 663 SetDeviceConditionsForTest(device_conditions); |
| 617 | 664 |
| 618 // Call the OfflinerDoneCallback to simulate the request failed, wait | 665 // Call the OfflinerDoneCallback to simulate the request failed, wait |
| 619 // for callbacks. | 666 // for callbacks. |
| 620 EnableOfflinerCallback(true); | |
| 621 SendOfflinerDoneCallback(request, | 667 SendOfflinerDoneCallback(request, |
| 622 Offliner::RequestStatus::PRERENDERING_CANCELED); | 668 Offliner::RequestStatus::PRERENDERING_CANCELED); |
| 623 PumpLoop(); | 669 PumpLoop(); |
| 624 | 670 |
| 625 // Verify the request is not removed from the queue, and wait for callbacks. | 671 // Verify the request is not removed from the queue, and wait for callbacks. |
| 626 coordinator()->queue()->GetRequests(base::Bind( | 672 coordinator()->queue()->GetRequests(base::Bind( |
| 627 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this))); | 673 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this))); |
| 628 PumpLoop(); | 674 PumpLoop(); |
| 629 | 675 |
| 630 // Request still in the queue. | 676 // Request still in the queue. |
| (...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1096 // Now whether processing triggered immediately depends on whether test | 1142 // Now whether processing triggered immediately depends on whether test |
| 1097 // is run on svelte device or not. | 1143 // is run on svelte device or not. |
| 1098 if (base::SysInfo::IsLowEndDevice()) { | 1144 if (base::SysInfo::IsLowEndDevice()) { |
| 1099 EXPECT_FALSE(is_busy()); | 1145 EXPECT_FALSE(is_busy()); |
| 1100 } else { | 1146 } else { |
| 1101 EXPECT_TRUE(is_busy()); | 1147 EXPECT_TRUE(is_busy()); |
| 1102 } | 1148 } |
| 1103 } | 1149 } |
| 1104 | 1150 |
| 1105 } // namespace offline_pages | 1151 } // namespace offline_pages |
| OLD | NEW |