Chromium Code Reviews| Index: chrome/browser/android/offline_pages/offline_page_request_job_unittest.cc |
| diff --git a/chrome/browser/android/offline_pages/offline_page_request_job_unittest.cc b/chrome/browser/android/offline_pages/offline_page_request_job_unittest.cc |
| index 35f8e56db2e4e6c326a11b313988088a30819a65..110f3198d84c23e0ed81d45dd345803164eb9d02 100644 |
| --- a/chrome/browser/android/offline_pages/offline_page_request_job_unittest.cc |
| +++ b/chrome/browser/android/offline_pages/offline_page_request_job_unittest.cc |
| @@ -2,45 +2,43 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| #include "chrome/browser/android/offline_pages/offline_page_request_job.h" |
| #include "base/callback.h" |
| #include "base/feature_list.h" |
| #include "base/files/file_path.h" |
| #include "base/memory/ptr_util.h" |
| #include "base/message_loop/message_loop.h" |
| -#include "base/metrics/field_trial.h" |
| #include "base/path_service.h" |
| #include "base/run_loop.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/test/histogram_tester.h" |
| #include "base/test/simple_test_clock.h" |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| #include "chrome/browser/android/offline_pages/offline_page_request_interceptor.h" |
| #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" |
| #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h" |
| #include "chrome/common/chrome_paths.h" |
| #include "chrome/test/base/testing_browser_process.h" |
| #include "chrome/test/base/testing_profile.h" |
| #include "chrome/test/base/testing_profile_manager.h" |
| #include "components/offline_pages/client_namespace_constants.h" |
| #include "components/offline_pages/offline_page_model_impl.h" |
| -#include "components/previews/core/previews_experiments.h" |
| -#include "components/variations/variations_associated_data.h" |
| +#include "components/previews/core/previews_decider.h" |
| +#include "components/previews/core/previews_opt_out_store.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/resource_request_info.h" |
| #include "content/public/browser/web_contents.h" |
| #include "content/public/common/resource_type.h" |
| #include "content/public/test/test_browser_thread_bundle.h" |
| -#include "net/nqe/network_quality_estimator.h" |
| #include "net/url_request/url_request.h" |
| #include "net/url_request/url_request_context.h" |
| #include "net/url_request/url_request_intercepting_job_factory.h" |
| #include "net/url_request/url_request_job_factory_impl.h" |
| #include "net/url_request/url_request_test_util.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| namespace offline_pages { |
| namespace { |
| @@ -166,72 +164,43 @@ class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { |
| } |
| void set_online(bool online) { online_ = online; } |
| private: |
| bool online_; |
| DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); |
| }; |
| -class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { |
| +class TestPreviewsDecider : public previews::PreviewsDecider { |
| public: |
| - explicit TestNetworkQualityEstimator( |
| - const std::map<std::string, std::string>& variation_params) |
| - : NetworkQualityEstimator( |
| - std::unique_ptr<net::ExternalEstimateProvider>(), |
| - variation_params), |
| - effective_connection_type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {} |
| - ~TestNetworkQualityEstimator() override {} |
| - |
| - net::EffectiveConnectionType GetEffectiveConnectionType() const override { |
| - return effective_connection_type_; |
| - } |
| + TestPreviewsDecider() : should_allow_preview_(false), weak_factory_(this) {} |
| + ~TestPreviewsDecider() override {} |
| - void set_effective_connection_type( |
| - net::EffectiveConnectionType effective_connection_type) { |
| - effective_connection_type_ = effective_connection_type; |
| + bool ShouldAllowPreview(const net::URLRequest& request, |
| + previews::PreviewsType type) const override { |
| + return should_allow_preview_; |
| } |
| - private: |
| - net::EffectiveConnectionType effective_connection_type_; |
| - |
| - DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); |
| -}; |
| - |
| -class ScopedEnableProbihibitivelySlowNetwork { |
| - public: |
| - explicit ScopedEnableProbihibitivelySlowNetwork( |
| - net::URLRequestContext* url_request_context) |
| - : field_trial_list_(nullptr), |
| - url_request_context_(url_request_context) { |
| - previews::EnableOfflinePreviewsForTesting(); |
| - |
| - test_network_quality_estimator_ = |
| - base::MakeUnique<TestNetworkQualityEstimator> |
| - (network_quality_estimator_params_); |
| - test_network_quality_estimator_->set_effective_connection_type( |
| - net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| - url_request_context_->set_network_quality_estimator( |
| - test_network_quality_estimator_.get()); |
| + void set_should_allow_preview(bool should_allow_preview) { |
| + should_allow_preview_ = should_allow_preview; |
| } |
| - ~ScopedEnableProbihibitivelySlowNetwork() { |
| - url_request_context_->set_network_quality_estimator(nullptr); |
| - variations::testing::ClearAllVariationParams(); |
| + base::WeakPtr<TestPreviewsDecider> GetWeakPtr() { |
| + return weak_factory_.GetWeakPtr(); |
| } |
| private: |
| - base::FieldTrialList field_trial_list_; |
| - std::map<std::string, std::string> network_quality_estimator_params_; |
| - std::unique_ptr<TestNetworkQualityEstimator> test_network_quality_estimator_; |
| - net::URLRequestContext* url_request_context_; |
| + bool should_allow_preview_; |
| + base::WeakPtrFactory<TestPreviewsDecider> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestPreviewsDecider); |
| }; |
| class TestOfflinePageArchiver : public OfflinePageArchiver { |
| public: |
| TestOfflinePageArchiver(const GURL& url, |
| const base::FilePath& archive_file_path, |
| int archive_file_size) |
| : url_(url), |
| archive_file_path_(archive_file_path), |
| archive_file_size_(archive_file_size) {} |
| @@ -281,20 +250,24 @@ class OfflinePageRequestJobTest : public testing::Test { |
| return test_url_request_context_.get(); |
| } |
| Profile* profile() { return profile_; } |
| OfflinePageTabHelper* offline_page_tab_helper() const { |
| return offline_page_tab_helper_; |
| } |
| int64_t offline_id() const { return offline_id_; } |
| int64_t offline_id2() const { return offline_id2_; } |
| int bytes_read() const { return bytes_read_; } |
| + TestPreviewsDecider* test_previews_decider() { |
| + return test_previews_decider_.get(); |
| + } |
| + |
| private: |
| void OnSavePageDone(SavePageResult result, int64_t offline_id); |
| std::unique_ptr<net::URLRequest> CreateRequest( |
| const GURL& url, |
| const std::string& method, |
| content::ResourceType resource_type); |
| void ReadCompleted(int bytes_read); |
| // Runs on IO thread. |
| void InterceptRequestOnIO(const GURL& url, |
| @@ -305,20 +278,21 @@ class OfflinePageRequestJobTest : public testing::Test { |
| void ReadCompletedOnIO(int bytes_read); |
| content::TestBrowserThreadBundle thread_bundle_; |
| base::SimpleTestClock clock_; |
| std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; |
| std::unique_ptr<net::TestURLRequestContext> test_url_request_context_; |
| net::URLRequestJobFactoryImpl url_request_job_factory_; |
| std::unique_ptr<net::URLRequestInterceptingJobFactory> |
| intercepting_job_factory_; |
| std::unique_ptr<TestURLRequestDelegate> url_request_delegate_; |
| + std::unique_ptr<TestPreviewsDecider> test_previews_decider_; |
| net::TestNetworkDelegate network_delegate_; |
| TestingProfileManager profile_manager_; |
| TestingProfile* profile_; |
| std::unique_ptr<content::WebContents> web_contents_; |
| base::HistogramTester histogram_tester_; |
| OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. |
| std::unique_ptr<net::URLRequest> request_; |
| int64_t offline_id_; |
| int64_t offline_id2_; |
| int bytes_read_; |
| @@ -391,23 +365,25 @@ void OfflinePageRequestJobTest::SetUp() { |
| model->SavePage( |
| kTestUrl, kTestClientId2, 0, std::move(archiver2), |
| base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, |
| base::Unretained(this))); |
| RunUntilIdle(); |
| // Create a context with delayed initialization. |
| test_url_request_context_.reset(new net::TestURLRequestContext(true)); |
| + test_previews_decider_.reset(new TestPreviewsDecider()); |
| + |
| // Install the interceptor. |
| std::unique_ptr<net::URLRequestInterceptor> interceptor( |
| - new OfflinePageRequestInterceptor()); |
| + new OfflinePageRequestInterceptor(test_previews_decider_->GetWeakPtr())); |
| std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl( |
| new net::URLRequestJobFactoryImpl()); |
| intercepting_job_factory_.reset(new TestURLRequestInterceptingJobFactory( |
| std::move(job_factory_impl), |
| std::move(interceptor), |
| web_contents_.get())); |
| test_url_request_context_->set_job_factory(intercepting_job_factory_.get()); |
| test_url_request_context_->Init(); |
| } |
| @@ -586,38 +562,38 @@ TEST_F(OfflinePageRequestJobTest, PageNotFoundOnDisconnectedNetwork) { |
| EXPECT_EQ(0, bytes_read()); |
| EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); |
| ExpectAggregatedRequestResultHistogram( |
| OfflinePageRequestJob::AggregatedRequestResult:: |
| PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); |
| } |
| TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnProhibitivelySlowNetwork) { |
| SimulateHasNetworkConnectivity(true); |
| - ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); |
| + test_previews_decider()->set_should_allow_preview(true); |
|
jianli
2016/10/05 20:40:40
nit: might be better to restore its value at the e
|
| InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| base::RunLoop().Run(); |
| EXPECT_EQ(kTestFileSize2, bytes_read()); |
| ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest()); |
| EXPECT_EQ(offline_id2(), |
| offline_page_tab_helper()->GetOfflinePageForTest()->offline_id); |
| ExpectAggregatedRequestResultHistogram( |
| OfflinePageRequestJob::AggregatedRequestResult:: |
| SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK); |
| } |
| TEST_F(OfflinePageRequestJobTest, PageNotFoundOnProhibitivelySlowNetwork) { |
| SimulateHasNetworkConnectivity(true); |
| - ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); |
| + test_previews_decider()->set_should_allow_preview(true); |
| InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| base::RunLoop().Run(); |
| EXPECT_EQ(0, bytes_read()); |
| EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); |
| ExpectAggregatedRequestResultHistogram( |
| OfflinePageRequestJob::AggregatedRequestResult:: |
| PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK); |
| } |