[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / omnibox / omnibox_view_browsertest.cc
blob2cde269e8e600e9773e63afd41b3767c0fb99735
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.
5 #include <stdio.h>
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/browser/bookmark_model.h"
37 #include "components/bookmarks/browser/bookmark_utils.h"
38 #include "components/bookmarks/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;
50 using base::Time;
51 using base::TimeDelta;
53 namespace {
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[] = {
71 "foo",
72 "*.foo.com",
73 "bar",
74 "*.bar.com",
75 "abc",
76 "*.abc.com",
77 "def",
78 "*.def.com",
79 "*.site.com",
80 "history",
81 "z"
84 const struct TestHistoryEntry {
85 const char* url;
86 const char* title;
87 int visit_count;
88 int typed_count;
89 bool starred;
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
109 // name as the .com.
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;
122 #else
123 const int kCtrlOrCmdMask = ui::EF_CONTROL_DOWN;
124 #endif
126 } // namespace
128 class OmniboxViewTest : public InProcessBrowserTest,
129 public content::NotificationObserver {
130 protected:
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();
142 ASSERT_TRUE(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,
155 int modifiers) {
156 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
157 browser, key,
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,
175 int modifiers,
176 int type,
177 const content::NotificationSource& source)
178 WARN_UNUSED_RESULT {
179 return ui_test_utils::SendKeyPressAndWait(
180 browser, key,
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,
185 type, source);
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)
192 return;
194 content::NotificationRegistrar registrar;
195 registrar.Add(this,
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())
222 return;
224 content::NotificationRegistrar registrar;
225 registrar.Add(this,
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);
239 ASSERT_TRUE(model);
241 ui_test_utils::WaitForTemplateURLServiceToLoad(model);
243 ASSERT_TRUE(model->loaded());
245 TemplateURLData data;
246 data.short_name = ASCIIToUTF16(kSearchShortName);
247 data.SetKeyword(ASCIIToUTF16(kSearchKeyword));
248 data.SetURL(kSearchURL);
249 TemplateURL* template_url = new TemplateURL(profile, data);
250 model->Add(template_url);
251 model->SetUserSelectedDefaultSearchProvider(template_url);
253 data.SetKeyword(ASCIIToUTF16(kSearchKeyword2));
254 model->Add(new TemplateURL(profile, data));
256 // Remove built-in template urls, like google.com, bing.com etc., as they
257 // may appear as autocomplete suggests and interfere with our tests.
258 TemplateURLService::TemplateURLVector urls = model->GetTemplateURLs();
259 for (TemplateURLService::TemplateURLVector::const_iterator i = urls.begin();
260 i != urls.end();
261 ++i) {
262 if ((*i)->prepopulate_id() != 0)
263 model->Remove(*i);
267 void AddHistoryEntry(const TestHistoryEntry& entry, const Time& time) {
268 Profile* profile = browser()->profile();
269 HistoryService* history_service = HistoryServiceFactory::GetForProfile(
270 profile, Profile::EXPLICIT_ACCESS);
271 ASSERT_TRUE(history_service);
273 if (!history_service->BackendLoaded()) {
274 content::NotificationRegistrar registrar;
275 registrar.Add(this, chrome::NOTIFICATION_HISTORY_LOADED,
276 content::Source<Profile>(profile));
277 content::RunMessageLoop();
280 BookmarkModel* bookmark_model =
281 BookmarkModelFactory::GetForProfile(profile);
282 ASSERT_TRUE(bookmark_model);
283 test::WaitForBookmarkModelToLoad(bookmark_model);
285 GURL url(entry.url);
286 // Add everything in order of time. We don't want to have a time that
287 // is "right now" or it will nondeterministically appear in the results.
288 history_service->AddPageWithDetails(url, base::UTF8ToUTF16(entry.title),
289 entry.visit_count,
290 entry.typed_count, time, false,
291 history::SOURCE_BROWSED);
292 if (entry.starred)
293 bookmark_utils::AddIfNotBookmarked(bookmark_model, url, base::string16());
294 // Wait at least for the AddPageWithDetails() call to finish.
296 content::NotificationRegistrar registrar;
297 registrar.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED,
298 content::Source<Profile>(profile));
299 content::RunMessageLoop();
300 // We don't want to return until all observers have processed this
301 // notification, because some (e.g. the in-memory history database) may do
302 // something important. Since we don't know where in the observer list we
303 // stand, just spin the message loop once more to allow the current
304 // callstack to complete.
305 content::RunAllPendingInMessageLoop();
309 void SetupHistory() {
310 // Add enough history pages containing |kSearchText| to trigger
311 // open history page url in autocomplete result.
312 for (size_t i = 0; i < arraysize(kHistoryEntries); i++) {
313 // Add everything in order of time. We don't want to have a time that
314 // is "right now" or it will nondeterministically appear in the results.
315 Time t = Time::Now() - TimeDelta::FromHours(i + 1);
316 ASSERT_NO_FATAL_FAILURE(AddHistoryEntry(kHistoryEntries[i], t));
320 void SetupHostResolver() {
321 for (size_t i = 0; i < arraysize(kBlockedHostnames); ++i)
322 host_resolver()->AddSimulatedFailure(kBlockedHostnames[i]);
325 void SetupComponents() {
326 ASSERT_NO_FATAL_FAILURE(SetupHostResolver());
327 ASSERT_NO_FATAL_FAILURE(SetupSearchEngine());
328 ASSERT_NO_FATAL_FAILURE(SetupHistory());
331 virtual void Observe(int type,
332 const content::NotificationSource& source,
333 const content::NotificationDetails& details) OVERRIDE {
334 switch (type) {
335 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED:
336 case chrome::NOTIFICATION_TAB_PARENTED:
337 case chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY:
338 case chrome::NOTIFICATION_HISTORY_LOADED:
339 case chrome::NOTIFICATION_HISTORY_URLS_MODIFIED:
340 break;
341 default:
342 FAIL() << "Unexpected notification type";
344 base::MessageLoop::current()->Quit();
348 // Test if ctrl-* accelerators are workable in omnibox.
349 // See http://crbug.com/19193: omnibox blocks ctrl-* commands
351 // Flaky on interactive tests (dbg), http://crbug.com/69433
352 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DISABLED_BrowserAccelerators) {
353 OmniboxView* omnibox_view = NULL;
354 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
356 int tab_count = browser()->tab_strip_model()->count();
358 // Create a new Tab.
359 chrome::NewTab(browser());
360 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count + 1));
362 // Select the first Tab.
363 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_1, kCtrlOrCmdMask));
364 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
366 chrome::FocusLocationBar(browser());
368 // Select the second Tab.
369 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_2, kCtrlOrCmdMask));
370 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
372 chrome::FocusLocationBar(browser());
374 // Try ctrl-w to close a Tab.
375 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_W, kCtrlOrCmdMask));
376 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count));
378 // Try ctrl-l to focus location bar.
379 omnibox_view->SetUserText(ASCIIToUTF16("Hello world"));
380 EXPECT_FALSE(omnibox_view->IsSelectAll());
381 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_L, kCtrlOrCmdMask));
382 EXPECT_TRUE(omnibox_view->IsSelectAll());
384 // Try editing the location bar text.
385 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, 0));
386 EXPECT_FALSE(omnibox_view->IsSelectAll());
387 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_S, 0));
388 EXPECT_EQ(ASCIIToUTF16("Hello worlds"), omnibox_view->GetText());
390 // Try ctrl-x to cut text.
391 #if defined(OS_MACOSX)
392 // Mac uses alt-left/right to select a word.
393 ASSERT_NO_FATAL_FAILURE(
394 SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN));
395 #else
396 ASSERT_NO_FATAL_FAILURE(
397 SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN));
398 #endif
399 EXPECT_FALSE(omnibox_view->IsSelectAll());
400 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_X, kCtrlOrCmdMask));
401 EXPECT_EQ(ASCIIToUTF16("Hello "), omnibox_view->GetText());
403 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
404 // Try alt-f4 to close the browser.
405 ASSERT_TRUE(SendKeyAndWait(
406 browser(), ui::VKEY_F4, ui::EF_ALT_DOWN,
407 chrome::NOTIFICATION_BROWSER_CLOSED,
408 content::Source<Browser>(browser())));
409 #endif
412 // Flakily fails and times out on Win only. http://crbug.com/69941
413 #if defined(OS_WIN)
414 #define MAYBE_PopupAccelerators DISABLED_PopupAccelerators
415 #else
416 #define MAYBE_PopupAccelerators PopupAccelerators
417 #endif
419 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_PopupAccelerators) {
420 // Create a popup.
421 Browser* popup = CreateBrowserForPopup(browser()->profile());
422 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup));
423 OmniboxView* omnibox_view = NULL;
424 ASSERT_NO_FATAL_FAILURE(
425 GetOmniboxViewForBrowser(popup, &omnibox_view));
426 chrome::FocusLocationBar(popup);
427 EXPECT_TRUE(omnibox_view->IsSelectAll());
429 #if !defined(OS_MACOSX)
430 // Try ctrl-w to close the popup.
431 // This piece of code doesn't work on Mac, because the Browser object won't
432 // be destroyed before finishing the current message loop iteration, thus
433 // No BROWSER_CLOSED notification will be sent.
434 ASSERT_TRUE(SendKeyAndWait(
435 popup, ui::VKEY_W, ui::EF_CONTROL_DOWN,
436 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(popup)));
438 // Create another popup.
439 popup = CreateBrowserForPopup(browser()->profile());
440 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup));
441 ASSERT_NO_FATAL_FAILURE(
442 GetOmniboxViewForBrowser(popup, &omnibox_view));
443 #endif
445 // Set the edit text to "Hello world".
446 omnibox_view->SetUserText(ASCIIToUTF16("Hello world"));
447 chrome::FocusLocationBar(popup);
448 EXPECT_TRUE(omnibox_view->IsSelectAll());
450 // Try editing the location bar text -- should be disallowed.
451 ASSERT_NO_FATAL_FAILURE(SendKeyForBrowser(popup, ui::VKEY_S, 0));
452 EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view->GetText());
453 EXPECT_TRUE(omnibox_view->IsSelectAll());
455 ASSERT_NO_FATAL_FAILURE(
456 SendKeyForBrowser(popup, ui::VKEY_X, kCtrlOrCmdMask));
457 EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view->GetText());
458 EXPECT_TRUE(omnibox_view->IsSelectAll());
460 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
461 // Try alt-f4 to close the popup.
462 ASSERT_TRUE(SendKeyAndWait(
463 popup, ui::VKEY_F4, ui::EF_ALT_DOWN,
464 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(popup)));
465 #endif
468 // http://crbug.com/133341
469 #if defined(OS_LINUX)
470 #define MAYBE_BackspaceInKeywordMode DISABLED_BackspaceInKeywordMode
471 #else
472 #define MAYBE_BackspaceInKeywordMode BackspaceInKeywordMode
473 #endif
475 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_BackspaceInKeywordMode) {
476 OmniboxView* omnibox_view = NULL;
477 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
479 // Trigger keyword hint mode.
480 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
481 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
482 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
484 // Trigger keyword mode.
485 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
486 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
487 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
489 // Backspace without search text should bring back keyword hint mode.
490 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
491 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
492 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
494 // Trigger keyword mode again.
495 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
496 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
497 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
499 // Input something as search text.
500 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
502 // Should stay in keyword mode while deleting search text by pressing
503 // backspace.
504 for (size_t i = 0; i < arraysize(kSearchText) - 1; ++i) {
505 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
506 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
507 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
510 // Input something as search text.
511 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
513 // Move cursor to the beginning of the search text.
514 #if defined(OS_MACOSX)
515 // Home doesn't work on Mac trybot.
516 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, ui::EF_CONTROL_DOWN));
517 #else
518 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_HOME, 0));
519 #endif
520 // Backspace at the beginning of the search text shall turn off
521 // the keyword mode.
522 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
523 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
524 ASSERT_EQ(base::string16(), omnibox_view->model()->keyword());
525 ASSERT_EQ(std::string(kSearchKeyword) + kSearchText,
526 UTF16ToUTF8(omnibox_view->GetText()));
529 // http://crbug.com/158913
530 #if defined(OS_CHROMEOS) || defined(OS_WIN)
531 #define MAYBE_Escape DISABLED_Escape
532 #else
533 #define MAYBE_Escape Escape
534 #endif
535 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_Escape) {
536 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIHistoryURL));
537 chrome::FocusLocationBar(browser());
539 OmniboxView* omnibox_view = NULL;
540 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
542 base::string16 old_text = omnibox_view->GetText();
543 EXPECT_FALSE(old_text.empty());
544 EXPECT_TRUE(omnibox_view->IsSelectAll());
546 // Delete all text in omnibox.
547 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
548 EXPECT_TRUE(omnibox_view->GetText().empty());
550 // Escape shall revert the text in omnibox.
551 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE, 0));
552 EXPECT_EQ(old_text, omnibox_view->GetText());
553 EXPECT_TRUE(omnibox_view->IsSelectAll());
555 #undef MAYBE_ESCAPE
557 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DesiredTLD) {
558 OmniboxView* omnibox_view = NULL;
559 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
560 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
561 ASSERT_TRUE(popup_model);
563 // Test ctrl-Enter.
564 const wchar_t kKeys[] = { ui::VKEY_B, ui::VKEY_A, ui::VKEY_R, 0 };
565 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys));
566 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
567 ASSERT_TRUE(popup_model->IsOpen());
568 // ctrl-Enter triggers desired_tld feature, thus www.bar.com shall be
569 // opened.
570 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, ui::EF_CONTROL_DOWN,
571 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
572 content::Source<content::NavigationController>(
573 &browser()->tab_strip_model()->GetActiveWebContents()->
574 GetController())));
576 GURL url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
577 EXPECT_EQ("www.bar.com", url.host());
578 EXPECT_EQ("/", url.path());
581 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DesiredTLDWithTemporaryText) {
582 OmniboxView* omnibox_view = NULL;
583 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
584 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
585 ASSERT_TRUE(popup_model);
587 Profile* profile = browser()->profile();
588 TemplateURLService* template_url_service =
589 TemplateURLServiceFactory::GetForProfile(profile);
591 // Add a non-substituting keyword. This ensures the popup will have a
592 // non-verbatim entry with "ab" as a prefix. This way, by arrowing down, we
593 // can set "abc" as temporary text in the omnibox.
594 TemplateURLData data;
595 data.short_name = ASCIIToUTF16("abc");
596 data.SetKeyword(ASCIIToUTF16(kSearchText));
597 data.SetURL("http://abc.com/");
598 template_url_service->Add(new TemplateURL(profile, data));
600 // Send "ab", so that an "abc" entry appears in the popup.
601 const wchar_t kSearchTextPrefixKeys[] = { ui::VKEY_A, ui::VKEY_B, 0 };
602 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextPrefixKeys));
603 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
604 ASSERT_TRUE(popup_model->IsOpen());
606 // Arrow down to the "abc" entry in the popup.
607 size_t size = popup_model->result().size();
608 while (popup_model->selected_line() < size - 1) {
609 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
610 if (omnibox_view->GetText() == ASCIIToUTF16("abc"))
611 break;
613 ASSERT_EQ(ASCIIToUTF16("abc"), omnibox_view->GetText());
615 // Hitting ctrl-enter should navigate based on the current text rather than
616 // the original input, i.e. to www.abc.com instead of www.ab.com.
617 ASSERT_TRUE(SendKeyAndWait(
618 browser(), ui::VKEY_RETURN, ui::EF_CONTROL_DOWN,
619 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
620 content::Source<content::NavigationController>(
621 &browser()->tab_strip_model()->GetActiveWebContents()->
622 GetController())));
624 GURL url(browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
625 EXPECT_EQ("www.abc.com", url.host());
626 EXPECT_EQ("/", url.path());
629 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, AltEnter) {
630 OmniboxView* omnibox_view = NULL;
631 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
633 omnibox_view->SetUserText(ASCIIToUTF16(chrome::kChromeUIHistoryURL));
634 int tab_count = browser()->tab_strip_model()->count();
635 // alt-Enter opens a new tab.
636 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RETURN, ui::EF_ALT_DOWN));
637 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count + 1));
640 // http://crbug.com/133354, http://crbug.com/146953
641 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DISABLED_EnterToSearch) {
642 OmniboxView* omnibox_view = NULL;
643 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
644 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
645 ASSERT_TRUE(popup_model);
647 // Test Enter to search.
648 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
649 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
650 ASSERT_TRUE(popup_model->IsOpen());
652 // Check if the default match result is Search Primary Provider.
653 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
654 popup_model->result().default_match()->type);
656 // Open the default match.
657 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, 0,
658 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
659 content::Source<content::NavigationController>(
660 &browser()->tab_strip_model()->GetActiveWebContents()->
661 GetController())));
662 GURL url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
663 EXPECT_EQ(kSearchTextURL, url.spec());
665 // Test that entering a single character then Enter performs a search.
666 const wchar_t kSearchSingleCharKeys[] = { ui::VKEY_Z, 0 };
667 chrome::FocusLocationBar(browser());
668 EXPECT_TRUE(omnibox_view->IsSelectAll());
669 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchSingleCharKeys));
670 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
671 ASSERT_TRUE(popup_model->IsOpen());
672 EXPECT_EQ("z", UTF16ToUTF8(omnibox_view->GetText()));
674 // Check if the default match result is Search Primary Provider.
675 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
676 popup_model->result().default_match()->type);
678 // Open the default match.
679 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, 0,
680 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
681 content::Source<content::NavigationController>(
682 &browser()->tab_strip_model()->GetActiveWebContents()->
683 GetController())));
684 url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
685 EXPECT_EQ("http://www.foo.com/search?q=z", url.spec());
688 // http://crbug.com/131179
689 #if defined(OS_LINUX)
690 #define MAYBE_EscapeToDefaultMatch DISABLED_EscapeToDefaultMatch
691 #else
692 #define MAYBE_EscapeToDefaultMatch EscapeToDefaultMatch
693 #endif
694 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_EscapeToDefaultMatch) {
695 OmniboxView* omnibox_view = NULL;
696 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
697 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
698 ASSERT_TRUE(popup_model);
700 // Input something to trigger inline autocomplete.
701 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
702 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
703 ASSERT_TRUE(popup_model->IsOpen());
705 base::string16 old_text = omnibox_view->GetText();
707 // Make sure inline autocomplete is triggerred.
708 EXPECT_GT(old_text.length(), arraysize(kInlineAutocompleteText) - 1);
710 size_t old_selected_line = popup_model->selected_line();
711 EXPECT_EQ(0U, old_selected_line);
713 // Move to another line with different text.
714 size_t size = popup_model->result().size();
715 while (popup_model->selected_line() < size - 1) {
716 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
717 ASSERT_NE(old_selected_line, popup_model->selected_line());
718 if (old_text != omnibox_view->GetText())
719 break;
722 EXPECT_NE(old_text, omnibox_view->GetText());
724 // Escape shall revert back to the default match item.
725 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE, 0));
726 EXPECT_EQ(old_text, omnibox_view->GetText());
727 EXPECT_EQ(old_selected_line, popup_model->selected_line());
730 // http://crbug.com/131179, http://crbug.com/146619
731 #if defined(OS_LINUX) || defined(OS_WIN)
732 #define MAYBE_BasicTextOperations DISABLED_BasicTextOperations
733 #else
734 #define MAYBE_BasicTextOperations BasicTextOperations
735 #endif
736 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_BasicTextOperations) {
737 ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL));
738 chrome::FocusLocationBar(browser());
740 OmniboxView* omnibox_view = NULL;
741 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
743 base::string16 old_text = omnibox_view->GetText();
744 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL), old_text);
745 EXPECT_TRUE(omnibox_view->IsSelectAll());
747 size_t start, end;
748 omnibox_view->GetSelectionBounds(&start, &end);
749 EXPECT_EQ(0U, start);
750 EXPECT_EQ(old_text.size(), end);
752 // Move the cursor to the end.
753 #if defined(OS_MACOSX)
754 // End doesn't work on Mac trybot.
755 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E, ui::EF_CONTROL_DOWN));
756 #else
757 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END, 0));
758 #endif
759 EXPECT_FALSE(omnibox_view->IsSelectAll());
761 // Make sure the cursor is placed correctly.
762 omnibox_view->GetSelectionBounds(&start, &end);
763 EXPECT_EQ(old_text.size(), start);
764 EXPECT_EQ(old_text.size(), end);
766 // Insert one character at the end. Make sure we won't insert
767 // anything after the special ZWS mark used in gtk implementation.
768 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, 0));
769 EXPECT_EQ(old_text + base::char16('a'), omnibox_view->GetText());
771 // Delete one character from the end. Make sure we won't delete the special
772 // ZWS mark used in gtk implementation.
773 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
774 EXPECT_EQ(old_text, omnibox_view->GetText());
776 omnibox_view->SelectAll(true);
777 EXPECT_TRUE(omnibox_view->IsSelectAll());
778 omnibox_view->GetSelectionBounds(&start, &end);
779 EXPECT_EQ(0U, start);
780 EXPECT_EQ(old_text.size(), end);
782 // Delete the content
783 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE, 0));
784 EXPECT_TRUE(omnibox_view->IsSelectAll());
785 omnibox_view->GetSelectionBounds(&start, &end);
786 EXPECT_EQ(0U, start);
787 EXPECT_EQ(0U, end);
788 EXPECT_TRUE(omnibox_view->GetText().empty());
790 // Check if RevertAll() can set text and cursor correctly.
791 omnibox_view->RevertAll();
792 EXPECT_FALSE(omnibox_view->IsSelectAll());
793 EXPECT_EQ(old_text, omnibox_view->GetText());
794 omnibox_view->GetSelectionBounds(&start, &end);
795 EXPECT_EQ(old_text.size(), start);
796 EXPECT_EQ(old_text.size(), end);
799 // http://crbug.com/131179
800 #if defined(OS_LINUX)
801 #define MAYBE_AcceptKeywordBySpace DISABLED_AcceptKeywordBySpace
802 #else
803 #define MAYBE_AcceptKeywordBySpace AcceptKeywordBySpace
804 #endif
806 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_AcceptKeywordBySpace) {
807 OmniboxView* omnibox_view = NULL;
808 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
810 base::string16 search_keyword(ASCIIToUTF16(kSearchKeyword));
812 // Trigger keyword hint mode.
813 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
814 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
815 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
816 ASSERT_EQ(search_keyword, omnibox_view->GetText());
818 // Trigger keyword mode by space.
819 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
820 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
821 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
822 ASSERT_TRUE(omnibox_view->GetText().empty());
824 // Revert to keyword hint mode.
825 omnibox_view->model()->ClearKeyword(base::string16());
826 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
827 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
828 ASSERT_EQ(search_keyword, omnibox_view->GetText());
830 // Keyword should also be accepted by typing an ideographic space.
831 omnibox_view->OnBeforePossibleChange();
832 omnibox_view->SetWindowTextAndCaretPos(search_keyword +
833 base::WideToUTF16(L"\x3000"), search_keyword.length() + 1, false, false);
834 omnibox_view->OnAfterPossibleChange();
835 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
836 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
837 ASSERT_TRUE(omnibox_view->GetText().empty());
839 // Revert to keyword hint mode.
840 omnibox_view->model()->ClearKeyword(base::string16());
841 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
842 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
843 ASSERT_EQ(search_keyword, omnibox_view->GetText());
845 // Keyword shouldn't be accepted by pressing space with a trailing
846 // whitespace.
847 omnibox_view->SetWindowTextAndCaretPos(search_keyword + base::char16(' '),
848 search_keyword.length() + 1, false, false);
849 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
850 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
851 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
852 ASSERT_EQ(search_keyword + ASCIIToUTF16(" "), omnibox_view->GetText());
854 // Keyword shouldn't be accepted by deleting the trailing space.
855 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
856 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
857 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
858 ASSERT_EQ(search_keyword + base::char16(' '), omnibox_view->GetText());
860 // Keyword shouldn't be accepted by pressing space before a trailing space.
861 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
862 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
863 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
864 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
865 ASSERT_EQ(search_keyword + ASCIIToUTF16(" "), omnibox_view->GetText());
867 // Keyword should be accepted by pressing space in the middle of context and
868 // just after the keyword.
869 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
870 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, 0));
871 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
872 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
873 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
874 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
875 ASSERT_EQ(ASCIIToUTF16("a "), omnibox_view->GetText());
876 size_t start, end;
877 omnibox_view->GetSelectionBounds(&start, &end);
878 EXPECT_EQ(0U, start);
879 EXPECT_EQ(0U, end);
881 // Keyword shouldn't be accepted by pasting "foo bar".
882 omnibox_view->SetUserText(base::string16());
883 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
884 ASSERT_TRUE(omnibox_view->model()->keyword().empty());
886 omnibox_view->OnBeforePossibleChange();
887 omnibox_view->model()->OnPaste();
888 omnibox_view->SetWindowTextAndCaretPos(search_keyword +
889 ASCIIToUTF16(" bar"), search_keyword.length() + 4, false, false);
890 omnibox_view->OnAfterPossibleChange();
891 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
892 ASSERT_TRUE(omnibox_view->model()->keyword().empty());
893 ASSERT_EQ(search_keyword + ASCIIToUTF16(" bar"), omnibox_view->GetText());
895 // Keyword shouldn't be accepted for case like: "foo b|ar" -> "foo b |ar".
896 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
897 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
898 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
899 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
900 ASSERT_TRUE(omnibox_view->model()->keyword().empty());
901 ASSERT_EQ(search_keyword + ASCIIToUTF16(" b ar"), omnibox_view->GetText());
903 // Keyword could be accepted by pressing space with a selected range at the
904 // end of text.
905 omnibox_view->OnBeforePossibleChange();
906 omnibox_view->OnInlineAutocompleteTextMaybeChanged(
907 search_keyword + ASCIIToUTF16(" "), search_keyword.length());
908 omnibox_view->OnAfterPossibleChange();
909 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
910 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
911 ASSERT_EQ(search_keyword + ASCIIToUTF16(" "), omnibox_view->GetText());
913 omnibox_view->GetSelectionBounds(&start, &end);
914 ASSERT_NE(start, end);
915 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
916 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
917 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
918 ASSERT_EQ(base::string16(), omnibox_view->GetText());
920 // Space should accept keyword even when inline autocomplete is available.
921 omnibox_view->SetUserText(base::string16());
922 const TestHistoryEntry kHistoryFoobar = {
923 "http://www.foobar.com", "Page foobar", 100, 100, true
926 // Add a history entry to trigger inline autocomplete when typing "foo".
927 ASSERT_NO_FATAL_FAILURE(
928 AddHistoryEntry(kHistoryFoobar, Time::Now() - TimeDelta::FromHours(1)));
930 // Type "foo" to trigger inline autocomplete.
931 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
932 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
933 ASSERT_TRUE(omnibox_view->model()->popup_model()->IsOpen());
934 ASSERT_NE(search_keyword, omnibox_view->GetText());
936 // Keyword hint shouldn't be visible.
937 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
938 ASSERT_TRUE(omnibox_view->model()->keyword().empty());
940 // Trigger keyword mode by space.
941 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
942 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
943 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
944 ASSERT_TRUE(omnibox_view->GetText().empty());
946 // Space in the middle of a temporary text, which separates the text into
947 // keyword and replacement portions, should trigger keyword mode.
948 omnibox_view->SetUserText(base::string16());
949 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
950 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
951 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
952 ASSERT_TRUE(popup_model->IsOpen());
953 ASSERT_EQ(ASCIIToUTF16("foobar.com"), omnibox_view->GetText());
954 omnibox_view->model()->OnUpOrDownKeyPressed(1);
955 omnibox_view->model()->OnUpOrDownKeyPressed(-1);
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_LEFT, 0));
961 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
962 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
963 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
964 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
965 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
966 ASSERT_EQ(ASCIIToUTF16("bar.com"), omnibox_view->GetText());
968 // Space after temporary text that looks like a keyword, when the original
969 // input does not look like a keyword, should trigger keyword mode.
970 omnibox_view->SetUserText(base::string16());
971 const TestHistoryEntry kHistoryFoo = {
972 "http://footest.com", "Page footest", 1000, 1000, true
975 // Add a history entry to trigger HQP matching with text == keyword when
976 // typing "fo te".
977 ASSERT_NO_FATAL_FAILURE(
978 AddHistoryEntry(kHistoryFoo, Time::Now() - TimeDelta::FromMinutes(10)));
980 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_F, 0));
981 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_O, 0));
982 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
983 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_T, 0));
984 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E, 0));
985 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
986 ASSERT_TRUE(popup_model->IsOpen());
987 base::string16 search_keyword2(ASCIIToUTF16(kSearchKeyword2));
988 while ((omnibox_view->GetText() != search_keyword2) &&
989 (popup_model->selected_line() < popup_model->result().size() - 1))
990 omnibox_view->model()->OnUpOrDownKeyPressed(1);
991 ASSERT_EQ(search_keyword2, omnibox_view->GetText());
992 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
993 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
994 ASSERT_EQ(search_keyword2, omnibox_view->model()->keyword());
995 ASSERT_TRUE(omnibox_view->GetText().empty());
998 // http://crbug.com/131179
999 #if defined(OS_LINUX)
1000 #define MAYBE_NonSubstitutingKeywordTest DISABLED_NonSubstitutingKeywordTest
1001 #else
1002 #define MAYBE_NonSubstitutingKeywordTest NonSubstitutingKeywordTest
1003 #endif
1005 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_NonSubstitutingKeywordTest) {
1006 OmniboxView* omnibox_view = NULL;
1007 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1008 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1009 ASSERT_TRUE(popup_model);
1011 Profile* profile = browser()->profile();
1012 TemplateURLService* template_url_service =
1013 TemplateURLServiceFactory::GetForProfile(profile);
1015 // Add a non-default substituting keyword.
1016 TemplateURLData data;
1017 data.short_name = ASCIIToUTF16("Search abc");
1018 data.SetKeyword(ASCIIToUTF16(kSearchText));
1019 data.SetURL("http://abc.com/{searchTerms}");
1020 TemplateURL* template_url = new TemplateURL(profile, data);
1021 template_url_service->Add(template_url);
1023 omnibox_view->SetUserText(base::string16());
1025 // Non-default substituting keyword shouldn't be matched by default.
1026 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
1027 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1028 ASSERT_TRUE(popup_model->IsOpen());
1030 // Check if the default match result is Search Primary Provider.
1031 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
1032 popup_model->result().default_match()->type);
1033 ASSERT_EQ(kSearchTextURL,
1034 popup_model->result().default_match()->destination_url.spec());
1036 omnibox_view->SetUserText(base::string16());
1037 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1038 ASSERT_FALSE(popup_model->IsOpen());
1040 // Try a non-substituting keyword.
1041 template_url_service->Remove(template_url);
1042 data.short_name = ASCIIToUTF16("abc");
1043 data.SetURL("http://abc.com/");
1044 template_url_service->Add(new TemplateURL(profile, data));
1046 // We always allow exact matches for non-substituting keywords.
1047 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
1048 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1049 ASSERT_TRUE(popup_model->IsOpen());
1050 ASSERT_EQ(AutocompleteMatchType::HISTORY_KEYWORD,
1051 popup_model->result().default_match()->type);
1052 ASSERT_EQ("http://abc.com/",
1053 popup_model->result().default_match()->destination_url.spec());
1056 // http://crbug.com/131179 http://crbug.com/165765
1057 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_MACOSX)
1058 #define MAYBE_DeleteItem DISABLED_DeleteItem
1059 #else
1060 #define MAYBE_DeleteItem DeleteItem
1061 #endif
1062 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_DeleteItem) {
1063 // Disable the search provider, to make sure the popup contains only history
1064 // items.
1065 TemplateURLService* model =
1066 TemplateURLServiceFactory::GetForProfile(browser()->profile());
1067 model->SetUserSelectedDefaultSearchProvider(NULL);
1069 ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL));
1070 chrome::FocusLocationBar(browser());
1072 OmniboxView* omnibox_view = NULL;
1073 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1075 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1076 ASSERT_TRUE(popup_model);
1078 base::string16 old_text = omnibox_view->GetText();
1080 // Input something that can match history items.
1081 omnibox_view->SetUserText(ASCIIToUTF16("site.com/p"));
1082 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1083 ASSERT_TRUE(popup_model->IsOpen());
1085 // Delete the inline autocomplete part.
1086 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE, 0));
1087 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1088 ASSERT_TRUE(popup_model->IsOpen());
1089 ASSERT_GE(popup_model->result().size(), 3U);
1091 base::string16 user_text = omnibox_view->GetText();
1092 ASSERT_EQ(ASCIIToUTF16("site.com/p"), user_text);
1093 omnibox_view->SelectAll(true);
1094 ASSERT_TRUE(omnibox_view->IsSelectAll());
1096 // Move down.
1097 size_t default_line = popup_model->selected_line();
1098 omnibox_view->model()->OnUpOrDownKeyPressed(1);
1099 ASSERT_EQ(default_line + 1, popup_model->selected_line());
1100 base::string16 selected_text =
1101 popup_model->result().match_at(default_line + 1).fill_into_edit;
1102 // Temporary text is shown.
1103 ASSERT_EQ(selected_text, omnibox_view->GetText());
1104 ASSERT_FALSE(omnibox_view->IsSelectAll());
1106 // Delete the item.
1107 popup_model->TryDeletingCurrentItem();
1108 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1109 // The selected line shouldn't be changed, because we have more than two
1110 // items.
1111 ASSERT_EQ(default_line + 1, popup_model->selected_line());
1112 // Make sure the item is really deleted.
1113 ASSERT_NE(selected_text,
1114 popup_model->result().match_at(default_line + 1).fill_into_edit);
1115 selected_text =
1116 popup_model->result().match_at(default_line + 1).fill_into_edit;
1117 // New temporary text is shown.
1118 ASSERT_EQ(selected_text, omnibox_view->GetText());
1120 // Revert to the default match.
1121 ASSERT_TRUE(omnibox_view->model()->OnEscapeKeyPressed());
1122 ASSERT_EQ(default_line, popup_model->selected_line());
1123 ASSERT_EQ(user_text, omnibox_view->GetText());
1124 ASSERT_TRUE(omnibox_view->IsSelectAll());
1126 // Move down and up to select the default match as temporary text.
1127 omnibox_view->model()->OnUpOrDownKeyPressed(1);
1128 ASSERT_EQ(default_line + 1, popup_model->selected_line());
1129 omnibox_view->model()->OnUpOrDownKeyPressed(-1);
1130 ASSERT_EQ(default_line, popup_model->selected_line());
1132 selected_text = popup_model->result().match_at(default_line).fill_into_edit;
1133 // New temporary text is shown.
1134 ASSERT_EQ(selected_text, omnibox_view->GetText());
1135 ASSERT_FALSE(omnibox_view->IsSelectAll());
1137 #if 0
1138 // TODO(mrossetti): http://crbug.com/82335
1139 // Delete the default item.
1140 popup_model->TryDeletingCurrentItem();
1141 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1142 // The selected line shouldn't be changed, but the default item should have
1143 // been changed.
1144 ASSERT_EQ(default_line, popup_model->selected_line());
1145 // Make sure the item is really deleted.
1146 EXPECT_NE(selected_text,
1147 popup_model->result().match_at(default_line).fill_into_edit);
1148 selected_text =
1149 popup_model->result().match_at(default_line).fill_into_edit;
1150 // New temporary text is shown.
1151 ASSERT_EQ(selected_text, omnibox_view->GetText());
1152 #endif
1154 // As the current selected item is the new default item, pressing Escape key
1155 // should revert all directly.
1156 ASSERT_TRUE(omnibox_view->model()->OnEscapeKeyPressed());
1157 ASSERT_EQ(old_text, omnibox_view->GetText());
1158 ASSERT_TRUE(omnibox_view->IsSelectAll());
1161 // http://crbug.com/133344
1162 #if defined(OS_LINUX)
1163 #define MAYBE_TabAcceptKeyword DISABLED_TabAcceptKeyword
1164 #else
1165 #define MAYBE_TabAcceptKeyword TabAcceptKeyword
1166 #endif
1168 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_TabAcceptKeyword) {
1169 OmniboxView* omnibox_view = NULL;
1170 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1172 base::string16 text = ASCIIToUTF16(kSearchKeyword);
1174 // Trigger keyword hint mode.
1175 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
1176 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1177 ASSERT_EQ(text, omnibox_view->model()->keyword());
1178 ASSERT_EQ(text, omnibox_view->GetText());
1180 // Trigger keyword mode by tab.
1181 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1182 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1183 ASSERT_EQ(text, omnibox_view->model()->keyword());
1184 ASSERT_TRUE(omnibox_view->GetText().empty());
1186 // Revert to keyword hint mode.
1187 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1188 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1189 ASSERT_EQ(text, omnibox_view->model()->keyword());
1190 ASSERT_EQ(text, omnibox_view->GetText());
1192 // The location bar should still have focus.
1193 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1195 // Trigger keyword mode by tab.
1196 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1197 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1198 ASSERT_EQ(text, omnibox_view->model()->keyword());
1199 ASSERT_TRUE(omnibox_view->GetText().empty());
1201 // Revert to keyword hint mode with SHIFT+TAB.
1202 #if defined(OS_MACOSX)
1203 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACKTAB, 0));
1204 #else
1205 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1206 #endif
1207 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1208 ASSERT_EQ(text, omnibox_view->model()->keyword());
1209 ASSERT_EQ(text, omnibox_view->GetText());
1210 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1213 #if !defined(OS_MACOSX)
1214 // Mac intentionally does not support this behavior.
1216 // http://crbug.com/133360
1217 #if defined(OS_LINUX)
1218 #define MAYBE_TabTraverseResultsTest DISABLED_TabTraverseResultsTest
1219 #else
1220 #define MAYBE_TabTraverseResultsTest TabTraverseResultsTest
1221 #endif
1223 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_TabTraverseResultsTest) {
1224 OmniboxView* omnibox_view = NULL;
1225 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1226 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1227 ASSERT_TRUE(popup_model);
1229 // Input something to trigger results.
1230 const wchar_t kKeys[] = { ui::VKEY_B, ui::VKEY_A, ui::VKEY_R, 0 };
1231 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys));
1232 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1233 ASSERT_TRUE(popup_model->IsOpen());
1235 size_t old_selected_line = popup_model->selected_line();
1236 EXPECT_EQ(0U, old_selected_line);
1238 // Move down the results.
1239 for (size_t size = popup_model->result().size();
1240 popup_model->selected_line() < size - 1;
1241 old_selected_line = popup_model->selected_line()) {
1242 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1243 ASSERT_LT(old_selected_line, popup_model->selected_line());
1246 // Don't move past the end.
1247 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1248 ASSERT_EQ(old_selected_line, popup_model->selected_line());
1249 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1251 // Move back up the results.
1252 for (; popup_model->selected_line() > 0U;
1253 old_selected_line = popup_model->selected_line()) {
1254 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1255 ASSERT_GT(old_selected_line, popup_model->selected_line());
1258 // Don't move past the beginning.
1259 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1260 ASSERT_EQ(0U, popup_model->selected_line());
1261 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1263 const TestHistoryEntry kHistoryFoo = {
1264 "http://foo/", "Page foo", 1, 1, false
1267 // Add a history entry so "foo" gets multiple matches.
1268 ASSERT_NO_FATAL_FAILURE(
1269 AddHistoryEntry(kHistoryFoo, Time::Now() - TimeDelta::FromHours(1)));
1271 // Load results.
1272 ASSERT_NO_FATAL_FAILURE(omnibox_view->SelectAll(false));
1273 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
1274 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1276 // Trigger keyword mode by tab.
1277 base::string16 text = ASCIIToUTF16(kSearchKeyword);
1278 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1279 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1280 ASSERT_EQ(text, omnibox_view->model()->keyword());
1281 ASSERT_TRUE(omnibox_view->GetText().empty());
1283 // The location bar should still have focus.
1284 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1286 // Pressing tab again should move to the next result and clear keyword
1287 // mode.
1288 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1289 ASSERT_EQ(1U, omnibox_view->model()->popup_model()->selected_line());
1290 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1291 ASSERT_NE(text, omnibox_view->model()->keyword());
1293 // The location bar should still have focus.
1294 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1296 // Moving back up should not show keyword mode.
1297 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1298 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1299 ASSERT_EQ(text, omnibox_view->model()->keyword());
1301 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1303 #endif
1306 // http://crbug.com/133347
1307 #if defined(OS_LINUX)
1308 #define MAYBE_PersistKeywordModeOnTabSwitch DISABLED_PersistKeywordModeOnTabSwitch
1309 #else
1310 #define MAYBE_PersistKeywordModeOnTabSwitch PersistKeywordModeOnTabSwitch
1311 #endif
1313 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1314 MAYBE_PersistKeywordModeOnTabSwitch) {
1315 OmniboxView* omnibox_view = NULL;
1316 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1318 // Trigger keyword hint mode.
1319 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
1320 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1321 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
1323 // Trigger keyword mode.
1324 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1325 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1326 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
1328 // Input something as search text.
1329 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
1331 // Create a new tab.
1332 chrome::NewTab(browser());
1334 // Switch back to the first tab.
1335 browser()->tab_strip_model()->ActivateTabAt(0, true);
1337 // Make sure we're still in keyword mode.
1338 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
1341 // http://crbug.com/133355
1342 #if defined(OS_LINUX)
1343 #define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest DISABLED_CtrlKeyPressedWithInlineAutocompleteTest
1344 #else
1345 #define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest CtrlKeyPressedWithInlineAutocompleteTest
1346 #endif
1348 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1349 MAYBE_CtrlKeyPressedWithInlineAutocompleteTest) {
1350 OmniboxView* omnibox_view = NULL;
1351 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1352 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1353 ASSERT_TRUE(popup_model);
1355 // Input something to trigger inline autocomplete.
1356 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
1357 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1358 ASSERT_TRUE(popup_model->IsOpen());
1360 base::string16 old_text = omnibox_view->GetText();
1362 // Make sure inline autocomplete is triggerred.
1363 EXPECT_GT(old_text.length(), arraysize(kInlineAutocompleteText) - 1);
1365 // Press ctrl key.
1366 omnibox_view->model()->OnControlKeyChanged(true);
1368 // Inline autocomplete should still be there.
1369 EXPECT_EQ(old_text, omnibox_view->GetText());
1372 #if defined(TOOLKIT_VIEWS)
1373 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, UndoRedo) {
1374 ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL));
1375 chrome::FocusLocationBar(browser());
1377 OmniboxView* omnibox_view = NULL;
1378 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1380 base::string16 old_text = omnibox_view->GetText();
1381 EXPECT_EQ(base::UTF8ToUTF16(content::kAboutBlankURL), old_text);
1382 EXPECT_TRUE(omnibox_view->IsSelectAll());
1384 // Delete the text, then undo.
1385 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1386 EXPECT_TRUE(omnibox_view->GetText().empty());
1387 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1388 EXPECT_EQ(old_text, omnibox_view->GetText());
1390 // Redo should delete the text again.
1391 ASSERT_NO_FATAL_FAILURE(
1392 SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN));
1393 EXPECT_TRUE(omnibox_view->GetText().empty());
1395 // Looks like the undo manager doesn't support restoring selection.
1396 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1397 EXPECT_FALSE(omnibox_view->IsSelectAll());
1399 // The cursor should be at the end.
1400 size_t start, end;
1401 omnibox_view->GetSelectionBounds(&start, &end);
1402 EXPECT_EQ(old_text.size(), start);
1403 EXPECT_EQ(old_text.size(), end);
1405 // Delete two characters.
1406 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1407 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1408 EXPECT_EQ(old_text.substr(0, old_text.size() - 2), omnibox_view->GetText());
1410 // Undo delete.
1411 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1412 EXPECT_EQ(old_text, omnibox_view->GetText());
1414 // Redo delete.
1415 ASSERT_NO_FATAL_FAILURE(
1416 SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN));
1417 EXPECT_EQ(old_text.substr(0, old_text.size() - 2), omnibox_view->GetText());
1419 // Delete everything.
1420 omnibox_view->SelectAll(true);
1421 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1422 EXPECT_TRUE(omnibox_view->GetText().empty());
1424 // Undo delete everything.
1425 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1426 EXPECT_EQ(old_text.substr(0, old_text.size() - 2), omnibox_view->GetText());
1428 // Undo delete two characters.
1429 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1430 EXPECT_EQ(old_text, omnibox_view->GetText());
1433 // See http://crosbug.com/10306
1434 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1435 BackspaceDeleteHalfWidthKatakana) {
1436 OmniboxView* omnibox_view = NULL;
1437 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1438 // Insert text: ダ
1439 omnibox_view->SetUserText(base::UTF8ToUTF16("\357\276\200\357\276\236"));
1441 // Move the cursor to the end.
1442 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END, 0));
1444 // Backspace should delete one character.
1445 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1446 EXPECT_EQ(base::UTF8ToUTF16("\357\276\200"), omnibox_view->GetText());
1448 #endif // defined(TOOLKIT_VIEWS)
1450 // Flaky test. crbug.com/356850
1451 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1452 DISABLED_DoesNotUpdateAutocompleteOnBlur) {
1453 OmniboxView* omnibox_view = NULL;
1454 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1455 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1456 ASSERT_TRUE(popup_model);
1458 // Input something to trigger inline autocomplete.
1459 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
1460 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1461 ASSERT_TRUE(popup_model->IsOpen());
1462 size_t start, end;
1463 omnibox_view->GetSelectionBounds(&start, &end);
1464 EXPECT_TRUE(start != end);
1465 base::string16 old_autocomplete_text =
1466 omnibox_view->model()->autocomplete_controller()->input_.text();
1468 // Unfocus the omnibox. This should clear the text field selection and
1469 // close the popup, but should not run autocomplete.
1470 // Note: GTK preserves the selection when the omnibox is unfocused.
1471 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
1472 ASSERT_FALSE(popup_model->IsOpen());
1473 omnibox_view->GetSelectionBounds(&start, &end);
1474 EXPECT_TRUE(start == end);
1476 EXPECT_EQ(old_autocomplete_text,
1477 omnibox_view->model()->autocomplete_controller()->input_.text());
1480 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, Paste) {
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);
1485 EXPECT_FALSE(popup_model->IsOpen());
1487 // Paste should yield the expected text and open the popup.
1488 SetClipboardText(ASCIIToUTF16(kSearchText));
1489 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
1490 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1491 EXPECT_EQ(ASCIIToUTF16(kSearchText), omnibox_view->GetText());
1492 EXPECT_TRUE(popup_model->IsOpen());
1494 // Close the popup and select all.
1495 omnibox_view->CloseOmniboxPopup();
1496 omnibox_view->SelectAll(false);
1497 EXPECT_FALSE(popup_model->IsOpen());
1499 // Pasting the same text again over itself should re-open the popup.
1500 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
1501 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1502 EXPECT_EQ(ASCIIToUTF16(kSearchText), omnibox_view->GetText());
1503 // This fails on GTK, see http://crbug.com/131179
1504 #if !defined(TOOLKIT_GTK)
1505 EXPECT_TRUE(popup_model->IsOpen());
1506 #endif
1507 omnibox_view->CloseOmniboxPopup();
1508 EXPECT_FALSE(popup_model->IsOpen());
1510 // Pasting amid text should yield the expected text and re-open the popup.
1511 omnibox_view->SetWindowTextAndCaretPos(ASCIIToUTF16("abcd"), 2, false, false);
1512 SetClipboardText(ASCIIToUTF16("123"));
1513 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
1514 EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view->GetText());
1515 EXPECT_TRUE(popup_model->IsOpen());
1517 // Ctrl/Cmd+Alt+V should not paste.
1518 ASSERT_NO_FATAL_FAILURE(
1519 SendKey(ui::VKEY_V, kCtrlOrCmdMask | ui::EF_ALT_DOWN));
1520 EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view->GetText());
1521 // TODO(msw): Test that AltGr+V does not paste.
1524 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CopyURLToClipboard) {
1525 // Set permanent text thus making sure that omnibox treats 'google.com'
1526 // as URL (not as ordinary user input).
1527 TestToolbarModel* test_toolbar_model = new TestToolbarModel;
1528 scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
1529 test_toolbar_model->set_text(ASCIIToUTF16("http://www.google.com/"));
1530 browser()->swap_toolbar_models(&toolbar_model);
1531 OmniboxView* omnibox_view = NULL;
1532 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1533 OmniboxEditModel* edit_model = omnibox_view->model();
1534 ASSERT_NE(static_cast<OmniboxEditModel*>(NULL), edit_model);
1535 edit_model->UpdatePermanentText();
1537 const char* target_url = "http://www.google.com/calendar";
1538 omnibox_view->SetUserText(ASCIIToUTF16(target_url));
1540 // Location bar must have focus.
1541 chrome::FocusLocationBar(browser());
1542 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1544 // Select full URL and copy it to clipboard. General text and html should
1545 // be available.
1546 omnibox_view->SelectAll(true);
1547 EXPECT_TRUE(omnibox_view->IsSelectAll());
1548 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1549 clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1550 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY));
1551 EXPECT_EQ(ASCIIToUTF16(target_url), omnibox_view->GetText());
1552 EXPECT_TRUE(clipboard->IsFormatAvailable(
1553 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1555 // Make sure HTML format isn't written. See
1556 // BookmarkNodeData::WriteToClipboard() for details.
1557 EXPECT_FALSE(clipboard->IsFormatAvailable(
1558 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1560 // These platforms should read bookmark format.
1561 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1562 base::string16 title;
1563 std::string url;
1564 clipboard->ReadBookmark(&title, &url);
1565 EXPECT_EQ(target_url, url);
1566 EXPECT_EQ(ASCIIToUTF16(target_url), title);
1567 #endif
1570 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CutURLToClipboard) {
1571 // Set permanent text thus making sure that omnibox treats 'google.com'
1572 // as URL (not as ordinary user input).
1573 TestToolbarModel* test_toolbar_model = new TestToolbarModel;
1574 scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
1575 test_toolbar_model->set_text(ASCIIToUTF16("http://www.google.com/"));
1576 browser()->swap_toolbar_models(&toolbar_model);
1577 OmniboxView* omnibox_view = NULL;
1578 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1579 OmniboxEditModel* edit_model = omnibox_view->model();
1580 ASSERT_NE(static_cast<OmniboxEditModel*>(NULL), edit_model);
1581 edit_model->UpdatePermanentText();
1583 const char* target_url = "http://www.google.com/calendar";
1584 omnibox_view->SetUserText(ASCIIToUTF16(target_url));
1586 // Location bar must have focus.
1587 chrome::FocusLocationBar(browser());
1588 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1590 // Select full URL and cut it. General text and html should be available
1591 // in the clipboard.
1592 omnibox_view->SelectAll(true);
1593 EXPECT_TRUE(omnibox_view->IsSelectAll());
1594 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1595 clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1596 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT));
1597 EXPECT_EQ(base::string16(), omnibox_view->GetText());
1598 EXPECT_TRUE(clipboard->IsFormatAvailable(
1599 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1601 // Make sure HTML format isn't written. See
1602 // BookmarkNodeData::WriteToClipboard() for details.
1603 EXPECT_FALSE(clipboard->IsFormatAvailable(
1604 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1606 // These platforms should read bookmark format.
1607 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1608 base::string16 title;
1609 std::string url;
1610 clipboard->ReadBookmark(&title, &url);
1611 EXPECT_EQ(target_url, url);
1612 EXPECT_EQ(ASCIIToUTF16(target_url), title);
1613 #endif
1616 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CopyTextToClipboard) {
1617 OmniboxView* omnibox_view = NULL;
1618 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1619 const char* target_text = "foo";
1620 omnibox_view->SetUserText(ASCIIToUTF16(target_text));
1622 // Location bar must have focus.
1623 chrome::FocusLocationBar(browser());
1624 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1626 // Select full text and copy it to the clipboard.
1627 omnibox_view->SelectAll(true);
1628 EXPECT_TRUE(omnibox_view->IsSelectAll());
1629 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1630 clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1631 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY));
1632 EXPECT_TRUE(clipboard->IsFormatAvailable(
1633 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1634 EXPECT_FALSE(clipboard->IsFormatAvailable(
1635 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1636 EXPECT_EQ(ASCIIToUTF16(target_text), omnibox_view->GetText());
1639 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CutTextToClipboard) {
1640 OmniboxView* omnibox_view = NULL;
1641 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1642 const char* target_text = "foo";
1643 omnibox_view->SetUserText(ASCIIToUTF16(target_text));
1645 // Location bar must have focus.
1646 chrome::FocusLocationBar(browser());
1647 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1649 // Select full text and cut it to the clipboard.
1650 omnibox_view->SelectAll(true);
1651 EXPECT_TRUE(omnibox_view->IsSelectAll());
1652 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1653 clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1654 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT));
1655 EXPECT_TRUE(clipboard->IsFormatAvailable(
1656 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1657 EXPECT_FALSE(clipboard->IsFormatAvailable(
1658 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1659 EXPECT_EQ(base::string16(), omnibox_view->GetText());
1662 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, EditSearchEngines) {
1663 OmniboxView* omnibox_view = NULL;
1664 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1665 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_EDIT_SEARCH_ENGINES));
1666 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1667 const std::string target_url =
1668 std::string(chrome::kChromeUISettingsURL) + chrome::kSearchEnginesSubPage;
1669 EXPECT_EQ(ASCIIToUTF16(target_url), omnibox_view->GetText());
1670 EXPECT_FALSE(omnibox_view->model()->popup_model()->IsOpen());
1673 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, BeginningShownAfterBlur) {
1674 OmniboxView* omnibox_view = NULL;
1675 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1677 omnibox_view->OnBeforePossibleChange();
1678 omnibox_view->SetWindowTextAndCaretPos(ASCIIToUTF16("data:text/plain,test"),
1679 5U, false, false);
1680 omnibox_view->OnAfterPossibleChange();
1681 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1682 size_t start, end;
1683 omnibox_view->GetSelectionBounds(&start, &end);
1684 ASSERT_EQ(5U, start);
1685 ASSERT_EQ(5U, end);
1687 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
1688 ASSERT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1690 omnibox_view->GetSelectionBounds(&start, &end);
1691 ASSERT_EQ(0U, start);
1692 ASSERT_EQ(0U, end);
1695 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CtrlArrowAfterArrowSuggestions) {
1696 OmniboxView* omnibox_view = NULL;
1697 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1698 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1699 ASSERT_TRUE(popup_model);
1701 // Input something to trigger results.
1702 const wchar_t kKeys[] = { ui::VKEY_B, ui::VKEY_A, ui::VKEY_R, 0 };
1703 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys));
1704 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1705 ASSERT_TRUE(popup_model->IsOpen());
1707 ASSERT_EQ(ASCIIToUTF16("bar.com/1"), omnibox_view->GetText());
1709 // Arrow down on a suggestion, and omnibox text should be the suggestion.
1710 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
1711 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1712 ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view->GetText());
1714 // Highlight the last 2 words and the omnibox text should not change.
1715 // Simulating Ctrl-shift-left only once does not seem to highlight anything
1716 // on Linux.
1717 #if defined(OS_MACOSX)
1718 // Mac uses alt-left/right to select a word.
1719 const int modifiers = ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN;
1720 #else
1721 const int modifiers = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN;
1722 #endif
1723 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, modifiers));
1724 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, modifiers));
1725 ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view->GetText());
1728 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1729 PersistSearchReplacementAcrossTabSwitch) {
1730 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
1731 browser()->toolbar_model()->set_url_replacement_enabled(false);
1733 // Create a new tab.
1734 chrome::NewTab(browser());
1735 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
1737 // Switch back to the first tab.
1738 browser()->tab_strip_model()->ActivateTabAt(0, true);
1739 EXPECT_FALSE(browser()->toolbar_model()->url_replacement_enabled());
1742 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1743 DontUpdateURLWhileSearchTermReplacementIsDisabled) {
1744 OmniboxView* omnibox_view = NULL;
1745 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1746 TestToolbarModel* test_toolbar_model = new TestToolbarModel;
1747 scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
1748 browser()->swap_toolbar_models(&toolbar_model);
1750 base::string16 url_a(ASCIIToUTF16("http://www.a.com/"));
1751 base::string16 url_b(ASCIIToUTF16("http://www.b.com/"));
1752 base::string16 url_c(ASCIIToUTF16("http://www.c.com/"));
1753 chrome::FocusLocationBar(browser());
1754 test_toolbar_model->set_text(url_a);
1755 omnibox_view->Update();
1756 EXPECT_EQ(url_a, omnibox_view->GetText());
1758 // Disable URL replacement and update. Because the omnibox has focus, the
1759 // visible text shouldn't change; see comments in
1760 // OmniboxEditModel::UpdatePermanentText().
1761 browser()->toolbar_model()->set_url_replacement_enabled(false);
1762 test_toolbar_model->set_text(url_b);
1763 omnibox_view->Update();
1764 EXPECT_EQ(url_a, omnibox_view->GetText());
1766 // Re-enable URL replacement and ensure updating changes the text.
1767 browser()->toolbar_model()->set_url_replacement_enabled(true);
1768 // We have to change the toolbar model text here, or Update() will do nothing.
1769 // This is because the previous update already updated the permanent text.
1770 test_toolbar_model->set_text(url_c);
1771 omnibox_view->Update();
1772 EXPECT_EQ(url_c, omnibox_view->GetText());
1774 // The same test, but using RevertAll() to reset search term replacement.
1775 test_toolbar_model->set_text(url_a);
1776 omnibox_view->Update();
1777 EXPECT_EQ(url_a, omnibox_view->GetText());
1778 browser()->toolbar_model()->set_url_replacement_enabled(false);
1779 test_toolbar_model->set_text(url_b);
1780 omnibox_view->Update();
1781 EXPECT_EQ(url_a, omnibox_view->GetText());
1782 omnibox_view->RevertAll();
1783 EXPECT_EQ(url_b, omnibox_view->GetText());
1784 test_toolbar_model->set_text(url_c);
1785 omnibox_view->Update();
1786 EXPECT_EQ(url_c, omnibox_view->GetText());
1789 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, EscDisablesSearchTermReplacement) {
1790 browser()->toolbar_model()->set_url_replacement_enabled(true);
1791 chrome::FocusLocationBar(browser());
1792 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE, 0));
1793 EXPECT_FALSE(browser()->toolbar_model()->url_replacement_enabled());