| 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 <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 SetEffectiveConnectionTypeForTest( | 196 SetEffectiveConnectionTypeForTest( |
| 197 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G); | 197 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G); |
| 198 } else { | 198 } else { |
| 199 SetNetworkConditionsForTest( | 199 SetNetworkConditionsForTest( |
| 200 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE); | 200 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE); |
| 201 SetEffectiveConnectionTypeForTest( | 201 SetEffectiveConnectionTypeForTest( |
| 202 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_OFFLINE); | 202 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_OFFLINE); |
| 203 } | 203 } |
| 204 } | 204 } |
| 205 | 205 |
| 206 void CallConnectionTypeObserver() { |
| 207 if (coordinator()->connection_notifier_) { |
| 208 coordinator()->connection_notifier_->OnConnectionTypeChanged( |
| 209 GetConnectionType()); |
| 210 } |
| 211 } |
| 212 |
| 206 void SetIsLowEndDeviceForTest(bool is_low_end_device) { | 213 void SetIsLowEndDeviceForTest(bool is_low_end_device) { |
| 207 coordinator()->is_low_end_device_ = is_low_end_device; | 214 coordinator()->is_low_end_device_ = is_low_end_device; |
| 208 } | 215 } |
| 209 | 216 |
| 210 void SetProcessingStateForTest( | 217 void SetProcessingStateForTest( |
| 211 RequestCoordinator::ProcessingWindowState processing_state) { | 218 RequestCoordinator::ProcessingWindowState processing_state) { |
| 212 coordinator()->processing_state_ = processing_state; | 219 coordinator()->processing_state_ = processing_state; |
| 213 } | 220 } |
| 214 | 221 |
| 215 void SetOperationStartTimeForTest(base::Time start_time) { | 222 void SetOperationStartTimeForTest(base::Time start_time) { |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 // RequestPicker should *not* have tried to start an additional job, | 570 // RequestPicker should *not* have tried to start an additional job, |
| 564 // because the request queue is empty now. | 571 // because the request queue is empty now. |
| 565 EXPECT_EQ(0UL, last_requests().size()); | 572 EXPECT_EQ(0UL, last_requests().size()); |
| 566 // Check that the observer got the notification that we succeeded, and that | 573 // Check that the observer got the notification that we succeeded, and that |
| 567 // the request got removed from the queue. | 574 // the request got removed from the queue. |
| 568 EXPECT_TRUE(observer().completed_called()); | 575 EXPECT_TRUE(observer().completed_called()); |
| 569 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::SUCCESS, | 576 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::SUCCESS, |
| 570 observer().last_status()); | 577 observer().last_status()); |
| 571 } | 578 } |
| 572 | 579 |
| 580 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceededButLostNetwork) { |
| 581 // Add a request to the queue and set offliner done callback for it. |
| 582 offline_pages::SavePageRequest request(kRequestId1, kUrl1, kClientId1, |
| 583 base::Time::Now(), kUserRequested); |
| 584 SetupForOfflinerDoneCallbackTest(&request); |
| 585 EnableOfflinerCallback(false); |
| 586 |
| 587 // Add a 2nd request to the queue. |
| 588 AddRequest2(); |
| 589 |
| 590 // Disconnect network. |
| 591 SetNetworkConnected(false); |
| 592 |
| 593 // Call the OfflinerDoneCallback to simulate the page being completed, wait |
| 594 // for callbacks. |
| 595 SendOfflinerDoneCallback(request, Offliner::RequestStatus::SAVED); |
| 596 PumpLoop(); |
| 597 EXPECT_TRUE(immediate_schedule_callback_called()); |
| 598 |
| 599 // Verify not busy with 2nd request (since no connection). |
| 600 EXPECT_FALSE(is_busy()); |
| 601 |
| 602 // Now connect network and verify processing starts. |
| 603 SetNetworkConnected(true); |
| 604 CallConnectionTypeObserver(); |
| 605 PumpLoop(); |
| 606 EXPECT_TRUE(is_busy()); |
| 607 } |
| 608 |
| 573 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { | 609 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { |
| 574 // Add a request to the queue, wait for callbacks to finish. | 610 // Add a request to the queue, wait for callbacks to finish. |
| 575 offline_pages::SavePageRequest request( | 611 offline_pages::SavePageRequest request( |
| 576 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); | 612 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); |
| 577 request.set_completed_attempt_count(kMaxCompletedTries - 1); | 613 request.set_completed_attempt_count(kMaxCompletedTries - 1); |
| 578 SetupForOfflinerDoneCallbackTest(&request); | 614 SetupForOfflinerDoneCallbackTest(&request); |
| 579 // Stop processing before completing the second request on the queue. | 615 // Stop processing before completing the second request on the queue. |
| 580 EnableOfflinerCallback(false); | 616 EnableOfflinerCallback(false); |
| 581 | 617 |
| 582 // Add second request to the queue to check handling when first fails. | 618 // Add second request to the queue to check handling when first fails. |
| (...skipping 653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1236 kUrl2, kClientId2, kUserRequested, | 1272 kUrl2, kClientId2, kUserRequested, |
| 1237 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER), | 1273 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER), |
| 1238 0); | 1274 0); |
| 1239 PumpLoop(); | 1275 PumpLoop(); |
| 1240 | 1276 |
| 1241 // Verify immediate processing did start this time. | 1277 // Verify immediate processing did start this time. |
| 1242 EXPECT_TRUE(is_busy()); | 1278 EXPECT_TRUE(is_busy()); |
| 1243 } | 1279 } |
| 1244 | 1280 |
| 1245 TEST_F(RequestCoordinatorTest, SavePageDoesntStartProcessingWhenDisconnected) { | 1281 TEST_F(RequestCoordinatorTest, SavePageDoesntStartProcessingWhenDisconnected) { |
| 1282 // If low end device, pretend it is not so that immediate start allowed. |
| 1283 SetIsLowEndDeviceForTest(false); |
| 1284 |
| 1246 SetNetworkConnected(false); | 1285 SetNetworkConnected(false); |
| 1286 EnableOfflinerCallback(false); |
| 1247 EXPECT_NE( | 1287 EXPECT_NE( |
| 1248 coordinator()->SavePageLater( | 1288 coordinator()->SavePageLater( |
| 1249 kUrl1, kClientId1, kUserRequested, | 1289 kUrl1, kClientId1, kUserRequested, |
| 1250 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER), 0); | 1290 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER), 0); |
| 1251 PumpLoop(); | 1291 PumpLoop(); |
| 1252 EXPECT_FALSE(is_busy()); | 1292 EXPECT_FALSE(is_busy()); |
| 1293 |
| 1294 // Now connect network and verify processing starts. |
| 1295 SetNetworkConnected(true); |
| 1296 CallConnectionTypeObserver(); |
| 1297 PumpLoop(); |
| 1298 EXPECT_TRUE(is_busy()); |
| 1253 } | 1299 } |
| 1254 | 1300 |
| 1255 TEST_F(RequestCoordinatorTest, | 1301 TEST_F(RequestCoordinatorTest, |
| 1256 SavePageDoesStartProcessingWhenPoorlyConnected) { | 1302 SavePageDoesStartProcessingWhenPoorlyConnected) { |
| 1257 // Set specific network type for 2G with poor effective connection. | 1303 // Set specific network type for 2G with poor effective connection. |
| 1258 SetNetworkConditionsForTest( | 1304 SetNetworkConditionsForTest( |
| 1259 net::NetworkChangeNotifier::ConnectionType::CONNECTION_2G); | 1305 net::NetworkChangeNotifier::ConnectionType::CONNECTION_2G); |
| 1260 SetEffectiveConnectionTypeForTest( | 1306 SetEffectiveConnectionTypeForTest( |
| 1261 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); | 1307 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 1262 | 1308 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1312 // Now whether processing triggered immediately depends on whether test | 1358 // Now whether processing triggered immediately depends on whether test |
| 1313 // is run on svelte device or not. | 1359 // is run on svelte device or not. |
| 1314 if (base::SysInfo::IsLowEndDevice()) { | 1360 if (base::SysInfo::IsLowEndDevice()) { |
| 1315 EXPECT_FALSE(is_busy()); | 1361 EXPECT_FALSE(is_busy()); |
| 1316 } else { | 1362 } else { |
| 1317 EXPECT_TRUE(is_busy()); | 1363 EXPECT_TRUE(is_busy()); |
| 1318 } | 1364 } |
| 1319 } | 1365 } |
| 1320 | 1366 |
| 1321 } // namespace offline_pages | 1367 } // namespace offline_pages |
| OLD | NEW |