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
;
28 // The bookmark corpus against which we will simulate searches.
29 struct BookmarksTestInfo
{
32 } bookmark_provider_test_data
[] = {
33 { "abc def", "http://www.catsanddogs.com/a" },
34 { "abcde", "http://www.catsanddogs.com/b" },
35 { "abcdef", "http://www.catsanddogs.com/c" },
36 { "carry carbon carefully", "http://www.catsanddogs.com/d" },
37 { "a definition", "http://www.catsanddogs.com/e" },
38 { "ghi jkl", "http://www.catsanddogs.com/f" },
39 { "jkl ghi", "http://www.catsanddogs.com/g" },
40 { "frankly frankly frank", "http://www.catsanddogs.com/h" },
41 { "foobar foobar", "http://www.foobar.com/" },
42 { "domain", "http://www.domain.com/http/" },
43 { "repeat", "http://www.repeat.com/1/repeat/2/" },
44 // For testing inline_autocompletion.
45 { "http://blah.com/", "http://blah.com/" },
46 { "http://fiddle.com/", "http://fiddle.com/" },
47 { "http://www.www.com/", "http://www.www.com/" },
48 { "chrome://version", "chrome://version" },
49 { "chrome://omnibox", "chrome://omnibox" },
50 // For testing ranking with different URLs.
51 {"achlorhydric featherheads resuscitates mockingbirds",
52 "http://www.manylongwords.com/1a" },
53 {"achlorhydric mockingbirds resuscitates featherhead",
54 "http://www.manylongwords.com/2b" },
55 {"featherhead resuscitates achlorhydric mockingbirds",
56 "http://www.manylongwords.com/3c" },
57 {"mockingbirds resuscitates featherheads achlorhydric",
58 "http://www.manylongwords.com/4d" },
59 // For testing URL boosting. (URLs referenced multiple times are boosted.)
60 {"burning worms #1", "http://www.burns.com/" },
61 {"burning worms #2", "http://www.worms.com/" },
62 {"worming burns #10", "http://www.burns.com/" },
65 class BookmarkProviderTest
: public testing::Test
{
67 BookmarkProviderTest();
70 virtual void SetUp() OVERRIDE
;
72 test::TestBookmarkClient client_
;
73 scoped_ptr
<TestingProfile
> profile_
;
74 scoped_ptr
<BookmarkModel
> model_
;
75 scoped_refptr
<BookmarkProvider
> provider_
;
78 DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest
);
81 BookmarkProviderTest::BookmarkProviderTest() {
82 model_
= client_
.CreateModel();
85 void BookmarkProviderTest::SetUp() {
86 profile_
.reset(new TestingProfile());
87 DCHECK(profile_
.get());
88 provider_
= new BookmarkProvider(profile_
.get());
89 DCHECK(provider_
.get());
90 provider_
->set_bookmark_model_for_testing(model_
.get());
92 const BookmarkNode
* other_node
= model_
->other_node();
93 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(bookmark_provider_test_data
); ++i
) {
94 const BookmarksTestInfo
& cur(bookmark_provider_test_data
[i
]);
95 const GURL
url(cur
.url
);
96 model_
->AddURL(other_node
, other_node
->child_count(),
97 base::ASCIIToUTF16(cur
.title
), url
);
101 // Structures and functions supporting the BookmarkProviderTest.Positions
104 struct TestBookmarkPosition
{
105 TestBookmarkPosition(size_t begin
, size_t end
)
106 : begin(begin
), end(end
) {}
111 typedef std::vector
<TestBookmarkPosition
> TestBookmarkPositions
;
113 // Return |positions| as a formatted string for unit test diagnostic output.
114 std::string
TestBookmarkPositionsAsString(
115 const TestBookmarkPositions
& positions
) {
116 std::string
position_string("{");
117 for (TestBookmarkPositions::const_iterator i
= positions
.begin();
118 i
!= positions
.end(); ++i
) {
119 if (i
!= positions
.begin())
120 position_string
+= ", ";
121 position_string
+= "{" + base::IntToString(i
->begin
) + ", " +
122 base::IntToString(i
->end
) + "}";
124 position_string
+= "}\n";
125 return position_string
;
128 // Return the positions in |matches| as a formatted string for unit test
129 // diagnostic output.
130 base::string16
MatchesAsString16(const ACMatches
& matches
) {
131 base::string16 matches_string
;
132 for (ACMatches::const_iterator i
= matches
.begin(); i
!= matches
.end(); ++i
) {
133 matches_string
.append(base::ASCIIToUTF16(" '"));
134 matches_string
.append(i
->description
);
135 matches_string
.append(base::ASCIIToUTF16("'\n"));
137 return matches_string
;
140 // Comparison function for sorting search terms by descending length.
141 bool TestBookmarkPositionsEqual(const TestBookmarkPosition
& pos_a
,
142 const TestBookmarkPosition
& pos_b
) {
143 return pos_a
.begin
== pos_b
.begin
&& pos_a
.end
== pos_b
.end
;
146 // Convience function to make comparing ACMatchClassifications against the
147 // test expectations structure easier.
148 TestBookmarkPositions
PositionsFromAutocompleteMatch(
149 const AutocompleteMatch
& match
) {
150 TestBookmarkPositions positions
;
151 bool started
= false;
153 for (AutocompleteMatch::ACMatchClassifications::const_iterator
154 i
= match
.description_class
.begin();
155 i
!= match
.description_class
.end(); ++i
) {
156 if (i
->style
& AutocompleteMatch::ACMatchClassification::MATCH
) {
157 // We have found the start of a match.
158 EXPECT_FALSE(started
);
161 } else if (started
) {
162 // We have found the end of a match.
164 positions
.push_back(TestBookmarkPosition(start
, i
->offset
));
168 // Record the final position if the last match goes to the end of the
171 positions
.push_back(TestBookmarkPosition(start
, match
.description
.size()));
175 // Convience function to make comparing test expectations structure against the
176 // actual ACMatchClassifications easier.
177 TestBookmarkPositions
PositionsFromExpectations(
178 const size_t expectations
[9][2]) {
179 TestBookmarkPositions positions
;
181 // The array is zero-terminated in the [1]th element.
182 while (expectations
[i
][1]) {
184 TestBookmarkPosition(expectations
[i
][0], expectations
[i
][1]));
190 TEST_F(BookmarkProviderTest
, Positions
) {
191 // Simulate searches.
192 // Description of |positions|:
193 // The first index represents the collection of positions for each expected
194 // match. The count of the actual subarrays in each instance of |query_data|
195 // must equal |match_count|. The second index represents each expected
196 // match position. The third index represents the |start| and |end| of the
197 // expected match's position within the |test_data|. This array must be
198 // terminated by an entry with a value of '0' for |end|.
200 // Consider the line for 'def' below:
201 // {"def", 2, {{{4, 7}, {XXX, 0}}, {{2, 5}, {11, 14}, {XXX, 0}}}},
202 // There are two expected matches:
203 // 0. {{4, 7}, {XXX, 0}}
204 // 1. {{2, 5}, {11 ,14}, {XXX, 0}}
205 // For the first match, [0], there is one match within the bookmark's title
206 // expected, {4, 7}, which maps to the 'def' within "abc def". The 'XXX'
207 // value is ignored. The second match, [1], indicates that two matches are
208 // expected within the bookmark title "a definite definition". In each case,
209 // the {XXX, 0} indicates the end of the subarray. Or:
211 // ------------------ ----------------------------
212 // Pos1 Term Pos1 Pos2 Term
213 // ------ -------- ------ -------- --------
214 // {"def", 2, {{{4, 7}, {999, 0}}, {{2, 5}, {11, 14}, {999, 0}}}},
217 const std::string query
;
218 const size_t match_count
; // This count must match the number of major
219 // elements in the following |positions| array.
220 const size_t positions
[99][9][2];
222 // This first set is primarily for position detection validation.
223 {"abc", 3, {{{0, 3}, {0, 0}},
226 {"abcde", 2, {{{0, 5}, {0, 0}},
228 {"foo bar", 0, {{{0, 0}}}},
229 {"fooey bark", 0, {{{0, 0}}}},
230 {"def", 2, {{{2, 5}, {0, 0}},
232 {"ghi jkl", 2, {{{0, 3}, {4, 7}, {0, 0}},
233 {{0, 3}, {4, 7}, {0, 0}}}},
234 // NB: GetBookmarksMatching(...) uses exact match for "a" in title or URL.
235 {"a", 2, {{{0, 1}, {0, 0}},
237 {"a d", 0, {{{0, 0}}}},
238 {"carry carbon", 1, {{{0, 5}, {6, 12}, {0, 0}}}},
239 // NB: GetBookmarksMatching(...) sorts the match positions.
240 {"carbon carry", 1, {{{0, 5}, {6, 12}, {0, 0}}}},
241 {"arbon", 0, {{{0, 0}}}},
242 {"ar", 0, {{{0, 0}}}},
243 {"arry", 0, {{{0, 0}}}},
244 // Quoted terms are single terms.
245 {"\"carry carbon\"", 1, {{{0, 12}, {0, 0}}}},
246 {"\"carry carbon\" care", 1, {{{0, 12}, {13, 17}, {0, 0}}}},
247 // Quoted terms require complete word matches.
248 {"\"carry carbo\"", 0, {{{0, 0}}}},
249 // This set uses duplicated and/or overlaps search terms in the title.
250 {"frank", 1, {{{0, 5}, {8, 13}, {16, 21}, {0, 0}}}},
251 {"frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}},
252 {"frankly frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}},
253 {"foobar foo", 1, {{{0, 6}, {7, 13}, {0, 0}}}},
254 {"foo foobar", 1, {{{0, 6}, {7, 13}, {0, 0}}}},
257 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(query_data
); ++i
) {
258 AutocompleteInput
input(base::ASCIIToUTF16(query_data
[i
].query
),
259 base::string16::npos
, base::string16(), GURL(),
260 metrics::OmniboxEventProto::INVALID_SPEC
, false,
262 ChromeAutocompleteSchemeClassifier(profile_
.get()));
263 provider_
->Start(input
, false);
264 const ACMatches
& matches(provider_
->matches());
265 // Validate number of results is as expected.
266 EXPECT_LE(matches
.size(), query_data
[i
].match_count
)
267 << "One or more of the following matches were unexpected:\n"
268 << MatchesAsString16(matches
)
269 << "For query '" << query_data
[i
].query
<< "'.";
270 EXPECT_GE(matches
.size(), query_data
[i
].match_count
)
271 << "One or more expected matches are missing. Matches found:\n"
272 << MatchesAsString16(matches
)
273 << "for query '" << query_data
[i
].query
<< "'.";
274 // Validate positions within each match is as expected.
275 for (size_t j
= 0; j
< matches
.size(); ++j
) {
276 // Collect the expected positions as a vector, collect the match's
277 // classifications for match positions as a vector, then compare.
278 TestBookmarkPositions
expected_positions(
279 PositionsFromExpectations(query_data
[i
].positions
[j
]));
280 TestBookmarkPositions
actual_positions(
281 PositionsFromAutocompleteMatch(matches
[j
]));
282 EXPECT_TRUE(std::equal(expected_positions
.begin(),
283 expected_positions
.end(),
284 actual_positions
.begin(),
285 TestBookmarkPositionsEqual
))
286 << "EXPECTED: " << TestBookmarkPositionsAsString(expected_positions
)
287 << "ACTUAL: " << TestBookmarkPositionsAsString(actual_positions
)
288 << " for query: '" << query_data
[i
].query
<< "'.";
293 TEST_F(BookmarkProviderTest
, Rankings
) {
294 // Simulate searches.
296 const std::string query
;
297 // |match_count| must match the number of elements in the following
299 const size_t match_count
;
300 // |matches| specifies the titles for all bookmarks expected to be matched
302 const std::string matches
[3];
304 // Basic ranking test.
305 {"abc", 3, {"abcde", // Most complete match.
307 "abc def"}}, // Least complete match.
308 {"ghi", 2, {"ghi jkl", // Matched earlier.
309 "jkl ghi", // Matched later.
311 // Rankings of exact-word matches with different URLs.
313 3, {"achlorhydric mockingbirds resuscitates featherhead",
314 "achlorhydric featherheads resuscitates mockingbirds",
315 "featherhead resuscitates achlorhydric mockingbirds"}},
316 {"achlorhydric featherheads",
317 2, {"achlorhydric featherheads resuscitates mockingbirds",
318 "mockingbirds resuscitates featherheads achlorhydric",
320 {"mockingbirds resuscitates",
321 3, {"mockingbirds resuscitates featherheads achlorhydric",
322 "achlorhydric mockingbirds resuscitates featherhead",
323 "featherhead resuscitates achlorhydric mockingbirds"}},
324 // Ranking of exact-word matches with URL boosts.
325 {"worms", 2, {"burning worms #1", // boosted
326 "burning worms #2", // not boosted
328 // Ranking of prefix matches with URL boost.
329 {"burn worm", 3, {"burning worms #1", // boosted
330 "worming burns #10", // boosted but longer title
331 "burning worms #2"}}, // not boosted
332 // A query of "worm burn" will have the same results.
333 {"worm burn", 3, {"burning worms #1", // boosted
334 "worming burns #10", // boosted but longer title
335 "burning worms #2"}}, // not boosted
338 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(query_data
); ++i
) {
339 AutocompleteInput
input(base::ASCIIToUTF16(query_data
[i
].query
),
340 base::string16::npos
, base::string16(), GURL(),
341 metrics::OmniboxEventProto::INVALID_SPEC
, false,
343 ChromeAutocompleteSchemeClassifier(profile_
.get()));
344 provider_
->Start(input
, false);
345 const ACMatches
& matches(provider_
->matches());
346 // Validate number and content of results is as expected.
347 for (size_t j
= 0; j
< std::max(query_data
[i
].match_count
, matches
.size());
349 EXPECT_LT(j
, query_data
[i
].match_count
) << " Unexpected match '"
350 << base::UTF16ToUTF8(matches
[j
].description
) << "' for query: '"
351 << query_data
[i
].query
<< "'.";
352 if (j
>= query_data
[i
].match_count
)
354 EXPECT_LT(j
, matches
.size()) << " Missing match '"
355 << query_data
[i
].matches
[j
] << "' for query: '"
356 << query_data
[i
].query
<< "'.";
357 if (j
>= matches
.size())
359 EXPECT_EQ(query_data
[i
].matches
[j
],
360 base::UTF16ToUTF8(matches
[j
].description
))
361 << " Mismatch at [" << base::IntToString(j
) << "] for query '"
362 << query_data
[i
].query
<< "'.";
367 TEST_F(BookmarkProviderTest
, InlineAutocompletion
) {
368 // Simulate searches.
370 const std::string query
;
371 const std::string url
;
372 const bool allowed_to_be_default_match
;
373 const std::string inline_autocompletion
;
375 { "bla", "http://blah.com/", true, "h.com" },
376 { "blah ", "http://blah.com/", false, ".com" },
377 { "http://bl", "http://blah.com/", true, "ah.com" },
378 { "fiddle.c", "http://fiddle.com/", true, "om" },
379 { "www", "http://www.www.com/", true, ".com" },
380 { "chro", "chrome://version", true, "me://version" },
381 { "chrome://ve", "chrome://version", true, "rsion" },
382 { "chrome ver", "chrome://version", false, "" },
383 { "versi", "chrome://version", false, "" },
384 { "abou", "chrome://omnibox", false, "" },
385 { "about:om", "chrome://omnibox", true, "nibox" }
386 // Note: when adding a new URL to this test, be sure to add it to the list
387 // of bookmarks at the top of the file as well. All items in this list
388 // need to be in the bookmarks list because BookmarkProvider's
389 // TitleMatchToACMatch() has an assertion that verifies the URL is
390 // actually bookmarked.
393 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(query_data
); ++i
) {
394 const std::string description
= "for query=" + query_data
[i
].query
+
395 " and url=" + query_data
[i
].url
;
396 AutocompleteInput
input(base::ASCIIToUTF16(query_data
[i
].query
),
397 base::string16::npos
, base::string16(), GURL(),
398 metrics::OmniboxEventProto::INVALID_SPEC
, false,
400 ChromeAutocompleteSchemeClassifier(profile_
.get()));
401 const base::string16
fixed_up_input(
402 provider_
->FixupUserInput(input
).second
);
403 BookmarkNode
node(GURL(query_data
[i
].url
));
404 node
.SetTitle(base::ASCIIToUTF16(query_data
[i
].url
));
405 BookmarkMatch bookmark_match
;
406 bookmark_match
.node
= &node
;
407 const AutocompleteMatch
& ac_match
= provider_
->BookmarkMatchToACMatch(
408 input
, fixed_up_input
, bookmark_match
);
409 EXPECT_EQ(query_data
[i
].allowed_to_be_default_match
,
410 ac_match
.allowed_to_be_default_match
) << description
;
411 EXPECT_EQ(base::ASCIIToUTF16(query_data
[i
].inline_autocompletion
),
412 ac_match
.inline_autocompletion
) << description
;
416 TEST_F(BookmarkProviderTest
, StripHttpAndAdjustOffsets
) {
417 // Simulate searches.
419 const std::string query
;
420 const std::string expected_contents
;
421 // |expected_contents_class| is in format offset:style,offset:style,...
422 const std::string expected_contents_class
;
424 { "foo", "www.foobar.com", "0:1,4:3,7:1" },
425 { "www foo", "www.foobar.com", "0:3,3:1,4:3,7:1" },
426 { "foo www", "www.foobar.com", "0:3,3:1,4:3,7:1" },
427 { "foo http", "http://www.foobar.com", "0:3,4:1,11:3,14:1" },
428 { "blah", "blah.com", "0:3,4:1" },
429 { "http blah", "http://blah.com", "0:3,4:1,7:3,11:1" },
430 { "dom", "www.domain.com/http/", "0:1,4:3,7:1" },
431 { "dom http", "http://www.domain.com/http/",
432 "0:3,4:1,11:3,14:1,22:3,26:1" },
433 { "rep", "www.repeat.com/1/repeat/2/", "0:1,4:3,7:1,17:3,20:1" },
434 { "versi", "chrome://version", "0:1,9:3,14:1" }
437 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(query_data
); ++i
) {
438 std::string description
= "for query=" + query_data
[i
].query
;
439 AutocompleteInput
input(base::ASCIIToUTF16(query_data
[i
].query
),
440 base::string16::npos
, base::string16(), GURL(),
441 metrics::OmniboxEventProto::INVALID_SPEC
, false,
443 ChromeAutocompleteSchemeClassifier(profile_
.get()));
444 provider_
->Start(input
, false);
445 const ACMatches
& matches(provider_
->matches());
446 ASSERT_EQ(1U, matches
.size()) << description
;
447 const AutocompleteMatch
& match
= matches
[0];
448 EXPECT_EQ(base::ASCIIToUTF16(query_data
[i
].expected_contents
),
449 match
.contents
) << description
;
450 std::vector
<std::string
> class_strings
;
452 query_data
[i
].expected_contents_class
, ',', &class_strings
);
453 ASSERT_EQ(class_strings
.size(), match
.contents_class
.size())
455 for (size_t i
= 0; i
< class_strings
.size(); ++i
) {
456 std::vector
<std::string
> chunks
;
457 base::SplitString(class_strings
[i
], ':', &chunks
);
458 ASSERT_EQ(2U, chunks
.size()) << description
;
460 EXPECT_TRUE(base::StringToSizeT(chunks
[0], &offset
)) << description
;
461 EXPECT_EQ(offset
, match
.contents_class
[i
].offset
) << description
;
463 EXPECT_TRUE(base::StringToInt(chunks
[1], &style
)) << description
;
464 EXPECT_EQ(style
, match
.contents_class
[i
].style
) << description
;