File: family_link_user_metrics_provider_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 (721 lines) | stat: -rw-r--r-- 30,718 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
// 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/metrics/family_link_user_metrics_provider.h"

#include <string>
#include <utility>

#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/browser/supervised_user/supervised_user_test_util.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/test/content_settings_mock_provider.h"
#include "components/content_settings/core/test/content_settings_test_utils.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/account_capabilities_test_mutator.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/supervised_user/core/browser/supervised_user_preferences.h"
#include "components/supervised_user/core/browser/supervised_user_test_environment.h"
#include "components/supervised_user/core/browser/supervised_user_utils.h"
#include "components/supervised_user/core/common/features.h"
#include "components/supervised_user/core/common/pref_names.h"
#include "components/supervised_user/core/common/supervised_user_constants.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace supervised_user {
namespace {
constexpr char kTestEmail[] = "test@gmail.com";
constexpr char kTestEmail1[] = "test1@gmail.com";
constexpr char kTestEmail2[] = "test2@gmail.com";
constexpr char kTestProfile[] = "profile";
constexpr char kTestProfile1[] = "profile1";
constexpr char kTestProfile2[] = "profile2";

class FamilyLinkUserMetricsProviderTest : public testing::Test {
 protected:
  FamilyLinkUserMetricsProviderTest()
      : test_profile_manager_(TestingBrowserProcess::GetGlobal()) {}

  void SetUp() override {
    ASSERT_TRUE(test_profile_manager_.SetUp());
    metrics_provider()->skip_active_browser_count_for_unittesting_ = true;
  }

  void TearDown() override { test_profile_manager_.DeleteAllTestingProfiles(); }

  FamilyLinkUserMetricsProvider* metrics_provider() {
    return &metrics_provider_;
  }

  TestingProfileManager* test_profile_manager() {
    return &test_profile_manager_;
  }

  Profile* CreateTestingProfile(const std::string& test_email,
                                const std::string& test_profile,
                                bool is_subject_to_parental_controls,
                                bool is_opted_in_to_parental_supervision) {
    Profile* profile = test_profile_manager()->CreateTestingProfile(
        test_profile, /*prefs=*/nullptr, base::UTF8ToUTF16(test_profile),
        /*avatar_id=*/0, GetTestingFactories(),
        /*is_supervised_profile=*/is_subject_to_parental_controls,
        /*is_new_profile=*/std::nullopt,
        /*policy_service=*/std::nullopt, /*shared_url_loader_factory=*/nullptr);

    AccountInfo account = signin::MakePrimaryAccountAvailable(
        IdentityManagerFactory::GetForProfile(profile), test_email,
        signin::ConsentLevel::kSignin);
    AccountCapabilitiesTestMutator mutator(&account.capabilities);
    // Tests assume that this account is in Family Link.
    mutator.set_can_fetch_family_member_info(true);
    mutator.set_is_subject_to_parental_controls(
        is_subject_to_parental_controls);
    mutator.set_is_opted_in_to_parental_supervision(
        is_opted_in_to_parental_supervision);
    signin::UpdateAccountInfoForAccount(
        IdentityManagerFactory::GetForProfile(profile), account);

#if BUILDFLAG(ENABLE_EXTENSIONS)
    if (is_subject_to_parental_controls) {
      // Set Family Link `Permissions` switch (and its dependencies) to the default value.
      // Mimics the assignment by the `SupervisedUserPrefStore`.
      supervised_user_test_util::
          SetSupervisedUserExtensionsMayRequestPermissionsPref(profile, true);
    }
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)
    return profile;
  }

  // Creates the testing factories for the profile. The important part is to
  // inject identity manager related factories and supervised user service
  // factory - which would allow overrides in this test fixture subclasses.
  TestingProfile::TestingFactories GetTestingFactories() {
    TestingProfile::TestingFactories factories =
        IdentityTestEnvironmentProfileAdaptor::
            GetIdentityTestEnvironmentFactories();
    factories.emplace_back(
        SupervisedUserServiceFactory::GetInstance(),
        base::BindOnce(
            &FamilyLinkUserMetricsProviderTest::BuildSupervisedUserService,
            base::Unretained(this)));
    return factories;
  }

