| 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/offline_page_request_job.h" | 5 #include "chrome/browser/android/offline_pages/offline_page_request_job.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/feature_list.h" | 8 #include "base/feature_list.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 | 267 |
| 268 void SimulateHasNetworkConnectivity(bool has_connectivity); | 268 void SimulateHasNetworkConnectivity(bool has_connectivity); |
| 269 void RunUntilIdle(); | 269 void RunUntilIdle(); |
| 270 | 270 |
| 271 void InterceptRequest(const GURL& url, | 271 void InterceptRequest(const GURL& url, |
| 272 const std::string& method, | 272 const std::string& method, |
| 273 const std::string& extra_header_name, | 273 const std::string& extra_header_name, |
| 274 const std::string& extra_header_value, | 274 const std::string& extra_header_value, |
| 275 content::ResourceType resource_type); | 275 content::ResourceType resource_type); |
| 276 | 276 |
| 277 void VerifyInfo(bool should_use_offline, int64_t offline_id); |
| 278 |
| 277 void ExpectAggregatedRequestResultHistogram( | 279 void ExpectAggregatedRequestResultHistogram( |
| 278 OfflinePageRequestJob::AggregatedRequestResult result); | 280 OfflinePageRequestJob::AggregatedRequestResult result); |
| 279 | 281 |
| 280 net::TestURLRequestContext* url_request_context() { | 282 net::TestURLRequestContext* url_request_context() { |
| 281 return test_url_request_context_.get(); | 283 return test_url_request_context_.get(); |
| 282 } | 284 } |
| 283 Profile* profile() { return profile_; } | 285 Profile* profile() { return profile_; } |
| 284 OfflinePageTabHelper* offline_page_tab_helper() const { | 286 OfflinePageTabHelper* offline_page_tab_helper() const { |
| 285 return offline_page_tab_helper_; | 287 return offline_page_tab_helper_; |
| 286 } | 288 } |
| 287 int64_t offline_id() const { return offline_id_; } | 289 int64_t offline_id() const { return offline_id_; } |
| 288 int64_t offline_id2() const { return offline_id2_; } | 290 int64_t offline_id2() const { return offline_id2_; } |
| 289 int bytes_read() const { return bytes_read_; } | 291 int bytes_read() const { return bytes_read_; } |
| 290 | 292 |
| 291 private: | 293 private: |
| 292 void OnSavePageDone(SavePageResult result, int64_t offline_id); | 294 void OnSavePageDone(SavePageResult result, int64_t offline_id); |
| 293 std::unique_ptr<net::URLRequest> CreateRequest( | 295 std::unique_ptr<net::URLRequest> CreateRequest( |
| 294 const GURL& url, | 296 const GURL& url, |
| 295 const std::string& method, | 297 const std::string& method, |
| 296 content::ResourceType resource_type); | 298 content::ResourceType resource_type); |
| 297 void ReadCompleted(int bytes_read); | 299 void ReadCompleted(int bytes_read); |
| 298 | 300 |
| 299 // Runs on IO thread. | 301 // Runs on IO thread. |
| 300 void InterceptRequestOnIO(const GURL& url, | 302 void InterceptRequestOnIO(const GURL& url, |
| 301 const std::string& method, | 303 const std::string& method, |
| 302 const std::string& extra_header_name, | 304 const std::string& extra_header_name, |
| 303 const std::string& extra_header_value, | 305 const std::string& extra_header_value, |
| 304 content::ResourceType resource_type); | 306 content::ResourceType resource_type); |
| 305 void ReadCompletedOnIO(int bytes_read); | 307 void ReadCompletedOnIO(int bytes_read); |
| 308 void VerifyInfoOnIO(bool should_use_offline, int64_t offline_id); |
| 306 | 309 |
| 307 content::TestBrowserThreadBundle thread_bundle_; | 310 content::TestBrowserThreadBundle thread_bundle_; |
| 308 base::SimpleTestClock clock_; | 311 base::SimpleTestClock clock_; |
| 309 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; | 312 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; |
| 310 std::unique_ptr<net::TestURLRequestContext> test_url_request_context_; | 313 std::unique_ptr<net::TestURLRequestContext> test_url_request_context_; |
| 311 net::URLRequestJobFactoryImpl url_request_job_factory_; | 314 net::URLRequestJobFactoryImpl url_request_job_factory_; |
| 312 std::unique_ptr<net::URLRequestInterceptingJobFactory> | 315 std::unique_ptr<net::URLRequestInterceptingJobFactory> |
| 313 intercepting_job_factory_; | 316 intercepting_job_factory_; |
| 314 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_; | 317 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_; |
| 315 net::TestNetworkDelegate network_delegate_; | 318 net::TestNetworkDelegate network_delegate_; |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 content::ResourceType resource_type) { | 498 content::ResourceType resource_type) { |
| 496 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 499 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 497 | 500 |
| 498 content::BrowserThread::PostTask( | 501 content::BrowserThread::PostTask( |
| 499 content::BrowserThread::IO, FROM_HERE, | 502 content::BrowserThread::IO, FROM_HERE, |
| 500 base::Bind(&OfflinePageRequestJobTest::InterceptRequestOnIO, | 503 base::Bind(&OfflinePageRequestJobTest::InterceptRequestOnIO, |
| 501 base::Unretained(this), url, method, extra_header_name, | 504 base::Unretained(this), url, method, extra_header_name, |
| 502 extra_header_value, resource_type)); | 505 extra_header_value, resource_type)); |
| 503 } | 506 } |
| 504 | 507 |
| 508 void OfflinePageRequestJobTest::VerifyInfoOnIO(bool should_use_offline, |
| 509 int64_t offline_id) { |
| 510 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 511 EXPECT_EQ(should_use_offline, !!LoadedOfflinePageInfo::GetInfo(*request_)); |
| 512 if (should_use_offline) { |
| 513 EXPECT_EQ( |
| 514 offline_id, |
| 515 LoadedOfflinePageInfo::GetInfo(*request_)->offline_page()->offline_id); |
| 516 } |
| 517 } |
| 518 |
| 519 void OfflinePageRequestJobTest::VerifyInfo(bool should_use_offline, |
| 520 int64_t offline_id) { |
| 521 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 522 |
| 523 content::BrowserThread::PostTask( |
| 524 content::BrowserThread::IO, FROM_HERE, |
| 525 base::Bind(&OfflinePageRequestJobTest::VerifyInfoOnIO, |
| 526 base::Unretained(this), should_use_offline, offline_id)); |
| 527 } |
| 528 |
| 505 void OfflinePageRequestJobTest::ReadCompletedOnIO(int bytes_read) { | 529 void OfflinePageRequestJobTest::ReadCompletedOnIO(int bytes_read) { |
| 506 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 530 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 507 | 531 |
| 508 content::BrowserThread::PostTask( | 532 content::BrowserThread::PostTask( |
| 509 content::BrowserThread::UI, FROM_HERE, | 533 content::BrowserThread::UI, FROM_HERE, |
| 510 base::Bind(&OfflinePageRequestJobTest::ReadCompleted, | 534 base::Bind(&OfflinePageRequestJobTest::ReadCompleted, |
| 511 base::Unretained(this), bytes_read)); | 535 base::Unretained(this), bytes_read)); |
| 512 } | 536 } |
| 513 | 537 |
| 514 void OfflinePageRequestJobTest::ReadCompleted(int bytes_read) { | 538 void OfflinePageRequestJobTest::ReadCompleted(int bytes_read) { |
| 515 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 539 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 516 | 540 |
| 517 bytes_read_ = bytes_read; | 541 bytes_read_ = bytes_read; |
| 518 base::ThreadTaskRunnerHandle::Get()->PostTask( | 542 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 519 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 543 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 520 } | 544 } |
| 521 | 545 |
| 522 TEST_F(OfflinePageRequestJobTest, FailedToCreateRequestJob) { | 546 TEST_F(OfflinePageRequestJobTest, FailedToCreateRequestJob) { |
| 523 SimulateHasNetworkConnectivity(false); | 547 SimulateHasNetworkConnectivity(false); |
| 524 | 548 |
| 525 // Must be http/https URL. | 549 // Must be http/https URL. |
| 526 InterceptRequest(GURL("ftp://host/doc"), "GET", "", "", | 550 InterceptRequest(GURL("ftp://host/doc"), "GET", "", "", |
| 527 content::RESOURCE_TYPE_MAIN_FRAME); | 551 content::RESOURCE_TYPE_MAIN_FRAME); |
| 528 base::RunLoop().Run(); | 552 base::RunLoop().Run(); |
| 529 EXPECT_EQ(0, bytes_read()); | 553 EXPECT_EQ(0, bytes_read()); |
| 530 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); | 554 VerifyInfo(false, 0); |
| 555 base::RunLoop().RunUntilIdle(); |
| 531 | 556 |
| 532 InterceptRequest(GURL("file:///path/doc"), "GET", "", "", | 557 InterceptRequest(GURL("file:///path/doc"), "GET", "", "", |
| 533 content::RESOURCE_TYPE_MAIN_FRAME); | 558 content::RESOURCE_TYPE_MAIN_FRAME); |
| 534 base::RunLoop().Run(); | 559 base::RunLoop().Run(); |
| 535 EXPECT_EQ(0, bytes_read()); | 560 EXPECT_EQ(0, bytes_read()); |
| 536 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); | 561 VerifyInfo(false, 0); |
| 562 base::RunLoop().RunUntilIdle(); |
| 537 | 563 |
| 538 // Must be GET method. | 564 // Must be GET method. |
| 539 InterceptRequest( | 565 InterceptRequest( |
| 540 kTestUrl, "POST", "", "", content::RESOURCE_TYPE_MAIN_FRAME); | 566 kTestUrl, "POST", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| 541 base::RunLoop().Run(); | 567 base::RunLoop().Run(); |
| 542 EXPECT_EQ(0, bytes_read()); | 568 EXPECT_EQ(0, bytes_read()); |
| 543 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); | 569 VerifyInfo(false, 0); |
| 570 base::RunLoop().RunUntilIdle(); |
| 544 | 571 |
| 545 InterceptRequest( | 572 InterceptRequest( |
| 546 kTestUrl, "HEAD", "", "", content::RESOURCE_TYPE_MAIN_FRAME); | 573 kTestUrl, "HEAD", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| 547 base::RunLoop().Run(); | 574 base::RunLoop().Run(); |
| 548 EXPECT_EQ(0, bytes_read()); | 575 EXPECT_EQ(0, bytes_read()); |
| 549 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); | 576 VerifyInfo(false, 0); |
| 577 base::RunLoop().RunUntilIdle(); |
| 550 | 578 |
| 551 // Must be main resource. | 579 // Must be main resource. |
| 552 InterceptRequest( | 580 InterceptRequest( |
| 553 kTestUrl, "POST", "", "", content::RESOURCE_TYPE_SUB_FRAME); | 581 kTestUrl, "POST", "", "", content::RESOURCE_TYPE_SUB_FRAME); |
| 554 base::RunLoop().Run(); | 582 base::RunLoop().Run(); |
| 555 EXPECT_EQ(0, bytes_read()); | 583 EXPECT_EQ(0, bytes_read()); |
| 556 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); | 584 VerifyInfo(false, 0); |
| 585 base::RunLoop().RunUntilIdle(); |
| 557 | 586 |
| 558 InterceptRequest( | 587 InterceptRequest( |
| 559 kTestUrl, "POST", "", "", content::RESOURCE_TYPE_IMAGE); | 588 kTestUrl, "POST", "", "", content::RESOURCE_TYPE_IMAGE); |
| 560 base::RunLoop().Run(); | 589 base::RunLoop().Run(); |
| 561 EXPECT_EQ(0, bytes_read()); | 590 EXPECT_EQ(0, bytes_read()); |
| 562 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); | 591 VerifyInfo(false, 0); |
| 592 base::RunLoop().RunUntilIdle(); |
| 563 } | 593 } |
| 564 | 594 |
| 565 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnDisconnectedNetwork) { | 595 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnDisconnectedNetwork) { |
| 566 SimulateHasNetworkConnectivity(false); | 596 SimulateHasNetworkConnectivity(false); |
| 567 | 597 |
| 568 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); | 598 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| 569 base::RunLoop().Run(); | 599 base::RunLoop().Run(); |
| 570 | 600 |
| 571 EXPECT_EQ(kTestFileSize2, bytes_read()); | 601 EXPECT_EQ(kTestFileSize2, bytes_read()); |
| 572 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest()); | 602 VerifyInfo(true, offline_id2()); |
| 573 EXPECT_EQ(offline_id2(), | 603 base::RunLoop().RunUntilIdle(); |
| 574 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id); | 604 |
| 575 ExpectAggregatedRequestResultHistogram( | 605 ExpectAggregatedRequestResultHistogram( |
| 576 OfflinePageRequestJob::AggregatedRequestResult:: | 606 OfflinePageRequestJob::AggregatedRequestResult:: |
| 577 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK); | 607 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK); |
| 578 } | 608 } |
| 579 | 609 |
| 580 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnDisconnectedNetwork) { | 610 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnDisconnectedNetwork) { |
| 581 SimulateHasNetworkConnectivity(false); | 611 SimulateHasNetworkConnectivity(false); |
| 582 | 612 |
| 583 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); | 613 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| 584 base::RunLoop().Run(); | 614 base::RunLoop().Run(); |
| 585 | 615 |
| 586 EXPECT_EQ(0, bytes_read()); | 616 EXPECT_EQ(0, bytes_read()); |
| 587 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); | 617 VerifyInfo(false, 0); |
| 618 base::RunLoop().RunUntilIdle(); |
| 619 |
| 588 ExpectAggregatedRequestResultHistogram( | 620 ExpectAggregatedRequestResultHistogram( |
| 589 OfflinePageRequestJob::AggregatedRequestResult:: | 621 OfflinePageRequestJob::AggregatedRequestResult:: |
| 590 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); | 622 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); |
| 591 } | 623 } |
| 592 | 624 |
| 593 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnProhibitivelySlowNetwork) { | 625 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnProhibitivelySlowNetwork) { |
| 594 SimulateHasNetworkConnectivity(true); | 626 SimulateHasNetworkConnectivity(true); |
| 595 | 627 |
| 596 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); | 628 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); |
| 597 | 629 |
| 598 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); | 630 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| 599 base::RunLoop().Run(); | 631 base::RunLoop().Run(); |
| 600 | 632 |
| 601 EXPECT_EQ(kTestFileSize2, bytes_read()); | 633 EXPECT_EQ(kTestFileSize2, bytes_read()); |
| 602 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest()); | 634 VerifyInfo(true, offline_id2()); |
| 603 EXPECT_EQ(offline_id2(), | 635 base::RunLoop().RunUntilIdle(); |
| 604 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id); | 636 |
| 605 ExpectAggregatedRequestResultHistogram( | 637 ExpectAggregatedRequestResultHistogram( |
| 606 OfflinePageRequestJob::AggregatedRequestResult:: | 638 OfflinePageRequestJob::AggregatedRequestResult:: |
| 607 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK); | 639 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK); |
| 608 } | 640 } |
| 609 | 641 |
| 610 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnProhibitivelySlowNetwork) { | 642 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnProhibitivelySlowNetwork) { |
| 611 SimulateHasNetworkConnectivity(true); | 643 SimulateHasNetworkConnectivity(true); |
| 612 | 644 |
| 613 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); | 645 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); |
| 614 | 646 |
| 615 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); | 647 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| 616 base::RunLoop().Run(); | 648 base::RunLoop().Run(); |
| 617 | 649 |
| 618 EXPECT_EQ(0, bytes_read()); | 650 EXPECT_EQ(0, bytes_read()); |
| 619 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); | 651 VerifyInfo(false, 0); |
| 652 base::RunLoop().RunUntilIdle(); |
| 653 |
| 620 ExpectAggregatedRequestResultHistogram( | 654 ExpectAggregatedRequestResultHistogram( |
| 621 OfflinePageRequestJob::AggregatedRequestResult:: | 655 OfflinePageRequestJob::AggregatedRequestResult:: |
| 622 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK); | 656 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK); |
| 623 } | 657 } |
| 624 | 658 |
| 625 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnFlakyNetwork) { | 659 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnFlakyNetwork) { |
| 626 SimulateHasNetworkConnectivity(true); | 660 SimulateHasNetworkConnectivity(true); |
| 627 | 661 |
| 628 // When custom offline header exists and contains "reason=error", it means | 662 // When custom offline header exists and contains "reason=error", it means |
| 629 // that net error is hit in last request due to flaky network. | 663 // that net error is hit in last request due to flaky network. |
| 630 InterceptRequest( | 664 InterceptRequest( |
| 631 kTestUrl, | 665 kTestUrl, |
| 632 "GET", | 666 "GET", |
| 633 kOfflinePageHeader, | 667 kOfflinePageHeader, |
| 634 std::string(kOfflinePageHeaderReasonKey) + "=" + | 668 std::string(kOfflinePageHeaderReasonKey) + "=" + |
| 635 kOfflinePageHeaderReasonValueDueToNetError, | 669 kOfflinePageHeaderReasonValueDueToNetError, |
| 636 content::RESOURCE_TYPE_MAIN_FRAME); | 670 content::RESOURCE_TYPE_MAIN_FRAME); |
| 637 base::RunLoop().Run(); | 671 base::RunLoop().Run(); |
| 638 | 672 |
| 639 EXPECT_EQ(kTestFileSize2, bytes_read()); | 673 EXPECT_EQ(kTestFileSize2, bytes_read()); |
| 640 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest()); | 674 VerifyInfo(true, offline_id2()); |
| 641 EXPECT_EQ(offline_id2(), | 675 base::RunLoop().RunUntilIdle(); |
| 642 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id); | 676 |
| 643 ExpectAggregatedRequestResultHistogram( | 677 ExpectAggregatedRequestResultHistogram( |
| 644 OfflinePageRequestJob::AggregatedRequestResult:: | 678 OfflinePageRequestJob::AggregatedRequestResult:: |
| 645 SHOW_OFFLINE_ON_FLAKY_NETWORK); | 679 SHOW_OFFLINE_ON_FLAKY_NETWORK); |
| 646 } | 680 } |
| 647 | 681 |
| 648 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnFlakyNetwork) { | 682 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnFlakyNetwork) { |
| 649 SimulateHasNetworkConnectivity(true); | 683 SimulateHasNetworkConnectivity(true); |
| 650 | 684 |
| 651 // When custom offline header exists and contains "reason=error", it means | 685 // When custom offline header exists and contains "reason=error", it means |
| 652 // that net error is hit in last request due to flaky network. | 686 // that net error is hit in last request due to flaky network. |
| 653 InterceptRequest( | 687 InterceptRequest( |
| 654 kTestUrl2, | 688 kTestUrl2, |
| 655 "GET", | 689 "GET", |
| 656 kOfflinePageHeader, | 690 kOfflinePageHeader, |
| 657 std::string(kOfflinePageHeaderReasonKey) + "=" + | 691 std::string(kOfflinePageHeaderReasonKey) + "=" + |
| 658 kOfflinePageHeaderReasonValueDueToNetError, | 692 kOfflinePageHeaderReasonValueDueToNetError, |
| 659 content::RESOURCE_TYPE_MAIN_FRAME); | 693 content::RESOURCE_TYPE_MAIN_FRAME); |
| 660 base::RunLoop().Run(); | 694 base::RunLoop().Run(); |
| 661 | 695 |
| 662 EXPECT_EQ(0, bytes_read()); | 696 EXPECT_EQ(0, bytes_read()); |
| 663 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); | 697 VerifyInfo(false, 0); |
| 698 base::RunLoop().RunUntilIdle(); |
| 699 |
| 664 ExpectAggregatedRequestResultHistogram( | 700 ExpectAggregatedRequestResultHistogram( |
| 665 OfflinePageRequestJob::AggregatedRequestResult:: | 701 OfflinePageRequestJob::AggregatedRequestResult:: |
| 666 PAGE_NOT_FOUND_ON_FLAKY_NETWORK); | 702 PAGE_NOT_FOUND_ON_FLAKY_NETWORK); |
| 667 } | 703 } |
| 668 | 704 |
| 669 TEST_F(OfflinePageRequestJobTest, ForceLoadOfflinePageOnConnectedNetwork) { | 705 TEST_F(OfflinePageRequestJobTest, ForceLoadOfflinePageOnConnectedNetwork) { |
| 670 SimulateHasNetworkConnectivity(true); | 706 SimulateHasNetworkConnectivity(true); |
| 671 | 707 |
| 672 // When custom offline header exists and contains value other than | 708 // When custom offline header exists and contains value other than |
| 673 // "reason=error", it means that offline page is forced to load. | 709 // "reason=error", it means that offline page is forced to load. |
| 674 InterceptRequest( | 710 InterceptRequest( |
| 675 kTestUrl, | 711 kTestUrl, |
| 676 "GET", | 712 "GET", |
| 677 kOfflinePageHeader, | 713 kOfflinePageHeader, |
| 678 std::string(kOfflinePageHeaderReasonKey) + "=download", | 714 std::string(kOfflinePageHeaderReasonKey) + "=download", |
| 679 content::RESOURCE_TYPE_MAIN_FRAME); | 715 content::RESOURCE_TYPE_MAIN_FRAME); |
| 680 base::RunLoop().Run(); | 716 base::RunLoop().Run(); |
| 681 | 717 |
| 682 EXPECT_EQ(kTestFileSize2, bytes_read()); | 718 EXPECT_EQ(kTestFileSize2, bytes_read()); |
| 683 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest()); | 719 VerifyInfo(true, offline_id2()); |
| 684 EXPECT_EQ(offline_id2(), | 720 base::RunLoop().RunUntilIdle(); |
| 685 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id); | 721 |
| 686 ExpectAggregatedRequestResultHistogram( | 722 ExpectAggregatedRequestResultHistogram( |
| 687 OfflinePageRequestJob::AggregatedRequestResult:: | 723 OfflinePageRequestJob::AggregatedRequestResult:: |
| 688 SHOW_OFFLINE_ON_CONNECTED_NETWORK); | 724 SHOW_OFFLINE_ON_CONNECTED_NETWORK); |
| 689 } | 725 } |
| 690 | 726 |
| 691 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnConnectedNetwork) { | 727 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnConnectedNetwork) { |
| 692 SimulateHasNetworkConnectivity(true); | 728 SimulateHasNetworkConnectivity(true); |
| 693 | 729 |
| 694 // When custom offline header exists and contains value other than | 730 // When custom offline header exists and contains value other than |
| 695 // "reason=error", it means that offline page is forced to load. | 731 // "reason=error", it means that offline page is forced to load. |
| 696 InterceptRequest( | 732 InterceptRequest( |
| 697 kTestUrl2, | 733 kTestUrl2, |
| 698 "GET", | 734 "GET", |
| 699 kOfflinePageHeader, | 735 kOfflinePageHeader, |
| 700 std::string(kOfflinePageHeaderReasonKey) + "=download", | 736 std::string(kOfflinePageHeaderReasonKey) + "=download", |
| 701 content::RESOURCE_TYPE_MAIN_FRAME); | 737 content::RESOURCE_TYPE_MAIN_FRAME); |
| 702 base::RunLoop().Run(); | 738 base::RunLoop().Run(); |
| 703 | 739 |
| 704 EXPECT_EQ(0, bytes_read()); | 740 EXPECT_EQ(0, bytes_read()); |
| 705 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); | 741 VerifyInfo(false, 0); |
| 742 base::RunLoop().RunUntilIdle(); |
| 743 |
| 706 ExpectAggregatedRequestResultHistogram( | 744 ExpectAggregatedRequestResultHistogram( |
| 707 OfflinePageRequestJob::AggregatedRequestResult:: | 745 OfflinePageRequestJob::AggregatedRequestResult:: |
| 708 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK); | 746 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK); |
| 709 } | 747 } |
| 710 | 748 |
| 711 TEST_F(OfflinePageRequestJobTest, DoNotLoadOfflinePageOnConnectedNetwork) { | 749 TEST_F(OfflinePageRequestJobTest, DoNotLoadOfflinePageOnConnectedNetwork) { |
| 712 SimulateHasNetworkConnectivity(true); | 750 SimulateHasNetworkConnectivity(true); |
| 713 | 751 |
| 714 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); | 752 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| 715 base::RunLoop().Run(); | 753 base::RunLoop().Run(); |
| 716 | 754 |
| 717 EXPECT_EQ(0, bytes_read()); | 755 EXPECT_EQ(0, bytes_read()); |
| 718 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); | 756 VerifyInfo(false, 0); |
| 757 base::RunLoop().RunUntilIdle(); |
| 719 } | 758 } |
| 720 | 759 |
| 721 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageByOfflineID) { | 760 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageByOfflineID) { |
| 722 SimulateHasNetworkConnectivity(true); | 761 SimulateHasNetworkConnectivity(true); |
| 723 | 762 |
| 724 InterceptRequest( | 763 InterceptRequest( |
| 725 kTestUrl, | 764 kTestUrl, |
| 726 "GET", | 765 "GET", |
| 727 kOfflinePageHeader, | 766 kOfflinePageHeader, |
| 728 std::string(kOfflinePageHeaderReasonKey) + "=download " + | 767 std::string(kOfflinePageHeaderReasonKey) + "=download " + |
| 729 kOfflinePageHeaderIDKey + "=" + base::Int64ToString(offline_id()), | 768 kOfflinePageHeaderIDKey + "=" + base::Int64ToString(offline_id()), |
| 730 content::RESOURCE_TYPE_MAIN_FRAME); | 769 content::RESOURCE_TYPE_MAIN_FRAME); |
| 731 base::RunLoop().Run(); | 770 base::RunLoop().Run(); |
| 732 | 771 |
| 733 EXPECT_EQ(kTestFileSize, bytes_read()); | 772 EXPECT_EQ(kTestFileSize, bytes_read()); |
| 734 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest()); | 773 VerifyInfo(true, offline_id()); |
| 735 EXPECT_EQ(offline_id(), | 774 base::RunLoop().RunUntilIdle(); |
| 736 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id); | 775 |
| 737 ExpectAggregatedRequestResultHistogram( | 776 ExpectAggregatedRequestResultHistogram( |
| 738 OfflinePageRequestJob::AggregatedRequestResult:: | 777 OfflinePageRequestJob::AggregatedRequestResult:: |
| 739 SHOW_OFFLINE_ON_CONNECTED_NETWORK); | 778 SHOW_OFFLINE_ON_CONNECTED_NETWORK); |
| 740 } | 779 } |
| 741 | 780 |
| 742 TEST_F(OfflinePageRequestJobTest, | 781 TEST_F(OfflinePageRequestJobTest, |
| 743 LoadOfflinePageByOfflineIDAndFallbackToOnlineURL) { | 782 LoadOfflinePageByOfflineIDAndFallbackToOnlineURL) { |
| 744 SimulateHasNetworkConnectivity(true); | 783 SimulateHasNetworkConnectivity(true); |
| 745 | 784 |
| 746 // The offline page found with specific offline ID does not match the passed | 785 // The offline page found with specific offline ID does not match the passed |
| 747 // online URL. Should fall back to find the offline page based on the online | 786 // online URL. Should fall back to find the offline page based on the online |
| 748 // URL. | 787 // URL. |
| 749 InterceptRequest( | 788 InterceptRequest( |
| 750 kTestUrl2, | 789 kTestUrl2, |
| 751 "GET", | 790 "GET", |
| 752 kOfflinePageHeader, | 791 kOfflinePageHeader, |
| 753 std::string(kOfflinePageHeaderReasonKey) + "=download " + | 792 std::string(kOfflinePageHeaderReasonKey) + "=download " + |
| 754 kOfflinePageHeaderIDKey + "=" + base::Int64ToString(offline_id()), | 793 kOfflinePageHeaderIDKey + "=" + base::Int64ToString(offline_id()), |
| 755 content::RESOURCE_TYPE_MAIN_FRAME); | 794 content::RESOURCE_TYPE_MAIN_FRAME); |
| 756 base::RunLoop().Run(); | 795 base::RunLoop().Run(); |
| 757 | 796 |
| 758 EXPECT_EQ(0, bytes_read()); | 797 EXPECT_EQ(0, bytes_read()); |
| 759 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); | 798 VerifyInfo(false, 0); |
| 799 base::RunLoop().RunUntilIdle(); |
| 800 |
| 760 ExpectAggregatedRequestResultHistogram( | 801 ExpectAggregatedRequestResultHistogram( |
| 761 OfflinePageRequestJob::AggregatedRequestResult:: | 802 OfflinePageRequestJob::AggregatedRequestResult:: |
| 762 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK); | 803 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK); |
| 763 } | 804 } |
| 764 | 805 |
| 765 } // namespace offline_pages | 806 } // namespace offline_pages |
| OLD | NEW |