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" |
13 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 15 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
14 #include "chrome/browser/search_engines/template_url_service.h" | 16 #include "chrome/browser/search_engines/template_url_service.h" |
15 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 17 #include "chrome/browser/search_engines/template_url_service_test_util.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 } | |
34 | 44 |
35 virtual void SetUp() OVERRIDE { | 45 virtual void SetUp() OVERRIDE { |
36 #if defined(OS_ANDROID) | 46 #if defined(OS_ANDROID) |
37 TemplateURLPrepopulateData::InitCountryCode( | 47 TemplateURLPrepopulateData::InitCountryCode( |
38 std::string() /* unknown country code */); | 48 std::string() /* unknown country code */); |
39 #endif | 49 #endif |
40 test_util_.SetUp(); | 50 test_util_.SetUp(); |
41 test_util_.VerifyLoad(); | 51 test_util_.VerifyLoad(); |
42 } | 52 } |
43 | 53 |
(...skipping 18 matching lines...) Expand all Loading... | |
62 // Creates an AutocompleteResult from |last| and |current|. The two are | 72 // Creates an AutocompleteResult from |last| and |current|. The two are |
63 // merged by |CopyOldMatches| and compared by |AssertResultMatches|. | 73 // merged by |CopyOldMatches| and compared by |AssertResultMatches|. |
64 void RunCopyOldMatchesTest(const TestData* last, size_t last_size, | 74 void RunCopyOldMatchesTest(const TestData* last, size_t last_size, |
65 const TestData* current, size_t current_size, | 75 const TestData* current, size_t current_size, |
66 const TestData* expected, size_t expected_size); | 76 const TestData* expected, size_t expected_size); |
67 | 77 |
68 protected: | 78 protected: |
69 TemplateURLServiceTestUtil test_util_; | 79 TemplateURLServiceTestUtil test_util_; |
70 | 80 |
71 private: | 81 private: |
82 scoped_ptr<base::FieldTrialList> field_trial_list_; | |
83 | |
72 DISALLOW_COPY_AND_ASSIGN(AutocompleteResultTest); | 84 DISALLOW_COPY_AND_ASSIGN(AutocompleteResultTest); |
73 }; | 85 }; |
74 | 86 |
75 // static | 87 // static |
76 void AutocompleteResultTest::PopulateAutocompleteMatch( | 88 void AutocompleteResultTest::PopulateAutocompleteMatch( |
77 const TestData& data, | 89 const TestData& data, |
78 AutocompleteMatch* match) { | 90 AutocompleteMatch* match) { |
79 match->provider = reinterpret_cast<AutocompleteProvider*>(data.provider_id); | 91 match->provider = reinterpret_cast<AutocompleteProvider*>(data.provider_id); |
80 match->fill_into_edit = base::IntToString16(data.url_id); | 92 match->fill_into_edit = base::IntToString16(data.url_id); |
81 std::string url_id(1, data.url_id + 'a'); | 93 std::string url_id(1, data.url_id + 'a'); |
82 match->destination_url = GURL("http://" + url_id); | 94 match->destination_url = GURL("http://" + url_id); |
83 match->relevance = data.relevance; | 95 match->relevance = data.relevance; |
96 match->allowed_to_be_default_match = true; | |
84 } | 97 } |
85 | 98 |
86 // static | 99 // static |
87 void AutocompleteResultTest::PopulateAutocompleteMatches( | 100 void AutocompleteResultTest::PopulateAutocompleteMatches( |
88 const TestData* data, | 101 const TestData* data, |
89 size_t count, | 102 size_t count, |
90 ACMatches* matches) { | 103 ACMatches* matches) { |
91 for (size_t i = 0; i < count; ++i) { | 104 for (size_t i = 0; i < count; ++i) { |
92 AutocompleteMatch match; | 105 AutocompleteMatch match; |
93 PopulateAutocompleteMatch(data[i], &match); | 106 PopulateAutocompleteMatch(data[i], &match); |
(...skipping 47 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.allowed_to_be_default_match = true; | |
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, SortAndCullReorderForDefaultMatch) { | |
311 TestData data[] = { | |
312 { 0, 0, 1300 }, | |
313 { 1, 0, 1200 }, | |
314 { 2, 0, 1100 }, | |
315 { 3, 0, 1000 } | |
316 }; | |
317 | |
318 // Must be the same as kBundledExperimentFieldTrialName | |
msw
2013/08/07 20:09:21
Is this duplication of constants common/necessary?
Mark P
2013/08/07 22:13:05
Alexei asked a similar question on a different cod
Peter Kasting
2013/08/07 22:20:01
FWIW, I also asked about this kind of thing.
I do
| |
319 // defined in omnibox_field_trial.cc. | |
320 const std::string kTrialName = "OmniboxBundledExperimentV1"; | |
321 // Must be the same as kReorderForLegalDefaultMatchRule defined in | |
322 // omnibox_field_trial.cc. | |
323 const std::string kRuleName = "ReorderForLegalDefaultMatch"; | |
324 // Must be the same as the constant used in | |
325 // OmniboxFieldTrial::ReorderForLegalDefaultMatch(). | |
326 const std::string kReorderEnabled = "ReorderForLegalDefaultMatch"; | |
327 std::map<std::string, std::string> params; | |
328 // Enable reorder for omnibox inputs on the user's homepage. | |
329 params[kRuleName + ":3"] = kReorderEnabled; | |
330 ASSERT_TRUE(chrome_variations::AssociateVariationParams( | |
331 kTrialName, "A", params)); | |
332 base::FieldTrialList::CreateFieldTrial(kTrialName, "A"); | |
333 | |
334 { | |
335 // Check that reorder doesn't do anything if the top result | |
336 // is already a legal default match (which is the default from | |
337 // PopulateAutocompleteMatches()). | |
338 ACMatches matches; | |
339 PopulateAutocompleteMatches(data, arraysize(data), &matches); | |
340 AutocompleteResult result; | |
341 result.AppendMatches(matches); | |
342 AutocompleteInput input(string16(), string16::npos, string16(), GURL(), | |
343 AutocompleteInput::HOMEPAGE, false, false, false, | |
344 AutocompleteInput::ALL_MATCHES); | |
345 result.SortAndCull(input, test_util_.profile()); | |
346 AssertResultMatches(result, data, 4); | |
347 } | |
348 | |
349 { | |
350 // Check that reorder swaps up a result appropriately. | |
351 ACMatches matches; | |
352 PopulateAutocompleteMatches(data, arraysize(data), &matches); | |
353 matches[0].allowed_to_be_default_match = false; | |
354 matches[1].allowed_to_be_default_match = false; | |
355 AutocompleteResult result; | |
356 result.AppendMatches(matches); | |
357 AutocompleteInput input(string16(), string16::npos, string16(), GURL(), | |
358 AutocompleteInput::HOMEPAGE, false, false, false, | |
359 AutocompleteInput::ALL_MATCHES); | |
360 result.SortAndCull(input, test_util_.profile()); | |
361 ASSERT_EQ(4U, result.size()); | |
362 EXPECT_EQ("http://c/", result.match_at(0)->destination_url.spec()); | |
363 EXPECT_EQ("http://a/", result.match_at(1)->destination_url.spec()); | |
364 EXPECT_EQ("http://b/", result.match_at(2)->destination_url.spec()); | |
365 EXPECT_EQ("http://d/", result.match_at(3)->destination_url.spec()); | |
366 } | |
367 } | |
OLD | NEW |