2 * Unit test suite for GDI objects
4 * Copyright 2002 Mike McCormack
5 * Copyright 2004 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/test.h"
33 static void check_font(const char* test, const LOGFONTA* lf, HFONT hfont)
41 ret = GetObject(hfont, sizeof(getobj_lf), &getobj_lf);
42 /* NT4 tries to be clever and only returns the minimum length */
43 while (lf->lfFaceName[minlen] && minlen < LF_FACESIZE-1)
45 minlen += FIELD_OFFSET(LOGFONTA, lfFaceName) + 1;
46 ok(ret == sizeof(LOGFONTA) || ret == minlen,
47 "%s: GetObject returned %d expected %d or %d\n", test, ret, sizeof(LOGFONTA), minlen);
48 ok(!memcmp(&lf, &lf, FIELD_OFFSET(LOGFONTA, lfFaceName)), "%s: fonts don't match\n", test);
49 ok(!lstrcmpA(lf->lfFaceName, getobj_lf.lfFaceName),
50 "%s: font names don't match: %s != %s\n", test, lf->lfFaceName, getobj_lf.lfFaceName);
53 static HFONT create_font(const char* test, const LOGFONTA* lf)
55 HFONT hfont = CreateFontIndirectA(lf);
56 ok(hfont != 0, "%s: CreateFontIndirect failed\n", test);
58 check_font(test, lf, hfont);
62 static void test_logfont(void)
67 memset(&lf, 0, sizeof lf);
69 lf.lfCharSet = ANSI_CHARSET;
70 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
71 lf.lfWeight = FW_DONTCARE;
74 lf.lfQuality = DEFAULT_QUALITY;
76 lstrcpyA(lf.lfFaceName, "Arial");
77 hfont = create_font("Arial", &lf);
80 memset(&lf, 'A', sizeof(lf));
81 hfont = CreateFontIndirectA(&lf);
82 ok(hfont != 0, "CreateFontIndirectA with strange LOGFONT failed\n");
84 lf.lfFaceName[LF_FACESIZE - 1] = 0;
85 check_font("AAA...", &lf, hfont);
89 static INT CALLBACK font_enum_proc(const LOGFONT *elf, const TEXTMETRIC *ntm, DWORD type, LPARAM lParam)
91 if (type & RASTER_FONTTYPE)
93 LOGFONT *lf = (LOGFONT *)lParam;
95 return 0; /* stop enumeration */
98 return 1; /* continue enumeration */
101 static void test_font_metrics(HDC hdc, HFONT hfont, const char *test_str,
102 INT test_str_len, const TEXTMETRICA *tm_orig,
103 const SIZE *size_orig, INT width_orig,
104 INT scale_x, INT scale_y)
114 old_hfont = SelectObject(hdc, hfont);
116 GetTextMetricsA(hdc, &tm);
118 ok(tm.tmHeight == tm_orig->tmHeight * scale_y, "%ld != %ld\n", tm.tmHeight, tm_orig->tmHeight * scale_y);
119 ok(tm.tmAscent == tm_orig->tmAscent * scale_y, "%ld != %ld\n", tm.tmAscent, tm_orig->tmAscent * scale_y);
120 ok(tm.tmDescent == tm_orig->tmDescent * scale_y, "%ld != %ld\n", tm.tmDescent, tm_orig->tmDescent * scale_y);
121 ok(tm.tmAveCharWidth == tm_orig->tmAveCharWidth * scale_x, "%ld != %ld\n", tm.tmAveCharWidth, tm_orig->tmAveCharWidth * scale_x);
123 GetTextExtentPoint32A(hdc, test_str, test_str_len, &size);
125 ok(size.cx == size_orig->cx * scale_x, "%ld != %ld\n", size.cx, size_orig->cx * scale_x);
126 ok(size.cy == size_orig->cy * scale_y, "%ld != %ld\n", size.cy, size_orig->cy * scale_y);
128 GetCharWidthA(hdc, 'A', 'A', &width);
130 ok(width == width_orig * scale_x, "%d != %d\n", width, width_orig * scale_x);
132 SelectObject(hdc, old_hfont);
135 /* see whether GDI scales bitmap font metrics */
136 static void test_bitmap_font(void)
138 static const char test_str[11] = "Test String";
141 HFONT hfont, old_hfont;
144 INT ret, i, width_orig, height_orig;
148 /* "System" has only 1 pixel size defined, otherwise the test breaks */
149 ret = EnumFontFamiliesA(hdc, "System", font_enum_proc, (LPARAM)&bitmap_lf);
153 trace("no bitmap fonts were found, skipping the test\n");
157 trace("found bitmap font %s, height %ld\n", bitmap_lf.lfFaceName, bitmap_lf.lfHeight);
159 height_orig = bitmap_lf.lfHeight;
160 hfont = create_font("bitmap", &bitmap_lf);
162 old_hfont = SelectObject(hdc, hfont);
163 ok(GetTextMetricsA(hdc, &tm_orig), "GetTextMetricsA failed\n");
164 ok(GetTextExtentPoint32A(hdc, test_str, sizeof(test_str), &size_orig), "GetTextExtentPoint32A failed\n");
165 ok(GetCharWidthA(hdc, 'A', 'A', &width_orig), "GetCharWidthA failed\n");
166 SelectObject(hdc, old_hfont);
169 /* test fractional scaling */
170 for (i = 1; i < height_orig; i++)
172 hfont = create_font("fractional", &bitmap_lf);
173 test_font_metrics(hdc, hfont, test_str, sizeof(test_str), &tm_orig, &size_orig, width_orig, 1, 1);
177 /* test integer scaling 3x2 */
178 bitmap_lf.lfHeight = height_orig * 2;
179 bitmap_lf.lfWidth *= 3;
180 hfont = create_font("3x2", &bitmap_lf);
183 test_font_metrics(hdc, hfont, test_str, sizeof(test_str), &tm_orig, &size_orig, width_orig, 3, 2);
187 /* test integer scaling 3x3 */
188 bitmap_lf.lfHeight = height_orig * 3;
189 bitmap_lf.lfWidth = 0;
190 hfont = create_font("3x3", &bitmap_lf);
194 test_font_metrics(hdc, hfont, test_str, sizeof(test_str), &tm_orig, &size_orig, width_orig, 3, 3);
201 static void test_gdi_objects(void)
204 HDC hdc = GetDC(NULL);
209 /* SelectObject() with a NULL DC returns 0 and sets ERROR_INVALID_HANDLE.
210 * Note: Under XP at least invalid ptrs can also be passed, not just NULL;
211 * Don't test that here in case it crashes earlier win versions.
214 hp = SelectObject(NULL, GetStockObject(BLACK_PEN));
215 ok(!hp && GetLastError() == ERROR_INVALID_HANDLE,
216 "SelectObject(NULL DC) expected 0, ERROR_INVALID_HANDLE, got %p, 0x%08lx\n",
219 /* With a valid DC and a NULL object, the call returns 0 but does not SetLastError() */
221 hp = SelectObject(hdc, NULL);
222 ok(!hp && !GetLastError(),
223 "SelectObject(NULL obj) expected 0, NO_ERROR, got %p, 0x%08lx\n",
226 /* The DC is unaffected by the NULL SelectObject */
228 hp = SelectObject(hdc, GetStockObject(BLACK_PEN));
229 ok(hp && !GetLastError(),
230 "SelectObject(post NULL) expected non-null, NO_ERROR, got %p, 0x%08lx\n",
233 /* GetCurrentObject does not SetLastError() on a null object */
235 hp = GetCurrentObject(NULL, OBJ_PEN);
236 ok(!hp && !GetLastError(),
237 "GetCurrentObject(NULL DC) expected 0, NO_ERROR, got %p, 0x%08lx\n",
240 /* DeleteObject does not SetLastError() on a null object */
241 ret = DeleteObject(NULL);
242 ok( !ret && !GetLastError(),
243 "DeleteObject(NULL obj), expected 0, NO_ERROR, got %d, 0x%08lx\n",
244 ret, GetLastError());
246 /* GetObject does not SetLastError() on a null object */
248 i = GetObjectA(NULL, sizeof(buff), buff);
249 ok (!i && !GetLastError(),
250 "GetObject(NULL obj), expected 0, NO_ERROR, got %d, 0x%08lx\n",
253 /* GetObjectType does SetLastError() on a null object */
255 i = GetObjectType(NULL);
256 ok (!i && GetLastError() == ERROR_INVALID_HANDLE,
257 "GetObjectType(NULL obj), expected 0, ERROR_INVALID_HANDLE, got %d, 0x%08lx\n",
260 /* UnrealizeObject does not SetLastError() on a null object */
262 i = UnrealizeObject(NULL);
263 ok (!i && !GetLastError(),
264 "UnrealizeObject(NULL obj), expected 0, NO_ERROR, got %d, 0x%08lx\n",
267 ReleaseDC(NULL, hdc);
270 static void test_GdiGetCharDimensions(void)
276 LONG avgwidth, height;
277 static const char szAlphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
278 typedef LONG (WINAPI *fnGdiGetCharDimensions)(HDC hdc, LPTEXTMETRICW lptm, LONG *height);
279 fnGdiGetCharDimensions GdiGetCharDimensions = (fnGdiGetCharDimensions)GetProcAddress(LoadLibrary("gdi32"), "GdiGetCharDimensions");
280 if (!GdiGetCharDimensions) return;
282 hdc = CreateCompatibleDC(NULL);
284 GetTextExtentPoint(hdc, szAlphabet, strlen(szAlphabet), &size);
285 avgwidth = ((size.cx / 26) + 1) / 2;
287 ret = GdiGetCharDimensions(hdc, &tm, &height);
288 ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
289 ok(height == tm.tmHeight, "GdiGetCharDimensions should have set height to %ld instead of %ld\n", tm.tmHeight, height);
291 ret = GdiGetCharDimensions(hdc, &tm, NULL);
292 ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
294 ret = GdiGetCharDimensions(hdc, NULL, NULL);
295 ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
298 ret = GdiGetCharDimensions(hdc, NULL, &height);
299 ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
300 ok(height == size.cy, "GdiGetCharDimensions should have set height to %ld instead of %ld\n", size.cy, height);
305 static void test_text_extents(void)
313 memset(&lf, 0, sizeof(lf));
314 strcpy(lf.lfFaceName, "Arial");
317 hfont = CreateFontIndirectA(&lf);
319 hfont = SelectObject(hdc, hfont);
320 GetTextMetricsA(hdc, &tm);
321 GetTextExtentPointA(hdc, "o", 1, &sz);
322 ok(sz.cy == tm.tmHeight, "cy %ld tmHeight %ld\n", sz.cy, tm.tmHeight);
324 SelectObject(hdc, hfont);
326 ReleaseDC(NULL, hdc);
338 static DWORD WINAPI thread_proc(void *param)
341 struct hgdiobj_event *hgdiobj_event = (struct hgdiobj_event *)param;
343 hgdiobj_event->hdc = CreateDC("display", NULL, NULL, NULL);
344 ok(hgdiobj_event->hdc != NULL, "CreateDC error %ld\n", GetLastError());
346 hgdiobj_event->hgdiobj1 = CreatePen(PS_DASHDOTDOT, 17, RGB(1, 2, 3));
347 ok(hgdiobj_event->hgdiobj1 != 0, "Failed to create pen\n");
349 hgdiobj_event->hgdiobj2 = CreateRectRgn(0, 1, 12, 17);
350 ok(hgdiobj_event->hgdiobj2 != 0, "Failed to create pen\n");
352 SetEvent(hgdiobj_event->ready_event);
353 ok(WaitForSingleObject(hgdiobj_event->stop_event, INFINITE) == WAIT_OBJECT_0,
354 "WaitForSingleObject error %ld\n", GetLastError());
356 ok(!GetObject(hgdiobj_event->hgdiobj1, sizeof(lp), &lp), "GetObject should fail\n");
358 ok(!GetDeviceCaps(hgdiobj_event->hdc, TECHNOLOGY), "GetDeviceCaps(TECHNOLOGY) should fail\n");
363 static void test_thread_objects(void)
368 struct hgdiobj_event hgdiobj_event;
371 hgdiobj_event.stop_event = CreateEvent(NULL, 0, 0, NULL);
372 ok(hgdiobj_event.stop_event != NULL, "CreateEvent error %ld\n", GetLastError());
373 hgdiobj_event.ready_event = CreateEvent(NULL, 0, 0, NULL);
374 ok(hgdiobj_event.ready_event != NULL, "CreateEvent error %ld\n", GetLastError());
376 hthread = CreateThread(NULL, 0, thread_proc, &hgdiobj_event, 0, &tid);
377 ok(hthread != NULL, "CreateThread error %ld\n", GetLastError());
379 ok(WaitForSingleObject(hgdiobj_event.ready_event, INFINITE) == WAIT_OBJECT_0,
380 "WaitForSingleObject error %ld\n", GetLastError());
382 ok(GetObject(hgdiobj_event.hgdiobj1, sizeof(lp), &lp) == sizeof(lp),
383 "GetObject error %ld\n", GetLastError());
384 ok(lp.lopnStyle == PS_DASHDOTDOT, "wrong pen style %d\n", lp.lopnStyle);
385 ok(lp.lopnWidth.x == 17, "wrong pen width.y %ld\n", lp.lopnWidth.x);
386 ok(lp.lopnWidth.y == 0, "wrong pen width.y %ld\n", lp.lopnWidth.y);
387 ok(lp.lopnColor == RGB(1, 2, 3), "wrong pen width.y %08lx\n", lp.lopnColor);
389 ret = GetDeviceCaps(hgdiobj_event.hdc, TECHNOLOGY);
390 ok(ret == DT_RASDISPLAY, "GetDeviceCaps(TECHNOLOGY) should return DT_RASDISPLAY not %d\n", ret);
392 ok(DeleteObject(hgdiobj_event.hgdiobj1), "DeleteObject error %ld\n", GetLastError());
393 ok(DeleteDC(hgdiobj_event.hdc), "DeleteDC error %ld\n", GetLastError());
395 type = GetObjectType(hgdiobj_event.hgdiobj2);
396 ok(type == OBJ_REGION, "GetObjectType returned %lu\n", type);
398 SetEvent(hgdiobj_event.stop_event);
399 ok(WaitForSingleObject(hthread, INFINITE) == WAIT_OBJECT_0,
400 "WaitForSingleObject error %ld\n", GetLastError());
401 CloseHandle(hthread);
403 type = GetObjectType(hgdiobj_event.hgdiobj2);
404 ok(type == OBJ_REGION, "GetObjectType returned %lu\n", type);
405 ok(DeleteObject(hgdiobj_event.hgdiobj2), "DeleteObject error %ld\n", GetLastError());
407 CloseHandle(hgdiobj_event.stop_event);
408 CloseHandle(hgdiobj_event.ready_event);
411 static void test_GetCurrentObject(void)
426 hdc = CreateCompatibleDC(0);
429 type = GetObjectType(hdc);
430 ok(type == OBJ_MEMDC, "GetObjectType returned %lu\n", type);
432 hpen = CreatePen(PS_SOLID, 10, RGB(10, 20, 30));
434 SelectObject(hdc, hpen);
435 hobj = GetCurrentObject(hdc, OBJ_PEN);
436 ok(hobj == hpen, "OBJ_PEN is wrong: %p\n", hobj);
437 hobj = GetCurrentObject(hdc, OBJ_EXTPEN);
438 ok(hobj == hpen, "OBJ_EXTPEN is wrong: %p\n", hobj);
440 hbrush = CreateSolidBrush(RGB(10, 20, 30));
442 SelectObject(hdc, hbrush);
443 hobj = GetCurrentObject(hdc, OBJ_BRUSH);
444 ok(hobj == hbrush, "OBJ_BRUSH is wrong: %p\n", hobj);
446 hpal = CreateHalftonePalette(hdc);
448 SelectPalette(hdc, hpal, FALSE);
449 hobj = GetCurrentObject(hdc, OBJ_PAL);
450 ok(hobj == hpal, "OBJ_PAL is wrong: %p\n", hobj);
452 hfont = CreateFontA(10, 5, 0, 0, FW_DONTCARE, 0, 0, 0, ANSI_CHARSET,
453 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
454 DEFAULT_PITCH, "MS Sans Serif");
456 SelectObject(hdc, hfont);
457 hobj = GetCurrentObject(hdc, OBJ_FONT);
458 ok(hobj == hfont, "OBJ_FONT is wrong: %p\n", hobj);
460 hbmp = CreateBitmap(100, 100, 1, 1, NULL);
462 SelectObject(hdc, hbmp);
463 hobj = GetCurrentObject(hdc, OBJ_BITMAP);
464 ok(hobj == hbmp, "OBJ_BITMAP is wrong: %p\n", hobj);
466 assert(GetObject(hbrush, sizeof(lb), &lb) == sizeof(lb));
467 hpen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_SQUARE | PS_JOIN_BEVEL,
470 SelectObject(hdc, hpen);
471 hobj = GetCurrentObject(hdc, OBJ_PEN);
472 ok(hobj == hpen, "OBJ_PEN is wrong: %p\n", hobj);
473 hobj = GetCurrentObject(hdc, OBJ_EXTPEN);
474 ok(hobj == hpen, "OBJ_EXTPEN is wrong: %p\n", hobj);
476 hcs = GetColorSpace(hdc);
479 trace("current color space is not NULL\n");
480 ok(GetLogColorSpaceA(hcs, &lcs, sizeof(lcs)), "GetLogColorSpace failed\n");
481 hcs = CreateColorSpaceA(&lcs);
482 ok(hcs != 0, "CreateColorSpace failed\n");
483 SelectObject(hdc, hcs);
484 hobj = GetCurrentObject(hdc, OBJ_COLORSPACE);
485 ok(hobj == hcs, "OBJ_COLORSPACE is wrong: %p\n", hobj);
488 hrgn = CreateRectRgn(1, 1, 100, 100);
490 SelectObject(hdc, hrgn);
491 hobj = GetCurrentObject(hdc, OBJ_REGION);
492 ok(!hobj, "OBJ_REGION is wrong: %p\n", hobj);
502 test_GdiGetCharDimensions();
504 test_thread_objects();
505 test_GetCurrentObject();