File: identifiability_study_state_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 (1145 lines) | stat: -rw-r--r-- 49,896 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
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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
// 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/privacy_budget/identifiability_study_state.h"
#include <math.h>

#include <cstddef>
#include <cstdint>
#include <memory>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/containers/flat_tree.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/privacy_budget/inspectable_identifiability_study_state.h"
#include "chrome/browser/privacy_budget/privacy_budget_prefs.h"
#include "chrome/common/privacy_budget/field_trial_param_conversions.h"
#include "chrome/common/privacy_budget/privacy_budget_features.h"
#include "chrome/common/privacy_budget/scoped_privacy_budget_config.h"
#include "chrome/common/privacy_budget/types.h"
#include "components/prefs/testing_pref_service.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/privacy_budget/identifiable_surface.h"

namespace {

// Matcher that takes an IdentifiableSurface() as a parameter and checks if the
// argument is the corresponding RepresentativeSurface.
//
// Use as:
//
//     const auto kSurface = blink::IdentifiableSurface::(...);
//     const auto RepresentativeSurface representative_surface(...);
//
//     EXPECT_THAT(representative_surface, Represents(kSurface));
MATCHER_P(Represents, surface, "") {
  return arg.value() == surface;
}

using ::testing::AllOf;
using ::testing::Contains;
using ::testing::ElementsAre;
using ::testing::ElementsAreArray;
using ::testing::IsEmpty;
using ::testing::IsSupersetOf;
using ::testing::Not;
using ::testing::UnorderedElementsAre;

// Constants used to set up the test configuration.
constexpr int kTestingStudyGeneration = 58;

constexpr auto kRegularTypeId =
    blink::IdentifiableSurface::Type::kHTMLMediaElement_CanPlayType;
constexpr auto kBlockedSurface1 =
    blink::IdentifiableSurface::FromTypeAndToken(kRegularTypeId, 1);
constexpr auto kBlockedType1 =
    blink::IdentifiableSurface::Type::kCanvasReadback;
constexpr auto kInternalSurface = blink::IdentifiableSurface::FromTypeAndToken(
    blink::IdentifiableSurface::Type::kReservedInternal,
    3);

constexpr auto kTestingActiveSurfaceBudget = 40u;
constexpr auto kTestingExpectedSurfaceCount = 1u;

// Sample surfaces. "Regular" means that the surface is not blocked and its type
// is not blocked.
constexpr auto kRegularSurface1 =
    blink::IdentifiableSurface::FromTypeAndToken(kRegularTypeId, 3);
constexpr auto kRegularSurface2 =
    blink::IdentifiableSurface::FromTypeAndToken(kRegularTypeId, 4);
constexpr auto kRegularSurface3 =
    blink::IdentifiableSurface::FromTypeAndToken(kRegularTypeId, 5);

constexpr auto kBlockedTypeSurface1 =
    blink::IdentifiableSurface::FromTypeAndToken(kBlockedType1, 1);  // = 258

// Invokes ShouldRecordSurface() on `state` for `surface_count` distinct
// surfaces. All the surfaces belong to the type kRegularTypeId with inputs
// in [0, surface_count).
int SimulateSurfaceSelectionRound(IdentifiabilityStudyState& state,
                                  int surface_count) {
  int selected_surface_count = 0;
  for (int i = 0; i < surface_count; ++i) {
    auto surface =
        blink::IdentifiableSurface::FromTypeAndToken(kRegularTypeId, i);
    if (state.ShouldRecordSurface(surface))
      ++selected_surface_count;
  }
  return selected_surface_count;
}

// Creates a list of distinct IdentifiableSurfaces with type kWebFeature and
// inputs in the range [offset, offset+count).
IdentifiableSurfaceList CreateSurfaceList(size_t count, size_t offset = 0) {
  IdentifiableSurfaceList list;
  list.reserve(count);
  for (auto token = 0u; token < count; ++token) {
    list.push_back(blink::IdentifiableSurface::FromTypeAndToken(
        blink::IdentifiableSurface::Type::kWebFeature, token + offset));
  }
  return list;
}

}  // namespace

class IdentifiabilityStudyStateTest : public ::testing::Test {
 public:
  IdentifiabilityStudyStateTest() {
    config_parameters_.generation = kTestingStudyGeneration;
    config_parameters_.blocked_surfaces.push_back(kBlockedSurface1);
    config_parameters_.blocked_types.push_back(kBlockedType1);
    config_parameters_.active_surface_budget = kTestingActiveSurfaceBudget;
    config_parameters_.expected_surface_count = kTestingExpectedSurfaceCount;
    config_.Apply(config_parameters_);
    prefs::RegisterPrivacyBudgetPrefs(pref_service_.registry());
    pref_service()->SetInteger(prefs::kPrivacyBudgetGeneration,
                               kTestingStudyGeneration);
    scoped_feature_list_.InitAndDisableFeature(
        features::kIdentifiabilityStudyMetaExperiment);
  }

  TestingPrefServiceSimple* pref_service() { return &pref_service_; }

 private:
  TestingPrefServiceSimple pref_service_;
  test::ScopedPrivacyBudgetConfig::Parameters config_parameters_;
  test::ScopedPrivacyBudgetConfig config_;
  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST(IdentifiabilityStudyStateStandaloneTest, InstantiateAndInitialize) {
  test::ScopedPrivacyBudgetConfig config(
      test::ScopedPrivacyBudgetConfig::Presets::kEnableRandomSampling);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());

  // Default is zero.
  ASSERT_EQ(0, pref_service.GetInteger(prefs::kPrivacyBudgetGeneration));
  auto settings = std::make_unique<IdentifiabilityStudyState>(&pref_service);

  // Successful initialization should result in setting the generation number.
  EXPECT_EQ(test::ScopedPrivacyBudgetConfig::kDefaultGeneration,
            pref_service.GetInteger(prefs::kPrivacyBudgetGeneration));
  // There should be at least one offset selected.
  EXPECT_FALSE(
      pref_service.GetString(prefs::kPrivacyBudgetSelectedOffsets).empty());
  // But there should be no seen surfaces.
  EXPECT_TRUE(
      pref_service.GetString(prefs::kPrivacyBudgetSeenSurfaces).empty());
}

