File: platform_notification_service_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 (923 lines) | stat: -rw-r--r-- 39,569 bytes parent folder | download | duplicates (2)
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>

#include <memory>
#include <optional>
#include <utility>

#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/json/values_util.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/notifications/metrics/mock_notification_metrics_logger.h"
#include "chrome/browser/notifications/metrics/notification_metrics_logger_factory.h"
#include "chrome/browser/notifications/notification_display_service_impl.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/notifications/platform_notification_service_factory.h"
#include "chrome/browser/notifications/platform_notification_service_impl.h"
#include "chrome/browser/safe_browsing/notification_content_detection/mock_notification_content_detection_service.h"
#include "chrome/browser/safe_browsing/notification_content_detection/notification_content_detection_service_factory.h"
#include "chrome/browser/ui/safety_hub/disruptive_notification_permissions_manager.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/test/test_history_database.h"
#include "components/safe_browsing/content/browser/notification_content_detection/notification_content_detection_constants.h"
#include "components/safe_browsing/content/browser/notification_content_detection/test_model_observer_tracker.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/site_engagement/content/site_engagement_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/platform_notification_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/storage_partition_config.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/buildflags/buildflags.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/notifications/notification_resources.h"
#include "third_party/blink/public/common/notifications/platform_notification_data.h"
#include "third_party/blink/public/mojom/notifications/notification.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "url/gurl.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "base/values.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/test/fake_web_app_provider.h"
#include "chrome/browser/web_applications/test/os_integration_test_override_impl.h"
#include "chrome/browser/web_applications/test/web_app_icon_test_utils.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/test/web_app_test_utils.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_icon_generator.h"
#include "chrome/browser/web_applications/web_app_icon_manager.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_params.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "components/webapps/browser/install_result_code.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#endif

using blink::NotificationResources;
using blink::PlatformNotificationData;
using content::NotificationDatabaseData;
using message_center::Notification;
using ::testing::_;

namespace {

const int kNotificationVibrationPattern[] = { 100, 200, 300 };
const char kNotificationId[] = "my-notification-id";
const char kClosedReason[] = "ClosedReason";
const char kDidReplaceAnotherNotification[] = "DidReplaceAnotherNotification";
const char kHasBadge[] = "HasBadge";
const char kHasImage[] = "HasImage";
const char kHasIcon[] = "HasIcon";
const char kHasRenotify[] = "HasRenotify";
const char kHasTag[] = "HasTag";
const char kIsSilent[] = "IsSilent";
const char kNumActions[] = "NumActions";
const char kNumClicks[] = "NumClicks";
const char kNumActionButtonClicks[] = "NumActionButtonClicks";
const char kRequireInteraction[] = "RequireInteraction";
const char kTimeUntilCloseMillis[] = "TimeUntilClose";
const char kTimeUntilFirstClickMillis[] = "TimeUntilFirstClick";
const char kTimeUntilLastClickMillis[] = "TimeUntilLastClick";

}  // namespace

class PlatformNotificationServiceTest : public testing::Test {
 public:
  void SetUp() override {
    scoped_feature_list_.InitWithFeatures(
        {}, {safe_browsing::kOnDeviceNotificationContentDetectionModel,
             safe_browsing::kReportNotificationContentDetectionData});
    TestingProfile::Builder profile_builder;
    profile_builder.AddTestingFactory(
        HistoryServiceFactory::GetInstance(),
        HistoryServiceFactory::GetDefaultFactory());
    profile_ = profile_builder.Build();

    display_service_tester_ =
        std::make_unique<NotificationDisplayServiceTester>(profile_.get());

    mock_logger_ = static_cast<MockNotificationMetricsLogger*>(
        NotificationMetricsLoggerFactory::GetInstance()
            ->SetTestingFactoryAndUse(
                profile_.get(),
                base::BindRepeating(
                    &MockNotificationMetricsLogger::FactoryForTests)));

    recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
  }

  void TearDown() override {
    display_service_tester_.reset();
    mock_logger_ = nullptr;
    profile_.reset();
  }

  content::PlatformNotificationContext* GetPlatformNotificationContext(
      GURL origin) {
    return profile_->GetStoragePartitionForUrl(origin)
        ->GetPlatformNotificationContext();
  }

  NotificationDatabaseData ReadNotificationDataAndRecordInteractionSynchronous(
      content::PlatformNotificationContext* context,
      const std::string& notification_id,
      const GURL& origin) {
    base::RunLoop run_loop;
    context->ReadNotificationDataAndRecordInteraction(
        "p#" + origin.spec() + "#0" + notification_id, origin,
        content::PlatformNotificationContext::Interaction::NONE,
        base::BindOnce(
            &PlatformNotificationServiceTest::
                DidReadNotificationDataAndRecordInteractionSynchronous,
            base::Unretained(this), run_loop.QuitClosure()));
    run_loop.Run();
    return notification_database_data_;
  }

