OLD | NEW |
(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 Experiment Helpers. |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/time.h" |
| 11 #include "chrome/common/metrics/experiments_helper.h" |
| 12 #include "content/test/test_browser_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 // Convenience helper to retrieve the GoogleExperimentID for a FieldTrial. Note |
| 18 // that this will do the group assignment in |trial| if not already done. |
| 19 experiments_helper::GoogleExperimentID GetIDForTrial(base::FieldTrial* trial) { |
| 20 return experiments_helper::GetGoogleExperimentID( |
| 21 base::FieldTrial::MakeNameGroupId(trial->name(), |
| 22 trial->group_name())); |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 27 class ExperimentsHelperTest : public ::testing::Test { |
| 28 public: |
| 29 ExperimentsHelperTest() { |
| 30 // Since the API can only be called on the UI thread, we have to fake that |
| 31 // we're on it. |
| 32 ui_thread_.reset(new content::TestBrowserThread( |
| 33 content::BrowserThread::UI, &message_loop_)); |
| 34 |
| 35 base::Time now = base::Time::NowFromSystemTime(); |
| 36 base::TimeDelta oneYear = base::TimeDelta::FromDays(365); |
| 37 base::Time::Exploded exploded; |
| 38 |
| 39 base::Time next_year_time = now + oneYear; |
| 40 next_year_time.LocalExplode(&exploded); |
| 41 next_year_ = exploded.year; |
| 42 } |
| 43 |
| 44 protected: |
| 45 int next_year_; |
| 46 |
| 47 private: |
| 48 MessageLoop message_loop_; |
| 49 scoped_ptr<content::TestBrowserThread> ui_thread_; |
| 50 }; |
| 51 |
| 52 // Test that if the trial is immediately disabled, GetGoogleExperimentID just |
| 53 // returns the empty ID. |
| 54 TEST_F(ExperimentsHelperTest, DisableImmediately) { |
| 55 int default_group_number = -1; |
| 56 scoped_refptr<base::FieldTrial> trial( |
| 57 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, "default", |
| 58 next_year_, 12, 12, |
| 59 &default_group_number)); |
| 60 trial->Disable(); |
| 61 |
| 62 ASSERT_EQ(default_group_number, trial->group()); |
| 63 ASSERT_EQ(experiments_helper::kEmptyGoogleExperimentID, |
| 64 GetIDForTrial(trial.get())); |
| 65 } |
| 66 |
| 67 // Test that successfully associating the FieldTrial with some ID, and then |
| 68 // disabling the FieldTrial actually makes GetGoogleExperimentID correctly |
| 69 // return the empty ID. |
| 70 TEST_F(ExperimentsHelperTest, DisableAfterInitialization) { |
| 71 const std::string default_name = "default"; |
| 72 const std::string non_default_name = "non_default"; |
| 73 |
| 74 scoped_refptr<base::FieldTrial> trial( |
| 75 base::FieldTrialList::FactoryGetFieldTrial("trial", 100, default_name, |
| 76 next_year_, 12, 12, NULL)); |
| 77 trial->AppendGroup(non_default_name, 100); |
| 78 experiments_helper::AssociateGoogleExperimentID( |
| 79 base::FieldTrial::MakeNameGroupId(trial->name(), default_name), 123); |
| 80 experiments_helper::AssociateGoogleExperimentID( |
| 81 base::FieldTrial::MakeNameGroupId(trial->name(), non_default_name), 456); |
| 82 ASSERT_EQ(non_default_name, trial->group_name()); |
| 83 ASSERT_EQ(456U, GetIDForTrial(trial.get())); |
| 84 trial->Disable(); |
| 85 ASSERT_EQ(default_name, trial->group_name()); |
| 86 ASSERT_EQ(123U, GetIDForTrial(trial.get())); |
| 87 } |
| 88 |
| 89 // Test various successful association cases. |
| 90 TEST_F(ExperimentsHelperTest, AssociateGoogleExperimentID) { |
| 91 const std::string default_name1 = "default1"; |
| 92 scoped_refptr<base::FieldTrial> trial_true( |
| 93 base::FieldTrialList::FactoryGetFieldTrial("d1", 10, default_name1, |
| 94 next_year_, 12, 31, NULL)); |
| 95 const std::string winner = "TheWinner"; |
| 96 int winner_group = trial_true->AppendGroup(winner, 10); |
| 97 |
| 98 // Set GoogleExperimentIDs so we can verify that they were chosen correctly. |
| 99 experiments_helper::AssociateGoogleExperimentID( |
| 100 base::FieldTrial::MakeNameGroupId(trial_true->name(), default_name1), |
| 101 123); |
| 102 experiments_helper::AssociateGoogleExperimentID( |
| 103 base::FieldTrial::MakeNameGroupId(trial_true->name(), winner), |
| 104 456); |
| 105 |
| 106 EXPECT_EQ(winner_group, trial_true->group()); |
| 107 EXPECT_EQ(winner, trial_true->group_name()); |
| 108 EXPECT_EQ(456U, GetIDForTrial(trial_true.get())); |
| 109 |
| 110 const std::string default_name2 = "default2"; |
| 111 scoped_refptr<base::FieldTrial> trial_false( |
| 112 base::FieldTrialList::FactoryGetFieldTrial("d2", 10, default_name2, |
| 113 next_year_, 12, 31, NULL)); |
| 114 const std::string loser = "ALoser"; |
| 115 int loser_group = trial_false->AppendGroup(loser, 0); |
| 116 |
| 117 experiments_helper::AssociateGoogleExperimentID( |
| 118 base::FieldTrial::MakeNameGroupId(trial_false->name(), default_name2), |
| 119 123); |
| 120 experiments_helper::AssociateGoogleExperimentID( |
| 121 base::FieldTrial::MakeNameGroupId(trial_false->name(), loser), |
| 122 456); |
| 123 |
| 124 EXPECT_NE(loser_group, trial_false->group()); |
| 125 EXPECT_EQ(123U, GetIDForTrial(trial_false.get())); |
| 126 } |
| 127 |
| 128 // Test that not associating a FieldTrial with any IDs ensure that the empty ID |
| 129 // will be returned. |
| 130 TEST_F(ExperimentsHelperTest, NoAssociation) { |
| 131 const std::string default_name = "default"; |
| 132 scoped_refptr<base::FieldTrial> no_id_trial( |
| 133 base::FieldTrialList::FactoryGetFieldTrial("d3", 10, default_name, |
| 134 next_year_, 12, 31, NULL)); |
| 135 const std::string winner = "TheWinner"; |
| 136 int winner_group = no_id_trial->AppendGroup(winner, 10); |
| 137 |
| 138 // Ensure that despite the fact that a normal winner is elected, it does not |
| 139 // have a valid GoogleExperimentID associated with it. |
| 140 EXPECT_EQ(winner_group, no_id_trial->group()); |
| 141 EXPECT_EQ(winner, no_id_trial->group_name()); |
| 142 EXPECT_EQ(experiments_helper::kEmptyGoogleExperimentID, |
| 143 GetIDForTrial(no_id_trial.get())); |
| 144 } |
OLD | NEW |