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/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
8 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/autocomplete/autocomplete_input.h" | 11 #include "chrome/browser/autocomplete/autocomplete_input.h" |
11 #include "chrome/browser/autocomplete/autocomplete_match.h" | 12 #include "chrome/browser/autocomplete/autocomplete_match.h" |
12 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 13 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
13 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 14 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
14 #include "chrome/browser/search_engines/template_url_service.h" | 15 #include "chrome/browser/search_engines/template_url_service.h" |
15 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 16 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
| 17 #include "chrome/common/autocomplete_match_type.h" |
| 18 #include "chrome/common/metrics/entropy_provider.h" |
| 19 #include "chrome/common/metrics/variations/variations_util.h" |
16 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
18 | 22 |
19 class AutocompleteResultTest : public testing::Test { | 23 class AutocompleteResultTest : public testing::Test { |
20 public: | 24 public: |
21 struct TestData { | 25 struct TestData { |
22 // Used to build a url for the AutocompleteMatch. The URL becomes | 26 // Used to build a url for the AutocompleteMatch. The URL becomes |
23 // "http://" + ('a' + |url_id|) (e.g. an ID of 2 yields "http://b"). | 27 // "http://" + ('a' + |url_id|) (e.g. an ID of 2 yields "http://b"). |
24 int url_id; | 28 int url_id; |
25 | 29 |
26 // ID of the provider. | 30 // ID of the provider. |
27 int provider_id; | 31 int provider_id; |
28 | 32 |
29 // Relevance score. | 33 // Relevance score. |
30 int relevance; | 34 int relevance; |
31 }; | 35 }; |
32 | 36 |
33 AutocompleteResultTest() {} | 37 AutocompleteResultTest() { |
| 38 // Destroy the existing FieldTrialList before creating a new one to avoid |
| 39 // a DCHECK. |
| 40 field_trial_list_.reset(); |
| 41 field_trial_list_.reset(new base::FieldTrialList( |
| 42 new metrics::SHA1EntropyProvider("foo"))); |
| 43 chrome_variations::testing::ClearAllVariationParams(); |
| 44 } |
34 | 45 |
35 virtual void SetUp() OVERRIDE { | 46 virtual void SetUp() OVERRIDE { |
36 #if defined(OS_ANDROID) | 47 #if defined(OS_ANDROID) |
37 TemplateURLPrepopulateData::InitCountryCode( | 48 TemplateURLPrepopulateData::InitCountryCode( |
38 std::string() /* unknown country code */); | 49 std::string() /* unknown country code */); |
39 #endif | 50 #endif |
40 test_util_.SetUp(); | 51 test_util_.SetUp(); |
41 test_util_.VerifyLoad(); | 52 test_util_.VerifyLoad(); |
42 } | 53 } |
43 | 54 |
(...skipping 18 matching lines...) Expand all Loading... |
62 // Creates an AutocompleteResult from |last| and |current|. The two are | 73 // Creates an AutocompleteResult from |last| and |current|. The two are |
63 // merged by |CopyOldMatches| and compared by |AssertResultMatches|. | 74 // merged by |CopyOldMatches| and compared by |AssertResultMatches|. |
64 void RunCopyOldMatchesTest(const TestData* last, size_t last_size, | 75 void RunCopyOldMatchesTest(const TestData* last, size_t last_size, |
65 const TestData* current, size_t current_size, | 76 const TestData* current, size_t current_size, |
66 const TestData* expected, size_t expected_size); | 77 const TestData* expected, size_t expected_size); |
67 | 78 |
68 protected: | 79 protected: |
69 TemplateURLServiceTestUtil test_util_; | 80 TemplateURLServiceTestUtil test_util_; |
70 | 81 |
71 private: | 82 private: |
| 83 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 84 |
72 DISALLOW_COPY_AND_ASSIGN(AutocompleteResultTest); | 85 DISALLOW_COPY_AND_ASSIGN(AutocompleteResultTest); |
73 }; | 86 }; |
74 | 87 |
75 // static | 88 // static |
76 void AutocompleteResultTest::PopulateAutocompleteMatch( | 89 void AutocompleteResultTest::PopulateAutocompleteMatch( |
77 const TestData& data, | 90 const TestData& data, |
78 AutocompleteMatch* match) { | 91 AutocompleteMatch* match) { |
79 match->provider = reinterpret_cast<AutocompleteProvider*>(data.provider_id); | 92 match->provider = reinterpret_cast<AutocompleteProvider*>(data.provider_id); |
80 match->fill_into_edit = base::IntToString16(data.url_id); | 93 match->fill_into_edit = base::IntToString16(data.url_id); |
81 std::string url_id(1, data.url_id + 'a'); | 94 std::string url_id(1, data.url_id + 'a'); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 AutocompleteResult r2; | 154 AutocompleteResult r2; |
142 | 155 |
143 // Swap with empty shouldn't do anything interesting. | 156 // Swap with empty shouldn't do anything interesting. |
144 r1.Swap(&r2); | 157 r1.Swap(&r2); |
145 EXPECT_EQ(r1.end(), r1.default_match()); | 158 EXPECT_EQ(r1.end(), r1.default_match()); |
146 EXPECT_EQ(r2.end(), r2.default_match()); | 159 EXPECT_EQ(r2.end(), r2.default_match()); |
147 | 160 |
148 // Swap with a single match. | 161 // Swap with a single match. |
149 ACMatches matches; | 162 ACMatches matches; |
150 AutocompleteMatch match; | 163 AutocompleteMatch match; |
| 164 match.relevance = 1; |
151 AutocompleteInput input(ASCIIToUTF16("a"), string16::npos, string16(), GURL(), | 165 AutocompleteInput input(ASCIIToUTF16("a"), string16::npos, string16(), GURL(), |
152 AutocompleteInput::INVALID_SPEC, false, false, false, | 166 AutocompleteInput::INVALID_SPEC, false, false, false, |
153 AutocompleteInput::ALL_MATCHES); | 167 AutocompleteInput::ALL_MATCHES); |
154 matches.push_back(match); | 168 matches.push_back(match); |
155 r1.AppendMatches(matches); | 169 r1.AppendMatches(matches); |
156 r1.SortAndCull(input, test_util_.profile()); | 170 r1.SortAndCull(input, test_util_.profile()); |
157 EXPECT_EQ(r1.begin(), r1.default_match()); | 171 EXPECT_EQ(r1.begin(), r1.default_match()); |
158 EXPECT_EQ("http://a/", r1.alternate_nav_url().spec()); | 172 EXPECT_EQ("http://a/", r1.alternate_nav_url().spec()); |
159 r1.Swap(&r2); | 173 r1.Swap(&r2); |
160 EXPECT_TRUE(r1.empty()); | 174 EXPECT_TRUE(r1.empty()); |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 EXPECT_EQ("http://www.foo.com/s?q=foo", | 299 EXPECT_EQ("http://www.foo.com/s?q=foo", |
286 result.match_at(0)->destination_url.spec()); | 300 result.match_at(0)->destination_url.spec()); |
287 EXPECT_EQ(1300, result.match_at(0)->relevance); | 301 EXPECT_EQ(1300, result.match_at(0)->relevance); |
288 EXPECT_EQ("http://www.foo.com/s?q=foo2", | 302 EXPECT_EQ("http://www.foo.com/s?q=foo2", |
289 result.match_at(1)->destination_url.spec()); | 303 result.match_at(1)->destination_url.spec()); |
290 EXPECT_EQ(1200, result.match_at(1)->relevance); | 304 EXPECT_EQ(1200, result.match_at(1)->relevance); |
291 EXPECT_EQ("http://www.foo.com/", | 305 EXPECT_EQ("http://www.foo.com/", |
292 result.match_at(2)->destination_url.spec()); | 306 result.match_at(2)->destination_url.spec()); |
293 EXPECT_EQ(900, result.match_at(2)->relevance); | 307 EXPECT_EQ(900, result.match_at(2)->relevance); |
294 } | 308 } |
| 309 |
| 310 TEST_F(AutocompleteResultTest, SortAndCullWithDemotionsByType) { |
| 311 // Add some matches. |
| 312 ACMatches matches; |
| 313 { |
| 314 AutocompleteMatch match; |
| 315 match.destination_url = GURL("http://history-url/"); |
| 316 match.relevance = 1400; |
| 317 match.type = AutocompleteMatchType::HISTORY_URL; |
| 318 matches.push_back(match); |
| 319 } |
| 320 { |
| 321 AutocompleteMatch match; |
| 322 match.destination_url = GURL("http://search-what-you-typed/"); |
| 323 match.relevance = 1300; |
| 324 match.type = AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED; |
| 325 matches.push_back(match); |
| 326 } |
| 327 { |
| 328 AutocompleteMatch match; |
| 329 match.destination_url = GURL("http://history-title/"); |
| 330 match.relevance = 1200; |
| 331 match.type = AutocompleteMatchType::HISTORY_TITLE; |
| 332 matches.push_back(match); |
| 333 } |
| 334 { |
| 335 AutocompleteMatch match; |
| 336 match.destination_url = GURL("http://search-history/"); |
| 337 match.relevance = 500; |
| 338 match.type = AutocompleteMatchType::SEARCH_HISTORY; |
| 339 matches.push_back(match); |
| 340 } |
| 341 |
| 342 // 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 { |
| 350 std::map<std::string, std::string> params; |
| 351 params[kRuleName + ":3"] = "1:50,7:100,2:0"; // 3 == HOMEPAGE |
| 352 ASSERT_TRUE(chrome_variations::AssociateVariationParams( |
| 353 kTrialName, "A", params)); |
| 354 } |
| 355 base::FieldTrialList::CreateFieldTrial(kTrialName, "A"); |
| 356 |
| 357 AutocompleteResult result; |
| 358 result.AppendMatches(matches); |
| 359 AutocompleteInput input(string16(), string16::npos, string16(), GURL(), |
| 360 AutocompleteInput::HOMEPAGE, false, false, false, |
| 361 AutocompleteInput::ALL_MATCHES); |
| 362 result.SortAndCull(input, test_util_.profile()); |
| 363 |
| 364 // Check the new ordering. The history-title results should be omitted. |
| 365 // We cannot check relevance scores because the matches are sorted by |
| 366 // demoted relevance but the actual relevance scores are not modified. |
| 367 ASSERT_EQ(3u, result.size()); |
| 368 EXPECT_EQ("http://search-what-you-typed/", |
| 369 result.match_at(0)->destination_url.spec()); |
| 370 EXPECT_EQ("http://history-url/", |
| 371 result.match_at(1)->destination_url.spec()); |
| 372 EXPECT_EQ("http://search-history/", |
| 373 result.match_at(2)->destination_url.spec()); |
| 374 } |
OLD | NEW |