  void DidReadNotificationDataAndRecordInteractionSynchronous(
      base::OnceClosure quit_closure,
      bool success,
      const NotificationDatabaseData& data) {
    ASSERT_TRUE(success);
    notification_database_data_ = data;
    std::move(quit_closure).Run();
  }

 protected:
  // Returns the Platform Notification Service these unit tests are for.
  PlatformNotificationServiceImpl* service() {
    return PlatformNotificationServiceFactory::GetForProfile(profile_.get());
  }

  size_t GetNotificationCountForType(NotificationHandler::Type type) {
    return display_service_tester_->GetDisplayedNotificationsForType(type)
        .size();
  }

  Notification GetDisplayedNotificationForType(NotificationHandler::Type type) {
    std::vector<Notification> notifications =
        display_service_tester_->GetDisplayedNotificationsForType(type);
    DCHECK_EQ(1u, notifications.size());

    return notifications[0];
  }

 protected:
  // This needs to be declared before `task_environment_`, so that it is
  // destroyed after `task_environment_` - this ensures that tasks running on
  // other threads won't access `scoped_feature_list_` while it is being
  // destroyed.
  base::test::ScopedFeatureList scoped_feature_list_;
  content::BrowserTaskEnvironment task_environment_;
  std::unique_ptr<TestingProfile> profile_;

  std::unique_ptr<NotificationDisplayServiceTester> display_service_tester_;

  // Owned by the |profile_| as a keyed service.
  raw_ptr<MockNotificationMetricsLogger> mock_logger_;

  std::unique_ptr<ukm::TestAutoSetUkmRecorder> recorder_;

 private:
  NotificationDatabaseData notification_database_data_;
};

TEST_F(PlatformNotificationServiceTest, DisplayNonPersistentThenClose) {
  PlatformNotificationData data;
  data.title = u"My Notification";
  data.body = u"Hello, world!";

  service()->DisplayNotification(kNotificationId, GURL("https://chrome.com/"),
                                 /*document_url=*/GURL(), data,
                                 NotificationResources());

  EXPECT_EQ(1u, GetNotificationCountForType(
                    NotificationHandler::Type::WEB_NON_PERSISTENT));

  service()->CloseNotification(kNotificationId);

  EXPECT_EQ(0u, GetNotificationCountForType(
                    NotificationHandler::Type::WEB_NON_PERSISTENT));
}

TEST_F(PlatformNotificationServiceTest, DisplayPersistentThenClose) {
  PlatformNotificationData data;
  data.title = u"My notification's title";
  data.body = u"Hello, world!";

  EXPECT_CALL(*mock_logger_, LogPersistentNotificationShown());
  service()->DisplayPersistentNotification(
      kNotificationId, GURL() /* service_worker_scope */,
      GURL("https://chrome.com/"), data, NotificationResources());

  ASSERT_EQ(1u, GetNotificationCountForType(
                    NotificationHandler::Type::WEB_PERSISTENT));

  Notification notification = GetDisplayedNotificationForType(
      NotificationHandler::Type::WEB_PERSISTENT);
  EXPECT_EQ("https://chrome.com/", notification.origin_url().spec());
  EXPECT_EQ("My notification's title",
      base::UTF16ToUTF8(notification.title()));
  EXPECT_EQ("Hello, world!",
      base::UTF16ToUTF8(notification.message()));

  service()->ClosePersistentNotification(kNotificationId);
  EXPECT_EQ(0u, GetNotificationCountForType(
                    NotificationHandler::Type::WEB_PERSISTENT));
}

TEST_F(PlatformNotificationServiceTest, DisplayNonPersistentPropertiesMatch) {
  std::vector<int> vibration_pattern(std::begin(kNotificationVibrationPattern),
                                     std::end(kNotificationVibrationPattern));

  PlatformNotificationData data;
  data.title = u"My notification's title";
  data.body = u"Hello, world!";
  data.vibration_pattern = vibration_pattern;
  data.silent = true;

  service()->DisplayNotification(kNotificationId, GURL("https://chrome.com/"),
                                 /*document_url=*/GURL(), data,
                                 NotificationResources());

  ASSERT_EQ(1u, GetNotificationCountForType(
                    NotificationHandler::Type::WEB_NON_PERSISTENT));

  Notification notification = GetDisplayedNotificationForType(
      NotificationHandler::Type::WEB_NON_PERSISTENT);
  EXPECT_EQ("https://chrome.com/", notification.origin_url().spec());
  EXPECT_EQ("My notification's title",
      base::UTF16ToUTF8(notification.title()));
  EXPECT_EQ("Hello, world!",
      base::UTF16ToUTF8(notification.message()));

  EXPECT_THAT(notification.vibration_pattern(),
      testing::ElementsAreArray(kNotificationVibrationPattern));

  EXPECT_TRUE(notification.silent());
}