  // Default supervised user service, as in production.
  virtual std::unique_ptr<KeyedService> BuildSupervisedUserService(
      content::BrowserContext* browser_context) {
    Profile* profile = Profile::FromBrowserContext(browser_context);
    return SupervisedUserServiceFactory::BuildInstanceFor(profile);
  }

  void SetFamilyRole(Profile* profile, kidsmanagement::FamilyRole family_role) {
    profile->GetPrefs()->SetString(prefs::kFamilyLinkUserMemberRole,
                                   FamilyRoleToString(family_role));
  }

  void SetPermissionsToggleForSupervisedUser(Profile* profile, bool enabled) {
    supervised_user_test_util::
        SetSupervisedUserGeolocationEnabledContentSetting(profile, enabled);
  }

 private:
  content::BrowserTaskEnvironment task_environment_;
  FamilyLinkUserMetricsProvider metrics_provider_;
  TestingProfileManager test_profile_manager_;
};

TEST_F(FamilyLinkUserMetricsProviderTest,
       ProfileWithUnknownCapabilitiesDoesNotOutputHistogram) {
  Profile* profile = test_profile_manager()->CreateTestingProfile(
      kTestProfile, IdentityTestEnvironmentProfileAdaptor::
                        GetIdentityTestEnvironmentFactories());
  AccountInfo account = signin::MakePrimaryAccountAvailable(
      IdentityManagerFactory::GetForProfile(profile), kTestEmail,
      signin::ConsentLevel::kSignin);
  // Does not set account capabilities, default is unknown.

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectTotalCount(kFamilyLinkUserLogSegmentHistogramName,
                                    /*expected_count=*/0);
}

TEST_F(FamilyLinkUserMetricsProviderTest,
       ProfileWithRequiredSupervisionLoggedAsSupervisionEnabledByPolicy) {
  // Profile with supervision set by policy
  CreateTestingProfile(kTestEmail2, kTestProfile2,
                       /*is_subject_to_parental_controls=*/true,
                       /*is_opted_in_to_parental_supervision=*/false);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();

  histogram_tester.ExpectUniqueSample(
      kFamilyLinkUserLogSegmentHistogramName,
      FamilyLinkUserLogRecord::Segment::kSupervisionEnabledByFamilyLinkPolicy,
      /*expected_bucket_count=*/1);
}

TEST_F(FamilyLinkUserMetricsProviderTest,
       ProfileWithOptionalSupervisionLoggedSupervisionEnabledByUser) {
  // Profile with supervision set by user
  CreateTestingProfile(kTestEmail1, kTestProfile1,
                       /*is_subject_to_parental_controls=*/true,
                       /*is_opted_in_to_parental_supervision=*/true);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();

  histogram_tester.ExpectUniqueSample(
      kFamilyLinkUserLogSegmentHistogramName,
      FamilyLinkUserLogRecord::Segment::kSupervisionEnabledByFamilyLinkUser,
      /*expected_bucket_count=*/1);
}

TEST_F(FamilyLinkUserMetricsProviderTest,
       ProfileWithAdultUserLoggedAsUnsupervised) {
  // Adult profile
  CreateTestingProfile(kTestEmail, kTestProfile,
                       /*is_subject_to_parental_controls=*/false,
                       /*is_opted_in_to_parental_supervision=*/false);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();

  histogram_tester.ExpectUniqueSample(
      kFamilyLinkUserLogSegmentHistogramName,
      FamilyLinkUserLogRecord::Segment::kUnsupervised,
      /*expected_bucket_count=*/1);
}

TEST_F(
    FamilyLinkUserMetricsProviderTest,
    ProfilesWithMixedSupervisedUsersLoggedAsMixedProfileWithDefaultWebFilter) {
  // Profile with supervision set by user
  CreateTestingProfile(kTestEmail1, kTestProfile1,
                       /*is_subject_to_parental_controls=*/true,
                       /*is_opted_in_to_parental_supervision=*/false);
  // Profile with supervision set by policy
  CreateTestingProfile(kTestEmail2, kTestProfile2,
                       /*is_subject_to_parental_controls=*/true,
                       /*is_opted_in_to_parental_supervision=*/true);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectBucketCount(
      kFamilyLinkUserLogSegmentHistogramName,
      FamilyLinkUserLogRecord::Segment::kMixedProfile,
      /*expected_count=*/1);
  histogram_tester.ExpectUniqueSample(
      kFamilyLinkUserLogSegmentWebFilterHistogramName,
      WebFilterType::kTryToBlockMatureSites,
      /*expected_bucket_count=*/1);
}

