OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/omnibox/browser/titled_url_match_utils.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "components/bookmarks/browser/titled_url_match.h" |
| 10 #include "components/bookmarks/browser/titled_url_node.h" |
| 11 #include "components/metrics/proto/omnibox_event.pb.h" |
| 12 #include "components/omnibox/browser/autocomplete_input.h" |
| 13 #include "components/omnibox/browser/autocomplete_match.h" |
| 14 #include "components/omnibox/browser/autocomplete_provider.h" |
| 15 #include "components/omnibox/browser/test_scheme_classifier.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 using bookmarks::TitledUrlMatchToAutocompleteMatch; |
| 20 using bookmarks::CorrectTitleAndMatchPositions; |
| 21 |
| 22 namespace { |
| 23 |
| 24 // A simple AutocompleteProvider that does nothing. |
| 25 class MockAutocompleteProvider : public AutocompleteProvider { |
| 26 public: |
| 27 MockAutocompleteProvider(Type type) : AutocompleteProvider(type) {} |
| 28 |
| 29 void Start(const AutocompleteInput& input, bool minimal_changes) override {} |
| 30 |
| 31 private: |
| 32 ~MockAutocompleteProvider() override {} |
| 33 }; |
| 34 |
| 35 class MockTitledUrlNode : public bookmarks::TitledUrlNode { |
| 36 public: |
| 37 MockTitledUrlNode(const base::string16& title, const GURL& url) |
| 38 : title_(title), url_(url) {} |
| 39 |
| 40 // TitledUrlNode |
| 41 const base::string16& GetTitledUrlNodeTitle() const override { |
| 42 return title_; |
| 43 } |
| 44 const GURL& GetTitledUrlNodeUrl() const override { return url_; } |
| 45 |
| 46 private: |
| 47 base::string16 title_; |
| 48 GURL url_; |
| 49 }; |
| 50 |
| 51 } // namespace |
| 52 |
| 53 bool operator==(const ACMatchClassification& lhs, |
| 54 const ACMatchClassification& rhs) { |
| 55 return (lhs.offset == rhs.offset) && (lhs.style == rhs.style); |
| 56 } |
| 57 |
| 58 TEST(TitledUrlMatchUtilsTest, TitledUrlMatchToAutocompleteMatch) { |
| 59 base::string16 input_text(base::ASCIIToUTF16("goo")); |
| 60 base::string16 match_title(base::ASCIIToUTF16("Google Search")); |
| 61 base::string16 match_url_string( |
| 62 base::ASCIIToUTF16("https://www.google.com/")); |
| 63 GURL match_url(match_url_string); |
| 64 bookmarks::TitledUrlMatch::MatchPositions title_match_positions = {{0, 3}}; |
| 65 bookmarks::TitledUrlMatch::MatchPositions url_match_positions = {{12, 15}}; |
| 66 AutocompleteMatchType::Type type = AutocompleteMatchType::BOOKMARK_TITLE; |
| 67 int relevance = 123; |
| 68 |
| 69 MockTitledUrlNode node(match_title, match_url); |
| 70 bookmarks::TitledUrlMatch titled_url_match; |
| 71 titled_url_match.node = &node; |
| 72 titled_url_match.title_match_positions = title_match_positions; |
| 73 titled_url_match.url_match_positions = url_match_positions; |
| 74 |
| 75 scoped_refptr<MockAutocompleteProvider> provider = |
| 76 new MockAutocompleteProvider(AutocompleteProvider::Type::TYPE_BOOKMARK); |
| 77 TestSchemeClassifier classifier; |
| 78 AutocompleteInput input(input_text, base::string16::npos, std::string(), |
| 79 GURL(), metrics::OmniboxEventProto::NTP, false, false, |
| 80 true, true, false, classifier); |
| 81 const base::string16 fixed_up_input(input_text); |
| 82 |
| 83 AutocompleteMatch autocomplete_match = TitledUrlMatchToAutocompleteMatch( |
| 84 titled_url_match, type, relevance, provider.get(), classifier, input, |
| 85 fixed_up_input); |
| 86 |
| 87 ACMatchClassifications expected_contents_class = { |
| 88 {0, ACMatchClassification::URL}, |
| 89 {12, ACMatchClassification::URL | ACMatchClassification::MATCH}, |
| 90 {15, ACMatchClassification::URL}, |
| 91 }; |
| 92 ACMatchClassifications expected_description_class = { |
| 93 {0, ACMatchClassification::MATCH}, {3, ACMatchClassification::NONE}, |
| 94 }; |
| 95 base::string16 expected_inline_autocompletion(base::ASCIIToUTF16("gle.com")); |
| 96 base::string16 expected_contents( |
| 97 base::ASCIIToUTF16("https://www.google.com")); |
| 98 |
| 99 EXPECT_EQ(provider.get(), autocomplete_match.provider); |
| 100 EXPECT_EQ(type, autocomplete_match.type); |
| 101 EXPECT_EQ(relevance, autocomplete_match.relevance); |
| 102 EXPECT_EQ(match_url, autocomplete_match.destination_url); |
| 103 EXPECT_EQ(expected_contents, autocomplete_match.contents); |
| 104 EXPECT_TRUE(std::equal(expected_contents_class.begin(), |
| 105 expected_contents_class.end(), |
| 106 autocomplete_match.contents_class.begin())); |
| 107 EXPECT_EQ(match_title, autocomplete_match.description); |
| 108 EXPECT_TRUE(std::equal(expected_description_class.begin(), |
| 109 expected_description_class.end(), |
| 110 autocomplete_match.description_class.begin())); |
| 111 EXPECT_EQ(expected_contents, autocomplete_match.fill_into_edit); |
| 112 EXPECT_TRUE(autocomplete_match.allowed_to_be_default_match); |
| 113 EXPECT_EQ(expected_inline_autocompletion, |
| 114 autocomplete_match.inline_autocompletion); |
| 115 } |
| 116 |
| 117 TEST(TitledUrlMatchUtilsTest, EmptyInlineAutocompletion) { |
| 118 // The search term matches the title but not the URL. Since there is no URL |
| 119 // match, the inline autocompletion string will be empty. |
| 120 base::string16 input_text(base::ASCIIToUTF16("goo")); |
| 121 base::string16 match_title(base::ASCIIToUTF16("Email by Google")); |
| 122 base::string16 match_url_string(base::ASCIIToUTF16("https://www.gmail.com/")); |
| 123 GURL match_url(match_url_string); |
| 124 bookmarks::TitledUrlMatch::MatchPositions title_match_positions = {{9, 12}}; |
| 125 bookmarks::TitledUrlMatch::MatchPositions url_match_positions; |
| 126 AutocompleteMatchType::Type type = AutocompleteMatchType::BOOKMARK_TITLE; |
| 127 int relevance = 123; |
| 128 |
| 129 MockTitledUrlNode node(match_title, match_url); |
| 130 bookmarks::TitledUrlMatch titled_url_match; |
| 131 titled_url_match.node = &node; |
| 132 titled_url_match.title_match_positions = title_match_positions; |
| 133 titled_url_match.url_match_positions = url_match_positions; |
| 134 |
| 135 scoped_refptr<MockAutocompleteProvider> provider = |
| 136 new MockAutocompleteProvider(AutocompleteProvider::Type::TYPE_BOOKMARK); |
| 137 TestSchemeClassifier classifier; |
| 138 AutocompleteInput input(input_text, base::string16::npos, std::string(), |
| 139 GURL(), metrics::OmniboxEventProto::NTP, false, false, |
| 140 true, true, false, classifier); |
| 141 const base::string16 fixed_up_input(input_text); |
| 142 |
| 143 AutocompleteMatch autocomplete_match = TitledUrlMatchToAutocompleteMatch( |
| 144 titled_url_match, type, relevance, provider.get(), classifier, input, |
| 145 fixed_up_input); |
| 146 |
| 147 ACMatchClassifications expected_contents_class = { |
| 148 {0, ACMatchClassification::URL}, |
| 149 }; |
| 150 ACMatchClassifications expected_description_class = { |
| 151 {0, ACMatchClassification::NONE}, |
| 152 {9, ACMatchClassification::MATCH}, |
| 153 {12, ACMatchClassification::NONE}, |
| 154 }; |
| 155 base::string16 expected_contents(base::ASCIIToUTF16("https://www.gmail.com")); |
| 156 |
| 157 EXPECT_EQ(provider.get(), autocomplete_match.provider); |
| 158 EXPECT_EQ(type, autocomplete_match.type); |
| 159 EXPECT_EQ(relevance, autocomplete_match.relevance); |
| 160 EXPECT_EQ(match_url, autocomplete_match.destination_url); |
| 161 EXPECT_EQ(expected_contents, autocomplete_match.contents); |
| 162 EXPECT_TRUE(std::equal(expected_contents_class.begin(), |
| 163 expected_contents_class.end(), |
| 164 autocomplete_match.contents_class.begin())); |
| 165 EXPECT_EQ(match_title, autocomplete_match.description); |
| 166 EXPECT_TRUE(std::equal(expected_description_class.begin(), |
| 167 expected_description_class.end(), |
| 168 autocomplete_match.description_class.begin())); |
| 169 EXPECT_EQ(expected_contents, autocomplete_match.fill_into_edit); |
| 170 EXPECT_FALSE(autocomplete_match.allowed_to_be_default_match); |
| 171 EXPECT_TRUE(autocomplete_match.inline_autocompletion.empty()); |
| 172 } |
| 173 |
| 174 TEST(TitledUrlMatchUtilsTest, CorrectTitleAndMatchPositions) { |
| 175 bookmarks::TitledUrlMatch::MatchPositions match_positions = {{2, 6}, |
| 176 {10, 15}}; |
| 177 base::string16 title = base::ASCIIToUTF16(" Leading whitespace"); |
| 178 bookmarks::TitledUrlMatch::MatchPositions expected_match_positions = { |
| 179 {0, 4}, {8, 13}}; |
| 180 base::string16 expected_title = base::ASCIIToUTF16("Leading whitespace"); |
| 181 CorrectTitleAndMatchPositions(&title, &match_positions); |
| 182 EXPECT_EQ(expected_title, title); |
| 183 EXPECT_TRUE(std::equal(match_positions.begin(), match_positions.end(), |
| 184 expected_match_positions.begin())); |
| 185 } |
OLD | NEW |