5f80e4cb15fa88bb81c1cbcf6bcc446800366fd4
[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 if( resize_testcases[index].todo){
407 todo_wine {
408 ok( resize == rc.right - rc.left - initrc.right + initrc.left,
409 "testid %d size-x change %d expected %d\n", index,
410 rc.right - rc.left - initrc.right + initrc.left, resize);
411 ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
412 "testid %d size-y change %d expected %d\n", index,
413 rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
414 }
415 }else{
416 ok( resize == rc.right - rc.left - initrc.right + initrc.left,
417 "testid %d size-x change %d expected %d\n", index,
418 rc.right - rc.left - initrc.right + initrc.left, resize);
419 ok( resize == rc.bottom - rc.top - initrc.bottom + initrc.top,
420 "testid %d size-y change %d expected %d\n", index,
421 rc.bottom - rc.top - initrc.bottom + initrc.top, resize);
422 }
423 if( resize_testcases[index].testcontrols) {
424 int i;
425 RECT rc;
426 for( i = 0; i < nrctrls; i++) {
427 GetWindowRect( ctrls[i], &rc);
428 MapWindowPoints( NULL, parent, (LPPOINT) &rc, 2);
429 switch( ctrlids[i]){
430
431 /* test if RECT R1, moved and sized result in R2 */
432 #define TESTRECTS( R1, R2, Mx, My, Sx, Sy) \
433 ((R1).left + (Mx) ==(R2).left \
434 &&(R1).top + (My) ==(R2).top \
435 &&(R1).right + (Mx) + (Sx) == (R2).right \
436 &&(R1).bottom + (My) + (Sy) ==(R2).bottom)
437
438 /* sized horizontal and moved vertical */
439 case cmb1:
440 case edt1:
441 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 10, 0),
442 "control id %03x should have sized horizontally and moved vertically, before %d,%d-%d,%d after %d,%d-%d,%d\n",
443 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
444 ctrlrcs[i].right, ctrlrcs[i].bottom,
445 rc.left, rc.top, rc.right, rc.bottom);
446 break;
447 /* sized horizontal and vertical */
448 case lst2:
449 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 10),
450 "control id %03x should have sized 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 horizontal and vertical */
456 case IDCANCEL:
457 case pshHelp:
458 ok( TESTRECTS( ctrlrcs[i], rc, 10, 10, 0, 0),
459 "control id %03x should have moved horizontally and vertically, before %d,%d-%d,%d after %d,%d-%d,%d\n",
460 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
461 ctrlrcs[i].right, ctrlrcs[i].bottom,
462 rc.left, rc.top, rc.right, rc.bottom);
463 break;
464 /* moved vertically */
465 case chx1:
466 case stc2:
467 case stc3:
468 ok( TESTRECTS( ctrlrcs[i], rc, 0, 10, 0, 0),
469 "control id %03x should have moved vertically, 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 /* resized horizontal */
475 case cmb2: /* aka IDC_LOOKIN */
476 ok( TESTRECTS( ctrlrcs[i], rc, 0, 0, 10, 0)||
477 TESTRECTS( ctrlrcs[i], rc, 0, 0, 0, 0), /* Vista and higher */
478 "control id %03x should have resized horizontally, before %d,%d-%d,%d after %d,%d-%d,%d\n",
479 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
480 ctrlrcs[i].right, ctrlrcs[i].bottom,
481 rc.left, rc.top, rc.right, rc.bottom);
482 break;
483 /* non moving non sizing controls */
484 case stc4:
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 /* todo_wine: non moving non sizing controls */
492 case lst1:
493 todo_wine
494 ok( TESTRECTS( rc, ctrlrcs[i], 0, 0, 0, 0),
495 "control id %03x was moved/resized, before %d,%d-%d,%d after %d,%d-%d,%d\n",
496 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
497 ctrlrcs[i].right, ctrlrcs[i].bottom,
498 rc.left, rc.top, rc.right, rc.bottom);
499 break;
500 /* don't test: id is not unique */
501 case IDOK:
502 case stc1:
503 case 0:
504 case -1:
505 break;
506 default:
507 trace("untested control id %03x before %d,%d-%d,%d after %d,%d-%d,%d\n",
508 ctrlids[i], ctrlrcs[i].left, ctrlrcs[i].top,
509 ctrlrcs[i].right, ctrlrcs[i].bottom,
510 rc.left, rc.top, rc.right, rc.bottom);
511 #undef TESTRECTS
512 #undef MAXNRCTRLS
513 }
514 }
515 }
516 KillTimer( dlg, 0);
517 PostMessageA( parent, WM_COMMAND, IDCANCEL, 0);
518 }
519 count++;
520 }
521 break;
522 case WM_WINDOWPOSCHANGING:
523 {
524 WINDOWPOS *pwp = (WINDOWPOS *)lParam;
525 if( !index && pwp->hwndInsertAfter == HWND_BOTTOM){
526 gotSWP_bottom = TRUE;
527 ok(!gotShowWindow, "The WM_WINDOWPOSCHANGING message came after a WM_SHOWWINDOW message\n");
528 }
529 }
530 break;
531 case WM_SHOWWINDOW:
532 {
533 if( !index){
534 gotShowWindow = TRUE;
535 ok(gotSWP_bottom, "No WM_WINDOWPOSCHANGING message came before a WM_SHOWWINDOW message\n");
536 }
537 }
538 break;
539 }
540 return 0;
541 }
542
543 static void test_resize(void)
544 {
545 OPENFILENAMEA ofn = { OPENFILENAME_SIZE_VERSION_400A };
546 char filename[1024] = {0};
547 DWORD ret;
548 int i;
549
550 ofn.lpstrFile = filename;
551 ofn.nMaxFile = 1024;
552 ofn.lpfnHook = resize_template_hook;
553 ofn.hInstance = GetModuleHandleA(NULL);
554 ofn.lpTemplateName = "template_sz";
555 for( i = 0; resize_testcases[i].flags != 0xffffffff; i++) {
556 ofn.lCustData = i;
557 ofn.Flags = resize_testcases[i].flags |
558 OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE | OFN_SHOWHELP ;
559 ret = GetOpenFileNameA(&ofn);
560 ok(!ret, "GetOpenFileName returned %#x\n", ret);
561 ret = CommDlgExtendedError();
562 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
563 }
564 }
565
566 /* test cases for control message IDOK */
567 /* Show case for bug #19079 */
568 typedef struct {
569 int retval; /* return code of the message handler */
570 BOOL setmsgresult; /* set the result in the DWLP_MSGRESULT */
571 BOOL usemsgokstr; /* use the FILEOKSTRING message instead of WM_NOTIFY:CDN_FILEOK */
572 BOOL do_subclass; /* subclass the dialog hook procedure */
573 BOOL expclose; /* is the dialog expected to close ? */
574 BOOL actclose; /* has the dialog actually closed ? */
575 } ok_wndproc_testcase;
576
577 static ok_wndproc_testcase ok_testcases[] = {
578 { 0, FALSE, FALSE, FALSE, TRUE},
579 { 0, TRUE, FALSE, FALSE, TRUE},
580 { 0, FALSE, FALSE, TRUE, TRUE},
581 { 0, TRUE, FALSE, TRUE, TRUE},
582 { 1, FALSE, FALSE, FALSE, TRUE},
583 { 1, TRUE, FALSE, FALSE, FALSE},
584 { 1, FALSE, FALSE, TRUE, FALSE},
585 { 1, TRUE, FALSE, TRUE, FALSE},
586 /* FILEOKSTRING tests */
587 { 1, TRUE, TRUE, FALSE, FALSE},
588 { 1, FALSE, TRUE, TRUE, FALSE},
589 /* mark the end */
590 { -1 }
591 };
592
593 /* test_ok_wndproc can be used as hook procedure or a subclass
594 * window proc for the file dialog */
595 static UINT_PTR WINAPI test_ok_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
596 {
597 HWND parent = GetParent( dlg);
598 static ok_wndproc_testcase *testcase = NULL;
599 static UINT msgFILEOKSTRING;
600 if (msg == WM_INITDIALOG)
601 {
602 testcase = (ok_wndproc_testcase*)((OPENFILENAMEA*)lParam)->lCustData;
603 testcase->actclose = TRUE;
604 msgFILEOKSTRING = RegisterWindowMessageA( FILEOKSTRINGA);
605 }
606 if( msg == WM_NOTIFY) {
607 if(((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
608 SetTimer( dlg, 0, 100, 0);
609 PostMessageA( parent, WM_COMMAND, IDOK, 0);
610 return FALSE;
611 } else if(((LPNMHDR)lParam)->code == CDN_FILEOK) {
612 if( testcase->usemsgokstr)
613 return FALSE;
614 if( testcase->setmsgresult)
615 SetWindowLongPtrA( dlg, DWLP_MSGRESULT, testcase->retval);
616 return testcase->retval;
617 }
618 }
619 if( msg == msgFILEOKSTRING) {
620 if( !testcase->usemsgokstr)
621 return FALSE;
622 if( testcase->setmsgresult)
623 SetWindowLongPtrA( dlg, DWLP_MSGRESULT, testcase->retval);
624 return testcase->retval;
625 }
626 if( msg == WM_TIMER) {
627 /* the dialog did not close automatically */
628 testcase->actclose = FALSE;
629 KillTimer( dlg, 0);
630 PostMessageA( parent, WM_COMMAND, IDCANCEL, 0);
631 return FALSE;
632 }
633 if( testcase && testcase->do_subclass)
634 return DefWindowProcA( dlg, msg, wParam, lParam);
635 return FALSE;
636 }
637
638 static UINT_PTR WINAPI ok_template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
639 {
640 if (msg == WM_SETFONT)
641 SetWindowLongPtrA( dlg, GWLP_WNDPROC, (LONG_PTR) test_ok_wndproc);
642 return FALSE;
643 }
644
645 static void test_ok(void)
646 {
647 OPENFILENAMEA ofn = { OPENFILENAME_SIZE_VERSION_400A };
648 char filename[1024] = {0};
649 char tmpfilename[ MAX_PATH];
650 char curdir[MAX_PATH];
651 int i;
652 DWORD ret;
653 BOOL cdret;
654
655 cdret = GetCurrentDirectoryA(sizeof(curdir), curdir);
656 ok(cdret, "Failed to get current dir err %d\n", GetLastError());
657 if (!GetTempFileNameA(".", "txt", 0, tmpfilename)) {
658 skip("Failed to create a temporary file name\n");
659 return;
660 }
661 ofn.lpstrFile = filename;
662 ofn.nMaxFile = 1024;
663 ofn.hInstance = GetModuleHandleA(NULL);
664 ofn.lpTemplateName = "template1";
665 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE ;
666 for( i = 0; ok_testcases[i].retval != -1; i++) {
667 strcpy( filename, tmpfilename);
668 ofn.lCustData = (LPARAM)(ok_testcases + i);
669 ofn.lpfnHook = ok_testcases[i].do_subclass ? ok_template_hook : test_ok_wndproc;
670 ret = GetOpenFileNameA(&ofn);
671 ok( ok_testcases[i].expclose == ok_testcases[i].actclose,
672 "testid %d: Open File dialog should %shave closed.\n", i,
673 ok_testcases[i].expclose ? "" : "NOT ");
674 ok(ret == ok_testcases[i].expclose, "testid %d: GetOpenFileName returned %#x\n", i, ret);
675 ret = CommDlgExtendedError();
676 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
677 cdret = SetCurrentDirectoryA(curdir);
678 ok(cdret, "Failed to restore current dir err %d\n", GetLastError());
679 }
680 ret = DeleteFileA( tmpfilename);
681 ok( ret, "Failed to delete temporary file %s err %d\n", tmpfilename, GetLastError());
682 }
683
684 /* test arranging with a custom template */
685 typedef struct {
686 int x, y; /* left, top coordinates */
687 int cx, cy; /* width and height */
688 } posz;
689 static struct {
690 int nrcontrols; /* 0: no controls, 1: just the stc32 control 2: with button */
691 posz poszDlg;
692 posz poszStc32;
693 posz poszBtn;
694 DWORD ofnflags;
695 } arrange_tests[] = {
696 /* do not change the first two cases: used to get the uncustomized sizes */
697 { 0, {0},{0},{0},0 },
698 { 0, {0},{0},{0}, OFN_SHOWHELP},
699 /* two tests with just a subdialog, no controls */
700 { 0, {0, 0, 316, 76},{0},{0},0 },
701 { 0, {0, 0, 100, 76},{0},{0}, OFN_SHOWHELP},
702 /* now with a control with id stc32 */
703 { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0},0 }, /* bug #17748*/
704 { 1, {0, 0, 316, 76} ,{0, 0, 204, 76,},{0}, OFN_SHOWHELP}, /* bug #17748*/
705 /* tests with size of the stc32 control higher or wider then the standard dialog */
706 { 1, {0, 0, 316, 170} ,{0, 0, 204, 170,},{0},0 },
707 { 1, {0, 0, 316, 165} ,{0, 0, 411, 165,},{0}, OFN_SHOWHELP },
708 /* move the stc32 control around */
709 { 1, {0, 0, 300, 100} ,{73, 17, 50, 50,},{0},0 },
710 /* add control */
711 { 2, {0, 0, 280, 100} ,{0, 0, 50, 50,},{300,20,30,30},0 },
712 /* enable resizing should make the dialog bigger */
713 { 0, {0},{0},{0}, OFN_SHOWHELP|OFN_ENABLESIZING},
714 /* mark the end */
715 { -1 }
716 };
717
718 static UINT_PTR WINAPI template_hook_arrange(HWND dlgChild, UINT msg, WPARAM wParam, LPARAM lParam)
719 {
720 static int index, fixhelp;
721 static posz posz0[2];
722 static RECT clrcParent, clrcChild, rcStc32;
723 static HWND hwndStc32;
724 HWND dlgParent;
725
726 dlgParent = GetParent( dlgChild);
727 if (msg == WM_INITDIALOG) {
728 index = ((OPENFILENAMEA*)lParam)->lCustData;
729 /* get the positions before rearrangement */
730 GetClientRect( dlgParent, &clrcParent);
731 GetClientRect( dlgChild, &clrcChild);
732 hwndStc32 = GetDlgItem( dlgChild, stc32);
733 if( hwndStc32) GetWindowRect( hwndStc32, &rcStc32);
734 }
735 if (msg == WM_NOTIFY && ((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE) {
736 RECT wrcParent;
737
738 GetWindowRect( dlgParent, &wrcParent);
739 /* the fist two "tests" just save the dialogs position, with and without
740 * help button */
741 if( index == 0) {
742 posz0[0].x = wrcParent.left;
743 posz0[0].y = wrcParent.top;
744 posz0[0].cx = wrcParent.right - wrcParent.left;
745 posz0[0].cy = wrcParent.bottom - wrcParent.top;
746 } else if( index == 1) {
747 posz0[1].x = wrcParent.left;
748 posz0[1].y = wrcParent.top;
749 posz0[1].cx = wrcParent.right - wrcParent.left;
750 posz0[1].cy = wrcParent.bottom - wrcParent.top;
751 fixhelp = posz0[1].cy - posz0[0].cy;
752 } else {
753 /* the real tests */
754 int withhelp;
755 int expectx, expecty;
756 DWORD style;
757
758 withhelp = (arrange_tests[index].ofnflags & OFN_SHOWHELP) != 0;
759 GetWindowRect( dlgParent, &wrcParent);
760 if( !hwndStc32) {
761 /* case with no custom subitem with stc32:
762 * default to all custom controls below the standard */
763 expecty = posz0[withhelp].cy + clrcChild.bottom;
764 expectx = posz0[withhelp].cx;
765 } else {
766 /* special case: there is a control with id stc32 */
767 /* expected height */
768 expecty = posz0[withhelp].cy;
769 if( rcStc32.bottom - rcStc32.top + (withhelp ? 0 : fixhelp) > clrcParent.bottom) {
770 expecty += clrcChild.bottom - clrcParent.bottom;
771 if( !withhelp) expecty += fixhelp;
772 }
773 else
774 expecty += clrcChild.bottom - ( rcStc32.bottom - rcStc32.top) ;
775 /* expected width */
776 expectx = posz0[withhelp].cx;
777 if( rcStc32.right - rcStc32.left > clrcParent.right) {
778 expectx += clrcChild.right - clrcParent.right;
779 }
780 else
781 expectx += clrcChild.right - ( rcStc32.right - rcStc32.left) ;
782 }
783 style = GetWindowLongA( dlgParent, GWL_STYLE);
784 if( !(style & WS_SIZEBOX)) {
785 /* without the OFN_ENABLESIZING flag */
786 ok( wrcParent.bottom - wrcParent.top == expecty,
787 "Wrong height of dialog %d, expected %d\n",
788 wrcParent.bottom - wrcParent.top, expecty);
789 ok( wrcParent.right - wrcParent.left == expectx,
790 "Wrong width of dialog %d, expected %d\n",
791 wrcParent.right - wrcParent.left, expectx);
792 } else todo_wine {
793 /* with the OFN_ENABLESIZING flag */
794 ok( wrcParent.bottom - wrcParent.top > expecty,
795 "Wrong height of dialog %d, expected more than %d\n",
796 wrcParent.bottom - wrcParent.top, expecty);
797 ok( wrcParent.right - wrcParent.left > expectx,
798 "Wrong width of dialog %d, expected more than %d\n",
799 wrcParent.right - wrcParent.left, expectx);
800 }
801
802 }
803 PostMessageA( dlgParent, WM_COMMAND, IDCANCEL, 0);
804 }
805 return 0;
806 }
807
808 static void test_arrange(void)
809 {
810 OPENFILENAMEA ofn = {0};
811 char filename[1024] = {0};
812 DWORD ret;
813 HRSRC hRes;
814 HANDLE hDlgTmpl;
815 LPBYTE pv;
816 DLGTEMPLATE *template;
817 DLGITEMTEMPLATE *itemtemplateStc32, *itemtemplateBtn;
818 int i;
819
820 /* load subdialog template into memory */
821 hRes = FindResourceA( GetModuleHandleA(NULL), "template_stc32", (LPSTR)RT_DIALOG);
822 hDlgTmpl = LoadResource( GetModuleHandleA(NULL), hRes );
823 /* get pointers to the structures for the dialog and the controls */
824 pv = LockResource( hDlgTmpl );
825 template = (DLGTEMPLATE*)pv;
826 if( template->x != 11111) {
827 win_skip("could not find the dialog template\n");
828 return;
829 }
830 /* skip dialog template, menu, class and title */
831 pv += sizeof(DLGTEMPLATE);
832 pv += 3 * sizeof(WORD);
833 /* skip font info */
834 while( *(WORD*)pv)
835 pv += sizeof(WORD);
836 pv += sizeof(WORD);
837 /* align on 32 bit boundaries */
838 pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
839 itemtemplateStc32 = (DLGITEMTEMPLATE*)pv;
840 if( itemtemplateStc32->x != 22222) {
841 win_skip("could not find the first item template\n");
842 return;
843 }
844 /* skip itemtemplate, class, title and creation data */
845 pv += sizeof(DLGITEMTEMPLATE);
846 pv += 4 * sizeof(WORD);
847 /* align on 32 bit boundaries */
848 pv = (LPBYTE)(((UINT_PTR)pv + 3 ) & ~3);
849 itemtemplateBtn = (DLGITEMTEMPLATE*)pv;
850 if( itemtemplateBtn->x != 12345) {
851 win_skip("could not find the second item template\n");
852 return;
853 }
854
855 ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400A;
856 ofn.lpstrFile = filename;
857 ofn.nMaxFile = 1024;
858 ofn.lpfnHook = template_hook_arrange;
859 ofn.hInstance = hDlgTmpl;
860 ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
861 for( i = 0; arrange_tests[i].nrcontrols != -1; i++) {
862 ofn.lCustData = i;
863 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATEHANDLE | OFN_HIDEREADONLY |
864 arrange_tests[i].ofnflags;
865 template->cdit = arrange_tests[i].nrcontrols;
866 template->x = arrange_tests[i].poszDlg.x;
867 template->y = arrange_tests[i].poszDlg.y;
868 template->cx = arrange_tests[i].poszDlg.cx;
869 template->cy = arrange_tests[i].poszDlg.cy;
870 itemtemplateStc32->x = arrange_tests[i].poszStc32.x;
871 itemtemplateStc32->y = arrange_tests[i].poszStc32.y;
872 itemtemplateStc32->cx = arrange_tests[i].poszStc32.cx;
873 itemtemplateStc32->cy = arrange_tests[i].poszStc32.cy;
874 itemtemplateBtn->x = arrange_tests[i].poszBtn.x;
875 itemtemplateBtn->y = arrange_tests[i].poszBtn.y;
876 itemtemplateBtn->cx = arrange_tests[i].poszBtn.cx;
877 itemtemplateBtn->cy = arrange_tests[i].poszBtn.cy;
878 ret = GetOpenFileNameA(&ofn);
879 ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
880 ret = CommDlgExtendedError();
881 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
882 }
883 }
884
885 static CHAR SYSDIR[MAX_PATH];
886
887 static UINT_PTR CALLBACK path_hook_proc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
888 {
889 LPNMHDR nmh;
890
891 if( msg == WM_NOTIFY)
892 {
893 nmh = (LPNMHDR) lParam;
894 if( nmh->code == CDN_INITDONE)
895 {
896 PostMessageA( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
897 }
898 else if ( nmh->code == CDN_FOLDERCHANGE)
899 {
900 char buf[1024];
901 int ret;
902
903 memset(buf, 0x66, sizeof(buf));
904 ret = SendMessageA( GetParent(hDlg), CDM_GETFOLDERPATH, sizeof(buf), (LPARAM)buf);
905 ok(!lstrcmpiA(SYSDIR, buf), "Expected '%s', got '%s'\n", SYSDIR, buf);
906 ok(lstrlenA(SYSDIR) + 1 == ret, "Expected %d, got %d\n", lstrlenA(SYSDIR) + 1, ret);
907 }
908 }
909
910 return 0;
911 }
912
913 static void test_getfolderpath(void)
914 {
915 OPENFILENAMEA ofn;
916 BOOL result;
917 char szFileName[MAX_PATH] = "";
918 char szInitialDir[MAX_PATH];
919
920 /* We need to pick a different directory as the other tests because of new
921 * Windows 7 behavior.
922 */
923 GetSystemDirectoryA(szInitialDir, MAX_PATH);
924 lstrcpyA(SYSDIR, szInitialDir);
925
926 ZeroMemory(&ofn, sizeof(ofn));
927
928 ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400A;
929 ofn.hwndOwner = NULL;
930 ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
931 ofn.lpstrFile = szFileName;
932 ofn.nMaxFile = MAX_PATH;
933 ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
934 ofn.lpstrDefExt = "txt";
935 ofn.lpfnHook = path_hook_proc;
936 ofn.lpstrInitialDir = szInitialDir;
937
938 result = GetOpenFileNameA(&ofn);
939 ok(FALSE == result, "expected FALSE, got %d\n", result);
940 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
941 CommDlgExtendedError());
942
943 result = GetSaveFileNameA(&ofn);
944 ok(FALSE == result, "expected FALSE, got %d\n", result);
945 ok(0 == CommDlgExtendedError(), "expected 0, got %d\n",
946 CommDlgExtendedError());
947 }
948
949 static void test_resizable2(void)
950 {
951 OPENFILENAMEA ofn = {0};
952 char filename[1024] = "pls press Enter if sizable, Esc otherwise";
953 DWORD ret;
954
955 /* interactive because there is no hook function */
956 if( !winetest_interactive) {
957 skip( "some interactive resizable dialog tests (set WINETEST_INTERACTIVE=1)\n");
958 return;
959 }
960 ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400A;
961 ofn.lpstrFile = filename;
962 ofn.nMaxFile = 1024;
963 ofn.lpfnHook = NULL;
964 ofn.hInstance = GetModuleHandleA(NULL);
965 ofn.lpTemplateName = "template1";
966 ofn.Flags = OFN_EXPLORER;
967 #define ISSIZABLE TRUE
968 ret = GetOpenFileNameA(&ofn);
969 ok( ret == ISSIZABLE, "File Dialog should have been sizable\n");
970 ret = CommDlgExtendedError();
971 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
972 ofn.Flags = OFN_EXPLORER | OFN_ENABLETEMPLATE;
973 ret = GetOpenFileNameA(&ofn);
974 ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
975 ret = CommDlgExtendedError();
976 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
977 ofn.Flags = OFN_EXPLORER | OFN_ENABLETEMPLATEHANDLE;
978 ofn.hInstance = LoadResource( GetModuleHandleA(NULL), FindResourceA( GetModuleHandleA(NULL), "template1", (LPSTR)RT_DIALOG));
979 ofn.lpTemplateName = NULL;
980 ret = GetOpenFileNameA(&ofn);
981 ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
982 ret = CommDlgExtendedError();
983 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
984 ofn.Flags = OFN_EXPLORER | OFN_ENABLEHOOK;
985 ret = GetOpenFileNameA(&ofn);
986 ok( ret != ISSIZABLE, "File Dialog should NOT have been sizable\n");
987 ret = CommDlgExtendedError();
988 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
989 #undef ISSIZABLE
990 }
991
992 static void test_mru(void)
993 {
994 ok_wndproc_testcase testcase = {0};
995 OPENFILENAMEA ofn = { OPENFILENAME_SIZE_VERSION_400A };
996 const char *test_dir_name = "C:\\mru_test";
997 const char *test_file_name = "test.txt";
998 const char *test_full_path = "C:\\mru_test\\test.txt";
999 char filename_buf[MAX_PATH];
1000 DWORD ret;
1001
1002 ofn.lpstrFile = filename_buf;
1003 ofn.nMaxFile = sizeof(filename_buf);
1004 ofn.lpTemplateName = "template1";
1005 ofn.hInstance = GetModuleHandleA(NULL);
1006 ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER | OFN_ENABLETEMPLATE | OFN_NOCHANGEDIR;
1007 ofn.lCustData = (LPARAM)&testcase;
1008 ofn.lpfnHook = test_ok_wndproc;
1009
1010 SetLastError(0xdeadbeef);
1011 ret = CreateDirectoryA(test_dir_name, NULL);
1012 ok(ret == TRUE, "CreateDirectoryA should have succeeded: %d\n", GetLastError());
1013
1014 /* "teach" comdlg32 about this directory */
1015 strcpy(filename_buf, test_full_path);
1016 SetLastError(0xdeadbeef);
1017 ret = GetOpenFileNameA(&ofn);
1018 ok(ret, "GetOpenFileNameA should have succeeded: %d\n", GetLastError());
1019 ret = CommDlgExtendedError();
1020 ok(!ret, "CommDlgExtendedError returned %x\n", ret);
1021 ok(testcase.actclose, "Open File dialog should have closed.\n");
1022 ok(!strcmp(ofn.lpstrFile, test_full_path), "Expected to get %s, got %s\n", test_full_path, ofn.lpstrFile);
1023
1024 /* get a filename without a full path. it should return the file in
1025 * test_dir_name, not in the CWD */
1026 strcpy(filename_buf, test_file_name);
1027 SetLastError(0xdeadbeef);
1028 ret = GetOpenFileNameA(&ofn);
1029 ok(ret, "GetOpenFileNameA should have succeeded: %d\n", GetLastError());
1030 ret = CommDlgExtendedError();
1031 ok(!ret, "CommDlgExtendedError returned %x\n", ret);
1032 ok(testcase.actclose, "Open File dialog should have closed.\n");
1033 if(strcmp(ofn.lpstrFile, test_full_path) != 0)
1034 win_skip("Platform doesn't save MRU data\n");
1035
1036 SetLastError(0xdeadbeef);
1037 ret = RemoveDirectoryA(test_dir_name);
1038 ok(ret == TRUE, "RemoveDirectoryA should have succeeded: %d\n", GetLastError());
1039 }
1040
1041 static UINT_PTR WINAPI test_extension_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
1042 {
1043 HWND parent = GetParent( dlg);
1044 if( msg == WM_NOTIFY) {
1045 SetTimer( dlg, 0, 1000, 0);
1046 PostMessageA( parent, WM_COMMAND, IDOK, 0);
1047 }
1048 if( msg == WM_TIMER) {
1049 /* the dialog did not close automatically */
1050 KillTimer( dlg, 0);
1051 PostMessageA( parent, WM_COMMAND, IDCANCEL, 0);
1052 }
1053 return FALSE;
1054 }
1055
1056 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
1057
1058 static void test_extension_helper(OPENFILENAMEA* ofn, const char *filter,
1059 const char *expected_filename)
1060 {
1061 char *filename_ptr;
1062 DWORD ret;
1063 BOOL boolret;
1064
1065 strcpy(ofn->lpstrFile, "deadbeef");
1066 ofn->lpstrFilter = filter;
1067
1068 boolret = GetSaveFileNameA(ofn);
1069 ok(boolret, "%s: expected TRUE\n", filter);
1070
1071 ret = CommDlgExtendedError();
1072 ok(!ret, "%s: CommDlgExtendedError returned %#x\n", filter, ret);
1073
1074 filename_ptr = ofn->lpstrFile + ofn->nFileOffset;
1075 ok(strcmp(filename_ptr, expected_filename) == 0,
1076 "%s: Filename is %s, expected %s\n", filter, filename_ptr, expected_filename);
1077 }
1078
1079 static void test_extension(void)
1080 {
1081 OPENFILENAMEA ofn = { OPENFILENAME_SIZE_VERSION_400A };
1082 char filename[1024] = {0};
1083 char curdir[MAX_PATH];
1084 unsigned int i;
1085 BOOL boolret;
1086
1087 const char *defext_concrete_filters[] = {
1088 "TestFilter (*.abc)\0*.abc\0",
1089 "TestFilter (*.abc;)\0*.abc;\0",
1090 "TestFilter (*.abc;*.def)\0*.abc;*.def\0",
1091 };
1092
1093 const char *defext_wildcard_filters[] = {
1094 "TestFilter (*.pt*)\0*.pt*\0",
1095 "TestFilter (*.pt*;*.abc)\0*.pt*;*.abc\0",
1096 "TestFilter (*.ab?)\0*.ab?\0",
1097 "TestFilter (*.*)\0*.*\0",
1098 "TestFilter (*sav)\0*sav\0",
1099 NULL /* is a test, not an endmark! */
1100 };
1101
1102 boolret = GetCurrentDirectoryA(sizeof(curdir), curdir);
1103 ok(boolret, "Failed to get current dir err %d\n", GetLastError());
1104
1105 ofn.hwndOwner = NULL;
1106 ofn.lpstrFile = filename;
1107 ofn.nMaxFile = MAX_PATH;
1108 ofn.Flags = OFN_EXPLORER | OFN_ENABLEHOOK;
1109 ofn.lpstrInitialDir = curdir;
1110 ofn.lpfnHook = test_extension_wndproc;
1111 ofn.nFileExtension = 0;
1112
1113 ofn.lpstrDefExt = NULL;
1114
1115 /* Without lpstrDefExt, append no extension */
1116 test_extension_helper(&ofn, "TestFilter (*.abc) lpstrDefExt=NULL\0*.abc\0", "deadbeef");
1117 test_extension_helper(&ofn, "TestFilter (*.ab?) lpstrDefExt=NULL\0*.ab?\0", "deadbeef");
1118
1119 ofn.lpstrDefExt = "";
1120
1121 /* If lpstrDefExt="" and the filter has a concrete extension, append it */
1122 test_extension_helper(&ofn, "TestFilter (*.abc) lpstrDefExt=\"\"\0*.abc\0", "deadbeef.abc");
1123
1124 /* If lpstrDefExt="" and the filter has a wildcard extension, do nothing */
1125 test_extension_helper(&ofn, "TestFilter (*.ab?) lpstrDefExt=\"\"\0*.ab?\0", "deadbeef");
1126
1127 ofn.lpstrDefExt = "xyz";
1128
1129 /* Append concrete extensions from filters */
1130 for (i = 0; i < ARRAY_SIZE(defext_concrete_filters); i++) {
1131 test_extension_helper(&ofn, defext_concrete_filters[i], "deadbeef.abc");
1132 }
1133
1134 /* Append nothing from this filter */
1135 test_extension_helper(&ofn, "TestFilter (*.)\0*.\0", "deadbeef");
1136
1137 /* Ignore wildcard extensions in filters */
1138 for (i = 0; i < ARRAY_SIZE(defext_wildcard_filters); i++) {
1139 test_extension_helper(&ofn, defext_wildcard_filters[i], "deadbeef.xyz");
1140 }
1141
1142 /* Append valid extensions consisting of multiple parts */
1143 test_extension_helper(&ofn, "TestFilter (*.abc.def)\0*.abc.def\0", "deadbeef.abc.def");
1144 test_extension_helper(&ofn, "TestFilter (.abc.def)\0.abc.def\0", "deadbeef.abc.def");
1145 test_extension_helper(&ofn, "TestFilter (*.*.def)\0*.*.def\0", "deadbeef.xyz");
1146 }
1147
1148 #undef ARRAY_SIZE
1149
1150
1151 static BOOL WINAPI test_null_enum(HWND hwnd, LPARAM lParam)
1152 {
1153 /* Find the textbox and send a filename so IDOK will work.
1154 If the file textbox is empty IDOK will be ignored */
1155 CHAR className[20];
1156 if(GetClassNameA(hwnd, className, sizeof(className)) > 0 && !strcmp("Edit",className))
1157 {
1158 SetWindowTextA(hwnd, "testfile");
1159 return FALSE; /* break window enumeration */
1160 }
1161 return TRUE;
1162 }
1163
1164 static UINT_PTR WINAPI test_null_wndproc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
1165 {
1166 HWND parent = GetParent( dlg);
1167 if( msg == WM_NOTIFY) {
1168 SetTimer( dlg, 0, 100, 0);
1169 SetTimer( dlg, 1, 1000, 0);
1170 EnumChildWindows( parent, test_null_enum, 0);
1171 }
1172 if( msg == WM_TIMER) {
1173 if(!wParam)
1174 PostMessageA( parent, WM_COMMAND, IDOK, 0);
1175 else {
1176 /* the dialog did not close automatically */
1177 KillTimer( dlg, 0);
1178 PostMessageA( parent, WM_COMMAND, IDCANCEL, 0);
1179 }
1180 }
1181 return FALSE;
1182 }
1183
1184 static void test_null_filename(void)
1185 {
1186 OPENFILENAMEA ofnA = {0};
1187 OPENFILENAMEW ofnW = {0};
1188 WCHAR filterW[] = {'t','e','x','t','\0','*','.','t','x','t','\0',
1189 'A','l','l','\0','*','\0','\0'};
1190 DWORD ret;
1191
1192 ofnA.lStructSize = OPENFILENAME_SIZE_VERSION_400A;
1193 ofnA.lpstrFile = NULL;
1194 ofnA.nMaxFile = 0;
1195 ofnA.nFileOffset = 0xdead;
1196 ofnA.nFileExtension = 0xbeef;
1197 ofnA.lpfnHook = test_null_wndproc;
1198 ofnA.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
1199 ofnA.hInstance = GetModuleHandleA(NULL);
1200 ofnA.lpstrFilter = "text\0*.txt\0All\0*\0\0";
1201 ofnA.lpstrDefExt = NULL;
1202 ret = GetOpenFileNameA(&ofnA);
1203 todo_wine ok(ret, "GetOpenFileNameA returned %#x\n", ret);
1204 ret = CommDlgExtendedError();
1205 todo_wine ok(!ret, "CommDlgExtendedError returned %#x, should be 0\n", ret);
1206
1207 todo_wine ok(ofnA.nFileOffset != 0xdead, "ofnA.nFileOffset is 0xdead\n");
1208 todo_wine ok(ofnA.nFileExtension != 0xbeef, "ofnA.nFileExtension is 0xbeef\n");
1209
1210 ofnA.lpstrFile = NULL;
1211 ofnA.nMaxFile = 1024; /* bogus input - lpstrFile = NULL but fake 1024 bytes available */
1212 ofnA.nFileOffset = 0xdead;
1213 ofnA.nFileExtension = 0xbeef;
1214 ret = GetOpenFileNameA(&ofnA);
1215 ok(ret, "GetOpenFileNameA returned %#x\n", ret);
1216 ret = CommDlgExtendedError();
1217 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
1218
1219 ok(ofnA.nFileOffset != 0xdead, "ofnA.nFileOffset is 0xdead\n");
1220 ok(ofnA.nFileExtension == 0, "ofnA.nFileExtension is 0x%x, should be 0\n", ofnA.nFileExtension);
1221
1222 /* unicode tests */
1223 ofnW.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
1224 ofnW.lpstrFile = NULL;
1225 ofnW.nMaxFile = 0;
1226 ofnW.nFileOffset = 0xdead;
1227 ofnW.nFileExtension = 0xbeef;
1228 ofnW.lpfnHook = test_null_wndproc;
1229 ofnW.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
1230 ofnW.hInstance = GetModuleHandleW(NULL);
1231 ofnW.lpstrFilter = filterW;
1232 ofnW.lpstrDefExt = NULL;
1233 ret = GetOpenFileNameW(&ofnW);
1234 todo_wine ok(ret, "GetOpenFileNameW returned %#x\n", ret);
1235 ret = CommDlgExtendedError();
1236 todo_wine ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
1237
1238 todo_wine ok(ofnW.nFileOffset != 0xdead, "ofnW.nFileOffset is 0xdead\n");
1239 todo_wine ok(ofnW.nFileExtension != 0xbeef, "ofnW.nFileExtension is 0xbeef\n");
1240
1241 ofnW.lpstrFile = NULL;
1242 ofnW.nMaxFile = 1024; /* bogus input - lpstrFile = NULL but fake 1024 bytes available */
1243 ofnW.nFileOffset = 0xdead;
1244 ofnW.nFileExtension = 0xbeef;
1245 ret = GetOpenFileNameW(&ofnW);
1246 ok(ret, "GetOpenFileNameA returned %#x\n", ret);
1247 ret = CommDlgExtendedError();
1248 ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
1249
1250 ok(ofnW.nFileOffset != 0xdead, "ofnW.nFileOffset is 0xdead\n");
1251 ok(ofnW.nFileExtension == 0, "ofnW.nFileExtension is 0x%x, should be 0\n", ofnW.nFileExtension);
1252 }
1253
1254 START_TEST(filedlg)
1255 {
1256 test_DialogCancel();
1257 test_create_view_window2();
1258 test_create_view_template();
1259 test_arrange();
1260 test_resize();
1261 test_ok();
1262 test_getfolderpath();
1263 test_mru();
1264 if( resizesupported) test_resizable2();
1265 test_extension();
1266 test_null_filename();
1267 }