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