Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(863)

Side by Side Diff: chrome/browser/android/offline_pages/offline_page_tab_helper_unittest.cc

Issue 1721103002: Switch between online and offline version per network connection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 content::RenderViewHostImplTestHarness,
nasko 2016/02/26 15:42:56 Remove commented out inheritance.
jianli 2016/02/26 23:02:41 Done.
55 public ChromeRenderViewHostTestHarness,
56 public OfflinePageTestArchiver::Observer,
57 public base::SupportsWeakPtr<OfflinePageTabHelperTest> {
58 public:
59 OfflinePageTabHelperTest()
60 : network_change_notifier_(new TestNetworkChangeNotifier()) {}
61 ~OfflinePageTabHelperTest() override {}
62
63 void SetUp() override;
64 void TearDown() override;
65
66 void RunUntilIdle();
67 void SimulateHasNetworkConnectivity(bool has_connectivity);
68 void StartLoad(const GURL& url);
69 void CommitLoad(const GURL& url);
70 void FailLoad(const GURL& url);
71
72 OfflinePageTabHelper* offline_page_tab_helper() const {
73 return offline_page_tab_helper_;
74 }
75
76 private:
77 // OfflinePageTestArchiver::Observer implementation:
78 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override;
79
80 scoped_ptr<OfflinePageTestArchiver> BuildArchiver(
81 const GURL& url,
82 const base::FilePath& file_name);
83 void OnSavePageDone(OfflinePageModel::SavePageResult result);
84
85 scoped_ptr<TestNetworkChangeNotifier> network_change_notifier_;
86 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned.
87
88 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest);
89 };
90
91 void OfflinePageTabHelperTest::SetUp() {
92 // Creates a test web contents.
93 //content::RenderViewHostImplTestHarness::SetUp();
94 content::RenderViewHostTestHarness::SetUp();
95 OfflinePageTabHelper::CreateForWebContents(web_contents());
96 offline_page_tab_helper_ =
97 OfflinePageTabHelper::FromWebContents(web_contents());
98
99 // Enables offline pages feature.
100 base::CommandLine::ForCurrentProcess()->AppendSwitch(
101 switches::kEnableOfflinePages);
102
103 // Sets up the factory for testing.
104 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse(
105 browser_context(), BuildTestOfflinePageModel);
106 RunUntilIdle();
107
108 // Saves an offline page.
109 OfflinePageModel* model =
110 OfflinePageModelFactory::GetForBrowserContext(browser_context());
111 scoped_ptr<OfflinePageTestArchiver> archiver(BuildArchiver(
112 kTestPageUrl, base::FilePath(FILE_PATH_LITERAL("page1.mhtml"))));
113 model->SavePage(
114 kTestPageUrl, kTestPageBookmarkId, std::move(archiver),
115 base::Bind(&OfflinePageTabHelperTest::OnSavePageDone, AsWeakPtr()));
116 RunUntilIdle();
117 }
118
119 void OfflinePageTabHelperTest::TearDown() {
120 //content::RenderViewHostImplTestHarness::TearDown();
121 content::RenderViewHostTestHarness::TearDown();
122 }
123
124 void OfflinePageTabHelperTest::RunUntilIdle() {
125 base::RunLoop().RunUntilIdle();
126 }
127
128 void OfflinePageTabHelperTest::SimulateHasNetworkConnectivity(bool online) {
129 network_change_notifier_->set_online(online);
130 }
131
132 void OfflinePageTabHelperTest::StartLoad(const GURL& url) {
133 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED,
134 std::string());
135 main_test_rfh()->PrepareForCommit();
136 }
137
138 void OfflinePageTabHelperTest::CommitLoad(const GURL& url) {
139 int entry_id = controller().GetPendingEntry()->GetUniqueID();
140 main_test_rfh()->SendNavigate(0, entry_id, true, url);
141 }
142
143 void OfflinePageTabHelperTest::FailLoad(const GURL& url) {
144 main_test_rfh()->SimulateNavigationError(url, net::ERR_INTERNET_DISCONNECTED);
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698