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

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: jianli comment 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"
13 #include "base/path_service.h" 12 #include "base/path_service.h"
14 #include "base/run_loop.h" 13 #include "base/run_loop.h"
15 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
16 #include "base/test/histogram_tester.h" 15 #include "base/test/histogram_tester.h"
17 #include "base/test/simple_test_clock.h" 16 #include "base/test/simple_test_clock.h"
18 #include "base/threading/thread_task_runner_handle.h" 17 #include "base/threading/thread_task_runner_handle.h"
19 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" 18 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
20 #include "chrome/browser/android/offline_pages/offline_page_request_interceptor. h" 19 #include "chrome/browser/android/offline_pages/offline_page_request_interceptor. h"
21 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" 20 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h"
22 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " 21 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h "
23 #include "chrome/common/chrome_paths.h" 22 #include "chrome/common/chrome_paths.h"
24 #include "chrome/test/base/testing_browser_process.h" 23 #include "chrome/test/base/testing_browser_process.h"
25 #include "chrome/test/base/testing_profile.h" 24 #include "chrome/test/base/testing_profile.h"
26 #include "chrome/test/base/testing_profile_manager.h" 25 #include "chrome/test/base/testing_profile_manager.h"
27 #include "components/offline_pages/client_namespace_constants.h" 26 #include "components/offline_pages/client_namespace_constants.h"
28 #include "components/offline_pages/offline_page_model_impl.h" 27 #include "components/offline_pages/offline_page_model_impl.h"
29 #include "components/previews/core/previews_experiments.h" 28 #include "components/previews/core/previews_decider.h"
30 #include "components/variations/variations_associated_data.h" 29 #include "components/previews/core/previews_opt_out_store.h"
31 #include "content/public/browser/browser_thread.h" 30 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/resource_request_info.h" 31 #include "content/public/browser/resource_request_info.h"
33 #include "content/public/browser/web_contents.h" 32 #include "content/public/browser/web_contents.h"
34 #include "content/public/common/resource_type.h" 33 #include "content/public/common/resource_type.h"
35 #include "content/public/test/test_browser_thread_bundle.h" 34 #include "content/public/test/test_browser_thread_bundle.h"
36 #include "net/nqe/network_quality_estimator.h"
37 #include "net/url_request/url_request.h" 35 #include "net/url_request/url_request.h"
38 #include "net/url_request/url_request_context.h" 36 #include "net/url_request/url_request_context.h"
39 #include "net/url_request/url_request_intercepting_job_factory.h" 37 #include "net/url_request/url_request_intercepting_job_factory.h"
40 #include "net/url_request/url_request_job_factory_impl.h" 38 #include "net/url_request/url_request_job_factory_impl.h"
41 #include "net/url_request/url_request_test_util.h" 39 #include "net/url_request/url_request_test_util.h"
42 #include "testing/gtest/include/gtest/gtest.h" 40 #include "testing/gtest/include/gtest/gtest.h"
43 41
44 namespace offline_pages { 42 namespace offline_pages {
45 43
46 namespace { 44 namespace {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 } 164 }
167 165
168 void set_online(bool online) { online_ = online; } 166 void set_online(bool online) { online_ = online; }
169 167
170 private: 168 private:
171 bool online_; 169 bool online_;
172 170
173 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); 171 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier);
174 }; 172 };
175 173
176 class TestNetworkQualityEstimator : public net::NetworkQualityEstimator { 174 class TestPreviewsDecider : public previews::PreviewsDecider {
177 public: 175 public:
178 explicit TestNetworkQualityEstimator( 176 TestPreviewsDecider() : should_allow_preview_(false), weak_factory_(this) {}
179 const std::map<std::string, std::string>& variation_params) 177 ~TestPreviewsDecider() override {}
180 : NetworkQualityEstimator(
181 std::unique_ptr<net::ExternalEstimateProvider>(),
182 variation_params),
183 effective_connection_type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {}
184 ~TestNetworkQualityEstimator() override {}
185 178
186 net::EffectiveConnectionType GetEffectiveConnectionType() const override { 179 bool ShouldAllowPreview(const net::URLRequest& request,
187 return effective_connection_type_; 180 previews::PreviewsType type) const override {
181 return should_allow_preview_;
188 } 182 }
189 183
190 void set_effective_connection_type( 184 void set_should_allow_preview(bool should_allow_preview) {
191 net::EffectiveConnectionType effective_connection_type) { 185 should_allow_preview_ = should_allow_preview;
192 effective_connection_type_ = effective_connection_type; 186 }
187
188 base::WeakPtr<TestPreviewsDecider> GetWeakPtr() {
189 return weak_factory_.GetWeakPtr();
193 } 190 }
194 191
195 private: 192 private:
196 net::EffectiveConnectionType effective_connection_type_; 193 bool should_allow_preview_;
194 base::WeakPtrFactory<TestPreviewsDecider> weak_factory_;
197 195
198 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); 196 DISALLOW_COPY_AND_ASSIGN(TestPreviewsDecider);
199 };
200
201 class ScopedEnableProbihibitivelySlowNetwork {
202 public:
203 explicit ScopedEnableProbihibitivelySlowNetwork(
204 net::URLRequestContext* url_request_context)
205 : field_trial_list_(nullptr),
206 url_request_context_(url_request_context) {
207 previews::EnableOfflinePreviewsForTesting();
208
209 test_network_quality_estimator_ =
210 base::MakeUnique<TestNetworkQualityEstimator>
211 (network_quality_estimator_params_);
212 test_network_quality_estimator_->set_effective_connection_type(
213 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
214 url_request_context_->set_network_quality_estimator(
215 test_network_quality_estimator_.get());
216 }
217
218 ~ScopedEnableProbihibitivelySlowNetwork() {
219 url_request_context_->set_network_quality_estimator(nullptr);
220 variations::testing::ClearAllVariationParams();
221 }
222
223 private:
224 base::FieldTrialList field_trial_list_;
225 std::map<std::string, std::string> network_quality_estimator_params_;
226 std::unique_ptr<TestNetworkQualityEstimator> test_network_quality_estimator_;
227 net::URLRequestContext* url_request_context_;
228 }; 197 };
229 198
230 class TestOfflinePageArchiver : public OfflinePageArchiver { 199 class TestOfflinePageArchiver : public OfflinePageArchiver {
231 public: 200 public:
232 TestOfflinePageArchiver(const GURL& url, 201 TestOfflinePageArchiver(const GURL& url,
233 const base::FilePath& archive_file_path, 202 const base::FilePath& archive_file_path,
234 int archive_file_size) 203 int archive_file_size)
235 : url_(url), 204 : url_(url),
236 archive_file_path_(archive_file_path), 205 archive_file_path_(archive_file_path),
237 archive_file_size_(archive_file_size) {} 206 archive_file_size_(archive_file_size) {}
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 return test_url_request_context_.get(); 250 return test_url_request_context_.get();
282 } 251 }
283 Profile* profile() { return profile_; } 252 Profile* profile() { return profile_; }
284 OfflinePageTabHelper* offline_page_tab_helper() const { 253 OfflinePageTabHelper* offline_page_tab_helper() const {
285 return offline_page_tab_helper_; 254 return offline_page_tab_helper_;
286 } 255 }
287 int64_t offline_id() const { return offline_id_; } 256 int64_t offline_id() const { return offline_id_; }
288 int64_t offline_id2() const { return offline_id2_; } 257 int64_t offline_id2() const { return offline_id2_; }
289 int bytes_read() const { return bytes_read_; } 258 int bytes_read() const { return bytes_read_; }
290 259
260 TestPreviewsDecider* test_previews_decider() {
261 return test_previews_decider_.get();
262 }
263
291 private: 264 private:
292 void OnSavePageDone(SavePageResult result, int64_t offline_id); 265 void OnSavePageDone(SavePageResult result, int64_t offline_id);
293 std::unique_ptr<net::URLRequest> CreateRequest( 266 std::unique_ptr<net::URLRequest> CreateRequest(
294 const GURL& url, 267 const GURL& url,
295 const std::string& method, 268 const std::string& method,
296 content::ResourceType resource_type); 269 content::ResourceType resource_type);
297 void ReadCompleted(int bytes_read); 270 void ReadCompleted(int bytes_read);
298 271
299 // Runs on IO thread. 272 // Runs on IO thread.
300 void InterceptRequestOnIO(const GURL& url, 273 void InterceptRequestOnIO(const GURL& url,
301 const std::string& method, 274 const std::string& method,
302 const std::string& extra_header_name, 275 const std::string& extra_header_name,
303 const std::string& extra_header_value, 276 const std::string& extra_header_value,
304 content::ResourceType resource_type); 277 content::ResourceType resource_type);
305 void ReadCompletedOnIO(int bytes_read); 278 void ReadCompletedOnIO(int bytes_read);
306 279
307 content::TestBrowserThreadBundle thread_bundle_; 280 content::TestBrowserThreadBundle thread_bundle_;
308 base::SimpleTestClock clock_; 281 base::SimpleTestClock clock_;
309 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; 282 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_;
310 std::unique_ptr<net::TestURLRequestContext> test_url_request_context_; 283 std::unique_ptr<net::TestURLRequestContext> test_url_request_context_;
311 net::URLRequestJobFactoryImpl url_request_job_factory_; 284 net::URLRequestJobFactoryImpl url_request_job_factory_;
312 std::unique_ptr<net::URLRequestInterceptingJobFactory> 285 std::unique_ptr<net::URLRequestInterceptingJobFactory>
313 intercepting_job_factory_; 286 intercepting_job_factory_;
314 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_; 287 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_;
288 std::unique_ptr<TestPreviewsDecider> test_previews_decider_;
315 net::TestNetworkDelegate network_delegate_; 289 net::TestNetworkDelegate network_delegate_;
316 TestingProfileManager profile_manager_; 290 TestingProfileManager profile_manager_;
317 TestingProfile* profile_; 291 TestingProfile* profile_;
318 std::unique_ptr<content::WebContents> web_contents_; 292 std::unique_ptr<content::WebContents> web_contents_;
319 base::HistogramTester histogram_tester_; 293 base::HistogramTester histogram_tester_;
320 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. 294 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned.
321 std::unique_ptr<net::URLRequest> request_; 295 std::unique_ptr<net::URLRequest> request_;
322 int64_t offline_id_; 296 int64_t offline_id_;
323 int64_t offline_id2_; 297 int64_t offline_id2_;
324 int bytes_read_; 298 int bytes_read_;
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 365
392 model->SavePage( 366 model->SavePage(
393 kTestUrl, kTestClientId2, 0, std::move(archiver2), 367 kTestUrl, kTestClientId2, 0, std::move(archiver2),
394 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, 368 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone,
395 base::Unretained(this))); 369 base::Unretained(this)));
396 RunUntilIdle(); 370 RunUntilIdle();
397 371
398 // Create a context with delayed initialization. 372 // Create a context with delayed initialization.
399 test_url_request_context_.reset(new net::TestURLRequestContext(true)); 373 test_url_request_context_.reset(new net::TestURLRequestContext(true));
400 374
375 test_previews_decider_.reset(new TestPreviewsDecider());
376
401 // Install the interceptor. 377 // Install the interceptor.
402 std::unique_ptr<net::URLRequestInterceptor> interceptor( 378 std::unique_ptr<net::URLRequestInterceptor> interceptor(
403 new OfflinePageRequestInterceptor()); 379 new OfflinePageRequestInterceptor(test_previews_decider_->GetWeakPtr()));
404 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl( 380 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl(
405 new net::URLRequestJobFactoryImpl()); 381 new net::URLRequestJobFactoryImpl());
406 intercepting_job_factory_.reset(new TestURLRequestInterceptingJobFactory( 382 intercepting_job_factory_.reset(new TestURLRequestInterceptingJobFactory(
407 std::move(job_factory_impl), 383 std::move(job_factory_impl),
408 std::move(interceptor), 384 std::move(interceptor),
409 web_contents_.get())); 385 web_contents_.get()));
410 386
411 test_url_request_context_->set_job_factory(intercepting_job_factory_.get()); 387 test_url_request_context_->set_job_factory(intercepting_job_factory_.get());
412 test_url_request_context_->Init(); 388 test_url_request_context_->Init();
413 } 389 }
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 EXPECT_EQ(0, bytes_read()); 562 EXPECT_EQ(0, bytes_read());
587 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); 563 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest());
588 ExpectAggregatedRequestResultHistogram( 564 ExpectAggregatedRequestResultHistogram(
589 OfflinePageRequestJob::AggregatedRequestResult:: 565 OfflinePageRequestJob::AggregatedRequestResult::
590 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); 566 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK);
591 } 567 }
592 568
593 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnProhibitivelySlowNetwork) { 569 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnProhibitivelySlowNetwork) {
594 SimulateHasNetworkConnectivity(true); 570 SimulateHasNetworkConnectivity(true);
595 571
596 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); 572 test_previews_decider()->set_should_allow_preview(true);
597 573
598 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); 574 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
599 base::RunLoop().Run(); 575 base::RunLoop().Run();
600 576
601 EXPECT_EQ(kTestFileSize2, bytes_read()); 577 EXPECT_EQ(kTestFileSize2, bytes_read());
602 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest()); 578 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest());
603 EXPECT_EQ(offline_id2(), 579 EXPECT_EQ(offline_id2(),
604 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id); 580 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id);
605 ExpectAggregatedRequestResultHistogram( 581 ExpectAggregatedRequestResultHistogram(
606 OfflinePageRequestJob::AggregatedRequestResult:: 582 OfflinePageRequestJob::AggregatedRequestResult::
607 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK); 583 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK);
584 test_previews_decider()->set_should_allow_preview(false);
608 } 585 }
609 586
610 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnProhibitivelySlowNetwork) { 587 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnProhibitivelySlowNetwork) {
611 SimulateHasNetworkConnectivity(true); 588 SimulateHasNetworkConnectivity(true);
612 589
613 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); 590 test_previews_decider()->set_should_allow_preview(true);
614 591
615 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); 592 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
616 base::RunLoop().Run(); 593 base::RunLoop().Run();
617 594
618 EXPECT_EQ(0, bytes_read()); 595 EXPECT_EQ(0, bytes_read());
619 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); 596 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest());
620 ExpectAggregatedRequestResultHistogram( 597 ExpectAggregatedRequestResultHistogram(
621 OfflinePageRequestJob::AggregatedRequestResult:: 598 OfflinePageRequestJob::AggregatedRequestResult::
622 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK); 599 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK);
600 test_previews_decider()->set_should_allow_preview(false);
623 } 601 }
624 602
625 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnFlakyNetwork) { 603 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnFlakyNetwork) {
626 SimulateHasNetworkConnectivity(true); 604 SimulateHasNetworkConnectivity(true);
627 605
628 // When custom offline header exists and contains "reason=error", it means 606 // When custom offline header exists and contains "reason=error", it means
629 // that net error is hit in last request due to flaky network. 607 // that net error is hit in last request due to flaky network.
630 InterceptRequest( 608 InterceptRequest(
631 kTestUrl, 609 kTestUrl,
632 "GET", 610 "GET",
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
756 base::RunLoop().Run(); 734 base::RunLoop().Run();
757 735
758 EXPECT_EQ(0, bytes_read()); 736 EXPECT_EQ(0, bytes_read());
759 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); 737 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest());
760 ExpectAggregatedRequestResultHistogram( 738 ExpectAggregatedRequestResultHistogram(
761 OfflinePageRequestJob::AggregatedRequestResult:: 739 OfflinePageRequestJob::AggregatedRequestResult::
762 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK); 740 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK);
763 } 741 }
764 742
765 } // namespace offline_pages 743 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698