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/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "chrome/common/metrics/entropy_provider.h" | 10 #include "chrome/common/metrics/entropy_provider.h" |
11 #include "chrome/common/metrics/variations/variations_util.h" | 11 #include "chrome/common/metrics/variations/variations_util.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 class OmniboxFieldTrialTest : public testing::Test { | 14 class OmniboxFieldTrialTest : public testing::Test { |
15 public: | 15 public: |
16 OmniboxFieldTrialTest() {} | 16 OmniboxFieldTrialTest() { |
17 | |
18 static void SetUpTestCase() { | |
19 ResetFieldTrialList(); | 17 ResetFieldTrialList(); |
20 } | 18 } |
21 | 19 |
22 static void TearDownTestCase() { | 20 static void TearDownTestCase() { |
23 delete field_trial_list_; | 21 delete field_trial_list_; |
Alexei Svitkine (slow)
2013/08/05 18:33:11
If you make field_trial_list_ a non-static scoped_
Mark P
2013/08/05 22:11:41
If I make the changes you suggest, I get a failure
Alexei Svitkine (slow)
2013/08/06 18:20:41
Hmm, that should not be happening if you're actual
Mark P
2013/08/06 19:52:18
Stack trace below. I put the code I used to gener
| |
24 field_trial_list_ = NULL; | 22 field_trial_list_ = NULL; |
25 } | 23 } |
26 | 24 |
27 static void ResetFieldTrialList() { | 25 static void ResetFieldTrialList() { |
28 // It's important to delete the old pointer first which sets | 26 // It's important to delete the old pointer first which sets |
29 // FieldTrialList::global_ to NULL. | 27 // FieldTrialList::global_ to NULL. |
30 if (field_trial_list_) | 28 if (field_trial_list_) |
31 delete field_trial_list_; | 29 delete field_trial_list_; |
32 field_trial_list_ = new base::FieldTrialList( | 30 field_trial_list_ = new base::FieldTrialList( |
33 new metrics::SHA1EntropyProvider("foo")); | 31 new metrics::SHA1EntropyProvider("foo")); |
32 chrome_variations::testing::ClearAllVariationParams(); | |
34 OmniboxFieldTrial::ActivateDynamicTrials(); | 33 OmniboxFieldTrial::ActivateDynamicTrials(); |
35 } | 34 } |
36 | 35 |
37 // Creates and activates a field trial. | 36 // Creates and activates a field trial. |
38 static base::FieldTrial* CreateTestTrial(const std::string& name, | 37 static base::FieldTrial* CreateTestTrial(const std::string& name, |
39 const std::string& group_name) { | 38 const std::string& group_name) { |
40 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( | 39 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( |
41 name, group_name); | 40 name, group_name); |
42 trial->group(); | 41 trial->group(); |
43 return trial; | 42 return trial; |
44 } | 43 } |
45 | 44 |
46 private: | 45 private: |
47 // Needed for Activate{Static/Dynamic}Trials(). | |
48 static base::FieldTrialList* field_trial_list_; | 46 static base::FieldTrialList* field_trial_list_; |
49 | 47 |
50 DISALLOW_COPY_AND_ASSIGN(OmniboxFieldTrialTest); | 48 DISALLOW_COPY_AND_ASSIGN(OmniboxFieldTrialTest); |
51 }; | 49 }; |
52 | 50 |
53 // static | 51 // static |
54 base::FieldTrialList* OmniboxFieldTrialTest::field_trial_list_ = NULL; | 52 base::FieldTrialList* OmniboxFieldTrialTest::field_trial_list_ = NULL; |
55 | 53 |
56 // Test if GetDisabledProviderTypes() properly parses various field trial | 54 // Test if GetDisabledProviderTypes() properly parses various field trial |
57 // group names. | 55 // group names. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 ResetFieldTrialList(); | 122 ResetFieldTrialList(); |
125 CreateTestTrial("AutocompleteDynamicTrial_2", "EnableZeroSuggest_Queries"); | 123 CreateTestTrial("AutocompleteDynamicTrial_2", "EnableZeroSuggest_Queries"); |
126 EXPECT_TRUE(OmniboxFieldTrial::InZeroSuggestFieldTrial()); | 124 EXPECT_TRUE(OmniboxFieldTrial::InZeroSuggestFieldTrial()); |
127 | 125 |
128 ResetFieldTrialList(); | 126 ResetFieldTrialList(); |
129 CreateTestTrial("AutocompleteDynamicTrial_3", "EnableZeroSuggest_URLs"); | 127 CreateTestTrial("AutocompleteDynamicTrial_3", "EnableZeroSuggest_URLs"); |
130 EXPECT_TRUE(OmniboxFieldTrial::InZeroSuggestFieldTrial()); | 128 EXPECT_TRUE(OmniboxFieldTrial::InZeroSuggestFieldTrial()); |
131 } | 129 } |
132 } | 130 } |
133 | 131 |
132 TEST_F(OmniboxFieldTrialTest, GetDemotionsByTypeWithFallback) { | |
133 // Must be the same as kBundledExperimentFieldTrialName | |
134 // defined in omnibox_field_trial.cc. | |
135 const std::string kTrialName = "OmniboxBundledExperimentV1"; | |
136 // Must be the same as kDemoteByTypeRule defined in | |
137 // omnibox_field_trial.cc. | |
138 const std::string kRuleName = "DemoteByType"; | |
139 { | |
140 std::map<std::string, std::string> params; | |
141 params[kRuleName + ":1"] = "1:50,2:0"; | |
142 params[kRuleName + ":3"] = "5:100"; | |
143 params[kRuleName + ":*"] = "1:25"; | |
144 ASSERT_TRUE(chrome_variations::AssociateVariationParams( | |
145 kTrialName, "A", params)); | |
146 } | |
147 base::FieldTrialList::CreateFieldTrial(kTrialName, "A"); | |
148 OmniboxFieldTrial::DemotionMultiplierByType demotions_by_type; | |
149 OmniboxFieldTrial::GetDemotionsByType( | |
150 AutocompleteInput::NEW_TAB_PAGE, &demotions_by_type); | |
151 ASSERT_EQ(2u, demotions_by_type.size()); | |
152 EXPECT_FLOAT_EQ(0.5f, demotions_by_type[AutocompleteMatchType::HISTORY_URL]); | |
153 EXPECT_FLOAT_EQ(0.0f, | |
154 demotions_by_type[AutocompleteMatchType::HISTORY_TITLE]); | |
Peter Kasting
2013/08/06 19:33:50
I think this expectation will still pass even if t
Mark P
2013/08/06 22:00:15
Good point. Added helper function.
| |
155 OmniboxFieldTrial::GetDemotionsByType( | |
156 AutocompleteInput::HOMEPAGE, &demotions_by_type); | |
157 ASSERT_EQ(1u, demotions_by_type.size()); | |
158 EXPECT_FLOAT_EQ(1.0f, demotions_by_type[AutocompleteMatchType::NAVSUGGEST]); | |
159 OmniboxFieldTrial::GetDemotionsByType( | |
160 AutocompleteInput::BLANK, &demotions_by_type); | |
161 ASSERT_EQ(1u, demotions_by_type.size()); | |
162 EXPECT_FLOAT_EQ(0.25f, demotions_by_type[AutocompleteMatchType::HISTORY_URL]); | |
163 } | |
164 | |
134 TEST_F(OmniboxFieldTrialTest, GetValueForRuleInContext) { | 165 TEST_F(OmniboxFieldTrialTest, GetValueForRuleInContext) { |
135 // Must be the same as kBundledExperimentFieldTrialName | 166 // Must be the same as kBundledExperimentFieldTrialName |
136 // defined in omnibox_field_trial.cc. | 167 // defined in omnibox_field_trial.cc. |
137 const std::string kTrialName = "OmniboxBundledExperimentV1"; | 168 const std::string kTrialName = "OmniboxBundledExperimentV1"; |
138 { | 169 { |
139 std::map<std::string, std::string> params; | 170 std::map<std::string, std::string> params; |
140 // Rule 1 has some exact matches and a global fallback. | 171 // Rule 1 has some exact matches and a global fallback. |
141 params["rule1:1"] = "rule1-1-value"; // NEW_TAB_PAGE | 172 params["rule1:1"] = "rule1-1-value"; // NEW_TAB_PAGE |
142 params["rule1:3"] = "rule1-3-value"; // HOMEPAGE | 173 params["rule1:3"] = "rule1-3-value"; // HOMEPAGE |
143 params["rule1:*"] = "rule1-*-value"; // global | 174 params["rule1:*"] = "rule1-*-value"; // global |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 "rule3-4-value", | 225 "rule3-4-value", |
195 OmniboxFieldTrial::GetValueForRuleInContext( | 226 OmniboxFieldTrial::GetValueForRuleInContext( |
196 "rule3", AutocompleteInput::OTHER)); // exact match | 227 "rule3", AutocompleteInput::OTHER)); // exact match |
197 | 228 |
198 // Tests for rule 4 (a missing rule). | 229 // Tests for rule 4 (a missing rule). |
199 EXPECT_EQ( | 230 EXPECT_EQ( |
200 "", | 231 "", |
201 OmniboxFieldTrial::GetValueForRuleInContext( | 232 OmniboxFieldTrial::GetValueForRuleInContext( |
202 "rule4", AutocompleteInput::OTHER)); // no rule at all | 233 "rule4", AutocompleteInput::OTHER)); // no rule at all |
203 } | 234 } |
OLD | NEW |