TEST_F(
    FamilyLinkUserMetricsProviderTest,
    ProfilesWithMixedSupervisedAndAdultUsersLoggedAsMixedProfileWithDefaultWebFilter) {
  // Adult profile
  CreateTestingProfile(kTestEmail, kTestProfile,
                       /*is_subject_to_parental_controls=*/false,
                       /*is_opted_in_to_parental_supervision=*/false);

  // Profile with supervision set by user
  CreateTestingProfile(kTestEmail1, kTestProfile1,
                       /*is_subject_to_parental_controls=*/true,
                       /*is_opted_in_to_parental_supervision=*/false);

  // Profile with supervision set by policy
  CreateTestingProfile(kTestEmail2, kTestProfile2,
                       /*is_subject_to_parental_controls=*/true,
                       /*is_opted_in_to_parental_supervision=*/true);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectBucketCount(
      kFamilyLinkUserLogSegmentHistogramName,
      FamilyLinkUserLogRecord::Segment::kMixedProfile,
      /*expected_count=*/1);
  histogram_tester.ExpectUniqueSample(
      kFamilyLinkUserLogSegmentWebFilterHistogramName,
      WebFilterType::kTryToBlockMatureSites,
      /*expected_bucket_count=*/1);
}

TEST_F(FamilyLinkUserMetricsProviderTest,
       ProfilesWithMixedSupervisedFiltersLoggedAsMixed) {
  Profile* profile1 =
      CreateTestingProfile(kTestEmail1, kTestProfile1,
                           /*is_subject_to_parental_controls=*/true,
                           /*is_opted_in_to_parental_supervision=*/false);
  supervised_user_test_util::SetWebFilterType(profile1,
                                              WebFilterType::kAllowAllSites);
  Profile* profile2 =
      CreateTestingProfile(kTestEmail2, kTestProfile2,
                           /*is_subject_to_parental_controls=*/true,
                           /*is_opted_in_to_parental_supervision=*/false);
  supervised_user_test_util::SetWebFilterType(profile2,
                                              WebFilterType::kCertainSites);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectUniqueSample(
      kFamilyLinkUserLogSegmentWebFilterHistogramName, WebFilterType::kMixed,
      /*expected_bucket_count=*/1);
}

TEST_F(FamilyLinkUserMetricsProviderTest,
       AdultProfileDoesNotHavePermissionLogged) {
  CreateTestingProfile(kTestEmail1, kTestProfile1,
                       /*is_subject_to_parental_controls=*/false,
                       /*is_opted_in_to_parental_supervision=*/false);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectBucketCount(
      kSitesMayRequestCameraMicLocationHistogramName, ToggleState::kDisabled,
      /*expected_count=*/0);
}

TEST_F(FamilyLinkUserMetricsProviderTest,
       SupervisedProfileWithBlockedGeolocationLoggedAsPermissionsDisabled) {
  Profile* profile1 =
      CreateTestingProfile(kTestEmail1, kTestProfile1,
                           /*is_subject_to_parental_controls=*/true,
                           /*is_opted_in_to_parental_supervision=*/false);
  SetPermissionsToggleForSupervisedUser(profile1, false);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectUniqueSample(
      kSitesMayRequestCameraMicLocationHistogramName, ToggleState::kDisabled,
      /*expected_bucket_count=*/1);
}

TEST_F(FamilyLinkUserMetricsProviderTest,
       SupervisedProfileWithAllowedGeolocationLoggedAsPermissionsEnabled) {
  Profile* profile1 =
      CreateTestingProfile(kTestEmail1, kTestProfile1,
                           /*is_subject_to_parental_controls=*/true,
                           /*is_opted_in_to_parental_supervision=*/false);
  SetPermissionsToggleForSupervisedUser(profile1, true);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectUniqueSample(
      kSitesMayRequestCameraMicLocationHistogramName, ToggleState::kEnabled,
      /*expected_bucket_count=*/1);
}

