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_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/time.h" | |
| 16 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 19 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 17 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | 20 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 18 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " | 21 #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_factory.h" | |
| 19 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 24 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 25 #include "chrome/test/base/testing_profile.h" | |
| 20 #include "components/offline_pages/client_namespace_constants.h" | 26 #include "components/offline_pages/client_namespace_constants.h" |
| 21 #include "components/offline_pages/offline_page_feature.h" | 27 #include "components/offline_pages/offline_page_feature.h" |
| 22 #include "components/offline_pages/offline_page_item.h" | 28 #include "components/offline_pages/offline_page_item.h" |
| 23 #include "components/offline_pages/offline_page_model.h" | 29 #include "components/offline_pages/offline_page_model.h" |
| 24 #include "components/offline_pages/offline_page_test_archiver.h" | 30 #include "components/offline_pages/offline_page_test_archiver.h" |
| 25 #include "components/offline_pages/offline_page_types.h" | 31 #include "components/offline_pages/offline_page_types.h" |
| 32 #include "components/previews/previews_experiments.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 12 matching lines...) Expand all Loading... | |
| 52 void set_online(bool online) { online_ = online; } | 60 void set_online(bool online) { online_ = online; } |
| 53 | 61 |
| 54 private: | 62 private: |
| 55 bool online_; | 63 bool online_; |
| 56 | 64 |
| 57 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); | 65 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); |
| 58 }; | 66 }; |
| 59 | 67 |
| 60 class TestDelegate : public OfflinePageTabHelper::Delegate { | 68 class TestDelegate : public OfflinePageTabHelper::Delegate { |
| 61 public: | 69 public: |
| 62 TestDelegate(bool has_tab_android, std::string tab_id) | 70 TestDelegate(bool has_tab_android, |
| 63 : has_tab_android_(has_tab_android), tab_id_(tab_id) {} | 71 std::string tab_id, |
| 72 base::SimpleTestClock* clock) | |
| 73 : clock_(clock), has_tab_android_(has_tab_android), tab_id_(tab_id) {} | |
| 64 ~TestDelegate() override {} | 74 ~TestDelegate() override {} |
| 65 | 75 |
| 66 // offline_pages::OfflinePageTabHelper::Delegate implementation: | 76 // offline_pages::OfflinePageTabHelper::Delegate implementation: |
| 67 bool GetTabId(content::WebContents* web_contents, | 77 bool GetTabId(content::WebContents* web_contents, |
| 68 std::string* tab_id) const override { | 78 std::string* tab_id) const override { |
| 69 if (has_tab_android_) | 79 if (has_tab_android_) |
| 70 *tab_id = tab_id_; | 80 *tab_id = tab_id_; |
| 71 return has_tab_android_; | 81 return has_tab_android_; |
| 72 } | 82 } |
| 73 | 83 |
| 84 base::Time Now() const override { return clock_->Now(); } | |
| 85 | |
| 74 private: | 86 private: |
| 87 base::SimpleTestClock* clock_; | |
| 75 bool has_tab_android_; | 88 bool has_tab_android_; |
| 76 std::string tab_id_; | 89 std::string tab_id_; |
| 77 }; | 90 }; |
| 78 | 91 |
| 79 } // namespace | 92 } // namespace |
| 80 | 93 |
| 81 class OfflinePageTabHelperTest : | 94 class OfflinePageTabHelperTest : |
| 82 public ChromeRenderViewHostTestHarness, | 95 public ChromeRenderViewHostTestHarness, |
| 83 public OfflinePageTestArchiver::Observer, | 96 public OfflinePageTestArchiver::Observer, |
| 84 public base::SupportsWeakPtr<OfflinePageTabHelperTest> { | 97 public base::SupportsWeakPtr<OfflinePageTabHelperTest> { |
| 85 public: | 98 public: |
| 86 OfflinePageTabHelperTest() | 99 OfflinePageTabHelperTest() |
| 87 : network_change_notifier_(new TestNetworkChangeNotifier()) {} | 100 : network_change_notifier_(new TestNetworkChangeNotifier()), |
| 101 clock_(new base::SimpleTestClock) {} | |
| 88 ~OfflinePageTabHelperTest() override {} | 102 ~OfflinePageTabHelperTest() override {} |
| 89 | 103 |
| 90 void SetUp() override; | 104 void SetUp() override; |
| 91 void TearDown() override; | 105 void TearDown() override; |
| 92 | 106 |
| 93 void RunUntilIdle(); | 107 void RunUntilIdle(); |
| 94 void SimulateHasNetworkConnectivity(bool has_connectivity); | 108 void SimulateHasNetworkConnectivity(bool has_connectivity); |
| 95 void StartLoad(const GURL& url); | 109 void StartLoad(const GURL& url); |
| 96 void FailLoad(const GURL& url); | 110 void FailLoad(const GURL& url); |
| 97 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( | 111 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( |
| 98 const GURL& url, | 112 const GURL& url, |
| 99 const base::FilePath& file_name); | 113 const base::FilePath& file_name); |
| 100 void OnSavePageDone(SavePageResult result, int64_t offline_id); | 114 void OnSavePageDone(SavePageResult result, int64_t offline_id); |
| 101 | 115 |
| 102 OfflinePageTabHelper* offline_page_tab_helper() const { | 116 OfflinePageTabHelper* offline_page_tab_helper() const { |
| 103 return offline_page_tab_helper_; | 117 return offline_page_tab_helper_; |
| 104 } | 118 } |
| 105 | 119 |
| 106 const GURL& online_url() const { return offline_page_item_->url; } | 120 const GURL& online_url() const { return offline_page_item_->url; } |
| 107 GURL offline_url() const { return offline_page_item_->GetOfflineURL(); } | 121 GURL offline_url() const { return offline_page_item_->GetOfflineURL(); } |
| 108 int64_t offline_id() const { return offline_page_item_->offline_id; } | 122 int64_t offline_id() const { return offline_page_item_->offline_id; } |
| 109 | 123 |
| 110 const base::HistogramTester& histograms() const { return histogram_tester_; } | 124 const base::HistogramTester& histograms() const { return histogram_tester_; } |
| 111 | 125 |
| 126 base::SimpleTestClock* clock() { return clock_.get(); } | |
| 127 | |
| 112 private: | 128 private: |
| 113 // OfflinePageTestArchiver::Observer implementation: | 129 // OfflinePageTestArchiver::Observer implementation: |
| 114 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; | 130 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; |
| 115 | 131 |
| 116 void OnGetPageByOfflineIdDone(const OfflinePageItem* result); | 132 void OnGetPageByOfflineIdDone(const OfflinePageItem* result); |
| 117 | 133 |
| 118 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; | 134 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; |
| 119 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. | 135 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. |
| 120 | 136 |
| 121 std::unique_ptr<OfflinePageItem> offline_page_item_; | 137 std::unique_ptr<OfflinePageItem> offline_page_item_; |
| 122 | 138 |
| 123 base::HistogramTester histogram_tester_; | 139 base::HistogramTester histogram_tester_; |
| 124 | 140 |
| 141 std::unique_ptr<base::SimpleTestClock> clock_; | |
| 142 | |
| 125 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest); | 143 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest); |
| 126 }; | 144 }; |
| 127 | 145 |
| 128 void OfflinePageTabHelperTest::SetUp() { | 146 void OfflinePageTabHelperTest::SetUp() { |
| 129 // Enables offline pages feature. | 147 // Enables offline pages feature. |
| 130 // TODO(jianli): Remove this once the feature is completely enabled. | 148 // TODO(jianli): Remove this once the feature is completely enabled. |
| 131 base::FeatureList::ClearInstanceForTesting(); | 149 base::FeatureList::ClearInstanceForTesting(); |
| 132 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | 150 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 133 feature_list->InitializeFromCommandLine( | 151 feature_list->InitializeFromCommandLine( |
| 134 offline_pages::kOfflineBookmarksFeature.name, ""); | 152 offline_pages::kOfflineBookmarksFeature.name, ""); |
| 135 base::FeatureList::SetInstance(std::move(feature_list)); | 153 base::FeatureList::SetInstance(std::move(feature_list)); |
| 136 | 154 |
| 137 // Creates a test web contents. | 155 // Creates a test web contents. |
| 138 content::RenderViewHostTestHarness::SetUp(); | 156 content::RenderViewHostTestHarness::SetUp(); |
| 139 OfflinePageTabHelper::CreateForWebContents(web_contents()); | 157 OfflinePageTabHelper::CreateForWebContents(web_contents()); |
| 140 offline_page_tab_helper_ = | 158 offline_page_tab_helper_ = |
| 141 OfflinePageTabHelper::FromWebContents(web_contents()); | 159 OfflinePageTabHelper::FromWebContents(web_contents()); |
| 142 offline_page_tab_helper_->SetDelegateForTesting( | 160 offline_page_tab_helper_->SetDelegateForTesting( |
| 143 base::MakeUnique<TestDelegate>(true, kTabId)); | 161 base::MakeUnique<TestDelegate>(true, kTabId, clock_.get())); |
| 144 | 162 |
| 145 // Sets up the factory for testing. | 163 // Sets up the factory for testing. |
| 146 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( | 164 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( |
| 147 browser_context(), BuildTestOfflinePageModel); | 165 browser_context(), BuildTestOfflinePageModel); |
| 148 RunUntilIdle(); | 166 RunUntilIdle(); |
| 149 | 167 |
| 150 // Saves an offline page. | 168 // Saves an offline page. |
| 151 OfflinePageModel* model = | 169 OfflinePageModel* model = |
| 152 OfflinePageModelFactory::GetForBrowserContext(browser_context()); | 170 OfflinePageModelFactory::GetForBrowserContext(browser_context()); |
| 153 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( | 171 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 344 RunUntilIdle(); | 362 RunUntilIdle(); |
| 345 | 363 |
| 346 const OfflinePageItem* item = | 364 const OfflinePageItem* item = |
| 347 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); | 365 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); |
| 348 EXPECT_EQ(expected_offline_id, item->offline_id); | 366 EXPECT_EQ(expected_offline_id, item->offline_id); |
| 349 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); | 367 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); |
| 350 EXPECT_EQ(kLastNNamespace, item->client_id.name_space); | 368 EXPECT_EQ(kLastNNamespace, item->client_id.name_space); |
| 351 EXPECT_EQ(kTabId, item->client_id.id); | 369 EXPECT_EQ(kTabId, item->client_id.id); |
| 352 } | 370 } |
| 353 | 371 |
| 372 TEST_F(OfflinePageTabHelperTest, PageFor2GSlow) { | |
| 373 SimulateHasNetworkConnectivity(true); | |
| 374 TestingProfile* test_profile = profile(); | |
| 375 UINetworkQualityEstimatorService* nqe_service = | |
| 376 UINetworkQualityEstimatorServiceFactory::GetForProfile(test_profile); | |
| 377 nqe_service->SetEffectiveConnectionTypeForTesting( | |
| 378 net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); | |
| 379 | |
| 380 clock()->SetNow(base::Time::Now()); | |
| 381 | |
| 382 StartLoad(kTestPageUrl); | |
| 383 // Gives a chance to run delayed task to do redirection. | |
| 384 RunUntilIdle(); | |
| 385 | |
| 386 // This is not included in the field trial, so it should not cause a redirect. | |
| 387 const OfflinePageItem* item = | |
| 388 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); | |
| 389 EXPECT_EQ(nullptr, item); | |
|
Lei Zhang
2016/07/28 20:41:52
Maybe just EXPECT_FALSE(item); ?
RyanSturm
2016/07/28 22:07:18
Done.
| |
| 390 | |
| 391 base::FieldTrialList field_trial_list(nullptr); | |
| 392 ASSERT_TRUE(previews::EnableOfflinePreviewsForTesting()); | |
| 393 | |
| 394 StartLoad(kTestPageUrl); | |
| 395 // Gives a chance to run delayed task to do redirection. | |
| 396 RunUntilIdle(); | |
| 397 | |
| 398 // This page should be fresh enough to cause a redirect. | |
| 399 item = OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); | |
| 400 EXPECT_EQ(offline_url(), item->GetOfflineURL()); | |
| 401 EXPECT_EQ(online_url(), item->url); | |
| 402 | |
| 403 clock()->Advance(base::TimeDelta::FromDays(2)); | |
| 404 StartLoad(kTestPageUrl); | |
| 405 // Gives a chance to run delayed task to do redirection. | |
| 406 RunUntilIdle(); | |
| 407 | |
| 408 // This page should not be fresh enough to cause a redirect. | |
| 409 item = OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); | |
| 410 EXPECT_EQ(nullptr, item); | |
| 411 } | |
| 412 | |
| 354 // This test saves another copy of page from Async Loading namespace | 413 // This test saves another copy of page from Async Loading namespace |
| 355 // and verifies it is redirected to it (as it is more recent). | 414 // and verifies it is redirected to it (as it is more recent). |
| 356 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineAsyncLoadedPageOnNoNetwork) { | 415 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineAsyncLoadedPageOnNoNetwork) { |
| 357 // Saves an offline page. | 416 // Saves an offline page. |
| 358 OfflinePageModel* model = | 417 OfflinePageModel* model = |
| 359 OfflinePageModelFactory::GetForBrowserContext(browser_context()); | 418 OfflinePageModelFactory::GetForBrowserContext(browser_context()); |
| 360 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( | 419 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| 361 kTestPageUrl, | 420 kTestPageUrl, |
| 362 base::FilePath(FILE_PATH_LITERAL("AsyncLoadedPage.mhtml")))); | 421 base::FilePath(FILE_PATH_LITERAL("AsyncLoadedPage.mhtml")))); |
| 363 | 422 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 376 RunUntilIdle(); | 435 RunUntilIdle(); |
| 377 | 436 |
| 378 const OfflinePageItem* item = | 437 const OfflinePageItem* item = |
| 379 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); | 438 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); |
| 380 EXPECT_EQ(expected_offline_id, item->offline_id); | 439 EXPECT_EQ(expected_offline_id, item->offline_id); |
| 381 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); | 440 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); |
| 382 EXPECT_EQ(kAsyncNamespace, item->client_id.name_space); | 441 EXPECT_EQ(kAsyncNamespace, item->client_id.name_space); |
| 383 } | 442 } |
| 384 | 443 |
| 385 } // namespace offline_pages | 444 } // namespace offline_pages |
| OLD | NEW |