urlmon: Don't create stgmed_obj for binding to object.
[wine] / dlls / user32 / tests / input.c
1 /* Test Key event to Key message translation
2  *
3  * Copyright 2003 Rein Klazes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 /* test whether the right type of messages:
21  * WM_KEYUP/DOWN vs WM_SYSKEYUP/DOWN  are sent in case of combined
22  * keystrokes.
23  *
24  * For instance <ALT>-X can be accompished by
25  * the sequence ALT-KEY-DOWN, X-KEY-DOWN, ALT-KEY-UP, X-KEY-UP
26  * but also X-KEY-DOWN, ALT-KEY-DOWN, X-KEY-UP, ALT-KEY-UP
27  * Whether a KEY or a SYSKEY message is sent is not always clear, it is
28  * also not the same in WINNT as in WIN9X */
29
30 /* NOTE that there will be test failures under WIN9X
31  * No applications are known to me that rely on this
32  * so I don't fix it */
33
34 /* TODO:
35  * 1. extend it to the wm_command and wm_syscommand notifications
36  * 2. add some more tests with special cases like dead keys or right (alt) key
37  * 3. there is some adapted code from input.c in here. Should really
38  *    make that code exactly the same.
39  * 4. resolve the win9x case when there is a need or the testing frame work
40  *    offers a nice way.
41  * 5. The test app creates a window, the user should not take the focus
42  *    away during its short existence. I could do something to prevent that
43  *    if it is a problem.
44  *
45  */
46
47 #define _WIN32_WINNT 0x401
48
49 #include <stdarg.h>
50 #include <assert.h>
51
52 #include "windef.h"
53 #include "winbase.h"
54 #include "winuser.h"
55
56 #include "wine/test.h"
57
58 /* globals */
59 static HWND hWndTest;
60 static long timetag = 0x10000000;
61
62 static UINT (WINAPI *pSendInput) (UINT, INPUT*, size_t);
63
64 #define MAXKEYEVENTS 6
65 #define MAXKEYMESSAGES MAXKEYEVENTS /* assuming a key event generates one
66                                        and only one message */
67
68 /* keyboard message names, sorted as their value */
69 static const char *MSGNAME[]={"WM_KEYDOWN", "WM_KEYUP", "WM_CHAR","WM_DEADCHAR",
70     "WM_SYSKEYDOWN", "WM_SYSKEYUP", "WM_SYSCHAR", "WM_SYSDEADCHAR" ,"WM_KEYLAST"};
71
72 /* keyevents, add more as needed */
73 typedef enum KEVtag
74 {  ALTDOWN = 1, ALTUP, XDOWN, XUP, SHIFTDOWN, SHIFTUP, CTRLDOWN, CTRLUP } KEV;
75 /* matching VK's */
76 static const int GETVKEY[]={0, VK_MENU, VK_MENU, 'X', 'X', VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL};
77 /* matching scan codes */
78 static const int GETSCAN[]={0, 0x38, 0x38, 0x2D, 0x2D, 0x2A, 0x2A, 0x1D, 0x1D };
79 /* matching updown events */
80 static const int GETUPDOWN[]={0, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP, 0, KEYEVENTF_KEYUP};
81 /* matching descripts */
82 static const char *getdesc[]={"", "+alt","-alt","+X","-X","+shift","-shift","+ctrl","-ctrl"};
83
84 /* The MSVC headers ignore our NONAMELESSUNION requests so we have to define our own type */
85 typedef struct
86 {
87     DWORD type;
88     union
89     {
90         MOUSEINPUT      mi;
91         KEYBDINPUT      ki;
92         HARDWAREINPUT   hi;
93     } u;
94 } TEST_INPUT;
95
96 #define ADDTOINPUTS(kev) \
97 inputs[evtctr].type = INPUT_KEYBOARD; \
98     ((TEST_INPUT*)inputs)[evtctr].u.ki.wVk = GETVKEY[ kev]; \
99     ((TEST_INPUT*)inputs)[evtctr].u.ki.wScan = GETSCAN[ kev]; \
100     ((TEST_INPUT*)inputs)[evtctr].u.ki.dwFlags = GETUPDOWN[ kev]; \
101     ((TEST_INPUT*)inputs)[evtctr].u.ki.dwExtraInfo = 0; \
102     ((TEST_INPUT*)inputs)[evtctr].u.ki.time = ++timetag; \
103     if( kev) evtctr++;
104
105 typedef struct {
106     UINT    message;
107     WPARAM  wParam;
108     LPARAM  lParam;
109 } KMSG;
110
111 /*******************************************
112  * add new test sets here
113  * the software will make all combinations of the
114  * keyevent defined here
115  */
116 static const struct {
117     int nrkev;
118     KEV keydwn[MAXKEYEVENTS];
119     KEV keyup[MAXKEYEVENTS];
120 } testkeyset[]= {
121     { 2, { ALTDOWN, XDOWN }, { ALTUP, XUP}},
122     { 3, { ALTDOWN, XDOWN , SHIFTDOWN}, { ALTUP, XUP, SHIFTUP}},
123     { 3, { ALTDOWN, XDOWN , CTRLDOWN}, { ALTUP, XUP, CTRLUP}},
124     { 3, { SHIFTDOWN, XDOWN , CTRLDOWN}, { SHIFTUP, XUP, CTRLUP}},
125     { 0 } /* mark the end */
126 };
127
128 /**********************adapted from input.c **********************************/
129
130 static BYTE InputKeyStateTable[256];
131 static BYTE AsyncKeyStateTable[256];
132 static BYTE TrackSysKey = 0; /* determine whether ALT key up will cause a WM_SYSKEYUP
133                          or a WM_KEYUP message */
134 typedef union
135 {
136     struct
137     {
138         unsigned long count : 16;
139         unsigned long code : 8;
140         unsigned long extended : 1;
141         unsigned long unused : 2;
142         unsigned long win_internal : 2;
143         unsigned long context : 1;
144         unsigned long previous : 1;
145         unsigned long transition : 1;
146     } lp1;
147     unsigned long lp2;
148 } KEYLP;
149
150 static void init_function_pointers(void)
151 {
152     HMODULE hdll = GetModuleHandleA("user32");
153
154 #define GET_PROC(func) \
155     p ## func = (void*)GetProcAddress(hdll, #func); \
156     if(!p ## func) \
157       trace("GetProcAddress(%s) failed\n", #func);
158
159     GET_PROC(SendInput)
160
161 #undef GET_PROC
162 }
163
164 static int KbdMessage( KEV kev, WPARAM *pwParam, LPARAM *plParam )
165 {
166     UINT message;
167     int VKey = GETVKEY[kev];
168     KEYLP keylp;
169
170     keylp.lp2 = 0;
171
172     keylp.lp1.count = 1;
173     keylp.lp1.code = GETSCAN[kev];
174     keylp.lp1.extended = 0 ;/*  FIXME (ki->dwFlags & KEYEVENTF_EXTENDEDKEY) != 0; */
175     keylp.lp1.win_internal = 0;
176
177     if (GETUPDOWN[kev] & KEYEVENTF_KEYUP )
178     {
179         message = WM_KEYUP;
180         if( (InputKeyStateTable[VK_MENU] & 0x80) && (
181                 (VKey == VK_MENU) || (VKey == VK_CONTROL) ||
182                  !(InputKeyStateTable[VK_CONTROL] & 0x80))) {
183             if(  TrackSysKey == VK_MENU || /* <ALT>-down/<ALT>-up sequence */
184                     (VKey != VK_MENU)) /* <ALT>-down...<something else>-up */
185                 message = WM_SYSKEYUP;
186                 TrackSysKey = 0;
187         }
188         InputKeyStateTable[VKey] &= ~0x80;
189         keylp.lp1.previous = 1;
190         keylp.lp1.transition = 1;
191     }
192     else
193     {
194         keylp.lp1.previous = (InputKeyStateTable[VKey] & 0x80) != 0;
195         keylp.lp1.transition = 0;
196         if (!(InputKeyStateTable[VKey] & 0x80)) InputKeyStateTable[VKey] ^= 0x01;
197         InputKeyStateTable[VKey] |= 0x80;
198         AsyncKeyStateTable[VKey] |= 0x80;
199
200         message = WM_KEYDOWN;
201         if( (InputKeyStateTable[VK_MENU] & 0x80) &&
202                 !(InputKeyStateTable[VK_CONTROL] & 0x80)) {
203             message = WM_SYSKEYDOWN;
204             TrackSysKey = VKey;
205         }
206     }
207
208     keylp.lp1.context = (InputKeyStateTable[VK_MENU] & 0x80) != 0; /* 1 if alt */
209
210     if( plParam) *plParam = keylp.lp2;
211     if( pwParam) *pwParam = VKey;
212     return message;
213 }
214
215 /****************************** end copy input.c ****************************/
216
217 /*
218  * . prepare the keyevents for SendInputs
219  * . calculate the "expected" messages
220  * . Send the events to our window
221  * . retrieve the messages from the input queue
222  * . verify
223  */
224 static void do_test( HWND hwnd, int seqnr, const KEV td[] )
225 {
226     INPUT inputs[MAXKEYEVENTS];
227     KMSG expmsg[MAXKEYEVENTS];
228     MSG msg;
229     char buf[100];
230     UINT evtctr=0;
231     int kmctr, i;
232
233     buf[0]='\0';
234     TrackSysKey=0; /* see input.c */
235     for( i = 0; i < MAXKEYEVENTS; i++) {
236         ADDTOINPUTS(td[i])
237         strcat(buf, getdesc[td[i]]);
238         if(td[i])
239             expmsg[i].message = KbdMessage(td[i], &(expmsg[i].wParam), &(expmsg[i].lParam)); /* see queue_kbd_event() */
240         else
241             expmsg[i].message = 0;
242     }
243     for( kmctr = 0; kmctr < MAXKEYEVENTS && expmsg[kmctr].message; kmctr++)
244         ;
245     assert( evtctr <= MAXKEYEVENTS );
246     assert( evtctr == pSendInput(evtctr, &inputs[0], sizeof(INPUT)));
247     i = 0;
248     if (winetest_debug > 1)
249         trace("======== key stroke sequence #%d: %s =============\n",
250             seqnr + 1, buf);
251     while( PeekMessage(&msg,hwnd,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE) ) {
252         if (winetest_debug > 1)
253             trace("message[%d] %-15s wParam %04lx lParam %08lx time %x\n", i,
254                   MSGNAME[msg.message - WM_KEYFIRST], msg.wParam, msg.lParam, msg.time);
255         if( i < kmctr ) {
256             ok( msg.message == expmsg[i].message &&
257                     msg.wParam == expmsg[i].wParam &&
258                     msg.lParam == expmsg[i].lParam,
259                     "wrong message! expected:\n"
260                     "message[%d] %-15s wParam %04lx lParam %08lx\n",i,
261                     MSGNAME[(expmsg[i]).message - WM_KEYFIRST],
262                     expmsg[i].wParam, expmsg[i].lParam );
263         }
264         i++;
265     }
266     if (winetest_debug > 1)
267         trace("%d messages retrieved\n", i);
268     ok( i == kmctr, "message count is wrong: got %d expected: %d\n", i, kmctr);
269 }
270
271 /* test all combinations of the specified key events */
272 static void TestASet( HWND hWnd, int nrkev, const KEV kevdwn[], const KEV kevup[] )
273 {
274     int i,j,k,l,m,n;
275     static int count=0;
276     KEV kbuf[MAXKEYEVENTS];
277     assert( nrkev==2 || nrkev==3);
278     for(i=0;i<MAXKEYEVENTS;i++) kbuf[i]=0;
279     /* two keys involved gives 4 test cases */
280     if(nrkev==2) {
281         for(i=0;i<nrkev;i++) {
282             for(j=0;j<nrkev;j++) {
283                 kbuf[0] = kevdwn[i];
284                 kbuf[1] = kevdwn[1-i];
285                 kbuf[2] = kevup[j];
286                 kbuf[3] = kevup[1-j];
287                 do_test( hWnd, count++, kbuf);
288             }
289         }
290     }
291     /* three keys involved gives 36 test cases */
292     if(nrkev==3){
293         for(i=0;i<nrkev;i++){
294             for(j=0;j<nrkev;j++){
295                 if(j==i) continue;
296                 for(k=0;k<nrkev;k++){
297                     if(k==i || k==j) continue;
298                     for(l=0;l<nrkev;l++){
299                         for(m=0;m<nrkev;m++){
300                             if(m==l) continue;
301                             for(n=0;n<nrkev;n++){
302                                 if(n==l ||n==m) continue;
303                                 kbuf[0] = kevdwn[i];
304                                 kbuf[1] = kevdwn[j];
305                                 kbuf[2] = kevdwn[k];
306                                 kbuf[3] = kevup[l];
307                                 kbuf[4] = kevup[m];
308                                 kbuf[5] = kevup[n];
309                                 do_test( hWnd, count++, kbuf);
310                             }
311                         }
312                     }
313                 }
314             }
315         }
316     }
317 }
318
319 /* test each set specified in the global testkeyset array */
320 static void TestSysKeys( HWND hWnd)
321 {
322     int i;
323     for(i=0; testkeyset[i].nrkev;i++)
324         TestASet( hWnd, testkeyset[i].nrkev, testkeyset[i].keydwn,
325                 testkeyset[i].keyup);
326 }
327
328 static LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam,
329         LPARAM lParam )
330 {
331     switch (msg) {
332         case WM_USER:
333             SetFocus(hWnd);
334             /* window has focus, now do the test */
335             if( hWnd == hWndTest) TestSysKeys( hWnd);
336             /* finished :-) */
337             break;
338
339         case WM_DESTROY:
340             PostQuitMessage( 0 );
341             break;
342
343         default:
344             return( DefWindowProcA( hWnd, msg, wParam, lParam ) );
345     }
346
347     return 0;
348 }
349
350 static void test_Input_whitebox(void)
351 {
352     MSG msg;
353     WNDCLASSA  wclass;
354     HANDLE hInstance = GetModuleHandleA( NULL );
355
356     wclass.lpszClassName = "InputSysKeyTestClass";
357     wclass.style         = CS_HREDRAW | CS_VREDRAW;
358     wclass.lpfnWndProc   = WndProc;
359     wclass.hInstance     = hInstance;
360     wclass.hIcon         = LoadIconA( 0, (LPSTR)IDI_APPLICATION );
361     wclass.hCursor       = LoadCursorA( NULL, IDC_ARROW);
362     wclass.hbrBackground = (HBRUSH)( COLOR_WINDOW + 1);
363     wclass.lpszMenuName = 0;
364     wclass.cbClsExtra    = 0;
365     wclass.cbWndExtra    = 0;
366     assert (RegisterClassA( &wclass ));
367     /* create the test window that will receive the keystrokes */
368     assert ( hWndTest = CreateWindowA( wclass.lpszClassName, "InputSysKeyTest",
369                 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 100, 100,
370                 NULL, NULL, hInstance, NULL) );
371     ShowWindow( hWndTest, SW_SHOW);
372     UpdateWindow( hWndTest);
373
374     /* flush pending messages */
375     while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
376
377     SendMessageA(hWndTest, WM_USER, 0, 0);
378     DestroyWindow(hWndTest);
379 }
380
381 static void empty_message_queue(void) {
382     MSG msg;
383     while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
384         TranslateMessage(&msg);
385         DispatchMessage(&msg);
386     }
387 }
388
389 struct transition_s {
390     WORD wVk;
391     BYTE before_state;
392     BOOL _todo_wine;
393 };
394
395 struct sendinput_test_s {
396     WORD wVk;
397     DWORD dwFlags;
398     struct transition_s expected_transitions[MAXKEYEVENTS+1];
399     UINT expected_messages[MAXKEYMESSAGES+1];
400 } sendinput_test[] = {
401     /* test ALT+F */
402     {VK_LMENU, 0, {{VK_MENU, 0x00, 0}, {VK_LMENU, 0x00, 0}, {0}},
403         {WM_SYSKEYDOWN, 0}},
404     {'F', 0, {{'F', 0x00, 0}, {0}},
405         {WM_SYSKEYDOWN, WM_SYSCHAR, WM_SYSCOMMAND, 0}},
406     {'F', KEYEVENTF_KEYUP, {{'F', 0x80, 0}, {0}}, {WM_SYSKEYUP, 0}},
407     {VK_LMENU, KEYEVENTF_KEYUP, {{VK_MENU, 0x80, 0}, {VK_LMENU, 0x80, 0}, {0}},
408         {WM_KEYUP, 0}},
409
410     /* test CTRL+O */
411     {VK_LCONTROL, 0, {{VK_CONTROL, 0x00, 0}, {VK_LCONTROL, 0x00, 0}, {0}},
412         {WM_KEYDOWN, 0}},
413     {'O', 0, {{'O', 0x00, 0}, {0}}, {WM_KEYDOWN, WM_CHAR, 0}},
414     {'O', KEYEVENTF_KEYUP, {{'O', 0x80, 0}, {0}}, {WM_KEYUP, 0}},
415     {VK_LCONTROL, KEYEVENTF_KEYUP,
416         {{VK_CONTROL, 0x80, 0}, {VK_LCONTROL, 0x80, 0}, {0}}, {WM_KEYUP, 0}},
417
418     /* test ALT+CTRL+X */
419     {VK_LMENU, 0, {{VK_MENU, 0x00, 0}, {VK_LMENU, 0x00, 0}, {0}},
420         {WM_SYSKEYDOWN, 0}},
421     {VK_LCONTROL, 0, {{VK_CONTROL, 0x00, 0}, {VK_LCONTROL, 0x00, 0}, {0}},
422         {WM_KEYDOWN, 0}},
423     {'X', 0, {{'X', 0x00, 0}, {0}}, {WM_KEYDOWN, 0}},
424     {'X', KEYEVENTF_KEYUP, {{'X', 0x80, 0}, {0}}, {WM_KEYUP, 0}},
425     {VK_LCONTROL, KEYEVENTF_KEYUP,
426         {{VK_CONTROL, 0x80, 0}, {VK_LCONTROL, 0x80, 0}, {0}},
427         {WM_SYSKEYUP, 0}},
428     {VK_LMENU, KEYEVENTF_KEYUP, {{VK_MENU, 0x80, 0}, {VK_LMENU, 0x80, 0}, {0}},
429         {WM_KEYUP, 0}},
430
431     /* test SHIFT+A */
432     {VK_LSHIFT, 0,
433         {{VK_SHIFT, 0x00, 0}, {VK_LSHIFT, 0x00, 0}, {0}}, {WM_KEYDOWN, 0}},
434     {'A', 0, {{'A', 0x00, 0}, {0}}, {WM_KEYDOWN, WM_CHAR, 0}},
435     {'A', KEYEVENTF_KEYUP, {{'A', 0x80, 0}, {0}}, {WM_KEYUP, 0}},
436     {VK_LSHIFT, KEYEVENTF_KEYUP,
437         {{VK_SHIFT, 0x80, 0}, {VK_LSHIFT, 0x80, 0}, {0}}, {WM_KEYUP, 0}},
438
439     {0, 0, {{0}}, {0}} /* end */
440 };
441
442 static struct sendinput_test_s *pTest = sendinput_test;
443 static UINT *pMsg = sendinput_test[0].expected_messages;
444
445 /* Verify that only specified key state transitions occur */
446 static void compare_and_check(int id, BYTE *ks1, BYTE *ks2, struct transition_s *t) {
447     int i;
448     while (t->wVk) {
449         int matched = ((ks1[t->wVk]&0x80) == (t->before_state&0x80)
450                        && (ks2[t->wVk]&0x80) == (~t->before_state&0x80));
451         if (t->_todo_wine) {
452             todo_wine {
453                 ok(matched, "%02d: %02x from %02x -> %02x "
454                    "instead of %02x -> %02x\n", id, t->wVk,
455                    ks1[t->wVk]&0x80, ks2[t->wVk]&0x80, t->before_state,
456                    ~t->before_state&0x80);
457             }
458         } else {
459             ok(matched, "%02d: %02x from %02x -> %02x "
460                "instead of %02x -> %02x\n", id, t->wVk,
461                ks1[t->wVk]&0x80, ks2[t->wVk]&0x80, t->before_state,
462                ~t->before_state&0x80);
463         }
464         ks2[t->wVk] = ks1[t->wVk]; /* clear the match */
465         t++;
466     }
467     for (i = 0; i < 256; i++)
468         ok(ks2[i] == ks1[i], "%02d: %02x from %02x -> %02x unexpected\n",
469            id, i, ks1[i], ks2[i]);
470 }
471
472 /* WndProc2 checks that we get at least the messages specified */
473 static LRESULT CALLBACK WndProc2(HWND hWnd, UINT Msg, WPARAM wParam,
474                                    LPARAM lParam)
475 {
476     if (pTest->wVk != 0) { /* not end */
477         while(pTest->wVk != 0 && *pMsg == 0) {
478             pTest++;
479             pMsg = pTest->expected_messages;
480         }
481         if (Msg == *pMsg)
482             pMsg++;
483     }
484     return DefWindowProc(hWnd, Msg, wParam, lParam);
485 }
486
487 static void test_Input_blackbox(void)
488 {
489     TEST_INPUT i;
490     int ii;
491     BYTE ks1[256], ks2[256];
492     LONG_PTR prevWndProc;
493     HWND window;
494
495     if (!pSendInput)
496     {
497         skip("SendInput is not available\n");
498         return;
499     }
500
501     window = CreateWindow("Static", NULL, WS_POPUP|WS_HSCROLL|WS_VSCROLL
502         |WS_VISIBLE, 0, 0, 200, 60, NULL, NULL,
503         NULL, NULL);
504     ok(window != NULL, "error: %d\n", (int) GetLastError());
505
506     /* must process all initial messages, otherwise X11DRV_KeymapNotify unsets
507      * key state set by SendInput(). */
508     empty_message_queue();
509
510     prevWndProc = SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR) WndProc2);
511     ok(prevWndProc != 0 || (prevWndProc == 0 && GetLastError() == 0),
512        "error: %d\n", (int) GetLastError());
513
514     i.type = INPUT_KEYBOARD;
515     i.u.ki.wScan = 0;
516     i.u.ki.time = 0;
517     i.u.ki.dwExtraInfo = 0;
518
519     for (ii = 0; ii < sizeof(sendinput_test)/sizeof(struct sendinput_test_s)-1;
520          ii++) {
521         GetKeyboardState(ks1);
522         i.u.ki.dwFlags = sendinput_test[ii].dwFlags;
523         i.u.ki.wVk = sendinput_test[ii].wVk;
524         pSendInput(1, (INPUT*)&i, sizeof(TEST_INPUT));
525         empty_message_queue();
526         GetKeyboardState(ks2);
527         compare_and_check(ii, ks1, ks2,
528                           sendinput_test[ii].expected_transitions);
529     }
530
531     /* *pMsg should be 0 and (++pTest)->wVk should be 0 */
532     if (pTest->wVk && *pMsg == 0) pTest++;
533     while(pTest->wVk && pTest->expected_messages[0] == 0) {
534         ++pTest;
535     }
536     ok(*pMsg == 0 && pTest->wVk == 0,
537        "not enough messages found; looking for %x\n", *pMsg);
538     DestroyWindow(window);
539 }
540
541 static void test_keynames(void)
542 {
543     int i, len;
544     char buff[256];
545
546     for (i = 0; i < 512; i++)
547     {
548         strcpy(buff, "----");
549         len = GetKeyNameTextA(i << 16, buff, sizeof(buff));
550         ok(len || !buff[0], "%d: Buffer is not zeroed\n", i);
551     }
552 }
553
554 static POINT pt_old, pt_new;
555 static BOOL clipped;
556 #define STEP 20
557
558 static LRESULT CALLBACK hook_proc1( int code, WPARAM wparam, LPARAM lparam )
559 {
560     MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
561     POINT pt, pt1;
562
563     if (code == HC_ACTION)
564     {
565         /* This is our new cursor position */
566         pt_new = hook->pt;
567         /* Should return previous position */
568         GetCursorPos(&pt);
569         ok(pt.x == pt_old.x && pt.y == pt_old.y, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
570
571         /* Should set new position until hook chain is finished. */
572         pt.x = pt_old.x + STEP;
573         pt.y = pt_old.y + STEP;
574         SetCursorPos(pt.x, pt.y);
575         GetCursorPos(&pt1);
576         if (clipped)
577             ok(pt1.x == pt_old.x && pt1.y == pt_old.y, "Wrong set pos: (%d,%d)\n", pt1.x, pt1.y);
578         else
579             ok(pt1.x == pt.x && pt1.y == pt.y, "Wrong set pos: (%d,%d)\n", pt1.x, pt1.y);
580     }
581     return CallNextHookEx( 0, code, wparam, lparam );
582 }
583
584 static LRESULT CALLBACK hook_proc2( int code, WPARAM wparam, LPARAM lparam )
585 {
586     MSLLHOOKSTRUCT *hook = (MSLLHOOKSTRUCT *)lparam;
587     POINT pt;
588
589     if (code == HC_ACTION)
590     {
591         ok(hook->pt.x == pt_new.x && hook->pt.y == pt_new.y,
592            "Wrong hook coords: (%d %d) != (%d,%d)\n", hook->pt.x, hook->pt.y, pt_new.x, pt_new.y);
593
594         /* Should match position set above */
595         GetCursorPos(&pt);
596         if (clipped)
597             ok(pt.x == pt_old.x && pt.y == pt_old.y, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
598         else
599             ok(pt.x == pt_old.x +STEP && pt.y == pt_old.y +STEP, "GetCursorPos: (%d,%d)\n", pt.x, pt.y);
600     }
601     return CallNextHookEx( 0, code, wparam, lparam );
602 }
603
604 static void test_mouse_ll_hook(void)
605 {
606     HWND hwnd;
607     HHOOK hook1, hook2;
608     POINT pt_org, pt;
609     RECT rc;
610
611     GetCursorPos(&pt_org);
612     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
613                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
614     SetCursorPos(100, 100);
615
616     hook2 = SetWindowsHookExA(WH_MOUSE_LL, hook_proc2, GetModuleHandleA(0), 0);
617     hook1 = SetWindowsHookExA(WH_MOUSE_LL, hook_proc1, GetModuleHandleA(0), 0);
618
619     GetCursorPos(&pt_old);
620     mouse_event(MOUSEEVENTF_MOVE, -STEP,  0, 0, 0);
621     GetCursorPos(&pt_old);
622     ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
623     mouse_event(MOUSEEVENTF_MOVE, +STEP,  0, 0, 0);
624     GetCursorPos(&pt_old);
625     ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
626     mouse_event(MOUSEEVENTF_MOVE,  0, -STEP, 0, 0);
627     GetCursorPos(&pt_old);
628     ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
629     mouse_event(MOUSEEVENTF_MOVE,  0, +STEP, 0, 0);
630     GetCursorPos(&pt_old);
631     ok(pt_old.x == pt_new.x && pt_old.y == pt_new.y, "Wrong new pos: (%d,%d)\n", pt_old.x, pt_old.y);
632
633     SetRect(&rc, 50, 50, 151, 151);
634     ClipCursor(&rc);
635     clipped = TRUE;
636
637     SetCursorPos(40, 40);
638     GetCursorPos(&pt_old);
639     ok(pt_old.x == 50 && pt_old.y == 50, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
640     SetCursorPos(160, 160);
641     GetCursorPos(&pt_old);
642     ok(pt_old.x == 150 && pt_old.y == 150, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
643     mouse_event(MOUSEEVENTF_MOVE, +STEP, +STEP, 0, 0);
644     GetCursorPos(&pt_old);
645     ok(pt_old.x == 150 && pt_old.y == 150, "Wrong new pos: (%d,%d)\n", pt_new.x, pt_new.y);
646
647     clipped = FALSE;
648     pt_new.x = pt_new.y = 150;
649     ClipCursor(NULL);
650     UnhookWindowsHookEx(hook1);
651
652     /* Now check that mouse buttons do not change mouse position
653        if we don't have MOUSEEVENTF_MOVE flag specified. */
654
655     /* We reusing the same hook callback, so make it happy */
656     pt_old.x = pt_new.x - STEP;
657     pt_old.y = pt_new.y - STEP;
658     mouse_event(MOUSEEVENTF_LEFTUP, 123, 456, 0, 0);
659     GetCursorPos(&pt);
660     ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
661     mouse_event(MOUSEEVENTF_RIGHTUP, 456, 123, 0, 0);
662     GetCursorPos(&pt);
663     ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
664
665     mouse_event(MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE, 123, 456, 0, 0);
666     GetCursorPos(&pt);
667     ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
668     mouse_event(MOUSEEVENTF_RIGHTUP | MOUSEEVENTF_ABSOLUTE, 456, 123, 0, 0);
669     GetCursorPos(&pt);
670     ok(pt.x == pt_new.x && pt.y == pt_new.y, "Position changed: (%d,%d)\n", pt.x, pt.y);
671
672     UnhookWindowsHookEx(hook2);
673     DestroyWindow(hwnd);
674     SetCursorPos(pt_org.x, pt_org.y);
675 }
676
677 START_TEST(input)
678 {
679     init_function_pointers();
680
681     if (!pSendInput)
682         skip("SendInput is not available\n");
683     else
684         test_Input_whitebox();
685
686     test_Input_blackbox();
687     test_keynames();
688     test_mouse_ll_hook();
689 }