Release 1.5.29.
[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  *   - more stuff to test
45  */
46
47 #include <windows.h>
48 #include <commctrl.h>
49 #include <stdio.h>
50
51 #include "wine/test.h"
52 #include "msg.h"
53
54 #define expect(EXPECTED,GOT) ok((GOT)==(EXPECTED), "Expected %d, got %d\n", (EXPECTED), (GOT))
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 #define UPDOWN_ID           0
62 #define BUDDY_ID            1
63
64 static HWND parent_wnd, g_edit;
65
66 static BOOL (WINAPI *pSetWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR, DWORD_PTR);
67
68 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
69
70 static const struct message add_updown_with_edit_seq[] = {
71     { WM_WINDOWPOSCHANGING, sent },
72     { WM_NCCALCSIZE, sent|wparam, TRUE },
73     { WM_WINDOWPOSCHANGED, sent },
74     { WM_SIZE, sent|wparam|defwinproc, SIZE_RESTORED /*, MAKELONG(91, 75) exact size depends on font */ },
75     { 0 }
76 };
77
78 static const struct message add_updown_to_parent_seq[] = {
79     { WM_NOTIFYFORMAT, sent|lparam, 0, NF_QUERY },
80     { WM_QUERYUISTATE, sent|optional },
81     { WM_PARENTNOTIFY, sent|wparam, MAKELONG(WM_CREATE, WM_CREATE) },
82     { 0 }
83 };
84
85 static const struct message get_edit_text_seq[] = {
86     { WM_GETTEXT, sent },
87     { 0 }
88 };
89
90 static const struct message test_updown_pos_seq[] = {
91     { UDM_SETRANGE, sent|lparam, 0, MAKELONG(100,0) },
92     { UDM_GETRANGE, sent},
93     { UDM_SETPOS, sent|lparam, 0, 5},
94     { UDM_GETPOS, sent},
95     { UDM_SETPOS, sent|lparam, 0, 0},
96     { UDM_GETPOS, sent},
97     { UDM_SETPOS, sent|lparam, 0, MAKELONG(-1,0)},
98     { UDM_GETPOS, sent},
99     { UDM_SETPOS, sent|lparam, 0, 100},
100     { UDM_GETPOS, sent},
101     { UDM_SETPOS, sent|lparam, 0, 101},
102     { UDM_GETPOS, sent},
103     { 0 }
104 };
105
106 static const struct message test_updown_pos32_seq[] = {
107     { UDM_SETRANGE32, sent|lparam, 0, 1000 },
108     { UDM_GETRANGE32, sent}, /* Cannot check wparam and lparam as they are ptrs */
109     { UDM_SETPOS32, sent|lparam, 0, 500 },
110     { UDM_GETPOS32, sent},
111     { UDM_SETPOS32, sent|lparam, 0, 0 },
112     { UDM_GETPOS32, sent},
113     { UDM_SETPOS32, sent|lparam, 0, -1 },
114     { UDM_GETPOS32, sent},
115     { UDM_SETPOS32, sent|lparam, 0, 1000 },
116     { UDM_GETPOS32, sent},
117     { UDM_SETPOS32, sent|lparam, 0, 1001 },
118     { UDM_GETPOS32, sent},
119     { 0 }
120 };
121
122 static const struct message test_updown_buddy_seq[] = {
123     { UDM_GETBUDDY, sent },
124     { UDM_SETBUDDY, sent },
125     { WM_STYLECHANGING, sent|defwinproc },
126     { WM_STYLECHANGED, sent|defwinproc },
127     { WM_STYLECHANGING, sent|defwinproc },
128     { WM_STYLECHANGED, sent|defwinproc },
129     { WM_WINDOWPOSCHANGING, sent|defwinproc },
130     { WM_NCCALCSIZE, sent|wparam|optional|defwinproc, 1 },
131     { WM_WINDOWPOSCHANGED, sent|defwinproc },
132     { WM_MOVE, sent|defwinproc },
133     { UDM_GETBUDDY, sent },
134     { 0 }
135 };
136
137 static const struct message test_updown_base_seq[] = {
138     { UDM_SETBASE, sent|wparam, 10 },
139     { UDM_GETBASE, sent },
140     { UDM_SETBASE, sent|wparam, 80 },
141     { UDM_GETBASE, sent },
142     { UDM_SETBASE, sent|wparam, 16 },
143     { UDM_GETBASE, sent },
144     { UDM_SETBASE, sent|wparam, 80 },
145     { UDM_GETBASE, sent },
146     { UDM_SETBASE, sent|wparam, 10 },
147     { UDM_GETBASE, sent },
148     { 0 }
149 };
150
151 static const struct message test_updown_unicode_seq[] = {
152     { UDM_SETUNICODEFORMAT, sent|wparam, 0 },
153     { UDM_GETUNICODEFORMAT, sent },
154     { UDM_SETUNICODEFORMAT, sent|wparam, 1 },
155     { UDM_GETUNICODEFORMAT, sent },
156     { UDM_SETUNICODEFORMAT, sent|wparam, 0 },
157     { UDM_GETUNICODEFORMAT, sent },
158     { 0 }
159 };
160
161 static const struct message test_updown_pos_nochange_seq[] = {
162     { WM_GETTEXT, sent|id, 0, 0, BUDDY_ID },
163     { 0 }
164 };
165
166 static LRESULT WINAPI parent_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
167 {
168     static LONG defwndproc_counter = 0;
169     LRESULT ret;
170     struct message msg;
171
172     /* log system messages, except for painting */
173     if (message < WM_USER &&
174         message != WM_PAINT &&
175         message != WM_ERASEBKGND &&
176         message != WM_NCPAINT &&
177         message != WM_NCHITTEST &&
178         message != WM_GETTEXT &&
179         message != WM_GETICON &&
180         message != WM_DEVICECHANGE)
181     {
182         trace("parent: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
183
184         msg.message = message;
185         msg.flags = sent|wparam|lparam;
186         if (defwndproc_counter) msg.flags |= defwinproc;
187         msg.wParam = wParam;
188         msg.lParam = lParam;
189         msg.id = 0;
190         add_message(sequences, PARENT_SEQ_INDEX, &msg);
191     }
192
193     defwndproc_counter++;
194     ret = DefWindowProcA(hwnd, message, wParam, lParam);
195     defwndproc_counter--;
196
197     return ret;
198 }
199
200 static BOOL register_parent_wnd_class(void)
201 {
202     WNDCLASSA cls;
203
204     cls.style = 0;
205     cls.lpfnWndProc = parent_wnd_proc;
206     cls.cbClsExtra = 0;
207     cls.cbWndExtra = 0;
208     cls.hInstance = GetModuleHandleA(NULL);
209     cls.hIcon = 0;
210     cls.hCursor = LoadCursorA(0, IDC_ARROW);
211     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
212     cls.lpszMenuName = NULL;
213     cls.lpszClassName = "Up-Down test parent class";
214     return RegisterClassA(&cls);
215 }
216
217 static HWND create_parent_window(void)
218 {
219     if (!register_parent_wnd_class())
220         return NULL;
221
222     return CreateWindowEx(0, "Up-Down test parent class",
223                           "Up-Down test parent window",
224                           WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX |
225                           WS_MAXIMIZEBOX | WS_VISIBLE,
226                           0, 0, 100, 100,
227                           GetDesktopWindow(), NULL, GetModuleHandleA(NULL), NULL);
228 }
229
230 static LRESULT WINAPI edit_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
231 {
232     WNDPROC oldproc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
233     static LONG defwndproc_counter = 0;
234     LRESULT ret;
235     struct message msg;
236
237     trace("edit: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
238
239     msg.message = message;
240     msg.flags = sent|wparam|lparam;
241     if (defwndproc_counter) msg.flags |= defwinproc;
242     msg.wParam = wParam;
243     msg.lParam = lParam;
244     msg.id     = BUDDY_ID;
245     add_message(sequences, EDIT_SEQ_INDEX, &msg);
246
247     defwndproc_counter++;
248     ret = CallWindowProcA(oldproc, hwnd, message, wParam, lParam);
249     defwndproc_counter--;
250     return ret;
251 }
252
253 static HWND create_edit_control(void)
254 {
255     WNDPROC oldproc;
256     HWND hwnd;
257     RECT rect;
258
259     GetClientRect(parent_wnd, &rect);
260     hwnd = CreateWindowExA(0, WC_EDITA, NULL, WS_CHILD | WS_BORDER | WS_VISIBLE,
261                            0, 0, rect.right, rect.bottom,
262                            parent_wnd, NULL, GetModuleHandleA(NULL), NULL);
263     if (!hwnd) return NULL;
264
265     oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
266                                          (LONG_PTR)edit_subclass_proc);
267     SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)oldproc);
268
269     return hwnd;
270 }
271
272 static LRESULT WINAPI updown_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
273 {
274     WNDPROC oldproc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
275     static LONG defwndproc_counter = 0;
276     LRESULT ret;
277     struct message msg;
278
279     trace("updown: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
280
281     msg.message = message;
282     msg.flags = sent|wparam|lparam;
283     if (defwndproc_counter) msg.flags |= defwinproc;
284     msg.wParam = wParam;
285     msg.lParam = lParam;
286     msg.id     = UPDOWN_ID;
287     add_message(sequences, UPDOWN_SEQ_INDEX, &msg);
288
289     defwndproc_counter++;
290     ret = CallWindowProcA(oldproc, hwnd, message, wParam, lParam);
291     defwndproc_counter--;
292
293     return ret;
294 }
295
296 static HWND create_updown_control(DWORD style, HWND buddy)
297 {
298     WNDPROC oldproc;
299     HWND updown;
300     RECT rect;
301
302     GetClientRect(parent_wnd, &rect);
303     updown = CreateUpDownControl(WS_CHILD | WS_BORDER | WS_VISIBLE | style,
304                                  0, 0, rect.right, rect.bottom, parent_wnd, 1, GetModuleHandleA(NULL), buddy,
305                                  100, 0, 50);
306     if (!updown) return NULL;
307
308     oldproc = (WNDPROC)SetWindowLongPtrA(updown, GWLP_WNDPROC,
309                                          (LONG_PTR)updown_subclass_proc);
310     SetWindowLongPtrA(updown, GWLP_USERDATA, (LONG_PTR)oldproc);
311
312     return updown;
313 }
314
315 static void test_updown_pos(void)
316 {
317     HWND updown;
318     int r;
319
320     updown = create_updown_control(UDS_ALIGNRIGHT, g_edit);
321
322     flush_sequences(sequences, NUM_MSG_SEQUENCES);
323
324     /* Set Range from 0 to 100 */
325     SendMessage(updown, UDM_SETRANGE, 0 , MAKELONG(100,0) );
326     r = SendMessage(updown, UDM_GETRANGE, 0,0);
327     expect(100,LOWORD(r));
328     expect(0,HIWORD(r));
329
330     /* Set the position to 5, return is not checked as it was set before func call */
331     SendMessage(updown, UDM_SETPOS, 0 , MAKELONG(5,0) );
332     /* Since UDM_SETBUDDYINT was not set at creation HIWORD(r) will always be 1 as a return from UDM_GETPOS */
333     /* Get the position, which should be 5 */
334     r = SendMessage(updown, UDM_GETPOS, 0 , 0 );
335     expect(5,LOWORD(r));
336     expect(1,HIWORD(r));
337
338     /* Set the position to 0, return should be 5 */
339     r = SendMessage(updown, UDM_SETPOS, 0 , MAKELONG(0,0) );
340     expect(5,r);
341     /* Get the position, which should be 0 */
342     r = SendMessage(updown, UDM_GETPOS, 0 , 0 );
343     expect(0,LOWORD(r));
344     expect(1,HIWORD(r));
345
346     /* Set the position to -1, return should be 0 */
347     r = SendMessage(updown, UDM_SETPOS, 0 , MAKELONG(-1,0) );
348     expect(0,r);
349     /* Get the position, which should be 0 */
350     r = SendMessage(updown, UDM_GETPOS, 0 , 0 );
351     expect(0,LOWORD(r));
352     expect(1,HIWORD(r));
353
354     /* Set the position to 100, return should be 0 */
355     r = SendMessage(updown, UDM_SETPOS, 0 , MAKELONG(100,0) );
356     expect(0,r);
357     /* Get the position, which should be 100 */
358     r = SendMessage(updown, UDM_GETPOS, 0 , 0 );
359     expect(100,LOWORD(r));
360     expect(1,HIWORD(r));
361
362     /* Set the position to 101, return should be 100 */
363     r = SendMessage(updown, UDM_SETPOS, 0 , MAKELONG(101,0) );
364     expect(100,r);
365     /* Get the position, which should be 100 */
366     r = SendMessage(updown, UDM_GETPOS, 0 , 0 );
367     expect(100,LOWORD(r));
368     expect(1,HIWORD(r));
369
370     ok_sequence(sequences, UPDOWN_SEQ_INDEX, test_updown_pos_seq , "test updown pos", FALSE);
371
372     DestroyWindow(updown);
373
374     /* there's no attempt to update buddy Edit if text didn't change */
375     SetWindowTextA(g_edit, "50");
376     updown = create_updown_control(UDS_ALIGNRIGHT | UDS_SETBUDDYINT, g_edit);
377
378     /* test sequence only on 5.8x versions */
379     r = SendMessage(updown, UDM_GETPOS32, 0, 0);
380     if (r)
381     {
382         flush_sequences(sequences, NUM_MSG_SEQUENCES);
383
384         r = SendMessage(updown, UDM_SETPOS, 0, 50);
385         expect(50,r);
386
387         ok_sequence(sequences, EDIT_SEQ_INDEX, test_updown_pos_nochange_seq,
388                     "test updown pos, no change", FALSE);
389     }
390
391     DestroyWindow(updown);
392 }
393
394 static void test_updown_pos32(void)
395 {
396     HWND updown;
397     int r;
398     int low, high;
399
400     updown = create_updown_control(UDS_ALIGNRIGHT, g_edit);
401
402     flush_sequences(sequences, NUM_MSG_SEQUENCES);
403
404     /* Set the position to 0 to 1000 */
405     SendMessage(updown, UDM_SETRANGE32, 0 , 1000 );
406
407     low = high = -1;
408     r = SendMessage(updown, UDM_GETRANGE32, (WPARAM) &low , (LPARAM) &high );
409     expect(0,r);
410     if (low == -1)
411     {
412         win_skip("UDM_SETRANGE32/UDM_GETRANGE32 not available\n");
413         DestroyWindow(updown);
414         return;
415     }
416
417     expect(0,low);
418     expect(1000,high);
419
420     /* Set position to 500 */
421     r = SendMessage(updown, UDM_SETPOS32, 0 , 500 );
422     if (!r)
423     {
424         win_skip("UDM_SETPOS32 and UDM_GETPOS32 need 5.80\n");
425         DestroyWindow(updown);
426         return;
427     }
428     expect(50,r);
429
430     /* Since UDM_SETBUDDYINT was not set at creation bRet will always be true as a return from UDM_GETPOS32 */
431
432     r = SendMessage(updown, UDM_GETPOS32, 0 , (LPARAM) &high );
433     expect(500,r);
434     expect(1,high);
435
436     /* Set position to 0, return should be 500 */
437     r = SendMessage(updown, UDM_SETPOS32, 0 , 0 );
438     expect(500,r);
439     r = SendMessage(updown, UDM_GETPOS32, 0 , (LPARAM) &high );
440     expect(0,r);
441     expect(1,high);
442
443     /* Set position to -1 which should become 0, return should be 0 */
444     r = SendMessage(updown, UDM_SETPOS32, 0 , -1 );
445     expect(0,r);
446     r = SendMessage(updown, UDM_GETPOS32, 0 , (LPARAM) &high );
447     expect(0,r);
448     expect(1,high);
449
450     /* Set position to 1000, return should be 0 */
451     r = SendMessage(updown, UDM_SETPOS32, 0 , 1000 );
452     expect(0,r);
453     r = SendMessage(updown, UDM_GETPOS32, 0 , (LPARAM) &high );
454     expect(1000,r);
455     expect(1,high);
456
457     /* Set position to 1001 which should become 1000, return should be 1000 */
458     r = SendMessage(updown, UDM_SETPOS32, 0 , 1001 );
459     expect(1000,r);
460     r = SendMessage(updown, UDM_GETPOS32, 0 , (LPARAM) &high );
461     expect(1000,r);
462     expect(1,high);
463
464     ok_sequence(sequences, UPDOWN_SEQ_INDEX, test_updown_pos32_seq, "test updown pos32", FALSE);
465
466     DestroyWindow(updown);
467
468     /* there's no attempt to update buddy Edit if text didn't change */
469     SetWindowTextA(g_edit, "50");
470     updown = create_updown_control(UDS_ALIGNRIGHT | UDS_SETBUDDYINT, g_edit);
471
472     flush_sequences(sequences, NUM_MSG_SEQUENCES);
473
474     r = SendMessage(updown, UDM_SETPOS32, 0, 50);
475     expect(50,r);
476     ok_sequence(sequences, EDIT_SEQ_INDEX, test_updown_pos_nochange_seq,
477                 "test updown pos, no change", FALSE);
478
479     DestroyWindow(updown);
480 }
481
482 static void test_updown_buddy(void)
483 {
484     HWND updown, buddyReturn, buddy;
485     WNDPROC proc;
486     DWORD style;
487
488     updown = create_updown_control(UDS_ALIGNRIGHT, g_edit);
489
490     flush_sequences(sequences, NUM_MSG_SEQUENCES);
491
492     buddyReturn = (HWND)SendMessage(updown, UDM_GETBUDDY, 0 , 0 );
493     ok(buddyReturn == g_edit, "Expected edit handle\n");
494
495     buddyReturn = (HWND)SendMessage(updown, UDM_SETBUDDY, (WPARAM) g_edit, 0);
496     ok(buddyReturn == g_edit, "Expected edit handle\n");
497
498     buddyReturn = (HWND)SendMessage(updown, UDM_GETBUDDY, 0 , 0 );
499     ok(buddyReturn == g_edit, "Expected edit handle\n");
500
501     ok_sequence(sequences, UPDOWN_SEQ_INDEX, test_updown_buddy_seq, "test updown buddy", TRUE);
502     ok_sequence(sequences, EDIT_SEQ_INDEX, add_updown_with_edit_seq, "test updown buddy_edit", FALSE);
503
504     DestroyWindow(updown);
505
506     buddy = create_edit_control();
507     proc  = (WNDPROC)GetWindowLongPtrA(buddy, GWLP_WNDPROC);
508
509     updown= create_updown_control(UDS_ALIGNRIGHT, buddy);
510     ok(proc == (WNDPROC)GetWindowLongPtrA(buddy, GWLP_WNDPROC), "No subclassing expected\n");
511
512     style = GetWindowLongA(updown, GWL_STYLE);
513     SetWindowLongA(updown, GWL_STYLE, style | UDS_ARROWKEYS);
514     style = GetWindowLongA(updown, GWL_STYLE);
515     ok(style & UDS_ARROWKEYS, "Expected UDS_ARROWKEYS\n");
516     /* no subclass if UDS_ARROWKEYS set after creation */
517     ok(proc == (WNDPROC)GetWindowLongPtrA(buddy, GWLP_WNDPROC), "No subclassing expected\n");
518
519     DestroyWindow(updown);
520
521     updown= create_updown_control(UDS_ALIGNRIGHT | UDS_ARROWKEYS, buddy);
522     ok(proc != (WNDPROC)GetWindowLongPtrA(buddy, GWLP_WNDPROC), "Subclassing expected\n");
523
524     if (pSetWindowSubclass)
525     {
526         /* updown uses subclass helpers for buddy on >5.8x systems */
527         ok(GetPropA(buddy, "CC32SubclassInfo") != NULL, "Expected CC32SubclassInfo property\n");
528     }
529
530     DestroyWindow(updown);
531
532     DestroyWindow(buddy);
533 }
534
535 static void test_updown_base(void)
536 {
537     HWND updown;
538     int r;
539     CHAR text[10];
540
541     updown = create_updown_control(UDS_ALIGNRIGHT, g_edit);
542
543     flush_sequences(sequences, NUM_MSG_SEQUENCES);
544
545     SendMessage(updown, UDM_SETBASE, 10 , 0);
546     r = SendMessage(updown, UDM_GETBASE, 0 , 0);
547     expect(10,r);
548
549     /* Set base to an invalid value, should return 0 and stay at 10 */
550     r = SendMessage(updown, UDM_SETBASE, 80 , 0);
551     expect(0,r);
552     r = SendMessage(updown, UDM_GETBASE, 0 , 0);
553     expect(10,r);
554
555     /* Set base to 16 now, should get 16 as the return */
556     r = SendMessage(updown, UDM_SETBASE, 16 , 0);
557     expect(10,r);
558     r = SendMessage(updown, UDM_GETBASE, 0 , 0);
559     expect(16,r);
560
561     /* Set base to an invalid value, should return 0 and stay at 16 */
562     r = SendMessage(updown, UDM_SETBASE, 80 , 0);
563     expect(0,r);
564     r = SendMessage(updown, UDM_GETBASE, 0 , 0);
565     expect(16,r);
566
567     /* Set base back to 10, return should be 16 */
568     r = SendMessage(updown, UDM_SETBASE, 10 , 0);
569     expect(16,r);
570     r = SendMessage(updown, UDM_GETBASE, 0 , 0);
571     expect(10,r);
572
573     ok_sequence(sequences, UPDOWN_SEQ_INDEX, test_updown_base_seq, "test updown base", FALSE);
574
575     DestroyWindow(updown);
576
577     /* switch base with buddy attached */
578     updown = create_updown_control(UDS_SETBUDDYINT | UDS_ALIGNRIGHT, g_edit);
579
580     r = SendMessage(updown, UDM_SETPOS, 0, 10);
581     expect(50, r);
582
583     GetWindowTextA(g_edit, text, sizeof(text)/sizeof(CHAR));
584     ok(lstrcmpA(text, "10") == 0, "Expected '10', got '%s'\n", text);
585
586     r = SendMessage(updown, UDM_SETBASE, 16, 0);
587     expect(10, r);
588
589     GetWindowTextA(g_edit, text, sizeof(text)/sizeof(CHAR));
590     /* FIXME: currently hex output isn't properly formatted, but for this
591        test only change from initial text matters */
592     ok(lstrcmpA(text, "10") != 0, "Expected '0x000A', got '%s'\n", text);
593
594     DestroyWindow(updown);
595 }
596
597 static void test_updown_unicode(void)
598 {
599     HWND updown;
600     int r;
601
602     updown = create_updown_control(UDS_ALIGNRIGHT, g_edit);
603
604     flush_sequences(sequences, NUM_MSG_SEQUENCES);
605
606     /* Set it to ANSI, don't check return as we don't know previous state */
607     SendMessage(updown, UDM_SETUNICODEFORMAT, 0 , 0);
608     r = SendMessage(updown, UDM_GETUNICODEFORMAT, 0 , 0);
609     expect(0,r);
610
611     /* Now set it to Unicode format */
612     r = SendMessage(updown, UDM_SETUNICODEFORMAT, 1 , 0);
613     expect(0,r);
614     r = SendMessage(updown, UDM_GETUNICODEFORMAT, 0 , 0);
615     if (!r)
616     {
617         win_skip("UDM_SETUNICODEFORMAT not available\n");
618         DestroyWindow(updown);
619         return;
620     }
621     expect(1,r);
622
623     /* And now set it back to ANSI */
624     r = SendMessage(updown, UDM_SETUNICODEFORMAT, 0 , 0);
625     expect(1,r);
626     r = SendMessage(updown, UDM_GETUNICODEFORMAT, 0 , 0);
627     expect(0,r);
628
629     ok_sequence(sequences, UPDOWN_SEQ_INDEX, test_updown_unicode_seq, "test updown unicode", FALSE);
630
631     DestroyWindow(updown);
632 }
633
634 static void test_updown_create(void)
635 {
636     CHAR text[MAX_PATH];
637     HWND updown;
638     RECT r;
639
640     flush_sequences(sequences, NUM_MSG_SEQUENCES);
641
642     updown = create_updown_control(UDS_ALIGNRIGHT, g_edit);
643     ok(updown != NULL, "Failed to create updown control\n");
644     ok_sequence(sequences, PARENT_SEQ_INDEX, add_updown_to_parent_seq, "add updown control to parent", TRUE);
645     ok_sequence(sequences, EDIT_SEQ_INDEX, add_updown_with_edit_seq, "add updown control with edit", FALSE);
646
647     flush_sequences(sequences, NUM_MSG_SEQUENCES);
648
649     GetWindowTextA(g_edit, text, MAX_PATH);
650     ok(lstrlenA(text) == 0, "Expected empty string\n");
651     ok_sequence(sequences, EDIT_SEQ_INDEX, get_edit_text_seq, "get edit text", FALSE);
652
653     DestroyWindow(updown);
654
655     /* create with zero width */
656     updown = CreateWindowA (UPDOWN_CLASSA, 0, WS_CHILD | WS_BORDER | WS_VISIBLE, 0, 0, 0, 0,
657                    parent_wnd, (HMENU)(DWORD_PTR)1, GetModuleHandleA(NULL), 0);
658     ok(updown != NULL, "Failed to create updown control\n");
659     r.right = 0;
660     GetClientRect(updown, &r);
661     ok(r.right > 0, "Expected default width, got %d\n", r.right);
662     DestroyWindow(updown);
663     /* create with really small width */
664     updown = CreateWindowA (UPDOWN_CLASSA, 0, WS_CHILD | WS_BORDER | WS_VISIBLE, 0, 0, 2, 0,
665                    parent_wnd, (HMENU)(DWORD_PTR)1, GetModuleHandleA(NULL), 0);
666     ok(updown != NULL, "Failed to create updown control\n");
667     r.right = 0;
668     GetClientRect(updown, &r);
669     ok(r.right != 2 && r.right > 0, "Expected default width, got %d\n", r.right);
670     DestroyWindow(updown);
671     /* create with width greater than default */
672     updown = CreateWindowA (UPDOWN_CLASSA, 0, WS_CHILD | WS_BORDER | WS_VISIBLE, 0, 0, 100, 0,
673                    parent_wnd, (HMENU)(DWORD_PTR)1, GetModuleHandleA(NULL), 0);
674     ok(updown != NULL, "Failed to create updown control\n");
675     r.right = 0;
676     GetClientRect(updown, &r);
677     ok(r.right < 100 && r.right > 0, "Expected default width, got %d\n", r.right);
678     DestroyWindow(updown);
679     /* create with zero height, UDS_HORZ */
680     updown = CreateWindowA (UPDOWN_CLASSA, 0, UDS_HORZ | WS_CHILD | WS_BORDER | WS_VISIBLE, 0, 0, 0, 0,
681                    parent_wnd, (HMENU)(DWORD_PTR)1, GetModuleHandleA(NULL), 0);
682     ok(updown != NULL, "Failed to create updown control\n");
683     r.bottom = 0;
684     GetClientRect(updown, &r);
685     ok(r.bottom == 0, "Expected zero height, got %d\n", r.bottom);
686     DestroyWindow(updown);
687     /* create with really small height, UDS_HORZ */
688     updown = CreateWindowA (UPDOWN_CLASSA, 0, UDS_HORZ | WS_CHILD | WS_BORDER | WS_VISIBLE, 0, 0, 0, 2,
689                    parent_wnd, (HMENU)(DWORD_PTR)1, GetModuleHandleA(NULL), 0);
690     ok(updown != NULL, "Failed to create updown control\n");
691     r.bottom = 0;
692     GetClientRect(updown, &r);
693     ok(r.bottom == 0, "Expected zero height, got %d\n", r.bottom);
694     DestroyWindow(updown);
695     /* create with height greater than default, UDS_HORZ */
696     updown = CreateWindowA (UPDOWN_CLASSA, 0, UDS_HORZ | WS_CHILD | WS_BORDER | WS_VISIBLE, 0, 0, 0, 100,
697                    parent_wnd, (HMENU)(DWORD_PTR)1, GetModuleHandleA(NULL), 0);
698     ok(updown != NULL, "Failed to create updown control\n");
699     r.bottom = 0;
700     GetClientRect(updown, &r);
701     ok(r.bottom < 100 && r.bottom > 0, "Expected default height, got %d\n", r.bottom);
702     DestroyWindow(updown);
703 }
704
705 static void test_UDS_SETBUDDYINT(void)
706 {
707     HWND updown;
708     DWORD style, ret;
709     CHAR text[10];
710
711     /* cleanup buddy */
712     text[0] = '\0';
713     SetWindowTextA(g_edit, text);
714
715     /* creating without UDS_SETBUDDYINT */
716     updown = create_updown_control(UDS_ALIGNRIGHT, g_edit);
717     /* try to set UDS_SETBUDDYINT after creation */
718     style = GetWindowLongA(updown, GWL_STYLE);
719     SetWindowLongA(updown, GWL_STYLE, style | UDS_SETBUDDYINT);
720     style = GetWindowLongA(updown, GWL_STYLE);
721     ok(style & UDS_SETBUDDYINT, "Expected UDS_SETBUDDY to be set\n");
722     SendMessage(updown, UDM_SETPOS, 0, 20);
723     GetWindowTextA(g_edit, text, sizeof(text)/sizeof(CHAR));
724     ok(lstrlenA(text) == 0, "Expected empty string\n");
725     DestroyWindow(updown);
726
727     /* creating with UDS_SETBUDDYINT */
728     updown = create_updown_control(UDS_SETBUDDYINT | UDS_ALIGNRIGHT, g_edit);
729     GetWindowTextA(g_edit, text, sizeof(text)/sizeof(CHAR));
730     /* 50 is initial value here */
731     ok(lstrcmpA(text, "50") == 0, "Expected '50', got '%s'\n", text);
732     /* now remove style flag */
733     style = GetWindowLongA(updown, GWL_STYLE);
734     SetWindowLongA(updown, GWL_STYLE, style & ~UDS_SETBUDDYINT);
735     SendMessage(updown, UDM_SETPOS, 0, 20);
736     GetWindowTextA(g_edit, text, sizeof(text)/sizeof(CHAR));
737     ok(lstrcmpA(text, "20") == 0, "Expected '20', got '%s'\n", text);
738     /* set edit text directly, check position */
739     strcpy(text, "10");
740     SetWindowTextA(g_edit, text);
741     ret = SendMessageA(updown, UDM_GETPOS, 0, 0);
742     expect(10, ret);
743     strcpy(text, "11");
744     SetWindowTextA(g_edit, text);
745     ret = SendMessageA(updown, UDM_GETPOS, 0, 0);
746     expect(11, LOWORD(ret));
747     expect(0,  HIWORD(ret));
748     /* set to invalid value */
749     strcpy(text, "21st");
750     SetWindowTextA(g_edit, text);
751     ret = SendMessageA(updown, UDM_GETPOS, 0, 0);
752     expect(11, LOWORD(ret));
753     expect(TRUE, HIWORD(ret));
754     /* set style back */
755     style = GetWindowLongA(updown, GWL_STYLE);
756     SetWindowLongA(updown, GWL_STYLE, style | UDS_SETBUDDYINT);
757     SendMessage(updown, UDM_SETPOS, 0, 30);
758     GetWindowTextA(g_edit, text, sizeof(text)/sizeof(CHAR));
759     ok(lstrcmpA(text, "30") == 0, "Expected '30', got '%s'\n", text);
760     DestroyWindow(updown);
761 }
762
763 START_TEST(updown)
764 {
765     HMODULE mod = GetModuleHandleA("comctl32.dll");
766
767     pSetWindowSubclass = (void*)GetProcAddress(mod, (LPSTR)410);
768
769     InitCommonControls();
770     init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
771
772     parent_wnd = create_parent_window();
773     ok(parent_wnd != NULL, "Failed to create parent window!\n");
774     g_edit = create_edit_control();
775     ok(g_edit != NULL, "Failed to create edit control\n");
776
777     test_updown_create();
778     test_updown_pos();
779     test_updown_pos32();
780     test_updown_buddy();
781     test_updown_base();
782     test_updown_unicode();
783     test_UDS_SETBUDDYINT();
784
785     DestroyWindow(g_edit);
786     DestroyWindow(parent_wnd);
787 }