push 212f1dad91f15aefd8e676124180e0b86d7c9ee6
[wine/hacks.git] / dlls / jscript / tests / run.c
blob66c312ebfe9b35bbae7b57f00d2f7472d4e33403
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);
66 DEFINE_EXPECT(testobj_delete);
67 DEFINE_EXPECT(GetItemInfo_testVal);
69 #define DISPID_GLOBAL_TESTPROPGET 0x1000
70 #define DISPID_GLOBAL_TESTPROPPUT 0x1001
71 #define DISPID_GLOBAL_REPORTSUCCESS 0x1002
72 #define DISPID_GLOBAL_TRACE 0x1003
73 #define DISPID_GLOBAL_OK 0x1004
74 #define DISPID_GLOBAL_GETVT 0x1005
75 #define DISPID_GLOBAL_TESTOBJ 0x1006
77 static const WCHAR testW[] = {'t','e','s','t',0};
78 static const CHAR testA[] = "test";
79 static const WCHAR test_valW[] = {'t','e','s','t','V','a','l',0};
80 static const CHAR test_valA[] = "testVal";
82 static BOOL strict_dispid_check;
83 static const char *test_name = "(null)";
85 static const char *debugstr_w(LPCWSTR str)
87 static char buf[1024];
89 if(!str)
90 return "(null)";
92 WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
93 return buf;
96 static BSTR a2bstr(const char *str)
98 BSTR ret;
99 int len;
101 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
102 ret = SysAllocStringLen(NULL, len-1);
103 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
105 return ret;
108 static int strcmp_wa(LPCWSTR strw, const char *stra)
110 CHAR buf[512];
111 WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), 0, 0);
112 return lstrcmpA(buf, stra);
115 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
117 *ppv = NULL;
119 if(IsEqualGUID(riid, &IID_IUnknown)
120 || IsEqualGUID(riid, &IID_IDispatch)
121 || IsEqualGUID(riid, &IID_IDispatchEx))
122 *ppv = iface;
123 else
124 return E_NOINTERFACE;
126 return S_OK;
129 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
131 return 2;
134 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
136 return 1;
139 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
141 ok(0, "unexpected call\n");
142 return E_NOTIMPL;
145 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
146 LCID lcid, ITypeInfo **ppTInfo)
148 ok(0, "unexpected call\n");
149 return E_NOTIMPL;
152 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
153 LPOLESTR *rgszNames, UINT cNames,
154 LCID lcid, DISPID *rgDispId)
156 ok(0, "unexpected call\n");
157 return E_NOTIMPL;
160 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
161 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
162 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
164 ok(0, "unexpected call\n");
165 return E_NOTIMPL;
168 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
170 ok(0, "unexpected call %s %x\n", debugstr_w(bstrName), grfdex);
171 return E_NOTIMPL;
174 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
176 ok(0, "unexpected call\n");
177 return E_NOTIMPL;
180 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
182 ok(0, "unexpected call\n");
183 return E_NOTIMPL;
186 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
188 ok(0, "unexpected call\n");
189 return E_NOTIMPL;
192 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
194 ok(0, "unexpected call\n");
195 return E_NOTIMPL;
198 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
200 ok(0, "unexpected call\n");
201 return E_NOTIMPL;
204 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
206 ok(0, "unexpected call\n");
207 return E_NOTIMPL;
210 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
211 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
213 ok(0, "unexpected call\n");
214 return E_NOTIMPL;
217 static HRESULT WINAPI testObj_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
219 CHECK_EXPECT(testobj_delete);
221 ok(!strcmp_wa(bstrName, "deleteTest"), "unexpected name %s\n", debugstr_w(bstrName));
222 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
223 return S_OK;
226 static IDispatchExVtbl testObjVtbl = {
227 DispatchEx_QueryInterface,
228 DispatchEx_AddRef,
229 DispatchEx_Release,
230 DispatchEx_GetTypeInfoCount,
231 DispatchEx_GetTypeInfo,
232 DispatchEx_GetIDsOfNames,
233 DispatchEx_Invoke,
234 DispatchEx_GetDispID,
235 DispatchEx_InvokeEx,
236 testObj_DeleteMemberByName,
237 DispatchEx_DeleteMemberByDispID,
238 DispatchEx_GetMemberProperties,
239 DispatchEx_GetMemberName,
240 DispatchEx_GetNextDispID,
241 DispatchEx_GetNameSpaceParent
244 static IDispatchEx testObj = { &testObjVtbl };
246 static HRESULT WINAPI Global_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
248 if(!strcmp_wa(bstrName, "ok")) {
249 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
250 *pid = DISPID_GLOBAL_OK;
251 return S_OK;
253 if(!strcmp_wa(bstrName, "trace")) {
254 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
255 *pid = DISPID_GLOBAL_TRACE;
256 return S_OK;
258 if(!strcmp_wa(bstrName, "reportSuccess")) {
259 CHECK_EXPECT(global_success_d);
260 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
261 *pid = DISPID_GLOBAL_REPORTSUCCESS;
262 return S_OK;
264 if(!strcmp_wa(bstrName, "testPropGet")) {
265 CHECK_EXPECT(global_propget_d);
266 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
267 *pid = DISPID_GLOBAL_TESTPROPGET;
268 return S_OK;
270 if(!strcmp_wa(bstrName, "testPropPut")) {
271 CHECK_EXPECT(global_propput_d);
272 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
273 *pid = DISPID_GLOBAL_TESTPROPPUT;
274 return S_OK;
276 if(!strcmp_wa(bstrName, "getVT")) {
277 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
278 *pid = DISPID_GLOBAL_GETVT;
279 return S_OK;
281 if(!strcmp_wa(bstrName, "testObj")) {
282 ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
283 *pid = DISPID_GLOBAL_TESTOBJ;
284 return S_OK;
287 if(strict_dispid_check)
288 ok(0, "unexpected call %s\n", debugstr_w(bstrName));
289 return DISP_E_UNKNOWNNAME;
292 static HRESULT WINAPI Global_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
293 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
295 switch(id) {
296 case DISPID_GLOBAL_OK:
297 ok(wFlags == INVOKE_FUNC || wFlags == (INVOKE_FUNC|INVOKE_PROPERTYGET), "wFlags = %x\n", wFlags);
298 ok(pdp != NULL, "pdp == NULL\n");
299 ok(pdp->rgvarg != NULL, "rgvarg == NULL\n");
300 ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
301 ok(pdp->cArgs == 2, "cArgs = %d\n", pdp->cArgs);
302 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
303 if(wFlags & INVOKE_PROPERTYGET)
304 ok(pvarRes != NULL, "pvarRes == NULL\n");
305 else
306 ok(!pvarRes, "pvarRes != NULL\n");
307 ok(pei != NULL, "pei == NULL\n");
309 ok(V_VT(pdp->rgvarg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(pdp->rgvarg));
310 ok(V_VT(pdp->rgvarg+1) == VT_BOOL, "V_VT(psp->rgvargs+1) = %d\n", V_VT(pdp->rgvarg));
311 ok(V_BOOL(pdp->rgvarg+1), "%s: %s\n", test_name, debugstr_w(V_BSTR(pdp->rgvarg)));
313 return S_OK;
315 case DISPID_GLOBAL_TRACE:
316 ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
317 ok(pdp != NULL, "pdp == NULL\n");
318 ok(pdp->rgvarg != NULL, "rgvarg == NULL\n");
319 ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
320 ok(pdp->cArgs == 1, "cArgs = %d\n", pdp->cArgs);
321 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
322 ok(!pvarRes, "pvarRes != NULL\n");
323 ok(pei != NULL, "pei == NULL\n");
325 ok(V_VT(pdp->rgvarg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(pdp->rgvarg));
326 if(V_VT(pdp->rgvarg) == VT_BSTR)
327 trace("%s: %s\n", test_name, debugstr_w(V_BSTR(pdp->rgvarg)));
329 return S_OK;
331 case DISPID_GLOBAL_REPORTSUCCESS:
332 CHECK_EXPECT(global_success_i);
334 ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
335 ok(pdp != NULL, "pdp == NULL\n");
336 ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
337 ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
338 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
339 ok(!pvarRes, "pvarRes != NULL\n");
340 ok(pei != NULL, "pei == NULL\n");
342 return S_OK;
344 case DISPID_GLOBAL_TESTPROPGET:
345 CHECK_EXPECT(global_propget_i);
347 ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags);
348 ok(pdp != NULL, "pdp == NULL\n");
349 ok(!pdp->rgvarg, "rgvarg != NULL\n");
350 ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
351 ok(!pdp->cArgs, "cArgs = %d\n", pdp->cArgs);
352 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
353 ok(pvarRes != NULL, "pvarRes == NULL\n");
354 ok(V_VT(pvarRes) == VT_EMPTY, "V_VT(pvarRes) = %d\n", V_VT(pvarRes));
355 ok(pei != NULL, "pei == NULL\n");
357 V_VT(pvarRes) = VT_I4;
358 V_I4(pvarRes) = 1;
360 return S_OK;
362 case DISPID_GLOBAL_TESTPROPPUT:
363 CHECK_EXPECT(global_propput_i);
365 ok(wFlags == INVOKE_PROPERTYPUT, "wFlags = %x\n", wFlags);
366 ok(pdp != NULL, "pdp == NULL\n");
367 ok(pdp->rgvarg != NULL, "rgvarg == NULL\n");
368 ok(pdp->rgdispidNamedArgs != NULL, "rgdispidNamedArgs == NULL\n");
369 ok(pdp->cArgs == 1, "cArgs = %d\n", pdp->cArgs);
370 ok(pdp->cNamedArgs == 1, "cNamedArgs = %d\n", pdp->cNamedArgs);
371 ok(pdp->rgdispidNamedArgs[0] == DISPID_PROPERTYPUT, "pdp->rgdispidNamedArgs[0] = %d\n", pdp->rgdispidNamedArgs[0]);
372 ok(!pvarRes, "pvarRes != NULL\n");
374 ok(V_VT(pdp->rgvarg) == VT_I4, "V_VT(pdp->rgvarg)=%d\n", V_VT(pdp->rgvarg));
375 ok(V_I4(pdp->rgvarg) == 1, "V_I4(pdp->rgvarg)=%d\n", V_I4(pdp->rgvarg));
376 return S_OK;
378 case DISPID_GLOBAL_GETVT:
379 ok(pdp != NULL, "pdp == NULL\n");
380 ok(pdp->rgvarg != NULL, "rgvarg == NULL\n");
381 ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
382 ok(pdp->cArgs == 1, "cArgs = %d\n", pdp->cArgs);
383 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
384 ok(pvarRes != NULL, "pvarRes == NULL\n");
385 ok(V_VT(pvarRes) == VT_EMPTY, "V_VT(pvarRes) = %d\n", V_VT(pvarRes));
386 ok(pei != NULL, "pei == NULL\n");
388 V_VT(pvarRes) = VT_BSTR;
389 switch(V_VT(pdp->rgvarg)) {
390 case VT_EMPTY:
391 V_BSTR(pvarRes) = a2bstr("VT_EMPTY");
392 break;
393 case VT_NULL:
394 V_BSTR(pvarRes) = a2bstr("VT_NULL");
395 break;
396 case VT_I4:
397 V_BSTR(pvarRes) = a2bstr("VT_I4");
398 break;
399 case VT_R8:
400 V_BSTR(pvarRes) = a2bstr("VT_R8");
401 break;
402 case VT_BSTR:
403 V_BSTR(pvarRes) = a2bstr("VT_BSTR");
404 break;
405 case VT_DISPATCH:
406 V_BSTR(pvarRes) = a2bstr("VT_DISPATCH");
407 break;
408 case VT_BOOL:
409 V_BSTR(pvarRes) = a2bstr("VT_BOOL");
410 break;
411 default:
412 ok(0, "unknown vt %d\n", V_VT(pdp->rgvarg));
413 return E_FAIL;
416 return S_OK;
418 case DISPID_GLOBAL_TESTOBJ:
419 ok(wFlags == INVOKE_PROPERTYGET, "wFlags = %x\n", wFlags);
420 ok(pdp != NULL, "pdp == NULL\n");
421 ok(!pdp->rgvarg, "rgvarg != NULL\n");
422 ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
423 ok(!pdp->cArgs, "cArgs = %d\n", pdp->cArgs);
424 ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
425 ok(pvarRes != NULL, "pvarRes == NULL\n");
426 ok(V_VT(pvarRes) == VT_EMPTY, "V_VT(pvarRes) = %d\n", V_VT(pvarRes));
427 ok(pei != NULL, "pei == NULL\n");
429 V_VT(pvarRes) = VT_DISPATCH;
430 V_DISPATCH(pvarRes) = (IDispatch*)&testObj;
431 return S_OK;
434 ok(0, "unexpected call %x\n", id);
435 return DISP_E_MEMBERNOTFOUND;
438 static IDispatchExVtbl GlobalVtbl = {
439 DispatchEx_QueryInterface,
440 DispatchEx_AddRef,
441 DispatchEx_Release,
442 DispatchEx_GetTypeInfoCount,
443 DispatchEx_GetTypeInfo,
444 DispatchEx_GetIDsOfNames,
445 DispatchEx_Invoke,
446 Global_GetDispID,
447 Global_InvokeEx,
448 DispatchEx_DeleteMemberByName,
449 DispatchEx_DeleteMemberByDispID,
450 DispatchEx_GetMemberProperties,
451 DispatchEx_GetMemberName,
452 DispatchEx_GetNextDispID,
453 DispatchEx_GetNameSpaceParent
456 static IDispatchEx Global = { &GlobalVtbl };
458 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
460 *ppv = NULL;
462 if(IsEqualGUID(&IID_IUnknown, riid))
463 *ppv = iface;
464 else if(IsEqualGUID(&IID_IActiveScriptSite, riid))
465 *ppv = iface;
466 else
467 return E_NOINTERFACE;
469 IUnknown_AddRef((IUnknown*)*ppv);
470 return S_OK;
473 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
475 return 2;
478 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
480 return 1;
483 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
485 *plcid = GetUserDefaultLCID();
486 return S_OK;
489 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
490 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
492 ok(dwReturnMask == SCRIPTINFO_IUNKNOWN, "unexpected dwReturnMask %x\n", dwReturnMask);
493 ok(!ppti, "ppti != NULL\n");
495 if(!strcmp_wa(pstrName, test_valA))
496 CHECK_EXPECT(GetItemInfo_testVal);
497 else if(strcmp_wa(pstrName, testA))
498 ok(0, "unexpected pstrName %s\n", debugstr_w(pstrName));
500 *ppiunkItem = (IUnknown*)&Global;
501 return S_OK;
504 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
506 return E_NOTIMPL;
509 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
510 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
512 return E_NOTIMPL;
515 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
517 return E_NOTIMPL;
520 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
522 return E_NOTIMPL;
525 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
527 return E_NOTIMPL;
530 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
532 return E_NOTIMPL;
535 #undef ACTSCPSITE_THIS
537 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
538 ActiveScriptSite_QueryInterface,
539 ActiveScriptSite_AddRef,
540 ActiveScriptSite_Release,
541 ActiveScriptSite_GetLCID,
542 ActiveScriptSite_GetItemInfo,
543 ActiveScriptSite_GetDocVersionString,
544 ActiveScriptSite_OnScriptTerminate,
545 ActiveScriptSite_OnStateChange,
546 ActiveScriptSite_OnScriptError,
547 ActiveScriptSite_OnEnterScript,
548 ActiveScriptSite_OnLeaveScript
551 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
553 static IActiveScript *create_script(void)
555 IActiveScript *script;
556 HRESULT hres;
558 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
559 &IID_IActiveScript, (void**)&script);
560 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
562 return script;
565 static void parse_script(BSTR script_str)
567 IActiveScriptParse *parser;
568 IActiveScript *engine;
569 HRESULT hres;
571 engine = create_script();
572 if(!engine)
573 return;
575 hres = IActiveScript_QueryInterface(engine, &IID_IActiveScriptParse, (void**)&parser);
576 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
577 if (FAILED(hres))
579 IActiveScript_Release(engine);
580 return;
583 hres = IActiveScriptParse_InitNew(parser);
584 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
586 hres = IActiveScript_SetScriptSite(engine, &ActiveScriptSite);
587 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
589 hres = IActiveScript_AddNamedItem(engine, testW,
590 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
591 ok(hres == S_OK, "AddNamedItem failed: %08x\n", hres);
593 hres = IActiveScript_SetScriptState(engine, SCRIPTSTATE_STARTED);
594 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
596 hres = IActiveScriptParse_ParseScriptText(parser, script_str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
597 ok(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
599 IActiveScript_Release(engine);
600 IActiveScriptParse_Release(parser);
603 static void parse_script_a(const char *src)
605 BSTR tmp = a2bstr(src);
606 parse_script(tmp);
607 SysFreeString(tmp);
610 static BSTR get_script_from_file(const char *filename)
612 DWORD size, len;
613 HANDLE file, map;
614 const char *file_map;
615 BSTR ret;
617 file = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
618 if(file == INVALID_HANDLE_VALUE) {
619 trace("Could not open file: %u\n", GetLastError());
620 return NULL;
623 size = GetFileSize(file, NULL);
625 map = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
626 CloseHandle(file);
627 if(map == INVALID_HANDLE_VALUE) {
628 trace("Could not create file mapping: %u\n", GetLastError());
629 return NULL;
632 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
633 CloseHandle(map);
634 if(!file_map) {
635 trace("MapViewOfFile failed: %u\n", GetLastError());
636 return NULL;
639 len = MultiByteToWideChar(CP_ACP, 0, file_map, size, NULL, 0);
640 ret = SysAllocStringLen(NULL, len);
641 MultiByteToWideChar(CP_ACP, 0, file_map, size, ret, len);
643 UnmapViewOfFile(file_map);
645 return ret;
648 static void run_from_file(const char *filename)
650 BSTR script_str = get_script_from_file(filename);
652 strict_dispid_check = FALSE;
654 if(script_str)
655 parse_script(script_str);
657 SysFreeString(script_str);
660 static void run_from_res(const char *name)
662 const char *data;
663 DWORD size, len;
664 BSTR str;
665 HRSRC src;
667 strict_dispid_check = FALSE;
668 test_name = name;
670 src = FindResourceA(NULL, name, (LPCSTR)40);
671 ok(src != NULL, "Could not find resource %s\n", name);
673 size = SizeofResource(NULL, src);
674 data = LoadResource(NULL, src);
676 len = MultiByteToWideChar(CP_ACP, 0, data, size, NULL, 0);
677 str = SysAllocStringLen(NULL, len);
678 len = MultiByteToWideChar(CP_ACP, 0, data, size, str, len);
680 SET_EXPECT(global_success_d);
681 SET_EXPECT(global_success_i);
682 parse_script(str);
683 CHECK_CALLED(global_success_d);
684 CHECK_CALLED(global_success_i);
686 SysFreeString(str);
689 static void test_isvisible(BOOL global_members)
691 IActiveScriptParse *parser;
692 IActiveScript *engine;
693 HRESULT hres;
695 static const WCHAR script_textW[] =
696 {'v','a','r',' ','v',' ','=',' ','t','e','s','t','V','a','l',';',0};
698 engine = create_script();
699 if(!engine)
700 return;
702 hres = IActiveScript_QueryInterface(engine, &IID_IActiveScriptParse, (void**)&parser);
703 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
704 if (FAILED(hres))
706 IActiveScript_Release(engine);
707 return;
710 hres = IActiveScriptParse_InitNew(parser);
711 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
713 hres = IActiveScript_SetScriptSite(engine, &ActiveScriptSite);
714 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
716 if(global_members)
717 SET_EXPECT(GetItemInfo_testVal);
718 hres = IActiveScript_AddNamedItem(engine, test_valW,
719 SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|
720 (global_members ? SCRIPTITEM_GLOBALMEMBERS : 0));
721 ok(hres == S_OK, "AddNamedItem failed: %08x\n", hres);
722 if(global_members)
723 CHECK_CALLED(GetItemInfo_testVal);
725 hres = IActiveScript_SetScriptState(engine, SCRIPTSTATE_STARTED);
726 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
728 if(!global_members)
729 SET_EXPECT(GetItemInfo_testVal);
730 hres = IActiveScriptParse_ParseScriptText(parser, script_textW, NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
731 ok(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
732 if(!global_members)
733 CHECK_CALLED(GetItemInfo_testVal);
735 hres = IActiveScriptParse_ParseScriptText(parser, script_textW, NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
736 ok(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
738 IActiveScript_Release(engine);
739 IActiveScriptParse_Release(parser);
742 static void run_tests(void)
744 strict_dispid_check = TRUE;
746 parse_script_a("");
747 parse_script_a("/* empty */ ;");
749 SET_EXPECT(global_propget_d);
750 SET_EXPECT(global_propget_i);
751 parse_script_a("testPropGet;");
752 CHECK_CALLED(global_propget_d);
753 CHECK_CALLED(global_propget_i);
755 SET_EXPECT(global_propput_d);
756 SET_EXPECT(global_propput_i);
757 parse_script_a("testPropPut = 1;");
758 CHECK_CALLED(global_propput_d);
759 CHECK_CALLED(global_propput_i);
761 SET_EXPECT(global_success_d);
762 SET_EXPECT(global_success_i);
763 parse_script_a("reportSuccess();");
764 CHECK_CALLED(global_success_d);
765 CHECK_CALLED(global_success_i);
767 SET_EXPECT(testobj_delete);
768 parse_script_a("delete testObj.deleteTest;");
769 CHECK_CALLED(testobj_delete);
771 parse_script_a("ok(typeof(test) === 'object', \"typeof(test) != 'object'\");");
773 run_from_res("lang.js");
774 run_from_res("api.js");
775 run_from_res("regexp.js");
777 test_isvisible(FALSE);
778 test_isvisible(TRUE);
781 START_TEST(run)
783 int argc;
784 char **argv;
786 argc = winetest_get_mainargs(&argv);
788 CoInitialize(NULL);
790 if(argc > 2)
791 run_from_file(argv[2]);
792 else
793 run_tests();
795 CoUninitialize();