File: permission_decision_auto_blocker_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (980 lines) | stat: -rw-r--r-- 41,927 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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
// Copyright 2016 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/permissions/permission_decision_auto_blocker.h"

#include <map>
#include <memory>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/gtest_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/permissions/features.h"
#include "components/permissions/permission_util.h"
#include "components/permissions/test/test_permissions_client.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"

namespace permissions {
namespace {

using PermissionStatus = blink::mojom::PermissionStatus;

bool FilterGoogle(const GURL& url) {
  return url == "https://www.google.com/";
}

bool FilterAll(const GURL& url) {
  return true;
}

}  // namespace

class PermissionDecisionAutoBlockerUnitTest : public testing::Test {
 protected:
  PermissionDecisionAutoBlockerUnitTest() {
    feature_list_.InitWithFeatures({features::kBlockPromptsIfDismissedOften,
                                    features::kBlockPromptsIfIgnoredOften},
                                   {});
    last_embargoed_status_ = false;
    autoblocker()->SetClockForTesting(&clock_);
    callback_was_run_ = false;
  }

  PermissionDecisionAutoBlocker* autoblocker() {
    return PermissionsClient::Get()->GetPermissionDecisionAutoBlocker(
        &browser_context_);
  }

  void SetLastEmbargoStatus(base::OnceClosure quit_closure, bool status) {
    callback_was_run_ = true;
    last_embargoed_status_ = status;
    if (quit_closure) {
      std::move(quit_closure).Run();
    }
  }

  bool last_embargoed_status() { return last_embargoed_status_; }

  bool callback_was_run() { return callback_was_run_; }

  base::SimpleTestClock* clock() { return &clock_; }

 private:
  content::BrowserTaskEnvironment task_environment_;
  base::SimpleTestClock clock_;
  base::test::ScopedFeatureList feature_list_;
  content::TestBrowserContext browser_context_;
  TestPermissionsClient permissions_client_;
  bool last_embargoed_status_;
  bool callback_was_run_;
};

class MockObserver : public PermissionDecisionAutoBlocker::Observer {
 public:
  void OnEmbargoStarted(const GURL& origin,
                        ContentSettingsType content_setting) override {
    callbacks_[origin].push_back(content_setting);
  }

  std::map<GURL, std::vector<ContentSettingsType>>& GetCallbacks() {
    return callbacks_;
  }

 private:
  std::map<GURL, std::vector<ContentSettingsType>> callbacks_;
};

// Check removing the the embargo for a single permission on a site works, and
// that it doesn't interfere with other embargoed permissions or the same
// permission embargoed on other sites.
TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveEmbargoAndResetCounts) {
  GURL url1("https://www.google.com");
  GURL url2("https://www.example.com");

  // Record dismissals for location and notifications in |url1|.
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::GEOLOCATION, false));
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::GEOLOCATION, false));
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::GEOLOCATION, false));
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::NOTIFICATIONS, false));
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::NOTIFICATIONS, false));
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::NOTIFICATIONS, false));
  // Record dismissals for location in |url2|.
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url2, ContentSettingsType::GEOLOCATION, false));
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url2, ContentSettingsType::GEOLOCATION, false));
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url2, ContentSettingsType::GEOLOCATION, false));

  // Verify all dismissals recorded above resulted in embargo.
  absl::optional<content::PermissionResult> result =
      autoblocker()->GetEmbargoResult(url1, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);
  result =
      autoblocker()->GetEmbargoResult(url1, ContentSettingsType::NOTIFICATIONS);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);
  result =
      autoblocker()->GetEmbargoResult(url2, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);

  // Remove the embargo on notifications. Verify it is no longer under embargo,
  // but location still is.
  autoblocker()->RemoveEmbargoAndResetCounts(
      url1, ContentSettingsType::NOTIFICATIONS);
  result =
      autoblocker()->GetEmbargoResult(url1, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);
  result =
      autoblocker()->GetEmbargoResult(url1, ContentSettingsType::NOTIFICATIONS);
  // If not under embargo, GetEmbargoResult() returns absl::nullopt.
  EXPECT_FALSE(result.has_value());
  // Verify |url2|'s embargo is still intact as well.
  result =
      autoblocker()->GetEmbargoResult(url2, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);
}

