| 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 "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" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #include "net/base/network_change_notifier.h" | 29 #include "net/base/network_change_notifier.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 31 |
| 32 namespace offline_pages { | 32 namespace offline_pages { |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 const GURL kTestPageUrl("http://test.org/page1"); | 36 const GURL kTestPageUrl("http://test.org/page1"); |
| 37 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); | 37 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); |
| 38 const int64_t kTestFileSize = 876543LL; | 38 const int64_t kTestFileSize = 876543LL; |
| 39 const char kBadNetworkHistogram[] = "OfflinePages.ShowOfflinePageOnBadNetwork"; | 39 const char kRedirectResultHistogram[] = "OfflinePages.RedirectResult"; |
| 40 const char kRedirectToOfflineHistogram[] = | |
| 41 "OfflinePages.RedirectToOfflineCount"; | |
| 42 const char kRedirectToOnlineHistogram[] = "OfflinePages.RedirectToOnlineCount"; | |
| 43 const char kTabId[] = "42"; | 40 const char kTabId[] = "42"; |
| 44 | 41 |
| 45 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { | 42 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { |
| 46 public: | 43 public: |
| 47 TestNetworkChangeNotifier() : online_(true) {} | 44 TestNetworkChangeNotifier() : online_(true) {} |
| 48 | 45 |
| 49 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() | 46 net::NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() |
| 50 const override { | 47 const override { |
| 51 return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN | 48 return online_ ? net::NetworkChangeNotifier::CONNECTION_UNKNOWN |
| 52 : net::NetworkChangeNotifier::CONNECTION_NONE; | 49 : net::NetworkChangeNotifier::CONNECTION_NONE; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 } | 217 } |
| 221 | 218 |
| 222 TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOfflineOnNetwork) { | 219 TEST_F(OfflinePageTabHelperTest, SwitchToOnlineFromOfflineOnNetwork) { |
| 223 SimulateHasNetworkConnectivity(true); | 220 SimulateHasNetworkConnectivity(true); |
| 224 | 221 |
| 225 StartLoad(offline_url()); | 222 StartLoad(offline_url()); |
| 226 // Gives a chance to run delayed task to do redirection. | 223 // Gives a chance to run delayed task to do redirection. |
| 227 RunUntilIdle(); | 224 RunUntilIdle(); |
| 228 // Redirection will be done immediately on navigation start. | 225 // Redirection will be done immediately on navigation start. |
| 229 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); | 226 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); |
| 230 histograms().ExpectTotalCount(kBadNetworkHistogram, 0); | 227 histograms().ExpectUniqueSample( |
| 231 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 0); | 228 kRedirectResultHistogram, |
| 232 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 1); | 229 OfflinePageTabHelper::RedirectResult::CONNECTED_NETWORK, |
| 230 1); |
| 233 } | 231 } |
| 234 | 232 |
| 235 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnNoNetwork) { | 233 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnNoNetwork) { |
| 236 SimulateHasNetworkConnectivity(false); | 234 SimulateHasNetworkConnectivity(false); |
| 237 | 235 |
| 238 StartLoad(online_url()); | 236 StartLoad(online_url()); |
| 239 // Gives a chance to run delayed task to do redirection. | 237 // Gives a chance to run delayed task to do redirection. |
| 240 RunUntilIdle(); | 238 RunUntilIdle(); |
| 241 // Redirection will be done immediately on navigation start. | 239 // Redirection will be done immediately on navigation start. |
| 242 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); | 240 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); |
| 243 histograms().ExpectTotalCount(kBadNetworkHistogram, 0); | 241 histograms().ExpectUniqueSample( |
| 244 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 1); | 242 kRedirectResultHistogram, |
| 245 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0); | 243 OfflinePageTabHelper::RedirectResult::DISCONNECTED_NETWORK, |
| 244 1); |
| 246 } | 245 } |
| 247 | 246 |
| 248 TEST_F(OfflinePageTabHelperTest, TestCurrentOfflinePage) { | 247 TEST_F(OfflinePageTabHelperTest, TestCurrentOfflinePage) { |
| 249 SimulateHasNetworkConnectivity(false); | 248 SimulateHasNetworkConnectivity(false); |
| 250 | 249 |
| 251 StartLoad(online_url()); | 250 StartLoad(online_url()); |
| 252 // Gives a chance to run delayed task to do redirection. | 251 // Gives a chance to run delayed task to do redirection. |
| 253 RunUntilIdle(); | 252 RunUntilIdle(); |
| 254 | 253 |
| 255 const OfflinePageItem* item = | 254 const OfflinePageItem* item = |
| (...skipping 12 matching lines...) Expand all Loading... |
| 268 SimulateHasNetworkConnectivity(true); | 267 SimulateHasNetworkConnectivity(true); |
| 269 | 268 |
| 270 StartLoad(online_url()); | 269 StartLoad(online_url()); |
| 271 RunUntilIdle(); | 270 RunUntilIdle(); |
| 272 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); | 271 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); |
| 273 | 272 |
| 274 // Redirection will be done immediately on navigation end with error. | 273 // Redirection will be done immediately on navigation end with error. |
| 275 FailLoad(online_url()); | 274 FailLoad(online_url()); |
| 276 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); | 275 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); |
| 277 | 276 |
| 278 histograms().ExpectBucketCount(kBadNetworkHistogram, false, 0); | 277 histograms().ExpectUniqueSample( |
| 279 histograms().ExpectBucketCount(kBadNetworkHistogram, true, 1); | 278 kRedirectResultHistogram, |
| 280 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 1); | 279 OfflinePageTabHelper::RedirectResult::FLAKY_NETWORK, |
| 281 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0); | 280 1); |
| 282 } | 281 } |
| 283 | 282 |
| 284 TEST_F(OfflinePageTabHelperTest, NewNavigationCancelsPendingRedirects) { | 283 TEST_F(OfflinePageTabHelperTest, NewNavigationCancelsPendingRedirects) { |
| 285 SimulateHasNetworkConnectivity(false); | 284 SimulateHasNetworkConnectivity(false); |
| 286 | 285 |
| 287 StartLoad(online_url()); | 286 StartLoad(online_url()); |
| 288 const GURL unsaved_url("http://test.org/page2"); | 287 const GURL unsaved_url("http://test.org/page2"); |
| 289 | 288 |
| 290 // We should have a pending task that will do the redirect. | 289 // We should have a pending task that will do the redirect. |
| 291 ASSERT_TRUE(offline_page_tab_helper()->weak_ptr_factory_.HasWeakPtrs()); | 290 ASSERT_TRUE(offline_page_tab_helper()->weak_ptr_factory_.HasWeakPtrs()); |
| 292 ASSERT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); | 291 ASSERT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); |
| 293 | 292 |
| 294 // Should cancel pending tasks for previous URL. | 293 // Should cancel pending tasks for previous URL. |
| 295 StartLoad(unsaved_url); | 294 StartLoad(unsaved_url); |
| 296 | 295 |
| 297 // Gives a chance to run delayed task to do redirection. | 296 // Gives a chance to run delayed task to do redirection. |
| 298 RunUntilIdle(); | 297 RunUntilIdle(); |
| 299 | 298 |
| 300 // Redirection should be cancelled so we should still navigate to | 299 // Redirection should be cancelled so we should still navigate to |
| 301 // |unsaved_url|. | 300 // |unsaved_url|. |
| 302 EXPECT_EQ(unsaved_url, controller().GetPendingEntry()->GetURL()); | 301 EXPECT_EQ(unsaved_url, controller().GetPendingEntry()->GetURL()); |
| 303 | 302 |
| 304 histograms().ExpectTotalCount(kBadNetworkHistogram, 0); | 303 // Should report attempt of redirect, but the page not found. |
| 305 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 0); | 304 histograms().ExpectUniqueSample( |
| 306 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0); | 305 kRedirectResultHistogram, |
| 306 OfflinePageTabHelper::RedirectResult::DISCONNECTED_NETWORK_NOT_FOUND, |
| 307 1); |
| 307 } | 308 } |
| 308 | 309 |
| 309 // This test saves 3 pages (one in setup and 2 in test). The most appropriate | 310 // This test saves 3 pages (one in setup and 2 in test). The most appropriate |
| 310 // test is related to |kTabId|, as it is saved in the latest moment and can be | 311 // test is related to |kTabId|, as it is saved in the latest moment and can be |
| 311 // used in the current tab. | 312 // used in the current tab. |
| 312 TEST_F(OfflinePageTabHelperTest, SelectBestPageForCurrentTab) { | 313 TEST_F(OfflinePageTabHelperTest, SelectBestPageForCurrentTab) { |
| 313 // Saves an offline page. | 314 // Saves an offline page. |
| 314 OfflinePageModel* model = | 315 OfflinePageModel* model = |
| 315 OfflinePageModelFactory::GetForBrowserContext(browser_context()); | 316 OfflinePageModelFactory::GetForBrowserContext(browser_context()); |
| 316 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( | 317 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 339 RunUntilIdle(); | 340 RunUntilIdle(); |
| 340 | 341 |
| 341 const OfflinePageItem* item = | 342 const OfflinePageItem* item = |
| 342 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); | 343 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); |
| 343 EXPECT_EQ(expected_offline_id, item->offline_id); | 344 EXPECT_EQ(expected_offline_id, item->offline_id); |
| 344 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); | 345 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); |
| 345 EXPECT_EQ(kLastNNamespace, item->client_id.name_space); | 346 EXPECT_EQ(kLastNNamespace, item->client_id.name_space); |
| 346 EXPECT_EQ(kTabId, item->client_id.id); | 347 EXPECT_EQ(kTabId, item->client_id.id); |
| 347 } | 348 } |
| 348 } // namespace offline_pages | 349 } // namespace offline_pages |
| OLD | NEW |