OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome_frame/test/simulate_input.h" | 5 #include "chrome_frame/test/simulate_input.h" |
6 | 6 |
7 #include <atlbase.h> | 7 #include <atlbase.h> |
8 #include <atlwin.h> | 8 #include <atlwin.h> |
9 | 9 |
10 #include "chrome_frame/utils.h" | 10 #include "chrome_frame/utils.h" |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 if (should_sleep) { | 185 if (should_sleep) { |
186 Sleep(10); | 186 Sleep(10); |
187 } | 187 } |
188 } | 188 } |
189 } | 189 } |
190 | 190 |
191 void SetKeyboardFocusToWindow(HWND window) { | 191 void SetKeyboardFocusToWindow(HWND window) { |
192 SendMouseClick(window, 1, 1, LEFT); | 192 SendMouseClick(window, 1, 1, LEFT); |
193 } | 193 } |
194 | 194 |
195 void SendMouseClick(HWND window, int x, int y, MouseButton button) { | 195 void SendMouseClick(int x, int y, MouseButton button) { |
196 if (!IsWindow(window)) { | |
197 NOTREACHED() << "Invalid window handle."; | |
198 return; | |
199 } | |
200 | |
201 HWND top_level_window = window; | |
202 if (!IsTopLevelWindow(top_level_window)) { | |
203 top_level_window = GetAncestor(window, GA_ROOT); | |
204 } | |
205 | |
206 ForceSetForegroundWindow(top_level_window); | |
207 | |
208 POINT cursor_position = {x, y}; | |
209 ClientToScreen(window, &cursor_position); | |
210 | |
211 // TODO(joshia): Fix this. GetSystemMetrics(SM_CXSCREEN) will | 196 // TODO(joshia): Fix this. GetSystemMetrics(SM_CXSCREEN) will |
212 // retrieve screen size of the primarary monitor only. And monitors | 197 // retrieve screen size of the primarary monitor only. And monitors |
213 // arrangement could be pretty arbitrary. | 198 // arrangement could be pretty arbitrary. |
214 double screen_width = ::GetSystemMetrics(SM_CXSCREEN) - 1; | 199 double screen_width = ::GetSystemMetrics(SM_CXSCREEN) - 1; |
215 double screen_height = ::GetSystemMetrics(SM_CYSCREEN) - 1; | 200 double screen_height = ::GetSystemMetrics(SM_CYSCREEN) - 1; |
216 double location_x = cursor_position.x * (65535.0f / screen_width); | 201 double location_x = x * (65535.0f / screen_width); |
217 double location_y = cursor_position.y * (65535.0f / screen_height); | 202 double location_y = y * (65535.0f / screen_height); |
218 | 203 |
219 // Take advantage of button flag bitmask layout | 204 // Take advantage of button flag bitmask layout |
220 unsigned int button_flag = MOUSEEVENTF_LEFTDOWN << (button + button); | 205 unsigned int button_flag = MOUSEEVENTF_LEFTDOWN << (button + button); |
221 | 206 |
222 INPUT input_info = {0}; | 207 INPUT input_info = {0}; |
223 input_info.type = INPUT_MOUSE; | 208 input_info.type = INPUT_MOUSE; |
224 input_info.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE; | 209 input_info.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE; |
225 input_info.mi.dx = static_cast<LONG>(location_x); | 210 input_info.mi.dx = static_cast<LONG>(location_x); |
226 input_info.mi.dy = static_cast<LONG>(location_y); | 211 input_info.mi.dy = static_cast<LONG>(location_y); |
227 ::SendInput(1, &input_info, sizeof(INPUT)); | 212 ::SendInput(1, &input_info, sizeof(INPUT)); |
228 | 213 |
229 Sleep(10); | 214 Sleep(10); |
230 | 215 |
231 input_info.mi.dwFlags = button_flag | MOUSEEVENTF_ABSOLUTE; | 216 input_info.mi.dwFlags = button_flag | MOUSEEVENTF_ABSOLUTE; |
232 ::SendInput(1, &input_info, sizeof(INPUT)); | 217 ::SendInput(1, &input_info, sizeof(INPUT)); |
233 | 218 |
234 Sleep(10); | 219 Sleep(10); |
235 | 220 |
236 input_info.mi.dwFlags = (button_flag << 1) | MOUSEEVENTF_ABSOLUTE; | 221 input_info.mi.dwFlags = (button_flag << 1) | MOUSEEVENTF_ABSOLUTE; |
237 ::SendInput(1, &input_info, sizeof(INPUT)); | 222 ::SendInput(1, &input_info, sizeof(INPUT)); |
238 } | 223 } |
239 | 224 |
| 225 void SendMouseClick(HWND window, int x, int y, MouseButton button) { |
| 226 if (!IsWindow(window)) { |
| 227 NOTREACHED() << "Invalid window handle."; |
| 228 return; |
| 229 } |
| 230 |
| 231 HWND top_level_window = window; |
| 232 if (!IsTopLevelWindow(top_level_window)) { |
| 233 top_level_window = GetAncestor(window, GA_ROOT); |
| 234 } |
| 235 |
| 236 ForceSetForegroundWindow(top_level_window); |
| 237 |
| 238 POINT cursor_position = {x, y}; |
| 239 ClientToScreen(window, &cursor_position); |
| 240 SendMouseClick(cursor_position.x, cursor_position.y, button); |
| 241 } |
| 242 |
240 void SendExtendedKey(WORD key, Modifier modifiers) { | 243 void SendExtendedKey(WORD key, Modifier modifiers) { |
241 SendMnemonic(key, modifiers, true, false); | 244 SendMnemonic(key, modifiers, true, false); |
242 } | 245 } |
243 | 246 |
244 void SendStringW(const wchar_t* s) { | 247 void SendStringW(const wchar_t* s) { |
245 while (*s) { | 248 while (*s) { |
246 wchar_t ch = *s; | 249 wchar_t ch = *s; |
247 SendCharW(ch, NONE); | 250 SendCharW(ch, NONE); |
248 Sleep(10); | 251 Sleep(10); |
249 s++; | 252 s++; |
250 } | 253 } |
251 } | 254 } |
252 | 255 |
253 void SendStringA(const char* s) { | 256 void SendStringA(const char* s) { |
254 while (*s) { | 257 while (*s) { |
255 char ch = *s; | 258 char ch = *s; |
256 SendCharA(ch, NONE); | 259 SendCharA(ch, NONE); |
257 Sleep(10); | 260 Sleep(10); |
258 s++; | 261 s++; |
259 } | 262 } |
260 } | 263 } |
261 | 264 |
262 } // namespace simulate_input | 265 } // namespace simulate_input |
263 | 266 |
OLD | NEW |