msvcrt: NULL terminate program arguments list in __getmainargs.
[wine] / dlls / comdlg32 / tests / itemdlg.c
1 /*
2  * Unit tests for the Common Item Dialog
3  *
4  * Copyright 2010,2011 David Hedberg
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 #define COBJMACROS
23 #define CONST_VTABLE
24
25 #include "shlobj.h"
26 #include "wine/test.h"
27
28 static HRESULT (WINAPI *pSHCreateShellItem)(LPCITEMIDLIST,IShellFolder*,LPCITEMIDLIST,IShellItem**);
29 static HRESULT (WINAPI *pSHGetIDListFromObject)(IUnknown*, PIDLIST_ABSOLUTE*);
30 static HRESULT (WINAPI *pSHCreateItemFromParsingName)(PCWSTR,IBindCtx*,REFIID,void**);
31
32 static void init_function_pointers(void)
33 {
34     HMODULE hmod = GetModuleHandleA("shell32.dll");
35
36 #define MAKEFUNC(f) (p##f = (void*)GetProcAddress(hmod, #f))
37     MAKEFUNC(SHCreateShellItem);
38     MAKEFUNC(SHGetIDListFromObject);
39     MAKEFUNC(SHCreateItemFromParsingName);
40 #undef MAKEFUNC
41 }
42
43 #include <initguid.h>
44 DEFINE_GUID(IID_IFileDialogCustomizeAlt, 0x8016B7B3, 0x3D49, 0x4504, 0xA0,0xAA, 0x2A,0x37,0x49,0x4E,0x60,0x6F);
45
46 /**************************************************************************
47  * IFileDialogEvents implementation
48  */
49 typedef struct {
50     IFileDialogEvents IFileDialogEvents_iface;
51     LONG ref;
52     LONG QueryInterface;
53     LONG OnFileOk, OnFolderChanging, OnFolderChange;
54     LONG OnSelectionChange, OnShareViolation, OnTypeChange;
55     LONG OnOverwrite;
56     LPCWSTR set_filename;
57     BOOL set_filename_tried;
58     BOOL cfd_test1;
59 } IFileDialogEventsImpl;
60
61 static inline IFileDialogEventsImpl *impl_from_IFileDialogEvents(IFileDialogEvents *iface)
62 {
63     return CONTAINING_RECORD(iface, IFileDialogEventsImpl, IFileDialogEvents_iface);
64 }
65
66 static HRESULT WINAPI IFileDialogEvents_fnQueryInterface(IFileDialogEvents *iface, REFIID riid, void **ppv)
67 {
68     /* Not called. */
69     ok(0, "Unexpectedly called.\n");
70     return E_NOINTERFACE;
71 }
72
73 static ULONG WINAPI IFileDialogEvents_fnAddRef(IFileDialogEvents *iface)
74 {
75     IFileDialogEventsImpl *This = impl_from_IFileDialogEvents(iface);
76     return InterlockedIncrement(&This->ref);
77 }
78
79 static ULONG WINAPI IFileDialogEvents_fnRelease(IFileDialogEvents *iface)
80 {
81     IFileDialogEventsImpl *This = impl_from_IFileDialogEvents(iface);
82     LONG ref = InterlockedDecrement(&This->ref);
83
84     if(!ref)
85         HeapFree(GetProcessHeap(), 0, This);
86
87     return ref;
88 }
89
90 static HRESULT WINAPI IFileDialogEvents_fnOnFileOk(IFileDialogEvents *iface, IFileDialog *pfd)
91 {
92     IFileDialogEventsImpl *This = impl_from_IFileDialogEvents(iface);
93     This->OnFileOk++;
94     return S_OK;
95 }
96
97 static HRESULT WINAPI IFileDialogEvents_fnOnFolderChanging(IFileDialogEvents *iface,
98                                                            IFileDialog *pfd,
99                                                            IShellItem *psiFolder)
100 {
101     IFileDialogEventsImpl *This = impl_from_IFileDialogEvents(iface);
102     This->OnFolderChanging++;
103     return S_OK;
104 }
105
106 static void test_customize_onfolderchange(IFileDialog *pfd);
107
108 static LRESULT CALLBACK test_customize_dlgproc(HWND hwnd, UINT message, LPARAM lparam, WPARAM wparam)
109 {
110     WNDPROC oldwndproc = GetPropA(hwnd, "WT_OLDWC");
111
112     if(message == WM_USER+0x1234)
113     {
114         IFileDialog *pfd = (IFileDialog*)lparam;
115         test_customize_onfolderchange(pfd);
116     }
117
118     return CallWindowProcW(oldwndproc, hwnd, message, lparam, wparam);
119 }
120
121 static HRESULT WINAPI IFileDialogEvents_fnOnFolderChange(IFileDialogEvents *iface, IFileDialog *pfd)
122 {
123     IFileDialogEventsImpl *This = impl_from_IFileDialogEvents(iface);
124     IOleWindow *pow;
125     HWND dlg_hwnd;
126     HRESULT hr;
127     BOOL br;
128     This->OnFolderChange++;
129
130     if(This->set_filename)
131     {
132         hr = IFileDialog_QueryInterface(pfd, &IID_IOleWindow, (void**)&pow);
133         ok(hr == S_OK, "Got 0x%08x\n", hr);
134
135         hr = IOleWindow_GetWindow(pow, &dlg_hwnd);
136         ok(hr == S_OK, "Got 0x%08x\n", hr);
137         ok(dlg_hwnd != NULL, "Got NULL.\n");
138
139         IOleWindow_Release(pow);
140
141         hr = IFileDialog_SetFileName(pfd, This->set_filename);
142         ok(hr == S_OK, "Got 0x%08x\n", hr);
143
144         if(!This->set_filename_tried)
145         {
146             br = PostMessageW(dlg_hwnd, WM_COMMAND, IDOK, 0);
147             ok(br, "Failed\n");
148             This->set_filename_tried = TRUE;
149         }
150     }
151
152     if(This->cfd_test1)
153     {
154         WNDPROC oldwndproc;
155         hr = IFileDialog_QueryInterface(pfd, &IID_IOleWindow, (void**)&pow);
156         ok(hr == S_OK, "Got 0x%08x\n", hr);
157
158         hr = IOleWindow_GetWindow(pow, &dlg_hwnd);
159         ok(hr == S_OK, "Got 0x%08x\n", hr);
160         ok(dlg_hwnd != NULL, "Got NULL.\n");
161
162         IOleWindow_Release(pow);
163
164         /* On Vista, the custom control area of the dialog is not
165          * fully set up when the first OnFolderChange event is
166          * issued. */
167         oldwndproc = (WNDPROC)GetWindowLongPtrW(dlg_hwnd, GWLP_WNDPROC);
168         SetPropA(dlg_hwnd, "WT_OLDWC", (HANDLE)oldwndproc);
169         SetWindowLongPtrW(dlg_hwnd, GWLP_WNDPROC, (LPARAM)test_customize_dlgproc);
170
171         br = PostMessageW(dlg_hwnd, WM_USER+0x1234, (LPARAM)pfd, 0);
172         ok(br, "Failed\n");
173     }
174
175     return S_OK;
176 }
177
178 static HRESULT WINAPI IFileDialogEvents_fnOnSelectionChange(IFileDialogEvents *iface, IFileDialog *pfd)
179 {
180     IFileDialogEventsImpl *This = impl_from_IFileDialogEvents(iface);
181     This->OnSelectionChange++;
182     return S_OK;
183 }
184
185 static HRESULT WINAPI IFileDialogEvents_fnOnShareViolation(IFileDialogEvents *iface,
186                                                            IFileDialog *pfd,
187                                                            IShellItem *psi,
188                                                            FDE_SHAREVIOLATION_RESPONSE *pResponse)
189 {
190     IFileDialogEventsImpl *This = impl_from_IFileDialogEvents(iface);
191     This->OnShareViolation++;
192     return S_OK;
193 }
194
195 static HRESULT WINAPI IFileDialogEvents_fnOnTypeChange(IFileDialogEvents *iface, IFileDialog *pfd)
196 {
197     IFileDialogEventsImpl *This = impl_from_IFileDialogEvents(iface);
198     This->OnTypeChange++;
199     return S_OK;
200 }
201
202 static HRESULT WINAPI IFileDialogEvents_fnOnOverwrite(IFileDialogEvents *iface,
203                                                       IFileDialog *pfd,
204                                                       IShellItem *psi,
205                                                       FDE_OVERWRITE_RESPONSE *pResponse)
206 {
207     IFileDialogEventsImpl *This = impl_from_IFileDialogEvents(iface);
208     This->OnOverwrite++;
209     return S_OK;
210 }
211
212 static const IFileDialogEventsVtbl vt_IFileDialogEvents = {
213     IFileDialogEvents_fnQueryInterface,
214     IFileDialogEvents_fnAddRef,
215     IFileDialogEvents_fnRelease,
216     IFileDialogEvents_fnOnFileOk,
217     IFileDialogEvents_fnOnFolderChanging,
218     IFileDialogEvents_fnOnFolderChange,
219     IFileDialogEvents_fnOnSelectionChange,
220     IFileDialogEvents_fnOnShareViolation,
221     IFileDialogEvents_fnOnTypeChange,
222     IFileDialogEvents_fnOnOverwrite
223 };
224
225 static IFileDialogEvents *IFileDialogEvents_Constructor(void)
226 {
227     IFileDialogEventsImpl *This;
228
229     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IFileDialogEventsImpl));
230     This->IFileDialogEvents_iface.lpVtbl = &vt_IFileDialogEvents;
231     This->ref = 1;
232
233     return &This->IFileDialogEvents_iface;
234 }
235
236 static BOOL test_instantiation(void)
237 {
238     IFileDialog *pfd;
239     IFileOpenDialog *pfod;
240     IFileSaveDialog *pfsd;
241     IServiceProvider *psp;
242     IOleWindow *pow;
243     IUnknown *punk;
244     HRESULT hr;
245     LONG ref;
246
247     /* Instantiate FileOpenDialog */
248     hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER,
249                           &IID_IFileOpenDialog, (void**)&pfod);
250     if(FAILED(hr))
251     {
252         skip("Could not instantiate the FileOpenDialog.\n");
253         return FALSE;
254     }
255     ok(hr == S_OK, "got 0x%08x.\n", hr);
256
257     hr = IFileOpenDialog_QueryInterface(pfod, &IID_IFileDialog, (void**)&pfd);
258     ok(hr == S_OK, "got 0x%08x.\n", hr);
259     if(SUCCEEDED(hr)) IFileDialog_Release(pfd);
260
261     hr = IFileOpenDialog_QueryInterface(pfod, &IID_IFileDialogCustomize, (void**)&punk);
262     ok(hr == S_OK, "got 0x%08x.\n", hr);
263     if(SUCCEEDED(hr)) IUnknown_Release(punk);
264
265     hr = IFileOpenDialog_QueryInterface(pfod, &IID_IFileDialogCustomizeAlt, (void**)&punk);
266     ok(hr == S_OK, "got 0x%08x.\n", hr);
267     if(SUCCEEDED(hr)) IUnknown_Release(punk);
268
269     hr = IFileOpenDialog_QueryInterface(pfod, &IID_IFileSaveDialog, (void**)&pfsd);
270     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
271     if(SUCCEEDED(hr)) IFileSaveDialog_Release(pfsd);
272
273     hr = IFileOpenDialog_QueryInterface(pfod, &IID_IServiceProvider, (void**)&psp);
274     ok(hr == S_OK, "got 0x%08x.\n", hr);
275     if(SUCCEEDED(hr))
276     {
277         IExplorerBrowser *peb;
278         IShellBrowser *psb;
279
280         hr = IServiceProvider_QueryService(psp, &SID_SExplorerBrowserFrame, &IID_ICommDlgBrowser, (void**)&punk);
281         ok(hr == S_OK, "got 0x%08x.\n", hr);
282         if(SUCCEEDED(hr)) IUnknown_Release(punk);
283
284         /* since win8, the result is E_NOTIMPL for all other services */
285         hr = IServiceProvider_QueryService(psp, &SID_STopLevelBrowser, &IID_IExplorerBrowser, (void**)&peb);
286         ok(hr == E_NOTIMPL || broken(hr == E_FAIL), "got 0x%08x (expected E_NOTIMPL)\n", hr);
287         if(SUCCEEDED(hr)) IExplorerBrowser_Release(peb);
288         hr = IServiceProvider_QueryService(psp, &SID_STopLevelBrowser, &IID_IShellBrowser, (void**)&psb);
289         ok(hr == E_NOTIMPL || broken(hr == E_FAIL), "got 0x%08x (expected E_NOTIMPL)\n", hr);
290         if(SUCCEEDED(hr)) IShellBrowser_Release(psb);
291         hr = IServiceProvider_QueryService(psp, &SID_STopLevelBrowser, &IID_ICommDlgBrowser, (void**)&punk);
292         ok(hr == E_NOTIMPL || broken(hr == E_FAIL), "got 0x%08x (expected E_NOTIMPL)\n", hr);
293         if(SUCCEEDED(hr)) IUnknown_Release(punk);
294
295         hr = IServiceProvider_QueryService(psp, &SID_STopLevelBrowser, &IID_IUnknown, (void**)&punk);
296         ok(hr == E_NOTIMPL || broken(hr == E_FAIL), "got 0x%08x (expected E_NOTIMPL)\n", hr);
297         if(SUCCEEDED(hr)) IUnknown_Release(punk);
298         hr = IServiceProvider_QueryService(psp, &IID_IUnknown, &IID_IUnknown, (void**)&punk);
299         ok(hr == E_NOTIMPL || broken(hr == E_FAIL), "got 0x%08x (expected E_NOTIMPL)\n", hr);
300         if(SUCCEEDED(hr)) IUnknown_Release(punk);
301
302         IServiceProvider_Release(psp);
303     }
304
305     hr = IFileOpenDialog_QueryInterface(pfod, &IID_IFileDialogEvents, (void**)&punk);
306     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
307     if(SUCCEEDED(hr)) IUnknown_Release(punk);
308
309     hr = IFileOpenDialog_QueryInterface(pfod, &IID_IExplorerBrowser, (void**)&punk);
310     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
311     if(SUCCEEDED(hr)) IUnknown_Release(punk);
312
313     hr = IFileOpenDialog_QueryInterface(pfod, &IID_IExplorerBrowserEvents, (void**)&punk);
314     ok(hr == S_OK, "got 0x%08x.\n", hr);
315     if(SUCCEEDED(hr)) IUnknown_Release(punk);
316
317     hr = IFileOpenDialog_QueryInterface(pfod, &IID_ICommDlgBrowser3, (void**)&punk);
318     ok(hr == S_OK, "got 0x%08x.\n", hr);
319     if(SUCCEEDED(hr)) IUnknown_Release(punk);
320
321     hr = IFileOpenDialog_QueryInterface(pfod, &IID_IShellBrowser, (void**)&punk);
322     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
323     if(SUCCEEDED(hr)) IUnknown_Release(punk);
324
325     hr = IFileOpenDialog_QueryInterface(pfod, &IID_IOleWindow, (void**)&pow);
326     ok(hr == S_OK, "got 0x%08x.\n", hr);
327     if(SUCCEEDED(hr))
328     {
329         HWND hwnd;
330
331         hr = IOleWindow_ContextSensitiveHelp(pow, TRUE);
332         todo_wine ok(hr == S_OK, "Got 0x%08x\n", hr);
333
334         hr = IOleWindow_ContextSensitiveHelp(pow, FALSE);
335         todo_wine ok(hr == S_OK, "Got 0x%08x\n", hr);
336
337         if(0)
338         {
339             /* Crashes on win7 */
340             IOleWindow_GetWindow(pow, NULL);
341         }
342
343         hr = IOleWindow_GetWindow(pow, &hwnd);
344         ok(hr == S_OK, "Got 0x%08x\n", hr);
345         ok(hwnd == NULL, "Got %p\n", hwnd);
346
347         IOleWindow_Release(pow);
348     }
349
350     ref = IFileOpenDialog_Release(pfod);
351     ok(!ref, "Got refcount %d, should have been released.\n", ref);
352
353     /* Instantiate FileSaveDialog */
354     hr = CoCreateInstance(&CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER,
355                           &IID_IFileSaveDialog, (void**)&pfsd);
356     if(FAILED(hr))
357     {
358         skip("Could not instantiate the FileSaveDialog.\n");
359         return FALSE;
360     }
361     ok(hr == S_OK, "got 0x%08x.\n", hr);
362
363     hr = IFileSaveDialog_QueryInterface(pfsd, &IID_IFileDialog, (void**)&pfd);
364     ok(hr == S_OK, "got 0x%08x.\n", hr);
365     if(SUCCEEDED(hr)) IFileDialog_Release(pfd);
366
367     hr = IFileSaveDialog_QueryInterface(pfsd, &IID_IFileDialogCustomize, (void**)&punk);
368     ok(hr == S_OK, "got 0x%08x.\n", hr);
369     if(SUCCEEDED(hr)) IUnknown_Release(punk);
370
371     hr = IFileSaveDialog_QueryInterface(pfsd, &IID_IFileDialogCustomizeAlt, (void**)&punk);
372     ok(hr == S_OK, "got 0x%08x.\n", hr);
373     if(SUCCEEDED(hr)) IUnknown_Release(punk);
374
375     hr = IFileSaveDialog_QueryInterface(pfsd, &IID_IFileOpenDialog, (void**)&pfod);
376     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
377     if(SUCCEEDED(hr)) IFileOpenDialog_Release(pfod);
378
379     hr = IFileSaveDialog_QueryInterface(pfsd, &IID_IFileDialogEvents, (void**)&punk);
380     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
381     if(SUCCEEDED(hr)) IFileDialog_Release(pfd);
382
383     hr = IFileSaveDialog_QueryInterface(pfsd, &IID_IExplorerBrowser, (void**)&punk);
384     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
385     if(SUCCEEDED(hr)) IUnknown_Release(punk);
386
387     hr = IFileSaveDialog_QueryInterface(pfsd, &IID_IExplorerBrowserEvents, (void**)&punk);
388     ok(hr == S_OK, "got 0x%08x.\n", hr);
389     if(SUCCEEDED(hr)) IUnknown_Release(punk);
390
391     hr = IFileSaveDialog_QueryInterface(pfsd, &IID_ICommDlgBrowser3, (void**)&punk);
392     ok(hr == S_OK, "got 0x%08x.\n", hr);
393     if(SUCCEEDED(hr)) IUnknown_Release(punk);
394
395     hr = IFileSaveDialog_QueryInterface(pfsd, &IID_IShellBrowser, (void**)&punk);
396     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
397     if(SUCCEEDED(hr)) IUnknown_Release(punk);
398
399     hr = IFileSaveDialog_QueryInterface(pfsd, &IID_IOleWindow, (void**)&pow);
400     ok(hr == S_OK, "got 0x%08x.\n", hr);
401     if(SUCCEEDED(hr))
402     {
403         HWND hwnd;
404
405         hr = IOleWindow_ContextSensitiveHelp(pow, TRUE);
406         todo_wine ok(hr == S_OK, "Got 0x%08x\n", hr);
407
408         hr = IOleWindow_ContextSensitiveHelp(pow, FALSE);
409         todo_wine ok(hr == S_OK, "Got 0x%08x\n", hr);
410
411         if(0)
412         {
413             /* Crashes on win7 */
414             IOleWindow_GetWindow(pow, NULL);
415         }
416
417         hr = IOleWindow_GetWindow(pow, &hwnd);
418         ok(hr == S_OK, "Got 0x%08x\n", hr);
419         ok(hwnd == NULL, "Got %p\n", hwnd);
420
421         IOleWindow_Release(pow);
422     }
423
424
425     ref = IFileSaveDialog_Release(pfsd);
426     ok(!ref, "Got refcount %d, should have been released.\n", ref);
427     return TRUE;
428 }
429
430 static void test_basics(void)
431 {
432     IFileOpenDialog *pfod;
433     IFileSaveDialog *pfsd;
434     IFileDialog2 *pfd2;
435     FILEOPENDIALOGOPTIONS fdoptions;
436     IShellFolder *psfdesktop;
437     IShellItem *psi, *psidesktop, *psi_original;
438     IShellItemArray *psia;
439     IPropertyStore *pps;
440     LPITEMIDLIST pidl;
441     WCHAR *filename;
442     UINT filetype;
443     LONG ref;
444     HRESULT hr;
445     const WCHAR txt[] = {'t','x','t', 0};
446     const WCHAR null[] = {0};
447     const WCHAR fname1[] = {'f','n','a','m','e','1', 0};
448     const WCHAR fspec1[] = {'*','.','t','x','t',0};
449     const WCHAR fname2[] = {'f','n','a','m','e','2', 0};
450     const WCHAR fspec2[] = {'*','.','e','x','e',0};
451     COMDLG_FILTERSPEC filterspec[2] = {{fname1, fspec1}, {fname2, fspec2}};
452
453     /* This should work on every platform with IFileDialog */
454     SHGetDesktopFolder(&psfdesktop);
455     hr = pSHGetIDListFromObject((IUnknown*)psfdesktop, &pidl);
456     if(SUCCEEDED(hr))
457     {
458         hr = pSHCreateShellItem(NULL, NULL, pidl, &psidesktop);
459         ILFree(pidl);
460     }
461     IShellFolder_Release(psfdesktop);
462     if(FAILED(hr))
463     {
464         skip("Failed to get ShellItem from DesktopFolder, skipping tests.\n");
465         return;
466     }
467
468
469     /* Instantiate FileOpenDialog and FileSaveDialog */
470     hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER,
471                           &IID_IFileOpenDialog, (void**)&pfod);
472     ok(hr == S_OK, "got 0x%08x.\n", hr);
473     hr = CoCreateInstance(&CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER,
474                           &IID_IFileSaveDialog, (void**)&pfsd);
475     ok(hr == S_OK, "got 0x%08x.\n", hr);
476
477     /* ClearClientData */
478     todo_wine
479     {
480     hr = IFileOpenDialog_ClearClientData(pfod);
481     ok(hr == E_FAIL, "got 0x%08x.\n", hr);
482     hr = IFileSaveDialog_ClearClientData(pfsd);
483     ok(hr == E_FAIL, "got 0x%08x.\n", hr);
484     }
485
486     /* GetOptions */
487     hr = IFileOpenDialog_GetOptions(pfod, NULL);
488     ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
489     hr = IFileSaveDialog_GetOptions(pfsd, NULL);
490     ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
491
492     /* Check default options */
493     hr = IFileOpenDialog_GetOptions(pfod, &fdoptions);
494     ok(hr == S_OK, "got 0x%08x.\n", hr);
495     ok(fdoptions == (FOS_PATHMUSTEXIST | FOS_FILEMUSTEXIST | FOS_NOCHANGEDIR),
496        "Unexpected default options: 0x%08x\n", fdoptions);
497     hr = IFileSaveDialog_GetOptions(pfsd, &fdoptions);
498     ok(hr == S_OK, "got 0x%08x.\n", hr);
499     ok(fdoptions == (FOS_OVERWRITEPROMPT | FOS_NOREADONLYRETURN | FOS_PATHMUSTEXIST | FOS_NOCHANGEDIR),
500        "Unexpected default options: 0x%08x\n", fdoptions);
501
502     /* GetResult */
503     hr = IFileOpenDialog_GetResult(pfod, NULL);
504     ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
505     hr = IFileSaveDialog_GetResult(pfsd, NULL);
506     ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
507
508     psi = (void*)0xdeadbeef;
509     hr = IFileOpenDialog_GetResult(pfod, &psi);
510     ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
511     ok(psi == (void*)0xdeadbeef, "got %p.\n", psi);
512     psi = (void*)0xdeadbeef;
513     hr = IFileSaveDialog_GetResult(pfsd, &psi);
514     ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
515     ok(psi == (void*)0xdeadbeef, "got %p.\n", psi);
516
517     /* GetCurrentSelection */
518     if(0) {
519         /* Crashes on Vista/W2K8. Tests below passes on Windows 7 */
520         hr = IFileOpenDialog_GetCurrentSelection(pfod, NULL);
521         ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
522         hr = IFileSaveDialog_GetCurrentSelection(pfsd, NULL);
523         ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
524         hr = IFileOpenDialog_GetCurrentSelection(pfod, &psi);
525         ok(hr == E_FAIL, "got 0x%08x.\n", hr);
526         hr = IFileSaveDialog_GetCurrentSelection(pfsd, &psi);
527         ok(hr == E_FAIL, "got 0x%08x.\n", hr);
528     }
529
530     /* GetFileName */
531     hr = IFileOpenDialog_GetFileName(pfod, NULL);
532     ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
533     filename = (void*)0xdeadbeef;
534     hr = IFileOpenDialog_GetFileName(pfod, &filename);
535     ok(hr == E_FAIL, "got 0x%08x.\n", hr);
536     ok(filename == NULL, "got %p\n", filename);
537     hr = IFileSaveDialog_GetFileName(pfsd, NULL);
538     ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
539     filename = (void*)0xdeadbeef;
540     hr = IFileSaveDialog_GetFileName(pfsd, &filename);
541     ok(hr == E_FAIL, "got 0x%08x.\n", hr);
542     ok(filename == NULL, "got %p\n", filename);
543
544     /* GetFileTypeIndex */
545     hr = IFileOpenDialog_GetFileTypeIndex(pfod, NULL);
546     ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
547     filetype = 0x12345;
548     hr = IFileOpenDialog_GetFileTypeIndex(pfod, &filetype);
549     ok(hr == S_OK, "got 0x%08x.\n", hr);
550     ok(filetype == 0, "got %d.\n", filetype);
551     hr = IFileSaveDialog_GetFileTypeIndex(pfsd, NULL);
552     ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
553     filetype = 0x12345;
554     hr = IFileSaveDialog_GetFileTypeIndex(pfsd, &filetype);
555     ok(hr == S_OK, "got 0x%08x.\n", hr);
556     ok(filetype == 0, "got %d.\n", filetype);
557
558     /* SetFileTypes / SetFileTypeIndex */
559     hr = IFileOpenDialog_SetFileTypes(pfod, 0, NULL);
560     ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
561     hr = IFileOpenDialog_SetFileTypes(pfod, 0, filterspec);
562     ok(hr == S_OK, "got 0x%08x.\n", hr);
563
564     hr = IFileOpenDialog_SetFileTypeIndex(pfod, -1);
565     ok(hr == E_FAIL, "got 0x%08x.\n", hr);
566     hr = IFileOpenDialog_SetFileTypeIndex(pfod, 0);
567     ok(hr == E_FAIL, "got 0x%08x.\n", hr);
568     hr = IFileOpenDialog_SetFileTypeIndex(pfod, 1);
569     ok(hr == E_FAIL, "got 0x%08x.\n", hr);
570     hr = IFileOpenDialog_SetFileTypes(pfod, 1, filterspec);
571     ok(hr == S_OK, "got 0x%08x.\n", hr);
572     hr = IFileOpenDialog_SetFileTypes(pfod, 0, filterspec);
573     ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
574     hr = IFileOpenDialog_SetFileTypeIndex(pfod, 0);
575     ok(hr == S_OK, "got 0x%08x.\n", hr);
576     hr = IFileOpenDialog_SetFileTypeIndex(pfod, 100);
577     ok(hr == S_OK, "got 0x%08x.\n", hr);
578     hr = IFileOpenDialog_SetFileTypes(pfod, 1, filterspec);
579     ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
580     hr = IFileOpenDialog_SetFileTypes(pfod, 1, &filterspec[1]);
581     ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
582
583     hr = IFileSaveDialog_SetFileTypeIndex(pfsd, -1);
584     ok(hr == E_FAIL, "got 0x%08x.\n", hr);
585     hr = IFileSaveDialog_SetFileTypeIndex(pfsd, 0);
586     ok(hr == E_FAIL, "got 0x%08x.\n", hr);
587     hr = IFileSaveDialog_SetFileTypeIndex(pfsd, 1);
588     ok(hr == E_FAIL, "got 0x%08x.\n", hr);
589     hr = IFileSaveDialog_SetFileTypes(pfsd, 1, filterspec);
590     ok(hr == S_OK, "got 0x%08x.\n", hr);
591     hr = IFileSaveDialog_SetFileTypeIndex(pfsd, 0);
592     ok(hr == S_OK, "got 0x%08x.\n", hr);
593     hr = IFileSaveDialog_SetFileTypeIndex(pfsd, 100);
594     ok(hr == S_OK, "got 0x%08x.\n", hr);
595     hr = IFileSaveDialog_SetFileTypes(pfsd, 1, filterspec);
596     ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
597     hr = IFileSaveDialog_SetFileTypes(pfsd, 1, &filterspec[1]);
598     ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
599
600     /* SetFilter */
601     todo_wine
602     {
603     hr = IFileOpenDialog_SetFilter(pfod, NULL);
604     ok(hr == S_OK, "got 0x%08x.\n", hr);
605     hr = IFileSaveDialog_SetFilter(pfsd, NULL);
606     ok(hr == S_OK, "got 0x%08x.\n", hr);
607     }
608
609     /* SetFolder */
610     hr = IFileOpenDialog_SetFolder(pfod, NULL);
611     ok(hr == S_OK, "got 0x%08x.\n", hr);
612     hr = IFileSaveDialog_SetFolder(pfsd, NULL);
613     ok(hr == S_OK, "got 0x%08x.\n", hr);
614
615     /* SetDefaultExtension */
616     hr = IFileOpenDialog_SetDefaultExtension(pfod, NULL);
617     ok(hr == S_OK, "got 0x%08x.\n", hr);
618     hr = IFileOpenDialog_SetDefaultExtension(pfod, txt);
619     ok(hr == S_OK, "got 0x%08x.\n", hr);
620     hr = IFileOpenDialog_SetDefaultExtension(pfod, null);
621     ok(hr == S_OK, "got 0x%08x.\n", hr);
622
623     hr = IFileSaveDialog_SetDefaultExtension(pfsd, NULL);
624     ok(hr == S_OK, "got 0x%08x.\n", hr);
625     hr = IFileSaveDialog_SetDefaultExtension(pfsd, txt);
626     ok(hr == S_OK, "got 0x%08x.\n", hr);
627     hr = IFileSaveDialog_SetDefaultExtension(pfsd, null);
628     ok(hr == S_OK, "got 0x%08x.\n", hr);
629
630     /* SetDefaultFolder */
631     hr = IFileOpenDialog_SetDefaultFolder(pfod, NULL);
632     ok(hr == S_OK, "got 0x%08x\n", hr);
633     hr = IFileSaveDialog_SetDefaultFolder(pfsd, NULL);
634     ok(hr == S_OK, "got 0x%08x\n", hr);
635
636     hr = IFileOpenDialog_SetDefaultFolder(pfod, psidesktop);
637     ok(hr == S_OK, "got 0x%08x\n", hr);
638     hr = IFileSaveDialog_SetDefaultFolder(pfsd, psidesktop);
639     ok(hr == S_OK, "got 0x%08x\n", hr);
640
641     if(0)
642     {
643         /* Crashes under Windows 7 */
644         IFileOpenDialog_SetDefaultFolder(pfod, (void*)0x1234);
645         IFileSaveDialog_SetDefaultFolder(pfsd, (void*)0x1234);
646     }
647
648     /* GetFolder / SetFolder */
649     hr = IFileOpenDialog_GetFolder(pfod, NULL);
650     ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
651
652     hr = IFileOpenDialog_GetFolder(pfod, &psi_original);
653     ok(hr == S_OK, "got 0x%08x.\n", hr);
654     if(SUCCEEDED(hr))
655     {
656         hr = IFileOpenDialog_SetFolder(pfod, psidesktop);
657         ok(hr == S_OK, "got 0x%08x.\n", hr);
658         hr = IFileOpenDialog_SetFolder(pfod, psi_original);
659         ok(hr == S_OK, "got 0x%08x.\n", hr);
660         IShellItem_Release(psi_original);
661     }
662
663     hr = IFileSaveDialog_GetFolder(pfsd, &psi_original);
664     ok(hr == S_OK, "got 0x%08x.\n", hr);
665     if(SUCCEEDED(hr))
666     {
667         hr = IFileSaveDialog_SetFolder(pfsd, psidesktop);
668         ok(hr == S_OK, "got 0x%08x.\n", hr);
669         hr = IFileSaveDialog_SetFolder(pfsd, psi_original);
670         ok(hr == S_OK, "got 0x%08x.\n", hr);
671         IShellItem_Release(psi_original);
672     }
673
674     /* AddPlace */
675     if(0)
676     {
677         /* Crashes under Windows 7 */
678         IFileOpenDialog_AddPlace(pfod, NULL, 0);
679         IFileSaveDialog_AddPlace(pfsd, NULL, 0);
680     }
681
682     todo_wine
683     {
684     hr = IFileOpenDialog_AddPlace(pfod, psidesktop, FDAP_TOP + 1);
685     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
686     hr = IFileOpenDialog_AddPlace(pfod, psidesktop, FDAP_BOTTOM);
687     ok(hr == S_OK, "got 0x%08x\n", hr);
688     hr = IFileOpenDialog_AddPlace(pfod, psidesktop, FDAP_TOP);
689     ok(hr == S_OK, "got 0x%08x\n", hr);
690
691     hr = IFileSaveDialog_AddPlace(pfsd, psidesktop, FDAP_TOP + 1);
692     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
693     hr = IFileSaveDialog_AddPlace(pfsd, psidesktop, FDAP_BOTTOM);
694     ok(hr == S_OK, "got 0x%08x\n", hr);
695     hr = IFileSaveDialog_AddPlace(pfsd, psidesktop, FDAP_TOP);
696     ok(hr == S_OK, "got 0x%08x\n", hr);
697     }
698
699     /* SetFileName */
700     hr = IFileOpenDialog_SetFileName(pfod, NULL);
701     ok(hr == S_OK, "got 0x%08x\n", hr);
702     hr = IFileOpenDialog_SetFileName(pfod, null);
703     ok(hr == S_OK, "got 0x%08x\n", hr);
704     hr = IFileOpenDialog_SetFileName(pfod, txt);
705     ok(hr == S_OK, "got 0x%08x\n", hr);
706     hr = IFileOpenDialog_GetFileName(pfod, &filename);
707     ok(hr == S_OK, "Got 0x%08x\n", hr);
708     ok(!lstrcmpW(filename, txt), "Strings do not match.\n");
709     CoTaskMemFree(filename);
710
711     hr = IFileSaveDialog_SetFileName(pfsd, NULL);
712     ok(hr == S_OK, "got 0x%08x\n", hr);
713     hr = IFileSaveDialog_SetFileName(pfsd, null);
714     ok(hr == S_OK, "got 0x%08x\n", hr);
715     hr = IFileSaveDialog_SetFileName(pfsd, txt);
716     ok(hr == S_OK, "got 0x%08x\n", hr);
717     hr = IFileSaveDialog_GetFileName(pfsd, &filename);
718     ok(hr == S_OK, "Got 0x%08x\n", hr);
719     ok(!lstrcmpW(filename, txt), "Strings do not match.\n");
720     CoTaskMemFree(filename);
721
722     /* SetFileNameLabel */
723     hr = IFileOpenDialog_SetFileNameLabel(pfod, NULL);
724     ok(hr == S_OK, "got 0x%08x\n", hr);
725     hr = IFileOpenDialog_SetFileNameLabel(pfod, null);
726     ok(hr == S_OK, "got 0x%08x\n", hr);
727     hr = IFileOpenDialog_SetFileNameLabel(pfod, txt);
728     ok(hr == S_OK, "got 0x%08x\n", hr);
729
730     hr = IFileSaveDialog_SetFileNameLabel(pfsd, NULL);
731     ok(hr == S_OK, "got 0x%08x\n", hr);
732     hr = IFileSaveDialog_SetFileNameLabel(pfsd, null);
733     ok(hr == S_OK, "got 0x%08x\n", hr);
734     hr = IFileSaveDialog_SetFileNameLabel(pfsd, txt);
735     ok(hr == S_OK, "got 0x%08x\n", hr);
736
737     /* Close */
738     hr = IFileOpenDialog_Close(pfod, S_FALSE);
739     ok(hr == S_OK, "got 0x%08x\n", hr);
740     hr = IFileSaveDialog_Close(pfsd, S_FALSE);
741     ok(hr == S_OK, "got 0x%08x\n", hr);
742
743     /* SetOkButtonLabel */
744     hr = IFileOpenDialog_SetOkButtonLabel(pfod, NULL);
745     ok(hr == S_OK, "got 0x%08x\n", hr);
746     hr = IFileOpenDialog_SetOkButtonLabel(pfod, null);
747     ok(hr == S_OK, "got 0x%08x\n", hr);
748     hr = IFileOpenDialog_SetOkButtonLabel(pfod, txt);
749     ok(hr == S_OK, "got 0x%08x\n", hr);
750     hr = IFileSaveDialog_SetOkButtonLabel(pfsd, NULL);
751     ok(hr == S_OK, "got 0x%08x\n", hr);
752     hr = IFileSaveDialog_SetOkButtonLabel(pfsd, null);
753     ok(hr == S_OK, "got 0x%08x\n", hr);
754     hr = IFileSaveDialog_SetOkButtonLabel(pfsd, txt);
755     ok(hr == S_OK, "got 0x%08x\n", hr);
756
757     /* SetTitle */
758     hr = IFileOpenDialog_SetTitle(pfod, NULL);
759     ok(hr == S_OK, "got 0x%08x\n", hr);
760     hr = IFileOpenDialog_SetTitle(pfod, null);
761     ok(hr == S_OK, "got 0x%08x\n", hr);
762     hr = IFileOpenDialog_SetTitle(pfod, txt);
763     ok(hr == S_OK, "got 0x%08x\n", hr);
764     hr = IFileSaveDialog_SetTitle(pfsd, NULL);
765     ok(hr == S_OK, "got 0x%08x\n", hr);
766     hr = IFileSaveDialog_SetTitle(pfsd, null);
767     ok(hr == S_OK, "got 0x%08x\n", hr);
768     hr = IFileSaveDialog_SetTitle(pfsd, txt);
769     ok(hr == S_OK, "got 0x%08x\n", hr);
770
771     /** IFileOpenDialog specific **/
772
773     /* GetResults */
774     if(0)
775     {
776         /* Crashes under Windows 7 */
777         IFileOpenDialog_GetResults(pfod, NULL);
778     }
779     psia = (void*)0xdeadbeef;
780     hr = IFileOpenDialog_GetResults(pfod, &psia);
781     ok(hr == E_FAIL, "got 0x%08x.\n", hr);
782     ok(psia == NULL, "got %p.\n", psia);
783
784     /* GetSelectedItems */
785     if(0)
786     {
787         /* Crashes under W2K8 */
788         hr = IFileOpenDialog_GetSelectedItems(pfod, NULL);
789         ok(hr == E_FAIL, "got 0x%08x.\n", hr);
790         psia = (void*)0xdeadbeef;
791         hr = IFileOpenDialog_GetSelectedItems(pfod, &psia);
792         ok(hr == E_FAIL, "got 0x%08x.\n", hr);
793         ok(psia == (void*)0xdeadbeef, "got %p.\n", psia);
794     }
795
796     /** IFileSaveDialog specific **/
797
798     /* ApplyProperties */
799     if(0)
800     {
801         /* Crashes under windows 7 */
802         IFileSaveDialog_ApplyProperties(pfsd, NULL, NULL, NULL, NULL);
803         IFileSaveDialog_ApplyProperties(pfsd, psidesktop, NULL, NULL, NULL);
804     }
805
806     /* GetProperties */
807     hr = IFileSaveDialog_GetProperties(pfsd, NULL);
808     todo_wine ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr);
809     pps = (void*)0xdeadbeef;
810     hr = IFileSaveDialog_GetProperties(pfsd, &pps);
811     todo_wine ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr);
812     ok(pps == (void*)0xdeadbeef, "got %p\n", pps);
813
814     /* SetProperties */
815     if(0)
816     {
817         /* Crashes under W2K8 */
818         hr = IFileSaveDialog_SetProperties(pfsd, NULL);
819         ok(hr == S_OK, "got 0x%08x\n", hr);
820     }
821
822     /* SetCollectedProperties */
823     todo_wine
824     {
825     hr = IFileSaveDialog_SetCollectedProperties(pfsd, NULL, TRUE);
826     ok(hr == S_OK, "got 0x%08x\n", hr);
827     hr = IFileSaveDialog_SetCollectedProperties(pfsd, NULL, FALSE);
828     ok(hr == S_OK, "got 0x%08x\n", hr);
829     }
830
831     /* SetSaveAsItem */
832     todo_wine
833     {
834     hr = IFileSaveDialog_SetSaveAsItem(pfsd, NULL);
835     ok(hr == S_OK, "got 0x%08x\n", hr);
836     hr = IFileSaveDialog_SetSaveAsItem(pfsd, psidesktop);
837     ok(hr == MK_E_NOOBJECT, "got 0x%08x\n", hr);
838     }
839
840     /** IFileDialog2 **/
841
842     hr = IFileOpenDialog_QueryInterface(pfod, &IID_IFileDialog2, (void**)&pfd2);
843     ok((hr == S_OK) || broken(hr == E_NOINTERFACE), "got 0x%08x\n", hr);
844     if(SUCCEEDED(hr))
845     {
846         /* SetCancelButtonLabel */
847         hr = IFileDialog2_SetOkButtonLabel(pfd2, NULL);
848         ok(hr == S_OK, "got 0x%08x\n", hr);
849         hr = IFileDialog2_SetOkButtonLabel(pfd2, null);
850         ok(hr == S_OK, "got 0x%08x\n", hr);
851         hr = IFileDialog2_SetOkButtonLabel(pfd2, txt);
852         ok(hr == S_OK, "got 0x%08x\n", hr);
853
854         /* SetNavigationRoot */
855         todo_wine
856         {
857         hr = IFileDialog2_SetNavigationRoot(pfd2, NULL);
858         ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
859         hr = IFileDialog2_SetNavigationRoot(pfd2, psidesktop);
860         ok(hr == S_OK, "got 0x%08x\n", hr);
861         }
862
863         IFileDialog2_Release(pfd2);
864     }
865
866     hr = IFileSaveDialog_QueryInterface(pfsd, &IID_IFileDialog2, (void**)&pfd2);
867     ok((hr == S_OK) || broken(hr == E_NOINTERFACE), "got 0x%08x\n", hr);
868     if(SUCCEEDED(hr))
869     {
870         /* SetCancelButtonLabel */
871         hr = IFileDialog2_SetOkButtonLabel(pfd2, NULL);
872         ok(hr == S_OK, "got 0x%08x\n", hr);
873         hr = IFileDialog2_SetOkButtonLabel(pfd2, null);
874         ok(hr == S_OK, "got 0x%08x\n", hr);
875         hr = IFileDialog2_SetOkButtonLabel(pfd2, txt);
876         ok(hr == S_OK, "got 0x%08x\n", hr);
877
878         /* SetNavigationRoot */
879         todo_wine
880         {
881         hr = IFileDialog2_SetNavigationRoot(pfd2, NULL);
882         ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
883         hr = IFileDialog2_SetNavigationRoot(pfd2, psidesktop);
884         ok(hr == S_OK, "got 0x%08x\n", hr);
885         }
886
887         IFileDialog2_Release(pfd2);
888     }
889
890     /* Cleanup */
891     IShellItem_Release(psidesktop);
892     ref = IFileOpenDialog_Release(pfod);
893     ok(!ref, "Got refcount %d, should have been released.\n", ref);
894     ref = IFileSaveDialog_Release(pfsd);
895     ok(!ref, "Got refcount %d, should have been released.\n", ref);
896 }
897
898 static void ensure_zero_events_(const char *file, int line, IFileDialogEventsImpl *impl)
899 {
900     ok_(file, line)(!impl->OnFileOk, "OnFileOk: %d\n", impl->OnFileOk);
901     ok_(file, line)(!impl->OnFolderChanging, "OnFolderChanging: %d\n", impl->OnFolderChanging);
902     ok_(file, line)(!impl->OnFolderChange, "OnFolderChange: %d\n", impl->OnFolderChange);
903     ok_(file, line)(!impl->OnSelectionChange, "OnSelectionChange: %d\n", impl->OnSelectionChange);
904     ok_(file, line)(!impl->OnShareViolation, "OnShareViolation: %d\n", impl->OnShareViolation);
905     ok_(file, line)(!impl->OnTypeChange, "OnTypeChange: %d\n", impl->OnTypeChange);
906     ok_(file, line)(!impl->OnOverwrite, "OnOverwrite: %d\n", impl->OnOverwrite);
907     impl->OnFileOk = impl->OnFolderChanging = impl->OnFolderChange = 0;
908     impl->OnSelectionChange = impl->OnShareViolation = impl->OnTypeChange = 0;
909     impl->OnOverwrite = 0;
910 }
911 #define ensure_zero_events(impl) ensure_zero_events_(__FILE__, __LINE__, impl)
912
913 static void test_advise_helper(IFileDialog *pfd)
914 {
915     IFileDialogEventsImpl *pfdeimpl;
916     IFileDialogEvents *pfde;
917     DWORD cookie[10];
918     UINT i;
919     HRESULT hr;
920
921     pfde = IFileDialogEvents_Constructor();
922     pfdeimpl = impl_from_IFileDialogEvents(pfde);
923
924     hr = IFileDialog_Advise(pfd, NULL, NULL);
925     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
926     hr = IFileDialog_Advise(pfd, pfde, NULL);
927     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
928     hr = IFileDialog_Advise(pfd, NULL, &cookie[0]);
929     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
930     ok(pfdeimpl->ref == 1, "got ref %d\n", pfdeimpl->ref);
931     ensure_zero_events(pfdeimpl);
932
933     hr = IFileDialog_Unadvise(pfd, 0);
934     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
935     for(i = 0; i < 10; i++) {
936         hr = IFileDialog_Advise(pfd, pfde, &cookie[i]);
937         ok(hr == S_OK, "got 0x%08x\n", hr);
938         ok(cookie[i] == i+1, "Got cookie: %d\n", cookie[i]);
939     }
940     ok(pfdeimpl->ref == 10+1, "got ref %d\n", pfdeimpl->ref);
941     ensure_zero_events(pfdeimpl);
942
943     for(i = 3; i < 7; i++) {
944         hr = IFileDialog_Unadvise(pfd, cookie[i]);
945         ok(hr == S_OK, "got 0x%08x\n", hr);
946     }
947     ok(pfdeimpl->ref == 6+1, "got ref %d\n", pfdeimpl->ref);
948     ensure_zero_events(pfdeimpl);
949
950     for(i = 0; i < 3; i++) {
951         hr = IFileDialog_Unadvise(pfd, cookie[i]);
952         ok(hr == S_OK, "got 0x%08x\n", hr);
953     }
954     ok(pfdeimpl->ref == 3+1, "got ref %d\n", pfdeimpl->ref);
955     ensure_zero_events(pfdeimpl);
956
957     for(i = 7; i < 10; i++) {
958         hr = IFileDialog_Unadvise(pfd, cookie[i]);
959         ok(hr == S_OK, "got 0x%08x\n", hr);
960     }
961     ok(pfdeimpl->ref == 1, "got ref %d\n", pfdeimpl->ref);
962     ensure_zero_events(pfdeimpl);
963
964     hr = IFileDialog_Unadvise(pfd, cookie[9]+1);
965     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
966     ok(pfdeimpl->ref == 1, "got ref %d\n", pfdeimpl->ref);
967     ensure_zero_events(pfdeimpl);
968
969     hr = IFileDialog_Advise(pfd, pfde, &cookie[0]);
970     ok(hr == S_OK, "got 0x%08x\n", hr);
971     todo_wine ok(cookie[0] == 1, "got cookie: %d\n", cookie[0]);
972     ok(pfdeimpl->ref == 1+1, "got ref %d\n", pfdeimpl->ref);
973     ensure_zero_events(pfdeimpl);
974
975     hr = IFileDialog_Unadvise(pfd, cookie[0]);
976
977     if(0)
978     {
979         /* Unadvising already unadvised cookies crashes on
980            Windows 7. */
981         IFileDialog_Unadvise(pfd, cookie[0]);
982     }
983
984
985     IFileDialogEvents_Release(pfde);
986 }
987
988 static void test_advise(void)
989 {
990     IFileDialog *pfd;
991     HRESULT hr;
992     LONG ref;
993
994     trace("Testing FileOpenDialog (advise)\n");
995     hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER,
996                           &IID_IFileDialog, (void**)&pfd);
997     ok(hr == S_OK, "got 0x%08x.\n", hr);
998     test_advise_helper(pfd);
999     ref = IFileDialog_Release(pfd);
1000     ok(!ref, "Got refcount %d, should have been released.\n", ref);
1001
1002     trace("Testing FileSaveDialog (advise)\n");
1003     hr = CoCreateInstance(&CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER,
1004                           &IID_IFileDialog, (void**)&pfd);
1005     ok(hr == S_OK, "got 0x%08x.\n", hr);
1006     test_advise_helper(pfd);
1007     ref = IFileDialog_Release(pfd);
1008     ok(!ref, "Got refcount %d, should have been released.\n", ref);
1009 }
1010
1011 static void touch_file(LPCWSTR filename)
1012 {
1013     HANDLE file;
1014     file = CreateFileW(filename, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
1015     ok(file != INVALID_HANDLE_VALUE, "Failed to create file.\n");
1016     CloseHandle(file);
1017 }
1018
1019 static void test_filename_savedlg_(LPCWSTR set_filename, LPCWSTR defext,
1020                                    const COMDLG_FILTERSPEC *filterspec, UINT fs_count,
1021                                    LPCWSTR exp_filename, const char *file, int line)
1022 {
1023     IFileSaveDialog *pfsd;
1024     IFileDialogEventsImpl *pfdeimpl;
1025     IFileDialogEvents *pfde;
1026     DWORD cookie;
1027     LPWSTR filename;
1028     IShellItem *psi;
1029     LONG ref;
1030     HRESULT hr;
1031
1032     hr = CoCreateInstance(&CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER,
1033                           &IID_IFileSaveDialog, (void**)&pfsd);
1034     ok_(file,line)(hr == S_OK, "Got 0x%08x\n", hr);
1035
1036     if(fs_count)
1037     {
1038         hr = IFileSaveDialog_SetFileTypes(pfsd, fs_count, filterspec);
1039         ok_(file,line)(hr == S_OK, "SetFileTypes failed: Got 0x%08x\n", hr);
1040     }
1041
1042     if(defext)
1043     {
1044         hr = IFileSaveDialog_SetDefaultExtension(pfsd, defext);
1045         ok_(file,line)(hr == S_OK, "SetDefaultExtensions failed: Got 0x%08x\n", hr);
1046     }
1047
1048     pfde = IFileDialogEvents_Constructor();
1049     pfdeimpl = impl_from_IFileDialogEvents(pfde);
1050     pfdeimpl->set_filename = set_filename;
1051     hr = IFileSaveDialog_Advise(pfsd, pfde, &cookie);
1052     ok_(file,line)(hr == S_OK, "Advise failed: Got 0x%08x\n", hr);
1053
1054     hr = IFileSaveDialog_Show(pfsd, NULL);
1055     ok_(file,line)(hr == S_OK, "Show failed: Got 0x%08x\n", hr);
1056
1057     hr = IFileSaveDialog_GetFileName(pfsd, &filename);
1058     ok_(file,line)(hr == S_OK, "GetFileName failed: Got 0x%08x\n", hr);
1059     ok_(file,line)(!lstrcmpW(filename, set_filename), "Got %s\n", wine_dbgstr_w(filename));
1060     CoTaskMemFree(filename);
1061
1062     hr = IFileSaveDialog_GetResult(pfsd, &psi);
1063     ok_(file,line)(hr == S_OK, "GetResult failed: Got 0x%08x\n", hr);
1064
1065     hr = IShellItem_GetDisplayName(psi, SIGDN_PARENTRELATIVEPARSING, &filename);
1066     ok_(file,line)(hr == S_OK, "GetDisplayName failed: Got 0x%08x\n", hr);
1067     ok_(file,line)(!lstrcmpW(filename, exp_filename), "(GetDisplayName) Got %s\n", wine_dbgstr_w(filename));
1068     CoTaskMemFree(filename);
1069     IShellItem_Release(psi);
1070
1071     hr = IFileSaveDialog_Unadvise(pfsd, cookie);
1072     ok_(file,line)(hr == S_OK, "Unadvise failed: Got 0x%08x\n", hr);
1073
1074     ref = IFileSaveDialog_Release(pfsd);
1075     ok_(file,line)(!ref, "Got refcount %d, should have been released.\n", ref);
1076
1077     IFileDialogEvents_Release(pfde);
1078 }
1079 #define test_filename_savedlg(set_filename, defext, filterspec, fs_count, exp_filename) \
1080     test_filename_savedlg_(set_filename, defext, filterspec, fs_count, exp_filename, __FILE__, __LINE__)
1081
1082 static void test_filename_opendlg_(LPCWSTR set_filename, IShellItem *psi_current, LPCWSTR defext,
1083                                    const COMDLG_FILTERSPEC *filterspec, UINT fs_count,
1084                                    LPCWSTR exp_filename, const char *file, int line)
1085 {
1086     IFileOpenDialog *pfod;
1087     IFileDialogEventsImpl *pfdeimpl;
1088     IFileDialogEvents *pfde;
1089     DWORD cookie;
1090     LPWSTR filename;
1091     IShellItemArray *psia;
1092     IShellItem *psi;
1093     LONG ref;
1094     HRESULT hr;
1095
1096     hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER,
1097                           &IID_IFileOpenDialog, (void**)&pfod);
1098     ok_(file,line)(hr == S_OK, "CoCreateInstance failed: Got 0x%08x\n", hr);
1099
1100     if(defext)
1101     {
1102         hr = IFileOpenDialog_SetDefaultExtension(pfod, defext);
1103         ok_(file,line)(hr == S_OK, "SetDefaultExtensions failed: Got 0x%08x\n", hr);
1104     }
1105
1106     if(fs_count)
1107     {
1108         hr = IFileOpenDialog_SetFileTypes(pfod, 2, filterspec);
1109         ok_(file,line)(hr == S_OK, "SetFileTypes failed: Got 0x%08x\n", hr);
1110     }
1111
1112     hr = IFileOpenDialog_SetFolder(pfod, psi_current);
1113     ok_(file,line)(hr == S_OK, "SetFolder failed: Got 0x%08x\n", hr);
1114
1115     pfde = IFileDialogEvents_Constructor();
1116     pfdeimpl = impl_from_IFileDialogEvents(pfde);
1117     pfdeimpl->set_filename = set_filename;
1118     pfdeimpl->set_filename_tried = FALSE;
1119     hr = IFileOpenDialog_Advise(pfod, pfde, &cookie);
1120     ok_(file,line)(hr == S_OK, "Advise failed: Got 0x%08x\n", hr);
1121
1122     hr = IFileOpenDialog_Show(pfod, NULL);
1123     ok_(file,line)(hr == S_OK || (!exp_filename && hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)),
1124                    "Show failed: Got 0x%08x\n", hr);
1125     if(hr == S_OK)
1126     {
1127         hr = IFileOpenDialog_GetResult(pfod, &psi);
1128         ok_(file,line)(hr == S_OK, "GetResult failed: Got 0x%08x\n", hr);
1129
1130         hr = IShellItem_GetDisplayName(psi, SIGDN_PARENTRELATIVEPARSING, &filename);
1131         ok_(file,line)(hr == S_OK, "GetDisplayName(Result) failed: Got 0x%08x\n", hr);
1132         ok_(file,line)(!lstrcmpW(filename, exp_filename), "(GetResult) Got %s\n", wine_dbgstr_w(filename));
1133         CoTaskMemFree(filename);
1134         IShellItem_Release(psi);
1135
1136         hr = IFileOpenDialog_GetResults(pfod, &psia);
1137         ok_(file,line)(hr == S_OK, "GetResults failed: Got 0x%08x\n", hr);
1138         hr = IShellItemArray_GetItemAt(psia, 0, &psi);
1139         ok_(file,line)(hr == S_OK, "GetItemAt failed: Got 0x%08x\n", hr);
1140
1141         hr = IShellItem_GetDisplayName(psi, SIGDN_PARENTRELATIVEPARSING, &filename);
1142         ok_(file,line)(hr == S_OK, "GetDisplayName(Results) failed: Got 0x%08x\n", hr);
1143         ok_(file,line)(!lstrcmpW(filename, exp_filename), "(GetResults) Got %s\n", wine_dbgstr_w(filename));
1144         CoTaskMemFree(filename);
1145
1146         IShellItem_Release(psi);
1147         IShellItemArray_Release(psia);
1148     }
1149     else
1150     {
1151         hr = IFileOpenDialog_GetResult(pfod, &psi);
1152         ok_(file,line)(hr == E_UNEXPECTED, "GetResult: Got 0x%08x\n", hr);
1153
1154         hr = IFileOpenDialog_GetResults(pfod, &psia);
1155         ok_(file,line)(hr == E_FAIL, "GetResults: Got 0x%08x\n", hr);
1156     }
1157
1158     hr = IFileOpenDialog_GetFileName(pfod, &filename);
1159     ok_(file,line)(hr == S_OK, "GetFileName failed: Got 0x%08x\n", hr);
1160     ok_(file,line)(!lstrcmpW(filename, set_filename), "(GetFileName) Got %s\n", wine_dbgstr_w(filename));
1161     CoTaskMemFree(filename);
1162
1163
1164     hr = IFileOpenDialog_Unadvise(pfod, cookie);
1165     ok_(file,line)(hr == S_OK, "Unadvise failed: Got 0x%08x\n", hr);
1166
1167     ref = IFileOpenDialog_Release(pfod);
1168     ok_(file,line)(!ref, "Got refcount %d, should have been released.\n", ref);
1169
1170     IFileDialogEvents_Release(pfde);
1171 }
1172 #define test_filename_opendlg(set_filename, psi, defext, filterspec, fs_count, exp_filename) \
1173     test_filename_opendlg_(set_filename, psi, defext, filterspec, fs_count, exp_filename, __FILE__, __LINE__)
1174
1175 static void test_filename(void)
1176 {
1177     IShellItem *psi_current;
1178     HRESULT hr;
1179     WCHAR buf[MAX_PATH];
1180
1181     static const WCHAR filename_noextW[] = {'w','i','n','e','t','e','s','t',0};
1182     static const WCHAR filename_dotextW[] = {'w','i','n','e','t','e','s','t','.',0};
1183     static const WCHAR filename_dotanddefW[] = {'w','i','n','e','t','e','s','t','.','.','w','t','e',0};
1184     static const WCHAR filename_defextW[] = {'w','i','n','e','t','e','s','t','.','w','t','e',0};
1185     static const WCHAR filename_ext1W[] = {'w','i','n','e','t','e','s','t','.','w','t','1',0};
1186     static const WCHAR filename_ext2W[] = {'w','i','n','e','t','e','s','t','.','w','t','2',0};
1187     static const WCHAR filename_ext1anddefW[] =
1188         {'w','i','n','e','t','e','s','t','.','w','t','1','.','w','t','e',0};
1189     static const WCHAR defextW[] = {'w','t','e',0};
1190     static const WCHAR desc1[] = {'d','e','s','c','r','i','p','t','i','o','n','1',0};
1191     static const WCHAR desc2[] = {'d','e','s','c','r','i','p','t','i','o','n','2',0};
1192     static const WCHAR descdef[] = {'d','e','f','a','u','l','t',' ','d','e','s','c',0};
1193     static const WCHAR ext1[] = {'*','.','w','t','1',0};
1194     static const WCHAR ext2[] = {'*','.','w','t','2',0};
1195     static const WCHAR extdef[] = {'*','.','w','t','e',0};
1196     static const WCHAR complexext[] = {'*','.','w','t','2',';','*','.','w','t','1',0};
1197
1198     static const COMDLG_FILTERSPEC filterspec[] = {
1199         { desc1, ext1 }, { desc2, ext2 }, { descdef, extdef }
1200     };
1201     static const COMDLG_FILTERSPEC filterspec2[] = {
1202         { desc1, complexext }
1203     };
1204
1205     /* No extension */
1206     test_filename_savedlg(filename_noextW, NULL, NULL, 0, filename_noextW);
1207     /* Default extension */
1208     test_filename_savedlg(filename_noextW, defextW, NULL, 0, filename_defextW);
1209     /* Default extension on filename ending with a . */
1210     test_filename_savedlg(filename_dotextW, defextW, NULL, 0, filename_dotanddefW);
1211     /* Default extension on filename with default extension */
1212     test_filename_savedlg(filename_defextW, defextW, NULL, 0, filename_defextW);
1213     /* Default extension on filename with another extension */
1214     test_filename_savedlg(filename_ext1W, defextW, NULL, 0, filename_ext1anddefW);
1215     /* Default extension, filterspec without default extension */
1216     test_filename_savedlg(filename_noextW, defextW, filterspec, 2, filename_ext1W);
1217     /* Default extension, filterspec with default extension */
1218     test_filename_savedlg(filename_noextW, defextW, filterspec, 3, filename_ext1W);
1219     /* Default extension, filterspec with "complex" extension */
1220     test_filename_savedlg(filename_noextW, defextW, filterspec2, 1, filename_ext2W);
1221
1222     GetCurrentDirectoryW(MAX_PATH, buf);
1223     ok(!!pSHCreateItemFromParsingName, "SHCreateItemFromParsingName is missing.\n");
1224     hr = pSHCreateItemFromParsingName(buf, NULL, &IID_IShellItem, (void**)&psi_current);
1225     ok(hr == S_OK, "Got 0x%08x\n", hr);
1226
1227     touch_file(filename_noextW);
1228     touch_file(filename_defextW);
1229     touch_file(filename_ext2W);
1230
1231     /* IFileOpenDialog, default extension */
1232     test_filename_opendlg(filename_noextW, psi_current, defextW, NULL, 0, filename_noextW);
1233     /* IFileOpenDialog, default extension and filterspec */
1234     test_filename_opendlg(filename_noextW, psi_current, defextW, filterspec, 2, filename_noextW);
1235
1236     DeleteFileW(filename_noextW);
1237     /* IFileOpenDialog, default extension, noextW deleted */
1238     test_filename_opendlg(filename_noextW, psi_current, defextW, NULL, 0, filename_defextW);
1239     if(0) /* Interactive */
1240     {
1241     /* IFileOpenDialog, filterspec, no default extension, noextW deleted */
1242     test_filename_opendlg(filename_noextW, psi_current, NULL, filterspec, 2, NULL);
1243     }
1244
1245     IShellItem_Release(psi_current);
1246     DeleteFileW(filename_defextW);
1247     DeleteFileW(filename_ext2W);
1248 }
1249
1250 static const WCHAR label[] = {'l','a','b','e','l',0};
1251 static const WCHAR label2[] = {'t','e','s','t',0};
1252 static const WCHAR menuW[] = {'m','e','n','u','_','i','t','e','m',0};
1253 static const WCHAR pushbutton1W[] = {'p','u','s','h','b','u','t','t','o','n','_','i','t','e','m',0};
1254 static const WCHAR pushbutton2W[] = {'p','u','s','h','b','u','t','t','o','n','2','_','i','t','e','m',0};
1255 static const WCHAR comboboxitem1W[] = {'c','o','m','b','o','b','o','x','1','_','i','t','e','m',0};
1256 static const WCHAR comboboxitem2W[] = {'c','o','m','b','o','b','o','x','2','_','i','t','e','m',0};
1257 static const WCHAR radiobutton1W[] = {'r','a','d','i','o','b','u','t','t','o','n','1','_','i','t','e','m',0};
1258 static const WCHAR radiobutton2W[] = {'r','a','d','i','o','b','u','t','t','o','n','2','_','i','t','e','m',0};
1259 static const WCHAR checkbutton1W[] = {'c','h','e','c','k','b','u','t','t','o','n','1','_','i','t','e','m',0};
1260 static const WCHAR checkbutton2W[] = {'c','h','e','c','k','b','u','t','t','o','n','2','_','i','t','e','m',0};
1261 static const WCHAR editbox1W[] = {'e','d','i','t','b','o','x','W','1','_','i','t','e','m',0};
1262 static const WCHAR editbox2W[] = {'e','d','i','t','b','o','x','W','2','_','i','t','e','m',0};
1263 static const WCHAR textW[] = {'t','e','x','t','_','i','t','e','m',0};
1264 static const WCHAR text2W[] = {'t','e','x','t','2','_','i','t','e','m',0};
1265 static const WCHAR separatorW[] = {'s','e','p','a','r','a','t','o','r','_','i','t','e','m',0};
1266 static const WCHAR visualgroup1W[] = {'v','i','s','u','a','l','g','r','o','u','p','1',0};
1267 static const WCHAR visualgroup2W[] = {'v','i','s','u','a','l','g','r','o','u','p','2',0};
1268
1269 static const WCHAR floatnotifysinkW[] = {'F','l','o','a','t','N','o','t','i','f','y','S','i','n','k',0};
1270 static const WCHAR RadioButtonListW[] = {'R','a','d','i','o','B','u','t','t','o','n','L','i','s','t',0};
1271
1272 struct fw_arg {
1273     LPCWSTR class, text;
1274     HWND hwnd_res;
1275 };
1276 static BOOL CALLBACK find_window_callback(HWND hwnd, LPARAM lparam)
1277 {
1278     struct fw_arg *arg = (struct fw_arg*)lparam;
1279     WCHAR buf[1024];
1280
1281     if(arg->class)
1282     {
1283         GetClassNameW(hwnd, buf, 1024);
1284         if(lstrcmpW(buf, arg->class))
1285             return TRUE;
1286     }
1287
1288     if(arg->text)
1289     {
1290         GetWindowTextW(hwnd, buf, 1024);
1291         if(lstrcmpW(buf, arg->text))
1292             return TRUE;
1293     }
1294
1295     arg->hwnd_res = hwnd;
1296     return FALSE;
1297 }
1298
1299 static HWND find_window(HWND parent, LPCWSTR class, LPCWSTR text)
1300 {
1301     struct fw_arg arg = {class, text, NULL};
1302
1303     EnumChildWindows(parent, find_window_callback, (LPARAM)&arg);
1304     return arg.hwnd_res;
1305 }
1306
1307 static void test_customize_onfolderchange(IFileDialog *pfd)
1308 {
1309     IOleWindow *pow;
1310     HWND dlg_hwnd, item, item_parent;
1311     HRESULT hr;
1312     BOOL br;
1313     WCHAR buf[1024];
1314
1315     buf[0] = '\0';
1316
1317     hr = IFileDialog_QueryInterface(pfd, &IID_IOleWindow, (void**)&pow);
1318     ok(hr == S_OK, "Got 0x%08x\n", hr);
1319     hr = IOleWindow_GetWindow(pow, &dlg_hwnd);
1320     ok(hr == S_OK, "Got 0x%08x\n", hr);
1321     ok(dlg_hwnd != NULL, "Got NULL.\n");
1322     IOleWindow_Release(pow);
1323
1324     item = find_window(dlg_hwnd, NULL, checkbutton2W);
1325     ok(item != NULL, "Failed to find item.\n");
1326     item_parent = GetParent(item);
1327     GetClassNameW(item_parent, buf, 1024);
1328     ok(!lstrcmpW(buf, floatnotifysinkW), "Got %s\n", wine_dbgstr_w(buf));
1329     item = find_window(dlg_hwnd, NULL, text2W);
1330     ok(item != NULL, "Failed to find item.\n");
1331     item_parent = GetParent(item);
1332     GetClassNameW(item_parent, buf, 1024);
1333     ok(!lstrcmpW(buf, floatnotifysinkW), "Got %s\n", wine_dbgstr_w(buf));
1334     item = find_window(dlg_hwnd, NULL, radiobutton1W);
1335     todo_wine ok(item != NULL, "Failed to find item.\n");
1336     item_parent = GetParent(item);
1337     GetClassNameW(item_parent, buf, 1024);
1338     todo_wine ok(!lstrcmpW(buf, RadioButtonListW), "Got %s\n", wine_dbgstr_w(buf));
1339     item_parent = GetParent(item_parent);
1340     GetClassNameW(item_parent, buf, 1024);
1341     ok(!lstrcmpW(buf, floatnotifysinkW), "Got %s\n", wine_dbgstr_w(buf));
1342
1343     item = find_window(dlg_hwnd, NULL, pushbutton1W);
1344     ok(item == NULL, "Found item: %p\n", item);
1345     item = find_window(dlg_hwnd, NULL, pushbutton2W);
1346     ok(item == NULL, "Found item: %p\n", item);
1347     item = find_window(dlg_hwnd, NULL, comboboxitem1W);
1348     ok(item == NULL, "Found item: %p\n", item);
1349     item = find_window(dlg_hwnd, NULL, comboboxitem2W);
1350     ok(item == NULL, "Found item: %p\n", item);
1351     item = find_window(dlg_hwnd, NULL, radiobutton2W);
1352     ok(item == NULL, "Found item: %p\n", item);
1353     item = find_window(dlg_hwnd, NULL, checkbutton1W);
1354     ok(item == NULL, "Found item: %p\n", item);
1355     item = find_window(dlg_hwnd, NULL, editbox1W);
1356     ok(item == NULL, "Found item: %p\n", item);
1357     item = find_window(dlg_hwnd, NULL, editbox2W);
1358     ok(item == NULL, "Found item: %p\n", item);
1359     item = find_window(dlg_hwnd, NULL, textW);
1360     ok(item == NULL, "Found item: %p\n", item);
1361     item = find_window(dlg_hwnd, NULL, separatorW);
1362     ok(item == NULL, "Found item: %p\n", item);
1363     item = find_window(dlg_hwnd, NULL, visualgroup1W);
1364     ok(item == NULL, "Found item: %p\n", item);
1365     item = find_window(dlg_hwnd, NULL, visualgroup2W);
1366     ok(item == NULL, "Found item: %p\n", item);
1367
1368     br = PostMessageW(dlg_hwnd, WM_COMMAND, IDCANCEL, 0);
1369     ok(br, "Failed\n");
1370 }
1371
1372 static void test_customize(void)
1373 {
1374     IFileDialog *pfod;
1375     IFileDialogCustomize *pfdc;
1376     IFileDialogEventsImpl *pfdeimpl;
1377     IFileDialogEvents *pfde;
1378     IOleWindow *pow;
1379     CDCONTROLSTATEF cdstate;
1380     DWORD cookie;
1381     LPWSTR tmpstr;
1382     UINT i;
1383     LONG ref;
1384     HWND dlg_hwnd;
1385     HRESULT hr;
1386     hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER,
1387                           &IID_IFileDialog, (void**)&pfod);
1388     ok(hr == S_OK, "got 0x%08x.\n", hr);
1389
1390     hr = IFileDialog_QueryInterface(pfod, &IID_IFileDialogCustomize, (void**)&pfdc);
1391     ok(hr == S_OK, "got 0x%08x.\n", hr);
1392     if(FAILED(hr))
1393     {
1394         skip("Skipping IFileDialogCustomize tests.\n");
1395         return;
1396     }
1397
1398     i = 0;
1399     hr = IFileDialogCustomize_AddPushButton(pfdc, i, pushbutton1W);
1400     ok(hr == S_OK, "got 0x%08x.\n", hr);
1401     hr = IFileDialogCustomize_AddPushButton(pfdc, i, pushbutton1W);
1402     ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
1403
1404     hr = IFileDialog_QueryInterface(pfod, &IID_IOleWindow, (void**)&pow);
1405     ok(hr == S_OK, "Got 0x%08x\n", hr);
1406     hr = IOleWindow_GetWindow(pow, &dlg_hwnd);
1407     ok(hr == S_OK, "Got 0x%08x\n", hr);
1408     ok(dlg_hwnd == NULL, "NULL\n");
1409     IOleWindow_Release(pow);
1410
1411     cdstate = 0xdeadbeef;
1412     hr = IFileDialogCustomize_GetControlState(pfdc, i, &cdstate);
1413     ok(hr == S_OK, "got 0x%08x.\n", hr);
1414     ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1415
1416     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1417     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
1418
1419     hr = IFileDialogCustomize_SetControlLabel(pfdc, i, label2);
1420     ok(hr == S_OK, "got 0x%08x (control: %d).\n", hr, i);
1421
1422     hr = IFileDialogCustomize_EnableOpenDropDown(pfdc, i);
1423     todo_wine ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
1424     hr = IFileDialogCustomize_EnableOpenDropDown(pfdc, ++i);
1425     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1426
1427     cdstate = 0xdeadbeef;
1428     hr = IFileDialogCustomize_GetControlState(pfdc, i, &cdstate);
1429     ok(hr == E_NOTIMPL, "got 0x%08x.\n", hr);
1430     ok(cdstate == 0xdeadbeef, "got 0x%08x.\n", cdstate);
1431
1432     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1433     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1434     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1435     todo_wine ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
1436
1437     cdstate = 0xdeadbeef;
1438     hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1439     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1440     todo_wine ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1441     hr = IFileDialogCustomize_SetControlItemState(pfdc, i, 0, 0);
1442     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1443     cdstate = 0xdeadbeef;
1444     hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1445     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1446     todo_wine ok(!cdstate, "got 0x%08x.\n", cdstate);
1447     hr = IFileDialogCustomize_SetControlItemState(pfdc, i, 0, CDCS_ENABLEDVISIBLE);
1448     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1449     cdstate = 0xdeadbeef;
1450     hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1451     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1452     todo_wine ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1453
1454     hr = IFileDialogCustomize_SetControlLabel(pfdc, i, label2);
1455     todo_wine ok(hr == E_NOTIMPL, "got 0x%08x (control: %d).\n", hr, i);
1456
1457     hr = IFileDialogCustomize_AddMenu(pfdc, i, menuW);
1458     todo_wine ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
1459     hr = IFileDialogCustomize_AddMenu(pfdc, ++i, label);
1460     ok(hr == S_OK, "got 0x%08x.\n", hr);
1461
1462     cdstate = 0xdeadbeef;
1463     hr = IFileDialogCustomize_GetControlState(pfdc, i, &cdstate);
1464     ok(hr == S_OK, "got 0x%08x.\n", hr);
1465     ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1466
1467     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1468     ok(hr == S_OK, "got 0x%08x.\n", hr);
1469     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1470     ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
1471
1472     hr = IFileDialogCustomize_SetControlLabel(pfdc, i, label2);
1473     ok(hr == S_OK, "got 0x%08x (control: %d).\n", hr, i);
1474
1475     hr = IFileDialogCustomize_AddPushButton(pfdc, i, pushbutton2W);
1476     ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
1477     hr = IFileDialogCustomize_AddPushButton(pfdc, ++i, pushbutton2W);
1478     ok(hr == S_OK, "got 0x%08x.\n", hr);
1479
1480     cdstate = 0xdeadbeef;
1481     hr = IFileDialogCustomize_GetControlState(pfdc, i, &cdstate);
1482     ok(hr == S_OK, "got 0x%08x.\n", hr);
1483     ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1484
1485     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1486     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
1487
1488     hr = IFileDialogCustomize_SetControlLabel(pfdc, i, label2);
1489     ok(hr == S_OK, "got 0x%08x (control: %d).\n", hr, i);
1490
1491     hr = IFileDialogCustomize_AddComboBox(pfdc, i);
1492     ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
1493     hr = IFileDialogCustomize_AddComboBox(pfdc, ++i);
1494     ok(hr == S_OK, "got 0x%08x.\n", hr);
1495
1496     cdstate = 0xdeadbeef;
1497     hr = IFileDialogCustomize_GetControlState(pfdc, i, &cdstate);
1498     ok(hr == S_OK, "got 0x%08x.\n", hr);
1499     ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1500
1501     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1502     ok(hr == S_OK, "got 0x%08x.\n", hr);
1503     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1504     ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
1505
1506     hr = IFileDialogCustomize_SetControlLabel(pfdc, i, label2);
1507     ok(hr == S_OK, "got 0x%08x (control: %d).\n", hr, i);
1508
1509     hr = IFileDialogCustomize_AddRadioButtonList(pfdc, i);
1510     todo_wine ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
1511     hr = IFileDialogCustomize_AddRadioButtonList(pfdc, ++i);
1512     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1513
1514     cdstate = 0xdeadbeef;
1515     hr = IFileDialogCustomize_GetControlState(pfdc, i, &cdstate);
1516     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1517     todo_wine ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1518
1519     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, radiobutton1W);
1520     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1521     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, radiobutton1W);
1522     todo_wine ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
1523
1524     hr = IFileDialogCustomize_SetControlLabel(pfdc, i, radiobutton2W);
1525     todo_wine ok(hr == S_OK, "got 0x%08x (control: %d).\n", hr, i);
1526
1527     hr = IFileDialogCustomize_AddCheckButton(pfdc, i, label, TRUE);
1528     todo_wine ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
1529     hr = IFileDialogCustomize_AddCheckButton(pfdc, ++i, checkbutton1W, TRUE);
1530     ok(hr == S_OK, "got 0x%08x.\n", hr);
1531
1532     cdstate = 0xdeadbeef;
1533     hr = IFileDialogCustomize_GetControlState(pfdc, i, &cdstate);
1534     ok(hr == S_OK, "got 0x%08x.\n", hr);
1535     ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1536
1537     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1538     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
1539
1540     hr = IFileDialogCustomize_SetControlLabel(pfdc, i, checkbutton2W);
1541     ok(hr == S_OK, "got 0x%08x (control: %d).\n", hr, i);
1542
1543     if(SUCCEEDED(hr))
1544     {
1545         BOOL checked;
1546         hr = IFileDialogCustomize_GetCheckButtonState(pfdc, i, &checked);
1547         ok(hr == S_OK, "got 0x%08x.\n", hr);
1548         ok(checked, "checkbox not checked.\n");
1549
1550         hr = IFileDialogCustomize_SetCheckButtonState(pfdc, i, FALSE);
1551         ok(hr == S_OK, "got 0x%08x.\n", hr);
1552
1553         hr = IFileDialogCustomize_GetCheckButtonState(pfdc, i, &checked);
1554         ok(hr == S_OK, "got 0x%08x.\n", hr);
1555         ok(!checked, "checkbox checked.\n");
1556
1557         hr = IFileDialogCustomize_SetCheckButtonState(pfdc, i, TRUE);
1558         ok(hr == S_OK, "got 0x%08x.\n", hr);
1559
1560         hr = IFileDialogCustomize_GetCheckButtonState(pfdc, i, &checked);
1561         ok(hr == S_OK, "got 0x%08x.\n", hr);
1562         ok(checked, "checkbox not checked.\n");
1563     }
1564
1565     hr = IFileDialogCustomize_AddEditBox(pfdc, i, label);
1566     ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
1567     hr = IFileDialogCustomize_AddEditBox(pfdc, ++i, editbox1W);
1568     ok(hr == S_OK, "got 0x%08x.\n", hr);
1569
1570     cdstate = 0xdeadbeef;
1571     hr = IFileDialogCustomize_GetControlState(pfdc, i, &cdstate);
1572     ok(hr == S_OK, "got 0x%08x.\n", hr);
1573     ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1574
1575     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1576     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
1577
1578     /* Does not affect the text in the editbox */
1579     hr = IFileDialogCustomize_SetControlLabel(pfdc, i, editbox2W);
1580     ok(hr == S_OK, "got 0x%08x (control: %d).\n", hr, i);
1581
1582     hr = IFileDialogCustomize_GetEditBoxText(pfdc, i, &tmpstr);
1583     ok(hr == S_OK, "got 0x%08x.\n", hr);
1584     if(SUCCEEDED(hr))
1585     {
1586         ok(!lstrcmpW(tmpstr, editbox1W), "got %s.\n", wine_dbgstr_w(tmpstr));
1587         CoTaskMemFree(tmpstr);
1588     }
1589
1590     hr = IFileDialogCustomize_SetEditBoxText(pfdc, i, label2);
1591     ok(hr == S_OK, "got 0x%08x.\n", hr);
1592
1593     hr = IFileDialogCustomize_GetEditBoxText(pfdc, i, &tmpstr);
1594     ok(hr == S_OK, "got 0x%08x.\n", hr);
1595     if(SUCCEEDED(hr))
1596     {
1597         ok(!lstrcmpW(tmpstr, label2), "got %s.\n", wine_dbgstr_w(tmpstr));
1598         CoTaskMemFree(tmpstr);
1599     }
1600
1601     hr = IFileDialogCustomize_AddSeparator(pfdc, i);
1602     ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
1603     hr = IFileDialogCustomize_AddSeparator(pfdc, ++i);
1604     ok(hr == S_OK, "got 0x%08x.\n", hr);
1605
1606     cdstate = 0xdeadbeef;
1607     hr = IFileDialogCustomize_GetControlState(pfdc, i, &cdstate);
1608     ok(hr == S_OK, "got 0x%08x.\n", hr);
1609     ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1610
1611     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1612     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
1613
1614     hr = IFileDialogCustomize_SetControlLabel(pfdc, i, separatorW);
1615     ok(hr == S_OK, "got 0x%08x (control: %d).\n", hr, i);
1616
1617     hr = IFileDialogCustomize_AddText(pfdc, i, label);
1618     ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
1619     hr = IFileDialogCustomize_AddText(pfdc, ++i, textW);
1620     ok(hr == S_OK, "got 0x%08x.\n", hr);
1621
1622     cdstate = 0xdeadbeef;
1623     hr = IFileDialogCustomize_GetControlState(pfdc, i, &cdstate);
1624     ok(hr == S_OK, "got 0x%08x.\n", hr);
1625     ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1626
1627     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1628     ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
1629
1630     hr = IFileDialogCustomize_SetControlLabel(pfdc, i, text2W);
1631     ok(hr == S_OK, "got 0x%08x (control: %d).\n", hr, i);
1632
1633     hr = IFileDialogCustomize_StartVisualGroup(pfdc, i, label);
1634     todo_wine ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
1635     hr = IFileDialogCustomize_StartVisualGroup(pfdc, ++i, visualgroup1W);
1636     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1637
1638     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1639     todo_wine ok(hr == E_NOINTERFACE, "got 0x%08x.\n", hr);
1640
1641     hr = IFileDialogCustomize_SetControlLabel(pfdc, i, visualgroup2W);
1642     todo_wine ok(hr == S_OK, "got 0x%08x (control: %d).\n", hr, i);
1643
1644     cdstate = 0xdeadbeef;
1645     hr = IFileDialogCustomize_GetControlState(pfdc, i, &cdstate);
1646     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1647     todo_wine ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1648
1649     hr = IFileDialogCustomize_StartVisualGroup(pfdc, ++i, label);
1650     todo_wine ok(hr == E_UNEXPECTED, "got 0x%08x.\n", hr);
1651     hr = IFileDialogCustomize_EndVisualGroup(pfdc);
1652     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1653
1654     i++; /* Nonexisting control */
1655     hr = IFileDialogCustomize_AddControlItem(pfdc, i, 0, label);
1656     todo_wine ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
1657     hr = IFileDialogCustomize_SetControlLabel(pfdc, i, label2);
1658     ok(hr == E_INVALIDARG, "got 0x%08x (control: %d).\n", hr, i);
1659     cdstate = 0xdeadbeef;
1660     hr = IFileDialogCustomize_GetControlState(pfdc, i, &cdstate);
1661     todo_wine ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
1662     ok(cdstate == 0xdeadbeef, "got 0x%08x.\n", cdstate);
1663
1664     pfde = IFileDialogEvents_Constructor();
1665     pfdeimpl = impl_from_IFileDialogEvents(pfde);
1666     pfdeimpl->cfd_test1 = TRUE;
1667     hr = IFileDialog_Advise(pfod, pfde, &cookie);
1668     ok(hr == S_OK, "Got 0x%08x\n", hr);
1669
1670     hr = IFileDialog_Show(pfod, NULL);
1671     ok(hr == HRESULT_FROM_WIN32(ERROR_CANCELLED), "Got 0x%08x\n", hr);
1672
1673     hr = IFileDialog_Unadvise(pfod, cookie);
1674     ok(hr == S_OK, "Got 0x%08x\n", hr);
1675
1676     IFileDialogEvents_Release(pfde);
1677     IFileDialogCustomize_Release(pfdc);
1678     ref = IFileDialog_Release(pfod);
1679     ok(!ref, "Refcount not zero (%d).\n", ref);
1680
1681
1682     hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER,
1683                           &IID_IFileDialog, (void**)&pfod);
1684     ok(hr == S_OK, "got 0x%08x.\n", hr);
1685
1686     hr = IFileDialog_QueryInterface(pfod, &IID_IFileDialogCustomize, (void**)&pfdc);
1687     ok(hr == S_OK, "got 0x%08x.\n", hr);
1688
1689     i = 0;
1690     hr = IFileDialogCustomize_AddMenu(pfdc, ++i, label);
1691     ok(hr == S_OK, "got 0x%08x.\n", hr);
1692     if(SUCCEEDED(hr))
1693     {
1694         DWORD selected;
1695         UINT j = 0;
1696
1697         for(j = 0; j < 10; j++)
1698         {
1699             hr = IFileDialogCustomize_AddControlItem(pfdc, i, j, label);
1700             ok(hr == S_OK, "got 0x%08x.\n", hr);
1701         }
1702
1703         hr = IFileDialogCustomize_GetSelectedControlItem(pfdc, i, &selected);
1704         ok(hr == E_NOTIMPL, "got 0x%08x.\n", hr);
1705
1706         cdstate = 0xdeadbeef;
1707         hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1708         todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1709         todo_wine ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1710         hr = IFileDialogCustomize_SetControlItemState(pfdc, i, 0, 0);
1711         todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1712         cdstate = 0xdeadbeef;
1713         hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1714         todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1715         todo_wine ok(cdstate == 0, "got 0x%08x.\n", cdstate);
1716         hr = IFileDialogCustomize_SetControlItemState(pfdc, i, 0, CDCS_ENABLEDVISIBLE);
1717         todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1718         cdstate = 0xdeadbeef;
1719         hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1720         todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1721         todo_wine ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1722
1723         hr = IFileDialogCustomize_RemoveAllControlItems(pfdc, i);
1724         ok(hr == E_NOTIMPL, "got 0x%08x.\n", hr);
1725
1726         for(j = 0; j < 10; j++)
1727         {
1728             hr = IFileDialogCustomize_RemoveControlItem(pfdc, i, j);
1729             ok(hr == S_OK, "got 0x%08x.\n", hr);
1730         }
1731     }
1732     hr = IFileDialogCustomize_AddPushButton(pfdc, ++i, label);
1733     ok(hr == S_OK, "got 0x%08x.\n", hr);
1734     hr = IFileDialogCustomize_AddComboBox(pfdc, ++i);
1735     ok(hr == S_OK, "got 0x%08x.\n", hr);
1736     if(SUCCEEDED(hr))
1737     {
1738         DWORD selected = -1;
1739         UINT j = 0;
1740
1741         for(j = 0; j < 10; j++)
1742         {
1743             hr = IFileDialogCustomize_AddControlItem(pfdc, i, j, label);
1744             ok(hr == S_OK, "got 0x%08x.\n", hr);
1745         }
1746
1747         hr = IFileDialogCustomize_GetSelectedControlItem(pfdc, i, &selected);
1748         ok(hr == E_FAIL, "got 0x%08x.\n", hr);
1749         ok(selected == -1, "got %d.\n", selected);
1750
1751         todo_wine {
1752         cdstate = 0xdeadbeef;
1753         hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1754         ok(hr == S_OK, "got 0x%08x.\n", hr);
1755         ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1756         hr = IFileDialogCustomize_SetControlItemState(pfdc, i, 0, 0);
1757         ok(hr == S_OK, "got 0x%08x.\n", hr);
1758         cdstate = 0xdeadbeef;
1759         hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1760         ok(hr == S_OK, "got 0x%08x.\n", hr);
1761         ok(cdstate == 0, "got 0x%08x.\n", cdstate);
1762         hr = IFileDialogCustomize_SetControlItemState(pfdc, i, 0, CDCS_ENABLEDVISIBLE);
1763         ok(hr == S_OK, "got 0x%08x.\n", hr);
1764         cdstate = 0xdeadbeef;
1765         hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1766         ok(hr == S_OK, "got 0x%08x.\n", hr);
1767         ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1768         }
1769
1770         for(j = 0; j < 10; j++)
1771         {
1772             hr = IFileDialogCustomize_SetSelectedControlItem(pfdc, i, j);
1773             ok(hr == S_OK, "got 0x%08x.\n", hr);
1774             hr = IFileDialogCustomize_GetSelectedControlItem(pfdc, i, &selected);
1775             ok(hr == S_OK, "got 0x%08x.\n", hr);
1776             ok(selected == j, "got %d.\n", selected);
1777         }
1778         j++;
1779         hr = IFileDialogCustomize_SetSelectedControlItem(pfdc, i, j);
1780         ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
1781
1782         hr = IFileDialogCustomize_RemoveAllControlItems(pfdc, i);
1783         ok(hr == E_NOTIMPL, "got 0x%08x.\n", hr);
1784
1785         for(j = 0; j < 10; j++)
1786         {
1787             hr = IFileDialogCustomize_RemoveControlItem(pfdc, i, j);
1788             ok(hr == S_OK, "got 0x%08x.\n", hr);
1789         }
1790     }
1791
1792     hr = IFileDialogCustomize_AddRadioButtonList(pfdc, ++i);
1793     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1794     if(SUCCEEDED(hr))
1795     {
1796         DWORD selected = -1;
1797         UINT j = 0;
1798
1799         for(j = 0; j < 10; j++)
1800         {
1801             hr = IFileDialogCustomize_AddControlItem(pfdc, i, j, label);
1802             ok(hr == S_OK, "got 0x%08x.\n", hr);
1803         }
1804
1805         hr = IFileDialogCustomize_GetSelectedControlItem(pfdc, i, &selected);
1806         ok(hr == E_FAIL, "got 0x%08x.\n", hr);
1807         ok(selected == -1, "got %d.\n", selected);
1808
1809         todo_wine {
1810         cdstate = 0xdeadbeef;
1811         hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1812         ok(hr == S_OK, "got 0x%08x.\n", hr);
1813         ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1814         hr = IFileDialogCustomize_SetControlItemState(pfdc, i, 0, 0);
1815         ok(hr == S_OK, "got 0x%08x.\n", hr);
1816         cdstate = 0xdeadbeef;
1817         hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1818         ok(hr == S_OK, "got 0x%08x.\n", hr);
1819         ok(cdstate == 0, "got 0x%08x.\n", cdstate);
1820         hr = IFileDialogCustomize_SetControlItemState(pfdc, i, 0, CDCS_ENABLEDVISIBLE);
1821         ok(hr == S_OK, "got 0x%08x.\n", hr);
1822         cdstate = 0xdeadbeef;
1823         hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1824         ok(hr == S_OK, "got 0x%08x.\n", hr);
1825         ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1826         }
1827
1828         for(j = 0; j < 10; j++)
1829         {
1830             hr = IFileDialogCustomize_SetSelectedControlItem(pfdc, i, j);
1831             ok(hr == S_OK, "got 0x%08x.\n", hr);
1832             hr = IFileDialogCustomize_GetSelectedControlItem(pfdc, i, &selected);
1833             ok(hr == S_OK, "got 0x%08x.\n", hr);
1834             ok(selected == j, "got %d.\n", selected);
1835         }
1836         j++;
1837         hr = IFileDialogCustomize_SetSelectedControlItem(pfdc, i, j);
1838         ok(hr == E_INVALIDARG, "got 0x%08x.\n", hr);
1839
1840         hr = IFileDialogCustomize_RemoveAllControlItems(pfdc, i);
1841         ok(hr == E_NOTIMPL, "got 0x%08x.\n", hr);
1842
1843         for(j = 0; j < 10; j++)
1844         {
1845             hr = IFileDialogCustomize_RemoveControlItem(pfdc, i, j);
1846             ok(hr == S_OK, "got 0x%08x.\n", hr);
1847         }
1848     }
1849     hr = IFileDialogCustomize_EnableOpenDropDown(pfdc, ++i);
1850     todo_wine ok(hr == S_OK, "got 0x%08x.\n", hr);
1851     if(SUCCEEDED(hr))
1852     {
1853         DWORD selected = -1;
1854         UINT j = 0;
1855
1856         for(j = 0; j < 10; j++)
1857         {
1858             hr = IFileDialogCustomize_AddControlItem(pfdc, i, j, label);
1859             ok(hr == S_OK, "got 0x%08x.\n", hr);
1860         }
1861
1862         hr = IFileDialogCustomize_GetSelectedControlItem(pfdc, i, &selected);
1863         ok(hr == S_OK, "got 0x%08x.\n", hr);
1864         ok(selected == 0, "got %d.\n", selected);
1865
1866         cdstate = 0xdeadbeef;
1867         hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1868         ok(hr == S_OK, "got 0x%08x.\n", hr);
1869         ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1870         hr = IFileDialogCustomize_SetControlItemState(pfdc, i, 0, 0);
1871         ok(hr == S_OK, "got 0x%08x.\n", hr);
1872         cdstate = 0xdeadbeef;
1873         hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1874         ok(hr == S_OK, "got 0x%08x.\n", hr);
1875         ok(cdstate == 0, "got 0x%08x.\n", cdstate);
1876         hr = IFileDialogCustomize_SetControlItemState(pfdc, i, 0, CDCS_ENABLEDVISIBLE);
1877         ok(hr == S_OK, "got 0x%08x.\n", hr);
1878         cdstate = 0xdeadbeef;
1879         hr = IFileDialogCustomize_GetControlItemState(pfdc, i, 0, &cdstate);
1880         ok(hr == S_OK, "got 0x%08x.\n", hr);
1881         ok(cdstate == CDCS_ENABLEDVISIBLE, "got 0x%08x.\n", cdstate);
1882         hr = IFileDialogCustomize_SetSelectedControlItem(pfdc, i, 0);
1883         ok(hr == E_NOTIMPL, "got 0x%08x.\n", hr);
1884
1885         hr = IFileDialogCustomize_RemoveAllControlItems(pfdc, i);
1886         ok(hr == E_NOTIMPL, "got 0x%08x.\n", hr);
1887
1888         for(j = 0; j < 10; j++)
1889         {
1890             hr = IFileDialogCustomize_RemoveControlItem(pfdc, i, j);
1891             ok(hr == S_OK, "got 0x%08x.\n", hr);
1892         }
1893     }
1894
1895     IFileDialogCustomize_Release(pfdc);
1896     ref = IFileDialog_Release(pfod);
1897     ok(!ref, "Refcount not zero (%d).\n", ref);
1898 }
1899
1900 START_TEST(itemdlg)
1901 {
1902     OleInitialize(NULL);
1903     init_function_pointers();
1904
1905     if(test_instantiation())
1906     {
1907         test_basics();
1908         test_advise();
1909         test_filename();
1910         test_customize();
1911     }
1912     else
1913         skip("Skipping all Item Dialog tests.\n");
1914
1915     OleUninitialize();
1916 }