Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/omnibox/omnibox_field_trial.h" | 5 #include "chrome/browser/omnibox/omnibox_field_trial.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 9 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| 10 #include "chrome/common/metrics/entropy_provider.h" | 11 #include "chrome/common/metrics/entropy_provider.h" |
| 11 #include "chrome/common/metrics/variations/variations_util.h" | 12 #include "chrome/common/metrics/variations/variations_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 class OmniboxFieldTrialTest : public testing::Test { | 15 class OmniboxFieldTrialTest : public testing::Test { |
| 15 public: | 16 public: |
| 16 OmniboxFieldTrialTest() {} | 17 OmniboxFieldTrialTest() { |
| 17 | |
| 18 static void SetUpTestCase() { | |
| 19 ResetFieldTrialList(); | 18 ResetFieldTrialList(); |
| 20 } | 19 } |
| 21 | 20 |
| 22 static void TearDownTestCase() { | 21 void ResetFieldTrialList() { |
| 23 delete field_trial_list_; | 22 // Destroy the existing FieldTrialList before creating a new one to avoid |
| 24 field_trial_list_ = NULL; | 23 // a DCHECK. |
| 25 } | 24 field_trial_list_.reset(); |
| 26 | 25 field_trial_list_.reset(new base::FieldTrialList( |
| 27 static void ResetFieldTrialList() { | 26 new metrics::SHA1EntropyProvider("foo"))); |
| 28 // It's important to delete the old pointer first which sets | 27 chrome_variations::testing::ClearAllVariationParams(); |
| 29 // FieldTrialList::global_ to NULL. | |
| 30 if (field_trial_list_) | |
| 31 delete field_trial_list_; | |
| 32 field_trial_list_ = new base::FieldTrialList( | |
| 33 new metrics::SHA1EntropyProvider("foo")); | |
| 34 OmniboxFieldTrial::ActivateDynamicTrials(); | 28 OmniboxFieldTrial::ActivateDynamicTrials(); |
| 35 } | 29 } |
| 36 | 30 |
| 37 // Creates and activates a field trial. | 31 // Creates and activates a field trial. |
| 38 static base::FieldTrial* CreateTestTrial(const std::string& name, | 32 static base::FieldTrial* CreateTestTrial(const std::string& name, |
| 39 const std::string& group_name) { | 33 const std::string& group_name) { |
| 40 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( | 34 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( |
| 41 name, group_name); | 35 name, group_name); |
| 42 trial->group(); | 36 trial->group(); |
| 43 return trial; | 37 return trial; |
| 44 } | 38 } |
| 45 | 39 |
| 40 // EXPECTS that demotions[match_type] exists with value expected_value. | |
| 41 static void VerifyDemotion( | |
| 42 const OmniboxFieldTrial::DemotionMultipliers& demotions, | |
| 43 AutocompleteMatchType::Type match_type, | |
| 44 float expected_value); | |
| 45 | |
| 46 private: | 46 private: |
| 47 // Needed for Activate{Static/Dynamic}Trials(). | 47 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 48 static base::FieldTrialList* field_trial_list_; | |
| 49 | 48 |
| 50 DISALLOW_COPY_AND_ASSIGN(OmniboxFieldTrialTest); | 49 DISALLOW_COPY_AND_ASSIGN(OmniboxFieldTrialTest); |
| 51 }; | 50 }; |
| 52 | 51 |
| 53 // static | 52 // static |
| 54 base::FieldTrialList* OmniboxFieldTrialTest::field_trial_list_ = NULL; | 53 void OmniboxFieldTrialTest::VerifyDemotion( |
| 54 const OmniboxFieldTrial::DemotionMultipliers& demotions, | |
| 55 AutocompleteMatchType::Type match_type, | |
| 56 float expected_value) { | |
| 57 OmniboxFieldTrial::DemotionMultipliers::const_iterator demotion_it = | |
| 58 demotions.find(match_type); | |
| 59 EXPECT_TRUE(demotion_it != demotions.end()); | |
|
Peter Kasting
2013/08/06 22:19:40
Nit: ASSERT_TRUE, since you deref this
Mark P
2013/08/06 22:44:49
Done.
| |
| 60 EXPECT_FLOAT_EQ(expected_value, demotion_it->second); | |
| 61 } | |
| 55 | 62 |
| 56 // Test if GetDisabledProviderTypes() properly parses various field trial | 63 // Test if GetDisabledProviderTypes() properly parses various field trial |
| 57 // group names. | 64 // group names. |
| 58 TEST_F(OmniboxFieldTrialTest, GetDisabledProviderTypes) { | 65 TEST_F(OmniboxFieldTrialTest, GetDisabledProviderTypes) { |
| 59 EXPECT_EQ(0, OmniboxFieldTrial::GetDisabledProviderTypes()); | 66 EXPECT_EQ(0, OmniboxFieldTrial::GetDisabledProviderTypes()); |
| 60 | 67 |
| 61 { | 68 { |
| 62 SCOPED_TRACE("Invalid groups"); | 69 SCOPED_TRACE("Invalid groups"); |
| 63 CreateTestTrial("AutocompleteDynamicTrial_0", "DisabledProviders_"); | 70 CreateTestTrial("AutocompleteDynamicTrial_0", "DisabledProviders_"); |
| 64 EXPECT_EQ(0, OmniboxFieldTrial::GetDisabledProviderTypes()); | 71 EXPECT_EQ(0, OmniboxFieldTrial::GetDisabledProviderTypes()); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 ResetFieldTrialList(); | 131 ResetFieldTrialList(); |
| 125 CreateTestTrial("AutocompleteDynamicTrial_2", "EnableZeroSuggest_Queries"); | 132 CreateTestTrial("AutocompleteDynamicTrial_2", "EnableZeroSuggest_Queries"); |
| 126 EXPECT_TRUE(OmniboxFieldTrial::InZeroSuggestFieldTrial()); | 133 EXPECT_TRUE(OmniboxFieldTrial::InZeroSuggestFieldTrial()); |
| 127 | 134 |
| 128 ResetFieldTrialList(); | 135 ResetFieldTrialList(); |
| 129 CreateTestTrial("AutocompleteDynamicTrial_3", "EnableZeroSuggest_URLs"); | 136 CreateTestTrial("AutocompleteDynamicTrial_3", "EnableZeroSuggest_URLs"); |
| 130 EXPECT_TRUE(OmniboxFieldTrial::InZeroSuggestFieldTrial()); | 137 EXPECT_TRUE(OmniboxFieldTrial::InZeroSuggestFieldTrial()); |
| 131 } | 138 } |
| 132 } | 139 } |
| 133 | 140 |
| 141 TEST_F(OmniboxFieldTrialTest, GetDemotionsByTypeWithFallback) { | |
| 142 // Must be the same as kBundledExperimentFieldTrialName | |
| 143 // defined in omnibox_field_trial.cc. | |
| 144 const std::string kTrialName = "OmniboxBundledExperimentV1"; | |
| 145 // Must be the same as kDemoteByTypeRule defined in | |
| 146 // omnibox_field_trial.cc. | |
| 147 const std::string kRuleName = "DemoteByType"; | |
| 148 { | |
| 149 std::map<std::string, std::string> params; | |
| 150 params[kRuleName + ":1"] = "1:50,2:0"; | |
| 151 params[kRuleName + ":3"] = "5:100"; | |
| 152 params[kRuleName + ":*"] = "1:25"; | |
| 153 ASSERT_TRUE(chrome_variations::AssociateVariationParams( | |
| 154 kTrialName, "A", params)); | |
| 155 } | |
| 156 base::FieldTrialList::CreateFieldTrial(kTrialName, "A"); | |
| 157 OmniboxFieldTrial::DemotionMultipliers demotions_by_type; | |
| 158 OmniboxFieldTrial::GetDemotionsByType( | |
| 159 AutocompleteInput::NEW_TAB_PAGE, &demotions_by_type); | |
| 160 ASSERT_EQ(2u, demotions_by_type.size()); | |
| 161 VerifyDemotion(demotions_by_type, AutocompleteMatchType::HISTORY_URL, 0.5); | |
| 162 VerifyDemotion(demotions_by_type, AutocompleteMatchType::HISTORY_TITLE, 0.0); | |
| 163 OmniboxFieldTrial::GetDemotionsByType( | |
| 164 AutocompleteInput::HOMEPAGE, &demotions_by_type); | |
| 165 ASSERT_EQ(1u, demotions_by_type.size()); | |
| 166 VerifyDemotion(demotions_by_type, AutocompleteMatchType::NAVSUGGEST, 1.0); | |
| 167 OmniboxFieldTrial::GetDemotionsByType( | |
| 168 AutocompleteInput::BLANK, &demotions_by_type); | |
| 169 ASSERT_EQ(1u, demotions_by_type.size()); | |
| 170 VerifyDemotion(demotions_by_type, AutocompleteMatchType::HISTORY_URL, 0.25); | |
| 171 } | |
| 172 | |
| 134 TEST_F(OmniboxFieldTrialTest, GetValueForRuleInContext) { | 173 TEST_F(OmniboxFieldTrialTest, GetValueForRuleInContext) { |
| 135 // Must be the same as kBundledExperimentFieldTrialName | 174 // Must be the same as kBundledExperimentFieldTrialName |
| 136 // defined in omnibox_field_trial.cc. | 175 // defined in omnibox_field_trial.cc. |
| 137 const std::string kTrialName = "OmniboxBundledExperimentV1"; | 176 const std::string kTrialName = "OmniboxBundledExperimentV1"; |
| 138 { | 177 { |
| 139 std::map<std::string, std::string> params; | 178 std::map<std::string, std::string> params; |
| 140 // Rule 1 has some exact matches and a global fallback. | 179 // Rule 1 has some exact matches and a global fallback. |
| 141 params["rule1:1"] = "rule1-1-value"; // NEW_TAB_PAGE | 180 params["rule1:1"] = "rule1-1-value"; // NEW_TAB_PAGE |
| 142 params["rule1:3"] = "rule1-3-value"; // HOMEPAGE | 181 params["rule1:3"] = "rule1-3-value"; // HOMEPAGE |
| 143 params["rule1:*"] = "rule1-*-value"; // global | 182 params["rule1:*"] = "rule1-*-value"; // global |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 "rule3-4-value", | 233 "rule3-4-value", |
| 195 OmniboxFieldTrial::GetValueForRuleInContext( | 234 OmniboxFieldTrial::GetValueForRuleInContext( |
| 196 "rule3", AutocompleteInput::OTHER)); // exact match | 235 "rule3", AutocompleteInput::OTHER)); // exact match |
| 197 | 236 |
| 198 // Tests for rule 4 (a missing rule). | 237 // Tests for rule 4 (a missing rule). |
| 199 EXPECT_EQ( | 238 EXPECT_EQ( |
| 200 "", | 239 "", |
| 201 OmniboxFieldTrial::GetValueForRuleInContext( | 240 OmniboxFieldTrial::GetValueForRuleInContext( |
| 202 "rule4", AutocompleteInput::OTHER)); // no rule at all | 241 "rule4", AutocompleteInput::OTHER)); // no rule at all |
| 203 } | 242 } |
| OLD | NEW |