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
29 #include "wine/test.h"
31 static const CLSID CLSID_JScript
=
32 {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
34 #define DEFINE_EXPECT(func) \
35 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
37 #define SET_EXPECT(func) \
38 expect_ ## func = TRUE
40 #define CHECK_EXPECT2(func) \
42 ok(expect_ ##func, "unexpected call " #func "\n"); \
43 called_ ## func = TRUE; \
46 #define CHECK_EXPECT(func) \
48 CHECK_EXPECT2(func); \
49 expect_ ## func = FALSE; \
52 #define CHECK_CALLED(func) \
54 ok(called_ ## func, "expected " #func "\n"); \
55 expect_ ## func = called_ ## func = FALSE; \
58 DEFINE_EXPECT(testArgConv
);
60 static const WCHAR testW
[] = {'t','e','s','t',0};
62 static IVariantChangeType
*script_change_type
;
63 static IDispatch
*stored_obj
;
65 #define DISPID_TEST_TESTARGCONV 0x1000
67 static BSTR
a2bstr(const char *str
)
72 len
= MultiByteToWideChar(CP_ACP
, 0, str
, -1, NULL
, 0);
73 ret
= SysAllocStringLen(NULL
, len
-1);
74 MultiByteToWideChar(CP_ACP
, 0, str
, -1, ret
, len
);
79 static int strcmp_wa(LPCWSTR strw
, const char *stra
)
82 WideCharToMultiByte(CP_ACP
, 0, strw
, -1, buf
, sizeof(buf
), 0, 0);
83 return lstrcmpA(buf
, stra
);
88 const char *str_result
;
89 VARIANT_BOOL bool_result
;
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
)
100 hres
= IVariantChangeType_ChangeType(change_type
, dst
, src
, 0, vt
);
101 ok_(__FILE__
,line
)(hres
== S_OK
, "ChangeType(%d) failed: %08x\n", vt
, hres
);
102 ok_(__FILE__
,line
)(V_VT(dst
) == vt
, "V_VT(dst) = %d\n", V_VT(dst
));
105 #define change_type_fail(a,b,c,d) _change_type_fail(__LINE__,a,b,c,d)
106 static void _change_type_fail(unsigned line
, IVariantChangeType
*change_type
, VARIANT
*src
, VARTYPE vt
, HRESULT exhres
)
112 hres
= IVariantChangeType_ChangeType(change_type
, &v
, src
, 0, vt
);
113 ok_(__FILE__
,line
)(hres
== exhres
, "ChangeType failed: %08x, expected %08x\n", hres
, exhres
);
116 static void test_change_type(IVariantChangeType
*change_type
, VARIANT
*src
, const conv_results_t
*ex
)
120 call_change_type(change_type
, &v
, src
, VT_I4
);
121 ok(V_I4(&v
) == ex
->int_result
, "V_I4(v) = %d, expected %d\n", V_I4(&v
), ex
->int_result
);
123 call_change_type(change_type
, &v
, src
, VT_BSTR
);
124 ok(!strcmp_wa(V_BSTR(&v
), ex
->str_result
), "V_BSTR(v) = %s, expected %s\n", wine_dbgstr_w(V_BSTR(&v
)), ex
->str_result
);
127 call_change_type(change_type
, &v
, src
, VT_BOOL
);
128 ok(V_BOOL(&v
) == ex
->bool_result
, "V_BOOL(v) = %x, expected %x\n", V_BOOL(&v
), ex
->bool_result
);
130 if(ex
->test_double
) {
131 call_change_type(change_type
, &v
, src
, VT_R8
);
132 ok(V_R8(&v
) == ex
->double_result
, "V_R8(v) = %lf, expected %lf\n", V_R8(&v
), ex
->double_result
);
134 call_change_type(change_type
, &v
, src
, VT_R4
);
135 ok(V_R4(&v
) == (float)ex
->double_result
, "V_R4(v) = %f, expected %f\n", V_R4(&v
), (float)ex
->double_result
);
138 if(V_VT(src
) == VT_NULL
)
139 call_change_type(change_type
, &v
, src
, VT_NULL
);
141 change_type_fail(change_type
, src
, VT_NULL
, E_NOTIMPL
);
143 if(V_VT(src
) == VT_EMPTY
)
144 call_change_type(change_type
, &v
, src
, VT_EMPTY
);
146 change_type_fail(change_type
, src
, VT_EMPTY
, E_NOTIMPL
);
148 call_change_type(change_type
, &v
, src
, VT_I2
);
149 ok(V_I2(&v
) == (INT16
)ex
->int_result
, "V_I2(v) = %d, expected %d\n", V_I2(&v
), ex
->int_result
);
152 static void test_change_types(IVariantChangeType
*change_type
, IDispatch
*obj_disp
)
157 static const conv_results_t bool_results
[] = {
158 {0, "false", VARIANT_FALSE
, 1,0.0},
159 {1, "true", VARIANT_TRUE
, 1,1.0}};
160 static const conv_results_t int_results
[] = {
161 {0, "0", VARIANT_FALSE
, 1,0.0},
162 {-100, "-100", VARIANT_TRUE
, 1,-100.0},
163 {0x10010, "65552", VARIANT_TRUE
, 1,65552.0}};
164 static const conv_results_t empty_results
=
165 {0, "undefined", VARIANT_FALSE
, 0,0};
166 static const conv_results_t null_results
=
167 {0, "null", VARIANT_FALSE
, 0,0};
168 static const conv_results_t obj_results
=
169 {10, "strval", VARIANT_TRUE
, 1,10.0};
172 V_BOOL(&v
) = VARIANT_FALSE
;
173 test_change_type(change_type
, &v
, bool_results
);
174 V_BOOL(&v
) = VARIANT_TRUE
;
175 test_change_type(change_type
, &v
, bool_results
+1);
179 test_change_type(change_type
, &v
, int_results
);
181 test_change_type(change_type
, &v
, int_results
+1);
183 test_change_type(change_type
, &v
, int_results
+2);
186 test_change_type(change_type
, &v
, &empty_results
);
189 test_change_type(change_type
, &v
, &null_results
);
191 V_VT(&v
) = VT_DISPATCH
;
192 V_DISPATCH(&v
) = obj_disp
;
193 test_change_type(change_type
, &v
, &obj_results
);
196 V_BOOL(&v
) = VARIANT_FALSE
;
198 hres
= IVariantChangeType_ChangeType(change_type
, &dst
, &v
, 0, VT_I4
);
199 ok(hres
== DISP_E_BADVARTYPE
, "ChangeType failed: %08x, expected DISP_E_BADVARTYPE\n", hres
);
200 ok(V_VT(&dst
) == 0xdead, "V_VT(dst) = %d\n", V_VT(&dst
));
203 static void test_caller(IServiceProvider
*caller
, IDispatch
*arg_obj
)
205 IVariantChangeType
*change_type
;
208 hres
= IServiceProvider_QueryService(caller
, &SID_VariantConversion
, &IID_IVariantChangeType
, (void**)&change_type
);
209 ok(hres
== S_OK
, "Could not get SID_VariantConversion service: %08x\n", hres
);
211 ok(change_type
== script_change_type
, "change_type != script_change_type\n");
212 test_change_types(change_type
, arg_obj
);
214 IVariantChangeType_Release(change_type
);
217 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
219 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
221 }else if(IsEqualGUID(riid
, &IID_IDispatch
) || IsEqualGUID(riid
, &IID_IDispatchEx
)) {
223 }else if(IsEqualGUID(&IID_IObjectSafety
, riid
)) {
224 ok(0, "Unexpected IID_IObjectSafety query\n");
227 return E_NOINTERFACE
;
233 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
238 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
243 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
245 ok(0, "unexpected call\n");
249 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
250 LCID lcid
, ITypeInfo
**ppTInfo
)
252 ok(0, "unexpected call\n");
256 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
257 LPOLESTR
*rgszNames
, UINT cNames
,
258 LCID lcid
, DISPID
*rgDispId
)
260 ok(0, "unexpected call\n");
264 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
265 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
266 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
268 ok(0, "unexpected call\n");
272 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
274 ok(0, "unexpected call %s %x\n", wine_dbgstr_w(bstrName
), grfdex
);
278 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
280 ok(0, "unexpected call\n");
284 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
286 ok(0, "unexpected call\n");
290 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
292 ok(0, "unexpected call\n");
296 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
298 ok(0, "unexpected call\n");
302 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
304 ok(0, "unexpected call\n");
308 static HRESULT WINAPI
Test_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
310 if(!strcmp_wa(bstrName
, "testArgConv")) {
311 ok(grfdex
== fdexNameCaseSensitive
, "grfdex = %x\n", grfdex
);
312 *pid
= DISPID_TEST_TESTARGCONV
;
319 static HRESULT WINAPI
Test_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
320 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
322 ok(pspCaller
!= NULL
, "pspCaller == NULL\n");
325 case DISPID_TEST_TESTARGCONV
:
326 CHECK_EXPECT(testArgConv
);
328 ok(wFlags
== INVOKE_FUNC
, "wFlags = %x\n", wFlags
);
329 ok(pdp
!= NULL
, "pdp == NULL\n");
330 ok(!pdp
->rgdispidNamedArgs
, "rgdispidNamedArgs != NULL\n");
331 ok(!pvarRes
, "pvarRes != NULL\n");
332 ok(pei
!= NULL
, "pei == NULL\n");
334 ok(pdp
->cArgs
== 1, "cArgs = %d\n", pdp
->cArgs
);
335 ok(V_VT(pdp
->rgvarg
) == VT_DISPATCH
, "V_VT(rgvarg) = %d\n", V_VT(pdp
->rgvarg
));
337 test_caller(pspCaller
, V_DISPATCH(pdp
->rgvarg
));
339 stored_obj
= V_DISPATCH(pdp
->rgvarg
);
340 IDispatch_AddRef(stored_obj
);
344 ok(0, "unexpected call\n");
351 static IDispatchExVtbl testObjVtbl
= {
352 DispatchEx_QueryInterface
,
355 DispatchEx_GetTypeInfoCount
,
356 DispatchEx_GetTypeInfo
,
357 DispatchEx_GetIDsOfNames
,
361 DispatchEx_DeleteMemberByName
,
362 DispatchEx_DeleteMemberByDispID
,
363 DispatchEx_GetMemberProperties
,
364 DispatchEx_GetMemberName
,
365 DispatchEx_GetNextDispID
,
366 DispatchEx_GetNameSpaceParent
369 static IDispatchEx testObj
= { &testObjVtbl
};
371 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
373 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
375 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
379 return E_NOINTERFACE
;
382 IUnknown_AddRef((IUnknown
*)*ppv
);
386 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
391 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
396 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
398 *plcid
= GetUserDefaultLCID();
402 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
403 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
405 ok(dwReturnMask
== SCRIPTINFO_IUNKNOWN
, "unexpected dwReturnMask %x\n", dwReturnMask
);
406 ok(!ppti
, "ppti != NULL\n");
407 ok(!strcmp_wa(pstrName
, "test"), "pstrName = %s\n", wine_dbgstr_w(pstrName
));
409 *ppiunkItem
= (IUnknown
*)&testObj
;
413 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
418 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
419 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
424 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
429 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
434 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
439 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
444 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
445 ActiveScriptSite_QueryInterface
,
446 ActiveScriptSite_AddRef
,
447 ActiveScriptSite_Release
,
448 ActiveScriptSite_GetLCID
,
449 ActiveScriptSite_GetItemInfo
,
450 ActiveScriptSite_GetDocVersionString
,
451 ActiveScriptSite_OnScriptTerminate
,
452 ActiveScriptSite_OnStateChange
,
453 ActiveScriptSite_OnScriptError
,
454 ActiveScriptSite_OnEnterScript
,
455 ActiveScriptSite_OnLeaveScript
458 static IActiveScriptSite ActiveScriptSite
= { &ActiveScriptSiteVtbl
};
460 #define parse_script_a(p,s) _parse_script_a(__LINE__,p,s)
461 static void _parse_script_a(unsigned line
, IActiveScriptParse
*parser
, const char *script
)
466 str
= a2bstr(script
);
467 hres
= IActiveScriptParse64_ParseScriptText(parser
, str
, NULL
, NULL
, NULL
, 0, 0, 0, NULL
, NULL
);
469 ok_(__FILE__
,line
)(hres
== S_OK
, "ParseScriptText failed: %08x\n", hres
);
472 static IActiveScriptParse
*create_script(void)
474 IActiveScriptParse
*parser
;
475 IActiveScript
*script
;
478 hres
= CoCreateInstance(&CLSID_JScript
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
479 &IID_IActiveScript
, (void**)&script
);
483 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptParse
, (void**)&parser
);
484 ok(hres
== S_OK
, "Could not get IActiveScriptParse: %08x\n", hres
);
486 hres
= IActiveScriptParse64_InitNew(parser
);
487 ok(hres
== S_OK
, "InitNew failed: %08x\n", hres
);
489 hres
= IActiveScript_SetScriptSite(script
, &ActiveScriptSite
);
490 ok(hres
== S_OK
, "SetScriptSite failed: %08x\n", hres
);
492 hres
= IActiveScript_AddNamedItem(script
, testW
,
493 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
494 ok(hres
== S_OK
, "AddNamedItem failed: %08x\n", hres
);
496 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_STARTED
);
497 ok(hres
== S_OK
, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres
);
499 IActiveScript_Release(script
);
504 static void run_scripts(void)
506 IActiveScriptParse
*parser
;
509 parser
= create_script();
511 hres
= IActiveScriptParse64_QueryInterface(parser
, &IID_IVariantChangeType
, (void**)&script_change_type
);
512 ok(hres
== S_OK
, "Could not get IVariantChangeType iface: %08x\n", hres
);
514 SET_EXPECT(testArgConv
);
515 parse_script_a(parser
,
517 " toString: function() { return 'strval'; },"
518 " valueOf: function() { return 10; }"
520 "testArgConv(obj);");
521 CHECK_CALLED(testArgConv
);
523 test_change_types(script_change_type
, stored_obj
);
524 IDispatch_Release(stored_obj
);
525 IVariantChangeType_Release(script_change_type
);
527 IUnknown_Release(parser
);
530 static BOOL
check_jscript(void)
532 IActiveScriptProperty
*script_prop
;
533 IActiveScriptParse
*parser
;
537 parser
= create_script();
541 str
= a2bstr("if(!('localeCompare' in String.prototype)) throw 1;");
542 hres
= IActiveScriptParse64_ParseScriptText(parser
, str
, NULL
, NULL
, NULL
, 0, 0, 0, NULL
, NULL
);
546 hres
= IUnknown_QueryInterface(parser
, &IID_IActiveScriptProperty
, (void**)&script_prop
);
547 IUnknown_Release(parser
);
549 IActiveScriptProperty_Release(script_prop
);
561 win_skip("Broken (too old) jscript\n");