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
10namespace ui_test_utils {
11
12namespace {
13
14bool 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
25bool 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
33bool 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  return SendKeyPressToWindowSync(window, key, control, shift, alt, command);
43}
44
45bool SendKeyPressToWindowSync(const gfx::NativeWindow window,
46                              ui::KeyboardCode key,
47                              bool control,
48                              bool shift,
49                              bool alt,
50                              bool command) {
51  scoped_refptr<content::MessageLoopRunner> runner =
52      new content::MessageLoopRunner;
53  bool result;
54  result = ui_controls::SendKeyPressNotifyWhenDone(
55      window, key, control, shift, alt, command, runner->QuitClosure());
56#if defined(OS_WIN)
57  if (!result && ui_test_utils::ShowAndFocusNativeWindow(window)) {
58    result = ui_controls::SendKeyPressNotifyWhenDone(
59        window, key, control, shift, alt, command, runner->QuitClosure());
60  }
61#endif
62  if (!result) {
63    LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed";
64    return false;
65  }
66
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
69  // be set).
70  runner->Run();
71  return !testing::Test::HasFatalFailure();
72}
73
74bool SendKeyPressAndWait(const Browser* browser,
75                         ui::KeyboardCode key,
76                         bool control,
77                         bool shift,
78                         bool alt,
79                         bool command,
80                         int type,
81                         const content::NotificationSource& source) {
82  content::WindowedNotificationObserver observer(type, source);
83
84  if (!SendKeyPressSync(browser, key, control, shift, alt, command))
85    return false;
86
87  observer.Wait();
88  return !testing::Test::HasFatalFailure();
89}
90
91bool 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())) {
96    return false;
97  }
98  runner->Run();
99  return !testing::Test::HasFatalFailure();
100}
101
102bool 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())) {
107    return false;
108  }
109  runner->Run();
110  return !testing::Test::HasFatalFailure();
111}
112
113namespace internal {
114
115void ClickTask(ui_controls::MouseButton button,
116               int state,
117               const base::Closure& followup) {
118  if (!followup.is_null())
119    ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup);
120  else
121    ui_controls::SendMouseEvents(button, state);
122}
123
124}  // namespace internal
125
126}  // namespace ui_test_utils
127