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" | |
8 #include "base/metrics/field_trial.h" | |
7 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
8 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/autocomplete/autocomplete_input.h" | 12 #include "chrome/browser/autocomplete/autocomplete_input.h" |
11 #include "chrome/browser/autocomplete/autocomplete_match.h" | 13 #include "chrome/browser/autocomplete/autocomplete_match.h" |
12 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 14 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
15 #include "chrome/browser/omnibox/omnibox_field_trial.h" | |
13 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
14 #include "chrome/browser/search_engines/template_url_service.h" | 17 #include "chrome/browser/search_engines/template_url_service.h" |
15 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 18 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
19 #include "chrome/common/metrics/entropy_provider.h" | |
20 #include "chrome/common/metrics/variations/variations_util.h" | |
16 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
18 | 23 |
19 class AutocompleteResultTest : public testing::Test { | 24 class AutocompleteResultTest : public testing::Test { |
20 public: | 25 public: |
21 struct TestData { | 26 struct TestData { |
22 // Used to build a url for the AutocompleteMatch. The URL becomes | 27 // 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"). | 28 // "http://" + ('a' + |url_id|) (e.g. an ID of 2 yields "http://b"). |
24 int url_id; | 29 int url_id; |
25 | 30 |
26 // ID of the provider. | 31 // ID of the provider. |
27 int provider_id; | 32 int provider_id; |
28 | 33 |
29 // Relevance score. | 34 // Relevance score. |
30 int relevance; | 35 int relevance; |
31 }; | 36 }; |
32 | 37 |
33 AutocompleteResultTest() {} | 38 AutocompleteResultTest() { |
39 // Destroy the existing FieldTrialList before creating a new one to avoid | |
40 // a DCHECK. | |
41 field_trial_list_.reset(); | |
H Fung
2013/08/08 20:10:36
not needed?
Mark P
2013/08/08 22:06:10
Yes, needed. See comment directly above the line.
| |
42 field_trial_list_.reset(new base::FieldTrialList( | |
43 new metrics::SHA1EntropyProvider("foo"))); | |
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'); |
82 match->destination_url = GURL("http://" + url_id); | 95 match->destination_url = GURL("http://" + url_id); |
83 match->relevance = data.relevance; | 96 match->relevance = data.relevance; |
97 match->allowed_to_be_default_match = true; | |
84 } | 98 } |
85 | 99 |
86 // static | 100 // static |
87 void AutocompleteResultTest::PopulateAutocompleteMatches( | 101 void AutocompleteResultTest::PopulateAutocompleteMatches( |
88 const TestData* data, | 102 const TestData* data, |
89 size_t count, | 103 size_t count, |
90 ACMatches* matches) { | 104 ACMatches* matches) { |
91 for (size_t i = 0; i < count; ++i) { | 105 for (size_t i = 0; i < count; ++i) { |
92 AutocompleteMatch match; | 106 AutocompleteMatch match; |
93 PopulateAutocompleteMatch(data[i], &match); | 107 PopulateAutocompleteMatch(data[i], &match); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 AutocompleteResult r2; | 155 AutocompleteResult r2; |
142 | 156 |
143 // Swap with empty shouldn't do anything interesting. | 157 // Swap with empty shouldn't do anything interesting. |
144 r1.Swap(&r2); | 158 r1.Swap(&r2); |
145 EXPECT_EQ(r1.end(), r1.default_match()); | 159 EXPECT_EQ(r1.end(), r1.default_match()); |
146 EXPECT_EQ(r2.end(), r2.default_match()); | 160 EXPECT_EQ(r2.end(), r2.default_match()); |
147 | 161 |
148 // Swap with a single match. | 162 // Swap with a single match. |
149 ACMatches matches; | 163 ACMatches matches; |
150 AutocompleteMatch match; | 164 AutocompleteMatch match; |
165 match.allowed_to_be_default_match = true; | |
151 AutocompleteInput input(ASCIIToUTF16("a"), string16::npos, string16(), GURL(), | 166 AutocompleteInput input(ASCIIToUTF16("a"), string16::npos, string16(), GURL(), |
152 AutocompleteInput::INVALID_SPEC, false, false, false, | 167 AutocompleteInput::INVALID_SPEC, false, false, false, |
153 AutocompleteInput::ALL_MATCHES); | 168 AutocompleteInput::ALL_MATCHES); |
154 matches.push_back(match); | 169 matches.push_back(match); |
155 r1.AppendMatches(matches); | 170 r1.AppendMatches(matches); |
156 r1.SortAndCull(input, test_util_.profile()); | 171 r1.SortAndCull(input, test_util_.profile()); |
157 EXPECT_EQ(r1.begin(), r1.default_match()); | 172 EXPECT_EQ(r1.begin(), r1.default_match()); |
158 EXPECT_EQ("http://a/", r1.alternate_nav_url().spec()); | 173 EXPECT_EQ("http://a/", r1.alternate_nav_url().spec()); |
159 r1.Swap(&r2); | 174 r1.Swap(&r2); |
160 EXPECT_TRUE(r1.empty()); | 175 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", | 300 EXPECT_EQ("http://www.foo.com/s?q=foo", |
286 result.match_at(0)->destination_url.spec()); | 301 result.match_at(0)->destination_url.spec()); |
287 EXPECT_EQ(1300, result.match_at(0)->relevance); | 302 EXPECT_EQ(1300, result.match_at(0)->relevance); |
288 EXPECT_EQ("http://www.foo.com/s?q=foo2", | 303 EXPECT_EQ("http://www.foo.com/s?q=foo2", |
289 result.match_at(1)->destination_url.spec()); | 304 result.match_at(1)->destination_url.spec()); |
290 EXPECT_EQ(1200, result.match_at(1)->relevance); | 305 EXPECT_EQ(1200, result.match_at(1)->relevance); |
291 EXPECT_EQ("http://www.foo.com/", | 306 EXPECT_EQ("http://www.foo.com/", |
292 result.match_at(2)->destination_url.spec()); | 307 result.match_at(2)->destination_url.spec()); |
293 EXPECT_EQ(900, result.match_at(2)->relevance); | 308 EXPECT_EQ(900, result.match_at(2)->relevance); |
294 } | 309 } |
310 | |
311 TEST_F(AutocompleteResultTest, SortAndCullReorderForDefaultMatch) { | |
312 TestData data[] = { | |
313 { 0, 0, 1300 }, | |
314 { 1, 0, 1200 }, | |
315 { 2, 0, 1100 }, | |
316 { 3, 0, 1000 } | |
317 }; | |
318 | |
319 std::map<std::string, std::string> params; | |
320 // Enable reorder for omnibox inputs on the user's homepage. | |
321 params[std::string(OmniboxFieldTrial::kReorderForLegalDefaultMatchRule) + | |
322 ":3"] = OmniboxFieldTrial::kReorderForLegalDefaultMatchRuleEnabled; | |
323 ASSERT_TRUE(chrome_variations::AssociateVariationParams( | |
324 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params)); | |
325 base::FieldTrialList::CreateFieldTrial( | |
326 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A"); | |
327 | |
328 { | |
329 // Check that reorder doesn't do anything if the top result | |
330 // is already a legal default match (which is the default from | |
331 // PopulateAutocompleteMatches()). | |
332 ACMatches matches; | |
333 PopulateAutocompleteMatches(data, arraysize(data), &matches); | |
334 AutocompleteResult result; | |
335 result.AppendMatches(matches); | |
336 AutocompleteInput input(string16(), string16::npos, string16(), GURL(), | |
337 AutocompleteInput::HOMEPAGE, false, false, false, | |
338 AutocompleteInput::ALL_MATCHES); | |
339 result.SortAndCull(input, test_util_.profile()); | |
340 AssertResultMatches(result, data, 4); | |
341 } | |
342 | |
343 { | |
344 // Check that reorder swaps up a result appropriately. | |
345 ACMatches matches; | |
346 PopulateAutocompleteMatches(data, arraysize(data), &matches); | |
347 matches[0].allowed_to_be_default_match = false; | |
348 matches[1].allowed_to_be_default_match = false; | |
349 AutocompleteResult result; | |
350 result.AppendMatches(matches); | |
351 AutocompleteInput input(string16(), string16::npos, string16(), GURL(), | |
352 AutocompleteInput::HOMEPAGE, false, false, false, | |
353 AutocompleteInput::ALL_MATCHES); | |
354 result.SortAndCull(input, test_util_.profile()); | |
355 ASSERT_EQ(4U, result.size()); | |
356 EXPECT_EQ("http://c/", result.match_at(0)->destination_url.spec()); | |
357 EXPECT_EQ("http://a/", result.match_at(1)->destination_url.spec()); | |
358 EXPECT_EQ("http://b/", result.match_at(2)->destination_url.spec()); | |
359 EXPECT_EQ("http://d/", result.match_at(3)->destination_url.spec()); | |
360 } | |
361 } | |
OLD | NEW |