Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / autocomplete / bookmark_provider_unittest.cc
blob77d2dfb78a6404901c3327435aceab1fb02eaba4
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 "components/omnibox/browser/bookmark_provider.h"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
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_provider_client.h"
18 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.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 "components/omnibox/browser/autocomplete_provider.h"
25 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 using bookmarks::BookmarkMatch;
29 using bookmarks::BookmarkModel;
30 using bookmarks::BookmarkNode;
32 // The bookmark corpus against which we will simulate searches.
33 struct BookmarksTestInfo {
34 std::string title;
35 std::string url;
36 } bookmark_provider_test_data[] = {
37 { "abc def", "http://www.catsanddogs.com/a" },
38 { "abcde", "http://www.catsanddogs.com/b" },
39 { "abcdef", "http://www.catsanddogs.com/c" },
40 { "carry carbon carefully", "http://www.catsanddogs.com/d" },
41 { "a definition", "http://www.catsanddogs.com/e" },
42 { "ghi jkl", "http://www.catsanddogs.com/f" },
43 { "jkl ghi", "http://www.catsanddogs.com/g" },
44 { "frankly frankly frank", "http://www.catsanddogs.com/h" },
45 { "foobar foobar", "http://www.foobar.com/" },
46 { "domain", "http://www.domain.com/http/" },
47 { "repeat", "http://www.repeat.com/1/repeat/2/" },
48 // For testing inline_autocompletion.
49 { "http://blah.com/", "http://blah.com/" },
50 { "http://fiddle.com/", "http://fiddle.com/" },
51 { "http://www.www.com/", "http://www.www.com/" },
52 { "chrome://version", "chrome://version" },
53 { "chrome://omnibox", "chrome://omnibox" },
54 // For testing ranking with different URLs.
55 { "achlorhydric featherheads resuscitates mockingbirds",
56 "http://www.manylongwords.com/1a" },
57 { "achlorhydric mockingbirds resuscitates featherhead",
58 "http://www.manylongwords.com/2b" },
59 { "featherhead resuscitates achlorhydric mockingbirds",
60 "http://www.manylongwords.com/3c" },
61 { "mockingbirds resuscitates featherheads achlorhydric",
62 "http://www.manylongwords.com/4d" },
63 // For testing URL boosting. (URLs referenced multiple times are boosted.)
64 { "burning worms #1", "http://www.burns.com/" },
65 { "burning worms #2", "http://www.worms.com/" },
66 { "worming burns #10", "http://www.burns.com/" },
67 // For testing strange spacing in bookmark titles.
68 { " hello1 hello2 ", "http://whatever.com/" },
69 { "", "http://emptytitle.com/" },
72 class BookmarkProviderTest : public testing::Test {
73 public:
74 BookmarkProviderTest();
76 protected:
77 void SetUp() override;
79 content::TestBrowserThreadBundle thread_bundle_;
80 bookmarks::TestBookmarkClient bookmark_client_;
81 scoped_ptr<TestingProfile> profile_;
82 scoped_ptr<ChromeAutocompleteProviderClient> provider_client_;
83 scoped_ptr<BookmarkModel> model_;
84 scoped_refptr<BookmarkProvider> provider_;
86 private:
87 DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest);
90 BookmarkProviderTest::BookmarkProviderTest() {
91 model_ = bookmark_client_.CreateModel();
94 void BookmarkProviderTest::SetUp() {
95 profile_.reset(new TestingProfile());
96 DCHECK(profile_.get());
97 provider_client_.reset(new ChromeAutocompleteProviderClient(profile_.get()));
98 provider_ = new BookmarkProvider(provider_client_.get());
99 DCHECK(provider_.get());
100 provider_->set_bookmark_model_for_testing(model_.get());
102 const BookmarkNode* other_node = model_->other_node();
103 for (size_t i = 0; i < arraysize(bookmark_provider_test_data); ++i) {
104 const BookmarksTestInfo& cur(bookmark_provider_test_data[i]);
105 const GURL url(cur.url);
106 model_->AddURL(other_node, other_node->child_count(),
107 base::ASCIIToUTF16(cur.title), url);
111 // Structures and functions supporting the BookmarkProviderTest.Positions
112 // unit test.
114 struct TestBookmarkPosition {
115 TestBookmarkPosition(size_t begin, size_t end)
116 : begin(begin), end(end) {}
118 size_t begin;
119 size_t end;
121 typedef std::vector<TestBookmarkPosition> TestBookmarkPositions;
123 // Return |positions| as a formatted string for unit test diagnostic output.
124 std::string TestBookmarkPositionsAsString(
125 const TestBookmarkPositions& positions) {
126 std::string position_string("{");
127 for (TestBookmarkPositions::const_iterator i = positions.begin();
128 i != positions.end(); ++i) {
129 if (i != positions.begin())
130 position_string += ", ";
131 position_string += "{" + base::IntToString(i->begin) + ", " +
132 base::IntToString(i->end) + "}";
134 position_string += "}\n";
135 return position_string;
138 // Return the positions in |matches| as a formatted string for unit test
139 // diagnostic output.
140 base::string16 MatchesAsString16(const ACMatches& matches) {
141 base::string16 matches_string;
142 for (ACMatches::const_iterator i = matches.begin(); i != matches.end(); ++i) {
143 matches_string.append(base::ASCIIToUTF16(" '"));
144 matches_string.append(i->description);
145 matches_string.append(base::ASCIIToUTF16("'\n"));
147 return matches_string;
150 // Comparison function for sorting search terms by descending length.
151 bool TestBookmarkPositionsEqual(const TestBookmarkPosition& pos_a,
152 const TestBookmarkPosition& pos_b) {
153 return pos_a.begin == pos_b.begin && pos_a.end == pos_b.end;
156 // Convience function to make comparing ACMatchClassifications against the
157 // test expectations structure easier.
158 TestBookmarkPositions PositionsFromAutocompleteMatch(
159 const AutocompleteMatch& match) {
160 TestBookmarkPositions positions;
161 bool started = false;
162 size_t start = 0;
163 for (AutocompleteMatch::ACMatchClassifications::const_iterator
164 i = match.description_class.begin();
165 i != match.description_class.end(); ++i) {
166 if (i->style & AutocompleteMatch::ACMatchClassification::MATCH) {
167 // We have found the start of a match.
168 EXPECT_FALSE(started);
169 started = true;
170 start = i->offset;
171 } else if (started) {
172 // We have found the end of a match.
173 started = false;
174 positions.push_back(TestBookmarkPosition(start, i->offset));
175 start = 0;
178 // Record the final position if the last match goes to the end of the
179 // candidate string.
180 if (started)
181 positions.push_back(TestBookmarkPosition(start, match.description.size()));
182 return positions;
185 // Convience function to make comparing test expectations structure against the
186 // actual ACMatchClassifications easier.
187 TestBookmarkPositions PositionsFromExpectations(
188 const size_t expectations[9][2]) {
189 TestBookmarkPositions positions;
190 size_t i = 0;
191 // The array is zero-terminated in the [1]th element.
192 while (expectations[i][1]) {
193 positions.push_back(
194 TestBookmarkPosition(expectations[i][0], expectations[i][1]));
195 ++i;
197 return positions;
200 TEST_F(BookmarkProviderTest, Positions) {
201 // Simulate searches.
202 // Description of |positions|:
203 // The first index represents the collection of positions for each expected
204 // match. The count of the actual subarrays in each instance of |query_data|
205 // must equal |match_count|. The second index represents each expected
206 // match position. The third index represents the |start| and |end| of the
207 // expected match's position within the |test_data|. This array must be
208 // terminated by an entry with a value of '0' for |end|.
209 // Example:
210 // Consider the line for 'def' below:
211 // {"def", 2, {{{4, 7}, {XXX, 0}}, {{2, 5}, {11, 14}, {XXX, 0}}}},
212 // There are two expected matches:
213 // 0. {{4, 7}, {XXX, 0}}
214 // 1. {{2, 5}, {11 ,14}, {XXX, 0}}
215 // For the first match, [0], there is one match within the bookmark's title
216 // expected, {4, 7}, which maps to the 'def' within "abc def". The 'XXX'
217 // value is ignored. The second match, [1], indicates that two matches are
218 // expected within the bookmark title "a definite definition". In each case,
219 // the {XXX, 0} indicates the end of the subarray. Or:
220 // Match #1 Match #2
221 // ------------------ ----------------------------
222 // Pos1 Term Pos1 Pos2 Term
223 // ------ -------- ------ -------- --------
224 // {"def", 2, {{{4, 7}, {999, 0}}, {{2, 5}, {11, 14}, {999, 0}}}},
226 struct QueryData {
227 const std::string query;
228 const size_t match_count; // This count must match the number of major
229 // elements in the following |positions| array.
230 const size_t positions[99][9][2];
231 } query_data[] = {
232 // This first set is primarily for position detection validation.
233 {"abc", 3, {{{0, 3}, {0, 0}},
234 {{0, 3}, {0, 0}},
235 {{0, 3}, {0, 0}}}},
236 {"abcde", 2, {{{0, 5}, {0, 0}},
237 {{0, 5}, {0, 0}}}},
238 {"foo bar", 0, {{{0, 0}}}},
239 {"fooey bark", 0, {{{0, 0}}}},
240 {"def", 2, {{{2, 5}, {0, 0}},
241 {{4, 7}, {0, 0}}}},
242 {"ghi jkl", 2, {{{0, 3}, {4, 7}, {0, 0}},
243 {{0, 3}, {4, 7}, {0, 0}}}},
244 // NB: GetBookmarksMatching(...) uses exact match for "a" in title or URL.
245 {"a", 2, {{{0, 1}, {0, 0}},
246 {{0, 0}}}},
247 {"a d", 0, {{{0, 0}}}},
248 {"carry carbon", 1, {{{0, 5}, {6, 12}, {0, 0}}}},
249 // NB: GetBookmarksMatching(...) sorts the match positions.
250 {"carbon carry", 1, {{{0, 5}, {6, 12}, {0, 0}}}},
251 {"arbon", 0, {{{0, 0}}}},
252 {"ar", 0, {{{0, 0}}}},
253 {"arry", 0, {{{0, 0}}}},
254 // Quoted terms are single terms.
255 {"\"carry carbon\"", 1, {{{0, 12}, {0, 0}}}},
256 {"\"carry carbon\" care", 1, {{{0, 12}, {13, 17}, {0, 0}}}},
257 // Quoted terms require complete word matches.
258 {"\"carry carbo\"", 0, {{{0, 0}}}},
259 // This set uses duplicated and/or overlaps search terms in the title.
260 {"frank", 1, {{{0, 5}, {8, 13}, {16, 21}, {0, 0}}}},
261 {"frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}},
262 {"frankly frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}},
263 {"foobar foo", 1, {{{0, 6}, {7, 13}, {0, 0}}}},
264 {"foo foobar", 1, {{{0, 6}, {7, 13}, {0, 0}}}},
265 // This ensures that leading whitespace in the title is removed.
266 {"hello", 1, {{{0, 5}, {7, 12}, {0, 0}}}},
267 // This ensures that empty titles yield empty classifications.
268 {"emptytitle", 1, {}},
271 for (size_t i = 0; i < arraysize(query_data); ++i) {
272 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query),
273 base::string16::npos, std::string(), GURL(),
274 metrics::OmniboxEventProto::INVALID_SPEC, false,
275 false, false, true, false,
276 ChromeAutocompleteSchemeClassifier(profile_.get()));
277 provider_->Start(input, false);
278 const ACMatches& matches(provider_->matches());
279 // Validate number of results is as expected.
280 EXPECT_LE(matches.size(), query_data[i].match_count)
281 << "One or more of the following matches were unexpected:\n"
282 << MatchesAsString16(matches)
283 << "For query '" << query_data[i].query << "'.";
284 EXPECT_GE(matches.size(), query_data[i].match_count)
285 << "One or more expected matches are missing. Matches found:\n"
286 << MatchesAsString16(matches)
287 << "for query '" << query_data[i].query << "'.";
288 // Validate positions within each match is as expected.
289 for (size_t j = 0; j < matches.size(); ++j) {
290 // Collect the expected positions as a vector, collect the match's
291 // classifications for match positions as a vector, then compare.
292 TestBookmarkPositions expected_positions(
293 PositionsFromExpectations(query_data[i].positions[j]));
294 TestBookmarkPositions actual_positions(
295 PositionsFromAutocompleteMatch(matches[j]));
296 EXPECT_TRUE(std::equal(expected_positions.begin(),
297 expected_positions.end(),
298 actual_positions.begin(),
299 TestBookmarkPositionsEqual))
300 << "EXPECTED: " << TestBookmarkPositionsAsString(expected_positions)
301 << "ACTUAL: " << TestBookmarkPositionsAsString(actual_positions)
302 << " for query: '" << query_data[i].query << "'.";
307 TEST_F(BookmarkProviderTest, Rankings) {
308 // Simulate searches.
309 struct QueryData {
310 const std::string query;
311 // |match_count| must match the number of elements in the following
312 // |matches| array.
313 const size_t match_count;
314 // |matches| specifies the titles for all bookmarks expected to be matched
315 // by the |query|
316 const std::string matches[3];
317 } query_data[] = {
318 // Basic ranking test.
319 {"abc", 3, {"abcde", // Most complete match.
320 "abcdef",
321 "abc def"}}, // Least complete match.
322 {"ghi", 2, {"ghi jkl", // Matched earlier.
323 "jkl ghi", // Matched later.
324 ""}},
325 // Rankings of exact-word matches with different URLs.
326 {"achlorhydric",
327 3, {"achlorhydric mockingbirds resuscitates featherhead",
328 "achlorhydric featherheads resuscitates mockingbirds",
329 "featherhead resuscitates achlorhydric mockingbirds"}},
330 {"achlorhydric featherheads",
331 2, {"achlorhydric featherheads resuscitates mockingbirds",
332 "mockingbirds resuscitates featherheads achlorhydric",
333 ""}},
334 {"mockingbirds resuscitates",
335 3, {"mockingbirds resuscitates featherheads achlorhydric",
336 "achlorhydric mockingbirds resuscitates featherhead",
337 "featherhead resuscitates achlorhydric mockingbirds"}},
338 // Ranking of exact-word matches with URL boosts.
339 {"worms", 2, {"burning worms #1", // boosted
340 "burning worms #2", // not boosted
341 ""}},
342 // Ranking of prefix matches with URL boost.
343 {"burn worm", 3, {"burning worms #1", // boosted
344 "worming burns #10", // boosted but longer title
345 "burning worms #2"}}, // not boosted
346 // A query of "worm burn" will have the same results.
347 {"worm burn", 3, {"burning worms #1", // boosted
348 "worming burns #10", // boosted but longer title
349 "burning worms #2"}}, // not boosted
352 for (size_t i = 0; i < arraysize(query_data); ++i) {
353 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query),
354 base::string16::npos, std::string(), GURL(),
355 metrics::OmniboxEventProto::INVALID_SPEC, false,
356 false, false, true, false,
357 ChromeAutocompleteSchemeClassifier(profile_.get()));
358 provider_->Start(input, false);
359 const ACMatches& matches(provider_->matches());
360 // Validate number and content of results is as expected.
361 for (size_t j = 0; j < std::max(query_data[i].match_count, matches.size());
362 ++j) {
363 EXPECT_LT(j, query_data[i].match_count) << " Unexpected match '"
364 << base::UTF16ToUTF8(matches[j].description) << "' for query: '"
365 << query_data[i].query << "'.";
366 if (j >= query_data[i].match_count)
367 continue;
368 EXPECT_LT(j, matches.size()) << " Missing match '"
369 << query_data[i].matches[j] << "' for query: '"
370 << query_data[i].query << "'.";
371 if (j >= matches.size())
372 continue;
373 EXPECT_EQ(query_data[i].matches[j],
374 base::UTF16ToUTF8(matches[j].description))
375 << " Mismatch at [" << base::IntToString(j) << "] for query '"
376 << query_data[i].query << "'.";
381 TEST_F(BookmarkProviderTest, InlineAutocompletion) {
382 // Simulate searches.
383 struct QueryData {
384 const std::string query;
385 const std::string url;
386 const bool allowed_to_be_default_match;
387 const std::string inline_autocompletion;
388 } query_data[] = {
389 { "bla", "http://blah.com/", true, "h.com" },
390 { "blah ", "http://blah.com/", false, ".com" },
391 { "http://bl", "http://blah.com/", true, "ah.com" },
392 { "fiddle.c", "http://fiddle.com/", true, "om" },
393 { "www", "http://www.www.com/", true, ".com" },
394 { "chro", "chrome://version", true, "me://version" },
395 { "chrome://ve", "chrome://version", true, "rsion" },
396 { "chrome ver", "chrome://version", false, "" },
397 { "versi", "chrome://version", false, "" },
398 { "abou", "chrome://omnibox", false, "" },
399 { "about:om", "chrome://omnibox", true, "nibox" }
400 // Note: when adding a new URL to this test, be sure to add it to the list
401 // of bookmarks at the top of the file as well. All items in this list
402 // need to be in the bookmarks list because BookmarkProvider's
403 // TitleMatchToACMatch() has an assertion that verifies the URL is
404 // actually bookmarked.
407 for (size_t i = 0; i < arraysize(query_data); ++i) {
408 const std::string description = "for query=" + query_data[i].query +
409 " and url=" + query_data[i].url;
410 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query),
411 base::string16::npos, std::string(), GURL(),
412 metrics::OmniboxEventProto::INVALID_SPEC, false,
413 false, false, true, false,
414 ChromeAutocompleteSchemeClassifier(profile_.get()));
415 const base::string16 fixed_up_input(
416 provider_->FixupUserInput(input).second);
417 BookmarkNode node(GURL(query_data[i].url));
418 node.SetTitle(base::ASCIIToUTF16(query_data[i].url));
419 BookmarkMatch bookmark_match;
420 bookmark_match.node = &node;
421 const AutocompleteMatch& ac_match = provider_->BookmarkMatchToACMatch(
422 input, fixed_up_input, bookmark_match);
423 EXPECT_EQ(query_data[i].allowed_to_be_default_match,
424 ac_match.allowed_to_be_default_match) << description;
425 EXPECT_EQ(base::ASCIIToUTF16(query_data[i].inline_autocompletion),
426 ac_match.inline_autocompletion) << description;
430 TEST_F(BookmarkProviderTest, StripHttpAndAdjustOffsets) {
431 // Simulate searches.
432 struct QueryData {
433 const std::string query;
434 const std::string expected_contents;
435 // |expected_contents_class| is in format offset:style,offset:style,...
436 const std::string expected_contents_class;
437 } query_data[] = {
438 { "foo", "www.foobar.com", "0:1,4:3,7:1" },
439 { "www foo", "www.foobar.com", "0:3,3:1,4:3,7:1" },
440 { "foo www", "www.foobar.com", "0:3,3:1,4:3,7:1" },
441 { "foo http", "http://www.foobar.com", "0:3,4:1,11:3,14:1" },
442 { "blah", "blah.com", "0:3,4:1" },
443 { "http blah", "http://blah.com", "0:3,4:1,7:3,11:1" },
444 { "dom", "www.domain.com/http/", "0:1,4:3,7:1" },
445 { "dom http", "http://www.domain.com/http/",
446 "0:3,4:1,11:3,14:1,22:3,26:1" },
447 { "rep", "www.repeat.com/1/repeat/2/", "0:1,4:3,7:1,17:3,20:1" },
448 { "versi", "chrome://version", "0:1,9:3,14:1" }
451 for (size_t i = 0; i < arraysize(query_data); ++i) {
452 std::string description = "for query=" + query_data[i].query;
453 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query),
454 base::string16::npos, std::string(), GURL(),
455 metrics::OmniboxEventProto::INVALID_SPEC, false,
456 false, false, true, false,
457 ChromeAutocompleteSchemeClassifier(profile_.get()));
458 provider_->Start(input, false);
459 const ACMatches& matches(provider_->matches());
460 ASSERT_EQ(1U, matches.size()) << description;
461 const AutocompleteMatch& match = matches[0];
462 EXPECT_EQ(base::ASCIIToUTF16(query_data[i].expected_contents),
463 match.contents) << description;
464 std::vector<std::string> class_strings = base::SplitString(
465 query_data[i].expected_contents_class, ",",
466 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
467 ASSERT_EQ(class_strings.size(), match.contents_class.size())
468 << description;
469 for (size_t i = 0; i < class_strings.size(); ++i) {
470 std::vector<std::string> chunks = base::SplitString(
471 class_strings[i], ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
472 ASSERT_EQ(2U, chunks.size()) << description;
473 size_t offset;
474 EXPECT_TRUE(base::StringToSizeT(chunks[0], &offset)) << description;
475 EXPECT_EQ(offset, match.contents_class[i].offset) << description;
476 int style;
477 EXPECT_TRUE(base::StringToInt(chunks[1], &style)) << description;
478 EXPECT_EQ(style, match.contents_class[i].style) << description;
483 TEST_F(BookmarkProviderTest, DoesNotProvideMatchesOnFocus) {
484 AutocompleteInput input(
485 base::ASCIIToUTF16("foo"), base::string16::npos, std::string(), GURL(),
486 metrics::OmniboxEventProto::INVALID_SPEC, false, false, false, true, true,
487 ChromeAutocompleteSchemeClassifier(profile_.get()));
488 provider_->Start(input, false);
489 EXPECT_TRUE(provider_->matches().empty());