find_bar_host_interactive_uitest.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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
141// Flaky because the test server fails to start? See: http://crbug.com/96594.
142IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestoreOnTabSwitch) {
143  ASSERT_TRUE(test_server()->Start());
144
145  // First we navigate to our test page (tab A).
146  GURL url = test_server()->GetURL(kSimplePage);
147  ui_test_utils::NavigateToURL(browser(), url);
148
149  chrome::Find(browser());
150  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
151                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
152
153  FindBarTesting* find_bar =
154      browser()->GetFindBarController()->find_bar()->GetFindBarTesting();
155
156  // Search for 'a'.
157  ui_test_utils::FindInPage(
158      browser()->tab_strip_model()->GetActiveWebContents(),
159      ASCIIToUTF16("a"), true, false, NULL, NULL);
160  EXPECT_TRUE(ASCIIToUTF16("a") == find_bar->GetFindSelectedText());
161
162  // Open another tab (tab B).
163  content::WindowedNotificationObserver observer(
164      content::NOTIFICATION_LOAD_STOP,
165      content::NotificationService::AllSources());
166  chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED);
167  observer.Wait();
168
169  // Make sure Find box is open.
170  chrome::Find(browser());
171  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
172                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
173
174  // Search for 'b'.
175  ui_test_utils::FindInPage(
176      browser()->tab_strip_model()->GetActiveWebContents(),
177      ASCIIToUTF16("b"), true, false, NULL, NULL);
178  EXPECT_TRUE(ASCIIToUTF16("b") == find_bar->GetFindSelectedText());
179
180  // Set focus away from the Find bar (to the Location bar).
181  chrome::FocusLocationBar(browser());
182  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
183
184  // Select tab A. Find bar should get focus.
185  browser()->tab_strip_model()->ActivateTabAt(0, true);
186  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
187                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
188  EXPECT_TRUE(ASCIIToUTF16("a") == find_bar->GetFindSelectedText());
189
190  // Select tab B. Location bar should get focus.
191  browser()->tab_strip_model()->ActivateTabAt(1, true);
192  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
193}
194
195// Flaky because the test server fails to start? See: http://crbug.com/96594.
196// This tests that whenever you clear values from the Find box and close it that
197// it respects that and doesn't show you the last search, as reported in bug:
198// http://crbug.com/40121. For Aura see bug http://crbug.com/292299.
199IN_PROC_BROWSER_TEST_F(FindInPageTest, PrepopulateRespectBlank) {
200#if defined(OS_MACOSX) || defined(USE_AURA)
201  // FindInPage on Mac doesn't use prepopulated values. Search there is global.
202  return;
203#endif
204  ASSERT_TRUE(test_server()->Start());
205
206  // Make sure Chrome is in the foreground, otherwise sending input
207  // won't do anything and the test will hang.
208  ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
209
210  // First we navigate to any page.
211  GURL url = test_server()->GetURL(kSimplePage);
212  ui_test_utils::NavigateToURL(browser(), url);
213
214  // Show the Find bar.
215  browser()->GetFindBarController()->Show();
216
217  // Search for "a".
218  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
219      browser(), ui::VKEY_A, false, false, false, false));
220
221  // We should find "a" here.
222  EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
223
224  // Delete "a".
225  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
226      browser(), ui::VKEY_BACK, false, false, false, false));
227
228  // Validate we have cleared the text.
229  EXPECT_EQ(string16(), GetFindBarText());
230
231  // Close the Find box.
232  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
233      browser(), ui::VKEY_ESCAPE, false, false, false, false));
234
235  // Show the Find bar.
236  browser()->GetFindBarController()->Show();
237
238  // After the Find box has been reopened, it should not have been prepopulated
239  // with "a" again.
240  EXPECT_EQ(string16(), GetFindBarText());
241
242  // Close the Find box.
243  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
244      browser(), ui::VKEY_ESCAPE, false, false, false, false));
245
246  // Press F3 to trigger FindNext.
247  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
248      browser(), ui::VKEY_F3, false, false, false, false));
249
250  // After the Find box has been reopened, it should still have no prepopulate
251  // value.
252  EXPECT_EQ(string16(), GetFindBarText());
253}
254
255// Flaky on Win. http://crbug.com/92467
256// Flaky on ChromeOS. http://crbug.com/118216
257#if defined(OS_WIN) || defined(OS_CHROMEOS)
258#define MAYBE_PasteWithoutTextChange DISABLED_PasteWithoutTextChange
259#else
260#define MAYBE_PasteWithoutTextChange PasteWithoutTextChange
261#endif
262
263IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_PasteWithoutTextChange) {
264  ASSERT_TRUE(test_server()->Start());
265
266  // Make sure Chrome is in the foreground, otherwise sending input
267  // won't do anything and the test will hang.
268  ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
269
270  // First we navigate to any page.
271  GURL url = test_server()->GetURL(kSimplePage);
272  ui_test_utils::NavigateToURL(browser(), url);
273
274  // Show the Find bar.
275  browser()->GetFindBarController()->Show();
276
277  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
278                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
279
280  // Search for "a".
281  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
282      browser(), ui::VKEY_A, false, false, false, false));
283
284  // We should find "a" here.
285  EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
286
287  // Reload the page to clear the matching result.
288  chrome::Reload(browser(), CURRENT_TAB);
289
290  // Focus the Find bar again to make sure the text is selected.
291  browser()->GetFindBarController()->Show();
292
293  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
294                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
295
296  // "a" should be selected.
297  EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
298
299  // Press Ctrl-C to copy the content.
300  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
301      browser(), ui::VKEY_C, true, false, false, false));
302
303  string16 str;
304  ui::Clipboard::GetForCurrentThread()->
305      ReadText(ui::Clipboard::BUFFER_STANDARD, &str);
306
307  // Make sure the text is copied successfully.
308  EXPECT_EQ(ASCIIToUTF16("a"), str);
309
310  // Press Ctrl-V to paste the content back, it should start finding even if the
311  // content is not changed.
312  content::Source<WebContents> notification_source(
313      browser()->tab_strip_model()->GetActiveWebContents());
314  ui_test_utils::WindowedNotificationObserverWithDetails
315      <FindNotificationDetails> observer(
316          chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, notification_source);
317
318  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
319      browser(), ui::VKEY_V, true, false, false, false));
320
321  ASSERT_NO_FATAL_FAILURE(observer.Wait());
322  FindNotificationDetails details;
323  ASSERT_TRUE(observer.GetDetailsFor(notification_source.map_key(), &details));
324  EXPECT_TRUE(details.number_of_matches() > 0);
325}
326