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/bookmark_provider.h" | 5 #include "chrome/browser/autocomplete/bookmark_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 using bookmarks::BookmarkMatch; | 26 using bookmarks::BookmarkMatch; |
27 | 27 |
28 // The bookmark corpus against which we will simulate searches. | 28 // The bookmark corpus against which we will simulate searches. |
29 struct BookmarksTestInfo { | 29 struct BookmarksTestInfo { |
30 std::string title; | 30 std::string title; |
31 std::string url; | 31 std::string url; |
32 } bookmark_provider_test_data[] = { | 32 } bookmark_provider_test_data[] = { |
33 { "abc def", "http://www.catsanddogs.com/a" }, | 33 { "abc def", "http://www.catsanddogs.com/a" }, |
34 { "abcde", "http://www.catsanddogs.com/b" }, | 34 { "abcde", "http://www.catsanddogs.com/b" }, |
35 { "abcdef", "http://www.catsanddogs.com/c" }, | 35 { "abcdef", "http://www.catsanddogs.com/c" }, |
36 { "a definition", "http://www.catsanddogs.com/d" }, | 36 { "carry carbon carefully", "http://www.catsanddogs.com/d" }, |
37 { "carry carbon carefully", "http://www.catsanddogs.com/e" }, | 37 { "a definition", "http://www.catsanddogs.com/e" }, |
38 { "ghi jkl", "http://www.catsanddogs.com/f" }, | 38 { "ghi jkl", "http://www.catsanddogs.com/f" }, |
39 { "jkl ghi", "http://www.catsanddogs.com/g" }, | 39 { "jkl ghi", "http://www.catsanddogs.com/g" }, |
40 { "frankly frankly frank", "http://www.catsanddogs.com/h" }, | 40 { "frankly frankly frank", "http://www.catsanddogs.com/h" }, |
41 { "foobar foobar", "http://www.foobar.com/" }, | 41 { "foobar foobar", "http://www.foobar.com/" }, |
42 { "domain", "http://www.domain.com/http/" }, | 42 { "domain", "http://www.domain.com/http/" }, |
43 { "repeat", "http://www.repeat.com/1/repeat/2/" }, | 43 { "repeat", "http://www.repeat.com/1/repeat/2/" }, |
44 // For testing inline_autocompletion. | 44 // For testing inline_autocompletion. |
45 { "http://blah.com/", "http://blah.com/" }, | 45 { "http://blah.com/", "http://blah.com/" }, |
46 { "http://fiddle.com/", "http://fiddle.com/" }, | 46 { "http://fiddle.com/", "http://fiddle.com/" }, |
47 { "http://www.www.com/", "http://www.www.com/" }, | 47 { "http://www.www.com/", "http://www.www.com/" }, |
48 { "chrome://version", "chrome://version" }, | 48 { "chrome://version", "chrome://version" }, |
49 { "chrome://omnibox", "chrome://omnibox" }, | 49 { "chrome://omnibox", "chrome://omnibox" }, |
50 // For testing ranking with different URLs. | 50 // For testing ranking with different URLs. |
51 {"achlorhydric featherheads resuscitates mockingbirds", | 51 { "achlorhydric featherheads resuscitates mockingbirds", |
52 "http://www.featherheads.com/a" }, | 52 "http://www.manylongwords.com/1a" }, |
53 {"achlorhydric mockingbirds resuscitates featherhead", | 53 { "achlorhydric mockingbirds resuscitates featherhead", |
54 "http://www.featherheads.com/b" }, | 54 "http://www.manylongwords.com/2b" }, |
55 {"featherhead resuscitates achlorhydric mockingbirds", | 55 { "featherhead resuscitates achlorhydric mockingbirds", |
56 "http://www.featherheads.com/c" }, | 56 "http://www.manylongwords.com/3c" }, |
57 {"mockingbirds resuscitates featherheads achlorhydric", | 57 { "mockingbirds resuscitates featherheads achlorhydric", |
58 "http://www.featherheads.com/d" }, | 58 "http://www.manylongwords.com/4d" }, |
59 // For testing URL boosting. | 59 // For testing URL boosting. (URLs referenced multiple times are boosted.) |
60 {"burning worms #1", "http://www.burned.com/" }, | 60 { "burning worms #1", "http://www.burns.com/" }, |
61 {"burning worms #2", "http://www.worms.com/" }, | 61 { "burning worms #2", "http://www.worms.com/" }, |
62 {"worming burns #10", "http://www.burned.com/" }, | 62 { "worming burns #10", "http://www.burns.com/" }, |
63 {"worming burns #20", "http://www.worms.com/" }, | |
64 {"jive music", "http://www.worms.com/" }, | |
65 // For testing strange spacing in bookmark titles. | 63 // For testing strange spacing in bookmark titles. |
66 {" hello1 hello2 ", "http://whatever.com/" }, | 64 { " hello1 hello2 ", "http://whatever.com/" }, |
| 65 { "", "http://emptytitle.com/" }, |
67 }; | 66 }; |
68 | 67 |
69 class BookmarkProviderTest : public testing::Test { | 68 class BookmarkProviderTest : public testing::Test { |
70 public: | 69 public: |
71 BookmarkProviderTest(); | 70 BookmarkProviderTest(); |
72 | 71 |
73 protected: | 72 protected: |
74 virtual void SetUp() OVERRIDE; | 73 virtual void SetUp() OVERRIDE; |
75 | 74 |
76 test::TestBookmarkClient client_; | 75 test::TestBookmarkClient client_; |
77 scoped_ptr<TestingProfile> profile_; | 76 scoped_ptr<TestingProfile> profile_; |
78 scoped_ptr<BookmarkModel> model_; | 77 scoped_ptr<BookmarkModel> model_; |
79 scoped_refptr<BookmarkProvider> provider_; | 78 scoped_refptr<BookmarkProvider> provider_; |
80 | 79 |
81 private: | 80 private: |
82 DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest); | 81 DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest); |
83 }; | 82 }; |
84 | 83 |
85 BookmarkProviderTest::BookmarkProviderTest() { | 84 BookmarkProviderTest::BookmarkProviderTest() { |
86 model_ = client_.CreateModel(false); | 85 model_ = client_.CreateModel(); |
87 } | 86 } |
88 | 87 |
89 void BookmarkProviderTest::SetUp() { | 88 void BookmarkProviderTest::SetUp() { |
90 profile_.reset(new TestingProfile()); | 89 profile_.reset(new TestingProfile()); |
91 DCHECK(profile_.get()); | 90 DCHECK(profile_.get()); |
92 provider_ = new BookmarkProvider(profile_.get()); | 91 provider_ = new BookmarkProvider(profile_.get()); |
93 DCHECK(provider_.get()); | 92 DCHECK(provider_.get()); |
94 provider_->set_bookmark_model_for_testing(model_.get()); | 93 provider_->set_bookmark_model_for_testing(model_.get()); |
95 | 94 |
96 const BookmarkNode* other_node = model_->other_node(); | 95 const BookmarkNode* other_node = model_->other_node(); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 {{0, 3}, {0, 0}}, | 227 {{0, 3}, {0, 0}}, |
229 {{0, 3}, {0, 0}}}}, | 228 {{0, 3}, {0, 0}}}}, |
230 {"abcde", 2, {{{0, 5}, {0, 0}}, | 229 {"abcde", 2, {{{0, 5}, {0, 0}}, |
231 {{0, 5}, {0, 0}}}}, | 230 {{0, 5}, {0, 0}}}}, |
232 {"foo bar", 0, {{{0, 0}}}}, | 231 {"foo bar", 0, {{{0, 0}}}}, |
233 {"fooey bark", 0, {{{0, 0}}}}, | 232 {"fooey bark", 0, {{{0, 0}}}}, |
234 {"def", 2, {{{2, 5}, {0, 0}}, | 233 {"def", 2, {{{2, 5}, {0, 0}}, |
235 {{4, 7}, {0, 0}}}}, | 234 {{4, 7}, {0, 0}}}}, |
236 {"ghi jkl", 2, {{{0, 3}, {4, 7}, {0, 0}}, | 235 {"ghi jkl", 2, {{{0, 3}, {4, 7}, {0, 0}}, |
237 {{0, 3}, {4, 7}, {0, 0}}}}, | 236 {{0, 3}, {4, 7}, {0, 0}}}}, |
238 // NB: GetBookmarksWithTitlesMatching(...) uses exact match for "a". | 237 // NB: GetBookmarksMatching(...) uses exact match for "a" in title or URL. |
239 {"a", 1, {{{0, 1}, {0, 0}}}}, | 238 {"a", 2, {{{0, 1}, {0, 0}}, |
| 239 {{0, 0}}}}, |
240 {"a d", 0, {{{0, 0}}}}, | 240 {"a d", 0, {{{0, 0}}}}, |
241 {"carry carbon", 1, {{{0, 5}, {6, 12}, {0, 0}}}}, | 241 {"carry carbon", 1, {{{0, 5}, {6, 12}, {0, 0}}}}, |
242 // NB: GetBookmarksWithTitlesMatching(...) sorts the match positions. | 242 // NB: GetBookmarksMatching(...) sorts the match positions. |
243 {"carbon carry", 1, {{{0, 5}, {6, 12}, {0, 0}}}}, | 243 {"carbon carry", 1, {{{0, 5}, {6, 12}, {0, 0}}}}, |
244 {"arbon", 0, {{{0, 0}}}}, | 244 {"arbon", 0, {{{0, 0}}}}, |
245 {"ar", 0, {{{0, 0}}}}, | 245 {"ar", 0, {{{0, 0}}}}, |
246 {"arry", 0, {{{0, 0}}}}, | 246 {"arry", 0, {{{0, 0}}}}, |
247 // Quoted terms are single terms. | 247 // Quoted terms are single terms. |
248 {"\"carry carbon\"", 1, {{{0, 12}, {0, 0}}}}, | 248 {"\"carry carbon\"", 1, {{{0, 12}, {0, 0}}}}, |
249 {"\"carry carbon\" care", 1, {{{0, 12}, {13, 17}, {0, 0}}}}, | 249 {"\"carry carbon\" care", 1, {{{0, 12}, {13, 17}, {0, 0}}}}, |
250 // Quoted terms require complete word matches. | 250 // Quoted terms require complete word matches. |
251 {"\"carry carbo\"", 0, {{{0, 0}}}}, | 251 {"\"carry carbo\"", 0, {{{0, 0}}}}, |
252 // This set uses duplicated and/or overlaps search terms in the title. | 252 // This set uses duplicated and/or overlaps search terms in the title. |
253 {"frank", 1, {{{0, 5}, {8, 13}, {16, 21}, {0, 0}}}}, | 253 {"frank", 1, {{{0, 5}, {8, 13}, {16, 21}, {0, 0}}}}, |
254 {"frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}}, | 254 {"frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}}, |
255 {"frankly frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}}, | 255 {"frankly frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}}, |
256 {"foobar foo", 1, {{{0, 6}, {7, 13}, {0, 0}}}}, | 256 {"foobar foo", 1, {{{0, 6}, {7, 13}, {0, 0}}}}, |
257 {"foo foobar", 1, {{{0, 6}, {7, 13}, {0, 0}}}}, | 257 {"foo foobar", 1, {{{0, 6}, {7, 13}, {0, 0}}}}, |
258 // This one makes sure that leading whitespace in the title is removed. | 258 // This ensures that leading whitespace in the title is removed. |
259 {"hello", 1, {{{0, 5}, {7, 12}, {0, 0}}}}, | 259 {"hello", 1, {{{0, 5}, {7, 12}, {0, 0}}}}, |
| 260 // This ensures that empty titles yield empty classifications. |
| 261 {"emptytitle", 1, {}}, |
260 }; | 262 }; |
261 | 263 |
262 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { | 264 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { |
263 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query), | 265 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query), |
264 base::string16::npos, base::string16(), GURL(), | 266 base::string16::npos, base::string16(), GURL(), |
265 metrics::OmniboxEventProto::INVALID_SPEC, false, | 267 metrics::OmniboxEventProto::INVALID_SPEC, false, |
266 false, false, true, | 268 false, false, true, |
267 ChromeAutocompleteSchemeClassifier(profile_.get())); | 269 ChromeAutocompleteSchemeClassifier(profile_.get())); |
268 provider_->Start(input, false); | 270 provider_->Start(input, false); |
269 const ACMatches& matches(provider_->matches()); | 271 const ACMatches& matches(provider_->matches()); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 "achlorhydric featherheads resuscitates mockingbirds", | 321 "achlorhydric featherheads resuscitates mockingbirds", |
320 "featherhead resuscitates achlorhydric mockingbirds"}}, | 322 "featherhead resuscitates achlorhydric mockingbirds"}}, |
321 {"achlorhydric featherheads", | 323 {"achlorhydric featherheads", |
322 2, {"achlorhydric featherheads resuscitates mockingbirds", | 324 2, {"achlorhydric featherheads resuscitates mockingbirds", |
323 "mockingbirds resuscitates featherheads achlorhydric", | 325 "mockingbirds resuscitates featherheads achlorhydric", |
324 ""}}, | 326 ""}}, |
325 {"mockingbirds resuscitates", | 327 {"mockingbirds resuscitates", |
326 3, {"mockingbirds resuscitates featherheads achlorhydric", | 328 3, {"mockingbirds resuscitates featherheads achlorhydric", |
327 "achlorhydric mockingbirds resuscitates featherhead", | 329 "achlorhydric mockingbirds resuscitates featherhead", |
328 "featherhead resuscitates achlorhydric mockingbirds"}}, | 330 "featherhead resuscitates achlorhydric mockingbirds"}}, |
329 // Ranking of exact-word matches with URL boost. | 331 // Ranking of exact-word matches with URL boosts. |
330 {"worms", 2, {"burning worms #2", // boosted | 332 {"worms", 2, {"burning worms #1", // boosted |
331 "burning worms #1", // not boosted | 333 "burning worms #2", // not boosted |
332 ""}}, | 334 ""}}, |
333 // Ranking of prefix matches with URL boost. Note that a query of | 335 // Ranking of prefix matches with URL boost. |
334 // "worm burn" will have the same results. | 336 {"burn worm", 3, {"burning worms #1", // boosted |
335 {"burn worm", 3, {"burning worms #2", // boosted | 337 "worming burns #10", // boosted but longer title |
336 "worming burns #20", // boosted | 338 "burning worms #2"}}, // not boosted |
337 "burning worms #1"}}, // not boosted but shorter | 339 // A query of "worm burn" will have the same results. |
| 340 {"worm burn", 3, {"burning worms #1", // boosted |
| 341 "worming burns #10", // boosted but longer title |
| 342 "burning worms #2"}}, // not boosted |
338 }; | 343 }; |
339 | 344 |
340 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { | 345 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { |
341 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query), | 346 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query), |
342 base::string16::npos, base::string16(), GURL(), | 347 base::string16::npos, base::string16(), GURL(), |
343 metrics::OmniboxEventProto::INVALID_SPEC, false, | 348 metrics::OmniboxEventProto::INVALID_SPEC, false, |
344 false, false, true, | 349 false, false, true, |
345 ChromeAutocompleteSchemeClassifier(profile_.get())); | 350 ChromeAutocompleteSchemeClassifier(profile_.get())); |
346 provider_->Start(input, false); | 351 provider_->Start(input, false); |
347 const ACMatches& matches(provider_->matches()); | 352 const ACMatches& matches(provider_->matches()); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 { "foo http", "http://www.foobar.com", "0:3,4:1,11:3,14:1" }, | 434 { "foo http", "http://www.foobar.com", "0:3,4:1,11:3,14:1" }, |
430 { "blah", "blah.com", "0:3,4:1" }, | 435 { "blah", "blah.com", "0:3,4:1" }, |
431 { "http blah", "http://blah.com", "0:3,4:1,7:3,11:1" }, | 436 { "http blah", "http://blah.com", "0:3,4:1,7:3,11:1" }, |
432 { "dom", "www.domain.com/http/", "0:1,4:3,7:1" }, | 437 { "dom", "www.domain.com/http/", "0:1,4:3,7:1" }, |
433 { "dom http", "http://www.domain.com/http/", | 438 { "dom http", "http://www.domain.com/http/", |
434 "0:3,4:1,11:3,14:1,22:3,26:1" }, | 439 "0:3,4:1,11:3,14:1,22:3,26:1" }, |
435 { "rep", "www.repeat.com/1/repeat/2/", "0:1,4:3,7:1,17:3,20:1" }, | 440 { "rep", "www.repeat.com/1/repeat/2/", "0:1,4:3,7:1,17:3,20:1" }, |
436 { "versi", "chrome://version", "0:1,9:3,14:1" } | 441 { "versi", "chrome://version", "0:1,9:3,14:1" } |
437 }; | 442 }; |
438 | 443 |
439 // Reload the bookmarks index with |index_urls| == true. | |
440 model_ = client_.CreateModel(true); | |
441 SetUp(); | |
442 | |
443 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { | 444 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { |
444 std::string description = "for query=" + query_data[i].query; | 445 std::string description = "for query=" + query_data[i].query; |
445 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query), | 446 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query), |
446 base::string16::npos, base::string16(), GURL(), | 447 base::string16::npos, base::string16(), GURL(), |
447 metrics::OmniboxEventProto::INVALID_SPEC, false, | 448 metrics::OmniboxEventProto::INVALID_SPEC, false, |
448 false, false, true, | 449 false, false, true, |
449 ChromeAutocompleteSchemeClassifier(profile_.get())); | 450 ChromeAutocompleteSchemeClassifier(profile_.get())); |
450 provider_->Start(input, false); | 451 provider_->Start(input, false); |
451 const ACMatches& matches(provider_->matches()); | 452 const ACMatches& matches(provider_->matches()); |
452 ASSERT_EQ(1U, matches.size()) << description; | 453 ASSERT_EQ(1U, matches.size()) << description; |
(...skipping 11 matching lines...) Expand all Loading... |
464 ASSERT_EQ(2U, chunks.size()) << description; | 465 ASSERT_EQ(2U, chunks.size()) << description; |
465 size_t offset; | 466 size_t offset; |
466 EXPECT_TRUE(base::StringToSizeT(chunks[0], &offset)) << description; | 467 EXPECT_TRUE(base::StringToSizeT(chunks[0], &offset)) << description; |
467 EXPECT_EQ(offset, match.contents_class[i].offset) << description; | 468 EXPECT_EQ(offset, match.contents_class[i].offset) << description; |
468 int style; | 469 int style; |
469 EXPECT_TRUE(base::StringToInt(chunks[1], &style)) << description; | 470 EXPECT_TRUE(base::StringToInt(chunks[1], &style)) << description; |
470 EXPECT_EQ(style, match.contents_class[i].style) << description; | 471 EXPECT_EQ(style, match.contents_class[i].style) << description; |
471 } | 472 } |
472 } | 473 } |
473 } | 474 } |
OLD | NEW |