1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "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"
29 using content::WebContents;
33 static const char kSimplePage[] = "files/find_in_page/simple.html";
35 class FindInPageTest : public InProcessBrowserTest {
38 FindBarHost::disable_animations_during_testing_ = true;
41 string16 GetFindBarText() {
42 FindBar* find_bar = browser()->GetFindBarController()->find_bar();
43 return find_bar->GetFindText();
46 string16 GetFindBarSelectedText() {
47 FindBarTesting* find_bar =
48 browser()->GetFindBarController()->find_bar()->GetFindBarTesting();
49 return find_bar->GetFindSelectedText();
53 DISALLOW_COPY_AND_ASSIGN(FindInPageTest);
58 // Flaky because the test server fails to start? See: http://crbug.com/96594.
59 IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) {
60 ASSERT_TRUE(test_server()->Start());
62 // First we navigate to our test page (tab A).
63 GURL url = test_server()->GetURL(kSimplePage);
64 ui_test_utils::NavigateToURL(browser(), url);
66 chrome::Find(browser());
68 // Open another tab (tab B).
69 chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED);
71 chrome::Find(browser());
72 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
73 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
76 browser()->tab_strip_model()->ActivateTabAt(0, true);
79 browser()->tab_strip_model()->CloseWebContentsAt(1,
80 TabStripModel::CLOSE_NONE);
82 // Click on the location bar so that Find box loses focus.
83 ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(),
85 // Check the location bar is focused.
86 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
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));
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
97 #define MAYBE_FocusRestore FocusRestore
100 // Flaky because the test server fails to start? See: http://crbug.com/96594.
101 IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_FocusRestore) {
102 ASSERT_TRUE(test_server()->Start());
104 GURL url = test_server()->GetURL("title1.html");
105 ui_test_utils::NavigateToURL(browser(), url);
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));
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));
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
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));
149 // TODO(phajdan.jr): Disabling due to possible timing issues on XP
150 // interactive_ui_tests.
151 // http://crbug.com/311363
152 IN_PROC_BROWSER_TEST_F(FindInPageTest, DISABLED_SelectionRestoreOnTabSwitch) {
153 ASSERT_TRUE(test_server()->Start());
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()));
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);
163 // Show the Find bar.
164 browser()->GetFindBarController()->Show();
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());
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());
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);
189 // Show the Find bar.
190 browser()->GetFindBarController()->Show();
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());
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());
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());
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());
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
227 #define MAYBE_FocusRestoreOnTabSwitch FocusRestoreOnTabSwitch
230 // Flaky because the test server fails to start? See: http://crbug.com/96594.
231 IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_FocusRestoreOnTabSwitch) {
232 ASSERT_TRUE(test_server()->Start());
234 // First we navigate to our test page (tab A).
235 GURL url = test_server()->GetURL(kSimplePage);
236 ui_test_utils::NavigateToURL(browser(), url);
238 chrome::Find(browser());
239 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
240 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
243 ui_test_utils::FindInPage(
244 browser()->tab_strip_model()->GetActiveWebContents(),
245 ASCIIToUTF16("a"), true, false, NULL, NULL);
246 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
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);
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));
261 ui_test_utils::FindInPage(
262 browser()->tab_strip_model()->GetActiveWebContents(),
263 ASCIIToUTF16("b"), true, false, NULL, NULL);
264 EXPECT_EQ(ASCIIToUTF16("b"), GetFindBarSelectedText());
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));
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());
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));
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.
285 IN_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.
290 ASSERT_TRUE(test_server()->Start());
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()));
296 // First we navigate to any page.
297 GURL url = test_server()->GetURL(kSimplePage);
298 ui_test_utils::NavigateToURL(browser(), url);
300 // Show the Find bar.
301 browser()->GetFindBarController()->Show();
304 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
305 browser(), ui::VKEY_A, false, false, false, false));
307 // We should find "a" here.
308 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
311 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
312 browser(), ui::VKEY_BACK, false, false, false, false));
314 // Validate we have cleared the text.
315 EXPECT_EQ(string16(), GetFindBarText());
317 // Close the Find box.
318 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
319 browser(), ui::VKEY_ESCAPE, false, false, false, false));
321 // Show the Find bar.
322 browser()->GetFindBarController()->Show();
324 // After the Find box has been reopened, it should not have been prepopulated
326 EXPECT_EQ(string16(), GetFindBarText());
328 // Close the Find box.
329 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
330 browser(), ui::VKEY_ESCAPE, false, false, false, false));
332 // Press F3 to trigger FindNext.
333 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
334 browser(), ui::VKEY_F3, false, false, false, false));
336 // After the Find box has been reopened, it should still have no prepopulate
338 EXPECT_EQ(string16(), GetFindBarText());
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
347 #define MAYBE_PasteWithoutTextChange PasteWithoutTextChange
350 IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_PasteWithoutTextChange) {
351 ASSERT_TRUE(test_server()->Start());
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()));
357 // First we navigate to any page.
358 GURL url = test_server()->GetURL(kSimplePage);
359 ui_test_utils::NavigateToURL(browser(), url);
361 // Show the Find bar.
362 browser()->GetFindBarController()->Show();
364 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
365 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
368 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
369 browser(), ui::VKEY_A, false, false, false, false));
371 // We should find "a" here.
372 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
374 // Reload the page to clear the matching result.
375 chrome::Reload(browser(), CURRENT_TAB);
377 // Focus the Find bar again to make sure the text is selected.
378 browser()->GetFindBarController()->Show();
380 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
381 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
383 // "a" should be selected.
384 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
386 // Press Ctrl-C to copy the content.
387 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
388 browser(), ui::VKEY_C, true, false, false, false));
391 ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE,
394 // Make sure the text is copied successfully.
395 EXPECT_EQ(ASCIIToUTF16("a"), str);
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);
405 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
406 browser(), ui::VKEY_V, true, false, false, false));
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);
415 // TODO(phajdan.jr): Disabling due to possible timing issues on XP
416 // interactive_ui_tests.
417 // http://crbug.com/311363
418 IN_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>."));
423 browser()->GetFindBarController()->Show();
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());
436 ui_test_utils::UrlLoadObserver observer(
437 GURL("about:blank"), content::NotificationService::AllSources());
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));