| 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 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/location.h" |
| 11 #include "base/test/test_simple_task_runner.h" | 12 #include "base/test/test_simple_task_runner.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "components/offline_pages/background/offliner.h" |
| 13 #include "components/offline_pages/background/offliner_factory.h" | 15 #include "components/offline_pages/background/offliner_factory.h" |
| 14 #include "components/offline_pages/background/offliner_policy.h" | 16 #include "components/offline_pages/background/offliner_policy.h" |
| 15 #include "components/offline_pages/background/request_queue.h" | 17 #include "components/offline_pages/background/request_queue.h" |
| 16 #include "components/offline_pages/background/request_queue_in_memory_store.h" | 18 #include "components/offline_pages/background/request_queue_in_memory_store.h" |
| 17 #include "components/offline_pages/background/save_page_request.h" | 19 #include "components/offline_pages/background/save_page_request.h" |
| 18 #include "components/offline_pages/background/scheduler.h" | 20 #include "components/offline_pages/background/scheduler.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 22 |
| 21 namespace offline_pages { | 23 namespace offline_pages { |
| 22 | 24 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 41 | 43 |
| 42 bool schedule_called() const { return schedule_called_; } | 44 bool schedule_called() const { return schedule_called_; } |
| 43 | 45 |
| 44 bool unschedule_called() const { return unschedule_called_; } | 46 bool unschedule_called() const { return unschedule_called_; } |
| 45 | 47 |
| 46 private: | 48 private: |
| 47 bool schedule_called_; | 49 bool schedule_called_; |
| 48 bool unschedule_called_; | 50 bool unschedule_called_; |
| 49 }; | 51 }; |
| 50 | 52 |
| 53 class OfflinerStub : public Offliner { |
| 54 bool LoadAndSave(const SavePageRequest& request, |
| 55 const CompletionCallback& callback) override { |
| 56 // Post the callback on the run loop. |
| 57 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 58 FROM_HERE, base::Bind(callback, request, Offliner::SAVED)); |
| 59 return true; |
| 60 } |
| 61 |
| 62 // Clears the currently processing request, if any. |
| 63 void Cancel() override {} |
| 64 }; |
| 65 |
| 66 class OfflinerFactoryStub : public OfflinerFactory { |
| 67 public: |
| 68 Offliner* GetOffliner(const OfflinerPolicy* policy) override { |
| 69 offliner_.reset(new OfflinerStub()); |
| 70 return offliner_.get(); |
| 71 } |
| 72 |
| 73 private: |
| 74 std::unique_ptr<Offliner> offliner_; |
| 75 }; |
| 76 |
| 51 class RequestCoordinatorTest | 77 class RequestCoordinatorTest |
| 52 : public testing::Test { | 78 : public testing::Test { |
| 53 public: | 79 public: |
| 54 RequestCoordinatorTest(); | 80 RequestCoordinatorTest(); |
| 55 ~RequestCoordinatorTest() override; | 81 ~RequestCoordinatorTest() override; |
| 56 | 82 |
| 57 void SetUp() override; | 83 void SetUp() override; |
| 58 | 84 |
| 59 void PumpLoop(); | 85 void PumpLoop(); |
| 60 | 86 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 88 | 114 |
| 89 RequestCoordinatorTest::RequestCoordinatorTest() | 115 RequestCoordinatorTest::RequestCoordinatorTest() |
| 90 : last_get_requests_result_(RequestQueue::GetRequestsResult::kStoreFailure), | 116 : last_get_requests_result_(RequestQueue::GetRequestsResult::kStoreFailure), |
| 91 task_runner_(new base::TestSimpleTaskRunner), | 117 task_runner_(new base::TestSimpleTaskRunner), |
| 92 task_runner_handle_(task_runner_) {} | 118 task_runner_handle_(task_runner_) {} |
| 93 | 119 |
| 94 RequestCoordinatorTest::~RequestCoordinatorTest() {} | 120 RequestCoordinatorTest::~RequestCoordinatorTest() {} |
| 95 | 121 |
| 96 void RequestCoordinatorTest::SetUp() { | 122 void RequestCoordinatorTest::SetUp() { |
| 97 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); | 123 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); |
| 98 std::unique_ptr<OfflinerFactory> factory; | 124 std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub()); |
| 99 std::unique_ptr<RequestQueueInMemoryStore> | 125 std::unique_ptr<RequestQueueInMemoryStore> |
| 100 store(new RequestQueueInMemoryStore()); | 126 store(new RequestQueueInMemoryStore()); |
| 101 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store))); | 127 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store))); |
| 102 std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub()); | 128 std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub()); |
| 103 coordinator_.reset(new RequestCoordinator( | 129 coordinator_.reset(new RequestCoordinator( |
| 104 std::move(policy), std::move(factory), std::move(queue), | 130 std::move(policy), std::move(factory), std::move(queue), |
| 105 std::move(scheduler_stub))); | 131 std::move(scheduler_stub))); |
| 106 } | 132 } |
| 107 | 133 |
| 108 void RequestCoordinatorTest::PumpLoop() { | 134 void RequestCoordinatorTest::PumpLoop() { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 125 } | 151 } |
| 126 | 152 |
| 127 TEST_F(RequestCoordinatorTest, SavePageLater) { | 153 TEST_F(RequestCoordinatorTest, SavePageLater) { |
| 128 EXPECT_TRUE(coordinator()->SavePageLater(kUrl, kClientId)); | 154 EXPECT_TRUE(coordinator()->SavePageLater(kUrl, kClientId)); |
| 129 | 155 |
| 130 // Expect that a request got placed on the queue. | 156 // Expect that a request got placed on the queue. |
| 131 coordinator()->GetQueue()->GetRequests( | 157 coordinator()->GetQueue()->GetRequests( |
| 132 base::Bind(&RequestCoordinatorTest::GetRequestsDone, | 158 base::Bind(&RequestCoordinatorTest::GetRequestsDone, |
| 133 base::Unretained(this))); | 159 base::Unretained(this))); |
| 134 | 160 |
| 135 // Wait for callback to finish. | 161 // Wait for callbacks to finish, both request queue and offliner. |
| 136 PumpLoop(); | 162 PumpLoop(); |
| 137 | 163 |
| 138 // Check the results are as expected. | 164 // Check the request queue is as expected. |
| 139 EXPECT_EQ(1UL, last_requests().size()); | 165 EXPECT_EQ(1UL, last_requests().size()); |
| 140 EXPECT_EQ(kUrl, last_requests()[0].url()); | 166 EXPECT_EQ(kUrl, last_requests()[0].url()); |
| 141 EXPECT_EQ(kClientId, last_requests()[0].client_id()); | 167 EXPECT_EQ(kClientId, last_requests()[0].client_id()); |
| 142 | 168 |
| 143 // Expect that the scheduler got notified. | 169 // Expect that the scheduler got notified. |
| 144 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( | 170 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( |
| 145 coordinator()->GetSchedulerForTesting()); | 171 coordinator()->GetSchedulerForTesting()); |
| 146 EXPECT_TRUE(scheduler_stub->schedule_called()); | 172 EXPECT_TRUE(scheduler_stub->schedule_called()); |
| 173 |
| 174 // Check that the offliner callback got a response. |
| 175 EXPECT_EQ(Offliner::SAVED, coordinator()->last_offlining_status()); |
| 176 |
| 177 // TODO(petewil): Expect that the scheduler got notified. |
| 147 } | 178 } |
| 148 | 179 |
| 149 } // namespace offline_pages | 180 } // namespace offline_pages |
| OLD | NEW |