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..20700ea1dcde779eb83b6ccb4d150b8212e50c65 100644 |
--- a/components/offline_pages/background/request_coordinator_unittest.cc |
+++ b/components/offline_pages/background/request_coordinator_unittest.cc |
@@ -126,6 +126,49 @@ class OfflinerFactoryStub : public OfflinerFactory { |
std::unique_ptr<OfflinerStub> offliner_; |
}; |
+class ObserverStub : public RequestCoordinator::Observer { |
+ public: |
+ ObserverStub() |
+ : available_called_(false), |
+ succeeded_called_(false), |
+ failed_called_(false), |
+ paused_called_(false) {} |
+ |
+ void clear() { |
+ available_called_ = false; |
+ succeeded_called_ = false; |
+ failed_called_ = false; |
+ paused_called_ = false; |
+ } |
+ |
+ void OnAvailable(RequestCoordinator::RequestInfo info) override { |
+ available_called_ = true; |
+ } |
+ |
+ void OnSucceeded(RequestCoordinator::RequestInfo info) override { |
+ succeeded_called_ = true; |
+ } |
+ |
+ void OnFailed(RequestCoordinator::RequestInfo info) override { |
+ failed_called_ = true; |
+ } |
+ |
+ void OnPaused(RequestCoordinator::RequestInfo info) override { |
+ paused_called_ = true; |
+ } |
+ |
+ bool available_called() { return available_called_; } |
+ bool succeeded_called() { return succeeded_called_; } |
+ bool failed_called() { return failed_called_; } |
+ bool paused_called() { return paused_called_; } |
+ |
+ private: |
+ bool available_called_; |
+ bool succeeded_called_; |
+ bool failed_called_; |
+ bool paused_called_; |
+}; |
+ |
class RequestCoordinatorTest |
: public testing::Test { |
public: |
@@ -205,6 +248,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 +259,7 @@ class RequestCoordinatorTest |
std::unique_ptr<RequestCoordinator> coordinator_; |
OfflinerStub* offliner_; |
base::WaitableEvent waiter_; |
+ ObserverStub observer_; |
}; |
RequestCoordinatorTest::RequestCoordinatorTest() |
@@ -239,6 +285,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 +373,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().available_called()); |
} |
TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { |
@@ -367,6 +417,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 +473,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 +713,31 @@ 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<ClientId> client_ids; |
+ client_ids.push_back(kClientId1); |
+ coordinator()->PauseRequests(client_ids); |
+ PumpLoop(); |
+ |
+ EXPECT_EQ(true, observer().paused_called()); |
+ |
+ // Clear out the observer before the next call. |
+ observer().clear(); |
+ |
+ // Resume the request. |
+ coordinator()->ResumeRequests(client_ids); |
+ PumpLoop(); |
+ |
+ EXPECT_EQ(true, observer().available_called()); |
+} |
+ |
} // namespace offline_pages |