popup_blocked_message_delegate_unittest.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2021 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_message_delegate.h"
  5. #include "base/android/jni_android.h"
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/test/bind.h"
  8. #include "base/test/scoped_feature_list.h"
  9. #include "components/blocked_content/popup_blocker_tab_helper.h"
  10. #include "components/blocked_content/safe_browsing_triggered_popup_blocker.h"
  11. #include "components/content_settings/browser/page_specific_content_settings.h"
  12. #include "components/content_settings/browser/test_page_specific_content_settings_delegate.h"
  13. #include "components/content_settings/core/browser/host_content_settings_map.h"
  14. #include "components/messages/android/mock_message_dispatcher_bridge.h"
  15. #include "components/strings/grit/components_strings.h"
  16. #include "components/sync_preferences/testing_pref_service_syncable.h"
  17. #include "content/public/test/test_renderer_host.h"
  18. #include "testing/gmock/include/gmock/gmock.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. #include "ui/base/l10n/l10n_util.h"
  21. #include "url/gurl.h"
  22. namespace blocked_content {
  23. namespace {
  24. constexpr char kPageUrl[] = "http://example_page.test";
  25. } // namespace
  26. class PopupBlockedMessageDelegateTest
  27. : public content::RenderViewHostTestHarness {
  28. public:
  29. PopupBlockedMessageDelegateTest() = default;
  30. ~PopupBlockedMessageDelegateTest() override;
  31. // content::RenderViewHostTestHarness:
  32. void SetUp() override;
  33. PopupBlockerTabHelper* helper() { return helper_; }
  34. HostContentSettingsMap* settings_map() { return settings_map_.get(); }
  35. bool EnqueueMessage(int num_pops,
  36. base::OnceClosure on_accept_callback,
  37. bool success);
  38. messages::MessageWrapper* GetMessageWrapper();
  39. void TriggerMessageDismissedCallback(messages::DismissReason dismiss_reason);
  40. void TriggerActionClick();
  41. PopupBlockedMessageDelegate* GetDelegate() {
  42. return popup_blocked_message_delegate_;
  43. }
  44. private:
  45. raw_ptr<PopupBlockerTabHelper> helper_ = nullptr;
  46. base::test::ScopedFeatureList feature_list_;
  47. sync_preferences::TestingPrefServiceSyncable pref_service_;
  48. scoped_refptr<HostContentSettingsMap> settings_map_;
  49. messages::MockMessageDispatcherBridge message_dispatcher_bridge_;
  50. raw_ptr<PopupBlockedMessageDelegate> popup_blocked_message_delegate_;
  51. };
  52. PopupBlockedMessageDelegateTest::~PopupBlockedMessageDelegateTest() {
  53. settings_map_->ShutdownOnUIThread();
  54. }
  55. void PopupBlockedMessageDelegateTest::SetUp() {
  56. content::RenderViewHostTestHarness::SetUp();
  57. // Make sure the SafeBrowsingTriggeredPopupBlocker is not created.
  58. feature_list_.InitAndDisableFeature(kAbusiveExperienceEnforce);
  59. HostContentSettingsMap::RegisterProfilePrefs(pref_service_.registry());
  60. settings_map_ = base::MakeRefCounted<HostContentSettingsMap>(
  61. &pref_service_, false /* is_off_the_record */,
  62. false /* store_last_modified */, false /* restore_session*/);
  63. content_settings::PageSpecificContentSettings::CreateForWebContents(
  64. web_contents(),
  65. std::make_unique<
  66. content_settings::TestPageSpecificContentSettingsDelegate>(
  67. /*prefs=*/nullptr, settings_map_.get()));
  68. PopupBlockerTabHelper::CreateForWebContents(web_contents());
  69. helper_ = PopupBlockerTabHelper::FromWebContents(web_contents());
  70. PopupBlockedMessageDelegate::CreateForWebContents(web_contents());
  71. popup_blocked_message_delegate_ =
  72. PopupBlockedMessageDelegate::FromWebContents(web_contents());
  73. NavigateAndCommit(GURL(kPageUrl));
  74. message_dispatcher_bridge_.SetMessagesEnabledForEmbedder(true);
  75. messages::MessageDispatcherBridge::SetInstanceForTesting(
  76. &message_dispatcher_bridge_);
  77. }
  78. messages::MessageWrapper* PopupBlockedMessageDelegateTest::GetMessageWrapper() {
  79. return popup_blocked_message_delegate_->message_for_testing();
  80. }
  81. bool PopupBlockedMessageDelegateTest::EnqueueMessage(
  82. int num_pops,
  83. base::OnceClosure on_accept_callback,
  84. bool success) {
  85. EXPECT_CALL(message_dispatcher_bridge_, EnqueueMessage)
  86. .WillOnce(testing::Return(success));
  87. return GetDelegate()->ShowMessage(num_pops, settings_map(),
  88. std::move(on_accept_callback));
  89. }
  90. void PopupBlockedMessageDelegateTest::TriggerActionClick() {
  91. GetMessageWrapper()->HandleActionClick(base::android::AttachCurrentThread());
  92. }
  93. void PopupBlockedMessageDelegateTest::TriggerMessageDismissedCallback(
  94. messages::DismissReason dismiss_reason) {
  95. GetMessageWrapper()->HandleDismissCallback(
  96. base::android::AttachCurrentThread(), static_cast<int>(dismiss_reason));
  97. EXPECT_EQ(nullptr, GetMessageWrapper());
  98. }
  99. // Tests that message properties (title, description, icon, button text) are
  100. // set correctly.
  101. TEST_F(PopupBlockedMessageDelegateTest, MessagePropertyValues) {
  102. int num_popups = 3;
  103. EnqueueMessage(num_popups, base::NullCallback(), true);
  104. EXPECT_EQ(l10n_util::GetPluralStringFUTF16(IDS_POPUPS_BLOCKED_INFOBAR_TEXT,
  105. num_popups),
  106. GetMessageWrapper()->GetTitle());
  107. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_POPUPS_BLOCKED_INFOBAR_BUTTON_SHOW),
  108. GetMessageWrapper()->GetPrimaryButtonText());
  109. // Should update title; #EnqueueMessage ensure message is enqueued only once.
  110. GetDelegate()->ShowMessage(num_popups + 1, settings_map(),
  111. base::NullCallback());
  112. EXPECT_EQ(l10n_util::GetPluralStringFUTF16(IDS_POPUPS_BLOCKED_INFOBAR_TEXT,
  113. num_popups + 1),
  114. GetMessageWrapper()->GetTitle());
  115. TriggerMessageDismissedCallback(messages::DismissReason::UNKNOWN);
  116. }
  117. // Tests that title updated when another popup is blocked and a message
  118. // is already on the screen.
  119. TEST_F(PopupBlockedMessageDelegateTest, ShowsBlockedPopups) {
  120. bool on_accept_called = false;
  121. bool result =
  122. EnqueueMessage(1, base::BindLambdaForTesting([&on_accept_called] {
  123. on_accept_called = true;
  124. }),
  125. true);
  126. EXPECT_TRUE(result);
  127. TriggerActionClick();
  128. EXPECT_TRUE(on_accept_called);
  129. TriggerMessageDismissedCallback(messages::DismissReason::UNKNOWN);
  130. EXPECT_EQ(settings_map()->GetContentSetting(GURL(kPageUrl), GURL(kPageUrl),
  131. ContentSettingsType::POPUPS),
  132. CONTENT_SETTING_ALLOW);
  133. }
  134. // Tests that title updated when another popup is blocked and a message
  135. // is already on the screen.
  136. TEST_F(PopupBlockedMessageDelegateTest, FailToShowMessage) {
  137. bool on_accept_called = false;
  138. bool result =
  139. EnqueueMessage(1, base::BindLambdaForTesting([&on_accept_called] {
  140. on_accept_called = true;
  141. }),
  142. false);
  143. EXPECT_FALSE(result);
  144. EXPECT_FALSE(on_accept_called);
  145. }
  146. } // namespace blocked_content