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