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/strings/string16.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/autocomplete/autocomplete_input.h"
13 #include "chrome/browser/autocomplete/autocomplete_match.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.h"
18 #include "chrome/browser/history/history_service_factory.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/search_engines/template_url.h"
21 #include "chrome/browser/search_engines/template_url_service.h"
22 #include "chrome/browser/search_engines/template_url_service_factory.h"
23 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/browser_commands.h"
25 #include "chrome/browser/ui/browser_window.h"
26 #include "chrome/browser/ui/omnibox/location_bar.h"
27 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
28 #include "chrome/browser/ui/omnibox/omnibox_view.h"
29 #include "chrome/browser/ui/tabs/tab_strip_model.h"
30 #include "chrome/browser/ui/toolbar/test_toolbar_model.h"
31 #include "chrome/common/chrome_paths.h"
32 #include "chrome/common/url_constants.h"
33 #include "chrome/test/base/in_process_browser_test.h"
34 #include "chrome/test/base/interactive_test_utils.h"
35 #include "chrome/test/base/ui_test_utils.h"
36 #include "components/bookmarks/core/browser/bookmark_model.h"
37 #include "components/bookmarks/core/browser/bookmark_utils.h"
38 #include "components/bookmarks/core/test/bookmark_test_helpers.h"
39 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/web_contents.h"
41 #include "net/dns/mock_host_resolver.h"
42 #include "ui/base/clipboard/clipboard.h"
43 #include "ui/base/clipboard/scoped_clipboard_writer.h"
44 #include "ui/events/event_constants.h"
45 #include "ui/events/keycodes/keyboard_codes.h"
46 #include "ui/gfx/point.h"
48 using base::ASCIIToUTF16
;
49 using base::UTF16ToUTF8
;
51 using base::TimeDelta
;
55 const char kSearchKeyword
[] = "foo";
56 const char kSearchKeyword2
[] = "footest.com";
57 const wchar_t kSearchKeywordKeys
[] = { ui::VKEY_F
, ui::VKEY_O
, ui::VKEY_O
, 0 };
58 const char kSearchURL
[] = "http://www.foo.com/search?q={searchTerms}";
59 const char kSearchShortName
[] = "foo";
60 const char kSearchText
[] = "abc";
61 const wchar_t kSearchTextKeys
[] = { ui::VKEY_A
, ui::VKEY_B
, ui::VKEY_C
, 0 };
62 const char kSearchTextURL
[] = "http://www.foo.com/search?q=abc";
64 const char kInlineAutocompleteText
[] = "def";
65 const wchar_t kInlineAutocompleteTextKeys
[] = {
66 ui::VKEY_D
, ui::VKEY_E
, ui::VKEY_F
, 0
69 // Hostnames that shall be blocked by host resolver.
70 const char *kBlockedHostnames
[] = {
84 const struct TestHistoryEntry
{
90 } kHistoryEntries
[] = {
91 {"http://www.bar.com/1", "Page 1", 10, 10, false },
92 {"http://www.bar.com/2", "Page 2", 9, 9, false },
93 {"http://www.bar.com/3", "Page 3", 8, 8, false },
94 {"http://www.bar.com/4", "Page 4", 7, 7, false },
95 {"http://www.bar.com/5", "Page 5", 6, 6, false },
96 {"http://www.bar.com/6", "Page 6", 5, 5, false },
97 {"http://www.bar.com/7", "Page 7", 4, 4, false },
98 {"http://www.bar.com/8", "Page 8", 3, 3, false },
99 {"http://www.bar.com/9", "Page 9", 2, 2, false },
100 {"http://www.site.com/path/1", "Site 1", 4, 4, false },
101 {"http://www.site.com/path/2", "Site 2", 3, 3, false },
102 {"http://www.site.com/path/3", "Site 3", 2, 2, false },
104 // To trigger inline autocomplete.
105 {"http://www.def.com", "Page def", 10000, 10000, true },
107 // Used in particular for the desired TLD test. This makes it test
108 // the interesting case when there's an intranet host with the same
110 {"http://bar/", "Bar", 1, 0, false },
113 // Stores the given text to clipboard.
114 void SetClipboardText(const base::string16
& text
) {
115 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
116 ui::ScopedClipboardWriter
writer(clipboard
, ui::CLIPBOARD_TYPE_COPY_PASTE
);
117 writer
.WriteText(text
);
120 #if defined(OS_MACOSX)
121 const int kCtrlOrCmdMask
= ui::EF_COMMAND_DOWN
;
123 const int kCtrlOrCmdMask
= ui::EF_CONTROL_DOWN
;
128 class OmniboxViewTest
: public InProcessBrowserTest
,
129 public content::NotificationObserver
{
131 virtual void SetUpOnMainThread() OVERRIDE
{
132 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
133 ASSERT_NO_FATAL_FAILURE(SetupComponents());
134 chrome::FocusLocationBar(browser());
135 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
138 static void GetOmniboxViewForBrowser(
139 const Browser
* browser
,
140 OmniboxView
** omnibox_view
) {
141 BrowserWindow
* window
= browser
->window();
143 LocationBar
* location_bar
= window
->GetLocationBar();
144 ASSERT_TRUE(location_bar
);
145 *omnibox_view
= location_bar
->GetOmniboxView();
146 ASSERT_TRUE(*omnibox_view
);
149 void GetOmniboxView(OmniboxView
** omnibox_view
) {
150 GetOmniboxViewForBrowser(browser(), omnibox_view
);
153 static void SendKeyForBrowser(const Browser
* browser
,
154 ui::KeyboardCode key
,
156 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
158 (modifiers
& ui::EF_CONTROL_DOWN
) != 0,
159 (modifiers
& ui::EF_SHIFT_DOWN
) != 0,
160 (modifiers
& ui::EF_ALT_DOWN
) != 0,
161 (modifiers
& ui::EF_COMMAND_DOWN
) != 0));
164 void SendKey(ui::KeyboardCode key
, int modifiers
) {
165 SendKeyForBrowser(browser(), key
, modifiers
);
168 void SendKeySequence(const wchar_t* keys
) {
169 for (; *keys
; ++keys
)
170 ASSERT_NO_FATAL_FAILURE(SendKey(static_cast<ui::KeyboardCode
>(*keys
), 0));
173 bool SendKeyAndWait(const Browser
* browser
,
174 ui::KeyboardCode key
,
177 const content::NotificationSource
& source
)
179 return ui_test_utils::SendKeyPressAndWait(
181 (modifiers
& ui::EF_CONTROL_DOWN
) != 0,
182 (modifiers
& ui::EF_SHIFT_DOWN
) != 0,
183 (modifiers
& ui::EF_ALT_DOWN
) != 0,
184 (modifiers
& ui::EF_COMMAND_DOWN
) != 0,
188 void WaitForTabOpenOrCloseForBrowser(const Browser
* browser
,
189 int expected_tab_count
) {
190 int tab_count
= browser
->tab_strip_model()->count();
191 if (tab_count
== expected_tab_count
)
194 content::NotificationRegistrar registrar
;
196 (tab_count
< expected_tab_count
) ?
197 static_cast<int>(chrome::NOTIFICATION_TAB_PARENTED
) :
198 static_cast<int>(content::NOTIFICATION_WEB_CONTENTS_DESTROYED
),
199 content::NotificationService::AllSources());
201 while (!HasFailure() &&
202 browser
->tab_strip_model()->count() != expected_tab_count
) {
203 content::RunMessageLoop();
206 ASSERT_EQ(expected_tab_count
, browser
->tab_strip_model()->count());
209 void WaitForTabOpenOrClose(int expected_tab_count
) {
210 WaitForTabOpenOrCloseForBrowser(browser(), expected_tab_count
);
213 void WaitForAutocompleteControllerDone() {
214 OmniboxView
* omnibox_view
= NULL
;
215 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
217 AutocompleteController
* controller
=
218 omnibox_view
->model()->autocomplete_controller();
219 ASSERT_TRUE(controller
);
221 if (controller
->done())
224 content::NotificationRegistrar registrar
;
226 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
227 content::Source
<AutocompleteController
>(controller
));
229 while (!HasFailure() && !controller
->done())
230 content::RunMessageLoop();
232 ASSERT_TRUE(controller
->done());
235 void SetupSearchEngine() {
236 Profile
* profile
= browser()->profile();
237 TemplateURLService
* model
=
238 TemplateURLServiceFactory::GetForProfile(profile
);
241 ui_test_utils::WaitForTemplateURLServiceToLoad(model
);
243 ASSERT_TRUE(model
->loaded());
244 // Remove built-in template urls, like google.com, bing.com etc., as they
245 // may appear as autocomplete suggests and interfere with our tests.
246 model
->SetUserSelectedDefaultSearchProvider(NULL
);
247 TemplateURLService::TemplateURLVector builtins
= model
->GetTemplateURLs();
248 for (TemplateURLService::TemplateURLVector::const_iterator
249 i
= builtins
.begin(); i
!= builtins
.end(); ++i
)
252 TemplateURLData data
;
253 data
.short_name
= ASCIIToUTF16(kSearchShortName
);
254 data
.SetKeyword(ASCIIToUTF16(kSearchKeyword
));
255 data
.SetURL(kSearchURL
);
256 TemplateURL
* template_url
= new TemplateURL(profile
, data
);
257 model
->Add(template_url
);
258 model
->SetUserSelectedDefaultSearchProvider(template_url
);
260 data
.SetKeyword(ASCIIToUTF16(kSearchKeyword2
));
261 model
->Add(new TemplateURL(profile
, data
));
264 void AddHistoryEntry(const TestHistoryEntry
& entry
, const Time
& time
) {
265 Profile
* profile
= browser()->profile();
266 HistoryService
* history_service
= HistoryServiceFactory::GetForProfile(
267 profile
, Profile::EXPLICIT_ACCESS
);
268 ASSERT_TRUE(history_service
);
270 if (!history_service
->BackendLoaded()) {
271 content::NotificationRegistrar registrar
;
272 registrar
.Add(this, chrome::NOTIFICATION_HISTORY_LOADED
,
273 content::Source
<Profile
>(profile
));
274 content::RunMessageLoop();
277 BookmarkModel
* bookmark_model
=
278 BookmarkModelFactory::GetForProfile(profile
);
279 ASSERT_TRUE(bookmark_model
);
280 test::WaitForBookmarkModelToLoad(bookmark_model
);
283 // Add everything in order of time. We don't want to have a time that
284 // is "right now" or it will nondeterministically appear in the results.
285 history_service
->AddPageWithDetails(url
, base::UTF8ToUTF16(entry
.title
),
287 entry
.typed_count
, time
, false,
288 history::SOURCE_BROWSED
);
290 bookmark_utils::AddIfNotBookmarked(bookmark_model
, url
, base::string16());
291 // Wait at least for the AddPageWithDetails() call to finish.
293 content::NotificationRegistrar registrar
;
294 registrar
.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED
,
295 content::Source
<Profile
>(profile
));
296 content::RunMessageLoop();
297 // We don't want to return until all observers have processed this
298 // notification, because some (e.g. the in-memory history database) may do
299 // something important. Since we don't know where in the observer list we
300 // stand, just spin the message loop once more to allow the current
301 // callstack to complete.
302 content::RunAllPendingInMessageLoop();
306 void SetupHistory() {
307 // Add enough history pages containing |kSearchText| to trigger
308 // open history page url in autocomplete result.
309 for (size_t i
= 0; i
< arraysize(kHistoryEntries
); i
++) {
310 // Add everything in order of time. We don't want to have a time that
311 // is "right now" or it will nondeterministically appear in the results.
312 Time t
= Time::Now() - TimeDelta::FromHours(i
+ 1);
313 ASSERT_NO_FATAL_FAILURE(AddHistoryEntry(kHistoryEntries
[i
], t
));
317 void SetupHostResolver() {
318 for (size_t i
= 0; i
< arraysize(kBlockedHostnames
); ++i
)
319 host_resolver()->AddSimulatedFailure(kBlockedHostnames
[i
]);
322 void SetupComponents() {
323 ASSERT_NO_FATAL_FAILURE(SetupHostResolver());
324 ASSERT_NO_FATAL_FAILURE(SetupSearchEngine());
325 ASSERT_NO_FATAL_FAILURE(SetupHistory());
328 virtual void Observe(int type
,
329 const content::NotificationSource
& source
,
330 const content::NotificationDetails
& details
) OVERRIDE
{
332 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED
:
333 case chrome::NOTIFICATION_TAB_PARENTED
:
334 case chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
:
335 case chrome::NOTIFICATION_HISTORY_LOADED
:
336 case chrome::NOTIFICATION_HISTORY_URLS_MODIFIED
:
339 FAIL() << "Unexpected notification type";
341 base::MessageLoop::current()->Quit();
345 // Test if ctrl-* accelerators are workable in omnibox.
346 // See http://crbug.com/19193: omnibox blocks ctrl-* commands
348 // Flaky on interactive tests (dbg), http://crbug.com/69433
349 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, DISABLED_BrowserAccelerators
) {
350 OmniboxView
* omnibox_view
= NULL
;
351 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
353 int tab_count
= browser()->tab_strip_model()->count();
356 chrome::NewTab(browser());
357 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count
+ 1));
359 // Select the first Tab.
360 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_1
, kCtrlOrCmdMask
));
361 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
363 chrome::FocusLocationBar(browser());
365 // Select the second Tab.
366 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_2
, kCtrlOrCmdMask
));
367 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
369 chrome::FocusLocationBar(browser());
371 // Try ctrl-w to close a Tab.
372 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_W
, kCtrlOrCmdMask
));
373 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count
));
375 // Try ctrl-l to focus location bar.
376 omnibox_view
->SetUserText(ASCIIToUTF16("Hello world"));
377 EXPECT_FALSE(omnibox_view
->IsSelectAll());
378 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_L
, kCtrlOrCmdMask
));
379 EXPECT_TRUE(omnibox_view
->IsSelectAll());
381 // Try editing the location bar text.
382 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT
, 0));
383 EXPECT_FALSE(omnibox_view
->IsSelectAll());
384 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_S
, 0));
385 EXPECT_EQ(ASCIIToUTF16("Hello worlds"), omnibox_view
->GetText());
387 // Try ctrl-x to cut text.
388 #if defined(OS_MACOSX)
389 // Mac uses alt-left/right to select a word.
390 ASSERT_NO_FATAL_FAILURE(
391 SendKey(ui::VKEY_LEFT
, ui::EF_SHIFT_DOWN
| ui::EF_ALT_DOWN
));
393 ASSERT_NO_FATAL_FAILURE(
394 SendKey(ui::VKEY_LEFT
, ui::EF_SHIFT_DOWN
| ui::EF_CONTROL_DOWN
));
396 EXPECT_FALSE(omnibox_view
->IsSelectAll());
397 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_X
, kCtrlOrCmdMask
));
398 EXPECT_EQ(ASCIIToUTF16("Hello "), omnibox_view
->GetText());
400 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
401 // Try alt-f4 to close the browser.
402 ASSERT_TRUE(SendKeyAndWait(
403 browser(), ui::VKEY_F4
, ui::EF_ALT_DOWN
,
404 chrome::NOTIFICATION_BROWSER_CLOSED
,
405 content::Source
<Browser
>(browser())));
409 // Flakily fails and times out on Win only. http://crbug.com/69941
411 #define MAYBE_PopupAccelerators DISABLED_PopupAccelerators
413 #define MAYBE_PopupAccelerators PopupAccelerators
416 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_PopupAccelerators
) {
418 Browser
* popup
= CreateBrowserForPopup(browser()->profile());
419 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup
));
420 OmniboxView
* omnibox_view
= NULL
;
421 ASSERT_NO_FATAL_FAILURE(
422 GetOmniboxViewForBrowser(popup
, &omnibox_view
));
423 chrome::FocusLocationBar(popup
);
424 EXPECT_TRUE(omnibox_view
->IsSelectAll());
426 #if !defined(OS_MACOSX)
427 // Try ctrl-w to close the popup.
428 // This piece of code doesn't work on Mac, because the Browser object won't
429 // be destroyed before finishing the current message loop iteration, thus
430 // No BROWSER_CLOSED notification will be sent.
431 ASSERT_TRUE(SendKeyAndWait(
432 popup
, ui::VKEY_W
, ui::EF_CONTROL_DOWN
,
433 chrome::NOTIFICATION_BROWSER_CLOSED
, content::Source
<Browser
>(popup
)));
435 // Create another popup.
436 popup
= CreateBrowserForPopup(browser()->profile());
437 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup
));
438 ASSERT_NO_FATAL_FAILURE(
439 GetOmniboxViewForBrowser(popup
, &omnibox_view
));
442 // Set the edit text to "Hello world".
443 omnibox_view
->SetUserText(ASCIIToUTF16("Hello world"));
444 chrome::FocusLocationBar(popup
);
445 EXPECT_TRUE(omnibox_view
->IsSelectAll());
447 // Try editing the location bar text -- should be disallowed.
448 ASSERT_NO_FATAL_FAILURE(SendKeyForBrowser(popup
, ui::VKEY_S
, 0));
449 EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view
->GetText());
450 EXPECT_TRUE(omnibox_view
->IsSelectAll());
452 ASSERT_NO_FATAL_FAILURE(
453 SendKeyForBrowser(popup
, ui::VKEY_X
, kCtrlOrCmdMask
));
454 EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view
->GetText());
455 EXPECT_TRUE(omnibox_view
->IsSelectAll());
457 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
458 // Try alt-f4 to close the popup.
459 ASSERT_TRUE(SendKeyAndWait(
460 popup
, ui::VKEY_F4
, ui::EF_ALT_DOWN
,
461 chrome::NOTIFICATION_BROWSER_CLOSED
, content::Source
<Browser
>(popup
)));
465 // http://crbug.com/133341
466 #if defined(OS_LINUX)
467 #define MAYBE_BackspaceInKeywordMode DISABLED_BackspaceInKeywordMode
469 #define MAYBE_BackspaceInKeywordMode BackspaceInKeywordMode
472 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_BackspaceInKeywordMode
) {
473 OmniboxView
* omnibox_view
= NULL
;
474 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
476 // Trigger keyword hint mode.
477 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys
));
478 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
479 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
481 // Trigger keyword mode.
482 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
483 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
484 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
486 // Backspace without search text should bring back keyword hint mode.
487 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
488 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
489 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
491 // Trigger keyword mode again.
492 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
493 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
494 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
496 // Input something as search text.
497 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys
));
499 // Should stay in keyword mode while deleting search text by pressing
501 for (size_t i
= 0; i
< arraysize(kSearchText
) - 1; ++i
) {
502 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
503 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
504 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
507 // Input something as search text.
508 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys
));
510 // Move cursor to the beginning of the search text.
511 #if defined(OS_MACOSX)
512 // Home doesn't work on Mac trybot.
513 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A
, ui::EF_CONTROL_DOWN
));
515 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_HOME
, 0));
517 // Backspace at the beginning of the search text shall turn off
519 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
520 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
521 ASSERT_EQ(base::string16(), omnibox_view
->model()->keyword());
522 ASSERT_EQ(std::string(kSearchKeyword
) + kSearchText
,
523 UTF16ToUTF8(omnibox_view
->GetText()));
526 // http://crbug.com/158913
527 #if defined(OS_CHROMEOS) || defined(OS_WIN)
528 #define MAYBE_Escape DISABLED_Escape
530 #define MAYBE_Escape Escape
532 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_Escape
) {
533 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIHistoryURL
));
534 chrome::FocusLocationBar(browser());
536 OmniboxView
* omnibox_view
= NULL
;
537 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
539 base::string16 old_text
= omnibox_view
->GetText();
540 EXPECT_FALSE(old_text
.empty());
541 EXPECT_TRUE(omnibox_view
->IsSelectAll());
543 // Delete all text in omnibox.
544 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
545 EXPECT_TRUE(omnibox_view
->GetText().empty());
547 // Escape shall revert the text in omnibox.
548 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE
, 0));
549 EXPECT_EQ(old_text
, omnibox_view
->GetText());
550 EXPECT_TRUE(omnibox_view
->IsSelectAll());
554 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, DesiredTLD
) {
555 OmniboxView
* omnibox_view
= NULL
;
556 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
557 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
558 ASSERT_TRUE(popup_model
);
561 const wchar_t kKeys
[] = { ui::VKEY_B
, ui::VKEY_A
, ui::VKEY_R
, 0 };
562 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys
));
563 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
564 ASSERT_TRUE(popup_model
->IsOpen());
565 // ctrl-Enter triggers desired_tld feature, thus www.bar.com shall be
567 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN
, ui::EF_CONTROL_DOWN
,
568 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
569 content::Source
<content::NavigationController
>(
570 &browser()->tab_strip_model()->GetActiveWebContents()->
573 GURL url
= browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
574 EXPECT_EQ("www.bar.com", url
.host());
575 EXPECT_EQ("/", url
.path());
578 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, DesiredTLDWithTemporaryText
) {
579 OmniboxView
* omnibox_view
= NULL
;
580 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
581 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
582 ASSERT_TRUE(popup_model
);
584 Profile
* profile
= browser()->profile();
585 TemplateURLService
* template_url_service
=
586 TemplateURLServiceFactory::GetForProfile(profile
);
588 // Add a non-substituting keyword. This ensures the popup will have a
589 // non-verbatim entry with "ab" as a prefix. This way, by arrowing down, we
590 // can set "abc" as temporary text in the omnibox.
591 TemplateURLData data
;
592 data
.short_name
= ASCIIToUTF16("abc");
593 data
.SetKeyword(ASCIIToUTF16(kSearchText
));
594 data
.SetURL("http://abc.com/");
595 template_url_service
->Add(new TemplateURL(profile
, data
));
597 // Send "ab", so that an "abc" entry appears in the popup.
598 const wchar_t kSearchTextPrefixKeys
[] = { ui::VKEY_A
, ui::VKEY_B
, 0 };
599 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextPrefixKeys
));
600 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
601 ASSERT_TRUE(popup_model
->IsOpen());
603 // Arrow down to the "abc" entry in the popup.
604 size_t size
= popup_model
->result().size();
605 while (popup_model
->selected_line() < size
- 1) {
606 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN
, 0));
607 if (omnibox_view
->GetText() == ASCIIToUTF16("abc"))
610 ASSERT_EQ(ASCIIToUTF16("abc"), omnibox_view
->GetText());
612 // Hitting ctrl-enter should navigate based on the current text rather than
613 // the original input, i.e. to www.abc.com instead of www.ab.com.
614 ASSERT_TRUE(SendKeyAndWait(
615 browser(), ui::VKEY_RETURN
, ui::EF_CONTROL_DOWN
,
616 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
617 content::Source
<content::NavigationController
>(
618 &browser()->tab_strip_model()->GetActiveWebContents()->
621 GURL
url(browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
622 EXPECT_EQ("www.abc.com", url
.host());
623 EXPECT_EQ("/", url
.path());
626 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, AltEnter
) {
627 OmniboxView
* omnibox_view
= NULL
;
628 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
630 omnibox_view
->SetUserText(ASCIIToUTF16(chrome::kChromeUIHistoryURL
));
631 int tab_count
= browser()->tab_strip_model()->count();
632 // alt-Enter opens a new tab.
633 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RETURN
, ui::EF_ALT_DOWN
));
634 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count
+ 1));
637 // http://crbug.com/133354, http://crbug.com/146953
638 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, DISABLED_EnterToSearch
) {
639 OmniboxView
* omnibox_view
= NULL
;
640 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
641 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
642 ASSERT_TRUE(popup_model
);
644 // Test Enter to search.
645 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys
));
646 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
647 ASSERT_TRUE(popup_model
->IsOpen());
649 // Check if the default match result is Search Primary Provider.
650 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
651 popup_model
->result().default_match()->type
);
653 // Open the default match.
654 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN
, 0,
655 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
656 content::Source
<content::NavigationController
>(
657 &browser()->tab_strip_model()->GetActiveWebContents()->
659 GURL url
= browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
660 EXPECT_EQ(kSearchTextURL
, url
.spec());
662 // Test that entering a single character then Enter performs a search.
663 const wchar_t kSearchSingleCharKeys
[] = { ui::VKEY_Z
, 0 };
664 chrome::FocusLocationBar(browser());
665 EXPECT_TRUE(omnibox_view
->IsSelectAll());
666 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchSingleCharKeys
));
667 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
668 ASSERT_TRUE(popup_model
->IsOpen());
669 EXPECT_EQ("z", UTF16ToUTF8(omnibox_view
->GetText()));
671 // Check if the default match result is Search Primary Provider.
672 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
673 popup_model
->result().default_match()->type
);
675 // Open the default match.
676 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN
, 0,
677 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
678 content::Source
<content::NavigationController
>(
679 &browser()->tab_strip_model()->GetActiveWebContents()->
681 url
= browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
682 EXPECT_EQ("http://www.foo.com/search?q=z", url
.spec());
685 // http://crbug.com/131179
686 #if defined(OS_LINUX)
687 #define MAYBE_EscapeToDefaultMatch DISABLED_EscapeToDefaultMatch
689 #define MAYBE_EscapeToDefaultMatch EscapeToDefaultMatch
691 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_EscapeToDefaultMatch
) {
692 OmniboxView
* omnibox_view
= NULL
;
693 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
694 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
695 ASSERT_TRUE(popup_model
);
697 // Input something to trigger inline autocomplete.
698 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys
));
699 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
700 ASSERT_TRUE(popup_model
->IsOpen());
702 base::string16 old_text
= omnibox_view
->GetText();
704 // Make sure inline autocomplete is triggerred.
705 EXPECT_GT(old_text
.length(), arraysize(kInlineAutocompleteText
) - 1);
707 size_t old_selected_line
= popup_model
->selected_line();
708 EXPECT_EQ(0U, old_selected_line
);
710 // Move to another line with different text.
711 size_t size
= popup_model
->result().size();
712 while (popup_model
->selected_line() < size
- 1) {
713 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN
, 0));
714 ASSERT_NE(old_selected_line
, popup_model
->selected_line());
715 if (old_text
!= omnibox_view
->GetText())
719 EXPECT_NE(old_text
, omnibox_view
->GetText());
721 // Escape shall revert back to the default match item.
722 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE
, 0));
723 EXPECT_EQ(old_text
, omnibox_view
->GetText());
724 EXPECT_EQ(old_selected_line
, popup_model
->selected_line());
727 // http://crbug.com/131179, http://crbug.com/146619
728 #if defined(OS_LINUX) || defined(OS_WIN)
729 #define MAYBE_BasicTextOperations DISABLED_BasicTextOperations
731 #define MAYBE_BasicTextOperations BasicTextOperations
733 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_BasicTextOperations
) {
734 ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL
));
735 chrome::FocusLocationBar(browser());
737 OmniboxView
* omnibox_view
= NULL
;
738 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
740 base::string16 old_text
= omnibox_view
->GetText();
741 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL
), old_text
);
742 EXPECT_TRUE(omnibox_view
->IsSelectAll());
745 omnibox_view
->GetSelectionBounds(&start
, &end
);
746 EXPECT_EQ(0U, start
);
747 EXPECT_EQ(old_text
.size(), end
);
749 // Move the cursor to the end.
750 #if defined(OS_MACOSX)
751 // End doesn't work on Mac trybot.
752 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E
, ui::EF_CONTROL_DOWN
));
754 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END
, 0));
756 EXPECT_FALSE(omnibox_view
->IsSelectAll());
758 // Make sure the cursor is placed correctly.
759 omnibox_view
->GetSelectionBounds(&start
, &end
);
760 EXPECT_EQ(old_text
.size(), start
);
761 EXPECT_EQ(old_text
.size(), end
);
763 // Insert one character at the end. Make sure we won't insert
764 // anything after the special ZWS mark used in gtk implementation.
765 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A
, 0));
766 EXPECT_EQ(old_text
+ base::char16('a'), omnibox_view
->GetText());
768 // Delete one character from the end. Make sure we won't delete the special
769 // ZWS mark used in gtk implementation.
770 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
771 EXPECT_EQ(old_text
, omnibox_view
->GetText());
773 omnibox_view
->SelectAll(true);
774 EXPECT_TRUE(omnibox_view
->IsSelectAll());
775 omnibox_view
->GetSelectionBounds(&start
, &end
);
776 EXPECT_EQ(0U, start
);
777 EXPECT_EQ(old_text
.size(), end
);
779 // Delete the content
780 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE
, 0));
781 EXPECT_TRUE(omnibox_view
->IsSelectAll());
782 omnibox_view
->GetSelectionBounds(&start
, &end
);
783 EXPECT_EQ(0U, start
);
785 EXPECT_TRUE(omnibox_view
->GetText().empty());
787 // Check if RevertAll() can set text and cursor correctly.
788 omnibox_view
->RevertAll();
789 EXPECT_FALSE(omnibox_view
->IsSelectAll());
790 EXPECT_EQ(old_text
, omnibox_view
->GetText());
791 omnibox_view
->GetSelectionBounds(&start
, &end
);
792 EXPECT_EQ(old_text
.size(), start
);
793 EXPECT_EQ(old_text
.size(), end
);
796 // http://crbug.com/131179
797 #if defined(OS_LINUX)
798 #define MAYBE_AcceptKeywordBySpace DISABLED_AcceptKeywordBySpace
800 #define MAYBE_AcceptKeywordBySpace AcceptKeywordBySpace
803 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_AcceptKeywordBySpace
) {
804 OmniboxView
* omnibox_view
= NULL
;
805 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
807 base::string16
search_keyword(ASCIIToUTF16(kSearchKeyword
));
809 // Trigger keyword hint mode.
810 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys
));
811 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
812 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
813 ASSERT_EQ(search_keyword
, omnibox_view
->GetText());
815 // Trigger keyword mode by space.
816 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
817 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
818 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
819 ASSERT_TRUE(omnibox_view
->GetText().empty());
821 // Revert to keyword hint mode.
822 omnibox_view
->model()->ClearKeyword(base::string16());
823 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
824 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
825 ASSERT_EQ(search_keyword
, omnibox_view
->GetText());
827 // Keyword should also be accepted by typing an ideographic space.
828 omnibox_view
->OnBeforePossibleChange();
829 omnibox_view
->SetWindowTextAndCaretPos(search_keyword
+
830 base::WideToUTF16(L
"\x3000"), search_keyword
.length() + 1, false, false);
831 omnibox_view
->OnAfterPossibleChange();
832 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
833 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
834 ASSERT_TRUE(omnibox_view
->GetText().empty());
836 // Revert to keyword hint mode.
837 omnibox_view
->model()->ClearKeyword(base::string16());
838 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
839 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
840 ASSERT_EQ(search_keyword
, omnibox_view
->GetText());
842 // Keyword shouldn't be accepted by pressing space with a trailing
844 omnibox_view
->SetWindowTextAndCaretPos(search_keyword
+ base::char16(' '),
845 search_keyword
.length() + 1, false, false);
846 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
847 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
848 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
849 ASSERT_EQ(search_keyword
+ ASCIIToUTF16(" "), omnibox_view
->GetText());
851 // Keyword shouldn't be accepted by deleting the trailing space.
852 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
853 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
854 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
855 ASSERT_EQ(search_keyword
+ base::char16(' '), omnibox_view
->GetText());
857 // Keyword shouldn't be accepted by pressing space before a trailing space.
858 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
859 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
860 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
861 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
862 ASSERT_EQ(search_keyword
+ ASCIIToUTF16(" "), omnibox_view
->GetText());
864 // Keyword should be accepted by pressing space in the middle of context and
865 // just after the keyword.
866 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
867 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A
, 0));
868 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
869 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
870 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
871 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
872 ASSERT_EQ(ASCIIToUTF16("a "), omnibox_view
->GetText());
874 omnibox_view
->GetSelectionBounds(&start
, &end
);
875 EXPECT_EQ(0U, start
);
878 // Keyword shouldn't be accepted by pasting "foo bar".
879 omnibox_view
->SetUserText(base::string16());
880 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
881 ASSERT_TRUE(omnibox_view
->model()->keyword().empty());
883 omnibox_view
->OnBeforePossibleChange();
884 omnibox_view
->model()->OnPaste();
885 omnibox_view
->SetWindowTextAndCaretPos(search_keyword
+
886 ASCIIToUTF16(" bar"), search_keyword
.length() + 4, false, false);
887 omnibox_view
->OnAfterPossibleChange();
888 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
889 ASSERT_TRUE(omnibox_view
->model()->keyword().empty());
890 ASSERT_EQ(search_keyword
+ ASCIIToUTF16(" bar"), omnibox_view
->GetText());
892 // Keyword shouldn't be accepted for case like: "foo b|ar" -> "foo b |ar".
893 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
894 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
895 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
896 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
897 ASSERT_TRUE(omnibox_view
->model()->keyword().empty());
898 ASSERT_EQ(search_keyword
+ ASCIIToUTF16(" b ar"), omnibox_view
->GetText());
900 // Keyword could be accepted by pressing space with a selected range at the
902 omnibox_view
->OnBeforePossibleChange();
903 omnibox_view
->OnInlineAutocompleteTextMaybeChanged(
904 search_keyword
+ ASCIIToUTF16(" "), search_keyword
.length());
905 omnibox_view
->OnAfterPossibleChange();
906 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
907 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
908 ASSERT_EQ(search_keyword
+ ASCIIToUTF16(" "), omnibox_view
->GetText());
910 omnibox_view
->GetSelectionBounds(&start
, &end
);
911 ASSERT_NE(start
, end
);
912 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
913 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
914 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
915 ASSERT_EQ(base::string16(), omnibox_view
->GetText());
917 // Space should accept keyword even when inline autocomplete is available.
918 omnibox_view
->SetUserText(base::string16());
919 const TestHistoryEntry kHistoryFoobar
= {
920 "http://www.foobar.com", "Page foobar", 100, 100, true
923 // Add a history entry to trigger inline autocomplete when typing "foo".
924 ASSERT_NO_FATAL_FAILURE(
925 AddHistoryEntry(kHistoryFoobar
, Time::Now() - TimeDelta::FromHours(1)));
927 // Type "foo" to trigger inline autocomplete.
928 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys
));
929 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
930 ASSERT_TRUE(omnibox_view
->model()->popup_model()->IsOpen());
931 ASSERT_NE(search_keyword
, omnibox_view
->GetText());
933 // Keyword hint shouldn't be visible.
934 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
935 ASSERT_TRUE(omnibox_view
->model()->keyword().empty());
937 // Trigger keyword mode by space.
938 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
939 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
940 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
941 ASSERT_TRUE(omnibox_view
->GetText().empty());
943 // Space in the middle of a temporary text, which separates the text into
944 // keyword and replacement portions, should trigger keyword mode.
945 omnibox_view
->SetUserText(base::string16());
946 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys
));
947 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
948 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
949 ASSERT_TRUE(popup_model
->IsOpen());
950 ASSERT_EQ(ASCIIToUTF16("foobar.com"), omnibox_view
->GetText());
951 omnibox_view
->model()->OnUpOrDownKeyPressed(1);
952 omnibox_view
->model()->OnUpOrDownKeyPressed(-1);
953 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
954 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
955 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
956 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, 0));
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_LEFT
, 0));
960 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
961 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
962 ASSERT_EQ(search_keyword
, omnibox_view
->model()->keyword());
963 ASSERT_EQ(ASCIIToUTF16("bar.com"), omnibox_view
->GetText());
965 // Space after temporary text that looks like a keyword, when the original
966 // input does not look like a keyword, should trigger keyword mode.
967 omnibox_view
->SetUserText(base::string16());
968 const TestHistoryEntry kHistoryFoo
= {
969 "http://footest.com", "Page footest", 1000, 1000, true
972 // Add a history entry to trigger HQP matching with text == keyword when
974 ASSERT_NO_FATAL_FAILURE(
975 AddHistoryEntry(kHistoryFoo
, Time::Now() - TimeDelta::FromMinutes(10)));
977 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_F
, 0));
978 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_O
, 0));
979 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
980 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_T
, 0));
981 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E
, 0));
982 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
983 ASSERT_TRUE(popup_model
->IsOpen());
984 base::string16
search_keyword2(ASCIIToUTF16(kSearchKeyword2
));
985 while ((omnibox_view
->GetText() != search_keyword2
) &&
986 (popup_model
->selected_line() < popup_model
->result().size() - 1))
987 omnibox_view
->model()->OnUpOrDownKeyPressed(1);
988 ASSERT_EQ(search_keyword2
, omnibox_view
->GetText());
989 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE
, 0));
990 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
991 ASSERT_EQ(search_keyword2
, omnibox_view
->model()->keyword());
992 ASSERT_TRUE(omnibox_view
->GetText().empty());
995 // http://crbug.com/131179
996 #if defined(OS_LINUX)
997 #define MAYBE_NonSubstitutingKeywordTest DISABLED_NonSubstitutingKeywordTest
999 #define MAYBE_NonSubstitutingKeywordTest NonSubstitutingKeywordTest
1002 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_NonSubstitutingKeywordTest
) {
1003 OmniboxView
* omnibox_view
= NULL
;
1004 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1005 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1006 ASSERT_TRUE(popup_model
);
1008 Profile
* profile
= browser()->profile();
1009 TemplateURLService
* template_url_service
=
1010 TemplateURLServiceFactory::GetForProfile(profile
);
1012 // Add a non-default substituting keyword.
1013 TemplateURLData data
;
1014 data
.short_name
= ASCIIToUTF16("Search abc");
1015 data
.SetKeyword(ASCIIToUTF16(kSearchText
));
1016 data
.SetURL("http://abc.com/{searchTerms}");
1017 TemplateURL
* template_url
= new TemplateURL(profile
, data
);
1018 template_url_service
->Add(template_url
);
1020 omnibox_view
->SetUserText(base::string16());
1022 // Non-default substituting keyword shouldn't be matched by default.
1023 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys
));
1024 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1025 ASSERT_TRUE(popup_model
->IsOpen());
1027 // Check if the default match result is Search Primary Provider.
1028 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
1029 popup_model
->result().default_match()->type
);
1030 ASSERT_EQ(kSearchTextURL
,
1031 popup_model
->result().default_match()->destination_url
.spec());
1033 omnibox_view
->SetUserText(base::string16());
1034 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1035 ASSERT_FALSE(popup_model
->IsOpen());
1037 // Try a non-substituting keyword.
1038 template_url_service
->Remove(template_url
);
1039 data
.short_name
= ASCIIToUTF16("abc");
1040 data
.SetURL("http://abc.com/");
1041 template_url_service
->Add(new TemplateURL(profile
, data
));
1043 // We always allow exact matches for non-substituting keywords.
1044 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys
));
1045 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1046 ASSERT_TRUE(popup_model
->IsOpen());
1047 ASSERT_EQ(AutocompleteMatchType::HISTORY_KEYWORD
,
1048 popup_model
->result().default_match()->type
);
1049 ASSERT_EQ("http://abc.com/",
1050 popup_model
->result().default_match()->destination_url
.spec());
1053 // http://crbug.com/131179 http://crbug.com/165765
1054 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_MACOSX)
1055 #define MAYBE_DeleteItem DISABLED_DeleteItem
1057 #define MAYBE_DeleteItem DeleteItem
1059 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_DeleteItem
) {
1060 // Disable the search provider, to make sure the popup contains only history
1062 TemplateURLService
* model
=
1063 TemplateURLServiceFactory::GetForProfile(browser()->profile());
1064 model
->SetUserSelectedDefaultSearchProvider(NULL
);
1066 ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL
));
1067 chrome::FocusLocationBar(browser());
1069 OmniboxView
* omnibox_view
= NULL
;
1070 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1072 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1073 ASSERT_TRUE(popup_model
);
1075 base::string16 old_text
= omnibox_view
->GetText();
1077 // Input something that can match history items.
1078 omnibox_view
->SetUserText(ASCIIToUTF16("site.com/p"));
1079 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1080 ASSERT_TRUE(popup_model
->IsOpen());
1082 // Delete the inline autocomplete part.
1083 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE
, 0));
1084 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1085 ASSERT_TRUE(popup_model
->IsOpen());
1086 ASSERT_GE(popup_model
->result().size(), 3U);
1088 base::string16 user_text
= omnibox_view
->GetText();
1089 ASSERT_EQ(ASCIIToUTF16("site.com/p"), user_text
);
1090 omnibox_view
->SelectAll(true);
1091 ASSERT_TRUE(omnibox_view
->IsSelectAll());
1094 size_t default_line
= popup_model
->selected_line();
1095 omnibox_view
->model()->OnUpOrDownKeyPressed(1);
1096 ASSERT_EQ(default_line
+ 1, popup_model
->selected_line());
1097 base::string16 selected_text
=
1098 popup_model
->result().match_at(default_line
+ 1).fill_into_edit
;
1099 // Temporary text is shown.
1100 ASSERT_EQ(selected_text
, omnibox_view
->GetText());
1101 ASSERT_FALSE(omnibox_view
->IsSelectAll());
1104 popup_model
->TryDeletingCurrentItem();
1105 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1106 // The selected line shouldn't be changed, because we have more than two
1108 ASSERT_EQ(default_line
+ 1, popup_model
->selected_line());
1109 // Make sure the item is really deleted.
1110 ASSERT_NE(selected_text
,
1111 popup_model
->result().match_at(default_line
+ 1).fill_into_edit
);
1113 popup_model
->result().match_at(default_line
+ 1).fill_into_edit
;
1114 // New temporary text is shown.
1115 ASSERT_EQ(selected_text
, omnibox_view
->GetText());
1117 // Revert to the default match.
1118 ASSERT_TRUE(omnibox_view
->model()->OnEscapeKeyPressed());
1119 ASSERT_EQ(default_line
, popup_model
->selected_line());
1120 ASSERT_EQ(user_text
, omnibox_view
->GetText());
1121 ASSERT_TRUE(omnibox_view
->IsSelectAll());
1123 // Move down and up to select the default match as temporary text.
1124 omnibox_view
->model()->OnUpOrDownKeyPressed(1);
1125 ASSERT_EQ(default_line
+ 1, popup_model
->selected_line());
1126 omnibox_view
->model()->OnUpOrDownKeyPressed(-1);
1127 ASSERT_EQ(default_line
, popup_model
->selected_line());
1129 selected_text
= popup_model
->result().match_at(default_line
).fill_into_edit
;
1130 // New temporary text is shown.
1131 ASSERT_EQ(selected_text
, omnibox_view
->GetText());
1132 ASSERT_FALSE(omnibox_view
->IsSelectAll());
1135 // TODO(mrossetti): http://crbug.com/82335
1136 // Delete the default item.
1137 popup_model
->TryDeletingCurrentItem();
1138 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1139 // The selected line shouldn't be changed, but the default item should have
1141 ASSERT_EQ(default_line
, popup_model
->selected_line());
1142 // Make sure the item is really deleted.
1143 EXPECT_NE(selected_text
,
1144 popup_model
->result().match_at(default_line
).fill_into_edit
);
1146 popup_model
->result().match_at(default_line
).fill_into_edit
;
1147 // New temporary text is shown.
1148 ASSERT_EQ(selected_text
, omnibox_view
->GetText());
1151 // As the current selected item is the new default item, pressing Escape key
1152 // should revert all directly.
1153 ASSERT_TRUE(omnibox_view
->model()->OnEscapeKeyPressed());
1154 ASSERT_EQ(old_text
, omnibox_view
->GetText());
1155 ASSERT_TRUE(omnibox_view
->IsSelectAll());
1158 // http://crbug.com/133344
1159 #if defined(OS_LINUX)
1160 #define MAYBE_TabAcceptKeyword DISABLED_TabAcceptKeyword
1162 #define MAYBE_TabAcceptKeyword TabAcceptKeyword
1165 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_TabAcceptKeyword
) {
1166 OmniboxView
* omnibox_view
= NULL
;
1167 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1169 base::string16 text
= ASCIIToUTF16(kSearchKeyword
);
1171 // Trigger keyword hint mode.
1172 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys
));
1173 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
1174 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1175 ASSERT_EQ(text
, omnibox_view
->GetText());
1177 // Trigger keyword mode by tab.
1178 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1179 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
1180 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1181 ASSERT_TRUE(omnibox_view
->GetText().empty());
1183 // Revert to keyword hint mode.
1184 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
1185 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
1186 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1187 ASSERT_EQ(text
, omnibox_view
->GetText());
1189 // The location bar should still have focus.
1190 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1192 // Trigger keyword mode by tab.
1193 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1194 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
1195 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1196 ASSERT_TRUE(omnibox_view
->GetText().empty());
1198 // Revert to keyword hint mode with SHIFT+TAB.
1199 #if defined(OS_MACOSX)
1200 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACKTAB
, 0));
1202 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, ui::EF_SHIFT_DOWN
));
1204 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
1205 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1206 ASSERT_EQ(text
, omnibox_view
->GetText());
1207 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1210 #if !defined(OS_MACOSX)
1211 // Mac intentionally does not support this behavior.
1213 // http://crbug.com/133360
1214 #if defined(OS_LINUX)
1215 #define MAYBE_TabTraverseResultsTest DISABLED_TabTraverseResultsTest
1217 #define MAYBE_TabTraverseResultsTest TabTraverseResultsTest
1220 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, MAYBE_TabTraverseResultsTest
) {
1221 OmniboxView
* omnibox_view
= NULL
;
1222 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1223 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1224 ASSERT_TRUE(popup_model
);
1226 // Input something to trigger results.
1227 const wchar_t kKeys
[] = { ui::VKEY_B
, ui::VKEY_A
, ui::VKEY_R
, 0 };
1228 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys
));
1229 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1230 ASSERT_TRUE(popup_model
->IsOpen());
1232 size_t old_selected_line
= popup_model
->selected_line();
1233 EXPECT_EQ(0U, old_selected_line
);
1235 // Move down the results.
1236 for (size_t size
= popup_model
->result().size();
1237 popup_model
->selected_line() < size
- 1;
1238 old_selected_line
= popup_model
->selected_line()) {
1239 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1240 ASSERT_LT(old_selected_line
, popup_model
->selected_line());
1243 // Don't move past the end.
1244 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1245 ASSERT_EQ(old_selected_line
, popup_model
->selected_line());
1246 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1248 // Move back up the results.
1249 for (; popup_model
->selected_line() > 0U;
1250 old_selected_line
= popup_model
->selected_line()) {
1251 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, ui::EF_SHIFT_DOWN
));
1252 ASSERT_GT(old_selected_line
, popup_model
->selected_line());
1255 // Don't move past the beginning.
1256 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, ui::EF_SHIFT_DOWN
));
1257 ASSERT_EQ(0U, popup_model
->selected_line());
1258 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1260 const TestHistoryEntry kHistoryFoo
= {
1261 "http://foo/", "Page foo", 1, 1, false
1264 // Add a history entry so "foo" gets multiple matches.
1265 ASSERT_NO_FATAL_FAILURE(
1266 AddHistoryEntry(kHistoryFoo
, Time::Now() - TimeDelta::FromHours(1)));
1269 ASSERT_NO_FATAL_FAILURE(omnibox_view
->SelectAll(false));
1270 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys
));
1271 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1273 // Trigger keyword mode by tab.
1274 base::string16 text
= ASCIIToUTF16(kSearchKeyword
);
1275 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1276 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
1277 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1278 ASSERT_TRUE(omnibox_view
->GetText().empty());
1280 // The location bar should still have focus.
1281 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1283 // Pressing tab again should move to the next result and clear keyword
1285 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1286 ASSERT_EQ(1U, omnibox_view
->model()->popup_model()->selected_line());
1287 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
1288 ASSERT_NE(text
, omnibox_view
->model()->keyword());
1290 // The location bar should still have focus.
1291 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1293 // Moving back up should not show keyword mode.
1294 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, ui::EF_SHIFT_DOWN
));
1295 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
1296 ASSERT_EQ(text
, omnibox_view
->model()->keyword());
1298 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1303 // http://crbug.com/133347
1304 #if defined(OS_LINUX)
1305 #define MAYBE_PersistKeywordModeOnTabSwitch DISABLED_PersistKeywordModeOnTabSwitch
1307 #define MAYBE_PersistKeywordModeOnTabSwitch PersistKeywordModeOnTabSwitch
1310 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
,
1311 MAYBE_PersistKeywordModeOnTabSwitch
) {
1312 OmniboxView
* omnibox_view
= NULL
;
1313 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1315 // Trigger keyword hint mode.
1316 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys
));
1317 ASSERT_TRUE(omnibox_view
->model()->is_keyword_hint());
1318 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
1320 // Trigger keyword mode.
1321 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB
, 0));
1322 ASSERT_FALSE(omnibox_view
->model()->is_keyword_hint());
1323 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
1325 // Input something as search text.
1326 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys
));
1328 // Create a new tab.
1329 chrome::NewTab(browser());
1331 // Switch back to the first tab.
1332 browser()->tab_strip_model()->ActivateTabAt(0, true);
1334 // Make sure we're still in keyword mode.
1335 ASSERT_EQ(kSearchKeyword
, UTF16ToUTF8(omnibox_view
->model()->keyword()));
1338 // http://crbug.com/133355
1339 #if defined(OS_LINUX)
1340 #define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest DISABLED_CtrlKeyPressedWithInlineAutocompleteTest
1342 #define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest CtrlKeyPressedWithInlineAutocompleteTest
1345 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
,
1346 MAYBE_CtrlKeyPressedWithInlineAutocompleteTest
) {
1347 OmniboxView
* omnibox_view
= NULL
;
1348 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1349 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1350 ASSERT_TRUE(popup_model
);
1352 // Input something to trigger inline autocomplete.
1353 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys
));
1354 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1355 ASSERT_TRUE(popup_model
->IsOpen());
1357 base::string16 old_text
= omnibox_view
->GetText();
1359 // Make sure inline autocomplete is triggerred.
1360 EXPECT_GT(old_text
.length(), arraysize(kInlineAutocompleteText
) - 1);
1363 omnibox_view
->model()->OnControlKeyChanged(true);
1365 // Inline autocomplete should still be there.
1366 EXPECT_EQ(old_text
, omnibox_view
->GetText());
1369 #if defined(TOOLKIT_VIEWS)
1370 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, UndoRedo
) {
1371 ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL
));
1372 chrome::FocusLocationBar(browser());
1374 OmniboxView
* omnibox_view
= NULL
;
1375 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1377 base::string16 old_text
= omnibox_view
->GetText();
1378 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL
), old_text
);
1379 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1381 // Delete the text, then undo.
1382 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
1383 EXPECT_TRUE(omnibox_view
->GetText().empty());
1384 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z
, ui::EF_CONTROL_DOWN
));
1385 EXPECT_EQ(old_text
, omnibox_view
->GetText());
1387 // Redo should delete the text again.
1388 ASSERT_NO_FATAL_FAILURE(
1389 SendKey(ui::VKEY_Z
, ui::EF_CONTROL_DOWN
| ui::EF_SHIFT_DOWN
));
1390 EXPECT_TRUE(omnibox_view
->GetText().empty());
1392 // Looks like the undo manager doesn't support restoring selection.
1393 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z
, ui::EF_CONTROL_DOWN
));
1394 EXPECT_FALSE(omnibox_view
->IsSelectAll());
1396 // The cursor should be at the end.
1398 omnibox_view
->GetSelectionBounds(&start
, &end
);
1399 EXPECT_EQ(old_text
.size(), start
);
1400 EXPECT_EQ(old_text
.size(), end
);
1402 // Delete two characters.
1403 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
1404 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
1405 EXPECT_EQ(old_text
.substr(0, old_text
.size() - 2), omnibox_view
->GetText());
1408 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z
, ui::EF_CONTROL_DOWN
));
1409 EXPECT_EQ(old_text
, omnibox_view
->GetText());
1412 ASSERT_NO_FATAL_FAILURE(
1413 SendKey(ui::VKEY_Z
, ui::EF_CONTROL_DOWN
| ui::EF_SHIFT_DOWN
));
1414 EXPECT_EQ(old_text
.substr(0, old_text
.size() - 2), omnibox_view
->GetText());
1416 // Delete everything.
1417 omnibox_view
->SelectAll(true);
1418 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
1419 EXPECT_TRUE(omnibox_view
->GetText().empty());
1421 // Undo delete everything.
1422 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z
, ui::EF_CONTROL_DOWN
));
1423 EXPECT_EQ(old_text
.substr(0, old_text
.size() - 2), omnibox_view
->GetText());
1425 // Undo delete two characters.
1426 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z
, ui::EF_CONTROL_DOWN
));
1427 EXPECT_EQ(old_text
, omnibox_view
->GetText());
1430 // See http://crosbug.com/10306
1431 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
,
1432 BackspaceDeleteHalfWidthKatakana
) {
1433 OmniboxView
* omnibox_view
= NULL
;
1434 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1435 // Insert text: ダ
1436 omnibox_view
->SetUserText(base::UTF8ToUTF16("\357\276\200\357\276\236"));
1438 // Move the cursor to the end.
1439 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END
, 0));
1441 // Backspace should delete one character.
1442 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK
, 0));
1443 EXPECT_EQ(base::UTF8ToUTF16("\357\276\200"), omnibox_view
->GetText());
1445 #endif // defined(TOOLKIT_VIEWS)
1447 // Flaky test. crbug.com/356850
1448 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
,
1449 DISABLED_DoesNotUpdateAutocompleteOnBlur
) {
1450 OmniboxView
* omnibox_view
= NULL
;
1451 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1452 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1453 ASSERT_TRUE(popup_model
);
1455 // Input something to trigger inline autocomplete.
1456 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys
));
1457 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1458 ASSERT_TRUE(popup_model
->IsOpen());
1460 omnibox_view
->GetSelectionBounds(&start
, &end
);
1461 EXPECT_TRUE(start
!= end
);
1462 base::string16 old_autocomplete_text
=
1463 omnibox_view
->model()->autocomplete_controller()->input_
.text();
1465 // Unfocus the omnibox. This should clear the text field selection and
1466 // close the popup, but should not run autocomplete.
1467 // Note: GTK preserves the selection when the omnibox is unfocused.
1468 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER
);
1469 ASSERT_FALSE(popup_model
->IsOpen());
1470 omnibox_view
->GetSelectionBounds(&start
, &end
);
1471 EXPECT_TRUE(start
== end
);
1473 EXPECT_EQ(old_autocomplete_text
,
1474 omnibox_view
->model()->autocomplete_controller()->input_
.text());
1477 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, Paste
) {
1478 OmniboxView
* omnibox_view
= NULL
;
1479 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1480 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1481 ASSERT_TRUE(popup_model
);
1482 EXPECT_FALSE(popup_model
->IsOpen());
1484 // Paste should yield the expected text and open the popup.
1485 SetClipboardText(ASCIIToUTF16(kSearchText
));
1486 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V
, kCtrlOrCmdMask
));
1487 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1488 EXPECT_EQ(ASCIIToUTF16(kSearchText
), omnibox_view
->GetText());
1489 EXPECT_TRUE(popup_model
->IsOpen());
1491 // Close the popup and select all.
1492 omnibox_view
->CloseOmniboxPopup();
1493 omnibox_view
->SelectAll(false);
1494 EXPECT_FALSE(popup_model
->IsOpen());
1496 // Pasting the same text again over itself should re-open the popup.
1497 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V
, kCtrlOrCmdMask
));
1498 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1499 EXPECT_EQ(ASCIIToUTF16(kSearchText
), omnibox_view
->GetText());
1500 // This fails on GTK, see http://crbug.com/131179
1501 #if !defined(TOOLKIT_GTK)
1502 EXPECT_TRUE(popup_model
->IsOpen());
1504 omnibox_view
->CloseOmniboxPopup();
1505 EXPECT_FALSE(popup_model
->IsOpen());
1507 // Pasting amid text should yield the expected text and re-open the popup.
1508 omnibox_view
->SetWindowTextAndCaretPos(ASCIIToUTF16("abcd"), 2, false, false);
1509 SetClipboardText(ASCIIToUTF16("123"));
1510 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V
, kCtrlOrCmdMask
));
1511 EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view
->GetText());
1512 EXPECT_TRUE(popup_model
->IsOpen());
1514 // Ctrl/Cmd+Alt+V should not paste.
1515 ASSERT_NO_FATAL_FAILURE(
1516 SendKey(ui::VKEY_V
, kCtrlOrCmdMask
| ui::EF_ALT_DOWN
));
1517 EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view
->GetText());
1518 // TODO(msw): Test that AltGr+V does not paste.
1521 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, CopyURLToClipboard
) {
1522 // Set permanent text thus making sure that omnibox treats 'google.com'
1523 // as URL (not as ordinary user input).
1524 TestToolbarModel
* test_toolbar_model
= new TestToolbarModel
;
1525 scoped_ptr
<ToolbarModel
> toolbar_model(test_toolbar_model
);
1526 test_toolbar_model
->set_text(ASCIIToUTF16("http://www.google.com/"));
1527 browser()->swap_toolbar_models(&toolbar_model
);
1528 OmniboxView
* omnibox_view
= NULL
;
1529 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1530 OmniboxEditModel
* edit_model
= omnibox_view
->model();
1531 ASSERT_NE(static_cast<OmniboxEditModel
*>(NULL
), edit_model
);
1532 edit_model
->UpdatePermanentText();
1534 const char* target_url
= "http://www.google.com/calendar";
1535 omnibox_view
->SetUserText(ASCIIToUTF16(target_url
));
1537 // Location bar must have focus.
1538 chrome::FocusLocationBar(browser());
1539 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1541 // Select full URL and copy it to clipboard. General text and html should
1543 omnibox_view
->SelectAll(true);
1544 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1545 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1546 clipboard
->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE
);
1547 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY
));
1548 EXPECT_EQ(ASCIIToUTF16(target_url
), omnibox_view
->GetText());
1549 EXPECT_TRUE(clipboard
->IsFormatAvailable(
1550 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1552 // Make sure HTML format isn't written. See
1553 // BookmarkNodeData::WriteToClipboard() for details.
1554 EXPECT_FALSE(clipboard
->IsFormatAvailable(
1555 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1557 // These platforms should read bookmark format.
1558 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1559 base::string16 title
;
1561 clipboard
->ReadBookmark(&title
, &url
);
1562 EXPECT_EQ(target_url
, url
);
1563 EXPECT_EQ(ASCIIToUTF16(target_url
), title
);
1567 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, CutURLToClipboard
) {
1568 // Set permanent text thus making sure that omnibox treats 'google.com'
1569 // as URL (not as ordinary user input).
1570 TestToolbarModel
* test_toolbar_model
= new TestToolbarModel
;
1571 scoped_ptr
<ToolbarModel
> toolbar_model(test_toolbar_model
);
1572 test_toolbar_model
->set_text(ASCIIToUTF16("http://www.google.com/"));
1573 browser()->swap_toolbar_models(&toolbar_model
);
1574 OmniboxView
* omnibox_view
= NULL
;
1575 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1576 OmniboxEditModel
* edit_model
= omnibox_view
->model();
1577 ASSERT_NE(static_cast<OmniboxEditModel
*>(NULL
), edit_model
);
1578 edit_model
->UpdatePermanentText();
1580 const char* target_url
= "http://www.google.com/calendar";
1581 omnibox_view
->SetUserText(ASCIIToUTF16(target_url
));
1583 // Location bar must have focus.
1584 chrome::FocusLocationBar(browser());
1585 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1587 // Select full URL and cut it. General text and html should be available
1588 // in the clipboard.
1589 omnibox_view
->SelectAll(true);
1590 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1591 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1592 clipboard
->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE
);
1593 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT
));
1594 EXPECT_EQ(base::string16(), omnibox_view
->GetText());
1595 EXPECT_TRUE(clipboard
->IsFormatAvailable(
1596 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1598 // Make sure HTML format isn't written. See
1599 // BookmarkNodeData::WriteToClipboard() for details.
1600 EXPECT_FALSE(clipboard
->IsFormatAvailable(
1601 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1603 // These platforms should read bookmark format.
1604 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1605 base::string16 title
;
1607 clipboard
->ReadBookmark(&title
, &url
);
1608 EXPECT_EQ(target_url
, url
);
1609 EXPECT_EQ(ASCIIToUTF16(target_url
), title
);
1613 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, CopyTextToClipboard
) {
1614 OmniboxView
* omnibox_view
= NULL
;
1615 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1616 const char* target_text
= "foo";
1617 omnibox_view
->SetUserText(ASCIIToUTF16(target_text
));
1619 // Location bar must have focus.
1620 chrome::FocusLocationBar(browser());
1621 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1623 // Select full text and copy it to the clipboard.
1624 omnibox_view
->SelectAll(true);
1625 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1626 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1627 clipboard
->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE
);
1628 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY
));
1629 EXPECT_TRUE(clipboard
->IsFormatAvailable(
1630 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1631 EXPECT_FALSE(clipboard
->IsFormatAvailable(
1632 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1633 EXPECT_EQ(ASCIIToUTF16(target_text
), omnibox_view
->GetText());
1636 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, CutTextToClipboard
) {
1637 OmniboxView
* omnibox_view
= NULL
;
1638 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1639 const char* target_text
= "foo";
1640 omnibox_view
->SetUserText(ASCIIToUTF16(target_text
));
1642 // Location bar must have focus.
1643 chrome::FocusLocationBar(browser());
1644 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1646 // Select full text and cut it to the clipboard.
1647 omnibox_view
->SelectAll(true);
1648 EXPECT_TRUE(omnibox_view
->IsSelectAll());
1649 ui::Clipboard
* clipboard
= ui::Clipboard::GetForCurrentThread();
1650 clipboard
->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE
);
1651 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT
));
1652 EXPECT_TRUE(clipboard
->IsFormatAvailable(
1653 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1654 EXPECT_FALSE(clipboard
->IsFormatAvailable(
1655 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE
));
1656 EXPECT_EQ(base::string16(), omnibox_view
->GetText());
1659 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, EditSearchEngines
) {
1660 OmniboxView
* omnibox_view
= NULL
;
1661 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1662 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_EDIT_SEARCH_ENGINES
));
1663 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1664 const std::string target_url
=
1665 std::string(chrome::kChromeUISettingsURL
) + chrome::kSearchEnginesSubPage
;
1666 EXPECT_EQ(ASCIIToUTF16(target_url
), omnibox_view
->GetText());
1667 EXPECT_FALSE(omnibox_view
->model()->popup_model()->IsOpen());
1670 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, BeginningShownAfterBlur
) {
1671 OmniboxView
* omnibox_view
= NULL
;
1672 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1674 omnibox_view
->OnBeforePossibleChange();
1675 omnibox_view
->SetWindowTextAndCaretPos(ASCIIToUTF16("data:text/plain,test"),
1677 omnibox_view
->OnAfterPossibleChange();
1678 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1680 omnibox_view
->GetSelectionBounds(&start
, &end
);
1681 ASSERT_EQ(5U, start
);
1684 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER
);
1685 ASSERT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
1687 omnibox_view
->GetSelectionBounds(&start
, &end
);
1688 ASSERT_EQ(0U, start
);
1692 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, CtrlArrowAfterArrowSuggestions
) {
1693 OmniboxView
* omnibox_view
= NULL
;
1694 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1695 OmniboxPopupModel
* popup_model
= omnibox_view
->model()->popup_model();
1696 ASSERT_TRUE(popup_model
);
1698 // Input something to trigger results.
1699 const wchar_t kKeys
[] = { ui::VKEY_B
, ui::VKEY_A
, ui::VKEY_R
, 0 };
1700 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys
));
1701 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1702 ASSERT_TRUE(popup_model
->IsOpen());
1704 ASSERT_EQ(ASCIIToUTF16("bar.com/1"), omnibox_view
->GetText());
1706 // Arrow down on a suggestion, and omnibox text should be the suggestion.
1707 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN
, 0));
1708 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1709 ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view
->GetText());
1711 // Highlight the last 2 words and the omnibox text should not change.
1712 // Simulating Ctrl-shift-left only once does not seem to highlight anything
1714 #if defined(OS_MACOSX)
1715 // Mac uses alt-left/right to select a word.
1716 const int modifiers
= ui::EF_SHIFT_DOWN
| ui::EF_ALT_DOWN
;
1718 const int modifiers
= ui::EF_SHIFT_DOWN
| ui::EF_CONTROL_DOWN
;
1720 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, modifiers
));
1721 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT
, modifiers
));
1722 ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view
->GetText());
1725 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
,
1726 PersistSearchReplacementAcrossTabSwitch
) {
1727 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
1728 browser()->toolbar_model()->set_url_replacement_enabled(false);
1730 // Create a new tab.
1731 chrome::NewTab(browser());
1732 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
1734 // Switch back to the first tab.
1735 browser()->tab_strip_model()->ActivateTabAt(0, true);
1736 EXPECT_FALSE(browser()->toolbar_model()->url_replacement_enabled());
1739 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
,
1740 DontUpdateURLWhileSearchTermReplacementIsDisabled
) {
1741 OmniboxView
* omnibox_view
= NULL
;
1742 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view
));
1743 TestToolbarModel
* test_toolbar_model
= new TestToolbarModel
;
1744 scoped_ptr
<ToolbarModel
> toolbar_model(test_toolbar_model
);
1745 browser()->swap_toolbar_models(&toolbar_model
);
1747 base::string16
url_a(ASCIIToUTF16("http://www.a.com/"));
1748 base::string16
url_b(ASCIIToUTF16("http://www.b.com/"));
1749 base::string16
url_c(ASCIIToUTF16("http://www.c.com/"));
1750 chrome::FocusLocationBar(browser());
1751 test_toolbar_model
->set_text(url_a
);
1752 omnibox_view
->Update();
1753 EXPECT_EQ(url_a
, omnibox_view
->GetText());
1755 // Disable URL replacement and update. Because the omnibox has focus, the
1756 // visible text shouldn't change; see comments in
1757 // OmniboxEditModel::UpdatePermanentText().
1758 browser()->toolbar_model()->set_url_replacement_enabled(false);
1759 test_toolbar_model
->set_text(url_b
);
1760 omnibox_view
->Update();
1761 EXPECT_EQ(url_a
, omnibox_view
->GetText());
1763 // Re-enable URL replacement and ensure updating changes the text.
1764 browser()->toolbar_model()->set_url_replacement_enabled(true);
1765 // We have to change the toolbar model text here, or Update() will do nothing.
1766 // This is because the previous update already updated the permanent text.
1767 test_toolbar_model
->set_text(url_c
);
1768 omnibox_view
->Update();
1769 EXPECT_EQ(url_c
, omnibox_view
->GetText());
1771 // The same test, but using RevertAll() to reset search term replacement.
1772 test_toolbar_model
->set_text(url_a
);
1773 omnibox_view
->Update();
1774 EXPECT_EQ(url_a
, omnibox_view
->GetText());
1775 browser()->toolbar_model()->set_url_replacement_enabled(false);
1776 test_toolbar_model
->set_text(url_b
);
1777 omnibox_view
->Update();
1778 EXPECT_EQ(url_a
, omnibox_view
->GetText());
1779 omnibox_view
->RevertAll();
1780 EXPECT_EQ(url_b
, omnibox_view
->GetText());
1781 test_toolbar_model
->set_text(url_c
);
1782 omnibox_view
->Update();
1783 EXPECT_EQ(url_c
, omnibox_view
->GetText());
1786 IN_PROC_BROWSER_TEST_F(OmniboxViewTest
, EscDisablesSearchTermReplacement
) {
1787 browser()->toolbar_model()->set_url_replacement_enabled(true);
1788 chrome::FocusLocationBar(browser());
1789 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE
, 0));
1790 EXPECT_FALSE(browser()->toolbar_model()->url_replacement_enabled());