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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
// Copyright 2017 The Chromium Authors
// 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/scoped_observation.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/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace variations {
namespace {
using ::testing::_;
using ::testing::Contains;
class MockSyntheticTrialObserver : public SyntheticTrialObserver {
public:
MockSyntheticTrialObserver() = default;
~MockSyntheticTrialObserver() override = default;
MOCK_METHOD(void,
OnSyntheticTrialsChanged,
(const std::vector<SyntheticTrialGroup>&,
const std::vector<SyntheticTrialGroup>&,
const std::vector<SyntheticTrialGroup>&),
(override));
};
} // namespace
bool operator==(const SyntheticTrialGroup& a, const SyntheticTrialGroup& b) {
return a.group_name() == b.group_name() && a.trial_name() == b.trial_name() &&
a.annotation_mode() == b.annotation_mode();
}
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("TestTrial1", "Group1",
SyntheticTrialAnnotationMode::kNextLog);
registry.RegisterSyntheticFieldTrial(trial1_group1);
SyntheticTrialGroup trial2_group2("TestTrial2", "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("TestTrial1", "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("TestTrial3", "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(
"TestTrial3", "Group3", SyntheticTrialAnnotationMode::kCurrentLog);
SyntheticTrialGroup trial4_group4_current_log(
"TestTrial4", "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, GetSyntheticFieldTrialsOlderThanSuffix) {
SyntheticTrialRegistry registry;
SyntheticTrialGroup trial("TestTrial", "Group",
SyntheticTrialAnnotationMode::kCurrentLog);
registry.RegisterSyntheticFieldTrial(trial);
std::vector<ActiveGroupId> synthetic_trials;
// Get list of synthetic trials, but with no added suffixes to the trial and
// group names.
registry.GetSyntheticFieldTrialsOlderThan(base::TimeTicks::Now(),
&synthetic_trials);
ASSERT_EQ(1U, synthetic_trials.size());
EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial", "Group"));
// Get list of synthetic trials, but with "UKM" suffixed to the trial and
// group names.
registry.GetSyntheticFieldTrialsOlderThan(base::TimeTicks::Now(),
&synthetic_trials, "UKM");
ASSERT_EQ(1U, synthetic_trials.size());
EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrialUKM", "GroupUKM"));
}
TEST_F(SyntheticTrialRegistryTest, RegisterExternalExperiments_NoAllowlist) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(
internal::kExternalExperimentAllowlist);
SyntheticTrialRegistry registry;
const auto mode = SyntheticTrialRegistry::kOverrideExistingIds;
registry.RegisterExternalExperiments({100, 200}, mode);
std::vector<ActiveGroupId> synthetic_trials;
GetSyntheticTrials(registry, &synthetic_trials);
EXPECT_EQ(0U, synthetic_trials.size());
registry.RegisterExternalExperiments({500}, 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 auto override_mode = SyntheticTrialRegistry::kOverrideExistingIds;
SyntheticTrialRegistry registry;
std::vector<ActiveGroupId> synthetic_trials;
// Register a synthetic trial TestTrial1 with groups A and B.
registry.RegisterExternalExperiments({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({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({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({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({}, 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({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.AddObserver(SyntheticTrialsActiveGroupIdProvider::GetInstance());
// Add two synthetic trials and confirm that they show up in the list.
SyntheticTrialGroup trial1("TestTrial1", "Group1",
SyntheticTrialAnnotationMode::kNextLog);
registry.RegisterSyntheticFieldTrial(trial1);
SyntheticTrialGroup trial2("TestTrial2", "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());
ActiveGroupId trial1_id = trial1.id();
std::string trial1_hash =
base::StringPrintf("%x-%x", trial1_id.name, trial1_id.group);
EXPECT_TRUE(base::Contains(output, trial1_hash));
ActiveGroupId trial2_id = trial2.id();
std::string trial2_hash =
base::StringPrintf("%x-%x", trial2_id.name, trial2.id().group);
EXPECT_TRUE(base::Contains(output, trial2_hash));
}
TEST_F(SyntheticTrialRegistryTest, NotifyObserver) {
SyntheticTrialRegistry registry;
MockSyntheticTrialObserver observer;
registry.AddObserver(&observer);
// A brand new synthetic trial is registered. Observers should be notified.
SyntheticTrialGroup trial1("TestTrial1", "Group1",
SyntheticTrialAnnotationMode::kNextLog);
EXPECT_CALL(observer, OnSyntheticTrialsChanged(
std::vector<SyntheticTrialGroup>({trial1}),
std::vector<SyntheticTrialGroup>(),
std::vector<SyntheticTrialGroup>({trial1})));
registry.RegisterSyntheticFieldTrial(trial1);
// A brand new synthetic trial is registered. Observers should be notified.
SyntheticTrialGroup trial2("TestTrial2", "Group1",
SyntheticTrialAnnotationMode::kNextLog);
EXPECT_CALL(observer,
OnSyntheticTrialsChanged(
std::vector<SyntheticTrialGroup>({trial2}),
std::vector<SyntheticTrialGroup>(),
std::vector<SyntheticTrialGroup>({trial1, trial2})));
registry.RegisterSyntheticFieldTrial(trial2);
// The group of a trial has changed. Observers should be notified.
SyntheticTrialGroup trial3("TestTrial1", "Group2",
SyntheticTrialAnnotationMode::kNextLog);
EXPECT_CALL(observer,
OnSyntheticTrialsChanged(
std::vector<SyntheticTrialGroup>({trial3}),
std::vector<SyntheticTrialGroup>(),
std::vector<SyntheticTrialGroup>({trial3, trial2})));
registry.RegisterSyntheticFieldTrial(trial3);
// The annotation mode of a trial has changed. Observers should be notified.
SyntheticTrialGroup trial4("TestTrial1", "Group2",
SyntheticTrialAnnotationMode::kCurrentLog);
EXPECT_CALL(observer,
OnSyntheticTrialsChanged(
std::vector<SyntheticTrialGroup>({trial4}),
std::vector<SyntheticTrialGroup>(),
std::vector<SyntheticTrialGroup>({trial4, trial2})));
registry.RegisterSyntheticFieldTrial(trial4);
// A synthetic trial is re-registered with no changes. Observers should NOT be
// notified.
EXPECT_CALL(observer, OnSyntheticTrialsChanged(_, _, _)).Times(0);
registry.RegisterSyntheticFieldTrial(trial4);
registry.RemoveObserver(&observer);
}
TEST_F(SyntheticTrialRegistryTest, NotifyObserverExternalTrials) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
internal::kExternalExperimentAllowlist,
{{"100", "A"}, {"101", "B"}, {"102", "C"}});
SyntheticTrialRegistry registry;
MockSyntheticTrialObserver observer;
registry.AddObserver(&observer);
const auto mode = SyntheticTrialRegistry::kOverrideExistingIds;
const SyntheticTrialGroup kTrial1("A", "100",
SyntheticTrialAnnotationMode::kNextLog);
const SyntheticTrialGroup kTrial2("B", "101",
SyntheticTrialAnnotationMode::kNextLog);
const SyntheticTrialGroup kTrial3("C", "102",
SyntheticTrialAnnotationMode::kNextLog);
// A trial that's not in the allowlist.
const SyntheticTrialGroup kTrial4("D", "103",
SyntheticTrialAnnotationMode::kNextLog);
EXPECT_CALL(observer,
OnSyntheticTrialsChanged(
std::vector<SyntheticTrialGroup>({kTrial1, kTrial2}),
std::vector<SyntheticTrialGroup>(),
std::vector<SyntheticTrialGroup>({kTrial1, kTrial2})));
registry.RegisterExternalExperiments({100, 101}, mode);
// Registering one trial with override should remove existing trials.
EXPECT_CALL(observer,
OnSyntheticTrialsChanged(
std::vector<SyntheticTrialGroup>({kTrial3}),
std::vector<SyntheticTrialGroup>({kTrial1, kTrial2}),
std::vector<SyntheticTrialGroup>({kTrial3})));
registry.RegisterExternalExperiments({102}, mode);
registry.RemoveObserver(&observer);
}
} // namespace variations
|