comdlg32/tests: Untie ok_testcases from test_ok().
[wine] / dlls / comdlg32 / tests / filedlg.c
1 /*
2  * Unit test suite for comdlg32 API functions: file dialogs
3  *
4  * Copyright 2007 Google (Lei Zhang)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  */
21
22 #include <windows.h>
23 #include <wine/test.h>
24
25 #include "initguid.h"
26 #include "shlguid.h"
27 #define COBJMACROS
28 #include "shobjidl.h"
29
30 /* ##### */
31
32 static int resizesupported = TRUE;
33
34 static void toolbarcheck( HWND hDlg)
35 {
36     /* test toolbar properties */
37     /* bug #10532 */
38     int maxtextrows;
39     HWND ctrl;
40     DWORD ret;
41     char classname[20];
42
43     for( ctrl = GetWindow( hDlg, GW_CHILD);
44             ctrl ; ctrl = GetWindow( ctrl, GW_HWNDNEXT)) {
45         GetClassName( ctrl, classname, 10);
46         classname[7] = '\0';
47         if( !strcmp( classname, "Toolbar")) break;
48     }
49     ok( ctrl != NULL, "could not get the toolbar control\n");
50     ret = SendMessage( ctrl, TB_ADDSTRING, 0, (LPARAM)"winetestwinetest\0\0");
51     ok( ret == 0, "addstring returned %d (expected 0)\n", ret);
52     maxtextrows = SendMessage( ctrl, TB_GETTEXTROWS, 0, 0);
53     ok( maxtextrows == 0 || broken(maxtextrows == 1),  /* Win2k and below */
54         "Get(Max)TextRows returned %d (expected 0)\n", maxtextrows);
55 }
56
57
58 static UINT_PTR CALLBACK OFNHookProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
59 {
60     LPNMHDR nmh;
61
62     if( msg == WM_NOTIFY)
63     {
64         nmh = (LPNMHDR) lParam;
65         if( nmh->code == CDN_INITDONE)
66         {
67             PostMessage( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
68         } else if (nmh->code == CDN_FOLDERCHANGE )
69         {
70             char buf[1024];
71             int ret;
72
73             memset(buf, 0x66, sizeof(buf));
74             ret = SendMessage( GetParent(hDlg), CDM_GETFOLDERIDLIST, 5, (LPARAM)buf);
75             ok(ret > 0, "CMD_GETFOLDERIDLIST not implemented\n");
76             if (ret > 5)
77                 ok(buf[0] == 0x66 && buf[1] == 0x66, "CMD_GETFOLDERIDLIST: The buffer was touched on failure\n");
78             toolbarcheck( GetParent(hDlg));
79         }
80     }
81
82     return 0;
83 }
84
85 /* bug 6829 */
86 static void test_DialogCancel(void)
87 {
88     OPENFILENAMEA ofn;
89     BOOL result;
90     char szFileName[MAX_PATH] = "";
91     char szInitialDir[MAX_PATH];
92
93     GetWindowsDirectory(szInitialDir, MAX_PATH);
94
95     ZeroMemory(&ofn, sizeof(ofn));
96
97     ofn.lStructSize = sizeof(ofn);
98     ofn.hwndOwner = NULL;
99     ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
100     ofn.lpstrFile = szFileName;
101     ofn.nMaxFile = MAX_PATH;
102     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
103     ofn.lpstrDefExt = "txt";
104     ofn.lpfnHook = OFNHookProc;
105     ofn.lpstrInitialDir = szInitialDir;
106
107     PrintDlgA(NULL);
108     ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
109        "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
110
111     result = GetOpenFileNameA(&ofn);
112     ok(0 == result, "expected 0, got %d\n", result);
113     ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
114        CommDlgExtendedError());
115
116     PrintDlgA(NULL);
117     ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
118        "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
119
120     result = GetSaveFileNameA(&ofn);
121     ok(0 == result, "expected 0, got %d\n", result);
122     ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
123        CommDlgExtendedError());
124
125     PrintDlgA(NULL);
126     ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
127        "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
128
129     /* Before passing the ofn to Unicode functions, remove the ANSI strings */
130     ofn.lpstrFilter = NULL;
131     ofn.lpstrInitialDir = NULL;
132     ofn.lpstrDefExt = NULL;
133
134     PrintDlgA(NULL);
135     ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
136        "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
137
138     SetLastError(0xdeadbeef);
139     result = GetOpenFileNameW((LPOPENFILENAMEW) &ofn);
140     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
141         win_skip("GetOpenFileNameW is not implemented\n");
142     else
143     {
144         ok(0 == result, "expected 0, got %d\n", result);
145         ok(0 == CommDlgExtendedError() ||
146            broken(CDERR_INITIALIZATION == CommDlgExtendedError()), /* win9x */
147            "expected 0, got %d\n", CommDlgExtendedError());
148     }
149
150     SetLastError(0xdeadbeef);
151     result = GetSaveFileNameW((LPOPENFILENAMEW) &ofn);
152     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
153         win_skip("GetSaveFileNameW is not implemented\n");
154     else
155     {
156         ok(0 == result, "expected 0, got %d\n", result);
157         ok(0 == CommDlgExtendedError() ||
158            broken(CDERR_INITIALIZATION == CommDlgExtendedError()), /* win9x */
159            "expected 0, got %d\n", CommDlgExtendedError());
160     }
161 }
162
163 static UINT_PTR CALLBACK create_view_window2_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
164 {
165     if (msg == WM_NOTIFY)
166     {
167         if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
168         {
169             IShellBrowser *shell_browser = (IShellBrowser *)SendMessage(GetParent(dlg), WM_USER + 7 /* WM_GETISHELLBROWSER */, 0, 0);
170             IShellView *shell_view = NULL;
171             IShellView2 *shell_view2 = NULL;
172             SV2CVW2_PARAMS view_params;
173             FOLDERSETTINGS folder_settings;
174             HRESULT hr;
175             RECT rect = {0, 0, 0, 0};
176
177             hr = IShellBrowser_QueryActiveShellView(shell_browser, &shell_view);
178             ok(SUCCEEDED(hr), "QueryActiveShellView returned %#x\n", hr);
179             if (FAILED(hr)) goto cleanup;
180
181             hr = IShellView_QueryInterface(shell_view, &IID_IShellView2, (void **)&shell_view2);
182             if (hr == E_NOINTERFACE)
183             {
184                 win_skip("IShellView2 not supported\n");
185                 goto cleanup;
186             }
187             ok(SUCCEEDED(hr), "QueryInterface returned %#x\n", hr);
188             if (FAILED(hr)) goto cleanup;
189
190             hr = IShellView2_DestroyViewWindow(shell_view2);
191             ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
192
193             folder_settings.ViewMode = FVM_LIST;
194             folder_settings.fFlags = 0;
195
196             view_params.cbSize = sizeof(view_params);
197             view_params.psvPrev = NULL;
198             view_params.pfs = &folder_settings;
199             view_params.psbOwner = shell_browser;
200             view_params.prcView = &rect;
201             view_params.pvid = NULL;
202             view_params.hwndView = NULL;
203
204             hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
205             if (hr == E_FAIL)
206             {
207                 win_skip("CreateViewWindow2 is broken on Vista/W2K8\n");
208                 goto cleanup;
209             }
210             ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
211             if (FAILED(hr)) goto cleanup;
212
213             hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
214             ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
215             ok(folder_settings.ViewMode == FVM_LIST,
216                "view mode is %d, expected FVM_LIST\n",
217                folder_settings.ViewMode);
218
219             hr = IShellView2_DestroyViewWindow(shell_view2);
220             ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
221
222             /* XP and W2K3 need this. On Win9x and W2K the call to DestroyWindow() fails and has
223              * no side effects. NT4 doesn't get here. (FIXME: Vista doesn't get here yet).
224              */
225             DestroyWindow(view_params.hwndView);
226
227             view_params.pvid = &VID_Details;
228             hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
229             ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
230             if (FAILED(hr)) goto cleanup;
231
232             hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
233             ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
234             ok(folder_settings.ViewMode == FVM_DETAILS ||
235                broken(folder_settings.ViewMode == FVM_LIST), /* Win9x */
236                "view mode is %d, expected FVM_DETAILS\n",
237                folder_settings.ViewMode);
238
239 cleanup:
240             if (shell_view2) IShellView2_Release(shell_view2);
241             if (shell_view) IShellView_Release(shell_view);
242             PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
243         }
244     }
245     return 0;
246 }
247
248 static LONG_PTR WINAPI template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
249 {
250     if (msg == WM_INITDIALOG)
251     {
252         HWND p,cb;
253         INT sel;
254         p = GetParent(dlg);
255         ok(p!=NULL, "Failed to get parent of template\n");
256         cb = GetDlgItem(p,0x470);
257         ok(cb!=NULL, "Failed to get filter combobox\n");
258         sel = SendMessage(cb, CB_GETCURSEL, 0, 0);
259         ok (sel != -1, "Failed to get selection from filter listbox\n");
260     }
261     if (msg == WM_NOTIFY)
262     {
263         if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
264             PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
265     }
266     return 0;
267 }
268
269 static void test_create_view_window2(void)
270 {
271     OPENFILENAMEA ofn = {0};
272     char filename[1024] = {0};
273     DWORD ret;
274
275     ofn.lStructSize = sizeof(ofn);
276     ofn.lpstrFile = filename;
277     ofn.nMaxFile = 1024;
278     ofn.lpfnHook = create_view_window2_hook;
279     ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
280     ret = GetOpenFileNameA(&ofn);
281     ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
282     ret = CommDlgExtendedError();
283     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
284 }
285
286 static void test_create_view_template(void)
287 {
288     OPENFILENAMEA ofn = {0};
289     char filename[1024] = {0};
290     DWORD ret;
291
292     ofn.lStructSize = sizeof(ofn);
293     ofn.lpstrFile = filename;
294     ofn.nMaxFile = 1024;
295     ofn.lpfnHook = (LPOFNHOOKPROC)template_hook;
296     ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE;
297     ofn.hInstance = GetModuleHandleA(NULL);
298     ofn.lpTemplateName = "template1";
299     ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
300     ret = GetOpenFileNameA(&ofn);
301     ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
302     ret = CommDlgExtendedError();
303     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
304 }
305
306 /* test cases for resizing of the file dialog */
307 struct {
308     DWORD flags;
309     int resize_folderchange;/* change in CDN_FOLDERCHANGE handler */
310     int resize_timer1;      /* change in first WM_TIMER handler */
311     int resize_check;       /* expected change (in second  WM_TIMER handler) */
312     BOOL todo;              /* mark that test todo_wine */
313     BOOL testcontrols;      /* test resizing and moving of the controls */
314 } resize_testcases[] = {
315     { 0                , 10, 10, 20,FALSE,FALSE},   /* 0 */
316     { 0                ,-10,-10,-20,FALSE,FALSE},
317     { OFN_ENABLESIZING ,  0,  0,  0,FALSE,FALSE},
318     { OFN_ENABLESIZING ,  0,-10,  0,FALSE,FALSE},
319     { OFN_ENABLESIZING ,  0, 10, 10,FALSE, TRUE},
320     { OFN_ENABLESIZING ,-10,  0, 10,FALSE,FALSE},   /* 5 */
321     { OFN_ENABLESIZING , 10,  0, 10,FALSE,FALSE},
322     { OFN_ENABLESIZING ,  0, 10, 20,FALSE,FALSE},
323     /* mark the end */
324     { 0xffffffff }
325 };
326
327 static LONG_PTR WINAPI resize_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
328 {
329     static RECT initrc, rc;
330     static int index, count;
331     static int gotSWP_bottom, gotShowWindow;
332     HWND parent = GetParent( dlg);
333     int resize;
334 #define MAXNRCTRLS 30
335     static RECT ctrlrcs[MAXNRCTRLS];
336     static int ctrlids[MAXNRCTRLS];
337     static HWND ctrls[MAXNRCTRLS];
338     static int nrctrls;
339
340     switch( msg)
341     {
342         case WM_INITDIALOG:
343         {
344             DWORD style;
345
346             index = ((OPENFILENAME*)lParam)->lCustData;
347             count = 0;
348             gotSWP_bottom = gotShowWindow = 0;
349             /* test style */
350             style = GetWindowLong( parent, GWL_STYLE);
351             if( resize_testcases[index].flags & OFN_ENABLESIZING)
352                 if( !(style & WS_SIZEBOX)) {
353                     win_skip( "OFN_ENABLESIZING flag not supported.\n");
354                     resizesupported = FALSE;
355                     PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
356                 } else
357                     ok( style & WS_SIZEBOX,
358                             "testid %d: dialog should have a WS_SIZEBOX style.\n", index);
359             else
360                 ok( !(style & WS_SIZEBOX),
361                         "testid %d: dialog should not have a WS_SIZEBOX style.\n", index);
362             break;
363         }
364         case WM_NOTIFY:
365         {
366             if(( (LPNMHDR)lParam)->code == CDN_FOLDERCHANGE){
367                 GetWindowRect( parent, &initrc);
368                 if( (resize  = resize_testcases[index].resize_folderchange)){
369                     MoveWindow( parent, initrc.left,initrc.top, initrc.right - initrc.left + resize,
370                             initrc.bottom - initrc.top + resize, TRUE);
371                 }
372                 SetTimer( dlg, 0, 100, 0);
373             }
374             break;
375         }
376         case WM_TIMER:
377         {
378             if( count == 0){
379                 /* store the control rectangles */
380                 if( resize_testcases[index].testcontrols) {
381                     HWND ctrl;
382                     int i;
383                     for( i = 0, ctrl = GetWindow( parent, GW_CHILD);
384                             i < MAXNRCTRLS && ctrl;
385                             i++, ctrl = GetWindow( ctrl, GW_HWNDNEXT)) {
386                         ctrlids[i] = GetDlgCtrlID( ctrl);
387                         GetWindowRect( ctrl, &ctrlrcs[i]);
388                         MapWindowPoints( NULL, parent, (LPPOINT) &ctrlrcs[i], 2);
389                         ctrls[i] = ctrl;
390                     }
391                     nrctrls = i;
392                 }
393                 if( (resize  = resize_testcases[index].resize_timer1)){
394                     GetWindowRect( parent, &rc);
395                     MoveWindow( parent, rc.left,rc.top, rc.right - rc.left + resize,
396                             rc.bottom - rc.top + resize, TRUE);
397                 }
398             } else if( count == 1){
399                 resize  = resize_testcases[index].resize_check;
400                 GetWindowRect( parent, &rc);
401                 if( resize_testcases[index].todo){
402                     todo_wine {
403                         ok( resize == rc.right - rc.left - initrc.right + initrc.left,
404                             "testid %d size-x change %d expected %d\n", index,
405                             rc.right - rc.left - initrc.right + initrc.left, resize);
406                         ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
407                             "testid %d size-y change %d expected %d\n", index,
408                             rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
409                     }
410                 }else{
411                     ok( resize == rc.right - rc.left - initrc.right + initrc.left,
412                         "testid %d size-x change %d expected %d\n", index,
413                         rc.right - rc.left - initrc.right + initrc.left, resize);
414                     ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
415                         "testid %d size-y change %d expected %d\n", index,
416                         rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
417                 }
418                 if( resize_testcases[index].testcontrols) {
419                     int i;
420                     RECT rc;
421                     for( i = 0; i < nrctrls; i++) {
422                         GetWindowRect( ctrls[i], &rc);
423                         MapWindowPoints( NULL, parent, (LPPOINT) &rc, 2);
424                         switch( ctrlids[i]){
425
426 /* test if RECT R1, moved and sized result in R2 */
427 #define TESTRECTS( R1, R2, Mx, My, Sx, Sy) \
428          ((R1).left + (Mx) ==(R2).left \
429         &&(R1).top + (My) ==(R2).top \
430         &&(R1).right + (Mx) + (Sx) == (R2).right \
431         &&(R1).bottom + (My) + (Sy) ==(R2).bottom)
432
433                             /* sized horizontal and moved vertical */
434                             case cmb1:
435                             case edt1:
436                                 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 10, 0) ||
437                                     broken(TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0)),/*win98*/
438                                     "control id %03x should have sized horizontally and moved vertically, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
439                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
440                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
441                                     rc.left, rc.top, rc.right, rc.bottom);
442                                 break;
443                             /* sized horizontal and vertical */
444                             case lst2:
445                                 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 10),
446                                     "control id %03x should have sized horizontally and vertically, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
447                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
448                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
449                                     rc.left, rc.top, rc.right, rc.bottom);
450                                 break;
451                             /* moved horizontal and vertical */
452                             case IDCANCEL:
453                             case pshHelp:
454                                 ok( TESTRECTS( ctrlrcs[i], rc, 10, 10, 0, 0) ||
455                                     broken(TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0)),/*win98*/
456                                     "control id %03x should have moved horizontally and vertically, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
457                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
458                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
459                                     rc.left, rc.top, rc.right, rc.bottom);
460                                 break;
461                             /* moved vertically */
462                             case chx1:
463                             case stc2:
464                             case stc3:
465                                 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0),
466                                     "control id %03x should have moved vertically, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
467                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
468                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
469                                     rc.left, rc.top, rc.right, rc.bottom);
470                                 break;
471                             /* resized horizontal */
472                             case cmb2: /* aka IDC_LOOKIN */
473                                 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 0)||
474                                         TESTRECTS( ctrlrcs[i], rc, 0, 0, 0, 0), /* Vista and higher */
475                                     "control id %03x should have resized horizontally, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
476                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
477                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
478                                     rc.left, rc.top, rc.right, rc.bottom);
479                                 break;
480                             /* non moving non sizing controls */
481                             case stc4:
482                                 ok( TESTRECTS( rc, ctrlrcs[i], 0, 0, 0, 0),
483                                     "control id %03x was moved/resized, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
484                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
485                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
486                                     rc.left, rc.top, rc.right, rc.bottom);
487                                 break;
488                             /* todo_wine: non moving non sizing controls */
489                             case lst1:
490 todo_wine
491                                 ok( TESTRECTS( rc, ctrlrcs[i], 0, 0, 0, 0),
492                                     "control id %03x was moved/resized, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
493                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
494                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
495                                     rc.left, rc.top, rc.right, rc.bottom);
496                                 break;
497                             /* don't test: id is not unique */
498                             case IDOK:
499                             case stc1:
500                             case 0:
501                             case  -1:
502                                 break;
503                             default:
504                                 trace("untested control id %03x before %d,%d-%d,%d after  %d,%d-%d,%d\n",
505                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
506                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
507                                     rc.left, rc.top, rc.right, rc.bottom);
508 #undef TESTRECTS
509 #undef MAXNRCTRLS
510                         }
511                     }
512                 }
513                 KillTimer( dlg, 0);
514                 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
515             }
516             count++;
517         }
518         break;
519         case WM_WINDOWPOSCHANGING:
520         {
521             WINDOWPOS *pwp = (WINDOWPOS *)lParam;
522             if(  !index && pwp->hwndInsertAfter == HWND_BOTTOM){
523                 gotSWP_bottom = 1;
524                 ok( gotShowWindow == 0, "The WM_WINDOWPOSCHANGING message came after a WM_SHOWWINDOW message\n");
525             }
526         }
527         break;
528         case WM_SHOWWINDOW:
529         {
530             if(  !index){
531                 gotShowWindow = 1;
532                 ok( gotSWP_bottom == 1, "No WM_WINDOWPOSCHANGING message came before a WM_SHOWWINDOW message\n");
533             }
534         }
535         break;
536     }
537     return 0;
538 }
539
540 static void test_resize(void)
541 {
542     OPENFILENAME ofn = { sizeof(OPENFILENAME)};
543     char filename[1024] = {0};
544     DWORD ret;
545     int i;
546
547     ofn.lpstrFile = filename;
548     ofn.nMaxFile = 1024;
549     ofn.lpfnHook = (LPOFNHOOKPROC) resize_template_hook;
550     ofn.hInstance = GetModuleHandle(NULL);
551     ofn.lpTemplateName = "template_sz";
552     for( i = 0; resize_testcases[i].flags != 0xffffffff; i++) {
553         ofn.lCustData = i;
554         ofn.Flags = resize_testcases[i].flags |
555             OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE | OFN_SHOWHELP ;
556         ret = GetOpenFileName(&ofn);
557         ok(!ret, "GetOpenFileName returned %#x\n", ret);
558         ret = CommDlgExtendedError();
559         ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
560     }
561 }
562
563 /* test cases for control message IDOK */
564 /* Show case for bug #19079 */
565 typedef struct {
566     int  retval;        /* return code of the message handler */
567     BOOL setmsgresult;  /* set the result in the DWLP_MSGRESULT */
568     BOOL usemsgokstr;   /* use the FILEOKSTRING message instead of WM_NOTIFY:CDN_FILEOK */
569     BOOL do_subclass;   /* subclass the dialog hook procedure */
570     BOOL expclose;      /* is the dialog expected to close ? */
571     BOOL actclose;      /* has the dialog actually closed ? */
572 } ok_wndproc_testcase;
573
574 static ok_wndproc_testcase ok_testcases[] = {
575     { 0,        FALSE,  FALSE,  FALSE,  TRUE},
576     { 0,         TRUE,  FALSE,  FALSE,  TRUE},
577     { 0,        FALSE,  FALSE,   TRUE,  TRUE},
578     { 0,         TRUE,  FALSE,   TRUE,  TRUE},
579     { 1,        FALSE,  FALSE,  FALSE,  TRUE},
580     { 1,         TRUE,  FALSE,  FALSE, FALSE},
581     { 1,        FALSE,  FALSE,   TRUE, FALSE},
582     { 1,         TRUE,  FALSE,   TRUE, FALSE},
583     /* FILEOKSTRING tests */
584     { 1,         TRUE,   TRUE,  FALSE, FALSE},
585     { 1,        FALSE,   TRUE,   TRUE, FALSE},
586     /* mark the end */
587     { -1 }
588 };
589
590 /* test_ok_wndproc can be used as hook procedure or a subclass
591  * window proc for the file dialog */
592 static LONG_PTR WINAPI test_ok_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
593 {
594     HWND parent = GetParent( dlg);
595     static ok_wndproc_testcase *testcase = NULL;
596     static UINT msgFILEOKSTRING;
597     if (msg == WM_INITDIALOG)
598     {
599         testcase = (ok_wndproc_testcase*)((OPENFILENAME*)lParam)->lCustData;
600         testcase->actclose = TRUE;
601         msgFILEOKSTRING = RegisterWindowMessageA( FILEOKSTRING);
602     }
603     if( msg == WM_NOTIFY) {
604         if(((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
605             SetTimer( dlg, 0, 100, 0);
606             PostMessage( parent, WM_COMMAND, IDOK, 0);
607             return FALSE;
608         } else if(((LPNMHDR)lParam)->code == CDN_FILEOK) {
609             if( testcase->usemsgokstr)
610                 return FALSE;
611             if( testcase->setmsgresult)
612                 SetWindowLongPtrA( dlg, DWLP_MSGRESULT, testcase->retval);
613             return testcase->retval;
614         }
615     }
616     if( msg == msgFILEOKSTRING) {
617         if( !testcase->usemsgokstr)
618             return FALSE;
619         if( testcase->setmsgresult)
620             SetWindowLongPtrA( dlg, DWLP_MSGRESULT, testcase->retval);
621         return testcase->retval;
622     }
623     if( msg == WM_TIMER) {
624         /* the dialog did not close automatically */
625         testcase->actclose = FALSE;
626         KillTimer( dlg, 0);
627         PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
628         return FALSE;
629     }
630     if( testcase && testcase->do_subclass)
631         return DefWindowProc( dlg, msg, wParam, lParam);
632     return FALSE;
633 }
634
635 static LONG_PTR WINAPI ok_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
636 {
637     if (msg == WM_SETFONT)
638         SetWindowLongPtrA( dlg, GWLP_WNDPROC, (LONG_PTR) test_ok_wndproc);
639     return FALSE;
640 }
641
642 static void test_ok(void)
643 {
644     OPENFILENAME ofn = { sizeof(OPENFILENAME)};
645     char filename[1024] = {0};
646     char tmpfilename[ MAX_PATH];
647     char curdir[MAX_PATH];
648     int i;
649     DWORD ret;
650
651     ok(GetCurrentDirectoryA(sizeof(curdir), curdir) != 0, "Failed to get current dir err %d\n", GetLastError());
652     if (!GetTempFileNameA(".", "txt", 0, tmpfilename)) {
653         skip("Failed to create a temporary file name\n");
654         return;
655     }
656     ofn.lpstrFile = filename;
657     ofn.nMaxFile = 1024;
658     ofn.hInstance = GetModuleHandle(NULL);
659     ofn.lpTemplateName = "template1";
660     ofn.Flags =  OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE ;
661     for( i = 0; ok_testcases[i].retval != -1; i++) {
662         strcpy( filename, tmpfilename);
663         ofn.lCustData = (LPARAM)(ok_testcases + i);
664         ofn.lpfnHook = ok_testcases[i].do_subclass
665             ? (LPOFNHOOKPROC) ok_template_hook
666             : (LPOFNHOOKPROC) test_ok_wndproc;
667         ret = GetOpenFileNameA(&ofn);
668         ok( ok_testcases[i].expclose == ok_testcases[i].actclose,
669                 "testid %d: Open File dialog should %shave closed.\n", i,
670                 ok_testcases[i].expclose ? "" : "NOT ");
671         ok(ret == ok_testcases[i].expclose, "testid %d: GetOpenFileName returned %#x\n", i, ret);
672         ret = CommDlgExtendedError();
673         ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
674         ok(SetCurrentDirectoryA(curdir), "Failed to restore current dir err %d\n", GetLastError());
675     }
676     ret =  DeleteFileA( tmpfilename);
677     ok( ret, "Failed to delete temporary file %s err %d\n", tmpfilename, GetLastError());
678 }
679
680 /* test arranging with a custom template */
681 typedef struct {
682     int x, y;  /* left, top coordinates */
683     int cx, cy; /* width and height */
684 } posz;
685 static struct {
686    int nrcontrols;    /* 0: no controls, 1: just the stc32 control 2: with button */
687    posz poszDlg;
688    posz poszStc32;
689    posz poszBtn;
690    DWORD ofnflags;
691 } arrange_tests[] = {
692     /* do not change the first two cases: used to get the uncustomized sizes */
693     { 0, {0},{0},{0},0 },
694     { 0, {0},{0},{0}, OFN_SHOWHELP},
695     /* two tests with just a subdialog, no controls */
696     { 0, {0, 0, 316, 76},{0},{0},0 },
697     { 0, {0, 0, 100, 76},{0},{0}, OFN_SHOWHELP},
698     /* now with a control with id stc32 */
699     { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0},0 }, /* bug #17748*/
700     { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0}, OFN_SHOWHELP}, /* bug #17748*/
701     /* tests with size of the stc32 control higher or wider then the standard dialog */
702     { 1, {0, 0, 316, 170} ,{0, 0, 204, 170,},{0},0 },
703     { 1, {0, 0, 316, 165} ,{0, 0, 411, 165,},{0}, OFN_SHOWHELP },
704     /* move the stc32 control around */
705     { 1, {0, 0, 300, 100} ,{73, 17, 50, 50,},{0},0 },
706     /* add control */
707     { 2, {0, 0, 280, 100} ,{0, 0, 50, 50,},{300,20,30,30},0 },
708     /* enable resizing should make the dialog bigger */
709     { 0, {0},{0},{0}, OFN_SHOWHELP|OFN_ENABLESIZING},
710     /* mark the end */
711     { -1 }
712 };
713
714 static LONG_PTR WINAPI template_hook_arrange(HWND dlgChild, UINT msg, WPARAM wParam, LPARAM lParam)
715 {
716     static int index, fixhelp;
717     static posz posz0[2];
718     static RECT clrcParent, clrcChild, rcStc32;
719     static HWND hwndStc32;
720     HWND dlgParent;
721
722     dlgParent = GetParent( dlgChild);
723     if (msg == WM_INITDIALOG) {
724         index = ((OPENFILENAME*)lParam)->lCustData;
725         /* get the positions before rearrangement */
726         GetClientRect( dlgParent, &clrcParent);
727         GetClientRect( dlgChild, &clrcChild);
728         hwndStc32 = GetDlgItem( dlgChild, stc32);
729         if( hwndStc32)  GetWindowRect( hwndStc32, &rcStc32);
730     }
731     if (msg == WM_NOTIFY && ((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
732         RECT wrcParent;
733
734         GetWindowRect( dlgParent, &wrcParent);
735         /* the fist two "tests" just save the dialogs position, with and without
736          * help button */
737         if( index == 0) {
738             posz0[0].x = wrcParent.left;
739             posz0[0].y = wrcParent.top;
740             posz0[0].cx = wrcParent.right - wrcParent.left;
741             posz0[0].cy = wrcParent.bottom - wrcParent.top;
742         } else if( index == 1) {
743             posz0[1].x = wrcParent.left;
744             posz0[1].y = wrcParent.top;
745             posz0[1].cx = wrcParent.right - wrcParent.left;
746             posz0[1].cy = wrcParent.bottom - wrcParent.top;
747             fixhelp = posz0[1].cy - posz0[0].cy;
748         } else {
749             /* the real tests */
750             int withhelp;
751             int expectx, expecty;
752             DWORD style;
753
754             withhelp = (arrange_tests[index].ofnflags & OFN_SHOWHELP) != 0;
755             GetWindowRect( dlgParent, &wrcParent);
756             if( !hwndStc32) {
757                 /* case with no custom subitem with stc32:
758                  * default to all custom controls below the standard */
759                 expecty = posz0[withhelp].cy + clrcChild.bottom;
760                 expectx =  posz0[withhelp].cx;
761             } else {
762                 /* special case: there is a control with id stc32 */
763                 /* expected height */
764                 expecty = posz0[withhelp].cy;
765                 if( rcStc32.bottom - rcStc32.top > clrcParent.bottom) {
766                     expecty +=  clrcChild.bottom -  clrcParent.bottom;
767                     if( !withhelp) expecty += fixhelp;
768                 }
769                 else
770                     expecty +=  clrcChild.bottom - ( rcStc32.bottom - rcStc32.top) ;
771                 /* expected width */
772                 expectx = posz0[withhelp].cx;
773                 if( rcStc32.right - rcStc32.left > clrcParent.right) {
774                     expectx +=  clrcChild.right -  clrcParent.right;
775                 }
776                 else
777                     expectx +=  clrcChild.right - ( rcStc32.right - rcStc32.left) ;
778             }
779             style = GetWindowLong( dlgParent, GWL_STYLE);
780             if( !(style & WS_SIZEBOX)) {
781                 /* without the OFN_ENABLESIZING flag */
782                 ok( wrcParent.bottom - wrcParent.top == expecty,
783                         "Wrong height of dialog %d, expected %d\n",
784                         wrcParent.bottom - wrcParent.top, expecty);
785                 ok( wrcParent.right - wrcParent.left == expectx,
786                         "Wrong width of dialog %d, expected %d\n",
787                         wrcParent.right - wrcParent.left, expectx);
788             } else todo_wine {
789                 /* with the OFN_ENABLESIZING flag */
790                 ok( wrcParent.bottom - wrcParent.top > expecty,
791                         "Wrong height of dialog %d, expected more than %d\n",
792                         wrcParent.bottom - wrcParent.top, expecty);
793                 ok( wrcParent.right - wrcParent.left > expectx,
794                         "Wrong width of dialog %d, expected more than %d\n",
795                         wrcParent.right - wrcParent.left, expectx);
796             }
797
798         }
799         PostMessage( dlgParent, WM_COMMAND, IDCANCEL, 0);
800     }
801     return 0;
802 }
803
804 static void test_arrange(void)
805 {
806     OPENFILENAMEA ofn = {0};
807     char filename[1024] = {0};
808     DWORD ret;
809     HRSRC hRes;
810     HANDLE hDlgTmpl;
811     LPBYTE pv;
812     DLGTEMPLATE *template;
813     DLGITEMTEMPLATE *itemtemplateStc32, *itemtemplateBtn;
814     int i;
815
816     /* load subdialog template into memory */
817     hRes = FindResource( GetModuleHandle(NULL), "template_stc32", (LPSTR)RT_DIALOG);
818     hDlgTmpl = LoadResource( GetModuleHandle(NULL), hRes );
819     /* get pointers to the structures for the dialog and the controls */
820     pv = LockResource( hDlgTmpl );
821     template = (DLGTEMPLATE*)pv;
822     if( template->x != 11111) {
823         win_skip("could not find the dialog template\n");
824         return;
825     }
826     /* skip dialog template, menu, class and title */
827     pv +=  sizeof(DLGTEMPLATE);
828     pv += 3 * sizeof(WORD);
829     /* skip font info */
830     while( *(WORD*)pv)
831         pv += sizeof(WORD);
832     pv += sizeof(WORD);
833     /* align on 32 bit boundaries */
834     pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
835     itemtemplateStc32 = (DLGITEMTEMPLATE*)pv;
836     if( itemtemplateStc32->x != 22222) {
837         win_skip("could not find the first item template\n");
838         return;
839     }
840     /* skip itemtemplate, class, title and creation data */
841     pv += sizeof(DLGITEMTEMPLATE);
842     pv +=  4 * sizeof(WORD);
843     /* align on 32 bit boundaries */
844     pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
845     itemtemplateBtn = (DLGITEMTEMPLATE*)pv;
846     if( itemtemplateBtn->x != 12345) {
847         win_skip("could not find the second item template\n");
848         return;
849     }
850
851     ofn.lStructSize = sizeof(ofn);
852     ofn.lpstrFile = filename;
853     ofn.nMaxFile = 1024;
854     ofn.lpfnHook = (LPOFNHOOKPROC)template_hook_arrange;
855     ofn.hInstance = hDlgTmpl;
856     ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
857     for( i = 0; arrange_tests[i].nrcontrols != -1; i++) {
858         ofn.lCustData = i;
859         ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATEHANDLE | OFN_HIDEREADONLY |
860             arrange_tests[i].ofnflags;
861         template->cdit = arrange_tests[i].nrcontrols;
862         template->x = arrange_tests[i].poszDlg.x;
863         template->y = arrange_tests[i].poszDlg.y;
864         template->cx = arrange_tests[i].poszDlg.cx;
865         template->cy = arrange_tests[i].poszDlg.cy;
866         itemtemplateStc32->x = arrange_tests[i].poszStc32.x;
867         itemtemplateStc32->y = arrange_tests[i].poszStc32.y;
868         itemtemplateStc32->cx = arrange_tests[i].poszStc32.cx;
869         itemtemplateStc32->cy = arrange_tests[i].poszStc32.cy;
870         itemtemplateBtn->x = arrange_tests[i].poszBtn.x;
871         itemtemplateBtn->y = arrange_tests[i].poszBtn.y;
872         itemtemplateBtn->cx = arrange_tests[i].poszBtn.cx;
873         itemtemplateBtn->cy = arrange_tests[i].poszBtn.cy;
874         ret = GetOpenFileNameA(&ofn);
875         ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
876         ret = CommDlgExtendedError();
877         ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
878     }
879 }
880
881 static CHAR SYSDIR[MAX_PATH];
882
883 static UINT_PTR CALLBACK path_hook_proc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
884 {
885     LPNMHDR nmh;
886
887     if( msg == WM_NOTIFY)
888     {
889         nmh = (LPNMHDR) lParam;
890         if( nmh->code == CDN_INITDONE)
891         {
892             PostMessage( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
893         }
894         else if ( nmh->code == CDN_FOLDERCHANGE)
895         {
896             char buf[1024];
897             int ret;
898
899             memset(buf, 0x66, sizeof(buf));
900             ret = SendMessageA( GetParent(hDlg), CDM_GETFOLDERPATH, sizeof(buf), (LPARAM)buf);
901             ok(!lstrcmpiA(SYSDIR, buf), "Expected '%s', got '%s'\n", SYSDIR, buf);
902             ok(lstrlenA(SYSDIR) + 1 == ret, "Expected %d, got %d\n", lstrlenA(SYSDIR) + 1, ret);
903         }
904     }
905
906     return 0;
907 }
908
909 static void test_getfolderpath(void)
910 {
911     OPENFILENAMEA ofn;
912     BOOL result;
913     char szFileName[MAX_PATH] = "";
914     char szInitialDir[MAX_PATH];
915
916     /* We need to pick a different directory as the other tests because of new
917      * Windows 7 behavior.
918      */
919     GetSystemDirectory(szInitialDir, MAX_PATH);
920     lstrcpyA(SYSDIR, szInitialDir);
921
922     ZeroMemory(&ofn, sizeof(ofn));
923
924     ofn.lStructSize = sizeof(ofn);
925     ofn.hwndOwner = NULL;
926     ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
927     ofn.lpstrFile = szFileName;
928     ofn.nMaxFile = MAX_PATH;
929     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
930     ofn.lpstrDefExt = "txt";
931     ofn.lpfnHook = path_hook_proc;
932     ofn.lpstrInitialDir = szInitialDir;
933
934     result = GetOpenFileNameA(&ofn);
935     ok(0 == result, "expected 0, got %d\n", result);
936     ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
937        CommDlgExtendedError());
938
939     result = GetSaveFileNameA(&ofn);
940     ok(0 == result, "expected 0, got %d\n", result);
941     ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
942        CommDlgExtendedError());
943 }
944
945 static void test_resizable2(void)
946 {
947     OPENFILENAMEA ofn = {0};
948     char filename[1024] = "pls press Enter if sizable, Esc otherwise";
949     DWORD ret;
950
951     /* interactive because there is no hook function */
952     if( !winetest_interactive) {
953         skip( "some interactive resizable dialog tests (set WINETEST_INTERACTIVE=1)\n");
954         return;
955     }
956     ofn.lStructSize = sizeof(ofn);
957     ofn.lpstrFile = filename;
958     ofn.nMaxFile = 1024;
959     ofn.lpfnHook = NULL;
960     ofn.hInstance = GetModuleHandleA(NULL);
961     ofn.lpTemplateName = "template1";
962     ofn.Flags = OFN_EXPLORER;
963 #define ISSIZABLE 1
964     ret = GetOpenFileNameA(&ofn);
965     ok( ret == ISSIZABLE, "File Dialog should have been sizable\n");
966     ret = CommDlgExtendedError();
967     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
968     ofn.Flags = OFN_EXPLORER | OFN_ENABLETEMPLATE;
969     ret = GetOpenFileNameA(&ofn);
970     ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
971     ret = CommDlgExtendedError();
972     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
973     ofn.Flags = OFN_EXPLORER | OFN_ENABLETEMPLATEHANDLE;
974     ofn.hInstance = LoadResource( GetModuleHandle(NULL), FindResource( GetModuleHandle(NULL), "template1", (LPSTR)RT_DIALOG));
975     ofn.lpTemplateName = NULL;
976     ret = GetOpenFileNameA(&ofn);
977     ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
978     ret = CommDlgExtendedError();
979     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
980     ofn.Flags = OFN_EXPLORER | OFN_ENABLEHOOK;
981     ret = GetOpenFileNameA(&ofn);
982     ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
983     ret = CommDlgExtendedError();
984     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
985 #undef ISSIZABLE
986 }
987
988 START_TEST(filedlg)
989 {
990     test_DialogCancel();
991     test_create_view_window2();
992     test_create_view_template();
993     test_arrange();
994     test_resize();
995     test_ok();
996     test_getfolderpath();
997     if( resizesupported) test_resizable2();
998 }