#if BUILDFLAG(ENABLE_EXTENSIONS)
class FamilyLinkUserMetricsProviderTestWithExtensionsPermissionsEnabled
    : public FamilyLinkUserMetricsProviderTest {
 protected:
  FamilyLinkUserMetricsProviderTestWithExtensionsPermissionsEnabled() = default;

  void SetExtensionToggleStateForSupervisedUser(Profile* profile,
                                                bool toggle_state) {
    supervised_user_test_util::SetSkipParentApprovalToInstallExtensionsPref(
        profile, toggle_state);
  }
};

TEST_F(FamilyLinkUserMetricsProviderTestWithExtensionsPermissionsEnabled,
       ProfileWithExtensionToggleStateUnsetLoggedAsDisabled) {
  CreateTestingProfile(kTestEmail1, kTestProfile1,
                       /*is_subject_to_parental_controls=*/true,
                       /*is_opted_in_to_parental_supervision=*/false);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectUniqueSample(
      kSkipParentApprovalToInstallExtensionsHistogramName,
      ToggleState::kDisabled,
      /*expected_bucket_count=*/1);
}

TEST_F(FamilyLinkUserMetricsProviderTestWithExtensionsPermissionsEnabled,
       ProfileWithExtensionToggleStateOffLoggedAsDisabled) {
  Profile* profile1 =
      CreateTestingProfile(kTestEmail1, kTestProfile1,
                           /*is_subject_to_parental_controls=*/true,
                           /*is_opted_in_to_parental_supervision=*/false);
  SetExtensionToggleStateForSupervisedUser(profile1, false);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectUniqueSample(
      kSkipParentApprovalToInstallExtensionsHistogramName,
      ToggleState::kDisabled,
      /*expected_bucket_count=*/1);
}

TEST_F(FamilyLinkUserMetricsProviderTestWithExtensionsPermissionsEnabled,
       ProfileWithExtensionToggleStateOnLoggedAsEnabled) {
  Profile* profile1 =
      CreateTestingProfile(kTestEmail1, kTestProfile1,
                           /*is_subject_to_parental_controls=*/true,
                           /*is_opted_in_to_parental_supervision=*/false);
  SetExtensionToggleStateForSupervisedUser(profile1, true);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectUniqueSample(
      kSkipParentApprovalToInstallExtensionsHistogramName,
      ToggleState::kEnabled,
      /*expected_bucket_count=*/1);
}

TEST_F(FamilyLinkUserMetricsProviderTestWithExtensionsPermissionsEnabled,
       ProfilesWithMixedExtensionToggleStateLoggedAsMixed) {
  Profile* profile1 =
      CreateTestingProfile(kTestEmail1, kTestProfile1,
                           /*is_subject_to_parental_controls=*/true,
                           /*is_opted_in_to_parental_supervision=*/false);
  SetExtensionToggleStateForSupervisedUser(profile1, true);

  Profile* profile2 =
      CreateTestingProfile(kTestEmail2, kTestProfile2,
                           /*is_subject_to_parental_controls=*/true,
                           /*is_opted_in_to_parental_supervision=*/false);
  SetExtensionToggleStateForSupervisedUser(profile2, false);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectUniqueSample(
      kSkipParentApprovalToInstallExtensionsHistogramName, ToggleState::kMixed,
      /*expected_bucket_count=*/1);
}
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

TEST_F(FamilyLinkUserMetricsProviderTest,
       NoProfilesAddedShouldNotLogHistogram) {
  // Add no profiles
  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectBucketCount(
      kFamilyLinkUserLogSegmentHistogramName,
      FamilyLinkUserLogRecord::Segment::kMixedProfile,
      /*expected_count=*/0);
}

TEST_F(FamilyLinkUserMetricsProviderTest,
       SignedOutProfileLoggedAsUnsupervised) {
  test_profile_manager()->CreateTestingProfile(
      kTestProfile, IdentityTestEnvironmentProfileAdaptor::
                        GetIdentityTestEnvironmentFactories());

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectBucketCount(
      kFamilyLinkUserLogSegmentHistogramName,
      FamilyLinkUserLogRecord::Segment::kUnsupervised,
      /*expected_count=*/1);
}

