Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / ui / omnibox / omnibox_view_browsertest.cc
bloba74309effa78d935327af9cb47091c6cad4e6cf1
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <stdio.h>
7 #include "base/command_line.h"
8 #include "base/scoped_observer.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/app/chrome_command_ids.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_factory.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_commands.h"
21 #include "chrome/browser/ui/browser_window.h"
22 #include "chrome/browser/ui/location_bar/location_bar.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/browser/ui/toolbar/test_toolbar_model.h"
25 #include "chrome/common/chrome_paths.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "chrome/common/url_constants.h"
28 #include "chrome/test/base/in_process_browser_test.h"
29 #include "chrome/test/base/interactive_test_utils.h"
30 #include "chrome/test/base/ui_test_utils.h"
31 #include "components/bookmarks/browser/bookmark_model.h"
32 #include "components/bookmarks/browser/bookmark_utils.h"
33 #include "components/bookmarks/test/bookmark_test_helpers.h"
34 #include "components/history/core/browser/history_service.h"
35 #include "components/history/core/browser/history_service_observer.h"
36 #include "components/omnibox/browser/autocomplete_input.h"
37 #include "components/omnibox/browser/autocomplete_match.h"
38 #include "components/omnibox/browser/history_quick_provider.h"
39 #include "components/omnibox/browser/omnibox_popup_model.h"
40 #include "components/omnibox/browser/omnibox_view.h"
41 #include "components/search_engines/template_url.h"
42 #include "components/search_engines/template_url_service.h"
43 #include "content/public/browser/notification_service.h"
44 #include "content/public/browser/web_contents.h"
45 #include "net/dns/mock_host_resolver.h"
46 #include "ui/base/clipboard/clipboard.h"
47 #include "ui/base/clipboard/scoped_clipboard_writer.h"
48 #include "ui/events/event_constants.h"
49 #include "ui/events/keycodes/keyboard_codes.h"
50 #include "ui/gfx/geometry/point.h"
52 // For fine-grained suppression on flaky tests.
53 #if defined(OS_WIN)
54 #include "base/win/windows_version.h"
55 #endif
57 using base::ASCIIToUTF16;
58 using base::UTF16ToUTF8;
59 using base::Time;
60 using base::TimeDelta;
61 using bookmarks::BookmarkModel;
63 namespace {
65 const char kSearchKeyword[] = "foo";
66 const char kSearchKeyword2[] = "footest.com";
67 const ui::KeyboardCode kSearchKeywordKeys[] = {
68 ui::VKEY_F, ui::VKEY_O, ui::VKEY_O, ui::VKEY_UNKNOWN
70 const ui::KeyboardCode kSearchKeywordPrefixKeys[] = {
71 ui::VKEY_F, ui::VKEY_O, ui::VKEY_UNKNOWN
73 const ui::KeyboardCode kSearchKeywordCompletionKeys[] = {
74 ui::VKEY_O, ui::VKEY_UNKNOWN
76 const char kSearchURL[] = "http://www.foo.com/search?q={searchTerms}";
77 const char kSearchShortName[] = "foo";
78 const char kSearchText[] = "abc";
79 const ui::KeyboardCode kSearchTextKeys[] = {
80 ui::VKEY_A, ui::VKEY_B, ui::VKEY_C, ui::VKEY_UNKNOWN
82 const char kSearchTextURL[] = "http://www.foo.com/search?q=abc";
84 const char kInlineAutocompleteText[] = "def";
85 const ui::KeyboardCode kInlineAutocompleteTextKeys[] = {
86 ui::VKEY_D, ui::VKEY_E, ui::VKEY_F, ui::VKEY_UNKNOWN
89 // Hostnames that shall be blocked by host resolver.
90 const char *kBlockedHostnames[] = {
91 "foo",
92 "*.foo.com",
93 "bar",
94 "*.bar.com",
95 "abc",
96 "*.abc.com",
97 "def",
98 "*.def.com",
99 "*.site.com",
100 "history",
104 const struct TestHistoryEntry {
105 const char* url;
106 const char* title;
107 int visit_count;
108 int typed_count;
109 bool starred;
110 } kHistoryEntries[] = {
111 {"http://www.bar.com/1", "Page 1", 10, 10, false },
112 {"http://www.bar.com/2", "Page 2", 9, 9, false },
113 {"http://www.bar.com/3", "Page 3", 8, 8, false },
114 {"http://www.bar.com/4", "Page 4", 7, 7, false },
115 {"http://www.bar.com/5", "Page 5", 6, 6, false },
116 {"http://www.bar.com/6", "Page 6", 5, 5, false },
117 {"http://www.bar.com/7", "Page 7", 4, 4, false },
118 {"http://www.bar.com/8", "Page 8", 3, 3, false },
119 {"http://www.bar.com/9", "Page 9", 2, 2, false },
120 {"http://www.site.com/path/1", "Site 1", 4, 4, false },
121 {"http://www.site.com/path/2", "Site 2", 3, 3, false },
122 {"http://www.site.com/path/3", "Site 3", 2, 2, false },
124 // To trigger inline autocomplete.
125 {"http://www.def.com", "Page def", 10000, 10000, true },
127 // Used in particular for the desired TLD test. This makes it test
128 // the interesting case when there's an intranet host with the same
129 // name as the .com.
130 {"http://bar/", "Bar", 1, 0, false },
133 // Stores the given text to clipboard.
134 void SetClipboardText(const base::string16& text) {
135 ui::ScopedClipboardWriter writer(ui::CLIPBOARD_TYPE_COPY_PASTE);
136 writer.WriteText(text);
139 #if defined(OS_MACOSX)
140 const int kCtrlOrCmdMask = ui::EF_COMMAND_DOWN;
141 #else
142 const int kCtrlOrCmdMask = ui::EF_CONTROL_DOWN;
143 #endif
145 } // namespace
147 class OmniboxViewTest : public InProcessBrowserTest,
148 public content::NotificationObserver,
149 public history::HistoryServiceObserver {
150 public:
151 OmniboxViewTest() : observer_(this) {}
153 // history::HisoryServiceObserver
154 void OnHistoryServiceLoaded(
155 history::HistoryService* history_service) override {
156 base::MessageLoop::current()->Quit();
159 protected:
160 void SetUpOnMainThread() override {
161 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
162 ASSERT_NO_FATAL_FAILURE(SetupComponents());
163 chrome::FocusLocationBar(browser());
164 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
167 static void GetOmniboxViewForBrowser(
168 const Browser* browser,
169 OmniboxView** omnibox_view) {
170 BrowserWindow* window = browser->window();
171 ASSERT_TRUE(window);
172 LocationBar* location_bar = window->GetLocationBar();
173 ASSERT_TRUE(location_bar);
174 *omnibox_view = location_bar->GetOmniboxView();
175 ASSERT_TRUE(*omnibox_view);
178 void GetOmniboxView(OmniboxView** omnibox_view) {
179 GetOmniboxViewForBrowser(browser(), omnibox_view);
182 static void SendKeyForBrowser(const Browser* browser,
183 ui::KeyboardCode key,
184 int modifiers) {
185 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
186 browser, key,
187 (modifiers & ui::EF_CONTROL_DOWN) != 0,
188 (modifiers & ui::EF_SHIFT_DOWN) != 0,
189 (modifiers & ui::EF_ALT_DOWN) != 0,
190 (modifiers & ui::EF_COMMAND_DOWN) != 0));
193 void SendKey(ui::KeyboardCode key, int modifiers) {
194 SendKeyForBrowser(browser(), key, modifiers);
197 void SendKeySequence(const ui::KeyboardCode* keys) {
198 for (; *keys != ui::VKEY_UNKNOWN; ++keys)
199 ASSERT_NO_FATAL_FAILURE(SendKey(*keys, 0));
202 bool SendKeyAndWait(const Browser* browser,
203 ui::KeyboardCode key,
204 int modifiers,
205 int type,
206 const content::NotificationSource& source)
207 WARN_UNUSED_RESULT {
208 return ui_test_utils::SendKeyPressAndWait(
209 browser, key,
210 (modifiers & ui::EF_CONTROL_DOWN) != 0,
211 (modifiers & ui::EF_SHIFT_DOWN) != 0,
212 (modifiers & ui::EF_ALT_DOWN) != 0,
213 (modifiers & ui::EF_COMMAND_DOWN) != 0,
214 type, source);
217 void WaitForTabOpenOrCloseForBrowser(const Browser* browser,
218 int expected_tab_count) {
219 int tab_count = browser->tab_strip_model()->count();
220 if (tab_count == expected_tab_count)
221 return;
223 content::NotificationRegistrar registrar;
224 registrar.Add(this,
225 (tab_count < expected_tab_count) ?
226 static_cast<int>(chrome::NOTIFICATION_TAB_PARENTED) :
227 static_cast<int>(content::NOTIFICATION_WEB_CONTENTS_DESTROYED),
228 content::NotificationService::AllSources());
230 while (!HasFailure() &&
231 browser->tab_strip_model()->count() != expected_tab_count) {
232 content::RunMessageLoop();
235 ASSERT_EQ(expected_tab_count, browser->tab_strip_model()->count());
238 void WaitForTabOpenOrClose(int expected_tab_count) {
239 WaitForTabOpenOrCloseForBrowser(browser(), expected_tab_count);
242 void WaitForAutocompleteControllerDone() {
243 OmniboxView* omnibox_view = NULL;
244 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
246 AutocompleteController* controller =
247 omnibox_view->model()->autocomplete_controller();
248 ASSERT_TRUE(controller);
250 if (controller->done())
251 return;
253 content::NotificationRegistrar registrar;
254 registrar.Add(this,
255 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
256 content::Source<AutocompleteController>(controller));
258 while (!HasFailure() && !controller->done())
259 content::RunMessageLoop();
261 ASSERT_TRUE(controller->done());
264 void SetupSearchEngine() {
265 Profile* profile = browser()->profile();
266 TemplateURLService* model =
267 TemplateURLServiceFactory::GetForProfile(profile);
268 ASSERT_TRUE(model);
270 ui_test_utils::WaitForTemplateURLServiceToLoad(model);
272 ASSERT_TRUE(model->loaded());
274 TemplateURLData data;
275 data.SetShortName(ASCIIToUTF16(kSearchShortName));
276 data.SetKeyword(ASCIIToUTF16(kSearchKeyword));
277 data.SetURL(kSearchURL);
278 TemplateURL* template_url = new TemplateURL(data);
279 model->Add(template_url);
280 model->SetUserSelectedDefaultSearchProvider(template_url);
282 data.SetKeyword(ASCIIToUTF16(kSearchKeyword2));
283 model->Add(new TemplateURL(data));
285 // Remove built-in template urls, like google.com, bing.com etc., as they
286 // may appear as autocomplete suggests and interfere with our tests.
287 TemplateURLService::TemplateURLVector urls = model->GetTemplateURLs();
288 for (TemplateURLService::TemplateURLVector::const_iterator i = urls.begin();
289 i != urls.end();
290 ++i) {
291 if ((*i)->prepopulate_id() != 0)
292 model->Remove(*i);
296 void AddHistoryEntry(const TestHistoryEntry& entry, const Time& time) {
297 Profile* profile = browser()->profile();
298 history::HistoryService* history_service =
299 HistoryServiceFactory::GetForProfile(
300 profile, ServiceAccessType::EXPLICIT_ACCESS);
301 ASSERT_TRUE(history_service);
303 if (!history_service->BackendLoaded()) {
304 observer_.Add(history_service);
305 content::RunMessageLoop();
308 BookmarkModel* bookmark_model =
309 BookmarkModelFactory::GetForProfile(profile);
310 ASSERT_TRUE(bookmark_model);
311 bookmarks::test::WaitForBookmarkModelToLoad(bookmark_model);
313 GURL url(entry.url);
314 // Add everything in order of time. We don't want to have a time that
315 // is "right now" or it will nondeterministically appear in the results.
316 history_service->AddPageWithDetails(url, base::UTF8ToUTF16(entry.title),
317 entry.visit_count,
318 entry.typed_count, time, false,
319 history::SOURCE_BROWSED);
320 if (entry.starred)
321 bookmarks::AddIfNotBookmarked(bookmark_model, url, base::string16());
322 // Wait at least for the AddPageWithDetails() call to finish.
324 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
325 observer(this);
326 observer.Add(history_service);
327 content::RunMessageLoop();
328 // We don't want to return until all observers have processed this
329 // notification, because some (e.g. the in-memory history database) may do
330 // something important. Since we don't know where in the observer list we
331 // stand, just spin the message loop once more to allow the current
332 // callstack to complete.
333 content::RunAllPendingInMessageLoop();
337 void SetupHistory() {
338 // Add enough history pages containing |kSearchText| to trigger
339 // open history page url in autocomplete result.
340 for (size_t i = 0; i < arraysize(kHistoryEntries); i++) {
341 // Add everything in order of time. We don't want to have a time that
342 // is "right now" or it will nondeterministically appear in the results.
343 Time t = Time::Now() - TimeDelta::FromHours(i + 1);
344 ASSERT_NO_FATAL_FAILURE(AddHistoryEntry(kHistoryEntries[i], t));
348 void SetupHostResolver() {
349 for (size_t i = 0; i < arraysize(kBlockedHostnames); ++i)
350 host_resolver()->AddSimulatedFailure(kBlockedHostnames[i]);
353 void SetupComponents() {
354 ASSERT_NO_FATAL_FAILURE(SetupHostResolver());
355 ASSERT_NO_FATAL_FAILURE(SetupSearchEngine());
356 ASSERT_NO_FATAL_FAILURE(SetupHistory());
359 void Observe(int type,
360 const content::NotificationSource& source,
361 const content::NotificationDetails& details) override {
362 switch (type) {
363 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED:
364 case chrome::NOTIFICATION_TAB_PARENTED:
365 case chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY:
366 break;
367 default:
368 FAIL() << "Unexpected notification type";
370 base::MessageLoop::current()->Quit();
373 void OnURLsModified(history::HistoryService* history_service,
374 const history::URLRows& changed_urls) override {
375 base::MessageLoop::current()->Quit();
378 private:
379 ScopedObserver<history::HistoryService, OmniboxViewTest> observer_;
381 DISALLOW_COPY_AND_ASSIGN(OmniboxViewTest);
384 // Test if ctrl-* accelerators are workable in omnibox.
385 // See http://crbug.com/19193: omnibox blocks ctrl-* commands
387 // Flaky on interactive tests (dbg), http://crbug.com/69433
388 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DISABLED_BrowserAccelerators) {
389 OmniboxView* omnibox_view = NULL;
390 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
392 int tab_count = browser()->tab_strip_model()->count();
394 // Create a new Tab.
395 chrome::NewTab(browser());
396 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count + 1));
398 // Select the first Tab.
399 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_1, kCtrlOrCmdMask));
400 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
402 chrome::FocusLocationBar(browser());
404 // Select the second Tab.
405 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_2, kCtrlOrCmdMask));
406 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
408 chrome::FocusLocationBar(browser());
410 // Try ctrl-w to close a Tab.
411 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_W, kCtrlOrCmdMask));
412 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count));
414 // Try ctrl-l to focus location bar.
415 omnibox_view->SetUserText(ASCIIToUTF16("Hello world"));
416 EXPECT_FALSE(omnibox_view->IsSelectAll());
417 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_L, kCtrlOrCmdMask));
418 EXPECT_TRUE(omnibox_view->IsSelectAll());
420 // Try editing the location bar text.
421 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, 0));
422 EXPECT_FALSE(omnibox_view->IsSelectAll());
423 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_S, 0));
424 EXPECT_EQ(ASCIIToUTF16("Hello worlds"), omnibox_view->GetText());
426 // Try ctrl-x to cut text.
427 #if defined(OS_MACOSX)
428 // Mac uses alt-left/right to select a word.
429 ASSERT_NO_FATAL_FAILURE(
430 SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN));
431 #else
432 ASSERT_NO_FATAL_FAILURE(
433 SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN));
434 #endif
435 EXPECT_FALSE(omnibox_view->IsSelectAll());
436 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_X, kCtrlOrCmdMask));
437 EXPECT_EQ(ASCIIToUTF16("Hello "), omnibox_view->GetText());
439 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
440 // Try alt-f4 to close the browser.
441 ASSERT_TRUE(SendKeyAndWait(
442 browser(), ui::VKEY_F4, ui::EF_ALT_DOWN,
443 chrome::NOTIFICATION_BROWSER_CLOSED,
444 content::Source<Browser>(browser())));
445 #endif
448 // Fails on Linux. http://crbug.com/408634
449 #if defined(OS_LINUX)
450 #define MAYBE_PopupAccelerators DISABLED_PopupAccelerators
451 #else
452 #define MAYBE_PopupAccelerators PopupAccelerators
453 #endif
455 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_PopupAccelerators) {
456 #if defined(OS_WIN)
457 // Flaky on XP bot. http://crbug.com/499155
458 if (base::win::GetVersion() <= base::win::VERSION_XP)
459 return;
460 #endif
461 // Create a popup.
462 Browser* popup = CreateBrowserForPopup(browser()->profile());
463 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup));
464 OmniboxView* omnibox_view = NULL;
465 ASSERT_NO_FATAL_FAILURE(
466 GetOmniboxViewForBrowser(popup, &omnibox_view));
467 chrome::FocusLocationBar(popup);
468 EXPECT_TRUE(omnibox_view->IsSelectAll());
470 #if !defined(OS_MACOSX)
471 // Try ctrl-w to close the popup.
472 // This piece of code doesn't work on Mac, because the Browser object won't
473 // be destroyed before finishing the current message loop iteration, thus
474 // No BROWSER_CLOSED notification will be sent.
475 ASSERT_TRUE(SendKeyAndWait(
476 popup, ui::VKEY_W, ui::EF_CONTROL_DOWN,
477 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(popup)));
479 // Create another popup.
480 popup = CreateBrowserForPopup(browser()->profile());
481 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(popup));
482 ASSERT_NO_FATAL_FAILURE(
483 GetOmniboxViewForBrowser(popup, &omnibox_view));
484 #endif
486 // Set the edit text to "Hello world".
487 omnibox_view->SetUserText(ASCIIToUTF16("Hello world"));
488 chrome::FocusLocationBar(popup);
489 EXPECT_TRUE(omnibox_view->IsSelectAll());
491 // Try editing the location bar text -- should be disallowed.
492 ASSERT_NO_FATAL_FAILURE(SendKeyForBrowser(popup, ui::VKEY_S, 0));
493 EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view->GetText());
494 EXPECT_TRUE(omnibox_view->IsSelectAll());
496 ASSERT_NO_FATAL_FAILURE(
497 SendKeyForBrowser(popup, ui::VKEY_X, kCtrlOrCmdMask));
498 EXPECT_EQ(ASCIIToUTF16("Hello world"), omnibox_view->GetText());
499 EXPECT_TRUE(omnibox_view->IsSelectAll());
501 #if !defined(OS_CHROMEOS) && !defined(OS_MACOSX)
502 // Try alt-f4 to close the popup.
503 ASSERT_TRUE(SendKeyAndWait(
504 popup, ui::VKEY_F4, ui::EF_ALT_DOWN,
505 chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(popup)));
506 #endif
509 // http://crbug.com/133341
510 #if defined(OS_LINUX)
511 #define MAYBE_BackspaceInKeywordMode DISABLED_BackspaceInKeywordMode
512 #else
513 #define MAYBE_BackspaceInKeywordMode BackspaceInKeywordMode
514 #endif
516 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_BackspaceInKeywordMode) {
517 OmniboxView* omnibox_view = NULL;
518 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
520 // Trigger keyword hint mode.
521 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
522 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
523 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
525 // Trigger keyword mode.
526 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
527 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
528 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
530 // Backspace without search text should bring back keyword hint mode.
531 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
532 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
533 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
535 // Trigger keyword mode again.
536 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
537 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
538 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
540 // Input something as search text.
541 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
543 // Should stay in keyword mode while deleting search text by pressing
544 // backspace.
545 for (size_t i = 0; i < arraysize(kSearchText) - 1; ++i) {
546 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
547 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
548 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
551 // Input something as search text.
552 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
554 // Move cursor to the beginning of the search text.
555 #if defined(OS_MACOSX)
556 // Home doesn't work on Mac trybot.
557 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, ui::EF_CONTROL_DOWN));
558 #else
559 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_HOME, 0));
560 #endif
561 // Backspace at the beginning of the search text shall turn off
562 // the keyword mode.
563 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
564 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
565 ASSERT_EQ(base::string16(), omnibox_view->model()->keyword());
566 ASSERT_EQ(std::string(kSearchKeyword) + kSearchText,
567 UTF16ToUTF8(omnibox_view->GetText()));
570 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, Escape) {
571 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIHistoryURL));
572 chrome::FocusLocationBar(browser());
574 OmniboxView* omnibox_view = NULL;
575 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
577 base::string16 old_text = omnibox_view->GetText();
578 EXPECT_FALSE(old_text.empty());
579 EXPECT_TRUE(omnibox_view->IsSelectAll());
581 // Delete all text in omnibox.
582 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
583 EXPECT_TRUE(omnibox_view->GetText().empty());
585 // Escape shall revert the text in omnibox.
586 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE, 0));
587 EXPECT_EQ(old_text, omnibox_view->GetText());
588 EXPECT_TRUE(omnibox_view->IsSelectAll());
591 #if defined(OS_LINUX)
592 #define MAYBE_DesiredTLD DISABLED_DesiredTLD
593 #else
594 #define MAYBE_DesiredTLD DesiredTLD
595 #endif
597 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_DesiredTLD) {
598 OmniboxView* omnibox_view = NULL;
599 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
600 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
601 ASSERT_TRUE(popup_model);
603 // Test ctrl-Enter.
604 const ui::KeyboardCode kKeys[] = {
605 ui::VKEY_B, ui::VKEY_A, ui::VKEY_R, ui::VKEY_UNKNOWN
607 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys));
608 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
609 ASSERT_TRUE(popup_model->IsOpen());
610 // ctrl-Enter triggers desired_tld feature, thus www.bar.com shall be
611 // opened.
612 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, ui::EF_CONTROL_DOWN,
613 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
614 content::Source<content::NavigationController>(
615 &browser()->tab_strip_model()->GetActiveWebContents()->
616 GetController())));
618 GURL url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
619 EXPECT_EQ("www.bar.com", url.host());
620 EXPECT_EQ("/", url.path());
623 #if defined(OS_LINUX)
624 #define MAYBE_DesiredTLDWithTemporaryText DISABLED_DesiredTLDWithTemporaryText
625 #else
626 #define MAYBE_DesiredTLDWithTemporaryText DesiredTLDWithTemporaryText
627 #endif
629 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_DesiredTLDWithTemporaryText) {
630 OmniboxView* omnibox_view = NULL;
631 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
632 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
633 ASSERT_TRUE(popup_model);
635 Profile* profile = browser()->profile();
636 TemplateURLService* template_url_service =
637 TemplateURLServiceFactory::GetForProfile(profile);
639 // Add a non-substituting keyword. This ensures the popup will have a
640 // non-verbatim entry with "ab" as a prefix. This way, by arrowing down, we
641 // can set "abc" as temporary text in the omnibox.
642 TemplateURLData data;
643 data.SetShortName(ASCIIToUTF16("abc"));
644 data.SetKeyword(ASCIIToUTF16(kSearchText));
645 data.SetURL("http://abc.com/");
646 template_url_service->Add(new TemplateURL(data));
648 // Send "ab", so that an "abc" entry appears in the popup.
649 const ui::KeyboardCode kSearchTextPrefixKeys[] = {
650 ui::VKEY_A, ui::VKEY_B, ui::VKEY_UNKNOWN
652 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextPrefixKeys));
653 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
654 ASSERT_TRUE(popup_model->IsOpen());
656 // Arrow down to the "abc" entry in the popup.
657 size_t size = popup_model->result().size();
658 while (popup_model->selected_line() < size - 1) {
659 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
660 if (omnibox_view->GetText() == ASCIIToUTF16("abc"))
661 break;
663 ASSERT_EQ(ASCIIToUTF16("abc"), omnibox_view->GetText());
665 // Hitting ctrl-enter should navigate based on the current text rather than
666 // the original input, i.e. to www.abc.com instead of www.ab.com.
667 ASSERT_TRUE(SendKeyAndWait(
668 browser(), ui::VKEY_RETURN, ui::EF_CONTROL_DOWN,
669 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
670 content::Source<content::NavigationController>(
671 &browser()->tab_strip_model()->GetActiveWebContents()->
672 GetController())));
674 GURL url(browser()->tab_strip_model()->GetActiveWebContents()->GetURL());
675 EXPECT_EQ("www.abc.com", url.host());
676 EXPECT_EQ("/", url.path());
679 // See http://crbug.com/431575.
680 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, ClearUserTextAfterBackgroundCommit) {
681 OmniboxView* omnibox_view = NULL;
682 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
684 // Navigate in first tab and enter text into the omnibox.
685 GURL url1("data:text/html,page1");
686 ui_test_utils::NavigateToURL(browser(), url1);
687 omnibox_view->SetUserText(ASCIIToUTF16("foo"));
688 content::WebContents* contents =
689 browser()->tab_strip_model()->GetActiveWebContents();
691 // Create another tab in the foreground.
692 AddTabAtIndex(1, url1, ui::PAGE_TRANSITION_TYPED);
693 EXPECT_EQ(2, browser()->tab_strip_model()->count());
694 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
696 // Navigate in the first tab, currently in the background.
697 GURL url2("data:text/html,page2");
698 chrome::NavigateParams params(browser(), url2, ui::PAGE_TRANSITION_LINK);
699 params.source_contents = contents;
700 params.disposition = CURRENT_TAB;
701 ui_test_utils::NavigateToURL(&params);
703 // Switch back to the first tab. The user text should be cleared, and the
704 // omnibox should have the new URL.
705 browser()->tab_strip_model()->ActivateTabAt(0, true);
706 EXPECT_EQ(ASCIIToUTF16(url2.spec()), omnibox_view->GetText());
709 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, AltEnter) {
710 OmniboxView* omnibox_view = NULL;
711 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
713 omnibox_view->SetUserText(ASCIIToUTF16(chrome::kChromeUIHistoryURL));
714 int tab_count = browser()->tab_strip_model()->count();
715 // alt-Enter opens a new tab.
716 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RETURN, ui::EF_ALT_DOWN));
717 ASSERT_NO_FATAL_FAILURE(WaitForTabOpenOrClose(tab_count + 1));
720 // http://crbug.com/133354, http://crbug.com/146953
721 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DISABLED_EnterToSearch) {
722 OmniboxView* omnibox_view = NULL;
723 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
724 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
725 ASSERT_TRUE(popup_model);
727 // Test Enter to search.
728 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
729 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
730 ASSERT_TRUE(popup_model->IsOpen());
732 // Check if the default match result is Search Primary Provider.
733 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
734 popup_model->result().default_match()->type);
736 // Open the default match.
737 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, 0,
738 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
739 content::Source<content::NavigationController>(
740 &browser()->tab_strip_model()->GetActiveWebContents()->
741 GetController())));
742 GURL url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
743 EXPECT_EQ(kSearchTextURL, url.spec());
745 // Test that entering a single character then Enter performs a search.
746 const ui::KeyboardCode kSearchSingleCharKeys[] = {
747 ui::VKEY_Z, ui::VKEY_UNKNOWN
749 chrome::FocusLocationBar(browser());
750 EXPECT_TRUE(omnibox_view->IsSelectAll());
751 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchSingleCharKeys));
752 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
753 ASSERT_TRUE(popup_model->IsOpen());
754 EXPECT_EQ("z", UTF16ToUTF8(omnibox_view->GetText()));
756 // Check if the default match result is Search Primary Provider.
757 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
758 popup_model->result().default_match()->type);
760 // Open the default match.
761 ASSERT_TRUE(SendKeyAndWait(browser(), ui::VKEY_RETURN, 0,
762 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
763 content::Source<content::NavigationController>(
764 &browser()->tab_strip_model()->GetActiveWebContents()->
765 GetController())));
766 url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL();
767 EXPECT_EQ("http://www.foo.com/search?q=z", url.spec());
770 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, EscapeToDefaultMatch) {
771 OmniboxView* omnibox_view = NULL;
772 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
773 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
774 ASSERT_TRUE(popup_model);
776 // Input something to trigger inline autocomplete.
777 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
778 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
779 ASSERT_TRUE(popup_model->IsOpen());
781 base::string16 old_text = omnibox_view->GetText();
783 // Make sure inline autocomplete is triggerred.
784 EXPECT_GT(old_text.length(), arraysize(kInlineAutocompleteText) - 1);
786 size_t old_selected_line = popup_model->selected_line();
787 EXPECT_EQ(0U, old_selected_line);
789 // Move to another line with different text.
790 size_t size = popup_model->result().size();
791 while (popup_model->selected_line() < size - 1) {
792 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
793 ASSERT_NE(old_selected_line, popup_model->selected_line());
794 if (old_text != omnibox_view->GetText())
795 break;
798 EXPECT_NE(old_text, omnibox_view->GetText());
800 // Escape shall revert back to the default match item.
801 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_ESCAPE, 0));
802 EXPECT_EQ(old_text, omnibox_view->GetText());
803 EXPECT_EQ(old_selected_line, popup_model->selected_line());
806 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, BasicTextOperations) {
807 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
808 chrome::FocusLocationBar(browser());
810 OmniboxView* omnibox_view = NULL;
811 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
813 base::string16 old_text = omnibox_view->GetText();
814 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), old_text);
815 EXPECT_TRUE(omnibox_view->IsSelectAll());
817 size_t start, end;
818 omnibox_view->GetSelectionBounds(&start, &end);
819 #if defined(OS_WIN) || defined(OS_LINUX)
820 // Views textfields select-all in reverse to show the leading text.
821 std::swap(start, end);
822 #endif
823 EXPECT_EQ(0U, start);
824 EXPECT_EQ(old_text.size(), end);
826 // Move the cursor to the end.
827 #if defined(OS_MACOSX)
828 // End doesn't work on Mac trybot.
829 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E, ui::EF_CONTROL_DOWN));
830 #else
831 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END, 0));
832 #endif
833 EXPECT_FALSE(omnibox_view->IsSelectAll());
835 // Make sure the cursor is placed correctly.
836 omnibox_view->GetSelectionBounds(&start, &end);
837 EXPECT_EQ(old_text.size(), start);
838 EXPECT_EQ(old_text.size(), end);
840 // Insert one character at the end. Make sure we won't insert
841 // anything after the special ZWS mark used in gtk implementation.
842 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, 0));
843 EXPECT_EQ(old_text + base::char16('a'), omnibox_view->GetText());
845 // Delete one character from the end. Make sure we won't delete the special
846 // ZWS mark used in gtk implementation.
847 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
848 EXPECT_EQ(old_text, omnibox_view->GetText());
850 omnibox_view->SelectAll(true);
851 EXPECT_TRUE(omnibox_view->IsSelectAll());
852 omnibox_view->GetSelectionBounds(&start, &end);
853 #if defined(OS_WIN) || defined(OS_LINUX)
854 // Views textfields select-all in reverse to show the leading text.
855 std::swap(start, end);
856 #endif
857 EXPECT_EQ(0U, start);
858 EXPECT_EQ(old_text.size(), end);
860 // Delete the content
861 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE, 0));
862 EXPECT_TRUE(omnibox_view->IsSelectAll());
863 omnibox_view->GetSelectionBounds(&start, &end);
864 EXPECT_EQ(0U, start);
865 EXPECT_EQ(0U, end);
866 EXPECT_TRUE(omnibox_view->GetText().empty());
868 // Check if RevertAll() can set text and cursor correctly.
869 omnibox_view->RevertAll();
870 EXPECT_FALSE(omnibox_view->IsSelectAll());
871 EXPECT_EQ(old_text, omnibox_view->GetText());
872 omnibox_view->GetSelectionBounds(&start, &end);
873 EXPECT_EQ(old_text.size(), start);
874 EXPECT_EQ(old_text.size(), end);
877 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, AcceptKeywordBySpace) {
878 OmniboxView* omnibox_view = NULL;
879 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
881 base::string16 search_keyword(ASCIIToUTF16(kSearchKeyword));
883 // Trigger keyword hint mode.
884 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
885 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
886 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
887 ASSERT_EQ(search_keyword, omnibox_view->GetText());
889 // Trigger keyword mode by space.
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_TRUE(omnibox_view->GetText().empty());
895 // Revert to keyword hint mode.
896 omnibox_view->model()->ClearKeyword();
897 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
898 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
899 ASSERT_EQ(search_keyword, omnibox_view->GetText());
901 // Keyword should also be accepted by typing an ideographic space.
902 omnibox_view->OnBeforePossibleChange();
903 omnibox_view->SetWindowTextAndCaretPos(search_keyword +
904 base::WideToUTF16(L"\x3000"), search_keyword.length() + 1, false, false);
905 omnibox_view->OnAfterPossibleChange();
906 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
907 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
908 ASSERT_TRUE(omnibox_view->GetText().empty());
910 // Revert to keyword hint mode.
911 omnibox_view->model()->ClearKeyword();
912 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
913 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
914 ASSERT_EQ(search_keyword, omnibox_view->GetText());
916 // Keyword shouldn't be accepted by pressing space with a trailing
917 // whitespace.
918 omnibox_view->SetWindowTextAndCaretPos(search_keyword + base::char16(' '),
919 search_keyword.length() + 1, false, false);
920 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
921 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
922 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
923 ASSERT_EQ(search_keyword + ASCIIToUTF16(" "), omnibox_view->GetText());
925 // Keyword shouldn't be accepted by deleting the trailing space.
926 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
927 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
928 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
929 ASSERT_EQ(search_keyword + base::char16(' '), omnibox_view->GetText());
931 // Keyword shouldn't be accepted by pressing space before a trailing space.
932 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
933 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
934 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
935 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
936 ASSERT_EQ(search_keyword + ASCIIToUTF16(" "), omnibox_view->GetText());
938 // Keyword should be accepted by pressing space in the middle of context and
939 // just after the keyword.
940 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
941 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_A, 0));
942 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
943 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
944 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
945 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
946 ASSERT_EQ(ASCIIToUTF16("a "), omnibox_view->GetText());
947 size_t start, end;
948 omnibox_view->GetSelectionBounds(&start, &end);
949 EXPECT_EQ(0U, start);
950 EXPECT_EQ(0U, end);
952 // Keyword shouldn't be accepted by pasting "foo bar".
953 omnibox_view->SetUserText(base::string16());
954 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
955 ASSERT_TRUE(omnibox_view->model()->keyword().empty());
957 omnibox_view->OnBeforePossibleChange();
958 omnibox_view->model()->OnPaste();
959 omnibox_view->SetWindowTextAndCaretPos(search_keyword +
960 ASCIIToUTF16(" bar"), search_keyword.length() + 4, false, false);
961 omnibox_view->OnAfterPossibleChange();
962 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
963 ASSERT_TRUE(omnibox_view->model()->keyword().empty());
964 ASSERT_EQ(search_keyword + ASCIIToUTF16(" bar"), omnibox_view->GetText());
966 // Keyword shouldn't be accepted for case like: "foo b|ar" -> "foo b |ar".
967 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
968 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
969 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
970 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
971 ASSERT_TRUE(omnibox_view->model()->keyword().empty());
972 ASSERT_EQ(search_keyword + ASCIIToUTF16(" b ar"), omnibox_view->GetText());
974 // Keyword could be accepted by pressing space with a selected range at the
975 // end of text.
976 omnibox_view->OnBeforePossibleChange();
977 omnibox_view->OnInlineAutocompleteTextMaybeChanged(
978 search_keyword + ASCIIToUTF16(" "), search_keyword.length());
979 omnibox_view->OnAfterPossibleChange();
980 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
981 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
982 ASSERT_EQ(search_keyword + ASCIIToUTF16(" "), omnibox_view->GetText());
984 omnibox_view->GetSelectionBounds(&start, &end);
985 ASSERT_NE(start, end);
986 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
987 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
988 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
989 ASSERT_EQ(base::string16(), omnibox_view->GetText());
991 // Space should accept keyword even when inline autocomplete is available.
992 omnibox_view->SetUserText(base::string16());
993 const TestHistoryEntry kHistoryFoobar = {
994 "http://www.foobar.com", "Page foobar", 100, 100, true
997 // Add a history entry to trigger inline autocomplete when typing "foo".
998 ASSERT_NO_FATAL_FAILURE(
999 AddHistoryEntry(kHistoryFoobar, Time::Now() - TimeDelta::FromHours(1)));
1001 // Type "fo" to trigger inline autocomplete.
1002 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordPrefixKeys));
1003 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1004 ASSERT_TRUE(omnibox_view->model()->popup_model()->IsOpen());
1005 ASSERT_NE(search_keyword, omnibox_view->GetText());
1007 // Keyword hint shouldn't be visible.
1008 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1009 ASSERT_TRUE(omnibox_view->model()->keyword().empty());
1011 // Add the "o". Inline autocompletion should still happen, but now we
1012 // should also get a keyword hint because we've typed a keyword exactly.
1013 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordCompletionKeys));
1014 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1015 ASSERT_TRUE(omnibox_view->model()->popup_model()->IsOpen());
1016 ASSERT_NE(search_keyword, omnibox_view->GetText());
1017 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1018 ASSERT_FALSE(omnibox_view->model()->keyword().empty());
1020 // Trigger keyword mode by space.
1021 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
1022 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1023 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
1024 ASSERT_TRUE(omnibox_view->GetText().empty());
1026 // Space in the middle of a temporary text, which separates the text into
1027 // keyword and replacement portions, should trigger keyword mode.
1028 omnibox_view->SetUserText(base::string16());
1029 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
1030 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1031 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1032 ASSERT_TRUE(popup_model->IsOpen());
1033 ASSERT_EQ(ASCIIToUTF16("foobar.com"), omnibox_view->GetText());
1034 omnibox_view->model()->OnUpOrDownKeyPressed(1);
1035 omnibox_view->model()->OnUpOrDownKeyPressed(-1);
1036 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
1037 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
1038 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
1039 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
1040 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
1041 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
1042 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
1043 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
1044 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1045 ASSERT_EQ(search_keyword, omnibox_view->model()->keyword());
1046 ASSERT_EQ(ASCIIToUTF16("bar.com"), omnibox_view->GetText());
1048 // Space after temporary text that looks like a keyword, when the original
1049 // input does not look like a keyword, should trigger keyword mode.
1050 omnibox_view->SetUserText(base::string16());
1051 const TestHistoryEntry kHistoryFoo = {
1052 "http://footest.com", "Page footest", 1000, 1000, true
1055 // Add a history entry to trigger HQP matching with text == keyword when
1056 // typing "fo te".
1057 ASSERT_NO_FATAL_FAILURE(
1058 AddHistoryEntry(kHistoryFoo, Time::Now() - TimeDelta::FromMinutes(10)));
1060 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_F, 0));
1061 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_O, 0));
1062 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
1063 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_T, 0));
1064 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_E, 0));
1065 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1066 ASSERT_TRUE(popup_model->IsOpen());
1067 base::string16 search_keyword2(ASCIIToUTF16(kSearchKeyword2));
1068 while ((omnibox_view->GetText() != search_keyword2) &&
1069 (popup_model->selected_line() < popup_model->result().size() - 1))
1070 omnibox_view->model()->OnUpOrDownKeyPressed(1);
1071 ASSERT_EQ(search_keyword2, omnibox_view->GetText());
1072 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_SPACE, 0));
1073 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1074 ASSERT_EQ(search_keyword2, omnibox_view->model()->keyword());
1075 ASSERT_TRUE(omnibox_view->GetText().empty());
1078 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, NonSubstitutingKeywordTest) {
1079 OmniboxView* omnibox_view = NULL;
1080 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1081 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1082 ASSERT_TRUE(popup_model);
1084 Profile* profile = browser()->profile();
1085 TemplateURLService* template_url_service =
1086 TemplateURLServiceFactory::GetForProfile(profile);
1088 // Add a non-default substituting keyword.
1089 TemplateURLData data;
1090 data.SetShortName(ASCIIToUTF16("Search abc"));
1091 data.SetKeyword(ASCIIToUTF16(kSearchText));
1092 data.SetURL("http://abc.com/{searchTerms}");
1093 TemplateURL* template_url = new TemplateURL(data);
1094 template_url_service->Add(template_url);
1096 omnibox_view->SetUserText(base::string16());
1098 // Non-default substituting keyword shouldn't be matched by default.
1099 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
1100 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1101 ASSERT_TRUE(popup_model->IsOpen());
1103 // Check if the default match result is Search Primary Provider.
1104 ASSERT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
1105 popup_model->result().default_match()->type);
1106 ASSERT_EQ(kSearchTextURL,
1107 popup_model->result().default_match()->destination_url.spec());
1109 omnibox_view->SetUserText(base::string16());
1110 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1111 ASSERT_FALSE(popup_model->IsOpen());
1113 // Try a non-substituting keyword.
1114 template_url_service->Remove(template_url);
1115 data.SetShortName(ASCIIToUTF16("abc"));
1116 data.SetURL("http://abc.com/");
1117 template_url_service->Add(new TemplateURL(data));
1119 // We always allow exact matches for non-substituting keywords.
1120 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
1121 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1122 ASSERT_TRUE(popup_model->IsOpen());
1123 ASSERT_EQ(AutocompleteMatchType::HISTORY_KEYWORD,
1124 popup_model->result().default_match()->type);
1125 ASSERT_EQ("http://abc.com/",
1126 popup_model->result().default_match()->destination_url.spec());
1129 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, DeleteItem) {
1130 // Disable the search provider, to make sure the popup contains only history
1131 // items.
1132 TemplateURLService* model =
1133 TemplateURLServiceFactory::GetForProfile(browser()->profile());
1134 model->SetUserSelectedDefaultSearchProvider(NULL);
1136 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
1137 chrome::FocusLocationBar(browser());
1139 OmniboxView* omnibox_view = NULL;
1140 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1142 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1143 ASSERT_TRUE(popup_model);
1145 base::string16 old_text = omnibox_view->GetText();
1147 // Input something that can match history items.
1148 omnibox_view->SetUserText(ASCIIToUTF16("site.com/p"));
1149 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1150 ASSERT_TRUE(popup_model->IsOpen());
1152 // Delete the inline autocomplete part.
1153 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DELETE, 0));
1154 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1155 ASSERT_TRUE(popup_model->IsOpen());
1156 ASSERT_GE(popup_model->result().size(), 3U);
1158 base::string16 user_text = omnibox_view->GetText();
1159 ASSERT_EQ(ASCIIToUTF16("site.com/p"), user_text);
1160 omnibox_view->SelectAll(true);
1161 ASSERT_TRUE(omnibox_view->IsSelectAll());
1163 // Move down.
1164 size_t default_line = popup_model->selected_line();
1165 omnibox_view->model()->OnUpOrDownKeyPressed(1);
1166 ASSERT_EQ(default_line + 1, popup_model->selected_line());
1167 base::string16 selected_text =
1168 popup_model->result().match_at(default_line + 1).fill_into_edit;
1169 // Temporary text is shown.
1170 ASSERT_EQ(selected_text, omnibox_view->GetText());
1171 ASSERT_FALSE(omnibox_view->IsSelectAll());
1173 // Delete the item.
1174 popup_model->TryDeletingCurrentItem();
1175 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1176 // The selected line shouldn't be changed, because we have more than two
1177 // items.
1178 ASSERT_EQ(default_line + 1, popup_model->selected_line());
1179 // Make sure the item is really deleted.
1180 ASSERT_NE(selected_text,
1181 popup_model->result().match_at(default_line + 1).fill_into_edit);
1182 selected_text =
1183 popup_model->result().match_at(default_line + 1).fill_into_edit;
1184 // New temporary text is shown.
1185 ASSERT_EQ(selected_text, omnibox_view->GetText());
1187 // Revert to the default match.
1188 ASSERT_TRUE(omnibox_view->model()->OnEscapeKeyPressed());
1189 ASSERT_EQ(default_line, popup_model->selected_line());
1190 ASSERT_EQ(user_text, omnibox_view->GetText());
1191 ASSERT_TRUE(omnibox_view->IsSelectAll());
1193 // Move down and up to select the default match as temporary text.
1194 omnibox_view->model()->OnUpOrDownKeyPressed(1);
1195 ASSERT_EQ(default_line + 1, popup_model->selected_line());
1196 omnibox_view->model()->OnUpOrDownKeyPressed(-1);
1197 ASSERT_EQ(default_line, popup_model->selected_line());
1199 selected_text = popup_model->result().match_at(default_line).fill_into_edit;
1200 // New temporary text is shown.
1201 ASSERT_EQ(selected_text, omnibox_view->GetText());
1202 ASSERT_FALSE(omnibox_view->IsSelectAll());
1204 #if 0
1205 // TODO(mrossetti): http://crbug.com/82335
1206 // Delete the default item.
1207 popup_model->TryDeletingCurrentItem();
1208 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1209 // The selected line shouldn't be changed, but the default item should have
1210 // been changed.
1211 ASSERT_EQ(default_line, popup_model->selected_line());
1212 // Make sure the item is really deleted.
1213 EXPECT_NE(selected_text,
1214 popup_model->result().match_at(default_line).fill_into_edit);
1215 selected_text =
1216 popup_model->result().match_at(default_line).fill_into_edit;
1217 // New temporary text is shown.
1218 ASSERT_EQ(selected_text, omnibox_view->GetText());
1219 #endif
1221 // As the current selected item is the new default item, pressing Escape key
1222 // should revert all directly.
1223 ASSERT_TRUE(omnibox_view->model()->OnEscapeKeyPressed());
1224 ASSERT_EQ(old_text, omnibox_view->GetText());
1225 ASSERT_TRUE(omnibox_view->IsSelectAll());
1228 // http://crbug.com/133344
1229 #if defined(OS_LINUX)
1230 #define MAYBE_TabAcceptKeyword DISABLED_TabAcceptKeyword
1231 #else
1232 #define MAYBE_TabAcceptKeyword TabAcceptKeyword
1233 #endif
1235 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_TabAcceptKeyword) {
1236 OmniboxView* omnibox_view = NULL;
1237 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1239 base::string16 text = ASCIIToUTF16(kSearchKeyword);
1241 // Trigger keyword hint mode.
1242 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
1243 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1244 ASSERT_EQ(text, omnibox_view->model()->keyword());
1245 ASSERT_EQ(text, omnibox_view->GetText());
1247 // Trigger keyword mode by tab.
1248 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1249 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1250 ASSERT_EQ(text, omnibox_view->model()->keyword());
1251 ASSERT_TRUE(omnibox_view->GetText().empty());
1253 // Revert to keyword hint mode.
1254 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1255 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1256 ASSERT_EQ(text, omnibox_view->model()->keyword());
1257 ASSERT_EQ(text, omnibox_view->GetText());
1259 // The location bar should still have focus.
1260 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1262 // Trigger keyword mode by tab.
1263 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1264 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1265 ASSERT_EQ(text, omnibox_view->model()->keyword());
1266 ASSERT_TRUE(omnibox_view->GetText().empty());
1268 // Revert to keyword hint mode with SHIFT+TAB.
1269 #if defined(OS_MACOSX)
1270 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACKTAB, 0));
1271 #else
1272 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1273 #endif
1274 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1275 ASSERT_EQ(text, omnibox_view->model()->keyword());
1276 ASSERT_EQ(text, omnibox_view->GetText());
1277 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1280 #if !defined(OS_MACOSX)
1281 // Mac intentionally does not support this behavior.
1283 // http://crbug.com/133360
1284 #if defined(OS_LINUX)
1285 #define MAYBE_TabTraverseResultsTest DISABLED_TabTraverseResultsTest
1286 #else
1287 #define MAYBE_TabTraverseResultsTest TabTraverseResultsTest
1288 #endif
1290 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, MAYBE_TabTraverseResultsTest) {
1291 OmniboxView* omnibox_view = NULL;
1292 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1293 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1294 ASSERT_TRUE(popup_model);
1296 // Input something to trigger results.
1297 const ui::KeyboardCode kKeys[] = {
1298 ui::VKEY_B, ui::VKEY_A, ui::VKEY_R, ui::VKEY_UNKNOWN
1300 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys));
1301 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1302 ASSERT_TRUE(popup_model->IsOpen());
1304 size_t old_selected_line = popup_model->selected_line();
1305 EXPECT_EQ(0U, old_selected_line);
1307 // Move down the results.
1308 for (size_t size = popup_model->result().size();
1309 popup_model->selected_line() < size - 1;
1310 old_selected_line = popup_model->selected_line()) {
1311 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1312 ASSERT_LT(old_selected_line, popup_model->selected_line());
1315 // Don't move past the end.
1316 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1317 ASSERT_EQ(old_selected_line, popup_model->selected_line());
1318 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1320 // Move back up the results.
1321 for (; popup_model->selected_line() > 0U;
1322 old_selected_line = popup_model->selected_line()) {
1323 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1324 ASSERT_GT(old_selected_line, popup_model->selected_line());
1327 // Don't move past the beginning.
1328 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1329 ASSERT_EQ(0U, popup_model->selected_line());
1330 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1332 const TestHistoryEntry kHistoryFoo = {
1333 "http://foo/", "Page foo", 1, 1, false
1336 // Add a history entry so "foo" gets multiple matches.
1337 ASSERT_NO_FATAL_FAILURE(
1338 AddHistoryEntry(kHistoryFoo, Time::Now() - TimeDelta::FromHours(1)));
1340 // Load results.
1341 ASSERT_NO_FATAL_FAILURE(omnibox_view->SelectAll(false));
1342 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
1343 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1345 // Trigger keyword mode by tab.
1346 base::string16 text = ASCIIToUTF16(kSearchKeyword);
1347 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1348 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1349 ASSERT_EQ(text, omnibox_view->model()->keyword());
1350 ASSERT_TRUE(omnibox_view->GetText().empty());
1352 // The location bar should still have focus.
1353 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1355 // Pressing tab again should move to the next result and clear keyword
1356 // mode.
1357 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1358 ASSERT_EQ(1U, omnibox_view->model()->popup_model()->selected_line());
1359 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1360 ASSERT_NE(text, omnibox_view->model()->keyword());
1362 // The location bar should still have focus.
1363 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1365 // Moving back up should not show keyword mode.
1366 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, ui::EF_SHIFT_DOWN));
1367 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1368 ASSERT_EQ(text, omnibox_view->model()->keyword());
1370 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1372 #endif
1375 // http://crbug.com/133347
1376 #if defined(OS_LINUX)
1377 #define MAYBE_PersistKeywordModeOnTabSwitch \
1378 DISABLED_PersistKeywordModeOnTabSwitch
1379 #else
1380 #define MAYBE_PersistKeywordModeOnTabSwitch PersistKeywordModeOnTabSwitch
1381 #endif
1383 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1384 MAYBE_PersistKeywordModeOnTabSwitch) {
1385 OmniboxView* omnibox_view = NULL;
1386 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1388 // Trigger keyword hint mode.
1389 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchKeywordKeys));
1390 ASSERT_TRUE(omnibox_view->model()->is_keyword_hint());
1391 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
1393 // Trigger keyword mode.
1394 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_TAB, 0));
1395 ASSERT_FALSE(omnibox_view->model()->is_keyword_hint());
1396 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
1398 // Input something as search text.
1399 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kSearchTextKeys));
1401 // Create a new tab.
1402 chrome::NewTab(browser());
1404 // Switch back to the first tab.
1405 browser()->tab_strip_model()->ActivateTabAt(0, true);
1407 // Make sure we're still in keyword mode.
1408 ASSERT_EQ(kSearchKeyword, UTF16ToUTF8(omnibox_view->model()->keyword()));
1411 // http://crbug.com/133355
1412 #if defined(OS_LINUX)
1413 #define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest \
1414 DISABLED_CtrlKeyPressedWithInlineAutocompleteTest
1415 #else
1416 #define MAYBE_CtrlKeyPressedWithInlineAutocompleteTest \
1417 CtrlKeyPressedWithInlineAutocompleteTest
1418 #endif
1420 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1421 MAYBE_CtrlKeyPressedWithInlineAutocompleteTest) {
1422 OmniboxView* omnibox_view = NULL;
1423 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1424 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1425 ASSERT_TRUE(popup_model);
1427 // Input something to trigger inline autocomplete.
1428 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
1429 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1430 ASSERT_TRUE(popup_model->IsOpen());
1432 base::string16 old_text = omnibox_view->GetText();
1434 // Make sure inline autocomplete is triggerred.
1435 EXPECT_GT(old_text.length(), arraysize(kInlineAutocompleteText) - 1);
1437 // Press ctrl key.
1438 omnibox_view->model()->OnControlKeyChanged(true);
1440 // Inline autocomplete should still be there.
1441 EXPECT_EQ(old_text, omnibox_view->GetText());
1444 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, UndoRedo) {
1445 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));
1446 chrome::FocusLocationBar(browser());
1448 OmniboxView* omnibox_view = NULL;
1449 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1451 base::string16 old_text = omnibox_view->GetText();
1452 EXPECT_EQ(base::UTF8ToUTF16(url::kAboutBlankURL), old_text);
1453 EXPECT_TRUE(omnibox_view->IsSelectAll());
1455 // Delete the text, then undo.
1456 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1457 EXPECT_TRUE(omnibox_view->GetText().empty());
1458 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, kCtrlOrCmdMask));
1459 EXPECT_EQ(old_text, omnibox_view->GetText());
1461 // Redo should delete the text again.
1462 ASSERT_NO_FATAL_FAILURE(
1463 SendKey(ui::VKEY_Z, kCtrlOrCmdMask | ui::EF_SHIFT_DOWN));
1464 EXPECT_TRUE(omnibox_view->GetText().empty());
1466 // The toolkit-views undo manager doesn't support restoring selection. Cocoa
1467 // does, so it needs to be cleared.
1468 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, kCtrlOrCmdMask));
1469 #if defined(OS_MACOSX)
1470 // TODO(tapted): This next line may fail if running a toolkit-views browser
1471 // window on Mac. We should fix the toolkit-views undo manager to restore
1472 // selection rather than deleting this #ifdef.
1473 EXPECT_TRUE(omnibox_view->IsSelectAll());
1474 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, 0));
1475 #endif
1476 EXPECT_FALSE(omnibox_view->IsSelectAll());
1478 // The cursor should be at the end.
1479 size_t start, end;
1480 omnibox_view->GetSelectionBounds(&start, &end);
1481 EXPECT_EQ(old_text.size(), start);
1482 EXPECT_EQ(old_text.size(), end);
1484 // Delete three characters; "about:bl" should not trigger inline autocomplete.
1485 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1486 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1487 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1488 EXPECT_EQ(old_text.substr(0, old_text.size() - 3), omnibox_view->GetText());
1490 // Undo delete.
1491 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, kCtrlOrCmdMask));
1492 EXPECT_EQ(old_text, omnibox_view->GetText());
1494 // Redo delete.
1495 ASSERT_NO_FATAL_FAILURE(
1496 SendKey(ui::VKEY_Z, kCtrlOrCmdMask | ui::EF_SHIFT_DOWN));
1497 EXPECT_EQ(old_text.substr(0, old_text.size() - 3), omnibox_view->GetText());
1499 // Delete everything.
1500 omnibox_view->SelectAll(true);
1501 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1502 EXPECT_TRUE(omnibox_view->GetText().empty());
1504 // Undo delete everything.
1505 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, kCtrlOrCmdMask));
1506 EXPECT_EQ(old_text.substr(0, old_text.size() - 3), omnibox_view->GetText());
1508 // Undo delete two characters.
1509 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_Z, kCtrlOrCmdMask));
1510 EXPECT_EQ(old_text, omnibox_view->GetText());
1513 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, BackspaceDeleteHalfWidthKatakana) {
1514 OmniboxView* omnibox_view = NULL;
1515 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1516 // Insert text: ダ. This is two, 3-byte UTF-8 characters:
1517 // U+FF80 "HALFWIDTH KATAKANA LETTER TA" and
1518 // U+FF9E "HALFWIDTH KATAKANA VOICED SOUND MARK".
1519 omnibox_view->SetUserText(base::UTF8ToUTF16("\357\276\200\357\276\236"));
1520 EXPECT_FALSE(omnibox_view->GetText().empty());
1522 // Move the cursor to the end.
1523 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_END, 0));
1525 // Backspace should delete the character. In http://crbug.com/192743, the bug
1526 // was that nothing was deleted.
1527 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_BACK, 0));
1528 #if defined(OS_MACOSX)
1529 // Cocoa text fields attach the sound mark and delete the whole thing. This
1530 // behavior should remain on Mac even when using a toolkit-views browser
1531 // window.
1532 EXPECT_TRUE(omnibox_view->GetText().empty());
1533 #else
1534 // Toolkit-views text fields delete just the sound mark.
1535 EXPECT_EQ(base::UTF8ToUTF16("\357\276\200"), omnibox_view->GetText());
1536 #endif
1539 // Flaky test. crbug.com/356850
1540 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1541 DISABLED_DoesNotUpdateAutocompleteOnBlur) {
1542 OmniboxView* omnibox_view = NULL;
1543 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1544 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1545 ASSERT_TRUE(popup_model);
1547 // Input something to trigger inline autocomplete.
1548 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kInlineAutocompleteTextKeys));
1549 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1550 ASSERT_TRUE(popup_model->IsOpen());
1551 size_t start, end;
1552 omnibox_view->GetSelectionBounds(&start, &end);
1553 EXPECT_TRUE(start != end);
1554 base::string16 old_autocomplete_text =
1555 omnibox_view->model()->autocomplete_controller()->input_.text();
1557 // Unfocus the omnibox. This should clear the text field selection and
1558 // close the popup, but should not run autocomplete.
1559 // Note: GTK preserves the selection when the omnibox is unfocused.
1560 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
1561 ASSERT_FALSE(popup_model->IsOpen());
1562 omnibox_view->GetSelectionBounds(&start, &end);
1563 EXPECT_TRUE(start == end);
1565 EXPECT_EQ(old_autocomplete_text,
1566 omnibox_view->model()->autocomplete_controller()->input_.text());
1569 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, Paste) {
1570 OmniboxView* omnibox_view = NULL;
1571 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1572 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1573 ASSERT_TRUE(popup_model);
1574 EXPECT_FALSE(popup_model->IsOpen());
1576 // Paste should yield the expected text and open the popup.
1577 SetClipboardText(ASCIIToUTF16(kSearchText));
1578 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
1579 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1580 EXPECT_EQ(ASCIIToUTF16(kSearchText), omnibox_view->GetText());
1581 EXPECT_TRUE(popup_model->IsOpen());
1583 // Close the popup and select all.
1584 omnibox_view->CloseOmniboxPopup();
1585 omnibox_view->SelectAll(false);
1586 EXPECT_FALSE(popup_model->IsOpen());
1588 // Pasting the same text again over itself should re-open the popup.
1589 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
1590 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1591 EXPECT_EQ(ASCIIToUTF16(kSearchText), omnibox_view->GetText());
1592 EXPECT_TRUE(popup_model->IsOpen());
1593 omnibox_view->CloseOmniboxPopup();
1594 EXPECT_FALSE(popup_model->IsOpen());
1596 // Pasting amid text should yield the expected text and re-open the popup.
1597 omnibox_view->SetWindowTextAndCaretPos(ASCIIToUTF16("abcd"), 2, false, false);
1598 SetClipboardText(ASCIIToUTF16("123"));
1599 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_V, kCtrlOrCmdMask));
1600 EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view->GetText());
1601 EXPECT_TRUE(popup_model->IsOpen());
1603 // Ctrl/Cmd+Alt+V should not paste.
1604 ASSERT_NO_FATAL_FAILURE(
1605 SendKey(ui::VKEY_V, kCtrlOrCmdMask | ui::EF_ALT_DOWN));
1606 EXPECT_EQ(ASCIIToUTF16("ab123cd"), omnibox_view->GetText());
1607 // TODO(msw): Test that AltGr+V does not paste.
1610 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CopyURLToClipboard) {
1611 // Set permanent text thus making sure that omnibox treats 'google.com'
1612 // as URL (not as ordinary user input).
1613 TestToolbarModel* test_toolbar_model = new TestToolbarModel;
1614 scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
1615 test_toolbar_model->set_text(ASCIIToUTF16("http://www.google.com/"));
1616 browser()->swap_toolbar_models(&toolbar_model);
1617 OmniboxView* omnibox_view = NULL;
1618 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1619 OmniboxEditModel* edit_model = omnibox_view->model();
1620 ASSERT_NE(static_cast<OmniboxEditModel*>(NULL), edit_model);
1621 edit_model->UpdatePermanentText();
1623 const char* target_url = "http://www.google.com/calendar";
1624 omnibox_view->SetUserText(ASCIIToUTF16(target_url));
1626 // Location bar must have focus.
1627 chrome::FocusLocationBar(browser());
1628 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1630 // Select full URL and copy it to clipboard. General text and html should
1631 // be available.
1632 omnibox_view->SelectAll(true);
1633 EXPECT_TRUE(omnibox_view->IsSelectAll());
1634 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1635 clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1636 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY));
1637 EXPECT_EQ(ASCIIToUTF16(target_url), omnibox_view->GetText());
1638 EXPECT_TRUE(clipboard->IsFormatAvailable(
1639 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1641 // Make sure HTML format isn't written. See
1642 // BookmarkNodeData::WriteToClipboard() for details.
1643 EXPECT_FALSE(clipboard->IsFormatAvailable(
1644 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1646 // These platforms should read bookmark format.
1647 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1648 base::string16 title;
1649 std::string url;
1650 clipboard->ReadBookmark(&title, &url);
1651 EXPECT_EQ(target_url, url);
1652 EXPECT_EQ(ASCIIToUTF16(target_url), title);
1653 #endif
1656 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CutURLToClipboard) {
1657 // Set permanent text thus making sure that omnibox treats 'google.com'
1658 // as URL (not as ordinary user input).
1659 TestToolbarModel* test_toolbar_model = new TestToolbarModel;
1660 scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
1661 test_toolbar_model->set_text(ASCIIToUTF16("http://www.google.com/"));
1662 browser()->swap_toolbar_models(&toolbar_model);
1663 OmniboxView* omnibox_view = NULL;
1664 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1665 OmniboxEditModel* edit_model = omnibox_view->model();
1666 ASSERT_NE(static_cast<OmniboxEditModel*>(NULL), edit_model);
1667 edit_model->UpdatePermanentText();
1669 const char* target_url = "http://www.google.com/calendar";
1670 omnibox_view->SetUserText(ASCIIToUTF16(target_url));
1672 // Location bar must have focus.
1673 chrome::FocusLocationBar(browser());
1674 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1676 // Select full URL and cut it. General text and html should be available
1677 // in the clipboard.
1678 omnibox_view->SelectAll(true);
1679 EXPECT_TRUE(omnibox_view->IsSelectAll());
1680 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1681 clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1682 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT));
1683 EXPECT_EQ(base::string16(), omnibox_view->GetText());
1684 EXPECT_TRUE(clipboard->IsFormatAvailable(
1685 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1687 // Make sure HTML format isn't written. See
1688 // BookmarkNodeData::WriteToClipboard() for details.
1689 EXPECT_FALSE(clipboard->IsFormatAvailable(
1690 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1692 // These platforms should read bookmark format.
1693 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1694 base::string16 title;
1695 std::string url;
1696 clipboard->ReadBookmark(&title, &url);
1697 EXPECT_EQ(target_url, url);
1698 EXPECT_EQ(ASCIIToUTF16(target_url), title);
1699 #endif
1702 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CopyTextToClipboard) {
1703 OmniboxView* omnibox_view = NULL;
1704 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1705 const char* target_text = "foo";
1706 omnibox_view->SetUserText(ASCIIToUTF16(target_text));
1708 // Location bar must have focus.
1709 chrome::FocusLocationBar(browser());
1710 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1712 // Select full text and copy it to the clipboard.
1713 omnibox_view->SelectAll(true);
1714 EXPECT_TRUE(omnibox_view->IsSelectAll());
1715 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1716 clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1717 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_COPY));
1718 EXPECT_TRUE(clipboard->IsFormatAvailable(
1719 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1720 EXPECT_FALSE(clipboard->IsFormatAvailable(
1721 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1722 EXPECT_EQ(ASCIIToUTF16(target_text), omnibox_view->GetText());
1725 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CutTextToClipboard) {
1726 OmniboxView* omnibox_view = NULL;
1727 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1728 const char* target_text = "foo";
1729 omnibox_view->SetUserText(ASCIIToUTF16(target_text));
1731 // Location bar must have focus.
1732 chrome::FocusLocationBar(browser());
1733 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1735 // Select full text and cut it to the clipboard.
1736 omnibox_view->SelectAll(true);
1737 EXPECT_TRUE(omnibox_view->IsSelectAll());
1738 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
1739 clipboard->Clear(ui::CLIPBOARD_TYPE_COPY_PASTE);
1740 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_CUT));
1741 EXPECT_TRUE(clipboard->IsFormatAvailable(
1742 ui::Clipboard::GetPlainTextFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1743 EXPECT_FALSE(clipboard->IsFormatAvailable(
1744 ui::Clipboard::GetHtmlFormatType(), ui::CLIPBOARD_TYPE_COPY_PASTE));
1745 EXPECT_EQ(base::string16(), omnibox_view->GetText());
1748 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, EditSearchEngines) {
1749 // Disable settings-in-a-window to simplify test.
1750 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1751 ::switches::kDisableSettingsWindow);
1752 OmniboxView* omnibox_view = NULL;
1753 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1754 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_EDIT_SEARCH_ENGINES));
1755 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1756 const std::string target_url =
1757 std::string(chrome::kChromeUISettingsURL) + chrome::kSearchEnginesSubPage;
1758 EXPECT_EQ(ASCIIToUTF16(target_url), omnibox_view->GetText());
1759 EXPECT_FALSE(omnibox_view->model()->popup_model()->IsOpen());
1762 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, BeginningShownAfterBlur) {
1763 OmniboxView* omnibox_view = NULL;
1764 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1766 omnibox_view->OnBeforePossibleChange();
1767 omnibox_view->SetWindowTextAndCaretPos(ASCIIToUTF16("data:text/plain,test"),
1768 5U, false, false);
1769 omnibox_view->OnAfterPossibleChange();
1770 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1771 size_t start, end;
1772 omnibox_view->GetSelectionBounds(&start, &end);
1773 EXPECT_EQ(5U, start);
1774 EXPECT_EQ(5U, end);
1776 ui_test_utils::FocusView(browser(), VIEW_ID_TAB_CONTAINER);
1777 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
1779 omnibox_view->GetSelectionBounds(&start, &end);
1780 EXPECT_EQ(0U, start);
1781 EXPECT_EQ(0U, end);
1784 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, CtrlArrowAfterArrowSuggestions) {
1785 OmniboxView* omnibox_view = NULL;
1786 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1787 OmniboxPopupModel* popup_model = omnibox_view->model()->popup_model();
1788 ASSERT_TRUE(popup_model);
1790 // Input something to trigger results.
1791 const ui::KeyboardCode kKeys[] = {
1792 ui::VKEY_B, ui::VKEY_A, ui::VKEY_R, ui::VKEY_UNKNOWN
1794 ASSERT_NO_FATAL_FAILURE(SendKeySequence(kKeys));
1795 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1796 ASSERT_TRUE(popup_model->IsOpen());
1798 ASSERT_EQ(ASCIIToUTF16("bar.com/1"), omnibox_view->GetText());
1800 // Arrow down on a suggestion, and omnibox text should be the suggestion.
1801 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_DOWN, 0));
1802 ASSERT_NO_FATAL_FAILURE(WaitForAutocompleteControllerDone());
1803 ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view->GetText());
1805 // Highlight the last 2 words and the omnibox text should not change.
1806 // Simulating Ctrl-shift-left only once does not seem to highlight anything
1807 // on Linux.
1808 #if defined(OS_MACOSX)
1809 // Mac uses alt-left/right to select a word.
1810 const int modifiers = ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN;
1811 #else
1812 const int modifiers = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN;
1813 #endif
1814 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, modifiers));
1815 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, modifiers));
1816 ASSERT_EQ(ASCIIToUTF16("www.bar.com/2"), omnibox_view->GetText());
1819 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1820 PersistSearchReplacementAcrossTabSwitch) {
1821 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
1822 browser()->toolbar_model()->set_url_replacement_enabled(false);
1824 // Create a new tab.
1825 chrome::NewTab(browser());
1826 EXPECT_TRUE(browser()->toolbar_model()->url_replacement_enabled());
1828 // Switch back to the first tab.
1829 browser()->tab_strip_model()->ActivateTabAt(0, true);
1830 EXPECT_FALSE(browser()->toolbar_model()->url_replacement_enabled());
1833 IN_PROC_BROWSER_TEST_F(OmniboxViewTest,
1834 DontUpdateURLWhileSearchTermReplacementIsDisabled) {
1835 OmniboxView* omnibox_view = NULL;
1836 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1837 TestToolbarModel* test_toolbar_model = new TestToolbarModel;
1838 scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
1839 browser()->swap_toolbar_models(&toolbar_model);
1841 base::string16 url_a(ASCIIToUTF16("http://www.a.com/"));
1842 base::string16 url_b(ASCIIToUTF16("http://www.b.com/"));
1843 base::string16 url_c(ASCIIToUTF16("http://www.c.com/"));
1844 chrome::FocusLocationBar(browser());
1845 test_toolbar_model->set_text(url_a);
1846 omnibox_view->Update();
1847 EXPECT_EQ(url_a, omnibox_view->GetText());
1849 // Disable URL replacement and update. Because the omnibox has focus, the
1850 // visible text shouldn't change; see comments in
1851 // OmniboxEditModel::UpdatePermanentText().
1852 browser()->toolbar_model()->set_url_replacement_enabled(false);
1853 test_toolbar_model->set_text(url_b);
1854 omnibox_view->Update();
1855 EXPECT_EQ(url_a, omnibox_view->GetText());
1857 // Re-enable URL replacement and ensure updating changes the text.
1858 browser()->toolbar_model()->set_url_replacement_enabled(true);
1859 // We have to change the toolbar model text here, or Update() will do nothing.
1860 // This is because the previous update already updated the permanent text.
1861 test_toolbar_model->set_text(url_c);
1862 omnibox_view->Update();
1863 EXPECT_EQ(url_c, omnibox_view->GetText());
1865 // The same test, but using RevertAll() to reset search term replacement.
1866 test_toolbar_model->set_text(url_a);
1867 omnibox_view->Update();
1868 EXPECT_EQ(url_a, omnibox_view->GetText());
1869 browser()->toolbar_model()->set_url_replacement_enabled(false);
1870 test_toolbar_model->set_text(url_b);
1871 omnibox_view->Update();
1872 EXPECT_EQ(url_a, omnibox_view->GetText());
1873 omnibox_view->RevertAll();
1874 EXPECT_EQ(url_b, omnibox_view->GetText());
1875 test_toolbar_model->set_text(url_c);
1876 omnibox_view->Update();
1877 EXPECT_EQ(url_c, omnibox_view->GetText());
1880 namespace {
1882 // Returns the number of characters currently selected in |omnibox_view|.
1883 size_t GetSelectionSize(OmniboxView* omnibox_view) {
1884 size_t start, end;
1885 omnibox_view->GetSelectionBounds(&start, &end);
1886 if (end >= start)
1887 return end - start;
1888 return start - end;
1891 } // namespace
1893 // Test that if the Omnibox has focus, and had everything selected before a
1894 // non-user-initiated update, then it retains the selection after the update.
1895 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, SelectAllStaysAfterUpdate) {
1896 OmniboxView* omnibox_view = nullptr;
1897 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view));
1898 TestToolbarModel* test_toolbar_model = new TestToolbarModel;
1899 scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model);
1900 browser()->swap_toolbar_models(&toolbar_model);
1902 base::string16 url_a(ASCIIToUTF16("http://www.a.com/"));
1903 base::string16 url_b(ASCIIToUTF16("http://www.b.com/"));
1904 chrome::FocusLocationBar(browser());
1906 test_toolbar_model->set_text(url_a);
1907 omnibox_view->Update();
1908 EXPECT_EQ(url_a, omnibox_view->GetText());
1909 EXPECT_TRUE(omnibox_view->IsSelectAll());
1911 // Updating while selected should retain SelectAll().
1912 test_toolbar_model->set_text(url_b);
1913 omnibox_view->Update();
1914 EXPECT_EQ(url_b, omnibox_view->GetText());
1915 EXPECT_TRUE(omnibox_view->IsSelectAll());
1917 // Select nothing, then switch back. Shouldn't gain a selection.
1918 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, 0));
1919 test_toolbar_model->set_text(url_a);
1920 omnibox_view->Update();
1921 EXPECT_EQ(url_a, omnibox_view->GetText());
1922 EXPECT_FALSE(omnibox_view->IsSelectAll());
1924 // Test behavior of the "reversed" attribute of OmniboxView::SelectAll().
1925 test_toolbar_model->set_text(ASCIIToUTF16("AB"));
1926 omnibox_view->Update();
1927 // Should be at the end already. Shift+Left to select "reversed".
1928 EXPECT_EQ(0u, GetSelectionSize(omnibox_view));
1929 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN));
1930 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN));
1931 EXPECT_EQ(2u, GetSelectionSize(omnibox_view));
1932 EXPECT_TRUE(omnibox_view->IsSelectAll());
1934 test_toolbar_model->set_text(ASCIIToUTF16("CD"));
1935 omnibox_view->Update();
1936 EXPECT_EQ(2u, GetSelectionSize(omnibox_view));
1938 // At the start, so Shift+Left should do nothing.
1939 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN));
1940 EXPECT_EQ(2u, GetSelectionSize(omnibox_view));
1942 // And Shift+Right should reduce by one character.
1943 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, ui::EF_SHIFT_DOWN));
1944 EXPECT_EQ(1u, GetSelectionSize(omnibox_view));
1946 // No go to start and select all to the right (not reversed).
1947 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
1948 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0));
1949 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, ui::EF_SHIFT_DOWN));
1950 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, ui::EF_SHIFT_DOWN));
1951 test_toolbar_model->set_text(ASCIIToUTF16("AB"));
1952 omnibox_view->Update();
1953 EXPECT_EQ(2u, GetSelectionSize(omnibox_view));
1955 // Now Shift+Right should do nothing, and Shift+Left should reduce.
1956 // At the end, so Shift+Right should do nothing.
1957 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, ui::EF_SHIFT_DOWN));
1958 EXPECT_EQ(2u, GetSelectionSize(omnibox_view));
1960 // And Left should reduce by one character.
1961 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN));
1962 EXPECT_EQ(1u, GetSelectionSize(omnibox_view));