winecoreaudio: Use same hard-coded limit on frequency as ALSA/OSS.
[wine.git] / dlls / imm32 / tests / imm32.c
blobe35ef89a33c5361987551f6c054641df28586e11
1 /*
2 * Unit tests for imm32
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
21 #include <stdio.h>
23 #include "wine/test.h"
24 #include "winuser.h"
25 #include "imm.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 {
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;
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;
54 msg_spy.i_msg++;
58 return CallNextHookEx(msg_spy.get_msg_hook, nCode, wParam, lParam);
61 static LRESULT CALLBACK call_wnd_proc_filter(int nCode, WPARAM wParam,
62 LPARAM lParam)
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]));
71 msg_spy.i_msg++;
75 return CallNextHookEx(msg_spy.call_wnd_proc_hook, nCode, wParam, lParam);
78 static void msg_spy_pump_msg_queue(void) {
79 MSG msg;
81 while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
82 TranslateMessage(&msg);
83 DispatchMessage(&msg);
86 return;
89 static void msg_spy_flush_msgs(void) {
90 msg_spy_pump_msg_queue();
91 msg_spy.i_msg = 0;
94 static CWPSTRUCT* msg_spy_find_msg(UINT message) {
95 UINT i;
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",
101 __FILE__, __LINE__);
103 for (i = 0; i < msg_spy.i_msg; i++)
104 if (msg_spy.msgs[i].message == message)
105 return &msg_spy.msgs[i];
107 return NULL;
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;
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";
136 static HWND hwnd;
138 static BOOL init(void) {
139 WNDCLASSEX wc;
140 HIMC imc;
141 HMODULE hmod;
143 hmod = GetModuleHandleA("imm32.dll");
144 pImmAssociateContextEx = (void*)GetProcAddress(hmod, "ImmAssociateContextEx");
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);
159 if (!RegisterClassExA(&wc))
160 return FALSE;
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;
168 imc = ImmGetContext(hwnd);
169 if (!imc)
171 win_skip("IME support not implemented\n");
172 return FALSE;
174 ImmReleaseContext(hwnd, imc);
176 ShowWindow(hwnd, SW_SHOWNORMAL);
177 UpdateWindow(hwnd);
179 msg_spy_init(hwnd);
181 return TRUE;
184 static void cleanup(void) {
185 msg_spy_cleanup();
186 if (hwnd)
187 DestroyWindow(hwnd);
188 UnregisterClass(wndcls, GetModuleHandle(0));
191 static void test_ImmNotifyIME(void) {
192 static const char string[] = "wine";
193 char resstr[16] = "";
194 HIMC imc;
195 BOOL ret;
197 imc = ImmGetContext(hwnd);
198 msg_spy_flush_msgs();
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");
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);
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");
230 msg_spy_flush_msgs();
231 ImmReleaseContext(hwnd, imc);
234 static void test_ImmGetCompositionString(void)
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;
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)
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)
260 HIMC imc;
261 BOOL ret;
263 SetLastError(0xdeadbeef);
264 imc = ImmGetContext(hwnd);
265 ok(imc != 0, "ImmGetContext() failed. Last error: %u\n", GetLastError());
266 if (!imc)
267 return;
269 ret = ImmSetCompositionStringW(imc, SCS_SETSTR, NULL, 0, NULL, 0);
270 ok(broken(!ret) ||
271 ret, /* Vista+ */
272 "ImmSetCompositionStringW() failed.\n");
274 ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGEATTR,
275 NULL, 0, NULL, 0);
276 ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
278 ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGECLAUSE,
279 NULL, 0, NULL, 0);
280 ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
282 ret = ImmSetCompositionStringW(imc, SCS_CHANGEATTR | SCS_CHANGECLAUSE,
283 NULL, 0, NULL, 0);
284 ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
286 ret = ImmSetCompositionStringW(imc, SCS_SETSTR | SCS_CHANGEATTR | SCS_CHANGECLAUSE,
287 NULL, 0, NULL, 0);
288 ok(!ret, "ImmSetCompositionStringW() succeeded.\n");
290 ImmReleaseContext(hwnd, imc);
293 static void test_ImmIME(void)
295 HIMC imc;
297 imc = ImmGetContext(hwnd);
298 if (imc)
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");
306 ImmReleaseContext(hwnd,imc);
309 static void test_ImmAssociateContextEx(void)
311 HIMC imc;
312 BOOL rc;
314 if (!pImmAssociateContextEx) return;
316 imc = ImmGetContext(hwnd);
317 if (imc)
319 HIMC retimc, newimc;
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);
348 START_TEST(imm32) {
349 if (init())
351 test_ImmNotifyIME();
352 test_ImmGetCompositionString();
353 test_ImmSetCompositionString();
354 test_ImmIME();
355 test_ImmAssociateContextEx();
357 cleanup();