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

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: mmenke comments 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) {}
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;
193 } 186 }
194 187
195 private: 188 private:
196 net::EffectiveConnectionType effective_connection_type_; 189 bool should_allow_preview_;
197 190
198 DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimator); 191 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 }; 192 };
229 193
230 class TestOfflinePageArchiver : public OfflinePageArchiver { 194 class TestOfflinePageArchiver : public OfflinePageArchiver {
231 public: 195 public:
232 TestOfflinePageArchiver(const GURL& url, 196 TestOfflinePageArchiver(const GURL& url,
233 const base::FilePath& archive_file_path, 197 const base::FilePath& archive_file_path,
234 int archive_file_size) 198 int archive_file_size)
235 : url_(url), 199 : url_(url),
236 archive_file_path_(archive_file_path), 200 archive_file_path_(archive_file_path),
237 archive_file_size_(archive_file_size) {} 201 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(); 245 return test_url_request_context_.get();
282 } 246 }
283 Profile* profile() { return profile_; } 247 Profile* profile() { return profile_; }
284 OfflinePageTabHelper* offline_page_tab_helper() const { 248 OfflinePageTabHelper* offline_page_tab_helper() const {
285 return offline_page_tab_helper_; 249 return offline_page_tab_helper_;
286 } 250 }
287 int64_t offline_id() const { return offline_id_; } 251 int64_t offline_id() const { return offline_id_; }
288 int64_t offline_id2() const { return offline_id2_; } 252 int64_t offline_id2() const { return offline_id2_; }
289 int bytes_read() const { return bytes_read_; } 253 int bytes_read() const { return bytes_read_; }
290 254
255 TestPreviewsDecider* test_previews_decider() {
256 return test_previews_decider_.get();
257 }
258
291 private: 259 private:
292 void OnSavePageDone(SavePageResult result, int64_t offline_id); 260 void OnSavePageDone(SavePageResult result, int64_t offline_id);
293 std::unique_ptr<net::URLRequest> CreateRequest( 261 std::unique_ptr<net::URLRequest> CreateRequest(
294 const GURL& url, 262 const GURL& url,
295 const std::string& method, 263 const std::string& method,
296 content::ResourceType resource_type); 264 content::ResourceType resource_type);
297 void ReadCompleted(int bytes_read); 265 void ReadCompleted(int bytes_read);
298 266
299 // Runs on IO thread. 267 // Runs on IO thread.
300 void InterceptRequestOnIO(const GURL& url, 268 void InterceptRequestOnIO(const GURL& url,
301 const std::string& method, 269 const std::string& method,
302 const std::string& extra_header_name, 270 const std::string& extra_header_name,
303 const std::string& extra_header_value, 271 const std::string& extra_header_value,
304 content::ResourceType resource_type); 272 content::ResourceType resource_type);
305 void ReadCompletedOnIO(int bytes_read); 273 void ReadCompletedOnIO(int bytes_read);
306 274
307 content::TestBrowserThreadBundle thread_bundle_; 275 content::TestBrowserThreadBundle thread_bundle_;
308 base::SimpleTestClock clock_; 276 base::SimpleTestClock clock_;
309 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; 277 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_;
310 std::unique_ptr<net::TestURLRequestContext> test_url_request_context_; 278 std::unique_ptr<net::TestURLRequestContext> test_url_request_context_;
311 net::URLRequestJobFactoryImpl url_request_job_factory_; 279 net::URLRequestJobFactoryImpl url_request_job_factory_;
312 std::unique_ptr<net::URLRequestInterceptingJobFactory> 280 std::unique_ptr<net::URLRequestInterceptingJobFactory>
313 intercepting_job_factory_; 281 intercepting_job_factory_;
314 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_; 282 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_;
283 std::unique_ptr<TestPreviewsDecider> test_previews_decider_;
315 net::TestNetworkDelegate network_delegate_; 284 net::TestNetworkDelegate network_delegate_;
316 TestingProfileManager profile_manager_; 285 TestingProfileManager profile_manager_;
317 TestingProfile* profile_; 286 TestingProfile* profile_;
318 std::unique_ptr<content::WebContents> web_contents_; 287 std::unique_ptr<content::WebContents> web_contents_;
319 base::HistogramTester histogram_tester_; 288 base::HistogramTester histogram_tester_;
320 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. 289 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned.
321 std::unique_ptr<net::URLRequest> request_; 290 std::unique_ptr<net::URLRequest> request_;
322 int64_t offline_id_; 291 int64_t offline_id_;
323 int64_t offline_id2_; 292 int64_t offline_id2_;
324 int bytes_read_; 293 int bytes_read_;
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 360
392 model->SavePage( 361 model->SavePage(
393 kTestUrl, kTestClientId2, 0, std::move(archiver2), 362 kTestUrl, kTestClientId2, 0, std::move(archiver2),
394 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, 363 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone,
395 base::Unretained(this))); 364 base::Unretained(this)));
396 RunUntilIdle(); 365 RunUntilIdle();
397 366
398 // Create a context with delayed initialization. 367 // Create a context with delayed initialization.
399 test_url_request_context_.reset(new net::TestURLRequestContext(true)); 368 test_url_request_context_.reset(new net::TestURLRequestContext(true));
400 369
370 test_previews_decider_.reset(new TestPreviewsDecider());
371
401 // Install the interceptor. 372 // Install the interceptor.
402 std::unique_ptr<net::URLRequestInterceptor> interceptor( 373 std::unique_ptr<net::URLRequestInterceptor> interceptor(
403 new OfflinePageRequestInterceptor()); 374 new OfflinePageRequestInterceptor(test_previews_decider_.get()));
404 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl( 375 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl(
405 new net::URLRequestJobFactoryImpl()); 376 new net::URLRequestJobFactoryImpl());
406 intercepting_job_factory_.reset(new TestURLRequestInterceptingJobFactory( 377 intercepting_job_factory_.reset(new TestURLRequestInterceptingJobFactory(
407 std::move(job_factory_impl), 378 std::move(job_factory_impl),
408 std::move(interceptor), 379 std::move(interceptor),
409 web_contents_.get())); 380 web_contents_.get()));
410 381
411 test_url_request_context_->set_job_factory(intercepting_job_factory_.get()); 382 test_url_request_context_->set_job_factory(intercepting_job_factory_.get());
412 test_url_request_context_->Init(); 383 test_url_request_context_->Init();
413 } 384 }
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 EXPECT_EQ(0, bytes_read()); 557 EXPECT_EQ(0, bytes_read());
587 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); 558 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest());
588 ExpectAggregatedRequestResultHistogram( 559 ExpectAggregatedRequestResultHistogram(
589 OfflinePageRequestJob::AggregatedRequestResult:: 560 OfflinePageRequestJob::AggregatedRequestResult::
590 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); 561 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK);
591 } 562 }
592 563
593 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnProhibitivelySlowNetwork) { 564 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnProhibitivelySlowNetwork) {
594 SimulateHasNetworkConnectivity(true); 565 SimulateHasNetworkConnectivity(true);
595 566
596 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); 567 test_previews_decider()->set_should_allow_preview(true);
597 568
598 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); 569 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
599 base::RunLoop().Run(); 570 base::RunLoop().Run();
600 571
601 EXPECT_EQ(kTestFileSize2, bytes_read()); 572 EXPECT_EQ(kTestFileSize2, bytes_read());
602 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest()); 573 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest());
603 EXPECT_EQ(offline_id2(), 574 EXPECT_EQ(offline_id2(),
604 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id); 575 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id);
605 ExpectAggregatedRequestResultHistogram( 576 ExpectAggregatedRequestResultHistogram(
606 OfflinePageRequestJob::AggregatedRequestResult:: 577 OfflinePageRequestJob::AggregatedRequestResult::
607 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK); 578 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK);
579 test_previews_decider()->set_should_allow_preview(false);
608 } 580 }
609 581
610 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnProhibitivelySlowNetwork) { 582 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnProhibitivelySlowNetwork) {
611 SimulateHasNetworkConnectivity(true); 583 SimulateHasNetworkConnectivity(true);
612 584
613 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); 585 test_previews_decider()->set_should_allow_preview(true);
614 586
615 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); 587 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
616 base::RunLoop().Run(); 588 base::RunLoop().Run();
617 589
618 EXPECT_EQ(0, bytes_read()); 590 EXPECT_EQ(0, bytes_read());
619 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); 591 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest());
620 ExpectAggregatedRequestResultHistogram( 592 ExpectAggregatedRequestResultHistogram(
621 OfflinePageRequestJob::AggregatedRequestResult:: 593 OfflinePageRequestJob::AggregatedRequestResult::
622 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK); 594 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK);
595 test_previews_decider()->set_should_allow_preview(false);
623 } 596 }
624 597
625 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnFlakyNetwork) { 598 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnFlakyNetwork) {
626 SimulateHasNetworkConnectivity(true); 599 SimulateHasNetworkConnectivity(true);
627 600
628 // When custom offline header exists and contains "reason=error", it means 601 // When custom offline header exists and contains "reason=error", it means
629 // that net error is hit in last request due to flaky network. 602 // that net error is hit in last request due to flaky network.
630 InterceptRequest( 603 InterceptRequest(
631 kTestUrl, 604 kTestUrl,
632 "GET", 605 "GET",
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
756 base::RunLoop().Run(); 729 base::RunLoop().Run();
757 730
758 EXPECT_EQ(0, bytes_read()); 731 EXPECT_EQ(0, bytes_read());
759 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest()); 732 EXPECT_FALSE(offline_page_tab_helper()->GetOfflinePageForTest());
760 ExpectAggregatedRequestResultHistogram( 733 ExpectAggregatedRequestResultHistogram(
761 OfflinePageRequestJob::AggregatedRequestResult:: 734 OfflinePageRequestJob::AggregatedRequestResult::
762 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK); 735 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK);
763 } 736 }
764 737
765 } // namespace offline_pages 738 } // namespace offline_pages
OLDNEW
« no previous file with comments | « chrome/browser/android/offline_pages/offline_page_request_job.cc ('k') | chrome/browser/profiles/profile_impl_io_data.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698