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

Side by Side Diff: components/variations/variations_associated_data_unittest.cc

Issue 2667553002: Move API for field trial params to base from variations. (Closed)
Patch Set: Created 3 years, 10 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
« no previous file with comments | « components/variations/variations_associated_data.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/variations/variations_associated_data.h" 5 #include "components/variations/variations_associated_data.h"
6 6
7 #include "base/feature_list.h"
8 #include "base/macros.h" 7 #include "base/macros.h"
9 #include "base/metrics/field_trial.h" 8 #include "base/metrics/field_trial.h"
10 #include "base/test/scoped_feature_list.h"
11 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
12 10
13 namespace variations { 11 namespace variations {
14 12
15 namespace { 13 namespace {
16 14
17 const VariationID TEST_VALUE_A = 3300200; 15 const VariationID TEST_VALUE_A = 3300200;
18 const VariationID TEST_VALUE_B = 3300201; 16 const VariationID TEST_VALUE_B = 3300201;
19 17
20 // Convenience helper to retrieve the variations::VariationID for a FieldTrial. 18 // Convenience helper to retrieve the variations::VariationID for a FieldTrial.
(...skipping 18 matching lines...) Expand all
39 37
40 class VariationsAssociatedDataTest : public ::testing::Test { 38 class VariationsAssociatedDataTest : public ::testing::Test {
41 public: 39 public:
42 VariationsAssociatedDataTest() : field_trial_list_(NULL) { 40 VariationsAssociatedDataTest() : field_trial_list_(NULL) {
43 } 41 }
44 42
45 ~VariationsAssociatedDataTest() override { 43 ~VariationsAssociatedDataTest() override {
46 // Ensure that the maps are cleared between tests, since they are stored as 44 // Ensure that the maps are cleared between tests, since they are stored as
47 // process singletons. 45 // process singletons.
48 testing::ClearAllVariationIDs(); 46 testing::ClearAllVariationIDs();
49 testing::ClearAllVariationParams();
50 }
51
52 void CreateFeatureWithTrial(const base::Feature& feature,
53 base::FeatureList::OverrideState override_state,
54 base::FieldTrial* trial) {
55 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
56 feature_list->RegisterFieldTrialOverride(feature.name, override_state,
57 trial);
58 scoped_feature_list_.InitWithFeatureList(std::move(feature_list));
59 } 47 }
60 48
61 private: 49 private:
62 base::FieldTrialList field_trial_list_; 50 base::FieldTrialList field_trial_list_;
63 base::test::ScopedFeatureList scoped_feature_list_;
64 51
65 DISALLOW_COPY_AND_ASSIGN(VariationsAssociatedDataTest); 52 DISALLOW_COPY_AND_ASSIGN(VariationsAssociatedDataTest);
66 }; 53 };
67 54
68 // Test that if the trial is immediately disabled, GetGoogleVariationID just 55 // Test that if the trial is immediately disabled, GetGoogleVariationID just
69 // returns the empty ID. 56 // returns the empty ID.
70 TEST_F(VariationsAssociatedDataTest, DisableImmediately) { 57 TEST_F(VariationsAssociatedDataTest, DisableImmediately) {
71 int default_group_number = -1; 58 int default_group_number = -1;
72 scoped_refptr<base::FieldTrial> trial( 59 scoped_refptr<base::FieldTrial> trial(
73 CreateFieldTrial("trial", 100, "default", &default_group_number)); 60 CreateFieldTrial("trial", 100, "default", &default_group_number));
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 GetIDForTrial(CHROME_SYNC_SERVICE, trial_true.get())); 196 GetIDForTrial(CHROME_SYNC_SERVICE, trial_true.get()));
210 197
211 AssociateGoogleVariationID(CHROME_SYNC_SERVICE, trial_true->trial_name(), 198 AssociateGoogleVariationID(CHROME_SYNC_SERVICE, trial_true->trial_name(),
212 default_name, TEST_VALUE_A); 199 default_name, TEST_VALUE_A);
213 EXPECT_EQ(TEST_VALUE_A, 200 EXPECT_EQ(TEST_VALUE_A,
214 GetIDForTrial(GOOGLE_WEB_PROPERTIES_TRIGGER, trial_true.get())); 201 GetIDForTrial(GOOGLE_WEB_PROPERTIES_TRIGGER, trial_true.get()));
215 EXPECT_EQ(TEST_VALUE_A, 202 EXPECT_EQ(TEST_VALUE_A,
216 GetIDForTrial(CHROME_SYNC_SERVICE, trial_true.get())); 203 GetIDForTrial(CHROME_SYNC_SERVICE, trial_true.get()));
217 } 204 }
218 205
219 TEST_F(VariationsAssociatedDataTest, AssociateVariationParams) {
220 const std::string kTrialName = "AssociateVariationParams";
221
222 {
223 std::map<std::string, std::string> params;
224 params["a"] = "10";
225 params["b"] = "test";
226 ASSERT_TRUE(AssociateVariationParams(kTrialName, "A", params));
227 }
228 {
229 std::map<std::string, std::string> params;
230 params["a"] = "5";
231 ASSERT_TRUE(AssociateVariationParams(kTrialName, "B", params));
232 }
233
234 base::FieldTrialList::CreateFieldTrial(kTrialName, "B");
235 EXPECT_EQ("5", GetVariationParamValue(kTrialName, "a"));
236 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "b"));
237 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x"));
238
239 std::map<std::string, std::string> params;
240 EXPECT_TRUE(GetVariationParams(kTrialName, &params));
241 EXPECT_EQ(1U, params.size());
242 EXPECT_EQ("5", params["a"]);
243 }
244
245 TEST_F(VariationsAssociatedDataTest, AssociateVariationParams_Fail) {
246 const std::string kTrialName = "AssociateVariationParams_Fail";
247 const std::string kGroupName = "A";
248
249 std::map<std::string, std::string> params;
250 params["a"] = "10";
251 ASSERT_TRUE(AssociateVariationParams(kTrialName, kGroupName, params));
252 params["a"] = "1";
253 params["b"] = "2";
254 ASSERT_FALSE(AssociateVariationParams(kTrialName, kGroupName, params));
255
256 base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName);
257 EXPECT_EQ("10", GetVariationParamValue(kTrialName, "a"));
258 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "b"));
259 }
260
261 TEST_F(VariationsAssociatedDataTest, AssociateVariationParams_TrialActiveFail) {
262 const std::string kTrialName = "AssociateVariationParams_TrialActiveFail";
263 base::FieldTrialList::CreateFieldTrial(kTrialName, "A");
264 ASSERT_EQ("A", base::FieldTrialList::FindFullName(kTrialName));
265
266 std::map<std::string, std::string> params;
267 params["a"] = "10";
268 EXPECT_FALSE(AssociateVariationParams(kTrialName, "B", params));
269 EXPECT_FALSE(AssociateVariationParams(kTrialName, "A", params));
270 }
271
272 TEST_F(VariationsAssociatedDataTest,
273 AssociateVariationParams_DoesntActivateTrial) {
274 const std::string kTrialName = "AssociateVariationParams_DoesntActivateTrial";
275
276 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName));
277 scoped_refptr<base::FieldTrial> trial(
278 CreateFieldTrial(kTrialName, 100, "A", NULL));
279 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName));
280
281 std::map<std::string, std::string> params;
282 params["a"] = "10";
283 EXPECT_TRUE(AssociateVariationParams(kTrialName, "A", params));
284 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName));
285 }
286
287 TEST_F(VariationsAssociatedDataTest, GetVariationParams_NoTrial) {
288 const std::string kTrialName = "GetVariationParams_NoParams";
289
290 std::map<std::string, std::string> params;
291 EXPECT_FALSE(GetVariationParams(kTrialName, &params));
292 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x"));
293 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "y"));
294 }
295
296 TEST_F(VariationsAssociatedDataTest, GetVariationParams_NoParams) {
297 const std::string kTrialName = "GetVariationParams_NoParams";
298
299 base::FieldTrialList::CreateFieldTrial(kTrialName, "A");
300
301 std::map<std::string, std::string> params;
302 EXPECT_FALSE(GetVariationParams(kTrialName, &params));
303 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x"));
304 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "y"));
305 }
306
307 TEST_F(VariationsAssociatedDataTest, GetVariationParams_ActivatesTrial) {
308 const std::string kTrialName = "GetVariationParams_ActivatesTrial";
309
310 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName));
311 scoped_refptr<base::FieldTrial> trial(
312 CreateFieldTrial(kTrialName, 100, "A", NULL));
313 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName));
314
315 std::map<std::string, std::string> params;
316 EXPECT_FALSE(GetVariationParams(kTrialName, &params));
317 ASSERT_TRUE(base::FieldTrialList::IsTrialActive(kTrialName));
318 }
319
320 TEST_F(VariationsAssociatedDataTest, GetVariationParamValue_ActivatesTrial) {
321 const std::string kTrialName = "GetVariationParamValue_ActivatesTrial";
322
323 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName));
324 scoped_refptr<base::FieldTrial> trial(
325 CreateFieldTrial(kTrialName, 100, "A", NULL));
326 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName));
327
328 std::map<std::string, std::string> params;
329 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x"));
330 ASSERT_TRUE(base::FieldTrialList::IsTrialActive(kTrialName));
331 }
332
333 TEST_F(VariationsAssociatedDataTest, GetVariationParamsByFeature) {
334 const std::string kTrialName = "GetVariationParamsByFeature";
335 const base::Feature kFeature{"TestFeature",
336 base::FEATURE_DISABLED_BY_DEFAULT};
337
338 std::map<std::string, std::string> params;
339 params["x"] = "1";
340 variations::AssociateVariationParams(kTrialName, "A", params);
341 scoped_refptr<base::FieldTrial> trial(
342 CreateFieldTrial(kTrialName, 100, "A", NULL));
343
344 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_ENABLE_FEATURE,
345 trial.get());
346
347 std::map<std::string, std::string> actualParams;
348 EXPECT_TRUE(GetVariationParamsByFeature(kFeature, &actualParams));
349 EXPECT_EQ(params, actualParams);
350 }
351
352 TEST_F(VariationsAssociatedDataTest, GetVariationParamValueByFeature) {
353 const std::string kTrialName = "GetVariationParamsByFeature";
354 const base::Feature kFeature{"TestFeature",
355 base::FEATURE_DISABLED_BY_DEFAULT};
356
357 std::map<std::string, std::string> params;
358 params["x"] = "1";
359 variations::AssociateVariationParams(kTrialName, "A", params);
360 scoped_refptr<base::FieldTrial> trial(
361 CreateFieldTrial(kTrialName, 100, "A", NULL));
362
363 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_ENABLE_FEATURE,
364 trial.get());
365
366 std::map<std::string, std::string> actualParams;
367 EXPECT_EQ(params["x"], GetVariationParamValueByFeature(kFeature, "x"));
368 }
369
370 TEST_F(VariationsAssociatedDataTest, GetVariationParamsByFeature_Disable) {
371 const std::string kTrialName = "GetVariationParamsByFeature";
372 const base::Feature kFeature{"TestFeature",
373 base::FEATURE_DISABLED_BY_DEFAULT};
374
375 std::map<std::string, std::string> params;
376 params["x"] = "1";
377 variations::AssociateVariationParams(kTrialName, "A", params);
378 scoped_refptr<base::FieldTrial> trial(
379 CreateFieldTrial(kTrialName, 100, "A", NULL));
380
381 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_DISABLE_FEATURE,
382 trial.get());
383
384 std::map<std::string, std::string> actualParams;
385 EXPECT_FALSE(GetVariationParamsByFeature(kFeature, &actualParams));
386 }
387
388 TEST_F(VariationsAssociatedDataTest, GetVariationParamValueByFeature_Disable) {
389 const std::string kTrialName = "GetVariationParamsByFeature";
390 const base::Feature kFeature{"TestFeature",
391 base::FEATURE_DISABLED_BY_DEFAULT};
392
393 std::map<std::string, std::string> params;
394 params["x"] = "1";
395 variations::AssociateVariationParams(kTrialName, "A", params);
396 scoped_refptr<base::FieldTrial> trial(
397 CreateFieldTrial(kTrialName, 100, "A", NULL));
398
399 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_DISABLE_FEATURE,
400 trial.get());
401
402 std::map<std::string, std::string> actualParams;
403 EXPECT_EQ(std::string(), GetVariationParamValueByFeature(kFeature, "x"));
404 }
405
406 TEST_F(VariationsAssociatedDataTest, GetVariationParamByFeatureAsInt) {
407 const std::string kTrialName = "GetVariationParamsByFeature";
408 const base::Feature kFeature{"TestFeature",
409 base::FEATURE_DISABLED_BY_DEFAULT};
410
411 std::map<std::string, std::string> params;
412 params["a"] = "1";
413 params["b"] = "1.5";
414 params["c"] = "foo";
415 params["d"] = "";
416 // "e" is not registered
417 variations::AssociateVariationParams(kTrialName, "A", params);
418 scoped_refptr<base::FieldTrial> trial(
419 CreateFieldTrial(kTrialName, 100, "A", NULL));
420
421 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_ENABLE_FEATURE,
422 trial.get());
423
424 std::map<std::string, std::string> actualParams;
425 EXPECT_EQ(1, GetVariationParamByFeatureAsInt(kFeature, "a", 0));
426 EXPECT_EQ(0, GetVariationParamByFeatureAsInt(kFeature, "b", 0)); // invalid
427 EXPECT_EQ(0, GetVariationParamByFeatureAsInt(kFeature, "c", 0)); // invalid
428 EXPECT_EQ(0, GetVariationParamByFeatureAsInt(kFeature, "d", 0)); // empty
429 EXPECT_EQ(0, GetVariationParamByFeatureAsInt(kFeature, "e", 0)); // empty
430 }
431
432 TEST_F(VariationsAssociatedDataTest, GetVariationParamByFeatureAsDouble) {
433 const std::string kTrialName = "GetVariationParamsByFeature";
434 const base::Feature kFeature{"TestFeature",
435 base::FEATURE_DISABLED_BY_DEFAULT};
436
437 std::map<std::string, std::string> params;
438 params["a"] = "1";
439 params["b"] = "1.5";
440 params["c"] = "1.0e-10";
441 params["d"] = "foo";
442 params["e"] = "";
443 // "f" is not registered
444 variations::AssociateVariationParams(kTrialName, "A", params);
445 scoped_refptr<base::FieldTrial> trial(
446 CreateFieldTrial(kTrialName, 100, "A", NULL));
447
448 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_ENABLE_FEATURE,
449 trial.get());
450
451 std::map<std::string, std::string> actualParams;
452 EXPECT_EQ(1, GetVariationParamByFeatureAsDouble(kFeature, "a", 0));
453 EXPECT_EQ(1.5, GetVariationParamByFeatureAsDouble(kFeature, "b", 0));
454 EXPECT_EQ(1.0e-10, GetVariationParamByFeatureAsDouble(kFeature, "c", 0));
455 EXPECT_EQ(0,
456 GetVariationParamByFeatureAsDouble(kFeature, "d", 0)); // invalid
457 EXPECT_EQ(0, GetVariationParamByFeatureAsDouble(kFeature, "e", 0)); // empty
458 EXPECT_EQ(0, GetVariationParamByFeatureAsDouble(kFeature, "f", 0)); // empty
459 }
460
461 TEST_F(VariationsAssociatedDataTest, GetVariationParamByFeatureAsBool) {
462 const std::string kTrialName = "GetVariationParamsByFeature";
463 const base::Feature kFeature{"TestFeature",
464 base::FEATURE_DISABLED_BY_DEFAULT};
465
466 std::map<std::string, std::string> params;
467 params["a"] = "true";
468 params["b"] = "false";
469 params["c"] = "1";
470 params["d"] = "False";
471 params["e"] = "";
472 // "f" is not registered
473 variations::AssociateVariationParams(kTrialName, "A", params);
474 scoped_refptr<base::FieldTrial> trial(
475 CreateFieldTrial(kTrialName, 100, "A", NULL));
476
477 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_ENABLE_FEATURE,
478 trial.get());
479
480 std::map<std::string, std::string> actualParams;
481 EXPECT_TRUE(GetVariationParamByFeatureAsBool(kFeature, "a", false));
482 EXPECT_FALSE(GetVariationParamByFeatureAsBool(kFeature, "b", true));
483 EXPECT_FALSE(
484 GetVariationParamByFeatureAsBool(kFeature, "c", false)); // invalid
485 EXPECT_TRUE(
486 GetVariationParamByFeatureAsBool(kFeature, "d", true)); // invalid
487 EXPECT_TRUE(GetVariationParamByFeatureAsBool(kFeature, "e", true)); // empty
488 EXPECT_TRUE(GetVariationParamByFeatureAsBool(kFeature, "f", true)); // empty
489 }
490
491 } // namespace variations 206 } // namespace variations
OLDNEW
« no previous file with comments | « components/variations/variations_associated_data.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698