| 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_tab_helper.h" | 5 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/feature_list.h" | 10 #include "base/feature_list.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/metrics/field_trial.h" |
| 14 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 15 #include "base/test/histogram_tester.h" | 16 #include "base/test/histogram_tester.h" |
| 17 #include "base/test/simple_test_clock.h" |
| 18 #include "base/time/clock.h" |
| 19 #include "base/time/time.h" |
| 16 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 20 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 17 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | 21 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 18 #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
" |
| 23 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service.h" |
| 24 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service_factory.h" |
| 19 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 25 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 26 #include "chrome/test/base/testing_profile.h" |
| 20 #include "components/offline_pages/client_namespace_constants.h" | 27 #include "components/offline_pages/client_namespace_constants.h" |
| 21 #include "components/offline_pages/offline_page_feature.h" | 28 #include "components/offline_pages/offline_page_feature.h" |
| 22 #include "components/offline_pages/offline_page_item.h" | 29 #include "components/offline_pages/offline_page_item.h" |
| 23 #include "components/offline_pages/offline_page_model.h" | 30 #include "components/offline_pages/offline_page_model.h" |
| 24 #include "components/offline_pages/offline_page_test_archiver.h" | 31 #include "components/offline_pages/offline_page_test_archiver.h" |
| 25 #include "components/offline_pages/offline_page_types.h" | 32 #include "components/offline_pages/offline_page_types.h" |
| 26 #include "content/public/browser/navigation_entry.h" | 33 #include "content/public/browser/navigation_entry.h" |
| 27 #include "content/public/browser/web_contents.h" | 34 #include "content/public/browser/web_contents.h" |
| 28 #include "net/base/net_errors.h" | 35 #include "net/base/net_errors.h" |
| 29 #include "net/base/network_change_notifier.h" | 36 #include "net/base/network_change_notifier.h" |
| 37 #include "net/nqe/network_quality_estimator.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 39 |
| 32 namespace offline_pages { | 40 namespace offline_pages { |
| 33 | 41 |
| 34 namespace { | 42 namespace { |
| 35 | 43 |
| 36 const GURL kTestPageUrl("http://test.org/page1"); | 44 const GURL kTestPageUrl("http://test.org/page1"); |
| 37 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); | 45 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); |
| 38 const int64_t kTestFileSize = 876543LL; | 46 const int64_t kTestFileSize = 876543LL; |
| 39 const char kRedirectResultHistogram[] = "OfflinePages.RedirectResult"; | 47 const char kRedirectResultHistogram[] = "OfflinePages.RedirectResult"; |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 RunUntilIdle(); | 352 RunUntilIdle(); |
| 345 | 353 |
| 346 const OfflinePageItem* item = | 354 const OfflinePageItem* item = |
| 347 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); | 355 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); |
| 348 EXPECT_EQ(expected_offline_id, item->offline_id); | 356 EXPECT_EQ(expected_offline_id, item->offline_id); |
| 349 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); | 357 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); |
| 350 EXPECT_EQ(kLastNNamespace, item->client_id.name_space); | 358 EXPECT_EQ(kLastNNamespace, item->client_id.name_space); |
| 351 EXPECT_EQ(kTabId, item->client_id.id); | 359 EXPECT_EQ(kTabId, item->client_id.id); |
| 352 } | 360 } |
| 353 | 361 |
| 362 TEST_F(OfflinePageTabHelperTest, PageFor2GSlow) { |
| 363 SimulateHasNetworkConnectivity(true); |
| 364 TestingProfile* test_profile = profile(); |
| 365 UINetworkQualityEstimatorService* nqe_service = |
| 366 UINetworkQualityEstimatorServiceFactory::GetForProfile(test_profile); |
| 367 nqe_service->SetEffectiveConnectionTypeForTest( |
| 368 net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 369 |
| 370 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
| 371 clock->SetNow(base::Time::Now()); |
| 372 offline_page_tab_helper()->SetClockForTesting(base::WrapUnique(clock)); |
| 373 |
| 374 StartLoad(kTestPageUrl); |
| 375 // Gives a chance to run delayed task to do redirection. |
| 376 RunUntilIdle(); |
| 377 |
| 378 // This is not included in the field trial, so it should not cause a redirect. |
| 379 const OfflinePageItem* item = |
| 380 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); |
| 381 EXPECT_EQ(nullptr, item); |
| 382 |
| 383 // Enable the field trial for using 2G slow. |
| 384 base::FieldTrialList field_trial_list(nullptr); |
| 385 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 386 "DataReductionProxyClientSideExperimentsFieldTrial", |
| 387 "EnabledOfflinePages")); |
| 388 |
| 389 StartLoad(kTestPageUrl); |
| 390 // Gives a chance to run delayed task to do redirection. |
| 391 RunUntilIdle(); |
| 392 |
| 393 // This page should be fresh enough to cause a redirect. |
| 394 item = OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); |
| 395 EXPECT_EQ(offline_url(), item->GetOfflineURL()); |
| 396 EXPECT_EQ(online_url(), item->url); |
| 397 |
| 398 clock->Advance(base::TimeDelta::FromDays(2)); |
| 399 StartLoad(kTestPageUrl); |
| 400 // Gives a chance to run delayed task to do redirection. |
| 401 RunUntilIdle(); |
| 402 |
| 403 // This page should not be fresh enough to cause a redirect. |
| 404 item = OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); |
| 405 EXPECT_EQ(nullptr, item); |
| 406 } |
| 407 |
| 354 // This test saves another copy of page from Async Loading namespace | 408 // This test saves another copy of page from Async Loading namespace |
| 355 // and verifies it is redirected to it (as it is more recent). | 409 // and verifies it is redirected to it (as it is more recent). |
| 356 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineAsyncLoadedPageOnNoNetwork) { | 410 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineAsyncLoadedPageOnNoNetwork) { |
| 357 // Saves an offline page. | 411 // Saves an offline page. |
| 358 OfflinePageModel* model = | 412 OfflinePageModel* model = |
| 359 OfflinePageModelFactory::GetForBrowserContext(browser_context()); | 413 OfflinePageModelFactory::GetForBrowserContext(browser_context()); |
| 360 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( | 414 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| 361 kTestPageUrl, | 415 kTestPageUrl, |
| 362 base::FilePath(FILE_PATH_LITERAL("AsyncLoadedPage.mhtml")))); | 416 base::FilePath(FILE_PATH_LITERAL("AsyncLoadedPage.mhtml")))); |
| 363 | 417 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 376 RunUntilIdle(); | 430 RunUntilIdle(); |
| 377 | 431 |
| 378 const OfflinePageItem* item = | 432 const OfflinePageItem* item = |
| 379 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); | 433 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); |
| 380 EXPECT_EQ(expected_offline_id, item->offline_id); | 434 EXPECT_EQ(expected_offline_id, item->offline_id); |
| 381 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); | 435 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); |
| 382 EXPECT_EQ(kAsyncNamespace, item->client_id.name_space); | 436 EXPECT_EQ(kAsyncNamespace, item->client_id.name_space); |
| 383 } | 437 } |
| 384 | 438 |
| 385 } // namespace offline_pages | 439 } // namespace offline_pages |
| OLD | NEW |