Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(643)

Side by Side Diff: chrome/browser/autocomplete/bookmark_provider_unittest.cc

Issue 10913262: Implement Bookmark Autocomplete Provider (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Now handles duplicate term matching. Comments, etc. Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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 <vector>
9
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/autocomplete/autocomplete_provider.h"
14 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
15 #include "chrome/browser/bookmarks/bookmark_model.h"
16 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 // The bookmark corpus against which we will simulate searches.
21 struct BookmarksTestInfo {
22 std::string title;
23 std::string url;
24 } bookmark_provider_test_data[] = {
25 { "abc def", "http://www.catsanddogs.com/a" },
26 { "abcde", "http://www.catsanddogs.com/b" },
27 { "abcdef", "http://www.catsanddogs.com/c" },
28 { "a definition", "http://www.catsanddogs.com/d" },
29 { "carry carbon carefully", "http://www.catsanddogs.com/e" },
30 { "ghi jkl", "http://www.catsanddogs.com/f" },
31 { "jkl ghi", "http://www.catsanddogs.com/g" },
32 { "frankly frankly frank", "http://www.catsanddogs.com/h" },
33 { "abcdefghijklmnopqrstuvwx bcdefghijklmnopqrstuvwxy "
34 "cdefghijklmnopqrstuvwxyz defghijklmnopqrstuvwxyz01",
35 "http://www.catsanddogs.com/i" },
36 };
37
38 class BookmarkProviderTest : public testing::Test,
39 public AutocompleteProviderListener {
40 public:
41 BookmarkProviderTest() : model_(new BookmarkModel(NULL)) {}
42
43 // AutocompleteProviderListener: Not called.
44 virtual void OnProviderUpdate(bool updated_matches) OVERRIDE {}
45
46 protected:
47 virtual void SetUp() OVERRIDE;
48
49 scoped_ptr<TestingProfile> profile_;
50 scoped_ptr<BookmarkModel> model_;
51 scoped_refptr<BookmarkProvider> provider_;
52
53 private:
54 DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest);
55 };
56
57 void BookmarkProviderTest::SetUp() {
58 profile_.reset(new TestingProfile());
59 DCHECK(profile_.get());
60 provider_ = new BookmarkProvider(this, profile_.get());
61 DCHECK(provider_);
62 provider_->set_bookmark_model_for_testing(model_.get());
63
64 const BookmarkNode* other_node = model_->other_node();
65 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(bookmark_provider_test_data); ++i) {
66 const BookmarksTestInfo& cur(bookmark_provider_test_data[i]);
67 const GURL url(cur.url);
68 model_->AddURL(other_node, other_node->child_count(),
69 ASCIIToUTF16(cur.title), url);
70 }
71 }
72
73 // Structures and functions supporting the BookmarkProviderTest.Positions
74 // unit test.
75
76 struct TestBookmarkPosition {
77 TestBookmarkPosition(size_t begin, size_t end)
78 : begin(begin), end(end) {}
79
80 // Log the positions for unit test diagnostics.
Peter Kasting 2012/10/10 18:29:06 Nit: Is this a comment about the class as a whole?
mrossetti 2012/10/15 19:22:46 Removed leftovers. On 2012/10/10 18:29:06, Peter
81
82 size_t begin;
83 size_t end;
84 };
85 typedef std::vector<TestBookmarkPosition> TestBookmarkPositions;
86
87 void DumpTestBookmarkPositions(const char* title,
88 const TestBookmarkPositions& positions) {
89 LOG(WARNING) << title << ":";
Peter Kasting 2012/10/10 18:29:06 Using LOG(WARNING) directly doesn't seem right. C
mrossetti 2012/10/15 19:22:46 Great idea! Done. On 2012/10/10 18:29:06, Peter K
90 for (TestBookmarkPositions::const_iterator i = positions.begin();
91 i != positions.end(); ++i) {
92 LOG(WARNING) << "{" << i->begin << ", " << i->end << "}";
93 }
94 }
95
96 // Comparison function for sorting search terms by descending length.
97 bool TestBookmarkPositionsEqual(const TestBookmarkPosition& pos_a,
98 const TestBookmarkPosition& pos_b) {
99 return pos_a.begin == pos_b.begin && pos_a.end == pos_b.end;
100 }
101
102 // Convience function to make comparing ACMatchClassifications against the
103 // test expectations structure easier.
104 TestBookmarkPositions PositionsFromAutocompleteMatch(
105 const AutocompleteMatch& match) {
106 TestBookmarkPositions positions;
107 bool started = false;
108 size_t start = 0;
109 for (AutocompleteMatch::ACMatchClassifications::const_iterator
110 i = match.description_class.begin();
111 i != match.description_class.end(); ++i) {
112 if (i->style & AutocompleteMatch::ACMatchClassification::MATCH) {
113 // We have found the start of a match.
114 EXPECT_FALSE(started);
115 started = true;
116 start = i->offset;
117 } else if (started) {
118 // We have found the end of a match.
119 started = false;
120 positions.push_back(TestBookmarkPosition(start, i->offset));
121 start = 0;
122 }
123 }
124 // Record the final position if the last match goes to the end of the
125 // candidate string.
126 if (started)
127 positions.push_back(TestBookmarkPosition(start, match.description.size()));
128 return positions;
129 }
130
131 // Convience function to make comparing test expectations structure against the
132 // actual ACMatchClassifications easier.
133 TestBookmarkPositions PositionsFromExpectations(
134 const size_t expectations[9][2], int* score) {
Peter Kasting 2012/10/10 18:29:06 Nit: One arg per line
mrossetti 2012/10/15 19:22:46 Done.
135 TestBookmarkPositions positions;
136 size_t i = 0;
137 // The array is zero-terminated in the [1]th element.
138 while (expectations[i][1]) {
139 positions.push_back(
140 TestBookmarkPosition(expectations[i][0], expectations[i][1]));
141 ++i;
142 }
143 if (score)
144 *score = static_cast<int>(expectations[i][0]);
145 return positions;
146 }
147
148 TEST_F(BookmarkProviderTest, PositionsAndScores) {
149 // Simulate searches.
150 // Description of |positions|:
151 // The first index represents the collection of positions for each expected
152 // match. The count of the actual subarrays in each instance of |query_data|
153 // must equal |match_count|. The second index represents each expected
154 // match position. The third index represents the |start| and |end| of the
155 // expected match's position within the |test_data|. This array must be
156 // terminated by an entry with a value of '0' for |end| and the expected
157 // score for |begin|. If the score is zero then do not test the score for
158 // the match.
159 // Example:
160 // Consider the line for 'def' below:
161 // {"def", 2, {{{4, 7}, {XXX, 0}}, {{2, 5}, {11, 14}, {XXX, 0}}}},
162 // There are two expected matches:
163 // 0. {{4, 7}, {XXX, 0}}
164 // 1. {{2, 5}, {11 ,14}, {XXX, 0}}
165 // For the first match, [0], there is one match within the bookmark's title
166 // expected, {4, 7}, which maps to the 'def' within "abc def". The expected
167 // score is XXX. The second match, [1], indicates that two matches are
168 // expected within the bookmark title "a definite definition". In each case,
169 // the {score, 0} indicates the end of the subarray. Or:
170 // Match #1 Match #2
171 // ------------------ ----------------------------
172 // Pos1 Term Pos1 Pos2 Term
173 // ------ -------- ------ -------- --------
174 // {"def", 2, {{{4, 7}, {XXX, 0}}, {{2, 5}, {11, 14}, {XXX, 0}}}},
175 //
176 struct QueryData {
177 const std::string query;
178 const size_t match_count;
179 const size_t positions[99][9][2];
180 } query_data[] = {
181 // This first set is primarily for position detection validation.
182 {"abc", 3, {{{0, 3}, {0, 0}},
183 {{0, 3}, {0, 0}},
184 {{0, 3}, {0, 0}}}},
185 {"abcde", 3, {{{0, 5}, {1199, 0}},
186 {{0, 5}, {1149, 0}},
187 {{0, 5}, {914, 0}}}},
188 {"foo bar", 0, {{{0, 0}}}},
189 {"fooey bark", 0, {{{0, 0}}}},
190 {"def", 3, {{{2, 5}, {0, 0}},
191 {{4, 7}, {0, 0}},
192 {{75, 78}, {902, 0}}}},
193 {"ghi jkl", 2, {{{0, 3}, {4, 7}, {0, 0}},
194 {{0, 3}, {4, 7}, {0, 0}}}},
195 // NB: GetBookmarksWithTitlesMatching(...) uses exact match for "a".
196 {"a", 1, {{{0, 1}, {0, 0}}}},
197 {"a d", 0, {{{0, 0}}}},
198 {"carry carbon", 1, {{{0, 5}, {6, 12}, {0, 0}}}},
199 // NB: GetBookmarksWithTitlesMatching(...) sorts the match positions.
200 {"carbon carry", 1, {{{0, 5}, {6, 12}, {0, 0}}}},
201 {"arbon", 0, {{{0, 0}}}},
202 {"ar", 0, {{{0, 0}}}},
203 {"arry", 0, {{{0, 0}}}},
204 // Quoted terms are single terms.
205 {"\"carry carbon\"", 1, {{{0, 12}, {0, 0}}}},
206 {"\"carry carbon\" care", 1, {{{0, 12}, {13, 17}, {0, 0}}}},
207 // Quoted terms require complete word matches.
208 {"\"carry carbo\"", 0, {{{0, 0}}}},
209 // This next set is for scoring validation of single term matches.
Peter Kasting 2012/10/10 18:29:06 I would really rather not expect precise scores fr
mrossetti 2012/10/15 19:22:46 Okay. Done. On 2012/10/10 18:29:06, Peter Kasting
210 // To hand-calculate the expected scores, as has been done below, use the
211 // following formula:
212 //
213 // factor = match_len / title_len * (title_len - match_pos) / title_len
214 //
215 // Multiply 299, the available score range, by the factor and round down.
216 // Finally, add 900, the base score.
217 {"abcdefghijklmnopqrstuvwx", 1, {{{0, 24}, {971, 0}}}},
218 {"bcdefghijklmnopqrstuvwxy", 1, {{{25, 49}, {953, 0}}}},
219 {"cdefghijklmnopqrstuvwxyz", 1, {{{50, 74}, {935, 0}}}},
220 {"defghijklmnopqrstuvwxyz01", 1, {{{75, 100}, {918, 0}}}},
221 // This next set is for scoring validation of multiple term matches.
222 // To hand-calculate the expected scores, as has been done below, use the
223 // formula above to calculate a factor for each term match, calculate the
224 // sum of all factors, round the resulting factor down, multiply by
225 // 299 and add 900.
226 {"abcdefghijklmnopqrstuvwx bcdefghijklmnopqrstuvwxy",
227 1, {{{0, 24}, {25, 49}, {1025, 0}}}},
228 {"abcdefghijklmnopqrstuvwx cdefghijklmnopqrstuvwxyz",
229 1, {{{0, 24}, {50, 74}, {1007, 0}}}},
230 {"abcdefghijklmnopqrstuvwx defghijklmnopqrstuvwxyz01",
231 1, {{{0, 24}, {75, 100}, {990, 0}}}},
232 {"bcdefghijklmnopqrstuvwxy cdefghijklmnopqrstuvwxyz",
233 1, {{{25, 49}, {50, 74}, {989, 0}}}},
234 {"bcdefghijklmnopqrstuvwxy defghijklmnopqrstuvwxyz01",
235 1, {{{25, 49}, {75, 100}, {972, 0}}}},
236 {"cdefghijklmnopqrstuvwxyz defghijklmnopqrstuvwxyz01",
237 1, {{{50, 74}, {75, 100}, {954, 0}}}},
238 // This set uses duplicated search terms.
239 {"frank", 1, {{{0, 5}, {8, 13}, {16, 21}, {1032, 0}}}},
240 {"frankly", 1, {{{0, 7}, {8, 15}, {1061, 0}}}},
241 {"frankly frankly", 1, {{{0, 7}, {8, 15}, {1061, 0}}}},
242 };
243
244 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) {
245 AutocompleteInput input(ASCIIToUTF16(query_data[i].query),
246 string16(), false, false, false,
247 AutocompleteInput::ALL_MATCHES);
248 provider_->Start(input, false);
249 const ACMatches& matches(provider_->matches());
250 // Validate number of results is as expected.
251 EXPECT_EQ(query_data[i].match_count, matches.size());
252 if (query_data[i].match_count != matches.size()) {
253 // Log the actual matches to aid in diagnosis.
254 LOG(ERROR) << "One or more of the following were unexpected:";
255 for (ACMatches::const_iterator j = matches.begin(); j != matches.end();
256 ++j)
257 LOG(ERROR) << " '" << j->description << "'";
258 LOG(ERROR) << "For the search term: '" << query_data[i].query << "'";
259 }
260 // Validate positions within each match and final score is as expected.
261 for (size_t j = 0; j < matches.size(); ++j) {
262 // Collect the expected positions as a vector, collect the match's
263 // classifications for match positions as a vector, then compare.
264 int expected_score = 0;
265 TestBookmarkPositions expected_positions(
266 PositionsFromExpectations(query_data[i].positions[j],
267 &expected_score));
268 TestBookmarkPositions actual_positions(
269 PositionsFromAutocompleteMatch(matches[j]));
270 bool positions_equal = std::equal(expected_positions.begin(),
271 expected_positions.end(),
272 actual_positions.begin(),
273 TestBookmarkPositionsEqual);
274 EXPECT_TRUE(positions_equal)
275 << " for match[" << j << "] ('" << matches[j].description
276 << "') for query[" << i
277 << "] ('" << query_data[i].query << "').";
278 if (!positions_equal) {
279 DumpTestBookmarkPositions("EXPECTED", expected_positions);
280 DumpTestBookmarkPositions("ACTUAL", actual_positions);
281 }
282 if (expected_score != 0)
283 EXPECT_EQ(expected_score, matches[j].relevance)
284 << " for match[" << j << "] ('" << matches[j].description
285 << "') for query[" << i
286 << "] ('" << query_data[i].query << "').";
287 }
288 }
289 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698