1a21f84cc03cf46b714949fe46a4ae235f2a2f94
[reactos.git] / rostests / winetests / comctl32 / datetime.c
1 /* Unit test suite for datetime control.
2 *
3 * Copyright 2007 Kanit Therdsteerasukdi
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 //#include <windows.h>
21 #include <stdarg.h>
22 #include <windef.h>
23 #include <winbase.h>
24 #include <wingdi.h>
25 #include <winuser.h>
26 #include <winnls.h>
27 #include <commctrl.h>
28 //#include "wine/test.h"
29 #include "msg.h"
30
31 #define expect(EXPECTED, GOT) ok((GOT)==(EXPECTED), "Expected %d, got %ld\n", (EXPECTED), (GOT))
32
33 #define expect_unsuccess(EXPECTED, GOT) ok((GOT)==(EXPECTED), "Expected %d(unsuccessful), got %ld(successful)\n", (EXPECTED), (GOT))
34
35 #define NUM_MSG_SEQUENCES 1
36 #define DATETIME_SEQ_INDEX 0
37
38 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
39
40 static const struct message test_dtm_set_format_seq[] = {
41 { DTM_SETFORMATA, sent|wparam|lparam, 0, 0 },
42 { DTM_SETFORMATA, sent|wparam, 0 },
43 { 0 }
44 };
45
46 static const struct message test_dtm_set_and_get_mccolor_seq[] = {
47 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_BACKGROUND, 0 },
48 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_BACKGROUND, RGB(255, 255, 255) },
49 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_BACKGROUND, RGB(100, 180, 220) },
50 { DTM_GETMCCOLOR, sent|wparam|lparam, MCSC_BACKGROUND, 0 },
51 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_MONTHBK, 0 },
52 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_MONTHBK, RGB(255, 255, 255) },
53 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_MONTHBK, RGB(100, 180, 220) },
54 { DTM_GETMCCOLOR, sent|wparam|lparam, MCSC_MONTHBK, 0 },
55 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_TEXT, 0 },
56 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_TEXT, RGB(255, 255, 255) },
57 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_TEXT, RGB(100, 180, 220) },
58 { DTM_GETMCCOLOR, sent|wparam|lparam, MCSC_TEXT, 0 },
59 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_TITLEBK, 0 },
60 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_TITLEBK, RGB(255, 255, 255) },
61 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_TITLEBK, RGB(100, 180, 220) },
62 { DTM_GETMCCOLOR, sent|wparam|lparam, MCSC_TITLEBK, 0 },
63 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_TITLETEXT, 0 },
64 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_TITLETEXT, RGB(255, 255, 255) },
65 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_TITLETEXT, RGB(100, 180, 220) },
66 { DTM_GETMCCOLOR, sent|wparam|lparam, MCSC_TITLETEXT, 0 },
67 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_TRAILINGTEXT, 0 },
68 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_TRAILINGTEXT, RGB(255, 255, 255) },
69 { DTM_SETMCCOLOR, sent|wparam|lparam, MCSC_TRAILINGTEXT, RGB(100, 180, 220) },
70 { DTM_GETMCCOLOR, sent|wparam|lparam, MCSC_TRAILINGTEXT, 0 },
71 { 0 }
72 };
73
74 static const struct message test_dtm_set_and_get_mcfont_seq[] = {
75 { DTM_SETMCFONT, sent|lparam, 0, 1 },
76 { DTM_GETMCFONT, sent|wparam|lparam, 0, 0 },
77 { 0 }
78 };
79
80 static const struct message test_dtm_get_monthcal_seq[] = {
81 { DTM_GETMONTHCAL, sent|wparam|lparam, 0, 0 },
82 { 0 }
83 };
84
85 static const struct message test_dtm_set_and_get_range_seq[] = {
86 { DTM_SETRANGE, sent|wparam, GDTR_MIN },
87 { DTM_GETRANGE, sent|wparam, 0 },
88 { DTM_SETRANGE, sent|wparam, GDTR_MAX },
89 { DTM_SETRANGE, sent|wparam, GDTR_MAX },
90 { DTM_GETRANGE, sent|wparam, 0 },
91 { DTM_SETRANGE, sent|wparam, GDTR_MIN },
92 { DTM_SETRANGE, sent|wparam, GDTR_MIN | GDTR_MAX },
93 { DTM_SETRANGE, sent|wparam, GDTR_MIN | GDTR_MAX },
94 { DTM_GETRANGE, sent|wparam, 0 },
95 { DTM_SETRANGE, sent|wparam, GDTR_MIN | GDTR_MAX },
96 { DTM_GETRANGE, sent|wparam, 0 },
97 { DTM_SETRANGE, sent|wparam, GDTR_MIN | GDTR_MAX },
98 { DTM_GETRANGE, sent|wparam, 0 },
99 { 0 }
100 };
101
102 static const struct message test_dtm_set_range_swap_min_max_seq[] = {
103 { DTM_SETSYSTEMTIME, sent|wparam, 0 },
104 { DTM_GETSYSTEMTIME, sent|wparam, 0 },
105 { DTM_SETRANGE, sent|wparam, GDTR_MIN | GDTR_MAX },
106 { DTM_GETRANGE, sent|wparam, 0 },
107 { DTM_SETSYSTEMTIME, sent|wparam, 0 },
108 { DTM_GETSYSTEMTIME, sent|wparam, 0 },
109 { DTM_SETRANGE, sent|wparam, GDTR_MIN | GDTR_MAX },
110 { DTM_GETRANGE, sent|wparam, 0 },
111 { DTM_SETRANGE, sent|wparam, GDTR_MIN | GDTR_MAX },
112 { DTM_GETRANGE, sent|wparam, 0 },
113 { DTM_SETRANGE, sent|wparam, GDTR_MIN | GDTR_MAX },
114 { DTM_GETRANGE, sent|wparam, 0 },
115 { 0 }
116 };
117
118 static const struct message test_dtm_set_and_get_system_time_seq[] = {
119 { DTM_SETSYSTEMTIME, sent|wparam, GDT_NONE },
120 { DTM_GETSYSTEMTIME, sent|wparam, 0 },
121 { DTM_SETSYSTEMTIME, sent|wparam, 0 },
122 { DTM_SETSYSTEMTIME, sent|wparam, 0 },
123 { DTM_SETSYSTEMTIME, sent|wparam, 0 },
124 { DTM_GETSYSTEMTIME, sent|wparam, 0 },
125 { DTM_SETSYSTEMTIME, sent|wparam, 0 },
126 { 0 }
127 };
128
129 static const struct message test_dtm_set_and_get_systime_with_limits[] = {
130 { DTM_SETRANGE, sent|wparam, GDTR_MIN | GDTR_MAX },
131 { DTM_GETRANGE, sent|wparam, 0 },
132 { DTM_SETSYSTEMTIME, sent|wparam, 0 },
133 { DTM_GETSYSTEMTIME, sent|wparam, 0 },
134 { DTM_SETSYSTEMTIME, sent|wparam, 0 },
135 { DTM_GETSYSTEMTIME, sent|wparam, 0 },
136 { DTM_SETSYSTEMTIME, sent|wparam, 0 },
137 { DTM_GETSYSTEMTIME, sent|wparam, 0 },
138 { 0 }
139 };
140
141 static LRESULT WINAPI datetime_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
142 {
143 WNDPROC oldproc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
144 static LONG defwndproc_counter = 0;
145 LRESULT ret;
146 struct message msg;
147
148 msg.message = message;
149 msg.flags = sent|wparam|lparam;
150 if (defwndproc_counter) msg.flags |= defwinproc;
151 msg.wParam = wParam;
152 msg.lParam = lParam;
153 msg.id = 0;
154 add_message(sequences, DATETIME_SEQ_INDEX, &msg);
155
156 defwndproc_counter++;
157 ret = CallWindowProcA(oldproc, hwnd, message, wParam, lParam);
158 defwndproc_counter--;
159
160 return ret;
161 }
162
163 static HWND create_datetime_control(DWORD style)
164 {
165 WNDPROC oldproc;
166 HWND hWndDateTime = NULL;
167
168 hWndDateTime = CreateWindowEx(0,
169 DATETIMEPICK_CLASS,
170 NULL,
171 style,
172 0,50,300,120,
173 NULL,
174 NULL,
175 NULL,
176 NULL);
177
178 if (!hWndDateTime) return NULL;
179
180 oldproc = (WNDPROC)SetWindowLongPtrA(hWndDateTime, GWLP_WNDPROC,
181 (LONG_PTR)datetime_subclass_proc);
182 SetWindowLongPtrA(hWndDateTime, GWLP_USERDATA, (LONG_PTR)oldproc);
183
184 return hWndDateTime;
185 }
186
187 static void test_dtm_set_format(void)
188 {
189 HWND hWnd;
190 CHAR txt[256];
191 SYSTEMTIME systime;
192 LRESULT r;
193
194 hWnd = create_datetime_control(DTS_SHOWNONE);
195
196 flush_sequences(sequences, NUM_MSG_SEQUENCES);
197
198 r = SendMessage(hWnd, DTM_SETFORMAT, 0, 0);
199 expect(1, r);
200
201 r = SendMessage(hWnd, DTM_SETFORMAT, 0,
202 (LPARAM)"'Today is: 'hh':'m':'s dddd MMM dd', 'yyyy");
203 expect(1, r);
204
205 ok_sequence(sequences, DATETIME_SEQ_INDEX, test_dtm_set_format_seq, "test_dtm_set_format", FALSE);
206
207 r = SendMessage(hWnd, DTM_SETFORMAT, 0,
208 (LPARAM)"'hh' hh");
209 expect(1, r);
210 ZeroMemory(&systime, sizeof(systime));
211 systime.wYear = 2000;
212 systime.wMonth = systime.wDay = 1;
213 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, 0, (LPARAM)&systime);
214 expect(1, r);
215 GetWindowText(hWnd, txt, 256);
216 ok(strcmp(txt, "hh 12") == 0, "String mismatch (\"%s\" vs \"hh 12\")\n", txt);
217
218 DestroyWindow(hWnd);
219 }
220
221 static void test_mccolor_types(HWND hWndDateTime, int mccolor_type, const char* mccolor_name)
222 {
223 COLORREF theColor, prevColor, crColor;
224
225 theColor=RGB(0,0,0);
226 crColor = SendMessage(hWndDateTime, DTM_SETMCCOLOR, mccolor_type, theColor);
227 ok(crColor != ~0u, "%s: Set RGB(0,0,0): Expected COLORREF of previous value, got %d\n", mccolor_name, crColor);
228 prevColor=theColor;
229 theColor=RGB(255,255,255);
230 crColor = SendMessage(hWndDateTime, DTM_SETMCCOLOR, mccolor_type, theColor);
231 ok(crColor==prevColor, "%s: Set RGB(255,255,255): Expected COLORREF of previous value, got %d\n", mccolor_name, crColor);
232 prevColor=theColor;
233 theColor=RGB(100,180,220);
234 crColor = SendMessage(hWndDateTime, DTM_SETMCCOLOR, mccolor_type, theColor);
235 ok(crColor==prevColor, "%s: Set RGB(100,180,220): Expected COLORREF of previous value, got %d\n", mccolor_name, crColor);
236 crColor = SendMessage(hWndDateTime, DTM_GETMCCOLOR, mccolor_type, 0);
237 ok(crColor==theColor, "%s: GETMCCOLOR: Expected %d, got %d\n", mccolor_name, theColor, crColor);
238 }
239
240 static void test_dtm_set_and_get_mccolor(void)
241 {
242 HWND hWnd;
243
244 hWnd = create_datetime_control(DTS_SHOWNONE);
245
246 flush_sequences(sequences, NUM_MSG_SEQUENCES);
247
248 test_mccolor_types(hWnd, MCSC_BACKGROUND, "MCSC_BACKGROUND");
249 test_mccolor_types(hWnd, MCSC_MONTHBK, "MCSC_MONTHBK");
250 test_mccolor_types(hWnd, MCSC_TEXT, "MCSC_TEXT");
251 test_mccolor_types(hWnd, MCSC_TITLEBK, "MCSC_TITLEBK");
252 test_mccolor_types(hWnd, MCSC_TITLETEXT, "MCSC_TITLETEXT");
253 test_mccolor_types(hWnd, MCSC_TRAILINGTEXT, "MCSC_TRAILINGTEXT");
254
255 ok_sequence(sequences, DATETIME_SEQ_INDEX, test_dtm_set_and_get_mccolor_seq, "test_dtm_set_and_get_mccolor", FALSE);
256
257 DestroyWindow(hWnd);
258 }
259
260 static void test_dtm_set_and_get_mcfont(void)
261 {
262 HFONT hFontOrig, hFontNew;
263 HWND hWnd;
264
265 hWnd = create_datetime_control(DTS_SHOWNONE);
266
267 flush_sequences(sequences, NUM_MSG_SEQUENCES);
268
269 hFontOrig = GetStockObject(DEFAULT_GUI_FONT);
270 SendMessage(hWnd, DTM_SETMCFONT, (WPARAM)hFontOrig, TRUE);
271 hFontNew = (HFONT)SendMessage(hWnd, DTM_GETMCFONT, 0, 0);
272 ok(hFontOrig == hFontNew, "Expected hFontOrig==hFontNew, hFontOrig=%p, hFontNew=%p\n", hFontOrig, hFontNew);
273
274 ok_sequence(sequences, DATETIME_SEQ_INDEX, test_dtm_set_and_get_mcfont_seq, "test_dtm_set_and_get_mcfont", FALSE);
275 DestroyWindow(hWnd);
276 }
277
278 static void test_dtm_get_monthcal(void)
279 {
280 LRESULT r;
281 HWND hWnd;
282
283 hWnd = create_datetime_control(DTS_SHOWNONE);
284
285 flush_sequences(sequences, NUM_MSG_SEQUENCES);
286
287 todo_wine {
288 r = SendMessage(hWnd, DTM_GETMONTHCAL, 0, 0);
289 ok(r == 0, "Expected NULL(no child month calendar control), got %ld\n", r);
290 }
291
292 ok_sequence(sequences, DATETIME_SEQ_INDEX, test_dtm_get_monthcal_seq, "test_dtm_get_monthcal", FALSE);
293 DestroyWindow(hWnd);
294 }
295
296 static void fill_systime_struct(SYSTEMTIME *st, int year, int month, int dayofweek, int day, int hour, int minute, int second, int milliseconds)
297 {
298 st->wYear = year;
299 st->wMonth = month;
300 st->wDayOfWeek = dayofweek;
301 st->wDay = day;
302 st->wHour = hour;
303 st->wMinute = minute;
304 st->wSecond = second;
305 st->wMilliseconds = milliseconds;
306 }
307
308 static LPARAM compare_systime_date(SYSTEMTIME *st1, SYSTEMTIME *st2)
309 {
310 return (st1->wYear == st2->wYear)
311 && (st1->wMonth == st2->wMonth)
312 && (st1->wDayOfWeek == st2->wDayOfWeek)
313 && (st1->wDay == st2->wDay);
314 }
315
316 static LPARAM compare_systime_time(SYSTEMTIME *st1, SYSTEMTIME *st2)
317 {
318 return (st1->wHour == st2->wHour)
319 && (st1->wMinute == st2->wMinute)
320 && (st1->wSecond == st2->wSecond)
321 && (st1->wMilliseconds == st2->wMilliseconds);
322 }
323
324 static LPARAM compare_systime(SYSTEMTIME *st1, SYSTEMTIME *st2)
325 {
326 if(!compare_systime_date(st1, st2))
327 return 0;
328
329 return compare_systime_time(st1, st2);
330 }
331
332 #define expect_systime(ST1, ST2) ok(compare_systime((ST1), (ST2))==1, "ST1 != ST2\n")
333 #define expect_systime_date(ST1, ST2) ok(compare_systime_date((ST1), (ST2))==1, "ST1.date != ST2.date\n")
334 #define expect_systime_time(ST1, ST2) ok(compare_systime_time((ST1), (ST2))==1, "ST1.time != ST2.time\n")
335
336 static void test_dtm_set_and_get_range(void)
337 {
338 LRESULT r;
339 SYSTEMTIME st[2];
340 SYSTEMTIME getSt[2];
341 HWND hWnd;
342
343 hWnd = create_datetime_control(DTS_SHOWNONE);
344
345 flush_sequences(sequences, NUM_MSG_SEQUENCES);
346
347 /* initialize st[0] to lowest possible value */
348 fill_systime_struct(&st[0], 1601, 1, 0, 1, 0, 0, 0, 0);
349 /* initialize st[1] to all invalid numbers */
350 fill_systime_struct(&st[1], 0, 0, 7, 0, 24, 60, 60, 1000);
351
352 r = SendMessage(hWnd, DTM_SETRANGE, GDTR_MIN, (LPARAM)st);
353 expect(1, r);
354 r = SendMessage(hWnd, DTM_GETRANGE, 0, (LPARAM)getSt);
355 ok(r == GDTR_MIN, "Expected %x, not %x(GDTR_MAX) or %x(GDTR_MIN | GDTR_MAX), got %lx\n", GDTR_MIN, GDTR_MAX, GDTR_MIN | GDTR_MAX, r);
356 expect_systime(&st[0], &getSt[0]);
357
358 r = SendMessage(hWnd, DTM_SETRANGE, GDTR_MAX, (LPARAM)st);
359 expect_unsuccess(0, r);
360
361 /* set st[0] to all invalid numbers */
362 fill_systime_struct(&st[0], 0, 0, 7, 0, 24, 60, 60, 1000);
363 /* set st[1] to highest possible value */
364 fill_systime_struct(&st[1], 30827, 12, 6, 31, 23, 59, 59, 999);
365
366 r = SendMessage(hWnd, DTM_SETRANGE, GDTR_MAX, (LPARAM)st);
367 expect(1, r);
368 r = SendMessage(hWnd, DTM_GETRANGE, 0, (LPARAM)getSt);
369 todo_wine {
370 ok(r == GDTR_MAX, "Expected %x, not %x(GDTR_MIN) or %x(GDTR_MIN | GDTR_MAX), got %lx\n", GDTR_MAX, GDTR_MIN, GDTR_MIN | GDTR_MAX, r);
371 }
372 expect_systime(&st[1], &getSt[1]);
373
374 r = SendMessage(hWnd, DTM_SETRANGE, GDTR_MIN, (LPARAM)st);
375 expect_unsuccess(0, r);
376 r = SendMessage(hWnd, DTM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st);
377 expect_unsuccess(0, r);
378
379 /* set st[0] to highest possible value */
380 fill_systime_struct(&st[0], 30827, 12, 6, 31, 23, 59, 59, 999);
381
382 r = SendMessage(hWnd, DTM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st);
383 expect(1, r);
384 r = SendMessage(hWnd, DTM_GETRANGE, 0, (LPARAM)getSt);
385 ok(r == (GDTR_MIN | GDTR_MAX), "Expected %x, not %x(GDTR_MIN) or %x(GDTR_MAX), got %lx\n", (GDTR_MIN | GDTR_MAX), GDTR_MIN, GDTR_MAX, r);
386 expect_systime(&st[0], &getSt[0]);
387 expect_systime(&st[1], &getSt[1]);
388
389 /* initialize st[0] to lowest possible value */
390 fill_systime_struct(&st[0], 1601, 1, 0, 1, 0, 0, 0, 0);
391 /* set st[1] to highest possible value */
392 fill_systime_struct(&st[1], 30827, 12, 6, 31, 23, 59, 59, 999);
393
394 r = SendMessage(hWnd, DTM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st);
395 expect(1, r);
396 r = SendMessage(hWnd, DTM_GETRANGE, 0, (LPARAM)getSt);
397 ok(r == (GDTR_MIN | GDTR_MAX), "Expected %x, not %x(GDTR_MIN) or %x(GDTR_MAX), got %lx\n", (GDTR_MIN | GDTR_MAX), GDTR_MIN, GDTR_MAX, r);
398 expect_systime(&st[0], &getSt[0]);
399 expect_systime(&st[1], &getSt[1]);
400
401 /* set st[0] to value higher than minimum */
402 fill_systime_struct(&st[0], 1980, 1, 3, 23, 14, 34, 37, 465);
403 /* set st[1] to value lower than maximum */
404 fill_systime_struct(&st[1], 2007, 3, 2, 31, 23, 59, 59, 999);
405
406 r = SendMessage(hWnd, DTM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st);
407 expect(1, r);
408 r = SendMessage(hWnd, DTM_GETRANGE, 0, (LPARAM)getSt);
409 ok(r == (GDTR_MIN | GDTR_MAX), "Expected %x, not %x(GDTR_MIN) or %x(GDTR_MAX), got %lx\n", (GDTR_MIN | GDTR_MAX), GDTR_MIN, GDTR_MAX, r);
410 expect_systime(&st[0], &getSt[0]);
411 expect_systime(&st[1], &getSt[1]);
412
413 ok_sequence(sequences, DATETIME_SEQ_INDEX, test_dtm_set_and_get_range_seq, "test_dtm_set_and_get_range", FALSE);
414
415 DestroyWindow(hWnd);
416 }
417
418 /* when max<min for DTM_SETRANGE, Windows seems to swap the min and max values,
419 although that's undocumented. However, it doesn't seem to be implemented
420 correctly, causing some strange side effects */
421 static void test_dtm_set_range_swap_min_max(void)
422 {
423 LRESULT r;
424 SYSTEMTIME st[2];
425 SYSTEMTIME getSt[2];
426 SYSTEMTIME origSt;
427 HWND hWnd;
428
429 hWnd = create_datetime_control(DTS_SHOWNONE);
430 flush_sequences(sequences, NUM_MSG_SEQUENCES);
431
432 fill_systime_struct(&st[0], 2007, 2, 4, 15, 2, 2, 2, 2);
433
434 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st[0]);
435 expect(1, r);
436 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&origSt);
437 ok(r == GDT_VALID, "Expected %d, not %d(GDT_NONE) or %d(GDT_ERROR), got %ld\n", GDT_VALID, GDT_NONE, GDT_ERROR, r);
438 expect_systime(&st[0], &origSt);
439
440 /* set st[0] to value higher than st[1] */
441 fill_systime_struct(&st[0], 2007, 3, 2, 31, 23, 59, 59, 999);
442 fill_systime_struct(&st[1], 1980, 1, 3, 23, 14, 34, 37, 465);
443
444 /* since min>max, min and max values should be swapped by DTM_SETRANGE
445 automatically */
446 r = SendMessage(hWnd, DTM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st);
447 expect(1, r);
448 r = SendMessage(hWnd, DTM_GETRANGE, 0, (LPARAM)getSt);
449 ok(r == (GDTR_MIN | GDTR_MAX), "Expected %x, not %x(GDTR_MIN) or %x(GDTR_MAX), got %lx\n", (GDTR_MIN | GDTR_MAX), GDTR_MIN, GDTR_MAX, r);
450 todo_wine {
451 ok(compare_systime(&st[0], &getSt[0]) == 1 ||
452 broken(compare_systime(&st[0], &getSt[1]) == 1), /* comctl32 version <= 5.80 */
453 "ST1 != ST2\n");
454
455 ok(compare_systime(&st[1], &getSt[1]) == 1 ||
456 broken(compare_systime(&st[1], &getSt[0]) == 1), /* comctl32 version <= 5.80 */
457 "ST1 != ST2\n");
458 }
459
460 fill_systime_struct(&st[0], 1980, 1, 3, 23, 14, 34, 37, 465);
461
462 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st[0]);
463 expect(1, r);
464 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt[0]);
465 ok(r == GDT_VALID, "Expected %d, not %d(GDT_NONE) or %d(GDT_ERROR), got %ld\n", GDT_VALID, GDT_NONE, GDT_ERROR, r);
466 /* the time part seems to not change after swapping the min and max values
467 and doing DTM_SETSYSTEMTIME */
468 expect_systime_date(&st[0], &getSt[0]);
469 todo_wine {
470 ok(compare_systime_time(&origSt, &getSt[0]) == 1 ||
471 broken(compare_systime_time(&st[0], &getSt[0]) == 1), /* comctl32 version <= 5.80 */
472 "ST1.time != ST2.time\n");
473 }
474
475 /* set st[0] to value higher than minimum */
476 fill_systime_struct(&st[0], 1980, 1, 3, 23, 14, 34, 37, 465);
477 /* set st[1] to value lower than maximum */
478 fill_systime_struct(&st[1], 2007, 3, 2, 31, 23, 59, 59, 999);
479
480 r = SendMessage(hWnd, DTM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st);
481 expect(1, r);
482 /* for some reason after we swapped the min and max values before,
483 whenever we do a DTM_SETRANGE, the DTM_GETRANGE will return the values
484 swapped*/
485 r = SendMessage(hWnd, DTM_GETRANGE, 0, (LPARAM)getSt);
486 ok(r == (GDTR_MIN | GDTR_MAX), "Expected %x, not %x(GDTR_MIN) or %x(GDTR_MAX), got %lx\n", (GDTR_MIN | GDTR_MAX), GDTR_MIN, GDTR_MAX, r);
487 todo_wine {
488 ok(compare_systime(&st[0], &getSt[1]) == 1 ||
489 broken(compare_systime(&st[0], &getSt[0]) == 1), /* comctl32 version <= 5.80 */
490 "ST1 != ST2\n");
491
492 ok(compare_systime(&st[1], &getSt[0]) == 1 ||
493 broken(compare_systime(&st[1], &getSt[1]) == 1), /* comctl32 version <= 5.80 */
494 "ST1 != ST2\n");
495 }
496
497 /* set st[0] to value higher than st[1] */
498 fill_systime_struct(&st[0], 2007, 3, 2, 31, 23, 59, 59, 999);
499 fill_systime_struct(&st[1], 1980, 1, 3, 23, 14, 34, 37, 465);
500
501 /* set min>max again, so that the return values of DTM_GETRANGE are no
502 longer swapped the next time we do a DTM SETRANGE and DTM_GETRANGE*/
503 r = SendMessage(hWnd, DTM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st);
504 expect(1, r);
505 r = SendMessage(hWnd, DTM_GETRANGE, 0, (LPARAM)getSt);
506 ok(r == (GDTR_MIN | GDTR_MAX), "Expected %x, not %x(GDTR_MIN) or %x(GDTR_MAX), got %lx\n", (GDTR_MIN | GDTR_MAX), GDTR_MIN, GDTR_MAX, r);
507 expect_systime(&st[0], &getSt[1]);
508 expect_systime(&st[1], &getSt[0]);
509
510 /* initialize st[0] to lowest possible value */
511 fill_systime_struct(&st[0], 1601, 1, 0, 1, 0, 0, 0, 0);
512 /* set st[1] to highest possible value */
513 fill_systime_struct(&st[1], 30827, 12, 6, 31, 23, 59, 59, 999);
514
515 r = SendMessage(hWnd, DTM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st);
516 expect(1, r);
517 r = SendMessage(hWnd, DTM_GETRANGE, 0, (LPARAM)getSt);
518 ok(r == (GDTR_MIN | GDTR_MAX), "Expected %x, not %x(GDTR_MIN) or %x(GDTR_MAX), got %lx\n", (GDTR_MIN | GDTR_MAX), GDTR_MIN, GDTR_MAX, r);
519 expect_systime(&st[0], &getSt[0]);
520 expect_systime(&st[1], &getSt[1]);
521
522 ok_sequence(sequences, DATETIME_SEQ_INDEX, test_dtm_set_range_swap_min_max_seq, "test_dtm_set_range_swap_min_max", FALSE);
523
524 DestroyWindow(hWnd);
525 }
526
527 static void test_dtm_set_and_get_system_time(void)
528 {
529 LRESULT r;
530 SYSTEMTIME st, getSt, ref;
531 HWND hWnd, hWndDateTime_test_gdt_none;
532
533 hWndDateTime_test_gdt_none = create_datetime_control(0);
534
535 ok(hWndDateTime_test_gdt_none!=NULL, "Expected non NULL, got %p\n", hWndDateTime_test_gdt_none);
536 if(hWndDateTime_test_gdt_none) {
537 r = SendMessage(hWndDateTime_test_gdt_none, DTM_SETSYSTEMTIME, GDT_NONE, (LPARAM)&st);
538 expect(0, r);
539 }
540 else {
541 skip("hWndDateTime_test_gdt_none is NULL\n");
542 flush_sequences(sequences, NUM_MSG_SEQUENCES);
543
544 return;
545 }
546
547 DestroyWindow(hWndDateTime_test_gdt_none);
548
549 hWnd = create_datetime_control(DTS_SHOWNONE);
550 flush_sequences(sequences, NUM_MSG_SEQUENCES);
551
552 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_NONE, (LPARAM)&st);
553 expect(1, r);
554 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt);
555 ok(r == GDT_NONE, "Expected %d, not %d(GDT_VALID) or %d(GDT_ERROR), got %ld\n", GDT_NONE, GDT_VALID, GDT_ERROR, r);
556
557 /* set st to lowest possible value */
558 fill_systime_struct(&st, 1601, 1, 0, 1, 0, 0, 0, 0);
559
560 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
561 expect(1, r);
562
563 /* set st to highest possible value */
564 fill_systime_struct(&st, 30827, 12, 6, 31, 23, 59, 59, 999);
565
566 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
567 expect(1, r);
568
569 /* set st to value between min and max */
570 fill_systime_struct(&st, 1980, 1, 3, 23, 14, 34, 37, 465);
571
572 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
573 expect(1, r);
574 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt);
575 ok(r == GDT_VALID, "Expected %d, not %d(GDT_NONE) or %d(GDT_ERROR), got %ld\n", GDT_VALID, GDT_NONE, GDT_ERROR, r);
576 expect_systime(&st, &getSt);
577
578 /* set st to invalid value */
579 fill_systime_struct(&st, 0, 0, 7, 0, 24, 60, 60, 1000);
580
581 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
582 expect_unsuccess(0, r);
583
584 ok_sequence(sequences, DATETIME_SEQ_INDEX, test_dtm_set_and_get_system_time_seq, "test_dtm_set_and_get_system_time", FALSE);
585
586 /* set to some valid value */
587 GetSystemTime(&ref);
588 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&ref);
589 expect(1, r);
590 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt);
591 expect(GDT_VALID, r);
592 expect_systime(&ref, &getSt);
593
594 /* year invalid */
595 st = ref;
596 st.wYear = 0;
597 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
598 todo_wine expect(1, r);
599 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt);
600 expect(GDT_VALID, r);
601 expect_systime(&ref, &getSt);
602 /* month invalid */
603 st = ref;
604 st.wMonth = 13;
605 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
606 expect(0, r);
607 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt);
608 expect(GDT_VALID, r);
609 expect_systime(&ref, &getSt);
610 /* day invalid */
611 st = ref;
612 st.wDay = 32;
613 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
614 expect(0, r);
615 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt);
616 expect(GDT_VALID, r);
617 expect_systime(&ref, &getSt);
618 /* day invalid for current month */
619 st = ref;
620 st.wDay = 30;
621 st.wMonth = 2;
622 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
623 expect(0, r);
624 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt);
625 expect(GDT_VALID, r);
626 expect_systime(&ref, &getSt);
627 /* day of week isn't validated */
628 st = ref;
629 st.wDayOfWeek = 10;
630 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
631 expect(1, r);
632 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt);
633 expect(GDT_VALID, r);
634 expect_systime(&ref, &getSt);
635 /* hour invalid */
636 st = ref;
637 st.wHour = 25;
638 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
639 expect(0, r);
640 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt);
641 expect(GDT_VALID, r);
642 expect_systime(&ref, &getSt);
643 /* minute invalid */
644 st = ref;
645 st.wMinute = 60;
646 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
647 expect(0, r);
648 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt);
649 expect(GDT_VALID, r);
650 expect_systime(&ref, &getSt);
651 /* sec invalid */
652 st = ref;
653 st.wSecond = 60;
654 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
655 expect(0, r);
656 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt);
657 expect(GDT_VALID, r);
658 expect_systime(&ref, &getSt);
659 /* msec invalid */
660 st = ref;
661 st.wMilliseconds = 1000;
662 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
663 expect(0, r);
664 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt);
665 expect(GDT_VALID, r);
666 expect_systime(&ref, &getSt);
667
668 /* day of week should be calculated automatically,
669 actual day of week for this date is 4 */
670 fill_systime_struct(&st, 2009, 10, 1, 1, 0, 0, 10, 200);
671 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st);
672 expect(1, r);
673 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt);
674 expect(GDT_VALID, r);
675 /* 01.10.2009 is Thursday */
676 expect(4, (LRESULT)getSt.wDayOfWeek);
677 st.wDayOfWeek = 4;
678 expect_systime(&st, &getSt);
679 }
680
681 static void test_dtm_set_and_get_systemtime_with_limits(void)
682 {
683 LRESULT r;
684 SYSTEMTIME st[2], getSt[2], refSt;
685 HWND hWnd;
686
687 hWnd = create_datetime_control(DTS_SHOWNONE);
688
689 flush_sequences(sequences, NUM_MSG_SEQUENCES);
690
691 /* set range */
692 fill_systime_struct(&st[0], 1980, 1, 3, 23, 14, 34, 37, 465);
693 fill_systime_struct(&st[1], 2007, 3, 2, 31, 23, 59, 59, 999);
694
695 r = SendMessage(hWnd, DTM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)st);
696 expect(1, r);
697 r = SendMessage(hWnd, DTM_GETRANGE, 0, (LPARAM)getSt);
698 ok(r == (GDTR_MIN | GDTR_MAX), "Expected %x, not %x(GDTR_MIN) or %x(GDTR_MAX), got %lx\n", (GDTR_MIN | GDTR_MAX), GDTR_MIN, GDTR_MAX, r);
699 expect_systime(&st[0], &getSt[0]);
700 expect_systime(&st[1], &getSt[1]);
701
702 /* Initially set a valid time */
703 fill_systime_struct(&refSt, 1999, 9, 4, 9, 19, 9, 9, 999);
704 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&refSt);
705 expect(1, r);
706 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt[0]);
707 ok(r == GDT_VALID, "Expected %d, not %d(GDT_NONE) or %d(GDT_ERROR), got %ld\n", GDT_VALID, GDT_NONE, GDT_ERROR, r);
708 expect_systime(&refSt, &getSt[0]);
709
710 /* Now set an out-of-bounds time */
711 fill_systime_struct(&st[0], 2010, 1, 0, 1, 0, 0, 0, 0);
712
713 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st[0]);
714 expect(1, r);
715 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt[0]);
716 ok(r == GDT_VALID, "Expected %d, not %d(GDT_NONE) or %d(GDT_ERROR), got %ld\n", GDT_VALID, GDT_NONE, GDT_ERROR, r);
717 expect_systime(&refSt, &getSt[0]);
718
719 fill_systime_struct(&st[0], 1977, 1, 0, 1, 0, 0, 0, 0);
720
721 r = SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&st[0]);
722 expect(1, r);
723 r = SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&getSt[0]);
724 ok(r == GDT_VALID, "Expected %d, not %d(GDT_NONE) or %d(GDT_ERROR), got %ld\n", GDT_VALID, GDT_NONE, GDT_ERROR, r);
725 expect_systime(&refSt, &getSt[0]);
726
727 ok_sequence(sequences, DATETIME_SEQ_INDEX, test_dtm_set_and_get_systime_with_limits, "test_dtm_set_and_get_systime_with_limits", FALSE);
728
729 DestroyWindow(hWnd);
730 }
731
732 static void test_wm_set_get_text(void)
733 {
734 static const CHAR a_str[] = "a";
735 CHAR buff[16], time[16], caltype[3];
736 HWND hWnd;
737 LRESULT ret;
738
739 hWnd = create_datetime_control(0);
740
741 ret = SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)a_str);
742 ok(CB_ERR == ret ||
743 broken(0 == ret) || /* comctl32 <= 4.72 */
744 broken(1 == ret), /* comctl32 <= 4.70 */
745 "Expected CB_ERR, got %ld\n", ret);
746
747 buff[0] = 0;
748 ret = SendMessage(hWnd, WM_GETTEXT, sizeof(buff), (LPARAM)buff);
749 ok(strcmp(buff, a_str) != 0, "Expected text to change, got %s\n", buff);
750 ok(ret != 0, "Expected non-zero return value\n");
751
752 SetLastError(0xdeadbeef);
753 ret = GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_ICALENDARTYPE, caltype, 3);
754 if (ret == 0)
755 skip("Must know local calendar type (%x)\n", GetLastError());
756 else if (atoi(caltype) != CAL_GREGORIAN)
757 skip("DateTimePicker Control only supports Gregorian calendar (type: %s)\n", caltype);
758 else {
759 SetLastError(0xdeadbeef);
760 ret = GetDateFormat(LOCALE_USER_DEFAULT, 0, NULL, NULL, time, sizeof(time));
761 if (ret == 0)
762 skip("GetDateFormat failed, returned %ld, error %d\n", ret, GetLastError());
763 else
764 ok(!strcmp(buff, time), "Expected %s, got %s\n", time, buff);
765 }
766
767 DestroyWindow(hWnd);
768 }
769
770 static void test_dts_shownone(void)
771 {
772 HWND hwnd;
773 DWORD style;
774
775 /* it isn't allowed to change DTS_SHOWNONE after creation */
776 hwnd = create_datetime_control(0);
777 style = GetWindowLong(hwnd, GWL_STYLE);
778 SetWindowLong(hwnd, GWL_STYLE, style | DTS_SHOWNONE);
779 style = GetWindowLong(hwnd, GWL_STYLE);
780 ok(!(style & DTS_SHOWNONE), "Expected DTS_SHOWNONE not to be set\n");
781 DestroyWindow(hwnd);
782
783 hwnd = create_datetime_control(DTS_SHOWNONE);
784 style = GetWindowLong(hwnd, GWL_STYLE);
785 SetWindowLong(hwnd, GWL_STYLE, style & ~DTS_SHOWNONE);
786 style = GetWindowLong(hwnd, GWL_STYLE);
787 ok(style & DTS_SHOWNONE, "Expected DTS_SHOWNONE to be set\n");
788 DestroyWindow(hwnd);
789 }
790
791 START_TEST(datetime)
792 {
793 HMODULE hComctl32;
794 BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
795 INITCOMMONCONTROLSEX iccex;
796
797 hComctl32 = GetModuleHandleA("comctl32.dll");
798 pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
799 if (!pInitCommonControlsEx)
800 {
801 skip("InitCommonControlsEx() is missing. Skipping the tests\n");
802 return;
803 }
804 iccex.dwSize = sizeof(iccex);
805 iccex.dwICC = ICC_DATE_CLASSES;
806 pInitCommonControlsEx(&iccex);
807
808 init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
809
810 test_dtm_set_format();
811 test_dtm_set_and_get_mccolor();
812 test_dtm_set_and_get_mcfont();
813 test_dtm_get_monthcal();
814 test_dtm_set_and_get_range();
815 test_dtm_set_range_swap_min_max();
816 test_dtm_set_and_get_system_time();
817 test_dtm_set_and_get_systemtime_with_limits();
818 test_wm_set_get_text();
819 test_dts_shownone();
820 }