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