imm32: Fix tests to apply to Vista+ as it does the same as Wine.
[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 "imm.h"
26
27 #define NUMELEMS(array) (sizeof((array))/sizeof((array)[0]))
28
29 static BOOL (WINAPI *pImmAssociateContextEx)(HWND,HIMC,DWORD);
30
31 /*
32  * msgspy - record and analyse message traces sent to a certain window
33  */
34 static struct _msg_spy {
35     HWND         hwnd;
36     HHOOK        get_msg_hook;
37     HHOOK        call_wnd_proc_hook;
38     CWPSTRUCT    msgs[32];
39     unsigned int i_msg;
40 } msg_spy;
41
42 static LRESULT CALLBACK get_msg_filter(int nCode, WPARAM wParam, LPARAM lParam)
43 {
44     if (HC_ACTION == nCode) {
45         MSG *msg = (MSG*)lParam;
46
47         if ((msg->hwnd == msg_spy.hwnd) &&
48             (msg_spy.i_msg < NUMELEMS(msg_spy.msgs)))
49         {
50             msg_spy.msgs[msg_spy.i_msg].hwnd    = msg->hwnd;
51             msg_spy.msgs[msg_spy.i_msg].message = msg->message;
52             msg_spy.msgs[msg_spy.i_msg].wParam  = msg->wParam;
53             msg_spy.msgs[msg_spy.i_msg].lParam  = msg->lParam;
54             msg_spy.i_msg++;
55         }
56     }
57
58     return CallNextHookEx(msg_spy.get_msg_hook, nCode, wParam, lParam);
59 }
60
61 static LRESULT CALLBACK call_wnd_proc_filter(int nCode, WPARAM wParam,
62                                              LPARAM lParam)
63 {
64     if (HC_ACTION == nCode) {
65         CWPSTRUCT *cwp = (CWPSTRUCT*)lParam;
66
67         if ((cwp->hwnd == msg_spy.hwnd) &&
68             (msg_spy.i_msg < NUMELEMS(msg_spy.msgs)))
69         {
70             memcpy(&msg_spy.msgs[msg_spy.i_msg], cwp, sizeof(msg_spy.msgs[0]));
71             msg_spy.i_msg++;
72         }
73     }
74
75     return CallNextHookEx(msg_spy.call_wnd_proc_hook, nCode, wParam, lParam);
76 }
77
78 static void msg_spy_pump_msg_queue(void) {
79     MSG msg;
80
81     while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
82         TranslateMessage(&msg);
83         DispatchMessage(&msg);
84     }
85
86     return;
87 }
88
89 static void msg_spy_flush_msgs(void) {
90     msg_spy_pump_msg_queue();
91     msg_spy.i_msg = 0;
92 }
93
94 static CWPSTRUCT* msg_spy_find_msg(UINT message) {
95     UINT i;
96
97     msg_spy_pump_msg_queue();
98
99     if (msg_spy.i_msg >= NUMELEMS(msg_spy.msgs))
100         fprintf(stdout, "%s:%d: msg_spy: message buffer overflow!\n",
101                 __FILE__, __LINE__);
102
103     for (i = 0; i < msg_spy.i_msg; i++)
104         if (msg_spy.msgs[i].message == message)
105             return &msg_spy.msgs[i];
106
107     return NULL;
108 }
109
110 static void msg_spy_init(HWND hwnd) {
111     msg_spy.hwnd = hwnd;
112     msg_spy.get_msg_hook =
113             SetWindowsHookEx(WH_GETMESSAGE, get_msg_filter, GetModuleHandle(0),
114                              GetCurrentThreadId());
115     msg_spy.call_wnd_proc_hook =
116             SetWindowsHookEx(WH_CALLWNDPROC, call_wnd_proc_filter,
117                              GetModuleHandle(0), GetCurrentThreadId());
118     msg_spy.i_msg = 0;
119
120     msg_spy_flush_msgs();
121 }
122
123 static void msg_spy_cleanup(void) {
124     if (msg_spy.get_msg_hook)
125         UnhookWindowsHookEx(msg_spy.get_msg_hook);
126     if (msg_spy.call_wnd_proc_hook)
127         UnhookWindowsHookEx(msg_spy.call_wnd_proc_hook);
128     memset(&msg_spy, 0, sizeof(msg_spy));
129 }
130
131 /*
132  * imm32 test cases - Issue some IMM commands on a dummy window and analyse the
133  * messages being sent to this window in response.
134  */
135 static const char wndcls[] = "winetest_imm32_wndcls";
136 static HWND hwnd;
137
138 static BOOL init(void) {
139     WNDCLASSEX wc;
140     HIMC imc;
141     HMODULE hmod;
142
143     hmod = GetModuleHandleA("imm32.dll");
144     pImmAssociateContextEx = (void*)GetProcAddress(hmod, "ImmAssociateContextEx");
145
146     wc.cbSize        = sizeof(WNDCLASSEX);
147     wc.style         = 0;
148     wc.lpfnWndProc   = DefWindowProc;
149     wc.cbClsExtra    = 0;
150     wc.cbWndExtra    = 0;
151     wc.hInstance     = GetModuleHandle(0);
152     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
153     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
154     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
155     wc.lpszMenuName  = NULL;
156     wc.lpszClassName = wndcls;
157     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
158
159     if (!RegisterClassExA(&wc))
160         return FALSE;
161
162     hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, wndcls, "Wine imm32.dll test",
163                           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
164                           240, 120, NULL, NULL, GetModuleHandle(0), NULL);
165     if (!hwnd)
166         return FALSE;
167
168     imc = ImmGetContext(hwnd);
169     if (!imc)
170     {
171         win_skip("IME support not implemented\n");
172         return FALSE;
173     }
174     ImmReleaseContext(hwnd, imc);
175
176     ShowWindow(hwnd, SW_SHOWNORMAL);
177     UpdateWindow(hwnd);
178
179     msg_spy_init(hwnd);
180
181     return TRUE;
182 }
183
184 static void cleanup(void) {
185     msg_spy_cleanup();
186     if (hwnd)
187         DestroyWindow(hwnd);
188     UnregisterClass(wndcls, GetModuleHandle(0));
189 }
190
191 static void test_ImmNotifyIME(void) {
192     static const char string[] = "wine";
193     char resstr[16] = "";
194     HIMC imc;
195     BOOL ret;
196
197     imc = ImmGetContext(hwnd);
198     msg_spy_flush_msgs();
199
200     ret = ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
201     ok(broken(!ret) ||
202        ret, /* Vista+ */
203        "Canceling an empty composition string should succeed.\n");
204     ok(!msg_spy_find_msg(WM_IME_COMPOSITION), "Windows does not post "
205        "WM_IME_COMPOSITION in response to NI_COMPOSITIONSTR / CPS_CANCEL, if "
206        "the composition string being canceled is empty.\n");
207
208     ImmSetCompositionString(imc, SCS_SETSTR, string, sizeof(string), NULL, 0);
209     msg_spy_flush_msgs();
210
211     ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
212     ok(!msg_spy_find_msg(WM_IME_COMPOSITION), "Windows does not post "
213        "WM_IME_COMPOSITION in response to NI_COMPOSITIONSTR / CPS_CANCEL, if "
214        "the composition string being canceled is non empty.\n");
215
216     /* behavior differs between win9x and NT */
217     ret = ImmGetCompositionString(imc, GCS_COMPSTR, resstr, sizeof(resstr));
218     ok(!ret, "After being cancelled the composition string is empty.\n");
219
220     msg_spy_flush_msgs();
221
222     ret = ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
223     ok(broken(!ret) ||
224        ret, /* Vista+ */
225        "Canceling an empty composition string should succeed.\n");
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 empty.\n");
229
230     msg_spy_flush_msgs();
231     ImmReleaseContext(hwnd, imc);
232 }
233
234 static void test_ImmGetCompositionString(void)
235 {
236     HIMC imc;
237     static const WCHAR string[] = {'w','i','n','e',0x65e5,0x672c,0x8a9e};
238     char cstring[20];
239     WCHAR wstring[20];
240     DWORD len;
241     DWORD alen,wlen;
242
243     imc = ImmGetContext(hwnd);
244     ImmSetCompositionStringW(imc, SCS_SETSTR, string, sizeof(string), NULL,0);
245     alen = ImmGetCompositionStringA(imc, GCS_COMPSTR, cstring, 20);
246     wlen = ImmGetCompositionStringW(imc, GCS_COMPSTR, wstring, 20);
247     /* windows machines without any IME installed just return 0 above */
248     if( alen && wlen)
249     {
250         len = ImmGetCompositionStringW(imc, GCS_COMPATTR, NULL, 0);
251         ok(len*sizeof(WCHAR)==wlen,"GCS_COMPATTR(W) not returning correct count\n");
252         len = ImmGetCompositionStringA(imc, GCS_COMPATTR, NULL, 0);
253         ok(len==alen,"GCS_COMPATTR(A) not returning correct count\n");
254     }
255     ImmReleaseContext(hwnd, imc);
256 }
257
258 static void test_ImmSetCompositionString(void)
259 {
260     HIMC imc;
261     BOOL ret;
262
263     SetLastError(0xdeadbeef);
264     imc = ImmGetContext(hwnd);
265     ok(imc != 0, "ImmGetContext() failed. Last error: %u\n", GetLastError());
266     if (!imc)
267         return;
268
269     ret = ImmSetCompositionStringW(imc, SCS_SETSTR, NULL, 0, NULL, 0);
270     ok(broken(!ret) ||
271        ret, /* Vista+ */
272        "ImmSetCompositionStringW() failed.\n");
273
274     ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGEATTR,
275         NULL, 0, NULL, 0);
276     ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
277
278     ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGECLAUSE,
279         NULL, 0, NULL, 0);
280     ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
281
282     ret = ImmSetCompositionStringW(imc, SCS_CHANGEATTR | SCS_CHANGECLAUSE,
283         NULL, 0, NULL, 0);
284     ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
285
286     ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGEATTR | SCS_CHANGECLAUSE,
287         NULL, 0, NULL, 0);
288     ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
289
290     ImmReleaseContext(hwnd, imc);
291 }
292
293 static void test_ImmIME(void)
294 {
295     HIMC imc;
296
297     imc = ImmGetContext(hwnd);
298     if (imc)
299     {
300         BOOL rc;
301         rc = ImmConfigureIMEA(imc, NULL, IME_CONFIG_REGISTERWORD, NULL);
302         ok (rc == 0, "ImmConfigureIMEA did not fail\n");
303         rc = ImmConfigureIMEW(imc, NULL, IME_CONFIG_REGISTERWORD, NULL);
304         ok (rc == 0, "ImmConfigureIMEW did not fail\n");
305     }
306     ImmReleaseContext(hwnd,imc);
307 }
308
309 static void test_ImmAssociateContextEx(void)
310 {
311     HIMC imc;
312     BOOL rc;
313
314     if (!pImmAssociateContextEx) return;
315
316     imc = ImmGetContext(hwnd);
317     if (imc)
318     {
319         HIMC retimc, newimc;
320
321         newimc = ImmCreateContext();
322         ok(newimc != imc, "handles should not be the same\n");
323         rc = pImmAssociateContextEx(NULL, NULL, 0);
324         ok(!rc, "ImmAssociateContextEx succeeded\n");
325         rc = pImmAssociateContextEx(hwnd, NULL, 0);
326         ok(rc, "ImmAssociateContextEx failed\n");
327         rc = pImmAssociateContextEx(NULL, imc, 0);
328         ok(!rc, "ImmAssociateContextEx succeeded\n");
329
330         rc = pImmAssociateContextEx(hwnd, imc, 0);
331         ok(rc, "ImmAssociateContextEx failed\n");
332         retimc = ImmGetContext(hwnd);
333         ok(retimc == imc, "handles should be the same\n");
334         ImmReleaseContext(hwnd,retimc);
335
336         rc = pImmAssociateContextEx(hwnd, newimc, 0);
337         ok(rc, "ImmAssociateContextEx failed\n");
338         retimc = ImmGetContext(hwnd);
339         ok(retimc == newimc, "handles should be the same\n");
340         ImmReleaseContext(hwnd,retimc);
341
342         rc = pImmAssociateContextEx(hwnd, NULL, IACE_DEFAULT);
343         ok(rc, "ImmAssociateContextEx failed\n");
344     }
345     ImmReleaseContext(hwnd,imc);
346 }
347
348 START_TEST(imm32) {
349     if (init())
350     {
351         test_ImmNotifyIME();
352         test_ImmGetCompositionString();
353         test_ImmSetCompositionString();
354         test_ImmIME();
355         test_ImmAssociateContextEx();
356     }
357     cleanup();
358 }