| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/bookmarks/browser/bookmark_index.h" | 5 #include "components/bookmarks/browser/bookmark_index.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 | 166 |
| 167 // Title with term multiple times. | 167 // Title with term multiple times. |
| 168 { "ab ab", "ab", "ab ab"}, | 168 { "ab ab", "ab", "ab ab"}, |
| 169 | 169 |
| 170 // Make sure quotes don't do a prefix match. | 170 // Make sure quotes don't do a prefix match. |
| 171 { "think", "\"thi\"", ""}, | 171 { "think", "\"thi\"", ""}, |
| 172 | 172 |
| 173 // Prefix matches against multiple candidates. | 173 // Prefix matches against multiple candidates. |
| 174 { "abc1 abc2 abc3 abc4", "abc", "abc1 abc2 abc3 abc4"}, | 174 { "abc1 abc2 abc3 abc4", "abc", "abc1 abc2 abc3 abc4"}, |
| 175 }; | 175 }; |
| 176 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 176 for (size_t i = 0; i < arraysize(data); ++i) { |
| 177 std::vector<std::string> titles; | 177 std::vector<std::string> titles; |
| 178 base::SplitString(data[i].titles, ';', &titles); | 178 base::SplitString(data[i].titles, ';', &titles); |
| 179 std::vector<TitleAndURL> bookmarks; | 179 std::vector<TitleAndURL> bookmarks; |
| 180 for (size_t j = 0; j < titles.size(); ++j) { | 180 for (size_t j = 0; j < titles.size(); ++j) { |
| 181 TitleAndURL bookmark(titles[j], kAboutBlankURL); | 181 TitleAndURL bookmark(titles[j], kAboutBlankURL); |
| 182 bookmarks.push_back(bookmark); | 182 bookmarks.push_back(bookmark); |
| 183 } | 183 } |
| 184 AddBookmarks(bookmarks); | 184 AddBookmarks(bookmarks); |
| 185 | 185 |
| 186 std::vector<std::string> expected; | 186 std::vector<std::string> expected; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 { "foo bar", "Baz Bar", "http://blah.com/foo", true }, | 230 { "foo bar", "Baz Bar", "http://blah.com/foo", true }, |
| 231 { "foo bar", "Baz Barly", "http://blah.com/foodie", true }, | 231 { "foo bar", "Baz Barly", "http://blah.com/foodie", true }, |
| 232 { "foo bar", "Baz Bur", "http://blah.com/foo/bar", true }, | 232 { "foo bar", "Baz Bur", "http://blah.com/foo/bar", true }, |
| 233 { "foo bar", "Baz Bur", "http://blah.com/food/barly", true }, | 233 { "foo bar", "Baz Bur", "http://blah.com/food/barly", true }, |
| 234 { "foo bar", "Baz Bur", "http://bar.com/blah/foo", true }, | 234 { "foo bar", "Baz Bur", "http://bar.com/blah/foo", true }, |
| 235 { "foo bar", "Baz Bur", "http://barly.com/blah/food", true }, | 235 { "foo bar", "Baz Bur", "http://barly.com/blah/food", true }, |
| 236 { "foo bar", "Baz Bur", "http://bar.com/blah/flub", false }, | 236 { "foo bar", "Baz Bur", "http://bar.com/blah/flub", false }, |
| 237 { "foo bar", "Baz Bur", "http://foo.com/blah/flub", false } | 237 { "foo bar", "Baz Bur", "http://foo.com/blah/flub", false } |
| 238 }; | 238 }; |
| 239 | 239 |
| 240 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 240 for (size_t i = 0; i < arraysize(data); ++i) { |
| 241 model_ = client_.CreateModel(); | 241 model_ = client_.CreateModel(); |
| 242 std::vector<TitleAndURL> bookmarks; | 242 std::vector<TitleAndURL> bookmarks; |
| 243 bookmarks.push_back(TitleAndURL(data[i].title, data[i].url)); | 243 bookmarks.push_back(TitleAndURL(data[i].title, data[i].url)); |
| 244 AddBookmarks(bookmarks); | 244 AddBookmarks(bookmarks); |
| 245 | 245 |
| 246 std::vector<std::string> expected; | 246 std::vector<std::string> expected; |
| 247 if (data[i].should_be_retrieved) | 247 if (data[i].should_be_retrieved) |
| 248 expected.push_back(data[i].title); | 248 expected.push_back(data[i].title); |
| 249 | 249 |
| 250 ExpectMatches(data[i].query, expected); | 250 ExpectMatches(data[i].query, expected); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 263 { "fooa\xcc\x88-test", "foo" }, | 263 { "fooa\xcc\x88-test", "foo" }, |
| 264 { "foo\xc3\xa4-test", "foo\xc3\xa4-test" }, | 264 { "foo\xc3\xa4-test", "foo\xc3\xa4-test" }, |
| 265 { "foo\xc3\xa4-test", "fooa\xcc\x88-test" }, | 265 { "foo\xc3\xa4-test", "fooa\xcc\x88-test" }, |
| 266 { "foo\xc3\xa4-test", "foo\xc3\xa4" }, | 266 { "foo\xc3\xa4-test", "foo\xc3\xa4" }, |
| 267 { "foo\xc3\xa4-test", "fooa\xcc\x88" }, | 267 { "foo\xc3\xa4-test", "fooa\xcc\x88" }, |
| 268 { "foo\xc3\xa4-test", "foo" }, | 268 { "foo\xc3\xa4-test", "foo" }, |
| 269 { "foo", "foo" } | 269 { "foo", "foo" } |
| 270 }; | 270 }; |
| 271 | 271 |
| 272 GURL url(kAboutBlankURL); | 272 GURL url(kAboutBlankURL); |
| 273 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 273 for (size_t i = 0; i < arraysize(data); ++i) { |
| 274 model_->AddURL(model_->other_node(), 0, UTF8ToUTF16(data[i].title), url); | 274 model_->AddURL(model_->other_node(), 0, UTF8ToUTF16(data[i].title), url); |
| 275 std::vector<BookmarkMatch> matches; | 275 std::vector<BookmarkMatch> matches; |
| 276 model_->GetBookmarksMatching(UTF8ToUTF16(data[i].query), 10, &matches); | 276 model_->GetBookmarksMatching(UTF8ToUTF16(data[i].query), 10, &matches); |
| 277 EXPECT_EQ(1u, matches.size()); | 277 EXPECT_EQ(1u, matches.size()); |
| 278 model_ = client_.CreateModel(); | 278 model_ = client_.CreateModel(); |
| 279 } | 279 } |
| 280 } | 280 } |
| 281 | 281 |
| 282 // Makes sure match positions are updated appropriately for title matches. | 282 // Makes sure match positions are updated appropriately for title matches. |
| 283 TEST_F(BookmarkIndexTest, MatchPositionsTitles) { | 283 TEST_F(BookmarkIndexTest, MatchPositionsTitles) { |
| 284 struct TestData { | 284 struct TestData { |
| 285 const std::string title; | 285 const std::string title; |
| 286 const std::string query; | 286 const std::string query; |
| 287 const std::string expected_title_match_positions; | 287 const std::string expected_title_match_positions; |
| 288 } data[] = { | 288 } data[] = { |
| 289 // Trivial test case of only one term, exact match. | 289 // Trivial test case of only one term, exact match. |
| 290 { "a", "A", "0,1" }, | 290 { "a", "A", "0,1" }, |
| 291 { "foo bar", "bar", "4,7" }, | 291 { "foo bar", "bar", "4,7" }, |
| 292 { "fooey bark", "bar foo", "0,3:6,9" }, | 292 { "fooey bark", "bar foo", "0,3:6,9" }, |
| 293 // Non-trivial tests. | 293 // Non-trivial tests. |
| 294 { "foobar foo", "foobar foo", "0,6:7,10" }, | 294 { "foobar foo", "foobar foo", "0,6:7,10" }, |
| 295 { "foobar foo", "foo foobar", "0,6:7,10" }, | 295 { "foobar foo", "foo foobar", "0,6:7,10" }, |
| 296 { "foobar foobar", "foobar foo", "0,6:7,13" }, | 296 { "foobar foobar", "foobar foo", "0,6:7,13" }, |
| 297 { "foobar foobar", "foo foobar", "0,6:7,13" }, | 297 { "foobar foobar", "foo foobar", "0,6:7,13" }, |
| 298 }; | 298 }; |
| 299 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 299 for (size_t i = 0; i < arraysize(data); ++i) { |
| 300 std::vector<TitleAndURL> bookmarks; | 300 std::vector<TitleAndURL> bookmarks; |
| 301 TitleAndURL bookmark(data[i].title, kAboutBlankURL); | 301 TitleAndURL bookmark(data[i].title, kAboutBlankURL); |
| 302 bookmarks.push_back(bookmark); | 302 bookmarks.push_back(bookmark); |
| 303 AddBookmarks(bookmarks); | 303 AddBookmarks(bookmarks); |
| 304 | 304 |
| 305 std::vector<BookmarkMatch> matches; | 305 std::vector<BookmarkMatch> matches; |
| 306 model_->GetBookmarksMatching(ASCIIToUTF16(data[i].query), 1000, &matches); | 306 model_->GetBookmarksMatching(ASCIIToUTF16(data[i].query), 1000, &matches); |
| 307 ASSERT_EQ(1U, matches.size()); | 307 ASSERT_EQ(1U, matches.size()); |
| 308 | 308 |
| 309 BookmarkMatch::MatchPositions expected_title_matches; | 309 BookmarkMatch::MatchPositions expected_title_matches; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 340 { "http www", "http://www.foo.com/", "0,4:7,10" }, | 340 { "http www", "http://www.foo.com/", "0,4:7,10" }, |
| 341 { "http foo", "http://www.foo.com/", "0,4:11,14" }, | 341 { "http foo", "http://www.foo.com/", "0,4:11,14" }, |
| 342 { "http foo", "http://www.bar.com/baz/foodie/hi", "0,4:23,26" }, | 342 { "http foo", "http://www.bar.com/baz/foodie/hi", "0,4:23,26" }, |
| 343 { "第二次", ja_wiki_url, "29,56" }, | 343 { "第二次", ja_wiki_url, "29,56" }, |
| 344 { "ja 第二次", ja_wiki_url, "7,9:29,56" }, | 344 { "ja 第二次", ja_wiki_url, "7,9:29,56" }, |
| 345 { "第二次 E3.8", ja_wiki_url, "29,56:94,98:103,107:" | 345 { "第二次 E3.8", ja_wiki_url, "29,56:94,98:103,107:" |
| 346 "112,116:121,125:" | 346 "112,116:121,125:" |
| 347 "130,134:139,143" } | 347 "130,134:139,143" } |
| 348 }; | 348 }; |
| 349 | 349 |
| 350 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 350 for (size_t i = 0; i < arraysize(data); ++i) { |
| 351 model_ = client_.CreateModel(); | 351 model_ = client_.CreateModel(); |
| 352 std::vector<TitleAndURL> bookmarks; | 352 std::vector<TitleAndURL> bookmarks; |
| 353 TitleAndURL bookmark("123456", data[i].url); | 353 TitleAndURL bookmark("123456", data[i].url); |
| 354 bookmarks.push_back(bookmark); | 354 bookmarks.push_back(bookmark); |
| 355 AddBookmarks(bookmarks); | 355 AddBookmarks(bookmarks); |
| 356 | 356 |
| 357 std::vector<BookmarkMatch> matches; | 357 std::vector<BookmarkMatch> matches; |
| 358 model_->GetBookmarksMatching(UTF8ToUTF16(data[i].query), 1000, &matches); | 358 model_->GetBookmarksMatching(UTF8ToUTF16(data[i].query), 1000, &matches); |
| 359 ASSERT_EQ(1U, matches.size()) << data[i].url << data[i].query; | 359 ASSERT_EQ(1U, matches.size()) << data[i].url << data[i].query; |
| 360 | 360 |
| 361 BookmarkMatch::MatchPositions expected_url_matches; | 361 BookmarkMatch::MatchPositions expected_url_matches; |
| 362 ExtractMatchPositions(data[i].expected_url_match_positions, | 362 ExtractMatchPositions(data[i].expected_url_match_positions, |
| 363 &expected_url_matches); | 363 &expected_url_matches); |
| 364 ExpectMatchPositions(matches[0].url_match_positions, expected_url_matches); | 364 ExpectMatchPositions(matches[0].url_match_positions, expected_url_matches); |
| 365 } | 365 } |
| 366 } | 366 } |
| 367 | 367 |
| 368 // Makes sure index is updated when a node is removed. | 368 // Makes sure index is updated when a node is removed. |
| 369 TEST_F(BookmarkIndexTest, Remove) { | 369 TEST_F(BookmarkIndexTest, Remove) { |
| 370 const char* titles[] = { "a", "b" }; | 370 const char* titles[] = { "a", "b" }; |
| 371 const char* urls[] = {kAboutBlankURL, kAboutBlankURL}; | 371 const char* urls[] = {kAboutBlankURL, kAboutBlankURL}; |
| 372 AddBookmarks(titles, urls, ARRAYSIZE_UNSAFE(titles)); | 372 AddBookmarks(titles, urls, arraysize(titles)); |
| 373 | 373 |
| 374 // Remove the node and make sure we don't get back any results. | 374 // Remove the node and make sure we don't get back any results. |
| 375 model_->Remove(model_->other_node(), 0); | 375 model_->Remove(model_->other_node(), 0); |
| 376 ExpectMatches("A", NULL, 0U); | 376 ExpectMatches("A", NULL, 0U); |
| 377 } | 377 } |
| 378 | 378 |
| 379 // Makes sure index is updated when a node's title is changed. | 379 // Makes sure index is updated when a node's title is changed. |
| 380 TEST_F(BookmarkIndexTest, ChangeTitle) { | 380 TEST_F(BookmarkIndexTest, ChangeTitle) { |
| 381 const char* titles[] = { "a", "b" }; | 381 const char* titles[] = { "a", "b" }; |
| 382 const char* urls[] = {kAboutBlankURL, kAboutBlankURL}; | 382 const char* urls[] = {kAboutBlankURL, kAboutBlankURL}; |
| 383 AddBookmarks(titles, urls, ARRAYSIZE_UNSAFE(titles)); | 383 AddBookmarks(titles, urls, arraysize(titles)); |
| 384 | 384 |
| 385 // Remove the node and make sure we don't get back any results. | 385 // Remove the node and make sure we don't get back any results. |
| 386 const char* expected[] = { "blah" }; | 386 const char* expected[] = { "blah" }; |
| 387 model_->SetTitle(model_->other_node()->GetChild(0), ASCIIToUTF16("blah")); | 387 model_->SetTitle(model_->other_node()->GetChild(0), ASCIIToUTF16("blah")); |
| 388 ExpectMatches("BlAh", expected, ARRAYSIZE_UNSAFE(expected)); | 388 ExpectMatches("BlAh", expected, arraysize(expected)); |
| 389 } | 389 } |
| 390 | 390 |
| 391 // Makes sure no more than max queries is returned. | 391 // Makes sure no more than max queries is returned. |
| 392 TEST_F(BookmarkIndexTest, HonorMax) { | 392 TEST_F(BookmarkIndexTest, HonorMax) { |
| 393 const char* titles[] = { "abcd", "abcde" }; | 393 const char* titles[] = { "abcd", "abcde" }; |
| 394 const char* urls[] = {kAboutBlankURL, kAboutBlankURL}; | 394 const char* urls[] = {kAboutBlankURL, kAboutBlankURL}; |
| 395 AddBookmarks(titles, urls, ARRAYSIZE_UNSAFE(titles)); | 395 AddBookmarks(titles, urls, arraysize(titles)); |
| 396 | 396 |
| 397 std::vector<BookmarkMatch> matches; | 397 std::vector<BookmarkMatch> matches; |
| 398 model_->GetBookmarksMatching(ASCIIToUTF16("ABc"), 1, &matches); | 398 model_->GetBookmarksMatching(ASCIIToUTF16("ABc"), 1, &matches); |
| 399 EXPECT_EQ(1U, matches.size()); | 399 EXPECT_EQ(1U, matches.size()); |
| 400 } | 400 } |
| 401 | 401 |
| 402 // Makes sure if the lower case string of a bookmark title is more characters | 402 // Makes sure if the lower case string of a bookmark title is more characters |
| 403 // than the upper case string no match positions are returned. | 403 // than the upper case string no match positions are returned. |
| 404 TEST_F(BookmarkIndexTest, EmptyMatchOnMultiwideLowercaseString) { | 404 TEST_F(BookmarkIndexTest, EmptyMatchOnMultiwideLowercaseString) { |
| 405 const BookmarkNode* n1 = model_->AddURL(model_->other_node(), 0, | 405 const BookmarkNode* n1 = model_->AddURL(model_->other_node(), 0, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 419 const char* title; | 419 const char* title; |
| 420 const int typed_count; | 420 const int typed_count; |
| 421 } data[] = { | 421 } data[] = { |
| 422 { GURL("http://www.google.com/"), "Google", 100 }, | 422 { GURL("http://www.google.com/"), "Google", 100 }, |
| 423 { GURL("http://maps.google.com/"), "Google Maps", 40 }, | 423 { GURL("http://maps.google.com/"), "Google Maps", 40 }, |
| 424 { GURL("http://docs.google.com/"), "Google Docs", 50 }, | 424 { GURL("http://docs.google.com/"), "Google Docs", 50 }, |
| 425 { GURL("http://reader.google.com/"), "Google Reader", 80 }, | 425 { GURL("http://reader.google.com/"), "Google Reader", 80 }, |
| 426 }; | 426 }; |
| 427 | 427 |
| 428 std::map<GURL, int> typed_count_map; | 428 std::map<GURL, int> typed_count_map; |
| 429 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) | 429 for (size_t i = 0; i < arraysize(data); ++i) |
| 430 typed_count_map.insert(std::make_pair(data[i].url, data[i].typed_count)); | 430 typed_count_map.insert(std::make_pair(data[i].url, data[i].typed_count)); |
| 431 | 431 |
| 432 BookmarkClientMock client(typed_count_map); | 432 BookmarkClientMock client(typed_count_map); |
| 433 scoped_ptr<BookmarkModel> model = client.CreateModel(); | 433 scoped_ptr<BookmarkModel> model = client.CreateModel(); |
| 434 | 434 |
| 435 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) | 435 for (size_t i = 0; i < arraysize(data); ++i) |
| 436 // Populate the BookmarkIndex. | 436 // Populate the BookmarkIndex. |
| 437 model->AddURL( | 437 model->AddURL( |
| 438 model->other_node(), i, UTF8ToUTF16(data[i].title), data[i].url); | 438 model->other_node(), i, UTF8ToUTF16(data[i].title), data[i].url); |
| 439 | 439 |
| 440 // Populate match nodes. | 440 // Populate match nodes. |
| 441 std::vector<BookmarkMatch> matches; | 441 std::vector<BookmarkMatch> matches; |
| 442 model->GetBookmarksMatching(ASCIIToUTF16("google"), 4, &matches); | 442 model->GetBookmarksMatching(ASCIIToUTF16("google"), 4, &matches); |
| 443 | 443 |
| 444 // The resulting order should be: | 444 // The resulting order should be: |
| 445 // 1. Google (google.com) 100 | 445 // 1. Google (google.com) 100 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 456 // Select top two matches. | 456 // Select top two matches. |
| 457 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches); | 457 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches); |
| 458 | 458 |
| 459 ASSERT_EQ(2, static_cast<int>(matches.size())); | 459 ASSERT_EQ(2, static_cast<int>(matches.size())); |
| 460 EXPECT_EQ(data[0].url, matches[0].node->url()); | 460 EXPECT_EQ(data[0].url, matches[0].node->url()); |
| 461 EXPECT_EQ(data[3].url, matches[1].node->url()); | 461 EXPECT_EQ(data[3].url, matches[1].node->url()); |
| 462 } | 462 } |
| 463 | 463 |
| 464 } // namespace | 464 } // namespace |
| 465 } // namespace bookmarks | 465 } // namespace bookmarks |
| OLD | NEW |