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

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

Issue 2113383002: More detailed implementation of the RequestPicker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Try a different way of checking policy to separate policy from checking. Created 4 years, 5 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 17 matching lines...) Expand all
28 namespace offline_pages { 28 namespace offline_pages {
29 29
30 namespace { 30 namespace {
31 // put test constants here 31 // put test constants here
32 const GURL kUrl("http://universe.com/everything"); 32 const GURL kUrl("http://universe.com/everything");
33 const ClientId kClientId("bookmark", "42"); 33 const ClientId kClientId("bookmark", "42");
34 const int kRequestId(1); 34 const int kRequestId(1);
35 const long kTestTimeoutSeconds = 1; 35 const long kTestTimeoutSeconds = 1;
36 const int kBatteryPercentageHigh = 75; 36 const int kBatteryPercentageHigh = 75;
37 const bool kPowerRequired = true; 37 const bool kPowerRequired = true;
38 const bool kUserRequested = true;
38 } // namespace 39 } // namespace
39 40
40 class SchedulerStub : public Scheduler { 41 class SchedulerStub : public Scheduler {
41 public: 42 public:
42 SchedulerStub() 43 SchedulerStub()
43 : schedule_called_(false), 44 : schedule_called_(false),
44 unschedule_called_(false), 45 unschedule_called_(false),
45 conditions_(false, 0, false) {} 46 conditions_(false, 0, false) {}
46 47
47 void Schedule(const TriggerConditions& trigger_conditions) override { 48 void Schedule(const TriggerConditions& trigger_conditions) override {
(...skipping 14 matching lines...) Expand all
62 63
63 private: 64 private:
64 bool schedule_called_; 65 bool schedule_called_;
65 bool unschedule_called_; 66 bool unschedule_called_;
66 TriggerConditions conditions_; 67 TriggerConditions conditions_;
67 }; 68 };
68 69
69 class OfflinerStub : public Offliner { 70 class OfflinerStub : public Offliner {
70 public: 71 public:
71 OfflinerStub() 72 OfflinerStub()
72 : request_(kRequestId, kUrl, kClientId, base::Time::Now()), 73 : request_(kRequestId, kUrl, kClientId, base::Time::Now(),
74 kUserRequested),
73 enable_callback_(false), 75 enable_callback_(false),
74 cancel_called_(false) {} 76 cancel_called_(false) {}
75 77
76 bool LoadAndSave(const SavePageRequest& request, 78 bool LoadAndSave(const SavePageRequest& request,
77 const CompletionCallback& callback) override { 79 const CompletionCallback& callback) override {
78 callback_ = callback; 80 callback_ = callback;
79 request_ = request; 81 request_ = request;
80 // Post the callback on the run loop. 82 // Post the callback on the run loop.
81 if (enable_callback_) { 83 if (enable_callback_) {
82 base::ThreadTaskRunnerHandle::Get()->PostTask( 84 base::ThreadTaskRunnerHandle::Get()->PostTask(
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 } 165 }
164 166
165 void EnableOfflinerCallback(bool enable) { 167 void EnableOfflinerCallback(bool enable) {
166 offliner_->enable_callback(enable); 168 offliner_->enable_callback(enable);
167 } 169 }
168 170
169 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) { 171 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) {
170 coordinator_->SetOfflinerTimeoutForTest(timeout); 172 coordinator_->SetOfflinerTimeoutForTest(timeout);
171 } 173 }
172 174
175 void SetDeviceConditionsForTest(DeviceConditions device_conditions) {
176 coordinator_->SetDeviceConditionsForTest(device_conditions);
177 }
178
173 void WaitForCallback() { 179 void WaitForCallback() {
174 waiter_.Wait(); 180 waiter_.Wait();
175 } 181 }
176 182
177 void AdvanceClockBy(base::TimeDelta delta) { 183 void AdvanceClockBy(base::TimeDelta delta) {
178 task_runner_->FastForwardBy(delta); 184 task_runner_->FastForwardBy(delta);
179 } 185 }
180 186
181 Offliner::RequestStatus last_offlining_status() const { 187 Offliner::RequestStatus last_offlining_status() const {
182 return coordinator_->last_offlining_status_; 188 return coordinator_->last_offlining_status_;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 net::NetworkChangeNotifier::CONNECTION_3G); 251 net::NetworkChangeNotifier::CONNECTION_3G);
246 base::Callback<void(bool)> callback = 252 base::Callback<void(bool)> callback =
247 base::Bind( 253 base::Bind(
248 &RequestCoordinatorTest::EmptyCallbackFunction, 254 &RequestCoordinatorTest::EmptyCallbackFunction,
249 base::Unretained(this)); 255 base::Unretained(this));
250 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); 256 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback));
251 } 257 }
252 258
253 TEST_F(RequestCoordinatorTest, StartProcessingWithRequestInProgress) { 259 TEST_F(RequestCoordinatorTest, StartProcessingWithRequestInProgress) {
254 // Put the request on the queue. 260 // Put the request on the queue.
255 EXPECT_TRUE(coordinator()->SavePageLater(kUrl, kClientId)); 261 EXPECT_TRUE(coordinator()->SavePageLater(kUrl, kClientId, kUserRequested));
256 262
257 // Set up for the call to StartProcessing by building arguments. 263 // Set up for the call to StartProcessing by building arguments.
258 DeviceConditions device_conditions(false, 75, 264 DeviceConditions device_conditions(false, 75,
259 net::NetworkChangeNotifier::CONNECTION_3G); 265 net::NetworkChangeNotifier::CONNECTION_3G);
260 base::Callback<void(bool)> callback = 266 base::Callback<void(bool)> callback =
261 base::Bind(&RequestCoordinatorTest::EmptyCallbackFunction, 267 base::Bind(&RequestCoordinatorTest::EmptyCallbackFunction,
262 base::Unretained(this)); 268 base::Unretained(this));
263 269
264 // Ensure that the forthcoming request does not finish - we simulate it being 270 // Ensure that the forthcoming request does not finish - we simulate it being
265 // in progress by asking it to skip making the completion callback. 271 // in progress by asking it to skip making the completion callback.
266 EnableOfflinerCallback(false); 272 EnableOfflinerCallback(false);
267 273
268 // Sending the request to the offliner should make it busy. 274 // Sending the request to the offliner should make it busy.
269 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); 275 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback));
270 PumpLoop(); 276 PumpLoop();
271 EXPECT_TRUE(is_busy()); 277 EXPECT_TRUE(is_busy());
272 278
273 // Now trying to start processing on another request should return false. 279 // Now trying to start processing on another request should return false.
274 EXPECT_FALSE(coordinator()->StartProcessing(device_conditions, callback)); 280 EXPECT_FALSE(coordinator()->StartProcessing(device_conditions, callback));
275 } 281 }
276 282
277 TEST_F(RequestCoordinatorTest, SavePageLater) { 283 TEST_F(RequestCoordinatorTest, SavePageLater) {
278 EXPECT_TRUE(coordinator()->SavePageLater(kUrl, kClientId)); 284 EXPECT_TRUE(coordinator()->SavePageLater(kUrl, kClientId, kUserRequested));
279 285
280 // Expect that a request got placed on the queue. 286 // Expect that a request got placed on the queue.
281 coordinator()->queue()->GetRequests( 287 coordinator()->queue()->GetRequests(
282 base::Bind(&RequestCoordinatorTest::GetRequestsDone, 288 base::Bind(&RequestCoordinatorTest::GetRequestsDone,
283 base::Unretained(this))); 289 base::Unretained(this)));
284 290
285 // Wait for callbacks to finish, both request queue and offliner. 291 // Wait for callbacks to finish, both request queue and offliner.
286 PumpLoop(); 292 PumpLoop();
287 293
288 // Check the request queue is as expected. 294 // Check the request queue is as expected.
289 EXPECT_EQ(1UL, last_requests().size()); 295 EXPECT_EQ(1UL, last_requests().size());
290 EXPECT_EQ(kUrl, last_requests()[0].url()); 296 EXPECT_EQ(kUrl, last_requests()[0].url());
291 EXPECT_EQ(kClientId, last_requests()[0].client_id()); 297 EXPECT_EQ(kClientId, last_requests()[0].client_id());
292 298
293 // Expect that the scheduler got notified. 299 // Expect that the scheduler got notified.
294 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>( 300 SchedulerStub* scheduler_stub = reinterpret_cast<SchedulerStub*>(
295 coordinator()->scheduler()); 301 coordinator()->scheduler());
296 EXPECT_TRUE(scheduler_stub->schedule_called()); 302 EXPECT_TRUE(scheduler_stub->schedule_called());
297 EXPECT_EQ(coordinator() 303 EXPECT_EQ(coordinator()
298 ->GetTriggerConditionsForUserRequest() 304 ->GetTriggerConditionsForUserRequest()
299 .minimum_battery_percentage, 305 .minimum_battery_percentage,
300 scheduler_stub->conditions()->minimum_battery_percentage); 306 scheduler_stub->conditions()->minimum_battery_percentage);
301 } 307 }
302 308
303 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) { 309 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestSucceeded) {
304 // Add a request to the queue, wait for callbacks to finish. 310 // Add a request to the queue, wait for callbacks to finish.
305 offline_pages::SavePageRequest request( 311 offline_pages::SavePageRequest request(
306 kRequestId, kUrl, kClientId, base::Time::Now()); 312 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested);
307 coordinator()->queue()->AddRequest( 313 coordinator()->queue()->AddRequest(
308 request, 314 request,
309 base::Bind(&RequestCoordinatorTest::AddRequestDone, 315 base::Bind(&RequestCoordinatorTest::AddRequestDone,
310 base::Unretained(this))); 316 base::Unretained(this)));
311 PumpLoop(); 317 PumpLoop();
312 318
313 // We need to give a callback to the request. 319 // We need to give a callback to the request.
314 base::Callback<void(bool)> callback = 320 base::Callback<void(bool)> callback =
315 base::Bind( 321 base::Bind(
316 &RequestCoordinatorTest::EmptyCallbackFunction, 322 &RequestCoordinatorTest::EmptyCallbackFunction,
317 base::Unretained(this)); 323 base::Unretained(this));
318 coordinator()->SetProcessingCallbackForTest(callback); 324 coordinator()->SetProcessingCallbackForTest(callback);
319 325
326 // Set up device conditions for the test.
327 DeviceConditions device_conditions(
328 false, 75, net::NetworkChangeNotifier::CONNECTION_3G);
329 SetDeviceConditionsForTest(device_conditions);
330
320 // Call the OfflinerDoneCallback to simulate the page being completed, wait 331 // Call the OfflinerDoneCallback to simulate the page being completed, wait
321 // for callbacks. 332 // for callbacks.
322 EnableOfflinerCallback(true); 333 EnableOfflinerCallback(true);
323 SendOfflinerDoneCallback(request, Offliner::RequestStatus::SAVED); 334 SendOfflinerDoneCallback(request, Offliner::RequestStatus::SAVED);
324 PumpLoop(); 335 PumpLoop();
325 336
326 // Verify the request gets removed from the queue, and wait for callbacks. 337 // Verify the request gets removed from the queue, and wait for callbacks.
327 coordinator()->queue()->GetRequests( 338 coordinator()->queue()->GetRequests(
328 base::Bind(&RequestCoordinatorTest::GetRequestsDone, 339 base::Bind(&RequestCoordinatorTest::GetRequestsDone,
329 base::Unretained(this))); 340 base::Unretained(this)));
330 PumpLoop(); 341 PumpLoop();
331 342
332 // We should not find any requests in the queue anymore. 343 // We should not find any requests in the queue anymore.
333 // RequestPicker should *not* have tried to start an additional job, 344 // RequestPicker should *not* have tried to start an additional job,
334 // because the request queue is empty now. 345 // because the request queue is empty now.
335 EXPECT_EQ(0UL, last_requests().size()); 346 EXPECT_EQ(0UL, last_requests().size());
336 } 347 }
337 348
338 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) { 349 TEST_F(RequestCoordinatorTest, OfflinerDoneRequestFailed) {
339 // Add a request to the queue, wait for callbacks to finish. 350 // Add a request to the queue, wait for callbacks to finish.
340 offline_pages::SavePageRequest request( 351 offline_pages::SavePageRequest request(
341 kRequestId, kUrl, kClientId, base::Time::Now()); 352 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested);
342 coordinator()->queue()->AddRequest( 353 coordinator()->queue()->AddRequest(
343 request, 354 request,
344 base::Bind(&RequestCoordinatorTest::AddRequestDone, 355 base::Bind(&RequestCoordinatorTest::AddRequestDone,
345 base::Unretained(this))); 356 base::Unretained(this)));
346 PumpLoop(); 357 PumpLoop();
347 358
348 // We need to give a callback to the request. 359 // We need to give a callback to the request.
349 base::Callback<void(bool)> callback = 360 base::Callback<void(bool)> callback =
350 base::Bind( 361 base::Bind(
351 &RequestCoordinatorTest::EmptyCallbackFunction, 362 &RequestCoordinatorTest::EmptyCallbackFunction,
(...skipping 16 matching lines...) Expand all
368 // Still one request in the queue. 379 // Still one request in the queue.
369 EXPECT_EQ(1UL, last_requests().size()); 380 EXPECT_EQ(1UL, last_requests().size());
370 // TODO(dougarnett): Verify retry count gets incremented. 381 // TODO(dougarnett): Verify retry count gets incremented.
371 } 382 }
372 383
373 // This tests a StopProcessing call before we have actually started the 384 // This tests a StopProcessing call before we have actually started the
374 // prerenderer. 385 // prerenderer.
375 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingImmediately) { 386 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingImmediately) {
376 // Add a request to the queue, wait for callbacks to finish. 387 // Add a request to the queue, wait for callbacks to finish.
377 offline_pages::SavePageRequest request( 388 offline_pages::SavePageRequest request(
378 kRequestId, kUrl, kClientId, base::Time::Now()); 389 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested);
379 coordinator()->queue()->AddRequest( 390 coordinator()->queue()->AddRequest(
380 request, 391 request,
381 base::Bind(&RequestCoordinatorTest::AddRequestDone, 392 base::Bind(&RequestCoordinatorTest::AddRequestDone,
382 base::Unretained(this))); 393 base::Unretained(this)));
383 PumpLoop(); 394 PumpLoop();
384 395
385 DeviceConditions device_conditions(false, 75, 396 DeviceConditions device_conditions(false, 75,
386 net::NetworkChangeNotifier::CONNECTION_3G); 397 net::NetworkChangeNotifier::CONNECTION_3G);
387 base::Callback<void(bool)> callback = 398 base::Callback<void(bool)> callback =
388 base::Bind( 399 base::Bind(
(...skipping 13 matching lines...) Expand all
402 last_offlining_status()); 413 last_offlining_status());
403 414
404 // Since offliner was not started, it will not have seen cancel call. 415 // Since offliner was not started, it will not have seen cancel call.
405 EXPECT_FALSE(OfflinerWasCanceled()); 416 EXPECT_FALSE(OfflinerWasCanceled());
406 } 417 }
407 418
408 // This tests a StopProcessing call after the prerenderer has been started. 419 // This tests a StopProcessing call after the prerenderer has been started.
409 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingLater) { 420 TEST_F(RequestCoordinatorTest, StartProcessingThenStopProcessingLater) {
410 // Add a request to the queue, wait for callbacks to finish. 421 // Add a request to the queue, wait for callbacks to finish.
411 offline_pages::SavePageRequest request( 422 offline_pages::SavePageRequest request(
412 kRequestId, kUrl, kClientId, base::Time::Now()); 423 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested);
413 coordinator()->queue()->AddRequest( 424 coordinator()->queue()->AddRequest(
414 request, 425 request,
415 base::Bind(&RequestCoordinatorTest::AddRequestDone, 426 base::Bind(&RequestCoordinatorTest::AddRequestDone,
416 base::Unretained(this))); 427 base::Unretained(this)));
417 PumpLoop(); 428 PumpLoop();
418 429
419 // Ensure the start processing request stops before the completion callback. 430 // Ensure the start processing request stops before the completion callback.
420 EnableOfflinerCallback(false); 431 EnableOfflinerCallback(false);
421 432
422 DeviceConditions device_conditions(false, 75, 433 DeviceConditions device_conditions(false, 75,
(...skipping 18 matching lines...) Expand all
441 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, 452 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED,
442 last_offlining_status()); 453 last_offlining_status());
443 454
444 // Since offliner was started, it will have seen cancel call. 455 // Since offliner was started, it will have seen cancel call.
445 EXPECT_TRUE(OfflinerWasCanceled()); 456 EXPECT_TRUE(OfflinerWasCanceled());
446 } 457 }
447 458
448 TEST_F(RequestCoordinatorTest, PrerendererTimeout) { 459 TEST_F(RequestCoordinatorTest, PrerendererTimeout) {
449 // Build a request to use with the pre-renderer, and put it on the queue. 460 // Build a request to use with the pre-renderer, and put it on the queue.
450 offline_pages::SavePageRequest request( 461 offline_pages::SavePageRequest request(
451 kRequestId, kUrl, kClientId, base::Time::Now()); 462 kRequestId, kUrl, kClientId, base::Time::Now(), kUserRequested);
452 coordinator()->queue()->AddRequest( 463 coordinator()->queue()->AddRequest(
453 request, 464 request,
454 base::Bind(&RequestCoordinatorTest::AddRequestDone, 465 base::Bind(&RequestCoordinatorTest::AddRequestDone,
455 base::Unretained(this))); 466 base::Unretained(this)));
456 PumpLoop(); 467 PumpLoop();
457 468
458 // Set up for the call to StartProcessing. 469 // Set up for the call to StartProcessing.
459 DeviceConditions device_conditions( 470 DeviceConditions device_conditions(
460 !kPowerRequired, kBatteryPercentageHigh, 471 !kPowerRequired, kBatteryPercentageHigh,
461 net::NetworkChangeNotifier::CONNECTION_3G); 472 net::NetworkChangeNotifier::CONNECTION_3G);
(...skipping 22 matching lines...) Expand all
484 // tasks too soon. 495 // tasks too soon.
485 WaitForCallback(); 496 WaitForCallback();
486 PumpLoop(); 497 PumpLoop();
487 498
488 EXPECT_TRUE(OfflinerWasCanceled()); 499 EXPECT_TRUE(OfflinerWasCanceled());
489 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, 500 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED,
490 last_offlining_status()); 501 last_offlining_status());
491 } 502 }
492 503
493 } // namespace offline_pages 504 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698