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 "precomp.h"
23 
24 #include <shlguid.h>
25 #include <shobjidl.h>
26 #include <ole2.h>
27 #include <reactos/undocuser.h>
28 
29 /* ##### */
30 
31 static BOOL resizesupported = TRUE;
32 
33 static void toolbarcheck( HWND hDlg)
34 {
35     /* test toolbar properties */
36     /* bug #10532 */
37     int maxtextrows;
38     HWND ctrl;
39     DWORD ret;
40     char classname[20];
41 
42     for( ctrl = GetWindow( hDlg, GW_CHILD);
43             ctrl ; ctrl = GetWindow( ctrl, GW_HWNDNEXT)) {
44         GetClassNameA( ctrl, classname, 10);
45         classname[7] = '\0';
46         if( !strcmp( classname, "Toolbar")) break;
47     }
48     ok( ctrl != NULL, "could not get the toolbar control\n");
49     ret = SendMessageA( ctrl, TB_ADDSTRINGA, 0, (LPARAM)"winetestwinetest\0\0");
50     ok( ret == 0, "addstring returned %d (expected 0)\n", ret);
51     maxtextrows = SendMessageA( ctrl, TB_GETTEXTROWS, 0, 0);
52     ok( maxtextrows == 0 || broken(maxtextrows == 1),  /* Win2k and below */
53         "Get(Max)TextRows returned %d (expected 0)\n", maxtextrows);
54 }
55 
56 
57 static UINT_PTR CALLBACK OFNHookProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
58 {
59     LPNMHDR nmh;
60 
61     if( msg == WM_NOTIFY)
62     {
63         nmh = (LPNMHDR) lParam;
64         if( nmh->code == CDN_INITDONE)
65         {
66             PostMessageA( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
67         } else if (nmh->code == CDN_FOLDERCHANGE )
68         {
69             char buf[1024];
70             int ret;
71 
72             memset(buf, 0x66, sizeof(buf));
73             ret = SendMessageA( GetParent(hDlg), CDM_GETFOLDERIDLIST, 5, (LPARAM)buf);
74             ok(ret > 0, "CMD_GETFOLDERIDLIST not implemented\n");
75             if (ret > 5)
76                 ok(buf[0] == 0x66 && buf[1] == 0x66, "CMD_GETFOLDERIDLIST: The buffer was touched on failure\n");
77             toolbarcheck( GetParent(hDlg));
78         }
79     }
80 
81     return 0;
82 }
83 
84 /* bug 6829 */
85 static void test_DialogCancel(void)
86 {
87     OPENFILENAMEA ofn;
88     BOOL result;
89     char szFileName[MAX_PATH] = "";
90     char szInitialDir[MAX_PATH];
91 
92     GetWindowsDirectoryA(szInitialDir, MAX_PATH);
93 
94     ZeroMemory(&ofn, sizeof(ofn));
95 
96     ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400A;
97     ofn.hwndOwner = NULL;
98     ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
99     ofn.lpstrFile = szFileName;
100     ofn.nMaxFile = MAX_PATH;
101     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
102     ofn.lpstrDefExt = "txt";
103     ofn.lpfnHook = OFNHookProc;
104     ofn.lpstrInitialDir = szInitialDir;
105 
106     PrintDlgA(NULL);
107     ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
108        "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
109 
110     result = GetOpenFileNameA(&ofn);
111     ok(FALSE == result, "expected FALSE, got %d\n", result);
112     ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
113        CommDlgExtendedError());
114 
115     PrintDlgA(NULL);
116     ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
117        "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
118 
119     result = GetSaveFileNameA(&ofn);
120     ok(FALSE == result, "expected FALSE, got %d\n", result);
121     ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
122        CommDlgExtendedError());
123 
124     PrintDlgA(NULL);
125     ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
126        "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
127 
128     /* Before passing the ofn to Unicode functions, remove the ANSI strings */
129     ofn.lpstrFilter = NULL;
130     ofn.lpstrInitialDir = NULL;
131     ofn.lpstrDefExt = NULL;
132 
133     PrintDlgA(NULL);
134     ok(CDERR_INITIALIZATION == CommDlgExtendedError(),
135        "expected CDERR_INITIALIZATION, got %d\n", CommDlgExtendedError());
136 
137     SetLastError(0xdeadbeef);
138     result = GetOpenFileNameW((LPOPENFILENAMEW) &ofn);
139     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
140         win_skip("GetOpenFileNameW is not implemented\n");
141     else
142     {
143         ok(FALSE == result, "expected FALSE, got %d\n", result);
144         ok(0 == CommDlgExtendedError(), "expected 0, got %d\n", CommDlgExtendedError());
145     }
146 
147     SetLastError(0xdeadbeef);
148     result = GetSaveFileNameW((LPOPENFILENAMEW) &ofn);
149     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
150         win_skip("GetSaveFileNameW is not implemented\n");
151     else
152     {
153         ok(FALSE == result, "expected FALSE, got %d\n", result);
154         ok(0 == CommDlgExtendedError(), "expected 0, got %d\n", CommDlgExtendedError());
155     }
156 }
157 
158 static UINT_PTR CALLBACK create_view_window2_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
159 {
160     if (msg == WM_NOTIFY)
161     {
162         if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
163         {
164             IShellBrowser *shell_browser = (IShellBrowser *)SendMessageA(GetParent(dlg), WM_USER + 7 /* WM_GETISHELLBROWSER */, 0, 0);
165             IShellView *shell_view = NULL;
166             IShellView2 *shell_view2 = NULL;
167             SV2CVW2_PARAMS view_params;
168             FOLDERSETTINGS folder_settings;
169             HRESULT hr;
170             RECT rect = {0, 0, 0, 0};
171 
172             hr = IShellBrowser_QueryActiveShellView(shell_browser, &shell_view);
173             ok(SUCCEEDED(hr), "QueryActiveShellView returned %#x\n", hr);
174             if (FAILED(hr)) goto cleanup;
175 
176             hr = IShellView_QueryInterface(shell_view, &IID_IShellView2, (void **)&shell_view2);
177             if (hr == E_NOINTERFACE)
178             {
179                 win_skip("IShellView2 not supported\n");
180                 goto cleanup;
181             }
182             ok(SUCCEEDED(hr), "QueryInterface returned %#x\n", hr);
183             if (FAILED(hr)) goto cleanup;
184 
185             hr = IShellView2_DestroyViewWindow(shell_view2);
186             ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
187 
188             folder_settings.ViewMode = FVM_LIST;
189             folder_settings.fFlags = 0;
190 
191             view_params.cbSize = sizeof(view_params);
192             view_params.psvPrev = NULL;
193             view_params.pfs = &folder_settings;
194             view_params.psbOwner = shell_browser;
195             view_params.prcView = &rect;
196             view_params.pvid = NULL;
197             view_params.hwndView = NULL;
198 
199             hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
200             if (hr == E_FAIL)
201             {
202                 win_skip("CreateViewWindow2 is broken on Vista/W2K8\n");
203                 goto cleanup;
204             }
205             ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
206             if (FAILED(hr)) goto cleanup;
207 
208             hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
209             ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
210             ok(folder_settings.ViewMode == FVM_LIST,
211                "view mode is %d, expected FVM_LIST\n",
212                folder_settings.ViewMode);
213 
214             hr = IShellView2_DestroyViewWindow(shell_view2);
215             ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
216 
217             /* XP and W2K3 need this. On W2K the call to DestroyWindow() fails and has
218              * no side effects. NT4 doesn't get here. (FIXME: Vista doesn't get here yet).
219              */
220             DestroyWindow(view_params.hwndView);
221 
222             view_params.pvid = &VID_Details;
223             hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
224             ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
225             if (FAILED(hr)) goto cleanup;
226 
227             hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
228             ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
229             ok(folder_settings.ViewMode == FVM_DETAILS || broken(folder_settings.ViewMode == FVM_LIST), /* nt4 */
230                "view mode is %d, expected FVM_DETAILS\n",
231                folder_settings.ViewMode);
232 
233 cleanup:
234             if (shell_view2) IShellView2_Release(shell_view2);
235             if (shell_view) IShellView_Release(shell_view);
236             PostMessageA(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
237         }
238     }
239     return 0;
240 }
241 
242 static UINT_PTR WINAPI template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
243 {
244     if (msg == WM_INITDIALOG)
245     {
246         HWND p,cb;
247         INT sel;
248         p = GetParent(dlg);
249         ok(p!=NULL, "Failed to get parent of template\n");
250         cb = GetDlgItem(p,0x470);
251         ok(cb!=NULL, "Failed to get filter combobox\n");
252         sel = SendMessageA(cb, CB_GETCURSEL, 0, 0);
253         ok (sel != -1, "Failed to get selection from filter listbox\n");
254     }
255     if (msg == WM_NOTIFY)
256     {
257         if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
258             PostMessageA(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
259     }
260     return 0;
261 }
262 
263 static void test_create_view_window2(void)
264 {
265     OPENFILENAMEA ofn = {0};
266     char filename[1024] = {0};
267     DWORD ret;
268 
269     ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400A;
270     ofn.lpstrFile = filename;
271     ofn.nMaxFile = 1024;
272     ofn.lpfnHook = create_view_window2_hook;
273     ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
274     ret = GetOpenFileNameA(&ofn);
275     ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
276     ret = CommDlgExtendedError();
277     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
278 }
279 
280 static void test_create_view_template(void)
281 {
282     OPENFILENAMEA ofn = {0};
283     char filename[1024] = {0};
284     DWORD ret;
285 
286     ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400A;
287     ofn.lpstrFile = filename;
288     ofn.nMaxFile = 1024;
289     ofn.lpfnHook = template_hook;
290     ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE;
291     ofn.hInstance = GetModuleHandleA(NULL);
292     ofn.lpTemplateName = "template1";
293     ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
294     ret = GetOpenFileNameA(&ofn);
295     ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
296     ret = CommDlgExtendedError();
297     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
298 }
299 
300 /* test cases for resizing of the file dialog */
301 static const struct {
302     DWORD flags;
303     int resize_folderchange;/* change in CDN_FOLDERCHANGE handler */
304     int resize_timer1;      /* change in first WM_TIMER handler */
305     int resize_check;       /* expected change (in second  WM_TIMER handler) */
306     BOOL todo;              /* mark that test todo_wine */
307     BOOL testcontrols;      /* test resizing and moving of the controls */
308 } resize_testcases[] = {
309     { 0                , 10, 10, 20,FALSE,FALSE},   /* 0 */
310     { 0                ,-10,-10,-20,FALSE,FALSE},
311     { OFN_ENABLESIZING ,  0,  0,  0,FALSE,FALSE},
312     { OFN_ENABLESIZING ,  0,-10,  0,FALSE,FALSE},
313     { OFN_ENABLESIZING ,  0, 10, 10,FALSE, TRUE},
314     { OFN_ENABLESIZING ,-10,  0, 10,FALSE,FALSE},   /* 5 */
315     { OFN_ENABLESIZING , 10,  0, 10,FALSE,FALSE},
316     { OFN_ENABLESIZING ,  0, 10, 20,FALSE,FALSE},
317     /* mark the end */
318     { 0xffffffff }
319 };
320 
321 static UINT_PTR WINAPI resize_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
322 {
323     static RECT initrc, rc;
324     static int index, count;
325     static BOOL gotSWP_bottom, gotShowWindow;
326     HWND parent = GetParent( dlg);
327     int resize;
328 #define MAXNRCTRLS 30
329     static RECT ctrlrcs[MAXNRCTRLS];
330     static int ctrlids[MAXNRCTRLS];
331     static HWND ctrls[MAXNRCTRLS];
332     static int nrctrls;
333 
334     switch( msg)
335     {
336         case WM_INITDIALOG:
337         {
338             DWORD style;
339 
340             index = ((OPENFILENAMEA*)lParam)->lCustData;
341             count = 0;
342             gotSWP_bottom = gotShowWindow = FALSE;
343             /* test style */
344             style = GetWindowLongA( parent, GWL_STYLE);
345             if( resize_testcases[index].flags & OFN_ENABLESIZING)
346                 if( !(style & WS_SIZEBOX)) {
347                     win_skip( "OFN_ENABLESIZING flag not supported.\n");
348                     resizesupported = FALSE;
349                     PostMessageA( parent, WM_COMMAND, IDCANCEL, 0);
350                 } else
351                     ok( style & WS_SIZEBOX,
352                             "testid %d: dialog should have a WS_SIZEBOX style.\n", index);
353             else
354                 ok( !(style & WS_SIZEBOX),
355                         "testid %d: dialog should not have a WS_SIZEBOX style.\n", index);
356             break;
357         }
358         case WM_NOTIFY:
359         {
360             if(( (LPNMHDR)lParam)->code == CDN_FOLDERCHANGE){
361                 GetWindowRect( parent, &initrc);
362                 if( (resize  = resize_testcases[index].resize_folderchange)){
363                     MoveWindow( parent, initrc.left,initrc.top, initrc.right - initrc.left + resize,
364                             initrc.bottom - initrc.top + resize, TRUE);
365                 }
366                 SetTimer( dlg, 0, 100, 0);
367             }
368             break;
369         }
370         case WM_TIMER:
371         {
372             if( count == 0){
373                 /* store the control rectangles */
374                 if( resize_testcases[index].testcontrols) {
375                     HWND ctrl;
376                     int i;
377                     for( i = 0, ctrl = GetWindow( parent, GW_CHILD);
378                             i < MAXNRCTRLS && ctrl;
379                             i++, ctrl = GetWindow( ctrl, GW_HWNDNEXT)) {
380                         ctrlids[i] = GetDlgCtrlID( ctrl);
381                         GetWindowRect( ctrl, &ctrlrcs[i]);
382                         MapWindowPoints( NULL, parent, (LPPOINT) &ctrlrcs[i], 2);
383                         ctrls[i] = ctrl;
384                     }
385                     nrctrls = i;
386                 }
387                 if( (resize  = resize_testcases[index].resize_timer1)){
388                     GetWindowRect( parent, &rc);
389                     MoveWindow( parent, rc.left,rc.top, rc.right - rc.left + resize,
390                             rc.bottom - rc.top + resize, TRUE);
391                 }
392             } else if( count == 1){
393                 resize  = resize_testcases[index].resize_check;
394                 GetWindowRect( parent, &rc);
395                 todo_wine_if( resize_testcases[index].todo){
396                         ok( resize == rc.right - rc.left - initrc.right + initrc.left,
397                             "testid %d size-x change %d expected %d\n", index,
398                             rc.right - rc.left - initrc.right + initrc.left, resize);
399                         ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
400                             "testid %d size-y change %d expected %d\n", index,
401                             rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
402                     }
403                 if( resize_testcases[index].testcontrols) {
404                     int i;
405                     RECT rc;
406                     for( i = 0; i < nrctrls; i++) {
407                         GetWindowRect( ctrls[i], &rc);
408                         MapWindowPoints( NULL, parent, (LPPOINT) &rc, 2);
409                         switch( ctrlids[i]){
410 
411 /* test if RECT R1, moved and sized result in R2 */
412 #define TESTRECTS( R1, R2, Mx, My, Sx, Sy) \
413          ((R1).left + (Mx) ==(R2).left \
414         &&(R1).top + (My) ==(R2).top \
415         &&(R1).right + (Mx) + (Sx) == (R2).right \
416         &&(R1).bottom + (My) + (Sy) ==(R2).bottom)
417 
418                             /* sized horizontal and moved vertical */
419                             case cmb1:
420                             case edt1:
421                                 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 10, 0),
422                                     "control id %03x should have sized horizontally and moved vertically, before %s after %s\n",
423                                     ctrlids[i], wine_dbgstr_rect( &ctrlrcs[i] ),
424                                     wine_dbgstr_rect( &rc ));
425                                 break;
426                             /* sized horizontal and vertical */
427                             case lst2:
428                                 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 10),
429                                     "control id %03x should have sized horizontally and vertically, before %s after %s\n",
430                                     ctrlids[i], wine_dbgstr_rect( &ctrlrcs[i] ),
431                                     wine_dbgstr_rect( &rc ));
432                                 break;
433                             /* moved horizontal and vertical */
434                             case IDCANCEL:
435                             case pshHelp:
436                                 ok( TESTRECTS( ctrlrcs[i], rc, 10, 10, 0, 0),
437                                     "control id %03x should have moved horizontally and vertically, before %s after %s\n",
438                                     ctrlids[i], wine_dbgstr_rect( &ctrlrcs[i] ),
439                                     wine_dbgstr_rect( &rc ));
440                                 break;
441                             /* moved vertically */
442                             case chx1:
443                             case stc2:
444                             case stc3:
445                                 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0),
446                                     "control id %03x should have moved vertically, before %s after %s\n",
447                                     ctrlids[i], wine_dbgstr_rect( &ctrlrcs[i] ),
448                                     wine_dbgstr_rect( &rc ));
449                                 break;
450                             /* resized horizontal */
451                             case cmb2: /* aka IDC_LOOKIN */
452                                 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 0)||
453                                         TESTRECTS( ctrlrcs[i], rc, 0, 0, 0, 0), /* Vista and higher */
454                                     "control id %03x should have resized horizontally, before %s after %s\n",
455                                     ctrlids[i], wine_dbgstr_rect( &ctrlrcs[i] ),
456                                     wine_dbgstr_rect( &rc ));
457                                 break;
458                             /* non moving non sizing controls */
459                             case stc4:
460                                 ok( TESTRECTS( rc, ctrlrcs[i], 0, 0, 0, 0),
461                                     "control id %03x was moved/resized, before %s after %s\n",
462                                     ctrlids[i], wine_dbgstr_rect( &ctrlrcs[i] ),
463                                     wine_dbgstr_rect( &rc ));
464                                 break;
465                             /* todo_wine: non moving non sizing controls */
466                             case lst1:
467 todo_wine
468                                 ok( TESTRECTS( rc, ctrlrcs[i], 0, 0, 0, 0),
469                                     "control id %03x was moved/resized, before %s after %s\n",
470                                     ctrlids[i], wine_dbgstr_rect( &ctrlrcs[i] ),
471                                     wine_dbgstr_rect( &rc ));
472                                 break;
473                             /* don't test: id is not unique */
474                             case IDOK:
475                             case stc1:
476                             case 0:
477                             case  -1:
478                                 break;
479                             default:
480                                 trace("untested control id %03x before %s after %s\n",
481                                     ctrlids[i], wine_dbgstr_rect( &ctrlrcs[i] ),
482                                     wine_dbgstr_rect( &rc ));
483 #undef TESTRECTS
484 #undef MAXNRCTRLS
485                         }
486                     }
487                 }
488                 KillTimer( dlg, 0);
489                 PostMessageA( parent, WM_COMMAND, IDCANCEL, 0);
490             }
491             count++;
492         }
493         break;
494         case WM_WINDOWPOSCHANGING:
495         {
496             WINDOWPOS *pwp = (WINDOWPOS *)lParam;
497             if(  !index && pwp->hwndInsertAfter == HWND_BOTTOM){
498                 gotSWP_bottom = TRUE;
499                 ok(!gotShowWindow, "The WM_WINDOWPOSCHANGING message came after a WM_SHOWWINDOW message\n");
500             }
501         }
502         break;
503         case WM_SHOWWINDOW:
504         {
505             if(  !index){
506                 gotShowWindow = TRUE;
507                 ok(gotSWP_bottom, "No WM_WINDOWPOSCHANGING message came before a WM_SHOWWINDOW message\n");
508             }
509         }
510         break;
511     }
512     return 0;
513 }
514 
515 static void test_resize(void)
516 {
517     OPENFILENAMEA ofn = { OPENFILENAME_SIZE_VERSION_400A };
518     char filename[1024] = {0};
519     DWORD ret;
520     int i;
521 
522     ofn.lpstrFile = filename;
523     ofn.nMaxFile = 1024;
524     ofn.lpfnHook = resize_template_hook;
525     ofn.hInstance = GetModuleHandleA(NULL);
526     ofn.lpTemplateName = "template_sz";
527     for( i = 0; resize_testcases[i].flags != 0xffffffff; i++) {
528         ofn.lCustData = i;
529         ofn.Flags = resize_testcases[i].flags |
530             OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE | OFN_SHOWHELP ;
531         ret = GetOpenFileNameA(&ofn);
532         ok(!ret, "GetOpenFileName returned %#x\n", ret);
533         ret = CommDlgExtendedError();
534         ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
535     }
536 }
537 
538 /* test cases for control message IDOK */
539 /* Show case for bug #19079 */
540 typedef struct {
541     int  retval;        /* return code of the message handler */
542     BOOL setmsgresult;  /* set the result in the DWLP_MSGRESULT */
543     BOOL usemsgokstr;   /* use the FILEOKSTRING message instead of WM_NOTIFY:CDN_FILEOK */
544     BOOL do_subclass;   /* subclass the dialog hook procedure */
545     BOOL expclose;      /* is the dialog expected to close ? */
546     BOOL actclose;      /* has the dialog actually closed ? */
547 } ok_wndproc_testcase;
548 
549 static ok_wndproc_testcase ok_testcases[] = {
550     { 0,        FALSE,  FALSE,  FALSE,  TRUE},
551     { 0,         TRUE,  FALSE,  FALSE,  TRUE},
552     { 0,        FALSE,  FALSE,   TRUE,  TRUE},
553     { 0,         TRUE,  FALSE,   TRUE,  TRUE},
554     { 1,        FALSE,  FALSE,  FALSE,  TRUE},
555     { 1,         TRUE,  FALSE,  FALSE, FALSE},
556     { 1,        FALSE,  FALSE,   TRUE, FALSE},
557     { 1,         TRUE,  FALSE,   TRUE, FALSE},
558     /* FILEOKSTRING tests */
559     { 1,         TRUE,   TRUE,  FALSE, FALSE},
560     { 1,        FALSE,   TRUE,   TRUE, FALSE},
561     /* mark the end */
562     { -1 }
563 };
564 
565 /* test_ok_wndproc can be used as hook procedure or a subclass
566  * window proc for the file dialog */
567 static UINT_PTR WINAPI test_ok_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
568 {
569     HWND parent = GetParent( dlg);
570     static ok_wndproc_testcase *testcase = NULL;
571     static UINT msgFILEOKSTRING;
572     if (msg == WM_INITDIALOG)
573     {
574         testcase = (ok_wndproc_testcase*)((OPENFILENAMEA*)lParam)->lCustData;
575         testcase->actclose = TRUE;
576         msgFILEOKSTRING = RegisterWindowMessageA( FILEOKSTRINGA);
577     }
578     if( msg == WM_NOTIFY) {
579         if(((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
580             SetTimer( dlg, 0, 100, 0);
581             PostMessageA( parent, WM_COMMAND, IDOK, 0);
582             return FALSE;
583         } else if(((LPNMHDR)lParam)->code == CDN_FILEOK) {
584             if( testcase->usemsgokstr)
585                 return FALSE;
586             if( testcase->setmsgresult)
587                 SetWindowLongPtrA( dlg, DWLP_MSGRESULT, testcase->retval);
588             return testcase->retval;
589         }
590     }
591     if( msg == msgFILEOKSTRING) {
592         if( !testcase->usemsgokstr)
593             return FALSE;
594         if( testcase->setmsgresult)
595             SetWindowLongPtrA( dlg, DWLP_MSGRESULT, testcase->retval);
596         return testcase->retval;
597     }
598     if( msg == WM_TIMER) {
599         /* the dialog did not close automatically */
600         testcase->actclose = FALSE;
601         KillTimer( dlg, 0);
602         PostMessageA( parent, WM_COMMAND, IDCANCEL, 0);
603         return FALSE;
604     }
605     if( testcase && testcase->do_subclass)
606         return DefWindowProcA( dlg, msg, wParam, lParam);
607     return FALSE;
608 }
609 
610 static UINT_PTR WINAPI ok_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
611 {
612     if (msg == WM_SETFONT)
613         SetWindowLongPtrA( dlg, GWLP_WNDPROC, (LONG_PTR) test_ok_wndproc);
614     return FALSE;
615 }
616 
617 static void test_ok(void)
618 {
619     OPENFILENAMEA ofn = { OPENFILENAME_SIZE_VERSION_400A };
620     char filename[1024] = {0};
621     char tmpfilename[ MAX_PATH];
622     char curdir[MAX_PATH];
623     int i;
624     DWORD ret;
625     BOOL cdret;
626 
627     cdret = GetCurrentDirectoryA(sizeof(curdir), curdir);
628     ok(cdret, "Failed to get current dir err %d\n", GetLastError());
629     if (!GetTempFileNameA(".", "txt", 0, tmpfilename)) {
630         skip("Failed to create a temporary file name\n");
631         return;
632     }
633     ofn.lpstrFile = filename;
634     ofn.nMaxFile = 1024;
635     ofn.hInstance = GetModuleHandleA(NULL);
636     ofn.lpTemplateName = "template1";
637     ofn.Flags =  OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE ;
638     for( i = 0; ok_testcases[i].retval != -1; i++) {
639         strcpy( filename, tmpfilename);
640         ofn.lCustData = (LPARAM)(ok_testcases + i);
641         ofn.lpfnHook = ok_testcases[i].do_subclass ? ok_template_hook : test_ok_wndproc;
642         ret = GetOpenFileNameA(&ofn);
643         ok( ok_testcases[i].expclose == ok_testcases[i].actclose,
644                 "testid %d: Open File dialog should %shave closed.\n", i,
645                 ok_testcases[i].expclose ? "" : "NOT ");
646         ok(ret == ok_testcases[i].expclose, "testid %d: GetOpenFileName returned %#x\n", i, ret);
647         ret = CommDlgExtendedError();
648         ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
649         cdret = SetCurrentDirectoryA(curdir);
650         ok(cdret, "Failed to restore current dir err %d\n", GetLastError());
651     }
652     ret =  DeleteFileA( tmpfilename);
653     ok( ret, "Failed to delete temporary file %s err %d\n", tmpfilename, GetLastError());
654 }
655 
656 /* test arranging with a custom template */
657 typedef struct {
658     int x, y;  /* left, top coordinates */
659     int cx, cy; /* width and height */
660 } posz;
661 static struct {
662    int nrcontrols;    /* 0: no controls, 1: just the stc32 control 2: with button */
663    posz poszDlg;
664    posz poszStc32;
665    posz poszBtn;
666    DWORD ofnflags;
667 } arrange_tests[] = {
668     /* do not change the first two cases: used to get the uncustomized sizes */
669     { 0, {0},{0},{0},0 },
670     { 0, {0},{0},{0}, OFN_SHOWHELP},
671     /* two tests with just a subdialog, no controls */
672     { 0, {0, 0, 316, 76},{0},{0},0 },
673     { 0, {0, 0, 100, 76},{0},{0}, OFN_SHOWHELP},
674     /* now with a control with id stc32 */
675     { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0},0 }, /* bug #17748*/
676     { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0}, OFN_SHOWHELP}, /* bug #17748*/
677     /* tests with size of the stc32 control higher or wider then the standard dialog */
678     { 1, {0, 0, 316, 170} ,{0, 0, 204, 170,},{0},0 },
679     { 1, {0, 0, 316, 165} ,{0, 0, 411, 165,},{0}, OFN_SHOWHELP },
680     /* move the stc32 control around */
681     { 1, {0, 0, 300, 100} ,{73, 17, 50, 50,},{0},0 },
682     /* add control */
683     { 2, {0, 0, 280, 100} ,{0, 0, 50, 50,},{300,20,30,30},0 },
684     /* enable resizing should make the dialog bigger */
685     { 0, {0},{0},{0}, OFN_SHOWHELP|OFN_ENABLESIZING},
686     /* mark the end */
687     { -1 }
688 };
689 
690 static UINT_PTR WINAPI template_hook_arrange(HWND dlgChild, UINT msg, WPARAM wParam, LPARAM lParam)
691 {
692     static int index, fixhelp;
693     static posz posz0[2];
694     static RECT clrcParent, clrcChild, rcStc32;
695     static HWND hwndStc32;
696     HWND dlgParent;
697 
698     dlgParent = GetParent( dlgChild);
699     if (msg == WM_INITDIALOG) {
700         index = ((OPENFILENAMEA*)lParam)->lCustData;
701         /* get the positions before rearrangement */
702         GetClientRect( dlgParent, &clrcParent);
703         GetClientRect( dlgChild, &clrcChild);
704         hwndStc32 = GetDlgItem( dlgChild, stc32);
705         if( hwndStc32)  GetWindowRect( hwndStc32, &rcStc32);
706     }
707     if (msg == WM_NOTIFY && ((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
708         RECT wrcParent;
709 
710         GetWindowRect( dlgParent, &wrcParent);
711         /* the fist two "tests" just save the dialogs position, with and without
712          * help button */
713         if( index == 0) {
714             posz0[0].x = wrcParent.left;
715             posz0[0].y = wrcParent.top;
716             posz0[0].cx = wrcParent.right - wrcParent.left;
717             posz0[0].cy = wrcParent.bottom - wrcParent.top;
718         } else if( index == 1) {
719             posz0[1].x = wrcParent.left;
720             posz0[1].y = wrcParent.top;
721             posz0[1].cx = wrcParent.right - wrcParent.left;
722             posz0[1].cy = wrcParent.bottom - wrcParent.top;
723             fixhelp = posz0[1].cy - posz0[0].cy;
724         } else {
725             /* the real tests */
726             int withhelp;
727             int expectx, expecty;
728             DWORD style;
729 
730             withhelp = (arrange_tests[index].ofnflags & OFN_SHOWHELP) != 0;
731             GetWindowRect( dlgParent, &wrcParent);
732             if( !hwndStc32) {
733                 /* case with no custom subitem with stc32:
734                  * default to all custom controls below the standard */
735                 expecty = posz0[withhelp].cy + clrcChild.bottom;
736                 expectx =  posz0[withhelp].cx;
737             } else {
738                 /* special case: there is a control with id stc32 */
739                 /* expected height */
740                 expecty = posz0[withhelp].cy;
741                 if( rcStc32.bottom - rcStc32.top + (withhelp ? 0 : fixhelp) > clrcParent.bottom) {
742                     expecty +=  clrcChild.bottom -  clrcParent.bottom;
743                     if( !withhelp) expecty += fixhelp;
744                 }
745                 else
746                     expecty +=  clrcChild.bottom - ( rcStc32.bottom - rcStc32.top) ;
747                 /* expected width */
748                 expectx = posz0[withhelp].cx;
749                 if( rcStc32.right - rcStc32.left > clrcParent.right) {
750                     expectx +=  clrcChild.right -  clrcParent.right;
751                 }
752                 else
753                     expectx +=  clrcChild.right - ( rcStc32.right - rcStc32.left) ;
754             }
755             style = GetWindowLongA( dlgParent, GWL_STYLE);
756             if( !(style & WS_SIZEBOX)) {
757                 /* without the OFN_ENABLESIZING flag */
758                 ok( wrcParent.bottom - wrcParent.top == expecty,
759                         "Wrong height of dialog %d, expected %d\n",
760                         wrcParent.bottom - wrcParent.top, expecty);
761                 ok( wrcParent.right - wrcParent.left == expectx,
762                         "Wrong width of dialog %d, expected %d\n",
763                         wrcParent.right - wrcParent.left, expectx);
764             } else {
765                 /* with the OFN_ENABLESIZING flag */
766                 ok( wrcParent.bottom - wrcParent.top > expecty,
767                         "Wrong height of dialog %d, expected more than %d\n",
768                         wrcParent.bottom - wrcParent.top, expecty);
769                 ok( wrcParent.right - wrcParent.left > expectx,
770                         "Wrong width of dialog %d, expected more than %d\n",
771                         wrcParent.right - wrcParent.left, expectx);
772             }
773 
774         }
775         PostMessageA( dlgParent, WM_COMMAND, IDCANCEL, 0);
776     }
777     return 0;
778 }
779 
780 static void test_arrange(void)
781 {
782     OPENFILENAMEA ofn = {0};
783     char filename[1024] = {0};
784     DWORD ret;
785     HRSRC hRes;
786     HANDLE hDlgTmpl;
787     LPBYTE pv;
788     DLGTEMPLATE *template;
789     DLGITEMTEMPLATE *itemtemplateStc32, *itemtemplateBtn;
790     int i;
791 
792     /* load subdialog template into memory */
793     hRes = FindResourceA( GetModuleHandleA(NULL), "template_stc32", (LPSTR)RT_DIALOG);
794     hDlgTmpl = LoadResource( GetModuleHandleA(NULL), hRes );
795     /* get pointers to the structures for the dialog and the controls */
796     pv = LockResource( hDlgTmpl );
797     template = (DLGTEMPLATE*)pv;
798     if( template->x != 11111) {
799         win_skip("could not find the dialog template\n");
800         return;
801     }
802     /* skip dialog template, menu, class and title */
803     pv +=  sizeof(DLGTEMPLATE);
804     pv += 3 * sizeof(WORD);
805     /* skip font info */
806     while( *(WORD*)pv)
807         pv += sizeof(WORD);
808     pv += sizeof(WORD);
809     /* align on 32 bit boundaries */
810     pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
811     itemtemplateStc32 = (DLGITEMTEMPLATE*)pv;
812     if( itemtemplateStc32->x != 22222) {
813         win_skip("could not find the first item template\n");
814         return;
815     }
816     /* skip itemtemplate, class, title and creation data */
817     pv += sizeof(DLGITEMTEMPLATE);
818     pv +=  4 * sizeof(WORD);
819     /* align on 32 bit boundaries */
820     pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
821     itemtemplateBtn = (DLGITEMTEMPLATE*)pv;
822     if( itemtemplateBtn->x != 12345) {
823         win_skip("could not find the second item template\n");
824         return;
825     }
826 
827     ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400A;
828     ofn.lpstrFile = filename;
829     ofn.nMaxFile = 1024;
830     ofn.lpfnHook = template_hook_arrange;
831     ofn.hInstance = hDlgTmpl;
832     ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
833     for( i = 0; arrange_tests[i].nrcontrols != -1; i++) {
834         ofn.lCustData = i;
835         ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATEHANDLE | OFN_HIDEREADONLY |
836             arrange_tests[i].ofnflags;
837         template->cdit = arrange_tests[i].nrcontrols;
838         template->x = arrange_tests[i].poszDlg.x;
839         template->y = arrange_tests[i].poszDlg.y;
840         template->cx = arrange_tests[i].poszDlg.cx;
841         template->cy = arrange_tests[i].poszDlg.cy;
842         itemtemplateStc32->x = arrange_tests[i].poszStc32.x;
843         itemtemplateStc32->y = arrange_tests[i].poszStc32.y;
844         itemtemplateStc32->cx = arrange_tests[i].poszStc32.cx;
845         itemtemplateStc32->cy = arrange_tests[i].poszStc32.cy;
846         itemtemplateBtn->x = arrange_tests[i].poszBtn.x;
847         itemtemplateBtn->y = arrange_tests[i].poszBtn.y;
848         itemtemplateBtn->cx = arrange_tests[i].poszBtn.cx;
849         itemtemplateBtn->cy = arrange_tests[i].poszBtn.cy;
850         ret = GetOpenFileNameA(&ofn);
851         ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
852         ret = CommDlgExtendedError();
853         ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
854     }
855 }
856 
857 static CHAR SYSDIR[MAX_PATH];
858 
859 static UINT_PTR CALLBACK path_hook_proc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
860 {
861     LPNMHDR nmh;
862 
863     if( msg == WM_NOTIFY)
864     {
865         nmh = (LPNMHDR) lParam;
866         if( nmh->code == CDN_INITDONE)
867         {
868             PostMessageA( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
869         }
870         else if ( nmh->code == CDN_FOLDERCHANGE)
871         {
872             char buf[1024];
873             int ret;
874 
875             memset(buf, 0x66, sizeof(buf));
876             ret = SendMessageA( GetParent(hDlg), CDM_GETFOLDERPATH, sizeof(buf), (LPARAM)buf);
877             ok(!lstrcmpiA(SYSDIR, buf), "Expected '%s', got '%s'\n", SYSDIR, buf);
878             ok(lstrlenA(SYSDIR) + 1 == ret, "Expected %d, got %d\n", lstrlenA(SYSDIR) + 1, ret);
879         }
880     }
881 
882     return 0;
883 }
884 
885 static void test_getfolderpath(void)
886 {
887     OPENFILENAMEA ofn;
888     BOOL result;
889     char szFileName[MAX_PATH] = "";
890     char szInitialDir[MAX_PATH];
891 
892     /* We need to pick a different directory as the other tests because of new
893      * Windows 7 behavior.
894      */
895     GetSystemDirectoryA(szInitialDir, MAX_PATH);
896     lstrcpyA(SYSDIR, szInitialDir);
897 
898     ZeroMemory(&ofn, sizeof(ofn));
899 
900     ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400A;
901     ofn.hwndOwner = NULL;
902     ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
903     ofn.lpstrFile = szFileName;
904     ofn.nMaxFile = MAX_PATH;
905     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
906     ofn.lpstrDefExt = "txt";
907     ofn.lpfnHook = path_hook_proc;
908     ofn.lpstrInitialDir = szInitialDir;
909 
910     result = GetOpenFileNameA(&ofn);
911     ok(FALSE == result, "expected FALSE, got %d\n", result);
912     ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
913        CommDlgExtendedError());
914 
915     result = GetSaveFileNameA(&ofn);
916     ok(FALSE == result, "expected FALSE, got %d\n", result);
917     ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
918        CommDlgExtendedError());
919 }
920 
921 static void test_resizable2(void)
922 {
923     OPENFILENAMEA ofn = {0};
924     char filename[1024] = "pls press Enter if sizable, Esc otherwise";
925     DWORD ret;
926 
927     /* interactive because there is no hook function */
928     if( !winetest_interactive) {
929         skip( "some interactive resizable dialog tests (set WINETEST_INTERACTIVE=1)\n");
930         return;
931     }
932     ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400A;
933     ofn.lpstrFile = filename;
934     ofn.nMaxFile = 1024;
935     ofn.lpfnHook = NULL;
936     ofn.hInstance = GetModuleHandleA(NULL);
937     ofn.lpTemplateName = "template1";
938     ofn.Flags = OFN_EXPLORER;
939 #define ISSIZABLE TRUE
940     ret = GetOpenFileNameA(&ofn);
941     ok( ret == ISSIZABLE, "File Dialog should have been sizable\n");
942     ret = CommDlgExtendedError();
943     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
944     ofn.Flags = OFN_EXPLORER | OFN_ENABLETEMPLATE;
945     ret = GetOpenFileNameA(&ofn);
946     ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
947     ret = CommDlgExtendedError();
948     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
949     ofn.Flags = OFN_EXPLORER | OFN_ENABLETEMPLATEHANDLE;
950     ofn.hInstance = LoadResource( GetModuleHandleA(NULL), FindResourceA( GetModuleHandleA(NULL), "template1", (LPSTR)RT_DIALOG));
951     ofn.lpTemplateName = NULL;
952     ret = GetOpenFileNameA(&ofn);
953     ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
954     ret = CommDlgExtendedError();
955     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
956     ofn.Flags = OFN_EXPLORER | OFN_ENABLEHOOK;
957     ret = GetOpenFileNameA(&ofn);
958     ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
959     ret = CommDlgExtendedError();
960     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
961 #undef ISSIZABLE
962 }
963 
964 static void test_mru(void)
965 {
966     ok_wndproc_testcase testcase = {0};
967     OPENFILENAMEA ofn = { OPENFILENAME_SIZE_VERSION_400A };
968     const char *test_dir_name = "C:\\mru_test";
969     const char *test_file_name = "test.txt";
970     const char *test_full_path = "C:\\mru_test\\test.txt";
971     char filename_buf[MAX_PATH];
972     DWORD ret;
973 
974     ofn.lpstrFile = filename_buf;
975     ofn.nMaxFile = sizeof(filename_buf);
976     ofn.lpTemplateName = "template1";
977     ofn.hInstance = GetModuleHandleA(NULL);
978     ofn.Flags =  OFN_ENABLEHOOK | OFN_EXPLORER | OFN_ENABLETEMPLATE | OFN_NOCHANGEDIR;
979     ofn.lCustData = (LPARAM)&testcase;
980     ofn.lpfnHook = test_ok_wndproc;
981 
982     SetLastError(0xdeadbeef);
983     ret = CreateDirectoryA(test_dir_name, NULL);
984     ok(ret == TRUE, "CreateDirectoryA should have succeeded: %d\n", GetLastError());
985 
986     /* "teach" comdlg32 about this directory */
987     strcpy(filename_buf, test_full_path);
988     SetLastError(0xdeadbeef);
989     ret = GetOpenFileNameA(&ofn);
990     ok(ret, "GetOpenFileNameA should have succeeded: %d\n", GetLastError());
991     ret = CommDlgExtendedError();
992     ok(!ret, "CommDlgExtendedError returned %x\n", ret);
993     ok(testcase.actclose, "Open File dialog should have closed.\n");
994     ok(!strcmp(ofn.lpstrFile, test_full_path), "Expected to get %s, got %s\n", test_full_path, ofn.lpstrFile);
995 
996     /* get a filename without a full path. it should return the file in
997      * test_dir_name, not in the CWD */
998     strcpy(filename_buf, test_file_name);
999     SetLastError(0xdeadbeef);
1000     ret = GetOpenFileNameA(&ofn);
1001     ok(ret, "GetOpenFileNameA should have succeeded: %d\n", GetLastError());
1002     ret = CommDlgExtendedError();
1003     ok(!ret, "CommDlgExtendedError returned %x\n", ret);
1004     ok(testcase.actclose, "Open File dialog should have closed.\n");
1005     if(strcmp(ofn.lpstrFile, test_full_path) != 0)
1006         win_skip("Platform doesn't save MRU data\n");
1007 
1008     SetLastError(0xdeadbeef);
1009     ret = RemoveDirectoryA(test_dir_name);
1010     ok(ret == TRUE, "RemoveDirectoryA should have succeeded: %d\n", GetLastError());
1011 }
1012 
1013 static UINT_PTR WINAPI test_extension_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
1014 {
1015     HWND parent = GetParent( dlg);
1016     if( msg == WM_NOTIFY) {
1017         SetTimer( dlg, 0, 1000, 0);
1018         PostMessageA( parent, WM_COMMAND, IDOK, 0);
1019     }
1020     if( msg == WM_TIMER) {
1021         /* the dialog did not close automatically */
1022         KillTimer( dlg, 0);
1023         PostMessageA( parent, WM_COMMAND, IDCANCEL, 0);
1024     }
1025     return FALSE;
1026 }
1027 
1028 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
1029 
1030 static void test_extension_helper(OPENFILENAMEA* ofn, const char *filter,
1031                                   const char *expected_filename)
1032 {
1033     char *filename_ptr;
1034     DWORD ret;
1035     BOOL boolret;
1036 
1037     strcpy(ofn->lpstrFile, "deadbeef");
1038     ofn->lpstrFilter = filter;
1039 
1040     boolret = GetSaveFileNameA(ofn);
1041     ok(boolret, "%s: expected TRUE\n", filter);
1042 
1043     ret = CommDlgExtendedError();
1044     ok(!ret, "%s: CommDlgExtendedError returned %#x\n", filter, ret);
1045 
1046     filename_ptr = ofn->lpstrFile + ofn->nFileOffset;
1047     ok(strcmp(filename_ptr, expected_filename) == 0,
1048         "%s: Filename is %s, expected %s\n", filter, filename_ptr, expected_filename);
1049 }
1050 
1051 static void test_extension(void)
1052 {
1053     OPENFILENAMEA ofn = { OPENFILENAME_SIZE_VERSION_400A };
1054     char filename[1024] = {0};
1055     char curdir[MAX_PATH];
1056     unsigned int i;
1057     BOOL boolret;
1058 
1059     const char *defext_concrete_filters[] = {
1060         "TestFilter (*.abc)\0*.abc\0",
1061         "TestFilter (*.abc;)\0*.abc;\0",
1062         "TestFilter (*.abc;*.def)\0*.abc;*.def\0",
1063     };
1064 
1065     const char *defext_wildcard_filters[] = {
1066         "TestFilter (*.pt*)\0*.pt*\0",
1067         "TestFilter (*.pt*;*.abc)\0*.pt*;*.abc\0",
1068         "TestFilter (*.ab?)\0*.ab?\0",
1069         "TestFilter (*.*)\0*.*\0",
1070         "TestFilter (*sav)\0*sav\0",
1071         NULL    /* is a test, not an endmark! */
1072     };
1073 
1074     boolret = GetCurrentDirectoryA(sizeof(curdir), curdir);
1075     ok(boolret, "Failed to get current dir err %d\n", GetLastError());
1076 
1077     ofn.hwndOwner = NULL;
1078     ofn.lpstrFile = filename;
1079     ofn.nMaxFile = MAX_PATH;
1080     ofn.Flags = OFN_EXPLORER | OFN_ENABLEHOOK;
1081     ofn.lpstrInitialDir = curdir;
1082     ofn.lpfnHook = test_extension_wndproc;
1083     ofn.nFileExtension = 0;
1084 
1085     ofn.lpstrDefExt = NULL;
1086 
1087     /* Without lpstrDefExt, append no extension */
1088     test_extension_helper(&ofn, "TestFilter (*.abc) lpstrDefExt=NULL\0*.abc\0", "deadbeef");
1089     test_extension_helper(&ofn, "TestFilter (*.ab?) lpstrDefExt=NULL\0*.ab?\0", "deadbeef");
1090 
1091     ofn.lpstrDefExt = "";
1092 
1093     /* If lpstrDefExt="" and the filter has a concrete extension, append it */
1094     test_extension_helper(&ofn, "TestFilter (*.abc) lpstrDefExt=\"\"\0*.abc\0", "deadbeef.abc");
1095 
1096     /* If lpstrDefExt="" and the filter has a wildcard extension, do nothing */
1097     test_extension_helper(&ofn, "TestFilter (*.ab?) lpstrDefExt=\"\"\0*.ab?\0", "deadbeef");
1098 
1099     ofn.lpstrDefExt = "xyz";
1100 
1101     /* Append concrete extensions from filters */
1102     for (i = 0; i < ARRAY_SIZE(defext_concrete_filters); i++) {
1103         test_extension_helper(&ofn, defext_concrete_filters[i], "deadbeef.abc");
1104     }
1105 
1106     /* Append nothing from this filter */
1107     test_extension_helper(&ofn, "TestFilter (*.)\0*.\0", "deadbeef");
1108 
1109     /* Ignore wildcard extensions in filters */
1110     for (i = 0; i < ARRAY_SIZE(defext_wildcard_filters); i++) {
1111         test_extension_helper(&ofn, defext_wildcard_filters[i], "deadbeef.xyz");
1112     }
1113 
1114     /* Append valid extensions consisting of multiple parts */
1115     test_extension_helper(&ofn, "TestFilter (*.abc.def)\0*.abc.def\0", "deadbeef.abc.def");
1116     test_extension_helper(&ofn, "TestFilter (.abc.def)\0.abc.def\0", "deadbeef.abc.def");
1117     test_extension_helper(&ofn, "TestFilter (*.*.def)\0*.*.def\0", "deadbeef.xyz");
1118 }
1119 
1120 #undef ARRAY_SIZE
1121 
1122 
1123 static BOOL WINAPI test_null_enum(HWND hwnd, LPARAM lParam)
1124 {
1125     /* Find the textbox and send a filename so IDOK will work.
1126        If the file textbox is empty IDOK will be ignored */
1127     CHAR className[20];
1128     if(GetClassNameA(hwnd, className, sizeof(className)) > 0 && !strcmp("Edit",className))
1129     {
1130         SetWindowTextA(hwnd, "testfile");
1131         return FALSE; /* break window enumeration */
1132     }
1133     return TRUE;
1134 }
1135 
1136 static UINT_PTR WINAPI test_null_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
1137 {
1138     HWND parent = GetParent( dlg);
1139     if( msg == WM_NOTIFY) {
1140         SetTimer( dlg, 0, 100, 0);
1141         SetTimer( dlg, 1, 1000, 0);
1142         EnumChildWindows( parent, test_null_enum, 0);
1143     }
1144     if( msg == WM_TIMER) {
1145         if(!wParam)
1146             PostMessageA( parent, WM_COMMAND, IDOK, 0);
1147         else {
1148             /* the dialog did not close automatically */
1149             KillTimer( dlg, 0);
1150             PostMessageA( parent, WM_COMMAND, IDCANCEL, 0);
1151         }
1152     }
1153     return FALSE;
1154 }
1155 
1156 static void test_null_filename(void)
1157 {
1158     OPENFILENAMEA ofnA = {0};
1159     OPENFILENAMEW ofnW = {0};
1160     WCHAR filterW[] = {'t','e','x','t','\0','*','.','t','x','t','\0',
1161                        'A','l','l','\0','*','\0','\0'};
1162     DWORD ret;
1163 
1164     ofnA.lStructSize = OPENFILENAME_SIZE_VERSION_400A;
1165     ofnA.lpstrFile = NULL;
1166     ofnA.nMaxFile = 0;
1167     ofnA.nFileOffset = 0xdead;
1168     ofnA.nFileExtension = 0xbeef;
1169     ofnA.lpfnHook = test_null_wndproc;
1170     ofnA.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
1171     ofnA.hInstance = GetModuleHandleA(NULL);
1172     ofnA.lpstrFilter = "text\0*.txt\0All\0*\0\0";
1173     ofnA.lpstrDefExt = NULL;
1174     ret = GetOpenFileNameA(&ofnA);
1175     todo_wine ok(ret, "GetOpenFileNameA returned %#x\n", ret);
1176     ret = CommDlgExtendedError();
1177     todo_wine ok(!ret, "CommDlgExtendedError returned %#x, should be 0\n", ret);
1178 
1179     todo_wine ok(ofnA.nFileOffset != 0xdead, "ofnA.nFileOffset is 0xdead\n");
1180     todo_wine ok(ofnA.nFileExtension != 0xbeef, "ofnA.nFileExtension is 0xbeef\n");
1181 
1182     ofnA.lpstrFile = NULL;
1183     ofnA.nMaxFile = 1024; /* bogus input - lpstrFile = NULL but fake 1024 bytes available */
1184     ofnA.nFileOffset = 0xdead;
1185     ofnA.nFileExtension = 0xbeef;
1186     ret = GetOpenFileNameA(&ofnA);
1187     ok(ret, "GetOpenFileNameA returned %#x\n", ret);
1188     ret = CommDlgExtendedError();
1189     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
1190 
1191     ok(ofnA.nFileOffset != 0xdead, "ofnA.nFileOffset is 0xdead\n");
1192     ok(ofnA.nFileExtension == 0, "ofnA.nFileExtension is 0x%x, should be 0\n", ofnA.nFileExtension);
1193 
1194     /* unicode tests */
1195     ofnW.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
1196     ofnW.lpstrFile = NULL;
1197     ofnW.nMaxFile = 0;
1198     ofnW.nFileOffset = 0xdead;
1199     ofnW.nFileExtension = 0xbeef;
1200     ofnW.lpfnHook = test_null_wndproc;
1201     ofnW.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
1202     ofnW.hInstance = GetModuleHandleW(NULL);
1203     ofnW.lpstrFilter = filterW;
1204     ofnW.lpstrDefExt = NULL;
1205     ret = GetOpenFileNameW(&ofnW);
1206     todo_wine ok(ret, "GetOpenFileNameW returned %#x\n", ret);
1207     ret = CommDlgExtendedError();
1208     todo_wine ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
1209 
1210     todo_wine ok(ofnW.nFileOffset != 0xdead, "ofnW.nFileOffset is 0xdead\n");
1211     todo_wine ok(ofnW.nFileExtension != 0xbeef, "ofnW.nFileExtension is 0xbeef\n");
1212 
1213     ofnW.lpstrFile = NULL;
1214     ofnW.nMaxFile = 1024; /* bogus input - lpstrFile = NULL but fake 1024 bytes available */
1215     ofnW.nFileOffset = 0xdead;
1216     ofnW.nFileExtension = 0xbeef;
1217     ret = GetOpenFileNameW(&ofnW);
1218     ok(ret, "GetOpenFileNameA returned %#x\n", ret);
1219     ret = CommDlgExtendedError();
1220     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
1221 
1222     ok(ofnW.nFileOffset != 0xdead, "ofnW.nFileOffset is 0xdead\n");
1223     ok(ofnW.nFileExtension == 0, "ofnW.nFileExtension is 0x%x, should be 0\n", ofnW.nFileExtension);
1224 }
1225 
1226 static void test_directory_filename(void)
1227 {
1228     OPENFILENAMEA ofnA = {0};
1229     OPENFILENAMEW ofnW = {0};
1230     WCHAR filterW[] = {'t','e','x','t','\0','*','.','t','x','t','\0',
1231                        'A','l','l','\0','*','\0','\0'};
1232     char szInitialDir[MAX_PATH] = {0};
1233     WCHAR szInitialDirW[MAX_PATH] = {0};
1234     DWORD ret;
1235 
1236     GetWindowsDirectoryA(szInitialDir, MAX_PATH);
1237     GetWindowsDirectoryW(szInitialDirW, MAX_PATH);
1238 
1239     szInitialDir[strlen(szInitialDir)] = '\\';
1240     szInitialDirW[lstrlenW(szInitialDirW)] = '\\';
1241 
1242     ofnA.lStructSize = OPENFILENAME_SIZE_VERSION_400A;
1243     ofnA.lpstrFile = szInitialDir;
1244     ofnA.nMaxFile = MAX_PATH;
1245     ofnA.lpfnHook = test_null_wndproc;
1246     ofnA.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
1247     ofnA.hInstance = GetModuleHandleA(NULL);
1248     ofnA.lpstrFilter = "text\0*.txt\0All\0*\0\0";
1249     ofnA.lpstrDefExt = NULL;
1250     ret = GetOpenFileNameA(&ofnA);
1251     todo_wine ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
1252 
1253     /* unicode tests */
1254     ofnW.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
1255     ofnW.lpstrFile = szInitialDirW;
1256     ofnW.nMaxFile = MAX_PATH;
1257     ofnW.lpfnHook = test_null_wndproc;
1258     ofnW.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
1259     ofnW.hInstance = GetModuleHandleW(NULL);
1260     ofnW.lpstrFilter = filterW;
1261     ofnW.lpstrDefExt = NULL;
1262     ret = GetOpenFileNameW(&ofnW);
1263     todo_wine ok(!ret, "GetOpenFileNameW returned %#x\n", ret);
1264 }
1265 
1266 static UINT_PTR WINAPI test_ole_init_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
1267 {
1268     HRESULT hr;
1269 
1270     hr = OleInitialize(NULL);
1271     ok(hr == S_FALSE, "OleInitialize() returned %#x\n", hr);
1272     OleUninitialize();
1273 
1274     if (msg == WM_NOTIFY)
1275         PostMessageA(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
1276     return FALSE;
1277 }
1278 
1279 static LRESULT CALLBACK hook_proc(int code, WPARAM wp, LPARAM lp)
1280 {
1281     static BOOL first_dlg = TRUE;
1282     HRESULT hr;
1283 
1284     if (code == HCBT_CREATEWND)
1285     {
1286         CBT_CREATEWNDW *c = (CBT_CREATEWNDW *)lp;
1287 
1288         if (c->lpcs->lpszClass == (LPWSTR)WC_DIALOG)
1289         {
1290             /* OleInitialize() creates a window for the main apartment. Since
1291              * Vista OleInitialize() is called before the file dialog is
1292              * created. SimCity 2000 expects that the first window created
1293              * after GetOpenFileA() is a file dialog window. Mark Vista+
1294              * behavior as broken. */
1295             hr = OleInitialize(NULL);
1296             ok((first_dlg ? hr == S_OK : hr == S_FALSE)
1297                     || broken(first_dlg && hr == S_FALSE),
1298                     "OleInitialize() returned %#x (first dialog %#x)\n", hr, first_dlg);
1299             OleUninitialize();
1300             first_dlg = FALSE;
1301         }
1302     }
1303 
1304     return CallNextHookEx(NULL, code, wp, lp);
1305 }
1306 
1307 static void test_ole_initialization(void)
1308 {
1309     char file[MAX_PATH] = {0};
1310     OPENFILENAMEA ofn = {0};
1311     HRESULT hr;
1312     HHOOK hook;
1313     BOOL ret;
1314 
1315     hook = SetWindowsHookExW(WH_CBT, hook_proc, NULL, GetCurrentThreadId());
1316 
1317     ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400A;
1318     ofn.lpstrFile = file;
1319     ofn.nMaxFile = MAX_PATH;
1320     ofn.lpfnHook = test_ole_init_wndproc;
1321     ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
1322     ofn.hInstance = GetModuleHandleA(NULL);
1323     ret = GetOpenFileNameA(&ofn);
1324     ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
1325 
1326     hr = OleInitialize(NULL);
1327     ok(hr == S_OK, "OleInitialize() returned %#x\n", hr);
1328     OleUninitialize();
1329 
1330     UnhookWindowsHookEx(hook);
1331 }
1332 
1333 START_TEST(filedlg)
1334 {
1335     test_DialogCancel();
1336     test_create_view_window2();
1337     test_create_view_template();
1338     test_arrange();
1339     test_resize();
1340     test_ok();
1341     test_getfolderpath();
1342     test_mru();
1343     if( resizesupported) test_resizable2();
1344     test_extension();
1345     test_null_filename();
1346     test_directory_filename();
1347     test_ole_initialization();
1348 }
1349