File: popup_blocked_message_delegate_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (173 lines) | stat: -rw-r--r-- 6,985 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/blocked_content/android/popup_blocked_message_delegate.h"

#include "base/android/jni_android.h"
#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "components/blocked_content/popup_blocker_tab_helper.h"
#include "components/blocked_content/safe_browsing_triggered_popup_blocker.h"
#include "components/content_settings/browser/page_specific_content_settings.h"
#include "components/content_settings/browser/test_page_specific_content_settings_delegate.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/messages/android/mock_message_dispatcher_bridge.h"
#include "components/strings/grit/components_strings.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/test_renderer_host.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"

namespace blocked_content {
namespace {
constexpr char kPageUrl[] = "http://example_page.test";
}  // namespace

class PopupBlockedMessageDelegateTest
    : public content::RenderViewHostTestHarness {
 public:
  PopupBlockedMessageDelegateTest() = default;
  ~PopupBlockedMessageDelegateTest() override;

  // content::RenderViewHostTestHarness:
  void SetUp() override;

  PopupBlockerTabHelper* helper() { return helper_; }

  HostContentSettingsMap* settings_map() { return settings_map_.get(); }

  bool EnqueueMessage(int num_pops,
                      base::OnceClosure on_accept_callback,
                      bool success);

  messages::MessageWrapper* GetMessageWrapper();
  void TriggerMessageDismissedCallback(messages::DismissReason dismiss_reason);
  void TriggerActionClick();

  PopupBlockedMessageDelegate* GetDelegate() {
    return popup_blocked_message_delegate_;
  }

 private:
  raw_ptr<PopupBlockerTabHelper> helper_ = nullptr;
  base::test::ScopedFeatureList feature_list_;
  sync_preferences::TestingPrefServiceSyncable pref_service_;
  scoped_refptr<HostContentSettingsMap> settings_map_;
  messages::MockMessageDispatcherBridge message_dispatcher_bridge_;
  raw_ptr<PopupBlockedMessageDelegate> popup_blocked_message_delegate_;
};

PopupBlockedMessageDelegateTest::~PopupBlockedMessageDelegateTest() {
  settings_map_->ShutdownOnUIThread();
}

void PopupBlockedMessageDelegateTest::SetUp() {
  content::RenderViewHostTestHarness::SetUp();

  // Make sure the SafeBrowsingTriggeredPopupBlocker is not created.
  feature_list_.InitAndDisableFeature(kAbusiveExperienceEnforce);

  HostContentSettingsMap::RegisterProfilePrefs(pref_service_.registry());
  settings_map_ = base::MakeRefCounted<HostContentSettingsMap>(
      &pref_service_, false /* is_off_the_record */,
      false /* store_last_modified */, false /* restore_session*/,
      false /* should_record_metrics */);
  content_settings::PageSpecificContentSettings::CreateForWebContents(
      web_contents(),
      std::make_unique<
          content_settings::TestPageSpecificContentSettingsDelegate>(
          /*prefs=*/nullptr, settings_map_.get()));

  PopupBlockerTabHelper::CreateForWebContents(web_contents());
  helper_ = PopupBlockerTabHelper::FromWebContents(web_contents());

  PopupBlockedMessageDelegate::CreateForWebContents(web_contents());
  popup_blocked_message_delegate_ =
      PopupBlockedMessageDelegate::FromWebContents(web_contents());
  NavigateAndCommit(GURL(kPageUrl));
  message_dispatcher_bridge_.SetMessagesEnabledForEmbedder(true);
  messages::MessageDispatcherBridge::SetInstanceForTesting(
      &message_dispatcher_bridge_);
}

messages::MessageWrapper* PopupBlockedMessageDelegateTest::GetMessageWrapper() {
  return popup_blocked_message_delegate_->message_for_testing();
}

bool PopupBlockedMessageDelegateTest::EnqueueMessage(
    int num_pops,
    base::OnceClosure on_accept_callback,
    bool success) {
  EXPECT_CALL(message_dispatcher_bridge_, EnqueueMessage)
      .WillOnce(testing::Return(success));
  return GetDelegate()->ShowMessage(num_pops, settings_map(),
                                    std::move(on_accept_callback));
}

void PopupBlockedMessageDelegateTest::TriggerActionClick() {
  GetMessageWrapper()->HandleActionClick(base::android::AttachCurrentThread());
}

void PopupBlockedMessageDelegateTest::TriggerMessageDismissedCallback(
    messages::DismissReason dismiss_reason) {
  GetMessageWrapper()->HandleDismissCallback(
      base::android::AttachCurrentThread(), static_cast<int>(dismiss_reason));
  EXPECT_EQ(nullptr, GetMessageWrapper());
}

// Tests that message properties (title, description, icon, button text) are
// set correctly.
TEST_F(PopupBlockedMessageDelegateTest, MessagePropertyValues) {
  int num_popups = 3;
  EnqueueMessage(num_popups, base::NullCallback(), true);
  EXPECT_EQ(l10n_util::GetPluralStringFUTF16(IDS_POPUPS_BLOCKED_INFOBAR_TEXT,
                                             num_popups),
            GetMessageWrapper()->GetTitle());
  EXPECT_EQ(l10n_util::GetStringUTF16(IDS_POPUPS_BLOCKED_INFOBAR_BUTTON_SHOW),
            GetMessageWrapper()->GetPrimaryButtonText());

  // Should update title; #EnqueueMessage ensure message is enqueued only once.
  GetDelegate()->ShowMessage(num_popups + 1, settings_map(),
                             base::NullCallback());
  EXPECT_EQ(l10n_util::GetPluralStringFUTF16(IDS_POPUPS_BLOCKED_INFOBAR_TEXT,
                                             num_popups + 1),
            GetMessageWrapper()->GetTitle());
  TriggerMessageDismissedCallback(messages::DismissReason::UNKNOWN);
}

// Tests that title updated when another popup is blocked and a message
// is already on the screen.
TEST_F(PopupBlockedMessageDelegateTest, ShowsBlockedPopups) {
  bool on_accept_called = false;
  bool result =
      EnqueueMessage(1, base::BindLambdaForTesting([&on_accept_called] {
                       on_accept_called = true;
                     }),
                     true);
  EXPECT_TRUE(result);
  TriggerActionClick();
  EXPECT_TRUE(on_accept_called);
  TriggerMessageDismissedCallback(messages::DismissReason::UNKNOWN);
  EXPECT_EQ(settings_map()->GetContentSetting(GURL(kPageUrl), GURL(kPageUrl),
                                              ContentSettingsType::POPUPS),
            CONTENT_SETTING_ALLOW);
}

// Tests that title updated when another popup is blocked and a message
// is already on the screen.
TEST_F(PopupBlockedMessageDelegateTest, FailToShowMessage) {
  bool on_accept_called = false;
  bool result =
      EnqueueMessage(1, base::BindLambdaForTesting([&on_accept_called] {
                       on_accept_called = true;
                     }),
                     false);
  EXPECT_FALSE(result);
  EXPECT_FALSE(on_accept_called);
}

}  // namespace blocked_content