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

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

Issue 1941963002: Add variations params api to get params from features. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added tests Created 4 years, 7 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
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"
7 #include "base/macros.h" 8 #include "base/macros.h"
8 #include "base/metrics/field_trial.h" 9 #include "base/metrics/field_trial.h"
9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
10 11
11 namespace variations { 12 namespace variations {
12 13
13 namespace { 14 namespace {
14 15
15 const VariationID TEST_VALUE_A = 3300200; 16 const VariationID TEST_VALUE_A = 3300200;
16 const VariationID TEST_VALUE_B = 3300201; 17 const VariationID TEST_VALUE_B = 3300201;
17 18
18 // Convenience helper to retrieve the variations::VariationID for a FieldTrial. 19 // 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. 20 // Note that this will do the group assignment in |trial| if not already done.
20 VariationID GetIDForTrial(IDCollectionKey key, base::FieldTrial* trial) { 21 VariationID GetIDForTrial(IDCollectionKey key, base::FieldTrial* trial) {
21 return GetGoogleVariationID(key, trial->trial_name(), trial->group_name()); 22 return GetGoogleVariationID(key, trial->trial_name(), trial->group_name());
22 } 23 }
23 24
24 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date. 25 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date.
25 scoped_refptr<base::FieldTrial> CreateFieldTrial( 26 scoped_refptr<base::FieldTrial> CreateFieldTrial(
26 const std::string& trial_name, 27 const std::string& trial_name,
27 int total_probability, 28 int total_probability,
28 const std::string& default_group_name, 29 const std::string& default_group_name,
29 int* default_group_number) { 30 int* default_group_number) {
30 return base::FieldTrialList::FactoryGetFieldTrial( 31 return base::FieldTrialList::FactoryGetFieldTrial(
31 trial_name, total_probability, default_group_name, 32 trial_name, total_probability, default_group_name,
32 base::FieldTrialList::kNoExpirationYear, 1, 1, 33 base::FieldTrialList::kNoExpirationYear, 1, 1,
33 base::FieldTrial::SESSION_RANDOMIZED, default_group_number); 34 base::FieldTrial::SESSION_RANDOMIZED, default_group_number);
34 } 35 }
35 36
37 void CreateFeatureWithTrial(const base::Feature& feature,
38 base::FeatureList::OverrideState override_state,
39 scoped_refptr<base::FieldTrial> trial) {
Alexei Svitkine (slow) 2016/05/03 17:04:25 Nit: Just pass by FieldTrial*.
jwd 2016/05/03 20:12:27 Done.
40 base::FeatureList::ClearInstanceForTesting();
41 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
42 feature_list->RegisterFieldTrialOverride(feature.name, override_state,
43 trial.get());
44 base::FeatureList::SetInstance(std::move(feature_list));
45 }
46
36 } // namespace 47 } // namespace
37 48
38 class VariationsAssociatedDataTest : public ::testing::Test { 49 class VariationsAssociatedDataTest : public ::testing::Test {
39 public: 50 public:
40 VariationsAssociatedDataTest() : field_trial_list_(NULL) { 51 VariationsAssociatedDataTest() : field_trial_list_(NULL) {
41 } 52 }
42 53
43 ~VariationsAssociatedDataTest() override { 54 ~VariationsAssociatedDataTest() override {
44 // Ensure that the maps are cleared between tests, since they are stored as 55 // Ensure that the maps are cleared between tests, since they are stored as
45 // process singletons. 56 // process singletons.
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName)); 350 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName));
340 scoped_refptr<base::FieldTrial> trial( 351 scoped_refptr<base::FieldTrial> trial(
341 CreateFieldTrial(kTrialName, 100, "A", NULL)); 352 CreateFieldTrial(kTrialName, 100, "A", NULL));
342 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName)); 353 ASSERT_FALSE(base::FieldTrialList::IsTrialActive(kTrialName));
343 354
344 std::map<std::string, std::string> params; 355 std::map<std::string, std::string> params;
345 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x")); 356 EXPECT_EQ(std::string(), GetVariationParamValue(kTrialName, "x"));
346 ASSERT_TRUE(base::FieldTrialList::IsTrialActive(kTrialName)); 357 ASSERT_TRUE(base::FieldTrialList::IsTrialActive(kTrialName));
347 } 358 }
348 359
360 TEST_F(VariationsAssociatedDataTest, GetVariationParamsByFeature) {
361 const std::string kTrialName = "GetVariationParamsByFeature";
362
363 std::map<std::string, std::string> params;
364 params["x"] = "1";
365 variations::AssociateVariationParams(kTrialName, "A", params);
366 scoped_refptr<base::FieldTrial> trial(
367 CreateFieldTrial(kTrialName, 100, "A", NULL));
368
369 const base::Feature kFeature{"TestFeature",
370 base::FEATURE_DISABLED_BY_DEFAULT};
371
372 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_ENABLE_FEATURE,
373 trial);
374
375 std::map<std::string, std::string> actualParams;
376 EXPECT_TRUE(GetVariationParamsByFeature(kFeature, &actualParams));
377 EXPECT_EQ(params, actualParams);
378 }
379
380 TEST_F(VariationsAssociatedDataTest, GetVariationParamValueByFeature) {
381 const std::string kTrialName = "GetVariationParamsByFeature";
382
383 std::map<std::string, std::string> params;
384 params["x"] = "1";
385 variations::AssociateVariationParams(kTrialName, "A", params);
386 scoped_refptr<base::FieldTrial> trial(
387 CreateFieldTrial(kTrialName, 100, "A", NULL));
388
389 const base::Feature kFeature{"TestFeature",
390 base::FEATURE_DISABLED_BY_DEFAULT};
391
392 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_ENABLE_FEATURE,
393 trial);
394
395 std::map<std::string, std::string> actualParams;
396 EXPECT_EQ(params["x"], GetVariationParamValueByFeature(kFeature, "x"));
397 }
398
399 TEST_F(VariationsAssociatedDataTest, GetVariationParamsByFeature_Disable) {
400 const std::string kTrialName = "GetVariationParamsByFeature";
401
402 std::map<std::string, std::string> params;
403 params["x"] = "1";
404 variations::AssociateVariationParams(kTrialName, "A", params);
405 scoped_refptr<base::FieldTrial> trial(
406 CreateFieldTrial(kTrialName, 100, "A", NULL));
407
408 const base::Feature kFeature{"TestFeature",
409 base::FEATURE_DISABLED_BY_DEFAULT};
410
411 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_DISABLE_FEATURE,
412 trial);
413
414 std::map<std::string, std::string> actualParams;
415 EXPECT_FALSE(GetVariationParamsByFeature(kFeature, &actualParams));
416 }
417
418 TEST_F(VariationsAssociatedDataTest, GetVariationParamValueByFeature_Disable) {
419 const std::string kTrialName = "GetVariationParamsByFeature";
420
421 std::map<std::string, std::string> params;
422 params["x"] = "1";
423 variations::AssociateVariationParams(kTrialName, "A", params);
424 scoped_refptr<base::FieldTrial> trial(
425 CreateFieldTrial(kTrialName, 100, "A", NULL));
426
427 const base::Feature kFeature{"TestFeature",
Alexei Svitkine (slow) 2016/05/03 17:04:25 Nit: Maybe move this to the top of the function be
jwd 2016/05/03 20:12:27 Done.
428 base::FEATURE_DISABLED_BY_DEFAULT};
429
430 CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_DISABLE_FEATURE,
431 trial);
432
433 std::map<std::string, std::string> actualParams;
434 EXPECT_EQ(std::string(), GetVariationParamValueByFeature(kFeature, "x"));
435 }
436
349 } // namespace variations 437 } // namespace variations
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698