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
23 #include <wine/test.h>
32 static UINT_PTR CALLBACK OFNHookProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
38 nmh = (LPNMHDR) lParam;
39 if( nmh->code == CDN_INITDONE)
41 PostMessage( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
42 } else if (nmh->code == CDN_FOLDERCHANGE )
47 memset(buf, 0x66, sizeof(buf));
48 ret = SendMessage( GetParent(hDlg), CDM_GETFOLDERIDLIST, 5, (LPARAM)buf);
49 ok(ret > 0, "CMD_GETFOLDERIDLIST not implemented\n");
51 ok(buf[0] == 0x66 && buf[1] == 0x66, "CMD_GETFOLDERIDLIST: The buffer was touched on failure\n");
59 static void test_DialogCancel(void)
63 char szFileName[MAX_PATH] = "";
64 char szInitialDir[MAX_PATH];
66 GetWindowsDirectory(szInitialDir, MAX_PATH);
68 ZeroMemory(&ofn, sizeof(ofn));
70 ofn.lStructSize = sizeof(ofn);
72 ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
73 ofn.lpstrFile = szFileName;
74 ofn.nMaxFile = MAX_PATH;
75 ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
76 ofn.lpstrDefExt = "txt";
77 ofn.lpfnHook = OFNHookProc;
78 ofn.lpstrInitialDir = szInitialDir;
81 ok(CDERR_INITIALIZATION == CommDlgExtendedError(), "expected %d, got %d\n",
82 CDERR_INITIALIZATION, CommDlgExtendedError());
84 result = GetOpenFileNameA(&ofn);
85 ok(0 == result, "expected %d, got %d\n", 0, result);
86 ok(0 == CommDlgExtendedError(), "expected %d, got %d\n", 0,
87 CommDlgExtendedError());
90 ok(CDERR_INITIALIZATION == CommDlgExtendedError(), "expected %d, got %d\n",
91 CDERR_INITIALIZATION, CommDlgExtendedError());
93 result = GetSaveFileNameA(&ofn);
94 ok(0 == result, "expected %d, got %d\n", 0, result);
95 ok(0 == CommDlgExtendedError(), "expected %d, got %d\n", 0,
96 CommDlgExtendedError());
99 ok(CDERR_INITIALIZATION == CommDlgExtendedError(), "expected %d, got %d\n",
100 CDERR_INITIALIZATION, CommDlgExtendedError());
102 /* Before passing the ofn to Unicode functions, remove the ANSI strings */
103 ofn.lpstrFilter = NULL;
104 ofn.lpstrInitialDir = NULL;
105 ofn.lpstrDefExt = NULL;
108 ok(CDERR_INITIALIZATION == CommDlgExtendedError(), "expected %d, got %d\n",
109 CDERR_INITIALIZATION, CommDlgExtendedError());
111 SetLastError(0xdeadbeef);
112 result = GetOpenFileNameW((LPOPENFILENAMEW) &ofn);
113 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
114 win_skip("GetOpenFileNameW is not implemented\n");
117 ok(0 == result, "expected %d, got %d\n", 0, result);
118 ok(0 == CommDlgExtendedError() ||
119 CDERR_INITIALIZATION == CommDlgExtendedError(), /* win9x */
120 "expected %d or %d, got %d\n", 0, CDERR_INITIALIZATION,
121 CommDlgExtendedError());
124 SetLastError(0xdeadbeef);
125 result = GetSaveFileNameW((LPOPENFILENAMEW) &ofn);
126 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
127 win_skip("GetSaveFileNameW is not implemented\n");
130 ok(0 == result, "expected %d, got %d\n", 0, result);
131 ok(0 == CommDlgExtendedError() ||
132 CDERR_INITIALIZATION == CommDlgExtendedError(), /* win9x */
133 "expected %d or %d, got %d\n", 0, CDERR_INITIALIZATION,
134 CommDlgExtendedError());
138 static UINT_PTR CALLBACK create_view_window2_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
140 if (msg == WM_NOTIFY)
142 if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
144 IShellBrowser *shell_browser = (IShellBrowser *)SendMessage(GetParent(dlg), WM_USER + 7 /* WM_GETISHELLBROWSER */, 0, 0);
145 IShellView *shell_view = NULL;
146 IShellView2 *shell_view2 = NULL;
147 SV2CVW2_PARAMS view_params;
148 FOLDERSETTINGS folder_settings;
150 RECT rect = {0, 0, 0, 0};
152 hr = IShellBrowser_QueryActiveShellView(shell_browser, &shell_view);
153 ok(SUCCEEDED(hr), "QueryActiveShellView returned %#x\n", hr);
154 if (FAILED(hr)) goto cleanup;
156 hr = IShellView_QueryInterface(shell_view, &IID_IShellView2, (void **)&shell_view2);
157 if (hr == E_NOINTERFACE)
159 win_skip("IShellView2 not supported\n");
162 ok(SUCCEEDED(hr), "QueryInterface returned %#x\n", hr);
163 if (FAILED(hr)) goto cleanup;
165 hr = IShellView2_DestroyViewWindow(shell_view2);
166 ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
168 folder_settings.ViewMode = FVM_LIST;
169 folder_settings.fFlags = 0;
171 view_params.cbSize = sizeof(view_params);
172 view_params.psvPrev = NULL;
173 view_params.pfs = &folder_settings;
174 view_params.psbOwner = shell_browser;
175 view_params.prcView = ▭
176 view_params.pvid = NULL;
177 view_params.hwndView = NULL;
179 hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
180 ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
181 if (FAILED(hr)) goto cleanup;
183 hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
184 ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
185 ok(folder_settings.ViewMode == FVM_LIST, "view mode is %d, expected %d\n", folder_settings.ViewMode, FVM_LIST);
187 hr = IShellView2_DestroyViewWindow(shell_view2);
188 ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
190 /* XP and W2K3 need this. On Win9x and W2K the call to DestroyWindow() fails and has
191 * no side effects. NT4 doesn't get here. (FIXME: Vista doesn't get here yet).
193 DestroyWindow(view_params.hwndView);
195 view_params.pvid = &VID_Details;
196 hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
197 ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
198 if (FAILED(hr)) goto cleanup;
200 hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
201 ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
202 ok(folder_settings.ViewMode == FVM_DETAILS ||
203 broken(folder_settings.ViewMode == FVM_LIST), /* Win9x */
204 "view mode is %d, expected %d\n", folder_settings.ViewMode, FVM_DETAILS);
207 if (shell_view2) IShellView2_Release(shell_view2);
208 if (shell_view) IShellView_Release(shell_view);
209 PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
215 static LONG_PTR WINAPI template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
217 if (msg == WM_INITDIALOG)
222 ok(p!=NULL, "Failed to get parent of template\n");
223 cb = GetDlgItem(p,0x470);
224 ok(cb!=NULL, "Failed to get filter combobox\n");
225 sel = SendMessage(cb, CB_GETCURSEL, 0, 0);
226 ok (sel != -1, "Failed to get selection from filter listbox\n");
228 if (msg == WM_NOTIFY)
230 if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
231 PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
236 static void test_create_view_window2(void)
238 OPENFILENAMEA ofn = {0};
239 char filename[1024] = {0};
242 ofn.lStructSize = sizeof(ofn);
243 ofn.lpstrFile = filename;
245 ofn.lpfnHook = create_view_window2_hook;
246 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
247 ret = GetOpenFileNameA(&ofn);
248 ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
249 ret = CommDlgExtendedError();
250 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
253 static void test_create_view_template(void)
255 OPENFILENAMEA ofn = {0};
256 char filename[1024] = {0};
259 ofn.lStructSize = sizeof(ofn);
260 ofn.lpstrFile = filename;
262 ofn.lpfnHook = (LPOFNHOOKPROC)template_hook;
263 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE;
264 ofn.hInstance = GetModuleHandleA(NULL);
265 ofn.lpTemplateName = "template1";
266 ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
267 ret = GetOpenFileNameA(&ofn);
268 ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
269 ret = CommDlgExtendedError();
270 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
273 /* test cases for resizing of the file dialog */
276 int resize_init; /* change in CDN_INITDONE handler */
277 int resize_folderchg; /* change in CDN_FOLDERCHANGE handler */
278 int resize_timer1; /* change in first WM_TIMER handler */
279 int resize_check; /* expected change (in second WM_TIMER handler) */
280 BOOL todo; /* mark that test todo_wine */
281 } resize_testcases[] = {
282 { 0 , 10, 10, 10, 30,FALSE}, /* 0 */
283 { 0 ,-10,-10,-10,-30,FALSE},
284 { OFN_ENABLESIZING , 0, 0, 0, 0,FALSE},
285 { OFN_ENABLESIZING , 0, 0,-10, 0,FALSE},
286 { OFN_ENABLESIZING , 0, 0, 10, 10,FALSE},
287 { OFN_ENABLESIZING , 0,-10, 0, 10, TRUE}, /* 5 */
288 { OFN_ENABLESIZING , 0, 10, 0, 10,FALSE},
289 { OFN_ENABLESIZING ,-10, 0, 0, 10, TRUE},
290 { OFN_ENABLESIZING , 10, 0, 0, 10,FALSE},
295 static LONG_PTR WINAPI resize_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
297 static RECT initrc, rc;
298 static int index, count;
299 static int gotSWP_bottom, gotShowWindow;
300 HWND parent = GetParent( dlg);
308 index = ((OPENFILENAME*)lParam)->lCustData;
310 gotSWP_bottom = gotShowWindow = 0;
312 style = GetWindowLong( parent, GWL_STYLE);
313 if( resize_testcases[index].flags & OFN_ENABLESIZING)
314 if( !(style & WS_SIZEBOX)) {
315 win_skip( "OFN_ENABLESIZING flag not supported.\n");
316 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
318 ok( style & WS_SIZEBOX,
319 "testid %d: dialog should have a WS_SIZEBOX style.\n", index);
321 ok( !(style & WS_SIZEBOX),
322 "testid %d: dialog should not have a WS_SIZEBOX style.\n", index);
327 if(( (LPNMHDR)lParam)->code == CDN_INITDONE){
328 GetWindowRect( parent, &initrc);
329 if( (resize = resize_testcases[index].resize_init)){
330 MoveWindow( parent, initrc.left,initrc.top, initrc.right - initrc.left + resize,
331 initrc.bottom - initrc.top + resize, TRUE);
333 } else if(( (LPNMHDR)lParam)->code == CDN_FOLDERCHANGE){
334 if( (resize = resize_testcases[index].resize_folderchg)){
335 GetWindowRect( parent, &rc);
336 MoveWindow( parent, rc.left,rc.top, rc.right - rc.left + resize,
337 rc.bottom - rc.top + resize, TRUE);
339 SetTimer( dlg, 0, 100, 0);
346 if( (resize = resize_testcases[index].resize_timer1)){
347 GetWindowRect( parent, &rc);
348 MoveWindow( parent, rc.left,rc.top, rc.right - rc.left + resize,
349 rc.bottom - rc.top + resize, TRUE);
351 } else if( count == 1){
352 resize = resize_testcases[index].resize_check;
353 GetWindowRect( parent, &rc);
354 if( resize_testcases[index].todo){
356 ok( resize == rc.right - rc.left - initrc.right + initrc.left,
357 "testid %d size-x change %d expected %d\n", index,
358 rc.right - rc.left - initrc.right + initrc.left, resize);
359 ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
360 "testid %d size-y change %d expected %d\n", index,
361 rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
364 ok( resize == rc.right - rc.left - initrc.right + initrc.left,
365 "testid %d size-x change %d expected %d\n", index,
366 rc.right - rc.left - initrc.right + initrc.left, resize);
367 ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
368 "testid %d size-y change %d expected %d\n", index,
369 rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
372 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
377 case WM_WINDOWPOSCHANGING:
379 WINDOWPOS *pwp = (WINDOWPOS *)lParam;
380 if( !index && pwp->hwndInsertAfter == HWND_BOTTOM){
382 ok( gotShowWindow == 0, "The WM_WINDOWPOSCHANGING message came after a WM_SHOWWINDOW message\n");
390 ok( gotSWP_bottom == 1, "No WM_WINDOWPOSCHANGING message came before a WM_SHOWWINDOW message\n");
398 static void test_resize(void)
400 OPENFILENAME ofn = { sizeof(OPENFILENAME)};
401 char filename[1024] = {0};
405 ofn.lpstrFile = filename;
407 ofn.lpfnHook = (LPOFNHOOKPROC) resize_template_hook;
408 ofn.hInstance = GetModuleHandle(NULL);
409 ofn.lpTemplateName = "template_sz";
410 for( i = 0; resize_testcases[i].flags != 0xffffffff; i++) {
412 ofn.Flags = resize_testcases[i].flags |
413 OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE ;
414 ret = GetOpenFileName(&ofn);
415 ok(!ret, "GetOpenFileName returned %#x\n", ret);
416 ret = CommDlgExtendedError();
417 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
421 /* test cases for control message IDOK */
422 /* Show case for bug #19079 */
424 int retval; /* return code of the message handler */
425 BOOL setmsgresult; /* set the result in the DWLP_MSGRESULT */
426 BOOL usemsgokstr; /* use the FILEOKSTRING message instead of WM_NOTIFY:CDN_FILEOK */
427 BOOL do_subclass; /* subclass the dialog hook procedure */
428 BOOL expclose; /* is the dialog expected to close ? */
429 BOOL actclose; /* has the dialog actually closed ? */
431 { 0, FALSE, FALSE, FALSE, TRUE},
432 { 0, TRUE, FALSE, FALSE, TRUE},
433 { 0, FALSE, FALSE, TRUE, TRUE},
434 { 0, TRUE, FALSE, TRUE, TRUE},
435 { 1, FALSE, FALSE, FALSE, TRUE},
436 { 1, TRUE, FALSE, FALSE, FALSE},
437 { 1, FALSE, FALSE, TRUE, FALSE},
438 { 1, TRUE, FALSE, TRUE, FALSE},
439 /* FILEOKSTRING tests */
440 { 1, TRUE, TRUE, FALSE, FALSE},
441 { 1, FALSE, TRUE, TRUE, FALSE},
446 /* test_ok_wndproc can be used as hook procedure or a subclass
447 * window proc for the file dialog */
448 static LONG_PTR WINAPI test_ok_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
450 HWND parent = GetParent( dlg);
452 static UINT msgFILEOKSTRING;
453 if (msg == WM_INITDIALOG)
455 index = ((OPENFILENAME*)lParam)->lCustData;
456 ok_testcases[index].actclose = TRUE;
457 msgFILEOKSTRING = RegisterWindowMessageA( FILEOKSTRING);
459 if( msg == WM_NOTIFY) {
460 if(((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
461 SetTimer( dlg, 0, 100, 0);
462 PostMessage( parent, WM_COMMAND, IDOK, 0);
464 } else if(((LPNMHDR)lParam)->code == CDN_FILEOK) {
465 if( ok_testcases[index].usemsgokstr)
467 if( ok_testcases[index].setmsgresult)
468 SetWindowLongPtrA( dlg, DWLP_MSGRESULT, ok_testcases[index].retval);
469 return ok_testcases[index].retval;
472 if( msg == msgFILEOKSTRING) {
473 if( !ok_testcases[index].usemsgokstr)
475 if( ok_testcases[index].setmsgresult)
476 SetWindowLongPtrA( dlg, DWLP_MSGRESULT, ok_testcases[index].retval);
477 return ok_testcases[index].retval;
479 if( msg == WM_TIMER) {
480 /* the dialog did not close automatically */
481 ok_testcases[index].actclose = FALSE;
483 PostMessage( parent, WM_COMMAND, IDCANCEL, 0);
486 if( ok_testcases[index].do_subclass)
487 return DefWindowProc( dlg, msg, wParam, lParam);
491 static LONG_PTR WINAPI ok_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
493 if (msg == WM_SETFONT)
494 SetWindowLongPtrA( dlg, GWLP_WNDPROC, (LONG_PTR) test_ok_wndproc);
498 static void test_ok(void)
500 OPENFILENAME ofn = { sizeof(OPENFILENAME)};
501 char filename[1024] = {0};
502 char tmpfilename[ MAX_PATH];
506 if (!GetTempFileNameA(".", "tmp", 0, tmpfilename)) {
507 skip("Failed to create a temporary file name\n");
510 ofn.lpstrFile = filename;
512 ofn.hInstance = GetModuleHandle(NULL);
513 ofn.lpTemplateName = "template1";
514 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE ;
515 for( i = 0; ok_testcases[i].retval != -1; i++) {
516 strcpy( filename, tmpfilename);
518 ofn.lpfnHook = ok_testcases[i].do_subclass
519 ? (LPOFNHOOKPROC) ok_template_hook
520 : (LPOFNHOOKPROC) test_ok_wndproc;
521 ret = GetOpenFileNameA(&ofn);
522 ok( ok_testcases[i].expclose == ok_testcases[i].actclose,
523 "testid %d: Open File dialog should %shave closed.\n", i,
524 ok_testcases[i].expclose ? "" : "NOT ");
525 ok(ret == ok_testcases[i].expclose, "testid %d: GetOpenFileName returned %#x\n", i, ret);
526 ret = CommDlgExtendedError();
527 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
529 ok( DeleteFileA( tmpfilename), "Failed to delete temporary file\n");
535 test_create_view_window2();
536 test_create_view_template();