| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 GURL(bookmarks[i].second)); | 76 GURL(bookmarks[i].second)); |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 | 79 |
| 80 void ExpectMatches(const std::string& query, | 80 void ExpectMatches(const std::string& query, |
| 81 const char** expected_titles, | 81 const char** expected_titles, |
| 82 size_t expected_count) { | 82 size_t expected_count) { |
| 83 std::vector<std::string> title_vector; | 83 std::vector<std::string> title_vector; |
| 84 for (size_t i = 0; i < expected_count; ++i) | 84 for (size_t i = 0; i < expected_count; ++i) |
| 85 title_vector.push_back(expected_titles[i]); | 85 title_vector.push_back(expected_titles[i]); |
| 86 ExpectMatches(query, title_vector); | 86 ExpectMatches(query, false /* always_prefix_search */, title_vector); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void ExpectMatches(const std::string& query, | 89 void ExpectMatches(const std::string& query, |
| 90 bool always_prefix_search, |
| 90 const std::vector<std::string>& expected_titles) { | 91 const std::vector<std::string>& expected_titles) { |
| 91 std::vector<BookmarkMatch> matches; | 92 std::vector<BookmarkMatch> matches; |
| 92 model_->GetBookmarksMatching(ASCIIToUTF16(query), 1000, &matches); | 93 if (always_prefix_search) |
| 94 model_->GetBookmarksMatchingPrefixes(ASCIIToUTF16(query), 1000, &matches); |
| 95 else |
| 96 model_->GetBookmarksMatching(ASCIIToUTF16(query), 1000, &matches); |
| 93 ASSERT_EQ(expected_titles.size(), matches.size()); | 97 ASSERT_EQ(expected_titles.size(), matches.size()); |
| 94 for (size_t i = 0; i < expected_titles.size(); ++i) { | 98 for (size_t i = 0; i < expected_titles.size(); ++i) { |
| 95 bool found = false; | 99 bool found = false; |
| 96 for (size_t j = 0; j < matches.size(); ++j) { | 100 for (size_t j = 0; j < matches.size(); ++j) { |
| 97 if (ASCIIToUTF16(expected_titles[i]) == matches[j].node->GetTitle()) { | 101 if (ASCIIToUTF16(expected_titles[i]) == matches[j].node->GetTitle()) { |
| 98 matches.erase(matches.begin() + j); | 102 matches.erase(matches.begin() + j); |
| 99 found = true; | 103 found = true; |
| 100 break; | 104 break; |
| 101 } | 105 } |
| 102 } | 106 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 "ab cde ghi", | 168 "ab cde ghi", |
| 165 "ab cdef ghij"}, | 169 "ab cdef ghij"}, |
| 166 | 170 |
| 167 // Title with term multiple times. | 171 // Title with term multiple times. |
| 168 { "ab ab", "ab", "ab ab"}, | 172 { "ab ab", "ab", "ab ab"}, |
| 169 | 173 |
| 170 // Make sure quotes don't do a prefix match. | 174 // Make sure quotes don't do a prefix match. |
| 171 { "think", "\"thi\"", ""}, | 175 { "think", "\"thi\"", ""}, |
| 172 | 176 |
| 173 // Prefix matches against multiple candidates. | 177 // Prefix matches against multiple candidates. |
| 174 { "abc1 abc2 abc3 abc4", "abc", "abc1 abc2 abc3 abc4"}, | 178 { "abc1 abc2 abc3 abc4", "abc", "abc1 abc2 abc3 abc4"}, |
| 179 |
| 180 // Prefix match on the first term. |
| 181 { "abc", "a", "" }, |
| 182 |
| 183 // Prefix match on subsequent terms. |
| 184 { "abc def", "abc d", "" }, |
| 185 |
| 186 |
| 175 }; | 187 }; |
| 176 for (size_t i = 0; i < arraysize(data); ++i) { | 188 for (size_t i = 0; i < arraysize(data); ++i) { |
| 177 std::vector<std::string> titles; | 189 std::vector<std::string> titles; |
| 190 base::SplitString(data[i].titles, ';', &titles); |
| 191 std::vector<TitleAndURL> bookmarks; |
| 192 for (size_t j = 0; j < titles.size(); ++j) { |
| 193 TitleAndURL bookmark(titles[j], kAboutBlankURL); |
| 194 bookmarks.push_back(bookmark); |
| 195 } |
| 196 AddBookmarks(bookmarks); |
| 197 |
| 198 std::vector<std::string> expected; |
| 199 if (!data[i].expected.empty()) |
| 200 base::SplitString(data[i].expected, ';', &expected); |
| 201 |
| 202 ExpectMatches(data[i].query, false /* always_prefix_search */, expected); |
| 203 |
| 204 model_ = client_.CreateModel(); |
| 205 } |
| 206 } |
| 207 |
| 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" }, |
| 216 |
| 217 // Prefix match, one term. |
| 218 { "abcd;abc;b", "abc", "abcd;abc" }, |
| 219 |
| 220 // Prefix match, multiple terms. |
| 221 { "abcd cdef;abcd;abcd cdefg", "abc cde", "abcd cdef;abcd cdefg" }, |
| 222 |
| 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" }, |
| 227 |
| 228 // Title with term multiple times. |
| 229 { "ab ab", "ab", "ab ab" }, |
| 230 |
| 231 // Make sure quotes don't do a prefix match. |
| 232 { "think", "\"thi\"", "" }, |
| 233 |
| 234 // Prefix matches against multiple candidates. |
| 235 { "abc1 abc2 abc3 abc4", "abc", "abc1 abc2 abc3 abc4" }, |
| 236 |
| 237 // Prefix match on the first term. |
| 238 { "abc", "a", "abc" }, |
| 239 |
| 240 // Prefix match on subsequent terms. |
| 241 { "abc def", "abc d", "abc def" }, |
| 242 |
| 243 // Exact and prefix match. |
| 244 { "ab cdef;abcd;abcd cdefg", "ab cdef", "ab cdef;abcd cdefg" }, |
| 245 }; |
| 246 for (size_t i = 0; i < arraysize(data); ++i) { |
| 247 std::vector<std::string> titles; |
| 178 base::SplitString(data[i].titles, ';', &titles); | 248 base::SplitString(data[i].titles, ';', &titles); |
| 179 std::vector<TitleAndURL> bookmarks; | 249 std::vector<TitleAndURL> bookmarks; |
| 180 for (size_t j = 0; j < titles.size(); ++j) { | 250 for (size_t j = 0; j < titles.size(); ++j) { |
| 181 TitleAndURL bookmark(titles[j], kAboutBlankURL); | 251 TitleAndURL bookmark(titles[j], kAboutBlankURL); |
| 182 bookmarks.push_back(bookmark); | 252 bookmarks.push_back(bookmark); |
| 183 } | 253 } |
| 184 AddBookmarks(bookmarks); | 254 AddBookmarks(bookmarks); |
| 185 | 255 |
| 186 std::vector<std::string> expected; | 256 std::vector<std::string> expected; |
| 187 if (!data[i].expected.empty()) | 257 if (!data[i].expected.empty()) |
| 188 base::SplitString(data[i].expected, ';', &expected); | 258 base::SplitString(data[i].expected, ';', &expected); |
| 189 | 259 |
| 190 ExpectMatches(data[i].query, expected); | 260 ExpectMatches(data[i].query, true /* always_prefix_search */, expected); |
| 191 | 261 |
| 192 model_ = client_.CreateModel(); | 262 model_ = client_.CreateModel(); |
| 193 } | 263 } |
| 194 } | 264 } |
| 195 | 265 |
| 196 // Analogous to GetBookmarksMatching, this test tests various permutations | 266 // Analogous to GetBookmarksMatching, this test tests various permutations |
| 197 // of title, URL, and input to see if the title/URL matches the input as | 267 // of title, URL, and input to see if the title/URL matches the input as |
| 198 // expected. | 268 // expected. |
| 199 TEST_F(BookmarkIndexTest, GetBookmarksMatchingWithURLs) { | 269 TEST_F(BookmarkIndexTest, GetBookmarksMatchingWithURLs) { |
| 200 struct TestData { | 270 struct TestData { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 for (size_t i = 0; i < arraysize(data); ++i) { | 310 for (size_t i = 0; i < arraysize(data); ++i) { |
| 241 model_ = client_.CreateModel(); | 311 model_ = client_.CreateModel(); |
| 242 std::vector<TitleAndURL> bookmarks; | 312 std::vector<TitleAndURL> bookmarks; |
| 243 bookmarks.push_back(TitleAndURL(data[i].title, data[i].url)); | 313 bookmarks.push_back(TitleAndURL(data[i].title, data[i].url)); |
| 244 AddBookmarks(bookmarks); | 314 AddBookmarks(bookmarks); |
| 245 | 315 |
| 246 std::vector<std::string> expected; | 316 std::vector<std::string> expected; |
| 247 if (data[i].should_be_retrieved) | 317 if (data[i].should_be_retrieved) |
| 248 expected.push_back(data[i].title); | 318 expected.push_back(data[i].title); |
| 249 | 319 |
| 250 ExpectMatches(data[i].query, expected); | 320 ExpectMatches(data[i].query, false /* always_prefix_search */, expected); |
| 251 } | 321 } |
| 252 } | 322 } |
| 253 | 323 |
| 254 TEST_F(BookmarkIndexTest, Normalization) { | 324 TEST_F(BookmarkIndexTest, Normalization) { |
| 255 struct TestData { | 325 struct TestData { |
| 256 const char* const title; | 326 const char* const title; |
| 257 const char* const query; | 327 const char* const query; |
| 258 } data[] = { | 328 } data[] = { |
| 259 { "fooa\xcc\x88-test", "foo\xc3\xa4-test" }, | 329 { "fooa\xcc\x88-test", "foo\xc3\xa4-test" }, |
| 260 { "fooa\xcc\x88-test", "fooa\xcc\x88-test" }, | 330 { "fooa\xcc\x88-test", "fooa\xcc\x88-test" }, |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 // Select top two matches. | 526 // Select top two matches. |
| 457 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches); | 527 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches); |
| 458 | 528 |
| 459 ASSERT_EQ(2, static_cast<int>(matches.size())); | 529 ASSERT_EQ(2, static_cast<int>(matches.size())); |
| 460 EXPECT_EQ(data[0].url, matches[0].node->url()); | 530 EXPECT_EQ(data[0].url, matches[0].node->url()); |
| 461 EXPECT_EQ(data[3].url, matches[1].node->url()); | 531 EXPECT_EQ(data[3].url, matches[1].node->url()); |
| 462 } | 532 } |
| 463 | 533 |
| 464 } // namespace | 534 } // namespace |
| 465 } // namespace bookmarks | 535 } // namespace bookmarks |
| OLD | NEW |