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#ifndef CHROME_TEST_BASE_INTERACTIVE_TEST_UTILS_H_
6#define CHROME_TEST_BASE_INTERACTIVE_TEST_UTILS_H_
7
8#include "chrome/browser/ui/view_ids.h"
9#include "chrome/test/base/ui_test_utils.h"
10#include "ui/base/test/ui_controls.h"
11
12namespace gfx {
13class Point;
14}
15
16#if defined(TOOLKIT_VIEWS)
17namespace views {
18class View;
19}
20#endif
21
22namespace ui_test_utils {
23
24// Brings the native window for |browser| to the foreground. Returns true on
25// success.
26bool BringBrowserWindowToFront(const Browser* browser) WARN_UNUSED_RESULT;
27
28// Returns true if the View is focused.
29bool IsViewFocused(const Browser* browser, ViewID vid);
30
31// Simulates a mouse click on a View in the browser.
32void ClickOnView(const Browser* browser, ViewID vid);
33
34// Makes focus shift to the given View without clicking it.
35void FocusView(const Browser* browser, ViewID vid);
36
37// A collection of utilities that are used from interactive_ui_tests. These are
38// separated from ui_test_utils.h to ensure that browser_tests don't use them,
39// since they depend on focus which isn't possible for sharded test.
40
41// Hide a native window.
42void HideNativeWindow(gfx::NativeWindow window);
43
44// Show and focus a native window. Returns true on success.
45bool ShowAndFocusNativeWindow(gfx::NativeWindow window) WARN_UNUSED_RESULT;
46
47// Sends a key press, blocking until the key press is received or the test times
48// out. This uses ui_controls::SendKeyPress, see it for details. Returns true
49// if the event was successfully sent and received.
50bool SendKeyPressSync(const Browser* browser,
51                      ui::KeyboardCode key,
52                      bool control,
53                      bool shift,
54                      bool alt,
55                      bool command) WARN_UNUSED_RESULT;
56
57// Sends a key press, blocking until the key press is received or the test times
58// out. This uses ui_controls::SendKeyPress, see it for details. Returns true
59// if the event was successfully sent and received.
60bool SendKeyPressToWindowSync(const gfx::NativeWindow window,
61                              ui::KeyboardCode key,
62                              bool control,
63                              bool shift,
64                              bool alt,
65                              bool command) WARN_UNUSED_RESULT;
66
67// Sends a key press, blocking until both the key press and a notification from
68// |source| of type |type| are received, or until the test times out. This uses
69// ui_controls::SendKeyPress, see it for details. Returns true if the event was
70// successfully sent and both the event and notification were received.
71bool SendKeyPressAndWait(const Browser* browser,
72                         ui::KeyboardCode key,
73                         bool control,
74                         bool shift,
75                         bool alt,
76                         bool command,
77                         int type,
78                         const content::NotificationSource& source)
79                             WARN_UNUSED_RESULT;
80
81// Sends a move event blocking until received. Returns true if the event was
82// successfully received. This uses ui_controls::SendMouse***NotifyWhenDone,
83// see it for details.
84bool SendMouseMoveSync(const gfx::Point& location) WARN_UNUSED_RESULT;
85bool SendMouseEventsSync(ui_controls::MouseButton type,
86                         int state) WARN_UNUSED_RESULT;
87
88// See SendKeyPressAndWait.  This function additionally performs a check on the
89// NotificationDetails using the provided Details<U>.
90template <class U>
91bool SendKeyPressAndWaitWithDetails(
92    const Browser* browser,
93    ui::KeyboardCode key,
94    bool control,
95    bool shift,
96    bool alt,
97    bool command,
98    int type,
99    const content::NotificationSource& source,
100    const content::Details<U>& details) WARN_UNUSED_RESULT;
101
102template <class U>
103bool SendKeyPressAndWaitWithDetails(
104    const Browser* browser,
105    ui::KeyboardCode key,
106    bool control,
107    bool shift,
108    bool alt,
109    bool command,
110    int type,
111    const content::NotificationSource& source,
112    const content::Details<U>& details) {
113  WindowedNotificationObserverWithDetails<U> observer(type, source);
114
115  if (!SendKeyPressSync(browser, key, control, shift, alt, command))
116    return false;
117
118  observer.Wait();
119
120  U my_details;
121  if (!observer.GetDetailsFor(source.map_key(), &my_details))
122    return false;
123
124  return *details.ptr() == my_details && !testing::Test::HasFatalFailure();
125}
126
127// A combination of SendMouseMove to the middle of the view followed by
128// SendMouseEvents. Only exposed for toolkit-views.
129// Alternatives: ClickOnView() and ui::test::EventGenerator.
130#if defined(TOOLKIT_VIEWS)
131void MoveMouseToCenterAndPress(views::View* view,
132                               ui_controls::MouseButton button,
133                               int state,
134                               const base::Closure& task);
135#endif
136
137namespace internal {
138
139// A utility function to send a mouse click event in a closure. It's shared by
140// ui_controls_linux.cc and ui_controls_mac.cc
141void ClickTask(ui_controls::MouseButton button,
142               int state,
143               const base::Closure& followup);
144
145}  // namespace internal
146
147}  // namespace ui_test_utils
148
149#endif  // CHROME_TEST_BASE_INTERACTIVE_TEST_UTILS_H_
150