msvcrt: Added read tests on unicode files.
[wine] / dlls / comdlg32 / tests / printdlg.c
1 /* 
2  * Unit test suite for comdlg32 API functions: printer dialogs
3  *
4  * Copyright 2006-2007 Detlef Riekenberg
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "objbase.h"
30
31 #include "cderr.h"
32 #include "commdlg.h"
33
34 #include "wine/test.h"
35
36 /* ########################### */
37
38 static HMODULE  hcomdlg32;
39 static HRESULT (WINAPI * pPrintDlgExW)(LPPRINTDLGEXW);
40
41 /* ########################### */
42
43 static const CHAR emptyA[] = "";
44 static const CHAR PrinterPortsA[] = "PrinterPorts";
45
46 /* ########################### */
47
48 static void test_PageSetupDlgA(void)
49 {
50     LPPAGESETUPDLGA pDlg;
51     DWORD res;
52
53     pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PAGESETUPDLGA)) * 2);
54     if (!pDlg) return;
55
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());
61
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());
69
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());
78
79
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());
88
89     if (!res && (CommDlgExtendedError() == PDERR_NODEFAULTPRN)) {
90         skip("No printer configured.\n");
91         HeapFree(GetProcessHeap(), 0, pDlg);
92         return;
93     }
94
95     ok( pDlg->hDevMode && pDlg->hDevNames,
96         "got %p and %p (expected '!= NULL' for both)\n",
97         pDlg->hDevMode, pDlg->hDevNames);
98
99     GlobalFree(pDlg->hDevMode);
100     GlobalFree(pDlg->hDevNames);
101
102     HeapFree(GetProcessHeap(), 0, pDlg);
103
104 }
105
106 /* ########################### */
107
108 static void test_PrintDlgA(void)
109 {
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;
118
119
120     pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGA)) * 2);
121     if (!pDlg) return;
122
123
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());
130
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());
138
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());
147
148
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());
157
158     if (!res && (CommDlgExtendedError() == PDERR_NODEFAULTPRN)) {
159         skip("No printer configured.\n");
160         HeapFree(GetProcessHeap(), 0, pDlg);
161         return;
162     }
163
164     ok(pDlg->hDevNames != NULL, "(expected '!= NULL')\n");
165     pDevNames = GlobalLock(pDlg->hDevNames);
166     ok(pDevNames != NULL, "(expected '!= NULL')\n");
167
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);
173
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);
178
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);
182
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);
187         }
188
189
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);
197
198         if (ptr) ptr[0] = '\0';
199         ok( lstrcmpiA(driver, buffer) == 0,
200             "got driver '%s' (expected '%s')\n", driver, buffer);
201     }
202
203     GlobalUnlock(pDlg->hDevNames);
204
205     GlobalFree(pDlg->hDevMode);
206     GlobalFree(pDlg->hDevNames);
207     HeapFree(GetProcessHeap(), 0, pDlg);
208
209 }
210
211 /* ########################### */
212
213 static void test_PrintDlgExW(void)
214 {
215     PRINTPAGERANGE pagerange[2];
216     LPPRINTDLGEXW pDlg;
217     DEVNAMES *dn;
218     HRESULT res;
219
220     /* PrintDlgEx not present before w2k */
221     if (!pPrintDlgExW) {
222         skip("PrintDlgExW not available\n");
223         return;
224     }
225
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());
233
234
235     pDlg = HeapAlloc(GetProcessHeap(), 0, (sizeof(PRINTDLGEXW)) + 8);
236     if (!pDlg) return;
237
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());
247
248
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());
257
258
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());
266
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);
274
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);
283
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);
293
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);
303
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)
314     {
315         skip("No printer configured.\n");
316         HeapFree(GetProcessHeap(), 0, pDlg);
317         return;
318     }
319
320     ok(res == S_OK, "got 0x%x (expected S_OK)\n", res);
321
322     dn = GlobalLock(pDlg->hDevNames);
323     ok(dn != NULL, "expected '!= NULL' for GlobalLock(%p)\n",pDlg->hDevNames);
324     if (dn)
325     {
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);
330
331         GlobalUnlock(pDlg->hDevNames);
332     }
333     GlobalFree(pDlg->hDevMode);
334     GlobalFree(pDlg->hDevNames);
335
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);
346
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);
358     DeleteDC(pDlg->hDC);
359
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);
371     DeleteDC(pDlg->hDC);
372
373     HeapFree(GetProcessHeap(), 0, pDlg);
374     return;
375
376 }
377
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)
381 {
382     HDC print_dc;
383     RECT rect = {0, 0, 100, 100};
384     DOCINFOA doc_info = {0};
385     PRINTDLGA pd = {0};
386     char filename[MAX_PATH];
387     int job_id;
388
389     if (!GetTempFileNameA(".", "prn", 0, filename))
390     {
391         skip("Failed to create a temporary file name\n");
392         return;
393     }
394
395     pd.lStructSize = sizeof(pd);
396     pd.Flags = PD_RETURNDEFAULT | PD_ALLPAGES | PD_RETURNDC | PD_PRINTTOFILE;
397     pd.nFromPage = 1;
398     pd.nToPage = 1;
399     pd.nCopies = 1;
400
401     if (!PrintDlgA(&pd))
402     {
403         skip("No default printer available.\n");
404         goto end;
405     }
406     GlobalFree(pd.hDevMode);
407     GlobalFree(pd.hDevNames);
408
409     ok(pd.hDC != NULL, "PrintDlg didn't return a DC.\n");
410     if (!(print_dc = pd.hDC))
411         goto end;
412
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;
416
417     doc_info.cbSize = sizeof(doc_info);
418     doc_info.lpszDocName = "Some document";
419     doc_info.lpszOutput = filename;
420
421     job_id = StartDocA(print_dc, &doc_info);
422
423     ok(job_id > 0 ||
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());
426
427     if(job_id <= 0)
428     {
429         skip("StartDoc failed\n");
430         goto end;
431     }
432
433     /* StartDoc may or may not call abort proc */
434
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;
439
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");
444
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;
449
450 end:
451     SetLastError(0xdeadbeef);
452     if(!DeleteFileA(filename))
453         trace("Failed to delete temporary file (err = %x)\n", GetLastError());
454 }
455
456 /* ########################### */
457
458 START_TEST(printdlg)
459 {
460     hcomdlg32 = GetModuleHandleA("comdlg32.dll");
461     pPrintDlgExW = (void *) GetProcAddress(hcomdlg32, "PrintDlgExW");
462
463     test_PageSetupDlgA();
464     test_PrintDlgA();
465     test_PrintDlgExW();
466     test_abort_proc();
467 }