d3dcompiler: Add argument check in D3DReflect().
[wine] / dlls / imm32 / tests / imm32.c
1 /*
2  * Unit tests for imm32
3  *
4  * Copyright (c) 2008 Michael Jung
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdio.h>
22
23 #include "wine/test.h"
24 #include "winuser.h"
25 #include "wingdi.h"
26 #include "imm.h"
27
28 #define NUMELEMS(array) (sizeof((array))/sizeof((array)[0]))
29
30 static BOOL (WINAPI *pImmAssociateContextEx)(HWND,HIMC,DWORD);
31
32 /*
33  * msgspy - record and analyse message traces sent to a certain window
34  */
35 static struct _msg_spy {
36     HWND         hwnd;
37     HHOOK        get_msg_hook;
38     HHOOK        call_wnd_proc_hook;
39     CWPSTRUCT    msgs[32];
40     unsigned int i_msg;
41 } msg_spy;
42
43 static LRESULT CALLBACK get_msg_filter(int nCode, WPARAM wParam, LPARAM lParam)
44 {
45     if (HC_ACTION == nCode) {
46         MSG *msg = (MSG*)lParam;
47
48         if ((msg->hwnd == msg_spy.hwnd) &&
49             (msg_spy.i_msg < NUMELEMS(msg_spy.msgs)))
50         {
51             msg_spy.msgs[msg_spy.i_msg].hwnd    = msg->hwnd;
52             msg_spy.msgs[msg_spy.i_msg].message = msg->message;
53             msg_spy.msgs[msg_spy.i_msg].wParam  = msg->wParam;
54             msg_spy.msgs[msg_spy.i_msg].lParam  = msg->lParam;
55             msg_spy.i_msg++;
56         }
57     }
58
59     return CallNextHookEx(msg_spy.get_msg_hook, nCode, wParam, lParam);
60 }
61
62 static LRESULT CALLBACK call_wnd_proc_filter(int nCode, WPARAM wParam,
63                                              LPARAM lParam)
64 {
65     if (HC_ACTION == nCode) {
66         CWPSTRUCT *cwp = (CWPSTRUCT*)lParam;
67
68         if ((cwp->hwnd == msg_spy.hwnd) &&
69             (msg_spy.i_msg < NUMELEMS(msg_spy.msgs)))
70         {
71             memcpy(&msg_spy.msgs[msg_spy.i_msg], cwp, sizeof(msg_spy.msgs[0]));
72             msg_spy.i_msg++;
73         }
74     }
75
76     return CallNextHookEx(msg_spy.call_wnd_proc_hook, nCode, wParam, lParam);
77 }
78
79 static void msg_spy_pump_msg_queue(void) {
80     MSG msg;
81
82     while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
83         TranslateMessage(&msg);
84         DispatchMessage(&msg);
85     }
86
87     return;
88 }
89
90 static void msg_spy_flush_msgs(void) {
91     msg_spy_pump_msg_queue();
92     msg_spy.i_msg = 0;
93 }
94
95 static CWPSTRUCT* msg_spy_find_msg(UINT message) {
96     UINT i;
97
98     msg_spy_pump_msg_queue();
99
100     if (msg_spy.i_msg >= NUMELEMS(msg_spy.msgs))
101         fprintf(stdout, "%s:%d: msg_spy: message buffer overflow!\n",
102                 __FILE__, __LINE__);
103
104     for (i = 0; i < msg_spy.i_msg; i++)
105         if (msg_spy.msgs[i].message == message)
106             return &msg_spy.msgs[i];
107
108     return NULL;
109 }
110
111 static void msg_spy_init(HWND hwnd) {
112     msg_spy.hwnd = hwnd;
113     msg_spy.get_msg_hook =
114             SetWindowsHookEx(WH_GETMESSAGE, get_msg_filter, GetModuleHandle(0),
115                              GetCurrentThreadId());
116     msg_spy.call_wnd_proc_hook =
117             SetWindowsHookEx(WH_CALLWNDPROC, call_wnd_proc_filter,
118                              GetModuleHandle(0), GetCurrentThreadId());
119     msg_spy.i_msg = 0;
120
121     msg_spy_flush_msgs();
122 }
123
124 static void msg_spy_cleanup(void) {
125     if (msg_spy.get_msg_hook)
126         UnhookWindowsHookEx(msg_spy.get_msg_hook);
127     if (msg_spy.call_wnd_proc_hook)
128         UnhookWindowsHookEx(msg_spy.call_wnd_proc_hook);
129     memset(&msg_spy, 0, sizeof(msg_spy));
130 }
131
132 /*
133  * imm32 test cases - Issue some IMM commands on a dummy window and analyse the
134  * messages being sent to this window in response.
135  */
136 static const char wndcls[] = "winetest_imm32_wndcls";
137 static HWND hwnd;
138
139 static LRESULT WINAPI wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
140 {
141     switch (msg)
142     {
143         case WM_IME_SETCONTEXT:
144         case WM_NCCREATE:
145         case WM_CREATE:
146             return TRUE;
147     }
148
149     return DefWindowProcA(hwnd,msg,wParam,lParam);
150 }
151
152 static BOOL init(void) {
153     WNDCLASSEX wc;
154     HIMC imc;
155     HMODULE hmod;
156
157     hmod = GetModuleHandleA("imm32.dll");
158     pImmAssociateContextEx = (void*)GetProcAddress(hmod, "ImmAssociateContextEx");
159
160     wc.cbSize        = sizeof(WNDCLASSEX);
161     wc.style         = 0;
162     wc.lpfnWndProc   = wndProc;
163     wc.cbClsExtra    = 0;
164     wc.cbWndExtra    = 0;
165     wc.hInstance     = GetModuleHandle(0);
166     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
167     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
168     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
169     wc.lpszMenuName  = NULL;
170     wc.lpszClassName = wndcls;
171     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
172
173     if (!RegisterClassExA(&wc))
174         return FALSE;
175
176     hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, wndcls, "Wine imm32.dll test",
177                           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
178                           240, 120, NULL, NULL, GetModuleHandle(0), NULL);
179     if (!hwnd)
180         return FALSE;
181
182     imc = ImmGetContext(hwnd);
183     if (!imc)
184     {
185         win_skip("IME support not implemented\n");
186         return FALSE;
187     }
188     ImmReleaseContext(hwnd, imc);
189
190     ShowWindow(hwnd, SW_SHOWNORMAL);
191     UpdateWindow(hwnd);
192
193     msg_spy_init(hwnd);
194
195     return TRUE;
196 }
197
198 static void cleanup(void) {
199     msg_spy_cleanup();
200     if (hwnd)
201         DestroyWindow(hwnd);
202     UnregisterClass(wndcls, GetModuleHandle(0));
203 }
204
205 static void test_ImmNotifyIME(void) {
206     static const char string[] = "wine";
207     char resstr[16] = "";
208     HIMC imc;
209     BOOL ret;
210
211     imc = ImmGetContext(hwnd);
212     msg_spy_flush_msgs();
213
214     ret = ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
215     ok(broken(!ret) ||
216        ret, /* Vista+ */
217        "Canceling an empty composition string should succeed.\n");
218     ok(!msg_spy_find_msg(WM_IME_COMPOSITION), "Windows does not post "
219        "WM_IME_COMPOSITION in response to NI_COMPOSITIONSTR / CPS_CANCEL, if "
220        "the composition string being canceled is empty.\n");
221
222     ImmSetCompositionString(imc, SCS_SETSTR, string, sizeof(string), NULL, 0);
223     msg_spy_flush_msgs();
224
225     ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
226     msg_spy_flush_msgs();
227
228     /* behavior differs between win9x and NT */
229     ret = ImmGetCompositionString(imc, GCS_COMPSTR, resstr, sizeof(resstr));
230     ok(!ret, "After being cancelled the composition string is empty.\n");
231
232     msg_spy_flush_msgs();
233
234     ret = ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
235     ok(broken(!ret) ||
236        ret, /* Vista+ */
237        "Canceling an empty composition string should succeed.\n");
238     ok(!msg_spy_find_msg(WM_IME_COMPOSITION), "Windows does not post "
239        "WM_IME_COMPOSITION in response to NI_COMPOSITIONSTR / CPS_CANCEL, if "
240        "the composition string being canceled is empty.\n");
241
242     msg_spy_flush_msgs();
243     ImmReleaseContext(hwnd, imc);
244 }
245
246 static void test_ImmGetCompositionString(void)
247 {
248     HIMC imc;
249     static const WCHAR string[] = {'w','i','n','e',0x65e5,0x672c,0x8a9e};
250     char cstring[20];
251     WCHAR wstring[20];
252     DWORD len;
253     DWORD alen,wlen;
254
255     imc = ImmGetContext(hwnd);
256     ImmSetCompositionStringW(imc, SCS_SETSTR, string, sizeof(string), NULL,0);
257     alen = ImmGetCompositionStringA(imc, GCS_COMPSTR, cstring, 20);
258     wlen = ImmGetCompositionStringW(imc, GCS_COMPSTR, wstring, 20);
259     /* windows machines without any IME installed just return 0 above */
260     if( alen && wlen)
261     {
262         len = ImmGetCompositionStringW(imc, GCS_COMPATTR, NULL, 0);
263         ok(len*sizeof(WCHAR)==wlen,"GCS_COMPATTR(W) not returning correct count\n");
264         len = ImmGetCompositionStringA(imc, GCS_COMPATTR, NULL, 0);
265         ok(len==alen,"GCS_COMPATTR(A) not returning correct count\n");
266     }
267     ImmReleaseContext(hwnd, imc);
268 }
269
270 static void test_ImmSetCompositionString(void)
271 {
272     HIMC imc;
273     BOOL ret;
274
275     SetLastError(0xdeadbeef);
276     imc = ImmGetContext(hwnd);
277     ok(imc != 0, "ImmGetContext() failed. Last error: %u\n", GetLastError());
278     if (!imc)
279         return;
280
281     ret = ImmSetCompositionStringW(imc, SCS_SETSTR, NULL, 0, NULL, 0);
282     ok(broken(!ret) ||
283        ret, /* Vista+ */
284        "ImmSetCompositionStringW() failed.\n");
285
286     ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGEATTR,
287         NULL, 0, NULL, 0);
288     ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
289
290     ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGECLAUSE,
291         NULL, 0, NULL, 0);
292     ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
293
294     ret = ImmSetCompositionStringW(imc, SCS_CHANGEATTR | SCS_CHANGECLAUSE,
295         NULL, 0, NULL, 0);
296     ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
297
298     ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGEATTR | SCS_CHANGECLAUSE,
299         NULL, 0, NULL, 0);
300     ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
301
302     ImmReleaseContext(hwnd, imc);
303 }
304
305 static void test_ImmIME(void)
306 {
307     HIMC imc;
308
309     imc = ImmGetContext(hwnd);
310     if (imc)
311     {
312         BOOL rc;
313         rc = ImmConfigureIMEA(imc, NULL, IME_CONFIG_REGISTERWORD, NULL);
314         ok (rc == 0, "ImmConfigureIMEA did not fail\n");
315         rc = ImmConfigureIMEW(imc, NULL, IME_CONFIG_REGISTERWORD, NULL);
316         ok (rc == 0, "ImmConfigureIMEW did not fail\n");
317     }
318     ImmReleaseContext(hwnd,imc);
319 }
320
321 static void test_ImmAssociateContextEx(void)
322 {
323     HIMC imc;
324     BOOL rc;
325
326     if (!pImmAssociateContextEx) return;
327
328     imc = ImmGetContext(hwnd);
329     if (imc)
330     {
331         HIMC retimc, newimc;
332
333         newimc = ImmCreateContext();
334         ok(newimc != imc, "handles should not be the same\n");
335         rc = pImmAssociateContextEx(NULL, NULL, 0);
336         ok(!rc, "ImmAssociateContextEx succeeded\n");
337         rc = pImmAssociateContextEx(hwnd, NULL, 0);
338         ok(rc, "ImmAssociateContextEx failed\n");
339         rc = pImmAssociateContextEx(NULL, imc, 0);
340         ok(!rc, "ImmAssociateContextEx succeeded\n");
341
342         rc = pImmAssociateContextEx(hwnd, imc, 0);
343         ok(rc, "ImmAssociateContextEx failed\n");
344         retimc = ImmGetContext(hwnd);
345         ok(retimc == imc, "handles should be the same\n");
346         ImmReleaseContext(hwnd,retimc);
347
348         rc = pImmAssociateContextEx(hwnd, newimc, 0);
349         ok(rc, "ImmAssociateContextEx failed\n");
350         retimc = ImmGetContext(hwnd);
351         ok(retimc == newimc, "handles should be the same\n");
352         ImmReleaseContext(hwnd,retimc);
353
354         rc = pImmAssociateContextEx(hwnd, NULL, IACE_DEFAULT);
355         ok(rc, "ImmAssociateContextEx failed\n");
356     }
357     ImmReleaseContext(hwnd,imc);
358 }
359
360 typedef struct _igc_threadinfo {
361     HWND hwnd;
362     HANDLE event;
363     HIMC himc;
364 } igc_threadinfo;
365
366
367 static DWORD WINAPI ImmGetContextThreadFunc( LPVOID lpParam)
368 {
369     HIMC h1,h2;
370     HWND hwnd2;
371     COMPOSITIONFORM cf;
372     POINT pt;
373     igc_threadinfo *info= (igc_threadinfo*)lpParam;
374     info->hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, wndcls, "Wine imm32.dll test",
375                           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
376                           240, 120, NULL, NULL, GetModuleHandle(0), NULL);
377
378     h1 = ImmGetContext(hwnd);
379     todo_wine ok(info->himc == h1, "hwnd context changed in new thread\n");
380     h2 = ImmGetContext(info->hwnd);
381     todo_wine ok(h2 != h1, "new hwnd in new thread should have different context\n");
382     info->himc = h2;
383     ImmReleaseContext(hwnd,h1);
384
385     hwnd2 = CreateWindowEx(WS_EX_CLIENTEDGE, wndcls, "Wine imm32.dll test",
386                           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
387                           240, 120, NULL, NULL, GetModuleHandle(0), NULL);
388     h1 = ImmGetContext(hwnd2);
389
390     ok(h1 == h2, "Windows in same thread should have same default context\n");
391     ImmReleaseContext(hwnd2,h1);
392     ImmReleaseContext(info->hwnd,h2);
393     DestroyWindow(hwnd2);
394
395     /* priming for later tests */
396     ImmSetCompositionWindow(h1, &cf);
397     ImmSetStatusWindowPos(h1, &pt);
398
399     SetEvent(info->event);
400     Sleep(INFINITE);
401     return 1;
402 }
403
404 static void test_ImmThreads(void)
405 {
406     HIMC himc, otherHimc, h1;
407     igc_threadinfo threadinfo;
408     HANDLE hThread;
409     DWORD dwThreadId;
410     BOOL rc;
411     LOGFONT lf;
412     COMPOSITIONFORM cf;
413     DWORD status, sentence;
414     POINT pt;
415
416     himc = ImmGetContext(hwnd);
417     threadinfo.event = CreateEvent(NULL, TRUE, FALSE, NULL);
418     threadinfo.himc = himc;
419     hThread = CreateThread(NULL, 0, ImmGetContextThreadFunc, &threadinfo, 0, &dwThreadId );
420     WaitForSingleObject(threadinfo.event, INFINITE);
421
422     otherHimc = ImmGetContext(threadinfo.hwnd);
423
424     todo_wine ok(himc != otherHimc, "Windows from other threads should have different himc\n");
425     todo_wine ok(otherHimc == threadinfo.himc, "Context from other thread should not change in main thread\n");
426
427     if (0) /* FIXME: Causes wine to hang */
428     {
429     h1 = ImmAssociateContext(hwnd,otherHimc);
430     ok(h1 == NULL, "Should fail to be able to Associate a default context from a different thread\n");
431     h1 = ImmGetContext(hwnd);
432     ok(h1 == himc, "Context for window should remain unchanged\n");
433     ImmReleaseContext(hwnd,h1);
434     }
435
436
437     /* OpenStatus */
438     rc = ImmSetOpenStatus(himc, TRUE);
439     ok(rc != 0, "ImmSetOpenStatus failed\n");
440     rc = ImmGetOpenStatus(himc);
441     ok(rc != 0, "ImmGetOpenStatus failed\n");
442     rc = ImmSetOpenStatus(himc, FALSE);
443     ok(rc != 0, "ImmSetOpenStatus failed\n");
444     rc = ImmGetOpenStatus(himc);
445     ok(rc == 0, "ImmGetOpenStatus failed\n");
446
447     rc = ImmSetOpenStatus(otherHimc, TRUE);
448     todo_wine ok(rc == 0, "ImmSetOpenStatus should fail\n");
449     rc = ImmGetOpenStatus(otherHimc);
450     todo_wine ok(rc == 0, "ImmGetOpenStatus failed\n");
451     rc = ImmSetOpenStatus(otherHimc, FALSE);
452     todo_wine ok(rc == 0, "ImmSetOpenStatus should fail\n");
453     rc = ImmGetOpenStatus(otherHimc);
454     ok(rc == 0, "ImmGetOpenStatus failed\n");
455
456     /* CompositionFont */
457     rc = ImmGetCompositionFont(himc, &lf);
458     ok(rc != 0, "ImmGetCompositionFont failed\n");
459     rc = ImmSetCompositionFont(himc, &lf);
460     ok(rc != 0, "ImmSetCompositionFont failed\n");
461
462     rc = ImmGetCompositionFont(otherHimc, &lf);
463     ok(rc != 0 || broken(rc == 0), "ImmGetCompositionFont failed\n");
464     rc = ImmSetCompositionFont(otherHimc, &lf);
465     todo_wine ok(rc == 0, "ImmSetCompositionFont should fail\n");
466
467     /* CompositionWindow */
468     rc = ImmSetCompositionWindow(himc, &cf);
469     ok(rc != 0, "ImmSetCompositionWindow failed\n");
470     rc = ImmGetCompositionWindow(himc, &cf);
471     ok(rc != 0, "ImmGetCompositionWindow failed\n");
472
473     rc = ImmSetCompositionWindow(otherHimc, &cf);
474     todo_wine ok(rc == 0, "ImmSetCompositionWindow should fail\n");
475     rc = ImmGetCompositionWindow(otherHimc, &cf);
476     ok(rc != 0 || broken(rc == 0), "ImmGetCompositionWindow failed\n");
477
478     /* ConversionStatus */
479     rc = ImmGetConversionStatus(himc, &status, &sentence);
480     ok(rc != 0, "ImmGetConversionStatus failed\n");
481     rc = ImmSetConversionStatus(himc, status, sentence);
482     ok(rc != 0, "ImmSetConversionStatus failed\n");
483
484     rc = ImmGetConversionStatus(otherHimc, &status, &sentence);
485     ok(rc != 0 || broken(rc == 0), "ImmGetConversionStatus failed\n");
486     rc = ImmSetConversionStatus(otherHimc, status, sentence);
487     todo_wine ok(rc == 0, "ImmSetConversionStatus should fail\n");
488
489     /* StatusWindowPos */
490     rc = ImmSetStatusWindowPos(himc, &pt);
491     ok(rc != 0, "ImmSetStatusWindowPos failed\n");
492     rc = ImmGetStatusWindowPos(himc, &pt);
493     ok(rc != 0, "ImmGetStatusWindowPos failed\n");
494
495     rc = ImmSetStatusWindowPos(otherHimc, &pt);
496     todo_wine ok(rc == 0, "ImmSetStatusWindowPos should fail\n");
497     rc = ImmGetStatusWindowPos(otherHimc, &pt);
498     ok(rc != 0 || broken(rc == 0), "ImmGetStatusWindowPos failed\n");
499
500     ImmReleaseContext(threadinfo.hwnd,otherHimc);
501     ImmReleaseContext(hwnd,himc);
502
503     DestroyWindow(threadinfo.hwnd);
504     TerminateThread(hThread, 1);
505
506     himc = ImmGetContext(GetDesktopWindow());
507     todo_wine ok(himc == NULL, "Should not be able to get himc from other process window\n");
508 }
509
510 START_TEST(imm32) {
511     if (init())
512     {
513         test_ImmNotifyIME();
514         test_ImmGetCompositionString();
515         test_ImmSetCompositionString();
516         test_ImmIME();
517         test_ImmAssociateContextEx();
518         test_ImmThreads();
519     }
520     cleanup();
521 }