Chromium Code Reviews| 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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ee5a663aa803963bb72992eb364e185b52579471 |
| --- /dev/null |
| +++ b/chrome/browser/android/offline_pages/offline_page_tab_helper_unittest.cc |
| @@ -0,0 +1,197 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "base/command_line.h" |
| +#include "base/files/file_path.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| +#include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" |
| +#include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h" |
| +#include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "components/offline_pages/offline_page_item.h" |
| +#include "components/offline_pages/offline_page_model.h" |
| +#include "components/offline_pages/offline_page_switches.h" |
| +#include "components/offline_pages/offline_page_test_archiver.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 "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace offline_pages { |
| + |
| +namespace { |
| + |
| +const GURL kTestPageUrl("http://test.org/page1"); |
| +const int64_t kTestPageBookmarkId = 1234; |
| +const int64_t kTestFileSize = 876543LL; |
| + |
| +class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { |
| + public: |
| + TestNetworkChangeNotifier() : online_(true) {} |
| + |
| + net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() |
| + const override { |
| + return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN |
| + : net::NetworkChangeNotifier::CONNECTION_NONE; |
| + } |
| + |
| + void set_online(bool online) { online_ = online; } |
| + |
| + private: |
| + bool online_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); |
| +}; |
| + |
| +} // namespace |
| + |
| +class OfflinePageTabHelperTest : |
| + public ChromeRenderViewHostTestHarness, |
| + public OfflinePageTestArchiver::Observer, |
| + public base::SupportsWeakPtr<OfflinePageTabHelperTest> { |
| + public: |
| + OfflinePageTabHelperTest(); |
| + ~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); |
| + |
| + OfflinePageTabHelper* offline_page_tab_helper() const { |
| + return offline_page_tab_helper_; |
| + } |
| + |
| + private: |
| + // OfflinePageTestArchiver::Observer implementation: |
| + void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; |
| + |
| + scoped_ptr<OfflinePageTestArchiver> BuildArchiver( |
| + const GURL& url, |
| + const base::FilePath& file_name); |
| + void OnSavePageDone(OfflinePageModel::SavePageResult result); |
| + |
| + scoped_ptr<TestNetworkChangeNotifier> network_change_notifier_; |
| + OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. |
| + |
| + DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest); |
| +}; |
| + |
| +OfflinePageTabHelperTest::OfflinePageTabHelperTest() |
|
nasko
2016/02/26 00:12:19
nit: Since this is a test class, you can inline th
jianli
2016/02/26 03:03:22
Done.
|
| + : network_change_notifier_(new TestNetworkChangeNotifier()) { |
| +} |
| + |
| +OfflinePageTabHelperTest::~OfflinePageTabHelperTest() { |
| +} |
| + |
| +void OfflinePageTabHelperTest::SetUp() { |
| + // Creates a test web contents. |
| + content::RenderViewHostTestHarness::SetUp(); |
| + OfflinePageTabHelper::CreateForWebContents(web_contents()); |
| + offline_page_tab_helper_ = |
| + OfflinePageTabHelper::FromWebContents(web_contents()); |
| + |
| + // Enables offline pages feature. |
| + base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| + switches::kEnableOfflinePages); |
| + |
| + // Sets up the factory for testing. |
| + OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( |
| + profile(), BuildTestOfflinePageModel); |
| + RunUntilIdle(); |
| + |
| + // Saves an offline page. |
| + OfflinePageModel* model = |
| + OfflinePageModelFactory::GetForBrowserContext(profile()); |
| + scoped_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| + kTestPageUrl, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); |
| + model->SavePage( |
| + kTestPageUrl, kTestPageBookmarkId, std::move(archiver), |
| + base::Bind(&OfflinePageTabHelperTest::OnSavePageDone, AsWeakPtr())); |
| + RunUntilIdle(); |
| +} |
| + |
| +void OfflinePageTabHelperTest::TearDown() { |
| + content::RenderViewHostTestHarness::TearDown(); |
| +} |
| + |
| +void OfflinePageTabHelperTest::RunUntilIdle() { |
| + base::RunLoop().RunUntilIdle(); |
| +} |
| + |
| +void OfflinePageTabHelperTest::SimulateHasNetworkConnectivity(bool online) { |
| + network_change_notifier_->set_online(online); |
| + RunUntilIdle(); |
|
nasko
2016/02/26 00:12:19
Why do you need to call RunUntilIdle() here? It on
jianli
2016/02/26 03:03:22
Not needed. Removed.
|
| +} |
| + |
| +void OfflinePageTabHelperTest::StartLoad(const GURL& url) { |
| + controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, |
| + std::string()); |
| + offline_page_tab_helper()->DidStartProvisionalLoadForFrame( |
|
nasko
2016/02/26 00:12:19
This will not be compatible with PlzNavigate. You
jianli
2016/02/26 03:03:22
Done.
|
| + main_rfh(), url, false, false); |
| + RunUntilIdle(); |
|
nasko
2016/02/26 00:12:19
Why is this needed?
jianli
2016/02/26 03:03:22
Removed.
|
| +} |
| + |
| +void OfflinePageTabHelperTest::FailLoad(const GURL& url) { |
| + offline_page_tab_helper()->DidFailProvisionalLoad( |
|
nasko
2016/02/26 00:12:19
Use main_test_rfh()->SimulateNavigationError inste
jianli
2016/02/26 03:03:22
Done.
|
| + main_rfh(), url, net::ERR_INTERNET_DISCONNECTED, base::string16(), false); |
| + RunUntilIdle(); |
| +} |
| + |
| +void OfflinePageTabHelperTest::SetLastPathCreatedByArchiver( |
| + const base::FilePath& file_path) { |
| +} |
|
nasko
2016/02/26 00:12:19
Why do we need an empty method?
jianli
2016/02/26 03:03:22
This is needed for OfflinePageTestArchiver::Observ
|
| + |
| +scoped_ptr<OfflinePageTestArchiver> OfflinePageTabHelperTest::BuildArchiver( |
| + const GURL& url, |
| + const base::FilePath& file_name) { |
| + scoped_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( |
| + this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, |
| + kTestFileSize, base::ThreadTaskRunnerHandle::Get())); |
| + archiver->set_filename(file_name); |
| + return archiver; |
| +} |
| + |
| +void OfflinePageTabHelperTest::OnSavePageDone( |
| + OfflinePageModel::SavePageResult result) { |
| +} |
|
nasko
2016/02/26 00:12:19
Why do we need an empty method?
jianli
2016/02/26 03:03:22
This is needed to call model->SavePage.
|
| + |
| +TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOffline) { |
| + SimulateHasNetworkConnectivity(true); |
| + |
| + OfflinePageModel* model = |
| + OfflinePageModelFactory::GetForBrowserContext(profile()); |
| + const OfflinePageItem* page = model->GetPageByBookmarkId(kTestPageBookmarkId); |
| + GURL offline_url = page->GetOfflineURL(); |
| + GURL online_url = page->url; |
| + |
| + StartLoad(offline_url); |
| + EXPECT_EQ(online_url, controller().GetPendingEntry()->GetURL()); |
|
nasko
2016/02/26 00:12:19
I'd suggest calling main_test_rfh()->SendNavigate(
|
| +} |
| + |
| +TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnline) { |
| + SimulateHasNetworkConnectivity(false); |
| + |
| + OfflinePageModel* model = |
| + OfflinePageModelFactory::GetForBrowserContext(profile()); |
| + const OfflinePageItem* page = model->GetPageByBookmarkId(kTestPageBookmarkId); |
| + GURL offline_url = page->GetOfflineURL(); |
| + GURL online_url = page->url; |
| + |
| + StartLoad(online_url); |
| + EXPECT_EQ(online_url, controller().GetPendingEntry()->GetURL()); |
| + |
| + FailLoad(online_url); |
| + EXPECT_EQ(offline_url, controller().GetPendingEntry()->GetURL()); |
| +} |
| + |
| +} // namespace offline_pages |