Put screenshot.py back to work
[chromium-blink-merge.git] / components / bookmarks / browser / bookmark_index_unittest.cc
blob4913e7c9304b86a17c49cad22a36d1e06d0f3b40
1 // Copyright 2014 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/bookmarks/browser/bookmark_index.h"
7 #include <string>
8 #include <vector>
10 #include "base/macros.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "components/bookmarks/browser/bookmark_match.h"
16 #include "components/bookmarks/browser/bookmark_model.h"
17 #include "components/bookmarks/test/bookmark_test_helpers.h"
18 #include "components/bookmarks/test/test_bookmark_client.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using base::ASCIIToUTF16;
22 using base::UTF8ToUTF16;
24 namespace bookmarks {
25 namespace {
27 const char kAboutBlankURL[] = "about:blank";
29 class BookmarkClientMock : public TestBookmarkClient {
30 public:
31 BookmarkClientMock(const std::map<GURL, int>& typed_count_map)
32 : typed_count_map_(typed_count_map) {}
34 bool SupportsTypedCountForNodes() override { return true; }
36 void GetTypedCountForNodes(
37 const NodeSet& nodes,
38 NodeTypedCountPairs* node_typed_count_pairs) override {
39 for (NodeSet::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
40 const BookmarkNode* node = *it;
41 std::map<GURL, int>::const_iterator found =
42 typed_count_map_.find(node->url());
43 if (found == typed_count_map_.end())
44 continue;
46 node_typed_count_pairs->push_back(std::make_pair(node, found->second));
50 private:
51 const std::map<GURL, int> typed_count_map_;
53 DISALLOW_COPY_AND_ASSIGN(BookmarkClientMock);
56 class BookmarkIndexTest : public testing::Test {
57 public:
58 BookmarkIndexTest() : model_(client_.CreateModel()) {}
60 typedef std::pair<std::string, std::string> TitleAndURL;
62 void AddBookmarks(const char** titles, const char** urls, size_t count) {
63 // The pair is (title, url).
64 std::vector<TitleAndURL> bookmarks;
65 for (size_t i = 0; i < count; ++i) {
66 TitleAndURL bookmark(titles[i], urls[i]);
67 bookmarks.push_back(bookmark);
69 AddBookmarks(bookmarks);
72 void AddBookmarks(const std::vector<TitleAndURL>& bookmarks) {
73 for (size_t i = 0; i < bookmarks.size(); ++i) {
74 model_->AddURL(model_->other_node(), static_cast<int>(i),
75 ASCIIToUTF16(bookmarks[i].first),
76 GURL(bookmarks[i].second));
80 void ExpectMatches(const std::string& query,
81 const char** expected_titles,
82 size_t expected_count) {
83 std::vector<std::string> title_vector;
84 for (size_t i = 0; i < expected_count; ++i)
85 title_vector.push_back(expected_titles[i]);
86 ExpectMatches(query, query_parser::MatchingAlgorithm::DEFAULT,
87 title_vector);
90 void ExpectMatches(const std::string& query,
91 query_parser::MatchingAlgorithm matching_algorithm,
92 const std::vector<std::string>& expected_titles) {
93 std::vector<BookmarkMatch> matches;
94 model_->GetBookmarksMatching(ASCIIToUTF16(query), 1000, matching_algorithm,
95 &matches);
96 ASSERT_EQ(expected_titles.size(), matches.size());
97 for (size_t i = 0; i < expected_titles.size(); ++i) {
98 bool found = false;
99 for (size_t j = 0; j < matches.size(); ++j) {
100 if (ASCIIToUTF16(expected_titles[i]) == matches[j].node->GetTitle()) {
101 matches.erase(matches.begin() + j);
102 found = true;
103 break;
106 ASSERT_TRUE(found);
110 void ExtractMatchPositions(const std::string& string,
111 BookmarkMatch::MatchPositions* matches) {
112 std::vector<std::string> match_strings;
113 base::SplitString(string, ':', &match_strings);
114 for (size_t i = 0; i < match_strings.size(); ++i) {
115 std::vector<std::string> chunks;
116 base::SplitString(match_strings[i], ',', &chunks);
117 ASSERT_EQ(2U, chunks.size());
118 matches->push_back(BookmarkMatch::MatchPosition());
119 int chunks0, chunks1;
120 EXPECT_TRUE(base::StringToInt(chunks[0], &chunks0));
121 EXPECT_TRUE(base::StringToInt(chunks[1], &chunks1));
122 matches->back().first = chunks0;
123 matches->back().second = chunks1;
127 void ExpectMatchPositions(
128 const BookmarkMatch::MatchPositions& actual_positions,
129 const BookmarkMatch::MatchPositions& expected_positions) {
130 ASSERT_EQ(expected_positions.size(), actual_positions.size());
131 for (size_t i = 0; i < expected_positions.size(); ++i) {
132 EXPECT_EQ(expected_positions[i].first, actual_positions[i].first);
133 EXPECT_EQ(expected_positions[i].second, actual_positions[i].second);
137 protected:
138 TestBookmarkClient client_;
139 scoped_ptr<BookmarkModel> model_;
141 private:
142 DISALLOW_COPY_AND_ASSIGN(BookmarkIndexTest);
145 // Various permutations with differing input, queries and output that exercises
146 // all query paths.
147 TEST_F(BookmarkIndexTest, GetBookmarksMatching) {
148 struct TestData {
149 const std::string titles;
150 const std::string query;
151 const std::string expected;
152 } data[] = {
153 // Trivial test case of only one term, exact match.
154 { "a;b", "A", "a" },
156 // Prefix match, one term.
157 { "abcd;abc;b", "abc", "abcd;abc" },
159 // Prefix match, multiple terms.
160 { "abcd cdef;abcd;abcd cdefg", "abc cde", "abcd cdef;abcd cdefg"},
162 // Exact and prefix match.
163 { "ab cdef;abcd;abcd cdefg", "ab cdef", "ab cdef"},
165 // Exact and prefix match.
166 { "ab cdef ghij;ab;cde;cdef;ghi;cdef ab;ghij ab",
167 "ab cde ghi",
168 "ab cdef ghij"},
170 // Title with term multiple times.
171 { "ab ab", "ab", "ab ab"},
173 // Make sure quotes don't do a prefix match.
174 { "think", "\"thi\"", ""},
176 // Prefix matches against multiple candidates.
177 { "abc1 abc2 abc3 abc4", "abc", "abc1 abc2 abc3 abc4"},
179 // Prefix match on the first term.
180 { "abc", "a", "" },
182 // Prefix match on subsequent terms.
183 { "abc def", "abc d", "" },
187 for (size_t i = 0; i < arraysize(data); ++i) {
188 std::vector<std::string> titles;
189 base::SplitString(data[i].titles, ';', &titles);
190 std::vector<TitleAndURL> bookmarks;
191 for (size_t j = 0; j < titles.size(); ++j) {
192 TitleAndURL bookmark(titles[j], kAboutBlankURL);
193 bookmarks.push_back(bookmark);
195 AddBookmarks(bookmarks);
197 std::vector<std::string> expected;
198 if (!data[i].expected.empty())
199 base::SplitString(data[i].expected, ';', &expected);
201 ExpectMatches(data[i].query, query_parser::MatchingAlgorithm::DEFAULT,
202 expected);
204 model_ = client_.CreateModel();
208 TEST_F(BookmarkIndexTest, GetBookmarksMatchingAlwaysPrefixSearch) {
209 struct TestData {
210 const std::string titles;
211 const std::string query;
212 const std::string expected;
213 } data[] = {
214 // Trivial test case of only one term, exact match.
215 { "z;y", "Z", "z" },
217 // Prefix match, one term.
218 { "abcd;abc;b", "abc", "abcd;abc" },
220 // Prefix match, multiple terms.
221 { "abcd cdef;abcd;abcd cdefg", "abc cde", "abcd cdef;abcd cdefg" },
223 // Exact and prefix match.
224 { "ab cdef ghij;ab;cde;cdef;ghi;cdef ab;ghij ab",
225 "ab cde ghi",
226 "ab cdef ghij" },
228 // Title with term multiple times.
229 { "ab ab", "ab", "ab ab" },
231 // Make sure quotes don't do a prefix match.
232 { "think", "\"thi\"", "" },
234 // Prefix matches against multiple candidates.
235 { "abc1 abc2 abc3 abc4", "abc", "abc1 abc2 abc3 abc4" },
237 // Prefix match on the first term.
238 { "abc", "a", "abc" },
240 // Prefix match on subsequent terms.
241 { "abc def", "abc d", "abc def" },
243 // Exact and prefix match.
244 { "ab cdef;abcd;abcd cdefg", "ab cdef", "ab cdef;abcd cdefg" },
246 for (size_t i = 0; i < arraysize(data); ++i) {
247 std::vector<std::string> titles;
248 base::SplitString(data[i].titles, ';', &titles);
249 std::vector<TitleAndURL> bookmarks;
250 for (size_t j = 0; j < titles.size(); ++j) {
251 TitleAndURL bookmark(titles[j], kAboutBlankURL);
252 bookmarks.push_back(bookmark);
254 AddBookmarks(bookmarks);
256 std::vector<std::string> expected;
257 if (!data[i].expected.empty())
258 base::SplitString(data[i].expected, ';', &expected);
260 ExpectMatches(data[i].query,
261 query_parser::MatchingAlgorithm::ALWAYS_PREFIX_SEARCH,
262 expected);
264 model_ = client_.CreateModel();
268 // Analogous to GetBookmarksMatching, this test tests various permutations
269 // of title, URL, and input to see if the title/URL matches the input as
270 // expected.
271 TEST_F(BookmarkIndexTest, GetBookmarksMatchingWithURLs) {
272 struct TestData {
273 const std::string query;
274 const std::string title;
275 const std::string url;
276 const bool should_be_retrieved;
277 } data[] = {
278 // Test single-word inputs. Include both exact matches and prefix matches.
279 { "foo", "Foo", "http://www.bar.com/", true },
280 { "foo", "Foodie", "http://www.bar.com/", true },
281 { "foo", "Bar", "http://www.foo.com/", true },
282 { "foo", "Bar", "http://www.foodie.com/", true },
283 { "foo", "Foo", "http://www.foo.com/", true },
284 { "foo", "Bar", "http://www.bar.com/", false },
285 { "foo", "Bar", "http://www.bar.com/blah/foo/blah-again/ ", true },
286 { "foo", "Bar", "http://www.bar.com/blah/foodie/blah-again/ ", true },
287 { "foo", "Bar", "http://www.bar.com/blah-foo/blah-again/ ", true },
288 { "foo", "Bar", "http://www.bar.com/blah-foodie/blah-again/ ", true },
289 { "foo", "Bar", "http://www.bar.com/blahafoo/blah-again/ ", false },
291 // Test multi-word inputs.
292 { "foo bar", "Foo Bar", "http://baz.com/", true },
293 { "foo bar", "Foodie Bar", "http://baz.com/", true },
294 { "bar foo", "Foo Bar", "http://baz.com/", true },
295 { "bar foo", "Foodie Barly", "http://baz.com/", true },
296 { "foo bar", "Foo Baz", "http://baz.com/", false },
297 { "foo bar", "Foo Baz", "http://bar.com/", true },
298 { "foo bar", "Foo Baz", "http://barly.com/", true },
299 { "foo bar", "Foodie Baz", "http://barly.com/", true },
300 { "bar foo", "Foo Baz", "http://bar.com/", true },
301 { "bar foo", "Foo Baz", "http://barly.com/", true },
302 { "foo bar", "Baz Bar", "http://blah.com/foo", true },
303 { "foo bar", "Baz Barly", "http://blah.com/foodie", true },
304 { "foo bar", "Baz Bur", "http://blah.com/foo/bar", true },
305 { "foo bar", "Baz Bur", "http://blah.com/food/barly", true },
306 { "foo bar", "Baz Bur", "http://bar.com/blah/foo", true },
307 { "foo bar", "Baz Bur", "http://barly.com/blah/food", true },
308 { "foo bar", "Baz Bur", "http://bar.com/blah/flub", false },
309 { "foo bar", "Baz Bur", "http://foo.com/blah/flub", false }
312 for (size_t i = 0; i < arraysize(data); ++i) {
313 model_ = client_.CreateModel();
314 std::vector<TitleAndURL> bookmarks;
315 bookmarks.push_back(TitleAndURL(data[i].title, data[i].url));
316 AddBookmarks(bookmarks);
318 std::vector<std::string> expected;
319 if (data[i].should_be_retrieved)
320 expected.push_back(data[i].title);
322 ExpectMatches(data[i].query, query_parser::MatchingAlgorithm::DEFAULT,
323 expected);
327 TEST_F(BookmarkIndexTest, Normalization) {
328 struct TestData {
329 const char* const title;
330 const char* const query;
331 } data[] = {
332 { "fooa\xcc\x88-test", "foo\xc3\xa4-test" },
333 { "fooa\xcc\x88-test", "fooa\xcc\x88-test" },
334 { "fooa\xcc\x88-test", "foo\xc3\xa4" },
335 { "fooa\xcc\x88-test", "fooa\xcc\x88" },
336 { "fooa\xcc\x88-test", "foo" },
337 { "foo\xc3\xa4-test", "foo\xc3\xa4-test" },
338 { "foo\xc3\xa4-test", "fooa\xcc\x88-test" },
339 { "foo\xc3\xa4-test", "foo\xc3\xa4" },
340 { "foo\xc3\xa4-test", "fooa\xcc\x88" },
341 { "foo\xc3\xa4-test", "foo" },
342 { "foo", "foo" }
345 GURL url(kAboutBlankURL);
346 for (size_t i = 0; i < arraysize(data); ++i) {
347 model_->AddURL(model_->other_node(), 0, UTF8ToUTF16(data[i].title), url);
348 std::vector<BookmarkMatch> matches;
349 model_->GetBookmarksMatching(UTF8ToUTF16(data[i].query), 10, &matches);
350 EXPECT_EQ(1u, matches.size());
351 model_ = client_.CreateModel();
355 // Makes sure match positions are updated appropriately for title matches.
356 TEST_F(BookmarkIndexTest, MatchPositionsTitles) {
357 struct TestData {
358 const std::string title;
359 const std::string query;
360 const std::string expected_title_match_positions;
361 } data[] = {
362 // Trivial test case of only one term, exact match.
363 { "a", "A", "0,1" },
364 { "foo bar", "bar", "4,7" },
365 { "fooey bark", "bar foo", "0,3:6,9" },
366 // Non-trivial tests.
367 { "foobar foo", "foobar foo", "0,6:7,10" },
368 { "foobar foo", "foo foobar", "0,6:7,10" },
369 { "foobar foobar", "foobar foo", "0,6:7,13" },
370 { "foobar foobar", "foo foobar", "0,6:7,13" },
372 for (size_t i = 0; i < arraysize(data); ++i) {
373 std::vector<TitleAndURL> bookmarks;
374 TitleAndURL bookmark(data[i].title, kAboutBlankURL);
375 bookmarks.push_back(bookmark);
376 AddBookmarks(bookmarks);
378 std::vector<BookmarkMatch> matches;
379 model_->GetBookmarksMatching(ASCIIToUTF16(data[i].query), 1000, &matches);
380 ASSERT_EQ(1U, matches.size());
382 BookmarkMatch::MatchPositions expected_title_matches;
383 ExtractMatchPositions(data[i].expected_title_match_positions,
384 &expected_title_matches);
385 ExpectMatchPositions(matches[0].title_match_positions,
386 expected_title_matches);
388 model_ = client_.CreateModel();
392 // Makes sure match positions are updated appropriately for URL matches.
393 TEST_F(BookmarkIndexTest, MatchPositionsURLs) {
394 // The encoded stuff between /wiki/ and the # is 第二次世界大戦
395 const std::string ja_wiki_url = "http://ja.wikipedia.org/wiki/%E7%AC%AC%E4"
396 "%BA%8C%E6%AC%A1%E4%B8%96%E7%95%8C%E5%A4%A7%E6%88%A6#.E3.83.B4.E3.82.A7"
397 ".E3.83.AB.E3.82.B5.E3.82.A4.E3.83.A6.E4.BD.93.E5.88.B6";
398 struct TestData {
399 const std::string query;
400 const std::string url;
401 const std::string expected_url_match_positions;
402 } data[] = {
403 { "foo", "http://www.foo.com/", "11,14" },
404 { "foo", "http://www.foodie.com/", "11,14" },
405 { "foo", "http://www.foofoo.com/", "11,14" },
406 { "www", "http://www.foo.com/", "7,10" },
407 { "foo", "http://www.foodie.com/blah/foo/fi", "11,14:27,30" },
408 { "foo", "http://www.blah.com/blah/foo/fi", "25,28" },
409 { "foo www", "http://www.foodie.com/blah/foo/fi", "7,10:11,14:27,30" },
410 { "www foo", "http://www.foodie.com/blah/foo/fi", "7,10:11,14:27,30" },
411 { "www bla", "http://www.foodie.com/blah/foo/fi", "7,10:22,25" },
412 { "http", "http://www.foo.com/", "0,4" },
413 { "http www", "http://www.foo.com/", "0,4:7,10" },
414 { "http foo", "http://www.foo.com/", "0,4:11,14" },
415 { "http foo", "http://www.bar.com/baz/foodie/hi", "0,4:23,26" },
416 { "第二次", ja_wiki_url, "29,56" },
417 { "ja 第二次", ja_wiki_url, "7,9:29,56" },
418 { "第二次 E3.8", ja_wiki_url, "29,56:94,98:103,107:"
419 "112,116:121,125:"
420 "130,134:139,143" }
423 for (size_t i = 0; i < arraysize(data); ++i) {
424 model_ = client_.CreateModel();
425 std::vector<TitleAndURL> bookmarks;
426 TitleAndURL bookmark("123456", data[i].url);
427 bookmarks.push_back(bookmark);
428 AddBookmarks(bookmarks);
430 std::vector<BookmarkMatch> matches;
431 model_->GetBookmarksMatching(UTF8ToUTF16(data[i].query), 1000, &matches);
432 ASSERT_EQ(1U, matches.size()) << data[i].url << data[i].query;
434 BookmarkMatch::MatchPositions expected_url_matches;
435 ExtractMatchPositions(data[i].expected_url_match_positions,
436 &expected_url_matches);
437 ExpectMatchPositions(matches[0].url_match_positions, expected_url_matches);
441 // Makes sure index is updated when a node is removed.
442 TEST_F(BookmarkIndexTest, Remove) {
443 const char* titles[] = { "a", "b" };
444 const char* urls[] = {kAboutBlankURL, kAboutBlankURL};
445 AddBookmarks(titles, urls, arraysize(titles));
447 // Remove the node and make sure we don't get back any results.
448 model_->Remove(model_->other_node(), 0);
449 ExpectMatches("A", NULL, 0U);
452 // Makes sure index is updated when a node's title is changed.
453 TEST_F(BookmarkIndexTest, ChangeTitle) {
454 const char* titles[] = { "a", "b" };
455 const char* urls[] = {kAboutBlankURL, kAboutBlankURL};
456 AddBookmarks(titles, urls, arraysize(titles));
458 // Remove the node and make sure we don't get back any results.
459 const char* expected[] = { "blah" };
460 model_->SetTitle(model_->other_node()->GetChild(0), ASCIIToUTF16("blah"));
461 ExpectMatches("BlAh", expected, arraysize(expected));
464 // Makes sure index is updated when a node's URL is changed.
465 TEST_F(BookmarkIndexTest, ChangeURL) {
466 const char* titles[] = { "a", "b" };
467 const char* urls[] = {"http://fizz",
468 "http://fuzz"};
469 AddBookmarks(titles, urls, arraysize(titles));
471 const char* expected[] = { "a" };
472 model_->SetURL(model_->other_node()->GetChild(0), GURL("http://blah"));
473 ExpectMatches("blah", expected, arraysize(expected));
476 // Makes sure no more than max queries is returned.
477 TEST_F(BookmarkIndexTest, HonorMax) {
478 const char* titles[] = { "abcd", "abcde" };
479 const char* urls[] = {kAboutBlankURL, kAboutBlankURL};
480 AddBookmarks(titles, urls, arraysize(titles));
482 std::vector<BookmarkMatch> matches;
483 model_->GetBookmarksMatching(ASCIIToUTF16("ABc"), 1, &matches);
484 EXPECT_EQ(1U, matches.size());
487 // Makes sure if the lower case string of a bookmark title is more characters
488 // than the upper case string no match positions are returned.
489 TEST_F(BookmarkIndexTest, EmptyMatchOnMultiwideLowercaseString) {
490 const BookmarkNode* n1 = model_->AddURL(model_->other_node(), 0,
491 base::WideToUTF16(L"\u0130 i"),
492 GURL("http://www.google.com"));
494 std::vector<BookmarkMatch> matches;
495 model_->GetBookmarksMatching(ASCIIToUTF16("i"), 100, &matches);
496 ASSERT_EQ(1U, matches.size());
497 EXPECT_EQ(n1, matches[0].node);
498 EXPECT_TRUE(matches[0].title_match_positions.empty());
501 TEST_F(BookmarkIndexTest, GetResultsSortedByTypedCount) {
502 struct TestData {
503 const GURL url;
504 const char* title;
505 const int typed_count;
506 } data[] = {
507 { GURL("http://www.google.com/"), "Google", 100 },
508 { GURL("http://maps.google.com/"), "Google Maps", 40 },
509 { GURL("http://docs.google.com/"), "Google Docs", 50 },
510 { GURL("http://reader.google.com/"), "Google Reader", 80 },
513 std::map<GURL, int> typed_count_map;
514 for (size_t i = 0; i < arraysize(data); ++i)
515 typed_count_map.insert(std::make_pair(data[i].url, data[i].typed_count));
517 BookmarkClientMock client(typed_count_map);
518 scoped_ptr<BookmarkModel> model = client.CreateModel();
520 for (size_t i = 0; i < arraysize(data); ++i)
521 // Populate the BookmarkIndex.
522 model->AddURL(
523 model->other_node(), i, UTF8ToUTF16(data[i].title), data[i].url);
525 // Populate match nodes.
526 std::vector<BookmarkMatch> matches;
527 model->GetBookmarksMatching(ASCIIToUTF16("google"), 4, &matches);
529 // The resulting order should be:
530 // 1. Google (google.com) 100
531 // 2. Google Reader (google.com/reader) 80
532 // 3. Google Docs (docs.google.com) 50
533 // 4. Google Maps (maps.google.com) 40
534 ASSERT_EQ(4, static_cast<int>(matches.size()));
535 EXPECT_EQ(data[0].url, matches[0].node->url());
536 EXPECT_EQ(data[3].url, matches[1].node->url());
537 EXPECT_EQ(data[2].url, matches[2].node->url());
538 EXPECT_EQ(data[1].url, matches[3].node->url());
540 matches.clear();
541 // Select top two matches.
542 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches);
544 ASSERT_EQ(2, static_cast<int>(matches.size()));
545 EXPECT_EQ(data[0].url, matches[0].node->url());
546 EXPECT_EQ(data[3].url, matches[1].node->url());
549 } // namespace
550 } // namespace bookmarks