find_bar_host_interactive_uitest.cc revision bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3
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/base/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 FindBarTesting* find_bar = 43 browser()->GetFindBarController()->find_bar()->GetFindBarTesting(); 44 return find_bar->GetFindText(); 45 } 46 47 string16 GetFindBarSelectedText() { 48 FindBarTesting* find_bar = 49 browser()->GetFindBarController()->find_bar()->GetFindBarTesting(); 50 return find_bar->GetFindSelectedText(); 51 } 52 53 private: 54 DISALLOW_COPY_AND_ASSIGN(FindInPageTest); 55}; 56 57} // namespace 58 59// Flaky because the test server fails to start? See: http://crbug.com/96594. 60IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) { 61 ASSERT_TRUE(test_server()->Start()); 62 63 // First we navigate to our test page (tab A). 64 GURL url = test_server()->GetURL(kSimplePage); 65 ui_test_utils::NavigateToURL(browser(), url); 66 67 chrome::Find(browser()); 68 69 // Open another tab (tab B). 70 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED); 71 72 chrome::Find(browser()); 73 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 74 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 75 76 // Select tab A. 77 browser()->tab_strip_model()->ActivateTabAt(0, true); 78 79 // Close tab B. 80 browser()->tab_strip_model()->CloseWebContentsAt(1, 81 TabStripModel::CLOSE_NONE); 82 83 // Click on the location bar so that Find box loses focus. 84 ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), 85 VIEW_ID_OMNIBOX)); 86 // Check the location bar is focused. 87 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 88 89 // This used to crash until bug 1303709 was fixed. 90 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 91 browser(), ui::VKEY_ESCAPE, false, false, false, false)); 92} 93 94// Flaky because the test server fails to start? See: http://crbug.com/96594. 95IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestore) { 96 ASSERT_TRUE(test_server()->Start()); 97 98 GURL url = test_server()->GetURL("title1.html"); 99 ui_test_utils::NavigateToURL(browser(), url); 100 101 // Focus the location bar, open and close the find-in-page, focus should 102 // return to the location bar. 103 chrome::FocusLocationBar(browser()); 104 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 105 // Ensure the creation of the find bar controller. 106 browser()->GetFindBarController()->Show(); 107 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 108 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 109 browser()->GetFindBarController()->EndFindSession( 110 FindBarController::kKeepSelectionOnPage, 111 FindBarController::kKeepResultsInFindBox); 112 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 113 114 // Focus the location bar, find something on the page, close the find box, 115 // focus should go to the page. 116 chrome::FocusLocationBar(browser()); 117 chrome::Find(browser()); 118 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 119 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 120 ui_test_utils::FindInPage( 121 browser()->tab_strip_model()->GetActiveWebContents(), 122 ASCIIToUTF16("a"), true, false, NULL, NULL); 123 browser()->GetFindBarController()->EndFindSession( 124 FindBarController::kKeepSelectionOnPage, 125 FindBarController::kKeepResultsInFindBox); 126 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); 127 128 // Focus the location bar, open and close the find box, focus should return to 129 // the location bar (same as before, just checking that http://crbug.com/23599 130 // is fixed). 131 chrome::FocusLocationBar(browser()); 132 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 133 browser()->GetFindBarController()->Show(); 134 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 135 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 136 browser()->GetFindBarController()->EndFindSession( 137 FindBarController::kKeepSelectionOnPage, 138 FindBarController::kKeepResultsInFindBox); 139 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 140} 141 142// Flaky because the test server fails to start? See: http://crbug.com/96594. 143IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestoreOnTabSwitch) { 144 ASSERT_TRUE(test_server()->Start()); 145 146 // First we navigate to our test page (tab A). 147 GURL url = test_server()->GetURL(kSimplePage); 148 ui_test_utils::NavigateToURL(browser(), url); 149 150 chrome::Find(browser()); 151 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 152 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 153 154 FindBarTesting* find_bar = 155 browser()->GetFindBarController()->find_bar()->GetFindBarTesting(); 156 157 // Search for 'a'. 158 ui_test_utils::FindInPage( 159 browser()->tab_strip_model()->GetActiveWebContents(), 160 ASCIIToUTF16("a"), true, false, NULL, NULL); 161 EXPECT_TRUE(ASCIIToUTF16("a") == find_bar->GetFindSelectedText()); 162 163 // Open another tab (tab B). 164 content::WindowedNotificationObserver observer( 165 content::NOTIFICATION_LOAD_STOP, 166 content::NotificationService::AllSources()); 167 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED); 168 observer.Wait(); 169 170 // Make sure Find box is open. 171 chrome::Find(browser()); 172 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 173 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 174 175 // Search for 'b'. 176 ui_test_utils::FindInPage( 177 browser()->tab_strip_model()->GetActiveWebContents(), 178 ASCIIToUTF16("b"), true, false, NULL, NULL); 179 EXPECT_TRUE(ASCIIToUTF16("b") == find_bar->GetFindSelectedText()); 180 181 // Set focus away from the Find bar (to the Location bar). 182 chrome::FocusLocationBar(browser()); 183 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 184 185 // Select tab A. Find bar should get focus. 186 browser()->tab_strip_model()->ActivateTabAt(0, true); 187 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 188 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 189 EXPECT_TRUE(ASCIIToUTF16("a") == find_bar->GetFindSelectedText()); 190 191 // Select tab B. Location bar should get focus. 192 browser()->tab_strip_model()->ActivateTabAt(1, true); 193 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 194} 195 196// Flaky because the test server fails to start? See: http://crbug.com/96594. 197// This tests that whenever you clear values from the Find box and close it that 198// it respects that and doesn't show you the last search, as reported in bug: 199// http://crbug.com/40121. 200IN_PROC_BROWSER_TEST_F(FindInPageTest, PrepopulateRespectBlank) { 201#if defined(OS_MACOSX) 202 // FindInPage on Mac doesn't use prepopulated values. Search there is global. 203 return; 204#endif 205 ASSERT_TRUE(test_server()->Start()); 206 207 // Make sure Chrome is in the foreground, otherwise sending input 208 // won't do anything and the test will hang. 209 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); 210 211 // First we navigate to any page. 212 GURL url = test_server()->GetURL(kSimplePage); 213 ui_test_utils::NavigateToURL(browser(), url); 214 215 // Show the Find bar. 216 browser()->GetFindBarController()->Show(); 217 218 // Search for "a". 219 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 220 browser(), ui::VKEY_A, false, false, false, false)); 221 222 // We should find "a" here. 223 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText()); 224 225 // Delete "a". 226 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 227 browser(), ui::VKEY_BACK, false, false, false, false)); 228 229 // Validate we have cleared the text. 230 EXPECT_EQ(string16(), GetFindBarText()); 231 232 // Close the Find box. 233 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 234 browser(), ui::VKEY_ESCAPE, false, false, false, false)); 235 236 // Show the Find bar. 237 browser()->GetFindBarController()->Show(); 238 239 // After the Find box has been reopened, it should not have been prepopulated 240 // with "a" again. 241 EXPECT_EQ(string16(), GetFindBarText()); 242 243 // Close the Find box. 244 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 245 browser(), ui::VKEY_ESCAPE, false, false, false, false)); 246 247 // Press F3 to trigger FindNext. 248 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 249 browser(), ui::VKEY_F3, false, false, false, false)); 250 251 // After the Find box has been reopened, it should still have no prepopulate 252 // value. 253 EXPECT_EQ(string16(), GetFindBarText()); 254} 255 256// Flaky on Win. http://crbug.com/92467 257// Flaky on ChromeOS. http://crbug.com/118216 258#if defined(OS_WIN) || defined(OS_CHROMEOS) 259#define MAYBE_PasteWithoutTextChange DISABLED_PasteWithoutTextChange 260#else 261#define MAYBE_PasteWithoutTextChange PasteWithoutTextChange 262#endif 263 264IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_PasteWithoutTextChange) { 265 ASSERT_TRUE(test_server()->Start()); 266 267 // Make sure Chrome is in the foreground, otherwise sending input 268 // won't do anything and the test will hang. 269 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); 270 271 // First we navigate to any page. 272 GURL url = test_server()->GetURL(kSimplePage); 273 ui_test_utils::NavigateToURL(browser(), url); 274 275 // Show the Find bar. 276 browser()->GetFindBarController()->Show(); 277 278 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 279 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 280 281 // Search for "a". 282 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 283 browser(), ui::VKEY_A, false, false, false, false)); 284 285 // We should find "a" here. 286 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText()); 287 288 // Reload the page to clear the matching result. 289 chrome::Reload(browser(), CURRENT_TAB); 290 291 // Focus the Find bar again to make sure the text is selected. 292 browser()->GetFindBarController()->Show(); 293 294 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), 295 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); 296 297 // "a" should be selected. 298 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText()); 299 300 // Press Ctrl-C to copy the content. 301 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 302 browser(), ui::VKEY_C, true, false, false, false)); 303 304 string16 str; 305 ui::Clipboard::GetForCurrentThread()-> 306 ReadText(ui::Clipboard::BUFFER_STANDARD, &str); 307 308 // Make sure the text is copied successfully. 309 EXPECT_EQ(ASCIIToUTF16("a"), str); 310 311 // Press Ctrl-V to paste the content back, it should start finding even if the 312 // content is not changed. 313 content::Source<WebContents> notification_source( 314 browser()->tab_strip_model()->GetActiveWebContents()); 315 ui_test_utils::WindowedNotificationObserverWithDetails 316 <FindNotificationDetails> observer( 317 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, notification_source); 318 319 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( 320 browser(), ui::VKEY_V, true, false, false, false)); 321 322 ASSERT_NO_FATAL_FAILURE(observer.Wait()); 323 FindNotificationDetails details; 324 ASSERT_TRUE(observer.GetDetailsFor(notification_source.map_key(), &details)); 325 EXPECT_TRUE(details.number_of_matches() > 0); 326} 327