TEST_F(IdentifiabilityStudyStateTest, ReInitializesWhenGenerationChanges) {
  pref_service()->SetInteger(prefs::kPrivacyBudgetGeneration,
                             kTestingStudyGeneration - 1);
  pref_service()->SetString(
      prefs::kPrivacyBudgetSeenSurfaces,
      EncodeIdentifiabilityFieldTrialParam(IdentifiableSurfaceList{
          blink::IdentifiableSurface::FromMetricHash(4),
          blink::IdentifiableSurface::FromMetricHash(5),
          blink::IdentifiableSurface::FromMetricHash(6)}));

  auto settings = std::make_unique<IdentifiabilityStudyState>(pref_service());

  EXPECT_EQ(kTestingStudyGeneration,
            pref_service()->GetInteger(prefs::kPrivacyBudgetGeneration));
  EXPECT_THAT(pref_service()->GetString(prefs::kPrivacyBudgetSelectedOffsets),
              Not(IsEmpty()));
  EXPECT_THAT(pref_service()->GetString(prefs::kPrivacyBudgetSeenSurfaces),
              IsEmpty());
}

TEST_F(IdentifiabilityStudyStateTest,
       LoadsSeenSurfacesAndSelectedOffsetsFromPrefs) {
  auto kSurfaces = IdentifiableSurfaceList{kRegularSurface1, kRegularSurface2};
  pref_service()->SetString(prefs::kPrivacyBudgetSeenSurfaces,
                            EncodeIdentifiabilityFieldTrialParam(kSurfaces));

  auto kOffsets = std::vector<IdentifiabilityStudyState::OffsetType>{0, 1};
  pref_service()->SetString(prefs::kPrivacyBudgetSelectedOffsets,
                            EncodeIdentifiabilityFieldTrialParam(kOffsets));
  auto state =
      std::make_unique<test_utils::InspectableIdentifiabilityStudyState>(
          pref_service());
  EXPECT_THAT(state->active_surfaces(),
              UnorderedElementsAre(Represents(kRegularSurface1),
                                   Represents(kRegularSurface2)));
  EXPECT_THAT(state->seen_surfaces().AsList(), ElementsAreArray(kSurfaces));
  EXPECT_THAT(state->selected_offsets(), IsSupersetOf(kOffsets));
  EXPECT_EQ(kTestingStudyGeneration, state->generation());
}

// Valid offsets, as decided by IsValidOffset() go from 0 thru
// kMaxSelectedSurfaceOffset. Encountering invalid ones is a good sign that the
// prefs are inconsistent.
TEST_F(IdentifiabilityStudyStateTest, RecoversFromInvalidOffsets) {
  const std::vector<unsigned int> kBadOffsets = {
      5, 5, 6, 3, IdentifiabilityStudyState::kMaxSelectedSurfaceOffset + 1,
      2, 0, 5};

  pref_service()->SetString(prefs::kPrivacyBudgetSelectedOffsets,
                            EncodeIdentifiabilityFieldTrialParam(kBadOffsets));
  const IdentifiableSurfaceList kSeenSurfaces = {
      kRegularSurface1, kRegularSurface2, kRegularSurface3};
  pref_service()->SetString(
      prefs::kPrivacyBudgetSeenSurfaces,
      EncodeIdentifiabilityFieldTrialParam(kSeenSurfaces));

  auto state =
      std::make_unique<test_utils::InspectableIdentifiabilityStudyState>(
          pref_service());

  // The seen surfaces should've been dumped due to having invalid persisted
  // state.
  EXPECT_TRUE(state->seen_surfaces().empty());

  // Upon re-initialization there should be at least one selected offset to get
  // started with.
  EXPECT_FALSE(state->selected_offsets().empty());
}

// Any blocked surfaces in the seen surface list should be dropped. Selected
// offsets should be adjusted correspondingly.
TEST_F(IdentifiabilityStudyStateTest, DropsBlockedSurfaces_Suffix) {
  // The suffix of the seen surfaces list consists of blocked surfaces.
  pref_service()->SetString(
      prefs::kPrivacyBudgetSeenSurfaces,
      EncodeIdentifiabilityFieldTrialParam(IdentifiableSurfaceList{
          kRegularSurface1, kRegularSurface2, kRegularSurface3,
          kBlockedSurface1, kBlockedTypeSurface1}));
  pref_service()->SetString(prefs::kPrivacyBudgetSelectedOffsets, "0,1,3,4");
  auto settings =
      std::make_unique<test_utils::InspectableIdentifiabilityStudyState>(
          pref_service());
  EXPECT_THAT(settings->selected_offsets(),
              UnorderedElementsAre(IdentifiabilityStudyState::OffsetType{0},
                                   IdentifiabilityStudyState::OffsetType{1}));
  EXPECT_THAT(
      settings->active_surfaces(),
      ElementsAre(Represents(kRegularSurface1), Represents(kRegularSurface2)));
}

// Any blocked surfaces in the seen surface list should be dropped. Selected
// offsets should be adjusted correspondingly.
TEST_F(IdentifiabilityStudyStateTest, DropsBlockedSurfaces_Prefix) {
  // The prefix of the seen surfaces list consists of blocked surfaces.
  pref_service()->SetString(
      prefs::kPrivacyBudgetSeenSurfaces,
      EncodeIdentifiabilityFieldTrialParam(
          IdentifiableSurfaceList{kBlockedSurface1, kRegularSurface1,
                                  kRegularSurface2, kRegularSurface3}));
  pref_service()->SetString(prefs::kPrivacyBudgetSelectedOffsets, "1,3");
  auto settings =
      std::make_unique<test_utils::InspectableIdentifiabilityStudyState>(
          pref_service());
  EXPECT_THAT(settings->selected_offsets(), UnorderedElementsAre(0, 2));
  EXPECT_THAT(
      settings->active_surfaces(),
      ElementsAre(Represents(kRegularSurface1), Represents(kRegularSurface3)));
}

// Any blocked surfaces in the seen surface list should be dropped. Selected
// offsets should be adjusted correspondingly.
TEST_F(IdentifiabilityStudyStateTest, DropsBlockedSurfaces_Infix) {
  pref_service()->SetString(
      prefs::kPrivacyBudgetSeenSurfaces,
      EncodeIdentifiabilityFieldTrialParam(
          IdentifiableSurfaceList{kRegularSurface1, kBlockedSurface1,
                                  kRegularSurface2, kRegularSurface3}));
  pref_service()->SetString(prefs::kPrivacyBudgetSelectedOffsets,
                            "0,1,2,3,4,5");
  auto settings =
      std::make_unique<test_utils::InspectableIdentifiabilityStudyState>(
          pref_service());
  EXPECT_THAT(settings->selected_offsets(),
              UnorderedElementsAre(0, 1, 2, 3, 4));
  EXPECT_THAT(
      settings->active_surfaces(),
      ElementsAre(Represents(kRegularSurface1), Represents(kRegularSurface2),
                  Represents(kRegularSurface3)));
}

