File: privacy_sandbox_settings_delegate_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (840 lines) | stat: -rw-r--r-- 32,369 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
// Copyright 2022 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_sandbox/privacy_sandbox_settings_delegate.h"

#include <stddef.h>

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

#include "base/strings/to_string.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/privacy_sandbox/tracking_protection_onboarding_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/browser/supervised_user/supervised_user_test_util.h"
#include "chrome/browser/tpcd/experiment/mock_experiment_manager.h"
#include "chrome/browser/tpcd/experiment/tpcd_experiment_features.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/metrics/metrics_pref_names.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/privacy_sandbox/privacy_sandbox_prefs.h"
#include "components/privacy_sandbox/tpcd_experiment_eligibility.h"
#include "components/privacy_sandbox/tracking_protection_onboarding.h"
#include "components/privacy_sandbox/tracking_protection_prefs.h"
#include "components/signin/public/identity_manager/account_capabilities_test_mutator.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/android/webapps/webapp_registry.h"
#endif

namespace {

using ::privacy_sandbox::tracking_protection::
    TrackingProtectionOnboardingStatus;

constexpr char kTestEmail[] = "test@test.com";

class PrivacySandboxSettingsDelegateTest : public testing::Test {
 public:
  PrivacySandboxSettingsDelegateTest() {
    local_state_ = std::make_unique<ScopedTestingLocalState>(
        TestingBrowserProcess::GetGlobal());
    profile_ = IdentityTestEnvironmentProfileAdaptor::
        CreateProfileForIdentityTestEnvironment();
    adapter_ =
        std::make_unique<IdentityTestEnvironmentProfileAdaptor>(profile_.get());
    experiment_manager_ =
        std::make_unique<tpcd::experiment::MockExperimentManager>();
    delegate_ = std::make_unique<PrivacySandboxSettingsDelegate>(
        profile_.get(), experiment_manager_.get(),
        GetSingletonPrivacySandboxCountries());
  }

 protected:
  void SetPrivacySandboxAccountCapability(const std::string& account,
                                          bool enabled) {
    auto account_info = identity_test_env()
                            ->identity_manager()
                            ->FindExtendedAccountInfoByEmailAddress(kTestEmail);
    AccountCapabilitiesTestMutator mutator(&account_info.capabilities);
    mutator.set_can_run_chrome_privacy_sandbox_trials(enabled);
    signin::UpdateAccountInfoForAccount(identity_test_env()->identity_manager(),
                                        account_info);
  }

  void SetRestrictedNoticeCapability(const std::string& account, bool enabled) {
    auto account_info = identity_test_env()
                            ->identity_manager()
                            ->FindExtendedAccountInfoByEmailAddress(kTestEmail);
    AccountCapabilitiesTestMutator mutator(&account_info.capabilities);
    mutator
        .set_is_subject_to_chrome_privacy_sandbox_restricted_measurement_notice(
            enabled);
    signin::UpdateAccountInfoForAccount(identity_test_env()->identity_manager(),
                                        account_info);
  }

  PrivacySandboxSettingsDelegate* delegate() { return delegate_.get(); }
  base::test::ScopedFeatureList* feature_list() { return &feature_list_; }
  signin::IdentityTestEnvironment* identity_test_env() {
    return adapter_->identity_test_env();
  }
  ScopedTestingLocalState* local_state() { return local_state_.get(); }
  TestingProfile* profile() { return profile_.get(); }
  sync_preferences::TestingPrefServiceSyncable* prefs() {
    return profile()->GetTestingPrefService();
  }
  tpcd::experiment::MockExperimentManager* experiment_manager() {
    return experiment_manager_.get();
  }

