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