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