windowscodecs: Remove unneeded address-of operator from array name.
[wine] / dlls / vbscript / tests / vbscript.c
1 /*
2  * Copyright 2011 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 #ifdef _WIN64
31
32 #define IActiveScriptParse_QueryInterface IActiveScriptParse64_QueryInterface
33 #define IActiveScriptParse_Release IActiveScriptParse64_Release
34 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
35 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
36 #define IActiveScriptParseProcedure2_Release \
37     IActiveScriptParseProcedure2_64_Release
38
39 #else
40
41 #define IActiveScriptParse_QueryInterface IActiveScriptParse32_QueryInterface
42 #define IActiveScriptParse_Release IActiveScriptParse32_Release
43 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
44 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
45 #define IActiveScriptParseProcedure2_Release \
46     IActiveScriptParseProcedure2_32_Release
47
48 #endif
49
50 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
51
52 #define DEFINE_EXPECT(func) \
53     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
54
55 #define SET_EXPECT(func) \
56     expect_ ## func = TRUE
57
58 #define CHECK_EXPECT2(func) \
59     do { \
60         ok(expect_ ##func, "unexpected call " #func "\n"); \
61         called_ ## func = TRUE; \
62     }while(0)
63
64 #define CHECK_EXPECT(func) \
65     do { \
66         CHECK_EXPECT2(func); \
67         expect_ ## func = FALSE; \
68     }while(0)
69
70 #define CHECK_CALLED(func) \
71     do { \
72         ok(called_ ## func, "expected " #func "\n"); \
73         expect_ ## func = called_ ## func = FALSE; \
74     }while(0)
75
76 DEFINE_EXPECT(GetLCID);
77 DEFINE_EXPECT(OnStateChange_UNINITIALIZED);
78 DEFINE_EXPECT(OnStateChange_STARTED);
79 DEFINE_EXPECT(OnStateChange_CONNECTED);
80 DEFINE_EXPECT(OnStateChange_DISCONNECTED);
81 DEFINE_EXPECT(OnStateChange_CLOSED);
82 DEFINE_EXPECT(OnStateChange_INITIALIZED);
83 DEFINE_EXPECT(OnEnterScript);
84 DEFINE_EXPECT(OnLeaveScript);
85
86 DEFINE_GUID(CLSID_VBScript, 0xb54f3741, 0x5b07, 0x11cf, 0xa4,0xb0, 0x00,0xaa,0x00,0x4a,0x55,0xe8);
87
88 static BSTR a2bstr(const char *str)
89 {
90     BSTR ret;
91     int len;
92
93     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
94     ret = SysAllocStringLen(NULL, len-1);
95     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
96
97     return ret;
98 }
99
100 #define test_state(s,ss) _test_state(__LINE__,s,ss)
101 static void _test_state(unsigned line, IActiveScript *script, SCRIPTSTATE exstate)
102 {
103     SCRIPTSTATE state = -1;
104     HRESULT hres;
105
106     hres = IActiveScript_GetScriptState(script, &state);
107     ok_(__FILE__,line) (hres == S_OK, "GetScriptState failed: %08x\n", hres);
108     ok_(__FILE__,line) (state == exstate, "state=%d, expected %d\n", state, exstate);
109 }
110
111 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
112 {
113     *ppv = NULL;
114
115     if(IsEqualGUID(&IID_IUnknown, riid))
116         *ppv = iface;
117     else if(IsEqualGUID(&IID_IActiveScriptSite, riid))
118         *ppv = iface;
119     else
120         return E_NOINTERFACE;
121
122     IUnknown_AddRef((IUnknown*)*ppv);
123     return S_OK;
124 }
125
126 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
127 {
128     return 2;
129 }
130
131 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
132 {
133     return 1;
134 }
135
136 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
137 {
138     CHECK_EXPECT(GetLCID);
139     return E_NOTIMPL;
140 }
141
142 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
143         DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
144 {
145     ok(0, "unexpected call\n");
146     return E_NOTIMPL;
147 }
148
149 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
150 {
151     ok(0, "unexpected call\n");
152     return E_NOTIMPL;
153 }
154
155 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
156         const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
157 {
158     ok(0, "unexpected call\n");
159     return E_NOTIMPL;
160 }
161
162 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
163 {
164     switch(ssScriptState) {
165     case SCRIPTSTATE_UNINITIALIZED:
166         CHECK_EXPECT(OnStateChange_UNINITIALIZED);
167         return S_OK;
168     case SCRIPTSTATE_STARTED:
169         CHECK_EXPECT(OnStateChange_STARTED);
170         return S_OK;
171     case SCRIPTSTATE_CONNECTED:
172         CHECK_EXPECT(OnStateChange_CONNECTED);
173         return S_OK;
174     case SCRIPTSTATE_DISCONNECTED:
175         CHECK_EXPECT(OnStateChange_DISCONNECTED);
176         return S_OK;
177     case SCRIPTSTATE_CLOSED:
178         CHECK_EXPECT(OnStateChange_CLOSED);
179         return S_OK;
180     case SCRIPTSTATE_INITIALIZED:
181         CHECK_EXPECT(OnStateChange_INITIALIZED);
182         return S_OK;
183     default:
184         ok(0, "unexpected call %d\n", ssScriptState);
185     }
186
187     return E_NOTIMPL;
188 }
189
190 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
191 {
192     ok(0, "unexpected call\n");
193     return E_NOTIMPL;
194 }
195
196 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
197 {
198     CHECK_EXPECT(OnEnterScript);
199     return S_OK;
200 }
201
202 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
203 {
204     CHECK_EXPECT(OnLeaveScript);
205     return S_OK;
206 }
207
208 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
209     ActiveScriptSite_QueryInterface,
210     ActiveScriptSite_AddRef,
211     ActiveScriptSite_Release,
212     ActiveScriptSite_GetLCID,
213     ActiveScriptSite_GetItemInfo,
214     ActiveScriptSite_GetDocVersionString,
215     ActiveScriptSite_OnScriptTerminate,
216     ActiveScriptSite_OnStateChange,
217     ActiveScriptSite_OnScriptError,
218     ActiveScriptSite_OnEnterScript,
219     ActiveScriptSite_OnLeaveScript
220 };
221
222 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
223
224 static void test_safety(IActiveScript *script)
225 {
226     IObjectSafety *safety;
227     DWORD supported, enabled;
228     HRESULT hres;
229
230     hres = IActiveScript_QueryInterface(script, &IID_IObjectSafety, (void**)&safety);
231     ok(hres == S_OK, "Could not get IObjectSafety: %08x\n", hres);
232     if(FAILED(hres))
233         return;
234
235     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, NULL);
236     ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
237     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, NULL, &enabled);
238     ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
239
240     supported = enabled = 0xdeadbeef;
241     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, &enabled);
242     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
243     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
244        "supported=%x\n", supported);
245     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
246
247     supported = enabled = 0xdeadbeef;
248     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScript, &supported, &enabled);
249     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
250     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
251        "supported=%x\n", supported);
252     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
253
254     supported = enabled = 0xdeadbeef;
255     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
256     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
257     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
258        "supported=%x\n", supported);
259     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
260
261     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
262             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER
263                 |INTERFACESAFE_FOR_UNTRUSTED_CALLER,
264             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
265     ok(hres == E_FAIL, "SetInterfaceSafetyOptions failed: %08x, expected E_FAIL\n", hres);
266
267     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
268             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
269             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
270     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
271
272     supported = enabled = 0xdeadbeef;
273     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
274     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
275     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
276        "supported=%x\n", supported);
277     ok(enabled == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
278        "enabled=%x\n", enabled);
279
280     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, INTERFACESAFE_FOR_UNTRUSTED_DATA, 0);
281     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
282
283     supported = enabled = 0xdeadbeef;
284     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
285     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
286     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
287        "supported=%x\n", supported);
288     ok(enabled == (INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER), "enabled=%x\n", enabled);
289
290     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
291             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER, 0);
292     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
293
294     supported = enabled = 0xdeadbeef;
295     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
296     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
297     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
298        "supported=%x\n", supported);
299     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
300
301     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
302             INTERFACE_USES_DISPEX, 0);
303     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
304
305     supported = enabled = 0xdeadbeef;
306     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
307     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
308     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
309        "supported=%x\n", supported);
310     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
311
312     IObjectSafety_Release(safety);
313 }
314
315 static IDispatchEx *get_script_dispatch(IActiveScript *script)
316 {
317     IDispatchEx *dispex;
318     IDispatch *disp;
319     HRESULT hres;
320
321     disp = (void*)0xdeadbeef;
322     hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
323     ok(hres == S_OK, "GetScriptDispatch failed: %08x\n", hres);
324     if(FAILED(hres))
325         return NULL;
326
327     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
328     IDispatch_Release(disp);
329     ok(hres == S_OK, "Could not get IDispatchEx iface: %08x\n", hres);
330     return dispex;
331 }
332
333 static void parse_script(IActiveScriptParse *parse, const char *src)
334 {
335     BSTR str;
336     HRESULT hres;
337
338     SET_EXPECT(OnEnterScript);
339     SET_EXPECT(OnLeaveScript);
340
341     str = a2bstr(src);
342     hres = IActiveScriptParse_ParseScriptText(parse, str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
343     SysFreeString(str);
344     ok(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
345
346     CHECK_CALLED(OnEnterScript);
347     CHECK_CALLED(OnLeaveScript);
348 }
349
350 #define get_disp_id(a,b,c,d) _get_disp_id(__LINE__,a,b,c,d)
351 static void _get_disp_id(unsigned line, IDispatchEx *dispex, const char *name, HRESULT exhres, DISPID *id)
352 {
353     DISPID id2;
354     BSTR str;
355     HRESULT hres;
356
357     str = a2bstr(name);
358     hres = IDispatchEx_GetDispID(dispex, str, 0, id);
359     ok_(__FILE__,line)(hres == exhres, "GetDispID(%s) returned %08x, expected %08x\n", name, hres, exhres);
360
361     hres = IDispatchEx_GetIDsOfNames(dispex, &IID_NULL, &str, 1, 0, &id2);
362     SysFreeString(str);
363     ok_(__FILE__,line)(hres == exhres, "GetIDsOfNames(%s) returned %08x, expected %08x\n", name, hres, exhres);
364     ok_(__FILE__,line)(*id == id2, "GetIDsOfNames(%s) id != id2\n", name);
365 }
366
367 static void test_no_script_dispatch(IActiveScript *script)
368 {
369     IDispatch *disp;
370     HRESULT hres;
371
372     disp = (void*)0xdeadbeef;
373     hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
374     ok(hres == E_UNEXPECTED, "hres = %08x, expected E_UNEXPECTED\n", hres);
375     ok(!disp, "disp != NULL\n");
376 }
377
378 static IActiveScript *create_vbscript(void)
379 {
380     IActiveScript *ret;
381     HRESULT hres;
382
383     hres = CoCreateInstance(&CLSID_VBScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
384             &IID_IActiveScript, (void**)&ret);
385     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
386
387     return ret;
388 }
389
390 static void test_scriptdisp(void)
391 {
392     IActiveScriptParse *parser;
393     IDispatchEx *script_disp;
394     IActiveScript *vbscript;
395     DISPID id, id2;
396     DISPPARAMS dp;
397     EXCEPINFO ei;
398     VARIANT v;
399     ULONG ref;
400     HRESULT hres;
401
402     vbscript = create_vbscript();
403
404     hres = IActiveScript_QueryInterface(vbscript, &IID_IActiveScriptParse, (void**)&parser);
405     ok(hres == S_OK, "Could not get IActiveScriptParse iface: %08x\n", hres);
406
407     test_state(vbscript, SCRIPTSTATE_UNINITIALIZED);
408     test_safety(vbscript);
409
410     SET_EXPECT(GetLCID);
411     hres = IActiveScript_SetScriptSite(vbscript, &ActiveScriptSite);
412     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
413     CHECK_CALLED(GetLCID);
414
415     test_state(vbscript, SCRIPTSTATE_UNINITIALIZED);
416
417     SET_EXPECT(OnStateChange_INITIALIZED);
418     hres = IActiveScriptParse_InitNew(parser);
419     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
420     CHECK_CALLED(OnStateChange_INITIALIZED);
421
422     test_state(vbscript, SCRIPTSTATE_INITIALIZED);
423
424     SET_EXPECT(OnStateChange_CONNECTED);
425     hres = IActiveScript_SetScriptState(vbscript, SCRIPTSTATE_CONNECTED);
426     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
427     CHECK_CALLED(OnStateChange_CONNECTED);
428
429     test_state(vbscript, SCRIPTSTATE_CONNECTED);
430
431     script_disp = get_script_dispatch(vbscript);
432
433     id = 100;
434     get_disp_id(script_disp, "LCase", DISP_E_UNKNOWNNAME, &id);
435     ok(id == -1, "id = %d, expected -1\n", id);
436
437     get_disp_id(script_disp, "globalVariable", DISP_E_UNKNOWNNAME, &id);
438     parse_script(parser, "dim globalVariable\nglobalVariable = 3");
439     get_disp_id(script_disp, "globalVariable", S_OK, &id);
440
441     memset(&dp, 0, sizeof(dp));
442     memset(&ei, 0, sizeof(ei));
443     V_VT(&v) = VT_EMPTY;
444     hres = IDispatchEx_InvokeEx(script_disp, id, 0, DISPATCH_PROPERTYGET|DISPATCH_METHOD, &dp, &v, &ei, NULL);
445     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
446     ok(V_VT(&v) == VT_I2, "V_VT(v) = %d\n", V_VT(&v));
447     ok(V_I2(&v) == 3, "V_I2(v) = %d\n", V_I2(&v));
448
449     get_disp_id(script_disp, "globalVariable2", DISP_E_UNKNOWNNAME, &id);
450     parse_script(parser, "globalVariable2 = 4");
451     get_disp_id(script_disp, "globalVariable2", S_OK, &id);
452
453     get_disp_id(script_disp, "globalFunction", DISP_E_UNKNOWNNAME, &id);
454     parse_script(parser, "function globalFunction()\nglobalFunction=5\nend function");
455     get_disp_id(script_disp, "globalFunction", S_OK, &id);
456
457     SET_EXPECT(OnEnterScript);
458     SET_EXPECT(OnLeaveScript);
459
460     memset(&dp, 0, sizeof(dp));
461     memset(&ei, 0, sizeof(ei));
462     V_VT(&v) = VT_EMPTY;
463     hres = IDispatchEx_InvokeEx(script_disp, id, 0, DISPATCH_PROPERTYGET|DISPATCH_METHOD, &dp, &v, &ei, NULL);
464     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
465     ok(V_VT(&v) == VT_I2, "V_VT(v) = %d\n", V_VT(&v));
466     ok(V_I2(&v) == 5, "V_I2(v) = %d\n", V_I2(&v));
467
468     CHECK_CALLED(OnEnterScript);
469     CHECK_CALLED(OnLeaveScript);
470
471     SET_EXPECT(OnEnterScript);
472     SET_EXPECT(OnLeaveScript);
473
474     memset(&dp, 0, sizeof(dp));
475     memset(&ei, 0, sizeof(ei));
476     V_VT(&v) = VT_EMPTY;
477     hres = IDispatchEx_Invoke(script_disp, id, &IID_NULL, 0, DISPATCH_PROPERTYGET|DISPATCH_METHOD, &dp, &v, &ei, NULL);
478     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
479     ok(V_VT(&v) == VT_I2, "V_VT(v) = %d\n", V_VT(&v));
480     ok(V_I2(&v) == 5, "V_I2(v) = %d\n", V_I2(&v));
481
482     CHECK_CALLED(OnEnterScript);
483     CHECK_CALLED(OnLeaveScript);
484
485     get_disp_id(script_disp, "globalSub", DISP_E_UNKNOWNNAME, &id);
486     parse_script(parser, "sub globalSub()\nend sub");
487     get_disp_id(script_disp, "globalSub", S_OK, &id);
488     get_disp_id(script_disp, "globalSub", S_OK, &id2);
489     ok(id == id2, "id != id2\n");
490
491     get_disp_id(script_disp, "constVariable", DISP_E_UNKNOWNNAME, &id);
492     parse_script(parser, "const constVariable = 6");
493     get_disp_id(script_disp, "ConstVariable", S_OK, &id);
494     get_disp_id(script_disp, "Constvariable", S_OK, &id2);
495     ok(id == id2, "id != id2\n");
496
497     IDispatchEx_Release(script_disp);
498
499     IActiveScriptParse_Release(parser);
500
501     SET_EXPECT(OnStateChange_DISCONNECTED);
502     SET_EXPECT(OnStateChange_INITIALIZED);
503     SET_EXPECT(OnStateChange_CLOSED);
504     hres = IActiveScript_Close(vbscript);
505     ok(hres == S_OK, "Close failed: %08x\n", hres);
506     CHECK_CALLED(OnStateChange_DISCONNECTED);
507     CHECK_CALLED(OnStateChange_INITIALIZED);
508     CHECK_CALLED(OnStateChange_CLOSED);
509
510     ref = IActiveScript_Release(vbscript);
511     ok(!ref, "ref = %d\n", ref);
512 }
513
514 static void test_vbscript(void)
515 {
516     IActiveScriptParseProcedure2 *parse_proc;
517     IActiveScriptParse *parser;
518     IActiveScript *vbscript;
519     ULONG ref;
520     HRESULT hres;
521
522     vbscript = create_vbscript();
523
524     hres = IActiveScript_QueryInterface(vbscript, &IID_IActiveScriptParse, (void**)&parser);
525     ok(hres == S_OK, "Could not get IActiveScriptParse iface: %08x\n", hres);
526
527     test_state(vbscript, SCRIPTSTATE_UNINITIALIZED);
528     test_safety(vbscript);
529
530     SET_EXPECT(GetLCID);
531     hres = IActiveScript_SetScriptSite(vbscript, &ActiveScriptSite);
532     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
533     CHECK_CALLED(GetLCID);
534
535     test_state(vbscript, SCRIPTSTATE_UNINITIALIZED);
536
537     SET_EXPECT(OnStateChange_INITIALIZED);
538     hres = IActiveScriptParse_InitNew(parser);
539     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
540     CHECK_CALLED(OnStateChange_INITIALIZED);
541
542     test_state(vbscript, SCRIPTSTATE_INITIALIZED);
543
544     hres = IActiveScriptParse_InitNew(parser);
545     ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
546
547     SET_EXPECT(OnStateChange_CONNECTED);
548     hres = IActiveScript_SetScriptState(vbscript, SCRIPTSTATE_CONNECTED);
549     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
550     CHECK_CALLED(OnStateChange_CONNECTED);
551
552     test_state(vbscript, SCRIPTSTATE_CONNECTED);
553
554     SET_EXPECT(OnStateChange_DISCONNECTED);
555     SET_EXPECT(OnStateChange_INITIALIZED);
556     SET_EXPECT(OnStateChange_CLOSED);
557     hres = IActiveScript_Close(vbscript);
558     ok(hres == S_OK, "Close failed: %08x\n", hres);
559     CHECK_CALLED(OnStateChange_DISCONNECTED);
560     CHECK_CALLED(OnStateChange_INITIALIZED);
561     CHECK_CALLED(OnStateChange_CLOSED);
562
563     test_state(vbscript, SCRIPTSTATE_CLOSED);
564     test_no_script_dispatch(vbscript);
565
566     IActiveScriptParse_Release(parser);
567
568     hres = IActiveScript_QueryInterface(vbscript, &IID_IActiveScriptParseProcedure, (void**)&parse_proc);
569     ok(hres == E_NOINTERFACE, "Got IActiveScriptParseProcedure interface, expected E_NOTIMPL\n");
570
571     hres = IActiveScript_QueryInterface(vbscript, &IID_IActiveScriptParseProcedure2, (void**)&parse_proc);
572     ok(hres == S_OK, "Could not get IActiveScriptParseProcedure2 interface\n");
573     IActiveScriptParseProcedure2_Release(parse_proc);
574
575     ref = IActiveScript_Release(vbscript);
576     ok(!ref, "ref = %d\n", ref);
577 }
578
579 static void test_vbscript_uninitializing(void)
580 {
581     IActiveScriptParse *parse;
582     IActiveScript *script;
583     IDispatchEx *dispex;
584     ULONG ref;
585     HRESULT hres;
586
587     static const WCHAR script_textW[] =
588         {'F','u','n','c','t','i','o','n',' ','f','\n','E','n','d',' ','F','u','n','c','t','i','o','n','\n',0};
589
590     script = create_vbscript();
591
592     hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
593     ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
594
595     test_state(script, SCRIPTSTATE_UNINITIALIZED);
596
597     hres = IActiveScriptParse_InitNew(parse);
598     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
599
600     SET_EXPECT(GetLCID);
601     SET_EXPECT(OnStateChange_INITIALIZED);
602     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
603     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
604     CHECK_CALLED(GetLCID);
605     CHECK_CALLED(OnStateChange_INITIALIZED);
606
607     test_state(script, SCRIPTSTATE_INITIALIZED);
608
609     hres = IActiveScriptParse_ParseScriptText(parse, script_textW, NULL, NULL, NULL, 0, 1, 0x42, NULL, NULL);
610     ok(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
611
612     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
613     ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
614
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_UNINITIALIZED);
619
620     test_state(script, SCRIPTSTATE_UNINITIALIZED);
621
622     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
623     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
624
625     SET_EXPECT(GetLCID);
626     SET_EXPECT(OnStateChange_INITIALIZED);
627     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
628     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
629     CHECK_CALLED(GetLCID);
630     CHECK_CALLED(OnStateChange_INITIALIZED);
631
632     SET_EXPECT(OnStateChange_CONNECTED);
633     SET_EXPECT(OnEnterScript);
634     SET_EXPECT(OnLeaveScript);
635     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_CONNECTED);
636     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
637     CHECK_CALLED(OnStateChange_CONNECTED);
638     CHECK_CALLED(OnEnterScript);
639     CHECK_CALLED(OnLeaveScript);
640
641     test_state(script, SCRIPTSTATE_CONNECTED);
642
643     dispex = get_script_dispatch(script);
644     ok(dispex != NULL, "dispex == NULL\n");
645     if(dispex)
646         IDispatchEx_Release(dispex);
647
648     SET_EXPECT(OnStateChange_DISCONNECTED);
649     SET_EXPECT(OnStateChange_INITIALIZED);
650     SET_EXPECT(OnStateChange_UNINITIALIZED);
651     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
652     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
653     CHECK_CALLED(OnStateChange_DISCONNECTED);
654     CHECK_CALLED(OnStateChange_INITIALIZED);
655     CHECK_CALLED(OnStateChange_UNINITIALIZED);
656
657     test_state(script, SCRIPTSTATE_UNINITIALIZED);
658
659     hres = IActiveScript_Close(script);
660     ok(hres == S_OK, "Close failed: %08x\n", hres);
661
662     test_state(script, SCRIPTSTATE_CLOSED);
663
664     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
665     ok(hres == E_UNEXPECTED, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x, expected E_UNEXPECTED\n", hres);
666
667     test_state(script, SCRIPTSTATE_CLOSED);
668
669     SET_EXPECT(GetLCID);
670     SET_EXPECT(OnStateChange_INITIALIZED);
671     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
672     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
673     CHECK_CALLED(GetLCID);
674     CHECK_CALLED(OnStateChange_INITIALIZED);
675
676     test_state(script, SCRIPTSTATE_INITIALIZED);
677
678     SET_EXPECT(OnStateChange_CLOSED);
679     hres = IActiveScript_Close(script);
680     ok(hres == S_OK, "Close failed: %08x\n", hres);
681     CHECK_CALLED(OnStateChange_CLOSED);
682
683     test_state(script, SCRIPTSTATE_CLOSED);
684
685     IActiveScriptParse_Release(parse);
686
687     ref = IActiveScript_Release(script);
688     ok(!ref, "ref = %d\n", ref);
689 }
690
691 static void test_vbscript_release(void)
692 {
693     IActiveScriptParse *parser;
694     IActiveScript *vbscript;
695     ULONG ref;
696     HRESULT hres;
697
698     vbscript = create_vbscript();
699
700     hres = IActiveScript_QueryInterface(vbscript, &IID_IActiveScriptParse, (void**)&parser);
701     ok(hres == S_OK, "Could not get IActiveScriptParse iface: %08x\n", hres);
702
703     test_state(vbscript, SCRIPTSTATE_UNINITIALIZED);
704     test_safety(vbscript);
705
706     SET_EXPECT(GetLCID);
707     hres = IActiveScript_SetScriptSite(vbscript, &ActiveScriptSite);
708     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
709     CHECK_CALLED(GetLCID);
710
711     test_state(vbscript, SCRIPTSTATE_UNINITIALIZED);
712
713     SET_EXPECT(OnStateChange_INITIALIZED);
714     hres = IActiveScriptParse_InitNew(parser);
715     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
716     CHECK_CALLED(OnStateChange_INITIALIZED);
717
718     test_state(vbscript, SCRIPTSTATE_INITIALIZED);
719
720     SET_EXPECT(OnStateChange_CONNECTED);
721     hres = IActiveScript_SetScriptState(vbscript, SCRIPTSTATE_CONNECTED);
722     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
723     CHECK_CALLED(OnStateChange_CONNECTED);
724
725     test_state(vbscript, SCRIPTSTATE_CONNECTED);
726
727     IActiveScriptParse_Release(parser);
728
729     SET_EXPECT(OnStateChange_DISCONNECTED);
730     SET_EXPECT(OnStateChange_INITIALIZED);
731     SET_EXPECT(OnStateChange_CLOSED);
732     ref = IActiveScript_Release(vbscript);
733     ok(!ref, "ref = %d\n", ref);
734     CHECK_CALLED(OnStateChange_DISCONNECTED);
735     CHECK_CALLED(OnStateChange_INITIALIZED);
736     CHECK_CALLED(OnStateChange_CLOSED);
737 }
738
739 static void test_vbscript_simplecreate(void)
740 {
741     IActiveScript *script;
742     ULONG ref;
743     HRESULT hres;
744
745     script = create_vbscript();
746
747     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
748     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
749
750     ref = IActiveScript_Release(script);
751     ok(!ref, "ref = %d\n", ref);
752 }
753
754 static void test_vbscript_initializing(void)
755 {
756     IActiveScriptParse *parse;
757     IActiveScript *script;
758     ULONG ref;
759     HRESULT hres;
760
761     script = create_vbscript();
762
763     hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
764     ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
765
766     test_state(script, SCRIPTSTATE_UNINITIALIZED);
767
768     SET_EXPECT(GetLCID);
769     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
770     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
771     CHECK_CALLED(GetLCID);
772
773     SET_EXPECT(OnStateChange_INITIALIZED);
774     hres = IActiveScriptParse_InitNew(parse);
775     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
776     CHECK_CALLED(OnStateChange_INITIALIZED);
777
778     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
779     ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
780
781     SET_EXPECT(OnStateChange_CLOSED);
782     hres = IActiveScript_Close(script);
783     ok(hres == S_OK, "Close failed: %08x\n", hres);
784     CHECK_CALLED(OnStateChange_CLOSED);
785
786     test_state(script, SCRIPTSTATE_CLOSED);
787
788     IActiveScriptParse_Release(parse);
789
790     ref = IActiveScript_Release(script);
791     ok(!ref, "ref = %d\n", ref);
792 }
793
794 static BOOL check_vbscript(void)
795 {
796     IActiveScriptParseProcedure2 *vbscript;
797     HRESULT hres;
798
799     hres = CoCreateInstance(&CLSID_VBScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
800             &IID_IActiveScriptParseProcedure2, (void**)&vbscript);
801     if(SUCCEEDED(hres))
802         IActiveScriptParseProcedure2_Release(vbscript);
803
804     return hres == S_OK;
805 }
806
807 START_TEST(vbscript)
808 {
809     CoInitialize(NULL);
810
811     if(check_vbscript()) {
812         test_vbscript();
813         test_vbscript_uninitializing();
814         test_vbscript_release();
815         test_vbscript_simplecreate();
816         test_vbscript_initializing();
817         test_scriptdisp();
818     }else {
819         win_skip("VBScript engine not available or too old\n");
820     }
821
822     CoUninitialize();
823 }