kernel32/tests: Init pointers in common way, add some win_skip() while skipping.
[wine/hramrach.git] / dlls / jscript / tests / jscript.c
blobed94a89c0efa01f3dadef4b84dcb259c46a24b3d
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(IActiveScript *script, BOOL initialized)
208 IDispatchEx *dispex;
209 IDispatch *disp;
210 BSTR str;
211 DISPID id;
212 HRESULT hres;
214 disp = (void*)0xdeadbeef;
215 hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
216 if(!initialized) {
217 ok(hres == E_UNEXPECTED, "hres = %08x, expected E_UNEXPECTED\n", hres);
218 ok(!disp, "disp != NULL\n");
219 return;
222 ok(hres == S_OK, "GetScriptDispatch failed: %08x\n", hres);
223 if(FAILED(hres))
224 return;
226 ok(disp != NULL, "disp == NULL\n");
227 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
228 IDispatch_Release(disp);
229 ok(hres == S_OK, "Could not get IDispatchEx interface: %08x\n", hres);
231 str = a2bstr("ActiveXObject");
232 hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id);
233 SysFreeString(str);
234 ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
236 IDispatchEx_Release(dispex);
239 static void test_safety(IUnknown *unk)
241 IObjectSafety *safety;
242 DWORD supported, enabled;
243 HRESULT hres;
245 hres = IUnknown_QueryInterface(unk, &IID_IObjectSafety, (void**)&safety);
246 ok(hres == S_OK, "Could not get IObjectSafety: %08x\n", hres);
247 if(FAILED(hres))
248 return;
250 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, NULL);
251 ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
252 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, NULL, &enabled);
253 ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
255 supported = enabled = 0xdeadbeef;
256 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, &enabled);
257 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
258 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
259 "supported=%x\n", supported);
260 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
262 supported = enabled = 0xdeadbeef;
263 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScript, &supported, &enabled);
264 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
265 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
266 "supported=%x\n", supported);
267 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
269 supported = enabled = 0xdeadbeef;
270 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
271 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
272 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
273 "supported=%x\n", supported);
274 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
276 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
277 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER
278 |INTERFACESAFE_FOR_UNTRUSTED_CALLER,
279 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
280 ok(hres == E_FAIL, "SetInterfaceSafetyOptions failed: %08x, expected E_FAIL\n", hres);
282 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
283 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
284 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
285 ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
287 supported = enabled = 0xdeadbeef;
288 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
289 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
290 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
291 "supported=%x\n", supported);
292 ok(enabled == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
293 "enabled=%x\n", enabled);
295 IObjectSafety_Release(safety);
298 static HRESULT set_script_prop(IActiveScript *engine, DWORD property, VARIANT *val)
300 IActiveScriptProperty *script_prop;
301 HRESULT hres;
303 hres = IActiveScript_QueryInterface(engine, &IID_IActiveScriptProperty,
304 (void**)&script_prop);
305 ok(hres == S_OK, "Could not get IActiveScriptProperty iface: %08x\n", hres);
307 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
308 IActiveScriptProperty_Release(script_prop);
309 return hres;
312 static void test_invoke_versioning(IActiveScript *script)
314 VARIANT v;
315 HRESULT hres;
317 V_VT(&v) = VT_NULL;
318 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
319 if(hres == E_NOTIMPL) {
320 win_skip("SCRIPTPROP_INVOKESTRING not supported\n");
321 return;
323 ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
325 V_VT(&v) = VT_I2;
326 V_I2(&v) = 0;
327 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
328 ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
330 V_VT(&v) = VT_I4;
331 V_I4(&v) = 16;
332 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
333 ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
335 V_VT(&v) = VT_I4;
336 V_I4(&v) = 2;
337 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
338 ok(hres == S_OK, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
341 static void test_jscript(void)
343 IActiveScriptParse *parse;
344 IActiveScript *script;
345 IUnknown *unk;
346 ULONG ref;
347 HRESULT hres;
349 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
350 &IID_IUnknown, (void**)&unk);
351 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
352 if(FAILED(hres))
353 return;
355 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
356 ok(hres == S_OK, "Could not get IActiveScript: %08x\n", hres);
358 hres = IUnknown_QueryInterface(unk, &IID_IActiveScriptParse, (void**)&parse);
359 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
360 if (FAILED(hres))
362 IActiveScript_Release(script);
363 return;
366 test_state(script, SCRIPTSTATE_UNINITIALIZED);
367 test_safety(unk);
368 test_invoke_versioning(script);
370 hres = IActiveScriptParse64_InitNew(parse);
371 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
373 hres = IActiveScriptParse64_InitNew(parse);
374 ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
376 hres = IActiveScript_SetScriptSite(script, NULL);
377 ok(hres == E_POINTER, "SetScriptSite failed: %08x, expected E_POINTER\n", hres);
379 test_state(script, SCRIPTSTATE_UNINITIALIZED);
380 test_script_dispatch(script, FALSE);
382 SET_EXPECT(GetLCID);
383 SET_EXPECT(OnStateChange_INITIALIZED);
384 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
385 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
386 CHECK_CALLED(GetLCID);
387 CHECK_CALLED(OnStateChange_INITIALIZED);
389 test_state(script, SCRIPTSTATE_INITIALIZED);
391 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
392 ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
394 test_script_dispatch(script, TRUE);
396 SET_EXPECT(OnStateChange_STARTED);
397 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
398 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
399 CHECK_CALLED(OnStateChange_STARTED);
401 test_state(script, SCRIPTSTATE_STARTED);
403 SET_EXPECT(OnStateChange_CLOSED);
404 hres = IActiveScript_Close(script);
405 ok(hres == S_OK, "Close failed: %08x\n", hres);
406 CHECK_CALLED(OnStateChange_CLOSED);
408 test_state(script, SCRIPTSTATE_CLOSED);
409 test_script_dispatch(script, FALSE);
411 IUnknown_Release(parse);
412 IActiveScript_Release(script);
414 ref = IUnknown_Release(unk);
415 ok(!ref, "ref = %d\n", ref);
418 static void test_jscript2(void)
420 IActiveScriptParse *parse;
421 IActiveScript *script;
422 IUnknown *unk;
423 ULONG ref;
424 HRESULT hres;
426 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
427 &IID_IUnknown, (void**)&unk);
428 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
429 if(FAILED(hres))
430 return;
432 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
433 ok(hres == S_OK, "Could not get IActiveScript: %08x\n", hres);
435 hres = IUnknown_QueryInterface(unk, &IID_IActiveScriptParse, (void**)&parse);
436 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
437 if (FAILED(hres))
439 IActiveScript_Release(script);
440 return;
443 test_state(script, SCRIPTSTATE_UNINITIALIZED);
445 SET_EXPECT(GetLCID);
446 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
447 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
448 CHECK_CALLED(GetLCID);
450 test_state(script, SCRIPTSTATE_UNINITIALIZED);
452 SET_EXPECT(OnStateChange_INITIALIZED);
453 hres = IActiveScriptParse64_InitNew(parse);
454 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
455 CHECK_CALLED(OnStateChange_INITIALIZED);
457 hres = IActiveScriptParse64_InitNew(parse);
458 ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
460 SET_EXPECT(OnStateChange_CONNECTED);
461 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_CONNECTED);
462 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
463 CHECK_CALLED(OnStateChange_CONNECTED);
465 test_state(script, SCRIPTSTATE_CONNECTED);
467 SET_EXPECT(OnStateChange_DISCONNECTED);
468 SET_EXPECT(OnStateChange_INITIALIZED);
469 SET_EXPECT(OnStateChange_CLOSED);
470 hres = IActiveScript_Close(script);
471 ok(hres == S_OK, "Close failed: %08x\n", hres);
472 CHECK_CALLED(OnStateChange_DISCONNECTED);
473 CHECK_CALLED(OnStateChange_INITIALIZED);
474 CHECK_CALLED(OnStateChange_CLOSED);
476 test_state(script, SCRIPTSTATE_CLOSED);
477 test_script_dispatch(script, FALSE);
479 IUnknown_Release(parse);
480 IActiveScript_Release(script);
482 ref = IUnknown_Release(unk);
483 ok(!ref, "ref = %d\n", ref);
486 START_TEST(jscript)
488 CoInitialize(NULL);
490 test_jscript();
491 test_jscript2();
493 CoUninitialize();