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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 12 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" |
13 #include "chrome/browser/extensions/extension_browsertest.h" | 13 #include "chrome/browser/extensions/extension_browsertest.h" |
14 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
15 #include "chrome/browser/extensions/unpacked_installer.h" | 15 #include "chrome/browser/extensions/unpacked_installer.h" |
16 #include "chrome/browser/history/history_service_factory.h" | 16 #include "chrome/browser/history/history_service_factory.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/search_engines/template_url_service_factory.h" | 18 #include "chrome/browser/search_engines/template_url_service_factory.h" |
19 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
20 #include "chrome/browser/ui/browser_commands.h" | 20 #include "chrome/browser/ui/browser_commands.h" |
21 #include "chrome/browser/ui/browser_tabstrip.h" | 21 #include "chrome/browser/ui/browser_tabstrip.h" |
22 #include "chrome/browser/ui/browser_window.h" | 22 #include "chrome/browser/ui/browser_window.h" |
23 #include "chrome/browser/ui/location_bar/location_bar.h" | 23 #include "chrome/browser/ui/location_bar/location_bar.h" |
24 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
25 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
26 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
27 #include "chrome/test/base/in_process_browser_test.h" | 27 #include "chrome/test/base/in_process_browser_test.h" |
28 #include "chrome/test/base/interactive_test_utils.h" | |
28 #include "chrome/test/base/search_test_utils.h" | 29 #include "chrome/test/base/search_test_utils.h" |
29 #include "chrome/test/base/test_switches.h" | 30 #include "chrome/test/base/test_switches.h" |
30 #include "chrome/test/base/ui_test_utils.h" | 31 #include "chrome/test/base/ui_test_utils.h" |
31 #include "components/history/core/browser/history_service.h" | 32 #include "components/history/core/browser/history_service.h" |
32 #include "components/metrics/proto/omnibox_event.pb.h" | 33 #include "components/metrics/proto/omnibox_event.pb.h" |
33 #include "components/omnibox/browser/autocomplete_input.h" | 34 #include "components/omnibox/browser/autocomplete_input.h" |
34 #include "components/omnibox/browser/autocomplete_match.h" | 35 #include "components/omnibox/browser/autocomplete_match.h" |
35 #include "components/omnibox/browser/autocomplete_provider.h" | 36 #include "components/omnibox/browser/autocomplete_provider.h" |
36 #include "components/omnibox/browser/omnibox_popup_model.h" | 37 #include "components/omnibox/browser/omnibox_popup_model.h" |
37 #include "components/omnibox/browser/omnibox_view.h" | 38 #include "components/omnibox/browser/omnibox_view.h" |
39 #include "components/search_engines/template_url_service.h" | |
38 #include "content/public/browser/notification_service.h" | 40 #include "content/public/browser/notification_service.h" |
39 #include "content/public/browser/notification_types.h" | 41 #include "content/public/browser/notification_types.h" |
40 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
41 | 43 |
42 namespace { | 44 namespace { |
43 | 45 |
44 base::string16 AutocompleteResultAsString(const AutocompleteResult& result) { | 46 base::string16 AutocompleteResultAsString(const AutocompleteResult& result) { |
45 std::string output(base::StringPrintf("{%" PRIuS "} ", result.size())); | 47 std::string output(base::StringPrintf("{%" PRIuS "} ", result.size())); |
46 for (size_t i = 0; i < result.size(); ++i) { | 48 for (size_t i = 0; i < result.size(); ++i) { |
47 AutocompleteMatch match = result.match_at(i); | 49 AutocompleteMatch match = result.match_at(i); |
(...skipping 14 matching lines...) Expand all Loading... | |
62 } | 64 } |
63 | 65 |
64 LocationBar* GetLocationBar() const { | 66 LocationBar* GetLocationBar() const { |
65 return browser()->window()->GetLocationBar(); | 67 return browser()->window()->GetLocationBar(); |
66 } | 68 } |
67 | 69 |
68 AutocompleteController* GetAutocompleteController() const { | 70 AutocompleteController* GetAutocompleteController() const { |
69 return GetLocationBar()->GetOmniboxView()->model()->popup_model()-> | 71 return GetLocationBar()->GetOmniboxView()->model()->popup_model()-> |
70 autocomplete_controller(); | 72 autocomplete_controller(); |
71 } | 73 } |
74 | |
75 void FocusSearchCheckPreconditions() { | |
Peter Kasting
2016/04/15 00:58:55
Nit: Technically, I think this could be const, sin
Tom (Use chromium acct)
2016/04/15 05:20:51
Acknowledged.
| |
76 LocationBar* location_bar = GetLocationBar(); | |
77 OmniboxView* omnibox_view = location_bar->GetOmniboxView(); | |
78 OmniboxEditModel* omnibox_model = omnibox_view->model(); | |
79 | |
80 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | |
81 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), | |
82 omnibox_view->GetText()); | |
83 EXPECT_EQ(base::string16(), omnibox_model->keyword()); | |
84 EXPECT_FALSE(omnibox_model->is_keyword_hint()); | |
85 EXPECT_FALSE(omnibox_model->is_keyword_selected()); | |
86 } | |
72 }; | 87 }; |
73 | 88 |
74 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Basic) { | 89 IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, Basic) { |
75 #if defined(OS_WIN) && defined(USE_ASH) | 90 #if defined(OS_WIN) && defined(USE_ASH) |
76 // Disable this test in Metro+Ash for now (http://crbug.com/262796). | 91 // Disable this test in Metro+Ash for now (http://crbug.com/262796). |
77 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 92 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
78 switches::kAshBrowserTests)) | 93 switches::kAshBrowserTests)) |
79 return; | 94 return; |
80 #endif | 95 #endif |
81 | 96 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 #if defined(OS_WIN) && defined(USE_ASH) | 216 #if defined(OS_WIN) && defined(USE_ASH) |
202 // Disable this test in Metro+Ash for now (http://crbug.com/262796). | 217 // Disable this test in Metro+Ash for now (http://crbug.com/262796). |
203 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 218 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
204 switches::kAshBrowserTests)) | 219 switches::kAshBrowserTests)) |
205 return; | 220 return; |
206 #endif | 221 #endif |
207 | 222 |
208 WaitForTemplateURLServiceToLoad(); | 223 WaitForTemplateURLServiceToLoad(); |
209 LocationBar* location_bar = GetLocationBar(); | 224 LocationBar* location_bar = GetLocationBar(); |
210 OmniboxView* omnibox_view = location_bar->GetOmniboxView(); | 225 OmniboxView* omnibox_view = location_bar->GetOmniboxView(); |
226 OmniboxEditModel* omnibox_model = omnibox_view->model(); | |
211 | 227 |
212 // Focus search when omnibox is blank | 228 TemplateURLService* template_url_service = |
229 TemplateURLServiceFactory::GetForProfile(browser()->profile()); | |
230 base::string16 default_search_keyword = | |
231 template_url_service->GetDefaultSearchProvider()->keyword(); | |
232 | |
233 base::string16 query_text = base::ASCIIToUTF16("foo"); | |
234 | |
235 size_t selection_start, selection_end; | |
236 | |
237 // Focus search when omnibox is blank. | |
213 { | 238 { |
214 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 239 FocusSearchCheckPreconditions(); |
215 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), omnibox_view->GetText()); | |
216 | 240 |
217 location_bar->FocusSearch(); | 241 location_bar->FocusSearch(); |
218 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 242 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
219 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText()); | 243 EXPECT_EQ(base::string16(), omnibox_view->GetText()); |
244 EXPECT_EQ(default_search_keyword, omnibox_model->keyword()); | |
245 EXPECT_FALSE(omnibox_model->is_keyword_hint()); | |
246 EXPECT_TRUE(omnibox_model->is_keyword_selected()); | |
220 | 247 |
221 size_t selection_start, selection_end; | |
222 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); | 248 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); |
223 EXPECT_EQ(1U, selection_start); | 249 EXPECT_EQ(0U, selection_start); |
224 EXPECT_EQ(1U, selection_end); | 250 EXPECT_EQ(0U, selection_end); |
251 | |
252 omnibox_view->RevertAll(); | |
225 } | 253 } |
226 | 254 |
227 // Focus search when omnibox is _not_ alread in forced query mode. | 255 // Focus search when omnibox is _not_ alread in forced query mode. |
Peter Kasting
2016/04/15 00:58:55
There is no such thing as forced query mode anymor
Tom (Use chromium acct)
2016/04/15 05:20:51
Done.
| |
228 { | 256 { |
229 omnibox_view->SetUserText(base::ASCIIToUTF16("foo")); | 257 FocusSearchCheckPreconditions(); |
258 | |
259 omnibox_view->SetUserText(query_text); | |
230 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 260 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
231 EXPECT_EQ(base::ASCIIToUTF16("foo"), omnibox_view->GetText()); | 261 EXPECT_EQ(query_text, omnibox_view->GetText()); |
262 EXPECT_EQ(base::string16(), omnibox_model->keyword()); | |
263 EXPECT_FALSE(omnibox_model->is_keyword_hint()); | |
264 EXPECT_FALSE(omnibox_model->is_keyword_selected()); | |
232 | 265 |
233 location_bar->FocusSearch(); | 266 location_bar->FocusSearch(); |
234 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 267 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
235 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText()); | 268 EXPECT_EQ(query_text, omnibox_view->GetText()); |
269 EXPECT_EQ(default_search_keyword, omnibox_model->keyword()); | |
270 EXPECT_FALSE(omnibox_model->is_keyword_hint()); | |
271 EXPECT_TRUE(omnibox_model->is_keyword_selected()); | |
236 | 272 |
237 size_t selection_start, selection_end; | |
238 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); | 273 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); |
239 EXPECT_EQ(1U, selection_start); | 274 EXPECT_EQ(0U, std::min(selection_start, selection_end)); |
240 EXPECT_EQ(1U, selection_end); | 275 EXPECT_EQ(query_text.length(), std::max(selection_start, selection_end)); |
276 | |
277 omnibox_view->RevertAll(); | |
241 } | 278 } |
242 | 279 |
243 // Focus search when omnibox _is_ already in forced query mode, but no query | 280 // Focus search when omnibox _is_ already in forced query mode, but no query |
244 // has been typed. | 281 // has been typed. |
245 { | 282 { |
246 omnibox_view->SetUserText(base::ASCIIToUTF16("?")); | 283 FocusSearchCheckPreconditions(); |
247 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | |
248 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText()); | |
249 | 284 |
250 location_bar->FocusSearch(); | 285 location_bar->FocusSearch(); |
251 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 286 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
252 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText()); | 287 EXPECT_EQ(base::string16(), omnibox_view->GetText()); |
288 EXPECT_EQ(default_search_keyword, omnibox_model->keyword()); | |
289 EXPECT_FALSE(omnibox_model->is_keyword_hint()); | |
290 EXPECT_TRUE(omnibox_model->is_keyword_selected()); | |
253 | 291 |
254 size_t selection_start, selection_end; | |
255 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); | 292 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); |
256 EXPECT_EQ(1U, selection_start); | 293 EXPECT_EQ(0U, selection_start); |
257 EXPECT_EQ(1U, selection_end); | 294 EXPECT_EQ(0U, selection_end); |
295 | |
296 location_bar->FocusSearch(); | |
297 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | |
298 EXPECT_EQ(base::string16(), omnibox_view->GetText()); | |
299 EXPECT_EQ(default_search_keyword, omnibox_model->keyword()); | |
300 EXPECT_FALSE(omnibox_model->is_keyword_hint()); | |
301 EXPECT_TRUE(omnibox_model->is_keyword_selected()); | |
302 | |
303 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); | |
304 EXPECT_EQ(0U, selection_start); | |
305 EXPECT_EQ(0U, selection_end); | |
306 | |
307 omnibox_view->RevertAll(); | |
258 } | 308 } |
259 | 309 |
260 // Focus search when omnibox _is_ already in forced query mode, and some query | 310 // Focus search when omnibox _is_ already in forced query mode, and some query |
261 // has been typed. | 311 // has been typed. |
262 { | 312 { |
263 omnibox_view->SetUserText(base::ASCIIToUTF16("?foo")); | 313 FocusSearchCheckPreconditions(); |
314 | |
315 omnibox_view->SetUserText(query_text); | |
316 location_bar->FocusSearch(); | |
264 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 317 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
265 EXPECT_EQ(base::ASCIIToUTF16("?foo"), omnibox_view->GetText()); | 318 EXPECT_EQ(query_text, omnibox_view->GetText()); |
319 EXPECT_EQ(default_search_keyword, omnibox_model->keyword()); | |
320 EXPECT_FALSE(omnibox_model->is_keyword_hint()); | |
321 EXPECT_TRUE(omnibox_model->is_keyword_selected()); | |
322 | |
323 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); | |
324 EXPECT_EQ(0U, std::min(selection_start, selection_end)); | |
325 EXPECT_EQ(query_text.length(), std::max(selection_start, selection_end)); | |
266 | 326 |
267 location_bar->FocusSearch(); | 327 location_bar->FocusSearch(); |
268 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 328 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
269 EXPECT_EQ(base::ASCIIToUTF16("?foo"), omnibox_view->GetText()); | 329 EXPECT_EQ(query_text, omnibox_view->GetText()); |
330 EXPECT_EQ(default_search_keyword, omnibox_model->keyword()); | |
331 EXPECT_FALSE(omnibox_model->is_keyword_hint()); | |
332 EXPECT_TRUE(omnibox_model->is_keyword_selected()); | |
270 | 333 |
271 size_t selection_start, selection_end; | |
272 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); | 334 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); |
273 EXPECT_EQ(1U, std::min(selection_start, selection_end)); | 335 EXPECT_EQ(0U, std::min(selection_start, selection_end)); |
274 EXPECT_EQ(4U, std::max(selection_start, selection_end)); | 336 EXPECT_EQ(query_text.length(), std::max(selection_start, selection_end)); |
337 | |
338 omnibox_view->RevertAll(); | |
275 } | 339 } |
276 | 340 |
277 // Focus search when omnibox is in forced query mode with leading whitespace. | 341 // If the user gets into keyword mode using a keyboard shortcut, and presses |
342 // backspace, they should be left with their original query without their dsp | |
343 // keyword. | |
278 { | 344 { |
279 omnibox_view->SetUserText(base::ASCIIToUTF16(" ?foo")); | 345 FocusSearchCheckPreconditions(); |
346 | |
347 omnibox_view->SetUserText(query_text); | |
348 // The user presses Ctrl-K. | |
349 location_bar->FocusSearch(); | |
350 // The user presses backspace. | |
351 omnibox_model->ClearKeyword(); | |
352 | |
280 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 353 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
281 EXPECT_EQ(base::ASCIIToUTF16(" ?foo"), omnibox_view->GetText()); | 354 EXPECT_EQ(query_text, omnibox_view->GetText()); |
355 EXPECT_EQ(base::string16(), omnibox_model->keyword()); | |
356 EXPECT_FALSE(omnibox_model->is_keyword_hint()); | |
357 EXPECT_FALSE(omnibox_model->is_keyword_selected()); | |
282 | 358 |
283 location_bar->FocusSearch(); | 359 omnibox_view->RevertAll(); |
360 } | |
361 | |
362 // If the user gets into keyword mode by typing '?', they should be put into | |
363 // keyword mode for their default search provider. If they press backspace, | |
364 // they should be left with '?' in the omnibox. | |
365 { | |
366 FocusSearchCheckPreconditions(); | |
367 | |
368 omnibox_view->SetUserText(base::ASCIIToUTF16("?")); | |
369 | |
284 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); | 370 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
285 EXPECT_EQ(base::ASCIIToUTF16(" ?foo"), omnibox_view->GetText()); | 371 EXPECT_EQ(base::string16(), omnibox_view->GetText()); |
372 EXPECT_EQ(default_search_keyword, omnibox_model->keyword()); | |
373 EXPECT_FALSE(omnibox_model->is_keyword_hint()); | |
374 EXPECT_TRUE(omnibox_model->is_keyword_selected()); | |
286 | 375 |
287 size_t selection_start, selection_end; | 376 // The user presses backspace. |
288 omnibox_view->GetSelectionBounds(&selection_start, &selection_end); | 377 omnibox_model->ClearKeyword(); |
289 EXPECT_EQ(4U, std::min(selection_start, selection_end)); | 378 |
290 EXPECT_EQ(7U, std::max(selection_start, selection_end)); | 379 EXPECT_FALSE(location_bar->GetDestinationURL().is_valid()); |
380 EXPECT_EQ(base::ASCIIToUTF16("?"), omnibox_view->GetText()); | |
381 EXPECT_EQ(base::string16(), omnibox_model->keyword()); | |
382 EXPECT_FALSE(omnibox_model->is_keyword_hint()); | |
383 EXPECT_FALSE(omnibox_model->is_keyword_selected()); | |
384 | |
385 omnibox_view->RevertAll(); | |
291 } | 386 } |
292 } | 387 } |
OLD | NEW |