| 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 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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_UNSAFE(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], url::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; |
| 187 if (!data[i].expected.empty()) | 187 if (!data[i].expected.empty()) |
| 188 base::SplitString(data[i].expected, ';', &expected); | 188 base::SplitString(data[i].expected, ';', &expected); |
| 189 | 189 |
| 190 ExpectMatches(data[i].query, expected); | 190 ExpectMatches(data[i].query, expected); |
| 191 | 191 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 { "fooa\xcc\x88-test", "fooa\xcc\x88" }, | 262 { "fooa\xcc\x88-test", "fooa\xcc\x88" }, |
| 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(url::kAboutBlankURL); |
| 273 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 273 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(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(false); | 278 model_ = client_.CreateModel(false); |
| 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_UNSAFE(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, url::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; |
| 310 ExtractMatchPositions(data[i].expected_title_match_positions, | 310 ExtractMatchPositions(data[i].expected_title_match_positions, |
| 311 &expected_title_matches); | 311 &expected_title_matches); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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[] = { url::kAboutBlankURL, url::kAboutBlankURL }; |
| 372 AddBookmarks(titles, urls, ARRAYSIZE_UNSAFE(titles)); | 372 AddBookmarks(titles, urls, ARRAYSIZE_UNSAFE(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[] = { url::kAboutBlankURL, url::kAboutBlankURL }; |
| 383 AddBookmarks(titles, urls, ARRAYSIZE_UNSAFE(titles)); | 383 AddBookmarks(titles, urls, ARRAYSIZE_UNSAFE(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_UNSAFE(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[] = { url::kAboutBlankURL, url::kAboutBlankURL }; |
| 395 AddBookmarks(titles, urls, ARRAYSIZE_UNSAFE(titles)); | 395 AddBookmarks(titles, urls, ARRAYSIZE_UNSAFE(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) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |