Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: components/omnibox/browser/titled_url_match_utils_unittest.cc

Issue 2583763003: Factor out AutocompleteMatch creation from BookmarkProvider (Closed)
Patch Set: re-add unittest Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 class TitledUrlMatchUtilsTest : public testing::Test {
59 public:
60 TitledUrlMatchUtilsTest() {}
61
62 private:
63 DISALLOW_COPY_AND_ASSIGN(TitledUrlMatchUtilsTest);
64 };
65
66 TEST_F(TitledUrlMatchUtilsTest, TitledUrlMatchToAutocompleteMatch) {
67 base::string16 input_text(base::ASCIIToUTF16("goo"));
68 base::string16 match_title(base::ASCIIToUTF16("Google Search"));
69 base::string16 match_url_string(
70 base::ASCIIToUTF16("https://www.google.com/"));
71 GURL match_url(match_url_string);
72 bookmarks::TitledUrlMatch::MatchPositions title_match_positions = {{0, 3}};
73 bookmarks::TitledUrlMatch::MatchPositions url_match_positions = {{12, 15}};
74 AutocompleteMatchType::Type type = AutocompleteMatchType::BOOKMARK_TITLE;
75 int relevance = 123;
76
77 MockTitledUrlNode node(match_title, match_url);
78 bookmarks::TitledUrlMatch titled_url_match;
79 titled_url_match.node = &node;
80 titled_url_match.title_match_positions = title_match_positions;
81 titled_url_match.url_match_positions = url_match_positions;
82
83 scoped_refptr<MockAutocompleteProvider> provider =
84 new MockAutocompleteProvider(AutocompleteProvider::Type::TYPE_BOOKMARK);
85 TestSchemeClassifier classifier;
86 AutocompleteInput input(input_text, base::string16::npos, std::string(),
87 GURL(), metrics::OmniboxEventProto::NTP, false, false,
88 true, true, false, classifier);
89 const base::string16 fixed_up_input(input_text);
90
91 AutocompleteMatch autocomplete_match = TitledUrlMatchToAutocompleteMatch(
92 titled_url_match, type, relevance, provider.get(), classifier, input,
93 fixed_up_input);
94
95 ACMatchClassifications expected_contents_class = {
96 {0, ACMatchClassification::URL},
97 {12, ACMatchClassification::URL | ACMatchClassification::MATCH},
98 {15, ACMatchClassification::URL},
99 };
100 ACMatchClassifications expected_description_class = {
101 {0, ACMatchClassification::MATCH}, {3, ACMatchClassification::NONE},
102 };
103 base::string16 expected_inline_autocompletion(base::ASCIIToUTF16("gle.com"));
104 base::string16 expected_contents(
105 base::ASCIIToUTF16("https://www.google.com"));
106
107 EXPECT_EQ(provider.get(), autocomplete_match.provider);
108 EXPECT_EQ(type, autocomplete_match.type);
109 EXPECT_EQ(relevance, autocomplete_match.relevance);
110 EXPECT_EQ(match_url, autocomplete_match.destination_url);
111 EXPECT_EQ(expected_contents, autocomplete_match.contents);
112 EXPECT_TRUE(std::equal(expected_contents_class.begin(),
113 expected_contents_class.end(),
114 autocomplete_match.contents_class.begin()));
115 EXPECT_EQ(match_title, autocomplete_match.description);
116 EXPECT_TRUE(std::equal(expected_description_class.begin(),
117 expected_description_class.end(),
118 autocomplete_match.description_class.begin()));
119 EXPECT_EQ(expected_contents, autocomplete_match.fill_into_edit);
120 EXPECT_TRUE(autocomplete_match.allowed_to_be_default_match);
121 EXPECT_EQ(expected_inline_autocompletion,
122 autocomplete_match.inline_autocompletion);
123 }
124
125 TEST_F(TitledUrlMatchUtilsTest, EmptyInlineAutocompletion) {
126 // The search term matches the title but not the URL. Since there is no URL
127 // match, the inline autocompletion string will be empty.
128 base::string16 input_text(base::ASCIIToUTF16("goo"));
129 base::string16 match_title(base::ASCIIToUTF16("Email by Google"));
130 base::string16 match_url_string(base::ASCIIToUTF16("https://www.gmail.com/"));
131 GURL match_url(match_url_string);
132 bookmarks::TitledUrlMatch::MatchPositions title_match_positions = {{9, 12}};
133 bookmarks::TitledUrlMatch::MatchPositions url_match_positions;
134 AutocompleteMatchType::Type type = AutocompleteMatchType::BOOKMARK_TITLE;
135 int relevance = 123;
136
137 MockTitledUrlNode node(match_title, match_url);
138 bookmarks::TitledUrlMatch titled_url_match;
139 titled_url_match.node = &node;
140 titled_url_match.title_match_positions = title_match_positions;
141 titled_url_match.url_match_positions = url_match_positions;
142
143 scoped_refptr<MockAutocompleteProvider> provider =
144 new MockAutocompleteProvider(AutocompleteProvider::Type::TYPE_BOOKMARK);
145 TestSchemeClassifier classifier;
146 AutocompleteInput input(input_text, base::string16::npos, std::string(),
147 GURL(), metrics::OmniboxEventProto::NTP, false, false,
148 true, true, false, classifier);
149 const base::string16 fixed_up_input(input_text);
150
151 AutocompleteMatch autocomplete_match = TitledUrlMatchToAutocompleteMatch(
152 titled_url_match, type, relevance, provider.get(), classifier, input,
153 fixed_up_input);
154
155 ACMatchClassifications expected_contents_class = {
156 {0, ACMatchClassification::URL},
157 };
158 ACMatchClassifications expected_description_class = {
159 {0, ACMatchClassification::NONE},
160 {9, ACMatchClassification::MATCH},
161 {12, ACMatchClassification::NONE},
162 };
163 base::string16 expected_contents(base::ASCIIToUTF16("https://www.gmail.com"));
164
165 EXPECT_EQ(provider.get(), autocomplete_match.provider);
166 EXPECT_EQ(type, autocomplete_match.type);
167 EXPECT_EQ(relevance, autocomplete_match.relevance);
168 EXPECT_EQ(match_url, autocomplete_match.destination_url);
169 EXPECT_EQ(expected_contents, autocomplete_match.contents);
170 EXPECT_TRUE(std::equal(expected_contents_class.begin(),
171 expected_contents_class.end(),
172 autocomplete_match.contents_class.begin()));
173 EXPECT_EQ(match_title, autocomplete_match.description);
174 EXPECT_TRUE(std::equal(expected_description_class.begin(),
175 expected_description_class.end(),
176 autocomplete_match.description_class.begin()));
177 EXPECT_EQ(expected_contents, autocomplete_match.fill_into_edit);
178 EXPECT_FALSE(autocomplete_match.allowed_to_be_default_match);
179 EXPECT_TRUE(autocomplete_match.inline_autocompletion.empty());
180 }
181
182 TEST_F(TitledUrlMatchUtilsTest, CorrectTitleAndMatchPositions) {
183 bookmarks::TitledUrlMatch::MatchPositions match_positions = {{2, 6},
184 {10, 15}};
185 base::string16 title = base::ASCIIToUTF16(" Leading whitespace");
186 bookmarks::TitledUrlMatch::MatchPositions expected_match_positions = {
187 {0, 4}, {8, 13}};
188 base::string16 expected_title = base::ASCIIToUTF16("Leading whitespace");
189 CorrectTitleAndMatchPositions(&title, &match_positions);
190 EXPECT_EQ(expected_title, title);
191 EXPECT_TRUE(std::equal(match_positions.begin(), match_positions.end(),
192 expected_match_positions.begin()));
193 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698