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.
5 #include "components/metrics/field_trials_provider.h"
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"
16 using ActiveGroup = base::FieldTrial::ActiveGroup;
18 namespace variations {
22 constexpr const char* kSuffix = "UKM";
24 const ActiveGroup kFieldTrials[] = {{"Trial1", "Group1"},
26 {"Trial3", "Group3"}};
27 const ActiveGroup kSyntheticFieldTrials[] = {{"Synthetic1", "SyntheticGroup1"},
28 {"Synthetic2", "SyntheticGroup2"}};
30 ActiveGroupId ToActiveGroupId(ActiveGroup active_group,
31 std::string suffix = "");
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)};
47 // Check that the field trials in |system_profile| correspond to |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());
59 ActiveGroupId ToActiveGroupId(ActiveGroup active_group, std::string suffix) {
60 return MakeActiveGroupId(active_group.trial_name + suffix,
61 active_group.group_name + suffix);
66 class FieldTrialsProviderTest : public ::testing::Test {
68 FieldTrialsProviderTest() { scope_.InitWithEmptyFeatureAndFieldTrialLists(); }
70 ~FieldTrialsProviderTest() override = default;
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();
83 // Register trials which should get recorded.
84 void RegisterExpectedSyntheticTrials() {
85 for (const ActiveGroup& trial : kSyntheticFieldTrials) {
86 registry_.RegisterSyntheticFieldTrial(SyntheticTrialGroup(
87 trial.trial_name, trial.group_name,
89 variations::SyntheticTrialAnnotationMode::kNextLog));
92 // Register trial which shouldn't get recorded.
93 void RegisterExtraSyntheticTrial() {
94 registry_.RegisterSyntheticFieldTrial(SyntheticTrialGroup(
95 "ExtraSynthetic", "ExtraGroup",
97 variations::SyntheticTrialAnnotationMode::kNextLog));
100 // Waits until base::TimeTicks::Now() no longer equals |value|. This should
101 // take between 1-15ms per the documented resolution of base::TimeTicks.
102 void WaitUntilTimeChanges(const base::TimeTicks& value) {
103 while (base::TimeTicks::Now() == value) {
104 base::PlatformThread::Sleep(base::Milliseconds(1));
108 SyntheticTrialRegistry registry_;
109 base::test::ScopedFeatureList scope_;
112 TEST_F(FieldTrialsProviderTest, ProvideSyntheticTrials) {
113 FieldTrialsProvider provider(®istry_, base::StringPiece());
115 RegisterExpectedSyntheticTrials();
116 // Make sure these trials are older than the log.
117 WaitUntilTimeChanges(base::TimeTicks::Now());
119 // Get the current time and wait for it to change.
120 base::TimeTicks log_creation_time = base::TimeTicks::Now();
122 // Make sure that the log is older than the trials that should be excluded.
123 WaitUntilTimeChanges(log_creation_time);
125 RegisterExtraSyntheticTrial();
127 metrics::SystemProfileProto proto;
128 provider.ProvideSystemProfileMetricsWithLogCreationTime(log_creation_time,
131 EXPECT_EQ(std::size(kAllTrialIds),
132 static_cast<size_t>(proto.field_trial_size()));
133 CheckFieldTrialsInSystemProfile(proto, kAllTrialIds);
136 TEST_F(FieldTrialsProviderTest, NoSyntheticTrials) {
137 FieldTrialsProvider provider(nullptr, base::StringPiece());
139 metrics::SystemProfileProto proto;
140 provider.ProvideSystemProfileMetricsWithLogCreationTime(base::TimeTicks(),
143 EXPECT_EQ(std::size(kFieldTrialIds),
144 static_cast<size_t>(proto.field_trial_size()));
145 CheckFieldTrialsInSystemProfile(proto, kFieldTrialIds);
148 TEST_F(FieldTrialsProviderTest, ProvideCurrentSessionData) {
149 metrics::ChromeUserMetricsExtension uma_log;
150 uma_log.system_profile();
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);
160 FieldTrialsProvider provider(®istry_, base::StringPiece());
161 RegisterExpectedSyntheticTrials();
162 WaitUntilTimeChanges(base::TimeTicks::Now());
163 provider.SetLogCreationTimeForTesting(base::TimeTicks::Now());
165 provider.ProvideCurrentSessionData(&uma_log);
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);
172 TEST_F(FieldTrialsProviderTest, GetAndWriteFieldTrialsWithSuffixes) {
173 metrics::ChromeUserMetricsExtension uma_log;
174 uma_log.system_profile();
176 FieldTrialsProvider provider(®istry_, kSuffix);
177 RegisterExpectedSyntheticTrials();
178 WaitUntilTimeChanges(base::TimeTicks::Now());
179 provider.SetLogCreationTimeForTesting(base::TimeTicks::Now());
181 provider.ProvideCurrentSessionData(&uma_log);
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);
189 } // namespace variations