| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" |
| 5 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 7 #include "chrome/browser/autocomplete/autocomplete_match.h" | 8 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 8 #include "chrome/browser/autocomplete/keyword_provider.h" | 9 #include "chrome/browser/autocomplete/keyword_provider.h" |
| 9 #include "chrome/browser/search_engines/template_url.h" | 10 #include "chrome/browser/search_engines/template_url.h" |
| 10 #include "chrome/browser/search_engines/template_url_service.h" | 11 #include "chrome/browser/search_engines/template_url_service.h" |
| 12 #include "chrome/common/chrome_switches.h" |
| 11 #include "chrome/test/base/testing_browser_process.h" | 13 #include "chrome/test/base/testing_browser_process.h" |
| 12 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 16 |
| 15 class KeywordProviderTest : public testing::Test { | 17 class KeywordProviderTest : public testing::Test { |
| 16 protected: | 18 protected: |
| 17 template<class ResultType> | 19 template<class ResultType> |
| 18 struct test_data { | 20 struct test_data { |
| 19 const string16 input; | 21 const string16 input; |
| 20 const size_t num_results; | 22 const size_t num_results; |
| 21 const ResultType output[3]; | 23 const ResultType output[3]; |
| 22 }; | 24 }; |
| 23 | 25 |
| 24 KeywordProviderTest() : kw_provider_(NULL) { } | 26 KeywordProviderTest() : kw_provider_(NULL) { } |
| 25 virtual ~KeywordProviderTest() { } | 27 virtual ~KeywordProviderTest() { } |
| 26 | 28 |
| 27 virtual void SetUp(); | 29 virtual void SetUp(); |
| 28 virtual void TearDown(); | 30 virtual void TearDown(); |
| 29 | 31 |
| 30 template<class ResultType> | 32 template<class ResultType> |
| 31 void RunTest(test_data<ResultType>* keyword_cases, | 33 void RunTest(test_data<ResultType>* keyword_cases, |
| 32 int num_cases, | 34 int num_cases, |
| 33 ResultType AutocompleteMatch::* member); | 35 ResultType AutocompleteMatch::* member); |
| 34 | 36 |
| 35 protected: | 37 protected: |
| 38 static const TemplateURLService::Initializer kTestData[]; |
| 39 |
| 36 scoped_refptr<KeywordProvider> kw_provider_; | 40 scoped_refptr<KeywordProvider> kw_provider_; |
| 37 scoped_ptr<TemplateURLService> model_; | 41 scoped_ptr<TemplateURLService> model_; |
| 38 }; | 42 }; |
| 39 | 43 |
| 44 // static |
| 45 const TemplateURLService::Initializer KeywordProviderTest::kTestData[] = { |
| 46 { "aa", "aa.com?foo=%s", "aa" }, |
| 47 { "aaaa", "http://aaaa/?aaaa=1&b=%s&c", "aaaa" }, |
| 48 { "aaaaa", "%s", "aaaaa" }, |
| 49 { "ab", "bogus URL %s", "ab" }, |
| 50 { "weasel", "weasel%sweasel", "weasel" }, |
| 51 { "www", " +%2B?=%sfoo ", "www" }, |
| 52 { "z", "%s=z", "z" }, |
| 53 }; |
| 54 |
| 40 void KeywordProviderTest::SetUp() { | 55 void KeywordProviderTest::SetUp() { |
| 41 static const TemplateURLService::Initializer kTestKeywordData[] = { | 56 model_.reset(new TemplateURLService(kTestData, arraysize(kTestData))); |
| 42 { "aa", "aa.com?foo=%s", "aa" }, | |
| 43 { "aaaa", "http://aaaa/?aaaa=1&b=%s&c", "aaaa" }, | |
| 44 { "aaaaa", "%s", "aaaaa" }, | |
| 45 { "ab", "bogus URL %s", "ab" }, | |
| 46 { "weasel", "weasel%sweasel", "weasel" }, | |
| 47 { "www", " +%2B?=%sfoo ", "www" }, | |
| 48 { "z", "%s=z", "z" }, | |
| 49 }; | |
| 50 | |
| 51 model_.reset(new TemplateURLService(kTestKeywordData, | |
| 52 arraysize(kTestKeywordData))); | |
| 53 kw_provider_ = new KeywordProvider(NULL, model_.get()); | 57 kw_provider_ = new KeywordProvider(NULL, model_.get()); |
| 54 } | 58 } |
| 55 | 59 |
| 56 void KeywordProviderTest::TearDown() { | 60 void KeywordProviderTest::TearDown() { |
| 57 model_.reset(); | 61 model_.reset(); |
| 58 kw_provider_ = NULL; | 62 kw_provider_ = NULL; |
| 59 } | 63 } |
| 60 | 64 |
| 61 template<class ResultType> | 65 template<class ResultType> |
| 62 void KeywordProviderTest::RunTest( | 66 void KeywordProviderTest::RunTest( |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 {ASCIIToUTF16("https://z"), 1, {ASCIIToUTF16("z ")}}, | 131 {ASCIIToUTF16("https://z"), 1, {ASCIIToUTF16("z ")}}, |
| 128 }; | 132 }; |
| 129 | 133 |
| 130 RunTest<string16>(edit_cases, arraysize(edit_cases), | 134 RunTest<string16>(edit_cases, arraysize(edit_cases), |
| 131 &AutocompleteMatch::fill_into_edit); | 135 &AutocompleteMatch::fill_into_edit); |
| 132 } | 136 } |
| 133 | 137 |
| 134 TEST_F(KeywordProviderTest, URL) { | 138 TEST_F(KeywordProviderTest, URL) { |
| 135 test_data<GURL> url_cases[] = { | 139 test_data<GURL> url_cases[] = { |
| 136 // No query input -> empty destination URL. | 140 // No query input -> empty destination URL. |
| 137 {ASCIIToUTF16("z"), 1, {GURL()}}, | 141 {ASCIIToUTF16("z"), 1, {GURL()}}, |
| 138 {ASCIIToUTF16("z \t"), 1, {GURL()}}, | 142 {ASCIIToUTF16("z \t"), 1, {GURL()}}, |
| 139 | 143 |
| 140 // Check that tokenization only collapses whitespace between first tokens | 144 // Check that tokenization only collapses whitespace between first tokens |
| 141 // and query input, but not rest of URL, is escaped. | 145 // and query input, but not rest of URL, is escaped. |
| 142 {ASCIIToUTF16("w bar +baz"), 2, {GURL(" +%2B?=bar+%2Bbazfoo "), | 146 {ASCIIToUTF16("w bar +baz"), 2, {GURL(" +%2B?=bar+%2Bbazfoo "), |
| 143 GURL("bar+%2Bbaz=z")}}, | 147 GURL("bar+%2Bbaz=z")}}, |
| 144 | 148 |
| 145 // Substitution should work with various locations of the "%s". | 149 // Substitution should work with various locations of the "%s". |
| 146 {ASCIIToUTF16("aaa 1a2b"), 2, {GURL("http://aaaa/?aaaa=1&b=1a2b&c"), | 150 {ASCIIToUTF16("aaa 1a2b"), 2, {GURL("http://aaaa/?aaaa=1&b=1a2b&c"), |
| 147 GURL("1a2b")}}, | 151 GURL("1a2b")}}, |
| 148 {ASCIIToUTF16("a 1 2 3"), 3, {GURL("aa.com?foo=1+2+3"), | 152 {ASCIIToUTF16("a 1 2 3"), 3, {GURL("aa.com?foo=1+2+3"), |
| 149 GURL("bogus URL 1+2+3"), | 153 GURL("bogus URL 1+2+3"), |
| 150 GURL("http://aaaa/?aaaa=1&b=1+2+3&c")}}, | 154 GURL("http://aaaa/?aaaa=1&b=1+2+3&c")}}, |
| 151 {ASCIIToUTF16("www.w w"), 2, {GURL(" +%2B?=wfoo "), | 155 {ASCIIToUTF16("www.w w"), 2, {GURL(" +%2B?=wfoo "), |
| 152 GURL("weaselwweasel")}}, | 156 GURL("weaselwweasel")}}, |
| 153 }; | 157 }; |
| 154 | 158 |
| 155 RunTest<GURL>(url_cases, arraysize(url_cases), | 159 RunTest<GURL>(url_cases, arraysize(url_cases), |
| 156 &AutocompleteMatch::destination_url); | 160 &AutocompleteMatch::destination_url); |
| 157 } | 161 } |
| 158 | 162 |
| 159 TEST_F(KeywordProviderTest, Contents) { | 163 TEST_F(KeywordProviderTest, Contents) { |
| 160 test_data<string16> contents_cases[] = { | 164 test_data<string16> contents_cases[] = { |
| 161 // No query input -> substitute "<enter query>" into contents. | 165 // No query input -> substitute "<enter query>" into contents. |
| 162 {ASCIIToUTF16("z"), 1, | 166 {ASCIIToUTF16("z"), 1, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 182 ASCIIToUTF16("Search weasel for w")}}, | 186 ASCIIToUTF16("Search weasel for w")}}, |
| 183 // Also, check that tokenization only collapses whitespace between first | 187 // Also, check that tokenization only collapses whitespace between first |
| 184 // tokens and contents are not escaped or unescaped. | 188 // tokens and contents are not escaped or unescaped. |
| 185 {ASCIIToUTF16("a 1 2+ 3"), 3, | 189 {ASCIIToUTF16("a 1 2+ 3"), 3, |
| 186 {ASCIIToUTF16("Search aa for 1 2+ 3"), | 190 {ASCIIToUTF16("Search aa for 1 2+ 3"), |
| 187 ASCIIToUTF16("Search ab for 1 2+ 3"), | 191 ASCIIToUTF16("Search ab for 1 2+ 3"), |
| 188 ASCIIToUTF16("Search aaaa for 1 2+ 3")}}, | 192 ASCIIToUTF16("Search aaaa for 1 2+ 3")}}, |
| 189 }; | 193 }; |
| 190 | 194 |
| 191 RunTest<string16>(contents_cases, arraysize(contents_cases), | 195 RunTest<string16>(contents_cases, arraysize(contents_cases), |
| 192 &AutocompleteMatch::contents); | 196 &AutocompleteMatch::contents); |
| 193 } | |
| 194 | |
| 195 TEST_F(KeywordProviderTest, DISABLED_Description) { | |
| 196 test_data<string16> description_cases[] = { | |
| 197 // Whole keyword should be returned for both exact and inexact matches. | |
| 198 {ASCIIToUTF16("z foo"), 1, {ASCIIToUTF16("(Keyword: z)")}}, | |
| 199 {ASCIIToUTF16("a foo"), 3, {ASCIIToUTF16("(Keyword: aa)"), | |
| 200 ASCIIToUTF16("(Keyword: ab)"), | |
| 201 ASCIIToUTF16("(Keyword: aaaa)")}}, | |
| 202 {ASCIIToUTF16("ftp://www.www w"), 0, {}}, | |
| 203 {ASCIIToUTF16("http://www.ab w"), 1, {ASCIIToUTF16("(Keyword: ab)")}}, | |
| 204 | |
| 205 // Keyword should be returned regardless of query input. | |
| 206 {ASCIIToUTF16("z"), 1, {ASCIIToUTF16("(Keyword: z)")}}, | |
| 207 {ASCIIToUTF16("z \t"), 1, {ASCIIToUTF16("(Keyword: z)")}}, | |
| 208 {ASCIIToUTF16("z a b c++"), 1, {ASCIIToUTF16("(Keyword: z)")}}, | |
| 209 }; | |
| 210 | |
| 211 RunTest<string16>(description_cases, arraysize(description_cases), | |
| 212 &AutocompleteMatch::description); | |
| 213 } | 197 } |
| 214 | 198 |
| 215 TEST_F(KeywordProviderTest, AddKeyword) { | 199 TEST_F(KeywordProviderTest, AddKeyword) { |
| 216 TemplateURLData data; | 200 TemplateURLData data; |
| 217 data.short_name = ASCIIToUTF16("Test"); | 201 data.short_name = ASCIIToUTF16("Test"); |
| 218 string16 keyword(ASCIIToUTF16("foo")); | 202 string16 keyword(ASCIIToUTF16("foo")); |
| 219 data.SetKeyword(keyword); | 203 data.SetKeyword(keyword); |
| 220 data.SetURL("http://www.google.com/foo?q={searchTerms}"); | 204 data.SetURL("http://www.google.com/foo?q={searchTerms}"); |
| 221 TemplateURL* template_url = new TemplateURL(NULL, data); | 205 TemplateURL* template_url = new TemplateURL(NULL, data); |
| 222 model_->Add(template_url); | 206 model_->Add(template_url); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 KeywordProvider::GetSubstitutingTemplateURLForInput(model_.get(), | 271 KeywordProvider::GetSubstitutingTemplateURLForInput(model_.get(), |
| 288 &input); | 272 &input); |
| 289 if (cases[i].expected_url.empty()) | 273 if (cases[i].expected_url.empty()) |
| 290 EXPECT_FALSE(url); | 274 EXPECT_FALSE(url); |
| 291 else | 275 else |
| 292 EXPECT_EQ(cases[i].expected_url, url->url()); | 276 EXPECT_EQ(cases[i].expected_url, url->url()); |
| 293 EXPECT_EQ(ASCIIToUTF16(cases[i].updated_text), input.text()); | 277 EXPECT_EQ(ASCIIToUTF16(cases[i].updated_text), input.text()); |
| 294 EXPECT_EQ(cases[i].updated_cursor_position, input.cursor_position()); | 278 EXPECT_EQ(cases[i].updated_cursor_position, input.cursor_position()); |
| 295 } | 279 } |
| 296 } | 280 } |
| 281 |
| 282 // If extra query params are specified on the command line, they should be |
| 283 // reflected (only) in the default search provider's destination URL. |
| 284 TEST_F(KeywordProviderTest, ExtraQueryParams) { |
| 285 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 286 switches::kExtraSearchQueryParams, "a=b"); |
| 287 |
| 288 test_data<GURL> url_cases[] = { |
| 289 {ASCIIToUTF16("a 1 2 3"), 3, {GURL("aa.com?a=b&foo=1+2+3"), |
| 290 GURL("bogus URL 1+2+3"), |
| 291 GURL("http://aaaa/?aaaa=1&b=1+2+3&c")}}, |
| 292 }; |
| 293 |
| 294 RunTest<GURL>(url_cases, arraysize(url_cases), |
| 295 &AutocompleteMatch::destination_url); |
| 296 } |
| OLD | NEW |