| 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 static_cast<int>(OfflinePageTabHelper::RedirectResult:: |
| 230 REDIRECTED_ON_CONNECTED_NETWORK), |
| 231 1); |
| 233 } | 232 } |
| 234 | 233 |
| 235 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnNoNetwork) { | 234 TEST_F(OfflinePageTabHelperTest, SwitchToOfflineFromOnlineOnNoNetwork) { |
| 236 SimulateHasNetworkConnectivity(false); | 235 SimulateHasNetworkConnectivity(false); |
| 237 | 236 |
| 238 StartLoad(online_url()); | 237 StartLoad(online_url()); |
| 239 // Gives a chance to run delayed task to do redirection. | 238 // Gives a chance to run delayed task to do redirection. |
| 240 RunUntilIdle(); | 239 RunUntilIdle(); |
| 241 // Redirection will be done immediately on navigation start. | 240 // Redirection will be done immediately on navigation start. |
| 242 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); | 241 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); |
| 243 histograms().ExpectTotalCount(kBadNetworkHistogram, 0); | 242 histograms().ExpectUniqueSample( |
| 244 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 1); | 243 kRedirectResultHistogram, |
| 245 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0); | 244 static_cast<int>(OfflinePageTabHelper::RedirectResult:: |
| 245 REDIRECTED_ON_DISCONNECTED_NETWORK), |
| 246 1); |
| 246 } | 247 } |
| 247 | 248 |
| 248 TEST_F(OfflinePageTabHelperTest, TestCurrentOfflinePage) { | 249 TEST_F(OfflinePageTabHelperTest, TestCurrentOfflinePage) { |
| 249 SimulateHasNetworkConnectivity(false); | 250 SimulateHasNetworkConnectivity(false); |
| 250 | 251 |
| 251 StartLoad(online_url()); | 252 StartLoad(online_url()); |
| 252 // Gives a chance to run delayed task to do redirection. | 253 // Gives a chance to run delayed task to do redirection. |
| 253 RunUntilIdle(); | 254 RunUntilIdle(); |
| 254 | 255 |
| 255 const OfflinePageItem* item = | 256 const OfflinePageItem* item = |
| (...skipping 12 matching lines...) Expand all Loading... |
| 268 SimulateHasNetworkConnectivity(true); | 269 SimulateHasNetworkConnectivity(true); |
| 269 | 270 |
| 270 StartLoad(online_url()); | 271 StartLoad(online_url()); |
| 271 RunUntilIdle(); | 272 RunUntilIdle(); |
| 272 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); | 273 EXPECT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); |
| 273 | 274 |
| 274 // Redirection will be done immediately on navigation end with error. | 275 // Redirection will be done immediately on navigation end with error. |
| 275 FailLoad(online_url()); | 276 FailLoad(online_url()); |
| 276 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); | 277 EXPECT_EQ(offline_url(), controller().GetPendingEntry()->GetURL()); |
| 277 | 278 |
| 278 histograms().ExpectBucketCount(kBadNetworkHistogram, false, 0); | 279 histograms().ExpectUniqueSample( |
| 279 histograms().ExpectBucketCount(kBadNetworkHistogram, true, 1); | 280 kRedirectResultHistogram, |
| 280 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 1); | 281 static_cast<int>(OfflinePageTabHelper::RedirectResult:: |
| 281 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0); | 282 REDIRECTED_ON_FLAKY_NETWORK), |
| 283 1); |
| 282 } | 284 } |
| 283 | 285 |
| 284 TEST_F(OfflinePageTabHelperTest, NewNavigationCancelsPendingRedirects) { | 286 TEST_F(OfflinePageTabHelperTest, NewNavigationCancelsPendingRedirects) { |
| 285 SimulateHasNetworkConnectivity(false); | 287 SimulateHasNetworkConnectivity(false); |
| 286 | 288 |
| 287 StartLoad(online_url()); | 289 StartLoad(online_url()); |
| 288 const GURL unsaved_url("http://test.org/page2"); | 290 const GURL unsaved_url("http://test.org/page2"); |
| 289 | 291 |
| 290 // We should have a pending task that will do the redirect. | 292 // We should have a pending task that will do the redirect. |
| 291 ASSERT_TRUE(offline_page_tab_helper()->weak_ptr_factory_.HasWeakPtrs()); | 293 ASSERT_TRUE(offline_page_tab_helper()->weak_ptr_factory_.HasWeakPtrs()); |
| 292 ASSERT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); | 294 ASSERT_EQ(online_url(), controller().GetPendingEntry()->GetURL()); |
| 293 | 295 |
| 294 // Should cancel pending tasks for previous URL. | 296 // Should cancel pending tasks for previous URL. |
| 295 StartLoad(unsaved_url); | 297 StartLoad(unsaved_url); |
| 296 | 298 |
| 297 // Gives a chance to run delayed task to do redirection. | 299 // Gives a chance to run delayed task to do redirection. |
| 298 RunUntilIdle(); | 300 RunUntilIdle(); |
| 299 | 301 |
| 300 // Redirection should be cancelled so we should still navigate to | 302 // Redirection should be cancelled so we should still navigate to |
| 301 // |unsaved_url|. | 303 // |unsaved_url|. |
| 302 EXPECT_EQ(unsaved_url, controller().GetPendingEntry()->GetURL()); | 304 EXPECT_EQ(unsaved_url, controller().GetPendingEntry()->GetURL()); |
| 303 | 305 |
| 304 histograms().ExpectTotalCount(kBadNetworkHistogram, 0); | 306 // Should report attempt of redirect, but the page not found. |
| 305 histograms().ExpectTotalCount(kRedirectToOfflineHistogram, 0); | 307 histograms().ExpectUniqueSample( |
| 306 histograms().ExpectTotalCount(kRedirectToOnlineHistogram, 0); | 308 kRedirectResultHistogram, |
| 309 static_cast<int>(OfflinePageTabHelper::RedirectResult:: |
| 310 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK), |
| 311 1); |
| 307 } | 312 } |
| 308 | 313 |
| 309 // This test saves 3 pages (one in setup and 2 in test). The most appropriate | 314 // 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 | 315 // test is related to |kTabId|, as it is saved in the latest moment and can be |
| 311 // used in the current tab. | 316 // used in the current tab. |
| 312 TEST_F(OfflinePageTabHelperTest, SelectBestPageForCurrentTab) { | 317 TEST_F(OfflinePageTabHelperTest, SelectBestPageForCurrentTab) { |
| 313 // Saves an offline page. | 318 // Saves an offline page. |
| 314 OfflinePageModel* model = | 319 OfflinePageModel* model = |
| 315 OfflinePageModelFactory::GetForBrowserContext(browser_context()); | 320 OfflinePageModelFactory::GetForBrowserContext(browser_context()); |
| 316 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( | 321 std::unique_ptr<OfflinePageTestArchiver> archiver(BuildArchiver( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 339 RunUntilIdle(); | 344 RunUntilIdle(); |
| 340 | 345 |
| 341 const OfflinePageItem* item = | 346 const OfflinePageItem* item = |
| 342 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); | 347 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()); |
| 343 EXPECT_EQ(expected_offline_id, item->offline_id); | 348 EXPECT_EQ(expected_offline_id, item->offline_id); |
| 344 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); | 349 EXPECT_EQ(expected_offline_url, item->GetOfflineURL()); |
| 345 EXPECT_EQ(kLastNNamespace, item->client_id.name_space); | 350 EXPECT_EQ(kLastNNamespace, item->client_id.name_space); |
| 346 EXPECT_EQ(kTabId, item->client_id.id); | 351 EXPECT_EQ(kTabId, item->client_id.id); |
| 347 } | 352 } |
| 348 } // namespace offline_pages | 353 } // namespace offline_pages |
| OLD | NEW |