|
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 <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 class BookmarkProviderTest : public testing::Test, | |
21 public AutocompleteProviderListener { | |
22 public: | |
23 BookmarkProviderTest() : model_(new BookmarkModel(NULL)) {} | |
24 | |
25 // AutocompleteProviderListener: Not called. | |
26 virtual void OnProviderUpdate(bool updated_matches) OVERRIDE {} | |
27 | |
28 protected: | |
29 virtual void SetUp() OVERRIDE; | |
30 virtual void TearDown() OVERRIDE; | |
31 | |
32 // Initializes test data. | |
33 void AddBookmarksWithTitles(const char** titles, size_t count); | |
34 void AddBookmarksWithTitles(const std::vector<std::string>& titles); | |
35 | |
36 scoped_ptr<TestingProfile> profile_; | |
37 scoped_ptr<BookmarkModel> model_; | |
38 scoped_refptr<BookmarkProvider> provider_; | |
39 | |
40 private: | |
41 DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest); | |
42 }; | |
43 | |
44 void BookmarkProviderTest::SetUp() { | |
45 profile_.reset(new TestingProfile()); | |
46 DCHECK(profile_.get()); | |
47 provider_ = new BookmarkProvider(this, profile_.get()); | |
48 DCHECK(provider_); | |
49 provider_->set_bookmark_model_for_testing(model_.get()); | |
50 } | |
51 | |
52 void BookmarkProviderTest::TearDown() { | |
53 } | |
54 | |
55 void BookmarkProviderTest::AddBookmarksWithTitles(const char** titles, | |
56 size_t count) { | |
57 std::vector<std::string> title_vector; | |
58 for (size_t i = 0; i < count; ++i) | |
59 title_vector.push_back(titles[i]); | |
60 AddBookmarksWithTitles(title_vector); | |
61 } | |
62 | |
63 void BookmarkProviderTest::AddBookmarksWithTitles( | |
64 const std::vector<std::string>& titles) { | |
65 GURL url("about:blank"); | |
66 for (size_t i = 0; i < titles.size(); ++i) { | |
67 model_->AddURL(model_->other_node(), static_cast<int>(i), | |
68 ASCIIToUTF16(titles[i]), url); | |
69 } | |
70 } | |
71 | |
72 // Structures and functions supporting the BookmarkProviderTest.Positions | |
73 // unit test. | |
74 | |
75 struct TestBookmarkPosition { | |
76 TestBookmarkPosition(size_t from, size_t to) | |
Peter Kasting
2012/10/04 20:40:31
Nit: Use the same arg names as your member names (
mrossetti
2012/10/05 22:10:04
Done.
| |
77 : begin(from), end(to) {} | |
78 size_t begin; | |
79 size_t end; | |
80 }; | |
81 typedef std::vector<TestBookmarkPosition> TestBookmarkPositions; | |
82 | |
83 // Comparison function for sorting search terms by descending length. | |
84 bool TestBookmarkPositionsEqual(const TestBookmarkPosition& pos_a, | |
85 const TestBookmarkPosition& pos_b) { | |
86 return pos_a.begin == pos_b.begin && pos_a.end == pos_b.end; | |
87 } | |
88 | |
89 TestBookmarkPositions PositionsFromAutocompleteMatch( | |
90 const AutocompleteMatch& match) { | |
91 TestBookmarkPositions positions; | |
92 bool started = false; | |
93 size_t start = 0; | |
94 for (AutocompleteMatch::ACMatchClassifications::const_iterator | |
95 i = match.description_class.begin(); | |
96 i != match.description_class.end(); ++i) { | |
97 if (i->style & AutocompleteMatch::ACMatchClassification::MATCH) { | |
98 // We have found the start of a match. | |
99 EXPECT_FALSE(started); | |
100 started = true; | |
101 start = i->offset; | |
102 } else if (started) { | |
103 // We have found the end of a match. | |
104 started = false; | |
105 positions.push_back(TestBookmarkPosition(start, i->offset)); | |
106 start = 0; | |
107 } | |
108 } | |
109 // Record the final position if the last match goes to the end of the | |
110 // candidate string. | |
111 if (started) | |
112 positions.push_back(TestBookmarkPosition(start, match.description.size())); | |
113 return positions; | |
114 } | |
115 | |
116 TestBookmarkPositions PositionsFromExpectations( | |
117 const size_t expectations[9][2]) { | |
118 TestBookmarkPositions positions; | |
119 size_t i = 0; | |
120 // The array is zero-terminated in the [1]th element. | |
121 while (expectations[i][1]) { | |
122 positions.push_back( | |
123 TestBookmarkPosition(expectations[i][0], expectations[i][1])); | |
124 ++i; | |
125 } | |
126 return positions; | |
127 } | |
128 | |
129 TEST_F(BookmarkProviderTest, Positions) { | |
130 // Create the bookmark corpus against which we will simulate searches. | |
131 const char* test_data[] = { | |
132 "abc def", | |
133 "abcde", | |
134 "abcdef", | |
135 "a definition", | |
136 "carry carbon", | |
137 }; | |
138 AddBookmarksWithTitles(test_data, ARRAYSIZE_UNSAFE(test_data)); | |
139 | |
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. This array must be terminated by an entry with a value | |
146 // of '0' for |end|. The third index represents the |start| and |end| of | |
147 // the expected match's position within the |test_data|. | |
148 // Example: | |
149 // Consider the line for 'def' below: | |
150 // {"def", 2, {{{4, 7}, {0, 0}}, {{2, 5}, {11, 14}, {0, 0}}}}, | |
151 // There are two expected matches: | |
152 // 0. {{4, 7}, {0, 0}} | |
153 // 1. {{2, 5}, {11 ,14}, {0, 0}} | |
154 // For the first match, [0], there is one match within the bookmark's title | |
155 // expected, {4,7}, which maps to the 'def' within "abc def". | |
156 // The second match, [1], indicates that two matches are expected within | |
157 // the bookmark title "a definite definition". In each case, the {0,0} | |
158 // indicates the end of the subarray. Or: | |
159 // Match #1 Match #2 | |
160 // ---------------- -------------------------- | |
161 // Pos1 Term Pos1 Pos2 Term | |
162 // ------ ------ ------ -------- ------ | |
163 // {"def", 2, {{{4, 7}, {0, 0}}, {{2, 5}, {11, 14}, {0, 0}}}}, | |
164 // | |
165 struct QueryData { | |
166 const std::string query; | |
167 const size_t match_count; | |
168 const size_t positions[99][9][2]; | |
169 } query_data[] = { | |
170 {"abc", 3, {{{0, 3}, {0, 0}}, | |
171 {{0, 3}, {0, 0}}, | |
172 {{0, 3}, {0, 0}}}}, | |
173 {"foo bar", 0, {{{0, 0}}}}, | |
174 {"fooey bark", 0, {{{0, 0}}}}, | |
175 {"def", 2, {{{4, 7}, {0, 0}}, | |
176 {{2, 5}, {0, 0}}}}, | |
177 // NB: GetBookmarksWithTitlesMatching(...) uses exact match for "a". | |
178 {"a", 1, {{{0, 1}, {0, 0}}}}, | |
179 {"a d", 0, {{{0, 0}}}}, | |
180 {"carry carbon", 1, {{{0, 5}, {6, 12}, {0, 0}}}}, | |
181 // NB: GetBookmarksWithTitlesMatching(...) sorts the match positions. | |
182 {"carbon carry", 1, {{{0, 5}, {6, 12}, {0, 0}}}}, | |
183 {"arbon", 0, {{{0, 0}}}}, | |
184 {"ar", 0, {{{0, 0}}}}, | |
185 {"arry", 0, {{{0, 0}}}}, | |
186 }; | |
187 | |
188 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { | |
189 // WHAT??? | |
Peter Kasting
2012/10/04 20:40:31
??
mrossetti
2012/10/05 22:10:04
Leftovers removed.
| |
190 AutocompleteInput input(ASCIIToUTF16(query_data[i].query), | |
191 ASCIIToUTF16(""), false, false, false, | |
Peter Kasting
2012/10/04 20:40:31
Nit: string16()
mrossetti
2012/10/05 22:10:04
Done.
| |
192 AutocompleteInput::ALL_MATCHES); | |
193 provider_->Start(input, false); | |
194 const ACMatches& matches(provider_->matches()); | |
195 // Validate number of results is as expected. | |
196 EXPECT_EQ(query_data[i].match_count, matches.size()); | |
197 if (query_data[i].match_count != matches.size()) { | |
198 // Log the actual matches to aid in diagnosis. | |
199 LOG(ERROR) << "One or more of the following were unexpected:"; | |
200 for (ACMatches::const_iterator j = matches.begin(); j != matches.end(); | |
201 ++j) | |
202 LOG(ERROR) << " '" << j->description << "'"; | |
203 LOG(ERROR) << "For the search term: '" << query_data[i].query << "'"; | |
204 } | |
205 // Validate positions within each match is as expected. | |
206 for (size_t j = 0; j <matches.size(); ++j) { | |
207 // Collect the expected positions as a vector, collect the match's | |
208 // classifications for match positions as a vector, then compare. | |
209 TestBookmarkPositions expected_positions( | |
210 PositionsFromExpectations(query_data[i].positions[j])); | |
211 TestBookmarkPositions actual_positions( | |
212 PositionsFromAutocompleteMatch(matches[j])); | |
213 EXPECT_TRUE(std::equal(expected_positions.begin(), | |
214 expected_positions.end(), | |
215 actual_positions.begin(), | |
216 TestBookmarkPositionsEqual)); | |
217 } | |
218 } | |
219 } | |
OLD | NEW |