| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "chrome/browser/android/offline_pages/offline_page_model_factory.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" | 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
" | 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" | 14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 15 #include "components/offline_pages/offline_page_item.h" | 15 #include "components/offline_pages/offline_page_item.h" |
| 16 #include "components/offline_pages/offline_page_model.h" | 16 #include "components/offline_pages/offline_page_model.h" |
| 17 #include "components/offline_pages/offline_page_switches.h" | 17 #include "components/offline_pages/offline_page_switches.h" |
| 18 #include "components/offline_pages/offline_page_test_archiver.h" | 18 #include "components/offline_pages/offline_page_test_archiver.h" |
| 19 #include "content/public/browser/navigation_entry.h" | 19 #include "content/public/browser/navigation_entry.h" |
| 20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 22 #include "net/base/network_change_notifier.h" | 22 #include "net/base/network_change_notifier.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 24 |
| 25 namespace offline_pages { | 25 namespace offline_pages { |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 const GURL kTestPageUrl("http://test.org/page1"); | 29 const GURL kTestPageUrl("http://test.org/page1"); |
| 30 const int64_t kTestPageBookmarkId = 1234; | 30 const ClientId kTestPageBookmarkId = ClientId(BOOKMARK_NAMESPACE, "1234"); |
| 31 const int64_t kTestFileSize = 876543LL; | 31 const int64_t kTestFileSize = 876543LL; |
| 32 | 32 |
| 33 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { | 33 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { |
| 34 public: | 34 public: |
| 35 TestNetworkChangeNotifier() : online_(true) {} | 35 TestNetworkChangeNotifier() : online_(true) {} |
| 36 | 36 |
| 37 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() | 37 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() |
| 38 const override { | 38 const override { |
| 39 return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN | 39 return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN |
| 40 : net::NetworkChangeNotifier::CONNECTION_NONE; | 40 : net::NetworkChangeNotifier::CONNECTION_NONE; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 65 void RunUntilIdle(); | 65 void RunUntilIdle(); |
| 66 void SimulateHasNetworkConnectivity(bool has_connectivity); | 66 void SimulateHasNetworkConnectivity(bool has_connectivity); |
| 67 void StartLoad(const GURL& url); | 67 void StartLoad(const GURL& url); |
| 68 void CommitLoad(const GURL& url); | 68 void CommitLoad(const GURL& url); |
| 69 void FailLoad(const GURL& url); | 69 void FailLoad(const GURL& url); |
| 70 | 70 |
| 71 OfflinePageTabHelper* offline_page_tab_helper() const { | 71 OfflinePageTabHelper* offline_page_tab_helper() const { |
| 72 return offline_page_tab_helper_; | 72 return offline_page_tab_helper_; |
| 73 } | 73 } |
| 74 | 74 |
| 75 int64_t offline_id() const { return offline_id_; } |
| 76 |
| 75 private: | 77 private: |
| 76 // OfflinePageTestArchiver::Observer implementation: | 78 // OfflinePageTestArchiver::Observer implementation: |
| 77 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; | 79 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; |
| 78 | 80 |
| 79 scoped_ptr<OfflinePageTestArchiver> BuildArchiver( | 81 scoped_ptr<OfflinePageTestArchiver> BuildArchiver( |
| 80 const GURL& url, | 82 const GURL& url, |
| 81 const base::FilePath& file_name); | 83 const base::FilePath& file_name); |
| 82 void OnSavePageDone(OfflinePageModel::SavePageResult result); | 84 void OnSavePageDone(OfflinePageModel::SavePageResult result, |
| 85 int64_t offline_id); |
| 83 | 86 |
| 84 scoped_ptr<TestNetworkChangeNotifier> network_change_notifier_; | 87 scoped_ptr<TestNetworkChangeNotifier> network_change_notifier_; |
| 85 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. | 88 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. |
| 86 | 89 |
| 90 int64_t offline_id_; |
| 91 |
| 87 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest); | 92 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest); |
| 88 }; | 93 }; |
| 89 | 94 |
| 90 void OfflinePageTabHelperTest::SetUp() { | 95 void OfflinePageTabHelperTest::SetUp() { |
| 91 // Creates a test web contents. | 96 // Creates a test web contents. |
| 92 content::RenderViewHostTestHarness::SetUp(); | 97 content::RenderViewHostTestHarness::SetUp(); |
| 93 OfflinePageTabHelper::CreateForWebContents(web_contents()); | 98 OfflinePageTabHelper::CreateForWebContents(web_contents()); |
| 94 offline_page_tab_helper_ = | 99 offline_page_tab_helper_ = |
| 95 OfflinePageTabHelper::FromWebContents(web_contents()); | 100 OfflinePageTabHelper::FromWebContents(web_contents()); |
| 96 | 101 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 const GURL& url, | 157 const GURL& url, |
| 153 const base::FilePath& file_name) { | 158 const base::FilePath& file_name) { |
| 154 scoped_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( | 159 scoped_ptr<OfflinePageTestArchiver> archiver(new OfflinePageTestArchiver( |
| 155 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, | 160 this, url, OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, |
| 156 kTestFileSize, base::ThreadTaskRunnerHandle::Get())); | 161 kTestFileSize, base::ThreadTaskRunnerHandle::Get())); |
| 157 archiver->set_filename(file_name); | 162 archiver->set_filename(file_name); |
| 158 return archiver; | 163 return archiver; |
| 159 } | 164 } |
| 160 | 165 |
| 161 void OfflinePageTabHelperTest::OnSavePageDone( | 166 void OfflinePageTabHelperTest::OnSavePageDone( |
| 162 OfflinePageModel::SavePageResult result) { | 167 OfflinePageModel::SavePageResult result, |
| 168 int64_t offline_id) { |
| 169 offline_id_ = offline_id; |
| 163 } | 170 } |
| 164 | 171 |
| 165 TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOffline) { | 172 TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOffline) { |
| 166 SimulateHasNetworkConnectivity(true); | 173 SimulateHasNetworkConnectivity(true); |
| 167 | 174 |
| 168 OfflinePageModel* model = | 175 OfflinePageModel* model = |
| 169 OfflinePageModelFactory::GetForBrowserContext(browser_context()); | 176 OfflinePageModelFactory::GetForBrowserContext(browser_context()); |
| 170 const OfflinePageItem* page = model->GetPageByBookmarkId(kTestPageBookmarkId); | 177 const OfflinePageItem* page = model->GetPageByOfflineId(offline_id()); |
| 171 GURL offline_url = page->GetOfflineURL(); | 178 GURL offline_url = page->GetOfflineURL(); |
| 172 GURL online_url = page->url; | 179 GURL online_url = page->url; |
| 173 | 180 |
| 174 StartLoad(offline_url); | 181 StartLoad(offline_url); |
| 175 CommitLoad(online_url); | 182 CommitLoad(online_url); |
| 176 EXPECT_EQ(online_url, controller().GetLastCommittedEntry()->GetURL()); | 183 EXPECT_EQ(online_url, controller().GetLastCommittedEntry()->GetURL()); |
| 177 } | 184 } |
| 178 | 185 |
| 179 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnline) { | 186 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnline) { |
| 180 SimulateHasNetworkConnectivity(false); | 187 SimulateHasNetworkConnectivity(false); |
| 181 | 188 |
| 182 OfflinePageModel* model = | 189 OfflinePageModel* model = |
| 183 OfflinePageModelFactory::GetForBrowserContext(browser_context()); | 190 OfflinePageModelFactory::GetForBrowserContext(browser_context()); |
| 184 const OfflinePageItem* page = model->GetPageByBookmarkId(kTestPageBookmarkId); | 191 const OfflinePageItem* page = model->GetPageByOfflineId(offline_id()); |
| 185 GURL offline_url = page->GetOfflineURL(); | 192 GURL offline_url = page->GetOfflineURL(); |
| 186 GURL online_url = page->url; | 193 GURL online_url = page->url; |
| 187 | 194 |
| 188 StartLoad(online_url); | 195 StartLoad(online_url); |
| 189 EXPECT_EQ(online_url, controller().GetPendingEntry()->GetURL()); | 196 EXPECT_EQ(online_url, controller().GetPendingEntry()->GetURL()); |
| 190 | 197 |
| 191 FailLoad(online_url); | 198 FailLoad(online_url); |
| 192 EXPECT_EQ(offline_url, controller().GetPendingEntry()->GetURL()); | 199 EXPECT_EQ(offline_url, controller().GetPendingEntry()->GetURL()); |
| 193 } | 200 } |
| 194 | 201 |
| 195 } // namespace offline_pages | 202 } // namespace offline_pages |
| OLD | NEW |