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