Index: components/offline_pages/background/request_coordinator_unittest.cc |
diff --git a/components/offline_pages/background/request_coordinator_unittest.cc b/components/offline_pages/background/request_coordinator_unittest.cc |
index 6ef185c7725ccf8ce91868b9521b35a0c1658d61..7dfa772387cb10f3e099d5ea756224002d49cd5c 100644 |
--- a/components/offline_pages/background/request_coordinator_unittest.cc |
+++ b/components/offline_pages/background/request_coordinator_unittest.cc |
@@ -126,6 +126,55 @@ class OfflinerFactoryStub : public OfflinerFactory { |
std::unique_ptr<OfflinerStub> offliner_; |
}; |
+class ObserverStub : public RequestCoordinator::Observer { |
+ public: |
+ ObserverStub() |
+ : added_called_(false), |
+ succeeded_called_(false), |
+ failed_called_(false), |
+ changed_called_(false), |
+ state_(SavePageRequest::RequestState::PRERENDERING) {} |
+ |
+ void clear() { |
fgorski
2016/08/10 21:30:45
I believe this should be Clear().
Pete Williamson
2016/08/11 00:08:37
Done.
|
+ added_called_ = false; |
+ succeeded_called_ = false; |
+ failed_called_ = false; |
+ changed_called_ = false; |
+ state_ = SavePageRequest::RequestState::PRERENDERING; |
+ } |
+ |
+ void OnAdded(const SavePageRequest& request) override { |
+ added_called_ = true; |
+ } |
+ |
+ void OnSucceeded( |
+ const SavePageRequest& request, int64_t offline_id) override { |
+ succeeded_called_ = true; |
+ } |
+ |
+ void OnFailed(const SavePageRequest& request, int64_t failure_code) override { |
+ failed_called_ = true; |
+ } |
+ |
+ void OnChanged(const SavePageRequest& request) override { |
+ changed_called_ = true; |
+ state_ = request.request_state(); |
+ } |
+ |
+ bool added_called() { return added_called_; } |
+ bool succeeded_called() { return succeeded_called_; } |
+ bool failed_called() { return failed_called_; } |
+ bool changed_called() { return changed_called_; } |
+ SavePageRequest::RequestState state() { return state_; } |
+ |
+ private: |
+ bool added_called_; |
+ bool succeeded_called_; |
+ bool failed_called_; |
+ bool changed_called_; |
+ SavePageRequest::RequestState state_; |
+}; |
+ |
class RequestCoordinatorTest |
: public testing::Test { |
public: |
@@ -205,6 +254,8 @@ class RequestCoordinatorTest |
bool OfflinerWasCanceled() const { return offliner_->cancel_called(); } |
+ ObserverStub observer() { return observer_; } |
+ |
private: |
RequestQueue::GetRequestsResult last_get_requests_result_; |
std::vector<SavePageRequest> last_requests_; |
@@ -214,6 +265,7 @@ class RequestCoordinatorTest |
std::unique_ptr<RequestCoordinator> coordinator_; |
OfflinerStub* offliner_; |
base::WaitableEvent waiter_; |
+ ObserverStub observer_; |
}; |
RequestCoordinatorTest::RequestCoordinatorTest() |
@@ -239,6 +291,7 @@ void RequestCoordinatorTest::SetUp() { |
coordinator_.reset(new RequestCoordinator( |
std::move(policy), std::move(factory), std::move(queue), |
std::move(scheduler_stub))); |
+ coordinator_->AddObserver(&observer_); |
} |
void RequestCoordinatorTest::PumpLoop() { |
@@ -326,6 +379,9 @@ TEST_F(RequestCoordinatorTest, SavePageLater) { |
->GetTriggerConditionsForUserRequest() |
.minimum_battery_percentage, |
scheduler_stub->conditions()->minimum_battery_percentage); |
+ |
+ // Check that the observer got the notification that a page is available |
+ EXPECT_EQ(true, observer().added_called()); |
fgorski
2016/08/10 21:30:44
EXPECT_TRUE and applies everywhere you do EXPECT_E
Pete Williamson
2016/08/11 00:08:36
Done everywhere.
|
} |
TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { |
@@ -367,6 +423,8 @@ TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { |
// RequestPicker should *not* have tried to start an additional job, |
// because the request queue is empty now. |
EXPECT_EQ(0UL, last_requests().size()); |
+ // Check that the observer got the notification that we succeeded. |
+ EXPECT_EQ(true, observer().succeeded_called()); |
} |
TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { |
@@ -421,6 +479,8 @@ TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { |
// Now just one request in the queue since failed request removed |
// (for single attempt policy). |
EXPECT_EQ(1UL, last_requests().size()); |
+ // Check that the observer got the notification that we failed |
+ EXPECT_EQ(true, observer().failed_called()); |
} |
TEST_F(RequestCoordinatorTest, OfflinerDoneForegroundCancel) { |
@@ -659,4 +719,33 @@ TEST_F(RequestCoordinatorTest, GetQueuedRequests) { |
EXPECT_EQ(kId2, last_client_ids().at(1).id); |
} |
+TEST_F(RequestCoordinatorTest, PauseAndResumeObserver) { |
+ // Add a request to the queue. |
+ offline_pages::SavePageRequest request1(kRequestId1, kUrl1, kClientId1, |
+ base::Time::Now(), kUserRequested); |
+ coordinator()->queue()->AddRequest( |
+ request1, base::Bind(&RequestCoordinatorTest::AddRequestDone, |
+ base::Unretained(this))); |
+ PumpLoop(); |
+ |
+ // Pause the request. |
+ std::vector<int64_t> request_ids; |
+ request_ids.push_back(kRequestId1); |
+ coordinator()->PauseRequests(request_ids); |
+ PumpLoop(); |
+ |
+ EXPECT_EQ(true, observer().changed_called()); |
+ EXPECT_EQ(SavePageRequest::RequestState::PAUSED, observer().state()); |
+ |
+ // Clear out the observer before the next call. |
+ observer().clear(); |
+ |
+ // Resume the request. |
+ coordinator()->ResumeRequests(request_ids); |
+ PumpLoop(); |
+ |
+ EXPECT_EQ(true, observer().changed_called()); |
+ EXPECT_EQ(SavePageRequest::RequestState::AVAILABLE, observer().state()); |
+} |
+ |
} // namespace offline_pages |