summaryrefslogtreecommitdiff
path: root/chromium/components/variations/synthetic_trial_registry_unittest.cc
blob: caf67799bf0b3ec137bfc15fdef71aaf38db3d78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/variations/synthetic_trial_registry.h"

#include <string>

#include "base/containers/contains.h"
#include "base/metrics/field_trial.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/variations/active_field_trials.h"
#include "components/variations/hashing.h"
#include "components/variations/synthetic_trials.h"
#include "components/variations/synthetic_trials_active_group_id_provider.h"
#include "components/variations/variations_crash_keys.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace variations {

class SyntheticTrialRegistryTest : public ::testing::Test {
 public:
  SyntheticTrialRegistryTest() { InitCrashKeys(); }

  SyntheticTrialRegistryTest(const SyntheticTrialRegistryTest&) = delete;
  SyntheticTrialRegistryTest& operator=(const SyntheticTrialRegistryTest&) =
      delete;

  ~SyntheticTrialRegistryTest() override { ClearCrashKeysInstanceForTesting(); }

  // Returns true if there is a synthetic trial in the given vector that matches
  // the given trial name and trial group; returns false otherwise.
  bool HasSyntheticTrial(const std::vector<ActiveGroupId>& synthetic_trials,
                         const std::string& trial_name,
                         const std::string& trial_group) {
    uint32_t trial_name_hash = HashName(trial_name);
    uint32_t trial_group_hash = HashName(trial_group);
    for (const ActiveGroupId& trial : synthetic_trials) {
      if (trial.name == trial_name_hash && trial.group == trial_group_hash)
        return true;
    }
    return false;
  }

  // Waits until base::TimeTicks::Now() no longer equals |value|. This should
  // take between 1-15ms per the documented resolution of base::TimeTicks.
  void WaitUntilTimeChanges(const base::TimeTicks& value) {
    while (base::TimeTicks::Now() == value) {
      base::PlatformThread::Sleep(base::Milliseconds(1));
    }
  }

  // Gets the current synthetic trials.
  void GetSyntheticTrials(const SyntheticTrialRegistry& registry,
                          std::vector<ActiveGroupId>* synthetic_trials) {
    // Ensure that time has advanced by at least a tick before proceeding.
    WaitUntilTimeChanges(base::TimeTicks::Now());
    registry.GetSyntheticFieldTrialsOlderThan(base::TimeTicks::Now(),
                                              synthetic_trials);
  }

 private:
  base::test::TaskEnvironment task_environment_;
};

TEST_F(SyntheticTrialRegistryTest, RegisterSyntheticTrial) {
  SyntheticTrialRegistry registry;

  // Add two synthetic trials and confirm that they show up in the list.
  SyntheticTrialGroup trial1_group1(HashName("TestTrial1"), HashName("Group1"),
                                    SyntheticTrialAnnotationMode::kNextLog);
  registry.RegisterSyntheticFieldTrial(trial1_group1);

  SyntheticTrialGroup trial2_group2(HashName("TestTrial2"), HashName("Group2"),
                                    SyntheticTrialAnnotationMode::kNextLog);
  registry.RegisterSyntheticFieldTrial(trial2_group2);
  // Ensure that time has advanced by at least a tick before proceeding.
  WaitUntilTimeChanges(base::TimeTicks::Now());

  // Save the time when the log was started (it's okay for this to be greater
  // than the time recorded by the above call since it's used to ensure the
  // value changes).
  const base::TimeTicks begin_log_time = base::TimeTicks::Now();

  std::vector<ActiveGroupId> synthetic_trials;
  registry.GetSyntheticFieldTrialsOlderThan(base::TimeTicks::Now(),
                                            &synthetic_trials);
  EXPECT_EQ(2U, synthetic_trials.size());
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group1"));
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));

  // Ensure that time has advanced by at least a tick before proceeding.
  WaitUntilTimeChanges(begin_log_time);

  // Change the group for the first trial after the log started.
  SyntheticTrialGroup trial1_group2(HashName("TestTrial1"), HashName("Group2"),
                                    SyntheticTrialAnnotationMode::kNextLog);
  registry.RegisterSyntheticFieldTrial(trial1_group2);
  registry.GetSyntheticFieldTrialsOlderThan(begin_log_time, &synthetic_trials);
  EXPECT_EQ(1U, synthetic_trials.size());
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));

  // Add a new trial after the log started and confirm that it doesn't show up.
  SyntheticTrialGroup trial3_group3(HashName("TestTrial3"), HashName("Group3"),
                                    SyntheticTrialAnnotationMode::kNextLog);
  registry.RegisterSyntheticFieldTrial(trial3_group3);
  registry.GetSyntheticFieldTrialsOlderThan(begin_log_time, &synthetic_trials);
  EXPECT_EQ(1U, synthetic_trials.size());
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));

  // Change TestTrial3 to be active immediately, add a new trial that also is
  // active immediately, and confirm they both show up despite being added after
  // the log started.
  SyntheticTrialGroup trial3_group3_current_log(
      HashName("TestTrial3"), HashName("Group3"),
      SyntheticTrialAnnotationMode::kCurrentLog);
  SyntheticTrialGroup trial4_group4_current_log(
      HashName("TestTrial4"), HashName("Group4"),
      SyntheticTrialAnnotationMode::kCurrentLog);
  registry.RegisterSyntheticFieldTrial(trial3_group3_current_log);
  registry.RegisterSyntheticFieldTrial(trial4_group4_current_log);
  registry.GetSyntheticFieldTrialsOlderThan(begin_log_time, &synthetic_trials);
  ASSERT_EQ(3U, synthetic_trials.size());
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3"));
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial4", "Group4"));

  // Start a new log and ensure all four trials appear in it.
  GetSyntheticTrials(registry, &synthetic_trials);
  ASSERT_EQ(4U, synthetic_trials.size());
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2"));
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2"));
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3"));
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial4", "Group4"));
}

