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