Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: chrome/browser/android/offline_pages/offline_page_request_job_unittest.cc

Issue 2388253002: Use the previews black list for offline previews (Closed)
Patch Set: git cl try Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/field_trial.h" 12 #include "base/metrics/field_trial.h"
13 #include "base/path_service.h" 13 #include "base/path_service.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
16 #include "base/test/histogram_tester.h" 16 #include "base/test/histogram_tester.h"
17 #include "base/test/simple_test_clock.h" 17 #include "base/test/simple_test_clock.h"
18 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
19 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" 19 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
20 #include "chrome/browser/android/offline_pages/offline_page_request_interceptor. h" 20 #include "chrome/browser/android/offline_pages/offline_page_request_interceptor. h"
21 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" 21 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h"
22 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " 22 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h "
23 #include "chrome/common/chrome_paths.h" 23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/test/base/testing_browser_process.h" 24 #include "chrome/test/base/testing_browser_process.h"
25 #include "chrome/test/base/testing_profile.h" 25 #include "chrome/test/base/testing_profile.h"
26 #include "chrome/test/base/testing_profile_manager.h" 26 #include "chrome/test/base/testing_profile_manager.h"
27 #include "components/offline_pages/client_namespace_constants.h" 27 #include "components/offline_pages/client_namespace_constants.h"
28 #include "components/offline_pages/offline_page_model_impl.h" 28 #include "components/offline_pages/offline_page_model_impl.h"
29 #include "components/previews/core/previews_experiments.h" 29 #include "components/previews/core/previews_decider.h"
30 #include "components/previews/core/previews_opt_out_store.h"
30 #include "components/variations/variations_associated_data.h" 31 #include "components/variations/variations_associated_data.h"
31 #include "content/public/browser/browser_thread.h" 32 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/resource_request_info.h" 33 #include "content/public/browser/resource_request_info.h"
33 #include "content/public/browser/web_contents.h" 34 #include "content/public/browser/web_contents.h"
34 #include "content/public/common/resource_type.h" 35 #include "content/public/common/resource_type.h"
35 #include "content/public/test/test_browser_thread_bundle.h" 36 #include "content/public/test/test_browser_thread_bundle.h"
36 #include "net/nqe/network_quality_estimator.h" 37 #include "net/nqe/network_quality_estimator.h"
37 #include "net/url_request/url_request.h" 38 #include "net/url_request/url_request.h"
38 #include "net/url_request/url_request_context.h" 39 #include "net/url_request/url_request_context.h"
39 #include "net/url_request/url_request_intercepting_job_factory.h" 40 #include "net/url_request/url_request_intercepting_job_factory.h"
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 net::EffectiveConnectionType effective_connection_type) { 192 net::EffectiveConnectionType effective_connection_type) {
192 effective_connection_type_ = effective_connection_type; 193 effective_connection_type_ = effective_connection_type;
193 } 194 }
194 195
195 private: 196 private:
196 net::EffectiveConnectionType effective_connection_type_; 197 net::EffectiveConnectionType effective_connection_type_;
197 198
198 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); 199 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator);
199 }; 200 };
200 201
202 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.
203 public:
204 TestPreviewsDecider() : weak_factory_(this) {}
205 ~TestPreviewsDecider() override {}
206
207 bool ShouldAllowPreview(net::URLRequest* request,
208 previews::PreviewsType type) override {
209 // Allow Previews when NQE reports a slow connection.
210 net::NetworkQualityEstimator* network_quality_estimator =
211 request->context()->network_quality_estimator();
212 if (!network_quality_estimator)
213 return false;
214
215 net::EffectiveConnectionType effective_connection_type =
216 network_quality_estimator->GetEffectiveConnectionType();
217 return effective_connection_type >=
218 net::EFFECTIVE_CONNECTION_TYPE_OFFLINE &&
219 effective_connection_type <= net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G;
220 }
221
222 base::WeakPtr<TestPreviewsDecider> GetWeakPtr() {
223 return weak_factory_.GetWeakPtr();
224 }
225
226 private:
227 base::WeakPtrFactory<TestPreviewsDecider> weak_factory_;
228
229 DISALLOW_COPY_AND_ASSIGN(TestPreviewsDecider);
230 };
231
201 class ScopedEnableProbihibitivelySlowNetwork { 232 class ScopedEnableProbihibitivelySlowNetwork {
202 public: 233 public:
203 explicit ScopedEnableProbihibitivelySlowNetwork( 234 explicit ScopedEnableProbihibitivelySlowNetwork(
204 net::URLRequestContext* url_request_context) 235 net::URLRequestContext* url_request_context)
205 : field_trial_list_(nullptr), 236 : field_trial_list_(nullptr),
206 url_request_context_(url_request_context) { 237 url_request_context_(url_request_context) {
207 previews::EnableOfflinePreviewsForTesting();
208 238
209 test_network_quality_estimator_ = 239 test_network_quality_estimator_ =
210 base::MakeUnique<TestNetworkQualityEstimator> 240 base::MakeUnique<TestNetworkQualityEstimator>
211 (network_quality_estimator_params_); 241 (network_quality_estimator_params_);
212 test_network_quality_estimator_->set_effective_connection_type( 242 test_network_quality_estimator_->set_effective_connection_type(
213 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); 243 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
214 url_request_context_->set_network_quality_estimator( 244 url_request_context_->set_network_quality_estimator(
215 test_network_quality_estimator_.get()); 245 test_network_quality_estimator_.get());
216 } 246 }
217 247
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 void ReadCompletedOnIO(int bytes_read); 335 void ReadCompletedOnIO(int bytes_read);
306 336
307 content::TestBrowserThreadBundle thread_bundle_; 337 content::TestBrowserThreadBundle thread_bundle_;
308 base::SimpleTestClock clock_; 338 base::SimpleTestClock clock_;
309 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; 339 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_;
310 std::unique_ptr<net::TestURLRequestContext> test_url_request_context_; 340 std::unique_ptr<net::TestURLRequestContext> test_url_request_context_;
311 net::URLRequestJobFactoryImpl url_request_job_factory_; 341 net::URLRequestJobFactoryImpl url_request_job_factory_;
312 std::unique_ptr<net::URLRequestInterceptingJobFactory> 342 std::unique_ptr<net::URLRequestInterceptingJobFactory>
313 intercepting_job_factory_; 343 intercepting_job_factory_;
314 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_; 344 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_;
345 std::unique_ptr<TestPreviewsDecider> test_previews_decider_;
315 net::TestNetworkDelegate network_delegate_; 346 net::TestNetworkDelegate network_delegate_;
316 TestingProfileManager profile_manager_; 347 TestingProfileManager profile_manager_;
317 TestingProfile* profile_; 348 TestingProfile* profile_;
318 std::unique_ptr<content::WebContents> web_contents_; 349 std::unique_ptr<content::WebContents> web_contents_;
319 base::HistogramTester histogram_tester_; 350 base::HistogramTester histogram_tester_;
320 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. 351 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned.
321 std::unique_ptr<net::URLRequest> request_; 352 std::unique_ptr<net::URLRequest> request_;
322 int64_t offline_id_; 353 int64_t offline_id_;
323 int64_t offline_id2_; 354 int64_t offline_id2_;
324 int bytes_read_; 355 int bytes_read_;
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 422
392 model->SavePage( 423 model->SavePage(
393 kTestUrl, kTestClientId2, 0, std::move(archiver2), 424 kTestUrl, kTestClientId2, 0, std::move(archiver2),
394 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, 425 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone,
395 base::Unretained(this))); 426 base::Unretained(this)));
396 RunUntilIdle(); 427 RunUntilIdle();
397 428
398 // Create a context with delayed initialization. 429 // Create a context with delayed initialization.
399 test_url_request_context_.reset(new net::TestURLRequestContext(true)); 430 test_url_request_context_.reset(new net::TestURLRequestContext(true));
400 431
432 test_previews_decider_.reset(new TestPreviewsDecider());
433
401 // Install the interceptor. 434 // Install the interceptor.
402 std::unique_ptr<net::URLRequestInterceptor> interceptor( 435 std::unique_ptr<net::URLRequestInterceptor> interceptor(
403 new OfflinePageRequestInterceptor()); 436 new OfflinePageRequestInterceptor(test_previews_decider_->GetWeakPtr()));
404 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl( 437 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl(
405 new net::URLRequestJobFactoryImpl()); 438 new net::URLRequestJobFactoryImpl());
406 intercepting_job_factory_.reset(new TestURLRequestInterceptingJobFactory( 439 intercepting_job_factory_.reset(new TestURLRequestInterceptingJobFactory(
407 std::move(job_factory_impl), 440 std::move(job_factory_impl),
408 std::move(interceptor), 441 std::move(interceptor),
409 web_contents_.get())); 442 web_contents_.get()));
410 443
411 test_url_request_context_->set_job_factory(intercepting_job_factory_.get()); 444 test_url_request_context_->set_job_factory(intercepting_job_factory_.get());
412 test_url_request_context_->Init(); 445 test_url_request_context_->Init();
413 } 446 }
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
756 base::RunLoop().Run(); 789 base::RunLoop().Run();
757 790
758 EXPECT_EQ(0, bytes_read()); 791 EXPECT_EQ(0, bytes_read());
759 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); 792 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest());
760 ExpectAggregatedRequestResultHistogram( 793 ExpectAggregatedRequestResultHistogram(
761 OfflinePageRequestJob::AggregatedRequestResult:: 794 OfflinePageRequestJob::AggregatedRequestResult::
762 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK); 795 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK);
763 } 796 }
764 797
765 } // namespace offline_pages 798 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698