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 : opt_out_called_(false), |
| 146 field_trial_list_(new base::FieldTrialList(nullptr)), |
130 tester_(new base::HistogramTester()) {} | 147 tester_(new base::HistogramTester()) {} |
131 | 148 |
132 void SetUp() override { | 149 void SetUp() override { |
133 ChromeRenderViewHostTestHarness::SetUp(); | 150 PageLoadMetricsObserverTestHarness::SetUp(); |
134 InfoBarService::CreateForWebContents(web_contents()); | 151 InfoBarService::CreateForWebContents(web_contents()); |
135 PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); | 152 PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); |
136 TestPreviewsWebContentsObserver::CreateForWebContents(web_contents()); | 153 TestPreviewsWebContentsObserver::CreateForWebContents(web_contents()); |
137 | 154 |
138 drp_test_context_ = | 155 drp_test_context_ = |
139 data_reduction_proxy::DataReductionProxyTestContext::Builder() | 156 data_reduction_proxy::DataReductionProxyTestContext::Builder() |
140 .WithMockConfig() | 157 .WithMockConfig() |
141 .SkipSettingsInitialization() | 158 .SkipSettingsInitialization() |
142 .Build(); | 159 .Build(); |
143 | 160 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 } | 242 } |
226 | 243 |
227 void OnDismissPreviewsInfobar(bool user_opt_out) { | 244 void OnDismissPreviewsInfobar(bool user_opt_out) { |
228 user_opt_out_ = user_opt_out; | 245 user_opt_out_ = user_opt_out; |
229 } | 246 } |
230 | 247 |
231 InfoBarService* infobar_service() { | 248 InfoBarService* infobar_service() { |
232 return InfoBarService::FromWebContents(web_contents()); | 249 return InfoBarService::FromWebContents(web_contents()); |
233 } | 250 } |
234 | 251 |
| 252 void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override { |
| 253 tracker->AddObserver(base::MakeUnique<TestOptOutObserver>(base::Bind( |
| 254 &PreviewsInfoBarDelegateUnitTest::OptOut, base::Unretained(this)))); |
| 255 } |
| 256 |
| 257 void OptOut() { opt_out_called_ = true; } |
| 258 |
| 259 bool opt_out_called_; |
| 260 |
235 std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> | 261 std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> |
236 drp_test_context_; | 262 drp_test_context_; |
237 | 263 |
238 base::Optional<bool> user_opt_out_; | 264 base::Optional<bool> user_opt_out_; |
239 std::unique_ptr<base::FieldTrialList> field_trial_list_; | 265 std::unique_ptr<base::FieldTrialList> field_trial_list_; |
240 base::test::ScopedFeatureList scoped_feature_list_; | 266 base::test::ScopedFeatureList scoped_feature_list_; |
241 std::unique_ptr<base::HistogramTester> tester_; | 267 std::unique_ptr<base::HistogramTester> tester_; |
242 }; | 268 }; |
243 | 269 |
244 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestNavigationDismissal) { | 270 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( | 322 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( |
297 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); | 323 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); |
298 | 324 |
299 auto* data_reduction_proxy_settings = | 325 auto* data_reduction_proxy_settings = |
300 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | 326 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
301 web_contents()->GetBrowserContext()); | 327 web_contents()->GetBrowserContext()); |
302 | 328 |
303 EXPECT_EQ(0u, data_reduction_proxy_settings->data_reduction_proxy_service() | 329 EXPECT_EQ(0u, data_reduction_proxy_settings->data_reduction_proxy_service() |
304 ->pingback_client() | 330 ->pingback_client() |
305 ->OptOutsSizeForTesting()); | 331 ->OptOutsSizeForTesting()); |
| 332 |
| 333 EXPECT_FALSE(opt_out_called_); |
306 } | 334 } |
307 | 335 |
308 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestUserDismissal) { | 336 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestUserDismissal) { |
309 ConfirmInfoBarDelegate* infobar = | 337 ConfirmInfoBarDelegate* infobar = |
310 CreateInfoBar(previews::PreviewsType::LOFI, base::Time(), | 338 CreateInfoBar(previews::PreviewsType::LOFI, base::Time(), |
311 true /* is_data_saver_user */, false /* is_reload */); | 339 true /* is_data_saver_user */, false /* is_reload */); |
312 | 340 |
313 // Simulate dismissing the infobar. | 341 // Simulate dismissing the infobar. |
314 infobar->InfoBarDismissed(); | 342 infobar->InfoBarDismissed(); |
315 infobar_service()->infobar_at(0)->RemoveSelf(); | 343 infobar_service()->infobar_at(0)->RemoveSelf(); |
(...skipping 24 matching lines...) Expand all Loading... |
340 } | 368 } |
341 | 369 |
342 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLoFi) { | 370 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLoFi) { |
343 NavigateAndCommit(GURL(kTestUrl)); | 371 NavigateAndCommit(GURL(kTestUrl)); |
344 const struct { | 372 const struct { |
345 bool using_previews_blacklist; | 373 bool using_previews_blacklist; |
346 } tests[] = { | 374 } tests[] = { |
347 {true}, {false}, | 375 {true}, {false}, |
348 }; | 376 }; |
349 for (const auto test : tests) { | 377 for (const auto test : tests) { |
| 378 opt_out_called_ = false; |
350 tester_.reset(new base::HistogramTester()); | 379 tester_.reset(new base::HistogramTester()); |
351 drp_test_context_->config()->ResetLoFiStatusForTest(); | 380 drp_test_context_->config()->ResetLoFiStatusForTest(); |
352 field_trial_list_.reset(); | 381 field_trial_list_.reset(); |
353 field_trial_list_.reset(new base::FieldTrialList(nullptr)); | 382 field_trial_list_.reset(new base::FieldTrialList(nullptr)); |
354 if (test.using_previews_blacklist) { | 383 if (test.using_previews_blacklist) { |
355 base::FieldTrialList::CreateFieldTrial( | 384 base::FieldTrialList::CreateFieldTrial( |
356 "DataReductionProxyPreviewsBlackListTransition", "Enabled_"); | 385 "DataReductionProxyPreviewsBlackListTransition", "Enabled_"); |
357 } | 386 } |
358 | 387 |
359 // Call Reload and CommitPendingNavigation to force DidFinishNavigation. | 388 // Call Reload and CommitPendingNavigation to force DidFinishNavigation. |
(...skipping 16 matching lines...) Expand all Loading... |
376 drp_test_context_->pref_service()->GetInteger( | 405 drp_test_context_->pref_service()->GetInteger( |
377 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); | 406 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); |
378 EXPECT_TRUE(user_opt_out_.value()); | 407 EXPECT_TRUE(user_opt_out_.value()); |
379 | 408 |
380 auto* data_reduction_proxy_settings = | 409 auto* data_reduction_proxy_settings = |
381 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | 410 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
382 web_contents()->GetBrowserContext()); | 411 web_contents()->GetBrowserContext()); |
383 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() | 412 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() |
384 ->pingback_client() | 413 ->pingback_client() |
385 ->OptOutsSizeForTesting()); | 414 ->OptOutsSizeForTesting()); |
| 415 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() |
| 416 ->pingback_client() |
| 417 ->OptOutsSizeForTesting()); |
| 418 |
| 419 EXPECT_TRUE(opt_out_called_); |
386 } | 420 } |
387 } | 421 } |
388 | 422 |
389 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLitePage) { | 423 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLitePage) { |
390 NavigateAndCommit(GURL(kTestUrl)); | 424 NavigateAndCommit(GURL(kTestUrl)); |
391 | |
392 ConfirmInfoBarDelegate* infobar = | 425 ConfirmInfoBarDelegate* infobar = |
393 CreateInfoBar(previews::PreviewsType::LITE_PAGE, base::Time(), | 426 CreateInfoBar(previews::PreviewsType::LITE_PAGE, base::Time(), |
394 true /* is_data_saver_user */, false /* is_reload */); | 427 true /* is_data_saver_user */, false /* is_reload */); |
395 | 428 |
396 // Simulate clicking the infobar link. | 429 // Simulate clicking the infobar link. |
397 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) | 430 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) |
398 infobar_service()->infobar_at(0)->RemoveSelf(); | 431 infobar_service()->infobar_at(0)->RemoveSelf(); |
399 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 432 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
400 | 433 |
401 tester_->ExpectBucketCount( | 434 tester_->ExpectBucketCount( |
402 kUMAPreviewsInfoBarActionLitePage, | 435 kUMAPreviewsInfoBarActionLitePage, |
403 PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1); | 436 PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1); |
404 | 437 |
405 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); | 438 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
406 | 439 |
407 EXPECT_EQ(content::ReloadType::DISABLE_LOFI_MODE, | 440 EXPECT_EQ(content::ReloadType::DISABLE_LOFI_MODE, |
408 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) | 441 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) |
409 ->last_navigation_reload_type()); | 442 ->last_navigation_reload_type()); |
410 | 443 |
411 auto* data_reduction_proxy_settings = | 444 auto* data_reduction_proxy_settings = |
412 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | 445 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
413 web_contents()->GetBrowserContext()); | 446 web_contents()->GetBrowserContext()); |
414 | 447 |
415 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() | 448 EXPECT_EQ(1u, data_reduction_proxy_settings->data_reduction_proxy_service() |
416 ->pingback_client() | 449 ->pingback_client() |
417 ->OptOutsSizeForTesting()); | 450 ->OptOutsSizeForTesting()); |
| 451 EXPECT_TRUE(opt_out_called_); |
418 } | 452 } |
419 | 453 |
420 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestShownOncePerNavigation) { | 454 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestShownOncePerNavigation) { |
421 ConfirmInfoBarDelegate* infobar = | 455 ConfirmInfoBarDelegate* infobar = |
422 CreateInfoBar(previews::PreviewsType::LOFI, base::Time(), | 456 CreateInfoBar(previews::PreviewsType::LOFI, base::Time(), |
423 true /* is_data_saver_user */, false /* is_reload */); | 457 true /* is_data_saver_user */, false /* is_reload */); |
424 | 458 |
425 // Simulate dismissing the infobar. | 459 // Simulate dismissing the infobar. |
426 infobar->InfoBarDismissed(); | 460 infobar->InfoBarDismissed(); |
427 infobar_service()->infobar_at(0)->RemoveSelf(); | 461 infobar_service()->infobar_at(0)->RemoveSelf(); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) | 594 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) |
561 ->last_navigation_reload_type()); | 595 ->last_navigation_reload_type()); |
562 | 596 |
563 auto* data_reduction_proxy_settings = | 597 auto* data_reduction_proxy_settings = |
564 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | 598 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
565 web_contents()->GetBrowserContext()); | 599 web_contents()->GetBrowserContext()); |
566 | 600 |
567 EXPECT_EQ(0u, data_reduction_proxy_settings->data_reduction_proxy_service() | 601 EXPECT_EQ(0u, data_reduction_proxy_settings->data_reduction_proxy_service() |
568 ->pingback_client() | 602 ->pingback_client() |
569 ->OptOutsSizeForTesting()); | 603 ->OptOutsSizeForTesting()); |
| 604 |
| 605 EXPECT_TRUE(opt_out_called_); |
570 } | 606 } |
571 | 607 |
572 TEST_F(PreviewsInfoBarDelegateUnitTest, PingbackClientClearedTabClosed) { | 608 TEST_F(PreviewsInfoBarDelegateUnitTest, PingbackClientClearedTabClosed) { |
573 NavigateAndCommit(GURL(kTestUrl)); | 609 NavigateAndCommit(GURL(kTestUrl)); |
574 | 610 |
575 ConfirmInfoBarDelegate* infobar = | 611 ConfirmInfoBarDelegate* infobar = |
576 CreateInfoBar(previews::PreviewsType::LITE_PAGE, base::Time(), | 612 CreateInfoBar(previews::PreviewsType::LITE_PAGE, base::Time(), |
577 true /* is_data_saver_user */, false /* is_reload */); | 613 true /* is_data_saver_user */, false /* is_reload */); |
578 | 614 |
579 // Simulate clicking the infobar link. | 615 // 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, | 736 l10n_util::GetStringFUTF16(IDS_PREVIEWS_INFOBAR_TIMESTAMP_MINUTES, |
701 base::IntToString16(staleness_in_minutes)), | 737 base::IntToString16(staleness_in_minutes)), |
702 PreviewsInfoBarDelegate::TIMESTAMP_SHOWN); | 738 PreviewsInfoBarDelegate::TIMESTAMP_SHOWN); |
703 | 739 |
704 staleness_in_minutes = 1; | 740 staleness_in_minutes = 1; |
705 TestStalePreviews( | 741 TestStalePreviews( |
706 staleness_in_minutes, true /* is_reload */, | 742 staleness_in_minutes, true /* is_reload */, |
707 l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_TIMESTAMP_UPDATED_NOW), | 743 l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_TIMESTAMP_UPDATED_NOW), |
708 PreviewsInfoBarDelegate::TIMESTAMP_UPDATED_NOW_SHOWN); | 744 PreviewsInfoBarDelegate::TIMESTAMP_UPDATED_NOW_SHOWN); |
709 } | 745 } |
OLD | NEW |