Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/test/histogram_tester.h" | 16 #include "base/test/histogram_tester.h" |
| 17 #include "base/test/simple_test_clock.h" | |
| 16 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 19 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 18 #include "chrome/browser/android/offline_pages/offline_page_request_interceptor. h" | 20 #include "chrome/browser/android/offline_pages/offline_page_request_interceptor. h" |
| 19 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" | 21 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" |
| 20 #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 " |
| 21 #include "chrome/common/chrome_paths.h" | 23 #include "chrome/common/chrome_paths.h" |
| 22 #include "chrome/test/base/testing_browser_process.h" | 24 #include "chrome/test/base/testing_browser_process.h" |
| 23 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
| 24 #include "chrome/test/base/testing_profile_manager.h" | 26 #include "chrome/test/base/testing_profile_manager.h" |
| 25 #include "components/offline_pages/client_namespace_constants.h" | 27 #include "components/offline_pages/client_namespace_constants.h" |
| 26 #include "components/offline_pages/offline_page_model.h" | 28 #include "components/offline_pages/offline_page_model_impl.h" |
| 27 #include "components/previews/previews_experiments.h" | 29 #include "components/previews/previews_experiments.h" |
| 28 #include "components/variations/variations_associated_data.h" | 30 #include "components/variations/variations_associated_data.h" |
| 29 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/resource_request_info.h" | 32 #include "content/public/browser/resource_request_info.h" |
| 31 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
| 32 #include "content/public/common/resource_type.h" | 34 #include "content/public/common/resource_type.h" |
| 33 #include "content/public/test/test_browser_thread_bundle.h" | 35 #include "content/public/test/test_browser_thread_bundle.h" |
| 34 #include "net/nqe/network_quality_estimator.h" | 36 #include "net/nqe/network_quality_estimator.h" |
| 35 #include "net/url_request/url_request.h" | 37 #include "net/url_request/url_request.h" |
| 36 #include "net/url_request/url_request_context.h" | 38 #include "net/url_request/url_request_context.h" |
| 37 #include "net/url_request/url_request_intercepting_job_factory.h" | 39 #include "net/url_request/url_request_intercepting_job_factory.h" |
| 38 #include "net/url_request/url_request_job_factory_impl.h" | 40 #include "net/url_request/url_request_job_factory_impl.h" |
| 39 #include "net/url_request/url_request_test_util.h" | 41 #include "net/url_request/url_request_test_util.h" |
| 40 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
| 41 | 43 |
| 42 namespace offline_pages { | 44 namespace offline_pages { |
| 43 | 45 |
| 44 namespace { | 46 namespace { |
| 45 | 47 |
| 46 const GURL kTestUrl("http://test.org/page1"); | 48 const GURL kTestUrl("http://test.org/page1"); |
| 47 const GURL kTestUrl2("http://test.org/page2"); | 49 const GURL kTestUrl2("http://test.org/page2"); |
| 48 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); | 50 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); |
| 51 const ClientId kTestClientId2 = ClientId(kDownloadNamespace, "1a2b3c4d"); | |
| 49 const int kTestFileSize = 444; | 52 const int kTestFileSize = 444; |
| 53 const int kTestFileSize2 = 450; | |
| 50 const int kTabId = 1; | 54 const int kTabId = 1; |
| 51 const int kBufSize = 1024; | 55 const int kBufSize = 1024; |
| 52 const char kAggregatedRequestResultHistogram[] = | 56 const char kAggregatedRequestResultHistogram[] = |
| 53 "OfflinePages.AggregatedRequestResult"; | 57 "OfflinePages.AggregatedRequestResult"; |
| 54 | 58 |
| 55 class OfflinePageRequestJobTestDelegate : | 59 class OfflinePageRequestJobTestDelegate : |
| 56 public OfflinePageRequestJob::Delegate { | 60 public OfflinePageRequestJob::Delegate { |
| 57 public: | 61 public: |
| 58 OfflinePageRequestJobTestDelegate(content::WebContents* web_content, | 62 OfflinePageRequestJobTestDelegate(content::WebContents* web_content, |
| 59 int tab_id) | 63 int tab_id) |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 private: | 223 private: |
| 220 base::FieldTrialList field_trial_list_; | 224 base::FieldTrialList field_trial_list_; |
| 221 std::map<std::string, std::string> network_quality_estimator_params_; | 225 std::map<std::string, std::string> network_quality_estimator_params_; |
| 222 std::unique_ptr<TestNetworkQualityEstimator> test_network_quality_estimator_; | 226 std::unique_ptr<TestNetworkQualityEstimator> test_network_quality_estimator_; |
| 223 net::URLRequestContext* url_request_context_; | 227 net::URLRequestContext* url_request_context_; |
| 224 }; | 228 }; |
| 225 | 229 |
| 226 class TestOfflinePageArchiver : public OfflinePageArchiver { | 230 class TestOfflinePageArchiver : public OfflinePageArchiver { |
| 227 public: | 231 public: |
| 228 TestOfflinePageArchiver(const GURL& url, | 232 TestOfflinePageArchiver(const GURL& url, |
| 229 const base::FilePath& archive_file_path) | 233 const base::FilePath& archive_file_path, |
| 234 int archive_file_size) | |
| 230 : url_(url), | 235 : url_(url), |
| 231 archive_file_path_(archive_file_path) {} | 236 archive_file_path_(archive_file_path), |
| 237 archive_file_size_(archive_file_size) {} | |
| 232 ~TestOfflinePageArchiver() override {} | 238 ~TestOfflinePageArchiver() override {} |
| 233 | 239 |
| 234 void CreateArchive(const base::FilePath& archives_dir, | 240 void CreateArchive(const base::FilePath& archives_dir, |
| 235 int64_t archive_id, | 241 int64_t archive_id, |
| 236 const CreateArchiveCallback& callback) override { | 242 const CreateArchiveCallback& callback) override { |
| 237 base::ThreadTaskRunnerHandle::Get()->PostTask( | 243 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 238 FROM_HERE, | 244 FROM_HERE, |
| 239 base::Bind(callback, this, ArchiverResult::SUCCESSFULLY_CREATED, | 245 base::Bind(callback, this, ArchiverResult::SUCCESSFULLY_CREATED, |
| 240 url_, archive_file_path_, base::string16(), kTestFileSize)); | 246 url_, archive_file_path_, base::string16(), |
| 247 archive_file_size_)); | |
| 241 } | 248 } |
| 242 | 249 |
| 243 private: | 250 private: |
| 244 const GURL url_; | 251 const GURL url_; |
| 245 const base::FilePath archive_file_path_; | 252 const base::FilePath archive_file_path_; |
| 253 const int archive_file_size_; | |
| 246 | 254 |
| 247 DISALLOW_COPY_AND_ASSIGN(TestOfflinePageArchiver); | 255 DISALLOW_COPY_AND_ASSIGN(TestOfflinePageArchiver); |
| 248 }; | 256 }; |
| 249 | 257 |
| 250 } // namespace | 258 } // namespace |
| 251 | 259 |
| 252 class OfflinePageRequestJobTest : public testing::Test { | 260 class OfflinePageRequestJobTest : public testing::Test { |
| 253 public: | 261 public: |
| 254 OfflinePageRequestJobTest(); | 262 OfflinePageRequestJobTest(); |
| 255 ~OfflinePageRequestJobTest() override {} | 263 ~OfflinePageRequestJobTest() override {} |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 269 OfflinePageRequestJob::AggregatedRequestResult result); | 277 OfflinePageRequestJob::AggregatedRequestResult result); |
| 270 | 278 |
| 271 net::TestURLRequestContext* url_request_context() { | 279 net::TestURLRequestContext* url_request_context() { |
| 272 return test_url_request_context_.get(); | 280 return test_url_request_context_.get(); |
| 273 } | 281 } |
| 274 Profile* profile() { return profile_; } | 282 Profile* profile() { return profile_; } |
| 275 OfflinePageTabHelper* offline_page_tab_helper() const { | 283 OfflinePageTabHelper* offline_page_tab_helper() const { |
| 276 return offline_page_tab_helper_; | 284 return offline_page_tab_helper_; |
| 277 } | 285 } |
| 278 int64_t offline_id() const { return offline_id_; } | 286 int64_t offline_id() const { return offline_id_; } |
| 287 int64_t offline_id2() const { return offline_id2_; } | |
| 279 int bytes_read() const { return bytes_read_; } | 288 int bytes_read() const { return bytes_read_; } |
| 280 | 289 |
| 281 private: | 290 private: |
| 282 void OnSavePageDone(SavePageResult result, int64_t offline_id); | 291 void OnSavePageDone(SavePageResult result, int64_t offline_id); |
| 283 std::unique_ptr<net::URLRequest> CreateRequest( | 292 std::unique_ptr<net::URLRequest> CreateRequest( |
| 284 const GURL& url, | 293 const GURL& url, |
| 285 const std::string& method, | 294 const std::string& method, |
| 286 content::ResourceType resource_type); | 295 content::ResourceType resource_type); |
| 287 void ReadCompleted(int bytes_read); | 296 void ReadCompleted(int bytes_read); |
| 288 | 297 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 303 intercepting_job_factory_; | 312 intercepting_job_factory_; |
| 304 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_; | 313 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_; |
| 305 net::TestNetworkDelegate network_delegate_; | 314 net::TestNetworkDelegate network_delegate_; |
| 306 TestingProfileManager profile_manager_; | 315 TestingProfileManager profile_manager_; |
| 307 TestingProfile* profile_; | 316 TestingProfile* profile_; |
| 308 std::unique_ptr<content::WebContents> web_contents_; | 317 std::unique_ptr<content::WebContents> web_contents_; |
| 309 base::HistogramTester histogram_tester_; | 318 base::HistogramTester histogram_tester_; |
| 310 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. | 319 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. |
| 311 std::unique_ptr<net::URLRequest> request_; | 320 std::unique_ptr<net::URLRequest> request_; |
| 312 int64_t offline_id_; | 321 int64_t offline_id_; |
| 322 int64_t offline_id2_; | |
| 313 int bytes_read_; | 323 int bytes_read_; |
| 314 | 324 |
| 315 DISALLOW_COPY_AND_ASSIGN(OfflinePageRequestJobTest); | 325 DISALLOW_COPY_AND_ASSIGN(OfflinePageRequestJobTest); |
| 316 }; | 326 }; |
| 317 | 327 |
| 318 OfflinePageRequestJobTest::OfflinePageRequestJobTest() | 328 OfflinePageRequestJobTest::OfflinePageRequestJobTest() |
| 319 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 329 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 320 network_change_notifier_(new TestNetworkChangeNotifier()), | 330 network_change_notifier_(new TestNetworkChangeNotifier()), |
| 321 profile_manager_(TestingBrowserProcess::GetGlobal()), | 331 profile_manager_(TestingBrowserProcess::GetGlobal()), |
| 322 offline_id_(-1), | 332 offline_id_(-1), |
| 333 offline_id2_(-1), | |
| 323 bytes_read_(0) { | 334 bytes_read_(0) { |
| 324 } | 335 } |
| 325 | 336 |
| 326 void OfflinePageRequestJobTest::SetUp() { | 337 void OfflinePageRequestJobTest::SetUp() { |
| 327 // Create a test profile. | 338 // Create a test profile. |
| 328 ASSERT_TRUE(profile_manager_.SetUp()); | 339 ASSERT_TRUE(profile_manager_.SetUp()); |
| 329 profile_ = profile_manager_.CreateTestingProfile("Profile 1"); | 340 profile_ = profile_manager_.CreateTestingProfile("Profile 1"); |
| 330 | 341 |
| 331 // Create a test web contents. | 342 // Create a test web contents. |
| 332 web_contents_.reset(content::WebContents::Create( | 343 web_contents_.reset(content::WebContents::Create( |
| 333 content::WebContents::CreateParams(profile_))); | 344 content::WebContents::CreateParams(profile_))); |
| 334 OfflinePageTabHelper::CreateForWebContents(web_contents_.get()); | 345 OfflinePageTabHelper::CreateForWebContents(web_contents_.get()); |
| 335 offline_page_tab_helper_ = | 346 offline_page_tab_helper_ = |
| 336 OfflinePageTabHelper::FromWebContents(web_contents_.get()); | 347 OfflinePageTabHelper::FromWebContents(web_contents_.get()); |
| 337 | 348 |
| 338 // Set up the factory for testing. | 349 // Set up the factory for testing. |
| 339 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( | 350 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( |
| 340 profile(), BuildTestOfflinePageModel); | 351 profile(), BuildTestOfflinePageModel); |
| 341 RunUntilIdle(); | 352 RunUntilIdle(); |
| 342 | 353 |
| 343 // Use a test archive file. | 354 OfflinePageModel* model = |
| 344 base::FilePath archive_file_path; | 355 OfflinePageModelFactory::GetForBrowserContext(profile()); |
| 345 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &archive_file_path)); | 356 |
| 346 archive_file_path = | 357 // Hook up a test clock such that we can control the time when the offline |
| 347 archive_file_path.AppendASCII("offline_pages").AppendASCII("test.mhtml"); | 358 // page is created. |
| 348 std::unique_ptr<TestOfflinePageArchiver> archiver( | 359 std::unique_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock()); |
| 349 new TestOfflinePageArchiver(kTestUrl, archive_file_path)); | 360 base::SimpleTestClock* temp_clock = clock.get(); |
| 361 clock->SetNow(base::Time::Now()); | |
| 362 static_cast<OfflinePageModelImpl*>(model)->SetClockForTesting( | |
| 363 std::move(clock)); | |
|
Dmitry Titov
2016/09/09 04:33:06
This is very unorthodox thing you are doing here w
jianli
2016/09/10 00:56:04
Updated per discussion.
| |
| 364 | |
| 365 // All offline pages being created below will point to real archive files | |
| 366 // residing in test data directory. | |
| 367 base::FilePath test_data_dir_path; | |
| 368 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_path)); | |
| 350 | 369 |
| 351 // Save an offline page. | 370 // Save an offline page. |
| 352 OfflinePageModel* model = | 371 base::FilePath archive_file_path = |
| 353 OfflinePageModelFactory::GetForBrowserContext(profile()); | 372 test_data_dir_path.AppendASCII("offline_pages").AppendASCII("test.mhtml"); |
| 373 std::unique_ptr<TestOfflinePageArchiver> archiver( | |
| 374 new TestOfflinePageArchiver(kTestUrl, archive_file_path, kTestFileSize)); | |
| 375 | |
| 354 model->SavePage( | 376 model->SavePage( |
| 355 kTestUrl, kTestClientId, 0, std::move(archiver), | 377 kTestUrl, kTestClientId, 0, std::move(archiver), |
| 356 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, | 378 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, |
| 357 base::Unretained(this))); | 379 base::Unretained(this))); |
| 358 RunUntilIdle(); | 380 RunUntilIdle(); |
| 359 | 381 |
| 382 // Save another offline page associated with same online URL as above, but | |
| 383 // pointing to different archive file. | |
| 384 base::FilePath archive_file_path2 = | |
| 385 test_data_dir_path.AppendASCII("offline_pages"). | |
| 386 AppendASCII("hello.mhtml"); | |
| 387 std::unique_ptr<TestOfflinePageArchiver> archiver2( | |
| 388 new TestOfflinePageArchiver( | |
| 389 kTestUrl, archive_file_path2, kTestFileSize2)); | |
| 390 | |
| 391 // Make sure that the creation time of 2nd offline file is later. | |
| 392 temp_clock->Advance(base::TimeDelta::FromMinutes(10)); | |
| 393 | |
| 394 model->SavePage( | |
| 395 kTestUrl, kTestClientId2, 0, std::move(archiver2), | |
| 396 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, | |
| 397 base::Unretained(this))); | |
| 398 RunUntilIdle(); | |
| 399 | |
| 360 // Create a context with delayed initialization. | 400 // Create a context with delayed initialization. |
| 361 test_url_request_context_.reset(new net::TestURLRequestContext(true)); | 401 test_url_request_context_.reset(new net::TestURLRequestContext(true)); |
| 362 | 402 |
| 363 // Install the interceptor. | 403 // Install the interceptor. |
| 364 std::unique_ptr<net::URLRequestInterceptor> interceptor( | 404 std::unique_ptr<net::URLRequestInterceptor> interceptor( |
| 365 new OfflinePageRequestInterceptor(profile_)); | 405 new OfflinePageRequestInterceptor(profile_)); |
| 366 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl( | 406 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl( |
| 367 new net::URLRequestJobFactoryImpl()); | 407 new net::URLRequestJobFactoryImpl()); |
| 368 intercepting_job_factory_.reset(new TestURLRequestInterceptingJobFactory( | 408 intercepting_job_factory_.reset(new TestURLRequestInterceptingJobFactory( |
| 369 std::move(job_factory_impl), | 409 std::move(job_factory_impl), |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 412 return request; | 452 return request; |
| 413 } | 453 } |
| 414 | 454 |
| 415 void OfflinePageRequestJobTest::ExpectAggregatedRequestResultHistogram( | 455 void OfflinePageRequestJobTest::ExpectAggregatedRequestResultHistogram( |
| 416 OfflinePageRequestJob::AggregatedRequestResult result) { | 456 OfflinePageRequestJob::AggregatedRequestResult result) { |
| 417 histogram_tester_.ExpectUniqueSample( | 457 histogram_tester_.ExpectUniqueSample( |
| 418 kAggregatedRequestResultHistogram, static_cast<int>(result), 1); | 458 kAggregatedRequestResultHistogram, static_cast<int>(result), 1); |
| 419 } | 459 } |
| 420 | 460 |
| 421 void OfflinePageRequestJobTest::OnSavePageDone(SavePageResult result, | 461 void OfflinePageRequestJobTest::OnSavePageDone(SavePageResult result, |
| 422 int64_t offline_id) { | 462 int64_t offline_id) { |
| 423 ASSERT_EQ(SavePageResult::SUCCESS, result); | 463 ASSERT_EQ(SavePageResult::SUCCESS, result); |
| 424 offline_id_ = offline_id; | 464 if (offline_id_ == -1) |
| 465 offline_id_ = offline_id; | |
| 466 else if (offline_id2_ == -1) | |
| 467 offline_id2_ = offline_id; | |
| 425 } | 468 } |
| 426 | 469 |
| 427 void OfflinePageRequestJobTest::InterceptRequestOnIO( | 470 void OfflinePageRequestJobTest::InterceptRequestOnIO( |
| 428 const GURL& url, | 471 const GURL& url, |
| 429 const std::string& method, | 472 const std::string& method, |
| 430 const std::string& extra_header_name, | 473 const std::string& extra_header_name, |
| 431 const std::string& extra_header_value, | 474 const std::string& extra_header_value, |
| 432 content::ResourceType resource_type, | 475 content::ResourceType resource_type, |
| 433 void* profile_id) { | 476 void* profile_id) { |
| 434 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 477 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 515 EXPECT_EQ(0, bytes_read()); | 558 EXPECT_EQ(0, bytes_read()); |
| 516 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); | 559 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); |
| 517 } | 560 } |
| 518 | 561 |
| 519 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnDisconnectedNetwork) { | 562 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnDisconnectedNetwork) { |
| 520 SimulateHasNetworkConnectivity(false); | 563 SimulateHasNetworkConnectivity(false); |
| 521 | 564 |
| 522 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); | 565 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| 523 base::RunLoop().Run(); | 566 base::RunLoop().Run(); |
| 524 | 567 |
| 525 EXPECT_EQ(kTestFileSize, bytes_read()); | 568 EXPECT_EQ(kTestFileSize2, bytes_read()); |
| 526 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); | 569 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); |
| 527 EXPECT_EQ(offline_id(), | 570 EXPECT_EQ(offline_id2(), |
| 528 offline_page_tab_helper()->offline_page()->offline_id); | 571 offline_page_tab_helper()->offline_page()->offline_id); |
| 529 ExpectAggregatedRequestResultHistogram( | 572 ExpectAggregatedRequestResultHistogram( |
| 530 OfflinePageRequestJob::AggregatedRequestResult:: | 573 OfflinePageRequestJob::AggregatedRequestResult:: |
| 531 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK); | 574 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK); |
| 532 } | 575 } |
| 533 | 576 |
| 534 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnDisconnectedNetwork) { | 577 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnDisconnectedNetwork) { |
| 535 SimulateHasNetworkConnectivity(false); | 578 SimulateHasNetworkConnectivity(false); |
| 536 | 579 |
| 537 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); | 580 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| 538 base::RunLoop().Run(); | 581 base::RunLoop().Run(); |
| 539 | 582 |
| 540 EXPECT_EQ(0, bytes_read()); | 583 EXPECT_EQ(0, bytes_read()); |
| 541 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); | 584 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); |
| 542 ExpectAggregatedRequestResultHistogram( | 585 ExpectAggregatedRequestResultHistogram( |
| 543 OfflinePageRequestJob::AggregatedRequestResult:: | 586 OfflinePageRequestJob::AggregatedRequestResult:: |
| 544 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); | 587 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); |
| 545 } | 588 } |
| 546 | 589 |
| 547 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnProhibitivelySlowNetwork) { | 590 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnProhibitivelySlowNetwork) { |
| 548 SimulateHasNetworkConnectivity(true); | 591 SimulateHasNetworkConnectivity(true); |
| 549 | 592 |
| 550 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); | 593 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); |
| 551 | 594 |
| 552 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); | 595 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| 553 base::RunLoop().Run(); | 596 base::RunLoop().Run(); |
| 554 | 597 |
| 555 EXPECT_EQ(kTestFileSize, bytes_read()); | 598 EXPECT_EQ(kTestFileSize2, bytes_read()); |
| 556 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); | 599 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); |
| 557 EXPECT_EQ(offline_id(), | 600 EXPECT_EQ(offline_id2(), |
| 558 offline_page_tab_helper()->offline_page()->offline_id); | 601 offline_page_tab_helper()->offline_page()->offline_id); |
| 559 ExpectAggregatedRequestResultHistogram( | 602 ExpectAggregatedRequestResultHistogram( |
| 560 OfflinePageRequestJob::AggregatedRequestResult:: | 603 OfflinePageRequestJob::AggregatedRequestResult:: |
| 561 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK); | 604 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK); |
| 562 } | 605 } |
| 563 | 606 |
| 564 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnProhibitivelySlowNetwork) { | 607 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnProhibitivelySlowNetwork) { |
| 565 SimulateHasNetworkConnectivity(true); | 608 SimulateHasNetworkConnectivity(true); |
| 566 | 609 |
| 567 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); | 610 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); |
| 568 | 611 |
| 569 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); | 612 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| 570 base::RunLoop().Run(); | 613 base::RunLoop().Run(); |
| 571 | 614 |
| 572 EXPECT_EQ(0, bytes_read()); | 615 EXPECT_EQ(0, bytes_read()); |
| 573 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); | 616 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); |
| 574 ExpectAggregatedRequestResultHistogram( | 617 ExpectAggregatedRequestResultHistogram( |
| 575 OfflinePageRequestJob::AggregatedRequestResult:: | 618 OfflinePageRequestJob::AggregatedRequestResult:: |
| 576 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK); | 619 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK); |
| 577 } | 620 } |
| 578 | 621 |
| 579 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnFlakyNetwork) { | 622 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnFlakyNetwork) { |
| 580 SimulateHasNetworkConnectivity(true); | 623 SimulateHasNetworkConnectivity(true); |
| 581 | 624 |
| 582 // When custom offline header exists and contains "reason=error", it means | 625 // When custom offline header exists and contains "reason=error", it means |
| 583 // that net error is hit in last request due to flaky network. | 626 // that net error is hit in last request due to flaky network. |
| 584 InterceptRequest( | 627 InterceptRequest( |
| 585 kTestUrl, | 628 kTestUrl, |
| 586 "GET", | 629 "GET", |
| 587 kLoadingOfflinePageHeader, | 630 kOfflinePageHeader, |
| 588 std::string(kLoadingOfflinePageReason) + kLoadingOfflinePageDueToNetError, | 631 std::string(kOfflinePageHeaderReasonKey) + "=" + |
| 632 kOfflinePageHeaderReasonValueDueToNetError, | |
| 589 content::RESOURCE_TYPE_MAIN_FRAME); | 633 content::RESOURCE_TYPE_MAIN_FRAME); |
| 590 base::RunLoop().Run(); | 634 base::RunLoop().Run(); |
| 591 | 635 |
| 592 EXPECT_EQ(kTestFileSize, bytes_read()); | 636 EXPECT_EQ(kTestFileSize2, bytes_read()); |
| 593 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); | 637 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); |
| 594 EXPECT_EQ(offline_id(), | 638 EXPECT_EQ(offline_id2(), |
| 595 offline_page_tab_helper()->offline_page()->offline_id); | 639 offline_page_tab_helper()->offline_page()->offline_id); |
| 596 ExpectAggregatedRequestResultHistogram( | 640 ExpectAggregatedRequestResultHistogram( |
| 597 OfflinePageRequestJob::AggregatedRequestResult:: | 641 OfflinePageRequestJob::AggregatedRequestResult:: |
| 598 SHOW_OFFLINE_ON_FLAKY_NETWORK); | 642 SHOW_OFFLINE_ON_FLAKY_NETWORK); |
| 599 } | 643 } |
| 600 | 644 |
| 601 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnFlakyNetwork) { | 645 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnFlakyNetwork) { |
| 602 SimulateHasNetworkConnectivity(true); | 646 SimulateHasNetworkConnectivity(true); |
| 603 | 647 |
| 604 // When custom offline header exists and contains "reason=error", it means | 648 // When custom offline header exists and contains "reason=error", it means |
| 605 // that net error is hit in last request due to flaky network. | 649 // that net error is hit in last request due to flaky network. |
| 606 InterceptRequest( | 650 InterceptRequest( |
| 607 kTestUrl2, | 651 kTestUrl2, |
| 608 "GET", | 652 "GET", |
| 609 kLoadingOfflinePageHeader, | 653 kOfflinePageHeader, |
| 610 std::string(kLoadingOfflinePageReason) + kLoadingOfflinePageDueToNetError, | 654 std::string(kOfflinePageHeaderReasonKey) + "=" + |
| 655 kOfflinePageHeaderReasonValueDueToNetError, | |
| 611 content::RESOURCE_TYPE_MAIN_FRAME); | 656 content::RESOURCE_TYPE_MAIN_FRAME); |
| 612 base::RunLoop().Run(); | 657 base::RunLoop().Run(); |
| 613 | 658 |
| 614 EXPECT_EQ(0, bytes_read()); | 659 EXPECT_EQ(0, bytes_read()); |
| 615 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); | 660 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); |
| 616 ExpectAggregatedRequestResultHistogram( | 661 ExpectAggregatedRequestResultHistogram( |
| 617 OfflinePageRequestJob::AggregatedRequestResult:: | 662 OfflinePageRequestJob::AggregatedRequestResult:: |
| 618 PAGE_NOT_FOUND_ON_FLAKY_NETWORK); | 663 PAGE_NOT_FOUND_ON_FLAKY_NETWORK); |
| 619 } | 664 } |
| 620 | 665 |
| 621 TEST_F(OfflinePageRequestJobTest, ForceLoadOfflinePageOnConnectedNetwork) { | 666 TEST_F(OfflinePageRequestJobTest, ForceLoadOfflinePageOnConnectedNetwork) { |
| 622 SimulateHasNetworkConnectivity(true); | 667 SimulateHasNetworkConnectivity(true); |
| 623 | 668 |
| 624 // When custom offline header exists and contains value other than | 669 // When custom offline header exists and contains value other than |
| 625 // "reason=error", it means that offline page is forced to load. | 670 // "reason=error", it means that offline page is forced to load. |
| 626 InterceptRequest( | 671 InterceptRequest( |
| 627 kTestUrl, | 672 kTestUrl, |
| 628 "GET", | 673 "GET", |
| 629 kLoadingOfflinePageHeader, | 674 kOfflinePageHeader, |
| 630 std::string(kLoadingOfflinePageReason) + "download", | 675 std::string(kOfflinePageHeaderReasonKey) + "=download", |
| 631 content::RESOURCE_TYPE_MAIN_FRAME); | 676 content::RESOURCE_TYPE_MAIN_FRAME); |
| 632 base::RunLoop().Run(); | 677 base::RunLoop().Run(); |
| 633 | 678 |
| 634 EXPECT_EQ(kTestFileSize, bytes_read()); | 679 EXPECT_EQ(kTestFileSize2, bytes_read()); |
| 635 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); | 680 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); |
| 636 EXPECT_EQ(offline_id(), | 681 EXPECT_EQ(offline_id2(), |
| 637 offline_page_tab_helper()->offline_page()->offline_id); | 682 offline_page_tab_helper()->offline_page()->offline_id); |
| 638 ExpectAggregatedRequestResultHistogram( | 683 ExpectAggregatedRequestResultHistogram( |
| 639 OfflinePageRequestJob::AggregatedRequestResult:: | 684 OfflinePageRequestJob::AggregatedRequestResult:: |
| 640 SHOW_OFFLINE_ON_CONNECTED_NETWORK); | 685 SHOW_OFFLINE_ON_CONNECTED_NETWORK); |
| 641 } | 686 } |
| 642 | 687 |
| 643 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnConnectedNetwork) { | 688 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnConnectedNetwork) { |
| 644 SimulateHasNetworkConnectivity(true); | 689 SimulateHasNetworkConnectivity(true); |
| 645 | 690 |
| 646 // When custom offline header exists and contains value other than | 691 // When custom offline header exists and contains value other than |
| 647 // "reason=error", it means that offline page is forced to load. | 692 // "reason=error", it means that offline page is forced to load. |
| 648 InterceptRequest( | 693 InterceptRequest( |
| 649 kTestUrl2, | 694 kTestUrl2, |
| 650 "GET", | 695 "GET", |
| 651 kLoadingOfflinePageHeader, | 696 kOfflinePageHeader, |
| 652 std::string(kLoadingOfflinePageReason) + "download", | 697 std::string(kOfflinePageHeaderReasonKey) + "=download", |
| 653 content::RESOURCE_TYPE_MAIN_FRAME); | 698 content::RESOURCE_TYPE_MAIN_FRAME); |
| 654 base::RunLoop().Run(); | 699 base::RunLoop().Run(); |
| 655 | 700 |
| 656 EXPECT_EQ(0, bytes_read()); | 701 EXPECT_EQ(0, bytes_read()); |
| 657 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); | 702 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); |
| 658 ExpectAggregatedRequestResultHistogram( | 703 ExpectAggregatedRequestResultHistogram( |
| 659 OfflinePageRequestJob::AggregatedRequestResult:: | 704 OfflinePageRequestJob::AggregatedRequestResult:: |
| 660 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK); | 705 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK); |
| 661 } | 706 } |
| 662 | 707 |
| 663 TEST_F(OfflinePageRequestJobTest, DoNotLoadOfflinePageOnConnectedNetwork) { | 708 TEST_F(OfflinePageRequestJobTest, DoNotLoadOfflinePageOnConnectedNetwork) { |
| 664 SimulateHasNetworkConnectivity(true); | 709 SimulateHasNetworkConnectivity(true); |
| 665 | 710 |
| 666 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); | 711 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); |
| 667 base::RunLoop().Run(); | 712 base::RunLoop().Run(); |
| 668 | 713 |
| 669 EXPECT_EQ(0, bytes_read()); | 714 EXPECT_EQ(0, bytes_read()); |
| 670 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); | 715 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); |
| 671 } | 716 } |
| 672 | 717 |
| 718 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageByClientID) { | |
| 719 SimulateHasNetworkConnectivity(true); | |
| 720 | |
| 721 InterceptRequest( | |
| 722 kTestUrl, | |
| 723 "GET", | |
| 724 kOfflinePageHeader, | |
| 725 std::string(kOfflinePageHeaderReasonKey) + "=download " + | |
| 726 kOfflinePageHeaderNamespaceKey + "=" + kTestClientId.name_space + | |
| 727 " " + kOfflinePageHeaderIDKey + "=" + kTestClientId.id, | |
| 728 content::RESOURCE_TYPE_MAIN_FRAME); | |
| 729 base::RunLoop().Run(); | |
| 730 | |
| 731 EXPECT_EQ(kTestFileSize, bytes_read()); | |
| 732 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); | |
| 733 EXPECT_EQ(offline_id(), | |
| 734 offline_page_tab_helper()->offline_page()->offline_id); | |
| 735 ExpectAggregatedRequestResultHistogram( | |
| 736 OfflinePageRequestJob::AggregatedRequestResult:: | |
| 737 SHOW_OFFLINE_ON_CONNECTED_NETWORK); | |
| 738 } | |
| 739 | |
| 740 TEST_F(OfflinePageRequestJobTest, | |
| 741 LoadOfflinePageByClientIDAndFallbackToOnlineURL) { | |
| 742 SimulateHasNetworkConnectivity(true); | |
| 743 | |
| 744 // The offline page found with specific client ID does not match the passed | |
| 745 // online URL. Should fall back to find the offline page based on the online | |
| 746 // URL. | |
| 747 InterceptRequest( | |
| 748 kTestUrl2, | |
| 749 "GET", | |
| 750 kOfflinePageHeader, | |
| 751 std::string(kOfflinePageHeaderReasonKey) + "=download " + | |
| 752 kOfflinePageHeaderNamespaceKey + "=" + kTestClientId.name_space + | |
| 753 " " + kOfflinePageHeaderIDKey + "=" + kTestClientId.id, | |
| 754 content::RESOURCE_TYPE_MAIN_FRAME); | |
| 755 base::RunLoop().Run(); | |
| 756 | |
| 757 EXPECT_EQ(0, bytes_read()); | |
| 758 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); | |
| 759 ExpectAggregatedRequestResultHistogram( | |
| 760 OfflinePageRequestJob::AggregatedRequestResult:: | |
| 761 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK); | |
| 762 } | |
| 763 | |
| 764 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageByOfflineID) { | |
| 765 SimulateHasNetworkConnectivity(true); | |
| 766 | |
| 767 InterceptRequest( | |
| 768 kTestUrl, | |
| 769 "GET", | |
| 770 kOfflinePageHeader, | |
| 771 std::string(kOfflinePageHeaderReasonKey) + "=download " + | |
| 772 kOfflinePageHeaderIDKey + "=" + base::Int64ToString(offline_id()), | |
| 773 content::RESOURCE_TYPE_MAIN_FRAME); | |
| 774 base::RunLoop().Run(); | |
| 775 | |
| 776 EXPECT_EQ(kTestFileSize, bytes_read()); | |
| 777 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); | |
| 778 EXPECT_EQ(offline_id(), | |
| 779 offline_page_tab_helper()->offline_page()->offline_id); | |
| 780 ExpectAggregatedRequestResultHistogram( | |
| 781 OfflinePageRequestJob::AggregatedRequestResult:: | |
| 782 SHOW_OFFLINE_ON_CONNECTED_NETWORK); | |
| 783 } | |
| 784 | |
| 785 TEST_F(OfflinePageRequestJobTest, | |
| 786 LoadOfflinePageByOfflineIDAndFallbackToOnlineURL) { | |
| 787 SimulateHasNetworkConnectivity(true); | |
| 788 | |
| 789 // The offline page found with specific offline ID does not match the passed | |
| 790 // online URL. Should fall back to find the offline page based on the online | |
| 791 // URL. | |
| 792 InterceptRequest( | |
| 793 kTestUrl2, | |
| 794 "GET", | |
| 795 kOfflinePageHeader, | |
| 796 std::string(kOfflinePageHeaderReasonKey) + "=download " + | |
| 797 kOfflinePageHeaderIDKey + "=" + base::Int64ToString(offline_id()), | |
| 798 content::RESOURCE_TYPE_MAIN_FRAME); | |
| 799 base::RunLoop().Run(); | |
| 800 | |
| 801 EXPECT_EQ(0, bytes_read()); | |
| 802 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); | |
| 803 ExpectAggregatedRequestResultHistogram( | |
| 804 OfflinePageRequestJob::AggregatedRequestResult:: | |
| 805 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK); | |
| 806 } | |
| 807 | |
| 673 } // namespace offline_pages | 808 } // namespace offline_pages |
| OLD | NEW |