1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         GPL - See COPYING in the top level directory
4  * PURPOSE:         Test for GetCurrentObject
5  * PROGRAMMERS:     Timo Kreuzer
6  */
7 
8 #include "precomp.h"
9 
10 void Test_GetCurrentObject()
11 {
12 	HWND hWnd;
13 	HDC hDC;
14 	HBITMAP hBmp;
15 	HGDIOBJ hObj;
16 
17 	/* Create a window */
18 	hWnd = CreateWindowW(L"BUTTON", L"TestWindow", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
19 	                    CW_USEDEFAULT, CW_USEDEFAULT, 100, 100,
20 	                    NULL, NULL, 0, 0);
21 	/* Get the DC */
22 	hDC = GetDC(hWnd);
23 
24 	/* Test NULL DC */
25 	SetLastError(ERROR_SUCCESS);
26 	hObj = GetCurrentObject(NULL, 0);
27 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
28 	ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %lu\n", GetLastError());
29 
30 	SetLastError(ERROR_SUCCESS);
31 	hObj = GetCurrentObject(NULL, OBJ_BITMAP);
32 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
33 	hObj = GetCurrentObject(NULL, OBJ_BRUSH);
34 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
35 	hObj = GetCurrentObject(NULL, OBJ_COLORSPACE);
36 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
37 	hObj = GetCurrentObject(NULL, OBJ_FONT);
38 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
39 	hObj = GetCurrentObject(NULL, OBJ_PAL);
40 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
41 	hObj = GetCurrentObject(NULL, OBJ_PEN);
42 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
43 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
44 
45 	/* Test invalid DC handle */
46 	SetLastError(ERROR_SUCCESS);
47 	hObj = GetCurrentObject((HDC)-123, OBJ_PEN);
48 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
49 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
50 
51 	SetLastError(ERROR_SUCCESS);
52 	hObj = GetCurrentObject((HDC)-123, OBJ_BITMAP);
53 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
54 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
55 
56 	/* Test invalid types */
57 	SetLastError(ERROR_SUCCESS);
58 	hObj = GetCurrentObject(hDC, 0);
59 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
60 	ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %lu\n", GetLastError());
61 
62 	SetLastError(ERROR_SUCCESS);
63 	hObj = GetCurrentObject((HDC)-123, 0);
64 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
65 	ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %lu\n", GetLastError());
66 
67 	SetLastError(ERROR_SUCCESS);
68 	hObj = GetCurrentObject(hDC, 3);
69 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
70 	ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %lu\n", GetLastError());
71 
72 	SetLastError(ERROR_SUCCESS);
73 	hObj = GetCurrentObject(NULL, 3);
74 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
75 	ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %lu\n", GetLastError());
76 
77 	SetLastError(ERROR_SUCCESS);
78 	hObj = GetCurrentObject(hDC, 4);
79 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
80 	ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %lu\n", GetLastError());
81 
82 	SetLastError(ERROR_SUCCESS);
83 	hObj = GetCurrentObject(hDC, 8);
84 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
85 	ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %lu\n", GetLastError());
86 
87 	SetLastError(ERROR_SUCCESS);
88 	hObj = GetCurrentObject(hDC, 9);
89 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
90 	ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %lu\n", GetLastError());
91 
92 	SetLastError(ERROR_SUCCESS);
93 	hObj = GetCurrentObject(hDC, 10);
94 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
95 	ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %lu\n", GetLastError());
96 
97 	SetLastError(ERROR_SUCCESS);
98 	hObj = GetCurrentObject(hDC, 12);
99 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
100 	ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %lu\n", GetLastError());
101 
102 	SetLastError(ERROR_SUCCESS);
103 	hObj = GetCurrentObject(hDC, 13);
104 	ok(hObj == 0, "Expected 0, got %p\n", hObj);
105 	ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %lu\n", GetLastError());
106 
107 	/* Default bitmap */
108 	SetLastError(ERROR_SUCCESS);
109 	hBmp = GetCurrentObject(hDC, OBJ_BITMAP);
110 	ok(GDI_HANDLE_GET_TYPE(hBmp) == GDI_OBJECT_TYPE_BITMAP, "Expected GDI_OBJECT_TYPE_BITMAP, got %lu\n", GDI_HANDLE_GET_TYPE(hBmp));
111 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
112 
113 	/* Other bitmap */
114 	SetLastError(ERROR_SUCCESS);
115 	SelectObject(hDC, GetStockObject(21));
116 	ok(hBmp == GetCurrentObject(hDC, OBJ_BITMAP), "\n");
117 	ok(GDI_HANDLE_GET_TYPE(hBmp) == GDI_OBJECT_TYPE_BITMAP, "Expected GDI_OBJECT_TYPE_BITMAP, got %lu\n", GDI_HANDLE_GET_TYPE(hBmp));
118 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
119 
120 	/* Default brush */
121 	SetLastError(ERROR_SUCCESS);
122 	hObj = GetCurrentObject(hDC, OBJ_BRUSH);
123 	ok(hObj == GetStockObject(WHITE_BRUSH), "Expected %p, got %p\n", GetStockObject(WHITE_BRUSH), hObj);
124 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
125 
126 	/* Other brush */
127 	SetLastError(ERROR_SUCCESS);
128 	SelectObject(hDC, GetStockObject(BLACK_BRUSH));
129 	hObj = GetCurrentObject(hDC, OBJ_BRUSH);
130 	ok(hObj == GetStockObject(BLACK_BRUSH), "Expected %p, got %p\n", GetStockObject(BLACK_BRUSH), hObj);
131 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
132 
133 	/* Default colorspace */
134 	SetLastError(ERROR_SUCCESS);
135 	hObj = GetCurrentObject(hDC, OBJ_COLORSPACE);
136 	ok(hObj == GetStockObject(20), "Expected %p, got %p\n", GetStockObject(20), hObj);
137 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
138 
139 	/* Default font */
140 	SetLastError(ERROR_SUCCESS);
141 	hObj = GetCurrentObject(hDC, OBJ_FONT);
142 	ok(hObj == GetStockObject(SYSTEM_FONT), "Expected %p, got %p\n", GetStockObject(SYSTEM_FONT), hObj);
143 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
144 
145 	/* Other font */
146 	SetLastError(ERROR_SUCCESS);
147 	SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
148 	hObj = GetCurrentObject(hDC, OBJ_FONT);
149 	ok(hObj == GetStockObject(DEFAULT_GUI_FONT), "Expected %p, got %p\n", GetStockObject(DEFAULT_GUI_FONT), hObj);
150 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
151 
152 	/* Default palette */
153 	SetLastError(ERROR_SUCCESS);
154 	hObj = GetCurrentObject(hDC, OBJ_PAL);
155 	ok(hObj == GetStockObject(DEFAULT_PALETTE), "Expected %p, got %p\n", GetStockObject(DEFAULT_PALETTE), hObj);
156 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
157 
158 	/* Default pen */
159 	SetLastError(ERROR_SUCCESS);
160 	hObj = GetCurrentObject(hDC, OBJ_PEN);
161 	ok(hObj == GetStockObject(BLACK_PEN), "Expected %p, got %p\n", GetStockObject(BLACK_PEN), hObj);
162 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
163 
164 	/* Other pen */
165 	SetLastError(ERROR_SUCCESS);
166 	SelectObject(hDC, GetStockObject(WHITE_PEN));
167 	hObj = GetCurrentObject(hDC, OBJ_PEN);
168 	ok(hObj == GetStockObject(WHITE_PEN), "Expected %p, got %p\n", GetStockObject(WHITE_PEN), hObj);
169 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
170 
171 	/* DC pen */
172 	SetLastError(ERROR_SUCCESS);
173 	SelectObject(hDC, GetStockObject(DC_PEN));
174 	hObj = GetCurrentObject(hDC, OBJ_PEN);
175 	ok(hObj == GetStockObject(DC_PEN), "Expected %p, got %p\n", GetStockObject(DC_PEN), hObj);
176 	ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %lu\n", GetLastError());
177 
178 	ReleaseDC(hWnd, hDC);
179 	DestroyWindow(hWnd);
180 }
181 
182 START_TEST(GetCurrentObject)
183 {
184     Test_GetCurrentObject();
185 }
186 
187