TEST_F(PlatformNotificationServiceTest, DisplayPersistentPropertiesMatch) {
  std::vector<int> vibration_pattern(std::begin(kNotificationVibrationPattern),
                                     std::end(kNotificationVibrationPattern));
  PlatformNotificationData data;
  data.title = u"My notification's title";
  data.body = u"Hello, world!";
  data.vibration_pattern = vibration_pattern;
  data.silent = true;
  data.actions.resize(2);
  data.actions[0] = blink::mojom::NotificationAction::New();
  data.actions[0]->type = blink::mojom::NotificationActionType::BUTTON;
  data.actions[0]->title = u"Button 1";
  data.actions[1] = blink::mojom::NotificationAction::New();
  data.actions[1]->type = blink::mojom::NotificationActionType::TEXT;
  data.actions[1]->title = u"Button 2";

  NotificationResources notification_resources;
  notification_resources.action_icons.resize(data.actions.size());

  EXPECT_CALL(*mock_logger_, LogPersistentNotificationShown());
  service()->DisplayPersistentNotification(
      kNotificationId, GURL() /* service_worker_scope */,
      GURL("https://chrome.com/"), data, notification_resources);

  ASSERT_EQ(1u, GetNotificationCountForType(
                    NotificationHandler::Type::WEB_PERSISTENT));

  Notification notification = GetDisplayedNotificationForType(
      NotificationHandler::Type::WEB_PERSISTENT);
  EXPECT_EQ("https://chrome.com/", notification.origin_url().spec());
  EXPECT_EQ("My notification's title", base::UTF16ToUTF8(notification.title()));
  EXPECT_EQ("Hello, world!", base::UTF16ToUTF8(notification.message()));

  EXPECT_THAT(notification.vibration_pattern(),
              testing::ElementsAreArray(kNotificationVibrationPattern));

  EXPECT_TRUE(notification.silent());

  const auto& buttons = notification.buttons();
  ASSERT_EQ(2u, buttons.size());
  EXPECT_EQ("Button 1", base::UTF16ToUTF8(buttons[0].title));
  EXPECT_FALSE(buttons[0].placeholder);
  EXPECT_EQ("Button 2", base::UTF16ToUTF8(buttons[1].title));
  EXPECT_TRUE(buttons[1].placeholder);
}

TEST_F(PlatformNotificationServiceTest, RecordNotificationUkmEvent) {
  NotificationDatabaseData data;
  data.notification_id = "notification1";
  data.origin = GURL("https://example.com");
  data.closed_reason = NotificationDatabaseData::ClosedReason::USER;
  data.replaced_existing_notification = true;
  data.notification_data.icon = GURL("https://icon.com");
  data.notification_data.image = GURL("https://image.com");
  data.notification_data.renotify = false;
  data.notification_data.tag = "tag";
  data.notification_data.silent = true;
  auto action1 = blink::mojom::NotificationAction::New();
  data.notification_data.actions.push_back(std::move(action1));
  auto action2 = blink::mojom::NotificationAction::New();
  data.notification_data.actions.push_back(std::move(action2));
  auto action3 = blink::mojom::NotificationAction::New();
  data.notification_data.actions.push_back(std::move(action3));
  data.notification_data.require_interaction = false;
  data.num_clicks = 3;
  data.num_action_button_clicks = 1;
  data.time_until_close_millis = base::Milliseconds(10000);
  data.time_until_first_click_millis = base::Milliseconds(2222);
  data.time_until_last_click_millis = base::Milliseconds(3333);

  // Set up UKM recording conditions.
  auto* history_service = HistoryServiceFactory::GetForProfile(
      profile_.get(), ServiceAccessType::EXPLICIT_ACCESS);
  history_service->AddPage(data.origin, base::Time::Now(),
                           history::SOURCE_BROWSED);

  // Initially there are no UKM entries.
  std::vector<raw_ptr<const ukm::mojom::UkmEntry, VectorExperimental>> entries =
      recorder_->GetEntriesByName(ukm::builders::Notification::kEntryName);
  EXPECT_EQ(0u, entries.size());

  {
    base::RunLoop run_loop;
    service()->set_ukm_recorded_closure_for_testing(run_loop.QuitClosure());
    service()->RecordNotificationUkmEvent(data);
    run_loop.Run();
  }

  entries =
      recorder_->GetEntriesByName(ukm::builders::Notification::kEntryName);
  ASSERT_EQ(1u, entries.size());
  auto* entry = entries[0].get();
  recorder_->ExpectEntryMetric(
      entry, kClosedReason,
      static_cast<int>(NotificationDatabaseData::ClosedReason::USER));
  recorder_->ExpectEntryMetric(entry, kDidReplaceAnotherNotification, true);
  recorder_->ExpectEntryMetric(entry, kHasBadge, false);
  recorder_->ExpectEntryMetric(entry, kHasIcon, 1);
  recorder_->ExpectEntryMetric(entry, kHasImage, 1);
  recorder_->ExpectEntryMetric(entry, kHasRenotify, false);
  recorder_->ExpectEntryMetric(entry, kHasTag, true);
  recorder_->ExpectEntryMetric(entry, kIsSilent, 1);
  recorder_->ExpectEntryMetric(entry, kNumActions, 3);
  recorder_->ExpectEntryMetric(entry, kNumActionButtonClicks, 1);
  recorder_->ExpectEntryMetric(entry, kNumClicks, 3);
  recorder_->ExpectEntryMetric(entry, kRequireInteraction, false);
  recorder_->ExpectEntryMetric(entry, kTimeUntilCloseMillis, 10000);
  recorder_->ExpectEntryMetric(entry, kTimeUntilFirstClickMillis, 2222);
  recorder_->ExpectEntryMetric(entry, kTimeUntilLastClickMillis, 3333);
}

