quartz: Make dwSamplesProcessed a longlong.
[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 /*
30  * msgspy - record and analyse message traces sent to a certain window
31  */
32 static struct _msg_spy {
33     HWND         hwnd;
34     HHOOK        get_msg_hook;
35     HHOOK        call_wnd_proc_hook;
36     CWPSTRUCT    msgs[32];
37     unsigned int i_msg;
38 } msg_spy;
39
40 static LRESULT CALLBACK get_msg_filter(int nCode, WPARAM wParam, LPARAM lParam)
41 {
42     if (HC_ACTION == nCode) {
43         MSG *msg = (MSG*)lParam;
44
45         if ((msg->hwnd == msg_spy.hwnd) &&
46             (msg_spy.i_msg < NUMELEMS(msg_spy.msgs)))
47         {
48             msg_spy.msgs[msg_spy.i_msg].hwnd    = msg->hwnd;
49             msg_spy.msgs[msg_spy.i_msg].message = msg->message;
50             msg_spy.msgs[msg_spy.i_msg].wParam  = msg->wParam;
51             msg_spy.msgs[msg_spy.i_msg].lParam  = msg->lParam;
52             msg_spy.i_msg++;
53         }
54     }
55
56     return CallNextHookEx(msg_spy.get_msg_hook, nCode, wParam, lParam);
57 }
58
59 static LRESULT CALLBACK call_wnd_proc_filter(int nCode, WPARAM wParam,
60                                              LPARAM lParam)
61 {
62     if (HC_ACTION == nCode) {
63         CWPSTRUCT *cwp = (CWPSTRUCT*)lParam;
64
65         if ((cwp->hwnd == msg_spy.hwnd) &&
66             (msg_spy.i_msg < NUMELEMS(msg_spy.msgs)))
67         {
68             memcpy(&msg_spy.msgs[msg_spy.i_msg], cwp, sizeof(msg_spy.msgs[0]));
69             msg_spy.i_msg++;
70         }
71     }
72
73     return CallNextHookEx(msg_spy.call_wnd_proc_hook, nCode, wParam, lParam);
74 }
75
76 static void msg_spy_pump_msg_queue(void) {
77     MSG msg;
78
79     while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
80         TranslateMessage(&msg);
81         DispatchMessage(&msg);
82     }
83
84     return;
85 }
86
87 static void msg_spy_flush_msgs(void) {
88     msg_spy_pump_msg_queue();
89     msg_spy.i_msg = 0;
90 }
91
92 static CWPSTRUCT* msg_spy_find_msg(UINT message) {
93     int i;
94
95     msg_spy_pump_msg_queue();
96
97     if (msg_spy.i_msg >= NUMELEMS(msg_spy.msgs))
98         fprintf(stdout, "%s:%d: msg_spy: message buffer overflow!\n",
99                 __FILE__, __LINE__);
100
101     for (i = 0; i < msg_spy.i_msg; i++)
102         if (msg_spy.msgs[i].message == message)
103             return &msg_spy.msgs[i];
104
105     return NULL;
106 }
107
108 static void msg_spy_init(HWND hwnd) {
109     msg_spy.hwnd = hwnd;
110     msg_spy.get_msg_hook =
111             SetWindowsHookEx(WH_GETMESSAGE, get_msg_filter, GetModuleHandle(0),
112                              GetCurrentThreadId());
113     msg_spy.call_wnd_proc_hook =
114             SetWindowsHookEx(WH_CALLWNDPROC, call_wnd_proc_filter,
115                              GetModuleHandle(0), GetCurrentThreadId());
116     msg_spy.i_msg = 0;
117
118     msg_spy_flush_msgs();
119 }
120
121 static void msg_spy_cleanup() {
122     if (msg_spy.get_msg_hook)
123         UnhookWindowsHookEx(msg_spy.get_msg_hook);
124     if (msg_spy.call_wnd_proc_hook)
125         UnhookWindowsHookEx(msg_spy.call_wnd_proc_hook);
126     memset(&msg_spy, 0, sizeof(msg_spy));
127 }
128
129 /*
130  * imm32 test cases - Issue some IMM commands on a dummy window and analyse the
131  * messages being sent to this window in response.
132  */
133 static const char wndcls[] = "winetest_imm32_wndcls";
134 static HWND hwnd;
135
136 static int init(void) {
137     WNDCLASSEX wc;
138
139     wc.cbSize        = sizeof(WNDCLASSEX);
140     wc.style         = 0;
141     wc.lpfnWndProc   = DefWindowProc;
142     wc.cbClsExtra    = 0;
143     wc.cbWndExtra    = 0;
144     wc.hInstance     = GetModuleHandle(0);
145     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
146     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
147     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
148     wc.lpszMenuName  = NULL;
149     wc.lpszClassName = wndcls;
150     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
151
152     if (!RegisterClassExA(&wc))
153         return 0;
154
155     hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, wndcls, "Wine imm32.dll test",
156                           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
157                           240, 120, NULL, NULL, GetModuleHandle(0), NULL);
158     if (!hwnd)
159         return 0;
160
161     ShowWindow(hwnd, SW_SHOWNORMAL);
162     UpdateWindow(hwnd);
163
164     msg_spy_init(hwnd);
165
166     return 1;
167 }
168
169 static void cleanup(void) {
170     msg_spy_cleanup();
171     if (hwnd)
172         DestroyWindow(hwnd);
173     UnregisterClass(wndcls, GetModuleHandle(0));
174 }
175
176 static int test_ImmNotifyIME(void) {
177     static const char string[] = "wine";
178     char resstr[16] = "";
179     HIMC imc;
180     BOOL ret;
181
182     imc = ImmGetContext(hwnd);
183     msg_spy_flush_msgs();
184
185     todo_wine
186     {
187         ok(!ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0), "Canceling an "
188            "empty composition string should fail.\n");
189     }
190     ok(!msg_spy_find_msg(WM_IME_COMPOSITION), "Windows does not post "
191        "WM_IME_COMPOSITION in response to NI_COMPOSITIONSTR / CPS_CANCEL, if "
192        "the composition string being canceled is empty.\n");
193
194     ImmSetCompositionString(imc, SCS_SETSTR, string, sizeof(string), NULL, 0);
195     msg_spy_flush_msgs();
196
197     ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
198     ok(!msg_spy_find_msg(WM_IME_COMPOSITION), "Windows does not post "
199        "WM_IME_COMPOSITION in response to NI_COMPOSITIONSTR / CPS_CANCEL, if "
200        "the composition string being canceled is non empty.\n");
201
202     /* behavior differs between win9x and NT */
203     ret = ImmGetCompositionString(imc, GCS_COMPSTR, resstr, sizeof(resstr));
204     ok(ret || !ret, "You'll never read this.\n");
205
206     msg_spy_flush_msgs();
207
208     todo_wine
209     {
210         ok(!ImmNotifyIME(imc, NI_COMPOSITIONSTR, CPS_CANCEL, 0), "Canceling an "
211            "empty composition string should fail.\n");
212     }
213     ok(!msg_spy_find_msg(WM_IME_COMPOSITION), "Windows does not post "
214        "WM_IME_COMPOSITION in response to NI_COMPOSITIONSTR / CPS_CANCEL, if "
215        "the composition string being canceled is empty.\n");
216
217     msg_spy_flush_msgs();
218     ImmReleaseContext(hwnd, imc);
219
220     return 0;
221 }
222
223 START_TEST(imm32) {
224     if (init())
225         test_ImmNotifyIME();
226     cleanup();
227 }