TEST_F(IdentifiabilityStudyStateTest, DropsBlockedTypes) {
  pref_service()->SetString(
      prefs::kPrivacyBudgetSeenSurfaces,
      EncodeIdentifiabilityFieldTrialParam(
          IdentifiableSurfaceList{kRegularSurface1, kBlockedTypeSurface1,
                                  kRegularSurface2, kRegularSurface3}));
  pref_service()->SetString(prefs::kPrivacyBudgetSelectedOffsets,
                            "0,1,2,3,4,5");
  auto settings =
      std::make_unique<test_utils::InspectableIdentifiabilityStudyState>(
          pref_service());
  EXPECT_THAT(
      settings->active_surfaces(),
      ElementsAre(Represents(kRegularSurface1), Represents(kRegularSurface2),
                  Represents(kRegularSurface3)));
  EXPECT_THAT(settings->selected_offsets(),
              AllOf(Contains(0u), Contains(1u), Contains(2u), Contains(3u),
                    Contains(4u)));
}

TEST_F(IdentifiabilityStudyStateTest, AllowsValidPersistedSurfaces) {
  pref_service()->SetString(
      prefs::kPrivacyBudgetSeenSurfaces,
      EncodeIdentifiabilityFieldTrialParam(IdentifiableSurfaceList{
          kRegularSurface1, kRegularSurface2, kRegularSurface3}));
  pref_service()->SetString(prefs::kPrivacyBudgetSelectedOffsets,
                            "0,1,2,3,4,5");
  auto settings =
      std::make_unique<test_utils::InspectableIdentifiabilityStudyState>(
          pref_service());
  EXPECT_TRUE(settings->ShouldRecordSurface(kRegularSurface1));
  EXPECT_TRUE(settings->ShouldRecordSurface(kRegularSurface2));
  EXPECT_TRUE(settings->ShouldRecordSurface(kRegularSurface3));
  EXPECT_THAT(settings->active_surfaces(),
              ::testing::ElementsAre(Represents(kRegularSurface1),
                                     Represents(kRegularSurface2),
                                     Represents(kRegularSurface3)));
}

// Different from the DropsBlockedSurfaces* tests in that the behavior being
// verified is that of ShouldRecordSurface().
TEST_F(IdentifiabilityStudyStateTest, DisallowsBlockedSurfaces) {
  pref_service()->SetString(
      prefs::kPrivacyBudgetSeenSurfaces,
      EncodeIdentifiabilityFieldTrialParam(IdentifiableSurfaceList{
          kRegularSurface1, kRegularSurface2, kRegularSurface3}));
  pref_service()->SetString(prefs::kPrivacyBudgetSelectedOffsets,
                            "0,1,2,3,4,5");
  auto settings =
      std::make_unique<test_utils::InspectableIdentifiabilityStudyState>(
          pref_service());
  EXPECT_FALSE(settings->ShouldRecordSurface(kBlockedSurface1));
  EXPECT_FALSE(settings->ShouldRecordSurface(kBlockedTypeSurface1));
}

TEST_F(IdentifiabilityStudyStateTest, UpdatesSeenSurfaces) {
  // The list of seen surfaces should be updated when a new surface was
  // observed.
  auto settings =
      std::make_unique<test_utils::InspectableIdentifiabilityStudyState>(
          pref_service());
  settings->SelectAllOffsetsForTesting();
  EXPECT_TRUE(settings->active_surfaces().Empty());
  EXPECT_THAT(settings->seen_surfaces(), IsEmpty());
  EXPECT_TRUE(settings->ShouldRecordSurface(kRegularSurface1));
  EXPECT_THAT(settings->active_surfaces().Container(),
              ElementsAre(Represents(kRegularSurface1)));
  EXPECT_THAT(settings->seen_surfaces().AsList(),
              ElementsAre(kRegularSurface1));
  EXPECT_EQ(pref_service()->GetString(prefs::kPrivacyBudgetSeenSurfaces),
            std::string("779"));
}

// If there are duplicate `seen` surfaces, then IdentifiabilityStudyState
// should drop the existing persisted state and re-initialize the study. An
// inconsistency in the persisted state should be considered irrecoverable.
TEST_F(IdentifiabilityStudyStateTest, DropsInvalidPrefs_DuplicateSurfaces) {
  pref_service()->SetString(
      prefs::kPrivacyBudgetSeenSurfaces,
      EncodeIdentifiabilityFieldTrialParam(
          IdentifiableSurfaceList{kRegularSurface1, kRegularSurface1}));

  auto settings =
      std::make_unique<test_utils::InspectableIdentifiabilityStudyState>(
          pref_service());
  EXPECT_TRUE(settings->seen_surfaces().empty());
}

// If there are `seen` surfaces that are internal metadata types, then
// IdentifiabilityStudyState should drop the existing persisted state and
// re-initialize the study. An inconsistency in the persisted state should be
// considered irrecoverable.
TEST_F(IdentifiabilityStudyStateTest, DropsInvalidPrefs_InternalTypes) {
  pref_service()->SetString(
      prefs::kPrivacyBudgetSeenSurfaces,
      EncodeIdentifiabilityFieldTrialParam(
          IdentifiableSurfaceList{blink::IdentifiableSurface::FromTypeAndToken(
              blink::IdentifiableSurface::Type::kReservedInternal, 1)}));

  auto settings =
      std::make_unique<test_utils::InspectableIdentifiabilityStudyState>(
          pref_service());
  EXPECT_TRUE(settings->seen_surfaces().empty());
}

// If there's not enough offsets selected, IdentifiabilityStudyState should
// select more. If any of the newly selected offsets are within the range of
// the seen surfaces, those seen surfaces should be automatically promoted to
// active surfaces.
TEST(IdentifiabilityStudyStateStandaloneTest,
     OffsetSelectionPromotesSeenToActiveSurfaces) {
  auto params = test::ScopedPrivacyBudgetConfig::Parameters{};
  params.active_surface_budget = 40u;
  params.expected_surface_count = params.active_surface_budget * 2;
  test::ScopedPrivacyBudgetConfig config(params);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());

  IdentifiableSurfaceList seen_surfaces;
  constexpr unsigned kTargetOffsetCount =
      test_utils::InspectableIdentifiabilityStudyState::
          kMaxSelectedSurfaceOffset +
      1;
  for (uint64_t i = 0; i < kTargetOffsetCount; ++i) {
    seen_surfaces.push_back(blink::IdentifiableSurface::FromTypeAndToken(
        blink::IdentifiableSurface::Type::kCanvasReadback, i));
  }
  pref_service.SetString(prefs::kPrivacyBudgetSeenSurfaces,
                         EncodeIdentifiabilityFieldTrialParam(seen_surfaces));
  pref_service.SetInteger(prefs::kPrivacyBudgetGeneration, params.generation);

  auto settings =
      std::make_unique<test_utils::InspectableIdentifiabilityStudyState>(
          &pref_service);

  // The default costing model counts each surface as a single unit. So
  // params.active_surface_budget is also the maximum number of surfaces that
  // could be selected.
  EXPECT_EQ(static_cast<unsigned>(params.active_surface_budget),
            settings->active_surfaces().Size());
  EXPECT_EQ(kTargetOffsetCount, settings->seen_surfaces().size());
  EXPECT_EQ(static_cast<unsigned>(params.active_surface_budget),
            settings->selected_offsets().size());
}

