| 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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 std::unique_ptr<OfflinerStub> offliner_; | 126 std::unique_ptr<OfflinerStub> offliner_; |
| 127 }; | 127 }; |
| 128 | 128 |
| 129 class ObserverStub : public RequestCoordinator::Observer { | 129 class ObserverStub : public RequestCoordinator::Observer { |
| 130 public: | 130 public: |
| 131 ObserverStub() | 131 ObserverStub() |
| 132 : added_called_(false), | 132 : added_called_(false), |
| 133 completed_called_(false), | 133 completed_called_(false), |
| 134 changed_called_(false), | 134 changed_called_(false), |
| 135 last_status_(RequestCoordinator::SavePageStatus::SUCCESS), | 135 last_status_(RequestCoordinator::SavePageStatus::SUCCESS), |
| 136 previous_status_(RequestCoordinator::SavePageStatus::SUCCESS), | |
| 137 state_(SavePageRequest::RequestState::PRERENDERING) {} | 136 state_(SavePageRequest::RequestState::PRERENDERING) {} |
| 138 | 137 |
| 139 void Clear() { | 138 void Clear() { |
| 140 added_called_ = false; | 139 added_called_ = false; |
| 141 completed_called_ = false; | 140 completed_called_ = false; |
| 142 changed_called_ = false; | 141 changed_called_ = false; |
| 143 state_ = SavePageRequest::RequestState::PRERENDERING; | 142 state_ = SavePageRequest::RequestState::PRERENDERING; |
| 144 last_status_ = RequestCoordinator::SavePageStatus::SUCCESS; | 143 last_status_ = RequestCoordinator::SavePageStatus::SUCCESS; |
| 145 previous_status_ = RequestCoordinator::SavePageStatus::SUCCESS; | |
| 146 } | 144 } |
| 147 | 145 |
| 148 void OnAdded(const SavePageRequest& request) override { | 146 void OnAdded(const SavePageRequest& request) override { |
| 149 added_called_ = true; | 147 added_called_ = true; |
| 150 } | 148 } |
| 151 | 149 |
| 152 void OnCompleted(const SavePageRequest& request, | 150 void OnCompleted(const SavePageRequest& request, |
| 153 RequestCoordinator::SavePageStatus status) override { | 151 RequestCoordinator::SavePageStatus status) override { |
| 154 completed_called_ = true; | 152 completed_called_ = true; |
| 155 previous_status_ = last_status_; | |
| 156 last_status_ = status; | 153 last_status_ = status; |
| 157 } | 154 } |
| 158 | 155 |
| 159 void OnChanged(const SavePageRequest& request) override { | 156 void OnChanged(const SavePageRequest& request) override { |
| 160 changed_called_ = true; | 157 changed_called_ = true; |
| 161 state_ = request.request_state(); | 158 state_ = request.request_state(); |
| 162 } | 159 } |
| 163 | 160 |
| 164 bool added_called() { return added_called_; } | 161 bool added_called() { return added_called_; } |
| 165 bool completed_called() { return completed_called_; } | 162 bool completed_called() { return completed_called_; } |
| 166 bool changed_called() { return changed_called_; } | 163 bool changed_called() { return changed_called_; } |
| 167 RequestCoordinator::SavePageStatus last_status() { return last_status_; } | 164 RequestCoordinator::SavePageStatus last_status() { return last_status_; } |
| 168 RequestCoordinator::SavePageStatus previous_status() { | |
| 169 return previous_status_; | |
| 170 } | |
| 171 SavePageRequest::RequestState state() { return state_; } | 165 SavePageRequest::RequestState state() { return state_; } |
| 172 | 166 |
| 173 private: | 167 private: |
| 174 bool added_called_; | 168 bool added_called_; |
| 175 bool completed_called_; | 169 bool completed_called_; |
| 176 bool changed_called_; | 170 bool changed_called_; |
| 177 RequestCoordinator::SavePageStatus last_status_; | 171 RequestCoordinator::SavePageStatus last_status_; |
| 178 RequestCoordinator::SavePageStatus previous_status_; | |
| 179 SavePageRequest::RequestState state_; | 172 SavePageRequest::RequestState state_; |
| 180 }; | 173 }; |
| 181 | 174 |
| 182 class RequestCoordinatorTest | 175 class RequestCoordinatorTest |
| 183 : public testing::Test { | 176 : public testing::Test { |
| 184 public: | 177 public: |
| 185 RequestCoordinatorTest(); | 178 RequestCoordinatorTest(); |
| 186 ~RequestCoordinatorTest() override; | 179 ~RequestCoordinatorTest() override; |
| 187 | 180 |
| 188 void SetUp() override; | 181 void SetUp() override; |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 PumpLoop(); | 428 PumpLoop(); |
| 436 | 429 |
| 437 // We should not find any requests in the queue anymore. | 430 // We should not find any requests in the queue anymore. |
| 438 // RequestPicker should *not* have tried to start an additional job, | 431 // RequestPicker should *not* have tried to start an additional job, |
| 439 // because the request queue is empty now. | 432 // because the request queue is empty now. |
| 440 EXPECT_EQ(0UL, last_requests().size()); | 433 EXPECT_EQ(0UL, last_requests().size()); |
| 441 // Check that the observer got the notification that we succeeded, and that | 434 // Check that the observer got the notification that we succeeded, and that |
| 442 // the request got removed from the queue. | 435 // the request got removed from the queue. |
| 443 EXPECT_TRUE(observer().completed_called()); | 436 EXPECT_TRUE(observer().completed_called()); |
| 444 EXPECT_EQ(RequestCoordinator::SavePageStatus::SUCCESS, | 437 EXPECT_EQ(RequestCoordinator::SavePageStatus::SUCCESS, |
| 445 observer().previous_status()); | |
| 446 EXPECT_EQ(RequestCoordinator::SavePageStatus::REMOVED, | |
| 447 observer().last_status()); | 438 observer().last_status()); |
| 448 } | 439 } |
| 449 | 440 |
| 450 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { | 441 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { |
| 451 // Add a request to the queue, wait for callbacks to finish. | 442 // Add a request to the queue, wait for callbacks to finish. |
| 452 offline_pages::SavePageRequest request( | 443 offline_pages::SavePageRequest request( |
| 453 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); | 444 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
| 454 request.MarkAttemptStarted(base::Time::Now()); | 445 request.MarkAttemptStarted(base::Time::Now()); |
| 455 coordinator()->queue()->AddRequest( | 446 coordinator()->queue()->AddRequest( |
| 456 request, | 447 request, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 base::Unretained(this))); | 487 base::Unretained(this))); |
| 497 PumpLoop(); | 488 PumpLoop(); |
| 498 | 489 |
| 499 // Now just one request in the queue since failed request removed | 490 // Now just one request in the queue since failed request removed |
| 500 // (for single attempt policy). | 491 // (for single attempt policy). |
| 501 EXPECT_EQ(1UL, last_requests().size()); | 492 EXPECT_EQ(1UL, last_requests().size()); |
| 502 // Check that the observer got the notification that we failed (and the | 493 // Check that the observer got the notification that we failed (and the |
| 503 // subsequent notification that the request was removed). | 494 // subsequent notification that the request was removed). |
| 504 EXPECT_TRUE(observer().completed_called()); | 495 EXPECT_TRUE(observer().completed_called()); |
| 505 EXPECT_EQ(RequestCoordinator::SavePageStatus::RETRY_COUNT_EXCEEDED, | 496 EXPECT_EQ(RequestCoordinator::SavePageStatus::RETRY_COUNT_EXCEEDED, |
| 506 observer().previous_status()); | |
| 507 EXPECT_EQ(RequestCoordinator::SavePageStatus::REMOVED, | |
| 508 observer().last_status()); | 497 observer().last_status()); |
| 509 } | 498 } |
| 510 | 499 |
| 511 TEST_F(RequestCoordinatorTest, OfflinerDoneForegroundCancel) { | 500 TEST_F(RequestCoordinatorTest, OfflinerDoneForegroundCancel) { |
| 512 // Add a request to the queue, wait for callbacks to finish. | 501 // Add a request to the queue, wait for callbacks to finish. |
| 513 offline_pages::SavePageRequest request( | 502 offline_pages::SavePageRequest request( |
| 514 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); | 503 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
| 515 request.MarkAttemptStarted(base::Time::Now()); | 504 request.MarkAttemptStarted(base::Time::Now()); |
| 516 coordinator()->queue()->AddRequest( | 505 coordinator()->queue()->AddRequest( |
| 517 request, base::Bind(&RequestCoordinatorTest::AddRequestDone, | 506 request, base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 833 PumpLoop(); | 822 PumpLoop(); |
| 834 | 823 |
| 835 EXPECT_TRUE(observer().completed_called()); | 824 EXPECT_TRUE(observer().completed_called()); |
| 836 EXPECT_EQ(RequestCoordinator::SavePageStatus::REMOVED, | 825 EXPECT_EQ(RequestCoordinator::SavePageStatus::REMOVED, |
| 837 observer().last_status()); | 826 observer().last_status()); |
| 838 EXPECT_EQ(1UL, last_remove_results().size()); | 827 EXPECT_EQ(1UL, last_remove_results().size()); |
| 839 EXPECT_EQ(kRequestId1, std::get<0>(last_remove_results().at(0))); | 828 EXPECT_EQ(kRequestId1, std::get<0>(last_remove_results().at(0))); |
| 840 } | 829 } |
| 841 | 830 |
| 842 } // namespace offline_pages | 831 } // namespace offline_pages |
| OLD | NEW |