[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / autocomplete / builtin_provider_unittest.cc
bloba7df4be9324169657383caf853e7ec2b24bdd200
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/message_loop/message_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/autocomplete/autocomplete_input.h"
10 #include "chrome/browser/autocomplete/autocomplete_match.h"
11 #include "chrome/browser/autocomplete/autocomplete_provider.h"
12 #include "chrome/common/url_constants.h"
13 #include "chrome/test/base/testing_browser_process.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "url/gurl.h"
17 using base::ASCIIToUTF16;
19 class BuiltinProviderTest : public testing::Test {
20 protected:
21 template<class ResultType>
22 struct test_data {
23 const base::string16 input;
24 const size_t num_results;
25 const ResultType output[3];
28 BuiltinProviderTest() : builtin_provider_(NULL) {}
29 virtual ~BuiltinProviderTest() {}
31 virtual void SetUp();
32 virtual void TearDown();
34 template<class ResultType>
35 void RunTest(test_data<ResultType>* builtin_cases,
36 int num_cases,
37 ResultType AutocompleteMatch::* member);
39 protected:
40 scoped_refptr<BuiltinProvider> builtin_provider_;
43 void BuiltinProviderTest::SetUp() {
44 builtin_provider_ = new BuiltinProvider(NULL, NULL);
47 void BuiltinProviderTest::TearDown() {
48 builtin_provider_ = NULL;
51 template<class ResultType>
52 void BuiltinProviderTest::RunTest(test_data<ResultType>* builtin_cases,
53 int num_cases,
54 ResultType AutocompleteMatch::* member) {
55 ACMatches matches;
56 for (int i = 0; i < num_cases; ++i) {
57 AutocompleteInput input(builtin_cases[i].input, base::string16::npos,
58 base::string16(), GURL(),
59 AutocompleteInput::INVALID_SPEC, true,
60 false, true, true);
61 builtin_provider_->Start(input, false);
62 EXPECT_TRUE(builtin_provider_->done());
63 matches = builtin_provider_->matches();
64 EXPECT_EQ(builtin_cases[i].num_results, matches.size()) <<
65 ASCIIToUTF16("Input was: ") << builtin_cases[i].input;
66 if (matches.size() == builtin_cases[i].num_results) {
67 for (size_t j = 0; j < builtin_cases[i].num_results; ++j) {
68 EXPECT_EQ(builtin_cases[i].output[j], matches[j].*member) <<
69 ASCIIToUTF16("Input was: ") << builtin_cases[i].input;
70 EXPECT_FALSE(matches[j].allowed_to_be_default_match);
76 #if !defined(OS_ANDROID)
77 TEST_F(BuiltinProviderTest, TypingScheme) {
78 const base::string16 kAbout = ASCIIToUTF16(content::kAboutScheme);
79 const base::string16 kChrome = ASCIIToUTF16(content::kChromeUIScheme);
80 const base::string16 kSeparator1 = ASCIIToUTF16(":");
81 const base::string16 kSeparator2 = ASCIIToUTF16(":/");
82 const base::string16 kSeparator3 =
83 ASCIIToUTF16(content::kStandardSchemeSeparator);
85 // These default URLs should correspond with those in BuiltinProvider::Start.
86 const GURL kURL1 = GURL(chrome::kChromeUIChromeURLsURL);
87 const GURL kURL2 = GURL(chrome::kChromeUISettingsURL);
88 const GURL kURL3 = GURL(chrome::kChromeUIVersionURL);
90 test_data<GURL> typing_scheme_cases[] = {
91 // Typing an unrelated scheme should give nothing.
92 {ASCIIToUTF16("h"), 0, {}},
93 {ASCIIToUTF16("http"), 0, {}},
94 {ASCIIToUTF16("file"), 0, {}},
95 {ASCIIToUTF16("abouz"), 0, {}},
96 {ASCIIToUTF16("aboutt"), 0, {}},
97 {ASCIIToUTF16("aboutt:"), 0, {}},
98 {ASCIIToUTF16("chroma"), 0, {}},
99 {ASCIIToUTF16("chromee"), 0, {}},
100 {ASCIIToUTF16("chromee:"), 0, {}},
102 // Typing a portion of about:// should give the default urls.
103 {kAbout.substr(0, 1), 3, {kURL1, kURL2, kURL3}},
104 {ASCIIToUTF16("A"), 3, {kURL1, kURL2, kURL3}},
105 {kAbout, 3, {kURL1, kURL2, kURL3}},
106 {kAbout + kSeparator1, 3, {kURL1, kURL2, kURL3}},
107 {kAbout + kSeparator2, 3, {kURL1, kURL2, kURL3}},
108 {kAbout + kSeparator3, 3, {kURL1, kURL2, kURL3}},
109 {ASCIIToUTF16("aBoUT://"), 3, {kURL1, kURL2, kURL3}},
111 // Typing a portion of chrome:// should give the default urls.
112 {kChrome.substr(0, 1), 3, {kURL1, kURL2, kURL3}},
113 {ASCIIToUTF16("C"), 3, {kURL1, kURL2, kURL3}},
114 {kChrome, 3, {kURL1, kURL2, kURL3}},
115 {kChrome + kSeparator1, 3, {kURL1, kURL2, kURL3}},
116 {kChrome + kSeparator2, 3, {kURL1, kURL2, kURL3}},
117 {kChrome + kSeparator3, 3, {kURL1, kURL2, kURL3}},
118 {ASCIIToUTF16("ChRoMe://"), 3, {kURL1, kURL2, kURL3}},
121 RunTest<GURL>(typing_scheme_cases, arraysize(typing_scheme_cases),
122 &AutocompleteMatch::destination_url);
124 #else // Android uses a subset of the URLs
125 TEST_F(BuiltinProviderTest, TypingScheme) {
126 const base::string16 kAbout = ASCIIToUTF16(content::kAboutScheme);
127 const base::string16 kChrome = ASCIIToUTF16(content::kChromeUIScheme);
128 const base::string16 kSeparator1 = ASCIIToUTF16(":");
129 const base::string16 kSeparator2 = ASCIIToUTF16(":/");
130 const base::string16 kSeparator3 =
131 ASCIIToUTF16(content::kStandardSchemeSeparator);
133 // These default URLs should correspond with those in BuiltinProvider::Start.
134 const GURL kURL1 = GURL(chrome::kChromeUIChromeURLsURL);
135 const GURL kURL2 = GURL(chrome::kChromeUIVersionURL);
137 test_data<GURL> typing_scheme_cases[] = {
138 // Typing an unrelated scheme should give nothing.
139 {ASCIIToUTF16("h"), 0, {}},
140 {ASCIIToUTF16("http"), 0, {}},
141 {ASCIIToUTF16("file"), 0, {}},
142 {ASCIIToUTF16("abouz"), 0, {}},
143 {ASCIIToUTF16("aboutt"), 0, {}},
144 {ASCIIToUTF16("aboutt:"), 0, {}},
145 {ASCIIToUTF16("chroma"), 0, {}},
146 {ASCIIToUTF16("chromee"), 0, {}},
147 {ASCIIToUTF16("chromee:"), 0, {}},
149 // Typing a portion of about:// should give the default urls.
150 {kAbout.substr(0, 1), 2, {kURL1, kURL2}},
151 {ASCIIToUTF16("A"), 2, {kURL1, kURL2}},
152 {kAbout, 2, {kURL1, kURL2}},
153 {kAbout + kSeparator1, 2, {kURL1, kURL2}},
154 {kAbout + kSeparator2, 2, {kURL1, kURL2}},
155 {kAbout + kSeparator3, 2, {kURL1, kURL2}},
156 {ASCIIToUTF16("aBoUT://"), 2, {kURL1, kURL2}},
158 // Typing a portion of chrome:// should give the default urls.
159 {kChrome.substr(0, 1), 2, {kURL1, kURL2}},
160 {ASCIIToUTF16("C"), 2, {kURL1, kURL2}},
161 {kChrome, 2, {kURL1, kURL2}},
162 {kChrome + kSeparator1, 2, {kURL1, kURL2}},
163 {kChrome + kSeparator2, 2, {kURL1, kURL2}},
164 {kChrome + kSeparator3, 2, {kURL1, kURL2}},
165 {ASCIIToUTF16("ChRoMe://"), 2, {kURL1, kURL2}},
168 RunTest<GURL>(typing_scheme_cases, arraysize(typing_scheme_cases),
169 &AutocompleteMatch::destination_url);
171 #endif
173 TEST_F(BuiltinProviderTest, NonChromeURLs) {
174 test_data<GURL> non_chrome_url_cases[] = {
175 // Typing an unrelated scheme should give nothing.
176 {ASCIIToUTF16("g@rb@g3"), 0, {}},
177 {ASCIIToUTF16("www.google.com"), 0, {}},
178 {ASCIIToUTF16("http:www.google.com"), 0, {}},
179 {ASCIIToUTF16("http://www.google.com"), 0, {}},
180 {ASCIIToUTF16("file:filename"), 0, {}},
181 {ASCIIToUTF16("scheme:"), 0, {}},
182 {ASCIIToUTF16("scheme://"), 0, {}},
183 {ASCIIToUTF16("scheme://host"), 0, {}},
184 {ASCIIToUTF16("scheme:host/path?query#ref"), 0, {}},
185 {ASCIIToUTF16("scheme://host/path?query#ref"), 0, {}},
188 RunTest<GURL>(non_chrome_url_cases, arraysize(non_chrome_url_cases),
189 &AutocompleteMatch::destination_url);
192 TEST_F(BuiltinProviderTest, ChromeURLs) {
193 const base::string16 kAbout = ASCIIToUTF16(content::kAboutScheme);
194 const base::string16 kChrome = ASCIIToUTF16(content::kChromeUIScheme);
195 const base::string16 kSeparator1 = ASCIIToUTF16(":");
196 const base::string16 kSeparator2 = ASCIIToUTF16(":/");
197 const base::string16 kSeparator3 =
198 ASCIIToUTF16(content::kStandardSchemeSeparator);
200 // This makes assumptions about the chrome URLs listed by the BuiltinProvider.
201 // Currently they are derived from chrome::kChromeHostURLs[].
202 const base::string16 kHostM1 =
203 ASCIIToUTF16(content::kChromeUIMediaInternalsHost);
204 const base::string16 kHostM2 =
205 ASCIIToUTF16(chrome::kChromeUIMemoryHost);
206 const base::string16 kHostM3 =
207 ASCIIToUTF16(chrome::kChromeUIMemoryInternalsHost);
208 const GURL kURLM1 = GURL(kChrome + kSeparator3 + kHostM1);
209 const GURL kURLM2 = GURL(kChrome + kSeparator3 + kHostM2);
210 const GURL kURLM3 = GURL(kChrome + kSeparator3 + kHostM3);
212 test_data<GURL> chrome_url_cases[] = {
213 // Typing an about URL with an unknown host should give nothing.
214 {kAbout + kSeparator1 + ASCIIToUTF16("host"), 0, {}},
215 {kAbout + kSeparator2 + ASCIIToUTF16("host"), 0, {}},
216 {kAbout + kSeparator3 + ASCIIToUTF16("host"), 0, {}},
218 // Typing a chrome URL with an unknown host should give nothing.
219 {kChrome + kSeparator1 + ASCIIToUTF16("host"), 0, {}},
220 {kChrome + kSeparator2 + ASCIIToUTF16("host"), 0, {}},
221 {kChrome + kSeparator3 + ASCIIToUTF16("host"), 0, {}},
223 // Typing an about URL should provide matching URLs.
224 {kAbout + kSeparator1 + kHostM1.substr(0, 1), 3, {kURLM1, kURLM2, kURLM3}},
225 {kAbout + kSeparator2 + kHostM1.substr(0, 2), 3, {kURLM1, kURLM2, kURLM3}},
226 {kAbout + kSeparator3 + kHostM1.substr(0, 3), 1, {kURLM1}},
227 {kAbout + kSeparator3 + kHostM2.substr(0, 3), 2, {kURLM2, kURLM3}},
228 {kAbout + kSeparator3 + kHostM1, 1, {kURLM1}},
229 {kAbout + kSeparator2 + kHostM2, 2, {kURLM2, kURLM3}},
230 {kAbout + kSeparator2 + kHostM3, 1, {kURLM3}},
232 // Typing a chrome URL should provide matching URLs.
233 {kChrome + kSeparator1 + kHostM1.substr(0, 1), 3, {kURLM1, kURLM2, kURLM3}},
234 {kChrome + kSeparator2 + kHostM1.substr(0, 2), 3, {kURLM1, kURLM2, kURLM3}},
235 {kChrome + kSeparator3 + kHostM1.substr(0, 3), 1, {kURLM1}},
236 {kChrome + kSeparator3 + kHostM2.substr(0, 3), 2, {kURLM2, kURLM3}},
237 {kChrome + kSeparator3 + kHostM1, 1, {kURLM1}},
238 {kChrome + kSeparator2 + kHostM2, 2, {kURLM2, kURLM3}},
239 {kChrome + kSeparator2 + kHostM3, 1, {kURLM3}},
242 RunTest<GURL>(chrome_url_cases, arraysize(chrome_url_cases),
243 &AutocompleteMatch::destination_url);
246 #if !defined(OS_ANDROID)
247 // Disabled on Android where we use native UI instead of chrome://settings.
248 TEST_F(BuiltinProviderTest, ChromeSettingsSubpages) {
249 // This makes assumptions about the chrome URLs listed by the BuiltinProvider.
250 // Currently they are derived from chrome::kChromeHostURLs[].
251 const base::string16 kSettings = ASCIIToUTF16(chrome::kChromeUISettingsURL);
252 const base::string16 kDefaultPage1 = ASCIIToUTF16(chrome::kAutofillSubPage);
253 const base::string16 kDefaultPage2 =
254 ASCIIToUTF16(chrome::kClearBrowserDataSubPage);
255 const GURL kDefaultURL1 = GURL(kSettings + kDefaultPage1);
256 const GURL kDefaultURL2 = GURL(kSettings + kDefaultPage2);
257 const base::string16 kPage1 = ASCIIToUTF16(chrome::kSearchEnginesSubPage);
258 const base::string16 kPage2 = ASCIIToUTF16(chrome::kSyncSetupSubPage);
259 const GURL kURL1 = GURL(kSettings + kPage1);
260 const GURL kURL2 = GURL(kSettings + kPage2);
262 test_data<GURL> settings_subpage_cases[] = {
263 // Typing the settings path should show settings and the first two subpages.
264 {kSettings, 3, {GURL(kSettings), kDefaultURL1, kDefaultURL2}},
266 // Typing a subpage path should return the appropriate results.
267 {kSettings + kPage1.substr(0, 1), 2, {kURL1, kURL2}},
268 {kSettings + kPage1.substr(0, 2), 1, {kURL1}},
269 {kSettings + kPage1.substr(0, kPage1.length() - 1), 1, {kURL1}},
270 {kSettings + kPage1, 1, {kURL1}},
271 {kSettings + kPage2, 1, {kURL2}},
274 RunTest<GURL>(settings_subpage_cases, arraysize(settings_subpage_cases),
275 &AutocompleteMatch::destination_url);
277 #endif