// Verify that the study parameters don't overflow.
TEST(IdentifiabilityStudyStateStandaloneTest, HighClamps) {
  auto params = test::ScopedPrivacyBudgetConfig::Parameters{};
  params.active_surface_budget =
      features::kMaxIdentifiabilityStudyActiveSurfaceBudget + 1;
  params.expected_surface_count =
      features::kMaxIdentifiabilityStudyExpectedSurfaceCount + 1;
  test::ScopedPrivacyBudgetConfig config(params);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState settings(&pref_service);

  EXPECT_EQ(features::kMaxIdentifiabilityStudyActiveSurfaceBudget,
            settings.active_surface_budget());
}

// Verify that the study parameters don't underflow.
TEST(IdentifiabilityStudyStateStandaloneTest, LowClamps) {
  auto params = test::ScopedPrivacyBudgetConfig::Parameters{};
  params.active_surface_budget = -1;
  params.expected_surface_count = -1;
  test::ScopedPrivacyBudgetConfig config(params);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState settings(&pref_service);

  EXPECT_EQ(0, settings.active_surface_budget());
}

TEST(IdentifiabilityStudyStateStandaloneTest, Disabled) {
  test::ScopedPrivacyBudgetConfig config(
      test::ScopedPrivacyBudgetConfig::Presets::kDisable);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState settings(&pref_service);

  // The specific surface doesn't matter.
  EXPECT_FALSE(settings.ShouldRecordSurface(kRegularSurface1));
}

TEST(IdentifiabilityStudyStateStandaloneTest, ShouldReportEncounteredSurface) {
  test::ScopedPrivacyBudgetConfig config(
      test::ScopedPrivacyBudgetConfig::Presets::kEnableRandomSampling);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState state(&pref_service);

  EXPECT_TRUE(state.group_settings().enabled());
  EXPECT_TRUE(state.group_settings().IsUsingRandomSampling());

  // The specific surface doesn't matter.
  EXPECT_TRUE(state.ShouldReportEncounteredSurface(ukm::AssignNewSourceId(),
                                                   kRegularSurface1));
  EXPECT_TRUE(state.ShouldReportEncounteredSurface(ukm::NoURLSourceId(),
                                                   kRegularSurface1));
}

// Test the mode in which only the meta experiment (i.e. reporting encountered
// surfaces) is enabled.
TEST(IdentifiabilityStudyStateStandaloneTest, OnlyReportEncounteredSurface) {
  test::ScopedPrivacyBudgetConfig::Parameters params(
      test::ScopedPrivacyBudgetConfig::Presets::kEnableRandomSampling);
  params.allowed_random_types = {
      blink::IdentifiableSurface::Type::kReservedInternal};
  test::ScopedPrivacyBudgetConfig config(params);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState state(&pref_service);

  EXPECT_TRUE(state.group_settings().enabled());
  EXPECT_TRUE(state.group_settings().IsUsingRandomSampling());

  // The specific surface doesn't matter.
  EXPECT_TRUE(state.ShouldReportEncounteredSurface(ukm::AssignNewSourceId(),
                                                   kRegularSurface1));
  EXPECT_FALSE(state.ShouldRecordSurface(kRegularSurface1));
}

TEST(IdentifiabilityStudyStateStandaloneTest, ClearsPrefsIfStudyIsDisabled) {
  test::ScopedPrivacyBudgetConfig config(
      test::ScopedPrivacyBudgetConfig::Presets::kDisable);
  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  pref_service.SetInteger(prefs::kPrivacyBudgetGeneration,
                          test::ScopedPrivacyBudgetConfig::kDefaultGeneration);
  pref_service.SetString(prefs::kPrivacyBudgetSeenSurfaces, "1,2,3");
  pref_service.SetString(prefs::kPrivacyBudgetSelectedOffsets, "100,200,300");

  IdentifiabilityStudyState state(&pref_service);

  EXPECT_EQ(0, pref_service.GetInteger(prefs::kPrivacyBudgetGeneration));
  EXPECT_THAT(pref_service.GetString(prefs::kPrivacyBudgetSeenSurfaces),
              IsEmpty());
  EXPECT_THAT(pref_service.GetString(prefs::kPrivacyBudgetSelectedOffsets),
              IsEmpty());
}

TEST_F(IdentifiabilityStudyStateTest, StripDisallowedSurfaces_Empty) {
  test_utils::InspectableIdentifiabilityStudyState state(pref_service());
  std::vector<IdentifiabilityStudyState::OffsetType> dropped;
  IdentifiableSurfaceList surfaces;
  EXPECT_TRUE(
      test_utils::InspectableIdentifiabilityStudyState::StripDisallowedSurfaces(
          surfaces, dropped));
  EXPECT_THAT(dropped, IsEmpty());
  EXPECT_THAT(surfaces, IsEmpty());
}

TEST_F(IdentifiabilityStudyStateTest, StripDisallowedSurfaces_SingleBad) {
  test_utils::InspectableIdentifiabilityStudyState state(pref_service());
  std::vector<IdentifiabilityStudyState::OffsetType> dropped;
  IdentifiableSurfaceList surfaces{kBlockedSurface1, kRegularSurface1};
  EXPECT_TRUE(
      test_utils::InspectableIdentifiabilityStudyState::StripDisallowedSurfaces(
          surfaces, dropped));
  EXPECT_THAT(dropped, ElementsAre(0));
  EXPECT_THAT(surfaces, ElementsAre(kRegularSurface1));
}

TEST_F(IdentifiabilityStudyStateTest, StripDisallowedSurfaces_BadSuffix) {
  test_utils::InspectableIdentifiabilityStudyState state(pref_service());
  std::vector<IdentifiabilityStudyState::OffsetType> dropped;
  IdentifiableSurfaceList surfaces{kRegularSurface1, kBlockedSurface1};
  EXPECT_TRUE(
      test_utils::InspectableIdentifiabilityStudyState::StripDisallowedSurfaces(
          surfaces, dropped));
  EXPECT_THAT(dropped, ElementsAre(1));
  EXPECT_THAT(surfaces, ElementsAre(kRegularSurface1));
}

