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)
218 /* PrintDlgEx not present before w2k */
220 skip("PrintDlgExW not available\n");
224 /* Set CommDlgExtendedError != 0 */
226 SetLastError(0xdeadbeef);
227 res
= pPrintDlgExW(NULL
);
228 ok( (res
== E_INVALIDARG
),
229 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
230 res
, GetLastError(), CommDlgExtendedError());
233 pDlg
= HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGEXW
)) + 8);
236 /* lStructSize must be exact */
237 ZeroMemory(pDlg
, sizeof(PRINTDLGEXW
));
238 pDlg
->lStructSize
= sizeof(PRINTDLGEXW
) - 1;
240 SetLastError(0xdeadbeef);
241 res
= pPrintDlgExW(pDlg
);
242 ok( (res
== E_INVALIDARG
),
243 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
244 res
, GetLastError(), CommDlgExtendedError());
247 ZeroMemory(pDlg
, sizeof(PRINTDLGEXW
));
248 pDlg
->lStructSize
= sizeof(PRINTDLGEXW
) + 1;
250 SetLastError(0xdeadbeef);
251 res
= pPrintDlgExW(pDlg
);
252 ok( (res
== E_INVALIDARG
),
253 "got 0x%x with %u and %u (expected 'E_INVALIDARG')\n",
254 res
, GetLastError(), CommDlgExtendedError());
257 ZeroMemory(pDlg
, sizeof(PRINTDLGEXW
));
258 pDlg
->lStructSize
= sizeof(PRINTDLGEXW
);
259 SetLastError(0xdeadbeef);
260 res
= pPrintDlgExW(pDlg
);
261 ok( (res
== E_HANDLE
),
262 "got 0x%x with %u and %u (expected 'E_HANDLE')\n",
263 res
, GetLastError(), CommDlgExtendedError());
266 HeapFree(GetProcessHeap(), 0, pDlg
);
271 static BOOL abort_proc_called
= FALSE
;
272 static BOOL CALLBACK
abort_proc(HDC hdc
, int error
) { return abort_proc_called
= TRUE
; }
273 static void test_abort_proc(void)
276 RECT rect
= {0, 0, 100, 100};
277 DOCINFOA doc_info
= {0};
279 char filename
[MAX_PATH
];
282 if (!GetTempFileNameA(".", "prn", 0, filename
))
284 skip("Failed to create a temporary file name\n");
288 pd
.lStructSize
= sizeof(pd
);
289 pd
.Flags
= PD_RETURNDEFAULT
| PD_ALLPAGES
| PD_RETURNDC
| PD_PRINTTOFILE
;
296 skip("No default printer available.\n");
299 GlobalFree(pd
.hDevMode
);
300 GlobalFree(pd
.hDevNames
);
302 ok(pd
.hDC
!= NULL
, "PrintDlg didn't return a DC.\n");
303 if (!(print_dc
= pd
.hDC
))
306 ok(SetAbortProc(print_dc
, abort_proc
) > 0, "SetAbortProc failed\n");
307 ok(!abort_proc_called
, "AbortProc got called unexpectedly by SetAbortProc.\n");
308 abort_proc_called
= FALSE
;
310 doc_info
.cbSize
= sizeof(doc_info
);
311 doc_info
.lpszDocName
= "Some document";
312 doc_info
.lpszOutput
= filename
;
314 job_id
= StartDocA(print_dc
, &doc_info
);
317 GetLastError() == ERROR_SPL_NO_STARTDOC
, /* Vista can fail with this error when using the XPS driver */
318 "StartDocA failed ret %d gle %d\n", job_id
, GetLastError());
322 skip("StartDoc failed\n");
326 /* StartDoc may or may not call abort proc */
328 abort_proc_called
= FALSE
;
329 ok(StartPage(print_dc
) > 0, "StartPage failed\n");
330 ok(!abort_proc_called
, "AbortProc got called unexpectedly by StartPage.\n");
331 abort_proc_called
= FALSE
;
333 /* following functions sometimes call abort proc too */
334 ok(FillRect(print_dc
, &rect
, (HBRUSH
)(COLOR_BACKGROUND
+ 1)), "FillRect failed\n");
335 ok(EndPage(print_dc
) > 0, "EndPage failed\n");
336 ok(EndDoc(print_dc
) > 0, "EndDoc failed\n");
338 abort_proc_called
= FALSE
;
339 ok(DeleteDC(print_dc
), "DeleteDC failed\n");
340 ok(!abort_proc_called
, "AbortProc got called unexpectedly by DeleteDC.\n");
341 abort_proc_called
= FALSE
;
344 SetLastError(0xdeadbeef);
345 if(!DeleteFileA(filename
))
346 trace("Failed to delete temporary file (err = %x)\n", GetLastError());
349 /* ########################### */
353 hcomdlg32
= GetModuleHandleA("comdlg32.dll");
354 pPrintDlgExW
= (void *) GetProcAddress(hcomdlg32
, "PrintDlgExW");
356 test_PageSetupDlgA();