1 // Copyright (c) 2012 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/browser/builtin_provider.h"
7 #include "base/format_macros.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h"
11 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
12 #include "chrome/common/url_constants.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/metrics/proto/omnibox_event.pb.h"
15 #include "components/omnibox/browser/autocomplete_input.h"
16 #include "components/omnibox/browser/autocomplete_match.h"
17 #include "components/omnibox/browser/autocomplete_provider.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h"
22 using base::ASCIIToUTF16
;
24 class BuiltinProviderTest
: public testing::Test
{
27 const base::string16 input
;
28 const size_t num_results
;
32 BuiltinProviderTest() : provider_(NULL
) {}
33 ~BuiltinProviderTest() override
{}
35 void SetUp() override
{
36 profile_
.reset(new TestingProfile());
37 client_
.reset(new ChromeAutocompleteProviderClient(profile_
.get()));
38 provider_
= new BuiltinProvider(client_
.get());
40 void TearDown() override
{ provider_
= NULL
; }
42 void RunTest(const TestData cases
[], size_t num_cases
) {
44 for (size_t i
= 0; i
< num_cases
; ++i
) {
45 SCOPED_TRACE(base::StringPrintf(
46 "case %" PRIuS
": %s", i
, base::UTF16ToUTF8(cases
[i
].input
).c_str()));
47 const AutocompleteInput
input(
48 cases
[i
].input
, base::string16::npos
, std::string(), GURL(),
49 metrics::OmniboxEventProto::INVALID_SPEC
, true, false, true, true,
50 false, ChromeAutocompleteSchemeClassifier(NULL
));
51 provider_
->Start(input
, false);
52 EXPECT_TRUE(provider_
->done());
53 matches
= provider_
->matches();
54 EXPECT_EQ(cases
[i
].num_results
, matches
.size());
55 if (matches
.size() == cases
[i
].num_results
) {
56 for (size_t j
= 0; j
< cases
[i
].num_results
; ++j
) {
57 EXPECT_EQ(cases
[i
].output
[j
], matches
[j
].destination_url
);
58 EXPECT_FALSE(matches
[j
].allowed_to_be_default_match
);
64 content::TestBrowserThreadBundle thread_bundle_
;
66 scoped_ptr
<TestingProfile
> profile_
;
67 scoped_ptr
<ChromeAutocompleteProviderClient
> client_
;
68 scoped_refptr
<BuiltinProvider
> provider_
;
71 DISALLOW_COPY_AND_ASSIGN(BuiltinProviderTest
);
74 #if !defined(OS_ANDROID)
75 TEST_F(BuiltinProviderTest
, TypingScheme
) {
76 const base::string16 kAbout
= ASCIIToUTF16(url::kAboutScheme
);
77 const base::string16 kChrome
= ASCIIToUTF16(content::kChromeUIScheme
);
78 const base::string16 kSeparator1
= ASCIIToUTF16(":");
79 const base::string16 kSeparator2
= ASCIIToUTF16(":/");
80 const base::string16 kSeparator3
=
81 ASCIIToUTF16(url::kStandardSchemeSeparator
);
83 // These default URLs should correspond with those in BuiltinProvider::Start.
84 const GURL kURL1
= GURL(chrome::kChromeUIChromeURLsURL
);
85 const GURL kURL2
= GURL(chrome::kChromeUISettingsURL
);
86 const GURL kURL3
= GURL(chrome::kChromeUIVersionURL
);
88 TestData typing_scheme_cases
[] = {
89 // Typing an unrelated scheme should give nothing.
90 {ASCIIToUTF16("h"), 0, {}},
91 {ASCIIToUTF16("http"), 0, {}},
92 {ASCIIToUTF16("file"), 0, {}},
93 {ASCIIToUTF16("abouz"), 0, {}},
94 {ASCIIToUTF16("aboutt"), 0, {}},
95 {ASCIIToUTF16("aboutt:"), 0, {}},
96 {ASCIIToUTF16("chroma"), 0, {}},
97 {ASCIIToUTF16("chromee"), 0, {}},
98 {ASCIIToUTF16("chromee:"), 0, {}},
100 // Typing a portion of about:// should give the default urls.
101 {kAbout
.substr(0, 1), 3, {kURL1
, kURL2
, kURL3
}},
102 {ASCIIToUTF16("A"), 3, {kURL1
, kURL2
, kURL3
}},
103 {kAbout
, 3, {kURL1
, kURL2
, kURL3
}},
104 {kAbout
+ kSeparator1
, 3, {kURL1
, kURL2
, kURL3
}},
105 {kAbout
+ kSeparator2
, 3, {kURL1
, kURL2
, kURL3
}},
106 {kAbout
+ kSeparator3
, 3, {kURL1
, kURL2
, kURL3
}},
107 {ASCIIToUTF16("aBoUT://"), 3, {kURL1
, kURL2
, kURL3
}},
109 // Typing a portion of chrome:// should give the default urls.
110 {kChrome
.substr(0, 1), 3, {kURL1
, kURL2
, kURL3
}},
111 {ASCIIToUTF16("C"), 3, {kURL1
, kURL2
, kURL3
}},
112 {kChrome
, 3, {kURL1
, kURL2
, kURL3
}},
113 {kChrome
+ kSeparator1
, 3, {kURL1
, kURL2
, kURL3
}},
114 {kChrome
+ kSeparator2
, 3, {kURL1
, kURL2
, kURL3
}},
115 {kChrome
+ kSeparator3
, 3, {kURL1
, kURL2
, kURL3
}},
116 {ASCIIToUTF16("ChRoMe://"), 3, {kURL1
, kURL2
, kURL3
}},
119 RunTest(typing_scheme_cases
, arraysize(typing_scheme_cases
));
121 #else // Android uses a subset of the URLs
122 TEST_F(BuiltinProviderTest
, TypingScheme
) {
123 const base::string16 kAbout
= ASCIIToUTF16(url::kAboutScheme
);
124 const base::string16 kChrome
= ASCIIToUTF16(content::kChromeUIScheme
);
125 const base::string16 kSeparator1
= ASCIIToUTF16(":");
126 const base::string16 kSeparator2
= ASCIIToUTF16(":/");
127 const base::string16 kSeparator3
=
128 ASCIIToUTF16(url::kStandardSchemeSeparator
);
130 // These default URLs should correspond with those in BuiltinProvider::Start.
131 const GURL kURL1
= GURL(chrome::kChromeUIChromeURLsURL
);
132 const GURL kURL2
= GURL(chrome::kChromeUIVersionURL
);
134 TestData typing_scheme_cases
[] = {
135 // Typing an unrelated scheme should give nothing.
136 {ASCIIToUTF16("h"), 0, {}},
137 {ASCIIToUTF16("http"), 0, {}},
138 {ASCIIToUTF16("file"), 0, {}},
139 {ASCIIToUTF16("abouz"), 0, {}},
140 {ASCIIToUTF16("aboutt"), 0, {}},
141 {ASCIIToUTF16("aboutt:"), 0, {}},
142 {ASCIIToUTF16("chroma"), 0, {}},
143 {ASCIIToUTF16("chromee"), 0, {}},
144 {ASCIIToUTF16("chromee:"), 0, {}},
146 // Typing a portion of about:// should give the default urls.
147 {kAbout
.substr(0, 1), 2, {kURL1
, kURL2
}},
148 {ASCIIToUTF16("A"), 2, {kURL1
, kURL2
}},
149 {kAbout
, 2, {kURL1
, kURL2
}},
150 {kAbout
+ kSeparator1
, 2, {kURL1
, kURL2
}},
151 {kAbout
+ kSeparator2
, 2, {kURL1
, kURL2
}},
152 {kAbout
+ kSeparator3
, 2, {kURL1
, kURL2
}},
153 {ASCIIToUTF16("aBoUT://"), 2, {kURL1
, kURL2
}},
155 // Typing a portion of chrome:// should give the default urls.
156 {kChrome
.substr(0, 1), 2, {kURL1
, kURL2
}},
157 {ASCIIToUTF16("C"), 2, {kURL1
, kURL2
}},
158 {kChrome
, 2, {kURL1
, kURL2
}},
159 {kChrome
+ kSeparator1
, 2, {kURL1
, kURL2
}},
160 {kChrome
+ kSeparator2
, 2, {kURL1
, kURL2
}},
161 {kChrome
+ kSeparator3
, 2, {kURL1
, kURL2
}},
162 {ASCIIToUTF16("ChRoMe://"), 2, {kURL1
, kURL2
}},
165 RunTest(typing_scheme_cases
, arraysize(typing_scheme_cases
));
169 TEST_F(BuiltinProviderTest
, NonChromeURLs
) {
170 TestData non_chrome_url_cases
[] = {
171 // Typing an unrelated scheme should give nothing.
172 {ASCIIToUTF16("g@rb@g3"), 0, {}},
173 {ASCIIToUTF16("www.google.com"), 0, {}},
174 {ASCIIToUTF16("http:www.google.com"), 0, {}},
175 {ASCIIToUTF16("http://www.google.com"), 0, {}},
176 {ASCIIToUTF16("file:filename"), 0, {}},
177 {ASCIIToUTF16("scheme:"), 0, {}},
178 {ASCIIToUTF16("scheme://"), 0, {}},
179 {ASCIIToUTF16("scheme://host"), 0, {}},
180 {ASCIIToUTF16("scheme:host/path?query#ref"), 0, {}},
181 {ASCIIToUTF16("scheme://host/path?query#ref"), 0, {}},
184 RunTest(non_chrome_url_cases
, arraysize(non_chrome_url_cases
));
187 TEST_F(BuiltinProviderTest
, ChromeURLs
) {
188 const base::string16 kAbout
= ASCIIToUTF16(url::kAboutScheme
);
189 const base::string16 kChrome
= ASCIIToUTF16(content::kChromeUIScheme
);
190 const base::string16 kSeparator1
= ASCIIToUTF16(":");
191 const base::string16 kSeparator2
= ASCIIToUTF16(":/");
192 const base::string16 kSeparator3
=
193 ASCIIToUTF16(url::kStandardSchemeSeparator
);
195 // This makes assumptions about the chrome URLs listed by the BuiltinProvider.
196 // Currently they are derived from chrome::kChromeHostURLs[].
197 const base::string16 kHostM1
=
198 ASCIIToUTF16(content::kChromeUIMediaInternalsHost
);
199 const base::string16 kHostM2
=
200 ASCIIToUTF16(chrome::kChromeUIMemoryHost
);
201 const base::string16 kHostM3
=
202 ASCIIToUTF16(chrome::kChromeUIMemoryInternalsHost
);
203 const GURL kURLM1
= GURL(kChrome
+ kSeparator3
+ kHostM1
);
204 const GURL kURLM2
= GURL(kChrome
+ kSeparator3
+ kHostM2
);
205 const GURL kURLM3
= GURL(kChrome
+ kSeparator3
+ kHostM3
);
207 TestData chrome_url_cases
[] = {
208 // Typing an about URL with an unknown host should give nothing.
209 {kAbout
+ kSeparator1
+ ASCIIToUTF16("host"), 0, {}},
210 {kAbout
+ kSeparator2
+ ASCIIToUTF16("host"), 0, {}},
211 {kAbout
+ kSeparator3
+ ASCIIToUTF16("host"), 0, {}},
213 // Typing a chrome URL with an unknown host should give nothing.
214 {kChrome
+ kSeparator1
+ ASCIIToUTF16("host"), 0, {}},
215 {kChrome
+ kSeparator2
+ ASCIIToUTF16("host"), 0, {}},
216 {kChrome
+ kSeparator3
+ ASCIIToUTF16("host"), 0, {}},
218 // Typing an about URL should provide matching URLs.
219 {kAbout
+ kSeparator1
+ kHostM1
.substr(0, 1), 3, {kURLM1
, kURLM2
, kURLM3
}},
220 {kAbout
+ kSeparator2
+ kHostM1
.substr(0, 2), 3, {kURLM1
, kURLM2
, kURLM3
}},
221 {kAbout
+ kSeparator3
+ kHostM1
.substr(0, 3), 1, {kURLM1
}},
222 {kAbout
+ kSeparator3
+ kHostM2
.substr(0, 3), 2, {kURLM2
, kURLM3
}},
223 {kAbout
+ kSeparator3
+ kHostM1
, 1, {kURLM1
}},
224 {kAbout
+ kSeparator2
+ kHostM2
, 2, {kURLM2
, kURLM3
}},
225 {kAbout
+ kSeparator2
+ kHostM3
, 1, {kURLM3
}},
227 // Typing a chrome URL should provide matching URLs.
228 {kChrome
+ kSeparator1
+ kHostM1
.substr(0, 1), 3, {kURLM1
, kURLM2
, kURLM3
}},
229 {kChrome
+ kSeparator2
+ kHostM1
.substr(0, 2), 3, {kURLM1
, kURLM2
, kURLM3
}},
230 {kChrome
+ kSeparator3
+ kHostM1
.substr(0, 3), 1, {kURLM1
}},
231 {kChrome
+ kSeparator3
+ kHostM2
.substr(0, 3), 2, {kURLM2
, kURLM3
}},
232 {kChrome
+ kSeparator3
+ kHostM1
, 1, {kURLM1
}},
233 {kChrome
+ kSeparator2
+ kHostM2
, 2, {kURLM2
, kURLM3
}},
234 {kChrome
+ kSeparator2
+ kHostM3
, 1, {kURLM3
}},
237 RunTest(chrome_url_cases
, arraysize(chrome_url_cases
));
240 TEST_F(BuiltinProviderTest
, AboutBlank
) {
241 const base::string16 kAbout
= ASCIIToUTF16(url::kAboutScheme
);
242 const base::string16 kChrome
= ASCIIToUTF16(content::kChromeUIScheme
);
243 const base::string16 kAboutBlank
= ASCIIToUTF16(url::kAboutBlankURL
);
244 const base::string16 kBlank
= ASCIIToUTF16("blank");
245 const base::string16 kSeparator1
=
246 ASCIIToUTF16(url::kStandardSchemeSeparator
);
247 const base::string16 kSeparator2
= ASCIIToUTF16(":///");
248 const base::string16 kSeparator3
= ASCIIToUTF16(";///");
250 const GURL kURLBlob
= GURL(kChrome
+ kSeparator1
+
251 ASCIIToUTF16(content::kChromeUIBlobInternalsHost
));
252 const GURL kURLBlank
= GURL(kAboutBlank
);
254 TestData about_blank_cases
[] = {
255 // Typing an about:blank prefix should yield about:blank, among other URLs.
256 {kAboutBlank
.substr(0, 8), 2, {kURLBlank
, kURLBlob
}},
257 {kAboutBlank
.substr(0, 9), 1, {kURLBlank
}},
259 // Using any separator that is supported by fixup should yield about:blank.
260 // For now, BuiltinProvider does not suggest url-what-you-typed matches for
261 // for about:blank; check "about:blan" and "about;blan" substrings instead.
262 {kAbout
+ kSeparator2
.substr(0, 1) + kBlank
.substr(0, 4), 1, {kURLBlank
}},
263 {kAbout
+ kSeparator2
.substr(0, 2) + kBlank
, 1, {kURLBlank
}},
264 {kAbout
+ kSeparator2
.substr(0, 3) + kBlank
, 1, {kURLBlank
}},
265 {kAbout
+ kSeparator2
+ kBlank
, 1, {kURLBlank
}},
266 {kAbout
+ kSeparator3
.substr(0, 1) + kBlank
.substr(0, 4), 1, {kURLBlank
}},
267 {kAbout
+ kSeparator3
.substr(0, 2) + kBlank
, 1, {kURLBlank
}},
268 {kAbout
+ kSeparator3
.substr(0, 3) + kBlank
, 1, {kURLBlank
}},
269 {kAbout
+ kSeparator3
+ kBlank
, 1, {kURLBlank
}},
271 // Using the chrome scheme should not yield about:blank.
272 {kChrome
+ kSeparator1
.substr(0, 1) + kBlank
, 0, {}},
273 {kChrome
+ kSeparator1
.substr(0, 2) + kBlank
, 0, {}},
274 {kChrome
+ kSeparator1
.substr(0, 3) + kBlank
, 0, {}},
275 {kChrome
+ kSeparator1
+ kBlank
, 0, {}},
277 // Adding trailing text should not yield about:blank.
278 {kAboutBlank
+ ASCIIToUTF16("/"), 0, {}},
279 {kAboutBlank
+ ASCIIToUTF16("/p"), 0, {}},
280 {kAboutBlank
+ ASCIIToUTF16("x"), 0, {}},
281 {kAboutBlank
+ ASCIIToUTF16("?q"), 0, {}},
282 {kAboutBlank
+ ASCIIToUTF16("#r"), 0, {}},
284 // Interrupting "blank" with conflicting text should not yield about:blank.
285 {kAboutBlank
.substr(0, 9) + ASCIIToUTF16("/"), 0, {}},
286 {kAboutBlank
.substr(0, 9) + ASCIIToUTF16("/p"), 0, {}},
287 {kAboutBlank
.substr(0, 9) + ASCIIToUTF16("x"), 0, {}},
288 {kAboutBlank
.substr(0, 9) + ASCIIToUTF16("?q"), 0, {}},
289 {kAboutBlank
.substr(0, 9) + ASCIIToUTF16("#r"), 0, {}},
292 RunTest(about_blank_cases
, arraysize(about_blank_cases
));
295 TEST_F(BuiltinProviderTest
, DoesNotSupportMatchesOnFocus
) {
296 const AutocompleteInput
input(
297 ASCIIToUTF16("chrome://s"), base::string16::npos
, std::string(), GURL(),
298 metrics::OmniboxEventProto::INVALID_SPEC
, true, false, true, true, true,
299 ChromeAutocompleteSchemeClassifier(NULL
));
300 provider_
->Start(input
, false);
301 EXPECT_TRUE(provider_
->matches().empty());
304 #if !defined(OS_ANDROID)
305 // Disabled on Android where we use native UI instead of chrome://settings.
306 TEST_F(BuiltinProviderTest
, ChromeSettingsSubpages
) {
307 // This makes assumptions about the chrome URLs listed by the BuiltinProvider.
308 // Currently they are derived from chrome::kChromeHostURLs[].
309 const base::string16 kSettings
= ASCIIToUTF16(chrome::kChromeUISettingsURL
);
310 const base::string16 kDefaultPage1
= ASCIIToUTF16(chrome::kAutofillSubPage
);
311 const base::string16 kDefaultPage2
=
312 ASCIIToUTF16(chrome::kClearBrowserDataSubPage
);
313 const GURL kDefaultURL1
= GURL(kSettings
+ kDefaultPage1
);
314 const GURL kDefaultURL2
= GURL(kSettings
+ kDefaultPage2
);
315 const base::string16 kPage1
= ASCIIToUTF16(chrome::kSearchEnginesSubPage
);
316 const base::string16 kPage2
= ASCIIToUTF16(chrome::kSyncSetupSubPage
);
317 const GURL kURL1
= GURL(kSettings
+ kPage1
);
318 const GURL kURL2
= GURL(kSettings
+ kPage2
);
320 TestData settings_subpage_cases
[] = {
321 // Typing the settings path should show settings and the first two subpages.
322 {kSettings
, 3, {GURL(kSettings
), kDefaultURL1
, kDefaultURL2
}},
324 // Typing a subpage path should return the appropriate results.
325 {kSettings
+ kPage1
.substr(0, 1), 2, {kURL1
, kURL2
}},
326 {kSettings
+ kPage1
.substr(0, 2), 1, {kURL1
}},
327 {kSettings
+ kPage1
.substr(0, kPage1
.length() - 1), 1, {kURL1
}},
328 {kSettings
+ kPage1
, 1, {kURL1
}},
329 {kSettings
+ kPage2
, 1, {kURL2
}},
332 RunTest(settings_subpage_cases
, arraysize(settings_subpage_cases
));