| 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 30e3eb86f6dce3083aac698977f55c65a3562dbd..ba76871c70dda2ef3b1bfafef54dbe70a5862245 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
|
| @@ -9,6 +9,7 @@
|
| #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/run_loop.h"
|
| #include "base/test/histogram_tester.h"
|
| @@ -39,6 +40,7 @@ const char kBadNetworkHistogram[] = "OfflinePages.ShowOfflinePageOnBadNetwork";
|
| const char kRedirectToOfflineHistogram[] =
|
| "OfflinePages.RedirectToOfflineCount";
|
| const char kRedirectToOnlineHistogram[] = "OfflinePages.RedirectToOnlineCount";
|
| +const char kTabId[] = "42";
|
|
|
| class TestNetworkChangeNotifier : public net::NetworkChangeNotifier {
|
| public:
|
| @@ -58,6 +60,25 @@ class TestNetworkChangeNotifier : public net::NetworkChangeNotifier {
|
| 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() 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_;
|
| + }
|
| +
|
| + private:
|
| + bool has_tab_android_;
|
| + std::string tab_id_;
|
| +};
|
| +
|
| } // namespace
|
|
|
| class OfflinePageTabHelperTest :
|
| @@ -76,6 +97,10 @@ class OfflinePageTabHelperTest :
|
| void SimulateHasNetworkConnectivity(bool has_connectivity);
|
| void StartLoad(const GURL& url);
|
| void FailLoad(const GURL& url);
|
| + std::unique_ptr<OfflinePageTestArchiver> BuildArchiver(
|
| + const GURL& url,
|
| + const base::FilePath& file_name);
|
| + void OnSavePageDone(SavePageResult result, int64_t offline_id);
|
|
|
| OfflinePageTabHelper* offline_page_tab_helper() const {
|
| return offline_page_tab_helper_;
|
| @@ -83,6 +108,7 @@ class OfflinePageTabHelperTest :
|
|
|
| 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_; }
|
|
|
| @@ -90,10 +116,6 @@ class OfflinePageTabHelperTest :
|
| // OfflinePageTestArchiver::Observer implementation:
|
| void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override;
|
|
|
| - std::unique_ptr<OfflinePageTestArchiver> BuildArchiver(
|
| - const GURL& url,
|
| - const base::FilePath& file_name);
|
| - void OnSavePageDone(SavePageResult result, int64_t offline_id);
|
| void OnGetPageByOfflineIdDone(const OfflinePageItem* result);
|
|
|
| std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_;
|
| @@ -120,6 +142,8 @@ void OfflinePageTabHelperTest::SetUp() {
|
| OfflinePageTabHelper::CreateForWebContents(web_contents());
|
| offline_page_tab_helper_ =
|
| OfflinePageTabHelper::FromWebContents(web_contents());
|
| + offline_page_tab_helper_->SetDelegateForTesting(
|
| + base::MakeUnique<TestDelegate>(true, kTabId));
|
|
|
| // Sets up the factory for testing.
|
| OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse(
|
| @@ -167,10 +191,6 @@ void OfflinePageTabHelperTest::FailLoad(const GURL& url) {
|
| RunUntilIdle();
|
| }
|
|
|
| -void OfflinePageTabHelperTest::SetLastPathCreatedByArchiver(
|
| - const base::FilePath& file_path) {
|
| -}
|
| -
|
| std::unique_ptr<OfflinePageTestArchiver>
|
| OfflinePageTabHelperTest::BuildArchiver(const GURL& url,
|
| const base::FilePath& file_name) {
|
| @@ -190,6 +210,9 @@ void OfflinePageTabHelperTest::OnSavePageDone(SavePageResult result,
|
| AsWeakPtr()));
|
| }
|
|
|
| +void OfflinePageTabHelperTest::SetLastPathCreatedByArchiver(
|
| + const base::FilePath& file_path) {}
|
| +
|
| void OfflinePageTabHelperTest::OnGetPageByOfflineIdDone(
|
| const OfflinePageItem* result) {
|
| DCHECK(result);
|
| @@ -282,4 +305,44 @@ TEST_F(OfflinePageTabHelperTest, NewNavigationCancelsPendingRedirects) {
|
| histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 0);
|
| histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0);
|
| }
|
| +
|
| +// This test saves 3 pages (one in setup and 2 in test). The most appropriate
|
| +// test is related to |kTabId|, as it is saved in the latest moment and can be
|
| +// used in the current tab.
|
| +TEST_F(OfflinePageTabHelperTest, SelectBestPageForCurrentTab) {
|
| + // Saves an offline page.
|
| + OfflinePageModel* model =
|
| + OfflinePageModelFactory::GetForBrowserContext(browser_context());
|
| + std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver(
|
| + kTestPageUrl, base::FilePath(FILE_PATH_LITERAL("page2.mhtml"))));
|
| +
|
| + // We expect this copy to be used later.
|
| + ClientId client_id(kLastNNamespace, kTabId);
|
| + model->SavePage(
|
| + kTestPageUrl, client_id, std::move(archiver),
|
| + base::Bind(&OfflinePageTabHelperTest::OnSavePageDone, AsWeakPtr()));
|
| + RunUntilIdle();
|
| + const int64_t expected_offline_id = offline_id();
|
| + const GURL expected_offline_url = offline_url();
|
| +
|
| + archiver = BuildArchiver(kTestPageUrl,
|
| + base::FilePath(FILE_PATH_LITERAL("page3.html")));
|
| + client_id.id = "39";
|
| + model->SavePage(
|
| + kTestPageUrl, client_id, std::move(archiver),
|
| + base::Bind(&OfflinePageTabHelperTest::OnSavePageDone, AsWeakPtr()));
|
| + RunUntilIdle();
|
| +
|
| + SimulateHasNetworkConnectivity(false);
|
| + StartLoad(kTestPageUrl);
|
| + // Gives a chance to run delayed task to do redirection.
|
| + 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);
|
| +}
|
| } // namespace offline_pages
|
|
|