TEST_F(SyntheticTrialRegistryTest, RegisterExternalExperiments_NoAllowlist) {
  SyntheticTrialRegistry registry(false);
  const std::string context = "TestTrial1";
  const auto mode = SyntheticTrialRegistry::kOverrideExistingIds;

  registry.RegisterExternalExperiments(context, {100, 200}, mode);
  std::vector<ActiveGroupId> synthetic_trials;
  GetSyntheticTrials(registry, &synthetic_trials);
  EXPECT_EQ(2U, synthetic_trials.size());
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "100"));
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "200"));

  // Change the group for the trial to a single group.
  registry.RegisterExternalExperiments(context, {500}, mode);
  GetSyntheticTrials(registry, &synthetic_trials);
  EXPECT_EQ(1U, synthetic_trials.size());
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "500"));

  // Register a trial with no groups, which should effectively remove the trial.
  registry.RegisterExternalExperiments(context, {}, mode);
  GetSyntheticTrials(registry, &synthetic_trials);
  EXPECT_EQ(0U, synthetic_trials.size());
}

TEST_F(SyntheticTrialRegistryTest, RegisterExternalExperiments_WithAllowlist) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      internal::kExternalExperimentAllowlist,
      {{"100", "A"}, {"101", "A"}, {"300", "C,xyz"}});

  const std::string context = "Test";
  const auto override_mode = SyntheticTrialRegistry::kOverrideExistingIds;
  SyntheticTrialRegistry registry;
  std::vector<ActiveGroupId> synthetic_trials;

  // Register a synthetic trial TestTrial1 with groups A and B.
  registry.RegisterExternalExperiments(context, {100, 200, 300}, override_mode);
  GetSyntheticTrials(registry, &synthetic_trials);
  EXPECT_EQ(2U, synthetic_trials.size());
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "A", "100"));
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "C", "300"));

  // A new call that only contains 100 will clear the other ones.
  registry.RegisterExternalExperiments(context, {101}, override_mode);
  GetSyntheticTrials(registry, &synthetic_trials);
  EXPECT_EQ(1U, synthetic_trials.size());
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "A", "101"));

  const auto dont_override = SyntheticTrialRegistry::kDoNotOverrideExistingIds;
  // Now, register another id that doesn't exist with kDoNotOverrideExistingIds.
  registry.RegisterExternalExperiments(context, {300}, dont_override);
  GetSyntheticTrials(registry, &synthetic_trials);
  EXPECT_EQ(2U, synthetic_trials.size());
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "A", "101"));
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "C", "300"));

  // Registering 100, which already has a trial A registered, shouldn't work.
  registry.RegisterExternalExperiments(context, {100}, dont_override);
  GetSyntheticTrials(registry, &synthetic_trials);
  EXPECT_EQ(2U, synthetic_trials.size());
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "A", "101"));
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "C", "300"));

  // Registering an empty set should also do nothing.
  registry.RegisterExternalExperiments(context, {}, dont_override);
  GetSyntheticTrials(registry, &synthetic_trials);
  EXPECT_EQ(2U, synthetic_trials.size());
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "A", "101"));
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "C", "300"));

  // Registering with an override should reset existing ones.
  registry.RegisterExternalExperiments(context, {100}, override_mode);
  GetSyntheticTrials(registry, &synthetic_trials);
  EXPECT_EQ(1U, synthetic_trials.size());
  EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "A", "100"));
}

TEST_F(SyntheticTrialRegistryTest, GetSyntheticFieldTrialActiveGroups) {
  SyntheticTrialRegistry registry;

  // Instantiate and set up the corresponding singleton observer which tracks
  // the creation of all SyntheticTrialGroups.
  registry.AddSyntheticTrialObserver(
      SyntheticTrialsActiveGroupIdProvider::GetInstance());

  // Add two synthetic trials and confirm that they show up in the list.
  SyntheticTrialGroup trial1(HashName("TestTrial1"), HashName("Group1"),
                             SyntheticTrialAnnotationMode::kNextLog);
  registry.RegisterSyntheticFieldTrial(trial1);

  SyntheticTrialGroup trial2(HashName("TestTrial2"), HashName("Group2"),
                             SyntheticTrialAnnotationMode::kNextLog);
  registry.RegisterSyntheticFieldTrial(trial2);

  // Ensure that time has advanced by at least a tick before proceeding.
  WaitUntilTimeChanges(base::TimeTicks::Now());

  // Now get the list of currently active groups.
  std::vector<std::string> output;
  GetSyntheticTrialGroupIdsAsString(&output);
  EXPECT_EQ(2U, output.size());

  std::string trial1_hash =
      base::StringPrintf("%x-%x", trial1.id.name, trial1.id.group);
  EXPECT_TRUE(base::Contains(output, trial1_hash));

  std::string trial2_hash =
      base::StringPrintf("%x-%x", trial2.id.name, trial2.id.group);
  EXPECT_TRUE(base::Contains(output, trial2_hash));
}

}  // namespace variations