| 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/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_test_util.h" |
| 16 #include "chrome/test/base/testing_profile.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 18 |
| 15 class AutocompleteResultTest : public testing::Test { | 19 class AutocompleteResultTest : public testing::Test { |
| 16 public: | 20 public: |
| 17 struct TestData { | 21 struct TestData { |
| 18 // Used to build a url for the AutocompleteMatch. The URL becomes | 22 // 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"). | 23 // "http://" + ('a' + |url_id|) (e.g. an ID of 2 yields "http://b"). |
| 20 int url_id; | 24 int url_id; |
| 21 | 25 |
| 22 // ID of the provider. | 26 // ID of the provider. |
| 23 int provider_id; | 27 int provider_id; |
| 24 | 28 |
| 25 // Relevance score. | 29 // Relevance score. |
| 26 int relevance; | 30 int relevance; |
| 27 }; | 31 }; |
| 28 | 32 |
| 29 AutocompleteResultTest() {} | 33 AutocompleteResultTest() {} |
| 30 | 34 |
| 35 virtual void SetUp() OVERRIDE { |
| 36 #if defined(OS_ANDROID) |
| 37 TemplateURLPrepopulateData::InitCountryCode( |
| 38 std::string() /* unknown country code */); |
| 39 #endif |
| 40 test_util_.SetUp(); |
| 41 test_util_.VerifyLoad(); |
| 42 } |
| 43 |
| 44 virtual void TearDown() OVERRIDE { |
| 45 test_util_.TearDown(); |
| 46 } |
| 47 |
| 31 // Configures |match| from |data|. | 48 // Configures |match| from |data|. |
| 32 static void PopulateAutocompleteMatch(const TestData& data, | 49 static void PopulateAutocompleteMatch(const TestData& data, |
| 33 AutocompleteMatch* match); | 50 AutocompleteMatch* match); |
| 34 | 51 |
| 35 // Adds |count| AutocompleteMatches to |matches|. | 52 // Adds |count| AutocompleteMatches to |matches|. |
| 36 static void PopulateAutocompleteMatches(const TestData* data, | 53 static void PopulateAutocompleteMatches(const TestData* data, |
| 37 size_t count, | 54 size_t count, |
| 38 ACMatches* matches); | 55 ACMatches* matches); |
| 39 | 56 |
| 40 // Asserts that |result| has |expected_count| matches matching |expected|. | 57 // Asserts that |result| has |expected_count| matches matching |expected|. |
| 41 void AssertResultMatches(const AutocompleteResult& result, | 58 void AssertResultMatches(const AutocompleteResult& result, |
| 42 const TestData* expected, | 59 const TestData* expected, |
| 43 size_t expected_count); | 60 size_t expected_count); |
| 44 | 61 |
| 45 // Creates an AutocompleteResult from |last| and |current|. The two are | 62 // Creates an AutocompleteResult from |last| and |current|. The two are |
| 46 // merged by |CopyOldMatches| and compared by |AssertResultMatches|. | 63 // merged by |CopyOldMatches| and compared by |AssertResultMatches|. |
| 47 void RunCopyOldMatchesTest(const TestData* last, size_t last_size, | 64 void RunCopyOldMatchesTest(const TestData* last, size_t last_size, |
| 48 const TestData* current, size_t current_size, | 65 const TestData* current, size_t current_size, |
| 49 const TestData* expected, size_t expected_size); | 66 const TestData* expected, size_t expected_size); |
| 50 | 67 |
| 68 protected: |
| 69 TemplateURLServiceTestUtil test_util_; |
| 70 |
| 51 private: | 71 private: |
| 52 DISALLOW_COPY_AND_ASSIGN(AutocompleteResultTest); | 72 DISALLOW_COPY_AND_ASSIGN(AutocompleteResultTest); |
| 53 }; | 73 }; |
| 54 | 74 |
| 55 // static | 75 // static |
| 56 void AutocompleteResultTest::PopulateAutocompleteMatch( | 76 void AutocompleteResultTest::PopulateAutocompleteMatch( |
| 57 const TestData& data, | 77 const TestData& data, |
| 58 AutocompleteMatch* match) { | 78 AutocompleteMatch* match) { |
| 59 match->provider = reinterpret_cast<AutocompleteProvider*>(data.provider_id); | 79 match->provider = reinterpret_cast<AutocompleteProvider*>(data.provider_id); |
| 60 match->fill_into_edit = base::IntToString16(data.url_id); | 80 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, | 115 const TestData* last, size_t last_size, |
| 96 const TestData* current, size_t current_size, | 116 const TestData* current, size_t current_size, |
| 97 const TestData* expected, size_t expected_size) { | 117 const TestData* expected, size_t expected_size) { |
| 98 AutocompleteInput input(ASCIIToUTF16("a"), string16(), false, false, false, | 118 AutocompleteInput input(ASCIIToUTF16("a"), string16(), false, false, false, |
| 99 AutocompleteInput::ALL_MATCHES); | 119 AutocompleteInput::ALL_MATCHES); |
| 100 | 120 |
| 101 ACMatches last_matches; | 121 ACMatches last_matches; |
| 102 PopulateAutocompleteMatches(last, last_size, &last_matches); | 122 PopulateAutocompleteMatches(last, last_size, &last_matches); |
| 103 AutocompleteResult last_result; | 123 AutocompleteResult last_result; |
| 104 last_result.AppendMatches(last_matches); | 124 last_result.AppendMatches(last_matches); |
| 105 last_result.SortAndCull(input); | 125 last_result.SortAndCull(input, test_util_.profile()); |
| 106 | 126 |
| 107 ACMatches current_matches; | 127 ACMatches current_matches; |
| 108 PopulateAutocompleteMatches(current, current_size, ¤t_matches); | 128 PopulateAutocompleteMatches(current, current_size, ¤t_matches); |
| 109 AutocompleteResult current_result; | 129 AutocompleteResult current_result; |
| 110 current_result.AppendMatches(current_matches); | 130 current_result.AppendMatches(current_matches); |
| 111 current_result.SortAndCull(input); | 131 current_result.SortAndCull(input, test_util_.profile()); |
| 112 current_result.CopyOldMatches(input, last_result); | 132 current_result.CopyOldMatches(input, last_result, test_util_.profile()); |
| 113 | 133 |
| 114 AssertResultMatches(current_result, expected, expected_size); | 134 AssertResultMatches(current_result, expected, expected_size); |
| 115 } | 135 } |
| 116 | 136 |
| 117 // Assertion testing for AutocompleteResult::Swap. | 137 // Assertion testing for AutocompleteResult::Swap. |
| 118 TEST_F(AutocompleteResultTest, Swap) { | 138 TEST_F(AutocompleteResultTest, Swap) { |
| 119 AutocompleteResult r1; | 139 AutocompleteResult r1; |
| 120 AutocompleteResult r2; | 140 AutocompleteResult r2; |
| 121 | 141 |
| 122 // Swap with empty shouldn't do anything interesting. | 142 // Swap with empty shouldn't do anything interesting. |
| 123 r1.Swap(&r2); | 143 r1.Swap(&r2); |
| 124 EXPECT_EQ(r1.end(), r1.default_match()); | 144 EXPECT_EQ(r1.end(), r1.default_match()); |
| 125 EXPECT_EQ(r2.end(), r2.default_match()); | 145 EXPECT_EQ(r2.end(), r2.default_match()); |
| 126 | 146 |
| 127 // Swap with a single match. | 147 // Swap with a single match. |
| 128 ACMatches matches; | 148 ACMatches matches; |
| 129 AutocompleteMatch match; | 149 AutocompleteMatch match; |
| 130 AutocompleteInput input(ASCIIToUTF16("a"), string16(), false, false, false, | 150 AutocompleteInput input(ASCIIToUTF16("a"), string16(), false, false, false, |
| 131 AutocompleteInput::ALL_MATCHES); | 151 AutocompleteInput::ALL_MATCHES); |
| 132 matches.push_back(match); | 152 matches.push_back(match); |
| 133 r1.AppendMatches(matches); | 153 r1.AppendMatches(matches); |
| 134 r1.SortAndCull(input); | 154 r1.SortAndCull(input, test_util_.profile()); |
| 135 EXPECT_EQ(r1.begin(), r1.default_match()); | 155 EXPECT_EQ(r1.begin(), r1.default_match()); |
| 136 EXPECT_EQ("http://a/", r1.alternate_nav_url().spec()); | 156 EXPECT_EQ("http://a/", r1.alternate_nav_url().spec()); |
| 137 r1.Swap(&r2); | 157 r1.Swap(&r2); |
| 138 EXPECT_TRUE(r1.empty()); | 158 EXPECT_TRUE(r1.empty()); |
| 139 EXPECT_EQ(r1.end(), r1.default_match()); | 159 EXPECT_EQ(r1.end(), r1.default_match()); |
| 140 EXPECT_TRUE(r1.alternate_nav_url().is_empty()); | 160 EXPECT_TRUE(r1.alternate_nav_url().is_empty()); |
| 141 ASSERT_FALSE(r2.empty()); | 161 ASSERT_FALSE(r2.empty()); |
| 142 EXPECT_EQ(r2.begin(), r2.default_match()); | 162 EXPECT_EQ(r2.begin(), r2.default_match()); |
| 143 EXPECT_EQ("http://a/", r2.alternate_nav_url().spec()); | 163 EXPECT_EQ("http://a/", r2.alternate_nav_url().spec()); |
| 144 } | 164 } |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 ACMatches matches; | 223 ACMatches matches; |
| 204 PopulateAutocompleteMatches(data, arraysize(data), &matches); | 224 PopulateAutocompleteMatches(data, arraysize(data), &matches); |
| 205 matches[1].destination_url = GURL(); | 225 matches[1].destination_url = GURL(); |
| 206 matches[3].destination_url = GURL(); | 226 matches[3].destination_url = GURL(); |
| 207 matches[4].destination_url = GURL(); | 227 matches[4].destination_url = GURL(); |
| 208 | 228 |
| 209 AutocompleteResult result; | 229 AutocompleteResult result; |
| 210 result.AppendMatches(matches); | 230 result.AppendMatches(matches); |
| 211 AutocompleteInput input(string16(), string16(), false, false, false, | 231 AutocompleteInput input(string16(), string16(), false, false, false, |
| 212 AutocompleteInput::ALL_MATCHES); | 232 AutocompleteInput::ALL_MATCHES); |
| 213 result.SortAndCull(input); | 233 result.SortAndCull(input, test_util_.profile()); |
| 214 | 234 |
| 215 // Of the two results with the same non-empty destination URL, the | 235 // 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 | 236 // lower-relevance one should be dropped. All of the results with empty URLs |
| 217 // should be kept. | 237 // should be kept. |
| 218 ASSERT_EQ(4U, result.size()); | 238 ASSERT_EQ(4U, result.size()); |
| 219 EXPECT_TRUE(result.match_at(0)->destination_url.is_empty()); | 239 EXPECT_TRUE(result.match_at(0)->destination_url.is_empty()); |
| 220 EXPECT_EQ(1300, result.match_at(0)->relevance); | 240 EXPECT_EQ(1300, result.match_at(0)->relevance); |
| 221 EXPECT_TRUE(result.match_at(1)->destination_url.is_empty()); | 241 EXPECT_TRUE(result.match_at(1)->destination_url.is_empty()); |
| 222 EXPECT_EQ(1200, result.match_at(1)->relevance); | 242 EXPECT_EQ(1200, result.match_at(1)->relevance); |
| 223 EXPECT_TRUE(result.match_at(2)->destination_url.is_empty()); | 243 EXPECT_TRUE(result.match_at(2)->destination_url.is_empty()); |
| 224 EXPECT_EQ(1100, result.match_at(2)->relevance); | 244 EXPECT_EQ(1100, result.match_at(2)->relevance); |
| 225 EXPECT_EQ("http://b/", result.match_at(3)->destination_url.spec()); | 245 EXPECT_EQ("http://b/", result.match_at(3)->destination_url.spec()); |
| 226 EXPECT_EQ(1000, result.match_at(3)->relevance); | 246 EXPECT_EQ(1000, result.match_at(3)->relevance); |
| 227 } | 247 } |
| 248 |
| 249 TEST_F(AutocompleteResultTest, SortAndCullDuplicateSearchURLs) { |
| 250 // Register a template URL that corresponds to 'foo' search engine. |
| 251 TemplateURLData url_data; |
| 252 url_data.short_name = ASCIIToUTF16("unittest"); |
| 253 url_data.SetKeyword(ASCIIToUTF16("foo")); |
| 254 url_data.SetURL("http://www.foo.com/s?q={searchTerms}"); |
| 255 test_util_.model()->Add(new TemplateURL(test_util_.profile(), url_data)); |
| 256 |
| 257 TestData data[] = { |
| 258 { 0, 0, 1300 }, |
| 259 { 1, 0, 1200 }, |
| 260 { 2, 0, 1100 }, |
| 261 { 3, 0, 1000 }, |
| 262 { 4, 1, 900 }, |
| 263 }; |
| 264 |
| 265 ACMatches matches; |
| 266 PopulateAutocompleteMatches(data, arraysize(data), &matches); |
| 267 matches[0].destination_url = GURL("http://www.foo.com/s?q=foo"); |
| 268 matches[1].destination_url = GURL("http://www.foo.com/s?q=foo2"); |
| 269 matches[2].destination_url = GURL("http://www.foo.com/s?q=foo&oq=f"); |
| 270 matches[3].destination_url = GURL("http://www.foo.com/s?q=foo&aqs=0"); |
| 271 matches[4].destination_url = GURL("http://www.foo.com/"); |
| 272 |
| 273 AutocompleteResult result; |
| 274 result.AppendMatches(matches); |
| 275 AutocompleteInput input(string16(), string16(), false, false, false, |
| 276 AutocompleteInput::ALL_MATCHES); |
| 277 result.SortAndCull(input, test_util_.profile()); |
| 278 |
| 279 // We expect the 3rd and 4th results to be removed. |
| 280 ASSERT_EQ(3U, result.size()); |
| 281 EXPECT_EQ("http://www.foo.com/s?q=foo", |
| 282 result.match_at(0)->destination_url.spec()); |
| 283 EXPECT_EQ(1300, result.match_at(0)->relevance); |
| 284 EXPECT_EQ("http://www.foo.com/s?q=foo2", |
| 285 result.match_at(1)->destination_url.spec()); |
| 286 EXPECT_EQ(1200, result.match_at(1)->relevance); |
| 287 EXPECT_EQ("http://www.foo.com/", |
| 288 result.match_at(2)->destination_url.spec()); |
| 289 EXPECT_EQ(900, result.match_at(2)->relevance); |
| 290 } |
| OLD | NEW |