OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "base/message_loop.h" |
| 6 #include "base/utf_string_conversions.h" |
| 7 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 8 #include "chrome/browser/autocomplete/builtin_provider.h" |
| 9 #include "chrome/common/url_constants.h" |
| 10 #include "chrome/test/testing_browser_process.h" |
| 11 #include "chrome/test/testing_browser_process_test.h" |
| 12 #include "googleurl/src/gurl.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 class BuiltinProviderTest : public TestingBrowserProcessTest { |
| 16 protected: |
| 17 template<class ResultType> |
| 18 struct test_data { |
| 19 const string16 input; |
| 20 const size_t num_results; |
| 21 const ResultType output[3]; |
| 22 }; |
| 23 |
| 24 BuiltinProviderTest() : builtin_provider_(NULL) { } |
| 25 virtual ~BuiltinProviderTest() { } |
| 26 |
| 27 virtual void SetUp(); |
| 28 virtual void TearDown(); |
| 29 |
| 30 template<class ResultType> |
| 31 void RunTest(test_data<ResultType>* builtin_cases, |
| 32 int num_cases, |
| 33 ResultType AutocompleteMatch::* member); |
| 34 |
| 35 protected: |
| 36 scoped_refptr<BuiltinProvider> builtin_provider_; |
| 37 }; |
| 38 |
| 39 void BuiltinProviderTest::SetUp() { |
| 40 builtin_provider_ = new BuiltinProvider(NULL, NULL); |
| 41 } |
| 42 |
| 43 void BuiltinProviderTest::TearDown() { |
| 44 builtin_provider_ = NULL; |
| 45 } |
| 46 |
| 47 template<class ResultType> |
| 48 void BuiltinProviderTest::RunTest(test_data<ResultType>* builtin_cases, |
| 49 int num_cases, |
| 50 ResultType AutocompleteMatch::* member) { |
| 51 ACMatches matches; |
| 52 for (int i = 0; i < num_cases; ++i) { |
| 53 AutocompleteInput input(builtin_cases[i].input, string16(), true, |
| 54 false, true, AutocompleteInput::ALL_MATCHES); |
| 55 builtin_provider_->Start(input, false); |
| 56 EXPECT_TRUE(builtin_provider_->done()); |
| 57 matches = builtin_provider_->matches(); |
| 58 EXPECT_EQ(builtin_cases[i].num_results, matches.size()) << |
| 59 ASCIIToUTF16("Input was: ") << builtin_cases[i].input; |
| 60 if (matches.size() == builtin_cases[i].num_results) { |
| 61 for (size_t j = 0; j < builtin_cases[i].num_results; ++j) { |
| 62 EXPECT_EQ(builtin_cases[i].output[j], matches[j].*member) << |
| 63 ASCIIToUTF16("Input was: ") << builtin_cases[i].input; |
| 64 } |
| 65 } |
| 66 } |
| 67 } |
| 68 |
| 69 TEST_F(BuiltinProviderTest, TypingScheme) { |
| 70 const string16 kAbout = ASCIIToUTF16(chrome::kAboutScheme); |
| 71 const string16 kChrome = ASCIIToUTF16(chrome::kChromeUIScheme); |
| 72 const string16 kSeparator1 = ASCIIToUTF16(":"); |
| 73 const string16 kSeparator2 = ASCIIToUTF16(":/"); |
| 74 const string16 kSeparator3 = ASCIIToUTF16(chrome::kStandardSchemeSeparator); |
| 75 |
| 76 // These default URLs should correspond with those in BuiltinProvider::Start. |
| 77 const GURL kURL1 = GURL(chrome::kChromeUIChromeURLsURL); |
| 78 const GURL kURL2 = GURL(chrome::kChromeUISettingsURL); |
| 79 const GURL kURL3 = GURL(chrome::kChromeUIVersionURL); |
| 80 |
| 81 test_data<GURL> typing_scheme_cases[] = { |
| 82 // Typing an unrelated scheme should give nothing. |
| 83 {ASCIIToUTF16("h"), 0, {}}, |
| 84 {ASCIIToUTF16("http"), 0, {}}, |
| 85 {ASCIIToUTF16("file"), 0, {}}, |
| 86 {ASCIIToUTF16("abouz"), 0, {}}, |
| 87 {ASCIIToUTF16("aboutt"), 0, {}}, |
| 88 {ASCIIToUTF16("aboutt:"), 0, {}}, |
| 89 {ASCIIToUTF16("chroma"), 0, {}}, |
| 90 {ASCIIToUTF16("chromee"), 0, {}}, |
| 91 {ASCIIToUTF16("chromee:"), 0, {}}, |
| 92 |
| 93 // Typing a portion of about:// should give the default urls. |
| 94 {kAbout.substr(0, 1), 3, {kURL1, kURL2, kURL3}}, |
| 95 {ASCIIToUTF16("A"), 3, {kURL1, kURL2, kURL3}}, |
| 96 {kAbout, 3, {kURL1, kURL2, kURL3}}, |
| 97 {kAbout + kSeparator1, 3, {kURL1, kURL2, kURL3}}, |
| 98 {kAbout + kSeparator2, 3, {kURL1, kURL2, kURL3}}, |
| 99 {kAbout + kSeparator3, 3, {kURL1, kURL2, kURL3}}, |
| 100 {ASCIIToUTF16("aBoUT://"), 3, {kURL1, kURL2, kURL3}}, |
| 101 |
| 102 // Typing a portion of chrome:// should give the default urls. |
| 103 {kChrome.substr(0, 1), 3, {kURL1, kURL2, kURL3}}, |
| 104 {ASCIIToUTF16("C"), 3, {kURL1, kURL2, kURL3}}, |
| 105 {kChrome, 3, {kURL1, kURL2, kURL3}}, |
| 106 {kChrome + kSeparator1, 3, {kURL1, kURL2, kURL3}}, |
| 107 {kChrome + kSeparator2, 3, {kURL1, kURL2, kURL3}}, |
| 108 {kChrome + kSeparator3, 3, {kURL1, kURL2, kURL3}}, |
| 109 {ASCIIToUTF16("ChRoMe://"), 3, {kURL1, kURL2, kURL3}}, |
| 110 }; |
| 111 |
| 112 RunTest<GURL>(typing_scheme_cases, arraysize(typing_scheme_cases), |
| 113 &AutocompleteMatch::destination_url); |
| 114 } |
| 115 |
| 116 TEST_F(BuiltinProviderTest, NonChromeURLs) { |
| 117 test_data<GURL> non_chrome_url_cases[] = { |
| 118 // Typing an unrelated scheme should give nothing. |
| 119 {ASCIIToUTF16("g@rb@g3"), 0, {}}, |
| 120 {ASCIIToUTF16("www.google.com"), 0, {}}, |
| 121 {ASCIIToUTF16("http:www.google.com"), 0, {}}, |
| 122 {ASCIIToUTF16("http://www.google.com"), 0, {}}, |
| 123 {ASCIIToUTF16("file:filename"), 0, {}}, |
| 124 {ASCIIToUTF16("scheme:"), 0, {}}, |
| 125 {ASCIIToUTF16("scheme://"), 0, {}}, |
| 126 {ASCIIToUTF16("scheme://host"), 0, {}}, |
| 127 {ASCIIToUTF16("scheme:host/path?query#ref"), 0, {}}, |
| 128 {ASCIIToUTF16("scheme://host/path?query#ref"), 0, {}}, |
| 129 }; |
| 130 |
| 131 RunTest<GURL>(non_chrome_url_cases, arraysize(non_chrome_url_cases), |
| 132 &AutocompleteMatch::destination_url); |
| 133 } |
| 134 |
| 135 TEST_F(BuiltinProviderTest, ChromeURLs) { |
| 136 const string16 kAbout = ASCIIToUTF16(chrome::kAboutScheme); |
| 137 const string16 kChrome = ASCIIToUTF16(chrome::kChromeUIScheme); |
| 138 const string16 kSeparator1 = ASCIIToUTF16(":"); |
| 139 const string16 kSeparator2 = ASCIIToUTF16(":/"); |
| 140 const string16 kSeparator3 = ASCIIToUTF16(chrome::kStandardSchemeSeparator); |
| 141 |
| 142 // This makes assumptions about the chrome URLs listed by the BuiltinProvider. |
| 143 // Currently they are derived from ChromePaths() in browser_about_handler.cc. |
| 144 const string16 kHostA = ASCIIToUTF16(chrome::kChromeUIAppCacheInternalsHost); |
| 145 const GURL kURLA = GURL(kChrome + kSeparator3 + kHostA); |
| 146 const string16 kHostF1 = ASCIIToUTF16(chrome::kChromeUIFlagsHost); |
| 147 const string16 kHostF2 = ASCIIToUTF16(chrome::kChromeUIFlashHost); |
| 148 const GURL kURLF1 = GURL(kChrome + kSeparator3 + kHostF1); |
| 149 const GURL kURLF2 = GURL(kChrome + kSeparator3 + kHostF2); |
| 150 |
| 151 test_data<GURL> chrome_url_cases[] = { |
| 152 // Typing an about URL with an unknown host should give nothing. |
| 153 {kAbout + kSeparator1 + ASCIIToUTF16("host"), 0, {}}, |
| 154 {kAbout + kSeparator2 + ASCIIToUTF16("host"), 0, {}}, |
| 155 {kAbout + kSeparator3 + ASCIIToUTF16("host"), 0, {}}, |
| 156 |
| 157 // Typing a chrome URL with an unknown host should give nothing. |
| 158 {kChrome + kSeparator1 + ASCIIToUTF16("host"), 0, {}}, |
| 159 {kChrome + kSeparator2 + ASCIIToUTF16("host"), 0, {}}, |
| 160 {kChrome + kSeparator3 + ASCIIToUTF16("host"), 0, {}}, |
| 161 |
| 162 // Typing an about URL for a unique host should provide that full URL. |
| 163 {kAbout + kSeparator1 + kHostA.substr(0, 1), 1, {kURLA}}, |
| 164 {kAbout + kSeparator2 + kHostA.substr(0, 2), 1, {kURLA}}, |
| 165 {kAbout + kSeparator3 + kHostA.substr(0, kHostA.length() - 1), 1, {kURLA}}, |
| 166 {kAbout + kSeparator1 + kHostA, 1, {kURLA}}, |
| 167 {kAbout + kSeparator2 + kHostA, 1, {kURLA}}, |
| 168 {kAbout + kSeparator3 + kHostA, 1, {kURLA}}, |
| 169 |
| 170 // Typing a chrome URL for a unique host should provide that full URL. |
| 171 {kChrome + kSeparator1 + kHostA.substr(0, 1), 1, {kURLA}}, |
| 172 {kChrome + kSeparator2 + kHostA.substr(0, 2), 1, {kURLA}}, |
| 173 {kChrome + kSeparator3 + kHostA.substr(0, kHostA.length() - 1), 1, {kURLA}}, |
| 174 {kChrome + kSeparator1 + kHostA, 1, {kURLA}}, |
| 175 {kChrome + kSeparator2 + kHostA, 1, {kURLA}}, |
| 176 {kChrome + kSeparator3 + kHostA, 1, {kURLA}}, |
| 177 |
| 178 // Typing an about URL with a non-unique host should provide matching URLs. |
| 179 {kAbout + kSeparator1 + kHostF1.substr(0, 1), 2, {kURLF1, kURLF2}}, |
| 180 {kAbout + kSeparator2 + kHostF1.substr(0, 2), 2, {kURLF1, kURLF2}}, |
| 181 {kAbout + kSeparator3 + kHostF2.substr(0, 3), 2, {kURLF1, kURLF2}}, |
| 182 {kAbout + kSeparator3 + kHostF1.substr(0, 4), 1, {kURLF1}}, |
| 183 {kAbout + kSeparator3 + kHostF2.substr(0, 4), 1, {kURLF2}}, |
| 184 {kAbout + kSeparator3 + kHostF1, 1, {kURLF1}}, |
| 185 {kAbout + kSeparator2 + kHostF2, 1, {kURLF2}}, |
| 186 |
| 187 // Typing a chrome URL with a non-unique host should provide matching URLs. |
| 188 {kChrome + kSeparator1 + kHostF1.substr(0, 1), 2, {kURLF1, kURLF2}}, |
| 189 {kChrome + kSeparator2 + kHostF1.substr(0, 2), 2, {kURLF1, kURLF2}}, |
| 190 {kChrome + kSeparator3 + kHostF2.substr(0, 3), 2, {kURLF1, kURLF2}}, |
| 191 {kChrome + kSeparator3 + kHostF1.substr(0, 4), 1, {kURLF1}}, |
| 192 {kChrome + kSeparator3 + kHostF2.substr(0, 4), 1, {kURLF2}}, |
| 193 {kChrome + kSeparator3 + kHostF1, 1, {kURLF1}}, |
| 194 {kChrome + kSeparator2 + kHostF2, 1, {kURLF2}}, |
| 195 }; |
| 196 |
| 197 RunTest<GURL>(chrome_url_cases, arraysize(chrome_url_cases), |
| 198 &AutocompleteMatch::destination_url); |
| 199 } |
| 200 |
| 201 TEST_F(BuiltinProviderTest, ChromeSettingsSubpages) { |
| 202 // This makes assumptions about the chrome URLs listed by the BuiltinProvider. |
| 203 // Currently they are derived from ChromePaths() in browser_about_handler.cc. |
| 204 const string16 kSettings = ASCIIToUTF16(chrome::kChromeUISettingsURL); |
| 205 const string16 kDefaultPage1 = ASCIIToUTF16(chrome::kAdvancedOptionsSubPage); |
| 206 const string16 kDefaultPage2 = ASCIIToUTF16(chrome::kAutofillSubPage); |
| 207 const GURL kDefaultURL1 = GURL(kSettings + kDefaultPage1); |
| 208 const GURL kDefaultURL2 = GURL(kSettings + kDefaultPage2); |
| 209 const string16 kPage1 = ASCIIToUTF16(chrome::kPersonalOptionsSubPage); |
| 210 const string16 kPage2 = ASCIIToUTF16(chrome::kPasswordManagerSubPage); |
| 211 const GURL kURL1 = GURL(kSettings + kPage1); |
| 212 const GURL kURL2 = GURL(kSettings + kPage2); |
| 213 |
| 214 test_data<GURL> settings_subpage_cases[] = { |
| 215 // Typing the settings path should show settings and the first two subpages. |
| 216 {kSettings, 3, {GURL(kSettings), kDefaultURL1, kDefaultURL2}}, |
| 217 |
| 218 // Typing a subpage path should return the appropriate results. |
| 219 {kSettings + kPage1.substr(0, 1), 2, {kURL1, kURL2}}, |
| 220 {kSettings + kPage1.substr(0, 2), 1, {kURL1}}, |
| 221 {kSettings + kPage1.substr(0, kPage1.length() - 1), 1, {kURL1}}, |
| 222 {kSettings + kPage1, 1, {kURL1}}, |
| 223 {kSettings + kPage2, 1, {kURL2}}, |
| 224 }; |
| 225 |
| 226 RunTest<GURL>(settings_subpage_cases, arraysize(settings_subpage_cases), |
| 227 &AutocompleteMatch::destination_url); |
| 228 } |
OLD | NEW |