1 // Copyright 2015 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 "chrome/common/variations/variations_util.h"
7 #include "base/metrics/field_trial.h"
8 #include "chrome/common/variations/fieldtrial_testing_config.h"
9 #include "components/variations/variations_associated_data.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace chrome_variations
{
14 class VariationsUtilTest
: public ::testing::Test
{
16 VariationsUtilTest() : field_trial_list_(NULL
) {}
18 ~VariationsUtilTest() override
{
19 // Ensure that the maps are cleared between tests, since they are stored as
20 // process singletons.
21 variations::testing::ClearAllVariationIDs();
22 variations::testing::ClearAllVariationParams();
26 base::FieldTrialList field_trial_list_
;
28 DISALLOW_COPY_AND_ASSIGN(VariationsUtilTest
);
31 TEST_F(VariationsUtilTest
, AssociateParamsFromString
) {
32 const std::string kTrialName
= "AssociateVariationParams";
33 const std::string kVariationsString
=
34 "AssociateVariationParams.A:a/10/b/test,AssociateVariationParams.B:a/%2F";
35 ASSERT_TRUE(AssociateParamsFromString(kVariationsString
));
37 base::FieldTrialList::CreateFieldTrial(kTrialName
, "B");
38 EXPECT_EQ("/", variations::GetVariationParamValue(kTrialName
, "a"));
39 EXPECT_EQ(std::string(), variations::GetVariationParamValue(kTrialName
, "b"));
40 EXPECT_EQ(std::string(), variations::GetVariationParamValue(kTrialName
, "x"));
42 std::map
<std::string
, std::string
> params
;
43 EXPECT_TRUE(variations::GetVariationParams(kTrialName
, ¶ms
));
44 EXPECT_EQ(1U, params
.size());
45 EXPECT_EQ("/", params
["a"]);
48 TEST_F(VariationsUtilTest
, AssociateParamsFromFieldTrialConfig
) {
49 const FieldTrialGroupParams array_kFieldTrialConfig_params
[] = {{"x", "1"},
52 const FieldTrialTestingGroup array_kFieldTrialConfig_groups
[] = {
53 {"TestStudy1", "TestGroup1", array_kFieldTrialConfig_params
, 2},
54 {"TestStudy2", "TestGroup2", NULL
, 0}};
56 const FieldTrialTestingConfig kConfig
= {
57 array_kFieldTrialConfig_groups
, 2,
59 AssociateParamsFromFieldTrialConfig(kConfig
);
61 EXPECT_EQ("1", variations::GetVariationParamValue("TestStudy1", "x"));
62 EXPECT_EQ("2", variations::GetVariationParamValue("TestStudy1", "y"));
64 std::map
<std::string
, std::string
> params
;
65 EXPECT_TRUE(variations::GetVariationParams("TestStudy1", ¶ms
));
66 EXPECT_EQ(2U, params
.size());
67 EXPECT_EQ("1", params
["x"]);
68 EXPECT_EQ("2", params
["y"]);
70 EXPECT_EQ("TestGroup1", base::FieldTrialList::FindFullName("TestStudy1"));
71 EXPECT_EQ("TestGroup2", base::FieldTrialList::FindFullName("TestStudy2"));
74 } // namespace chrome_variations