5cf49871738db1ec36d512e612f694053d395d9f
[reactos.git] / modules / rostests / winetests / oleaut32 / olefont.c
1 /*
2 * OLEFONT test program
3 *
4 * Copyright 2003 Marcus Meissner
5 * Copyright 2006 (Google) Benjamin Arai
6 *
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #define WIN32_NO_STATUS
24 #define _INC_WINDOWS
25 #define COM_NO_WINDOWS_H
26
27 //#include <stdarg.h>
28 //#include <stdio.h>
29 //#include <math.h>
30 //#include <float.h>
31
32 #define COBJMACROS
33
34 #include <wine/test.h>
35 //#include <windef.h>
36 //#include <winbase.h>
37 //#include <winuser.h>
38 #include <wingdi.h>
39 //#include <winnls.h>
40 //#include <winerror.h>
41 //#include <winnt.h>
42 //#include <initguid.h>
43 //#include <wtypes.h>
44 #include <ole2.h>
45 #include <olectl.h>
46 //#include <ocidl.h>
47
48 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
49
50 static WCHAR MSSansSerif_font[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0};
51 static WCHAR system_font[] = { 'S','y','s','t','e','m',0 };
52 static WCHAR arial_font[] = { 'A','r','i','a','l',0 };
53 static WCHAR marlett_font[] = { 'M','a','r','l','e','t','t',0 };
54
55 static HMODULE hOleaut32;
56
57 static HRESULT (WINAPI *pOleCreateFontIndirect)(LPFONTDESC,REFIID,LPVOID*);
58
59 #define EXPECT_HR(hr,hr_exp) \
60 ok(hr == hr_exp, "got 0x%08x, expected 0x%08x\n", hr, hr_exp)
61
62 /* Create a font with cySize given by lo_size, hi_size, */
63 /* SetRatio to ratio_logical, ratio_himetric, */
64 /* check that resulting hfont has height hfont_height. */
65 /* Various checks along the way. */
66 static void test_ifont_size(LONGLONG size, LONG ratio_logical, LONG ratio_himetric,
67 LONG hfont_height, const char * test_name)
68 {
69 FONTDESC fd;
70 LPVOID pvObj = NULL;
71 IFont* ifnt = NULL;
72 HFONT hfont;
73 LOGFONTA lf;
74 CY psize;
75 HRESULT hres;
76 DWORD rtnval;
77
78 fd.cbSizeofstruct = sizeof(FONTDESC);
79 fd.lpstrName = arial_font; /* using scalable instead of bitmap font reduces errors due to font realization */
80 fd.cySize.int64 = size;
81 fd.sWeight = 0;
82 fd.sCharset = 0;
83 fd.fItalic = FALSE;
84 fd.fUnderline = FALSE;
85 fd.fStrikethrough = FALSE;
86
87 /* Create font, test that it worked. */
88 hres = pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj);
89 ifnt = pvObj;
90 ok(hres == S_OK,"%s: OCFI returns 0x%08x instead of S_OK.\n",
91 test_name, hres);
92 ok(pvObj != NULL,"%s: OCFI returns NULL.\n", test_name);
93
94 /* Change the scaling ratio */
95 hres = IFont_SetRatio(ifnt, ratio_logical, ratio_himetric);
96 ok((ratio_logical && ratio_himetric) ? hres == S_OK : hres == E_FAIL,
97 "%s: IFont_SetRatio unexpectedly returned 0x%08x.\n", test_name, hres);
98
99 /* Read back size. */
100 hres = IFont_get_Size(ifnt, &psize);
101 ok(hres == S_OK,"%s: IFont_get_size returns 0x%08x instead of S_OK.\n",
102 test_name, hres);
103
104 /* Check returned size - allow for errors due to rounding & font realization. */
105 ok((psize.int64 - size) < 10000 && (psize.int64 - size) > -10000,
106 "%s: IFont_get_Size: Lo=%d, Hi=%d; expected Lo=%d, Hi=%d.\n",
107 test_name, S(psize).Lo, S(psize).Hi, fd.cySize.Lo, fd.cySize.Hi);
108
109 /* Check hFont size. */
110 hres = IFont_get_hFont (ifnt, &hfont);
111 ok(hres == S_OK, "%s: IFont_get_hFont returns 0x%08x instead of S_OK.\n",
112 test_name, hres);
113 rtnval = GetObjectA(hfont, sizeof(LOGFONTA), &lf);
114 ok(rtnval > 0, "GetObject(hfont) failed\n");
115
116 /* Since font scaling may encounter rounding errors, allow 1 pixel deviation. */
117 ok(abs(lf.lfHeight - hfont_height) <= 1,
118 "%s: hFont has lf.lfHeight=%d, expected %d.\n",
119 test_name, lf.lfHeight, hfont_height);
120
121 /* Free IFont. */
122 IFont_Release(ifnt);
123 }
124
125 static void test_ifont_sizes(void)
126 {
127 /* Test various size operations and conversions. */
128 /* Add more as needed. */
129
130 /* Results of first 2 tests depend on display resolution. */
131 HDC hdc = GetDC(0);
132 LONG dpi = GetDeviceCaps(hdc, LOGPIXELSY); /* expected results depend on display DPI */
133 ReleaseDC(0, hdc);
134 if(dpi == 96) /* normal resolution display */
135 {
136 test_ifont_size(180000, 0, 0, -24, "default"); /* normal font */
137 test_ifont_size(186000, 0, 0, -25, "rounding"); /* test rounding */
138 } else if(dpi == 72) /* low resolution display */
139 {
140 test_ifont_size(180000, 0, 0, -18, "default"); /* normal font */
141 test_ifont_size(186000, 0, 0, -19, "rounding"); /* test rounding */
142 } else if(dpi == 120) /* high resolution display */
143 {
144 test_ifont_size(180000, 0, 0, -30, "default"); /* normal font */
145 test_ifont_size(186000, 0, 0, -31, "rounding"); /* test rounding */
146 } else
147 skip("Skipping resolution dependent font size tests - display resolution is %d\n", dpi);
148
149 /* Next 4 tests specify a scaling ratio, so display resolution is not a factor. */
150 test_ifont_size(180000, 72, 2540, -18, "ratio1"); /* change ratio */
151 test_ifont_size(180000, 144, 2540, -36, "ratio2"); /* another ratio */
152 test_ifont_size(180000, 72, 1270, -36, "ratio3"); /* yet another ratio */
153 test_ifont_size(186000, 72, 2540, -19, "rounding+ratio"); /* test rounding with ratio */
154
155 /* test various combinations of logical == himetric */
156 test_ifont_size(180000, 10, 10, -635, "identical ratio 1");
157 test_ifont_size(240000, 10, 10, -848, "identical ratio 2");
158 test_ifont_size(300000, 10, 10, -1058, "identical ratio 3");
159
160 /* test various combinations of logical and himetric both set to 1 */
161 test_ifont_size(180000, 1, 1, -24, "1:1 ratio 1");
162 test_ifont_size(240000, 1, 1, -32, "1:1 ratio 2");
163 test_ifont_size(300000, 1, 1, -40, "1:1 ratio 3");
164
165 /* test various combinations of logical set to 1 */
166 test_ifont_size(180000, 1, 0, -24, "1:0 ratio 1");
167 test_ifont_size(240000, 1, 0, -32, "1:0 ratio 2");
168 test_ifont_size(300000, 1, 0, -40, "1:0 ratio 3");
169
170 /* test various combinations of himetric set to 1 */
171 test_ifont_size(180000, 0, 1, -24, "0:1 ratio 1");
172 test_ifont_size(240000, 0, 1, -32, "0:1 ratio 2");
173 test_ifont_size(300000, 0, 1, -40, "0:1 ratio 3");
174
175 /* test various combinations of 2:1 logical:himetric */
176 test_ifont_size(180000, 2, 1, -1270, "2:1 ratio 1");
177 test_ifont_size(240000, 2, 1, -1694, "2:1 ratio 2");
178 test_ifont_size(300000, 2, 1, -2117, "2:1 ratio 3");
179
180 /* test various combinations of 1:2 logical:himetric */
181 test_ifont_size(180000, 1, 2, -318, "1:2 ratio 1");
182 test_ifont_size(240000, 1, 2, -424, "1:2 ratio 2");
183 test_ifont_size(300000, 1, 2, -529, "1:2 ratio 3");
184
185 /* test various combinations of logical and himetric both set to 2 */
186 test_ifont_size(180000, 2, 2, -635, "2:2 ratio 1");
187 test_ifont_size(240000, 2, 2, -848, "2:2 ratio 2");
188 test_ifont_size(300000, 2, 2, -1058, "2:2 ratio 3");
189 }
190
191 static void test_QueryInterface(void)
192 {
193 LPVOID pvObj = NULL;
194 HRESULT hr;
195 IFont* font = NULL;
196 LONG ref;
197
198 hr = pOleCreateFontIndirect(NULL, &IID_IFont, NULL);
199 EXPECT_HR(hr, E_POINTER);
200
201 hr = pOleCreateFontIndirect(NULL, &IID_IFont, &pvObj);
202 font = pvObj;
203
204 EXPECT_HR(hr, S_OK);
205 ok(font != NULL,"OCFI (NULL,..) returns NULL, instead of !NULL\n");
206
207 pvObj = NULL;
208 hr = IFont_QueryInterface( font, &IID_IFont, &pvObj);
209 EXPECT_HR(hr, S_OK);
210
211 /* Test if QueryInterface increments ref counter for IFONTs */
212 ref = IFont_AddRef(font);
213 ok(ref == 3 ||
214 broken(ref == 1), /* win95 */
215 "IFont_QI expected ref value 3 but instead got %d\n", ref);
216 IFont_Release(font);
217
218 ok(pvObj != NULL,"IFont_QI does return NULL, instead of a ptr\n");
219
220 IFont_Release(font);
221 IFont_Release(font);
222 }
223
224 static void test_type_info(void)
225 {
226 LPVOID pvObj = NULL;
227 HRESULT hres;
228 IFontDisp* fontdisp = NULL;
229 ITypeInfo* pTInfo;
230 WCHAR name_Name[] = {'N','a','m','e',0};
231 BSTR names[3];
232 UINT n;
233 LCID en_us = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
234 SORT_DEFAULT);
235 DISPPARAMS dispparams;
236 VARIANT varresult;
237
238 pOleCreateFontIndirect(NULL, &IID_IFontDisp, &pvObj);
239 fontdisp = pvObj;
240
241 hres = IFontDisp_GetTypeInfo(fontdisp, 0, en_us, &pTInfo);
242 ok(hres == S_OK, "GTI returned 0x%08x instead of S_OK.\n", hres);
243 ok(pTInfo != NULL, "GTI returned NULL.\n");
244
245 hres = ITypeInfo_GetNames(pTInfo, DISPID_FONT_NAME, names, 3, &n);
246 ok(hres == S_OK, "GetNames returned 0x%08x instead of S_OK.\n", hres);
247 ok(n == 1, "GetNames returned %d names instead of 1.\n", n);
248 ok(!lstrcmpiW(names[0],name_Name), "DISPID_FONT_NAME doesn't get 'Names'.\n");
249 SysFreeString(names[0]);
250
251 ITypeInfo_Release(pTInfo);
252
253 dispparams.cNamedArgs = 0;
254 dispparams.rgdispidNamedArgs = NULL;
255 dispparams.cArgs = 0;
256 dispparams.rgvarg = NULL;
257 VariantInit(&varresult);
258 hres = IFontDisp_Invoke(fontdisp, DISPID_FONT_NAME, &IID_NULL,
259 LOCALE_NEUTRAL, DISPATCH_PROPERTYGET, &dispparams, &varresult,
260 NULL, NULL);
261 ok(hres == S_OK, "IFontDisp_Invoke return 0x%08x instead of S_OK.\n", hres);
262 VariantClear(&varresult);
263
264 IFontDisp_Release(fontdisp);
265 }
266
267 static HRESULT WINAPI FontEventsDisp_QueryInterface(IFontEventsDisp *iface, REFIID riid, void **ppvObject)
268 {
269 if (IsEqualIID(riid, &IID_IFontEventsDisp) || IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDispatch))
270 {
271 IFontEventsDisp_AddRef(iface);
272 *ppvObject = iface;
273 return S_OK;
274 }
275 else
276 {
277 *ppvObject = NULL;
278 return E_NOINTERFACE;
279 }
280 }
281
282 static ULONG WINAPI FontEventsDisp_AddRef(
283 IFontEventsDisp *iface)
284 {
285 return 2;
286 }
287
288 static ULONG WINAPI FontEventsDisp_Release(
289 IFontEventsDisp *iface)
290 {
291 return 1;
292 }
293
294 static HRESULT WINAPI FontEventsDisp_GetTypeInfoCount(IFontEventsDisp *iface, UINT *pctinfo)
295 {
296 ok(0, "unexpected call\n");
297 return E_NOTIMPL;
298 }
299
300 static HRESULT WINAPI FontEventsDisp_GetTypeInfo(IFontEventsDisp *iface, UINT itinfo, LCID lcid, ITypeInfo **pptinfo)
301 {
302 ok(0, "unexpected call\n");
303 return E_NOTIMPL;
304 }
305
306 static HRESULT WINAPI FontEventsDisp_GetIDsOfNames(IFontEventsDisp *iface, REFIID riid, LPOLESTR *names, UINT cNames, LCID lcid,
307 DISPID *dispid)
308 {
309 ok(0, "unexpected call\n");
310 return E_NOTIMPL;
311 }
312
313 static int fonteventsdisp_invoke_called;
314 static BSTR fonteventsdisp_invoke_arg0;
315
316 static HRESULT WINAPI FontEventsDisp_Invoke(
317 IFontEventsDisp *iface,
318 DISPID dispid,
319 REFIID riid,
320 LCID lcid,
321 WORD wFlags,
322 DISPPARAMS *pDispParams,
323 VARIANT *pVarResult,
324 EXCEPINFO *pExcepInfo,
325 UINT *puArgErr)
326 {
327 VARIANTARG *arg0 = &pDispParams->rgvarg[0];
328
329 ok(dispid == DISPID_FONT_CHANGED, "expected DISPID_FONT_CHANGED instead of 0x%x\n", dispid);
330 ok(IsEqualGUID(riid, &GUID_NULL), "got riid %s\n", wine_dbgstr_guid(riid));
331 ok(wFlags == INVOKE_FUNC, "expected INVOKE_FUNC instead of 0x%x\n", wFlags);
332 ok(pDispParams->cArgs == 1, "expected arg count 1, got %d\n", pDispParams->cArgs);
333 ok(V_VT(arg0) == VT_BSTR, "expected VT_BSTR, got %d\n", V_VT(arg0));
334
335 fonteventsdisp_invoke_arg0 = SysAllocString(V_BSTR(arg0));
336 fonteventsdisp_invoke_called++;
337 return S_OK;
338 }
339
340 static IFontEventsDispVtbl FontEventsDisp_Vtbl =
341 {
342 FontEventsDisp_QueryInterface,
343 FontEventsDisp_AddRef,
344 FontEventsDisp_Release,
345 FontEventsDisp_GetTypeInfoCount,
346 FontEventsDisp_GetTypeInfo,
347 FontEventsDisp_GetIDsOfNames,
348 FontEventsDisp_Invoke
349 };
350
351 static IFontEventsDisp FontEventsDisp = { &FontEventsDisp_Vtbl };
352
353 struct font_dispid
354 {
355 DISPID dispid;
356 const WCHAR *name;
357 };
358
359 static void test_font_events_disp(void)
360 {
361 static const WCHAR nameW[] = {'N','a','m','e',0};
362 static const WCHAR sizeW[] = {'S','i','z','e',0};
363 static const WCHAR boldW[] = {'B','o','l','d',0};
364 static const WCHAR italicW[] = {'I','t','a','l','i','c',0};
365 static const WCHAR underlineW[] = {'U','n','d','e','r','l','i','n','e',0};
366 static const WCHAR strikeW[] = {'S','t','r','i','k','e','t','h','r','o','u','g','h',0};
367 static const WCHAR weightW[] = {'W','e','i','g','h','t',0};
368 static const WCHAR charsetW[] = {'C','h','a','r','s','e','t',0};
369
370 static const struct font_dispid font_dispids[] =
371 {
372 { DISPID_FONT_NAME, nameW },
373 { DISPID_FONT_SIZE, sizeW },
374 { DISPID_FONT_BOLD, boldW },
375 { DISPID_FONT_ITALIC, italicW },
376 { DISPID_FONT_UNDER, underlineW },
377 { DISPID_FONT_STRIKE, strikeW },
378 { DISPID_FONT_WEIGHT, weightW },
379 { DISPID_FONT_CHARSET, charsetW }
380 };
381
382 IFont *pFont;
383 IFont *pFont2;
384 IConnectionPointContainer *pCPC;
385 IConnectionPoint *pCP;
386 FONTDESC fontdesc;
387 HRESULT hr;
388 DWORD dwCookie;
389 IFontDisp *pFontDisp;
390 DISPPARAMS dispparams;
391 VARIANTARG vararg;
392 INT i;
393
394 fontdesc.cbSizeofstruct = sizeof(fontdesc);
395 fontdesc.lpstrName = MSSansSerif_font;
396 fontdesc.cySize.int64 = 12 * 10000; /* 12 pt */
397 fontdesc.sWeight = FW_NORMAL;
398 fontdesc.sCharset = 0;
399 fontdesc.fItalic = FALSE;
400 fontdesc.fUnderline = FALSE;
401 fontdesc.fStrikethrough = FALSE;
402
403 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void **)&pFont);
404 EXPECT_HR(hr, S_OK);
405
406 hr = IFont_QueryInterface(pFont, &IID_IConnectionPointContainer, (void **)&pCPC);
407 EXPECT_HR(hr, S_OK);
408
409 hr = IConnectionPointContainer_FindConnectionPoint(pCPC, &IID_IFontEventsDisp, &pCP);
410 EXPECT_HR(hr, S_OK);
411 IConnectionPointContainer_Release(pCPC);
412
413 hr = IConnectionPoint_Advise(pCP, (IUnknown *)&FontEventsDisp, &dwCookie);
414 EXPECT_HR(hr, S_OK);
415 IConnectionPoint_Release(pCP);
416
417 fonteventsdisp_invoke_called = 0;
418 fonteventsdisp_invoke_arg0 = NULL;
419 hr = IFont_put_Bold(pFont, TRUE);
420 EXPECT_HR(hr, S_OK);
421
422 ok(fonteventsdisp_invoke_called == 1, "IFontEventDisp::Invoke wasn't called once\n");
423 SysFreeString(fonteventsdisp_invoke_arg0);
424
425 hr = IFont_QueryInterface(pFont, &IID_IFontDisp, (void **)&pFontDisp);
426 EXPECT_HR(hr, S_OK);
427
428 for (i = 0; i < sizeof(font_dispids)/sizeof(font_dispids[0]); i++)
429 {
430 switch (font_dispids[i].dispid)
431 {
432 case DISPID_FONT_NAME:
433 {
434 static const WCHAR arialW[] = {'A','r','i','a','l',0};
435 V_VT(&vararg) = VT_BSTR;
436 V_BSTR(&vararg) = SysAllocString(arialW);
437 break;
438 }
439 case DISPID_FONT_SIZE:
440 V_VT(&vararg) = VT_CY;
441 S(V_CY(&vararg)).Lo = 25;
442 S(V_CY(&vararg)).Hi = 0;
443 break;
444 case DISPID_FONT_BOLD:
445 V_VT(&vararg) = VT_BOOL;
446 V_BOOL(&vararg) = VARIANT_FALSE;
447 break;
448 case DISPID_FONT_ITALIC:
449 case DISPID_FONT_UNDER:
450 case DISPID_FONT_STRIKE:
451 V_VT(&vararg) = VT_BOOL;
452 V_BOOL(&vararg) = VARIANT_TRUE;
453 break;
454 case DISPID_FONT_WEIGHT:
455 V_VT(&vararg) = VT_I2;
456 V_I2(&vararg) = FW_BLACK;
457 break;
458 case DISPID_FONT_CHARSET:
459 V_VT(&vararg) = VT_I2;
460 V_I2(&vararg) = 1;
461 break;
462 default:
463 ;
464 }
465
466 dispparams.cNamedArgs = 0;
467 dispparams.rgdispidNamedArgs = NULL;
468 dispparams.cArgs = 1;
469 dispparams.rgvarg = &vararg;
470 fonteventsdisp_invoke_called = 0;
471 hr = IFontDisp_Invoke(pFontDisp, font_dispids[i].dispid, &IID_NULL, 0, DISPATCH_PROPERTYPUT, &dispparams, NULL, NULL, NULL);
472 ok(hr == S_OK, "dispid=%d, got 0x%08x\n", font_dispids[i].dispid, hr);
473 ok(fonteventsdisp_invoke_called == 1, "dispid=%d, DISPID_FONT_CHANGED not called, got %d\n", font_dispids[i].dispid,
474 fonteventsdisp_invoke_called);
475 if (hr == S_OK)
476 {
477 ok(!lstrcmpW(font_dispids[i].name, fonteventsdisp_invoke_arg0), "dispid=%d, got %s, expected %s\n",
478 font_dispids[i].dispid, wine_dbgstr_w(fonteventsdisp_invoke_arg0), wine_dbgstr_w(font_dispids[i].name));
479 SysFreeString(fonteventsdisp_invoke_arg0);
480 }
481 VariantClear(&vararg);
482 }
483
484 IFontDisp_Release(pFontDisp);
485
486 hr = IFont_Clone(pFont, &pFont2);
487 EXPECT_HR(hr, S_OK);
488 IFont_Release(pFont);
489
490 /* this test shows that the notification routine isn't called again */
491 fonteventsdisp_invoke_called = 0;
492 hr = IFont_put_Bold(pFont2, FALSE);
493 EXPECT_HR(hr, S_OK);
494 ok(fonteventsdisp_invoke_called == 0, "got %d\n", fonteventsdisp_invoke_called);
495
496 IFont_Release(pFont2);
497 }
498
499 static void test_names_ids(WCHAR* w_name_1, const char* a_name_1,
500 WCHAR* w_name_2, const char* a_name_2,
501 LCID lcid, DISPID id_1, DISPID id_2,
502 HRESULT hres_expect, int numnames)
503 {
504 LPVOID pvObj = NULL;
505 IFontDisp *fontdisp = NULL;
506 HRESULT hres;
507 DISPID rgDispId[2] = {0xdeadbeef, 0xdeadbeef};
508 LPOLESTR names[2] = {w_name_1, w_name_2};
509
510 pOleCreateFontIndirect(NULL, &IID_IFontDisp, &pvObj);
511 fontdisp = pvObj;
512
513 hres = IFontDisp_GetIDsOfNames(fontdisp, &IID_NULL, names, numnames,
514 lcid, rgDispId);
515
516 /* test hres */
517 ok(hres == hres_expect,
518 "GetIDsOfNames: \"%s\", \"%s\" returns 0x%08x, expected 0x%08x.\n",
519 a_name_1, a_name_2, hres, hres_expect);
520
521 /* test first DISPID */
522 ok(rgDispId[0]==id_1,
523 "GetIDsOfNames: \"%s\" gets DISPID 0x%08x, expected 0x%08x.\n",
524 a_name_1, rgDispId[0], id_1);
525
526 /* test second DISPID is present */
527 if (numnames == 2)
528 {
529 ok(rgDispId[1]==id_2,
530 "GetIDsOfNames: ..., \"%s\" gets DISPID 0x%08x, expected 0x%08x.\n",
531 a_name_2, rgDispId[1], id_2);
532 }
533
534 IFontDisp_Release(fontdisp);
535 }
536
537 static void test_GetIDsOfNames(void)
538 {
539 WCHAR name_Name[] = {'N','a','m','e',0};
540 WCHAR name_Italic[] = {'I','t','a','l','i','c',0};
541 WCHAR name_Size[] = {'S','i','z','e',0};
542 WCHAR name_Bold[] = {'B','o','l','d',0};
543 WCHAR name_Underline[] = {'U','n','d','e','r','l','i','n','e',0};
544 WCHAR name_Strikethrough[] = {'S','t','r','i','k','e','t','h','r','o','u','g','h',0};
545 WCHAR name_Weight[] = {'W','e','i','g','h','t',0};
546 WCHAR name_Charset[] = {'C','h','a','r','s','e','t',0};
547 WCHAR name_Foo[] = {'F','o','o',0};
548 WCHAR name_nAmE[] = {'n','A','m','E',0};
549 WCHAR name_Nom[] = {'N','o','m',0};
550
551 LCID en_us = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
552 SORT_DEFAULT);
553 LCID fr_fr = MAKELCID(MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH),
554 SORT_DEFAULT);
555
556 /* Test DISPID_FONTs for the various properties. */
557 test_names_ids(name_Name, "Name", NULL, "", en_us,
558 DISPID_FONT_NAME, 0, S_OK,1);
559 test_names_ids(name_Size, "Size", NULL, "", en_us,
560 DISPID_FONT_SIZE, 0, S_OK,1);
561 test_names_ids(name_Bold, "Bold", NULL, "", en_us,
562 DISPID_FONT_BOLD, 0, S_OK,1);
563 test_names_ids(name_Italic, "Italic", NULL, "", en_us,
564 DISPID_FONT_ITALIC, 0, S_OK,1);
565 test_names_ids(name_Underline, "Underline", NULL, "", en_us,
566 DISPID_FONT_UNDER, 0, S_OK,1);
567 test_names_ids(name_Strikethrough, "Strikethrough", NULL, "", en_us,
568 DISPID_FONT_STRIKE, 0, S_OK,1);
569 test_names_ids(name_Weight, "Weight", NULL, "", en_us,
570 DISPID_FONT_WEIGHT, 0, S_OK,1);
571 test_names_ids(name_Charset, "Charset", NULL, "", en_us,
572 DISPID_FONT_CHARSET, 0, S_OK,1);
573
574 /* Capitalization doesn't matter. */
575 test_names_ids(name_nAmE, "nAmE", NULL, "", en_us,
576 DISPID_FONT_NAME, 0, S_OK,1);
577
578 /* Unknown name. */
579 test_names_ids(name_Foo, "Foo", NULL, "", en_us,
580 DISPID_UNKNOWN, 0, DISP_E_UNKNOWNNAME,1);
581
582 /* Pass several names: first is processed, */
583 /* second gets DISPID_UNKNOWN and doesn't affect retval. */
584 test_names_ids(name_Italic, "Italic", name_Name, "Name", en_us,
585 DISPID_FONT_ITALIC, DISPID_UNKNOWN, S_OK,2);
586 test_names_ids(name_Italic, "Italic", name_Foo, "Foo", en_us,
587 DISPID_FONT_ITALIC, DISPID_UNKNOWN, S_OK,2);
588
589 /* Locale ID has no effect. */
590 test_names_ids(name_Name, "Name", NULL, "", fr_fr,
591 DISPID_FONT_NAME, 0, S_OK,1);
592 test_names_ids(name_Nom, "This is not a font", NULL, "", fr_fr,
593 DISPID_UNKNOWN, 0, DISP_E_UNKNOWNNAME,1);
594
595 /* One of the arguments are invalid */
596 test_names_ids(name_Name, "Name", NULL, "", en_us,
597 0xdeadbeef, 0xdeadbeef, E_INVALIDARG,0);
598 test_names_ids(name_Italic, "Italic", NULL, "", en_us,
599 0xdeadbeef, 0xdeadbeef, E_INVALIDARG,0);
600 test_names_ids(name_Foo, "Foo", NULL, "", en_us,
601 0xdeadbeef, 0xdeadbeef, E_INVALIDARG,0);
602
603 /* Crazy locale ID? */
604 test_names_ids(name_Name, "Name", NULL, "", -1,
605 DISPID_FONT_NAME, 0, S_OK,1);
606 }
607
608 static void test_Invoke(void)
609 {
610 IFontDisp *fontdisp;
611 HRESULT hr;
612 VARIANTARG vararg;
613 DISPPARAMS dispparams;
614 VARIANT varresult;
615
616 hr = pOleCreateFontIndirect(NULL, &IID_IFontDisp, (void **)&fontdisp);
617 EXPECT_HR(hr, S_OK);
618
619 V_VT(&vararg) = VT_BOOL;
620 V_BOOL(&vararg) = VARIANT_FALSE;
621 dispparams.cNamedArgs = 0;
622 dispparams.rgdispidNamedArgs = NULL;
623 dispparams.cArgs = 1;
624 dispparams.rgvarg = &vararg;
625 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_IFontDisp, 0, DISPATCH_PROPERTYPUT, &dispparams, NULL, NULL, NULL);
626 EXPECT_HR(hr, DISP_E_UNKNOWNINTERFACE);
627
628 dispparams.cArgs = 0;
629 dispparams.rgvarg = NULL;
630 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYPUT, &dispparams, NULL, NULL, NULL);
631 EXPECT_HR(hr, DISP_E_BADPARAMCOUNT);
632
633 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYPUT, NULL, NULL, NULL, NULL);
634 EXPECT_HR(hr, DISP_E_PARAMNOTOPTIONAL);
635
636 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYGET, NULL, NULL, NULL, NULL);
637 EXPECT_HR(hr, DISP_E_PARAMNOTOPTIONAL);
638
639 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYGET, NULL, &varresult, NULL, NULL);
640 EXPECT_HR(hr, S_OK);
641
642 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_METHOD, NULL, &varresult, NULL, NULL);
643 EXPECT_HR(hr, DISP_E_MEMBERNOTFOUND);
644
645 hr = IFontDisp_Invoke(fontdisp, 0xdeadbeef, &IID_NULL, 0, DISPATCH_PROPERTYGET, NULL, &varresult, NULL, NULL);
646 EXPECT_HR(hr, DISP_E_MEMBERNOTFOUND);
647
648 dispparams.cArgs = 1;
649 dispparams.rgvarg = &vararg;
650 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYGET, &dispparams, &varresult, NULL, NULL);
651 EXPECT_HR(hr, S_OK);
652
653 IFontDisp_Release(fontdisp);
654 }
655
656 static void test_IsEqual(void)
657 {
658 FONTDESC fd;
659 IFont* ifnt = NULL;
660 IFont* ifnt2 = NULL;
661 HRESULT hres;
662
663 /* Basic font description */
664 fd.cbSizeofstruct = sizeof(FONTDESC);
665 fd.lpstrName = system_font;
666 S(fd.cySize).Lo = 100;
667 S(fd.cySize).Hi = 100;
668 fd.sWeight = 0;
669 fd.sCharset = 0;
670 fd.fItalic = FALSE;
671 fd.fUnderline = FALSE;
672 fd.fStrikethrough = FALSE;
673
674 /* Create font */
675 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt);
676
677 /* Test equal fonts */
678 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt2);
679 hres = IFont_IsEqual(ifnt,ifnt2);
680 ok(hres == S_OK,
681 "IFont_IsEqual: (EQUAL) Expected S_OK but got 0x%08x\n",hres);
682 IFont_Release(ifnt2);
683
684 /* Check for bad pointer */
685 hres = IFont_IsEqual(ifnt,NULL);
686 ok(hres == E_POINTER,
687 "IFont_IsEqual: (NULL) Expected 0x80004003 but got 0x%08x\n",hres);
688
689 /* Test strName */
690 fd.lpstrName = arial_font;
691 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt2);
692 hres = IFont_IsEqual(ifnt,ifnt2);
693 ok(hres == S_FALSE,
694 "IFont_IsEqual: (strName) Expected S_FALSE but got 0x%08x\n",hres);
695 fd.lpstrName = system_font;
696 IFont_Release(ifnt2);
697
698 /* Test lo font size */
699 S(fd.cySize).Lo = 10000;
700 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt2);
701 hres = IFont_IsEqual(ifnt,ifnt2);
702 ok(hres == S_FALSE,
703 "IFont_IsEqual: (Lo font size) Expected S_FALSE but got 0x%08x\n",hres);
704 S(fd.cySize).Lo = 100;
705 IFont_Release(ifnt2);
706
707 /* Test hi font size */
708 S(fd.cySize).Hi = 10000;
709 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt2);
710 hres = IFont_IsEqual(ifnt,ifnt2);
711 ok(hres == S_FALSE,
712 "IFont_IsEqual: (Hi font size) Expected S_FALSE but got 0x%08x\n",hres);
713 S(fd.cySize).Hi = 100;
714 IFont_Release(ifnt2);
715
716 /* Test font weight */
717 fd.sWeight = 100;
718 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt2);
719 hres = IFont_IsEqual(ifnt,ifnt2);
720 ok(hres == S_FALSE,
721 "IFont_IsEqual: (Weight) Expected S_FALSE but got 0x%08x\n",hres);
722 fd.sWeight = 0;
723 IFont_Release(ifnt2);
724
725 /* Test charset */
726 fd.sCharset = 1;
727 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt2);
728 hres = IFont_IsEqual(ifnt,ifnt2);
729 ok(hres == S_FALSE,
730 "IFont_IsEqual: (Charset) Expected S_FALSE but got 0x%08x\n",hres);
731 fd.sCharset = 0;
732 IFont_Release(ifnt2);
733
734 /* Test italic setting */
735 fd.fItalic = TRUE;
736 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt2);
737 hres = IFont_IsEqual(ifnt,ifnt2);
738 ok(hres == S_FALSE,
739 "IFont_IsEqual: (Italic) Expected S_FALSE but got 0x%08x\n",hres);
740 fd.fItalic = FALSE;
741 IFont_Release(ifnt2);
742
743 /* Test underline setting */
744 fd.fUnderline = TRUE;
745 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt2);
746 hres = IFont_IsEqual(ifnt,ifnt2);
747 ok(hres == S_FALSE,
748 "IFont_IsEqual: (Underline) Expected S_FALSE but got 0x%08x\n",hres);
749 fd.fUnderline = FALSE;
750 IFont_Release(ifnt2);
751
752 /* Test strikethrough setting */
753 fd.fStrikethrough = TRUE;
754 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt2);
755 hres = IFont_IsEqual(ifnt,ifnt2);
756 ok(hres == S_FALSE,
757 "IFont_IsEqual: (Strikethrough) Expected S_FALSE but got 0x%08x\n",hres);
758 fd.fStrikethrough = FALSE;
759 IFont_Release(ifnt2);
760
761 /* Free IFont. */
762 IFont_Release(ifnt);
763 }
764
765 static void test_ReleaseHfont(void)
766 {
767 FONTDESC fd;
768 LPVOID pvObj1 = NULL;
769 LPVOID pvObj2 = NULL;
770 IFont* ifnt1 = NULL;
771 IFont* ifnt2 = NULL;
772 HFONT hfnt1 = 0;
773 HFONT hfnt2 = 0;
774 HRESULT hres;
775
776 /* Basic font description */
777 fd.cbSizeofstruct = sizeof(FONTDESC);
778 fd.lpstrName = system_font;
779 S(fd.cySize).Lo = 100;
780 S(fd.cySize).Hi = 100;
781 fd.sWeight = 0;
782 fd.sCharset = 0;
783 fd.fItalic = FALSE;
784 fd.fUnderline = FALSE;
785 fd.fStrikethrough = FALSE;
786
787 /* Create HFONTs and IFONTs */
788 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj1);
789 ifnt1 = pvObj1;
790 IFont_get_hFont(ifnt1,&hfnt1);
791 fd.lpstrName = arial_font;
792 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj2);
793 ifnt2 = pvObj2;
794 IFont_get_hFont(ifnt2,&hfnt2);
795
796 /* Try invalid HFONT */
797 hres = IFont_ReleaseHfont(ifnt1,NULL);
798 ok(hres == E_INVALIDARG,
799 "IFont_ReleaseHfont: (Bad HFONT) Expected E_INVALIDARG but got 0x%08x\n",
800 hres);
801
802 /* Try to add a bad HFONT */
803 hres = IFont_ReleaseHfont(ifnt1,(HFONT)32);
804 ok(hres == S_FALSE,
805 "IFont_ReleaseHfont: (Bad HFONT) Expected S_FALSE but got 0x%08x\n",
806 hres);
807
808 /* Release all refs */
809 hres = IFont_ReleaseHfont(ifnt1,hfnt1);
810 ok(hres == S_OK,
811 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
812 hres);
813
814 hres = IFont_ReleaseHfont(ifnt2,hfnt2);
815 ok(hres == S_OK,
816 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
817 hres);
818
819 /* Check that both lists are empty */
820 hres = IFont_ReleaseHfont(ifnt1,hfnt1);
821 ok(hres == S_FALSE,
822 "IFont_AddRefHfont: (Release ref) Expected S_FALSE but got 0x%08x\n",
823 hres);
824
825 /* The list should be empty */
826 hres = IFont_ReleaseHfont(ifnt2,hfnt2);
827 ok(hres == S_FALSE,
828 "IFont_AddRefHfont: (Release ref) Expected S_FALSE but got 0x%08x\n",
829 hres);
830
831 IFont_Release(ifnt1);
832 IFont_Release(ifnt2);
833 }
834
835 static void test_AddRefHfont(void)
836 {
837 FONTDESC fd;
838 IFont* ifnt1 = NULL;
839 IFont* ifnt2 = NULL;
840 IFont* ifnt3 = NULL;
841 HFONT hfnt1 = 0;
842 HFONT hfnt2 = 0;
843 HFONT hfnt3 = 0;
844 HRESULT hres;
845
846 /* Basic font description */
847 fd.cbSizeofstruct = sizeof(FONTDESC);
848 fd.lpstrName = system_font;
849 S(fd.cySize).Lo = 100;
850 S(fd.cySize).Hi = 100;
851 fd.sWeight = 0;
852 fd.sCharset = 0;
853 fd.fItalic = FALSE;
854 fd.fUnderline = FALSE;
855 fd.fStrikethrough = FALSE;
856
857 /* Create HFONTs and IFONTs */
858 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt1);
859 IFont_get_hFont(ifnt1,&hfnt1);
860 fd.lpstrName = arial_font;
861 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt2);
862 IFont_get_hFont(ifnt2,&hfnt2);
863
864 /* Try invalid HFONT */
865 hres = IFont_AddRefHfont(ifnt1,NULL);
866 ok(hres == E_INVALIDARG,
867 "IFont_AddRefHfont: (Bad HFONT) Expected E_INVALIDARG but got 0x%08x\n",
868 hres);
869
870 /* Try to add a bad HFONT */
871 hres = IFont_AddRefHfont(ifnt1,(HFONT)32);
872 ok(hres == S_FALSE,
873 "IFont_AddRefHfont: (Bad HFONT) Expected S_FALSE but got 0x%08x\n",
874 hres);
875
876 /* Add simple IFONT HFONT pair */
877 hres = IFont_AddRefHfont(ifnt1,hfnt1);
878 ok(hres == S_OK,
879 "IFont_AddRefHfont: (Add ref) Expected S_OK but got 0x%08x\n",
880 hres);
881
882 /* IFONT and HFONT do not have to be the same (always looks at HFONT) */
883 hres = IFont_AddRefHfont(ifnt2,hfnt1);
884 ok(hres == S_OK,
885 "IFont_AddRefHfont: (Add ref) Expected S_OK but got 0x%08x\n",
886 hres);
887
888 /* Release all hfnt1 refs */
889 hres = IFont_ReleaseHfont(ifnt1,hfnt1);
890 ok(hres == S_OK,
891 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
892 hres);
893
894 hres = IFont_ReleaseHfont(ifnt1,hfnt1);
895 ok(hres == S_OK,
896 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
897 hres);
898
899 hres = IFont_ReleaseHfont(ifnt1,hfnt1);
900 ok(hres == S_OK,
901 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
902 hres);
903
904 /* Check if hfnt1 is empty */
905 hres = IFont_ReleaseHfont(ifnt1,hfnt1);
906 ok(hres == S_FALSE,
907 "IFont_AddRefHfont: (Release ref) Expected S_FALSE but got 0x%08x\n",
908 hres);
909
910 /* Release all hfnt2 refs */
911 hres = IFont_ReleaseHfont(ifnt2,hfnt2);
912 ok(hres == S_OK,
913 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
914 hres);
915
916 /* Check if hfnt2 is empty */
917 hres = IFont_ReleaseHfont(ifnt2,hfnt2);
918 ok(hres == S_FALSE,
919 "IFont_AddRefHfont: (Release ref) Expected S_FALSE but got 0x%08x\n",
920 hres);
921
922 /* Show that releasing an IFONT does not always release it from the HFONT cache. */
923
924 IFont_Release(ifnt1);
925
926 /* Add a reference for destroyed hfnt1 */
927 hres = IFont_AddRefHfont(ifnt2,hfnt1);
928 ok(hres == S_OK,
929 "IFont_AddRefHfont: (Add ref) Expected S_OK but got 0x%08x\n",
930 hres);
931
932 /* Decrement reference for destroyed hfnt1 */
933 hres = IFont_ReleaseHfont(ifnt2,hfnt1);
934 ok(hres == S_OK ||
935 hres == S_FALSE, /* <= win2k */
936 "IFont_AddRefHfont: (Release ref) Expected S_OK or S_FALSE but got 0x%08x\n",
937 hres);
938
939 /* Shows that releasing all IFONT's does clear the HFONT cache. */
940
941 IFont_Release(ifnt2);
942
943 /* Need to make a new IFONT for testing */
944 fd.fUnderline = TRUE;
945 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt3);
946 IFont_get_hFont(ifnt3,&hfnt3);
947
948 /* Add a reference for destroyed hfnt1 */
949 hres = IFont_AddRefHfont(ifnt3,hfnt1);
950 ok(hres == S_FALSE,
951 "IFont_AddRefHfont: (Add ref) Expected S_OK but got 0x%08x\n",
952 hres);
953
954 /* Decrement reference for destroyed hfnt1 */
955 hres = IFont_ReleaseHfont(ifnt3,hfnt1);
956 ok(hres == S_FALSE,
957 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
958 hres);
959
960 IFont_Release(ifnt3);
961 }
962
963 static void test_returns(void)
964 {
965 IFont *pFont;
966 FONTDESC fontdesc;
967 HRESULT hr;
968
969 fontdesc.cbSizeofstruct = sizeof(fontdesc);
970 fontdesc.lpstrName = MSSansSerif_font;
971 fontdesc.cySize.int64 = 12 * 10000; /* 12 pt */
972 fontdesc.sWeight = FW_NORMAL;
973 fontdesc.sCharset = 0;
974 fontdesc.fItalic = FALSE;
975 fontdesc.fUnderline = FALSE;
976 fontdesc.fStrikethrough = FALSE;
977
978 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void **)&pFont);
979 EXPECT_HR(hr, S_OK);
980
981 hr = IFont_put_Name(pFont, NULL);
982 EXPECT_HR(hr, CTL_E_INVALIDPROPERTYVALUE);
983
984 hr = IFont_get_Name(pFont, NULL);
985 EXPECT_HR(hr, E_POINTER);
986
987 hr = IFont_get_Size(pFont, NULL);
988 EXPECT_HR(hr, E_POINTER);
989
990 hr = IFont_get_Bold(pFont, NULL);
991 EXPECT_HR(hr, E_POINTER);
992
993 IFont_Release(pFont);
994 }
995
996 static void test_hfont_lifetime(void)
997 {
998 IFont *font, *font2;
999 FONTDESC fontdesc;
1000 HRESULT hr;
1001 HFONT hfont, first_hfont = NULL;
1002 CY size;
1003 DWORD obj_type;
1004 int i;
1005
1006 fontdesc.cbSizeofstruct = sizeof(fontdesc);
1007 fontdesc.lpstrName = arial_font;
1008 fontdesc.cySize.int64 = 12 * 10000; /* 12 pt */
1009 fontdesc.sWeight = FW_NORMAL;
1010 fontdesc.sCharset = ANSI_CHARSET;
1011 fontdesc.fItalic = FALSE;
1012 fontdesc.fUnderline = FALSE;
1013 fontdesc.fStrikethrough = FALSE;
1014
1015 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void **)&font);
1016 EXPECT_HR(hr, S_OK);
1017
1018 hr = IFont_get_hFont(font, &hfont);
1019 EXPECT_HR(hr, S_OK);
1020
1021 /* show that if the font is updated the old hfont is deleted when the
1022 new font is realized */
1023 for(i = 0; i < 100; i++)
1024 {
1025 HFONT last_hfont = hfont;
1026
1027 size.int64 = (i + 10) * 20000;
1028
1029 obj_type = GetObjectType(hfont);
1030 ok(obj_type == OBJ_FONT, "got obj type %d\n", obj_type);
1031
1032 hr = IFont_put_Size(font, size);
1033 EXPECT_HR(hr, S_OK);
1034
1035 /* put_Size doesn't cause the new font to be realized */
1036 obj_type = GetObjectType(last_hfont);
1037 ok(obj_type == OBJ_FONT, "got obj type %d\n", obj_type);
1038
1039 hr = IFont_get_hFont(font, &hfont);
1040 EXPECT_HR(hr, S_OK);
1041
1042 obj_type = GetObjectType(last_hfont);
1043 ok(obj_type == 0, "%d: got obj type %d\n", i, obj_type);
1044 }
1045
1046 /* now show that if we take a reference on the hfont, it persists
1047 until the font object is released */
1048 for(i = 0; i < 100; i++)
1049 {
1050 size.int64 = (i + 10) * 20000;
1051
1052 obj_type = GetObjectType(hfont);
1053 ok(obj_type == OBJ_FONT, "got obj type %d\n", obj_type);
1054
1055 hr = IFont_put_Size(font, size);
1056 EXPECT_HR(hr, S_OK);
1057
1058 hr = IFont_get_hFont(font, &hfont);
1059 EXPECT_HR(hr, S_OK);
1060
1061 hr = IFont_AddRefHfont(font, hfont);
1062 EXPECT_HR(hr, S_OK);
1063
1064 if(i == 0) first_hfont = hfont;
1065 obj_type = GetObjectType(first_hfont);
1066 ok(obj_type == OBJ_FONT, "got obj type %d\n", obj_type);
1067 }
1068
1069 IFont_Release(font);
1070
1071 obj_type = GetObjectType(first_hfont);
1072 ok(obj_type == 0, "got obj type %d\n", obj_type);
1073
1074 /* An AddRefHfont followed by a ReleaseHfont means the font doesn't not persist
1075 through re-realization */
1076
1077 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void **)&font);
1078 EXPECT_HR(hr, S_OK);
1079
1080 hr = IFont_get_hFont(font, &hfont);
1081 EXPECT_HR(hr, S_OK);
1082
1083 for(i = 0; i < 100; i++)
1084 {
1085 HFONT last_hfont = hfont;
1086
1087 size.int64 = (i + 10) * 20000;
1088
1089 obj_type = GetObjectType(hfont);
1090 ok(obj_type == OBJ_FONT, "got obj type %d\n", obj_type);
1091
1092 hr = IFont_put_Size(font, size);
1093 EXPECT_HR(hr, S_OK);
1094
1095 /* put_Size doesn't cause the new font to be realized */
1096 obj_type = GetObjectType(last_hfont);
1097 ok(obj_type == OBJ_FONT, "got obj type %d\n", obj_type);
1098
1099 hr = IFont_get_hFont(font, &hfont);
1100 EXPECT_HR(hr, S_OK);
1101
1102 hr = IFont_AddRefHfont(font, hfont);
1103 EXPECT_HR(hr, S_OK);
1104
1105 hr = IFont_ReleaseHfont(font, hfont);
1106 EXPECT_HR(hr, S_OK);
1107
1108 obj_type = GetObjectType(last_hfont);
1109 ok(obj_type == 0, "%d: got obj type %d\n", i, obj_type);
1110 }
1111
1112 /* Interestingly if we release a nonexistent reference on the hfont,
1113 * it persists until the font object is released
1114 */
1115 for(i = 0; i < 100; i++)
1116 {
1117 size.int64 = (i + 10) * 20000;
1118
1119 obj_type = GetObjectType(hfont);
1120 ok(obj_type == OBJ_FONT, "got obj type %d\n", obj_type);
1121
1122 hr = IFont_put_Size(font, size);
1123 EXPECT_HR(hr, S_OK);
1124
1125 hr = IFont_get_hFont(font, &hfont);
1126 EXPECT_HR(hr, S_OK);
1127
1128 hr = IFont_ReleaseHfont(font, hfont);
1129 EXPECT_HR(hr, S_OK);
1130
1131 if(i == 0) first_hfont = hfont;
1132 obj_type = GetObjectType(first_hfont);
1133 ok(obj_type == OBJ_FONT, "got obj type %d\n", obj_type);
1134 }
1135
1136 IFont_Release(font);
1137
1138 obj_type = GetObjectType(first_hfont);
1139 ok(obj_type == 0, "got obj type %d\n", obj_type);
1140
1141 /* If we take two internal references on a hfont then we can release
1142 it twice. So it looks like there's a total reference count
1143 that includes internal and external references */
1144
1145 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void **)&font);
1146 EXPECT_HR(hr, S_OK);
1147 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void **)&font2);
1148 EXPECT_HR(hr, S_OK);
1149
1150 hr = IFont_get_hFont(font, &hfont);
1151 EXPECT_HR(hr, S_OK);
1152 hr = IFont_get_hFont(font2, &first_hfont);
1153 EXPECT_HR(hr, S_OK);
1154 todo_wine
1155 ok(hfont == first_hfont, "fonts differ\n");
1156 hr = IFont_ReleaseHfont(font, hfont);
1157 EXPECT_HR(hr, S_OK);
1158 hr = IFont_ReleaseHfont(font, hfont);
1159 todo_wine
1160 EXPECT_HR(hr, S_OK);
1161 hr = IFont_ReleaseHfont(font, hfont);
1162 EXPECT_HR(hr, S_FALSE);
1163
1164 obj_type = GetObjectType(hfont);
1165 ok(obj_type == OBJ_FONT, "got obj type %d\n", obj_type);
1166
1167 IFont_Release(font);
1168
1169 obj_type = GetObjectType(hfont);
1170 ok(obj_type == OBJ_FONT, "got obj type %d\n", obj_type);
1171
1172 IFont_Release(font2);
1173
1174 obj_type = GetObjectType(hfont);
1175 ok(obj_type == 0, "got obj type %d\n", obj_type);
1176 }
1177
1178 static void test_realization(void)
1179 {
1180 IFont *font;
1181 FONTDESC fontdesc;
1182 HRESULT hr;
1183 BSTR name;
1184 SHORT cs;
1185
1186 /* Try to create a symbol only font (marlett) with charset
1187 set to ANSI. This will result in another, ANSI, font
1188 being selected */
1189 fontdesc.cbSizeofstruct = sizeof(fontdesc);
1190 fontdesc.lpstrName = marlett_font;
1191 fontdesc.cySize.int64 = 12 * 10000; /* 12 pt */
1192 fontdesc.sWeight = FW_NORMAL;
1193 fontdesc.sCharset = ANSI_CHARSET;
1194 fontdesc.fItalic = FALSE;
1195 fontdesc.fUnderline = FALSE;
1196 fontdesc.fStrikethrough = FALSE;
1197
1198 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void **)&font);
1199 EXPECT_HR(hr, S_OK);
1200
1201 hr = IFont_get_Charset(font, &cs);
1202 EXPECT_HR(hr, S_OK);
1203 ok(cs == ANSI_CHARSET, "got charset %d\n", cs);
1204
1205 IFont_Release(font);
1206
1207 /* Now create an ANSI font and change the name to marlett */
1208
1209 fontdesc.lpstrName = arial_font;
1210
1211 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void **)&font);
1212 EXPECT_HR(hr, S_OK);
1213
1214 hr = IFont_get_Charset(font, &cs);
1215 EXPECT_HR(hr, S_OK);
1216 ok(cs == ANSI_CHARSET, "got charset %d\n", cs);
1217
1218 name = SysAllocString(marlett_font);
1219 hr = IFont_put_Name(font, name);
1220 EXPECT_HR(hr, S_OK);
1221 SysFreeString(name);
1222
1223 hr = IFont_get_Name(font, &name);
1224 EXPECT_HR(hr, S_OK);
1225 ok(!lstrcmpiW(name, marlett_font), "got name %s\n", wine_dbgstr_w(name));
1226 SysFreeString(name);
1227
1228 hr = IFont_get_Charset(font, &cs);
1229 EXPECT_HR(hr, S_OK);
1230 ok(cs == SYMBOL_CHARSET, "got charset %d\n", cs);
1231
1232 IFont_Release(font);
1233 }
1234
1235 static void test_OleCreateFontIndirect(void)
1236 {
1237 FONTDESC fontdesc;
1238 IUnknown *unk, *unk2;
1239 IFont *font;
1240 HRESULT hr;
1241
1242 fontdesc.cbSizeofstruct = sizeof(fontdesc);
1243 fontdesc.lpstrName = arial_font;
1244 fontdesc.cySize.int64 = 12 * 10000; /* 12 pt */
1245 fontdesc.sWeight = FW_NORMAL;
1246 fontdesc.sCharset = ANSI_CHARSET;
1247 fontdesc.fItalic = FALSE;
1248 fontdesc.fUnderline = FALSE;
1249 fontdesc.fStrikethrough = FALSE;
1250
1251 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void**)&font);
1252 EXPECT_HR(hr, S_OK);
1253 IFont_Release(font);
1254
1255 /* play with cbSizeofstruct value */
1256 fontdesc.cbSizeofstruct = sizeof(fontdesc)-1;
1257 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void**)&font);
1258 EXPECT_HR(hr, S_OK);
1259 IFont_Release(font);
1260
1261 fontdesc.cbSizeofstruct = sizeof(fontdesc)+1;
1262 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void**)&font);
1263 EXPECT_HR(hr, S_OK);
1264 IFont_Release(font);
1265
1266 fontdesc.cbSizeofstruct = 0;
1267 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void**)&font);
1268 EXPECT_HR(hr, S_OK);
1269 IFont_Release(font);
1270
1271 hr = OleInitialize(NULL);
1272 ok(hr == S_OK, "got 0x%08x\n", hr);
1273
1274 hr = CoGetClassObject(&CLSID_StdFont, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void**)&unk);
1275 ok(hr == S_OK, "got 0x%08x\n", hr);
1276
1277 hr = IUnknown_QueryInterface(unk, &IID_IUnknown, (void**)&unk2);
1278 ok(hr == S_OK, "got 0x%08x\n", hr);
1279
1280 IUnknown_Release(unk);
1281 IUnknown_Release(unk2);
1282
1283 OleUninitialize();
1284 }
1285
1286 START_TEST(olefont)
1287 {
1288 hOleaut32 = GetModuleHandleA("oleaut32.dll");
1289 pOleCreateFontIndirect = (void*)GetProcAddress(hOleaut32, "OleCreateFontIndirect");
1290 if (!pOleCreateFontIndirect)
1291 {
1292 win_skip("OleCreateFontIndirect not available\n");
1293 return;
1294 }
1295
1296 test_QueryInterface();
1297 test_type_info();
1298 test_ifont_sizes();
1299 test_font_events_disp();
1300 test_GetIDsOfNames();
1301 test_Invoke();
1302 test_IsEqual();
1303 test_ReleaseHfont();
1304 test_AddRefHfont();
1305 test_returns();
1306 test_hfont_lifetime();
1307 test_realization();
1308 test_OleCreateFontIndirect();
1309 }