// Expect each call to ReadNextPersistentNotificationId to return a larger
// value.
TEST_F(PlatformNotificationServiceTest, NextPersistentNotificationId) {
  int64_t first_id = service()->ReadNextPersistentNotificationId();
  int64_t second_id = service()->ReadNextPersistentNotificationId();
  EXPECT_LT(first_id, second_id);
}

TEST_F(PlatformNotificationServiceTest,
       ProposedDisruptiveNotificationRevocationMetricsPersistent) {
  GURL url("https://chrome.test/");
  const int kDailyNotificationCount = 4;

  HostContentSettingsMap* hcsm =
      HostContentSettingsMapFactory::GetForProfile(profile_.get());
  DisruptiveNotificationPermissionsManager::RevocationEntry entry(
      /*revocation_state=*/DisruptiveNotificationPermissionsManager::
          RevocationState::kProposed,
      /*site_engagement=*/0.0,
      /*daily_notification_count=*/kDailyNotificationCount);

  DisruptiveNotificationPermissionsManager::ContentSettingHelper(*hcsm)
      .PersistRevocationEntry(url, entry);

  PlatformNotificationData data;
  data.title = u"My notification's title";
  data.body = u"Hello, world!";

  service()->DisplayPersistentNotification(kNotificationId,
                                           GURL() /* service_worker_scope */,
                                           url, data, NotificationResources());

  EXPECT_EQ(1u, GetNotificationCountForType(
                    NotificationHandler::Type::WEB_PERSISTENT));
  // Check that the correct metric is reported.
  EXPECT_EQ(1u, recorder_
                    ->GetEntriesByName(
                        "SafetyHub.DisruptiveNotificationRevocations.Proposed")
                    .size());
}

TEST_F(PlatformNotificationServiceTest,
       ProposedDisruptiveNotificationRevocationMetricsNonPersistent) {
  GURL url("https://chrome.test/");
  const int kDailyNotificationCount = 4;

  HostContentSettingsMap* hcsm =
      HostContentSettingsMapFactory::GetForProfile(profile_.get());
  DisruptiveNotificationPermissionsManager::RevocationEntry entry(
      /*revocation_state=*/DisruptiveNotificationPermissionsManager::
          RevocationState::kProposed,
      /*site_engagement=*/0.0,
      /*daily_notification_count=*/kDailyNotificationCount);

  DisruptiveNotificationPermissionsManager::ContentSettingHelper(*hcsm)
      .PersistRevocationEntry(url, entry);

  PlatformNotificationData data;
  data.title = u"My notification's title";
  data.body = u"Hello, world!";

  service()->DisplayNotification(kNotificationId, url,
                                 /*document_url=*/GURL(), data,
                                 NotificationResources());

  EXPECT_EQ(1u, GetNotificationCountForType(
                    NotificationHandler::Type::WEB_NON_PERSISTENT));
  // Check that the correct metric is reported.
  EXPECT_EQ(1u, recorder_
                    ->GetEntriesByName(
                        "SafetyHub.DisruptiveNotificationRevocations.Proposed")
                    .size());
}

#if !BUILDFLAG(IS_ANDROID)

