| 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/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
| 7 #include "chrome/browser/autocomplete/autocomplete_match.h" | 7 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 8 #include "chrome/browser/autocomplete/keyword_provider.h" | 8 #include "chrome/browser/autocomplete/keyword_provider.h" |
| 9 #include "chrome/browser/search_engines/template_url.h" | 9 #include "chrome/browser/search_engines/template_url.h" |
| 10 #include "chrome/browser/search_engines/template_url_service.h" | 10 #include "chrome/browser/search_engines/template_url_service.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 } | 81 } |
| 82 | 82 |
| 83 TEST_F(KeywordProviderTest, Edit) { | 83 TEST_F(KeywordProviderTest, Edit) { |
| 84 test_data<string16> edit_cases[] = { | 84 test_data<string16> edit_cases[] = { |
| 85 // Searching for a nonexistent prefix should give nothing. | 85 // Searching for a nonexistent prefix should give nothing. |
| 86 {ASCIIToUTF16("Not Found"), 0, {}}, | 86 {ASCIIToUTF16("Not Found"), 0, {}}, |
| 87 {ASCIIToUTF16("aaaaaNot Found"), 0, {}}, | 87 {ASCIIToUTF16("aaaaaNot Found"), 0, {}}, |
| 88 | 88 |
| 89 // Check that tokenization only collapses whitespace between first tokens, | 89 // Check that tokenization only collapses whitespace between first tokens, |
| 90 // no-query-input cases have a space appended, and action is not escaped. | 90 // no-query-input cases have a space appended, and action is not escaped. |
| 91 {ASCIIToUTF16("z foo"), 1, {ASCIIToUTF16("z foo")}}, | |
| 92 {ASCIIToUTF16("z"), 1, {ASCIIToUTF16("z ")}}, | 91 {ASCIIToUTF16("z"), 1, {ASCIIToUTF16("z ")}}, |
| 93 {ASCIIToUTF16("z \t"), 1, {ASCIIToUTF16("z ")}}, | 92 {ASCIIToUTF16("z \t"), 1, {ASCIIToUTF16("z ")}}, |
| 94 {ASCIIToUTF16("z a b c++"), 1, {ASCIIToUTF16("z a b c++")}}, | 93 |
| 94 // Check that exact, substituting keywords with a verbatim search term |
| 95 // don't generate a result. (These are handled by SearchProvider.) |
| 96 {ASCIIToUTF16("z foo"), 0, {}}, |
| 97 {ASCIIToUTF16("z a b c++"), 0, {}}, |
| 95 | 98 |
| 96 // Matches should be limited to three, and sorted in quality order, not | 99 // Matches should be limited to three, and sorted in quality order, not |
| 97 // alphabetical. | 100 // alphabetical. |
| 98 {ASCIIToUTF16("aaa"), 2, {ASCIIToUTF16("aaaa "), | 101 {ASCIIToUTF16("aaa"), 2, {ASCIIToUTF16("aaaa "), |
| 99 ASCIIToUTF16("aaaaa ")}}, | 102 ASCIIToUTF16("aaaaa ")}}, |
| 100 {ASCIIToUTF16("a 1 2 3"), 3, {ASCIIToUTF16("aa 1 2 3"), | 103 {ASCIIToUTF16("a 1 2 3"), 3, {ASCIIToUTF16("aa 1 2 3"), |
| 101 ASCIIToUTF16("ab 1 2 3"), | 104 ASCIIToUTF16("ab 1 2 3"), |
| 102 ASCIIToUTF16("aaaa 1 2 3")}}, | 105 ASCIIToUTF16("aaaa 1 2 3")}}, |
| 103 {ASCIIToUTF16("www.a"), 3, {ASCIIToUTF16("aa "), | 106 {ASCIIToUTF16("www.a"), 3, {ASCIIToUTF16("aa "), |
| 104 ASCIIToUTF16("ab "), | 107 ASCIIToUTF16("ab "), |
| 105 ASCIIToUTF16("aaaa ")}}, | 108 ASCIIToUTF16("aaaa ")}}, |
| 106 // Exact matches should prevent returning inexact matches. | 109 // Exact matches should prevent returning inexact matches. Also, the |
| 107 {ASCIIToUTF16("aaaa foo"), 1, {ASCIIToUTF16("aaaa foo")}}, | 110 // verbatim query for this keyword match should not be returned. (It's |
| 108 {ASCIIToUTF16("www.aaaa foo"), 1, {ASCIIToUTF16("aaaa foo")}}, | 111 // returned by SearchProvider.) |
| 112 {ASCIIToUTF16("aaaa foo"), 0, {}}, |
| 113 {ASCIIToUTF16("www.aaaa foo"), 0, {}}, |
| 109 | 114 |
| 110 // Clean up keyword input properly. "http" and "https" are the only | 115 // Clean up keyword input properly. "http" and "https" are the only |
| 111 // allowed schemes. | 116 // allowed schemes. |
| 112 {ASCIIToUTF16("www"), 1, {ASCIIToUTF16("www ")}}, | 117 {ASCIIToUTF16("www"), 1, {ASCIIToUTF16("www ")}}, |
| 113 {ASCIIToUTF16("www."), 0, {}}, | 118 {ASCIIToUTF16("www."), 0, {}}, |
| 114 {ASCIIToUTF16("www.w w"), 2, {ASCIIToUTF16("www w"), | 119 {ASCIIToUTF16("www.w w"), 2, {ASCIIToUTF16("www w"), |
| 115 ASCIIToUTF16("weasel w")}}, | 120 ASCIIToUTF16("weasel w")}}, |
| 116 {ASCIIToUTF16("http://www"), 1, {ASCIIToUTF16("www ")}}, | 121 {ASCIIToUTF16("http://www"), 1, {ASCIIToUTF16("www ")}}, |
| 117 {ASCIIToUTF16("http://www."), 0, {}}, | 122 {ASCIIToUTF16("http://www."), 0, {}}, |
| 118 {ASCIIToUTF16("ftp: blah"), 0, {}}, | 123 {ASCIIToUTF16("ftp: blah"), 0, {}}, |
| 119 {ASCIIToUTF16("mailto:z"), 0, {}}, | 124 {ASCIIToUTF16("mailto:z"), 0, {}}, |
| 120 {ASCIIToUTF16("ftp://z"), 0, {}}, | 125 {ASCIIToUTF16("ftp://z"), 0, {}}, |
| 121 {ASCIIToUTF16("https://z"), 1, {ASCIIToUTF16("z ")}}, | 126 {ASCIIToUTF16("https://z"), 1, {ASCIIToUTF16("z ")}}, |
| 122 }; | 127 }; |
| 123 | 128 |
| 124 RunTest<string16>(edit_cases, arraysize(edit_cases), | 129 RunTest<string16>(edit_cases, arraysize(edit_cases), |
| 125 &AutocompleteMatch::fill_into_edit); | 130 &AutocompleteMatch::fill_into_edit); |
| 126 } | 131 } |
| 127 | 132 |
| 128 TEST_F(KeywordProviderTest, URL) { | 133 TEST_F(KeywordProviderTest, URL) { |
| 129 test_data<GURL> url_cases[] = { | 134 test_data<GURL> url_cases[] = { |
| 130 // No query input -> empty destination URL. | 135 // No query input -> empty destination URL. |
| 131 {ASCIIToUTF16("z"), 1, {GURL()}}, | 136 {ASCIIToUTF16("z"), 1, {GURL()}}, |
| 132 {ASCIIToUTF16("z \t"), 1, {GURL()}}, | 137 {ASCIIToUTF16("z \t"), 1, {GURL()}}, |
| 133 | 138 |
| 134 // Check that tokenization only collapses whitespace between first tokens | 139 // Check that tokenization only collapses whitespace between first tokens |
| 135 // and query input, but not rest of URL, is escaped. | 140 // and query input, but not rest of URL, is escaped. |
| 136 {ASCIIToUTF16("z a b c++"), 1, {GURL("a+++b+++c%2B%2B=z")}}, | 141 {ASCIIToUTF16("w bar +baz"), 2, {GURL(" +%2B?=bar+%2Bbazfoo "), |
| 137 {ASCIIToUTF16("www.www www"), 1, {GURL(" +%2B?=wwwfoo ")}}, | 142 GURL("bar+%2Bbaz=z")}}, |
| 138 | 143 |
| 139 // Substitution should work with various locations of the "%s". | 144 // Substitution should work with various locations of the "%s". |
| 140 {ASCIIToUTF16("aaa 1a2b"), 2, {GURL("http://aaaa/?aaaa=1&b=1a2b&c"), | 145 {ASCIIToUTF16("aaa 1a2b"), 2, {GURL("http://aaaa/?aaaa=1&b=1a2b&c"), |
| 141 GURL("1a2b")}}, | 146 GURL("1a2b")}}, |
| 142 {ASCIIToUTF16("a 1 2 3"), 3, {GURL("aa.com?foo=1+2+3"), | 147 {ASCIIToUTF16("a 1 2 3"), 3, {GURL("aa.com?foo=1+2+3"), |
| 143 GURL("bogus URL 1+2+3"), | 148 GURL("bogus URL 1+2+3"), |
| 144 GURL("http://aaaa/?aaaa=1&b=1+2+3&c")}}, | 149 GURL("http://aaaa/?aaaa=1&b=1+2+3&c")}}, |
| 145 {ASCIIToUTF16("www.w w"), 2, {GURL(" +%2B?=wfoo "), | 150 {ASCIIToUTF16("www.w w"), 2, {GURL(" +%2B?=wfoo "), |
| 146 GURL("weaselwweasel")}}, | 151 GURL("weaselwweasel")}}, |
| 147 }; | 152 }; |
| 148 | 153 |
| 149 RunTest<GURL>(url_cases, arraysize(url_cases), | 154 RunTest<GURL>(url_cases, arraysize(url_cases), |
| 150 &AutocompleteMatch::destination_url); | 155 &AutocompleteMatch::destination_url); |
| 151 } | 156 } |
| 152 | 157 |
| 153 TEST_F(KeywordProviderTest, Contents) { | 158 TEST_F(KeywordProviderTest, Contents) { |
| 154 test_data<string16> contents_cases[] = { | 159 test_data<string16> contents_cases[] = { |
| 155 // No query input -> substitute "<enter query>" into contents. | 160 // No query input -> substitute "<enter query>" into contents. |
| 156 {ASCIIToUTF16("z"), 1, | 161 {ASCIIToUTF16("z"), 1, |
| 157 {ASCIIToUTF16("Search z for <enter query>")}}, | 162 {ASCIIToUTF16("Search z for <enter query>")}}, |
| 158 {ASCIIToUTF16("z \t"), 1, | 163 {ASCIIToUTF16("z \t"), 1, |
| 159 {ASCIIToUTF16("Search z for <enter query>")}}, | 164 {ASCIIToUTF16("Search z for <enter query>")}}, |
| 160 | 165 |
| 161 // Check that tokenization only collapses whitespace between first tokens | 166 // Exact keyword matches with remaining text should return nothing. |
| 162 // and contents are not escaped or unescaped. | 167 {ASCIIToUTF16("www.www www"), 0, {}}, |
| 163 {ASCIIToUTF16("z a b c++"), 1, | 168 {ASCIIToUTF16("z a b c++"), 0, {}}, |
| 164 {ASCIIToUTF16("Search z for a b c++")}}, | 169 |
| 165 {ASCIIToUTF16("www.www www"), 1, {ASCIIToUTF16("Search www for www")}}, | 170 // Exact keyword matches with remaining text when the keyword is an |
| 171 // extension keyword should return something. This is tested in |
| 172 // chrome/browser/extensions/api/omnibox/omnibox_apitest.cc's |
| 173 // in OmniboxApiTest's Basic test. |
| 166 | 174 |
| 167 // Substitution should work with various locations of the "%s". | 175 // Substitution should work with various locations of the "%s". |
| 168 {ASCIIToUTF16("aaa"), 2, | 176 {ASCIIToUTF16("aaa"), 2, |
| 169 {ASCIIToUTF16("Search aaaa for <enter query>"), | 177 {ASCIIToUTF16("Search aaaa for <enter query>"), |
| 170 ASCIIToUTF16("Search aaaaa for <enter query>")}}, | 178 ASCIIToUTF16("Search aaaaa for <enter query>")}}, |
| 171 {ASCIIToUTF16("a 1 2 3"), 3, {ASCIIToUTF16("Search aa for 1 2 3"), | 179 {ASCIIToUTF16("www.w w"), 2, |
| 172 ASCIIToUTF16("Search ab for 1 2 3"), | 180 {ASCIIToUTF16("Search www for w"), |
| 173 ASCIIToUTF16("Search aaaa for 1 2 3")}}, | 181 ASCIIToUTF16("Search weasel for w")}}, |
| 174 {ASCIIToUTF16("www.w w"), 2, {ASCIIToUTF16("Search www for w"), | 182 // Also, check that tokenization only collapses whitespace between first |
| 175 ASCIIToUTF16("Search weasel for w")}}, | 183 // tokens and contents are not escaped or unescaped. |
| 184 {ASCIIToUTF16("a 1 2+ 3"), 3, |
| 185 {ASCIIToUTF16("Search aa for 1 2+ 3"), |
| 186 ASCIIToUTF16("Search ab for 1 2+ 3"), |
| 187 ASCIIToUTF16("Search aaaa for 1 2+ 3")}}, |
| 176 }; | 188 }; |
| 177 | 189 |
| 178 RunTest<string16>(contents_cases, arraysize(contents_cases), | 190 RunTest<string16>(contents_cases, arraysize(contents_cases), |
| 179 &AutocompleteMatch::contents); | 191 &AutocompleteMatch::contents); |
| 180 } | 192 } |
| 181 | 193 |
| 182 TEST_F(KeywordProviderTest, DISABLED_Description) { | 194 TEST_F(KeywordProviderTest, DISABLED_Description) { |
| 183 test_data<string16> description_cases[] = { | 195 test_data<string16> description_cases[] = { |
| 184 // Whole keyword should be returned for both exact and inexact matches. | 196 // Whole keyword should be returned for both exact and inexact matches. |
| 185 {ASCIIToUTF16("z foo"), 1, {ASCIIToUTF16("(Keyword: z)")}}, | 197 {ASCIIToUTF16("z foo"), 1, {ASCIIToUTF16("(Keyword: z)")}}, |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 KeywordProvider::GetSubstitutingTemplateURLForInput(model_.get(), | 286 KeywordProvider::GetSubstitutingTemplateURLForInput(model_.get(), |
| 275 &input); | 287 &input); |
| 276 if (cases[i].expected_url.empty()) | 288 if (cases[i].expected_url.empty()) |
| 277 EXPECT_FALSE(url); | 289 EXPECT_FALSE(url); |
| 278 else | 290 else |
| 279 EXPECT_EQ(cases[i].expected_url, url->url()); | 291 EXPECT_EQ(cases[i].expected_url, url->url()); |
| 280 EXPECT_EQ(ASCIIToUTF16(cases[i].updated_text), input.text()); | 292 EXPECT_EQ(ASCIIToUTF16(cases[i].updated_text), input.text()); |
| 281 EXPECT_EQ(cases[i].updated_cursor_position, input.cursor_position()); | 293 EXPECT_EQ(cases[i].updated_cursor_position, input.cursor_position()); |
| 282 } | 294 } |
| 283 } | 295 } |
| OLD | NEW |