// Test it does not take one more dismissal to re-trigger embargo after
// removing the embargo status for a site.
TEST_F(PermissionDecisionAutoBlockerUnitTest,
       DismissAfterRemovingEmbargoByURL) {
  GURL url("https://www.example.com");

  // Record dismissals for location.
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));

  // Verify location is under embargo.
  absl::optional<content::PermissionResult> result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);

  // Remove embargo and verify this is true.
  autoblocker()->RemoveEmbargoAndResetCounts(url,
                                             ContentSettingsType::GEOLOCATION);
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_FALSE(result.has_value());

  // Record another dismissal and verify location is not under embargo again.
  autoblocker()->RecordDismissAndEmbargo(url, ContentSettingsType::GEOLOCATION,
                                         false);
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_FALSE(result.has_value());
}

TEST_F(PermissionDecisionAutoBlockerUnitTest,
       NonembargoedOriginRemoveEmbargoCounts) {
  GURL gurl_to_embargo("https://www.google.com");

  // Make sure that an origin's Dismiss count is 0.
  EXPECT_EQ(0, autoblocker()->GetDismissCount(
                   gurl_to_embargo, ContentSettingsType::GEOLOCATION));

  // Dismiss the origin a few times but do not add under embargo.
  autoblocker()->RecordDismissAndEmbargo(
      gurl_to_embargo, ContentSettingsType::GEOLOCATION, false);
  autoblocker()->RecordDismissAndEmbargo(
      gurl_to_embargo, ContentSettingsType::GEOLOCATION, false);

  EXPECT_EQ(2, autoblocker()->GetDismissCount(
                   gurl_to_embargo, ContentSettingsType::GEOLOCATION));

  autoblocker()->RemoveEmbargoAndResetCounts(gurl_to_embargo,
                                             ContentSettingsType::GEOLOCATION);

  EXPECT_EQ(0, autoblocker()->GetDismissCount(
                   gurl_to_embargo, ContentSettingsType::GEOLOCATION));

  autoblocker()->RecordIgnoreAndEmbargo(gurl_to_embargo,
                                        ContentSettingsType::MIDI_SYSEX, false);
  autoblocker()->RecordIgnoreAndEmbargo(gurl_to_embargo,
                                        ContentSettingsType::MIDI_SYSEX, false);

  EXPECT_EQ(2, autoblocker()->GetIgnoreCount(gurl_to_embargo,
                                             ContentSettingsType::MIDI_SYSEX));

  autoblocker()->RemoveEmbargoAndResetCounts(gurl_to_embargo,
                                             ContentSettingsType::MIDI_SYSEX);

  EXPECT_EQ(0, autoblocker()->GetIgnoreCount(gurl_to_embargo,
                                             ContentSettingsType::MIDI_SYSEX));
}

TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveEmbargoCounts) {
  GURL gurl_to_embargo("https://www.google.com");

  // Add an origin under embargo for 2 dismissed and 1 ignored
  // ContentSettingsType.
  autoblocker()->RecordDismissAndEmbargo(
      gurl_to_embargo, ContentSettingsType::GEOLOCATION, false);
  autoblocker()->RecordDismissAndEmbargo(
      gurl_to_embargo, ContentSettingsType::GEOLOCATION, false);
  autoblocker()->RecordDismissAndEmbargo(
      gurl_to_embargo, ContentSettingsType::GEOLOCATION, false);

  EXPECT_EQ(3, autoblocker()->GetDismissCount(
                   gurl_to_embargo, ContentSettingsType::GEOLOCATION));

  autoblocker()->RecordDismissAndEmbargo(
      gurl_to_embargo, ContentSettingsType::NOTIFICATIONS, false);
  autoblocker()->RecordDismissAndEmbargo(
      gurl_to_embargo, ContentSettingsType::NOTIFICATIONS, false);
  autoblocker()->RecordDismissAndEmbargo(
      gurl_to_embargo, ContentSettingsType::NOTIFICATIONS, false);

  EXPECT_EQ(3, autoblocker()->GetDismissCount(
                   gurl_to_embargo, ContentSettingsType::NOTIFICATIONS));

  autoblocker()->RecordIgnoreAndEmbargo(gurl_to_embargo,
                                        ContentSettingsType::MIDI_SYSEX, false);
  autoblocker()->RecordIgnoreAndEmbargo(gurl_to_embargo,
                                        ContentSettingsType::MIDI_SYSEX, false);
  autoblocker()->RecordIgnoreAndEmbargo(gurl_to_embargo,
                                        ContentSettingsType::MIDI_SYSEX, false);
  autoblocker()->RecordIgnoreAndEmbargo(gurl_to_embargo,
                                        ContentSettingsType::MIDI_SYSEX, false);

  EXPECT_EQ(4, autoblocker()->GetIgnoreCount(gurl_to_embargo,
                                             ContentSettingsType::MIDI_SYSEX));

  autoblocker()->RemoveEmbargoAndResetCounts(gurl_to_embargo,
                                             ContentSettingsType::GEOLOCATION);

  // GEOLOCATION has been cleared, a dismiss count should be 0.
  EXPECT_EQ(0, autoblocker()->GetDismissCount(
                   gurl_to_embargo, ContentSettingsType::GEOLOCATION));
  // GEOLOCATION has been cleared, but other counts should be
  // preservet.
  EXPECT_EQ(3, autoblocker()->GetDismissCount(
                   gurl_to_embargo, ContentSettingsType::NOTIFICATIONS));
  EXPECT_EQ(4, autoblocker()->GetIgnoreCount(gurl_to_embargo,
                                             ContentSettingsType::MIDI_SYSEX));
}

TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveEmbargoAndResetCounts_All) {
  GURL url1("https://www.google.com");
  GURL url2("https://www.example.com");

  // Record some dismissals.
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(1, autoblocker()->GetDismissCount(
                   url1, ContentSettingsType::GEOLOCATION));

  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(2, autoblocker()->GetDismissCount(
                   url1, ContentSettingsType::GEOLOCATION));

  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(3, autoblocker()->GetDismissCount(
                   url1, ContentSettingsType::GEOLOCATION));

  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url2, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(1, autoblocker()->GetDismissCount(
                   url2, ContentSettingsType::GEOLOCATION));

  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::NOTIFICATIONS, false));
  EXPECT_EQ(1, autoblocker()->GetDismissCount(
                   url1, ContentSettingsType::NOTIFICATIONS));

  // Record some ignores.
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url1, ContentSettingsType::MIDI_SYSEX, false));
  EXPECT_EQ(
      1, autoblocker()->GetIgnoreCount(url1, ContentSettingsType::MIDI_SYSEX));
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url1, ContentSettingsType::DURABLE_STORAGE, false));
  EXPECT_EQ(1, autoblocker()->GetIgnoreCount(
                   url1, ContentSettingsType::DURABLE_STORAGE));
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url2, ContentSettingsType::GEOLOCATION, false));
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url2, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(
      2, autoblocker()->GetIgnoreCount(url2, ContentSettingsType::GEOLOCATION));

  autoblocker()->RemoveEmbargoAndResetCounts(
      base::BindRepeating(&FilterGoogle));

  // Expect that url1's actions are gone, but url2's remain.
  EXPECT_EQ(0, autoblocker()->GetDismissCount(
                   url1, ContentSettingsType::GEOLOCATION));
  EXPECT_EQ(0, autoblocker()->GetDismissCount(
                   url1, ContentSettingsType::NOTIFICATIONS));
  EXPECT_EQ(
      0, autoblocker()->GetIgnoreCount(url1, ContentSettingsType::MIDI_SYSEX));
  EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
                   url1, ContentSettingsType::DURABLE_STORAGE));

  EXPECT_EQ(1, autoblocker()->GetDismissCount(
                   url2, ContentSettingsType::GEOLOCATION));
  EXPECT_EQ(
      2, autoblocker()->GetIgnoreCount(url2, ContentSettingsType::GEOLOCATION));

  // Add some more actions.
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(1, autoblocker()->GetDismissCount(
                   url1, ContentSettingsType::GEOLOCATION));

  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::NOTIFICATIONS, false));
  EXPECT_EQ(1, autoblocker()->GetDismissCount(
                   url1, ContentSettingsType::NOTIFICATIONS));

  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url2, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(2, autoblocker()->GetDismissCount(
                   url2, ContentSettingsType::GEOLOCATION));

  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url1, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(
      1, autoblocker()->GetIgnoreCount(url1, ContentSettingsType::GEOLOCATION));
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url1, ContentSettingsType::NOTIFICATIONS, false));
  EXPECT_EQ(1, autoblocker()->GetIgnoreCount(
                   url1, ContentSettingsType::NOTIFICATIONS));
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url1, ContentSettingsType::DURABLE_STORAGE, false));
  EXPECT_EQ(1, autoblocker()->GetIgnoreCount(
                   url1, ContentSettingsType::DURABLE_STORAGE));
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url2, ContentSettingsType::MIDI_SYSEX, false));
  EXPECT_EQ(
      1, autoblocker()->GetIgnoreCount(url2, ContentSettingsType::MIDI_SYSEX));

  // Remove everything and expect that it's all gone.
  autoblocker()->RemoveEmbargoAndResetCounts(base::BindRepeating(&FilterAll));

  EXPECT_EQ(0, autoblocker()->GetDismissCount(
                   url1, ContentSettingsType::GEOLOCATION));
  EXPECT_EQ(0, autoblocker()->GetDismissCount(
                   url1, ContentSettingsType::NOTIFICATIONS));
  EXPECT_EQ(0, autoblocker()->GetDismissCount(
                   url2, ContentSettingsType::GEOLOCATION));

  EXPECT_EQ(
      0, autoblocker()->GetIgnoreCount(url1, ContentSettingsType::GEOLOCATION));
  EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
                   url1, ContentSettingsType::NOTIFICATIONS));
  EXPECT_EQ(
      0, autoblocker()->GetIgnoreCount(url2, ContentSettingsType::GEOLOCATION));
  EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
                   url2, ContentSettingsType::DURABLE_STORAGE));
  EXPECT_EQ(
      0, autoblocker()->GetIgnoreCount(url2, ContentSettingsType::MIDI_SYSEX));
}