 private:
  content::BrowserTaskEnvironment browser_task_environment_;
  base::test::ScopedFeatureList feature_list_;
  std::unique_ptr<IdentityTestEnvironmentProfileAdaptor> adapter_;
  std::unique_ptr<ScopedTestingLocalState> local_state_;
  std::unique_ptr<TestingProfile> profile_;
  std::unique_ptr<tpcd::experiment::MockExperimentManager> experiment_manager_;
  std::unique_ptr<PrivacySandboxSettingsDelegate> delegate_;
};

TEST_F(PrivacySandboxSettingsDelegateTest,
       CapabilityRestrictionForSignedInUser) {
  // Sign the user in.
  identity_test_env()->MakePrimaryAccountAvailable(
      kTestEmail, signin::ConsentLevel::kSignin);

  // Initially the account capability will be in an unknown state, which
  // should be interpreted as no restriction.
  EXPECT_FALSE(delegate()->IsPrivacySandboxRestricted());

  // When the capability is restricted, the delegate should return as such.
  SetPrivacySandboxAccountCapability(kTestEmail, false);
  EXPECT_TRUE(delegate()->IsPrivacySandboxRestricted());
  // Even when the capability is currently unrestricted, the sandbox should
  // remain restricted. The capability should be reported as currently
  // unrestricted.
  // TODO (crbug.com/1428546): Adjust when we have a graduation flow.
  SetPrivacySandboxAccountCapability(kTestEmail, true);
  EXPECT_TRUE(delegate()->IsPrivacySandboxRestricted());
  EXPECT_TRUE(delegate()->IsPrivacySandboxCurrentlyUnrestricted());
}

TEST_F(PrivacySandboxSettingsDelegateTest,
       CapabilityRestrictionForSignedOutUser) {
  // If the user is not signed in to Chrome then we don't use any age signal and
  // don't restrict the feature.
  EXPECT_FALSE(delegate()->IsPrivacySandboxRestricted());
  EXPECT_FALSE(delegate()->IsPrivacySandboxCurrentlyUnrestricted());
}

TEST_F(PrivacySandboxSettingsDelegateTest,
       RestrictedNoticeRequiredForSignedInUser) {
  feature_list()->InitAndEnableFeatureWithParameters(
      privacy_sandbox::kPrivacySandboxSettings4,
      {{privacy_sandbox::kPrivacySandboxSettings4RestrictedNoticeName,
        "true"}});
  // Sign the user in.
  identity_test_env()->MakePrimaryAccountAvailable(
      kTestEmail, signin::ConsentLevel::kSignin);

  // Initially the account capability will be in an unknown state, which
  // should be interpreted as no restriction.
  EXPECT_FALSE(delegate()->IsSubjectToM1NoticeRestricted());

  // Validate that the notice is not required when the account is not configured
  // to show it.
  SetRestrictedNoticeCapability(kTestEmail, false);
  EXPECT_FALSE(delegate()->IsSubjectToM1NoticeRestricted());

  // Validate that the notice is required when the account is configured to show
  // it.
  SetRestrictedNoticeCapability(kTestEmail, true);
  EXPECT_TRUE(delegate()->IsSubjectToM1NoticeRestricted());
}

TEST_F(PrivacySandboxSettingsDelegateTest,
       RestrictedNoticeRequiredWithoutAccountToken) {
  feature_list()->InitAndEnableFeatureWithParameters(
      privacy_sandbox::kPrivacySandboxSettings4,
      {{privacy_sandbox::kPrivacySandboxSettings4RestrictedNoticeName,
        "true"}});
  // Sign the user in.
  identity_test_env()->MakePrimaryAccountAvailable(
      kTestEmail, signin::ConsentLevel::kSignin);

  // Initially the account capability will be in an unknown state
  EXPECT_FALSE(delegate()->IsSubjectToM1NoticeRestricted());

  // Enable the account capability
  SetRestrictedNoticeCapability(kTestEmail, true);

  // Remove the refresh token for the account
  signin::RemoveRefreshTokenForPrimaryAccount(
      identity_test_env()->identity_manager());

  // Capability is fetched even if the token is not available
  EXPECT_TRUE(delegate()->IsSubjectToM1NoticeRestricted());
}

TEST_F(PrivacySandboxSettingsDelegateTest,
       RestrictedNoticeRequiredForSignedOutUser) {
  feature_list()->InitAndEnableFeatureWithParameters(
      privacy_sandbox::kPrivacySandboxSettings4,
      {{privacy_sandbox::kPrivacySandboxSettings4RestrictedNoticeName,
        "true"}});
  // If the user is not signed in to Chrome then we don't use any age signal and
  // don't restrict the feature.
  EXPECT_FALSE(delegate()->IsSubjectToM1NoticeRestricted());
}

TEST_F(PrivacySandboxSettingsDelegateTest,
       RestrictedNoticeRequiredFeatureDisabled) {
  feature_list()->InitAndEnableFeatureWithParameters(
      privacy_sandbox::kPrivacySandboxSettings4,
      {{privacy_sandbox::kPrivacySandboxSettings4RestrictedNoticeName,
        "false"}});
  identity_test_env()->MakePrimaryAccountAvailable(
      kTestEmail, signin::ConsentLevel::kSignin);
  SetRestrictedNoticeCapability(kTestEmail, true);
  // Even if the user is signed in to Chrome, the feature being disabled means
  // no notice should be shown.
  EXPECT_FALSE(delegate()->IsSubjectToM1NoticeRestricted());
}

TEST_F(PrivacySandboxSettingsDelegateTest,
       AppropriateTopicsConsent_ConsentNotRequired) {
  // When the V4 consent required parameter is not present, Topics always has
  // an appropriate level of consent.
  prefs()->SetBoolean(prefs::kPrivacySandboxTopicsConsentGiven, false);
  feature_list()->InitAndEnableFeature(
      privacy_sandbox::kPrivacySandboxSettings4);

  EXPECT_TRUE(delegate()->HasAppropriateTopicsConsent());

  feature_list()->Reset();
  feature_list()->InitAndEnableFeatureWithParameters(
      privacy_sandbox::kPrivacySandboxSettings4,
      {{privacy_sandbox::kPrivacySandboxSettings4NoticeRequired.name, "true"}});

  EXPECT_TRUE(delegate()->HasAppropriateTopicsConsent());
}

TEST_F(PrivacySandboxSettingsDelegateTest,
       AppropriateTopicsConsent_ConsentRequired) {
  feature_list()->InitAndEnableFeatureWithParameters(
      privacy_sandbox::kPrivacySandboxSettings4,
      {{privacy_sandbox::kPrivacySandboxSettings4ConsentRequired.name,
        "true"}});

  // Default state should be a not-active consent.
  EXPECT_FALSE(delegate()->HasAppropriateTopicsConsent());

  prefs()->SetBoolean(prefs::kPrivacySandboxTopicsConsentGiven, true);
  EXPECT_TRUE(delegate()->HasAppropriateTopicsConsent());

  prefs()->SetBoolean(prefs::kPrivacySandboxTopicsConsentGiven, false);
  EXPECT_FALSE(delegate()->HasAppropriateTopicsConsent());
}

namespace {

using TpcdExperimentEligibility = privacy_sandbox::TpcdExperimentEligibility;

const base::Time kCurrentTime = base::Time::Now();
const base::Time kValidInstallDate = kCurrentTime - base::Days(31);

#if BUILDFLAG(IS_ANDROID)
class MockWebappRegistry : public WebappRegistry {
 public:
  // WebappRegistry:
  MOCK_METHOD(std::vector<std::string>,
              GetOriginsWithInstalledApp,
              (),
              (override));
};
#endif

struct CookieDeprecationExperimentEligibilityTestCase {
  bool force_eligible = false;
  bool exclude_3pc_blocked = true;
  bool exclude_not_seen_notice = true;
  bool exclude_dasher_account = true;
  bool exclude_new_user = true;
  std::string install_time_new_user = "30d";
#if BUILDFLAG(IS_ANDROID)
  bool exclude_pwa_twa_installed = true;
#endif
  std::optional<bool> is_subject_to_enterprise_policies;
  content_settings::CookieControlsMode cookie_controls_mode_pref =
      content_settings::CookieControlsMode::kOff;
  ContentSetting cookie_content_setting = ContentSetting::CONTENT_SETTING_ALLOW;
  bool tracking_protection_3pcd_enabled_pref = false;
  bool privacy_sandbox_eea_notice_acknowledged_pref = false;
  bool privacy_sandbox_row_notice_acknowledged_pref = false;
  std::optional<base::Time> install_date = kValidInstallDate;
#if BUILDFLAG(IS_ANDROID)
  std::vector<std::string> origins_with_installed_app;
#endif
  // The eligibility before the set up, which should be sticky.
  std::optional<bool> expected_eligible_before;
  bool expected_eligible;
  TpcdExperimentEligibility::Reason expected_current_eligibility;
};

const CookieDeprecationExperimentEligibilityTestCase
    kCookieDeprecationExperimentEligibilityTestCases[] = {
        {
            .expected_eligible = false,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kHasNotSeenNotice,
        },
        {
            .exclude_not_seen_notice = false,
            .expected_eligible = true,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kEligible,
        },
        {
            .force_eligible = true,
            .expected_eligible = true,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kForcedEligible,
        },
        {
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .expected_eligible = true,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kEligible,
        },
        {
            .privacy_sandbox_row_notice_acknowledged_pref = true,
            .expected_eligible = true,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kEligible,
        },
        {
            .cookie_controls_mode_pref =
                content_settings::CookieControlsMode::kBlockThirdParty,
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .expected_eligible = false,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::k3pCookiesBlocked,
        },
        {
            .exclude_3pc_blocked = false,
            .cookie_controls_mode_pref =
                content_settings::CookieControlsMode::kBlockThirdParty,
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .expected_eligible = true,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kEligible,
        },
        {
            .cookie_content_setting = ContentSetting::CONTENT_SETTING_BLOCK,
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .expected_eligible = false,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::k3pCookiesBlocked,
        },
        {
            .tracking_protection_3pcd_enabled_pref = true,
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .expected_eligible = true,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kEligible,
        },
        {
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .install_date = std::nullopt,
            .expected_eligible = false,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kNewUser,
        },
        {
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .install_date = kCurrentTime - base::Days(29),
            .expected_eligible = false,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kNewUser,
        },
        {
            .install_time_new_user = "4d",  // base::Days(4)
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .install_date = kCurrentTime - base::Days(5),
            .expected_eligible = true,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kEligible,
        },
        {
            .exclude_new_user = false,
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .install_date = kCurrentTime - base::Days(5),
            .expected_eligible = true,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kEligible,
        },
        {
            .is_subject_to_enterprise_policies = true,
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .expected_eligible = false,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kEnterpriseUser,
        },
        {
            .exclude_dasher_account = false,
            .is_subject_to_enterprise_policies = true,
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .expected_eligible = true,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kEligible,
        },
        {
            .is_subject_to_enterprise_policies = false,
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .expected_eligible = true,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kEligible,
        },
#if BUILDFLAG(IS_ANDROID)
        {
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .origins_with_installed_app =
                std::vector<std::string>({"https://a.test"}),
            .expected_eligible = false,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kPwaOrTwaInstalled,
        },
#endif
        {
            .privacy_sandbox_eea_notice_acknowledged_pref = true,
            .expected_eligible_before = false,
            .expected_eligible = false,
            .expected_current_eligibility =
                TpcdExperimentEligibility::Reason::kEligible,
        },
};

class CookieDeprecationExperimentEligibilityTest
    : public PrivacySandboxSettingsDelegateTest,
      public ::testing::WithParamInterface<
          CookieDeprecationExperimentEligibilityTestCase> {
 public:
  CookieDeprecationExperimentEligibilityTest() {
#if BUILDFLAG(IS_ANDROID)
    auto webapp_registry = std::make_unique<MockWebappRegistry>();
    webapp_registry_ = webapp_registry.get();
    delegate()->OverrideWebappRegistryForTesting(std::move(webapp_registry));
#endif
  }

 protected:
  content_settings::CookieSettings* cookie_settings() {
    return CookieSettingsFactory::GetForProfile(profile()).get();
  }

  void SetSubjectToEnterprisePoliciesCapability(const std::string& account,
                                                bool enabled) {
    auto account_info = identity_test_env()
                            ->identity_manager()
                            ->FindExtendedAccountInfoByEmailAddress(kTestEmail);
    AccountCapabilitiesTestMutator mutator(&account_info.capabilities);
    mutator.set_is_subject_to_enterprise_policies(enabled);
    signin::UpdateAccountInfoForAccount(identity_test_env()->identity_manager(),
                                        account_info);
  }

#if BUILDFLAG(IS_ANDROID)
  raw_ptr<MockWebappRegistry> webapp_registry_;
#endif
};

// The parameter indicates whether to use per-profile filtering.
class CookieDeprecationExperimentEligibilityOTRProfileTest
    : public PrivacySandboxSettingsDelegateTest,
      public testing::WithParamInterface<bool> {};

// The parameter indicates whether to disable 3pcs.
class CookieDeprecationLabelAllowedTest
    : public PrivacySandboxSettingsDelegateTest,
      public testing::WithParamInterface<bool> {};

}  // namespace

TEST_F(PrivacySandboxSettingsDelegateTest, IsEligible) {
  feature_list()->InitAndEnableFeature(
      features::kCookieDeprecationFacilitatedTesting);

  const struct {
    const char* description;
    std::optional<bool> is_eligible;
    bool expected_eligible;
  } kTestCases[] = {
      {
          .description = "unknown",
          .expected_eligible = false,
      },
      {
          .description = "eligible",
          .is_eligible = true,
          .expected_eligible = true,
      },
      {
          .description = "ineligible",
          .is_eligible = false,
          .expected_eligible = false,
      },
  };

  for (const auto& test_case : kTestCases) {
    SCOPED_TRACE(test_case.description);
    EXPECT_CALL(*experiment_manager(), IsClientEligible)
        .WillOnce(::testing::Return(test_case.is_eligible));
    EXPECT_EQ(delegate()->IsCookieDeprecationExperimentEligible(),
              test_case.expected_eligible);
  }
}

TEST_P(CookieDeprecationExperimentEligibilityTest, IsEligible) {
  const CookieDeprecationExperimentEligibilityTestCase& test_case = GetParam();

  feature_list()->InitAndEnableFeatureWithParameters(
      features::kCookieDeprecationFacilitatedTesting,
      {{"use_profile_filtering", "true"},
       {"force_eligible", base::ToString(test_case.force_eligible)},
       {tpcd::experiment::kExclude3PCBlockedName,
        base::ToString(test_case.exclude_3pc_blocked)},
       {tpcd::experiment::kExcludeNotSeenAdsAPIsNoticeName,
        base::ToString(test_case.exclude_not_seen_notice)},
       {tpcd::experiment::kExcludeDasherAccountName,
        base::ToString(test_case.exclude_dasher_account)},
       {tpcd::experiment::kExcludeNewUserName,
        base::ToString(test_case.exclude_new_user)},
       {tpcd::experiment::kInstallTimeForNewUserName,
        test_case.install_time_new_user},
#if BUILDFLAG(IS_ANDROID)
       {tpcd::experiment::kExcludePwaOrTwaInstalledName,
        base::ToString(test_case.exclude_pwa_twa_installed)}
#endif
      });

  if (test_case.expected_eligible_before) {
    EXPECT_EQ(delegate()->IsCookieDeprecationExperimentEligible(),
              *test_case.expected_eligible_before);
  }

  if (test_case.is_subject_to_enterprise_policies.has_value()) {
    // Sign the user in.
    identity_test_env()->MakePrimaryAccountAvailable(
        kTestEmail, signin::ConsentLevel::kSignin);
    SetSubjectToEnterprisePoliciesCapability(
        kTestEmail, *test_case.is_subject_to_enterprise_policies);
  }

  prefs()->SetInteger(prefs::kCookieControlsMode,
                      static_cast<int>(test_case.cookie_controls_mode_pref));
  prefs()->SetBoolean(prefs::kPrivacySandboxM1RowNoticeAcknowledged,
                      test_case.privacy_sandbox_row_notice_acknowledged_pref);
  prefs()->SetBoolean(prefs::kPrivacySandboxM1EEANoticeAcknowledged,
                      test_case.privacy_sandbox_eea_notice_acknowledged_pref);
  prefs()->SetBoolean(prefs::kTrackingProtection3pcdEnabled,
                      test_case.tracking_protection_3pcd_enabled_pref);

  cookie_settings()->SetDefaultCookieSetting(test_case.cookie_content_setting);

  if (test_case.install_date.has_value()) {
    local_state()->Get()->SetInt64(metrics::prefs::kInstallDate,
                                   test_case.install_date->ToTimeT());
  }

#if BUILDFLAG(IS_ANDROID)
  ON_CALL(*webapp_registry_, GetOriginsWithInstalledApp)
      .WillByDefault(testing::Return(test_case.origins_with_installed_app));
#endif

  EXPECT_EQ(
      delegate()->GetCookieDeprecationExperimentCurrentEligibility().reason(),
      test_case.expected_current_eligibility);
  EXPECT_EQ(delegate()->IsCookieDeprecationExperimentEligible(),
            test_case.expected_eligible);
}

INSTANTIATE_TEST_SUITE_P(
    CookieDeprecationExperimentEligibility,
    CookieDeprecationExperimentEligibilityTest,
    ::testing::ValuesIn(kCookieDeprecationExperimentEligibilityTestCases));

TEST_P(CookieDeprecationExperimentEligibilityOTRProfileTest, IsEligible) {
  Profile* off_the_record_profile = profile()->GetOffTheRecordProfile(
      Profile::OTRProfileID::CreateUniqueForTesting(),
      /*create_if_needed=*/true);
  PrivacySandboxSettingsDelegate otr_delegate_under_test(
      off_the_record_profile, experiment_manager(),
      GetSingletonPrivacySandboxCountries());

  // Android does not have guest profiles.
#if !BUILDFLAG(IS_ANDROID)
  auto guest_profile = TestingProfile::Builder().SetGuestSession().Build();
  PrivacySandboxSettingsDelegate guest_delegate_under_test(
      guest_profile.get(), experiment_manager(),
      GetSingletonPrivacySandboxCountries());
#endif  // !BUILDFLAG(IS_ANDROID)

  const bool use_profile_filtering = GetParam();

  const std::string use_profile_filtering_param =
      base::ToString(use_profile_filtering);

  {
    feature_list()->InitAndEnableFeatureWithParameters(
        features::kCookieDeprecationFacilitatedTesting,
        {{"force_eligible", "true"},
         {"use_profile_filtering", use_profile_filtering_param},
         {"enable_otr_profiles", "true"}});

    if (!use_profile_filtering) {
      EXPECT_CALL(*experiment_manager(), IsClientEligible)
          .WillOnce(::testing::Return(true));
    } else {
      EXPECT_CALL(*experiment_manager(), IsClientEligible).Times(0);
    }

    EXPECT_TRUE(
        otr_delegate_under_test.IsCookieDeprecationExperimentEligible());

#if !BUILDFLAG(IS_ANDROID)
    if (!use_profile_filtering) {
      EXPECT_CALL(*experiment_manager(), IsClientEligible)
          .WillOnce(::testing::Return(true));
    } else {
      EXPECT_CALL(*experiment_manager(), IsClientEligible).Times(0);
    }

    EXPECT_TRUE(
        guest_delegate_under_test.IsCookieDeprecationExperimentEligible());
#endif  // !BUILDFLAG(IS_ANDROID)

    feature_list()->Reset();
  }

  {
    feature_list()->InitAndEnableFeatureWithParameters(
        features::kCookieDeprecationFacilitatedTesting,
        {{"force_eligible", "true"},
         {"use_profile_filtering", use_profile_filtering_param},
         {"enable_otr_profiles", "false"}});
    EXPECT_FALSE(
        otr_delegate_under_test.IsCookieDeprecationExperimentEligible());

#if !BUILDFLAG(IS_ANDROID)
    EXPECT_FALSE(
        guest_delegate_under_test.IsCookieDeprecationExperimentEligible());
#endif  // !BUILDFLAG(IS_ANDROID)

    feature_list()->Reset();
  }
}

INSTANTIATE_TEST_SUITE_P(All,
                         CookieDeprecationExperimentEligibilityOTRProfileTest,
                         testing::Bool());

TEST_P(CookieDeprecationLabelAllowedTest, IsClientEligibleChecked) {
  feature_list()->InitAndEnableFeatureWithParameters(
      features::kCookieDeprecationFacilitatedTesting,
      {{tpcd::experiment::kDisable3PCookiesName, base::ToString(GetParam())}});

  const bool disable_3pcs = GetParam();
  if (disable_3pcs) {
    // Simulate onboarding a profile.
    prefs()->SetInteger(
        prefs::kTrackingProtectionOnboardingStatus,
        static_cast<int>(privacy_sandbox::TrackingProtectionOnboarding::
                             OnboardingStatus::kOnboarded));
  }

  for (bool is_client_eligible : {false, true}) {
    SCOPED_TRACE(is_client_eligible);

    EXPECT_CALL(*experiment_manager(), IsClientEligible)
        .WillOnce(::testing::Return(is_client_eligible));
    EXPECT_EQ(delegate()->IsCookieDeprecationLabelAllowed(),
              is_client_eligible);
  }
}

TEST_P(CookieDeprecationLabelAllowedTest, OnboardingStatusChecked) {
  const struct {
    TrackingProtectionOnboardingStatus onboarding_status;
    bool need_onboarding = false;
    bool expected_allowed;
  } kTestCases[] = {
      {
          .onboarding_status = TrackingProtectionOnboardingStatus::kIneligible,
          .expected_allowed = false,
      },
      {
          .onboarding_status = TrackingProtectionOnboardingStatus::kEligible,
          .need_onboarding = false,
          .expected_allowed = true,
      },
      {
          .onboarding_status = TrackingProtectionOnboardingStatus::kEligible,
          .need_onboarding = true,
          .expected_allowed = false,
      },
      {
          .onboarding_status = TrackingProtectionOnboardingStatus::kOnboarded,
          .need_onboarding = false,
          .expected_allowed = true,
      },
      {
          .onboarding_status = TrackingProtectionOnboardingStatus::kOnboarded,
          .need_onboarding = true,
          .expected_allowed = true,
      },

  };

  EXPECT_CALL(*experiment_manager(), IsClientEligible)
      .WillRepeatedly(::testing::Return(true));

  const bool disable_3pcs = GetParam();

  for (const auto& test_case : kTestCases) {
    SCOPED_TRACE(static_cast<int>(test_case.onboarding_status));

    feature_list()->InitAndEnableFeatureWithParameters(
        features::kCookieDeprecationFacilitatedTesting,
        {{tpcd::experiment::kDisable3PCookiesName,
          base::ToString(disable_3pcs)},
         {tpcd::experiment::kNeedOnboardingForLabelName,
          base::ToString(test_case.need_onboarding)},
         {tpcd::experiment::kEnableSilentOnboardingName, "true"}});

    if (disable_3pcs) {
      prefs()->SetInteger(prefs::kTrackingProtectionOnboardingStatus,
                          static_cast<int>(test_case.onboarding_status));
      prefs()->SetInteger(prefs::kTrackingProtectionSilentOnboardingStatus,
                          static_cast<int>(test_case.onboarding_status));
      EXPECT_EQ(delegate()->IsCookieDeprecationLabelAllowed(),
                test_case.expected_allowed);
    }

    feature_list()->Reset();
  }
}

INSTANTIATE_TEST_SUITE_P(All,
                         CookieDeprecationLabelAllowedTest,
                         testing::Bool());

namespace {

struct ThirdPartyCookiesBlockedByCookieDeprecationExperimentTestCase {
  bool is_client_eligible;
  bool is_profile_onboarded = false;
  content_settings::CookieControlsMode cookie_controls_mode_pref =
      content_settings::CookieControlsMode::kOff;
  bool block_all_3pc_toggle_enabled = false;
  bool expected;
};

const ThirdPartyCookiesBlockedByCookieDeprecationExperimentTestCase
    kThirdPartyCookiesBlockedByCookieDeprecationExperimentTestCases[] = {
        {
            .is_client_eligible = false,
            .expected = false,
        },
        {
            .is_client_eligible = true,
            .is_profile_onboarded = false,
            .expected = false,
        },
        {
            .is_client_eligible = true,
            .is_profile_onboarded = true,
            .expected = true,
        },
        {
            .is_client_eligible = true,
            .is_profile_onboarded = true,
            .cookie_controls_mode_pref =
                content_settings::CookieControlsMode::kBlockThirdParty,
            .expected = false,
        },
        {
            .is_client_eligible = true,
            .is_profile_onboarded = true,
            .block_all_3pc_toggle_enabled = true,
            .expected = false,
        },
};

class ThirdPartyCookiesBlockedByCookieDeprecationExperimentTest
    : public PrivacySandboxSettingsDelegateTest,
      public ::testing::WithParamInterface<
          ThirdPartyCookiesBlockedByCookieDeprecationExperimentTestCase> {
 public:
  ThirdPartyCookiesBlockedByCookieDeprecationExperimentTest() {
    feature_list()->InitAndEnableFeatureWithParameters(
        features::kCookieDeprecationFacilitatedTesting,
        {{tpcd::experiment::kDisable3PCookiesName, "true"}});
  }
};

}  // namespace

TEST_P(ThirdPartyCookiesBlockedByCookieDeprecationExperimentTest,
       AreThirdPartyCookiesBlockedByExperiment) {
  const ThirdPartyCookiesBlockedByCookieDeprecationExperimentTestCase&
      test_case = GetParam();

  if (test_case.is_profile_onboarded) {
    // Simulate onboarding a profile.
    prefs()->SetInteger(
        prefs::kTrackingProtectionOnboardingStatus,
        static_cast<int>(privacy_sandbox::TrackingProtectionOnboarding::
                             OnboardingStatus::kOnboarded));
    prefs()->SetBoolean(prefs::kTrackingProtection3pcdEnabled, true);
  }

  prefs()->SetInteger(prefs::kCookieControlsMode,
                      static_cast<int>(test_case.cookie_controls_mode_pref));
  prefs()->SetBoolean(prefs::kBlockAll3pcToggleEnabled,
                      test_case.block_all_3pc_toggle_enabled);

  EXPECT_CALL(*experiment_manager(), IsClientEligible)
      .WillOnce(::testing::Return(test_case.is_client_eligible));

  EXPECT_EQ(
      delegate()->AreThirdPartyCookiesBlockedByCookieDeprecationExperiment(),
      test_case.expected);
}

INSTANTIATE_TEST_SUITE_P(
    ThirdPartyCookiesBlockedByCookieDeprecationExperiment,
    ThirdPartyCookiesBlockedByCookieDeprecationExperimentTest,
    ::testing::ValuesIn(
        kThirdPartyCookiesBlockedByCookieDeprecationExperimentTestCases));

}  // namespace