File: majority_age_user_metrics_provider_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: 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 (143 lines) | stat: -rw-r--r-- 5,282 bytes parent folder | download | duplicates (4)
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
// 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 <memory>

#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "chrome/browser/ash/login/test/guest_session_mixin.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/metrics/testing/sync_metrics_test_utils.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "components/metrics/delegating_provider.h"
#include "components/metrics/demographics/demographic_metrics_provider.h"
#include "components/metrics/demographics/demographic_metrics_test_utils.h"
#include "components/metrics/demographics/user_demographics.h"
#include "components/metrics/metrics_service.h"
#include "components/sync/test/fake_server.h"
#include "content/public/test/browser_test.h"
#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
#include "third_party/metrics_proto/system_profile.pb.h"
#include "third_party/metrics_proto/user_demographics.pb.h"

namespace chromeos {

namespace {

// Explicitly calls ProvideCurrentSessionData() for all metrics providers.
void ProvideCurrentSessionData() {
  // The purpose of the below call is to avoid a DCHECK failure in an
  // unrelated metrics provider, in
  // |FieldTrialsProvider::ProvideCurrentSessionData()|.
  metrics::SystemProfileProto system_profile_proto;
  g_browser_process->metrics_service()
      ->GetDelegatingProviderForTesting()
      ->ProvideSystemProfileMetricsWithLogCreationTime(base::TimeTicks::Now(),
                                                       &system_profile_proto);
  metrics::ChromeUserMetricsExtension uma_proto;
  g_browser_process->metrics_service()
      ->GetDelegatingProviderForTesting()
      ->ProvideCurrentSessionData(&uma_proto);
}

}  // namespace

class MajorityAgeUserMetricsProviderTest
    : public SyncTest,
      public testing::WithParamInterface</*age=*/int> {
 public:
  MajorityAgeUserMetricsProviderTest() : SyncTest(SINGLE_CLIENT) {
    scoped_feature_list_.InitAndEnableFeature(
        metrics::kDemographicMetricsReporting);
  }

  int GetAge() { return GetParam(); }

  PrefService* local_state() { return g_browser_process->local_state(); }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_P(MajorityAgeUserMetricsProviderTest,
                       IsUserOver21Under85) {
  base::HistogramTester histogram_tester;

  // Set network time for test.
  const base::Time now = base::Time::Now();
  metrics::test::UpdateNetworkTime(now,
                                   g_browser_process->network_time_tracker());

  // Compute birth year.
  base::Time::Exploded exploded_now_time;
  now.LocalExplode(&exploded_now_time);
  int test_birth_year = exploded_now_time.year - GetAge();
  metrics::UserDemographicsProto::Gender test_gender =
      metrics::UserDemographicsProto::GENDER_MALE;
  // Assign a random gender.
  if (test_birth_year % 2 == 0)
    test_gender = metrics::UserDemographicsProto::GENDER_FEMALE;

  metrics::test::AddUserBirthYearAndGenderToSyncServer(
      GetFakeServer()->AsWeakPtr(), test_birth_year, test_gender);

  // TODO(crbug.com/40688248): Try to replace the below set-up code with
  // functions from SyncTest.
  std::unique_ptr<SyncServiceImplHarness> harness =
      metrics::test::InitializeProfileForSync(browser()->profile(),
                                              GetFakeServer()->AsWeakPtr());
  ASSERT_TRUE(harness->SetupSync());

  // Simulate calling ProvideCurrentSessionData() after logging in.
  ProvideCurrentSessionData();

  const int noised_age =
      exploded_now_time.year -
      metrics::test::GetNoisedBirthYear(local_state(), test_birth_year);
  const bool is_eligible =
      noised_age > metrics::kUserDemographicsMinAgeInYears &&
      noised_age <= metrics::kUserDemographicsMaxAgeInYears;
  histogram_tester.ExpectUniqueSample(
      "UMA.UserDemographics.IsNoisedAgeOver21Under85",
      /*sample=*/is_eligible,
      /*expected_count=*/1);
}

INSTANTIATE_TEST_SUITE_P(
    ,
    MajorityAgeUserMetricsProviderTest,
    testing::Values(0, 12, 13, 14, 17, 18, 19, 20, 21, 22, 83, 84, 85, 86));

class MajorityAgeUserMetricsProviderGuestModeTest
    : public MixinBasedInProcessBrowserTest {
 public:
  MajorityAgeUserMetricsProviderGuestModeTest() {
    scoped_feature_list_.InitAndEnableFeature(
        metrics::kDemographicMetricsReporting);
  }

 private:
  ash::GuestSessionMixin guest_session_mixin_{&mixin_host_};

  base::test::ScopedFeatureList scoped_feature_list_;
};

// Tests that guest users report unknown data because they don't have an age and
// don't crash.
IN_PROC_BROWSER_TEST_F(MajorityAgeUserMetricsProviderGuestModeTest, GuestMode) {
  base::HistogramTester histogram_tester;

  ProvideCurrentSessionData();

  histogram_tester.ExpectUniqueSample(
      "UMA.UserDemographics.IsNoisedAgeOver21Under85",
      /*sample=*/false,  // unknown
      /*expected_count=*/1);
}

}  // namespace chromeos