File: notification_display_service_impl_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (383 lines) | stat: -rw-r--r-- 14,286 bytes parent folder | download | duplicates (3)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/notifications/notification_display_service_impl.h"

#include <memory>
#include <optional>
#include <set>
#include <string>
#include <vector>

#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "build/build_config.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/notifications/notification_blocker.h"
#include "chrome/browser/notifications/notification_display_queue.h"
#include "chrome/browser/notifications/notification_platform_bridge_delegator.h"
#include "chrome/common/notifications/notification_operation.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_web_contents_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_delegate.h"
#include "ui/message_center/public/cpp/notification_types.h"
#include "url/gurl.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/nearby_sharing/nearby_sharing_service_factory.h"
#endif

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/notifications/muted_notification_handler.h"
#include "chrome/browser/notifications/screen_capture_notification_blocker.h"
#endif

namespace {

class FakeNotificationBlocker : public NotificationBlocker {
 public:
  FakeNotificationBlocker() = default;
  ~FakeNotificationBlocker() override = default;

  // NotificationDisplayQueue::NotificationBlocker:
  bool ShouldBlockNotification(
      const message_center::Notification& notification) override {
    return should_block_;
  }

  void SetShouldBlockNotifications(bool should_block) {
    should_block_ = should_block;
    NotifyBlockingStateChanged();
  }

 private:
  bool should_block_ = false;
};

class TestNotificationPlatformBridgeDelegator
    : public NotificationPlatformBridgeDelegator {
 public:
  explicit TestNotificationPlatformBridgeDelegator(Profile* profile)
      : NotificationPlatformBridgeDelegator(profile, base::DoNothing()) {}
  ~TestNotificationPlatformBridgeDelegator() override = default;

  // NotificationPlatformBridgeDelegator:
  void Display(
      NotificationHandler::Type notification_type,
      const message_center::Notification& notification,
      std::unique_ptr<NotificationCommon::Metadata> metadata) override {
    notification_ids_.insert(notification.id());
    notification_origins_[notification.id()] = notification.origin_url();
  }

  void Close(NotificationHandler::Type notification_type,
             const std::string& notification_id) override {
    notification_ids_.erase(notification_id);
    notification_origins_.erase(notification_id);
  }

  void GetDisplayed(GetDisplayedNotificationsCallback callback) const override {
    std::move(callback).Run(notification_ids_, /*supports_sync=*/true);
  }

  void GetDisplayedForOrigin(
      const GURL& origin,
      GetDisplayedNotificationsCallback callback) const override {
    std::set<std::string> result;
    for (const auto& id : notification_ids_) {
      auto origin_it = notification_origins_.find(id);
      if (url::IsSameOriginWith(origin_it->second, origin)) {
        result.insert(id);
      }
    }
    std::move(callback).Run(result, /*supports_sync=*/true);
  }

 private:
  std::set<std::string> notification_ids_;
  std::map<std::string, GURL> notification_origins_;
};

message_center::Notification CreateNotification(const std::string& id,
                                                const GURL& origin = {}) {
  return message_center::Notification(
      message_center::NOTIFICATION_TYPE_SIMPLE, id, /*title=*/std::u16string(),
      /*message=*/std::u16string(), /*icon=*/ui::ImageModel(),
      /*display_source=*/std::u16string(), origin, message_center::NotifierId(),
      message_center::RichNotificationData(), /*delegate=*/nullptr);
}

}  // namespace

class BaseNotificationDisplayServiceImplTest : public testing::Test {
 protected:
  BaseNotificationDisplayServiceImplTest() = default;
  ~BaseNotificationDisplayServiceImplTest() override = default;

  // testing::Test:
  void SetUp() override {
    service_ = std::make_unique<NotificationDisplayServiceImpl>(&profile_);

    auto notification_delegator =
        std::make_unique<TestNotificationPlatformBridgeDelegator>(&profile_);
    notification_delegator_ = notification_delegator.get();

    service_->SetNotificationPlatformBridgeDelegatorForTesting(
        std::move(notification_delegator));
  }

  Profile* profile() { return &profile_; }

  NotificationDisplayServiceImpl& service() { return *service_; }

 protected:
  std::set<std::string> GetDisplayedServiceSync() {
    base::test::TestFuture<std::set<std::string>, bool> displayed;
    service_->GetDisplayed(displayed.GetCallback());
    return displayed.Get<0>();
  }

  std::set<std::string> GetDisplayedForOriginServiceSync(const GURL& origin) {
    base::test::TestFuture<std::set<std::string>, bool> displayed;
    service_->GetDisplayedForOrigin(origin, displayed.GetCallback());
    return displayed.Get<0>();
  }

  std::set<std::string> GetDisplayedPlatformSync() {
    base::test::TestFuture<std::set<std::string>, bool> displayed;
    notification_delegator_->GetDisplayed(displayed.GetCallback());
    return displayed.Get<0>();
  }

