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"
27 #define NUMELEMS(array) (sizeof((array))/sizeof((array)[0]))
29 static BOOL (WINAPI *pImmAssociateContextEx)(HWND,HIMC,DWORD);
32 * msgspy - record and analyse message traces sent to a certain window
34 static struct _msg_spy {
37 HHOOK call_wnd_proc_hook;
42 static LRESULT CALLBACK get_msg_filter(int nCode, WPARAM wParam, LPARAM lParam)
44 if (HC_ACTION == nCode) {
45 MSG *msg = (MSG*)lParam;
47 if ((msg->hwnd == msg_spy.hwnd) &&
48 (msg_spy.i_msg < NUMELEMS(msg_spy.msgs)))
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;
58 return CallNextHookEx(msg_spy.get_msg_hook, nCode, wParam, lParam);
61 static LRESULT CALLBACK call_wnd_proc_filter(int nCode, WPARAM wParam,
64 if (HC_ACTION == nCode) {
65 CWPSTRUCT *cwp = (CWPSTRUCT*)lParam;
67 if ((cwp->hwnd == msg_spy.hwnd) &&
68 (msg_spy.i_msg < NUMELEMS(msg_spy.msgs)))
70 memcpy(&msg_spy.msgs[msg_spy.i_msg], cwp, sizeof(msg_spy.msgs[0]));
75 return CallNextHookEx(msg_spy.call_wnd_proc_hook, nCode, wParam, lParam);
78 static void msg_spy_pump_msg_queue(void) {
81 while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
82 TranslateMessage(&msg);
83 DispatchMessage(&msg);
89 static void msg_spy_flush_msgs(void) {
90 msg_spy_pump_msg_queue();
94 static CWPSTRUCT* msg_spy_find_msg(UINT message) {
97 msg_spy_pump_msg_queue();
99 if (msg_spy.i_msg >= NUMELEMS(msg_spy.msgs))
100 fprintf(stdout, "%s:%d: msg_spy: message buffer overflow!\n",
103 for (i = 0; i < msg_spy.i_msg; i++)
104 if (msg_spy.msgs[i].message == message)
105 return &msg_spy.msgs[i];
110 static void msg_spy_init(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());
120 msg_spy_flush_msgs();
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));
132 * imm32 test cases - Issue some IMM commands on a dummy window and analyse the
133 * messages being sent to this window in response.
135 static const char wndcls[] = "winetest_imm32_wndcls";
138 static BOOL init(void) {
143 hmod = GetModuleHandleA("imm32.dll");
144 pImmAssociateContextEx = (void*)GetProcAddress(hmod, "ImmAssociateContextEx");
146 wc.cbSize = sizeof(WNDCLASSEX);
148 wc.lpfnWndProc = DefWindowProc;
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);
159 if (!RegisterClassExA(&wc))
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);
168 imc = ImmGetContext(hwnd);
171 win_skip("IME support not implemented\n");
174 ImmReleaseContext(hwnd, imc);
176 ShowWindow(hwnd, SW_SHOWNORMAL);
184 static void cleanup(void) {
188 UnregisterClass(wndcls, GetModuleHandle(0));
191 static void test_ImmNotifyIME(void) {
192 static const char string[] = "wine";
193 char resstr[16] = "";
197 imc = ImmGetContext(hwnd);
198 msg_spy_flush_msgs();
200 ret = ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
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");
208 ImmSetCompositionString(imc, SCS_SETSTR, string, sizeof(string), NULL, 0);
209 msg_spy_flush_msgs();
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");
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");
220 msg_spy_flush_msgs();
222 ret = ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
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");
230 msg_spy_flush_msgs();
231 ImmReleaseContext(hwnd, imc);
234 static void test_ImmGetCompositionString(void)
237 static const WCHAR string[] = {'w','i','n','e',0x65e5,0x672c,0x8a9e};
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 */
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");
255 ImmReleaseContext(hwnd, imc);
258 static void test_ImmSetCompositionString(void)
263 SetLastError(0xdeadbeef);
264 imc = ImmGetContext(hwnd);
265 ok(imc != 0, "ImmGetContext() failed. Last error: %u\n", GetLastError());
269 ret = ImmSetCompositionStringW(imc, SCS_SETSTR, NULL, 0, NULL, 0);
272 "ImmSetCompositionStringW() failed.\n");
274 ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGEATTR,
276 ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
278 ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGECLAUSE,
280 ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
282 ret = ImmSetCompositionStringW(imc, SCS_CHANGEATTR | SCS_CHANGECLAUSE,
284 ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
286 ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGEATTR | SCS_CHANGECLAUSE,
288 ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
290 ImmReleaseContext(hwnd, imc);
293 static void test_ImmIME(void)
297 imc = ImmGetContext(hwnd);
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");
306 ImmReleaseContext(hwnd,imc);
309 static void test_ImmAssociateContextEx(void)
314 if (!pImmAssociateContextEx) return;
316 imc = ImmGetContext(hwnd);
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");
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);
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);
342 rc = pImmAssociateContextEx(hwnd, NULL, IACE_DEFAULT);
343 ok(rc, "ImmAssociateContextEx failed\n");
345 ImmReleaseContext(hwnd,imc);
352 test_ImmGetCompositionString();
353 test_ImmSetCompositionString();
355 test_ImmAssociateContextEx();