msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / mshtml / tests / script.c
blob6178cdf3fc96b9eb355b0453156a676f8d661936
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 #define COBJMACROS
20 #define CONST_VTABLE
22 #include <wine/test.h>
23 #include <stdarg.h>
24 #include <stdio.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "ole2.h"
29 #include "dispex.h"
30 #include "mshtml.h"
31 #include "initguid.h"
32 #include "activscp.h"
33 #include "activdbg.h"
34 #include "objsafe.h"
35 #include "mshtmdid.h"
37 DEFINE_GUID(CLSID_IdentityUnmarshal,0x0000001b,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
39 #define DEFINE_EXPECT(func) \
40 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
42 #define SET_EXPECT(func) \
43 do { called_ ## func = FALSE; expect_ ## func = TRUE; } while(0)
45 #define CHECK_EXPECT2(func) \
46 do { \
47 ok(expect_ ##func, "unexpected call " #func "\n"); \
48 called_ ## func = TRUE; \
49 }while(0)
51 #define CHECK_EXPECT(func) \
52 do { \
53 CHECK_EXPECT2(func); \
54 expect_ ## func = FALSE; \
55 }while(0)
57 #define CHECK_CALLED(func) \
58 do { \
59 ok(called_ ## func, "expected " #func "\n"); \
60 expect_ ## func = called_ ## func = FALSE; \
61 }while(0)
63 #define CHECK_NOT_CALLED(func) \
64 do { \
65 ok(!called_ ## func, "unexpected " #func "\n"); \
66 expect_ ## func = called_ ## func = FALSE; \
67 }while(0)
69 #define CLEAR_CALLED(func) \
70 expect_ ## func = called_ ## func = FALSE
73 DEFINE_EXPECT(CreateInstance);
74 DEFINE_EXPECT(GetInterfaceSafetyOptions);
75 DEFINE_EXPECT(SetInterfaceSafetyOptions);
76 DEFINE_EXPECT(InitNew);
77 DEFINE_EXPECT(Close);
78 DEFINE_EXPECT(SetProperty);
79 DEFINE_EXPECT(SetScriptSite);
80 DEFINE_EXPECT(GetScriptState);
81 DEFINE_EXPECT(SetScriptState_STARTED);
82 DEFINE_EXPECT(SetScriptState_CONNECTED);
83 DEFINE_EXPECT(SetScriptState_DISCONNECTED);
84 DEFINE_EXPECT(AddNamedItem);
85 DEFINE_EXPECT(ParseScriptText);
86 DEFINE_EXPECT(GetScriptDispatch);
87 DEFINE_EXPECT(funcDisp);
89 #define TESTSCRIPT_CLSID "{178fc163-f585-4e24-9c13-4bb7faf80746}"
91 static const GUID CLSID_TestScript =
92 {0x178fc163,0xf585,0x4e24,{0x9c,0x13,0x4b,0xb7,0xfa,0xf8,0x07,0x46}};
94 static IHTMLDocument2 *notif_doc;
95 static IDispatchEx *window_dispex;
96 static BOOL doc_complete;
98 static const char *debugstr_w(LPCWSTR str)
100 static char buf[1024];
101 WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
102 return buf;
105 static const char *debugstr_guid(REFIID riid)
107 static char buf[50];
109 sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
110 riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
111 riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
112 riid->Data4[5], riid->Data4[6], riid->Data4[7]);
114 return buf;
117 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
118 REFIID riid, void**ppv)
120 if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
121 *ppv = iface;
122 return S_OK;
125 return E_NOINTERFACE;
128 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
130 return 2;
133 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
135 return 1;
138 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
140 if(dispID == DISPID_READYSTATE){
141 BSTR state;
142 HRESULT hres;
144 static const WCHAR completeW[] = {'c','o','m','p','l','e','t','e',0};
146 hres = IHTMLDocument2_get_readyState(notif_doc, &state);
147 ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
149 if(!lstrcmpW(state, completeW))
150 doc_complete = TRUE;
152 SysFreeString(state);
155 return S_OK;
158 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
160 ok(0, "unexpected call\n");
161 return E_NOTIMPL;
164 static IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
165 PropertyNotifySink_QueryInterface,
166 PropertyNotifySink_AddRef,
167 PropertyNotifySink_Release,
168 PropertyNotifySink_OnChanged,
169 PropertyNotifySink_OnRequestEdit
172 static IPropertyNotifySink PropertyNotifySink = { &PropertyNotifySinkVtbl };
174 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
176 *ppv = NULL;
178 if(IsEqualGUID(riid, &IID_IUnknown)
179 || IsEqualGUID(riid, &IID_IDispatch)
180 || IsEqualGUID(riid, &IID_IDispatchEx))
181 *ppv = iface;
182 else
183 return E_NOINTERFACE;
185 return S_OK;
188 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
190 return 2;
193 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
195 return 1;
198 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
200 ok(0, "unexpected call\n");
201 return E_NOTIMPL;
204 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
205 LCID lcid, ITypeInfo **ppTInfo)
207 ok(0, "unexpected call\n");
208 return E_NOTIMPL;
211 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
212 LPOLESTR *rgszNames, UINT cNames,
213 LCID lcid, DISPID *rgDispId)
215 ok(0, "unexpected call\n");
216 return E_NOTIMPL;
219 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
220 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
221 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
223 ok(0, "unexpected call\n");
224 return E_NOTIMPL;
227 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
229 ok(0, "unexpected call %s %x\n", debugstr_w(bstrName), grfdex);
230 return E_NOTIMPL;
233 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
235 ok(0, "unexpected call\n");
236 return E_NOTIMPL;
239 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
241 ok(0, "unexpected call\n");
242 return E_NOTIMPL;
245 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
247 ok(0, "unexpected call\n");
248 return E_NOTIMPL;
251 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
253 ok(0, "unexpected call\n");
254 return E_NOTIMPL;
257 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
259 ok(0, "unexpected call\n");
260 return E_NOTIMPL;
263 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
265 ok(0, "unexpected call\n");
266 return E_NOTIMPL;
269 static HRESULT WINAPI funcDisp_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
270 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
272 CHECK_EXPECT(funcDisp);
274 ok(id == DISPID_VALUE, "id = %d\n", id);
275 ok(lcid == 0, "lcid = %x\n", lcid);
276 ok(wFlags == DISPATCH_METHOD, "wFlags = %x\n", wFlags);
277 ok(pdp != NULL, "pdp == NULL\n");
278 ok(pdp->cArgs == 2, "pdp->cArgs = %d\n", pdp->cArgs);
279 ok(pdp->cNamedArgs == 1, "pdp->cNamedArgs = %d\n", pdp->cNamedArgs);
280 ok(pdp->rgdispidNamedArgs[0] == DISPID_THIS, "pdp->rgdispidNamedArgs[0] = %d\n", pdp->rgdispidNamedArgs[0]);
281 ok(V_VT(pdp->rgvarg) == VT_DISPATCH, "V_VT(rgvarg) = %d\n", V_VT(pdp->rgvarg));
282 ok(V_VT(pdp->rgvarg+1) == VT_BOOL, "V_VT(rgvarg[1]) = %d\n", V_VT(pdp->rgvarg));
283 ok(V_BOOL(pdp->rgvarg+1) == VARIANT_TRUE, "V_BOOL(rgvarg[1]) = %x\n", V_BOOL(pdp->rgvarg));
284 ok(pvarRes != NULL, "pvarRes == NULL\n");
285 ok(pei != NULL, "pei == NULL\n");
286 ok(!pspCaller, "pspCaller != NULL\n");
288 V_VT(pvarRes) = VT_I4;
289 V_I4(pvarRes) = 100;
290 return S_OK;
293 static IDispatchExVtbl testObjVtbl = {
294 DispatchEx_QueryInterface,
295 DispatchEx_AddRef,
296 DispatchEx_Release,
297 DispatchEx_GetTypeInfoCount,
298 DispatchEx_GetTypeInfo,
299 DispatchEx_GetIDsOfNames,
300 DispatchEx_Invoke,
301 DispatchEx_GetDispID,
302 funcDisp_InvokeEx,
303 DispatchEx_DeleteMemberByName,
304 DispatchEx_DeleteMemberByDispID,
305 DispatchEx_GetMemberProperties,
306 DispatchEx_GetMemberName,
307 DispatchEx_GetNextDispID,
308 DispatchEx_GetNameSpaceParent
311 static IDispatchEx funcDisp = { &testObjVtbl };
313 static IHTMLDocument2 *create_document(void)
315 IHTMLDocument2 *doc;
316 HRESULT hres;
318 hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
319 &IID_IHTMLDocument2, (void**)&doc);
320 ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
322 return doc;
325 static IHTMLDocument2 *create_doc_with_string(const char *str)
327 IPersistStreamInit *init;
328 IStream *stream;
329 IHTMLDocument2 *doc;
330 HGLOBAL mem;
331 SIZE_T len;
333 notif_doc = doc = create_document();
334 if(!doc)
335 return NULL;
337 doc_complete = FALSE;
338 len = strlen(str);
339 mem = GlobalAlloc(0, len);
340 memcpy(mem, str, len);
341 CreateStreamOnHGlobal(mem, TRUE, &stream);
343 IHTMLDocument2_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&init);
345 IPersistStreamInit_Load(init, stream);
346 IPersistStreamInit_Release(init);
347 IStream_Release(stream);
349 return doc;
352 static void do_advise(IUnknown *unk, REFIID riid, IUnknown *unk_advise)
354 IConnectionPointContainer *container;
355 IConnectionPoint *cp;
356 DWORD cookie;
357 HRESULT hres;
359 hres = IUnknown_QueryInterface(unk, &IID_IConnectionPointContainer, (void**)&container);
360 ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
362 hres = IConnectionPointContainer_FindConnectionPoint(container, riid, &cp);
363 IConnectionPointContainer_Release(container);
364 ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
366 hres = IConnectionPoint_Advise(cp, unk_advise, &cookie);
367 IConnectionPoint_Release(cp);
368 ok(hres == S_OK, "Advise failed: %08x\n", hres);
371 typedef void (*domtest_t)(IHTMLDocument2*);
373 static IHTMLDocument2 *create_and_load_doc(const char *str)
375 IHTMLDocument2 *doc;
376 IHTMLElement *body = NULL;
377 ULONG ref;
378 MSG msg;
379 HRESULT hres;
380 static const WCHAR ucPtr[] = {'b','a','c','k','g','r','o','u','n','d',0};
381 DISPID dispID = -1;
382 OLECHAR *name;
385 doc = create_doc_with_string(str);
386 do_advise((IUnknown*)doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
388 while(!doc_complete && GetMessage(&msg, NULL, 0, 0)) {
389 TranslateMessage(&msg);
390 DispatchMessage(&msg);
393 hres = IHTMLDocument2_get_body(doc, &body);
394 ok(hres == S_OK, "get_body failed: %08x\n", hres);
396 if(!body) {
397 skip("Could not get document body. Assuming no Gecko installed.\n");
398 ref = IHTMLDocument2_Release(doc);
399 ok(!ref, "ref = %d\n", ref);
400 return NULL;
403 /* Check we can query for function on the IHTMLElementBody interface */
404 name = (WCHAR*)ucPtr;
405 hres = IHTMLElement_GetIDsOfNames(body, &IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &dispID);
406 ok(hres == S_OK, "GetIDsOfNames(background) failed %08x\n", hres);
407 ok(dispID == DISPID_IHTMLBODYELEMENT_BACKGROUND, "Incorrect dispID got (%d)\n", dispID);
409 IHTMLElement_Release(body);
410 return doc;
413 static IActiveScriptSite *site;
414 static SCRIPTSTATE state;
416 static HRESULT WINAPI ObjectSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
418 *ppv = NULL;
419 ok(0, "unexpected call\n");
420 return E_NOINTERFACE;
423 static ULONG WINAPI ObjectSafety_AddRef(IObjectSafety *iface)
425 return 2;
428 static ULONG WINAPI ObjectSafety_Release(IObjectSafety *iface)
430 return 1;
433 static HRESULT WINAPI ObjectSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
434 DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
436 CHECK_EXPECT(GetInterfaceSafetyOptions);
438 ok(IsEqualGUID(&IID_IActiveScriptParse, riid), "unexpected riid %s\n", debugstr_guid(riid));
439 ok(pdwSupportedOptions != NULL, "pdwSupportedOptions == NULL\n");
440 ok(pdwEnabledOptions != NULL, "pdwEnabledOptions == NULL\n");
442 *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER;
443 *pdwEnabledOptions = INTERFACE_USES_DISPEX;
445 return S_OK;
448 static HRESULT WINAPI ObjectSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
449 DWORD dwOptionSetMask, DWORD dwEnabledOptions)
451 CHECK_EXPECT(SetInterfaceSafetyOptions);
453 ok(IsEqualGUID(&IID_IActiveScriptParse, riid), "unexpected riid %s\n", debugstr_guid(riid));
455 ok(dwOptionSetMask == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
456 "dwOptionSetMask=%x\n", dwOptionSetMask);
457 ok(dwEnabledOptions == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
458 "dwEnabledOptions=%x\n", dwOptionSetMask);
460 return S_OK;
463 static const IObjectSafetyVtbl ObjectSafetyVtbl = {
464 ObjectSafety_QueryInterface,
465 ObjectSafety_AddRef,
466 ObjectSafety_Release,
467 ObjectSafety_GetInterfaceSafetyOptions,
468 ObjectSafety_SetInterfaceSafetyOptions
471 static IObjectSafety ObjectSafety = { &ObjectSafetyVtbl };
473 static HRESULT WINAPI ActiveScriptProperty_QueryInterface(IActiveScriptProperty *iface, REFIID riid, void **ppv)
475 *ppv = NULL;
476 ok(0, "unexpected call\n");
477 return E_NOINTERFACE;
480 static ULONG WINAPI ActiveScriptProperty_AddRef(IActiveScriptProperty *iface)
482 return 2;
485 static ULONG WINAPI ActiveScriptProperty_Release(IActiveScriptProperty *iface)
487 return 1;
490 static HRESULT WINAPI ActiveScriptProperty_GetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
491 VARIANT *pvarIndex, VARIANT *pvarValue)
493 ok(0, "unexpected call\n");
494 return E_NOTIMPL;
497 static HRESULT WINAPI ActiveScriptProperty_SetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
498 VARIANT *pvarIndex, VARIANT *pvarValue)
500 CHECK_EXPECT(SetProperty);
502 ok(dwProperty == SCRIPTPROP_HACK_TRIDENTEVENTSINK, "unexpected property %d\n", dwProperty);
503 ok(!pvarIndex, "pvarIndex != NULL\n");
504 ok(pvarValue != NULL, "pvarValue == NULL\n");
505 ok(V_VT(pvarValue) == VT_BOOL, "V_VT(pvarValue)=%d\n", V_VT(pvarValue));
506 ok(V_BOOL(pvarValue) == VARIANT_TRUE, "V_BOOL(pvarValue)=%x\n", V_BOOL(pvarValue));
508 return E_NOTIMPL;
511 static const IActiveScriptPropertyVtbl ActiveScriptPropertyVtbl = {
512 ActiveScriptProperty_QueryInterface,
513 ActiveScriptProperty_AddRef,
514 ActiveScriptProperty_Release,
515 ActiveScriptProperty_GetProperty,
516 ActiveScriptProperty_SetProperty
519 static IActiveScriptProperty ActiveScriptProperty = { &ActiveScriptPropertyVtbl };
521 static HRESULT WINAPI ActiveScriptParseProcedure_QueryInterface(IActiveScriptParseProcedure2 *iface, REFIID riid, void **ppv)
523 *ppv = NULL;
524 ok(0, "unexpected call\n");
525 return E_NOINTERFACE;
528 static ULONG WINAPI ActiveScriptParseProcedure_AddRef(IActiveScriptParseProcedure2 *iface)
530 return 2;
533 static ULONG WINAPI ActiveScriptParseProcedure_Release(IActiveScriptParseProcedure2 *iface)
535 return 1;
538 static HRESULT WINAPI ActiveScriptParseProcedure_ParseProcedureText(IActiveScriptParseProcedure2 *iface,
539 LPCOLESTR pstrCode, LPCOLESTR pstrFormalParams, LPCOLESTR pstrProcedureName,
540 LPCOLESTR pstrItemName, IUnknown *punkContext, LPCOLESTR pstrDelimiter,
541 DWORD dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags, IDispatch **ppdisp)
543 ok(0, "unexpected call\n");
544 return E_NOTIMPL;
547 static const IActiveScriptParseProcedure2Vtbl ActiveScriptParseProcedureVtbl = {
548 ActiveScriptParseProcedure_QueryInterface,
549 ActiveScriptParseProcedure_AddRef,
550 ActiveScriptParseProcedure_Release,
551 ActiveScriptParseProcedure_ParseProcedureText
554 static IActiveScriptParseProcedure2 ActiveScriptParseProcedure = { &ActiveScriptParseProcedureVtbl };
556 static HRESULT WINAPI ActiveScriptParse_QueryInterface(IActiveScriptParse *iface, REFIID riid, void **ppv)
558 *ppv = NULL;
559 ok(0, "unexpected call\n");
560 return E_NOINTERFACE;
563 static ULONG WINAPI ActiveScriptParse_AddRef(IActiveScriptParse *iface)
565 return 2;
568 static ULONG WINAPI ActiveScriptParse_Release(IActiveScriptParse *iface)
570 return 1;
573 static HRESULT WINAPI ActiveScriptParse_InitNew(IActiveScriptParse *iface)
575 CHECK_EXPECT(InitNew);
576 return S_OK;
579 static HRESULT WINAPI ActiveScriptParse_AddScriptlet(IActiveScriptParse *iface,
580 LPCOLESTR pstrDefaultName, LPCOLESTR pstrCode, LPCOLESTR pstrItemName,
581 LPCOLESTR pstrSubItemName, LPCOLESTR pstrEventName, LPCOLESTR pstrDelimiter,
582 DWORD dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags,
583 BSTR *pbstrName, EXCEPINFO *pexcepinfo)
585 ok(0, "unexpected call\n");
586 return E_NOTIMPL;
589 static HRESULT WINAPI ActiveScriptParse_ParseScriptText(IActiveScriptParse *iface,
590 LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext,
591 LPCOLESTR pstrDelimiter, DWORD dwSourceContextCookie, ULONG ulStartingLine,
592 DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo)
594 IDispatchEx *document;
595 IUnknown *unk;
596 VARIANT var, arg;
597 DISPPARAMS dp;
598 EXCEPINFO ei;
599 DISPID id, named_arg = DISPID_PROPERTYPUT;
600 BSTR tmp;
601 HRESULT hres;
603 static const WCHAR documentW[] = {'d','o','c','u','m','e','n','t',0};
604 static const WCHAR testW[] = {'t','e','s','t',0};
605 static const WCHAR funcW[] = {'f','u','n','c',0};
607 CHECK_EXPECT(ParseScriptText);
609 SET_EXPECT(GetScriptDispatch);
611 tmp = SysAllocString(documentW);
612 hres = IDispatchEx_GetDispID(window_dispex, tmp, fdexNameCaseSensitive, &id);
613 SysFreeString(tmp);
614 ok(hres == S_OK, "GetDispID(document) failed: %08x\n", hres);
615 ok(id == DISPID_IHTMLWINDOW2_DOCUMENT, "id=%x\n", id);
617 todo_wine CHECK_CALLED(GetScriptDispatch);
619 VariantInit(&var);
620 memset(&dp, 0, sizeof(dp));
621 memset(&ei, 0, sizeof(ei));
623 hres = IDispatchEx_InvokeEx(window_dispex, id, LOCALE_NEUTRAL, INVOKE_PROPERTYGET, &dp, &var, &ei, NULL);
624 ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
625 ok(V_VT(&var) == VT_DISPATCH, "V_VT(var)=%d\n", V_VT(&var));
626 ok(V_DISPATCH(&var) != NULL, "V_DISPATCH(&var) == NULL\n");
628 hres = IDispatch_QueryInterface(V_DISPATCH(&var), &IID_IDispatchEx, (void**)&document);
629 VariantClear(&var);
630 ok(hres == S_OK, "Could not get DispatchEx: %08x\n", hres);
632 tmp = SysAllocString(testW);
633 hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive, &id);
634 ok(hres == DISP_E_UNKNOWNNAME, "GetDispID(document) failed: %08x, expected DISP_E_UNKNOWNNAME\n", hres);
635 hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive | fdexNameImplicit, &id);
636 ok(hres == DISP_E_UNKNOWNNAME, "GetDispID(document) failed: %08x, expected DISP_E_UNKNOWNNAME\n", hres);
637 SysFreeString(tmp);
639 id = 0;
640 tmp = SysAllocString(testW);
641 hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive|fdexNameEnsure, &id);
642 SysFreeString(tmp);
643 ok(hres == S_OK, "GetDispID(document) failed: %08x\n", hres);
644 ok(id, "id == 0\n");
646 dp.cArgs = 1;
647 dp.rgvarg = &var;
648 dp.cNamedArgs = 1;
649 dp.rgdispidNamedArgs = &named_arg;
650 V_VT(&var) = VT_I4;
651 V_I4(&var) = 100;
653 hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_PROPERTYPUT, &dp, NULL, &ei, NULL);
654 ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
656 tmp = SysAllocString(testW);
657 hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive, &id);
658 SysFreeString(tmp);
659 ok(hres == S_OK, "GetDispID(document) failed: %08x\n", hres);
661 VariantInit(&var);
662 memset(&dp, 0, sizeof(dp));
663 memset(&ei, 0, sizeof(ei));
664 hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_PROPERTYGET, &dp, &var, &ei, NULL);
665 ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
666 ok(V_VT(&var) == VT_I4, "V_VT(var)=%d\n", V_VT(&var));
667 ok(V_I4(&var) == 100, "V_I4(&var) == NULL\n");
669 unk = (void*)0xdeadbeef;
670 hres = IDispatchEx_GetNameSpaceParent(window_dispex, &unk);
671 ok(hres == S_OK, "GetNameSpaceParent failed: %08x\n", hres);
672 ok(!unk, "unk=%p, expected NULL\n", unk);
674 id = 0;
675 tmp = SysAllocString(funcW);
676 hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive|fdexNameEnsure, &id);
677 SysFreeString(tmp);
678 ok(hres == S_OK, "GetDispID(func) failed: %08x\n", hres);
679 ok(id, "id == 0\n");
681 dp.cArgs = 1;
682 dp.rgvarg = &var;
683 dp.cNamedArgs = 0;
684 dp.rgdispidNamedArgs = NULL;
685 V_VT(&var) = VT_DISPATCH;
686 V_DISPATCH(&var) = (IDispatch*)&funcDisp;
688 hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_PROPERTYPUT, &dp, NULL, &ei, NULL);
689 ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
691 VariantInit(&var);
692 memset(&dp, 0, sizeof(dp));
693 memset(&ei, 0, sizeof(ei));
694 V_VT(&arg) = VT_BOOL;
695 V_BOOL(&arg) = VARIANT_TRUE;
696 dp.cArgs = 1;
697 dp.rgvarg = &arg;
699 SET_EXPECT(funcDisp);
700 hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_FUNC, &dp, &var, &ei, NULL);
701 CHECK_CALLED(funcDisp);
703 ok(hres == S_OK, "InvokeEx(INVOKE_FUNC) failed: %08x\n", hres);
704 ok(V_VT(&var) == VT_I4, "V_VT(var)=%d\n", V_VT(&var));
705 ok(V_I4(&var) == 100, "V_I4(&var) == NULL\n");
707 IDispatchEx_Release(document);
709 return S_OK;
712 static const IActiveScriptParseVtbl ActiveScriptParseVtbl = {
713 ActiveScriptParse_QueryInterface,
714 ActiveScriptParse_AddRef,
715 ActiveScriptParse_Release,
716 ActiveScriptParse_InitNew,
717 ActiveScriptParse_AddScriptlet,
718 ActiveScriptParse_ParseScriptText
721 static IActiveScriptParse ActiveScriptParse = { &ActiveScriptParseVtbl };
723 static HRESULT WINAPI ActiveScript_QueryInterface(IActiveScript *iface, REFIID riid, void **ppv)
725 *ppv = NULL;
727 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IActiveScript, riid)) {
728 *ppv = iface;
729 return S_OK;
732 if(IsEqualGUID(&IID_IActiveScriptParse, riid)) {
733 *ppv = &ActiveScriptParse;
734 return S_OK;
737 if(IsEqualGUID(&IID_IActiveScriptParseProcedure2, riid)) {
738 *ppv = &ActiveScriptParseProcedure;
739 return S_OK;
742 if(IsEqualGUID(&IID_IActiveScriptProperty, riid)) {
743 *ppv = &ActiveScriptProperty;
744 return S_OK;
747 if(IsEqualGUID(&IID_IObjectSafety, riid)) {
748 *ppv = &ObjectSafety;
749 return S_OK;
752 ok(0, "unexpected riid %s\n", debugstr_guid(riid));
753 return E_NOINTERFACE;
756 static ULONG WINAPI ActiveScript_AddRef(IActiveScript *iface)
758 return 2;
761 static ULONG WINAPI ActiveScript_Release(IActiveScript *iface)
763 return 1;
766 static HRESULT WINAPI ActiveScript_SetScriptSite(IActiveScript *iface, IActiveScriptSite *pass)
768 IActiveScriptSiteInterruptPoll *poll;
769 IActiveScriptSiteDebug *debug;
770 IServiceProvider *service;
771 ICanHandleException *canexpection;
772 LCID lcid;
773 HRESULT hres;
775 CHECK_EXPECT(SetScriptSite);
777 ok(pass != NULL, "pass == NULL\n");
779 hres = IActiveScriptSite_QueryInterface(pass, &IID_IActiveScriptSiteInterruptPoll, (void**)&poll);
780 ok(hres == S_OK, "Could not get IActiveScriptSiteInterruptPoll interface: %08x\n", hres);
781 if(FAILED(hres))
782 IActiveScriptSiteInterruptPoll_Release(poll);
784 hres = IActiveScriptSite_GetLCID(pass, &lcid);
785 ok(hres == S_OK, "GetLCID failed: %08x\n", hres);
787 hres = IActiveScriptSite_OnStateChange(pass, (state = SCRIPTSTATE_INITIALIZED));
788 ok(hres == S_OK, "OnStateChange failed: %08x\n", hres);
790 hres = IActiveScriptSite_QueryInterface(pass, &IID_IActiveScriptSiteDebug, (void**)&debug);
791 ok(hres == S_OK, "Could not get IActiveScriptSiteDebug interface: %08x\n", hres);
792 if(SUCCEEDED(hres))
793 IActiveScriptSiteDebug32_Release(debug);
795 hres = IActiveScriptSite_QueryInterface(pass, &IID_ICanHandleException, (void**)&canexpection);
796 ok(hres == E_NOINTERFACE, "Could not get IID_ICanHandleException interface: %08x\n", hres);
798 hres = IActiveScriptSite_QueryInterface(pass, &IID_IServiceProvider, (void**)&service);
799 todo_wine ok(hres == S_OK, "Could not get IServiceProvider interface: %08x\n", hres);
800 if(SUCCEEDED(hres))
801 IServiceProvider_Release(service);
803 site = pass;
804 IActiveScriptSite_AddRef(site);
805 return S_OK;
808 static HRESULT WINAPI ActiveScript_GetScriptSite(IActiveScript *iface, REFIID riid,
809 void **ppvObject)
811 ok(0, "unexpected call\n");
812 return E_NOTIMPL;
815 static HRESULT WINAPI ActiveScript_SetScriptState(IActiveScript *iface, SCRIPTSTATE ss)
817 HRESULT hres;
819 switch(ss) {
820 case SCRIPTSTATE_STARTED:
821 CHECK_EXPECT(SetScriptState_STARTED);
822 break;
823 case SCRIPTSTATE_CONNECTED:
824 CHECK_EXPECT(SetScriptState_CONNECTED);
825 break;
826 case SCRIPTSTATE_DISCONNECTED:
827 CHECK_EXPECT(SetScriptState_DISCONNECTED);
828 break;
829 default:
830 ok(0, "unexpected state %d\n", ss);
831 return E_NOTIMPL;
834 hres = IActiveScriptSite_OnStateChange(site, (state = ss));
835 return S_OK;
838 static HRESULT WINAPI ActiveScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE *pssState)
840 CHECK_EXPECT(GetScriptState);
842 *pssState = state;
843 return S_OK;
846 static HRESULT WINAPI ActiveScript_Close(IActiveScript *iface)
848 CHECK_EXPECT(Close);
849 return E_NOTIMPL;
852 static HRESULT WINAPI ActiveScript_AddNamedItem(IActiveScript *iface,
853 LPCOLESTR pstrName, DWORD dwFlags)
855 IDispatch *disp;
856 IUnknown *unk = NULL, *unk2;
857 HRESULT hres;
859 static const WCHAR windowW[] = {'w','i','n','d','o','w',0};
861 static const IID unknown_iid = {0x719C3050,0xF9D3,0x11CF,{0xA4,0x93,0x00,0x40,0x05,0x23,0xA8,0xA0}};
863 CHECK_EXPECT(AddNamedItem);
865 ok(!lstrcmpW(pstrName, windowW), "pstrName=%s\n", debugstr_w(pstrName));
866 ok(dwFlags == (SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS), "dwFlags=%x\n", dwFlags);
868 hres = IActiveScriptSite_GetItemInfo(site, windowW, SCRIPTINFO_IUNKNOWN, &unk, NULL);
869 ok(hres == S_OK, "GetItemInfo failed: %08x\n", hres);
870 ok(unk != NULL, "unk == NULL\n");
872 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
873 ok(hres == S_OK, "Could not get IDispatch interface: %08x\n", hres);
874 if(SUCCEEDED(hres))
875 IDispatch_Release(disp);
877 hres = IUnknown_QueryInterface(unk, &unknown_iid, (void**)&unk2);
878 ok(hres == E_NOINTERFACE, "Got ?? interface: %p\n", unk2);
879 if(SUCCEEDED(hres))
880 IUnknown_Release(unk2);
882 hres = IUnknown_QueryInterface(unk, &IID_IDispatchEx, (void**)&window_dispex);
883 ok(hres == S_OK, "Could not get IDispatchEx interface: %08x\n", hres);
885 IUnknown_Release(unk);
886 return S_OK;
889 static HRESULT WINAPI ActiveScript_AddTypeLib(IActiveScript *iface, REFGUID rguidTypeLib,
890 DWORD dwMajor, DWORD dwMinor, DWORD dwFlags)
892 ok(0, "unexpected call\n");
893 return E_NOTIMPL;
896 static HRESULT WINAPI ActiveScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR pstrItemName,
897 IDispatch **ppdisp)
899 CHECK_EXPECT(GetScriptDispatch);
900 return E_NOTIMPL;
903 static HRESULT WINAPI ActiveScript_GetCurrentScriptThreadID(IActiveScript *iface,
904 SCRIPTTHREADID *pstridThread)
906 ok(0, "unexpected call\n");
907 return E_NOTIMPL;
910 static HRESULT WINAPI ActiveScript_GetScriptThreadID(IActiveScript *iface,
911 DWORD dwWin32ThreadId, SCRIPTTHREADID *pstidThread)
913 ok(0, "unexpected call\n");
914 return E_NOTIMPL;
917 static HRESULT WINAPI ActiveScript_GetScriptThreadState(IActiveScript *iface,
918 SCRIPTTHREADID stidThread, SCRIPTTHREADSTATE *pstsState)
920 ok(0, "unexpected call\n");
921 return E_NOTIMPL;
924 static HRESULT WINAPI ActiveScript_InterruptScriptThread(IActiveScript *iface,
925 SCRIPTTHREADID stidThread, const EXCEPINFO *pexcepinfo, DWORD dwFlags)
927 ok(0, "unexpected call\n");
928 return E_NOTIMPL;
931 static HRESULT WINAPI ActiveScript_Clone(IActiveScript *iface, IActiveScript **ppscript)
933 ok(0, "unexpected call\n");
934 return E_NOTIMPL;
937 static const IActiveScriptVtbl ActiveScriptVtbl = {
938 ActiveScript_QueryInterface,
939 ActiveScript_AddRef,
940 ActiveScript_Release,
941 ActiveScript_SetScriptSite,
942 ActiveScript_GetScriptSite,
943 ActiveScript_SetScriptState,
944 ActiveScript_GetScriptState,
945 ActiveScript_Close,
946 ActiveScript_AddNamedItem,
947 ActiveScript_AddTypeLib,
948 ActiveScript_GetScriptDispatch,
949 ActiveScript_GetCurrentScriptThreadID,
950 ActiveScript_GetScriptThreadID,
951 ActiveScript_GetScriptThreadState,
952 ActiveScript_InterruptScriptThread,
953 ActiveScript_Clone
956 static IActiveScript ActiveScript = { &ActiveScriptVtbl };
958 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
960 *ppv = NULL;
962 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
963 *ppv = iface;
964 return S_OK;
967 if(IsEqualGUID(&IID_IMarshal, riid))
968 return E_NOINTERFACE;
969 if(IsEqualGUID(&CLSID_IdentityUnmarshal, riid))
970 return E_NOINTERFACE;
972 ok(0, "unexpected riid %s\n", debugstr_guid(riid));
973 return E_NOTIMPL;
976 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
978 return 2;
981 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
983 return 1;
986 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
988 CHECK_EXPECT(CreateInstance);
990 ok(!outer, "outer = %p\n", outer);
991 ok(IsEqualGUID(&IID_IActiveScript, riid), "unexpected riid %s\n", debugstr_guid(riid));
992 *ppv = &ActiveScript;
993 return S_OK;
996 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
998 ok(0, "unexpected call\n");
999 return S_OK;
1002 static const IClassFactoryVtbl ClassFactoryVtbl = {
1003 ClassFactory_QueryInterface,
1004 ClassFactory_AddRef,
1005 ClassFactory_Release,
1006 ClassFactory_CreateInstance,
1007 ClassFactory_LockServer
1010 static IClassFactory script_cf = { &ClassFactoryVtbl };
1012 static const char simple_script_str[] =
1013 "<html><head></head><body>"
1014 "<script language=\"TestScript\">simple script</script>"
1015 "</body></html>";
1017 static void test_simple_script(void)
1019 IHTMLDocument2 *doc;
1021 SET_EXPECT(CreateInstance);
1022 SET_EXPECT(GetInterfaceSafetyOptions);
1023 SET_EXPECT(SetInterfaceSafetyOptions);
1024 SET_EXPECT(SetProperty);
1025 SET_EXPECT(InitNew);
1026 SET_EXPECT(SetScriptSite);
1027 SET_EXPECT(GetScriptState);
1028 SET_EXPECT(SetScriptState_STARTED);
1029 SET_EXPECT(AddNamedItem);
1030 SET_EXPECT(ParseScriptText);
1031 SET_EXPECT(SetScriptState_CONNECTED);
1033 doc = create_and_load_doc(simple_script_str);
1034 if(!doc) return;
1036 CHECK_CALLED(CreateInstance);
1037 CHECK_CALLED(GetInterfaceSafetyOptions);
1038 CHECK_CALLED(SetInterfaceSafetyOptions);
1039 CHECK_CALLED(SetProperty);
1040 CHECK_CALLED(InitNew);
1041 CHECK_CALLED(SetScriptSite);
1042 CHECK_CALLED(GetScriptState);
1043 CHECK_CALLED(SetScriptState_STARTED);
1044 CHECK_CALLED(AddNamedItem);
1045 CHECK_CALLED(ParseScriptText);
1046 CHECK_CALLED(SetScriptState_CONNECTED);
1048 if(site)
1049 IActiveScriptSite_Release(site);
1050 if(window_dispex)
1051 IDispatchEx_Release(window_dispex);
1053 SET_EXPECT(SetScriptState_DISCONNECTED);
1054 SET_EXPECT(Close);
1056 IHTMLDocument2_Release(doc);
1058 CHECK_CALLED(SetScriptState_DISCONNECTED);
1059 CHECK_CALLED(Close);
1062 static BOOL init_key(const char *key_name, const char *def_value, BOOL init)
1064 HKEY hkey;
1065 DWORD res;
1067 if(!init) {
1068 RegDeleteKey(HKEY_CLASSES_ROOT, key_name);
1069 return TRUE;
1072 res = RegCreateKeyA(HKEY_CLASSES_ROOT, key_name, &hkey);
1073 if(res != ERROR_SUCCESS)
1074 return FALSE;
1076 if(def_value)
1077 res = RegSetValueA(hkey, NULL, REG_SZ, def_value, strlen(def_value));
1079 RegCloseKey(hkey);
1081 return res == ERROR_SUCCESS;
1084 static BOOL init_registry(BOOL init)
1086 return init_key("TestScript\\CLSID", TESTSCRIPT_CLSID, init)
1087 && init_key("CLSID\\"TESTSCRIPT_CLSID"\\Implemented Categories\\{F0B7A1A1-9847-11CF-8F20-00805F2CD064}",
1088 NULL, init)
1089 && init_key("CLSID\\"TESTSCRIPT_CLSID"\\Implemented Categories\\{F0B7A1A2-9847-11CF-8F20-00805F2CD064}",
1090 NULL, init);
1093 static BOOL register_script_engine(void)
1095 DWORD regid;
1096 HRESULT hres;
1098 if(!init_registry(TRUE)) {
1099 init_registry(FALSE);
1100 return FALSE;
1103 hres = CoRegisterClassObject(&CLSID_TestScript, (IUnknown *)&script_cf,
1104 CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
1105 ok(hres == S_OK, "Could not register screipt engine: %08x\n", hres);
1107 return TRUE;
1110 static void gecko_installer_workaround(BOOL disable)
1112 HKEY hkey;
1113 DWORD res;
1115 static BOOL has_url = FALSE;
1116 static char url[2048];
1118 if(!disable && !has_url)
1119 return;
1121 res = RegOpenKey(HKEY_CURRENT_USER, "Software\\Wine\\MSHTML", &hkey);
1122 if(res != ERROR_SUCCESS)
1123 return;
1125 if(disable) {
1126 DWORD type, size = sizeof(url);
1128 res = RegQueryValueEx(hkey, "GeckoUrl", NULL, &type, (PVOID)url, &size);
1129 if(res == ERROR_SUCCESS && type == REG_SZ)
1130 has_url = TRUE;
1132 RegDeleteValue(hkey, "GeckoUrl");
1133 }else {
1134 RegSetValueEx(hkey, "GeckoUrl", 0, REG_SZ, (PVOID)url, lstrlenA(url)+1);
1137 RegCloseKey(hkey);
1140 /* Check if Internet Explorer is configured to run in "Enhanced Security Configuration" (aka hardened mode) */
1141 /* Note: this code is duplicated in dlls/mshtml/tests/dom.c, dlls/mshtml/tests/script.c and dlls/urlmon/tests/misc.c */
1142 static BOOL is_ie_hardened(void)
1144 HKEY zone_map;
1145 DWORD ie_harden, type, size;
1147 ie_harden = 0;
1148 if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap",
1149 0, KEY_QUERY_VALUE, &zone_map) == ERROR_SUCCESS) {
1150 size = sizeof(DWORD);
1151 if (RegQueryValueEx(zone_map, "IEHarden", NULL, &type, (LPBYTE) &ie_harden, &size) != ERROR_SUCCESS ||
1152 type != REG_DWORD) {
1153 ie_harden = 0;
1155 RegCloseKey(zone_map);
1158 return ie_harden != 0;
1161 START_TEST(script)
1163 gecko_installer_workaround(TRUE);
1164 CoInitialize(NULL);
1166 if(winetest_interactive || ! is_ie_hardened()) {
1167 if(register_script_engine()) {
1168 test_simple_script();
1169 init_registry(FALSE);
1170 }else {
1171 skip("Could not register TestScript engine\n");
1173 }else {
1174 skip("IE running in Enhanced Security Configuration\n");
1177 CoUninitialize();
1178 gecko_installer_workaround(FALSE);