TEST_F(FamilyLinkUserMetricsProviderTest, ParentProfileLoggedAsParent) {
  Profile* profile =
      CreateTestingProfile(kTestEmail1, kTestProfile1,
                           /*is_subject_to_parental_controls=*/false,
                           /*is_opted_in_to_parental_supervision=*/false);
  SetFamilyRole(profile, kidsmanagement::PARENT);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectBucketCount(kFamilyLinkUserLogSegmentHistogramName,
                                     FamilyLinkUserLogRecord::Segment::kParent,
                                     /*expected_count=*/1);
}

TEST_F(FamilyLinkUserMetricsProviderTest, FamilyManagerProfileLoggedAsParent) {
  Profile* profile =
      CreateTestingProfile(kTestEmail1, kTestProfile1,
                           /*is_subject_to_parental_controls=*/false,
                           /*is_opted_in_to_parental_supervision=*/false);
  SetFamilyRole(profile, kidsmanagement::HEAD_OF_HOUSEHOLD);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectBucketCount(kFamilyLinkUserLogSegmentHistogramName,
                                     FamilyLinkUserLogRecord::Segment::kParent,
                                     /*expected_count=*/1);
}

TEST_F(FamilyLinkUserMetricsProviderTest, ParentAndChildProfileLoggedAsMixed) {
  Profile* profile1 =
      CreateTestingProfile(kTestEmail1, kTestProfile1,
                           /*is_subject_to_parental_controls=*/false,
                           /*is_opted_in_to_parental_supervision=*/false);
  SetFamilyRole(profile1, kidsmanagement::HEAD_OF_HOUSEHOLD);

  CreateTestingProfile(kTestEmail2, kTestProfile2,
                       /*is_subject_to_parental_controls=*/true,
                       /*is_opted_in_to_parental_supervision=*/false);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectBucketCount(
      kFamilyLinkUserLogSegmentHistogramName,
      FamilyLinkUserLogRecord::Segment::kMixedProfile,
      /*expected_count=*/1);
}

TEST_F(FamilyLinkUserMetricsProviderTest, TwoParentProfilesLoggedAsParent) {
  Profile* profile1 =
      CreateTestingProfile(kTestEmail1, kTestProfile1,
                           /*is_subject_to_parental_controls=*/false,
                           /*is_opted_in_to_parental_supervision=*/false);
  SetFamilyRole(profile1, kidsmanagement::HEAD_OF_HOUSEHOLD);

  Profile* profile2 =
      CreateTestingProfile(kTestEmail2, kTestProfile2,
                           /*is_subject_to_parental_controls=*/false,
                           /*is_opted_in_to_parental_supervision=*/false);
  SetFamilyRole(profile2, kidsmanagement::PARENT);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectBucketCount(kFamilyLinkUserLogSegmentHistogramName,
                                     FamilyLinkUserLogRecord::Segment::kParent,
                                     /*expected_count=*/1);
}

TEST_F(FamilyLinkUserMetricsProviderTest,
       ParentAndSignedOutProfilesLoggedAsParent) {
  test_profile_manager()->CreateTestingProfile(
      kTestProfile, IdentityTestEnvironmentProfileAdaptor::
                        GetIdentityTestEnvironmentFactories());

  Profile* profile1 =
      CreateTestingProfile(kTestEmail1, kTestProfile1,
                           /*is_subject_to_parental_controls=*/false,
                           /*is_opted_in_to_parental_supervision=*/false);
  SetFamilyRole(profile1, kidsmanagement::HEAD_OF_HOUSEHOLD);

  Profile* profile2 =
      CreateTestingProfile(kTestEmail2, kTestProfile2,
                           /*is_subject_to_parental_controls=*/false,
                           /*is_opted_in_to_parental_supervision=*/false);
  SetFamilyRole(profile2, kidsmanagement::PARENT);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();
  histogram_tester.ExpectBucketCount(kFamilyLinkUserLogSegmentHistogramName,
                                     FamilyLinkUserLogRecord::Segment::kParent,
                                     /*expected_count=*/1);
}

#if BUILDFLAG(IS_ANDROID)
struct ContentFiltersTestCase {
  std::size_t profile_count;
  std::string test_name;
};

