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/command_line.h" |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
9 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
10 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "chrome/browser/search/search.h" |
| 13 #include "chrome/common/chrome_switches.h" |
11 #include "chrome/common/metrics/entropy_provider.h" | 14 #include "chrome/common/metrics/entropy_provider.h" |
12 #include "chrome/common/metrics/variations/variations_util.h" | 15 #include "chrome/common/metrics/variations/variations_util.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
14 | 17 |
15 class OmniboxFieldTrialTest : public testing::Test { | 18 class OmniboxFieldTrialTest : public testing::Test { |
16 public: | 19 public: |
17 OmniboxFieldTrialTest() { | 20 OmniboxFieldTrialTest() { |
18 ResetFieldTrialList(); | 21 ResetFieldTrialList(); |
19 } | 22 } |
20 | 23 |
(...skipping 15 matching lines...) Expand all Loading... |
36 trial->group(); | 39 trial->group(); |
37 return trial; | 40 return trial; |
38 } | 41 } |
39 | 42 |
40 // EXPECTS that demotions[match_type] exists with value expected_value. | 43 // EXPECTS that demotions[match_type] exists with value expected_value. |
41 static void VerifyDemotion( | 44 static void VerifyDemotion( |
42 const OmniboxFieldTrial::DemotionMultipliers& demotions, | 45 const OmniboxFieldTrial::DemotionMultipliers& demotions, |
43 AutocompleteMatchType::Type match_type, | 46 AutocompleteMatchType::Type match_type, |
44 float expected_value); | 47 float expected_value); |
45 | 48 |
| 49 // EXPECT()s that OmniboxFieldTrial::GetValueForRuleInContext(|rule|, |
| 50 // |page_classification|) returns |rule_value|. |
| 51 static void ExpectRuleValue( |
| 52 const std::string& rule_value, |
| 53 const std::string& rule, |
| 54 AutocompleteInput::PageClassification page_classification); |
| 55 |
46 private: | 56 private: |
47 scoped_ptr<base::FieldTrialList> field_trial_list_; | 57 scoped_ptr<base::FieldTrialList> field_trial_list_; |
48 | 58 |
49 DISALLOW_COPY_AND_ASSIGN(OmniboxFieldTrialTest); | 59 DISALLOW_COPY_AND_ASSIGN(OmniboxFieldTrialTest); |
50 }; | 60 }; |
51 | 61 |
52 // static | 62 // static |
53 void OmniboxFieldTrialTest::VerifyDemotion( | 63 void OmniboxFieldTrialTest::VerifyDemotion( |
54 const OmniboxFieldTrial::DemotionMultipliers& demotions, | 64 const OmniboxFieldTrial::DemotionMultipliers& demotions, |
55 AutocompleteMatchType::Type match_type, | 65 AutocompleteMatchType::Type match_type, |
56 float expected_value) { | 66 float expected_value) { |
57 OmniboxFieldTrial::DemotionMultipliers::const_iterator demotion_it = | 67 OmniboxFieldTrial::DemotionMultipliers::const_iterator demotion_it = |
58 demotions.find(match_type); | 68 demotions.find(match_type); |
59 ASSERT_TRUE(demotion_it != demotions.end()); | 69 ASSERT_TRUE(demotion_it != demotions.end()); |
60 EXPECT_FLOAT_EQ(expected_value, demotion_it->second); | 70 EXPECT_FLOAT_EQ(expected_value, demotion_it->second); |
61 } | 71 } |
62 | 72 |
| 73 // static |
| 74 void OmniboxFieldTrialTest::ExpectRuleValue( |
| 75 const std::string& rule_value, |
| 76 const std::string& rule, |
| 77 AutocompleteInput::PageClassification page_classification) { |
| 78 EXPECT_EQ(rule_value, |
| 79 OmniboxFieldTrial::GetValueForRuleInContext( |
| 80 rule, page_classification)); |
| 81 } |
| 82 |
63 // Test if GetDisabledProviderTypes() properly parses various field trial | 83 // Test if GetDisabledProviderTypes() properly parses various field trial |
64 // group names. | 84 // group names. |
65 TEST_F(OmniboxFieldTrialTest, GetDisabledProviderTypes) { | 85 TEST_F(OmniboxFieldTrialTest, GetDisabledProviderTypes) { |
66 EXPECT_EQ(0, OmniboxFieldTrial::GetDisabledProviderTypes()); | 86 EXPECT_EQ(0, OmniboxFieldTrial::GetDisabledProviderTypes()); |
67 | 87 |
68 { | 88 { |
69 SCOPED_TRACE("Invalid groups"); | 89 SCOPED_TRACE("Invalid groups"); |
70 CreateTestTrial("AutocompleteDynamicTrial_0", "DisabledProviders_"); | 90 CreateTestTrial("AutocompleteDynamicTrial_0", "DisabledProviders_"); |
71 EXPECT_EQ(0, OmniboxFieldTrial::GetDisabledProviderTypes()); | 91 EXPECT_EQ(0, OmniboxFieldTrial::GetDisabledProviderTypes()); |
72 ResetFieldTrialList(); | 92 ResetFieldTrialList(); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 | 160 |
141 TEST_F(OmniboxFieldTrialTest, GetDemotionsByTypeWithFallback) { | 161 TEST_F(OmniboxFieldTrialTest, GetDemotionsByTypeWithFallback) { |
142 // Must be the same as kBundledExperimentFieldTrialName | 162 // Must be the same as kBundledExperimentFieldTrialName |
143 // defined in omnibox_field_trial.cc. | 163 // defined in omnibox_field_trial.cc. |
144 const std::string kTrialName = "OmniboxBundledExperimentV1"; | 164 const std::string kTrialName = "OmniboxBundledExperimentV1"; |
145 // Must be the same as kDemoteByTypeRule defined in | 165 // Must be the same as kDemoteByTypeRule defined in |
146 // omnibox_field_trial.cc. | 166 // omnibox_field_trial.cc. |
147 const std::string kRuleName = "DemoteByType"; | 167 const std::string kRuleName = "DemoteByType"; |
148 { | 168 { |
149 std::map<std::string, std::string> params; | 169 std::map<std::string, std::string> params; |
150 params[kRuleName + ":1"] = "1:50,2:0"; | 170 params[kRuleName + ":1:*"] = "1:50,2:0"; |
151 params[kRuleName + ":3"] = "5:100"; | 171 params[kRuleName + ":3:*"] = "5:100"; |
152 params[kRuleName + ":*"] = "1:25"; | 172 params[kRuleName + ":*:*"] = "1:25"; |
153 ASSERT_TRUE(chrome_variations::AssociateVariationParams( | 173 ASSERT_TRUE(chrome_variations::AssociateVariationParams( |
154 kTrialName, "A", params)); | 174 kTrialName, "A", params)); |
155 } | 175 } |
156 base::FieldTrialList::CreateFieldTrial(kTrialName, "A"); | 176 base::FieldTrialList::CreateFieldTrial(kTrialName, "A"); |
157 OmniboxFieldTrial::DemotionMultipliers demotions_by_type; | 177 OmniboxFieldTrial::DemotionMultipliers demotions_by_type; |
158 OmniboxFieldTrial::GetDemotionsByType( | 178 OmniboxFieldTrial::GetDemotionsByType( |
159 AutocompleteInput::NEW_TAB_PAGE, &demotions_by_type); | 179 AutocompleteInput::NEW_TAB_PAGE, &demotions_by_type); |
160 ASSERT_EQ(2u, demotions_by_type.size()); | 180 ASSERT_EQ(2u, demotions_by_type.size()); |
161 VerifyDemotion(demotions_by_type, AutocompleteMatchType::HISTORY_URL, 0.5); | 181 VerifyDemotion(demotions_by_type, AutocompleteMatchType::HISTORY_URL, 0.5); |
162 VerifyDemotion(demotions_by_type, AutocompleteMatchType::HISTORY_TITLE, 0.0); | 182 VerifyDemotion(demotions_by_type, AutocompleteMatchType::HISTORY_TITLE, 0.0); |
163 OmniboxFieldTrial::GetDemotionsByType( | 183 OmniboxFieldTrial::GetDemotionsByType( |
164 AutocompleteInput::HOMEPAGE, &demotions_by_type); | 184 AutocompleteInput::HOMEPAGE, &demotions_by_type); |
165 ASSERT_EQ(1u, demotions_by_type.size()); | 185 ASSERT_EQ(1u, demotions_by_type.size()); |
166 VerifyDemotion(demotions_by_type, AutocompleteMatchType::NAVSUGGEST, 1.0); | 186 VerifyDemotion(demotions_by_type, AutocompleteMatchType::NAVSUGGEST, 1.0); |
167 OmniboxFieldTrial::GetDemotionsByType( | 187 OmniboxFieldTrial::GetDemotionsByType( |
168 AutocompleteInput::BLANK, &demotions_by_type); | 188 AutocompleteInput::BLANK, &demotions_by_type); |
169 ASSERT_EQ(1u, demotions_by_type.size()); | 189 ASSERT_EQ(1u, demotions_by_type.size()); |
170 VerifyDemotion(demotions_by_type, AutocompleteMatchType::HISTORY_URL, 0.25); | 190 VerifyDemotion(demotions_by_type, AutocompleteMatchType::HISTORY_URL, 0.25); |
171 } | 191 } |
172 | 192 |
173 TEST_F(OmniboxFieldTrialTest, GetValueForRuleInContext) { | 193 TEST_F(OmniboxFieldTrialTest, GetValueForRuleInContext) { |
| 194 // This test starts with Instant Extended off (the default state), then |
| 195 // enables Instant Extended and tests again on the same rules. |
| 196 |
174 // Must be the same as kBundledExperimentFieldTrialName | 197 // Must be the same as kBundledExperimentFieldTrialName |
175 // defined in omnibox_field_trial.cc. | 198 // defined in omnibox_field_trial.cc. |
176 const std::string kTrialName = "OmniboxBundledExperimentV1"; | 199 const std::string kTrialName = "OmniboxBundledExperimentV1"; |
177 { | 200 { |
178 std::map<std::string, std::string> params; | 201 std::map<std::string, std::string> params; |
179 // Rule 1 has some exact matches and a global fallback. | 202 // Rule 1 has some exact matches and fallbacks at every level. |
180 params["rule1:1"] = "rule1-1-value"; // NEW_TAB_PAGE | 203 params["rule1:1:0"] = "rule1-1-0-value"; // NEW_TAB_PAGE |
181 params["rule1:3"] = "rule1-3-value"; // HOMEPAGE | 204 params["rule1:3:0"] = "rule1-3-0-value"; // HOMEPAGE |
182 params["rule1:*"] = "rule1-*-value"; // global | 205 params["rule1:4:1"] = "rule1-4-1-value"; // OTHER |
183 // Rule 2 has no exact matches but has a global fallback. | 206 params["rule1:4:*"] = "rule1-4-*-value"; // OTHER |
184 params["rule2:*"] = "rule2-*-value"; // global | 207 params["rule1:*:1"] = "rule1-*-1-value"; // global |
185 // Rule 3 has an exact match but no global fallback. | 208 params["rule1:*:*"] = "rule1-*-*-value"; // global |
186 params["rule3:4"] = "rule3-4-value"; // OTHER | 209 // Rule 2 has no exact matches but has fallbacks. |
| 210 params["rule2:*:0"] = "rule2-*-0-value"; // global |
| 211 params["rule2:1:*"] = "rule2-1-*-value"; // NEW_TAB_PAGE |
| 212 params["rule2:*:*"] = "rule2-*-*-value"; // global |
| 213 // Rule 3 has only a global fallback. |
| 214 params["rule3:*:*"] = "rule3-*-*-value"; // global |
| 215 // Rule 4 has an exact match but no fallbacks. |
| 216 params["rule4:4:0"] = "rule4-4-0-value"; // OTHER |
187 // Add a malformed rule to make sure it doesn't screw things up. | 217 // Add a malformed rule to make sure it doesn't screw things up. |
188 params["unrecognized"] = "unrecognized-value"; | 218 params["unrecognized"] = "unrecognized-value"; |
189 ASSERT_TRUE(chrome_variations::AssociateVariationParams( | 219 ASSERT_TRUE(chrome_variations::AssociateVariationParams( |
190 kTrialName, "A", params)); | 220 kTrialName, "A", params)); |
191 } | 221 } |
192 | 222 |
193 base::FieldTrialList::CreateFieldTrial(kTrialName, "A"); | 223 base::FieldTrialList::CreateFieldTrial(kTrialName, "A"); |
194 | 224 |
| 225 // Tests with Instant Extended disabled. |
195 // Tests for rule 1. | 226 // Tests for rule 1. |
196 EXPECT_EQ( | 227 ExpectRuleValue("rule1-1-0-value", |
197 "rule1-1-value", | 228 "rule1", AutocompleteInput::NEW_TAB_PAGE); // exact match |
198 OmniboxFieldTrial::GetValueForRuleInContext( | 229 ExpectRuleValue("rule1-1-0-value", |
199 "rule1", AutocompleteInput::NEW_TAB_PAGE)); // exact match | 230 "rule1", AutocompleteInput::NEW_TAB_PAGE); // exact match |
200 EXPECT_EQ( | 231 ExpectRuleValue("rule1-*-*-value", |
201 "rule1-*-value", | 232 "rule1", AutocompleteInput::BLANK); // fallback to global |
202 OmniboxFieldTrial::GetValueForRuleInContext( | 233 ExpectRuleValue("rule1-3-0-value", |
203 "rule1", AutocompleteInput::BLANK)); // fallback to global | 234 "rule1", AutocompleteInput::HOMEPAGE); // exact match |
204 EXPECT_EQ( | 235 ExpectRuleValue("rule1-4-*-value", |
205 "rule1-3-value", | 236 "rule1", AutocompleteInput::OTHER); // partial fallback |
206 OmniboxFieldTrial::GetValueForRuleInContext( | 237 ExpectRuleValue("rule1-*-*-value", |
207 "rule1", AutocompleteInput::HOMEPAGE)); // exact match | 238 "rule1", |
208 EXPECT_EQ( | 239 AutocompleteInput:: // fallback to global |
209 "rule1-*-value", | 240 SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT); |
210 OmniboxFieldTrial::GetValueForRuleInContext( | 241 // Tests for rule 2. |
211 "rule1", AutocompleteInput::OTHER)); // fallback to global | 242 ExpectRuleValue("rule2-*-0-value", |
| 243 "rule2", AutocompleteInput::HOMEPAGE); // partial fallback |
| 244 ExpectRuleValue("rule2-*-0-value", |
| 245 "rule2", AutocompleteInput::OTHER); // partial fallback |
| 246 |
| 247 // Tests for rule 3. |
| 248 ExpectRuleValue("rule3-*-*-value", |
| 249 "rule3", AutocompleteInput::HOMEPAGE); // fallback to global |
| 250 ExpectRuleValue("rule3-*-*-value", |
| 251 "rule3", AutocompleteInput::OTHER); // fallback to global |
| 252 |
| 253 // Tests for rule 4. |
| 254 ExpectRuleValue("", |
| 255 "rule4", AutocompleteInput::BLANK); // no global fallback |
| 256 ExpectRuleValue("", |
| 257 "rule4", AutocompleteInput::HOMEPAGE); // no global fallback |
| 258 ExpectRuleValue("rule4-4-0-value", |
| 259 "rule4", AutocompleteInput::OTHER); // exact match |
| 260 |
| 261 // Tests for rule 5 (a missing rule). |
| 262 ExpectRuleValue("", |
| 263 "rule5", AutocompleteInput::OTHER); // no rule at all |
| 264 |
| 265 // Now change the Instant Extended state and run analogous tests. |
| 266 // Instant Extended only works on non-mobile platforms. |
| 267 #if !defined(OS_IOS) && !defined(OS_ANDROID) |
| 268 chrome::ResetInstantExtendedOptInStateGateForTest(); |
| 269 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 270 switches::kEnableInstantExtendedAPI); |
| 271 |
| 272 // Tests with Instant Extended enabled. |
| 273 // Tests for rule 1. |
| 274 ExpectRuleValue("rule1-4-1-value", |
| 275 "rule1", AutocompleteInput::OTHER); // exact match |
| 276 ExpectRuleValue("rule1-*-1-value", |
| 277 "rule1", AutocompleteInput::BLANK); // partial fallback |
| 278 ExpectRuleValue("rule1-*-1-value", |
| 279 "rule1", |
| 280 AutocompleteInput::NEW_TAB_PAGE); // partial fallback |
212 | 281 |
213 // Tests for rule 2. | 282 // Tests for rule 2. |
214 EXPECT_EQ( | 283 ExpectRuleValue("rule2-1-*-value", |
215 "rule2-*-value", | 284 "rule2", |
216 OmniboxFieldTrial::GetValueForRuleInContext( | 285 AutocompleteInput::NEW_TAB_PAGE); // partial fallback |
217 "rule2", AutocompleteInput::HOMEPAGE)); // fallback to global | 286 ExpectRuleValue("rule2-*-*-value", |
218 EXPECT_EQ( | 287 "rule2", AutocompleteInput::OTHER); // global fallback |
219 "rule2-*-value", | |
220 OmniboxFieldTrial::GetValueForRuleInContext( | |
221 "rule2", AutocompleteInput::OTHER)); // fallback to global | |
222 | 288 |
223 // Tests for rule 3. | 289 // Tests for rule 3. |
224 EXPECT_EQ( | 290 ExpectRuleValue("rule3-*-*-value", |
225 "", | 291 "rule3", AutocompleteInput::HOMEPAGE); // global fallback |
226 OmniboxFieldTrial::GetValueForRuleInContext( | 292 ExpectRuleValue("rule3-*-*-value", |
227 "rule3", AutocompleteInput::BLANK)); // no global fallback | 293 "rule3", AutocompleteInput::OTHER); // global fallback |
228 EXPECT_EQ( | |
229 "", | |
230 OmniboxFieldTrial::GetValueForRuleInContext( | |
231 "rule3", AutocompleteInput::HOMEPAGE)); // no global fallback | |
232 EXPECT_EQ( | |
233 "rule3-4-value", | |
234 OmniboxFieldTrial::GetValueForRuleInContext( | |
235 "rule3", AutocompleteInput::OTHER)); // exact match | |
236 | 294 |
237 // Tests for rule 4 (a missing rule). | 295 // Tests for rule 4. |
238 EXPECT_EQ( | 296 ExpectRuleValue("", |
239 "", | 297 "rule4", AutocompleteInput::BLANK); // no global fallback |
240 OmniboxFieldTrial::GetValueForRuleInContext( | 298 ExpectRuleValue("", |
241 "rule4", AutocompleteInput::OTHER)); // no rule at all | 299 "rule4", AutocompleteInput::HOMEPAGE); // no global fallback |
| 300 |
| 301 // Tests for rule 5 (a missing rule). |
| 302 ExpectRuleValue("", |
| 303 "rule5", AutocompleteInput::OTHER); // no rule at all |
| 304 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) |
242 } | 305 } |
OLD | NEW |