Release 1.6-rc2.
[wine/testsucceed.git] / dlls / comdlg32 / tests / printdlg.c
blob83be8934778dd09e8ffd04ad4502a96dca1266a4
1 /*
2 * Unit test suite for comdlg32 API functions: printer dialogs
4 * Copyright 2006-2007 Detlef Riekenberg
5 * Copyright 2013 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define COBJMACROS
24 #define CONST_VTABLE
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "objbase.h"
36 #include "cderr.h"
37 #include "commdlg.h"
39 #include "wine/test.h"
41 /* ########################### */
43 extern const IID IID_IObjectWithSite;
45 static HMODULE hcomdlg32;
46 static HRESULT (WINAPI * pPrintDlgExW)(LPPRINTDLGEXW);
48 /* ########################### */
50 static const CHAR emptyA[] = "";
51 static const CHAR PrinterPortsA[] = "PrinterPorts";
53 static const char *debugstr_guid(const GUID *guid)
55 static char buf[50];
57 if (!guid) return "(null)";
58 sprintf(buf, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
59 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
60 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
61 guid->Data4[5], guid->Data4[6], guid->Data4[7]);
62 return buf;
65 /* ########################### */
67 static void test_PageSetupDlgA(void)
69 LPPAGESETUPDLGA pDlg;
70 DWORD res;
72 pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PAGESETUPDLGA)) * 2);
73 if (!pDlg) return;
75 SetLastError(0xdeadbeef);
76 res = PageSetupDlgA(NULL);
77 ok( !res && (CommDlgExtendedError() == CDERR_INITIALIZATION),
78 "returned %u with %u and 0x%x (expected '0' and "
79 "CDERR_INITIALIZATION)\n", res, GetLastError(), CommDlgExtendedError());
81 ZeroMemory(pDlg, sizeof(PAGESETUPDLGA));
82 pDlg->lStructSize = sizeof(PAGESETUPDLGA) -1;
83 SetLastError(0xdeadbeef);
84 res = PageSetupDlgA(pDlg);
85 ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
86 "returned %u with %u and 0x%x (expected '0' and "
87 "CDERR_STRUCTSIZE)\n", res, GetLastError(), CommDlgExtendedError());
89 ZeroMemory(pDlg, sizeof(PAGESETUPDLGA));
90 pDlg->lStructSize = sizeof(PAGESETUPDLGA) +1;
91 pDlg->Flags = PSD_RETURNDEFAULT;
92 SetLastError(0xdeadbeef);
93 res = PageSetupDlgA(pDlg);
94 ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
95 "returned %u with %u and 0x%x (expected '0' and CDERR_STRUCTSIZE)\n",
96 res, GetLastError(), CommDlgExtendedError());
99 ZeroMemory(pDlg, sizeof(PAGESETUPDLGA));
100 pDlg->lStructSize = sizeof(PAGESETUPDLGA);
101 pDlg->Flags = PSD_RETURNDEFAULT | PSD_NOWARNING;
102 SetLastError(0xdeadbeef);
103 res = PageSetupDlgA(pDlg);
104 ok( res || (CommDlgExtendedError() == PDERR_NODEFAULTPRN),
105 "returned %u with %u and 0x%x (expected '!= 0' or '0' and "
106 "PDERR_NODEFAULTPRN)\n", res, GetLastError(), CommDlgExtendedError());
108 if (!res && (CommDlgExtendedError() == PDERR_NODEFAULTPRN)) {
109 skip("No printer configured.\n");
110 HeapFree(GetProcessHeap(), 0, pDlg);
111 return;
114 ok( pDlg->hDevMode && pDlg->hDevNames,
115 "got %p and %p (expected '!= NULL' for both)\n",
116 pDlg->hDevMode, pDlg->hDevNames);
118 GlobalFree(pDlg->hDevMode);
119 GlobalFree(pDlg->hDevNames);
121 HeapFree(GetProcessHeap(), 0, pDlg);
125 /* ########################### */
127 static UINT_PTR CALLBACK print_hook_proc(HWND hdlg, UINT msg, WPARAM wp, LPARAM lp)
129 if (msg == WM_INITDIALOG)
131 SetDlgItemInt(hdlg, edt3, 1234, FALSE);
132 PostMessage(hdlg, WM_COMMAND, IDOK, FALSE);
134 return 0;
137 static void test_PrintDlgA(void)
139 DWORD res, n_copies = 0;
140 LPPRINTDLGA pDlg;
141 DEVNAMES *pDevNames;
142 LPCSTR driver;
143 LPCSTR device;
144 LPCSTR port;
145 CHAR buffer[MAX_PATH];
146 LPSTR ptr;
147 DEVMODE *dm;
149 pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGA)) * 2);
150 if (!pDlg) return;
153 /* will crash with unpatched wine */
154 SetLastError(0xdeadbeef);
155 res = PrintDlgA(NULL);
156 ok( !res && (CommDlgExtendedError() == CDERR_INITIALIZATION),
157 "returned %d with 0x%x and 0x%x (expected '0' and "
158 "CDERR_INITIALIZATION)\n", res, GetLastError(), CommDlgExtendedError());
160 ZeroMemory(pDlg, sizeof(PRINTDLGA));
161 pDlg->lStructSize = sizeof(PRINTDLGA) - 1;
162 SetLastError(0xdeadbeef);
163 res = PrintDlgA(pDlg);
164 ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
165 "returned %d with 0x%x and 0x%x (expected '0' and "
166 "CDERR_STRUCTSIZE)\n", res, GetLastError(), CommDlgExtendedError());
168 ZeroMemory(pDlg, sizeof(PRINTDLGA));
169 pDlg->lStructSize = sizeof(PRINTDLGA) + 1;
170 pDlg->Flags = PD_RETURNDEFAULT;
171 SetLastError(0xdeadbeef);
172 res = PrintDlgA(pDlg);
173 ok( !res && (CommDlgExtendedError() == CDERR_STRUCTSIZE),
174 "returned %u with %u and 0x%x (expected '0' and "
175 "CDERR_STRUCTSIZE)\n", res, GetLastError(), CommDlgExtendedError());
178 ZeroMemory(pDlg, sizeof(PRINTDLGA));
179 pDlg->lStructSize = sizeof(PRINTDLGA);
180 pDlg->Flags = PD_RETURNDEFAULT;
181 SetLastError(0xdeadbeef);
182 res = PrintDlgA(pDlg);
183 ok( res || (CommDlgExtendedError() == PDERR_NODEFAULTPRN),
184 "returned %d with 0x%x and 0x%x (expected '!= 0' or '0' and "
185 "PDERR_NODEFAULTPRN)\n", res, GetLastError(), CommDlgExtendedError());
187 if (!res && (CommDlgExtendedError() == PDERR_NODEFAULTPRN)) {
188 skip("No printer configured.\n");
189 HeapFree(GetProcessHeap(), 0, pDlg);
190 return;
193 ok(pDlg->hDevNames != NULL, "(expected '!= NULL')\n");
194 pDevNames = GlobalLock(pDlg->hDevNames);
195 ok(pDevNames != NULL, "(expected '!= NULL')\n");
197 if (pDevNames) {
198 ok(pDevNames->wDriverOffset, "(expected '!= 0' for wDriverOffset)\n");
199 ok(pDevNames->wDeviceOffset, "(expected '!= 0' for wDeviceOffset)\n");
200 ok(pDevNames->wOutputOffset, "(expected '!= 0' for wOutputOffset)\n");
201 ok(pDevNames->wDefault == DN_DEFAULTPRN, "got 0x%x (expected DN_DEFAULTPRN)\n", pDevNames->wDefault);
203 driver = (LPCSTR)pDevNames + pDevNames->wDriverOffset;
204 device = (LPCSTR)pDevNames + pDevNames->wDeviceOffset;
205 port = (LPCSTR)pDevNames + pDevNames->wOutputOffset;
206 trace("driver '%s' device '%s' port '%s'\n", driver, device, port);
208 /* The Driver Entry does not include a Path */
209 ptr = strrchr(driver, '\\');
210 ok( ptr == NULL, "got %p for '%s' (expected NULL for a simple name)\n", ptr, driver);
212 /* The Driver Entry does not have an extension (fixed to ".drv") */
213 ptr = strrchr(driver, '.');
214 todo_wine {
215 ok( ptr == NULL, "got %p for '%s' (expected NULL for no extension)\n", ptr, driver);
219 buffer[0] = '\0';
220 SetLastError(0xdeadbeef);
221 res = GetProfileStringA(PrinterPortsA, device, emptyA, buffer, sizeof(buffer));
222 ptr = strchr(buffer, ',');
223 ok( (res > 1) && (ptr != NULL),
224 "got %u with %u and %p for '%s' (expected '>1' and '!= NULL')\n",
225 res, GetLastError(), ptr, buffer);
227 if (ptr) ptr[0] = '\0';
228 ok( lstrcmpiA(driver, buffer) == 0,
229 "got driver '%s' (expected '%s')\n", driver, buffer);
231 n_copies = DeviceCapabilities(device, port, DC_COPIES, NULL, NULL);
232 ok(n_copies > 0, "DeviceCapabilities(DC_COPIES) failed\n");
235 GlobalUnlock(pDlg->hDevNames);
236 GlobalFree(pDlg->hDevMode);
237 GlobalFree(pDlg->hDevNames);
239 /* if device doesn't support printing of multiple copies then
240 * an attempt to set number of copies > 1 in print dialog would
241 * cause the PrintDlg under Windows display the MessageBox and
242 * the test will hang waiting for user response.
244 if (n_copies > 1)
246 ZeroMemory(pDlg, sizeof(*pDlg));
247 pDlg->lStructSize = sizeof(*pDlg);
248 pDlg->Flags = PD_ENABLEPRINTHOOK;
249 pDlg->lpfnPrintHook = print_hook_proc;
250 res = PrintDlg(pDlg);
251 ok(res, "PrintDlg error %#x\n", CommDlgExtendedError());
252 /* Version of Microsoft XPS Document Writer driver shipped before Win7
253 * reports that it can print multiple copies, but returns 1.
255 ok(pDlg->nCopies == 1234 || broken(pDlg->nCopies == 1), "expected nCopies 1234, got %d\n", pDlg->nCopies);
256 ok(pDlg->hDevMode != 0, "hDevMode should not be 0\n");
257 dm = GlobalLock(pDlg->hDevMode);
258 ok(S1(U1(*dm)).dmCopies == 1, "expected dm->dmCopies 1, got %d\n", S1(U1(*dm)).dmCopies);
259 GlobalUnlock(pDlg->hDevMode);
260 GlobalFree(pDlg->hDevMode);
261 GlobalFree(pDlg->hDevNames);
263 ZeroMemory(pDlg, sizeof(*pDlg));
264 pDlg->lStructSize = sizeof(*pDlg);
265 pDlg->Flags = PD_ENABLEPRINTHOOK | PD_USEDEVMODECOPIES;
266 pDlg->lpfnPrintHook = print_hook_proc;
267 res = PrintDlg(pDlg);
268 ok(res, "PrintDlg error %#x\n", CommDlgExtendedError());
269 ok(pDlg->nCopies == 1, "expected nCopies 1, got %d\n", pDlg->nCopies);
270 ok(pDlg->hDevMode != 0, "hDevMode should not be 0\n");
271 dm = GlobalLock(pDlg->hDevMode);
272 ok(S1(U1(*dm)).dmCopies == 1234, "expected dm->dmCopies 1234, got %d\n", S1(U1(*dm)).dmCopies);
273 GlobalUnlock(pDlg->hDevMode);
274 GlobalFree(pDlg->hDevMode);
275 GlobalFree(pDlg->hDevNames);
278 HeapFree(GetProcessHeap(), 0, pDlg);
281 /* ########################### */
283 static HRESULT WINAPI callback_QueryInterface(IPrintDialogCallback *iface,
284 REFIID riid, void **ppv)
286 ok(0, "callback_QueryInterface(%s): unexpected call\n", debugstr_guid(riid));
287 return E_NOINTERFACE;
290 static ULONG WINAPI callback_AddRef(IPrintDialogCallback *iface)
292 trace("callback_AddRef\n");
293 return 2;
296 static ULONG WINAPI callback_Release(IPrintDialogCallback *iface)
298 trace("callback_Release\n");
299 return 1;
302 static HRESULT WINAPI callback_InitDone(IPrintDialogCallback *iface)
304 trace("callback_InitDone\n");
305 return S_OK;
308 static HRESULT WINAPI callback_SelectionChange(IPrintDialogCallback *iface)
310 trace("callback_SelectionChange\n");
311 return S_OK;
314 static HRESULT WINAPI callback_HandleMessage(IPrintDialogCallback *iface,
315 HWND hdlg, UINT msg, WPARAM wp, LPARAM lp, LRESULT *res)
317 trace("callback_HandleMessage %p,%04x,%lx,%lx,%p\n", hdlg, msg, wp, lp, res);
318 /* *res = PD_RESULT_PRINT; */
319 return S_OK;
322 static const IPrintDialogCallbackVtbl callback_Vtbl =
324 callback_QueryInterface,
325 callback_AddRef,
326 callback_Release,
327 callback_InitDone,
328 callback_SelectionChange,
329 callback_HandleMessage
332 static IPrintDialogCallback callback = { &callback_Vtbl };
334 static HRESULT WINAPI unknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
336 trace("unknown_QueryInterface %s\n", debugstr_guid(riid));
338 if (IsEqualGUID(riid, &IID_IPrintDialogCallback))
340 *ppv = &callback;
341 return S_OK;
343 else if (IsEqualGUID(riid, &IID_IObjectWithSite))
345 *ppv = NULL;
346 return E_NOINTERFACE;
349 ok(0, "unexpected IID %s\n", debugstr_guid(riid));
350 *ppv = NULL;
351 return E_NOINTERFACE;
354 static ULONG WINAPI unknown_AddRef(IUnknown *iface)
356 trace("unknown_AddRef\n");
357 return 2;
360 static ULONG WINAPI unknown_Release(IUnknown *iface)
362 trace("unknown_Release\n");
363 return 1;
366 static const IUnknownVtbl unknown_Vtbl =
368 unknown_QueryInterface,
369 unknown_AddRef,
370 unknown_Release
373 static IUnknown unknown = { &unknown_Vtbl };
375 static void test_PrintDlgExW(void)
377 PRINTPAGERANGE pagerange[2];
378 LPPRINTDLGEXW pDlg;
379 DEVNAMES *dn;
380 HRESULT res;
382 /* PrintDlgEx not present before w2k */
383 if (!pPrintDlgExW) {
384 skip("PrintDlgExW not available\n");
385 return;
388 /* Set CommDlgExtendedError != 0 */
389 PrintDlg(NULL);
390 SetLastError(0xdeadbeef);
391 res = pPrintDlgExW(NULL);
392 ok( (res == E_INVALIDARG),
393 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
394 res, GetLastError(), CommDlgExtendedError());
397 pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGEXW)) + 8);
398 if (!pDlg) return;
400 /* lStructSize must be exact */
401 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
402 pDlg->lStructSize = sizeof(PRINTDLGEXW) - 1;
403 PrintDlg(NULL);
404 SetLastError(0xdeadbeef);
405 res = pPrintDlgExW(pDlg);
406 ok( (res == E_INVALIDARG),
407 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
408 res, GetLastError(), CommDlgExtendedError());
411 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
412 pDlg->lStructSize = sizeof(PRINTDLGEXW) + 1;
413 PrintDlg(NULL);
414 SetLastError(0xdeadbeef);
415 res = pPrintDlgExW(pDlg);
416 ok( (res == E_INVALIDARG),
417 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
418 res, GetLastError(), CommDlgExtendedError());
421 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
422 pDlg->lStructSize = sizeof(PRINTDLGEXW);
423 SetLastError(0xdeadbeef);
424 res = pPrintDlgExW(pDlg);
425 ok( (res == E_HANDLE),
426 "got 0x%x with %u and %u (expected 'E_HANDLE')\n",
427 res, GetLastError(), CommDlgExtendedError());
429 /* nStartPage must be START_PAGE_GENERAL for the general page or a valid property sheet index */
430 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
431 pDlg->lStructSize = sizeof(PRINTDLGEXW);
432 pDlg->hwndOwner = GetDesktopWindow();
433 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING | PD_NOPAGENUMS;
434 res = pPrintDlgExW(pDlg);
435 ok((res == E_INVALIDARG), "got 0x%x (expected 'E_INVALIDARG')\n", res);
437 /* Use PD_NOPAGENUMS or set nMaxPageRanges and lpPageRanges */
438 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
439 pDlg->lStructSize = sizeof(PRINTDLGEXW);
440 pDlg->hwndOwner = GetDesktopWindow();
441 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING;
442 pDlg->nStartPage = START_PAGE_GENERAL;
443 res = pPrintDlgExW(pDlg);
444 ok((res == E_INVALIDARG), "got 0x%x (expected 'E_INVALIDARG')\n", res);
446 /* this is invalid: a valid lpPageRanges with 0 for nMaxPageRanges */
447 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
448 pDlg->lStructSize = sizeof(PRINTDLGEXW);
449 pDlg->hwndOwner = GetDesktopWindow();
450 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING;
451 pDlg->lpPageRanges = pagerange;
452 pDlg->nStartPage = START_PAGE_GENERAL;
453 res = pPrintDlgExW(pDlg);
454 ok((res == E_INVALIDARG), "got 0x%x (expected 'E_INVALIDARG')\n", res);
456 /* this is invalid: NULL for lpPageRanges with a valid nMaxPageRanges */
457 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
458 pDlg->lStructSize = sizeof(PRINTDLGEXW);
459 pDlg->hwndOwner = GetDesktopWindow();
460 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING;
461 pDlg->nMaxPageRanges = 1;
462 pDlg->nStartPage = START_PAGE_GENERAL;
463 res = pPrintDlgExW(pDlg);
464 ok((res == E_INVALIDARG), "got 0x%x (expected 'E_INVALIDARG')\n", res);
466 /* this works: lpPageRanges with a valid nMaxPageRanges */
467 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
468 pDlg->lStructSize = sizeof(PRINTDLGEXW);
469 pDlg->hwndOwner = GetDesktopWindow();
470 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING;
471 pDlg->nMaxPageRanges = 1;
472 pDlg->lpPageRanges = pagerange;
473 pDlg->nStartPage = START_PAGE_GENERAL;
474 res = pPrintDlgExW(pDlg);
475 if (res == E_FAIL)
477 skip("No printer configured.\n");
478 HeapFree(GetProcessHeap(), 0, pDlg);
479 return;
482 ok(res == S_OK, "got 0x%x (expected S_OK)\n", res);
484 dn = GlobalLock(pDlg->hDevNames);
485 ok(dn != NULL, "expected '!= NULL' for GlobalLock(%p)\n",pDlg->hDevNames);
486 if (dn)
488 ok(dn->wDriverOffset, "(expected '!= 0' for wDriverOffset)\n");
489 ok(dn->wDeviceOffset, "(expected '!= 0' for wDeviceOffset)\n");
490 ok(dn->wOutputOffset, "(expected '!= 0' for wOutputOffset)\n");
491 ok(dn->wDefault == DN_DEFAULTPRN, "got 0x%x (expected DN_DEFAULTPRN)\n", dn->wDefault);
493 GlobalUnlock(pDlg->hDevNames);
495 GlobalFree(pDlg->hDevMode);
496 GlobalFree(pDlg->hDevNames);
498 /* this works also: PD_NOPAGENUMS */
499 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
500 pDlg->lStructSize = sizeof(PRINTDLGEXW);
501 pDlg->hwndOwner = GetDesktopWindow();
502 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING | PD_NOPAGENUMS;
503 pDlg->nStartPage = START_PAGE_GENERAL;
504 res = pPrintDlgExW(pDlg);
505 ok(res == S_OK, "got 0x%x (expected S_OK)\n", res);
506 GlobalFree(pDlg->hDevMode);
507 GlobalFree(pDlg->hDevNames);
509 /* this works: PD_RETURNDC with PD_RETURNDEFAULT */
510 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
511 pDlg->lStructSize = sizeof(PRINTDLGEXW);
512 pDlg->hwndOwner = GetDesktopWindow();
513 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING | PD_NOPAGENUMS | PD_RETURNDC;
514 pDlg->nStartPage = START_PAGE_GENERAL;
515 res = pPrintDlgExW(pDlg);
516 ok(res == S_OK, "got 0x%x (expected S_OK)\n", res);
517 ok(pDlg->hDC != NULL, "HDC missing for PD_RETURNDC\n");
518 GlobalFree(pDlg->hDevMode);
519 GlobalFree(pDlg->hDevNames);
520 DeleteDC(pDlg->hDC);
522 /* this works: PD_RETURNIC with PD_RETURNDEFAULT */
523 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
524 pDlg->lStructSize = sizeof(PRINTDLGEXW);
525 pDlg->hwndOwner = GetDesktopWindow();
526 pDlg->Flags = PD_RETURNDEFAULT | PD_NOWARNING | PD_NOPAGENUMS | PD_RETURNIC;
527 pDlg->nStartPage = START_PAGE_GENERAL;
528 res = pPrintDlgExW(pDlg);
529 ok(res == S_OK, "got 0x%x (expected S_OK)\n", res);
530 ok(pDlg->hDC != NULL, "HDC missing for PD_RETURNIC\n");
531 GlobalFree(pDlg->hDevMode);
532 GlobalFree(pDlg->hDevNames);
533 DeleteDC(pDlg->hDC);
535 /* interactive PrintDlgEx tests */
537 if (!winetest_interactive)
539 skip("interactive PrintDlgEx tests (set WINETEST_INTERACTIVE=1)\n");
540 return;
543 ZeroMemory(pDlg, sizeof(PRINTDLGEXW));
544 pDlg->lStructSize = sizeof(PRINTDLGEXW);
545 pDlg->hwndOwner = GetDesktopWindow();
546 pDlg->Flags = PD_NOPAGENUMS | PD_RETURNIC;
547 pDlg->nStartPage = START_PAGE_GENERAL;
548 pDlg->lpCallback = &unknown;
549 pDlg->dwResultAction = S_OK;
550 res = pPrintDlgExW(pDlg);
551 ok(res == S_OK, "got 0x%x (expected S_OK)\n", res);
552 ok(pDlg->dwResultAction == PD_RESULT_PRINT, "expected PD_RESULT_PRINT, got %#x\n", pDlg->dwResultAction);
553 ok(pDlg->hDC != NULL, "HDC missing for PD_RETURNIC\n");
554 GlobalFree(pDlg->hDevMode);
555 GlobalFree(pDlg->hDevNames);
556 DeleteDC(pDlg->hDC);
558 HeapFree(GetProcessHeap(), 0, pDlg);
561 static BOOL abort_proc_called = FALSE;
562 static BOOL CALLBACK abort_proc(HDC hdc, int error) { return abort_proc_called = TRUE; }
563 static void test_abort_proc(void)
565 HDC print_dc;
566 RECT rect = {0, 0, 100, 100};
567 DOCINFOA doc_info = {0};
568 PRINTDLGA pd = {0};
569 char filename[MAX_PATH];
570 int job_id;
572 if (!GetTempFileNameA(".", "prn", 0, filename))
574 skip("Failed to create a temporary file name\n");
575 return;
578 pd.lStructSize = sizeof(pd);
579 pd.Flags = PD_RETURNDEFAULT | PD_ALLPAGES | PD_RETURNDC | PD_PRINTTOFILE;
580 pd.nFromPage = 1;
581 pd.nToPage = 1;
582 pd.nCopies = 1;
584 if (!PrintDlgA(&pd))
586 skip("No default printer available.\n");
587 goto end;
589 GlobalFree(pd.hDevMode);
590 GlobalFree(pd.hDevNames);
592 ok(pd.hDC != NULL, "PrintDlg didn't return a DC.\n");
593 if (!(print_dc = pd.hDC))
594 goto end;
596 ok(SetAbortProc(print_dc, abort_proc) > 0, "SetAbortProc failed\n");
597 ok(!abort_proc_called, "AbortProc got called unexpectedly by SetAbortProc.\n");
598 abort_proc_called = FALSE;
600 doc_info.cbSize = sizeof(doc_info);
601 doc_info.lpszDocName = "Some document";
602 doc_info.lpszOutput = filename;
604 job_id = StartDocA(print_dc, &doc_info);
606 ok(job_id > 0 ||
607 GetLastError() == ERROR_SPL_NO_STARTDOC, /* Vista can fail with this error when using the XPS driver */
608 "StartDocA failed ret %d gle %d\n", job_id, GetLastError());
610 if(job_id <= 0)
612 skip("StartDoc failed\n");
613 goto end;
616 /* StartDoc may or may not call abort proc */
618 abort_proc_called = FALSE;
619 ok(StartPage(print_dc) > 0, "StartPage failed\n");
620 ok(!abort_proc_called, "AbortProc got called unexpectedly by StartPage.\n");
621 abort_proc_called = FALSE;
623 /* following functions sometimes call abort proc too */
624 ok(FillRect(print_dc, &rect, (HBRUSH)(COLOR_BACKGROUND + 1)), "FillRect failed\n");
625 ok(EndPage(print_dc) > 0, "EndPage failed\n");
626 ok(EndDoc(print_dc) > 0, "EndDoc failed\n");
628 abort_proc_called = FALSE;
629 ok(DeleteDC(print_dc), "DeleteDC failed\n");
630 ok(!abort_proc_called, "AbortProc got called unexpectedly by DeleteDC.\n");
631 abort_proc_called = FALSE;
633 end:
634 SetLastError(0xdeadbeef);
635 if(!DeleteFileA(filename))
636 trace("Failed to delete temporary file (err = %x)\n", GetLastError());
639 /* ########################### */
641 START_TEST(printdlg)
643 hcomdlg32 = GetModuleHandleA("comdlg32.dll");
644 pPrintDlgExW = (void *) GetProcAddress(hcomdlg32, "PrintDlgExW");
646 test_PageSetupDlgA();
647 test_PrintDlgA();
648 test_PrintDlgExW();
649 test_abort_proc();