// Check that GetEmbargoedOrigins only returns origins where embargo is the
// effective permission enforcement mechanism.
TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoedOrigins) {
  GURL url1("https://www.google.com");
  GURL url2("https://www.google.com:8443");
  std::vector<ContentSettingsType> content_types = {
      ContentSettingsType::GEOLOCATION, ContentSettingsType::NOTIFICATIONS};
  std::set<GURL> origins;
  clock()->SetNow(base::Time::Now());

  EXPECT_EQ(0UL, autoblocker()->GetEmbargoedOrigins(content_types).size());

  // Place both origins under embargo and verify.
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::GEOLOCATION, false));
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::GEOLOCATION, false));
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url1, ContentSettingsType::GEOLOCATION, false));
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url2, ContentSettingsType::NOTIFICATIONS, false));
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url2, ContentSettingsType::NOTIFICATIONS, false));
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url2, ContentSettingsType::NOTIFICATIONS, false));

  origins = autoblocker()->GetEmbargoedOrigins(content_types);
  EXPECT_EQ(2UL, origins.size());
  EXPECT_EQ(1UL, origins.count(url1));
  EXPECT_EQ(1UL, origins.count(url2));

  // Check no leakage between content types
  origins =
      autoblocker()->GetEmbargoedOrigins(ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(1UL, origins.count(url1));
  origins =
      autoblocker()->GetEmbargoedOrigins(ContentSettingsType::NOTIFICATIONS);
  EXPECT_EQ(1UL, origins.count(url2));

  // Remove an embargo and confirm it's removed from origins
  autoblocker()->RemoveEmbargoAndResetCounts(url1,
                                             ContentSettingsType::GEOLOCATION);
  origins = autoblocker()->GetEmbargoedOrigins(content_types);
  EXPECT_EQ(1UL, origins.size());
  EXPECT_EQ(1UL, origins.count(url2));

  // Expire the remaining embargo and confirm the origin is removed
  clock()->Advance(base::Days(8));
  origins = autoblocker()->GetEmbargoedOrigins(content_types);
  EXPECT_EQ(0UL, origins.size());
}

