| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 12 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" |
| 13 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h
" |
| 14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "components/offline_pages/offline_page_item.h" |
| 17 #include "components/offline_pages/offline_page_model.h" |
| 18 #include "components/offline_pages/offline_page_switches.h" |
| 19 #include "components/offline_pages/offline_page_test_archiver.h" |
| 20 #include "content/public/browser/navigation_entry.h" |
| 21 #include "content/public/browser/web_contents.h" |
| 22 #include "net/base/net_errors.h" |
| 23 #include "net/base/network_change_notifier.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 namespace offline_pages { |
| 27 |
| 28 namespace { |
| 29 |
| 30 const GURL kTestPageUrl("http://test.org/page1"); |
| 31 const int64_t kTestPageBookmarkId = 1234; |
| 32 const int64_t kTestFileSize = 876543LL; |
| 33 |
| 34 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { |
| 35 public: |
| 36 TestNetworkChangeNotifier() : online_(true) {} |
| 37 |
| 38 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() |
| 39 const override { |
| 40 return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN |
| 41 : net::NetworkChangeNotifier::CONNECTION_NONE; |
| 42 } |
| 43 |
| 44 void set_online(bool online) { online_ = online; } |
| 45 |
| 46 private: |
| 47 bool online_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); |
| 50 }; |
| 51 |
| 52 } // namespace |
| 53 |
| 54 class OfflinePageTabHelperTest : |
| 55 public ChromeRenderViewHostTestHarness, |
| 56 public OfflinePageTestArchiver::Observer, |
| 57 public base::SupportsWeakPtr<OfflinePageTabHelperTest> { |
| 58 public: |
| 59 OfflinePageTabHelperTest(); |
| 60 ~OfflinePageTabHelperTest() override; |
| 61 |
| 62 void SetUp() override; |
| 63 void TearDown() override; |
| 64 |
| 65 void RunUntilIdle(); |
| 66 void SimulateHasNetworkConnectivity(bool has_connectivity); |
| 67 void StartLoad(const GURL& url); |
| 68 void FailLoad(const GURL& url); |
| 69 |
| 70 OfflinePageTabHelper* offline_page_tab_helper() const { |
| 71 return offline_page_tab_helper_; |
| 72 } |
| 73 |
| 74 private: |
| 75 // OfflinePageTestArchiver::Observer implementation: |
| 76 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; |
| 77 |
| 78 scoped_ptr<OfflinePageTestArchiver> BuildArchiver( |
| 79 const GURL& url, |
| 80 const base::FilePath& file_name); |
| 81 void OnSavePageDone(OfflinePageModel::SavePageResult result); |
| 82 |
| 83 scoped_ptr<TestNetworkChangeNotifier> network_change_notifier_; |
| 84 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest); |
| 87 }; |
| 88 |
| 89 OfflinePageTabHelperTest::OfflinePageTabHelperTest() |
| 90 : network_change_notifier_(new TestNetworkChangeNotifier()) { |
| 91 } |
| 92 |
| 93 OfflinePageTabHelperTest::~OfflinePageTabHelperTest() { |
| 94 } |
| 95 |
| 96 void OfflinePageTabHelperTest::SetUp() { |
| 97 // Creates a test web contents. |
| 98 content::RenderViewHostTestHarness::SetUp(); |
| 99 OfflinePageTabHelper::CreateForWebContents(web_contents()); |
| 100 offline_page_tab_helper_ = |
| 101 OfflinePageTabHelper::FromWebContents(web_contents()); |
| 102 |
| 103 // Enables offline pages feature. |
| 104 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 105 switches::kEnableOfflinePages); |
| 106 |
| 107 // Sets up the factory for testing. |
| 108 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( |
| 109 profile(), BuildTestOfflinePageModel); |
| 110 RunUntilIdle(); |
| 111 |
| 112 // Saves an offline page. |
| 113 OfflinePageModel* model = |
| 114 OfflinePageModelFactory::GetForBrowserContext(profile()); |
| 115 scoped_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| 116 kTestPageUrl, base::FilePath(FILE_PATH_LITERAL("page1.mhtml")))); |
| 117 model->SavePage( |
| 118 kTestPageUrl, kTestPageBookmarkId, std::move(archiver), |
| 119 base::Bind(&OfflinePageTabHelperTest::OnSavePageDone, AsWeakPtr())); |
| 120 RunUntilIdle(); |
| 121 } |
| 122 |
| 123 void OfflinePageTabHelperTest::TearDown() { |
| 124 content::RenderViewHostTestHarness::TearDown(); |
| 125 } |
| 126 |
| 127 void OfflinePageTabHelperTest::RunUntilIdle() { |
| 128 base::RunLoop().RunUntilIdle(); |
| 129 } |
| 130 |
| 131 void OfflinePageTabHelperTest::SimulateHasNetworkConnectivity(bool online) { |
| 132 network_change_notifier_->set_online(online); |
| 133 RunUntilIdle(); |
| 134 } |
| 135 |
| 136 void OfflinePageTabHelperTest::StartLoad(const GURL& url) { |
| 137 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 138 std::string()); |
| 139 offline_page_tab_helper()->DidStartProvisionalLoadForFrame( |
| 140 main_rfh(), url, false, false); |
| 141 RunUntilIdle(); |
| 142 } |
| 143 |
| 144 void OfflinePageTabHelperTest::FailLoad(const GURL& url) { |
| 145 offline_page_tab_helper()->DidFailProvisionalLoad( |
| 146 main_rfh(), url, net::ERR_INTERNET_DISCONNECTED, base::string16(), false); |
| 147 RunUntilIdle(); |
| 148 } |
| 149 |
| 150 void OfflinePageTabHelperTest::SetLastPathCreatedByArchiver( |
| 151 const base::FilePath& file_path) { |
| 152 } |
| 153 |
| 154 scoped_ptr<OfflinePageTestArchiver> OfflinePageTabHelperTest::BuildArchiver( |
| 155 const GURL& url, |
| 156 const base::FilePath& file_name) { |
| 157 scoped_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( |
| 158 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, |
| 159 kTestFileSize, base::ThreadTaskRunnerHandle::Get())); |
| 160 archiver->set_filename(file_name); |
| 161 return archiver; |
| 162 } |
| 163 |
| 164 void OfflinePageTabHelperTest::OnSavePageDone( |
| 165 OfflinePageModel::SavePageResult result) { |
| 166 } |
| 167 |
| 168 TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOffline) { |
| 169 SimulateHasNetworkConnectivity(true); |
| 170 |
| 171 OfflinePageModel* model = |
| 172 OfflinePageModelFactory::GetForBrowserContext(profile()); |
| 173 const OfflinePageItem* page = model->GetPageByBookmarkId(kTestPageBookmarkId); |
| 174 GURL offline_url = page->GetOfflineURL(); |
| 175 GURL online_url = page->url; |
| 176 |
| 177 StartLoad(offline_url); |
| 178 EXPECT_EQ(online_url, controller().GetPendingEntry()->GetURL()); |
| 179 } |
| 180 |
| 181 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnline) { |
| 182 SimulateHasNetworkConnectivity(false); |
| 183 |
| 184 OfflinePageModel* model = |
| 185 OfflinePageModelFactory::GetForBrowserContext(profile()); |
| 186 const OfflinePageItem* page = model->GetPageByBookmarkId(kTestPageBookmarkId); |
| 187 GURL offline_url = page->GetOfflineURL(); |
| 188 GURL online_url = page->url; |
| 189 |
| 190 StartLoad(online_url); |
| 191 EXPECT_EQ(online_url, controller().GetPendingEntry()->GetURL()); |
| 192 |
| 193 FailLoad(online_url); |
| 194 EXPECT_EQ(offline_url, controller().GetPendingEntry()->GetURL()); |
| 195 } |
| 196 |
| 197 } // namespace offline_pages |
| OLD | NEW |