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 "chrome/browser/autocomplete/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/autocomplete_input.h"
11 #include "chrome/browser/autocomplete/autocomplete_match.h"
12 #include "chrome/browser/autocomplete/autocomplete_provider.h"
13 #include "chrome/common/url_constants.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 using base::ASCIIToUTF16
;
19 class BuiltinProviderTest
: public testing::Test
{
22 const base::string16 input
;
23 const size_t num_results
;
27 BuiltinProviderTest() : provider_(NULL
) {}
28 virtual ~BuiltinProviderTest() {}
30 virtual void SetUp() OVERRIDE
{ provider_
= new BuiltinProvider(NULL
, NULL
); }
31 virtual void TearDown() OVERRIDE
{ provider_
= NULL
; }
33 void RunTest(const TestData cases
[], size_t num_cases
) {
35 for (size_t i
= 0; i
< num_cases
; ++i
) {
36 SCOPED_TRACE(base::StringPrintf(
37 "case %" PRIuS
": %s", i
, base::UTF16ToUTF8(cases
[i
].input
).c_str()));
38 const AutocompleteInput
input(cases
[i
].input
, base::string16::npos
,
39 base::string16(), GURL(),
40 AutocompleteInput::INVALID_SPEC
,
41 true, false, true, true);
42 provider_
->Start(input
, false);
43 EXPECT_TRUE(provider_
->done());
44 matches
= provider_
->matches();
45 EXPECT_EQ(cases
[i
].num_results
, matches
.size());
46 if (matches
.size() == cases
[i
].num_results
) {
47 for (size_t j
= 0; j
< cases
[i
].num_results
; ++j
) {
48 EXPECT_EQ(cases
[i
].output
[j
], matches
[j
].destination_url
);
49 EXPECT_FALSE(matches
[j
].allowed_to_be_default_match
);
56 scoped_refptr
<BuiltinProvider
> provider_
;
58 DISALLOW_COPY_AND_ASSIGN(BuiltinProviderTest
);
61 #if !defined(OS_ANDROID)
62 TEST_F(BuiltinProviderTest
, TypingScheme
) {
63 const base::string16 kAbout
= ASCIIToUTF16(content::kAboutScheme
);
64 const base::string16 kChrome
= ASCIIToUTF16(content::kChromeUIScheme
);
65 const base::string16 kSeparator1
= ASCIIToUTF16(":");
66 const base::string16 kSeparator2
= ASCIIToUTF16(":/");
67 const base::string16 kSeparator3
=
68 ASCIIToUTF16(content::kStandardSchemeSeparator
);
70 // These default URLs should correspond with those in BuiltinProvider::Start.
71 const GURL kURL1
= GURL(chrome::kChromeUIChromeURLsURL
);
72 const GURL kURL2
= GURL(chrome::kChromeUISettingsURL
);
73 const GURL kURL3
= GURL(chrome::kChromeUIVersionURL
);
75 TestData typing_scheme_cases
[] = {
76 // Typing an unrelated scheme should give nothing.
77 {ASCIIToUTF16("h"), 0, {}},
78 {ASCIIToUTF16("http"), 0, {}},
79 {ASCIIToUTF16("file"), 0, {}},
80 {ASCIIToUTF16("abouz"), 0, {}},
81 {ASCIIToUTF16("aboutt"), 0, {}},
82 {ASCIIToUTF16("aboutt:"), 0, {}},
83 {ASCIIToUTF16("chroma"), 0, {}},
84 {ASCIIToUTF16("chromee"), 0, {}},
85 {ASCIIToUTF16("chromee:"), 0, {}},
87 // Typing a portion of about:// should give the default urls.
88 {kAbout
.substr(0, 1), 3, {kURL1
, kURL2
, kURL3
}},
89 {ASCIIToUTF16("A"), 3, {kURL1
, kURL2
, kURL3
}},
90 {kAbout
, 3, {kURL1
, kURL2
, kURL3
}},
91 {kAbout
+ kSeparator1
, 3, {kURL1
, kURL2
, kURL3
}},
92 {kAbout
+ kSeparator2
, 3, {kURL1
, kURL2
, kURL3
}},
93 {kAbout
+ kSeparator3
, 3, {kURL1
, kURL2
, kURL3
}},
94 {ASCIIToUTF16("aBoUT://"), 3, {kURL1
, kURL2
, kURL3
}},
96 // Typing a portion of chrome:// should give the default urls.
97 {kChrome
.substr(0, 1), 3, {kURL1
, kURL2
, kURL3
}},
98 {ASCIIToUTF16("C"), 3, {kURL1
, kURL2
, kURL3
}},
99 {kChrome
, 3, {kURL1
, kURL2
, kURL3
}},
100 {kChrome
+ kSeparator1
, 3, {kURL1
, kURL2
, kURL3
}},
101 {kChrome
+ kSeparator2
, 3, {kURL1
, kURL2
, kURL3
}},
102 {kChrome
+ kSeparator3
, 3, {kURL1
, kURL2
, kURL3
}},
103 {ASCIIToUTF16("ChRoMe://"), 3, {kURL1
, kURL2
, kURL3
}},
106 RunTest(typing_scheme_cases
, arraysize(typing_scheme_cases
));
108 #else // Android uses a subset of the URLs
109 TEST_F(BuiltinProviderTest
, TypingScheme
) {
110 const base::string16 kAbout
= ASCIIToUTF16(content::kAboutScheme
);
111 const base::string16 kChrome
= ASCIIToUTF16(content::kChromeUIScheme
);
112 const base::string16 kSeparator1
= ASCIIToUTF16(":");
113 const base::string16 kSeparator2
= ASCIIToUTF16(":/");
114 const base::string16 kSeparator3
=
115 ASCIIToUTF16(content::kStandardSchemeSeparator
);
117 // These default URLs should correspond with those in BuiltinProvider::Start.
118 const GURL kURL1
= GURL(chrome::kChromeUIChromeURLsURL
);
119 const GURL kURL2
= GURL(chrome::kChromeUIVersionURL
);
121 TestData typing_scheme_cases
[] = {
122 // Typing an unrelated scheme should give nothing.
123 {ASCIIToUTF16("h"), 0, {}},
124 {ASCIIToUTF16("http"), 0, {}},
125 {ASCIIToUTF16("file"), 0, {}},
126 {ASCIIToUTF16("abouz"), 0, {}},
127 {ASCIIToUTF16("aboutt"), 0, {}},
128 {ASCIIToUTF16("aboutt:"), 0, {}},
129 {ASCIIToUTF16("chroma"), 0, {}},
130 {ASCIIToUTF16("chromee"), 0, {}},
131 {ASCIIToUTF16("chromee:"), 0, {}},
133 // Typing a portion of about:// should give the default urls.
134 {kAbout
.substr(0, 1), 2, {kURL1
, kURL2
}},
135 {ASCIIToUTF16("A"), 2, {kURL1
, kURL2
}},
136 {kAbout
, 2, {kURL1
, kURL2
}},
137 {kAbout
+ kSeparator1
, 2, {kURL1
, kURL2
}},
138 {kAbout
+ kSeparator2
, 2, {kURL1
, kURL2
}},
139 {kAbout
+ kSeparator3
, 2, {kURL1
, kURL2
}},
140 {ASCIIToUTF16("aBoUT://"), 2, {kURL1
, kURL2
}},
142 // Typing a portion of chrome:// should give the default urls.
143 {kChrome
.substr(0, 1), 2, {kURL1
, kURL2
}},
144 {ASCIIToUTF16("C"), 2, {kURL1
, kURL2
}},
145 {kChrome
, 2, {kURL1
, kURL2
}},
146 {kChrome
+ kSeparator1
, 2, {kURL1
, kURL2
}},
147 {kChrome
+ kSeparator2
, 2, {kURL1
, kURL2
}},
148 {kChrome
+ kSeparator3
, 2, {kURL1
, kURL2
}},
149 {ASCIIToUTF16("ChRoMe://"), 2, {kURL1
, kURL2
}},
152 RunTest(typing_scheme_cases
, arraysize(typing_scheme_cases
));
156 TEST_F(BuiltinProviderTest
, NonChromeURLs
) {
157 TestData non_chrome_url_cases
[] = {
158 // Typing an unrelated scheme should give nothing.
159 {ASCIIToUTF16("g@rb@g3"), 0, {}},
160 {ASCIIToUTF16("www.google.com"), 0, {}},
161 {ASCIIToUTF16("http:www.google.com"), 0, {}},
162 {ASCIIToUTF16("http://www.google.com"), 0, {}},
163 {ASCIIToUTF16("file:filename"), 0, {}},
164 {ASCIIToUTF16("scheme:"), 0, {}},
165 {ASCIIToUTF16("scheme://"), 0, {}},
166 {ASCIIToUTF16("scheme://host"), 0, {}},
167 {ASCIIToUTF16("scheme:host/path?query#ref"), 0, {}},
168 {ASCIIToUTF16("scheme://host/path?query#ref"), 0, {}},
171 RunTest(non_chrome_url_cases
, arraysize(non_chrome_url_cases
));
174 TEST_F(BuiltinProviderTest
, ChromeURLs
) {
175 const base::string16 kAbout
= ASCIIToUTF16(content::kAboutScheme
);
176 const base::string16 kChrome
= ASCIIToUTF16(content::kChromeUIScheme
);
177 const base::string16 kSeparator1
= ASCIIToUTF16(":");
178 const base::string16 kSeparator2
= ASCIIToUTF16(":/");
179 const base::string16 kSeparator3
=
180 ASCIIToUTF16(content::kStandardSchemeSeparator
);
182 // This makes assumptions about the chrome URLs listed by the BuiltinProvider.
183 // Currently they are derived from chrome::kChromeHostURLs[].
184 const base::string16 kHostM1
=
185 ASCIIToUTF16(content::kChromeUIMediaInternalsHost
);
186 const base::string16 kHostM2
=
187 ASCIIToUTF16(chrome::kChromeUIMemoryHost
);
188 const base::string16 kHostM3
=
189 ASCIIToUTF16(chrome::kChromeUIMemoryInternalsHost
);
190 const GURL kURLM1
= GURL(kChrome
+ kSeparator3
+ kHostM1
);
191 const GURL kURLM2
= GURL(kChrome
+ kSeparator3
+ kHostM2
);
192 const GURL kURLM3
= GURL(kChrome
+ kSeparator3
+ kHostM3
);
194 TestData chrome_url_cases
[] = {
195 // Typing an about URL with an unknown host should give nothing.
196 {kAbout
+ kSeparator1
+ ASCIIToUTF16("host"), 0, {}},
197 {kAbout
+ kSeparator2
+ ASCIIToUTF16("host"), 0, {}},
198 {kAbout
+ kSeparator3
+ ASCIIToUTF16("host"), 0, {}},
200 // Typing a chrome URL with an unknown host should give nothing.
201 {kChrome
+ kSeparator1
+ ASCIIToUTF16("host"), 0, {}},
202 {kChrome
+ kSeparator2
+ ASCIIToUTF16("host"), 0, {}},
203 {kChrome
+ kSeparator3
+ ASCIIToUTF16("host"), 0, {}},
205 // Typing an about URL should provide matching URLs.
206 {kAbout
+ kSeparator1
+ kHostM1
.substr(0, 1), 3, {kURLM1
, kURLM2
, kURLM3
}},
207 {kAbout
+ kSeparator2
+ kHostM1
.substr(0, 2), 3, {kURLM1
, kURLM2
, kURLM3
}},
208 {kAbout
+ kSeparator3
+ kHostM1
.substr(0, 3), 1, {kURLM1
}},
209 {kAbout
+ kSeparator3
+ kHostM2
.substr(0, 3), 2, {kURLM2
, kURLM3
}},
210 {kAbout
+ kSeparator3
+ kHostM1
, 1, {kURLM1
}},
211 {kAbout
+ kSeparator2
+ kHostM2
, 2, {kURLM2
, kURLM3
}},
212 {kAbout
+ kSeparator2
+ kHostM3
, 1, {kURLM3
}},
214 // Typing a chrome URL should provide matching URLs.
215 {kChrome
+ kSeparator1
+ kHostM1
.substr(0, 1), 3, {kURLM1
, kURLM2
, kURLM3
}},
216 {kChrome
+ kSeparator2
+ kHostM1
.substr(0, 2), 3, {kURLM1
, kURLM2
, kURLM3
}},
217 {kChrome
+ kSeparator3
+ kHostM1
.substr(0, 3), 1, {kURLM1
}},
218 {kChrome
+ kSeparator3
+ kHostM2
.substr(0, 3), 2, {kURLM2
, kURLM3
}},
219 {kChrome
+ kSeparator3
+ kHostM1
, 1, {kURLM1
}},
220 {kChrome
+ kSeparator2
+ kHostM2
, 2, {kURLM2
, kURLM3
}},
221 {kChrome
+ kSeparator2
+ kHostM3
, 1, {kURLM3
}},
224 RunTest(chrome_url_cases
, arraysize(chrome_url_cases
));
227 TEST_F(BuiltinProviderTest
, AboutBlank
) {
228 const base::string16 kAbout
= ASCIIToUTF16(content::kAboutScheme
);
229 const base::string16 kChrome
= ASCIIToUTF16(content::kChromeUIScheme
);
230 const base::string16 kAboutBlank
= ASCIIToUTF16(content::kAboutBlankURL
);
231 const base::string16 kBlank
= ASCIIToUTF16("blank");
232 const base::string16 kSeparator1
=
233 ASCIIToUTF16(content::kStandardSchemeSeparator
);
234 const base::string16 kSeparator2
= ASCIIToUTF16(":///");
235 const base::string16 kSeparator3
= ASCIIToUTF16(";///");
237 const GURL kURLBlob
= GURL(kChrome
+ kSeparator1
+
238 ASCIIToUTF16(content::kChromeUIBlobInternalsHost
));
239 const GURL kURLBlank
= GURL(kAboutBlank
);
241 TestData about_blank_cases
[] = {
242 // Typing an about:blank prefix should yield about:blank, among other URLs.
243 {kAboutBlank
.substr(0, 8), 2, {kURLBlank
, kURLBlob
}},
244 {kAboutBlank
.substr(0, 9), 1, {kURLBlank
}},
246 // Using any separator that is supported by fixup should yield about:blank.
247 // For now, BuiltinProvider does not suggest url-what-you-typed matches for
248 // for about:blank; check "about:blan" and "about;blan" substrings instead.
249 {kAbout
+ kSeparator2
.substr(0, 1) + kBlank
.substr(0, 4), 1, {kURLBlank
}},
250 {kAbout
+ kSeparator2
.substr(0, 2) + kBlank
, 1, {kURLBlank
}},
251 {kAbout
+ kSeparator2
.substr(0, 3) + kBlank
, 1, {kURLBlank
}},
252 {kAbout
+ kSeparator2
+ kBlank
, 1, {kURLBlank
}},
253 {kAbout
+ kSeparator3
.substr(0, 1) + kBlank
.substr(0, 4), 1, {kURLBlank
}},
254 {kAbout
+ kSeparator3
.substr(0, 2) + kBlank
, 1, {kURLBlank
}},
255 {kAbout
+ kSeparator3
.substr(0, 3) + kBlank
, 1, {kURLBlank
}},
256 {kAbout
+ kSeparator3
+ kBlank
, 1, {kURLBlank
}},
258 // Using the chrome scheme should not yield about:blank.
259 {kChrome
+ kSeparator1
.substr(0, 1) + kBlank
, 0, {}},
260 {kChrome
+ kSeparator1
.substr(0, 2) + kBlank
, 0, {}},
261 {kChrome
+ kSeparator1
.substr(0, 3) + kBlank
, 0, {}},
262 {kChrome
+ kSeparator1
+ kBlank
, 0, {}},
264 // Adding trailing text should not yield about:blank.
265 {kAboutBlank
+ ASCIIToUTF16("/"), 0, {}},
266 {kAboutBlank
+ ASCIIToUTF16("/p"), 0, {}},
267 {kAboutBlank
+ ASCIIToUTF16("x"), 0, {}},
268 {kAboutBlank
+ ASCIIToUTF16("?q"), 0, {}},
269 {kAboutBlank
+ ASCIIToUTF16("#r"), 0, {}},
271 // Interrupting "blank" with conflicting text should not yield about:blank.
272 {kAboutBlank
.substr(0, 9) + ASCIIToUTF16("/"), 0, {}},
273 {kAboutBlank
.substr(0, 9) + ASCIIToUTF16("/p"), 0, {}},
274 {kAboutBlank
.substr(0, 9) + ASCIIToUTF16("x"), 0, {}},
275 {kAboutBlank
.substr(0, 9) + ASCIIToUTF16("?q"), 0, {}},
276 {kAboutBlank
.substr(0, 9) + ASCIIToUTF16("#r"), 0, {}},
279 RunTest(about_blank_cases
, arraysize(about_blank_cases
));
282 #if !defined(OS_ANDROID)
283 // Disabled on Android where we use native UI instead of chrome://settings.
284 TEST_F(BuiltinProviderTest
, ChromeSettingsSubpages
) {
285 // This makes assumptions about the chrome URLs listed by the BuiltinProvider.
286 // Currently they are derived from chrome::kChromeHostURLs[].
287 const base::string16 kSettings
= ASCIIToUTF16(chrome::kChromeUISettingsURL
);
288 const base::string16 kDefaultPage1
= ASCIIToUTF16(chrome::kAutofillSubPage
);
289 const base::string16 kDefaultPage2
=
290 ASCIIToUTF16(chrome::kClearBrowserDataSubPage
);
291 const GURL kDefaultURL1
= GURL(kSettings
+ kDefaultPage1
);
292 const GURL kDefaultURL2
= GURL(kSettings
+ kDefaultPage2
);
293 const base::string16 kPage1
= ASCIIToUTF16(chrome::kSearchEnginesSubPage
);
294 const base::string16 kPage2
= ASCIIToUTF16(chrome::kSyncSetupSubPage
);
295 const GURL kURL1
= GURL(kSettings
+ kPage1
);
296 const GURL kURL2
= GURL(kSettings
+ kPage2
);
298 TestData settings_subpage_cases
[] = {
299 // Typing the settings path should show settings and the first two subpages.
300 {kSettings
, 3, {GURL(kSettings
), kDefaultURL1
, kDefaultURL2
}},
302 // Typing a subpage path should return the appropriate results.
303 {kSettings
+ kPage1
.substr(0, 1), 2, {kURL1
, kURL2
}},
304 {kSettings
+ kPage1
.substr(0, 2), 1, {kURL1
}},
305 {kSettings
+ kPage1
.substr(0, kPage1
.length() - 1), 1, {kURL1
}},
306 {kSettings
+ kPage1
, 1, {kURL1
}},
307 {kSettings
+ kPage2
, 1, {kURL2
}},
310 RunTest(settings_subpage_cases
, arraysize(settings_subpage_cases
));