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