- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / omnibox / omnibox_view_browsertest.cc
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.
4
5 #include <stdio.h>
6
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.h"
16 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
17 #include "chrome/browser/bookmarks/bookmark_test_helpers.h"
18 #include "chrome/browser/bookmarks/bookmark_utils.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/history/history_service.h"
21 #include "chrome/browser/history/history_service_factory.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/search_engines/template_url.h"
24 #include "chrome/browser/search_engines/template_url_service.h"
25 #include "chrome/browser/search_engines/template_url_service_factory.h"
26 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_commands.h"
28 #include "chrome/browser/ui/browser_window.h"
29 #include "chrome/browser/ui/omnibox/location_bar.h"
30 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
31 #include "chrome/browser/ui/omnibox/omnibox_view.h"
32 #include "chrome/browser/ui/tabs/tab_strip_model.h"
33 #include "chrome/browser/ui/toolbar/test_toolbar_model.h"
34 #include "chrome/common/chrome_paths.h"
35 #include "chrome/common/url_constants.h"
36 #include "chrome/test/base/in_process_browser_test.h"
37 #include "chrome/test/base/interactive_test_utils.h"
38 #include "chrome/test/base/ui_test_utils.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"
47
48 #if defined(TOOLKIT_GTK)
49 #include <gdk/gdk.h>
50 #include <gtk/gtk.h>
51 #endif
52
53 using base::Time;
54 using base::TimeDelta;
55
56 namespace {
57
58 const char kSearchKeyword[] = "foo";
59 const char kSearchKeyword2[] = "footest.com";
60 const wchar_t kSearchKeywordKeys[] = { ui::VKEY_F, ui::VKEY_O, ui::VKEY_O, 0 };
61 const char kSearchURL[] = "http://www.foo.com/search?q={searchTerms}";
62 const char kSearchShortName[] = "foo";
63 const char kSearchText[] = "abc";
64 const wchar_t kSearchTextKeys[] = { ui::VKEY_A, ui::VKEY_B, ui::VKEY_C, 0 };
65 const char kSearchTextURL[] = "http://www.foo.com/search?q=abc";
66 const char kSearchSingleChar[] = "z";
67 const wchar_t kSearchSingleCharKeys[] = { ui::VKEY_Z, 0 };
68 const char kSearchSingleCharURL[] = "http://www.foo.com/search?q=z";
69
70 const char kDesiredTLDHostname[] = "www.bar.com";
71 const wchar_t kDesiredTLDKeys[] = { ui::VKEY_B, ui::VKEY_A, ui::VKEY_R, 0 };
72
73 const char kInlineAutocompleteText[] = "def";
74 const wchar_t kInlineAutocompleteTextKeys[] = {
75   ui::VKEY_D, ui::VKEY_E, ui::VKEY_F, 0
76 };
77
78 // Hostnames that shall be blocked by host resolver.
79 const char *kBlockedHostnames[] = {
80   "foo",
81   "*.foo.com",
82   "bar",
83   "*.bar.com",
84   "abc",
85   "*.abc.com",
86   "def",
87   "*.def.com",
88   "*.site.com",
89   "history",
90   "z"
91 };
92
93 const struct TestHistoryEntry {
94   const char* url;
95   const char* title;
96   const char* body;
97   int visit_count;
98   int typed_count;
99   bool starred;
100 } kHistoryEntries[] = {
101   {"http://www.bar.com/1", "Page 1", kSearchText, 10, 10, false },
102   {"http://www.bar.com/2", "Page 2", kSearchText, 9, 9, false },
103   {"http://www.bar.com/3", "Page 3", kSearchText, 8, 8, false },
104   {"http://www.bar.com/4", "Page 4", kSearchText, 7, 7, false },
105   {"http://www.bar.com/5", "Page 5", kSearchText, 6, 6, false },
106   {"http://www.bar.com/6", "Page 6", kSearchText, 5, 5, false },
107   {"http://www.bar.com/7", "Page 7", kSearchText, 4, 4, false },
108   {"http://www.bar.com/8", "Page 8", kSearchText, 3, 3, false },
109   {"http://www.bar.com/9", "Page 9", kSearchText, 2, 2, false },
110   {"http://www.site.com/path/1", "Site 1", kSearchText, 4, 4, false },
111   {"http://www.site.com/path/2", "Site 2", kSearchText, 3, 3, false },
112   {"http://www.site.com/path/3", "Site 3", kSearchText, 2, 2, false },
113
114   // To trigger inline autocomplete.
115   {"http://www.def.com", "Page def", kSearchText, 10000, 10000, true },
116
117   // Used in particular for the desired TLD test.  This makes it test
118   // the interesting case when there's an intranet host with the same
119   // name as the .com.
120   {"http://bar/", "Bar", kSearchText, 1, 0, false },
121 };
122
123 #if defined(TOOLKIT_GTK)
124 // Returns the text stored in the PRIMARY clipboard.
125 std::string GetPrimarySelectionText() {
126   GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
127   DCHECK(clipboard);
128
129   gchar* selection_text = gtk_clipboard_wait_for_text(clipboard);
130   std::string result(selection_text ? selection_text : "");
131   g_free(selection_text);
132   return result;
133 }
134 #endif
135
136 // Stores the given text to clipboard.
137 void SetClipboardText(const string16& text) {
138   ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
139   ui::ScopedClipboardWriter writer(clipboard, ui::CLIPBOARD_TYPE_COPY_PASTE);
140   writer.WriteText(text);
141 }
142
143 #if defined(OS_MACOSX)
144 const int kCtrlOrCmdMask = ui::EF_COMMAND_DOWN;
145 #else
146 const int kCtrlOrCmdMask = ui::EF_CONTROL_DOWN;
147 #endif
148
149 }  // namespace
150
151 class OmniboxViewTest : public InProcessBrowserTest,
152                         public content::NotificationObserver {
153  protected:
154   virtual void SetUpOnMainThread() OVERRIDE {
155     ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
156     ASSERT_NO_FATAL_FAILURE(SetupComponents());
157     chrome::FocusLocationBar(browser());
158     ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
159   }
160
161   static void GetOmniboxViewForBrowser(
162       const Browser* browser,
163       OmniboxView** omnibox_view) {
164     BrowserWindow* window = browser->window();
165     ASSERT_TRUE(window);
166     LocationBar* location_bar = window->GetLocationBar();
167     ASSERT_TRUE(location_bar);
168     *omnibox_view = location_bar->GetLocationEntry();
169     ASSERT_TRUE(*omnibox_view);
170   }
171
172   void GetOmniboxView(OmniboxView** omnibox_view) {
173     GetOmniboxViewForBrowser(browser(), omnibox_view);
174   }
175
176   static void SendKeyForBrowser(const Browser* browser,
177                                 ui::KeyboardCode key,
178                                 int modifiers) {
179     ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
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   }
186
187   void SendKey(ui::KeyboardCode key, int modifiers) {
188     SendKeyForBrowser(browser(), key, modifiers);
189   }
190
191   void SendKeySequence(const wchar_t* keys) {
192     for (; *keys; ++keys)
193       ASSERT_NO_FATAL_FAILURE(SendKey(static_cast<ui::KeyboardCode>(*keys), 0));
194   }
195
196   bool SendKeyAndWait(const Browser* browser,
197                       ui::KeyboardCode key,
198                       int modifiers,
199                       int type,
200                       const content::NotificationSource& source)
201                           WARN_UNUSED_RESULT {
202     return ui_test_utils::SendKeyPressAndWait(
203         browser, key,
204         (modifiers & ui::EF_CONTROL_DOWN) != 0,
205         (modifiers & ui::EF_SHIFT_DOWN) != 0,
206         (modifiers & ui::EF_ALT_DOWN) != 0,
207         (modifiers & ui::EF_COMMAND_DOWN) != 0,
208         type, source);
209   }
210
211   void WaitForTabOpenOrCloseForBrowser(const Browser* browser,
212                                        int expected_tab_count) {
213     int tab_count = browser->tab_strip_model()->count();
214     if (tab_count == expected_tab_count)
215       return;
216
217     content::NotificationRegistrar registrar;
218     registrar.Add(this,
219         (tab_count < expected_tab_count) ?
220             static_cast<int>(chrome::NOTIFICATION_TAB_PARENTED) :
221             static_cast<int>(content::NOTIFICATION_WEB_CONTENTS_DESTROYED),
222         content::NotificationService::AllSources());
223
224     while (!HasFailure() &&
225            browser->tab_strip_model()->count() != expected_tab_count) {
226       content::RunMessageLoop();
227     }
228
229     ASSERT_EQ(expected_tab_count, browser->tab_strip_model()->count());
230   }
231
232   void WaitForTabOpenOrClose(int expected_tab_count) {
233     WaitForTabOpenOrCloseForBrowser(browser(), expected_tab_count);
234   }
235
236   void WaitForAutocompleteControllerDone() {
237     OmniboxView* omnibox_view = NULL;
238     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
239
240     AutocompleteController* controller =
241         omnibox_view->model()->autocomplete_controller();
242     ASSERT_TRUE(controller);
243
244     if (controller->done())
245       return;
246
247     content::NotificationRegistrar registrar;
248     registrar.Add(this,
249                   chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
250                   content::Source<AutocompleteController>(controller));
251
252     while (!HasFailure() && !controller->done())
253       content::RunMessageLoop();
254
255     ASSERT_TRUE(controller->done());
256   }
257
258   void SetupSearchEngine() {
259     Profile* profile = browser()->profile();
260     TemplateURLService* model =
261         TemplateURLServiceFactory::GetForProfile(profile);
262     ASSERT_TRUE(model);
263
264     ui_test_utils::WaitForTemplateURLServiceToLoad(model);
265
266     ASSERT_TRUE(model->loaded());
267     // Remove built-in template urls, like google.com, bing.com etc., as they
268     // may appear as autocomplete suggests and interfere with our tests.
269     model->SetDefaultSearchProvider(NULL);
270     TemplateURLService::TemplateURLVector builtins = model->GetTemplateURLs();
271     for (TemplateURLService::TemplateURLVector::const_iterator
272          i = builtins.begin(); i != builtins.end(); ++i)
273       model->Remove(*i);
274
275     TemplateURLData data;
276     data.short_name = ASCIIToUTF16(kSearchShortName);
277     data.SetKeyword(ASCIIToUTF16(kSearchKeyword));
278     data.SetURL(kSearchURL);
279     TemplateURL* template_url = new TemplateURL(profile, data);
280     model->Add(template_url);
281     model->SetDefaultSearchProvider(template_url);
282
283     data.SetKeyword(ASCIIToUTF16(kSearchKeyword2));
284     model->Add(new TemplateURL(profile, data));
285   }
286
287   void AddHistoryEntry(const TestHistoryEntry& entry, const Time& time) {
288     Profile* profile = browser()->profile();
289     HistoryService* history_service = HistoryServiceFactory::GetForProfile(
290         profile, Profile::EXPLICIT_ACCESS);
291     ASSERT_TRUE(history_service);
292
293     if (!history_service->BackendLoaded()) {
294       content::NotificationRegistrar registrar;
295       registrar.Add(this, chrome::NOTIFICATION_HISTORY_LOADED,
296                     content::Source<Profile>(profile));
297       content::RunMessageLoop();
298     }
299
300     BookmarkModel* bookmark_model =
301         BookmarkModelFactory::GetForProfile(profile);
302     ASSERT_TRUE(bookmark_model);
303     test::WaitForBookmarkModelToLoad(bookmark_model);
304
305     GURL url(entry.url);
306     // Add everything in order of time. We don't want to have a time that
307     // is "right now" or it will nondeterministically appear in the results.
308     history_service->AddPageWithDetails(url, UTF8ToUTF16(entry.title),
309                                         entry.visit_count,
310                                         entry.typed_count, time, false,
311                                         history::SOURCE_BROWSED);
312     if (entry.starred)
313       bookmark_utils::AddIfNotBookmarked(bookmark_model, url, string16());
314     // Wait at least for the AddPageWithDetails() call to finish.
315     {
316       content::NotificationRegistrar registrar;
317       registrar.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED,
318                     content::Source<Profile>(profile));
319       content::RunMessageLoop();
320       // We don't want to return until all observers have processed this
321       // notification, because some (e.g. the in-memory history database) may do
322       // something important.  Since we don't know where in the observer list we
323       // stand, just spin the message loop once more to allow the current
324       // callstack to complete.
325       content::RunAllPendingInMessageLoop();
326     }
327   }
328
329   void SetupHistory() {
330     // Add enough history pages containing |kSearchText| to trigger
331     // open history page url in autocomplete result.
332     for (size_t i = 0; i < arraysize(kHistoryEntries); i++) {
333       // Add everything in order of time. We don't want to have a time that
334       // is "right now" or it will nondeterministically appear in the results.
335       Time t = Time::Now() - TimeDelta::FromHours(i + 1);
336       ASSERT_NO_FATAL_FAILURE(AddHistoryEntry(kHistoryEntries[i], t));
337     }
338   }
339
340   void SetupHostResolver() {
341     for (size_t i = 0; i < arraysize(kBlockedHostnames); ++i)
342       host_resolver()->AddSimulatedFailure(kBlockedHostnames[i]);
343   }
344
345   void SetupComponents() {
346     ASSERT_NO_FATAL_FAILURE(SetupHostResolver());
347     ASSERT_NO_FATAL_FAILURE(SetupSearchEngine());
348     ASSERT_NO_FATAL_FAILURE(SetupHistory());
349   }
350
351   virtual void Observe(int type,
352                        const content::NotificationSource& source,
353                        const content::NotificationDetails& details) OVERRIDE {
354     switch (type) {
355       case content::NOTIFICATION_WEB_CONTENTS_DESTROYED:
356       case chrome::NOTIFICATION_TAB_PARENTED:
357       case chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY:
358       case chrome::NOTIFICATION_HISTORY_LOADED:
359       case chrome::NOTIFICATION_HISTORY_URLS_MODIFIED:
360         break;
361       default:
362         FAIL() << "Unexpected notification type";
363     }
364     base::MessageLoop::current()->Quit();
365   }
366
367   void BrowserAcceleratorsTest() {
368     OmniboxView* omnibox_view = NULL;
369     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
370
371     int tab_count = browser()->tab_strip_model()->count();
372
373     // Create a new Tab.
374     chrome::NewTab(browser());
375     ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count + 1));
376
377     // Select the first Tab.
378     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_1, kCtrlOrCmdMask));
379     ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
380
381     chrome::FocusLocationBar(browser());
382
383     // Select the second Tab.
384     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_2, kCtrlOrCmdMask));
385     ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
386
387     chrome::FocusLocationBar(browser());
388
389     // Try ctrl-w to close a Tab.
390     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_W, kCtrlOrCmdMask));
391     ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count));
392
393     // Try ctrl-l to focus location bar.
394     omnibox_view->SetUserText(ASCIIToUTF16("Hello world"));
395     EXPECT_FALSE(omnibox_view->IsSelectAll());
396     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_L, kCtrlOrCmdMask));
397     EXPECT_TRUE(omnibox_view->IsSelectAll());
398
399     // Try editing the location bar text.
400     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, 0));
401     EXPECT_FALSE(omnibox_view->IsSelectAll());
402     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_S, 0));
403     EXPECT_EQ(ASCIIToUTF16("Hello worlds"), omnibox_view->GetText());
404
405     // Try ctrl-x to cut text.
406 #if defined(OS_MACOSX)
407     // Mac uses alt-left/right to select a word.
408     ASSERT_NO_FATAL_FAILURE(
409         SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN));
410 #else
411     ASSERT_NO_FATAL_FAILURE(
412         SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN));
413 #endif
414     EXPECT_FALSE(omnibox_view->IsSelectAll());
415     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_X, kCtrlOrCmdMask));
416     EXPECT_EQ(ASCIIToUTF16("Hello "), omnibox_view->GetText());
417
418 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
419     // Try alt-f4 to close the browser.
420     ASSERT_TRUE(SendKeyAndWait(
421         browser(), ui::VKEY_F4, ui::EF_ALT_DOWN,
422         chrome::NOTIFICATION_BROWSER_CLOSED,
423         content::Source<Browser>(browser())));
424 #endif
425   }
426
427   void PopupAcceleratorsTest() {
428     // Create a popup.
429     Browser* popup = CreateBrowserForPopup(browser()->profile());
430     ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup));
431     OmniboxView* omnibox_view = NULL;
432     ASSERT_NO_FATAL_FAILURE(
433         GetOmniboxViewForBrowser(popup, &omnibox_view));
434     chrome::FocusLocationBar(popup);
435     EXPECT_TRUE(omnibox_view->IsSelectAll());
436
437 #if !defined(OS_MACOSX)
438     // Try ctrl-w to close the popup.
439     // This piece of code doesn't work on Mac, because the Browser object won't
440     // be destroyed before finishing the current message loop iteration, thus
441     // No BROWSER_CLOSED notification will be sent.
442     ASSERT_TRUE(SendKeyAndWait(
443         popup, ui::VKEY_W, ui::EF_CONTROL_DOWN,
444         chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(popup)));
445
446     // Create another popup.
447     popup = CreateBrowserForPopup(browser()->profile());
448     ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup));
449     ASSERT_NO_FATAL_FAILURE(
450         GetOmniboxViewForBrowser(popup, &omnibox_view));
451 #endif
452
453     // Set the edit text to "Hello world".
454     omnibox_view->SetUserText(ASCIIToUTF16("Hello world"));
455     chrome::FocusLocationBar(popup);
456     EXPECT_TRUE(omnibox_view->IsSelectAll());
457
458     // Try editing the location bar text -- should be disallowed.
459     ASSERT_NO_FATAL_FAILURE(SendKeyForBrowser(popup, ui::VKEY_S, 0));
460     EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view->GetText());
461     EXPECT_TRUE(omnibox_view->IsSelectAll());
462
463     ASSERT_NO_FATAL_FAILURE(
464         SendKeyForBrowser(popup, ui::VKEY_X, kCtrlOrCmdMask));
465     EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view->GetText());
466     EXPECT_TRUE(omnibox_view->IsSelectAll());
467
468 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
469     // Try alt-f4 to close the popup.
470     ASSERT_TRUE(SendKeyAndWait(
471         popup, ui::VKEY_F4, ui::EF_ALT_DOWN,
472         chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(popup)));
473 #endif
474   }
475
476   void BackspaceInKeywordModeTest() {
477     OmniboxView* omnibox_view = NULL;
478     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
479
480     // Trigger keyword hint mode.
481     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
482     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
483     ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
484
485     // Trigger keyword mode.
486     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
487     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
488     ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
489
490     // Backspace without search text should bring back keyword hint mode.
491     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
492     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
493     ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
494
495     // Trigger keyword mode again.
496     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
497     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
498     ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
499
500     // Input something as search text.
501     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
502
503     // Should stay in keyword mode while deleting search text by pressing
504     // backspace.
505     for (size_t i = 0; i < arraysize(kSearchText) - 1; ++i) {
506       ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
507       ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
508       ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
509     }
510
511     // Input something as search text.
512     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
513
514     // Move cursor to the beginning of the search text.
515 #if defined(OS_MACOSX)
516     // Home doesn't work on Mac trybot.
517     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, ui::EF_CONTROL_DOWN));
518 #else
519     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_HOME, 0));
520 #endif
521     // Backspace at the beginning of the search text shall turn off
522     // the keyword mode.
523     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
524     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
525     ASSERT_EQ(string16(), omnibox_view->model()->keyword());
526     ASSERT_EQ(std::string(kSearchKeyword) + kSearchText,
527               UTF16ToUTF8(omnibox_view->GetText()));
528   }
529
530   void EscapeTest() {
531     ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIHistoryURL));
532     chrome::FocusLocationBar(browser());
533
534     OmniboxView* omnibox_view = NULL;
535     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
536
537     string16 old_text = omnibox_view->GetText();
538     EXPECT_FALSE(old_text.empty());
539     EXPECT_TRUE(omnibox_view->IsSelectAll());
540
541     // Delete all text in omnibox.
542     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
543     EXPECT_TRUE(omnibox_view->GetText().empty());
544
545     // Escape shall revert the text in omnibox.
546     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE, 0));
547     EXPECT_EQ(old_text, omnibox_view->GetText());
548     EXPECT_TRUE(omnibox_view->IsSelectAll());
549   }
550
551   void DesiredTLDTest() {
552     OmniboxView* omnibox_view = NULL;
553     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
554     OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
555     ASSERT_TRUE(popup_model);
556
557     // Test ctrl-Enter.
558     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kDesiredTLDKeys));
559     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
560     ASSERT_TRUE(popup_model->IsOpen());
561     // ctrl-Enter triggers desired_tld feature, thus www.bar.com shall be
562     // opened.
563     ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, ui::EF_CONTROL_DOWN,
564         content::NOTIFICATION_NAV_ENTRY_COMMITTED,
565         content::Source<content::NavigationController>(
566             &browser()->tab_strip_model()->GetActiveWebContents()->
567                 GetController())));
568
569     GURL url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
570     EXPECT_EQ(kDesiredTLDHostname, url.host());
571     EXPECT_EQ("/", url.path());
572   }
573
574   void AltEnterTest() {
575     OmniboxView* omnibox_view = NULL;
576     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
577
578     omnibox_view->SetUserText(ASCIIToUTF16(chrome::kChromeUIHistoryURL));
579     int tab_count = browser()->tab_strip_model()->count();
580     // alt-Enter opens a new tab.
581     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RETURN, ui::EF_ALT_DOWN));
582     ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count + 1));
583   }
584
585   void EnterToSearchTest() {
586     OmniboxView* omnibox_view = NULL;
587     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
588     OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
589     ASSERT_TRUE(popup_model);
590
591     // Test Enter to search.
592     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
593     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
594     ASSERT_TRUE(popup_model->IsOpen());
595
596     // Check if the default match result is Search Primary Provider.
597     ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
598               popup_model->result().default_match()->type);
599
600     // Open the default match.
601     ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, 0,
602         content::NOTIFICATION_NAV_ENTRY_COMMITTED,
603         content::Source<content::NavigationController>(
604             &browser()->tab_strip_model()->GetActiveWebContents()->
605                 GetController())));
606     GURL url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
607     EXPECT_EQ(kSearchTextURL, url.spec());
608
609     // Test that entering a single character then Enter performs a search.
610     chrome::FocusLocationBar(browser());
611     EXPECT_TRUE(omnibox_view->IsSelectAll());
612     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchSingleCharKeys));
613     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
614     ASSERT_TRUE(popup_model->IsOpen());
615     EXPECT_EQ(kSearchSingleChar, UTF16ToUTF8(omnibox_view->GetText()));
616
617     // Check if the default match result is Search Primary Provider.
618     ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
619               popup_model->result().default_match()->type);
620
621     // Open the default match.
622     ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, 0,
623         content::NOTIFICATION_NAV_ENTRY_COMMITTED,
624         content::Source<content::NavigationController>(
625             &browser()->tab_strip_model()->GetActiveWebContents()->
626                 GetController())));
627     url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
628     EXPECT_EQ(kSearchSingleCharURL, url.spec());
629   }
630
631   void EscapeToDefaultMatchTest() {
632     OmniboxView* omnibox_view = NULL;
633     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
634     OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
635     ASSERT_TRUE(popup_model);
636
637     // Input something to trigger inline autocomplete.
638     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
639     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
640     ASSERT_TRUE(popup_model->IsOpen());
641
642     string16 old_text = omnibox_view->GetText();
643
644     // Make sure inline autocomplete is triggerred.
645     EXPECT_GT(old_text.length(), arraysize(kInlineAutocompleteText) - 1);
646
647     size_t old_selected_line = popup_model->selected_line();
648     EXPECT_EQ(0U, old_selected_line);
649
650     // Move to another line with different text.
651     size_t size = popup_model->result().size();
652     while (popup_model->selected_line() < size - 1) {
653       ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
654       ASSERT_NE(old_selected_line, popup_model->selected_line());
655       if (old_text != omnibox_view->GetText())
656         break;
657     }
658
659     EXPECT_NE(old_text, omnibox_view->GetText());
660
661     // Escape shall revert back to the default match item.
662     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE, 0));
663     EXPECT_EQ(old_text, omnibox_view->GetText());
664     EXPECT_EQ(old_selected_line, popup_model->selected_line());
665   }
666
667   void BasicTextOperationsTest() {
668     ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL));
669     chrome::FocusLocationBar(browser());
670
671     OmniboxView* omnibox_view = NULL;
672     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
673
674     string16 old_text = omnibox_view->GetText();
675     EXPECT_EQ(UTF8ToUTF16(content::kAboutBlankURL), old_text);
676     EXPECT_TRUE(omnibox_view->IsSelectAll());
677
678     size_t start, end;
679     omnibox_view->GetSelectionBounds(&start, &end);
680     EXPECT_EQ(0U, start);
681     EXPECT_EQ(old_text.size(), end);
682
683     // Move the cursor to the end.
684 #if defined(OS_MACOSX)
685     // End doesn't work on Mac trybot.
686     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E, ui::EF_CONTROL_DOWN));
687 #else
688     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END, 0));
689 #endif
690     EXPECT_FALSE(omnibox_view->IsSelectAll());
691
692     // Make sure the cursor is placed correctly.
693     omnibox_view->GetSelectionBounds(&start, &end);
694     EXPECT_EQ(old_text.size(), start);
695     EXPECT_EQ(old_text.size(), end);
696
697     // Insert one character at the end. Make sure we won't insert
698     // anything after the special ZWS mark used in gtk implementation.
699     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, 0));
700     EXPECT_EQ(old_text + char16('a'), omnibox_view->GetText());
701
702     // Delete one character from the end. Make sure we won't delete the special
703     // ZWS mark used in gtk implementation.
704     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
705     EXPECT_EQ(old_text, omnibox_view->GetText());
706
707     omnibox_view->SelectAll(true);
708     EXPECT_TRUE(omnibox_view->IsSelectAll());
709     omnibox_view->GetSelectionBounds(&start, &end);
710     EXPECT_EQ(0U, start);
711     EXPECT_EQ(old_text.size(), end);
712
713     // Delete the content
714     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE, 0));
715     EXPECT_TRUE(omnibox_view->IsSelectAll());
716     omnibox_view->GetSelectionBounds(&start, &end);
717     EXPECT_EQ(0U, start);
718     EXPECT_EQ(0U, end);
719     EXPECT_TRUE(omnibox_view->GetText().empty());
720
721     // Check if RevertAll() can set text and cursor correctly.
722     omnibox_view->RevertAll();
723     EXPECT_FALSE(omnibox_view->IsSelectAll());
724     EXPECT_EQ(old_text, omnibox_view->GetText());
725     omnibox_view->GetSelectionBounds(&start, &end);
726     EXPECT_EQ(old_text.size(), start);
727     EXPECT_EQ(old_text.size(), end);
728   }
729
730   void AcceptKeywordBySpaceTest() {
731     OmniboxView* omnibox_view = NULL;
732     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
733
734     string16 search_keyword(ASCIIToUTF16(kSearchKeyword));
735
736     // Trigger keyword hint mode.
737     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
738     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
739     ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
740     ASSERT_EQ(search_keyword, omnibox_view->GetText());
741
742     // Trigger keyword mode by space.
743     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
744     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
745     ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
746     ASSERT_TRUE(omnibox_view->GetText().empty());
747
748     // Revert to keyword hint mode.
749     omnibox_view->model()->ClearKeyword(string16());
750     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
751     ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
752     ASSERT_EQ(search_keyword, omnibox_view->GetText());
753
754     // Keyword should also be accepted by typing an ideographic space.
755     omnibox_view->OnBeforePossibleChange();
756     omnibox_view->SetWindowTextAndCaretPos(search_keyword +
757         WideToUTF16(L"\x3000"), search_keyword.length() + 1, false, false);
758     omnibox_view->OnAfterPossibleChange();
759     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
760     ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
761     ASSERT_TRUE(omnibox_view->GetText().empty());
762
763     // Revert to keyword hint mode.
764     omnibox_view->model()->ClearKeyword(string16());
765     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
766     ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
767     ASSERT_EQ(search_keyword, omnibox_view->GetText());
768
769     // Keyword shouldn't be accepted by pressing space with a trailing
770     // whitespace.
771     omnibox_view->SetWindowTextAndCaretPos(search_keyword + char16(' '),
772         search_keyword.length() + 1, false, false);
773     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
774     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
775     ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
776     ASSERT_EQ(search_keyword + ASCIIToUTF16("  "), omnibox_view->GetText());
777
778     // Keyword shouldn't be accepted by deleting the trailing space.
779     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
780     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
781     ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
782     ASSERT_EQ(search_keyword + char16(' '), omnibox_view->GetText());
783
784     // Keyword shouldn't be accepted by pressing space before a trailing space.
785     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
786     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
787     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
788     ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
789     ASSERT_EQ(search_keyword + ASCIIToUTF16("  "), omnibox_view->GetText());
790
791     // Keyword should be accepted by pressing space in the middle of context and
792     // just after the keyword.
793     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
794     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, 0));
795     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
796     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
797     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
798     ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
799     ASSERT_EQ(ASCIIToUTF16("a "), omnibox_view->GetText());
800     size_t start, end;
801     omnibox_view->GetSelectionBounds(&start, &end);
802     EXPECT_EQ(0U, start);
803     EXPECT_EQ(0U, end);
804
805     // Keyword shouldn't be accepted by pasting "foo bar".
806     omnibox_view->SetUserText(string16());
807     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
808     ASSERT_TRUE(omnibox_view->model()->keyword().empty());
809
810     omnibox_view->OnBeforePossibleChange();
811     omnibox_view->model()->on_paste();
812     omnibox_view->SetWindowTextAndCaretPos(search_keyword +
813         ASCIIToUTF16(" bar"), search_keyword.length() + 4, false, false);
814     omnibox_view->OnAfterPossibleChange();
815     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
816     ASSERT_TRUE(omnibox_view->model()->keyword().empty());
817     ASSERT_EQ(search_keyword + ASCIIToUTF16(" bar"), omnibox_view->GetText());
818
819     // Keyword shouldn't be accepted for case like: "foo b|ar" -> "foo b |ar".
820     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
821     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
822     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
823     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
824     ASSERT_TRUE(omnibox_view->model()->keyword().empty());
825     ASSERT_EQ(search_keyword + ASCIIToUTF16(" b ar"), omnibox_view->GetText());
826
827     // Keyword could be accepted by pressing space with a selected range at the
828     // end of text.
829     omnibox_view->OnBeforePossibleChange();
830     omnibox_view->OnInlineAutocompleteTextMaybeChanged(
831         search_keyword + ASCIIToUTF16("  "), search_keyword.length());
832     omnibox_view->OnAfterPossibleChange();
833     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
834     ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
835     ASSERT_EQ(search_keyword + ASCIIToUTF16("  "), omnibox_view->GetText());
836
837     omnibox_view->GetSelectionBounds(&start, &end);
838     ASSERT_NE(start, end);
839     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
840     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
841     ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
842     ASSERT_EQ(string16(), omnibox_view->GetText());
843
844     // Space should accept keyword even when inline autocomplete is available.
845     omnibox_view->SetUserText(string16());
846     const TestHistoryEntry kHistoryFoobar = {
847       "http://www.foobar.com", "Page foobar", kSearchText, 100, 100, true
848     };
849
850     // Add a history entry to trigger inline autocomplete when typing "foo".
851     ASSERT_NO_FATAL_FAILURE(
852         AddHistoryEntry(kHistoryFoobar, Time::Now() - TimeDelta::FromHours(1)));
853
854     // Type "foo" to trigger inline autocomplete.
855     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
856     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
857     ASSERT_TRUE(omnibox_view->model()->popup_model()->IsOpen());
858     ASSERT_NE(search_keyword, omnibox_view->GetText());
859
860     // Keyword hint shouldn't be visible.
861     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
862     ASSERT_TRUE(omnibox_view->model()->keyword().empty());
863
864     // Trigger keyword mode by space.
865     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
866     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
867     ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
868     ASSERT_TRUE(omnibox_view->GetText().empty());
869
870     // Space in the middle of a temporary text, which separates the text into
871     // keyword and replacement portions, should trigger keyword mode.
872     omnibox_view->SetUserText(string16());
873     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
874     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
875     OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
876     ASSERT_TRUE(popup_model->IsOpen());
877     ASSERT_EQ(ASCIIToUTF16("foobar.com"), omnibox_view->GetText());
878     omnibox_view->model()->OnUpOrDownKeyPressed(1);
879     omnibox_view->model()->OnUpOrDownKeyPressed(-1);
880     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
881     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
882     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
883     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
884     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
885     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
886     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
887     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
888     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
889     ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
890     ASSERT_EQ(ASCIIToUTF16("bar.com"), omnibox_view->GetText());
891
892     // Space after temporary text that looks like a keyword, when the original
893     // input does not look like a keyword, should trigger keyword mode.
894     omnibox_view->SetUserText(string16());
895     const TestHistoryEntry kHistoryFoo = {
896       "http://footest.com", "Page footest", kSearchText, 1000, 1000, true
897     };
898
899     // Add a history entry to trigger HQP matching with text == keyword when
900     // typing "fo te".
901     ASSERT_NO_FATAL_FAILURE(
902         AddHistoryEntry(kHistoryFoo, Time::Now() - TimeDelta::FromMinutes(10)));
903
904     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_F, 0));
905     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_O, 0));
906     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
907     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_T, 0));
908     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E, 0));
909     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
910     ASSERT_TRUE(popup_model->IsOpen());
911     string16 search_keyword2(ASCIIToUTF16(kSearchKeyword2));
912     while ((omnibox_view->GetText() != search_keyword2) &&
913            (popup_model->selected_line() < popup_model->result().size() - 1))
914       omnibox_view->model()->OnUpOrDownKeyPressed(1);
915     ASSERT_EQ(search_keyword2, omnibox_view->GetText());
916     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
917     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
918     ASSERT_EQ(search_keyword2, omnibox_view->model()->keyword());
919     ASSERT_TRUE(omnibox_view->GetText().empty());
920   }
921
922   void NonSubstitutingKeywordTest() {
923     OmniboxView* omnibox_view = NULL;
924     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
925     OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
926     ASSERT_TRUE(popup_model);
927
928     Profile* profile = browser()->profile();
929     TemplateURLService* template_url_service =
930         TemplateURLServiceFactory::GetForProfile(profile);
931
932     // Add a non-default substituting keyword.
933     TemplateURLData data;
934     data.short_name = ASCIIToUTF16("Search abc");
935     data.SetKeyword(ASCIIToUTF16(kSearchText));
936     data.SetURL("http://abc.com/{searchTerms}");
937     TemplateURL* template_url = new TemplateURL(profile, data);
938     template_url_service->Add(template_url);
939
940     omnibox_view->SetUserText(string16());
941
942     // Non-default substituting keyword shouldn't be matched by default.
943     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
944     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
945     ASSERT_TRUE(popup_model->IsOpen());
946
947     // Check if the default match result is Search Primary Provider.
948     ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
949               popup_model->result().default_match()->type);
950     ASSERT_EQ(kSearchTextURL,
951               popup_model->result().default_match()->destination_url.spec());
952
953     omnibox_view->SetUserText(string16());
954     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
955     ASSERT_FALSE(popup_model->IsOpen());
956
957     // Try a non-substituting keyword.
958     template_url_service->Remove(template_url);
959     data.short_name = ASCIIToUTF16("abc");
960     data.SetURL("http://abc.com/");
961     template_url_service->Add(new TemplateURL(profile, data));
962
963     // We always allow exact matches for non-substituting keywords.
964     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
965     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
966     ASSERT_TRUE(popup_model->IsOpen());
967     ASSERT_EQ(AutocompleteMatchType::HISTORY_KEYWORD,
968               popup_model->result().default_match()->type);
969     ASSERT_EQ("http://abc.com/",
970               popup_model->result().default_match()->destination_url.spec());
971   }
972
973   void DeleteItemTest() {
974     // Disable the search provider, to make sure the popup contains only history
975     // items.
976     TemplateURLService* model =
977         TemplateURLServiceFactory::GetForProfile(browser()->profile());
978     model->SetDefaultSearchProvider(NULL);
979
980     ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL));
981     chrome::FocusLocationBar(browser());
982
983     OmniboxView* omnibox_view = NULL;
984     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
985
986     OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
987     ASSERT_TRUE(popup_model);
988
989     string16 old_text = omnibox_view->GetText();
990
991     // Input something that can match history items.
992     omnibox_view->SetUserText(ASCIIToUTF16("site.com/p"));
993     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
994     ASSERT_TRUE(popup_model->IsOpen());
995
996     // Delete the inline autocomplete part.
997     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE, 0));
998     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
999     ASSERT_TRUE(popup_model->IsOpen());
1000     ASSERT_GE(popup_model->result().size(), 3U);
1001
1002     string16 user_text = omnibox_view->GetText();
1003     ASSERT_EQ(ASCIIToUTF16("site.com/p"), user_text);
1004     omnibox_view->SelectAll(true);
1005     ASSERT_TRUE(omnibox_view->IsSelectAll());
1006
1007     // Move down.
1008     size_t default_line = popup_model->selected_line();
1009     omnibox_view->model()->OnUpOrDownKeyPressed(1);
1010     ASSERT_EQ(default_line + 1, popup_model->selected_line());
1011     string16 selected_text =
1012         popup_model->result().match_at(default_line + 1).fill_into_edit;
1013     // Temporary text is shown.
1014     ASSERT_EQ(selected_text, omnibox_view->GetText());
1015     ASSERT_FALSE(omnibox_view->IsSelectAll());
1016
1017     // Delete the item.
1018     popup_model->TryDeletingCurrentItem();
1019     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1020     // The selected line shouldn't be changed, because we have more than two
1021     // items.
1022     ASSERT_EQ(default_line + 1, popup_model->selected_line());
1023     // Make sure the item is really deleted.
1024     ASSERT_NE(selected_text,
1025               popup_model->result().match_at(default_line + 1).fill_into_edit);
1026     selected_text =
1027         popup_model->result().match_at(default_line + 1).fill_into_edit;
1028     // New temporary text is shown.
1029     ASSERT_EQ(selected_text, omnibox_view->GetText());
1030
1031     // Revert to the default match.
1032     ASSERT_TRUE(omnibox_view->model()->OnEscapeKeyPressed());
1033     ASSERT_EQ(default_line, popup_model->selected_line());
1034     ASSERT_EQ(user_text, omnibox_view->GetText());
1035     ASSERT_TRUE(omnibox_view->IsSelectAll());
1036
1037     // Move down and up to select the default match as temporary text.
1038     omnibox_view->model()->OnUpOrDownKeyPressed(1);
1039     ASSERT_EQ(default_line + 1, popup_model->selected_line());
1040     omnibox_view->model()->OnUpOrDownKeyPressed(-1);
1041     ASSERT_EQ(default_line, popup_model->selected_line());
1042
1043     selected_text = popup_model->result().match_at(default_line).fill_into_edit;
1044     // New temporary text is shown.
1045     ASSERT_EQ(selected_text, omnibox_view->GetText());
1046     ASSERT_FALSE(omnibox_view->IsSelectAll());
1047
1048 #if 0
1049     // TODO(mrossetti): http://crbug.com/82335
1050     // Delete the default item.
1051     popup_model->TryDeletingCurrentItem();
1052     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1053     // The selected line shouldn't be changed, but the default item should have
1054     // been changed.
1055     ASSERT_EQ(default_line, popup_model->selected_line());
1056     // Make sure the item is really deleted.
1057     EXPECT_NE(selected_text,
1058               popup_model->result().match_at(default_line).fill_into_edit);
1059     selected_text =
1060         popup_model->result().match_at(default_line).fill_into_edit;
1061     // New temporary text is shown.
1062     ASSERT_EQ(selected_text, omnibox_view->GetText());
1063 #endif
1064
1065     // As the current selected item is the new default item, pressing Escape key
1066     // should revert all directly.
1067     ASSERT_TRUE(omnibox_view->model()->OnEscapeKeyPressed());
1068     ASSERT_EQ(old_text, omnibox_view->GetText());
1069     ASSERT_TRUE(omnibox_view->IsSelectAll());
1070   }
1071
1072   void TabAcceptKeyword() {
1073     OmniboxView* omnibox_view = NULL;
1074     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1075
1076     string16 text = ASCIIToUTF16(kSearchKeyword);
1077
1078     // Trigger keyword hint mode.
1079     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
1080     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1081     ASSERT_EQ(text, omnibox_view->model()->keyword());
1082     ASSERT_EQ(text, omnibox_view->GetText());
1083
1084     // Trigger keyword mode by tab.
1085     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1086     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1087     ASSERT_EQ(text, omnibox_view->model()->keyword());
1088     ASSERT_TRUE(omnibox_view->GetText().empty());
1089
1090     // Revert to keyword hint mode.
1091     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1092     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1093     ASSERT_EQ(text, omnibox_view->model()->keyword());
1094     ASSERT_EQ(text, omnibox_view->GetText());
1095
1096     // The location bar should still have focus.
1097     ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1098
1099     // Trigger keyword mode by tab.
1100     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1101     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1102     ASSERT_EQ(text, omnibox_view->model()->keyword());
1103     ASSERT_TRUE(omnibox_view->GetText().empty());
1104
1105     // Revert to keyword hint mode with SHIFT+TAB.
1106 #if defined(OS_MACOSX)
1107     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACKTAB, 0));
1108 #else
1109     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1110 #endif
1111     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1112     ASSERT_EQ(text, omnibox_view->model()->keyword());
1113     ASSERT_EQ(text, omnibox_view->GetText());
1114     ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1115   }
1116
1117   void TabTraverseResultsTest() {
1118     OmniboxView* omnibox_view = NULL;
1119     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1120     OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1121     ASSERT_TRUE(popup_model);
1122
1123     // Input something to trigger results.
1124     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kDesiredTLDKeys));
1125     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1126     ASSERT_TRUE(popup_model->IsOpen());
1127
1128     size_t old_selected_line = popup_model->selected_line();
1129     EXPECT_EQ(0U, old_selected_line);
1130
1131     // Move down the results.
1132     for (size_t size = popup_model->result().size();
1133          popup_model->selected_line() < size - 1;
1134          old_selected_line = popup_model->selected_line()) {
1135       ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1136       ASSERT_LT(old_selected_line, popup_model->selected_line());
1137     }
1138
1139     // Don't move past the end.
1140     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1141     ASSERT_EQ(old_selected_line, popup_model->selected_line());
1142     ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1143
1144     // Move back up the results.
1145     for (; popup_model->selected_line() > 0U;
1146          old_selected_line = popup_model->selected_line()) {
1147       ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1148       ASSERT_GT(old_selected_line, popup_model->selected_line());
1149     }
1150
1151     // Don't move past the beginning.
1152     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1153     ASSERT_EQ(0U, popup_model->selected_line());
1154     ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1155
1156     const TestHistoryEntry kHistoryFoo = {
1157       "http://foo/", "Page foo", kSearchText, 1, 1, false
1158     };
1159
1160     // Add a history entry so "foo" gets multiple matches.
1161     ASSERT_NO_FATAL_FAILURE(
1162         AddHistoryEntry(kHistoryFoo, Time::Now() - TimeDelta::FromHours(1)));
1163
1164     // Load results.
1165     ASSERT_NO_FATAL_FAILURE(omnibox_view->SelectAll(false));
1166     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
1167     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1168
1169     // Trigger keyword mode by tab.
1170     string16 text = ASCIIToUTF16(kSearchKeyword);
1171     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1172     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1173     ASSERT_EQ(text, omnibox_view->model()->keyword());
1174     ASSERT_TRUE(omnibox_view->GetText().empty());
1175
1176     // The location bar should still have focus.
1177     ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1178
1179     // Pressing tab again should move to the next result and clear keyword
1180     // mode.
1181     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1182     ASSERT_EQ(1U, omnibox_view->model()->popup_model()->selected_line());
1183     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1184     ASSERT_NE(text, omnibox_view->model()->keyword());
1185
1186     // The location bar should still have focus.
1187     ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1188
1189     // Moving back up should not show keyword mode.
1190     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1191     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1192     ASSERT_EQ(text, omnibox_view->model()->keyword());
1193
1194     ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1195   }
1196
1197   void PersistKeywordModeOnTabSwitch() {
1198     OmniboxView* omnibox_view = NULL;
1199     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1200
1201     // Trigger keyword hint mode.
1202     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
1203     ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1204     ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
1205
1206     // Trigger keyword mode.
1207     ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1208     ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1209     ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
1210
1211     // Input something as search text.
1212     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
1213
1214     // Create a new tab.
1215     chrome::NewTab(browser());
1216
1217     // Switch back to the first tab.
1218     browser()->tab_strip_model()->ActivateTabAt(0, true);
1219
1220     // Make sure we're still in keyword mode.
1221     ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
1222   }
1223
1224   void CtrlKeyPressedWithInlineAutocompleteTest() {
1225     OmniboxView* omnibox_view = NULL;
1226     ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1227     OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1228     ASSERT_TRUE(popup_model);
1229
1230     // Input something to trigger inline autocomplete.
1231     ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
1232     ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1233     ASSERT_TRUE(popup_model->IsOpen());
1234
1235     string16 old_text = omnibox_view->GetText();
1236
1237     // Make sure inline autocomplete is triggerred.
1238     EXPECT_GT(old_text.length(), arraysize(kInlineAutocompleteText) - 1);
1239
1240     // Press ctrl key.
1241     omnibox_view->model()->OnControlKeyChanged(true);
1242
1243     // Inline autocomplete should still be there.
1244     EXPECT_EQ(old_text, omnibox_view->GetText());
1245   }
1246 };
1247
1248 // Test if ctrl-* accelerators are workable in omnibox.
1249 // See http://crbug.com/19193: omnibox blocks ctrl-* commands
1250 //
1251 // Flaky on interactive tests (dbg), http://crbug.com/69433
1252 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DISABLED_BrowserAccelerators) {
1253   BrowserAcceleratorsTest();
1254 }
1255
1256 // Flakily fails and times out on Win only.  http://crbug.com/69941
1257 #if defined(OS_WIN)
1258 #define MAYBE_PopupAccelerators DISABLED_PopupAccelerators
1259 #else
1260 #define MAYBE_PopupAccelerators PopupAccelerators
1261 #endif
1262
1263 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_PopupAccelerators) {
1264   PopupAcceleratorsTest();
1265 }
1266
1267 // http://crbug.com/133341
1268 #if defined(OS_LINUX)
1269 #define MAYBE_BackspaceInKeywordMode DISABLED_BackspaceInKeywordMode
1270 #else
1271 #define MAYBE_BackspaceInKeywordMode BackspaceInKeywordMode
1272 #endif
1273
1274 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_BackspaceInKeywordMode) {
1275   BackspaceInKeywordModeTest();
1276 }
1277
1278 // http://crbug.com/158913
1279 #if defined(OS_CHROMEOS) || defined(OS_WIN)
1280 #define MAYBE_Escape DISABLED_Escape
1281 #else
1282 #define MAYBE_Escape Escape
1283 #endif
1284 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_Escape) {
1285   EscapeTest();
1286 }
1287 #undef MAYBE_ESCAPE
1288
1289 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DesiredTLD) {
1290   DesiredTLDTest();
1291 }
1292
1293 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, AltEnter) {
1294   AltEnterTest();
1295 }
1296
1297 // http://crbug.com/133354, http://crbug.com/146953
1298 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DISABLED_EnterToSearch) {
1299   EnterToSearchTest();
1300 }
1301
1302 // http://crbug.com/131179
1303 #if defined(OS_LINUX)
1304 #define MAYBE_EscapeToDefaultMatch DISABLED_EscapeToDefaultMatch
1305 #else
1306 #define MAYBE_EscapeToDefaultMatch EscapeToDefaultMatch
1307 #endif
1308 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_EscapeToDefaultMatch) {
1309   EscapeToDefaultMatchTest();
1310 }
1311
1312 // http://crbug.com/131179, http://crbug.com/146619
1313 #if defined(OS_LINUX) || defined(OS_WIN)
1314 #define MAYBE_BasicTextOperations DISABLED_BasicTextOperations
1315 #else
1316 #define MAYBE_BasicTextOperations BasicTextOperations
1317 #endif
1318 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_BasicTextOperations) {
1319   BasicTextOperationsTest();
1320 }
1321
1322 // http://crbug.com/131179
1323 #if defined(OS_LINUX)
1324 #define MAYBE_AcceptKeywordBySpace DISABLED_AcceptKeywordBySpace
1325 #else
1326 #define MAYBE_AcceptKeywordBySpace AcceptKeywordBySpace
1327 #endif
1328
1329 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_AcceptKeywordBySpace) {
1330   AcceptKeywordBySpaceTest();
1331 }
1332
1333 // http://crbug.com/131179
1334 #if defined(OS_LINUX)
1335 #define MAYBE_NonSubstitutingKeywordTest DISABLED_NonSubstitutingKeywordTest
1336 #else
1337 #define MAYBE_NonSubstitutingKeywordTest NonSubstitutingKeywordTest
1338 #endif
1339
1340 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_NonSubstitutingKeywordTest) {
1341   NonSubstitutingKeywordTest();
1342 }
1343
1344 // http://crbug.com/131179 http://crbug.com/165765
1345 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_MACOSX)
1346 #define MAYBE_DeleteItem DISABLED_DeleteItem
1347 #else
1348 #define MAYBE_DeleteItem DeleteItem
1349 #endif
1350 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_DeleteItem) {
1351   DeleteItemTest();
1352 }
1353
1354 // http://crbug.com/133344
1355 #if defined(OS_LINUX)
1356 #define MAYBE_TabAcceptKeyword DISABLED_TabAcceptKeyword
1357 #else
1358 #define MAYBE_TabAcceptKeyword TabAcceptKeyword
1359 #endif
1360
1361 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_TabAcceptKeyword) {
1362   TabAcceptKeyword();
1363 }
1364
1365 #if !defined(OS_MACOSX)
1366 // Mac intentionally does not support this behavior.
1367
1368 // http://crbug.com/133360
1369 #if defined(OS_LINUX)
1370 #define MAYBE_TabTraverseResultsTest DISABLED_TabTraverseResultsTest
1371 #else
1372 #define MAYBE_TabTraverseResultsTest TabTraverseResultsTest
1373 #endif
1374
1375 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_TabTraverseResultsTest) {
1376   TabTraverseResultsTest();
1377 }
1378 #endif
1379
1380
1381 // http://crbug.com/133347
1382 #if defined(OS_LINUX)
1383 #define MAYBE_PersistKeywordModeOnTabSwitch DISABLED_PersistKeywordModeOnTabSwitch
1384 #else
1385 #define MAYBE_PersistKeywordModeOnTabSwitch PersistKeywordModeOnTabSwitch
1386 #endif
1387
1388 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1389                        MAYBE_PersistKeywordModeOnTabSwitch) {
1390   PersistKeywordModeOnTabSwitch();
1391 }
1392
1393 // http://crbug.com/133355
1394 #if defined(OS_LINUX)
1395 #define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest DISABLED_CtrlKeyPressedWithInlineAutocompleteTest
1396 #else
1397 #define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest CtrlKeyPressedWithInlineAutocompleteTest
1398 #endif
1399
1400 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1401                        MAYBE_CtrlKeyPressedWithInlineAutocompleteTest) {
1402   CtrlKeyPressedWithInlineAutocompleteTest();
1403 }
1404
1405 #if defined(TOOLKIT_GTK) || defined(TOOLKIT_VIEWS)
1406 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, UndoRedo) {
1407   ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL));
1408   chrome::FocusLocationBar(browser());
1409
1410   OmniboxView* omnibox_view = NULL;
1411   ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1412
1413   string16 old_text = omnibox_view->GetText();
1414   EXPECT_EQ(UTF8ToUTF16(content::kAboutBlankURL), old_text);
1415   EXPECT_TRUE(omnibox_view->IsSelectAll());
1416
1417   // Delete the text, then undo.
1418   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1419   EXPECT_TRUE(omnibox_view->GetText().empty());
1420   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1421   EXPECT_EQ(old_text, omnibox_view->GetText());
1422
1423   // Redo should delete the text again.
1424   ASSERT_NO_FATAL_FAILURE(
1425       SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN));
1426   EXPECT_TRUE(omnibox_view->GetText().empty());
1427
1428   // Looks like the undo manager doesn't support restoring selection.
1429   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1430   EXPECT_FALSE(omnibox_view->IsSelectAll());
1431
1432   // The cursor should be at the end.
1433   size_t start, end;
1434   omnibox_view->GetSelectionBounds(&start, &end);
1435   EXPECT_EQ(old_text.size(), start);
1436   EXPECT_EQ(old_text.size(), end);
1437
1438   // Delete two characters.
1439   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1440   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1441   EXPECT_EQ(old_text.substr(0, old_text.size() - 2), omnibox_view->GetText());
1442
1443   // Undo delete.
1444   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1445   EXPECT_EQ(old_text, omnibox_view->GetText());
1446
1447   // Redo delete.
1448   ASSERT_NO_FATAL_FAILURE(
1449       SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN));
1450   EXPECT_EQ(old_text.substr(0, old_text.size() - 2), omnibox_view->GetText());
1451
1452   // Delete everything.
1453   omnibox_view->SelectAll(true);
1454   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1455   EXPECT_TRUE(omnibox_view->GetText().empty());
1456
1457   // Undo delete everything.
1458   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1459   EXPECT_EQ(old_text.substr(0, old_text.size() - 2), omnibox_view->GetText());
1460
1461   // Undo delete two characters.
1462   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, ui::EF_CONTROL_DOWN));
1463   EXPECT_EQ(old_text, omnibox_view->GetText());
1464 }
1465
1466 // See http://crosbug.com/10306
1467 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1468                        BackspaceDeleteHalfWidthKatakana) {
1469   OmniboxView* omnibox_view = NULL;
1470   ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1471   // Insert text: ï¾€ï¾ž
1472   omnibox_view->SetUserText(UTF8ToUTF16("\357\276\200\357\276\236"));
1473
1474   // Move the cursor to the end.
1475   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END, 0));
1476
1477   // Backspace should delete one character.
1478   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1479   EXPECT_EQ(UTF8ToUTF16("\357\276\200"), omnibox_view->GetText());
1480 }
1481 #endif  // defined(TOOLKIT_GTK) || defined(TOOLKIT_VIEWS)
1482
1483 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DoesNotUpdateAutocompleteOnBlur) {
1484   OmniboxView* omnibox_view = NULL;
1485   ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1486   OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1487   ASSERT_TRUE(popup_model);
1488
1489   // Input something to trigger inline autocomplete.
1490   ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
1491   ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1492   ASSERT_TRUE(popup_model->IsOpen());
1493   size_t start, end;
1494   omnibox_view->GetSelectionBounds(&start, &end);
1495   EXPECT_TRUE(start != end);
1496   string16 old_autocomplete_text =
1497       omnibox_view->model()->autocomplete_controller()->input().text();
1498
1499   // Unfocus the omnibox. This should clear the text field selection and
1500   // close the popup, but should not run autocomplete.
1501   // Note: GTK preserves the selection when the omnibox is unfocused.
1502   ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
1503   ASSERT_FALSE(popup_model->IsOpen());
1504   omnibox_view->GetSelectionBounds(&start, &end);
1505 #if !defined(TOOLKIT_GTK)
1506   EXPECT_TRUE(start == end);
1507 #endif
1508
1509   EXPECT_EQ(old_autocomplete_text,
1510       omnibox_view->model()->autocomplete_controller()->input().text());
1511 }
1512
1513 #if defined(TOOLKIT_GTK)
1514 // See http://crbug.com/63860
1515 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, PrimarySelection) {
1516   OmniboxView* omnibox_view = NULL;
1517   ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1518   omnibox_view->SetUserText(ASCIIToUTF16("Hello world"));
1519   EXPECT_FALSE(omnibox_view->IsSelectAll());
1520
1521   // Move the cursor to the end.
1522   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END, 0));
1523
1524   // Select all text by pressing Shift+Home
1525   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_HOME, ui::EF_SHIFT_DOWN));
1526   EXPECT_TRUE(omnibox_view->IsSelectAll());
1527
1528   // The selected content should be saved to the PRIMARY clipboard.
1529   EXPECT_EQ("Hello world", GetPrimarySelectionText());
1530
1531   // Move the cursor to the end.
1532   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END, 0));
1533   EXPECT_FALSE(omnibox_view->IsSelectAll());
1534
1535   // The content in the PRIMARY clipboard should not be cleared.
1536   EXPECT_EQ("Hello world", GetPrimarySelectionText());
1537 }
1538 #endif  // defined(TOOLKIT_GTK)
1539
1540 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, Paste) {
1541   OmniboxView* omnibox_view = NULL;
1542   ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1543   OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1544   ASSERT_TRUE(popup_model);
1545   EXPECT_FALSE(popup_model->IsOpen());
1546
1547   // Paste should yield the expected text and open the popup.
1548   SetClipboardText(ASCIIToUTF16(kSearchText));
1549   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
1550   ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1551   EXPECT_EQ(ASCIIToUTF16(kSearchText), omnibox_view->GetText());
1552   EXPECT_TRUE(popup_model->IsOpen());
1553
1554   // Close the popup and select all.
1555   omnibox_view->CloseOmniboxPopup();
1556   omnibox_view->SelectAll(false);
1557   EXPECT_FALSE(popup_model->IsOpen());
1558
1559   // Pasting the same text again over itself should re-open the popup.
1560   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
1561   ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1562   EXPECT_EQ(ASCIIToUTF16(kSearchText), omnibox_view->GetText());
1563   // This fails on GTK, see http://crbug.com/131179
1564 #if !defined(TOOLKIT_GTK)
1565   EXPECT_TRUE(popup_model->IsOpen());
1566 #endif
1567   omnibox_view->CloseOmniboxPopup();
1568   EXPECT_FALSE(popup_model->IsOpen());
1569
1570   // Pasting amid text should yield the expected text and re-open the popup.
1571   omnibox_view->SetWindowTextAndCaretPos(ASCIIToUTF16("abcd"), 2, false, false);
1572   SetClipboardText(ASCIIToUTF16("123"));
1573   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
1574   EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view->GetText());
1575   EXPECT_TRUE(popup_model->IsOpen());
1576
1577   // Ctrl/Cmd+Alt+V should not paste.
1578   ASSERT_NO_FATAL_FAILURE(
1579       SendKey(ui::VKEY_V, kCtrlOrCmdMask | ui::EF_ALT_DOWN));
1580   EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view->GetText());
1581   // TODO(msw): Test that AltGr+V does not paste.
1582 }
1583
1584 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CopyURLToClipboard) {
1585   // Set permanent text thus making sure that omnibox treats 'google.com'
1586   // as URL (not as ordinary user input).
1587   TestToolbarModel* test_toolbar_model = new TestToolbarModel;
1588   scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
1589   test_toolbar_model->set_text(ASCIIToUTF16("http://www.google.com/"));
1590   browser()->swap_toolbar_models(&toolbar_model);
1591   OmniboxView* omnibox_view = NULL;
1592   ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1593   OmniboxEditModel* edit_model = omnibox_view->model();
1594   ASSERT_NE(static_cast<OmniboxEditModel*>(NULL), edit_model);
1595   edit_model->UpdatePermanentText();
1596
1597   const char* target_url = "http://www.google.com/calendar";
1598   omnibox_view->SetUserText(ASCIIToUTF16(target_url));
1599
1600   // Location bar must have focus.
1601   chrome::FocusLocationBar(browser());
1602   ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1603
1604   // Select full URL and copy it to clipboard. General text and html should
1605   // be available.
1606   omnibox_view->SelectAll(true);
1607   EXPECT_TRUE(omnibox_view->IsSelectAll());
1608   ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1609   clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1610   EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY));
1611   EXPECT_EQ(ASCIIToUTF16(target_url), omnibox_view->GetText());
1612   EXPECT_TRUE(clipboard->IsFormatAvailable(
1613       ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1614
1615   // Make sure HTML format isn't written. See
1616   // BookmarkNodeData::WriteToClipboard() for details.
1617   EXPECT_FALSE(clipboard->IsFormatAvailable(
1618       ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1619
1620   // These platforms should read bookmark format.
1621 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1622   string16 title;
1623   std::string url;
1624   clipboard->ReadBookmark(&title, &url);
1625   EXPECT_EQ(target_url, url);
1626   EXPECT_EQ(ASCIIToUTF16(target_url), title);
1627 #endif
1628 }
1629
1630 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CutURLToClipboard) {
1631   // Set permanent text thus making sure that omnibox treats 'google.com'
1632   // as URL (not as ordinary user input).
1633   TestToolbarModel* test_toolbar_model = new TestToolbarModel;
1634   scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
1635   test_toolbar_model->set_text(ASCIIToUTF16("http://www.google.com/"));
1636   browser()->swap_toolbar_models(&toolbar_model);
1637   OmniboxView* omnibox_view = NULL;
1638   ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1639   OmniboxEditModel* edit_model = omnibox_view->model();
1640   ASSERT_NE(static_cast<OmniboxEditModel*>(NULL), edit_model);
1641   edit_model->UpdatePermanentText();
1642
1643   const char* target_url = "http://www.google.com/calendar";
1644   omnibox_view->SetUserText(ASCIIToUTF16(target_url));
1645
1646   // Location bar must have focus.
1647   chrome::FocusLocationBar(browser());
1648   ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1649
1650   // Select full URL and cut it. General text and html should be available
1651   // in 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_CUT));
1657   EXPECT_EQ(string16(), omnibox_view->GetText());
1658   EXPECT_TRUE(clipboard->IsFormatAvailable(
1659       ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1660
1661   // Make sure HTML format isn't written. See
1662   // BookmarkNodeData::WriteToClipboard() for details.
1663   EXPECT_FALSE(clipboard->IsFormatAvailable(
1664       ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1665
1666   // These platforms should read bookmark format.
1667 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1668   string16 title;
1669   std::string url;
1670   clipboard->ReadBookmark(&title, &url);
1671   EXPECT_EQ(target_url, url);
1672   EXPECT_EQ(ASCIIToUTF16(target_url), title);
1673 #endif
1674 }
1675
1676 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CopyTextToClipboard) {
1677   OmniboxView* omnibox_view = NULL;
1678   ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1679   const char* target_text = "foo";
1680   omnibox_view->SetUserText(ASCIIToUTF16(target_text));
1681
1682   // Location bar must have focus.
1683   chrome::FocusLocationBar(browser());
1684   ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1685
1686   // Select full text and copy it to the clipboard.
1687   omnibox_view->SelectAll(true);
1688   EXPECT_TRUE(omnibox_view->IsSelectAll());
1689   ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1690   clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1691   EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY));
1692   EXPECT_TRUE(clipboard->IsFormatAvailable(
1693       ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1694   EXPECT_FALSE(clipboard->IsFormatAvailable(
1695       ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1696   EXPECT_EQ(ASCIIToUTF16(target_text), omnibox_view->GetText());
1697 }
1698
1699 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CutTextToClipboard) {
1700   OmniboxView* omnibox_view = NULL;
1701   ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1702   const char* target_text = "foo";
1703   omnibox_view->SetUserText(ASCIIToUTF16(target_text));
1704
1705   // Location bar must have focus.
1706   chrome::FocusLocationBar(browser());
1707   ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1708
1709   // Select full text and cut it to the clipboard.
1710   omnibox_view->SelectAll(true);
1711   EXPECT_TRUE(omnibox_view->IsSelectAll());
1712   ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1713   clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1714   EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT));
1715   EXPECT_TRUE(clipboard->IsFormatAvailable(
1716       ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1717   EXPECT_FALSE(clipboard->IsFormatAvailable(
1718       ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1719   EXPECT_EQ(string16(), omnibox_view->GetText());
1720 }
1721
1722 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, EditSearchEngines) {
1723   OmniboxView* omnibox_view = NULL;
1724   ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1725   EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_EDIT_SEARCH_ENGINES));
1726   ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1727   const std::string target_url =
1728       std::string(chrome::kChromeUISettingsURL) + chrome::kSearchEnginesSubPage;
1729   EXPECT_EQ(ASCIIToUTF16(target_url), omnibox_view->GetText());
1730   EXPECT_FALSE(omnibox_view->model()->popup_model()->IsOpen());
1731 }
1732
1733 #if !defined(TOOLKIT_GTK)
1734 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, BeginningShownAfterBlur) {
1735   OmniboxView* omnibox_view = NULL;
1736   ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1737
1738   omnibox_view->OnBeforePossibleChange();
1739   omnibox_view->SetWindowTextAndCaretPos(ASCIIToUTF16("data:text/plain,test"),
1740       5U, false, false);
1741   omnibox_view->OnAfterPossibleChange();
1742   ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1743   size_t start, end;
1744   omnibox_view->GetSelectionBounds(&start, &end);
1745   ASSERT_EQ(5U, start);
1746   ASSERT_EQ(5U, end);
1747
1748   ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
1749   ASSERT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1750
1751   omnibox_view->GetSelectionBounds(&start, &end);
1752   ASSERT_EQ(0U, start);
1753   ASSERT_EQ(0U, end);
1754 }
1755 #endif  // !defined(TOOLKIT_GTK)
1756
1757 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CtrlArrowAfterArrowSuggestions) {
1758   OmniboxView* omnibox_view = NULL;
1759   ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1760   OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1761   ASSERT_TRUE(popup_model);
1762
1763   // Input something to trigger results.
1764   ASSERT_NO_FATAL_FAILURE(SendKeySequence(kDesiredTLDKeys));
1765   ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1766   ASSERT_TRUE(popup_model->IsOpen());
1767
1768   ASSERT_EQ(ASCIIToUTF16("bar.com/1"), omnibox_view->GetText());
1769
1770   // Arrow down on a suggestion, and omnibox text should be the suggestion.
1771   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
1772   ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1773   ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view->GetText());
1774
1775   // Highlight the last 2 words and the omnibox text should not change.
1776   // Simulating Ctrl-shift-left only once does not seem to highlight anything
1777   // on Linux.
1778 #if defined(OS_MACOSX)
1779   // Mac uses alt-left/right to select a word.
1780   const int modifiers = ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN;
1781 #else
1782   const int modifiers = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN;
1783 #endif
1784   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, modifiers));
1785   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, modifiers));
1786   ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view->GetText());
1787 }
1788
1789 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1790                        PersistSearchReplacementAcrossTabSwitch) {
1791   EXPECT_TRUE(browser()->toolbar_model()->search_term_replacement_enabled());
1792   browser()->toolbar_model()->set_search_term_replacement_enabled(false);
1793
1794   // Create a new tab.
1795   chrome::NewTab(browser());
1796   EXPECT_TRUE(browser()->toolbar_model()->search_term_replacement_enabled());
1797
1798   // Switch back to the first tab.
1799   browser()->tab_strip_model()->ActivateTabAt(0, true);
1800   EXPECT_FALSE(browser()->toolbar_model()->search_term_replacement_enabled());
1801 }
1802
1803 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1804                        DontUpdateURLWhileSearchTermReplacementIsDisabled) {
1805   OmniboxView* omnibox_view = NULL;
1806   ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1807   TestToolbarModel* test_toolbar_model = new TestToolbarModel;
1808   scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
1809   browser()->swap_toolbar_models(&toolbar_model);
1810
1811   string16 url_a(ASCIIToUTF16("http://www.a.com/"));
1812   string16 url_b(ASCIIToUTF16("http://www.b.com/"));
1813   string16 url_c(ASCIIToUTF16("http://www.c.com/"));
1814   chrome::FocusLocationBar(browser());
1815   test_toolbar_model->set_text(url_a);
1816   omnibox_view->Update();
1817   EXPECT_EQ(url_a, omnibox_view->GetText());
1818
1819   // Disable search term replacement and update.  Because the omnibox has focus,
1820   // the visible text shouldn't change; see comments in
1821   // OmniboxEditModel::UpdatePermanentText().
1822   browser()->toolbar_model()->set_search_term_replacement_enabled(false);
1823   test_toolbar_model->set_text(url_b);
1824   omnibox_view->Update();
1825   EXPECT_EQ(url_a, omnibox_view->GetText());
1826
1827   // Re-enable search term replacement and ensure updating changes the text.
1828   browser()->toolbar_model()->set_search_term_replacement_enabled(true);
1829   // We have to change the toolbar model text here, or Update() will do nothing.
1830   // This is because the previous update already updated the permanent text.
1831   test_toolbar_model->set_text(url_c);
1832   omnibox_view->Update();
1833   EXPECT_EQ(url_c, omnibox_view->GetText());
1834
1835   // The same test, but using RevertAll() to reset search term replacement.
1836   test_toolbar_model->set_text(url_a);
1837   omnibox_view->Update();
1838   EXPECT_EQ(url_a, omnibox_view->GetText());
1839   browser()->toolbar_model()->set_search_term_replacement_enabled(false);
1840   test_toolbar_model->set_text(url_b);
1841   omnibox_view->Update();
1842   EXPECT_EQ(url_a, omnibox_view->GetText());
1843   omnibox_view->RevertAll();
1844   EXPECT_EQ(url_b, omnibox_view->GetText());
1845   test_toolbar_model->set_text(url_c);
1846   omnibox_view->Update();
1847   EXPECT_EQ(url_c, omnibox_view->GetText());
1848 }
1849
1850 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, InputResetsSearchTermReplacement) {
1851   browser()->toolbar_model()->set_search_term_replacement_enabled(false);
1852   chrome::FocusLocationBar(browser());
1853   ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, 0));
1854   EXPECT_TRUE(browser()->toolbar_model()->search_term_replacement_enabled());
1855 }