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

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

Issue 2040163003: Refactors offline page tab helper to stash the offline page item on redirect. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@single-result
Patch Set: fix nit. Created 4 years, 6 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
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 "base/test/histogram_tester.h" 14 #include "base/test/histogram_tester.h"
15 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" 15 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
16 #include "chrome/browser/android/offline_pages/offline_page_utils.h"
16 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " 17 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h "
17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
18 #include "components/offline_pages/client_namespace_constants.h" 19 #include "components/offline_pages/client_namespace_constants.h"
19 #include "components/offline_pages/offline_page_feature.h" 20 #include "components/offline_pages/offline_page_feature.h"
20 #include "components/offline_pages/offline_page_item.h" 21 #include "components/offline_pages/offline_page_item.h"
21 #include "components/offline_pages/offline_page_model.h" 22 #include "components/offline_pages/offline_page_model.h"
22 #include "components/offline_pages/offline_page_test_archiver.h" 23 #include "components/offline_pages/offline_page_test_archiver.h"
23 #include "components/offline_pages/offline_page_types.h" 24 #include "components/offline_pages/offline_page_types.h"
24 #include "content/public/browser/navigation_entry.h" 25 #include "content/public/browser/navigation_entry.h"
25 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 74
74 void RunUntilIdle(); 75 void RunUntilIdle();
75 void SimulateHasNetworkConnectivity(bool has_connectivity); 76 void SimulateHasNetworkConnectivity(bool has_connectivity);
76 void StartLoad(const GURL& url); 77 void StartLoad(const GURL& url);
77 void FailLoad(const GURL& url); 78 void FailLoad(const GURL& url);
78 79
79 OfflinePageTabHelper* offline_page_tab_helper() const { 80 OfflinePageTabHelper* offline_page_tab_helper() const {
80 return offline_page_tab_helper_; 81 return offline_page_tab_helper_;
81 } 82 }
82 83
83 const GURL& online_url() const { return online_url_; } 84 const GURL& online_url() const { return offline_page_item_->url; }
84 const GURL& offline_url() const { return offline_url_; } 85 GURL offline_url() const { return offline_page_item_->GetOfflineURL(); }
85 86
86 const base::HistogramTester& histograms() const { return histogram_tester_; } 87 const base::HistogramTester& histograms() const { return histogram_tester_; }
87 88
88 private: 89 private:
89 // OfflinePageTestArchiver::Observer implementation: 90 // OfflinePageTestArchiver::Observer implementation:
90 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; 91 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override;
91 92
92 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( 93 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver(
93 const GURL& url, 94 const GURL& url,
94 const base::FilePath& file_name); 95 const base::FilePath& file_name);
95 void OnSavePageDone(SavePageResult result, int64_t offline_id); 96 void OnSavePageDone(SavePageResult result, int64_t offline_id);
96 void OnGetPageByOfflineIdDone(const OfflinePageItem* result); 97 void OnGetPageByOfflineIdDone(const OfflinePageItem* result);
97 98
98 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; 99 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_;
99 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. 100 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned.
100 101
101 GURL online_url_; 102 std::unique_ptr<OfflinePageItem> offline_page_item_;
102 GURL offline_url_;
103 103
104 base::HistogramTester histogram_tester_; 104 base::HistogramTester histogram_tester_;
105 105
106 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest); 106 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest);
107 }; 107 };
108 108
109 void OfflinePageTabHelperTest::SetUp() { 109 void OfflinePageTabHelperTest::SetUp() {
110 // Enables offline pages feature. 110 // Enables offline pages feature.
111 // TODO(jianli): Remove this once the feature is completely enabled. 111 // TODO(jianli): Remove this once the feature is completely enabled.
112 base::FeatureList::ClearInstanceForTesting(); 112 base::FeatureList::ClearInstanceForTesting();
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 OfflinePageModel* model = 186 OfflinePageModel* model =
187 OfflinePageModelFactory::GetForBrowserContext(browser_context()); 187 OfflinePageModelFactory::GetForBrowserContext(browser_context());
188 model->GetPageByOfflineId(offline_id, 188 model->GetPageByOfflineId(offline_id,
189 base::Bind(&OfflinePageTabHelperTest::OnGetPageByOfflineIdDone, 189 base::Bind(&OfflinePageTabHelperTest::OnGetPageByOfflineIdDone,
190 AsWeakPtr())); 190 AsWeakPtr()));
191 } 191 }
192 192
193 void OfflinePageTabHelperTest::OnGetPageByOfflineIdDone( 193 void OfflinePageTabHelperTest::OnGetPageByOfflineIdDone(
194 const OfflinePageItem* result) { 194 const OfflinePageItem* result) {
195 DCHECK(result); 195 DCHECK(result);
196 online_url_ = result->url; 196 offline_page_item_.reset(new OfflinePageItem(*result));
197 offline_url_ = result->GetOfflineURL();
198 } 197 }
199 198
200 TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOfflineOnNetwork) { 199 TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOfflineOnNetwork) {
201 SimulateHasNetworkConnectivity(true); 200 SimulateHasNetworkConnectivity(true);
202 201
203 StartLoad(offline_url()); 202 StartLoad(offline_url());
204 // Gives a chance to run delayed task to do redirection. 203 // Gives a chance to run delayed task to do redirection.
205 RunUntilIdle(); 204 RunUntilIdle();
206 // Redirection will be done immediately on navigation start. 205 // Redirection will be done immediately on navigation start.
207 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); 206 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL());
208 histograms().ExpectTotalCount(kBadNetworkHistogram, 0); 207 histograms().ExpectTotalCount(kBadNetworkHistogram, 0);
209 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 0); 208 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 0);
210 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 1); 209 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 1);
211 } 210 }
212 211
213 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnNoNetwork) { 212 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnNoNetwork) {
214 SimulateHasNetworkConnectivity(false); 213 SimulateHasNetworkConnectivity(false);
215 214
216 StartLoad(online_url()); 215 StartLoad(online_url());
217 // Gives a chance to run delayed task to do redirection. 216 // Gives a chance to run delayed task to do redirection.
218 RunUntilIdle(); 217 RunUntilIdle();
219 // Redirection will be done immediately on navigation start. 218 // Redirection will be done immediately on navigation start.
220 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); 219 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL());
221 histograms().ExpectTotalCount(kBadNetworkHistogram, 0); 220 histograms().ExpectTotalCount(kBadNetworkHistogram, 0);
222 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 1); 221 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 1);
223 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0); 222 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0);
224 } 223 }
225 224
225 TEST_F(OfflinePageTabHelperTest, TestCurrentOfflinePage) {
226 SimulateHasNetworkConnectivity(false);
227
228 StartLoad(online_url());
229 // Gives a chance to run delayed task to do redirection.
230 RunUntilIdle();
231
232 const OfflinePageItem* item =
233 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents());
234 EXPECT_EQ(offline_url(), item->GetOfflineURL());
235 EXPECT_EQ(online_url(), item->url);
236
237 SimulateHasNetworkConnectivity(true);
238 StartLoad(offline_url());
239 RunUntilIdle();
240 item = OfflinePageUtils::GetOfflinePageFromWebContents(web_contents());
241 EXPECT_EQ(nullptr, item);
242 }
243
226 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnError) { 244 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnError) {
227 SimulateHasNetworkConnectivity(true); 245 SimulateHasNetworkConnectivity(true);
228 246
229 StartLoad(online_url()); 247 StartLoad(online_url());
230 RunUntilIdle(); 248 RunUntilIdle();
231 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); 249 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL());
232 250
233 // Redirection will be done immediately on navigation end with error. 251 // Redirection will be done immediately on navigation end with error.
234 FailLoad(online_url()); 252 FailLoad(online_url());
235 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); 253 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL());
(...skipping 22 matching lines...) Expand all
258 276
259 // Redirection should be cancelled so we should still navigate to 277 // Redirection should be cancelled so we should still navigate to
260 // |unsaved_url|. 278 // |unsaved_url|.
261 EXPECT_EQ(unsaved_url, controller().GetPendingEntry()->GetURL()); 279 EXPECT_EQ(unsaved_url, controller().GetPendingEntry()->GetURL());
262 280
263 histograms().ExpectTotalCount(kBadNetworkHistogram, 0); 281 histograms().ExpectTotalCount(kBadNetworkHistogram, 0);
264 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 0); 282 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 0);
265 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0); 283 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0);
266 } 284 }
267 } // namespace offline_pages 285 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698