Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/autocomplete/bookmark_provider.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/string_number_conversions.h" | |
| 14 #include "base/string16.h" | |
| 15 #include "base/utf_string_conversions.h" | |
| 16 #include "chrome/browser/autocomplete/autocomplete_provider.h" | |
| 17 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | |
| 18 #include "chrome/browser/bookmarks/bookmark_model.h" | |
| 19 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | |
| 20 #include "chrome/test/base/testing_profile.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 // The bookmark corpus against which we will simulate searches. | |
| 24 struct BookmarksTestInfo { | |
| 25 std::string title; | |
| 26 std::string url; | |
| 27 } bookmark_provider_test_data[] = { | |
| 28 { "abc def", "http://www.catsanddogs.com/a" }, | |
| 29 { "abcde", "http://www.catsanddogs.com/b" }, | |
| 30 { "abcdef", "http://www.catsanddogs.com/c" }, | |
| 31 { "a definition", "http://www.catsanddogs.com/d" }, | |
| 32 { "carry carbon carefully", "http://www.catsanddogs.com/e" }, | |
| 33 { "ghi jkl", "http://www.catsanddogs.com/f" }, | |
| 34 { "jkl ghi", "http://www.catsanddogs.com/g" }, | |
| 35 { "frankly frankly frank", "http://www.catsanddogs.com/h" }, | |
| 36 { "foobar foobar", "http://www.foobar.com/" }, | |
| 37 // For testing ranking with different URLs. | |
| 38 {"achlorhydric featherheads resuscitates mockingbirds", | |
| 39 "http://www.featherheads.com/a" }, | |
| 40 {"achlorhydric mockingbirds resuscitates featherhead", | |
| 41 "http://www.featherheads.com/b" }, | |
| 42 {"featherhead resuscitates achlorhydric mockingbirds", | |
| 43 "http://www.featherheads.com/c" }, | |
| 44 {"mockingbirds resuscitates featherheads achlorhydric", | |
| 45 "http://www.featherheads.com/d" }, | |
| 46 // For testing URL boosting. | |
| 47 {"burning worms #1", "http://www.burned.com/" }, | |
| 48 {"burning worms #2", "http://www.worms.com/" }, | |
| 49 {"worming burns #10", "http://www.burned.com/" }, | |
| 50 {"worming burns #20", "http://www.worms.com/" }, | |
| 51 {"jive music", "http://www.worms.com/" }, | |
| 52 }; | |
| 53 | |
| 54 class BookmarkProviderTest : public testing::Test, | |
| 55 public AutocompleteProviderListener { | |
| 56 public: | |
| 57 BookmarkProviderTest() : model_(new BookmarkModel(NULL)) {} | |
| 58 | |
| 59 // AutocompleteProviderListener: Not called. | |
| 60 virtual void OnProviderUpdate(bool updated_matches) OVERRIDE {} | |
| 61 | |
| 62 protected: | |
| 63 virtual void SetUp() OVERRIDE; | |
| 64 | |
| 65 scoped_ptr<TestingProfile> profile_; | |
| 66 scoped_ptr<BookmarkModel> model_; | |
| 67 scoped_refptr<BookmarkProvider> provider_; | |
| 68 | |
| 69 private: | |
| 70 DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest); | |
| 71 }; | |
| 72 | |
| 73 void BookmarkProviderTest::SetUp() { | |
| 74 profile_.reset(new TestingProfile()); | |
| 75 DCHECK(profile_.get()); | |
| 76 provider_ = new BookmarkProvider(this, profile_.get()); | |
| 77 DCHECK(provider_); | |
| 78 provider_->set_bookmark_model_for_testing(model_.get()); | |
| 79 | |
| 80 const BookmarkNode* other_node = model_->other_node(); | |
| 81 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(bookmark_provider_test_data); ++i) { | |
| 82 const BookmarksTestInfo& cur(bookmark_provider_test_data[i]); | |
| 83 const GURL url(cur.url); | |
| 84 model_->AddURL(other_node, other_node->child_count(), | |
| 85 ASCIIToUTF16(cur.title), url); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 // Structures and functions supporting the BookmarkProviderTest.Positions | |
| 90 // unit test. | |
| 91 | |
| 92 struct TestBookmarkPosition { | |
| 93 TestBookmarkPosition(size_t begin, size_t end) | |
| 94 : begin(begin), end(end) {} | |
| 95 | |
| 96 size_t begin; | |
| 97 size_t end; | |
| 98 }; | |
| 99 typedef std::vector<TestBookmarkPosition> TestBookmarkPositions; | |
| 100 | |
| 101 // Return |positions| as a formatted string for unit test diagnostic output. | |
| 102 std::string TestBookmarkPositionsAsString( | |
| 103 const TestBookmarkPositions& positions) { | |
| 104 std::string position_string("{"); | |
| 105 bool first = true; | |
| 106 for (TestBookmarkPositions::const_iterator i = positions.begin(); | |
| 107 i != positions.end(); ++i) { | |
| 108 if (first) | |
|
Peter Kasting
2012/10/15 22:17:22
Nit: Or just eliminate |first|:
if (i != positi
mrossetti
2012/10/16 16:38:05
Done.
| |
| 109 first = false; | |
| 110 else | |
| 111 position_string += ", "; | |
| 112 position_string += "{" + base::IntToString(i->begin) + ", " + | |
| 113 base::IntToString(i->end) + "}"; | |
| 114 } | |
| 115 position_string += "}"; | |
|
Peter Kasting
2012/10/15 22:17:22
Nit: Might as well tack on a \n too since callers
mrossetti
2012/10/16 16:38:05
Done.
| |
| 116 return position_string; | |
| 117 } | |
| 118 | |
| 119 // Return the positions in |matches| as a formatted string for unit test | |
| 120 // diagnostic output. | |
| 121 string16 MatchesAsString16(const ACMatches& matches) { | |
| 122 string16 matches_string; | |
| 123 for (ACMatches::const_iterator i = matches.begin(); i != matches.end(); ++i) { | |
| 124 matches_string.append(ASCIIToUTF16(" '")); | |
| 125 matches_string.append(i->description); | |
| 126 matches_string.append(ASCIIToUTF16("'\n")); | |
| 127 } | |
| 128 return matches_string; | |
| 129 } | |
| 130 | |
| 131 // Comparison function for sorting search terms by descending length. | |
| 132 bool TestBookmarkPositionsEqual(const TestBookmarkPosition& pos_a, | |
| 133 const TestBookmarkPosition& pos_b) { | |
| 134 return pos_a.begin == pos_b.begin && pos_a.end == pos_b.end; | |
| 135 } | |
| 136 | |
| 137 // Convience function to make comparing ACMatchClassifications against the | |
| 138 // test expectations structure easier. | |
| 139 TestBookmarkPositions PositionsFromAutocompleteMatch( | |
| 140 const AutocompleteMatch& match) { | |
| 141 TestBookmarkPositions positions; | |
| 142 bool started = false; | |
| 143 size_t start = 0; | |
| 144 for (AutocompleteMatch::ACMatchClassifications::const_iterator | |
| 145 i = match.description_class.begin(); | |
| 146 i != match.description_class.end(); ++i) { | |
| 147 if (i->style & AutocompleteMatch::ACMatchClassification::MATCH) { | |
| 148 // We have found the start of a match. | |
| 149 EXPECT_FALSE(started); | |
| 150 started = true; | |
| 151 start = i->offset; | |
| 152 } else if (started) { | |
| 153 // We have found the end of a match. | |
| 154 started = false; | |
| 155 positions.push_back(TestBookmarkPosition(start, i->offset)); | |
| 156 start = 0; | |
| 157 } | |
| 158 } | |
| 159 // Record the final position if the last match goes to the end of the | |
| 160 // candidate string. | |
| 161 if (started) | |
| 162 positions.push_back(TestBookmarkPosition(start, match.description.size())); | |
| 163 return positions; | |
| 164 } | |
| 165 | |
| 166 // Convience function to make comparing test expectations structure against the | |
| 167 // actual ACMatchClassifications easier. | |
| 168 TestBookmarkPositions PositionsFromExpectations( | |
| 169 const size_t expectations[9][2]) { | |
| 170 TestBookmarkPositions positions; | |
| 171 size_t i = 0; | |
| 172 // The array is zero-terminated in the [1]th element. | |
| 173 while (expectations[i][1]) { | |
| 174 positions.push_back( | |
| 175 TestBookmarkPosition(expectations[i][0], expectations[i][1])); | |
| 176 ++i; | |
| 177 } | |
| 178 return positions; | |
| 179 } | |
| 180 | |
| 181 TEST_F(BookmarkProviderTest, Positions) { | |
| 182 // Simulate searches. | |
| 183 // Description of |positions|: | |
| 184 // The first index represents the collection of positions for each expected | |
| 185 // match. The count of the actual subarrays in each instance of |query_data| | |
| 186 // must equal |match_count|. The second index represents each expected | |
| 187 // match position. The third index represents the |start| and |end| of the | |
| 188 // expected match's position within the |test_data|. This array must be | |
| 189 // terminated by an entry with a value of '0' for |end|. | |
| 190 // Example: | |
| 191 // Consider the line for 'def' below: | |
| 192 // {"def", 2, {{{4, 7}, {XXX, 0}}, {{2, 5}, {11, 14}, {XXX, 0}}}}, | |
| 193 // There are two expected matches: | |
| 194 // 0. {{4, 7}, {XXX, 0}} | |
| 195 // 1. {{2, 5}, {11 ,14}, {XXX, 0}} | |
| 196 // For the first match, [0], there is one match within the bookmark's title | |
| 197 // expected, {4, 7}, which maps to the 'def' within "abc def". The 'XXX' | |
| 198 // value is ignored. The second match, [1], indicates that two matches are | |
| 199 // expected within the bookmark title "a definite definition". In each case, | |
| 200 // the {XXX, 0} indicates the end of the subarray. Or: | |
| 201 // Match #1 Match #2 | |
| 202 // ------------------ ---------------------------- | |
| 203 // Pos1 Term Pos1 Pos2 Term | |
| 204 // ------ -------- ------ -------- -------- | |
| 205 // {"def", 2, {{{4, 7}, {999, 0}}, {{2, 5}, {11, 14}, {999, 0}}}}, | |
| 206 // | |
| 207 struct QueryData { | |
| 208 const std::string query; | |
| 209 const size_t match_count; // This count must match the number of major | |
| 210 // elements in the following |positions| array. | |
| 211 const size_t positions[99][9][2]; | |
| 212 } query_data[] = { | |
| 213 // This first set is primarily for position detection validation. | |
| 214 {"abc", 3, {{{0, 3}, {0, 0}}, | |
| 215 {{0, 3}, {0, 0}}, | |
| 216 {{0, 3}, {0, 0}}}}, | |
| 217 {"abcde", 2, {{{0, 5}, {0, 0}}, | |
| 218 {{0, 5}, {0, 0}}}}, | |
| 219 {"foo bar", 0, {{{0, 0}}}}, | |
| 220 {"fooey bark", 0, {{{0, 0}}}}, | |
| 221 {"def", 2, {{{2, 5}, {0, 0}}, | |
| 222 {{4, 7}, {0, 0}}}}, | |
| 223 {"ghi jkl", 2, {{{0, 3}, {4, 7}, {0, 0}}, | |
| 224 {{0, 3}, {4, 7}, {0, 0}}}}, | |
| 225 // NB: GetBookmarksWithTitlesMatching(...) uses exact match for "a". | |
| 226 {"a", 1, {{{0, 1}, {0, 0}}}}, | |
| 227 {"a d", 0, {{{0, 0}}}}, | |
| 228 {"carry carbon", 1, {{{0, 5}, {6, 12}, {0, 0}}}}, | |
| 229 // NB: GetBookmarksWithTitlesMatching(...) sorts the match positions. | |
| 230 {"carbon carry", 1, {{{0, 5}, {6, 12}, {0, 0}}}}, | |
| 231 {"arbon", 0, {{{0, 0}}}}, | |
| 232 {"ar", 0, {{{0, 0}}}}, | |
| 233 {"arry", 0, {{{0, 0}}}}, | |
| 234 // Quoted terms are single terms. | |
| 235 {"\"carry carbon\"", 1, {{{0, 12}, {0, 0}}}}, | |
| 236 {"\"carry carbon\" care", 1, {{{0, 12}, {13, 17}, {0, 0}}}}, | |
| 237 // Quoted terms require complete word matches. | |
| 238 {"\"carry carbo\"", 0, {{{0, 0}}}}, | |
| 239 // This set uses duplicated and/or overlaps search terms in the title. | |
| 240 {"frank", 1, {{{0, 5}, {8, 13}, {16, 21}, {0, 0}}}}, | |
| 241 {"frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}}, | |
| 242 {"frankly frankly", 1, {{{0, 7}, {8, 15}, {0, 0}}}}, | |
| 243 {"foobar foo", 1, {{{0, 6}, {7, 13}, {0, 0}}}}, | |
| 244 {"foo foobar", 1, {{{0, 6}, {7, 13}, {0, 0}}}}, | |
| 245 }; | |
| 246 | |
| 247 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { | |
| 248 AutocompleteInput input(ASCIIToUTF16(query_data[i].query), | |
| 249 string16(), false, false, false, | |
| 250 AutocompleteInput::ALL_MATCHES); | |
| 251 provider_->Start(input, false); | |
| 252 const ACMatches& matches(provider_->matches()); | |
| 253 // Validate number of results is as expected. | |
| 254 EXPECT_LE(matches.size(), query_data[i].match_count) | |
| 255 << "One or more of the following matches were unexpected:\n" | |
| 256 << MatchesAsString16(matches) | |
| 257 << "For query '" << query_data[i].query << "'."; | |
| 258 EXPECT_GE(matches.size(), query_data[i].match_count) | |
| 259 << "One or more expected matches are missing. Matches found:\n" | |
| 260 << MatchesAsString16(matches) | |
| 261 << "for query '" << query_data[i].query << "'."; | |
| 262 // Validate positions within each match is as expected. | |
| 263 for (size_t j = 0; j < matches.size(); ++j) { | |
| 264 // Collect the expected positions as a vector, collect the match's | |
| 265 // classifications for match positions as a vector, then compare. | |
| 266 TestBookmarkPositions expected_positions( | |
| 267 PositionsFromExpectations(query_data[i].positions[j])); | |
| 268 TestBookmarkPositions actual_positions( | |
| 269 PositionsFromAutocompleteMatch(matches[j])); | |
| 270 EXPECT_TRUE(std::equal(expected_positions.begin(), | |
| 271 expected_positions.end(), | |
| 272 actual_positions.begin(), | |
| 273 TestBookmarkPositionsEqual)) | |
| 274 << "EXPECTED: " << TestBookmarkPositionsAsString(expected_positions) | |
| 275 << "\nACTUAL: " << TestBookmarkPositionsAsString(actual_positions) | |
| 276 << "\n for query: '" << query_data[i].query << "'.";; | |
| 277 } | |
| 278 } | |
| 279 } | |
| 280 | |
| 281 TEST_F(BookmarkProviderTest, Rankings) { | |
| 282 // Simulate searches. | |
| 283 struct QueryData { | |
| 284 const std::string query; | |
| 285 // |match_count| must match the number of elements in the following | |
| 286 // |matches| array. | |
| 287 const size_t match_count; | |
| 288 // |matches| specifies the titles for all bookmarks expected to be matched | |
| 289 // by the |query| | |
| 290 const std::string matches[99]; | |
| 291 } query_data[] = { | |
| 292 // Basic ranking test. | |
| 293 {"abc", 3, {"abcde", // Most complete match. | |
| 294 "abcdef", | |
| 295 "abc def"}}, // Least complete match. | |
| 296 {"ghi", 2, {"ghi jkl", // Matched earlier. | |
| 297 "jkl ghi"}}, // Matched later. | |
| 298 // Rankings of exact-word matches with different URLs. | |
| 299 {"achlorhydric", | |
| 300 3, {"achlorhydric mockingbirds resuscitates featherhead", | |
| 301 "achlorhydric featherheads resuscitates mockingbirds", | |
| 302 "featherhead resuscitates achlorhydric mockingbirds"}}, | |
| 303 {"achlorhydric featherheads", | |
| 304 2, {"achlorhydric featherheads resuscitates mockingbirds", | |
| 305 "mockingbirds resuscitates featherheads achlorhydric"}}, | |
| 306 {"mockingbirds resuscitates", | |
| 307 3, {"mockingbirds resuscitates featherheads achlorhydric", | |
| 308 "achlorhydric mockingbirds resuscitates featherhead", | |
| 309 "featherhead resuscitates achlorhydric mockingbirds"}}, | |
| 310 // Ranking of exact-word matches with URL boost. | |
| 311 {"worms", 2, {"burning worms #2", // boosted | |
| 312 "burning worms #1"}}, // not boosted | |
| 313 // Ranking of prefix matches with URL boost. Note that a query of | |
| 314 // "worm burn" will have the same results. | |
| 315 {"burn worm", 3, {"burning worms #2", // boosted | |
| 316 "worming burns #20", // boosted | |
| 317 "burning worms #1"}}, // not boosted but shorter | |
| 318 }; | |
| 319 | |
| 320 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { | |
| 321 AutocompleteInput input(ASCIIToUTF16(query_data[i].query), | |
| 322 string16(), false, false, false, | |
| 323 AutocompleteInput::ALL_MATCHES); | |
| 324 provider_->Start(input, false); | |
| 325 const ACMatches& matches(provider_->matches()); | |
| 326 // Validate number and content of results is as expected. | |
| 327 for (size_t j = 0; j < std::max(query_data[i].match_count, matches.size()); | |
| 328 ++j) { | |
| 329 // Let's use a variable name that will make sense when reported by EXPECT. | |
| 330 bool results_match_expectations = true; | |
| 331 std::string failure; | |
| 332 if (j >= query_data[i].match_count) { | |
|
Peter Kasting
2012/10/15 22:17:22
Nit: Why not something more like:
EXPECT_LT(j,
mrossetti
2012/10/16 16:38:05
Okay, I can make that work. There really is only o
| |
| 333 // We have an unexpected match. | |
| 334 failure += " Unexpected match[" + base::IntToString(j) + "]: '" + | |
| 335 UTF16ToUTF8(matches[j].description) + "' for query: '" + | |
| 336 query_data[i].query + "'."; | |
| 337 results_match_expectations = false; | |
| 338 } else if (j >= matches.size()) { | |
| 339 // We have a missing match. | |
| 340 failure += " Missing match[" + base::IntToString(j) + "]: '" + | |
| 341 query_data[i].matches[j] + "' for query: '" + query_data[i].query + | |
| 342 "'."; | |
| 343 results_match_expectations = false; | |
| 344 } else if (query_data[i].matches[j] != | |
| 345 UTF16ToUTF8(matches[j].description)) { | |
| 346 // We have a mismatch. | |
| 347 failure += " Mismatch at [" + base::IntToString(j) + | |
| 348 "]: Expected '" + query_data[i].matches[j] + "' vs actual '" + | |
| 349 UTF16ToUTF8(matches[j].description) + "' for query '" + | |
| 350 query_data[i].query + "'."; | |
| 351 results_match_expectations = false; | |
| 352 } | |
| 353 EXPECT_TRUE(results_match_expectations) << failure; | |
| 354 } | |
| 355 } | |
| 356 } | |
| OLD | NEW |