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.
5 #include "chrome/test/base/interactive_test_utils.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_window.h"
10 namespace ui_test_utils
{
14 bool GetNativeWindow(const Browser
* browser
, gfx::NativeWindow
* native_window
) {
15 BrowserWindow
* window
= browser
->window();
19 *native_window
= window
->GetNativeWindow();
20 return *native_window
;
25 bool BringBrowserWindowToFront(const Browser
* browser
) {
26 gfx::NativeWindow window
= NULL
;
27 if (!GetNativeWindow(browser
, &window
))
30 return ui_test_utils::ShowAndFocusNativeWindow(window
);
33 bool SendKeyPressSync(const Browser
* browser
,
39 gfx::NativeWindow window
= NULL
;
40 if (!GetNativeWindow(browser
, &window
))
42 scoped_refptr
<content::MessageLoopRunner
> runner
=
43 new content::MessageLoopRunner
;
45 result
= ui_controls::SendKeyPressNotifyWhenDone(
46 window
, key
, control
, shift
, alt
, command
, runner
->QuitClosure());
48 if (!result
&& BringBrowserWindowToFront(browser
)) {
49 result
= ui_controls::SendKeyPressNotifyWhenDone(
50 window
, key
, control
, shift
, alt
, command
, runner
->QuitClosure());
54 LOG(ERROR
) << "ui_controls::SendKeyPressNotifyWhenDone failed";
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
62 return !testing::Test::HasFatalFailure();
65 bool SendKeyPressAndWait(const Browser
* browser
,
72 const content::NotificationSource
& source
) {
73 content::WindowedNotificationObserver
observer(type
, source
);
75 if (!SendKeyPressSync(browser
, key
, control
, shift
, alt
, command
))
79 return !testing::Test::HasFatalFailure();
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())) {
90 return !testing::Test::HasFatalFailure();
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())) {
101 return !testing::Test::HasFatalFailure();
105 } // namespace ui_test_utils