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 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
32 static const CLSID CLSID_JScript =
33 {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
35 #define DEFINE_EXPECT(func) \
36 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
38 #define SET_EXPECT(func) \
39 expect_ ## func = TRUE
41 #define SET_CALLED(func) \
42 called_ ## func = TRUE
44 #define CHECK_EXPECT2(func) \
46 ok(expect_ ##func, "unexpected call " #func "\n"); \
47 called_ ## func = TRUE; \
50 #define CHECK_EXPECT(func) \
52 CHECK_EXPECT2(func); \
53 expect_ ## func = FALSE; \
56 #define CHECK_CALLED(func) \
58 ok(called_ ## func, "expected " #func "\n"); \
59 expect_ ## func = called_ ## func = FALSE; \
62 DEFINE_EXPECT(GetLCID);
63 DEFINE_EXPECT(OnStateChange_STARTED);
64 DEFINE_EXPECT(OnStateChange_CONNECTED);
65 DEFINE_EXPECT(OnStateChange_DISCONNECTED);
66 DEFINE_EXPECT(OnStateChange_CLOSED);
67 DEFINE_EXPECT(OnStateChange_INITIALIZED);
68 DEFINE_EXPECT(OnEnterScript);
69 DEFINE_EXPECT(OnLeaveScript);
71 #define test_state(s,ss) _test_state(__LINE__,s,ss)
72 static void _test_state(unsigned line, IActiveScript *script, SCRIPTSTATE exstate)
74 SCRIPTSTATE state = -1;
77 hres = IActiveScript_GetScriptState(script, &state);
78 ok_(__FILE__,line) (hres == S_OK, "GetScriptState failed: %08x\n", hres);
79 ok_(__FILE__,line) (state == exstate, "state=%d, expected %d\n", state, exstate);
82 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
86 if(IsEqualGUID(&IID_IUnknown, riid))
88 else if(IsEqualGUID(&IID_IActiveScriptSite, riid))
93 IUnknown_AddRef((IUnknown*)*ppv);
97 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
102 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
107 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
109 CHECK_EXPECT(GetLCID);
113 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
114 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
116 ok(0, "unexpected call\n");
120 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
122 ok(0, "unexpected call\n");
126 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
127 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
129 ok(0, "unexpected call\n");
133 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
135 switch(ssScriptState) {
136 case SCRIPTSTATE_STARTED:
137 CHECK_EXPECT(OnStateChange_STARTED);
139 case SCRIPTSTATE_CONNECTED:
140 CHECK_EXPECT(OnStateChange_CONNECTED);
142 case SCRIPTSTATE_DISCONNECTED:
143 CHECK_EXPECT(OnStateChange_DISCONNECTED);
145 case SCRIPTSTATE_CLOSED:
146 CHECK_EXPECT(OnStateChange_CLOSED);
148 case SCRIPTSTATE_INITIALIZED:
149 CHECK_EXPECT(OnStateChange_INITIALIZED);
152 ok(0, "unexpected call %d\n", ssScriptState);
158 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
160 ok(0, "unexpected call\n");
164 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
166 CHECK_EXPECT(OnEnterScript);
170 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
172 CHECK_EXPECT(OnLeaveScript);
176 #undef ACTSCPSITE_THIS
178 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
179 ActiveScriptSite_QueryInterface,
180 ActiveScriptSite_AddRef,
181 ActiveScriptSite_Release,
182 ActiveScriptSite_GetLCID,
183 ActiveScriptSite_GetItemInfo,
184 ActiveScriptSite_GetDocVersionString,
185 ActiveScriptSite_OnScriptTerminate,
186 ActiveScriptSite_OnStateChange,
187 ActiveScriptSite_OnScriptError,
188 ActiveScriptSite_OnEnterScript,
189 ActiveScriptSite_OnLeaveScript
192 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
194 static void test_script_dispatch(IActiveScript *script, BOOL initialized)
200 disp = (void*)0xdeadbeef;
201 hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
203 ok(hres == E_UNEXPECTED, "hres = %08x, expected E_UNEXPECTED\n", hres);
204 ok(!disp, "disp != NULL\n");
208 ok(hres == S_OK, "GetScriptDispatch failed: %08x\n", hres);
212 ok(disp != NULL, "disp == NULL\n");
213 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
214 IDispatch_Release(disp);
215 ok(hres == S_OK, "Could not get IDispatchEx interface: %08x\n", hres);
217 IDispatchEx_Release(dispex);
220 static void test_safety(IUnknown *unk)
222 IObjectSafety *safety;
223 DWORD supported, enabled;
226 hres = IUnknown_QueryInterface(unk, &IID_IObjectSafety, (void**)&safety);
227 ok(hres == S_OK, "Could not get IObjectSafety: %08x\n", hres);
231 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, NULL);
232 ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
233 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, NULL, &enabled);
234 ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
236 supported = enabled = 0xdeadbeef;
237 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, &enabled);
238 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
239 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
240 "supported=%x\n", supported);
241 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
243 supported = enabled = 0xdeadbeef;
244 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScript, &supported, &enabled);
245 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
246 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
247 "supported=%x\n", supported);
248 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
250 supported = enabled = 0xdeadbeef;
251 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
252 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
253 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
254 "supported=%x\n", supported);
255 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
257 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
258 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER
259 |INTERFACESAFE_FOR_UNTRUSTED_CALLER,
260 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
261 ok(hres == E_FAIL, "SetInterfaceSafetyOptions failed: %08x, expected E_FAIL\n", hres);
263 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
264 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
265 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
266 ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
268 supported = enabled = 0xdeadbeef;
269 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
270 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
271 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
272 "supported=%x\n", supported);
273 ok(enabled == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
274 "enabled=%x\n", enabled);
276 IObjectSafety_Release(safety);
279 static void test_jscript(void)
281 IActiveScriptParse *parse;
282 IActiveScript *script;
287 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
288 &IID_IUnknown, (void**)&unk);
289 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
293 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
294 ok(hres == S_OK, "Could not get IActiveScript: %08x\n", hres);
296 hres = IUnknown_QueryInterface(unk, &IID_IActiveScriptParse, (void**)&parse);
297 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
300 IActiveScript_Release(script);
304 test_state(script, SCRIPTSTATE_UNINITIALIZED);
307 hres = IActiveScriptParse64_InitNew(parse);
308 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
310 hres = IActiveScriptParse64_InitNew(parse);
311 ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
313 hres = IActiveScript_SetScriptSite(script, NULL);
314 ok(hres == E_POINTER, "SetScriptSite failed: %08x, expected E_POINTER\n", hres);
316 test_state(script, SCRIPTSTATE_UNINITIALIZED);
317 test_script_dispatch(script, FALSE);
320 SET_EXPECT(OnStateChange_INITIALIZED);
321 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
322 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
323 CHECK_CALLED(GetLCID);
324 CHECK_CALLED(OnStateChange_INITIALIZED);
326 test_state(script, SCRIPTSTATE_INITIALIZED);
328 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
329 ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
331 test_script_dispatch(script, TRUE);
333 SET_EXPECT(OnStateChange_STARTED);
334 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
335 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
336 CHECK_CALLED(OnStateChange_STARTED);
338 test_state(script, SCRIPTSTATE_STARTED);
340 SET_EXPECT(OnStateChange_CLOSED);
341 hres = IActiveScript_Close(script);
342 ok(hres == S_OK, "Close failed: %08x\n", hres);
343 CHECK_CALLED(OnStateChange_CLOSED);
345 test_state(script, SCRIPTSTATE_CLOSED);
346 test_script_dispatch(script, FALSE);
348 IUnknown_Release(parse);
349 IActiveScript_Release(script);
351 ref = IUnknown_Release(unk);
352 ok(!ref, "ref = %d\n", ref);
355 static void test_jscript2(void)
357 IActiveScriptParse *parse;
358 IActiveScript *script;
363 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
364 &IID_IUnknown, (void**)&unk);
365 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
369 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
370 ok(hres == S_OK, "Could not get IActiveScript: %08x\n", hres);
372 hres = IUnknown_QueryInterface(unk, &IID_IActiveScriptParse, (void**)&parse);
373 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
376 IActiveScript_Release(script);
380 test_state(script, SCRIPTSTATE_UNINITIALIZED);
383 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
384 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
385 CHECK_CALLED(GetLCID);
387 test_state(script, SCRIPTSTATE_UNINITIALIZED);
389 SET_EXPECT(OnStateChange_INITIALIZED);
390 hres = IActiveScriptParse64_InitNew(parse);
391 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
392 CHECK_CALLED(OnStateChange_INITIALIZED);
394 hres = IActiveScriptParse64_InitNew(parse);
395 ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
397 SET_EXPECT(OnStateChange_CONNECTED);
398 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_CONNECTED);
399 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
400 CHECK_CALLED(OnStateChange_CONNECTED);
402 test_state(script, SCRIPTSTATE_CONNECTED);
404 SET_EXPECT(OnStateChange_DISCONNECTED);
405 SET_EXPECT(OnStateChange_INITIALIZED);
406 SET_EXPECT(OnStateChange_CLOSED);
407 hres = IActiveScript_Close(script);
408 ok(hres == S_OK, "Close failed: %08x\n", hres);
409 CHECK_CALLED(OnStateChange_DISCONNECTED);
410 CHECK_CALLED(OnStateChange_INITIALIZED);
411 CHECK_CALLED(OnStateChange_CLOSED);
413 test_state(script, SCRIPTSTATE_CLOSED);
414 test_script_dispatch(script, FALSE);
416 IUnknown_Release(parse);
417 IActiveScript_Release(script);
419 ref = IUnknown_Release(unk);
420 ok(!ref, "ref = %d\n", ref);