// Check that GetEmbargoResult returns the correct value when the embargo is set
// and expires.
TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) {
  GURL url("https://www.google.com");
  clock()->SetNow(base::Time::Now());

  // Check the default state.
  absl::optional<content::PermissionResult> result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_FALSE(result.has_value());

  // Place under embargo and verify.
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);

  // Check that the origin is not under embargo for a different permission.
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::NOTIFICATIONS);
  EXPECT_FALSE(result.has_value());

  // Confirm embargo status during the embargo period.
  clock()->Advance(base::Days(5));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);

  // Check embargo is lifted on expiry day. A small offset after the exact
  // embargo expiration date has been added to account for any precision errors
  // when removing the date stored as a double from the permission dictionary.
  clock()->Advance(base::Hours(3 * 24 + 1));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_FALSE(result.has_value());

  // Check embargo is lifted well after the expiry day.
  clock()->Advance(base::Days(1));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_FALSE(result.has_value());

  // Place under embargo again and verify the embargo status.
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::NOTIFICATIONS, false));
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::NOTIFICATIONS, false));
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::NOTIFICATIONS, false));
  clock()->Advance(base::Days(1));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::NOTIFICATIONS);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_FALSE(result.has_value());
}

// Check that GetEmbargoStartTime returns the correct time for embargoes whether
// they are nonexistent, expired or active.
TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStartTime) {
  GURL url("https://www.google.com");

  // The time recorded for embargoes will be stored as a double, which will
  // cause aliasing to a limited set of base::Time values upon retrieval. We
  // thus pick a base::Time for our test time that is part of this set via
  // aliasing the current time by passing it through a double. This allows us
  // to directly compare the test time and times retrieved from storage.
  base::Time test_time = base::Time::FromDeltaSinceWindowsEpoch(
      base::Microseconds(static_cast<double>(
          base::Time::Now().ToDeltaSinceWindowsEpoch().InMicroseconds())));
  clock()->SetNow(test_time);

  // Check the default non embargod state.
  base::Time embargo_start_time =
      autoblocker()->GetEmbargoStartTime(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(base::Time(), embargo_start_time);

  // Ensure that dismissing less than the required number for an embargo
  // does not record an embargo start time.
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  embargo_start_time =
      autoblocker()->GetEmbargoStartTime(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(base::Time(), embargo_start_time);

  // Place site under geolocation dismissal embargo.
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));

  // Confirm embargo is recorded as starting at the correct time.
  embargo_start_time =
      autoblocker()->GetEmbargoStartTime(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(test_time, embargo_start_time);

  // Ensure moving clock while within embargo period does not affect embargo
  // start time.
  clock()->Advance(base::Days(5));
  embargo_start_time =
      autoblocker()->GetEmbargoStartTime(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(test_time, embargo_start_time);

  // Move clock beyond embaro (plus a small offset for potential precision
  // errors) and confirm start time is unaffected but embargo is suspended.
  clock()->Advance(base::Hours(3 * 24 + 1));
  embargo_start_time =
      autoblocker()->GetEmbargoStartTime(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(test_time, embargo_start_time);
  absl::optional<content::PermissionResult> result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_FALSE(result.has_value());

  // Advance time, reinstate embargo and confirm that time is updated.
  test_time += base::Days(9);
  test_time = base::Time::FromDeltaSinceWindowsEpoch(
      base::Microseconds(static_cast<double>(
          test_time.ToDeltaSinceWindowsEpoch().InMicroseconds())));
  clock()->SetNow(test_time);

  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  embargo_start_time =
      autoblocker()->GetEmbargoStartTime(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(test_time, embargo_start_time);

  // Advance time to expire dismiss embargo and create new embargo for ignoring.
  test_time += base::Days(7);
  test_time = base::Time::FromDeltaSinceWindowsEpoch(
      base::Microseconds(static_cast<double>(
          test_time.ToDeltaSinceWindowsEpoch().InMicroseconds())));
  clock()->SetNow(test_time);

  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_TRUE(autoblocker()->RecordIgnoreAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));

  // Confirm the most recent embargo is updated to match new ignore embargo.
  embargo_start_time =
      autoblocker()->GetEmbargoStartTime(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(test_time, embargo_start_time);

  // Advance time, reinstate the dismiss embargo via a single action, and
  // confirm that time is updated.
  test_time += base::Days(1);
  test_time = base::Time::FromDeltaSinceWindowsEpoch(
      base::Microseconds(static_cast<double>(
          test_time.ToDeltaSinceWindowsEpoch().InMicroseconds())));
  clock()->SetNow(test_time);

  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  embargo_start_time =
      autoblocker()->GetEmbargoStartTime(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(test_time, embargo_start_time);

  // Remove records of dismiss and ignore embargoes and confirm start time
  // reverts to default.
  autoblocker()->RemoveEmbargoAndResetCounts(
      base::BindRepeating(&FilterGoogle));
  embargo_start_time =
      autoblocker()->GetEmbargoStartTime(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(base::Time(), embargo_start_time);
}

// Tests the alternating pattern of the block on multiple dismiss behaviour. On
// N dismissals, the origin to be embargoed for the requested permission and
// automatically blocked. Each time the embargo is lifted, the site gets another
// chance to request the permission, but if it is again dismissed it is placed
// under embargo again and its permission requests blocked.
TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoBackoff) {
  GURL url("https://www.google.com");
  clock()->SetNow(base::Time::Now());
  base::HistogramTester histograms;

  // Record some dismisses.
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));

  // A request with < 3 prior dismisses should not be automatically blocked.
  absl::optional<content::PermissionResult> result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_FALSE(result.has_value());

  // After the 3rd dismiss subsequent permission requests should be autoblocked.
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);

  // Accelerate time forward, check that the embargo status is lifted and the
  // request won't be automatically blocked.
  clock()->Advance(base::Days(8));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_FALSE(result.has_value());

  // Record another dismiss, subsequent requests should be autoblocked again.
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);

  // Accelerate time again, check embargo is lifted and another permission
  // request is let through.
  clock()->Advance(base::Days(8));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_FALSE(result.has_value());

  // Record another dismiss, subsequent requests should be autoblocked again.
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::GEOLOCATION);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);
}

