xref: /aosp_15_r20/external/cronet/components/metrics/demographics/demographic_metrics_test_utils.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_METRICS_DEMOGRAPHICS_DEMOGRAPHIC_METRICS_TEST_UTILS_H_
6 #define COMPONENTS_METRICS_DEMOGRAPHICS_DEMOGRAPHIC_METRICS_TEST_UTILS_H_
7 
8 #include <memory>
9 
10 #include "base/memory/weak_ptr.h"
11 #include "base/time/time.h"
12 #include "components/metrics/metrics_log_store.h"
13 #include "components/metrics/metrics_service.h"
14 #include "components/network_time/network_time_tracker.h"
15 #include "components/prefs/pref_service.h"
16 #include "components/sync/test/fake_server.h"
17 #include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
18 #include "third_party/metrics_proto/user_demographics.pb.h"
19 
20 // Helpers to support testing the reporting of user demographic metrics in
21 // browser tests.
22 
23 namespace metrics {
24 namespace test {
25 
26 // Parameters for the parameterized tests.
27 struct DemographicsTestParams {
28   // Enable the feature to report the user's birth year and gender.
29   bool enable_feature = false;
30   // Expectation for the user's noised birth year and gender to be reported.
31   // Having |enable_feature| set to true does not necessarily mean that
32   // |expect_reported_demographics| will be true because other conditions might
33   // stop the reporting of the user's noised birth year and gender, e.g.,
34   // sync is turned off.
35   bool expect_reported_demographics = false;
36 };
37 
38 // Adds the User Demographic priority pref to the sync |fake_server|, which
39 // contains the synced test user's raw, i.e. un-noised, |birth_year| and
40 // |gender|.
41 void AddUserBirthYearAndGenderToSyncServer(
42     base::WeakPtr<fake_server::FakeServer> fake_server,
43     int birth_year,
44     UserDemographicsProto::Gender gender);
45 
46 // Updates the network time to approximately |now|.
47 void UpdateNetworkTime(const base::Time& now,
48                        network_time::NetworkTimeTracker* time_tracker);
49 
50 // Returns the maximum eligible birth year for the given time. The returned year
51 // is inclusive; i.e. years <= the returned year are eligible. In  order to
52 // compute the synced test user's age, the network time should have already been
53 // set to |now|.
54 int GetMaximumEligibleBirthYear(const base::Time& now);
55 
56 // Gets the noised birth year of the user, where the |raw_birth_year|
57 // is the true birth year, pre-noise, and |local_state| is the service with the
58 // user's noise pref. This function should be run only after a
59 // DemographicMetricsProvider has provided user demographics to a report.
60 int GetNoisedBirthYear(const PrefService* local_state, int raw_birth_year);
61 
62 // If data are available, creates an UMA log and stores it in the
63 // MetricsService's MetricsLogStore.
64 void BuildAndStoreLog(MetricsService* metrics_service);
65 
66 // Returns true if |metrics_service|'s log store has logs to send.
67 bool HasUnsentLogs(MetricsService* metrics_service);
68 
69 // Returns an UMA log if the MetricsService has a staged log.
70 std::unique_ptr<ChromeUserMetricsExtension> GetLastUmaLog(
71     MetricsService* metrics_service);
72 
73 }  // namespace test
74 }  // namespace metrics
75 
76 #endif  // COMPONENTS_METRICS_DEMOGRAPHICS_DEMOGRAPHIC_METRICS_TEST_UTILS_H_
77