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 "vbsregexp55.h"
29 
30 #include "wine/test.h"
31 
32 #ifdef _WIN64
33 
34 #define IActiveScriptParse_QueryInterface IActiveScriptParse64_QueryInterface
35 #define IActiveScriptParse_Release IActiveScriptParse64_Release
36 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
37 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
38 #define IActiveScriptParseProcedure2_Release \
39     IActiveScriptParseProcedure2_64_Release
40 
41 #else
42 
43 #define IActiveScriptParse_QueryInterface IActiveScriptParse32_QueryInterface
44 #define IActiveScriptParse_Release IActiveScriptParse32_Release
45 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
46 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
47 #define IActiveScriptParseProcedure2_Release \
48     IActiveScriptParseProcedure2_32_Release
49 
50 #endif
51 
52 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
53 
54 #define DEFINE_EXPECT(func) \
55     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
56 
57 #define SET_EXPECT(func) \
58     expect_ ## func = TRUE
59 
60 #define CHECK_EXPECT2(func) \
61     do { \
62         ok(expect_ ##func, "unexpected call " #func "\n"); \
63         called_ ## func = TRUE; \
64     }while(0)
65 
66 #define CHECK_EXPECT(func) \
67     do { \
68         CHECK_EXPECT2(func); \
69         expect_ ## func = FALSE; \
70     }while(0)
71 
72 #define CHECK_CALLED(func) \
73     do { \
74         ok(called_ ## func, "expected " #func "\n"); \
75         expect_ ## func = called_ ## func = FALSE; \
76     }while(0)
77 
78 DEFINE_EXPECT(GetLCID);
79 DEFINE_EXPECT(OnStateChange_UNINITIALIZED);
80 DEFINE_EXPECT(OnStateChange_STARTED);
81 DEFINE_EXPECT(OnStateChange_CONNECTED);
82 DEFINE_EXPECT(OnStateChange_DISCONNECTED);
83 DEFINE_EXPECT(OnStateChange_CLOSED);
84 DEFINE_EXPECT(OnStateChange_INITIALIZED);
85 DEFINE_EXPECT(OnEnterScript);
86 DEFINE_EXPECT(OnLeaveScript);
87 DEFINE_EXPECT(GetItemInfo_global);
88 DEFINE_EXPECT(GetItemInfo_visible);
89 DEFINE_EXPECT(testCall);
90 
91 DEFINE_GUID(CLSID_VBScript, 0xb54f3741, 0x5b07, 0x11cf, 0xa4,0xb0, 0x00,0xaa,0x00,0x4a,0x55,0xe8);
92 DEFINE_GUID(CLSID_VBScriptRegExp, 0x3f4daca4, 0x160d, 0x11d2, 0xa8,0xe9, 0x00,0x10,0x4b,0x36,0x5c,0x9f);
93 
a2bstr(const char * str)94 static BSTR a2bstr(const char *str)
95 {
96     BSTR ret;
97     int len;
98 
99     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
100     ret = SysAllocStringLen(NULL, len-1);
101     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
102 
103     return ret;
104 }
105 
106 #define test_state(s,ss) _test_state(__LINE__,s,ss)
_test_state(unsigned line,IActiveScript * script,SCRIPTSTATE exstate)107 static void _test_state(unsigned line, IActiveScript *script, SCRIPTSTATE exstate)
108 {
109     SCRIPTSTATE state = -1;
110     HRESULT hres;
111 
112     hres = IActiveScript_GetScriptState(script, &state);
113     ok_(__FILE__,line) (hres == S_OK, "GetScriptState failed: %08x\n", hres);
114     ok_(__FILE__,line) (state == exstate, "state=%d, expected %d\n", state, exstate);
115 }
116 
Dispatch_QueryInterface(IDispatch * iface,REFIID riid,void ** ppv)117 static HRESULT WINAPI Dispatch_QueryInterface(IDispatch *iface, REFIID riid, void **ppv)
118 {
119     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IDispatch, riid)) {
120         *ppv = iface;
121         IDispatch_AddRef(iface);
122         return S_OK;
123     }
124 
125     *ppv = NULL;
126     return E_NOINTERFACE;
127 }
128 
129 static ULONG global_named_item_ref, visible_named_item_ref;
130 
global_AddRef(IDispatch * iface)131 static ULONG WINAPI global_AddRef(IDispatch *iface)
132 {
133     return ++global_named_item_ref;
134 }
135 
global_Release(IDispatch * iface)136 static ULONG WINAPI global_Release(IDispatch *iface)
137 {
138     return --global_named_item_ref;
139 }
140 
visible_AddRef(IDispatch * iface)141 static ULONG WINAPI visible_AddRef(IDispatch *iface)
142 {
143     return ++visible_named_item_ref;
144 }
145 
visible_Release(IDispatch * iface)146 static ULONG WINAPI visible_Release(IDispatch *iface)
147 {
148     return --visible_named_item_ref;
149 }
150 
Dispatch_GetTypeInfoCount(IDispatch * iface,UINT * pctinfo)151 static HRESULT WINAPI Dispatch_GetTypeInfoCount(IDispatch *iface, UINT *pctinfo)
152 {
153     ok(0, "unexpected call\n");
154     return E_NOTIMPL;
155 }
156 
Dispatch_GetTypeInfo(IDispatch * iface,UINT iTInfo,LCID lcid,ITypeInfo ** ppTInfo)157 static HRESULT WINAPI Dispatch_GetTypeInfo(IDispatch *iface, UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
158 {
159     return DISP_E_BADINDEX;
160 }
161 
Dispatch_GetIDsOfNames(IDispatch * iface,REFIID riid,LPOLESTR * names,UINT name_cnt,LCID lcid,DISPID * ids)162 static HRESULT WINAPI Dispatch_GetIDsOfNames(IDispatch *iface, REFIID riid, LPOLESTR *names, UINT name_cnt,
163                                             LCID lcid, DISPID *ids)
164 {
165     ok(name_cnt == 1, "name_cnt = %u\n", name_cnt);
166     ok(!wcscmp(names[0], L"testCall"), "names[0] = %s\n", wine_dbgstr_w(names[0]));
167     *ids = 1;
168     return S_OK;
169 }
170 
Dispatch_Invoke(IDispatch * iface,DISPID id,REFIID riid,LCID lcid,WORD flags,DISPPARAMS * dp,VARIANT * res,EXCEPINFO * ei,UINT * err)171 static HRESULT WINAPI Dispatch_Invoke(IDispatch *iface, DISPID id, REFIID riid, LCID lcid, WORD flags,
172                                       DISPPARAMS *dp, VARIANT *res, EXCEPINFO *ei, UINT *err)
173 {
174     CHECK_EXPECT(testCall);
175     ok(id == 1, "id = %u\n", id);
176     ok(flags == DISPATCH_METHOD, "flags = %x\n", flags);
177     ok(!dp->cArgs, "cArgs = %u\n", dp->cArgs);
178     ok(!res, "res = %p\n", res);
179     return S_OK;
180 }
181 
182 static const IDispatchVtbl global_named_item_vtbl = {
183     Dispatch_QueryInterface,
184     global_AddRef,
185     global_Release,
186     Dispatch_GetTypeInfoCount,
187     Dispatch_GetTypeInfo,
188     Dispatch_GetIDsOfNames,
189     Dispatch_Invoke
190 };
191 
192 static IDispatch global_named_item = { &global_named_item_vtbl };
193 
194 static const IDispatchVtbl visible_named_item_vtbl = {
195     Dispatch_QueryInterface,
196     visible_AddRef,
197     visible_Release,
198     Dispatch_GetTypeInfoCount,
199     Dispatch_GetTypeInfo,
200     Dispatch_GetIDsOfNames,
201     Dispatch_Invoke
202 };
203 
204 static IDispatch visible_named_item = { &visible_named_item_vtbl };
205 
ActiveScriptSite_QueryInterface(IActiveScriptSite * iface,REFIID riid,void ** ppv)206 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
207 {
208     *ppv = NULL;
209 
210     if(IsEqualGUID(&IID_IUnknown, riid))
211         *ppv = iface;
212     else if(IsEqualGUID(&IID_IActiveScriptSite, riid))
213         *ppv = iface;
214     else
215         return E_NOINTERFACE;
216 
217     IUnknown_AddRef((IUnknown*)*ppv);
218     return S_OK;
219 }
220 
ActiveScriptSite_AddRef(IActiveScriptSite * iface)221 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
222 {
223     return 2;
224 }
225 
ActiveScriptSite_Release(IActiveScriptSite * iface)226 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
227 {
228     return 1;
229 }
230 
ActiveScriptSite_GetLCID(IActiveScriptSite * iface,LCID * plcid)231 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
232 {
233     CHECK_EXPECT(GetLCID);
234     return E_NOTIMPL;
235 }
236 
ActiveScriptSite_GetItemInfo(IActiveScriptSite * iface,LPCOLESTR name,DWORD return_mask,IUnknown ** item_unk,ITypeInfo ** item_ti)237 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR name,
238         DWORD return_mask, IUnknown **item_unk, ITypeInfo **item_ti)
239 {
240     ok(return_mask == SCRIPTINFO_IUNKNOWN, "return_mask = %x\n", return_mask);
241     if(!wcscmp(name, L"globalItem")) {
242         CHECK_EXPECT(GetItemInfo_global);
243         IDispatch_AddRef(&global_named_item);
244         *item_unk = (IUnknown*)&global_named_item;
245         return S_OK;
246     }
247     if(!wcscmp(name, L"visibleItem")) {
248         CHECK_EXPECT(GetItemInfo_visible);
249         IDispatch_AddRef(&visible_named_item);
250         *item_unk = (IUnknown*)&visible_named_item;
251         return S_OK;
252     }
253     ok(0, "unexpected call %s\n", wine_dbgstr_w(name));
254     return E_NOTIMPL;
255 }
256 
ActiveScriptSite_GetDocVersionString(IActiveScriptSite * iface,BSTR * pbstrVersion)257 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
258 {
259     ok(0, "unexpected call\n");
260     return E_NOTIMPL;
261 }
262 
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite * iface,const VARIANT * pvarResult,const EXCEPINFO * pexcepinfo)263 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
264         const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
265 {
266     ok(0, "unexpected call\n");
267     return E_NOTIMPL;
268 }
269 
ActiveScriptSite_OnStateChange(IActiveScriptSite * iface,SCRIPTSTATE ssScriptState)270 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
271 {
272     switch(ssScriptState) {
273     case SCRIPTSTATE_UNINITIALIZED:
274         CHECK_EXPECT(OnStateChange_UNINITIALIZED);
275         return S_OK;
276     case SCRIPTSTATE_STARTED:
277         CHECK_EXPECT(OnStateChange_STARTED);
278         return S_OK;
279     case SCRIPTSTATE_CONNECTED:
280         CHECK_EXPECT(OnStateChange_CONNECTED);
281         return S_OK;
282     case SCRIPTSTATE_DISCONNECTED:
283         CHECK_EXPECT(OnStateChange_DISCONNECTED);
284         return S_OK;
285     case SCRIPTSTATE_CLOSED:
286         CHECK_EXPECT(OnStateChange_CLOSED);
287         return S_OK;
288     case SCRIPTSTATE_INITIALIZED:
289         CHECK_EXPECT(OnStateChange_INITIALIZED);
290         return S_OK;
291     default:
292         ok(0, "unexpected call %d\n", ssScriptState);
293     }
294 
295     return E_NOTIMPL;
296 }
297 
ActiveScriptSite_OnScriptError(IActiveScriptSite * iface,IActiveScriptError * pscripterror)298 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
299 {
300     ok(0, "unexpected call\n");
301     return E_NOTIMPL;
302 }
303 
ActiveScriptSite_OnEnterScript(IActiveScriptSite * iface)304 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
305 {
306     CHECK_EXPECT(OnEnterScript);
307     return S_OK;
308 }
309 
ActiveScriptSite_OnLeaveScript(IActiveScriptSite * iface)310 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
311 {
312     CHECK_EXPECT(OnLeaveScript);
313     return S_OK;
314 }
315 
316 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
317     ActiveScriptSite_QueryInterface,
318     ActiveScriptSite_AddRef,
319     ActiveScriptSite_Release,
320     ActiveScriptSite_GetLCID,
321     ActiveScriptSite_GetItemInfo,
322     ActiveScriptSite_GetDocVersionString,
323     ActiveScriptSite_OnScriptTerminate,
324     ActiveScriptSite_OnStateChange,
325     ActiveScriptSite_OnScriptError,
326     ActiveScriptSite_OnEnterScript,
327     ActiveScriptSite_OnLeaveScript
328 };
329 
330 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
331 
test_safety(IActiveScript * script)332 static void test_safety(IActiveScript *script)
333 {
334     IObjectSafety *safety;
335     DWORD supported, enabled;
336     HRESULT hres;
337 
338     hres = IActiveScript_QueryInterface(script, &IID_IObjectSafety, (void**)&safety);
339     ok(hres == S_OK, "Could not get IObjectSafety: %08x\n", hres);
340     if(FAILED(hres))
341         return;
342 
343     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, NULL);
344     ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
345     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, NULL, &enabled);
346     ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
347 
348     supported = enabled = 0xdeadbeef;
349     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, &enabled);
350     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
351     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
352        "supported=%x\n", supported);
353     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
354 
355     supported = enabled = 0xdeadbeef;
356     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScript, &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);
361 
362     supported = enabled = 0xdeadbeef;
363     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
364     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
365     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
366        "supported=%x\n", supported);
367     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
368 
369     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
370             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER
371                 |INTERFACESAFE_FOR_UNTRUSTED_CALLER,
372             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
373     ok(hres == E_FAIL, "SetInterfaceSafetyOptions failed: %08x, expected E_FAIL\n", hres);
374 
375     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
376             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
377             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
378     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
379 
380     supported = enabled = 0xdeadbeef;
381     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
382     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
383     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
384        "supported=%x\n", supported);
385     ok(enabled == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
386        "enabled=%x\n", enabled);
387 
388     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, INTERFACESAFE_FOR_UNTRUSTED_DATA, 0);
389     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
390 
391     supported = enabled = 0xdeadbeef;
392     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
393     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
394     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
395        "supported=%x\n", supported);
396     ok(enabled == (INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER), "enabled=%x\n", enabled);
397 
398     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
399             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER, 0);
400     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
401 
402     supported = enabled = 0xdeadbeef;
403     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
404     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
405     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
406        "supported=%x\n", supported);
407     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
408 
409     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
410             INTERFACE_USES_DISPEX, 0);
411     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
412 
413     supported = enabled = 0xdeadbeef;
414     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
415     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
416     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
417        "supported=%x\n", supported);
418     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
419 
420     IObjectSafety_Release(safety);
421 }
422 
get_script_dispatch(IActiveScript * script)423 static IDispatchEx *get_script_dispatch(IActiveScript *script)
424 {
425     IDispatchEx *dispex;
426     IDispatch *disp;
427     HRESULT hres;
428 
429     disp = (void*)0xdeadbeef;
430     hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
431     ok(hres == S_OK, "GetScriptDispatch failed: %08x\n", hres);
432     if(FAILED(hres))
433         return NULL;
434 
435     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
436     IDispatch_Release(disp);
437     ok(hres == S_OK, "Could not get IDispatchEx iface: %08x\n", hres);
438     return dispex;
439 }
440 
parse_script(IActiveScriptParse * parse,const char * src)441 static void parse_script(IActiveScriptParse *parse, const char *src)
442 {
443     BSTR str;
444     HRESULT hres;
445 
446     SET_EXPECT(OnEnterScript);
447     SET_EXPECT(OnLeaveScript);
448 
449     str = a2bstr(src);
450     hres = IActiveScriptParse_ParseScriptText(parse, str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
451     SysFreeString(str);
452     ok(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
453 
454     CHECK_CALLED(OnEnterScript);
455     CHECK_CALLED(OnLeaveScript);
456 }
457 
458 #define get_disp_id(a,b,c,d) _get_disp_id(__LINE__,a,b,c,d)
_get_disp_id(unsigned line,IDispatchEx * dispex,const char * name,HRESULT exhres,DISPID * id)459 static void _get_disp_id(unsigned line, IDispatchEx *dispex, const char *name, HRESULT exhres, DISPID *id)
460 {
461     DISPID id2;
462     BSTR str;
463     HRESULT hres;
464 
465     str = a2bstr(name);
466     hres = IDispatchEx_GetDispID(dispex, str, 0, id);
467     ok_(__FILE__,line)(hres == exhres, "GetDispID(%s) returned %08x, expected %08x\n", name, hres, exhres);
468 
469     hres = IDispatchEx_GetIDsOfNames(dispex, &IID_NULL, &str, 1, 0, &id2);
470     SysFreeString(str);
471     ok_(__FILE__,line)(hres == exhres, "GetIDsOfNames(%s) returned %08x, expected %08x\n", name, hres, exhres);
472     ok_(__FILE__,line)(*id == id2, "GetIDsOfNames(%s) id != id2\n", name);
473 }
474 
test_no_script_dispatch(IActiveScript * script)475 static void test_no_script_dispatch(IActiveScript *script)
476 {
477     IDispatch *disp;
478     HRESULT hres;
479 
480     disp = (void*)0xdeadbeef;
481     hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
482     ok(hres == E_UNEXPECTED, "hres = %08x, expected E_UNEXPECTED\n", hres);
483     ok(!disp, "disp != NULL\n");
484 }
485 
create_vbscript(void)486 static IActiveScript *create_vbscript(void)
487 {
488     IActiveScript *ret;
489     HRESULT hres;
490 
491     hres = CoCreateInstance(&CLSID_VBScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
492             &IID_IActiveScript, (void**)&ret);
493     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
494 
495     return ret;
496 }
497 
test_scriptdisp(void)498 static void test_scriptdisp(void)
499 {
500     IDispatchEx *script_disp, *script_disp2;
501     IActiveScriptParse *parser;
502     IActiveScript *vbscript;
503     DISPID id, id2;
504     DISPPARAMS dp;
505     EXCEPINFO ei;
506     VARIANT v;
507     ULONG ref;
508     HRESULT hres;
509 
510     vbscript = create_vbscript();
511 
512     hres = IActiveScript_QueryInterface(vbscript, &IID_IActiveScriptParse, (void**)&parser);
513     ok(hres == S_OK, "Could not get IActiveScriptParse iface: %08x\n", hres);
514 
515     test_state(vbscript, SCRIPTSTATE_UNINITIALIZED);
516     test_safety(vbscript);
517 
518     SET_EXPECT(GetLCID);
519     hres = IActiveScript_SetScriptSite(vbscript, &ActiveScriptSite);
520     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
521     CHECK_CALLED(GetLCID);
522 
523     script_disp2 = get_script_dispatch(vbscript);
524 
525     test_state(vbscript, SCRIPTSTATE_UNINITIALIZED);
526 
527     SET_EXPECT(OnStateChange_INITIALIZED);
528     hres = IActiveScriptParse_InitNew(parser);
529     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
530     CHECK_CALLED(OnStateChange_INITIALIZED);
531 
532     test_state(vbscript, SCRIPTSTATE_INITIALIZED);
533 
534     SET_EXPECT(OnStateChange_CONNECTED);
535     hres = IActiveScript_SetScriptState(vbscript, SCRIPTSTATE_CONNECTED);
536     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
537     CHECK_CALLED(OnStateChange_CONNECTED);
538 
539     test_state(vbscript, SCRIPTSTATE_CONNECTED);
540 
541     script_disp = get_script_dispatch(vbscript);
542     ok(script_disp == script_disp2, "script_disp != script_disp2\n");
543     IDispatchEx_Release(script_disp2);
544 
545     id = 100;
546     get_disp_id(script_disp, "LCase", DISP_E_UNKNOWNNAME, &id);
547     ok(id == -1, "id = %d, expected -1\n", id);
548 
549     get_disp_id(script_disp, "globalVariable", DISP_E_UNKNOWNNAME, &id);
550     parse_script(parser, "dim globalVariable\nglobalVariable = 3");
551     get_disp_id(script_disp, "globalVariable", S_OK, &id);
552 
553     memset(&dp, 0, sizeof(dp));
554     memset(&ei, 0, sizeof(ei));
555     V_VT(&v) = VT_EMPTY;
556     hres = IDispatchEx_InvokeEx(script_disp, id, 0, DISPATCH_PROPERTYGET|DISPATCH_METHOD, &dp, &v, &ei, NULL);
557     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
558     ok(V_VT(&v) == VT_I2, "V_VT(v) = %d\n", V_VT(&v));
559     ok(V_I2(&v) == 3, "V_I2(v) = %d\n", V_I2(&v));
560 
561     get_disp_id(script_disp, "globalVariable2", DISP_E_UNKNOWNNAME, &id);
562     parse_script(parser, "globalVariable2 = 4");
563     get_disp_id(script_disp, "globalVariable2", S_OK, &id);
564 
565     get_disp_id(script_disp, "globalFunction", DISP_E_UNKNOWNNAME, &id);
566     parse_script(parser, "function globalFunction()\nglobalFunction=5\nend function");
567     get_disp_id(script_disp, "globalFunction", S_OK, &id);
568 
569     SET_EXPECT(OnEnterScript);
570     SET_EXPECT(OnLeaveScript);
571 
572     memset(&dp, 0, sizeof(dp));
573     memset(&ei, 0, sizeof(ei));
574     V_VT(&v) = VT_EMPTY;
575     hres = IDispatchEx_InvokeEx(script_disp, id, 0, DISPATCH_PROPERTYGET|DISPATCH_METHOD, &dp, &v, &ei, NULL);
576     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
577     ok(V_VT(&v) == VT_I2, "V_VT(v) = %d\n", V_VT(&v));
578     ok(V_I2(&v) == 5, "V_I2(v) = %d\n", V_I2(&v));
579 
580     CHECK_CALLED(OnEnterScript);
581     CHECK_CALLED(OnLeaveScript);
582 
583     SET_EXPECT(OnEnterScript);
584     SET_EXPECT(OnLeaveScript);
585 
586     memset(&dp, 0, sizeof(dp));
587     memset(&ei, 0, sizeof(ei));
588     V_VT(&v) = VT_EMPTY;
589     hres = IDispatchEx_Invoke(script_disp, id, &IID_NULL, 0, DISPATCH_PROPERTYGET|DISPATCH_METHOD, &dp, &v, &ei, NULL);
590     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
591     ok(V_VT(&v) == VT_I2, "V_VT(v) = %d\n", V_VT(&v));
592     ok(V_I2(&v) == 5, "V_I2(v) = %d\n", V_I2(&v));
593 
594     CHECK_CALLED(OnEnterScript);
595     CHECK_CALLED(OnLeaveScript);
596 
597     get_disp_id(script_disp, "globalSub", DISP_E_UNKNOWNNAME, &id);
598     parse_script(parser, "sub globalSub()\nend sub");
599     get_disp_id(script_disp, "globalSub", S_OK, &id);
600     get_disp_id(script_disp, "globalSub", S_OK, &id2);
601     ok(id == id2, "id != id2\n");
602 
603     get_disp_id(script_disp, "constVariable", DISP_E_UNKNOWNNAME, &id);
604     parse_script(parser, "const constVariable = 6");
605     get_disp_id(script_disp, "ConstVariable", S_OK, &id);
606     get_disp_id(script_disp, "Constvariable", S_OK, &id2);
607     ok(id == id2, "id != id2\n");
608 
609     IDispatchEx_Release(script_disp);
610 
611     IActiveScriptParse_Release(parser);
612 
613     SET_EXPECT(OnStateChange_DISCONNECTED);
614     SET_EXPECT(OnStateChange_INITIALIZED);
615     SET_EXPECT(OnStateChange_CLOSED);
616     hres = IActiveScript_Close(vbscript);
617     ok(hres == S_OK, "Close failed: %08x\n", hres);
618     CHECK_CALLED(OnStateChange_DISCONNECTED);
619     CHECK_CALLED(OnStateChange_INITIALIZED);
620     CHECK_CALLED(OnStateChange_CLOSED);
621 
622     ref = IActiveScript_Release(vbscript);
623     ok(!ref, "ref = %d\n", ref);
624 }
625 
test_vbscript(void)626 static void test_vbscript(void)
627 {
628     IActiveScriptParseProcedure2 *parse_proc;
629     IActiveScriptParse *parser;
630     IActiveScript *vbscript;
631     ULONG ref;
632     HRESULT hres;
633 
634     vbscript = create_vbscript();
635 
636     hres = IActiveScript_QueryInterface(vbscript, &IID_IActiveScriptParse, (void**)&parser);
637     ok(hres == S_OK, "Could not get IActiveScriptParse iface: %08x\n", hres);
638 
639     test_state(vbscript, SCRIPTSTATE_UNINITIALIZED);
640     test_safety(vbscript);
641     test_no_script_dispatch(vbscript);
642 
643     SET_EXPECT(GetLCID);
644     hres = IActiveScript_SetScriptSite(vbscript, &ActiveScriptSite);
645     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
646     CHECK_CALLED(GetLCID);
647 
648     test_state(vbscript, SCRIPTSTATE_UNINITIALIZED);
649 
650     SET_EXPECT(OnStateChange_INITIALIZED);
651     hres = IActiveScriptParse_InitNew(parser);
652     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
653     CHECK_CALLED(OnStateChange_INITIALIZED);
654 
655     test_state(vbscript, SCRIPTSTATE_INITIALIZED);
656 
657     hres = IActiveScriptParse_InitNew(parser);
658     ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
659 
660     SET_EXPECT(OnStateChange_CONNECTED);
661     hres = IActiveScript_SetScriptState(vbscript, SCRIPTSTATE_CONNECTED);
662     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
663     CHECK_CALLED(OnStateChange_CONNECTED);
664 
665     test_state(vbscript, SCRIPTSTATE_CONNECTED);
666 
667     SET_EXPECT(OnStateChange_DISCONNECTED);
668     SET_EXPECT(OnStateChange_INITIALIZED);
669     SET_EXPECT(OnStateChange_CLOSED);
670     hres = IActiveScript_Close(vbscript);
671     ok(hres == S_OK, "Close failed: %08x\n", hres);
672     CHECK_CALLED(OnStateChange_DISCONNECTED);
673     CHECK_CALLED(OnStateChange_INITIALIZED);
674     CHECK_CALLED(OnStateChange_CLOSED);
675 
676     test_state(vbscript, SCRIPTSTATE_CLOSED);
677     test_no_script_dispatch(vbscript);
678 
679     IActiveScriptParse_Release(parser);
680 
681     hres = IActiveScript_QueryInterface(vbscript, &IID_IActiveScriptParseProcedure, (void**)&parse_proc);
682     ok(hres == E_NOINTERFACE, "Got IActiveScriptParseProcedure interface, expected E_NOTIMPL\n");
683 
684     hres = IActiveScript_QueryInterface(vbscript, &IID_IActiveScriptParseProcedure2, (void**)&parse_proc);
685     ok(hres == S_OK, "Could not get IActiveScriptParseProcedure2 interface\n");
686     IActiveScriptParseProcedure2_Release(parse_proc);
687 
688     ref = IActiveScript_Release(vbscript);
689     ok(!ref, "ref = %d\n", ref);
690 }
691 
test_vbscript_uninitializing(void)692 static void test_vbscript_uninitializing(void)
693 {
694     IActiveScriptParse *parse;
695     IActiveScript *script;
696     IDispatchEx *dispex;
697     ULONG ref;
698     HRESULT hres;
699 
700     static const WCHAR script_textW[] =
701         {'F','u','n','c','t','i','o','n',' ','f','\n','E','n','d',' ','F','u','n','c','t','i','o','n','\n',0};
702 
703     script = create_vbscript();
704 
705     hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
706     ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
707 
708     test_state(script, SCRIPTSTATE_UNINITIALIZED);
709 
710     hres = IActiveScriptParse_InitNew(parse);
711     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
712 
713     test_no_script_dispatch(script);
714 
715     SET_EXPECT(GetLCID);
716     SET_EXPECT(OnStateChange_INITIALIZED);
717     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
718     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
719     CHECK_CALLED(GetLCID);
720     CHECK_CALLED(OnStateChange_INITIALIZED);
721 
722     test_state(script, SCRIPTSTATE_INITIALIZED);
723 
724     hres = IActiveScriptParse_ParseScriptText(parse, script_textW, NULL, NULL, NULL, 0, 1, 0x42, NULL, NULL);
725     ok(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
726 
727     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
728     ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
729 
730     SET_EXPECT(OnStateChange_UNINITIALIZED);
731     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
732     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
733     CHECK_CALLED(OnStateChange_UNINITIALIZED);
734 
735     test_state(script, SCRIPTSTATE_UNINITIALIZED);
736 
737     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
738     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
739 
740     SET_EXPECT(GetLCID);
741     SET_EXPECT(OnStateChange_INITIALIZED);
742     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
743     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
744     CHECK_CALLED(GetLCID);
745     CHECK_CALLED(OnStateChange_INITIALIZED);
746 
747     SET_EXPECT(OnStateChange_CONNECTED);
748     SET_EXPECT(OnEnterScript);
749     SET_EXPECT(OnLeaveScript);
750     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_CONNECTED);
751     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
752     CHECK_CALLED(OnStateChange_CONNECTED);
753     CHECK_CALLED(OnEnterScript);
754     CHECK_CALLED(OnLeaveScript);
755 
756     test_state(script, SCRIPTSTATE_CONNECTED);
757 
758     dispex = get_script_dispatch(script);
759     ok(dispex != NULL, "dispex == NULL\n");
760     if(dispex)
761         IDispatchEx_Release(dispex);
762 
763     SET_EXPECT(OnStateChange_DISCONNECTED);
764     SET_EXPECT(OnStateChange_INITIALIZED);
765     SET_EXPECT(OnStateChange_UNINITIALIZED);
766     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
767     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
768     CHECK_CALLED(OnStateChange_DISCONNECTED);
769     CHECK_CALLED(OnStateChange_INITIALIZED);
770     CHECK_CALLED(OnStateChange_UNINITIALIZED);
771 
772     test_state(script, SCRIPTSTATE_UNINITIALIZED);
773 
774     hres = IActiveScript_Close(script);
775     ok(hres == S_OK, "Close failed: %08x\n", hres);
776 
777     test_state(script, SCRIPTSTATE_CLOSED);
778 
779     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
780     ok(hres == E_UNEXPECTED, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x, expected E_UNEXPECTED\n", hres);
781 
782     test_state(script, SCRIPTSTATE_CLOSED);
783 
784     SET_EXPECT(GetLCID);
785     SET_EXPECT(OnStateChange_INITIALIZED);
786     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
787     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
788     CHECK_CALLED(GetLCID);
789     CHECK_CALLED(OnStateChange_INITIALIZED);
790 
791     test_state(script, SCRIPTSTATE_INITIALIZED);
792 
793     SET_EXPECT(OnStateChange_CLOSED);
794     hres = IActiveScript_Close(script);
795     ok(hres == S_OK, "Close failed: %08x\n", hres);
796     CHECK_CALLED(OnStateChange_CLOSED);
797 
798     test_state(script, SCRIPTSTATE_CLOSED);
799 
800     hres = IActiveScriptParse_InitNew(parse);
801     ok(hres == E_UNEXPECTED, "InitNew failed: %08x\n", hres);
802 
803     IActiveScriptParse_Release(parse);
804 
805     ref = IActiveScript_Release(script);
806     ok(!ref, "ref = %d\n", ref);
807 }
808 
test_vbscript_release(void)809 static void test_vbscript_release(void)
810 {
811     IActiveScriptParse *parser;
812     IActiveScript *vbscript;
813     ULONG ref;
814     HRESULT hres;
815 
816     vbscript = create_vbscript();
817 
818     hres = IActiveScript_QueryInterface(vbscript, &IID_IActiveScriptParse, (void**)&parser);
819     ok(hres == S_OK, "Could not get IActiveScriptParse iface: %08x\n", hres);
820 
821     test_state(vbscript, SCRIPTSTATE_UNINITIALIZED);
822     test_safety(vbscript);
823 
824     SET_EXPECT(GetLCID);
825     hres = IActiveScript_SetScriptSite(vbscript, &ActiveScriptSite);
826     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
827     CHECK_CALLED(GetLCID);
828 
829     test_state(vbscript, SCRIPTSTATE_UNINITIALIZED);
830 
831     SET_EXPECT(OnStateChange_INITIALIZED);
832     hres = IActiveScriptParse_InitNew(parser);
833     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
834     CHECK_CALLED(OnStateChange_INITIALIZED);
835 
836     test_state(vbscript, SCRIPTSTATE_INITIALIZED);
837 
838     SET_EXPECT(OnStateChange_CONNECTED);
839     hres = IActiveScript_SetScriptState(vbscript, SCRIPTSTATE_CONNECTED);
840     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
841     CHECK_CALLED(OnStateChange_CONNECTED);
842 
843     test_state(vbscript, SCRIPTSTATE_CONNECTED);
844 
845     IActiveScriptParse_Release(parser);
846 
847     SET_EXPECT(OnStateChange_DISCONNECTED);
848     SET_EXPECT(OnStateChange_INITIALIZED);
849     SET_EXPECT(OnStateChange_CLOSED);
850     ref = IActiveScript_Release(vbscript);
851     ok(!ref, "ref = %d\n", ref);
852     CHECK_CALLED(OnStateChange_DISCONNECTED);
853     CHECK_CALLED(OnStateChange_INITIALIZED);
854     CHECK_CALLED(OnStateChange_CLOSED);
855 }
856 
test_vbscript_simplecreate(void)857 static void test_vbscript_simplecreate(void)
858 {
859     IActiveScript *script;
860     ULONG ref;
861     HRESULT hres;
862 
863     script = create_vbscript();
864 
865     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
866     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
867 
868     ref = IActiveScript_Release(script);
869     ok(!ref, "ref = %d\n", ref);
870 }
871 
test_vbscript_initializing(void)872 static void test_vbscript_initializing(void)
873 {
874     IActiveScriptParse *parse;
875     IActiveScript *script;
876     ULONG ref;
877     HRESULT hres;
878 
879     script = create_vbscript();
880 
881     hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
882     ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
883 
884     test_state(script, SCRIPTSTATE_UNINITIALIZED);
885 
886     SET_EXPECT(GetLCID);
887     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
888     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
889     CHECK_CALLED(GetLCID);
890 
891     SET_EXPECT(OnStateChange_INITIALIZED);
892     hres = IActiveScriptParse_InitNew(parse);
893     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
894     CHECK_CALLED(OnStateChange_INITIALIZED);
895 
896     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
897     ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
898 
899     SET_EXPECT(OnStateChange_CLOSED);
900     hres = IActiveScript_Close(script);
901     ok(hres == S_OK, "Close failed: %08x\n", hres);
902     CHECK_CALLED(OnStateChange_CLOSED);
903 
904     test_state(script, SCRIPTSTATE_CLOSED);
905 
906     IActiveScriptParse_Release(parse);
907 
908     ref = IActiveScript_Release(script);
909     ok(!ref, "ref = %d\n", ref);
910 }
911 
test_named_items(void)912 static void test_named_items(void)
913 {
914     IActiveScriptParse *parse;
915     IActiveScript *script;
916     ULONG ref;
917     HRESULT hres;
918 
919     script = create_vbscript();
920 
921     hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
922     ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
923 
924     test_state(script, SCRIPTSTATE_UNINITIALIZED);
925 
926     hres = IActiveScript_AddNamedItem(script, L"visibleItem", SCRIPTITEM_ISVISIBLE);
927     ok(hres == E_UNEXPECTED, "AddNamedItem returned: %08x\n", hres);
928     hres = IActiveScript_AddNamedItem(script, L"globalItem", SCRIPTITEM_GLOBALMEMBERS);
929     ok(hres == E_UNEXPECTED, "AddNamedItem returned: %08x\n", hres);
930 
931     SET_EXPECT(GetLCID);
932     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
933     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
934     CHECK_CALLED(GetLCID);
935 
936     SET_EXPECT(GetItemInfo_global);
937     hres = IActiveScript_AddNamedItem(script, L"globalItem", SCRIPTITEM_GLOBALMEMBERS);
938     ok(hres == S_OK, "AddNamedItem failed: %08x\n", hres);
939     CHECK_CALLED(GetItemInfo_global);
940 
941     hres = IActiveScript_AddNamedItem(script, L"visibleItem", SCRIPTITEM_ISVISIBLE);
942     ok(hres == S_OK, "AddNamedItem failed: %08x\n", hres);
943 
944     ok(global_named_item_ref > 0, "global_named_item_ref = %u\n", global_named_item_ref);
945     ok(visible_named_item_ref == 0, "visible_named_item_ref = %u\n", visible_named_item_ref);
946 
947     SET_EXPECT(OnStateChange_INITIALIZED);
948     hres = IActiveScriptParse_InitNew(parse);
949     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
950     CHECK_CALLED(OnStateChange_INITIALIZED);
951 
952     SET_EXPECT(OnStateChange_CONNECTED);
953     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_CONNECTED);
954     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
955     CHECK_CALLED(OnStateChange_CONNECTED);
956 
957     SET_EXPECT(testCall);
958     parse_script(parse, "testCall\n");
959     CHECK_CALLED(testCall);
960 
961     SET_EXPECT(GetItemInfo_visible);
962     SET_EXPECT(testCall);
963     parse_script(parse, "visibleItem.testCall\n");
964     CHECK_CALLED(GetItemInfo_visible);
965     CHECK_CALLED(testCall);
966 
967     ok(global_named_item_ref > 0, "global_named_item_ref = %u\n", global_named_item_ref);
968     ok(visible_named_item_ref == 1, "visible_named_item_ref = %u\n", visible_named_item_ref);
969 
970     SET_EXPECT(testCall);
971     parse_script(parse, "visibleItem.testCall\n");
972     CHECK_CALLED(testCall);
973 
974     SET_EXPECT(OnStateChange_DISCONNECTED);
975     SET_EXPECT(OnStateChange_INITIALIZED);
976     SET_EXPECT(OnStateChange_CLOSED);
977     hres = IActiveScript_Close(script);
978     ok(hres == S_OK, "Close failed: %08x\n", hres);
979     CHECK_CALLED(OnStateChange_DISCONNECTED);
980     CHECK_CALLED(OnStateChange_INITIALIZED);
981     CHECK_CALLED(OnStateChange_CLOSED);
982 
983     ok(global_named_item_ref == 0, "global_named_item_ref = %u\n", global_named_item_ref);
984     ok(visible_named_item_ref == 0, "visible_named_item_ref = %u\n", visible_named_item_ref);
985 
986     test_state(script, SCRIPTSTATE_CLOSED);
987 
988     IActiveScriptParse_Release(parse);
989 
990     ref = IActiveScript_Release(script);
991     ok(!ref, "ref = %d\n", ref);
992 }
993 
test_RegExp(void)994 static void test_RegExp(void)
995 {
996     IRegExp2 *regexp;
997     IMatchCollection2 *mc;
998     IMatch2 *match;
999     ISubMatches *sm;
1000     IEnumVARIANT *ev;
1001     IUnknown *unk;
1002     IDispatch *disp;
1003     HRESULT hres;
1004     BSTR bstr;
1005     LONG count;
1006     VARIANT v;
1007     ULONG fetched;
1008 
1009     hres = CoCreateInstance(&CLSID_VBScriptRegExp, NULL,
1010             CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1011             &IID_IUnknown, (void**)&unk);
1012     if(hres == REGDB_E_CLASSNOTREG) {
1013         win_skip("VBScriptRegExp is not registered\n");
1014         return;
1015     }
1016     ok(hres == S_OK, "CoCreateInstance(CLSID_VBScriptRegExp) failed: %x\n", hres);
1017 
1018     hres = IUnknown_QueryInterface(unk, &IID_IRegExp2, (void**)&regexp);
1019     if(hres == E_NOINTERFACE) {
1020         win_skip("IRegExp2 interface is not available\n");
1021         return;
1022     }
1023     ok(hres == S_OK, "QueryInterface(IID_IRegExp2) failed: %x\n", hres);
1024     IUnknown_Release(unk);
1025 
1026     hres = IRegExp2_QueryInterface(regexp, &IID_IRegExp, (void**)&unk);
1027     ok(hres == S_OK, "QueryInterface(IID_IRegExp) returned %x\n", hres);
1028     IUnknown_Release(unk);
1029 
1030     hres = IRegExp2_QueryInterface(regexp, &IID_IDispatchEx, (void**)&unk);
1031     ok(hres == E_NOINTERFACE, "QueryInterface(IID_IDispatchEx) returned %x\n", hres);
1032 
1033     hres = IRegExp2_get_Pattern(regexp, &bstr);
1034     ok(bstr == NULL, "bstr != NULL\n");
1035     ok(hres == S_OK, "get_Pattern returned %x, expected S_OK\n", hres);
1036 
1037     hres = IRegExp2_get_Pattern(regexp, NULL);
1038     ok(hres == E_POINTER, "get_Pattern returned %x, expected E_POINTER\n", hres);
1039 
1040     hres = IRegExp2_get_IgnoreCase(regexp, NULL);
1041     ok(hres == E_POINTER, "get_IgnoreCase returned %x, expected E_POINTER\n", hres);
1042 
1043     hres = IRegExp2_get_Global(regexp, NULL);
1044     ok(hres == E_POINTER, "get_Global returned %x, expected E_POINTER\n", hres);
1045 
1046     hres = IRegExp2_Execute(regexp, NULL, &disp);
1047     ok(hres == S_OK, "Execute returned %x, expected S_OK\n", hres);
1048     hres = IDispatch_QueryInterface(disp, &IID_IMatchCollection2, (void**)&mc);
1049     ok(hres == S_OK, "QueryInterface(IID_IMatchCollection2) returned %x\n", hres);
1050     IDispatch_Release(disp);
1051 
1052     hres = IMatchCollection2_QueryInterface(mc, &IID_IMatchCollection, (void**)&unk);
1053     ok(hres == S_OK, "QueryInterface(IID_IMatchCollection) returned %x\n", hres);
1054     IUnknown_Release(unk);
1055 
1056     hres = IMatchCollection2_get_Count(mc, NULL);
1057     ok(hres == E_POINTER, "get_Count returned %x, expected E_POINTER\n", hres);
1058 
1059     hres = IMatchCollection2_get_Count(mc, &count);
1060     ok(hres == S_OK, "get_Count returned %x, expected S_OK\n", hres);
1061     ok(count == 1, "count = %d\n", count);
1062 
1063     hres = IMatchCollection2_get_Item(mc, 1, &disp);
1064     ok(hres == E_INVALIDARG, "get_Item returned %x, expected E_INVALIDARG\n", hres);
1065 
1066     hres = IMatchCollection2_get_Item(mc, 1, NULL);
1067     ok(hres == E_POINTER, "get_Item returned %x, expected E_POINTER\n", hres);
1068 
1069     hres = IMatchCollection2_get_Item(mc, 0, &disp);
1070     ok(hres == S_OK, "get_Item returned %x, expected S_OK\n", hres);
1071     hres = IDispatch_QueryInterface(disp, &IID_IMatch2, (void**)&match);
1072     ok(hres == S_OK, "QueryInterface(IID_IMatch2) returned %x\n", hres);
1073     IDispatch_Release(disp);
1074 
1075     hres = IMatch2_QueryInterface(match, &IID_IMatch, (void**)&unk);
1076     ok(hres == S_OK, "QueryInterface(IID_IMatch) returned %x\n", hres);
1077     IUnknown_Release(unk);
1078 
1079     hres = IMatch2_get_Value(match, NULL);
1080     ok(hres == E_POINTER, "get_Value returned %x, expected E_POINTER\n", hres);
1081 
1082     hres = IMatch2_get_FirstIndex(match, NULL);
1083     ok(hres == E_POINTER, "get_FirstIndex returned %x, expected E_POINTER\n", hres);
1084 
1085     hres = IMatch2_get_Length(match, NULL);
1086     ok(hres == E_POINTER, "get_Length returned %x, expected E_POINTER\n", hres);
1087 
1088     hres = IMatch2_get_SubMatches(match, NULL);
1089     ok(hres == E_POINTER, "get_SubMatches returned %x, expected E_POINTER\n", hres);
1090 
1091     hres = IMatch2_get_SubMatches(match, &disp);
1092     ok(hres == S_OK, "get_SubMatches returned %x, expected S_OK\n", hres);
1093     IMatch2_Release(match);
1094     hres = IDispatch_QueryInterface(disp, &IID_ISubMatches, (void**)&sm);
1095     ok(hres == S_OK, "QueryInterface(IID_ISubMatches) returned %x\n", hres);
1096     IDispatch_Release(disp);
1097 
1098     hres = ISubMatches_get_Item(sm, 0, &v);
1099     ok(hres == E_INVALIDARG, "get_Item returned %x, expected E_INVALIDARG\n", hres);
1100 
1101     hres = ISubMatches_get_Item(sm, 0, NULL);
1102     ok(hres == E_POINTER, "get_Item returned %x, expected E_POINTER\n", hres);
1103 
1104     hres = ISubMatches_get_Count(sm, NULL);
1105     ok(hres == E_POINTER, "get_Count returned %x, expected E_POINTER\n", hres);
1106     ISubMatches_Release(sm);
1107 
1108     hres = IMatchCollection2_get__NewEnum(mc, &unk);
1109     ok(hres == S_OK, "get__NewEnum returned %x, expected S_OK\n", hres);
1110     hres = IUnknown_QueryInterface(unk, &IID_IEnumVARIANT, (void**)&ev);
1111     ok(hres == S_OK, "QueryInterface(IID_IEnumVARIANT) returned %x\n", hres);
1112     IUnknown_Release(unk);
1113     IMatchCollection2_Release(mc);
1114 
1115     hres = IEnumVARIANT_Skip(ev, 2);
1116     ok(hres == S_OK, "Skip returned %x\n", hres);
1117 
1118     hres = IEnumVARIANT_Next(ev, 1, &v, &fetched);
1119     ok(hres == S_FALSE, "Next returned %x, expected S_FALSE\n", hres);
1120     ok(fetched == 0, "fetched = %d\n", fetched);
1121 
1122     hres = IEnumVARIANT_Skip(ev, -1);
1123     ok(hres == S_OK, "Skip returned %x\n", hres);
1124 
1125     hres = IEnumVARIANT_Next(ev, 1, &v, &fetched);
1126     ok(hres == S_OK, "Next returned %x\n", hres);
1127     ok(fetched == 1, "fetched = %d\n", fetched);
1128     VariantClear(&v);
1129     IEnumVARIANT_Release(ev);
1130 
1131     IRegExp2_Release(regexp);
1132 }
1133 
check_vbscript(void)1134 static BOOL check_vbscript(void)
1135 {
1136     IActiveScriptParseProcedure2 *vbscript;
1137     HRESULT hres;
1138 
1139     hres = CoCreateInstance(&CLSID_VBScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1140             &IID_IActiveScriptParseProcedure2, (void**)&vbscript);
1141     if(SUCCEEDED(hres))
1142         IActiveScriptParseProcedure2_Release(vbscript);
1143 
1144     return hres == S_OK;
1145 }
1146 
START_TEST(vbscript)1147 START_TEST(vbscript)
1148 {
1149     CoInitialize(NULL);
1150 
1151     if(check_vbscript()) {
1152         test_vbscript();
1153         test_vbscript_uninitializing();
1154         test_vbscript_release();
1155         test_vbscript_simplecreate();
1156         test_vbscript_initializing();
1157         test_named_items();
1158         test_scriptdisp();
1159         test_RegExp();
1160     }else {
1161         win_skip("VBScript engine not available or too old\n");
1162     }
1163 
1164     CoUninitialize();
1165 }
1166