TEST_F(IdentifiabilityStudyStateTest, StripDisallowedSurfaces_Duplicates) {
  test_utils::InspectableIdentifiabilityStudyState state(pref_service());
  std::vector<IdentifiabilityStudyState::OffsetType> dropped;
  IdentifiableSurfaceList surfaces{kRegularSurface1, kRegularSurface1};
  EXPECT_FALSE(
      test_utils::InspectableIdentifiabilityStudyState::StripDisallowedSurfaces(
          surfaces, dropped));
}

TEST_F(IdentifiabilityStudyStateTest, StripDisallowedSurfaces_InternalSurface) {
  test_utils::InspectableIdentifiabilityStudyState state(pref_service());
  std::vector<IdentifiabilityStudyState::OffsetType> dropped;
  IdentifiableSurfaceList surfaces{
      kRegularSurface1,
      blink::IdentifiableSurface::FromTypeAndToken(
          blink::IdentifiableSurface::Type::kReservedInternal, 1)};
  EXPECT_FALSE(
      test_utils::InspectableIdentifiabilityStudyState::StripDisallowedSurfaces(
          surfaces, dropped));
}

TEST(IdentifiabilityStudyStateStandaloneTest, AdjustForDroppedOffsets_Empty) {
  std::vector<IdentifiabilityStudyState::OffsetType> dropped;
  std::vector<IdentifiabilityStudyState::OffsetType> original;
  auto result =
      test_utils::InspectableIdentifiabilityStudyState::AdjustForDroppedOffsets(
          dropped, original);
  EXPECT_THAT(result, IsEmpty());
}

TEST(IdentifiabilityStudyStateStandaloneTest,
     AdjustForDroppedOffsets_NoDropped) {
  std::vector<IdentifiabilityStudyState::OffsetType> dropped;
  std::vector<IdentifiabilityStudyState::OffsetType> original{1, 5, 7, 9};
  auto result =
      test_utils::InspectableIdentifiabilityStudyState::AdjustForDroppedOffsets(
          dropped, original);
  EXPECT_THAT(result, ElementsAreArray(original));
}

TEST(IdentifiabilityStudyStateStandaloneTest,
     AdjustForDroppedOffsets_DroppedZero) {
  std::vector<IdentifiabilityStudyState::OffsetType> dropped{0};
  std::vector<IdentifiabilityStudyState::OffsetType> original{1, 5, 7, 9};
  auto result =
      test_utils::InspectableIdentifiabilityStudyState::AdjustForDroppedOffsets(
          dropped, original);
  // All offsets in `original` should be decreased by 1.
  EXPECT_THAT(result, ElementsAre(0, 4, 6, 8));
}

TEST(IdentifiabilityStudyStateStandaloneTest,
     AdjustForDroppedOffsets_DroppedOne) {
  std::vector<IdentifiabilityStudyState::OffsetType> dropped{1};
  std::vector<IdentifiabilityStudyState::OffsetType> original{1, 5, 7, 9};
  auto result =
      test_utils::InspectableIdentifiabilityStudyState::AdjustForDroppedOffsets(
          dropped, original);
  EXPECT_THAT(result, ElementsAre(4, 6, 8));
}

TEST(IdentifiabilityStudyStateStandaloneTest,
     AdjustForDroppedOffsets_DroppedMultiple) {
  std::vector<IdentifiabilityStudyState::OffsetType> dropped{2, 3, 5};
  std::vector<IdentifiabilityStudyState::OffsetType> original{1, 2, 4, 6};
  auto result =
      test_utils::InspectableIdentifiabilityStudyState::AdjustForDroppedOffsets(
          dropped, original);
  EXPECT_THAT(result, ElementsAre(1, 2, 3));
}

TEST(IdentifiabilityStudyStateStandaloneTest, OffsetUsesUpAllTheBudget) {
  test::ScopedPrivacyBudgetConfig::Parameters parameters;
  parameters.expected_surface_count = kTestingExpectedSurfaceCount;
  parameters.active_surface_budget = kTestingActiveSurfaceBudget;
  // kRegularSurface2 costs the entire budget.
  parameters.per_surface_cost = {
      {kRegularSurface1, 1.0},
      {kRegularSurface2, parameters.active_surface_budget - 1},
      {kRegularSurface3, 1.0}};
  test::ScopedPrivacyBudgetConfig config(parameters);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  pref_service.SetInteger(prefs::kPrivacyBudgetGeneration,
                          parameters.generation);
  pref_service.SetString(prefs::kPrivacyBudgetSelectedOffsets, "0,1,2");

  test_utils::InspectableIdentifiabilityStudyState state(&pref_service);

  EXPECT_TRUE(state.ShouldRecordSurface(kRegularSurface1));
  EXPECT_TRUE(state.ShouldRecordSurface(kRegularSurface2));
  EXPECT_FALSE(state.ShouldRecordSurface(kRegularSurface3));

  // kRegularSurface3 doesn't end up in the seen_surface_sequence because we've
  // saturated the active surface budget and are no longer interested in new
  // surfaces.
  EXPECT_THAT(state.seen_surfaces(),
              ElementsAre(kRegularSurface1, kRegularSurface2));
}

TEST(IdentifiabilityStudyStateStandaloneTest, NextOffsetIsTooExpensive) {
  test::ScopedPrivacyBudgetConfig::Parameters parameters;
  parameters.expected_surface_count = kTestingExpectedSurfaceCount;
  parameters.active_surface_budget = kTestingActiveSurfaceBudget;
  // kRegularSurface1 costs the entire budget.
  parameters.per_surface_cost = {
      {kRegularSurface1, parameters.active_surface_budget + 1}};
  test::ScopedPrivacyBudgetConfig config(parameters);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  pref_service.SetInteger(prefs::kPrivacyBudgetGeneration,
                          parameters.generation);
  pref_service.SetString(prefs::kPrivacyBudgetSelectedOffsets, "0");

  test_utils::InspectableIdentifiabilityStudyState state(&pref_service);

  // Selected offset `0` is from prefs.
  EXPECT_THAT(state.selected_offsets(), Contains(0));

  // This surface is too expensive, which should result in `state` dropping the
  // corresponding offset.
  EXPECT_FALSE(state.ShouldRecordSurface(kRegularSurface1));
  EXPECT_THAT(state.selected_offsets(), Not(Contains(0)));
  EXPECT_THAT(state.seen_surfaces(), ElementsAre(kRegularSurface1));
}

