builtin_provider_unittest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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/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 "googleurl/src/gurl.h" 15#include "testing/gtest/include/gtest/gtest.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 GURL kURLM1 = GURL(kChrome + kSeparator3 + kHostM1); 150 const GURL kURLM2 = GURL(kChrome + kSeparator3 + kHostM2); 151 152 test_data<GURL> chrome_url_cases[] = { 153 // Typing an about URL with an unknown host should give nothing. 154 {kAbout + kSeparator1 + ASCIIToUTF16("host"), 0, {}}, 155 {kAbout + kSeparator2 + ASCIIToUTF16("host"), 0, {}}, 156 {kAbout + kSeparator3 + ASCIIToUTF16("host"), 0, {}}, 157 158 // Typing a chrome URL with an unknown host should give nothing. 159 {kChrome + kSeparator1 + ASCIIToUTF16("host"), 0, {}}, 160 {kChrome + kSeparator2 + ASCIIToUTF16("host"), 0, {}}, 161 {kChrome + kSeparator3 + ASCIIToUTF16("host"), 0, {}}, 162 163 // Typing an about URL should provide matching URLs. 164 {kAbout + kSeparator1 + kHostM1.substr(0, 1), 2, {kURLM1, kURLM2}}, 165 {kAbout + kSeparator2 + kHostM1.substr(0, 2), 2, {kURLM1, kURLM2}}, 166 {kAbout + kSeparator3 + kHostM1.substr(0, 3), 1, {kURLM1}}, 167 {kAbout + kSeparator3 + kHostM2.substr(0, 3), 1, {kURLM2}}, 168 {kAbout + kSeparator3 + kHostM1, 1, {kURLM1}}, 169 {kAbout + kSeparator2 + kHostM2, 1, {kURLM2}}, 170 171 // Typing a chrome URL should provide matching URLs. 172 {kChrome + kSeparator1 + kHostM1.substr(0, 1), 2, {kURLM1, kURLM2}}, 173 {kChrome + kSeparator2 + kHostM1.substr(0, 2), 2, {kURLM1, kURLM2}}, 174 {kChrome + kSeparator3 + kHostM1.substr(0, 3), 1, {kURLM1}}, 175 {kChrome + kSeparator3 + kHostM2.substr(0, 3), 1, {kURLM2}}, 176 {kChrome + kSeparator3 + kHostM1, 1, {kURLM1}}, 177 {kChrome + kSeparator2 + kHostM2, 1, {kURLM2}}, 178 }; 179 180 RunTest<GURL>(chrome_url_cases, arraysize(chrome_url_cases), 181 &AutocompleteMatch::destination_url); 182} 183 184#if !defined(OS_ANDROID) 185// Disabled on Android where we use native UI instead of chrome://settings. 186TEST_F(BuiltinProviderTest, ChromeSettingsSubpages) { 187 // This makes assumptions about the chrome URLs listed by the BuiltinProvider. 188 // Currently they are derived from ChromePaths() in browser_about_handler.cc. 189 const string16 kSettings = ASCIIToUTF16(chrome::kChromeUISettingsURL); 190 const string16 kDefaultPage1 = ASCIIToUTF16(chrome::kAutofillSubPage); 191 const string16 kDefaultPage2 = ASCIIToUTF16(chrome::kClearBrowserDataSubPage); 192 const GURL kDefaultURL1 = GURL(kSettings + kDefaultPage1); 193 const GURL kDefaultURL2 = GURL(kSettings + kDefaultPage2); 194 const string16 kPage1 = ASCIIToUTF16(chrome::kSearchEnginesSubPage); 195 const string16 kPage2 = ASCIIToUTF16(chrome::kSyncSetupSubPage); 196 const GURL kURL1 = GURL(kSettings + kPage1); 197 const GURL kURL2 = GURL(kSettings + kPage2); 198 199 test_data<GURL> settings_subpage_cases[] = { 200 // Typing the settings path should show settings and the first two subpages. 201 {kSettings, 3, {GURL(kSettings), kDefaultURL1, kDefaultURL2}}, 202 203 // Typing a subpage path should return the appropriate results. 204 {kSettings + kPage1.substr(0, 1), 2, {kURL1, kURL2}}, 205 {kSettings + kPage1.substr(0, 2), 1, {kURL1}}, 206 {kSettings + kPage1.substr(0, kPage1.length() - 1), 1, {kURL1}}, 207 {kSettings + kPage1, 1, {kURL1}}, 208 {kSettings + kPage2, 1, {kURL2}}, 209 }; 210 211 RunTest<GURL>(settings_subpage_cases, arraysize(settings_subpage_cases), 212 &AutocompleteMatch::destination_url); 213} 214#endif 215