OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/aura/test/event_generator.h" | 5 #include "ui/aura/test/event_generator.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "ui/aura/event.h" | 8 #include "ui/aura/event.h" |
9 #include "ui/aura/root_window.h" | 9 #include "ui/aura/root_window.h" |
10 | 10 |
11 #if defined(USE_X11) | 11 #if defined(USE_X11) |
12 #include <X11/Xlib.h> | 12 #include <X11/Xlib.h> |
13 #include "ui/base/x/x11_util.h" | 13 #include "ui/base/x/x11_util.h" |
14 #endif | 14 #endif |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 class TestKeyEvent : public aura::KeyEvent { | 18 class TestKeyEvent : public aura::KeyEvent { |
19 public: | 19 public: |
20 TestKeyEvent(const base::NativeEvent& native_event, int flags) | 20 TestKeyEvent(const base::NativeEvent& native_event, int flags) |
21 : KeyEvent(native_event, false /* is_char */) { | 21 : KeyEvent(native_event, false /* is_char */) { |
22 set_flags(flags); | 22 set_flags(flags); |
23 } | 23 } |
24 }; | 24 }; |
25 | 25 |
26 gfx::Point CenterOfWindowInRootWindowCoordinate(aura::Window* window) { | 26 gfx::Point CenterOfWindowInRootWindowCoordinate(aura::RootWindow* root_window, |
| 27 aura::Window* window) { |
27 gfx::Point center = window->bounds().CenterPoint(); | 28 gfx::Point center = window->bounds().CenterPoint(); |
28 aura::RootWindow* root_window = aura::RootWindow::GetInstance(); | |
29 aura::Window::ConvertPointToWindow(window->parent(), root_window, ¢er); | 29 aura::Window::ConvertPointToWindow(window->parent(), root_window, ¢er); |
30 return center; | 30 return center; |
31 } | 31 } |
32 | 32 |
33 } // namespace | 33 } // namespace |
34 | 34 |
35 namespace aura { | 35 namespace aura { |
36 namespace test { | 36 namespace test { |
37 | 37 |
38 EventGenerator::EventGenerator() : flags_(0) { | 38 EventGenerator::EventGenerator(RootWindow* root_window) |
| 39 : root_window_(root_window), |
| 40 flags_(0) { |
39 } | 41 } |
40 | 42 |
41 EventGenerator::EventGenerator(const gfx::Point& point) | 43 EventGenerator::EventGenerator(RootWindow* root_window, const gfx::Point& point) |
42 : flags_(0), | 44 : root_window_(root_window), |
| 45 flags_(0), |
43 current_location_(point) { | 46 current_location_(point) { |
44 } | 47 } |
45 | 48 |
46 EventGenerator::EventGenerator(Window* window) | 49 EventGenerator::EventGenerator(RootWindow* root_window, Window* window) |
47 : flags_(0), | 50 : root_window_(root_window), |
48 current_location_(CenterOfWindowInRootWindowCoordinate(window)) { | 51 flags_(0), |
| 52 current_location_(CenterOfWindowInRootWindowCoordinate(root_window, |
| 53 window)) { |
49 } | 54 } |
50 | 55 |
51 EventGenerator::~EventGenerator() { | 56 EventGenerator::~EventGenerator() { |
52 } | 57 } |
53 | 58 |
54 void EventGenerator::PressLeftButton() { | 59 void EventGenerator::PressLeftButton() { |
55 if ((flags_ & ui::EF_LEFT_MOUSE_BUTTON) == 0) { | 60 if ((flags_ & ui::EF_LEFT_MOUSE_BUTTON) == 0) { |
56 flags_ |= ui::EF_LEFT_MOUSE_BUTTON; | 61 flags_ |= ui::EF_LEFT_MOUSE_BUTTON; |
57 MouseEvent mouseev( | 62 MouseEvent mouseev( |
58 ui::ET_MOUSE_PRESSED, current_location_, current_location_, flags_); | 63 ui::ET_MOUSE_PRESSED, current_location_, current_location_, flags_); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 gfx::Point(diff.x() / count * i, diff.y() / count * i)); | 96 gfx::Point(diff.x() / count * i, diff.y() / count * i)); |
92 MouseEvent mouseev(event_type, move_point, move_point, flags_); | 97 MouseEvent mouseev(event_type, move_point, move_point, flags_); |
93 Dispatch(mouseev); | 98 Dispatch(mouseev); |
94 } | 99 } |
95 current_location_ = point; | 100 current_location_ = point; |
96 } | 101 } |
97 | 102 |
98 void EventGenerator::MoveMouseRelativeTo(const Window* window, | 103 void EventGenerator::MoveMouseRelativeTo(const Window* window, |
99 const gfx::Point& point) { | 104 const gfx::Point& point) { |
100 gfx::Point root_point(point); | 105 gfx::Point root_point(point); |
101 aura::RootWindow* root_window = aura::RootWindow::GetInstance(); | 106 aura::Window::ConvertPointToWindow(window, root_window_, &root_point); |
102 aura::Window::ConvertPointToWindow(window, root_window, &root_point); | |
103 | 107 |
104 MoveMouseTo(root_point); | 108 MoveMouseTo(root_point); |
105 } | 109 } |
106 | 110 |
107 void EventGenerator::DragMouseTo(const gfx::Point& point) { | 111 void EventGenerator::DragMouseTo(const gfx::Point& point) { |
108 PressLeftButton(); | 112 PressLeftButton(); |
109 MoveMouseTo(point); | 113 MoveMouseTo(point); |
110 ReleaseLeftButton(); | 114 ReleaseLeftButton(); |
111 } | 115 } |
112 | 116 |
113 void EventGenerator::MoveMouseToCenterOf(Window* window) { | 117 void EventGenerator::MoveMouseToCenterOf(Window* window) { |
114 MoveMouseTo(CenterOfWindowInRootWindowCoordinate(window)); | 118 MoveMouseTo(CenterOfWindowInRootWindowCoordinate(root_window_, window)); |
115 } | 119 } |
116 | 120 |
117 void EventGenerator::PressTouch() { | 121 void EventGenerator::PressTouch() { |
118 TouchEvent touchev(ui::ET_TOUCH_PRESSED, current_location_, flags_); | 122 TouchEvent touchev(ui::ET_TOUCH_PRESSED, current_location_, flags_); |
119 Dispatch(touchev); | 123 Dispatch(touchev); |
120 } | 124 } |
121 | 125 |
122 void EventGenerator::ReleaseTouch() { | 126 void EventGenerator::ReleaseTouch() { |
123 TouchEvent touchev(ui::ET_TOUCH_RELEASED, current_location_, flags_); | 127 TouchEvent touchev(ui::ET_TOUCH_RELEASED, current_location_, flags_); |
124 Dispatch(touchev); | 128 Dispatch(touchev); |
125 } | 129 } |
126 | 130 |
127 void EventGenerator::PressMoveAndReleaseTouchTo(const gfx::Point& point) { | 131 void EventGenerator::PressMoveAndReleaseTouchTo(const gfx::Point& point) { |
128 PressTouch(); | 132 PressTouch(); |
129 | 133 |
130 TouchEvent touchev(ui::ET_TOUCH_MOVED, point, flags_); | 134 TouchEvent touchev(ui::ET_TOUCH_MOVED, point, flags_); |
131 Dispatch(touchev); | 135 Dispatch(touchev); |
132 | 136 |
133 current_location_ = point; | 137 current_location_ = point; |
134 | 138 |
135 ReleaseTouch(); | 139 ReleaseTouch(); |
136 } | 140 } |
137 | 141 |
138 void EventGenerator::PressMoveAndReleaseTouchToCenterOf(Window* window) { | 142 void EventGenerator::PressMoveAndReleaseTouchToCenterOf(Window* window) { |
139 PressMoveAndReleaseTouchTo(CenterOfWindowInRootWindowCoordinate(window)); | 143 PressMoveAndReleaseTouchTo(CenterOfWindowInRootWindowCoordinate(root_window_, |
| 144 window)); |
140 } | 145 } |
141 | 146 |
142 void EventGenerator::PressKey(ui::KeyboardCode key_code, int flags) { | 147 void EventGenerator::PressKey(ui::KeyboardCode key_code, int flags) { |
143 DispatchKeyEvent(true, key_code, flags); | 148 DispatchKeyEvent(true, key_code, flags); |
144 } | 149 } |
145 | 150 |
146 void EventGenerator::ReleaseKey(ui::KeyboardCode key_code, int flags) { | 151 void EventGenerator::ReleaseKey(ui::KeyboardCode key_code, int flags) { |
147 DispatchKeyEvent(false, key_code, flags); | 152 DispatchKeyEvent(false, key_code, flags); |
148 } | 153 } |
149 | 154 |
150 void EventGenerator::Dispatch(Event& event) { | 155 void EventGenerator::Dispatch(Event& event) { |
151 switch (event.type()) { | 156 switch (event.type()) { |
152 case ui::ET_KEY_PRESSED: | 157 case ui::ET_KEY_PRESSED: |
153 case ui::ET_KEY_RELEASED: | 158 case ui::ET_KEY_RELEASED: |
154 aura::RootWindow::GetInstance()->DispatchKeyEvent( | 159 root_window_->DispatchKeyEvent(static_cast<KeyEvent*>(&event)); |
155 static_cast<KeyEvent*>(&event)); | |
156 break; | 160 break; |
157 case ui::ET_MOUSE_PRESSED: | 161 case ui::ET_MOUSE_PRESSED: |
158 case ui::ET_MOUSE_DRAGGED: | 162 case ui::ET_MOUSE_DRAGGED: |
159 case ui::ET_MOUSE_RELEASED: | 163 case ui::ET_MOUSE_RELEASED: |
160 case ui::ET_MOUSE_MOVED: | 164 case ui::ET_MOUSE_MOVED: |
161 case ui::ET_MOUSE_ENTERED: | 165 case ui::ET_MOUSE_ENTERED: |
162 case ui::ET_MOUSE_EXITED: | 166 case ui::ET_MOUSE_EXITED: |
163 case ui::ET_MOUSEWHEEL: | 167 case ui::ET_MOUSEWHEEL: |
164 aura::RootWindow::GetInstance()->DispatchMouseEvent( | 168 root_window_->DispatchMouseEvent(static_cast<MouseEvent*>(&event)); |
165 static_cast<MouseEvent*>(&event)); | |
166 break; | 169 break; |
167 case ui::ET_TOUCH_RELEASED: | 170 case ui::ET_TOUCH_RELEASED: |
168 case ui::ET_TOUCH_PRESSED: | 171 case ui::ET_TOUCH_PRESSED: |
169 case ui::ET_TOUCH_MOVED: | 172 case ui::ET_TOUCH_MOVED: |
170 case ui::ET_TOUCH_STATIONARY: | 173 case ui::ET_TOUCH_STATIONARY: |
171 case ui::ET_TOUCH_CANCELLED: | 174 case ui::ET_TOUCH_CANCELLED: |
172 aura::RootWindow::GetInstance()->DispatchTouchEvent( | 175 root_window_->DispatchTouchEvent(static_cast<TouchEvent*>(&event)); |
173 static_cast<TouchEvent*>(&event)); | |
174 break; | 176 break; |
175 default: | 177 default: |
176 NOTIMPLEMENTED(); | 178 NOTIMPLEMENTED(); |
177 break; | 179 break; |
178 } | 180 } |
179 } | 181 } |
180 | 182 |
181 void EventGenerator::DispatchKeyEvent(bool is_press, | 183 void EventGenerator::DispatchKeyEvent(bool is_press, |
182 ui::KeyboardCode key_code, | 184 ui::KeyboardCode key_code, |
183 int flags) { | 185 int flags) { |
184 #if defined(OS_WIN) | 186 #if defined(OS_WIN) |
185 MSG native_event = | 187 MSG native_event = |
186 { NULL, (is_press ? WM_KEYDOWN : WM_KEYUP), key_code, 0 }; | 188 { NULL, (is_press ? WM_KEYDOWN : WM_KEYUP), key_code, 0 }; |
187 TestKeyEvent keyev(native_event, flags); | 189 TestKeyEvent keyev(native_event, flags); |
188 #else | 190 #else |
189 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED; | 191 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED; |
190 #if defined(USE_X11) | 192 #if defined(USE_X11) |
191 scoped_ptr<XEvent> native_event(new XEvent); | 193 scoped_ptr<XEvent> native_event(new XEvent); |
192 ui::InitXKeyEventForTesting(type, key_code, flags, native_event.get()); | 194 ui::InitXKeyEventForTesting(type, key_code, flags, native_event.get()); |
193 TestKeyEvent keyev(native_event.get(), flags); | 195 TestKeyEvent keyev(native_event.get(), flags); |
194 #else | 196 #else |
195 KeyEvent keyev(type, key_code, flags); | 197 KeyEvent keyev(type, key_code, flags); |
196 #endif // USE_X11 | 198 #endif // USE_X11 |
197 #endif // OS_WIN | 199 #endif // OS_WIN |
198 Dispatch(keyev); | 200 Dispatch(keyev); |
199 } | 201 } |
200 | 202 |
201 } // namespace test | 203 } // namespace test |
202 } // namespace aura | 204 } // namespace aura |
OLD | NEW |