// TODO(crbug.com/40888212): Flaky on all platforms
TEST(IdentifiabilityStudyStateStandaloneTest, DISABLED_ReachesPivotPoint) {
  test::ScopedPrivacyBudgetConfig::Parameters parameters;
  parameters.active_surface_budget = kTestingActiveSurfaceBudget;

  // Try out 10x the number of surfaces we want to select.
  constexpr auto kExpectedSurfaceCount = kTestingActiveSurfaceBudget * 10;
  parameters.expected_surface_count = kExpectedSurfaceCount;
  test::ScopedPrivacyBudgetConfig config(parameters);

  int active_surface_count = 0;

  constexpr int kTrials = 10;

  for (auto trials = 0; trials < kTrials; ++trials) {
    TestingPrefServiceSimple pref_service;
    prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
    test_utils::InspectableIdentifiabilityStudyState state(&pref_service);
    active_surface_count +=
        SimulateSurfaceSelectionRound(state, kExpectedSurfaceCount);
  }

  // If we see `expected_surface_count` many surfaces, then we *should* consume
  // roughly `active_surface_budget` * `kMesaDistributionRatio` surfaces. (the
  // budget that's allocated for the ordinal space between 0 and the pivot
  // point.)
  constexpr double kExpectedActiveSurfaceCount =
      kTestingActiveSurfaceBudget *
      IdentifiabilityStudyState::kMesaDistributionRatio;

  const double kEpsilon =
      kExpectedActiveSurfaceCount * 0.05 /* 5% margin of error */;

  EXPECT_NEAR(kExpectedActiveSurfaceCount, active_surface_count * 1.0 / kTrials,
              kEpsilon);
}

TEST(IdentifiabilityStudyStateStandaloneTest, CheapSurfaces) {
  // For this test, we are going to use surfaces that cost
  // 1/kSurfacesPerCostUnit units. The objective is to ensure that the code
  // correctly handles the case where the number of surfaces that saturate the
  // budget is substantially larger than the budget itself.
  constexpr auto kSurfacesPerCostUnit = 4;

  // The number of surfaces that are selected must not exceed this value.
  constexpr auto kMaxSelectedSurfaces =
      kTestingActiveSurfaceBudget * kSurfacesPerCostUnit + 1 -
      kSurfacesPerCostUnit;

  // Expect a substantially larger number of surfaces than the number we expect
  // to select.
  constexpr auto kExpectedSurfaceCount = kMaxSelectedSurfaces * 10;

  test::ScopedPrivacyBudgetConfig::Parameters parameters;
  parameters.active_surface_budget = kTestingActiveSurfaceBudget;
  parameters.expected_surface_count = kExpectedSurfaceCount;
  parameters.per_type_cost = {
      {kRegularTypeId, 1.0 / kSurfacesPerCostUnit},
  };
  test::ScopedPrivacyBudgetConfig config(parameters);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState state(&pref_service);
  // Invoking ShouldRecordSurface() for kExpectedSurfaceCount surfaces should
  // result in at least
  unsigned selected_surfaces =
      SimulateSurfaceSelectionRound(state, kExpectedSurfaceCount);
  ASSERT_LE(selected_surfaces, kMaxSelectedSurfaces);

  EXPECT_GT(selected_surfaces, kTestingActiveSurfaceBudget);
  EXPECT_LE(selected_surfaces, kMaxSelectedSurfaces);
}

TEST(IdentifiabilityStudyStateStandaloneTest,
     AlwaysSampleReservedInternalRandom) {
  test::ScopedPrivacyBudgetConfig::Parameters parameters;
  parameters.active_surface_budget = kTestingActiveSurfaceBudget;
  parameters.expected_surface_count = 20;
  parameters.allowed_random_types = {
      blink::IdentifiableSurface::Type::kWebFeature};
  test::ScopedPrivacyBudgetConfig config(parameters);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState state(&pref_service);

  EXPECT_TRUE(
      state.ShouldRecordSurface(blink::IdentifiableSurface::FromTypeAndToken(
          blink::IdentifiableSurface::Type::kReservedInternal,
          blink::IdentifiableToken(
              blink::IdentifiableSurface::ReservedSurfaceMetrics::
                  kDocumentCreated_IsCrossOriginFrame))));
  EXPECT_TRUE(
      state.ShouldRecordSurface(blink::IdentifiableSurface::FromTypeAndToken(
          blink::IdentifiableSurface::Type::kReservedInternal,
          blink::IdentifiableToken(
              blink::IdentifiableSurface::ReservedSurfaceMetrics::
                  kDocumentCreated_IsCrossSiteFrame))));
  EXPECT_TRUE(
      state.ShouldRecordSurface(blink::IdentifiableSurface::FromTypeAndToken(
          blink::IdentifiableSurface::Type::kReservedInternal,
          blink::IdentifiableToken(
              blink::IdentifiableSurface::ReservedSurfaceMetrics::
                  kDocumentCreated_IsMainFrame))));
  EXPECT_TRUE(
      state.ShouldRecordSurface(blink::IdentifiableSurface::FromTypeAndToken(
          blink::IdentifiableSurface::Type::kReservedInternal,
          blink::IdentifiableToken(
              blink::IdentifiableSurface::ReservedSurfaceMetrics::
                  kDocumentCreated_NavigationSourceId))));
}

TEST(IdentifiabilityStudyStateStandaloneTest,
     AlwaysSampleReservedInternalBlock) {
  const int kTestGroupCount = 80;
  const int kSurfacesInGroup = 40;

  test::ScopedPrivacyBudgetConfig::Parameters parameters;
  for (int group_index = 0; group_index < kTestGroupCount; ++group_index) {
    parameters.blocks.emplace_back(
        CreateSurfaceList(kSurfacesInGroup, kSurfacesInGroup * group_index));
  }

  parameters.block_weights.assign(kTestGroupCount, 1.0);
  parameters.active_surface_budget = kSurfacesInGroup;
  test::ScopedPrivacyBudgetConfig config(parameters);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState state(&pref_service);

  EXPECT_TRUE(
      state.ShouldRecordSurface(blink::IdentifiableSurface::FromTypeAndToken(
          blink::IdentifiableSurface::Type::kReservedInternal,
          blink::IdentifiableToken(
              blink::IdentifiableSurface::ReservedSurfaceMetrics::
                  kDocumentCreated_IsCrossOriginFrame))));
  EXPECT_TRUE(
      state.ShouldRecordSurface(blink::IdentifiableSurface::FromTypeAndToken(
          blink::IdentifiableSurface::Type::kReservedInternal,
          blink::IdentifiableToken(
              blink::IdentifiableSurface::ReservedSurfaceMetrics::
                  kDocumentCreated_IsCrossSiteFrame))));
  EXPECT_TRUE(
      state.ShouldRecordSurface(blink::IdentifiableSurface::FromTypeAndToken(
          blink::IdentifiableSurface::Type::kReservedInternal,
          blink::IdentifiableToken(
              blink::IdentifiableSurface::ReservedSurfaceMetrics::
                  kDocumentCreated_IsMainFrame))));
  EXPECT_TRUE(
      state.ShouldRecordSurface(blink::IdentifiableSurface::FromTypeAndToken(
          blink::IdentifiableSurface::Type::kReservedInternal,
          blink::IdentifiableToken(
              blink::IdentifiableSurface::ReservedSurfaceMetrics::
                  kDocumentCreated_NavigationSourceId))));
}

