jscript: Added more SetInterfaceSafetyOptions tests and fixes.
[wine] / dlls / jscript / tests / jscript.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #define COBJMACROS
20 #define CONST_VTABLE
21
22 #include <initguid.h>
23 #include <ole2.h>
24 #include <activscp.h>
25 #include <objsafe.h>
26 #include <dispex.h>
27
28 #include "wine/test.h"
29
30 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
31
32 static const CLSID CLSID_JScript =
33     {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
34
35 #define DEFINE_EXPECT(func) \
36     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
37
38 #define SET_EXPECT(func) \
39     expect_ ## func = TRUE
40
41 #define SET_CALLED(func) \
42     called_ ## func = TRUE
43
44 #define CHECK_EXPECT2(func) \
45     do { \
46         ok(expect_ ##func, "unexpected call " #func "\n"); \
47         called_ ## func = TRUE; \
48     }while(0)
49
50 #define CHECK_EXPECT(func) \
51     do { \
52         CHECK_EXPECT2(func); \
53         expect_ ## func = FALSE; \
54     }while(0)
55
56 #define CHECK_CALLED(func) \
57     do { \
58         ok(called_ ## func, "expected " #func "\n"); \
59         expect_ ## func = called_ ## func = FALSE; \
60     }while(0)
61
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);
71
72 static BSTR a2bstr(const char *str)
73 {
74     BSTR ret;
75     int len;
76
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);
80
81     return ret;
82 }
83
84 #define test_state(s,ss) _test_state(__LINE__,s,ss)
85 static void _test_state(unsigned line, IActiveScript *script, SCRIPTSTATE exstate)
86 {
87     SCRIPTSTATE state = -1;
88     HRESULT hres;
89
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);
93 }
94
95 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
96 {
97     *ppv = NULL;
98
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;
105
106     IUnknown_AddRef((IUnknown*)*ppv);
107     return S_OK;
108 }
109
110 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
111 {
112     return 2;
113 }
114
115 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
116 {
117     return 1;
118 }
119
120 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
121 {
122     CHECK_EXPECT(GetLCID);
123     return E_NOTIMPL;
124 }
125
126 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
127         DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
128 {
129     ok(0, "unexpected call\n");
130     return E_NOTIMPL;
131 }
132
133 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
134 {
135     ok(0, "unexpected call\n");
136     return E_NOTIMPL;
137 }
138
139 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
140         const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
141 {
142     ok(0, "unexpected call\n");
143     return E_NOTIMPL;
144 }
145
146 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
147 {
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);
169     }
170
171     return E_NOTIMPL;
172 }
173
174 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
175 {
176     ok(0, "unexpected call\n");
177     return E_NOTIMPL;
178 }
179
180 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
181 {
182     CHECK_EXPECT(OnEnterScript);
183     return S_OK;
184 }
185
186 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
187 {
188     CHECK_EXPECT(OnLeaveScript);
189     return S_OK;
190 }
191
192 #undef ACTSCPSITE_THIS
193
194 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
195     ActiveScriptSite_QueryInterface,
196     ActiveScriptSite_AddRef,
197     ActiveScriptSite_Release,
198     ActiveScriptSite_GetLCID,
199     ActiveScriptSite_GetItemInfo,
200     ActiveScriptSite_GetDocVersionString,
201     ActiveScriptSite_OnScriptTerminate,
202     ActiveScriptSite_OnStateChange,
203     ActiveScriptSite_OnScriptError,
204     ActiveScriptSite_OnEnterScript,
205     ActiveScriptSite_OnLeaveScript
206 };
207
208 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
209
210 static void test_script_dispatch(IDispatchEx *dispex)
211 {
212     DISPPARAMS dp = {NULL,NULL,0,0};
213     EXCEPINFO ei;
214     BSTR str;
215     DISPID id;
216     VARIANT v;
217     HRESULT hres;
218
219     str = a2bstr("ActiveXObject");
220     hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id);
221     SysFreeString(str);
222     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
223
224     str = a2bstr("Math");
225     hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id);
226     SysFreeString(str);
227     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
228
229     memset(&ei, 0, sizeof(ei));
230     hres = IDispatchEx_InvokeEx(dispex, id, 0, DISPATCH_PROPERTYGET, &dp, &v, &ei, NULL);
231     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
232     ok(V_VT(&v) == VT_DISPATCH, "V_VT(v) = %d\n", V_VT(&v));
233     ok(V_DISPATCH(&v) != NULL, "V_DISPATCH(v) = NULL\n");
234     VariantClear(&v);
235
236     str = a2bstr("String");
237     hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id);
238     SysFreeString(str);
239     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
240
241     memset(&ei, 0, sizeof(ei));
242     hres = IDispatchEx_InvokeEx(dispex, id, 0, DISPATCH_PROPERTYGET, &dp, &v, &ei, NULL);
243     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
244     ok(V_VT(&v) == VT_DISPATCH, "V_VT(v) = %d\n", V_VT(&v));
245     ok(V_DISPATCH(&v) != NULL, "V_DISPATCH(v) = NULL\n");
246     VariantClear(&v);
247 }
248
249 static IDispatchEx *get_script_dispatch(IActiveScript *script)
250 {
251     IDispatchEx *dispex;
252     IDispatch *disp;
253     HRESULT hres;
254
255     disp = (void*)0xdeadbeef;
256     hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
257     ok(hres == S_OK, "GetScriptDispatch failed: %08x\n", hres);
258
259     IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
260     IDispatch_Release(disp);
261     ok(hres == S_OK, "Could not get IDispatch iface: %08x\n", hres);
262     return dispex;
263 }
264
265 static void test_no_script_dispatch(IActiveScript *script)
266 {
267     IDispatch *disp;
268     HRESULT hres;
269
270     disp = (void*)0xdeadbeef;
271     hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
272     ok(hres == E_UNEXPECTED, "hres = %08x, expected E_UNEXPECTED\n", hres);
273     ok(!disp, "disp != NULL\n");
274 }
275
276 static void test_safety(IUnknown *unk)
277 {
278     IObjectSafety *safety;
279     DWORD supported, enabled;
280     HRESULT hres;
281
282     hres = IUnknown_QueryInterface(unk, &IID_IObjectSafety, (void**)&safety);
283     ok(hres == S_OK, "Could not get IObjectSafety: %08x\n", hres);
284     if(FAILED(hres))
285         return;
286
287     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, NULL);
288     ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
289     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, NULL, &enabled);
290     ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
291
292     supported = enabled = 0xdeadbeef;
293     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, &enabled);
294     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
295     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
296        "supported=%x\n", supported);
297     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
298
299     supported = enabled = 0xdeadbeef;
300     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScript, &supported, &enabled);
301     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
302     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
303        "supported=%x\n", supported);
304     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
305
306     supported = enabled = 0xdeadbeef;
307     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
308     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
309     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
310        "supported=%x\n", supported);
311     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
312
313     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
314             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER
315                 |INTERFACESAFE_FOR_UNTRUSTED_CALLER,
316             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
317     ok(hres == E_FAIL, "SetInterfaceSafetyOptions failed: %08x, expected E_FAIL\n", hres);
318
319     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
320             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
321             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
322     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
323
324     supported = enabled = 0xdeadbeef;
325     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
326     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
327     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
328        "supported=%x\n", supported);
329     ok(enabled == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
330        "enabled=%x\n", enabled);
331
332     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, INTERFACESAFE_FOR_UNTRUSTED_DATA, 0);
333     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
334
335     supported = enabled = 0xdeadbeef;
336     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
337     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
338     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
339        "supported=%x\n", supported);
340     ok(enabled == (INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER), "enabled=%x\n", enabled);
341
342     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
343             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER, 0);
344     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
345
346     supported = enabled = 0xdeadbeef;
347     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
348     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
349     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
350        "supported=%x\n", supported);
351     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
352
353     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
354             INTERFACE_USES_DISPEX, 0);
355     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
356
357     supported = enabled = 0xdeadbeef;
358     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
359     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
360     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
361        "supported=%x\n", supported);
362     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
363
364     IObjectSafety_Release(safety);
365 }
366
367 static HRESULT set_script_prop(IActiveScript *engine, DWORD property, VARIANT *val)
368 {
369     IActiveScriptProperty *script_prop;
370     HRESULT hres;
371
372     hres = IActiveScript_QueryInterface(engine, &IID_IActiveScriptProperty,
373             (void**)&script_prop);
374     ok(hres == S_OK, "Could not get IActiveScriptProperty: %08x\n", hres);
375     if(FAILED(hres))
376         return hres;
377
378     hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
379     IActiveScriptProperty_Release(script_prop);
380     return hres;
381 }
382
383 static void test_invoke_versioning(IActiveScript *script)
384 {
385     VARIANT v;
386     HRESULT hres;
387
388     V_VT(&v) = VT_NULL;
389     hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
390     if(hres == E_NOTIMPL) {
391         win_skip("SCRIPTPROP_INVOKESTRING not supported\n");
392         return;
393     }
394     ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
395
396     V_VT(&v) = VT_I2;
397     V_I2(&v) = 0;
398     hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
399     ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
400
401     V_VT(&v) = VT_I4;
402     V_I4(&v) = 16;
403     hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
404     ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
405
406     V_VT(&v) = VT_I4;
407     V_I4(&v) = 2;
408     hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
409     ok(hres == S_OK, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
410 }
411
412 static IActiveScript *create_jscript(void)
413 {
414     IActiveScript *ret;
415     HRESULT hres;
416
417     hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
418             &IID_IActiveScript, (void**)&ret);
419     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
420
421     return ret;
422 }
423
424 static void test_jscript(void)
425 {
426     IActiveScriptParse *parse;
427     IActiveScript *script;
428     IDispatchEx *dispex;
429     ULONG ref;
430     HRESULT hres;
431
432     script = create_jscript();
433
434     hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
435     ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
436
437     test_state(script, SCRIPTSTATE_UNINITIALIZED);
438     test_safety((IUnknown*)script);
439     test_invoke_versioning(script);
440
441     hres = IActiveScriptParse64_InitNew(parse);
442     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
443
444     hres = IActiveScriptParse64_InitNew(parse);
445     ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
446
447     hres = IActiveScript_SetScriptSite(script, NULL);
448     ok(hres == E_POINTER, "SetScriptSite failed: %08x, expected E_POINTER\n", hres);
449
450     test_state(script, SCRIPTSTATE_UNINITIALIZED);
451     test_no_script_dispatch(script);
452
453     SET_EXPECT(GetLCID);
454     SET_EXPECT(OnStateChange_INITIALIZED);
455     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
456     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
457     CHECK_CALLED(GetLCID);
458     CHECK_CALLED(OnStateChange_INITIALIZED);
459
460     test_state(script, SCRIPTSTATE_INITIALIZED);
461
462     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
463     ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
464
465     dispex = get_script_dispatch(script);
466     test_script_dispatch(dispex);
467
468     SET_EXPECT(OnStateChange_STARTED);
469     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
470     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
471     CHECK_CALLED(OnStateChange_STARTED);
472
473     test_state(script, SCRIPTSTATE_STARTED);
474
475     SET_EXPECT(OnStateChange_CLOSED);
476     hres = IActiveScript_Close(script);
477     ok(hres == S_OK, "Close failed: %08x\n", hres);
478     CHECK_CALLED(OnStateChange_CLOSED);
479
480     test_state(script, SCRIPTSTATE_CLOSED);
481     test_no_script_dispatch(script);
482     test_script_dispatch(dispex);
483     IDispatchEx_Release(dispex);
484
485     IUnknown_Release(parse);
486
487     ref = IActiveScript_Release(script);
488     ok(!ref, "ref = %d\n", ref);
489 }
490
491 static void test_jscript2(void)
492 {
493     IActiveScriptParse *parse;
494     IActiveScript *script;
495     ULONG ref;
496     HRESULT hres;
497
498     script = create_jscript();
499
500     hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
501     ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
502
503     test_state(script, SCRIPTSTATE_UNINITIALIZED);
504
505     SET_EXPECT(GetLCID);
506     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
507     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
508     CHECK_CALLED(GetLCID);
509
510     test_state(script, SCRIPTSTATE_UNINITIALIZED);
511
512     SET_EXPECT(OnStateChange_INITIALIZED);
513     hres = IActiveScriptParse64_InitNew(parse);
514     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
515     CHECK_CALLED(OnStateChange_INITIALIZED);
516
517     hres = IActiveScriptParse64_InitNew(parse);
518     ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
519
520     SET_EXPECT(OnStateChange_CONNECTED);
521     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_CONNECTED);
522     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
523     CHECK_CALLED(OnStateChange_CONNECTED);
524
525     test_state(script, SCRIPTSTATE_CONNECTED);
526
527     SET_EXPECT(OnStateChange_DISCONNECTED);
528     SET_EXPECT(OnStateChange_INITIALIZED);
529     SET_EXPECT(OnStateChange_CLOSED);
530     hres = IActiveScript_Close(script);
531     ok(hres == S_OK, "Close failed: %08x\n", hres);
532     CHECK_CALLED(OnStateChange_DISCONNECTED);
533     CHECK_CALLED(OnStateChange_INITIALIZED);
534     CHECK_CALLED(OnStateChange_CLOSED);
535
536     test_state(script, SCRIPTSTATE_CLOSED);
537     test_no_script_dispatch(script);
538
539     IUnknown_Release(parse);
540
541     ref = IActiveScript_Release(script);
542     ok(!ref, "ref = %d\n", ref);
543 }
544
545 static void test_jscript_uninitializing(void)
546 {
547     IActiveScriptParse *parse;
548     IActiveScript *script;
549     IDispatchEx *dispex;
550     ULONG ref;
551     HRESULT hres;
552
553     static const WCHAR script_textW[] =
554         {'f','u','n','c','t','i','o','n',' ','f','(',')',' ','{','}',0};
555
556     script = create_jscript();
557
558     hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
559     ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
560
561     test_state(script, SCRIPTSTATE_UNINITIALIZED);
562
563     hres = IActiveScriptParse64_InitNew(parse);
564     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
565
566     SET_EXPECT(GetLCID);
567     SET_EXPECT(OnStateChange_INITIALIZED);
568     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
569     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
570     CHECK_CALLED(GetLCID);
571     CHECK_CALLED(OnStateChange_INITIALIZED);
572
573     test_state(script, SCRIPTSTATE_INITIALIZED);
574
575     hres = IActiveScriptParse64_ParseScriptText(parse, script_textW, NULL, NULL, NULL, 0, 1, 0x42, NULL, NULL);
576     ok(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
577
578     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
579     ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
580
581     SET_EXPECT(OnStateChange_UNINITIALIZED);
582     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
583     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
584     CHECK_CALLED(OnStateChange_UNINITIALIZED);
585
586     test_state(script, SCRIPTSTATE_UNINITIALIZED);
587
588     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
589     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
590
591     SET_EXPECT(GetLCID);
592     SET_EXPECT(OnStateChange_INITIALIZED);
593     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
594     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
595     CHECK_CALLED(GetLCID);
596     CHECK_CALLED(OnStateChange_INITIALIZED);
597
598     SET_EXPECT(OnStateChange_CONNECTED);
599     SET_EXPECT(OnEnterScript);
600     SET_EXPECT(OnLeaveScript);
601     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_CONNECTED);
602     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
603     CHECK_CALLED(OnStateChange_CONNECTED);
604     CHECK_CALLED(OnEnterScript);
605     CHECK_CALLED(OnLeaveScript);
606
607     test_state(script, SCRIPTSTATE_CONNECTED);
608
609     dispex = get_script_dispatch(script);
610     ok(dispex != NULL, "dispex == NULL\n");
611     IDispatchEx_Release(dispex);
612
613     SET_EXPECT(OnStateChange_DISCONNECTED);
614     SET_EXPECT(OnStateChange_INITIALIZED);
615     SET_EXPECT(OnStateChange_UNINITIALIZED);
616     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
617     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
618     CHECK_CALLED(OnStateChange_DISCONNECTED);
619     CHECK_CALLED(OnStateChange_INITIALIZED);
620     CHECK_CALLED(OnStateChange_UNINITIALIZED);
621
622     test_state(script, SCRIPTSTATE_UNINITIALIZED);
623
624     hres = IActiveScript_Close(script);
625     ok(hres == S_OK, "Close failed: %08x\n", hres);
626
627     test_state(script, SCRIPTSTATE_CLOSED);
628
629     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
630     ok(hres == E_UNEXPECTED, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x, expected E_UNEXPECTED\n", hres);
631
632     test_state(script, SCRIPTSTATE_CLOSED);
633
634     IUnknown_Release(parse);
635
636     ref = IActiveScript_Release(script);
637     ok(!ref, "ref = %d\n", ref);
638 }
639
640 static void test_aggregation(void)
641 {
642     IUnknown *unk = (IUnknown*)0xdeadbeef;
643     HRESULT hres;
644
645     hres = CoCreateInstance(&CLSID_JScript, (IUnknown*)0xdeadbeef, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
646             &IID_IUnknown, (void**)&unk);
647     ok(hres == CLASS_E_NOAGGREGATION || broken(E_INVALIDARG) /* win2k */,
648        "CoCreateInstance failed: %08x, expected CLASS_E_NOAGGREGATION\n", hres);
649     ok(!unk, "unk = %p\n", unk);
650 }
651
652 static BOOL check_jscript(void)
653 {
654     IActiveScriptProperty *script_prop;
655     HRESULT hres;
656
657     hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
658             &IID_IActiveScriptProperty, (void**)&script_prop);
659     if(SUCCEEDED(hres))
660         IActiveScriptProperty_Release(script_prop);
661
662     return hres == S_OK;
663 }
664
665 START_TEST(jscript)
666 {
667     CoInitialize(NULL);
668
669     if(check_jscript()) {
670         test_jscript();
671         test_jscript2();
672         test_jscript_uninitializing();
673         test_aggregation();
674     }else {
675         win_skip("Broken engine, probably too old\n");
676     }
677
678     CoUninitialize();
679 }