comctl32: Move up-down msg seq test functions into msg.c.
[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  * Copyright (C) 2007 Leslie Choong
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 /* TO TEST:
23  *   - send click messages to the up-down control, check the current position
24  *   - up-down control automatically positions itself next to its buddy window
25  *   - up-down control sets the caption of the buddy window
26  *   - test CreateUpDownControl API
27  *   - check UDS_AUTOBUDDY style, up-down control selects previous window in z-order
28  *   - check UDM_SETBUDDY message
29  *   - check UDM_GETBUDDY message
30  *   - up-down control and buddy control must have the same parent
31  *   - up-down control notifies its parent window when its position changes with UDN_DELTAPOS + WM_VSCROLL or WM_HSCROLL
32  *   - check UDS_ALIGN[LEFT,RIGHT]...check that width of buddy window is decreased
33  *   - check that UDS_SETBUDDYINT sets the caption of the buddy window when it is changed
34  *   - check that the thousands operator is set for large numbers
35  *   - check that the thousands operator is not set with UDS_NOTHOUSANDS
36  *   - check UDS_ARROWKEYS, control subclasses the buddy window so that it processes the keys when it has focus
37  *   - check UDS_HORZ
38  *   - check changing past min/max values
39  *   - check UDS_WRAP wraps values past min/max, incrementing past upper value wraps position to lower value
40  *   - can change control's position, min/max pos, radix
41  *   - check UDM_GETPOS, for up-down control with a buddy window, position is the caption of the buddy window, so change the
42  *     caption of the buddy window then call UDM_GETPOS
43  *   - check UDM_SETRANGE, max can be less than min, so clicking the up arrow decreases the current position
44  *   - check UDM_GETRANGE
45  *   - more stuff to test
46  */
47
48 #include <assert.h>
49 #include <windows.h>
50 #include <commctrl.h>
51 #include <stdio.h>
52
53 #include "wine/test.h"
54 #include "msg.h"
55
56 #define NUM_MSG_SEQUENCES   3
57 #define PARENT_SEQ_INDEX    0
58 #define EDIT_SEQ_INDEX      1
59 #define UPDOWN_SEQ_INDEX    2
60
61 static HWND parent_wnd, edit, updown;
62
63 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
64
65 static const struct message create_parent_wnd_seq[] = {
66     { WM_GETMINMAXINFO, sent },
67     { WM_NCCREATE, sent },
68     { WM_NCCALCSIZE, sent|wparam, 0 },
69     { WM_CREATE, sent },
70     { WM_SHOWWINDOW, sent|wparam, 1 },
71     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
72     { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
73     { WM_ACTIVATEAPP, sent|wparam, 1 },
74     { WM_NCACTIVATE, sent|wparam, 1 },
75     { WM_ACTIVATE, sent|wparam, 1 },
76     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
77     { WM_IME_NOTIFY, sent|defwinproc|optional },
78     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
79     /* Win9x adds SWP_NOZORDER below */
80     { WM_WINDOWPOSCHANGED, sent, /*|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE*/ },
81     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
82     { WM_SIZE, sent },
83     { WM_MOVE, sent },
84     { 0 }
85 };
86
87 static const struct message add_edit_to_parent_seq[] = {
88     { WM_PARENTNOTIFY, sent|wparam, WM_CREATE },
89     { 0 }
90 };
91
92 static const struct message add_updown_with_edit_seq[] = {
93     { WM_WINDOWPOSCHANGING, sent },
94     { WM_NCCALCSIZE, sent|wparam, TRUE },
95     { WM_WINDOWPOSCHANGED, sent },
96     { WM_SIZE, sent|wparam|defwinproc, SIZE_RESTORED /*, MAKELONG(91, 75) exact size depends on font */ },
97     { 0 }
98 };
99
100 static const struct message add_updown_to_parent_seq[] = {
101     { WM_NOTIFYFORMAT, sent|lparam, 0, NF_QUERY },
102     { WM_QUERYUISTATE, sent },
103     { WM_PARENTNOTIFY, sent|wparam, MAKELONG(WM_CREATE, WM_CREATE) },
104     { 0 }
105 };
106
107 static const struct message get_edit_text_seq[] = {
108     { WM_GETTEXT, sent },
109     { 0 }
110 };
111
112 static LRESULT WINAPI parent_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
113 {
114     static long defwndproc_counter = 0;
115     LRESULT ret;
116     struct message msg;
117
118     /* do not log painting messages */
119     if (message != WM_PAINT &&
120         message != WM_ERASEBKGND &&
121         message != WM_NCPAINT &&
122         message != WM_NCHITTEST &&
123         message != WM_GETTEXT &&
124         message != WM_GETICON &&
125         message != WM_DEVICECHANGE)
126     {
127         trace("parent: %p, %04x, %08x, %08lx\n", hwnd, message, wParam, lParam);
128
129         msg.message = message;
130         msg.flags = sent|wparam|lparam;
131         if (defwndproc_counter) msg.flags |= defwinproc;
132         msg.wParam = wParam;
133         msg.lParam = lParam;
134         add_message(sequences, PARENT_SEQ_INDEX, &msg);
135     }
136
137     defwndproc_counter++;
138     ret = DefWindowProcA(hwnd, message, wParam, lParam);
139     defwndproc_counter--;
140
141     return ret;
142 }
143
144 static BOOL register_parent_wnd_class(void)
145 {
146     WNDCLASSA cls;
147
148     cls.style = 0;
149     cls.lpfnWndProc = parent_wnd_proc;
150     cls.cbClsExtra = 0;
151     cls.cbWndExtra = 0;
152     cls.hInstance = GetModuleHandleA(NULL);
153     cls.hIcon = 0;
154     cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
155     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
156     cls.lpszMenuName = NULL;
157     cls.lpszClassName = "Up-Down test parent class";
158     return RegisterClassA(&cls);
159 }
160
161 static HWND create_parent_window(void)
162 {
163     if (!register_parent_wnd_class())
164         return NULL;
165
166     return CreateWindowEx(0, "Up-Down test parent class",
167                           "Up-Down test parent window",
168                           WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
169                           WS_MAXIMIZEBOX | WS_VISIBLE,
170                           0, 0, 100, 100,
171                           GetDesktopWindow(), NULL, GetModuleHandleA(NULL), NULL);
172 }
173
174 struct subclass_info
175 {
176     WNDPROC oldproc;
177 };
178
179 static LRESULT WINAPI edit_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
180 {
181     struct subclass_info *info = (struct subclass_info *)GetWindowLongA(hwnd, GWL_USERDATA);
182     static long defwndproc_counter = 0;
183     LRESULT ret;
184     struct message msg;
185
186     trace("edit: %p, %04x, %08x, %08lx\n", hwnd, message, wParam, lParam);
187
188     msg.message = message;
189     msg.flags = sent|wparam|lparam;
190     if (defwndproc_counter) msg.flags |= defwinproc;
191     msg.wParam = wParam;
192     msg.lParam = lParam;
193     add_message(sequences, EDIT_SEQ_INDEX, &msg);
194
195     defwndproc_counter++;
196     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
197     defwndproc_counter--;
198     return ret;
199 }
200
201 static HWND create_edit_control()
202 {
203     struct subclass_info *info;
204     RECT rect;
205
206     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
207     if (!info)
208         return NULL;
209
210     GetClientRect(parent_wnd, &rect);
211     edit = CreateWindowExA(0, "EDIT", NULL, WS_CHILD | WS_BORDER | WS_VISIBLE,
212                            0, 0, rect.right, rect.bottom,
213                            parent_wnd, NULL, GetModuleHandleA(NULL), NULL);
214     if (!edit)
215     {
216         HeapFree(GetProcessHeap(), 0, info);
217         return NULL;
218     }
219
220     info->oldproc = (WNDPROC)SetWindowLongA(edit, GWL_WNDPROC,
221                                             (LONG)edit_subclass_proc);
222     SetWindowLongA(edit, GWL_USERDATA, (LONG)info);
223
224     return edit;
225 }
226
227 static LRESULT WINAPI updown_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
228 {
229     struct subclass_info *info = (struct subclass_info *)GetWindowLongA(hwnd, GWL_USERDATA);
230     static long defwndproc_counter = 0;
231     LRESULT ret;
232     struct message msg;
233
234     trace("updown: %p, %04x, %08x, %08lx\n", hwnd, message, wParam, lParam);
235
236     msg.message = message;
237     msg.flags = sent|wparam|lparam;
238     if (defwndproc_counter) msg.flags |= defwinproc;
239     msg.wParam = wParam;
240     msg.lParam = lParam;
241     add_message(sequences, EDIT_SEQ_INDEX, &msg);
242
243     defwndproc_counter++;
244     ret = CallWindowProcA(info->oldproc, hwnd, message, wParam, lParam);
245     defwndproc_counter--;
246
247     return ret;
248 }
249
250 static HWND create_updown_control()
251 {
252     struct subclass_info *info;
253     HWND updown;
254     RECT rect;
255
256     info = HeapAlloc(GetProcessHeap(), 0, sizeof(struct subclass_info));
257     if (!info)
258         return NULL;
259
260     GetClientRect(parent_wnd, &rect);
261     updown = CreateUpDownControl(WS_CHILD | WS_BORDER | WS_VISIBLE | UDS_ALIGNRIGHT,
262                                  0, 0, rect.right, rect.bottom, parent_wnd, 1, GetModuleHandleA(NULL), edit,
263                                  100, 0, 50);
264     if (!updown)
265     {
266         HeapFree(GetProcessHeap(), 0, info);
267         return NULL;
268     }
269
270     info->oldproc = (WNDPROC)SetWindowLongA(updown, GWL_WNDPROC,
271                                             (LONG)updown_subclass_proc);
272     SetWindowLongA(updown, GWL_USERDATA, (LONG)info);
273
274     return updown;
275 }
276
277 static void test_updown_pos(void)
278 {
279     int r;
280
281     /* Set Range from 0 to 100 */
282     SendMessage(updown, UDM_SETRANGE, 0 , MAKELONG(100,0) );
283     r = SendMessage(updown, UDM_GETRANGE, 0,0);
284     ok(LOWORD(r) == 100, "Expected 100, got %d\n", LOWORD(r));
285     ok(HIWORD(r) == 0, "Expected 0, got %d\n", HIWORD(r));
286
287     /* Set the position to 5, return is not checked as it was set before func call */
288     SendMessage(updown, UDM_SETPOS, 0 , MAKELONG(5,0) );
289     /* Since UDM_SETBUDDYINT was not set at creation HIWORD(r) will always be 1 as a return from UDM_GETPOS */
290     /* Get the position, which should be 5 */
291     r = SendMessage(updown, UDM_GETPOS, 0 , 0 );
292     ok(LOWORD(r) == 5, "Expected 5, got %d\n", LOWORD(r));
293     ok(HIWORD(r) == 1, "Expected 1, got %d\n", HIWORD(r));
294
295     /* Set the position to 0, return should be 5 */
296     r = SendMessage(updown, UDM_SETPOS, 0 , MAKELONG(0,0) );
297     ok(r == 5, "Expected 5, got %d\n", r);
298     /* Get the position, which should be 0 */
299     r = SendMessage(updown, UDM_GETPOS, 0 , 0 );
300     ok(LOWORD(r) == 0, "Expected 0, got %d\n", LOWORD(r));
301     ok(HIWORD(r) == 1, "Expected 1, got %d\n", HIWORD(r));
302
303     /* Set the position to -1, return should be 0 */
304     r = SendMessage(updown, UDM_SETPOS, 0 , MAKELONG(-1,0) );
305     ok(r == 0, "Expected 0, got %d\n", r);
306     /* Get the position, which should be 0 */
307     r = SendMessage(updown, UDM_GETPOS, 0 , 0 );
308     ok(LOWORD(r) == 0, "Expected 0, got %d\n", LOWORD(r));
309     ok(HIWORD(r) == 1, "Expected 1, got %d\n", HIWORD(r));
310
311     /* Set the position to 100, return should be 0 */
312     r = SendMessage(updown, UDM_SETPOS, 0 , MAKELONG(100,0) );
313     ok(r == 0, "Expected 0, got %d\n", r);
314     /* Get the position, which should be 100 */
315     r = SendMessage(updown, UDM_GETPOS, 0 , 0 );
316     ok(LOWORD(r) == 100, "Expected 100, got %d\n", LOWORD(r));
317     ok(HIWORD(r) == 1, "Expected 1, got %d\n", HIWORD(r));
318
319     /* Set the position to 101, return should be 100 */
320     r = SendMessage(updown, UDM_SETPOS, 0 , MAKELONG(101,0) );
321     ok(r == 100, "Expected 100, got %d\n", r);
322     /* Get the position, which should be 100 */
323     r = SendMessage(updown, UDM_GETPOS, 0 , 0 );
324     ok(LOWORD(r) == 100, "Expected 100, got %d\n", LOWORD(r));
325     ok(HIWORD(r) == 1, "Expected 1, got %d\n", HIWORD(r));
326 }
327
328 static void test_updown_pos32(void)
329 {
330     int r;
331     int low, high;
332
333     /* Set the position to 0 to 1000 */
334     SendMessage(updown, UDM_SETRANGE32, 0 , 1000 );
335
336     r = SendMessage(updown, UDM_GETRANGE32, (WPARAM) &low , (LPARAM) &high );
337     ok(low == 0, "Expected 0, got %d\n", low);
338     ok(high == 1000, "Expected 1000, got %d\n", high);
339
340     /* Set position to 500, don't check return since it is unset*/
341     r = SendMessage(updown, UDM_SETPOS32, 0 , 500 );
342
343     /* Since UDM_SETBUDDYINT was not set at creation bRet will always be true as a return from UDM_GETPOS32 */
344
345     r = SendMessage(updown, UDM_GETPOS32, 0 , (LPARAM) &high );
346     ok(r == 500, "Expected 500, got %d\n", r);
347     ok(high == 1 , "Expected 0, got %d\n", high);
348
349     /* Set position to 0, return should be 500 */
350     r = SendMessage(updown, UDM_SETPOS32, 0 , 0 );
351     ok(r == 500, "Expected 500, got %d\n", r);
352     r = SendMessage(updown, UDM_GETPOS32, 0 , (LPARAM) &high );
353     ok(r == 0, "Expected 0, got %d\n", r);
354     ok(high == 1 , "Expected 0, got %d\n", high);
355
356     /* Set position to -1 which sohuld become 0, return should be 0 */
357     r = SendMessage(updown, UDM_SETPOS32, 0 , -1 );
358     ok(r == 0, "Expected 0, got %d\n", r);
359     r = SendMessage(updown, UDM_GETPOS32, 0 , (LPARAM) &high );
360     ok(r == 0, "Expected 0, got %d\n", r);
361     ok(high == 1 , "Expected 0, got %d\n", high);
362
363     /* Set position to 1000, return should be 0 */
364     r = SendMessage(updown, UDM_SETPOS32, 0 , 1000 );
365     ok(r == 0, "Expected 0, got %d\n", r);
366     r = SendMessage(updown, UDM_GETPOS32, 0 , (LPARAM) &high );
367     ok(r == 1000, "Expected 1000, got %d\n", r);
368     ok(high == 1 , "Expected 0, got %d\n", high);
369
370     /* Set position to 1001 which sohuld become 1000, return should be 1000 */
371     r = SendMessage(updown, UDM_SETPOS32, 0 , 1001 );
372     ok(r == 1000, "Expected 1000, got %d\n", r);
373     r = SendMessage(updown, UDM_GETPOS32, 0 , (LPARAM) &high );
374     ok(r == 1000, "Expected 0, got %d\n", r);
375     ok(high == 1 , "Expected 0, got %d\n", high);
376 }
377
378 static void test_updown_buddy(void)
379 {
380     HWND buddyReturn;
381
382     buddyReturn = (HWND)SendMessage(updown, UDM_GETBUDDY, 0 , 0 );
383     ok(buddyReturn == edit, "Expected edit handle\n");
384 }
385
386 static void test_updown_base(void)
387 {
388     int r;
389
390     SendMessage(updown, UDM_SETBASE, 10 , 0);
391     r = SendMessage(updown, UDM_GETBASE, 0 , 0);
392     ok(r == 10, "Expected 10, got %d\n", r);
393
394     /* Set base to an invalid value, should return 0 and stay at 10 */
395     r = SendMessage(updown, UDM_SETBASE, 80 , 0);
396     ok(r == 0, "Expected 0, got %d\n", r);
397     r = SendMessage(updown, UDM_GETBASE, 0 , 0);
398     ok(r == 10, "Expected 10, got %d\n", r);
399
400     /* Set base to 16 now, should get 16 as the return */
401     r = SendMessage(updown, UDM_SETBASE, 16 , 0);
402     ok(r == 10, "Expected 10, got %d\n", r);
403     r = SendMessage(updown, UDM_GETBASE, 0 , 0);
404     ok(r == 16, "Expected 16, got %d\n", r);
405
406     /* Set base to an invalid value, should return 0 and stay at 16 */
407     r = SendMessage(updown, UDM_SETBASE, 80 , 0);
408     ok(r == 0, "Expected 0, got %d\n", r);
409     r = SendMessage(updown, UDM_GETBASE, 0 , 0);
410     ok(r == 16, "Expected 16, got %d\n", r);
411
412     /* Set base back to 10, return should be 16 */
413     r = SendMessage(updown, UDM_SETBASE, 10 , 0);
414     ok(r == 16, "Expected 16, got %d\n", r);
415     r = SendMessage(updown, UDM_GETBASE, 0 , 0);
416     ok(r == 10, "Expected 10, got %d\n", r);
417 }
418
419 static void test_updown_unicode(void)
420 {
421     int r;
422
423     /* Set it to ANSI, don't check return as we don't know previous state */
424     SendMessage(updown, UDM_SETUNICODEFORMAT, 0 , 0);
425     r = SendMessage(updown, UDM_GETUNICODEFORMAT, 0 , 0);
426     ok(r == 0, "Expected 0, got %d\n", r);
427
428     /* Now set it to Unicode format */
429     r = SendMessage(updown, UDM_SETUNICODEFORMAT, 1 , 0);
430     ok(r == 0, "Expected 0, got %d\n", r);
431     r = SendMessage(updown, UDM_GETUNICODEFORMAT, 0 , 0);
432     ok(r == 1, "Expected 1, got %d\n", r);
433
434     /* And now set it back to ANSI */
435     r = SendMessage(updown, UDM_SETUNICODEFORMAT, 0 , 0);
436     ok(r == 1, "Expected 1, got %d\n", r);
437     r = SendMessage(updown, UDM_GETUNICODEFORMAT, 0 , 0);
438     ok(r == 0, "Expected 0, got %d\n", r);
439 }
440
441
442 static void test_create_updown_control(void)
443 {
444     CHAR text[MAX_PATH];
445
446     parent_wnd = create_parent_window();
447     ok(parent_wnd != NULL, "Failed to create parent window!\n");
448     ok_sequence(sequences, PARENT_SEQ_INDEX, create_parent_wnd_seq, "create parent window", TRUE);
449
450     flush_sequences(sequences, NUM_MSG_SEQUENCES);
451
452     edit = create_edit_control();
453     ok(edit != NULL, "Failed to create edit control\n");
454     ok_sequence(sequences, PARENT_SEQ_INDEX, add_edit_to_parent_seq, "add edit control to parent", FALSE);
455
456     flush_sequences(sequences, NUM_MSG_SEQUENCES);
457
458     updown = create_updown_control();
459     ok(updown != NULL, "Failed to create updown control\n");
460     ok_sequence(sequences, PARENT_SEQ_INDEX, add_updown_to_parent_seq, "add updown control to parent", TRUE);
461     ok_sequence(sequences, EDIT_SEQ_INDEX, add_updown_with_edit_seq, "add updown control with edit", FALSE);
462
463     flush_sequences(sequences, NUM_MSG_SEQUENCES);
464
465     GetWindowTextA(edit, text, MAX_PATH);
466     ok(lstrlenA(text) == 0, "Expected empty string\n");
467     ok_sequence(sequences, EDIT_SEQ_INDEX, get_edit_text_seq, "get edit text", FALSE);
468
469     flush_sequences(sequences, NUM_MSG_SEQUENCES);
470
471     test_updown_pos();
472     test_updown_pos32();
473     test_updown_buddy();
474     test_updown_base();
475     test_updown_unicode();
476 }
477
478 START_TEST(updown)
479 {
480     InitCommonControls();
481     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
482
483     test_create_updown_control();
484 }