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 base::ASCIIToUTF16
;
30 using content::WebContents
;
34 static const char kSimplePage
[] = "files/find_in_page/simple.html";
36 class FindInPageTest
: public InProcessBrowserTest
{
39 FindBarHost::disable_animations_during_testing_
= true;
42 base::string16
GetFindBarText() {
43 FindBar
* find_bar
= browser()->GetFindBarController()->find_bar();
44 return find_bar
->GetFindText();
47 base::string16
GetFindBarSelectedText() {
48 FindBarTesting
* find_bar
=
49 browser()->GetFindBarController()->find_bar()->GetFindBarTesting();
50 return find_bar
->GetFindSelectedText();
54 DISALLOW_COPY_AND_ASSIGN(FindInPageTest
);
59 // Flaky because the test server fails to start? See: http://crbug.com/96594.
60 IN_PROC_BROWSER_TEST_F(FindInPageTest
, CrashEscHandlers
) {
61 ASSERT_TRUE(test_server()->Start());
63 // First we navigate to our test page (tab A).
64 GURL url
= test_server()->GetURL(kSimplePage
);
65 ui_test_utils::NavigateToURL(browser(), url
);
67 chrome::Find(browser());
69 // Open another tab (tab B).
70 chrome::AddSelectedTabWithURL(browser(), url
, content::PAGE_TRANSITION_TYPED
);
72 chrome::Find(browser());
73 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
74 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
77 browser()->tab_strip_model()->ActivateTabAt(0, true);
80 browser()->tab_strip_model()->CloseWebContentsAt(1,
81 TabStripModel::CLOSE_NONE
);
83 // Click on the location bar so that Find box loses focus.
84 ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(),
86 // Check the location bar is focused.
87 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
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));
94 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
95 // TODO(erg): linux_aura bringup: http://crbug.com/163931
96 #define MAYBE_FocusRestore DISABLED_FocusRestore
98 #define MAYBE_FocusRestore FocusRestore
101 // Flaky because the test server fails to start? See: http://crbug.com/96594.
102 IN_PROC_BROWSER_TEST_F(FindInPageTest
, MAYBE_FocusRestore
) {
103 ASSERT_TRUE(test_server()->Start());
105 GURL url
= test_server()->GetURL("title1.html");
106 ui_test_utils::NavigateToURL(browser(), url
);
108 // Focus the location bar, open and close the find-in-page, focus should
109 // return to the location bar.
110 chrome::FocusLocationBar(browser());
111 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
112 // Ensure the creation of the find bar controller.
113 browser()->GetFindBarController()->Show();
114 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
115 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
116 browser()->GetFindBarController()->EndFindSession(
117 FindBarController::kKeepSelectionOnPage
,
118 FindBarController::kKeepResultsInFindBox
);
119 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
121 // Focus the location bar, find something on the page, close the find box,
122 // focus should go to the page.
123 chrome::FocusLocationBar(browser());
124 chrome::Find(browser());
125 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
126 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
127 ui_test_utils::FindInPage(
128 browser()->tab_strip_model()->GetActiveWebContents(),
129 ASCIIToUTF16("a"), true, false, NULL
, NULL
);
130 browser()->GetFindBarController()->EndFindSession(
131 FindBarController::kKeepSelectionOnPage
,
132 FindBarController::kKeepResultsInFindBox
);
133 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER
));
135 // Focus the location bar, open and close the find box, focus should return to
136 // the location bar (same as before, just checking that http://crbug.com/23599
138 chrome::FocusLocationBar(browser());
139 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
140 browser()->GetFindBarController()->Show();
141 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
142 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
143 browser()->GetFindBarController()->EndFindSession(
144 FindBarController::kKeepSelectionOnPage
,
145 FindBarController::kKeepResultsInFindBox
);
146 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
150 // TODO(phajdan.jr): Disabling due to possible timing issues on XP
151 // interactive_ui_tests.
152 // http://crbug.com/311363
153 IN_PROC_BROWSER_TEST_F(FindInPageTest
, DISABLED_SelectionRestoreOnTabSwitch
) {
154 ASSERT_TRUE(test_server()->Start());
156 // Make sure Chrome is in the foreground, otherwise sending input
157 // won't do anything and the test will hang.
158 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
160 // First we navigate to any page in the current tab (tab A).
161 GURL url
= test_server()->GetURL(kSimplePage
);
162 ui_test_utils::NavigateToURL(browser(), url
);
164 // Show the Find bar.
165 browser()->GetFindBarController()->Show();
168 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
169 browser(), ui::VKEY_A
, false, false, false, false));
170 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
171 browser(), ui::VKEY_B
, false, false, false, false));
172 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
173 browser(), ui::VKEY_C
, false, false, false, false));
174 EXPECT_EQ(ASCIIToUTF16("abc"), GetFindBarText());
177 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
178 browser(), ui::VKEY_LEFT
, false, true, false, false));
179 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
180 browser(), ui::VKEY_LEFT
, false, true, false, false));
181 EXPECT_EQ(ASCIIToUTF16("bc"), GetFindBarSelectedText());
183 // Open another tab (tab B).
184 content::WindowedNotificationObserver
observer(
185 content::NOTIFICATION_LOAD_STOP
,
186 content::NotificationService::AllSources());
187 chrome::AddSelectedTabWithURL(browser(), url
, content::PAGE_TRANSITION_TYPED
);
190 // Show the Find bar.
191 browser()->GetFindBarController()->Show();
194 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
195 browser(), ui::VKEY_D
, false, false, false, false));
196 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
197 browser(), ui::VKEY_E
, false, false, false, false));
198 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
199 browser(), ui::VKEY_F
, false, false, false, false));
200 EXPECT_EQ(ASCIIToUTF16("def"), GetFindBarText());
203 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
204 browser(), ui::VKEY_HOME
, false, false, false, false));
205 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
206 browser(), ui::VKEY_RIGHT
, false, true, false, false));
207 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
208 browser(), ui::VKEY_RIGHT
, false, true, false, false));
209 EXPECT_EQ(ASCIIToUTF16("de"), GetFindBarSelectedText());
211 // Select tab A. Find bar should select "bc".
212 browser()->tab_strip_model()->ActivateTabAt(0, true);
213 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
214 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
215 EXPECT_EQ(ASCIIToUTF16("bc"), GetFindBarSelectedText());
217 // Select tab B. Find bar should select "de".
218 browser()->tab_strip_model()->ActivateTabAt(1, true);
219 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
220 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
221 EXPECT_EQ(ASCIIToUTF16("de"), GetFindBarSelectedText());
224 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
225 // TODO(erg): linux_aura bringup: http://crbug.com/163931
226 #define MAYBE_FocusRestoreOnTabSwitch DISABLED_FocusRestoreOnTabSwitch
228 #define MAYBE_FocusRestoreOnTabSwitch FocusRestoreOnTabSwitch
231 // Flaky because the test server fails to start? See: http://crbug.com/96594.
232 IN_PROC_BROWSER_TEST_F(FindInPageTest
, MAYBE_FocusRestoreOnTabSwitch
) {
233 ASSERT_TRUE(test_server()->Start());
235 // First we navigate to our test page (tab A).
236 GURL url
= test_server()->GetURL(kSimplePage
);
237 ui_test_utils::NavigateToURL(browser(), url
);
239 chrome::Find(browser());
240 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
241 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
244 ui_test_utils::FindInPage(
245 browser()->tab_strip_model()->GetActiveWebContents(),
246 ASCIIToUTF16("a"), true, false, NULL
, NULL
);
247 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
249 // Open another tab (tab B).
250 content::WindowedNotificationObserver
observer(
251 content::NOTIFICATION_LOAD_STOP
,
252 content::NotificationService::AllSources());
253 chrome::AddSelectedTabWithURL(browser(), url
, content::PAGE_TRANSITION_TYPED
);
256 // Make sure Find box is open.
257 chrome::Find(browser());
258 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
259 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
262 ui_test_utils::FindInPage(
263 browser()->tab_strip_model()->GetActiveWebContents(),
264 ASCIIToUTF16("b"), true, false, NULL
, NULL
);
265 EXPECT_EQ(ASCIIToUTF16("b"), GetFindBarSelectedText());
267 // Set focus away from the Find bar (to the Location bar).
268 chrome::FocusLocationBar(browser());
269 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
271 // Select tab A. Find bar should get focus.
272 browser()->tab_strip_model()->ActivateTabAt(0, true);
273 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
274 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
275 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
277 // Select tab B. Location bar should get focus.
278 browser()->tab_strip_model()->ActivateTabAt(1, true);
279 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
282 // FindInPage on Mac doesn't use prepopulated values. Search there is global.
283 #if !defined(OS_MACOSX) && !defined(USE_AURA)
284 // Flaky because the test server fails to start? See: http://crbug.com/96594.
285 // This tests that whenever you clear values from the Find box and close it that
286 // it respects that and doesn't show you the last search, as reported in bug:
287 // http://crbug.com/40121. For Aura see bug http://crbug.com/292299.
288 IN_PROC_BROWSER_TEST_F(FindInPageTest
, PrepopulateRespectBlank
) {
289 ASSERT_TRUE(test_server()->Start());
291 // Make sure Chrome is in the foreground, otherwise sending input
292 // won't do anything and the test will hang.
293 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
295 // First we navigate to any page.
296 GURL url
= test_server()->GetURL(kSimplePage
);
297 ui_test_utils::NavigateToURL(browser(), url
);
299 // Show the Find bar.
300 browser()->GetFindBarController()->Show();
303 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
304 browser(), ui::VKEY_A
, false, false, false, false));
306 // We should find "a" here.
307 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
310 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
311 browser(), ui::VKEY_BACK
, false, false, false, false));
313 // Validate we have cleared the text.
314 EXPECT_EQ(base::string16(), GetFindBarText());
316 // Close the Find box.
317 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
318 browser(), ui::VKEY_ESCAPE
, false, false, false, false));
320 // Show the Find bar.
321 browser()->GetFindBarController()->Show();
323 // After the Find box has been reopened, it should not have been prepopulated
325 EXPECT_EQ(base::string16(), GetFindBarText());
327 // Close the Find box.
328 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
329 browser(), ui::VKEY_ESCAPE
, false, false, false, false));
331 // Press F3 to trigger FindNext.
332 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
333 browser(), ui::VKEY_F3
, false, false, false, false));
335 // After the Find box has been reopened, it should still have no prepopulate
337 EXPECT_EQ(base::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));