find_bar_host_interactive_uitest.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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/process_util.h"
6#include "base/strings/string_util.h"
7#include "base/strings/utf_string_conversions.h"
8#include "chrome/browser/chrome_notification_types.h"
9#include "chrome/browser/ui/browser.h"
10#include "chrome/browser/ui/browser_commands.h"
11#include "chrome/browser/ui/browser_tabstrip.h"
12#include "chrome/browser/ui/find_bar/find_bar_controller.h"
13#include "chrome/browser/ui/find_bar/find_notification_details.h"
14#include "chrome/browser/ui/tabs/tab_strip_model.h"
15#include "chrome/browser/ui/view_ids.h"
16#include "chrome/browser/ui/views/find_bar_host.h"
17#include "chrome/browser/ui/views/frame/browser_view.h"
18#include "chrome/test/base/in_process_browser_test.h"
19#include "chrome/test/base/interactive_test_utils.h"
20#include "chrome/test/base/ui_test_utils.h"
21#include "content/public/browser/notification_service.h"
22#include "content/public/browser/web_contents.h"
23#include "net/test/spawned_test_server/spawned_test_server.h"
24#include "ui/base/clipboard/clipboard.h"
25#include "ui/base/keycodes/keyboard_codes.h"
26#include "ui/views/focus/focus_manager.h"
27#include "ui/views/view.h"
28#include "ui/views/views_delegate.h"
29
30using content::WebContents;
31
32namespace {
33
34static const char kSimplePage[] = "files/find_in_page/simple.html";
35
36class FindInPageTest : public InProcessBrowserTest {
37 public:
38  FindInPageTest() {
39    FindBarHost::disable_animations_during_testing_ = true;
40  }
41
42  string16 GetFindBarText() {
43    FindBarTesting* find_bar =
44        browser()->GetFindBarController()->find_bar()->GetFindBarTesting();
45    return find_bar->GetFindText();
46  }
47
48  string16 GetFindBarSelectedText() {
49    FindBarTesting* find_bar =
50        browser()->GetFindBarController()->find_bar()->GetFindBarTesting();
51    return find_bar->GetFindSelectedText();
52  }
53
54 private:
55  DISALLOW_COPY_AND_ASSIGN(FindInPageTest);
56};
57
58}  // namespace
59
60// Flaky because the test server fails to start? See: http://crbug.com/96594.
61IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) {
62  ASSERT_TRUE(test_server()->Start());
63
64  // First we navigate to our test page (tab A).
65  GURL url = test_server()->GetURL(kSimplePage);
66  ui_test_utils::NavigateToURL(browser(), url);
67
68  chrome::Find(browser());
69
70  // Open another tab (tab B).
71  chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED);
72
73  chrome::Find(browser());
74  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
75                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
76
77  // Select tab A.
78  browser()->tab_strip_model()->ActivateTabAt(0, true);
79
80  // Close tab B.
81  browser()->tab_strip_model()->CloseWebContentsAt(1,
82                                                   TabStripModel::CLOSE_NONE);
83
84  // Click on the location bar so that Find box loses focus.
85  ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(),
86                                                     VIEW_ID_OMNIBOX));
87  // Check the location bar is focused.
88  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
89
90  // This used to crash until bug 1303709 was fixed.
91  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
92      browser(), ui::VKEY_ESCAPE, false, false, false, false));
93}
94
95// Flaky because the test server fails to start? See: http://crbug.com/96594.
96IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestore) {
97  ASSERT_TRUE(test_server()->Start());
98
99  GURL url = test_server()->GetURL("title1.html");
100  ui_test_utils::NavigateToURL(browser(), url);
101
102  // Focus the location bar, open and close the find-in-page, focus should
103  // return to the location bar.
104  chrome::FocusLocationBar(browser());
105  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
106  // Ensure the creation of the find bar controller.
107  browser()->GetFindBarController()->Show();
108  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
109                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
110  browser()->GetFindBarController()->EndFindSession(
111      FindBarController::kKeepSelectionOnPage,
112      FindBarController::kKeepResultsInFindBox);
113  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
114
115  // Focus the location bar, find something on the page, close the find box,
116  // focus should go to the page.
117  chrome::FocusLocationBar(browser());
118  chrome::Find(browser());
119  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
120                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
121  ui_test_utils::FindInPage(
122      browser()->tab_strip_model()->GetActiveWebContents(),
123      ASCIIToUTF16("a"), true, false, NULL, NULL);
124  browser()->GetFindBarController()->EndFindSession(
125      FindBarController::kKeepSelectionOnPage,
126      FindBarController::kKeepResultsInFindBox);
127  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
128
129  // Focus the location bar, open and close the find box, focus should return to
130  // the location bar (same as before, just checking that http://crbug.com/23599
131  // is fixed).
132  chrome::FocusLocationBar(browser());
133  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
134  browser()->GetFindBarController()->Show();
135  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
136                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
137  browser()->GetFindBarController()->EndFindSession(
138      FindBarController::kKeepSelectionOnPage,
139      FindBarController::kKeepResultsInFindBox);
140  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
141}
142
143// Flaky because the test server fails to start? See: http://crbug.com/96594.
144IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestoreOnTabSwitch) {
145  ASSERT_TRUE(test_server()->Start());
146
147  // First we navigate to our test page (tab A).
148  GURL url = test_server()->GetURL(kSimplePage);
149  ui_test_utils::NavigateToURL(browser(), url);
150
151  chrome::Find(browser());
152  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
153                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
154
155  FindBarTesting* find_bar =
156      browser()->GetFindBarController()->find_bar()->GetFindBarTesting();
157
158  // Search for 'a'.
159  ui_test_utils::FindInPage(
160      browser()->tab_strip_model()->GetActiveWebContents(),
161      ASCIIToUTF16("a"), true, false, NULL, NULL);
162  EXPECT_TRUE(ASCIIToUTF16("a") == find_bar->GetFindSelectedText());
163
164  // Open another tab (tab B).
165  content::WindowedNotificationObserver observer(
166      content::NOTIFICATION_LOAD_STOP,
167      content::NotificationService::AllSources());
168  chrome::AddSelectedTabWithURL(browser(), url, content::PAGE_TRANSITION_TYPED);
169  observer.Wait();
170
171  // Make sure Find box is open.
172  chrome::Find(browser());
173  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
174                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
175
176  // Search for 'b'.
177  ui_test_utils::FindInPage(
178      browser()->tab_strip_model()->GetActiveWebContents(),
179      ASCIIToUTF16("b"), true, false, NULL, NULL);
180  EXPECT_TRUE(ASCIIToUTF16("b") == find_bar->GetFindSelectedText());
181
182  // Set focus away from the Find bar (to the Location bar).
183  chrome::FocusLocationBar(browser());
184  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
185
186  // Select tab A. Find bar should get focus.
187  browser()->tab_strip_model()->ActivateTabAt(0, true);
188  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
189                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
190  EXPECT_TRUE(ASCIIToUTF16("a") == find_bar->GetFindSelectedText());
191
192  // Select tab B. Location bar should get focus.
193  browser()->tab_strip_model()->ActivateTabAt(1, true);
194  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
195}
196
197// Flaky because the test server fails to start? See: http://crbug.com/96594.
198// This tests that whenever you clear values from the Find box and close it that
199// it respects that and doesn't show you the last search, as reported in bug:
200// http://crbug.com/40121.
201IN_PROC_BROWSER_TEST_F(FindInPageTest, PrepopulateRespectBlank) {
202#if defined(OS_MACOSX)
203  // FindInPage on Mac doesn't use prepopulated values. Search there is global.
204  return;
205#endif
206  ASSERT_TRUE(test_server()->Start());
207
208  // Make sure Chrome is in the foreground, otherwise sending input
209  // won't do anything and the test will hang.
210  ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
211
212  // First we navigate to any page.
213  GURL url = test_server()->GetURL(kSimplePage);
214  ui_test_utils::NavigateToURL(browser(), url);
215
216  // Show the Find bar.
217  browser()->GetFindBarController()->Show();
218
219  // Search for "a".
220  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
221      browser(), ui::VKEY_A, false, false, false, false));
222
223  // We should find "a" here.
224  EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
225
226  // Delete "a".
227  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
228      browser(), ui::VKEY_BACK, false, false, false, false));
229
230  // Validate we have cleared the text.
231  EXPECT_EQ(string16(), GetFindBarText());
232
233  // Close the Find box.
234  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
235      browser(), ui::VKEY_ESCAPE, false, false, false, false));
236
237  // Show the Find bar.
238  browser()->GetFindBarController()->Show();
239
240  // After the Find box has been reopened, it should not have been prepopulated
241  // with "a" again.
242  EXPECT_EQ(string16(), GetFindBarText());
243
244  // Close the Find box.
245  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
246      browser(), ui::VKEY_ESCAPE, false, false, false, false));
247
248  // Press F3 to trigger FindNext.
249  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
250      browser(), ui::VKEY_F3, false, false, false, false));
251
252  // After the Find box has been reopened, it should still have no prepopulate
253  // value.
254  EXPECT_EQ(string16(), GetFindBarText());
255}
256
257// Flaky on Win. http://crbug.com/92467
258// Flaky on ChromeOS. http://crbug.com/118216
259#if defined(OS_WIN) || defined(OS_CHROMEOS)
260#define MAYBE_PasteWithoutTextChange DISABLED_PasteWithoutTextChange
261#else
262#define MAYBE_PasteWithoutTextChange PasteWithoutTextChange
263#endif
264
265IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_PasteWithoutTextChange) {
266  ASSERT_TRUE(test_server()->Start());
267
268  // Make sure Chrome is in the foreground, otherwise sending input
269  // won't do anything and the test will hang.
270  ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
271
272  // First we navigate to any page.
273  GURL url = test_server()->GetURL(kSimplePage);
274  ui_test_utils::NavigateToURL(browser(), url);
275
276  // Show the Find bar.
277  browser()->GetFindBarController()->Show();
278
279  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
280                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
281
282  // Search for "a".
283  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
284      browser(), ui::VKEY_A, false, false, false, false));
285
286  // We should find "a" here.
287  EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
288
289  // Reload the page to clear the matching result.
290  chrome::Reload(browser(), CURRENT_TAB);
291
292  // Focus the Find bar again to make sure the text is selected.
293  browser()->GetFindBarController()->Show();
294
295  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
296                                           VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
297
298  // "a" should be selected.
299  EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
300
301  // Press Ctrl-C to copy the content.
302  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
303      browser(), ui::VKEY_C, true, false, false, false));
304
305  string16 str;
306  ui::Clipboard::GetForCurrentThread()->
307      ReadText(ui::Clipboard::BUFFER_STANDARD, &str);
308
309  // Make sure the text is copied successfully.
310  EXPECT_EQ(ASCIIToUTF16("a"), str);
311
312  // Press Ctrl-V to paste the content back, it should start finding even if the
313  // content is not changed.
314  content::Source<WebContents> notification_source(
315      browser()->tab_strip_model()->GetActiveWebContents());
316  ui_test_utils::WindowedNotificationObserverWithDetails
317      <FindNotificationDetails> observer(
318          chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, notification_source);
319
320  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
321      browser(), ui::VKEY_V, true, false, false, false));
322
323  ASSERT_NO_FATAL_FAILURE(observer.Wait());
324  FindNotificationDetails details;
325  ASSERT_TRUE(observer.GetDetailsFor(notification_source.map_key(), &details));
326  EXPECT_TRUE(details.number_of_matches() > 0);
327}
328