| 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" |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 public: | 230 public: |
| 231 OfflinePageRequestJobTest(); | 231 OfflinePageRequestJobTest(); |
| 232 ~OfflinePageRequestJobTest() override {} | 232 ~OfflinePageRequestJobTest() override {} |
| 233 | 233 |
| 234 void SetUp() override; | 234 void SetUp() override; |
| 235 void TearDown() override; | 235 void TearDown() override; |
| 236 | 236 |
| 237 void SimulateHasNetworkConnectivity(bool has_connectivity); | 237 void SimulateHasNetworkConnectivity(bool has_connectivity); |
| 238 void RunUntilIdle(); | 238 void RunUntilIdle(); |
| 239 | 239 |
| 240 void SavePage(const GURL& url, |
| 241 const ClientId& client_id, |
| 242 std::unique_ptr<OfflinePageArchiver> archiver); |
| 243 |
| 240 void InterceptRequest(const GURL& url, | 244 void InterceptRequest(const GURL& url, |
| 241 const std::string& method, | 245 const std::string& method, |
| 242 const std::string& extra_header_name, | 246 const std::string& extra_header_name, |
| 243 const std::string& extra_header_value, | 247 const std::string& extra_header_value, |
| 244 content::ResourceType resource_type); | 248 content::ResourceType resource_type); |
| 245 | 249 |
| 246 void ExpectAggregatedRequestResultHistogram( | 250 void ExpectAggregatedRequestResultHistogram( |
| 247 OfflinePageRequestJob::AggregatedRequestResult result); | 251 OfflinePageRequestJob::AggregatedRequestResult result); |
| 248 void ExpectAggregatedRequestResultHistogramWithCount( | 252 void ExpectAggregatedRequestResultHistogramWithCount( |
| 249 OfflinePageRequestJob::AggregatedRequestResult result, int count); | 253 OfflinePageRequestJob::AggregatedRequestResult result, int count); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 // residing in test data directory. | 347 // residing in test data directory. |
| 344 base::FilePath test_data_dir_path; | 348 base::FilePath test_data_dir_path; |
| 345 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_path)); | 349 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_path)); |
| 346 | 350 |
| 347 // Save an offline page. | 351 // Save an offline page. |
| 348 base::FilePath archive_file_path = | 352 base::FilePath archive_file_path = |
| 349 test_data_dir_path.AppendASCII("offline_pages").AppendASCII("test.mhtml"); | 353 test_data_dir_path.AppendASCII("offline_pages").AppendASCII("test.mhtml"); |
| 350 std::unique_ptr<TestOfflinePageArchiver> archiver( | 354 std::unique_ptr<TestOfflinePageArchiver> archiver( |
| 351 new TestOfflinePageArchiver(kTestUrl, archive_file_path, kTestFileSize)); | 355 new TestOfflinePageArchiver(kTestUrl, archive_file_path, kTestFileSize)); |
| 352 | 356 |
| 353 model->SavePage( | 357 SavePage(kTestUrl, kTestClientId, std::move(archiver)); |
| 354 kTestUrl, kTestClientId, 0, std::move(archiver), | |
| 355 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, | |
| 356 base::Unretained(this))); | |
| 357 RunUntilIdle(); | |
| 358 | 358 |
| 359 // Save another offline page associated with same online URL as above, but | 359 // Save another offline page associated with same online URL as above, but |
| 360 // pointing to different archive file. | 360 // pointing to different archive file. |
| 361 base::FilePath archive_file_path2 = | 361 base::FilePath archive_file_path2 = |
| 362 test_data_dir_path.AppendASCII("offline_pages"). | 362 test_data_dir_path.AppendASCII("offline_pages"). |
| 363 AppendASCII("hello.mhtml"); | 363 AppendASCII("hello.mhtml"); |
| 364 std::unique_ptr<TestOfflinePageArchiver> archiver2( | 364 std::unique_ptr<TestOfflinePageArchiver> archiver2( |
| 365 new TestOfflinePageArchiver( | 365 new TestOfflinePageArchiver( |
| 366 kTestUrl, archive_file_path2, kTestFileSize2)); | 366 kTestUrl, archive_file_path2, kTestFileSize2)); |
| 367 | 367 |
| 368 // Make sure that the creation time of 2nd offline file is later. | 368 // Make sure that the creation time of 2nd offline file is later. |
| 369 clock_.Advance(base::TimeDelta::FromMinutes(10)); | 369 clock_.Advance(base::TimeDelta::FromMinutes(10)); |
| 370 | 370 |
| 371 model->SavePage( | 371 SavePage(kTestUrl, kTestClientId2, std::move(archiver2)); |
| 372 kTestUrl, kTestClientId2, 0, std::move(archiver2), | |
| 373 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, | |
| 374 base::Unretained(this))); | |
| 375 RunUntilIdle(); | |
| 376 | 372 |
| 377 // Save an offline page associated with online URL that has a fragment | 373 // Save an offline page associated with online URL that has a fragment |
| 378 // identifier. | 374 // identifier. |
| 379 base::FilePath archive_file_path3 = | 375 base::FilePath archive_file_path3 = |
| 380 test_data_dir_path.AppendASCII("offline_pages"). | 376 test_data_dir_path.AppendASCII("offline_pages"). |
| 381 AppendASCII("hello.mhtml"); | 377 AppendASCII("hello.mhtml"); |
| 382 std::unique_ptr<TestOfflinePageArchiver> archiver3( | 378 std::unique_ptr<TestOfflinePageArchiver> archiver3( |
| 383 new TestOfflinePageArchiver( | 379 new TestOfflinePageArchiver( |
| 384 kTestUrl3WithFragment, archive_file_path3, kTestFileSize3)); | 380 kTestUrl3WithFragment, archive_file_path3, kTestFileSize3)); |
| 385 | 381 |
| 386 model->SavePage( | 382 SavePage(kTestUrl3WithFragment, kTestClientId3, std::move(archiver3)); |
| 387 kTestUrl3WithFragment, kTestClientId3, 0, std::move(archiver3), | |
| 388 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, | |
| 389 base::Unretained(this))); | |
| 390 RunUntilIdle(); | |
| 391 | 383 |
| 392 // Create a context with delayed initialization. | 384 // Create a context with delayed initialization. |
| 393 test_url_request_context_.reset(new net::TestURLRequestContext(true)); | 385 test_url_request_context_.reset(new net::TestURLRequestContext(true)); |
| 394 | 386 |
| 395 test_previews_decider_.reset(new TestPreviewsDecider()); | 387 test_previews_decider_.reset(new TestPreviewsDecider()); |
| 396 | 388 |
| 397 // Install the interceptor. | 389 // Install the interceptor. |
| 398 std::unique_ptr<net::URLRequestInterceptor> interceptor( | 390 std::unique_ptr<net::URLRequestInterceptor> interceptor( |
| 399 new OfflinePageRequestInterceptor(test_previews_decider_.get())); | 391 new OfflinePageRequestInterceptor(test_previews_decider_.get())); |
| 400 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl( | 392 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl( |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 histogram_tester_.ExpectUniqueSample( | 449 histogram_tester_.ExpectUniqueSample( |
| 458 kAggregatedRequestResultHistogram, static_cast<int>(result), 1); | 450 kAggregatedRequestResultHistogram, static_cast<int>(result), 1); |
| 459 } | 451 } |
| 460 | 452 |
| 461 void OfflinePageRequestJobTest::ExpectAggregatedRequestResultHistogramWithCount( | 453 void OfflinePageRequestJobTest::ExpectAggregatedRequestResultHistogramWithCount( |
| 462 OfflinePageRequestJob::AggregatedRequestResult result, int count) { | 454 OfflinePageRequestJob::AggregatedRequestResult result, int count) { |
| 463 histogram_tester_.ExpectUniqueSample( | 455 histogram_tester_.ExpectUniqueSample( |
| 464 kAggregatedRequestResultHistogram, static_cast<int>(result), count); | 456 kAggregatedRequestResultHistogram, static_cast<int>(result), count); |
| 465 } | 457 } |
| 466 | 458 |
| 459 void OfflinePageRequestJobTest::SavePage( |
| 460 const GURL& url, |
| 461 const ClientId& client_id, |
| 462 std::unique_ptr<OfflinePageArchiver> archiver) { |
| 463 OfflinePageModel::SavePageParams save_page_params; |
| 464 save_page_params.url = url; |
| 465 save_page_params.client_id = client_id; |
| 466 OfflinePageModelFactory::GetForBrowserContext(profile())->SavePage( |
| 467 save_page_params, |
| 468 std::move(archiver), |
| 469 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, |
| 470 base::Unretained(this))); |
| 471 RunUntilIdle(); |
| 472 } |
| 473 |
| 467 void OfflinePageRequestJobTest::OnSavePageDone(SavePageResult result, | 474 void OfflinePageRequestJobTest::OnSavePageDone(SavePageResult result, |
| 468 int64_t offline_id) { | 475 int64_t offline_id) { |
| 469 ASSERT_EQ(SavePageResult::SUCCESS, result); | 476 ASSERT_EQ(SavePageResult::SUCCESS, result); |
| 470 if (offline_id_ == -1) | 477 if (offline_id_ == -1) |
| 471 offline_id_ = offline_id; | 478 offline_id_ = offline_id; |
| 472 else if (offline_id2_ == -1) | 479 else if (offline_id2_ == -1) |
| 473 offline_id2_ = offline_id; | 480 offline_id2_ = offline_id; |
| 474 else if (offline_id3_ == -1) | 481 else if (offline_id3_ == -1) |
| 475 offline_id3_ = offline_id; | 482 offline_id3_ = offline_id; |
| 476 } | 483 } |
| (...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 810 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest()); | 817 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest()); |
| 811 EXPECT_EQ(offline_id3(), | 818 EXPECT_EQ(offline_id3(), |
| 812 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id); | 819 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id); |
| 813 ExpectAggregatedRequestResultHistogramWithCount( | 820 ExpectAggregatedRequestResultHistogramWithCount( |
| 814 OfflinePageRequestJob::AggregatedRequestResult:: | 821 OfflinePageRequestJob::AggregatedRequestResult:: |
| 815 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK, 3); | 822 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK, 3); |
| 816 | 823 |
| 817 } | 824 } |
| 818 | 825 |
| 819 } // namespace offline_pages | 826 } // namespace offline_pages |
| OLD | NEW |