[JSCRIPT_WINETEST] Sync with Wine Staging 1.9.23. CORE-12409
[reactos.git] / rostests / winetests / jscript / jscript.c
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #define WIN32_NO_STATUS
20 #define _INC_WINDOWS
21 #define COM_NO_WINDOWS_H
22
23 #define COBJMACROS
24 #define CONST_VTABLE
25
26 #include <stdarg.h>
27
28 #include <windef.h>
29 #include <winbase.h>
30 #include <winnls.h>
31 #include <initguid.h>
32 #include <ole2.h>
33 #include <activscp.h>
34 #include <objsafe.h>
35 #include <dispex.h>
36
37 #include <wine/test.h>
38
39 #ifdef _WIN64
40
41 #define IActiveScriptParse_QueryInterface IActiveScriptParse64_QueryInterface
42 #define IActiveScriptParse_Release IActiveScriptParse64_Release
43 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
44 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
45
46 #else
47
48 #define IActiveScriptParse_QueryInterface IActiveScriptParse32_QueryInterface
49 #define IActiveScriptParse_Release IActiveScriptParse32_Release
50 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
51 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
52
53 #endif
54
55 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
56
57 static const CLSID CLSID_JScript =
58 {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
59 static const CLSID CLSID_JScriptEncode =
60 {0xf414c262,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
61
62 #define DEFINE_EXPECT(func) \
63 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
64
65 #define SET_EXPECT(func) \
66 expect_ ## func = TRUE
67
68 #define CHECK_EXPECT2(func) \
69 do { \
70 ok(expect_ ##func, "unexpected call " #func "\n"); \
71 called_ ## func = TRUE; \
72 }while(0)
73
74 #define CHECK_EXPECT(func) \
75 do { \
76 CHECK_EXPECT2(func); \
77 expect_ ## func = FALSE; \
78 }while(0)
79
80 #define CHECK_CALLED(func) \
81 do { \
82 ok(called_ ## func, "expected " #func "\n"); \
83 expect_ ## func = called_ ## func = FALSE; \
84 }while(0)
85
86 DEFINE_EXPECT(GetLCID);
87 DEFINE_EXPECT(OnStateChange_UNINITIALIZED);
88 DEFINE_EXPECT(OnStateChange_STARTED);
89 DEFINE_EXPECT(OnStateChange_CONNECTED);
90 DEFINE_EXPECT(OnStateChange_DISCONNECTED);
91 DEFINE_EXPECT(OnStateChange_CLOSED);
92 DEFINE_EXPECT(OnStateChange_INITIALIZED);
93 DEFINE_EXPECT(OnEnterScript);
94 DEFINE_EXPECT(OnLeaveScript);
95
96 static const CLSID *engine_clsid = &CLSID_JScript;
97
98 static BSTR a2bstr(const char *str)
99 {
100 BSTR ret;
101 int len;
102
103 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
104 ret = SysAllocStringLen(NULL, len-1);
105 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
106
107 return ret;
108 }
109
110 #define test_state(s,ss) _test_state(__LINE__,s,ss)
111 static void _test_state(unsigned line, IActiveScript *script, SCRIPTSTATE exstate)
112 {
113 SCRIPTSTATE state = -1;
114 HRESULT hres;
115
116 hres = IActiveScript_GetScriptState(script, &state);
117 ok_(__FILE__,line) (hres == S_OK, "GetScriptState failed: %08x\n", hres);
118 ok_(__FILE__,line) (state == exstate, "state=%d, expected %d\n", state, exstate);
119 }
120
121 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
122 {
123 *ppv = NULL;
124
125 if(IsEqualGUID(&IID_IUnknown, riid))
126 *ppv = iface;
127 else if(IsEqualGUID(&IID_IActiveScriptSite, riid))
128 *ppv = iface;
129 else
130 return E_NOINTERFACE;
131
132 IUnknown_AddRef((IUnknown*)*ppv);
133 return S_OK;
134 }
135
136 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
137 {
138 return 2;
139 }
140
141 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
142 {
143 return 1;
144 }
145
146 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
147 {
148 CHECK_EXPECT(GetLCID);
149 return E_NOTIMPL;
150 }
151
152 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
153 DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
154 {
155 ok(0, "unexpected call\n");
156 return E_NOTIMPL;
157 }
158
159 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
160 {
161 ok(0, "unexpected call\n");
162 return E_NOTIMPL;
163 }
164
165 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
166 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
167 {
168 ok(0, "unexpected call\n");
169 return E_NOTIMPL;
170 }
171
172 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
173 {
174 switch(ssScriptState) {
175 case SCRIPTSTATE_UNINITIALIZED:
176 CHECK_EXPECT(OnStateChange_UNINITIALIZED);
177 return S_OK;
178 case SCRIPTSTATE_STARTED:
179 CHECK_EXPECT(OnStateChange_STARTED);
180 return S_OK;
181 case SCRIPTSTATE_CONNECTED:
182 CHECK_EXPECT(OnStateChange_CONNECTED);
183 return S_OK;
184 case SCRIPTSTATE_DISCONNECTED:
185 CHECK_EXPECT(OnStateChange_DISCONNECTED);
186 return S_OK;
187 case SCRIPTSTATE_CLOSED:
188 CHECK_EXPECT(OnStateChange_CLOSED);
189 return S_OK;
190 case SCRIPTSTATE_INITIALIZED:
191 CHECK_EXPECT(OnStateChange_INITIALIZED);
192 return S_OK;
193 default:
194 ok(0, "unexpected call %d\n", ssScriptState);
195 }
196
197 return E_NOTIMPL;
198 }
199
200 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
201 {
202 ok(0, "unexpected call\n");
203 return E_NOTIMPL;
204 }
205
206 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
207 {
208 CHECK_EXPECT(OnEnterScript);
209 return S_OK;
210 }
211
212 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
213 {
214 CHECK_EXPECT(OnLeaveScript);
215 return S_OK;
216 }
217
218 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
219 ActiveScriptSite_QueryInterface,
220 ActiveScriptSite_AddRef,
221 ActiveScriptSite_Release,
222 ActiveScriptSite_GetLCID,
223 ActiveScriptSite_GetItemInfo,
224 ActiveScriptSite_GetDocVersionString,
225 ActiveScriptSite_OnScriptTerminate,
226 ActiveScriptSite_OnStateChange,
227 ActiveScriptSite_OnScriptError,
228 ActiveScriptSite_OnEnterScript,
229 ActiveScriptSite_OnLeaveScript
230 };
231
232 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
233
234 static void test_script_dispatch(IDispatchEx *dispex)
235 {
236 DISPPARAMS dp = {NULL,NULL,0,0};
237 EXCEPINFO ei;
238 BSTR str;
239 DISPID id;
240 VARIANT v;
241 HRESULT hres;
242
243 str = a2bstr("ActiveXObject");
244 hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id);
245 SysFreeString(str);
246 ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
247
248 str = a2bstr("Math");
249 hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id);
250 SysFreeString(str);
251 ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
252
253 memset(&ei, 0, sizeof(ei));
254 hres = IDispatchEx_InvokeEx(dispex, id, 0, DISPATCH_PROPERTYGET, &dp, &v, &ei, NULL);
255 ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
256 ok(V_VT(&v) == VT_DISPATCH, "V_VT(v) = %d\n", V_VT(&v));
257 ok(V_DISPATCH(&v) != NULL, "V_DISPATCH(v) = NULL\n");
258 VariantClear(&v);
259
260 str = a2bstr("String");
261 hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id);
262 SysFreeString(str);
263 ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
264
265 memset(&ei, 0, sizeof(ei));
266 hres = IDispatchEx_InvokeEx(dispex, id, 0, DISPATCH_PROPERTYGET, &dp, &v, &ei, NULL);
267 ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
268 ok(V_VT(&v) == VT_DISPATCH, "V_VT(v) = %d\n", V_VT(&v));
269 ok(V_DISPATCH(&v) != NULL, "V_DISPATCH(v) = NULL\n");
270 VariantClear(&v);
271 }
272
273 static IDispatchEx *get_script_dispatch(IActiveScript *script)
274 {
275 IDispatchEx *dispex;
276 IDispatch *disp;
277 HRESULT hres;
278
279 disp = (void*)0xdeadbeef;
280 hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
281 ok(hres == S_OK, "GetScriptDispatch failed: %08x\n", hres);
282
283 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
284 IDispatch_Release(disp);
285 ok(hres == S_OK, "Could not get IDispatch iface: %08x\n", hres);
286 return dispex;
287 }
288
289 static void test_no_script_dispatch(IActiveScript *script)
290 {
291 IDispatch *disp;
292 HRESULT hres;
293
294 disp = (void*)0xdeadbeef;
295 hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
296 ok(hres == E_UNEXPECTED, "hres = %08x, expected E_UNEXPECTED\n", hres);
297 ok(!disp, "disp != NULL\n");
298 }
299
300 static void test_safety(IUnknown *unk)
301 {
302 IObjectSafety *safety;
303 DWORD supported, enabled;
304 HRESULT hres;
305
306 hres = IUnknown_QueryInterface(unk, &IID_IObjectSafety, (void**)&safety);
307 ok(hres == S_OK, "Could not get IObjectSafety: %08x\n", hres);
308 if(FAILED(hres))
309 return;
310
311 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, NULL);
312 ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
313 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, NULL, &enabled);
314 ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
315
316 supported = enabled = 0xdeadbeef;
317 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, &enabled);
318 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
319 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
320 "supported=%x\n", supported);
321 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
322
323 supported = enabled = 0xdeadbeef;
324 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScript, &supported, &enabled);
325 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
326 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
327 "supported=%x\n", supported);
328 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
329
330 supported = enabled = 0xdeadbeef;
331 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
332 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
333 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
334 "supported=%x\n", supported);
335 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
336
337 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
338 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER
339 |INTERFACESAFE_FOR_UNTRUSTED_CALLER,
340 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
341 ok(hres == E_FAIL, "SetInterfaceSafetyOptions failed: %08x, expected E_FAIL\n", hres);
342
343 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
344 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
345 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
346 ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
347
348 supported = enabled = 0xdeadbeef;
349 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
350 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
351 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
352 "supported=%x\n", supported);
353 ok(enabled == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
354 "enabled=%x\n", enabled);
355
356 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, INTERFACESAFE_FOR_UNTRUSTED_DATA, 0);
357 ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
358
359 supported = enabled = 0xdeadbeef;
360 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
361 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
362 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
363 "supported=%x\n", supported);
364 ok(enabled == (INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER), "enabled=%x\n", enabled);
365
366 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
367 INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER, 0);
368 ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
369
370 supported = enabled = 0xdeadbeef;
371 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
372 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
373 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
374 "supported=%x\n", supported);
375 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
376
377 hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
378 INTERFACE_USES_DISPEX, 0);
379 ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
380
381 supported = enabled = 0xdeadbeef;
382 hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
383 ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
384 ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
385 "supported=%x\n", supported);
386 ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
387
388 IObjectSafety_Release(safety);
389 }
390
391 static HRESULT set_script_prop(IActiveScript *engine, DWORD property, VARIANT *val)
392 {
393 IActiveScriptProperty *script_prop;
394 HRESULT hres;
395
396 hres = IActiveScript_QueryInterface(engine, &IID_IActiveScriptProperty,
397 (void**)&script_prop);
398 ok(hres == S_OK, "Could not get IActiveScriptProperty: %08x\n", hres);
399 if(FAILED(hres))
400 return hres;
401
402 hres = IActiveScriptProperty_SetProperty(script_prop, property, NULL, val);
403 IActiveScriptProperty_Release(script_prop);
404 return hres;
405 }
406
407 static void test_invoke_versioning(IActiveScript *script)
408 {
409 VARIANT v;
410 HRESULT hres;
411
412 V_VT(&v) = VT_NULL;
413 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
414 if(hres == E_NOTIMPL) {
415 win_skip("SCRIPTPROP_INVOKESTRING not supported\n");
416 return;
417 }
418 ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
419
420 V_VT(&v) = VT_I2;
421 V_I2(&v) = 0;
422 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
423 ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
424
425 V_VT(&v) = VT_I4;
426 V_I4(&v) = 16;
427 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
428 ok(hres == E_INVALIDARG, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
429
430 V_VT(&v) = VT_I4;
431 V_I4(&v) = 2;
432 hres = set_script_prop(script, SCRIPTPROP_INVOKEVERSIONING, &v);
433 ok(hres == S_OK, "SetProperty(SCRIPTPROP_INVOKEVERSIONING) failed: %08x\n", hres);
434 }
435
436 static IActiveScript *create_jscript(void)
437 {
438 IActiveScript *ret;
439 HRESULT hres;
440
441 hres = CoCreateInstance(engine_clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
442 &IID_IActiveScript, (void**)&ret);
443 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
444
445 return ret;
446 }
447
448 static void test_jscript(void)
449 {
450 IActiveScriptParse *parse;
451 IActiveScript *script;
452 IDispatchEx *dispex;
453 ULONG ref;
454 HRESULT hres;
455
456 script = create_jscript();
457
458 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
459 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
460
461 test_state(script, SCRIPTSTATE_UNINITIALIZED);
462 test_safety((IUnknown*)script);
463 test_invoke_versioning(script);
464
465 hres = IActiveScriptParse_InitNew(parse);
466 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
467
468 hres = IActiveScriptParse_InitNew(parse);
469 ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
470
471 hres = IActiveScript_SetScriptSite(script, NULL);
472 ok(hres == E_POINTER, "SetScriptSite failed: %08x, expected E_POINTER\n", hres);
473
474 test_state(script, SCRIPTSTATE_UNINITIALIZED);
475 test_no_script_dispatch(script);
476
477 SET_EXPECT(GetLCID);
478 SET_EXPECT(OnStateChange_INITIALIZED);
479 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
480 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
481 CHECK_CALLED(GetLCID);
482 CHECK_CALLED(OnStateChange_INITIALIZED);
483
484 test_state(script, SCRIPTSTATE_INITIALIZED);
485
486 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
487 ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
488
489 dispex = get_script_dispatch(script);
490 test_script_dispatch(dispex);
491
492 SET_EXPECT(OnStateChange_STARTED);
493 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
494 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
495 CHECK_CALLED(OnStateChange_STARTED);
496
497 test_state(script, SCRIPTSTATE_STARTED);
498
499 SET_EXPECT(OnStateChange_CLOSED);
500 hres = IActiveScript_Close(script);
501 ok(hres == S_OK, "Close failed: %08x\n", hres);
502 CHECK_CALLED(OnStateChange_CLOSED);
503
504 test_state(script, SCRIPTSTATE_CLOSED);
505 test_no_script_dispatch(script);
506 test_script_dispatch(dispex);
507 IDispatchEx_Release(dispex);
508
509 IActiveScriptParse_Release(parse);
510
511 ref = IActiveScript_Release(script);
512 ok(!ref, "ref = %d\n", ref);
513 }
514
515 static void test_jscript2(void)
516 {
517 IActiveScriptParse *parse;
518 IActiveScript *script;
519 ULONG ref;
520 HRESULT hres;
521
522 script = create_jscript();
523
524 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
525 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
526
527 test_state(script, SCRIPTSTATE_UNINITIALIZED);
528
529 SET_EXPECT(GetLCID);
530 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
531 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
532 CHECK_CALLED(GetLCID);
533
534 test_state(script, SCRIPTSTATE_UNINITIALIZED);
535
536 SET_EXPECT(OnStateChange_INITIALIZED);
537 hres = IActiveScriptParse_InitNew(parse);
538 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
539 CHECK_CALLED(OnStateChange_INITIALIZED);
540
541 hres = IActiveScriptParse_InitNew(parse);
542 ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
543
544 SET_EXPECT(OnStateChange_CONNECTED);
545 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_CONNECTED);
546 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
547 CHECK_CALLED(OnStateChange_CONNECTED);
548
549 test_state(script, SCRIPTSTATE_CONNECTED);
550
551 SET_EXPECT(OnStateChange_DISCONNECTED);
552 SET_EXPECT(OnStateChange_INITIALIZED);
553 SET_EXPECT(OnStateChange_CLOSED);
554 hres = IActiveScript_Close(script);
555 ok(hres == S_OK, "Close failed: %08x\n", hres);
556 CHECK_CALLED(OnStateChange_DISCONNECTED);
557 CHECK_CALLED(OnStateChange_INITIALIZED);
558 CHECK_CALLED(OnStateChange_CLOSED);
559
560 test_state(script, SCRIPTSTATE_CLOSED);
561 test_no_script_dispatch(script);
562
563 IActiveScriptParse_Release(parse);
564
565 ref = IActiveScript_Release(script);
566 ok(!ref, "ref = %d\n", ref);
567 }
568
569 static void test_jscript_uninitializing(void)
570 {
571 IActiveScriptParse *parse;
572 IActiveScript *script;
573 IDispatchEx *dispex;
574 ULONG ref;
575 HRESULT hres;
576
577 static const WCHAR script_textW[] =
578 {'f','u','n','c','t','i','o','n',' ','f','(',')',' ','{','}',0};
579
580 script = create_jscript();
581
582 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)&parse);
583 ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
584
585 test_state(script, SCRIPTSTATE_UNINITIALIZED);
586
587 hres = IActiveScriptParse_InitNew(parse);
588 ok(hres == S_OK, "InitNew failed: %08x\n", hres);
589
590 SET_EXPECT(GetLCID);
591 SET_EXPECT(OnStateChange_INITIALIZED);
592 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
593 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
594 CHECK_CALLED(GetLCID);
595 CHECK_CALLED(OnStateChange_INITIALIZED);
596
597 test_state(script, SCRIPTSTATE_INITIALIZED);
598
599 hres = IActiveScriptParse_ParseScriptText(parse, script_textW, NULL, NULL, NULL, 0, 1, 0x42, NULL, NULL);
600 ok(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
601
602 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
603 ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
604
605 SET_EXPECT(OnStateChange_UNINITIALIZED);
606 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
607 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
608 CHECK_CALLED(OnStateChange_UNINITIALIZED);
609
610 test_state(script, SCRIPTSTATE_UNINITIALIZED);
611
612 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
613 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
614
615 SET_EXPECT(GetLCID);
616 SET_EXPECT(OnStateChange_INITIALIZED);
617 hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
618 ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
619 CHECK_CALLED(GetLCID);
620 CHECK_CALLED(OnStateChange_INITIALIZED);
621
622 SET_EXPECT(OnStateChange_CONNECTED);
623 SET_EXPECT(OnEnterScript);
624 SET_EXPECT(OnLeaveScript);
625 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_CONNECTED);
626 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
627 CHECK_CALLED(OnStateChange_CONNECTED);
628 CHECK_CALLED(OnEnterScript);
629 CHECK_CALLED(OnLeaveScript);
630
631 test_state(script, SCRIPTSTATE_CONNECTED);
632
633 dispex = get_script_dispatch(script);
634 ok(dispex != NULL, "dispex == NULL\n");
635 IDispatchEx_Release(dispex);
636
637 SET_EXPECT(OnStateChange_DISCONNECTED);
638 SET_EXPECT(OnStateChange_INITIALIZED);
639 SET_EXPECT(OnStateChange_UNINITIALIZED);
640 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
641 ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x\n", hres);
642 CHECK_CALLED(OnStateChange_DISCONNECTED);
643 CHECK_CALLED(OnStateChange_INITIALIZED);
644 CHECK_CALLED(OnStateChange_UNINITIALIZED);
645
646 test_state(script, SCRIPTSTATE_UNINITIALIZED);
647
648 hres = IActiveScript_Close(script);
649 ok(hres == S_OK, "Close failed: %08x\n", hres);
650
651 test_state(script, SCRIPTSTATE_CLOSED);
652
653 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_UNINITIALIZED);
654 ok(hres == E_UNEXPECTED, "SetScriptState(SCRIPTSTATE_UNINITIALIZED) failed: %08x, expected E_UNEXPECTED\n", hres);
655
656 test_state(script, SCRIPTSTATE_CLOSED);
657
658 IActiveScriptParse_Release(parse);
659
660 ref = IActiveScript_Release(script);
661 ok(!ref, "ref = %d\n", ref);
662 }
663
664 static void test_aggregation(void)
665 {
666 IUnknown *unk = (IUnknown*)0xdeadbeef;
667 HRESULT hres;
668
669 hres = CoCreateInstance(&CLSID_JScript, (IUnknown*)0xdeadbeef, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
670 &IID_IUnknown, (void**)&unk);
671 ok(hres == CLASS_E_NOAGGREGATION,
672 "CoCreateInstance failed: %08x, expected CLASS_E_NOAGGREGATION\n", hres);
673 ok(!unk || broken(unk != NULL), "unk = %p\n", unk);
674 }
675
676 static BOOL check_jscript(void)
677 {
678 IActiveScriptProperty *script_prop;
679 HRESULT hres;
680
681 hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
682 &IID_IActiveScriptProperty, (void**)&script_prop);
683 if(SUCCEEDED(hres))
684 IActiveScriptProperty_Release(script_prop);
685
686 return hres == S_OK;
687 }
688
689 START_TEST(jscript)
690 {
691 CoInitialize(NULL);
692
693 if(check_jscript()) {
694 trace("Testing JScript object...\n");
695 test_jscript();
696 test_jscript2();
697 test_jscript_uninitializing();
698 test_aggregation();
699
700 trace("Testing JScriptEncode object...\n");
701 engine_clsid = &CLSID_JScriptEncode;
702 test_jscript();
703 }else {
704 win_skip("Broken engine, probably too old\n");
705 }
706
707 CoUninitialize();
708 }