comdlg32: Filedlg should not add extension if it contains a glob.
[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 static const 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     BOOL cdret;
651
652     cdret = GetCurrentDirectoryA(sizeof(curdir), curdir);
653     ok(cdret, "Failed to get current dir err %d\n", GetLastError());
654     if (!GetTempFileNameA(".", "txt", 0, tmpfilename)) {
655         skip("Failed to create a temporary file name\n");
656         return;
657     }
658     ofn.lpstrFile = filename;
659     ofn.nMaxFile = 1024;
660     ofn.hInstance = GetModuleHandle(NULL);
661     ofn.lpTemplateName = "template1";
662     ofn.Flags =  OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE ;
663     for( i = 0; ok_testcases[i].retval != -1; i++) {
664         strcpy( filename, tmpfilename);
665         ofn.lCustData = (LPARAM)(ok_testcases + i);
666         ofn.lpfnHook = ok_testcases[i].do_subclass
667             ? (LPOFNHOOKPROC) ok_template_hook
668             : (LPOFNHOOKPROC) test_ok_wndproc;
669         ret = GetOpenFileNameA(&ofn);
670         ok( ok_testcases[i].expclose == ok_testcases[i].actclose,
671                 "testid %d: Open File dialog should %shave closed.\n", i,
672                 ok_testcases[i].expclose ? "" : "NOT ");
673         ok(ret == ok_testcases[i].expclose, "testid %d: GetOpenFileName returned %#x\n", i, ret);
674         ret = CommDlgExtendedError();
675         ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
676         cdret = SetCurrentDirectoryA(curdir);
677         ok(cdret, "Failed to restore current dir err %d\n", GetLastError());
678     }
679     ret =  DeleteFileA( tmpfilename);
680     ok( ret, "Failed to delete temporary file %s err %d\n", tmpfilename, GetLastError());
681 }
682
683 /* test arranging with a custom template */
684 typedef struct {
685     int x, y;  /* left, top coordinates */
686     int cx, cy; /* width and height */
687 } posz;
688 static struct {
689    int nrcontrols;    /* 0: no controls, 1: just the stc32 control 2: with button */
690    posz poszDlg;
691    posz poszStc32;
692    posz poszBtn;
693    DWORD ofnflags;
694 } arrange_tests[] = {
695     /* do not change the first two cases: used to get the uncustomized sizes */
696     { 0, {0},{0},{0},0 },
697     { 0, {0},{0},{0}, OFN_SHOWHELP},
698     /* two tests with just a subdialog, no controls */
699     { 0, {0, 0, 316, 76},{0},{0},0 },
700     { 0, {0, 0, 100, 76},{0},{0}, OFN_SHOWHELP},
701     /* now with a control with id stc32 */
702     { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0},0 }, /* bug #17748*/
703     { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0}, OFN_SHOWHELP}, /* bug #17748*/
704     /* tests with size of the stc32 control higher or wider then the standard dialog */
705     { 1, {0, 0, 316, 170} ,{0, 0, 204, 170,},{0},0 },
706     { 1, {0, 0, 316, 165} ,{0, 0, 411, 165,},{0}, OFN_SHOWHELP },
707     /* move the stc32 control around */
708     { 1, {0, 0, 300, 100} ,{73, 17, 50, 50,},{0},0 },
709     /* add control */
710     { 2, {0, 0, 280, 100} ,{0, 0, 50, 50,},{300,20,30,30},0 },
711     /* enable resizing should make the dialog bigger */
712     { 0, {0},{0},{0}, OFN_SHOWHELP|OFN_ENABLESIZING},
713     /* mark the end */
714     { -1 }
715 };
716
717 static LONG_PTR WINAPI template_hook_arrange(HWND dlgChild, UINT msg, WPARAM wParam, LPARAM lParam)
718 {
719     static int index, fixhelp;
720     static posz posz0[2];
721     static RECT clrcParent, clrcChild, rcStc32;
722     static HWND hwndStc32;
723     HWND dlgParent;
724
725     dlgParent = GetParent( dlgChild);
726     if (msg == WM_INITDIALOG) {
727         index = ((OPENFILENAME*)lParam)->lCustData;
728         /* get the positions before rearrangement */
729         GetClientRect( dlgParent, &clrcParent);
730         GetClientRect( dlgChild, &clrcChild);
731         hwndStc32 = GetDlgItem( dlgChild, stc32);
732         if( hwndStc32)  GetWindowRect( hwndStc32, &rcStc32);
733     }
734     if (msg == WM_NOTIFY && ((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
735         RECT wrcParent;
736
737         GetWindowRect( dlgParent, &wrcParent);
738         /* the fist two "tests" just save the dialogs position, with and without
739          * help button */
740         if( index == 0) {
741             posz0[0].x = wrcParent.left;
742             posz0[0].y = wrcParent.top;
743             posz0[0].cx = wrcParent.right - wrcParent.left;
744             posz0[0].cy = wrcParent.bottom - wrcParent.top;
745         } else if( index == 1) {
746             posz0[1].x = wrcParent.left;
747             posz0[1].y = wrcParent.top;
748             posz0[1].cx = wrcParent.right - wrcParent.left;
749             posz0[1].cy = wrcParent.bottom - wrcParent.top;
750             fixhelp = posz0[1].cy - posz0[0].cy;
751         } else {
752             /* the real tests */
753             int withhelp;
754             int expectx, expecty;
755             DWORD style;
756
757             withhelp = (arrange_tests[index].ofnflags & OFN_SHOWHELP) != 0;
758             GetWindowRect( dlgParent, &wrcParent);
759             if( !hwndStc32) {
760                 /* case with no custom subitem with stc32:
761                  * default to all custom controls below the standard */
762                 expecty = posz0[withhelp].cy + clrcChild.bottom;
763                 expectx =  posz0[withhelp].cx;
764             } else {
765                 /* special case: there is a control with id stc32 */
766                 /* expected height */
767                 expecty = posz0[withhelp].cy;
768                 if( rcStc32.bottom - rcStc32.top + (withhelp ? 0 : fixhelp) > clrcParent.bottom) {
769                     expecty +=  clrcChild.bottom -  clrcParent.bottom;
770                     if( !withhelp) expecty += fixhelp;
771                 }
772                 else
773                     expecty +=  clrcChild.bottom - ( rcStc32.bottom - rcStc32.top) ;
774                 /* expected width */
775                 expectx = posz0[withhelp].cx;
776                 if( rcStc32.right - rcStc32.left > clrcParent.right) {
777                     expectx +=  clrcChild.right -  clrcParent.right;
778                 }
779                 else
780                     expectx +=  clrcChild.right - ( rcStc32.right - rcStc32.left) ;
781             }
782             style = GetWindowLong( dlgParent, GWL_STYLE);
783             if( !(style & WS_SIZEBOX)) {
784                 /* without the OFN_ENABLESIZING flag */
785                 ok( wrcParent.bottom - wrcParent.top == expecty,
786                         "Wrong height of dialog %d, expected %d\n",
787                         wrcParent.bottom - wrcParent.top, expecty);
788                 ok( wrcParent.right - wrcParent.left == expectx,
789                         "Wrong width of dialog %d, expected %d\n",
790                         wrcParent.right - wrcParent.left, expectx);
791             } else todo_wine {
792                 /* with the OFN_ENABLESIZING flag */
793                 ok( wrcParent.bottom - wrcParent.top > expecty,
794                         "Wrong height of dialog %d, expected more than %d\n",
795                         wrcParent.bottom - wrcParent.top, expecty);
796                 ok( wrcParent.right - wrcParent.left > expectx,
797                         "Wrong width of dialog %d, expected more than %d\n",
798                         wrcParent.right - wrcParent.left, expectx);
799             }
800
801         }
802         PostMessage( dlgParent, WM_COMMAND, IDCANCEL, 0);
803     }
804     return 0;
805 }
806
807 static void test_arrange(void)
808 {
809     OPENFILENAMEA ofn = {0};
810     char filename[1024] = {0};
811     DWORD ret;
812     HRSRC hRes;
813     HANDLE hDlgTmpl;
814     LPBYTE pv;
815     DLGTEMPLATE *template;
816     DLGITEMTEMPLATE *itemtemplateStc32, *itemtemplateBtn;
817     int i;
818
819     /* load subdialog template into memory */
820     hRes = FindResource( GetModuleHandle(NULL), "template_stc32", (LPSTR)RT_DIALOG);
821     hDlgTmpl = LoadResource( GetModuleHandle(NULL), hRes );
822     /* get pointers to the structures for the dialog and the controls */
823     pv = LockResource( hDlgTmpl );
824     template = (DLGTEMPLATE*)pv;
825     if( template->x != 11111) {
826         win_skip("could not find the dialog template\n");
827         return;
828     }
829     /* skip dialog template, menu, class and title */
830     pv +=  sizeof(DLGTEMPLATE);
831     pv += 3 * sizeof(WORD);
832     /* skip font info */
833     while( *(WORD*)pv)
834         pv += sizeof(WORD);
835     pv += sizeof(WORD);
836     /* align on 32 bit boundaries */
837     pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
838     itemtemplateStc32 = (DLGITEMTEMPLATE*)pv;
839     if( itemtemplateStc32->x != 22222) {
840         win_skip("could not find the first item template\n");
841         return;
842     }
843     /* skip itemtemplate, class, title and creation data */
844     pv += sizeof(DLGITEMTEMPLATE);
845     pv +=  4 * sizeof(WORD);
846     /* align on 32 bit boundaries */
847     pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
848     itemtemplateBtn = (DLGITEMTEMPLATE*)pv;
849     if( itemtemplateBtn->x != 12345) {
850         win_skip("could not find the second item template\n");
851         return;
852     }
853
854     ofn.lStructSize = sizeof(ofn);
855     ofn.lpstrFile = filename;
856     ofn.nMaxFile = 1024;
857     ofn.lpfnHook = (LPOFNHOOKPROC)template_hook_arrange;
858     ofn.hInstance = hDlgTmpl;
859     ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
860     for( i = 0; arrange_tests[i].nrcontrols != -1; i++) {
861         ofn.lCustData = i;
862         ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATEHANDLE | OFN_HIDEREADONLY |
863             arrange_tests[i].ofnflags;
864         template->cdit = arrange_tests[i].nrcontrols;
865         template->x = arrange_tests[i].poszDlg.x;
866         template->y = arrange_tests[i].poszDlg.y;
867         template->cx = arrange_tests[i].poszDlg.cx;
868         template->cy = arrange_tests[i].poszDlg.cy;
869         itemtemplateStc32->x = arrange_tests[i].poszStc32.x;
870         itemtemplateStc32->y = arrange_tests[i].poszStc32.y;
871         itemtemplateStc32->cx = arrange_tests[i].poszStc32.cx;
872         itemtemplateStc32->cy = arrange_tests[i].poszStc32.cy;
873         itemtemplateBtn->x = arrange_tests[i].poszBtn.x;
874         itemtemplateBtn->y = arrange_tests[i].poszBtn.y;
875         itemtemplateBtn->cx = arrange_tests[i].poszBtn.cx;
876         itemtemplateBtn->cy = arrange_tests[i].poszBtn.cy;
877         ret = GetOpenFileNameA(&ofn);
878         ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
879         ret = CommDlgExtendedError();
880         ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
881     }
882 }
883
884 static CHAR SYSDIR[MAX_PATH];
885
886 static UINT_PTR CALLBACK path_hook_proc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
887 {
888     LPNMHDR nmh;
889
890     if( msg == WM_NOTIFY)
891     {
892         nmh = (LPNMHDR) lParam;
893         if( nmh->code == CDN_INITDONE)
894         {
895             PostMessage( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
896         }
897         else if ( nmh->code == CDN_FOLDERCHANGE)
898         {
899             char buf[1024];
900             int ret;
901
902             memset(buf, 0x66, sizeof(buf));
903             ret = SendMessageA( GetParent(hDlg), CDM_GETFOLDERPATH, sizeof(buf), (LPARAM)buf);
904             ok(!lstrcmpiA(SYSDIR, buf), "Expected '%s', got '%s'\n", SYSDIR, buf);
905             ok(lstrlenA(SYSDIR) + 1 == ret, "Expected %d, got %d\n", lstrlenA(SYSDIR) + 1, ret);
906         }
907     }
908
909     return 0;
910 }
911
912 static void test_getfolderpath(void)
913 {
914     OPENFILENAMEA ofn;
915     BOOL result;
916     char szFileName[MAX_PATH] = "";
917     char szInitialDir[MAX_PATH];
918
919     /* We need to pick a different directory as the other tests because of new
920      * Windows 7 behavior.
921      */
922     GetSystemDirectory(szInitialDir, MAX_PATH);
923     lstrcpyA(SYSDIR, szInitialDir);
924
925     ZeroMemory(&ofn, sizeof(ofn));
926
927     ofn.lStructSize = sizeof(ofn);
928     ofn.hwndOwner = NULL;
929     ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
930     ofn.lpstrFile = szFileName;
931     ofn.nMaxFile = MAX_PATH;
932     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
933     ofn.lpstrDefExt = "txt";
934     ofn.lpfnHook = path_hook_proc;
935     ofn.lpstrInitialDir = szInitialDir;
936
937     result = GetOpenFileNameA(&ofn);
938     ok(0 == result, "expected 0, got %d\n", result);
939     ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
940        CommDlgExtendedError());
941
942     result = GetSaveFileNameA(&ofn);
943     ok(0 == result, "expected 0, got %d\n", result);
944     ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
945        CommDlgExtendedError());
946 }
947
948 static void test_resizable2(void)
949 {
950     OPENFILENAMEA ofn = {0};
951     char filename[1024] = "pls press Enter if sizable, Esc otherwise";
952     DWORD ret;
953
954     /* interactive because there is no hook function */
955     if( !winetest_interactive) {
956         skip( "some interactive resizable dialog tests (set WINETEST_INTERACTIVE=1)\n");
957         return;
958     }
959     ofn.lStructSize = sizeof(ofn);
960     ofn.lpstrFile = filename;
961     ofn.nMaxFile = 1024;
962     ofn.lpfnHook = NULL;
963     ofn.hInstance = GetModuleHandleA(NULL);
964     ofn.lpTemplateName = "template1";
965     ofn.Flags = OFN_EXPLORER;
966 #define ISSIZABLE 1
967     ret = GetOpenFileNameA(&ofn);
968     ok( ret == ISSIZABLE, "File Dialog should have been sizable\n");
969     ret = CommDlgExtendedError();
970     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
971     ofn.Flags = OFN_EXPLORER | OFN_ENABLETEMPLATE;
972     ret = GetOpenFileNameA(&ofn);
973     ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
974     ret = CommDlgExtendedError();
975     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
976     ofn.Flags = OFN_EXPLORER | OFN_ENABLETEMPLATEHANDLE;
977     ofn.hInstance = LoadResource( GetModuleHandle(NULL), FindResource( GetModuleHandle(NULL), "template1", (LPSTR)RT_DIALOG));
978     ofn.lpTemplateName = NULL;
979     ret = GetOpenFileNameA(&ofn);
980     ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
981     ret = CommDlgExtendedError();
982     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
983     ofn.Flags = OFN_EXPLORER | OFN_ENABLEHOOK;
984     ret = GetOpenFileNameA(&ofn);
985     ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
986     ret = CommDlgExtendedError();
987     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
988 #undef ISSIZABLE
989 }
990
991 static void test_mru(void)
992 {
993     ok_wndproc_testcase testcase = {0};
994     OPENFILENAME ofn = {sizeof(OPENFILENAME)};
995     const char *test_dir_name = "C:\\mru_test";
996     const char *test_file_name = "test.txt";
997     const char *test_full_path = "C:\\mru_test\\test.txt";
998     char filename_buf[MAX_PATH];
999     DWORD ret;
1000
1001     ofn.lpstrFile = filename_buf;
1002     ofn.nMaxFile = sizeof(filename_buf);
1003     ofn.lpTemplateName = "template1";
1004     ofn.hInstance = GetModuleHandle(NULL);
1005     ofn.Flags =  OFN_ENABLEHOOK | OFN_EXPLORER | OFN_ENABLETEMPLATE | OFN_NOCHANGEDIR;
1006     ofn.lCustData = (LPARAM)&testcase;
1007     ofn.lpfnHook = (LPOFNHOOKPROC)test_ok_wndproc;
1008
1009     SetLastError(0xdeadbeef);
1010     ret = CreateDirectoryA(test_dir_name, NULL);
1011     ok(ret == TRUE, "CreateDirectoryA should have succeeded: %d\n", GetLastError());
1012
1013     /* "teach" comdlg32 about this directory */
1014     strcpy(filename_buf, test_full_path);
1015     SetLastError(0xdeadbeef);
1016     ret = GetOpenFileNameA(&ofn);
1017     ok(ret, "GetOpenFileNameA should have succeeded: %d\n", GetLastError());
1018     ret = CommDlgExtendedError();
1019     ok(!ret, "CommDlgExtendedError returned %x\n", ret);
1020     ok(testcase.actclose, "Open File dialog should have closed.\n");
1021     ok(!strcmp(ofn.lpstrFile, test_full_path), "Expected to get %s, got %s\n", test_full_path, ofn.lpstrFile);
1022
1023     /* get a filename without a full path. it should return the file in
1024      * test_dir_name, not in the CWD */
1025     strcpy(filename_buf, test_file_name);
1026     SetLastError(0xdeadbeef);
1027     ret = GetOpenFileNameA(&ofn);
1028     ok(ret, "GetOpenFileNameA should have succeeded: %d\n", GetLastError());
1029     ret = CommDlgExtendedError();
1030     ok(!ret, "CommDlgExtendedError returned %x\n", ret);
1031     ok(testcase.actclose, "Open File dialog should have closed.\n");
1032     if(strcmp(ofn.lpstrFile, test_full_path) != 0)
1033         win_skip("Platform doesn't save MRU data\n");
1034
1035     SetLastError(0xdeadbeef);
1036     ret = RemoveDirectoryA(test_dir_name);
1037     ok(ret == TRUE, "RemoveDirectoryA should have succeeded: %d\n", GetLastError());
1038 }
1039
1040 static UINT_PTR WINAPI test_extension_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
1041 {
1042     HWND parent = GetParent( dlg);
1043     if( msg == WM_NOTIFY) {
1044         SetTimer( dlg, 0, 100, 0);
1045         PostMessage( parent, WM_COMMAND, IDOK, 0);
1046     }
1047     if( msg == WM_TIMER) {
1048         /* the dialog did not close automatically */
1049         KillTimer( dlg, 0);
1050         PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
1051     }
1052     return FALSE;
1053 }
1054
1055 static const char *defext_filters[] = {
1056     "TestFilter (*.pt*)\0*.pt*\0",
1057     "TestFilter (*.ab?)\0*.ab?\0",
1058     "TestFilter (*.*)\0*.*\0",
1059     NULL    /* is a test, not an endmark! */
1060 };
1061
1062 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
1063
1064 static void test_extension(void)
1065 {
1066     OPENFILENAME ofn = { sizeof(OPENFILENAME)};
1067     char filename[1024] = {0};
1068     char curdir[MAX_PATH];
1069     char *filename_ptr;
1070     const char *test_file_name = "deadbeef";
1071     const char **cur_filter;
1072     DWORD ret;
1073     BOOL boolret;
1074
1075     boolret = GetCurrentDirectoryA(sizeof(curdir), curdir);
1076     ok(boolret, "Failed to get current dir err %d\n", GetLastError());
1077
1078     /* Ignore .* extension */
1079     ofn.lStructSize = sizeof(ofn);
1080     ofn.hwndOwner = NULL;
1081     ofn.lpstrFile = filename;
1082     ofn.nMaxFile = MAX_PATH;
1083     ofn.Flags = OFN_EXPLORER | OFN_ENABLEHOOK;
1084     ofn.lpstrDefExt = NULL;
1085     ofn.lpstrInitialDir = curdir;
1086     ofn.lpfnHook = test_extension_wndproc;
1087     ofn.nFileExtension = 0;
1088
1089     for (cur_filter = defext_filters; cur_filter < defext_filters + ARRAY_SIZE(defext_filters); cur_filter++) {
1090         ofn.lpstrFilter = *cur_filter;
1091         strcpy(filename, test_file_name);
1092         boolret = GetSaveFileNameA(&ofn);
1093         ok(boolret, "expected true\n");
1094         ret = CommDlgExtendedError();
1095         ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
1096         filename_ptr = ofn.lpstrFile + strlen( ofn.lpstrFile ) - strlen( test_file_name );
1097         ok( strlen(ofn.lpstrFile) >= strlen(test_file_name), "Filename %s is too short\n", ofn.lpstrFile );
1098         ok( strcmp(filename_ptr, test_file_name) == 0,
1099             "Filename is %s, expected %s\n", filename_ptr, test_file_name );
1100     }
1101 }
1102
1103 #undef ARRAY_SIZE
1104
1105 START_TEST(filedlg)
1106 {
1107     test_DialogCancel();
1108     test_create_view_window2();
1109     test_create_view_template();
1110     test_arrange();
1111     test_resize();
1112     test_ok();
1113     test_getfolderpath();
1114     test_mru();
1115     if( resizesupported) test_resizable2();
1116     test_extension();
1117 }