bookmark_provider_unittest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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/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"
25
26using bookmarks::BookmarkMatch;
27
28// The bookmark corpus against which we will simulate searches.
29struct 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
67class BookmarkProviderTest : public testing::Test {
68 public:
69  BookmarkProviderTest();
70
71 protected:
72  virtual void SetUp() OVERRIDE;
73
74  test::TestBookmarkClient client_;
75  scoped_ptr<TestingProfile> profile_;
76  scoped_ptr<BookmarkModel> model_;
77  scoped_refptr<BookmarkProvider> provider_;
78
79 private:
80  DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest);
81};
82
83BookmarkProviderTest::BookmarkProviderTest() {
84  model_ = client_.CreateModel(false);
85}
86
87void BookmarkProviderTest::SetUp() {
88  profile_.reset(new TestingProfile());
89  DCHECK(profile_.get());
90  provider_ = new BookmarkProvider(profile_.get());
91  DCHECK(provider_.get());
92  provider_->set_bookmark_model_for_testing(model_.get());
93
94  const BookmarkNode* other_node = model_->other_node();
95  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(bookmark_provider_test_data); ++i) {
96    const BookmarksTestInfo& cur(bookmark_provider_test_data[i]);
97    const GURL url(cur.url);
98    model_->AddURL(other_node, other_node->child_count(),
99                   base::ASCIIToUTF16(cur.title), url);
100  }
101}
102
103// Structures and functions supporting the BookmarkProviderTest.Positions
104// unit test.
105
106struct TestBookmarkPosition {
107  TestBookmarkPosition(size_t begin, size_t end)
108      : begin(begin), end(end) {}
109
110  size_t begin;
111  size_t end;
112};
113typedef std::vector<TestBookmarkPosition> TestBookmarkPositions;
114
115// Return |positions| as a formatted string for unit test diagnostic output.
116std::string TestBookmarkPositionsAsString(
117    const TestBookmarkPositions& positions) {
118  std::string position_string("{");
119  for (TestBookmarkPositions::const_iterator i = positions.begin();
120       i != positions.end(); ++i) {
121    if (i != positions.begin())
122      position_string += ", ";
123    position_string += "{" + base::IntToString(i->begin) + ", " +
124        base::IntToString(i->end) + "}";
125  }
126  position_string += "}\n";
127  return position_string;
128}
129
130// Return the positions in |matches| as a formatted string for unit test
131// diagnostic output.
132base::string16 MatchesAsString16(const ACMatches& matches) {
133  base::string16 matches_string;
134  for (ACMatches::const_iterator i = matches.begin(); i != matches.end(); ++i) {
135    matches_string.append(base::ASCIIToUTF16("    '"));
136    matches_string.append(i->description);
137    matches_string.append(base::ASCIIToUTF16("'\n"));
138  }
139  return matches_string;
140}
141
142// Comparison function for sorting search terms by descending length.
143bool TestBookmarkPositionsEqual(const TestBookmarkPosition& pos_a,
144                                const TestBookmarkPosition& pos_b) {
145  return pos_a.begin == pos_b.begin && pos_a.end == pos_b.end;
146}
147
148// Convience function to make comparing ACMatchClassifications against the
149// test expectations structure easier.
150TestBookmarkPositions PositionsFromAutocompleteMatch(
151    const AutocompleteMatch& match) {
152  TestBookmarkPositions positions;
153  bool started = false;
154  size_t start = 0;
155  for (AutocompleteMatch::ACMatchClassifications::const_iterator
156       i = match.description_class.begin();
157       i != match.description_class.end(); ++i) {
158    if (i->style & AutocompleteMatch::ACMatchClassification::MATCH) {
159      // We have found the start of a match.
160      EXPECT_FALSE(started);
161      started = true;
162      start = i->offset;
163    } else if (started) {
164      // We have found the end of a match.
165      started = false;
166      positions.push_back(TestBookmarkPosition(start, i->offset));
167      start = 0;
168    }
169  }
170  // Record the final position if the last match goes to the end of the
171  // candidate string.
172  if (started)
173    positions.push_back(TestBookmarkPosition(start, match.description.size()));
174  return positions;
175}
176
177// Convience function to make comparing test expectations structure against the
178// actual ACMatchClassifications easier.
179TestBookmarkPositions PositionsFromExpectations(
180    const size_t expectations[9][2]) {
181  TestBookmarkPositions positions;
182  size_t i = 0;
183  // The array is zero-terminated in the [1]th element.
184  while (expectations[i][1]) {
185    positions.push_back(
186        TestBookmarkPosition(expectations[i][0], expectations[i][1]));
187    ++i;
188  }
189  return positions;
190}
191
192TEST_F(BookmarkProviderTest, Positions) {
193  // Simulate searches.
194  // Description of |positions|:
195  //   The first index represents the collection of positions for each expected
196  //   match. The count of the actual subarrays in each instance of |query_data|
197  //   must equal |match_count|. The second index represents each expected
198  //   match position. The third index represents the |start| and |end| of the
199  //   expected match's position within the |test_data|. This array must be
200  //   terminated by an entry with a value of '0' for |end|.
201  // Example:
202  //   Consider the line for 'def' below:
203  //     {"def", 2, {{{4, 7}, {XXX, 0}}, {{2, 5}, {11, 14}, {XXX, 0}}}},
204  //   There are two expected matches:
205  //     0. {{4, 7}, {XXX, 0}}
206  //     1. {{2, 5}, {11 ,14}, {XXX, 0}}
207  //   For the first match, [0], there is one match within the bookmark's title
208  //   expected, {4, 7}, which maps to the 'def' within "abc def". The 'XXX'
209  //   value is ignored. The second match, [1], indicates that two matches are
210  //   expected within the bookmark title "a definite definition". In each case,
211  //   the {XXX, 0} indicates the end of the subarray. Or:
212  //                 Match #1            Match #2
213  //                 ------------------  ----------------------------
214  //                  Pos1    Term        Pos1    Pos2      Term
215  //                  ------  --------    ------  --------  --------
216  //     {"def", 2, {{{4, 7}, {999, 0}}, {{2, 5}, {11, 14}, {999, 0}}}},
217  //
218  struct QueryData {
219    const std::string query;
220    const size_t match_count;  // This count must match the number of major
221                               // elements in the following |positions| array.
222    const size_t positions[99][9][2];
223  } query_data[] = {
224    // This first set is primarily for position detection validation.
225    {"abc",                   3, {{{0, 3}, {0, 0}},
226                                  {{0, 3}, {0, 0}},
227                                  {{0, 3}, {0, 0}}}},
228    {"abcde",                 2, {{{0, 5}, {0, 0}},
229                                  {{0, 5}, {0, 0}}}},
230    {"foo bar",               0, {{{0, 0}}}},
231    {"fooey bark",            0, {{{0, 0}}}},
232    {"def",                   2, {{{2, 5}, {0, 0}},
233                                  {{4, 7}, {0, 0}}}},
234    {"ghi jkl",               2, {{{0, 3}, {4, 7}, {0, 0}},
235                                  {{0, 3}, {4, 7}, {0, 0}}}},
236    // NB: GetBookmarksWithTitlesMatching(...) uses exact match for "a".
237    {"a",                     1, {{{0, 1}, {0, 0}}}},
238    {"a d",                   0, {{{0, 0}}}},
239    {"carry carbon",          1, {{{0, 5}, {6, 12}, {0, 0}}}},
240    // NB: GetBookmarksWithTitlesMatching(...) sorts the match positions.
241    {"carbon carry",          1, {{{0, 5}, {6, 12}, {0, 0}}}},
242    {"arbon",                 0, {{{0, 0}}}},
243    {"ar",                    0, {{{0, 0}}}},
244    {"arry",                  0, {{{0, 0}}}},
245    // Quoted terms are single terms.
246    {"\"carry carbon\"",      1, {{{0, 12}, {0, 0}}}},
247    {"\"carry carbon\" care", 1, {{{0, 12}, {13, 17}, {0, 0}}}},
248    // Quoted terms require complete word matches.
249    {"\"carry carbo\"",       0, {{{0, 0}}}},
250    // This set uses duplicated and/or overlaps search terms in the title.
251    {"frank",                 1, {{{0, 5}, {8, 13}, {16, 21}, {0, 0}}}},
252    {"frankly",               1, {{{0, 7}, {8, 15}, {0, 0}}}},
253    {"frankly frankly",       1, {{{0, 7}, {8, 15}, {0, 0}}}},
254    {"foobar foo",            1, {{{0, 6}, {7, 13}, {0, 0}}}},
255    {"foo foobar",            1, {{{0, 6}, {7, 13}, {0, 0}}}},
256  };
257
258  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) {
259    AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query),
260                            base::string16::npos, base::string16(), GURL(),
261                            metrics::OmniboxEventProto::INVALID_SPEC, false,
262                            false, false, true,
263                            ChromeAutocompleteSchemeClassifier(profile_.get()));
264    provider_->Start(input, false);
265    const ACMatches& matches(provider_->matches());
266    // Validate number of results is as expected.
267    EXPECT_LE(matches.size(), query_data[i].match_count)
268        << "One or more of the following matches were unexpected:\n"
269        << MatchesAsString16(matches)
270        << "For query '" << query_data[i].query << "'.";
271    EXPECT_GE(matches.size(), query_data[i].match_count)
272        << "One or more expected matches are missing. Matches found:\n"
273        << MatchesAsString16(matches)
274        << "for query '" << query_data[i].query << "'.";
275    // Validate positions within each match is as expected.
276    for (size_t j = 0; j < matches.size(); ++j) {
277      // Collect the expected positions as a vector, collect the match's
278      // classifications for match positions as a vector, then compare.
279      TestBookmarkPositions expected_positions(
280          PositionsFromExpectations(query_data[i].positions[j]));
281      TestBookmarkPositions actual_positions(
282          PositionsFromAutocompleteMatch(matches[j]));
283      EXPECT_TRUE(std::equal(expected_positions.begin(),
284                             expected_positions.end(),
285                             actual_positions.begin(),
286                             TestBookmarkPositionsEqual))
287          << "EXPECTED: " << TestBookmarkPositionsAsString(expected_positions)
288          << "ACTUAL:   " << TestBookmarkPositionsAsString(actual_positions)
289          << "    for query: '" << query_data[i].query << "'.";
290    }
291  }
292}
293
294TEST_F(BookmarkProviderTest, Rankings) {
295  // Simulate searches.
296  struct QueryData {
297    const std::string query;
298    // |match_count| must match the number of elements in the following
299    // |matches| array.
300    const size_t match_count;
301    // |matches| specifies the titles for all bookmarks expected to be matched
302    // by the |query|
303    const std::string matches[3];
304  } query_data[] = {
305    // Basic ranking test.
306    {"abc",       3, {"abcde",      // Most complete match.
307                      "abcdef",
308                      "abc def"}},  // Least complete match.
309    {"ghi",       2, {"ghi jkl",    // Matched earlier.
310                      "jkl ghi",    // Matched later.
311                      ""}},
312    // Rankings of exact-word matches with different URLs.
313    {"achlorhydric",
314                  3, {"achlorhydric mockingbirds resuscitates featherhead",
315                      "achlorhydric featherheads resuscitates mockingbirds",
316                      "featherhead resuscitates achlorhydric mockingbirds"}},
317    {"achlorhydric featherheads",
318                  2, {"achlorhydric featherheads resuscitates mockingbirds",
319                      "mockingbirds resuscitates featherheads achlorhydric",
320                      ""}},
321    {"mockingbirds resuscitates",
322                  3, {"mockingbirds resuscitates featherheads achlorhydric",
323                      "achlorhydric mockingbirds resuscitates featherhead",
324                      "featherhead resuscitates achlorhydric mockingbirds"}},
325    // Ranking of exact-word matches with URL boost.
326    {"worms",     2, {"burning worms #2",    // boosted
327                      "burning worms #1",    // not boosted
328                      ""}},
329    // Ranking of prefix matches with URL boost. Note that a query of
330    // "worm burn" will have the same results.
331    {"burn worm", 3, {"burning worms #2",    // boosted
332                      "worming burns #20",   // boosted
333                      "burning worms #1"}},  // not boosted but shorter
334  };
335
336  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) {
337    AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query),
338                            base::string16::npos, base::string16(), GURL(),
339                            metrics::OmniboxEventProto::INVALID_SPEC, false,
340                            false, false, true,
341                            ChromeAutocompleteSchemeClassifier(profile_.get()));
342    provider_->Start(input, false);
343    const ACMatches& matches(provider_->matches());
344    // Validate number and content of results is as expected.
345    for (size_t j = 0; j < std::max(query_data[i].match_count, matches.size());
346         ++j) {
347      EXPECT_LT(j, query_data[i].match_count) << "    Unexpected match '"
348          << base::UTF16ToUTF8(matches[j].description) << "' for query: '"
349          <<  query_data[i].query << "'.";
350      if (j >= query_data[i].match_count)
351        continue;
352      EXPECT_LT(j, matches.size()) << "    Missing match '"
353          << query_data[i].matches[j] << "' for query: '"
354          << query_data[i].query << "'.";
355      if (j >= matches.size())
356        continue;
357      EXPECT_EQ(query_data[i].matches[j],
358                base::UTF16ToUTF8(matches[j].description))
359          << "    Mismatch at [" << base::IntToString(j) << "] for query '"
360          << query_data[i].query << "'.";
361    }
362  }
363}
364
365TEST_F(BookmarkProviderTest, InlineAutocompletion) {
366  // Simulate searches.
367  struct QueryData {
368    const std::string query;
369    const std::string url;
370    const bool allowed_to_be_default_match;
371    const std::string inline_autocompletion;
372  } query_data[] = {
373    { "bla", "http://blah.com/", true, "h.com" },
374    { "blah ", "http://blah.com/", false, ".com" },
375    { "http://bl", "http://blah.com/", true, "ah.com" },
376    { "fiddle.c", "http://fiddle.com/", true, "om" },
377    { "www", "http://www.www.com/", true, ".com" },
378    { "chro", "chrome://version", true, "me://version" },
379    { "chrome://ve", "chrome://version", true, "rsion" },
380    { "chrome ver", "chrome://version", false, "" },
381    { "versi", "chrome://version", false, "" },
382    { "abou", "chrome://omnibox", false, "" },
383    { "about:om", "chrome://omnibox", true, "nibox" }
384    // Note: when adding a new URL to this test, be sure to add it to the list
385    // of bookmarks at the top of the file as well.  All items in this list
386    // need to be in the bookmarks list because BookmarkProvider's
387    // TitleMatchToACMatch() has an assertion that verifies the URL is
388    // actually bookmarked.
389  };
390
391  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) {
392    const std::string description = "for query=" + query_data[i].query +
393        " and url=" + query_data[i].url;
394    AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query),
395                            base::string16::npos, base::string16(), GURL(),
396                            metrics::OmniboxEventProto::INVALID_SPEC, false,
397                            false, false, true,
398                            ChromeAutocompleteSchemeClassifier(profile_.get()));
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                            metrics::OmniboxEventProto::INVALID_SPEC, false,
444                            false, false, true,
445                            ChromeAutocompleteSchemeClassifier(profile_.get()));
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