| 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/offline_pages/background_loader_offliner.h" | 5 #include "chrome/browser/offline_pages/background_loader_offliner.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/test/histogram_tester.h" | 9 #include "base/test/histogram_tester.h" |
| 10 #include "base/test/scoped_feature_list.h" | 10 #include "base/test/scoped_feature_list.h" |
| 11 #include "base/test/scoped_mock_time_message_loop_task_runner.h" | 11 #include "base/test/scoped_mock_time_message_loop_task_runner.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "chrome/browser/android/offline_pages/offliner_helper.h" | 13 #include "chrome/browser/android/offline_pages/offliner_helper.h" |
| 14 #include "chrome/browser/loader/chrome_navigation_data.h" |
| 14 #include "chrome/browser/net/prediction_options.h" | 15 #include "chrome/browser/net/prediction_options.h" |
| 15 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 17 #include "components/content_settings/core/common/pref_names.h" | 18 #include "components/content_settings/core/common/pref_names.h" |
| 18 #include "components/offline_pages/content/background_loader/background_loader_c
ontents_stub.h" | 19 #include "components/offline_pages/content/background_loader/background_loader_c
ontents_stub.h" |
| 19 #include "components/offline_pages/core/background/load_termination_listener.h" | 20 #include "components/offline_pages/core/background/load_termination_listener.h" |
| 20 #include "components/offline_pages/core/background/offliner.h" | 21 #include "components/offline_pages/core/background/offliner.h" |
| 21 #include "components/offline_pages/core/background/offliner_policy.h" | 22 #include "components/offline_pages/core/background/offliner_policy.h" |
| 22 #include "components/offline_pages/core/background/save_page_request.h" | 23 #include "components/offline_pages/core/background/save_page_request.h" |
| 23 #include "components/offline_pages/core/offline_page_feature.h" | 24 #include "components/offline_pages/core/offline_page_feature.h" |
| (...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 offliner()->DidFinishNavigation(handle.get()); | 564 offliner()->DidFinishNavigation(handle.get()); |
| 564 // NavigationHandle is always destroyed after finishing navigation. | 565 // NavigationHandle is always destroyed after finishing navigation. |
| 565 handle.reset(); | 566 handle.reset(); |
| 566 CompleteLoading(); | 567 CompleteLoading(); |
| 567 PumpLoop(); | 568 PumpLoop(); |
| 568 | 569 |
| 569 EXPECT_TRUE(completion_callback_called()); | 570 EXPECT_TRUE(completion_callback_called()); |
| 570 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_NEXT, request_status()); | 571 EXPECT_EQ(Offliner::RequestStatus::LOADING_FAILED_NO_NEXT, request_status()); |
| 571 } | 572 } |
| 572 | 573 |
| 574 TEST_F(BackgroundLoaderOfflinerTest, OffliningPreviewsStatusOffHistogram) { |
| 575 base::Time creation_time = base::Time::Now(); |
| 576 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 577 kUserRequested); |
| 578 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 579 progress_callback())); |
| 580 |
| 581 // Called after calling LoadAndSave so we have web_contents to work with. |
| 582 std::unique_ptr<content::NavigationHandle> handle( |
| 583 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 584 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, |
| 585 net::Error::OK)); |
| 586 // Set up ChromeNavigationData on the handle. |
| 587 std::unique_ptr<ChromeNavigationData> chrome_navigation_data( |
| 588 new ChromeNavigationData()); |
| 589 chrome_navigation_data->set_previews_state( |
| 590 content::PreviewsTypes::PREVIEWS_NO_TRANSFORM); |
| 591 std::unique_ptr<content::NavigationData> navigation_data( |
| 592 chrome_navigation_data.release()); |
| 593 // Call DidFinishNavigation with handle. |
| 594 offliner()->DidFinishNavigation(handle.get()); |
| 595 |
| 596 histograms().ExpectBucketCount( |
| 597 "OfflinePages.Background.OffliningPreviewStatus.async_loading", |
| 598 0, // Previews Disabled |
| 599 1); |
| 600 } |
| 601 |
| 602 TEST_F(BackgroundLoaderOfflinerTest, OffliningPreviewsStatusOnHistogram) { |
| 603 base::Time creation_time = base::Time::Now(); |
| 604 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 605 kUserRequested); |
| 606 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 607 progress_callback())); |
| 608 |
| 609 // Called after calling LoadAndSave so we have web_contents to work with. |
| 610 std::unique_ptr<content::NavigationHandle> handle( |
| 611 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 612 kHttpUrl, offliner()->web_contents()->GetMainFrame(), true, |
| 613 net::Error::OK)); |
| 614 // Set up ChromeNavigationData on the handle. |
| 615 std::unique_ptr<ChromeNavigationData> chrome_navigation_data( |
| 616 new ChromeNavigationData()); |
| 617 chrome_navigation_data->set_previews_state( |
| 618 content::PreviewsTypes::CLIENT_LOFI_ON); |
| 619 std::unique_ptr<content::NavigationData> navigation_data( |
| 620 chrome_navigation_data.release()); |
| 621 offliner()->web_contents_tester()->SetNavigationData( |
| 622 handle.get(), std::move(navigation_data)); |
| 623 // Call DidFinishNavigation with handle. |
| 624 offliner()->DidFinishNavigation(handle.get()); |
| 625 |
| 626 histograms().ExpectBucketCount( |
| 627 "OfflinePages.Background.OffliningPreviewStatus.async_loading", |
| 628 1, // Previews Enabled |
| 629 1); |
| 630 } |
| 631 |
| 573 TEST_F(BackgroundLoaderOfflinerTest, OnlySavesOnceOnMultipleLoads) { | 632 TEST_F(BackgroundLoaderOfflinerTest, OnlySavesOnceOnMultipleLoads) { |
| 574 base::Time creation_time = base::Time::Now(); | 633 base::Time creation_time = base::Time::Now(); |
| 575 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, | 634 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, |
| 576 kUserRequested); | 635 kUserRequested); |
| 577 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), | 636 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), |
| 578 progress_callback())); | 637 progress_callback())); |
| 579 // First load | 638 // First load |
| 580 CompleteLoading(); | 639 CompleteLoading(); |
| 581 // Second load | 640 // Second load |
| 582 CompleteLoading(); | 641 CompleteLoading(); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 697 CompleteLoading(); | 756 CompleteLoading(); |
| 698 PumpLoop(); | 757 PumpLoop(); |
| 699 | 758 |
| 700 // One extra part should be added if the flag is on. | 759 // One extra part should be added if the flag is on. |
| 701 content::MHTMLExtraParts* extra_parts = | 760 content::MHTMLExtraParts* extra_parts = |
| 702 content::MHTMLExtraParts::FromWebContents(offliner()->web_contents()); | 761 content::MHTMLExtraParts::FromWebContents(offliner()->web_contents()); |
| 703 EXPECT_EQ(extra_parts->size(), 1); | 762 EXPECT_EQ(extra_parts->size(), 1); |
| 704 } | 763 } |
| 705 | 764 |
| 706 } // namespace offline_pages | 765 } // namespace offline_pages |
| OLD | NEW |