class PlatformNotificationServiceTest_WebApps
    : public PlatformNotificationServiceTest {
 public:
  void SetUp() override {
    PlatformNotificationServiceTest::SetUp();

    web_app::test::AwaitStartWebAppProviderAndSubsystems(profile_.get());

    web_app::WebAppProvider* provider =
        web_app::WebAppProvider::GetForTest(profile_.get());

    std::unique_ptr<web_app::WebAppInstallInfo> web_app =
        web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(
            kWebAppStartUrl);
    web_app->title = u"Test app 1";
    installed_app_id =
        web_app::test::InstallWebApp(profile_.get(), std::move(web_app));

    web_app = web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(
        kInstalledNestedWebAppStartUrl);
    web_app->title = u"Test app 2";
    nested_installed_app_id =
        web_app::test::InstallWebApp(profile_.get(), std::move(web_app));

    web_app = web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(
        kNotInstalledNestedWebAppStartUrl);
    web_app->title = u"Test app 3";
    web_app::WebAppInstallParams params;
    params.install_state =
        web_app::proto::InstallState::SUGGESTED_FROM_ANOTHER_DEVICE;
    // OS Hooks must be disabled for non-locally installed app.
    params.add_to_applications_menu = false;
    params.add_to_desktop = false;
    params.add_to_quick_launch_bar = false;

    base::test::TestFuture<const webapps::AppId&, webapps::InstallResultCode>
        future;
    provider->scheduler().InstallFromInfoWithParams(
        std::move(web_app), /*overwrite_existing_manifest_fields=*/false,
        webapps::WebappInstallSource::OMNIBOX_INSTALL_ICON,
        future.GetCallback(), params);
    ASSERT_TRUE(future.Wait());
    EXPECT_EQ(future.Get<webapps::InstallResultCode>(),
              webapps::InstallResultCode::kSuccessNewInstall);
  }

 protected:
  const GURL kWebAppOrigin = GURL("https://example.com");
  const GURL kWebAppStartUrl = GURL("https://example.com/scope/start.html");
  const GURL kInstalledNestedWebAppStartUrl =
      GURL("https://example.com/scope/other/foo.html");
  const GURL kNotInstalledNestedWebAppStartUrl =
      GURL("https://example.com/scope/nestedscope/foo.html");
  const GURL kOutOfScopeUrl = GURL("https://example.com/outofscope");
  const GURL kServiceWorkerScope = GURL("https://example.com/scope/");

  static PlatformNotificationData CreateDummyNotificationData() {
    PlatformNotificationData data;
    data.title = u"My Notification";
    data.body = u"Hello, world!";
    return data;
  }
  const PlatformNotificationData kNotificationData =
      CreateDummyNotificationData();

  webapps::AppId installed_app_id;
  webapps::AppId nested_installed_app_id;

 private:
  web_app::OsIntegrationTestOverrideBlockingRegistration fake_os_integration_;
};

TEST_F(PlatformNotificationServiceTest_WebApps, IncomingCallWebApp) {
  EXPECT_TRUE(service()->IsActivelyInstalledWebAppScope(kWebAppStartUrl));

  web_app::test::UninstallAllWebApps(profile_.get());
  EXPECT_FALSE(service()->IsActivelyInstalledWebAppScope(kWebAppStartUrl));
}

TEST_F(PlatformNotificationServiceTest_WebApps, CreateNotificationFromData) {
  PlatformNotificationData notification_data;
  notification_data.title = u"My Notification";
  notification_data.body = u"Hello, world!";

  GURL origin = url::Origin::Create(kWebAppStartUrl).GetURL();

  Notification notification = service()->CreateNotificationFromData(
      origin, "id", notification_data, NotificationResources(),
      /*web_app_hint_url=*/kWebAppStartUrl);
  EXPECT_EQ(notification.notifier_id().web_app_id, installed_app_id);

  Notification nested_notification = service()->CreateNotificationFromData(
      origin, "id", notification_data, NotificationResources(),
      /*web_app_hint_url=*/kInstalledNestedWebAppStartUrl);
  EXPECT_EQ(nested_notification.notifier_id().web_app_id,
            nested_installed_app_id);
}

TEST_F(PlatformNotificationServiceTest_WebApps, PopulateWebAppId_MatchesScope) {
  service()->DisplayNotification(kNotificationId, kWebAppOrigin,
                                 kNotInstalledNestedWebAppStartUrl,
                                 kNotificationData, NotificationResources());
  ASSERT_EQ(1u, GetNotificationCountForType(
                    NotificationHandler::Type::WEB_NON_PERSISTENT));
  Notification notification = GetDisplayedNotificationForType(
      NotificationHandler::Type::WEB_NON_PERSISTENT);
  EXPECT_EQ(installed_app_id, notification.notifier_id().web_app_id);

  service()->DisplayPersistentNotification(kNotificationId, kServiceWorkerScope,
                                           kWebAppOrigin, kNotificationData,
                                           NotificationResources());
  ASSERT_EQ(1u, GetNotificationCountForType(
                    NotificationHandler::Type::WEB_PERSISTENT));
  notification = GetDisplayedNotificationForType(
      NotificationHandler::Type::WEB_PERSISTENT);
  EXPECT_EQ(installed_app_id, notification.notifier_id().web_app_id);
}

