blob: 6a91b69443cd9723029af6cf53d849f24c8bdde3 [file] [log] [blame]
[email protected]7e797792013-08-05 18:24:401// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]50ae9f12013-08-29 18:03:225#include "components/variations/variations_associated_data.h"
[email protected]7e797792013-08-05 18:24:406
avi5dd91f82015-12-25 22:30:467#include "base/macros.h"
[email protected]7e797792013-08-05 18:24:408#include "base/metrics/field_trial.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
[email protected]59b6f672014-07-26 18:35:4711namespace variations {
[email protected]7e797792013-08-05 18:24:4012
13namespace {
14
[email protected]2b9badf2013-08-16 22:15:1815const VariationID TEST_VALUE_A = 3300200;
16const VariationID TEST_VALUE_B = 3300201;
17
[email protected]59b6f672014-07-26 18:35:4718// Convenience helper to retrieve the variations::VariationID for a FieldTrial.
19// Note that this will do the group assignment in |trial| if not already done.
[email protected]7e797792013-08-05 18:24:4020VariationID GetIDForTrial(IDCollectionKey key, base::FieldTrial* trial) {
21 return GetGoogleVariationID(key, trial->trial_name(), trial->group_name());
22}
23
Alexei Svitkinecde0b632019-05-29 14:22:3524// Call FieldTrialList::FactoryGetFieldTrial().
[email protected]7e797792013-08-05 18:24:4025scoped_refptr<base::FieldTrial> CreateFieldTrial(
26 const std::string& trial_name,
27 int total_probability,
28 const std::string& default_group_name,
29 int* default_group_number) {
30 return base::FieldTrialList::FactoryGetFieldTrial(
31 trial_name, total_probability, default_group_name,
[email protected]7e797792013-08-05 18:24:4032 base::FieldTrial::SESSION_RANDOMIZED, default_group_number);
33}
34
35} // namespace
36
37class VariationsAssociatedDataTest : public ::testing::Test {
38 public:
Ivan Kotenkov75b1c3a2017-10-24 14:47:2439 VariationsAssociatedDataTest() : field_trial_list_(nullptr) {}
[email protected]7e797792013-08-05 18:24:4040
dcheng30a1b1542014-10-29 21:27:5041 ~VariationsAssociatedDataTest() override {
[email protected]7e797792013-08-05 18:24:4042 // Ensure that the maps are cleared between tests, since they are stored as
43 // process singletons.
44 testing::ClearAllVariationIDs();
asvitkine9499b8d2016-08-09 05:37:0745 }
46
[email protected]7e797792013-08-05 18:24:4047 private:
48 base::FieldTrialList field_trial_list_;
49
50 DISALLOW_COPY_AND_ASSIGN(VariationsAssociatedDataTest);
51};
52
53// Test that if the trial is immediately disabled, GetGoogleVariationID just
54// returns the empty ID.
55TEST_F(VariationsAssociatedDataTest, DisableImmediately) {
56 int default_group_number = -1;
57 scoped_refptr<base::FieldTrial> trial(
58 CreateFieldTrial("trial", 100, "default", &default_group_number));
59
60 ASSERT_EQ(default_group_number, trial->group());
61 ASSERT_EQ(EMPTY_ID, GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial.get()));
62}
63
64// Test that successfully associating the FieldTrial with some ID, and then
65// disabling the FieldTrial actually makes GetGoogleVariationID correctly
66// return the empty ID.
67TEST_F(VariationsAssociatedDataTest, DisableAfterInitialization) {
68 const std::string default_name = "default";
69 const std::string non_default_name = "non_default";
70
71 scoped_refptr<base::FieldTrial> trial(
Ivan Kotenkov75b1c3a2017-10-24 14:47:2472 CreateFieldTrial("trial", 100, default_name, nullptr));
[email protected]7e797792013-08-05 18:24:4073
74 trial->AppendGroup(non_default_name, 100);
75 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial->trial_name(),
76 default_name, TEST_VALUE_A);
77 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial->trial_name(),
78 non_default_name, TEST_VALUE_B);
79 trial->Disable();
80 ASSERT_EQ(default_name, trial->group_name());
81 ASSERT_EQ(TEST_VALUE_A, GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial.get()));
82}
83
84// Test various successful association cases.
85TEST_F(VariationsAssociatedDataTest, AssociateGoogleVariationID) {
86 const std::string default_name1 = "default";
87 scoped_refptr<base::FieldTrial> trial_true(
Ivan Kotenkov75b1c3a2017-10-24 14:47:2488 CreateFieldTrial("d1", 10, default_name1, nullptr));
[email protected]7e797792013-08-05 18:24:4089 const std::string winner = "TheWinner";
90 int winner_group = trial_true->AppendGroup(winner, 10);
91
92 // Set GoogleVariationIDs so we can verify that they were chosen correctly.
93 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_true->trial_name(),
94 default_name1, TEST_VALUE_A);
95 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_true->trial_name(),
96 winner, TEST_VALUE_B);
97
98 EXPECT_EQ(winner_group, trial_true->group());
99 EXPECT_EQ(winner, trial_true->group_name());
100 EXPECT_EQ(TEST_VALUE_B,
101 GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial_true.get()));
102
103 const std::string default_name2 = "default2";
104 scoped_refptr<base::FieldTrial> trial_false(
Ivan Kotenkov75b1c3a2017-10-24 14:47:24105 CreateFieldTrial("d2", 10, default_name2, nullptr));
[email protected]7e797792013-08-05 18:24:40106 const std::string loser = "ALoser";
107 const int loser_group = trial_false->AppendGroup(loser, 0);
108
109 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_false->trial_name(),
110 default_name2, TEST_VALUE_A);
111 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_false->trial_name(),
112 loser, TEST_VALUE_B);
113
114 EXPECT_NE(loser_group, trial_false->group());
115 EXPECT_EQ(TEST_VALUE_A,
116 GetIDForTrial(GOOGLE_WEB_PROPERTIES, trial_false.get()));
117}
118
119// Test that not associating a FieldTrial with any IDs ensure that the empty ID
120// will be returned.
121TEST_F(VariationsAssociatedDataTest, NoAssociation) {
122 const std::string default_name = "default";
123 scoped_refptr<base::FieldTrial> no_id_trial(
Ivan Kotenkov75b1c3a2017-10-24 14:47:24124 CreateFieldTrial("d3", 10, default_name, nullptr));
[email protected]7e797792013-08-05 18:24:40125
126 const std::string winner = "TheWinner";
127 const int winner_group = no_id_trial->AppendGroup(winner, 10);
128
129 // Ensure that despite the fact that a normal winner is elected, it does not
130 // have a valid VariationID associated with it.
131 EXPECT_EQ(winner_group, no_id_trial->group());
132 EXPECT_EQ(winner, no_id_trial->group_name());
133 EXPECT_EQ(EMPTY_ID, GetIDForTrial(GOOGLE_WEB_PROPERTIES, no_id_trial.get()));
134}
135
136// Ensure that the AssociateGoogleVariationIDForce works as expected.
137TEST_F(VariationsAssociatedDataTest, ForceAssociation) {
138 EXPECT_EQ(EMPTY_ID,
139 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group"));
140 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group",
141 TEST_VALUE_A);
142 EXPECT_EQ(TEST_VALUE_A,
143 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group"));
144 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group",
145 TEST_VALUE_B);
146 EXPECT_EQ(TEST_VALUE_A,
147 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group"));
148 AssociateGoogleVariationIDForce(GOOGLE_WEB_PROPERTIES, "trial", "group",
149 TEST_VALUE_B);
150 EXPECT_EQ(TEST_VALUE_B,
151 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", "group"));
152}
153
[email protected]59b6f672014-07-26 18:35:47154} // namespace variations