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();
58 testing::ClearAllVariationParams();
62 base::FieldTrialList field_trial_list_
;
64 DISALLOW_COPY_AND_ASSIGN(VariationsAssociatedDataTest
);
67 // Test that if the trial is immediately disabled, GetGoogleVariationID just
68 // returns the empty ID.
69 TEST_F(VariationsAssociatedDataTest
, DisableImmediately
) {
70 int default_group_number
= -1;
71 scoped_refptr
<base::FieldTrial
> trial(
72 CreateFieldTrial("trial", 100, "default", &default_group_number
));
74 ASSERT_EQ(default_group_number
, trial
->group());
75 ASSERT_EQ(EMPTY_ID
, GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial
.get()));
78 // Test that successfully associating the FieldTrial with some ID, and then
79 // disabling the FieldTrial actually makes GetGoogleVariationID correctly
80 // return the empty ID.
81 TEST_F(VariationsAssociatedDataTest
, DisableAfterInitialization
) {
82 const std::string default_name
= "default";
83 const std::string non_default_name
= "non_default";
85 scoped_refptr
<base::FieldTrial
> trial(
86 CreateFieldTrial("trial", 100, default_name
, NULL
));
88 trial
->AppendGroup(non_default_name
, 100);
89 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial
->trial_name(),
90 default_name
, TEST_VALUE_A
);
91 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial
->trial_name(),
92 non_default_name
, TEST_VALUE_B
);
94 ASSERT_EQ(default_name
, trial
->group_name());
95 ASSERT_EQ(TEST_VALUE_A
, GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial
.get()));
98 // Test various successful association cases.
99 TEST_F(VariationsAssociatedDataTest
, AssociateGoogleVariationID
) {
100 const std::string default_name1
= "default";
101 scoped_refptr
<base::FieldTrial
> trial_true(
102 CreateFieldTrial("d1", 10, default_name1
, NULL
));
103 const std::string winner
= "TheWinner";
104 int winner_group
= trial_true
->AppendGroup(winner
, 10);
106 // Set GoogleVariationIDs so we can verify that they were chosen correctly.
107 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial_true
->trial_name(),
108 default_name1
, TEST_VALUE_A
);
109 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial_true
->trial_name(),
110 winner
, TEST_VALUE_B
);
112 EXPECT_EQ(winner_group
, trial_true
->group());
113 EXPECT_EQ(winner
, trial_true
->group_name());
114 EXPECT_EQ(TEST_VALUE_B
,
115 GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial_true
.get()));
117 const std::string default_name2
= "default2";
118 scoped_refptr
<base::FieldTrial
> trial_false(
119 CreateFieldTrial("d2", 10, default_name2
, NULL
));
120 const std::string loser
= "ALoser";
121 const int loser_group
= trial_false
->AppendGroup(loser
, 0);
123 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial_false
->trial_name(),
124 default_name2
, TEST_VALUE_A
);
125 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial_false
->trial_name(),
126 loser
, TEST_VALUE_B
);
128 EXPECT_NE(loser_group
, trial_false
->group());
129 EXPECT_EQ(TEST_VALUE_A
,
130 GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial_false
.get()));
133 // Test that not associating a FieldTrial with any IDs ensure that the empty ID
135 TEST_F(VariationsAssociatedDataTest
, NoAssociation
) {
136 const std::string default_name
= "default";
137 scoped_refptr
<base::FieldTrial
> no_id_trial(
138 CreateFieldTrial("d3", 10, default_name
, NULL
));
140 const std::string winner
= "TheWinner";
141 const int winner_group
= no_id_trial
->AppendGroup(winner
, 10);
143 // Ensure that despite the fact that a normal winner is elected, it does not
144 // have a valid VariationID associated with it.
145 EXPECT_EQ(winner_group
, no_id_trial
->group());
146 EXPECT_EQ(winner
, no_id_trial
->group_name());
147 EXPECT_EQ(EMPTY_ID
, GetIDForTrial(GOOGLE_WEB_PROPERTIES
, no_id_trial
.get()));
150 // Ensure that the AssociateGoogleVariationIDForce works as expected.
151 TEST_F(VariationsAssociatedDataTest
, ForceAssociation
) {
153 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES
, "trial", "group"));
154 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, "trial", "group",
156 EXPECT_EQ(TEST_VALUE_A
,
157 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES
, "trial", "group"));
158 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, "trial", "group",
160 EXPECT_EQ(TEST_VALUE_A
,
161 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES
, "trial", "group"));
162 AssociateGoogleVariationIDForce(GOOGLE_WEB_PROPERTIES
, "trial", "group",
164 EXPECT_EQ(TEST_VALUE_B
,
165 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES
, "trial", "group"));
168 // Ensure that two collections can coexist without affecting each other.
169 TEST_F(VariationsAssociatedDataTest
, CollectionsCoexist
) {
170 const std::string default_name
= "default";
171 int default_group_number
= -1;
172 scoped_refptr
<base::FieldTrial
> trial_true(
173 CreateFieldTrial("d1", 10, default_name
, &default_group_number
));
174 ASSERT_EQ(default_group_number
, trial_true
->group());
175 ASSERT_EQ(default_name
, trial_true
->group_name());
178 GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial_true
.get()));
180 GetIDForTrial(GOOGLE_UPDATE_SERVICE
, trial_true
.get()));
182 AssociateGoogleVariationID(GOOGLE_WEB_PROPERTIES
, trial_true
->trial_name(),
183 default_name
, TEST_VALUE_A
);
184 EXPECT_EQ(TEST_VALUE_A
,
185 GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial_true
.get()));
187 GetIDForTrial(GOOGLE_UPDATE_SERVICE
, trial_true
.get()));
189 AssociateGoogleVariationID(GOOGLE_UPDATE_SERVICE
, trial_true
->trial_name(),
190 default_name
, TEST_VALUE_A
);
191 EXPECT_EQ(TEST_VALUE_A
,
192 GetIDForTrial(GOOGLE_WEB_PROPERTIES
, trial_true
.get()));
193 EXPECT_EQ(TEST_VALUE_A
,
194 GetIDForTrial(GOOGLE_UPDATE_SERVICE
, trial_true
.get()));
197 TEST_F(VariationsAssociatedDataTest
, AssociateVariationParams
) {
198 const std::string kTrialName
= "AssociateVariationParams";
201 std::map
<std::string
, std::string
> params
;
203 params
["b"] = "test";
204 ASSERT_TRUE(AssociateVariationParams(kTrialName
, "A", params
));
207 std::map
<std::string
, std::string
> params
;
209 ASSERT_TRUE(AssociateVariationParams(kTrialName
, "B", params
));
212 base::FieldTrialList::CreateFieldTrial(kTrialName
, "B");
213 EXPECT_EQ("5", GetVariationParamValue(kTrialName
, "a"));
214 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "b"));
215 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "x"));
217 std::map
<std::string
, std::string
> params
;
218 EXPECT_TRUE(GetVariationParams(kTrialName
, ¶ms
));
219 EXPECT_EQ(1U, params
.size());
220 EXPECT_EQ("5", params
["a"]);
223 TEST_F(VariationsAssociatedDataTest
, AssociateVariationParams_Fail
) {
224 const std::string kTrialName
= "AssociateVariationParams_Fail";
225 const std::string kGroupName
= "A";
227 std::map
<std::string
, std::string
> params
;
229 ASSERT_TRUE(AssociateVariationParams(kTrialName
, kGroupName
, params
));
232 ASSERT_FALSE(AssociateVariationParams(kTrialName
, kGroupName
, params
));
234 base::FieldTrialList::CreateFieldTrial(kTrialName
, kGroupName
);
235 EXPECT_EQ("10", GetVariationParamValue(kTrialName
, "a"));
236 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "b"));
239 TEST_F(VariationsAssociatedDataTest
, AssociateVariationParams_TrialActiveFail
) {
240 const std::string kTrialName
= "AssociateVariationParams_TrialActiveFail";
241 base::FieldTrialList::CreateFieldTrial(kTrialName
, "A");
242 ASSERT_EQ("A", base::FieldTrialList::FindFullName(kTrialName
));
244 std::map
<std::string
, std::string
> params
;
246 EXPECT_FALSE(AssociateVariationParams(kTrialName
, "B", params
));
247 EXPECT_FALSE(AssociateVariationParams(kTrialName
, "A", params
));
250 TEST_F(VariationsAssociatedDataTest
,
251 AssociateVariationParams_DoesntActivateTrial
) {
252 const std::string kTrialName
= "AssociateVariationParams_DoesntActivateTrial";
254 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
255 scoped_refptr
<base::FieldTrial
> trial(
256 CreateFieldTrial(kTrialName
, 100, "A", NULL
));
257 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
259 std::map
<std::string
, std::string
> params
;
261 EXPECT_TRUE(AssociateVariationParams(kTrialName
, "A", params
));
262 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
265 TEST_F(VariationsAssociatedDataTest
, GetVariationParams_NoTrial
) {
266 const std::string kTrialName
= "GetVariationParams_NoParams";
268 std::map
<std::string
, std::string
> params
;
269 EXPECT_FALSE(GetVariationParams(kTrialName
, ¶ms
));
270 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "x"));
271 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "y"));
274 TEST_F(VariationsAssociatedDataTest
, GetVariationParams_NoParams
) {
275 const std::string kTrialName
= "GetVariationParams_NoParams";
277 base::FieldTrialList::CreateFieldTrial(kTrialName
, "A");
279 std::map
<std::string
, std::string
> params
;
280 EXPECT_FALSE(GetVariationParams(kTrialName
, ¶ms
));
281 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "x"));
282 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "y"));
285 TEST_F(VariationsAssociatedDataTest
, GetVariationParams_ActivatesTrial
) {
286 const std::string kTrialName
= "GetVariationParams_ActivatesTrial";
288 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
289 scoped_refptr
<base::FieldTrial
> trial(
290 CreateFieldTrial(kTrialName
, 100, "A", NULL
));
291 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
293 std::map
<std::string
, std::string
> params
;
294 EXPECT_FALSE(GetVariationParams(kTrialName
, ¶ms
));
295 ASSERT_TRUE(IsFieldTrialActive(kTrialName
));
298 TEST_F(VariationsAssociatedDataTest
, GetVariationParamValue_ActivatesTrial
) {
299 const std::string kTrialName
= "GetVariationParamValue_ActivatesTrial";
301 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
302 scoped_refptr
<base::FieldTrial
> trial(
303 CreateFieldTrial(kTrialName
, 100, "A", NULL
));
304 ASSERT_FALSE(IsFieldTrialActive(kTrialName
));
306 std::map
<std::string
, std::string
> params
;
307 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName
, "x"));
308 ASSERT_TRUE(IsFieldTrialActive(kTrialName
));
311 } // namespace chrome_variations