TEST(IdentifiabilityStudyStateStandaloneTest, NoAllowedTypes) {
  constexpr auto kExpectedSurfaceCount = 20;

  test::ScopedPrivacyBudgetConfig::Parameters parameters;
  parameters.active_surface_budget = kTestingActiveSurfaceBudget;
  parameters.expected_surface_count = kExpectedSurfaceCount;
  parameters.allowed_random_types = {
      blink::IdentifiableSurface::Type::kWebFeature};
  test::ScopedPrivacyBudgetConfig config(parameters);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState state(&pref_service);
  // Invoking ShouldRecordSurface() for kExpectedSurfaceCount surfaces should
  // result in at least
  unsigned selected_surfaces =
      SimulateSurfaceSelectionRound(state, kExpectedSurfaceCount);
  // We only allow kWebFeature, but SimulateSurfaceSelectionRound is trying to
  // add kGenericFontLookup surfaces
  const unsigned int expected_surfaces = 0;
  EXPECT_EQ(selected_surfaces, expected_surfaces);
}

TEST(IdentifiabilityStudyStateStandaloneTest, OnlyAllowedTypes) {
  constexpr auto kExpectedSurfaceCount = 20;

  test::ScopedPrivacyBudgetConfig::Parameters parameters;
  parameters.active_surface_budget = kTestingActiveSurfaceBudget;
  parameters.expected_surface_count = kExpectedSurfaceCount;
  parameters.allowed_random_types = {
      blink::IdentifiableSurface::Type::kHTMLMediaElement_CanPlayType,
      blink::IdentifiableSurface::Type::kWebFeature};
  test::ScopedPrivacyBudgetConfig config(parameters);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState state(&pref_service);
  // Invoking ShouldRecordSurface() for kExpectedSurfaceCount surfaces should
  // result in at least
  unsigned selected_surfaces =
      SimulateSurfaceSelectionRound(state, kExpectedSurfaceCount);
  // We allow kWebFeature and kGenericFontLookup types, and
  // SimulateSurfaceSelectionRound is trying to add kGenericFontLookup surfaces
  // so we should sample at least one surface
  const unsigned int min_expected_surfaces = 1;
  EXPECT_GT(selected_surfaces, min_expected_surfaces);
}

TEST(IdentifiabilityStudyStateStandaloneTest, SomeSetOfGroups) {
  // Number of test groups. Arbitrary.
  constexpr unsigned kTestGroupCount = 80;

  // Number of surfaces in a single group. The groups don't need to be the same
  // size.
  constexpr unsigned kSurfacesInGroup = 40;

  test::ScopedPrivacyBudgetConfig::Parameters parameters;
  for (unsigned group_index = 0; group_index < kTestGroupCount; ++group_index) {
    parameters.blocks.emplace_back(
        CreateSurfaceList(kSurfacesInGroup, kSurfacesInGroup * group_index));
  }
  parameters.block_weights.assign(kTestGroupCount, 1.0);
  parameters.active_surface_budget = kSurfacesInGroup;
  test::ScopedPrivacyBudgetConfig config(parameters);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState state(&pref_service);

  // Any single selected group contributes kSurfacesInGroup surfaces.
  EXPECT_EQ(kSurfacesInGroup, state.active_surfaces().Size());
  EXPECT_EQ(0u, state.seen_surfaces().size());
}

// Regression test for the problem solved by https://crrev.com/c/3211286
TEST(IdentifiabilityStudyStateStandaloneTest,
     SomeSetOfGroupsWithRhoEqualToZero) {
  // Number of test groups. Arbitrary.
  constexpr unsigned kTestGroupCount = 80;

  // Number of surfaces in a single group. The groups don't need to be the same
  // size.
  constexpr unsigned kSurfacesInGroup = 40;

  test::ScopedPrivacyBudgetConfig::Parameters parameters;
  for (unsigned group_index = 0; group_index < kTestGroupCount; ++group_index) {
    parameters.blocks.emplace_back(
        CreateSurfaceList(kSurfacesInGroup, kSurfacesInGroup * group_index));
  }
  parameters.block_weights.assign(kTestGroupCount, 1.0);
  parameters.active_surface_budget = kSurfacesInGroup;
  parameters.expected_surface_count = 0;
  test::ScopedPrivacyBudgetConfig config(parameters);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState state(&pref_service);

  EXPECT_TRUE(state.group_settings().IsUsingAssignedBlockSampling());

  // Any single selected group contributes kSurfacesInGroup surfaces.
  EXPECT_EQ(kSurfacesInGroup, state.active_surfaces().Size());
}

TEST(IdentifiabilityStudyStateStandaloneTest, GroupsWithWeights) {
  constexpr unsigned kTestGroupCount = 80;
  constexpr unsigned kSurfacesInGroup = 40;

  test::ScopedPrivacyBudgetConfig::Parameters parameters;
  for (unsigned group_index = 0; group_index < kTestGroupCount; ++group_index) {
    parameters.blocks.emplace_back(
        CreateSurfaceList(kSurfacesInGroup, kSurfacesInGroup * group_index));
  }

  parameters.block_weights.assign(kTestGroupCount, 1.0);
  parameters.active_surface_budget = kSurfacesInGroup;
  test::ScopedPrivacyBudgetConfig config(parameters);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState state(&pref_service);

  int surfaces_to_record = 0;
  for (const IdentifiableSurfaceList& block : parameters.blocks) {
    if (state.ShouldRecordSurface(block[0]))
      surfaces_to_record++;
  }
  // Should record surfaces from exactly one group.
  EXPECT_EQ(1, surfaces_to_record);
}