// Tests the alternating pattern of the block on multiple ignores behaviour.
TEST_F(PermissionDecisionAutoBlockerUnitTest, TestIgnoreEmbargoBackoff) {
  GURL url("https://www.google.com");
  clock()->SetNow(base::Time::Now());
  base::HistogramTester histograms;

  // Record some ignores.
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url, ContentSettingsType::MIDI_SYSEX, false));
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url, ContentSettingsType::MIDI_SYSEX, false));

  // A request with < 4 prior ignores should not be automatically blocked.
  absl::optional<content::PermissionResult> result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::MIDI_SYSEX);
  EXPECT_FALSE(result.has_value());

  // After the 4th ignore subsequent permission requests should be autoblocked.
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url, ContentSettingsType::MIDI_SYSEX, false));
  EXPECT_TRUE(autoblocker()->RecordIgnoreAndEmbargo(
      url, ContentSettingsType::MIDI_SYSEX, false));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::MIDI_SYSEX);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_IGNORES, result->source);

  // Accelerate time forward, check that the embargo status is lifted and the
  // request won't be automatically blocked.
  clock()->Advance(base::Days(8));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::MIDI_SYSEX);
  EXPECT_FALSE(result.has_value());

  // Record another dismiss, subsequent requests should be autoblocked again.
  EXPECT_TRUE(autoblocker()->RecordIgnoreAndEmbargo(
      url, ContentSettingsType::MIDI_SYSEX, false));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::MIDI_SYSEX);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_IGNORES, result->source);

  // Accelerate time again, check embargo is lifted and another permission
  // request is let through.
  clock()->Advance(base::Days(8));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::MIDI_SYSEX);
  EXPECT_FALSE(result.has_value());

  // Record another dismiss, subsequent requests should be autoblocked again.
  EXPECT_TRUE(autoblocker()->RecordIgnoreAndEmbargo(
      url, ContentSettingsType::MIDI_SYSEX, false));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::MIDI_SYSEX);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_IGNORES, result->source);
}

