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

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

Issue 2040573002: [Offlining] Updates the tab helper to use the async OfflinePageModel API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix TODOs 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 "chrome/browser/android/offline_pages/offline_page_model_factory.h" 15 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
15 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " 16 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h "
16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 17 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
17 #include "components/offline_pages/client_namespace_constants.h" 18 #include "components/offline_pages/client_namespace_constants.h"
18 #include "components/offline_pages/offline_page_feature.h" 19 #include "components/offline_pages/offline_page_feature.h"
19 #include "components/offline_pages/offline_page_item.h" 20 #include "components/offline_pages/offline_page_item.h"
20 #include "components/offline_pages/offline_page_model.h" 21 #include "components/offline_pages/offline_page_model.h"
21 #include "components/offline_pages/offline_page_test_archiver.h" 22 #include "components/offline_pages/offline_page_test_archiver.h"
22 #include "components/offline_pages/offline_page_types.h" 23 #include "components/offline_pages/offline_page_types.h"
23 #include "content/public/browser/navigation_entry.h" 24 #include "content/public/browser/navigation_entry.h"
24 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents.h"
25 #include "net/base/net_errors.h" 26 #include "net/base/net_errors.h"
26 #include "net/base/network_change_notifier.h" 27 #include "net/base/network_change_notifier.h"
27 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
28 29
29 namespace offline_pages { 30 namespace offline_pages {
30 31
31 namespace { 32 namespace {
32 33
33 const GURL kTestPageUrl("http://test.org/page1"); 34 const GURL kTestPageUrl("http://test.org/page1");
34 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); 35 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234");
35 const int64_t kTestFileSize = 876543LL; 36 const int64_t kTestFileSize = 876543LL;
37 const char kBadNetworkHistogram[] =
38 "OfflinePages.ShowOfflinePageOnBadNetwork";
39 const char kRedirectToOfflineHistogram[] =
40 "OfflinePages.RedirectToOfflineCount";
41 const char kRedirectToOnlineHistogram[] =
42 "OfflinePages.RedirectToOnlineCount";
36 43
37 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { 44 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier {
38 public: 45 public:
39 TestNetworkChangeNotifier() : online_(true) {} 46 TestNetworkChangeNotifier() : online_(true) {}
40 47
41 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() 48 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType()
42 const override { 49 const override {
43 return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN 50 return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN
44 : net::NetworkChangeNotifier::CONNECTION_NONE; 51 : net::NetworkChangeNotifier::CONNECTION_NONE;
45 } 52 }
(...skipping 25 matching lines...) Expand all
71 void StartLoad(const GURL& url); 78 void StartLoad(const GURL& url);
72 void FailLoad(const GURL& url); 79 void FailLoad(const GURL& url);
73 80
74 OfflinePageTabHelper* offline_page_tab_helper() const { 81 OfflinePageTabHelper* offline_page_tab_helper() const {
75 return offline_page_tab_helper_; 82 return offline_page_tab_helper_;
76 } 83 }
77 84
78 const GURL& online_url() const { return online_url_; } 85 const GURL& online_url() const { return online_url_; }
79 const GURL& offline_url() const { return offline_url_; } 86 const GURL& offline_url() const { return offline_url_; }
80 87
88 const base::HistogramTester& histograms() const { return histogram_tester_; }
89
81 private: 90 private:
82 // OfflinePageTestArchiver::Observer implementation: 91 // OfflinePageTestArchiver::Observer implementation:
83 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override; 92 void SetLastPathCreatedByArchiver(const base::FilePath& file_path) override;
84 93
85 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver( 94 std::unique_ptr<OfflinePageTestArchiver> BuildArchiver(
86 const GURL& url, 95 const GURL& url,
87 const base::FilePath& file_name); 96 const base::FilePath& file_name);
88 void OnSavePageDone(SavePageResult result, int64_t offline_id); 97 void OnSavePageDone(SavePageResult result, int64_t offline_id);
89 void OnGetPageByOfflineIdDone(const SingleOfflinePageItemResult& result); 98 void OnGetPageByOfflineIdDone(const SingleOfflinePageItemResult& result);
90 99
91 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; 100 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_;
92 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. 101 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned.
93 102
94 GURL online_url_; 103 GURL online_url_;
95 GURL offline_url_; 104 GURL offline_url_;
96 105
106 base::HistogramTester histogram_tester_;
107
97 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest); 108 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelperTest);
98 }; 109 };
99 110
100 void OfflinePageTabHelperTest::SetUp() { 111 void OfflinePageTabHelperTest::SetUp() {
101 // Enables offline pages feature. 112 // Enables offline pages feature.
102 // TODO(jianli): Remove this once the feature is completely enabled. 113 // TODO(jianli): Remove this once the feature is completely enabled.
103 base::FeatureList::ClearInstanceForTesting(); 114 base::FeatureList::ClearInstanceForTesting();
104 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); 115 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
105 feature_list->InitializeFromCommandLine( 116 feature_list->InitializeFromCommandLine(
106 offline_pages::kOfflineBookmarksFeature.name, ""); 117 offline_pages::kOfflineBookmarksFeature.name, "");
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 } 200 }
190 201
191 TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOfflineOnNetwork) { 202 TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOfflineOnNetwork) {
192 SimulateHasNetworkConnectivity(true); 203 SimulateHasNetworkConnectivity(true);
193 204
194 StartLoad(offline_url()); 205 StartLoad(offline_url());
195 // Gives a chance to run delayed task to do redirection. 206 // Gives a chance to run delayed task to do redirection.
196 RunUntilIdle(); 207 RunUntilIdle();
197 // Redirection will be done immediately on navigation start. 208 // Redirection will be done immediately on navigation start.
198 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); 209 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL());
210 histograms().ExpectTotalCount(kBadNetworkHistogram, 0);
211 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 0);
212 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 1);
199 } 213 }
200 214
201 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnNoNetwork) { 215 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnNoNetwork) {
202 SimulateHasNetworkConnectivity(false); 216 SimulateHasNetworkConnectivity(false);
203 217
204 StartLoad(online_url()); 218 StartLoad(online_url());
205 // Gives a chance to run delayed task to do redirection. 219 // Gives a chance to run delayed task to do redirection.
206 RunUntilIdle(); 220 RunUntilIdle();
207 // Redirection will be done immediately on navigation start. 221 // Redirection will be done immediately on navigation start.
208 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); 222 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL());
223 histograms().ExpectTotalCount(kBadNetworkHistogram, 0);
224 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 1);
225 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0);
209 } 226 }
210 227
211 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnError) { 228 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnError) {
212 SimulateHasNetworkConnectivity(true); 229 SimulateHasNetworkConnectivity(true);
213 230
214 StartLoad(online_url()); 231 StartLoad(online_url());
215 RunUntilIdle(); 232 RunUntilIdle();
216 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); 233 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL());
217 234
218 // Redirection will be done immediately on navigation end with error. 235 // Redirection will be done immediately on navigation end with error.
219 FailLoad(online_url()); 236 FailLoad(online_url());
220 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); 237 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL());
238
239 histograms().ExpectBucketCount(kBadNetworkHistogram, false, 0);
240 histograms().ExpectBucketCount(kBadNetworkHistogram, true, 1);
241 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 1);
242 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0);
221 } 243 }
222 244
245 TEST_F(OfflinePageTabHelperTest, NewNavigationCancelsPendingRedirects) {
246 SimulateHasNetworkConnectivity(false);
247
248 StartLoad(online_url());
249 const GURL unsaved_url("http://test.org/page2");
250
251 // We should have a pending task that will do the redirect.
252 ASSERT_TRUE(offline_page_tab_helper()->weak_ptr_factory_.HasWeakPtrs());
253 ASSERT_EQ(online_url(), controller().GetPendingEntry()->GetURL());
254
255 // Should cancel pending tasks for previous URL.
256 StartLoad(unsaved_url);
257
258 // Gives a chance to run delayed task to do redirection.
259 RunUntilIdle();
260
261 // Redirection should be cancelled so we should still navigate to
262 // |unsaved_url|.
263 EXPECT_EQ(unsaved_url, controller().GetPendingEntry()->GetURL());
264
265 histograms().ExpectTotalCount(kBadNetworkHistogram, 0);
266 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 0);
267 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0);
268 }
223 } // namespace offline_pages 269 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698