| 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 <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/feature_list.h" | 13 #include "base/feature_list.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/field_trial.h" | 15 #include "base/metrics/field_trial.h" |
| 16 #include "base/metrics/field_trial_param_associator.h" | 16 #include "base/metrics/field_trial_param_associator.h" |
| 17 #include "base/metrics/field_trial_params.h" | 17 #include "base/metrics/field_trial_params.h" |
| 18 #include "base/optional.h" | 18 #include "base/optional.h" |
| 19 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
| 20 #include "base/test/histogram_tester.h" | 20 #include "base/test/histogram_tester.h" |
| 21 #include "base/test/scoped_feature_list.h" | 21 #include "base/test/scoped_feature_list.h" |
| 22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
| 23 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 24 #include "chrome/browser/android/android_theme_resources.h" | 24 #include "chrome/browser/android/android_theme_resources.h" |
| 25 #include "chrome/browser/infobars/infobar_service.h" | 25 #include "chrome/browser/infobars/infobar_service.h" |
| 26 #include "chrome/browser/loader/chrome_navigation_data.h" | 26 #include "chrome/browser/loader/chrome_navigation_data.h" |
| 27 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" | 27 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" |
| 28 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" | 28 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" |
| 29 #include "chrome/browser/page_load_metrics/observers/page_load_metrics_observer_
test_harness.h" |
| 29 #include "chrome/browser/previews/previews_infobar_tab_helper.h" | 30 #include "chrome/browser/previews/previews_infobar_tab_helper.h" |
| 30 #include "chrome/grit/generated_resources.h" | 31 #include "chrome/grit/generated_resources.h" |
| 31 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 32 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 32 #include "chrome/test/base/testing_browser_process.h" | 33 #include "chrome/test/base/testing_browser_process.h" |
| 33 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_conf
ig_test_utils.h" | 34 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_conf
ig_test_utils.h" |
| 34 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" | 35 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" |
| 35 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_ping
back_client.h" | 36 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_ping
back_client.h" |
| 36 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_serv
ice.h" | 37 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_serv
ice.h" |
| 37 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_sett
ings.h" | 38 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_sett
ings.h" |
| 38 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test
_utils.h" | 39 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test
_utils.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 113 |
| 113 void set_should_have_page_id(bool should_have_page_id) { | 114 void set_should_have_page_id(bool should_have_page_id) { |
| 114 should_have_page_id_ = should_have_page_id; | 115 should_have_page_id_ = should_have_page_id; |
| 115 } | 116 } |
| 116 | 117 |
| 117 private: | 118 private: |
| 118 content::ReloadType last_navigation_reload_type_; | 119 content::ReloadType last_navigation_reload_type_; |
| 119 bool should_have_page_id_; | 120 bool should_have_page_id_; |
| 120 }; | 121 }; |
| 121 | 122 |
| 123 class TestOptOutObserver : public page_load_metrics::PageLoadMetricsObserver { |
| 124 public: |
| 125 explicit TestOptOutObserver(const base::Callback<void()>& callback) |
| 126 : callback_(callback) {} |
| 127 ~TestOptOutObserver() override {} |
| 128 |
| 129 void OnEventOccurred(const void* const event_key) override { |
| 130 if (PreviewsInfoBarDelegate::OptOutEventKey() == event_key) |
| 131 callback_.Run(); |
| 132 } |
| 133 |
| 134 base::Callback<void()> callback_; |
| 135 }; |
| 136 |
| 122 } // namespace | 137 } // namespace |
| 123 | 138 |
| 124 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TestPreviewsWebContentsObserver); | 139 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TestPreviewsWebContentsObserver); |
| 125 | 140 |
| 126 class PreviewsInfoBarDelegateUnitTest : public ChromeRenderViewHostTestHarness { | 141 class PreviewsInfoBarDelegateUnitTest |
| 142 : public page_load_metrics::PageLoadMetricsObserverTestHarness { |
| 127 protected: | 143 protected: |
| 128 PreviewsInfoBarDelegateUnitTest() | 144 PreviewsInfoBarDelegateUnitTest() |
| 129 : field_trial_list_(new base::FieldTrialList(nullptr)), | 145 : field_trial_list_(new base::FieldTrialList(nullptr)), |
| 130 tester_(new base::HistogramTester()) {} | 146 tester_(new base::HistogramTester()) {} |
| 131 | 147 |
| 132 void SetUp() override { | 148 void SetUp() override { |
| 133 ChromeRenderViewHostTestHarness::SetUp(); | 149 PageLoadMetricsObserverTestHarness::SetUp(); |
| 134 InfoBarService::CreateForWebContents(web_contents()); | 150 InfoBarService::CreateForWebContents(web_contents()); |
| 135 PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); | 151 PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); |
| 136 TestPreviewsWebContentsObserver::CreateForWebContents(web_contents()); | 152 TestPreviewsWebContentsObserver::CreateForWebContents(web_contents()); |
| 137 | 153 |
| 138 drp_test_context_ = | 154 drp_test_context_ = |
| 139 data_reduction_proxy::DataReductionProxyTestContext::Builder() | 155 data_reduction_proxy::DataReductionProxyTestContext::Builder() |
| 140 .WithMockConfig() | 156 .WithMockConfig() |
| 141 .SkipSettingsInitialization() | 157 .SkipSettingsInitialization() |
| 142 .Build(); | 158 .Build(); |
| 143 | 159 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 } | 241 } |
| 226 | 242 |
| 227 void OnDismissPreviewsInfobar(bool user_opt_out) { | 243 void OnDismissPreviewsInfobar(bool user_opt_out) { |
| 228 user_opt_out_ = user_opt_out; | 244 user_opt_out_ = user_opt_out; |
| 229 } | 245 } |
| 230 | 246 |
| 231 InfoBarService* infobar_service() { | 247 InfoBarService* infobar_service() { |
| 232 return InfoBarService::FromWebContents(web_contents()); | 248 return InfoBarService::FromWebContents(web_contents()); |
| 233 } | 249 } |
| 234 | 250 |
| 251 void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override { |
| 252 tracker->AddObserver(base::MakeUnique<TestOptOutObserver>(base::Bind( |
| 253 &PreviewsInfoBarDelegateUnitTest::OptOut, base::Unretained(this)))); |
| 254 } |
| 255 |
| 256 void OptOut() { opt_out_called_ = true; } |
| 257 |
| 258 bool opt_out_called_ = false; |
| 259 |
| 235 std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> | 260 std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> |
| 236 drp_test_context_; | 261 drp_test_context_; |
| 237 | 262 |
| 238 base::Optional<bool> user_opt_out_; | 263 base::Optional<bool> user_opt_out_; |
| 239 std::unique_ptr<base::FieldTrialList> field_trial_list_; | 264 std::unique_ptr<base::FieldTrialList> field_trial_list_; |
| 240 base::test::ScopedFeatureList scoped_feature_list_; | 265 base::test::ScopedFeatureList scoped_feature_list_; |
| 241 std::unique_ptr<base::HistogramTester> tester_; | 266 std::unique_ptr<base::HistogramTester> tester_; |
| 242 }; | 267 }; |
| 243 | 268 |
| 244 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestNavigationDismissal) { | 269 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestNavigationDismissal) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( | 321 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( |
| 297 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); | 322 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); |
| 298 | 323 |
| 299 auto* data_reduction_proxy_settings = | 324 auto* data_reduction_proxy_settings = |
| 300 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | 325 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 301 web_contents()->GetBrowserContext()); | 326 web_contents()->GetBrowserContext()); |
| 302 | 327 |
| 303 EXPECT_EQ(0u, data_reduction_proxy_settings->data_reduction_proxy_service() | 328 EXPECT_EQ(0u, data_reduction_proxy_settings->data_reduction_proxy_service() |
| 304 ->pingback_client() | 329 ->pingback_client() |
| 305 ->OptOutsSizeForTesting()); | 330 ->OptOutsSizeForTesting()); |
| 331 |
| 332 EXPECT_FALSE(opt_out_called_); |
| 306 } | 333 } |
| 307 | 334 |
| 308 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestUserDismissal) { | 335 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestUserDismissal) { |
| 309 ConfirmInfoBarDelegate* infobar = | 336 ConfirmInfoBarDelegate* infobar = |
| 310 CreateInfoBar(previews::PreviewsType::LOFI, base::Time(), | 337 CreateInfoBar(previews::PreviewsType::LOFI, base::Time(), |
| 311 true /* is_data_saver_user */, false /* is_reload */); | 338 true /* is_data_saver_user */, false /* is_reload */); |
| 312 | 339 |
| 313 // Simulate dismissing the infobar. | 340 // Simulate dismissing the infobar. |
| 314 infobar->InfoBarDismissed(); | 341 infobar->InfoBarDismissed(); |
| 315 infobar_service()->infobar_at(0)->RemoveSelf(); | 342 infobar_service()->infobar_at(0)->RemoveSelf(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 340 } | 367 } |
| 341 | 368 |
| 342 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLoFi) { | 369 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLoFi) { |
| 343 NavigateAndCommit(GURL(kTestUrl)); | 370 NavigateAndCommit(GURL(kTestUrl)); |
| 344 const struct { | 371 const struct { |
| 345 bool using_previews_blacklist; | 372 bool using_previews_blacklist; |
| 346 } tests[] = { | 373 } tests[] = { |
| 347 {true}, {false}, | 374 {true}, {false}, |
| 348 }; | 375 }; |
| 349 for (const auto test : tests) { | 376 for (const auto test : tests) { |
| 377 opt_out_called_ = false; |
| 350 tester_.reset(new base::HistogramTester()); | 378 tester_.reset(new base::HistogramTester()); |
| 351 drp_test_context_->config()->ResetLoFiStatusForTest(); | 379 drp_test_context_->config()->ResetLoFiStatusForTest(); |
| 352 field_trial_list_.reset(); | 380 field_trial_list_.reset(); |
| 353 field_trial_list_.reset(new base::FieldTrialList(nullptr)); | 381 field_trial_list_.reset(new base::FieldTrialList(nullptr)); |
| 354 if (test.using_previews_blacklist) { | 382 if (test.using_previews_blacklist) { |
| 355 base::FieldTrialList::CreateFieldTrial( | 383 base::FieldTrialList::CreateFieldTrial( |
| 356 "DataReductionProxyPreviewsBlackListTransition", "Enabled_"); | 384 "DataReductionProxyPreviewsBlackListTransition", "Enabled_"); |
| 357 } | 385 } |
| 358 | 386 |
| 359 // Call Reload and CommitPendingNavigation to force DidFinishNavigation. | 387 // Call Reload and CommitPendingNavigation to force DidFinishNavigation. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 376 drp_test_context_->pref_service()->GetInteger( | 404 drp_test_context_->pref_service()->GetInteger( |
| 377 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); | 405 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); |
| 378 EXPECT_TRUE(user_opt_out_.value()); | 406 EXPECT_TRUE(user_opt_out_.value()); |
| 379 | 407 |
| 380 auto* data_reduction_proxy_settings = | 408 auto* data_reduction_proxy_settings = |
| 381 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | 409 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 382 web_contents()->GetBrowserContext()); | 410 web_contents()->GetBrowserContext()); |
| 383 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() | 411 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() |
| 384 ->pingback_client() | 412 ->pingback_client() |
| 385 ->OptOutsSizeForTesting()); | 413 ->OptOutsSizeForTesting()); |
| 414 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() |
| 415 ->pingback_client() |
| 416 ->OptOutsSizeForTesting()); |
| 417 |
| 418 EXPECT_TRUE(opt_out_called_); |
| 386 } | 419 } |
| 387 } | 420 } |
| 388 | 421 |
| 389 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLitePage) { | 422 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLitePage) { |
| 390 NavigateAndCommit(GURL(kTestUrl)); | 423 NavigateAndCommit(GURL(kTestUrl)); |
| 391 | |
| 392 ConfirmInfoBarDelegate* infobar = | 424 ConfirmInfoBarDelegate* infobar = |
| 393 CreateInfoBar(previews::PreviewsType::LITE_PAGE, base::Time(), | 425 CreateInfoBar(previews::PreviewsType::LITE_PAGE, base::Time(), |
| 394 true /* is_data_saver_user */, false /* is_reload */); | 426 true /* is_data_saver_user */, false /* is_reload */); |
| 395 | 427 |
| 396 // Simulate clicking the infobar link. | 428 // Simulate clicking the infobar link. |
| 397 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) | 429 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) |
| 398 infobar_service()->infobar_at(0)->RemoveSelf(); | 430 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 399 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 431 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 400 | 432 |
| 401 tester_->ExpectBucketCount( | 433 tester_->ExpectBucketCount( |
| 402 kUMAPreviewsInfoBarActionLitePage, | 434 kUMAPreviewsInfoBarActionLitePage, |
| 403 PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1); | 435 PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1); |
| 404 | 436 |
| 405 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); | 437 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 406 | 438 |
| 407 EXPECT_EQ(content::ReloadType::DISABLE_LOFI_MODE, | 439 EXPECT_EQ(content::ReloadType::DISABLE_LOFI_MODE, |
| 408 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) | 440 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) |
| 409 ->last_navigation_reload_type()); | 441 ->last_navigation_reload_type()); |
| 410 | 442 |
| 411 auto* data_reduction_proxy_settings = | 443 auto* data_reduction_proxy_settings = |
| 412 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | 444 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 413 web_contents()->GetBrowserContext()); | 445 web_contents()->GetBrowserContext()); |
| 414 | 446 |
| 415 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() | 447 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() |
| 416 ->pingback_client() | 448 ->pingback_client() |
| 417 ->OptOutsSizeForTesting()); | 449 ->OptOutsSizeForTesting()); |
| 450 EXPECT_TRUE(opt_out_called_); |
| 418 } | 451 } |
| 419 | 452 |
| 420 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestShownOncePerNavigation) { | 453 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestShownOncePerNavigation) { |
| 421 ConfirmInfoBarDelegate* infobar = | 454 ConfirmInfoBarDelegate* infobar = |
| 422 CreateInfoBar(previews::PreviewsType::LOFI, base::Time(), | 455 CreateInfoBar(previews::PreviewsType::LOFI, base::Time(), |
| 423 true /* is_data_saver_user */, false /* is_reload */); | 456 true /* is_data_saver_user */, false /* is_reload */); |
| 424 | 457 |
| 425 // Simulate dismissing the infobar. | 458 // Simulate dismissing the infobar. |
| 426 infobar->InfoBarDismissed(); | 459 infobar->InfoBarDismissed(); |
| 427 infobar_service()->infobar_at(0)->RemoveSelf(); | 460 infobar_service()->infobar_at(0)->RemoveSelf(); |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) | 593 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) |
| 561 ->last_navigation_reload_type()); | 594 ->last_navigation_reload_type()); |
| 562 | 595 |
| 563 auto* data_reduction_proxy_settings = | 596 auto* data_reduction_proxy_settings = |
| 564 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | 597 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 565 web_contents()->GetBrowserContext()); | 598 web_contents()->GetBrowserContext()); |
| 566 | 599 |
| 567 EXPECT_EQ(0u, data_reduction_proxy_settings->data_reduction_proxy_service() | 600 EXPECT_EQ(0u, data_reduction_proxy_settings->data_reduction_proxy_service() |
| 568 ->pingback_client() | 601 ->pingback_client() |
| 569 ->OptOutsSizeForTesting()); | 602 ->OptOutsSizeForTesting()); |
| 603 |
| 604 EXPECT_TRUE(opt_out_called_); |
| 570 } | 605 } |
| 571 | 606 |
| 572 TEST_F(PreviewsInfoBarDelegateUnitTest, PingbackClientClearedTabClosed) { | 607 TEST_F(PreviewsInfoBarDelegateUnitTest, PingbackClientClearedTabClosed) { |
| 573 NavigateAndCommit(GURL(kTestUrl)); | 608 NavigateAndCommit(GURL(kTestUrl)); |
| 574 | 609 |
| 575 ConfirmInfoBarDelegate* infobar = | 610 ConfirmInfoBarDelegate* infobar = |
| 576 CreateInfoBar(previews::PreviewsType::LITE_PAGE, base::Time(), | 611 CreateInfoBar(previews::PreviewsType::LITE_PAGE, base::Time(), |
| 577 true /* is_data_saver_user */, false /* is_reload */); | 612 true /* is_data_saver_user */, false /* is_reload */); |
| 578 | 613 |
| 579 // Simulate clicking the infobar link. | 614 // Simulate clicking the infobar link. |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 l10n_util::GetStringFUTF16(IDS_PREVIEWS_INFOBAR_TIMESTAMP_MINUTES, | 735 l10n_util::GetStringFUTF16(IDS_PREVIEWS_INFOBAR_TIMESTAMP_MINUTES, |
| 701 base::IntToString16(staleness_in_minutes)), | 736 base::IntToString16(staleness_in_minutes)), |
| 702 PreviewsInfoBarDelegate::TIMESTAMP_SHOWN); | 737 PreviewsInfoBarDelegate::TIMESTAMP_SHOWN); |
| 703 | 738 |
| 704 staleness_in_minutes = 1; | 739 staleness_in_minutes = 1; |
| 705 TestStalePreviews( | 740 TestStalePreviews( |
| 706 staleness_in_minutes, true /* is_reload */, | 741 staleness_in_minutes, true /* is_reload */, |
| 707 l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_TIMESTAMP_UPDATED_NOW), | 742 l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_TIMESTAMP_UPDATED_NOW), |
| 708 PreviewsInfoBarDelegate::TIMESTAMP_UPDATED_NOW_SHOWN); | 743 PreviewsInfoBarDelegate::TIMESTAMP_UPDATED_NOW_SHOWN); |
| 709 } | 744 } |
| OLD | NEW |