// Test that quiet ui embargo has a different threshold for ignores.
TEST_F(PermissionDecisionAutoBlockerUnitTest, TestIgnoreEmbargoUsingQuietUi) {
  GURL url("https://www.google.com");
  clock()->SetNow(base::Time::Now());

  // Check the default state.
  absl::optional<content::PermissionResult> result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::NOTIFICATIONS);
  EXPECT_FALSE(result.has_value());

  // One quiet ui ignore is not enough to trigger embargo.
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url, ContentSettingsType::NOTIFICATIONS, true));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::NOTIFICATIONS);
  EXPECT_FALSE(result.has_value());

  // Loud ui ignores are counted separately.
  EXPECT_FALSE(autoblocker()->RecordIgnoreAndEmbargo(
      url, ContentSettingsType::NOTIFICATIONS, false));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::NOTIFICATIONS);
  EXPECT_FALSE(result.has_value());

  // The second quiet ui ignore puts the url under embargo.
  EXPECT_TRUE(autoblocker()->RecordIgnoreAndEmbargo(
      url, ContentSettingsType::NOTIFICATIONS, true));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::NOTIFICATIONS);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_IGNORES, result->source);
}

// Test that quiet ui embargo has a different threshold for dismisses.
TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoUsingQuietUi) {
  GURL url("https://www.google.com");
  clock()->SetNow(base::Time::Now());

  // Check the default state.
  absl::optional<content::PermissionResult> result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::NOTIFICATIONS);
  EXPECT_FALSE(result.has_value());

  // One loud ui dismiss does not trigger embargo.
  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::NOTIFICATIONS, false));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::NOTIFICATIONS);
  EXPECT_FALSE(result.has_value());

  // One quiet ui dismiss puts the url under embargo.
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::NOTIFICATIONS, true));
  result =
      autoblocker()->GetEmbargoResult(url, ContentSettingsType::NOTIFICATIONS);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);
}

namespace {

// Checks that embargo on federated identity permission is lifted only after the
// passed-in |time_delta| has elapsed.
void CheckFederatedIdentityApiEmbargoLiftedAfterTimeElapsing(
    PermissionDecisionAutoBlocker* autoblocker,
    base::SimpleTestClock* clock,
    const GURL& url,
    base::TimeDelta time_delta) {
  ASSERT_LT(base::Minutes(1), time_delta);

  clock->Advance(time_delta - base::Minutes(1));
  absl::optional<content::PermissionResult> result =
      autoblocker->GetEmbargoResult(
          url, ContentSettingsType::FEDERATED_IDENTITY_API);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::MULTIPLE_DISMISSALS,
            result->source);

  clock->Advance(base::Minutes(2));
  result = autoblocker->GetEmbargoResult(
      url, ContentSettingsType::FEDERATED_IDENTITY_API);
  EXPECT_FALSE(result.has_value());
}

// Checks that embargo on federated identity auto re-authn permission is lifted
// only after the passed-in |time_delta| has elapsed.
void CheckFederatedIdentityAutoReauthnEmbargoLiftedAfterTimeElapsing(
    PermissionDecisionAutoBlocker* autoblocker,
    base::SimpleTestClock* clock,
    const GURL& url,
    base::TimeDelta time_delta) {
  ASSERT_LT(base::Minutes(1), time_delta);

  clock->Advance(time_delta - base::Minutes(1));
  absl::optional<content::PermissionResult> result =
      autoblocker->GetEmbargoResult(
          url, ContentSettingsType::FEDERATED_IDENTITY_AUTO_REAUTHN_PERMISSION);
  EXPECT_EQ(PermissionStatus::DENIED, result->status);
  EXPECT_EQ(content::PermissionStatusSource::RECENT_DISPLAY, result->source);

  clock->Advance(base::Minutes(2));
  result = autoblocker->GetEmbargoResult(
      url, ContentSettingsType::FEDERATED_IDENTITY_AUTO_REAUTHN_PERMISSION);
  EXPECT_FALSE(result.has_value());
}

}  // namespace

