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/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/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/test/base/testing_profile.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 class AutocompleteResultTest : public testing::Test { | 16 class AutocompleteResultTest : public testing::Test { |
16 public: | 17 public: |
17 struct TestData { | 18 struct TestData { |
18 // Used to build a url for the AutocompleteMatch. The URL becomes | 19 // Used to build a url for the AutocompleteMatch. The URL becomes |
19 // "http://" + ('a' + |url_id|) (e.g. an ID of 2 yields "http://b"). | 20 // "http://" + ('a' + |url_id|) (e.g. an ID of 2 yields "http://b"). |
20 int url_id; | 21 int url_id; |
21 | 22 |
22 // ID of the provider. | 23 // ID of the provider. |
23 int provider_id; | 24 int provider_id; |
24 | 25 |
25 // Relevance score. | 26 // Relevance score. |
26 int relevance; | 27 int relevance; |
27 }; | 28 }; |
28 | 29 |
29 AutocompleteResultTest() {} | 30 AutocompleteResultTest() : profile_(new TestingProfile()) {} |
30 | 31 |
31 // Configures |match| from |data|. | 32 // Configures |match| from |data|. |
32 static void PopulateAutocompleteMatch(const TestData& data, | 33 static void PopulateAutocompleteMatch(const TestData& data, |
33 AutocompleteMatch* match); | 34 AutocompleteMatch* match); |
34 | 35 |
35 // Adds |count| AutocompleteMatches to |matches|. | 36 // Adds |count| AutocompleteMatches to |matches|. |
36 static void PopulateAutocompleteMatches(const TestData* data, | 37 static void PopulateAutocompleteMatches(const TestData* data, |
37 size_t count, | 38 size_t count, |
38 ACMatches* matches); | 39 ACMatches* matches); |
39 | 40 |
40 // Asserts that |result| has |expected_count| matches matching |expected|. | 41 // Asserts that |result| has |expected_count| matches matching |expected|. |
41 void AssertResultMatches(const AutocompleteResult& result, | 42 void AssertResultMatches(const AutocompleteResult& result, |
42 const TestData* expected, | 43 const TestData* expected, |
43 size_t expected_count); | 44 size_t expected_count); |
44 | 45 |
45 // Creates an AutocompleteResult from |last| and |current|. The two are | 46 // Creates an AutocompleteResult from |last| and |current|. The two are |
46 // merged by |CopyOldMatches| and compared by |AssertResultMatches|. | 47 // merged by |CopyOldMatches| and compared by |AssertResultMatches|. |
47 void RunCopyOldMatchesTest(const TestData* last, size_t last_size, | 48 void RunCopyOldMatchesTest(const TestData* last, size_t last_size, |
48 const TestData* current, size_t current_size, | 49 const TestData* current, size_t current_size, |
49 const TestData* expected, size_t expected_size); | 50 const TestData* expected, size_t expected_size); |
50 | 51 |
52 protected: | |
53 scoped_ptr<TestingProfile> profile_; | |
Peter Kasting
2012/10/26 22:13:49
Nit: Does this need to be heap-allocated? Seems l
Bart N.
2012/10/26 23:36:55
Yup. Done.
| |
54 | |
51 private: | 55 private: |
52 DISALLOW_COPY_AND_ASSIGN(AutocompleteResultTest); | 56 DISALLOW_COPY_AND_ASSIGN(AutocompleteResultTest); |
53 }; | 57 }; |
54 | 58 |
55 // static | 59 // static |
56 void AutocompleteResultTest::PopulateAutocompleteMatch( | 60 void AutocompleteResultTest::PopulateAutocompleteMatch( |
57 const TestData& data, | 61 const TestData& data, |
58 AutocompleteMatch* match) { | 62 AutocompleteMatch* match) { |
59 match->provider = reinterpret_cast<AutocompleteProvider*>(data.provider_id); | 63 match->provider = reinterpret_cast<AutocompleteProvider*>(data.provider_id); |
60 match->fill_into_edit = base::IntToString16(data.url_id); | 64 match->fill_into_edit = base::IntToString16(data.url_id); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 const TestData* last, size_t last_size, | 99 const TestData* last, size_t last_size, |
96 const TestData* current, size_t current_size, | 100 const TestData* current, size_t current_size, |
97 const TestData* expected, size_t expected_size) { | 101 const TestData* expected, size_t expected_size) { |
98 AutocompleteInput input(ASCIIToUTF16("a"), string16(), false, false, false, | 102 AutocompleteInput input(ASCIIToUTF16("a"), string16(), false, false, false, |
99 AutocompleteInput::ALL_MATCHES); | 103 AutocompleteInput::ALL_MATCHES); |
100 | 104 |
101 ACMatches last_matches; | 105 ACMatches last_matches; |
102 PopulateAutocompleteMatches(last, last_size, &last_matches); | 106 PopulateAutocompleteMatches(last, last_size, &last_matches); |
103 AutocompleteResult last_result; | 107 AutocompleteResult last_result; |
104 last_result.AppendMatches(last_matches); | 108 last_result.AppendMatches(last_matches); |
105 last_result.SortAndCull(input); | 109 last_result.SortAndCull(input, profile_.get()); |
106 | 110 |
107 ACMatches current_matches; | 111 ACMatches current_matches; |
108 PopulateAutocompleteMatches(current, current_size, ¤t_matches); | 112 PopulateAutocompleteMatches(current, current_size, ¤t_matches); |
109 AutocompleteResult current_result; | 113 AutocompleteResult current_result; |
110 current_result.AppendMatches(current_matches); | 114 current_result.AppendMatches(current_matches); |
111 current_result.SortAndCull(input); | 115 current_result.SortAndCull(input, profile_.get()); |
112 current_result.CopyOldMatches(input, last_result); | 116 current_result.CopyOldMatches(input, last_result, profile_.get()); |
113 | 117 |
114 AssertResultMatches(current_result, expected, expected_size); | 118 AssertResultMatches(current_result, expected, expected_size); |
115 } | 119 } |
116 | 120 |
117 // Assertion testing for AutocompleteResult::Swap. | 121 // Assertion testing for AutocompleteResult::Swap. |
118 TEST_F(AutocompleteResultTest, Swap) { | 122 TEST_F(AutocompleteResultTest, Swap) { |
119 AutocompleteResult r1; | 123 AutocompleteResult r1; |
120 AutocompleteResult r2; | 124 AutocompleteResult r2; |
121 | 125 |
122 // Swap with empty shouldn't do anything interesting. | 126 // Swap with empty shouldn't do anything interesting. |
123 r1.Swap(&r2); | 127 r1.Swap(&r2); |
124 EXPECT_EQ(r1.end(), r1.default_match()); | 128 EXPECT_EQ(r1.end(), r1.default_match()); |
125 EXPECT_EQ(r2.end(), r2.default_match()); | 129 EXPECT_EQ(r2.end(), r2.default_match()); |
126 | 130 |
127 // Swap with a single match. | 131 // Swap with a single match. |
128 ACMatches matches; | 132 ACMatches matches; |
129 AutocompleteMatch match; | 133 AutocompleteMatch match; |
130 AutocompleteInput input(ASCIIToUTF16("a"), string16(), false, false, false, | 134 AutocompleteInput input(ASCIIToUTF16("a"), string16(), false, false, false, |
131 AutocompleteInput::ALL_MATCHES); | 135 AutocompleteInput::ALL_MATCHES); |
132 matches.push_back(match); | 136 matches.push_back(match); |
133 r1.AppendMatches(matches); | 137 r1.AppendMatches(matches); |
134 r1.SortAndCull(input); | 138 r1.SortAndCull(input, profile_.get()); |
135 EXPECT_EQ(r1.begin(), r1.default_match()); | 139 EXPECT_EQ(r1.begin(), r1.default_match()); |
136 EXPECT_EQ("http://a/", r1.alternate_nav_url().spec()); | 140 EXPECT_EQ("http://a/", r1.alternate_nav_url().spec()); |
137 r1.Swap(&r2); | 141 r1.Swap(&r2); |
138 EXPECT_TRUE(r1.empty()); | 142 EXPECT_TRUE(r1.empty()); |
139 EXPECT_EQ(r1.end(), r1.default_match()); | 143 EXPECT_EQ(r1.end(), r1.default_match()); |
140 EXPECT_TRUE(r1.alternate_nav_url().is_empty()); | 144 EXPECT_TRUE(r1.alternate_nav_url().is_empty()); |
141 ASSERT_FALSE(r2.empty()); | 145 ASSERT_FALSE(r2.empty()); |
142 EXPECT_EQ(r2.begin(), r2.default_match()); | 146 EXPECT_EQ(r2.begin(), r2.default_match()); |
143 EXPECT_EQ("http://a/", r2.alternate_nav_url().spec()); | 147 EXPECT_EQ("http://a/", r2.alternate_nav_url().spec()); |
144 } | 148 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 ACMatches matches; | 207 ACMatches matches; |
204 PopulateAutocompleteMatches(data, arraysize(data), &matches); | 208 PopulateAutocompleteMatches(data, arraysize(data), &matches); |
205 matches[1].destination_url = GURL(); | 209 matches[1].destination_url = GURL(); |
206 matches[3].destination_url = GURL(); | 210 matches[3].destination_url = GURL(); |
207 matches[4].destination_url = GURL(); | 211 matches[4].destination_url = GURL(); |
208 | 212 |
209 AutocompleteResult result; | 213 AutocompleteResult result; |
210 result.AppendMatches(matches); | 214 result.AppendMatches(matches); |
211 AutocompleteInput input(string16(), string16(), false, false, false, | 215 AutocompleteInput input(string16(), string16(), false, false, false, |
212 AutocompleteInput::ALL_MATCHES); | 216 AutocompleteInput::ALL_MATCHES); |
213 result.SortAndCull(input); | 217 result.SortAndCull(input, profile_.get()); |
214 | 218 |
215 // Of the two results with the same non-empty destination URL, the | 219 // Of the two results with the same non-empty destination URL, the |
216 // lower-relevance one should be dropped. All of the results with empty URLs | 220 // lower-relevance one should be dropped. All of the results with empty URLs |
217 // should be kept. | 221 // should be kept. |
218 ASSERT_EQ(4U, result.size()); | 222 ASSERT_EQ(4U, result.size()); |
219 EXPECT_TRUE(result.match_at(0)->destination_url.is_empty()); | 223 EXPECT_TRUE(result.match_at(0)->destination_url.is_empty()); |
220 EXPECT_EQ(1300, result.match_at(0)->relevance); | 224 EXPECT_EQ(1300, result.match_at(0)->relevance); |
221 EXPECT_TRUE(result.match_at(1)->destination_url.is_empty()); | 225 EXPECT_TRUE(result.match_at(1)->destination_url.is_empty()); |
222 EXPECT_EQ(1200, result.match_at(1)->relevance); | 226 EXPECT_EQ(1200, result.match_at(1)->relevance); |
223 EXPECT_TRUE(result.match_at(2)->destination_url.is_empty()); | 227 EXPECT_TRUE(result.match_at(2)->destination_url.is_empty()); |
224 EXPECT_EQ(1100, result.match_at(2)->relevance); | 228 EXPECT_EQ(1100, result.match_at(2)->relevance); |
225 EXPECT_EQ("http://b/", result.match_at(3)->destination_url.spec()); | 229 EXPECT_EQ("http://b/", result.match_at(3)->destination_url.spec()); |
226 EXPECT_EQ(1000, result.match_at(3)->relevance); | 230 EXPECT_EQ(1000, result.match_at(3)->relevance); |
227 } | 231 } |
232 | |
233 TEST_F(AutocompleteResultTest, SortAndCullDuplicateSearchURLs) { | |
234 TestData data[] = { | |
235 { 0, 0, 1300 }, | |
236 { 1, 0, 1200 }, | |
237 { 2, 0, 1100 }, | |
238 { 3, 0, 1000 }, | |
239 { 4, 1, 900 }, | |
240 }; | |
241 | |
242 ACMatches matches; | |
243 PopulateAutocompleteMatches(data, arraysize(data), &matches); | |
244 matches[0].destination_url = GURL("http://www.google.com/search?q=foo"); | |
245 matches[1].destination_url = GURL("http://www.google.com/search?q=foo2"); | |
246 matches[2].destination_url = GURL("http://www.google.com/search?q=foo&oq=f"); | |
247 matches[3].destination_url = GURL("http://www.google.com/search?q=foo&aqs=0"); | |
248 matches[4].destination_url = GURL("http://www.google.com/"); | |
Peter Kasting
2012/10/26 22:13:49
This seems like it relies on the test data having
Bart N.
2012/10/26 23:36:55
Yes - I'll update it in the followup CL.
| |
249 | |
250 AutocompleteResult result; | |
251 result.AppendMatches(matches); | |
252 AutocompleteInput input(string16(), string16(), false, false, false, | |
253 AutocompleteInput::ALL_MATCHES); | |
254 result.SortAndCull(input, profile_.get()); | |
255 | |
256 // We expect the 3rd and 4th results to be removed. | |
257 ASSERT_EQ(3U, result.size()); | |
258 EXPECT_EQ("http://www.google.com/search?q=foo", | |
259 result.match_at(0)->destination_url.spec()); | |
260 EXPECT_EQ(1300, result.match_at(0)->relevance); | |
261 EXPECT_EQ("http://www.google.com/search?q=foo2", | |
262 result.match_at(1)->destination_url.spec()); | |
263 EXPECT_EQ(1200, result.match_at(1)->relevance); | |
264 EXPECT_EQ("http://www.google.com/", | |
265 result.match_at(2)->destination_url.spec()); | |
266 EXPECT_EQ(900, result.match_at(2)->relevance); | |
267 } | |
OLD | NEW |