  std::set<std::string> GetDisplayedForOriginPlatformSync(const GURL& origin) {
    base::test::TestFuture<std::set<std::string>, bool> displayed;
    notification_delegator_->GetDisplayedForOrigin(origin,
                                                   displayed.GetCallback());
    return displayed.Get<0>();
  }

  void DisplayNotification(const std::string id, const GURL& origin = {}) {
    service_->Display(NotificationHandler::Type::WEB_PERSISTENT,
                      CreateNotification(id, origin), /*metadata=*/nullptr);
  }

  void CloseNotification(const std::string id) {
    service_->Close(NotificationHandler::Type::WEB_PERSISTENT, id);
  }

 private:
  content::BrowserTaskEnvironment task_environment_;
  TestingProfile profile_;
  std::unique_ptr<NotificationDisplayServiceImpl> service_;
  raw_ptr<TestNotificationPlatformBridgeDelegator> notification_delegator_ =
      nullptr;
};

// Test class that uses a FakeNotificationBlocker instead of the real ones.
class NotificationDisplayServiceImplTest
    : public BaseNotificationDisplayServiceImplTest {
 protected:
  NotificationDisplayServiceImplTest() = default;
  ~NotificationDisplayServiceImplTest() override = default;

  // BaseNotificationDisplayServiceImplTest:
  void SetUp() override {
    BaseNotificationDisplayServiceImplTest::SetUp();

    auto blocker = std::make_unique<FakeNotificationBlocker>();
    notification_blocker_ = blocker.get();

    NotificationDisplayQueue::NotificationBlockers blockers;
    blockers.push_back(std::move(blocker));
    service().SetBlockersForTesting(std::move(blockers));
  }

  FakeNotificationBlocker& notification_blocker() {
    return *notification_blocker_;
  }

 private:
  raw_ptr<FakeNotificationBlocker, DanglingUntriaged> notification_blocker_ =
      nullptr;
};

TEST_F(NotificationDisplayServiceImplTest, DisplayWithoutBlockers) {
  service().SetBlockersForTesting({});
  std::string notification_id = "id";
  GURL origin("https://example.com/");
  DisplayNotification(notification_id, origin);

  std::set<std::string> displayed = GetDisplayedServiceSync();
  EXPECT_EQ(1u, displayed.size());
  EXPECT_EQ(1u, displayed.count(notification_id));
  EXPECT_EQ(displayed, GetDisplayedPlatformSync());
  EXPECT_EQ(displayed, GetDisplayedForOriginServiceSync(origin));
  EXPECT_EQ(displayed, GetDisplayedForOriginPlatformSync(origin));
  EXPECT_TRUE(
      GetDisplayedForOriginServiceSync(GURL("https://foo.bar")).empty());
  EXPECT_TRUE(
      GetDisplayedForOriginPlatformSync(GURL("https://foo.bar")).empty());
}

TEST_F(NotificationDisplayServiceImplTest, DisplayWithAllowingBlocker) {
  std::string notification_id = "id";
  DisplayNotification(notification_id);

  std::set<std::string> displayed = GetDisplayedServiceSync();
  EXPECT_EQ(1u, displayed.size());
  EXPECT_EQ(1u, displayed.count(notification_id));
  EXPECT_EQ(displayed, GetDisplayedPlatformSync());
}

TEST_F(NotificationDisplayServiceImplTest, DisplayWithBlockingBlocker) {
  notification_blocker().SetShouldBlockNotifications(true);
  std::string notification_id = "id";
  GURL origin("https://example.com/");
  DisplayNotification(notification_id, origin);

  std::set<std::string> displayed = GetDisplayedServiceSync();
  EXPECT_EQ(1u, displayed.size());
  EXPECT_EQ(1u, displayed.count(notification_id));
  EXPECT_TRUE(GetDisplayedPlatformSync().empty());
  EXPECT_EQ(displayed, GetDisplayedForOriginServiceSync(origin));
  EXPECT_TRUE(GetDisplayedForOriginPlatformSync(origin).empty());
}

TEST_F(NotificationDisplayServiceImplTest, UnblockQueuedNotification) {
  notification_blocker().SetShouldBlockNotifications(true);
  std::string notification_id = "id";
  DisplayNotification(notification_id);
  EXPECT_TRUE(GetDisplayedPlatformSync().empty());

  notification_blocker().SetShouldBlockNotifications(false);
  std::set<std::string> displayed = GetDisplayedServiceSync();
  EXPECT_EQ(1u, displayed.size());
  EXPECT_EQ(1u, displayed.count(notification_id));
  EXPECT_EQ(displayed, GetDisplayedPlatformSync());
}

