OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "app/keyboard_codes.h" | 5 #include "app/keyboard_codes.h" |
6 #include "base/process_util.h" | 6 #include "base/process_util.h" |
7 #include "base/string_number_conversions.h" | |
8 #include "base/string_util.h" | 7 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/tab_contents/tab_contents.h" | 9 #include "chrome/browser/tab_contents/tab_contents.h" |
11 #include "chrome/browser/tabs/tab_strip_model.h" | 10 #include "chrome/browser/tabs/tab_strip_model.h" |
12 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/find_bar/find_bar_controller.h" | 12 #include "chrome/browser/ui/find_bar/find_bar_controller.h" |
14 #include "chrome/browser/ui/view_ids.h" | 13 #include "chrome/browser/ui/view_ids.h" |
15 #include "chrome/browser/ui/views/find_bar_host.h" | 14 #include "chrome/browser/ui/views/find_bar_host.h" |
16 #include "chrome/browser/ui/views/frame/browser_view.h" | 15 #include "chrome/browser/ui/views/frame/browser_view.h" |
17 #include "chrome/test/in_process_browser_test.h" | 16 #include "chrome/test/in_process_browser_test.h" |
18 #include "chrome/test/ui_test_utils.h" | 17 #include "chrome/test/ui_test_utils.h" |
19 #include "net/test/test_server.h" | 18 #include "net/test/test_server.h" |
20 #include "views/focus/focus_manager.h" | 19 #include "views/focus/focus_manager.h" |
21 #include "views/view.h" | 20 #include "views/view.h" |
22 | 21 |
23 #if defined(OS_WIN) | |
24 #include <windows.h> | |
25 #include <Psapi.h> | |
26 #endif | |
27 | |
28 namespace { | 22 namespace { |
29 | 23 |
30 // The delay waited after sending an OS simulated event. | 24 // The delay waited after sending an OS simulated event. |
31 static const int kActionDelayMs = 500; | 25 static const int kActionDelayMs = 500; |
32 static const char kSimplePage[] = "files/find_in_page/simple.html"; | 26 static const char kSimplePage[] = "files/find_in_page/simple.html"; |
33 | 27 |
34 class FindInPageTest : public InProcessBrowserTest { | 28 class FindInPageTest : public InProcessBrowserTest { |
35 public: | 29 public: |
36 FindInPageTest() { | 30 FindInPageTest() { |
37 set_show_window(true); | 31 set_show_window(true); |
38 FindBarHost::disable_animations_during_testing_ = true; | 32 FindBarHost::disable_animations_during_testing_ = true; |
39 } | 33 } |
40 | 34 |
41 string16 GetFindBarText() { | 35 string16 GetFindBarText() { |
42 FindBarTesting* find_bar = | 36 FindBarTesting* find_bar = |
43 browser()->GetFindBarController()->find_bar()->GetFindBarTesting(); | 37 browser()->GetFindBarController()->find_bar()->GetFindBarTesting(); |
44 return find_bar->GetFindText(); | 38 return find_bar->GetFindText(); |
45 } | 39 } |
46 }; | 40 }; |
47 | 41 |
48 void Checkpoint(const char* message, const base::TimeTicks& start_time) { | |
49 LOG(INFO) << message << " : " | |
50 << (base::TimeTicks::Now() - start_time).InMilliseconds() | |
51 << " ms" << std::flush; | |
52 } | |
53 | |
54 // Test to make sure Chrome is in the foreground as we start testing. This is | |
55 // required for tests that synthesize input to the Chrome window. | |
56 bool ChromeInForeground() { | |
57 #if defined(OS_WIN) | |
58 HWND window = ::GetForegroundWindow(); | |
59 std::wstring caption; | |
60 std::wstring filename; | |
61 int len = ::GetWindowTextLength(window) + 1; | |
62 ::GetWindowText(window, WriteInto(&caption, len), len); | |
63 bool chrome_window_in_foreground = | |
64 (caption == L"about:blank - Google Chrome") || | |
65 (caption == L"about:blank - Chromium"); | |
66 if (!chrome_window_in_foreground) { | |
67 DWORD process_id; | |
68 int thread_id = ::GetWindowThreadProcessId(window, &process_id); | |
69 | |
70 base::ProcessHandle process; | |
71 if (base::OpenProcessHandleWithAccess(process_id, | |
72 PROCESS_QUERY_LIMITED_INFORMATION, | |
73 &process)) { | |
74 len = MAX_PATH; | |
75 if (!GetProcessImageFileName(process, WriteInto(&filename, len), len)) { | |
76 int error = GetLastError(); | |
77 filename = std::wstring(L"Unable to read filename for process id '" + | |
78 base::IntToString16(process_id) + | |
79 L"' (error ") + | |
80 base::IntToString16(error) + L")"; | |
81 } | |
82 base::CloseProcessHandle(process); | |
83 } | |
84 } | |
85 EXPECT_TRUE(chrome_window_in_foreground) | |
86 << "Chrome must be in the foreground when running interactive tests\n" | |
87 << "Process in foreground: " << filename.c_str() << "\n" | |
88 << "Window: " << window << "\n" | |
89 << "Caption: " << caption.c_str(); | |
90 return chrome_window_in_foreground; | |
91 #else | |
92 // Windows only at the moment. | |
93 return true; | |
94 #endif | |
95 } | |
96 | |
97 } // namespace | 42 } // namespace |
98 | 43 |
99 IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) { | 44 IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) { |
100 ASSERT_TRUE(test_server()->Start()); | 45 ASSERT_TRUE(test_server()->Start()); |
101 | 46 |
102 // First we navigate to our test page (tab A). | 47 // First we navigate to our test page (tab A). |
103 GURL url = test_server()->GetURL(kSimplePage); | 48 GURL url = test_server()->GetURL(kSimplePage); |
104 ui_test_utils::NavigateToURL(browser(), url); | 49 ui_test_utils::NavigateToURL(browser(), url); |
105 | 50 |
106 browser()->Find(); | 51 browser()->Find(); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR)); | 114 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR)); |
170 browser()->GetFindBarController()->Show(); | 115 browser()->GetFindBarController()->Show(); |
171 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), | 116 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), |
172 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 117 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
173 browser()->GetFindBarController()->EndFindSession( | 118 browser()->GetFindBarController()->EndFindSession( |
174 FindBarController::kKeepSelection); | 119 FindBarController::kKeepSelection); |
175 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR)); | 120 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR)); |
176 } | 121 } |
177 | 122 |
178 IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestoreOnTabSwitch) { | 123 IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestoreOnTabSwitch) { |
179 base::TimeTicks start_time = base::TimeTicks::Now(); | |
180 Checkpoint("Starting test server", start_time); | |
181 | |
182 ASSERT_TRUE(test_server()->Start()); | 124 ASSERT_TRUE(test_server()->Start()); |
183 | 125 |
184 // First we navigate to our test page (tab A). | 126 // First we navigate to our test page (tab A). |
185 GURL url = test_server()->GetURL(kSimplePage); | 127 GURL url = test_server()->GetURL(kSimplePage); |
186 ui_test_utils::NavigateToURL(browser(), url); | 128 ui_test_utils::NavigateToURL(browser(), url); |
187 | 129 |
188 Checkpoint("Calling Find", start_time); | |
189 | |
190 browser()->Find(); | 130 browser()->Find(); |
191 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), | 131 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), |
192 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 132 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
193 | 133 |
194 Checkpoint("GetFindBarTesting", start_time); | |
195 | |
196 FindBarTesting* find_bar = | 134 FindBarTesting* find_bar = |
197 browser()->GetFindBarController()->find_bar()->GetFindBarTesting(); | 135 browser()->GetFindBarController()->find_bar()->GetFindBarTesting(); |
198 | 136 |
199 Checkpoint("Search for 'a'", start_time); | |
200 | |
201 // Search for 'a'. | 137 // Search for 'a'. |
202 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), | 138 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), |
203 ASCIIToUTF16("a"), true, false, NULL); | 139 ASCIIToUTF16("a"), true, false, NULL); |
204 EXPECT_TRUE(ASCIIToUTF16("a") == find_bar->GetFindSelectedText()); | 140 EXPECT_TRUE(ASCIIToUTF16("a") == find_bar->GetFindSelectedText()); |
205 | 141 |
206 Checkpoint("Open tab B", start_time); | |
207 | |
208 // Open another tab (tab B). | 142 // Open another tab (tab B). |
209 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED); | 143 browser()->AddSelectedTabWithURL(url, PageTransition::TYPED); |
210 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); | 144 ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
211 | 145 |
212 Checkpoint("Open find", start_time); | |
213 | |
214 // Make sure Find box is open. | 146 // Make sure Find box is open. |
215 browser()->Find(); | 147 browser()->Find(); |
216 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), | 148 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), |
217 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 149 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
218 | 150 |
219 Checkpoint("Search for 'b'", start_time); | |
220 | |
221 // Search for 'b'. | 151 // Search for 'b'. |
222 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), | 152 ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), |
223 ASCIIToUTF16("b"), true, false, NULL); | 153 ASCIIToUTF16("b"), true, false, NULL); |
224 EXPECT_TRUE(ASCIIToUTF16("b") == find_bar->GetFindSelectedText()); | 154 EXPECT_TRUE(ASCIIToUTF16("b") == find_bar->GetFindSelectedText()); |
225 | 155 |
226 Checkpoint("Focus location bar", start_time); | |
227 | |
228 // Set focus away from the Find bar (to the Location bar). | 156 // Set focus away from the Find bar (to the Location bar). |
229 browser()->FocusLocationBar(); | 157 browser()->FocusLocationBar(); |
230 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR)); | 158 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR)); |
231 | 159 |
232 Checkpoint("Select tab A", start_time); | |
233 | |
234 // Select tab A. Find bar should get focus. | 160 // Select tab A. Find bar should get focus. |
235 browser()->SelectTabContentsAt(0, true); | 161 browser()->SelectTabContentsAt(0, true); |
236 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), | 162 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), |
237 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 163 VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
238 EXPECT_TRUE(ASCIIToUTF16("a") == find_bar->GetFindSelectedText()); | 164 EXPECT_TRUE(ASCIIToUTF16("a") == find_bar->GetFindSelectedText()); |
239 | 165 |
240 Checkpoint("Select tab B", start_time); | |
241 | |
242 // Select tab B. Location bar should get focus. | 166 // Select tab B. Location bar should get focus. |
243 browser()->SelectTabContentsAt(1, true); | 167 browser()->SelectTabContentsAt(1, true); |
244 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR)); | 168 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR)); |
245 | |
246 Checkpoint("Test done", start_time); | |
247 } | 169 } |
248 | 170 |
249 // This tests that whenever you clear values from the Find box and close it that | 171 // This tests that whenever you clear values from the Find box and close it that |
250 // it respects that and doesn't show you the last search, as reported in bug: | 172 // it respects that and doesn't show you the last search, as reported in bug: |
251 // http://crbug.com/40121. | 173 // http://crbug.com/40121. |
252 IN_PROC_BROWSER_TEST_F(FindInPageTest, PrepopulateRespectBlank) { | 174 IN_PROC_BROWSER_TEST_F(FindInPageTest, PrepopulateRespectBlank) { |
253 #if defined(OS_MACOSX) | 175 #if defined(OS_MACOSX) |
254 // FindInPage on Mac doesn't use prepopulated values. Search there is global. | 176 // FindInPage on Mac doesn't use prepopulated values. Search there is global. |
255 return; | 177 return; |
256 #endif | 178 #endif |
257 | 179 |
258 base::TimeTicks start_time = base::TimeTicks::Now(); | |
259 Checkpoint("Starting test server", start_time); | |
260 | |
261 ASSERT_TRUE(test_server()->Start()); | 180 ASSERT_TRUE(test_server()->Start()); |
262 | 181 |
263 // Make sure Chrome is in the foreground, otherwise sending input | 182 // Make sure Chrome is in the foreground, otherwise sending input |
264 // won't do anything and the test will hang. | 183 // won't do anything and the test will hang. |
265 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 184 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
266 EXPECT_TRUE(ChromeInForeground()); | |
267 | 185 |
268 // First we navigate to any page. | 186 // First we navigate to any page. |
269 Checkpoint("Navigating", start_time); | |
270 GURL url = test_server()->GetURL(kSimplePage); | 187 GURL url = test_server()->GetURL(kSimplePage); |
271 ui_test_utils::NavigateToURL(browser(), url); | 188 ui_test_utils::NavigateToURL(browser(), url); |
272 | 189 |
273 // Show the Find bar. | 190 // Show the Find bar. |
274 Checkpoint("Showing Find window", start_time); | |
275 browser()->GetFindBarController()->Show(); | 191 browser()->GetFindBarController()->Show(); |
276 | 192 |
277 // Search for "a". | 193 // Search for "a". |
278 Checkpoint("Search for 'a'", start_time); | |
279 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 194 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
280 browser(), app::VKEY_A, false, false, false, false)); | 195 browser(), app::VKEY_A, false, false, false, false)); |
281 | 196 |
282 // We should find "a" here. | 197 // We should find "a" here. |
283 Checkpoint("GetFindBarText", start_time); | |
284 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText()); | 198 EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText()); |
285 | 199 |
286 // Delete "a". | 200 // Delete "a". |
287 Checkpoint("Delete 'a'", start_time); | |
288 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 201 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
289 browser(), app::VKEY_BACK, false, false, false, false)); | 202 browser(), app::VKEY_BACK, false, false, false, false)); |
290 | 203 |
291 // Validate we have cleared the text. | 204 // Validate we have cleared the text. |
292 Checkpoint("Validate clear", start_time); | |
293 EXPECT_EQ(string16(), GetFindBarText()); | 205 EXPECT_EQ(string16(), GetFindBarText()); |
294 | 206 |
295 // Close the Find box. | 207 // Close the Find box. |
296 Checkpoint("Close find", start_time); | |
297 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 208 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
298 browser(), app::VKEY_ESCAPE, false, false, false, false)); | 209 browser(), app::VKEY_ESCAPE, false, false, false, false)); |
299 | 210 |
300 // Show the Find bar. | 211 // Show the Find bar. |
301 Checkpoint("Showing Find window", start_time); | |
302 browser()->GetFindBarController()->Show(); | 212 browser()->GetFindBarController()->Show(); |
303 | 213 |
304 // After the Find box has been reopened, it should not have been prepopulated | 214 // After the Find box has been reopened, it should not have been prepopulated |
305 // with "a" again. | 215 // with "a" again. |
306 Checkpoint("GetFindBarText", start_time); | |
307 EXPECT_EQ(string16(), GetFindBarText()); | 216 EXPECT_EQ(string16(), GetFindBarText()); |
308 | 217 |
309 // Close the Find box. | 218 // Close the Find box. |
310 Checkpoint("Press Esc", start_time); | |
311 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 219 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
312 browser(), app::VKEY_ESCAPE, false, false, false, false)); | 220 browser(), app::VKEY_ESCAPE, false, false, false, false)); |
313 | 221 |
314 // Press F3 to trigger FindNext. | 222 // Press F3 to trigger FindNext. |
315 Checkpoint("Press F3", start_time); | |
316 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 223 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
317 browser(), app::VKEY_F3, false, false, false, false)); | 224 browser(), app::VKEY_F3, false, false, false, false)); |
318 | 225 |
319 // After the Find box has been reopened, it should still have no prepopulate | 226 // After the Find box has been reopened, it should still have no prepopulate |
320 // value. | 227 // value. |
321 Checkpoint("GetFindBarText", start_time); | |
322 EXPECT_EQ(string16(), GetFindBarText()); | 228 EXPECT_EQ(string16(), GetFindBarText()); |
323 | |
324 Checkpoint("Test completed", start_time); | |
325 } | 229 } |
OLD | NEW |