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