ntdll: The FileMailslotSetInformation and FileCompletionInformation cases of NtSetInf...
[wine] / dlls / comdlg32 / tests / printdlg.c
1 /* 
2  * Unit test suite for comdlg32 API functions: printer dialogs
3  *
4  * Copyright 2006-2007 Detlef Riekenberg
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 <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29
30 #include "cderr.h"
31 #include "commdlg.h"
32
33 #include "wine/test.h"
34
35 /* ########################### */
36
37 static HMODULE  hcomdlg32;
38 static HRESULT (WINAPI * pPrintDlgExA)(LPPRINTDLGEXA);
39 static HRESULT (WINAPI * pPrintDlgExW)(LPPRINTDLGEXW);
40
41 /* ########################### */
42
43 static const CHAR emptyA[] = "";
44 static const CHAR PrinterPortsA[] = "PrinterPorts";
45
46 /* ########################### */
47
48 static LPCSTR load_functions(void)
49 {
50     LPCSTR  ptr;
51
52     ptr = "comdlg32.dll";
53     hcomdlg32 = LoadLibraryA(ptr);
54     if (!hcomdlg32) return ptr;
55
56     ptr = "PrintDlgExA";
57     pPrintDlgExA = (void *) GetProcAddress(hcomdlg32, ptr);
58     if (!pPrintDlgExA) return ptr;
59
60     ptr = "PrintDlgExW";
61     pPrintDlgExW = (void *) GetProcAddress(hcomdlg32, ptr);
62     if (!pPrintDlgExW) return ptr;
63
64     return NULL;
65
66 }
67
68 /* ########################### */
69
70 static void test_PageSetupDlgA(void)
71 {
72     LPPAGESETUPDLGA pDlg;
73     DWORD res;
74
75     pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PAGESETUPDLGA)) * 2);
76     if (!pDlg) return;
77
78     SetLastError(0xdeadbeef);
79     res = PageSetupDlgA(NULL);
80     ok( !res && (CommDlgExtendedError() == CDERR_INITIALIZATION),
81         "returned %u with %u and 0x%x (expected '0' and "
82         "CDERR_INITIALIZATION)\n", res, GetLastError(), CommDlgExtendedError());
83
84     ZeroMemory(pDlg, sizeof(PAGESETUPDLGA));
85     pDlg->lStructSize = sizeof(PAGESETUPDLGA) -1;
86     SetLastError(0xdeadbeef);
87     res = PageSetupDlgA(pDlg);
88     ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
89         "returned %u with %u and 0x%x (expected '0' and "
90         "CDERR_STRUCTSIZE)\n", res, GetLastError(), CommDlgExtendedError());
91
92     ZeroMemory(pDlg, sizeof(PAGESETUPDLGA));
93     pDlg->lStructSize = sizeof(PAGESETUPDLGA) +1;
94     pDlg->Flags = PSD_RETURNDEFAULT;
95     SetLastError(0xdeadbeef);
96     res = PageSetupDlgA(pDlg);
97     ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
98         "returned %u with %u and 0x%x (expected '0' and CDERR_STRUCTSIZE)\n",
99         res, GetLastError(), CommDlgExtendedError());
100
101
102     ZeroMemory(pDlg, sizeof(PAGESETUPDLGA));
103     pDlg->lStructSize = sizeof(PAGESETUPDLGA);
104     pDlg->Flags = PSD_RETURNDEFAULT | PSD_NOWARNING;
105     SetLastError(0xdeadbeef);
106     res = PageSetupDlgA(pDlg);
107     ok( res || (CommDlgExtendedError() == PDERR_NODEFAULTPRN),
108         "returned %u with %u and 0x%x (expected '!= 0' or '0' and "
109         "PDERR_NODEFAULTPRN)\n", res, GetLastError(), CommDlgExtendedError());
110
111     if (!res && (CommDlgExtendedError() == PDERR_NODEFAULTPRN)) {
112         skip("No printer configured.\n");
113         HeapFree(GetProcessHeap(), 0, pDlg);
114         return;
115     }
116
117     ok( pDlg->hDevMode && pDlg->hDevNames,
118         "got %p and %p (expected '!= NULL' for both)\n",
119         pDlg->hDevMode, pDlg->hDevNames);
120
121     GlobalFree(pDlg->hDevMode);
122     GlobalFree(pDlg->hDevNames);
123
124     HeapFree(GetProcessHeap(), 0, pDlg);
125
126 }
127
128 /* ########################### */
129
130 static void test_PrintDlgA(void)
131 {
132     DWORD       res;
133     LPPRINTDLGA pDlg;
134     DEVNAMES    *pDevNames;
135     LPCSTR driver;
136     LPCSTR device;
137     LPCSTR port;
138     CHAR   buffer[MAX_PATH];
139     LPSTR  ptr;
140
141
142     pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGA)) * 2);
143     if (!pDlg) return;
144
145
146     /* will crash with unpatched wine */
147     SetLastError(0xdeadbeef);
148     res = PrintDlgA(NULL);
149     ok( !res && (CommDlgExtendedError() == CDERR_INITIALIZATION),
150         "returned %d with 0x%x and 0x%x (expected '0' and "
151         "CDERR_INITIALIZATION)\n", res, GetLastError(), CommDlgExtendedError());
152
153     ZeroMemory(pDlg, sizeof(PRINTDLGA));
154     pDlg->lStructSize = sizeof(PRINTDLGA) - 1;
155     SetLastError(0xdeadbeef);
156     res = PrintDlgA(pDlg);
157     ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
158         "returned %d with 0x%x and 0x%x (expected '0' and "
159         "CDERR_STRUCTSIZE)\n", res, GetLastError(), CommDlgExtendedError());
160
161     ZeroMemory(pDlg, sizeof(PRINTDLGA));
162     pDlg->lStructSize = sizeof(PRINTDLGA) + 1;
163     pDlg->Flags = PD_RETURNDEFAULT;
164     SetLastError(0xdeadbeef);
165     res = PrintDlgA(pDlg);
166     ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
167         "returned %u with %u and 0x%x (expected '0' and "
168         "CDERR_STRUCTSIZE)\n", res, GetLastError(), CommDlgExtendedError());
169
170
171     ZeroMemory(pDlg, sizeof(PRINTDLGA));
172     pDlg->lStructSize = sizeof(PRINTDLGA);
173     pDlg->Flags = PD_RETURNDEFAULT;
174     SetLastError(0xdeadbeef);
175     res = PrintDlgA(pDlg);
176     ok( res || (CommDlgExtendedError() == PDERR_NODEFAULTPRN),
177         "returned %d with 0x%x and 0x%x (expected '!= 0' or '0' and "
178         "PDERR_NODEFAULTPRN)\n", res, GetLastError(), CommDlgExtendedError());
179
180     if (!res && (CommDlgExtendedError() == PDERR_NODEFAULTPRN)) {
181         skip("No printer configured.\n");
182         HeapFree(GetProcessHeap(), 0, pDlg);
183         return;
184     }
185
186     ok(pDlg->hDevNames != NULL, "(expected '!= NULL')\n");
187     pDevNames = GlobalLock(pDlg->hDevNames);
188     ok(pDevNames != NULL, "(expected '!= NULL')\n");
189
190     if (pDevNames) {
191         ok(pDevNames->wDriverOffset, "(expected '!= 0' for wDriverOffset)\n");
192         ok(pDevNames->wDeviceOffset, "(expected '!= 0' for wDeviceOffset)\n");
193         ok(pDevNames->wOutputOffset, "(expected '!= 0' for wOutputOffset)\n");
194         ok(pDevNames->wDefault == DN_DEFAULTPRN, "got 0x%x (expected DN_DEFAULTPRN)\n", pDevNames->wDefault);
195
196         driver = (LPCSTR)pDevNames + pDevNames->wDriverOffset;
197         device = (LPCSTR)pDevNames + pDevNames->wDeviceOffset;
198         port = (LPCSTR)pDevNames + pDevNames->wOutputOffset;
199         trace("driver '%s' device '%s' port '%s'\n", driver, device, port);
200
201         /* The Driver Entry does not include a Path */
202         ptr = strrchr(driver, '\\');
203         todo_wine {
204         ok( ptr == NULL, "got %p for '%s' (expected NULL for a simple name)\n", ptr, driver);
205         }
206
207         /* The Driver Entry does not have an extension (fixed to ".drv") */
208         ptr = strrchr(driver, '.');
209         todo_wine {
210         ok( ptr == NULL, "got %p for '%s' (expected NULL for no extension)\n", ptr, driver);
211         }
212
213
214         buffer[0] = '\0';
215         SetLastError(0xdeadbeef);
216         res = GetProfileStringA(PrinterPortsA, device, emptyA, buffer, sizeof(buffer));
217         ptr = strchr(buffer, ',');
218         todo_wine {
219         ok( (res > 1) && (ptr != NULL),
220             "got %u with %u and %p for '%s' (expected '>1' and '!= NULL')\n",
221             res, GetLastError(), ptr, buffer);
222         }
223
224         if (ptr) ptr[0] = '\0';
225         todo_wine {
226         ok( lstrcmpiA(driver, buffer) == 0,
227             "got driver '%s' (expected '%s')\n", driver, buffer);
228         }
229
230     }
231
232     GlobalUnlock(pDlg->hDevNames);
233
234     GlobalFree(pDlg->hDevMode);
235     GlobalFree(pDlg->hDevNames);
236     HeapFree(GetProcessHeap(), 0, pDlg);
237
238 }
239
240 /* ########################### */
241
242 static void test_PrintDlgExW(void)
243 {
244     LPPRINTDLGEXW pDlg;
245     HRESULT res;
246
247     /* Set CommDlgExtendedError != 0 */
248     PrintDlg(NULL);
249     SetLastError(0xdeadbeef);
250     res = pPrintDlgExW(NULL);
251     ok( (res == E_INVALIDARG),
252         "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
253         res, GetLastError(), CommDlgExtendedError());
254
255
256     pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGEXW)) + 8);
257     if (!pDlg) return;
258
259     /* lStructSize must be exact */
260     ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
261     pDlg->lStructSize = sizeof(PRINTDLGEXW) - 1;
262     PrintDlg(NULL);
263     SetLastError(0xdeadbeef);
264     res = pPrintDlgExW(pDlg);
265     ok( (res == E_INVALIDARG),
266         "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
267         res, GetLastError(), CommDlgExtendedError());
268
269
270     ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
271     pDlg->lStructSize = sizeof(PRINTDLGEXW) + 1;
272     PrintDlg(NULL);
273     SetLastError(0xdeadbeef);
274     res = pPrintDlgExW(pDlg);
275     ok( (res == E_INVALIDARG),
276         "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
277         res, GetLastError(), CommDlgExtendedError());
278
279
280     ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
281     pDlg->lStructSize = sizeof(PRINTDLGEXW);
282     SetLastError(0xdeadbeef);
283     res = pPrintDlgExW(pDlg);
284     ok( (res == E_HANDLE),
285         "got 0x%x with %u and %u (expected 'E_HANDLE')\n",
286         res, GetLastError(), CommDlgExtendedError());
287
288
289     HeapFree(GetProcessHeap(), 0, pDlg);
290     return;
291
292 }
293
294 /* ########################### */
295
296 START_TEST(printdlg)
297 {
298     LPCSTR  ptr;
299
300     ptr = load_functions();
301
302     test_PageSetupDlgA();
303     test_PrintDlgA();
304
305     /* PrintDlgEx not present before w2k */
306     if (ptr) {
307         skip("%s\n", ptr);
308         return;
309     }
310
311     test_PrintDlgExW();
312 }