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