mshtml: Implement IHTMLDOMNode replaceChild.
[wine] / dlls / user32 / tests / dialog.c
1 /* Unit test suite for the dialog functions.
2  *
3  * Copyright 2004 Bill Medland
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  *
21  * This test suite currently works by building a quite complex hierarchy of
22  * objects in a variety of styles and then performs a limited number of tests
23  * for the previous and next dialog group or tab items.
24  *
25  * The test specifically does not test all possibilities at this time since
26  * there are several cases where the Windows behaviour is rather strange and
27  * significant work would be required to get the Wine code to duplicate the
28  * strangeness, especially since most are in situations that would not
29  * normally be met.
30  */
31
32 #include <assert.h>
33 #include <stdio.h>
34 #include <stdarg.h>
35
36 #include "wine/test.h"
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41
42 #define MAXHWNDS 1024
43 static HWND hwnd [MAXHWNDS];
44 static unsigned int numwnds=1; /* 0 is reserved for null */
45
46 /* Global handles */
47 static HINSTANCE g_hinst;                          /* This application's HINSTANCE */
48 static HWND g_hwndMain, g_hwndButton1, g_hwndButton2, g_hwndButtonCancel;
49 static HWND g_hwndTestDlg, g_hwndTestDlgBut1, g_hwndTestDlgBut2, g_hwndTestDlgEdit;
50 static HWND g_hwndInitialFocusT1, g_hwndInitialFocusT2, g_hwndInitialFocusGroupBox;
51
52 static LONG g_styleInitialFocusT1, g_styleInitialFocusT2;
53 static BOOL g_bInitialFocusInitDlgResult, g_bReceivedCommand;
54
55 static int g_terminated;
56
57 typedef struct {
58     INT_PTR id;
59     int parent;
60     DWORD style;
61     DWORD exstyle;
62 } h_entry;
63
64 static const h_entry hierarchy [] = {
65     /* 0 is reserved for the null window */
66     {  1,  0, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE},
67     { 20,  1,  WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
68     {  2,  1,  WS_CHILD | WS_VISIBLE, WS_EX_CONTROLPARENT},
69     { 60,  2,  WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
70     /* What happens with groups when the parent is disabled */
71     {  8,  2,  WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_TABSTOP, WS_EX_CONTROLPARENT},
72     { 85,  8,  WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP, 0},
73     {  9,  8,  WS_CHILD, WS_EX_CONTROLPARENT},
74     { 86,  9,  WS_CHILD | WS_VISIBLE, 0},
75     { 87,  9,  WS_CHILD | WS_VISIBLE, 0},
76     { 31,  8,  WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
77     { 10,  2,  WS_CHILD | WS_VISIBLE, WS_EX_CONTROLPARENT},
78     { 88, 10,  WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
79     { 11, 10,  WS_CHILD, WS_EX_CONTROLPARENT},
80     { 89, 11,  WS_CHILD | WS_VISIBLE, 0},
81     { 32, 11,  WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
82     { 90, 11,  WS_CHILD | WS_VISIBLE, 0},
83     { 33, 10,  WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
84     { 21,  2,  WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
85     { 61,  2,  WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
86     {  3,  1,  WS_CHILD | WS_VISIBLE | DS_CONTROL, 0},
87     { 22,  3,  WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
88     { 62,  3,  WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
89     {  7,  3,  WS_CHILD | WS_VISIBLE, WS_EX_CONTROLPARENT},
90     {  4,  7,  WS_CHILD | WS_VISIBLE | DS_CONTROL, 0},
91     { 83,  4,  WS_CHILD | WS_VISIBLE, 0},
92     {  5,  4,  WS_CHILD | WS_VISIBLE | DS_CONTROL, 0},
93     /* A couple of controls around the main dialog */
94     { 29,  5,  WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
95     { 81,  5,  WS_CHILD | WS_VISIBLE, 0},
96     /* The main dialog with lots of controls */
97     {  6,  5,  WS_CHILD | WS_VISIBLE, WS_EX_CONTROLPARENT},
98         /* At the start of a dialog */
99         /* Disabled controls are skipped */
100     { 63,  6,  WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_TABSTOP, 0},
101         /* Invisible controls are skipped */
102     { 64,  6,  WS_CHILD | WS_TABSTOP, 0},
103         /* Invisible disabled controls are skipped */
104     { 65,  6,  WS_CHILD | WS_DISABLED | WS_TABSTOP, 0},
105         /* Non-tabstop controls are skipped for tabs but not for groups */
106     { 66,  6,  WS_CHILD | WS_VISIBLE, 0},
107         /* End of first group, with no tabstops in it */
108     { 23,  6,  WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
109         /* At last a tabstop */
110     { 67,  6,  WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
111     /* A group that is totally disabled or invisible */
112     { 24,  6,  WS_CHILD | WS_DISABLED | WS_GROUP, 0},
113     { 68,  6,  WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_TABSTOP, 0},
114     { 69,  6,  WS_CHILD | WS_TABSTOP, 0},
115     /* A valid group in the middle of the dialog (not the first nor last group*/
116     { 25,  6,  WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
117         /* A non-tabstop item will be skipped for tabs */
118     { 70,  6,  WS_CHILD | WS_VISIBLE, 0},
119         /* A disabled item will be skipped for tabs and groups */
120     { 71,  6,  WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_TABSTOP, 0},
121         /* A valid item will be found for tabs and groups */
122     { 72,  6,  WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
123         /* A disabled item to skip when looking for the next group item */
124     { 73,  6,  WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_TABSTOP, 0},
125     /* The next group begins with an enabled visible label */
126     { 26,  6,  WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
127     { 74,  6,  WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
128     { 75,  6,  WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
129     /* That group is terminated by a disabled label */
130     { 27,  6,  WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_GROUP, 0},
131     { 76,  6,  WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
132     { 77,  6,  WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
133     /* That group is terminated by an invisible label */
134     { 28,  6,  WS_CHILD | WS_GROUP, 0},
135     /* The end of the dialog with item for loop and recursion testing */
136     { 78,  6,  WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
137     /* No tabstop so skipped for prev tab, but found for prev group */
138     { 79,  6,  WS_CHILD | WS_VISIBLE, 0},
139     { 80,  6,  WS_CHILD | WS_VISIBLE | WS_DISABLED | WS_TABSTOP, 0},
140     /* A couple of controls after the main dialog */
141     { 82,  5,  WS_CHILD | WS_VISIBLE, 0},
142     { 30,  5,  WS_CHILD | WS_VISIBLE | WS_GROUP, 0},
143     /* And around them */
144     { 84,  4,  WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0},
145     {0, 0, 0, 0}
146 };
147
148 static BOOL CreateWindows (HINSTANCE hinst)
149 {
150     const h_entry *p = hierarchy;
151
152     while (p->id != 0)
153     {
154         DWORD style, exstyle;
155         char ctrlname[9];
156
157         /* Basically assert that the hierarchy is valid and track the
158          * maximum control number
159          */
160         if (p->id >= numwnds)
161         {
162             if (p->id >=  sizeof(hwnd)/sizeof(hwnd[0]))
163             {
164                 trace ("Control %ld is out of range\n", p->id);
165                 return FALSE;
166             }
167             else
168                 numwnds = p->id+1;
169         }
170         if (p->id <= 0)
171         {
172             trace ("Control %ld is out of range\n", p->id);
173             return FALSE;
174         }
175         if (hwnd[p->id] != 0)
176         {
177             trace ("Control %ld is used more than once\n", p->id);
178             return FALSE;
179         }
180
181         /* Create the control */
182         sprintf (ctrlname, "ctrl%4.4ld", p->id);
183         hwnd[p->id] = CreateWindowEx (p->exstyle, TEXT(p->parent ? "static" : "GetNextDlgItemWindowClass"), TEXT(ctrlname), p->style, 10, 10, 10, 10, hwnd[p->parent], p->parent ? (HMENU) (2000 + p->id) : 0, hinst, 0);
184         if (!hwnd[p->id])
185         {
186             trace ("Failed to create control %ld\n", p->id);
187             return FALSE;
188         }
189
190         /* Check that the styles are as we specified (except the main one
191          * which is quite frequently messed up).  If this keeps breaking then
192          * we could mask out the bits that don't concern us.
193          */
194         if (p->parent)
195         {
196             style = GetWindowLong (hwnd[p->id], GWL_STYLE);
197             exstyle = GetWindowLong (hwnd[p->id], GWL_EXSTYLE);
198             if (style != p->style || exstyle != p->exstyle)
199             {
200                 trace ("Style mismatch at %ld: %8.8x %8.8x cf %8.8x %8.8x\n", p->id, style, exstyle, p->style, p->exstyle);
201             }
202         }
203         p++;
204     }
205
206     return TRUE;
207 }
208
209 /* Form the lParam of a WM_KEYDOWN message */
210 static DWORD KeyDownData (int repeat, int scancode, int extended, int wasdown)
211 {
212     return ((repeat & 0x0000FFFF) | ((scancode & 0x00FF) << 16) |
213             (extended ? 0x01000000 : 0) | (wasdown ? 0x40000000 : 0));
214 }
215
216 /* Form a WM_KEYDOWN VK_TAB message to the specified window */
217 static void FormTabMsg (MSG *pMsg, HWND hwnd)
218 {
219     pMsg->hwnd = hwnd;
220     pMsg->message = WM_KEYDOWN;
221     pMsg->wParam = VK_TAB;
222     pMsg->lParam = KeyDownData (1, 0x0F, 0, 0);
223     /* pMsg->time is not set.  It shouldn't be needed */
224     /* pMsg->pt is ignored */
225 }
226
227 /* Form a WM_KEYDOWN VK_RETURN message to the specified window */
228 static void FormEnterMsg (MSG *pMsg, HWND hwnd)
229 {
230     pMsg->hwnd = hwnd;
231     pMsg->message = WM_KEYDOWN;
232     pMsg->wParam = VK_RETURN;
233     pMsg->lParam = KeyDownData (1, 0x1C, 0, 0);
234     /* pMsg->time is not set.  It shouldn't be needed */
235     /* pMsg->pt is ignored */
236 }
237
238 /***********************************************************************
239  *
240  * The actual tests
241  */
242
243 typedef struct
244 {
245     int isok; /* or is it todo */
246     int test;
247     int dlg;
248     int ctl;
249     int tab;
250     int prev;
251     int res;
252 } test_record;
253
254 static int id (HWND h)
255 {
256     unsigned int i;
257     for (i = 0; i < numwnds; i++)
258         if (hwnd[i] == h)
259             return i;
260     return -1;
261 }
262
263 /* Tests
264  *
265  * Tests 1-8 test the hCtl argument of null or the dialog itself.
266  *
267  *   1. Prev Group of null is null
268  *   2. Prev Tab of null is null
269  *   3. Prev Group of hDlg in hDlg is null
270  *   4. Prev Tab of hDlg in hDlg is null
271  *   5. Next Group of null is first visible enabled child
272  *      Check it skips invisible, disabled and both.
273  *   6. Next Tab of null is first visible enabled tabstop
274  *      Check it skips invisible, disabled, nontabstop, and in combination.
275  *   7. Next Group of hDlg in hDlg is as of null
276  *   8. Next Tab of hDlg in hDlg is as of null
277  *
278  * Tests 9-14 test descent
279  *
280  *   9. DS_CONTROL does not result in descending the hierarchy for Tab Next
281  *  10. DS_CONTROL does not result in descending the hierarchy for Group Next
282  *  11. WS_EX_CONTROLPARENT results in descending the hierarchy for Tab Next
283  *  12. WS_EX_CONTROLPARENT results in descending the hierarchy for Group Next
284  *  13. WS_EX_CONTROLPARENT results in descending the hierarchy for Tab Prev
285  *  14. WS_EX_CONTROLPARENT results in descending the hierarchy for Group Prev
286  *
287  * Tests 15-24 are the basic Prev/Next Group tests
288  *
289  *  15. Next Group of a visible enabled non-group control is the next visible
290  *      enabled non-group control, if there is one before the next group
291  *  16. Next Group of a visible enabled non-group control wraps around to the
292  *      beginning of the group on finding a control that starts another group.
293  *      Note that the group is in the middle of the dialog.
294  *  17. As 16 except note that the next group is started with a disabled
295  *      visible control.
296  *  18. As 16 except note that the next group is started with an invisible
297  *      enabled control.
298  *  19. Next Group wraps around the controls of the dialog
299  *  20. Next Group is the same even if the initial control is disabled.
300  *  21. Next Group is the same even if the initial control is invisible.
301  *  22. Next Group is the same even if the initial control has the group style
302  *  23. Next Group returns the initial control if there is no visible enabled
303  *      control in the group. (Initial control disabled and not group style).
304  *  24. Prev version of test 16.
305  *      Prev Group of a visible enabled non-group control wraps around to the
306  *      beginning of the group on finding a control that starts the group.
307  *      Note that the group is in the middle of the dialog.
308  *
309  * In tests 25 to 28 the control is sitting under dialogs which do not have
310  * the WS_EX_CONTROLPARENT style and so cannot be reached from the top of
311  * the dialog.
312  *
313  *  25. Next Group of an inaccessible control is as if it were accessible
314  *  26. Prev Group of an inaccessible control begins searching at the highest
315  *      level ancestor that did not permit recursion down the hierarchy
316  *  27. Next Tab of an inaccessible control is as if it were accessible
317  *  28. Prev Tab of an inaccessible control begins searching at the highest
318  *      level ancestor that did not permit recursion down the hierarchy.
319  *
320  * Tests 29- are the basic Tab tests
321  *
322  *  29. Next Tab of a control is the next visible enabled control with the
323  *      Tabstop style (N.B. skips disabled, invisible and non-tabstop)
324  *  30. Prev Tab of a control is the previous visible enabled control with the
325  *      Tabstop style (N.B. skips disabled, invisible and non-tabstop)
326  *  31. Next Tab test with at least two layers of descent and finding the
327  *      result not at the first control.
328  *  32. Next Tab test with at least two layers of descent with the descent and
329  *      control at the start of each level.
330  *  33. Prev Tab test with at least two layers of descent and finding the
331  *      result not at the last control.
332  *  34. Prev Tab test with at least two layers of descent with the descent and
333  *      control at the end of each level.
334  *
335  *  35. Passing NULL may result in the first child being the one returned.
336  *      (group test)
337  *  36. Passing NULL may result in the first child being the one returned.
338  *      (tab test)
339  */
340
341 static void test_GetNextDlgItem(void)
342 {
343     static test_record test [] =
344     {
345         /* isok test dlg  ctl  tab  prev res  */
346
347         {   1,   1,    6,   0,   0,   1,   0},
348         {   1,   2,    6,   0,   1,   1,   0},
349         {   1,   3,    6,   6,   0,   1,   0},
350         {   1,   4,    6,   6,   1,   1,   0},
351         {   1,   5,    6,   0,   0,   0,  66},
352         {   1,   6,    6,   0,   1,   0,  67},
353         {   1,   7,    6,   6,   0,   0,  66},
354         {   1,   8,    6,   6,   1,   0,  67},
355
356         {   1,   9,    4,  83,   1,   0,  84},
357         {   1,  10,    4,  83,   0,   0,   5},
358         {   1,  11,    5,  81,   1,   0,  67},
359         {   1,  12,    5,  81,   0,   0,  66},
360         {   1,  13,    5,  82,   1,   1,  78},
361
362         {   1,  14,    5,  82,   0,   1,  79},
363         {   1,  15,    6,  70,   0,   0,  72},
364         {   1,  16,    6,  72,   0,   0,  25},
365         {   1,  17,    6,  75,   0,   0,  26},
366         {   1,  18,    6,  77,   0,   0,  76},
367         {   1,  19,    6,  79,   0,   0,  66},
368         {   1,  20,    6,  71,   0,   0,  72},
369         {   1,  21,    6,  64,   0,   0,  66},
370
371         {   1,  22,    6,  25,   0,   0,  70},
372         {   1,  23,    6,  68,   0,   0,  68},
373         {   1,  24,    6,  25,   0,   1,  72},
374         {   1,  25,    1,  70,   0,   0,  72},
375         /*{   0,  26,    1,  70,   0,   1,   3}, Crashes Win95*/
376         {   1,  27,    1,  70,   1,   0,  72},
377         /*{   0,  28,    1,  70,   1,   1,  61}, Crashes Win95*/
378
379         {   1,  29,    6,  67,   1,   0,  72},
380         {   1,  30,    6,  72,   1,   1,  67},
381
382         {   1,  35,    2,   0,   0,   0,  60},
383         {   1,  36,    2,   0,   1,   0,  60},
384
385         {   0,   0,    0,   0,   0,   0,   0}  /* End of test */
386     };
387     const test_record *p = test;
388
389     ok (CreateWindows (g_hinst), "Could not create test windows\n");
390
391     while (p->dlg)
392     {
393         HWND a;
394         a = (p->tab ? GetNextDlgTabItem : GetNextDlgGroupItem) (hwnd[p->dlg], hwnd[p->ctl], p->prev);
395         if (p->isok)
396         {
397             ok (a == hwnd[p->res], "Test %d: %s %s item of %d in %d was %d instead of %d\n", p->test, p->prev ? "Prev" : "Next", p->tab ? "Tab" : "Group", p->ctl, p->dlg, id(a), p->res);
398         }
399         else
400         {
401             todo_wine
402             {
403                 ok (a == hwnd[p->res], "Test %d: %s %s item of %d in %d was actually  %d matching expected %d\n", p->test, p->prev ? "Prev" : "Next", p->tab ? "Tab" : "Group", p->ctl, p->dlg, id(a), p->res);
404             }
405         }
406         p++;
407     }
408 }
409
410 /*
411  *  OnMainWindowCreate
412  */
413 static BOOL OnMainWindowCreate (HWND hwnd, LPCREATESTRUCT lpcs)
414 {
415     g_hwndButton1 = CreateWindow (TEXT("button"), TEXT("Button &1"),
416             WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_DEFPUSHBUTTON | BS_TEXT,
417             10, 10, 80, 80, hwnd, (HMENU)100, g_hinst, 0);
418     if (!g_hwndButton1) return FALSE;
419
420     g_hwndButton2 = CreateWindow (TEXT("button"), TEXT("Button &2"),
421             WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_TEXT,
422             110, 10, 80, 80, hwnd, (HMENU)200, g_hinst, 0);
423     if (!g_hwndButton2) return FALSE;
424
425     g_hwndButtonCancel = CreateWindow (TEXT("button"), TEXT("Cancel"),
426             WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_TEXT,
427             210, 10, 80, 80, hwnd, (HMENU)IDCANCEL, g_hinst, 0);
428     if (!g_hwndButtonCancel) return FALSE;
429
430     return TRUE;
431 }
432
433
434 /*
435  *  OnTestDlgCreate
436  */
437
438 static BOOL OnTestDlgCreate (HWND hwnd, LPCREATESTRUCT lpcs)
439 {
440     g_hwndTestDlgEdit = CreateWindowEx ( WS_EX_LEFT | WS_EX_LTRREADING |
441             WS_EX_RIGHTSCROLLBAR | WS_EX_NOPARENTNOTIFY | WS_EX_CLIENTEDGE,
442             TEXT("Edit"), TEXT("Edit"),
443             WS_CHILDWINDOW | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL,
444             16,33,184,24, hwnd, (HMENU)101, g_hinst, 0);
445     if (!g_hwndTestDlgEdit) return FALSE;
446
447     g_hwndTestDlgBut1 = CreateWindowEx ( WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR
448             | WS_EX_NOPARENTNOTIFY,
449             TEXT("button"), TEXT("Button &1"),
450             WS_CHILDWINDOW | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_TEXT,
451             204,33,30,24, hwnd, (HMENU)201, g_hinst, 0);
452     if (!g_hwndTestDlgBut1) return FALSE;
453
454     g_hwndTestDlgBut2 = CreateWindowEx ( WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR
455             | WS_EX_NOPARENTNOTIFY, TEXT("button"),
456             TEXT("Button &2"),
457             WS_CHILDWINDOW | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_TEXT,
458             90,102,80,24, hwnd, (HMENU)IDCANCEL, g_hinst, 0);
459     if (!g_hwndTestDlgBut2) return FALSE;
460
461     return TRUE;
462 }
463
464 static LRESULT CALLBACK main_window_procA (HWND hwnd, UINT uiMsg, WPARAM wParam,
465         LPARAM lParam)
466 {
467     switch (uiMsg)
468     {
469         /* Add blank case statements for these to ensure we don't use them
470          * by mistake.
471          */
472         case DM_GETDEFID: break;
473         case DM_SETDEFID: break;
474
475         case WM_CREATE:
476             return (OnMainWindowCreate (hwnd,
477                     (LPCREATESTRUCTA) lParam) ? 0 : (LRESULT) -1);
478         case WM_COMMAND:
479             if (wParam == IDCANCEL)
480             {
481                 g_terminated = TRUE;
482                 return 0;
483             }
484             break;
485     }
486
487     return DefWindowProcA (hwnd, uiMsg, wParam, lParam);
488 }
489
490 static LRESULT CALLBACK disabled_test_proc (HWND hwnd, UINT uiMsg,
491         WPARAM wParam, LPARAM lParam)
492 {
493     switch (uiMsg)
494     {
495         case WM_INITDIALOG:
496         {
497             DWORD dw;
498             HWND hwndOk;
499
500             dw = SendMessage(hwnd, DM_GETDEFID, 0, 0);
501             assert(DC_HASDEFID == HIWORD(dw));
502             hwndOk = GetDlgItem(hwnd, LOWORD(dw));
503             assert(hwndOk);
504             EnableWindow(hwndOk, FALSE);
505
506             PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0);
507             PostMessage(hwnd, WM_COMMAND, IDCANCEL, 0);
508             break;
509         }
510         case WM_COMMAND:
511             if (wParam == IDOK)
512             {
513                 g_terminated = TRUE;
514                 EndDialog(hwnd, 0);
515                 return 0;
516             }
517             else if (wParam == IDCANCEL)
518             {
519                 EndDialog(hwnd, 0);
520                 return 0;
521             }
522             break;
523     }
524
525     return DefWindowProcA (hwnd, uiMsg, wParam, lParam);
526 }
527
528 static LRESULT CALLBACK testDlgWinProc (HWND hwnd, UINT uiMsg, WPARAM wParam,
529         LPARAM lParam)
530 {
531     switch (uiMsg)
532     {
533         /* Add blank case statements for these to ensure we don't use them
534          * by mistake.
535          */
536         case DM_GETDEFID: break;
537         case DM_SETDEFID: break;
538
539         case WM_CREATE:
540             return (OnTestDlgCreate (hwnd,
541                     (LPCREATESTRUCTA) lParam) ? 0 : (LRESULT) -1);
542         case WM_COMMAND:
543             if(LOWORD(wParam) == 300) g_bReceivedCommand = TRUE;
544     }
545
546     return DefDlgProcA (hwnd, uiMsg, wParam, lParam);
547 }
548
549 static BOOL RegisterWindowClasses (void)
550 {
551     WNDCLASSA cls;
552
553     cls.style = 0;
554     cls.lpfnWndProc = DefWindowProcA;
555     cls.cbClsExtra = 0;
556     cls.cbWndExtra = 0;
557     cls.hInstance = g_hinst;
558     cls.hIcon = NULL;
559     cls.hCursor = LoadCursorA (NULL, IDC_ARROW);
560     cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
561     cls.lpszMenuName = NULL;
562     cls.lpszClassName = "GetNextDlgItemWindowClass";
563
564     if (!RegisterClassA (&cls)) return FALSE;
565
566     cls.lpfnWndProc = main_window_procA;
567     cls.lpszClassName = "IsDialogMessageWindowClass";
568
569     if (!RegisterClassA (&cls)) return FALSE;
570
571     GetClassInfoA(0, "#32770", &cls);
572     cls.lpfnWndProc = testDlgWinProc;
573     cls.lpszClassName = "WM_NEXTDLGCTLWndClass";
574     if (!RegisterClassA (&cls)) return FALSE;
575
576     return TRUE;
577 }
578
579 static void test_WM_NEXTDLGCTL(void)
580 {
581     HWND child1, child2, child3;
582     MSG msg;
583     DWORD dwVal;
584
585     g_hwndTestDlg = CreateWindowEx( WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR
586               | WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE | WS_EX_CONTROLPARENT | WS_EX_APPWINDOW,
587               "WM_NEXTDLGCTLWndClass",
588               "WM_NEXTDLGCTL Message test window",
589               WS_POPUPWINDOW | WS_CLIPSIBLINGS | WS_DLGFRAME | WS_OVERLAPPED |
590               WS_MINIMIZEBOX | WS_MAXIMIZEBOX | DS_3DLOOK | DS_SETFONT | DS_MODALFRAME,
591               0, 0, 235, 135,
592               NULL, NULL, g_hinst, 0);
593
594     assert (g_hwndTestDlg);
595     assert (g_hwndTestDlgBut1);
596     assert (g_hwndTestDlgBut2);
597     assert (g_hwndTestDlgEdit);
598
599     /*
600      * Test message DM_SETDEFID
601      */
602
603     DefDlgProcA( g_hwndTestDlg, DM_SETDEFID, IDCANCEL, 0 );
604     DefDlgProcA( g_hwndTestDlgBut1, BM_SETSTYLE, BS_DEFPUSHBUTTON, FALSE );
605     dwVal = DefDlgProcA(g_hwndTestDlg, DM_GETDEFID, 0, 0);
606
607     ok ( IDCANCEL == (LOWORD(dwVal)), "Did not set default ID\n" );
608
609     /*
610      * Check whether message WM_NEXTDLGCTL is changing the focus to next control and if
611      * the destination control is a button, style of the button should be changed to
612      * BS_DEFPUSHBUTTON with out making it default.
613      */
614
615     /*
616      * Keep the focus on Edit control.
617      */
618
619     if ( SetFocus( g_hwndTestDlgEdit ) )
620     {
621          ok ((GetFocus() == g_hwndTestDlgEdit), "Focus didn't set on Edit control\n");
622
623         /*
624          * Test message WM_NEXTDLGCTL
625          */
626         DefDlgProcA( g_hwndTestDlg, WM_NEXTDLGCTL, 0, 0 );
627         ok ((GetFocus() == g_hwndTestDlgBut1), "Focus didn't move to first button\n");
628
629         /*
630          * Check whether the default button ID got changed by sending message "WM_NEXTDLGCTL"
631          */
632         dwVal = DefDlgProcA(g_hwndTestDlg, DM_GETDEFID, 0, 0);
633         ok ( IDCANCEL == (LOWORD(dwVal)), "WM_NEXTDLGCTL changed default button\n");
634
635         /*
636          * Check whether the style of the button which got the focus, changed to BS_DEFPUSHBUTTON and
637          * the style of default button changed to BS_PUSHBUTTON.
638          */
639         if ( IDCANCEL == (LOWORD(dwVal)) )
640         {
641                 ok ( ((GetWindowLong( g_hwndTestDlgBut1, GWL_STYLE)) & BS_DEFPUSHBUTTON),
642                         "Button1 style not set to BS_DEFPUSHBUTTON\n" );
643
644                 ok ( !((GetWindowLong( g_hwndTestDlgBut2, GWL_STYLE)) & BS_DEFPUSHBUTTON),
645                         "Button2's style not changed to BS_PUSHBUTTON\n" );
646         }
647
648         /*
649          * Move focus to Button2 using "WM_NEXTDLGCTL"
650          */
651         DefDlgProcA( g_hwndTestDlg, WM_NEXTDLGCTL, 0, 0 );
652         ok ((GetFocus() == g_hwndTestDlgBut2), "Focus didn't move to second button\n");
653
654         /*
655          * Check whether the default button ID got changed by sending message "WM_NEXTDLGCTL"
656          */
657         dwVal = DefDlgProcA(g_hwndTestDlg, DM_GETDEFID, 0, 0);
658         ok ( IDCANCEL == (LOWORD(dwVal)), "WM_NEXTDLGCTL changed default button\n");
659
660         /*
661          * Check whether the style of the button which got the focus, changed to BS_DEFPUSHBUTTON and
662          * the style of button which lost the focus changed to BS_PUSHBUTTON.
663          */
664         if ( IDCANCEL == (LOWORD(dwVal)) )
665         {
666                 ok ( ((GetWindowLong( g_hwndTestDlgBut2, GWL_STYLE)) & BS_DEFPUSHBUTTON),
667                         "Button2 style not set to BS_DEFPUSHBUTTON\n" );
668
669                 ok ( !((GetWindowLong( g_hwndTestDlgBut1, GWL_STYLE)) & BS_DEFPUSHBUTTON),
670                         "Button1's style not changed to BS_PUSHBUTTON\n" );
671         }
672
673         /*
674          * Move focus to Edit control using "WM_NEXTDLGCTL"
675          */
676         DefDlgProcA( g_hwndTestDlg, WM_NEXTDLGCTL, 0, 0 );
677         ok ((GetFocus() == g_hwndTestDlgEdit), "Focus didn't move to Edit control\n");
678
679         /*
680          * Check whether the default button ID got changed by sending message "WM_NEXTDLGCTL"
681          */
682         dwVal = DefDlgProcA(g_hwndTestDlg, DM_GETDEFID, 0, 0);
683         ok ( IDCANCEL == (LOWORD(dwVal)), "WM_NEXTDLGCTL changed default button\n");
684     }
685
686     /* test nested default buttons */
687
688     child1 = CreateWindowA("button", "child1", WS_VISIBLE|WS_CHILD, 0, 0, 50, 50,
689                            g_hwndTestDlg, (HMENU)100, g_hinst, NULL);
690     ok(child1 != NULL, "failed to create first child\n");
691     child2 = CreateWindowA("button", "child2", WS_VISIBLE|WS_CHILD, 60, 60, 30, 30,
692                            g_hwndTestDlg, (HMENU)200, g_hinst, NULL);
693     ok(child2 != NULL, "failed to create second child\n");
694     /* create nested child */
695     child3 = CreateWindowA("button", "child3", WS_VISIBLE|WS_CHILD, 10, 10, 10, 10,
696                            child1, (HMENU)300, g_hinst, NULL);
697     ok(child3 != NULL, "failed to create subchild\n");
698
699     DefDlgProcA( g_hwndTestDlg, DM_SETDEFID, 200, 0);
700     dwVal = DefDlgProcA( g_hwndTestDlg, DM_GETDEFID, 0, 0);
701     ok(LOWORD(dwVal) == 200, "expected 200, got %x\n", dwVal);
702
703     DefDlgProcA( g_hwndTestDlg, DM_SETDEFID, 300, 0);
704     dwVal = DefDlgProcA( g_hwndTestDlg, DM_GETDEFID, 0, 0);
705     ok(LOWORD(dwVal) == 300, "expected 300, got %x\n", dwVal);
706     ok(SendMessageW( child3, WM_GETDLGCODE, 0, 0) != DLGC_DEFPUSHBUTTON,
707        "expected child3 not to be marked as DLGC_DEFPUSHBUTTON\n");
708
709     g_bReceivedCommand = FALSE;
710     FormEnterMsg (&msg, child3);
711     ok(IsDialogMessage (g_hwndTestDlg, &msg), "Did not handle the ENTER\n");
712     ok(g_bReceivedCommand, "Did not trigger the default Button action\n");
713
714     DestroyWindow(child3);
715     DestroyWindow(child2);
716     DestroyWindow(child1);
717     DestroyWindow(g_hwndTestDlg);
718 }
719
720 static void test_IsDialogMessage(void)
721 {
722     MSG msg;
723
724     g_hwndMain = CreateWindow ("IsDialogMessageWindowClass", "IsDialogMessageWindowClass",
725             WS_OVERLAPPEDWINDOW,
726             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
727             NULL, NULL, g_hinst, 0);
728
729     assert (g_hwndMain);
730     assert (g_hwndButton1);
731     assert (g_hwndButtonCancel);
732
733     /* The focus should initially be nowhere.  The first TAB should take it
734      * to the first button.  The second TAB should take it to the Cancel
735      * button.
736      */
737     FormTabMsg (&msg, g_hwndMain);
738     ok (IsDialogMessage (g_hwndMain, &msg), "Did not handle first TAB\n");
739     ok ((GetFocus() == g_hwndButton1), "Focus did not move to first button\n");
740     FormTabMsg (&msg, g_hwndButton1);
741     ok (IsDialogMessage (g_hwndMain, &msg), "Did not handle second TAB\n");
742     ok ((GetFocus() == g_hwndButtonCancel),
743             "Focus did not move to cancel button\n");
744     FormEnterMsg (&msg, g_hwndButtonCancel);
745     ok (IsDialogMessage (g_hwndMain, &msg), "Did not handle the ENTER\n");
746     ok (g_terminated, "ENTER did not terminate\n");
747 }
748
749
750 static INT_PTR CALLBACK delayFocusDlgWinProc (HWND hDlg, UINT uiMsg, WPARAM wParam,
751         LPARAM lParam)
752 {
753     switch (uiMsg)
754     {
755     case WM_INITDIALOG:
756         g_hwndMain = hDlg;
757        g_hwndInitialFocusGroupBox = GetDlgItem(hDlg,100);
758        g_hwndButton1 = GetDlgItem(hDlg,200);
759        g_hwndButton2 = GetDlgItem(hDlg,201);
760        g_hwndButtonCancel = GetDlgItem(hDlg,IDCANCEL);
761        g_styleInitialFocusT1 = GetWindowLong(g_hwndInitialFocusGroupBox, GWL_STYLE);
762
763        /* Initially check the second radio button */
764        SendMessage(g_hwndButton1, BM_SETCHECK, BST_UNCHECKED, 0);
765        SendMessage(g_hwndButton2, BM_SETCHECK, BST_CHECKED  , 0);
766        /* Continue testing after dialog initialization */
767        PostMessage(hDlg, WM_USER, 0, 0);
768        return g_bInitialFocusInitDlgResult;
769
770     case WM_COMMAND:
771         if (LOWORD(wParam) == IDCANCEL)
772        {
773            EndDialog(hDlg, LOWORD(wParam));
774            return TRUE;
775        }
776        return FALSE;
777
778     case WM_USER:
779        g_styleInitialFocusT2 = GetWindowLong(hDlg, GWL_STYLE);
780         g_hwndInitialFocusT1 = GetFocus();
781        SetFocus(hDlg);
782         g_hwndInitialFocusT2 = GetFocus();
783        PostMessage(hDlg, WM_COMMAND, IDCANCEL, 0);
784        return TRUE;
785     }
786
787     return FALSE;
788 }
789
790 static INT_PTR CALLBACK focusDlgWinProc (HWND hDlg, UINT uiMsg, WPARAM wParam,
791         LPARAM lParam)
792 {
793     switch (uiMsg)
794     {
795     case WM_INITDIALOG:
796        return TRUE;
797
798     case WM_COMMAND:
799        if (LOWORD(wParam) == 200)
800        {
801            if (HIWORD(wParam) == EN_SETFOCUS)
802                g_hwndInitialFocusT1 = (HWND)lParam;
803        }
804        return FALSE;
805     }
806
807     return FALSE;
808 }
809
810 /* Helper for InitialFocusTest */
811 static const char * GetHwndString(HWND hw)
812 {
813   if (hw == NULL)
814     return "a null handle";
815   if (hw == g_hwndMain)
816     return "the dialog handle";
817   if (hw == g_hwndInitialFocusGroupBox)
818     return "the group box control";
819   if (hw == g_hwndButton1)
820     return "the first button";
821   if (hw == g_hwndButton2)
822     return "the second button";
823   if (hw == g_hwndButtonCancel)
824     return "the cancel button";
825
826   return "unknown handle";
827 }
828
829 static void test_initial_focus(void)
830 {
831     /* Test 1:
832      * This test intentionally returns FALSE in response to WM_INITDIALOG
833      * without setting focus to a control. This is not allowed according to
834      * MSDN, but it is exactly what MFC's CFormView does.
835      *
836      * Since the WM_INITDIALOG handler returns FALSE without setting the focus,
837      * the focus should initially be NULL. Later, when we manually set focus to
838      * the dialog, the default handler should set focus to the first control that
839      * is "visible, not disabled, and has the WS_TABSTOP style" (MSDN). Because the
840      * second radio button has been checked, it should be the first control
841      * that meets these criteria and should receive the focus.
842      */
843
844     g_bInitialFocusInitDlgResult = FALSE;
845     g_hwndInitialFocusT1 = (HWND) -1;
846     g_hwndInitialFocusT2 = (HWND) -1;
847     g_styleInitialFocusT1 = -1;
848     g_styleInitialFocusT2 = -1;
849
850     DialogBoxA(g_hinst, "RADIO_TEST_DIALOG", NULL, delayFocusDlgWinProc);
851
852     ok (((g_styleInitialFocusT1 & WS_TABSTOP) == 0),
853        "Error in wrc - Detected WS_TABSTOP as default style for GROUPBOX\n");
854
855     ok (((g_styleInitialFocusT2 & WS_VISIBLE) == 0),
856        "Modal dialogs should not be shown until the message queue first goes empty\n");
857
858     ok ((g_hwndInitialFocusT1 == NULL),
859         "Error in initial focus when WM_INITDIALOG returned FALSE: "
860         "Expected NULL focus, got %s (%p).\n",
861         GetHwndString(g_hwndInitialFocusT1), g_hwndInitialFocusT1);
862
863     ok ((g_hwndInitialFocusT2 == g_hwndButton2),
864         "Error after first SetFocus() when WM_INITDIALOG returned FALSE: "
865         "Expected the second button (%p), got %s (%p).\n",
866         g_hwndButton2, GetHwndString(g_hwndInitialFocusT2),
867         g_hwndInitialFocusT2);
868
869     /* Test 2:
870      * This is the same as above, except WM_INITDIALOG is made to return TRUE.
871      * This should cause the focus to go to the second radio button right away
872      * and stay there (until the user indicates otherwise).
873      */
874
875     g_bInitialFocusInitDlgResult = TRUE;
876     g_hwndInitialFocusT1 = (HWND) -1;
877     g_hwndInitialFocusT2 = (HWND) -1;
878     g_styleInitialFocusT1 = -1;
879     g_styleInitialFocusT2 = -1;
880
881     DialogBoxA(g_hinst, "RADIO_TEST_DIALOG", NULL, delayFocusDlgWinProc);
882
883     ok ((g_hwndInitialFocusT1 == g_hwndButton2),
884        "Error in initial focus when WM_INITDIALOG returned TRUE: "
885        "Expected the second button (%p), got %s (%p).\n",
886        g_hwndButton2, GetHwndString(g_hwndInitialFocusT1),
887        g_hwndInitialFocusT1);
888
889     ok ((g_hwndInitialFocusT2 == g_hwndButton2),
890        "Error after first SetFocus() when WM_INITDIALOG returned TRUE: "
891        "Expected the second button (%p), got %s (%p).\n",
892        g_hwndButton2, GetHwndString(g_hwndInitialFocusT2),
893        g_hwndInitialFocusT2);
894
895     /* Test 3:
896      * If the dialog has DS_CONTROL and it's not visible then we shouldn't change focus */
897     {
898         HWND hDlg;
899         HRSRC hResource;
900         HANDLE hTemplate;
901         DLGTEMPLATE* pTemplate;
902
903         hResource = FindResourceA(g_hinst,"FOCUS_TEST_DIALOG", RT_DIALOG);
904         hTemplate = LoadResource(g_hinst, hResource);
905         pTemplate = LockResource(hTemplate);
906
907         g_hwndInitialFocusT1 = 0;
908         hDlg = CreateDialogIndirectParamA(g_hinst, pTemplate, NULL, focusDlgWinProc, 0);
909         ok (hDlg != 0, "Failed to create test dialog.\n");
910
911         ok ((g_hwndInitialFocusT1 == 0),
912             "Focus should not be set for an invisible DS_CONTROL dialog %p.\n", g_hwndInitialFocusT1);
913
914         DestroyWindow(hDlg);
915     }
916 }
917
918 static void test_GetDlgItemText(void)
919 {
920     char string[64];
921     BOOL ret;
922
923     strcpy(string, "Overwrite Me");
924     ret = GetDlgItemTextA(NULL, 0, string, sizeof(string)/sizeof(string[0]));
925     ok(!ret, "GetDlgItemText(NULL) shouldn't have succeeded\n");
926
927     ok(string[0] == '\0' || broken(!strcmp(string, "Overwrite Me")),
928        "string retrieved using GetDlgItemText should have been NULL terminated\n");
929 }
930
931 static void test_GetDlgItem(void)
932 {
933     HWND hwnd, child1, child2, hwnd2;
934     BOOL ret;
935
936     hwnd = CreateWindowA("button", "parent", WS_VISIBLE, 0, 0, 100, 100, NULL, 0, g_hinst, NULL);
937     ok(hwnd != NULL, "failed to created window\n");
938
939     /* created with the same ID */
940     child1 = CreateWindowA("button", "child1", WS_VISIBLE|WS_CHILD, 0, 0, 10, 10, hwnd, 0, g_hinst, NULL);
941     ok(child1 != NULL, "failed to create first child\n");
942     child2 = CreateWindowA("button", "child2", WS_VISIBLE|WS_CHILD, 0, 0, 10, 10, hwnd, 0, g_hinst, NULL);
943     ok(child2 != NULL, "failed to create second child\n");
944
945     hwnd2 = GetDlgItem(hwnd, 0);
946     ok(hwnd2 == child1, "expected first child, got %p\n", hwnd2);
947
948     hwnd2 = GetTopWindow(hwnd);
949     ok(hwnd2 == child1, "expected first child to be top, got %p\n", hwnd2);
950
951     ret = SetWindowPos(child1, child2, 0, 0, 0, 0, SWP_NOMOVE);
952     ok(ret, "got %d\n", ret);
953     hwnd2 = GetTopWindow(hwnd);
954     ok(hwnd2 == child2, "expected second child to be top, got %p\n", hwnd2);
955
956     /* top window from child list is picked */
957     hwnd2 = GetDlgItem(hwnd, 0);
958     ok(hwnd2 == child2, "expected second child, got %p\n", hwnd2);
959
960     /* Now test how GetDlgItem searches */
961     DestroyWindow(child2);
962     child2 = CreateWindowA("button", "child2", WS_VISIBLE|WS_CHILD, 0, 0, 10, 10, child1, 0, g_hinst, NULL);
963     ok(child2 != NULL, "failed to create second child\n");
964
965     /* give child2 an ID */
966     SetWindowLong(child2, GWLP_ID, 100);
967
968     hwnd2 = GetDlgItem(hwnd, 100);
969     ok(!hwnd2, "expected child to not be found, got %p\n", hwnd2);
970
971     /* make the ID of child2 public with a WS_EX_CONTROLPARENT parent */
972     SetWindowLong(child1, GWL_EXSTYLE, WS_EX_CONTROLPARENT);
973
974     hwnd2 = GetDlgItem(hwnd, 100);
975     ok(!hwnd2, "expected child to not be found, got %p\n", hwnd2);
976
977     DestroyWindow(child1);
978     DestroyWindow(child2);
979     DestroyWindow(hwnd);
980 }
981
982 static INT_PTR CALLBACK DestroyDlgWinProc (HWND hDlg, UINT uiMsg,
983         WPARAM wParam, LPARAM lParam)
984 {
985     if (uiMsg == WM_INITDIALOG)
986     {
987         DestroyWindow(hDlg);
988         return TRUE;
989     }
990     return FALSE;
991 }
992
993 static INT_PTR CALLBACK DestroyOnCloseDlgWinProc (HWND hDlg, UINT uiMsg,
994         WPARAM wParam, LPARAM lParam)
995 {
996     switch (uiMsg)
997     {
998     case WM_INITDIALOG:
999         PostMessage(hDlg, WM_CLOSE, 0, 0);
1000         return TRUE;
1001     case WM_CLOSE:
1002         DestroyWindow(hDlg);
1003         return TRUE;
1004     case WM_DESTROY:
1005         PostQuitMessage(0);
1006         return TRUE;
1007     }
1008     return FALSE;
1009 }
1010
1011 static INT_PTR CALLBACK TestInitDialogHandleProc (HWND hDlg, UINT uiMsg,
1012         WPARAM wParam, LPARAM lParam)
1013 {
1014     if (uiMsg == WM_INITDIALOG)
1015     {
1016         HWND expected = GetNextDlgTabItem(hDlg, NULL, FALSE);
1017         ok(expected == (HWND)wParam,
1018            "Expected wParam to be the handle to the first tabstop control (%p), got %p\n",
1019            expected, (HWND)wParam);
1020
1021         EndDialog(hDlg, LOWORD(SendMessage(hDlg, DM_GETDEFID, 0, 0)));
1022         return TRUE;
1023     }
1024     return FALSE;
1025 }
1026
1027 static INT_PTR CALLBACK TestDefButtonDlgProc (HWND hDlg, UINT uiMsg,
1028                                               WPARAM wParam, LPARAM lParam)
1029 {
1030     switch (uiMsg)
1031     {
1032     case WM_INITDIALOG:
1033         EndDialog(hDlg, LOWORD(SendMessage(hDlg, DM_GETDEFID, 0, 0)));
1034         return TRUE;
1035     }
1036     return FALSE;
1037 }
1038
1039 static INT_PTR CALLBACK TestReturnKeyDlgProc (HWND hDlg, UINT uiMsg,
1040         WPARAM wParam, LPARAM lParam)
1041 {
1042     static int received_idok = 0;
1043     switch (uiMsg)
1044     {
1045     case WM_INITDIALOG:
1046         {
1047             MSG msg = {hDlg, WM_KEYDOWN, VK_RETURN, 0x011c0001};
1048             IsDialogMessage(hDlg, &msg);
1049         }
1050         ok(received_idok, "WM_COMMAND not received\n");
1051         EndDialog(hDlg, 0);
1052         return TRUE;
1053     case WM_COMMAND:
1054         ok(wParam==IDOK, "Expected IDOK\n");
1055         received_idok = 1;
1056         return TRUE;
1057     }
1058     return FALSE;
1059 }
1060
1061 static void test_DialogBoxParamA(void)
1062 {
1063     INT_PTR ret;
1064     HWND hwnd_invalid = (HWND)0x4444;
1065
1066     SetLastError(0xdeadbeef);
1067     ret = DialogBoxParamA(GetModuleHandle(NULL), "IDD_DIALOG" , hwnd_invalid, 0 , 0);
1068     ok(0 == ret || broken(ret == -1), "DialogBoxParamA returned %ld, expected 0\n", ret);
1069     ok(ERROR_INVALID_WINDOW_HANDLE == GetLastError() ||
1070        broken(GetLastError() == 0xdeadbeef),
1071        "got %d, expected ERROR_INVALID_WINDOW_HANDLE\n",GetLastError());
1072
1073     /* Test a dialog which destroys itself on WM_INITDIALOG. */
1074     SetLastError(0xdeadbeef);
1075     ret = DialogBoxParamA(GetModuleHandle(NULL), "IDD_DIALOG", 0, DestroyDlgWinProc, 0);
1076     ok(-1 == ret, "DialogBoxParamA returned %ld, expected -1\n", ret);
1077     ok(ERROR_INVALID_WINDOW_HANDLE == GetLastError() ||
1078        GetLastError() == ERROR_SUCCESS ||
1079        broken(GetLastError() == 0xdeadbeef),
1080        "got %d, expected ERROR_INVALID_WINDOW_HANDLE\n",GetLastError());
1081
1082     /* Test a dialog which destroys itself on WM_CLOSE. */
1083     ret = DialogBoxParamA(GetModuleHandle(NULL), "IDD_DIALOG", 0, DestroyOnCloseDlgWinProc, 0);
1084     ok(0 == ret, "DialogBoxParamA returned %ld, expected 0\n", ret);
1085
1086     SetLastError(0xdeadbeef);
1087     ret = DialogBoxParamA(GetModuleHandle(NULL), "RESOURCE_INVALID" , 0, 0, 0);
1088     ok(-1 == ret, "DialogBoxParamA returned %ld, expected -1\n", ret);
1089     ok(ERROR_RESOURCE_NAME_NOT_FOUND == GetLastError() ||
1090        broken(GetLastError() == 0xdeadbeef),
1091        "got %d, expected ERROR_RESOURCE_NAME_NOT_FOUND\n",GetLastError());
1092
1093     SetLastError(0xdeadbeef);
1094     ret = DefDlgProcA(0, WM_ERASEBKGND, 0, 0);
1095     ok(ret == 0, "DefDlgProcA returned %ld, expected 0\n", ret);
1096     ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE ||
1097        broken(GetLastError() == 0xdeadbeef),
1098        "got %d, expected ERROR_INVALID_WINDOW_HANDLE\n", GetLastError());
1099
1100     ret = DialogBoxParamA(GetModuleHandle(NULL), "TEST_EMPTY_DIALOG", 0, TestInitDialogHandleProc, 0);
1101     ok(ret == IDOK, "Expected IDOK\n");
1102
1103     ret = DialogBoxParamA(GetModuleHandle(NULL), "TEST_EMPTY_DIALOG", 0, TestDefButtonDlgProc, 0);
1104     ok(ret == IDOK, "Expected IDOK\n");
1105
1106     DialogBoxParamA(GetModuleHandle(NULL), "TEST_EMPTY_DIALOG", 0, TestReturnKeyDlgProc, 0);
1107 }
1108
1109 static void test_DisabledDialogTest(void)
1110 {
1111     g_terminated = FALSE;
1112     DialogBoxParam(g_hinst, "IDD_DIALOG", NULL, (DLGPROC)disabled_test_proc, 0);
1113     ok(FALSE == g_terminated, "dialog with disabled ok button has been terminated\n");
1114 }
1115
1116 static INT_PTR CALLBACK messageBoxFontDlgWinProc (HWND hDlg, UINT uiMsg, WPARAM wParam,
1117         LPARAM lParam)
1118 {
1119     return (uiMsg == WM_INITDIALOG);
1120 }
1121
1122 static void test_MessageBoxFontTest(void)
1123 {
1124     /* This dialog template defines a dialog template which got 0x7fff as its
1125      * font size and omits the other font members. On WinNT, passing such a
1126      * dialog template to CreateDialogIndirectParamW will result in a dialog
1127      * being created which uses the message box font. We test that here.
1128      */
1129
1130     static unsigned char dlgTemplate[] =
1131     {
1132         /* Dialog header */
1133         0x01,0x00,              /* Version */
1134         0xff,0xff,              /* Extended template marker */
1135         0x00,0x00,0x00,0x00,    /* Context Help ID */
1136         0x00,0x00,0x00,0x00,    /* Extended style */
1137         0xc0,0x00,0xc8,0x80,    /* Style (WS_SYSMENU|WS_CAPTION|WS_POPUP|DS_SETFONT|DS_MODALFRAME) */
1138         0x01,0x00,              /* Control count */
1139         0x00,0x00,              /* X */
1140         0x00,0x00,              /* Y */
1141         0x80,0x00,              /* Width */
1142         0x80,0x00,              /* Height */
1143         0x00,0x00,              /* Menu name */
1144         0x00,0x00,              /* Class name */
1145         'T',0x00,'e',0x00,      /* Caption (unicode) */
1146         's',0x00,'t',0x00,
1147         0x00,0x00,
1148         0xff,0x7f,              /* Font height (0x7fff = message box font) */
1149
1150         /* Control #1 */
1151         0x00,0x00,              /* Align to DWORD (header is 42 bytes) */
1152         0x00,0x00,0x00,0x00,    /* Context Help ID */
1153         0x00,0x00,0x00,0x00,    /* Extended style */
1154         0x00,0x00,0x00,0x50,    /* Style (WS_CHILD|WS_VISIBLE) */
1155         0x00,0x00,              /* X */
1156         0x00,0x00,              /* Y */
1157         0x80,0x00,              /* Width */
1158         0x80,0x00,              /* Height */
1159         0x00,0x01,0x00,0x00,    /* Control ID (256) */
1160         0xff,0xff,0x82,0x00,    /* Class (Static) */
1161         'W',0x00,'I',0x00,      /* Caption (unicode) */
1162         'N',0x00,'E',0x00,
1163         ' ',0x00,'d',0x00,
1164         'i',0x00,'a',0x00,
1165         'l',0x00,'o',0x00,
1166         'g',0x00,' ',0x00,
1167         't',0x00,'e',0x00,
1168         's',0x00,'t',0x00,
1169         '.',0x00,0x00,0x00,
1170         0x00,0x00,              /* Size of extended data */
1171
1172         0x00,0x00               /* Align to DWORD */
1173     };
1174
1175     HWND hDlg;
1176     HFONT hFont;
1177     LOGFONTW lfStaticFont;
1178     NONCLIENTMETRICSW ncMetrics;
1179
1180     /* Check if the dialog can be created from the template. On Win9x, this should fail
1181      * because we are calling the W function which is not implemented, but that's what
1182      * we want, because passing such a template to CreateDialogIndirectParamA would crash
1183      * anyway.
1184      */
1185     hDlg = CreateDialogIndirectParamW(g_hinst, (LPCDLGTEMPLATE)dlgTemplate, NULL, messageBoxFontDlgWinProc, 0);
1186     if (!hDlg)
1187     {
1188         win_skip("dialog wasn't created\n");
1189         return;
1190     }
1191
1192     hFont = (HFONT) SendDlgItemMessageW(hDlg, 256, WM_GETFONT, 0, 0);
1193     if (!hFont)
1194     {
1195         skip("dialog uses system font\n");
1196         DestroyWindow(hDlg);
1197         return;
1198     }
1199     GetObjectW(hFont, sizeof(LOGFONTW), &lfStaticFont);
1200
1201     ncMetrics.cbSize = FIELD_OFFSET(NONCLIENTMETRICSW, iPaddedBorderWidth);
1202     SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, 0, &ncMetrics, 0);
1203     ok( !memcmp(&lfStaticFont, &ncMetrics.lfMessageFont, FIELD_OFFSET(LOGFONTW, lfFaceName)) &&
1204         !lstrcmpW(lfStaticFont.lfFaceName, ncMetrics.lfMessageFont.lfFaceName),
1205         "dialog doesn't use message box font\n");
1206     DestroyWindow(hDlg);
1207 }
1208
1209 static void test_SaveRestoreFocus(void)
1210 {
1211     HWND hDlg;
1212     HRSRC hResource;
1213     HANDLE hTemplate;
1214     DLGTEMPLATE* pTemplate;
1215     LONG_PTR foundId;
1216     HWND foundHwnd;
1217
1218     /* create the dialog */
1219     hResource = FindResourceA(g_hinst, "MULTI_EDIT_DIALOG", RT_DIALOG);
1220     hTemplate = LoadResource(g_hinst, hResource);
1221     pTemplate = LockResource(hTemplate);
1222
1223     hDlg = CreateDialogIndirectParamA(g_hinst, pTemplate, NULL, messageBoxFontDlgWinProc, 0);
1224     ok (hDlg != 0, "Failed to create test dialog.\n");
1225
1226     foundId = GetWindowLongPtr(GetFocus(), GWLP_ID);
1227     ok (foundId == 1000, "First edit box should have gained focus on dialog creation. Expected: %d, Found: %ld\n", 1000, foundId);
1228
1229     /* de- then reactivate the dialog */
1230     SendMessage(hDlg, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), 0);
1231     SendMessage(hDlg, WM_ACTIVATE, MAKEWPARAM(WA_ACTIVE, 0), 0);
1232
1233     foundId = GetWindowLongPtr(GetFocus(), GWLP_ID);
1234     ok (foundId == 1000, "First edit box should have regained focus after dialog reactivation. Expected: %d, Found: %ld\n", 1000, foundId);
1235
1236     /* select the next tabbable item */
1237     SetFocus(GetNextDlgTabItem(hDlg, GetFocus(), FALSE));
1238
1239     foundId = GetWindowLongPtr(GetFocus(), GWLP_ID);
1240     ok (foundId == 1001, "Second edit box should have gained focus. Expected: %d, Found: %ld\n", 1001, foundId);
1241
1242     /* de- then reactivate the dialog */
1243     SendMessage(hDlg, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), 0);
1244     SendMessage(hDlg, WM_ACTIVATE, MAKEWPARAM(WA_ACTIVE, 0), 0);
1245
1246     foundId = GetWindowLongPtr(GetFocus(), GWLP_ID);
1247     ok (foundId == 1001, "Second edit box should have gained focus after dialog reactivation. Expected: %d, Found: %ld\n", 1001, foundId);
1248
1249     /* disable the 2nd box */
1250     EnableWindow(GetFocus(), FALSE);
1251
1252     foundHwnd = GetFocus();
1253     ok (foundHwnd == NULL, "Second edit box should have lost focus after being disabled. Expected: %p, Found: %p\n", NULL, foundHwnd);
1254
1255     /* de- then reactivate the dialog */
1256     SendMessage(hDlg, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), 0);
1257     SendMessage(hDlg, WM_ACTIVATE, MAKEWPARAM(WA_ACTIVE, 0), 0);
1258
1259     foundHwnd = GetFocus();
1260     ok (foundHwnd == NULL, "No controls should have gained focus after dialog reactivation. Expected: %p, Found: %p\n", NULL, foundHwnd);
1261
1262     /* clean up */
1263     DestroyWindow(hDlg);
1264 }
1265
1266 static INT_PTR CALLBACK timer_message_dlg_proc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
1267 {
1268     static int count;
1269     BOOL visible;
1270
1271     switch (msg)
1272     {
1273         case WM_INITDIALOG:
1274             visible = GetWindowLong(wnd, GWL_STYLE) & WS_VISIBLE;
1275             ok(!visible, "Dialog should not be visible.\n");
1276             SetTimer(wnd, 1, 100, NULL);
1277             Sleep(200);
1278             return FALSE;
1279
1280         case WM_COMMAND:
1281             if (LOWORD(wparam) != IDCANCEL) return FALSE;
1282             EndDialog(wnd, LOWORD(wparam));
1283             return TRUE;
1284
1285         case WM_TIMER:
1286             if (wparam != 1) return FALSE;
1287             visible = GetWindowLong(wnd, GWL_STYLE) & WS_VISIBLE;
1288             if (!count++)
1289             {
1290                 ok(!visible, "Dialog should not be visible.\n");
1291                 PostMessage(wnd, WM_USER, 0, 0);
1292             }
1293             else
1294             {
1295                 ok(visible, "Dialog should be visible.\n");
1296                 PostMessage(wnd, WM_COMMAND, IDCANCEL, 0);
1297             }
1298             return TRUE;
1299
1300         case WM_USER:
1301             visible = GetWindowLong(wnd, GWL_STYLE) & WS_VISIBLE;
1302             ok(visible, "Dialog should be visible.\n");
1303             return TRUE;
1304
1305         default:
1306             return FALSE;
1307     }
1308 }
1309
1310 static void test_timer_message(void)
1311 {
1312     DialogBoxA(g_hinst, "RADIO_TEST_DIALOG", NULL, timer_message_dlg_proc);
1313 }
1314
1315 START_TEST(dialog)
1316 {
1317     g_hinst = GetModuleHandleA (0);
1318
1319     if (!RegisterWindowClasses()) assert(0);
1320
1321     test_GetNextDlgItem();
1322     test_IsDialogMessage();
1323     test_WM_NEXTDLGCTL();
1324     test_initial_focus();
1325     test_GetDlgItem();
1326     test_GetDlgItemText();
1327     test_DialogBoxParamA();
1328     test_DisabledDialogTest();
1329     test_MessageBoxFontTest();
1330     test_SaveRestoreFocus();
1331     test_timer_message();
1332 }