| 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/previews/previews_infobar_delegate.h" | 5 #include "chrome/browser/previews/previews_infobar_delegate.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/optional.h" | 13 #include "base/optional.h" |
| 14 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "chrome/browser/android/android_theme_resources.h" | 16 #include "chrome/browser/android/android_theme_resources.h" |
| 17 #include "chrome/browser/infobars/infobar_service.h" | 17 #include "chrome/browser/infobars/infobar_service.h" |
| 18 #include "chrome/browser/loader/chrome_navigation_data.h" |
| 18 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" | 19 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" |
| 19 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" | 20 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" |
| 20 #include "chrome/browser/previews/previews_infobar_tab_helper.h" | 21 #include "chrome/browser/previews/previews_infobar_tab_helper.h" |
| 21 #include "chrome/grit/generated_resources.h" | 22 #include "chrome/grit/generated_resources.h" |
| 22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 23 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 24 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" |
| 25 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_ping
back_client.h" |
| 26 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_serv
ice.h" |
| 27 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_sett
ings.h" |
| 23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test
_utils.h" | 28 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test
_utils.h" |
| 24 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_
names.h" | 29 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_
names.h" |
| 25 #include "components/infobars/core/confirm_infobar_delegate.h" | 30 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 26 #include "components/infobars/core/infobar.h" | 31 #include "components/infobars/core/infobar.h" |
| 27 #include "components/infobars/core/infobar_delegate.h" | 32 #include "components/infobars/core/infobar_delegate.h" |
| 28 #include "components/prefs/pref_registry_simple.h" | 33 #include "components/prefs/pref_registry_simple.h" |
| 29 #include "components/proxy_config/proxy_config_pref_names.h" | 34 #include "components/proxy_config/proxy_config_pref_names.h" |
| 30 #include "content/public/browser/navigation_entry.h" | 35 #include "content/public/browser/navigation_entry.h" |
| 31 #include "content/public/browser/navigation_handle.h" | 36 #include "content/public/browser/navigation_handle.h" |
| 32 #include "content/public/browser/web_contents.h" | 37 #include "content/public/browser/web_contents.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 60 public: | 65 public: |
| 61 explicit TestPreviewsWebContentsObserver(content::WebContents* web_contents) | 66 explicit TestPreviewsWebContentsObserver(content::WebContents* web_contents) |
| 62 : content::WebContentsObserver(web_contents), | 67 : content::WebContentsObserver(web_contents), |
| 63 last_navigation_reload_type_(content::ReloadType::NONE) {} | 68 last_navigation_reload_type_(content::ReloadType::NONE) {} |
| 64 ~TestPreviewsWebContentsObserver() override {} | 69 ~TestPreviewsWebContentsObserver() override {} |
| 65 | 70 |
| 66 content::ReloadType last_navigation_reload_type() { | 71 content::ReloadType last_navigation_reload_type() { |
| 67 return last_navigation_reload_type_; | 72 return last_navigation_reload_type_; |
| 68 } | 73 } |
| 69 | 74 |
| 75 void DidStartNavigation( |
| 76 content::NavigationHandle* navigation_handle) override { |
| 77 ChromeNavigationData* chrome_navigation_data = new ChromeNavigationData(); |
| 78 content::WebContentsTester::For(web_contents()) |
| 79 ->SetNavigationData(navigation_handle, |
| 80 base::WrapUnique(chrome_navigation_data)); |
| 81 data_reduction_proxy::DataReductionProxyData* data = |
| 82 new data_reduction_proxy::DataReductionProxyData(); |
| 83 chrome_navigation_data->SetDataReductionProxyData(base::WrapUnique(data)); |
| 84 data->set_used_data_reduction_proxy(true); |
| 85 data->set_page_id(1u); |
| 86 } |
| 87 |
| 70 void DidFinishNavigation( | 88 void DidFinishNavigation( |
| 71 content::NavigationHandle* navigation_handle) override { | 89 content::NavigationHandle* navigation_handle) override { |
| 72 last_navigation_reload_type_ = navigation_handle->GetReloadType(); | 90 last_navigation_reload_type_ = navigation_handle->GetReloadType(); |
| 73 } | 91 } |
| 74 | 92 |
| 75 private: | 93 private: |
| 76 content::ReloadType last_navigation_reload_type_; | 94 content::ReloadType last_navigation_reload_type_; |
| 77 }; | 95 }; |
| 78 | 96 |
| 79 } // namespace | 97 } // namespace |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); | 213 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 196 | 214 |
| 197 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 215 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 198 EXPECT_FALSE(user_opt_out_.value()); | 216 EXPECT_FALSE(user_opt_out_.value()); |
| 199 | 217 |
| 200 tester.ExpectBucketCount(kUMAPreviewsInfoBarActionLoFi, | 218 tester.ExpectBucketCount(kUMAPreviewsInfoBarActionLoFi, |
| 201 PreviewsInfoBarDelegate::INFOBAR_DISMISSED_BY_RELOAD, | 219 PreviewsInfoBarDelegate::INFOBAR_DISMISSED_BY_RELOAD, |
| 202 1); | 220 1); |
| 203 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( | 221 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( |
| 204 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); | 222 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); |
| 223 |
| 224 auto* data_reduction_proxy_settings = |
| 225 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 226 web_contents()->GetBrowserContext()); |
| 227 |
| 228 EXPECT_EQ(0u, data_reduction_proxy_settings->data_reduction_proxy_service() |
| 229 ->pingback_client() |
| 230 ->OptOutsSizeForTesting()); |
| 205 } | 231 } |
| 206 | 232 |
| 207 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestUserDismissal) { | 233 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestUserDismissal) { |
| 208 base::HistogramTester tester; | 234 base::HistogramTester tester; |
| 209 | 235 |
| 210 ConfirmInfoBarDelegate* infobar = CreateInfoBar( | 236 ConfirmInfoBarDelegate* infobar = CreateInfoBar( |
| 211 PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); | 237 PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); |
| 212 | 238 |
| 213 // Simulate dismissing the infobar. | 239 // Simulate dismissing the infobar. |
| 214 infobar->InfoBarDismissed(); | 240 infobar->InfoBarDismissed(); |
| 215 infobar_service()->infobar_at(0)->RemoveSelf(); | 241 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 216 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 242 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 217 | 243 |
| 218 tester.ExpectBucketCount(kUMAPreviewsInfoBarActionLoFi, | 244 tester.ExpectBucketCount(kUMAPreviewsInfoBarActionLoFi, |
| 219 PreviewsInfoBarDelegate::INFOBAR_DISMISSED_BY_USER, | 245 PreviewsInfoBarDelegate::INFOBAR_DISMISSED_BY_USER, |
| 220 1); | 246 1); |
| 221 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( | 247 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( |
| 222 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); | 248 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); |
| 223 EXPECT_FALSE(user_opt_out_.value()); | 249 EXPECT_FALSE(user_opt_out_.value()); |
| 224 } | 250 } |
| 225 | 251 |
| 226 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLoFi) { | 252 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLoFi) { |
| 227 base::HistogramTester tester; | 253 base::HistogramTester tester; |
| 228 | 254 |
| 255 NavigateAndCommit(GURL(kTestUrl)); |
| 256 |
| 229 ConfirmInfoBarDelegate* infobar = CreateInfoBar( | 257 ConfirmInfoBarDelegate* infobar = CreateInfoBar( |
| 230 PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); | 258 PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); |
| 231 | 259 |
| 232 // Simulate clicking the infobar link. | 260 // Simulate clicking the infobar link. |
| 233 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) | 261 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) |
| 234 infobar_service()->infobar_at(0)->RemoveSelf(); | 262 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 235 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 263 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 236 | 264 |
| 237 tester.ExpectBucketCount( | 265 tester.ExpectBucketCount( |
| 238 kUMAPreviewsInfoBarActionLoFi, | 266 kUMAPreviewsInfoBarActionLoFi, |
| 239 PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1); | 267 PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1); |
| 240 EXPECT_EQ(1, drp_test_context_->pref_service()->GetInteger( | 268 EXPECT_EQ(1, drp_test_context_->pref_service()->GetInteger( |
| 241 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); | 269 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); |
| 242 EXPECT_TRUE(user_opt_out_.value()); | 270 EXPECT_TRUE(user_opt_out_.value()); |
| 271 |
| 272 auto* data_reduction_proxy_settings = |
| 273 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 274 web_contents()->GetBrowserContext()); |
| 275 |
| 276 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() |
| 277 ->pingback_client() |
| 278 ->OptOutsSizeForTesting()); |
| 243 } | 279 } |
| 244 | 280 |
| 245 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLitePage) { | 281 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLitePage) { |
| 246 base::HistogramTester tester; | 282 base::HistogramTester tester; |
| 247 | 283 |
| 248 NavigateAndCommit(GURL(kTestUrl)); | 284 NavigateAndCommit(GURL(kTestUrl)); |
| 249 | 285 |
| 250 ConfirmInfoBarDelegate* infobar = CreateInfoBar( | 286 ConfirmInfoBarDelegate* infobar = CreateInfoBar( |
| 251 PreviewsInfoBarDelegate::LITE_PAGE, true /* is_data_saver_user */); | 287 PreviewsInfoBarDelegate::LITE_PAGE, true /* is_data_saver_user */); |
| 252 | 288 |
| 253 // Simulate clicking the infobar link. | 289 // Simulate clicking the infobar link. |
| 254 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) | 290 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) |
| 255 infobar_service()->infobar_at(0)->RemoveSelf(); | 291 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 256 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 292 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 257 | 293 |
| 258 tester.ExpectBucketCount( | 294 tester.ExpectBucketCount( |
| 259 kUMAPreviewsInfoBarActionLitePage, | 295 kUMAPreviewsInfoBarActionLitePage, |
| 260 PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1); | 296 PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1); |
| 261 | 297 |
| 262 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); | 298 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 263 | 299 |
| 264 EXPECT_EQ(content::ReloadType::DISABLE_LOFI_MODE, | 300 EXPECT_EQ(content::ReloadType::DISABLE_LOFI_MODE, |
| 265 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) | 301 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) |
| 266 ->last_navigation_reload_type()); | 302 ->last_navigation_reload_type()); |
| 303 |
| 304 auto* data_reduction_proxy_settings = |
| 305 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 306 web_contents()->GetBrowserContext()); |
| 307 |
| 308 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() |
| 309 ->pingback_client() |
| 310 ->OptOutsSizeForTesting()); |
| 267 } | 311 } |
| 268 | 312 |
| 269 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestShownOncePerNavigation) { | 313 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestShownOncePerNavigation) { |
| 270 ConfirmInfoBarDelegate* infobar = CreateInfoBar( | 314 ConfirmInfoBarDelegate* infobar = CreateInfoBar( |
| 271 PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); | 315 PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); |
| 272 | 316 |
| 273 // Simulate dismissing the infobar. | 317 // Simulate dismissing the infobar. |
| 274 infobar->InfoBarDismissed(); | 318 infobar->InfoBarDismissed(); |
| 275 infobar_service()->infobar_at(0)->RemoveSelf(); | 319 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 276 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 320 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 // Simulate clicking the infobar link. | 441 // Simulate clicking the infobar link. |
| 398 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) | 442 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) |
| 399 infobar_service()->infobar_at(0)->RemoveSelf(); | 443 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 400 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 444 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 401 | 445 |
| 402 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); | 446 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 403 | 447 |
| 404 EXPECT_EQ(content::ReloadType::DISABLE_LOFI_MODE, | 448 EXPECT_EQ(content::ReloadType::DISABLE_LOFI_MODE, |
| 405 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) | 449 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) |
| 406 ->last_navigation_reload_type()); | 450 ->last_navigation_reload_type()); |
| 451 |
| 452 auto* data_reduction_proxy_settings = |
| 453 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 454 web_contents()->GetBrowserContext()); |
| 455 |
| 456 EXPECT_EQ(0u, data_reduction_proxy_settings->data_reduction_proxy_service() |
| 457 ->pingback_client() |
| 458 ->OptOutsSizeForTesting()); |
| 407 } | 459 } |
| 460 |
| 461 TEST_F(PreviewsInfoBarDelegateUnitTest, PingbackClientClearedTabClosed) { |
| 462 base::HistogramTester tester; |
| 463 |
| 464 NavigateAndCommit(GURL(kTestUrl)); |
| 465 |
| 466 ConfirmInfoBarDelegate* infobar = CreateInfoBar( |
| 467 PreviewsInfoBarDelegate::LITE_PAGE, true /* is_data_saver_user */); |
| 468 |
| 469 // Simulate clicking the infobar link. |
| 470 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) |
| 471 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 472 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 473 |
| 474 tester.ExpectBucketCount( |
| 475 kUMAPreviewsInfoBarActionLitePage, |
| 476 PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1); |
| 477 |
| 478 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 479 |
| 480 EXPECT_EQ(content::ReloadType::DISABLE_LOFI_MODE, |
| 481 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) |
| 482 ->last_navigation_reload_type()); |
| 483 |
| 484 auto* data_reduction_proxy_settings = |
| 485 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 486 web_contents()->GetBrowserContext()); |
| 487 |
| 488 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() |
| 489 ->pingback_client() |
| 490 ->OptOutsSizeForTesting()); |
| 491 |
| 492 { |
| 493 std::unique_ptr<content::NavigationHandle> navigation_handle( |
| 494 content::NavigationHandle::CreateNavigationHandleForTesting( |
| 495 GURL("url"), main_rfh(), true)); |
| 496 } |
| 497 |
| 498 SetContents(nullptr); |
| 499 |
| 500 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() |
| 501 ->pingback_client() |
| 502 ->OptOutsSizeForTesting()); |
| 503 |
| 504 drp_test_context_->RunUntilIdle(); |
| 505 |
| 506 EXPECT_EQ(0u, data_reduction_proxy_settings->data_reduction_proxy_service() |
| 507 ->pingback_client() |
| 508 ->OptOutsSizeForTesting()); |
| 509 } |
| OLD | NEW |