comdlg32/tests: Test more parameters for PrintDlgEx.
[wine/multimedia.git] / dlls / comdlg32 / tests / printdlg.c
blob2eaf9d49511acff1dbd7d5d4eac58d3fff40c4d1
1 /*
2 * Unit test suite for comdlg32 API functions: printer dialogs
4 * Copyright 2006-2007 Detlef Riekenberg
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.
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.
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
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "objbase.h"
31 #include "cderr.h"
32 #include "commdlg.h"
34 #include "wine/test.h"
36 /* ########################### */
38 static HMODULE hcomdlg32;
39 static HRESULT (WINAPI * pPrintDlgExW)(LPPRINTDLGEXW);
41 /* ########################### */
43 static const CHAR emptyA[] = "";
44 static const CHAR PrinterPortsA[] = "PrinterPorts";
46 /* ########################### */
48 static void test_PageSetupDlgA(void)
50 LPPAGESETUPDLGA pDlg;
51 DWORD res;
53 pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PAGESETUPDLGA)) * 2);
54 if (!pDlg) return;
56 SetLastError(0xdeadbeef);
57 res = PageSetupDlgA(NULL);
58 ok( !res && (CommDlgExtendedError() == CDERR_INITIALIZATION),
59 "returned %u with %u and 0x%x (expected '0' and "
60 "CDERR_INITIALIZATION)\n", res, GetLastError(), CommDlgExtendedError());
62 ZeroMemory(pDlg, sizeof(PAGESETUPDLGA));
63 pDlg->lStructSize = sizeof(PAGESETUPDLGA) -1;
64 SetLastError(0xdeadbeef);
65 res = PageSetupDlgA(pDlg);
66 ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
67 "returned %u with %u and 0x%x (expected '0' and "
68 "CDERR_STRUCTSIZE)\n", res, GetLastError(), CommDlgExtendedError());
70 ZeroMemory(pDlg, sizeof(PAGESETUPDLGA));
71 pDlg->lStructSize = sizeof(PAGESETUPDLGA) +1;
72 pDlg->Flags = PSD_RETURNDEFAULT;
73 SetLastError(0xdeadbeef);
74 res = PageSetupDlgA(pDlg);
75 ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
76 "returned %u with %u and 0x%x (expected '0' and CDERR_STRUCTSIZE)\n",
77 res, GetLastError(), CommDlgExtendedError());
80 ZeroMemory(pDlg, sizeof(PAGESETUPDLGA));
81 pDlg->lStructSize = sizeof(PAGESETUPDLGA);
82 pDlg->Flags = PSD_RETURNDEFAULT | PSD_NOWARNING;
83 SetLastError(0xdeadbeef);
84 res = PageSetupDlgA(pDlg);
85 ok( res || (CommDlgExtendedError() == PDERR_NODEFAULTPRN),
86 "returned %u with %u and 0x%x (expected '!= 0' or '0' and "
87 "PDERR_NODEFAULTPRN)\n", res, GetLastError(), CommDlgExtendedError());
89 if (!res && (CommDlgExtendedError() == PDERR_NODEFAULTPRN)) {
90 skip("No printer configured.\n");
91 HeapFree(GetProcessHeap(), 0, pDlg);
92 return;
95 ok( pDlg->hDevMode && pDlg->hDevNames,
96 "got %p and %p (expected '!= NULL' for both)\n",
97 pDlg->hDevMode, pDlg->hDevNames);
99 GlobalFree(pDlg->hDevMode);
100 GlobalFree(pDlg->hDevNames);
102 HeapFree(GetProcessHeap(), 0, pDlg);
106 /* ########################### */
108 static void test_PrintDlgA(void)
110 DWORD res;
111 LPPRINTDLGA pDlg;
112 DEVNAMES *pDevNames;
113 LPCSTR driver;
114 LPCSTR device;
115 LPCSTR port;
116 CHAR buffer[MAX_PATH];
117 LPSTR ptr;
120 pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGA)) * 2);
121 if (!pDlg) return;
124 /* will crash with unpatched wine */
125 SetLastError(0xdeadbeef);
126 res = PrintDlgA(NULL);
127 ok( !res && (CommDlgExtendedError() == CDERR_INITIALIZATION),
128 "returned %d with 0x%x and 0x%x (expected '0' and "
129 "CDERR_INITIALIZATION)\n", res, GetLastError(), CommDlgExtendedError());
131 ZeroMemory(pDlg, sizeof(PRINTDLGA));
132 pDlg->lStructSize = sizeof(PRINTDLGA) - 1;
133 SetLastError(0xdeadbeef);
134 res = PrintDlgA(pDlg);
135 ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
136 "returned %d with 0x%x and 0x%x (expected '0' and "
137 "CDERR_STRUCTSIZE)\n", res, GetLastError(), CommDlgExtendedError());
139 ZeroMemory(pDlg, sizeof(PRINTDLGA));
140 pDlg->lStructSize = sizeof(PRINTDLGA) + 1;
141 pDlg->Flags = PD_RETURNDEFAULT;
142 SetLastError(0xdeadbeef);
143 res = PrintDlgA(pDlg);
144 ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
145 "returned %u with %u and 0x%x (expected '0' and "
146 "CDERR_STRUCTSIZE)\n", res, GetLastError(), CommDlgExtendedError());
149 ZeroMemory(pDlg, sizeof(PRINTDLGA));
150 pDlg->lStructSize = sizeof(PRINTDLGA);
151 pDlg->Flags = PD_RETURNDEFAULT;
152 SetLastError(0xdeadbeef);
153 res = PrintDlgA(pDlg);
154 ok( res || (CommDlgExtendedError() == PDERR_NODEFAULTPRN),
155 "returned %d with 0x%x and 0x%x (expected '!= 0' or '0' and "
156 "PDERR_NODEFAULTPRN)\n", res, GetLastError(), CommDlgExtendedError());
158 if (!res && (CommDlgExtendedError() == PDERR_NODEFAULTPRN)) {
159 skip("No printer configured.\n");
160 HeapFree(GetProcessHeap(), 0, pDlg);
161 return;
164 ok(pDlg->hDevNames != NULL, "(expected '!= NULL')\n");
165 pDevNames = GlobalLock(pDlg->hDevNames);
166 ok(pDevNames != NULL, "(expected '!= NULL')\n");
168 if (pDevNames) {
169 ok(pDevNames->wDriverOffset, "(expected '!= 0' for wDriverOffset)\n");
170 ok(pDevNames->wDeviceOffset, "(expected '!= 0' for wDeviceOffset)\n");
171 ok(pDevNames->wOutputOffset, "(expected '!= 0' for wOutputOffset)\n");
172 ok(pDevNames->wDefault == DN_DEFAULTPRN, "got 0x%x (expected DN_DEFAULTPRN)\n", pDevNames->wDefault);
174 driver = (LPCSTR)pDevNames + pDevNames->wDriverOffset;
175 device = (LPCSTR)pDevNames + pDevNames->wDeviceOffset;
176 port = (LPCSTR)pDevNames + pDevNames->wOutputOffset;
177 trace("driver '%s' device '%s' port '%s'\n", driver, device, port);
179 /* The Driver Entry does not include a Path */
180 ptr = strrchr(driver, '\\');
181 ok( ptr == NULL, "got %p for '%s' (expected NULL for a simple name)\n", ptr, driver);
183 /* The Driver Entry does not have an extension (fixed to ".drv") */
184 ptr = strrchr(driver, '.');
185 todo_wine {
186 ok( ptr == NULL, "got %p for '%s' (expected NULL for no extension)\n", ptr, driver);
190 buffer[0] = '\0';
191 SetLastError(0xdeadbeef);
192 res = GetProfileStringA(PrinterPortsA, device, emptyA, buffer, sizeof(buffer));
193 ptr = strchr(buffer, ',');
194 ok( (res > 1) && (ptr != NULL),
195 "got %u with %u and %p for '%s' (expected '>1' and '!= NULL')\n",
196 res, GetLastError(), ptr, buffer);
198 if (ptr) ptr[0] = '\0';
199 ok( lstrcmpiA(driver, buffer) == 0,
200 "got driver '%s' (expected '%s')\n", driver, buffer);
203 GlobalUnlock(pDlg->hDevNames);
205 GlobalFree(pDlg->hDevMode);
206 GlobalFree(pDlg->hDevNames);
207 HeapFree(GetProcessHeap(), 0, pDlg);
211 /* ########################### */
213 static void test_PrintDlgExW(void)
215 PRINTPAGERANGE pagerange[2];
216 LPPRINTDLGEXW pDlg;
217 DEVNAMES *dn;
218 HRESULT res;
220 /* PrintDlgEx not present before w2k */
221 if (!pPrintDlgExW) {
222 skip("PrintDlgExW not available\n");
223 return;
226 /* Set CommDlgExtendedError != 0 */
227 PrintDlg(NULL);
228 SetLastError(0xdeadbeef);
229 res = pPrintDlgExW(NULL);
230 ok( (res == E_INVALIDARG),
231 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
232 res, GetLastError(), CommDlgExtendedError());
235 pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGEXW)) + 8);
236 if (!pDlg) return;
238 /* lStructSize must be exact */
239 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
240 pDlg->lStructSize = sizeof(PRINTDLGEXW) - 1;
241 PrintDlg(NULL);
242 SetLastError(0xdeadbeef);
243 res = pPrintDlgExW(pDlg);
244 ok( (res == E_INVALIDARG),
245 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
246 res, GetLastError(), CommDlgExtendedError());
249 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
250 pDlg->lStructSize = sizeof(PRINTDLGEXW) + 1;
251 PrintDlg(NULL);
252 SetLastError(0xdeadbeef);
253 res = pPrintDlgExW(pDlg);
254 ok( (res == E_INVALIDARG),
255 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
256 res, GetLastError(), CommDlgExtendedError());
259 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
260 pDlg->lStructSize = sizeof(PRINTDLGEXW);
261 SetLastError(0xdeadbeef);
262 res = pPrintDlgExW(pDlg);
263 ok( (res == E_HANDLE),
264 "got 0x%x with %u and %u (expected 'E_HANDLE')\n",
265 res, GetLastError(), CommDlgExtendedError());
267 /* nStartPage must be START_PAGE_GENERAL for the general page or a valid property sheet index */
268 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
269 pDlg->lStructSize = sizeof(PRINTDLGEXW);
270 pDlg->hwndOwner = GetDesktopWindow();
271 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING | PD_NOPAGENUMS;
272 res = pPrintDlgExW(pDlg);
273 ok((res == E_INVALIDARG), "got 0x%x (expected 'E_INVALIDARG')\n", res);
275 /* Use PD_NOPAGENUMS or set nMaxPageRanges and lpPageRanges */
276 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
277 pDlg->lStructSize = sizeof(PRINTDLGEXW);
278 pDlg->hwndOwner = GetDesktopWindow();
279 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING;
280 pDlg->nStartPage = START_PAGE_GENERAL;
281 res = pPrintDlgExW(pDlg);
282 ok((res == E_INVALIDARG), "got 0x%x (expected 'E_INVALIDARG')\n", res);
284 /* this is invalid: a valid lpPageRanges with 0 for nMaxPageRanges */
285 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
286 pDlg->lStructSize = sizeof(PRINTDLGEXW);
287 pDlg->hwndOwner = GetDesktopWindow();
288 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING;
289 pDlg->lpPageRanges = pagerange;
290 pDlg->nStartPage = START_PAGE_GENERAL;
291 res = pPrintDlgExW(pDlg);
292 ok((res == E_INVALIDARG), "got 0x%x (expected 'E_INVALIDARG')\n", res);
294 /* this is invalid: NULL for lpPageRanges with a valid nMaxPageRanges */
295 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
296 pDlg->lStructSize = sizeof(PRINTDLGEXW);
297 pDlg->hwndOwner = GetDesktopWindow();
298 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING;
299 pDlg->nMaxPageRanges = 1;
300 pDlg->nStartPage = START_PAGE_GENERAL;
301 res = pPrintDlgExW(pDlg);
302 ok((res == E_INVALIDARG), "got 0x%x (expected 'E_INVALIDARG')\n", res);
304 /* this works: lpPageRanges with a valid nMaxPageRanges */
305 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
306 pDlg->lStructSize = sizeof(PRINTDLGEXW);
307 pDlg->hwndOwner = GetDesktopWindow();
308 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING;
309 pDlg->nMaxPageRanges = 1;
310 pDlg->lpPageRanges = pagerange;
311 pDlg->nStartPage = START_PAGE_GENERAL;
312 res = pPrintDlgExW(pDlg);
313 if (res == E_FAIL)
315 skip("No printer configured.\n");
316 HeapFree(GetProcessHeap(), 0, pDlg);
317 return;
320 ok(res == S_OK, "got 0x%x (expected S_OK)\n", res);
322 dn = GlobalLock(pDlg->hDevNames);
323 ok(dn != NULL, "expected '!= NULL' for GlobalLock(%p)\n",pDlg->hDevNames);
324 if (dn)
326 ok(dn->wDriverOffset, "(expected '!= 0' for wDriverOffset)\n");
327 ok(dn->wDeviceOffset, "(expected '!= 0' for wDeviceOffset)\n");
328 ok(dn->wOutputOffset, "(expected '!= 0' for wOutputOffset)\n");
329 ok(dn->wDefault == DN_DEFAULTPRN, "got 0x%x (expected DN_DEFAULTPRN)\n", dn->wDefault);
331 GlobalUnlock(pDlg->hDevNames);
333 GlobalFree(pDlg->hDevMode);
334 GlobalFree(pDlg->hDevNames);
336 /* this works also: PD_NOPAGENUMS */
337 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
338 pDlg->lStructSize = sizeof(PRINTDLGEXW);
339 pDlg->hwndOwner = GetDesktopWindow();
340 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING | PD_NOPAGENUMS;
341 pDlg->nStartPage = START_PAGE_GENERAL;
342 res = pPrintDlgExW(pDlg);
343 ok(res == S_OK, "got 0x%x (expected S_OK)\n", res);
344 GlobalFree(pDlg->hDevMode);
345 GlobalFree(pDlg->hDevNames);
347 HeapFree(GetProcessHeap(), 0, pDlg);
348 return;
352 static BOOL abort_proc_called = FALSE;
353 static BOOL CALLBACK abort_proc(HDC hdc, int error) { return abort_proc_called = TRUE; }
354 static void test_abort_proc(void)
356 HDC print_dc;
357 RECT rect = {0, 0, 100, 100};
358 DOCINFOA doc_info = {0};
359 PRINTDLGA pd = {0};
360 char filename[MAX_PATH];
361 int job_id;
363 if (!GetTempFileNameA(".", "prn", 0, filename))
365 skip("Failed to create a temporary file name\n");
366 return;
369 pd.lStructSize = sizeof(pd);
370 pd.Flags = PD_RETURNDEFAULT | PD_ALLPAGES | PD_RETURNDC | PD_PRINTTOFILE;
371 pd.nFromPage = 1;
372 pd.nToPage = 1;
373 pd.nCopies = 1;
375 if (!PrintDlgA(&pd))
377 skip("No default printer available.\n");
378 goto end;
380 GlobalFree(pd.hDevMode);
381 GlobalFree(pd.hDevNames);
383 ok(pd.hDC != NULL, "PrintDlg didn't return a DC.\n");
384 if (!(print_dc = pd.hDC))
385 goto end;
387 ok(SetAbortProc(print_dc, abort_proc) > 0, "SetAbortProc failed\n");
388 ok(!abort_proc_called, "AbortProc got called unexpectedly by SetAbortProc.\n");
389 abort_proc_called = FALSE;
391 doc_info.cbSize = sizeof(doc_info);
392 doc_info.lpszDocName = "Some document";
393 doc_info.lpszOutput = filename;
395 job_id = StartDocA(print_dc, &doc_info);
397 ok(job_id > 0 ||
398 GetLastError() == ERROR_SPL_NO_STARTDOC, /* Vista can fail with this error when using the XPS driver */
399 "StartDocA failed ret %d gle %d\n", job_id, GetLastError());
401 if(job_id <= 0)
403 skip("StartDoc failed\n");
404 goto end;
407 /* StartDoc may or may not call abort proc */
409 abort_proc_called = FALSE;
410 ok(StartPage(print_dc) > 0, "StartPage failed\n");
411 ok(!abort_proc_called, "AbortProc got called unexpectedly by StartPage.\n");
412 abort_proc_called = FALSE;
414 /* following functions sometimes call abort proc too */
415 ok(FillRect(print_dc, &rect, (HBRUSH)(COLOR_BACKGROUND + 1)), "FillRect failed\n");
416 ok(EndPage(print_dc) > 0, "EndPage failed\n");
417 ok(EndDoc(print_dc) > 0, "EndDoc failed\n");
419 abort_proc_called = FALSE;
420 ok(DeleteDC(print_dc), "DeleteDC failed\n");
421 ok(!abort_proc_called, "AbortProc got called unexpectedly by DeleteDC.\n");
422 abort_proc_called = FALSE;
424 end:
425 SetLastError(0xdeadbeef);
426 if(!DeleteFileA(filename))
427 trace("Failed to delete temporary file (err = %x)\n", GetLastError());
430 /* ########################### */
432 START_TEST(printdlg)
434 hcomdlg32 = GetModuleHandleA("comdlg32.dll");
435 pPrintDlgExW = (void *) GetProcAddress(hcomdlg32, "PrintDlgExW");
437 test_PageSetupDlgA();
438 test_PrintDlgA();
439 test_PrintDlgExW();
440 test_abort_proc();