• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/autocomplete/bookmark_provider.h"
6 
7 #include <algorithm>
8 #include <string>
9 #include <vector>
10 
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/autocomplete_provider.h"
18 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "components/bookmarks/browser/bookmark_match.h"
21 #include "components/bookmarks/browser/bookmark_model.h"
22 #include "components/bookmarks/test/test_bookmark_client.h"
23 #include "components/metrics/proto/omnibox_event.pb.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 
26 using bookmarks::BookmarkMatch;
27 
28 // The bookmark corpus against which we will simulate searches.
29 struct BookmarksTestInfo {
30   std::string title;
31   std::string url;
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   { "a definition", "http://www.catsanddogs.com/d" },
37   { "carry carbon carefully", "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.featherheads.com/a" },
53   {"achlorhydric mockingbirds resuscitates featherhead",
54    "http://www.featherheads.com/b" },
55   {"featherhead resuscitates achlorhydric mockingbirds",
56    "http://www.featherheads.com/c" },
57   {"mockingbirds resuscitates featherheads achlorhydric",
58    "http://www.featherheads.com/d" },
59   // For testing URL boosting.
60   {"burning worms #1", "http://www.burned.com/" },
61   {"burning worms #2", "http://www.worms.com/" },
62   {"worming burns #10", "http://www.burned.com/" },
63   {"worming burns #20", "http://www.worms.com/" },
64   {"jive music", "http://www.worms.com/" },
65 };
66 
67 class BookmarkProviderTest : public testing::Test,
68                              public AutocompleteProviderListener {
69  public:
70   BookmarkProviderTest();
71 
72   // AutocompleteProviderListener: Not called.
OnProviderUpdate(bool updated_matches)73   virtual void OnProviderUpdate(bool updated_matches) OVERRIDE {}
74 
75  protected:
76   virtual void SetUp() OVERRIDE;
77 
78   test::TestBookmarkClient client_;
79   scoped_ptr<TestingProfile> profile_;
80   scoped_ptr<BookmarkModel> model_;
81   scoped_refptr<BookmarkProvider> provider_;
82 
83  private:
84   DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest);
85 };
86 
BookmarkProviderTest()87 BookmarkProviderTest::BookmarkProviderTest() {
88   model_ = client_.CreateModel(false);
89 }
90 
SetUp()91 void BookmarkProviderTest::SetUp() {
92   profile_.reset(new TestingProfile());
93   DCHECK(profile_.get());
94   provider_ = new BookmarkProvider(this, profile_.get());
95   DCHECK(provider_.get());
96   provider_->set_bookmark_model_for_testing(model_.get());
97 
98   const BookmarkNode* other_node = model_->other_node();
99   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(bookmark_provider_test_data); ++i) {
100     const BookmarksTestInfo& cur(bookmark_provider_test_data[i]);
101     const GURL url(cur.url);
102     model_->AddURL(other_node, other_node->child_count(),
103                    base::ASCIIToUTF16(cur.title), url);
104   }
105 }
106 
107 // Structures and functions supporting the BookmarkProviderTest.Positions
108 // unit test.
109 
110 struct TestBookmarkPosition {
TestBookmarkPositionTestBookmarkPosition111   TestBookmarkPosition(size_t begin, size_t end)
112       : begin(begin), end(end) {}
113 
114   size_t begin;
115   size_t end;
116 };
117 typedef std::vector<TestBookmarkPosition> TestBookmarkPositions;
118 
119 // Return |positions| as a formatted string for unit test diagnostic output.
TestBookmarkPositionsAsString(const TestBookmarkPositions & positions)120 std::string TestBookmarkPositionsAsString(
121     const TestBookmarkPositions& positions) {
122   std::string position_string("{");
123   for (TestBookmarkPositions::const_iterator i = positions.begin();
124        i != positions.end(); ++i) {
125     if (i != positions.begin())
126       position_string += ", ";
127     position_string += "{" + base::IntToString(i->begin) + ", " +
128         base::IntToString(i->end) + "}";
129   }
130   position_string += "}\n";
131   return position_string;
132 }
133 
134 // Return the positions in |matches| as a formatted string for unit test
135 // diagnostic output.
MatchesAsString16(const ACMatches & matches)136 base::string16 MatchesAsString16(const ACMatches& matches) {
137   base::string16 matches_string;
138   for (ACMatches::const_iterator i = matches.begin(); i != matches.end(); ++i) {
139     matches_string.append(base::ASCIIToUTF16("    '"));
140     matches_string.append(i->description);
141     matches_string.append(base::ASCIIToUTF16("'\n"));
142   }
143   return matches_string;
144 }
145 
146 // Comparison function for sorting search terms by descending length.
TestBookmarkPositionsEqual(const TestBookmarkPosition & pos_a,const TestBookmarkPosition & pos_b)147 bool TestBookmarkPositionsEqual(const TestBookmarkPosition& pos_a,
148                                 const TestBookmarkPosition& pos_b) {
149   return pos_a.begin == pos_b.begin && pos_a.end == pos_b.end;
150 }
151 
152 // Convience function to make comparing ACMatchClassifications against the
153 // test expectations structure easier.
PositionsFromAutocompleteMatch(const AutocompleteMatch & match)154 TestBookmarkPositions PositionsFromAutocompleteMatch(
155     const AutocompleteMatch& match) {
156   TestBookmarkPositions positions;
157   bool started = false;
158   size_t start = 0;
159   for (AutocompleteMatch::ACMatchClassifications::const_iterator
160        i = match.description_class.begin();
161        i != match.description_class.end(); ++i) {
162     if (i->style & AutocompleteMatch::ACMatchClassification::MATCH) {
163       // We have found the start of a match.
164       EXPECT_FALSE(started);
165       started = true;
166       start = i->offset;
167     } else if (started) {
168       // We have found the end of a match.
169       started = false;
170       positions.push_back(TestBookmarkPosition(start, i->offset));
171       start = 0;
172     }
173   }
174   // Record the final position if the last match goes to the end of the
175   // candidate string.
176   if (started)
177     positions.push_back(TestBookmarkPosition(start, match.description.size()));
178   return positions;
179 }
180 
181 // Convience function to make comparing test expectations structure against the
182 // actual ACMatchClassifications easier.
PositionsFromExpectations(const size_t expectations[9][2])183 TestBookmarkPositions PositionsFromExpectations(
184     const size_t expectations[9][2]) {
185   TestBookmarkPositions positions;
186   size_t i = 0;
187   // The array is zero-terminated in the [1]th element.
188   while (expectations[i][1]) {
189     positions.push_back(
190         TestBookmarkPosition(expectations[i][0], expectations[i][1]));
191     ++i;
192   }
193   return positions;
194 }
195 
TEST_F(BookmarkProviderTest,Positions)196 TEST_F(BookmarkProviderTest, Positions) {
197   // Simulate searches.
198   // Description of |positions|:
199   //   The first index represents the collection of positions for each expected
200   //   match. The count of the actual subarrays in each instance of |query_data|
201   //   must equal |match_count|. The second index represents each expected
202   //   match position. The third index represents the |start| and |end| of the
203   //   expected match's position within the |test_data|. This array must be
204   //   terminated by an entry with a value of '0' for |end|.
205   // Example:
206   //   Consider the line for 'def' below:
207   //     {"def", 2, {{{4, 7}, {XXX, 0}}, {{2, 5}, {11, 14}, {XXX, 0}}}},
208   //   There are two expected matches:
209   //     0. {{4, 7}, {XXX, 0}}
210   //     1. {{2, 5}, {11 ,14}, {XXX, 0}}
211   //   For the first match, [0], there is one match within the bookmark's title
212   //   expected, {4, 7}, which maps to the 'def' within "abc def". The 'XXX'
213   //   value is ignored. The second match, [1], indicates that two matches are
214   //   expected within the bookmark title "a definite definition". In each case,
215   //   the {XXX, 0} indicates the end of the subarray. Or:
216   //                 Match #1            Match #2
217   //                 ------------------  ----------------------------
218   //                  Pos1    Term        Pos1    Pos2      Term
219   //                  ------  --------    ------  --------  --------
220   //     {"def", 2, {{{4, 7}, {999, 0}}, {{2, 5}, {11, 14}, {999, 0}}}},
221   //
222   struct QueryData {
223     const std::string query;
224     const size_t match_count;  // This count must match the number of major
225                                // elements in the following |positions| array.
226     const size_t positions[99][9][2];
227   } query_data[] = {
228     // This first set is primarily for position detection validation.
229     {"abc",                   3, {{{0, 3}, {0, 0}},
230                                   {{0, 3}, {0, 0}},
231                                   {{0, 3}, {0, 0}}}},
232     {"abcde",                 2, {{{0, 5}, {0, 0}},
233                                   {{0, 5}, {0, 0}}}},
234     {"foo bar",               0, {{{0, 0}}}},
235     {"fooey bark",            0, {{{0, 0}}}},
236     {"def",                   2, {{{2, 5}, {0, 0}},
237                                   {{4, 7}, {0, 0}}}},
238     {"ghi jkl",               2, {{{0, 3}, {4, 7}, {0, 0}},
239                                   {{0, 3}, {4, 7}, {0, 0}}}},
240     // NB: GetBookmarksWithTitlesMatching(...) uses exact match for "a".
241     {"a",                     1, {{{0, 1}, {0, 0}}}},
242     {"a d",                   0, {{{0, 0}}}},
243     {"carry carbon",          1, {{{0, 5}, {6, 12}, {0, 0}}}},
244     // NB: GetBookmarksWithTitlesMatching(...) 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   };
261 
262   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) {
263     AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query),
264                             base::string16::npos, base::string16(), GURL(),
265                             metrics::OmniboxEventProto::INVALID_SPEC, false,
266                             false, false, true);
267     provider_->Start(input, false);
268     const ACMatches& matches(provider_->matches());
269     // Validate number of results is as expected.
270     EXPECT_LE(matches.size(), query_data[i].match_count)
271         << "One or more of the following matches were unexpected:\n"
272         << MatchesAsString16(matches)
273         << "For query '" << query_data[i].query << "'.";
274     EXPECT_GE(matches.size(), query_data[i].match_count)
275         << "One or more expected matches are missing. Matches found:\n"
276         << MatchesAsString16(matches)
277         << "for query '" << query_data[i].query << "'.";
278     // Validate positions within each match is as expected.
279     for (size_t j = 0; j < matches.size(); ++j) {
280       // Collect the expected positions as a vector, collect the match's
281       // classifications for match positions as a vector, then compare.
282       TestBookmarkPositions expected_positions(
283           PositionsFromExpectations(query_data[i].positions[j]));
284       TestBookmarkPositions actual_positions(
285           PositionsFromAutocompleteMatch(matches[j]));
286       EXPECT_TRUE(std::equal(expected_positions.begin(),
287                              expected_positions.end(),
288                              actual_positions.begin(),
289                              TestBookmarkPositionsEqual))
290           << "EXPECTED: " << TestBookmarkPositionsAsString(expected_positions)
291           << "ACTUAL:   " << TestBookmarkPositionsAsString(actual_positions)
292           << "    for query: '" << query_data[i].query << "'.";
293     }
294   }
295 }
296 
TEST_F(BookmarkProviderTest,Rankings)297 TEST_F(BookmarkProviderTest, Rankings) {
298   // Simulate searches.
299   struct QueryData {
300     const std::string query;
301     // |match_count| must match the number of elements in the following
302     // |matches| array.
303     const size_t match_count;
304     // |matches| specifies the titles for all bookmarks expected to be matched
305     // by the |query|
306     const std::string matches[3];
307   } query_data[] = {
308     // Basic ranking test.
309     {"abc",       3, {"abcde",      // Most complete match.
310                       "abcdef",
311                       "abc def"}},  // Least complete match.
312     {"ghi",       2, {"ghi jkl",    // Matched earlier.
313                       "jkl ghi",    // Matched later.
314                       ""}},
315     // Rankings of exact-word matches with different URLs.
316     {"achlorhydric",
317                   3, {"achlorhydric mockingbirds resuscitates featherhead",
318                       "achlorhydric featherheads resuscitates mockingbirds",
319                       "featherhead resuscitates achlorhydric mockingbirds"}},
320     {"achlorhydric featherheads",
321                   2, {"achlorhydric featherheads resuscitates mockingbirds",
322                       "mockingbirds resuscitates featherheads achlorhydric",
323                       ""}},
324     {"mockingbirds resuscitates",
325                   3, {"mockingbirds resuscitates featherheads achlorhydric",
326                       "achlorhydric mockingbirds resuscitates featherhead",
327                       "featherhead resuscitates achlorhydric mockingbirds"}},
328     // Ranking of exact-word matches with URL boost.
329     {"worms",     2, {"burning worms #2",    // boosted
330                       "burning worms #1",    // not boosted
331                       ""}},
332     // Ranking of prefix matches with URL boost. Note that a query of
333     // "worm burn" will have the same results.
334     {"burn worm", 3, {"burning worms #2",    // boosted
335                       "worming burns #20",   // boosted
336                       "burning worms #1"}},  // not boosted but shorter
337   };
338 
339   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) {
340     AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query),
341                             base::string16::npos, base::string16(), GURL(),
342                             metrics::OmniboxEventProto::INVALID_SPEC, false,
343                             false, false, true);
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());
348          ++j) {
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)
353         continue;
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())
358         continue;
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 << "'.";
363     }
364   }
365 }
366 
TEST_F(BookmarkProviderTest,InlineAutocompletion)367 TEST_F(BookmarkProviderTest, InlineAutocompletion) {
368   // Simulate searches.
369   struct QueryData {
370     const std::string query;
371     const std::string url;
372     const bool allowed_to_be_default_match;
373     const std::string inline_autocompletion;
374   } query_data[] = {
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.
391   };
392 
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,
399                             false, false, true);
400     const base::string16 fixed_up_input(
401         provider_->FixupUserInput(input).second);
402     BookmarkNode node(GURL(query_data[i].url));
403     node.SetTitle(base::ASCIIToUTF16(query_data[i].url));
404     BookmarkMatch bookmark_match;
405     bookmark_match.node = &node;
406     const AutocompleteMatch& ac_match = provider_->BookmarkMatchToACMatch(
407         input, fixed_up_input, bookmark_match);
408     EXPECT_EQ(query_data[i].allowed_to_be_default_match,
409               ac_match.allowed_to_be_default_match) << description;
410     EXPECT_EQ(base::ASCIIToUTF16(query_data[i].inline_autocompletion),
411               ac_match.inline_autocompletion) << description;
412   }
413 }
414 
TEST_F(BookmarkProviderTest,StripHttpAndAdjustOffsets)415 TEST_F(BookmarkProviderTest, StripHttpAndAdjustOffsets) {
416   // Simulate searches.
417   struct QueryData {
418     const std::string query;
419     const std::string expected_contents;
420     // |expected_contents_class| is in format offset:style,offset:style,...
421     const std::string expected_contents_class;
422   } query_data[] = {
423     { "foo",       "www.foobar.com",             "0:1,4:3,7:1"           },
424     { "www foo",   "www.foobar.com",             "0:3,3:1,4:3,7:1"       },
425     { "foo www",   "www.foobar.com",             "0:3,3:1,4:3,7:1"       },
426     { "foo http",  "http://www.foobar.com",      "0:3,4:1,11:3,14:1"     },
427     { "blah",      "blah.com",                   "0:3,4:1"               },
428     { "http blah", "http://blah.com",            "0:3,4:1,7:3,11:1"      },
429     { "dom",       "www.domain.com/http/",       "0:1,4:3,7:1"           },
430     { "dom http",  "http://www.domain.com/http/",
431       "0:3,4:1,11:3,14:1,22:3,26:1"                                      },
432     { "rep",       "www.repeat.com/1/repeat/2/", "0:1,4:3,7:1,17:3,20:1" },
433     { "versi",     "chrome://version",           "0:1,9:3,14:1"          }
434   };
435 
436   // Reload the bookmarks index with |index_urls| == true.
437   model_ = client_.CreateModel(true);
438   SetUp();
439 
440   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) {
441     std::string description = "for query=" + query_data[i].query;
442     AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query),
443                             base::string16::npos, base::string16(), GURL(),
444                             metrics::OmniboxEventProto::INVALID_SPEC, false,
445                             false, false, true);
446     provider_->Start(input, false);
447     const ACMatches& matches(provider_->matches());
448     ASSERT_EQ(1U, matches.size()) << description;
449     const AutocompleteMatch& match = matches[0];
450     EXPECT_EQ(base::ASCIIToUTF16(query_data[i].expected_contents),
451               match.contents) << description;
452     std::vector<std::string> class_strings;
453     base::SplitString(
454         query_data[i].expected_contents_class, ',', &class_strings);
455     ASSERT_EQ(class_strings.size(), match.contents_class.size())
456         << description;
457     for (size_t i = 0; i < class_strings.size(); ++i) {
458       std::vector<std::string> chunks;
459       base::SplitString(class_strings[i], ':', &chunks);
460       ASSERT_EQ(2U, chunks.size()) << description;
461       size_t offset;
462       EXPECT_TRUE(base::StringToSizeT(chunks[0], &offset)) << description;
463       EXPECT_EQ(offset, match.contents_class[i].offset) << description;
464       int style;
465       EXPECT_TRUE(base::StringToInt(chunks[1], &style)) << description;
466       EXPECT_EQ(style, match.contents_class[i].style) << description;
467     }
468   }
469 }
470