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 #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
, ui::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
, ui::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 // FindInPage on Mac doesn't use prepopulated values. Search there is global.
282 #if !defined(OS_MACOSX) && !defined(USE_AURA)
283 // Flaky because the test server fails to start? See: http://crbug.com/96594.
284 // This tests that whenever you clear values from the Find box and close it that
285 // it respects that and doesn't show you the last search, as reported in bug:
286 // http://crbug.com/40121. For Aura see bug http://crbug.com/292299.
287 IN_PROC_BROWSER_TEST_F(FindInPageTest
, PrepopulateRespectBlank
) {
288 ASSERT_TRUE(test_server()->Start());
290 // Make sure Chrome is in the foreground, otherwise sending input
291 // won't do anything and the test will hang.
292 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
294 // First we navigate to any page.
295 GURL url
= test_server()->GetURL(kSimplePage
);
296 ui_test_utils::NavigateToURL(browser(), url
);
298 // Show the Find bar.
299 browser()->GetFindBarController()->Show();
302 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
303 browser(), ui::VKEY_A
, false, false, false, false));
305 // We should find "a" here.
306 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
309 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
310 browser(), ui::VKEY_BACK
, false, false, false, false));
312 // Validate we have cleared the text.
313 EXPECT_EQ(base::string16(), GetFindBarText());
315 // Close the Find box.
316 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
317 browser(), ui::VKEY_ESCAPE
, false, false, false, false));
319 // Show the Find bar.
320 browser()->GetFindBarController()->Show();
322 // After the Find box has been reopened, it should not have been prepopulated
324 EXPECT_EQ(base::string16(), GetFindBarText());
326 // Close the Find box.
327 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
328 browser(), ui::VKEY_ESCAPE
, false, false, false, false));
330 // Press F3 to trigger FindNext.
331 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
332 browser(), ui::VKEY_F3
, false, false, false, false));
334 // After the Find box has been reopened, it should still have no prepopulate
336 EXPECT_EQ(base::string16(), GetFindBarText());
340 // Flaky on Win. http://crbug.com/92467
341 // Flaky on ChromeOS. http://crbug.com/118216
342 // Flaky on linux aura. http://crbug.com/163931
343 #if defined(TOOLKIT_VIEWS)
344 #define MAYBE_PasteWithoutTextChange DISABLED_PasteWithoutTextChange
346 #define MAYBE_PasteWithoutTextChange PasteWithoutTextChange
349 IN_PROC_BROWSER_TEST_F(FindInPageTest
, MAYBE_PasteWithoutTextChange
) {
350 ASSERT_TRUE(test_server()->Start());
352 // Make sure Chrome is in the foreground, otherwise sending input
353 // won't do anything and the test will hang.
354 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
356 // First we navigate to any page.
357 GURL url
= test_server()->GetURL(kSimplePage
);
358 ui_test_utils::NavigateToURL(browser(), url
);
360 // Show the Find bar.
361 browser()->GetFindBarController()->Show();
363 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
364 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
367 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
368 browser(), ui::VKEY_A
, false, false, false, false));
370 // We should find "a" here.
371 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
373 // Reload the page to clear the matching result.
374 chrome::Reload(browser(), CURRENT_TAB
);
376 // Focus the Find bar again to make sure the text is selected.
377 browser()->GetFindBarController()->Show();
379 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
380 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD
));
382 // "a" should be selected.
383 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
385 // Press Ctrl-C to copy the content.
386 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
387 browser(), ui::VKEY_C
, true, false, false, false));
390 ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE
,
393 // Make sure the text is copied successfully.
394 EXPECT_EQ(ASCIIToUTF16("a"), str
);
396 // Press Ctrl-V to paste the content back, it should start finding even if the
397 // content is not changed.
398 content::Source
<WebContents
> notification_source(
399 browser()->tab_strip_model()->GetActiveWebContents());
400 ui_test_utils::WindowedNotificationObserverWithDetails
401 <FindNotificationDetails
> observer(
402 chrome::NOTIFICATION_FIND_RESULT_AVAILABLE
, notification_source
);
404 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
405 browser(), ui::VKEY_V
, true, false, false, false));
407 ASSERT_NO_FATAL_FAILURE(observer
.Wait());
408 FindNotificationDetails details
;
409 ASSERT_TRUE(observer
.GetDetailsFor(notification_source
.map_key(), &details
));
410 EXPECT_TRUE(details
.number_of_matches() > 0);
414 // TODO(phajdan.jr): Disabling due to possible timing issues on XP
415 // interactive_ui_tests.
416 // http://crbug.com/311363
417 IN_PROC_BROWSER_TEST_F(FindInPageTest
, DISABLED_CtrlEnter
) {
418 ui_test_utils::NavigateToURL(browser(),
419 GURL("data:text/html,This is some text with a "
420 "<a href=\"about:blank\">link</a>."));
422 browser()->GetFindBarController()->Show();
424 // Search for "link".
425 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
426 browser(), ui::VKEY_L
, false, false, false, false));
427 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
428 browser(), ui::VKEY_I
, false, false, false, false));
429 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
430 browser(), ui::VKEY_N
, false, false, false, false));
431 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
432 browser(), ui::VKEY_K
, false, false, false, false));
433 EXPECT_EQ(ASCIIToUTF16("link"), GetFindBarText());
435 ui_test_utils::UrlLoadObserver
observer(
436 GURL("about:blank"), content::NotificationService::AllSources());
438 // Send Ctrl-Enter, should cause navigation to about:blank.
439 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
440 browser(), ui::VKEY_RETURN
, true, false, false, false));