TEST_F(PlatformNotificationServiceTest_WebApps,
       PopulateWebAppId_InNestedScope) {
  service()->DisplayNotification(kNotificationId, kWebAppOrigin,
                                 kInstalledNestedWebAppStartUrl,
                                 kNotificationData, NotificationResources());
  ASSERT_EQ(1u, GetNotificationCountForType(
                    NotificationHandler::Type::WEB_NON_PERSISTENT));
  Notification notification = GetDisplayedNotificationForType(
      NotificationHandler::Type::WEB_NON_PERSISTENT);
  EXPECT_EQ(nested_installed_app_id, notification.notifier_id().web_app_id);
}

TEST_F(PlatformNotificationServiceTest_WebApps, PopulateWebAppId_NotInScope) {
  service()->DisplayNotification(kNotificationId, kWebAppOrigin, kOutOfScopeUrl,
                                 kNotificationData, NotificationResources());
  ASSERT_EQ(1u, GetNotificationCountForType(
                    NotificationHandler::Type::WEB_NON_PERSISTENT));
  Notification notification = GetDisplayedNotificationForType(
      NotificationHandler::Type::WEB_NON_PERSISTENT);
  EXPECT_EQ(std::nullopt, notification.notifier_id().web_app_id);

  service()->DisplayPersistentNotification(kNotificationId, kOutOfScopeUrl,
                                           kWebAppOrigin, kNotificationData,
                                           NotificationResources());
  ASSERT_EQ(1u, GetNotificationCountForType(
                    NotificationHandler::Type::WEB_PERSISTENT));
  notification = GetDisplayedNotificationForType(
      NotificationHandler::Type::WEB_PERSISTENT);
  EXPECT_EQ(std::nullopt, notification.notifier_id().web_app_id);
}

#endif  // !BUILDFLAG(IS_ANDROID)

#if BUILDFLAG(ENABLE_EXTENSIONS)

