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/bookmarks/bookmark_index.h" | 5 #include "chrome/browser/bookmarks/bookmark_index.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/metrics/field_trial.h" |
11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
15 #include "chrome/browser/bookmarks/bookmark_model.h" | 16 #include "chrome/browser/bookmarks/bookmark_model.h" |
16 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 17 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
17 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" | 18 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" |
18 #include "chrome/browser/history/history_service.h" | 19 #include "chrome/browser/history/history_service.h" |
19 #include "chrome/browser/history/history_service_factory.h" | 20 #include "chrome/browser/history/history_service_factory.h" |
20 #include "chrome/browser/history/url_database.h" | 21 #include "chrome/browser/history/url_database.h" |
| 22 #include "chrome/browser/omnibox/omnibox_field_trial.h" |
| 23 #include "chrome/common/metrics/variations/variations_util.h" |
21 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
22 #include "components/bookmarks/core/browser/bookmark_title_match.h" | 25 #include "components/bookmarks/core/browser/bookmark_match.h" |
| 26 #include "components/variations/entropy_provider.h" |
23 #include "content/public/test/test_browser_thread_bundle.h" | 27 #include "content/public/test/test_browser_thread_bundle.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
25 | 29 |
26 using base::ASCIIToUTF16; | 30 using base::ASCIIToUTF16; |
27 | 31 |
28 class BookmarkIndexTest : public testing::Test { | 32 class BookmarkIndexTest : public testing::Test { |
29 public: | 33 public: |
30 BookmarkIndexTest() : model_(new BookmarkModel(NULL)) {} | 34 BookmarkIndexTest() : model_(new BookmarkModel(NULL)) { |
31 | 35 // Destroy the existing FieldTrialList before creating a new one to avoid |
32 void AddBookmarksWithTitles(const char** titles, size_t count) { | 36 // a DCHECK. |
33 std::vector<std::string> title_vector; | 37 field_trial_list_.reset(); |
34 for (size_t i = 0; i < count; ++i) | 38 field_trial_list_.reset(new base::FieldTrialList( |
35 title_vector.push_back(titles[i]); | 39 new metrics::SHA1EntropyProvider("foo"))); |
36 AddBookmarksWithTitles(title_vector); | 40 chrome_variations::testing::ClearAllVariationParams(); |
37 } | 41 } |
38 | 42 |
39 void AddBookmarksWithTitles(const std::vector<std::string>& titles) { | 43 typedef std::pair<std::string, std::string> TitleAndURL; |
40 GURL url("about:blank"); | 44 |
41 for (size_t i = 0; i < titles.size(); ++i) | 45 void AddBookmarks(const char** titles, const char** urls, size_t count) { |
| 46 // The pair is (title, url). |
| 47 std::vector<TitleAndURL> bookmarks; |
| 48 for (size_t i = 0; i < count; ++i) { |
| 49 TitleAndURL bookmark(titles[i], urls[i]); |
| 50 bookmarks.push_back(bookmark); |
| 51 } |
| 52 AddBookmarks(bookmarks); |
| 53 } |
| 54 |
| 55 void AddBookmarks(const std::vector<TitleAndURL> bookmarks) { |
| 56 for (size_t i = 0; i < bookmarks.size(); ++i) { |
42 model_->AddURL(model_->other_node(), static_cast<int>(i), | 57 model_->AddURL(model_->other_node(), static_cast<int>(i), |
43 ASCIIToUTF16(titles[i]), url); | 58 ASCIIToUTF16(bookmarks[i].first), |
| 59 GURL(bookmarks[i].second)); |
| 60 } |
44 } | 61 } |
45 | 62 |
46 void ExpectMatches(const std::string& query, | 63 void ExpectMatches(const std::string& query, |
47 const char** expected_titles, | 64 const char** expected_titles, |
48 size_t expected_count) { | 65 size_t expected_count) { |
49 std::vector<std::string> title_vector; | 66 std::vector<std::string> title_vector; |
50 for (size_t i = 0; i < expected_count; ++i) | 67 for (size_t i = 0; i < expected_count; ++i) |
51 title_vector.push_back(expected_titles[i]); | 68 title_vector.push_back(expected_titles[i]); |
52 ExpectMatches(query, title_vector); | 69 ExpectMatches(query, title_vector); |
53 } | 70 } |
54 | 71 |
55 void ExpectMatches(const std::string& query, | 72 void ExpectMatches(const std::string& query, |
56 const std::vector<std::string>& expected_titles) { | 73 const std::vector<std::string>& expected_titles) { |
57 std::vector<BookmarkTitleMatch> matches; | 74 std::vector<BookmarkMatch> matches; |
58 model_->GetBookmarksWithTitlesMatching(ASCIIToUTF16(query), 1000, &matches); | 75 model_->GetBookmarksMatching(ASCIIToUTF16(query), 1000, &matches); |
59 ASSERT_EQ(expected_titles.size(), matches.size()); | 76 ASSERT_EQ(expected_titles.size(), matches.size()); |
60 for (size_t i = 0; i < expected_titles.size(); ++i) { | 77 for (size_t i = 0; i < expected_titles.size(); ++i) { |
61 bool found = false; | 78 bool found = false; |
62 for (size_t j = 0; j < matches.size(); ++j) { | 79 for (size_t j = 0; j < matches.size(); ++j) { |
63 if (ASCIIToUTF16(expected_titles[i]) == matches[j].node->GetTitle()) { | 80 if (ASCIIToUTF16(expected_titles[i]) == matches[j].node->GetTitle()) { |
64 matches.erase(matches.begin() + j); | 81 matches.erase(matches.begin() + j); |
65 found = true; | 82 found = true; |
66 break; | 83 break; |
67 } | 84 } |
68 } | 85 } |
69 ASSERT_TRUE(found); | 86 ASSERT_TRUE(found); |
70 } | 87 } |
71 } | 88 } |
72 | 89 |
73 void ExtractMatchPositions(const std::string& string, | 90 void ExtractMatchPositions(const std::string& string, |
74 BookmarkTitleMatch::MatchPositions* matches) { | 91 BookmarkMatch::MatchPositions* matches) { |
75 std::vector<std::string> match_strings; | 92 std::vector<std::string> match_strings; |
76 base::SplitString(string, ':', &match_strings); | 93 base::SplitString(string, ':', &match_strings); |
77 for (size_t i = 0; i < match_strings.size(); ++i) { | 94 for (size_t i = 0; i < match_strings.size(); ++i) { |
78 std::vector<std::string> chunks; | 95 std::vector<std::string> chunks; |
79 base::SplitString(match_strings[i], ',', &chunks); | 96 base::SplitString(match_strings[i], ',', &chunks); |
80 ASSERT_EQ(2U, chunks.size()); | 97 ASSERT_EQ(2U, chunks.size()); |
81 matches->push_back(BookmarkTitleMatch::MatchPosition()); | 98 matches->push_back(BookmarkMatch::MatchPosition()); |
82 int chunks0, chunks1; | 99 int chunks0, chunks1; |
83 base::StringToInt(chunks[0], &chunks0); | 100 base::StringToInt(chunks[0], &chunks0); |
84 base::StringToInt(chunks[1], &chunks1); | 101 base::StringToInt(chunks[1], &chunks1); |
85 matches->back().first = chunks0; | 102 matches->back().first = chunks0; |
86 matches->back().second = chunks1; | 103 matches->back().second = chunks1; |
87 } | 104 } |
88 } | 105 } |
89 | 106 |
90 void ExpectMatchPositions( | 107 void ExpectMatchPositions( |
91 const std::string& query, | 108 const BookmarkMatch::MatchPositions& actual_positions, |
92 const BookmarkTitleMatch::MatchPositions& expected_positions) { | 109 const BookmarkMatch::MatchPositions& expected_positions) { |
93 std::vector<BookmarkTitleMatch> matches; | 110 ASSERT_EQ(expected_positions.size(), actual_positions.size()); |
94 model_->GetBookmarksWithTitlesMatching(ASCIIToUTF16(query), 1000, &matches); | |
95 ASSERT_EQ(1U, matches.size()); | |
96 const BookmarkTitleMatch& match = matches[0]; | |
97 ASSERT_EQ(expected_positions.size(), match.match_positions.size()); | |
98 for (size_t i = 0; i < expected_positions.size(); ++i) { | 111 for (size_t i = 0; i < expected_positions.size(); ++i) { |
99 EXPECT_EQ(expected_positions[i].first, match.match_positions[i].first); | 112 EXPECT_EQ(expected_positions[i].first, actual_positions[i].first); |
100 EXPECT_EQ(expected_positions[i].second, match.match_positions[i].second); | 113 EXPECT_EQ(expected_positions[i].second, actual_positions[i].second); |
101 } | 114 } |
102 } | 115 } |
103 | 116 |
104 protected: | 117 protected: |
105 scoped_ptr<BookmarkModel> model_; | 118 scoped_ptr<BookmarkModel> model_; |
106 | 119 |
107 private: | 120 private: |
| 121 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 122 |
108 DISALLOW_COPY_AND_ASSIGN(BookmarkIndexTest); | 123 DISALLOW_COPY_AND_ASSIGN(BookmarkIndexTest); |
109 }; | 124 }; |
110 | 125 |
111 // Various permutations with differing input, queries and output that exercises | 126 // Various permutations with differing input, queries and output that exercises |
112 // all query paths. | 127 // all query paths. |
113 TEST_F(BookmarkIndexTest, Tests) { | 128 TEST_F(BookmarkIndexTest, GetBookmarksMatching) { |
114 struct TestData { | 129 struct TestData { |
115 const std::string input; | 130 const std::string titles; |
116 const std::string query; | 131 const std::string query; |
117 const std::string expected; | 132 const std::string expected; |
118 } data[] = { | 133 } data[] = { |
119 // Trivial test case of only one term, exact match. | 134 // Trivial test case of only one term, exact match. |
120 { "a;b", "A", "a" }, | 135 { "a;b", "A", "a" }, |
121 | 136 |
122 // Prefix match, one term. | 137 // Prefix match, one term. |
123 { "abcd;abc;b", "abc", "abcd;abc" }, | 138 { "abcd;abc;b", "abc", "abcd;abc" }, |
124 | 139 |
125 // Prefix match, multiple terms. | 140 // Prefix match, multiple terms. |
(...skipping 11 matching lines...) Expand all Loading... |
137 { "ab ab", "ab", "ab ab"}, | 152 { "ab ab", "ab", "ab ab"}, |
138 | 153 |
139 // Make sure quotes don't do a prefix match. | 154 // Make sure quotes don't do a prefix match. |
140 { "think", "\"thi\"", ""}, | 155 { "think", "\"thi\"", ""}, |
141 | 156 |
142 // Prefix matches against multiple candidates. | 157 // Prefix matches against multiple candidates. |
143 { "abc1 abc2 abc3 abc4", "abc", "abc1 abc2 abc3 abc4"}, | 158 { "abc1 abc2 abc3 abc4", "abc", "abc1 abc2 abc3 abc4"}, |
144 }; | 159 }; |
145 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 160 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
146 std::vector<std::string> titles; | 161 std::vector<std::string> titles; |
147 base::SplitString(data[i].input, ';', &titles); | 162 base::SplitString(data[i].titles, ';', &titles); |
148 AddBookmarksWithTitles(titles); | 163 std::vector<TitleAndURL> bookmarks; |
| 164 for (size_t j = 0; j < titles.size(); ++j) { |
| 165 TitleAndURL bookmark(titles[j], "about:blank"); |
| 166 bookmarks.push_back(bookmark); |
| 167 } |
| 168 AddBookmarks(bookmarks); |
149 | 169 |
150 std::vector<std::string> expected; | 170 std::vector<std::string> expected; |
151 if (!data[i].expected.empty()) | 171 if (!data[i].expected.empty()) |
152 base::SplitString(data[i].expected, ';', &expected); | 172 base::SplitString(data[i].expected, ';', &expected); |
153 | 173 |
154 ExpectMatches(data[i].query, expected); | 174 ExpectMatches(data[i].query, expected); |
155 | 175 |
156 model_.reset(new BookmarkModel(NULL)); | 176 model_.reset(new BookmarkModel(NULL)); |
157 } | 177 } |
158 } | 178 } |
159 | 179 |
160 TEST_F(BookmarkIndexTest, TestNormalization) { | 180 // Analogous to GetBookmarksMatching, this test tests various permutations |
| 181 // of title, URL, and input to see if the title/URL matches the input as |
| 182 // expected. |
| 183 TEST_F(BookmarkIndexTest, GetBookmarksMatchingWithURLs) { |
| 184 struct TestData { |
| 185 const std::string query; |
| 186 const std::string title; |
| 187 const std::string url; |
| 188 const bool should_be_retrieved; |
| 189 } data[] = { |
| 190 // Test single-word inputs. Include both exact matches and prefix matches. |
| 191 { "foo", "Foo", "http://www.bar.com/", true }, |
| 192 { "foo", "Foodie", "http://www.bar.com/", true }, |
| 193 { "foo", "Bar", "http://www.foo.com/", true }, |
| 194 { "foo", "Bar", "http://www.foodie.com/", true }, |
| 195 { "foo", "Foo", "http://www.foo.com/", true }, |
| 196 { "foo", "Bar", "http://www.bar.com/", false }, |
| 197 { "foo", "Bar", "http://www.bar.com/blah/foo/blah-again/ ", true }, |
| 198 { "foo", "Bar", "http://www.bar.com/blah/foodie/blah-again/ ", true }, |
| 199 { "foo", "Bar", "http://www.bar.com/blah-foo/blah-again/ ", true }, |
| 200 { "foo", "Bar", "http://www.bar.com/blah-foodie/blah-again/ ", true }, |
| 201 { "foo", "Bar", "http://www.bar.com/blahafoo/blah-again/ ", false }, |
| 202 |
| 203 // Test multi-word inputs. |
| 204 { "foo bar", "Foo Bar", "http://baz.com/", true }, |
| 205 { "foo bar", "Foodie Bar", "http://baz.com/", true }, |
| 206 { "bar foo", "Foo Bar", "http://baz.com/", true }, |
| 207 { "bar foo", "Foodie Barly", "http://baz.com/", true }, |
| 208 { "foo bar", "Foo Baz", "http://baz.com/", false }, |
| 209 { "foo bar", "Foo Baz", "http://bar.com/", true }, |
| 210 { "foo bar", "Foo Baz", "http://barly.com/", true }, |
| 211 { "foo bar", "Foodie Baz", "http://barly.com/", true }, |
| 212 { "bar foo", "Foo Baz", "http://bar.com/", true }, |
| 213 { "bar foo", "Foo Baz", "http://barly.com/", true }, |
| 214 { "foo bar", "Baz Bar", "http://blah.com/foo", true }, |
| 215 { "foo bar", "Baz Barly", "http://blah.com/foodie", true }, |
| 216 { "foo bar", "Baz Bur", "http://blah.com/foo/bar", true }, |
| 217 { "foo bar", "Baz Bur", "http://blah.com/food/barly", true }, |
| 218 { "foo bar", "Baz Bur", "http://bar.com/blah/foo", true }, |
| 219 { "foo bar", "Baz Bur", "http://barly.com/blah/food", true }, |
| 220 { "foo bar", "Baz Bur", "http://bar.com/blah/flub", false }, |
| 221 { "foo bar", "Baz Bur", "http://foo.com/blah/flub", false } |
| 222 }; |
| 223 |
| 224 // Set the field trial parameter to enable indexing URLs. |
| 225 { |
| 226 std::map<std::string, std::string> params; |
| 227 params[OmniboxFieldTrial::kBookmarksIndexURLsRule] = "true"; |
| 228 ASSERT_TRUE(chrome_variations::AssociateVariationParams( |
| 229 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params)); |
| 230 } |
| 231 base::FieldTrialList::CreateFieldTrial( |
| 232 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A"); |
| 233 |
| 234 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
| 235 model_.reset(new BookmarkModel(NULL)); |
| 236 std::vector<TitleAndURL> bookmarks; |
| 237 bookmarks.push_back(TitleAndURL(data[i].title, data[i].url)); |
| 238 AddBookmarks(bookmarks); |
| 239 |
| 240 std::vector<std::string> expected; |
| 241 if (data[i].should_be_retrieved) |
| 242 expected.push_back(data[i].title); |
| 243 |
| 244 ExpectMatches(data[i].query, expected); |
| 245 } |
| 246 } |
| 247 |
| 248 TEST_F(BookmarkIndexTest, Normalization) { |
161 struct TestData { | 249 struct TestData { |
162 const char* title; | 250 const char* title; |
163 const char* query; | 251 const char* query; |
164 } data[] = { | 252 } data[] = { |
165 { "fooa\xcc\x88-test", "foo\xc3\xa4-test" }, | 253 { "fooa\xcc\x88-test", "foo\xc3\xa4-test" }, |
166 { "fooa\xcc\x88-test", "fooa\xcc\x88-test" }, | 254 { "fooa\xcc\x88-test", "fooa\xcc\x88-test" }, |
167 { "fooa\xcc\x88-test", "foo\xc3\xa4" }, | 255 { "fooa\xcc\x88-test", "foo\xc3\xa4" }, |
168 { "fooa\xcc\x88-test", "fooa\xcc\x88" }, | 256 { "fooa\xcc\x88-test", "fooa\xcc\x88" }, |
169 { "fooa\xcc\x88-test", "foo" }, | 257 { "fooa\xcc\x88-test", "foo" }, |
170 { "foo\xc3\xa4-test", "foo\xc3\xa4-test" }, | 258 { "foo\xc3\xa4-test", "foo\xc3\xa4-test" }, |
171 { "foo\xc3\xa4-test", "fooa\xcc\x88-test" }, | 259 { "foo\xc3\xa4-test", "fooa\xcc\x88-test" }, |
172 { "foo\xc3\xa4-test", "foo\xc3\xa4" }, | 260 { "foo\xc3\xa4-test", "foo\xc3\xa4" }, |
173 { "foo\xc3\xa4-test", "fooa\xcc\x88" }, | 261 { "foo\xc3\xa4-test", "fooa\xcc\x88" }, |
174 { "foo\xc3\xa4-test", "foo" }, | 262 { "foo\xc3\xa4-test", "foo" }, |
175 { "foo", "foo" } | 263 { "foo", "foo" } |
176 }; | 264 }; |
177 | 265 |
178 GURL url("about:blank"); | 266 GURL url("about:blank"); |
179 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 267 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
180 model_->AddURL(model_->other_node(), 0, base::UTF8ToUTF16(data[i].title), | 268 model_->AddURL(model_->other_node(), 0, base::UTF8ToUTF16(data[i].title), |
181 url); | 269 url); |
182 std::vector<BookmarkTitleMatch> matches; | 270 std::vector<BookmarkMatch> matches; |
183 model_->GetBookmarksWithTitlesMatching( | 271 model_->GetBookmarksMatching( |
184 base::UTF8ToUTF16(data[i].query), 10, &matches); | 272 base::UTF8ToUTF16(data[i].query), 10, &matches); |
185 EXPECT_EQ(1u, matches.size()); | 273 EXPECT_EQ(1u, matches.size()); |
186 model_.reset(new BookmarkModel(NULL)); | 274 model_.reset(new BookmarkModel(NULL)); |
187 } | 275 } |
188 } | 276 } |
189 | 277 |
190 // Makes sure match positions are updated appropriately. | 278 // Makes sure match positions are updated appropriately for title matches. |
191 TEST_F(BookmarkIndexTest, MatchPositions) { | 279 TEST_F(BookmarkIndexTest, MatchPositionsTitles) { |
192 struct TestData { | 280 struct TestData { |
193 const std::string title; | 281 const std::string title; |
194 const std::string query; | 282 const std::string query; |
195 const std::string expected; | 283 const std::string expected_title_match_positions; |
196 } data[] = { | 284 } data[] = { |
197 // Trivial test case of only one term, exact match. | 285 // Trivial test case of only one term, exact match. |
198 { "a", "A", "0,1" }, | 286 { "a", "A", "0,1" }, |
199 { "foo bar", "bar", "4,7" }, | 287 { "foo bar", "bar", "4,7" }, |
200 { "fooey bark", "bar foo", "0,3:6,9"}, | 288 { "fooey bark", "bar foo", "0,3:6,9" }, |
201 // Non-trivial tests. | 289 // Non-trivial tests. |
202 { "foobar foo", "foobar foo", "0,6:7,10" }, | 290 { "foobar foo", "foobar foo", "0,6:7,10" }, |
203 { "foobar foo", "foo foobar", "0,6:7,10" }, | 291 { "foobar foo", "foo foobar", "0,6:7,10" }, |
204 { "foobar foobar", "foobar foo", "0,6:7,13" }, | 292 { "foobar foobar", "foobar foo", "0,6:7,13" }, |
205 { "foobar foobar", "foo foobar", "0,6:7,13" }, | 293 { "foobar foobar", "foo foobar", "0,6:7,13" }, |
206 }; | 294 }; |
207 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 295 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
208 std::vector<std::string> titles; | 296 std::vector<TitleAndURL> bookmarks; |
209 titles.push_back(data[i].title); | 297 TitleAndURL bookmark(data[i].title, "about:blank"); |
210 AddBookmarksWithTitles(titles); | 298 bookmarks.push_back(bookmark); |
| 299 AddBookmarks(bookmarks); |
211 | 300 |
212 BookmarkTitleMatch::MatchPositions expected_matches; | 301 std::vector<BookmarkMatch> matches; |
213 ExtractMatchPositions(data[i].expected, &expected_matches); | 302 model_->GetBookmarksMatching(ASCIIToUTF16(data[i].query), 1000, &matches); |
214 ExpectMatchPositions(data[i].query, expected_matches); | 303 ASSERT_EQ(1U, matches.size()); |
| 304 |
| 305 BookmarkMatch::MatchPositions expected_title_matches; |
| 306 ExtractMatchPositions(data[i].expected_title_match_positions, |
| 307 &expected_title_matches); |
| 308 ExpectMatchPositions(matches[0].title_match_positions, |
| 309 expected_title_matches); |
215 | 310 |
216 model_.reset(new BookmarkModel(NULL)); | 311 model_.reset(new BookmarkModel(NULL)); |
217 } | 312 } |
218 } | 313 } |
219 | 314 |
| 315 // Makes sure match positions are updated appropriately for URL matches. |
| 316 TEST_F(BookmarkIndexTest, MatchPositionsURLs) { |
| 317 struct TestData { |
| 318 const std::string query; |
| 319 const std::string url; |
| 320 const std::string expected_url_match_positions; |
| 321 } data[] = { |
| 322 { "foo", "http://www.foo.com/", "11,14" }, |
| 323 { "foo", "http://www.foodie.com/", "11,14" }, |
| 324 { "foo", "http://www.foofoo.com/", "11,14" }, |
| 325 { "www", "http://www.foo.com/", "7,10" }, |
| 326 { "foo", "http://www.foodie.com/blah/foo/fi", "11,14:27,30" }, |
| 327 { "foo", "http://www.blah.com/blah/foo/fi", "25,28" }, |
| 328 { "foo www", "http://www.foodie.com/blah/foo/fi", "7,10:11,14:27,30" }, |
| 329 { "www foo", "http://www.foodie.com/blah/foo/fi", "7,10:11,14:27,30" }, |
| 330 { "www bla", "http://www.foodie.com/blah/foo/fi", "7,10:22,25" }, |
| 331 { "http", "http://www.foo.com/", "0,4" }, |
| 332 { "http www", "http://www.foo.com/", "0,4:7,10" }, |
| 333 { "http foo", "http://www.foo.com/", "0,4:11,14" }, |
| 334 { "http foo", "http://www.bar.com/baz/foodie/hi", "0,4:23,26" } |
| 335 }; |
| 336 |
| 337 // Set the field trial parameter to enable indexing URLs. |
| 338 { |
| 339 std::map<std::string, std::string> params; |
| 340 params[OmniboxFieldTrial::kBookmarksIndexURLsRule] = "true"; |
| 341 ASSERT_TRUE(chrome_variations::AssociateVariationParams( |
| 342 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params)); |
| 343 } |
| 344 base::FieldTrialList::CreateFieldTrial( |
| 345 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A"); |
| 346 |
| 347 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
| 348 model_.reset(new BookmarkModel(NULL)); |
| 349 std::vector<TitleAndURL> bookmarks; |
| 350 TitleAndURL bookmark("123456", data[i].url); |
| 351 bookmarks.push_back(bookmark); |
| 352 AddBookmarks(bookmarks); |
| 353 |
| 354 std::vector<BookmarkMatch> matches; |
| 355 model_->GetBookmarksMatching(ASCIIToUTF16(data[i].query), 1000, &matches); |
| 356 ASSERT_EQ(1U, matches.size()) << data[i].url << data[i].query; |
| 357 |
| 358 BookmarkMatch::MatchPositions expected_url_matches; |
| 359 ExtractMatchPositions(data[i].expected_url_match_positions, |
| 360 &expected_url_matches); |
| 361 ExpectMatchPositions(matches[0].url_match_positions, expected_url_matches); |
| 362 } |
| 363 } |
| 364 |
220 // Makes sure index is updated when a node is removed. | 365 // Makes sure index is updated when a node is removed. |
221 TEST_F(BookmarkIndexTest, Remove) { | 366 TEST_F(BookmarkIndexTest, Remove) { |
222 const char* input[] = { "a", "b" }; | 367 const char* titles[] = { "a", "b" }; |
223 AddBookmarksWithTitles(input, ARRAYSIZE_UNSAFE(input)); | 368 const char* urls[] = { "about:blank", "about:blank" }; |
| 369 AddBookmarks(titles, urls, ARRAYSIZE_UNSAFE(titles)); |
224 | 370 |
225 // Remove the node and make sure we don't get back any results. | 371 // Remove the node and make sure we don't get back any results. |
226 model_->Remove(model_->other_node(), 0); | 372 model_->Remove(model_->other_node(), 0); |
227 ExpectMatches("A", NULL, 0U); | 373 ExpectMatches("A", NULL, 0U); |
228 } | 374 } |
229 | 375 |
230 // Makes sure index is updated when a node's title is changed. | 376 // Makes sure index is updated when a node's title is changed. |
231 TEST_F(BookmarkIndexTest, ChangeTitle) { | 377 TEST_F(BookmarkIndexTest, ChangeTitle) { |
232 const char* input[] = { "a", "b" }; | 378 const char* titles[] = { "a", "b" }; |
233 AddBookmarksWithTitles(input, ARRAYSIZE_UNSAFE(input)); | 379 const char* urls[] = { "about:blank", "about:blank" }; |
| 380 AddBookmarks(titles, urls, ARRAYSIZE_UNSAFE(titles)); |
234 | 381 |
235 // Remove the node and make sure we don't get back any results. | 382 // Remove the node and make sure we don't get back any results. |
236 const char* expected[] = { "blah" }; | 383 const char* expected[] = { "blah" }; |
237 model_->SetTitle(model_->other_node()->GetChild(0), ASCIIToUTF16("blah")); | 384 model_->SetTitle(model_->other_node()->GetChild(0), ASCIIToUTF16("blah")); |
238 ExpectMatches("BlAh", expected, ARRAYSIZE_UNSAFE(expected)); | 385 ExpectMatches("BlAh", expected, ARRAYSIZE_UNSAFE(expected)); |
239 } | 386 } |
240 | 387 |
241 // Makes sure no more than max queries is returned. | 388 // Makes sure no more than max queries is returned. |
242 TEST_F(BookmarkIndexTest, HonorMax) { | 389 TEST_F(BookmarkIndexTest, HonorMax) { |
243 const char* input[] = { "abcd", "abcde" }; | 390 const char* titles[] = { "abcd", "abcde" }; |
244 AddBookmarksWithTitles(input, ARRAYSIZE_UNSAFE(input)); | 391 const char* urls[] = { "about:blank", "about:blank" }; |
| 392 AddBookmarks(titles, urls, ARRAYSIZE_UNSAFE(titles)); |
245 | 393 |
246 std::vector<BookmarkTitleMatch> matches; | 394 std::vector<BookmarkMatch> matches; |
247 model_->GetBookmarksWithTitlesMatching(ASCIIToUTF16("ABc"), 1, &matches); | 395 model_->GetBookmarksMatching(ASCIIToUTF16("ABc"), 1, &matches); |
248 EXPECT_EQ(1U, matches.size()); | 396 EXPECT_EQ(1U, matches.size()); |
249 } | 397 } |
250 | 398 |
251 // Makes sure if the lower case string of a bookmark title is more characters | 399 // Makes sure if the lower case string of a bookmark title is more characters |
252 // than the upper case string no match positions are returned. | 400 // than the upper case string no match positions are returned. |
253 TEST_F(BookmarkIndexTest, EmptyMatchOnMultiwideLowercaseString) { | 401 TEST_F(BookmarkIndexTest, EmptyMatchOnMultiwideLowercaseString) { |
254 const BookmarkNode* n1 = model_->AddURL(model_->other_node(), 0, | 402 const BookmarkNode* n1 = model_->AddURL(model_->other_node(), 0, |
255 base::WideToUTF16(L"\u0130 i"), | 403 base::WideToUTF16(L"\u0130 i"), |
256 GURL("http://www.google.com")); | 404 GURL("http://www.google.com")); |
257 | 405 |
258 std::vector<BookmarkTitleMatch> matches; | 406 std::vector<BookmarkMatch> matches; |
259 model_->GetBookmarksWithTitlesMatching(ASCIIToUTF16("i"), 100, &matches); | 407 model_->GetBookmarksMatching(ASCIIToUTF16("i"), 100, &matches); |
260 ASSERT_EQ(1U, matches.size()); | 408 ASSERT_EQ(1U, matches.size()); |
261 EXPECT_TRUE(matches[0].node == n1); | 409 EXPECT_TRUE(matches[0].node == n1); |
262 EXPECT_TRUE(matches[0].match_positions.empty()); | 410 EXPECT_TRUE(matches[0].title_match_positions.empty()); |
263 } | 411 } |
264 | 412 |
265 TEST_F(BookmarkIndexTest, GetResultsSortedByTypedCount) { | 413 TEST_F(BookmarkIndexTest, GetResultsSortedByTypedCount) { |
266 // This ensures MessageLoop::current() will exist, which is needed by | 414 // This ensures MessageLoop::current() will exist, which is needed by |
267 // TestingProfile::BlockUntilHistoryProcessesPendingRequests(). | 415 // TestingProfile::BlockUntilHistoryProcessesPendingRequests(). |
268 content::TestBrowserThreadBundle thread_bundle; | 416 content::TestBrowserThreadBundle thread_bundle; |
269 | 417 |
270 TestingProfile profile; | 418 TestingProfile profile; |
271 ASSERT_TRUE(profile.CreateHistoryService(true, false)); | 419 ASSERT_TRUE(profile.CreateHistoryService(true, false)); |
272 profile.BlockUntilHistoryProcessesPendingRequests(); | 420 profile.BlockUntilHistoryProcessesPendingRequests(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 | 461 |
314 history::URLRow result3; | 462 history::URLRow result3; |
315 url_db->GetRowForURL(data[2].url, &result3); | 463 url_db->GetRowForURL(data[2].url, &result3); |
316 EXPECT_EQ(data[2].title, base::UTF16ToUTF8(result3.title())); | 464 EXPECT_EQ(data[2].title, base::UTF16ToUTF8(result3.title())); |
317 | 465 |
318 history::URLRow result4; | 466 history::URLRow result4; |
319 url_db->GetRowForURL(data[3].url, &result4); | 467 url_db->GetRowForURL(data[3].url, &result4); |
320 EXPECT_EQ(data[3].title, base::UTF16ToUTF8(result4.title())); | 468 EXPECT_EQ(data[3].title, base::UTF16ToUTF8(result4.title())); |
321 | 469 |
322 // Populate match nodes. | 470 // Populate match nodes. |
323 std::vector<BookmarkTitleMatch> matches; | 471 std::vector<BookmarkMatch> matches; |
324 model->GetBookmarksWithTitlesMatching(ASCIIToUTF16("google"), 4, &matches); | 472 model->GetBookmarksMatching(ASCIIToUTF16("google"), 4, &matches); |
325 | 473 |
326 // The resulting order should be: | 474 // The resulting order should be: |
327 // 1. Google (google.com) 100 | 475 // 1. Google (google.com) 100 |
328 // 2. Google Reader (google.com/reader) 80 | 476 // 2. Google Reader (google.com/reader) 80 |
329 // 3. Google Docs (docs.google.com) 50 | 477 // 3. Google Docs (docs.google.com) 50 |
330 // 4. Google Maps (maps.google.com) 40 | 478 // 4. Google Maps (maps.google.com) 40 |
331 EXPECT_EQ(4, static_cast<int>(matches.size())); | 479 EXPECT_EQ(4, static_cast<int>(matches.size())); |
332 EXPECT_EQ(data[0].url, matches[0].node->url()); | 480 EXPECT_EQ(data[0].url, matches[0].node->url()); |
333 EXPECT_EQ(data[3].url, matches[1].node->url()); | 481 EXPECT_EQ(data[3].url, matches[1].node->url()); |
334 EXPECT_EQ(data[2].url, matches[2].node->url()); | 482 EXPECT_EQ(data[2].url, matches[2].node->url()); |
335 EXPECT_EQ(data[1].url, matches[3].node->url()); | 483 EXPECT_EQ(data[1].url, matches[3].node->url()); |
336 | 484 |
337 matches.clear(); | 485 matches.clear(); |
338 // Select top two matches. | 486 // Select top two matches. |
339 model->GetBookmarksWithTitlesMatching(ASCIIToUTF16("google"), 2, &matches); | 487 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches); |
340 | 488 |
341 EXPECT_EQ(2, static_cast<int>(matches.size())); | 489 EXPECT_EQ(2, static_cast<int>(matches.size())); |
342 EXPECT_EQ(data[0].url, matches[0].node->url()); | 490 EXPECT_EQ(data[0].url, matches[0].node->url()); |
343 EXPECT_EQ(data[3].url, matches[1].node->url()); | 491 EXPECT_EQ(data[3].url, matches[1].node->url()); |
344 } | 492 } |
OLD | NEW |