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
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) \
44 ok(expect_ ##func, "unexpected call " #func "\n"); \
45 called_ ## func = TRUE; \
48 #define CHECK_EXPECT(func) \
50 CHECK_EXPECT2(func); \
51 expect_ ## func = FALSE; \
54 #define CHECK_CALLED(func) \
56 ok(called_ ## func, "expected " #func "\n"); \
57 expect_ ## func = called_ ## func = FALSE; \
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];
85 WideCharToMultiByte(CP_ACP
, 0, str
, -1, buf
, sizeof(buf
), NULL
, NULL
);
89 static BSTR
a2bstr(const char *str
)
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
);
101 static int strcmp_wa(LPCWSTR strw
, const char *stra
)
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
)
112 if(IsEqualGUID(riid
, &IID_IUnknown
)
113 || IsEqualGUID(riid
, &IID_IDispatch
)
114 || IsEqualGUID(riid
, &IID_IDispatchEx
))
117 return E_NOINTERFACE
;
122 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
127 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
132 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
134 ok(0, "unexpected call\n");
138 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
139 LCID lcid
, ITypeInfo
**ppTInfo
)
141 ok(0, "unexpected call\n");
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");
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");
161 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
163 ok(0, "unexpected call %s %x\n", debugstr_w(bstrName
), grfdex
);
167 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
169 ok(0, "unexpected call\n");
173 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
175 ok(0, "unexpected call\n");
179 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
181 ok(0, "unexpected call\n");
185 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
187 ok(0, "unexpected call\n");
191 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
193 ok(0, "unexpected call\n");
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
;
204 if(!strcmp_wa(bstrName
, "trace")) {
205 ok(grfdex
== fdexNameCaseSensitive
, "grfdex = %x\n", grfdex
);
206 *pid
= DISPID_GLOBAL_TRACE
;
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
;
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
;
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
;
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
)
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
)));
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
)));
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");
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
;
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
));
317 ok(0, "unexpected call %x\n", id
);
318 return DISP_E_MEMBERNOTFOUND
;
321 static IDispatchExVtbl GlobalVtbl
= {
322 DispatchEx_QueryInterface
,
325 DispatchEx_GetTypeInfoCount
,
326 DispatchEx_GetTypeInfo
,
327 DispatchEx_GetIDsOfNames
,
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
)
345 if(IsEqualGUID(&IID_IUnknown
, riid
))
347 else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
))
350 return E_NOINTERFACE
;
352 IUnknown_AddRef((IUnknown
*)*ppv
);
356 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
361 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
366 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
368 *plcid
= GetUserDefaultLCID();
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
;
383 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
388 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
389 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
394 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
399 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
404 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
409 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
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
;
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
);
444 static void parse_script(BSTR script_str
)
446 IActiveScriptParse
*parser
;
447 IActiveScript
*engine
;
450 engine
= create_script();
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
);
484 static void run_from_res(const char *name
)
491 strict_dispid_check
= FALSE
;
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
);
507 CHECK_CALLED(global_success_d
);
508 CHECK_CALLED(global_success_i
);
513 static void run_tests(void)
515 strict_dispid_check
= TRUE
;
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");