push 88671f85dcf7a7cd89c63d6e9f34ad6b6ad2ae64
[wine/hacks.git] / dlls / oleaut32 / tests / olefont.c
blob11d6169a24285dd29d9ba3c4857f4de22362b11d
1 /*
2 * OLEFONT test program
4 * Copyright 2003 Marcus Meissner
5 * Copyright 2006 (Google) Benjamin Arai
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.
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.
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
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <math.h>
26 #include <float.h>
27 #include <time.h>
29 #define COBJMACROS
31 #include <wine/test.h>
32 #include <windef.h>
33 #include <winbase.h>
34 #include <winuser.h>
35 #include <wingdi.h>
36 #include <winnls.h>
37 #include <winerror.h>
38 #include <winnt.h>
39 #include <wtypes.h>
40 #include <olectl.h>
41 #include <ocidl.h>
43 static WCHAR MSSansSerif_font[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0};
44 static WCHAR system_font[] = { 'S','y','s','t','e','m',0 };
45 static WCHAR arial_font[] = { 'A','r','i','a','l',0 };
47 static HMODULE hOleaut32;
49 static HRESULT (WINAPI *pOleCreateFontIndirect)(LPFONTDESC,REFIID,LPVOID*);
51 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
53 /* Create a font with cySize given by lo_size, hi_size, */
54 /* SetRatio to ratio_logical, ratio_himetric, */
55 /* check that resulting hfont has height hfont_height. */
56 /* Various checks along the way. */
58 static void test_ifont_sizes(long lo_size, long hi_size,
59 long ratio_logical, long ratio_himetric,
60 long hfont_height, const char * test_name)
62 FONTDESC fd;
63 LPVOID pvObj = NULL;
64 IFont* ifnt = NULL;
65 HFONT hfont;
66 LOGFONT lf;
67 CY psize;
68 HRESULT hres;
70 fd.cbSizeofstruct = sizeof(FONTDESC);
71 fd.lpstrName = system_font;
72 S(fd.cySize).Lo = lo_size;
73 S(fd.cySize).Hi = hi_size;
74 fd.sWeight = 0;
75 fd.sCharset = 0;
76 fd.fItalic = 0;
77 fd.fUnderline = 0;
78 fd.fStrikethrough = 0;
80 /* Create font, test that it worked. */
81 hres = pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj);
82 ifnt = pvObj;
83 ok(hres == S_OK,"%s: OCFI returns 0x%08x instead of S_OK.\n",
84 test_name, hres);
85 ok(pvObj != NULL,"%s: OCFI returns NULL.\n", test_name);
87 /* Read back size. Hi part was ignored. */
88 hres = IFont_get_Size(ifnt, &psize);
89 ok(hres == S_OK,"%s: IFont_get_size returns 0x%08x instead of S_OK.\n",
90 test_name, hres);
91 ok(S(psize).Lo == lo_size && S(psize).Hi == 0,
92 "%s: get_Size: Lo=%d, Hi=%d; expected Lo=%ld, Hi=%ld.\n",
93 test_name, S(psize).Lo, S(psize).Hi, lo_size, 0L);
95 /* Change ratio, check size unchanged. Standard is 72, 2540. */
96 hres = IFont_SetRatio(ifnt, ratio_logical, ratio_himetric);
97 ok(hres == S_OK,"%s: IFont_SR returns 0x%08x instead of S_OK.\n",
98 test_name, hres);
99 hres = IFont_get_Size(ifnt, &psize);
100 ok(hres == S_OK,"%s: IFont_get_size returns 0x%08x instead of S_OK.\n",
101 test_name, hres);
102 ok(S(psize).Lo == lo_size && S(psize).Hi == 0,
103 "%s: gS after SR: Lo=%d, Hi=%d; expected Lo=%ld, Hi=%ld.\n",
104 test_name, S(psize).Lo, S(psize).Hi, lo_size, 0L);
106 /* Check hFont size with this ratio. This tests an important */
107 /* conversion for which MSDN is very wrong. */
108 hres = IFont_get_hFont (ifnt, &hfont);
109 ok(hres == S_OK, "%s: IFont_get_hFont returns 0x%08x instead of S_OK.\n",
110 test_name, hres);
111 hres = GetObject (hfont, sizeof(LOGFONT), &lf);
112 ok(lf.lfHeight == hfont_height,
113 "%s: hFont has lf.lfHeight=%d, expected %ld.\n",
114 test_name, lf.lfHeight, hfont_height);
116 /* Free IFont. */
117 IFont_Release(ifnt);
120 static void test_QueryInterface(void)
122 LPVOID pvObj = NULL;
123 HRESULT hres;
124 IFont* font = NULL;
125 LONG ret;
127 hres = pOleCreateFontIndirect(NULL, &IID_IFont, &pvObj);
128 font = pvObj;
130 ok(hres == S_OK,"OCFI (NULL,..) does not return 0, but 0x%08x\n",hres);
131 ok(font != NULL,"OCFI (NULL,..) returns NULL, instead of !NULL\n");
133 pvObj = NULL;
134 hres = IFont_QueryInterface( font, &IID_IFont, &pvObj);
136 /* Test if QueryInterface increments ref counter for IFONTs */
137 ret = IFont_AddRef(font);
138 ok(ret == 3, "IFont_QI expected ref value 3 but instead got %12u\n",ret);
139 IFont_Release(font);
141 ok(hres == S_OK,"IFont_QI does not return S_OK, but 0x%08x\n", hres);
142 ok(pvObj != NULL,"IFont_QI does return NULL, instead of a ptr\n");
144 /* Orignial ref and QueryInterface ref both have to be released */
145 IFont_Release(font);
146 IFont_Release(font);
149 static void test_type_info(void)
151 LPVOID pvObj = NULL;
152 HRESULT hres;
153 IFontDisp* fontdisp = NULL;
154 ITypeInfo* pTInfo;
155 WCHAR name_Name[] = {'N','a','m','e',0};
156 BSTR names[3];
157 UINT n;
158 LCID en_us = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
159 SORT_DEFAULT);
160 DISPPARAMS dispparams;
161 VARIANT varresult;
163 pOleCreateFontIndirect(NULL, &IID_IFontDisp, &pvObj);
164 fontdisp = pvObj;
166 hres = IFontDisp_GetTypeInfo(fontdisp, 0, en_us, &pTInfo);
167 ok(hres == S_OK, "GTI returned 0x%08x instead of S_OK.\n", hres);
168 ok(pTInfo != NULL, "GTI returned NULL.\n");
170 hres = ITypeInfo_GetNames(pTInfo, DISPID_FONT_NAME, names, 3, &n);
171 ok(hres == S_OK, "GetNames returned 0x%08x instead of S_OK.\n", hres);
172 ok(n == 1, "GetNames returned %d names instead of 1.\n", n);
173 ok(!lstrcmpiW(names[0],name_Name), "DISPID_FONT_NAME doesn't get 'Names'.\n");
174 SysFreeString(names[0]);
176 ITypeInfo_Release(pTInfo);
178 dispparams.cNamedArgs = 0;
179 dispparams.rgdispidNamedArgs = NULL;
180 dispparams.cArgs = 0;
181 dispparams.rgvarg = NULL;
182 VariantInit(&varresult);
183 hres = IFontDisp_Invoke(fontdisp, DISPID_FONT_NAME, &IID_NULL,
184 LOCALE_NEUTRAL, DISPATCH_PROPERTYGET, &dispparams, &varresult,
185 NULL, NULL);
186 ok(hres == S_OK, "IFontDisp_Invoke return 0x%08x instead of S_OK.\n", hres);
187 VariantClear(&varresult);
189 IFontDisp_Release(fontdisp);
192 static HRESULT WINAPI FontEventsDisp_QueryInterface(
193 IFontEventsDisp *iface,
194 /* [in] */ REFIID riid,
195 /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject)
197 if (IsEqualIID(riid, &IID_IFontEventsDisp) || IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDispatch))
199 IUnknown_AddRef(iface);
200 *ppvObject = iface;
201 return S_OK;
203 else
205 *ppvObject = NULL;
206 return E_NOINTERFACE;
210 static ULONG WINAPI FontEventsDisp_AddRef(
211 IFontEventsDisp *iface)
213 return 2;
216 static ULONG WINAPI FontEventsDisp_Release(
217 IFontEventsDisp *iface)
219 return 1;
222 static int fonteventsdisp_invoke_called = 0;
224 static HRESULT WINAPI FontEventsDisp_Invoke(
225 IFontEventsDisp __RPC_FAR * iface,
226 /* [in] */ DISPID dispIdMember,
227 /* [in] */ REFIID riid,
228 /* [in] */ LCID lcid,
229 /* [in] */ WORD wFlags,
230 /* [out][in] */ DISPPARAMS __RPC_FAR *pDispParams,
231 /* [out] */ VARIANT __RPC_FAR *pVarResult,
232 /* [out] */ EXCEPINFO __RPC_FAR *pExcepInfo,
233 /* [out] */ UINT __RPC_FAR *puArgErr)
235 static const WCHAR wszBold[] = {'B','o','l','d',0};
236 ok(wFlags == INVOKE_FUNC, "invoke flags should have been INVOKE_FUNC instead of 0x%x\n", wFlags);
237 ok(dispIdMember == DISPID_FONT_CHANGED, "dispIdMember should have been DISPID_FONT_CHANGED instead of 0x%x\n", dispIdMember);
238 ok(pDispParams->cArgs == 1, "pDispParams->cArgs should have been 1 instead of %d\n", pDispParams->cArgs);
239 ok(V_VT(&pDispParams->rgvarg[0]) == VT_BSTR, "VT of first param should have been VT_BSTR instead of %d\n", V_VT(&pDispParams->rgvarg[0]));
240 ok(!lstrcmpW(V_BSTR(&pDispParams->rgvarg[0]), wszBold), "String in first param should have been \"Bold\"\n");
242 fonteventsdisp_invoke_called++;
243 return S_OK;
246 static IFontEventsDispVtbl FontEventsDisp_Vtbl =
248 FontEventsDisp_QueryInterface,
249 FontEventsDisp_AddRef,
250 FontEventsDisp_Release,
251 NULL,
252 NULL,
253 NULL,
254 FontEventsDisp_Invoke
257 static IFontEventsDisp FontEventsDisp = { &FontEventsDisp_Vtbl };
259 static void test_font_events_disp(void)
261 IFont *pFont;
262 IFont *pFont2;
263 IConnectionPointContainer *pCPC;
264 IConnectionPoint *pCP;
265 FONTDESC fontdesc;
266 HRESULT hr;
267 DWORD dwCookie;
268 IFontDisp *pFontDisp;
269 DISPPARAMS dispparams;
270 VARIANTARG vararg;
272 fontdesc.cbSizeofstruct = sizeof(fontdesc);
273 fontdesc.lpstrName = MSSansSerif_font;
274 fontdesc.cySize.int64 = 12 * 10000; /* 12 pt */
275 fontdesc.sWeight = FW_NORMAL;
276 fontdesc.sCharset = 0;
277 fontdesc.fItalic = FALSE;
278 fontdesc.fUnderline = FALSE;
279 fontdesc.fStrikethrough = FALSE;
281 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void **)&pFont);
282 ok_ole_success(hr, "OleCreateFontIndirect");
284 hr = IFont_QueryInterface(pFont, &IID_IConnectionPointContainer, (void **)&pCPC);
285 ok_ole_success(hr, "IFont_QueryInterface");
287 hr = IConnectionPointContainer_FindConnectionPoint(pCPC, &IID_IFontEventsDisp, &pCP);
288 ok_ole_success(hr, "IConnectionPointContainer_FindConnectionPoint");
289 IConnectionPointContainer_Release(pCPC);
291 hr = IConnectionPoint_Advise(pCP, (IUnknown *)&FontEventsDisp, &dwCookie);
292 ok_ole_success(hr, "IConnectionPoint_Advise");
293 IConnectionPoint_Release(pCP);
295 hr = IFont_put_Bold(pFont, TRUE);
296 ok_ole_success(hr, "IFont_put_Bold");
298 ok(fonteventsdisp_invoke_called == 1, "IFontEventDisp::Invoke wasn't called once\n");
300 hr = IFont_QueryInterface(pFont, &IID_IFontDisp, (void **)&pFontDisp);
301 ok_ole_success(hr, "IFont_QueryInterface");
303 V_VT(&vararg) = VT_BOOL;
304 V_BOOL(&vararg) = VARIANT_FALSE;
305 dispparams.cNamedArgs = 0;
306 dispparams.rgdispidNamedArgs = NULL;
307 dispparams.cArgs = 1;
308 dispparams.rgvarg = &vararg;
309 hr = IFontDisp_Invoke(pFontDisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYPUT, &dispparams, NULL, NULL, NULL);
311 IFontDisp_Release(pFontDisp);
313 ok(fonteventsdisp_invoke_called == 2, "IFontEventDisp::Invoke was called %d times instead of twice\n",
314 fonteventsdisp_invoke_called);
316 hr = IFont_Clone(pFont, &pFont2);
317 ok_ole_success(hr, "IFont_Clone");
318 IFont_Release(pFont);
320 hr = IFont_put_Bold(pFont2, FALSE);
321 ok_ole_success(hr, "IFont_put_Bold");
323 /* this test shows that the notification routine isn't called again */
324 ok(fonteventsdisp_invoke_called == 2, "IFontEventDisp::Invoke was called %d times instead of twice\n",
325 fonteventsdisp_invoke_called);
327 IFont_Release(pFont2);
330 static void test_names_ids(WCHAR* w_name_1, const char* a_name_1,
331 WCHAR* w_name_2, const char* a_name_2,
332 LCID lcid, DISPID id_1, DISPID id_2,
333 HRESULT hres_expect, int numnames)
335 LPVOID pvObj = NULL;
336 IFontDisp *fontdisp = NULL;
337 HRESULT hres;
338 DISPID rgDispId[2] = {0xdeadbeef, 0xdeadbeef};
339 LPOLESTR names[2] = {w_name_1, w_name_2};
341 pOleCreateFontIndirect(NULL, &IID_IFontDisp, &pvObj);
342 fontdisp = pvObj;
344 hres = IFontDisp_GetIDsOfNames(fontdisp, &IID_NULL, names, numnames,
345 lcid, rgDispId);
347 /* test hres */
348 ok(hres == hres_expect,
349 "GetIDsOfNames: \"%s\", \"%s\" returns 0x%08x, expected 0x%08x.\n",
350 a_name_1, a_name_2, hres, hres_expect);
352 /* test first DISPID */
353 ok(rgDispId[0]==id_1,
354 "GetIDsOfNames: \"%s\" gets DISPID 0x%08x, expected 0x%08x.\n",
355 a_name_1, rgDispId[0], id_1);
357 /* test second DISPID is present */
358 if (numnames == 2)
360 ok(rgDispId[1]==id_2,
361 "GetIDsOfNames: ..., \"%s\" gets DISPID 0x%08x, expected 0x%08x.\n",
362 a_name_2, rgDispId[1], id_2);
365 IFontDisp_Release(fontdisp);
368 static void test_GetIDsOfNames(void)
370 WCHAR name_Name[] = {'N','a','m','e',0};
371 WCHAR name_Italic[] = {'I','t','a','l','i','c',0};
372 WCHAR name_Size[] = {'S','i','z','e',0};
373 WCHAR name_Bold[] = {'B','o','l','d',0};
374 WCHAR name_Underline[] = {'U','n','d','e','r','l','i','n','e',0};
375 WCHAR name_Strikethrough[] = {'S','t','r','i','k','e','t','h','r','o','u','g','h',0};
376 WCHAR name_Weight[] = {'W','e','i','g','h','t',0};
377 WCHAR name_Charset[] = {'C','h','a','r','s','e','t',0};
378 WCHAR name_Foo[] = {'F','o','o',0};
379 WCHAR name_nAmE[] = {'n','A','m','E',0};
380 WCHAR name_Nom[] = {'N','o','m',0};
382 LCID en_us = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
383 SORT_DEFAULT);
384 LCID fr_fr = MAKELCID(MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH),
385 SORT_DEFAULT);
387 /* Test DISPID_FONTs for the various properties. */
388 test_names_ids(name_Name, "Name", NULL, "", en_us,
389 DISPID_FONT_NAME, 0, S_OK,1);
390 test_names_ids(name_Size, "Size", NULL, "", en_us,
391 DISPID_FONT_SIZE, 0, S_OK,1);
392 test_names_ids(name_Bold, "Bold", NULL, "", en_us,
393 DISPID_FONT_BOLD, 0, S_OK,1);
394 test_names_ids(name_Italic, "Italic", NULL, "", en_us,
395 DISPID_FONT_ITALIC, 0, S_OK,1);
396 test_names_ids(name_Underline, "Underline", NULL, "", en_us,
397 DISPID_FONT_UNDER, 0, S_OK,1);
398 test_names_ids(name_Strikethrough, "Strikethrough", NULL, "", en_us,
399 DISPID_FONT_STRIKE, 0, S_OK,1);
400 test_names_ids(name_Weight, "Weight", NULL, "", en_us,
401 DISPID_FONT_WEIGHT, 0, S_OK,1);
402 test_names_ids(name_Charset, "Charset", NULL, "", en_us,
403 DISPID_FONT_CHARSET, 0, S_OK,1);
405 /* Capitalization doesn't matter. */
406 test_names_ids(name_nAmE, "nAmE", NULL, "", en_us,
407 DISPID_FONT_NAME, 0, S_OK,1);
409 /* Unknown name. */
410 test_names_ids(name_Foo, "Foo", NULL, "", en_us,
411 DISPID_UNKNOWN, 0, DISP_E_UNKNOWNNAME,1);
413 /* Pass several names: first is processed, */
414 /* second gets DISPID_UNKNOWN and doesn't affect retval. */
415 test_names_ids(name_Italic, "Italic", name_Name, "Name", en_us,
416 DISPID_FONT_ITALIC, DISPID_UNKNOWN, S_OK,2);
417 test_names_ids(name_Italic, "Italic", name_Foo, "Foo", en_us,
418 DISPID_FONT_ITALIC, DISPID_UNKNOWN, S_OK,2);
420 /* Locale ID has no effect. */
421 test_names_ids(name_Name, "Name", NULL, "", fr_fr,
422 DISPID_FONT_NAME, 0, S_OK,1);
423 test_names_ids(name_Nom, "This is not a font", NULL, "", fr_fr,
424 DISPID_UNKNOWN, 0, DISP_E_UNKNOWNNAME,1);
426 /* One of the arguments are invalid */
427 test_names_ids(name_Name, "Name", NULL, "", en_us,
428 0xdeadbeef, 0xdeadbeef, E_INVALIDARG,0);
429 test_names_ids(name_Italic, "Italic", NULL, "", en_us,
430 0xdeadbeef, 0xdeadbeef, E_INVALIDARG,0);
431 test_names_ids(name_Foo, "Foo", NULL, "", en_us,
432 0xdeadbeef, 0xdeadbeef, E_INVALIDARG,0);
434 /* Crazy locale ID? */
435 test_names_ids(name_Name, "Name", NULL, "", -1,
436 DISPID_FONT_NAME, 0, S_OK,1);
439 static void test_Invoke(void)
441 IFontDisp *fontdisp;
442 HRESULT hr;
443 VARIANTARG vararg;
444 DISPPARAMS dispparams;
445 VARIANT varresult;
447 hr = pOleCreateFontIndirect(NULL, &IID_IFontDisp, (void **)&fontdisp);
448 ok_ole_success(hr, "OleCreateFontIndirect");
450 V_VT(&vararg) = VT_BOOL;
451 V_BOOL(&vararg) = VARIANT_FALSE;
452 dispparams.cNamedArgs = 0;
453 dispparams.rgdispidNamedArgs = NULL;
454 dispparams.cArgs = 1;
455 dispparams.rgvarg = &vararg;
456 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_IFontDisp, 0, DISPATCH_PROPERTYPUT, &dispparams, NULL, NULL, NULL);
457 ok(hr == DISP_E_UNKNOWNINTERFACE, "IFontDisp_Invoke should have returned DISP_E_UNKNOWNINTERFACE instead of 0x%08x\n", hr);
459 dispparams.cArgs = 0;
460 dispparams.rgvarg = NULL;
461 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYPUT, &dispparams, NULL, NULL, NULL);
462 ok(hr == DISP_E_BADPARAMCOUNT, "IFontDisp_Invoke should have returned DISP_E_BADPARAMCOUNT instead of 0x%08x\n", hr);
464 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYPUT, NULL, NULL, NULL, NULL);
465 ok(hr == DISP_E_PARAMNOTOPTIONAL, "IFontDisp_Invoke should have returned DISP_E_PARAMNOTOPTIONAL instead of 0x%08x\n", hr);
467 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYGET, NULL, NULL, NULL, NULL);
468 ok(hr == DISP_E_PARAMNOTOPTIONAL, "IFontDisp_Invoke should have returned DISP_E_PARAMNOTOPTIONAL instead of 0x%08x\n", hr);
470 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYGET, NULL, &varresult, NULL, NULL);
471 ok_ole_success(hr, "IFontDisp_Invoke");
473 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_METHOD, NULL, &varresult, NULL, NULL);
474 ok(hr == DISP_E_MEMBERNOTFOUND, "IFontDisp_Invoke should have returned DISP_E_MEMBERNOTFOUND instead of 0x%08x\n", hr);
476 hr = IFontDisp_Invoke(fontdisp, 0xdeadbeef, &IID_NULL, 0, DISPATCH_PROPERTYGET, NULL, &varresult, NULL, NULL);
477 ok(hr == DISP_E_MEMBERNOTFOUND, "IFontDisp_Invoke should have returned DISP_E_MEMBERNOTFOUND instead of 0x%08x\n", hr);
479 dispparams.cArgs = 1;
480 dispparams.rgvarg = &vararg;
481 hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYGET, &dispparams, &varresult, NULL, NULL);
482 ok_ole_success(hr, "IFontDisp_Invoke");
484 IFontDisp_Release(fontdisp);
487 static void test_IsEqual(void)
489 FONTDESC fd;
490 LPVOID pvObj = NULL;
491 LPVOID pvObj2 = NULL;
492 IFont* ifnt = NULL;
493 IFont* ifnt2 = NULL;
494 HRESULT hres;
496 /* Basic font description */
497 fd.cbSizeofstruct = sizeof(FONTDESC);
498 fd.lpstrName = system_font;
499 S(fd.cySize).Lo = 100;
500 S(fd.cySize).Hi = 100;
501 fd.sWeight = 0;
502 fd.sCharset = 0;
503 fd.fItalic = 0;
504 fd.fUnderline = 0;
505 fd.fStrikethrough = 0;
507 /* Create font */
508 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj);
509 ifnt = pvObj;
511 /* Test equal fonts */
512 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj2);
513 ifnt2 = pvObj2;
514 hres = IFont_IsEqual(ifnt,ifnt2);
515 ok(hres == S_OK,
516 "IFont_IsEqual: (EQUAL) Expected S_OK but got 0x%08x\n",hres);
517 IFont_Release(ifnt2);
519 /* Check for bad pointer */
520 hres = IFont_IsEqual(ifnt,NULL);
521 ok(hres == E_POINTER,
522 "IFont_IsEqual: (NULL) Expected 0x80004003 but got 0x%08x\n",hres);
524 /* Test strName */
525 fd.lpstrName = arial_font;
526 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj2);
527 hres = IFont_IsEqual(ifnt,ifnt2);
528 ok(hres == S_FALSE,
529 "IFont_IsEqual: (strName) Expected S_FALSE but got 0x%08x\n",hres);
530 fd.lpstrName = system_font;
531 IFont_Release(ifnt2);
533 /* Test lo font size */
534 S(fd.cySize).Lo = 10000;
535 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj2);
536 ifnt2 = pvObj2;
537 hres = IFont_IsEqual(ifnt,ifnt2);
538 ok(hres == S_FALSE,
539 "IFont_IsEqual: (Lo font size) Expected S_FALSE but got 0x%08x\n",hres);
540 S(fd.cySize).Lo = 100;
541 IFont_Release(ifnt2);
543 /* Test hi font size */
544 S(fd.cySize).Hi = 10000;
545 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj2);
546 ifnt2 = pvObj2;
547 hres = IFont_IsEqual(ifnt,ifnt2);
548 ok(hres == S_FALSE,
549 "IFont_IsEqual: (Hi font size) Expected S_FALSE but got 0x%08x\n",hres);
550 S(fd.cySize).Hi = 100;
551 IFont_Release(ifnt2);
553 /* Test font weight */
554 fd.sWeight = 100;
555 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj2);
556 ifnt2 = pvObj2;
557 hres = IFont_IsEqual(ifnt,ifnt2);
558 ok(hres == S_FALSE,
559 "IFont_IsEqual: (Weight) Expected S_FALSE but got 0x%08x\n",hres);
560 fd.sWeight = 0;
561 IFont_Release(ifnt2);
563 /* Test charset */
564 fd.sCharset = 1;
565 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj2);
566 hres = IFont_IsEqual(ifnt,ifnt2);
567 ok(hres == S_FALSE,
568 "IFont_IsEqual: (Charset) Expected S_FALSE but got 0x%08x\n",hres);
569 fd.sCharset = 0;
570 IFont_Release(ifnt2);
572 /* Test italic setting */
573 fd.fItalic = 1;
574 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj2);
575 hres = IFont_IsEqual(ifnt,ifnt2);
576 ok(hres == S_FALSE,
577 "IFont_IsEqual: (Italic) Expected S_FALSE but got 0x%08x\n",hres);
578 fd.fItalic = 0;
579 IFont_Release(ifnt2);
581 /* Test underline setting */
582 fd.fUnderline = 1;
583 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj2);
584 hres = IFont_IsEqual(ifnt,ifnt2);
585 ok(hres == S_FALSE,
586 "IFont_IsEqual: (Underline) Expected S_FALSE but got 0x%08x\n",hres);
587 fd.fUnderline = 0;
588 IFont_Release(ifnt2);
590 /* Test strikethrough setting */
591 fd.fStrikethrough = 1;
592 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj2);
593 hres = IFont_IsEqual(ifnt,ifnt2);
594 ok(hres == S_FALSE,
595 "IFont_IsEqual: (Strikethrough) Expected S_FALSE but got 0x%08x\n",hres);
596 fd.fStrikethrough = 0;
597 IFont_Release(ifnt2);
599 /* Free IFont. */
600 IFont_Release(ifnt);
603 static void test_ReleaseHfont(void)
605 FONTDESC fd;
606 LPVOID pvObj1 = NULL;
607 LPVOID pvObj2 = NULL;
608 IFont* ifnt1 = NULL;
609 IFont* ifnt2 = NULL;
610 HFONT hfnt1 = 0;
611 HFONT hfnt2 = 0;
612 HRESULT hres;
614 /* Basic font description */
615 fd.cbSizeofstruct = sizeof(FONTDESC);
616 fd.lpstrName = system_font;
617 S(fd.cySize).Lo = 100;
618 S(fd.cySize).Hi = 100;
619 fd.sWeight = 0;
620 fd.sCharset = 0;
621 fd.fItalic = 0;
622 fd.fUnderline = 0;
623 fd.fStrikethrough = 0;
625 /* Create HFONTs and IFONTs */
626 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj1);
627 ifnt1 = pvObj1;
628 IFont_get_hFont(ifnt1,&hfnt1);
629 fd.lpstrName = arial_font;
630 pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj2);
631 ifnt2 = pvObj2;
632 IFont_get_hFont(ifnt2,&hfnt2);
634 /* Try invalid HFONT */
635 hres = IFont_ReleaseHfont(ifnt1,NULL);
636 ok(hres == E_INVALIDARG,
637 "IFont_ReleaseHfont: (Bad HFONT) Expected E_INVALIDARG but got 0x%08x\n",
638 hres);
640 /* Try to add a bad HFONT */
641 hres = IFont_ReleaseHfont(ifnt1,(HFONT)32);
642 ok(hres == S_FALSE,
643 "IFont_ReleaseHfont: (Bad HFONT) Expected S_FALSE but got 0x%08x\n",
644 hres);
646 /* Release all refs */
647 hres = IFont_ReleaseHfont(ifnt1,hfnt1);
648 ok(hres == S_OK,
649 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
650 hres);
652 hres = IFont_ReleaseHfont(ifnt2,hfnt2);
653 ok(hres == S_OK,
654 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
655 hres);
657 /* Check that both lists are empty */
658 hres = IFont_ReleaseHfont(ifnt1,hfnt1);
659 ok(hres == S_FALSE,
660 "IFont_AddRefHfont: (Release ref) Expected S_FALSE but got 0x%08x\n",
661 hres);
663 /* The list should be empty */
664 hres = IFont_ReleaseHfont(ifnt2,hfnt2);
665 ok(hres == S_FALSE,
666 "IFont_AddRefHfont: (Release ref) Expected S_FALSE but got 0x%08x\n",
667 hres);
669 IFont_Release(ifnt1);
670 IFont_Release(ifnt2);
673 static void test_AddRefHfont(void)
675 FONTDESC fd;
676 IFont* ifnt1 = NULL;
677 IFont* ifnt2 = NULL;
678 IFont* ifnt3 = NULL;
679 HFONT hfnt1 = 0;
680 HFONT hfnt2 = 0;
681 HFONT hfnt3 = 0;
682 HRESULT hres;
684 /* Basic font description */
685 fd.cbSizeofstruct = sizeof(FONTDESC);
686 fd.lpstrName = system_font;
687 S(fd.cySize).Lo = 100;
688 S(fd.cySize).Hi = 100;
689 fd.sWeight = 0;
690 fd.sCharset = 0;
691 fd.fItalic = 0;
692 fd.fUnderline = 0;
693 fd.fStrikethrough = 0;
695 /* Create HFONTs and IFONTs */
696 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt1);
697 IFont_get_hFont(ifnt1,&hfnt1);
698 fd.lpstrName = arial_font;
699 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt2);
700 IFont_get_hFont(ifnt2,&hfnt2);
702 /* Try invalid HFONT */
703 hres = IFont_AddRefHfont(ifnt1,NULL);
704 ok(hres == E_INVALIDARG,
705 "IFont_AddRefHfont: (Bad HFONT) Expected E_INVALIDARG but got 0x%08x\n",
706 hres);
708 /* Try to add a bad HFONT */
709 hres = IFont_AddRefHfont(ifnt1,(HFONT)32);
710 ok(hres == S_FALSE,
711 "IFont_AddRefHfont: (Bad HFONT) Expected S_FALSE but got 0x%08x\n",
712 hres);
714 /* Add simple IFONT HFONT pair */
715 hres = IFont_AddRefHfont(ifnt1,hfnt1);
716 ok(hres == S_OK,
717 "IFont_AddRefHfont: (Add ref) Expected S_OK but got 0x%08x\n",
718 hres);
720 /* IFONT and HFONT do not have to be the same (always looks at HFONT) */
721 hres = IFont_AddRefHfont(ifnt2,hfnt1);
722 ok(hres == S_OK,
723 "IFont_AddRefHfont: (Add ref) Expected S_OK but got 0x%08x\n",
724 hres);
726 /* Release all hfnt1 refs */
727 hres = IFont_ReleaseHfont(ifnt1,hfnt1);
728 ok(hres == S_OK,
729 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
730 hres);
732 hres = IFont_ReleaseHfont(ifnt1,hfnt1);
733 ok(hres == S_OK,
734 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
735 hres);
737 hres = IFont_ReleaseHfont(ifnt1,hfnt1);
738 ok(hres == S_OK,
739 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
740 hres);
742 /* Check if hfnt1 is empty */
743 hres = IFont_ReleaseHfont(ifnt1,hfnt1);
744 ok(hres == S_FALSE,
745 "IFont_AddRefHfont: (Release ref) Expected S_FALSE but got 0x%08x\n",
746 hres);
748 /* Release all hfnt2 refs */
749 hres = IFont_ReleaseHfont(ifnt2,hfnt2);
750 ok(hres == S_OK,
751 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
752 hres);
754 /* Check if hfnt2 is empty */
755 hres = IFont_ReleaseHfont(ifnt2,hfnt2);
756 ok(hres == S_FALSE,
757 "IFont_AddRefHfont: (Release ref) Expected S_FALSE but got 0x%08x\n",
758 hres);
760 /* Show that releasing an IFONT does not always release it from the HFONT cache. */
762 IFont_Release(ifnt1);
764 /* Add a reference for destroyed hfnt1 */
765 hres = IFont_AddRefHfont(ifnt2,hfnt1);
766 ok(hres == S_OK,
767 "IFont_AddRefHfont: (Add ref) Expected S_OK but got 0x%08x\n",
768 hres);
770 /* Decrement reference for destroyed hfnt1 */
771 hres = IFont_ReleaseHfont(ifnt2,hfnt1);
772 ok(hres == S_OK,
773 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
774 hres);
776 /* Shows that releasing all IFONT's does clear the HFONT cache. */
778 IFont_Release(ifnt2);
780 /* Need to make a new IFONT for testing */
781 fd.fUnderline = 1;
782 pOleCreateFontIndirect(&fd, &IID_IFont, (void **)&ifnt3);
783 IFont_get_hFont(ifnt3,&hfnt3);
785 /* Add a reference for destroyed hfnt1 */
786 hres = IFont_AddRefHfont(ifnt3,hfnt1);
787 ok(hres == S_FALSE,
788 "IFont_AddRefHfont: (Add ref) Expected S_OK but got 0x%08x\n",
789 hres);
791 /* Decrement reference for destroyed hfnt1 */
792 hres = IFont_ReleaseHfont(ifnt3,hfnt1);
793 ok(hres == S_FALSE,
794 "IFont_AddRefHfont: (Release ref) Expected S_OK but got 0x%08x\n",
795 hres);
797 IFont_Release(ifnt3);
800 START_TEST(olefont)
802 hOleaut32 = GetModuleHandleA("oleaut32.dll");
803 pOleCreateFontIndirect = (void*)GetProcAddress(hOleaut32, "OleCreateFontIndirect");
804 if (!pOleCreateFontIndirect)
806 skip("OleCreateFontIndirect not available\n");
807 return;
810 test_QueryInterface();
811 test_type_info();
813 /* Test various size operations and conversions. */
814 /* Add more as needed. */
815 test_ifont_sizes(180000, 0, 72, 2540, -18, "default");
816 test_ifont_sizes(180000, 0, 144, 2540, -36, "ratio1"); /* change ratio */
817 test_ifont_sizes(180000, 0, 72, 1270, -36, "ratio2"); /* 2nd part of ratio */
819 /* These depend on details of how IFont rounds sizes internally. */
820 test_ifont_sizes(0, 0, 72, 2540, 0, "zero size"); /* zero size */
821 test_ifont_sizes(186000, 0, 72, 2540, -19, "rounding"); /* test rounding */
823 test_font_events_disp();
824 test_GetIDsOfNames();
825 test_Invoke();
826 test_IsEqual();
827 test_ReleaseHfont();
828 test_AddRefHfont();