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