// Test fixture for verifying that the content filters are correctly
// reflected in the metrics. Content filters are mutually exclusive with
// Family-Link supervision and cannot be applied to these profiles.
class FamilyLinkUserMetricsProviderWithContentFiltersTest
    : public FamilyLinkUserMetricsProviderTest,
      public testing::WithParamInterface<ContentFiltersTestCase> {
 protected:
  void CreateProfiles(std::size_t count) {
    CHECK_GE(email_addresses_.size(), count) << "Not enough email addresses";
    CHECK_GE(profile_names_.size(), count) << "Not enough profile names";

    for (std::size_t i = 0; i < count; ++i) {
      Profile* unsupervised_profile = CreateUnsupervisedTestingProfile(
          email_addresses_[i], profile_names_[i]);

      // Services are lazily created, so we need to access them to force their
      // creation. That'll trigger the ::BuildSupervisedUserService factory,
      // which will bind fake content filters observers to this text fixture
      // instance.
      CHECK(SupervisedUserServiceFactory::GetInstance()->GetForProfile(
          unsupervised_profile));
    }
  }

  Profile* CreateUnsupervisedTestingProfile(const std::string& email,
                                            const std::string& profile_name) {
    // Content filters are not supported for family link supervised profiles.
    return CreateTestingProfile(email, profile_name,
                                /*is_subject_to_parental_controls=*/false,
                                /*is_opted_in_to_parental_supervision=*/false);
  }

  std::unique_ptr<ContentFiltersObserverBridge> CreateBridge(
      std::string_view setting_name,
      base::RepeatingClosure on_enabled,
      base::RepeatingClosure on_disabled) {
    std::unique_ptr<FakeContentFiltersObserverBridge> bridge =
        std::make_unique<FakeContentFiltersObserverBridge>(
            setting_name, on_enabled, on_disabled);
    if (setting_name == kBrowserContentFiltersSettingName) {
      browser_content_filters_observers_.push_back(bridge.get());
    } else if (setting_name == kSearchContentFiltersSettingName) {
      search_content_filters_observers_.push_back(bridge.get());
    }
    return bridge;
  }

  // Builds the `SupervisedUserService` and captures the content observers onto
  // this text fixture instance. Binding memory managed by foreign object (the
  // service) to raw_ptr is fine, because raw_ptr handles will be destroyed
  // before profile manager, that transitively owns the content observers.
  std::unique_ptr<KeyedService> BuildSupervisedUserService(
      content::BrowserContext* browser_context) override {
    Profile* profile = Profile::FromBrowserContext(browser_context);

    std::unique_ptr<SupervisedUserServicePlatformDelegate> platform_delegate =
        std::make_unique<SupervisedUserServicePlatformDelegate>(*profile);

    return std::make_unique<SupervisedUserService>(
        IdentityManagerFactory::GetForProfile(profile),
        profile->GetDefaultStoragePartition()
            ->GetURLLoaderFactoryForBrowserProcess(),
        *profile->GetPrefs(),
        *SupervisedUserSettingsServiceFactory::GetInstance()->GetForKey(
            profile->GetProfileKey()),
        SyncServiceFactory::GetInstance()->GetForProfile(profile),
        std::make_unique<SupervisedUserURLFilter>(
            *profile->GetPrefs(), std::make_unique<FakeURLFilterDelegate>(),
            std::make_unique<KidsChromeManagementURLCheckerClient>(
                IdentityManagerFactory::GetForProfile(profile),
                profile->GetDefaultStoragePartition()
                    ->GetURLLoaderFactoryForBrowserProcess(),
                *profile->GetPrefs(), platform_delegate->GetCountryCode(),
                platform_delegate->GetChannel())),
        std::make_unique<SupervisedUserServicePlatformDelegate>(*profile),
        base::BindRepeating(
            &FamilyLinkUserMetricsProviderWithContentFiltersTest::CreateBridge,
            base::Unretained(this)));
  }

  // Enables or disables the browser content filters for all profiles.
  void SetBrowserContentFilters(bool enabled) {
    for (FakeContentFiltersObserverBridge* observer :
         browser_content_filters_observers_) {
      observer->SetEnabled(enabled);
    }
  }

  // Enables or disables the search content filters for all profiles.
  void SetSearchContentFilters(bool enabled) {
    for (FakeContentFiltersObserverBridge* observer :
         search_content_filters_observers_) {
      observer->SetEnabled(enabled);
    }
  }

 private:
  // Required to propagate the device content filters to the supervised user
  // service. FakeContentFiltersObserverBridge which is in action here only
  // avoids creating the java bridge class, but uses prod notification patterns.
  base::test::ScopedFeatureList scoped_feature_list_{
      kPropagateDeviceContentFiltersToSupervisedUser};
  std::vector<std::string> email_addresses_{kTestEmail, kTestEmail1};
  std::vector<std::string> profile_names_{kTestProfile, kTestProfile1};

  // These are internal components of the `SupervisedUserService` and are used
  // to simulate changes to the android content filters. Both are owned by the
  // `SupervisedUserService`. The reason for they're held in vectors is that
  // content filters are applied at device level, for all profiles at once.
  std::vector<raw_ptr<FakeContentFiltersObserverBridge>>
      browser_content_filters_observers_;
  std::vector<raw_ptr<FakeContentFiltersObserverBridge>>
      search_content_filters_observers_;
};

