jscript: Throw type error on invalid delete.
[wine.git] / dlls / jscript / tests / jscript.c
blob152329bc65a46ec4335c45e11c985f9a3afa5d85
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_UNINITIALIZED);
64 DEFINE_EXPECT(OnStateChange_STARTED);
65 DEFINE_EXPECT(OnStateChange_CONNECTED);
66 DEFINE_EXPECT(OnStateChange_DISCONNECTED);
67 DEFINE_EXPECT(OnStateChange_CLOSED);
68 DEFINE_EXPECT(OnStateChange_INITIALIZED);
69 DEFINE_EXPECT(OnEnterScript);
70 DEFINE_EXPECT(OnLeaveScript);
72 static BSTR a2bstr(const char *str)
74 BSTR ret;
75 int len;
77 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
78 ret = SysAllocStringLen(NULL, len-1);
79 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
81 return ret;
84 #define test_state(s,ss) _test_state(__LINE__,s,ss)
85 static void _test_state(unsigned line, IActiveScript *script, SCRIPTSTATE exstate)
87 SCRIPTSTATE state = -1;
88 HRESULT hres;
90 hres = IActiveScript_GetScriptState(script, &state);
91 ok_(__FILE__,line) (hres == S_OK, "GetScriptState failed: %08x\n", hres);
92 ok_(__FILE__,line) (state == exstate, "state=%d, expected %d\n", state, exstate);
95 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
97 *ppv = NULL;
99 if(IsEqualGUID(&IID_IUnknown, riid))
100 *ppv = iface;
101 else if(IsEqualGUID(&IID_IActiveScriptSite, riid))
102 *ppv = iface;
103 else
104 return E_NOINTERFACE;
106 IUnknown_AddRef((IUnknown*)*ppv);
107 return S_OK;
110 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
112 return 2;
115 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
117 return 1;
120 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
122 CHECK_EXPECT(GetLCID);
123 return E_NOTIMPL;
126 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
127 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
129 ok(0, "unexpected call\n");
130 return E_NOTIMPL;
133 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
135 ok(0, "unexpected call\n");
136 return E_NOTIMPL;
139 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
140 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
142 ok(0, "unexpected call\n");
143 return E_NOTIMPL;
146 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
148 switch(ssScriptState) {
149 case SCRIPTSTATE_UNINITIALIZED:
150 CHECK_EXPECT(OnStateChange_UNINITIALIZED);
151 return S_OK;
152 case SCRIPTSTATE_STARTED:
153 CHECK_EXPECT(OnStateChange_STARTED);
154 return S_OK;
155 case SCRIPTSTATE_CONNECTED:
156 CHECK_EXPECT(OnStateChange_CONNECTED);
157 return S_OK;
158 case SCRIPTSTATE_DISCONNECTED:
159 CHECK_EXPECT(OnStateChange_DISCONNECTED);
160 return S_OK;
161 case SCRIPTSTATE_CLOSED:
162 CHECK_EXPECT(OnStateChange_CLOSED);
163 return S_OK;
164 case SCRIPTSTATE_INITIALIZED:
165 CHECK_EXPECT(OnStateChange_INITIALIZED);
166 return S_OK;
167 default:
168 ok(0, "unexpected call %d\n", ssScriptState);
171 return E_NOTIMPL;
174 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
176 ok(0, "unexpected call\n");
177 return E_NOTIMPL;
180 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
182 CHECK_EXPECT(OnEnterScript);
183 return S_OK;
186 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
188 CHECK_EXPECT(OnLeaveScript);
189 return S_OK;
192 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
193 ActiveScriptSite_QueryInterface,
194 ActiveScriptSite_AddRef,
195 ActiveScriptSite_Release,
196 ActiveScriptSite_GetLCID,
197 ActiveScriptSite_GetItemInfo,
198 ActiveScriptSite_GetDocVersionString,
199 ActiveScriptSite_OnScriptTerminate,
200 ActiveScriptSite_OnStateChange,
201 ActiveScriptSite_OnScriptError,
202 ActiveScriptSite_OnEnterScript,
203 ActiveScriptSite_OnLeaveScript
206 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
208 static void test_script_dispatch(IDispatchEx *dispex)
210 DISPPARAMS dp = {NULL,NULL,0,0};
211 EXCEPINFO ei;
212 BSTR str;
213 DISPID id;
214 VARIANT v;
215 HRESULT hres;
217 str = a2bstr("ActiveXObject");
218 hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id);
219 SysFreeString(str);
220 ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
222 str = a2bstr("Math");
223 hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id);
224 SysFreeString(str);
225 ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
227 memset(&ei, 0, sizeof(ei));
228 hres = IDispatchEx_InvokeEx(dispex, id, 0, DISPATCH_PROPERTYGET, &dp, &v, &ei, NULL);
229 ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
230 ok(V_VT(&v) == VT_DISPATCH, "V_VT(v) = %d\n", V_VT(&v));
231 ok(V_DISPATCH(&v) != NULL, "V_DISPATCH(v) = NULL\n");
232 VariantClear(&v);
234 str = a2bstr("String");
235 hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id);
236 SysFreeString(str);
237 ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
239 memset(&ei, 0, sizeof(ei));
240 hres = IDispatchEx_InvokeEx(dispex, id, 0, DISPATCH_PROPERTYGET, &dp, &v, &ei, NULL);
241 ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
242 ok(V_VT(&v) == VT_DISPATCH, "V_VT(v) = %d\n", V_VT(&v));
243 ok(V_DISPATCH(&v) != NULL, "V_DISPATCH(v) = NULL\n");
244 VariantClear(&v);
247 static IDispatchEx *get_script_dispatch(IActiveScript *script)
249 IDispatchEx *dispex;
250 IDispatch *disp;
251 HRESULT hres;
253 disp = (void*)0xdeadbeef;
254 hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
255 ok(hres == S_OK, "GetScriptDispatch failed: %08x\n", hres);
257 IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
258 IDispatch_Release(disp);
259 ok(hres == S_OK, "Could not get IDispatch iface: %08x\n", hres);
260 return dispex;
263 static void test_no_script_dispatch(IActiveScript *script)
265 IDispatch *disp;
266 HRESULT hres;
268 disp = (void*)0xdeadbeef;
269 hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
270 ok(hres == E_UNEXPECTED, "hres = %08x, expected E_UNEXPECTED\n", hres);
271 ok(!disp, "disp != NULL\n");
274 static void test_safety(IUnknown *unk)
276 IObjectSafety *safety;
277 DWORD supported, enabled;
278 HRESULT hres;
280 hres = IUnknown_QueryInterface(unk, &IID_IObjectSafety, (void**)&safety);
281 ok(hres == S_OK, "Could not get IObjectSafety: %08x\n", hres);
282 if(FAILED(hres))
283 return;
285 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, NULL);
286 ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
287 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, NULL, &enabled);
288 ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
290 supported = enabled = 0xdeadbeef;
291 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, &enabled);
292 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
293 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
294 "supported=%x\n", supported);
295 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
297 supported = enabled = 0xdeadbeef;
298 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScript, &supported, &enabled);
299 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
300 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
301 "supported=%x\n", supported);
302 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
304 supported = enabled = 0xdeadbeef;
305 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
306 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
307 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
308 "supported=%x\n", supported);
309 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
311 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
312 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER
313 |INTERFACESAFE_FOR_UNTRUSTED_CALLER,
314 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
315 ok(hres == E_FAIL, "SetInterfaceSafetyOptions failed: %08x, expected E_FAIL\n", hres);
317 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
318 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
319 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
320 ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
322 supported = enabled = 0xdeadbeef;
323 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
324 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
325 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
326 "supported=%x\n", supported);
327 ok(enabled == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
328 "enabled=%x\n", enabled);
330 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, INTERFACESAFE_FOR_UNTRUSTED_DATA, 0);
331 ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
333 supported = enabled = 0xdeadbeef;
334 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
335 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
336 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
337 "supported=%x\n", supported);
338 ok(enabled == (INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER), "enabled=%x\n", enabled);
340 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
341 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER, 0);
342 ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
344 supported = enabled = 0xdeadbeef;
345 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
346 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
347 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
348 "supported=%x\n", supported);
349 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
351 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
352 INTERFACE_USES_DISPEX, 0);
353 ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
355 supported = enabled = 0xdeadbeef;
356 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
357 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
358 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
359 "supported=%x\n", supported);
360 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
362 IObjectSafety_Release(safety);
365 static HRESULT set_script_prop(IActiveScript *engine, DWORD property, VARIANT *val)
367 IActiveScriptProperty *script_prop;
368 HRESULT hres;
370 hres = IActiveScript_QueryInterface(engine, &IID_IActiveScriptProperty,
371 (void**)&script_prop);
372 ok(hres == S_OK, "Could not get IActiveScriptProperty: %08x\n", hres);
373 if(FAILED(hres))
374 return hres;
376 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
377 IActiveScriptProperty_Release(script_prop);
378 return hres;
381 static void test_invoke_versioning(IActiveScript *script)
383 VARIANT v;
384 HRESULT hres;
386 V_VT(&v) = VT_NULL;
387 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
388 if(hres == E_NOTIMPL) {
389 win_skip("SCRIPTPROP_INVOKESTRING not supported\n");
390 return;
392 ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
394 V_VT(&v) = VT_I2;
395 V_I2(&v) = 0;
396 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
397 ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
399 V_VT(&v) = VT_I4;
400 V_I4(&v) = 16;
401 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
402 ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
404 V_VT(&v) = VT_I4;
405 V_I4(&v) = 2;
406 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
407 ok(hres == S_OK, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
410 static IActiveScript *create_jscript(void)
412 IActiveScript *ret;
413 HRESULT hres;
415 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
416 &IID_IActiveScript, (void**)&ret);
417 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
419 return ret;
422 static void test_jscript(void)
424 IActiveScriptParse *parse;
425 IActiveScript *script;
426 IDispatchEx *dispex;
427 ULONG ref;
428 HRESULT hres;
430 script = create_jscript();
432 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
433 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
435 test_state(script, SCRIPTSTATE_UNINITIALIZED);
436 test_safety((IUnknown*)script);
437 test_invoke_versioning(script);
439 hres = IActiveScriptParse64_InitNew(parse);
440 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
442 hres = IActiveScriptParse64_InitNew(parse);
443 ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
445 hres = IActiveScript_SetScriptSite(script, NULL);
446 ok(hres == E_POINTER, "SetScriptSite failed: %08x, expected E_POINTER\n", hres);
448 test_state(script, SCRIPTSTATE_UNINITIALIZED);
449 test_no_script_dispatch(script);
451 SET_EXPECT(GetLCID);
452 SET_EXPECT(OnStateChange_INITIALIZED);
453 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
454 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
455 CHECK_CALLED(GetLCID);
456 CHECK_CALLED(OnStateChange_INITIALIZED);
458 test_state(script, SCRIPTSTATE_INITIALIZED);
460 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
461 ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
463 dispex = get_script_dispatch(script);
464 test_script_dispatch(dispex);
466 SET_EXPECT(OnStateChange_STARTED);
467 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
468 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
469 CHECK_CALLED(OnStateChange_STARTED);
471 test_state(script, SCRIPTSTATE_STARTED);
473 SET_EXPECT(OnStateChange_CLOSED);
474 hres = IActiveScript_Close(script);
475 ok(hres == S_OK, "Close failed: %08x\n", hres);
476 CHECK_CALLED(OnStateChange_CLOSED);
478 test_state(script, SCRIPTSTATE_CLOSED);
479 test_no_script_dispatch(script);
480 test_script_dispatch(dispex);
481 IDispatchEx_Release(dispex);
483 IUnknown_Release(parse);
485 ref = IActiveScript_Release(script);
486 ok(!ref, "ref = %d\n", ref);
489 static void test_jscript2(void)
491 IActiveScriptParse *parse;
492 IActiveScript *script;
493 ULONG ref;
494 HRESULT hres;
496 script = create_jscript();
498 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
499 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
501 test_state(script, SCRIPTSTATE_UNINITIALIZED);
503 SET_EXPECT(GetLCID);
504 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
505 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
506 CHECK_CALLED(GetLCID);
508 test_state(script, SCRIPTSTATE_UNINITIALIZED);
510 SET_EXPECT(OnStateChange_INITIALIZED);
511 hres = IActiveScriptParse64_InitNew(parse);
512 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
513 CHECK_CALLED(OnStateChange_INITIALIZED);
515 hres = IActiveScriptParse64_InitNew(parse);
516 ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
518 SET_EXPECT(OnStateChange_CONNECTED);
519 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_CONNECTED);
520 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
521 CHECK_CALLED(OnStateChange_CONNECTED);
523 test_state(script, SCRIPTSTATE_CONNECTED);
525 SET_EXPECT(OnStateChange_DISCONNECTED);
526 SET_EXPECT(OnStateChange_INITIALIZED);
527 SET_EXPECT(OnStateChange_CLOSED);
528 hres = IActiveScript_Close(script);
529 ok(hres == S_OK, "Close failed: %08x\n", hres);
530 CHECK_CALLED(OnStateChange_DISCONNECTED);
531 CHECK_CALLED(OnStateChange_INITIALIZED);
532 CHECK_CALLED(OnStateChange_CLOSED);
534 test_state(script, SCRIPTSTATE_CLOSED);
535 test_no_script_dispatch(script);
537 IUnknown_Release(parse);
539 ref = IActiveScript_Release(script);
540 ok(!ref, "ref = %d\n", ref);
543 static void test_jscript_uninitializing(void)
545 IActiveScriptParse *parse;
546 IActiveScript *script;
547 IDispatchEx *dispex;
548 ULONG ref;
549 HRESULT hres;
551 static const WCHAR script_textW[] =
552 {'f','u','n','c','t','i','o','n',' ','f','(',')',' ','{','}',0};
554 script = create_jscript();
556 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
557 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
559 test_state(script, SCRIPTSTATE_UNINITIALIZED);
561 hres = IActiveScriptParse64_InitNew(parse);
562 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
564 SET_EXPECT(GetLCID);
565 SET_EXPECT(OnStateChange_INITIALIZED);
566 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
567 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
568 CHECK_CALLED(GetLCID);
569 CHECK_CALLED(OnStateChange_INITIALIZED);
571 test_state(script, SCRIPTSTATE_INITIALIZED);
573 hres = IActiveScriptParse64_ParseScriptText(parse, script_textW, NULL, NULL, NULL, 0, 1, 0x42, NULL, NULL);
574 ok(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
576 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
577 ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
579 SET_EXPECT(OnStateChange_UNINITIALIZED);
580 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
581 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
582 CHECK_CALLED(OnStateChange_UNINITIALIZED);
584 test_state(script, SCRIPTSTATE_UNINITIALIZED);
586 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
587 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
589 SET_EXPECT(GetLCID);
590 SET_EXPECT(OnStateChange_INITIALIZED);
591 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
592 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
593 CHECK_CALLED(GetLCID);
594 CHECK_CALLED(OnStateChange_INITIALIZED);
596 SET_EXPECT(OnStateChange_CONNECTED);
597 SET_EXPECT(OnEnterScript);
598 SET_EXPECT(OnLeaveScript);
599 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_CONNECTED);
600 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
601 CHECK_CALLED(OnStateChange_CONNECTED);
602 CHECK_CALLED(OnEnterScript);
603 CHECK_CALLED(OnLeaveScript);
605 test_state(script, SCRIPTSTATE_CONNECTED);
607 dispex = get_script_dispatch(script);
608 ok(dispex != NULL, "dispex == NULL\n");
609 IDispatchEx_Release(dispex);
611 SET_EXPECT(OnStateChange_DISCONNECTED);
612 SET_EXPECT(OnStateChange_INITIALIZED);
613 SET_EXPECT(OnStateChange_UNINITIALIZED);
614 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
615 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
616 CHECK_CALLED(OnStateChange_DISCONNECTED);
617 CHECK_CALLED(OnStateChange_INITIALIZED);
618 CHECK_CALLED(OnStateChange_UNINITIALIZED);
620 test_state(script, SCRIPTSTATE_UNINITIALIZED);
622 hres = IActiveScript_Close(script);
623 ok(hres == S_OK, "Close failed: %08x\n", hres);
625 test_state(script, SCRIPTSTATE_CLOSED);
627 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
628 ok(hres == E_UNEXPECTED, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x, expected E_UNEXPECTED\n", hres);
630 test_state(script, SCRIPTSTATE_CLOSED);
632 IUnknown_Release(parse);
634 ref = IActiveScript_Release(script);
635 ok(!ref, "ref = %d\n", ref);
638 static void test_aggregation(void)
640 IUnknown *unk = (IUnknown*)0xdeadbeef;
641 HRESULT hres;
643 hres = CoCreateInstance(&CLSID_JScript, (IUnknown*)0xdeadbeef, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
644 &IID_IUnknown, (void**)&unk);
645 ok(hres == CLASS_E_NOAGGREGATION || broken(E_INVALIDARG) /* win2k */,
646 "CoCreateInstance failed: %08x, expected CLASS_E_NOAGGREGATION\n", hres);
647 ok(!unk, "unk = %p\n", unk);
650 static BOOL check_jscript(void)
652 IActiveScriptProperty *script_prop;
653 HRESULT hres;
655 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
656 &IID_IActiveScriptProperty, (void**)&script_prop);
657 if(SUCCEEDED(hres))
658 IActiveScriptProperty_Release(script_prop);
660 return hres == S_OK;
663 START_TEST(jscript)
665 CoInitialize(NULL);
667 if(check_jscript()) {
668 test_jscript();
669 test_jscript2();
670 test_jscript_uninitializing();
671 test_aggregation();
672 }else {
673 win_skip("Broken engine, probably too old\n");
676 CoUninitialize();