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" |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 #include "testing/gtest/include/gtest/gtest.h" | 40 #include "testing/gtest/include/gtest/gtest.h" |
| 41 | 41 |
| 42 namespace offline_pages { | 42 namespace offline_pages { |
| 43 | 43 |
| 44 namespace { | 44 namespace { |
| 45 | 45 |
| 46 const GURL kTestUrl("http://test.org/page1"); | 46 const GURL kTestUrl("http://test.org/page1"); |
| 47 const GURL kTestUrl2("http://test.org/page2"); | 47 const GURL kTestUrl2("http://test.org/page2"); |
| 48 const GURL kTestUrl3("http://test.org/page3"); | 48 const GURL kTestUrl3("http://test.org/page3"); |
| 49 const GURL kTestUrl3WithFragment("http://test.org/page3#ref1"); | 49 const GURL kTestUrl3WithFragment("http://test.org/page3#ref1"); |
| 50 const GURL kTestOriginalUrl("http://test.org/first"); | |
| 50 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); | 51 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); |
| 51 const ClientId kTestClientId2 = ClientId(kDownloadNamespace, "1a2b3c4d"); | 52 const ClientId kTestClientId2 = ClientId(kDownloadNamespace, "1a2b3c4d"); |
| 52 const ClientId kTestClientId3 = ClientId(kDownloadNamespace, "3456abcd"); | 53 const ClientId kTestClientId3 = ClientId(kDownloadNamespace, "3456abcd"); |
| 53 const int kTestFileSize = 444; | 54 const int kTestFileSize = 444; |
| 54 const int kTestFileSize2 = 450; | 55 const int kTestFileSize2 = 450; |
| 55 const int kTestFileSize3 = 450; | 56 const int kTestFileSize3 = 450; |
| 56 const int kTabId = 1; | 57 const int kTabId = 1; |
| 57 const int kBufSize = 1024; | 58 const int kBufSize = 1024; |
| 58 const char kAggregatedRequestResultHistogram[] = | 59 const char kAggregatedRequestResultHistogram[] = |
| 59 "OfflinePages.AggregatedRequestResult2"; | 60 "OfflinePages.AggregatedRequestResult2"; |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 ~OfflinePageRequestJobTest() override {} | 233 ~OfflinePageRequestJobTest() override {} |
| 233 | 234 |
| 234 void SetUp() override; | 235 void SetUp() override; |
| 235 void TearDown() override; | 236 void TearDown() override; |
| 236 | 237 |
| 237 void SimulateHasNetworkConnectivity(bool has_connectivity); | 238 void SimulateHasNetworkConnectivity(bool has_connectivity); |
| 238 void RunUntilIdle(); | 239 void RunUntilIdle(); |
| 239 | 240 |
| 240 void SavePage(const GURL& url, | 241 void SavePage(const GURL& url, |
| 241 const ClientId& client_id, | 242 const ClientId& client_id, |
| 243 const GURL& original_url, | |
|
fgorski
2016/11/17 17:37:48
nit: This is quirky (you know me), but could GURLs
jianli
2016/11/17 23:42:06
I thought about this but decided to go with this s
| |
| 242 std::unique_ptr<OfflinePageArchiver> archiver); | 244 std::unique_ptr<OfflinePageArchiver> archiver); |
| 243 | 245 |
| 244 void InterceptRequest(const GURL& url, | 246 void InterceptRequest(const GURL& url, |
| 245 const std::string& method, | 247 const std::string& method, |
| 246 const std::string& extra_header_name, | 248 const std::string& extra_header_name, |
| 247 const std::string& extra_header_value, | 249 const std::string& extra_header_value, |
| 248 content::ResourceType resource_type); | 250 content::ResourceType resource_type); |
| 249 | 251 |
| 250 void ExpectAggregatedRequestResultHistogram( | 252 void ExpectAggregatedRequestResultHistogram( |
| 251 OfflinePageRequestJob::AggregatedRequestResult result); | 253 OfflinePageRequestJob::AggregatedRequestResult result); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 // residing in test data directory. | 349 // residing in test data directory. |
| 348 base::FilePath test_data_dir_path; | 350 base::FilePath test_data_dir_path; |
| 349 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_path)); | 351 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_path)); |
| 350 | 352 |
| 351 // Save an offline page. | 353 // Save an offline page. |
| 352 base::FilePath archive_file_path = | 354 base::FilePath archive_file_path = |
| 353 test_data_dir_path.AppendASCII("offline_pages").AppendASCII("test.mhtml"); | 355 test_data_dir_path.AppendASCII("offline_pages").AppendASCII("test.mhtml"); |
| 354 std::unique_ptr<TestOfflinePageArchiver> archiver( | 356 std::unique_ptr<TestOfflinePageArchiver> archiver( |
| 355 new TestOfflinePageArchiver(kTestUrl, archive_file_path, kTestFileSize)); | 357 new TestOfflinePageArchiver(kTestUrl, archive_file_path, kTestFileSize)); |
| 356 | 358 |
| 357 SavePage(kTestUrl, kTestClientId, std::move(archiver)); | 359 SavePage(kTestUrl, kTestClientId, GURL(), std::move(archiver)); |
| 358 | 360 |
| 359 // Save another offline page associated with same online URL as above, but | 361 // Save another offline page associated with same online URL as above, but |
| 360 // pointing to different archive file. | 362 // pointing to different archive file. |
| 361 base::FilePath archive_file_path2 = | 363 base::FilePath archive_file_path2 = |
| 362 test_data_dir_path.AppendASCII("offline_pages"). | 364 test_data_dir_path.AppendASCII("offline_pages"). |
| 363 AppendASCII("hello.mhtml"); | 365 AppendASCII("hello.mhtml"); |
| 364 std::unique_ptr<TestOfflinePageArchiver> archiver2( | 366 std::unique_ptr<TestOfflinePageArchiver> archiver2( |
| 365 new TestOfflinePageArchiver( | 367 new TestOfflinePageArchiver( |
| 366 kTestUrl, archive_file_path2, kTestFileSize2)); | 368 kTestUrl, archive_file_path2, kTestFileSize2)); |
| 367 | 369 |
| 368 // Make sure that the creation time of 2nd offline file is later. | 370 // Make sure that the creation time of 2nd offline file is later. |
| 369 clock_.Advance(base::TimeDelta::FromMinutes(10)); | 371 clock_.Advance(base::TimeDelta::FromMinutes(10)); |
| 370 | 372 |
| 371 SavePage(kTestUrl, kTestClientId2, std::move(archiver2)); | 373 SavePage(kTestUrl, kTestClientId2, GURL(), std::move(archiver2)); |
| 372 | 374 |
| 373 // Save an offline page associated with online URL that has a fragment | 375 // Save an offline page associated with online URL that has a fragment |
| 374 // identifier. | 376 // identifier. |
| 375 base::FilePath archive_file_path3 = | 377 base::FilePath archive_file_path3 = |
| 376 test_data_dir_path.AppendASCII("offline_pages"). | 378 test_data_dir_path.AppendASCII("offline_pages"). |
| 377 AppendASCII("hello.mhtml"); | 379 AppendASCII("hello.mhtml"); |
| 378 std::unique_ptr<TestOfflinePageArchiver> archiver3( | 380 std::unique_ptr<TestOfflinePageArchiver> archiver3( |
| 379 new TestOfflinePageArchiver( | 381 new TestOfflinePageArchiver( |
| 380 kTestUrl3WithFragment, archive_file_path3, kTestFileSize3)); | 382 kTestUrl3WithFragment, archive_file_path3, kTestFileSize3)); |
| 381 | 383 |
| 382 SavePage(kTestUrl3WithFragment, kTestClientId3, std::move(archiver3)); | 384 SavePage(kTestUrl3WithFragment, kTestClientId3, kTestOriginalUrl, |
| 385 std::move(archiver3)); | |
| 383 | 386 |
| 384 // Create a context with delayed initialization. | 387 // Create a context with delayed initialization. |
| 385 test_url_request_context_.reset(new net::TestURLRequestContext(true)); | 388 test_url_request_context_.reset(new net::TestURLRequestContext(true)); |
| 386 | 389 |
| 387 test_previews_decider_.reset(new TestPreviewsDecider()); | 390 test_previews_decider_.reset(new TestPreviewsDecider()); |
| 388 | 391 |
| 389 // Install the interceptor. | 392 // Install the interceptor. |
| 390 std::unique_ptr<net::URLRequestInterceptor> interceptor( | 393 std::unique_ptr<net::URLRequestInterceptor> interceptor( |
| 391 new OfflinePageRequestInterceptor(test_previews_decider_.get())); | 394 new OfflinePageRequestInterceptor(test_previews_decider_.get())); |
| 392 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl( | 395 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl( |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 452 | 455 |
| 453 void OfflinePageRequestJobTest::ExpectAggregatedRequestResultHistogramWithCount( | 456 void OfflinePageRequestJobTest::ExpectAggregatedRequestResultHistogramWithCount( |
| 454 OfflinePageRequestJob::AggregatedRequestResult result, int count) { | 457 OfflinePageRequestJob::AggregatedRequestResult result, int count) { |
| 455 histogram_tester_.ExpectUniqueSample( | 458 histogram_tester_.ExpectUniqueSample( |
| 456 kAggregatedRequestResultHistogram, static_cast<int>(result), count); | 459 kAggregatedRequestResultHistogram, static_cast<int>(result), count); |
| 457 } | 460 } |
| 458 | 461 |
| 459 void OfflinePageRequestJobTest::SavePage( | 462 void OfflinePageRequestJobTest::SavePage( |
| 460 const GURL& url, | 463 const GURL& url, |
| 461 const ClientId& client_id, | 464 const ClientId& client_id, |
| 465 const GURL& original_url, | |
| 462 std::unique_ptr<OfflinePageArchiver> archiver) { | 466 std::unique_ptr<OfflinePageArchiver> archiver) { |
| 463 OfflinePageModel::SavePageParams save_page_params; | 467 OfflinePageModel::SavePageParams save_page_params; |
| 464 save_page_params.url = url; | 468 save_page_params.url = url; |
| 465 save_page_params.client_id = client_id; | 469 save_page_params.client_id = client_id; |
| 470 save_page_params.original_url = original_url; | |
| 466 OfflinePageModelFactory::GetForBrowserContext(profile())->SavePage( | 471 OfflinePageModelFactory::GetForBrowserContext(profile())->SavePage( |
| 467 save_page_params, | 472 save_page_params, |
| 468 std::move(archiver), | 473 std::move(archiver), |
| 469 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, | 474 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, |
| 470 base::Unretained(this))); | 475 base::Unretained(this))); |
| 471 RunUntilIdle(); | 476 RunUntilIdle(); |
| 472 } | 477 } |
| 473 | 478 |
| 474 void OfflinePageRequestJobTest::OnSavePageDone(SavePageResult result, | 479 void OfflinePageRequestJobTest::OnSavePageDone(SavePageResult result, |
| 475 int64_t offline_id) { | 480 int64_t offline_id) { |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 816 EXPECT_EQ(kTestFileSize3, bytes_read()); | 821 EXPECT_EQ(kTestFileSize3, bytes_read()); |
| 817 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest()); | 822 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest()); |
| 818 EXPECT_EQ(offline_id3(), | 823 EXPECT_EQ(offline_id3(), |
| 819 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id); | 824 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id); |
| 820 ExpectAggregatedRequestResultHistogramWithCount( | 825 ExpectAggregatedRequestResultHistogramWithCount( |
| 821 OfflinePageRequestJob::AggregatedRequestResult:: | 826 OfflinePageRequestJob::AggregatedRequestResult:: |
| 822 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK, 3); | 827 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK, 3); |
| 823 | 828 |
| 824 } | 829 } |
| 825 | 830 |
| 831 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageAfterRedirect) { | |
| 832 SimulateHasNetworkConnectivity(false); | |
| 833 | |
| 834 // This should trigger redirect first. | |
| 835 InterceptRequest( | |
| 836 kTestOriginalUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); | |
| 837 base::RunLoop().Run(); | |
| 838 | |
| 839 EXPECT_EQ(kTestFileSize3, bytes_read()); | |
| 840 ASSERT_TRUE(offline_page_tab_helper()->GetOfflinePageForTest()); | |
| 841 EXPECT_EQ(offline_id3(), | |
| 842 offline_page_tab_helper()->GetOfflinePageForTest()->offline_id); | |
| 843 ExpectAggregatedRequestResultHistogram( | |
| 844 OfflinePageRequestJob::AggregatedRequestResult:: | |
| 845 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK); | |
| 846 } | |
| 847 | |
| 826 } // namespace offline_pages | 848 } // namespace offline_pages |
| OLD | NEW |