OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_model.h" | 10 #include "chrome/browser/search_engines/template_url_model.h" |
11 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 class KeywordProviderTest : public testing::Test { | 14 class KeywordProviderTest : public testing::Test { |
15 protected: | 15 protected: |
16 template<class ResultType> | 16 template<class ResultType> |
17 struct test_data { | 17 struct test_data { |
18 const string16 input; | 18 const std::wstring input; |
19 const size_t num_results; | 19 const size_t num_results; |
20 const ResultType output[3]; | 20 const ResultType output[3]; |
21 }; | 21 }; |
22 | 22 |
23 KeywordProviderTest() : kw_provider_(NULL) { } | 23 KeywordProviderTest() : kw_provider_(NULL) { } |
24 virtual ~KeywordProviderTest() { } | 24 virtual ~KeywordProviderTest() { } |
25 | 25 |
26 virtual void SetUp(); | 26 virtual void SetUp(); |
27 virtual void TearDown(); | 27 virtual void TearDown(); |
28 | 28 |
(...skipping 28 matching lines...) Expand all Loading... |
57 kw_provider_ = NULL; | 57 kw_provider_ = NULL; |
58 } | 58 } |
59 | 59 |
60 template<class ResultType> | 60 template<class ResultType> |
61 void KeywordProviderTest::RunTest( | 61 void KeywordProviderTest::RunTest( |
62 test_data<ResultType>* keyword_cases, | 62 test_data<ResultType>* keyword_cases, |
63 int num_cases, | 63 int num_cases, |
64 ResultType AutocompleteMatch::* member) { | 64 ResultType AutocompleteMatch::* member) { |
65 ACMatches matches; | 65 ACMatches matches; |
66 for (int i = 0; i < num_cases; ++i) { | 66 for (int i = 0; i < num_cases; ++i) { |
67 AutocompleteInput input(keyword_cases[i].input, string16(), true, | 67 AutocompleteInput input(keyword_cases[i].input, std::wstring(), true, |
68 false, true, false); | 68 false, true, false); |
69 kw_provider_->Start(input, false); | 69 kw_provider_->Start(input, false); |
70 EXPECT_TRUE(kw_provider_->done()); | 70 EXPECT_TRUE(kw_provider_->done()); |
71 matches = kw_provider_->matches(); | 71 matches = kw_provider_->matches(); |
72 EXPECT_EQ(keyword_cases[i].num_results, matches.size()) << | 72 EXPECT_EQ(keyword_cases[i].num_results, matches.size()) << |
73 ASCIIToUTF16("Input was: ") + keyword_cases[i].input; | 73 L"Input was: " + keyword_cases[i].input; |
74 if (matches.size() == keyword_cases[i].num_results) { | 74 if (matches.size() == keyword_cases[i].num_results) { |
75 for (size_t j = 0; j < keyword_cases[i].num_results; ++j) { | 75 for (size_t j = 0; j < keyword_cases[i].num_results; ++j) { |
76 EXPECT_EQ(keyword_cases[i].output[j], matches[j].*member); | 76 EXPECT_EQ(keyword_cases[i].output[j], matches[j].*member); |
77 } | 77 } |
78 } | 78 } |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 TEST_F(KeywordProviderTest, Edit) { | 82 TEST_F(KeywordProviderTest, Edit) { |
83 test_data<string16> edit_cases[] = { | 83 test_data<std::wstring> edit_cases[] = { |
84 // Searching for a nonexistent prefix should give nothing. | 84 // Searching for a nonexistent prefix should give nothing. |
85 {ASCIIToUTF16("Not Found"), 0, {}}, | 85 {L"Not Found", 0, {}}, |
86 {ASCIIToUTF16("aaaaaNot Found"), 0, {}}, | 86 {L"aaaaaNot Found", 0, {}}, |
87 | 87 |
88 // Check that tokenization only collapses whitespace between first tokens, | 88 // Check that tokenization only collapses whitespace between first tokens, |
89 // no-query-input cases have a space appended, and action is not escaped. | 89 // no-query-input cases have a space appended, and action is not escaped. |
90 {ASCIIToUTF16("z foo"), 1, {ASCIIToUTF16("z foo")}}, | 90 {L"z foo", 1, {L"z foo"}}, |
91 {ASCIIToUTF16("z"), 1, {ASCIIToUTF16("z ")}}, | 91 {L"z", 1, {L"z "}}, |
92 {ASCIIToUTF16("z \t"), 1, {ASCIIToUTF16("z ")}}, | 92 {L"z \t", 1, {L"z "}}, |
93 {ASCIIToUTF16("z a b c++"), 1, {ASCIIToUTF16("z a b c++")}}, | 93 {L"z a b c++", 1, {L"z a b c++"}}, |
94 | 94 |
95 // Matches should be limited to three, and sorted in quality order, not | 95 // Matches should be limited to three, and sorted in quality order, not |
96 // alphabetical. | 96 // alphabetical. |
97 {ASCIIToUTF16("aaa"), 2, {ASCIIToUTF16("aaaa "), | 97 {L"aaa", 2, {L"aaaa ", L"aaaaa "}}, |
98 ASCIIToUTF16("aaaaa ")}}, | 98 {L"a 1 2 3", 3, {L"aa 1 2 3", L"ab 1 2 3", L"aaaa 1 2 3"}}, |
99 {ASCIIToUTF16("a 1 2 3"), 3, {ASCIIToUTF16("aa 1 2 3"), | 99 {L"www.a", 3, {L"aa ", L"ab ", L"aaaa "}}, |
100 ASCIIToUTF16("ab 1 2 3"), | |
101 ASCIIToUTF16("aaaa 1 2 3")}}, | |
102 {ASCIIToUTF16("www.a"), 3, {ASCIIToUTF16("aa "), | |
103 ASCIIToUTF16("ab "), | |
104 ASCIIToUTF16("aaaa ")}}, | |
105 // Exact matches should prevent returning inexact matches. | 100 // Exact matches should prevent returning inexact matches. |
106 {ASCIIToUTF16("aaaa foo"), 1, {ASCIIToUTF16("aaaa foo")}}, | 101 {L"aaaa foo", 1, {L"aaaa foo"}}, |
107 {ASCIIToUTF16("www.aaaa foo"), 1, {ASCIIToUTF16("aaaa foo")}}, | 102 {L"www.aaaa foo", 1, {L"aaaa foo"}}, |
108 | 103 |
109 // Clean up keyword input properly. "http" and "https" are the only | 104 // Clean up keyword input properly. "http" and "https" are the only |
110 // allowed schemes. | 105 // allowed schemes. |
111 {ASCIIToUTF16("www"), 1, {ASCIIToUTF16("www ")}}, | 106 {L"www", 1, {L"www "}}, |
112 {ASCIIToUTF16("www."), 0, {}}, | 107 {L"www.", 0, {}}, |
113 {ASCIIToUTF16("www.w w"), 2, {ASCIIToUTF16("www w"), | 108 {L"www.w w", 2, {L"www w", L"weasel w"}}, |
114 ASCIIToUTF16("weasel w")}}, | 109 {L"http://www", 1, {L"www "}}, |
115 {ASCIIToUTF16("http://www"), 1, {ASCIIToUTF16("www ")}}, | 110 {L"http://www.", 0, {}}, |
116 {ASCIIToUTF16("http://www."), 0, {}}, | 111 {L"ftp: blah", 0, {}}, |
117 {ASCIIToUTF16("ftp: blah"), 0, {}}, | 112 {L"mailto:z", 0, {}}, |
118 {ASCIIToUTF16("mailto:z"), 0, {}}, | 113 {L"ftp://z", 0, {}}, |
119 {ASCIIToUTF16("ftp://z"), 0, {}}, | 114 {L"https://z", 1, {L"z "}}, |
120 {ASCIIToUTF16("https://z"), 1, {ASCIIToUTF16("z ")}}, | |
121 }; | 115 }; |
122 | 116 |
123 RunTest<string16>(edit_cases, arraysize(edit_cases), | 117 RunTest<std::wstring>(edit_cases, arraysize(edit_cases), |
124 &AutocompleteMatch::fill_into_edit); | 118 &AutocompleteMatch::fill_into_edit); |
125 } | 119 } |
126 | 120 |
127 TEST_F(KeywordProviderTest, URL) { | 121 TEST_F(KeywordProviderTest, URL) { |
128 test_data<GURL> url_cases[] = { | 122 test_data<GURL> url_cases[] = { |
129 // No query input -> empty destination URL. | 123 // No query input -> empty destination URL. |
130 {ASCIIToUTF16("z"), 1, {GURL()}}, | 124 {L"z", 1, {GURL()}}, |
131 {ASCIIToUTF16("z \t"), 1, {GURL()}}, | 125 {L"z \t", 1, {GURL()}}, |
132 | 126 |
133 // Check that tokenization only collapses whitespace between first tokens | 127 // Check that tokenization only collapses whitespace between first tokens |
134 // and query input, but not rest of URL, is escaped. | 128 // and query input, but not rest of URL, is escaped. |
135 {ASCIIToUTF16("z a b c++"), 1, {GURL("a+++b+++c%2B%2B=z")}}, | 129 {L"z a b c++", 1, {GURL("a+++b+++c%2B%2B=z")}}, |
136 {ASCIIToUTF16("www.www www"), 1, {GURL(" +%2B?=wwwfoo ")}}, | 130 {L"www.www www", 1, {GURL(" +%2B?=wwwfoo ")}}, |
137 | 131 |
138 // Substitution should work with various locations of the "%s". | 132 // Substitution should work with various locations of the "%s". |
139 {ASCIIToUTF16("aaa 1a2b"), 2, {GURL("http://aaaa/?aaaa=1&b=1a2b&c"), | 133 {L"aaa 1a2b", 2, {GURL("http://aaaa/?aaaa=1&b=1a2b&c"), |
140 GURL("1a2b")}}, | 134 GURL("1a2b")}}, |
141 {ASCIIToUTF16("a 1 2 3"), 3, {GURL("aa.com?foo=1+2+3"), | 135 {L"a 1 2 3", 3, {GURL("aa.com?foo=1+2+3"), GURL("bogus URL 1+2+3"), |
142 GURL("bogus URL 1+2+3"), | 136 GURL("http://aaaa/?aaaa=1&b=1+2+3&c")}}, |
143 GURL("http://aaaa/?aaaa=1&b=1+2+3&c")}}, | 137 {L"www.w w", 2, {GURL(" +%2B?=wfoo "), GURL("weaselwweasel")}}, |
144 {ASCIIToUTF16("www.w w"), 2, {GURL(" +%2B?=wfoo "), | |
145 GURL("weaselwweasel")}}, | |
146 }; | 138 }; |
147 | 139 |
148 RunTest<GURL>(url_cases, arraysize(url_cases), | 140 RunTest<GURL>(url_cases, arraysize(url_cases), |
149 &AutocompleteMatch::destination_url); | 141 &AutocompleteMatch::destination_url); |
150 } | 142 } |
151 | 143 |
152 TEST_F(KeywordProviderTest, Contents) { | 144 TEST_F(KeywordProviderTest, Contents) { |
153 test_data<string16> contents_cases[] = { | 145 test_data<std::wstring> contents_cases[] = { |
154 // No query input -> substitute "<enter query>" into contents. | 146 // No query input -> substitute "<enter query>" into contents. |
155 {ASCIIToUTF16("z"), 1, | 147 {L"z", 1, {L"Search z for <enter query>"}}, |
156 {ASCIIToUTF16("Search z for <enter query>")}}, | 148 {L"z \t", 1, {L"Search z for <enter query>"}}, |
157 {ASCIIToUTF16("z \t"), 1, | |
158 {ASCIIToUTF16("Search z for <enter query>")}}, | |
159 | 149 |
160 // Check that tokenization only collapses whitespace between first tokens | 150 // Check that tokenization only collapses whitespace between first tokens |
161 // and contents are not escaped or unescaped. | 151 // and contents are not escaped or unescaped. |
162 {ASCIIToUTF16("z a b c++"), 1, | 152 {L"z a b c++", 1, {L"Search z for a b c++"}}, |
163 {ASCIIToUTF16("Search z for a b c++")}}, | 153 {L"www.www www", 1, {L"Search www for www"}}, |
164 {ASCIIToUTF16("www.www www"), 1, {ASCIIToUTF16("Search www for www")}}, | |
165 | 154 |
166 // Substitution should work with various locations of the "%s". | 155 // Substitution should work with various locations of the "%s". |
167 {ASCIIToUTF16("aaa"), 2, | 156 {L"aaa", 2, {L"Search aaaa for <enter query>", |
168 {ASCIIToUTF16("Search aaaa for <enter query>"), | 157 L"Search aaaaa for <enter query>"}}, |
169 ASCIIToUTF16("Search aaaaa for <enter query>")}}, | 158 {L"a 1 2 3", 3, {L"Search aa for 1 2 3", L"Search ab for 1 2 3", |
170 {ASCIIToUTF16("a 1 2 3"), 3, {ASCIIToUTF16("Search aa for 1 2 3"), | 159 L"Search aaaa for 1 2 3"}}, |
171 ASCIIToUTF16("Search ab for 1 2 3"), | 160 {L"www.w w", 2, {L"Search www for w", L"Search weasel for w"}}, |
172 ASCIIToUTF16("Search aaaa for 1 2 3")}}, | |
173 {ASCIIToUTF16("www.w w"), 2, {ASCIIToUTF16("Search www for w"), | |
174 ASCIIToUTF16("Search weasel for w")}}, | |
175 }; | 161 }; |
176 | 162 |
177 RunTest<string16>(contents_cases, arraysize(contents_cases), | 163 RunTest<std::wstring>(contents_cases, arraysize(contents_cases), |
178 &AutocompleteMatch::contents); | 164 &AutocompleteMatch::contents); |
179 } | 165 } |
180 | 166 |
181 TEST_F(KeywordProviderTest, Description) { | 167 TEST_F(KeywordProviderTest, Description) { |
182 test_data<string16> description_cases[] = { | 168 test_data<std::wstring> description_cases[] = { |
183 // Whole keyword should be returned for both exact and inexact matches. | 169 // Whole keyword should be returned for both exact and inexact matches. |
184 {ASCIIToUTF16("z foo"), 1, {ASCIIToUTF16("(Keyword: z)")}}, | 170 {L"z foo", 1, {L"(Keyword: z)"}}, |
185 {ASCIIToUTF16("a foo"), 3, {ASCIIToUTF16("(Keyword: aa)"), | 171 {L"a foo", 3, {L"(Keyword: aa)", L"(Keyword: ab)", |
186 ASCIIToUTF16("(Keyword: ab)"), | 172 L"(Keyword: aaaa)"}}, |
187 ASCIIToUTF16("(Keyword: aaaa)")}}, | 173 {L"ftp://www.www w", 0, {}}, |
188 {ASCIIToUTF16("ftp://www.www w"), 0, {}}, | 174 {L"http://www.ab w", 1, {L"(Keyword: ab)"}}, |
189 {ASCIIToUTF16("http://www.ab w"), 1, {ASCIIToUTF16("(Keyword: ab)")}}, | |
190 | 175 |
191 // Keyword should be returned regardless of query input. | 176 // Keyword should be returned regardless of query input. |
192 {ASCIIToUTF16("z"), 1, {ASCIIToUTF16("(Keyword: z)")}}, | 177 {L"z", 1, {L"(Keyword: z)"}}, |
193 {ASCIIToUTF16("z \t"), 1, {ASCIIToUTF16("(Keyword: z)")}}, | 178 {L"z \t", 1, {L"(Keyword: z)"}}, |
194 {ASCIIToUTF16("z a b c++"), 1, {ASCIIToUTF16("(Keyword: z)")}}, | 179 {L"z a b c++", 1, {L"(Keyword: z)"}}, |
195 }; | 180 }; |
196 | 181 |
197 RunTest<string16>(description_cases, arraysize(description_cases), | 182 RunTest<std::wstring>(description_cases, arraysize(description_cases), |
198 &AutocompleteMatch::description); | 183 &AutocompleteMatch::description); |
199 } | 184 } |
200 | 185 |
201 TEST_F(KeywordProviderTest, AddKeyword) { | 186 TEST_F(KeywordProviderTest, AddKeyword) { |
202 TemplateURL* template_url = new TemplateURL(); | 187 TemplateURL* template_url = new TemplateURL(); |
203 string16 keyword(ASCIIToUTF16("foo")); | 188 string16 keyword(ASCIIToUTF16("foo")); |
204 std::string url("http://www.google.com/foo?q={searchTerms}"); | 189 std::string url("http://www.google.com/foo?q={searchTerms}"); |
205 template_url->SetURL(url, 0, 0); | 190 template_url->SetURL(url, 0, 0); |
206 template_url->set_keyword(keyword); | 191 template_url->set_keyword(keyword); |
207 template_url->set_short_name(ASCIIToUTF16("Test")); | 192 template_url->set_short_name(ASCIIToUTF16("Test")); |
208 model_->Add(template_url); | 193 model_->Add(template_url); |
209 ASSERT_TRUE(template_url == model_->GetTemplateURLForKeyword(keyword)); | 194 ASSERT_TRUE(template_url == model_->GetTemplateURLForKeyword(keyword)); |
210 } | 195 } |
211 | 196 |
212 TEST_F(KeywordProviderTest, RemoveKeyword) { | 197 TEST_F(KeywordProviderTest, RemoveKeyword) { |
213 string16 url(ASCIIToUTF16("http://aaaa/?aaaa=1&b={searchTerms}&c")); | 198 string16 url(ASCIIToUTF16("http://aaaa/?aaaa=1&b={searchTerms}&c")); |
214 model_->Remove(model_->GetTemplateURLForKeyword(ASCIIToUTF16("aaaa"))); | 199 model_->Remove(model_->GetTemplateURLForKeyword(ASCIIToUTF16("aaaa"))); |
215 ASSERT_TRUE(model_->GetTemplateURLForKeyword(ASCIIToUTF16("aaaa")) == NULL); | 200 ASSERT_TRUE(model_->GetTemplateURLForKeyword(ASCIIToUTF16("aaaa")) == NULL); |
216 } | 201 } |
OLD | NEW |