mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / jscript / tests / caller.c
blob19323e8eaf1088086b887aa0d1ac315378d51957
1 /*
2 * Copyright 2012 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdio.h>
21 #define COBJMACROS
22 #define CONST_VTABLE
24 #include <ole2.h>
25 #include <dispex.h>
26 #include <activscp.h>
27 #include <objsafe.h>
29 #include "wine/test.h"
31 #ifdef _WIN64
33 #define IActiveScriptParse_QueryInterface IActiveScriptParse64_QueryInterface
34 #define IActiveScriptParse_Release IActiveScriptParse64_Release
35 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
36 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
38 #else
40 #define IActiveScriptParse_QueryInterface IActiveScriptParse32_QueryInterface
41 #define IActiveScriptParse_Release IActiveScriptParse32_Release
42 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
43 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
45 #endif
47 static const CLSID CLSID_JScript =
48 {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
50 #define DEFINE_EXPECT(func) \
51 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
53 #define SET_EXPECT(func) \
54 expect_ ## func = TRUE
56 #define CHECK_EXPECT2(func) \
57 do { \
58 ok(expect_ ##func, "unexpected call " #func "\n"); \
59 called_ ## func = TRUE; \
60 }while(0)
62 #define CHECK_EXPECT(func) \
63 do { \
64 CHECK_EXPECT2(func); \
65 expect_ ## func = FALSE; \
66 }while(0)
68 #define CHECK_CALLED(func) \
69 do { \
70 ok(called_ ## func, "expected " #func "\n"); \
71 expect_ ## func = called_ ## func = FALSE; \
72 }while(0)
74 #define CLEAR_CALLED(func) \
75 expect_ ## func = called_ ## func = FALSE
77 DEFINE_EXPECT(testArgConv);
78 DEFINE_EXPECT(OnEnterScript);
79 DEFINE_EXPECT(OnLeaveScript);
81 static IVariantChangeType *script_change_type;
82 static IDispatch *stored_obj;
84 #define DISPID_TEST_TESTARGCONV 0x1000
86 typedef struct {
87 int int_result;
88 const WCHAR *str_result;
89 VARIANT_BOOL bool_result;
90 int test_double;
91 double double_result;
92 } conv_results_t;
94 #define call_change_type(a,b,c,d) _call_change_type(__LINE__,a,b,c,d)
95 static void _call_change_type(unsigned line, IVariantChangeType *change_type, VARIANT *dst, VARIANT *src, VARTYPE vt)
97 HRESULT hres;
99 VariantInit(dst);
100 if(V_VT(src) == VT_DISPATCH && vt != VT_BOOL) {
101 SET_EXPECT(OnEnterScript);
102 SET_EXPECT(OnLeaveScript);
104 hres = IVariantChangeType_ChangeType(change_type, dst, src, 0, vt);
105 ok_(__FILE__,line)(hres == S_OK, "ChangeType(%d) failed: %08x\n", vt, hres);
106 ok_(__FILE__,line)(V_VT(dst) == vt, "V_VT(dst) = %d\n", V_VT(dst));
107 if(V_VT(src) == VT_DISPATCH && vt != VT_BOOL) {
108 CHECK_CALLED(OnEnterScript);
109 CHECK_CALLED(OnLeaveScript);
113 #define change_type_fail(a,b,c,d) _change_type_fail(__LINE__,a,b,c,d)
114 static void _change_type_fail(unsigned line, IVariantChangeType *change_type, VARIANT *src, VARTYPE vt, HRESULT exhres)
116 VARIANT v;
117 HRESULT hres;
119 V_VT(&v) = VT_EMPTY;
120 hres = IVariantChangeType_ChangeType(change_type, &v, src, 0, vt);
121 ok_(__FILE__,line)(hres == exhres, "ChangeType failed: %08x, expected %08x\n", hres, exhres);
124 static void test_change_type(IVariantChangeType *change_type, VARIANT *src, const conv_results_t *ex)
126 VARIANT v;
128 call_change_type(change_type, &v, src, VT_I4);
129 ok(V_I4(&v) == ex->int_result, "V_I4(v) = %d, expected %d\n", V_I4(&v), ex->int_result);
131 call_change_type(change_type, &v, src, VT_UI2);
132 ok(V_UI2(&v) == (UINT16)ex->int_result, "V_UI2(v) = %u, expected %u\n", V_UI2(&v), (UINT16)ex->int_result);
134 call_change_type(change_type, &v, src, VT_BSTR);
135 ok(!lstrcmpW(V_BSTR(&v), ex->str_result), "V_BSTR(v) = %s, expected %s\n", wine_dbgstr_w(V_BSTR(&v)), wine_dbgstr_w(ex->str_result));
136 VariantClear(&v);
138 call_change_type(change_type, &v, src, VT_BOOL);
139 ok(V_BOOL(&v) == ex->bool_result, "V_BOOL(v) = %x, expected %x\n", V_BOOL(&v), ex->bool_result);
141 if(ex->test_double) {
142 call_change_type(change_type, &v, src, VT_R8);
143 ok(V_R8(&v) == ex->double_result, "V_R8(v) = %lf, expected %lf\n", V_R8(&v), ex->double_result);
145 call_change_type(change_type, &v, src, VT_R4);
146 ok(V_R4(&v) == (float)ex->double_result, "V_R4(v) = %f, expected %f\n", V_R4(&v), (float)ex->double_result);
149 if(V_VT(src) == VT_NULL)
150 call_change_type(change_type, &v, src, VT_NULL);
151 else
152 change_type_fail(change_type, src, VT_NULL, E_NOTIMPL);
154 if(V_VT(src) == VT_EMPTY)
155 call_change_type(change_type, &v, src, VT_EMPTY);
156 else
157 change_type_fail(change_type, src, VT_EMPTY, E_NOTIMPL);
159 call_change_type(change_type, &v, src, VT_I2);
160 ok(V_I2(&v) == (INT16)ex->int_result, "V_I2(v) = %d, expected %d\n", V_I2(&v), ex->int_result);
163 static void test_change_types(IVariantChangeType *change_type, IDispatch *obj_disp)
165 VARIANT v, dst;
166 BSTR str;
167 HRESULT hres;
169 static const conv_results_t bool_results[] = {
170 {0, L"false", VARIANT_FALSE, 1,0.0},
171 {1, L"true", VARIANT_TRUE, 1,1.0}};
172 static const conv_results_t int_results[] = {
173 {0, L"0", VARIANT_FALSE, 1,0.0},
174 {-100, L"-100", VARIANT_TRUE, 1,-100.0},
175 {0x10010, L"65552", VARIANT_TRUE, 1,65552.0}};
176 static const conv_results_t empty_results =
177 {0, L"undefined", VARIANT_FALSE, 0,0};
178 static const conv_results_t null_results =
179 {0, L"null", VARIANT_FALSE, 0,0};
180 static const conv_results_t obj_results =
181 {10, L"strval", VARIANT_TRUE, 1,10.0};
183 V_VT(&v) = VT_BOOL;
184 V_BOOL(&v) = VARIANT_FALSE;
185 test_change_type(change_type, &v, bool_results);
186 V_BOOL(&v) = VARIANT_TRUE;
187 test_change_type(change_type, &v, bool_results+1);
189 V_VT(&v) = VT_I4;
190 V_I4(&v) = 0;
191 test_change_type(change_type, &v, int_results);
192 V_I4(&v) = -100;
193 test_change_type(change_type, &v, int_results+1);
194 V_I4(&v) = 0x10010;
195 test_change_type(change_type, &v, int_results+2);
197 V_VT(&v) = VT_EMPTY;
198 test_change_type(change_type, &v, &empty_results);
200 V_VT(&v) = VT_NULL;
201 test_change_type(change_type, &v, &null_results);
203 V_VT(&v) = VT_DISPATCH;
204 V_DISPATCH(&v) = obj_disp;
205 test_change_type(change_type, &v, &obj_results);
207 V_VT(&v) = VT_BOOL;
208 V_BOOL(&v) = VARIANT_FALSE;
209 V_VT(&dst) = 0xdead;
210 hres = IVariantChangeType_ChangeType(change_type, &dst, &v, 0, VT_I4);
211 ok(hres == DISP_E_BADVARTYPE, "ChangeType failed: %08x, expected DISP_E_BADVARTYPE\n", hres);
212 ok(V_VT(&dst) == 0xdead, "V_VT(dst) = %d\n", V_VT(&dst));
214 /* Test conversion in place */
215 V_VT(&v) = VT_BSTR;
216 V_BSTR(&v) = str = SysAllocString(L"test");
217 hres = IVariantChangeType_ChangeType(change_type, &v, &v, 0, VT_BSTR);
218 ok(hres == S_OK, "ChangeType failed: %08x\n", hres);
219 ok(V_VT(&v) == VT_BSTR, "V_VT(v) = %d\n", V_VT(&v));
220 ok(V_BSTR(&v) != str, "V_BSTR(v) == str\n");
221 ok(!lstrcmpW(V_BSTR(&v), L"test"), "V_BSTR(v) = %s\n", wine_dbgstr_w(V_BSTR(&v)));
222 VariantClear(&v);
225 static void test_caller(IServiceProvider *caller, IDispatch *arg_obj)
227 IVariantChangeType *change_type;
228 HRESULT hres;
230 hres = IServiceProvider_QueryService(caller, &SID_VariantConversion, &IID_IVariantChangeType, (void**)&change_type);
231 ok(hres == S_OK, "Could not get SID_VariantConversion service: %08x\n", hres);
233 ok(change_type == script_change_type, "change_type != script_change_type\n");
234 test_change_types(change_type, arg_obj);
236 IVariantChangeType_Release(change_type);
239 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
241 if(IsEqualGUID(riid, &IID_IUnknown)) {
242 *ppv = iface;
243 }else if(IsEqualGUID(riid, &IID_IDispatch) || IsEqualGUID(riid, &IID_IDispatchEx)) {
244 *ppv = iface;
245 }else if(IsEqualGUID(&IID_IObjectSafety, riid)) {
246 ok(0, "Unexpected IID_IObjectSafety query\n");
247 }else {
248 *ppv = NULL;
249 return E_NOINTERFACE;
252 return S_OK;
255 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
257 return 2;
260 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
262 return 1;
265 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
267 ok(0, "unexpected call\n");
268 return E_NOTIMPL;
271 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
273 return E_NOTIMPL;
276 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
277 LPOLESTR *rgszNames, UINT cNames,
278 LCID lcid, DISPID *rgDispId)
280 ok(0, "unexpected call\n");
281 return E_NOTIMPL;
284 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
285 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
286 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
288 ok(0, "unexpected call\n");
289 return E_NOTIMPL;
292 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
294 ok(0, "unexpected call %s %x\n", wine_dbgstr_w(bstrName), grfdex);
295 return E_NOTIMPL;
298 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
300 ok(0, "unexpected call\n");
301 return E_NOTIMPL;
304 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
306 ok(0, "unexpected call\n");
307 return E_NOTIMPL;
310 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
312 ok(0, "unexpected call\n");
313 return E_NOTIMPL;
316 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
318 ok(0, "unexpected call\n");
319 return E_NOTIMPL;
322 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
324 ok(0, "unexpected call\n");
325 return E_NOTIMPL;
328 static HRESULT WINAPI Test_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
330 if(!lstrcmpW(bstrName, L"testArgConv")) {
331 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
332 *pid = DISPID_TEST_TESTARGCONV;
333 return S_OK;
336 return E_NOTIMPL;
339 static HRESULT WINAPI Test_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
340 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
342 ok(pspCaller != NULL, "pspCaller == NULL\n");
344 switch(id) {
345 case DISPID_TEST_TESTARGCONV:
346 CHECK_EXPECT(testArgConv);
348 ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
349 ok(pdp != NULL, "pdp == NULL\n");
350 ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
351 ok(!pvarRes, "pvarRes != NULL\n");
352 ok(pei != NULL, "pei == NULL\n");
354 ok(pdp->cArgs == 1, "cArgs = %d\n", pdp->cArgs);
355 ok(V_VT(pdp->rgvarg) == VT_DISPATCH, "V_VT(rgvarg) = %d\n", V_VT(pdp->rgvarg));
357 CHECK_CALLED(OnEnterScript);
358 test_caller(pspCaller, V_DISPATCH(pdp->rgvarg));
359 SET_EXPECT(OnLeaveScript);
361 stored_obj = V_DISPATCH(pdp->rgvarg);
362 IDispatch_AddRef(stored_obj);
363 break;
365 default:
366 ok(0, "unexpected call\n");
367 return E_NOTIMPL;
370 return S_OK;
373 static IDispatchExVtbl testObjVtbl = {
374 DispatchEx_QueryInterface,
375 DispatchEx_AddRef,
376 DispatchEx_Release,
377 DispatchEx_GetTypeInfoCount,
378 DispatchEx_GetTypeInfo,
379 DispatchEx_GetIDsOfNames,
380 DispatchEx_Invoke,
381 Test_GetDispID,
382 Test_InvokeEx,
383 DispatchEx_DeleteMemberByName,
384 DispatchEx_DeleteMemberByDispID,
385 DispatchEx_GetMemberProperties,
386 DispatchEx_GetMemberName,
387 DispatchEx_GetNextDispID,
388 DispatchEx_GetNameSpaceParent
391 static IDispatchEx testObj = { &testObjVtbl };
393 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
395 if(IsEqualGUID(&IID_IUnknown, riid)) {
396 *ppv = iface;
397 }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
398 *ppv = iface;
399 }else {
400 *ppv = NULL;
401 return E_NOINTERFACE;
404 IUnknown_AddRef((IUnknown*)*ppv);
405 return S_OK;
408 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
410 return 2;
413 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
415 return 1;
418 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
420 *plcid = GetUserDefaultLCID();
421 return S_OK;
424 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
425 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
427 ok(dwReturnMask == SCRIPTINFO_IUNKNOWN, "unexpected dwReturnMask %x\n", dwReturnMask);
428 ok(!ppti, "ppti != NULL\n");
429 ok(!lstrcmpW(pstrName, L"test"), "pstrName = %s\n", wine_dbgstr_w(pstrName));
431 *ppiunkItem = (IUnknown*)&testObj;
432 return S_OK;
435 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
437 return E_NOTIMPL;
440 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
441 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
443 return E_NOTIMPL;
446 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
448 return E_NOTIMPL;
451 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
453 return E_NOTIMPL;
456 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
458 CHECK_EXPECT(OnEnterScript);
459 return E_NOTIMPL;
462 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
464 CHECK_EXPECT(OnLeaveScript);
465 return E_NOTIMPL;
468 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
469 ActiveScriptSite_QueryInterface,
470 ActiveScriptSite_AddRef,
471 ActiveScriptSite_Release,
472 ActiveScriptSite_GetLCID,
473 ActiveScriptSite_GetItemInfo,
474 ActiveScriptSite_GetDocVersionString,
475 ActiveScriptSite_OnScriptTerminate,
476 ActiveScriptSite_OnStateChange,
477 ActiveScriptSite_OnScriptError,
478 ActiveScriptSite_OnEnterScript,
479 ActiveScriptSite_OnLeaveScript
482 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
484 #define parse_script(p,s) _parse_script(__LINE__,p,s)
485 static void _parse_script(unsigned line, IActiveScriptParse *parser, const WCHAR *script)
487 HRESULT hres;
489 hres = IActiveScriptParse_ParseScriptText(parser, script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
490 ok_(__FILE__,line)(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
493 static IActiveScriptParse *create_script(void)
495 IActiveScriptParse *parser;
496 IActiveScript *script;
497 HRESULT hres;
499 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
500 &IID_IActiveScript, (void**)&script);
501 if(FAILED(hres))
502 return NULL;
504 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parser);
505 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
507 hres = IActiveScriptParse_InitNew(parser);
508 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
510 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
511 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
513 hres = IActiveScript_AddNamedItem(script, L"test",
514 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
515 ok(hres == S_OK, "AddNamedItem failed: %08x\n", hres);
517 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
518 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
520 IActiveScript_Release(script);
522 return parser;
525 static void run_scripts(void)
527 IActiveScriptParse *parser;
528 HRESULT hres;
530 parser = create_script();
532 hres = IActiveScriptParse_QueryInterface(parser, &IID_IVariantChangeType, (void**)&script_change_type);
533 ok(hres == S_OK, "Could not get IVariantChangeType iface: %08x\n", hres);
535 SET_EXPECT(OnEnterScript); /* checked in callback */
536 SET_EXPECT(testArgConv);
537 parse_script(parser,
538 L"var obj = {"
539 L" toString: function() { return 'strval'; },"
540 L" valueOf: function() { return 10; }"
541 L"};"
542 L"testArgConv(obj);");
543 CHECK_CALLED(testArgConv);
544 CHECK_CALLED(OnLeaveScript); /* set in callback */
546 test_change_types(script_change_type, stored_obj);
547 IDispatch_Release(stored_obj);
548 IVariantChangeType_Release(script_change_type);
550 IActiveScriptParse_Release(parser);
553 static BOOL check_jscript(void)
555 IActiveScriptProperty *script_prop;
556 IActiveScriptParse *parser;
557 HRESULT hres;
559 parser = create_script();
560 if(!parser)
561 return FALSE;
563 SET_EXPECT(OnEnterScript);
564 SET_EXPECT(OnLeaveScript);
565 hres = IActiveScriptParse_ParseScriptText(parser, L"if(!('localeCompare' in String.prototype)) throw 1;",
566 NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
567 CLEAR_CALLED(OnEnterScript);
568 CLEAR_CALLED(OnLeaveScript);
569 if(hres == S_OK)
570 hres = IActiveScriptParse_QueryInterface(parser, &IID_IActiveScriptProperty, (void**)&script_prop);
571 IActiveScriptParse_Release(parser);
572 if(hres == S_OK)
573 IActiveScriptProperty_Release(script_prop);
575 return hres == S_OK;
578 START_TEST(caller)
580 CoInitialize(NULL);
582 if(check_jscript())
583 run_scripts();
584 else
585 win_skip("Broken (too old) jscript\n");
587 CoUninitialize();