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"
28 using base::ASCIIToUTF16
;
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 base::string16
GetFindBarText() {
42 FindBar
* find_bar
= browser()->GetFindBarController()->find_bar();
43 return find_bar
->GetFindText();
46 base::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
, ui::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 IN_PROC_BROWSER_TEST_F(FindInPageTest
, NavigationByKeyEvent
) {
94 ASSERT_TRUE(test_server()->Start());
95 // Make sure Chrome is in the foreground, otherwise sending input
96 // won't do anything and the test will hang.
97 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
98 // First we navigate to any page.
99 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage
));
100 // Show the Find bar.
101 browser()->GetFindBarController()->Show();
103 ui_test_utils::IsViewFocused(browser(), VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
104 ui_test_utils::FindInPage(
105 browser()->tab_strip_model()->GetActiveWebContents(), ASCIIToUTF16("a"),
106 true, false, NULL
, NULL
);
108 // The textfield should be focused after pressing [Enter] on the find button.
109 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_TAB
, false,
110 false, false, false));
111 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_RETURN
, false,
112 false, false, false));
114 ui_test_utils::IsViewFocused(browser(), VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
116 // The textfield should be focused after pressing [Enter] on the find button.
117 ui_test_utils::FindInPage(
118 browser()->tab_strip_model()->GetActiveWebContents(), ASCIIToUTF16("b"),
119 true, false, NULL
, NULL
);
120 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_TAB
, false,
121 false, false, false));
122 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_TAB
, false,
123 false, false, false));
124 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_RETURN
, false,
125 false, false, false));
127 ui_test_utils::IsViewFocused(browser(), VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
130 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
131 // TODO(erg): linux_aura bringup: http://crbug.com/163931
132 #define MAYBE_FocusRestore DISABLED_FocusRestore
134 #define MAYBE_FocusRestore FocusRestore
137 // Flaky because the test server fails to start? See: http://crbug.com/96594.
138 IN_PROC_BROWSER_TEST_F(FindInPageTest
, MAYBE_FocusRestore
) {
139 ASSERT_TRUE(test_server()->Start());
141 GURL url
= test_server()->GetURL("title1.html");
142 ui_test_utils::NavigateToURL(browser(), url
);
144 // Focus the location bar, open and close the find-in-page, focus should
145 // return to the location bar.
146 chrome::FocusLocationBar(browser());
147 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
148 // Ensure the creation of the find bar controller.
149 browser()->GetFindBarController()->Show();
150 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
151 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
152 browser()->GetFindBarController()->EndFindSession(
153 FindBarController::kKeepSelectionOnPage
,
154 FindBarController::kKeepResultsInFindBox
);
155 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
157 // Focus the location bar, find something on the page, close the find box,
158 // focus should go to the page.
159 chrome::FocusLocationBar(browser());
160 chrome::Find(browser());
161 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
162 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
163 ui_test_utils::FindInPage(
164 browser()->tab_strip_model()->GetActiveWebContents(),
165 ASCIIToUTF16("a"), true, false, NULL
, NULL
);
166 browser()->GetFindBarController()->EndFindSession(
167 FindBarController::kKeepSelectionOnPage
,
168 FindBarController::kKeepResultsInFindBox
);
169 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER
));
171 // Focus the location bar, open and close the find box, focus should return to
172 // the location bar (same as before, just checking that http://crbug.com/23599
174 chrome::FocusLocationBar(browser());
175 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
176 browser()->GetFindBarController()->Show();
177 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
178 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
179 browser()->GetFindBarController()->EndFindSession(
180 FindBarController::kKeepSelectionOnPage
,
181 FindBarController::kKeepResultsInFindBox
);
182 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
186 // TODO(phajdan.jr): Disabling due to possible timing issues on XP
187 // interactive_ui_tests.
188 // http://crbug.com/311363
189 IN_PROC_BROWSER_TEST_F(FindInPageTest
, DISABLED_SelectionRestoreOnTabSwitch
) {
190 ASSERT_TRUE(test_server()->Start());
192 // Make sure Chrome is in the foreground, otherwise sending input
193 // won't do anything and the test will hang.
194 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
196 // First we navigate to any page in the current tab (tab A).
197 GURL url
= test_server()->GetURL(kSimplePage
);
198 ui_test_utils::NavigateToURL(browser(), url
);
200 // Show the Find bar.
201 browser()->GetFindBarController()->Show();
204 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
205 browser(), ui::VKEY_A
, false, false, false, false));
206 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
207 browser(), ui::VKEY_B
, false, false, false, false));
208 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
209 browser(), ui::VKEY_C
, false, false, false, false));
210 EXPECT_EQ(ASCIIToUTF16("abc"), GetFindBarText());
213 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
214 browser(), ui::VKEY_LEFT
, false, true, false, false));
215 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
216 browser(), ui::VKEY_LEFT
, false, true, false, false));
217 EXPECT_EQ(ASCIIToUTF16("bc"), GetFindBarSelectedText());
219 // Open another tab (tab B).
220 content::WindowedNotificationObserver
observer(
221 content::NOTIFICATION_LOAD_STOP
,
222 content::NotificationService::AllSources());
223 chrome::AddSelectedTabWithURL(browser(), url
, ui::PAGE_TRANSITION_TYPED
);
226 // Show the Find bar.
227 browser()->GetFindBarController()->Show();
230 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
231 browser(), ui::VKEY_D
, false, false, false, false));
232 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
233 browser(), ui::VKEY_E
, false, false, false, false));
234 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
235 browser(), ui::VKEY_F
, false, false, false, false));
236 EXPECT_EQ(ASCIIToUTF16("def"), GetFindBarText());
239 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
240 browser(), ui::VKEY_HOME
, false, false, false, false));
241 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
242 browser(), ui::VKEY_RIGHT
, false, true, false, false));
243 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
244 browser(), ui::VKEY_RIGHT
, false, true, false, false));
245 EXPECT_EQ(ASCIIToUTF16("de"), GetFindBarSelectedText());
247 // Select tab A. Find bar should select "bc".
248 browser()->tab_strip_model()->ActivateTabAt(0, true);
249 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
250 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
251 EXPECT_EQ(ASCIIToUTF16("bc"), GetFindBarSelectedText());
253 // Select tab B. Find bar should select "de".
254 browser()->tab_strip_model()->ActivateTabAt(1, true);
255 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
256 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
257 EXPECT_EQ(ASCIIToUTF16("de"), GetFindBarSelectedText());
260 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
261 // TODO(erg): linux_aura bringup: http://crbug.com/163931
262 #define MAYBE_FocusRestoreOnTabSwitch DISABLED_FocusRestoreOnTabSwitch
264 #define MAYBE_FocusRestoreOnTabSwitch FocusRestoreOnTabSwitch
267 // Flaky because the test server fails to start? See: http://crbug.com/96594.
268 IN_PROC_BROWSER_TEST_F(FindInPageTest
, MAYBE_FocusRestoreOnTabSwitch
) {
269 ASSERT_TRUE(test_server()->Start());
271 // First we navigate to our test page (tab A).
272 GURL url
= test_server()->GetURL(kSimplePage
);
273 ui_test_utils::NavigateToURL(browser(), url
);
275 chrome::Find(browser());
276 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
277 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
280 ui_test_utils::FindInPage(
281 browser()->tab_strip_model()->GetActiveWebContents(),
282 ASCIIToUTF16("a"), true, false, NULL
, NULL
);
283 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
285 // Open another tab (tab B).
286 content::WindowedNotificationObserver
observer(
287 content::NOTIFICATION_LOAD_STOP
,
288 content::NotificationService::AllSources());
289 chrome::AddSelectedTabWithURL(browser(), url
, ui::PAGE_TRANSITION_TYPED
);
292 // Make sure Find box is open.
293 chrome::Find(browser());
294 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
295 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
298 ui_test_utils::FindInPage(
299 browser()->tab_strip_model()->GetActiveWebContents(),
300 ASCIIToUTF16("b"), true, false, NULL
, NULL
);
301 EXPECT_EQ(ASCIIToUTF16("b"), GetFindBarSelectedText());
303 // Set focus away from the Find bar (to the Location bar).
304 chrome::FocusLocationBar(browser());
305 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
307 // Select tab A. Find bar should get focus.
308 browser()->tab_strip_model()->ActivateTabAt(0, true);
309 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
310 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
311 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
313 // Select tab B. Location bar should get focus.
314 browser()->tab_strip_model()->ActivateTabAt(1, true);
315 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
318 // FindInPage on Mac doesn't use prepopulated values. Search there is global.
319 #if !defined(OS_MACOSX) && !defined(USE_AURA)
320 // Flaky because the test server fails to start? See: http://crbug.com/96594.
321 // This tests that whenever you clear values from the Find box and close it that
322 // it respects that and doesn't show you the last search, as reported in bug:
323 // http://crbug.com/40121. For Aura see bug http://crbug.com/292299.
324 IN_PROC_BROWSER_TEST_F(FindInPageTest
, PrepopulateRespectBlank
) {
325 ASSERT_TRUE(test_server()->Start());
327 // Make sure Chrome is in the foreground, otherwise sending input
328 // won't do anything and the test will hang.
329 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
331 // First we navigate to any page.
332 GURL url
= test_server()->GetURL(kSimplePage
);
333 ui_test_utils::NavigateToURL(browser(), url
);
335 // Show the Find bar.
336 browser()->GetFindBarController()->Show();
339 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
340 browser(), ui::VKEY_A
, false, false, false, false));
342 // We should find "a" here.
343 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
346 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
347 browser(), ui::VKEY_BACK
, false, false, false, false));
349 // Validate we have cleared the text.
350 EXPECT_EQ(base::string16(), GetFindBarText());
352 // Close the Find box.
353 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
354 browser(), ui::VKEY_ESCAPE
, false, false, false, false));
356 // Show the Find bar.
357 browser()->GetFindBarController()->Show();
359 // After the Find box has been reopened, it should not have been prepopulated
361 EXPECT_EQ(base::string16(), GetFindBarText());
363 // Close the Find box.
364 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
365 browser(), ui::VKEY_ESCAPE
, false, false, false, false));
367 // Press F3 to trigger FindNext.
368 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
369 browser(), ui::VKEY_F3
, false, false, false, false));
371 // After the Find box has been reopened, it should still have no prepopulate
373 EXPECT_EQ(base::string16(), GetFindBarText());
377 // Flaky on Win. http://crbug.com/92467
378 // Flaky on ChromeOS. http://crbug.com/118216
379 // Flaky on linux aura. http://crbug.com/163931
380 #if defined(TOOLKIT_VIEWS)
381 #define MAYBE_PasteWithoutTextChange DISABLED_PasteWithoutTextChange
383 #define MAYBE_PasteWithoutTextChange PasteWithoutTextChange
386 IN_PROC_BROWSER_TEST_F(FindInPageTest
, MAYBE_PasteWithoutTextChange
) {
387 ASSERT_TRUE(test_server()->Start());
389 // Make sure Chrome is in the foreground, otherwise sending input
390 // won't do anything and the test will hang.
391 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
393 // First we navigate to any page.
394 GURL url
= test_server()->GetURL(kSimplePage
);
395 ui_test_utils::NavigateToURL(browser(), url
);
397 // Show the Find bar.
398 browser()->GetFindBarController()->Show();
400 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
401 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
404 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
405 browser(), ui::VKEY_A
, false, false, false, false));
407 // We should find "a" here.
408 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
410 // Reload the page to clear the matching result.
411 chrome::Reload(browser(), CURRENT_TAB
);
413 // Focus the Find bar again to make sure the text is selected.
414 browser()->GetFindBarController()->Show();
416 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
417 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
419 // "a" should be selected.
420 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
422 // Press Ctrl-C to copy the content.
423 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
424 browser(), ui::VKEY_C
, true, false, false, false));
427 ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
,
430 // Make sure the text is copied successfully.
431 EXPECT_EQ(ASCIIToUTF16("a"), str
);
433 // Press Ctrl-V to paste the content back, it should start finding even if the
434 // content is not changed.
435 content::Source
<WebContents
> notification_source(
436 browser()->tab_strip_model()->GetActiveWebContents());
437 ui_test_utils::WindowedNotificationObserverWithDetails
438 <FindNotificationDetails
> observer(
439 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE
, notification_source
);
441 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
442 browser(), ui::VKEY_V
, true, false, false, false));
444 ASSERT_NO_FATAL_FAILURE(observer
.Wait());
445 FindNotificationDetails details
;
446 ASSERT_TRUE(observer
.GetDetailsFor(notification_source
.map_key(), &details
));
447 EXPECT_TRUE(details
.number_of_matches() > 0);
451 // TODO(phajdan.jr): Disabling due to possible timing issues on XP
452 // interactive_ui_tests.
453 // http://crbug.com/311363
454 IN_PROC_BROWSER_TEST_F(FindInPageTest
, DISABLED_CtrlEnter
) {
455 ui_test_utils::NavigateToURL(browser(),
456 GURL("data:text/html,This is some text with a "
457 "<a href=\"about:blank\">link</a>."));
459 browser()->GetFindBarController()->Show();
461 // Search for "link".
462 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
463 browser(), ui::VKEY_L
, false, false, false, false));
464 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
465 browser(), ui::VKEY_I
, false, false, false, false));
466 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
467 browser(), ui::VKEY_N
, false, false, false, false));
468 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
469 browser(), ui::VKEY_K
, false, false, false, false));
470 EXPECT_EQ(ASCIIToUTF16("link"), GetFindBarText());
472 ui_test_utils::UrlLoadObserver
observer(
473 GURL("about:blank"), content::NotificationService::AllSources());
475 // Send Ctrl-Enter, should cause navigation to about:blank.
476 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
477 browser(), ui::VKEY_RETURN
, true, false, false, false));