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

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

Issue 2489443002: Move all components/offline_pages/ files into component/offline_pages/core (Closed)
Patch Set: rebase Created 4 years 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/core/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>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/synchronization/waitable_event.h" 15 #include "base/synchronization/waitable_event.h"
16 #include "base/sys_info.h" 16 #include "base/sys_info.h"
17 #include "base/test/histogram_tester.h" 17 #include "base/test/histogram_tester.h"
18 #include "base/test/scoped_feature_list.h" 18 #include "base/test/scoped_feature_list.h"
19 #include "base/test/test_mock_time_task_runner.h" 19 #include "base/test/test_mock_time_task_runner.h"
20 #include "base/threading/thread_task_runner_handle.h" 20 #include "base/threading/thread_task_runner_handle.h"
21 #include "base/time/time.h" 21 #include "base/time/time.h"
22 #include "components/offline_pages/background/device_conditions.h" 22 #include "components/offline_pages/core/background/device_conditions.h"
23 #include "components/offline_pages/background/network_quality_provider_stub.h" 23 #include "components/offline_pages/core/background/network_quality_provider_stub .h"
24 #include "components/offline_pages/background/offliner.h" 24 #include "components/offline_pages/core/background/offliner.h"
25 #include "components/offline_pages/background/offliner_factory.h" 25 #include "components/offline_pages/core/background/offliner_factory.h"
26 #include "components/offline_pages/background/offliner_factory_stub.h" 26 #include "components/offline_pages/core/background/offliner_factory_stub.h"
27 #include "components/offline_pages/background/offliner_policy.h" 27 #include "components/offline_pages/core/background/offliner_policy.h"
28 #include "components/offline_pages/background/offliner_stub.h" 28 #include "components/offline_pages/core/background/offliner_stub.h"
29 #include "components/offline_pages/background/request_queue.h" 29 #include "components/offline_pages/core/background/request_queue.h"
30 #include "components/offline_pages/background/request_queue_in_memory_store.h" 30 #include "components/offline_pages/core/background/request_queue_in_memory_store .h"
31 #include "components/offline_pages/background/save_page_request.h" 31 #include "components/offline_pages/core/background/save_page_request.h"
32 #include "components/offline_pages/background/scheduler.h" 32 #include "components/offline_pages/core/background/scheduler.h"
33 #include "components/offline_pages/background/scheduler_stub.h" 33 #include "components/offline_pages/core/background/scheduler_stub.h"
34 #include "components/offline_pages/offline_page_feature.h" 34 #include "components/offline_pages/core/offline_page_feature.h"
35 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
36 36
37 namespace offline_pages { 37 namespace offline_pages {
38 38
39 namespace { 39 namespace {
40 // put test constants here 40 // put test constants here
41 const GURL kUrl1("http://universe.com/everything"); 41 const GURL kUrl1("http://universe.com/everything");
42 const GURL kUrl2("http://universe.com/toinfinityandbeyond"); 42 const GURL kUrl2("http://universe.com/toinfinityandbeyond");
43 const std::string kClientNamespace("bookmark"); 43 const std::string kClientNamespace("bookmark");
44 const std::string kId1("42"); 44 const std::string kId1("42");
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 SavePageRequest::RequestState state() { return state_; } 97 SavePageRequest::RequestState state() { return state_; }
98 98
99 private: 99 private:
100 bool added_called_; 100 bool added_called_;
101 bool completed_called_; 101 bool completed_called_;
102 bool changed_called_; 102 bool changed_called_;
103 RequestCoordinator::BackgroundSavePageResult last_status_; 103 RequestCoordinator::BackgroundSavePageResult last_status_;
104 SavePageRequest::RequestState state_; 104 SavePageRequest::RequestState state_;
105 }; 105 };
106 106
107 class RequestCoordinatorTest 107 class RequestCoordinatorTest : public testing::Test {
108 : public testing::Test {
109 public: 108 public:
110 RequestCoordinatorTest(); 109 RequestCoordinatorTest();
111 ~RequestCoordinatorTest() override; 110 ~RequestCoordinatorTest() override;
112 111
113 void SetUp() override; 112 void SetUp() override;
114 113
115 void PumpLoop(); 114 void PumpLoop();
116 115
117 RequestCoordinator* coordinator() { 116 RequestCoordinator* coordinator() { return coordinator_.get(); }
118 return coordinator_.get();
119 }
120 117
121 bool is_busy() { 118 bool is_busy() { return coordinator_->is_busy(); }
122 return coordinator_->is_busy();
123 }
124 119
125 bool is_starting() { return coordinator_->is_starting(); } 120 bool is_starting() { return coordinator_->is_starting(); }
126 121
127 // Empty callback function. 122 // Empty callback function.
128 void ImmediateScheduleCallbackFunction(bool result) { 123 void ImmediateScheduleCallbackFunction(bool result) {
129 immediate_schedule_callback_called_ = true; 124 immediate_schedule_callback_called_ = true;
130 immediate_schedule_callback_result_ = result; 125 immediate_schedule_callback_result_ = result;
131 } 126 }
132 127
133 // Callback function which releases a wait for it. 128 // Callback function which releases a wait for it.
134 void WaitingCallbackFunction(bool result) { 129 void WaitingCallbackFunction(bool result) { waiter_.Signal(); }
135 waiter_.Signal();
136 }
137 130
138 net::NetworkChangeNotifier::ConnectionType GetConnectionType() { 131 net::NetworkChangeNotifier::ConnectionType GetConnectionType() {
139 return coordinator()->GetConnectionType(); 132 return coordinator()->GetConnectionType();
140 } 133 }
141 134
142 // Callback for Add requests. 135 // Callback for Add requests.
143 void AddRequestDone(AddRequestResult result, const SavePageRequest& request); 136 void AddRequestDone(AddRequestResult result, const SavePageRequest& request);
144 137
145 // Callback for getting requests. 138 // Callback for getting requests.
146 void GetRequestsDone(GetRequestsResult result, 139 void GetRequestsDone(GetRequestsResult result,
(...skipping 17 matching lines...) Expand all
164 } 157 }
165 158
166 const std::vector<std::unique_ptr<SavePageRequest>>& last_requests() const { 159 const std::vector<std::unique_ptr<SavePageRequest>>& last_requests() const {
167 return last_requests_; 160 return last_requests_;
168 } 161 }
169 162
170 const MultipleItemStatuses& last_remove_results() const { 163 const MultipleItemStatuses& last_remove_results() const {
171 return last_remove_results_; 164 return last_remove_results_;
172 } 165 }
173 166
174 void DisableLoading() { 167 void DisableLoading() { offliner_->disable_loading(); }
175 offliner_->disable_loading();
176 }
177 168
178 void EnableOfflinerCallback(bool enable) { 169 void EnableOfflinerCallback(bool enable) {
179 offliner_->enable_callback(enable); 170 offliner_->enable_callback(enable);
180 } 171 }
181 172
182 void SetNetworkConditionsForTest( 173 void SetNetworkConditionsForTest(
183 net::NetworkChangeNotifier::ConnectionType connection) { 174 net::NetworkChangeNotifier::ConnectionType connection) {
184 coordinator()->SetNetworkConditionsForTest(connection); 175 coordinator()->SetNetworkConditionsForTest(connection);
185 } 176 }
186 177
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 else 222 else
232 coordinator_->disabled_requests_.clear(); 223 coordinator_->disabled_requests_.clear();
233 224
234 coordinator_->RequestNotPicked(non_user_requested_tasks_remaining, false); 225 coordinator_->RequestNotPicked(non_user_requested_tasks_remaining, false);
235 } 226 }
236 227
237 void SetDeviceConditionsForTest(DeviceConditions device_conditions) { 228 void SetDeviceConditionsForTest(DeviceConditions device_conditions) {
238 coordinator_->SetDeviceConditionsForTest(device_conditions); 229 coordinator_->SetDeviceConditionsForTest(device_conditions);
239 } 230 }
240 231
241 void WaitForCallback() { 232 void WaitForCallback() { waiter_.Wait(); }
242 waiter_.Wait();
243 }
244 233
245 void AdvanceClockBy(base::TimeDelta delta) { 234 void AdvanceClockBy(base::TimeDelta delta) {
246 task_runner_->FastForwardBy(delta); 235 task_runner_->FastForwardBy(delta);
247 } 236 }
248 237
249 SavePageRequest AddRequest1(); 238 SavePageRequest AddRequest1();
250 239
251 SavePageRequest AddRequest2(); 240 SavePageRequest AddRequest2();
252 241
253 Offliner::RequestStatus last_offlining_status() const { 242 Offliner::RequestStatus last_offlining_status() const {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 net::NetworkChangeNotifier::CONNECTION_3G) {} 297 net::NetworkChangeNotifier::CONNECTION_3G) {}
309 298
310 RequestCoordinatorTest::~RequestCoordinatorTest() {} 299 RequestCoordinatorTest::~RequestCoordinatorTest() {}
311 300
312 void RequestCoordinatorTest::SetUp() { 301 void RequestCoordinatorTest::SetUp() {
313 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); 302 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy());
314 std::unique_ptr<OfflinerFactory> offliner_factory(new OfflinerFactoryStub()); 303 std::unique_ptr<OfflinerFactory> offliner_factory(new OfflinerFactoryStub());
315 // Save the offliner for use by the tests. 304 // Save the offliner for use by the tests.
316 offliner_ = reinterpret_cast<OfflinerStub*>( 305 offliner_ = reinterpret_cast<OfflinerStub*>(
317 offliner_factory->GetOffliner(policy.get())); 306 offliner_factory->GetOffliner(policy.get()));
318 std::unique_ptr<RequestQueueInMemoryStore> 307 std::unique_ptr<RequestQueueInMemoryStore> store(
319 store(new RequestQueueInMemoryStore()); 308 new RequestQueueInMemoryStore());
320 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store))); 309 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store)));
321 std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub()); 310 std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub());
322 network_quality_estimator_.reset(new NetworkQualityProviderStub()); 311 network_quality_estimator_.reset(new NetworkQualityProviderStub());
323 coordinator_.reset(new RequestCoordinator( 312 coordinator_.reset(new RequestCoordinator(
324 std::move(policy), std::move(offliner_factory), std::move(queue), 313 std::move(policy), std::move(offliner_factory), std::move(queue),
325 std::move(scheduler_stub), network_quality_estimator_.get())); 314 std::move(scheduler_stub), network_quality_estimator_.get()));
326 coordinator_->AddObserver(&observer_); 315 coordinator_->AddObserver(&observer_);
327 SetNetworkConnected(true); 316 SetNetworkConnected(true);
328 immediate_callback_ = 317 immediate_callback_ =
329 base::Bind(&RequestCoordinatorTest::ImmediateScheduleCallbackFunction, 318 base::Bind(&RequestCoordinatorTest::ImmediateScheduleCallbackFunction,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 base::Unretained(this)); 366 base::Unretained(this));
378 coordinator()->SetProcessingCallbackForTest(callback); 367 coordinator()->SetProcessingCallbackForTest(callback);
379 368
380 // Mock that coordinator is in actively processing state starting now. 369 // Mock that coordinator is in actively processing state starting now.
381 SetProcessingStateForTest( 370 SetProcessingStateForTest(
382 RequestCoordinator::ProcessingWindowState::IMMEDIATE_WINDOW); 371 RequestCoordinator::ProcessingWindowState::IMMEDIATE_WINDOW);
383 SetOperationStartTimeForTest(base::Time::Now()); 372 SetOperationStartTimeForTest(base::Time::Now());
384 } 373 }
385 374
386 void RequestCoordinatorTest::SendOfflinerDoneCallback( 375 void RequestCoordinatorTest::SendOfflinerDoneCallback(
387 const SavePageRequest& request, Offliner::RequestStatus status) { 376 const SavePageRequest& request,
377 Offliner::RequestStatus status) {
388 // Using the fact that the test class is a friend, call to the callback 378 // Using the fact that the test class is a friend, call to the callback
389 coordinator_->OfflinerDoneCallback(request, status); 379 coordinator_->OfflinerDoneCallback(request, status);
390 } 380 }
391 381
392 SavePageRequest RequestCoordinatorTest::AddRequest1() { 382 SavePageRequest RequestCoordinatorTest::AddRequest1() {
393 offline_pages::SavePageRequest request1(kRequestId1, kUrl1, kClientId1, 383 offline_pages::SavePageRequest request1(kRequestId1, kUrl1, kClientId1,
394 base::Time::Now(), kUserRequested); 384 base::Time::Now(), kUserRequested);
395 coordinator()->queue()->AddRequest( 385 coordinator()->queue()->AddRequest(
396 request1, base::Bind(&RequestCoordinatorTest::AddRequestDone, 386 request1, base::Bind(&RequestCoordinatorTest::AddRequestDone,
397 base::Unretained(this))); 387 base::Unretained(this)));
(...skipping 22 matching lines...) Expand all
420 "OfflinePages.Background.ScheduledStart.AvailableRequestCount.Svelte", 410 "OfflinePages.Background.ScheduledStart.AvailableRequestCount.Svelte",
421 0, 1); 411 0, 1);
422 } else { 412 } else {
423 histograms().ExpectBucketCount( 413 histograms().ExpectBucketCount(
424 "OfflinePages.Background.ScheduledStart.AvailableRequestCount", 0, 1); 414 "OfflinePages.Background.ScheduledStart.AvailableRequestCount", 0, 1);
425 } 415 }
426 } 416 }
427 417
428 TEST_F(RequestCoordinatorTest, StartProcessingWithRequestInProgress) { 418 TEST_F(RequestCoordinatorTest, StartProcessingWithRequestInProgress) {
429 // Start processing for this request. 419 // Start processing for this request.
430 EXPECT_NE( 420 EXPECT_NE(coordinator()->SavePageLater(
431 coordinator()->SavePageLater( 421 kUrl1, kClientId1, kUserRequested,
432 kUrl1, kClientId1, kUserRequested, 422 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER),
433 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER), 0); 423 0);
434 424
435 // Ensure that the forthcoming request does not finish - we simulate it being 425 // Ensure that the forthcoming request does not finish - we simulate it being
436 // in progress by asking it to skip making the completion callback. 426 // in progress by asking it to skip making the completion callback.
437 EnableOfflinerCallback(false); 427 EnableOfflinerCallback(false);
438 428
439 // Sending the request to the offliner should make it busy. 429 // Sending the request to the offliner should make it busy.
440 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions(), 430 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions(),
441 immediate_callback())); 431 immediate_callback()));
442 PumpLoop(); 432 PumpLoop();
443 433
444 EXPECT_TRUE(is_busy()); 434 EXPECT_TRUE(is_busy());
445 // Since the offliner is disabled, this callback should not be called. 435 // Since the offliner is disabled, this callback should not be called.
446 EXPECT_FALSE(immediate_schedule_callback_called()); 436 EXPECT_FALSE(immediate_schedule_callback_called());
447 437
448 // Now trying to start processing on another request should return false. 438 // Now trying to start processing on another request should return false.
449 EXPECT_FALSE(coordinator()->StartProcessing(device_conditions(), 439 EXPECT_FALSE(coordinator()->StartProcessing(device_conditions(),
450 immediate_callback())); 440 immediate_callback()));
451 } 441 }
452 442
453 TEST_F(RequestCoordinatorTest, SavePageLater) { 443 TEST_F(RequestCoordinatorTest, SavePageLater) {
454 // The user-requested request which gets processed by SavePageLater 444 // The user-requested request which gets processed by SavePageLater
455 // would invoke user request callback. 445 // would invoke user request callback.
456 coordinator()->SetImmediateScheduleCallbackForTest(immediate_callback()); 446 coordinator()->SetImmediateScheduleCallbackForTest(immediate_callback());
457 447
458 EXPECT_NE( 448 EXPECT_NE(coordinator()->SavePageLater(
459 coordinator()->SavePageLater( 449 kUrl1, kClientId1, kUserRequested,
460 kUrl1, kClientId1, kUserRequested, 450 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER),
461 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER), 0); 451 0);
462 452
463 // Expect that a request got placed on the queue. 453 // Expect that a request got placed on the queue.
464 coordinator()->queue()->GetRequests(base::Bind( 454 coordinator()->queue()->GetRequests(base::Bind(
465 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this))); 455 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this)));
466 456
467 // Wait for callbacks to finish, both request queue and offliner. 457 // Wait for callbacks to finish, both request queue and offliner.
468 PumpLoop(); 458 PumpLoop();
469 EXPECT_TRUE(immediate_schedule_callback_called()); 459 EXPECT_TRUE(immediate_schedule_callback_called());
470 460
471 // Check the request queue is as expected. 461 // Check the request queue is as expected.
(...skipping 28 matching lines...) Expand all
500 // The user-requested request which gets processed by SavePageLater 490 // The user-requested request which gets processed by SavePageLater
501 // would invoke user request callback. 491 // would invoke user request callback.
502 coordinator()->SetImmediateScheduleCallbackForTest(immediate_callback()); 492 coordinator()->SetImmediateScheduleCallbackForTest(immediate_callback());
503 493
504 EXPECT_TRUE( 494 EXPECT_TRUE(
505 coordinator()->SavePageLater( 495 coordinator()->SavePageLater(
506 kUrl1, kClientId1, kUserRequested, 496 kUrl1, kClientId1, kUserRequested,
507 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER) != 0); 497 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER) != 0);
508 498
509 // Expect that a request got placed on the queue. 499 // Expect that a request got placed on the queue.
510 coordinator()->queue()->GetRequests( 500 coordinator()->queue()->GetRequests(base::Bind(
511 base::Bind(&RequestCoordinatorTest::GetRequestsDone, 501 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this)));
512 base::Unretained(this)));
513 502
514 // Wait for callbacks to finish, both request queue and offliner. 503 // Wait for callbacks to finish, both request queue and offliner.
515 PumpLoop(); 504 PumpLoop();
516 505
517 // On low-end devices the callback will be called with false since the 506 // On low-end devices the callback will be called with false since the
518 // processing started but failed due to svelte devices. 507 // processing started but failed due to svelte devices.
519 EXPECT_TRUE(immediate_schedule_callback_called()); 508 EXPECT_TRUE(immediate_schedule_callback_called());
520 if (base::SysInfo::IsLowEndDevice()) { 509 if (base::SysInfo::IsLowEndDevice()) {
521 EXPECT_FALSE(immediate_schedule_callback_result()); 510 EXPECT_FALSE(immediate_schedule_callback_result());
522 } else { 511 } else {
523 EXPECT_TRUE(immediate_schedule_callback_result()); 512 EXPECT_TRUE(immediate_schedule_callback_result());
524 } 513 }
525 514
526 // Check the request queue is as expected. 515 // Check the request queue is as expected.
527 EXPECT_EQ(1UL, last_requests().size()); 516 EXPECT_EQ(1UL, last_requests().size());
528 EXPECT_EQ(kUrl1, last_requests().at(0)->url()); 517 EXPECT_EQ(kUrl1, last_requests().at(0)->url());
529 EXPECT_EQ(kClientId1, last_requests().at(0)->client_id()); 518 EXPECT_EQ(kClientId1, last_requests().at(0)->client_id());
530 519
531 // Expect that the scheduler got notified. 520 // Expect that the scheduler got notified.
532 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( 521 SchedulerStub* scheduler_stub =
533 coordinator()->scheduler()); 522 reinterpret_cast<SchedulerStub*>(coordinator()->scheduler());
534 EXPECT_TRUE(scheduler_stub->schedule_called()); 523 EXPECT_TRUE(scheduler_stub->schedule_called());
535 EXPECT_EQ(coordinator() 524 EXPECT_EQ(coordinator()
536 ->GetTriggerConditions(last_requests()[0]->user_requested()) 525 ->GetTriggerConditions(last_requests()[0]->user_requested())
537 .minimum_battery_percentage, 526 .minimum_battery_percentage,
538 scheduler_stub->conditions()->minimum_battery_percentage); 527 scheduler_stub->conditions()->minimum_battery_percentage);
539 528
540 // Check that the observer got the notification that a page is available 529 // Check that the observer got the notification that a page is available
541 EXPECT_TRUE(observer().added_called()); 530 EXPECT_TRUE(observer().added_called());
542 } 531 }
543 532
544 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { 533 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) {
545 // Add a request to the queue, wait for callbacks to finish. 534 // Add a request to the queue, wait for callbacks to finish.
546 offline_pages::SavePageRequest request( 535 offline_pages::SavePageRequest request(kRequestId1, kUrl1, kClientId1,
547 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); 536 base::Time::Now(), kUserRequested);
548 SetupForOfflinerDoneCallbackTest(&request); 537 SetupForOfflinerDoneCallbackTest(&request);
549 538
550 // Call the OfflinerDoneCallback to simulate the page being completed, wait 539 // Call the OfflinerDoneCallback to simulate the page being completed, wait
551 // for callbacks. 540 // for callbacks.
552 SendOfflinerDoneCallback(request, Offliner::RequestStatus::SAVED); 541 SendOfflinerDoneCallback(request, Offliner::RequestStatus::SAVED);
553 PumpLoop(); 542 PumpLoop();
554 EXPECT_TRUE(immediate_schedule_callback_called()); 543 EXPECT_TRUE(immediate_schedule_callback_called());
555 544
556 // Verify the request gets removed from the queue, and wait for callbacks. 545 // Verify the request gets removed from the queue, and wait for callbacks.
557 coordinator()->queue()->GetRequests( 546 coordinator()->queue()->GetRequests(base::Bind(
558 base::Bind(&RequestCoordinatorTest::GetRequestsDone, 547 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this)));
559 base::Unretained(this)));
560 PumpLoop(); 548 PumpLoop();
561 549
562 // We should not find any requests in the queue anymore. 550 // We should not find any requests in the queue anymore.
563 // RequestPicker should *not* have tried to start an additional job, 551 // RequestPicker should *not* have tried to start an additional job,
564 // because the request queue is empty now. 552 // because the request queue is empty now.
565 EXPECT_EQ(0UL, last_requests().size()); 553 EXPECT_EQ(0UL, last_requests().size());
566 // Check that the observer got the notification that we succeeded, and that 554 // Check that the observer got the notification that we succeeded, and that
567 // the request got removed from the queue. 555 // the request got removed from the queue.
568 EXPECT_TRUE(observer().completed_called()); 556 EXPECT_TRUE(observer().completed_called());
569 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::SUCCESS, 557 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::SUCCESS,
(...skipping 24 matching lines...) Expand all
594 582
595 // Now connect network and verify processing starts. 583 // Now connect network and verify processing starts.
596 SetNetworkConnected(true); 584 SetNetworkConnected(true);
597 CallConnectionTypeObserver(); 585 CallConnectionTypeObserver();
598 PumpLoop(); 586 PumpLoop();
599 EXPECT_TRUE(is_busy()); 587 EXPECT_TRUE(is_busy());
600 } 588 }
601 589
602 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { 590 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) {
603 // Add a request to the queue, wait for callbacks to finish. 591 // Add a request to the queue, wait for callbacks to finish.
604 offline_pages::SavePageRequest request( 592 offline_pages::SavePageRequest request(kRequestId1, kUrl1, kClientId1,
605 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); 593 base::Time::Now(), kUserRequested);
606 request.set_completed_attempt_count(kMaxCompletedTries - 1); 594 request.set_completed_attempt_count(kMaxCompletedTries - 1);
607 SetupForOfflinerDoneCallbackTest(&request); 595 SetupForOfflinerDoneCallbackTest(&request);
608 // Stop processing before completing the second request on the queue. 596 // Stop processing before completing the second request on the queue.
609 EnableOfflinerCallback(false); 597 EnableOfflinerCallback(false);
610 598
611 // Add second request to the queue to check handling when first fails. 599 // Add second request to the queue to check handling when first fails.
612 AddRequest2(); 600 AddRequest2();
613 PumpLoop(); 601 PumpLoop();
614 602
615 // Call the OfflinerDoneCallback to simulate the request failed, wait 603 // Call the OfflinerDoneCallback to simulate the request failed, wait
616 // for callbacks. 604 // for callbacks.
617 SendOfflinerDoneCallback(request, 605 SendOfflinerDoneCallback(request,
618 Offliner::RequestStatus::PRERENDERING_FAILED); 606 Offliner::RequestStatus::PRERENDERING_FAILED);
619 PumpLoop(); 607 PumpLoop();
620 608
621 // For retriable failure, processing should continue to 2nd request so 609 // For retriable failure, processing should continue to 2nd request so
622 // no scheduler callback yet. 610 // no scheduler callback yet.
623 EXPECT_FALSE(immediate_schedule_callback_called()); 611 EXPECT_FALSE(immediate_schedule_callback_called());
624 612
625 // Busy processing 2nd request. 613 // Busy processing 2nd request.
626 EXPECT_TRUE(is_busy()); 614 EXPECT_TRUE(is_busy());
627 615
628 coordinator()->queue()->GetRequests( 616 coordinator()->queue()->GetRequests(base::Bind(
629 base::Bind(&RequestCoordinatorTest::GetRequestsDone, 617 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this)));
630 base::Unretained(this)));
631 PumpLoop(); 618 PumpLoop();
632 619
633 // Now just one request in the queue since failed request removed 620 // Now just one request in the queue since failed request removed
634 // (max number of attempts exceeded). 621 // (max number of attempts exceeded).
635 EXPECT_EQ(1UL, last_requests().size()); 622 EXPECT_EQ(1UL, last_requests().size());
636 // Check that the observer got the notification that we failed (and the 623 // Check that the observer got the notification that we failed (and the
637 // subsequent notification that the request was removed) since we exceeded 624 // subsequent notification that the request was removed) since we exceeded
638 // retry count. 625 // retry count.
639 EXPECT_TRUE(observer().completed_called()); 626 EXPECT_TRUE(observer().completed_called());
640 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::RETRY_COUNT_EXCEEDED, 627 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::RETRY_COUNT_EXCEEDED,
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 coordinator()->queue()->GetRequests(base::Bind( 692 coordinator()->queue()->GetRequests(base::Bind(
706 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this))); 693 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this)));
707 PumpLoop(); 694 PumpLoop();
708 695
709 // Both requests still in queue. 696 // Both requests still in queue.
710 EXPECT_EQ(2UL, last_requests().size()); 697 EXPECT_EQ(2UL, last_requests().size());
711 } 698 }
712 699
713 TEST_F(RequestCoordinatorTest, OfflinerDoneForegroundCancel) { 700 TEST_F(RequestCoordinatorTest, OfflinerDoneForegroundCancel) {
714 // Add a request to the queue, wait for callbacks to finish. 701 // Add a request to the queue, wait for callbacks to finish.
715 offline_pages::SavePageRequest request( 702 offline_pages::SavePageRequest request(kRequestId1, kUrl1, kClientId1,
716 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); 703 base::Time::Now(), kUserRequested);
717 SetupForOfflinerDoneCallbackTest(&request); 704 SetupForOfflinerDoneCallbackTest(&request);
718 705
719 // Call the OfflinerDoneCallback to simulate the request failed, wait 706 // Call the OfflinerDoneCallback to simulate the request failed, wait
720 // for callbacks. 707 // for callbacks.
721 SendOfflinerDoneCallback(request, 708 SendOfflinerDoneCallback(request,
722 Offliner::RequestStatus::FOREGROUND_CANCELED); 709 Offliner::RequestStatus::FOREGROUND_CANCELED);
723 PumpLoop(); 710 PumpLoop();
724 EXPECT_TRUE(immediate_schedule_callback_called()); 711 EXPECT_TRUE(immediate_schedule_callback_called());
725 712
726 // Verify the request is not removed from the queue, and wait for callbacks. 713 // Verify the request is not removed from the queue, and wait for callbacks.
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
1009 coordinator()->EnableForOffliner(request_id, kClientId1); 996 coordinator()->EnableForOffliner(request_id, kClientId1);
1010 PumpLoop(); 997 PumpLoop();
1011 998
1012 // Verify request changed again. 999 // Verify request changed again.
1013 EXPECT_TRUE(observer().changed_called()); 1000 EXPECT_TRUE(observer().changed_called());
1014 EXPECT_EQ(SavePageRequest::RequestState::AVAILABLE, observer().state()); 1001 EXPECT_EQ(SavePageRequest::RequestState::AVAILABLE, observer().state());
1015 } 1002 }
1016 1003
1017 TEST_F(RequestCoordinatorTest, WatchdogTimeoutForScheduledProcessing) { 1004 TEST_F(RequestCoordinatorTest, WatchdogTimeoutForScheduledProcessing) {
1018 // Build a request to use with the pre-renderer, and put it on the queue. 1005 // Build a request to use with the pre-renderer, and put it on the queue.
1019 offline_pages::SavePageRequest request( 1006 offline_pages::SavePageRequest request(kRequestId1, kUrl1, kClientId1,
1020 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); 1007 base::Time::Now(), kUserRequested);
1021 // Set request to allow one more completed attempt. 1008 // Set request to allow one more completed attempt.
1022 int max_tries = coordinator()->policy()->GetMaxCompletedTries(); 1009 int max_tries = coordinator()->policy()->GetMaxCompletedTries();
1023 request.set_completed_attempt_count(max_tries - 1); 1010 request.set_completed_attempt_count(max_tries - 1);
1024 coordinator()->queue()->AddRequest( 1011 coordinator()->queue()->AddRequest(
1025 request, 1012 request, base::Bind(&RequestCoordinatorTest::AddRequestDone,
1026 base::Bind(&RequestCoordinatorTest::AddRequestDone, 1013 base::Unretained(this)));
1027 base::Unretained(this)));
1028 PumpLoop(); 1014 PumpLoop();
1029 1015
1030 // Ensure that the new request does not finish - we simulate it being 1016 // Ensure that the new request does not finish - we simulate it being
1031 // in progress by asking it to skip making the completion callback. 1017 // in progress by asking it to skip making the completion callback.
1032 EnableOfflinerCallback(false); 1018 EnableOfflinerCallback(false);
1033 1019
1034 // Sending the request to the offliner. 1020 // Sending the request to the offliner.
1035 EXPECT_TRUE( 1021 EXPECT_TRUE(
1036 coordinator()->StartProcessing(device_conditions(), waiting_callback())); 1022 coordinator()->StartProcessing(device_conditions(), waiting_callback()));
1037 PumpLoop(); 1023 PumpLoop();
1038 1024
1039 // Advance the mock clock far enough to cause a watchdog timeout 1025 // Advance the mock clock far enough to cause a watchdog timeout
1040 AdvanceClockBy(base::TimeDelta::FromSeconds( 1026 AdvanceClockBy(base::TimeDelta::FromSeconds(
1041 coordinator() 1027 coordinator()
1042 ->policy() 1028 ->policy()
1043 ->GetSinglePageTimeLimitWhenBackgroundScheduledInSeconds() + 1)); 1029 ->GetSinglePageTimeLimitWhenBackgroundScheduledInSeconds() +
1030 1));
1044 PumpLoop(); 1031 PumpLoop();
1045 1032
1046 // Wait for timeout to expire. Use a TaskRunner with a DelayedTaskRunner 1033 // Wait for timeout to expire. Use a TaskRunner with a DelayedTaskRunner
1047 // which won't time out immediately, so the watchdog thread doesn't kill valid 1034 // which won't time out immediately, so the watchdog thread doesn't kill valid
1048 // tasks too soon. 1035 // tasks too soon.
1049 WaitForCallback(); 1036 WaitForCallback();
1050 PumpLoop(); 1037 PumpLoop();
1051 1038
1052 EXPECT_FALSE(is_starting()); 1039 EXPECT_FALSE(is_starting());
1053 EXPECT_FALSE(coordinator()->is_busy()); 1040 EXPECT_FALSE(coordinator()->is_busy());
1054 EXPECT_TRUE(OfflinerWasCanceled()); 1041 EXPECT_TRUE(OfflinerWasCanceled());
1055 } 1042 }
1056 1043
1057 TEST_F(RequestCoordinatorTest, WatchdogTimeoutForImmediateProcessing) { 1044 TEST_F(RequestCoordinatorTest, WatchdogTimeoutForImmediateProcessing) {
1058 // If low end device, pretend it is not so that immediate start happens. 1045 // If low end device, pretend it is not so that immediate start happens.
1059 SetIsLowEndDeviceForTest(false); 1046 SetIsLowEndDeviceForTest(false);
1060 1047
1061 // Ensure that the new request does not finish - we simulate it being 1048 // Ensure that the new request does not finish - we simulate it being
1062 // in progress by asking it to skip making the completion callback. 1049 // in progress by asking it to skip making the completion callback.
1063 EnableOfflinerCallback(false); 1050 EnableOfflinerCallback(false);
1064 1051
1065 EXPECT_NE( 1052 EXPECT_NE(coordinator()->SavePageLater(
1066 coordinator()->SavePageLater( 1053 kUrl1, kClientId1, kUserRequested,
1067 kUrl1, kClientId1, kUserRequested, 1054 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER),
1068 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER), 0); 1055 0);
1069 PumpLoop(); 1056 PumpLoop();
1070 1057
1071 // Verify that immediate start from adding the request did happen. 1058 // Verify that immediate start from adding the request did happen.
1072 EXPECT_TRUE(coordinator()->is_busy()); 1059 EXPECT_TRUE(coordinator()->is_busy());
1073 1060
1074 // Advance the mock clock 1 second before the watchdog timeout. 1061 // Advance the mock clock 1 second before the watchdog timeout.
1075 AdvanceClockBy(base::TimeDelta::FromSeconds( 1062 AdvanceClockBy(base::TimeDelta::FromSeconds(
1076 coordinator() 1063 coordinator()
1077 ->policy() 1064 ->policy()
1078 ->GetSinglePageTimeLimitForImmediateLoadInSeconds() - 1)); 1065 ->GetSinglePageTimeLimitForImmediateLoadInSeconds() -
1066 1));
1079 PumpLoop(); 1067 PumpLoop();
1080 1068
1081 // Verify still busy. 1069 // Verify still busy.
1082 EXPECT_TRUE(coordinator()->is_busy()); 1070 EXPECT_TRUE(coordinator()->is_busy());
1083 EXPECT_FALSE(OfflinerWasCanceled()); 1071 EXPECT_FALSE(OfflinerWasCanceled());
1084 1072
1085 // Advance the mock clock past the watchdog timeout now. 1073 // Advance the mock clock past the watchdog timeout now.
1086 AdvanceClockBy(base::TimeDelta::FromSeconds(2)); 1074 AdvanceClockBy(base::TimeDelta::FromSeconds(2));
1087 PumpLoop(); 1075 PumpLoop();
1088 1076
(...skipping 21 matching lines...) Expand all
1110 1098
1111 // Advance the mock clock far enough to exceed our time budget. 1099 // Advance the mock clock far enough to exceed our time budget.
1112 // The first request will time out, and because we are over time budget, 1100 // The first request will time out, and because we are over time budget,
1113 // the second request will not be started. 1101 // the second request will not be started.
1114 AdvanceClockBy(base::TimeDelta::FromSeconds(kTestTimeBudgetSeconds)); 1102 AdvanceClockBy(base::TimeDelta::FromSeconds(kTestTimeBudgetSeconds));
1115 PumpLoop(); 1103 PumpLoop();
1116 1104
1117 // TryNextRequest should decide that there is no more work to be done, 1105 // TryNextRequest should decide that there is no more work to be done,
1118 // and call back to the scheduler, even though there is another request in the 1106 // and call back to the scheduler, even though there is another request in the
1119 // queue. Both requests should be left in the queue. 1107 // queue. Both requests should be left in the queue.
1120 coordinator()->queue()->GetRequests( 1108 coordinator()->queue()->GetRequests(base::Bind(
1121 base::Bind(&RequestCoordinatorTest::GetRequestsDone, 1109 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this)));
1122 base::Unretained(this)));
1123 PumpLoop(); 1110 PumpLoop();
1124 1111
1125 // We should find two requests in the queue. 1112 // We should find two requests in the queue.
1126 // The first request should now have a completed count of 1. 1113 // The first request should now have a completed count of 1.
1127 EXPECT_EQ(2UL, last_requests().size()); 1114 EXPECT_EQ(2UL, last_requests().size());
1128 EXPECT_EQ(1L, last_requests().at(0)->completed_attempt_count()); 1115 EXPECT_EQ(1L, last_requests().at(0)->completed_attempt_count());
1129 } 1116 }
1130 1117
1131 TEST_F(RequestCoordinatorTest, TryNextRequestWithNoNetwork) { 1118 TEST_F(RequestCoordinatorTest, TryNextRequestWithNoNetwork) {
1132 SavePageRequest request1 = AddRequest1(); 1119 SavePageRequest request1 = AddRequest1();
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
1243 observer().last_status()); 1230 observer().last_status());
1244 EXPECT_EQ(1UL, last_remove_results().size()); 1231 EXPECT_EQ(1UL, last_remove_results().size());
1245 EXPECT_EQ(kRequestId1, std::get<0>(last_remove_results().at(0))); 1232 EXPECT_EQ(kRequestId1, std::get<0>(last_remove_results().at(0)));
1246 } 1233 }
1247 1234
1248 TEST_F(RequestCoordinatorTest, 1235 TEST_F(RequestCoordinatorTest,
1249 SavePageStartsProcessingWhenConnectedAndNotLowEndDevice) { 1236 SavePageStartsProcessingWhenConnectedAndNotLowEndDevice) {
1250 // Turn off the callback so that the request stops before processing in 1237 // Turn off the callback so that the request stops before processing in
1251 // PumpLoop. 1238 // PumpLoop.
1252 EnableOfflinerCallback(false); 1239 EnableOfflinerCallback(false);
1253 EXPECT_NE( 1240 EXPECT_NE(coordinator()->SavePageLater(
1254 coordinator()->SavePageLater( 1241 kUrl1, kClientId1, kUserRequested,
1255 kUrl1, kClientId1, kUserRequested, 1242 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER),
1256 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER), 0); 1243 0);
1257 PumpLoop(); 1244 PumpLoop();
1258 1245
1259 // Now whether processing triggered immediately depends on whether test 1246 // Now whether processing triggered immediately depends on whether test
1260 // is run on svelte device or not. 1247 // is run on svelte device or not.
1261 if (base::SysInfo::IsLowEndDevice()) { 1248 if (base::SysInfo::IsLowEndDevice()) {
1262 EXPECT_FALSE(is_busy()); 1249 EXPECT_FALSE(is_busy());
1263 } else { 1250 } else {
1264 EXPECT_TRUE(is_busy()); 1251 EXPECT_TRUE(is_busy());
1265 } 1252 }
1266 } 1253 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1327 // Set specific network type for 2G with poor effective connection. 1314 // Set specific network type for 2G with poor effective connection.
1328 SetNetworkConditionsForTest( 1315 SetNetworkConditionsForTest(
1329 net::NetworkChangeNotifier::ConnectionType::CONNECTION_2G); 1316 net::NetworkChangeNotifier::ConnectionType::CONNECTION_2G);
1330 SetEffectiveConnectionTypeForTest( 1317 SetEffectiveConnectionTypeForTest(
1331 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); 1318 net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
1332 1319
1333 // Turn off the callback so that the request stops before processing in 1320 // Turn off the callback so that the request stops before processing in
1334 // PumpLoop. 1321 // PumpLoop.
1335 EnableOfflinerCallback(false); 1322 EnableOfflinerCallback(false);
1336 1323
1337 EXPECT_NE( 1324 EXPECT_NE(coordinator()->SavePageLater(
1338 coordinator()->SavePageLater( 1325 kUrl1, kClientId1, kUserRequested,
1339 kUrl1, kClientId1, kUserRequested, 1326 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER),
1340 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER), 0); 1327 0);
1341 PumpLoop(); 1328 PumpLoop();
1342 EXPECT_TRUE(is_busy()); 1329 EXPECT_TRUE(is_busy());
1343 } 1330 }
1344 1331
1345 TEST_F(RequestCoordinatorTest, 1332 TEST_F(RequestCoordinatorTest,
1346 ResumeStartsProcessingWhenConnectedAndNotLowEndDevice) { 1333 ResumeStartsProcessingWhenConnectedAndNotLowEndDevice) {
1347 // Start unconnected. 1334 // Start unconnected.
1348 SetNetworkConnected(false); 1335 SetNetworkConnected(false);
1349 1336
1350 // Turn off the callback so that the request stops before processing in 1337 // Turn off the callback so that the request stops before processing in
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1382 // Now whether processing triggered immediately depends on whether test 1369 // Now whether processing triggered immediately depends on whether test
1383 // is run on svelte device or not. 1370 // is run on svelte device or not.
1384 if (base::SysInfo::IsLowEndDevice()) { 1371 if (base::SysInfo::IsLowEndDevice()) {
1385 EXPECT_FALSE(is_busy()); 1372 EXPECT_FALSE(is_busy());
1386 } else { 1373 } else {
1387 EXPECT_TRUE(is_busy()); 1374 EXPECT_TRUE(is_busy());
1388 } 1375 }
1389 } 1376 }
1390 1377
1391 } // namespace offline_pages 1378 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698