Chromium Code Reviews| 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/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/test/test_simple_task_runner.h" | 14 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/test/test_mock_time_task_runner.h" | |
| 15 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "components/offline_pages/background/device_conditions.h" | 17 #include "components/offline_pages/background/device_conditions.h" |
| 17 #include "components/offline_pages/background/offliner.h" | 18 #include "components/offline_pages/background/offliner.h" |
| 18 #include "components/offline_pages/background/offliner_factory.h" | 19 #include "components/offline_pages/background/offliner_factory.h" |
| 19 #include "components/offline_pages/background/offliner_policy.h" | 20 #include "components/offline_pages/background/offliner_policy.h" |
| 20 #include "components/offline_pages/background/request_queue.h" | 21 #include "components/offline_pages/background/request_queue.h" |
| 21 #include "components/offline_pages/background/request_queue_in_memory_store.h" | 22 #include "components/offline_pages/background/request_queue_in_memory_store.h" |
| 22 #include "components/offline_pages/background/save_page_request.h" | 23 #include "components/offline_pages/background/save_page_request.h" |
| 23 #include "components/offline_pages/background/scheduler.h" | 24 #include "components/offline_pages/background/scheduler.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 namespace offline_pages { | 27 namespace offline_pages { |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 // put test constants here | 30 // put test constants here |
| 30 const GURL kUrl("http://universe.com/everything"); | 31 const GURL kUrl("http://universe.com/everything"); |
| 31 const ClientId kClientId("bookmark", "42"); | 32 const ClientId kClientId("bookmark", "42"); |
| 32 const int kRequestId(1); | 33 const int kRequestId(1); |
| 34 const long TEST_TIMEOUT_SECONDS = 1; | |
| 35 const int BATTERY_PERCENTAGE_HIGH = 75; | |
| 36 const bool POWER_NOT_REQUIRED = false; | |
|
dougarnett
2016/06/30 19:16:48
nit - this declaration is bit confusing to me - vs
Pete Williamson
2016/07/01 17:16:55
Done.
| |
| 33 } // namespace | 37 } // namespace |
| 34 | 38 |
| 35 class SchedulerStub : public Scheduler { | 39 class SchedulerStub : public Scheduler { |
| 36 public: | 40 public: |
| 37 SchedulerStub() | 41 SchedulerStub() |
| 38 : schedule_called_(false), | 42 : schedule_called_(false), |
| 39 unschedule_called_(false), | 43 unschedule_called_(false), |
| 40 conditions_(false, 0, false) {} | 44 conditions_(false, 0, false) {} |
| 41 | 45 |
| 42 void Schedule(const TriggerConditions& trigger_conditions) override { | 46 void Schedule(const TriggerConditions& trigger_conditions) override { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 void PumpLoop(); | 129 void PumpLoop(); |
| 126 | 130 |
| 127 RequestCoordinator* coordinator() { | 131 RequestCoordinator* coordinator() { |
| 128 return coordinator_.get(); | 132 return coordinator_.get(); |
| 129 } | 133 } |
| 130 | 134 |
| 131 bool is_busy() { | 135 bool is_busy() { |
| 132 return coordinator_->is_busy(); | 136 return coordinator_->is_busy(); |
| 133 } | 137 } |
| 134 | 138 |
| 135 void SendRequestToOffliner(SavePageRequest& request) { | |
| 136 coordinator_->SendRequestToOffliner(request); | |
| 137 } | |
| 138 | |
| 139 // Empty callback function | 139 // Empty callback function |
| 140 void EmptyCallbackFunction(bool result) { | 140 void EmptyCallbackFunction(bool result) { |
| 141 } | 141 } |
| 142 | 142 |
| 143 // Callback function which releases a wait for it. | |
| 144 void WaitingCallbackFunction(bool result) { | |
| 145 waiter_.Signal(); | |
| 146 } | |
| 147 | |
| 143 // Callback for Add requests | 148 // Callback for Add requests |
| 144 void AddRequestDone(RequestQueue::AddRequestResult result, | 149 void AddRequestDone(RequestQueue::AddRequestResult result, |
| 145 const SavePageRequest& request); | 150 const SavePageRequest& request); |
| 146 | 151 |
| 147 // Callback for getting requests. | 152 // Callback for getting requests. |
| 148 void GetRequestsDone(RequestQueue::GetRequestsResult result, | 153 void GetRequestsDone(RequestQueue::GetRequestsResult result, |
| 149 const std::vector<SavePageRequest>& requests); | 154 const std::vector<SavePageRequest>& requests); |
| 150 | 155 |
| 151 void SendOfflinerDoneCallback(const SavePageRequest& request, | 156 void SendOfflinerDoneCallback(const SavePageRequest& request, |
| 152 Offliner::RequestStatus status); | 157 Offliner::RequestStatus status); |
| 153 | 158 |
| 154 RequestQueue::GetRequestsResult last_get_requests_result() const { | 159 RequestQueue::GetRequestsResult last_get_requests_result() const { |
| 155 return last_get_requests_result_; | 160 return last_get_requests_result_; |
| 156 } | 161 } |
| 157 | 162 |
| 158 const std::vector<SavePageRequest>& last_requests() const { | 163 const std::vector<SavePageRequest>& last_requests() const { |
| 159 return last_requests_; | 164 return last_requests_; |
| 160 } | 165 } |
| 161 | 166 |
| 162 void EnableOfflinerCallback(bool enable) { | 167 void EnableOfflinerCallback(bool enable) { |
| 163 offliner_->enable_callback(enable); | 168 offliner_->enable_callback(enable); |
| 164 } | 169 } |
| 165 | 170 |
| 171 void SetOfflinerTimeoutForTest(long timeout) { | |
| 172 coordinator_->SetOfflinerTimeoutForTest(timeout); | |
| 173 } | |
| 174 | |
| 175 void WaitForCallback() { | |
| 176 waiter_.Wait(); | |
| 177 } | |
| 178 | |
| 179 void AdvanceClockBy(base::TimeDelta delta) { | |
| 180 task_runner_->FastForwardBy(delta); | |
| 181 } | |
| 182 | |
| 166 Offliner::RequestStatus last_offlining_status() const { | 183 Offliner::RequestStatus last_offlining_status() const { |
| 167 return coordinator_->last_offlining_status_; | 184 return coordinator_->last_offlining_status_; |
| 168 } | 185 } |
| 169 | 186 |
| 170 private: | 187 private: |
| 171 RequestQueue::GetRequestsResult last_get_requests_result_; | 188 RequestQueue::GetRequestsResult last_get_requests_result_; |
| 172 std::vector<SavePageRequest> last_requests_; | 189 std::vector<SavePageRequest> last_requests_; |
| 173 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 190 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
| 174 base::ThreadTaskRunnerHandle task_runner_handle_; | 191 base::ThreadTaskRunnerHandle task_runner_handle_; |
| 175 std::unique_ptr<RequestCoordinator> coordinator_; | 192 std::unique_ptr<RequestCoordinator> coordinator_; |
| 176 OfflinerStub* offliner_; | 193 OfflinerStub* offliner_; |
| 194 base::WaitableEvent waiter_; | |
| 177 }; | 195 }; |
| 178 | 196 |
| 179 RequestCoordinatorTest::RequestCoordinatorTest() | 197 RequestCoordinatorTest::RequestCoordinatorTest() |
| 180 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE), | 198 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE), |
| 181 task_runner_(new base::TestSimpleTaskRunner), | 199 task_runner_(new base::TestMockTimeTaskRunner), |
| 182 task_runner_handle_(task_runner_), | 200 task_runner_handle_(task_runner_), |
| 183 offliner_(nullptr) {} | 201 offliner_(nullptr), |
| 202 waiter_(base::WaitableEvent::ResetPolicy::MANUAL, | |
| 203 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | |
| 184 | 204 |
| 185 RequestCoordinatorTest::~RequestCoordinatorTest() {} | 205 RequestCoordinatorTest::~RequestCoordinatorTest() {} |
| 186 | 206 |
| 187 void RequestCoordinatorTest::SetUp() { | 207 void RequestCoordinatorTest::SetUp() { |
| 188 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); | 208 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); |
| 189 std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub()); | 209 std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub()); |
| 190 // Save the offliner for use by the tests. | 210 // Save the offliner for use by the tests. |
| 191 offliner_ = | 211 offliner_ = |
| 192 reinterpret_cast<OfflinerStub*>(factory->GetOffliner(policy.get())); | 212 reinterpret_cast<OfflinerStub*>(factory->GetOffliner(policy.get())); |
| 193 std::unique_ptr<RequestQueueInMemoryStore> | 213 std::unique_ptr<RequestQueueInMemoryStore> |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 409 coordinator()->StopProcessing(); | 429 coordinator()->StopProcessing(); |
| 410 | 430 |
| 411 // Let the async callbacks in the cancel run. | 431 // Let the async callbacks in the cancel run. |
| 412 PumpLoop(); | 432 PumpLoop(); |
| 413 | 433 |
| 414 // OfflinerDoneCallback will not end up getting called with status SAVED, | 434 // OfflinerDoneCallback will not end up getting called with status SAVED, |
| 415 // Since we cancelled the event before it called offliner_->LoadAndSave(). | 435 // Since we cancelled the event before it called offliner_->LoadAndSave(). |
| 416 EXPECT_EQ(Offliner::RequestStatus::CANCELED, last_offlining_status()); | 436 EXPECT_EQ(Offliner::RequestStatus::CANCELED, last_offlining_status()); |
| 417 } | 437 } |
| 418 | 438 |
| 439 TEST_F(RequestCoordinatorTest, PrerendererTimeout) { | |
| 440 // Build a request to use with the pre-renderer, and put it on the queue. | |
| 441 offline_pages::SavePageRequest request( | |
| 442 kRequestId, kUrl, kClientId, base::Time::Now()); | |
| 443 coordinator()->queue()->AddRequest( | |
| 444 request, | |
| 445 base::Bind(&RequestCoordinatorTest::AddRequestDone, | |
| 446 base::Unretained(this))); | |
| 447 PumpLoop(); | |
| 448 | |
| 449 // Set up for the call to StartProcessing. | |
| 450 DeviceConditions device_conditions( | |
| 451 POWER_NOT_REQUIRED, BATTERY_PERCENTAGE_HIGH, | |
| 452 net::NetworkChangeNotifier::CONNECTION_3G); | |
| 453 base::Callback<void(bool)> callback = | |
| 454 base::Bind(&RequestCoordinatorTest::WaitingCallbackFunction, | |
| 455 base::Unretained(this)); | |
| 456 | |
| 457 // Ensure that the new request does not finish - we simulate it being | |
| 458 // in progress by asking it to skip making the completion callback. | |
| 459 EnableOfflinerCallback(false); | |
| 460 | |
| 461 // Ask RequestCoordinator to stop waiting for the offliner after this many | |
| 462 // seconds. | |
| 463 SetOfflinerTimeoutForTest(TEST_TIMEOUT_SECONDS); | |
| 464 | |
| 465 // Sending the request to the offliner. | |
| 466 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); | |
| 467 PumpLoop(); | |
| 468 | |
| 469 // Advance the mock clock far enough to cause a watchdog timeout | |
| 470 AdvanceClockBy(base::TimeDelta::FromSeconds(TEST_TIMEOUT_SECONDS + 1)); | |
| 471 PumpLoop(); | |
| 472 | |
| 473 // Wait for timeout to expire. Use a TaskRunner with a DelayedTaskRunner | |
| 474 // which won't time out immediately, so the watchdog thread doesn't kill valid | |
| 475 // tasks too soon. | |
| 476 WaitForCallback(); | |
| 477 PumpLoop(); | |
| 478 | |
| 479 // Now trying to start processing on another request should return false. | |
| 480 EXPECT_EQ(Offliner::RequestStatus::CANCELED, last_offlining_status()); | |
| 481 } | |
| 482 | |
| 419 } // namespace offline_pages | 483 } // namespace offline_pages |
| OLD | NEW |