OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/autocomplete/autocomplete_result.h" | 5 #include "chrome/browser/autocomplete/autocomplete_result.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/metrics/field_trial.h" |
8 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/autocomplete/autocomplete_input.h" | 12 #include "chrome/browser/autocomplete/autocomplete_input.h" |
12 #include "chrome/browser/autocomplete/autocomplete_match.h" | 13 #include "chrome/browser/autocomplete/autocomplete_match.h" |
13 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 14 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
| 15 #include "chrome/browser/omnibox/omnibox_field_trial.h" |
14 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
15 #include "chrome/browser/search_engines/template_url_service.h" | 17 #include "chrome/browser/search_engines/template_url_service.h" |
16 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 18 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
17 #include "chrome/common/autocomplete_match_type.h" | 19 #include "chrome/common/autocomplete_match_type.h" |
18 #include "chrome/common/metrics/entropy_provider.h" | 20 #include "chrome/common/metrics/entropy_provider.h" |
19 #include "chrome/common/metrics/variations/variations_util.h" | 21 #include "chrome/common/metrics/variations/variations_util.h" |
20 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
22 | 24 |
23 class AutocompleteResultTest : public testing::Test { | 25 class AutocompleteResultTest : public testing::Test { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 89 |
88 // static | 90 // static |
89 void AutocompleteResultTest::PopulateAutocompleteMatch( | 91 void AutocompleteResultTest::PopulateAutocompleteMatch( |
90 const TestData& data, | 92 const TestData& data, |
91 AutocompleteMatch* match) { | 93 AutocompleteMatch* match) { |
92 match->provider = reinterpret_cast<AutocompleteProvider*>(data.provider_id); | 94 match->provider = reinterpret_cast<AutocompleteProvider*>(data.provider_id); |
93 match->fill_into_edit = base::IntToString16(data.url_id); | 95 match->fill_into_edit = base::IntToString16(data.url_id); |
94 std::string url_id(1, data.url_id + 'a'); | 96 std::string url_id(1, data.url_id + 'a'); |
95 match->destination_url = GURL("http://" + url_id); | 97 match->destination_url = GURL("http://" + url_id); |
96 match->relevance = data.relevance; | 98 match->relevance = data.relevance; |
| 99 match->allowed_to_be_default_match = true; |
97 } | 100 } |
98 | 101 |
99 // static | 102 // static |
100 void AutocompleteResultTest::PopulateAutocompleteMatches( | 103 void AutocompleteResultTest::PopulateAutocompleteMatches( |
101 const TestData* data, | 104 const TestData* data, |
102 size_t count, | 105 size_t count, |
103 ACMatches* matches) { | 106 ACMatches* matches) { |
104 for (size_t i = 0; i < count; ++i) { | 107 for (size_t i = 0; i < count; ++i) { |
105 AutocompleteMatch match; | 108 AutocompleteMatch match; |
106 PopulateAutocompleteMatch(data[i], &match); | 109 PopulateAutocompleteMatch(data[i], &match); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 | 158 |
156 // Swap with empty shouldn't do anything interesting. | 159 // Swap with empty shouldn't do anything interesting. |
157 r1.Swap(&r2); | 160 r1.Swap(&r2); |
158 EXPECT_EQ(r1.end(), r1.default_match()); | 161 EXPECT_EQ(r1.end(), r1.default_match()); |
159 EXPECT_EQ(r2.end(), r2.default_match()); | 162 EXPECT_EQ(r2.end(), r2.default_match()); |
160 | 163 |
161 // Swap with a single match. | 164 // Swap with a single match. |
162 ACMatches matches; | 165 ACMatches matches; |
163 AutocompleteMatch match; | 166 AutocompleteMatch match; |
164 match.relevance = 1; | 167 match.relevance = 1; |
| 168 match.allowed_to_be_default_match = true; |
165 AutocompleteInput input(ASCIIToUTF16("a"), string16::npos, string16(), GURL(), | 169 AutocompleteInput input(ASCIIToUTF16("a"), string16::npos, string16(), GURL(), |
166 AutocompleteInput::INVALID_SPEC, false, false, false, | 170 AutocompleteInput::INVALID_SPEC, false, false, false, |
167 AutocompleteInput::ALL_MATCHES); | 171 AutocompleteInput::ALL_MATCHES); |
168 matches.push_back(match); | 172 matches.push_back(match); |
169 r1.AppendMatches(matches); | 173 r1.AppendMatches(matches); |
170 r1.SortAndCull(input, test_util_.profile()); | 174 r1.SortAndCull(input, test_util_.profile()); |
171 EXPECT_EQ(r1.begin(), r1.default_match()); | 175 EXPECT_EQ(r1.begin(), r1.default_match()); |
172 EXPECT_EQ("http://a/", r1.alternate_nav_url().spec()); | 176 EXPECT_EQ("http://a/", r1.alternate_nav_url().spec()); |
173 r1.Swap(&r2); | 177 r1.Swap(&r2); |
174 EXPECT_TRUE(r1.empty()); | 178 EXPECT_TRUE(r1.empty()); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 EXPECT_EQ(900, result.match_at(2)->relevance); | 311 EXPECT_EQ(900, result.match_at(2)->relevance); |
308 } | 312 } |
309 | 313 |
310 TEST_F(AutocompleteResultTest, SortAndCullWithDemotionsByType) { | 314 TEST_F(AutocompleteResultTest, SortAndCullWithDemotionsByType) { |
311 // Add some matches. | 315 // Add some matches. |
312 ACMatches matches; | 316 ACMatches matches; |
313 { | 317 { |
314 AutocompleteMatch match; | 318 AutocompleteMatch match; |
315 match.destination_url = GURL("http://history-url/"); | 319 match.destination_url = GURL("http://history-url/"); |
316 match.relevance = 1400; | 320 match.relevance = 1400; |
| 321 match.allowed_to_be_default_match = true; |
317 match.type = AutocompleteMatchType::HISTORY_URL; | 322 match.type = AutocompleteMatchType::HISTORY_URL; |
318 matches.push_back(match); | 323 matches.push_back(match); |
319 } | 324 } |
320 { | 325 { |
321 AutocompleteMatch match; | 326 AutocompleteMatch match; |
322 match.destination_url = GURL("http://search-what-you-typed/"); | 327 match.destination_url = GURL("http://search-what-you-typed/"); |
323 match.relevance = 1300; | 328 match.relevance = 1300; |
| 329 match.allowed_to_be_default_match = true; |
324 match.type = AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED; | 330 match.type = AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED; |
325 matches.push_back(match); | 331 matches.push_back(match); |
326 } | 332 } |
327 { | 333 { |
328 AutocompleteMatch match; | 334 AutocompleteMatch match; |
329 match.destination_url = GURL("http://history-title/"); | 335 match.destination_url = GURL("http://history-title/"); |
330 match.relevance = 1200; | 336 match.relevance = 1200; |
| 337 match.allowed_to_be_default_match = true; |
331 match.type = AutocompleteMatchType::HISTORY_TITLE; | 338 match.type = AutocompleteMatchType::HISTORY_TITLE; |
332 matches.push_back(match); | 339 matches.push_back(match); |
333 } | 340 } |
334 { | 341 { |
335 AutocompleteMatch match; | 342 AutocompleteMatch match; |
336 match.destination_url = GURL("http://search-history/"); | 343 match.destination_url = GURL("http://search-history/"); |
337 match.relevance = 500; | 344 match.relevance = 500; |
| 345 match.allowed_to_be_default_match = true; |
338 match.type = AutocompleteMatchType::SEARCH_HISTORY; | 346 match.type = AutocompleteMatchType::SEARCH_HISTORY; |
339 matches.push_back(match); | 347 matches.push_back(match); |
340 } | 348 } |
341 | 349 |
342 // Add a rule demoting history-url and killing history-title. | 350 // Add a rule demoting history-url and killing history-title. |
343 // Must be the same as kBundledExperimentFieldTrialName | |
344 // defined in omnibox_field_trial.cc. | |
345 const std::string kTrialName = "OmniboxBundledExperimentV1"; | |
346 // Must be the same as kDemoteByTypeRule defined in | |
347 // omnibox_field_trial.cc. | |
348 const std::string kRuleName = "DemoteByType"; | |
349 { | 351 { |
350 std::map<std::string, std::string> params; | 352 std::map<std::string, std::string> params; |
351 params[kRuleName + ":3:*"] = "1:50,7:100,2:0"; // 3 == HOMEPAGE | 353 params[std::string(OmniboxFieldTrial::kDemoteByTypeRule) + ":3:*"] = |
| 354 "1:50,7:100,2:0"; // 3 == HOMEPAGE |
352 ASSERT_TRUE(chrome_variations::AssociateVariationParams( | 355 ASSERT_TRUE(chrome_variations::AssociateVariationParams( |
353 kTrialName, "A", params)); | 356 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params)); |
354 } | 357 } |
355 base::FieldTrialList::CreateFieldTrial(kTrialName, "A"); | 358 base::FieldTrialList::CreateFieldTrial( |
| 359 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A"); |
356 | 360 |
357 AutocompleteResult result; | 361 AutocompleteResult result; |
358 result.AppendMatches(matches); | 362 result.AppendMatches(matches); |
359 AutocompleteInput input(string16(), string16::npos, string16(), GURL(), | 363 AutocompleteInput input(string16(), string16::npos, string16(), GURL(), |
360 AutocompleteInput::HOMEPAGE, false, false, false, | 364 AutocompleteInput::HOMEPAGE, false, false, false, |
361 AutocompleteInput::ALL_MATCHES); | 365 AutocompleteInput::ALL_MATCHES); |
362 result.SortAndCull(input, test_util_.profile()); | 366 result.SortAndCull(input, test_util_.profile()); |
363 | 367 |
364 // Check the new ordering. The history-title results should be omitted. | 368 // Check the new ordering. The history-title results should be omitted. |
365 // We cannot check relevance scores because the matches are sorted by | 369 // We cannot check relevance scores because the matches are sorted by |
366 // demoted relevance but the actual relevance scores are not modified. | 370 // demoted relevance but the actual relevance scores are not modified. |
367 ASSERT_EQ(3u, result.size()); | 371 ASSERT_EQ(3u, result.size()); |
368 EXPECT_EQ("http://search-what-you-typed/", | 372 EXPECT_EQ("http://search-what-you-typed/", |
369 result.match_at(0)->destination_url.spec()); | 373 result.match_at(0)->destination_url.spec()); |
370 EXPECT_EQ("http://history-url/", | 374 EXPECT_EQ("http://history-url/", |
371 result.match_at(1)->destination_url.spec()); | 375 result.match_at(1)->destination_url.spec()); |
372 EXPECT_EQ("http://search-history/", | 376 EXPECT_EQ("http://search-history/", |
373 result.match_at(2)->destination_url.spec()); | 377 result.match_at(2)->destination_url.spec()); |
374 } | 378 } |
| 379 |
| 380 TEST_F(AutocompleteResultTest, SortAndCullReorderForDefaultMatch) { |
| 381 TestData data[] = { |
| 382 { 0, 0, 1300 }, |
| 383 { 1, 0, 1200 }, |
| 384 { 2, 0, 1100 }, |
| 385 { 3, 0, 1000 } |
| 386 }; |
| 387 |
| 388 std::map<std::string, std::string> params; |
| 389 // Enable reorder for omnibox inputs on the user's homepage. |
| 390 params[std::string(OmniboxFieldTrial::kReorderForLegalDefaultMatchRule) + |
| 391 ":3:*"] = OmniboxFieldTrial::kReorderForLegalDefaultMatchRuleEnabled; |
| 392 ASSERT_TRUE(chrome_variations::AssociateVariationParams( |
| 393 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params)); |
| 394 base::FieldTrialList::CreateFieldTrial( |
| 395 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A"); |
| 396 |
| 397 { |
| 398 // Check that reorder doesn't do anything if the top result |
| 399 // is already a legal default match (which is the default from |
| 400 // PopulateAutocompleteMatches()). |
| 401 ACMatches matches; |
| 402 PopulateAutocompleteMatches(data, arraysize(data), &matches); |
| 403 AutocompleteResult result; |
| 404 result.AppendMatches(matches); |
| 405 AutocompleteInput input(string16(), string16::npos, string16(), GURL(), |
| 406 AutocompleteInput::HOMEPAGE, false, false, false, |
| 407 AutocompleteInput::ALL_MATCHES); |
| 408 result.SortAndCull(input, test_util_.profile()); |
| 409 AssertResultMatches(result, data, 4); |
| 410 } |
| 411 |
| 412 { |
| 413 // Check that reorder swaps up a result appropriately. |
| 414 ACMatches matches; |
| 415 PopulateAutocompleteMatches(data, arraysize(data), &matches); |
| 416 matches[0].allowed_to_be_default_match = false; |
| 417 matches[1].allowed_to_be_default_match = false; |
| 418 AutocompleteResult result; |
| 419 result.AppendMatches(matches); |
| 420 AutocompleteInput input(string16(), string16::npos, string16(), GURL(), |
| 421 AutocompleteInput::HOMEPAGE, false, false, false, |
| 422 AutocompleteInput::ALL_MATCHES); |
| 423 result.SortAndCull(input, test_util_.profile()); |
| 424 ASSERT_EQ(4U, result.size()); |
| 425 EXPECT_EQ("http://c/", result.match_at(0)->destination_url.spec()); |
| 426 EXPECT_EQ("http://a/", result.match_at(1)->destination_url.spec()); |
| 427 EXPECT_EQ("http://b/", result.match_at(2)->destination_url.spec()); |
| 428 EXPECT_EQ("http://d/", result.match_at(3)->destination_url.spec()); |
| 429 } |
| 430 } |
OLD | NEW |