| OLD | NEW |
| (Empty) | |
| 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 "chrome/test/base/interactive_test_utils.h" |
| 6 |
| 7 #include "chrome/browser/ui/browser.h" |
| 8 #include "chrome/browser/ui/browser_window.h" |
| 9 |
| 10 namespace ui_test_utils { |
| 11 |
| 12 namespace { |
| 13 |
| 14 bool GetNativeWindow(const Browser* browser, gfx::NativeWindow* native_window) { |
| 15 BrowserWindow* window = browser->window(); |
| 16 if (!window) |
| 17 return false; |
| 18 |
| 19 *native_window = window->GetNativeWindow(); |
| 20 return *native_window; |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 bool BringBrowserWindowToFront(const Browser* browser) { |
| 26 gfx::NativeWindow window = NULL; |
| 27 if (!GetNativeWindow(browser, &window)) |
| 28 return false; |
| 29 |
| 30 return ui_test_utils::ShowAndFocusNativeWindow(window); |
| 31 } |
| 32 |
| 33 bool SendKeyPressSync(const Browser* browser, |
| 34 ui::KeyboardCode key, |
| 35 bool control, |
| 36 bool shift, |
| 37 bool alt, |
| 38 bool command) { |
| 39 gfx::NativeWindow window = NULL; |
| 40 if (!GetNativeWindow(browser, &window)) |
| 41 return false; |
| 42 scoped_refptr<content::MessageLoopRunner> runner = |
| 43 new content::MessageLoopRunner; |
| 44 bool result; |
| 45 result = ui_controls::SendKeyPressNotifyWhenDone( |
| 46 window, key, control, shift, alt, command, runner->QuitClosure()); |
| 47 #if defined(OS_WIN) |
| 48 if (!result && BringBrowserWindowToFront(browser)) { |
| 49 result = ui_controls::SendKeyPressNotifyWhenDone( |
| 50 window, key, control, shift, alt, command, runner->QuitClosure()); |
| 51 } |
| 52 #endif |
| 53 if (!result) { |
| 54 LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed"; |
| 55 return false; |
| 56 } |
| 57 |
| 58 // Run the message loop. It'll stop running when either the key was received |
| 59 // or the test timed out (in which case testing::Test::HasFatalFailure should |
| 60 // be set). |
| 61 runner->Run(); |
| 62 return !testing::Test::HasFatalFailure(); |
| 63 } |
| 64 |
| 65 bool SendKeyPressAndWait(const Browser* browser, |
| 66 ui::KeyboardCode key, |
| 67 bool control, |
| 68 bool shift, |
| 69 bool alt, |
| 70 bool command, |
| 71 int type, |
| 72 const content::NotificationSource& source) { |
| 73 content::WindowedNotificationObserver observer(type, source); |
| 74 |
| 75 if (!SendKeyPressSync(browser, key, control, shift, alt, command)) |
| 76 return false; |
| 77 |
| 78 observer.Wait(); |
| 79 return !testing::Test::HasFatalFailure(); |
| 80 } |
| 81 |
| 82 bool SendMouseMoveSync(const gfx::Point& location) { |
| 83 scoped_refptr<content::MessageLoopRunner> runner = |
| 84 new content::MessageLoopRunner; |
| 85 if (!ui_controls::SendMouseMoveNotifyWhenDone( |
| 86 location.x(), location.y(), runner->QuitClosure())) { |
| 87 return false; |
| 88 } |
| 89 runner->Run(); |
| 90 return !testing::Test::HasFatalFailure(); |
| 91 } |
| 92 |
| 93 bool SendMouseEventsSync(ui_controls::MouseButton type, int state) { |
| 94 scoped_refptr<content::MessageLoopRunner> runner = |
| 95 new content::MessageLoopRunner; |
| 96 if (!ui_controls::SendMouseEventsNotifyWhenDone( |
| 97 type, state, runner->QuitClosure())) { |
| 98 return false; |
| 99 } |
| 100 runner->Run(); |
| 101 return !testing::Test::HasFatalFailure(); |
| 102 } |
| 103 |
| 104 |
| 105 } // namespace ui_test_utils |
| OLD | NEW |