bookmark_provider_unittest.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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 "testing/gtest/include/gtest/gtest.h"
24
25using bookmarks::BookmarkMatch;
26
27// The bookmark corpus against which we will simulate searches.
28struct BookmarksTestInfo {
29  std::string title;
30  std::string url;
31} bookmark_provider_test_data[] = {
32  { "abc def", "http://www.catsanddogs.com/a" },
33  { "abcde", "http://www.catsanddogs.com/b" },
34  { "abcdef", "http://www.catsanddogs.com/c" },
35  { "a definition", "http://www.catsanddogs.com/d" },
36  { "carry carbon carefully", "http://www.catsanddogs.com/e" },
37  { "ghi jkl", "http://www.catsanddogs.com/f" },
38  { "jkl ghi", "http://www.catsanddogs.com/g" },
39  { "frankly frankly frank", "http://www.catsanddogs.com/h" },
40  { "foobar foobar", "http://www.foobar.com/" },
41  { "domain", "http://www.domain.com/http/" },
42  { "repeat", "http://www.repeat.com/1/repeat/2/" },
43  // For testing inline_autocompletion.
44  { "http://blah.com/", "http://blah.com/" },
45  { "http://fiddle.com/", "http://fiddle.com/" },
46  { "http://www.www.com/", "http://www.www.com/" },
47  { "chrome://version", "chrome://version" },
48  { "chrome://omnibox", "chrome://omnibox" },
49  // For testing ranking with different URLs.
50  {"achlorhydric featherheads resuscitates mockingbirds",
51   "http://www.featherheads.com/a" },
52  {"achlorhydric mockingbirds resuscitates featherhead",
53   "http://www.featherheads.com/b" },
54  {"featherhead resuscitates achlorhydric mockingbirds",
55   "http://www.featherheads.com/c" },
56  {"mockingbirds resuscitates featherheads achlorhydric",
57   "http://www.featherheads.com/d" },
58  // For testing URL boosting.
59  {"burning worms #1", "http://www.burned.com/" },
60  {"burning worms #2", "http://www.worms.com/" },
61  {"worming burns #10", "http://www.burned.com/" },
62  {"worming burns #20", "http://www.worms.com/" },
63  {"jive music", "http://www.worms.com/" },
64};
65
66class BookmarkProviderTest : public testing::Test,
67                             public AutocompleteProviderListener {
68 public:
69  BookmarkProviderTest();
70
71  // AutocompleteProviderListener: Not called.
72  virtual void OnProviderUpdate(bool updated_matches) OVERRIDE {}
73
74 protected:
75  virtual void SetUp() OVERRIDE;
76
77  test::TestBookmarkClient client_;
78  scoped_ptr<TestingProfile> profile_;
79  scoped_ptr<BookmarkModel> model_;
80  scoped_refptr<BookmarkProvider> provider_;
81
82 private:
83  DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest);
84};
85
86BookmarkProviderTest::BookmarkProviderTest() {
87  model_ = client_.CreateModel(false);
88}
89
90void BookmarkProviderTest::SetUp() {
91  profile_.reset(new TestingProfile());
92  DCHECK(profile_.get());
93  provider_ = new BookmarkProvider(this, profile_.get());
94  DCHECK(provider_.get());
95  provider_->set_bookmark_model_for_testing(model_.get());
96
97  const BookmarkNode* other_node = model_->other_node();
98  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(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);
103  }
104}
105
106// Structures and functions supporting the BookmarkProviderTest.Positions
107// unit test.
108
109struct TestBookmarkPosition {
110  TestBookmarkPosition(size_t begin, size_t end)
111      : begin(begin), end(end) {}
112
113  size_t begin;
114  size_t end;
115};
116typedef std::vector<TestBookmarkPosition> TestBookmarkPositions;
117
118// Return |positions| as a formatted string for unit test diagnostic output.
119std::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) + "}";
128  }
129  position_string += "}\n";
130  return position_string;
131}
132
133// Return the positions in |matches| as a formatted string for unit test
134// diagnostic output.
135base::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"));
141  }
142  return matches_string;
143}
144
145// Comparison function for sorting search terms by descending length.
146bool TestBookmarkPositionsEqual(const TestBookmarkPosition& pos_a,
147                                const TestBookmarkPosition& pos_b) {
148  return pos_a.begin == pos_b.begin && pos_a.end == pos_b.end;
149}
150
151// Convience function to make comparing ACMatchClassifications against the
152// test expectations structure easier.
153TestBookmarkPositions PositionsFromAutocompleteMatch(
154    const AutocompleteMatch& match) {
155  TestBookmarkPositions positions;
156  bool started = false;
157  size_t start = 0;
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);
164      started = true;
165      start = i->offset;
166    } else if (started) {
167      // We have found the end of a match.
168      started = false;
169      positions.push_back(TestBookmarkPosition(start, i->offset));
170      start = 0;
171    }
172  }
173  // Record the final position if the last match goes to the end of the
174  // candidate string.
175  if (started)
176    positions.push_back(TestBookmarkPosition(start, match.description.size()));
177  return positions;
178}
179
180// Convience function to make comparing test expectations structure against the
181// actual ACMatchClassifications easier.
182TestBookmarkPositions PositionsFromExpectations(
183    const size_t expectations[9][2]) {
184  TestBookmarkPositions positions;
185  size_t i = 0;
186  // The array is zero-terminated in the [1]th element.
187  while (expectations[i][1]) {
188    positions.push_back(
189        TestBookmarkPosition(expectations[i][0], expectations[i][1]));
190    ++i;
191  }
192  return positions;
193}
194
195TEST_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|.
204  // Example:
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:
215  //                 Match #1            Match #2
216  //                 ------------------  ----------------------------
217  //                  Pos1    Term        Pos1    Pos2      Term
218  //                  ------  --------    ------  --------  --------
219  //     {"def", 2, {{{4, 7}, {999, 0}}, {{2, 5}, {11, 14}, {999, 0}}}},
220  //
221  struct QueryData {
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];
226  } query_data[] = {
227    // This first set is primarily for position detection validation.
228    {"abc",                   3, {{{0, 3}, {0, 0}},
229                                  {{0, 3}, {0, 0}},
230                                  {{0, 3}, {0, 0}}}},
231    {"abcde",                 2, {{{0, 5}, {0, 0}},
232                                  {{0, 5}, {0, 0}}}},
233    {"foo bar",               0, {{{0, 0}}}},
234    {"fooey bark",            0, {{{0, 0}}}},
235    {"def",                   2, {{{2, 5}, {0, 0}},
236                                  {{4, 7}, {0, 0}}}},
237    {"ghi jkl",               2, {{{0, 3}, {4, 7}, {0, 0}},
238                                  {{0, 3}, {4, 7}, {0, 0}}}},
239    // NB: GetBookmarksWithTitlesMatching(...) uses exact match for "a".
240    {"a",                     1, {{{0, 1}, {0, 0}}}},
241    {"a d",                   0, {{{0, 0}}}},
242    {"carry carbon",          1, {{{0, 5}, {6, 12}, {0, 0}}}},
243    // NB: GetBookmarksWithTitlesMatching(...) sorts the match positions.
244    {"carbon carry",          1, {{{0, 5}, {6, 12}, {0, 0}}}},
245    {"arbon",                 0, {{{0, 0}}}},
246    {"ar",                    0, {{{0, 0}}}},
247    {"arry",                  0, {{{0, 0}}}},
248    // Quoted terms are single terms.
249    {"\"carry carbon\"",      1, {{{0, 12}, {0, 0}}}},
250    {"\"carry carbon\" care", 1, {{{0, 12}, {13, 17}, {0, 0}}}},
251    // Quoted terms require complete word matches.
252    {"\"carry carbo\"",       0, {{{0, 0}}}},
253    // This set uses duplicated and/or overlaps search terms in the title.
254    {"frank",                 1, {{{0, 5}, {8, 13}, {16, 21}, {0, 0}}}},
255    {"frankly",               1, {{{0, 7}, {8, 15}, {0, 0}}}},
256    {"frankly frankly",       1, {{{0, 7}, {8, 15}, {0, 0}}}},
257    {"foobar foo",            1, {{{0, 6}, {7, 13}, {0, 0}}}},
258    {"foo foobar",            1, {{{0, 6}, {7, 13}, {0, 0}}}},
259  };
260
261  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) {
262    AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query),
263                            base::string16::npos, base::string16(), GURL(),
264                            AutocompleteInput::INVALID_SPEC, false, false,
265                            false, true);
266    provider_->Start(input, false);
267    const ACMatches& matches(provider_->matches());
268    // Validate number of results is as expected.
269    EXPECT_LE(matches.size(), query_data[i].match_count)
270        << "One or more of the following matches were unexpected:\n"
271        << MatchesAsString16(matches)
272        << "For query '" << query_data[i].query << "'.";
273    EXPECT_GE(matches.size(), query_data[i].match_count)
274        << "One or more expected matches are missing. Matches found:\n"
275        << MatchesAsString16(matches)
276        << "for query '" << query_data[i].query << "'.";
277    // Validate positions within each match is as expected.
278    for (size_t j = 0; j < matches.size(); ++j) {
279      // Collect the expected positions as a vector, collect the match's
280      // classifications for match positions as a vector, then compare.
281      TestBookmarkPositions expected_positions(
282          PositionsFromExpectations(query_data[i].positions[j]));
283      TestBookmarkPositions actual_positions(
284          PositionsFromAutocompleteMatch(matches[j]));
285      EXPECT_TRUE(std::equal(expected_positions.begin(),
286                             expected_positions.end(),
287                             actual_positions.begin(),
288                             TestBookmarkPositionsEqual))
289          << "EXPECTED: " << TestBookmarkPositionsAsString(expected_positions)
290          << "ACTUAL:   " << TestBookmarkPositionsAsString(actual_positions)
291          << "    for query: '" << query_data[i].query << "'.";
292    }
293  }
294}
295
296TEST_F(BookmarkProviderTest, Rankings) {
297  // Simulate searches.
298  struct QueryData {
299    const std::string query;
300    // |match_count| must match the number of elements in the following
301    // |matches| array.
302    const size_t match_count;
303    // |matches| specifies the titles for all bookmarks expected to be matched
304    // by the |query|
305    const std::string matches[3];
306  } query_data[] = {
307    // Basic ranking test.
308    {"abc",       3, {"abcde",      // Most complete match.
309                      "abcdef",
310                      "abc def"}},  // Least complete match.
311    {"ghi",       2, {"ghi jkl",    // Matched earlier.
312                      "jkl ghi",    // Matched later.
313                      ""}},
314    // Rankings of exact-word matches with different URLs.
315    {"achlorhydric",
316                  3, {"achlorhydric mockingbirds resuscitates featherhead",
317                      "achlorhydric featherheads resuscitates mockingbirds",
318                      "featherhead resuscitates achlorhydric mockingbirds"}},
319    {"achlorhydric featherheads",
320                  2, {"achlorhydric featherheads resuscitates mockingbirds",
321                      "mockingbirds resuscitates featherheads achlorhydric",
322                      ""}},
323    {"mockingbirds resuscitates",
324                  3, {"mockingbirds resuscitates featherheads achlorhydric",
325                      "achlorhydric mockingbirds resuscitates featherhead",
326                      "featherhead resuscitates achlorhydric mockingbirds"}},
327    // Ranking of exact-word matches with URL boost.
328    {"worms",     2, {"burning worms #2",    // boosted
329                      "burning worms #1",    // not boosted
330                      ""}},
331    // Ranking of prefix matches with URL boost. Note that a query of
332    // "worm burn" will have the same results.
333    {"burn worm", 3, {"burning worms #2",    // boosted
334                      "worming burns #20",   // boosted
335                      "burning worms #1"}},  // not boosted but shorter
336  };
337
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                            AutocompleteInput::INVALID_SPEC, false, false,
342                            false, true);
343    provider_->Start(input, false);
344    const ACMatches& matches(provider_->matches());
345    // Validate number and content of results is as expected.
346    for (size_t j = 0; j < std::max(query_data[i].match_count, matches.size());
347         ++j) {
348      EXPECT_LT(j, query_data[i].match_count) << "    Unexpected match '"
349          << base::UTF16ToUTF8(matches[j].description) << "' for query: '"
350          <<  query_data[i].query << "'.";
351      if (j >= query_data[i].match_count)
352        continue;
353      EXPECT_LT(j, matches.size()) << "    Missing match '"
354          << query_data[i].matches[j] << "' for query: '"
355          << query_data[i].query << "'.";
356      if (j >= matches.size())
357        continue;
358      EXPECT_EQ(query_data[i].matches[j],
359                base::UTF16ToUTF8(matches[j].description))
360          << "    Mismatch at [" << base::IntToString(j) << "] for query '"
361          << query_data[i].query << "'.";
362    }
363  }
364}
365
366TEST_F(BookmarkProviderTest, InlineAutocompletion) {
367  // Simulate searches.
368  struct QueryData {
369    const std::string query;
370    const std::string url;
371    const bool allowed_to_be_default_match;
372    const std::string inline_autocompletion;
373  } query_data[] = {
374    { "bla", "http://blah.com/", true, "h.com" },
375    { "blah ", "http://blah.com/", false, ".com" },
376    { "http://bl", "http://blah.com/", true, "ah.com" },
377    { "fiddle.c", "http://fiddle.com/", true, "om" },
378    { "www", "http://www.www.com/", true, ".com" },
379    { "chro", "chrome://version", true, "me://version" },
380    { "chrome://ve", "chrome://version", true, "rsion" },
381    { "chrome ver", "chrome://version", false, "" },
382    { "versi", "chrome://version", false, "" },
383    { "abou", "chrome://omnibox", false, "" },
384    { "about:om", "chrome://omnibox", true, "nibox" }
385    // Note: when adding a new URL to this test, be sure to add it to the list
386    // of bookmarks at the top of the file as well.  All items in this list
387    // need to be in the bookmarks list because BookmarkProvider's
388    // TitleMatchToACMatch() has an assertion that verifies the URL is
389    // actually bookmarked.
390  };
391
392  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) {
393    const std::string description = "for query=" + query_data[i].query +
394        " and url=" + query_data[i].url;
395    AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query),
396                            base::string16::npos, base::string16(), GURL(),
397                            AutocompleteInput::INVALID_SPEC, false, false,
398                            false, true);
399    const base::string16 fixed_up_input(
400        provider_->FixupUserInput(input).second);
401    BookmarkNode node(GURL(query_data[i].url));
402    node.SetTitle(base::ASCIIToUTF16(query_data[i].url));
403    BookmarkMatch bookmark_match;
404    bookmark_match.node = &node;
405    const AutocompleteMatch& ac_match = provider_->BookmarkMatchToACMatch(
406        input, fixed_up_input, bookmark_match);
407    EXPECT_EQ(query_data[i].allowed_to_be_default_match,
408              ac_match.allowed_to_be_default_match) << description;
409    EXPECT_EQ(base::ASCIIToUTF16(query_data[i].inline_autocompletion),
410              ac_match.inline_autocompletion) << description;
411  }
412}
413
414TEST_F(BookmarkProviderTest, StripHttpAndAdjustOffsets) {
415  // Simulate searches.
416  struct QueryData {
417    const std::string query;
418    const std::string expected_contents;
419    // |expected_contents_class| is in format offset:style,offset:style,...
420    const std::string expected_contents_class;
421  } query_data[] = {
422    { "foo",       "www.foobar.com",             "0:1,4:3,7:1"           },
423    { "www foo",   "www.foobar.com",             "0:3,3:1,4:3,7:1"       },
424    { "foo www",   "www.foobar.com",             "0:3,3:1,4:3,7:1"       },
425    { "foo http",  "http://www.foobar.com",      "0:3,4:1,11:3,14:1"     },
426    { "blah",      "blah.com",                   "0:3,4:1"               },
427    { "http blah", "http://blah.com",            "0:3,4:1,7:3,11:1"      },
428    { "dom",       "www.domain.com/http/",       "0:1,4:3,7:1"           },
429    { "dom http",  "http://www.domain.com/http/",
430      "0:3,4:1,11:3,14:1,22:3,26:1"                                      },
431    { "rep",       "www.repeat.com/1/repeat/2/", "0:1,4:3,7:1,17:3,20:1" },
432    { "versi",     "chrome://version",           "0:1,9:3,14:1"          }
433  };
434
435  // Reload the bookmarks index with |index_urls| == true.
436  model_ = client_.CreateModel(true);
437  SetUp();
438
439  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) {
440    std::string description = "for query=" + query_data[i].query;
441    AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query),
442                            base::string16::npos, base::string16(), GURL(),
443                            AutocompleteInput::INVALID_SPEC, false, false,
444                            false, true);
445    provider_->Start(input, false);
446    const ACMatches& matches(provider_->matches());
447    ASSERT_EQ(1U, matches.size()) << description;
448    const AutocompleteMatch& match = matches[0];
449    EXPECT_EQ(base::ASCIIToUTF16(query_data[i].expected_contents),
450              match.contents) << description;
451    std::vector<std::string> class_strings;
452    base::SplitString(
453        query_data[i].expected_contents_class, ',', &class_strings);
454    ASSERT_EQ(class_strings.size(), match.contents_class.size())
455        << description;
456    for (size_t i = 0; i < class_strings.size(); ++i) {
457      std::vector<std::string> chunks;
458      base::SplitString(class_strings[i], ':', &chunks);
459      ASSERT_EQ(2U, chunks.size()) << description;
460      size_t offset;
461      EXPECT_TRUE(base::StringToSizeT(chunks[0], &offset)) << description;
462      EXPECT_EQ(offset, match.contents_class[i].offset) << description;
463      int style;
464      EXPECT_TRUE(base::StringToInt(chunks[1], &style)) << description;
465      EXPECT_EQ(style, match.contents_class[i].style) << description;
466    }
467  }
468}
469