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 "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 "base/test/test_timeouts.h" |
| 11 #include "base/threading/platform_thread.h" |
10 #include "chrome_frame/utils.h" | 12 #include "chrome_frame/utils.h" |
11 | 13 |
12 namespace simulate_input { | 14 namespace simulate_input { |
13 | 15 |
14 class ForegroundHelperWindow : public CWindowImpl<ForegroundHelperWindow> { | 16 class ForegroundHelperWindow : public CWindowImpl<ForegroundHelperWindow> { |
15 public: | 17 public: |
16 BEGIN_MSG_MAP(ForegroundHelperWindow) | 18 BEGIN_MSG_MAP(ForegroundHelperWindow) |
17 MESSAGE_HANDLER(WM_HOTKEY, OnHotKey) | 19 MESSAGE_HANDLER(WM_HOTKEY, OnHotKey) |
18 END_MSG_MAP() | 20 END_MSG_MAP() |
19 | 21 |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 } | 186 } |
185 | 187 |
186 SendInput(key_count, &keys[0], sizeof(keys[0])); | 188 SendInput(key_count, &keys[0], sizeof(keys[0])); |
187 } | 189 } |
188 | 190 |
189 void SetKeyboardFocusToWindow(HWND window) { | 191 void SetKeyboardFocusToWindow(HWND window) { |
190 SendMouseClick(window, 1, 1, LEFT); | 192 SendMouseClick(window, 1, 1, LEFT); |
191 } | 193 } |
192 | 194 |
193 void SendMouseClick(int x, int y, MouseButton button) { | 195 void SendMouseClick(int x, int y, MouseButton button) { |
| 196 const base::TimeDelta kMessageTimeout = TestTimeouts::tiny_timeout(); |
194 // TODO(joshia): Fix this. GetSystemMetrics(SM_CXSCREEN) will | 197 // TODO(joshia): Fix this. GetSystemMetrics(SM_CXSCREEN) will |
195 // retrieve screen size of the primarary monitor only. And monitors | 198 // retrieve screen size of the primarary monitor only. And monitors |
196 // arrangement could be pretty arbitrary. | 199 // arrangement could be pretty arbitrary. |
197 double screen_width = ::GetSystemMetrics(SM_CXSCREEN) - 1; | 200 double screen_width = ::GetSystemMetrics(SM_CXSCREEN) - 1; |
198 double screen_height = ::GetSystemMetrics(SM_CYSCREEN) - 1; | 201 double screen_height = ::GetSystemMetrics(SM_CYSCREEN) - 1; |
199 double location_x = x * (65535.0f / screen_width); | 202 double location_x = x * (65535.0f / screen_width); |
200 double location_y = y * (65535.0f / screen_height); | 203 double location_y = y * (65535.0f / screen_height); |
201 | 204 |
202 // Take advantage of button flag bitmask layout | 205 // Take advantage of button flag bitmask layout |
203 unsigned int button_flag = MOUSEEVENTF_LEFTDOWN << (button + button); | 206 unsigned int button_flag = MOUSEEVENTF_LEFTDOWN << (button + button); |
204 | 207 |
205 INPUT input_info = {0}; | 208 INPUT input_info = {0}; |
206 input_info.type = INPUT_MOUSE; | 209 input_info.type = INPUT_MOUSE; |
207 input_info.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE; | 210 input_info.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE; |
208 input_info.mi.dx = static_cast<LONG>(location_x); | 211 input_info.mi.dx = static_cast<LONG>(location_x); |
209 input_info.mi.dy = static_cast<LONG>(location_y); | 212 input_info.mi.dy = static_cast<LONG>(location_y); |
210 ::SendInput(1, &input_info, sizeof(INPUT)); | 213 ::SendInput(1, &input_info, sizeof(INPUT)); |
211 | 214 base::PlatformThread::Sleep(kMessageTimeout); |
212 Sleep(10); | |
213 | 215 |
214 input_info.mi.dwFlags = button_flag | MOUSEEVENTF_ABSOLUTE; | 216 input_info.mi.dwFlags = button_flag | MOUSEEVENTF_ABSOLUTE; |
215 ::SendInput(1, &input_info, sizeof(INPUT)); | 217 ::SendInput(1, &input_info, sizeof(INPUT)); |
216 | 218 base::PlatformThread::Sleep(kMessageTimeout); |
217 Sleep(10); | |
218 | 219 |
219 input_info.mi.dwFlags = (button_flag << 1) | MOUSEEVENTF_ABSOLUTE; | 220 input_info.mi.dwFlags = (button_flag << 1) | MOUSEEVENTF_ABSOLUTE; |
220 ::SendInput(1, &input_info, sizeof(INPUT)); | 221 ::SendInput(1, &input_info, sizeof(INPUT)); |
| 222 base::PlatformThread::Sleep(kMessageTimeout); |
221 } | 223 } |
222 | 224 |
223 void SendMouseClick(HWND window, int x, int y, MouseButton button) { | 225 void SendMouseClick(HWND window, int x, int y, MouseButton button) { |
224 if (!IsWindow(window)) { | 226 if (!IsWindow(window)) { |
225 NOTREACHED() << "Invalid window handle."; | 227 NOTREACHED() << "Invalid window handle."; |
226 return; | 228 return; |
227 } | 229 } |
228 | 230 |
229 HWND top_level_window = window; | 231 HWND top_level_window = window; |
230 if (!IsTopLevelWindow(top_level_window)) { | 232 if (!IsTopLevelWindow(top_level_window)) { |
(...skipping 19 matching lines...) Expand all Loading... |
250 } | 252 } |
251 | 253 |
252 void SendStringA(const std::string& s) { | 254 void SendStringA(const std::string& s) { |
253 for (size_t i = 0; i < s.length(); i++) { | 255 for (size_t i = 0; i < s.length(); i++) { |
254 SendCharA(s[i], NONE); | 256 SendCharA(s[i], NONE); |
255 Sleep(10); | 257 Sleep(10); |
256 } | 258 } |
257 } | 259 } |
258 | 260 |
259 } // namespace simulate_input | 261 } // namespace simulate_input |
OLD | NEW |