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