| 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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 found = true; | 102 found = true; |
| 103 break; | 103 break; |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 ASSERT_TRUE(found); | 106 ASSERT_TRUE(found); |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 | 109 |
| 110 void ExtractMatchPositions(const std::string& string, | 110 void ExtractMatchPositions(const std::string& string, |
| 111 BookmarkMatch::MatchPositions* matches) { | 111 BookmarkMatch::MatchPositions* matches) { |
| 112 std::vector<std::string> match_strings; | 112 for (const base::StringPiece& match : |
| 113 base::SplitString(string, ':', &match_strings); | 113 base::SplitStringPiece(string, ":", |
| 114 for (size_t i = 0; i < match_strings.size(); ++i) { | 114 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
| 115 std::vector<std::string> chunks; | 115 std::vector<base::StringPiece> chunks = base::SplitStringPiece( |
| 116 base::SplitString(match_strings[i], ',', &chunks); | 116 match, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 117 ASSERT_EQ(2U, chunks.size()); | 117 ASSERT_EQ(2U, chunks.size()); |
| 118 matches->push_back(BookmarkMatch::MatchPosition()); | 118 matches->push_back(BookmarkMatch::MatchPosition()); |
| 119 int chunks0, chunks1; | 119 int chunks0, chunks1; |
| 120 EXPECT_TRUE(base::StringToInt(chunks[0], &chunks0)); | 120 EXPECT_TRUE(base::StringToInt(chunks[0], &chunks0)); |
| 121 EXPECT_TRUE(base::StringToInt(chunks[1], &chunks1)); | 121 EXPECT_TRUE(base::StringToInt(chunks[1], &chunks1)); |
| 122 matches->back().first = chunks0; | 122 matches->back().first = chunks0; |
| 123 matches->back().second = chunks1; | 123 matches->back().second = chunks1; |
| 124 } | 124 } |
| 125 } | 125 } |
| 126 | 126 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 "abc def abc def abc def abc def abc def", | 185 "abc def abc def abc def abc def abc def", |
| 186 "abc1 abc2 abc3 abc4 def1 def2 def3 def4"}, | 186 "abc1 abc2 abc3 abc4 def1 def2 def3 def4"}, |
| 187 | 187 |
| 188 // Prefix match on the first term. | 188 // Prefix match on the first term. |
| 189 { "abc", "a", "" }, | 189 { "abc", "a", "" }, |
| 190 | 190 |
| 191 // Prefix match on subsequent terms. | 191 // Prefix match on subsequent terms. |
| 192 { "abc def", "abc d", "" }, | 192 { "abc def", "abc d", "" }, |
| 193 }; | 193 }; |
| 194 for (size_t i = 0; i < arraysize(data); ++i) { | 194 for (size_t i = 0; i < arraysize(data); ++i) { |
| 195 std::vector<std::string> titles; | |
| 196 base::SplitString(data[i].titles, ';', &titles); | |
| 197 std::vector<TitleAndURL> bookmarks; | 195 std::vector<TitleAndURL> bookmarks; |
| 198 for (size_t j = 0; j < titles.size(); ++j) { | 196 for (const std::string& title : base::SplitString( |
| 199 TitleAndURL bookmark(titles[j], kAboutBlankURL); | 197 data[i].titles, ";", |
| 198 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
| 199 TitleAndURL bookmark(title, kAboutBlankURL); |
| 200 bookmarks.push_back(bookmark); | 200 bookmarks.push_back(bookmark); |
| 201 } | 201 } |
| 202 AddBookmarks(bookmarks); | 202 AddBookmarks(bookmarks); |
| 203 | 203 |
| 204 std::vector<std::string> expected; | 204 std::vector<std::string> expected; |
| 205 if (!data[i].expected.empty()) | 205 if (!data[i].expected.empty()) { |
| 206 base::SplitString(data[i].expected, ';', &expected); | 206 expected = base::SplitString(data[i].expected, ";", |
| 207 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 208 } |
| 207 | 209 |
| 208 ExpectMatches(data[i].query, query_parser::MatchingAlgorithm::DEFAULT, | 210 ExpectMatches(data[i].query, query_parser::MatchingAlgorithm::DEFAULT, |
| 209 expected); | 211 expected); |
| 210 | 212 |
| 211 model_ = client_.CreateModel(); | 213 model_ = client_.CreateModel(); |
| 212 } | 214 } |
| 213 } | 215 } |
| 214 | 216 |
| 215 TEST_F(BookmarkIndexTest, GetBookmarksMatchingAlwaysPrefixSearch) { | 217 TEST_F(BookmarkIndexTest, GetBookmarksMatchingAlwaysPrefixSearch) { |
| 216 struct TestData { | 218 struct TestData { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 244 // Prefix match on the first term. | 246 // Prefix match on the first term. |
| 245 { "abc", "a", "abc" }, | 247 { "abc", "a", "abc" }, |
| 246 | 248 |
| 247 // Prefix match on subsequent terms. | 249 // Prefix match on subsequent terms. |
| 248 { "abc def", "abc d", "abc def" }, | 250 { "abc def", "abc d", "abc def" }, |
| 249 | 251 |
| 250 // Exact and prefix match. | 252 // Exact and prefix match. |
| 251 { "ab cdef;abcd;abcd cdefg", "ab cdef", "ab cdef;abcd cdefg" }, | 253 { "ab cdef;abcd;abcd cdefg", "ab cdef", "ab cdef;abcd cdefg" }, |
| 252 }; | 254 }; |
| 253 for (size_t i = 0; i < arraysize(data); ++i) { | 255 for (size_t i = 0; i < arraysize(data); ++i) { |
| 254 std::vector<std::string> titles; | |
| 255 base::SplitString(data[i].titles, ';', &titles); | |
| 256 std::vector<TitleAndURL> bookmarks; | 256 std::vector<TitleAndURL> bookmarks; |
| 257 for (size_t j = 0; j < titles.size(); ++j) { | 257 for (const std::string& title : base::SplitString( |
| 258 TitleAndURL bookmark(titles[j], kAboutBlankURL); | 258 data[i].titles, ";", |
| 259 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
| 260 TitleAndURL bookmark(title, kAboutBlankURL); |
| 259 bookmarks.push_back(bookmark); | 261 bookmarks.push_back(bookmark); |
| 260 } | 262 } |
| 261 AddBookmarks(bookmarks); | 263 AddBookmarks(bookmarks); |
| 262 | 264 |
| 263 std::vector<std::string> expected; | 265 std::vector<std::string> expected; |
| 264 if (!data[i].expected.empty()) | 266 if (!data[i].expected.empty()) { |
| 265 base::SplitString(data[i].expected, ';', &expected); | 267 expected = base::SplitString(data[i].expected, ";", |
| 268 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 269 } |
| 266 | 270 |
| 267 ExpectMatches(data[i].query, | 271 ExpectMatches(data[i].query, |
| 268 query_parser::MatchingAlgorithm::ALWAYS_PREFIX_SEARCH, | 272 query_parser::MatchingAlgorithm::ALWAYS_PREFIX_SEARCH, |
| 269 expected); | 273 expected); |
| 270 | 274 |
| 271 model_ = client_.CreateModel(); | 275 model_ = client_.CreateModel(); |
| 272 } | 276 } |
| 273 } | 277 } |
| 274 | 278 |
| 275 // Analogous to GetBookmarksMatching, this test tests various permutations | 279 // Analogous to GetBookmarksMatching, this test tests various permutations |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 // Select top two matches. | 552 // Select top two matches. |
| 549 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches); | 553 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches); |
| 550 | 554 |
| 551 ASSERT_EQ(2U, matches.size()); | 555 ASSERT_EQ(2U, matches.size()); |
| 552 EXPECT_EQ(data[0].url, matches[0].node->url()); | 556 EXPECT_EQ(data[0].url, matches[0].node->url()); |
| 553 EXPECT_EQ(data[3].url, matches[1].node->url()); | 557 EXPECT_EQ(data[3].url, matches[1].node->url()); |
| 554 } | 558 } |
| 555 | 559 |
| 556 } // namespace | 560 } // namespace |
| 557 } // namespace bookmarks | 561 } // namespace bookmarks |
| OLD | NEW |