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 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
88 const WCHAR
*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 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
)
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
)
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
));
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
);
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
);
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
)
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};
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);
191 test_change_type(change_type
, &v
, int_results
);
193 test_change_type(change_type
, &v
, int_results
+1);
195 test_change_type(change_type
, &v
, int_results
+2);
198 test_change_type(change_type
, &v
, &empty_results
);
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
);
208 V_BOOL(&v
) = VARIANT_FALSE
;
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 */
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
)));
225 static void test_caller(IServiceProvider
*caller
, IDispatch
*arg_obj
)
227 IVariantChangeType
*change_type
;
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
)) {
243 }else if(IsEqualGUID(riid
, &IID_IDispatch
) || IsEqualGUID(riid
, &IID_IDispatchEx
)) {
245 }else if(IsEqualGUID(&IID_IObjectSafety
, riid
)) {
246 ok(0, "Unexpected IID_IObjectSafety query\n");
249 return E_NOINTERFACE
;
255 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
260 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
265 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
267 ok(0, "unexpected call\n");
271 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
, LCID lcid
, ITypeInfo
**ppTInfo
)
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");
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");
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
);
298 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
300 ok(0, "unexpected call\n");
304 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
306 ok(0, "unexpected call\n");
310 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
312 ok(0, "unexpected call\n");
316 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
318 ok(0, "unexpected call\n");
322 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
324 ok(0, "unexpected call\n");
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
;
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");
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
);
366 ok(0, "unexpected call\n");
373 static IDispatchExVtbl testObjVtbl
= {
374 DispatchEx_QueryInterface
,
377 DispatchEx_GetTypeInfoCount
,
378 DispatchEx_GetTypeInfo
,
379 DispatchEx_GetIDsOfNames
,
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
)) {
397 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
401 return E_NOINTERFACE
;
404 IUnknown_AddRef((IUnknown
*)*ppv
);
408 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
413 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
418 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
420 *plcid
= GetUserDefaultLCID();
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
;
435 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
440 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
441 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
446 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
451 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
456 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
458 CHECK_EXPECT(OnEnterScript
);
462 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
464 CHECK_EXPECT(OnLeaveScript
);
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
)
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
;
499 hres
= CoCreateInstance(&CLSID_JScript
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
500 &IID_IActiveScript
, (void**)&script
);
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
);
525 static void run_scripts(void)
527 IActiveScriptParse
*parser
;
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
);
539 L
" toString: function() { return 'strval'; },"
540 L
" valueOf: function() { return 10; }"
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
;
559 parser
= create_script();
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
);
570 hres
= IActiveScriptParse_QueryInterface(parser
, &IID_IActiveScriptProperty
, (void**)&script_prop
);
571 IActiveScriptParse_Release(parser
);
573 IActiveScriptProperty_Release(script_prop
);
585 win_skip("Broken (too old) jscript\n");