4 * Copyright (c) 2008 Michael Jung
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.
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.
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
23 #include "wine/test.h"
28 #define NUMELEMS(array) (sizeof((array))/sizeof((array)[0]))
30 static BOOL (WINAPI *pImmAssociateContextEx)(HWND,HIMC,DWORD);
33 * msgspy - record and analyse message traces sent to a certain window
35 static struct _msg_spy {
38 HHOOK call_wnd_proc_hook;
43 static LRESULT CALLBACK get_msg_filter(int nCode, WPARAM wParam, LPARAM lParam)
45 if (HC_ACTION == nCode) {
46 MSG *msg = (MSG*)lParam;
48 if ((msg->hwnd == msg_spy.hwnd) &&
49 (msg_spy.i_msg < NUMELEMS(msg_spy.msgs)))
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;
59 return CallNextHookEx(msg_spy.get_msg_hook, nCode, wParam, lParam);
62 static LRESULT CALLBACK call_wnd_proc_filter(int nCode, WPARAM wParam,
65 if (HC_ACTION == nCode) {
66 CWPSTRUCT *cwp = (CWPSTRUCT*)lParam;
68 if ((cwp->hwnd == msg_spy.hwnd) &&
69 (msg_spy.i_msg < NUMELEMS(msg_spy.msgs)))
71 memcpy(&msg_spy.msgs[msg_spy.i_msg], cwp, sizeof(msg_spy.msgs[0]));
76 return CallNextHookEx(msg_spy.call_wnd_proc_hook, nCode, wParam, lParam);
79 static void msg_spy_pump_msg_queue(void) {
82 while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
83 TranslateMessage(&msg);
84 DispatchMessage(&msg);
90 static void msg_spy_flush_msgs(void) {
91 msg_spy_pump_msg_queue();
95 static CWPSTRUCT* msg_spy_find_msg(UINT message) {
98 msg_spy_pump_msg_queue();
100 if (msg_spy.i_msg >= NUMELEMS(msg_spy.msgs))
101 fprintf(stdout, "%s:%d: msg_spy: message buffer overflow!\n",
104 for (i = 0; i < msg_spy.i_msg; i++)
105 if (msg_spy.msgs[i].message == message)
106 return &msg_spy.msgs[i];
111 static void msg_spy_init(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());
121 msg_spy_flush_msgs();
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));
133 * imm32 test cases - Issue some IMM commands on a dummy window and analyse the
134 * messages being sent to this window in response.
136 static const char wndcls[] = "winetest_imm32_wndcls";
139 static LRESULT WINAPI wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
143 case WM_IME_SETCONTEXT:
149 return DefWindowProcA(hwnd,msg,wParam,lParam);
152 static BOOL init(void) {
157 hmod = GetModuleHandleA("imm32.dll");
158 pImmAssociateContextEx = (void*)GetProcAddress(hmod, "ImmAssociateContextEx");
160 wc.cbSize = sizeof(WNDCLASSEX);
162 wc.lpfnWndProc = wndProc;
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);
173 if (!RegisterClassExA(&wc))
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);
182 imc = ImmGetContext(hwnd);
185 win_skip("IME support not implemented\n");
188 ImmReleaseContext(hwnd, imc);
190 ShowWindow(hwnd, SW_SHOWNORMAL);
198 static void cleanup(void) {
202 UnregisterClass(wndcls, GetModuleHandle(0));
205 static void test_ImmNotifyIME(void) {
206 static const char string[] = "wine";
207 char resstr[16] = "";
211 imc = ImmGetContext(hwnd);
212 msg_spy_flush_msgs();
214 ret = ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
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");
222 ImmSetCompositionString(imc, SCS_SETSTR, string, sizeof(string), NULL, 0);
223 msg_spy_flush_msgs();
225 ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
226 msg_spy_flush_msgs();
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");
232 msg_spy_flush_msgs();
234 ret = ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
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");
242 msg_spy_flush_msgs();
243 ImmReleaseContext(hwnd, imc);
246 static void test_ImmGetCompositionString(void)
249 static const WCHAR string[] = {'w','i','n','e',0x65e5,0x672c,0x8a9e};
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 */
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");
267 ImmReleaseContext(hwnd, imc);
270 static void test_ImmSetCompositionString(void)
275 SetLastError(0xdeadbeef);
276 imc = ImmGetContext(hwnd);
277 ok(imc != 0, "ImmGetContext() failed. Last error: %u\n", GetLastError());
281 ret = ImmSetCompositionStringW(imc, SCS_SETSTR, NULL, 0, NULL, 0);
284 "ImmSetCompositionStringW() failed.\n");
286 ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGEATTR,
288 ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
290 ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGECLAUSE,
292 ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
294 ret = ImmSetCompositionStringW(imc, SCS_CHANGEATTR | SCS_CHANGECLAUSE,
296 ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
298 ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGEATTR | SCS_CHANGECLAUSE,
300 ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
302 ImmReleaseContext(hwnd, imc);
305 static void test_ImmIME(void)
309 imc = ImmGetContext(hwnd);
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");
318 ImmReleaseContext(hwnd,imc);
321 static void test_ImmAssociateContextEx(void)
326 if (!pImmAssociateContextEx) return;
328 imc = ImmGetContext(hwnd);
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");
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);
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);
354 rc = pImmAssociateContextEx(hwnd, NULL, IACE_DEFAULT);
355 ok(rc, "ImmAssociateContextEx failed\n");
357 ImmReleaseContext(hwnd,imc);
360 typedef struct _igc_threadinfo {
367 static DWORD WINAPI ImmGetContextThreadFunc( LPVOID lpParam)
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);
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");
383 ImmReleaseContext(hwnd,h1);
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);
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);
395 /* priming for later tests */
396 ImmSetCompositionWindow(h1, &cf);
397 ImmSetStatusWindowPos(h1, &pt);
399 SetEvent(info->event);
404 static void test_ImmThreads(void)
406 HIMC himc, otherHimc, h1;
407 igc_threadinfo threadinfo;
413 DWORD status, sentence;
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);
422 otherHimc = ImmGetContext(threadinfo.hwnd);
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");
427 if (0) /* FIXME: Causes wine to hang */
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);
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
500 ImmReleaseContext(threadinfo.hwnd,otherHimc);
501 ImmReleaseContext(hwnd,himc);
503 DestroyWindow(threadinfo.hwnd);
504 TerminateThread(hThread, 1);
506 himc = ImmGetContext(GetDesktopWindow());
507 todo_wine ok(himc == NULL, "Should not be able to get himc from other process window\n");
514 test_ImmGetCompositionString();
515 test_ImmSetCompositionString();
517 test_ImmAssociateContextEx();