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

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

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

Powered by Google App Engine
This is Rietveld 408576698