1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * PROJECT:         ReactOS api tests
 * LICENSE:         GPL - See COPYING in the top level directory
 * PURPOSE:         Test for SelectObject
 * PROGRAMMERS:     Timo Kreuzer
 */

#include "precomp.h"

HDC hdc1, hdc2;

static void
Test_SelectObject()
{
	HGDIOBJ hOldObj, hNewObj;
//	PGDI_TABLE_ENTRY pEntry;
//	PDC_ATTR pDc_Attr;
//	HANDLE hcmXform;

	/* Get the Dc_Attr for later testing */
//	pEntry = &GdiHandleTable[GDI_HANDLE_GET_INDEX(hdc1)];
//	pDc_Attr = pEntry->UserData;
//	ok(pDc_Attr != NULL, "Skipping tests.\n");
//	if (pDc_Attr == NULL) return;

	/* Test incomplete dc handle doesn't work */
	SetLastError(ERROR_SUCCESS);
	hNewObj = GetStockObject(GRAY_BRUSH);
	hOldObj = SelectObject((HDC)GDI_HANDLE_GET_INDEX(hdc1), hNewObj);
	ok_err(ERROR_INVALID_HANDLE);
	ok(hOldObj == NULL, "\n");
//	ok(pDc_Attr->hbrush == GetStockObject(WHITE_BRUSH), "\n");
	SelectObject(hdc1, hOldObj);

	/* Test incomplete hobj handle works */
	hNewObj = GetStockObject(GRAY_BRUSH);
	hOldObj = SelectObject(hdc1, (HGDIOBJ)GDI_HANDLE_GET_INDEX(hNewObj));
	ok(hOldObj == GetStockObject(WHITE_BRUSH), "\n");
//	ok(pDc_Attr->hbrush == hNewObj, "\n");
	SelectObject(hdc1, hOldObj);

	/* Test wrong hDC handle type */
	SetLastError(ERROR_SUCCESS);
	hNewObj = GetStockObject(GRAY_BRUSH);
	hdc2 = (HDC)((UINT_PTR)hdc1 & ~GDI_HANDLE_TYPE_MASK);
	hdc2 = (HDC)((UINT_PTR)hdc2 | GDI_OBJECT_TYPE_PEN);
	hOldObj = SelectObject(hdc2, hNewObj);
	ok_err(ERROR_INVALID_HANDLE);
	ok(hOldObj == NULL, "\n");
//	RTEST(pDc_Attr->hbrush == GetStockObject(WHITE_BRUSH));

	/* Test wrong hobj handle type */
	SetLastError(ERROR_SUCCESS);
	hNewObj = GetStockObject(GRAY_BRUSH);
	hNewObj = (HGDIOBJ)((UINT_PTR)hNewObj & ~GDI_HANDLE_TYPE_MASK);
	hNewObj = (HGDIOBJ)((UINT_PTR)hNewObj | GDI_OBJECT_TYPE_PEN);
	hOldObj = SelectObject(hdc1, hNewObj);
	ok_err(ERROR_SUCCESS);
	ok(hOldObj == NULL, "\n");
//	RTEST(pDc_Attr->hbrush == GetStockObject(WHITE_BRUSH));

	SetLastError(ERROR_SUCCESS);
	hNewObj = (HGDIOBJ)0x00761234;
	hOldObj = SelectObject(hdc1, hNewObj);
	ok(hOldObj == NULL, "\n");
	ok_err(ERROR_SUCCESS);
	SelectObject(hdc1, hOldObj);

	/* Test DC */
	SetLastError(ERROR_SUCCESS);
	hOldObj = SelectObject(hdc1, GetDC(NULL));
	ok(hOldObj == NULL, "\n");
	ok_err(ERROR_SUCCESS);


	/* Test CLIOBJ */

	/* Test PATH */

	/* Test PALETTE */
	SetLastError(ERROR_SUCCESS);
	hNewObj = GetStockObject(DEFAULT_PALETTE);
	hOldObj = SelectObject(hdc1, hNewObj);
	RTEST(hOldObj == NULL);
	ok_err(ERROR_INVALID_FUNCTION);

	/* Test COLORSPACE */

	/* Test FONT */

	/* Test PFE */

	/* Test BRUSH */
	hNewObj = GetStockObject(GRAY_BRUSH);
	hOldObj = SelectObject(hdc1, hNewObj);
	RTEST(hOldObj == GetStockObject(WHITE_BRUSH));
//	RTEST(pDc_Attr->hbrush == hNewObj);
	RTEST(GDI_HANDLE_GET_TYPE(hOldObj) == GDI_OBJECT_TYPE_BRUSH);
	SelectObject(hdc1, hOldObj);

	/* Test DC_BRUSH */
	hNewObj = GetStockObject(DC_BRUSH);
	hOldObj = SelectObject(hdc1, hNewObj);
//	RTEST(pDc_Attr->hbrush == hNewObj);
	SelectObject(hdc1, hOldObj);

	/* Test BRUSH color xform */
//	hcmXform = (HANDLE)pDc_Attr->hcmXform;


	/* Test EMF */

	/* test METAFILE */
	SetLastError(ERROR_SUCCESS);
	hNewObj = CreateMetaFile(NULL);
	ok(hNewObj != 0, "failed to create a meta dc\n");
	hOldObj = SelectObject(hdc1, hNewObj);
	RTEST(hOldObj == NULL);
	ok_err(ERROR_SUCCESS);

	/* Test ENHMETAFILE */

	/* Test EXTPEN */

	/* Test METADC */
}