TEST_F(NotificationDisplayServiceImplTest, CloseQueuedNotification) {
  notification_blocker().SetShouldBlockNotifications(true);
  std::string notification_id = "id";
  DisplayNotification(notification_id);
  EXPECT_EQ(1u, GetDisplayedServiceSync().size());
  EXPECT_TRUE(GetDisplayedPlatformSync().empty());

  CloseNotification(notification_id);
  EXPECT_TRUE(GetDisplayedServiceSync().empty());
  EXPECT_TRUE(GetDisplayedPlatformSync().empty());

  notification_blocker().SetShouldBlockNotifications(false);
  EXPECT_TRUE(GetDisplayedServiceSync().empty());
  EXPECT_TRUE(GetDisplayedPlatformSync().empty());
}

#if BUILDFLAG(IS_CHROMEOS)
TEST_F(NotificationDisplayServiceImplTest, NearbyNotificationHandler) {
  // Add the Nearby Share handler if and only if Nearby Share is supported.
  {
    NearbySharingServiceFactory::
        SetIsNearbyShareSupportedForBrowserContextForTesting(false);
    NotificationDisplayServiceImpl service(profile());
    EXPECT_FALSE(service.GetNotificationHandler(
        NotificationHandler::Type::NEARBY_SHARE));
  }
  {
    NearbySharingServiceFactory::
        SetIsNearbyShareSupportedForBrowserContextForTesting(true);
    NotificationDisplayServiceImpl service(profile());
    EXPECT_TRUE(service.GetNotificationHandler(
        NotificationHandler::Type::NEARBY_SHARE));
  }
}
#endif

#if !BUILDFLAG(IS_ANDROID)

// Desktop specific test class that uses the default NotificationBlockers.
class DesktopNotificationDisplayServiceImplTest
    : public BaseNotificationDisplayServiceImplTest {
 protected:
  DesktopNotificationDisplayServiceImplTest() = default;
  ~DesktopNotificationDisplayServiceImplTest() override = default;

  // BaseNotificationDisplayServiceImplTest:
  void SetUp() override {
    BaseNotificationDisplayServiceImplTest::SetUp();

    auto* muted_handler =
        static_cast<MutedNotificationHandler*>(service().GetNotificationHandler(
            NotificationHandler::Type::NOTIFICATIONS_MUTED));
    ASSERT_TRUE(muted_handler);
    screen_capture_blocker_ = static_cast<ScreenCaptureNotificationBlocker*>(
        muted_handler->get_delegate_for_testing());
    ASSERT_TRUE(screen_capture_blocker_);
  }

  ScreenCaptureNotificationBlocker* screen_capture_notification_blocker() {
    return screen_capture_blocker_;
  }

 private:
  raw_ptr<ScreenCaptureNotificationBlocker> screen_capture_blocker_ = nullptr;
};

TEST_F(DesktopNotificationDisplayServiceImplTest, SnoozeDuringScreenCapture) {
  base::test::ScopedFeatureList list;
  list.InitAndEnableFeature(features::kMuteNotificationSnoozeAction);

  content::TestWebContentsFactory web_contents_factory;
  content::WebContents* contents =
      web_contents_factory.CreateWebContents(profile());
  EXPECT_TRUE(GetDisplayedPlatformSync().empty());

  // Start a screen capture session.
  screen_capture_notification_blocker()->OnIsCapturingDisplayChanged(
      contents, /*is_capturing_display=*/true);

  // Displaying a notification should show the "Notifications Muted" generic
  // notification instead of the real notification content.
  std::string notification_id_1 = "id1";
  DisplayNotification(notification_id_1);
  EXPECT_EQ(1u, GetDisplayedPlatformSync().size());
  EXPECT_EQ(1u, GetDisplayedPlatformSync().count(kMuteNotificationId));

  // Emulate the user clicking on the "Snooze" action button.
  base::RunLoop run_loop;
  service().ProcessNotificationOperation(
      NotificationOperation::kClick,
      NotificationHandler::Type::NOTIFICATIONS_MUTED, /*origin=*/GURL(),
      kMuteNotificationId, /*action_index=*/0, /*reply=*/std::nullopt,
      /*by_user=*/true,
      base::BindOnce([](base::RunLoop* looper) { looper->Quit(); }, &run_loop));
  run_loop.Run();

  // Clicking "Snooze" should remove the "Notifications Muted" notification.
  EXPECT_TRUE(GetDisplayedPlatformSync().empty());

  // Showing another notification should not trigger any visible notification.
  std::string notification_id_2 = "id2";
  DisplayNotification(notification_id_2);
  EXPECT_TRUE(GetDisplayedPlatformSync().empty());

  // Stopping the screen sharing session should re-display all previously muted
  // notifications.
  screen_capture_notification_blocker()->OnIsCapturingDisplayChanged(
      contents, /*is_capturing_display=*/false);
  EXPECT_EQ(2u, GetDisplayedPlatformSync().size());
  EXPECT_EQ(1u, GetDisplayedPlatformSync().count(notification_id_1));
  EXPECT_EQ(1u, GetDisplayedPlatformSync().count(notification_id_2));
}

#endif  // !BUILDFLAG(IS_ANDROID)