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
* pPrintDlgExA
)(LPPRINTDLGEXA
);
40 static HRESULT (WINAPI
* pPrintDlgExW
)(LPPRINTDLGEXW
);
42 /* ########################### */
44 static const CHAR emptyA
[] = "";
45 static const CHAR PrinterPortsA
[] = "PrinterPorts";
47 /* ########################### */
49 static LPCSTR
load_functions(void)
54 hcomdlg32
= GetModuleHandleA(ptr
);
57 pPrintDlgExA
= (void *) GetProcAddress(hcomdlg32
, ptr
);
58 if (!pPrintDlgExA
) return ptr
;
61 pPrintDlgExW
= (void *) GetProcAddress(hcomdlg32
, ptr
);
62 if (!pPrintDlgExW
) return ptr
;
68 /* ########################### */
70 static void test_PageSetupDlgA(void)
75 pDlg
= HeapAlloc(GetProcessHeap(), 0, (sizeof(PAGESETUPDLGA
)) * 2);
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());
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());
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());
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());
111 if (!res
&& (CommDlgExtendedError() == PDERR_NODEFAULTPRN
)) {
112 skip("No printer configured.\n");
113 HeapFree(GetProcessHeap(), 0, pDlg
);
117 ok( pDlg
->hDevMode
&& pDlg
->hDevNames
,
118 "got %p and %p (expected '!= NULL' for both)\n",
119 pDlg
->hDevMode
, pDlg
->hDevNames
);
121 GlobalFree(pDlg
->hDevMode
);
122 GlobalFree(pDlg
->hDevNames
);
124 HeapFree(GetProcessHeap(), 0, pDlg
);
128 /* ########################### */
130 static void test_PrintDlgA(void)
138 CHAR buffer
[MAX_PATH
];
142 pDlg
= HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGA
)) * 2);
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());
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());
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());
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());
180 if (!res
&& (CommDlgExtendedError() == PDERR_NODEFAULTPRN
)) {
181 skip("No printer configured.\n");
182 HeapFree(GetProcessHeap(), 0, pDlg
);
186 ok(pDlg
->hDevNames
!= NULL
, "(expected '!= NULL')\n");
187 pDevNames
= GlobalLock(pDlg
->hDevNames
);
188 ok(pDevNames
!= NULL
, "(expected '!= NULL')\n");
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
);
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
);
201 /* The Driver Entry does not include a Path */
202 ptr
= strrchr(driver
, '\\');
204 ok( ptr
== NULL
, "got %p for '%s' (expected NULL for a simple name)\n", ptr
, driver
);
207 /* The Driver Entry does not have an extension (fixed to ".drv") */
208 ptr
= strrchr(driver
, '.');
210 ok( ptr
== NULL
, "got %p for '%s' (expected NULL for no extension)\n", ptr
, driver
);
215 SetLastError(0xdeadbeef);
216 res
= GetProfileStringA(PrinterPortsA
, device
, emptyA
, buffer
, sizeof(buffer
));
217 ptr
= strchr(buffer
, ',');
218 ok( (res
> 1) && (ptr
!= NULL
),
219 "got %u with %u and %p for '%s' (expected '>1' and '!= NULL')\n",
220 res
, GetLastError(), ptr
, buffer
);
222 if (ptr
) ptr
[0] = '\0';
224 ok( lstrcmpiA(driver
, buffer
) == 0,
225 "got driver '%s' (expected '%s')\n", driver
, buffer
);
230 GlobalUnlock(pDlg
->hDevNames
);
232 GlobalFree(pDlg
->hDevMode
);
233 GlobalFree(pDlg
->hDevNames
);
234 HeapFree(GetProcessHeap(), 0, pDlg
);
238 /* ########################### */
240 static void test_PrintDlgExW(void)
245 /* Set CommDlgExtendedError != 0 */
247 SetLastError(0xdeadbeef);
248 res
= pPrintDlgExW(NULL
);
251 win_skip("PrintDlgExW returns not implemented\n");
254 ok( (res
== E_INVALIDARG
),
255 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
256 res
, GetLastError(), CommDlgExtendedError());
259 pDlg
= HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGEXW
)) + 8);
262 /* lStructSize must be exact */
263 ZeroMemory(pDlg
, sizeof(PRINTDLGEXW
));
264 pDlg
->lStructSize
= sizeof(PRINTDLGEXW
) - 1;
266 SetLastError(0xdeadbeef);
267 res
= pPrintDlgExW(pDlg
);
268 ok( (res
== E_INVALIDARG
),
269 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
270 res
, GetLastError(), CommDlgExtendedError());
273 ZeroMemory(pDlg
, sizeof(PRINTDLGEXW
));
274 pDlg
->lStructSize
= sizeof(PRINTDLGEXW
) + 1;
276 SetLastError(0xdeadbeef);
277 res
= pPrintDlgExW(pDlg
);
278 ok( (res
== E_INVALIDARG
),
279 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
280 res
, GetLastError(), CommDlgExtendedError());
283 ZeroMemory(pDlg
, sizeof(PRINTDLGEXW
));
284 pDlg
->lStructSize
= sizeof(PRINTDLGEXW
);
285 SetLastError(0xdeadbeef);
286 res
= pPrintDlgExW(pDlg
);
287 ok( (res
== E_HANDLE
),
288 "got 0x%x with %u and %u (expected 'E_HANDLE')\n",
289 res
, GetLastError(), CommDlgExtendedError());
292 HeapFree(GetProcessHeap(), 0, pDlg
);
297 static BOOL abort_proc_called
= FALSE
;
298 static BOOL CALLBACK
abort_proc(HDC hdc
, int error
) { return abort_proc_called
= TRUE
; }
299 static void test_abort_proc(void)
302 RECT rect
= {0, 0, 100, 100};
303 DOCINFOA doc_info
= {0};
305 char filename
[MAX_PATH
];
308 if (!GetTempFileNameA(".", "prn", 0, filename
))
310 skip("Failed to create a temporary file name\n");
314 pd
.lStructSize
= sizeof(pd
);
315 pd
.Flags
= PD_RETURNDEFAULT
| PD_ALLPAGES
| PD_RETURNDC
| PD_PRINTTOFILE
;
322 skip("No default printer available.\n");
325 GlobalFree(pd
.hDevMode
);
326 GlobalFree(pd
.hDevNames
);
328 ok(pd
.hDC
!= NULL
, "PrintDlg didn't return a DC.\n");
329 if (!(print_dc
= pd
.hDC
))
332 ok(SetAbortProc(print_dc
, abort_proc
) > 0, "SetAbortProc failed\n");
333 ok(!abort_proc_called
, "AbortProc got called unexpectedly by SetAbortProc.\n");
334 abort_proc_called
= FALSE
;
336 doc_info
.cbSize
= sizeof(doc_info
);
337 doc_info
.lpszDocName
= "Some document";
338 doc_info
.lpszOutput
= filename
;
340 job_id
= StartDocA(print_dc
, &doc_info
);
343 GetLastError() == ERROR_SPL_NO_STARTDOC
, /* Vista can fail with this error when using the XPS driver */
344 "StartDocA failed ret %d gle %d\n", job_id
, GetLastError());
348 skip("StartDoc failed\n");
352 /* StartDoc may or may not call abort proc */
354 abort_proc_called
= FALSE
;
355 ok(StartPage(print_dc
) > 0, "StartPage failed\n");
356 ok(!abort_proc_called
, "AbortProc got called unexpectedly by StartPage.\n");
357 abort_proc_called
= FALSE
;
359 /* following functions sometimes call abort proc too */
360 ok(FillRect(print_dc
, &rect
, (HBRUSH
)(COLOR_BACKGROUND
+ 1)), "FillRect failed\n");
361 ok(EndPage(print_dc
) > 0, "EndPage failed\n");
362 ok(EndDoc(print_dc
) > 0, "EndDoc failed\n");
364 abort_proc_called
= FALSE
;
365 ok(DeleteDC(print_dc
), "DeleteDC failed\n");
366 ok(!abort_proc_called
, "AbortProc got called unexpectedly by DeleteDC.\n");
367 abort_proc_called
= FALSE
;
370 SetLastError(0xdeadbeef);
371 if(!DeleteFileA(filename
))
372 trace("Failed to delete temporary file (err = %x)\n", GetLastError());
375 /* ########################### */
381 ptr
= load_functions();
383 test_PageSetupDlgA();
387 /* PrintDlgEx not present before w2k */
389 win_skip("%s\n", ptr
);