TEST_F(PermissionDecisionAutoBlockerUnitTest,
       TestDismissFederatedIdentityApiBackoff) {
  GURL url("https://www.google.com");
  clock()->SetNow(base::Time::Now());

  absl::optional<content::PermissionResult> result =
      autoblocker()->GetEmbargoResult(
          url, ContentSettingsType::FEDERATED_IDENTITY_API);
  EXPECT_FALSE(result.has_value());

  // 2 hour embargo for 1st dismissal
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::FEDERATED_IDENTITY_API, false));
  CheckFederatedIdentityApiEmbargoLiftedAfterTimeElapsing(
      autoblocker(), clock(), url, base::Hours(2));

  // 1 day embargo for 2nd dismissal
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::FEDERATED_IDENTITY_API, false));
  CheckFederatedIdentityApiEmbargoLiftedAfterTimeElapsing(
      autoblocker(), clock(), url, base::Days(1));

  // 7 day embargo for 3rd dismissal
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::FEDERATED_IDENTITY_API, false));
  CheckFederatedIdentityApiEmbargoLiftedAfterTimeElapsing(
      autoblocker(), clock(), url, base::Days(7));

  // 28 day embargo for 4th dismissal (and all additional dismissals)
  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::FEDERATED_IDENTITY_API, false));
  CheckFederatedIdentityApiEmbargoLiftedAfterTimeElapsing(
      autoblocker(), clock(), url, base::Days(28));

  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::FEDERATED_IDENTITY_API, false));
  CheckFederatedIdentityApiEmbargoLiftedAfterTimeElapsing(
      autoblocker(), clock(), url, base::Days(28));

  // Return to 2 hour embargo after
  // PermissionDecisionAutoBlocker::RemoveEmbargoAndResetCounts()
  autoblocker()->RemoveEmbargoAndResetCounts(
      url, ContentSettingsType::FEDERATED_IDENTITY_API);
  result = autoblocker()->GetEmbargoResult(
      url, ContentSettingsType::FEDERATED_IDENTITY_API);
  EXPECT_FALSE(result.has_value());

  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::FEDERATED_IDENTITY_API, false));
  CheckFederatedIdentityApiEmbargoLiftedAfterTimeElapsing(
      autoblocker(), clock(), url, base::Hours(2));
}

TEST_F(PermissionDecisionAutoBlockerUnitTest,
       TestLogoutFederatedIdentityAutoReauthnBackoff) {
  GURL url("https://www.google.com");
  clock()->SetNow(base::Time::Now());

  absl::optional<content::PermissionResult> result =
      autoblocker()->GetEmbargoResult(
          url, ContentSettingsType::FEDERATED_IDENTITY_AUTO_REAUTHN_PERMISSION);
  EXPECT_FALSE(result.has_value());

  // 10 minute embargo
  EXPECT_TRUE(autoblocker()->RecordDisplayAndEmbargo(
      url, ContentSettingsType::FEDERATED_IDENTITY_AUTO_REAUTHN_PERMISSION));
  CheckFederatedIdentityAutoReauthnEmbargoLiftedAfterTimeElapsing(
      autoblocker(), clock(), url, base::Minutes(10));
}

TEST_F(PermissionDecisionAutoBlockerUnitTest,
       ObserverIsNotifiedWhenEmbargoStarts) {
  GURL url("https://www.google.com");
  MockObserver observer;
  autoblocker()->AddObserver(&observer);

  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(0u, observer.GetCallbacks().size());

  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(0u, observer.GetCallbacks().size());

  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(1u, observer.GetCallbacks().size());
  EXPECT_EQ(url, observer.GetCallbacks().begin()->first);
  EXPECT_EQ(ContentSettingsType::GEOLOCATION, observer.GetCallbacks()[url][0]);

  autoblocker()->RemoveObserver(&observer);
}

TEST_F(PermissionDecisionAutoBlockerUnitTest,
       RemovedObserverIsNotNotifiedWhenEmbargoStarts) {
  GURL url("https://www.google.com");
  MockObserver observer;
  autoblocker()->AddObserver(&observer);

  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(0u, observer.GetCallbacks().size());

  EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(0u, observer.GetCallbacks().size());
  autoblocker()->RemoveObserver(&observer);

  EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
      url, ContentSettingsType::GEOLOCATION, false));
  EXPECT_EQ(0u, observer.GetCallbacks().size());
}

}  // namespace permissions