| 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/test/test_simple_task_runner.h" | 11 #include "base/test/test_simple_task_runner.h" |
| 12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "components/offline_pages/background/offliner_factory.h" | 13 #include "components/offline_pages/background/offliner_factory.h" |
| 14 #include "components/offline_pages/background/offliner_policy.h" | 14 #include "components/offline_pages/background/offliner_policy.h" |
| 15 #include "components/offline_pages/background/request_queue.h" | 15 #include "components/offline_pages/background/request_queue.h" |
| 16 #include "components/offline_pages/background/request_queue_in_memory_store.h" | 16 #include "components/offline_pages/background/request_queue_in_memory_store.h" |
| 17 #include "components/offline_pages/background/save_page_request.h" | 17 #include "components/offline_pages/background/save_page_request.h" |
| 18 #include "components/offline_pages/background/scheduler.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 namespace offline_pages { | 21 namespace offline_pages { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 // put test constants here | 24 // put test constants here |
| 24 const GURL kUrl("http://universe.com/everything"); | 25 const GURL kUrl("http://universe.com/everything"); |
| 25 const ClientId kClientId("bookmark", "42"); | 26 const ClientId kClientId("bookmark", "42"); |
| 26 } // namespace | 27 } // namespace |
| 27 | 28 |
| 29 class SchedulerStub : public Scheduler { |
| 30 public: |
| 31 SchedulerStub() : schedule_called_(false), unschedule_called_(false) {} |
| 32 |
| 33 void Schedule(const TriggerCondition& trigger_condition) override { |
| 34 schedule_called_ = true; |
| 35 } |
| 36 |
| 37 // Unschedules the currently scheduled task, if any. |
| 38 void Unschedule() override { |
| 39 unschedule_called_ = true; |
| 40 } |
| 41 |
| 42 bool schedule_called() const { return schedule_called_; } |
| 43 |
| 44 bool unschedule_called() const { return unschedule_called_; } |
| 45 |
| 46 private: |
| 47 bool schedule_called_; |
| 48 bool unschedule_called_; |
| 49 }; |
| 50 |
| 28 class RequestCoordinatorTest | 51 class RequestCoordinatorTest |
| 29 : public testing::Test { | 52 : public testing::Test { |
| 30 public: | 53 public: |
| 31 RequestCoordinatorTest(); | 54 RequestCoordinatorTest(); |
| 32 ~RequestCoordinatorTest() override; | 55 ~RequestCoordinatorTest() override; |
| 33 | 56 |
| 34 void SetUp() override; | 57 void SetUp() override; |
| 35 | 58 |
| 36 void PumpLoop(); | 59 void PumpLoop(); |
| 37 | 60 |
| 38 RequestCoordinator* getCoordinator() { | 61 RequestCoordinator* coordinator() { |
| 39 return coordinator_.get(); | 62 return coordinator_.get(); |
| 40 } | 63 } |
| 41 | 64 |
| 42 // Empty callback function | 65 // Empty callback function |
| 43 void EmptyCallbackFunction() { | 66 void EmptyCallbackFunction() { |
| 44 } | 67 } |
| 45 | 68 |
| 46 // Callback for getting requests. | 69 // Callback for getting requests. |
| 47 void GetRequestsDone(RequestQueue::GetRequestsResult result, | 70 void GetRequestsDone(RequestQueue::GetRequestsResult result, |
| 48 const std::vector<SavePageRequest>& requests); | 71 const std::vector<SavePageRequest>& requests); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 69 task_runner_handle_(task_runner_) {} | 92 task_runner_handle_(task_runner_) {} |
| 70 | 93 |
| 71 RequestCoordinatorTest::~RequestCoordinatorTest() {} | 94 RequestCoordinatorTest::~RequestCoordinatorTest() {} |
| 72 | 95 |
| 73 void RequestCoordinatorTest::SetUp() { | 96 void RequestCoordinatorTest::SetUp() { |
| 74 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); | 97 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); |
| 75 std::unique_ptr<OfflinerFactory> factory; | 98 std::unique_ptr<OfflinerFactory> factory; |
| 76 std::unique_ptr<RequestQueueInMemoryStore> | 99 std::unique_ptr<RequestQueueInMemoryStore> |
| 77 store(new RequestQueueInMemoryStore()); | 100 store(new RequestQueueInMemoryStore()); |
| 78 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store))); | 101 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store))); |
| 102 std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub()); |
| 79 coordinator_.reset(new RequestCoordinator( | 103 coordinator_.reset(new RequestCoordinator( |
| 80 std::move(policy), std::move(factory), std::move(queue))); | 104 std::move(policy), std::move(factory), std::move(queue), |
| 105 std::move(scheduler_stub))); |
| 81 } | 106 } |
| 82 | 107 |
| 83 | |
| 84 void RequestCoordinatorTest::PumpLoop() { | 108 void RequestCoordinatorTest::PumpLoop() { |
| 85 task_runner_->RunUntilIdle(); | 109 task_runner_->RunUntilIdle(); |
| 86 } | 110 } |
| 87 | 111 |
| 88 void RequestCoordinatorTest::GetRequestsDone( | 112 void RequestCoordinatorTest::GetRequestsDone( |
| 89 RequestQueue::GetRequestsResult result, | 113 RequestQueue::GetRequestsResult result, |
| 90 const std::vector<SavePageRequest>& requests) { | 114 const std::vector<SavePageRequest>& requests) { |
| 91 last_get_requests_result_ = result; | 115 last_get_requests_result_ = result; |
| 92 last_requests_ = requests; | 116 last_requests_ = requests; |
| 93 } | 117 } |
| 94 | 118 |
| 95 TEST_F(RequestCoordinatorTest, StartProcessingWithNoRequests) { | 119 TEST_F(RequestCoordinatorTest, StartProcessingWithNoRequests) { |
| 96 RequestCoordinator::ProcessingDoneCallback callback = | 120 RequestCoordinator::ProcessingDoneCallback callback = |
| 97 base::Bind( | 121 base::Bind( |
| 98 &RequestCoordinatorTest::EmptyCallbackFunction, | 122 &RequestCoordinatorTest::EmptyCallbackFunction, |
| 99 base::Unretained(this)); | 123 base::Unretained(this)); |
| 100 EXPECT_FALSE(getCoordinator()->StartProcessing(callback)); | 124 EXPECT_FALSE(coordinator()->StartProcessing(callback)); |
| 101 } | 125 } |
| 102 | 126 |
| 103 TEST_F(RequestCoordinatorTest, SavePageLater) { | 127 TEST_F(RequestCoordinatorTest, SavePageLater) { |
| 104 EXPECT_TRUE(getCoordinator()->SavePageLater(kUrl, kClientId)); | 128 EXPECT_TRUE(coordinator()->SavePageLater(kUrl, kClientId)); |
| 105 | 129 |
| 106 // Expect that a request got placed on the queue. | 130 // Expect that a request got placed on the queue. |
| 107 getCoordinator()->GetQueue()->GetRequests( | 131 coordinator()->GetQueue()->GetRequests( |
| 108 base::Bind(&RequestCoordinatorTest::GetRequestsDone, | 132 base::Bind(&RequestCoordinatorTest::GetRequestsDone, |
| 109 base::Unretained(this))); | 133 base::Unretained(this))); |
| 110 | 134 |
| 111 // Wait for callback to finish. | 135 // Wait for callback to finish. |
| 112 PumpLoop(); | 136 PumpLoop(); |
| 113 | 137 |
| 114 // Check the results are as expected. | 138 // Check the results are as expected. |
| 115 EXPECT_EQ(1UL, last_requests().size()); | 139 EXPECT_EQ(1UL, last_requests().size()); |
| 116 EXPECT_EQ(kUrl, last_requests()[0].url()); | 140 EXPECT_EQ(kUrl, last_requests()[0].url()); |
| 117 EXPECT_EQ(kClientId, last_requests()[0].client_id()); | 141 EXPECT_EQ(kClientId, last_requests()[0].client_id()); |
| 118 | 142 |
| 119 // TODO(petewil): Expect that the scheduler got notified. | 143 // Expect that the scheduler got notified. |
| 144 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( |
| 145 coordinator()->GetSchedulerForTesting()); |
| 146 EXPECT_TRUE(scheduler_stub->schedule_called()); |
| 120 } | 147 } |
| 121 | 148 |
| 122 } // namespace offline_pages | 149 } // namespace offline_pages |
| OLD | NEW |