OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/autocomplete/bookmark_provider.h" | 5 #include "chrome/browser/autocomplete/bookmark_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 DCHECK(profile_.get()); | 75 DCHECK(profile_.get()); |
76 provider_ = new BookmarkProvider(this, profile_.get()); | 76 provider_ = new BookmarkProvider(this, profile_.get()); |
77 DCHECK(provider_.get()); | 77 DCHECK(provider_.get()); |
78 provider_->set_bookmark_model_for_testing(model_.get()); | 78 provider_->set_bookmark_model_for_testing(model_.get()); |
79 | 79 |
80 const BookmarkNode* other_node = model_->other_node(); | 80 const BookmarkNode* other_node = model_->other_node(); |
81 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(bookmark_provider_test_data); ++i) { | 81 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(bookmark_provider_test_data); ++i) { |
82 const BookmarksTestInfo& cur(bookmark_provider_test_data[i]); | 82 const BookmarksTestInfo& cur(bookmark_provider_test_data[i]); |
83 const GURL url(cur.url); | 83 const GURL url(cur.url); |
84 model_->AddURL(other_node, other_node->child_count(), | 84 model_->AddURL(other_node, other_node->child_count(), |
85 ASCIIToUTF16(cur.title), url); | 85 base::ASCIIToUTF16(cur.title), url); |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 // Structures and functions supporting the BookmarkProviderTest.Positions | 89 // Structures and functions supporting the BookmarkProviderTest.Positions |
90 // unit test. | 90 // unit test. |
91 | 91 |
92 struct TestBookmarkPosition { | 92 struct TestBookmarkPosition { |
93 TestBookmarkPosition(size_t begin, size_t end) | 93 TestBookmarkPosition(size_t begin, size_t end) |
94 : begin(begin), end(end) {} | 94 : begin(begin), end(end) {} |
95 | 95 |
(...skipping 15 matching lines...) Expand all Loading... |
111 } | 111 } |
112 position_string += "}\n"; | 112 position_string += "}\n"; |
113 return position_string; | 113 return position_string; |
114 } | 114 } |
115 | 115 |
116 // Return the positions in |matches| as a formatted string for unit test | 116 // Return the positions in |matches| as a formatted string for unit test |
117 // diagnostic output. | 117 // diagnostic output. |
118 base::string16 MatchesAsString16(const ACMatches& matches) { | 118 base::string16 MatchesAsString16(const ACMatches& matches) { |
119 base::string16 matches_string; | 119 base::string16 matches_string; |
120 for (ACMatches::const_iterator i = matches.begin(); i != matches.end(); ++i) { | 120 for (ACMatches::const_iterator i = matches.begin(); i != matches.end(); ++i) { |
121 matches_string.append(ASCIIToUTF16(" '")); | 121 matches_string.append(base::ASCIIToUTF16(" '")); |
122 matches_string.append(i->description); | 122 matches_string.append(i->description); |
123 matches_string.append(ASCIIToUTF16("'\n")); | 123 matches_string.append(base::ASCIIToUTF16("'\n")); |
124 } | 124 } |
125 return matches_string; | 125 return matches_string; |
126 } | 126 } |
127 | 127 |
128 // Comparison function for sorting search terms by descending length. | 128 // Comparison function for sorting search terms by descending length. |
129 bool TestBookmarkPositionsEqual(const TestBookmarkPosition& pos_a, | 129 bool TestBookmarkPositionsEqual(const TestBookmarkPosition& pos_a, |
130 const TestBookmarkPosition& pos_b) { | 130 const TestBookmarkPosition& pos_b) { |
131 return pos_a.begin == pos_b.begin && pos_a.end == pos_b.end; | 131 return pos_a.begin == pos_b.begin && pos_a.end == pos_b.end; |
132 } | 132 } |
133 | 133 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 {"\"carry carbo\"", 0, {{{0, 0}}}}, | 235 {"\"carry carbo\"", 0, {{{0, 0}}}}, |
236 // This set uses duplicated and/or overlaps search terms in the title. | 236 // This set uses duplicated and/or overlaps search terms in the title. |
237 {"frank", 1, {{{0, 5}, {8, 13}, {16, 21}, {0, 0}}}}, | 237 {"frank", 1, {{{0, 5}, {8, 13}, {16, 21}, {0, 0}}}}, |
238 {"frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}}, | 238 {"frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}}, |
239 {"frankly frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}}, | 239 {"frankly frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}}, |
240 {"foobar foo", 1, {{{0, 6}, {7, 13}, {0, 0}}}}, | 240 {"foobar foo", 1, {{{0, 6}, {7, 13}, {0, 0}}}}, |
241 {"foo foobar", 1, {{{0, 6}, {7, 13}, {0, 0}}}}, | 241 {"foo foobar", 1, {{{0, 6}, {7, 13}, {0, 0}}}}, |
242 }; | 242 }; |
243 | 243 |
244 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { | 244 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { |
245 AutocompleteInput input(ASCIIToUTF16(query_data[i].query), | 245 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query), |
246 base::string16::npos, base::string16(), GURL(), | 246 base::string16::npos, base::string16(), GURL(), |
247 AutocompleteInput::INVALID_SPEC, false, false, | 247 AutocompleteInput::INVALID_SPEC, false, false, |
248 false, AutocompleteInput::ALL_MATCHES); | 248 false, AutocompleteInput::ALL_MATCHES); |
249 provider_->Start(input, false); | 249 provider_->Start(input, false); |
250 const ACMatches& matches(provider_->matches()); | 250 const ACMatches& matches(provider_->matches()); |
251 // Validate number of results is as expected. | 251 // Validate number of results is as expected. |
252 EXPECT_LE(matches.size(), query_data[i].match_count) | 252 EXPECT_LE(matches.size(), query_data[i].match_count) |
253 << "One or more of the following matches were unexpected:\n" | 253 << "One or more of the following matches were unexpected:\n" |
254 << MatchesAsString16(matches) | 254 << MatchesAsString16(matches) |
255 << "For query '" << query_data[i].query << "'."; | 255 << "For query '" << query_data[i].query << "'."; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 {"worms", 2, {"burning worms #2", // boosted | 309 {"worms", 2, {"burning worms #2", // boosted |
310 "burning worms #1"}}, // not boosted | 310 "burning worms #1"}}, // not boosted |
311 // Ranking of prefix matches with URL boost. Note that a query of | 311 // Ranking of prefix matches with URL boost. Note that a query of |
312 // "worm burn" will have the same results. | 312 // "worm burn" will have the same results. |
313 {"burn worm", 3, {"burning worms #2", // boosted | 313 {"burn worm", 3, {"burning worms #2", // boosted |
314 "worming burns #20", // boosted | 314 "worming burns #20", // boosted |
315 "burning worms #1"}}, // not boosted but shorter | 315 "burning worms #1"}}, // not boosted but shorter |
316 }; | 316 }; |
317 | 317 |
318 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { | 318 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { |
319 AutocompleteInput input(ASCIIToUTF16(query_data[i].query), | 319 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query), |
320 base::string16::npos, base::string16(), GURL(), | 320 base::string16::npos, base::string16(), GURL(), |
321 AutocompleteInput::INVALID_SPEC, false, false, | 321 AutocompleteInput::INVALID_SPEC, false, false, |
322 false, AutocompleteInput::ALL_MATCHES); | 322 false, AutocompleteInput::ALL_MATCHES); |
323 provider_->Start(input, false); | 323 provider_->Start(input, false); |
324 const ACMatches& matches(provider_->matches()); | 324 const ACMatches& matches(provider_->matches()); |
325 // Validate number and content of results is as expected. | 325 // Validate number and content of results is as expected. |
326 for (size_t j = 0; j < std::max(query_data[i].match_count, matches.size()); | 326 for (size_t j = 0; j < std::max(query_data[i].match_count, matches.size()); |
327 ++j) { | 327 ++j) { |
328 EXPECT_LT(j, query_data[i].match_count) << " Unexpected match '" | 328 EXPECT_LT(j, query_data[i].match_count) << " Unexpected match '" |
329 << UTF16ToUTF8(matches[j].description) << "' for query: '" | 329 << base::UTF16ToUTF8(matches[j].description) << "' for query: '" |
330 << query_data[i].query << "'."; | 330 << query_data[i].query << "'."; |
331 if (j >= query_data[i].match_count) | 331 if (j >= query_data[i].match_count) |
332 continue; | 332 continue; |
333 EXPECT_LT(j, matches.size()) << " Missing match '" | 333 EXPECT_LT(j, matches.size()) << " Missing match '" |
334 << query_data[i].matches[j] << "' for query: '" | 334 << query_data[i].matches[j] << "' for query: '" |
335 << query_data[i].query << "'."; | 335 << query_data[i].query << "'."; |
336 if (j >= matches.size()) | 336 if (j >= matches.size()) |
337 continue; | 337 continue; |
338 EXPECT_EQ(query_data[i].matches[j], UTF16ToUTF8(matches[j].description)) | 338 EXPECT_EQ(query_data[i].matches[j], |
| 339 base::UTF16ToUTF8(matches[j].description)) |
339 << " Mismatch at [" << base::IntToString(j) << "] for query '" | 340 << " Mismatch at [" << base::IntToString(j) << "] for query '" |
340 << query_data[i].query << "'."; | 341 << query_data[i].query << "'."; |
341 EXPECT_FALSE(matches[j].allowed_to_be_default_match); | 342 EXPECT_FALSE(matches[j].allowed_to_be_default_match); |
342 } | 343 } |
343 } | 344 } |
344 } | 345 } |
OLD | NEW |