TEST_F(PlatformNotificationServiceTest, DisplayNameForContextMessage) {
  std::u16string display_name =
      service()->DisplayNameForContextMessage(GURL("https://chrome.com/"));

  EXPECT_TRUE(display_name.empty());

  // Create a mocked extension.
  scoped_refptr<const extensions::Extension> extension =
      extensions::ExtensionBuilder()
          .SetID("honijodknafkokifofgiaalefdiedpko")
          .SetManifest(base::Value::Dict()
                           .Set("name", "NotificationTest")
                           .Set("version", "1.0")
                           .Set("manifest_version", 2)
                           .Set("description", "Test Extension"))
          .Build();

  extensions::ExtensionRegistry* registry =
      extensions::ExtensionRegistry::Get(profile_.get());
  EXPECT_TRUE(registry->AddEnabled(extension));

  display_name = service()->DisplayNameForContextMessage(
      GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"));
  EXPECT_EQ("NotificationTest", base::UTF16ToUTF8(display_name));
}

TEST_F(PlatformNotificationServiceTest, CreateNotificationFromData) {
  PlatformNotificationData notification_data;
  notification_data.title = u"My Notification";
  notification_data.body = u"Hello, world!";
  GURL origin("https://chrome.com/");

  Notification notification = service()->CreateNotificationFromData(
      origin, "id", notification_data, NotificationResources(),
      /*web_app_hint_url=*/GURL());
  EXPECT_TRUE(notification.context_message().empty());

  // Create a mocked extension.
  scoped_refptr<const extensions::Extension> extension =
      extensions::ExtensionBuilder()
          .SetID("honijodknafkokifofgiaalefdiedpko")
          .SetManifest(base::Value::Dict()
                           .Set("name", "NotificationTest")
                           .Set("version", "1.0")
                           .Set("manifest_version", 2)
                           .Set("description", "Test Extension"))
          .Build();

  extensions::ExtensionRegistry* registry =
      extensions::ExtensionRegistry::Get(profile_.get());
  EXPECT_TRUE(registry->AddEnabled(extension));

  notification = service()->CreateNotificationFromData(
      GURL("chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"),
      "id", notification_data, NotificationResources(),
      /*web_app_hint_url=*/GURL());
  EXPECT_EQ("NotificationTest",
            base::UTF16ToUTF8(notification.context_message()));
}

#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

#if BUILDFLAG(IS_CHROMEOS)
using PlatformNotificationServiceTest_WebAppNotificationIconAndTitle =
    PlatformNotificationServiceTest;

TEST_F(PlatformNotificationServiceTest_WebAppNotificationIconAndTitle,
       FindWebAppIconAndTitle_NoApp) {
  web_app::FakeWebAppProvider* provider =
      web_app::FakeWebAppProvider::Get(profile_.get());
  provider->Start();

  const GURL web_app_url{"https://example.org/"};
  EXPECT_FALSE(service()->FindWebAppIconAndTitle(web_app_url).has_value());
}

TEST_F(PlatformNotificationServiceTest_WebAppNotificationIconAndTitle,
       FindWebAppIconAndTitle) {
  web_app::FakeWebAppProvider* provider =
      web_app::FakeWebAppProvider::Get(profile_.get());
  web_app::WebAppIconManager& icon_manager = provider->GetIconManager();

  std::unique_ptr<web_app::WebApp> web_app = web_app::test::CreateWebApp();
  const GURL web_app_url = web_app->start_url();
  const webapps::AppId app_id = web_app->app_id();
  web_app->SetName("Web App Title");

  IconManagerWriteGeneratedIcons(icon_manager, app_id,
                                 {{web_app::IconPurpose::MONOCHROME,
                                   {web_app::icon_size::k16},
                                   {SK_ColorTRANSPARENT}}});
  web_app->SetDownloadedIconSizes(web_app::IconPurpose::MONOCHROME,
                                  {web_app::icon_size::k16});

  provider->GetRegistrarMutable().registry().emplace(app_id,
                                                     std::move(web_app));

  base::RunLoop run_loop;
  icon_manager.SetFaviconMonochromeReadCallbackForTesting(
      base::BindLambdaForTesting(
          [&](const webapps::AppId& cached_app_id) { run_loop.Quit(); }));
  icon_manager.Start();
  run_loop.Run();

  provider->Start();

  std::optional<PlatformNotificationServiceImpl::WebAppIconAndTitle>
      icon_and_title = service()->FindWebAppIconAndTitle(web_app_url);

  ASSERT_TRUE(icon_and_title.has_value());
  EXPECT_EQ(u"Web App Title", icon_and_title->title);
  EXPECT_FALSE(icon_and_title->icon.isNull());
  EXPECT_EQ(
      SK_ColorTRANSPARENT,
      icon_and_title->icon.GetRepresentation(1.0f).GetBitmap().getColor(0, 0));
}
#endif  // BUILDFLAG(IS_CHROMEOS)

class PlatformNotificationServiceTest_NotificationContentDetection
    : public PlatformNotificationServiceTest,
      public testing::WithParamInterface<std::tuple<bool, bool>> {
 public:
  void SetUp() override {
    TestingProfile::Builder profile_builder;
    profile_builder.AddTestingFactory(
        HistoryServiceFactory::GetInstance(),
        HistoryServiceFactory::GetDefaultFactory());
    profile_ = profile_builder.Build();
    if (IsNotificationContentDetectionEnabled()) {
      scoped_feature_list_.InitWithFeatures(
          {safe_browsing::kOnDeviceNotificationContentDetectionModel},
          {safe_browsing::kReportNotificationContentDetectionData});
    } else {
      scoped_feature_list_.InitWithFeatures(
          {}, {safe_browsing::kOnDeviceNotificationContentDetectionModel,
               safe_browsing::kReportNotificationContentDetectionData});
    }
    if (IsSafeBrowsingEnabled()) {
      profile_->GetTestingPrefService()->SetManagedPref(
          prefs::kSafeBrowsingEnabled, std::make_unique<base::Value>(true));
    } else {
      profile_->GetTestingPrefService()->SetManagedPref(
          prefs::kSafeBrowsingEnabled, std::make_unique<base::Value>(false));
    }
    mock_notification_content_detection_service_ = static_cast<
        safe_browsing::MockNotificationContentDetectionService*>(
        safe_browsing::NotificationContentDetectionServiceFactory::GetInstance()
            ->SetTestingFactoryAndUse(
                profile_.get(),
                base::BindRepeating(
                    &safe_browsing::MockNotificationContentDetectionService::
                        FactoryForTests,
                    &model_observer_tracker_,
                    base::ThreadPool::CreateSequencedTaskRunner(
                        {base::MayBlock()}))));
  }

  void TearDown() override {
    mock_notification_content_detection_service_ = nullptr;
    PlatformNotificationServiceTest::TearDown();
  }

  bool IsSafeBrowsingEnabled() { return std::get<0>(GetParam()); }

  bool IsNotificationContentDetectionEnabled() {
    return std::get<1>(GetParam());
  }

 protected:
  raw_ptr<safe_browsing::MockNotificationContentDetectionService>
      mock_notification_content_detection_service_ = nullptr;
  safe_browsing::TestModelObserverTracker model_observer_tracker_;
};

INSTANTIATE_TEST_SUITE_P(
    ,
    PlatformNotificationServiceTest_NotificationContentDetection,
    testing::Combine(testing::Bool(), testing::Bool()));

TEST_P(PlatformNotificationServiceTest_NotificationContentDetection,
       PerformNotificationContentDetectionWhenEnabled) {
  PlatformNotificationData data;
  data.title = u"My notification's title";
  data.body = u"Hello, world!";

  int expected_number_of_calls = 0;
  if (IsSafeBrowsingEnabled() && IsNotificationContentDetectionEnabled()) {
    expected_number_of_calls = 1;
  }
  EXPECT_CALL(*mock_notification_content_detection_service_,
              MaybeCheckNotificationContentDetectionModel(_, _, _, _))
      .Times(expected_number_of_calls);
  service()->DisplayPersistentNotification(
      kNotificationId, GURL() /* service_worker_scope */,
      GURL("https://chrome.com/"), data, NotificationResources());
}

class PlatformNotificationServiceTest_ReportNotificationContentDetectionData
    : public PlatformNotificationServiceTest {
 public:
  void SetUp() override {
    TestingProfile::Builder profile_builder;
    profile_builder.AddTestingFactory(
        HistoryServiceFactory::GetInstance(),
        HistoryServiceFactory::GetDefaultFactory());
    profile_ = profile_builder.Build();
    scoped_feature_list_.InitWithFeatures(
        {safe_browsing::kOnDeviceNotificationContentDetectionModel,
         safe_browsing::kReportNotificationContentDetectionData},
        {});
    profile_->GetTestingPrefService()->SetManagedPref(
        prefs::kSafeBrowsingEnabled, std::make_unique<base::Value>(true));
    mock_notification_content_detection_service_ = static_cast<
        safe_browsing::MockNotificationContentDetectionService*>(
        safe_browsing::NotificationContentDetectionServiceFactory::GetInstance()
            ->SetTestingFactoryAndUse(
                profile_.get(),
                base::BindRepeating(
                    &safe_browsing::MockNotificationContentDetectionService::
                        FactoryForTests,
                    &model_observer_tracker_,
                    base::ThreadPool::CreateSequencedTaskRunner(
                        {base::MayBlock()}))));
  }

  void TearDown() override {
    mock_notification_content_detection_service_ = nullptr;
    PlatformNotificationServiceTest::TearDown();
  }

 protected:
  raw_ptr<safe_browsing::MockNotificationContentDetectionService>
      mock_notification_content_detection_service_ = nullptr;
  safe_browsing::TestModelObserverTracker model_observer_tracker_;
};

TEST_F(PlatformNotificationServiceTest_ReportNotificationContentDetectionData,
       UpdateNotificationDatabaseMetadata) {
  // Store notification data in `NotificationDatabase`.
  const int64_t kFakeServiceWorkerRegistrationId = 42;
  int notification_id = 1;
  GURL origin("https://example.com");
  NotificationDatabaseData notification_database_data;
  notification_database_data.origin = origin;
  GetPlatformNotificationContext(origin)->WriteNotificationData(
      notification_id, kFakeServiceWorkerRegistrationId, origin,
      notification_database_data, base::DoNothing());
  base::RunLoop().RunUntilIdle();
  // Update `NotificationDatabase` entry with metadata.
  std::string full_notification_id_str =
      "p#" + origin.spec() + "#0" + base::NumberToString(notification_id);
  Notification notification = message_center::Notification(
      message_center::NOTIFICATION_TYPE_SIMPLE, full_notification_id_str,
      /*title=*/std::u16string(),
      /*message=*/std::u16string(), /*icon=*/ui::ImageModel(),
      /*display_source=*/std::u16string(), origin, message_center::NotifierId(),
      message_center::RichNotificationData(), /*delegate=*/nullptr);
  auto metadata = std::make_unique<PersistentNotificationMetadata>();
  bool is_on_global_cache_list = false;
  bool is_allowlisted_by_user = false;
  double suspicious_score = 70.0;
  service()->UpdatePersistentMetadataThenDisplay(
      notification, std::move(metadata), /*should_show_warning=*/true,
      safe_browsing::NotificationContentDetectionModel::GetSerializedMetadata(
          is_on_global_cache_list, is_allowlisted_by_user, suspicious_score));
  // Read and check the entry from `NotificationDatabase`.
  NotificationDatabaseData data =
      ReadNotificationDataAndRecordInteractionSynchronous(
          GetPlatformNotificationContext(origin),
          base::NumberToString(notification_id), origin);
  ASSERT_TRUE(
      data.serialized_metadata.contains(safe_browsing::kMetadataDictionaryKey));
  ASSERT_EQ(
      safe_browsing::NotificationContentDetectionModel::GetSerializedMetadata(
          is_on_global_cache_list, is_allowlisted_by_user, suspicious_score),
      data.serialized_metadata.at(safe_browsing::kMetadataDictionaryKey));
  HostContentSettingsMap* hcsm =
      HostContentSettingsMapFactory::GetForProfile(profile_.get());
  base::Value cur_value(hcsm->GetWebsiteSetting(
      origin, origin, ContentSettingsType::SUSPICIOUS_NOTIFICATION_IDS));
#if BUILDFLAG(IS_ANDROID)
  const base::Value::List* suspicious_notification_ids =
      cur_value.GetDict().FindList(
          safe_browsing::kSuspiciousNotificationIdsKey);
  ASSERT_EQ(1u, suspicious_notification_ids->size());
  ASSERT_EQ(full_notification_id_str,
            suspicious_notification_ids->front().GetString());
#else
  ASSERT_TRUE(cur_value.is_none());
#endif
}