oleaut32: Clean up IFontDisp::Invoke by using VariantChangeTypeEx to coerce the input...
[wine/multimedia.git] / dlls / oleaut32 / tests / olefont.c
blob1640f796ece0cf270964ecbb511b636ae08736cc
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 HMODULE hOleaut32;
45 static HRESULT (WINAPI *pOleCreateFontIndirect)(LPFONTDESC,REFIID,LPVOID*);
47 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08lx\n", hr)
49 /* Create a font with cySize given by lo_size, hi_size, */
50 /* SetRatio to ratio_logical, ratio_himetric, */
51 /* check that resulting hfont has height hfont_height. */
52 /* Various checks along the way. */
54 static void test_ifont_sizes(long lo_size, long hi_size,
55 long ratio_logical, long ratio_himetric,
56 long hfont_height, const char * test_name)
58 FONTDESC fd;
59 static const WCHAR fname[] = { 'S','y','s','t','e','m',0 };
60 LPVOID pvObj = NULL;
61 IFont* ifnt = NULL;
62 HFONT hfont;
63 LOGFONT lf;
64 CY psize;
65 HRESULT hres;
67 fd.cbSizeofstruct = sizeof(FONTDESC);
68 fd.lpstrName = (WCHAR*)fname;
69 S(fd.cySize).Lo = lo_size;
70 S(fd.cySize).Hi = hi_size;
71 fd.sWeight = 0;
72 fd.sCharset = 0;
73 fd.fItalic = 0;
74 fd.fUnderline = 0;
75 fd.fStrikethrough = 0;
77 /* Create font, test that it worked. */
78 hres = pOleCreateFontIndirect(&fd, &IID_IFont, &pvObj);
79 ifnt = pvObj;
80 ok(hres == S_OK,"%s: OCFI returns 0x%08lx instead of S_OK.\n",
81 test_name, hres);
82 ok(pvObj != NULL,"%s: OCFI returns NULL.\n", test_name);
84 /* Read back size. Hi part was ignored. */
85 hres = IFont_get_Size(ifnt, &psize);
86 ok(hres == S_OK,"%s: IFont_get_size returns 0x%08lx instead of S_OK.\n",
87 test_name, hres);
88 ok(S(psize).Lo == lo_size && S(psize).Hi == 0,
89 "%s: get_Size: Lo=%ld, Hi=%ld; expected Lo=%ld, Hi=%ld.\n",
90 test_name, S(psize).Lo, S(psize).Hi, lo_size, 0L);
92 /* Change ratio, check size unchanged. Standard is 72, 2540. */
93 hres = IFont_SetRatio(ifnt, ratio_logical, ratio_himetric);
94 ok(hres == S_OK,"%s: IFont_SR returns 0x%08lx instead of S_OK.\n",
95 test_name, hres);
96 hres = IFont_get_Size(ifnt, &psize);
97 ok(hres == S_OK,"%s: IFont_get_size returns 0x%08lx instead of S_OK.\n",
98 test_name, hres);
99 ok(S(psize).Lo == lo_size && S(psize).Hi == 0,
100 "%s: gS after SR: Lo=%ld, Hi=%ld; expected Lo=%ld, Hi=%ld.\n",
101 test_name, S(psize).Lo, S(psize).Hi, lo_size, 0L);
103 /* Check hFont size with this ratio. This tests an important */
104 /* conversion for which MSDN is very wrong. */
105 hres = IFont_get_hFont (ifnt, &hfont);
106 ok(hres == S_OK, "%s: IFont_get_hFont returns 0x%08lx instead of S_OK.\n",
107 test_name, hres);
108 hres = GetObject (hfont, sizeof(LOGFONT), &lf);
109 ok(lf.lfHeight == hfont_height,
110 "%s: hFont has lf.lfHeight=%ld, expected %ld.\n",
111 test_name, lf.lfHeight, hfont_height);
113 /* Free IFont. */
114 IFont_Release(ifnt);
117 void test_QueryInterface(void)
119 LPVOID pvObj = NULL;
120 HRESULT hres;
121 IFont* font = NULL;
123 hres = pOleCreateFontIndirect(NULL, &IID_IFont, &pvObj);
124 font = pvObj;
126 ok(hres == S_OK,"OCFI (NULL,..) does not return 0, but 0x%08lx\n",hres);
127 ok(font != NULL,"OCFI (NULL,..) returns NULL, instead of !NULL\n");
129 pvObj = NULL;
130 hres = IFont_QueryInterface( font, &IID_IFont, &pvObj);
132 ok(hres == S_OK,"IFont_QI does not return S_OK, but 0x%08lx\n", hres);
133 ok(pvObj != NULL,"IFont_QI does return NULL, instead of a ptr\n");
135 IFont_Release(font);
138 void test_type_info(void)
140 LPVOID pvObj = NULL;
141 HRESULT hres;
142 IFontDisp* fontdisp = NULL;
143 ITypeInfo* pTInfo;
144 WCHAR name_Name[] = {'N','a','m','e',0};
145 BSTR names[3];
146 UINT n;
147 LCID en_us = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
148 SORT_DEFAULT);
149 DISPPARAMS dispparams;
150 VARIANT varresult;
152 pOleCreateFontIndirect(NULL, &IID_IFontDisp, &pvObj);
153 fontdisp = pvObj;
155 hres = IFontDisp_GetTypeInfo(fontdisp, 0, en_us, &pTInfo);
156 ok(hres == S_OK, "GTI returned 0x%08lx instead of S_OK.\n", hres);
157 ok(pTInfo != NULL, "GTI returned NULL.\n");
159 hres = ITypeInfo_GetNames(pTInfo, DISPID_FONT_NAME, names, 3, &n);
160 ok(hres == S_OK, "GetNames returned 0x%08lx instead of S_OK.\n", hres);
161 ok(n == 1, "GetNames returned %d names instead of 1.\n", n);
162 ok(!lstrcmpiW(names[0],name_Name), "DISPID_FONT_NAME doesn't get 'Names'.\n");
164 ITypeInfo_Release(pTInfo);
166 dispparams.cNamedArgs = 0;
167 dispparams.rgdispidNamedArgs = NULL;
168 dispparams.cArgs = 0;
169 dispparams.rgvarg = NULL;
170 VariantInit(&varresult);
171 hres = IFontDisp_Invoke(fontdisp, DISPID_FONT_NAME, &IID_NULL,
172 LOCALE_NEUTRAL, DISPATCH_PROPERTYGET, &dispparams, &varresult,
173 NULL, NULL);
174 ok(hres == S_OK, "IFontDisp_Invoke return 0x%08lx instead of S_OK.\n", hres);
175 VariantClear(&varresult);
177 IFontDisp_Release(fontdisp);
180 static HRESULT WINAPI FontEventsDisp_QueryInterface(
181 IFontEventsDisp *iface,
182 /* [in] */ REFIID riid,
183 /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject)
185 if (IsEqualIID(riid, &IID_IFontEventsDisp) || IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDispatch))
187 IUnknown_AddRef(iface);
188 *ppvObject = iface;
189 return S_OK;
191 else
193 *ppvObject = NULL;
194 return E_NOINTERFACE;
198 static ULONG WINAPI FontEventsDisp_AddRef(
199 IFontEventsDisp *iface)
201 return 2;
204 static ULONG WINAPI FontEventsDisp_Release(
205 IFontEventsDisp *iface)
207 return 1;
210 static int fonteventsdisp_invoke_called = 0;
212 static HRESULT WINAPI FontEventsDisp_Invoke(
213 IFontEventsDisp __RPC_FAR * iface,
214 /* [in] */ DISPID dispIdMember,
215 /* [in] */ REFIID riid,
216 /* [in] */ LCID lcid,
217 /* [in] */ WORD wFlags,
218 /* [out][in] */ DISPPARAMS __RPC_FAR *pDispParams,
219 /* [out] */ VARIANT __RPC_FAR *pVarResult,
220 /* [out] */ EXCEPINFO __RPC_FAR *pExcepInfo,
221 /* [out] */ UINT __RPC_FAR *puArgErr)
223 static const WCHAR wszBold[] = {'B','o','l','d',0};
224 ok(wFlags == INVOKE_FUNC, "invoke flags should have been INVOKE_FUNC instead of 0x%x\n", wFlags);
225 ok(dispIdMember == DISPID_FONT_CHANGED, "dispIdMember should have been DISPID_FONT_CHANGED instead of 0x%lx\n", dispIdMember);
226 ok(pDispParams->cArgs == 1, "pDispParams->cArgs should have been 1 instead of %d\n", pDispParams->cArgs);
227 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]));
228 ok(!lstrcmpW(V_BSTR(&pDispParams->rgvarg[0]), wszBold), "String in first param should have been \"Bold\"\n");
230 fonteventsdisp_invoke_called++;
231 return S_OK;
234 static IFontEventsDispVtbl FontEventsDisp_Vtbl =
236 FontEventsDisp_QueryInterface,
237 FontEventsDisp_AddRef,
238 FontEventsDisp_Release,
239 NULL,
240 NULL,
241 NULL,
242 FontEventsDisp_Invoke
245 static IFontEventsDisp FontEventsDisp = { &FontEventsDisp_Vtbl };
247 static void test_font_events_disp(void)
249 IFont *pFont;
250 IFont *pFont2;
251 IConnectionPointContainer *pCPC;
252 IConnectionPoint *pCP;
253 FONTDESC fontdesc;
254 HRESULT hr;
255 DWORD dwCookie;
256 IFontDisp *pFontDisp;
257 static const WCHAR wszMSSansSerif[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0};
258 DISPPARAMS dispparams;
259 VARIANTARG vararg;
261 fontdesc.cbSizeofstruct = sizeof(fontdesc);
262 fontdesc.lpstrName = (LPOLESTR)wszMSSansSerif;
263 fontdesc.cySize.int64 = 12 * 10000; /* 12 pt */
264 fontdesc.sWeight = FW_NORMAL;
265 fontdesc.sCharset = 0;
266 fontdesc.fItalic = FALSE;
267 fontdesc.fUnderline = FALSE;
268 fontdesc.fStrikethrough = FALSE;
270 hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void **)&pFont);
271 ok_ole_success(hr, "OleCreateFontIndirect");
273 hr = IFont_QueryInterface(pFont, &IID_IConnectionPointContainer, (void **)&pCPC);
274 ok_ole_success(hr, "IFont_QueryInterface");
276 hr = IConnectionPointContainer_FindConnectionPoint(pCPC, &IID_IFontEventsDisp, &pCP);
277 ok_ole_success(hr, "IConnectionPointContainer_FindConnectionPoint");
278 IConnectionPointContainer_Release(pCPC);
280 hr = IConnectionPoint_Advise(pCP, (IUnknown *)&FontEventsDisp, &dwCookie);
281 ok_ole_success(hr, "IConnectionPoint_Advise");
282 IConnectionPoint_Release(pCP);
284 hr = IFont_put_Bold(pFont, TRUE);
285 ok_ole_success(hr, "IFont_put_Bold");
287 ok(fonteventsdisp_invoke_called == 1, "IFontEventDisp::Invoke wasn't called once\n");
289 hr = IFont_QueryInterface(pFont, &IID_IFontDisp, (void **)&pFontDisp);
290 ok_ole_success(hr, "IFont_QueryInterface");
292 V_VT(&vararg) = VT_BOOL;
293 V_BOOL(&vararg) = VARIANT_FALSE;
294 dispparams.cNamedArgs = 0;
295 dispparams.rgdispidNamedArgs = NULL;
296 dispparams.cArgs = 1;
297 dispparams.rgvarg = &vararg;
298 hr = IFontDisp_Invoke(pFontDisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYPUT, &dispparams, NULL, NULL, NULL);
300 IFontDisp_Release(pFontDisp);
302 ok(fonteventsdisp_invoke_called == 2, "IFontEventDisp::Invoke was called %d times instead of twice\n",
303 fonteventsdisp_invoke_called);
305 hr = IFont_Clone(pFont, &pFont2);
306 ok_ole_success(hr, "IFont_Clone");
307 IFont_Release(pFont);
309 hr = IFont_put_Bold(pFont2, FALSE);
310 ok_ole_success(hr, "IFont_put_Bold");
312 /* this test shows that the notification routine isn't called again */
313 ok(fonteventsdisp_invoke_called == 2, "IFontEventDisp::Invoke was called %d times instead of twice\n",
314 fonteventsdisp_invoke_called);
316 IFont_Release(pFont2);
319 void test_names_ids(WCHAR* w_name_1, const char* a_name_1,
320 WCHAR* w_name_2, const char* a_name_2,
321 LCID lcid, DISPID id_1, DISPID id_2,
322 HRESULT hres_expect, int numnames)
324 LPVOID pvObj = NULL;
325 IFontDisp *fontdisp = NULL;
326 HRESULT hres;
327 DISPID rgDispId[2] = {0xdeadbeef, 0xdeadbeef};
328 LPOLESTR names[2] = {w_name_1, w_name_2};
330 pOleCreateFontIndirect(NULL, &IID_IFontDisp, &pvObj);
331 fontdisp = pvObj;
333 hres = IFontDisp_GetIDsOfNames(fontdisp, &IID_NULL, names, numnames,
334 lcid, rgDispId);
336 /* test hres */
337 ok(hres == hres_expect,
338 "GetIDsOfNames: \"%s\", \"%s\" returns 0x%08lx, expected 0x%08lx.\n",
339 a_name_1, a_name_2, hres, hres_expect);
341 /* test first DISPID */
342 ok(rgDispId[0]==id_1,
343 "GetIDsOfNames: \"%s\" gets DISPID 0x%08lx, expected 0x%08lx.\n",
344 a_name_1, rgDispId[0], id_1);
346 /* test second DISPID is present */
347 if (numnames == 2)
349 ok(rgDispId[1]==id_2,
350 "GetIDsOfNames: ..., \"%s\" gets DISPID 0x%08lx, expected 0x%08lx.\n",
351 a_name_2, rgDispId[1], id_2);
354 IFontDisp_Release(fontdisp);
357 void test_GetIDsOfNames(void)
359 WCHAR name_Name[] = {'N','a','m','e',0};
360 WCHAR name_Italic[] = {'I','t','a','l','i','c',0};
361 WCHAR name_Size[] = {'S','i','z','e',0};
362 WCHAR name_Bold[] = {'B','o','l','d',0};
363 WCHAR name_Underline[] = {'U','n','d','e','r','l','i','n','e',0};
364 WCHAR name_Strikethrough[] = {'S','t','r','i','k','e','t','h','r','o','u','g','h',0};
365 WCHAR name_Weight[] = {'W','e','i','g','h','t',0};
366 WCHAR name_Charset[] = {'C','h','a','r','s','e','t',0};
367 WCHAR name_Foo[] = {'F','o','o',0};
368 WCHAR name_nAmE[] = {'n','A','m','E',0};
369 WCHAR name_Nom[] = {'N','o','m',0};
371 LCID en_us = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
372 SORT_DEFAULT);
373 LCID fr_fr = MAKELCID(MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH),
374 SORT_DEFAULT);
376 /* Test DISPID_FONTs for the various properties. */
377 test_names_ids(name_Name, "Name", NULL, "", en_us,
378 DISPID_FONT_NAME, 0, S_OK,1);
379 test_names_ids(name_Size, "Size", NULL, "", en_us,
380 DISPID_FONT_SIZE, 0, S_OK,1);
381 test_names_ids(name_Bold, "Bold", NULL, "", en_us,
382 DISPID_FONT_BOLD, 0, S_OK,1);
383 test_names_ids(name_Italic, "Italic", NULL, "", en_us,
384 DISPID_FONT_ITALIC, 0, S_OK,1);
385 test_names_ids(name_Underline, "Underline", NULL, "", en_us,
386 DISPID_FONT_UNDER, 0, S_OK,1);
387 test_names_ids(name_Strikethrough, "Strikethrough", NULL, "", en_us,
388 DISPID_FONT_STRIKE, 0, S_OK,1);
389 test_names_ids(name_Weight, "Weight", NULL, "", en_us,
390 DISPID_FONT_WEIGHT, 0, S_OK,1);
391 test_names_ids(name_Charset, "Charset", NULL, "", en_us,
392 DISPID_FONT_CHARSET, 0, S_OK,1);
394 /* Capitalization doesn't matter. */
395 test_names_ids(name_nAmE, "nAmE", NULL, "", en_us,
396 DISPID_FONT_NAME, 0, S_OK,1);
398 /* Unknown name. */
399 test_names_ids(name_Foo, "Foo", NULL, "", en_us,
400 DISPID_UNKNOWN, 0, DISP_E_UNKNOWNNAME,1);
402 /* Pass several names: first is processed, */
403 /* second gets DISPID_UNKNOWN and doesn't affect retval. */
404 test_names_ids(name_Italic, "Italic", name_Name, "Name", en_us,
405 DISPID_FONT_ITALIC, DISPID_UNKNOWN, S_OK,2);
406 test_names_ids(name_Italic, "Italic", name_Foo, "Foo", en_us,
407 DISPID_FONT_ITALIC, DISPID_UNKNOWN, S_OK,2);
409 /* Locale ID has no effect. */
410 test_names_ids(name_Name, "Name", NULL, "", fr_fr,
411 DISPID_FONT_NAME, 0, S_OK,1);
412 test_names_ids(name_Nom, "This is not a font", NULL, "", fr_fr,
413 DISPID_UNKNOWN, 0, DISP_E_UNKNOWNNAME,1);
415 /* One of the arguments are invalid */
416 test_names_ids(name_Name, "Name", NULL, "", en_us,
417 0xdeadbeef, 0xdeadbeef, E_INVALIDARG,0);
418 test_names_ids(name_Italic, "Italic", NULL, "", en_us,
419 0xdeadbeef, 0xdeadbeef, E_INVALIDARG,0);
420 test_names_ids(name_Foo, "Foo", NULL, "", en_us,
421 0xdeadbeef, 0xdeadbeef, E_INVALIDARG,0);
423 /* Crazy locale ID? */
424 test_names_ids(name_Name, "Name", NULL, "", -1,
425 DISPID_FONT_NAME, 0, S_OK,1);
428 START_TEST(olefont)
430 hOleaut32 = LoadLibraryA("oleaut32.dll");
431 pOleCreateFontIndirect = (void*)GetProcAddress(hOleaut32, "OleCreateFontIndirect");
432 if (!pOleCreateFontIndirect)
433 return;
435 test_QueryInterface();
436 test_type_info();
438 /* Test various size operations and conversions. */
439 /* Add more as needed. */
440 test_ifont_sizes(180000, 0, 72, 2540, -18, "default");
441 test_ifont_sizes(180000, 0, 144, 2540, -36, "ratio1"); /* change ratio */
442 test_ifont_sizes(180000, 0, 72, 1270, -36, "ratio2"); /* 2nd part of ratio */
444 /* These depend on details of how IFont rounds sizes internally. */
445 /* test_ifont_sizes(0, 0, 72, 2540, 0, "zero size"); */ /* zero size */
446 /* test_ifont_sizes(186000, 0, 72, 2540, -19, "rounding"); */ /* test rounding */
448 test_font_events_disp();
449 test_GetIDsOfNames();