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..637326983f5b9359639eb66c4dbaf7f2c31c836d 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 |
| @@ -19,21 +19,22 @@ |
| #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/previews/core/previews_decider.h" |
| +#include "components/previews/core/previews_opt_out_store.h" |
| #include "components/variations/variations_associated_data.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" |
| @@ -191,27 +192,56 @@ class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { |
| net::EffectiveConnectionType effective_connection_type) { |
| effective_connection_type_ = effective_connection_type; |
| } |
| private: |
| net::EffectiveConnectionType effective_connection_type_; |
| DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); |
| }; |
| +class TestPreviewsDecider : public previews::PreviewsDecider { |
|
jianli
2016/10/05 01:13:02
Since you're overriding PreviewsDecider, I think y
RyanSturm
2016/10/05 03:09:39
Done.
|
| + public: |
| + TestPreviewsDecider() : weak_factory_(this) {} |
| + ~TestPreviewsDecider() override {} |
| + |
| + bool ShouldAllowPreview(net::URLRequest* request, |
| + previews::PreviewsType type) override { |
| + // Allow Previews when NQE reports a slow connection. |
| + net::NetworkQualityEstimator* network_quality_estimator = |
| + request->context()->network_quality_estimator(); |
| + if (!network_quality_estimator) |
| + return false; |
| + |
| + net::EffectiveConnectionType effective_connection_type = |
| + network_quality_estimator->GetEffectiveConnectionType(); |
| + return effective_connection_type >= |
| + net::EFFECTIVE_CONNECTION_TYPE_OFFLINE && |
| + effective_connection_type <= net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G; |
| + } |
| + |
| + base::WeakPtr<TestPreviewsDecider> GetWeakPtr() { |
| + return weak_factory_.GetWeakPtr(); |
| + } |
| + |
| + private: |
| + base::WeakPtrFactory<TestPreviewsDecider> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestPreviewsDecider); |
| +}; |
| + |
| 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()); |
| } |
| @@ -305,20 +335,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 +422,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(); |
| } |