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