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/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "chrome/browser/bookmarks/bookmark_model.h" | 15 #include "chrome/browser/bookmarks/bookmark_model.h" |
16 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 16 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
17 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" | 17 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" |
| 18 #include "chrome/browser/bookmarks/test_bookmark_client.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" |
21 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
22 #include "components/bookmarks/core/browser/bookmark_match.h" | 23 #include "components/bookmarks/core/browser/bookmark_match.h" |
23 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 | 26 |
26 using base::ASCIIToUTF16; | 27 using base::ASCIIToUTF16; |
27 | 28 |
28 class BookmarkIndexTest : public testing::Test { | 29 class BookmarkIndexTest : public testing::Test { |
29 public: | 30 public: |
30 BookmarkIndexTest() : model_(new BookmarkModel(NULL, false)) { | 31 BookmarkIndexTest() : model_(client_.CreateModel(false)) {} |
31 } | |
32 | 32 |
33 typedef std::pair<std::string, std::string> TitleAndURL; | 33 typedef std::pair<std::string, std::string> TitleAndURL; |
34 | 34 |
35 void AddBookmarks(const char** titles, const char** urls, size_t count) { | 35 void AddBookmarks(const char** titles, const char** urls, size_t count) { |
36 // The pair is (title, url). | 36 // The pair is (title, url). |
37 std::vector<TitleAndURL> bookmarks; | 37 std::vector<TitleAndURL> bookmarks; |
38 for (size_t i = 0; i < count; ++i) { | 38 for (size_t i = 0; i < count; ++i) { |
39 TitleAndURL bookmark(titles[i], urls[i]); | 39 TitleAndURL bookmark(titles[i], urls[i]); |
40 bookmarks.push_back(bookmark); | 40 bookmarks.push_back(bookmark); |
41 } | 41 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 const BookmarkMatch::MatchPositions& actual_positions, | 98 const BookmarkMatch::MatchPositions& actual_positions, |
99 const BookmarkMatch::MatchPositions& expected_positions) { | 99 const BookmarkMatch::MatchPositions& expected_positions) { |
100 ASSERT_EQ(expected_positions.size(), actual_positions.size()); | 100 ASSERT_EQ(expected_positions.size(), actual_positions.size()); |
101 for (size_t i = 0; i < expected_positions.size(); ++i) { | 101 for (size_t i = 0; i < expected_positions.size(); ++i) { |
102 EXPECT_EQ(expected_positions[i].first, actual_positions[i].first); | 102 EXPECT_EQ(expected_positions[i].first, actual_positions[i].first); |
103 EXPECT_EQ(expected_positions[i].second, actual_positions[i].second); | 103 EXPECT_EQ(expected_positions[i].second, actual_positions[i].second); |
104 } | 104 } |
105 } | 105 } |
106 | 106 |
107 protected: | 107 protected: |
| 108 test::TestBookmarkClient client_; |
108 scoped_ptr<BookmarkModel> model_; | 109 scoped_ptr<BookmarkModel> model_; |
109 | 110 |
110 private: | 111 private: |
111 DISALLOW_COPY_AND_ASSIGN(BookmarkIndexTest); | 112 DISALLOW_COPY_AND_ASSIGN(BookmarkIndexTest); |
112 }; | 113 }; |
113 | 114 |
114 // Various permutations with differing input, queries and output that exercises | 115 // Various permutations with differing input, queries and output that exercises |
115 // all query paths. | 116 // all query paths. |
116 TEST_F(BookmarkIndexTest, GetBookmarksMatching) { | 117 TEST_F(BookmarkIndexTest, GetBookmarksMatching) { |
117 struct TestData { | 118 struct TestData { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 bookmarks.push_back(bookmark); | 155 bookmarks.push_back(bookmark); |
155 } | 156 } |
156 AddBookmarks(bookmarks); | 157 AddBookmarks(bookmarks); |
157 | 158 |
158 std::vector<std::string> expected; | 159 std::vector<std::string> expected; |
159 if (!data[i].expected.empty()) | 160 if (!data[i].expected.empty()) |
160 base::SplitString(data[i].expected, ';', &expected); | 161 base::SplitString(data[i].expected, ';', &expected); |
161 | 162 |
162 ExpectMatches(data[i].query, expected); | 163 ExpectMatches(data[i].query, expected); |
163 | 164 |
164 model_.reset(new BookmarkModel(NULL, false)); | 165 model_ = client_.CreateModel(false); |
165 } | 166 } |
166 } | 167 } |
167 | 168 |
168 // Analogous to GetBookmarksMatching, this test tests various permutations | 169 // Analogous to GetBookmarksMatching, this test tests various permutations |
169 // of title, URL, and input to see if the title/URL matches the input as | 170 // of title, URL, and input to see if the title/URL matches the input as |
170 // expected. | 171 // expected. |
171 TEST_F(BookmarkIndexTest, GetBookmarksMatchingWithURLs) { | 172 TEST_F(BookmarkIndexTest, GetBookmarksMatchingWithURLs) { |
172 struct TestData { | 173 struct TestData { |
173 const std::string query; | 174 const std::string query; |
174 const std::string title; | 175 const std::string title; |
(...skipping 28 matching lines...) Expand all Loading... |
203 { "foo bar", "Baz Barly", "http://blah.com/foodie", true }, | 204 { "foo bar", "Baz Barly", "http://blah.com/foodie", true }, |
204 { "foo bar", "Baz Bur", "http://blah.com/foo/bar", true }, | 205 { "foo bar", "Baz Bur", "http://blah.com/foo/bar", true }, |
205 { "foo bar", "Baz Bur", "http://blah.com/food/barly", true }, | 206 { "foo bar", "Baz Bur", "http://blah.com/food/barly", true }, |
206 { "foo bar", "Baz Bur", "http://bar.com/blah/foo", true }, | 207 { "foo bar", "Baz Bur", "http://bar.com/blah/foo", true }, |
207 { "foo bar", "Baz Bur", "http://barly.com/blah/food", true }, | 208 { "foo bar", "Baz Bur", "http://barly.com/blah/food", true }, |
208 { "foo bar", "Baz Bur", "http://bar.com/blah/flub", false }, | 209 { "foo bar", "Baz Bur", "http://bar.com/blah/flub", false }, |
209 { "foo bar", "Baz Bur", "http://foo.com/blah/flub", false } | 210 { "foo bar", "Baz Bur", "http://foo.com/blah/flub", false } |
210 }; | 211 }; |
211 | 212 |
212 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 213 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
213 model_.reset(new BookmarkModel(NULL, true)); | 214 model_ = client_.CreateModel(true); |
214 std::vector<TitleAndURL> bookmarks; | 215 std::vector<TitleAndURL> bookmarks; |
215 bookmarks.push_back(TitleAndURL(data[i].title, data[i].url)); | 216 bookmarks.push_back(TitleAndURL(data[i].title, data[i].url)); |
216 AddBookmarks(bookmarks); | 217 AddBookmarks(bookmarks); |
217 | 218 |
218 std::vector<std::string> expected; | 219 std::vector<std::string> expected; |
219 if (data[i].should_be_retrieved) | 220 if (data[i].should_be_retrieved) |
220 expected.push_back(data[i].title); | 221 expected.push_back(data[i].title); |
221 | 222 |
222 ExpectMatches(data[i].query, expected); | 223 ExpectMatches(data[i].query, expected); |
223 } | 224 } |
(...skipping 18 matching lines...) Expand all Loading... |
242 }; | 243 }; |
243 | 244 |
244 GURL url("about:blank"); | 245 GURL url("about:blank"); |
245 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 246 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
246 model_->AddURL(model_->other_node(), 0, base::UTF8ToUTF16(data[i].title), | 247 model_->AddURL(model_->other_node(), 0, base::UTF8ToUTF16(data[i].title), |
247 url); | 248 url); |
248 std::vector<BookmarkMatch> matches; | 249 std::vector<BookmarkMatch> matches; |
249 model_->GetBookmarksMatching( | 250 model_->GetBookmarksMatching( |
250 base::UTF8ToUTF16(data[i].query), 10, &matches); | 251 base::UTF8ToUTF16(data[i].query), 10, &matches); |
251 EXPECT_EQ(1u, matches.size()); | 252 EXPECT_EQ(1u, matches.size()); |
252 model_.reset(new BookmarkModel(NULL, false)); | 253 model_ = client_.CreateModel(false); |
253 } | 254 } |
254 } | 255 } |
255 | 256 |
256 // Makes sure match positions are updated appropriately for title matches. | 257 // Makes sure match positions are updated appropriately for title matches. |
257 TEST_F(BookmarkIndexTest, MatchPositionsTitles) { | 258 TEST_F(BookmarkIndexTest, MatchPositionsTitles) { |
258 struct TestData { | 259 struct TestData { |
259 const std::string title; | 260 const std::string title; |
260 const std::string query; | 261 const std::string query; |
261 const std::string expected_title_match_positions; | 262 const std::string expected_title_match_positions; |
262 } data[] = { | 263 } data[] = { |
(...skipping 16 matching lines...) Expand all Loading... |
279 std::vector<BookmarkMatch> matches; | 280 std::vector<BookmarkMatch> matches; |
280 model_->GetBookmarksMatching(ASCIIToUTF16(data[i].query), 1000, &matches); | 281 model_->GetBookmarksMatching(ASCIIToUTF16(data[i].query), 1000, &matches); |
281 ASSERT_EQ(1U, matches.size()); | 282 ASSERT_EQ(1U, matches.size()); |
282 | 283 |
283 BookmarkMatch::MatchPositions expected_title_matches; | 284 BookmarkMatch::MatchPositions expected_title_matches; |
284 ExtractMatchPositions(data[i].expected_title_match_positions, | 285 ExtractMatchPositions(data[i].expected_title_match_positions, |
285 &expected_title_matches); | 286 &expected_title_matches); |
286 ExpectMatchPositions(matches[0].title_match_positions, | 287 ExpectMatchPositions(matches[0].title_match_positions, |
287 expected_title_matches); | 288 expected_title_matches); |
288 | 289 |
289 model_.reset(new BookmarkModel(NULL, false)); | 290 model_ = client_.CreateModel(false); |
290 } | 291 } |
291 } | 292 } |
292 | 293 |
293 // Makes sure match positions are updated appropriately for URL matches. | 294 // Makes sure match positions are updated appropriately for URL matches. |
294 TEST_F(BookmarkIndexTest, MatchPositionsURLs) { | 295 TEST_F(BookmarkIndexTest, MatchPositionsURLs) { |
295 struct TestData { | 296 struct TestData { |
296 const std::string query; | 297 const std::string query; |
297 const std::string url; | 298 const std::string url; |
298 const std::string expected_url_match_positions; | 299 const std::string expected_url_match_positions; |
299 } data[] = { | 300 } data[] = { |
300 { "foo", "http://www.foo.com/", "11,14" }, | 301 { "foo", "http://www.foo.com/", "11,14" }, |
301 { "foo", "http://www.foodie.com/", "11,14" }, | 302 { "foo", "http://www.foodie.com/", "11,14" }, |
302 { "foo", "http://www.foofoo.com/", "11,14" }, | 303 { "foo", "http://www.foofoo.com/", "11,14" }, |
303 { "www", "http://www.foo.com/", "7,10" }, | 304 { "www", "http://www.foo.com/", "7,10" }, |
304 { "foo", "http://www.foodie.com/blah/foo/fi", "11,14:27,30" }, | 305 { "foo", "http://www.foodie.com/blah/foo/fi", "11,14:27,30" }, |
305 { "foo", "http://www.blah.com/blah/foo/fi", "25,28" }, | 306 { "foo", "http://www.blah.com/blah/foo/fi", "25,28" }, |
306 { "foo www", "http://www.foodie.com/blah/foo/fi", "7,10:11,14:27,30" }, | 307 { "foo www", "http://www.foodie.com/blah/foo/fi", "7,10:11,14:27,30" }, |
307 { "www foo", "http://www.foodie.com/blah/foo/fi", "7,10:11,14:27,30" }, | 308 { "www foo", "http://www.foodie.com/blah/foo/fi", "7,10:11,14:27,30" }, |
308 { "www bla", "http://www.foodie.com/blah/foo/fi", "7,10:22,25" }, | 309 { "www bla", "http://www.foodie.com/blah/foo/fi", "7,10:22,25" }, |
309 { "http", "http://www.foo.com/", "0,4" }, | 310 { "http", "http://www.foo.com/", "0,4" }, |
310 { "http www", "http://www.foo.com/", "0,4:7,10" }, | 311 { "http www", "http://www.foo.com/", "0,4:7,10" }, |
311 { "http foo", "http://www.foo.com/", "0,4:11,14" }, | 312 { "http foo", "http://www.foo.com/", "0,4:11,14" }, |
312 { "http foo", "http://www.bar.com/baz/foodie/hi", "0,4:23,26" } | 313 { "http foo", "http://www.bar.com/baz/foodie/hi", "0,4:23,26" } |
313 }; | 314 }; |
314 | 315 |
315 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 316 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
316 model_.reset(new BookmarkModel(NULL, true)); | 317 model_ = client_.CreateModel(true); |
317 std::vector<TitleAndURL> bookmarks; | 318 std::vector<TitleAndURL> bookmarks; |
318 TitleAndURL bookmark("123456", data[i].url); | 319 TitleAndURL bookmark("123456", data[i].url); |
319 bookmarks.push_back(bookmark); | 320 bookmarks.push_back(bookmark); |
320 AddBookmarks(bookmarks); | 321 AddBookmarks(bookmarks); |
321 | 322 |
322 std::vector<BookmarkMatch> matches; | 323 std::vector<BookmarkMatch> matches; |
323 model_->GetBookmarksMatching(ASCIIToUTF16(data[i].query), 1000, &matches); | 324 model_->GetBookmarksMatching(ASCIIToUTF16(data[i].query), 1000, &matches); |
324 ASSERT_EQ(1U, matches.size()) << data[i].url << data[i].query; | 325 ASSERT_EQ(1U, matches.size()) << data[i].url << data[i].query; |
325 | 326 |
326 BookmarkMatch::MatchPositions expected_url_matches; | 327 BookmarkMatch::MatchPositions expected_url_matches; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 EXPECT_EQ(data[1].url, matches[3].node->url()); | 452 EXPECT_EQ(data[1].url, matches[3].node->url()); |
452 | 453 |
453 matches.clear(); | 454 matches.clear(); |
454 // Select top two matches. | 455 // Select top two matches. |
455 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches); | 456 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches); |
456 | 457 |
457 EXPECT_EQ(2, static_cast<int>(matches.size())); | 458 EXPECT_EQ(2, static_cast<int>(matches.size())); |
458 EXPECT_EQ(data[0].url, matches[0].node->url()); | 459 EXPECT_EQ(data[0].url, matches[0].node->url()); |
459 EXPECT_EQ(data[3].url, matches[1].node->url()); | 460 EXPECT_EQ(data[3].url, matches[1].node->url()); |
460 } | 461 } |
OLD | NEW |