| 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 ebcbe140f39a7b3f1a30b8ffbf8e619d52ed20f4..0953dd61dc9277640a1165b793ddd8b2682410ab 100644
|
| --- a/components/offline_pages/background/request_coordinator_unittest.cc
|
| +++ b/components/offline_pages/background/request_coordinator_unittest.cc
|
| @@ -10,6 +10,7 @@
|
|
|
| #include "base/bind.h"
|
| #include "base/location.h"
|
| +#include "base/logging.h"
|
| #include "base/test/test_simple_task_runner.h"
|
| #include "base/threading/thread_task_runner_handle.h"
|
| #include "components/offline_pages/background/device_conditions.h"
|
| @@ -54,23 +55,37 @@ class SchedulerStub : public Scheduler {
|
| };
|
|
|
| class OfflinerStub : public Offliner {
|
| + public:
|
| + OfflinerStub() : skip_callback_(false) {}
|
| +
|
| bool LoadAndSave(const SavePageRequest& request,
|
| const CompletionCallback& callback) override {
|
| // Post the callback on the run loop.
|
| - base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| - FROM_HERE,
|
| - base::Bind(callback, request, Offliner::RequestStatus::SAVED));
|
| + if (!skip_callback_) {
|
| + base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(callback, request, Offliner::RequestStatus::SAVED));
|
| + }
|
| return true;
|
| }
|
|
|
| // Clears the currently processing request, if any.
|
| void Cancel() override {}
|
| +
|
| + void set_skip_callback(bool skip) {
|
| + skip_callback_ = skip;
|
| + }
|
| +
|
| + private:
|
| + bool skip_callback_;
|
| };
|
|
|
| class OfflinerFactoryStub : public OfflinerFactory {
|
| public:
|
| Offliner* GetOffliner(const OfflinerPolicy* policy) override {
|
| - offliner_.reset(new OfflinerStub());
|
| + if (offliner_.get() == nullptr) {
|
| + offliner_.reset(new OfflinerStub());
|
| + }
|
| return offliner_.get();
|
| }
|
|
|
| @@ -92,6 +107,14 @@ class RequestCoordinatorTest
|
| return coordinator_.get();
|
| }
|
|
|
| + bool is_busy() {
|
| + return coordinator_->is_busy();
|
| + }
|
| +
|
| + void SendRequestToOffliner(SavePageRequest& request) {
|
| + coordinator_->SendRequestToOffliner(request);
|
| + }
|
| +
|
| // Empty callback function
|
| void EmptyCallbackFunction(bool result) {
|
| }
|
| @@ -115,24 +138,32 @@ class RequestCoordinatorTest
|
| return last_requests_;
|
| }
|
|
|
| + void skip_offliner_callback() {
|
| + offliner_->set_skip_callback(true);
|
| + }
|
| +
|
| private:
|
| RequestQueue::GetRequestsResult last_get_requests_result_;
|
| std::vector<SavePageRequest> last_requests_;
|
| scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
|
| base::ThreadTaskRunnerHandle task_runner_handle_;
|
| std::unique_ptr<RequestCoordinator> coordinator_;
|
| + OfflinerStub* offliner_;
|
| };
|
|
|
| RequestCoordinatorTest::RequestCoordinatorTest()
|
| : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE),
|
| task_runner_(new base::TestSimpleTaskRunner),
|
| - task_runner_handle_(task_runner_) {}
|
| + task_runner_handle_(task_runner_),
|
| + offliner_(nullptr) {}
|
|
|
| RequestCoordinatorTest::~RequestCoordinatorTest() {}
|
|
|
| void RequestCoordinatorTest::SetUp() {
|
| std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy());
|
| std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub());
|
| + offliner_ =
|
| + reinterpret_cast<OfflinerStub*>(factory->GetOffliner(policy.get()));
|
| std::unique_ptr<RequestQueueInMemoryStore>
|
| store(new RequestQueueInMemoryStore());
|
| std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store)));
|
| @@ -175,6 +206,30 @@ TEST_F(RequestCoordinatorTest, StartProcessingWithNoRequests) {
|
| EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback));
|
| }
|
|
|
| +TEST_F(RequestCoordinatorTest, StartProcessingWithRequestInProgress) {
|
| + // Put the request on the queue.
|
| + EXPECT_TRUE(coordinator()->SavePageLater(kUrl, kClientId));
|
| +
|
| + // Set up for the call to StartProcessing by building arguments.
|
| + DeviceConditions device_conditions(false, 75,
|
| + net::NetworkChangeNotifier::CONNECTION_3G);
|
| + base::Callback<void(bool)> callback =
|
| + base::Bind(&RequestCoordinatorTest::EmptyCallbackFunction,
|
| + base::Unretained(this));
|
| +
|
| + // Ensure that the forthcoming request does not finish - we simulate it being
|
| + // in progress by asking it to skip making the completion callback.
|
| + skip_offliner_callback();
|
| +
|
| + // Sending the request to the offliner should make it busy.
|
| + EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback));
|
| + PumpLoop();
|
| + EXPECT_TRUE(is_busy());
|
| +
|
| + // Now trying to start processing on another request should return false.
|
| + EXPECT_FALSE(coordinator()->StartProcessing(device_conditions, callback));
|
| +}
|
| +
|
| TEST_F(RequestCoordinatorTest, SavePageLater) {
|
| EXPECT_TRUE(coordinator()->SavePageLater(kUrl, kClientId));
|
|
|
|
|