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_CLOSED);
65 DEFINE_EXPECT(OnStateChange_INITIALIZED);
66 DEFINE_EXPECT(OnEnterScript);
67 DEFINE_EXPECT(OnLeaveScript);
69 #define test_state(s,ss) _test_state(__LINE__,s,ss)
70 static void _test_state(unsigned line, IActiveScript *script, SCRIPTSTATE exstate)
72 SCRIPTSTATE state = -1;
75 hres = IActiveScript_GetScriptState(script, &state);
76 ok_(__FILE__,line) (hres == S_OK, "GetScriptState failed: %08x\n", hres);
77 ok_(__FILE__,line) (state == exstate, "state=%d, expected %d\n", state, exstate);
80 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
84 if(IsEqualGUID(&IID_IUnknown, riid))
86 else if(IsEqualGUID(&IID_IActiveScriptSite, riid))
91 IUnknown_AddRef((IUnknown*)*ppv);
95 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
100 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
105 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
107 CHECK_EXPECT(GetLCID);
111 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
112 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
114 ok(0, "unexpected call\n");
118 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
120 ok(0, "unexpected call\n");
124 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
125 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
127 ok(0, "unexpected call\n");
131 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
133 switch(ssScriptState) {
134 case SCRIPTSTATE_STARTED:
135 CHECK_EXPECT(OnStateChange_STARTED);
137 case SCRIPTSTATE_CLOSED:
138 CHECK_EXPECT(OnStateChange_CLOSED);
140 case SCRIPTSTATE_INITIALIZED:
141 CHECK_EXPECT(OnStateChange_INITIALIZED);
144 ok(0, "unexpected call %d\n", ssScriptState);
150 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
152 ok(0, "unexpected call\n");
156 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
158 CHECK_EXPECT(OnEnterScript);
162 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
164 CHECK_EXPECT(OnLeaveScript);
168 #undef ACTSCPSITE_THIS
170 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
171 ActiveScriptSite_QueryInterface,
172 ActiveScriptSite_AddRef,
173 ActiveScriptSite_Release,
174 ActiveScriptSite_GetLCID,
175 ActiveScriptSite_GetItemInfo,
176 ActiveScriptSite_GetDocVersionString,
177 ActiveScriptSite_OnScriptTerminate,
178 ActiveScriptSite_OnStateChange,
179 ActiveScriptSite_OnScriptError,
180 ActiveScriptSite_OnEnterScript,
181 ActiveScriptSite_OnLeaveScript
184 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
186 static void test_script_dispatch(IActiveScript *script, BOOL initialized)
192 disp = (void*)0xdeadbeef;
193 hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
195 ok(hres == E_UNEXPECTED, "hres = %08x, expected E_UNEXPECTED\n", hres);
196 ok(!disp, "disp != NULL\n");
200 ok(hres == S_OK, "GetScriptDispatch failed: %08x\n", hres);
204 ok(disp != NULL, "disp == NULL\n");
205 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
206 IDispatch_Release(disp);
207 ok(hres == S_OK, "Could not get IDispatchEx interface: %08x\n", hres);
209 IDispatchEx_Release(dispex);
212 static void test_safety(IUnknown *unk)
214 IObjectSafety *safety;
215 DWORD supported, enabled;
218 hres = IUnknown_QueryInterface(unk, &IID_IObjectSafety, (void**)&safety);
219 ok(hres == S_OK, "Could not get IObjectSafety: %08x\n", hres);
223 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, NULL);
224 ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
225 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, NULL, &enabled);
226 ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
228 supported = enabled = 0xdeadbeef;
229 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, &enabled);
230 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
231 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
232 "supported=%x\n", supported);
233 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
235 supported = enabled = 0xdeadbeef;
236 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScript, &supported, &enabled);
237 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
238 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
239 "supported=%x\n", supported);
240 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
242 supported = enabled = 0xdeadbeef;
243 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
244 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
245 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
246 "supported=%x\n", supported);
247 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
249 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
250 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER
251 |INTERFACESAFE_FOR_UNTRUSTED_CALLER,
252 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
253 ok(hres == E_FAIL, "SetInterfaceSafetyOptions failed: %08x, expected E_FAIL\n", hres);
255 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
256 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
257 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
258 ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
260 supported = enabled = 0xdeadbeef;
261 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
262 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
263 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
264 "supported=%x\n", supported);
265 ok(enabled == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
266 "enabled=%x\n", enabled);
268 IObjectSafety_Release(safety);
271 static void test_jscript(void)
273 IActiveScriptParse *parse;
274 IActiveScript *script;
279 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
280 &IID_IUnknown, (void**)&unk);
281 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
285 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
286 ok(hres == S_OK, "Could not get IActiveScript: %08x\n", hres);
288 hres = IUnknown_QueryInterface(unk, &IID_IActiveScriptParse, (void**)&parse);
289 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
291 test_state(script, SCRIPTSTATE_UNINITIALIZED);
294 hres = IActiveScriptParse_InitNew(parse);
295 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
297 hres = IActiveScriptParse_InitNew(parse);
298 ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
300 hres = IActiveScript_SetScriptSite(script, NULL);
301 ok(hres == E_POINTER, "SetScriptSite failed: %08x, expected E_POINTER\n", hres);
303 test_state(script, SCRIPTSTATE_UNINITIALIZED);
304 test_script_dispatch(script, FALSE);
307 SET_EXPECT(OnStateChange_INITIALIZED);
308 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
309 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
310 CHECK_CALLED(GetLCID);
311 CHECK_CALLED(OnStateChange_INITIALIZED);
313 test_state(script, SCRIPTSTATE_INITIALIZED);
315 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
316 ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
318 test_script_dispatch(script, TRUE);
320 SET_EXPECT(OnStateChange_STARTED);
321 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
322 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
323 CHECK_CALLED(OnStateChange_STARTED);
325 test_state(script, SCRIPTSTATE_STARTED);
327 SET_EXPECT(OnStateChange_CLOSED);
328 hres = IActiveScript_Close(script);
329 ok(hres == S_OK, "Close failed: %08x\n", hres);
330 CHECK_CALLED(OnStateChange_CLOSED);
332 test_state(script, SCRIPTSTATE_CLOSED);
333 test_script_dispatch(script, FALSE);
335 IActiveScriptParse_Release(parse);
336 IActiveScript_Release(script);
338 ref = IUnknown_Release(unk);
339 ok(!ref, "ref = %d\n", ref);