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/strings/string16.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/time/time.h"
12 #include "chrome/app/chrome_command_ids.h"
13 #include "chrome/browser/autocomplete/history_quick_provider.h"
14 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/history/history_service.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/omnibox/autocomplete_input.h"
38 #include "components/omnibox/autocomplete_match.h"
39 #include "components/search_engines/template_url.h"
40 #include "components/search_engines/template_url_service.h"
41 #include "content/public/browser/notification_service.h"
42 #include "content/public/browser/web_contents.h"
43 #include "net/dns/mock_host_resolver.h"
44 #include "ui/base/clipboard/clipboard.h"
45 #include "ui/base/clipboard/scoped_clipboard_writer.h"
46 #include "ui/events/event_constants.h"
47 #include "ui/events/keycodes/keyboard_codes.h"
48 #include "ui/gfx/point.h"
50 using base::ASCIIToUTF16;
51 using base::UTF16ToUTF8;
53 using base::TimeDelta;
57 const char kSearchKeyword[] = "foo";
58 const char kSearchKeyword2[] = "footest.com";
59 const ui::KeyboardCode kSearchKeywordKeys[] = {
60 ui::VKEY_F, ui::VKEY_O, ui::VKEY_O, ui::VKEY_UNKNOWN
62 const ui::KeyboardCode kSearchKeywordPrefixKeys[] = {
63 ui::VKEY_F, ui::VKEY_O, ui::VKEY_UNKNOWN
65 const ui::KeyboardCode kSearchKeywordCompletionKeys[] = {
66 ui::VKEY_O, ui::VKEY_UNKNOWN
68 const char kSearchURL[] = "http://www.foo.com/search?q={searchTerms}";
69 const char kSearchShortName[] = "foo";
70 const char kSearchText[] = "abc";
71 const ui::KeyboardCode kSearchTextKeys[] = {
72 ui::VKEY_A, ui::VKEY_B, ui::VKEY_C, ui::VKEY_UNKNOWN
74 const char kSearchTextURL[] = "http://www.foo.com/search?q=abc";
76 const char kInlineAutocompleteText[] = "def";
77 const ui::KeyboardCode kInlineAutocompleteTextKeys[] = {
78 ui::VKEY_D, ui::VKEY_E, ui::VKEY_F, ui::VKEY_UNKNOWN
81 // Hostnames that shall be blocked by host resolver.
82 const char *kBlockedHostnames[] = {
96 const struct TestHistoryEntry {
102 } kHistoryEntries[] = {
103 {"http://www.bar.com/1", "Page 1", 10, 10, false },
104 {"http://www.bar.com/2", "Page 2", 9, 9, false },
105 {"http://www.bar.com/3", "Page 3", 8, 8, false },
106 {"http://www.bar.com/4", "Page 4", 7, 7, false },
107 {"http://www.bar.com/5", "Page 5", 6, 6, false },
108 {"http://www.bar.com/6", "Page 6", 5, 5, false },
109 {"http://www.bar.com/7", "Page 7", 4, 4, false },
110 {"http://www.bar.com/8", "Page 8", 3, 3, false },
111 {"http://www.bar.com/9", "Page 9", 2, 2, false },
112 {"http://www.site.com/path/1", "Site 1", 4, 4, false },
113 {"http://www.site.com/path/2", "Site 2", 3, 3, false },
114 {"http://www.site.com/path/3", "Site 3", 2, 2, false },
116 // To trigger inline autocomplete.
117 {"http://www.def.com", "Page def", 10000, 10000, true },
119 // Used in particular for the desired TLD test. This makes it test
120 // the interesting case when there's an intranet host with the same
122 {"http://bar/", "Bar", 1, 0, false },
125 // Stores the given text to clipboard.
126 void SetClipboardText(const base::string16& text) {
127 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
128 ui::ScopedClipboardWriter writer(clipboard, ui::CLIPBOARD_TYPE_COPY_PASTE);
129 writer.WriteText(text);
132 #if defined(OS_MACOSX)
133 const int kCtrlOrCmdMask = ui::EF_COMMAND_DOWN;
135 const int kCtrlOrCmdMask = ui::EF_CONTROL_DOWN;
140 class OmniboxViewTest : public InProcessBrowserTest,
141 public content::NotificationObserver {
143 virtual void SetUpOnMainThread() OVERRIDE {
144 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
145 ASSERT_NO_FATAL_FAILURE(SetupComponents());
146 chrome::FocusLocationBar(browser());
147 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
150 static void GetOmniboxViewForBrowser(
151 const Browser* browser,
152 OmniboxView** omnibox_view) {
153 BrowserWindow* window = browser->window();
155 LocationBar* location_bar = window->GetLocationBar();
156 ASSERT_TRUE(location_bar);
157 *omnibox_view = location_bar->GetOmniboxView();
158 ASSERT_TRUE(*omnibox_view);
161 void GetOmniboxView(OmniboxView** omnibox_view) {
162 GetOmniboxViewForBrowser(browser(), omnibox_view);
165 static void SendKeyForBrowser(const Browser* browser,
166 ui::KeyboardCode key,
168 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
170 (modifiers & ui::EF_CONTROL_DOWN) != 0,
171 (modifiers & ui::EF_SHIFT_DOWN) != 0,
172 (modifiers & ui::EF_ALT_DOWN) != 0,
173 (modifiers & ui::EF_COMMAND_DOWN) != 0));
176 void SendKey(ui::KeyboardCode key, int modifiers) {
177 SendKeyForBrowser(browser(), key, modifiers);
180 void SendKeySequence(const ui::KeyboardCode* keys) {
181 for (; *keys != ui::VKEY_UNKNOWN; ++keys)
182 ASSERT_NO_FATAL_FAILURE(SendKey(*keys, 0));
185 bool SendKeyAndWait(const Browser* browser,
186 ui::KeyboardCode key,
189 const content::NotificationSource& source)
191 return ui_test_utils::SendKeyPressAndWait(
193 (modifiers & ui::EF_CONTROL_DOWN) != 0,
194 (modifiers & ui::EF_SHIFT_DOWN) != 0,
195 (modifiers & ui::EF_ALT_DOWN) != 0,
196 (modifiers & ui::EF_COMMAND_DOWN) != 0,
200 void WaitForTabOpenOrCloseForBrowser(const Browser* browser,
201 int expected_tab_count) {
202 int tab_count = browser->tab_strip_model()->count();
203 if (tab_count == expected_tab_count)
206 content::NotificationRegistrar registrar;
208 (tab_count < expected_tab_count) ?
209 static_cast<int>(chrome::NOTIFICATION_TAB_PARENTED) :
210 static_cast<int>(content::NOTIFICATION_WEB_CONTENTS_DESTROYED),
211 content::NotificationService::AllSources());
213 while (!HasFailure() &&
214 browser->tab_strip_model()->count() != expected_tab_count) {
215 content::RunMessageLoop();
218 ASSERT_EQ(expected_tab_count, browser->tab_strip_model()->count());
221 void WaitForTabOpenOrClose(int expected_tab_count) {
222 WaitForTabOpenOrCloseForBrowser(browser(), expected_tab_count);
225 void WaitForAutocompleteControllerDone() {
226 OmniboxView* omnibox_view = NULL;
227 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
229 AutocompleteController* controller =
230 omnibox_view->model()->autocomplete_controller();
231 ASSERT_TRUE(controller);
233 if (controller->done())
236 content::NotificationRegistrar registrar;
238 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
239 content::Source<AutocompleteController>(controller));
241 while (!HasFailure() && !controller->done())
242 content::RunMessageLoop();
244 ASSERT_TRUE(controller->done());
247 void SetupSearchEngine() {
248 Profile* profile = browser()->profile();
249 TemplateURLService* model =
250 TemplateURLServiceFactory::GetForProfile(profile);
253 ui_test_utils::WaitForTemplateURLServiceToLoad(model);
255 ASSERT_TRUE(model->loaded());
257 TemplateURLData data;
258 data.short_name = ASCIIToUTF16(kSearchShortName);
259 data.SetKeyword(ASCIIToUTF16(kSearchKeyword));
260 data.SetURL(kSearchURL);
261 TemplateURL* template_url = new TemplateURL(data);
262 model->Add(template_url);
263 model->SetUserSelectedDefaultSearchProvider(template_url);
265 data.SetKeyword(ASCIIToUTF16(kSearchKeyword2));
266 model->Add(new TemplateURL(data));
268 // Remove built-in template urls, like google.com, bing.com etc., as they
269 // may appear as autocomplete suggests and interfere with our tests.
270 TemplateURLService::TemplateURLVector urls = model->GetTemplateURLs();
271 for (TemplateURLService::TemplateURLVector::const_iterator i = urls.begin();
274 if ((*i)->prepopulate_id() != 0)
279 void AddHistoryEntry(const TestHistoryEntry& entry, const Time& time) {
280 Profile* profile = browser()->profile();
281 HistoryService* history_service = HistoryServiceFactory::GetForProfile(
282 profile, Profile::EXPLICIT_ACCESS);
283 ASSERT_TRUE(history_service);
285 if (!history_service->BackendLoaded()) {
286 content::NotificationRegistrar registrar;
287 registrar.Add(this, chrome::NOTIFICATION_HISTORY_LOADED,
288 content::Source<Profile>(profile));
289 content::RunMessageLoop();
292 BookmarkModel* bookmark_model =
293 BookmarkModelFactory::GetForProfile(profile);
294 ASSERT_TRUE(bookmark_model);
295 test::WaitForBookmarkModelToLoad(bookmark_model);
298 // Add everything in order of time. We don't want to have a time that
299 // is "right now" or it will nondeterministically appear in the results.
300 history_service->AddPageWithDetails(url, base::UTF8ToUTF16(entry.title),
302 entry.typed_count, time, false,
303 history::SOURCE_BROWSED);
305 bookmarks::AddIfNotBookmarked(bookmark_model, url, base::string16());
306 // Wait at least for the AddPageWithDetails() call to finish.
308 content::NotificationRegistrar registrar;
309 registrar.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED,
310 content::Source<Profile>(profile));
311 content::RunMessageLoop();
312 // We don't want to return until all observers have processed this
313 // notification, because some (e.g. the in-memory history database) may do
314 // something important. Since we don't know where in the observer list we
315 // stand, just spin the message loop once more to allow the current
316 // callstack to complete.
317 content::RunAllPendingInMessageLoop();
321 void SetupHistory() {
322 // Add enough history pages containing |kSearchText| to trigger
323 // open history page url in autocomplete result.
324 for (size_t i = 0; i < arraysize(kHistoryEntries); i++) {
325 // Add everything in order of time. We don't want to have a time that
326 // is "right now" or it will nondeterministically appear in the results.
327 Time t = Time::Now() - TimeDelta::FromHours(i + 1);
328 ASSERT_NO_FATAL_FAILURE(AddHistoryEntry(kHistoryEntries[i], t));
332 void SetupHostResolver() {
333 for (size_t i = 0; i < arraysize(kBlockedHostnames); ++i)
334 host_resolver()->AddSimulatedFailure(kBlockedHostnames[i]);
337 void SetupComponents() {
338 ASSERT_NO_FATAL_FAILURE(SetupHostResolver());
339 ASSERT_NO_FATAL_FAILURE(SetupSearchEngine());
340 ASSERT_NO_FATAL_FAILURE(SetupHistory());
343 virtual void Observe(int type,
344 const content::NotificationSource& source,
345 const content::NotificationDetails& details) OVERRIDE {
347 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED:
348 case chrome::NOTIFICATION_TAB_PARENTED:
349 case chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY:
350 case chrome::NOTIFICATION_HISTORY_LOADED:
351 case chrome::NOTIFICATION_HISTORY_URLS_MODIFIED:
354 FAIL() << "Unexpected notification type";
356 base::MessageLoop::current()->Quit();
360 // Test if ctrl-* accelerators are workable in omnibox.
361 // See http://crbug.com/19193: omnibox blocks ctrl-* commands
363 // Flaky on interactive tests (dbg), http://crbug.com/69433
364 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DISABLED_BrowserAccelerators) {
365 OmniboxView* omnibox_view = NULL;
366 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
368 int tab_count = browser()->tab_strip_model()->count();
371 chrome::NewTab(browser());
372 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count + 1));
374 // Select the first Tab.
375 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_1, kCtrlOrCmdMask));
376 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
378 chrome::FocusLocationBar(browser());
380 // Select the second Tab.
381 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_2, kCtrlOrCmdMask));
382 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
384 chrome::FocusLocationBar(browser());
386 // Try ctrl-w to close a Tab.
387 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_W, kCtrlOrCmdMask));
388 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count));
390 // Try ctrl-l to focus location bar.
391 omnibox_view->SetUserText(ASCIIToUTF16("Hello world"));
392 EXPECT_FALSE(omnibox_view->IsSelectAll());
393 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_L, kCtrlOrCmdMask));
394 EXPECT_TRUE(omnibox_view->IsSelectAll());
396 // Try editing the location bar text.
397 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, 0));
398 EXPECT_FALSE(omnibox_view->IsSelectAll());
399 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_S, 0));
400 EXPECT_EQ(ASCIIToUTF16("Hello worlds"), omnibox_view->GetText());
402 // Try ctrl-x to cut text.
403 #if defined(OS_MACOSX)
404 // Mac uses alt-left/right to select a word.
405 ASSERT_NO_FATAL_FAILURE(
406 SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN));
408 ASSERT_NO_FATAL_FAILURE(
409 SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN));
411 EXPECT_FALSE(omnibox_view->IsSelectAll());
412 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_X, kCtrlOrCmdMask));
413 EXPECT_EQ(ASCIIToUTF16("Hello "), omnibox_view->GetText());
415 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
416 // Try alt-f4 to close the browser.
417 ASSERT_TRUE(SendKeyAndWait(
418 browser(), ui::VKEY_F4, ui::EF_ALT_DOWN,
419 chrome::NOTIFICATION_BROWSER_CLOSED,
420 content::Source<Browser>(browser())));
424 // Flakily fails and times out on Win only. http://crbug.com/69941
426 #define MAYBE_PopupAccelerators DISABLED_PopupAccelerators
428 #define MAYBE_PopupAccelerators PopupAccelerators
431 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_PopupAccelerators) {
433 Browser* popup = CreateBrowserForPopup(browser()->profile());
434 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup));
435 OmniboxView* omnibox_view = NULL;
436 ASSERT_NO_FATAL_FAILURE(
437 GetOmniboxViewForBrowser(popup, &omnibox_view));
438 chrome::FocusLocationBar(popup);
439 EXPECT_TRUE(omnibox_view->IsSelectAll());
441 #if !defined(OS_MACOSX)
442 // Try ctrl-w to close the popup.
443 // This piece of code doesn't work on Mac, because the Browser object won't
444 // be destroyed before finishing the current message loop iteration, thus
445 // No BROWSER_CLOSED notification will be sent.
446 ASSERT_TRUE(SendKeyAndWait(
447 popup, ui::VKEY_W, ui::EF_CONTROL_DOWN,
448 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(popup)));
450 // Create another popup.
451 popup = CreateBrowserForPopup(browser()->profile());
452 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup));
453 ASSERT_NO_FATAL_FAILURE(
454 GetOmniboxViewForBrowser(popup, &omnibox_view));
457 // Set the edit text to "Hello world".
458 omnibox_view->SetUserText(ASCIIToUTF16("Hello world"));
459 chrome::FocusLocationBar(popup);
460 EXPECT_TRUE(omnibox_view->IsSelectAll());
462 // Try editing the location bar text -- should be disallowed.
463 ASSERT_NO_FATAL_FAILURE(SendKeyForBrowser(popup, ui::VKEY_S, 0));
464 EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view->GetText());
465 EXPECT_TRUE(omnibox_view->IsSelectAll());
467 ASSERT_NO_FATAL_FAILURE(
468 SendKeyForBrowser(popup, ui::VKEY_X, kCtrlOrCmdMask));
469 EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view->GetText());
470 EXPECT_TRUE(omnibox_view->IsSelectAll());
472 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
473 // Try alt-f4 to close the popup.
474 ASSERT_TRUE(SendKeyAndWait(
475 popup, ui::VKEY_F4, ui::EF_ALT_DOWN,
476 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(popup)));
480 // http://crbug.com/133341
481 #if defined(OS_LINUX)
482 #define MAYBE_BackspaceInKeywordMode DISABLED_BackspaceInKeywordMode
484 #define MAYBE_BackspaceInKeywordMode BackspaceInKeywordMode
487 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_BackspaceInKeywordMode) {
488 OmniboxView* omnibox_view = NULL;
489 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
491 // Trigger keyword hint mode.
492 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
493 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
494 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
496 // Trigger keyword mode.
497 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
498 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
499 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
501 // Backspace without search text should bring back keyword hint mode.
502 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
503 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
504 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
506 // Trigger keyword mode again.
507 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
508 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
509 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
511 // Input something as search text.
512 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
514 // Should stay in keyword mode while deleting search text by pressing
516 for (size_t i = 0; i < arraysize(kSearchText) - 1; ++i) {
517 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
518 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
519 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
522 // Input something as search text.
523 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
525 // Move cursor to the beginning of the search text.
526 #if defined(OS_MACOSX)
527 // Home doesn't work on Mac trybot.
528 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, ui::EF_CONTROL_DOWN));
530 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_HOME, 0));
532 // Backspace at the beginning of the search text shall turn off
534 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
535 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
536 ASSERT_EQ(base::string16(), omnibox_view->model()->keyword());
537 ASSERT_EQ(std::string(kSearchKeyword) + kSearchText,
538 UTF16ToUTF8(omnibox_view->GetText()));
541 // http://crbug.com/158913
542 #if defined(OS_CHROMEOS) || defined(OS_WIN)
543 #define MAYBE_Escape DISABLED_Escape
545 #define MAYBE_Escape Escape
547 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_Escape) {
548 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIHistoryURL));
549 chrome::FocusLocationBar(browser());
551 OmniboxView* omnibox_view = NULL;
552 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
554 base::string16 old_text = omnibox_view->GetText();
555 EXPECT_FALSE(old_text.empty());
556 EXPECT_TRUE(omnibox_view->IsSelectAll());
558 // Delete all text in omnibox.
559 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
560 EXPECT_TRUE(omnibox_view->GetText().empty());
562 // Escape shall revert the text in omnibox.
563 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE, 0));
564 EXPECT_EQ(old_text, omnibox_view->GetText());
565 EXPECT_TRUE(omnibox_view->IsSelectAll());
569 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DesiredTLD) {
570 OmniboxView* omnibox_view = NULL;
571 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
572 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
573 ASSERT_TRUE(popup_model);
576 const ui::KeyboardCode kKeys[] = {
577 ui::VKEY_B, ui::VKEY_A, ui::VKEY_R, ui::VKEY_UNKNOWN
579 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys));
580 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
581 ASSERT_TRUE(popup_model->IsOpen());
582 // ctrl-Enter triggers desired_tld feature, thus www.bar.com shall be
584 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, ui::EF_CONTROL_DOWN,
585 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
586 content::Source<content::NavigationController>(
587 &browser()->tab_strip_model()->GetActiveWebContents()->
590 GURL url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
591 EXPECT_EQ("www.bar.com", url.host());
592 EXPECT_EQ("/", url.path());
595 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DesiredTLDWithTemporaryText) {
596 OmniboxView* omnibox_view = NULL;
597 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
598 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
599 ASSERT_TRUE(popup_model);
601 Profile* profile = browser()->profile();
602 TemplateURLService* template_url_service =
603 TemplateURLServiceFactory::GetForProfile(profile);
605 // Add a non-substituting keyword. This ensures the popup will have a
606 // non-verbatim entry with "ab" as a prefix. This way, by arrowing down, we
607 // can set "abc" as temporary text in the omnibox.
608 TemplateURLData data;
609 data.short_name = ASCIIToUTF16("abc");
610 data.SetKeyword(ASCIIToUTF16(kSearchText));
611 data.SetURL("http://abc.com/");
612 template_url_service->Add(new TemplateURL(data));
614 // Send "ab", so that an "abc" entry appears in the popup.
615 const ui::KeyboardCode kSearchTextPrefixKeys[] = {
616 ui::VKEY_A, ui::VKEY_B, ui::VKEY_UNKNOWN
618 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextPrefixKeys));
619 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
620 ASSERT_TRUE(popup_model->IsOpen());
622 // Arrow down to the "abc" entry in the popup.
623 size_t size = popup_model->result().size();
624 while (popup_model->selected_line() < size - 1) {
625 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
626 if (omnibox_view->GetText() == ASCIIToUTF16("abc"))
629 ASSERT_EQ(ASCIIToUTF16("abc"), omnibox_view->GetText());
631 // Hitting ctrl-enter should navigate based on the current text rather than
632 // the original input, i.e. to www.abc.com instead of www.ab.com.
633 ASSERT_TRUE(SendKeyAndWait(
634 browser(), ui::VKEY_RETURN, ui::EF_CONTROL_DOWN,
635 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
636 content::Source<content::NavigationController>(
637 &browser()->tab_strip_model()->GetActiveWebContents()->
640 GURL url(browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
641 EXPECT_EQ("www.abc.com", url.host());
642 EXPECT_EQ("/", url.path());
645 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, AltEnter) {
646 OmniboxView* omnibox_view = NULL;
647 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
649 omnibox_view->SetUserText(ASCIIToUTF16(chrome::kChromeUIHistoryURL));
650 int tab_count = browser()->tab_strip_model()->count();
651 // alt-Enter opens a new tab.
652 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RETURN, ui::EF_ALT_DOWN));
653 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count + 1));
656 // http://crbug.com/133354, http://crbug.com/146953
657 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DISABLED_EnterToSearch) {
658 OmniboxView* omnibox_view = NULL;
659 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
660 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
661 ASSERT_TRUE(popup_model);
663 // Test Enter to search.
664 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
665 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
666 ASSERT_TRUE(popup_model->IsOpen());
668 // Check if the default match result is Search Primary Provider.
669 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
670 popup_model->result().default_match()->type);
672 // Open the default match.
673 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, 0,
674 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
675 content::Source<content::NavigationController>(
676 &browser()->tab_strip_model()->GetActiveWebContents()->
678 GURL url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
679 EXPECT_EQ(kSearchTextURL, url.spec());
681 // Test that entering a single character then Enter performs a search.
682 const ui::KeyboardCode kSearchSingleCharKeys[] = {
683 ui::VKEY_Z, ui::VKEY_UNKNOWN
685 chrome::FocusLocationBar(browser());
686 EXPECT_TRUE(omnibox_view->IsSelectAll());
687 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchSingleCharKeys));
688 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
689 ASSERT_TRUE(popup_model->IsOpen());
690 EXPECT_EQ("z", UTF16ToUTF8(omnibox_view->GetText()));
692 // Check if the default match result is Search Primary Provider.
693 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
694 popup_model->result().default_match()->type);
696 // Open the default match.
697 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, 0,
698 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
699 content::Source<content::NavigationController>(
700 &browser()->tab_strip_model()->GetActiveWebContents()->
702 url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
703 EXPECT_EQ("http://www.foo.com/search?q=z", url.spec());
706 // http://crbug.com/131179
707 #if defined(OS_LINUX)
708 #define MAYBE_EscapeToDefaultMatch DISABLED_EscapeToDefaultMatch
710 #define MAYBE_EscapeToDefaultMatch EscapeToDefaultMatch
712 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_EscapeToDefaultMatch) {
713 OmniboxView* omnibox_view = NULL;
714 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
715 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
716 ASSERT_TRUE(popup_model);
718 // Input something to trigger inline autocomplete.
719 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
720 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
721 ASSERT_TRUE(popup_model->IsOpen());
723 base::string16 old_text = omnibox_view->GetText();
725 // Make sure inline autocomplete is triggerred.
726 EXPECT_GT(old_text.length(), arraysize(kInlineAutocompleteText) - 1);
728 size_t old_selected_line = popup_model->selected_line();
729 EXPECT_EQ(0U, old_selected_line);
731 // Move to another line with different text.
732 size_t size = popup_model->result().size();
733 while (popup_model->selected_line() < size - 1) {
734 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
735 ASSERT_NE(old_selected_line, popup_model->selected_line());
736 if (old_text != omnibox_view->GetText())
740 EXPECT_NE(old_text, omnibox_view->GetText());
742 // Escape shall revert back to the default match item.
743 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE, 0));
744 EXPECT_EQ(old_text, omnibox_view->GetText());
745 EXPECT_EQ(old_selected_line, popup_model->selected_line());
748 // http://crbug.com/131179, http://crbug.com/146619
749 #if defined(OS_LINUX) || defined(OS_WIN)
750 #define MAYBE_BasicTextOperations DISABLED_BasicTextOperations
752 #define MAYBE_BasicTextOperations BasicTextOperations
754 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_BasicTextOperations) {
755 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
756 chrome::FocusLocationBar(browser());
758 OmniboxView* omnibox_view = NULL;
759 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
761 base::string16 old_text = omnibox_view->GetText();
762 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), old_text);
763 EXPECT_TRUE(omnibox_view->IsSelectAll());
766 omnibox_view->GetSelectionBounds(&start, &end);
767 EXPECT_EQ(0U, start);
768 EXPECT_EQ(old_text.size(), end);
770 // Move the cursor to the end.
771 #if defined(OS_MACOSX)
772 // End doesn't work on Mac trybot.
773 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E, ui::EF_CONTROL_DOWN));
775 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END, 0));
777 EXPECT_FALSE(omnibox_view->IsSelectAll());
779 // Make sure the cursor is placed correctly.
780 omnibox_view->GetSelectionBounds(&start, &end);
781 EXPECT_EQ(old_text.size(), start);
782 EXPECT_EQ(old_text.size(), end);
784 // Insert one character at the end. Make sure we won't insert
785 // anything after the special ZWS mark used in gtk implementation.
786 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, 0));
787 EXPECT_EQ(old_text + base::char16('a'), omnibox_view->GetText());
789 // Delete one character from the end. Make sure we won't delete the special
790 // ZWS mark used in gtk implementation.
791 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
792 EXPECT_EQ(old_text, omnibox_view->GetText());
794 omnibox_view->SelectAll(true);
795 EXPECT_TRUE(omnibox_view->IsSelectAll());
796 omnibox_view->GetSelectionBounds(&start, &end);
797 EXPECT_EQ(0U, start);
798 EXPECT_EQ(old_text.size(), end);
800 // Delete the content
801 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE, 0));
802 EXPECT_TRUE(omnibox_view->IsSelectAll());
803 omnibox_view->GetSelectionBounds(&start, &end);
804 EXPECT_EQ(0U, start);
806 EXPECT_TRUE(omnibox_view->GetText().empty());
808 // Check if RevertAll() can set text and cursor correctly.
809 omnibox_view->RevertAll();
810 EXPECT_FALSE(omnibox_view->IsSelectAll());
811 EXPECT_EQ(old_text, omnibox_view->GetText());
812 omnibox_view->GetSelectionBounds(&start, &end);
813 EXPECT_EQ(old_text.size(), start);
814 EXPECT_EQ(old_text.size(), end);
817 // http://crbug.com/131179
818 #if defined(OS_LINUX)
819 #define MAYBE_AcceptKeywordBySpace DISABLED_AcceptKeywordBySpace
821 #define MAYBE_AcceptKeywordBySpace AcceptKeywordBySpace
824 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_AcceptKeywordBySpace) {
825 OmniboxView* omnibox_view = NULL;
826 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
828 base::string16 search_keyword(ASCIIToUTF16(kSearchKeyword));
830 // Trigger keyword hint mode.
831 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
832 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
833 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
834 ASSERT_EQ(search_keyword, omnibox_view->GetText());
836 // Trigger keyword mode by space.
837 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
838 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
839 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
840 ASSERT_TRUE(omnibox_view->GetText().empty());
842 // Revert to keyword hint mode.
843 omnibox_view->model()->ClearKeyword(base::string16());
844 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
845 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
846 ASSERT_EQ(search_keyword, omnibox_view->GetText());
848 // Keyword should also be accepted by typing an ideographic space.
849 omnibox_view->OnBeforePossibleChange();
850 omnibox_view->SetWindowTextAndCaretPos(search_keyword +
851 base::WideToUTF16(L"\x3000"), search_keyword.length() + 1, false, false);
852 omnibox_view->OnAfterPossibleChange();
853 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
854 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
855 ASSERT_TRUE(omnibox_view->GetText().empty());
857 // Revert to keyword hint mode.
858 omnibox_view->model()->ClearKeyword(base::string16());
859 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
860 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
861 ASSERT_EQ(search_keyword, omnibox_view->GetText());
863 // Keyword shouldn't be accepted by pressing space with a trailing
865 omnibox_view->SetWindowTextAndCaretPos(search_keyword + base::char16(' '),
866 search_keyword.length() + 1, false, false);
867 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
868 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
869 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
870 ASSERT_EQ(search_keyword + ASCIIToUTF16(" "), omnibox_view->GetText());
872 // Keyword shouldn't be accepted by deleting the trailing space.
873 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
874 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
875 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
876 ASSERT_EQ(search_keyword + base::char16(' '), omnibox_view->GetText());
878 // Keyword shouldn't be accepted by pressing space before a trailing space.
879 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
880 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
881 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
882 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
883 ASSERT_EQ(search_keyword + ASCIIToUTF16(" "), omnibox_view->GetText());
885 // Keyword should be accepted by pressing space in the middle of context and
886 // just after the keyword.
887 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
888 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, 0));
889 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
890 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
891 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
892 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
893 ASSERT_EQ(ASCIIToUTF16("a "), omnibox_view->GetText());
895 omnibox_view->GetSelectionBounds(&start, &end);
896 EXPECT_EQ(0U, start);
899 // Keyword shouldn't be accepted by pasting "foo bar".
900 omnibox_view->SetUserText(base::string16());
901 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
902 ASSERT_TRUE(omnibox_view->model()->keyword().empty());
904 omnibox_view->OnBeforePossibleChange();
905 omnibox_view->model()->OnPaste();
906 omnibox_view->SetWindowTextAndCaretPos(search_keyword +
907 ASCIIToUTF16(" bar"), search_keyword.length() + 4, false, false);
908 omnibox_view->OnAfterPossibleChange();
909 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
910 ASSERT_TRUE(omnibox_view->model()->keyword().empty());
911 ASSERT_EQ(search_keyword + ASCIIToUTF16(" bar"), omnibox_view->GetText());
913 // Keyword shouldn't be accepted for case like: "foo b|ar" -> "foo b |ar".
914 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
915 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
916 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
917 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
918 ASSERT_TRUE(omnibox_view->model()->keyword().empty());
919 ASSERT_EQ(search_keyword + ASCIIToUTF16(" b ar"), omnibox_view->GetText());
921 // Keyword could be accepted by pressing space with a selected range at the
923 omnibox_view->OnBeforePossibleChange();
924 omnibox_view->OnInlineAutocompleteTextMaybeChanged(
925 search_keyword + ASCIIToUTF16(" "), search_keyword.length());
926 omnibox_view->OnAfterPossibleChange();
927 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
928 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
929 ASSERT_EQ(search_keyword + ASCIIToUTF16(" "), omnibox_view->GetText());
931 omnibox_view->GetSelectionBounds(&start, &end);
932 ASSERT_NE(start, end);
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(base::string16(), omnibox_view->GetText());
938 // Space should accept keyword even when inline autocomplete is available.
939 omnibox_view->SetUserText(base::string16());
940 const TestHistoryEntry kHistoryFoobar = {
941 "http://www.foobar.com", "Page foobar", 100, 100, true
944 // Add a history entry to trigger inline autocomplete when typing "foo".
945 ASSERT_NO_FATAL_FAILURE(
946 AddHistoryEntry(kHistoryFoobar, Time::Now() - TimeDelta::FromHours(1)));
948 // Type "fo" to trigger inline autocomplete.
949 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordPrefixKeys));
950 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
951 ASSERT_TRUE(omnibox_view->model()->popup_model()->IsOpen());
952 ASSERT_NE(search_keyword, omnibox_view->GetText());
954 // Keyword hint shouldn't be visible.
955 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
956 ASSERT_TRUE(omnibox_view->model()->keyword().empty());
958 // Add the "o". Inline autocompletion should still happen, but now we
959 // should also get a keyword hint because we've typed a keyword exactly.
960 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordCompletionKeys));
961 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
962 ASSERT_TRUE(omnibox_view->model()->popup_model()->IsOpen());
963 ASSERT_NE(search_keyword, omnibox_view->GetText());
964 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
965 ASSERT_FALSE(omnibox_view->model()->keyword().empty());
967 // Trigger keyword mode by space.
968 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
969 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
970 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
971 ASSERT_TRUE(omnibox_view->GetText().empty());
973 // Space in the middle of a temporary text, which separates the text into
974 // keyword and replacement portions, should trigger keyword mode.
975 omnibox_view->SetUserText(base::string16());
976 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
977 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
978 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
979 ASSERT_TRUE(popup_model->IsOpen());
980 ASSERT_EQ(ASCIIToUTF16("foobar.com"), omnibox_view->GetText());
981 omnibox_view->model()->OnUpOrDownKeyPressed(1);
982 omnibox_view->model()->OnUpOrDownKeyPressed(-1);
983 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
984 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
985 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
986 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
987 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
988 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
989 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
990 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
991 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
992 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
993 ASSERT_EQ(ASCIIToUTF16("bar.com"), omnibox_view->GetText());
995 // Space after temporary text that looks like a keyword, when the original
996 // input does not look like a keyword, should trigger keyword mode.
997 omnibox_view->SetUserText(base::string16());
998 const TestHistoryEntry kHistoryFoo = {
999 "http://footest.com", "Page footest", 1000, 1000, true
1002 // Add a history entry to trigger HQP matching with text == keyword when
1004 ASSERT_NO_FATAL_FAILURE(
1005 AddHistoryEntry(kHistoryFoo, Time::Now() - TimeDelta::FromMinutes(10)));
1007 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_F, 0));
1008 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_O, 0));
1009 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
1010 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_T, 0));
1011 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E, 0));
1012 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1013 ASSERT_TRUE(popup_model->IsOpen());
1014 base::string16 search_keyword2(ASCIIToUTF16(kSearchKeyword2));
1015 while ((omnibox_view->GetText() != search_keyword2) &&
1016 (popup_model->selected_line() < popup_model->result().size() - 1))
1017 omnibox_view->model()->OnUpOrDownKeyPressed(1);
1018 ASSERT_EQ(search_keyword2, omnibox_view->GetText());
1019 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
1020 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1021 ASSERT_EQ(search_keyword2, omnibox_view->model()->keyword());
1022 ASSERT_TRUE(omnibox_view->GetText().empty());
1025 // http://crbug.com/131179
1026 #if defined(OS_LINUX)
1027 #define MAYBE_NonSubstitutingKeywordTest DISABLED_NonSubstitutingKeywordTest
1029 #define MAYBE_NonSubstitutingKeywordTest NonSubstitutingKeywordTest
1032 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_NonSubstitutingKeywordTest) {
1033 OmniboxView* omnibox_view = NULL;
1034 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1035 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1036 ASSERT_TRUE(popup_model);
1038 Profile* profile = browser()->profile();
1039 TemplateURLService* template_url_service =
1040 TemplateURLServiceFactory::GetForProfile(profile);
1042 // Add a non-default substituting keyword.
1043 TemplateURLData data;
1044 data.short_name = ASCIIToUTF16("Search abc");
1045 data.SetKeyword(ASCIIToUTF16(kSearchText));
1046 data.SetURL("http://abc.com/{searchTerms}");
1047 TemplateURL* template_url = new TemplateURL(data);
1048 template_url_service->Add(template_url);
1050 omnibox_view->SetUserText(base::string16());
1052 // Non-default substituting keyword shouldn't be matched by default.
1053 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
1054 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1055 ASSERT_TRUE(popup_model->IsOpen());
1057 // Check if the default match result is Search Primary Provider.
1058 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
1059 popup_model->result().default_match()->type);
1060 ASSERT_EQ(kSearchTextURL,
1061 popup_model->result().default_match()->destination_url.spec());
1063 omnibox_view->SetUserText(base::string16());
1064 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1065 ASSERT_FALSE(popup_model->IsOpen());
1067 // Try a non-substituting keyword.
1068 template_url_service->Remove(template_url);
1069 data.short_name = ASCIIToUTF16("abc");
1070 data.SetURL("http://abc.com/");
1071 template_url_service->Add(new TemplateURL(data));
1073 // We always allow exact matches for non-substituting keywords.
1074 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
1075 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1076 ASSERT_TRUE(popup_model->IsOpen());
1077 ASSERT_EQ(AutocompleteMatchType::HISTORY_KEYWORD,
1078 popup_model->result().default_match()->type);
1079 ASSERT_EQ("http://abc.com/",
1080 popup_model->result().default_match()->destination_url.spec());
1083 // http://crbug.com/131179 http://crbug.com/165765
1084 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_MACOSX)
1085 #define MAYBE_DeleteItem DISABLED_DeleteItem
1087 #define MAYBE_DeleteItem DeleteItem
1089 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_DeleteItem) {
1090 // Disable the search provider, to make sure the popup contains only history
1092 TemplateURLService* model =
1093 TemplateURLServiceFactory::GetForProfile(browser()->profile());
1094 model->SetUserSelectedDefaultSearchProvider(NULL);
1096 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
1097 chrome::FocusLocationBar(browser());
1099 OmniboxView* omnibox_view = NULL;
1100 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1102 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1103 ASSERT_TRUE(popup_model);
1105 base::string16 old_text = omnibox_view->GetText();
1107 // Input something that can match history items.
1108 omnibox_view->SetUserText(ASCIIToUTF16("site.com/p"));
1109 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1110 ASSERT_TRUE(popup_model->IsOpen());
1112 // Delete the inline autocomplete part.
1113 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE, 0));
1114 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1115 ASSERT_TRUE(popup_model->IsOpen());
1116 ASSERT_GE(popup_model->result().size(), 3U);
1118 base::string16 user_text = omnibox_view->GetText();
1119 ASSERT_EQ(ASCIIToUTF16("site.com/p"), user_text);
1120 omnibox_view->SelectAll(true);
1121 ASSERT_TRUE(omnibox_view->IsSelectAll());
1124 size_t default_line = popup_model->selected_line();
1125 omnibox_view->model()->OnUpOrDownKeyPressed(1);
1126 ASSERT_EQ(default_line + 1, popup_model->selected_line());
1127 base::string16 selected_text =
1128 popup_model->result().match_at(default_line + 1).fill_into_edit;
1129 // Temporary text is shown.
1130 ASSERT_EQ(selected_text, omnibox_view->GetText());
1131 ASSERT_FALSE(omnibox_view->IsSelectAll());
1134 popup_model->TryDeletingCurrentItem();
1135 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1136 // The selected line shouldn't be changed, because we have more than two
1138 ASSERT_EQ(default_line + 1, popup_model->selected_line());
1139 // Make sure the item is really deleted.
1140 ASSERT_NE(selected_text,
1141 popup_model->result().match_at(default_line + 1).fill_into_edit);
1143 popup_model->result().match_at(default_line + 1).fill_into_edit;
1144 // New temporary text is shown.
1145 ASSERT_EQ(selected_text, omnibox_view->GetText());
1147 // Revert to the default match.
1148 ASSERT_TRUE(omnibox_view->model()->OnEscapeKeyPressed());
1149 ASSERT_EQ(default_line, popup_model->selected_line());
1150 ASSERT_EQ(user_text, omnibox_view->GetText());
1151 ASSERT_TRUE(omnibox_view->IsSelectAll());
1153 // Move down and up to select the default match as temporary text.
1154 omnibox_view->model()->OnUpOrDownKeyPressed(1);
1155 ASSERT_EQ(default_line + 1, popup_model->selected_line());
1156 omnibox_view->model()->OnUpOrDownKeyPressed(-1);
1157 ASSERT_EQ(default_line, popup_model->selected_line());
1159 selected_text = popup_model->result().match_at(default_line).fill_into_edit;
1160 // New temporary text is shown.
1161 ASSERT_EQ(selected_text, omnibox_view->GetText());
1162 ASSERT_FALSE(omnibox_view->IsSelectAll());
1165 // TODO(mrossetti): http://crbug.com/82335
1166 // Delete the default item.
1167 popup_model->TryDeletingCurrentItem();
1168 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1169 // The selected line shouldn't be changed, but the default item should have
1171 ASSERT_EQ(default_line, popup_model->selected_line());
1172 // Make sure the item is really deleted.
1173 EXPECT_NE(selected_text,
1174 popup_model->result().match_at(default_line).fill_into_edit);
1176 popup_model->result().match_at(default_line).fill_into_edit;
1177 // New temporary text is shown.
1178 ASSERT_EQ(selected_text, omnibox_view->GetText());
1181 // As the current selected item is the new default item, pressing Escape key
1182 // should revert all directly.
1183 ASSERT_TRUE(omnibox_view->model()->OnEscapeKeyPressed());
1184 ASSERT_EQ(old_text, omnibox_view->GetText());
1185 ASSERT_TRUE(omnibox_view->IsSelectAll());
1188 // http://crbug.com/133344
1189 #if defined(OS_LINUX)
1190 #define MAYBE_TabAcceptKeyword DISABLED_TabAcceptKeyword
1192 #define MAYBE_TabAcceptKeyword TabAcceptKeyword
1195 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_TabAcceptKeyword) {
1196 OmniboxView* omnibox_view = NULL;
1197 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1199 base::string16 text = ASCIIToUTF16(kSearchKeyword);
1201 // Trigger keyword hint mode.
1202 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
1203 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1204 ASSERT_EQ(text, omnibox_view->model()->keyword());
1205 ASSERT_EQ(text, omnibox_view->GetText());
1207 // Trigger keyword mode by tab.
1208 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1209 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1210 ASSERT_EQ(text, omnibox_view->model()->keyword());
1211 ASSERT_TRUE(omnibox_view->GetText().empty());
1213 // Revert to keyword hint mode.
1214 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1215 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1216 ASSERT_EQ(text, omnibox_view->model()->keyword());
1217 ASSERT_EQ(text, omnibox_view->GetText());
1219 // The location bar should still have focus.
1220 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1222 // Trigger keyword mode by tab.
1223 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1224 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1225 ASSERT_EQ(text, omnibox_view->model()->keyword());
1226 ASSERT_TRUE(omnibox_view->GetText().empty());
1228 // Revert to keyword hint mode with SHIFT+TAB.
1229 #if defined(OS_MACOSX)
1230 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACKTAB, 0));
1232 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1234 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1235 ASSERT_EQ(text, omnibox_view->model()->keyword());
1236 ASSERT_EQ(text, omnibox_view->GetText());
1237 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1240 #if !defined(OS_MACOSX)
1241 // Mac intentionally does not support this behavior.
1243 // http://crbug.com/133360
1244 #if defined(OS_LINUX)
1245 #define MAYBE_TabTraverseResultsTest DISABLED_TabTraverseResultsTest
1247 #define MAYBE_TabTraverseResultsTest TabTraverseResultsTest
1250 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_TabTraverseResultsTest) {
1251 OmniboxView* omnibox_view = NULL;
1252 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1253 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1254 ASSERT_TRUE(popup_model);
1256 // Input something to trigger results.
1257 const ui::KeyboardCode kKeys[] = {
1258 ui::VKEY_B, ui::VKEY_A, ui::VKEY_R, ui::VKEY_UNKNOWN
1260 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys));
1261 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1262 ASSERT_TRUE(popup_model->IsOpen());
1264 size_t old_selected_line = popup_model->selected_line();
1265 EXPECT_EQ(0U, old_selected_line);
1267 // Move down the results.
1268 for (size_t size = popup_model->result().size();
1269 popup_model->selected_line() < size - 1;
1270 old_selected_line = popup_model->selected_line()) {
1271 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1272 ASSERT_LT(old_selected_line, popup_model->selected_line());
1275 // Don't move past the end.
1276 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1277 ASSERT_EQ(old_selected_line, popup_model->selected_line());
1278 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1280 // Move back up the results.
1281 for (; popup_model->selected_line() > 0U;
1282 old_selected_line = popup_model->selected_line()) {
1283 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1284 ASSERT_GT(old_selected_line, popup_model->selected_line());
1287 // Don't move past the beginning.
1288 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1289 ASSERT_EQ(0U, popup_model->selected_line());
1290 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1292 const TestHistoryEntry kHistoryFoo = {
1293 "http://foo/", "Page foo", 1, 1, false
1296 // Add a history entry so "foo" gets multiple matches.
1297 ASSERT_NO_FATAL_FAILURE(
1298 AddHistoryEntry(kHistoryFoo, Time::Now() - TimeDelta::FromHours(1)));
1301 ASSERT_NO_FATAL_FAILURE(omnibox_view->SelectAll(false));
1302 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
1303 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1305 // Trigger keyword mode by tab.
1306 base::string16 text = ASCIIToUTF16(kSearchKeyword);
1307 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1308 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1309 ASSERT_EQ(text, omnibox_view->model()->keyword());
1310 ASSERT_TRUE(omnibox_view->GetText().empty());
1312 // The location bar should still have focus.
1313 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1315 // Pressing tab again should move to the next result and clear keyword
1317 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1318 ASSERT_EQ(1U, omnibox_view->model()->popup_model()->selected_line());
1319 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1320 ASSERT_NE(text, omnibox_view->model()->keyword());
1322 // The location bar should still have focus.
1323 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1325 // Moving back up should not show keyword mode.
1326 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1327 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1328 ASSERT_EQ(text, omnibox_view->model()->keyword());
1330 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1335 // http://crbug.com/133347
1336 #if defined(OS_LINUX)
1337 #define MAYBE_PersistKeywordModeOnTabSwitch DISABLED_PersistKeywordModeOnTabSwitch
1339 #define MAYBE_PersistKeywordModeOnTabSwitch PersistKeywordModeOnTabSwitch
1342 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1343 MAYBE_PersistKeywordModeOnTabSwitch) {
1344 OmniboxView* omnibox_view = NULL;
1345 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1347 // Trigger keyword hint mode.
1348 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
1349 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1350 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
1352 // Trigger keyword mode.
1353 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1354 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1355 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
1357 // Input something as search text.
1358 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
1360 // Create a new tab.
1361 chrome::NewTab(browser());
1363 // Switch back to the first tab.
1364 browser()->tab_strip_model()->ActivateTabAt(0, true);
1366 // Make sure we're still in keyword mode.
1367 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
1370 // http://crbug.com/133355
1371 #if defined(OS_LINUX)
1372 #define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest DISABLED_CtrlKeyPressedWithInlineAutocompleteTest
1374 #define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest CtrlKeyPressedWithInlineAutocompleteTest
1377 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1378 MAYBE_CtrlKeyPressedWithInlineAutocompleteTest) {
1379 OmniboxView* omnibox_view = NULL;
1380 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1381 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1382 ASSERT_TRUE(popup_model);
1384 // Input something to trigger inline autocomplete.
1385 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
1386 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1387 ASSERT_TRUE(popup_model->IsOpen());
1389 base::string16 old_text = omnibox_view->GetText();
1391 // Make sure inline autocomplete is triggerred.
1392 EXPECT_GT(old_text.length(), arraysize(kInlineAutocompleteText) - 1);
1395 omnibox_view->model()->OnControlKeyChanged(true);
1397 // Inline autocomplete should still be there.
1398 EXPECT_EQ(old_text, omnibox_view->GetText());
1401 #if defined(TOOLKIT_VIEWS)
1402 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, UndoRedo) {
1403 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
1404 chrome::FocusLocationBar(browser());
1406 OmniboxView* omnibox_view = NULL;
1407 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1409 base::string16 old_text = omnibox_view->GetText();
1410 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), old_text);
1411 EXPECT_TRUE(omnibox_view->IsSelectAll());
1413 // Delete the text, then undo.
1414 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1415 EXPECT_TRUE(omnibox_view->GetText().empty());
1416 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1417 EXPECT_EQ(old_text, omnibox_view->GetText());
1419 // Redo should delete the text again.
1420 ASSERT_NO_FATAL_FAILURE(
1421 SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN));
1422 EXPECT_TRUE(omnibox_view->GetText().empty());
1424 // Looks like the undo manager doesn't support restoring selection.
1425 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1426 EXPECT_FALSE(omnibox_view->IsSelectAll());
1428 // The cursor should be at the end.
1430 omnibox_view->GetSelectionBounds(&start, &end);
1431 EXPECT_EQ(old_text.size(), start);
1432 EXPECT_EQ(old_text.size(), end);
1434 // Delete three characters; "about:bl" should not trigger inline autocomplete.
1435 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1436 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1437 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1438 EXPECT_EQ(old_text.substr(0, old_text.size() - 3), omnibox_view->GetText());
1441 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1442 EXPECT_EQ(old_text, omnibox_view->GetText());
1445 ASSERT_NO_FATAL_FAILURE(
1446 SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN));
1447 EXPECT_EQ(old_text.substr(0, old_text.size() - 3), omnibox_view->GetText());
1449 // Delete everything.
1450 omnibox_view->SelectAll(true);
1451 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1452 EXPECT_TRUE(omnibox_view->GetText().empty());
1454 // Undo delete everything.
1455 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1456 EXPECT_EQ(old_text.substr(0, old_text.size() - 3), omnibox_view->GetText());
1458 // Undo delete two characters.
1459 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1460 EXPECT_EQ(old_text, omnibox_view->GetText());
1463 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, BackspaceDeleteHalfWidthKatakana) {
1464 OmniboxView* omnibox_view = NULL;
1465 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1466 // Insert text: ダ
1467 omnibox_view->SetUserText(base::UTF8ToUTF16("\357\276\200\357\276\236"));
1469 // Move the cursor to the end.
1470 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END, 0));
1472 // Backspace should delete one character.
1473 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1474 EXPECT_EQ(base::UTF8ToUTF16("\357\276\200"), omnibox_view->GetText());
1476 #endif // defined(TOOLKIT_VIEWS)
1478 // Flaky test. crbug.com/356850
1479 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1480 DISABLED_DoesNotUpdateAutocompleteOnBlur) {
1481 OmniboxView* omnibox_view = NULL;
1482 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1483 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1484 ASSERT_TRUE(popup_model);
1486 // Input something to trigger inline autocomplete.
1487 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
1488 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1489 ASSERT_TRUE(popup_model->IsOpen());
1491 omnibox_view->GetSelectionBounds(&start, &end);
1492 EXPECT_TRUE(start != end);
1493 base::string16 old_autocomplete_text =
1494 omnibox_view->model()->autocomplete_controller()->input_.text();
1496 // Unfocus the omnibox. This should clear the text field selection and
1497 // close the popup, but should not run autocomplete.
1498 // Note: GTK preserves the selection when the omnibox is unfocused.
1499 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
1500 ASSERT_FALSE(popup_model->IsOpen());
1501 omnibox_view->GetSelectionBounds(&start, &end);
1502 EXPECT_TRUE(start == end);
1504 EXPECT_EQ(old_autocomplete_text,
1505 omnibox_view->model()->autocomplete_controller()->input_.text());
1508 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, Paste) {
1509 OmniboxView* omnibox_view = NULL;
1510 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1511 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1512 ASSERT_TRUE(popup_model);
1513 EXPECT_FALSE(popup_model->IsOpen());
1515 // Paste should yield the expected text and open the popup.
1516 SetClipboardText(ASCIIToUTF16(kSearchText));
1517 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
1518 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1519 EXPECT_EQ(ASCIIToUTF16(kSearchText), omnibox_view->GetText());
1520 EXPECT_TRUE(popup_model->IsOpen());
1522 // Close the popup and select all.
1523 omnibox_view->CloseOmniboxPopup();
1524 omnibox_view->SelectAll(false);
1525 EXPECT_FALSE(popup_model->IsOpen());
1527 // Pasting the same text again over itself should re-open the popup.
1528 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
1529 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1530 EXPECT_EQ(ASCIIToUTF16(kSearchText), omnibox_view->GetText());
1531 EXPECT_TRUE(popup_model->IsOpen());
1532 omnibox_view->CloseOmniboxPopup();
1533 EXPECT_FALSE(popup_model->IsOpen());
1535 // Pasting amid text should yield the expected text and re-open the popup.
1536 omnibox_view->SetWindowTextAndCaretPos(ASCIIToUTF16("abcd"), 2, false, false);
1537 SetClipboardText(ASCIIToUTF16("123"));
1538 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
1539 EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view->GetText());
1540 EXPECT_TRUE(popup_model->IsOpen());
1542 // Ctrl/Cmd+Alt+V should not paste.
1543 ASSERT_NO_FATAL_FAILURE(
1544 SendKey(ui::VKEY_V, kCtrlOrCmdMask | ui::EF_ALT_DOWN));
1545 EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view->GetText());
1546 // TODO(msw): Test that AltGr+V does not paste.
1549 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CopyURLToClipboard) {
1550 // Set permanent text thus making sure that omnibox treats 'google.com'
1551 // as URL (not as ordinary user input).
1552 TestToolbarModel* test_toolbar_model = new TestToolbarModel;
1553 scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
1554 test_toolbar_model->set_text(ASCIIToUTF16("http://www.google.com/"));
1555 browser()->swap_toolbar_models(&toolbar_model);
1556 OmniboxView* omnibox_view = NULL;
1557 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1558 OmniboxEditModel* edit_model = omnibox_view->model();
1559 ASSERT_NE(static_cast<OmniboxEditModel*>(NULL), edit_model);
1560 edit_model->UpdatePermanentText();
1562 const char* target_url = "http://www.google.com/calendar";
1563 omnibox_view->SetUserText(ASCIIToUTF16(target_url));
1565 // Location bar must have focus.
1566 chrome::FocusLocationBar(browser());
1567 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1569 // Select full URL and copy it to clipboard. General text and html should
1571 omnibox_view->SelectAll(true);
1572 EXPECT_TRUE(omnibox_view->IsSelectAll());
1573 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1574 clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1575 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY));
1576 EXPECT_EQ(ASCIIToUTF16(target_url), omnibox_view->GetText());
1577 EXPECT_TRUE(clipboard->IsFormatAvailable(
1578 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1580 // Make sure HTML format isn't written. See
1581 // BookmarkNodeData::WriteToClipboard() for details.
1582 EXPECT_FALSE(clipboard->IsFormatAvailable(
1583 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1585 // These platforms should read bookmark format.
1586 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1587 base::string16 title;
1589 clipboard->ReadBookmark(&title, &url);
1590 EXPECT_EQ(target_url, url);
1591 EXPECT_EQ(ASCIIToUTF16(target_url), title);
1595 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CutURLToClipboard) {
1596 // Set permanent text thus making sure that omnibox treats 'google.com'
1597 // as URL (not as ordinary user input).
1598 TestToolbarModel* test_toolbar_model = new TestToolbarModel;
1599 scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
1600 test_toolbar_model->set_text(ASCIIToUTF16("http://www.google.com/"));
1601 browser()->swap_toolbar_models(&toolbar_model);
1602 OmniboxView* omnibox_view = NULL;
1603 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1604 OmniboxEditModel* edit_model = omnibox_view->model();
1605 ASSERT_NE(static_cast<OmniboxEditModel*>(NULL), edit_model);
1606 edit_model->UpdatePermanentText();
1608 const char* target_url = "http://www.google.com/calendar";
1609 omnibox_view->SetUserText(ASCIIToUTF16(target_url));
1611 // Location bar must have focus.
1612 chrome::FocusLocationBar(browser());
1613 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1615 // Select full URL and cut it. General text and html should be available
1616 // in the clipboard.
1617 omnibox_view->SelectAll(true);
1618 EXPECT_TRUE(omnibox_view->IsSelectAll());
1619 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1620 clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1621 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT));
1622 EXPECT_EQ(base::string16(), omnibox_view->GetText());
1623 EXPECT_TRUE(clipboard->IsFormatAvailable(
1624 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1626 // Make sure HTML format isn't written. See
1627 // BookmarkNodeData::WriteToClipboard() for details.
1628 EXPECT_FALSE(clipboard->IsFormatAvailable(
1629 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1631 // These platforms should read bookmark format.
1632 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1633 base::string16 title;
1635 clipboard->ReadBookmark(&title, &url);
1636 EXPECT_EQ(target_url, url);
1637 EXPECT_EQ(ASCIIToUTF16(target_url), title);
1641 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CopyTextToClipboard) {
1642 OmniboxView* omnibox_view = NULL;
1643 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1644 const char* target_text = "foo";
1645 omnibox_view->SetUserText(ASCIIToUTF16(target_text));
1647 // Location bar must have focus.
1648 chrome::FocusLocationBar(browser());
1649 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1651 // Select full text and copy it to the clipboard.
1652 omnibox_view->SelectAll(true);
1653 EXPECT_TRUE(omnibox_view->IsSelectAll());
1654 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1655 clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1656 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY));
1657 EXPECT_TRUE(clipboard->IsFormatAvailable(
1658 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1659 EXPECT_FALSE(clipboard->IsFormatAvailable(
1660 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1661 EXPECT_EQ(ASCIIToUTF16(target_text), omnibox_view->GetText());
1664 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CutTextToClipboard) {
1665 OmniboxView* omnibox_view = NULL;
1666 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1667 const char* target_text = "foo";
1668 omnibox_view->SetUserText(ASCIIToUTF16(target_text));
1670 // Location bar must have focus.
1671 chrome::FocusLocationBar(browser());
1672 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1674 // Select full text and cut it to the clipboard.
1675 omnibox_view->SelectAll(true);
1676 EXPECT_TRUE(omnibox_view->IsSelectAll());
1677 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1678 clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1679 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT));
1680 EXPECT_TRUE(clipboard->IsFormatAvailable(
1681 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1682 EXPECT_FALSE(clipboard->IsFormatAvailable(
1683 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1684 EXPECT_EQ(base::string16(), omnibox_view->GetText());
1687 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, EditSearchEngines) {
1688 // Disable settings-in-a-window to simplify test.
1689 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1690 ::switches::kDisableSettingsWindow);
1691 OmniboxView* omnibox_view = NULL;
1692 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1693 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_EDIT_SEARCH_ENGINES));
1694 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1695 const std::string target_url =
1696 std::string(chrome::kChromeUISettingsURL) + chrome::kSearchEnginesSubPage;
1697 EXPECT_EQ(ASCIIToUTF16(target_url), omnibox_view->GetText());
1698 EXPECT_FALSE(omnibox_view->model()->popup_model()->IsOpen());
1701 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, BeginningShownAfterBlur) {
1702 OmniboxView* omnibox_view = NULL;
1703 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1705 omnibox_view->OnBeforePossibleChange();
1706 omnibox_view->SetWindowTextAndCaretPos(ASCIIToUTF16("data:text/plain,test"),
1708 omnibox_view->OnAfterPossibleChange();
1709 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1711 omnibox_view->GetSelectionBounds(&start, &end);
1712 ASSERT_EQ(5U, start);
1715 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
1716 ASSERT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1718 omnibox_view->GetSelectionBounds(&start, &end);
1719 ASSERT_EQ(0U, start);
1723 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CtrlArrowAfterArrowSuggestions) {
1724 OmniboxView* omnibox_view = NULL;
1725 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1726 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1727 ASSERT_TRUE(popup_model);
1729 // Input something to trigger results.
1730 const ui::KeyboardCode kKeys[] = {
1731 ui::VKEY_B, ui::VKEY_A, ui::VKEY_R, ui::VKEY_UNKNOWN
1733 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys));
1734 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1735 ASSERT_TRUE(popup_model->IsOpen());
1737 ASSERT_EQ(ASCIIToUTF16("bar.com/1"), omnibox_view->GetText());
1739 // Arrow down on a suggestion, and omnibox text should be the suggestion.
1740 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
1741 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1742 ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view->GetText());
1744 // Highlight the last 2 words and the omnibox text should not change.
1745 // Simulating Ctrl-shift-left only once does not seem to highlight anything
1747 #if defined(OS_MACOSX)
1748 // Mac uses alt-left/right to select a word.
1749 const int modifiers = ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN;
1751 const int modifiers = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN;
1753 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, modifiers));
1754 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, modifiers));
1755 ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view->GetText());
1758 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1759 PersistSearchReplacementAcrossTabSwitch) {
1760 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
1761 browser()->toolbar_model()->set_url_replacement_enabled(false);
1763 // Create a new tab.
1764 chrome::NewTab(browser());
1765 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
1767 // Switch back to the first tab.
1768 browser()->tab_strip_model()->ActivateTabAt(0, true);
1769 EXPECT_FALSE(browser()->toolbar_model()->url_replacement_enabled());
1772 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1773 DontUpdateURLWhileSearchTermReplacementIsDisabled) {
1774 OmniboxView* omnibox_view = NULL;
1775 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1776 TestToolbarModel* test_toolbar_model = new TestToolbarModel;
1777 scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
1778 browser()->swap_toolbar_models(&toolbar_model);
1780 base::string16 url_a(ASCIIToUTF16("http://www.a.com/"));
1781 base::string16 url_b(ASCIIToUTF16("http://www.b.com/"));
1782 base::string16 url_c(ASCIIToUTF16("http://www.c.com/"));
1783 chrome::FocusLocationBar(browser());
1784 test_toolbar_model->set_text(url_a);
1785 omnibox_view->Update();
1786 EXPECT_EQ(url_a, omnibox_view->GetText());
1788 // Disable URL replacement and update. Because the omnibox has focus, the
1789 // visible text shouldn't change; see comments in
1790 // OmniboxEditModel::UpdatePermanentText().
1791 browser()->toolbar_model()->set_url_replacement_enabled(false);
1792 test_toolbar_model->set_text(url_b);
1793 omnibox_view->Update();
1794 EXPECT_EQ(url_a, omnibox_view->GetText());
1796 // Re-enable URL replacement and ensure updating changes the text.
1797 browser()->toolbar_model()->set_url_replacement_enabled(true);
1798 // We have to change the toolbar model text here, or Update() will do nothing.
1799 // This is because the previous update already updated the permanent text.
1800 test_toolbar_model->set_text(url_c);
1801 omnibox_view->Update();
1802 EXPECT_EQ(url_c, omnibox_view->GetText());
1804 // The same test, but using RevertAll() to reset search term replacement.
1805 test_toolbar_model->set_text(url_a);
1806 omnibox_view->Update();
1807 EXPECT_EQ(url_a, omnibox_view->GetText());
1808 browser()->toolbar_model()->set_url_replacement_enabled(false);
1809 test_toolbar_model->set_text(url_b);
1810 omnibox_view->Update();
1811 EXPECT_EQ(url_a, omnibox_view->GetText());
1812 omnibox_view->RevertAll();
1813 EXPECT_EQ(url_b, omnibox_view->GetText());
1814 test_toolbar_model->set_text(url_c);
1815 omnibox_view->Update();
1816 EXPECT_EQ(url_c, omnibox_view->GetText());
1819 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, EscDisablesSearchTermReplacement) {
1820 browser()->toolbar_model()->set_url_replacement_enabled(true);
1821 chrome::FocusLocationBar(browser());
1822 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE, 0));
1823 EXPECT_FALSE(browser()->toolbar_model()->url_replacement_enabled());