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
3 // found in the LICENSE file.
7 #include "base/command_line.h"
8 #include "base/scoped_observer.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/autocomplete/history_quick_provider.h"
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/history/history_service_factory.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/search_engines/template_url_service_factory.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_commands.h"
22 #include "chrome/browser/ui/browser_window.h"
23 #include "chrome/browser/ui/location_bar/location_bar.h"
24 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
25 #include "chrome/browser/ui/omnibox/omnibox_view.h"
26 #include "chrome/browser/ui/tabs/tab_strip_model.h"
27 #include "chrome/browser/ui/toolbar/test_toolbar_model.h"
28 #include "chrome/common/chrome_paths.h"
29 #include "chrome/common/chrome_switches.h"
30 #include "chrome/common/url_constants.h"
31 #include "chrome/test/base/in_process_browser_test.h"
32 #include "chrome/test/base/interactive_test_utils.h"
33 #include "chrome/test/base/ui_test_utils.h"
34 #include "components/bookmarks/browser/bookmark_model.h"
35 #include "components/bookmarks/browser/bookmark_utils.h"
36 #include "components/bookmarks/test/bookmark_test_helpers.h"
37 #include "components/history/core/browser/history_service.h"
38 #include "components/history/core/browser/history_service_observer.h"
39 #include "components/omnibox/autocomplete_input.h"
40 #include "components/omnibox/autocomplete_match.h"
41 #include "components/search_engines/template_url.h"
42 #include "components/search_engines/template_url_service.h"
43 #include "content/public/browser/notification_service.h"
44 #include "content/public/browser/web_contents.h"
45 #include "net/dns/mock_host_resolver.h"
46 #include "ui/base/clipboard/clipboard.h"
47 #include "ui/base/clipboard/scoped_clipboard_writer.h"
48 #include "ui/events/event_constants.h"
49 #include "ui/events/keycodes/keyboard_codes.h"
50 #include "ui/gfx/geometry/point.h"
52 using base::ASCIIToUTF16
;
53 using base::UTF16ToUTF8
;
55 using base::TimeDelta
;
56 using bookmarks::BookmarkModel
;
60 const char kSearchKeyword
[] = "foo";
61 const char kSearchKeyword2
[] = "footest.com";
62 const ui::KeyboardCode kSearchKeywordKeys
[] = {
63 ui::VKEY_F
, ui::VKEY_O
, ui::VKEY_O
, ui::VKEY_UNKNOWN
65 const ui::KeyboardCode kSearchKeywordPrefixKeys
[] = {
66 ui::VKEY_F
, ui::VKEY_O
, ui::VKEY_UNKNOWN
68 const ui::KeyboardCode kSearchKeywordCompletionKeys
[] = {
69 ui::VKEY_O
, ui::VKEY_UNKNOWN
71 const char kSearchURL
[] = "http://www.foo.com/search?q={searchTerms}";
72 const char kSearchShortName
[] = "foo";
73 const char kSearchText
[] = "abc";
74 const ui::KeyboardCode kSearchTextKeys
[] = {
75 ui::VKEY_A
, ui::VKEY_B
, ui::VKEY_C
, ui::VKEY_UNKNOWN
77 const char kSearchTextURL
[] = "http://www.foo.com/search?q=abc";
79 const char kInlineAutocompleteText
[] = "def";
80 const ui::KeyboardCode kInlineAutocompleteTextKeys
[] = {
81 ui::VKEY_D
, ui::VKEY_E
, ui::VKEY_F
, ui::VKEY_UNKNOWN
84 // Hostnames that shall be blocked by host resolver.
85 const char *kBlockedHostnames
[] = {
99 const struct TestHistoryEntry
{
105 } kHistoryEntries
[] = {
106 {"http://www.bar.com/1", "Page 1", 10, 10, false },
107 {"http://www.bar.com/2", "Page 2", 9, 9, false },
108 {"http://www.bar.com/3", "Page 3", 8, 8, false },
109 {"http://www.bar.com/4", "Page 4", 7, 7, false },
110 {"http://www.bar.com/5", "Page 5", 6, 6, false },
111 {"http://www.bar.com/6", "Page 6", 5, 5, false },
112 {"http://www.bar.com/7", "Page 7", 4, 4, false },
113 {"http://www.bar.com/8", "Page 8", 3, 3, false },
114 {"http://www.bar.com/9", "Page 9", 2, 2, false },
115 {"http://www.site.com/path/1", "Site 1", 4, 4, false },
116 {"http://www.site.com/path/2", "Site 2", 3, 3, false },
117 {"http://www.site.com/path/3", "Site 3", 2, 2, false },
119 // To trigger inline autocomplete.
120 {"http://www.def.com", "Page def", 10000, 10000, true },
122 // Used in particular for the desired TLD test. This makes it test
123 // the interesting case when there's an intranet host with the same
125 {"http://bar/", "Bar", 1, 0, false },
128 // Stores the given text to clipboard.
129 void SetClipboardText(const base::string16
& text
) {
130 ui::ScopedClipboardWriter
writer(ui::CLIPBOARD_TYPE_COPY_PASTE
);
131 writer
.WriteText(text
);
134 #if defined(OS_MACOSX)
135 const int kCtrlOrCmdMask
= ui::EF_COMMAND_DOWN
;
137 const int kCtrlOrCmdMask
= ui::EF_CONTROL_DOWN
;
142 class OmniboxViewTest
: public InProcessBrowserTest
,
143 public content::NotificationObserver
,
144 public history::HistoryServiceObserver
{
146 OmniboxViewTest() : observer_(this) {}
148 // history::HisoryServiceObserver
149 void OnHistoryServiceLoaded(
150 history::HistoryService
* history_service
) override
{
151 base::MessageLoop::current()->Quit();
155 void SetUpOnMainThread() override
{
156 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
157 ASSERT_NO_FATAL_FAILURE(SetupComponents());
158 chrome::FocusLocationBar(browser());
159 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
162 static void GetOmniboxViewForBrowser(
163 const Browser
* browser
,
164 OmniboxView
** omnibox_view
) {
165 BrowserWindow
* window
= browser
->window();
167 LocationBar
* location_bar
= window
->GetLocationBar();
168 ASSERT_TRUE(location_bar
);
169 *omnibox_view
= location_bar
->GetOmniboxView();
170 ASSERT_TRUE(*omnibox_view
);
173 void GetOmniboxView(OmniboxView
** omnibox_view
) {
174 GetOmniboxViewForBrowser(browser(), omnibox_view
);
177 static void SendKeyForBrowser(const Browser
* browser
,
178 ui::KeyboardCode key
,
180 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
182 (modifiers
& ui::EF_CONTROL_DOWN
) != 0,
183 (modifiers
& ui::EF_SHIFT_DOWN
) != 0,
184 (modifiers
& ui::EF_ALT_DOWN
) != 0,
185 (modifiers
& ui::EF_COMMAND_DOWN
) != 0));
188 void SendKey(ui::KeyboardCode key
, int modifiers
) {
189 SendKeyForBrowser(browser(), key
, modifiers
);
192 void SendKeySequence(const ui::KeyboardCode
* keys
) {
193 for (; *keys
!= ui::VKEY_UNKNOWN
; ++keys
)
194 ASSERT_NO_FATAL_FAILURE(SendKey(*keys
, 0));
197 bool SendKeyAndWait(const Browser
* browser
,
198 ui::KeyboardCode key
,
201 const content::NotificationSource
& source
)
203 return ui_test_utils::SendKeyPressAndWait(
205 (modifiers
& ui::EF_CONTROL_DOWN
) != 0,
206 (modifiers
& ui::EF_SHIFT_DOWN
) != 0,
207 (modifiers
& ui::EF_ALT_DOWN
) != 0,
208 (modifiers
& ui::EF_COMMAND_DOWN
) != 0,
212 void WaitForTabOpenOrCloseForBrowser(const Browser
* browser
,
213 int expected_tab_count
) {
214 int tab_count
= browser
->tab_strip_model()->count();
215 if (tab_count
== expected_tab_count
)
218 content::NotificationRegistrar registrar
;
220 (tab_count
< expected_tab_count
) ?
221 static_cast<int>(chrome::NOTIFICATION_TAB_PARENTED
) :
222 static_cast<int>(content::NOTIFICATION_WEB_CONTENTS_DESTROYED
),
223 content::NotificationService::AllSources());
225 while (!HasFailure() &&
226 browser
->tab_strip_model()->count() != expected_tab_count
) {
227 content::RunMessageLoop();
230 ASSERT_EQ(expected_tab_count
, browser
->tab_strip_model()->count());
233 void WaitForTabOpenOrClose(int expected_tab_count
) {
234 WaitForTabOpenOrCloseForBrowser(browser(), expected_tab_count
);
237 void WaitForAutocompleteControllerDone() {
238 OmniboxView
* omnibox_view
= NULL
;
239 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
241 AutocompleteController
* controller
=
242 omnibox_view
->model()->autocomplete_controller();
243 ASSERT_TRUE(controller
);
245 if (controller
->done())
248 content::NotificationRegistrar registrar
;
250 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
251 content::Source
<AutocompleteController
>(controller
));
253 while (!HasFailure() && !controller
->done())
254 content::RunMessageLoop();
256 ASSERT_TRUE(controller
->done());
259 void SetupSearchEngine() {
260 Profile
* profile
= browser()->profile();
261 TemplateURLService
* model
=
262 TemplateURLServiceFactory::GetForProfile(profile
);
265 ui_test_utils::WaitForTemplateURLServiceToLoad(model
);
267 ASSERT_TRUE(model
->loaded());
269 TemplateURLData data
;
270 data
.SetShortName(ASCIIToUTF16(kSearchShortName
));
271 data
.SetKeyword(ASCIIToUTF16(kSearchKeyword
));
272 data
.SetURL(kSearchURL
);
273 TemplateURL
* template_url
= new TemplateURL(data
);
274 model
->Add(template_url
);
275 model
->SetUserSelectedDefaultSearchProvider(template_url
);
277 data
.SetKeyword(ASCIIToUTF16(kSearchKeyword2
));
278 model
->Add(new TemplateURL(data
));
280 // Remove built-in template urls, like google.com, bing.com etc., as they
281 // may appear as autocomplete suggests and interfere with our tests.
282 TemplateURLService::TemplateURLVector urls
= model
->GetTemplateURLs();
283 for (TemplateURLService::TemplateURLVector::const_iterator i
= urls
.begin();
286 if ((*i
)->prepopulate_id() != 0)
291 void AddHistoryEntry(const TestHistoryEntry
& entry
, const Time
& time
) {
292 Profile
* profile
= browser()->profile();
293 history::HistoryService
* history_service
=
294 HistoryServiceFactory::GetForProfile(
295 profile
, ServiceAccessType::EXPLICIT_ACCESS
);
296 ASSERT_TRUE(history_service
);
298 if (!history_service
->BackendLoaded()) {
299 observer_
.Add(history_service
);
300 content::RunMessageLoop();
303 BookmarkModel
* bookmark_model
=
304 BookmarkModelFactory::GetForProfile(profile
);
305 ASSERT_TRUE(bookmark_model
);
306 bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model
);
309 // Add everything in order of time. We don't want to have a time that
310 // is "right now" or it will nondeterministically appear in the results.
311 history_service
->AddPageWithDetails(url
, base::UTF8ToUTF16(entry
.title
),
313 entry
.typed_count
, time
, false,
314 history::SOURCE_BROWSED
);
316 bookmarks::AddIfNotBookmarked(bookmark_model
, url
, base::string16());
317 // Wait at least for the AddPageWithDetails() call to finish.
319 ScopedObserver
<history::HistoryService
, history::HistoryServiceObserver
>
321 observer
.Add(history_service
);
322 content::RunMessageLoop();
323 // We don't want to return until all observers have processed this
324 // notification, because some (e.g. the in-memory history database) may do
325 // something important. Since we don't know where in the observer list we
326 // stand, just spin the message loop once more to allow the current
327 // callstack to complete.
328 content::RunAllPendingInMessageLoop();
332 void SetupHistory() {
333 // Add enough history pages containing |kSearchText| to trigger
334 // open history page url in autocomplete result.
335 for (size_t i
= 0; i
< arraysize(kHistoryEntries
); i
++) {
336 // Add everything in order of time. We don't want to have a time that
337 // is "right now" or it will nondeterministically appear in the results.
338 Time t
= Time::Now() - TimeDelta::FromHours(i
+ 1);
339 ASSERT_NO_FATAL_FAILURE(AddHistoryEntry(kHistoryEntries
[i
], t
));
343 void SetupHostResolver() {
344 for (size_t i
= 0; i
< arraysize(kBlockedHostnames
); ++i
)
345 host_resolver()->AddSimulatedFailure(kBlockedHostnames
[i
]);
348 void SetupComponents() {
349 ASSERT_NO_FATAL_FAILURE(SetupHostResolver());
350 ASSERT_NO_FATAL_FAILURE(SetupSearchEngine());
351 ASSERT_NO_FATAL_FAILURE(SetupHistory());
354 void Observe(int type
,
355 const content::NotificationSource
& source
,
356 const content::NotificationDetails
& details
) override
{
358 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED
:
359 case chrome::NOTIFICATION_TAB_PARENTED
:
360 case chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
:
363 FAIL() << "Unexpected notification type";
365 base::MessageLoop::current()->Quit();
368 void OnURLsModified(history::HistoryService
* history_service
,
369 const history::URLRows
& changed_urls
) override
{
370 base::MessageLoop::current()->Quit();
374 ScopedObserver
<history::HistoryService
, OmniboxViewTest
> observer_
;
376 DISALLOW_COPY_AND_ASSIGN(OmniboxViewTest
);
379 // Test if ctrl-* accelerators are workable in omnibox.
380 // See http://crbug.com/19193: omnibox blocks ctrl-* commands
382 // Flaky on interactive tests (dbg), http://crbug.com/69433
383 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, DISABLED_BrowserAccelerators
) {
384 OmniboxView
* omnibox_view
= NULL
;
385 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
387 int tab_count
= browser()->tab_strip_model()->count();
390 chrome::NewTab(browser());
391 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count
+ 1));
393 // Select the first Tab.
394 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_1
, kCtrlOrCmdMask
));
395 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
397 chrome::FocusLocationBar(browser());
399 // Select the second Tab.
400 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_2
, kCtrlOrCmdMask
));
401 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
403 chrome::FocusLocationBar(browser());
405 // Try ctrl-w to close a Tab.
406 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_W
, kCtrlOrCmdMask
));
407 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count
));
409 // Try ctrl-l to focus location bar.
410 omnibox_view
->SetUserText(ASCIIToUTF16("Hello world"));
411 EXPECT_FALSE(omnibox_view
->IsSelectAll());
412 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_L
, kCtrlOrCmdMask
));
413 EXPECT_TRUE(omnibox_view
->IsSelectAll());
415 // Try editing the location bar text.
416 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT
, 0));
417 EXPECT_FALSE(omnibox_view
->IsSelectAll());
418 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_S
, 0));
419 EXPECT_EQ(ASCIIToUTF16("Hello worlds"), omnibox_view
->GetText());
421 // Try ctrl-x to cut text.
422 #if defined(OS_MACOSX)
423 // Mac uses alt-left/right to select a word.
424 ASSERT_NO_FATAL_FAILURE(
425 SendKey(ui::VKEY_LEFT
, ui::EF_SHIFT_DOWN
| ui::EF_ALT_DOWN
));
427 ASSERT_NO_FATAL_FAILURE(
428 SendKey(ui::VKEY_LEFT
, ui::EF_SHIFT_DOWN
| ui::EF_CONTROL_DOWN
));
430 EXPECT_FALSE(omnibox_view
->IsSelectAll());
431 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_X
, kCtrlOrCmdMask
));
432 EXPECT_EQ(ASCIIToUTF16("Hello "), omnibox_view
->GetText());
434 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
435 // Try alt-f4 to close the browser.
436 ASSERT_TRUE(SendKeyAndWait(
437 browser(), ui::VKEY_F4
, ui::EF_ALT_DOWN
,
438 chrome::NOTIFICATION_BROWSER_CLOSED
,
439 content::Source
<Browser
>(browser())));
443 // Fails on Linux. http://crbug.com/408634
444 #if defined(OS_LINUX)
445 #define MAYBE_PopupAccelerators DISABLED_PopupAccelerators
447 #define MAYBE_PopupAccelerators PopupAccelerators
450 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_PopupAccelerators
) {
452 Browser
* popup
= CreateBrowserForPopup(browser()->profile());
453 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup
));
454 OmniboxView
* omnibox_view
= NULL
;
455 ASSERT_NO_FATAL_FAILURE(
456 GetOmniboxViewForBrowser(popup
, &omnibox_view
));
457 chrome::FocusLocationBar(popup
);
458 EXPECT_TRUE(omnibox_view
->IsSelectAll());
460 #if !defined(OS_MACOSX)
461 // Try ctrl-w to close the popup.
462 // This piece of code doesn't work on Mac, because the Browser object won't
463 // be destroyed before finishing the current message loop iteration, thus
464 // No BROWSER_CLOSED notification will be sent.
465 ASSERT_TRUE(SendKeyAndWait(
466 popup
, ui::VKEY_W
, ui::EF_CONTROL_DOWN
,
467 chrome::NOTIFICATION_BROWSER_CLOSED
, content::Source
<Browser
>(popup
)));
469 // Create another popup.
470 popup
= CreateBrowserForPopup(browser()->profile());
471 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup
));
472 ASSERT_NO_FATAL_FAILURE(
473 GetOmniboxViewForBrowser(popup
, &omnibox_view
));
476 // Set the edit text to "Hello world".
477 omnibox_view
->SetUserText(ASCIIToUTF16("Hello world"));
478 chrome::FocusLocationBar(popup
);
479 EXPECT_TRUE(omnibox_view
->IsSelectAll());
481 // Try editing the location bar text -- should be disallowed.
482 ASSERT_NO_FATAL_FAILURE(SendKeyForBrowser(popup
, ui::VKEY_S
, 0));
483 EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view
->GetText());
484 EXPECT_TRUE(omnibox_view
->IsSelectAll());
486 ASSERT_NO_FATAL_FAILURE(
487 SendKeyForBrowser(popup
, ui::VKEY_X
, kCtrlOrCmdMask
));
488 EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view
->GetText());
489 EXPECT_TRUE(omnibox_view
->IsSelectAll());
491 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
492 // Try alt-f4 to close the popup.
493 ASSERT_TRUE(SendKeyAndWait(
494 popup
, ui::VKEY_F4
, ui::EF_ALT_DOWN
,
495 chrome::NOTIFICATION_BROWSER_CLOSED
, content::Source
<Browser
>(popup
)));
499 // http://crbug.com/133341
500 #if defined(OS_LINUX)
501 #define MAYBE_BackspaceInKeywordMode DISABLED_BackspaceInKeywordMode
503 #define MAYBE_BackspaceInKeywordMode BackspaceInKeywordMode
506 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_BackspaceInKeywordMode
) {
507 OmniboxView
* omnibox_view
= NULL
;
508 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
510 // Trigger keyword hint mode.
511 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys
));
512 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
513 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
515 // Trigger keyword mode.
516 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
517 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
518 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
520 // Backspace without search text should bring back keyword hint mode.
521 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
522 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
523 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
525 // Trigger keyword mode again.
526 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
527 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
528 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
530 // Input something as search text.
531 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys
));
533 // Should stay in keyword mode while deleting search text by pressing
535 for (size_t i
= 0; i
< arraysize(kSearchText
) - 1; ++i
) {
536 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
537 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
538 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
541 // Input something as search text.
542 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys
));
544 // Move cursor to the beginning of the search text.
545 #if defined(OS_MACOSX)
546 // Home doesn't work on Mac trybot.
547 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A
, ui::EF_CONTROL_DOWN
));
549 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_HOME
, 0));
551 // Backspace at the beginning of the search text shall turn off
553 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
554 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
555 ASSERT_EQ(base::string16(), omnibox_view
->model()->keyword());
556 ASSERT_EQ(std::string(kSearchKeyword
) + kSearchText
,
557 UTF16ToUTF8(omnibox_view
->GetText()));
560 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, Escape
) {
561 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIHistoryURL
));
562 chrome::FocusLocationBar(browser());
564 OmniboxView
* omnibox_view
= NULL
;
565 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
567 base::string16 old_text
= omnibox_view
->GetText();
568 EXPECT_FALSE(old_text
.empty());
569 EXPECT_TRUE(omnibox_view
->IsSelectAll());
571 // Delete all text in omnibox.
572 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
573 EXPECT_TRUE(omnibox_view
->GetText().empty());
575 // Escape shall revert the text in omnibox.
576 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE
, 0));
577 EXPECT_EQ(old_text
, omnibox_view
->GetText());
578 EXPECT_TRUE(omnibox_view
->IsSelectAll());
581 #if defined(OS_LINUX)
582 #define MAYBE_DesiredTLD DISABLED_DesiredTLD
584 #define MAYBE_DesiredTLD DesiredTLD
587 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_DesiredTLD
) {
588 OmniboxView
* omnibox_view
= NULL
;
589 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
590 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
591 ASSERT_TRUE(popup_model
);
594 const ui::KeyboardCode kKeys
[] = {
595 ui::VKEY_B
, ui::VKEY_A
, ui::VKEY_R
, ui::VKEY_UNKNOWN
597 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys
));
598 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
599 ASSERT_TRUE(popup_model
->IsOpen());
600 // ctrl-Enter triggers desired_tld feature, thus www.bar.com shall be
602 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN
, ui::EF_CONTROL_DOWN
,
603 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
604 content::Source
<content::NavigationController
>(
605 &browser()->tab_strip_model()->GetActiveWebContents()->
608 GURL url
= browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
609 EXPECT_EQ("www.bar.com", url
.host());
610 EXPECT_EQ("/", url
.path());
613 #if defined(OS_LINUX)
614 #define MAYBE_DesiredTLDWithTemporaryText DISABLED_DesiredTLDWithTemporaryText
616 #define MAYBE_DesiredTLDWithTemporaryText DesiredTLDWithTemporaryText
619 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_DesiredTLDWithTemporaryText
) {
620 OmniboxView
* omnibox_view
= NULL
;
621 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
622 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
623 ASSERT_TRUE(popup_model
);
625 Profile
* profile
= browser()->profile();
626 TemplateURLService
* template_url_service
=
627 TemplateURLServiceFactory::GetForProfile(profile
);
629 // Add a non-substituting keyword. This ensures the popup will have a
630 // non-verbatim entry with "ab" as a prefix. This way, by arrowing down, we
631 // can set "abc" as temporary text in the omnibox.
632 TemplateURLData data
;
633 data
.SetShortName(ASCIIToUTF16("abc"));
634 data
.SetKeyword(ASCIIToUTF16(kSearchText
));
635 data
.SetURL("http://abc.com/");
636 template_url_service
->Add(new TemplateURL(data
));
638 // Send "ab", so that an "abc" entry appears in the popup.
639 const ui::KeyboardCode kSearchTextPrefixKeys
[] = {
640 ui::VKEY_A
, ui::VKEY_B
, ui::VKEY_UNKNOWN
642 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextPrefixKeys
));
643 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
644 ASSERT_TRUE(popup_model
->IsOpen());
646 // Arrow down to the "abc" entry in the popup.
647 size_t size
= popup_model
->result().size();
648 while (popup_model
->selected_line() < size
- 1) {
649 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN
, 0));
650 if (omnibox_view
->GetText() == ASCIIToUTF16("abc"))
653 ASSERT_EQ(ASCIIToUTF16("abc"), omnibox_view
->GetText());
655 // Hitting ctrl-enter should navigate based on the current text rather than
656 // the original input, i.e. to www.abc.com instead of www.ab.com.
657 ASSERT_TRUE(SendKeyAndWait(
658 browser(), ui::VKEY_RETURN
, ui::EF_CONTROL_DOWN
,
659 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
660 content::Source
<content::NavigationController
>(
661 &browser()->tab_strip_model()->GetActiveWebContents()->
664 GURL
url(browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
665 EXPECT_EQ("www.abc.com", url
.host());
666 EXPECT_EQ("/", url
.path());
669 // See http://crbug.com/431575.
670 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, ClearUserTextAfterBackgroundCommit
) {
671 OmniboxView
* omnibox_view
= NULL
;
672 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
674 // Navigate in first tab and enter text into the omnibox.
675 GURL
url1("data:text/html,page1");
676 ui_test_utils::NavigateToURL(browser(), url1
);
677 omnibox_view
->SetUserText(ASCIIToUTF16("foo"));
678 content::WebContents
* contents
=
679 browser()->tab_strip_model()->GetActiveWebContents();
681 // Create another tab in the foreground.
682 AddTabAtIndex(1, url1
, ui::PAGE_TRANSITION_TYPED
);
683 EXPECT_EQ(2, browser()->tab_strip_model()->count());
684 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
686 // Navigate in the first tab, currently in the background.
687 GURL
url2("data:text/html,page2");
688 chrome::NavigateParams
params(browser(), url2
, ui::PAGE_TRANSITION_LINK
);
689 params
.source_contents
= contents
;
690 params
.disposition
= CURRENT_TAB
;
691 ui_test_utils::NavigateToURL(¶ms
);
693 // Switch back to the first tab. The user text should be cleared, and the
694 // omnibox should have the new URL.
695 browser()->tab_strip_model()->ActivateTabAt(0, true);
696 EXPECT_EQ(ASCIIToUTF16(url2
.spec()), omnibox_view
->GetText());
699 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, AltEnter
) {
700 OmniboxView
* omnibox_view
= NULL
;
701 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
703 omnibox_view
->SetUserText(ASCIIToUTF16(chrome::kChromeUIHistoryURL
));
704 int tab_count
= browser()->tab_strip_model()->count();
705 // alt-Enter opens a new tab.
706 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RETURN
, ui::EF_ALT_DOWN
));
707 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count
+ 1));
710 // http://crbug.com/133354, http://crbug.com/146953
711 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, DISABLED_EnterToSearch
) {
712 OmniboxView
* omnibox_view
= NULL
;
713 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
714 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
715 ASSERT_TRUE(popup_model
);
717 // Test Enter to search.
718 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys
));
719 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
720 ASSERT_TRUE(popup_model
->IsOpen());
722 // Check if the default match result is Search Primary Provider.
723 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
724 popup_model
->result().default_match()->type
);
726 // Open the default match.
727 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN
, 0,
728 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
729 content::Source
<content::NavigationController
>(
730 &browser()->tab_strip_model()->GetActiveWebContents()->
732 GURL url
= browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
733 EXPECT_EQ(kSearchTextURL
, url
.spec());
735 // Test that entering a single character then Enter performs a search.
736 const ui::KeyboardCode kSearchSingleCharKeys
[] = {
737 ui::VKEY_Z
, ui::VKEY_UNKNOWN
739 chrome::FocusLocationBar(browser());
740 EXPECT_TRUE(omnibox_view
->IsSelectAll());
741 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchSingleCharKeys
));
742 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
743 ASSERT_TRUE(popup_model
->IsOpen());
744 EXPECT_EQ("z", UTF16ToUTF8(omnibox_view
->GetText()));
746 // Check if the default match result is Search Primary Provider.
747 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
748 popup_model
->result().default_match()->type
);
750 // Open the default match.
751 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN
, 0,
752 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
753 content::Source
<content::NavigationController
>(
754 &browser()->tab_strip_model()->GetActiveWebContents()->
756 url
= browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
757 EXPECT_EQ("http://www.foo.com/search?q=z", url
.spec());
760 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, EscapeToDefaultMatch
) {
761 OmniboxView
* omnibox_view
= NULL
;
762 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
763 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
764 ASSERT_TRUE(popup_model
);
766 // Input something to trigger inline autocomplete.
767 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys
));
768 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
769 ASSERT_TRUE(popup_model
->IsOpen());
771 base::string16 old_text
= omnibox_view
->GetText();
773 // Make sure inline autocomplete is triggerred.
774 EXPECT_GT(old_text
.length(), arraysize(kInlineAutocompleteText
) - 1);
776 size_t old_selected_line
= popup_model
->selected_line();
777 EXPECT_EQ(0U, old_selected_line
);
779 // Move to another line with different text.
780 size_t size
= popup_model
->result().size();
781 while (popup_model
->selected_line() < size
- 1) {
782 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN
, 0));
783 ASSERT_NE(old_selected_line
, popup_model
->selected_line());
784 if (old_text
!= omnibox_view
->GetText())
788 EXPECT_NE(old_text
, omnibox_view
->GetText());
790 // Escape shall revert back to the default match item.
791 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE
, 0));
792 EXPECT_EQ(old_text
, omnibox_view
->GetText());
793 EXPECT_EQ(old_selected_line
, popup_model
->selected_line());
796 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, BasicTextOperations
) {
797 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL
));
798 chrome::FocusLocationBar(browser());
800 OmniboxView
* omnibox_view
= NULL
;
801 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
803 base::string16 old_text
= omnibox_view
->GetText();
804 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL
), old_text
);
805 EXPECT_TRUE(omnibox_view
->IsSelectAll());
808 omnibox_view
->GetSelectionBounds(&start
, &end
);
809 #if defined(OS_WIN) || defined(OS_LINUX)
810 // Views textfields select-all in reverse to show the leading text.
811 std::swap(start
, end
);
813 EXPECT_EQ(0U, start
);
814 EXPECT_EQ(old_text
.size(), end
);
816 // Move the cursor to the end.
817 #if defined(OS_MACOSX)
818 // End doesn't work on Mac trybot.
819 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E
, ui::EF_CONTROL_DOWN
));
821 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END
, 0));
823 EXPECT_FALSE(omnibox_view
->IsSelectAll());
825 // Make sure the cursor is placed correctly.
826 omnibox_view
->GetSelectionBounds(&start
, &end
);
827 EXPECT_EQ(old_text
.size(), start
);
828 EXPECT_EQ(old_text
.size(), end
);
830 // Insert one character at the end. Make sure we won't insert
831 // anything after the special ZWS mark used in gtk implementation.
832 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A
, 0));
833 EXPECT_EQ(old_text
+ base::char16('a'), omnibox_view
->GetText());
835 // Delete one character from the end. Make sure we won't delete the special
836 // ZWS mark used in gtk implementation.
837 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
838 EXPECT_EQ(old_text
, omnibox_view
->GetText());
840 omnibox_view
->SelectAll(true);
841 EXPECT_TRUE(omnibox_view
->IsSelectAll());
842 omnibox_view
->GetSelectionBounds(&start
, &end
);
843 #if defined(OS_WIN) || defined(OS_LINUX)
844 // Views textfields select-all in reverse to show the leading text.
845 std::swap(start
, end
);
847 EXPECT_EQ(0U, start
);
848 EXPECT_EQ(old_text
.size(), end
);
850 // Delete the content
851 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE
, 0));
852 EXPECT_TRUE(omnibox_view
->IsSelectAll());
853 omnibox_view
->GetSelectionBounds(&start
, &end
);
854 EXPECT_EQ(0U, start
);
856 EXPECT_TRUE(omnibox_view
->GetText().empty());
858 // Check if RevertAll() can set text and cursor correctly.
859 omnibox_view
->RevertAll();
860 EXPECT_FALSE(omnibox_view
->IsSelectAll());
861 EXPECT_EQ(old_text
, omnibox_view
->GetText());
862 omnibox_view
->GetSelectionBounds(&start
, &end
);
863 EXPECT_EQ(old_text
.size(), start
);
864 EXPECT_EQ(old_text
.size(), end
);
867 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, AcceptKeywordBySpace
) {
868 OmniboxView
* omnibox_view
= NULL
;
869 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
871 base::string16
search_keyword(ASCIIToUTF16(kSearchKeyword
));
873 // Trigger keyword hint mode.
874 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys
));
875 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
876 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
877 ASSERT_EQ(search_keyword
, omnibox_view
->GetText());
879 // Trigger keyword mode by space.
880 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
881 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
882 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
883 ASSERT_TRUE(omnibox_view
->GetText().empty());
885 // Revert to keyword hint mode.
886 omnibox_view
->model()->ClearKeyword();
887 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
888 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
889 ASSERT_EQ(search_keyword
, omnibox_view
->GetText());
891 // Keyword should also be accepted by typing an ideographic space.
892 omnibox_view
->OnBeforePossibleChange();
893 omnibox_view
->SetWindowTextAndCaretPos(search_keyword
+
894 base::WideToUTF16(L
"\x3000"), search_keyword
.length() + 1, false, false);
895 omnibox_view
->OnAfterPossibleChange();
896 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
897 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
898 ASSERT_TRUE(omnibox_view
->GetText().empty());
900 // Revert to keyword hint mode.
901 omnibox_view
->model()->ClearKeyword();
902 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
903 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
904 ASSERT_EQ(search_keyword
, omnibox_view
->GetText());
906 // Keyword shouldn't be accepted by pressing space with a trailing
908 omnibox_view
->SetWindowTextAndCaretPos(search_keyword
+ base::char16(' '),
909 search_keyword
.length() + 1, false, false);
910 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
911 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
912 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
913 ASSERT_EQ(search_keyword
+ ASCIIToUTF16(" "), omnibox_view
->GetText());
915 // Keyword shouldn't be accepted by deleting the trailing space.
916 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
917 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
918 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
919 ASSERT_EQ(search_keyword
+ base::char16(' '), omnibox_view
->GetText());
921 // Keyword shouldn't be accepted by pressing space before a trailing space.
922 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
923 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
924 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
925 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
926 ASSERT_EQ(search_keyword
+ ASCIIToUTF16(" "), omnibox_view
->GetText());
928 // Keyword should be accepted by pressing space in the middle of context and
929 // just after the keyword.
930 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
931 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A
, 0));
932 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
933 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
934 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
935 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
936 ASSERT_EQ(ASCIIToUTF16("a "), omnibox_view
->GetText());
938 omnibox_view
->GetSelectionBounds(&start
, &end
);
939 EXPECT_EQ(0U, start
);
942 // Keyword shouldn't be accepted by pasting "foo bar".
943 omnibox_view
->SetUserText(base::string16());
944 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
945 ASSERT_TRUE(omnibox_view
->model()->keyword().empty());
947 omnibox_view
->OnBeforePossibleChange();
948 omnibox_view
->model()->OnPaste();
949 omnibox_view
->SetWindowTextAndCaretPos(search_keyword
+
950 ASCIIToUTF16(" bar"), search_keyword
.length() + 4, false, false);
951 omnibox_view
->OnAfterPossibleChange();
952 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
953 ASSERT_TRUE(omnibox_view
->model()->keyword().empty());
954 ASSERT_EQ(search_keyword
+ ASCIIToUTF16(" bar"), omnibox_view
->GetText());
956 // Keyword shouldn't be accepted for case like: "foo b|ar" -> "foo b |ar".
957 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
958 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
959 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
960 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
961 ASSERT_TRUE(omnibox_view
->model()->keyword().empty());
962 ASSERT_EQ(search_keyword
+ ASCIIToUTF16(" b ar"), omnibox_view
->GetText());
964 // Keyword could be accepted by pressing space with a selected range at the
966 omnibox_view
->OnBeforePossibleChange();
967 omnibox_view
->OnInlineAutocompleteTextMaybeChanged(
968 search_keyword
+ ASCIIToUTF16(" "), search_keyword
.length());
969 omnibox_view
->OnAfterPossibleChange();
970 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
971 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
972 ASSERT_EQ(search_keyword
+ ASCIIToUTF16(" "), omnibox_view
->GetText());
974 omnibox_view
->GetSelectionBounds(&start
, &end
);
975 ASSERT_NE(start
, end
);
976 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
977 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
978 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
979 ASSERT_EQ(base::string16(), omnibox_view
->GetText());
981 // Space should accept keyword even when inline autocomplete is available.
982 omnibox_view
->SetUserText(base::string16());
983 const TestHistoryEntry kHistoryFoobar
= {
984 "http://www.foobar.com", "Page foobar", 100, 100, true
987 // Add a history entry to trigger inline autocomplete when typing "foo".
988 ASSERT_NO_FATAL_FAILURE(
989 AddHistoryEntry(kHistoryFoobar
, Time::Now() - TimeDelta::FromHours(1)));
991 // Type "fo" to trigger inline autocomplete.
992 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordPrefixKeys
));
993 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
994 ASSERT_TRUE(omnibox_view
->model()->popup_model()->IsOpen());
995 ASSERT_NE(search_keyword
, omnibox_view
->GetText());
997 // Keyword hint shouldn't be visible.
998 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
999 ASSERT_TRUE(omnibox_view
->model()->keyword().empty());
1001 // Add the "o". Inline autocompletion should still happen, but now we
1002 // should also get a keyword hint because we've typed a keyword exactly.
1003 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordCompletionKeys
));
1004 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1005 ASSERT_TRUE(omnibox_view
->model()->popup_model()->IsOpen());
1006 ASSERT_NE(search_keyword
, omnibox_view
->GetText());
1007 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
1008 ASSERT_FALSE(omnibox_view
->model()->keyword().empty());
1010 // Trigger keyword mode by space.
1011 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
1012 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
1013 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
1014 ASSERT_TRUE(omnibox_view
->GetText().empty());
1016 // Space in the middle of a temporary text, which separates the text into
1017 // keyword and replacement portions, should trigger keyword mode.
1018 omnibox_view
->SetUserText(base::string16());
1019 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys
));
1020 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1021 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1022 ASSERT_TRUE(popup_model
->IsOpen());
1023 ASSERT_EQ(ASCIIToUTF16("foobar.com"), omnibox_view
->GetText());
1024 omnibox_view
->model()->OnUpOrDownKeyPressed(1);
1025 omnibox_view
->model()->OnUpOrDownKeyPressed(-1);
1026 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
1027 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
1028 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
1029 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
1030 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
1031 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
1032 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
1033 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
1034 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
1035 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
1036 ASSERT_EQ(ASCIIToUTF16("bar.com"), omnibox_view
->GetText());
1038 // Space after temporary text that looks like a keyword, when the original
1039 // input does not look like a keyword, should trigger keyword mode.
1040 omnibox_view
->SetUserText(base::string16());
1041 const TestHistoryEntry kHistoryFoo
= {
1042 "http://footest.com", "Page footest", 1000, 1000, true
1045 // Add a history entry to trigger HQP matching with text == keyword when
1047 ASSERT_NO_FATAL_FAILURE(
1048 AddHistoryEntry(kHistoryFoo
, Time::Now() - TimeDelta::FromMinutes(10)));
1050 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_F
, 0));
1051 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_O
, 0));
1052 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
1053 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_T
, 0));
1054 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E
, 0));
1055 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1056 ASSERT_TRUE(popup_model
->IsOpen());
1057 base::string16
search_keyword2(ASCIIToUTF16(kSearchKeyword2
));
1058 while ((omnibox_view
->GetText() != search_keyword2
) &&
1059 (popup_model
->selected_line() < popup_model
->result().size() - 1))
1060 omnibox_view
->model()->OnUpOrDownKeyPressed(1);
1061 ASSERT_EQ(search_keyword2
, omnibox_view
->GetText());
1062 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
1063 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
1064 ASSERT_EQ(search_keyword2
, omnibox_view
->model()->keyword());
1065 ASSERT_TRUE(omnibox_view
->GetText().empty());
1068 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, NonSubstitutingKeywordTest
) {
1069 OmniboxView
* omnibox_view
= NULL
;
1070 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1071 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1072 ASSERT_TRUE(popup_model
);
1074 Profile
* profile
= browser()->profile();
1075 TemplateURLService
* template_url_service
=
1076 TemplateURLServiceFactory::GetForProfile(profile
);
1078 // Add a non-default substituting keyword.
1079 TemplateURLData data
;
1080 data
.SetShortName(ASCIIToUTF16("Search abc"));
1081 data
.SetKeyword(ASCIIToUTF16(kSearchText
));
1082 data
.SetURL("http://abc.com/{searchTerms}");
1083 TemplateURL
* template_url
= new TemplateURL(data
);
1084 template_url_service
->Add(template_url
);
1086 omnibox_view
->SetUserText(base::string16());
1088 // Non-default substituting keyword shouldn't be matched by default.
1089 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys
));
1090 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1091 ASSERT_TRUE(popup_model
->IsOpen());
1093 // Check if the default match result is Search Primary Provider.
1094 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
1095 popup_model
->result().default_match()->type
);
1096 ASSERT_EQ(kSearchTextURL
,
1097 popup_model
->result().default_match()->destination_url
.spec());
1099 omnibox_view
->SetUserText(base::string16());
1100 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1101 ASSERT_FALSE(popup_model
->IsOpen());
1103 // Try a non-substituting keyword.
1104 template_url_service
->Remove(template_url
);
1105 data
.SetShortName(ASCIIToUTF16("abc"));
1106 data
.SetURL("http://abc.com/");
1107 template_url_service
->Add(new TemplateURL(data
));
1109 // We always allow exact matches for non-substituting keywords.
1110 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys
));
1111 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1112 ASSERT_TRUE(popup_model
->IsOpen());
1113 ASSERT_EQ(AutocompleteMatchType::HISTORY_KEYWORD
,
1114 popup_model
->result().default_match()->type
);
1115 ASSERT_EQ("http://abc.com/",
1116 popup_model
->result().default_match()->destination_url
.spec());
1119 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, DeleteItem
) {
1120 // Disable the search provider, to make sure the popup contains only history
1122 TemplateURLService
* model
=
1123 TemplateURLServiceFactory::GetForProfile(browser()->profile());
1124 model
->SetUserSelectedDefaultSearchProvider(NULL
);
1126 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL
));
1127 chrome::FocusLocationBar(browser());
1129 OmniboxView
* omnibox_view
= NULL
;
1130 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1132 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1133 ASSERT_TRUE(popup_model
);
1135 base::string16 old_text
= omnibox_view
->GetText();
1137 // Input something that can match history items.
1138 omnibox_view
->SetUserText(ASCIIToUTF16("site.com/p"));
1139 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1140 ASSERT_TRUE(popup_model
->IsOpen());
1142 // Delete the inline autocomplete part.
1143 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE
, 0));
1144 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1145 ASSERT_TRUE(popup_model
->IsOpen());
1146 ASSERT_GE(popup_model
->result().size(), 3U);
1148 base::string16 user_text
= omnibox_view
->GetText();
1149 ASSERT_EQ(ASCIIToUTF16("site.com/p"), user_text
);
1150 omnibox_view
->SelectAll(true);
1151 ASSERT_TRUE(omnibox_view
->IsSelectAll());
1154 size_t default_line
= popup_model
->selected_line();
1155 omnibox_view
->model()->OnUpOrDownKeyPressed(1);
1156 ASSERT_EQ(default_line
+ 1, popup_model
->selected_line());
1157 base::string16 selected_text
=
1158 popup_model
->result().match_at(default_line
+ 1).fill_into_edit
;
1159 // Temporary text is shown.
1160 ASSERT_EQ(selected_text
, omnibox_view
->GetText());
1161 ASSERT_FALSE(omnibox_view
->IsSelectAll());
1164 popup_model
->TryDeletingCurrentItem();
1165 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1166 // The selected line shouldn't be changed, because we have more than two
1168 ASSERT_EQ(default_line
+ 1, popup_model
->selected_line());
1169 // Make sure the item is really deleted.
1170 ASSERT_NE(selected_text
,
1171 popup_model
->result().match_at(default_line
+ 1).fill_into_edit
);
1173 popup_model
->result().match_at(default_line
+ 1).fill_into_edit
;
1174 // New temporary text is shown.
1175 ASSERT_EQ(selected_text
, omnibox_view
->GetText());
1177 // Revert to the default match.
1178 ASSERT_TRUE(omnibox_view
->model()->OnEscapeKeyPressed());
1179 ASSERT_EQ(default_line
, popup_model
->selected_line());
1180 ASSERT_EQ(user_text
, omnibox_view
->GetText());
1181 ASSERT_TRUE(omnibox_view
->IsSelectAll());
1183 // Move down and up to select the default match as temporary text.
1184 omnibox_view
->model()->OnUpOrDownKeyPressed(1);
1185 ASSERT_EQ(default_line
+ 1, popup_model
->selected_line());
1186 omnibox_view
->model()->OnUpOrDownKeyPressed(-1);
1187 ASSERT_EQ(default_line
, popup_model
->selected_line());
1189 selected_text
= popup_model
->result().match_at(default_line
).fill_into_edit
;
1190 // New temporary text is shown.
1191 ASSERT_EQ(selected_text
, omnibox_view
->GetText());
1192 ASSERT_FALSE(omnibox_view
->IsSelectAll());
1195 // TODO(mrossetti): http://crbug.com/82335
1196 // Delete the default item.
1197 popup_model
->TryDeletingCurrentItem();
1198 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1199 // The selected line shouldn't be changed, but the default item should have
1201 ASSERT_EQ(default_line
, popup_model
->selected_line());
1202 // Make sure the item is really deleted.
1203 EXPECT_NE(selected_text
,
1204 popup_model
->result().match_at(default_line
).fill_into_edit
);
1206 popup_model
->result().match_at(default_line
).fill_into_edit
;
1207 // New temporary text is shown.
1208 ASSERT_EQ(selected_text
, omnibox_view
->GetText());
1211 // As the current selected item is the new default item, pressing Escape key
1212 // should revert all directly.
1213 ASSERT_TRUE(omnibox_view
->model()->OnEscapeKeyPressed());
1214 ASSERT_EQ(old_text
, omnibox_view
->GetText());
1215 ASSERT_TRUE(omnibox_view
->IsSelectAll());
1218 // http://crbug.com/133344
1219 #if defined(OS_LINUX)
1220 #define MAYBE_TabAcceptKeyword DISABLED_TabAcceptKeyword
1222 #define MAYBE_TabAcceptKeyword TabAcceptKeyword
1225 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_TabAcceptKeyword
) {
1226 OmniboxView
* omnibox_view
= NULL
;
1227 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1229 base::string16 text
= ASCIIToUTF16(kSearchKeyword
);
1231 // Trigger keyword hint mode.
1232 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys
));
1233 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
1234 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1235 ASSERT_EQ(text
, omnibox_view
->GetText());
1237 // Trigger keyword mode by tab.
1238 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1239 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
1240 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1241 ASSERT_TRUE(omnibox_view
->GetText().empty());
1243 // Revert to keyword hint mode.
1244 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
1245 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
1246 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1247 ASSERT_EQ(text
, omnibox_view
->GetText());
1249 // The location bar should still have focus.
1250 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1252 // Trigger keyword mode by tab.
1253 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1254 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
1255 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1256 ASSERT_TRUE(omnibox_view
->GetText().empty());
1258 // Revert to keyword hint mode with SHIFT+TAB.
1259 #if defined(OS_MACOSX)
1260 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACKTAB
, 0));
1262 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, ui::EF_SHIFT_DOWN
));
1264 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
1265 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1266 ASSERT_EQ(text
, omnibox_view
->GetText());
1267 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1270 #if !defined(OS_MACOSX)
1271 // Mac intentionally does not support this behavior.
1273 // http://crbug.com/133360
1274 #if defined(OS_LINUX)
1275 #define MAYBE_TabTraverseResultsTest DISABLED_TabTraverseResultsTest
1277 #define MAYBE_TabTraverseResultsTest TabTraverseResultsTest
1280 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_TabTraverseResultsTest
) {
1281 OmniboxView
* omnibox_view
= NULL
;
1282 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1283 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1284 ASSERT_TRUE(popup_model
);
1286 // Input something to trigger results.
1287 const ui::KeyboardCode kKeys
[] = {
1288 ui::VKEY_B
, ui::VKEY_A
, ui::VKEY_R
, ui::VKEY_UNKNOWN
1290 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys
));
1291 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1292 ASSERT_TRUE(popup_model
->IsOpen());
1294 size_t old_selected_line
= popup_model
->selected_line();
1295 EXPECT_EQ(0U, old_selected_line
);
1297 // Move down the results.
1298 for (size_t size
= popup_model
->result().size();
1299 popup_model
->selected_line() < size
- 1;
1300 old_selected_line
= popup_model
->selected_line()) {
1301 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1302 ASSERT_LT(old_selected_line
, popup_model
->selected_line());
1305 // Don't move past the end.
1306 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1307 ASSERT_EQ(old_selected_line
, popup_model
->selected_line());
1308 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1310 // Move back up the results.
1311 for (; popup_model
->selected_line() > 0U;
1312 old_selected_line
= popup_model
->selected_line()) {
1313 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, ui::EF_SHIFT_DOWN
));
1314 ASSERT_GT(old_selected_line
, popup_model
->selected_line());
1317 // Don't move past the beginning.
1318 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, ui::EF_SHIFT_DOWN
));
1319 ASSERT_EQ(0U, popup_model
->selected_line());
1320 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1322 const TestHistoryEntry kHistoryFoo
= {
1323 "http://foo/", "Page foo", 1, 1, false
1326 // Add a history entry so "foo" gets multiple matches.
1327 ASSERT_NO_FATAL_FAILURE(
1328 AddHistoryEntry(kHistoryFoo
, Time::Now() - TimeDelta::FromHours(1)));
1331 ASSERT_NO_FATAL_FAILURE(omnibox_view
->SelectAll(false));
1332 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys
));
1333 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1335 // Trigger keyword mode by tab.
1336 base::string16 text
= ASCIIToUTF16(kSearchKeyword
);
1337 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1338 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
1339 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1340 ASSERT_TRUE(omnibox_view
->GetText().empty());
1342 // The location bar should still have focus.
1343 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1345 // Pressing tab again should move to the next result and clear keyword
1347 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1348 ASSERT_EQ(1U, omnibox_view
->model()->popup_model()->selected_line());
1349 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
1350 ASSERT_NE(text
, omnibox_view
->model()->keyword());
1352 // The location bar should still have focus.
1353 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1355 // Moving back up should not show keyword mode.
1356 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, ui::EF_SHIFT_DOWN
));
1357 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
1358 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1360 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1365 // http://crbug.com/133347
1366 #if defined(OS_LINUX)
1367 #define MAYBE_PersistKeywordModeOnTabSwitch \
1368 DISABLED_PersistKeywordModeOnTabSwitch
1370 #define MAYBE_PersistKeywordModeOnTabSwitch PersistKeywordModeOnTabSwitch
1373 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
,
1374 MAYBE_PersistKeywordModeOnTabSwitch
) {
1375 OmniboxView
* omnibox_view
= NULL
;
1376 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1378 // Trigger keyword hint mode.
1379 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys
));
1380 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
1381 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
1383 // Trigger keyword mode.
1384 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1385 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
1386 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
1388 // Input something as search text.
1389 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys
));
1391 // Create a new tab.
1392 chrome::NewTab(browser());
1394 // Switch back to the first tab.
1395 browser()->tab_strip_model()->ActivateTabAt(0, true);
1397 // Make sure we're still in keyword mode.
1398 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
1401 // http://crbug.com/133355
1402 #if defined(OS_LINUX)
1403 #define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest \
1404 DISABLED_CtrlKeyPressedWithInlineAutocompleteTest
1406 #define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest \
1407 CtrlKeyPressedWithInlineAutocompleteTest
1410 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
,
1411 MAYBE_CtrlKeyPressedWithInlineAutocompleteTest
) {
1412 OmniboxView
* omnibox_view
= NULL
;
1413 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1414 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1415 ASSERT_TRUE(popup_model
);
1417 // Input something to trigger inline autocomplete.
1418 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys
));
1419 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1420 ASSERT_TRUE(popup_model
->IsOpen());
1422 base::string16 old_text
= omnibox_view
->GetText();
1424 // Make sure inline autocomplete is triggerred.
1425 EXPECT_GT(old_text
.length(), arraysize(kInlineAutocompleteText
) - 1);
1428 omnibox_view
->model()->OnControlKeyChanged(true);
1430 // Inline autocomplete should still be there.
1431 EXPECT_EQ(old_text
, omnibox_view
->GetText());
1434 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, UndoRedo
) {
1435 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL
));
1436 chrome::FocusLocationBar(browser());
1438 OmniboxView
* omnibox_view
= NULL
;
1439 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1441 base::string16 old_text
= omnibox_view
->GetText();
1442 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL
), old_text
);
1443 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1445 // Delete the text, then undo.
1446 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
1447 EXPECT_TRUE(omnibox_view
->GetText().empty());
1448 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z
, kCtrlOrCmdMask
));
1449 EXPECT_EQ(old_text
, omnibox_view
->GetText());
1451 // Redo should delete the text again.
1452 ASSERT_NO_FATAL_FAILURE(
1453 SendKey(ui::VKEY_Z
, kCtrlOrCmdMask
| ui::EF_SHIFT_DOWN
));
1454 EXPECT_TRUE(omnibox_view
->GetText().empty());
1456 // The toolkit-views undo manager doesn't support restoring selection. Cocoa
1457 // does, so it needs to be cleared.
1458 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z
, kCtrlOrCmdMask
));
1459 #if defined(OS_MACOSX)
1460 // TODO(tapted): This next line may fail if running a toolkit-views browser
1461 // window on Mac. We should fix the toolkit-views undo manager to restore
1462 // selection rather than deleting this #ifdef.
1463 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1464 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT
, 0));
1466 EXPECT_FALSE(omnibox_view
->IsSelectAll());
1468 // The cursor should be at the end.
1470 omnibox_view
->GetSelectionBounds(&start
, &end
);
1471 EXPECT_EQ(old_text
.size(), start
);
1472 EXPECT_EQ(old_text
.size(), end
);
1474 // Delete three characters; "about:bl" should not trigger inline autocomplete.
1475 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
1476 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
1477 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
1478 EXPECT_EQ(old_text
.substr(0, old_text
.size() - 3), omnibox_view
->GetText());
1481 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z
, kCtrlOrCmdMask
));
1482 EXPECT_EQ(old_text
, omnibox_view
->GetText());
1485 ASSERT_NO_FATAL_FAILURE(
1486 SendKey(ui::VKEY_Z
, kCtrlOrCmdMask
| ui::EF_SHIFT_DOWN
));
1487 EXPECT_EQ(old_text
.substr(0, old_text
.size() - 3), omnibox_view
->GetText());
1489 // Delete everything.
1490 omnibox_view
->SelectAll(true);
1491 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
1492 EXPECT_TRUE(omnibox_view
->GetText().empty());
1494 // Undo delete everything.
1495 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z
, kCtrlOrCmdMask
));
1496 EXPECT_EQ(old_text
.substr(0, old_text
.size() - 3), omnibox_view
->GetText());
1498 // Undo delete two characters.
1499 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z
, kCtrlOrCmdMask
));
1500 EXPECT_EQ(old_text
, omnibox_view
->GetText());
1503 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, BackspaceDeleteHalfWidthKatakana
) {
1504 OmniboxView
* omnibox_view
= NULL
;
1505 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1506 // Insert text: ダ. This is two, 3-byte UTF-8 characters:
1507 // U+FF80 "HALFWIDTH KATAKANA LETTER TA" and
1508 // U+FF9E "HALFWIDTH KATAKANA VOICED SOUND MARK".
1509 omnibox_view
->SetUserText(base::UTF8ToUTF16("\357\276\200\357\276\236"));
1510 EXPECT_FALSE(omnibox_view
->GetText().empty());
1512 // Move the cursor to the end.
1513 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END
, 0));
1515 // Backspace should delete the character. In http://crbug.com/192743, the bug
1516 // was that nothing was deleted.
1517 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
1518 #if defined(OS_MACOSX)
1519 // Cocoa text fields attach the sound mark and delete the whole thing. This
1520 // behavior should remain on Mac even when using a toolkit-views browser
1522 EXPECT_TRUE(omnibox_view
->GetText().empty());
1524 // Toolkit-views text fields delete just the sound mark.
1525 EXPECT_EQ(base::UTF8ToUTF16("\357\276\200"), omnibox_view
->GetText());
1529 // Flaky test. crbug.com/356850
1530 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
,
1531 DISABLED_DoesNotUpdateAutocompleteOnBlur
) {
1532 OmniboxView
* omnibox_view
= NULL
;
1533 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1534 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1535 ASSERT_TRUE(popup_model
);
1537 // Input something to trigger inline autocomplete.
1538 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys
));
1539 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1540 ASSERT_TRUE(popup_model
->IsOpen());
1542 omnibox_view
->GetSelectionBounds(&start
, &end
);
1543 EXPECT_TRUE(start
!= end
);
1544 base::string16 old_autocomplete_text
=
1545 omnibox_view
->model()->autocomplete_controller()->input_
.text();
1547 // Unfocus the omnibox. This should clear the text field selection and
1548 // close the popup, but should not run autocomplete.
1549 // Note: GTK preserves the selection when the omnibox is unfocused.
1550 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER
);
1551 ASSERT_FALSE(popup_model
->IsOpen());
1552 omnibox_view
->GetSelectionBounds(&start
, &end
);
1553 EXPECT_TRUE(start
== end
);
1555 EXPECT_EQ(old_autocomplete_text
,
1556 omnibox_view
->model()->autocomplete_controller()->input_
.text());
1559 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, Paste
) {
1560 OmniboxView
* omnibox_view
= NULL
;
1561 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1562 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1563 ASSERT_TRUE(popup_model
);
1564 EXPECT_FALSE(popup_model
->IsOpen());
1566 // Paste should yield the expected text and open the popup.
1567 SetClipboardText(ASCIIToUTF16(kSearchText
));
1568 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V
, kCtrlOrCmdMask
));
1569 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1570 EXPECT_EQ(ASCIIToUTF16(kSearchText
), omnibox_view
->GetText());
1571 EXPECT_TRUE(popup_model
->IsOpen());
1573 // Close the popup and select all.
1574 omnibox_view
->CloseOmniboxPopup();
1575 omnibox_view
->SelectAll(false);
1576 EXPECT_FALSE(popup_model
->IsOpen());
1578 // Pasting the same text again over itself should re-open the popup.
1579 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V
, kCtrlOrCmdMask
));
1580 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1581 EXPECT_EQ(ASCIIToUTF16(kSearchText
), omnibox_view
->GetText());
1582 EXPECT_TRUE(popup_model
->IsOpen());
1583 omnibox_view
->CloseOmniboxPopup();
1584 EXPECT_FALSE(popup_model
->IsOpen());
1586 // Pasting amid text should yield the expected text and re-open the popup.
1587 omnibox_view
->SetWindowTextAndCaretPos(ASCIIToUTF16("abcd"), 2, false, false);
1588 SetClipboardText(ASCIIToUTF16("123"));
1589 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V
, kCtrlOrCmdMask
));
1590 EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view
->GetText());
1591 EXPECT_TRUE(popup_model
->IsOpen());
1593 // Ctrl/Cmd+Alt+V should not paste.
1594 ASSERT_NO_FATAL_FAILURE(
1595 SendKey(ui::VKEY_V
, kCtrlOrCmdMask
| ui::EF_ALT_DOWN
));
1596 EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view
->GetText());
1597 // TODO(msw): Test that AltGr+V does not paste.
1600 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, CopyURLToClipboard
) {
1601 // Set permanent text thus making sure that omnibox treats 'google.com'
1602 // as URL (not as ordinary user input).
1603 TestToolbarModel
* test_toolbar_model
= new TestToolbarModel
;
1604 scoped_ptr
<ToolbarModel
> toolbar_model(test_toolbar_model
);
1605 test_toolbar_model
->set_text(ASCIIToUTF16("http://www.google.com/"));
1606 browser()->swap_toolbar_models(&toolbar_model
);
1607 OmniboxView
* omnibox_view
= NULL
;
1608 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1609 OmniboxEditModel
* edit_model
= omnibox_view
->model();
1610 ASSERT_NE(static_cast<OmniboxEditModel
*>(NULL
), edit_model
);
1611 edit_model
->UpdatePermanentText();
1613 const char* target_url
= "http://www.google.com/calendar";
1614 omnibox_view
->SetUserText(ASCIIToUTF16(target_url
));
1616 // Location bar must have focus.
1617 chrome::FocusLocationBar(browser());
1618 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1620 // Select full URL and copy it to clipboard. General text and html should
1622 omnibox_view
->SelectAll(true);
1623 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1624 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1625 clipboard
->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE
);
1626 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY
));
1627 EXPECT_EQ(ASCIIToUTF16(target_url
), omnibox_view
->GetText());
1628 EXPECT_TRUE(clipboard
->IsFormatAvailable(
1629 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1631 // Make sure HTML format isn't written. See
1632 // BookmarkNodeData::WriteToClipboard() for details.
1633 EXPECT_FALSE(clipboard
->IsFormatAvailable(
1634 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1636 // These platforms should read bookmark format.
1637 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1638 base::string16 title
;
1640 clipboard
->ReadBookmark(&title
, &url
);
1641 EXPECT_EQ(target_url
, url
);
1642 EXPECT_EQ(ASCIIToUTF16(target_url
), title
);
1646 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, CutURLToClipboard
) {
1647 // Set permanent text thus making sure that omnibox treats 'google.com'
1648 // as URL (not as ordinary user input).
1649 TestToolbarModel
* test_toolbar_model
= new TestToolbarModel
;
1650 scoped_ptr
<ToolbarModel
> toolbar_model(test_toolbar_model
);
1651 test_toolbar_model
->set_text(ASCIIToUTF16("http://www.google.com/"));
1652 browser()->swap_toolbar_models(&toolbar_model
);
1653 OmniboxView
* omnibox_view
= NULL
;
1654 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1655 OmniboxEditModel
* edit_model
= omnibox_view
->model();
1656 ASSERT_NE(static_cast<OmniboxEditModel
*>(NULL
), edit_model
);
1657 edit_model
->UpdatePermanentText();
1659 const char* target_url
= "http://www.google.com/calendar";
1660 omnibox_view
->SetUserText(ASCIIToUTF16(target_url
));
1662 // Location bar must have focus.
1663 chrome::FocusLocationBar(browser());
1664 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1666 // Select full URL and cut it. General text and html should be available
1667 // in the clipboard.
1668 omnibox_view
->SelectAll(true);
1669 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1670 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1671 clipboard
->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE
);
1672 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT
));
1673 EXPECT_EQ(base::string16(), omnibox_view
->GetText());
1674 EXPECT_TRUE(clipboard
->IsFormatAvailable(
1675 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1677 // Make sure HTML format isn't written. See
1678 // BookmarkNodeData::WriteToClipboard() for details.
1679 EXPECT_FALSE(clipboard
->IsFormatAvailable(
1680 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1682 // These platforms should read bookmark format.
1683 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1684 base::string16 title
;
1686 clipboard
->ReadBookmark(&title
, &url
);
1687 EXPECT_EQ(target_url
, url
);
1688 EXPECT_EQ(ASCIIToUTF16(target_url
), title
);
1692 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, CopyTextToClipboard
) {
1693 OmniboxView
* omnibox_view
= NULL
;
1694 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1695 const char* target_text
= "foo";
1696 omnibox_view
->SetUserText(ASCIIToUTF16(target_text
));
1698 // Location bar must have focus.
1699 chrome::FocusLocationBar(browser());
1700 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1702 // Select full text and copy it to the clipboard.
1703 omnibox_view
->SelectAll(true);
1704 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1705 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1706 clipboard
->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE
);
1707 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY
));
1708 EXPECT_TRUE(clipboard
->IsFormatAvailable(
1709 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1710 EXPECT_FALSE(clipboard
->IsFormatAvailable(
1711 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1712 EXPECT_EQ(ASCIIToUTF16(target_text
), omnibox_view
->GetText());
1715 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, CutTextToClipboard
) {
1716 OmniboxView
* omnibox_view
= NULL
;
1717 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1718 const char* target_text
= "foo";
1719 omnibox_view
->SetUserText(ASCIIToUTF16(target_text
));
1721 // Location bar must have focus.
1722 chrome::FocusLocationBar(browser());
1723 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1725 // Select full text and cut it to the clipboard.
1726 omnibox_view
->SelectAll(true);
1727 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1728 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1729 clipboard
->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE
);
1730 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT
));
1731 EXPECT_TRUE(clipboard
->IsFormatAvailable(
1732 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1733 EXPECT_FALSE(clipboard
->IsFormatAvailable(
1734 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1735 EXPECT_EQ(base::string16(), omnibox_view
->GetText());
1738 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, EditSearchEngines
) {
1739 // Disable settings-in-a-window to simplify test.
1740 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1741 ::switches::kDisableSettingsWindow
);
1742 OmniboxView
* omnibox_view
= NULL
;
1743 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1744 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_EDIT_SEARCH_ENGINES
));
1745 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1746 const std::string target_url
=
1747 std::string(chrome::kChromeUISettingsURL
) + chrome::kSearchEnginesSubPage
;
1748 EXPECT_EQ(ASCIIToUTF16(target_url
), omnibox_view
->GetText());
1749 EXPECT_FALSE(omnibox_view
->model()->popup_model()->IsOpen());
1752 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, BeginningShownAfterBlur
) {
1753 OmniboxView
* omnibox_view
= NULL
;
1754 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1756 omnibox_view
->OnBeforePossibleChange();
1757 omnibox_view
->SetWindowTextAndCaretPos(ASCIIToUTF16("data:text/plain,test"),
1759 omnibox_view
->OnAfterPossibleChange();
1760 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1762 omnibox_view
->GetSelectionBounds(&start
, &end
);
1763 EXPECT_EQ(5U, start
);
1766 ui_test_utils::FocusView(browser(), VIEW_ID_TAB_CONTAINER
);
1767 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1769 omnibox_view
->GetSelectionBounds(&start
, &end
);
1770 EXPECT_EQ(0U, start
);
1774 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, CtrlArrowAfterArrowSuggestions
) {
1775 OmniboxView
* omnibox_view
= NULL
;
1776 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1777 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1778 ASSERT_TRUE(popup_model
);
1780 // Input something to trigger results.
1781 const ui::KeyboardCode kKeys
[] = {
1782 ui::VKEY_B
, ui::VKEY_A
, ui::VKEY_R
, ui::VKEY_UNKNOWN
1784 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys
));
1785 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1786 ASSERT_TRUE(popup_model
->IsOpen());
1788 ASSERT_EQ(ASCIIToUTF16("bar.com/1"), omnibox_view
->GetText());
1790 // Arrow down on a suggestion, and omnibox text should be the suggestion.
1791 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN
, 0));
1792 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1793 ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view
->GetText());
1795 // Highlight the last 2 words and the omnibox text should not change.
1796 // Simulating Ctrl-shift-left only once does not seem to highlight anything
1798 #if defined(OS_MACOSX)
1799 // Mac uses alt-left/right to select a word.
1800 const int modifiers
= ui::EF_SHIFT_DOWN
| ui::EF_ALT_DOWN
;
1802 const int modifiers
= ui::EF_SHIFT_DOWN
| ui::EF_CONTROL_DOWN
;
1804 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, modifiers
));
1805 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, modifiers
));
1806 ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view
->GetText());
1809 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
,
1810 PersistSearchReplacementAcrossTabSwitch
) {
1811 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
1812 browser()->toolbar_model()->set_url_replacement_enabled(false);
1814 // Create a new tab.
1815 chrome::NewTab(browser());
1816 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
1818 // Switch back to the first tab.
1819 browser()->tab_strip_model()->ActivateTabAt(0, true);
1820 EXPECT_FALSE(browser()->toolbar_model()->url_replacement_enabled());
1823 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
,
1824 DontUpdateURLWhileSearchTermReplacementIsDisabled
) {
1825 OmniboxView
* omnibox_view
= NULL
;
1826 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1827 TestToolbarModel
* test_toolbar_model
= new TestToolbarModel
;
1828 scoped_ptr
<ToolbarModel
> toolbar_model(test_toolbar_model
);
1829 browser()->swap_toolbar_models(&toolbar_model
);
1831 base::string16
url_a(ASCIIToUTF16("http://www.a.com/"));
1832 base::string16
url_b(ASCIIToUTF16("http://www.b.com/"));
1833 base::string16
url_c(ASCIIToUTF16("http://www.c.com/"));
1834 chrome::FocusLocationBar(browser());
1835 test_toolbar_model
->set_text(url_a
);
1836 omnibox_view
->Update();
1837 EXPECT_EQ(url_a
, omnibox_view
->GetText());
1839 // Disable URL replacement and update. Because the omnibox has focus, the
1840 // visible text shouldn't change; see comments in
1841 // OmniboxEditModel::UpdatePermanentText().
1842 browser()->toolbar_model()->set_url_replacement_enabled(false);
1843 test_toolbar_model
->set_text(url_b
);
1844 omnibox_view
->Update();
1845 EXPECT_EQ(url_a
, omnibox_view
->GetText());
1847 // Re-enable URL replacement and ensure updating changes the text.
1848 browser()->toolbar_model()->set_url_replacement_enabled(true);
1849 // We have to change the toolbar model text here, or Update() will do nothing.
1850 // This is because the previous update already updated the permanent text.
1851 test_toolbar_model
->set_text(url_c
);
1852 omnibox_view
->Update();
1853 EXPECT_EQ(url_c
, omnibox_view
->GetText());
1855 // The same test, but using RevertAll() to reset search term replacement.
1856 test_toolbar_model
->set_text(url_a
);
1857 omnibox_view
->Update();
1858 EXPECT_EQ(url_a
, omnibox_view
->GetText());
1859 browser()->toolbar_model()->set_url_replacement_enabled(false);
1860 test_toolbar_model
->set_text(url_b
);
1861 omnibox_view
->Update();
1862 EXPECT_EQ(url_a
, omnibox_view
->GetText());
1863 omnibox_view
->RevertAll();
1864 EXPECT_EQ(url_b
, omnibox_view
->GetText());
1865 test_toolbar_model
->set_text(url_c
);
1866 omnibox_view
->Update();
1867 EXPECT_EQ(url_c
, omnibox_view
->GetText());
1872 // Returns the number of characters currently selected in |omnibox_view|.
1873 size_t GetSelectionSize(OmniboxView
* omnibox_view
) {
1875 omnibox_view
->GetSelectionBounds(&start
, &end
);
1883 // Test that if the Omnibox has focus, and had everything selected before a
1884 // non-user-initiated update, then it retains the selection after the update.
1885 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, SelectAllStaysAfterUpdate
) {
1886 OmniboxView
* omnibox_view
= nullptr;
1887 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1888 TestToolbarModel
* test_toolbar_model
= new TestToolbarModel
;
1889 scoped_ptr
<ToolbarModel
> toolbar_model(test_toolbar_model
);
1890 browser()->swap_toolbar_models(&toolbar_model
);
1892 base::string16
url_a(ASCIIToUTF16("http://www.a.com/"));
1893 base::string16
url_b(ASCIIToUTF16("http://www.b.com/"));
1894 chrome::FocusLocationBar(browser());
1896 test_toolbar_model
->set_text(url_a
);
1897 omnibox_view
->Update();
1898 EXPECT_EQ(url_a
, omnibox_view
->GetText());
1899 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1901 // Updating while selected should retain SelectAll().
1902 test_toolbar_model
->set_text(url_b
);
1903 omnibox_view
->Update();
1904 EXPECT_EQ(url_b
, omnibox_view
->GetText());
1905 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1907 // Select nothing, then switch back. Shouldn't gain a selection.
1908 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT
, 0));
1909 test_toolbar_model
->set_text(url_a
);
1910 omnibox_view
->Update();
1911 EXPECT_EQ(url_a
, omnibox_view
->GetText());
1912 EXPECT_FALSE(omnibox_view
->IsSelectAll());
1914 // Test behavior of the "reversed" attribute of OmniboxView::SelectAll().
1915 test_toolbar_model
->set_text(ASCIIToUTF16("AB"));
1916 omnibox_view
->Update();
1917 // Should be at the end already. Shift+Left to select "reversed".
1918 EXPECT_EQ(0u, GetSelectionSize(omnibox_view
));
1919 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, ui::EF_SHIFT_DOWN
));
1920 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, ui::EF_SHIFT_DOWN
));
1921 EXPECT_EQ(2u, GetSelectionSize(omnibox_view
));
1922 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1924 test_toolbar_model
->set_text(ASCIIToUTF16("CD"));
1925 omnibox_view
->Update();
1926 EXPECT_EQ(2u, GetSelectionSize(omnibox_view
));
1928 // At the start, so Shift+Left should do nothing.
1929 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, ui::EF_SHIFT_DOWN
));
1930 EXPECT_EQ(2u, GetSelectionSize(omnibox_view
));
1932 // And Shift+Right should reduce by one character.
1933 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT
, ui::EF_SHIFT_DOWN
));
1934 EXPECT_EQ(1u, GetSelectionSize(omnibox_view
));
1936 // No go to start and select all to the right (not reversed).
1937 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
1938 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
1939 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT
, ui::EF_SHIFT_DOWN
));
1940 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT
, ui::EF_SHIFT_DOWN
));
1941 test_toolbar_model
->set_text(ASCIIToUTF16("AB"));
1942 omnibox_view
->Update();
1943 EXPECT_EQ(2u, GetSelectionSize(omnibox_view
));
1945 // Now Shift+Right should do nothing, and Shift+Left should reduce.
1946 // At the end, so Shift+Right should do nothing.
1947 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT
, ui::EF_SHIFT_DOWN
));
1948 EXPECT_EQ(2u, GetSelectionSize(omnibox_view
));
1950 // And Left should reduce by one character.
1951 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, ui::EF_SHIFT_DOWN
));
1952 EXPECT_EQ(1u, GetSelectionSize(omnibox_view
));