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

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: 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 { "abcdefghijklmnopqrstuvwx bcdefghijklmnopqrstuvwxy "
33 "cdefghijklmnopqrstuvwxyz defghijklmnopqrstuvwxyz01",
34 "http://www.catsanddogs.com/h" },
35 };
36
37 class BookmarkProviderTest : public testing::Test,
38 public AutocompleteProviderListener {
39 public:
40 BookmarkProviderTest() : model_(new BookmarkModel(NULL)) {}
41
42 // AutocompleteProviderListener: Not called.
43 virtual void OnProviderUpdate(bool updated_matches) OVERRIDE {}
44
45 protected:
46 virtual void SetUp() OVERRIDE;
47 virtual void TearDown() 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 void BookmarkProviderTest::TearDown() {
74 }
75
76 // Structures and functions supporting the BookmarkProviderTest.Positions
77 // unit test.
78
79 struct TestBookmarkPosition {
80 TestBookmarkPosition(size_t begin, size_t end)
81 : begin(begin), end(end) {}
82 size_t begin;
83 size_t end;
84 };
85 typedef std::vector<TestBookmarkPosition> TestBookmarkPositions;
86
87 // Comparison function for sorting search terms by descending length.
88 bool TestBookmarkPositionsEqual(const TestBookmarkPosition& pos_a,
89 const TestBookmarkPosition& pos_b) {
90 return pos_a.begin == pos_b.begin && pos_a.end == pos_b.end;
91 }
92
93 // Convience function to make comparing ACMatchClassifications against the
94 // test expectations structure easier.
95 TestBookmarkPositions PositionsFromAutocompleteMatch(
96 const AutocompleteMatch& match) {
97 TestBookmarkPositions positions;
98 bool started = false;
99 size_t start = 0;
100 for (AutocompleteMatch::ACMatchClassifications::const_iterator
101 i = match.description_class.begin();
102 i != match.description_class.end(); ++i) {
103 if (i->style & AutocompleteMatch::ACMatchClassification::MATCH) {
104 // We have found the start of a match.
105 EXPECT_FALSE(started);
106 started = true;
107 start = i->offset;
108 } else if (started) {
109 // We have found the end of a match.
110 started = false;
111 positions.push_back(TestBookmarkPosition(start, i->offset));
112 start = 0;
113 }
114 }
115 // Record the final position if the last match goes to the end of the
116 // candidate string.
117 if (started)
118 positions.push_back(TestBookmarkPosition(start, match.description.size()));
119 return positions;
120 }
121
122 // Convience function to make comparing test expectations structure against the
123 // actual ACMatchClassifications easier.
124 TestBookmarkPositions PositionsFromExpectations(
125 const size_t expectations[9][2], int* score) {
126 TestBookmarkPositions positions;
127 size_t i = 0;
128 // The array is zero-terminated in the [1]th element.
129 while (expectations[i][1]) {
130 positions.push_back(
131 TestBookmarkPosition(expectations[i][0], expectations[i][1]));
132 ++i;
133 }
134 if (score)
135 *score = static_cast<int>(expectations[i][0]);
136 return positions;
137 }
138
139 TEST_F(BookmarkProviderTest, PositionsAndScores) {
140 // Simulate searches.
141 // Description of |positions|:
142 // The first index represents the collection of positions for each expected
143 // match. The count of the actual subarrays in each instance of |query_data|
144 // must equal |match_count|. The second index represents each expected
145 // match position. The third index represents the |start| and |end| of the
146 // expected match's position within the |test_data|. This array must be
147 // terminated by an entry with a value of '0' for |end| and the expected
148 // score for |begin|. If the score is zero then do not test the score for
149 // the match.
150 // Example:
151 // Consider the line for 'def' below:
152 // {"def", 2, {{{4, 7}, {XXX, 0}}, {{2, 5}, {11, 14}, {XXX, 0}}}},
153 // There are two expected matches:
154 // 0. {{4, 7}, {XXX, 0}}
155 // 1. {{2, 5}, {11 ,14}, {XXX, 0}}
156 // For the first match, [0], there is one match within the bookmark's title
157 // expected, {4, 7}, which maps to the 'def' within "abc def". The expected
158 // score is XXX. The second match, [1], indicates that two matches are
159 // expected within the bookmark title "a definite definition". In each case,
160 // the {score, 0} indicates the end of the subarray. Or:
161 // Match #1 Match #2
162 // ------------------ ----------------------------
163 // Pos1 Term Pos1 Pos2 Term
164 // ------ -------- ------ -------- --------
165 // {"def", 2, {{{4, 7}, {XXX, 0}}, {{2, 5}, {11, 14}, {XXX, 0}}}},
166 //
167 struct QueryData {
168 const std::string query;
169 const size_t match_count;
170 const size_t positions[99][9][2];
171 } query_data[] = {
172 // This first set is primarily for position detection validation.
173 {"abc", 3, {{{0, 3}, {0, 0}},
174 {{0, 3}, {0, 0}},
175 {{0, 3}, {0, 0}}}},
176 {"abcde", 3, {{{0, 5}, {1199, 0}},
177 {{0, 5}, {1149, 0}},
178 {{0, 5}, {914, 0}}}},
179 {"foo bar", 0, {{{0, 0}}}},
180 {"fooey bark", 0, {{{0, 0}}}},
181 {"def", 3, {{{2, 5}, {0, 0}},
182 {{4, 7}, {0, 0}},
183 {{75, 78}, {902, 0}}}},
184 {"ghi jkl", 2, {{{0, 3}, {4, 7}, {0, 0}},
185 {{0, 3}, {4, 7}, {0, 0}}}},
186 // NB: GetBookmarksWithTitlesMatching(...) uses exact match for "a".
187 {"a", 1, {{{0, 1}, {0, 0}}}},
188 {"a d", 0, {{{0, 0}}}},
189 {"carry carbon", 1, {{{0, 5}, {6, 12}, {0, 0}}}},
190 // Quoted terms are single terms.
191 {"\"carry carbon\"", 1, {{{0, 12}, {0, 0}}}},
192 {"\"carry carbon\" care", 1, {{{0, 12}, {13, 17}, {0, 0}}}},
193 // Quoted terms require complete word matches.
194 {"\"carry carbo\"", 0, {{{0, 0}}}},
195 // NB: GetBookmarksWithTitlesMatching(...) sorts the match positions.
196 {"carbon carry", 1, {{{0, 5}, {6, 12}, {0, 0}}}},
197 {"arbon", 0, {{{0, 0}}}},
198 {"ar", 0, {{{0, 0}}}},
199 {"arry", 0, {{{0, 0}}}},
200 // This next set is for scoring validation of single term matches.
201 // To hand-calculate the expected scores, as has been done below, use the
202 // following formula:
203 //
204 // factor = match_len / title_len * (title_len - match_pos) / title_len
205 //
206 // Round factor down then multiply by 299, the available score range.
207 // Finally, add 900, the base score.
208 {"abcdefghijklmnopqrstuvwx", 1, {{{0, 24}, {971, 0}}}},
209 {"bcdefghijklmnopqrstuvwxy", 1, {{{25, 49}, {953, 0}}}},
210 {"cdefghijklmnopqrstuvwxyz", 1, {{{50, 74}, {935, 0}}}},
211 {"defghijklmnopqrstuvwxyz01", 1, {{{75, 100}, {918, 0}}}},
212 // This next set is for scoring validation of multiple term matches.
213 // To hand-calculate the expected scores, as has been done below, use the
214 // formula above to calculate a factor for each term match, calculate the
215 // sum of all factors, round the resulting factor down, multiply by
216 // 299 and add 900.
217 {"abcdefghijklmnopqrstuvwx bcdefghijklmnopqrstuvwxy",
218 1, {{{0, 24}, {25, 49}, {1025, 0}}}},
219 {"abcdefghijklmnopqrstuvwx cdefghijklmnopqrstuvwxyz",
220 1, {{{0, 24}, {50, 74}, {1007, 0}}}},
221 {"abcdefghijklmnopqrstuvwx defghijklmnopqrstuvwxyz01",
222 1, {{{0, 24}, {75, 100}, {990, 0}}}},
223 {"bcdefghijklmnopqrstuvwxy cdefghijklmnopqrstuvwxyz",
224 1, {{{25, 49}, {50, 74}, {989, 0}}}},
225 {"bcdefghijklmnopqrstuvwxy defghijklmnopqrstuvwxyz01",
226 1, {{{25, 49}, {75, 100}, {972, 0}}}},
227 {"cdefghijklmnopqrstuvwxyz defghijklmnopqrstuvwxyz01",
228 1, {{{50, 74}, {75, 100}, {954, 0}}}},
229 };
230
231 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) {
232 AutocompleteInput input(ASCIIToUTF16(query_data[i].query),
233 string16(), false, false, false,
234 AutocompleteInput::ALL_MATCHES);
235 provider_->Start(input, false);
236 const ACMatches& matches(provider_->matches());
237 // Validate number of results is as expected.
238 EXPECT_EQ(query_data[i].match_count, matches.size());
239 if (query_data[i].match_count != matches.size()) {
240 // Log the actual matches to aid in diagnosis.
241 LOG(ERROR) << "One or more of the following were unexpected:";
242 for (ACMatches::const_iterator j = matches.begin(); j != matches.end();
243 ++j)
244 LOG(ERROR) << " '" << j->description << "'";
245 LOG(ERROR) << "For the search term: '" << query_data[i].query << "'";
246 }
247 // Validate positions within each match and final score is as expected.
248 for (size_t j = 0; j < matches.size(); ++j) {
249 // Collect the expected positions as a vector, collect the match's
250 // classifications for match positions as a vector, then compare.
251 int expected_score = 0;
252 TestBookmarkPositions expected_positions(
253 PositionsFromExpectations(query_data[i].positions[j],
254 &expected_score));
255 TestBookmarkPositions actual_positions(
256 PositionsFromAutocompleteMatch(matches[j]));
257 EXPECT_TRUE(std::equal(expected_positions.begin(),
258 expected_positions.end(),
259 actual_positions.begin(),
260 TestBookmarkPositionsEqual))
261 << " for match[" << j << "] ('" << matches[j].description
262 << "') for query[" << i
263 << "] ('" << query_data[i].query << "').";
264 if (expected_score != 0)
265 EXPECT_EQ(expected_score, matches[j].relevance)
266 << " for match[" << j << "] ('" << matches[j].description
267 << "') for query[" << i
268 << "] ('" << query_data[i].query << "').";
269 }
270 }
271 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698