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, query_parser::MatchingAlgorithm::DEFAULT, |
| 87 title_vector); |
87 } | 88 } |
88 | 89 |
89 void ExpectMatches(const std::string& query, | 90 void ExpectMatches(const std::string& query, |
| 91 query_parser::MatchingAlgorithm matching_algorithm, |
90 const std::vector<std::string>& expected_titles) { | 92 const std::vector<std::string>& expected_titles) { |
91 std::vector<BookmarkMatch> matches; | 93 std::vector<BookmarkMatch> matches; |
92 model_->GetBookmarksMatching(ASCIIToUTF16(query), 1000, &matches); | 94 model_->GetBookmarksMatching(ASCIIToUTF16(query), 1000, matching_algorithm, |
| 95 &matches); |
93 ASSERT_EQ(expected_titles.size(), matches.size()); | 96 ASSERT_EQ(expected_titles.size(), matches.size()); |
94 for (size_t i = 0; i < expected_titles.size(); ++i) { | 97 for (size_t i = 0; i < expected_titles.size(); ++i) { |
95 bool found = false; | 98 bool found = false; |
96 for (size_t j = 0; j < matches.size(); ++j) { | 99 for (size_t j = 0; j < matches.size(); ++j) { |
97 if (ASCIIToUTF16(expected_titles[i]) == matches[j].node->GetTitle()) { | 100 if (ASCIIToUTF16(expected_titles[i]) == matches[j].node->GetTitle()) { |
98 matches.erase(matches.begin() + j); | 101 matches.erase(matches.begin() + j); |
99 found = true; | 102 found = true; |
100 break; | 103 break; |
101 } | 104 } |
102 } | 105 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 "ab cde ghi", | 167 "ab cde ghi", |
165 "ab cdef ghij"}, | 168 "ab cdef ghij"}, |
166 | 169 |
167 // Title with term multiple times. | 170 // Title with term multiple times. |
168 { "ab ab", "ab", "ab ab"}, | 171 { "ab ab", "ab", "ab ab"}, |
169 | 172 |
170 // Make sure quotes don't do a prefix match. | 173 // Make sure quotes don't do a prefix match. |
171 { "think", "\"thi\"", ""}, | 174 { "think", "\"thi\"", ""}, |
172 | 175 |
173 // Prefix matches against multiple candidates. | 176 // Prefix matches against multiple candidates. |
174 { "abc1 abc2 abc3 abc4", "abc", "abc1 abc2 abc3 abc4"}, | 177 { "abc1 abc2 abc3 abc4", "abc", "abc1 abc2 abc3 abc4"}, |
| 178 |
| 179 // Prefix match on the first term. |
| 180 { "abc", "a", "" }, |
| 181 |
| 182 // Prefix match on subsequent terms. |
| 183 { "abc def", "abc d", "" }, |
| 184 |
| 185 |
175 }; | 186 }; |
176 for (size_t i = 0; i < arraysize(data); ++i) { | 187 for (size_t i = 0; i < arraysize(data); ++i) { |
177 std::vector<std::string> titles; | 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); |
| 194 } |
| 195 AddBookmarks(bookmarks); |
| 196 |
| 197 std::vector<std::string> expected; |
| 198 if (!data[i].expected.empty()) |
| 199 base::SplitString(data[i].expected, ';', &expected); |
| 200 |
| 201 ExpectMatches(data[i].query, query_parser::MatchingAlgorithm::DEFAULT, |
| 202 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, |
| 261 query_parser::MatchingAlgorithm::ALWAYS_PREFIX_SEARCH, |
| 262 expected); |
191 | 263 |
192 model_ = client_.CreateModel(); | 264 model_ = client_.CreateModel(); |
193 } | 265 } |
194 } | 266 } |
195 | 267 |
196 // Analogous to GetBookmarksMatching, this test tests various permutations | 268 // Analogous to GetBookmarksMatching, this test tests various permutations |
197 // of title, URL, and input to see if the title/URL matches the input as | 269 // of title, URL, and input to see if the title/URL matches the input as |
198 // expected. | 270 // expected. |
199 TEST_F(BookmarkIndexTest, GetBookmarksMatchingWithURLs) { | 271 TEST_F(BookmarkIndexTest, GetBookmarksMatchingWithURLs) { |
200 struct TestData { | 272 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) { | 312 for (size_t i = 0; i < arraysize(data); ++i) { |
241 model_ = client_.CreateModel(); | 313 model_ = client_.CreateModel(); |
242 std::vector<TitleAndURL> bookmarks; | 314 std::vector<TitleAndURL> bookmarks; |
243 bookmarks.push_back(TitleAndURL(data[i].title, data[i].url)); | 315 bookmarks.push_back(TitleAndURL(data[i].title, data[i].url)); |
244 AddBookmarks(bookmarks); | 316 AddBookmarks(bookmarks); |
245 | 317 |
246 std::vector<std::string> expected; | 318 std::vector<std::string> expected; |
247 if (data[i].should_be_retrieved) | 319 if (data[i].should_be_retrieved) |
248 expected.push_back(data[i].title); | 320 expected.push_back(data[i].title); |
249 | 321 |
250 ExpectMatches(data[i].query, expected); | 322 ExpectMatches(data[i].query, query_parser::MatchingAlgorithm::DEFAULT, |
| 323 expected); |
251 } | 324 } |
252 } | 325 } |
253 | 326 |
254 TEST_F(BookmarkIndexTest, Normalization) { | 327 TEST_F(BookmarkIndexTest, Normalization) { |
255 struct TestData { | 328 struct TestData { |
256 const char* const title; | 329 const char* const title; |
257 const char* const query; | 330 const char* const query; |
258 } data[] = { | 331 } data[] = { |
259 { "fooa\xcc\x88-test", "foo\xc3\xa4-test" }, | 332 { "fooa\xcc\x88-test", "foo\xc3\xa4-test" }, |
260 { "fooa\xcc\x88-test", "fooa\xcc\x88-test" }, | 333 { "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. | 529 // Select top two matches. |
457 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches); | 530 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches); |
458 | 531 |
459 ASSERT_EQ(2, static_cast<int>(matches.size())); | 532 ASSERT_EQ(2, static_cast<int>(matches.size())); |
460 EXPECT_EQ(data[0].url, matches[0].node->url()); | 533 EXPECT_EQ(data[0].url, matches[0].node->url()); |
461 EXPECT_EQ(data[3].url, matches[1].node->url()); | 534 EXPECT_EQ(data[3].url, matches[1].node->url()); |
462 } | 535 } |
463 | 536 |
464 } // namespace | 537 } // namespace |
465 } // namespace bookmarks | 538 } // namespace bookmarks |
OLD | NEW |