TEST_P(FamilyLinkUserMetricsProviderWithContentFiltersTest,
       AllFiltersDisabled) {
  CreateProfiles(GetParam().profile_count);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();

  histogram_tester.ExpectBucketCount(
      kFamilyLinkUserLogSegmentHistogramName,
      FamilyLinkUserLogRecord::Segment::kUnsupervised,
      /*expected_count=*/1);
  histogram_tester.ExpectTotalCount(
      kFamilyLinkUserLogSegmentWebFilterHistogramName,
      /*expected_count=*/0);
}

TEST_P(FamilyLinkUserMetricsProviderWithContentFiltersTest,
       SearchFilterEnabled) {
  CreateProfiles(GetParam().profile_count);
  SetSearchContentFilters(true);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();

  histogram_tester.ExpectBucketCount(
      kFamilyLinkUserLogSegmentHistogramName,
      FamilyLinkUserLogRecord::Segment::kSupervisionEnabledLocally,
      /*expected_count=*/1);
  histogram_tester.ExpectBucketCount(
      kFamilyLinkUserLogSegmentWebFilterHistogramName, WebFilterType::kDisabled,
      /*expected_count=*/1);
}

TEST_P(FamilyLinkUserMetricsProviderWithContentFiltersTest,
       ContentFiltersEnabled) {
  CreateProfiles(GetParam().profile_count);
  SetBrowserContentFilters(true);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();

  histogram_tester.ExpectBucketCount(
      kFamilyLinkUserLogSegmentHistogramName,
      FamilyLinkUserLogRecord::Segment::kSupervisionEnabledLocally,
      /*expected_count=*/1);
  histogram_tester.ExpectUniqueSample(
      kFamilyLinkUserLogSegmentWebFilterHistogramName,
      WebFilterType::kTryToBlockMatureSites,
      /*expected_bucket_count=*/1);
}

TEST_P(FamilyLinkUserMetricsProviderWithContentFiltersTest, AllFiltersEnabled) {
  CreateProfiles(GetParam().profile_count);
  SetBrowserContentFilters(true);
  SetSearchContentFilters(true);

  base::HistogramTester histogram_tester;
  metrics_provider()->OnDidCreateMetricsLog();

  histogram_tester.ExpectBucketCount(
      kFamilyLinkUserLogSegmentHistogramName,
      FamilyLinkUserLogRecord::Segment::kSupervisionEnabledLocally,
      /*expected_count=*/1);
  histogram_tester.ExpectUniqueSample(
      kFamilyLinkUserLogSegmentWebFilterHistogramName,
      WebFilterType::kTryToBlockMatureSites,
      /*expected_bucket_count=*/1);
}

INSTANTIATE_TEST_SUITE_P(
    ,
    FamilyLinkUserMetricsProviderWithContentFiltersTest,
    testing::ValuesIn<ContentFiltersTestCase>({
        {1, "SingleProfile"},
        {2, "MultipleProfiles"},
    }),
    [](const testing::TestParamInfo<ContentFiltersTestCase>& info) {
      return info.param.test_name;
    });
#endif  // BUILDFLAG(IS_ANDROID)

}  // namespace
}  // namespace supervised_user