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 #include "chrome/common/metrics/variations/variations_util.h" 6 7 #include <set> 8 #include <string> 9 #include <vector> 10 11 #include "base/metrics/field_trial.h" 12 #include "components/variations/metrics_util.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace chrome_variations { 16 17 class VariationsUtilTest : public ::testing::Test { 18 public: 19 VariationsUtilTest() : field_trial_list_(NULL) { 20 } 21 22 virtual ~VariationsUtilTest() { 23 // Ensure that the maps are cleared between tests, since they are stored as 24 // process singletons. 25 testing::ClearAllVariationIDs(); 26 } 27 28 private: 29 base::FieldTrialList field_trial_list_; 30 31 DISALLOW_COPY_AND_ASSIGN(VariationsUtilTest); 32 }; 33 34 TEST_F(VariationsUtilTest, GetFieldTrialActiveGroups) { 35 typedef std::set<ActiveGroupId, ActiveGroupIdCompare> ActiveGroupIdSet; 36 std::string trial_one("trial one"); 37 std::string group_one("group one"); 38 std::string trial_two("trial two"); 39 std::string group_two("group two"); 40 41 base::FieldTrial::ActiveGroups active_groups; 42 base::FieldTrial::ActiveGroup active_group; 43 active_group.trial_name = trial_one; 44 active_group.group_name = group_one; 45 active_groups.push_back(active_group); 46 47 active_group.trial_name = trial_two; 48 active_group.group_name = group_two; 49 active_groups.push_back(active_group); 50 51 // Create our expected groups of IDs. 52 ActiveGroupIdSet expected_groups; 53 ActiveGroupId name_group_id; 54 name_group_id.name = metrics::HashName(trial_one); 55 name_group_id.group = metrics::HashName(group_one); 56 expected_groups.insert(name_group_id); 57 name_group_id.name = metrics::HashName(trial_two); 58 name_group_id.group = metrics::HashName(group_two); 59 expected_groups.insert(name_group_id); 60 61 std::vector<ActiveGroupId> active_group_ids; 62 testing::TestGetFieldTrialActiveGroupIds(active_groups, &active_group_ids); 63 EXPECT_EQ(2U, active_group_ids.size()); 64 for (size_t i = 0; i < active_group_ids.size(); ++i) { 65 ActiveGroupIdSet::iterator expected_group = 66 expected_groups.find(active_group_ids[i]); 67 EXPECT_FALSE(expected_group == expected_groups.end()); 68 expected_groups.erase(expected_group); 69 } 70 EXPECT_EQ(0U, expected_groups.size()); 71 } 72 73 } // namespace chrome_variations 74