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"
33 #define IActiveScriptParse_QueryInterface IActiveScriptParse64_QueryInterface
34 #define IActiveScriptParse_Release IActiveScriptParse64_Release
35 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
36 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
40 #define IActiveScriptParse_QueryInterface IActiveScriptParse32_QueryInterface
41 #define IActiveScriptParse_Release IActiveScriptParse32_Release
42 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
43 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
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) \
58 ok(expect_ ##func, "unexpected call " #func "\n"); \
59 called_ ## func = TRUE; \
62 #define CHECK_EXPECT(func) \
64 CHECK_EXPECT2(func); \
65 expect_ ## func = FALSE; \
68 #define CHECK_CALLED(func) \
70 ok(called_ ## func, "expected " #func "\n"); \
71 expect_ ## func = called_ ## func = FALSE; \
74 DEFINE_EXPECT(testArgConv
);
76 static const WCHAR testW
[] = {'t','e','s','t',0};
78 static IVariantChangeType
*script_change_type
;
79 static IDispatch
*stored_obj
;
81 #define DISPID_TEST_TESTARGCONV 0x1000
83 static BSTR
a2bstr(const char *str
)
88 len
= MultiByteToWideChar(CP_ACP
, 0, str
, -1, NULL
, 0);
89 ret
= SysAllocStringLen(NULL
, len
-1);
90 MultiByteToWideChar(CP_ACP
, 0, str
, -1, ret
, len
);
95 static int strcmp_wa(LPCWSTR strw
, const char *stra
)
98 WideCharToMultiByte(CP_ACP
, 0, strw
, -1, buf
, sizeof(buf
), 0, 0);
99 return lstrcmpA(buf
, stra
);
104 const char *str_result
;
105 VARIANT_BOOL bool_result
;
107 double double_result
;
110 #define call_change_type(a,b,c,d) _call_change_type(__LINE__,a,b,c,d)
111 static void _call_change_type(unsigned line
, IVariantChangeType
*change_type
, VARIANT
*dst
, VARIANT
*src
, VARTYPE vt
)
116 hres
= IVariantChangeType_ChangeType(change_type
, dst
, src
, 0, vt
);
117 ok_(__FILE__
,line
)(hres
== S_OK
, "ChangeType(%d) failed: %08x\n", vt
, hres
);
118 ok_(__FILE__
,line
)(V_VT(dst
) == vt
, "V_VT(dst) = %d\n", V_VT(dst
));
121 #define change_type_fail(a,b,c,d) _change_type_fail(__LINE__,a,b,c,d)
122 static void _change_type_fail(unsigned line
, IVariantChangeType
*change_type
, VARIANT
*src
, VARTYPE vt
, HRESULT exhres
)
128 hres
= IVariantChangeType_ChangeType(change_type
, &v
, src
, 0, vt
);
129 ok_(__FILE__
,line
)(hres
== exhres
, "ChangeType failed: %08x, expected %08x\n", hres
, exhres
);
132 static void test_change_type(IVariantChangeType
*change_type
, VARIANT
*src
, const conv_results_t
*ex
)
136 call_change_type(change_type
, &v
, src
, VT_I4
);
137 ok(V_I4(&v
) == ex
->int_result
, "V_I4(v) = %d, expected %d\n", V_I4(&v
), ex
->int_result
);
139 call_change_type(change_type
, &v
, src
, VT_BSTR
);
140 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
);
143 call_change_type(change_type
, &v
, src
, VT_BOOL
);
144 ok(V_BOOL(&v
) == ex
->bool_result
, "V_BOOL(v) = %x, expected %x\n", V_BOOL(&v
), ex
->bool_result
);
146 if(ex
->test_double
) {
147 call_change_type(change_type
, &v
, src
, VT_R8
);
148 ok(V_R8(&v
) == ex
->double_result
, "V_R8(v) = %lf, expected %lf\n", V_R8(&v
), ex
->double_result
);
150 call_change_type(change_type
, &v
, src
, VT_R4
);
151 ok(V_R4(&v
) == (float)ex
->double_result
, "V_R4(v) = %f, expected %f\n", V_R4(&v
), (float)ex
->double_result
);
154 if(V_VT(src
) == VT_NULL
)
155 call_change_type(change_type
, &v
, src
, VT_NULL
);
157 change_type_fail(change_type
, src
, VT_NULL
, E_NOTIMPL
);
159 if(V_VT(src
) == VT_EMPTY
)
160 call_change_type(change_type
, &v
, src
, VT_EMPTY
);
162 change_type_fail(change_type
, src
, VT_EMPTY
, E_NOTIMPL
);
164 call_change_type(change_type
, &v
, src
, VT_I2
);
165 ok(V_I2(&v
) == (INT16
)ex
->int_result
, "V_I2(v) = %d, expected %d\n", V_I2(&v
), ex
->int_result
);
168 static void test_change_types(IVariantChangeType
*change_type
, IDispatch
*obj_disp
)
174 static const conv_results_t bool_results
[] = {
175 {0, "false", VARIANT_FALSE
, 1,0.0},
176 {1, "true", VARIANT_TRUE
, 1,1.0}};
177 static const conv_results_t int_results
[] = {
178 {0, "0", VARIANT_FALSE
, 1,0.0},
179 {-100, "-100", VARIANT_TRUE
, 1,-100.0},
180 {0x10010, "65552", VARIANT_TRUE
, 1,65552.0}};
181 static const conv_results_t empty_results
=
182 {0, "undefined", VARIANT_FALSE
, 0,0};
183 static const conv_results_t null_results
=
184 {0, "null", VARIANT_FALSE
, 0,0};
185 static const conv_results_t obj_results
=
186 {10, "strval", VARIANT_TRUE
, 1,10.0};
189 V_BOOL(&v
) = VARIANT_FALSE
;
190 test_change_type(change_type
, &v
, bool_results
);
191 V_BOOL(&v
) = VARIANT_TRUE
;
192 test_change_type(change_type
, &v
, bool_results
+1);
196 test_change_type(change_type
, &v
, int_results
);
198 test_change_type(change_type
, &v
, int_results
+1);
200 test_change_type(change_type
, &v
, int_results
+2);
203 test_change_type(change_type
, &v
, &empty_results
);
206 test_change_type(change_type
, &v
, &null_results
);
208 V_VT(&v
) = VT_DISPATCH
;
209 V_DISPATCH(&v
) = obj_disp
;
210 test_change_type(change_type
, &v
, &obj_results
);
213 V_BOOL(&v
) = VARIANT_FALSE
;
215 hres
= IVariantChangeType_ChangeType(change_type
, &dst
, &v
, 0, VT_I4
);
216 ok(hres
== DISP_E_BADVARTYPE
, "ChangeType failed: %08x, expected DISP_E_BADVARTYPE\n", hres
);
217 ok(V_VT(&dst
) == 0xdead, "V_VT(dst) = %d\n", V_VT(&dst
));
219 /* Test conversion in place */
221 V_BSTR(&v
) = str
= a2bstr("test");
222 hres
= IVariantChangeType_ChangeType(change_type
, &v
, &v
, 0, VT_BSTR
);
223 ok(hres
== S_OK
, "ChangeType failed: %08x\n", hres
);
224 ok(V_VT(&v
) == VT_BSTR
, "V_VT(v) = %d\n", V_VT(&v
));
225 ok(V_BSTR(&v
) != str
, "V_BSTR(v) == str\n");
226 ok(!strcmp_wa(V_BSTR(&v
), "test"), "V_BSTR(v) = %s\n", wine_dbgstr_w(V_BSTR(&v
)));
229 static void test_caller(IServiceProvider
*caller
, IDispatch
*arg_obj
)
231 IVariantChangeType
*change_type
;
234 hres
= IServiceProvider_QueryService(caller
, &SID_VariantConversion
, &IID_IVariantChangeType
, (void**)&change_type
);
235 ok(hres
== S_OK
, "Could not get SID_VariantConversion service: %08x\n", hres
);
237 ok(change_type
== script_change_type
, "change_type != script_change_type\n");
238 test_change_types(change_type
, arg_obj
);
240 IVariantChangeType_Release(change_type
);
243 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
245 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
247 }else if(IsEqualGUID(riid
, &IID_IDispatch
) || IsEqualGUID(riid
, &IID_IDispatchEx
)) {
249 }else if(IsEqualGUID(&IID_IObjectSafety
, riid
)) {
250 ok(0, "Unexpected IID_IObjectSafety query\n");
253 return E_NOINTERFACE
;
259 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
264 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
269 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
271 ok(0, "unexpected call\n");
275 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
276 LCID lcid
, ITypeInfo
**ppTInfo
)
278 ok(0, "unexpected call\n");
282 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
283 LPOLESTR
*rgszNames
, UINT cNames
,
284 LCID lcid
, DISPID
*rgDispId
)
286 ok(0, "unexpected call\n");
290 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
291 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
292 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
294 ok(0, "unexpected call\n");
298 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
300 ok(0, "unexpected call %s %x\n", wine_dbgstr_w(bstrName
), grfdex
);
304 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
306 ok(0, "unexpected call\n");
310 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
312 ok(0, "unexpected call\n");
316 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
318 ok(0, "unexpected call\n");
322 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
324 ok(0, "unexpected call\n");
328 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
330 ok(0, "unexpected call\n");
334 static HRESULT WINAPI
Test_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
336 if(!strcmp_wa(bstrName
, "testArgConv")) {
337 ok(grfdex
== fdexNameCaseSensitive
, "grfdex = %x\n", grfdex
);
338 *pid
= DISPID_TEST_TESTARGCONV
;
345 static HRESULT WINAPI
Test_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
346 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
348 ok(pspCaller
!= NULL
, "pspCaller == NULL\n");
351 case DISPID_TEST_TESTARGCONV
:
352 CHECK_EXPECT(testArgConv
);
354 ok(wFlags
== INVOKE_FUNC
, "wFlags = %x\n", wFlags
);
355 ok(pdp
!= NULL
, "pdp == NULL\n");
356 ok(!pdp
->rgdispidNamedArgs
, "rgdispidNamedArgs != NULL\n");
357 ok(!pvarRes
, "pvarRes != NULL\n");
358 ok(pei
!= NULL
, "pei == NULL\n");
360 ok(pdp
->cArgs
== 1, "cArgs = %d\n", pdp
->cArgs
);
361 ok(V_VT(pdp
->rgvarg
) == VT_DISPATCH
, "V_VT(rgvarg) = %d\n", V_VT(pdp
->rgvarg
));
363 test_caller(pspCaller
, V_DISPATCH(pdp
->rgvarg
));
365 stored_obj
= V_DISPATCH(pdp
->rgvarg
);
366 IDispatch_AddRef(stored_obj
);
370 ok(0, "unexpected call\n");
377 static IDispatchExVtbl testObjVtbl
= {
378 DispatchEx_QueryInterface
,
381 DispatchEx_GetTypeInfoCount
,
382 DispatchEx_GetTypeInfo
,
383 DispatchEx_GetIDsOfNames
,
387 DispatchEx_DeleteMemberByName
,
388 DispatchEx_DeleteMemberByDispID
,
389 DispatchEx_GetMemberProperties
,
390 DispatchEx_GetMemberName
,
391 DispatchEx_GetNextDispID
,
392 DispatchEx_GetNameSpaceParent
395 static IDispatchEx testObj
= { &testObjVtbl
};
397 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
399 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
401 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
405 return E_NOINTERFACE
;
408 IUnknown_AddRef((IUnknown
*)*ppv
);
412 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
417 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
422 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
424 *plcid
= GetUserDefaultLCID();
428 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
429 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
431 ok(dwReturnMask
== SCRIPTINFO_IUNKNOWN
, "unexpected dwReturnMask %x\n", dwReturnMask
);
432 ok(!ppti
, "ppti != NULL\n");
433 ok(!strcmp_wa(pstrName
, "test"), "pstrName = %s\n", wine_dbgstr_w(pstrName
));
435 *ppiunkItem
= (IUnknown
*)&testObj
;
439 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
444 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
445 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
450 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
455 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
460 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
465 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
470 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
471 ActiveScriptSite_QueryInterface
,
472 ActiveScriptSite_AddRef
,
473 ActiveScriptSite_Release
,
474 ActiveScriptSite_GetLCID
,
475 ActiveScriptSite_GetItemInfo
,
476 ActiveScriptSite_GetDocVersionString
,
477 ActiveScriptSite_OnScriptTerminate
,
478 ActiveScriptSite_OnStateChange
,
479 ActiveScriptSite_OnScriptError
,
480 ActiveScriptSite_OnEnterScript
,
481 ActiveScriptSite_OnLeaveScript
484 static IActiveScriptSite ActiveScriptSite
= { &ActiveScriptSiteVtbl
};
486 #define parse_script_a(p,s) _parse_script_a(__LINE__,p,s)
487 static void _parse_script_a(unsigned line
, IActiveScriptParse
*parser
, const char *script
)
492 str
= a2bstr(script
);
493 hres
= IActiveScriptParse_ParseScriptText(parser
, str
, NULL
, NULL
, NULL
, 0, 0, 0, NULL
, NULL
);
495 ok_(__FILE__
,line
)(hres
== S_OK
, "ParseScriptText failed: %08x\n", hres
);
498 static IActiveScriptParse
*create_script(void)
500 IActiveScriptParse
*parser
;
501 IActiveScript
*script
;
504 hres
= CoCreateInstance(&CLSID_JScript
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
505 &IID_IActiveScript
, (void**)&script
);
509 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptParse
, (void**)&parser
);
510 ok(hres
== S_OK
, "Could not get IActiveScriptParse: %08x\n", hres
);
512 hres
= IActiveScriptParse_InitNew(parser
);
513 ok(hres
== S_OK
, "InitNew failed: %08x\n", hres
);
515 hres
= IActiveScript_SetScriptSite(script
, &ActiveScriptSite
);
516 ok(hres
== S_OK
, "SetScriptSite failed: %08x\n", hres
);
518 hres
= IActiveScript_AddNamedItem(script
, testW
,
519 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
520 ok(hres
== S_OK
, "AddNamedItem failed: %08x\n", hres
);
522 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_STARTED
);
523 ok(hres
== S_OK
, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres
);
525 IActiveScript_Release(script
);
530 static void run_scripts(void)
532 IActiveScriptParse
*parser
;
535 parser
= create_script();
537 hres
= IActiveScriptParse_QueryInterface(parser
, &IID_IVariantChangeType
, (void**)&script_change_type
);
538 ok(hres
== S_OK
, "Could not get IVariantChangeType iface: %08x\n", hres
);
540 SET_EXPECT(testArgConv
);
541 parse_script_a(parser
,
543 " toString: function() { return 'strval'; },"
544 " valueOf: function() { return 10; }"
546 "testArgConv(obj);");
547 CHECK_CALLED(testArgConv
);
549 test_change_types(script_change_type
, stored_obj
);
550 IDispatch_Release(stored_obj
);
551 IVariantChangeType_Release(script_change_type
);
553 IActiveScriptParse_Release(parser
);
556 static BOOL
check_jscript(void)
558 IActiveScriptProperty
*script_prop
;
559 IActiveScriptParse
*parser
;
563 parser
= create_script();
567 str
= a2bstr("if(!('localeCompare' in String.prototype)) throw 1;");
568 hres
= IActiveScriptParse_ParseScriptText(parser
, str
, NULL
, NULL
, NULL
, 0, 0, 0, NULL
, NULL
);
572 hres
= IActiveScriptParse_QueryInterface(parser
, &IID_IActiveScriptProperty
, (void**)&script_prop
);
573 IActiveScriptParse_Release(parser
);
575 IActiveScriptProperty_Release(script_prop
);
587 win_skip("Broken (too old) jscript\n");