| Index: chrome/browser/android/offline_pages/offline_page_tab_helper_unittest.cc
|
| diff --git a/chrome/browser/android/offline_pages/offline_page_tab_helper_unittest.cc b/chrome/browser/android/offline_pages/offline_page_tab_helper_unittest.cc
|
| index 919ddbff6b14278ddf244b520feefa599dd95dc7..80f21854184c91432b2fd574a44c95c50ecc551d 100644
|
| --- a/chrome/browser/android/offline_pages/offline_page_tab_helper_unittest.cc
|
| +++ b/chrome/browser/android/offline_pages/offline_page_tab_helper_unittest.cc
|
| @@ -4,36 +4,44 @@
|
|
|
| #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h"
|
|
|
| #include <memory>
|
|
|
| #include "base/bind.h"
|
| #include "base/feature_list.h"
|
| #include "base/files/file_path.h"
|
| #include "base/memory/ptr_util.h"
|
| #include "base/memory/weak_ptr.h"
|
| +#include "base/metrics/field_trial.h"
|
| #include "base/run_loop.h"
|
| #include "base/test/histogram_tester.h"
|
| +#include "base/test/simple_test_clock.h"
|
| +#include "base/time/time.h"
|
| #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
|
| #include "chrome/browser/android/offline_pages/offline_page_utils.h"
|
| #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h"
|
| +#include "chrome/browser/net/nqe/ui_network_quality_estimator_service.h"
|
| +#include "chrome/browser/net/nqe/ui_network_quality_estimator_service_factory.h"
|
| #include "chrome/test/base/chrome_render_view_host_test_harness.h"
|
| +#include "chrome/test/base/testing_profile.h"
|
| #include "components/offline_pages/client_namespace_constants.h"
|
| #include "components/offline_pages/offline_page_feature.h"
|
| #include "components/offline_pages/offline_page_item.h"
|
| #include "components/offline_pages/offline_page_model.h"
|
| #include "components/offline_pages/offline_page_test_archiver.h"
|
| #include "components/offline_pages/offline_page_types.h"
|
| +#include "components/previews/previews_experiments.h"
|
| #include "content/public/browser/navigation_entry.h"
|
| #include "content/public/browser/web_contents.h"
|
| #include "net/base/net_errors.h"
|
| #include "net/base/network_change_notifier.h"
|
| +#include "net/nqe/network_quality_estimator.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| namespace offline_pages {
|
|
|
| namespace {
|
|
|
| const GURL kTestPageUrl("http://test.org/page1");
|
| const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234");
|
| const int64_t kTestFileSize = 876543LL;
|
| const char kRedirectResultHistogram[] = "OfflinePages.RedirectResult";
|
| @@ -52,46 +60,52 @@ class TestNetworkChangeNotifier : public net::NetworkChangeNotifier {
|
| void set_online(bool online) { online_ = online; }
|
|
|
| private:
|
| bool online_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier);
|
| };
|
|
|
| class TestDelegate : public OfflinePageTabHelper::Delegate {
|
| public:
|
| - TestDelegate(bool has_tab_android, std::string tab_id)
|
| - : has_tab_android_(has_tab_android), tab_id_(tab_id) {}
|
| + TestDelegate(bool has_tab_android,
|
| + std::string tab_id,
|
| + base::SimpleTestClock* clock)
|
| + : clock_(clock), has_tab_android_(has_tab_android), tab_id_(tab_id) {}
|
| ~TestDelegate() override {}
|
|
|
| // offline_pages::OfflinePageTabHelper::Delegate implementation:
|
| bool GetTabId(content::WebContents* web_contents,
|
| std::string* tab_id) const override {
|
| if (has_tab_android_)
|
| *tab_id = tab_id_;
|
| return has_tab_android_;
|
| }
|
|
|
| + base::Time Now() const override { return clock_->Now(); }
|
| +
|
| private:
|
| + base::SimpleTestClock* clock_;
|
| bool has_tab_android_;
|
| std::string tab_id_;
|
| };
|
|
|
| } // namespace
|
|
|
| class OfflinePageTabHelperTest :
|
| public ChromeRenderViewHostTestHarness,
|
| public OfflinePageTestArchiver::Observer,
|
| public base::SupportsWeakPtr<OfflinePageTabHelperTest> {
|
| public:
|
| OfflinePageTabHelperTest()
|
| - : network_change_notifier_(new TestNetworkChangeNotifier()) {}
|
| + : network_change_notifier_(new TestNetworkChangeNotifier()),
|
| + clock_(new base::SimpleTestClock) {}
|
| ~OfflinePageTabHelperTest() override {}
|
|
|
| void SetUp() override;
|
| void TearDown() override;
|
|
|
| void RunUntilIdle();
|
| void SimulateHasNetworkConnectivity(bool has_connectivity);
|
| void StartLoad(const GURL& url);
|
| void FailLoad(const GURL& url);
|
| std::unique_ptr<OfflinePageTestArchiver> BuildArchiver(
|
| @@ -102,52 +116,56 @@ class OfflinePageTabHelperTest :
|
| OfflinePageTabHelper* offline_page_tab_helper() const {
|
| return offline_page_tab_helper_;
|
| }
|
|
|
| const GURL& online_url() const { return offline_page_item_->url; }
|
| GURL offline_url() const { return offline_page_item_->GetOfflineURL(); }
|
| int64_t offline_id() const { return offline_page_item_->offline_id; }
|
|
|
| const base::HistogramTester& histograms() const { return histogram_tester_; }
|
|
|
| + base::SimpleTestClock* clock() { return clock_.get(); }
|
| +
|
| private:
|
| // OfflinePageTestArchiver::Observer implementation:
|
| void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override;
|
|
|
| void OnGetPageByOfflineIdDone(const OfflinePageItem* result);
|
|
|
| std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_;
|
| OfflinePageTabHelper* offline_page_tab_helper_; // Not owned.
|
|
|
| std::unique_ptr<OfflinePageItem> offline_page_item_;
|
|
|
| base::HistogramTester histogram_tester_;
|
|
|
| + std::unique_ptr<base::SimpleTestClock> clock_;
|
| +
|
| DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest);
|
| };
|
|
|
| void OfflinePageTabHelperTest::SetUp() {
|
| // Enables offline pages feature.
|
| // TODO(jianli): Remove this once the feature is completely enabled.
|
| base::FeatureList::ClearInstanceForTesting();
|
| std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
|
| feature_list->InitializeFromCommandLine(
|
| offline_pages::kOfflineBookmarksFeature.name, "");
|
| base::FeatureList::SetInstance(std::move(feature_list));
|
|
|
| // Creates a test web contents.
|
| content::RenderViewHostTestHarness::SetUp();
|
| OfflinePageTabHelper::CreateForWebContents(web_contents());
|
| offline_page_tab_helper_ =
|
| OfflinePageTabHelper::FromWebContents(web_contents());
|
| offline_page_tab_helper_->SetDelegateForTesting(
|
| - base::MakeUnique<TestDelegate>(true, kTabId));
|
| + base::MakeUnique<TestDelegate>(true, kTabId, clock_.get()));
|
|
|
| // Sets up the factory for testing.
|
| OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse(
|
| browser_context(), BuildTestOfflinePageModel);
|
| RunUntilIdle();
|
|
|
| // Saves an offline page.
|
| OfflinePageModel* model =
|
| OfflinePageModelFactory::GetForBrowserContext(browser_context());
|
| std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver(
|
| @@ -344,20 +362,61 @@ TEST_F(OfflinePageTabHelperTest, SelectBestPageForCurrentTab) {
|
| RunUntilIdle();
|
|
|
| const OfflinePageItem* item =
|
| OfflinePageUtils::GetOfflinePageFromWebContents(web_contents());
|
| EXPECT_EQ(expected_offline_id, item->offline_id);
|
| EXPECT_EQ(expected_offline_url, item->GetOfflineURL());
|
| EXPECT_EQ(kLastNNamespace, item->client_id.name_space);
|
| EXPECT_EQ(kTabId, item->client_id.id);
|
| }
|
|
|
| +TEST_F(OfflinePageTabHelperTest, PageFor2GSlow) {
|
| + SimulateHasNetworkConnectivity(true);
|
| + TestingProfile* test_profile = profile();
|
| + UINetworkQualityEstimatorService* nqe_service =
|
| + UINetworkQualityEstimatorServiceFactory::GetForProfile(test_profile);
|
| + nqe_service->SetEffectiveConnectionTypeForTesting(
|
| + net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
|
| +
|
| + clock()->SetNow(base::Time::Now());
|
| +
|
| + StartLoad(kTestPageUrl);
|
| + // Gives a chance to run delayed task to do redirection.
|
| + RunUntilIdle();
|
| +
|
| + // This is not included in the field trial, so it should not cause a redirect.
|
| + const OfflinePageItem* item =
|
| + OfflinePageUtils::GetOfflinePageFromWebContents(web_contents());
|
| + EXPECT_FALSE(item);
|
| +
|
| + base::FieldTrialList field_trial_list(nullptr);
|
| + ASSERT_TRUE(previews::EnableOfflinePreviewsForTesting());
|
| +
|
| + StartLoad(kTestPageUrl);
|
| + // Gives a chance to run delayed task to do redirection.
|
| + RunUntilIdle();
|
| +
|
| + // This page should be fresh enough to cause a redirect.
|
| + item = OfflinePageUtils::GetOfflinePageFromWebContents(web_contents());
|
| + EXPECT_EQ(offline_url(), item->GetOfflineURL());
|
| + EXPECT_EQ(online_url(), item->url);
|
| +
|
| + clock()->Advance(base::TimeDelta::FromDays(2));
|
| + StartLoad(kTestPageUrl);
|
| + // Gives a chance to run delayed task to do redirection.
|
| + RunUntilIdle();
|
| +
|
| + // This page should not be fresh enough to cause a redirect.
|
| + item = OfflinePageUtils::GetOfflinePageFromWebContents(web_contents());
|
| + EXPECT_EQ(nullptr, item);
|
| +}
|
| +
|
| // This test saves another copy of page from Async Loading namespace
|
| // and verifies it is redirected to it (as it is more recent).
|
| TEST_F(OfflinePageTabHelperTest, SwitchToOfflineAsyncLoadedPageOnNoNetwork) {
|
| // Saves an offline page.
|
| OfflinePageModel* model =
|
| OfflinePageModelFactory::GetForBrowserContext(browser_context());
|
| std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver(
|
| kTestPageUrl,
|
| base::FilePath(FILE_PATH_LITERAL("AsyncLoadedPage.mhtml"))));
|
|
|
|
|