comctl32: Fix the updown control test to use the optional flag.
[wine] / dlls / comctl32 / tests / updown.c
1 /* Unit tests for the up-down control
2  *
3  * Copyright 2005 C. Scott Ananian
4  * Copyright (C) 2007 James Hawkins
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 /* TO TEST:
22  *   - send click messages to the up-down control, check the current position
23  *   - up-down control automatically positions itself next to its buddy window
24  *   - up-down control sets the caption of the buddy window
25  *   - test CreateUpDownControl API
26  *   - check UDS_AUTOBUDDY style, up-down control selects previous window in z-order
27  *   - check UDM_SETBUDDY message
28  *   - check UDM_GETBUDDY message
29  *   - up-down control and buddy control must have the same parent
30  *   - up-down control notifies its parent window when its position changes with UDN_DELTAPOS + WM_VSCROLL or WM_HSCROLL
31  *   - check UDS_ALIGN[LEFT,RIGHT]...check that width of buddy window is decreased
32  *   - check that UDS_SETBUDDYINT sets the caption of the buddy window when it is changed
33  *   - check that the thousands operator is set for large numbers
34  *   - check that the thousands operator is not set with UDS_NOTHOUSANDS
35  *   - check UDS_ARROWKEYS, control subclasses the buddy window so that it processes the keys when it has focus
36  *   - check UDS_HORZ
37  *   - check changing past min/max values
38  *   - check UDS_WRAP wraps values past min/max, incrementing past upper value wraps position to lower value
39  *   - can change control's position, min/max pos, radix
40  *   - check UDM_GETPOS, for up-down control with a buddy window, position is the caption of the buddy window, so change the
41  *     caption of the buddy window then call UDM_GETPOS
42  *   - check UDM_SETRANGE, max can be less than min, so clicking the up arrow decreases the current position
43  *   - check UDM_GETRANGE
44  *   - more stuff to test
45  */
46
47 #include <assert.h>
48 #include <windows.h>
49 #include <commctrl.h>
50 #include <stdio.h>
51
52 #include "wine/test.h"
53
54 #define NUM_MSG_SEQUENCES   3
55 #define PARENT_SEQ_INDEX    0
56 #define EDIT_SEQ_INDEX      1
57 #define UPDOWN_SEQ_INDEX    2
58
59 /* undocumented SWP flags - from SDK 3.1 */
60 #define SWP_NOCLIENTSIZE        0x0800
61 #define SWP_NOCLIENTMOVE        0x1000
62
63 static HWND parent_wnd, edit, updown;
64
65 typedef enum
66 {
67     sent = 0x1,
68     posted = 0x2,
69     parent = 0x4,
70     wparam = 0x8,
71     lparam = 0x10,
72     defwinproc = 0x20,
73     beginpaint = 0x40,
74     optional = 0x80,
75     hook = 0x100,
76     winevent_hook =0x200
77 } msg_flags_t;
78
79 struct message
80 {
81     UINT message;       /* the WM_* code */
82     msg_flags_t flags;  /* message props */
83     WPARAM wParam;      /* expected value of wParam */
84     LPARAM lParam;      /* expected value of lParam */
85 };
86
87 struct msg_sequence
88 {
89     int count;
90     int size;
91     struct message *sequence;
92 };
93
94 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
95
96 static const struct message create_parent_wnd_seq[] = {
97     { WM_GETMINMAXINFO, sent },
98     { WM_NCCREATE, sent },
99     { WM_NCCALCSIZE, sent|wparam, 0 },
100     { WM_CREATE, sent },
101     { WM_SHOWWINDOW, sent|wparam, 1 },
102     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
103     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
104     { WM_ACTIVATEAPP, sent|wparam, 1 },
105     { WM_NCACTIVATE, sent|wparam, 1 },
106     { WM_ACTIVATE, sent|wparam, 1 },
107     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
108     { WM_IME_NOTIFY, sent|defwinproc|optional },
109     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
110     /* Win9x adds SWP_NOZORDER below */
111     { WM_WINDOWPOSCHANGED, sent, /*|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE*/ },
112     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
113     { WM_SIZE, sent },
114     { WM_MOVE, sent },
115     { 0 }
116 };
117
118 static const struct message add_edit_to_parent_seq[] = {
119     { WM_PARENTNOTIFY, sent|wparam, WM_CREATE },
120     { 0 }
121 };
122
123 static const struct message add_updown_with_edit_seq[] = {
124     { WM_WINDOWPOSCHANGING, sent },
125     { WM_NCCALCSIZE, sent|wparam, TRUE },
126     { WM_WINDOWPOSCHANGED, sent },
127     { WM_SIZE, sent|wparam|defwinproc, SIZE_RESTORED /*, MAKELONG(91, 75) exact size depends on font */ },
128     { 0 }
129 };
130
131 static const struct message add_updown_to_parent_seq[] = {
132     { WM_NOTIFYFORMAT, sent|lparam, 0, NF_QUERY },
133     { WM_QUERYUISTATE, sent },
134     { WM_PARENTNOTIFY, sent|wparam, MAKELONG(WM_CREATE, WM_CREATE) },
135     { 0 }
136 };
137
138 static const struct message get_edit_text_seq[] = {
139     { WM_GETTEXT, sent },
140     { 0 }
141 };
142
143 static void add_message(int sequence_index, const struct message *msg)
144 {
145     struct msg_sequence *msg_seq = sequences[sequence_index];
146
147     if (!msg_seq->sequence)
148     {
149         msg_seq->size = 10;
150         msg_seq->sequence = HeapAlloc(GetProcessHeap(), 0,
151                                       msg_seq->size * sizeof (struct message));
152     }
153
154     if (msg_seq->count == msg_seq->size)
155     {
156         msg_seq->size *= 2;
157         msg_seq->sequence = HeapReAlloc(GetProcessHeap(), 0,
158                                         msg_seq->sequence,
159                                         msg_seq->size * sizeof (struct message));
160     }
161
162     assert(msg_seq->sequence);
163
164     msg_seq->sequence[msg_seq->count].message = msg->message;
165     msg_seq->sequence[msg_seq->count].flags = msg->flags;
166     msg_seq->sequence[msg_seq->count].wParam = msg->wParam;
167     msg_seq->sequence[msg_seq->count].lParam = msg->lParam;
168
169     msg_seq->count++;
170 }
171
172 static void flush_sequence(int sequence_index)
173 {
174     struct msg_sequence *msg_seq = sequences[sequence_index];
175     HeapFree(GetProcessHeap(), 0, msg_seq->sequence);
176     msg_seq->sequence = NULL;
177     msg_seq->count = msg_seq->size = 0;
178 }
179
180 static void flush_sequences(void)
181 {
182     flush_sequence(PARENT_SEQ_INDEX);
183     flush_sequence(EDIT_SEQ_INDEX);
184     flush_sequence(UPDOWN_SEQ_INDEX);
185 }
186
187 #define ok_sequence(index, exp, contx, todo) \
188         ok_sequence_(index, (exp), (contx), (todo), __FILE__, __LINE__)
189
190
191 static void ok_sequence_(int sequence_index, const struct message *expected,
192                          const char *context, int todo, const char *file, int line)
193 {
194     struct msg_sequence *msg_seq = sequences[sequence_index];
195     static const struct message end_of_sequence = {0, 0, 0, 0};
196     const struct message *actual, *sequence;
197     int failcount = 0;
198
199     add_message(sequence_index, &end_of_sequence);
200
201     sequence = msg_seq->sequence;
202     actual = sequence;
203
204     while (expected->message && actual->message)
205     {
206         trace_( file, line)("expected %04x - actual %04x\n", expected->message, actual->message);
207
208         if (expected->message == actual->message)
209         {
210             if (expected->flags & wparam)
211             {
212                 if (expected->wParam != actual->wParam && todo)
213                 {
214                     todo_wine
215                     {
216                         failcount++;
217                         ok_(file, line) (FALSE,
218                             "%s: in msg 0x%04x expecting wParam 0x%x got 0x%x\n",
219                             context, expected->message, expected->wParam, actual->wParam);
220                     }
221                 }
222                 else
223                 {
224                     ok_(file, line) (expected->wParam == actual->wParam,
225                         "%s: in msg 0x%04x expecting wParam 0x%x got 0x%x\n",
226                         context, expected->message, expected->wParam, actual->wParam);
227                 }
228             }
229
230             if (expected->flags & lparam)
231             {
232                 if (expected->lParam != actual->lParam && todo)
233                 {
234                     todo_wine
235                     {
236                         failcount++;
237                         ok_(file, line) (FALSE,
238                             "%s: in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
239                             context, expected->message, expected->lParam, actual->lParam);
240                     }
241                 }
242                 else
243                 {
244                     ok_(file, line) (expected->lParam == actual->lParam,
245                         "%s: in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
246                         context, expected->message, expected->lParam, actual->lParam);
247                 }
248             }
249
250             if ((expected->flags & defwinproc) != (actual->flags & defwinproc) && todo)
251             {
252                 todo_wine
253                 {
254                     failcount++;
255                     ok_(file, line) (FALSE,
256                         "%s: the msg 0x%04x should %shave been sent by DefWindowProc\n",
257                         context, expected->message, (expected->flags & defwinproc) ? "" : "NOT ");
258                 }
259             }
260             else
261             {
262                 ok_(file, line) ((expected->flags & defwinproc) == (actual->flags & defwinproc),
263                     "%s: the msg 0x%04x should %shave been sent by DefWindowProc\n",
264                     context, expected->message, (expected->flags & defwinproc) ? "" : "NOT ");
265             }
266
267             ok_(file, line) ((expected->flags & beginpaint) == (actual->flags & beginpaint),
268                 "%s: the msg 0x%04x should %shave been sent by BeginPaint\n",
269                 context, expected->message, (expected->flags & beginpaint) ? "" : "NOT ");
270             ok_(file, line) ((expected->flags & (sent|posted)) == (actual->flags & (sent|posted)),
271                 "%s: the msg 0x%04x should have been %s\n",
272                 context, expected->message, (expected->flags & posted) ? "posted" : "sent");
273             ok_(file, line) ((expected->flags & parent) == (actual->flags & parent),
274                 "%s: the msg 0x%04x was expected in %s\n",
275                 context, expected->message, (expected->flags & parent) ? "parent" : "child");
276             ok_(file, line) ((expected->flags & hook) == (actual->flags & hook),
277                 "%s: the msg 0x%04x should have been sent by a hook\n",
278                 context, expected->message);
279             ok_(file, line) ((expected->flags & winevent_hook) == (actual->flags & winevent_hook),
280                 "%s: the msg 0x%04x should have been sent by a winevent hook\n",
281                 context, expected->message);
282             expected++;
283             actual++;
284         }
285         else if (expected->flags & optional)
286             expected++;
287         else if (todo)
288         {
289             failcount++;
290             todo_wine
291             {
292                 ok_(file, line) (FALSE, "%s: the msg 0x%04x was expected, but got msg 0x%04x instead\n",
293                     context, expected->message, actual->message);
294             }
295
296             flush_sequence(sequence_index);
297             return;
298         }
299         else
300         {
301             ok_(file, line) (FALSE, "%s: the msg 0x%04x was expected, but got msg 0x%04x instead\n",
302                 context, expected->message, actual->message);
303             expected++;
304             actual++;
305         }
306     }
307
308     /* skip all optional trailing messages */
309     while (expected->message && ((expected->flags & optional)))
310         expected++;
311
312     if (todo)
313     {
314         todo_wine
315         {
316             if (expected->message || actual->message)
317             {
318                 failcount++;
319                 ok_(file, line) (FALSE, "%s: the msg sequence is not complete: expected %04x - actual %04x\n",
320                     context, expected->message, actual->message);
321             }
322         }
323     }
324     else if (expected->message || actual->message)
325     {
326         ok_(file, line) (FALSE, "%s: the msg sequence is not complete: expected %04x - actual %04x\n",
327             context, expected->message, actual->message);
328     }
329
330     if(todo && !failcount) /* succeeded yet marked todo */
331     {
332         todo_wine
333         {
334             ok_(file, line)(TRUE, "%s: marked \"todo_wine\" but succeeds\n", context);
335         }
336     }
337
338     flush_sequence(sequence_index);
339 }
340
341 static void init_msg_sequences(void)
342 {
343     int i;
344
345     for (i = 0; i < NUM_MSG_SEQUENCES; i++)
346         sequences[i] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct msg_sequence));
347 }
348
349 static LRESULT WINAPI parent_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
350 {
351     static long defwndproc_counter = 0;
352     LRESULT ret;
353     struct message msg;
354
355     /* do not log painting messages */
356     if (message != WM_PAINT &&
357         message != WM_ERASEBKGND &&
358         message != WM_NCPAINT &&
359         message != WM_NCHITTEST &&
360         message != WM_GETTEXT &&
361         message != WM_GETICON &&
362         message != WM_DEVICECHANGE)
363     {
364         trace("parent: %p, %04x, %08x, %08lx\n", hwnd, message, wParam, lParam);
365
366         msg.message = message;
367         msg.flags = sent|wparam|lparam;
368         if (defwndproc_counter) msg.flags |= defwinproc;
369         msg.wParam = wParam;
370         msg.lParam = lParam;
371         add_message(PARENT_SEQ_INDEX, &msg);
372     }
373
374     defwndproc_counter++;
375     ret = DefWindowProcA(hwnd, message, wParam, lParam);
376     defwndproc_counter--;
377
378     return ret;
379 }
380
381 static BOOL register_parent_wnd_class(void)
382 {
383     WNDCLASSA cls;
384
385     cls.style = 0;
386     cls.lpfnWndProc = parent_wnd_proc;
387     cls.cbClsExtra = 0;
388     cls.cbWndExtra = 0;
389     cls.hInstance = GetModuleHandleA(NULL);
390     cls.hIcon = 0;
391     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
392     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
393     cls.lpszMenuName = NULL;
394     cls.lpszClassName = "Up-Down test parent class";
395     return RegisterClassA(&cls);
396 }
397
398 static HWND create_parent_window(void)
399 {
400     if (!register_parent_wnd_class())
401         return NULL;
402
403     return CreateWindowEx(0, "Up-Down test parent class",
404                           "Up-Down test parent window",
405                           WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
406                           WS_MAXIMIZEBOX | WS_VISIBLE,
407                           0, 0, 100, 100,
408                           GetDesktopWindow(), NULL, GetModuleHandleA(NULL), NULL);
409 }
410
411 struct subclass_info
412 {
413     WNDPROC oldproc;
414 };
415
416 static LRESULT WINAPI edit_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
417 {
418     struct subclass_info *info = (struct subclass_info *)GetWindowLongA(hwnd, GWL_USERDATA);
419     static long defwndproc_counter = 0;
420     LRESULT ret;
421     struct message msg;
422
423     trace("edit: %p, %04x, %08x, %08lx\n", hwnd, message, wParam, lParam);
424
425     msg.message = message;
426     msg.flags = sent|wparam|lparam;
427     if (defwndproc_counter) msg.flags |= defwinproc;
428     msg.wParam = wParam;
429     msg.lParam = lParam;
430     add_message(EDIT_SEQ_INDEX, &msg);
431
432     defwndproc_counter++;
433     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
434     defwndproc_counter--;
435     return ret;
436 }
437
438 static HWND create_edit_control()
439 {
440     struct subclass_info *info;
441     RECT rect;
442
443     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
444     if (!info)
445         return NULL;
446
447     GetClientRect(parent_wnd, &rect);
448     edit = CreateWindowExA(0, "EDIT", NULL, WS_CHILD | WS_BORDER | WS_VISIBLE,
449                            0, 0, rect.right, rect.bottom,
450                            parent_wnd, NULL, GetModuleHandleA(NULL), NULL);
451     if (!edit)
452     {
453         HeapFree(GetProcessHeap(), 0, info);
454         return NULL;
455     }
456
457     info->oldproc = (WNDPROC)SetWindowLongA(edit, GWL_WNDPROC,
458                                             (LONG)edit_subclass_proc);
459     SetWindowLongA(edit, GWL_USERDATA, (LONG)info);
460
461     return edit;
462 }
463
464 static LRESULT WINAPI updown_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
465 {
466     struct subclass_info *info = (struct subclass_info *)GetWindowLongA(hwnd, GWL_USERDATA);
467     static long defwndproc_counter = 0;
468     LRESULT ret;
469     struct message msg;
470
471     trace("updown: %p, %04x, %08x, %08lx\n", hwnd, message, wParam, lParam);
472
473     msg.message = message;
474     msg.flags = sent|wparam|lparam;
475     if (defwndproc_counter) msg.flags |= defwinproc;
476     msg.wParam = wParam;
477     msg.lParam = lParam;
478     add_message(EDIT_SEQ_INDEX, &msg);
479
480     defwndproc_counter++;
481     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
482     defwndproc_counter--;
483
484     return ret;
485 }
486
487 static HWND create_updown_control()
488 {
489     struct subclass_info *info;
490     HWND updown;
491     RECT rect;
492
493     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
494     if (!info)
495         return NULL;
496
497     GetClientRect(parent_wnd, &rect);
498     updown = CreateUpDownControl(WS_CHILD | WS_BORDER | WS_VISIBLE | UDS_ALIGNRIGHT,
499                                  0, 0, rect.right, rect.bottom, parent_wnd, 1, GetModuleHandleA(NULL), edit,
500                                  100, 0, 50);
501     if (!updown)
502     {
503         HeapFree(GetProcessHeap(), 0, info);
504         return NULL;
505     }
506
507     info->oldproc = (WNDPROC)SetWindowLongA(updown, GWL_WNDPROC,
508                                             (LONG)updown_subclass_proc);
509     SetWindowLongA(updown, GWL_USERDATA, (LONG)info);
510
511     return updown;
512 }
513
514 static void test_create_updown_control(void)
515 {
516     CHAR text[MAX_PATH];
517
518     parent_wnd = create_parent_window();
519     ok(parent_wnd != NULL, "Failed to create parent window!\n");
520     ok_sequence(PARENT_SEQ_INDEX, create_parent_wnd_seq, "create parent window", TRUE);
521
522     flush_sequences();
523
524     edit = create_edit_control();
525     ok(edit != NULL, "Failed to create edit control\n");
526     ok_sequence(PARENT_SEQ_INDEX, add_edit_to_parent_seq, "add edit control to parent", FALSE);
527
528     flush_sequences();
529
530     updown = create_updown_control();
531     ok(updown != NULL, "Failed to create updown control\n");
532     ok_sequence(PARENT_SEQ_INDEX, add_updown_to_parent_seq, "add updown control to parent", TRUE);
533     ok_sequence(EDIT_SEQ_INDEX, add_updown_with_edit_seq, "add updown control with edit", FALSE);
534
535     flush_sequences();
536
537     GetWindowTextA(edit, text, MAX_PATH);
538     ok(lstrlenA(text) == 0, "Expected empty string\n");
539     ok_sequence(EDIT_SEQ_INDEX, get_edit_text_seq, "get edit text", FALSE);
540
541     flush_sequences();
542 }
543
544 START_TEST(updown)
545 {
546     InitCommonControls();
547     init_msg_sequences();
548
549     test_create_updown_control();
550 }