[GDI32_APITEST]
[reactos.git] / rostests / apitests / gdi32 / SelectObject.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for SelectObject
5 * PROGRAMMERS: Timo Kreuzer
6 */
7
8 #include <stdio.h>
9 #include <wine/test.h>
10 #include <windows.h>
11 #include <winddi.h>
12 #include <reactos/win32k/ntgdityp.h>
13 #include <reactos/win32k/ntgdihdl.h>
14
15 #define TEST(x) ok(x, #x"\n")
16 #define RTEST(x) ok(x, #x"\n")
17
18 #define ok_err(dwErr) ok(GetLastError() == dwErr, "Wrong LastError, expected %d, got %ld\n", dwErr, GetLastError())
19
20 HDC hdc1, hdc2;
21
22 static void
23 Test_SelectObject()
24 {
25 HGDIOBJ hOldObj, hNewObj;
26 // PGDI_TABLE_ENTRY pEntry;
27 // PDC_ATTR pDc_Attr;
28 // HANDLE hcmXform;
29
30 /* Get the Dc_Attr for later testing */
31 // pEntry = &GdiHandleTable[GDI_HANDLE_GET_INDEX(hdc1)];
32 // pDc_Attr = pEntry->UserData;
33 // ok(pDc_Attr != NULL, "Skipping tests.\n");
34 // if (pDc_Attr == NULL) return;
35
36 /* Test incomplete dc handle doesn't work */
37 SetLastError(ERROR_SUCCESS);
38 hNewObj = GetStockObject(GRAY_BRUSH);
39 hOldObj = SelectObject((HDC)GDI_HANDLE_GET_INDEX(hdc1), hNewObj);
40 ok_err(ERROR_INVALID_HANDLE);
41 ok(hOldObj == NULL, "\n");
42 // ok(pDc_Attr->hbrush == GetStockObject(WHITE_BRUSH), "\n");
43 SelectObject(hdc1, hOldObj);
44
45 /* Test incomplete hobj handle works */
46 hNewObj = GetStockObject(GRAY_BRUSH);
47 hOldObj = SelectObject(hdc1, (HGDIOBJ)GDI_HANDLE_GET_INDEX(hNewObj));
48 ok(hOldObj == GetStockObject(WHITE_BRUSH), "\n");
49 // ok(pDc_Attr->hbrush == hNewObj, "\n");
50 SelectObject(hdc1, hOldObj);
51
52 /* Test wrong hDC handle type */
53 SetLastError(ERROR_SUCCESS);
54 hNewObj = GetStockObject(GRAY_BRUSH);
55 hdc2 = (HDC)((UINT_PTR)hdc1 & ~GDI_HANDLE_TYPE_MASK);
56 hdc2 = (HDC)((UINT_PTR)hdc2 | GDI_OBJECT_TYPE_PEN);
57 hOldObj = SelectObject(hdc2, hNewObj);
58 ok_err(ERROR_INVALID_HANDLE);
59 ok(hOldObj == NULL, "\n");
60 // RTEST(pDc_Attr->hbrush == GetStockObject(WHITE_BRUSH));
61
62 /* Test wrong hobj handle type */
63 SetLastError(ERROR_SUCCESS);
64 hNewObj = GetStockObject(GRAY_BRUSH);
65 hNewObj = (HGDIOBJ)((UINT_PTR)hNewObj & ~GDI_HANDLE_TYPE_MASK);
66 hNewObj = (HGDIOBJ)((UINT_PTR)hNewObj | GDI_OBJECT_TYPE_PEN);
67 hOldObj = SelectObject(hdc1, hNewObj);
68 ok_err(ERROR_SUCCESS);
69 ok(hOldObj == NULL, "\n");
70 // RTEST(pDc_Attr->hbrush == GetStockObject(WHITE_BRUSH));
71
72 SetLastError(ERROR_SUCCESS);
73 hNewObj = (HGDIOBJ)0x00761234;
74 hOldObj = SelectObject(hdc1, hNewObj);
75 ok(hOldObj == NULL, "\n");
76 ok_err(ERROR_SUCCESS);
77 SelectObject(hdc1, hOldObj);
78
79 /* Test DC */
80 SetLastError(ERROR_SUCCESS);
81 hOldObj = SelectObject(hdc1, GetDC(NULL));
82 ok(hOldObj == NULL, "\n");
83 ok_err(ERROR_SUCCESS);
84
85
86 /* Test CLIOBJ */
87
88 /* Test PATH */
89
90 /* Test PALETTE */
91 SetLastError(ERROR_SUCCESS);
92 hNewObj = GetStockObject(DEFAULT_PALETTE);
93 hOldObj = SelectObject(hdc1, hNewObj);
94 RTEST(hOldObj == NULL);
95 ok_err(ERROR_INVALID_FUNCTION);
96
97 /* Test COLORSPACE */
98
99 /* Test FONT */
100
101 /* Test PFE */
102
103 /* Test BRUSH */
104 hNewObj = GetStockObject(GRAY_BRUSH);
105 hOldObj = SelectObject(hdc1, hNewObj);
106 RTEST(hOldObj == GetStockObject(WHITE_BRUSH));
107 // RTEST(pDc_Attr->hbrush == hNewObj);
108 RTEST(GDI_HANDLE_GET_TYPE(hOldObj) == GDI_OBJECT_TYPE_BRUSH);
109 SelectObject(hdc1, hOldObj);
110
111 /* Test DC_BRUSH */
112 hNewObj = GetStockObject(DC_BRUSH);
113 hOldObj = SelectObject(hdc1, hNewObj);
114 // RTEST(pDc_Attr->hbrush == hNewObj);
115 SelectObject(hdc1, hOldObj);
116
117 /* Test BRUSH color xform */
118 // hcmXform = (HANDLE)pDc_Attr->hcmXform;
119
120
121 /* Test EMF */
122
123 /* test METAFILE */
124
125 /* Test ENHMETAFILE */
126
127 /* Test EXTPEN */
128
129 /* Test METADC */
130 }
131
132 static void
133 Test_Bitmap()
134 {
135 HBITMAP hbmp, hbmpInvalid, hbmpOld;
136 BYTE bmBits[4] = {0};
137 HDC hdcTmp;
138
139 hbmp = CreateBitmap(2, 2, 1, 1, &bmBits);
140 hbmpInvalid = CreateBitmap(2, 2, 1, 4, &bmBits);
141 if (!hbmp || !hbmpInvalid)
142 {
143 printf("couldn't create bitmaps, skipping\n");
144 return;
145 }
146
147 hbmpOld = SelectObject(hdc1, hbmp);
148 ok(GDI_HANDLE_GET_TYPE(hbmpOld) == GDI_OBJECT_TYPE_BITMAP, "wrong type\n");
149
150 /* Test invalid BITMAP */
151 ok(SelectObject(hdc1, hbmpInvalid) == NULL, "should fail\n");
152
153 /* Test if we get the right bitmap back */
154 hbmpOld = SelectObject(hdc1, hbmpOld);
155 ok(hbmpOld == hbmp, "didn't get the right bitmap back.\n");
156
157 /* Test selecting bitmap into 2 DCs */
158 hbmpOld = SelectObject(hdc1, hbmp);
159 ok(SelectObject(hdc2, hbmp) == NULL, "Should fail.\n");
160
161 /* Test selecting same bitmap twice */
162 hbmpOld = SelectObject(hdc1, hbmp);
163 ok(hbmpOld == hbmp, "didn't get the right bitmap back.\n");
164 SelectObject(hdc1, GetStockObject(DEFAULT_BITMAP));
165
166 /* Test selecting and then deleting the DC */
167 hdcTmp = CreateCompatibleDC(NULL);
168 hbmpOld = SelectObject(hdcTmp, hbmp);
169 ok(hbmpOld == GetStockObject(DEFAULT_BITMAP), "didn't get the right bitmap back.\n");
170 DeleteDC(hdcTmp);
171 hbmpOld = SelectObject(hdc1, hbmp);
172 ok(hbmpOld == GetStockObject(DEFAULT_BITMAP), "didn't get the right bitmap back.\n");
173
174 DeleteObject(hbmp);
175 DeleteObject(hbmpInvalid);
176 }
177
178 static void
179 Test_Pen()
180 {
181 HPEN hpen, hpenOld;
182
183 /* Test PEN */
184 hpen = GetStockObject(GRAY_BRUSH);
185 hpenOld = SelectObject(hdc1, hpen);
186 ok(hpenOld == GetStockObject(WHITE_BRUSH), "Got wrong pen.\n");
187 // RTEST(pDc_Attr->hbrush == hpen);
188 ok(GDI_HANDLE_GET_TYPE(hpenOld) == GDI_OBJECT_TYPE_BRUSH, "wrong type.\n");
189 SelectObject(hdc1, hpenOld);
190 }
191
192 static void
193 Test_Region()
194 {
195 HRGN hrgn, hrgnOld;
196
197 /* Test REGION */
198 SetLastError(ERROR_SUCCESS);
199 hrgn = CreateRectRgn(0,0,0,0);
200 hrgnOld = SelectObject(hdc1, hrgn);
201 ok((UINT_PTR)hrgnOld == NULLREGION, "\n");
202 DeleteObject(hrgn);
203
204 hrgn = CreateRectRgn(0,0,10,10);
205 ok((UINT_PTR)SelectObject(hdc1, hrgn) == SIMPLEREGION, "\n");
206 hrgnOld = CreateRectRgn(5,5,20,20);
207 ok(CombineRgn(hrgn, hrgn, hrgnOld, RGN_OR) == COMPLEXREGION, "\n");
208 DeleteObject(hrgnOld);
209 ok((UINT_PTR)SelectObject(hdc1, hrgn) == SIMPLEREGION, "\n"); // ??? Why this?
210 DeleteObject(hrgn);
211 // ok(IsHandleValid(hrgn) == TRUE, "\n");
212 ok_err(ERROR_SUCCESS);
213 }
214
215 START_TEST(SelectObject)
216 {
217 hdc1 = CreateCompatibleDC(NULL);
218 hdc2 = CreateCompatibleDC(NULL);
219 if (!hdc1 || !hdc2)
220 {
221 printf("couldn't create DCs, skipping all tests\n");
222 return;
223 }
224
225 Test_SelectObject();
226 Test_Bitmap();
227 Test_Pen();
228 Test_Region();
229 }
230