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 // Flaky because the test server fails to start? See: http://crbug.com/96594.
283 // This tests that whenever you clear values from the Find box and close it that
284 // it respects that and doesn't show you the last search, as reported in bug:
285 // http://crbug.com/40121. For Aura see bug http://crbug.com/292299.
286 IN_PROC_BROWSER_TEST_F(FindInPageTest
, PrepopulateRespectBlank
) {
287 #if defined(OS_MACOSX) || defined(USE_AURA)
288 // FindInPage on Mac doesn't use prepopulated values. Search there is global.
291 ASSERT_TRUE(test_server()->Start());
293 // Make sure Chrome is in the foreground, otherwise sending input
294 // won't do anything and the test will hang.
295 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
297 // First we navigate to any page.
298 GURL url
= test_server()->GetURL(kSimplePage
);
299 ui_test_utils::NavigateToURL(browser(), url
);
301 // Show the Find bar.
302 browser()->GetFindBarController()->Show();
305 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
306 browser(), ui::VKEY_A
, false, false, false, false));
308 // We should find "a" here.
309 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
312 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
313 browser(), ui::VKEY_BACK
, false, false, false, false));
315 // Validate we have cleared the text.
316 EXPECT_EQ(base::string16(), GetFindBarText());
318 // Close the Find box.
319 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
320 browser(), ui::VKEY_ESCAPE
, false, false, false, false));
322 // Show the Find bar.
323 browser()->GetFindBarController()->Show();
325 // After the Find box has been reopened, it should not have been prepopulated
327 EXPECT_EQ(base::string16(), GetFindBarText());
329 // Close the Find box.
330 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
331 browser(), ui::VKEY_ESCAPE
, false, false, false, false));
333 // Press F3 to trigger FindNext.
334 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
335 browser(), ui::VKEY_F3
, false, false, false, false));
337 // After the Find box has been reopened, it should still have no prepopulate
339 EXPECT_EQ(base::string16(), GetFindBarText());
342 // Flaky on Win. http://crbug.com/92467
343 // Flaky on ChromeOS. http://crbug.com/118216
344 // Flaky on linux aura. http://crbug.com/163931
345 #if defined(TOOLKIT_VIEWS)
346 #define MAYBE_PasteWithoutTextChange DISABLED_PasteWithoutTextChange
348 #define MAYBE_PasteWithoutTextChange PasteWithoutTextChange
351 IN_PROC_BROWSER_TEST_F(FindInPageTest
, MAYBE_PasteWithoutTextChange
) {
352 ASSERT_TRUE(test_server()->Start());
354 // Make sure Chrome is in the foreground, otherwise sending input
355 // won't do anything and the test will hang.
356 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
358 // First we navigate to any page.
359 GURL url
= test_server()->GetURL(kSimplePage
);
360 ui_test_utils::NavigateToURL(browser(), url
);
362 // Show the Find bar.
363 browser()->GetFindBarController()->Show();
365 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
366 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
369 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
370 browser(), ui::VKEY_A
, false, false, false, false));
372 // We should find "a" here.
373 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
375 // Reload the page to clear the matching result.
376 chrome::Reload(browser(), CURRENT_TAB
);
378 // Focus the Find bar again to make sure the text is selected.
379 browser()->GetFindBarController()->Show();
381 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
382 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
384 // "a" should be selected.
385 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
387 // Press Ctrl-C to copy the content.
388 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
389 browser(), ui::VKEY_C
, true, false, false, false));
392 ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
,
395 // Make sure the text is copied successfully.
396 EXPECT_EQ(ASCIIToUTF16("a"), str
);
398 // Press Ctrl-V to paste the content back, it should start finding even if the
399 // content is not changed.
400 content::Source
<WebContents
> notification_source(
401 browser()->tab_strip_model()->GetActiveWebContents());
402 ui_test_utils::WindowedNotificationObserverWithDetails
403 <FindNotificationDetails
> observer(
404 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE
, notification_source
);
406 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
407 browser(), ui::VKEY_V
, true, false, false, false));
409 ASSERT_NO_FATAL_FAILURE(observer
.Wait());
410 FindNotificationDetails details
;
411 ASSERT_TRUE(observer
.GetDetailsFor(notification_source
.map_key(), &details
));
412 EXPECT_TRUE(details
.number_of_matches() > 0);
416 // TODO(phajdan.jr): Disabling due to possible timing issues on XP
417 // interactive_ui_tests.
418 // http://crbug.com/311363
419 IN_PROC_BROWSER_TEST_F(FindInPageTest
, DISABLED_CtrlEnter
) {
420 ui_test_utils::NavigateToURL(browser(),
421 GURL("data:text/html,This is some text with a "
422 "<a href=\"about:blank\">link</a>."));
424 browser()->GetFindBarController()->Show();
426 // Search for "link".
427 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
428 browser(), ui::VKEY_L
, false, false, false, false));
429 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
430 browser(), ui::VKEY_I
, false, false, false, false));
431 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
432 browser(), ui::VKEY_N
, false, false, false, false));
433 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
434 browser(), ui::VKEY_K
, false, false, false, false));
435 EXPECT_EQ(ASCIIToUTF16("link"), GetFindBarText());
437 ui_test_utils::UrlLoadObserver
observer(
438 GURL("about:blank"), content::NotificationService::AllSources());
440 // Send Ctrl-Enter, should cause navigation to about:blank.
441 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
442 browser(), ui::VKEY_RETURN
, true, false, false, false));