Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: components/offline_pages/background/request_coordinator_unittest.cc

Issue 2347783003: [OfflinePages, NetworkQualityEstimator] Use NetworkQualityEstimator to decide on triggering Backgro… (Closed)
Patch Set: Addresses some feedback Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 // TODO(dougarnett): Replace with real NQE instance when/if support interface.
RyanSturm 2016/09/19 17:15:40 For the purposes of this test, you'd probably be b
dougarnett 2016/09/19 19:34:32 Done.
131 class NetworkQualityEstimatorStub
tbansal1 2016/09/16 22:05:56 nit: may be s/NetworkQualityEstimatorStub/TestNetw
dougarnett 2016/09/19 19:34:33 Acknowledged. I prefer that too but matching namin
132 : public net::NetworkQualityEstimator::NetworkQualityProvider {
133 public:
134 NetworkQualityEstimatorStub()
135 : connection_type_(
136 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G) {}
137
138 net::EffectiveConnectionType GetEffectiveConnectionType() override {
139 return connection_type_;
140 }
141
142 void SetEffectiveConnectionTypeForTesting(net::EffectiveConnectionType type) {
143 connection_type_ = type;
144 }
145
146 private:
147 net::EffectiveConnectionType connection_type_;
148 };
149
130 class ObserverStub : public RequestCoordinator::Observer { 150 class ObserverStub : public RequestCoordinator::Observer {
131 public: 151 public:
132 ObserverStub() 152 ObserverStub()
133 : added_called_(false), 153 : added_called_(false),
134 completed_called_(false), 154 completed_called_(false),
135 changed_called_(false), 155 changed_called_(false),
136 last_status_(RequestCoordinator::BackgroundSavePageResult::SUCCESS), 156 last_status_(RequestCoordinator::BackgroundSavePageResult::SUCCESS),
137 state_(SavePageRequest::RequestState::PRERENDERING) {} 157 state_(SavePageRequest::RequestState::PRERENDERING) {}
138 158
139 void Clear() { 159 void Clear() {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 259
240 void EnableOfflinerCallback(bool enable) { 260 void EnableOfflinerCallback(bool enable) {
241 offliner_->enable_callback(enable); 261 offliner_->enable_callback(enable);
242 } 262 }
243 263
244 void SetNetworkConditionsForTest( 264 void SetNetworkConditionsForTest(
245 net::NetworkChangeNotifier::ConnectionType connection) { 265 net::NetworkChangeNotifier::ConnectionType connection) {
246 coordinator()->SetNetworkConditionsForTest(connection); 266 coordinator()->SetNetworkConditionsForTest(connection);
247 } 267 }
248 268
269 void SetEffectiveConnectionTypeForTesting(net::EffectiveConnectionType type) {
270 nqe_->SetEffectiveConnectionTypeForTesting(type);
271 }
272
249 void ScheduleForTest() { coordinator_->ScheduleAsNeeded(); } 273 void ScheduleForTest() { coordinator_->ScheduleAsNeeded(); }
250 274
251 void CallRequestNotPicked(bool non_user_requested_tasks_remaining) { 275 void CallRequestNotPicked(bool non_user_requested_tasks_remaining) {
252 coordinator_->RequestNotPicked(non_user_requested_tasks_remaining); 276 coordinator_->RequestNotPicked(non_user_requested_tasks_remaining);
253 } 277 }
254 278
255 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { 279 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) {
256 coordinator_->SetOfflinerTimeoutForTest(timeout); 280 coordinator_->SetOfflinerTimeoutForTest(timeout);
257 } 281 }
258 282
(...skipping 16 matching lines...) Expand all
275 bool OfflinerWasCanceled() const { return offliner_->cancel_called(); } 299 bool OfflinerWasCanceled() const { return offliner_->cancel_called(); }
276 300
277 ObserverStub observer() { return observer_; } 301 ObserverStub observer() { return observer_; }
278 302
279 private: 303 private:
280 RequestQueue::GetRequestsResult last_get_requests_result_; 304 RequestQueue::GetRequestsResult last_get_requests_result_;
281 RequestQueue::UpdateMultipleRequestResults last_remove_results_; 305 RequestQueue::UpdateMultipleRequestResults last_remove_results_;
282 std::vector<std::unique_ptr<SavePageRequest>> last_requests_; 306 std::vector<std::unique_ptr<SavePageRequest>> last_requests_;
283 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; 307 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
284 base::ThreadTaskRunnerHandle task_runner_handle_; 308 base::ThreadTaskRunnerHandle task_runner_handle_;
309 NetworkQualityEstimatorStub* nqe_;
285 std::unique_ptr<RequestCoordinator> coordinator_; 310 std::unique_ptr<RequestCoordinator> coordinator_;
286 OfflinerStub* offliner_; 311 OfflinerStub* offliner_;
287 base::WaitableEvent waiter_; 312 base::WaitableEvent waiter_;
288 ObserverStub observer_; 313 ObserverStub observer_;
289 }; 314 };
290 315
291 RequestCoordinatorTest::RequestCoordinatorTest() 316 RequestCoordinatorTest::RequestCoordinatorTest()
292 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE), 317 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE),
293 task_runner_(new base::TestMockTimeTaskRunner), 318 task_runner_(new base::TestMockTimeTaskRunner),
294 task_runner_handle_(task_runner_), 319 task_runner_handle_(task_runner_),
320 nqe_(new NetworkQualityEstimatorStub()),
tbansal1 2016/09/16 22:05:56 Is this not leaking memory? May be you can store i
dougarnett 2016/09/19 19:34:32 Done.
295 offliner_(nullptr), 321 offliner_(nullptr),
296 waiter_(base::WaitableEvent::ResetPolicy::MANUAL, 322 waiter_(base::WaitableEvent::ResetPolicy::MANUAL,
297 base::WaitableEvent::InitialState::NOT_SIGNALED) {} 323 base::WaitableEvent::InitialState::NOT_SIGNALED) {}
298 324
299 RequestCoordinatorTest::~RequestCoordinatorTest() {} 325 RequestCoordinatorTest::~RequestCoordinatorTest() {}
300 326
301 void RequestCoordinatorTest::SetUp() { 327 void RequestCoordinatorTest::SetUp() {
302 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); 328 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy());
303 std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub()); 329 std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub());
304 // Save the offliner for use by the tests. 330 // Save the offliner for use by the tests.
305 offliner_ = 331 offliner_ =
306 reinterpret_cast<OfflinerStub*>(factory->GetOffliner(policy.get())); 332 reinterpret_cast<OfflinerStub*>(factory->GetOffliner(policy.get()));
307 std::unique_ptr<RequestQueueInMemoryStore> 333 std::unique_ptr<RequestQueueInMemoryStore>
308 store(new RequestQueueInMemoryStore()); 334 store(new RequestQueueInMemoryStore());
309 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store))); 335 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store)));
310 std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub()); 336 std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub());
311 coordinator_.reset(new RequestCoordinator( 337 coordinator_.reset(new RequestCoordinator(
312 std::move(policy), std::move(factory), std::move(queue), 338 std::move(policy), std::move(factory), std::move(queue),
313 std::move(scheduler_stub))); 339 std::move(scheduler_stub), nqe_));
314 coordinator_->AddObserver(&observer_); 340 coordinator_->AddObserver(&observer_);
315 } 341 }
316 342
317 void RequestCoordinatorTest::PumpLoop() { 343 void RequestCoordinatorTest::PumpLoop() {
318 task_runner_->RunUntilIdle(); 344 task_runner_->RunUntilIdle();
319 } 345 }
320 346
321 void RequestCoordinatorTest::GetRequestsDone( 347 void RequestCoordinatorTest::GetRequestsDone(
322 RequestQueue::GetRequestsResult result, 348 RequestQueue::GetRequestsResult result,
323 std::vector<std::unique_ptr<SavePageRequest>> requests) { 349 std::vector<std::unique_ptr<SavePageRequest>> requests) {
(...skipping 27 matching lines...) Expand all
351 DeviceConditions device_conditions(false, 75, 377 DeviceConditions device_conditions(false, 75,
352 net::NetworkChangeNotifier::CONNECTION_3G); 378 net::NetworkChangeNotifier::CONNECTION_3G);
353 base::Callback<void(bool)> callback = 379 base::Callback<void(bool)> callback =
354 base::Bind( 380 base::Bind(
355 &RequestCoordinatorTest::EmptyCallbackFunction, 381 &RequestCoordinatorTest::EmptyCallbackFunction,
356 base::Unretained(this)); 382 base::Unretained(this));
357 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); 383 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback));
358 } 384 }
359 385
360 TEST_F(RequestCoordinatorTest, StartProcessingWithRequestInProgress) { 386 TEST_F(RequestCoordinatorTest, StartProcessingWithRequestInProgress) {
387 SetEffectiveConnectionTypeForTesting(
Pete Williamson 2016/09/16 22:26:01 Maybe set up a default type in the ::SetUp functio
dougarnett 2016/09/19 19:34:33 Done.
388 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_OFFLINE);
361 SetNetworkConditionsForTest( 389 SetNetworkConditionsForTest(
362 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE); 390 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE);
363 // Put the request on the queue. 391 // Put the request on the queue.
364 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested)); 392 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested));
365 393
366 // Set up for the call to StartProcessing by building arguments. 394 // Set up for the call to StartProcessing by building arguments.
367 DeviceConditions device_conditions( 395 DeviceConditions device_conditions(
368 false, 75, net::NetworkChangeNotifier::CONNECTION_3G); 396 false, 75, net::NetworkChangeNotifier::CONNECTION_3G);
369 base::Callback<void(bool)> callback = 397 base::Callback<void(bool)> callback =
370 base::Bind(&RequestCoordinatorTest::EmptyCallbackFunction, 398 base::Bind(&RequestCoordinatorTest::EmptyCallbackFunction,
(...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after
955 983
956 EXPECT_TRUE(observer().completed_called()); 984 EXPECT_TRUE(observer().completed_called());
957 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::REMOVED, 985 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::REMOVED,
958 observer().last_status()); 986 observer().last_status());
959 EXPECT_EQ(1UL, last_remove_results().size()); 987 EXPECT_EQ(1UL, last_remove_results().size());
960 EXPECT_EQ(kRequestId1, std::get<0>(last_remove_results().at(0))); 988 EXPECT_EQ(kRequestId1, std::get<0>(last_remove_results().at(0)));
961 } 989 }
962 990
963 TEST_F(RequestCoordinatorTest, 991 TEST_F(RequestCoordinatorTest,
964 SavePageStartsProcessingWhenConnectedAndNotLowEndDevice) { 992 SavePageStartsProcessingWhenConnectedAndNotLowEndDevice) {
993 SetEffectiveConnectionTypeForTesting(
994 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G);
965 SetNetworkConditionsForTest( 995 SetNetworkConditionsForTest(
966 net::NetworkChangeNotifier::ConnectionType::CONNECTION_3G); 996 net::NetworkChangeNotifier::ConnectionType::CONNECTION_3G);
967 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested)); 997 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested));
968 PumpLoop(); 998 PumpLoop();
969 999
970 // Now whether processing triggered immediately depends on whether test 1000 // Now whether processing triggered immediately depends on whether test
971 // is run on svelte device or not. 1001 // is run on svelte device or not.
972 if (base::SysInfo::IsLowEndDevice()) { 1002 if (base::SysInfo::IsLowEndDevice()) {
973 EXPECT_FALSE(is_busy()); 1003 EXPECT_FALSE(is_busy());
974 } else { 1004 } else {
975 EXPECT_TRUE(is_busy()); 1005 EXPECT_TRUE(is_busy());
976 } 1006 }
977 } 1007 }
978 1008
979 TEST_F(RequestCoordinatorTest, SavePageDoesntStartProcessingWhenDisconnected) { 1009 TEST_F(RequestCoordinatorTest, SavePageDoesntStartProcessingWhenDisconnected) {
1010 SetEffectiveConnectionTypeForTesting(
1011 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_OFFLINE);
Pete Williamson 2016/09/16 22:26:01 Maybe EFFECTIVE_CONNECTION_TYPE_SLOW_2G (unless we
dougarnett 2016/09/19 19:34:33 Added ...WhenPoorlyConnected test variant.
980 SetNetworkConditionsForTest( 1012 SetNetworkConditionsForTest(
981 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE); 1013 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE);
982 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested)); 1014 EXPECT_TRUE(coordinator()->SavePageLater(kUrl1, kClientId1, kUserRequested));
983 PumpLoop(); 1015 PumpLoop();
984 EXPECT_FALSE(is_busy()); 1016 EXPECT_FALSE(is_busy());
985 } 1017 }
986 1018
987 TEST_F(RequestCoordinatorTest, 1019 TEST_F(RequestCoordinatorTest,
988 ResumeStartsProcessingWhenConnectedAndNotLowEndDevice) { 1020 ResumeStartsProcessingWhenConnectedAndNotLowEndDevice) {
1021 SetEffectiveConnectionTypeForTesting(
1022 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_OFFLINE);
989 SetNetworkConditionsForTest( 1023 SetNetworkConditionsForTest(
990 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE); 1024 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE);
991 1025
992 // Add a request to the queue. 1026 // Add a request to the queue.
993 offline_pages::SavePageRequest request1(kRequestId1, kUrl1, kClientId1, 1027 offline_pages::SavePageRequest request1(kRequestId1, kUrl1, kClientId1,
994 base::Time::Now(), kUserRequested); 1028 base::Time::Now(), kUserRequested);
995 coordinator()->queue()->AddRequest( 1029 coordinator()->queue()->AddRequest(
996 request1, base::Bind(&RequestCoordinatorTest::AddRequestDone, 1030 request1, base::Bind(&RequestCoordinatorTest::AddRequestDone,
997 base::Unretained(this))); 1031 base::Unretained(this)));
998 PumpLoop(); 1032 PumpLoop();
999 EXPECT_FALSE(is_busy()); 1033 EXPECT_FALSE(is_busy());
1000 1034
1001 // Pause the request. 1035 // Pause the request.
1002 std::vector<int64_t> request_ids; 1036 std::vector<int64_t> request_ids;
1003 request_ids.push_back(kRequestId1); 1037 request_ids.push_back(kRequestId1);
1004 coordinator()->PauseRequests(request_ids); 1038 coordinator()->PauseRequests(request_ids);
1005 PumpLoop(); 1039 PumpLoop();
1006 1040
1007 // Resume the request while disconnected. 1041 // Resume the request while disconnected.
1008 coordinator()->ResumeRequests(request_ids); 1042 coordinator()->ResumeRequests(request_ids);
1009 PumpLoop(); 1043 PumpLoop();
1010 EXPECT_FALSE(is_busy()); 1044 EXPECT_FALSE(is_busy());
1011 1045
1012 // Pause the request again. 1046 // Pause the request again.
1013 coordinator()->PauseRequests(request_ids); 1047 coordinator()->PauseRequests(request_ids);
1014 PumpLoop(); 1048 PumpLoop();
1015 1049
1016 // Now simulate being connected. 1050 // Now simulate being connected.
1051 SetEffectiveConnectionTypeForTesting(
1052 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G);
1017 SetNetworkConditionsForTest( 1053 SetNetworkConditionsForTest(
1018 net::NetworkChangeNotifier::ConnectionType::CONNECTION_3G); 1054 net::NetworkChangeNotifier::ConnectionType::CONNECTION_3G);
1019 1055
1020 // Resume the request while connected. 1056 // Resume the request while connected.
1021 coordinator()->ResumeRequests(request_ids); 1057 coordinator()->ResumeRequests(request_ids);
1022 EXPECT_FALSE(is_busy()); 1058 EXPECT_FALSE(is_busy());
1023 PumpLoop(); 1059 PumpLoop();
1024 1060
1025 // Now whether processing triggered immediately depends on whether test 1061 // Now whether processing triggered immediately depends on whether test
1026 // is run on svelte device or not. 1062 // is run on svelte device or not.
1027 if (base::SysInfo::IsLowEndDevice()) { 1063 if (base::SysInfo::IsLowEndDevice()) {
1028 EXPECT_FALSE(is_busy()); 1064 EXPECT_FALSE(is_busy());
1029 } else { 1065 } else {
1030 EXPECT_TRUE(is_busy()); 1066 EXPECT_TRUE(is_busy());
1031 } 1067 }
1032 } 1068 }
1033 1069
1034 } // namespace offline_pages 1070 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698