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 "chrome/browser/autocomplete/autocomplete_input.h" | 5 #include "chrome/browser/autocomplete/autocomplete_input.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "url/url_parse.h" | 12 #include "url/url_parse.h" |
13 | 13 |
14 using base::ASCIIToUTF16; | 14 using base::ASCIIToUTF16; |
| 15 using metrics::OmniboxEventProto; |
15 | 16 |
16 TEST(AutocompleteInputTest, InputType) { | 17 TEST(AutocompleteInputTest, InputType) { |
17 struct test_data { | 18 struct test_data { |
18 const base::string16 input; | 19 const base::string16 input; |
19 const AutocompleteInput::Type type; | 20 const metrics::OmniboxInputType::Type type; |
20 } input_cases[] = { | 21 } input_cases[] = { |
21 { base::string16(), metrics::OmniboxInputType::INVALID }, | 22 { base::string16(), metrics::OmniboxInputType::INVALID }, |
22 { ASCIIToUTF16("?"), metrics::OmniboxInputType::FORCED_QUERY }, | 23 { ASCIIToUTF16("?"), metrics::OmniboxInputType::FORCED_QUERY }, |
23 { ASCIIToUTF16("?foo"), metrics::OmniboxInputType::FORCED_QUERY }, | 24 { ASCIIToUTF16("?foo"), metrics::OmniboxInputType::FORCED_QUERY }, |
24 { ASCIIToUTF16("?foo bar"), metrics::OmniboxInputType::FORCED_QUERY }, | 25 { ASCIIToUTF16("?foo bar"), metrics::OmniboxInputType::FORCED_QUERY }, |
25 { ASCIIToUTF16("?http://foo.com/bar"), metrics::OmniboxInputType::FORCED_QUE
RY }, | 26 { ASCIIToUTF16("?http://foo.com/bar"), metrics::OmniboxInputType::FORCED_QUE
RY }, |
26 { ASCIIToUTF16("foo"), metrics::OmniboxInputType::UNKNOWN }, | 27 { ASCIIToUTF16("foo"), metrics::OmniboxInputType::UNKNOWN }, |
27 { ASCIIToUTF16("localhost"), metrics::OmniboxInputType::URL }, | 28 { ASCIIToUTF16("localhost"), metrics::OmniboxInputType::URL }, |
28 { ASCIIToUTF16("foo.c"), metrics::OmniboxInputType::UNKNOWN }, | 29 { ASCIIToUTF16("foo.c"), metrics::OmniboxInputType::UNKNOWN }, |
29 { ASCIIToUTF16("foo.com"), metrics::OmniboxInputType::URL }, | 30 { ASCIIToUTF16("foo.com"), metrics::OmniboxInputType::URL }, |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 { ASCIIToUTF16("chrome-devtools:"), metrics::OmniboxInputType::QUERY }, | 133 { ASCIIToUTF16("chrome-devtools:"), metrics::OmniboxInputType::QUERY }, |
133 { ASCIIToUTF16("about://f;"), metrics::OmniboxInputType::QUERY }, | 134 { ASCIIToUTF16("about://f;"), metrics::OmniboxInputType::QUERY }, |
134 { ASCIIToUTF16("://w"), metrics::OmniboxInputType::QUERY }, | 135 { ASCIIToUTF16("://w"), metrics::OmniboxInputType::QUERY }, |
135 { ASCIIToUTF16(":w"), metrics::OmniboxInputType::QUERY }, | 136 { ASCIIToUTF16(":w"), metrics::OmniboxInputType::QUERY }, |
136 }; | 137 }; |
137 | 138 |
138 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 139 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
139 SCOPED_TRACE(input_cases[i].input); | 140 SCOPED_TRACE(input_cases[i].input); |
140 AutocompleteInput input(input_cases[i].input, base::string16::npos, | 141 AutocompleteInput input(input_cases[i].input, base::string16::npos, |
141 base::string16(), GURL(), | 142 base::string16(), GURL(), |
142 AutocompleteInput::INVALID_SPEC, true, false, true, | 143 OmniboxEventProto::INVALID_SPEC, true, false, true, |
143 true); | 144 true); |
144 EXPECT_EQ(input_cases[i].type, input.type()); | 145 EXPECT_EQ(input_cases[i].type, input.type()); |
145 } | 146 } |
146 } | 147 } |
147 | 148 |
148 TEST(AutocompleteInputTest, InputTypeWithDesiredTLD) { | 149 TEST(AutocompleteInputTest, InputTypeWithDesiredTLD) { |
149 struct test_data { | 150 struct test_data { |
150 const base::string16 input; | 151 const base::string16 input; |
151 const AutocompleteInput::Type type; | 152 const metrics::OmniboxInputType::Type type; |
152 const std::string spec; // Unused if not a URL. | 153 const std::string spec; // Unused if not a URL. |
153 } input_cases[] = { | 154 } input_cases[] = { |
154 { ASCIIToUTF16("401k"), metrics::OmniboxInputType::URL, | 155 { ASCIIToUTF16("401k"), metrics::OmniboxInputType::URL, |
155 std::string("http://www.401k.com/") }, | 156 std::string("http://www.401k.com/") }, |
156 { ASCIIToUTF16("999999999999999"), metrics::OmniboxInputType::URL, | 157 { ASCIIToUTF16("999999999999999"), metrics::OmniboxInputType::URL, |
157 std::string("http://www.999999999999999.com/") }, | 158 std::string("http://www.999999999999999.com/") }, |
158 { ASCIIToUTF16("x@y"), metrics::OmniboxInputType::URL, | 159 { ASCIIToUTF16("x@y"), metrics::OmniboxInputType::URL, |
159 std::string("http://x@www.y.com/") }, | 160 std::string("http://x@www.y.com/") }, |
160 { ASCIIToUTF16("y/z z"), metrics::OmniboxInputType::URL, | 161 { ASCIIToUTF16("y/z z"), metrics::OmniboxInputType::URL, |
161 std::string("http://www.y.com/z%20z") }, | 162 std::string("http://www.y.com/z%20z") }, |
162 { ASCIIToUTF16("abc.com"), metrics::OmniboxInputType::URL, | 163 { ASCIIToUTF16("abc.com"), metrics::OmniboxInputType::URL, |
163 std::string("http://abc.com/") }, | 164 std::string("http://abc.com/") }, |
164 { ASCIIToUTF16("foo bar"), metrics::OmniboxInputType::QUERY, | 165 { ASCIIToUTF16("foo bar"), metrics::OmniboxInputType::QUERY, |
165 std::string() }, | 166 std::string() }, |
166 }; | 167 }; |
167 | 168 |
168 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 169 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
169 SCOPED_TRACE(input_cases[i].input); | 170 SCOPED_TRACE(input_cases[i].input); |
170 AutocompleteInput input(input_cases[i].input, base::string16::npos, | 171 AutocompleteInput input(input_cases[i].input, base::string16::npos, |
171 ASCIIToUTF16("com"), GURL(), | 172 ASCIIToUTF16("com"), GURL(), |
172 AutocompleteInput::INVALID_SPEC, true, false, true, | 173 OmniboxEventProto::INVALID_SPEC, true, false, true, |
173 true); | 174 true); |
174 EXPECT_EQ(input_cases[i].type, input.type()); | 175 EXPECT_EQ(input_cases[i].type, input.type()); |
175 if (input_cases[i].type == metrics::OmniboxInputType::URL) | 176 if (input_cases[i].type == metrics::OmniboxInputType::URL) |
176 EXPECT_EQ(input_cases[i].spec, input.canonicalized_url().spec()); | 177 EXPECT_EQ(input_cases[i].spec, input.canonicalized_url().spec()); |
177 } | 178 } |
178 } | 179 } |
179 | 180 |
180 // This tests for a regression where certain input in the omnibox caused us to | 181 // This tests for a regression where certain input in the omnibox caused us to |
181 // crash. As long as the test completes without crashing, we're fine. | 182 // crash. As long as the test completes without crashing, we're fine. |
182 TEST(AutocompleteInputTest, InputCrash) { | 183 TEST(AutocompleteInputTest, InputCrash) { |
183 AutocompleteInput input(base::WideToUTF16(L"\uff65@s"), base::string16::npos, | 184 AutocompleteInput input(base::WideToUTF16(L"\uff65@s"), base::string16::npos, |
184 base::string16(), GURL(), | 185 base::string16(), GURL(), |
185 AutocompleteInput::INVALID_SPEC, true, false, | 186 OmniboxEventProto::INVALID_SPEC, true, false, |
186 true, true); | 187 true, true); |
187 } | 188 } |
188 | 189 |
189 TEST(AutocompleteInputTest, ParseForEmphasizeComponent) { | 190 TEST(AutocompleteInputTest, ParseForEmphasizeComponent) { |
190 using url::Component; | 191 using url::Component; |
191 Component kInvalidComponent(0, -1); | 192 Component kInvalidComponent(0, -1); |
192 struct test_data { | 193 struct test_data { |
193 const base::string16 input; | 194 const base::string16 input; |
194 const Component scheme; | 195 const Component scheme; |
195 const Component host; | 196 const Component host; |
(...skipping 23 matching lines...) Expand all Loading... |
219 }; | 220 }; |
220 | 221 |
221 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 222 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
222 SCOPED_TRACE(input_cases[i].input); | 223 SCOPED_TRACE(input_cases[i].input); |
223 Component scheme, host; | 224 Component scheme, host; |
224 AutocompleteInput::ParseForEmphasizeComponents(input_cases[i].input, | 225 AutocompleteInput::ParseForEmphasizeComponents(input_cases[i].input, |
225 &scheme, | 226 &scheme, |
226 &host); | 227 &host); |
227 AutocompleteInput input(input_cases[i].input, base::string16::npos, | 228 AutocompleteInput input(input_cases[i].input, base::string16::npos, |
228 base::string16(), GURL(), | 229 base::string16(), GURL(), |
229 AutocompleteInput::INVALID_SPEC, true, | 230 OmniboxEventProto::INVALID_SPEC, true, |
230 false, true, true); | 231 false, true, true); |
231 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin); | 232 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin); |
232 EXPECT_EQ(input_cases[i].scheme.len, scheme.len); | 233 EXPECT_EQ(input_cases[i].scheme.len, scheme.len); |
233 EXPECT_EQ(input_cases[i].host.begin, host.begin); | 234 EXPECT_EQ(input_cases[i].host.begin, host.begin); |
234 EXPECT_EQ(input_cases[i].host.len, host.len); | 235 EXPECT_EQ(input_cases[i].host.len, host.len); |
235 } | 236 } |
236 } | 237 } |
237 | 238 |
238 TEST(AutocompleteInputTest, InputTypeWithCursorPosition) { | 239 TEST(AutocompleteInputTest, InputTypeWithCursorPosition) { |
239 struct test_data { | 240 struct test_data { |
(...skipping 18 matching lines...) Expand all Loading... |
258 { ASCIIToUTF16(" ?foo bar"), 4, ASCIIToUTF16("foo bar"), 1 }, | 259 { ASCIIToUTF16(" ?foo bar"), 4, ASCIIToUTF16("foo bar"), 1 }, |
259 { ASCIIToUTF16("? foo bar"), 4, ASCIIToUTF16("foo bar"), 1 }, | 260 { ASCIIToUTF16("? foo bar"), 4, ASCIIToUTF16("foo bar"), 1 }, |
260 { ASCIIToUTF16(" ? foo bar"), 6, ASCIIToUTF16("foo bar"), 1 }, | 261 { ASCIIToUTF16(" ? foo bar"), 6, ASCIIToUTF16("foo bar"), 1 }, |
261 }; | 262 }; |
262 | 263 |
263 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 264 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
264 SCOPED_TRACE(input_cases[i].input); | 265 SCOPED_TRACE(input_cases[i].input); |
265 AutocompleteInput input(input_cases[i].input, | 266 AutocompleteInput input(input_cases[i].input, |
266 input_cases[i].cursor_position, | 267 input_cases[i].cursor_position, |
267 base::string16(), GURL(), | 268 base::string16(), GURL(), |
268 AutocompleteInput::INVALID_SPEC, | 269 OmniboxEventProto::INVALID_SPEC, |
269 true, false, true, true); | 270 true, false, true, true); |
270 EXPECT_EQ(input_cases[i].normalized_input, input.text()); | 271 EXPECT_EQ(input_cases[i].normalized_input, input.text()); |
271 EXPECT_EQ(input_cases[i].normalized_cursor_position, | 272 EXPECT_EQ(input_cases[i].normalized_cursor_position, |
272 input.cursor_position()); | 273 input.cursor_position()); |
273 } | 274 } |
274 } | 275 } |
OLD | NEW |