secur32: Fix conversion of password in NTLM AcquireCredentialsHandleA.
[wine] / dlls / gdi / tests / gdiobj.c
1 /*
2  * Unit test suite for GDI objects
3  *
4  * Copyright 2002 Mike McCormack
5  * Copyright 2004 Dmitry Timoshkov
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <stdarg.h>
23 #include <assert.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29
30 #include "wine/test.h"
31
32
33 static void check_font(const char* test, const LOGFONTA* lf, HFONT hfont)
34 {
35     LOGFONTA getobj_lf;
36     int ret, minlen = 0;
37
38     if (!hfont)
39         return;
40
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)
44         minlen++;
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);
51 }
52
53 static HFONT create_font(const char* test, const LOGFONTA* lf)
54 {
55     HFONT hfont = CreateFontIndirectA(lf);
56     ok(hfont != 0, "%s: CreateFontIndirect failed\n", test);
57     if (hfont)
58         check_font(test, lf, hfont);
59     return hfont;
60 }
61
62 static void test_logfont(void)
63 {
64     LOGFONTA lf;
65     HFONT hfont;
66
67     memset(&lf, 0, sizeof lf);
68
69     lf.lfCharSet = ANSI_CHARSET;
70     lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
71     lf.lfWeight = FW_DONTCARE;
72     lf.lfHeight = 16;
73     lf.lfWidth = 16;
74     lf.lfQuality = DEFAULT_QUALITY;
75
76     lstrcpyA(lf.lfFaceName, "Arial");
77     hfont = create_font("Arial", &lf);
78     DeleteObject(hfont);
79
80     memset(&lf, 'A', sizeof(lf));
81     hfont = CreateFontIndirectA(&lf);
82     ok(hfont != 0, "CreateFontIndirectA with strange LOGFONT failed\n");
83     
84     lf.lfFaceName[LF_FACESIZE - 1] = 0;
85     check_font("AAA...", &lf, hfont);
86     DeleteObject(hfont);
87 }
88
89 static INT CALLBACK font_enum_proc(const LOGFONT *elf, const TEXTMETRIC *ntm, DWORD type, LPARAM lParam)
90 {
91     if (type & RASTER_FONTTYPE)
92     {
93         LOGFONT *lf = (LOGFONT *)lParam;
94         *lf = *elf;
95         return 0; /* stop enumeration */
96     }
97
98     return 1; /* continue enumeration */
99 }
100
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)
105 {
106     HFONT old_hfont;
107     TEXTMETRICA tm;
108     SIZE size;
109     INT width;
110
111     if (!hfont)
112         return;
113
114     old_hfont = SelectObject(hdc, hfont);
115
116     GetTextMetricsA(hdc, &tm);
117
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);
122
123     GetTextExtentPoint32A(hdc, test_str, test_str_len, &size);
124
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);
127
128     GetCharWidthA(hdc, 'A', 'A', &width);
129
130     ok(width == width_orig * scale_x, "%d != %d\n", width, width_orig * scale_x);
131
132     SelectObject(hdc, old_hfont);
133 }
134
135 /* see whether GDI scales bitmap font metrics */
136 static void test_bitmap_font(void)
137 {
138     static const char test_str[11] = "Test String";
139     HDC hdc;
140     LOGFONTA bitmap_lf;
141     HFONT hfont, old_hfont;
142     TEXTMETRICA tm_orig;
143     SIZE size_orig;
144     INT ret, i, width_orig, height_orig;
145
146     hdc = GetDC(0);
147
148     /* "System" has only 1 pixel size defined, otherwise the test breaks */
149     ret = EnumFontFamiliesA(hdc, "System", font_enum_proc, (LPARAM)&bitmap_lf);
150     if (ret)
151     {
152         ReleaseDC(0, hdc);
153         trace("no bitmap fonts were found, skipping the test\n");
154         return;
155     }
156
157     trace("found bitmap font %s, height %ld\n", bitmap_lf.lfFaceName, bitmap_lf.lfHeight);
158
159     height_orig = bitmap_lf.lfHeight;
160     hfont = create_font("bitmap", &bitmap_lf);
161
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);
167     DeleteObject(hfont);
168
169     /* test fractional scaling */
170     for (i = 1; i < height_orig; i++)
171     {
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);
174         DeleteObject(hfont);
175     }
176
177     /* test integer scaling 3x2 */
178     bitmap_lf.lfHeight = height_orig * 2;
179     bitmap_lf.lfWidth *= 3;
180     hfont = create_font("3x2", &bitmap_lf);
181 todo_wine
182 {
183     test_font_metrics(hdc, hfont, test_str, sizeof(test_str), &tm_orig, &size_orig, width_orig, 3, 2);
184 }
185     DeleteObject(hfont);
186
187     /* test integer scaling 3x3 */
188     bitmap_lf.lfHeight = height_orig * 3;
189     bitmap_lf.lfWidth = 0;
190     hfont = create_font("3x3", &bitmap_lf);
191
192 todo_wine
193 {
194     test_font_metrics(hdc, hfont, test_str, sizeof(test_str), &tm_orig, &size_orig, width_orig, 3, 3);
195 }
196     DeleteObject(hfont);
197
198     ReleaseDC(0, hdc);
199 }
200
201 static void test_gdi_objects(void)
202 {
203     BYTE buff[256];
204     HDC hdc = GetDC(NULL);
205     HPEN hp;
206     int i;
207     BOOL ret;
208
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.
212      */
213     SetLastError(0);
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",
217        hp, GetLastError());
218
219     /* With a valid DC and a NULL object, the call returns 0 but does not SetLastError() */
220     SetLastError(0);
221     hp = SelectObject(hdc, NULL);
222     ok(!hp && !GetLastError(),
223        "SelectObject(NULL obj) expected 0, NO_ERROR, got %p, 0x%08lx\n",
224        hp, GetLastError());
225
226     /* The DC is unaffected by the NULL SelectObject */
227     SetLastError(0);
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",
231        hp, GetLastError());
232
233     /* GetCurrentObject does not SetLastError() on a null object */
234     SetLastError(0);
235     hp = GetCurrentObject(NULL, OBJ_PEN);
236     ok(!hp && !GetLastError(),
237        "GetCurrentObject(NULL DC) expected 0, NO_ERROR, got %p, 0x%08lx\n",
238        hp, GetLastError());
239
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());
245
246     /* GetObject does not SetLastError() on a null object */
247     SetLastError(0);
248     i = GetObjectA(NULL, sizeof(buff), buff);
249     ok (!i && !GetLastError(),
250         "GetObject(NULL obj), expected 0, NO_ERROR, got %d, 0x%08lx\n",
251         i, GetLastError());
252
253     /* GetObjectType does SetLastError() on a null object */
254     SetLastError(0);
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",
258         i, GetLastError());
259
260     /* UnrealizeObject does not SetLastError() on a null object */
261     SetLastError(0);
262     i = UnrealizeObject(NULL);
263     ok (!i && !GetLastError(),
264         "UnrealizeObject(NULL obj), expected 0, NO_ERROR, got %d, 0x%08lx\n",
265         i, GetLastError());
266
267     ReleaseDC(NULL, hdc);
268 }
269
270 static void test_GdiGetCharDimensions(void)
271 {
272     HDC hdc;
273     TEXTMETRICW tm;
274     LONG ret;
275     SIZE size;
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;
281
282     hdc = CreateCompatibleDC(NULL);
283
284     GetTextExtentPoint(hdc, szAlphabet, strlen(szAlphabet), &size);
285     avgwidth = ((size.cx / 26) + 1) / 2;
286
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);
290
291     ret = GdiGetCharDimensions(hdc, &tm, NULL);
292     ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
293
294     ret = GdiGetCharDimensions(hdc, NULL, NULL);
295     ok(ret == avgwidth, "GdiGetCharDimensions should have returned width of %ld instead of %ld\n", avgwidth, ret);
296
297     height = 0;
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);
301
302     DeleteDC(hdc);
303 }
304
305 static void test_text_extents(void)
306 {
307     LOGFONTA lf;
308     TEXTMETRICA tm;
309     HDC hdc;
310     HFONT hfont;
311     SIZE sz;
312
313     memset(&lf, 0, sizeof(lf));
314     strcpy(lf.lfFaceName, "Arial");
315     lf.lfHeight = 20;
316
317     hfont = CreateFontIndirectA(&lf);
318     hdc = GetDC(0);
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);
323
324     SelectObject(hdc, hfont);
325     DeleteObject(hfont);
326     ReleaseDC(NULL, hdc);
327 }
328
329 struct hgdiobj_event
330 {
331     HDC hdc;
332     HGDIOBJ hgdiobj1;
333     HGDIOBJ hgdiobj2;
334     HANDLE stop_event;
335     HANDLE ready_event;
336 };
337
338 static DWORD WINAPI thread_proc(void *param)
339 {
340     LOGPEN lp;
341     struct hgdiobj_event *hgdiobj_event = (struct hgdiobj_event *)param;
342
343     hgdiobj_event->hdc = CreateDC("display", NULL, NULL, NULL);
344     ok(hgdiobj_event->hdc != NULL, "CreateDC error %ld\n", GetLastError());
345
346     hgdiobj_event->hgdiobj1 = CreatePen(PS_DASHDOTDOT, 17, RGB(1, 2, 3));
347     ok(hgdiobj_event->hgdiobj1 != 0, "Failed to create pen\n");
348
349     hgdiobj_event->hgdiobj2 = CreateRectRgn(0, 1, 12, 17);
350     ok(hgdiobj_event->hgdiobj2 != 0, "Failed to create pen\n");
351
352     SetEvent(hgdiobj_event->ready_event);
353     ok(WaitForSingleObject(hgdiobj_event->stop_event, INFINITE) == WAIT_OBJECT_0,
354        "WaitForSingleObject error %ld\n", GetLastError());
355
356     ok(!GetObject(hgdiobj_event->hgdiobj1, sizeof(lp), &lp), "GetObject should fail\n");
357
358     ok(!GetDeviceCaps(hgdiobj_event->hdc, TECHNOLOGY), "GetDeviceCaps(TECHNOLOGY) should fail\n");
359
360     return 0;
361 }
362
363 static void test_thread_objects(void)
364 {
365     LOGPEN lp;
366     DWORD tid, type;
367     HANDLE hthread;
368     struct hgdiobj_event hgdiobj_event;
369     INT ret;
370
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());
375
376     hthread = CreateThread(NULL, 0, thread_proc, &hgdiobj_event, 0, &tid);
377     ok(hthread != NULL, "CreateThread error %ld\n", GetLastError());
378
379     ok(WaitForSingleObject(hgdiobj_event.ready_event, INFINITE) == WAIT_OBJECT_0,
380        "WaitForSingleObject error %ld\n", GetLastError());
381
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);
388
389     ret = GetDeviceCaps(hgdiobj_event.hdc, TECHNOLOGY);
390     ok(ret == DT_RASDISPLAY, "GetDeviceCaps(TECHNOLOGY) should return DT_RASDISPLAY not %d\n", ret);
391
392     ok(DeleteObject(hgdiobj_event.hgdiobj1), "DeleteObject error %ld\n", GetLastError());
393     ok(DeleteDC(hgdiobj_event.hdc), "DeleteDC error %ld\n", GetLastError());
394
395     type = GetObjectType(hgdiobj_event.hgdiobj2);
396     ok(type == OBJ_REGION, "GetObjectType returned %lu\n", type);
397
398     SetEvent(hgdiobj_event.stop_event);
399     ok(WaitForSingleObject(hthread, INFINITE) == WAIT_OBJECT_0,
400        "WaitForSingleObject error %ld\n", GetLastError());
401     CloseHandle(hthread);
402
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());
406
407     CloseHandle(hgdiobj_event.stop_event);
408     CloseHandle(hgdiobj_event.ready_event);
409 }
410
411 static void test_GetCurrentObject(void)
412 {
413     DWORD type;
414     HPEN hpen;
415     HBRUSH hbrush;
416     HPALETTE hpal;
417     HFONT hfont;
418     HBITMAP hbmp;
419     HRGN hrgn;
420     HDC hdc;
421     HCOLORSPACE hcs;
422     HGDIOBJ hobj;
423     LOGBRUSH lb;
424     LOGCOLORSPACEA lcs;
425
426     hdc = CreateCompatibleDC(0);
427     assert(hdc != 0);
428
429     type = GetObjectType(hdc);
430     ok(type == OBJ_MEMDC, "GetObjectType returned %lu\n", type);
431
432     hpen = CreatePen(PS_SOLID, 10, RGB(10, 20, 30));
433     assert(hpen != 0);
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);
439
440     hbrush = CreateSolidBrush(RGB(10, 20, 30));
441     assert(hbrush != 0);
442     SelectObject(hdc, hbrush);
443     hobj = GetCurrentObject(hdc, OBJ_BRUSH);
444     ok(hobj == hbrush, "OBJ_BRUSH is wrong: %p\n", hobj);
445
446     hpal = CreateHalftonePalette(hdc);
447     assert(hpal != 0);
448     SelectPalette(hdc, hpal, FALSE);
449     hobj = GetCurrentObject(hdc, OBJ_PAL);
450     ok(hobj == hpal, "OBJ_PAL is wrong: %p\n", hobj);
451
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");
455     assert(hfont != 0);
456     SelectObject(hdc, hfont);
457     hobj = GetCurrentObject(hdc, OBJ_FONT);
458     ok(hobj == hfont, "OBJ_FONT is wrong: %p\n", hobj);
459
460     hbmp = CreateBitmap(100, 100, 1, 1, NULL);
461     assert(hbmp != 0);
462     SelectObject(hdc, hbmp);
463     hobj = GetCurrentObject(hdc, OBJ_BITMAP);
464     ok(hobj == hbmp, "OBJ_BITMAP is wrong: %p\n", hobj);
465
466     assert(GetObject(hbrush, sizeof(lb), &lb) == sizeof(lb));
467     hpen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_SQUARE | PS_JOIN_BEVEL,
468                         10, &lb, 0, NULL);
469     assert(hpen != 0);
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);
475
476     hcs = GetColorSpace(hdc);
477     if (hcs)
478     {
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);
486     }
487
488     hrgn = CreateRectRgn(1, 1, 100, 100);
489     assert(hrgn != 0);
490     SelectObject(hdc, hrgn);
491     hobj = GetCurrentObject(hdc, OBJ_REGION);
492     ok(!hobj, "OBJ_REGION is wrong: %p\n", hobj);
493
494     DeleteDC(hdc);
495 }
496
497 START_TEST(gdiobj)
498 {
499     test_logfont();
500     test_bitmap_font();
501     test_gdi_objects();
502     test_GdiGetCharDimensions();
503     test_text_extents();
504     test_thread_objects();
505     test_GetCurrentObject();
506 }