1 // 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.
5 #include "components/variations/variations_associated_data.h"
7 #include "base/metrics/field_trial.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace chrome_variations
{
14 const VariationID TEST_VALUE_A
= 3300200;
15 const VariationID TEST_VALUE_B
= 3300201;
17 // Convenience helper to retrieve the chrome_variations::VariationID for a
18 // FieldTrial. Note that this will do the group assignment in |trial| if not
20 VariationID
GetIDForTrial(IDCollectionKey key
, base::FieldTrial
* trial
) {
21 return GetGoogleVariationID(key
, trial
->trial_name(), trial
->group_name());
24 // Tests whether a field trial is active (i.e. group() has been called on it).
25 bool IsFieldTrialActive(const std::string
& trial_name
) {
26 base::FieldTrial::ActiveGroups active_groups
;
27 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups
);
28 for (size_t i
= 0; i
< active_groups
.size(); ++i
) {
29 if (active_groups
[i
].trial_name
== trial_name
)
35 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date.
36 scoped_refptr
<base::FieldTrial
> CreateFieldTrial(
37 const std::string
& trial_name
,
38 int total_probability
,
39 const std::string
& default_group_name
,
40 int* default_group_number
) {
41 return base::FieldTrialList::FactoryGetFieldTrial(
42 trial_name
, total_probability
, default_group_name
,
43 base::FieldTrialList::kNoExpirationYear
, 1, 1,
44 base::FieldTrial::SESSION_RANDOMIZED
, default_group_number
);
49 class VariationsAssociatedDataTest
: public ::testing::Test
{
51 VariationsAssociatedDataTest() : field_trial_list_(NULL
) {
54 virtual ~VariationsAssociatedDataTest() {
55 // Ensure that the maps are cleared between tests, since they are stored as
56 // process singletons.
57 testing::ClearAllVariationIDs();
61 base::FieldTrialList field_trial_list_
;
63 DISALLOW_COPY_AND_ASSIGN(VariationsAssociatedDataTest
);
66 // Test that if the trial is immediately disabled, GetGoogleVariationID just
67 // returns the empty ID.
68 TEST_F(VariationsAssociatedDataTest
, DisableImmediately
) {
69 int default_group_number
= -1;
70 scoped_refptr
<base::FieldTrial
> trial(
71 CreateFieldTrial("trial", 100, "default", &default_group_number
));
73 ASSERT_EQ(default_group_number
, trial
->group());
74 ASSERT_EQ(EMPTY_ID
, GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial
.get()));
77 // Test that successfully associating the FieldTrial with some ID, and then
78 // disabling the FieldTrial actually makes GetGoogleVariationID correctly
79 // return the empty ID.
80 TEST_F(VariationsAssociatedDataTest
, DisableAfterInitialization
) {
81 const std::string default_name
= "default";
82 const std::string non_default_name
= "non_default";
84 scoped_refptr
<base::FieldTrial
> trial(
85 CreateFieldTrial("trial", 100, default_name
, NULL
));
87 trial
->AppendGroup(non_default_name
, 100);
88 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial
->trial_name(),
89 default_name
, TEST_VALUE_A
);
90 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial
->trial_name(),
91 non_default_name
, TEST_VALUE_B
);
93 ASSERT_EQ(default_name
, trial
->group_name());
94 ASSERT_EQ(TEST_VALUE_A
, GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial
.get()));
97 // Test various successful association cases.
98 TEST_F(VariationsAssociatedDataTest
, AssociateGoogleVariationID
) {
99 const std::string default_name1
= "default";
100 scoped_refptr
<base::FieldTrial
> trial_true(
101 CreateFieldTrial("d1", 10, default_name1
, NULL
));
102 const std::string winner
= "TheWinner";
103 int winner_group
= trial_true
->AppendGroup(winner
, 10);
105 // Set GoogleVariationIDs so we can verify that they were chosen correctly.
106 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial_true
->trial_name(),
107 default_name1
, TEST_VALUE_A
);
108 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial_true
->trial_name(),
109 winner
, TEST_VALUE_B
);
111 EXPECT_EQ(winner_group
, trial_true
->group());
112 EXPECT_EQ(winner
, trial_true
->group_name());
113 EXPECT_EQ(TEST_VALUE_B
,
114 GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial_true
.get()));
116 const std::string default_name2
= "default2";
117 scoped_refptr
<base::FieldTrial
> trial_false(
118 CreateFieldTrial("d2", 10, default_name2
, NULL
));
119 const std::string loser
= "ALoser";
120 const int loser_group
= trial_false
->AppendGroup(loser
, 0);
122 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial_false
->trial_name(),
123 default_name2
, TEST_VALUE_A
);
124 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial_false
->trial_name(),
125 loser
, TEST_VALUE_B
);
127 EXPECT_NE(loser_group
, trial_false
->group());
128 EXPECT_EQ(TEST_VALUE_A
,
129 GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial_false
.get()));
132 // Test that not associating a FieldTrial with any IDs ensure that the empty ID
134 TEST_F(VariationsAssociatedDataTest
, NoAssociation
) {
135 const std::string default_name
= "default";
136 scoped_refptr
<base::FieldTrial
> no_id_trial(
137 CreateFieldTrial("d3", 10, default_name
, NULL
));
139 const std::string winner
= "TheWinner";
140 const int winner_group
= no_id_trial
->AppendGroup(winner
, 10);
142 // Ensure that despite the fact that a normal winner is elected, it does not
143 // have a valid VariationID associated with it.
144 EXPECT_EQ(winner_group
, no_id_trial
->group());
145 EXPECT_EQ(winner
, no_id_trial
->group_name());
146 EXPECT_EQ(EMPTY_ID
, GetIDForTrial(GOOGLE_WEB_PROPERTIES
, no_id_trial
.get()));
149 // Ensure that the AssociateGoogleVariationIDForce works as expected.
150 TEST_F(VariationsAssociatedDataTest
, ForceAssociation
) {
152 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES
, "trial", "group"));
153 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, "trial", "group",
155 EXPECT_EQ(TEST_VALUE_A
,
156 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES
, "trial", "group"));
157 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, "trial", "group",
159 EXPECT_EQ(TEST_VALUE_A
,
160 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES
, "trial", "group"));
161 AssociateGoogleVariationIDForce(GOOGLE_WEB_PROPERTIES
, "trial", "group",
163 EXPECT_EQ(TEST_VALUE_B
,
164 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES
, "trial", "group"));
167 // Ensure that two collections can coexist without affecting each other.
168 TEST_F(VariationsAssociatedDataTest
, CollectionsCoexist
) {
169 const std::string default_name
= "default";
170 int default_group_number
= -1;
171 scoped_refptr
<base::FieldTrial
> trial_true(
172 CreateFieldTrial("d1", 10, default_name
, &default_group_number
));
173 ASSERT_EQ(default_group_number
, trial_true
->group());
174 ASSERT_EQ(default_name
, trial_true
->group_name());
177 GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial_true
.get()));
179 GetIDForTrial(GOOGLE_UPDATE_SERVICE
, trial_true
.get()));
181 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial_true
->trial_name(),
182 default_name
, TEST_VALUE_A
);
183 EXPECT_EQ(TEST_VALUE_A
,
184 GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial_true
.get()));
186 GetIDForTrial(GOOGLE_UPDATE_SERVICE
, trial_true
.get()));
188 AssociateGoogleVariationID(GOOGLE_UPDATE_SERVICE
, trial_true
->trial_name(),
189 default_name
, TEST_VALUE_A
);
190 EXPECT_EQ(TEST_VALUE_A
,
191 GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial_true
.get()));
192 EXPECT_EQ(TEST_VALUE_A
,
193 GetIDForTrial(GOOGLE_UPDATE_SERVICE
, trial_true
.get()));
196 TEST_F(VariationsAssociatedDataTest
, AssociateVariationParams
) {
197 const std::string kTrialName
= "AssociateVariationParams";
200 std::map
<std::string
, std::string
> params
;
202 params
["b"] = "test";
203 ASSERT_TRUE(AssociateVariationParams(kTrialName
, "A", params
));
206 std::map
<std::string
, std::string
> params
;
208 ASSERT_TRUE(AssociateVariationParams(kTrialName
, "B", params
));
211 base::FieldTrialList::CreateFieldTrial(kTrialName
, "B");
212 EXPECT_EQ("5", GetVariationParamValue(kTrialName
, "a"));
213 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "b"));
214 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "x"));
216 std::map
<std::string
, std::string
> params
;
217 EXPECT_TRUE(GetVariationParams(kTrialName
, ¶ms
));
218 EXPECT_EQ(1U, params
.size());
219 EXPECT_EQ("5", params
["a"]);
222 TEST_F(VariationsAssociatedDataTest
, AssociateVariationParams_Fail
) {
223 const std::string kTrialName
= "AssociateVariationParams_Fail";
224 const std::string kGroupName
= "A";
226 std::map
<std::string
, std::string
> params
;
228 ASSERT_TRUE(AssociateVariationParams(kTrialName
, kGroupName
, params
));
231 ASSERT_FALSE(AssociateVariationParams(kTrialName
, kGroupName
, params
));
233 base::FieldTrialList::CreateFieldTrial(kTrialName
, kGroupName
);
234 EXPECT_EQ("10", GetVariationParamValue(kTrialName
, "a"));
235 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "b"));
238 TEST_F(VariationsAssociatedDataTest
, AssociateVariationParams_TrialActiveFail
) {
239 const std::string kTrialName
= "AssociateVariationParams_TrialActiveFail";
240 base::FieldTrialList::CreateFieldTrial(kTrialName
, "A");
241 ASSERT_EQ("A", base::FieldTrialList::FindFullName(kTrialName
));
243 std::map
<std::string
, std::string
> params
;
245 EXPECT_FALSE(AssociateVariationParams(kTrialName
, "B", params
));
246 EXPECT_FALSE(AssociateVariationParams(kTrialName
, "A", params
));
249 TEST_F(VariationsAssociatedDataTest
,
250 AssociateVariationParams_DoesntActivateTrial
) {
251 const std::string kTrialName
= "AssociateVariationParams_DoesntActivateTrial";
253 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
254 scoped_refptr
<base::FieldTrial
> trial(
255 CreateFieldTrial(kTrialName
, 100, "A", NULL
));
256 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
258 std::map
<std::string
, std::string
> params
;
260 EXPECT_TRUE(AssociateVariationParams(kTrialName
, "A", params
));
261 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
264 TEST_F(VariationsAssociatedDataTest
, GetVariationParams_NoTrial
) {
265 const std::string kTrialName
= "GetVariationParams_NoParams";
267 std::map
<std::string
, std::string
> params
;
268 EXPECT_FALSE(GetVariationParams(kTrialName
, ¶ms
));
269 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "x"));
270 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "y"));
273 TEST_F(VariationsAssociatedDataTest
, GetVariationParams_NoParams
) {
274 const std::string kTrialName
= "GetVariationParams_NoParams";
276 base::FieldTrialList::CreateFieldTrial(kTrialName
, "A");
278 std::map
<std::string
, std::string
> params
;
279 EXPECT_FALSE(GetVariationParams(kTrialName
, ¶ms
));
280 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "x"));
281 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "y"));
284 TEST_F(VariationsAssociatedDataTest
, GetVariationParams_ActivatesTrial
) {
285 const std::string kTrialName
= "GetVariationParams_ActivatesTrial";
287 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
288 scoped_refptr
<base::FieldTrial
> trial(
289 CreateFieldTrial(kTrialName
, 100, "A", NULL
));
290 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
292 std::map
<std::string
, std::string
> params
;
293 EXPECT_FALSE(GetVariationParams(kTrialName
, ¶ms
));
294 ASSERT_TRUE(IsFieldTrialActive(kTrialName
));
297 TEST_F(VariationsAssociatedDataTest
, GetVariationParamValue_ActivatesTrial
) {
298 const std::string kTrialName
= "GetVariationParamValue_ActivatesTrial";
300 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
301 scoped_refptr
<base::FieldTrial
> trial(
302 CreateFieldTrial(kTrialName
, 100, "A", NULL
));
303 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
305 std::map
<std::string
, std::string
> params
;
306 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "x"));
307 ASSERT_TRUE(IsFieldTrialActive(kTrialName
));
310 } // namespace chrome_variations