oleaut32: Add a successful test for IFontDisp::Invoke.
[wine] / dlls / oleaut32 / tests / 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 #include <stdarg.h>
24 #include <stdio.h>
25 #include <math.h>
26 #include <float.h>
27 #include <time.h>
28
29 #define COBJMACROS
30
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>
42
43 static HMODULE hOleaut32;
44
45 static HRESULT (WINAPI *pOleCreateFontIndirect)(LPFONTDESC,REFIID,LPVOID*);
46
47 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08lx\n", hr)
48
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.                         */
53
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)
57 {
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;
66
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;
76
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);
83
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);
91
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);
102
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);
112
113         /* Free IFont. */
114         IFont_Release(ifnt);
115 }
116
117 void test_QueryInterface(void)
118 {
119         LPVOID pvObj = NULL;
120         HRESULT hres;
121         IFont*  font = NULL;
122
123         hres = pOleCreateFontIndirect(NULL, &IID_IFont, &pvObj);
124         font = pvObj;
125
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");
128
129         pvObj = NULL;
130         hres = IFont_QueryInterface( font, &IID_IFont, &pvObj);
131
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");
134
135         IFont_Release(font);
136 }
137
138 void test_type_info(void)
139 {
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;
151
152         pOleCreateFontIndirect(NULL, &IID_IFontDisp, &pvObj);
153         fontdisp = pvObj;
154
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");
158
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");
163
164         ITypeInfo_Release(pTInfo);
165
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);
176
177         IFontDisp_Release(fontdisp);
178 }
179
180 static HRESULT WINAPI FontEventsDisp_QueryInterface(
181         IFontEventsDisp *iface,
182     /* [in] */ REFIID riid,
183     /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject)
184 {
185     if (IsEqualIID(riid, &IID_IFontEventsDisp) || IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDispatch))
186     {
187         IUnknown_AddRef(iface);
188         *ppvObject = iface;
189         return S_OK;
190     }
191     else
192     {
193         *ppvObject = NULL;
194         return E_NOINTERFACE;
195     }
196 }
197
198 static ULONG WINAPI FontEventsDisp_AddRef(
199     IFontEventsDisp *iface)
200 {
201     return 2;
202 }
203
204 static ULONG WINAPI FontEventsDisp_Release(
205         IFontEventsDisp *iface)
206 {
207     return 1;
208 }
209
210 static int fonteventsdisp_invoke_called = 0;
211
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)
222 {
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");
229
230     fonteventsdisp_invoke_called++;
231     return S_OK;
232 }
233
234 static IFontEventsDispVtbl FontEventsDisp_Vtbl =
235 {
236     FontEventsDisp_QueryInterface,
237     FontEventsDisp_AddRef,
238     FontEventsDisp_Release,
239     NULL,
240     NULL,
241     NULL,
242     FontEventsDisp_Invoke
243 };
244
245 static IFontEventsDisp FontEventsDisp = { &FontEventsDisp_Vtbl };
246
247 static void test_font_events_disp(void)
248 {
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;
260
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;
269
270     hr = pOleCreateFontIndirect(&fontdesc, &IID_IFont, (void **)&pFont);
271     ok_ole_success(hr, "OleCreateFontIndirect");
272
273     hr = IFont_QueryInterface(pFont, &IID_IConnectionPointContainer, (void **)&pCPC);
274     ok_ole_success(hr, "IFont_QueryInterface");
275
276     hr = IConnectionPointContainer_FindConnectionPoint(pCPC, &IID_IFontEventsDisp, &pCP);
277     ok_ole_success(hr, "IConnectionPointContainer_FindConnectionPoint");
278     IConnectionPointContainer_Release(pCPC);
279
280     hr = IConnectionPoint_Advise(pCP, (IUnknown *)&FontEventsDisp, &dwCookie);
281     ok_ole_success(hr, "IConnectionPoint_Advise");
282     IConnectionPoint_Release(pCP);
283
284     hr = IFont_put_Bold(pFont, TRUE);
285     ok_ole_success(hr, "IFont_put_Bold");
286
287     ok(fonteventsdisp_invoke_called == 1, "IFontEventDisp::Invoke wasn't called once\n");
288
289     hr = IFont_QueryInterface(pFont, &IID_IFontDisp, (void **)&pFontDisp);
290     ok_ole_success(hr, "IFont_QueryInterface");
291
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);
299
300     IFontDisp_Release(pFontDisp);
301
302     ok(fonteventsdisp_invoke_called == 2, "IFontEventDisp::Invoke was called %d times instead of twice\n",
303         fonteventsdisp_invoke_called);
304
305     hr = IFont_Clone(pFont, &pFont2);
306     ok_ole_success(hr, "IFont_Clone");
307     IFont_Release(pFont);
308
309     hr = IFont_put_Bold(pFont2, FALSE);
310     ok_ole_success(hr, "IFont_put_Bold");
311
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);
315
316     IFont_Release(pFont2);
317 }
318
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)
323 {
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};
329
330     pOleCreateFontIndirect(NULL, &IID_IFontDisp, &pvObj);
331     fontdisp = pvObj;
332
333     hres = IFontDisp_GetIDsOfNames(fontdisp, &IID_NULL, names, numnames,
334                                    lcid, rgDispId);
335
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);
340
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);
345
346     /* test second DISPID is present */
347     if (numnames == 2)
348     {
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);
352     }
353
354    IFontDisp_Release(fontdisp);
355 }
356
357 void test_GetIDsOfNames(void)
358 {
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};
370
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);
375
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);
393
394     /* Capitalization doesn't matter. */
395     test_names_ids(name_nAmE, "nAmE", NULL, "", en_us,
396                    DISPID_FONT_NAME, 0, S_OK,1);
397
398     /* Unknown name. */
399     test_names_ids(name_Foo, "Foo", NULL, "", en_us,
400                    DISPID_UNKNOWN, 0, DISP_E_UNKNOWNNAME,1);
401
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);
408
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);
414
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);
422
423     /* Crazy locale ID? */
424     test_names_ids(name_Name, "Name", NULL, "", -1,
425                    DISPID_FONT_NAME, 0, S_OK,1);
426 }
427
428 static void test_Invoke(void)
429 {
430     IFontDisp *fontdisp;
431     HRESULT hr;
432     VARIANTARG vararg;
433     DISPPARAMS dispparams;
434     VARIANT varresult;
435
436     hr = pOleCreateFontIndirect(NULL, &IID_IFontDisp, (void **)&fontdisp);
437     ok_ole_success(hr, "OleCreateFontIndirect");
438
439     V_VT(&vararg) = VT_BOOL;
440     V_BOOL(&vararg) = VARIANT_FALSE;
441     dispparams.cNamedArgs = 0;
442     dispparams.rgdispidNamedArgs = NULL;
443     dispparams.cArgs = 1;
444     dispparams.rgvarg = &vararg;
445     hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_IFontDisp, 0, DISPATCH_PROPERTYPUT, &dispparams, NULL, NULL, NULL);
446     ok(hr == DISP_E_UNKNOWNINTERFACE, "IFontDisp_Invoke should have returned DISP_E_UNKNOWNINTERFACE instead of 0x%08lx\n", hr);
447
448     dispparams.cArgs = 0;
449     dispparams.rgvarg = NULL;
450     hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYPUT, &dispparams, NULL, NULL, NULL);
451     ok(hr == DISP_E_BADPARAMCOUNT, "IFontDisp_Invoke should have returned DISP_E_BADPARAMCOUNT instead of 0x%08lx\n", hr);
452
453     hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYPUT, NULL, NULL, NULL, NULL);
454     ok(hr == DISP_E_PARAMNOTOPTIONAL, "IFontDisp_Invoke should have returned DISP_E_PARAMNOTOPTIONAL instead of 0x%08lx\n", hr);
455
456     hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYGET, NULL, NULL, NULL, NULL);
457     ok(hr == DISP_E_PARAMNOTOPTIONAL, "IFontDisp_Invoke should have returned DISP_E_PARAMNOTOPTIONAL instead of 0x%08lx\n", hr);
458
459     hr = IFontDisp_Invoke(fontdisp, DISPID_FONT_BOLD, &IID_NULL, 0, DISPATCH_PROPERTYGET, NULL, &varresult, NULL, NULL);
460     ok_ole_success(hr, "IFontDisp_Invoke");
461
462     IFontDisp_Release(fontdisp);
463 }
464
465 START_TEST(olefont)
466 {
467         hOleaut32 = LoadLibraryA("oleaut32.dll");    
468         pOleCreateFontIndirect = (void*)GetProcAddress(hOleaut32, "OleCreateFontIndirect");
469         if (!pOleCreateFontIndirect)
470             return;
471
472         test_QueryInterface();
473         test_type_info();
474
475         /* Test various size operations and conversions. */
476         /* Add more as needed. */
477         test_ifont_sizes(180000, 0, 72, 2540, -18, "default");
478         test_ifont_sizes(180000, 0, 144, 2540, -36, "ratio1");          /* change ratio */
479         test_ifont_sizes(180000, 0, 72, 1270, -36, "ratio2");           /* 2nd part of ratio */
480
481         /* These depend on details of how IFont rounds sizes internally. */
482         /* test_ifont_sizes(0, 0, 72, 2540, 0, "zero size");    */      /* zero size */
483         /* test_ifont_sizes(186000, 0, 72, 2540, -19, "rounding"); */   /* test rounding */
484
485         test_font_events_disp();
486         test_GetIDsOfNames();
487         test_Invoke();
488 }