[USER32_APITEST]
[reactos.git] / rostests / apitests / user32 / RegisterHotKey.c
1 /*
2 * PROJECT: ReactOS API tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test for RegisterHotKey
5 * PROGRAMMERS: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8 #include <apitest.h>
9
10 #include <winuser.h>
11 #include <shlobj.h>
12 #include <undocshell.h>
13 #include <undocuser.h>
14
15 #define msg_hotkey(msg, id, mod, vk) do \
16 { \
17 ok((msg)->message == WM_HOTKEY, "Unexpected message %u\n", (msg)->message); \
18 ok((msg)->hwnd == NULL, "hwnd = %p\n", (msg)->hwnd); \
19 ok((msg)->wParam == (id), "wParam = 0x%Ix\n", (msg)->wParam); \
20 ok((msg)->lParam == MAKELONG(mod, vk), \
21 "wParam = 0x%Ix, expected 0x%lx\n", (msg)->lParam, MAKELONG(mod, vk)); \
22 } while (0)
23 #define expect_hotkey(id, mod, vk) do \
24 { \
25 MSG msg; \
26 int hotkey_count = 0; \
27 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) \
28 { \
29 msg_hotkey(&msg, id, mod, vk); \
30 if (msg.message == WM_HOTKEY) hotkey_count++; \
31 DispatchMessageW(&msg); \
32 } \
33 ok(hotkey_count == 1, "Received %d WM_HOTKEY messages, expected 1\n"); \
34 } while (0)
35 #define msg_no_hotkey(msg) do \
36 { \
37 if ((msg)->message == WM_HOTKEY) \
38 ok((msg)->message != WM_HOTKEY, \
39 "Got WM_HOTKEY with hwnd=%p, wParam=0x%Ix, lParam=0x%Ix\n", \
40 (msg)->hwnd, (msg)->wParam, (msg)->lParam); \
41 else \
42 ok(0, \
43 "Unexpected message %u posted to thread with hwnd=%p, wParam=0x%Ix, lParam=0x%Ix\n", \
44 (msg)->message, (msg)->hwnd, (msg)->wParam, (msg)->lParam); \
45 } while (0)
46 #define expect_no_hotkey() do \
47 { \
48 MSG msg; \
49 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) \
50 { \
51 msg_no_hotkey(&msg); \
52 DispatchMessageW(&msg); \
53 } \
54 } while (0)
55
56 START_TEST(RegisterHotKey)
57 {
58 SetCursorPos(0, 0);
59
60 RegisterHotKey(NULL, 1, MOD_CONTROL, 0);
61 RegisterHotKey(NULL, 2, MOD_CONTROL, 'U');
62 RegisterHotKey(NULL, 3, MOD_CONTROL | MOD_ALT, 0);
63 RegisterHotKey(NULL, 4, MOD_CONTROL | MOD_ALT, 'U');
64
65 expect_no_hotkey();
66
67 trace("Ctrl only\n");
68 keybd_event(VK_CONTROL, 0, 0, 0);
69 expect_no_hotkey();
70 keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
71 expect_hotkey(1, 0, VK_CONTROL);
72
73 trace("Ctrl+U\n");
74 keybd_event(VK_CONTROL, 0, 0, 0);
75 expect_no_hotkey();
76 keybd_event('U', 0, 0, 0);
77 expect_hotkey(2, MOD_CONTROL, 'U');
78 keybd_event('U', 0, KEYEVENTF_KEYUP, 0);
79 expect_no_hotkey();
80 keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
81 expect_no_hotkey();
82
83 trace("Ctrl+U (with Ctrl up first)\n");
84 keybd_event(VK_CONTROL, 0, 0, 0);
85 expect_no_hotkey();
86 keybd_event('U', 0, 0, 0);
87 expect_hotkey(2, MOD_CONTROL, 'U');
88 keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
89 expect_no_hotkey();
90 keybd_event('U', 0, KEYEVENTF_KEYUP, 0);
91 expect_no_hotkey();
92
93 trace("Ctrl+U (with U down first and Ctrl up first)\n");
94 keybd_event('U', 0, 0, 0);
95 expect_no_hotkey();
96 keybd_event(VK_CONTROL, 0, 0, 0);
97 expect_no_hotkey();
98 keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
99 expect_hotkey(1, 0, VK_CONTROL);
100 keybd_event('U', 0, KEYEVENTF_KEYUP, 0);
101 expect_no_hotkey();
102
103 trace("Ctrl+U (with U down first and U up first)\n");
104 keybd_event('U', 0, 0, 0);
105 expect_no_hotkey();
106 keybd_event(VK_CONTROL, 0, 0, 0);
107 expect_no_hotkey();
108 keybd_event('U', 0, KEYEVENTF_KEYUP, 0);
109 expect_no_hotkey();
110 keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
111 expect_no_hotkey();
112
113 trace("Ctrl+Alt\n");
114 keybd_event(VK_CONTROL, 0, 0, 0);
115 expect_no_hotkey();
116 keybd_event(VK_MENU, 0, 0, 0);
117 expect_no_hotkey();
118 keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
119 expect_hotkey(3, MOD_CONTROL, VK_MENU);
120 keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
121 expect_no_hotkey();
122
123 trace("Ctrl+Alt (with Ctrl up first)\n");
124 keybd_event(VK_CONTROL, 0, 0, 0);
125 expect_no_hotkey();
126 keybd_event(VK_MENU, 0, 0, 0);
127 expect_no_hotkey();
128 keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
129 expect_hotkey(3, MOD_ALT, VK_CONTROL);
130 keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
131 expect_no_hotkey();
132
133 trace("Alt+Ctrl\n");
134 keybd_event(VK_MENU, 0, 0, 0);
135 expect_no_hotkey();
136 keybd_event(VK_CONTROL, 0, 0, 0);
137 expect_no_hotkey();
138 keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
139 expect_hotkey(3, MOD_ALT, VK_CONTROL);
140 keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
141 expect_no_hotkey();
142
143 trace("Alt+Ctrl (with Alt up first)\n");
144 keybd_event(VK_MENU, 0, 0, 0);
145 expect_no_hotkey();
146 keybd_event(VK_CONTROL, 0, 0, 0);
147 expect_no_hotkey();
148 keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
149 expect_hotkey(3, MOD_CONTROL, VK_MENU);
150 keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
151 expect_no_hotkey();
152
153 trace("Alt+U\n");
154 keybd_event(VK_MENU, 0, 0, 0);
155 expect_no_hotkey();
156 keybd_event('U', 0, 0, 0);
157 expect_no_hotkey();
158 keybd_event('U', 0, KEYEVENTF_KEYUP, 0);
159 expect_no_hotkey();
160 keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
161 expect_no_hotkey();
162
163 trace("Ctrl+Alt+U\n");
164 keybd_event(VK_CONTROL, 0, 0, 0);
165 expect_no_hotkey();
166 keybd_event(VK_MENU, 0, 0, 0);
167 expect_no_hotkey();
168 keybd_event('U', 0, 0, 0);
169 expect_hotkey(4, MOD_CONTROL | MOD_ALT, 'U');
170 keybd_event('U', 0, KEYEVENTF_KEYUP, 0);
171 expect_no_hotkey();
172 keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
173 expect_no_hotkey();
174 keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
175 expect_no_hotkey();
176
177 trace("Alt+Ctrl+U\n");
178 keybd_event(VK_MENU, 0, 0, 0);
179 expect_no_hotkey();
180 keybd_event(VK_CONTROL, 0, 0, 0);
181 expect_no_hotkey();
182 keybd_event('U', 0, 0, 0);
183 expect_hotkey(4, MOD_CONTROL | MOD_ALT, 'U');
184 keybd_event('U', 0, KEYEVENTF_KEYUP, 0);
185 expect_no_hotkey();
186 keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
187 expect_no_hotkey();
188 keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
189 expect_no_hotkey();
190
191 trace("Ctrl+U+Alt\n");
192 keybd_event(VK_CONTROL, 0, 0, 0);
193 expect_no_hotkey();
194 keybd_event('U', 0, 0, 0);
195 expect_hotkey(2, MOD_CONTROL, 'U');
196 keybd_event(VK_MENU, 0, 0, 0);
197 expect_no_hotkey();
198 keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
199 expect_hotkey(3, MOD_CONTROL, VK_MENU);
200 keybd_event('U', 0, KEYEVENTF_KEYUP, 0);
201 expect_no_hotkey();
202 keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
203 expect_no_hotkey();
204
205 trace("Alt+U+Ctrl\n");
206 keybd_event(VK_MENU, 0, 0, 0);
207 expect_no_hotkey();
208 keybd_event('U', 0, 0, 0);
209 expect_no_hotkey();
210 keybd_event(VK_CONTROL, 0, 0, 0);
211 expect_no_hotkey();
212 keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
213 expect_hotkey(3, MOD_ALT, VK_CONTROL);
214 keybd_event('U', 0, KEYEVENTF_KEYUP, 0);
215 expect_no_hotkey();
216 keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
217 expect_no_hotkey();
218
219 /* The remaining combinations are an exercise left to the reader */
220
221 UnregisterHotKey(NULL, 4);
222 UnregisterHotKey(NULL, 3);
223 UnregisterHotKey(NULL, 2);
224 UnregisterHotKey(NULL, 1);
225
226 expect_no_hotkey();
227 }