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

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: Address feedback 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 "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()
nasko 2016/02/26 00:12:19 nit: Since this is a test class, you can inline th
jianli 2016/02/26 03:03:22 Done.
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();
nasko 2016/02/26 00:12:19 Why do you need to call RunUntilIdle() here? It on
jianli 2016/02/26 03:03:22 Not needed. Removed.
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(
nasko 2016/02/26 00:12:19 This will not be compatible with PlzNavigate. You
jianli 2016/02/26 03:03:22 Done.
140 main_rfh(), url, false, false);
141 RunUntilIdle();
nasko 2016/02/26 00:12:19 Why is this needed?
jianli 2016/02/26 03:03:22 Removed.
142 }
143
144 void OfflinePageTabHelperTest::FailLoad(const GURL& url) {
145 offline_page_tab_helper()->DidFailProvisionalLoad(
nasko 2016/02/26 00:12:19 Use main_test_rfh()->SimulateNavigationError inste
jianli 2016/02/26 03:03:22 Done.
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 }
nasko 2016/02/26 00:12:19 Why do we need an empty method?
jianli 2016/02/26 03:03:22 This is needed for OfflinePageTestArchiver::Observ
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 }
nasko 2016/02/26 00:12:19 Why do we need an empty method?
jianli 2016/02/26 03:03:22 This is needed to call model->SavePage.
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());
nasko 2016/02/26 00:12:19 I'd suggest calling main_test_rfh()->SendNavigate(
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698