Avoid excessive heap memory reallocation when generating EMF
[wine] / dlls / user / tests / msg.c
1 /*
2  * Unit tests for window message handling
3  *
4  * Copyright 1999 Ove Kaaven
5  * Copyright 2003 Dimitrie O. Paun
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31
32 #include "wine/test.h"
33
34
35 /*
36 FIXME: add tests for these
37 Window Edge Styles (Win31/Win95/98 look), in order of precedence:
38  WS_EX_DLGMODALFRAME: double border, WS_CAPTION allowed
39  WS_THICKFRAME: thick border
40  WS_DLGFRAME: double border, WS_CAPTION not allowed (but possibly shown anyway)
41  WS_BORDER (default for overlapped windows): single black border
42  none (default for child (and popup?) windows): no border
43 */
44
45 typedef enum { 
46     sent=0x1, posted=0x2, parent=0x4, wparam=0x8, lparam=0x10,
47     defwinproc=0x20
48 } msg_flags_t;
49
50 struct message {
51     UINT message;          /* the WM_* code */
52     msg_flags_t flags;     /* message props */
53     WPARAM wParam;         /* expacted value of wParam */
54     LPARAM lParam;         /* expacted value of lParam */
55 };
56
57 /* CreateWindow (for overlapped window, not initially visible) (16/32) */
58 static struct message WmCreateOverlappedSeq[] = {
59     { WM_GETMINMAXINFO, sent },
60     { WM_NCCREATE, sent },
61     { WM_NCCALCSIZE, sent|wparam, 0 },
62     { WM_CREATE, sent },
63     { 0 }
64 };
65 /* ShowWindow (for overlapped window) (16/32) */
66 static struct message WmShowOverlappedSeq[] = {
67     { WM_SHOWWINDOW, sent|wparam, 1 },
68     { WM_WINDOWPOSCHANGING, sent|wparam, /*FIXME: SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW*/ 0 },
69     /* FIXME: WM_QUERYNEWPALETTE, if in 256-color mode */
70     { WM_WINDOWPOSCHANGING, sent|wparam, /*FIXME: SWP_NOMOVE|SWP_NOSIZE*/ 0 },
71     { WM_ACTIVATEAPP, sent|wparam, 1 },
72     { WM_NCACTIVATE, sent|wparam, 1 },
73     { WM_GETTEXT, sent|defwinproc },
74     { WM_ACTIVATE, sent|wparam, 1 },
75     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
76     { WM_NCPAINT, sent|wparam, 1 },
77     { WM_GETTEXT, sent|defwinproc },
78     { WM_ERASEBKGND, sent },
79     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_SHOWWINDOW },
80     { WM_SIZE, sent },
81     { WM_MOVE, sent },
82     { 0 }
83 };
84
85 /* DestroyWindow (for overlapped window) (32) */
86 static struct message WmDestroyOverlappedSeq[] = {
87     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
88     { WM_WINDOWPOSCHANGED, sent|wparam, 0 },
89     { WM_NCACTIVATE, sent|wparam, 0 },
90     { WM_ACTIVATE, sent|wparam, 0 },
91     { WM_ACTIVATEAPP, sent|wparam, 0 },
92     { WM_KILLFOCUS, sent|wparam, 0 },
93     { WM_DESTROY, sent },
94     { WM_NCDESTROY, sent },
95     { 0 }
96 };
97 /* CreateWindow (for child window, not initially visible) */
98 static struct message WmCreateChildSeq[] = {
99     { WM_NCCREATE, sent }, 
100     /* child is inserted into parent's child list after WM_NCCREATE returns */
101     { WM_NCCALCSIZE, sent|wparam, 0 },
102     { WM_CREATE, sent },
103     { WM_SIZE, sent },
104     { WM_MOVE, sent },
105     { WM_PARENTNOTIFY, sent|parent|wparam, 1 },
106     { 0 }
107 };
108 /* ShowWindow (for child window) */
109 static struct message WmShowChildSeq[] = {
110     { WM_SHOWWINDOW, sent|wparam, 1 },
111     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
112     { WM_ERASEBKGND, sent|parent },
113     { WM_WINDOWPOSCHANGED, sent|wparam, 0 },
114     { 0 }
115 };
116 /* DestroyWindow (for child window) */
117 static struct message WmDestroyChildSeq[] = {
118     { WM_PARENTNOTIFY, sent|parent|wparam, 2 },
119     { WM_SHOWWINDOW, sent|wparam, 0 },
120     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
121     { WM_ERASEBKGND, sent|parent },
122     { WM_WINDOWPOSCHANGED, sent|wparam, 0 },
123     { WM_DESTROY, sent },
124     { WM_NCDESTROY, sent },
125     { 0 }
126 };
127 /* Moving the mouse in nonclient area */
128 static struct message WmMouseMoveInNonClientAreaSeq[] = { /* FIXME: add */
129     { WM_NCHITTEST, sent },
130     { WM_SETCURSOR, sent },
131     { WM_NCMOUSEMOVE, posted },
132     { 0 }
133 };
134 /* Moving the mouse in client area */
135 static struct message WmMouseMoveInClientAreaSeq[] = { /* FIXME: add */
136     { WM_NCHITTEST, sent },
137     { WM_SETCURSOR, sent },
138     { WM_MOUSEMOVE, posted },
139     { 0 }
140 };
141 /* Moving by dragging the title bar (after WM_NCHITTEST and WM_SETCURSOR) (outline move) */
142 static struct message WmDragTitleBarSeq[] = { /* FIXME: add */
143     { WM_NCLBUTTONDOWN, sent|wparam, HTCAPTION },
144     { WM_SYSCOMMAND, sent|defwinproc|wparam, SC_MOVE+2 },
145     { WM_GETMINMAXINFO, sent|defwinproc },
146     { WM_ENTERSIZEMOVE, sent|defwinproc },
147     { WM_WINDOWPOSCHANGING, sent|defwinproc },
148     { WM_WINDOWPOSCHANGED, sent|defwinproc },
149     { WM_MOVE, sent|defwinproc },
150     { WM_EXITSIZEMOVE, sent|defwinproc },
151     { 0 }
152 };
153 /* Sizing by dragging the thick borders (after WM_NCHITTEST and WM_SETCURSOR) (outline move) */
154 static struct message WmDragThinkBordersBarSeq[] = { /* FIXME: add */
155     { WM_NCLBUTTONDOWN, sent|wparam, 0xd },
156     { WM_SYSCOMMAND, sent|defwinproc|wparam, 0xf004 },
157     { WM_GETMINMAXINFO, sent|defwinproc },
158     { WM_ENTERSIZEMOVE, sent|defwinproc },
159     { WM_SIZING, sent|defwinproc|wparam, 4}, /* one for each mouse movement */
160     { WM_WINDOWPOSCHANGING, sent|defwinproc },
161     { WM_GETMINMAXINFO, sent|defwinproc },
162     { WM_NCCALCSIZE, sent|defwinproc|wparam, 1 },
163     { WM_NCPAINT, sent|defwinproc|wparam, 1 },
164     { WM_GETTEXT, sent|defwinproc },
165     { WM_ERASEBKGND, sent|defwinproc },
166     { WM_WINDOWPOSCHANGED, sent|defwinproc },
167     { WM_MOVE, sent|defwinproc },
168     { WM_SIZE, sent|defwinproc },
169     { WM_EXITSIZEMOVE, sent|defwinproc },
170     { 0 }
171 };
172 /* Resizing child window with MoveWindow (32) */
173 static struct message WmResizingChildWithMoveWindowSeq[] = {
174     { WM_WINDOWPOSCHANGING, sent },
175     { WM_NCCALCSIZE, sent|wparam, 1 },
176     { WM_ERASEBKGND, sent },
177     { WM_WINDOWPOSCHANGED, sent },
178     { WM_MOVE, sent|defwinproc },
179     { WM_SIZE, sent|defwinproc },
180     { 0 }
181 };
182 /* Clicking on inactive button */
183 static struct message WmClickInactiveButtonSeq[] = { /* FIXME: add */
184     { WM_NCHITTEST, sent },
185     { WM_PARENTNOTIFY, sent|parent|wparam, WM_LBUTTONDOWN },
186     { WM_MOUSEACTIVATE, sent },
187     { WM_MOUSEACTIVATE, sent|parent|defwinproc },
188     { WM_SETCURSOR, sent },
189     { WM_SETCURSOR, sent|parent|defwinproc },
190     { WM_LBUTTONDOWN, posted },
191     { WM_KILLFOCUS, posted|parent },
192     { WM_SETFOCUS, posted },
193     { WM_CTLCOLORBTN, posted|parent },
194     { BM_SETSTATE, posted },
195     { WM_CTLCOLORBTN, posted|parent },
196     { WM_LBUTTONUP, posted },
197     { BM_SETSTATE, posted },
198     { WM_CTLCOLORBTN, posted|parent },
199     { WM_COMMAND, posted|parent },
200     { 0 }
201 };
202 /* Reparenting a button (16/32) */
203 /* The last child (button) reparented gets topmost for its new parent. */
204 static struct message WmReparentButtonSeq[] = { /* FIXME: add */
205     { WM_SHOWWINDOW, sent|wparam, 0 },
206     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER },
207     { WM_ERASEBKGND, sent|parent },
208     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER },
209     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOZORDER },
210     { WM_CHILDACTIVATE, sent },
211     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOSIZE|SWP_NOREDRAW|SWP_NOZORDER },
212     { WM_MOVE, sent|defwinproc },
213     { WM_SHOWWINDOW, sent|wparam, 1 },
214     { 0 }
215 };
216 /* Creation of a modal dialog (32) */
217 static struct message WmCreateModalDialogSeq[] = { /* FIXME: add */
218     { WM_CANCELMODE, sent|parent },
219     { WM_KILLFOCUS, sent|parent },
220     { WM_ENABLE, sent|parent|wparam, 0 },
221     /* (window proc creation messages not tracked yet, because...) */
222     { WM_SETFONT, sent },
223     { WM_INITDIALOG, sent },
224     /* (...the window proc message hook was installed here, IsVisible still FALSE) */
225     { WM_NCACTIVATE, sent|parent|wparam, 0 },
226     { WM_GETTEXT, sent|defwinproc },
227     { WM_ACTIVATE, sent|parent|wparam, 0 },
228     { WM_WINDOWPOSCHANGING, sent },
229     { WM_WINDOWPOSCHANGING, sent|parent },
230     { WM_NCACTIVATE, sent|wparam, 1 },
231     { WM_ACTIVATE, sent|wparam, 1 },
232     /* (setting focus) */
233     { WM_SHOWWINDOW, sent|wparam, 1 },
234     { WM_WINDOWPOSCHANGING, sent },
235     { WM_NCPAINT, sent },
236     { WM_GETTEXT, sent|defwinproc },
237     { WM_ERASEBKGND, sent },
238     { WM_CTLCOLORDLG, sent|defwinproc },
239     { WM_WINDOWPOSCHANGED, sent },
240     { WM_PAINT, sent },
241     /* FIXME: (bunch of WM_CTLCOLOR* for each control) */
242     { WM_PAINT, sent|parent },
243     { WM_ENTERIDLE, sent|parent|wparam, 0},
244     { WM_SETCURSOR, sent|parent },
245     { 0 }
246 };
247 /* Destruction of a modal dialog (32) */
248 static struct message WmDestroyModalDialogSeq[] = { /* FIXME: add */
249     /* (inside dialog proc: EndDialog is called) */
250     { WM_ENABLE, sent|parent|wparam, 1 },
251     { WM_SETFOCUS, sent },
252     { WM_WINDOWPOSCHANGING, sent },
253     { WM_NCPAINT, sent|parent },
254     { WM_GETTEXT, sent|defwinproc },
255     { WM_ERASEBKGND, sent|parent },
256     { WM_WINDOWPOSCHANGED, sent },
257     { WM_NCACTIVATE, sent|wparam, 0 },
258     { WM_ACTIVATE, sent|wparam, 0 },
259     { WM_WINDOWPOSCHANGING, sent },
260     { WM_WINDOWPOSCHANGING, sent|parent },
261     { WM_NCACTIVATE, sent|parent|wparam, 1 },
262     { WM_GETTEXT, sent|defwinproc },
263     { WM_ACTIVATE, sent|parent|wparam, 1 },
264     { WM_KILLFOCUS, sent },
265     { WM_SETFOCUS, sent|parent },
266     { WM_DESTROY, sent },
267     { WM_NCDESTROY, sent },
268     { 0 }
269 };
270 /* Creation of a modal dialog that is resized inside WM_INITDIALOG (32) */
271 static struct message WmCreateModalDialogResizeSeq[] = { /* FIXME: add */
272     /* (inside dialog proc, handling WM_INITDIALOG) */
273     { WM_WINDOWPOSCHANGING, sent },
274     { WM_NCCALCSIZE, sent },
275     { WM_NCACTIVATE, sent|parent|wparam, 0 },
276     { WM_GETTEXT, sent|defwinproc },
277     { WM_ACTIVATE, sent|parent|wparam, 0 },
278     { WM_WINDOWPOSCHANGING, sent },
279     { WM_WINDOWPOSCHANGING, sent|parent },
280     { WM_NCACTIVATE, sent|wparam, 1 },
281     { WM_ACTIVATE, sent|wparam, 1 },
282     { WM_WINDOWPOSCHANGED, sent },
283     { WM_SIZE, sent|defwinproc },
284     /* (setting focus) */
285     { WM_SHOWWINDOW, sent|wparam, 1 },
286     { WM_WINDOWPOSCHANGING, sent },
287     { WM_NCPAINT, sent },
288     { WM_GETTEXT, sent|defwinproc },
289     { WM_ERASEBKGND, sent },
290     { WM_CTLCOLORDLG, sent|defwinproc },
291     { WM_WINDOWPOSCHANGED, sent },
292     { WM_PAINT, sent },
293     /* (bunch of WM_CTLCOLOR* for each control) */
294     { WM_PAINT, sent|parent },
295     { WM_ENTERIDLE, sent|parent|wparam, 0 },
296     { WM_SETCURSOR, sent|parent },
297     { 0 }
298 };
299
300 static int sequence_cnt, sequence_size;
301 static struct message* sequence;
302
303 static void add_message(struct message msg)
304 {
305     if (!sequence) 
306         sequence = malloc ( (sequence_size = 10) * sizeof (struct message) );
307     if (sequence_cnt == sequence_size) 
308         sequence = realloc ( sequence, (sequence_size *= 2) * sizeof (struct message) );
309     assert(sequence);
310     sequence[sequence_cnt++] = msg;
311 }
312
313 static void flush_sequence()
314 {
315     free(sequence);
316     sequence = 0;
317     sequence_cnt = sequence_size = 0;
318 }
319
320 static void ok_sequence(struct message *expected, const char *context)
321 {
322     static struct message end_of_sequence = { 0, 0, 0, 0 };
323     struct message *actual = sequence;
324     
325     add_message(end_of_sequence);
326
327     /* naive sequence comparison. Would be nice to use a regexp engine here */
328     while (expected->message || actual->message)
329     {
330         if (expected->message == actual->message)
331         {
332             if (expected->flags & wparam)
333                  ok (expected->wParam == actual->wParam,
334                      "%s: in msg 0x%04x expecting wParam 0x%x got 0x%x\n",
335                      context, expected->message, expected->wParam, actual->wParam);
336             if (expected->flags & lparam)
337                  ok (expected->lParam == actual->lParam,
338                      "%s: in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
339                      context, expected->message, expected->lParam, actual->lParam);
340             /* FIXME: should we check defwinproc? */
341             ok ((expected->flags & (sent|posted)) == (actual->flags & (sent|posted)),
342                 "%s: the msg 0x%04x should have been %s\n",
343                 context, expected->message, (expected->flags & posted) ? "posted" : "sent");
344             ok ((expected->flags & parent) == (actual->flags & parent),
345                 "%s: the msg 0x%04x was expected in %s\n",
346                 context, expected->message, (expected->flags & parent) ? "parent" : "child");
347             expected++;
348             actual++;
349         }
350         else if (expected->message && ((expected + 1)->message == actual->message) )
351         {
352           todo_wine {
353             ok (FALSE, "%s: the msg 0x%04x was not received\n", context, expected->message);
354             expected++;
355           }
356         }
357         else if (actual->message && (expected->message == (actual + 1)->message) )
358         {
359           todo_wine {
360             ok (FALSE, "%s: the msg 0x%04x was not expected\n", context, actual->message);
361             actual++;
362           }
363         }
364         else
365         {
366           todo_wine {
367             ok (FALSE, "%s: the msg 0x%04x was expected, but got msg 0x%04x instead\n",
368                 context, expected->message, actual->message);
369             expected++;
370             actual++;
371           }
372         }
373     }
374
375     flush_sequence();
376 }
377
378 /* test if we receive the right sequence of messages */
379 static void test_messages(void)
380 {
381     HWND hwnd, hparent, hchild;
382     HWND hchild2, hbutton;
383
384     hwnd = CreateWindowExA(0, "TestWindowClass", "Test overlapped", WS_OVERLAPPEDWINDOW,
385                            100, 100, 200, 200, 0, 0, 0, NULL);
386     ok (hwnd != 0, "Failed to create overlapped window\n");
387     ok_sequence(WmCreateOverlappedSeq, "CreateWindow:overlapped");
388     
389     ShowWindow(hwnd, TRUE);
390     ok_sequence(WmShowOverlappedSeq, "ShowWindow:overlapped");
391
392     DestroyWindow(hwnd);
393     ok_sequence(WmDestroyOverlappedSeq, "DestroyWindow:overlapped");
394
395     hparent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW,
396                               100, 100, 200, 200, 0, 0, 0, NULL);
397     ok (hparent != 0, "Failed to create parent window\n");
398     flush_sequence();
399
400     hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILDWINDOW,
401                              0, 0, 10, 10, hparent, 0, 0, NULL);
402     ok (hchild != 0, "Failed to create child window\n");
403     ok_sequence(WmCreateChildSeq, "CreateWindow:child");
404     
405     hchild2 = CreateWindowExA(0, "SimpleWindowClass", "Test child2", WS_CHILDWINDOW,
406                                100, 100, 50, 50, hparent, 0, 0, NULL);
407     ok (hchild2 != 0, "Failed to create child2 window\n");
408     flush_sequence();
409
410     hbutton = CreateWindowExA(0, "TestWindowClass", "Test button", WS_CHILDWINDOW,
411                               0, 100, 50, 50, hchild, 0, 0, NULL);
412     ok (hbutton != 0, "Failed to create button window\n");
413     flush_sequence();
414
415     ShowWindow(hchild, TRUE);
416     ok_sequence(WmShowChildSeq, "ShowWindow:child");
417
418     MoveWindow(hchild, 10, 10, 20, 20, TRUE);
419     ok_sequence(WmResizingChildWithMoveWindowSeq, "MoveWindow:child");
420
421     DestroyWindow(hchild);
422     ok_sequence(WmDestroyChildSeq, "DestroyWindow:child");
423 }
424
425 static LRESULT WINAPI MsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
426 {
427     struct message msg = { message, sent|wparam|lparam, wParam, lParam };
428
429     add_message(msg);
430     return DefWindowProcA(hwnd, message, wParam, lParam);
431 }
432
433 static BOOL RegisterWindowClasses(void)
434 {
435     WNDCLASSA cls;
436
437     cls.style = 0;
438     cls.lpfnWndProc = MsgCheckProcA;
439     cls.cbClsExtra = 0;
440     cls.cbWndExtra = 0;
441     cls.hInstance = GetModuleHandleA(0);
442     cls.hIcon = 0;
443     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
444     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
445     cls.lpszMenuName = NULL;
446     cls.lpszClassName = "TestWindowClass";
447
448     if(!RegisterClassA(&cls)) return FALSE;
449
450     cls.style = 0;
451     cls.lpfnWndProc = DefWindowProcA;
452     cls.cbClsExtra = 0;
453     cls.cbWndExtra = 0;
454     cls.hInstance = GetModuleHandleA(0);
455     cls.hIcon = 0;
456     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
457     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
458     cls.lpszMenuName = NULL;
459     cls.lpszClassName = "TestParentClass";
460
461     if(!RegisterClassA(&cls)) return FALSE;
462
463     cls.style = 0;
464     cls.lpfnWndProc = DefWindowProcA;
465     cls.cbClsExtra = 0;
466     cls.cbWndExtra = 0;
467     cls.hInstance = GetModuleHandleA(0);
468     cls.hIcon = 0;
469     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
470     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
471     cls.lpszMenuName = NULL;
472     cls.lpszClassName = "SimpleWindowClass";
473
474     if(!RegisterClassA(&cls)) return FALSE;
475
476     return TRUE;
477 }
478
479 START_TEST(msg)
480 {
481     if (!RegisterWindowClasses()) assert(0);
482
483     test_messages();
484 }