static void
Test_Bitmap()
{
    HBITMAP hbmp, hbmpInvalid, hbmpOld;
	BYTE bmBits[4] = {0};
	HDC hdcTmp;

	hbmp = CreateBitmap(2, 2, 1, 1, &bmBits);
	hbmpInvalid = CreateBitmap(2, 2, 1, 4, &bmBits);
	if (!hbmp || !hbmpInvalid)
	{
	    printf("couldn't create bitmaps, skipping\n");
	    return;
	}

    hbmpOld = SelectObject(hdc1, hbmp);
    ok(GDI_HANDLE_GET_TYPE(hbmpOld) == GDI_OBJECT_TYPE_BITMAP, "wrong type\n");

	/* Test invalid BITMAP */
    ok(SelectObject(hdc1, hbmpInvalid) == NULL, "should fail\n");

    /* Test if we get the right bitmap back */
    hbmpOld = SelectObject(hdc1, hbmpOld);
    ok(hbmpOld == hbmp, "didn't get the right bitmap back.\n");

    /* Test selecting bitmap into 2 DCs */
    hbmpOld = SelectObject(hdc1, hbmp);<--- hbmpOld is assigned
    ok(SelectObject(hdc2, hbmp) == NULL, "Should fail.\n");

    /* Test selecting same bitmap twice */
    hbmpOld = SelectObject(hdc1, hbmp);<--- hbmpOld is overwritten
    ok(hbmpOld == hbmp, "didn't get the right bitmap back.\n");
    SelectObject(hdc1, GetStockObject(DEFAULT_BITMAP));

    /* Test selecting and then deleting the DC */
    hdcTmp = CreateCompatibleDC(NULL);
    hbmpOld = SelectObject(hdcTmp, hbmp);
    ok(hbmpOld == GetStockObject(DEFAULT_BITMAP), "didn't get the right bitmap back.\n");
    DeleteDC(hdcTmp);
    hbmpOld = SelectObject(hdc1, hbmp);
    ok(hbmpOld == GetStockObject(DEFAULT_BITMAP), "didn't get the right bitmap back.\n");

    DeleteObject(hbmp);
    DeleteObject(hbmpInvalid);
}

static void
Test_Pen()
{
    HPEN hpen, hpenOld;

	/* Test PEN */
	hpen = GetStockObject(GRAY_BRUSH);
	hpenOld = SelectObject(hdc1, hpen);
	ok(hpenOld == GetStockObject(WHITE_BRUSH), "Got wrong pen.\n");
//	RTEST(pDc_Attr->hbrush == hpen);
	ok(GDI_HANDLE_GET_TYPE(hpenOld) == GDI_OBJECT_TYPE_BRUSH, "wrong type.\n");
	SelectObject(hdc1, hpenOld);
}

static void
Test_Region()
{
    HRGN hrgn, hrgnOld;

	/* Test REGION */
	SetLastError(ERROR_SUCCESS);
	hrgn = CreateRectRgn(0,0,0,0);
	hrgnOld = SelectObject(hdc1, hrgn);
	ok((UINT_PTR)hrgnOld == NULLREGION, "\n");
	DeleteObject(hrgn);

	hrgn = CreateRectRgn(0,0,10,10);
	ok((UINT_PTR)SelectObject(hdc1, hrgn) == SIMPLEREGION, "\n");
	hrgnOld = CreateRectRgn(5,5,20,20);
	ok(CombineRgn(hrgn, hrgn, hrgnOld, RGN_OR) == COMPLEXREGION, "\n");
	DeleteObject(hrgnOld);
	ok((UINT_PTR)SelectObject(hdc1, hrgn) == SIMPLEREGION, "\n"); // ??? Why this?
	DeleteObject(hrgn);
//	ok(IsHandleValid(hrgn) == TRUE, "\n");
	ok_err(ERROR_SUCCESS);
}

START_TEST(SelectObject)
{
	hdc1 = CreateCompatibleDC(NULL);
	hdc2 = CreateCompatibleDC(NULL);
	if (!hdc1 || !hdc2)
	{
	    printf("couldn't create DCs, skipping all tests\n");
	    return;
	}

    Test_SelectObject();
    Test_Bitmap();
    Test_Pen();
    Test_Region();
}