| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" | 5 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/feature_list.h" | 10 #include "base/feature_list.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 14 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 15 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h
" | 15 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h
" |
| 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 17 #include "components/offline_pages/offline_page_feature.h" | 17 #include "components/offline_pages/offline_page_feature.h" |
| 18 #include "components/offline_pages/offline_page_item.h" | 18 #include "components/offline_pages/offline_page_item.h" |
| 19 #include "components/offline_pages/offline_page_model.h" | 19 #include "components/offline_pages/offline_page_model.h" |
| 20 #include "components/offline_pages/offline_page_test_archiver.h" | 20 #include "components/offline_pages/offline_page_test_archiver.h" |
| 21 #include "content/public/browser/navigation_entry.h" | 21 #include "content/public/browser/navigation_entry.h" |
| 22 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 23 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
| 24 #include "net/base/network_change_notifier.h" | 24 #include "net/base/network_change_notifier.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 26 |
| 27 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; |
| 28 using SingleOfflinePageItemResult = |
| 29 offline_pages::OfflinePageModel::SingleOfflinePageItemResult; |
| 30 |
| 27 namespace offline_pages { | 31 namespace offline_pages { |
| 28 | 32 |
| 29 namespace { | 33 namespace { |
| 30 | 34 |
| 31 const GURL kTestPageUrl("http://test.org/page1"); | 35 const GURL kTestPageUrl("http://test.org/page1"); |
| 32 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); | 36 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); |
| 33 const int64_t kTestFileSize = 876543LL; | 37 const int64_t kTestFileSize = 876543LL; |
| 34 | 38 |
| 35 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { | 39 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { |
| 36 public: | 40 public: |
| (...skipping 23 matching lines...) Expand all Loading... |
| 60 OfflinePageTabHelperTest() | 64 OfflinePageTabHelperTest() |
| 61 : network_change_notifier_(new TestNetworkChangeNotifier()) {} | 65 : network_change_notifier_(new TestNetworkChangeNotifier()) {} |
| 62 ~OfflinePageTabHelperTest() override {} | 66 ~OfflinePageTabHelperTest() override {} |
| 63 | 67 |
| 64 void SetUp() override; | 68 void SetUp() override; |
| 65 void TearDown() override; | 69 void TearDown() override; |
| 66 | 70 |
| 67 void RunUntilIdle(); | 71 void RunUntilIdle(); |
| 68 void SimulateHasNetworkConnectivity(bool has_connectivity); | 72 void SimulateHasNetworkConnectivity(bool has_connectivity); |
| 69 void StartLoad(const GURL& url); | 73 void StartLoad(const GURL& url); |
| 70 void CommitLoad(const GURL& url); | |
| 71 void FailLoad(const GURL& url); | 74 void FailLoad(const GURL& url); |
| 72 | 75 |
| 73 OfflinePageTabHelper* offline_page_tab_helper() const { | 76 OfflinePageTabHelper* offline_page_tab_helper() const { |
| 74 return offline_page_tab_helper_; | 77 return offline_page_tab_helper_; |
| 75 } | 78 } |
| 76 | 79 |
| 77 int64_t offline_id() const { return offline_id_; } | 80 const GURL& online_url() const { return online_url_; } |
| 81 const GURL& offline_url() const { return offline_url_; } |
| 78 | 82 |
| 79 private: | 83 private: |
| 80 // OfflinePageTestArchiver::Observer implementation: | 84 // OfflinePageTestArchiver::Observer implementation: |
| 81 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; | 85 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; |
| 82 | 86 |
| 83 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( | 87 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( |
| 84 const GURL& url, | 88 const GURL& url, |
| 85 const base::FilePath& file_name); | 89 const base::FilePath& file_name); |
| 86 void OnSavePageDone(OfflinePageModel::SavePageResult result, | 90 void OnSavePageDone(SavePageResult result, int64_t offline_id); |
| 87 int64_t offline_id); | 91 void OnGetPageByOfflineIdDone(const SingleOfflinePageItemResult& result); |
| 88 | 92 |
| 89 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; | 93 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; |
| 90 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. | 94 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. |
| 91 | 95 |
| 92 int64_t offline_id_; | 96 GURL online_url_; |
| 97 GURL offline_url_; |
| 93 | 98 |
| 94 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest); | 99 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest); |
| 95 }; | 100 }; |
| 96 | 101 |
| 97 void OfflinePageTabHelperTest::SetUp() { | 102 void OfflinePageTabHelperTest::SetUp() { |
| 98 // Enables offline pages feature. | 103 // Enables offline pages feature. |
| 99 // TODO(jianli): Remove this once the feature is completely enabled. | 104 // TODO(jianli): Remove this once the feature is completely enabled. |
| 100 base::FeatureList::ClearInstanceForTesting(); | 105 base::FeatureList::ClearInstanceForTesting(); |
| 101 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | 106 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 102 feature_list->InitializeFromCommandLine( | 107 feature_list->InitializeFromCommandLine( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 133 base::RunLoop().RunUntilIdle(); | 138 base::RunLoop().RunUntilIdle(); |
| 134 } | 139 } |
| 135 | 140 |
| 136 void OfflinePageTabHelperTest::SimulateHasNetworkConnectivity(bool online) { | 141 void OfflinePageTabHelperTest::SimulateHasNetworkConnectivity(bool online) { |
| 137 network_change_notifier_->set_online(online); | 142 network_change_notifier_->set_online(online); |
| 138 } | 143 } |
| 139 | 144 |
| 140 void OfflinePageTabHelperTest::StartLoad(const GURL& url) { | 145 void OfflinePageTabHelperTest::StartLoad(const GURL& url) { |
| 141 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, | 146 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 142 std::string()); | 147 std::string()); |
| 143 } | 148 content::RenderFrameHostTester::For(main_rfh())-> |
| 144 | 149 SimulateNavigationStart(url); |
| 145 void OfflinePageTabHelperTest::CommitLoad(const GURL& url) { | |
| 146 int entry_id = controller().GetPendingEntry()->GetUniqueID(); | |
| 147 content::RenderFrameHostTester::For(main_rfh()) | |
| 148 ->SendNavigate(0, entry_id, true, url); | |
| 149 } | 150 } |
| 150 | 151 |
| 151 void OfflinePageTabHelperTest::FailLoad(const GURL& url) { | 152 void OfflinePageTabHelperTest::FailLoad(const GURL& url) { |
| 152 content::RenderFrameHostTester::For(main_rfh())->SimulateNavigationStart(url); | 153 content::RenderFrameHostTester::For(main_rfh())->SimulateNavigationStart(url); |
| 153 // Set up the error code for the failed navigation. | 154 // Set up the error code for the failed navigation. |
| 154 content::RenderFrameHostTester::For(main_rfh())-> | 155 content::RenderFrameHostTester::For(main_rfh())-> |
| 155 SimulateNavigationError(url, net::ERR_INTERNET_DISCONNECTED); | 156 SimulateNavigationError(url, net::ERR_INTERNET_DISCONNECTED); |
| 156 content::RenderFrameHostTester::For(main_rfh())-> | 157 content::RenderFrameHostTester::For(main_rfh())-> |
| 157 SimulateNavigationErrorPageCommit(); | 158 SimulateNavigationErrorPageCommit(); |
| 158 // Gives a chance to run delayed task to do redirection. | 159 // Gives a chance to run delayed task to do redirection. |
| 159 RunUntilIdle(); | 160 RunUntilIdle(); |
| 160 } | 161 } |
| 161 | 162 |
| 162 void OfflinePageTabHelperTest::SetLastPathCreatedByArchiver( | 163 void OfflinePageTabHelperTest::SetLastPathCreatedByArchiver( |
| 163 const base::FilePath& file_path) { | 164 const base::FilePath& file_path) { |
| 164 } | 165 } |
| 165 | 166 |
| 166 std::unique_ptr<OfflinePageTestArchiver> | 167 std::unique_ptr<OfflinePageTestArchiver> |
| 167 OfflinePageTabHelperTest::BuildArchiver(const GURL& url, | 168 OfflinePageTabHelperTest::BuildArchiver(const GURL& url, |
| 168 const base::FilePath& file_name) { | 169 const base::FilePath& file_name) { |
| 169 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( | 170 std::unique_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( |
| 170 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, | 171 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, |
| 171 kTestFileSize, base::ThreadTaskRunnerHandle::Get())); | 172 kTestFileSize, base::ThreadTaskRunnerHandle::Get())); |
| 172 archiver->set_filename(file_name); | 173 archiver->set_filename(file_name); |
| 173 return archiver; | 174 return archiver; |
| 174 } | 175 } |
| 175 | 176 |
| 176 void OfflinePageTabHelperTest::OnSavePageDone( | 177 void OfflinePageTabHelperTest::OnSavePageDone(SavePageResult result, |
| 177 OfflinePageModel::SavePageResult result, | 178 int64_t offline_id) { |
| 178 int64_t offline_id) { | 179 OfflinePageModel* model = |
| 179 offline_id_ = offline_id; | 180 OfflinePageModelFactory::GetForBrowserContext(browser_context()); |
| 181 model->GetPageByOfflineId(offline_id, |
| 182 base::Bind(&OfflinePageTabHelperTest::OnGetPageByOfflineIdDone, |
| 183 AsWeakPtr())); |
| 180 } | 184 } |
| 181 | 185 |
| 182 TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOffline) { | 186 void OfflinePageTabHelperTest::OnGetPageByOfflineIdDone( |
| 187 const SingleOfflinePageItemResult& result) { |
| 188 DCHECK(result); |
| 189 online_url_ = result->url; |
| 190 offline_url_ = result->GetOfflineURL(); |
| 191 } |
| 192 |
| 193 TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOfflineOnNetwork) { |
| 183 SimulateHasNetworkConnectivity(true); | 194 SimulateHasNetworkConnectivity(true); |
| 184 | 195 |
| 185 OfflinePageModel* model = | 196 StartLoad(offline_url()); |
| 186 OfflinePageModelFactory::GetForBrowserContext(browser_context()); | 197 // Gives a chance to run delayed task to do redirection. |
| 187 const OfflinePageItem* page = model->MaybeGetPageByOfflineId(offline_id()); | 198 RunUntilIdle(); |
| 188 GURL offline_url = page->GetOfflineURL(); | 199 // Redirection will be done immediately on navigation start. |
| 189 GURL online_url = page->url; | 200 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); |
| 190 | |
| 191 StartLoad(offline_url); | |
| 192 CommitLoad(online_url); | |
| 193 EXPECT_EQ(online_url, controller().GetLastCommittedEntry()->GetURL()); | |
| 194 } | 201 } |
| 195 | 202 |
| 196 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnline) { | 203 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnNoNetwork) { |
| 197 SimulateHasNetworkConnectivity(false); | 204 SimulateHasNetworkConnectivity(false); |
| 198 | 205 |
| 199 OfflinePageModel* model = | 206 StartLoad(online_url()); |
| 200 OfflinePageModelFactory::GetForBrowserContext(browser_context()); | 207 // Gives a chance to run delayed task to do redirection. |
| 201 const OfflinePageItem* page = model->MaybeGetPageByOfflineId(offline_id()); | 208 RunUntilIdle(); |
| 202 GURL offline_url = page->GetOfflineURL(); | 209 // Redirection will be done immediately on navigation start. |
| 203 GURL online_url = page->url; | 210 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); |
| 211 } |
| 204 | 212 |
| 205 StartLoad(online_url); | 213 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnError) { |
| 206 EXPECT_EQ(online_url, controller().GetPendingEntry()->GetURL()); | 214 SimulateHasNetworkConnectivity(true); |
| 207 | 215 |
| 208 FailLoad(online_url); | 216 StartLoad(online_url()); |
| 209 EXPECT_EQ(offline_url, controller().GetPendingEntry()->GetURL()); | 217 RunUntilIdle(); |
| 218 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); |
| 219 |
| 220 // Redirection will be done immediately on navigation end with error. |
| 221 FailLoad(online_url()); |
| 222 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); |
| 210 } | 223 } |
| 211 | 224 |
| 212 } // namespace offline_pages | 225 } // namespace offline_pages |
| OLD | NEW |