find_bar_host_interactive_uitest.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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.
4
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"
28
29using content::WebContents;
30
31namespace {
32
33static const char kSimplePage[] = "files/find_in_page/simple.html";
34
35class FindInPageTest : public InProcessBrowserTest {
36 public:
37  FindInPageTest() {
38    FindBarHost::disable_animations_during_testing_ = true;
39  }
40
41  string16 GetFindBarText() {
42    FindBar* find_bar = browser()->GetFindBarController()->find_bar();
43    return find_bar->GetFindText();
44  }
45
46  string16 GetFindBarSelectedText() {
47    FindBarTesting* find_bar =
48        browser()->GetFindBarController()->find_bar()->GetFindBarTesting();
49    return find_bar->GetFindSelectedText();
50  }
51
52 private:
53  DISALLOW_COPY_AND_ASSIGN(FindInPageTest);
54};
55
56}  // namespace
57
58// Flaky because the test server fails to start? See: http://crbug.com/96594.
59IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) {
60  ASSERT_TRUE(test_server()->Start());
61
62  // First we navigate to our test page (tab A).
63  GURL url = test_server()->GetURL(kSimplePage);
64  ui_test_utils::NavigateToURL(browser(), url);
65
66  chrome::Find(browser());
67
68  // Open another tab (tab B).
69  chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED);
70
71  chrome::Find(browser());
72  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
73                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
74
75  // Select tab A.
76  browser()->tab_strip_model()->ActivateTabAt(0, true);
77
78  // Close tab B.
79  browser()->tab_strip_model()->CloseWebContentsAt(1,
80                                                   TabStripModel::CLOSE_NONE);
81
82  // Click on the location bar so that Find box loses focus.
83  ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(),
84                                                     VIEW_ID_OMNIBOX));
85  // Check the location bar is focused.
86  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
87
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));
91}
92
93// Flaky because the test server fails to start? See: http://crbug.com/96594.
94IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestore) {
95  ASSERT_TRUE(test_server()->Start());
96
97  GURL url = test_server()->GetURL("title1.html");
98  ui_test_utils::NavigateToURL(browser(), url);
99
100  // Focus the location bar, open and close the find-in-page, focus should
101  // return to the location bar.
102  chrome::FocusLocationBar(browser());
103  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
104  // Ensure the creation of the find bar controller.
105  browser()->GetFindBarController()->Show();
106  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
107                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
108  browser()->GetFindBarController()->EndFindSession(
109      FindBarController::kKeepSelectionOnPage,
110      FindBarController::kKeepResultsInFindBox);
111  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
112
113  // Focus the location bar, find something on the page, close the find box,
114  // focus should go to the page.
115  chrome::FocusLocationBar(browser());
116  chrome::Find(browser());
117  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
118                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
119  ui_test_utils::FindInPage(
120      browser()->tab_strip_model()->GetActiveWebContents(),
121      ASCIIToUTF16("a"), true, false, NULL, NULL);
122  browser()->GetFindBarController()->EndFindSession(
123      FindBarController::kKeepSelectionOnPage,
124      FindBarController::kKeepResultsInFindBox);
125  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
126
127  // Focus the location bar, open and close the find box, focus should return to
128  // the location bar (same as before, just checking that http://crbug.com/23599
129  // is fixed).
130  chrome::FocusLocationBar(browser());
131  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
132  browser()->GetFindBarController()->Show();
133  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
134                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
135  browser()->GetFindBarController()->EndFindSession(
136      FindBarController::kKeepSelectionOnPage,
137      FindBarController::kKeepResultsInFindBox);
138  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
139}
140
141IN_PROC_BROWSER_TEST_F(FindInPageTest, SelectionRestoreOnTabSwitch) {
142  ASSERT_TRUE(test_server()->Start());
143
144  // Make sure Chrome is in the foreground, otherwise sending input
145  // won't do anything and the test will hang.
146  ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
147
148  // First we navigate to any page in the current tab (tab A).
149  GURL url = test_server()->GetURL(kSimplePage);
150  ui_test_utils::NavigateToURL(browser(), url);
151
152  // Show the Find bar.
153  browser()->GetFindBarController()->Show();
154
155  // Search for "abc".
156  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
157      browser(), ui::VKEY_A, false, false, false, false));
158  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
159      browser(), ui::VKEY_B, false, false, false, false));
160  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
161      browser(), ui::VKEY_C, false, false, false, false));
162  EXPECT_EQ(ASCIIToUTF16("abc"), GetFindBarText());
163
164  // Select "bc".
165  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
166      browser(), ui::VKEY_LEFT, false, true, false, false));
167  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
168      browser(), ui::VKEY_LEFT, false, true, false, false));
169  EXPECT_EQ(ASCIIToUTF16("bc"), GetFindBarSelectedText());
170
171  // Open another tab (tab B).
172  content::WindowedNotificationObserver observer(
173      content::NOTIFICATION_LOAD_STOP,
174      content::NotificationService::AllSources());
175  chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED);
176  observer.Wait();
177
178  // Show the Find bar.
179  browser()->GetFindBarController()->Show();
180
181  // Search for "def".
182  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
183      browser(), ui::VKEY_D, false, false, false, false));
184  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
185      browser(), ui::VKEY_E, false, false, false, false));
186  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
187      browser(), ui::VKEY_F, false, false, false, false));
188  EXPECT_EQ(ASCIIToUTF16("def"), GetFindBarText());
189
190  // Select "de".
191  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
192      browser(), ui::VKEY_HOME, false, false, false, false));
193  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
194      browser(), ui::VKEY_RIGHT, false, true, false, false));
195  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
196      browser(), ui::VKEY_RIGHT, false, true, false, false));
197  EXPECT_EQ(ASCIIToUTF16("de"), GetFindBarSelectedText());
198
199  // Select tab A. Find bar should select "bc".
200  browser()->tab_strip_model()->ActivateTabAt(0, true);
201  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
202                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
203  EXPECT_EQ(ASCIIToUTF16("bc"), GetFindBarSelectedText());
204
205  // Select tab B. Find bar should select "de".
206  browser()->tab_strip_model()->ActivateTabAt(1, true);
207  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
208                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
209  EXPECT_EQ(ASCIIToUTF16("de"), GetFindBarSelectedText());
210}
211
212// Flaky because the test server fails to start? See: http://crbug.com/96594.
213IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestoreOnTabSwitch) {
214  ASSERT_TRUE(test_server()->Start());
215
216  // First we navigate to our test page (tab A).
217  GURL url = test_server()->GetURL(kSimplePage);
218  ui_test_utils::NavigateToURL(browser(), url);
219
220  chrome::Find(browser());
221  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
222                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
223
224  // Search for 'a'.
225  ui_test_utils::FindInPage(
226      browser()->tab_strip_model()->GetActiveWebContents(),
227      ASCIIToUTF16("a"), true, false, NULL, NULL);
228  EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
229
230  // Open another tab (tab B).
231  content::WindowedNotificationObserver observer(
232      content::NOTIFICATION_LOAD_STOP,
233      content::NotificationService::AllSources());
234  chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED);
235  observer.Wait();
236
237  // Make sure Find box is open.
238  chrome::Find(browser());
239  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
240                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
241
242  // Search for 'b'.
243  ui_test_utils::FindInPage(
244      browser()->tab_strip_model()->GetActiveWebContents(),
245      ASCIIToUTF16("b"), true, false, NULL, NULL);
246  EXPECT_EQ(ASCIIToUTF16("b"), GetFindBarSelectedText());
247
248  // Set focus away from the Find bar (to the Location bar).
249  chrome::FocusLocationBar(browser());
250  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
251
252  // Select tab A. Find bar should get focus.
253  browser()->tab_strip_model()->ActivateTabAt(0, true);
254  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
255                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
256  EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
257
258  // Select tab B. Location bar should get focus.
259  browser()->tab_strip_model()->ActivateTabAt(1, true);
260  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
261}
262
263// Flaky because the test server fails to start? See: http://crbug.com/96594.
264// This tests that whenever you clear values from the Find box and close it that
265// it respects that and doesn't show you the last search, as reported in bug:
266// http://crbug.com/40121. For Aura see bug http://crbug.com/292299.
267IN_PROC_BROWSER_TEST_F(FindInPageTest, PrepopulateRespectBlank) {
268#if defined(OS_MACOSX) || defined(USE_AURA)
269  // FindInPage on Mac doesn't use prepopulated values. Search there is global.
270  return;
271#endif
272  ASSERT_TRUE(test_server()->Start());
273
274  // Make sure Chrome is in the foreground, otherwise sending input
275  // won't do anything and the test will hang.
276  ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
277
278  // First we navigate to any page.
279  GURL url = test_server()->GetURL(kSimplePage);
280  ui_test_utils::NavigateToURL(browser(), url);
281
282  // Show the Find bar.
283  browser()->GetFindBarController()->Show();
284
285  // Search for "a".
286  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
287      browser(), ui::VKEY_A, false, false, false, false));
288
289  // We should find "a" here.
290  EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
291
292  // Delete "a".
293  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
294      browser(), ui::VKEY_BACK, false, false, false, false));
295
296  // Validate we have cleared the text.
297  EXPECT_EQ(string16(), GetFindBarText());
298
299  // Close the Find box.
300  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
301      browser(), ui::VKEY_ESCAPE, false, false, false, false));
302
303  // Show the Find bar.
304  browser()->GetFindBarController()->Show();
305
306  // After the Find box has been reopened, it should not have been prepopulated
307  // with "a" again.
308  EXPECT_EQ(string16(), GetFindBarText());
309
310  // Close the Find box.
311  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
312      browser(), ui::VKEY_ESCAPE, false, false, false, false));
313
314  // Press F3 to trigger FindNext.
315  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
316      browser(), ui::VKEY_F3, false, false, false, false));
317
318  // After the Find box has been reopened, it should still have no prepopulate
319  // value.
320  EXPECT_EQ(string16(), GetFindBarText());
321}
322
323// Flaky on Win. http://crbug.com/92467
324// Flaky on ChromeOS. http://crbug.com/118216
325#if defined(OS_WIN) || defined(OS_CHROMEOS)
326#define MAYBE_PasteWithoutTextChange DISABLED_PasteWithoutTextChange
327#else
328#define MAYBE_PasteWithoutTextChange PasteWithoutTextChange
329#endif
330
331IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_PasteWithoutTextChange) {
332  ASSERT_TRUE(test_server()->Start());
333
334  // Make sure Chrome is in the foreground, otherwise sending input
335  // won't do anything and the test will hang.
336  ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
337
338  // First we navigate to any page.
339  GURL url = test_server()->GetURL(kSimplePage);
340  ui_test_utils::NavigateToURL(browser(), url);
341
342  // Show the Find bar.
343  browser()->GetFindBarController()->Show();
344
345  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
346                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
347
348  // Search for "a".
349  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
350      browser(), ui::VKEY_A, false, false, false, false));
351
352  // We should find "a" here.
353  EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
354
355  // Reload the page to clear the matching result.
356  chrome::Reload(browser(), CURRENT_TAB);
357
358  // Focus the Find bar again to make sure the text is selected.
359  browser()->GetFindBarController()->Show();
360
361  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
362                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
363
364  // "a" should be selected.
365  EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
366
367  // Press Ctrl-C to copy the content.
368  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
369      browser(), ui::VKEY_C, true, false, false, false));
370
371  string16 str;
372  ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE,
373                                                 &str);
374
375  // Make sure the text is copied successfully.
376  EXPECT_EQ(ASCIIToUTF16("a"), str);
377
378  // Press Ctrl-V to paste the content back, it should start finding even if the
379  // content is not changed.
380  content::Source<WebContents> notification_source(
381      browser()->tab_strip_model()->GetActiveWebContents());
382  ui_test_utils::WindowedNotificationObserverWithDetails
383      <FindNotificationDetails> observer(
384          chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, notification_source);
385
386  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
387      browser(), ui::VKEY_V, true, false, false, false));
388
389  ASSERT_NO_FATAL_FAILURE(observer.Wait());
390  FindNotificationDetails details;
391  ASSERT_TRUE(observer.GetDetailsFor(notification_source.map_key(), &details));
392  EXPECT_TRUE(details.number_of_matches() > 0);
393}
394
395#if defined(OS_WIN)
396IN_PROC_BROWSER_TEST_F(FindInPageTest, CtrlEnter) {
397  ui_test_utils::NavigateToURL(browser(),
398                               GURL("data:text/html,This is some text with a "
399                                    "<a href=\"about:blank\">link</a>."));
400
401  browser()->GetFindBarController()->Show();
402
403  // Search for "link".
404  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
405      browser(), ui::VKEY_L, false, false, false, false));
406  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
407      browser(), ui::VKEY_I, false, false, false, false));
408  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
409      browser(), ui::VKEY_N, false, false, false, false));
410  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
411      browser(), ui::VKEY_K, false, false, false, false));
412  EXPECT_EQ(ASCIIToUTF16("link"), GetFindBarText());
413
414  ui_test_utils::UrlLoadObserver observer(
415      GURL("about:blank"), content::NotificationService::AllSources());
416
417  // Send Ctrl-Enter, should cause navigation to about:blank.
418  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
419      browser(), ui::VKEY_RETURN, true, false, false, false));
420
421  observer.Wait();
422}
423#endif
424