| 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 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/test/test_simple_task_runner.h" | 13 #include "base/test/test_simple_task_runner.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "components/offline_pages/background/device_conditions.h" |
| 15 #include "components/offline_pages/background/offliner.h" | 16 #include "components/offline_pages/background/offliner.h" |
| 16 #include "components/offline_pages/background/offliner_factory.h" | 17 #include "components/offline_pages/background/offliner_factory.h" |
| 17 #include "components/offline_pages/background/offliner_policy.h" | 18 #include "components/offline_pages/background/offliner_policy.h" |
| 18 #include "components/offline_pages/background/request_queue.h" | 19 #include "components/offline_pages/background/request_queue.h" |
| 19 #include "components/offline_pages/background/request_queue_in_memory_store.h" | 20 #include "components/offline_pages/background/request_queue_in_memory_store.h" |
| 20 #include "components/offline_pages/background/save_page_request.h" | 21 #include "components/offline_pages/background/save_page_request.h" |
| 21 #include "components/offline_pages/background/scheduler.h" | 22 #include "components/offline_pages/background/scheduler.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| 24 namespace offline_pages { | 25 namespace offline_pages { |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 const SavePageRequest& request) {} | 159 const SavePageRequest& request) {} |
| 159 | 160 |
| 160 void RequestCoordinatorTest::SendOfflinerDoneCallback( | 161 void RequestCoordinatorTest::SendOfflinerDoneCallback( |
| 161 const SavePageRequest& request, Offliner::RequestStatus status) { | 162 const SavePageRequest& request, Offliner::RequestStatus status) { |
| 162 // Using the fact that the test class is a friend, call to the callback | 163 // Using the fact that the test class is a friend, call to the callback |
| 163 coordinator_->OfflinerDoneCallback(request, status); | 164 coordinator_->OfflinerDoneCallback(request, status); |
| 164 } | 165 } |
| 165 | 166 |
| 166 | 167 |
| 167 TEST_F(RequestCoordinatorTest, StartProcessingWithNoRequests) { | 168 TEST_F(RequestCoordinatorTest, StartProcessingWithNoRequests) { |
| 169 DeviceConditions device_conditions(false, 75, |
| 170 net::NetworkChangeNotifier::CONNECTION_3G); |
| 168 base::Callback<void(bool)> callback = | 171 base::Callback<void(bool)> callback = |
| 169 base::Bind( | 172 base::Bind( |
| 170 &RequestCoordinatorTest::EmptyCallbackFunction, | 173 &RequestCoordinatorTest::EmptyCallbackFunction, |
| 171 base::Unretained(this)); | 174 base::Unretained(this)); |
| 172 EXPECT_TRUE(coordinator()->StartProcessing(callback)); | 175 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); |
| 173 } | 176 } |
| 174 | 177 |
| 175 TEST_F(RequestCoordinatorTest, SavePageLater) { | 178 TEST_F(RequestCoordinatorTest, SavePageLater) { |
| 176 EXPECT_TRUE(coordinator()->SavePageLater(kUrl, kClientId)); | 179 EXPECT_TRUE(coordinator()->SavePageLater(kUrl, kClientId)); |
| 177 | 180 |
| 178 // Expect that a request got placed on the queue. | 181 // Expect that a request got placed on the queue. |
| 179 coordinator()->queue()->GetRequests( | 182 coordinator()->queue()->GetRequests( |
| 180 base::Bind(&RequestCoordinatorTest::GetRequestsDone, | 183 base::Bind(&RequestCoordinatorTest::GetRequestsDone, |
| 181 base::Unretained(this))); | 184 base::Unretained(this))); |
| 182 | 185 |
| 183 // Wait for callbacks to finish, both request queue and offliner. | 186 // Wait for callbacks to finish, both request queue and offliner. |
| 184 PumpLoop(); | 187 PumpLoop(); |
| 185 | 188 |
| 186 // Check the request queue is as expected. | 189 // Check the request queue is as expected. |
| 187 EXPECT_EQ(1UL, last_requests().size()); | 190 EXPECT_EQ(1UL, last_requests().size()); |
| 188 EXPECT_EQ(kUrl, last_requests()[0].url()); | 191 EXPECT_EQ(kUrl, last_requests()[0].url()); |
| 189 EXPECT_EQ(kClientId, last_requests()[0].client_id()); | 192 EXPECT_EQ(kClientId, last_requests()[0].client_id()); |
| 190 | 193 |
| 191 // Expect that the scheduler got notified. | 194 // Expect that the scheduler got notified. |
| 192 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( | 195 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( |
| 193 coordinator()->scheduler()); | 196 coordinator()->scheduler()); |
| 194 EXPECT_TRUE(scheduler_stub->schedule_called()); | 197 EXPECT_TRUE(scheduler_stub->schedule_called()); |
| 195 } | 198 } |
| 196 | 199 |
| 197 TEST_F(RequestCoordinatorTest, OfflinerDone) { | 200 TEST_F(RequestCoordinatorTest, OfflinerDone) { |
| 198 | |
| 199 // Add a request to the queue, wait for callbacks to finish. | 201 // Add a request to the queue, wait for callbacks to finish. |
| 200 offline_pages::SavePageRequest request( | 202 offline_pages::SavePageRequest request( |
| 201 kRequestId, kUrl, kClientId, base::Time::Now()); | 203 kRequestId, kUrl, kClientId, base::Time::Now()); |
| 202 coordinator()->queue()->AddRequest( | 204 coordinator()->queue()->AddRequest( |
| 203 request, | 205 request, |
| 204 base::Bind(&RequestCoordinatorTest::AddRequestDone, | 206 base::Bind(&RequestCoordinatorTest::AddRequestDone, |
| 205 base::Unretained(this))); | 207 base::Unretained(this))); |
| 206 PumpLoop(); | 208 PumpLoop(); |
| 207 | 209 |
| 208 // We need to give a callback to the request. | 210 // We need to give a callback to the request. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 223 base::Unretained(this))); | 225 base::Unretained(this))); |
| 224 PumpLoop(); | 226 PumpLoop(); |
| 225 | 227 |
| 226 // We should not find any requests in the queue anymore. | 228 // We should not find any requests in the queue anymore. |
| 227 // RequestPicker should *not* have tried to start an additional job, | 229 // RequestPicker should *not* have tried to start an additional job, |
| 228 // because the request queue is empty now. | 230 // because the request queue is empty now. |
| 229 EXPECT_EQ(0UL, last_requests().size()); | 231 EXPECT_EQ(0UL, last_requests().size()); |
| 230 } | 232 } |
| 231 | 233 |
| 232 } // namespace offline_pages | 234 } // namespace offline_pages |
| OLD | NEW |