Chromium Code Reviews| 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 | |
| 21 namespace { | |
| 22 | |
| 23 // A simple AutocompleteProvider that does nothing. | |
| 24 class MockAutocompleteProvider : public AutocompleteProvider { | |
| 25 public: | |
| 26 MockAutocompleteProvider(Type type) : AutocompleteProvider(type) {} | |
| 27 | |
| 28 void Start(const AutocompleteInput& input, bool minimal_changes) override {} | |
| 29 | |
| 30 private: | |
| 31 ~MockAutocompleteProvider() override {} | |
| 32 }; | |
| 33 | |
| 34 class MockTitledUrlNode : public bookmarks::TitledUrlNode { | |
| 35 public: | |
| 36 MockTitledUrlNode(const base::string16& title, const GURL& url) | |
| 37 : title_(title), url_(url) {} | |
| 38 | |
| 39 // TitledUrlNode | |
| 40 const base::string16& GetTitledUrlNodeTitle() const override { | |
| 41 return title_; | |
| 42 } | |
| 43 const GURL& GetTitledUrlNodeUrl() const override { return url_; } | |
| 44 | |
| 45 private: | |
| 46 base::string16 title_; | |
| 47 GURL url_; | |
| 48 }; | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 52 bool operator==(const ACMatchClassification& lhs, | |
| 53 const ACMatchClassification& rhs) { | |
| 54 return (lhs.offset == rhs.offset) && (lhs.style == rhs.style); | |
| 55 } | |
| 56 | |
| 57 class TitledUrlMatchUtilsTest : public testing::Test { | |
| 58 public: | |
| 59 TitledUrlMatchUtilsTest() {} | |
| 60 | |
| 61 private: | |
| 62 DISALLOW_COPY_AND_ASSIGN(TitledUrlMatchUtilsTest); | |
| 63 }; | |
| 64 | |
| 65 TEST_F(TitledUrlMatchUtilsTest, TitledUrlMatchToAutocompleteMatch) { | |
| 66 base::string16 input_text(base::ASCIIToUTF16("goo")); | |
| 67 base::string16 match_title(base::ASCIIToUTF16("Google Search")); | |
| 68 base::string16 match_url_string( | |
| 69 base::ASCIIToUTF16("https://www.google.com/")); | |
| 70 GURL match_url(match_url_string); | |
| 71 bookmarks::TitledUrlMatch::MatchPositions title_match_positions = {{0, 3}}; | |
| 72 bookmarks::TitledUrlMatch::MatchPositions url_match_positions = {{12, 15}}; | |
| 73 AutocompleteMatchType::Type type = AutocompleteMatchType::BOOKMARK_TITLE; | |
| 74 int relevance = 123; | |
| 75 | |
| 76 MockTitledUrlNode node(match_title, match_url); | |
| 77 bookmarks::TitledUrlMatch titled_url_match; | |
| 78 titled_url_match.node = &node; | |
| 79 titled_url_match.title_match_positions = title_match_positions; | |
| 80 titled_url_match.url_match_positions = url_match_positions; | |
| 81 | |
| 82 scoped_refptr<MockAutocompleteProvider> provider = | |
| 83 new MockAutocompleteProvider(AutocompleteProvider::Type::TYPE_BOOKMARK); | |
| 84 TestSchemeClassifier classifier; | |
| 85 AutocompleteInput input(input_text, base::string16::npos, std::string(), | |
| 86 GURL(), metrics::OmniboxEventProto::NTP, | |
| 87 false, false, true, true, false, classifier); | |
| 88 base::string16 fixed_up_input(input_text); | |
|
Mark P
2017/01/05 20:56:02
nit: const
ditto below
mattreynolds
2017/01/05 21:21:37
Done.
| |
| 89 | |
| 90 AutocompleteMatch autocomplete_match = TitledUrlMatchToAutocompleteMatch( | |
| 91 titled_url_match, type, relevance, provider.get(), classifier, input, | |
| 92 fixed_up_input); | |
| 93 | |
| 94 ACMatchClassifications expected_contents_class = { | |
| 95 {0, ACMatchClassification::URL}, | |
| 96 {12, ACMatchClassification::URL | ACMatchClassification::MATCH}, | |
| 97 {15, ACMatchClassification::URL}, | |
| 98 }; | |
| 99 ACMatchClassifications expected_description_class = { | |
| 100 {0, ACMatchClassification::MATCH}, {3, ACMatchClassification::NONE}, | |
| 101 }; | |
| 102 base::string16 expected_inline_autocompletion(base::ASCIIToUTF16("gle.com")); | |
| 103 base::string16 expected_contents( | |
| 104 base::ASCIIToUTF16("https://www.google.com")); | |
| 105 | |
| 106 EXPECT_EQ(provider.get(), autocomplete_match.provider); | |
| 107 EXPECT_EQ(type, autocomplete_match.type); | |
| 108 EXPECT_EQ(relevance, autocomplete_match.relevance); | |
| 109 EXPECT_EQ(match_url, autocomplete_match.destination_url); | |
| 110 EXPECT_EQ(expected_contents, autocomplete_match.contents); | |
| 111 EXPECT_TRUE(std::equal(expected_contents_class.begin(), | |
| 112 expected_contents_class.end(), | |
| 113 autocomplete_match.contents_class.begin())); | |
| 114 EXPECT_EQ(match_title, autocomplete_match.description); | |
| 115 EXPECT_TRUE(std::equal(expected_description_class.begin(), | |
| 116 expected_description_class.end(), | |
| 117 autocomplete_match.description_class.begin())); | |
| 118 EXPECT_EQ(expected_contents, autocomplete_match.fill_into_edit); | |
| 119 EXPECT_TRUE(autocomplete_match.allowed_to_be_default_match); | |
| 120 EXPECT_EQ(expected_inline_autocompletion, | |
| 121 autocomplete_match.inline_autocompletion); | |
| 122 } | |
| 123 | |
| 124 TEST_F(TitledUrlMatchUtilsTest, EmptyInlineAutocompletion) { | |
| 125 // The search term matches the title but not the URL. Since there is no URL | |
| 126 // match, the inline autocompletion string will be empty. | |
| 127 base::string16 input_text(base::ASCIIToUTF16("goo")); | |
| 128 base::string16 match_title(base::ASCIIToUTF16("Email by Google")); | |
| 129 base::string16 match_url_string(base::ASCIIToUTF16("https://www.gmail.com/")); | |
| 130 GURL match_url(match_url_string); | |
| 131 bookmarks::TitledUrlMatch::MatchPositions title_match_positions = {{9, 12}}; | |
| 132 bookmarks::TitledUrlMatch::MatchPositions url_match_positions; | |
| 133 AutocompleteMatchType::Type type = AutocompleteMatchType::BOOKMARK_TITLE; | |
| 134 int relevance = 123; | |
| 135 | |
| 136 MockTitledUrlNode node(match_title, match_url); | |
| 137 bookmarks::TitledUrlMatch titled_url_match; | |
| 138 titled_url_match.node = &node; | |
| 139 titled_url_match.title_match_positions = title_match_positions; | |
| 140 titled_url_match.url_match_positions = url_match_positions; | |
| 141 | |
| 142 scoped_refptr<MockAutocompleteProvider> provider = | |
| 143 new MockAutocompleteProvider(AutocompleteProvider::Type::TYPE_BOOKMARK); | |
| 144 TestSchemeClassifier classifier; | |
| 145 AutocompleteInput input(input_text, base::string16::npos, std::string(), | |
| 146 GURL(), metrics::OmniboxEventProto::NTP, | |
| 147 false, false, true, true, false, classifier); | |
| 148 base::string16 fixed_up_input(input_text); | |
| 149 | |
| 150 AutocompleteMatch autocomplete_match = TitledUrlMatchToAutocompleteMatch( | |
| 151 titled_url_match, type, relevance, provider.get(), classifier, input, | |
| 152 fixed_up_input); | |
| 153 | |
| 154 ACMatchClassifications expected_contents_class = { | |
| 155 {0, ACMatchClassification::URL}, | |
| 156 }; | |
| 157 ACMatchClassifications expected_description_class = { | |
| 158 {0, ACMatchClassification::NONE}, | |
| 159 {9, ACMatchClassification::MATCH}, | |
| 160 {12, ACMatchClassification::NONE}, | |
| 161 }; | |
| 162 base::string16 expected_contents(base::ASCIIToUTF16("https://www.gmail.com")); | |
| 163 | |
| 164 EXPECT_EQ(provider.get(), autocomplete_match.provider); | |
| 165 EXPECT_EQ(type, autocomplete_match.type); | |
| 166 EXPECT_EQ(relevance, autocomplete_match.relevance); | |
| 167 EXPECT_EQ(match_url, autocomplete_match.destination_url); | |
| 168 EXPECT_EQ(expected_contents, autocomplete_match.contents); | |
| 169 EXPECT_TRUE(std::equal(expected_contents_class.begin(), | |
| 170 expected_contents_class.end(), | |
| 171 autocomplete_match.contents_class.begin())); | |
| 172 EXPECT_EQ(match_title, autocomplete_match.description); | |
| 173 EXPECT_TRUE(std::equal(expected_description_class.begin(), | |
| 174 expected_description_class.end(), | |
| 175 autocomplete_match.description_class.begin())); | |
| 176 EXPECT_EQ(expected_contents, autocomplete_match.fill_into_edit); | |
| 177 EXPECT_FALSE(autocomplete_match.allowed_to_be_default_match); | |
| 178 EXPECT_TRUE(autocomplete_match.inline_autocompletion.empty()); | |
| 179 } | |
| OLD | NEW |