include/msvcrt: Define more CPU control word flags.
[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     ok(!msg_spy_find_msg(WM_IME_COMPOSITION), "Windows does not post "
227        "WM_IME_COMPOSITION in response to NI_COMPOSITIONSTR / CPS_CANCEL, if "
228        "the composition string being canceled is non empty.\n");
229
230     /* behavior differs between win9x and NT */
231     ret = ImmGetCompositionString(imc, GCS_COMPSTR, resstr, sizeof(resstr));
232     ok(!ret, "After being cancelled the composition string is empty.\n");
233
234     msg_spy_flush_msgs();
235
236     ret = ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
237     ok(broken(!ret) ||
238        ret, /* Vista+ */
239        "Canceling an empty composition string should succeed.\n");
240     ok(!msg_spy_find_msg(WM_IME_COMPOSITION), "Windows does not post "
241        "WM_IME_COMPOSITION in response to NI_COMPOSITIONSTR / CPS_CANCEL, if "
242        "the composition string being canceled is empty.\n");
243
244     msg_spy_flush_msgs();
245     ImmReleaseContext(hwnd, imc);
246 }
247
248 static void test_ImmGetCompositionString(void)
249 {
250     HIMC imc;
251     static const WCHAR string[] = {'w','i','n','e',0x65e5,0x672c,0x8a9e};
252     char cstring[20];
253     WCHAR wstring[20];
254     DWORD len;
255     DWORD alen,wlen;
256
257     imc = ImmGetContext(hwnd);
258     ImmSetCompositionStringW(imc, SCS_SETSTR, string, sizeof(string), NULL,0);
259     alen = ImmGetCompositionStringA(imc, GCS_COMPSTR, cstring, 20);
260     wlen = ImmGetCompositionStringW(imc, GCS_COMPSTR, wstring, 20);
261     /* windows machines without any IME installed just return 0 above */
262     if( alen && wlen)
263     {
264         len = ImmGetCompositionStringW(imc, GCS_COMPATTR, NULL, 0);
265         ok(len*sizeof(WCHAR)==wlen,"GCS_COMPATTR(W) not returning correct count\n");
266         len = ImmGetCompositionStringA(imc, GCS_COMPATTR, NULL, 0);
267         ok(len==alen,"GCS_COMPATTR(A) not returning correct count\n");
268     }
269     ImmReleaseContext(hwnd, imc);
270 }
271
272 static void test_ImmSetCompositionString(void)
273 {
274     HIMC imc;
275     BOOL ret;
276
277     SetLastError(0xdeadbeef);
278     imc = ImmGetContext(hwnd);
279     ok(imc != 0, "ImmGetContext() failed. Last error: %u\n", GetLastError());
280     if (!imc)
281         return;
282
283     ret = ImmSetCompositionStringW(imc, SCS_SETSTR, NULL, 0, NULL, 0);
284     ok(broken(!ret) ||
285        ret, /* Vista+ */
286        "ImmSetCompositionStringW() failed.\n");
287
288     ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGEATTR,
289         NULL, 0, NULL, 0);
290     ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
291
292     ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGECLAUSE,
293         NULL, 0, NULL, 0);
294     ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
295
296     ret = ImmSetCompositionStringW(imc, SCS_CHANGEATTR | SCS_CHANGECLAUSE,
297         NULL, 0, NULL, 0);
298     ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
299
300     ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGEATTR | SCS_CHANGECLAUSE,
301         NULL, 0, NULL, 0);
302     ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
303
304     ImmReleaseContext(hwnd, imc);
305 }
306
307 static void test_ImmIME(void)
308 {
309     HIMC imc;
310
311     imc = ImmGetContext(hwnd);
312     if (imc)
313     {
314         BOOL rc;
315         rc = ImmConfigureIMEA(imc, NULL, IME_CONFIG_REGISTERWORD, NULL);
316         ok (rc == 0, "ImmConfigureIMEA did not fail\n");
317         rc = ImmConfigureIMEW(imc, NULL, IME_CONFIG_REGISTERWORD, NULL);
318         ok (rc == 0, "ImmConfigureIMEW did not fail\n");
319     }
320     ImmReleaseContext(hwnd,imc);
321 }
322
323 static void test_ImmAssociateContextEx(void)
324 {
325     HIMC imc;
326     BOOL rc;
327
328     if (!pImmAssociateContextEx) return;
329
330     imc = ImmGetContext(hwnd);
331     if (imc)
332     {
333         HIMC retimc, newimc;
334
335         newimc = ImmCreateContext();
336         ok(newimc != imc, "handles should not be the same\n");
337         rc = pImmAssociateContextEx(NULL, NULL, 0);
338         ok(!rc, "ImmAssociateContextEx succeeded\n");
339         rc = pImmAssociateContextEx(hwnd, NULL, 0);
340         ok(rc, "ImmAssociateContextEx failed\n");
341         rc = pImmAssociateContextEx(NULL, imc, 0);
342         ok(!rc, "ImmAssociateContextEx succeeded\n");
343
344         rc = pImmAssociateContextEx(hwnd, imc, 0);
345         ok(rc, "ImmAssociateContextEx failed\n");
346         retimc = ImmGetContext(hwnd);
347         ok(retimc == imc, "handles should be the same\n");
348         ImmReleaseContext(hwnd,retimc);
349
350         rc = pImmAssociateContextEx(hwnd, newimc, 0);
351         ok(rc, "ImmAssociateContextEx failed\n");
352         retimc = ImmGetContext(hwnd);
353         ok(retimc == newimc, "handles should be the same\n");
354         ImmReleaseContext(hwnd,retimc);
355
356         rc = pImmAssociateContextEx(hwnd, NULL, IACE_DEFAULT);
357         ok(rc, "ImmAssociateContextEx failed\n");
358     }
359     ImmReleaseContext(hwnd,imc);
360 }
361
362 typedef struct _igc_threadinfo {
363     HWND hwnd;
364     HANDLE event;
365     HIMC himc;
366 } igc_threadinfo;
367
368
369 static DWORD WINAPI ImmGetContextThreadFunc( LPVOID lpParam)
370 {
371     HIMC h1,h2;
372     HWND hwnd2;
373     COMPOSITIONFORM cf;
374     POINT pt;
375     igc_threadinfo *info= (igc_threadinfo*)lpParam;
376     info->hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, wndcls, "Wine imm32.dll test",
377                           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
378                           240, 120, NULL, NULL, GetModuleHandle(0), NULL);
379
380     h1 = ImmGetContext(hwnd);
381     todo_wine ok(info->himc == h1, "hwnd context changed in new thread\n");
382     h2 = ImmGetContext(info->hwnd);
383     todo_wine ok(h2 != h1, "new hwnd in new thread should have different context\n");
384     info->himc = h2;
385     ImmReleaseContext(hwnd,h1);
386
387     hwnd2 = CreateWindowEx(WS_EX_CLIENTEDGE, wndcls, "Wine imm32.dll test",
388                           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
389                           240, 120, NULL, NULL, GetModuleHandle(0), NULL);
390     h1 = ImmGetContext(hwnd2);
391
392     ok(h1 == h2, "Windows in same thread should have same default context\n");
393     ImmReleaseContext(hwnd2,h1);
394     ImmReleaseContext(info->hwnd,h2);
395     DestroyWindow(hwnd2);
396
397     /* priming for later tests */
398     ImmSetCompositionWindow(h1, &cf);
399     ImmSetStatusWindowPos(h1, &pt);
400
401     SetEvent(info->event);
402     Sleep(INFINITE);
403     return 1;
404 }
405
406 static void test_ImmThreads(void)
407 {
408     HIMC himc, otherHimc, h1;
409     igc_threadinfo threadinfo;
410     HANDLE hThread;
411     DWORD dwThreadId;
412     BOOL rc;
413     LOGFONT lf;
414     COMPOSITIONFORM cf;
415     DWORD status, sentence;
416     POINT pt;
417
418     himc = ImmGetContext(hwnd);
419     threadinfo.event = CreateEvent(NULL, TRUE, FALSE, NULL);
420     threadinfo.himc = himc;
421     hThread = CreateThread(NULL, 0, ImmGetContextThreadFunc, &threadinfo, 0, &dwThreadId );
422     WaitForSingleObject(threadinfo.event, INFINITE);
423
424     otherHimc = ImmGetContext(threadinfo.hwnd);
425
426     todo_wine ok(himc != otherHimc, "Windows from other threads should have different himc\n");
427     todo_wine ok(otherHimc == threadinfo.himc, "Context from other thread should not change in main thread\n");
428
429     if (0) /* FIXME: Causes wine to hang */
430     {
431     h1 = ImmAssociateContext(hwnd,otherHimc);
432     ok(h1 == NULL, "Should fail to be able to Associate a default context from a different thread\n");
433     h1 = ImmGetContext(hwnd);
434     ok(h1 == himc, "Context for window should remain unchanged\n");
435     ImmReleaseContext(hwnd,h1);
436     }
437
438
439     /* OpenStatus */
440     rc = ImmSetOpenStatus(himc, TRUE);
441     ok(rc != 0, "ImmSetOpenStatus failed\n");
442     rc = ImmGetOpenStatus(himc);
443     ok(rc != 0, "ImmGetOpenStatus failed\n");
444     rc = ImmSetOpenStatus(himc, FALSE);
445     ok(rc != 0, "ImmSetOpenStatus failed\n");
446     rc = ImmGetOpenStatus(himc);
447     ok(rc == 0, "ImmGetOpenStatus failed\n");
448
449     rc = ImmSetOpenStatus(otherHimc, TRUE);
450     todo_wine ok(rc == 0, "ImmSetOpenStatus should fail\n");
451     rc = ImmGetOpenStatus(otherHimc);
452     todo_wine ok(rc == 0, "ImmGetOpenStatus failed\n");
453     rc = ImmSetOpenStatus(otherHimc, FALSE);
454     todo_wine ok(rc == 0, "ImmSetOpenStatus should fail\n");
455     rc = ImmGetOpenStatus(otherHimc);
456     ok(rc == 0, "ImmGetOpenStatus failed\n");
457
458     /* CompositionFont */
459     rc = ImmGetCompositionFont(himc, &lf);
460     ok(rc != 0, "ImmGetCompositionFont failed\n");
461     rc = ImmSetCompositionFont(himc, &lf);
462     ok(rc != 0, "ImmSetCompositionFont failed\n");
463
464     rc = ImmGetCompositionFont(otherHimc, &lf);
465     ok(rc != 0 || broken(rc == 0), "ImmGetCompositionFont failed\n");
466     rc = ImmSetCompositionFont(otherHimc, &lf);
467     todo_wine ok(rc == 0, "ImmSetCompositionFont should fail\n");
468
469     /* CompositionWindow */
470     rc = ImmSetCompositionWindow(himc, &cf);
471     ok(rc != 0, "ImmSetCompositionWindow failed\n");
472     rc = ImmGetCompositionWindow(himc, &cf);
473     ok(rc != 0, "ImmGetCompositionWindow failed\n");
474
475     rc = ImmSetCompositionWindow(otherHimc, &cf);
476     todo_wine ok(rc == 0, "ImmSetCompositionWindow should fail\n");
477     rc = ImmGetCompositionWindow(otherHimc, &cf);
478     ok(rc != 0 || broken(rc == 0), "ImmGetCompositionWindow failed\n");
479
480     /* ConversionStatus */
481     rc = ImmGetConversionStatus(himc, &status, &sentence);
482     ok(rc != 0, "ImmGetConversionStatus failed\n");
483     rc = ImmSetConversionStatus(himc, status, sentence);
484     ok(rc != 0, "ImmSetConversionStatus failed\n");
485
486     rc = ImmGetConversionStatus(otherHimc, &status, &sentence);
487     ok(rc != 0 || broken(rc == 0), "ImmGetConversionStatus failed\n");
488     rc = ImmSetConversionStatus(otherHimc, status, sentence);
489     todo_wine ok(rc == 0, "ImmSetConversionStatus should fail\n");
490
491     /* StatusWindowPos */
492     rc = ImmSetStatusWindowPos(himc, &pt);
493     ok(rc != 0, "ImmSetStatusWindowPos failed\n");
494     rc = ImmGetStatusWindowPos(himc, &pt);
495     ok(rc != 0, "ImmGetStatusWindowPos failed\n");
496
497     rc = ImmSetStatusWindowPos(otherHimc, &pt);
498     todo_wine ok(rc == 0, "ImmSetStatusWindowPos should fail\n");
499     rc = ImmGetStatusWindowPos(otherHimc, &pt);
500     ok(rc != 0 || broken(rc == 0), "ImmGetStatusWindowPos failed\n");
501
502     ImmReleaseContext(threadinfo.hwnd,otherHimc);
503     ImmReleaseContext(hwnd,himc);
504
505     DestroyWindow(threadinfo.hwnd);
506     TerminateThread(hThread, 1);
507
508     himc = ImmGetContext(GetDesktopWindow());
509     todo_wine ok(himc == NULL, "Should not be able to get himc from other process window\n");
510 }
511
512 START_TEST(imm32) {
513     if (init())
514     {
515         test_ImmNotifyIME();
516         test_ImmGetCompositionString();
517         test_ImmSetCompositionString();
518         test_ImmIME();
519         test_ImmAssociateContextEx();
520         test_ImmThreads();
521     }
522     cleanup();
523 }