Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: chrome/common/metrics/variations_util_unittest.cc

Issue 10828314: Move Variations stuff into variations/ directories and add OWNERS files for the variations client t… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Add OWNERS files Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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
5 // Tests for the Variations Helpers.
6
7 #include <set>
8
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/time.h"
13 #include "base/utf_string_conversions.h"
14 #include "chrome/common/metrics/variations_util.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace chrome_variations {
19
20 namespace {
21
22 // Convenience helper to retrieve the chrome_variations::VariationID for a
23 // FieldTrial. Note that this will do the group assignment in |trial| if not
24 // already done.
25 chrome_variations::VariationID GetIDForTrial(base::FieldTrial* trial) {
26 return GetGoogleVariationID(trial->name(), trial->group_name());
27 }
28
29 } // namespace
30
31 class VariationsHelperTest : public ::testing::Test {
32 public:
33 VariationsHelperTest() {
34 // Since the API can only be called on the UI thread, we have to fake that
35 // we're on it.
36 ui_thread_.reset(new content::TestBrowserThread(
37 content::BrowserThread::UI, &message_loop_));
38
39 base::Time now = base::Time::NowFromSystemTime();
40 base::TimeDelta oneYear = base::TimeDelta::FromDays(365);
41 base::Time::Exploded exploded;
42
43 base::Time next_year_time = now + oneYear;
44 next_year_time.LocalExplode(&exploded);
45 next_year_ = exploded.year;
46 }
47
48 protected:
49 int next_year_;
50
51 private:
52 MessageLoop message_loop_;
53 scoped_ptr<content::TestBrowserThread> ui_thread_;
54 };
55
56 TEST_F(VariationsHelperTest, HashName) {
57 // Make sure hashing is stable on all platforms.
58 struct {
59 const char* name;
60 uint32 hash_value;
61 } known_hashes[] = {
62 {"a", 937752454u},
63 {"1", 723085877u},
64 {"Trial Name", 2713117220u},
65 {"Group Name", 3201815843u},
66 {"My Favorite Experiment", 3722155194u},
67 {"My Awesome Group Name", 4109503236u},
68 {"abcdefghijklmonpqrstuvwxyz", 787728696u},
69 {"0123456789ABCDEF", 348858318U}
70 };
71 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(known_hashes); ++i) {
72 EXPECT_EQ(known_hashes[i].hash_value,
73 testing::TestHashName(known_hashes[i].name));
74 }
75 }
76
77 TEST_F(VariationsHelperTest, GetFieldTrialSelectedGroups) {
78 typedef std::set<SelectedGroupId, SelectedGroupIdCompare> SelectedGroupIdSet;
79 std::string trial_one("trial one");
80 std::string group_one("group one");
81 std::string trial_two("trial two");
82 std::string group_two("group two");
83
84 base::FieldTrial::SelectedGroups selected_groups;
85 base::FieldTrial::SelectedGroup selected_group;
86 selected_group.trial = trial_one;
87 selected_group.group = group_one;
88 selected_groups.push_back(selected_group);
89
90 selected_group.trial = trial_two;
91 selected_group.group = group_two;
92 selected_groups.push_back(selected_group);
93
94 // Create our expected groups of IDs.
95 SelectedGroupIdSet expected_groups;
96 SelectedGroupId name_group_id;
97 name_group_id.name = testing::TestHashName(trial_one);
98 name_group_id.group = testing::TestHashName(group_one);
99 expected_groups.insert(name_group_id);
100 name_group_id.name = testing::TestHashName(trial_two);
101 name_group_id.group = testing::TestHashName(group_two);
102 expected_groups.insert(name_group_id);
103
104 std::vector<SelectedGroupId> selected_group_ids;
105 testing::TestGetFieldTrialSelectedGroupIdsForSelectedGroups(
106 selected_groups, &selected_group_ids);
107 EXPECT_EQ(2U, selected_group_ids.size());
108 for (size_t i = 0; i < selected_group_ids.size(); ++i) {
109 SelectedGroupIdSet::iterator expected_group =
110 expected_groups.find(selected_group_ids[i]);
111 EXPECT_FALSE(expected_group == expected_groups.end());
112 expected_groups.erase(expected_group);
113 }
114 EXPECT_EQ(0U, expected_groups.size());
115 }
116
117 // Test that if the trial is immediately disabled, GetGoogleVariationID just
118 // returns the empty ID.
119 TEST_F(VariationsHelperTest, DisableImmediately) {
120 int default_group_number = -1;
121 scoped_refptr<base::FieldTrial> trial(
122 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, "default",
123 next_year_, 12, 12,
124 &default_group_number));
125 trial->Disable();
126
127 ASSERT_EQ(default_group_number, trial->group());
128 ASSERT_EQ(chrome_variations::kEmptyID, GetIDForTrial(trial.get()));
129 }
130
131 // Test that successfully associating the FieldTrial with some ID, and then
132 // disabling the FieldTrial actually makes GetGoogleVariationID correctly
133 // return the empty ID.
134 TEST_F(VariationsHelperTest, DisableAfterInitialization) {
135 const std::string default_name = "default";
136 const std::string non_default_name = "non_default";
137
138 scoped_refptr<base::FieldTrial> trial(
139 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, default_name,
140 next_year_, 12, 12, NULL));
141 trial->AppendGroup(non_default_name, 100);
142 AssociateGoogleVariationID(
143 trial->name(), default_name, chrome_variations::kTestValueA);
144 AssociateGoogleVariationID(
145 trial->name(), non_default_name, chrome_variations::kTestValueB);
146 ASSERT_EQ(non_default_name, trial->group_name());
147 ASSERT_EQ(chrome_variations::kTestValueB, GetIDForTrial(trial.get()));
148 trial->Disable();
149 ASSERT_EQ(default_name, trial->group_name());
150 ASSERT_EQ(chrome_variations::kTestValueA, GetIDForTrial(trial.get()));
151 }
152
153 // Test various successful association cases.
154 TEST_F(VariationsHelperTest, AssociateGoogleVariationID) {
155 const std::string default_name1 = "default1";
156 scoped_refptr<base::FieldTrial> trial_true(
157 base::FieldTrialList::FactoryGetFieldTrial("d1", 10, default_name1,
158 next_year_, 12, 31, NULL));
159 const std::string winner = "TheWinner";
160 int winner_group = trial_true->AppendGroup(winner, 10);
161
162 // Set GoogleVariationIDs so we can verify that they were chosen correctly.
163 AssociateGoogleVariationID(
164 trial_true->name(), default_name1, chrome_variations::kTestValueA);
165 AssociateGoogleVariationID(
166 trial_true->name(), winner, chrome_variations::kTestValueB);
167
168 EXPECT_EQ(winner_group, trial_true->group());
169 EXPECT_EQ(winner, trial_true->group_name());
170 EXPECT_EQ(chrome_variations::kTestValueB, GetIDForTrial(trial_true.get()));
171
172 const std::string default_name2 = "default2";
173 scoped_refptr<base::FieldTrial> trial_false(
174 base::FieldTrialList::FactoryGetFieldTrial("d2", 10, default_name2,
175 next_year_, 12, 31, NULL));
176 const std::string loser = "ALoser";
177 int loser_group = trial_false->AppendGroup(loser, 0);
178
179 AssociateGoogleVariationID(
180 trial_false->name(), default_name2, chrome_variations::kTestValueA);
181 AssociateGoogleVariationID(
182 trial_false->name(), loser, chrome_variations::kTestValueB);
183
184 EXPECT_NE(loser_group, trial_false->group());
185 EXPECT_EQ(chrome_variations::kTestValueA, GetIDForTrial(trial_false.get()));
186 }
187
188 // Test that not associating a FieldTrial with any IDs ensure that the empty ID
189 // will be returned.
190 TEST_F(VariationsHelperTest, NoAssociation) {
191 const std::string default_name = "default";
192 scoped_refptr<base::FieldTrial> no_id_trial(
193 base::FieldTrialList::FactoryGetFieldTrial("d3", 10, default_name,
194 next_year_, 12, 31, NULL));
195 const std::string winner = "TheWinner";
196 int winner_group = no_id_trial->AppendGroup(winner, 10);
197
198 // Ensure that despite the fact that a normal winner is elected, it does not
199 // have a valid chrome_variations::VariationID associated with it.
200 EXPECT_EQ(winner_group, no_id_trial->group());
201 EXPECT_EQ(winner, no_id_trial->group_name());
202 EXPECT_EQ(chrome_variations::kEmptyID, GetIDForTrial(no_id_trial.get()));
203 }
204
205 // Ensure that the AssociateGoogleVariationIDForce works as expected.
206 TEST_F(VariationsHelperTest, ForceAssociation) {
207 EXPECT_EQ(chrome_variations::kEmptyID,
208 GetGoogleVariationID("trial", "group"));
209 AssociateGoogleVariationID("trial", "group",
210 chrome_variations::kTestValueA);
211 EXPECT_EQ(chrome_variations::kTestValueA,
212 GetGoogleVariationID("trial", "group"));
213 AssociateGoogleVariationID("trial", "group", chrome_variations::kTestValueB);
214 EXPECT_EQ(chrome_variations::kTestValueA,
215 GetGoogleVariationID("trial", "group"));
216 AssociateGoogleVariationIDForce("trial", "group",
217 chrome_variations::kTestValueB);
218 EXPECT_EQ(chrome_variations::kTestValueB,
219 GetGoogleVariationID("trial", "group"));
220 }
221
222 TEST_F(VariationsHelperTest, GenerateExperimentChunks) {
223 const char* kExperimentStrings[] = {
224 "1d3048f1-9de009d0",
225 "cd73da34-cf196cb",
226 "6214fa18-9e6dc24d",
227 "4dcb0cd6-d31c4ca1",
228 "9d5bce6-30d7d8ac",
229 };
230 const char* kExpectedChunks1[] = {
231 "1d3048f1-9de009d0",
232 };
233 const char* kExpectedChunks2[] = {
234 "1d3048f1-9de009d0,cd73da34-cf196cb",
235 };
236 const char* kExpectedChunks3[] = {
237 "1d3048f1-9de009d0,cd73da34-cf196cb,6214fa18-9e6dc24d",
238 };
239 const char* kExpectedChunks4[] = {
240 "1d3048f1-9de009d0,cd73da34-cf196cb,6214fa18-9e6dc24d",
241 "4dcb0cd6-d31c4ca1",
242 };
243 const char* kExpectedChunks5[] = {
244 "1d3048f1-9de009d0,cd73da34-cf196cb,6214fa18-9e6dc24d",
245 "4dcb0cd6-d31c4ca1,9d5bce6-30d7d8ac",
246 };
247
248 struct {
249 size_t strings_length;
250 size_t expected_chunks_length;
251 const char** expected_chunks;
252 } cases[] = {
253 { 0, 0, NULL },
254 { 1, arraysize(kExpectedChunks1), kExpectedChunks1 },
255 { 2, arraysize(kExpectedChunks2), kExpectedChunks2 },
256 { 3, arraysize(kExpectedChunks3), kExpectedChunks3 },
257 { 4, arraysize(kExpectedChunks4), kExpectedChunks4 },
258 { 5, arraysize(kExpectedChunks5), kExpectedChunks5 },
259 };
260
261 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
262 ASSERT_LE(cases[i].strings_length, arraysize(kExperimentStrings));
263
264 std::vector<string16> experiments;
265 for (size_t j = 0; j < cases[i].strings_length; ++j)
266 experiments.push_back(UTF8ToUTF16(kExperimentStrings[j]));
267
268 std::vector<string16> chunks;
269 GenerateVariationChunks(experiments, &chunks);
270 ASSERT_EQ(cases[i].expected_chunks_length, chunks.size());
271 for (size_t j = 0; j < chunks.size(); ++j)
272 EXPECT_EQ(UTF8ToUTF16(cases[i].expected_chunks[j]), chunks[j]);
273 }
274 }
275
276 } // namespace chrome_variations
OLDNEW
« no previous file with comments | « chrome/common/metrics/variations_util.cc ('k') | chrome/renderer/chrome_render_process_observer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698