• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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/string_util.h"
7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/tabs/tab_strip_model.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
11 #include "chrome/browser/ui/view_ids.h"
12 #include "chrome/browser/ui/views/find_bar_host.h"
13 #include "chrome/browser/ui/views/frame/browser_view.h"
14 #include "chrome/test/in_process_browser_test.h"
15 #include "chrome/test/ui_test_utils.h"
16 #include "content/browser/tab_contents/tab_contents.h"
17 #include "net/test/test_server.h"
18 #include "ui/base/keycodes/keyboard_codes.h"
19 #include "views/focus/focus_manager.h"
20 #include "views/view.h"
21 
22 namespace {
23 
24 // The delay waited after sending an OS simulated event.
25 static const int kActionDelayMs = 500;
26 static const char kSimplePage[] = "files/find_in_page/simple.html";
27 
Checkpoint(const char * message,const base::TimeTicks & start_time)28 void Checkpoint(const char* message, const base::TimeTicks& start_time) {
29   LOG(INFO) << message << " : "
30     << (base::TimeTicks::Now() - start_time).InMilliseconds()
31     << " ms" << std::flush;
32 }
33 
34 class FindInPageTest : public InProcessBrowserTest {
35  public:
FindInPageTest()36   FindInPageTest() {
37     set_show_window(true);
38     FindBarHost::disable_animations_during_testing_ = true;
39   }
40 
GetFindBarText()41   string16 GetFindBarText() {
42     FindBarTesting* find_bar =
43         browser()->GetFindBarController()->find_bar()->GetFindBarTesting();
44     return find_bar->GetFindText();
45   }
46 };
47 
48 }  // namespace
49 
IN_PROC_BROWSER_TEST_F(FindInPageTest,CrashEscHandlers)50 IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) {
51   ASSERT_TRUE(test_server()->Start());
52 
53   // First we navigate to our test page (tab A).
54   GURL url = test_server()->GetURL(kSimplePage);
55   ui_test_utils::NavigateToURL(browser(), url);
56 
57   browser()->Find();
58 
59   // Open another tab (tab B).
60   browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
61 
62   browser()->Find();
63   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
64                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
65 
66   // Select tab A.
67   browser()->ActivateTabAt(0, true);
68 
69   // Close tab B.
70   browser()->CloseTabContents(browser()->GetTabContentsAt(1));
71 
72   // Click on the location bar so that Find box loses focus.
73   ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(),
74                                                      VIEW_ID_LOCATION_BAR));
75 #if defined(TOOLKIT_VIEWS) || defined(OS_WIN)
76   // Check the location bar is focused.
77   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR));
78 #endif
79 
80   // This used to crash until bug 1303709 was fixed.
81   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
82       browser(), ui::VKEY_ESCAPE, false, false, false, false));
83 }
84 
IN_PROC_BROWSER_TEST_F(FindInPageTest,FocusRestore)85 IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestore) {
86   ASSERT_TRUE(test_server()->Start());
87 
88   GURL url = test_server()->GetURL("title1.html");
89   ui_test_utils::NavigateToURL(browser(), url);
90 
91   // Focus the location bar, open and close the find-in-page, focus should
92   // return to the location bar.
93   browser()->FocusLocationBar();
94   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR));
95   // Ensure the creation of the find bar controller.
96   browser()->GetFindBarController()->Show();
97   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
98                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
99   browser()->GetFindBarController()->EndFindSession(
100       FindBarController::kKeepSelection);
101   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR));
102 
103   // Focus the location bar, find something on the page, close the find box,
104   // focus should go to the page.
105   browser()->FocusLocationBar();
106   browser()->Find();
107   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
108                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
109   ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
110                             ASCIIToUTF16("a"), true, false, NULL);
111   browser()->GetFindBarController()->EndFindSession(
112       FindBarController::kKeepSelection);
113   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
114                                            VIEW_ID_TAB_CONTAINER_FOCUS_VIEW));
115 
116   // Focus the location bar, open and close the find box, focus should return to
117   // the location bar (same as before, just checking that http://crbug.com/23599
118   // is fixed).
119   browser()->FocusLocationBar();
120   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR));
121   browser()->GetFindBarController()->Show();
122   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
123                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
124   browser()->GetFindBarController()->EndFindSession(
125       FindBarController::kKeepSelection);
126   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR));
127 }
128 
IN_PROC_BROWSER_TEST_F(FindInPageTest,FocusRestoreOnTabSwitch)129 IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestoreOnTabSwitch) {
130   ASSERT_TRUE(test_server()->Start());
131 
132   // First we navigate to our test page (tab A).
133   GURL url = test_server()->GetURL(kSimplePage);
134   ui_test_utils::NavigateToURL(browser(), url);
135 
136   browser()->Find();
137   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
138                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
139 
140   FindBarTesting* find_bar =
141       browser()->GetFindBarController()->find_bar()->GetFindBarTesting();
142 
143   // Search for 'a'.
144   ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
145                             ASCIIToUTF16("a"), true, false, NULL);
146   EXPECT_TRUE(ASCIIToUTF16("a") == find_bar->GetFindSelectedText());
147 
148   // Open another tab (tab B).
149   browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
150   ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
151 
152   // Make sure Find box is open.
153   browser()->Find();
154   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
155                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
156 
157   // Search for 'b'.
158   ui_test_utils::FindInPage(browser()->GetSelectedTabContentsWrapper(),
159                             ASCIIToUTF16("b"), true, false, NULL);
160   EXPECT_TRUE(ASCIIToUTF16("b") == find_bar->GetFindSelectedText());
161 
162   // Set focus away from the Find bar (to the Location bar).
163   browser()->FocusLocationBar();
164   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR));
165 
166   // Select tab A. Find bar should get focus.
167   browser()->ActivateTabAt(0, true);
168   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
169                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
170   EXPECT_TRUE(ASCIIToUTF16("a") == find_bar->GetFindSelectedText());
171 
172   // Select tab B. Location bar should get focus.
173   browser()->ActivateTabAt(1, true);
174   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR));
175 }
176 
177 // This tests that whenever you clear values from the Find box and close it that
178 // it respects that and doesn't show you the last search, as reported in bug:
179 // http://crbug.com/40121.
IN_PROC_BROWSER_TEST_F(FindInPageTest,PrepopulateRespectBlank)180 IN_PROC_BROWSER_TEST_F(FindInPageTest, PrepopulateRespectBlank) {
181 #if defined(OS_MACOSX)
182   // FindInPage on Mac doesn't use prepopulated values. Search there is global.
183   return;
184 #endif
185   base::TimeTicks start_time = base::TimeTicks::Now();
186   Checkpoint("Test starting", start_time);
187 
188   ASSERT_TRUE(test_server()->Start());
189 
190   // Make sure Chrome is in the foreground, otherwise sending input
191   // won't do anything and the test will hang.
192   ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
193 
194   Checkpoint("Navigate", start_time);
195 
196   // First we navigate to any page.
197   GURL url = test_server()->GetURL(kSimplePage);
198   ui_test_utils::NavigateToURL(browser(), url);
199 
200   Checkpoint("Show Find bar", start_time);
201 
202   // Show the Find bar.
203   browser()->GetFindBarController()->Show();
204 
205   Checkpoint("Search for 'a'", start_time);
206 
207   // Search for "a".
208   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
209       browser(), ui::VKEY_A, false, false, false, false));
210 
211   // We should find "a" here.
212   EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
213 
214   Checkpoint("Delete 'a'", start_time);
215 
216   // Delete "a".
217   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
218       browser(), ui::VKEY_BACK, false, false, false, false));
219 
220   // Validate we have cleared the text.
221   EXPECT_EQ(string16(), GetFindBarText());
222 
223   Checkpoint("Close find bar", start_time);
224 
225   // Close the Find box.
226   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
227       browser(), ui::VKEY_ESCAPE, false, false, false, false));
228 
229   Checkpoint("Show Find bar", start_time);
230 
231   // Show the Find bar.
232   browser()->GetFindBarController()->Show();
233 
234   Checkpoint("Validate text", start_time);
235 
236   // After the Find box has been reopened, it should not have been prepopulated
237   // with "a" again.
238   EXPECT_EQ(string16(), GetFindBarText());
239 
240   Checkpoint("Close Find bar", start_time);
241 
242   // Close the Find box.
243   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
244       browser(), ui::VKEY_ESCAPE, false, false, false, false));
245 
246   Checkpoint("FindNext", start_time);
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   Checkpoint("Validate", start_time);
253 
254   // After the Find box has been reopened, it should still have no prepopulate
255   // value.
256   EXPECT_EQ(string16(), GetFindBarText());
257 
258   Checkpoint("Test done", start_time);
259 }
260