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
3 // found in the LICENSE file.
5 #include "chrome/browser/autocomplete/bookmark_provider.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/bookmarks/browser/bookmark_match.h"
20 #include "components/bookmarks/browser/bookmark_model.h"
21 #include "components/bookmarks/test/test_bookmark_client.h"
22 #include "components/metrics/proto/omnibox_event.pb.h"
23 #include "components/omnibox/autocomplete_provider.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 using bookmarks::BookmarkMatch
;
27 using bookmarks::BookmarkModel
;
28 using bookmarks::BookmarkNode
;
30 // The bookmark corpus against which we will simulate searches.
31 struct BookmarksTestInfo
{
34 } bookmark_provider_test_data
[] = {
35 { "abc def", "http://www.catsanddogs.com/a" },
36 { "abcde", "http://www.catsanddogs.com/b" },
37 { "abcdef", "http://www.catsanddogs.com/c" },
38 { "carry carbon carefully", "http://www.catsanddogs.com/d" },
39 { "a definition", "http://www.catsanddogs.com/e" },
40 { "ghi jkl", "http://www.catsanddogs.com/f" },
41 { "jkl ghi", "http://www.catsanddogs.com/g" },
42 { "frankly frankly frank", "http://www.catsanddogs.com/h" },
43 { "foobar foobar", "http://www.foobar.com/" },
44 { "domain", "http://www.domain.com/http/" },
45 { "repeat", "http://www.repeat.com/1/repeat/2/" },
46 // For testing inline_autocompletion.
47 { "http://blah.com/", "http://blah.com/" },
48 { "http://fiddle.com/", "http://fiddle.com/" },
49 { "http://www.www.com/", "http://www.www.com/" },
50 { "chrome://version", "chrome://version" },
51 { "chrome://omnibox", "chrome://omnibox" },
52 // For testing ranking with different URLs.
53 { "achlorhydric featherheads resuscitates mockingbirds",
54 "http://www.manylongwords.com/1a" },
55 { "achlorhydric mockingbirds resuscitates featherhead",
56 "http://www.manylongwords.com/2b" },
57 { "featherhead resuscitates achlorhydric mockingbirds",
58 "http://www.manylongwords.com/3c" },
59 { "mockingbirds resuscitates featherheads achlorhydric",
60 "http://www.manylongwords.com/4d" },
61 // For testing URL boosting. (URLs referenced multiple times are boosted.)
62 { "burning worms #1", "http://www.burns.com/" },
63 { "burning worms #2", "http://www.worms.com/" },
64 { "worming burns #10", "http://www.burns.com/" },
65 // For testing strange spacing in bookmark titles.
66 { " hello1 hello2 ", "http://whatever.com/" },
67 { "", "http://emptytitle.com/" },
70 class BookmarkProviderTest
: public testing::Test
{
72 BookmarkProviderTest();
75 void SetUp() override
;
77 bookmarks::TestBookmarkClient client_
;
78 scoped_ptr
<TestingProfile
> profile_
;
79 scoped_ptr
<BookmarkModel
> model_
;
80 scoped_refptr
<BookmarkProvider
> provider_
;
83 DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest
);
86 BookmarkProviderTest::BookmarkProviderTest() {
87 model_
= client_
.CreateModel();
90 void BookmarkProviderTest::SetUp() {
91 profile_
.reset(new TestingProfile());
92 DCHECK(profile_
.get());
93 provider_
= new BookmarkProvider(profile_
.get());
94 DCHECK(provider_
.get());
95 provider_
->set_bookmark_model_for_testing(model_
.get());
97 const BookmarkNode
* other_node
= model_
->other_node();
98 for (size_t i
= 0; i
< arraysize(bookmark_provider_test_data
); ++i
) {
99 const BookmarksTestInfo
& cur(bookmark_provider_test_data
[i
]);
100 const GURL
url(cur
.url
);
101 model_
->AddURL(other_node
, other_node
->child_count(),
102 base::ASCIIToUTF16(cur
.title
), url
);
106 // Structures and functions supporting the BookmarkProviderTest.Positions
109 struct TestBookmarkPosition
{
110 TestBookmarkPosition(size_t begin
, size_t end
)
111 : begin(begin
), end(end
) {}
116 typedef std::vector
<TestBookmarkPosition
> TestBookmarkPositions
;
118 // Return |positions| as a formatted string for unit test diagnostic output.
119 std::string
TestBookmarkPositionsAsString(
120 const TestBookmarkPositions
& positions
) {
121 std::string
position_string("{");
122 for (TestBookmarkPositions::const_iterator i
= positions
.begin();
123 i
!= positions
.end(); ++i
) {
124 if (i
!= positions
.begin())
125 position_string
+= ", ";
126 position_string
+= "{" + base::IntToString(i
->begin
) + ", " +
127 base::IntToString(i
->end
) + "}";
129 position_string
+= "}\n";
130 return position_string
;
133 // Return the positions in |matches| as a formatted string for unit test
134 // diagnostic output.
135 base::string16
MatchesAsString16(const ACMatches
& matches
) {
136 base::string16 matches_string
;
137 for (ACMatches::const_iterator i
= matches
.begin(); i
!= matches
.end(); ++i
) {
138 matches_string
.append(base::ASCIIToUTF16(" '"));
139 matches_string
.append(i
->description
);
140 matches_string
.append(base::ASCIIToUTF16("'\n"));
142 return matches_string
;
145 // Comparison function for sorting search terms by descending length.
146 bool TestBookmarkPositionsEqual(const TestBookmarkPosition
& pos_a
,
147 const TestBookmarkPosition
& pos_b
) {
148 return pos_a
.begin
== pos_b
.begin
&& pos_a
.end
== pos_b
.end
;
151 // Convience function to make comparing ACMatchClassifications against the
152 // test expectations structure easier.
153 TestBookmarkPositions
PositionsFromAutocompleteMatch(
154 const AutocompleteMatch
& match
) {
155 TestBookmarkPositions positions
;
156 bool started
= false;
158 for (AutocompleteMatch::ACMatchClassifications::const_iterator
159 i
= match
.description_class
.begin();
160 i
!= match
.description_class
.end(); ++i
) {
161 if (i
->style
& AutocompleteMatch::ACMatchClassification::MATCH
) {
162 // We have found the start of a match.
163 EXPECT_FALSE(started
);
166 } else if (started
) {
167 // We have found the end of a match.
169 positions
.push_back(TestBookmarkPosition(start
, i
->offset
));
173 // Record the final position if the last match goes to the end of the
176 positions
.push_back(TestBookmarkPosition(start
, match
.description
.size()));
180 // Convience function to make comparing test expectations structure against the
181 // actual ACMatchClassifications easier.
182 TestBookmarkPositions
PositionsFromExpectations(
183 const size_t expectations
[9][2]) {
184 TestBookmarkPositions positions
;
186 // The array is zero-terminated in the [1]th element.
187 while (expectations
[i
][1]) {
189 TestBookmarkPosition(expectations
[i
][0], expectations
[i
][1]));
195 TEST_F(BookmarkProviderTest
, Positions
) {
196 // Simulate searches.
197 // Description of |positions|:
198 // The first index represents the collection of positions for each expected
199 // match. The count of the actual subarrays in each instance of |query_data|
200 // must equal |match_count|. The second index represents each expected
201 // match position. The third index represents the |start| and |end| of the
202 // expected match's position within the |test_data|. This array must be
203 // terminated by an entry with a value of '0' for |end|.
205 // Consider the line for 'def' below:
206 // {"def", 2, {{{4, 7}, {XXX, 0}}, {{2, 5}, {11, 14}, {XXX, 0}}}},
207 // There are two expected matches:
208 // 0. {{4, 7}, {XXX, 0}}
209 // 1. {{2, 5}, {11 ,14}, {XXX, 0}}
210 // For the first match, [0], there is one match within the bookmark's title
211 // expected, {4, 7}, which maps to the 'def' within "abc def". The 'XXX'
212 // value is ignored. The second match, [1], indicates that two matches are
213 // expected within the bookmark title "a definite definition". In each case,
214 // the {XXX, 0} indicates the end of the subarray. Or:
216 // ------------------ ----------------------------
217 // Pos1 Term Pos1 Pos2 Term
218 // ------ -------- ------ -------- --------
219 // {"def", 2, {{{4, 7}, {999, 0}}, {{2, 5}, {11, 14}, {999, 0}}}},
222 const std::string query
;
223 const size_t match_count
; // This count must match the number of major
224 // elements in the following |positions| array.
225 const size_t positions
[99][9][2];
227 // This first set is primarily for position detection validation.
228 {"abc", 3, {{{0, 3}, {0, 0}},
231 {"abcde", 2, {{{0, 5}, {0, 0}},
233 {"foo bar", 0, {{{0, 0}}}},
234 {"fooey bark", 0, {{{0, 0}}}},
235 {"def", 2, {{{2, 5}, {0, 0}},
237 {"ghi jkl", 2, {{{0, 3}, {4, 7}, {0, 0}},
238 {{0, 3}, {4, 7}, {0, 0}}}},
239 // NB: GetBookmarksMatching(...) uses exact match for "a" in title or URL.
240 {"a", 2, {{{0, 1}, {0, 0}},
242 {"a d", 0, {{{0, 0}}}},
243 {"carry carbon", 1, {{{0, 5}, {6, 12}, {0, 0}}}},
244 // NB: GetBookmarksMatching(...) sorts the match positions.
245 {"carbon carry", 1, {{{0, 5}, {6, 12}, {0, 0}}}},
246 {"arbon", 0, {{{0, 0}}}},
247 {"ar", 0, {{{0, 0}}}},
248 {"arry", 0, {{{0, 0}}}},
249 // Quoted terms are single terms.
250 {"\"carry carbon\"", 1, {{{0, 12}, {0, 0}}}},
251 {"\"carry carbon\" care", 1, {{{0, 12}, {13, 17}, {0, 0}}}},
252 // Quoted terms require complete word matches.
253 {"\"carry carbo\"", 0, {{{0, 0}}}},
254 // This set uses duplicated and/or overlaps search terms in the title.
255 {"frank", 1, {{{0, 5}, {8, 13}, {16, 21}, {0, 0}}}},
256 {"frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}},
257 {"frankly frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}},
258 {"foobar foo", 1, {{{0, 6}, {7, 13}, {0, 0}}}},
259 {"foo foobar", 1, {{{0, 6}, {7, 13}, {0, 0}}}},
260 // This ensures that leading whitespace in the title is removed.
261 {"hello", 1, {{{0, 5}, {7, 12}, {0, 0}}}},
262 // This ensures that empty titles yield empty classifications.
263 {"emptytitle", 1, {}},
266 for (size_t i
= 0; i
< arraysize(query_data
); ++i
) {
267 AutocompleteInput
input(base::ASCIIToUTF16(query_data
[i
].query
),
268 base::string16::npos
, std::string(), GURL(),
269 metrics::OmniboxEventProto::INVALID_SPEC
, false,
271 ChromeAutocompleteSchemeClassifier(profile_
.get()));
272 provider_
->Start(input
, false, false);
273 const ACMatches
& matches(provider_
->matches());
274 // Validate number of results is as expected.
275 EXPECT_LE(matches
.size(), query_data
[i
].match_count
)
276 << "One or more of the following matches were unexpected:\n"
277 << MatchesAsString16(matches
)
278 << "For query '" << query_data
[i
].query
<< "'.";
279 EXPECT_GE(matches
.size(), query_data
[i
].match_count
)
280 << "One or more expected matches are missing. Matches found:\n"
281 << MatchesAsString16(matches
)
282 << "for query '" << query_data
[i
].query
<< "'.";
283 // Validate positions within each match is as expected.
284 for (size_t j
= 0; j
< matches
.size(); ++j
) {
285 // Collect the expected positions as a vector, collect the match's
286 // classifications for match positions as a vector, then compare.
287 TestBookmarkPositions
expected_positions(
288 PositionsFromExpectations(query_data
[i
].positions
[j
]));
289 TestBookmarkPositions
actual_positions(
290 PositionsFromAutocompleteMatch(matches
[j
]));
291 EXPECT_TRUE(std::equal(expected_positions
.begin(),
292 expected_positions
.end(),
293 actual_positions
.begin(),
294 TestBookmarkPositionsEqual
))
295 << "EXPECTED: " << TestBookmarkPositionsAsString(expected_positions
)
296 << "ACTUAL: " << TestBookmarkPositionsAsString(actual_positions
)
297 << " for query: '" << query_data
[i
].query
<< "'.";
302 TEST_F(BookmarkProviderTest
, Rankings
) {
303 // Simulate searches.
305 const std::string query
;
306 // |match_count| must match the number of elements in the following
308 const size_t match_count
;
309 // |matches| specifies the titles for all bookmarks expected to be matched
311 const std::string matches
[3];
313 // Basic ranking test.
314 {"abc", 3, {"abcde", // Most complete match.
316 "abc def"}}, // Least complete match.
317 {"ghi", 2, {"ghi jkl", // Matched earlier.
318 "jkl ghi", // Matched later.
320 // Rankings of exact-word matches with different URLs.
322 3, {"achlorhydric mockingbirds resuscitates featherhead",
323 "achlorhydric featherheads resuscitates mockingbirds",
324 "featherhead resuscitates achlorhydric mockingbirds"}},
325 {"achlorhydric featherheads",
326 2, {"achlorhydric featherheads resuscitates mockingbirds",
327 "mockingbirds resuscitates featherheads achlorhydric",
329 {"mockingbirds resuscitates",
330 3, {"mockingbirds resuscitates featherheads achlorhydric",
331 "achlorhydric mockingbirds resuscitates featherhead",
332 "featherhead resuscitates achlorhydric mockingbirds"}},
333 // Ranking of exact-word matches with URL boosts.
334 {"worms", 2, {"burning worms #1", // boosted
335 "burning worms #2", // not boosted
337 // Ranking of prefix matches with URL boost.
338 {"burn worm", 3, {"burning worms #1", // boosted
339 "worming burns #10", // boosted but longer title
340 "burning worms #2"}}, // not boosted
341 // A query of "worm burn" will have the same results.
342 {"worm burn", 3, {"burning worms #1", // boosted
343 "worming burns #10", // boosted but longer title
344 "burning worms #2"}}, // not boosted
347 for (size_t i
= 0; i
< arraysize(query_data
); ++i
) {
348 AutocompleteInput
input(base::ASCIIToUTF16(query_data
[i
].query
),
349 base::string16::npos
, std::string(), GURL(),
350 metrics::OmniboxEventProto::INVALID_SPEC
, false,
352 ChromeAutocompleteSchemeClassifier(profile_
.get()));
353 provider_
->Start(input
, false, false);
354 const ACMatches
& matches(provider_
->matches());
355 // Validate number and content of results is as expected.
356 for (size_t j
= 0; j
< std::max(query_data
[i
].match_count
, matches
.size());
358 EXPECT_LT(j
, query_data
[i
].match_count
) << " Unexpected match '"
359 << base::UTF16ToUTF8(matches
[j
].description
) << "' for query: '"
360 << query_data
[i
].query
<< "'.";
361 if (j
>= query_data
[i
].match_count
)
363 EXPECT_LT(j
, matches
.size()) << " Missing match '"
364 << query_data
[i
].matches
[j
] << "' for query: '"
365 << query_data
[i
].query
<< "'.";
366 if (j
>= matches
.size())
368 EXPECT_EQ(query_data
[i
].matches
[j
],
369 base::UTF16ToUTF8(matches
[j
].description
))
370 << " Mismatch at [" << base::IntToString(j
) << "] for query '"
371 << query_data
[i
].query
<< "'.";
376 TEST_F(BookmarkProviderTest
, InlineAutocompletion
) {
377 // Simulate searches.
379 const std::string query
;
380 const std::string url
;
381 const bool allowed_to_be_default_match
;
382 const std::string inline_autocompletion
;
384 { "bla", "http://blah.com/", true, "h.com" },
385 { "blah ", "http://blah.com/", false, ".com" },
386 { "http://bl", "http://blah.com/", true, "ah.com" },
387 { "fiddle.c", "http://fiddle.com/", true, "om" },
388 { "www", "http://www.www.com/", true, ".com" },
389 { "chro", "chrome://version", true, "me://version" },
390 { "chrome://ve", "chrome://version", true, "rsion" },
391 { "chrome ver", "chrome://version", false, "" },
392 { "versi", "chrome://version", false, "" },
393 { "abou", "chrome://omnibox", false, "" },
394 { "about:om", "chrome://omnibox", true, "nibox" }
395 // Note: when adding a new URL to this test, be sure to add it to the list
396 // of bookmarks at the top of the file as well. All items in this list
397 // need to be in the bookmarks list because BookmarkProvider's
398 // TitleMatchToACMatch() has an assertion that verifies the URL is
399 // actually bookmarked.
402 for (size_t i
= 0; i
< arraysize(query_data
); ++i
) {
403 const std::string description
= "for query=" + query_data
[i
].query
+
404 " and url=" + query_data
[i
].url
;
405 AutocompleteInput
input(base::ASCIIToUTF16(query_data
[i
].query
),
406 base::string16::npos
, std::string(), GURL(),
407 metrics::OmniboxEventProto::INVALID_SPEC
, false,
409 ChromeAutocompleteSchemeClassifier(profile_
.get()));
410 const base::string16
fixed_up_input(
411 provider_
->FixupUserInput(input
).second
);
412 BookmarkNode
node(GURL(query_data
[i
].url
));
413 node
.SetTitle(base::ASCIIToUTF16(query_data
[i
].url
));
414 BookmarkMatch bookmark_match
;
415 bookmark_match
.node
= &node
;
416 const AutocompleteMatch
& ac_match
= provider_
->BookmarkMatchToACMatch(
417 input
, fixed_up_input
, bookmark_match
);
418 EXPECT_EQ(query_data
[i
].allowed_to_be_default_match
,
419 ac_match
.allowed_to_be_default_match
) << description
;
420 EXPECT_EQ(base::ASCIIToUTF16(query_data
[i
].inline_autocompletion
),
421 ac_match
.inline_autocompletion
) << description
;
425 TEST_F(BookmarkProviderTest
, StripHttpAndAdjustOffsets
) {
426 // Simulate searches.
428 const std::string query
;
429 const std::string expected_contents
;
430 // |expected_contents_class| is in format offset:style,offset:style,...
431 const std::string expected_contents_class
;
433 { "foo", "www.foobar.com", "0:1,4:3,7:1" },
434 { "www foo", "www.foobar.com", "0:3,3:1,4:3,7:1" },
435 { "foo www", "www.foobar.com", "0:3,3:1,4:3,7:1" },
436 { "foo http", "http://www.foobar.com", "0:3,4:1,11:3,14:1" },
437 { "blah", "blah.com", "0:3,4:1" },
438 { "http blah", "http://blah.com", "0:3,4:1,7:3,11:1" },
439 { "dom", "www.domain.com/http/", "0:1,4:3,7:1" },
440 { "dom http", "http://www.domain.com/http/",
441 "0:3,4:1,11:3,14:1,22:3,26:1" },
442 { "rep", "www.repeat.com/1/repeat/2/", "0:1,4:3,7:1,17:3,20:1" },
443 { "versi", "chrome://version", "0:1,9:3,14:1" }
446 for (size_t i
= 0; i
< arraysize(query_data
); ++i
) {
447 std::string description
= "for query=" + query_data
[i
].query
;
448 AutocompleteInput
input(base::ASCIIToUTF16(query_data
[i
].query
),
449 base::string16::npos
, std::string(), GURL(),
450 metrics::OmniboxEventProto::INVALID_SPEC
, false,
452 ChromeAutocompleteSchemeClassifier(profile_
.get()));
453 provider_
->Start(input
, false, false);
454 const ACMatches
& matches(provider_
->matches());
455 ASSERT_EQ(1U, matches
.size()) << description
;
456 const AutocompleteMatch
& match
= matches
[0];
457 EXPECT_EQ(base::ASCIIToUTF16(query_data
[i
].expected_contents
),
458 match
.contents
) << description
;
459 std::vector
<std::string
> class_strings
;
461 query_data
[i
].expected_contents_class
, ',', &class_strings
);
462 ASSERT_EQ(class_strings
.size(), match
.contents_class
.size())
464 for (size_t i
= 0; i
< class_strings
.size(); ++i
) {
465 std::vector
<std::string
> chunks
;
466 base::SplitString(class_strings
[i
], ':', &chunks
);
467 ASSERT_EQ(2U, chunks
.size()) << description
;
469 EXPECT_TRUE(base::StringToSizeT(chunks
[0], &offset
)) << description
;
470 EXPECT_EQ(offset
, match
.contents_class
[i
].offset
) << description
;
472 EXPECT_TRUE(base::StringToInt(chunks
[1], &style
)) << description
;
473 EXPECT_EQ(style
, match
.contents_class
[i
].style
) << description
;
478 TEST_F(BookmarkProviderTest
, DoesNotProvideMatchesOnFocus
) {
479 AutocompleteInput
input(base::ASCIIToUTF16("foo"),
480 base::string16::npos
, std::string(), GURL(),
481 metrics::OmniboxEventProto::INVALID_SPEC
, false,
483 ChromeAutocompleteSchemeClassifier(profile_
.get()));
484 provider_
->Start(input
, false, true);
485 EXPECT_TRUE(provider_
->matches().empty());