1 // Copyright 2017 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 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include "components/metrics/field_trials_provider.h"
11
12 #include <string_view>
13
14 #include "base/metrics/field_trial.h"
15 #include "base/test/scoped_feature_list.h"
16 #include "base/threading/platform_thread.h"
17 #include "components/variations/active_field_trials.h"
18 #include "components/variations/synthetic_trial_registry.h"
19 #include "components/variations/synthetic_trials.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/metrics_proto/system_profile.pb.h"
22
23 using ActiveGroup = base::FieldTrial::ActiveGroup;
24
25 namespace variations {
26
27 namespace {
28
29 constexpr const char* kSuffix = "UKM";
30
31 const ActiveGroup kFieldTrials[] = {{"Trial1", "Group1"},
32 {"Trial2", "Group2"},
33 {"Trial3", "Group3"}};
34 const ActiveGroup kSyntheticFieldTrials[] = {{"Synthetic1", "SyntheticGroup1"},
35 {"Synthetic2", "SyntheticGroup2"}};
36
37 ActiveGroupId ToActiveGroupId(ActiveGroup active_group,
38 std::string suffix = "");
39
40 const ActiveGroupId kFieldTrialIds[] = {ToActiveGroupId(kFieldTrials[0]),
41 ToActiveGroupId(kFieldTrials[1]),
42 ToActiveGroupId(kFieldTrials[2])};
43 const ActiveGroupId kAllTrialIds[] = {
44 ToActiveGroupId(kFieldTrials[0]), ToActiveGroupId(kFieldTrials[1]),
45 ToActiveGroupId(kFieldTrials[2]), ToActiveGroupId(kSyntheticFieldTrials[0]),
46 ToActiveGroupId(kSyntheticFieldTrials[1])};
47 const ActiveGroupId kAllTrialIdsWithSuffixes[] = {
48 ToActiveGroupId(kFieldTrials[0], kSuffix),
49 ToActiveGroupId(kFieldTrials[1], kSuffix),
50 ToActiveGroupId(kFieldTrials[2], kSuffix),
51 ToActiveGroupId(kSyntheticFieldTrials[0], kSuffix),
52 ToActiveGroupId(kSyntheticFieldTrials[1], kSuffix)};
53
54 // Check that the field trials in |system_profile| correspond to |expected|.
CheckFieldTrialsInSystemProfile(const metrics::SystemProfileProto & system_profile,const ActiveGroupId * expected)55 void CheckFieldTrialsInSystemProfile(
56 const metrics::SystemProfileProto& system_profile,
57 const ActiveGroupId* expected) {
58 for (int i = 0; i < system_profile.field_trial_size(); ++i) {
59 const metrics::SystemProfileProto::FieldTrial& field_trial =
60 system_profile.field_trial(i);
61 EXPECT_EQ(expected[i].name, field_trial.name_id());
62 EXPECT_EQ(expected[i].group, field_trial.group_id());
63 }
64 }
65
ToActiveGroupId(ActiveGroup active_group,std::string suffix)66 ActiveGroupId ToActiveGroupId(ActiveGroup active_group, std::string suffix) {
67 return MakeActiveGroupId(active_group.trial_name + suffix,
68 active_group.group_name + suffix);
69 }
70
71 } // namespace
72
73 class FieldTrialsProviderTest : public ::testing::Test {
74 public:
FieldTrialsProviderTest()75 FieldTrialsProviderTest() { scope_.InitWithEmptyFeatureAndFieldTrialLists(); }
76
77 ~FieldTrialsProviderTest() override = default;
78
79 protected:
SetUp()80 void SetUp() override {
81 // Register the field trials.
82 for (const ActiveGroup& trial : kFieldTrials) {
83 base::FieldTrial* field_trial = base::FieldTrialList::CreateFieldTrial(
84 trial.trial_name, trial.group_name);
85 // Call Activate() to finalize and mark the field trial as active.
86 field_trial->Activate();
87 }
88 }
89
90 // Register trials which should get recorded.
RegisterExpectedSyntheticTrials()91 void RegisterExpectedSyntheticTrials() {
92 for (const ActiveGroup& trial : kSyntheticFieldTrials) {
93 registry_.RegisterSyntheticFieldTrial(SyntheticTrialGroup(
94 trial.trial_name, trial.group_name,
95 /*annotation_mode=*/
96 variations::SyntheticTrialAnnotationMode::kNextLog));
97 }
98 }
99 // Register trial which shouldn't get recorded.
RegisterExtraSyntheticTrial()100 void RegisterExtraSyntheticTrial() {
101 registry_.RegisterSyntheticFieldTrial(SyntheticTrialGroup(
102 "ExtraSynthetic", "ExtraGroup",
103 /*annotation_mode=*/
104 variations::SyntheticTrialAnnotationMode::kNextLog));
105 }
106
107 // Waits until base::TimeTicks::Now() no longer equals |value|. This should
108 // take between 1-15ms per the documented resolution of base::TimeTicks.
WaitUntilTimeChanges(const base::TimeTicks & value)109 void WaitUntilTimeChanges(const base::TimeTicks& value) {
110 while (base::TimeTicks::Now() == value) {
111 base::PlatformThread::Sleep(base::Milliseconds(1));
112 }
113 }
114
115 SyntheticTrialRegistry registry_;
116 base::test::ScopedFeatureList scope_;
117 };
118
TEST_F(FieldTrialsProviderTest,ProvideSyntheticTrials)119 TEST_F(FieldTrialsProviderTest, ProvideSyntheticTrials) {
120 FieldTrialsProvider provider(®istry_, std::string_view());
121
122 RegisterExpectedSyntheticTrials();
123 // Make sure these trials are older than the log.
124 WaitUntilTimeChanges(base::TimeTicks::Now());
125
126 // Get the current time and wait for it to change.
127 base::TimeTicks log_creation_time = base::TimeTicks::Now();
128
129 // Make sure that the log is older than the trials that should be excluded.
130 WaitUntilTimeChanges(log_creation_time);
131
132 RegisterExtraSyntheticTrial();
133
134 metrics::SystemProfileProto proto;
135 provider.ProvideSystemProfileMetricsWithLogCreationTime(log_creation_time,
136 &proto);
137
138 EXPECT_EQ(std::size(kAllTrialIds),
139 static_cast<size_t>(proto.field_trial_size()));
140 CheckFieldTrialsInSystemProfile(proto, kAllTrialIds);
141 }
142
TEST_F(FieldTrialsProviderTest,NoSyntheticTrials)143 TEST_F(FieldTrialsProviderTest, NoSyntheticTrials) {
144 FieldTrialsProvider provider(nullptr, std::string_view());
145
146 metrics::SystemProfileProto proto;
147 provider.ProvideSystemProfileMetricsWithLogCreationTime(base::TimeTicks(),
148 &proto);
149
150 EXPECT_EQ(std::size(kFieldTrialIds),
151 static_cast<size_t>(proto.field_trial_size()));
152 CheckFieldTrialsInSystemProfile(proto, kFieldTrialIds);
153 }
154
TEST_F(FieldTrialsProviderTest,ProvideCurrentSessionData)155 TEST_F(FieldTrialsProviderTest, ProvideCurrentSessionData) {
156 metrics::ChromeUserMetricsExtension uma_log;
157 uma_log.system_profile();
158
159 // {1, 1} should not be in the resulting proto as ProvideCurrentSessionData()
160 // clears existing trials and sets the trials to be those determined by
161 // GetSyntheticFieldTrialsOlderThan() and GetFieldTrialIds().
162 metrics::SystemProfileProto::FieldTrial* trial =
163 uma_log.mutable_system_profile()->add_field_trial();
164 trial->set_name_id(1);
165 trial->set_group_id(1);
166
167 FieldTrialsProvider provider(®istry_, std::string_view());
168 RegisterExpectedSyntheticTrials();
169 WaitUntilTimeChanges(base::TimeTicks::Now());
170 provider.SetLogCreationTimeForTesting(base::TimeTicks::Now());
171
172 provider.ProvideCurrentSessionData(&uma_log);
173
174 EXPECT_EQ(std::size(kAllTrialIds),
175 static_cast<size_t>(uma_log.system_profile().field_trial_size()));
176 CheckFieldTrialsInSystemProfile(uma_log.system_profile(), kAllTrialIds);
177 }
178
TEST_F(FieldTrialsProviderTest,GetAndWriteFieldTrialsWithSuffixes)179 TEST_F(FieldTrialsProviderTest, GetAndWriteFieldTrialsWithSuffixes) {
180 metrics::ChromeUserMetricsExtension uma_log;
181 uma_log.system_profile();
182
183 FieldTrialsProvider provider(®istry_, kSuffix);
184 RegisterExpectedSyntheticTrials();
185 WaitUntilTimeChanges(base::TimeTicks::Now());
186 provider.SetLogCreationTimeForTesting(base::TimeTicks::Now());
187
188 provider.ProvideCurrentSessionData(&uma_log);
189
190 EXPECT_EQ(std::size(kAllTrialIdsWithSuffixes),
191 static_cast<size_t>(uma_log.system_profile().field_trial_size()));
192 CheckFieldTrialsInSystemProfile(uma_log.system_profile(),
193 kAllTrialIdsWithSuffixes);
194 }
195
196 } // namespace variations
197