| 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 "chrome/browser/android/offline_pages/prerendering_offliner.h" | 5 #include "chrome/browser/android/offline_pages/prerendering_offliner.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 MockOfflinePageModel* model() { return model_; } | 200 MockOfflinePageModel* model() { return model_; } |
| 201 bool completion_callback_called() { return completion_callback_called_; } | 201 bool completion_callback_called() { return completion_callback_called_; } |
| 202 Offliner::RequestStatus request_status() { return request_status_; } | 202 Offliner::RequestStatus request_status() { return request_status_; } |
| 203 OfflinerPolicy* policy() { return policy_; } | 203 OfflinerPolicy* policy() { return policy_; } |
| 204 bool cancel_callback_called() { return cancel_callback_called_; } | 204 bool cancel_callback_called() { return cancel_callback_called_; } |
| 205 | 205 |
| 206 private: | 206 private: |
| 207 void OnCompletion(const SavePageRequest& request, | 207 void OnCompletion(const SavePageRequest& request, |
| 208 Offliner::RequestStatus status); | 208 Offliner::RequestStatus status); |
| 209 void OnProgress(const SavePageRequest& request, int64_t bytes); | 209 void OnProgress(const SavePageRequest& request, int64_t bytes); |
| 210 void OnCancel(int64_t offline_id); | 210 void OnCancel(const SavePageRequest& request); |
| 211 | 211 |
| 212 content::TestBrowserThreadBundle thread_bundle_; | 212 content::TestBrowserThreadBundle thread_bundle_; |
| 213 TestingProfile profile_; | 213 TestingProfile profile_; |
| 214 std::unique_ptr<PrerenderingOffliner> offliner_; | 214 std::unique_ptr<PrerenderingOffliner> offliner_; |
| 215 // Not owned. | 215 // Not owned. |
| 216 MockPrerenderingLoader* loader_; | 216 MockPrerenderingLoader* loader_; |
| 217 MockOfflinePageModel* model_; | 217 MockOfflinePageModel* model_; |
| 218 bool completion_callback_called_; | 218 bool completion_callback_called_; |
| 219 bool cancel_callback_called_; | 219 bool cancel_callback_called_; |
| 220 Offliner::RequestStatus request_status_; | 220 Offliner::RequestStatus request_status_; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 244 void PrerenderingOfflinerTest::OnCompletion(const SavePageRequest& request, | 244 void PrerenderingOfflinerTest::OnCompletion(const SavePageRequest& request, |
| 245 Offliner::RequestStatus status) { | 245 Offliner::RequestStatus status) { |
| 246 DCHECK(!completion_callback_called_); // Expect single callback per request. | 246 DCHECK(!completion_callback_called_); // Expect single callback per request. |
| 247 completion_callback_called_ = true; | 247 completion_callback_called_ = true; |
| 248 request_status_ = status; | 248 request_status_ = status; |
| 249 } | 249 } |
| 250 | 250 |
| 251 void PrerenderingOfflinerTest::OnProgress(const SavePageRequest& request, | 251 void PrerenderingOfflinerTest::OnProgress(const SavePageRequest& request, |
| 252 int64_t bytes) {} | 252 int64_t bytes) {} |
| 253 | 253 |
| 254 void PrerenderingOfflinerTest::OnCancel(int64_t offline_id) { | 254 void PrerenderingOfflinerTest::OnCancel(const SavePageRequest& request) { |
| 255 DCHECK(!cancel_callback_called_); | 255 DCHECK(!cancel_callback_called_); |
| 256 cancel_callback_called_ = true; | 256 cancel_callback_called_ = true; |
| 257 } | 257 } |
| 258 | 258 |
| 259 TEST_F(PrerenderingOfflinerTest, DISABLED_LoadAndSaveBadUrl) { | 259 TEST_F(PrerenderingOfflinerTest, DISABLED_LoadAndSaveBadUrl) { |
| 260 base::Time creation_time = base::Time::Now(); | 260 base::Time creation_time = base::Time::Now(); |
| 261 SavePageRequest request( | 261 SavePageRequest request( |
| 262 kRequestId, kFileUrl, kClientId, creation_time, kUserRequested); | 262 kRequestId, kFileUrl, kClientId, creation_time, kUserRequested); |
| 263 EXPECT_FALSE(offliner()->LoadAndSave(request, completion_callback(), | 263 EXPECT_FALSE(offliner()->LoadAndSave(request, completion_callback(), |
| 264 progress_callback())); | 264 progress_callback())); |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 TEST_F(PrerenderingOfflinerTest, HandleTimeoutWithLowbarAndCompletedTriesMet) { | 474 TEST_F(PrerenderingOfflinerTest, HandleTimeoutWithLowbarAndCompletedTriesMet) { |
| 475 offliner()->SetLowEndDeviceForTesting(false); | 475 offliner()->SetLowEndDeviceForTesting(false); |
| 476 | 476 |
| 477 base::Time creation_time = base::Time::Now(); | 477 base::Time creation_time = base::Time::Now(); |
| 478 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 478 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 479 kUserRequested); | 479 kUserRequested); |
| 480 request.set_completed_attempt_count(policy()->GetMaxCompletedTries() - 1); | 480 request.set_completed_attempt_count(policy()->GetMaxCompletedTries() - 1); |
| 481 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 481 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 482 progress_callback())); | 482 progress_callback())); |
| 483 loader()->set_is_lowbar_met(true); | 483 loader()->set_is_lowbar_met(true); |
| 484 EXPECT_TRUE(offliner()->HandleTimeout(request)); | 484 EXPECT_TRUE(offliner()->HandleTimeout(kRequestId)); |
| 485 EXPECT_TRUE(loader()->start_snapshot_called()); | 485 EXPECT_TRUE(loader()->start_snapshot_called()); |
| 486 PumpLoop(); | 486 PumpLoop(); |
| 487 // EXPECT_TRUE(SaveInProgress()); | 487 // EXPECT_TRUE(SaveInProgress()); |
| 488 model()->CompleteSavingAsSuccess(); | 488 model()->CompleteSavingAsSuccess(); |
| 489 PumpLoop(); | 489 PumpLoop(); |
| 490 EXPECT_TRUE(completion_callback_called()); | 490 EXPECT_TRUE(completion_callback_called()); |
| 491 EXPECT_EQ(Offliner::RequestStatus::SAVED_ON_LAST_RETRY, request_status()); | 491 EXPECT_EQ(Offliner::RequestStatus::SAVED_ON_LAST_RETRY, request_status()); |
| 492 } | 492 } |
| 493 | 493 |
| 494 TEST_F(PrerenderingOfflinerTest, | 494 TEST_F(PrerenderingOfflinerTest, |
| 495 HandleTimeoutWithLowbarAndCompletedTriesMetLowEndDevice) { | 495 HandleTimeoutWithLowbarAndCompletedTriesMetLowEndDevice) { |
| 496 offliner()->SetLowEndDeviceForTesting(true); | 496 offliner()->SetLowEndDeviceForTesting(true); |
| 497 | 497 |
| 498 base::Time creation_time = base::Time::Now(); | 498 base::Time creation_time = base::Time::Now(); |
| 499 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 499 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 500 kUserRequested); | 500 kUserRequested); |
| 501 request.set_completed_attempt_count(policy()->GetMaxCompletedTries() - 1); | 501 request.set_completed_attempt_count(policy()->GetMaxCompletedTries() - 1); |
| 502 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 502 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 503 progress_callback())); | 503 progress_callback())); |
| 504 loader()->set_is_lowbar_met(true); | 504 loader()->set_is_lowbar_met(true); |
| 505 EXPECT_TRUE(offliner()->HandleTimeout(request)); | 505 EXPECT_TRUE(offliner()->HandleTimeout(kRequestId)); |
| 506 EXPECT_TRUE(loader()->start_snapshot_called()); | 506 EXPECT_TRUE(loader()->start_snapshot_called()); |
| 507 } | 507 } |
| 508 | 508 |
| 509 TEST_F(PrerenderingOfflinerTest, | 509 TEST_F(PrerenderingOfflinerTest, |
| 510 DISABLED_HandleTimeoutCompletedTriesMetWithoutLowbarMet) { | 510 DISABLED_HandleTimeoutCompletedTriesMetWithoutLowbarMet) { |
| 511 offliner()->SetLowEndDeviceForTesting(false); | 511 offliner()->SetLowEndDeviceForTesting(false); |
| 512 | 512 |
| 513 base::Time creation_time = base::Time::Now(); | 513 base::Time creation_time = base::Time::Now(); |
| 514 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 514 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 515 kUserRequested); | 515 kUserRequested); |
| 516 request.set_completed_attempt_count(policy()->GetMaxCompletedTries() - 1); | 516 request.set_completed_attempt_count(policy()->GetMaxCompletedTries() - 1); |
| 517 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 517 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 518 progress_callback())); | 518 progress_callback())); |
| 519 loader()->set_is_lowbar_met(false); | 519 loader()->set_is_lowbar_met(false); |
| 520 EXPECT_FALSE(offliner()->HandleTimeout(request)); | 520 EXPECT_FALSE(offliner()->HandleTimeout(kRequestId)); |
| 521 EXPECT_FALSE(loader()->start_snapshot_called()); | 521 EXPECT_FALSE(loader()->start_snapshot_called()); |
| 522 } | 522 } |
| 523 | 523 |
| 524 TEST_F(PrerenderingOfflinerTest, | 524 TEST_F(PrerenderingOfflinerTest, |
| 525 DISABLED_HandleTimeoutStartedTriesMetWithoutLowbarMet) { | 525 DISABLED_HandleTimeoutStartedTriesMetWithoutLowbarMet) { |
| 526 offliner()->SetLowEndDeviceForTesting(false); | 526 offliner()->SetLowEndDeviceForTesting(false); |
| 527 | 527 |
| 528 base::Time creation_time = base::Time::Now(); | 528 base::Time creation_time = base::Time::Now(); |
| 529 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 529 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 530 kUserRequested); | 530 kUserRequested); |
| 531 request.set_started_attempt_count(policy()->GetMaxStartedTries() - 1); | 531 request.set_started_attempt_count(policy()->GetMaxStartedTries() - 1); |
| 532 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 532 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 533 progress_callback())); | 533 progress_callback())); |
| 534 loader()->set_is_lowbar_met(false); | 534 loader()->set_is_lowbar_met(false); |
| 535 EXPECT_FALSE(offliner()->HandleTimeout(request)); | 535 EXPECT_FALSE(offliner()->HandleTimeout(kRequestId)); |
| 536 EXPECT_FALSE(loader()->start_snapshot_called()); | 536 EXPECT_FALSE(loader()->start_snapshot_called()); |
| 537 } | 537 } |
| 538 | 538 |
| 539 TEST_F(PrerenderingOfflinerTest, HandleTimeoutWithLowbarAndStartedTriesMet) { | 539 TEST_F(PrerenderingOfflinerTest, HandleTimeoutWithLowbarAndStartedTriesMet) { |
| 540 offliner()->SetLowEndDeviceForTesting(false); | 540 offliner()->SetLowEndDeviceForTesting(false); |
| 541 | 541 |
| 542 base::Time creation_time = base::Time::Now(); | 542 base::Time creation_time = base::Time::Now(); |
| 543 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 543 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 544 kUserRequested); | 544 kUserRequested); |
| 545 request.set_started_attempt_count(policy()->GetMaxStartedTries() - 1); | 545 request.set_started_attempt_count(policy()->GetMaxStartedTries() - 1); |
| 546 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 546 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 547 progress_callback())); | 547 progress_callback())); |
| 548 loader()->set_is_lowbar_met(true); | 548 loader()->set_is_lowbar_met(true); |
| 549 EXPECT_TRUE(offliner()->HandleTimeout(request)); | 549 EXPECT_TRUE(offliner()->HandleTimeout(kRequestId)); |
| 550 EXPECT_TRUE(loader()->start_snapshot_called()); | 550 EXPECT_TRUE(loader()->start_snapshot_called()); |
| 551 } | 551 } |
| 552 | 552 |
| 553 TEST_F(PrerenderingOfflinerTest, DISABLED_HandleTimeoutWithOnlyLowbarMet) { | 553 TEST_F(PrerenderingOfflinerTest, DISABLED_HandleTimeoutWithOnlyLowbarMet) { |
| 554 offliner()->SetLowEndDeviceForTesting(false); | 554 offliner()->SetLowEndDeviceForTesting(false); |
| 555 | 555 |
| 556 base::Time creation_time = base::Time::Now(); | 556 base::Time creation_time = base::Time::Now(); |
| 557 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 557 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 558 kUserRequested); | 558 kUserRequested); |
| 559 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 559 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 560 progress_callback())); | 560 progress_callback())); |
| 561 loader()->set_is_lowbar_met(true); | 561 loader()->set_is_lowbar_met(true); |
| 562 EXPECT_FALSE(offliner()->HandleTimeout(request)); | 562 EXPECT_FALSE(offliner()->HandleTimeout(kRequestId)); |
| 563 EXPECT_FALSE(loader()->start_snapshot_called()); | 563 EXPECT_FALSE(loader()->start_snapshot_called()); |
| 564 } | 564 } |
| 565 | 565 |
| 566 TEST_F(PrerenderingOfflinerTest, SignalCollectionDisabled) { | 566 TEST_F(PrerenderingOfflinerTest, SignalCollectionDisabled) { |
| 567 // Ensure feature flag for Signal collection is off, | 567 // Ensure feature flag for Signal collection is off, |
| 568 EXPECT_FALSE(offline_pages::IsOfflinePagesLoadSignalCollectingEnabled()); | 568 EXPECT_FALSE(offline_pages::IsOfflinePagesLoadSignalCollectingEnabled()); |
| 569 | 569 |
| 570 base::Time creation_time = base::Time::Now(); | 570 base::Time creation_time = base::Time::Now(); |
| 571 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 571 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 572 kUserRequested); | 572 kUserRequested); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 PumpLoop(); | 610 PumpLoop(); |
| 611 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); | 611 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); |
| 612 | 612 |
| 613 // One extra part should be added if the flag is on. | 613 // One extra part should be added if the flag is on. |
| 614 content::MHTMLExtraParts* extra_parts = | 614 content::MHTMLExtraParts* extra_parts = |
| 615 content::MHTMLExtraParts::FromWebContents(loader()->web_contents()); | 615 content::MHTMLExtraParts::FromWebContents(loader()->web_contents()); |
| 616 EXPECT_EQ(extra_parts->size(), 1); | 616 EXPECT_EQ(extra_parts->size(), 1); |
| 617 } | 617 } |
| 618 | 618 |
| 619 } // namespace offline_pages | 619 } // namespace offline_pages |
| OLD | NEW |