1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/metrics/demographics/demographic_metrics_test_utils.h"
6 
7 #include "base/strings/stringprintf.h"
8 #include "base/time/default_clock.h"
9 #include "base/time/default_tick_clock.h"
10 #include "components/metrics/demographics/user_demographics.h"
11 #include "components/metrics/log_decoder.h"
12 #include "components/sync/engine_impl/loopback_server/persistent_unique_client_entity.h"
13 #include "components/sync/protocol/sync.pb.h"
14 #include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
15 
16 namespace metrics {
17 namespace test {
18 
AddUserBirthYearAndGenderToSyncServer(base::WeakPtr<fake_server::FakeServer> fake_server,int birth_year,UserDemographicsProto::Gender gender)19 void AddUserBirthYearAndGenderToSyncServer(
20     base::WeakPtr<fake_server::FakeServer> fake_server,
21     int birth_year,
22     UserDemographicsProto::Gender gender) {
23   sync_pb::EntitySpecifics specifics;
24   specifics.mutable_priority_preference()->mutable_preference()->set_name(
25       kSyncDemographicsPrefName);
26   specifics.mutable_priority_preference()->mutable_preference()->set_value(
27       base::StringPrintf("{\"birth_year\":%d,\"gender\":%d}", birth_year,
28                          static_cast<int>(gender)));
29   fake_server->InjectEntity(
30       syncer::PersistentUniqueClientEntity::CreateFromSpecificsForTesting(
31           /*non_unique_name=*/kSyncDemographicsPrefName,
32           /*client_tag=*/specifics.preference().name(), specifics,
33           /*creation_time=*/0,
34           /*last_modified_time=*/0));
35 }
36 
UpdateNetworkTime(const base::Time & now,network_time::NetworkTimeTracker * time_tracker)37 void UpdateNetworkTime(const base::Time& now,
38                        network_time::NetworkTimeTracker* time_tracker) {
39   // Simulate the latency in the network to get the network time from the remote
40   // server.
41   constexpr base::TimeDelta kLatency = base::TimeDelta::FromMilliseconds(10);
42 
43   // Simulate the time taken to call UpdateNetworkTime() since the moment the
44   // callback was created. When not testing with the fake sync server, the
45   // callback is called when doing an HTTP request to the sync server.
46   constexpr base::TimeDelta kCallbackDelay =
47       base::TimeDelta::FromMilliseconds(10);
48 
49   // Simulate a network time that is a bit earlier than the now time.
50   base::Time network_time = now - kCallbackDelay - kLatency;
51 
52   // Simulate the time in ticks at the moment the UpdateNetworkTime callback
53   // function is created, which time should be at least 1 millisecond behind the
54   // moment the callback is run to pass the DCHECK.
55   base::TimeTicks post_time = base::TimeTicks::Now() - kCallbackDelay;
56 
57   time_tracker->UpdateNetworkTime(
58       network_time, /*resolution=*/base::TimeDelta::FromMilliseconds(1),
59       kLatency, post_time);
60 }
61 
GetMaximumEligibleBirthYear(const base::Time & now)62 int GetMaximumEligibleBirthYear(const base::Time& now) {
63   constexpr int kEligibleAge = kUserDemographicsMinAgeInYears +
64                                kUserDemographicsBirthYearNoiseOffsetRange;
65 
66   base::Time::Exploded exploded_time;
67   now.UTCExplode(&exploded_time);
68 
69   // Return the maximum birth year that is eligible for reporting the user's
70   // birth year and gender. The -1 year is the extra buffer that Sync uses to
71   // make sure that the user is at least 20 years old because the user gives
72   // only the year of their birth date. E.g., if today's date is 05 Jan 2020
73   // and the user was born 05 Mar 2000, the user's age would be computed as 20
74   // years old using the year resolution, but the user is in fact 19.
75   return exploded_time.year - kEligibleAge - 1;
76 }
77 
GetNoisedBirthYear(const PrefService & pref_service,int raw_birth_year)78 int GetNoisedBirthYear(const PrefService& pref_service, int raw_birth_year) {
79   int birth_year_offset =
80       pref_service.GetInteger(kSyncDemographicsBirthYearOffsetPrefName);
81   return birth_year_offset + raw_birth_year;
82 }
83 
BuildAndStoreLog(MetricsService * metrics_service)84 void BuildAndStoreLog(MetricsService* metrics_service) {
85   metrics_service->StageCurrentLogForTest();
86 }
87 
HasUnsentLogs(MetricsService * metrics_service)88 bool HasUnsentLogs(MetricsService* metrics_service) {
89   return metrics_service->LogStoreForTest()->has_unsent_logs();
90 }
91 
92 // Returns an UMA log if the MetricsService has a staged log.
GetLastUmaLog(MetricsService * metrics_service)93 std::unique_ptr<ChromeUserMetricsExtension> GetLastUmaLog(
94     MetricsService* metrics_service) {
95   // Decompress and deserialize the staged log.
96   std::unique_ptr<ChromeUserMetricsExtension> log =
97       std::make_unique<ChromeUserMetricsExtension>();
98   if (!DecodeLogDataToProto(metrics_service->LogStoreForTest()->staged_log(),
99                             log.get())) {
100     return nullptr;
101   }
102   return log;
103 }
104 
105 }  // namespace test
106 }  // namespace metrics
107