1 // Copyright 2014 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.
5 #include "components/omnibox/autocomplete_match.h"
7 #include "base/basictypes.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/omnibox/test_scheme_classifier.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 TEST(AutocompleteMatchTest
, MoreRelevant
) {
13 struct RelevantCases
{
26 AutocompleteMatch
m1(NULL
, 0, false,
27 AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
28 AutocompleteMatch
m2(NULL
, 0, false,
29 AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
31 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
32 m1
.relevance
= cases
[i
].r1
;
33 m2
.relevance
= cases
[i
].r2
;
34 EXPECT_EQ(cases
[i
].expected_result
,
35 AutocompleteMatch::MoreRelevant(m1
, m2
));
39 TEST(AutocompleteMatchTest
, MergeClassifications
) {
40 // Merging two empty vectors should result in an empty vector.
41 EXPECT_EQ(std::string(),
42 AutocompleteMatch::ClassificationsToString(
43 AutocompleteMatch::MergeClassifications(
44 AutocompleteMatch::ACMatchClassifications(),
45 AutocompleteMatch::ACMatchClassifications())));
47 // If one vector is empty and the other is "trivial" but non-empty (i.e. (0,
48 // NONE)), the non-empty vector should be returned.
50 AutocompleteMatch::ClassificationsToString(
51 AutocompleteMatch::MergeClassifications(
52 AutocompleteMatch::ClassificationsFromString("0,0"),
53 AutocompleteMatch::ACMatchClassifications())));
55 AutocompleteMatch::ClassificationsToString(
56 AutocompleteMatch::MergeClassifications(
57 AutocompleteMatch::ACMatchClassifications(),
58 AutocompleteMatch::ClassificationsFromString("0,0"))));
60 // Ditto if the one-entry vector is non-trivial.
62 AutocompleteMatch::ClassificationsToString(
63 AutocompleteMatch::MergeClassifications(
64 AutocompleteMatch::ClassificationsFromString("0,1"),
65 AutocompleteMatch::ACMatchClassifications())));
67 AutocompleteMatch::ClassificationsToString(
68 AutocompleteMatch::MergeClassifications(
69 AutocompleteMatch::ACMatchClassifications(),
70 AutocompleteMatch::ClassificationsFromString("0,1"))));
72 // Merge an unstyled one-entry vector with a styled one-entry vector.
74 AutocompleteMatch::ClassificationsToString(
75 AutocompleteMatch::MergeClassifications(
76 AutocompleteMatch::ClassificationsFromString("0,0"),
77 AutocompleteMatch::ClassificationsFromString("0,1"))));
79 // Test simple cases of overlap.
80 EXPECT_EQ("0,3," "1,2",
81 AutocompleteMatch::ClassificationsToString(
82 AutocompleteMatch::MergeClassifications(
83 AutocompleteMatch::ClassificationsFromString("0,1," "1,0"),
84 AutocompleteMatch::ClassificationsFromString("0,2"))));
85 EXPECT_EQ("0,3," "1,2",
86 AutocompleteMatch::ClassificationsToString(
87 AutocompleteMatch::MergeClassifications(
88 AutocompleteMatch::ClassificationsFromString("0,2"),
89 AutocompleteMatch::ClassificationsFromString("0,1," "1,0"))));
91 // Test the case where both vectors have classifications at the same
94 AutocompleteMatch::ClassificationsToString(
95 AutocompleteMatch::MergeClassifications(
96 AutocompleteMatch::ClassificationsFromString("0,1," "1,2"),
97 AutocompleteMatch::ClassificationsFromString("0,2," "1,1"))));
99 // Test an arbitrary complicated case.
100 EXPECT_EQ("0,2," "1,0," "2,1," "4,3," "5,7," "6,3," "7,7," "15,1," "17,0",
101 AutocompleteMatch::ClassificationsToString(
102 AutocompleteMatch::MergeClassifications(
103 AutocompleteMatch::ClassificationsFromString(
104 "0,0," "2,1," "4,3," "7,7," "10,6," "15,0"),
105 AutocompleteMatch::ClassificationsFromString(
106 "0,2," "1,0," "5,7," "6,1," "17,0"))));
109 TEST(AutocompleteMatchTest
, SupportsDeletion
) {
110 // A non-deletable match with no duplicates.
111 AutocompleteMatch
m(NULL
, 0, false,
112 AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
113 EXPECT_FALSE(m
.SupportsDeletion());
115 // A deletable match with no duplicates.
116 AutocompleteMatch
m1(NULL
, 0, true,
117 AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
118 EXPECT_TRUE(m1
.SupportsDeletion());
120 // A non-deletable match, with non-deletable duplicates.
121 m
.duplicate_matches
.push_back(AutocompleteMatch(
122 NULL
, 0, false, AutocompleteMatchType::URL_WHAT_YOU_TYPED
));
123 m
.duplicate_matches
.push_back(AutocompleteMatch(
124 NULL
, 0, false, AutocompleteMatchType::URL_WHAT_YOU_TYPED
));
125 EXPECT_FALSE(m
.SupportsDeletion());
127 // A non-deletable match, with at least one deletable duplicate.
128 m
.duplicate_matches
.push_back(AutocompleteMatch(
129 NULL
, 0, true, AutocompleteMatchType::URL_WHAT_YOU_TYPED
));
130 EXPECT_TRUE(m
.SupportsDeletion());
133 TEST(AutocompleteMatchTest
, Duplicates
) {
134 struct DuplicateCases
{
135 const wchar_t* input
;
136 const std::string url1
;
137 const std::string url2
;
138 const bool expected_duplicate
;
140 { L
"g", "http://www.google.com/", "https://www.google.com/", true },
141 { L
"g", "http://www.google.com/", "http://www.google.com", true },
142 { L
"g", "http://google.com/", "http://www.google.com/", true },
143 { L
"g", "http://www.google.com/", "HTTP://www.GOOGLE.com/", true },
144 { L
"g", "http://www.google.com/1", "http://www.google.com/1/", true },
145 { L
"g", "http://www.google.com/", "http://www.google.com", true },
146 { L
"g", "https://www.google.com/", "http://google.com", true },
147 { L
"g", "http://www.google.com/", "wss://www.google.com/", false },
148 { L
"g", "http://www.google.com/", "http://www.google.com/1", false },
149 { L
"g", "http://www.google.com/", "http://www.goo.com/", false },
150 { L
"g", "http://www.google.com/", "http://w2.google.com/", false },
151 { L
"g", "http://www.google.com/", "http://m.google.com/", false },
152 { L
"g", "http://www.google.com/", "http://www.google.com/?foo", false },
154 // Don't allow URLs with different schemes to be considered duplicates for
156 { L
"http://g", "http://google.com/",
157 "https://google.com/", false },
158 { L
"http://g", "http://blah.com/",
159 "https://blah.com/", true },
160 { L
"http://g", "http://google.com/1",
161 "https://google.com/1", false },
162 { L
"http://g hello", "http://google.com/",
163 "https://google.com/", false },
164 { L
"hello http://g", "http://google.com/",
165 "https://google.com/", false },
166 { L
"hello http://g", "http://blah.com/",
167 "https://blah.com/", true },
168 { L
"http://b http://g", "http://google.com/",
169 "https://google.com/", false },
170 { L
"http://b http://g", "http://blah.com/",
171 "https://blah.com/", false },
173 // If the user types unicode that matches the beginning of a
174 // punycode-encoded hostname then consider that a match.
175 { L
"x", "http://xn--1lq90ic7f1rc.cn/",
176 "https://xn--1lq90ic7f1rc.cn/", true },
177 { L
"http://\x5317 x", "http://xn--1lq90ic7f1rc.cn/",
178 "https://xn--1lq90ic7f1rc.cn/", false },
179 { L
"http://\x89c6 x", "http://xn--1lq90ic7f1rc.cn/",
180 "https://xn--1lq90ic7f1rc.cn/", true },
183 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
184 SCOPED_TRACE("input=" + base::WideToUTF8(cases
[i
].input
) +
185 " url1=" + cases
[i
].url1
+ " url2=" + cases
[i
].url2
);
186 AutocompleteInput
input(
187 base::WideToUTF16(cases
[i
].input
), base::string16::npos
, std::string(),
188 GURL(), metrics::OmniboxEventProto::INVALID_SPEC
, false, false, true,
189 true, false, TestSchemeClassifier());
190 AutocompleteMatch
m1(NULL
, 100, false,
191 AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
192 m1
.destination_url
= GURL(cases
[i
].url1
);
193 m1
.ComputeStrippedDestinationURL(input
, "zh-CN", NULL
);
194 AutocompleteMatch
m2(NULL
, 100, false,
195 AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
196 m2
.destination_url
= GURL(cases
[i
].url2
);
197 m2
.ComputeStrippedDestinationURL(input
, "zh-CN", NULL
);
198 EXPECT_EQ(cases
[i
].expected_duplicate
,
199 AutocompleteMatch::DestinationsEqual(m1
, m2
));