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