| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/bookmarks/browser/bookmark_index.h" | 5 #include "components/bookmarks/browser/bookmark_index.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 } | 48 } |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 const std::map<GURL, int> typed_count_map_; | 51 const std::map<GURL, int> typed_count_map_; |
| 52 | 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(BookmarkClientMock); | 53 DISALLOW_COPY_AND_ASSIGN(BookmarkClientMock); |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 class BookmarkIndexTest : public testing::Test { | 56 class BookmarkIndexTest : public testing::Test { |
| 57 public: | 57 public: |
| 58 BookmarkIndexTest() : model_(client_.CreateModel()) {} | 58 BookmarkIndexTest() : model_(client_.CreateModel(false)) {} |
| 59 | 59 |
| 60 typedef std::pair<std::string, std::string> TitleAndURL; | 60 typedef std::pair<std::string, std::string> TitleAndURL; |
| 61 | 61 |
| 62 void AddBookmarks(const char** titles, const char** urls, size_t count) { | 62 void AddBookmarks(const char** titles, const char** urls, size_t count) { |
| 63 // The pair is (title, url). | 63 // The pair is (title, url). |
| 64 std::vector<TitleAndURL> bookmarks; | 64 std::vector<TitleAndURL> bookmarks; |
| 65 for (size_t i = 0; i < count; ++i) { | 65 for (size_t i = 0; i < count; ++i) { |
| 66 TitleAndURL bookmark(titles[i], urls[i]); | 66 TitleAndURL bookmark(titles[i], urls[i]); |
| 67 bookmarks.push_back(bookmark); | 67 bookmarks.push_back(bookmark); |
| 68 } | 68 } |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 bookmarks.push_back(bookmark); | 182 bookmarks.push_back(bookmark); |
| 183 } | 183 } |
| 184 AddBookmarks(bookmarks); | 184 AddBookmarks(bookmarks); |
| 185 | 185 |
| 186 std::vector<std::string> expected; | 186 std::vector<std::string> expected; |
| 187 if (!data[i].expected.empty()) | 187 if (!data[i].expected.empty()) |
| 188 base::SplitString(data[i].expected, ';', &expected); | 188 base::SplitString(data[i].expected, ';', &expected); |
| 189 | 189 |
| 190 ExpectMatches(data[i].query, expected); | 190 ExpectMatches(data[i].query, expected); |
| 191 | 191 |
| 192 model_ = client_.CreateModel(); | 192 model_ = client_.CreateModel(false); |
| 193 } | 193 } |
| 194 } | 194 } |
| 195 | 195 |
| 196 // Analogous to GetBookmarksMatching, this test tests various permutations | 196 // Analogous to GetBookmarksMatching, this test tests various permutations |
| 197 // of title, URL, and input to see if the title/URL matches the input as | 197 // of title, URL, and input to see if the title/URL matches the input as |
| 198 // expected. | 198 // expected. |
| 199 TEST_F(BookmarkIndexTest, GetBookmarksMatchingWithURLs) { | 199 TEST_F(BookmarkIndexTest, GetBookmarksMatchingWithURLs) { |
| 200 struct TestData { | 200 struct TestData { |
| 201 const std::string query; | 201 const std::string query; |
| 202 const std::string title; | 202 const std::string title; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 231 { "foo bar", "Baz Barly", "http://blah.com/foodie", true }, | 231 { "foo bar", "Baz Barly", "http://blah.com/foodie", true }, |
| 232 { "foo bar", "Baz Bur", "http://blah.com/foo/bar", true }, | 232 { "foo bar", "Baz Bur", "http://blah.com/foo/bar", true }, |
| 233 { "foo bar", "Baz Bur", "http://blah.com/food/barly", true }, | 233 { "foo bar", "Baz Bur", "http://blah.com/food/barly", true }, |
| 234 { "foo bar", "Baz Bur", "http://bar.com/blah/foo", true }, | 234 { "foo bar", "Baz Bur", "http://bar.com/blah/foo", true }, |
| 235 { "foo bar", "Baz Bur", "http://barly.com/blah/food", true }, | 235 { "foo bar", "Baz Bur", "http://barly.com/blah/food", true }, |
| 236 { "foo bar", "Baz Bur", "http://bar.com/blah/flub", false }, | 236 { "foo bar", "Baz Bur", "http://bar.com/blah/flub", false }, |
| 237 { "foo bar", "Baz Bur", "http://foo.com/blah/flub", false } | 237 { "foo bar", "Baz Bur", "http://foo.com/blah/flub", false } |
| 238 }; | 238 }; |
| 239 | 239 |
| 240 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 240 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
| 241 model_ = client_.CreateModel(); | 241 model_ = client_.CreateModel(true); |
| 242 std::vector<TitleAndURL> bookmarks; | 242 std::vector<TitleAndURL> bookmarks; |
| 243 bookmarks.push_back(TitleAndURL(data[i].title, data[i].url)); | 243 bookmarks.push_back(TitleAndURL(data[i].title, data[i].url)); |
| 244 AddBookmarks(bookmarks); | 244 AddBookmarks(bookmarks); |
| 245 | 245 |
| 246 std::vector<std::string> expected; | 246 std::vector<std::string> expected; |
| 247 if (data[i].should_be_retrieved) | 247 if (data[i].should_be_retrieved) |
| 248 expected.push_back(data[i].title); | 248 expected.push_back(data[i].title); |
| 249 | 249 |
| 250 ExpectMatches(data[i].query, expected); | 250 ExpectMatches(data[i].query, expected); |
| 251 } | 251 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 268 { "foo\xc3\xa4-test", "foo" }, | 268 { "foo\xc3\xa4-test", "foo" }, |
| 269 { "foo", "foo" } | 269 { "foo", "foo" } |
| 270 }; | 270 }; |
| 271 | 271 |
| 272 GURL url(kAboutBlankURL); | 272 GURL url(kAboutBlankURL); |
| 273 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 273 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
| 274 model_->AddURL(model_->other_node(), 0, UTF8ToUTF16(data[i].title), url); | 274 model_->AddURL(model_->other_node(), 0, UTF8ToUTF16(data[i].title), url); |
| 275 std::vector<BookmarkMatch> matches; | 275 std::vector<BookmarkMatch> matches; |
| 276 model_->GetBookmarksMatching(UTF8ToUTF16(data[i].query), 10, &matches); | 276 model_->GetBookmarksMatching(UTF8ToUTF16(data[i].query), 10, &matches); |
| 277 EXPECT_EQ(1u, matches.size()); | 277 EXPECT_EQ(1u, matches.size()); |
| 278 model_ = client_.CreateModel(); | 278 model_ = client_.CreateModel(false); |
| 279 } | 279 } |
| 280 } | 280 } |
| 281 | 281 |
| 282 // Makes sure match positions are updated appropriately for title matches. | 282 // Makes sure match positions are updated appropriately for title matches. |
| 283 TEST_F(BookmarkIndexTest, MatchPositionsTitles) { | 283 TEST_F(BookmarkIndexTest, MatchPositionsTitles) { |
| 284 struct TestData { | 284 struct TestData { |
| 285 const std::string title; | 285 const std::string title; |
| 286 const std::string query; | 286 const std::string query; |
| 287 const std::string expected_title_match_positions; | 287 const std::string expected_title_match_positions; |
| 288 } data[] = { | 288 } data[] = { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 305 std::vector<BookmarkMatch> matches; | 305 std::vector<BookmarkMatch> matches; |
| 306 model_->GetBookmarksMatching(ASCIIToUTF16(data[i].query), 1000, &matches); | 306 model_->GetBookmarksMatching(ASCIIToUTF16(data[i].query), 1000, &matches); |
| 307 ASSERT_EQ(1U, matches.size()); | 307 ASSERT_EQ(1U, matches.size()); |
| 308 | 308 |
| 309 BookmarkMatch::MatchPositions expected_title_matches; | 309 BookmarkMatch::MatchPositions expected_title_matches; |
| 310 ExtractMatchPositions(data[i].expected_title_match_positions, | 310 ExtractMatchPositions(data[i].expected_title_match_positions, |
| 311 &expected_title_matches); | 311 &expected_title_matches); |
| 312 ExpectMatchPositions(matches[0].title_match_positions, | 312 ExpectMatchPositions(matches[0].title_match_positions, |
| 313 expected_title_matches); | 313 expected_title_matches); |
| 314 | 314 |
| 315 model_ = client_.CreateModel(); | 315 model_ = client_.CreateModel(false); |
| 316 } | 316 } |
| 317 } | 317 } |
| 318 | 318 |
| 319 // Makes sure match positions are updated appropriately for URL matches. | 319 // Makes sure match positions are updated appropriately for URL matches. |
| 320 TEST_F(BookmarkIndexTest, MatchPositionsURLs) { | 320 TEST_F(BookmarkIndexTest, MatchPositionsURLs) { |
| 321 // The encoded stuff between /wiki/ and the # is 第二次世界大戦 | 321 // The encoded stuff between /wiki/ and the # is 第二次世界大戦 |
| 322 const std::string ja_wiki_url = "http://ja.wikipedia.org/wiki/%E7%AC%AC%E4" | 322 const std::string ja_wiki_url = "http://ja.wikipedia.org/wiki/%E7%AC%AC%E4" |
| 323 "%BA%8C%E6%AC%A1%E4%B8%96%E7%95%8C%E5%A4%A7%E6%88%A6#.E3.83.B4.E3.82.A7" | 323 "%BA%8C%E6%AC%A1%E4%B8%96%E7%95%8C%E5%A4%A7%E6%88%A6#.E3.83.B4.E3.82.A7" |
| 324 ".E3.83.AB.E3.82.B5.E3.82.A4.E3.83.A6.E4.BD.93.E5.88.B6"; | 324 ".E3.83.AB.E3.82.B5.E3.82.A4.E3.83.A6.E4.BD.93.E5.88.B6"; |
| 325 struct TestData { | 325 struct TestData { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 341 { "http foo", "http://www.foo.com/", "0,4:11,14" }, | 341 { "http foo", "http://www.foo.com/", "0,4:11,14" }, |
| 342 { "http foo", "http://www.bar.com/baz/foodie/hi", "0,4:23,26" }, | 342 { "http foo", "http://www.bar.com/baz/foodie/hi", "0,4:23,26" }, |
| 343 { "第二次", ja_wiki_url, "29,56" }, | 343 { "第二次", ja_wiki_url, "29,56" }, |
| 344 { "ja 第二次", ja_wiki_url, "7,9:29,56" }, | 344 { "ja 第二次", ja_wiki_url, "7,9:29,56" }, |
| 345 { "第二次 E3.8", ja_wiki_url, "29,56:94,98:103,107:" | 345 { "第二次 E3.8", ja_wiki_url, "29,56:94,98:103,107:" |
| 346 "112,116:121,125:" | 346 "112,116:121,125:" |
| 347 "130,134:139,143" } | 347 "130,134:139,143" } |
| 348 }; | 348 }; |
| 349 | 349 |
| 350 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 350 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
| 351 model_ = client_.CreateModel(); | 351 model_ = client_.CreateModel(true); |
| 352 std::vector<TitleAndURL> bookmarks; | 352 std::vector<TitleAndURL> bookmarks; |
| 353 TitleAndURL bookmark("123456", data[i].url); | 353 TitleAndURL bookmark("123456", data[i].url); |
| 354 bookmarks.push_back(bookmark); | 354 bookmarks.push_back(bookmark); |
| 355 AddBookmarks(bookmarks); | 355 AddBookmarks(bookmarks); |
| 356 | 356 |
| 357 std::vector<BookmarkMatch> matches; | 357 std::vector<BookmarkMatch> matches; |
| 358 model_->GetBookmarksMatching(UTF8ToUTF16(data[i].query), 1000, &matches); | 358 model_->GetBookmarksMatching(UTF8ToUTF16(data[i].query), 1000, &matches); |
| 359 ASSERT_EQ(1U, matches.size()) << data[i].url << data[i].query; | 359 ASSERT_EQ(1U, matches.size()) << data[i].url << data[i].query; |
| 360 | 360 |
| 361 BookmarkMatch::MatchPositions expected_url_matches; | 361 BookmarkMatch::MatchPositions expected_url_matches; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 { GURL("http://maps.google.com/"), "Google Maps", 40 }, | 423 { GURL("http://maps.google.com/"), "Google Maps", 40 }, |
| 424 { GURL("http://docs.google.com/"), "Google Docs", 50 }, | 424 { GURL("http://docs.google.com/"), "Google Docs", 50 }, |
| 425 { GURL("http://reader.google.com/"), "Google Reader", 80 }, | 425 { GURL("http://reader.google.com/"), "Google Reader", 80 }, |
| 426 }; | 426 }; |
| 427 | 427 |
| 428 std::map<GURL, int> typed_count_map; | 428 std::map<GURL, int> typed_count_map; |
| 429 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) | 429 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) |
| 430 typed_count_map.insert(std::make_pair(data[i].url, data[i].typed_count)); | 430 typed_count_map.insert(std::make_pair(data[i].url, data[i].typed_count)); |
| 431 | 431 |
| 432 BookmarkClientMock client(typed_count_map); | 432 BookmarkClientMock client(typed_count_map); |
| 433 scoped_ptr<BookmarkModel> model = client.CreateModel(); | 433 scoped_ptr<BookmarkModel> model = client.CreateModel(false); |
| 434 | 434 |
| 435 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) | 435 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) |
| 436 // Populate the BookmarkIndex. | 436 // Populate the BookmarkIndex. |
| 437 model->AddURL( | 437 model->AddURL( |
| 438 model->other_node(), i, UTF8ToUTF16(data[i].title), data[i].url); | 438 model->other_node(), i, UTF8ToUTF16(data[i].title), data[i].url); |
| 439 | 439 |
| 440 // Populate match nodes. | 440 // Populate match nodes. |
| 441 std::vector<BookmarkMatch> matches; | 441 std::vector<BookmarkMatch> matches; |
| 442 model->GetBookmarksMatching(ASCIIToUTF16("google"), 4, &matches); | 442 model->GetBookmarksMatching(ASCIIToUTF16("google"), 4, &matches); |
| 443 | 443 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 456 // Select top two matches. | 456 // Select top two matches. |
| 457 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches); | 457 model->GetBookmarksMatching(ASCIIToUTF16("google"), 2, &matches); |
| 458 | 458 |
| 459 ASSERT_EQ(2, static_cast<int>(matches.size())); | 459 ASSERT_EQ(2, static_cast<int>(matches.size())); |
| 460 EXPECT_EQ(data[0].url, matches[0].node->url()); | 460 EXPECT_EQ(data[0].url, matches[0].node->url()); |
| 461 EXPECT_EQ(data[3].url, matches[1].node->url()); | 461 EXPECT_EQ(data[3].url, matches[1].node->url()); |
| 462 } | 462 } |
| 463 | 463 |
| 464 } // namespace | 464 } // namespace |
| 465 } // namespace bookmarks | 465 } // namespace bookmarks |
| OLD | NEW |