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

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

Issue 2219393004: Adds an observer for the request coordinator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@resumeAPI
Patch Set: First round of feedback Created 4 years, 4 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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 if (offliner_.get() == nullptr) { 119 if (offliner_.get() == nullptr) {
120 offliner_.reset(new OfflinerStub()); 120 offliner_.reset(new OfflinerStub());
121 } 121 }
122 return offliner_.get(); 122 return offliner_.get();
123 } 123 }
124 124
125 private: 125 private:
126 std::unique_ptr<OfflinerStub> offliner_; 126 std::unique_ptr<OfflinerStub> offliner_;
127 }; 127 };
128 128
129 class ObserverStub : public RequestCoordinator::Observer {
130 public:
131 ObserverStub()
132 : added_called_(false),
133 succeeded_called_(false),
134 failed_called_(false),
135 changed_called_(false),
136 state_(SavePageRequest::RequestState::PRERENDERING) {}
137
138 void clear() {
fgorski 2016/08/10 21:30:45 I believe this should be Clear().
Pete Williamson 2016/08/11 00:08:37 Done.
139 added_called_ = false;
140 succeeded_called_ = false;
141 failed_called_ = false;
142 changed_called_ = false;
143 state_ = SavePageRequest::RequestState::PRERENDERING;
144 }
145
146 void OnAdded(const SavePageRequest& request) override {
147 added_called_ = true;
148 }
149
150 void OnSucceeded(
151 const SavePageRequest& request, int64_t offline_id) override {
152 succeeded_called_ = true;
153 }
154
155 void OnFailed(const SavePageRequest& request, int64_t failure_code) override {
156 failed_called_ = true;
157 }
158
159 void OnChanged(const SavePageRequest& request) override {
160 changed_called_ = true;
161 state_ = request.request_state();
162 }
163
164 bool added_called() { return added_called_; }
165 bool succeeded_called() { return succeeded_called_; }
166 bool failed_called() { return failed_called_; }
167 bool changed_called() { return changed_called_; }
168 SavePageRequest::RequestState state() { return state_; }
169
170 private:
171 bool added_called_;
172 bool succeeded_called_;
173 bool failed_called_;
174 bool changed_called_;
175 SavePageRequest::RequestState state_;
176 };
177
129 class RequestCoordinatorTest 178 class RequestCoordinatorTest
130 : public testing::Test { 179 : public testing::Test {
131 public: 180 public:
132 RequestCoordinatorTest(); 181 RequestCoordinatorTest();
133 ~RequestCoordinatorTest() override; 182 ~RequestCoordinatorTest() override;
134 183
135 void SetUp() override; 184 void SetUp() override;
136 185
137 void PumpLoop(); 186 void PumpLoop();
138 187
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 void AdvanceClockBy(base::TimeDelta delta) { 247 void AdvanceClockBy(base::TimeDelta delta) {
199 task_runner_->FastForwardBy(delta); 248 task_runner_->FastForwardBy(delta);
200 } 249 }
201 250
202 Offliner::RequestStatus last_offlining_status() const { 251 Offliner::RequestStatus last_offlining_status() const {
203 return coordinator_->last_offlining_status_; 252 return coordinator_->last_offlining_status_;
204 } 253 }
205 254
206 bool OfflinerWasCanceled() const { return offliner_->cancel_called(); } 255 bool OfflinerWasCanceled() const { return offliner_->cancel_called(); }
207 256
257 ObserverStub observer() { return observer_; }
258
208 private: 259 private:
209 RequestQueue::GetRequestsResult last_get_requests_result_; 260 RequestQueue::GetRequestsResult last_get_requests_result_;
210 std::vector<SavePageRequest> last_requests_; 261 std::vector<SavePageRequest> last_requests_;
211 std::vector<ClientId> last_client_ids_; 262 std::vector<ClientId> last_client_ids_;
212 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; 263 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
213 base::ThreadTaskRunnerHandle task_runner_handle_; 264 base::ThreadTaskRunnerHandle task_runner_handle_;
214 std::unique_ptr<RequestCoordinator> coordinator_; 265 std::unique_ptr<RequestCoordinator> coordinator_;
215 OfflinerStub* offliner_; 266 OfflinerStub* offliner_;
216 base::WaitableEvent waiter_; 267 base::WaitableEvent waiter_;
268 ObserverStub observer_;
217 }; 269 };
218 270
219 RequestCoordinatorTest::RequestCoordinatorTest() 271 RequestCoordinatorTest::RequestCoordinatorTest()
220 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE), 272 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE),
221 task_runner_(new base::TestMockTimeTaskRunner), 273 task_runner_(new base::TestMockTimeTaskRunner),
222 task_runner_handle_(task_runner_), 274 task_runner_handle_(task_runner_),
223 offliner_(nullptr), 275 offliner_(nullptr),
224 waiter_(base::WaitableEvent::ResetPolicy::MANUAL, 276 waiter_(base::WaitableEvent::ResetPolicy::MANUAL,
225 base::WaitableEvent::InitialState::NOT_SIGNALED) {} 277 base::WaitableEvent::InitialState::NOT_SIGNALED) {}
226 278
227 RequestCoordinatorTest::~RequestCoordinatorTest() {} 279 RequestCoordinatorTest::~RequestCoordinatorTest() {}
228 280
229 void RequestCoordinatorTest::SetUp() { 281 void RequestCoordinatorTest::SetUp() {
230 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); 282 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy());
231 std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub()); 283 std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub());
232 // Save the offliner for use by the tests. 284 // Save the offliner for use by the tests.
233 offliner_ = 285 offliner_ =
234 reinterpret_cast<OfflinerStub*>(factory->GetOffliner(policy.get())); 286 reinterpret_cast<OfflinerStub*>(factory->GetOffliner(policy.get()));
235 std::unique_ptr<RequestQueueInMemoryStore> 287 std::unique_ptr<RequestQueueInMemoryStore>
236 store(new RequestQueueInMemoryStore()); 288 store(new RequestQueueInMemoryStore());
237 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store))); 289 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store)));
238 std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub()); 290 std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub());
239 coordinator_.reset(new RequestCoordinator( 291 coordinator_.reset(new RequestCoordinator(
240 std::move(policy), std::move(factory), std::move(queue), 292 std::move(policy), std::move(factory), std::move(queue),
241 std::move(scheduler_stub))); 293 std::move(scheduler_stub)));
294 coordinator_->AddObserver(&observer_);
242 } 295 }
243 296
244 void RequestCoordinatorTest::PumpLoop() { 297 void RequestCoordinatorTest::PumpLoop() {
245 task_runner_->RunUntilIdle(); 298 task_runner_->RunUntilIdle();
246 } 299 }
247 300
248 void RequestCoordinatorTest::GetRequestsDone( 301 void RequestCoordinatorTest::GetRequestsDone(
249 RequestQueue::GetRequestsResult result, 302 RequestQueue::GetRequestsResult result,
250 const std::vector<SavePageRequest>& requests) { 303 const std::vector<SavePageRequest>& requests) {
251 last_get_requests_result_ = result; 304 last_get_requests_result_ = result;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 EXPECT_EQ(kClientId1, last_requests()[0].client_id()); 372 EXPECT_EQ(kClientId1, last_requests()[0].client_id());
320 373
321 // Expect that the scheduler got notified. 374 // Expect that the scheduler got notified.
322 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( 375 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>(
323 coordinator()->scheduler()); 376 coordinator()->scheduler());
324 EXPECT_TRUE(scheduler_stub->schedule_called()); 377 EXPECT_TRUE(scheduler_stub->schedule_called());
325 EXPECT_EQ(coordinator() 378 EXPECT_EQ(coordinator()
326 ->GetTriggerConditionsForUserRequest() 379 ->GetTriggerConditionsForUserRequest()
327 .minimum_battery_percentage, 380 .minimum_battery_percentage,
328 scheduler_stub->conditions()->minimum_battery_percentage); 381 scheduler_stub->conditions()->minimum_battery_percentage);
382
383 // Check that the observer got the notification that a page is available
384 EXPECT_EQ(true, observer().added_called());
fgorski 2016/08/10 21:30:44 EXPECT_TRUE and applies everywhere you do EXPECT_E
Pete Williamson 2016/08/11 00:08:36 Done everywhere.
329 } 385 }
330 386
331 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { 387 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) {
332 // Add a request to the queue, wait for callbacks to finish. 388 // Add a request to the queue, wait for callbacks to finish.
333 offline_pages::SavePageRequest request( 389 offline_pages::SavePageRequest request(
334 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); 390 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested);
335 request.MarkAttemptStarted(base::Time::Now()); 391 request.MarkAttemptStarted(base::Time::Now());
336 coordinator()->queue()->AddRequest( 392 coordinator()->queue()->AddRequest(
337 request, 393 request,
338 base::Bind(&RequestCoordinatorTest::AddRequestDone, 394 base::Bind(&RequestCoordinatorTest::AddRequestDone,
(...skipping 21 matching lines...) Expand all
360 // Verify the request gets removed from the queue, and wait for callbacks. 416 // Verify the request gets removed from the queue, and wait for callbacks.
361 coordinator()->queue()->GetRequests( 417 coordinator()->queue()->GetRequests(
362 base::Bind(&RequestCoordinatorTest::GetRequestsDone, 418 base::Bind(&RequestCoordinatorTest::GetRequestsDone,
363 base::Unretained(this))); 419 base::Unretained(this)));
364 PumpLoop(); 420 PumpLoop();
365 421
366 // We should not find any requests in the queue anymore. 422 // We should not find any requests in the queue anymore.
367 // RequestPicker should *not* have tried to start an additional job, 423 // RequestPicker should *not* have tried to start an additional job,
368 // because the request queue is empty now. 424 // because the request queue is empty now.
369 EXPECT_EQ(0UL, last_requests().size()); 425 EXPECT_EQ(0UL, last_requests().size());
426 // Check that the observer got the notification that we succeeded.
427 EXPECT_EQ(true, observer().succeeded_called());
370 } 428 }
371 429
372 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { 430 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) {
373 // Add a request to the queue, wait for callbacks to finish. 431 // Add a request to the queue, wait for callbacks to finish.
374 offline_pages::SavePageRequest request( 432 offline_pages::SavePageRequest request(
375 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); 433 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested);
376 request.MarkAttemptStarted(base::Time::Now()); 434 request.MarkAttemptStarted(base::Time::Now());
377 coordinator()->queue()->AddRequest( 435 coordinator()->queue()->AddRequest(
378 request, 436 request,
379 base::Bind(&RequestCoordinatorTest::AddRequestDone, 437 base::Bind(&RequestCoordinatorTest::AddRequestDone,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 472
415 // Verify neither request is removed from the queue; wait for callbacks. 473 // Verify neither request is removed from the queue; wait for callbacks.
416 coordinator()->queue()->GetRequests( 474 coordinator()->queue()->GetRequests(
417 base::Bind(&RequestCoordinatorTest::GetRequestsDone, 475 base::Bind(&RequestCoordinatorTest::GetRequestsDone,
418 base::Unretained(this))); 476 base::Unretained(this)));
419 PumpLoop(); 477 PumpLoop();
420 478
421 // Now just one request in the queue since failed request removed 479 // Now just one request in the queue since failed request removed
422 // (for single attempt policy). 480 // (for single attempt policy).
423 EXPECT_EQ(1UL, last_requests().size()); 481 EXPECT_EQ(1UL, last_requests().size());
482 // Check that the observer got the notification that we failed
483 EXPECT_EQ(true, observer().failed_called());
424 } 484 }
425 485
426 TEST_F(RequestCoordinatorTest, OfflinerDoneForegroundCancel) { 486 TEST_F(RequestCoordinatorTest, OfflinerDoneForegroundCancel) {
427 // Add a request to the queue, wait for callbacks to finish. 487 // Add a request to the queue, wait for callbacks to finish.
428 offline_pages::SavePageRequest request( 488 offline_pages::SavePageRequest request(
429 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); 489 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested);
430 request.MarkAttemptStarted(base::Time::Now()); 490 request.MarkAttemptStarted(base::Time::Now());
431 coordinator()->queue()->AddRequest( 491 coordinator()->queue()->AddRequest(
432 request, base::Bind(&RequestCoordinatorTest::AddRequestDone, 492 request, base::Bind(&RequestCoordinatorTest::AddRequestDone,
433 base::Unretained(this))); 493 base::Unretained(this)));
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
652 // Wait for async get to finish. 712 // Wait for async get to finish.
653 WaitForCallback(); 713 WaitForCallback();
654 PumpLoop(); 714 PumpLoop();
655 715
656 // Check that the statuses found in the callback match what we expect. 716 // Check that the statuses found in the callback match what we expect.
657 EXPECT_EQ(2UL, last_client_ids().size()); 717 EXPECT_EQ(2UL, last_client_ids().size());
658 EXPECT_EQ(kId1, last_client_ids().at(0).id); 718 EXPECT_EQ(kId1, last_client_ids().at(0).id);
659 EXPECT_EQ(kId2, last_client_ids().at(1).id); 719 EXPECT_EQ(kId2, last_client_ids().at(1).id);
660 } 720 }
661 721
722 TEST_F(RequestCoordinatorTest, PauseAndResumeObserver) {
723 // Add a request to the queue.
724 offline_pages::SavePageRequest request1(kRequestId1, kUrl1, kClientId1,
725 base::Time::Now(), kUserRequested);
726 coordinator()->queue()->AddRequest(
727 request1, base::Bind(&RequestCoordinatorTest::AddRequestDone,
728 base::Unretained(this)));
729 PumpLoop();
730
731 // Pause the request.
732 std::vector<int64_t> request_ids;
733 request_ids.push_back(kRequestId1);
734 coordinator()->PauseRequests(request_ids);
735 PumpLoop();
736
737 EXPECT_EQ(true, observer().changed_called());
738 EXPECT_EQ(SavePageRequest::RequestState::PAUSED, observer().state());
739
740 // Clear out the observer before the next call.
741 observer().clear();
742
743 // Resume the request.
744 coordinator()->ResumeRequests(request_ids);
745 PumpLoop();
746
747 EXPECT_EQ(true, observer().changed_called());
748 EXPECT_EQ(SavePageRequest::RequestState::AVAILABLE, observer().state());
749 }
750
662 } // namespace offline_pages 751 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698