push ab8f8bd6525a0f7af95fd0a2a63931cd3569f61f
[wine/hacks.git] / dlls / jscript / tests / run.c
blob46e33873a8ac6f43a905d5a49c5be03db5c0843c
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdio.h>
21 #define COBJMACROS
22 #define CONST_VTABLE
24 #include <ole2.h>
25 #include <dispex.h>
26 #include <activscp.h>
28 #include "wine/test.h"
30 static const CLSID CLSID_JScript =
31 {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
33 #define DEFINE_EXPECT(func) \
34 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
36 #define SET_EXPECT(func) \
37 expect_ ## func = TRUE
39 #define SET_CALLED(func) \
40 called_ ## func = TRUE
42 #define CHECK_EXPECT2(func) \
43 do { \
44 ok(expect_ ##func, "unexpected call " #func "\n"); \
45 called_ ## func = TRUE; \
46 }while(0)
48 #define CHECK_EXPECT(func) \
49 do { \
50 CHECK_EXPECT2(func); \
51 expect_ ## func = FALSE; \
52 }while(0)
54 #define CHECK_CALLED(func) \
55 do { \
56 ok(called_ ## func, "expected " #func "\n"); \
57 expect_ ## func = called_ ## func = FALSE; \
58 }while(0)
60 DEFINE_EXPECT(global_propget_d);
61 DEFINE_EXPECT(global_propget_i);
62 DEFINE_EXPECT(global_propput_d);
63 DEFINE_EXPECT(global_propput_i);
64 DEFINE_EXPECT(global_success_d);
65 DEFINE_EXPECT(global_success_i);
67 #define DISPID_GLOBAL_TESTPROPGET 0x1000
68 #define DISPID_GLOBAL_TESTPROPPUT 0x1001
69 #define DISPID_GLOBAL_REPORTSUCCESS 0x1002
70 #define DISPID_GLOBAL_TRACE 0x1003
71 #define DISPID_GLOBAL_OK 0x1004
73 static const WCHAR testW[] = {'t','e','s','t',0};
75 static BOOL strict_dispid_check;
76 static const char *test_name = "(null)";
78 static const char *debugstr_w(LPCWSTR str)
80 static char buf[1024];
82 if(!str)
83 return "(null)";
85 WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
86 return buf;
89 static BSTR a2bstr(const char *str)
91 BSTR ret;
92 int len;
94 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
95 ret = SysAllocStringLen(NULL, len-1);
96 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
98 return ret;
101 static int strcmp_wa(LPCWSTR strw, const char *stra)
103 WCHAR buf[512];
104 MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
105 return lstrcmpW(strw, buf);
108 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
110 *ppv = NULL;
112 if(IsEqualGUID(riid, &IID_IUnknown)
113 || IsEqualGUID(riid, &IID_IDispatch)
114 || IsEqualGUID(riid, &IID_IDispatchEx))
115 *ppv = iface;
116 else
117 return E_NOINTERFACE;
119 return S_OK;
122 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
124 return 2;
127 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
129 return 1;
132 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
134 ok(0, "unexpected call\n");
135 return E_NOTIMPL;
138 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
139 LCID lcid, ITypeInfo **ppTInfo)
141 ok(0, "unexpected call\n");
142 return E_NOTIMPL;
145 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
146 LPOLESTR *rgszNames, UINT cNames,
147 LCID lcid, DISPID *rgDispId)
149 ok(0, "unexpected call\n");
150 return E_NOTIMPL;
153 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
154 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
155 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
157 ok(0, "unexpected call\n");
158 return E_NOTIMPL;
161 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
163 ok(0, "unexpected call %s %x\n", debugstr_w(bstrName), grfdex);
164 return E_NOTIMPL;
167 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
169 ok(0, "unexpected call\n");
170 return E_NOTIMPL;
173 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
175 ok(0, "unexpected call\n");
176 return E_NOTIMPL;
179 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
181 ok(0, "unexpected call\n");
182 return E_NOTIMPL;
185 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
187 ok(0, "unexpected call\n");
188 return E_NOTIMPL;
191 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
193 ok(0, "unexpected call\n");
194 return E_NOTIMPL;
197 static HRESULT WINAPI Global_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
199 if(!strcmp_wa(bstrName, "ok")) {
200 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
201 *pid = DISPID_GLOBAL_OK;
202 return S_OK;
204 if(!strcmp_wa(bstrName, "trace")) {
205 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
206 *pid = DISPID_GLOBAL_TRACE;
207 return S_OK;
209 if(!strcmp_wa(bstrName, "reportSuccess")) {
210 CHECK_EXPECT(global_success_d);
211 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
212 *pid = DISPID_GLOBAL_REPORTSUCCESS;
213 return S_OK;
215 if(!strcmp_wa(bstrName, "testPropGet")) {
216 CHECK_EXPECT(global_propget_d);
217 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
218 *pid = DISPID_GLOBAL_TESTPROPGET;
219 return S_OK;
221 if(!strcmp_wa(bstrName, "testPropPut")) {
222 CHECK_EXPECT(global_propput_d);
223 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
224 *pid = DISPID_GLOBAL_TESTPROPPUT;
225 return S_OK;
228 if(strict_dispid_check)
229 ok(0, "unexpected call %s\n", debugstr_w(bstrName));
230 return DISP_E_UNKNOWNNAME;
233 static HRESULT WINAPI Global_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
234 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
236 switch(id) {
237 case DISPID_GLOBAL_OK:
238 ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
239 ok(pdp != NULL, "pdp == NULL\n");
240 ok(pdp->rgvarg != NULL, "rgvarg == NULL\n");
241 ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
242 ok(pdp->cArgs == 2, "cArgs = %d\n", pdp->cArgs);
243 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
244 ok(!pvarRes, "pvarRes != NULL\n");
245 ok(pei != NULL, "pei == NULL\n");
247 ok(V_VT(pdp->rgvarg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(pdp->rgvarg));
248 ok(V_VT(pdp->rgvarg+1) == VT_BOOL, "V_VT(psp->rgvargs+1) = %d\n", V_VT(pdp->rgvarg));
249 ok(V_BOOL(pdp->rgvarg+1), "%s: %s\n", test_name, debugstr_w(V_BSTR(pdp->rgvarg)));
251 return S_OK;
253 case DISPID_GLOBAL_TRACE:
254 ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
255 ok(pdp != NULL, "pdp == NULL\n");
256 ok(pdp->rgvarg != NULL, "rgvarg == NULL\n");
257 ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
258 ok(pdp->cArgs == 1, "cArgs = %d\n", pdp->cArgs);
259 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
260 ok(!pvarRes, "pvarRes != NULL\n");
261 ok(pei != NULL, "pei == NULL\n");
263 ok(V_VT(pdp->rgvarg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(pdp->rgvarg));
264 if(V_VT(pdp->rgvarg) == VT_BSTR)
265 trace("%s: %s\n", test_name, debugstr_w(V_BSTR(pdp->rgvarg)));
267 return S_OK;
269 case DISPID_GLOBAL_REPORTSUCCESS:
270 CHECK_EXPECT(global_success_i);
272 ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
273 ok(pdp != NULL, "pdp == NULL\n");
274 ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
275 ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
276 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
277 ok(!pvarRes, "pvarRes != NULL\n");
278 ok(pei != NULL, "pei == NULL\n");
280 return S_OK;
282 case DISPID_GLOBAL_TESTPROPGET:
283 CHECK_EXPECT(global_propget_i);
285 ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags);
286 ok(pdp != NULL, "pdp == NULL\n");
287 ok(!pdp->rgvarg, "rgvarg != NULL\n");
288 ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
289 ok(!pdp->cArgs, "cArgs = %d\n", pdp->cArgs);
290 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
291 ok(pvarRes != NULL, "pvarRes == NULL\n");
292 ok(V_VT(pvarRes) == VT_EMPTY, "V_VT(pvarRes) = %d\n", V_VT(pvarRes));
293 ok(pei != NULL, "pei == NULL\n");
295 V_VT(pvarRes) = VT_I4;
296 V_I4(pvarRes) = 1;
298 return S_OK;
300 case DISPID_GLOBAL_TESTPROPPUT:
301 CHECK_EXPECT(global_propput_i);
303 ok(wFlags == INVOKE_PROPERTYPUT, "wFlags = %x\n", wFlags);
304 ok(pdp != NULL, "pdp == NULL\n");
305 ok(pdp->rgvarg != NULL, "rgvarg == NULL\n");
306 ok(pdp->rgdispidNamedArgs != NULL, "rgdispidNamedArgs == NULL\n");
307 ok(pdp->cArgs == 1, "cArgs = %d\n", pdp->cArgs);
308 ok(pdp->cNamedArgs == 1, "cNamedArgs = %d\n", pdp->cNamedArgs);
309 ok(pdp->rgdispidNamedArgs[0] == DISPID_PROPERTYPUT, "pdp->rgdispidNamedArgs[0] = %d\n", pdp->rgdispidNamedArgs[0]);
310 ok(!pvarRes, "pvarRes != NULL\n");
312 ok(V_VT(pdp->rgvarg) == VT_I4, "V_VT(pdp->rgvarg)=%d\n", V_VT(pdp->rgvarg));
313 ok(V_I4(pdp->rgvarg) == 1, "V_I4(pdp->rgvarg)=%d\n", V_I4(pdp->rgvarg));
314 return S_OK;
317 ok(0, "unexpected call %x\n", id);
318 return DISP_E_MEMBERNOTFOUND;
321 static IDispatchExVtbl GlobalVtbl = {
322 DispatchEx_QueryInterface,
323 DispatchEx_AddRef,
324 DispatchEx_Release,
325 DispatchEx_GetTypeInfoCount,
326 DispatchEx_GetTypeInfo,
327 DispatchEx_GetIDsOfNames,
328 DispatchEx_Invoke,
329 Global_GetDispID,
330 Global_InvokeEx,
331 DispatchEx_DeleteMemberByName,
332 DispatchEx_DeleteMemberByDispID,
333 DispatchEx_GetMemberProperties,
334 DispatchEx_GetMemberName,
335 DispatchEx_GetNextDispID,
336 DispatchEx_GetNameSpaceParent
339 static IDispatchEx Global = { &GlobalVtbl };
341 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
343 *ppv = NULL;
345 if(IsEqualGUID(&IID_IUnknown, riid))
346 *ppv = iface;
347 else if(IsEqualGUID(&IID_IActiveScriptSite, riid))
348 *ppv = iface;
349 else
350 return E_NOINTERFACE;
352 IUnknown_AddRef((IUnknown*)*ppv);
353 return S_OK;
356 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
358 return 2;
361 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
363 return 1;
366 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
368 *plcid = GetUserDefaultLCID();
369 return S_OK;
372 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
373 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
375 ok(!lstrcmpW(pstrName, testW), "unexpected pstrName %s\n", debugstr_w(pstrName));
376 ok(dwReturnMask == SCRIPTINFO_IUNKNOWN, "unexpected dwReturnMask %x\n", dwReturnMask);
377 ok(!ppti, "ppti != NULL\n");
379 *ppiunkItem = (IUnknown*)&Global;
380 return S_OK;
383 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
385 return E_NOTIMPL;
388 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
389 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
391 return E_NOTIMPL;
394 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
396 return E_NOTIMPL;
399 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
401 return E_NOTIMPL;
404 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
406 return E_NOTIMPL;
409 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
411 return E_NOTIMPL;
414 #undef ACTSCPSITE_THIS
416 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
417 ActiveScriptSite_QueryInterface,
418 ActiveScriptSite_AddRef,
419 ActiveScriptSite_Release,
420 ActiveScriptSite_GetLCID,
421 ActiveScriptSite_GetItemInfo,
422 ActiveScriptSite_GetDocVersionString,
423 ActiveScriptSite_OnScriptTerminate,
424 ActiveScriptSite_OnStateChange,
425 ActiveScriptSite_OnScriptError,
426 ActiveScriptSite_OnEnterScript,
427 ActiveScriptSite_OnLeaveScript
430 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
432 static IActiveScript *create_script(void)
434 IActiveScript *script;
435 HRESULT hres;
437 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
438 &IID_IActiveScript, (void**)&script);
439 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
441 return script;
444 static void parse_script(BSTR script_str)
446 IActiveScriptParse *parser;
447 IActiveScript *engine;
448 HRESULT hres;
450 engine = create_script();
451 if(!engine)
452 return;
454 hres = IActiveScript_QueryInterface(engine, &IID_IActiveScriptParse, (void**)&parser);
455 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
457 hres = IActiveScriptParse_InitNew(parser);
458 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
460 hres = IActiveScript_SetScriptSite(engine, &ActiveScriptSite);
461 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
463 hres = IActiveScript_AddNamedItem(engine, testW,
464 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
465 ok(hres == S_OK, "AddNamedItem failed: %08x\n", hres);
467 hres = IActiveScript_SetScriptState(engine, SCRIPTSTATE_STARTED);
468 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
470 hres = IActiveScriptParse_ParseScriptText(parser, script_str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
471 ok(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
473 IActiveScript_Release(engine);
474 IActiveScriptParse_Release(parser);
477 static void parse_script_a(const char *src)
479 BSTR tmp = a2bstr(src);
480 parse_script(tmp);
481 SysFreeString(tmp);
484 static void run_from_res(const char *name)
486 const char *data;
487 DWORD size, len;
488 BSTR str;
489 HRSRC src;
491 strict_dispid_check = FALSE;
492 test_name = name;
494 src = FindResourceA(NULL, name, (LPCSTR)40);
495 ok(src != NULL, "Could not find resource %s\n", name);
497 size = SizeofResource(NULL, src);
498 data = LoadResource(NULL, src);
500 len = MultiByteToWideChar(CP_ACP, 0, data, size, NULL, 0);
501 str = SysAllocStringLen(NULL, len-1);
502 len = MultiByteToWideChar(CP_ACP, 0, data, size, str, len);
504 SET_EXPECT(global_success_d);
505 SET_EXPECT(global_success_i);
506 parse_script(str);
507 CHECK_CALLED(global_success_d);
508 CHECK_CALLED(global_success_i);
510 SysFreeString(str);
513 static void run_tests(void)
515 strict_dispid_check = TRUE;
517 parse_script_a("");
518 parse_script_a("/* empty */ ;");
520 SET_EXPECT(global_propget_d);
521 SET_EXPECT(global_propget_i);
522 parse_script_a("testPropGet;");
523 CHECK_CALLED(global_propget_d);
524 CHECK_CALLED(global_propget_i);
526 SET_EXPECT(global_propput_d);
527 SET_EXPECT(global_propput_i);
528 parse_script_a("testPropPut = 1;");
529 CHECK_CALLED(global_propput_d);
530 CHECK_CALLED(global_propput_i);
532 SET_EXPECT(global_success_d);
533 SET_EXPECT(global_success_i);
534 parse_script_a("reportSuccess();");
535 CHECK_CALLED(global_success_d);
536 CHECK_CALLED(global_success_i);
538 run_from_res("lang.js");
541 START_TEST(run)
543 CoInitialize(NULL);
545 run_tests();
547 CoUninitialize();