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/autocomplete/bookmark_provider.h" | 5 #include "chrome/browser/autocomplete/bookmark_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_split.h" |
15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
16 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 17 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
17 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 18 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
18 #include "chrome/browser/bookmarks/bookmark_model.h" | 19 #include "chrome/browser/bookmarks/bookmark_model.h" |
19 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 20 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
20 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
21 #include "components/bookmarks/core/browser/bookmark_title_match.h" | 22 #include "components/bookmarks/core/browser/bookmark_match.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 // The bookmark corpus against which we will simulate searches. | 25 // The bookmark corpus against which we will simulate searches. |
25 struct BookmarksTestInfo { | 26 struct BookmarksTestInfo { |
26 std::string title; | 27 std::string title; |
27 std::string url; | 28 std::string url; |
28 } bookmark_provider_test_data[] = { | 29 } bookmark_provider_test_data[] = { |
29 { "abc def", "http://www.catsanddogs.com/a" }, | 30 { "abc def", "http://www.catsanddogs.com/a" }, |
30 { "abcde", "http://www.catsanddogs.com/b" }, | 31 { "abcde", "http://www.catsanddogs.com/b" }, |
31 { "abcdef", "http://www.catsanddogs.com/c" }, | 32 { "abcdef", "http://www.catsanddogs.com/c" }, |
32 { "a definition", "http://www.catsanddogs.com/d" }, | 33 { "a definition", "http://www.catsanddogs.com/d" }, |
33 { "carry carbon carefully", "http://www.catsanddogs.com/e" }, | 34 { "carry carbon carefully", "http://www.catsanddogs.com/e" }, |
34 { "ghi jkl", "http://www.catsanddogs.com/f" }, | 35 { "ghi jkl", "http://www.catsanddogs.com/f" }, |
35 { "jkl ghi", "http://www.catsanddogs.com/g" }, | 36 { "jkl ghi", "http://www.catsanddogs.com/g" }, |
36 { "frankly frankly frank", "http://www.catsanddogs.com/h" }, | 37 { "frankly frankly frank", "http://www.catsanddogs.com/h" }, |
37 { "foobar foobar", "http://www.foobar.com/" }, | 38 { "foobar foobar", "http://www.foobar.com/" }, |
| 39 { "domain", "http://www.domain.com/http/" }, |
| 40 { "repeat", "http://www.repeat.com/1/repeat/2/" }, |
38 // For testing inline_autocompletion. | 41 // For testing inline_autocompletion. |
39 { "http://blah.com/", "http://blah.com/" }, | 42 { "http://blah.com/", "http://blah.com/" }, |
40 { "http://fiddle.com/", "http://fiddle.com/" }, | 43 { "http://fiddle.com/", "http://fiddle.com/" }, |
41 { "http://www.www.com/", "http://www.www.com/" }, | 44 { "http://www.www.com/", "http://www.www.com/" }, |
42 { "chrome://version", "chrome://version" }, | 45 { "chrome://version", "chrome://version" }, |
43 { "chrome://omnibox", "chrome://omnibox" }, | 46 { "chrome://omnibox", "chrome://omnibox" }, |
44 // For testing ranking with different URLs. | 47 // For testing ranking with different URLs. |
45 {"achlorhydric featherheads resuscitates mockingbirds", | 48 {"achlorhydric featherheads resuscitates mockingbirds", |
46 "http://www.featherheads.com/a" }, | 49 "http://www.featherheads.com/a" }, |
47 {"achlorhydric mockingbirds resuscitates featherhead", | 50 {"achlorhydric mockingbirds resuscitates featherhead", |
48 "http://www.featherheads.com/b" }, | 51 "http://www.featherheads.com/b" }, |
49 {"featherhead resuscitates achlorhydric mockingbirds", | 52 {"featherhead resuscitates achlorhydric mockingbirds", |
50 "http://www.featherheads.com/c" }, | 53 "http://www.featherheads.com/c" }, |
51 {"mockingbirds resuscitates featherheads achlorhydric", | 54 {"mockingbirds resuscitates featherheads achlorhydric", |
52 "http://www.featherheads.com/d" }, | 55 "http://www.featherheads.com/d" }, |
53 // For testing URL boosting. | 56 // For testing URL boosting. |
54 {"burning worms #1", "http://www.burned.com/" }, | 57 {"burning worms #1", "http://www.burned.com/" }, |
55 {"burning worms #2", "http://www.worms.com/" }, | 58 {"burning worms #2", "http://www.worms.com/" }, |
56 {"worming burns #10", "http://www.burned.com/" }, | 59 {"worming burns #10", "http://www.burned.com/" }, |
57 {"worming burns #20", "http://www.worms.com/" }, | 60 {"worming burns #20", "http://www.worms.com/" }, |
58 {"jive music", "http://www.worms.com/" }, | 61 {"jive music", "http://www.worms.com/" }, |
59 }; | 62 }; |
60 | 63 |
61 class BookmarkProviderTest : public testing::Test, | 64 class BookmarkProviderTest : public testing::Test, |
62 public AutocompleteProviderListener { | 65 public AutocompleteProviderListener { |
63 public: | 66 public: |
64 BookmarkProviderTest() : model_(new BookmarkModel(NULL)) {} | 67 BookmarkProviderTest(); |
65 | 68 |
66 // AutocompleteProviderListener: Not called. | 69 // AutocompleteProviderListener: Not called. |
67 virtual void OnProviderUpdate(bool updated_matches) OVERRIDE {} | 70 virtual void OnProviderUpdate(bool updated_matches) OVERRIDE {} |
68 | 71 |
69 protected: | 72 protected: |
70 virtual void SetUp() OVERRIDE; | 73 virtual void SetUp() OVERRIDE; |
71 | 74 |
72 scoped_ptr<TestingProfile> profile_; | 75 scoped_ptr<TestingProfile> profile_; |
73 scoped_ptr<BookmarkModel> model_; | 76 scoped_ptr<BookmarkModel> model_; |
74 scoped_refptr<BookmarkProvider> provider_; | 77 scoped_refptr<BookmarkProvider> provider_; |
75 | 78 |
76 private: | 79 private: |
77 DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest); | 80 DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest); |
78 }; | 81 }; |
79 | 82 |
| 83 BookmarkProviderTest::BookmarkProviderTest() { |
| 84 model_.reset(new BookmarkModel(NULL, false)); |
| 85 } |
| 86 |
80 void BookmarkProviderTest::SetUp() { | 87 void BookmarkProviderTest::SetUp() { |
81 profile_.reset(new TestingProfile()); | 88 profile_.reset(new TestingProfile()); |
82 DCHECK(profile_.get()); | 89 DCHECK(profile_.get()); |
83 provider_ = new BookmarkProvider(this, profile_.get()); | 90 provider_ = new BookmarkProvider(this, profile_.get()); |
84 DCHECK(provider_.get()); | 91 DCHECK(provider_.get()); |
85 provider_->set_bookmark_model_for_testing(model_.get()); | 92 provider_->set_bookmark_model_for_testing(model_.get()); |
86 | 93 |
87 const BookmarkNode* other_node = model_->other_node(); | 94 const BookmarkNode* other_node = model_->other_node(); |
88 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(bookmark_provider_test_data); ++i) { | 95 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(bookmark_provider_test_data); ++i) { |
89 const BookmarksTestInfo& cur(bookmark_provider_test_data[i]); | 96 const BookmarksTestInfo& cur(bookmark_provider_test_data[i]); |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 const std::string description = "for query=" + query_data[i].query + | 387 const std::string description = "for query=" + query_data[i].query + |
381 " and url=" + query_data[i].url; | 388 " and url=" + query_data[i].url; |
382 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query), | 389 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query), |
383 base::string16::npos, base::string16(), GURL(), | 390 base::string16::npos, base::string16(), GURL(), |
384 AutocompleteInput::INVALID_SPEC, false, false, | 391 AutocompleteInput::INVALID_SPEC, false, false, |
385 false, true); | 392 false, true); |
386 AutocompleteInput fixed_up_input(input); | 393 AutocompleteInput fixed_up_input(input); |
387 provider_->FixupUserInput(&fixed_up_input); | 394 provider_->FixupUserInput(&fixed_up_input); |
388 BookmarkNode node(GURL(query_data[i].url)); | 395 BookmarkNode node(GURL(query_data[i].url)); |
389 node.SetTitle(base::ASCIIToUTF16(query_data[i].url)); | 396 node.SetTitle(base::ASCIIToUTF16(query_data[i].url)); |
390 BookmarkTitleMatch bookmark_match; | 397 BookmarkMatch bookmark_match; |
391 bookmark_match.node = &node; | 398 bookmark_match.node = &node; |
392 const AutocompleteMatch& ac_match = | 399 const AutocompleteMatch& ac_match = provider_->BookmarkMatchToACMatch( |
393 provider_->TitleMatchToACMatch(input, fixed_up_input, bookmark_match); | 400 input, fixed_up_input, bookmark_match); |
394 EXPECT_EQ(query_data[i].allowed_to_be_default_match, | 401 EXPECT_EQ(query_data[i].allowed_to_be_default_match, |
395 ac_match.allowed_to_be_default_match) << description; | 402 ac_match.allowed_to_be_default_match) << description; |
396 EXPECT_EQ(base::ASCIIToUTF16(query_data[i].inline_autocompletion), | 403 EXPECT_EQ(base::ASCIIToUTF16(query_data[i].inline_autocompletion), |
397 ac_match.inline_autocompletion) << description; | 404 ac_match.inline_autocompletion) << description; |
398 } | 405 } |
399 } | 406 } |
| 407 |
| 408 TEST_F(BookmarkProviderTest, StripHttpAndAdjustOffsets) { |
| 409 // Simulate searches. |
| 410 struct QueryData { |
| 411 const std::string query; |
| 412 const std::string expected_contents; |
| 413 // |expected_contents_class| is in format offset:style,offset:style,... |
| 414 const std::string expected_contents_class; |
| 415 } query_data[] = { |
| 416 { "foo", "www.foobar.com", "0:1,4:3,7:1" }, |
| 417 { "www foo", "www.foobar.com", "0:3,3:1,4:3,7:1" }, |
| 418 { "foo www", "www.foobar.com", "0:3,3:1,4:3,7:1" }, |
| 419 { "foo http", "http://www.foobar.com", "0:3,4:1,11:3,14:1" }, |
| 420 { "blah", "blah.com", "0:3,4:1" }, |
| 421 { "http blah", "http://blah.com", "0:3,4:1,7:3,11:1" }, |
| 422 { "dom", "www.domain.com/http/", "0:1,4:3,7:1" }, |
| 423 { "dom http", "http://www.domain.com/http/", |
| 424 "0:3,4:1,11:3,14:1,22:3,26:1" }, |
| 425 { "rep", "www.repeat.com/1/repeat/2/", "0:1,4:3,7:1,17:3,20:1" }, |
| 426 { "versi", "chrome://version", "0:1,9:3,14:1" } |
| 427 }; |
| 428 |
| 429 // Reload the bookmarks index with |index_urls| == true. |
| 430 model_.reset(new BookmarkModel(NULL, true)); |
| 431 SetUp(); |
| 432 |
| 433 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { |
| 434 std::string description = "for query=" + query_data[i].query; |
| 435 AutocompleteInput input(base::ASCIIToUTF16(query_data[i].query), |
| 436 base::string16::npos, base::string16(), GURL(), |
| 437 AutocompleteInput::INVALID_SPEC, false, false, |
| 438 false, true); |
| 439 provider_->Start(input, false); |
| 440 const ACMatches& matches(provider_->matches()); |
| 441 ASSERT_EQ(1U, matches.size()) << description; |
| 442 const AutocompleteMatch& match = matches[0]; |
| 443 EXPECT_EQ(base::ASCIIToUTF16(query_data[i].expected_contents), |
| 444 match.contents) << description; |
| 445 std::vector<std::string> class_strings; |
| 446 base::SplitString( |
| 447 query_data[i].expected_contents_class, ',', &class_strings); |
| 448 ASSERT_EQ(class_strings.size(), match.contents_class.size()) |
| 449 << description; |
| 450 for (size_t i = 0; i < class_strings.size(); ++i) { |
| 451 std::vector<std::string> chunks; |
| 452 base::SplitString(class_strings[i], ':', &chunks); |
| 453 ASSERT_EQ(2U, chunks.size()) << description; |
| 454 size_t offset; |
| 455 EXPECT_TRUE(base::StringToSizeT(chunks[0], &offset)) << description; |
| 456 EXPECT_EQ(offset, match.contents_class[i].offset) << description; |
| 457 int style; |
| 458 EXPECT_TRUE(base::StringToInt(chunks[1], &style)) << description; |
| 459 EXPECT_EQ(style, match.contents_class[i].style) << description; |
| 460 } |
| 461 } |
| 462 } |
OLD | NEW |