TEST(IdentifiabilityStudyStateStandaloneTest, GroupSelectionPersists) {
  constexpr unsigned kTestGroupCount = 80;
  constexpr unsigned kSurfacesInGroup = 40;

  test::ScopedPrivacyBudgetConfig::Parameters parameters;
  for (unsigned group_index = 0; group_index < kTestGroupCount; ++group_index) {
    parameters.blocks.emplace_back(
        CreateSurfaceList(kSurfacesInGroup, kSurfacesInGroup * group_index));
  }
  parameters.block_weights.assign(kTestGroupCount, 1.0);
  parameters.active_surface_budget = kSurfacesInGroup;

  // Runs two scoped test. The selection from the first one should persist to
  // the next.
  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());

  int selected_block_offset = -1;

  {
    test::ScopedPrivacyBudgetConfig config(parameters);
    test_utils::InspectableIdentifiabilityStudyState state(&pref_service);
    selected_block_offset = state.selected_block_offset();
    ASSERT_GE(selected_block_offset, 0);
    ASSERT_LT(static_cast<unsigned>(selected_block_offset),
              parameters.blocks.size());
    EXPECT_TRUE(
        state.ShouldRecordSurface(parameters.blocks[selected_block_offset][0]));
  }

  {
    test::ScopedPrivacyBudgetConfig config(parameters);
    test_utils::InspectableIdentifiabilityStudyState state(&pref_service);
    EXPECT_TRUE(
        state.ShouldRecordSurface(parameters.blocks[selected_block_offset][0]));
  }
}

TEST(IdentifiabilityStudyStateStandaloneTest, GroupOverflowsExperimentBudget) {
  constexpr unsigned kSurfacesInGroup = 40;
  // One unit short of accommodating the surface set.
  constexpr unsigned kActiveSurfaceBudget = kSurfacesInGroup - 1;

  test::ScopedPrivacyBudgetConfig::Parameters parameters;
  parameters.blocks.emplace_back(CreateSurfaceList(kSurfacesInGroup));
  parameters.active_surface_budget = kActiveSurfaceBudget;
  test::ScopedPrivacyBudgetConfig config(parameters);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState state(&pref_service);

  // Experiment is active, but there are no active surfaces.
  EXPECT_TRUE(state.active_surfaces().Empty());
}

TEST_F(IdentifiabilityStudyStateTest, WithAlsoMetaExperimentEnabled) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      features::kIdentifiabilityStudyMetaExperiment,
      {{features::kIdentifiabilityStudyMetaExperimentActivationProbability.name,
        "1"}});

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState settings(&pref_service);

  EXPECT_TRUE(settings.meta_experiment_active());
  // The specific surface doesn't matter.
  EXPECT_TRUE(settings.ShouldReportEncounteredSurface(ukm::AssignNewSourceId(),
                                                      kRegularSurface1));
  EXPECT_TRUE(settings.ShouldReportEncounteredSurface(ukm::AssignNewSourceId(),
                                                      kBlockedSurface1));
  EXPECT_TRUE(settings.ShouldReportEncounteredSurface(ukm::AssignNewSourceId(),
                                                      kBlockedTypeSurface1));
}

TEST(IdentifiabilityStudyStateStandaloneTest, MetaExperimentEnabled) {
  test::ScopedPrivacyBudgetConfig config(
      test::ScopedPrivacyBudgetConfig::Presets::kDisable);
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      features::kIdentifiabilityStudyMetaExperiment,
      {{features::kIdentifiabilityStudyMetaExperimentActivationProbability.name,
        "1"}});

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState settings(&pref_service);

  EXPECT_TRUE(settings.meta_experiment_active());
  // The specific surface doesn't matter.
  EXPECT_TRUE(settings.ShouldReportEncounteredSurface(ukm::AssignNewSourceId(),
                                                      kRegularSurface1));
  EXPECT_TRUE(settings.ShouldReportEncounteredSurface(ukm::AssignNewSourceId(),
                                                      kBlockedSurface1));
  EXPECT_TRUE(settings.ShouldReportEncounteredSurface(ukm::AssignNewSourceId(),
                                                      kBlockedTypeSurface1));

  // Regular surfaces should be dropped, internal surfaces still recorded.
  EXPECT_TRUE(settings.ShouldRecordSurface(kInternalSurface));
  EXPECT_FALSE(settings.ShouldRecordSurface(kRegularSurface1));
}

TEST(IdentifiabilityStudyStateStandaloneTest, MetaExperimentDisabled) {
  test::ScopedPrivacyBudgetConfig config(
      test::ScopedPrivacyBudgetConfig::Presets::kDisable);
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndDisableFeature(
      features::kIdentifiabilityStudyMetaExperiment);

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState settings(&pref_service);

  EXPECT_FALSE(settings.meta_experiment_active());
  // The specific surface doesn't matter.
  EXPECT_FALSE(settings.ShouldReportEncounteredSurface(ukm::AssignNewSourceId(),
                                                       kRegularSurface1));
}

TEST(IdentifiabilityStudyStateStandaloneTest,
     MetaExperimentEnabledWithProbability) {
  test::ScopedPrivacyBudgetConfig config(
      test::ScopedPrivacyBudgetConfig::Presets::kDisable);
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      features::kIdentifiabilityStudyMetaExperiment,
      {{features::kIdentifiabilityStudyMetaExperimentActivationProbability.name,
        "0.3"}});

  int num_active = 0;
  for (int count = 0;; ++count) {
    TestingPrefServiceSimple pref_service;
    prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
    test_utils::InspectableIdentifiabilityStudyState settings(&pref_service);
    if (settings.meta_experiment_active()) {
      ++num_active;
    }
    if (count > 20 && std::abs(num_active - 0.3 * count) < 0.1) {
      break;
    }
  }
}

TEST(IdentifiabilityStudyStateStandaloneTest,
     MetaExperimentActivationStateStoredInPrefs) {
  test::ScopedPrivacyBudgetConfig config(
      test::ScopedPrivacyBudgetConfig::Presets::kDisable);
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      features::kIdentifiabilityStudyMetaExperiment,
      {{features::kIdentifiabilityStudyMetaExperimentActivationProbability.name,
        "0.5"}});

  TestingPrefServiceSimple pref_service;
  prefs::RegisterPrivacyBudgetPrefs(pref_service.registry());
  test_utils::InspectableIdentifiabilityStudyState settings(&pref_service);
  bool was_active = settings.meta_experiment_active();
  test_utils::InspectableIdentifiabilityStudyState new_settings(&pref_service);
  EXPECT_EQ(was_active, new_settings.meta_experiment_active());
}