jscript: Make String_toUpperCase generic.
[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 UINT_PTR CALLBACK OFNHookProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
33 {
34     LPNMHDR nmh;
35
36     if( msg == WM_NOTIFY)
37     {
38         nmh = (LPNMHDR) lParam;
39         if( nmh->code == CDN_INITDONE)
40         {
41             PostMessage( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
42         } else if (nmh->code == CDN_FOLDERCHANGE )
43         {
44             char buf[1024];
45             int ret;
46
47             memset(buf, 0x66, sizeof(buf));
48             ret = SendMessage( GetParent(hDlg), CDM_GETFOLDERIDLIST, 5, (LPARAM)buf);
49             ok(ret > 0, "CMD_GETFOLDERIDLIST not implemented\n");
50             if (ret > 5)
51                 ok(buf[0] == 0x66 && buf[1] == 0x66, "CMD_GETFOLDERIDLIST: The buffer was touched on failure\n");
52         }
53     }
54
55     return 0;
56 }
57
58 /* bug 6829 */
59 static void test_DialogCancel(void)
60 {
61     OPENFILENAMEA ofn;
62     BOOL result;
63     char szFileName[MAX_PATH] = "";
64     char szInitialDir[MAX_PATH];
65
66     GetWindowsDirectory(szInitialDir, MAX_PATH);
67
68     ZeroMemory(&ofn, sizeof(ofn));
69
70     ofn.lStructSize = sizeof(ofn);
71     ofn.hwndOwner = NULL;
72     ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
73     ofn.lpstrFile = szFileName;
74     ofn.nMaxFile = MAX_PATH;
75     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
76     ofn.lpstrDefExt = "txt";
77     ofn.lpfnHook = OFNHookProc;
78     ofn.lpstrInitialDir = szInitialDir;
79
80     PrintDlgA(NULL);
81     ok(CDERR_INITIALIZATION == CommDlgExtendedError(), "expected %d, got %d\n",
82        CDERR_INITIALIZATION, CommDlgExtendedError());
83
84     result = GetOpenFileNameA(&ofn);
85     ok(0 == result, "expected %d, got %d\n", 0, result);
86     ok(0 == CommDlgExtendedError(), "expected %d, got %d\n", 0,
87        CommDlgExtendedError());
88
89     PrintDlgA(NULL);
90     ok(CDERR_INITIALIZATION == CommDlgExtendedError(), "expected %d, got %d\n",
91               CDERR_INITIALIZATION, CommDlgExtendedError());
92
93     result = GetSaveFileNameA(&ofn);
94     ok(0 == result, "expected %d, got %d\n", 0, result);
95     ok(0 == CommDlgExtendedError(), "expected %d, got %d\n", 0,
96        CommDlgExtendedError());
97
98     PrintDlgA(NULL);
99     ok(CDERR_INITIALIZATION == CommDlgExtendedError(), "expected %d, got %d\n",
100               CDERR_INITIALIZATION, CommDlgExtendedError());
101
102     /* Before passing the ofn to Unicode functions, remove the ANSI strings */
103     ofn.lpstrFilter = NULL;
104     ofn.lpstrInitialDir = NULL;
105     ofn.lpstrDefExt = NULL;
106
107     PrintDlgA(NULL);
108     ok(CDERR_INITIALIZATION == CommDlgExtendedError(), "expected %d, got %d\n",
109               CDERR_INITIALIZATION, CommDlgExtendedError());
110
111     SetLastError(0xdeadbeef);
112     result = GetOpenFileNameW((LPOPENFILENAMEW) &ofn);
113     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
114         win_skip("GetOpenFileNameW is not implemented\n");
115     else
116     {
117         ok(0 == result, "expected %d, got %d\n", 0, result);
118         ok(0 == CommDlgExtendedError() ||
119            CDERR_INITIALIZATION == CommDlgExtendedError(), /* win9x */
120            "expected %d or %d, got %d\n", 0, CDERR_INITIALIZATION,
121            CommDlgExtendedError());
122     }
123
124     SetLastError(0xdeadbeef);
125     result = GetSaveFileNameW((LPOPENFILENAMEW) &ofn);
126     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
127         win_skip("GetSaveFileNameW is not implemented\n");
128     else
129     {
130         ok(0 == result, "expected %d, got %d\n", 0, result);
131         ok(0 == CommDlgExtendedError() ||
132            CDERR_INITIALIZATION == CommDlgExtendedError(), /* win9x */
133            "expected %d or %d, got %d\n", 0, CDERR_INITIALIZATION,
134            CommDlgExtendedError());
135     }
136 }
137
138 static UINT_PTR CALLBACK create_view_window2_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
139 {
140     if (msg == WM_NOTIFY)
141     {
142         if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
143         {
144             IShellBrowser *shell_browser = (IShellBrowser *)SendMessage(GetParent(dlg), WM_USER + 7 /* WM_GETISHELLBROWSER */, 0, 0);
145             IShellView *shell_view = NULL;
146             IShellView2 *shell_view2 = NULL;
147             SV2CVW2_PARAMS view_params;
148             FOLDERSETTINGS folder_settings;
149             HRESULT hr;
150             RECT rect = {0, 0, 0, 0};
151
152             hr = IShellBrowser_QueryActiveShellView(shell_browser, &shell_view);
153             ok(SUCCEEDED(hr), "QueryActiveShellView returned %#x\n", hr);
154             if (FAILED(hr)) goto cleanup;
155
156             hr = IShellView_QueryInterface(shell_view, &IID_IShellView2, (void **)&shell_view2);
157             if (hr == E_NOINTERFACE)
158             {
159                 win_skip("IShellView2 not supported\n");
160                 goto cleanup;
161             }
162             ok(SUCCEEDED(hr), "QueryInterface returned %#x\n", hr);
163             if (FAILED(hr)) goto cleanup;
164
165             hr = IShellView2_DestroyViewWindow(shell_view2);
166             ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
167
168             folder_settings.ViewMode = FVM_LIST;
169             folder_settings.fFlags = 0;
170
171             view_params.cbSize = sizeof(view_params);
172             view_params.psvPrev = NULL;
173             view_params.pfs = &folder_settings;
174             view_params.psbOwner = shell_browser;
175             view_params.prcView = &rect;
176             view_params.pvid = NULL;
177             view_params.hwndView = NULL;
178
179             hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
180             ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
181             if (FAILED(hr)) goto cleanup;
182
183             hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
184             ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
185             ok(folder_settings.ViewMode == FVM_LIST, "view mode is %d, expected %d\n", folder_settings.ViewMode, FVM_LIST);
186
187             hr = IShellView2_DestroyViewWindow(shell_view2);
188             ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
189
190             /* XP and W2K3 need this. On Win9x and W2K the call to DestroyWindow() fails and has
191              * no side effects. NT4 doesn't get here. (FIXME: Vista doesn't get here yet).
192              */
193             DestroyWindow(view_params.hwndView);
194
195             view_params.pvid = &VID_Details;
196             hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
197             ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
198             if (FAILED(hr)) goto cleanup;
199
200             hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
201             ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
202             ok(folder_settings.ViewMode == FVM_DETAILS ||
203                broken(folder_settings.ViewMode == FVM_LIST), /* Win9x */
204                "view mode is %d, expected %d\n", folder_settings.ViewMode, FVM_DETAILS);
205
206 cleanup:
207             if (shell_view2) IShellView2_Release(shell_view2);
208             if (shell_view) IShellView_Release(shell_view);
209             PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
210         }
211     }
212     return 0;
213 }
214
215 static LONG_PTR WINAPI template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
216 {
217     if (msg == WM_INITDIALOG)
218     {
219         HWND p,cb;
220         INT sel;
221         p = GetParent(dlg);
222         ok(p!=NULL, "Failed to get parent of template\n");
223         cb = GetDlgItem(p,0x470);
224         ok(cb!=NULL, "Failed to get filter combobox\n");
225         sel = SendMessage(cb, CB_GETCURSEL, 0, 0);
226         ok (sel != -1, "Failed to get selection from filter listbox\n");
227     }
228     if (msg == WM_NOTIFY)
229     {
230         if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
231             PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
232     }
233     return 0;
234 }
235
236 static void test_create_view_window2(void)
237 {
238     OPENFILENAMEA ofn = {0};
239     char filename[1024] = {0};
240     DWORD ret;
241
242     ofn.lStructSize = sizeof(ofn);
243     ofn.lpstrFile = filename;
244     ofn.nMaxFile = 1024;
245     ofn.lpfnHook = create_view_window2_hook;
246     ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
247     ret = GetOpenFileNameA(&ofn);
248     ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
249     ret = CommDlgExtendedError();
250     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
251 }
252
253 static void test_create_view_template(void)
254 {
255     OPENFILENAMEA ofn = {0};
256     char filename[1024] = {0};
257     DWORD ret;
258
259     ofn.lStructSize = sizeof(ofn);
260     ofn.lpstrFile = filename;
261     ofn.nMaxFile = 1024;
262     ofn.lpfnHook = (LPOFNHOOKPROC)template_hook;
263     ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE;
264     ofn.hInstance = GetModuleHandleA(NULL);
265     ofn.lpTemplateName = "template1";
266     ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
267     ret = GetOpenFileNameA(&ofn);
268     ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
269     ret = CommDlgExtendedError();
270     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
271 }
272
273 /* test cases for resizing of the file dialog */
274 struct {
275     DWORD flags;
276     int resize_folderchange;/* change in CDN_FOLDERCHANGE handler */
277     int resize_timer1;      /* change in first WM_TIMER handler */
278     int resize_check;       /* expected change (in second  WM_TIMER handler) */
279     BOOL todo;              /* mark that test todo_wine */
280     BOOL testcontrols;      /* test resizing and moving of the controls */
281 } resize_testcases[] = {
282     { 0                , 10, 10, 20,FALSE,FALSE},   /* 0 */
283     { 0                ,-10,-10,-20,FALSE,FALSE},
284     { OFN_ENABLESIZING ,  0,  0,  0,FALSE,FALSE},
285     { OFN_ENABLESIZING ,  0,-10,  0,FALSE,FALSE},
286     { OFN_ENABLESIZING ,  0, 10, 10,FALSE, TRUE},
287     { OFN_ENABLESIZING ,-10,  0, 10,FALSE,FALSE},   /* 5 */
288     { OFN_ENABLESIZING , 10,  0, 10,FALSE,FALSE},
289     { OFN_ENABLESIZING ,  0, 10, 20,FALSE,FALSE},
290     /* mark the end */
291     { 0xffffffff }
292 };
293
294 static LONG_PTR WINAPI resize_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
295 {
296     static RECT initrc, rc;
297     static int index, count;
298     static int gotSWP_bottom, gotShowWindow;
299     HWND parent = GetParent( dlg);
300     int resize;
301 #define MAXNRCTRLS 30
302     static RECT ctrlrcs[MAXNRCTRLS];
303     static int ctrlids[MAXNRCTRLS];
304     static HWND ctrls[MAXNRCTRLS];
305     static int nrctrls;
306
307     switch( msg)
308     {
309         case WM_INITDIALOG:
310         {
311             DWORD style;
312
313             index = ((OPENFILENAME*)lParam)->lCustData;
314             count = 0;
315             gotSWP_bottom = gotShowWindow = 0;
316             /* test style */
317             style = GetWindowLong( parent, GWL_STYLE);
318             if( resize_testcases[index].flags & OFN_ENABLESIZING)
319                 if( !(style & WS_SIZEBOX)) {
320                     win_skip( "OFN_ENABLESIZING flag not supported.\n");
321                     PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
322                 } else
323                     ok( style & WS_SIZEBOX,
324                             "testid %d: dialog should have a WS_SIZEBOX style.\n", index);
325             else
326                 ok( !(style & WS_SIZEBOX),
327                         "testid %d: dialog should not have a WS_SIZEBOX style.\n", index);
328             break;
329         }
330         case WM_NOTIFY:
331         {
332             if(( (LPNMHDR)lParam)->code == CDN_FOLDERCHANGE){
333                 GetWindowRect( parent, &initrc);
334                 if( (resize  = resize_testcases[index].resize_folderchange)){
335                     MoveWindow( parent, initrc.left,initrc.top, initrc.right - initrc.left + resize,
336                             initrc.bottom - initrc.top + resize, TRUE);
337                 }
338                 SetTimer( dlg, 0, 100, 0);
339             }
340             break;
341         }
342         case WM_TIMER:
343         {
344             if( count == 0){
345                 /* store the control rectangles */
346                 if( resize_testcases[index].testcontrols) {
347                     HWND ctrl;
348                     int i;
349                     for( i = 0, ctrl = GetWindow( parent, GW_CHILD);
350                             i < MAXNRCTRLS && ctrl;
351                             i++, ctrl = GetWindow( ctrl, GW_HWNDNEXT)) {
352                         ctrlids[i] = GetDlgCtrlID( ctrl);
353                         GetWindowRect( ctrl, &ctrlrcs[i]);
354                         MapWindowPoints( NULL, parent, (LPPOINT) &ctrlrcs[i], 2);
355                         ctrls[i] = ctrl;
356                     }
357                     nrctrls = i;
358                 }
359                 if( (resize  = resize_testcases[index].resize_timer1)){
360                     GetWindowRect( parent, &rc);
361                     MoveWindow( parent, rc.left,rc.top, rc.right - rc.left + resize,
362                             rc.bottom - rc.top + resize, TRUE);
363                 }
364             } else if( count == 1){
365                 resize  = resize_testcases[index].resize_check;
366                 GetWindowRect( parent, &rc);
367                 if( resize_testcases[index].todo){
368                     todo_wine {
369                         ok( resize == rc.right - rc.left - initrc.right + initrc.left,
370                             "testid %d size-x change %d expected %d\n", index,
371                             rc.right - rc.left - initrc.right + initrc.left, resize);
372                         ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
373                             "testid %d size-y change %d expected %d\n", index,
374                             rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
375                     }
376                 }else{
377                     ok( resize == rc.right - rc.left - initrc.right + initrc.left,
378                         "testid %d size-x change %d expected %d\n", index,
379                         rc.right - rc.left - initrc.right + initrc.left, resize);
380                     ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
381                         "testid %d size-y change %d expected %d\n", index,
382                         rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
383                 }
384                 if( resize_testcases[index].testcontrols) {
385                     int i;
386                     RECT rc;
387                     for( i = 0; i < nrctrls; i++) {
388                         GetWindowRect( ctrls[i], &rc);
389                         MapWindowPoints( NULL, parent, (LPPOINT) &rc, 2);
390                         switch( ctrlids[i]){
391
392 /* test if RECT R1, moved and sized result in R2 */
393 #define TESTRECTS( R1, R2, Mx, My, Sx, Sy) \
394          ((R1).left + (Mx) ==(R2).left \
395         &&(R1).top + (My) ==(R2).top \
396         &&(R1).right + (Mx) + (Sx) == (R2).right \
397         &&(R1).bottom + (My) + (Sy) ==(R2).bottom)
398
399                             /* sized horizontal and moved vertical */
400                             case cmb1:
401                             case edt1:
402                                 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 10, 0) ||
403                                     broken(TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0)),/*win98*/
404                                     "control id %03x should have sized horizontally and moved vertically, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
405                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
406                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
407                                     rc.left, rc.top, rc.right, rc.bottom);
408                                 break;
409                             /* sized horizontal and vertical */
410                             case lst2:
411                                 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 10),
412                                     "control id %03x should have sized horizontally and vertically, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
413                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
414                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
415                                     rc.left, rc.top, rc.right, rc.bottom);
416                                 break;
417                             /* moved horizontal and vertical */
418                             case IDCANCEL:
419                             case pshHelp:
420                                 ok( TESTRECTS( ctrlrcs[i], rc, 10, 10, 0, 0) ||
421                                     broken(TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0)),/*win98*/
422                                     "control id %03x should have moved horizontally and vertically, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
423                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
424                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
425                                     rc.left, rc.top, rc.right, rc.bottom);
426                                 break;
427                             /* moved vertically */
428                             case chx1:
429                             case stc2:
430                             case stc3:
431                                 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0),
432                                     "control id %03x should have moved vertically, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
433                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
434                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
435                                     rc.left, rc.top, rc.right, rc.bottom);
436                                 break;
437                             /* resized horizontal */
438                             case cmb2: /* aka IDC_LOOKIN */
439                                 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 0)||
440                                         TESTRECTS( ctrlrcs[i], rc, 0, 0, 0, 0), /* Vista and higher */
441                                     "control id %03x should have resized horizontally, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
442                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
443                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
444                                     rc.left, rc.top, rc.right, rc.bottom);
445                                 break;
446                             /* non moving non sizing controls */
447                             case stc4:
448                                 ok( TESTRECTS( rc, ctrlrcs[i], 0, 0, 0, 0),
449                                     "control id %03x was moved/resized, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
450                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
451                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
452                                     rc.left, rc.top, rc.right, rc.bottom);
453                                 break;
454                             /* todo_wine: non moving non sizing controls */
455                             case lst1:
456 todo_wine
457                                 ok( TESTRECTS( rc, ctrlrcs[i], 0, 0, 0, 0),
458                                     "control id %03x was moved/resized, before %d,%d-%d,%d after  %d,%d-%d,%d\n",
459                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
460                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
461                                     rc.left, rc.top, rc.right, rc.bottom);
462                                 break;
463                             /* don't test: id is not unique */
464                             case IDOK:
465                             case stc1:
466                             case 0:
467                             case  -1:
468                                 break;
469                             default:
470                                 trace("untested control id %03x before %d,%d-%d,%d after  %d,%d-%d,%d\n",
471                                     ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
472                                     ctrlrcs[i].right, ctrlrcs[i].bottom,
473                                     rc.left, rc.top, rc.right, rc.bottom);
474 #undef TESTRECTS
475 #undef MAXNRCTRLS
476                         }
477                     }
478                 }
479                 KillTimer( dlg, 0);
480                 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
481             }
482             count++;
483         }
484         break;
485         case WM_WINDOWPOSCHANGING:
486         {
487             WINDOWPOS *pwp = (WINDOWPOS *)lParam;
488             if(  !index && pwp->hwndInsertAfter == HWND_BOTTOM){
489                 gotSWP_bottom = 1;
490                 ok( gotShowWindow == 0, "The WM_WINDOWPOSCHANGING message came after a WM_SHOWWINDOW message\n");
491             }
492         }
493         break;
494         case WM_SHOWWINDOW:
495         {
496             if(  !index){
497                 gotShowWindow = 1;
498                 ok( gotSWP_bottom == 1, "No WM_WINDOWPOSCHANGING message came before a WM_SHOWWINDOW message\n");
499             }
500         }
501         break;
502     }
503     return 0;
504 }
505
506 static void test_resize(void)
507 {
508     OPENFILENAME ofn = { sizeof(OPENFILENAME)};
509     char filename[1024] = {0};
510     DWORD ret;
511     int i;
512
513     ofn.lpstrFile = filename;
514     ofn.nMaxFile = 1024;
515     ofn.lpfnHook = (LPOFNHOOKPROC) resize_template_hook;
516     ofn.hInstance = GetModuleHandle(NULL);
517     ofn.lpTemplateName = "template_sz";
518     for( i = 0; resize_testcases[i].flags != 0xffffffff; i++) {
519         ofn.lCustData = i;
520         ofn.Flags = resize_testcases[i].flags |
521             OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE | OFN_SHOWHELP ;
522         ret = GetOpenFileName(&ofn);
523         ok(!ret, "GetOpenFileName returned %#x\n", ret);
524         ret = CommDlgExtendedError();
525         ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
526     }
527 }
528
529 /* test cases for control message IDOK */
530 /* Show case for bug #19079 */
531 static struct {
532     int  retval;        /* return code of the message handler */
533     BOOL setmsgresult;  /* set the result in the DWLP_MSGRESULT */
534     BOOL usemsgokstr;   /* use the FILEOKSTRING message instead of WM_NOTIFY:CDN_FILEOK */
535     BOOL do_subclass;   /* subclass the dialog hook procedure */
536     BOOL expclose;      /* is the dialog expected to close ? */
537     BOOL actclose;      /* has the dialog actually closed ? */
538 } ok_testcases[] = {
539     { 0,        FALSE,  FALSE,  FALSE,  TRUE},
540     { 0,         TRUE,  FALSE,  FALSE,  TRUE},
541     { 0,        FALSE,  FALSE,   TRUE,  TRUE},
542     { 0,         TRUE,  FALSE,   TRUE,  TRUE},
543     { 1,        FALSE,  FALSE,  FALSE,  TRUE},
544     { 1,         TRUE,  FALSE,  FALSE, FALSE},
545     { 1,        FALSE,  FALSE,   TRUE, FALSE},
546     { 1,         TRUE,  FALSE,   TRUE, FALSE},
547     /* FILEOKSTRING tests */
548     { 1,         TRUE,   TRUE,  FALSE, FALSE},
549     { 1,        FALSE,   TRUE,   TRUE, FALSE},
550     /* mark the end */
551     { -1 }
552 };
553
554 /* test_ok_wndproc can be used as hook procedure or a subclass
555  * window proc for the file dialog */
556 static LONG_PTR WINAPI test_ok_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
557 {
558     HWND parent = GetParent( dlg);
559     static int index;
560     static UINT msgFILEOKSTRING;
561     if (msg == WM_INITDIALOG)
562     {
563         index = ((OPENFILENAME*)lParam)->lCustData;
564         ok_testcases[index].actclose = TRUE;
565         msgFILEOKSTRING = RegisterWindowMessageA( FILEOKSTRING);
566     }
567     if( msg == WM_NOTIFY) {
568         if(((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
569             SetTimer( dlg, 0, 100, 0);
570             PostMessage( parent, WM_COMMAND, IDOK, 0);
571             return FALSE;
572         } else if(((LPNMHDR)lParam)->code == CDN_FILEOK) {
573             if( ok_testcases[index].usemsgokstr)
574                 return FALSE;
575             if( ok_testcases[index].setmsgresult)
576                 SetWindowLongPtrA( dlg, DWLP_MSGRESULT, ok_testcases[index].retval);
577             return ok_testcases[index].retval;
578         }
579     }
580     if( msg == msgFILEOKSTRING) {
581         if( !ok_testcases[index].usemsgokstr)
582             return FALSE;
583         if( ok_testcases[index].setmsgresult)
584             SetWindowLongPtrA( dlg, DWLP_MSGRESULT, ok_testcases[index].retval);
585         return ok_testcases[index].retval;
586     }
587     if( msg == WM_TIMER) {
588         /* the dialog did not close automatically */
589         ok_testcases[index].actclose = FALSE;
590         KillTimer( dlg, 0);
591         PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
592         return FALSE;
593     }
594     if( ok_testcases[index].do_subclass)
595         return DefWindowProc( dlg, msg, wParam, lParam);
596     return FALSE;
597 }
598
599 static LONG_PTR WINAPI ok_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
600 {
601     if (msg == WM_SETFONT)
602         SetWindowLongPtrA( dlg, GWLP_WNDPROC, (LONG_PTR) test_ok_wndproc);
603     return FALSE;
604 }
605
606 static void test_ok(void)
607 {
608     OPENFILENAME ofn = { sizeof(OPENFILENAME)};
609     char filename[1024] = {0};
610     char tmpfilename[ MAX_PATH];
611     int i;
612     DWORD ret;
613
614     if (!GetTempFileNameA(".", "txt", 0, tmpfilename)) {
615         skip("Failed to create a temporary file name\n");
616         return;
617     }
618     ofn.lpstrFile = filename;
619     ofn.nMaxFile = 1024;
620     ofn.hInstance = GetModuleHandle(NULL);
621     ofn.lpTemplateName = "template1";
622     ofn.Flags =  OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE ;
623     for( i = 0; ok_testcases[i].retval != -1; i++) {
624         strcpy( filename, tmpfilename);
625         ofn.lCustData = i;
626         ofn.lpfnHook = ok_testcases[i].do_subclass
627             ? (LPOFNHOOKPROC) ok_template_hook
628             : (LPOFNHOOKPROC) test_ok_wndproc;
629         ret = GetOpenFileNameA(&ofn);
630         ok( ok_testcases[i].expclose == ok_testcases[i].actclose,
631                 "testid %d: Open File dialog should %shave closed.\n", i,
632                 ok_testcases[i].expclose ? "" : "NOT ");
633         ok(ret == ok_testcases[i].expclose, "testid %d: GetOpenFileName returned %#x\n", i, ret);
634         ret = CommDlgExtendedError();
635         ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
636     }
637     ret =  DeleteFileA( tmpfilename);
638     ok( ret, "Failed to delete temporary file %s err %d\n", tmpfilename, GetLastError());
639 }
640
641 START_TEST(filedlg)
642 {
643     test_DialogCancel();
644     test_create_view_window2();
645     test_create_view_template();
646     test_resize();
647     test_ok();
648 }