ReactOS  0.4.15-dev-5606-gf34e425
GetDefaultPrinter.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Print Spooler DLL API Tests
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Tests for GetDefaultPrinterA/GetDefaultPrinterW/SetDefaultPrinterA/SetDefaultPrinterW
5  * COPYRIGHT: Copyright 2017 Colin Finck (colin@reactos.org)
6  */
7 
8 #include <apitest.h>
9 
10 #define WIN32_NO_STATUS
11 #include <windef.h>
12 #include <winbase.h>
13 #include <wingdi.h>
14 #include <winspool.h>
15 
17 {
18  DWORD cchDefaultPrinter;
19  PSTR pszDefaultPrinter;
20 
21  // Don't supply any parameters, this has to fail with ERROR_INVALID_PARAMETER.
22  SetLastError(0xDEADBEEF);
23  ok(!GetDefaultPrinterA(NULL, NULL), "GetDefaultPrinterA returns TRUE!\n");
24  ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetDefaultPrinterA returns error %lu!\n", GetLastError());
25 
26  // Determine the size of the required buffer. This has to bail out with ERROR_INSUFFICIENT_BUFFER.
27  cchDefaultPrinter = 0;
28  SetLastError(0xDEADBEEF);
29  ok(!GetDefaultPrinterA(NULL, &cchDefaultPrinter), "GetDefaultPrinterA returns TRUE!\n");
30  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetDefaultPrinterA returns error %lu!\n", GetLastError());
31 
32  // Try with a buffer large enough.
33  pszDefaultPrinter = HeapAlloc(GetProcessHeap(), 0, cchDefaultPrinter);
34  SetLastError(0xDEADBEEF);
35  ok(GetDefaultPrinterA(pszDefaultPrinter, &cchDefaultPrinter), "GetDefaultPrinterA returns FALSE!\n");
36 
37  // SetDefaultPrinterA with NULL needs to succeed and leave the default printer unchanged.
38  SetLastError(0xDEADBEEF);
39  ok(SetDefaultPrinterA(NULL), "SetDefaultPrinterA returns FALSE!\n");
40 
41  // SetDefaultPrinterA with the previous default printer also needs to succeed.
42  SetLastError(0xDEADBEEF);
43  ok(SetDefaultPrinterA(pszDefaultPrinter), "SetDefaultPrinterA returns FALSE!\n");
44 
45  // SetDefaultPrinterA with an invalid printer name needs to fail with ERROR_INVALID_PRINTER_NAME.
46  SetLastError(0xDEADBEEF);
47  ok(!SetDefaultPrinterA("INVALID PRINTER NAME!!!"), "SetDefaultPrinterA returns TRUE!\n");
48  ok(GetLastError() == ERROR_INVALID_PRINTER_NAME, "SetDefaultPrinterA returns error %lu!\n", GetLastError());
49 
50  HeapFree(GetProcessHeap(), 0, pszDefaultPrinter);
51 }
52 
54 {
55  DWORD cchDefaultPrinter;
56  PWSTR pwszDefaultPrinter;
57 
58  // Don't supply any parameters, this has to fail with ERROR_INVALID_PARAMETER.
59  SetLastError(0xDEADBEEF);
60  ok(!GetDefaultPrinterW(NULL, NULL), "GetDefaultPrinterW returns TRUE!\n");
61  ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetDefaultPrinterW returns error %lu!\n", GetLastError());
62 
63  // Determine the size of the required buffer. This has to bail out with ERROR_INSUFFICIENT_BUFFER.
64  cchDefaultPrinter = 0;
65  SetLastError(0xDEADBEEF);
66  ok(!GetDefaultPrinterW(NULL, &cchDefaultPrinter), "GetDefaultPrinterW returns TRUE!\n");
67  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetDefaultPrinterW returns error %lu!\n", GetLastError());
68 
69  // Try with a buffer large enough.
70  pwszDefaultPrinter = HeapAlloc(GetProcessHeap(), 0, cchDefaultPrinter * sizeof(WCHAR));
71  SetLastError(0xDEADBEEF);
72  ok(GetDefaultPrinterW(pwszDefaultPrinter, &cchDefaultPrinter), "GetDefaultPrinterW returns FALSE!\n");
73 
74  // SetDefaultPrinterW with NULL needs to succeed and leave the default printer unchanged.
75  SetLastError(0xDEADBEEF);
76  ok(SetDefaultPrinterW(NULL), "SetDefaultPrinterW returns FALSE!\n");
77 
78  // SetDefaultPrinterW with the previous default printer also needs to succeed.
79  SetLastError(0xDEADBEEF);
80  ok(SetDefaultPrinterW(pwszDefaultPrinter), "SetDefaultPrinterW returns FALSE!\n");
81 
82  // SetDefaultPrinterW with an invalid printer name needs to fail with ERROR_INVALID_PRINTER_NAME.
83  SetLastError(0xDEADBEEF);
84  ok(!SetDefaultPrinterW(L"INVALID PRINTER NAME!!!"), "SetDefaultPrinterW returns TRUE!\n");
85  ok(GetLastError() == ERROR_INVALID_PRINTER_NAME, "SetDefaultPrinterW returns error %lu!\n", GetLastError());
86 
87  HeapFree(GetProcessHeap(), 0, pwszDefaultPrinter);
88 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
WINBOOL WINAPI SetDefaultPrinterA(LPCSTR pszPrinter)
Definition: printers.c:2971
WINBOOL WINAPI GetDefaultPrinterW(LPWSTR pszBuffer, LPDWORD pcchBuffer)
Definition: printers.c:1942
uint16_t * PWSTR
Definition: typedefs.h:56
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1040
#define L(x)
Definition: ntvdm.h:50
#define GetProcessHeap()
Definition: compat.h:736
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:752
START_TEST(GetDefaultPrinterA)
#define ERROR_INVALID_PRINTER_NAME
Definition: winerror.h:1108
WINBOOL WINAPI GetDefaultPrinterA(LPSTR pszBuffer, LPDWORD pcchBuffer)
Definition: printers.c:1896
#define ok(value,...)
Definition: atltest.h:57
signed char * PSTR
Definition: retypes.h:7
#define NULL
Definition: types.h:112
WINBOOL WINAPI SetDefaultPrinterW(LPCWSTR pszPrinter)
Definition: printers.c:3005
#define HeapFree(x, y, z)
Definition: compat.h:735
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10