wer: Add new stubbed wer.dll.
[wine/hramrach.git] / dlls / jscript / tests / jscript.c
blobf7f1e30c65259e71597b8fc8514e9c8792f4f847
1 /*
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
19 #define COBJMACROS
20 #define CONST_VTABLE
22 #include <initguid.h>
23 #include <ole2.h>
24 #include <activscp.h>
25 #include <objsafe.h>
26 #include <dispex.h>
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) \
45 do { \
46 ok(expect_ ##func, "unexpected call " #func "\n"); \
47 called_ ## func = TRUE; \
48 }while(0)
50 #define CHECK_EXPECT(func) \
51 do { \
52 CHECK_EXPECT2(func); \
53 expect_ ## func = FALSE; \
54 }while(0)
56 #define CHECK_CALLED(func) \
57 do { \
58 ok(called_ ## func, "expected " #func "\n"); \
59 expect_ ## func = called_ ## func = FALSE; \
60 }while(0)
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)
73 BSTR ret;
74 int len;
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);
80 return ret;
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;
87 HRESULT hres;
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)
96 *ppv = NULL;
98 if(IsEqualGUID(&IID_IUnknown, riid))
99 *ppv = iface;
100 else if(IsEqualGUID(&IID_IActiveScriptSite, riid))
101 *ppv = iface;
102 else
103 return E_NOINTERFACE;
105 IUnknown_AddRef((IUnknown*)*ppv);
106 return S_OK;
109 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
111 return 2;
114 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
116 return 1;
119 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
121 CHECK_EXPECT(GetLCID);
122 return E_NOTIMPL;
125 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
126 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
128 ok(0, "unexpected call\n");
129 return E_NOTIMPL;
132 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
134 ok(0, "unexpected call\n");
135 return E_NOTIMPL;
138 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
139 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
141 ok(0, "unexpected call\n");
142 return E_NOTIMPL;
145 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
147 switch(ssScriptState) {
148 case SCRIPTSTATE_STARTED:
149 CHECK_EXPECT(OnStateChange_STARTED);
150 return S_OK;
151 case SCRIPTSTATE_CONNECTED:
152 CHECK_EXPECT(OnStateChange_CONNECTED);
153 return S_OK;
154 case SCRIPTSTATE_DISCONNECTED:
155 CHECK_EXPECT(OnStateChange_DISCONNECTED);
156 return S_OK;
157 case SCRIPTSTATE_CLOSED:
158 CHECK_EXPECT(OnStateChange_CLOSED);
159 return S_OK;
160 case SCRIPTSTATE_INITIALIZED:
161 CHECK_EXPECT(OnStateChange_INITIALIZED);
162 return S_OK;
163 default:
164 ok(0, "unexpected call %d\n", ssScriptState);
167 return E_NOTIMPL;
170 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
172 ok(0, "unexpected call\n");
173 return E_NOTIMPL;
176 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
178 CHECK_EXPECT(OnEnterScript);
179 return S_OK;
182 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
184 CHECK_EXPECT(OnLeaveScript);
185 return S_OK;
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};
209 EXCEPINFO ei;
210 BSTR str;
211 DISPID id;
212 VARIANT v;
213 HRESULT hres;
215 str = a2bstr("ActiveXObject");
216 hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id);
217 SysFreeString(str);
218 ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
220 str = a2bstr("Math");
221 hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id);
222 SysFreeString(str);
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");
230 VariantClear(&v);
232 str = a2bstr("String");
233 hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id);
234 SysFreeString(str);
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");
242 VariantClear(&v);
245 static IDispatchEx *get_script_dispatch(IActiveScript *script)
247 IDispatchEx *dispex;
248 IDispatch *disp;
249 HRESULT hres;
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);
258 return dispex;
261 static void test_no_script_dispatch(IActiveScript *script)
263 IDispatch *disp;
264 HRESULT hres;
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;
276 HRESULT hres;
278 hres = IUnknown_QueryInterface(unk, &IID_IObjectSafety, (void**)&safety);
279 ok(hres == S_OK, "Could not get IObjectSafety: %08x\n", hres);
280 if(FAILED(hres))
281 return;
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;
334 HRESULT hres;
336 hres = IActiveScript_QueryInterface(engine, &IID_IActiveScriptProperty,
337 (void**)&script_prop);
338 if(FAILED(hres)) {
339 win_skip("IActiveScriptProperty not supported\n");
340 return E_NOTIMPL;
343 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
344 IActiveScriptProperty_Release(script_prop);
345 return hres;
348 static void test_invoke_versioning(IActiveScript *script)
350 VARIANT v;
351 HRESULT hres;
353 V_VT(&v) = VT_NULL;
354 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
355 if(hres == E_NOTIMPL) {
356 win_skip("SCRIPTPROP_INVOKESTRING not supported\n");
357 return;
359 ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
361 V_VT(&v) = VT_I2;
362 V_I2(&v) = 0;
363 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
364 ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
366 V_VT(&v) = VT_I4;
367 V_I4(&v) = 16;
368 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
369 ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
371 V_VT(&v) = VT_I4;
372 V_I4(&v) = 2;
373 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
374 ok(hres == S_OK, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
377 static void test_jscript(void)
379 IActiveScriptParse *parse;
380 IActiveScript *script;
381 IDispatchEx *dispex;
382 IUnknown *unk;
383 ULONG ref;
384 HRESULT hres;
386 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
387 &IID_IUnknown, (void**)&unk);
388 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
389 if(FAILED(hres))
390 return;
392 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
393 ok(hres == S_OK, "Could not get IActiveScript: %08x\n", hres);
395 hres = IUnknown_QueryInterface(unk, &IID_IActiveScriptParse, (void**)&parse);
396 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
397 if (FAILED(hres))
399 IActiveScript_Release(script);
400 return;
403 test_state(script, SCRIPTSTATE_UNINITIALIZED);
404 test_safety(unk);
405 test_invoke_versioning(script);
407 hres = IActiveScriptParse64_InitNew(parse);
408 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
410 hres = IActiveScriptParse64_InitNew(parse);
411 ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
413 hres = IActiveScript_SetScriptSite(script, NULL);
414 ok(hres == E_POINTER, "SetScriptSite failed: %08x, expected E_POINTER\n", hres);
416 test_state(script, SCRIPTSTATE_UNINITIALIZED);
417 test_no_script_dispatch(script);
419 SET_EXPECT(GetLCID);
420 SET_EXPECT(OnStateChange_INITIALIZED);
421 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
422 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
423 CHECK_CALLED(GetLCID);
424 CHECK_CALLED(OnStateChange_INITIALIZED);
426 test_state(script, SCRIPTSTATE_INITIALIZED);
428 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
429 ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
431 dispex = get_script_dispatch(script);
432 test_script_dispatch(dispex);
434 SET_EXPECT(OnStateChange_STARTED);
435 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
436 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
437 CHECK_CALLED(OnStateChange_STARTED);
439 test_state(script, SCRIPTSTATE_STARTED);
441 SET_EXPECT(OnStateChange_CLOSED);
442 hres = IActiveScript_Close(script);
443 ok(hres == S_OK, "Close failed: %08x\n", hres);
444 CHECK_CALLED(OnStateChange_CLOSED);
446 test_state(script, SCRIPTSTATE_CLOSED);
447 test_no_script_dispatch(script);
448 test_script_dispatch(dispex);
449 IDispatchEx_Release(dispex);
451 IUnknown_Release(parse);
452 IActiveScript_Release(script);
454 ref = IUnknown_Release(unk);
455 ok(!ref, "ref = %d\n", ref);
458 static void test_jscript2(void)
460 IActiveScriptParse *parse;
461 IActiveScript *script;
462 IUnknown *unk;
463 ULONG ref;
464 HRESULT hres;
466 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
467 &IID_IUnknown, (void**)&unk);
468 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
469 if(FAILED(hres))
470 return;
472 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
473 ok(hres == S_OK, "Could not get IActiveScript: %08x\n", hres);
475 hres = IUnknown_QueryInterface(unk, &IID_IActiveScriptParse, (void**)&parse);
476 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
477 if (FAILED(hres))
479 IActiveScript_Release(script);
480 return;
483 test_state(script, SCRIPTSTATE_UNINITIALIZED);
485 SET_EXPECT(GetLCID);
486 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
487 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
488 CHECK_CALLED(GetLCID);
490 test_state(script, SCRIPTSTATE_UNINITIALIZED);
492 SET_EXPECT(OnStateChange_INITIALIZED);
493 hres = IActiveScriptParse64_InitNew(parse);
494 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
495 CHECK_CALLED(OnStateChange_INITIALIZED);
497 hres = IActiveScriptParse64_InitNew(parse);
498 ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
500 SET_EXPECT(OnStateChange_CONNECTED);
501 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_CONNECTED);
502 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
503 CHECK_CALLED(OnStateChange_CONNECTED);
505 test_state(script, SCRIPTSTATE_CONNECTED);
507 SET_EXPECT(OnStateChange_DISCONNECTED);
508 SET_EXPECT(OnStateChange_INITIALIZED);
509 SET_EXPECT(OnStateChange_CLOSED);
510 hres = IActiveScript_Close(script);
511 ok(hres == S_OK, "Close failed: %08x\n", hres);
512 CHECK_CALLED(OnStateChange_DISCONNECTED);
513 CHECK_CALLED(OnStateChange_INITIALIZED);
514 CHECK_CALLED(OnStateChange_CLOSED);
516 test_state(script, SCRIPTSTATE_CLOSED);
517 test_no_script_dispatch(script);
519 IUnknown_Release(parse);
520 IActiveScript_Release(script);
522 ref = IUnknown_Release(unk);
523 ok(!ref, "ref = %d\n", ref);
526 START_TEST(jscript)
528 CoInitialize(NULL);
530 test_jscript();
531 test_jscript2();
533 CoUninitialize();