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