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 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 if (offliner_.get() == nullptr) { | 120 if (offliner_.get() == nullptr) { |
121 offliner_.reset(new OfflinerStub()); | 121 offliner_.reset(new OfflinerStub()); |
122 } | 122 } |
123 return offliner_.get(); | 123 return offliner_.get(); |
124 } | 124 } |
125 | 125 |
126 private: | 126 private: |
127 std::unique_ptr<OfflinerStub> offliner_; | 127 std::unique_ptr<OfflinerStub> offliner_; |
128 }; | 128 }; |
129 | 129 |
| 130 class NetworkQualityEstimatorStub |
| 131 : public net::NetworkQualityEstimator::NetworkQualityProvider { |
| 132 public: |
| 133 NetworkQualityEstimatorStub() |
| 134 : connection_type_( |
| 135 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G) {} |
| 136 |
| 137 net::EffectiveConnectionType GetEffectiveConnectionType() const override { |
| 138 return connection_type_; |
| 139 } |
| 140 |
| 141 void SetEffectiveConnectionTypeForTest(net::EffectiveConnectionType type) { |
| 142 connection_type_ = type; |
| 143 } |
| 144 |
| 145 private: |
| 146 net::EffectiveConnectionType connection_type_; |
| 147 }; |
| 148 |
130 class ObserverStub : public RequestCoordinator::Observer { | 149 class ObserverStub : public RequestCoordinator::Observer { |
131 public: | 150 public: |
132 ObserverStub() | 151 ObserverStub() |
133 : added_called_(false), | 152 : added_called_(false), |
134 completed_called_(false), | 153 completed_called_(false), |
135 changed_called_(false), | 154 changed_called_(false), |
136 last_status_(RequestCoordinator::BackgroundSavePageResult::SUCCESS), | 155 last_status_(RequestCoordinator::BackgroundSavePageResult::SUCCESS), |
137 state_(SavePageRequest::RequestState::PRERENDERING) {} | 156 state_(SavePageRequest::RequestState::PRERENDERING) {} |
138 | 157 |
139 void Clear() { | 158 void Clear() { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 | 258 |
240 void EnableOfflinerCallback(bool enable) { | 259 void EnableOfflinerCallback(bool enable) { |
241 offliner_->enable_callback(enable); | 260 offliner_->enable_callback(enable); |
242 } | 261 } |
243 | 262 |
244 void SetNetworkConditionsForTest( | 263 void SetNetworkConditionsForTest( |
245 net::NetworkChangeNotifier::ConnectionType connection) { | 264 net::NetworkChangeNotifier::ConnectionType connection) { |
246 coordinator()->SetNetworkConditionsForTest(connection); | 265 coordinator()->SetNetworkConditionsForTest(connection); |
247 } | 266 } |
248 | 267 |
| 268 void SetEffectiveConnectionTypeForTest(net::EffectiveConnectionType type) { |
| 269 network_quality_estimator_->SetEffectiveConnectionTypeForTest(type); |
| 270 } |
| 271 |
249 void ScheduleForTest() { coordinator_->ScheduleAsNeeded(); } | 272 void ScheduleForTest() { coordinator_->ScheduleAsNeeded(); } |
250 | 273 |
251 void CallRequestNotPicked(bool non_user_requested_tasks_remaining) { | 274 void CallRequestNotPicked(bool non_user_requested_tasks_remaining) { |
252 coordinator_->RequestNotPicked(non_user_requested_tasks_remaining); | 275 coordinator_->RequestNotPicked(non_user_requested_tasks_remaining); |
253 } | 276 } |
254 | 277 |
255 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { | 278 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { |
256 coordinator_->SetOfflinerTimeoutForTest(timeout); | 279 coordinator_->SetOfflinerTimeoutForTest(timeout); |
257 } | 280 } |
258 | 281 |
(...skipping 16 matching lines...) Expand all Loading... |
275 bool OfflinerWasCanceled() const { return offliner_->cancel_called(); } | 298 bool OfflinerWasCanceled() const { return offliner_->cancel_called(); } |
276 | 299 |
277 ObserverStub observer() { return observer_; } | 300 ObserverStub observer() { return observer_; } |
278 | 301 |
279 private: | 302 private: |
280 RequestQueue::GetRequestsResult last_get_requests_result_; | 303 RequestQueue::GetRequestsResult last_get_requests_result_; |
281 RequestQueue::UpdateMultipleRequestResults last_remove_results_; | 304 RequestQueue::UpdateMultipleRequestResults last_remove_results_; |
282 std::vector<std::unique_ptr<SavePageRequest>> last_requests_; | 305 std::vector<std::unique_ptr<SavePageRequest>> last_requests_; |
283 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; | 306 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
284 base::ThreadTaskRunnerHandle task_runner_handle_; | 307 base::ThreadTaskRunnerHandle task_runner_handle_; |
| 308 std::unique_ptr<NetworkQualityEstimatorStub> network_quality_estimator_; |
285 std::unique_ptr<RequestCoordinator> coordinator_; | 309 std::unique_ptr<RequestCoordinator> coordinator_; |
286 OfflinerStub* offliner_; | 310 OfflinerStub* offliner_; |
287 base::WaitableEvent waiter_; | 311 base::WaitableEvent waiter_; |
288 ObserverStub observer_; | 312 ObserverStub observer_; |
289 }; | 313 }; |
290 | 314 |
291 RequestCoordinatorTest::RequestCoordinatorTest() | 315 RequestCoordinatorTest::RequestCoordinatorTest() |
292 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE), | 316 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE), |
293 task_runner_(new base::TestMockTimeTaskRunner), | 317 task_runner_(new base::TestMockTimeTaskRunner), |
294 task_runner_handle_(task_runner_), | 318 task_runner_handle_(task_runner_), |
295 offliner_(nullptr), | 319 offliner_(nullptr), |
296 waiter_(base::WaitableEvent::ResetPolicy::MANUAL, | 320 waiter_(base::WaitableEvent::ResetPolicy::MANUAL, |
297 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | 321 base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
298 | 322 |
299 RequestCoordinatorTest::~RequestCoordinatorTest() {} | 323 RequestCoordinatorTest::~RequestCoordinatorTest() {} |
300 | 324 |
301 void RequestCoordinatorTest::SetUp() { | 325 void RequestCoordinatorTest::SetUp() { |
302 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); | 326 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); |
303 std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub()); | 327 std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub()); |
304 // Save the offliner for use by the tests. | 328 // Save the offliner for use by the tests. |
305 offliner_ = | 329 offliner_ = |
306 reinterpret_cast<OfflinerStub*>(factory->GetOffliner(policy.get())); | 330 reinterpret_cast<OfflinerStub*>(factory->GetOffliner(policy.get())); |
307 std::unique_ptr<RequestQueueInMemoryStore> | 331 std::unique_ptr<RequestQueueInMemoryStore> |
308 store(new RequestQueueInMemoryStore()); | 332 store(new RequestQueueInMemoryStore()); |
309 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store))); | 333 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store))); |
310 std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub()); | 334 std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub()); |
| 335 network_quality_estimator_.reset(new NetworkQualityEstimatorStub()); |
311 coordinator_.reset(new RequestCoordinator( | 336 coordinator_.reset(new RequestCoordinator( |
312 std::move(policy), std::move(factory), std::move(queue), | 337 std::move(policy), std::move(factory), std::move(queue), |
313 std::move(scheduler_stub))); | 338 std::move(scheduler_stub), network_quality_estimator_.get())); |
314 coordinator_->AddObserver(&observer_); | 339 coordinator_->AddObserver(&observer_); |
| 340 SetEffectiveConnectionTypeForTest( |
| 341 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_OFFLINE); |
| 342 SetNetworkConditionsForTest( |
| 343 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE); |
315 } | 344 } |
316 | 345 |
317 void RequestCoordinatorTest::PumpLoop() { | 346 void RequestCoordinatorTest::PumpLoop() { |
318 task_runner_->RunUntilIdle(); | 347 task_runner_->RunUntilIdle(); |
319 } | 348 } |
320 | 349 |
321 void RequestCoordinatorTest::GetRequestsDone( | 350 void RequestCoordinatorTest::GetRequestsDone( |
322 RequestQueue::GetRequestsResult result, | 351 RequestQueue::GetRequestsResult result, |
323 std::vector<std::unique_ptr<SavePageRequest>> requests) { | 352 std::vector<std::unique_ptr<SavePageRequest>> requests) { |
324 last_get_requests_result_ = result; | 353 last_get_requests_result_ = result; |
(...skipping 26 matching lines...) Expand all Loading... |
351 DeviceConditions device_conditions(false, 75, | 380 DeviceConditions device_conditions(false, 75, |
352 net::NetworkChangeNotifier::CONNECTION_3G); | 381 net::NetworkChangeNotifier::CONNECTION_3G); |
353 base::Callback<void(bool)> callback = | 382 base::Callback<void(bool)> callback = |
354 base::Bind( | 383 base::Bind( |
355 &RequestCoordinatorTest::EmptyCallbackFunction, | 384 &RequestCoordinatorTest::EmptyCallbackFunction, |
356 base::Unretained(this)); | 385 base::Unretained(this)); |
357 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); | 386 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); |
358 } | 387 } |
359 | 388 |
360 TEST_F(RequestCoordinatorTest, StartProcessingWithRequestInProgress) { | 389 TEST_F(RequestCoordinatorTest, StartProcessingWithRequestInProgress) { |
361 SetNetworkConditionsForTest( | |
362 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE); | |
363 // Put the request on the queue. | 390 // Put the request on the queue. |
364 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested)); | 391 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested)); |
365 | 392 |
366 // Set up for the call to StartProcessing by building arguments. | 393 // Set up for the call to StartProcessing by building arguments. |
367 DeviceConditions device_conditions( | 394 DeviceConditions device_conditions( |
368 false, 75, net::NetworkChangeNotifier::CONNECTION_3G); | 395 false, 75, net::NetworkChangeNotifier::CONNECTION_3G); |
369 base::Callback<void(bool)> callback = | 396 base::Callback<void(bool)> callback = |
370 base::Bind(&RequestCoordinatorTest::EmptyCallbackFunction, | 397 base::Bind(&RequestCoordinatorTest::EmptyCallbackFunction, |
371 base::Unretained(this)); | 398 base::Unretained(this)); |
372 | 399 |
(...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
955 | 982 |
956 EXPECT_TRUE(observer().completed_called()); | 983 EXPECT_TRUE(observer().completed_called()); |
957 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::REMOVED, | 984 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::REMOVED, |
958 observer().last_status()); | 985 observer().last_status()); |
959 EXPECT_EQ(1UL, last_remove_results().size()); | 986 EXPECT_EQ(1UL, last_remove_results().size()); |
960 EXPECT_EQ(kRequestId1, std::get<0>(last_remove_results().at(0))); | 987 EXPECT_EQ(kRequestId1, std::get<0>(last_remove_results().at(0))); |
961 } | 988 } |
962 | 989 |
963 TEST_F(RequestCoordinatorTest, | 990 TEST_F(RequestCoordinatorTest, |
964 SavePageStartsProcessingWhenConnectedAndNotLowEndDevice) { | 991 SavePageStartsProcessingWhenConnectedAndNotLowEndDevice) { |
965 SetNetworkConditionsForTest( | 992 SetEffectiveConnectionTypeForTest( |
966 net::NetworkChangeNotifier::ConnectionType::CONNECTION_3G); | 993 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G); |
967 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested)); | 994 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested)); |
968 PumpLoop(); | 995 PumpLoop(); |
969 | 996 |
970 // Now whether processing triggered immediately depends on whether test | 997 // Now whether processing triggered immediately depends on whether test |
971 // is run on svelte device or not. | 998 // is run on svelte device or not. |
972 if (base::SysInfo::IsLowEndDevice()) { | 999 if (base::SysInfo::IsLowEndDevice()) { |
973 EXPECT_FALSE(is_busy()); | 1000 EXPECT_FALSE(is_busy()); |
974 } else { | 1001 } else { |
975 EXPECT_TRUE(is_busy()); | 1002 EXPECT_TRUE(is_busy()); |
976 } | 1003 } |
977 } | 1004 } |
978 | 1005 |
979 TEST_F(RequestCoordinatorTest, SavePageDoesntStartProcessingWhenDisconnected) { | 1006 TEST_F(RequestCoordinatorTest, SavePageDoesntStartProcessingWhenDisconnected) { |
980 SetNetworkConditionsForTest( | |
981 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE); | |
982 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested)); | 1007 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested)); |
983 PumpLoop(); | 1008 PumpLoop(); |
984 EXPECT_FALSE(is_busy()); | 1009 EXPECT_FALSE(is_busy()); |
| 1010 } |
| 1011 |
| 1012 TEST_F(RequestCoordinatorTest, |
| 1013 SavePageDoesntStartProcessingWhenPoorlyConnected) { |
| 1014 SetEffectiveConnectionTypeForTest( |
| 1015 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 1016 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested)); |
| 1017 PumpLoop(); |
| 1018 EXPECT_FALSE(is_busy()); |
985 } | 1019 } |
986 | 1020 |
987 TEST_F(RequestCoordinatorTest, | 1021 TEST_F(RequestCoordinatorTest, |
988 ResumeStartsProcessingWhenConnectedAndNotLowEndDevice) { | 1022 ResumeStartsProcessingWhenConnectedAndNotLowEndDevice) { |
989 SetNetworkConditionsForTest( | |
990 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE); | |
991 | 1023 |
992 // Add a request to the queue. | 1024 // Add a request to the queue. |
993 offline_pages::SavePageRequest request1(kRequestId1, kUrl1, kClientId1, | 1025 offline_pages::SavePageRequest request1(kRequestId1, kUrl1, kClientId1, |
994 base::Time::Now(), kUserRequested); | 1026 base::Time::Now(), kUserRequested); |
995 coordinator()->queue()->AddRequest( | 1027 coordinator()->queue()->AddRequest( |
996 request1, base::Bind(&RequestCoordinatorTest::AddRequestDone, | 1028 request1, base::Bind(&RequestCoordinatorTest::AddRequestDone, |
997 base::Unretained(this))); | 1029 base::Unretained(this))); |
998 PumpLoop(); | 1030 PumpLoop(); |
999 EXPECT_FALSE(is_busy()); | 1031 EXPECT_FALSE(is_busy()); |
1000 | 1032 |
1001 // Pause the request. | 1033 // Pause the request. |
1002 std::vector<int64_t> request_ids; | 1034 std::vector<int64_t> request_ids; |
1003 request_ids.push_back(kRequestId1); | 1035 request_ids.push_back(kRequestId1); |
1004 coordinator()->PauseRequests(request_ids); | 1036 coordinator()->PauseRequests(request_ids); |
1005 PumpLoop(); | 1037 PumpLoop(); |
1006 | 1038 |
1007 // Resume the request while disconnected. | 1039 // Resume the request while disconnected. |
1008 coordinator()->ResumeRequests(request_ids); | 1040 coordinator()->ResumeRequests(request_ids); |
1009 PumpLoop(); | 1041 PumpLoop(); |
1010 EXPECT_FALSE(is_busy()); | 1042 EXPECT_FALSE(is_busy()); |
1011 | 1043 |
1012 // Pause the request again. | 1044 // Pause the request again. |
1013 coordinator()->PauseRequests(request_ids); | 1045 coordinator()->PauseRequests(request_ids); |
1014 PumpLoop(); | 1046 PumpLoop(); |
1015 | 1047 |
1016 // Now simulate being connected. | 1048 // Now simulate reasonable connection. |
1017 SetNetworkConditionsForTest( | 1049 SetEffectiveConnectionTypeForTest( |
1018 net::NetworkChangeNotifier::ConnectionType::CONNECTION_3G); | 1050 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G); |
1019 | 1051 |
1020 // Resume the request while connected. | 1052 // Resume the request while connected. |
1021 coordinator()->ResumeRequests(request_ids); | 1053 coordinator()->ResumeRequests(request_ids); |
1022 EXPECT_FALSE(is_busy()); | 1054 EXPECT_FALSE(is_busy()); |
1023 PumpLoop(); | 1055 PumpLoop(); |
1024 | 1056 |
1025 // Now whether processing triggered immediately depends on whether test | 1057 // Now whether processing triggered immediately depends on whether test |
1026 // is run on svelte device or not. | 1058 // is run on svelte device or not. |
1027 if (base::SysInfo::IsLowEndDevice()) { | 1059 if (base::SysInfo::IsLowEndDevice()) { |
1028 EXPECT_FALSE(is_busy()); | 1060 EXPECT_FALSE(is_busy()); |
1029 } else { | 1061 } else { |
1030 EXPECT_TRUE(is_busy()); | 1062 EXPECT_TRUE(is_busy()); |
1031 } | 1063 } |
1032 } | 1064 } |
1033 | 1065 |
1034 } // namespace offline_pages | 1066 } // namespace offline_pages |
OLD | NEW |