OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
6 #include "chrome/browser/profiles/profile.h" | 6 #include "chrome/browser/profiles/profile.h" |
7 #include "chrome/browser/ui/browser.h" | 7 #include "chrome/browser/ui/browser.h" |
8 #include "chrome/browser/ui/browser_commands.h" | 8 #include "chrome/browser/ui/browser_commands.h" |
9 #include "chrome/browser/ui/constrained_window_tab_helper.h" | 9 #include "chrome/browser/ui/constrained_window_tab_helper.h" |
10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
11 #include "chrome/browser/ui/views/constrained_window_views.h" | 11 #include "chrome/browser/ui/views/constrained_window_views.h" |
12 #include "chrome/browser/ui/views/frame/browser_view.h" | 12 #include "chrome/browser/ui/views/frame/browser_view.h" |
13 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" | 13 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" |
14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
15 #include "chrome/test/base/in_process_browser_test.h" | 15 #include "chrome/test/base/in_process_browser_test.h" |
16 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
17 #include "content/public/browser/native_web_keyboard_event.h" | |
18 #include "content/public/browser/render_view_host.h" | |
17 #include "ipc/ipc_message.h" | 19 #include "ipc/ipc_message.h" |
18 #include "ui/base/accelerators/accelerator.h" | 20 #include "ui/base/accelerators/accelerator.h" |
19 #include "ui/views/controls/textfield/textfield.h" | 21 #include "ui/views/controls/textfield/textfield.h" |
20 #include "ui/views/focus/focus_manager.h" | 22 #include "ui/views/focus/focus_manager.h" |
21 #include "ui/views/layout/fill_layout.h" | 23 #include "ui/views/layout/fill_layout.h" |
24 #include "ui/views/test/test_widget_observer.h" | |
22 #include "ui/views/window/dialog_delegate.h" | 25 #include "ui/views/window/dialog_delegate.h" |
23 #include "ui/web_dialogs/test/test_web_dialog_delegate.h" | 26 #include "ui/web_dialogs/test/test_web_dialog_delegate.h" |
24 | 27 |
28 #if defined(USE_AURA) && defined(USE_X11) | |
29 #include <X11/Xlib.h> | |
30 #include "ui/base/x/x11_util.h" | |
31 #endif | |
32 | |
25 namespace { | 33 namespace { |
26 | 34 |
27 class TestConstrainedDialogContentsView | 35 class TestConstrainedDialogContentsView |
28 : public views::View, | 36 : public views::View, |
29 public base::SupportsWeakPtr<TestConstrainedDialogContentsView> { | 37 public base::SupportsWeakPtr<TestConstrainedDialogContentsView> { |
30 public: | 38 public: |
31 TestConstrainedDialogContentsView() | 39 TestConstrainedDialogContentsView() |
32 : text_field_(new views::Textfield) { | 40 : text_field_(new views::Textfield) { |
33 SetLayoutManager(new views::FillLayout); | 41 SetLayoutManager(new views::FillLayout); |
34 AddChildView(text_field_); | 42 AddChildView(text_field_); |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 EXPECT_TRUE(window->IsVisible()); | 251 EXPECT_TRUE(window->IsVisible()); |
244 | 252 |
245 // Close the original tab. | 253 // Close the original tab. |
246 browser()->tab_strip_model()->CloseWebContentsAt( | 254 browser()->tab_strip_model()->CloseWebContentsAt( |
247 browser()->tab_strip_model()->active_index(), | 255 browser()->tab_strip_model()->active_index(), |
248 TabStripModel::CLOSE_NONE); | 256 TabStripModel::CLOSE_NONE); |
249 content::RunAllPendingInMessageLoop(); | 257 content::RunAllPendingInMessageLoop(); |
250 EXPECT_TRUE(test_dialog->done()); | 258 EXPECT_TRUE(test_dialog->done()); |
251 } | 259 } |
252 | 260 |
253 #if defined(OS_WIN) && !defined(USE_AURA) | 261 #if defined(OS_WIN) || (defined(USE_AURA) && defined(USE_X11)) |
262 | |
254 // Tests that backspace is not processed before it's sent to the web contents. | 263 // Tests that backspace is not processed before it's sent to the web contents. |
255 // We do not run this test on Aura because key events are sent to the web | |
256 // contents through a different code path that does not call | |
257 // views::FocusManager::OnKeyEvent when ConstrainedWindow is focused. | |
258 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, | 264 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, |
259 BackspaceSentToWebContent) { | 265 BackspaceSentToWebContent) { |
260 content::WebContents* web_contents = | 266 content::WebContents* web_contents = |
261 browser()->tab_strip_model()->GetActiveWebContents(); | 267 browser()->tab_strip_model()->GetActiveWebContents(); |
262 ASSERT_TRUE(web_contents != NULL); | 268 ASSERT_TRUE(web_contents != NULL); |
263 | 269 |
264 GURL url(chrome::kChromeUINewTabURL); | 270 GURL new_tab_url(chrome::kChromeUINewTabURL); |
265 ui_test_utils::NavigateToURL(browser(), url); | 271 ui_test_utils::NavigateToURL(browser(), new_tab_url); |
272 GURL about_url(chrome::kChromeUIAboutURL); | |
273 ui_test_utils::NavigateToURL(browser(), about_url); | |
266 | 274 |
267 ConstrainedWebDialogDelegate* cwdd = CreateConstrainedWebDialog( | 275 ConstrainedWebDialogDelegate* cwdd = CreateConstrainedWebDialog( |
268 browser()->profile(), | 276 browser()->profile(), |
269 new ui::test::TestWebDialogDelegate(url), | 277 new ui::test::TestWebDialogDelegate(about_url), |
278 NULL, | |
279 web_contents); | |
280 | |
281 #if defined(OS_WIN) | |
282 MSG native_key_event = { NULL, WM_KEYDOWN, ui::VKEY_BACK, 0 }; | |
283 #elif defined(USE_X11) | |
284 XEvent x_event; | |
285 ui::InitXKeyEventForTesting( | |
286 ui::ET_KEY_PRESSED, ui::VKEY_BACK, ui::EF_NONE, &x_event); | |
287 XEvent* native_key_event = &x_event; | |
288 #endif | |
289 | |
290 #if defined(USE_AURA) | |
291 ui::KeyEvent key(native_key_event, false); | |
292 ui::KeyEvent* native_ui_key_event = &key; | |
293 #else | |
294 MSG native_ui_key_event = native_key_event; | |
295 #endif | |
296 | |
297 content::RenderWidgetHost* render_widget_host = | |
298 cwdd->GetWebContents()->GetRenderViewHost(); | |
299 render_widget_host->ForwardKeyboardEvent( | |
300 content::NativeWebKeyboardEvent(native_ui_key_event)); | |
301 | |
302 // Backspace is not processed as accelerator before it's sent to web contents. | |
303 EXPECT_EQ(about_url.spec(), web_contents->GetURL().spec()); | |
304 | |
305 content::RunAllPendingInMessageLoop(); | |
306 | |
307 // Backspace is processed as accelerator after it's sent to web contents. | |
308 EXPECT_EQ(new_tab_url.spec(), web_contents->GetURL().spec()); | |
309 } | |
310 | |
311 // Tests that escape closes the constrained window. | |
312 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, | |
313 EscapeCloseConstrainedWindow) { | |
314 content::WebContents* web_contents = | |
315 browser()->tab_strip_model()->GetActiveWebContents(); | |
316 ASSERT_TRUE(web_contents != NULL); | |
317 | |
318 GURL new_tab_url(chrome::kChromeUINewTabURL); | |
319 ui_test_utils::NavigateToURL(browser(), new_tab_url); | |
320 ConstrainedWebDialogDelegate* cwdd = CreateConstrainedWebDialog( | |
321 browser()->profile(), | |
322 new ui::test::TestWebDialogDelegate(new_tab_url), | |
270 NULL, | 323 NULL, |
271 web_contents); | 324 web_contents); |
272 | 325 |
273 ConstrainedWindowViews* cwv = | 326 ConstrainedWindowViews* cwv = |
274 static_cast<ConstrainedWindowViews*>(cwdd->GetWindow()); | 327 static_cast<ConstrainedWindowViews*>(cwdd->GetWindow()); |
328 views::test::TestWidgetObserver observer(cwv); | |
275 cwv->FocusConstrainedWindow(); | 329 cwv->FocusConstrainedWindow(); |
276 | 330 |
277 BrowserView* browser_view = static_cast<BrowserView*>(browser()->window()); | 331 #if defined(OS_WIN) |
sky
2012/12/21 15:46:01
Can you refactor these ifdefs in some way to make
mazda
2013/01/07 22:41:14
Done.
| |
278 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, | 332 MSG native_key_event = { NULL, WM_KEYDOWN, ui::VKEY_ESCAPE, 0 }; |
279 ui::VKEY_BACK, | 333 #elif defined(USE_X11) |
280 ui::EF_NONE, | 334 XEvent x_event; |
281 false); | 335 ui::InitXKeyEventForTesting( |
282 bool not_consumed = browser_view->GetFocusManager()->OnKeyEvent(key_event); | 336 ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, ui::EF_NONE, &x_event); |
283 EXPECT_TRUE(not_consumed); | 337 XEvent* native_key_event = &x_event; |
338 #endif | |
339 | |
340 #if defined(USE_AURA) | |
341 ui::KeyEvent key(native_key_event, false); | |
342 ui::KeyEvent* native_ui_key_event = &key; | |
343 #else | |
344 MSG native_ui_key_event = native_key_event; | |
345 #endif | |
346 | |
347 content::RenderWidgetHost* render_widget_host = | |
348 cwdd->GetWebContents()->GetRenderViewHost(); | |
349 render_widget_host->ForwardKeyboardEvent( | |
350 content::NativeWebKeyboardEvent(native_ui_key_event)); | |
351 | |
352 // Escape is not processed as accelerator before it's sent to web contents. | |
353 EXPECT_FALSE(observer.widget_closed()); | |
354 | |
355 content::RunAllPendingInMessageLoop(); | |
356 | |
357 // Escape is processed as accelerator after it's sent to web contents. | |
358 EXPECT_TRUE(observer.widget_closed()); | |
284 } | 359 } |
285 #endif // defined(OS_WIN) && !defined(USE_AURA) | 360 |
361 #endif // defined(OS_WIN) || (defined(USE_AURA) && defined(USE_X11)) | |
OLD | NEW |