popup_blocked_infobar_delegate_unittest.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/blocked_content/android/popup_blocked_infobar_delegate.h"
  5. #include "base/callback_helpers.h"
  6. #include "base/containers/contains.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "base/test/bind.h"
  10. #include "base/test/scoped_feature_list.h"
  11. #include "components/blocked_content/popup_blocker_tab_helper.h"
  12. #include "components/blocked_content/safe_browsing_triggered_popup_blocker.h"
  13. #include "components/blocked_content/test/test_popup_navigation_delegate.h"
  14. #include "components/content_settings/browser/page_specific_content_settings.h"
  15. #include "components/content_settings/browser/test_page_specific_content_settings_delegate.h"
  16. #include "components/content_settings/core/browser/host_content_settings_map.h"
  17. #include "components/infobars/content/content_infobar_manager.h"
  18. #include "components/infobars/core/infobar.h"
  19. #include "components/sync_preferences/testing_pref_service_syncable.h"
  20. #include "content/public/test/test_renderer_host.h"
  21. #include "testing/gmock/include/gmock/gmock.h"
  22. namespace blocked_content {
  23. namespace {
  24. constexpr char kPageUrl[] = "http://example_page.test";
  25. constexpr char kPopupUrl[] = "http://example_popup.test";
  26. } // namespace
  27. class PopupBlockedInfoBarDelegateTest
  28. : public content::RenderViewHostTestHarness {
  29. public:
  30. PopupBlockedInfoBarDelegateTest() : content::RenderViewHostTestHarness() {
  31. // Make sure the SafeBrowsingTriggeredPopupBlocker is not created.
  32. feature_list_.InitAndDisableFeature(kAbusiveExperienceEnforce);
  33. }
  34. ~PopupBlockedInfoBarDelegateTest() override {
  35. settings_map_->ShutdownOnUIThread();
  36. }
  37. // content::RenderViewHostTestHarness:
  38. void SetUp() override {
  39. content::RenderViewHostTestHarness::SetUp();
  40. HostContentSettingsMap::RegisterProfilePrefs(pref_service_.registry());
  41. settings_map_ = base::MakeRefCounted<HostContentSettingsMap>(
  42. &pref_service_, false /* is_off_the_record */,
  43. false /* store_last_modified */, false /* restore_session*/);
  44. content_settings::PageSpecificContentSettings::CreateForWebContents(
  45. web_contents(),
  46. std::make_unique<
  47. content_settings::TestPageSpecificContentSettingsDelegate>(
  48. /*prefs=*/nullptr, settings_map_.get()));
  49. PopupBlockerTabHelper::CreateForWebContents(web_contents());
  50. helper_ = PopupBlockerTabHelper::FromWebContents(web_contents());
  51. infobar_manager_ =
  52. std::make_unique<infobars::ContentInfoBarManager>(web_contents());
  53. NavigateAndCommit(GURL(kPageUrl));
  54. }
  55. PopupBlockerTabHelper* helper() { return helper_; }
  56. infobars::ContentInfoBarManager* infobar_manager() {
  57. return infobar_manager_.get();
  58. }
  59. HostContentSettingsMap* settings_map() { return settings_map_.get(); }
  60. private:
  61. base::test::ScopedFeatureList feature_list_;
  62. raw_ptr<PopupBlockerTabHelper> helper_ = nullptr;
  63. sync_preferences::TestingPrefServiceSyncable pref_service_;
  64. scoped_refptr<HostContentSettingsMap> settings_map_;
  65. std::unique_ptr<infobars::ContentInfoBarManager> infobar_manager_;
  66. };
  67. TEST_F(PopupBlockedInfoBarDelegateTest, ReplacesInfobarOnSecondPopup) {
  68. EXPECT_TRUE(PopupBlockedInfoBarDelegate::Create(
  69. infobar_manager(), 1, settings_map(), base::NullCallback()));
  70. EXPECT_EQ(infobar_manager()->infobar_count(), 1u);
  71. // First message should not contain "2";
  72. EXPECT_FALSE(base::Contains(infobar_manager()
  73. ->infobar_at(0)
  74. ->delegate()
  75. ->AsConfirmInfoBarDelegate()
  76. ->GetMessageText(),
  77. u"2"));
  78. EXPECT_FALSE(PopupBlockedInfoBarDelegate::Create(
  79. infobar_manager(), 2, settings_map(), base::NullCallback()));
  80. EXPECT_EQ(infobar_manager()->infobar_count(), 1u);
  81. // Second message blocks 2 popups, so should contain "2";
  82. EXPECT_TRUE(base::Contains(infobar_manager()
  83. ->infobar_at(0)
  84. ->delegate()
  85. ->AsConfirmInfoBarDelegate()
  86. ->GetMessageText(),
  87. u"2"));
  88. }
  89. TEST_F(PopupBlockedInfoBarDelegateTest, ShowsBlockedPopups) {
  90. TestPopupNavigationDelegate::ResultHolder result;
  91. helper()->AddBlockedPopup(
  92. std::make_unique<TestPopupNavigationDelegate>(GURL(kPopupUrl), &result),
  93. blink::mojom::WindowFeatures(), PopupBlockType::kNoGesture);
  94. bool on_accept_called = false;
  95. EXPECT_TRUE(PopupBlockedInfoBarDelegate::Create(
  96. infobar_manager(), 1, settings_map(),
  97. base::BindLambdaForTesting(
  98. [&on_accept_called] { on_accept_called = true; })));
  99. EXPECT_FALSE(on_accept_called);
  100. EXPECT_TRUE(infobar_manager()
  101. ->infobar_at(0)
  102. ->delegate()
  103. ->AsConfirmInfoBarDelegate()
  104. ->Accept());
  105. EXPECT_TRUE(result.did_navigate);
  106. EXPECT_TRUE(on_accept_called);
  107. EXPECT_EQ(settings_map()->GetContentSetting(GURL(kPageUrl), GURL(kPageUrl),
  108. ContentSettingsType::POPUPS),
  109. CONTENT_SETTING_ALLOW);
  110. }
  111. } // namespace blocked_content