find_bar_host_interactive_uitest.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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 "base/strings/string_util.h" 6#include "base/strings/utf_string_conversions.h" 7#include "chrome/browser/chrome_notification_types.h" 8#include "chrome/browser/ui/browser.h" 9#include "chrome/browser/ui/browser_commands.h" 10#include "chrome/browser/ui/browser_tabstrip.h" 11#include "chrome/browser/ui/find_bar/find_bar_controller.h" 12#include "chrome/browser/ui/find_bar/find_notification_details.h" 13#include "chrome/browser/ui/tabs/tab_strip_model.h" 14#include "chrome/browser/ui/view_ids.h" 15#include "chrome/browser/ui/views/find_bar_host.h" 16#include "chrome/browser/ui/views/frame/browser_view.h" 17#include "chrome/test/base/in_process_browser_test.h" 18#include "chrome/test/base/interactive_test_utils.h" 19#include "chrome/test/base/ui_test_utils.h" 20#include "content/public/browser/notification_service.h" 21#include "content/public/browser/web_contents.h" 22#include "net/test/spawned_test_server/spawned_test_server.h" 23#include "ui/base/clipboard/clipboard.h" 24#include "ui/events/keycodes/keyboard_codes.h" 25#include "ui/views/focus/focus_manager.h" 26#include "ui/views/view.h" 27#include "ui/views/views_delegate.h" 28 29using content::WebContents; 30 31namespace { 32 33static const char kSimplePage[] = "files/find_in_page/simple.html"; 34 35class FindInPageTest : public InProcessBrowserTest { 36 public: 37 FindInPageTest() { 38 FindBarHost::disable_animations_during_testing_ = true; 39 } 40 41 string16 GetFindBarText() { 42 FindBar* find_bar = browser()->GetFindBarController()->find_bar(); 43 return find_bar->GetFindText(); 44 } 45 46 string16 GetFindBarSelectedText() { 47 FindBarTesting* find_bar = 48 browser()->GetFindBarController()->find_bar()->GetFindBarTesting(); 49 return find_bar->GetFindSelectedText(); 50 } 51 52 private: 53 DISALLOW_COPY_AND_ASSIGN(FindInPageTest); 54}; 55 56} // namespace 57 58// Flaky because the test server fails to start? See: http://crbug.com/96594. 59IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) { 60 ASSERT_TRUE(test_server()->Start()); 61 62 // First we navigate to our test page (tab A). 63 GURL url = test_server()->GetURL(kSimplePage); 64 ui_test_utils::NavigateToURL(browser(), url); 65 66 chrome::Find(browser()); 67 68 // Open another tab (tab B). 69 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED); 70 71 chrome::Find(browser()); 72 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 73 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 74 75 // Select tab A. 76 browser()->tab_strip_model()->ActivateTabAt(0, true); 77 78 // Close tab B. 79 browser()->tab_strip_model()->CloseWebContentsAt(1, 80 TabStripModel::CLOSE_NONE); 81 82 // Click on the location bar so that Find box loses focus. 83 ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), 84 VIEW_ID_OMNIBOX)); 85 // Check the location bar is focused. 86 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 87 88 // This used to crash until bug 1303709 was fixed. 89 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 90 browser(), ui::VKEY_ESCAPE, false, false, false, false)); 91} 92 93#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) 94// TODO(erg): linux_aura bringup: http://crbug.com/163931 95#define MAYBE_FocusRestore DISABLED_FocusRestore 96#else 97#define MAYBE_FocusRestore FocusRestore 98#endif 99 100// Flaky because the test server fails to start? See: http://crbug.com/96594. 101IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_FocusRestore) { 102 ASSERT_TRUE(test_server()->Start()); 103 104 GURL url = test_server()->GetURL("title1.html"); 105 ui_test_utils::NavigateToURL(browser(), url); 106 107 // Focus the location bar, open and close the find-in-page, focus should 108 // return to the location bar. 109 chrome::FocusLocationBar(browser()); 110 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 111 // Ensure the creation of the find bar controller. 112 browser()->GetFindBarController()->Show(); 113 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 114 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 115 browser()->GetFindBarController()->EndFindSession( 116 FindBarController::kKeepSelectionOnPage, 117 FindBarController::kKeepResultsInFindBox); 118 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 119 120 // Focus the location bar, find something on the page, close the find box, 121 // focus should go to the page. 122 chrome::FocusLocationBar(browser()); 123 chrome::Find(browser()); 124 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 125 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 126 ui_test_utils::FindInPage( 127 browser()->tab_strip_model()->GetActiveWebContents(), 128 ASCIIToUTF16("a"), true, false, NULL, NULL); 129 browser()->GetFindBarController()->EndFindSession( 130 FindBarController::kKeepSelectionOnPage, 131 FindBarController::kKeepResultsInFindBox); 132 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); 133 134 // Focus the location bar, open and close the find box, focus should return to 135 // the location bar (same as before, just checking that http://crbug.com/23599 136 // is fixed). 137 chrome::FocusLocationBar(browser()); 138 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 139 browser()->GetFindBarController()->Show(); 140 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 141 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 142 browser()->GetFindBarController()->EndFindSession( 143 FindBarController::kKeepSelectionOnPage, 144 FindBarController::kKeepResultsInFindBox); 145 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 146} 147 148 149// TODO(phajdan.jr): Disabling due to possible timing issues on XP 150// interactive_ui_tests. 151// http://crbug.com/311363 152IN_PROC_BROWSER_TEST_F(FindInPageTest, DISABLED_SelectionRestoreOnTabSwitch) { 153 ASSERT_TRUE(test_server()->Start()); 154 155 // Make sure Chrome is in the foreground, otherwise sending input 156 // won't do anything and the test will hang. 157 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); 158 159 // First we navigate to any page in the current tab (tab A). 160 GURL url = test_server()->GetURL(kSimplePage); 161 ui_test_utils::NavigateToURL(browser(), url); 162 163 // Show the Find bar. 164 browser()->GetFindBarController()->Show(); 165 166 // Search for "abc". 167 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 168 browser(), ui::VKEY_A, false, false, false, false)); 169 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 170 browser(), ui::VKEY_B, false, false, false, false)); 171 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 172 browser(), ui::VKEY_C, false, false, false, false)); 173 EXPECT_EQ(ASCIIToUTF16("abc"), GetFindBarText()); 174 175 // Select "bc". 176 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 177 browser(), ui::VKEY_LEFT, false, true, false, false)); 178 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 179 browser(), ui::VKEY_LEFT, false, true, false, false)); 180 EXPECT_EQ(ASCIIToUTF16("bc"), GetFindBarSelectedText()); 181 182 // Open another tab (tab B). 183 content::WindowedNotificationObserver observer( 184 content::NOTIFICATION_LOAD_STOP, 185 content::NotificationService::AllSources()); 186 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED); 187 observer.Wait(); 188 189 // Show the Find bar. 190 browser()->GetFindBarController()->Show(); 191 192 // Search for "def". 193 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 194 browser(), ui::VKEY_D, false, false, false, false)); 195 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 196 browser(), ui::VKEY_E, false, false, false, false)); 197 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 198 browser(), ui::VKEY_F, false, false, false, false)); 199 EXPECT_EQ(ASCIIToUTF16("def"), GetFindBarText()); 200 201 // Select "de". 202 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 203 browser(), ui::VKEY_HOME, false, false, false, false)); 204 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 205 browser(), ui::VKEY_RIGHT, false, true, false, false)); 206 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 207 browser(), ui::VKEY_RIGHT, false, true, false, false)); 208 EXPECT_EQ(ASCIIToUTF16("de"), GetFindBarSelectedText()); 209 210 // Select tab A. Find bar should select "bc". 211 browser()->tab_strip_model()->ActivateTabAt(0, true); 212 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 213 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 214 EXPECT_EQ(ASCIIToUTF16("bc"), GetFindBarSelectedText()); 215 216 // Select tab B. Find bar should select "de". 217 browser()->tab_strip_model()->ActivateTabAt(1, true); 218 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 219 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 220 EXPECT_EQ(ASCIIToUTF16("de"), GetFindBarSelectedText()); 221} 222 223#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA) 224// TODO(erg): linux_aura bringup: http://crbug.com/163931 225#define MAYBE_FocusRestoreOnTabSwitch DISABLED_FocusRestoreOnTabSwitch 226#else 227#define MAYBE_FocusRestoreOnTabSwitch FocusRestoreOnTabSwitch 228#endif 229 230// Flaky because the test server fails to start? See: http://crbug.com/96594. 231IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_FocusRestoreOnTabSwitch) { 232 ASSERT_TRUE(test_server()->Start()); 233 234 // First we navigate to our test page (tab A). 235 GURL url = test_server()->GetURL(kSimplePage); 236 ui_test_utils::NavigateToURL(browser(), url); 237 238 chrome::Find(browser()); 239 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 240 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 241 242 // Search for 'a'. 243 ui_test_utils::FindInPage( 244 browser()->tab_strip_model()->GetActiveWebContents(), 245 ASCIIToUTF16("a"), true, false, NULL, NULL); 246 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText()); 247 248 // Open another tab (tab B). 249 content::WindowedNotificationObserver observer( 250 content::NOTIFICATION_LOAD_STOP, 251 content::NotificationService::AllSources()); 252 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED); 253 observer.Wait(); 254 255 // Make sure Find box is open. 256 chrome::Find(browser()); 257 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 258 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 259 260 // Search for 'b'. 261 ui_test_utils::FindInPage( 262 browser()->tab_strip_model()->GetActiveWebContents(), 263 ASCIIToUTF16("b"), true, false, NULL, NULL); 264 EXPECT_EQ(ASCIIToUTF16("b"), GetFindBarSelectedText()); 265 266 // Set focus away from the Find bar (to the Location bar). 267 chrome::FocusLocationBar(browser()); 268 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 269 270 // Select tab A. Find bar should get focus. 271 browser()->tab_strip_model()->ActivateTabAt(0, true); 272 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 273 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 274 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText()); 275 276 // Select tab B. Location bar should get focus. 277 browser()->tab_strip_model()->ActivateTabAt(1, true); 278 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 279} 280 281// Flaky because the test server fails to start? See: http://crbug.com/96594. 282// This tests that whenever you clear values from the Find box and close it that 283// it respects that and doesn't show you the last search, as reported in bug: 284// http://crbug.com/40121. For Aura see bug http://crbug.com/292299. 285IN_PROC_BROWSER_TEST_F(FindInPageTest, PrepopulateRespectBlank) { 286#if defined(OS_MACOSX) || defined(USE_AURA) 287 // FindInPage on Mac doesn't use prepopulated values. Search there is global. 288 return; 289#endif 290 ASSERT_TRUE(test_server()->Start()); 291 292 // Make sure Chrome is in the foreground, otherwise sending input 293 // won't do anything and the test will hang. 294 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); 295 296 // First we navigate to any page. 297 GURL url = test_server()->GetURL(kSimplePage); 298 ui_test_utils::NavigateToURL(browser(), url); 299 300 // Show the Find bar. 301 browser()->GetFindBarController()->Show(); 302 303 // Search for "a". 304 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 305 browser(), ui::VKEY_A, false, false, false, false)); 306 307 // We should find "a" here. 308 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText()); 309 310 // Delete "a". 311 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 312 browser(), ui::VKEY_BACK, false, false, false, false)); 313 314 // Validate we have cleared the text. 315 EXPECT_EQ(string16(), GetFindBarText()); 316 317 // Close the Find box. 318 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 319 browser(), ui::VKEY_ESCAPE, false, false, false, false)); 320 321 // Show the Find bar. 322 browser()->GetFindBarController()->Show(); 323 324 // After the Find box has been reopened, it should not have been prepopulated 325 // with "a" again. 326 EXPECT_EQ(string16(), GetFindBarText()); 327 328 // Close the Find box. 329 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 330 browser(), ui::VKEY_ESCAPE, false, false, false, false)); 331 332 // Press F3 to trigger FindNext. 333 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 334 browser(), ui::VKEY_F3, false, false, false, false)); 335 336 // After the Find box has been reopened, it should still have no prepopulate 337 // value. 338 EXPECT_EQ(string16(), GetFindBarText()); 339} 340 341// Flaky on Win. http://crbug.com/92467 342// Flaky on ChromeOS. http://crbug.com/118216 343// Flaky on linux aura. http://crbug.com/163931 344#if defined(TOOLKIT_VIEWS) 345#define MAYBE_PasteWithoutTextChange DISABLED_PasteWithoutTextChange 346#else 347#define MAYBE_PasteWithoutTextChange PasteWithoutTextChange 348#endif 349 350IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_PasteWithoutTextChange) { 351 ASSERT_TRUE(test_server()->Start()); 352 353 // Make sure Chrome is in the foreground, otherwise sending input 354 // won't do anything and the test will hang. 355 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); 356 357 // First we navigate to any page. 358 GURL url = test_server()->GetURL(kSimplePage); 359 ui_test_utils::NavigateToURL(browser(), url); 360 361 // Show the Find bar. 362 browser()->GetFindBarController()->Show(); 363 364 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 365 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 366 367 // Search for "a". 368 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 369 browser(), ui::VKEY_A, false, false, false, false)); 370 371 // We should find "a" here. 372 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText()); 373 374 // Reload the page to clear the matching result. 375 chrome::Reload(browser(), CURRENT_TAB); 376 377 // Focus the Find bar again to make sure the text is selected. 378 browser()->GetFindBarController()->Show(); 379 380 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 381 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 382 383 // "a" should be selected. 384 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText()); 385 386 // Press Ctrl-C to copy the content. 387 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 388 browser(), ui::VKEY_C, true, false, false, false)); 389 390 string16 str; 391 ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, 392 &str); 393 394 // Make sure the text is copied successfully. 395 EXPECT_EQ(ASCIIToUTF16("a"), str); 396 397 // Press Ctrl-V to paste the content back, it should start finding even if the 398 // content is not changed. 399 content::Source<WebContents> notification_source( 400 browser()->tab_strip_model()->GetActiveWebContents()); 401 ui_test_utils::WindowedNotificationObserverWithDetails 402 <FindNotificationDetails> observer( 403 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, notification_source); 404 405 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 406 browser(), ui::VKEY_V, true, false, false, false)); 407 408 ASSERT_NO_FATAL_FAILURE(observer.Wait()); 409 FindNotificationDetails details; 410 ASSERT_TRUE(observer.GetDetailsFor(notification_source.map_key(), &details)); 411 EXPECT_TRUE(details.number_of_matches() > 0); 412} 413 414#if defined(OS_WIN) 415// TODO(phajdan.jr): Disabling due to possible timing issues on XP 416// interactive_ui_tests. 417// http://crbug.com/311363 418IN_PROC_BROWSER_TEST_F(FindInPageTest, DISABLED_CtrlEnter) { 419 ui_test_utils::NavigateToURL(browser(), 420 GURL("data:text/html,This is some text with a " 421 "<a href=\"about:blank\">link</a>.")); 422 423 browser()->GetFindBarController()->Show(); 424 425 // Search for "link". 426 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 427 browser(), ui::VKEY_L, false, false, false, false)); 428 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 429 browser(), ui::VKEY_I, false, false, false, false)); 430 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 431 browser(), ui::VKEY_N, false, false, false, false)); 432 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 433 browser(), ui::VKEY_K, false, false, false, false)); 434 EXPECT_EQ(ASCIIToUTF16("link"), GetFindBarText()); 435 436 ui_test_utils::UrlLoadObserver observer( 437 GURL("about:blank"), content::NotificationService::AllSources()); 438 439 // Send Ctrl-Enter, should cause navigation to about:blank. 440 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 441 browser(), ui::VKEY_RETURN, true, false, false, false)); 442 443 observer.Wait(); 444} 445#endif 446