| 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/metrics/field_trial.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/test/histogram_tester.h" | 17 #include "base/test/histogram_tester.h" |
| 17 #include "base/test/simple_test_clock.h" | 18 #include "base/test/simple_test_clock.h" |
| 18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 19 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 20 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 20 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | 21 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 21 #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
" |
| 22 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service.h" | 23 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service.h" |
| 23 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service_factory.h" | 24 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service_factory.h" |
| 24 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 25 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 25 #include "chrome/test/base/testing_profile.h" | 26 #include "chrome/test/base/testing_profile.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 38 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
| 39 | 40 |
| 40 namespace offline_pages { | 41 namespace offline_pages { |
| 41 | 42 |
| 42 namespace { | 43 namespace { |
| 43 | 44 |
| 44 const GURL kTestPageUrl("http://test.org/page1"); | 45 const GURL kTestPageUrl("http://test.org/page1"); |
| 45 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); | 46 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); |
| 46 const int64_t kTestFileSize = 876543LL; | 47 const int64_t kTestFileSize = 876543LL; |
| 47 const char kRedirectResultHistogram[] = "OfflinePages.RedirectResult"; | 48 const char kRedirectResultHistogram[] = "OfflinePages.RedirectResult"; |
| 48 const char kTabId[] = "42"; | 49 const int kTabId = 42; |
| 49 | 50 |
| 50 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { | 51 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { |
| 51 public: | 52 public: |
| 52 TestNetworkChangeNotifier() : online_(true) {} | 53 TestNetworkChangeNotifier() : online_(true) {} |
| 53 | 54 |
| 54 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() | 55 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() |
| 55 const override { | 56 const override { |
| 56 return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN | 57 return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN |
| 57 : net::NetworkChangeNotifier::CONNECTION_NONE; | 58 : net::NetworkChangeNotifier::CONNECTION_NONE; |
| 58 } | 59 } |
| 59 | 60 |
| 60 void set_online(bool online) { online_ = online; } | 61 void set_online(bool online) { online_ = online; } |
| 61 | 62 |
| 62 private: | 63 private: |
| 63 bool online_; | 64 bool online_; |
| 64 | 65 |
| 65 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); | 66 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); |
| 66 }; | 67 }; |
| 67 | 68 |
| 68 class TestDelegate : public OfflinePageTabHelper::Delegate { | 69 class TestDelegate : public OfflinePageTabHelper::Delegate { |
| 69 public: | 70 public: |
| 70 TestDelegate(bool has_tab_android, | 71 TestDelegate(bool has_tab_android, |
| 71 std::string tab_id, | 72 int tab_id, |
| 72 base::SimpleTestClock* clock) | 73 base::SimpleTestClock* clock) |
| 73 : clock_(clock), has_tab_android_(has_tab_android), tab_id_(tab_id) {} | 74 : clock_(clock), has_tab_android_(has_tab_android), tab_id_(tab_id) {} |
| 74 ~TestDelegate() override {} | 75 ~TestDelegate() override {} |
| 75 | 76 |
| 76 // offline_pages::OfflinePageTabHelper::Delegate implementation: | 77 // offline_pages::OfflinePageTabHelper::Delegate implementation: |
| 77 bool GetTabId(content::WebContents* web_contents, | 78 bool GetTabId(content::WebContents* web_contents, |
| 78 std::string* tab_id) const override { | 79 int* tab_id) const override { |
| 79 if (has_tab_android_) | 80 if (has_tab_android_) |
| 80 *tab_id = tab_id_; | 81 *tab_id = tab_id_; |
| 81 return has_tab_android_; | 82 return has_tab_android_; |
| 82 } | 83 } |
| 83 | 84 |
| 84 base::Time Now() const override { return clock_->Now(); } | 85 base::Time Now() const override { return clock_->Now(); } |
| 85 | 86 |
| 86 private: | 87 private: |
| 87 base::SimpleTestClock* clock_; | 88 base::SimpleTestClock* clock_; |
| 88 bool has_tab_android_; | 89 bool has_tab_android_; |
| 89 std::string tab_id_; | 90 int tab_id_; |
| 90 }; | 91 }; |
| 91 | 92 |
| 92 } // namespace | 93 } // namespace |
| 93 | 94 |
| 94 class OfflinePageTabHelperTest : | 95 class OfflinePageTabHelperTest : |
| 95 public ChromeRenderViewHostTestHarness, | 96 public ChromeRenderViewHostTestHarness, |
| 96 public OfflinePageTestArchiver::Observer, | 97 public OfflinePageTestArchiver::Observer, |
| 97 public base::SupportsWeakPtr<OfflinePageTabHelperTest> { | 98 public base::SupportsWeakPtr<OfflinePageTabHelperTest> { |
| 98 public: | 99 public: |
| 99 OfflinePageTabHelperTest() | 100 OfflinePageTabHelperTest() |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 // test is related to |kTabId|, as it is saved in the latest moment and can be | 334 // test is related to |kTabId|, as it is saved in the latest moment and can be |
| 334 // used in the current tab. | 335 // used in the current tab. |
| 335 TEST_F(OfflinePageTabHelperTest, SelectBestPageForCurrentTab) { | 336 TEST_F(OfflinePageTabHelperTest, SelectBestPageForCurrentTab) { |
| 336 // Saves an offline page. | 337 // Saves an offline page. |
| 337 OfflinePageModel* model = | 338 OfflinePageModel* model = |
| 338 OfflinePageModelFactory::GetForBrowserContext(browser_context()); | 339 OfflinePageModelFactory::GetForBrowserContext(browser_context()); |
| 339 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( | 340 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| 340 kTestPageUrl, base::FilePath(FILE_PATH_LITERAL("page2.mhtml")))); | 341 kTestPageUrl, base::FilePath(FILE_PATH_LITERAL("page2.mhtml")))); |
| 341 | 342 |
| 342 // We expect this copy to be used later. | 343 // We expect this copy to be used later. |
| 343 ClientId client_id(kLastNNamespace, kTabId); | 344 ClientId client_id(kLastNNamespace, base::IntToString(kTabId)); |
| 344 model->SavePage( | 345 model->SavePage( |
| 345 kTestPageUrl, client_id, std::move(archiver), | 346 kTestPageUrl, client_id, std::move(archiver), |
| 346 base::Bind(&OfflinePageTabHelperTest::OnSavePageDone, AsWeakPtr())); | 347 base::Bind(&OfflinePageTabHelperTest::OnSavePageDone, AsWeakPtr())); |
| 347 RunUntilIdle(); | 348 RunUntilIdle(); |
| 348 const int64_t expected_offline_id = offline_id(); | 349 const int64_t expected_offline_id = offline_id(); |
| 349 const GURL expected_offline_url = offline_url(); | 350 const GURL expected_offline_url = offline_url(); |
| 350 | 351 |
| 351 archiver = BuildArchiver(kTestPageUrl, | 352 archiver = BuildArchiver(kTestPageUrl, |
| 352 base::FilePath(FILE_PATH_LITERAL("page3.html"))); | 353 base::FilePath(FILE_PATH_LITERAL("page3.html"))); |
| 353 client_id.id = "39"; | 354 client_id.id = "39"; |
| 354 model->SavePage( | 355 model->SavePage( |
| 355 kTestPageUrl, client_id, std::move(archiver), | 356 kTestPageUrl, client_id, std::move(archiver), |
| 356 base::Bind(&OfflinePageTabHelperTest::OnSavePageDone, AsWeakPtr())); | 357 base::Bind(&OfflinePageTabHelperTest::OnSavePageDone, AsWeakPtr())); |
| 357 RunUntilIdle(); | 358 RunUntilIdle(); |
| 358 | 359 |
| 359 SimulateHasNetworkConnectivity(false); | 360 SimulateHasNetworkConnectivity(false); |
| 360 StartLoad(kTestPageUrl); | 361 StartLoad(kTestPageUrl); |
| 361 // Gives a chance to run delayed task to do redirection. | 362 // Gives a chance to run delayed task to do redirection. |
| 362 RunUntilIdle(); | 363 RunUntilIdle(); |
| 363 | 364 |
| 364 const OfflinePageItem* item = | 365 const OfflinePageItem* item = |
| 365 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); | 366 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); |
| 366 EXPECT_EQ(expected_offline_id, item->offline_id); | 367 EXPECT_EQ(expected_offline_id, item->offline_id); |
| 367 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); | 368 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); |
| 368 EXPECT_EQ(kLastNNamespace, item->client_id.name_space); | 369 EXPECT_EQ(kLastNNamespace, item->client_id.name_space); |
| 369 EXPECT_EQ(kTabId, item->client_id.id); | 370 EXPECT_EQ(base::IntToString(kTabId), item->client_id.id); |
| 370 } | 371 } |
| 371 | 372 |
| 372 TEST_F(OfflinePageTabHelperTest, PageFor2GSlow) { | 373 TEST_F(OfflinePageTabHelperTest, PageFor2GSlow) { |
| 373 SimulateHasNetworkConnectivity(true); | 374 SimulateHasNetworkConnectivity(true); |
| 374 TestingProfile* test_profile = profile(); | 375 TestingProfile* test_profile = profile(); |
| 375 UINetworkQualityEstimatorService* nqe_service = | 376 UINetworkQualityEstimatorService* nqe_service = |
| 376 UINetworkQualityEstimatorServiceFactory::GetForProfile(test_profile); | 377 UINetworkQualityEstimatorServiceFactory::GetForProfile(test_profile); |
| 377 nqe_service->SetEffectiveConnectionTypeForTesting( | 378 nqe_service->SetEffectiveConnectionTypeForTesting( |
| 378 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); | 379 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 379 | 380 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 // and verifies it is redirected to it (as it is more recent). | 415 // and verifies it is redirected to it (as it is more recent). |
| 415 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineAsyncLoadedPageOnNoNetwork) { | 416 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineAsyncLoadedPageOnNoNetwork) { |
| 416 // Saves an offline page. | 417 // Saves an offline page. |
| 417 OfflinePageModel* model = | 418 OfflinePageModel* model = |
| 418 OfflinePageModelFactory::GetForBrowserContext(browser_context()); | 419 OfflinePageModelFactory::GetForBrowserContext(browser_context()); |
| 419 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( | 420 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| 420 kTestPageUrl, | 421 kTestPageUrl, |
| 421 base::FilePath(FILE_PATH_LITERAL("AsyncLoadedPage.mhtml")))); | 422 base::FilePath(FILE_PATH_LITERAL("AsyncLoadedPage.mhtml")))); |
| 422 | 423 |
| 423 // We expect this Async Loading Namespace copy to be used. | 424 // We expect this Async Loading Namespace copy to be used. |
| 424 ClientId client_id(kAsyncNamespace, kTabId); | 425 ClientId client_id(kAsyncNamespace, base::IntToString(kTabId)); |
| 425 model->SavePage( | 426 model->SavePage( |
| 426 kTestPageUrl, client_id, std::move(archiver), | 427 kTestPageUrl, client_id, std::move(archiver), |
| 427 base::Bind(&OfflinePageTabHelperTest::OnSavePageDone, AsWeakPtr())); | 428 base::Bind(&OfflinePageTabHelperTest::OnSavePageDone, AsWeakPtr())); |
| 428 RunUntilIdle(); | 429 RunUntilIdle(); |
| 429 const int64_t expected_offline_id = offline_id(); | 430 const int64_t expected_offline_id = offline_id(); |
| 430 const GURL expected_offline_url = offline_url(); | 431 const GURL expected_offline_url = offline_url(); |
| 431 | 432 |
| 432 SimulateHasNetworkConnectivity(false); | 433 SimulateHasNetworkConnectivity(false); |
| 433 StartLoad(kTestPageUrl); | 434 StartLoad(kTestPageUrl); |
| 434 // Gives a chance to run delayed task to do redirection. | 435 // Gives a chance to run delayed task to do redirection. |
| 435 RunUntilIdle(); | 436 RunUntilIdle(); |
| 436 | 437 |
| 437 const OfflinePageItem* item = | 438 const OfflinePageItem* item = |
| 438 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); | 439 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); |
| 439 EXPECT_EQ(expected_offline_id, item->offline_id); | 440 EXPECT_EQ(expected_offline_id, item->offline_id); |
| 440 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); | 441 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); |
| 441 EXPECT_EQ(kAsyncNamespace, item->client_id.name_space); | 442 EXPECT_EQ(kAsyncNamespace, item->client_id.name_space); |
| 442 } | 443 } |
| 443 | 444 |
| 444 } // namespace offline_pages | 445 } // namespace offline_pages |
| OLD | NEW |