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
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)
53 pDlg
= HeapAlloc(GetProcessHeap(), 0, (sizeof(PAGESETUPDLGA
)) * 2);
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
);
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)
116 CHAR buffer
[MAX_PATH
];
120 pDlg
= HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGA
)) * 2);
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
);
164 ok(pDlg
->hDevNames
!= NULL
, "(expected '!= NULL')\n");
165 pDevNames
= GlobalLock(pDlg
->hDevNames
);
166 ok(pDevNames
!= NULL
, "(expected '!= NULL')\n");
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
, '.');
186 ok( ptr
== NULL
, "got %p for '%s' (expected NULL for no extension)\n", ptr
, driver
);
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];
220 /* PrintDlgEx not present before w2k */
222 skip("PrintDlgExW not available\n");
226 /* Set CommDlgExtendedError != 0 */
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);
238 /* lStructSize must be exact */
239 ZeroMemory(pDlg
, sizeof(PRINTDLGEXW
));
240 pDlg
->lStructSize
= sizeof(PRINTDLGEXW
) - 1;
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;
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
);
315 skip("No printer configured.\n");
316 HeapFree(GetProcessHeap(), 0, pDlg
);
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
);
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 /* this works: PD_RETURNDC with PD_RETURNDEFAULT */
348 ZeroMemory(pDlg
, sizeof(PRINTDLGEXW
));
349 pDlg
->lStructSize
= sizeof(PRINTDLGEXW
);
350 pDlg
->hwndOwner
= GetDesktopWindow();
351 pDlg
->Flags
= PD_RETURNDEFAULT
| PD_NOWARNING
| PD_NOPAGENUMS
| PD_RETURNDC
;
352 pDlg
->nStartPage
= START_PAGE_GENERAL
;
353 res
= pPrintDlgExW(pDlg
);
354 ok(res
== S_OK
, "got 0x%x (expected S_OK)\n", res
);
355 ok(pDlg
->hDC
!= NULL
, "HDC missing for PD_RETURNDC\n");
356 GlobalFree(pDlg
->hDevMode
);
357 GlobalFree(pDlg
->hDevNames
);
360 /* this works: PD_RETURNIC with PD_RETURNDEFAULT */
361 ZeroMemory(pDlg
, sizeof(PRINTDLGEXW
));
362 pDlg
->lStructSize
= sizeof(PRINTDLGEXW
);
363 pDlg
->hwndOwner
= GetDesktopWindow();
364 pDlg
->Flags
= PD_RETURNDEFAULT
| PD_NOWARNING
| PD_NOPAGENUMS
| PD_RETURNIC
;
365 pDlg
->nStartPage
= START_PAGE_GENERAL
;
366 res
= pPrintDlgExW(pDlg
);
367 ok(res
== S_OK
, "got 0x%x (expected S_OK)\n", res
);
368 ok(pDlg
->hDC
!= NULL
, "HDC missing for PD_RETURNIC\n");
369 GlobalFree(pDlg
->hDevMode
);
370 GlobalFree(pDlg
->hDevNames
);
373 HeapFree(GetProcessHeap(), 0, pDlg
);
378 static BOOL abort_proc_called
= FALSE
;
379 static BOOL CALLBACK
abort_proc(HDC hdc
, int error
) { return abort_proc_called
= TRUE
; }
380 static void test_abort_proc(void)
383 RECT rect
= {0, 0, 100, 100};
384 DOCINFOA doc_info
= {0};
386 char filename
[MAX_PATH
];
389 if (!GetTempFileNameA(".", "prn", 0, filename
))
391 skip("Failed to create a temporary file name\n");
395 pd
.lStructSize
= sizeof(pd
);
396 pd
.Flags
= PD_RETURNDEFAULT
| PD_ALLPAGES
| PD_RETURNDC
| PD_PRINTTOFILE
;
403 skip("No default printer available.\n");
406 GlobalFree(pd
.hDevMode
);
407 GlobalFree(pd
.hDevNames
);
409 ok(pd
.hDC
!= NULL
, "PrintDlg didn't return a DC.\n");
410 if (!(print_dc
= pd
.hDC
))
413 ok(SetAbortProc(print_dc
, abort_proc
) > 0, "SetAbortProc failed\n");
414 ok(!abort_proc_called
, "AbortProc got called unexpectedly by SetAbortProc.\n");
415 abort_proc_called
= FALSE
;
417 doc_info
.cbSize
= sizeof(doc_info
);
418 doc_info
.lpszDocName
= "Some document";
419 doc_info
.lpszOutput
= filename
;
421 job_id
= StartDocA(print_dc
, &doc_info
);
424 GetLastError() == ERROR_SPL_NO_STARTDOC
, /* Vista can fail with this error when using the XPS driver */
425 "StartDocA failed ret %d gle %d\n", job_id
, GetLastError());
429 skip("StartDoc failed\n");
433 /* StartDoc may or may not call abort proc */
435 abort_proc_called
= FALSE
;
436 ok(StartPage(print_dc
) > 0, "StartPage failed\n");
437 ok(!abort_proc_called
, "AbortProc got called unexpectedly by StartPage.\n");
438 abort_proc_called
= FALSE
;
440 /* following functions sometimes call abort proc too */
441 ok(FillRect(print_dc
, &rect
, (HBRUSH
)(COLOR_BACKGROUND
+ 1)), "FillRect failed\n");
442 ok(EndPage(print_dc
) > 0, "EndPage failed\n");
443 ok(EndDoc(print_dc
) > 0, "EndDoc failed\n");
445 abort_proc_called
= FALSE
;
446 ok(DeleteDC(print_dc
), "DeleteDC failed\n");
447 ok(!abort_proc_called
, "AbortProc got called unexpectedly by DeleteDC.\n");
448 abort_proc_called
= FALSE
;
451 SetLastError(0xdeadbeef);
452 if(!DeleteFileA(filename
))
453 trace("Failed to delete temporary file (err = %x)\n", GetLastError());
456 /* ########################### */
460 hcomdlg32
= GetModuleHandleA("comdlg32.dll");
461 pPrintDlgExW
= (void *) GetProcAddress(hcomdlg32
, "PrintDlgExW");
463 test_PageSetupDlgA();