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 return SendKeyPressToWindowSync(window
, key
, control
, shift
, alt
, command
);
45 bool SendKeyPressToWindowSync(const gfx::NativeWindow window
,
51 scoped_refptr
<content::MessageLoopRunner
> runner
=
52 new content::MessageLoopRunner
;
54 result
= ui_controls::SendKeyPressNotifyWhenDone(
55 window
, key
, control
, shift
, alt
, command
, runner
->QuitClosure());
57 if (!result
&& ui_test_utils::ShowAndFocusNativeWindow(window
)) {
58 result
= ui_controls::SendKeyPressNotifyWhenDone(
59 window
, key
, control
, shift
, alt
, command
, runner
->QuitClosure());
63 LOG(ERROR
) << "ui_controls::SendKeyPressNotifyWhenDone failed";
67 // Run the message loop. It'll stop running when either the key was received
68 // or the test timed out (in which case testing::Test::HasFatalFailure should
71 return !testing::Test::HasFatalFailure();
74 bool SendKeyPressAndWait(const Browser
* browser
,
81 const content::NotificationSource
& source
) {
82 content::WindowedNotificationObserver
observer(type
, source
);
84 if (!SendKeyPressSync(browser
, key
, control
, shift
, alt
, command
))
88 return !testing::Test::HasFatalFailure();
91 bool SendMouseMoveSync(const gfx::Point
& location
) {
92 scoped_refptr
<content::MessageLoopRunner
> runner
=
93 new content::MessageLoopRunner
;
94 if (!ui_controls::SendMouseMoveNotifyWhenDone(
95 location
.x(), location
.y(), runner
->QuitClosure())) {
99 return !testing::Test::HasFatalFailure();
102 bool SendMouseEventsSync(ui_controls::MouseButton type
, int state
) {
103 scoped_refptr
<content::MessageLoopRunner
> runner
=
104 new content::MessageLoopRunner
;
105 if (!ui_controls::SendMouseEventsNotifyWhenDone(
106 type
, state
, runner
->QuitClosure())) {
110 return !testing::Test::HasFatalFailure();
115 void ClickTask(ui_controls::MouseButton button
,
117 const base::Closure
& followup
) {
118 if (!followup
.is_null())
119 ui_controls::SendMouseEventsNotifyWhenDone(button
, state
, followup
);
121 ui_controls::SendMouseEvents(button
, state
);
124 } // namespace internal
126 } // namespace ui_test_utils