2 * Copyright 2008 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
28 #include "wine/test.h"
30 DEFINE_GUID(GUID_NULL
,0,0,0,0,0,0,0,0,0,0,0);
32 static const CLSID CLSID_JScript
=
33 {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
35 #define DEFINE_EXPECT(func) \
36 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
38 #define SET_EXPECT(func) \
39 expect_ ## func = TRUE
41 #define SET_CALLED(func) \
42 called_ ## func = TRUE
44 #define CHECK_EXPECT2(func) \
46 ok(expect_ ##func, "unexpected call " #func "\n"); \
47 called_ ## func = TRUE; \
50 #define CHECK_EXPECT(func) \
52 CHECK_EXPECT2(func); \
53 expect_ ## func = FALSE; \
56 #define CHECK_CALLED(func) \
58 ok(called_ ## func, "expected " #func "\n"); \
59 expect_ ## func = called_ ## func = FALSE; \
62 DEFINE_EXPECT(GetLCID
);
63 DEFINE_EXPECT(OnStateChange_STARTED
);
64 DEFINE_EXPECT(OnStateChange_CONNECTED
);
65 DEFINE_EXPECT(OnStateChange_DISCONNECTED
);
66 DEFINE_EXPECT(OnStateChange_CLOSED
);
67 DEFINE_EXPECT(OnStateChange_INITIALIZED
);
68 DEFINE_EXPECT(OnEnterScript
);
69 DEFINE_EXPECT(OnLeaveScript
);
71 static BSTR
a2bstr(const char *str
)
76 len
= MultiByteToWideChar(CP_ACP
, 0, str
, -1, NULL
, 0);
77 ret
= SysAllocStringLen(NULL
, len
-1);
78 MultiByteToWideChar(CP_ACP
, 0, str
, -1, ret
, len
);
83 #define test_state(s,ss) _test_state(__LINE__,s,ss)
84 static void _test_state(unsigned line
, IActiveScript
*script
, SCRIPTSTATE exstate
)
86 SCRIPTSTATE state
= -1;
89 hres
= IActiveScript_GetScriptState(script
, &state
);
90 ok_(__FILE__
,line
) (hres
== S_OK
, "GetScriptState failed: %08x\n", hres
);
91 ok_(__FILE__
,line
) (state
== exstate
, "state=%d, expected %d\n", state
, exstate
);
94 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
98 if(IsEqualGUID(&IID_IUnknown
, riid
))
100 else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
))
103 return E_NOINTERFACE
;
105 IUnknown_AddRef((IUnknown
*)*ppv
);
109 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
114 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
119 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
121 CHECK_EXPECT(GetLCID
);
125 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
126 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
128 ok(0, "unexpected call\n");
132 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
134 ok(0, "unexpected call\n");
138 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
139 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
141 ok(0, "unexpected call\n");
145 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
147 switch(ssScriptState
) {
148 case SCRIPTSTATE_STARTED
:
149 CHECK_EXPECT(OnStateChange_STARTED
);
151 case SCRIPTSTATE_CONNECTED
:
152 CHECK_EXPECT(OnStateChange_CONNECTED
);
154 case SCRIPTSTATE_DISCONNECTED
:
155 CHECK_EXPECT(OnStateChange_DISCONNECTED
);
157 case SCRIPTSTATE_CLOSED
:
158 CHECK_EXPECT(OnStateChange_CLOSED
);
160 case SCRIPTSTATE_INITIALIZED
:
161 CHECK_EXPECT(OnStateChange_INITIALIZED
);
164 ok(0, "unexpected call %d\n", ssScriptState
);
170 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
172 ok(0, "unexpected call\n");
176 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
178 CHECK_EXPECT(OnEnterScript
);
182 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
184 CHECK_EXPECT(OnLeaveScript
);
188 #undef ACTSCPSITE_THIS
190 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
191 ActiveScriptSite_QueryInterface
,
192 ActiveScriptSite_AddRef
,
193 ActiveScriptSite_Release
,
194 ActiveScriptSite_GetLCID
,
195 ActiveScriptSite_GetItemInfo
,
196 ActiveScriptSite_GetDocVersionString
,
197 ActiveScriptSite_OnScriptTerminate
,
198 ActiveScriptSite_OnStateChange
,
199 ActiveScriptSite_OnScriptError
,
200 ActiveScriptSite_OnEnterScript
,
201 ActiveScriptSite_OnLeaveScript
204 static IActiveScriptSite ActiveScriptSite
= { &ActiveScriptSiteVtbl
};
206 static void test_script_dispatch(IDispatchEx
*dispex
)
208 DISPPARAMS dp
= {NULL
,NULL
,0,0};
215 str
= a2bstr("ActiveXObject");
216 hres
= IDispatchEx_GetDispID(dispex
, str
, fdexNameCaseSensitive
, &id
);
218 ok(hres
== S_OK
, "GetDispID failed: %08x\n", hres
);
220 str
= a2bstr("Math");
221 hres
= IDispatchEx_GetDispID(dispex
, str
, fdexNameCaseSensitive
, &id
);
223 ok(hres
== S_OK
, "GetDispID failed: %08x\n", hres
);
225 memset(&ei
, 0, sizeof(ei
));
226 hres
= IDispatchEx_InvokeEx(dispex
, id
, 0, DISPATCH_PROPERTYGET
, &dp
, &v
, &ei
, NULL
);
227 ok(hres
== S_OK
, "InvokeEx failed: %08x\n", hres
);
228 ok(V_VT(&v
) == VT_DISPATCH
, "V_VT(v) = %d\n", V_VT(&v
));
229 ok(V_DISPATCH(&v
) != NULL
, "V_DISPATCH(v) = NULL\n");
232 str
= a2bstr("String");
233 hres
= IDispatchEx_GetDispID(dispex
, str
, fdexNameCaseSensitive
, &id
);
235 ok(hres
== S_OK
, "GetDispID failed: %08x\n", hres
);
237 memset(&ei
, 0, sizeof(ei
));
238 hres
= IDispatchEx_InvokeEx(dispex
, id
, 0, DISPATCH_PROPERTYGET
, &dp
, &v
, &ei
, NULL
);
239 ok(hres
== S_OK
, "InvokeEx failed: %08x\n", hres
);
240 ok(V_VT(&v
) == VT_DISPATCH
, "V_VT(v) = %d\n", V_VT(&v
));
241 ok(V_DISPATCH(&v
) != NULL
, "V_DISPATCH(v) = NULL\n");
245 static IDispatchEx
*get_script_dispatch(IActiveScript
*script
)
251 disp
= (void*)0xdeadbeef;
252 hres
= IActiveScript_GetScriptDispatch(script
, NULL
, &disp
);
253 ok(hres
== S_OK
, "GetScriptDispatch failed: %08x\n", hres
);
255 IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
256 IDispatch_Release(disp
);
257 ok(hres
== S_OK
, "Could not get IDispatch iface: %08x\n", hres
);
261 static void test_no_script_dispatch(IActiveScript
*script
)
266 disp
= (void*)0xdeadbeef;
267 hres
= IActiveScript_GetScriptDispatch(script
, NULL
, &disp
);
268 ok(hres
== E_UNEXPECTED
, "hres = %08x, expected E_UNEXPECTED\n", hres
);
269 ok(!disp
, "disp != NULL\n");
272 static void test_safety(IUnknown
*unk
)
274 IObjectSafety
*safety
;
275 DWORD supported
, enabled
;
278 hres
= IUnknown_QueryInterface(unk
, &IID_IObjectSafety
, (void**)&safety
);
279 ok(hres
== S_OK
, "Could not get IObjectSafety: %08x\n", hres
);
283 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_NULL
, &supported
, NULL
);
284 ok(hres
== E_POINTER
, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres
);
285 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_NULL
, NULL
, &enabled
);
286 ok(hres
== E_POINTER
, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres
);
288 supported
= enabled
= 0xdeadbeef;
289 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_NULL
, &supported
, &enabled
);
290 ok(hres
== S_OK
, "GetInterfaceSafetyOptions failed: %08x\n", hres
);
291 ok(supported
== (INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
),
292 "supported=%x\n", supported
);
293 ok(enabled
== INTERFACE_USES_DISPEX
, "enabled=%x\n", enabled
);
295 supported
= enabled
= 0xdeadbeef;
296 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScript
, &supported
, &enabled
);
297 ok(hres
== S_OK
, "GetInterfaceSafetyOptions failed: %08x\n", hres
);
298 ok(supported
== (INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
),
299 "supported=%x\n", supported
);
300 ok(enabled
== INTERFACE_USES_DISPEX
, "enabled=%x\n", enabled
);
302 supported
= enabled
= 0xdeadbeef;
303 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported
, &enabled
);
304 ok(hres
== S_OK
, "GetInterfaceSafetyOptions failed: %08x\n", hres
);
305 ok(supported
== (INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
),
306 "supported=%x\n", supported
);
307 ok(enabled
== INTERFACE_USES_DISPEX
, "enabled=%x\n", enabled
);
309 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
310 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
311 |INTERFACESAFE_FOR_UNTRUSTED_CALLER
,
312 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
313 ok(hres
== E_FAIL
, "SetInterfaceSafetyOptions failed: %08x, expected E_FAIL\n", hres
);
315 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
316 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
,
317 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
318 ok(hres
== S_OK
, "SetInterfaceSafetyOptions failed: %08x\n", hres
);
320 supported
= enabled
= 0xdeadbeef;
321 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported
, &enabled
);
322 ok(hres
== S_OK
, "GetInterfaceSafetyOptions failed: %08x\n", hres
);
323 ok(supported
== (INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
),
324 "supported=%x\n", supported
);
325 ok(enabled
== (INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
),
326 "enabled=%x\n", enabled
);
328 IObjectSafety_Release(safety
);
331 static HRESULT
set_script_prop(IActiveScript
*engine
, DWORD property
, VARIANT
*val
)
333 IActiveScriptProperty
*script_prop
;
336 hres
= IActiveScript_QueryInterface(engine
, &IID_IActiveScriptProperty
,
337 (void**)&script_prop
);
338 ok(hres
== S_OK
, "Could not get IActiveScriptProperty iface: %08x\n", hres
);
340 hres
= IActiveScriptProperty_SetProperty(script_prop
, property
, NULL
, val
);
341 IActiveScriptProperty_Release(script_prop
);
345 static void test_invoke_versioning(IActiveScript
*script
)
351 hres
= set_script_prop(script
, SCRIPTPROP_INVOKEVERSIONING
, &v
);
352 if(hres
== E_NOTIMPL
) {
353 win_skip("SCRIPTPROP_INVOKESTRING not supported\n");
356 ok(hres
== E_INVALIDARG
, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres
);
360 hres
= set_script_prop(script
, SCRIPTPROP_INVOKEVERSIONING
, &v
);
361 ok(hres
== E_INVALIDARG
, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres
);
365 hres
= set_script_prop(script
, SCRIPTPROP_INVOKEVERSIONING
, &v
);
366 ok(hres
== E_INVALIDARG
, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres
);
370 hres
= set_script_prop(script
, SCRIPTPROP_INVOKEVERSIONING
, &v
);
371 ok(hres
== S_OK
, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres
);
374 static void test_jscript(void)
376 IActiveScriptParse
*parse
;
377 IActiveScript
*script
;
383 hres
= CoCreateInstance(&CLSID_JScript
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
384 &IID_IUnknown
, (void**)&unk
);
385 ok(hres
== S_OK
, "CoCreateInstance failed: %08x\n", hres
);
389 hres
= IUnknown_QueryInterface(unk
, &IID_IActiveScript
, (void**)&script
);
390 ok(hres
== S_OK
, "Could not get IActiveScript: %08x\n", hres
);
392 hres
= IUnknown_QueryInterface(unk
, &IID_IActiveScriptParse
, (void**)&parse
);
393 ok(hres
== S_OK
, "Could not get IActiveScriptParse: %08x\n", hres
);
396 IActiveScript_Release(script
);
400 test_state(script
, SCRIPTSTATE_UNINITIALIZED
);
402 test_invoke_versioning(script
);
404 hres
= IActiveScriptParse64_InitNew(parse
);
405 ok(hres
== S_OK
, "InitNew failed: %08x\n", hres
);
407 hres
= IActiveScriptParse64_InitNew(parse
);
408 ok(hres
== E_UNEXPECTED
, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres
);
410 hres
= IActiveScript_SetScriptSite(script
, NULL
);
411 ok(hres
== E_POINTER
, "SetScriptSite failed: %08x, expected E_POINTER\n", hres
);
413 test_state(script
, SCRIPTSTATE_UNINITIALIZED
);
414 test_no_script_dispatch(script
);
417 SET_EXPECT(OnStateChange_INITIALIZED
);
418 hres
= IActiveScript_SetScriptSite(script
, &ActiveScriptSite
);
419 ok(hres
== S_OK
, "SetScriptSite failed: %08x\n", hres
);
420 CHECK_CALLED(GetLCID
);
421 CHECK_CALLED(OnStateChange_INITIALIZED
);
423 test_state(script
, SCRIPTSTATE_INITIALIZED
);
425 hres
= IActiveScript_SetScriptSite(script
, &ActiveScriptSite
);
426 ok(hres
== E_UNEXPECTED
, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres
);
428 dispex
= get_script_dispatch(script
);
429 test_script_dispatch(dispex
);
431 SET_EXPECT(OnStateChange_STARTED
);
432 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_STARTED
);
433 ok(hres
== S_OK
, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres
);
434 CHECK_CALLED(OnStateChange_STARTED
);
436 test_state(script
, SCRIPTSTATE_STARTED
);
438 SET_EXPECT(OnStateChange_CLOSED
);
439 hres
= IActiveScript_Close(script
);
440 ok(hres
== S_OK
, "Close failed: %08x\n", hres
);
441 CHECK_CALLED(OnStateChange_CLOSED
);
443 test_state(script
, SCRIPTSTATE_CLOSED
);
444 test_no_script_dispatch(script
);
445 test_script_dispatch(dispex
);
446 IDispatchEx_Release(dispex
);
448 IUnknown_Release(parse
);
449 IActiveScript_Release(script
);
451 ref
= IUnknown_Release(unk
);
452 ok(!ref
, "ref = %d\n", ref
);
455 static void test_jscript2(void)
457 IActiveScriptParse
*parse
;
458 IActiveScript
*script
;
463 hres
= CoCreateInstance(&CLSID_JScript
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
464 &IID_IUnknown
, (void**)&unk
);
465 ok(hres
== S_OK
, "CoCreateInstance failed: %08x\n", hres
);
469 hres
= IUnknown_QueryInterface(unk
, &IID_IActiveScript
, (void**)&script
);
470 ok(hres
== S_OK
, "Could not get IActiveScript: %08x\n", hres
);
472 hres
= IUnknown_QueryInterface(unk
, &IID_IActiveScriptParse
, (void**)&parse
);
473 ok(hres
== S_OK
, "Could not get IActiveScriptParse: %08x\n", hres
);
476 IActiveScript_Release(script
);
480 test_state(script
, SCRIPTSTATE_UNINITIALIZED
);
483 hres
= IActiveScript_SetScriptSite(script
, &ActiveScriptSite
);
484 ok(hres
== S_OK
, "SetScriptSite failed: %08x\n", hres
);
485 CHECK_CALLED(GetLCID
);
487 test_state(script
, SCRIPTSTATE_UNINITIALIZED
);
489 SET_EXPECT(OnStateChange_INITIALIZED
);
490 hres
= IActiveScriptParse64_InitNew(parse
);
491 ok(hres
== S_OK
, "InitNew failed: %08x\n", hres
);
492 CHECK_CALLED(OnStateChange_INITIALIZED
);
494 hres
= IActiveScriptParse64_InitNew(parse
);
495 ok(hres
== E_UNEXPECTED
, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres
);
497 SET_EXPECT(OnStateChange_CONNECTED
);
498 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_CONNECTED
);
499 ok(hres
== S_OK
, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres
);
500 CHECK_CALLED(OnStateChange_CONNECTED
);
502 test_state(script
, SCRIPTSTATE_CONNECTED
);
504 SET_EXPECT(OnStateChange_DISCONNECTED
);
505 SET_EXPECT(OnStateChange_INITIALIZED
);
506 SET_EXPECT(OnStateChange_CLOSED
);
507 hres
= IActiveScript_Close(script
);
508 ok(hres
== S_OK
, "Close failed: %08x\n", hres
);
509 CHECK_CALLED(OnStateChange_DISCONNECTED
);
510 CHECK_CALLED(OnStateChange_INITIALIZED
);
511 CHECK_CALLED(OnStateChange_CLOSED
);
513 test_state(script
, SCRIPTSTATE_CLOSED
);
514 test_no_script_dispatch(script
);
516 IUnknown_Release(parse
);
517 IActiveScript_Release(script
);
519 ref
= IUnknown_Release(unk
);
520 ok(!ref
, "ref = %d\n", ref
);