quartz: Sign-compare warnings fix.
[wine] / dlls / comdlg32 / tests / 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 #include <wine/test.h>
24
25 #include "initguid.h"
26 #include "shlguid.h"
27 #define COBJMACROS
28 #include "shobjidl.h"
29
30 /* ##### */
31
32 static UINT CALLBACK OFNHookProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
33 {
34     LPNMHDR nmh;
35
36     if( msg == WM_NOTIFY)
37     {
38         nmh = (LPNMHDR) lParam;
39         if( nmh->code == CDN_INITDONE)
40         {
41             PostMessage( GetParent(hDlg), WM_COMMAND, IDCANCEL, FALSE);
42         } else if (nmh->code == CDN_FOLDERCHANGE )
43         {
44             char buf[1024];
45             int ret;
46
47             memset(buf, 0x66, sizeof(buf));
48             ret = SendMessage( GetParent(hDlg), CDM_GETFOLDERIDLIST, 5, (LPARAM)buf);
49             ok(ret > 0, "CMD_GETFOLDERIDLIST not implemented\n");
50             if (ret > 5)
51                 ok(buf[0] == 0x66 && buf[1] == 0x66, "CMD_GETFOLDERIDLIST: The buffer was touched on failure\n");
52         }
53     }
54
55     return 0;
56 }
57
58 /* bug 6829 */
59 static void test_DialogCancel(void)
60 {
61     OPENFILENAMEA ofn;
62     BOOL result;
63     char szFileName[MAX_PATH] = "";
64     char szInitialDir[MAX_PATH];
65
66     GetWindowsDirectory(szInitialDir, MAX_PATH);
67
68     ZeroMemory(&ofn, sizeof(ofn));
69
70     ofn.lStructSize = sizeof(ofn);
71     ofn.hwndOwner = NULL;
72     ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
73     ofn.lpstrFile = szFileName;
74     ofn.nMaxFile = MAX_PATH;
75     ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK;
76     ofn.lpstrDefExt = "txt";
77     ofn.lpfnHook = (LPOFNHOOKPROC) OFNHookProc;
78     ofn.lpstrInitialDir = szInitialDir;
79
80     PrintDlgA(NULL);
81     ok(CDERR_INITIALIZATION == CommDlgExtendedError(), "expected %d, got %d\n",
82        CDERR_INITIALIZATION, CommDlgExtendedError());
83
84     result = GetOpenFileNameA(&ofn);
85     ok(0 == result, "expected %d, got %d\n", 0, result);
86     ok(0 == CommDlgExtendedError(), "expected %d, got %d\n", 0,
87        CommDlgExtendedError());
88
89     PrintDlgA(NULL);
90     ok(CDERR_INITIALIZATION == CommDlgExtendedError(), "expected %d, got %d\n",
91               CDERR_INITIALIZATION, CommDlgExtendedError());
92
93     result = GetSaveFileNameA(&ofn);
94     ok(0 == result, "expected %d, got %d\n", 0, result);
95     ok(0 == CommDlgExtendedError(), "expected %d, got %d\n", 0,
96        CommDlgExtendedError());
97
98     PrintDlgA(NULL);
99     ok(CDERR_INITIALIZATION == CommDlgExtendedError(), "expected %d, got %d\n",
100               CDERR_INITIALIZATION, CommDlgExtendedError());
101
102     /* Before passing the ofn to Unicode functions, remove the ANSI strings */
103     ofn.lpstrFilter = NULL;
104     ofn.lpstrInitialDir = NULL;
105     ofn.lpstrDefExt = NULL;
106
107     PrintDlgA(NULL);
108     ok(CDERR_INITIALIZATION == CommDlgExtendedError(), "expected %d, got %d\n",
109               CDERR_INITIALIZATION, CommDlgExtendedError());
110
111     SetLastError(0xdeadbeef);
112     result = GetOpenFileNameW((LPOPENFILENAMEW) &ofn);
113     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
114         skip("GetOpenFileNameW is not implemented\n");
115     else
116     {
117         ok(0 == result, "expected %d, got %d\n", 0, result);
118         ok(0 == CommDlgExtendedError() ||
119            CDERR_INITIALIZATION == CommDlgExtendedError(), /* win9x */
120            "expected %d or %d, got %d\n", 0, CDERR_INITIALIZATION,
121            CommDlgExtendedError());
122     }
123
124     SetLastError(0xdeadbeef);
125     result = GetSaveFileNameW((LPOPENFILENAMEW) &ofn);
126     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
127         skip("GetSaveFileNameW is not implemented\n");
128     else
129     {
130         ok(0 == result, "expected %d, got %d\n", 0, result);
131         ok(0 == CommDlgExtendedError() ||
132            CDERR_INITIALIZATION == CommDlgExtendedError(), /* win9x */
133            "expected %d or %d, got %d\n", 0, CDERR_INITIALIZATION,
134            CommDlgExtendedError());
135     }
136 }
137
138 static UINT CALLBACK create_view_window2_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
139 {
140     if (msg == WM_NOTIFY)
141     {
142         if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
143         {
144             IShellBrowser *shell_browser = (IShellBrowser *)SendMessage(GetParent(dlg), WM_USER + 7 /* WM_GETISHELLBROWSER */, 0, 0);
145             IShellView *shell_view = NULL;
146             IShellView2 *shell_view2 = NULL;
147             SV2CVW2_PARAMS view_params;
148             FOLDERSETTINGS folder_settings;
149             HRESULT hr;
150             RECT rect = {0, 0, 0, 0};
151
152             hr = IShellBrowser_QueryActiveShellView(shell_browser, &shell_view);
153             ok(SUCCEEDED(hr), "QueryActiveShellView returned %#x\n", hr);
154             if (FAILED(hr)) goto cleanup;
155
156             hr = IShellView_QueryInterface(shell_view, &IID_IShellView2, (void **)&shell_view2);
157             if (hr == E_NOINTERFACE)
158             {
159                 skip("IShellView2 not supported\n");
160                 goto cleanup;
161             }
162             ok(SUCCEEDED(hr), "QueryInterface returned %#x\n", hr);
163             if (FAILED(hr)) goto cleanup;
164
165             hr = IShellView2_DestroyViewWindow(shell_view2);
166             ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
167
168             folder_settings.ViewMode = FVM_LIST;
169             folder_settings.fFlags = 0;
170
171             view_params.cbSize = sizeof(view_params);
172             view_params.psvPrev = NULL;
173             view_params.pfs = &folder_settings;
174             view_params.psbOwner = shell_browser;
175             view_params.prcView = &rect;
176             view_params.pvid = NULL;
177             view_params.hwndView = NULL;
178
179             hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
180             ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
181             if (FAILED(hr)) goto cleanup;
182
183             hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
184             ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
185             ok(folder_settings.ViewMode == FVM_LIST, "view mode is %d, expected %d\n", folder_settings.ViewMode, FVM_LIST);
186
187             hr = IShellView2_DestroyViewWindow(shell_view2);
188             ok(SUCCEEDED(hr), "DestroyViewWindow returned %#x\n", hr);
189
190             view_params.pvid = &VID_Details;
191             hr = IShellView2_CreateViewWindow2(shell_view2, &view_params);
192             ok(SUCCEEDED(hr), "CreateViewWindow2 returned %#x\n", hr);
193             if (FAILED(hr)) goto cleanup;
194
195             hr = IShellView2_GetCurrentInfo(shell_view2, &folder_settings);
196             ok(SUCCEEDED(hr), "GetCurrentInfo returned %#x\n", hr);
197             ok(folder_settings.ViewMode == FVM_DETAILS, "view mode is %d, expected %d\n", folder_settings.ViewMode, FVM_DETAILS);
198
199 cleanup:
200             if (shell_view2) IShellView2_Release(shell_view2);
201             if (shell_view) IShellView_Release(shell_view);
202             PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
203         }
204     }
205     return 0;
206 }
207
208 static LONG_PTR WINAPI template_hook(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
209 {
210     if (msg == WM_INITDIALOG)
211     {
212         HWND p,cb;
213         INT sel;
214         p = GetParent(dlg);
215         ok(p!=NULL, "Failed to get parent of template\n");
216         cb = GetDlgItem(p,0x470);
217         ok(cb!=NULL, "Failed to get filter combobox\n");
218         sel = SendMessage(cb, CB_GETCURSEL, 0, 0);
219         ok (sel != -1, "Failed to get selection from filter listbox\n");
220     }
221     if (msg == WM_NOTIFY)
222     {
223         if (((LPNMHDR)lParam)->code == CDN_FOLDERCHANGE)
224             PostMessage(GetParent(dlg), WM_COMMAND, IDCANCEL, 0);
225     }
226     return 0;
227 }
228
229 static void test_create_view_window2(void)
230 {
231     OPENFILENAMEA ofn = {0};
232     char filename[1024] = {0};
233     DWORD ret;
234
235     ofn.lStructSize = sizeof(ofn);
236     ofn.lpstrFile = filename;
237     ofn.nMaxFile = 1042;
238     ofn.lpfnHook = create_view_window2_hook;
239     ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;
240     ret = GetOpenFileNameA(&ofn);
241     ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
242     ret = CommDlgExtendedError();
243     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
244 }
245
246 static void test_create_view_template(void)
247 {
248     OPENFILENAMEA ofn = {0};
249     char filename[1024] = {0};
250     DWORD ret;
251
252     ofn.lStructSize = sizeof(ofn);
253     ofn.lpstrFile = filename;
254     ofn.nMaxFile = 1042;
255     ofn.lpfnHook = (LPOFNHOOKPROC)template_hook;
256     ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER| OFN_ENABLETEMPLATE;
257     ofn.hInstance = GetModuleHandleA(NULL);
258     ofn.lpTemplateName = "template1";
259     ofn.lpstrFilter="text\0*.txt\0All\0*\0\0";
260     ret = GetOpenFileNameA(&ofn);
261     ok(!ret, "GetOpenFileNameA returned %#x\n", ret);
262     ret = CommDlgExtendedError();
263     ok(!ret, "CommDlgExtendedError returned %#x\n", ret);
264 }
265
266 START_TEST(filedlg)
267 {
268     test_DialogCancel();
269     test_create_view_window2();
270     test_create_view_template();
271 }