mshtml: Added function object tests.
[wine] / dlls / mshtml / tests / script.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 COBJMACROS
20 #define CONST_VTABLE
21
22 #include <wine/test.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25
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"
36
37 DEFINE_GUID(CLSID_IdentityUnmarshal,0x0000001b,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
38
39 #ifdef _WIN64
40
41 #define CTXARG_T DWORDLONG
42 #define IActiveScriptParseVtbl IActiveScriptParse64Vtbl
43 #define IActiveScriptParseProcedure2Vtbl IActiveScriptParseProcedure2_64Vtbl
44
45 #else
46
47 #define CTXARG_T DWORD
48 #define IActiveScriptParseVtbl IActiveScriptParse32Vtbl
49 #define IActiveScriptParseProcedure2Vtbl IActiveScriptParseProcedure2_32Vtbl
50
51 #endif
52
53 #define DEFINE_EXPECT(func) \
54     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
55
56 #define SET_EXPECT(func) \
57     do { called_ ## func = FALSE; expect_ ## func = TRUE; } while(0)
58
59 #define CHECK_EXPECT2(func) \
60     do { \
61         ok(expect_ ##func, "unexpected call " #func "\n"); \
62         called_ ## func = TRUE; \
63     }while(0)
64
65 #define CHECK_EXPECT(func) \
66     do { \
67         CHECK_EXPECT2(func); \
68         expect_ ## func = FALSE; \
69     }while(0)
70
71 #define CHECK_CALLED(func) \
72     do { \
73         ok(called_ ## func, "expected " #func "\n"); \
74         expect_ ## func = called_ ## func = FALSE; \
75     }while(0)
76
77 #define CHECK_CALLED_BROKEN(func) \
78     do { \
79         ok(called_ ## func || broken(!called_ ## func), "expected " #func "\n"); \
80         expect_ ## func = called_ ## func = FALSE; \
81     }while(0)
82
83 #define CHECK_NOT_CALLED(func) \
84     do { \
85         ok(!called_ ## func, "unexpected " #func "\n"); \
86         expect_ ## func = called_ ## func = FALSE; \
87     }while(0)
88
89 #define CLEAR_CALLED(func) \
90     expect_ ## func = called_ ## func = FALSE
91
92
93 DEFINE_EXPECT(CreateInstance);
94 DEFINE_EXPECT(GetInterfaceSafetyOptions);
95 DEFINE_EXPECT(SetInterfaceSafetyOptions);
96 DEFINE_EXPECT(InitNew);
97 DEFINE_EXPECT(Close);
98 DEFINE_EXPECT(SetProperty_HACK_TRIDENTEVENTSINK);
99 DEFINE_EXPECT(SetProperty_INVOKEVERSIONING);
100 DEFINE_EXPECT(SetProperty_ABBREVIATE_GLOBALNAME_RESOLUTION);
101 DEFINE_EXPECT(SetScriptSite);
102 DEFINE_EXPECT(GetScriptState);
103 DEFINE_EXPECT(SetScriptState_STARTED);
104 DEFINE_EXPECT(SetScriptState_CONNECTED);
105 DEFINE_EXPECT(SetScriptState_DISCONNECTED);
106 DEFINE_EXPECT(AddNamedItem);
107 DEFINE_EXPECT(ParseScriptText);
108 DEFINE_EXPECT(GetScriptDispatch);
109 DEFINE_EXPECT(funcDisp);
110
111 #define TESTSCRIPT_CLSID "{178fc163-f585-4e24-9c13-4bb7faf80746}"
112
113 static const GUID CLSID_TestScript =
114     {0x178fc163,0xf585,0x4e24,{0x9c,0x13,0x4b,0xb7,0xfa,0xf8,0x07,0x46}};
115
116 static IHTMLDocument2 *notif_doc;
117 static IDispatchEx *window_dispex;
118 static BOOL doc_complete;
119
120 static const char *debugstr_guid(REFIID riid)
121 {
122     static char buf[50];
123
124     sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
125             riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
126             riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
127             riid->Data4[5], riid->Data4[6], riid->Data4[7]);
128
129     return buf;
130 }
131
132 static int strcmp_wa(LPCWSTR strw, const char *stra)
133 {
134     CHAR buf[512];
135     WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), NULL, NULL);
136     return lstrcmpA(stra, buf);
137 }
138
139 static BSTR a2bstr(const char *str)
140 {
141     BSTR ret;
142     int len;
143
144     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
145     ret = SysAllocStringLen(NULL, len);
146     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
147
148     return ret;
149 }
150
151 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
152         REFIID riid, void**ppv)
153 {
154     if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
155         *ppv = iface;
156         return S_OK;
157     }
158
159     return E_NOINTERFACE;
160 }
161
162 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
163 {
164     return 2;
165 }
166
167 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
168 {
169     return 1;
170 }
171
172 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
173 {
174     if(dispID == DISPID_READYSTATE){
175         BSTR state;
176         HRESULT hres;
177
178         static const WCHAR completeW[] = {'c','o','m','p','l','e','t','e',0};
179
180         hres = IHTMLDocument2_get_readyState(notif_doc, &state);
181         ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
182
183         if(!lstrcmpW(state, completeW))
184             doc_complete = TRUE;
185
186         SysFreeString(state);
187     }
188
189     return S_OK;
190 }
191
192 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
193 {
194     ok(0, "unexpected call\n");
195     return E_NOTIMPL;
196 }
197
198 static IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
199     PropertyNotifySink_QueryInterface,
200     PropertyNotifySink_AddRef,
201     PropertyNotifySink_Release,
202     PropertyNotifySink_OnChanged,
203     PropertyNotifySink_OnRequestEdit
204 };
205
206 static IPropertyNotifySink PropertyNotifySink = { &PropertyNotifySinkVtbl };
207
208 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
209 {
210     *ppv = NULL;
211
212     if(IsEqualGUID(riid, &IID_IUnknown)
213        || IsEqualGUID(riid, &IID_IDispatch)
214        || IsEqualGUID(riid, &IID_IDispatchEx))
215         *ppv = iface;
216     else
217         return E_NOINTERFACE;
218
219     return S_OK;
220 }
221
222 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
223 {
224     return 2;
225 }
226
227 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
228 {
229     return 1;
230 }
231
232 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
233 {
234     ok(0, "unexpected call\n");
235     return E_NOTIMPL;
236 }
237
238 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
239                                               LCID lcid, ITypeInfo **ppTInfo)
240 {
241     ok(0, "unexpected call\n");
242     return E_NOTIMPL;
243 }
244
245 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
246                                                 LPOLESTR *rgszNames, UINT cNames,
247                                                 LCID lcid, DISPID *rgDispId)
248 {
249     ok(0, "unexpected call\n");
250     return E_NOTIMPL;
251 }
252
253 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
254                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
255                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
256 {
257     ok(0, "unexpected call\n");
258     return E_NOTIMPL;
259 }
260
261 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
262 {
263     ok(0, "unexpected call %s %x\n", wine_dbgstr_w(bstrName), grfdex);
264     return E_NOTIMPL;
265 }
266
267 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
268 {
269     ok(0, "unexpected call\n");
270     return E_NOTIMPL;
271 }
272
273 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
274 {
275     ok(0, "unexpected call\n");
276     return E_NOTIMPL;
277 }
278
279 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
280 {
281     ok(0, "unexpected call\n");
282     return E_NOTIMPL;
283 }
284
285 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
286 {
287     ok(0, "unexpected call\n");
288     return E_NOTIMPL;
289 }
290
291 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
292 {
293     ok(0, "unexpected call\n");
294     return E_NOTIMPL;
295 }
296
297 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
298 {
299     ok(0, "unexpected call\n");
300     return E_NOTIMPL;
301 }
302
303 static HRESULT WINAPI funcDisp_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
304         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
305 {
306     CHECK_EXPECT(funcDisp);
307
308     ok(id == DISPID_VALUE, "id = %d\n", id);
309     ok(lcid == 0, "lcid = %x\n", lcid);
310     ok(wFlags == DISPATCH_METHOD, "wFlags = %x\n", wFlags);
311     ok(pdp != NULL, "pdp == NULL\n");
312     ok(pdp->cArgs == 2, "pdp->cArgs = %d\n", pdp->cArgs);
313     ok(pdp->cNamedArgs == 1, "pdp->cNamedArgs = %d\n", pdp->cNamedArgs);
314     ok(pdp->rgdispidNamedArgs[0] == DISPID_THIS, "pdp->rgdispidNamedArgs[0] = %d\n", pdp->rgdispidNamedArgs[0]);
315     ok(V_VT(pdp->rgvarg) == VT_DISPATCH, "V_VT(rgvarg) = %d\n", V_VT(pdp->rgvarg));
316     ok(V_VT(pdp->rgvarg+1) == VT_BOOL, "V_VT(rgvarg[1]) = %d\n", V_VT(pdp->rgvarg));
317     ok(V_BOOL(pdp->rgvarg+1) == VARIANT_TRUE, "V_BOOL(rgvarg[1]) = %x\n", V_BOOL(pdp->rgvarg));
318     ok(pvarRes != NULL, "pvarRes == NULL\n");
319     ok(pei != NULL, "pei == NULL\n");
320     ok(!pspCaller, "pspCaller != NULL\n");
321
322     V_VT(pvarRes) = VT_I4;
323     V_I4(pvarRes) = 100;
324     return S_OK;
325 }
326
327 static IDispatchExVtbl testObjVtbl = {
328     DispatchEx_QueryInterface,
329     DispatchEx_AddRef,
330     DispatchEx_Release,
331     DispatchEx_GetTypeInfoCount,
332     DispatchEx_GetTypeInfo,
333     DispatchEx_GetIDsOfNames,
334     DispatchEx_Invoke,
335     DispatchEx_GetDispID,
336     funcDisp_InvokeEx,
337     DispatchEx_DeleteMemberByName,
338     DispatchEx_DeleteMemberByDispID,
339     DispatchEx_GetMemberProperties,
340     DispatchEx_GetMemberName,
341     DispatchEx_GetNextDispID,
342     DispatchEx_GetNameSpaceParent
343 };
344
345 static IDispatchEx funcDisp = { &testObjVtbl };
346
347 static IHTMLDocument2 *create_document(void)
348 {
349     IHTMLDocument2 *doc;
350     HRESULT hres;
351
352     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
353             &IID_IHTMLDocument2, (void**)&doc);
354     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
355
356     return doc;
357 }
358
359 static IHTMLDocument2 *create_doc_with_string(const char *str)
360 {
361     IPersistStreamInit *init;
362     IStream *stream;
363     IHTMLDocument2 *doc;
364     HGLOBAL mem;
365     SIZE_T len;
366
367     notif_doc = doc = create_document();
368     if(!doc)
369         return NULL;
370
371     doc_complete = FALSE;
372     len = strlen(str);
373     mem = GlobalAlloc(0, len);
374     memcpy(mem, str, len);
375     CreateStreamOnHGlobal(mem, TRUE, &stream);
376
377     IHTMLDocument2_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&init);
378
379     IPersistStreamInit_Load(init, stream);
380     IPersistStreamInit_Release(init);
381     IStream_Release(stream);
382
383     return doc;
384 }
385
386 static void do_advise(IUnknown *unk, REFIID riid, IUnknown *unk_advise)
387 {
388     IConnectionPointContainer *container;
389     IConnectionPoint *cp;
390     DWORD cookie;
391     HRESULT hres;
392
393     hres = IUnknown_QueryInterface(unk, &IID_IConnectionPointContainer, (void**)&container);
394     ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
395
396     hres = IConnectionPointContainer_FindConnectionPoint(container, riid, &cp);
397     IConnectionPointContainer_Release(container);
398     ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
399
400     hres = IConnectionPoint_Advise(cp, unk_advise, &cookie);
401     IConnectionPoint_Release(cp);
402     ok(hres == S_OK, "Advise failed: %08x\n", hres);
403 }
404
405 typedef void (*domtest_t)(IHTMLDocument2*);
406
407 static IHTMLDocument2 *create_and_load_doc(const char *str)
408 {
409     IHTMLDocument2 *doc;
410     IHTMLElement *body = NULL;
411     ULONG ref;
412     MSG msg;
413     HRESULT hres;
414     static const WCHAR ucPtr[] = {'b','a','c','k','g','r','o','u','n','d',0};
415     DISPID dispID = -1;
416     OLECHAR *name;
417
418
419     doc = create_doc_with_string(str);
420     do_advise((IUnknown*)doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
421
422     while(!doc_complete && GetMessage(&msg, NULL, 0, 0)) {
423         TranslateMessage(&msg);
424         DispatchMessage(&msg);
425     }
426
427     hres = IHTMLDocument2_get_body(doc, &body);
428     ok(hres == S_OK, "get_body failed: %08x\n", hres);
429
430     if(!body) {
431         skip("Could not get document body. Assuming no Gecko installed.\n");
432         ref = IHTMLDocument2_Release(doc);
433         ok(!ref, "ref = %d\n", ref);
434         return NULL;
435     }
436
437     /* Check we can query for function on the IHTMLElementBody interface */
438     name = (WCHAR*)ucPtr;
439     hres = IHTMLElement_GetIDsOfNames(body, &IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &dispID);
440     ok(hres == S_OK, "GetIDsOfNames(background) failed %08x\n", hres);
441     ok(dispID == DISPID_IHTMLBODYELEMENT_BACKGROUND, "Incorrect dispID got (%d)\n", dispID);
442
443     IHTMLElement_Release(body);
444     return doc;
445 }
446
447 static IActiveScriptSite *site;
448 static SCRIPTSTATE state;
449
450 static HRESULT WINAPI ObjectSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
451 {
452     *ppv = NULL;
453     ok(0, "unexpected call\n");
454     return E_NOINTERFACE;
455 }
456
457 static ULONG WINAPI ObjectSafety_AddRef(IObjectSafety *iface)
458 {
459     return 2;
460 }
461
462 static ULONG WINAPI ObjectSafety_Release(IObjectSafety *iface)
463 {
464     return 1;
465 }
466
467 static HRESULT WINAPI ObjectSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
468         DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
469 {
470     CHECK_EXPECT(GetInterfaceSafetyOptions);
471
472     ok(IsEqualGUID(&IID_IActiveScriptParse, riid), "unexpected riid %s\n", debugstr_guid(riid));
473     ok(pdwSupportedOptions != NULL, "pdwSupportedOptions == NULL\n");
474     ok(pdwEnabledOptions != NULL, "pdwEnabledOptions == NULL\n");
475
476     *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER;
477     *pdwEnabledOptions = INTERFACE_USES_DISPEX;
478
479     return S_OK;
480 }
481
482 static HRESULT WINAPI ObjectSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
483         DWORD dwOptionSetMask, DWORD dwEnabledOptions)
484 {
485     CHECK_EXPECT(SetInterfaceSafetyOptions);
486
487     ok(IsEqualGUID(&IID_IActiveScriptParse, riid), "unexpected riid %s\n", debugstr_guid(riid));
488
489     ok(dwOptionSetMask == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
490        "dwOptionSetMask=%x\n", dwOptionSetMask);
491     ok(dwEnabledOptions == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
492        "dwEnabledOptions=%x\n", dwOptionSetMask);
493
494     return S_OK;
495 }
496
497 static const IObjectSafetyVtbl ObjectSafetyVtbl = {
498     ObjectSafety_QueryInterface,
499     ObjectSafety_AddRef,
500     ObjectSafety_Release,
501     ObjectSafety_GetInterfaceSafetyOptions,
502     ObjectSafety_SetInterfaceSafetyOptions
503 };
504
505 static IObjectSafety ObjectSafety = { &ObjectSafetyVtbl };
506
507 static HRESULT WINAPI ActiveScriptProperty_QueryInterface(IActiveScriptProperty *iface, REFIID riid, void **ppv)
508 {
509     *ppv = NULL;
510     ok(0, "unexpected call\n");
511     return E_NOINTERFACE;
512 }
513
514 static ULONG WINAPI ActiveScriptProperty_AddRef(IActiveScriptProperty *iface)
515 {
516     return 2;
517 }
518
519 static ULONG WINAPI ActiveScriptProperty_Release(IActiveScriptProperty *iface)
520 {
521     return 1;
522 }
523
524 static HRESULT WINAPI ActiveScriptProperty_GetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
525         VARIANT *pvarIndex, VARIANT *pvarValue)
526 {
527     ok(0, "unexpected call\n");
528     return E_NOTIMPL;
529 }
530
531 static HRESULT WINAPI ActiveScriptProperty_SetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
532         VARIANT *pvarIndex, VARIANT *pvarValue)
533 {
534     switch(dwProperty) {
535     case SCRIPTPROP_HACK_TRIDENTEVENTSINK:
536         CHECK_EXPECT(SetProperty_HACK_TRIDENTEVENTSINK);
537         ok(V_VT(pvarValue) == VT_BOOL, "V_VT(pvarValue)=%d\n", V_VT(pvarValue));
538         ok(V_BOOL(pvarValue) == VARIANT_TRUE, "V_BOOL(pvarValue)=%x\n", V_BOOL(pvarValue));
539         break;
540     case SCRIPTPROP_INVOKEVERSIONING:
541         CHECK_EXPECT(SetProperty_INVOKEVERSIONING);
542         ok(V_VT(pvarValue) == VT_I4, "V_VT(pvarValue)=%d\n", V_VT(pvarValue));
543         ok(V_I4(pvarValue) == 1, "V_I4(pvarValue)=%d\n", V_I4(pvarValue));
544         break;
545     case SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION:
546         CHECK_EXPECT(SetProperty_ABBREVIATE_GLOBALNAME_RESOLUTION);
547         ok(V_VT(pvarValue) == VT_BOOL, "V_VT(pvarValue)=%d\n", V_VT(pvarValue));
548         ok(V_BOOL(pvarValue) == VARIANT_TRUE, "V_BOOL(pvarValue)=%x\n", V_BOOL(pvarValue));
549         break;
550     default:
551         ok(0, "unexpected property %x\n", dwProperty);
552         return E_NOTIMPL;
553     }
554
555     ok(!pvarIndex, "pvarIndex != NULL\n");
556     ok(pvarValue != NULL, "pvarValue == NULL\n");
557
558     return S_OK;
559 }
560
561 static const IActiveScriptPropertyVtbl ActiveScriptPropertyVtbl = {
562     ActiveScriptProperty_QueryInterface,
563     ActiveScriptProperty_AddRef,
564     ActiveScriptProperty_Release,
565     ActiveScriptProperty_GetProperty,
566     ActiveScriptProperty_SetProperty
567 };
568
569 static IActiveScriptProperty ActiveScriptProperty = { &ActiveScriptPropertyVtbl };
570
571 static HRESULT WINAPI ActiveScriptParseProcedure_QueryInterface(IActiveScriptParseProcedure2 *iface, REFIID riid, void **ppv)
572 {
573     *ppv = NULL;
574     ok(0, "unexpected call\n");
575     return E_NOINTERFACE;
576 }
577
578 static ULONG WINAPI ActiveScriptParseProcedure_AddRef(IActiveScriptParseProcedure2 *iface)
579 {
580     return 2;
581 }
582
583 static ULONG WINAPI ActiveScriptParseProcedure_Release(IActiveScriptParseProcedure2 *iface)
584 {
585     return 1;
586 }
587
588 static HRESULT WINAPI ActiveScriptParseProcedure_ParseProcedureText(IActiveScriptParseProcedure2 *iface,
589         LPCOLESTR pstrCode, LPCOLESTR pstrFormalParams, LPCOLESTR pstrProcedureName,
590         LPCOLESTR pstrItemName, IUnknown *punkContext, LPCOLESTR pstrDelimiter,
591         CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags, IDispatch **ppdisp)
592 {
593     ok(0, "unexpected call\n");
594     return E_NOTIMPL;
595 }
596
597 static const IActiveScriptParseProcedure2Vtbl ActiveScriptParseProcedureVtbl = {
598     ActiveScriptParseProcedure_QueryInterface,
599     ActiveScriptParseProcedure_AddRef,
600     ActiveScriptParseProcedure_Release,
601     ActiveScriptParseProcedure_ParseProcedureText
602 };
603
604 static IActiveScriptParseProcedure2 ActiveScriptParseProcedure = { &ActiveScriptParseProcedureVtbl };
605
606 static HRESULT WINAPI ActiveScriptParse_QueryInterface(IActiveScriptParse *iface, REFIID riid, void **ppv)
607 {
608     *ppv = NULL;
609     ok(0, "unexpected call\n");
610     return E_NOINTERFACE;
611 }
612
613 static ULONG WINAPI ActiveScriptParse_AddRef(IActiveScriptParse *iface)
614 {
615     return 2;
616 }
617
618 static ULONG WINAPI ActiveScriptParse_Release(IActiveScriptParse *iface)
619 {
620     return 1;
621 }
622
623 static HRESULT WINAPI ActiveScriptParse_InitNew(IActiveScriptParse *iface)
624 {
625     CHECK_EXPECT(InitNew);
626     return S_OK;
627 }
628
629 static HRESULT WINAPI ActiveScriptParse_AddScriptlet(IActiveScriptParse *iface,
630         LPCOLESTR pstrDefaultName, LPCOLESTR pstrCode, LPCOLESTR pstrItemName,
631         LPCOLESTR pstrSubItemName, LPCOLESTR pstrEventName, LPCOLESTR pstrDelimiter,
632         CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags,
633         BSTR *pbstrName, EXCEPINFO *pexcepinfo)
634 {
635     ok(0, "unexpected call\n");
636     return E_NOTIMPL;
637 }
638
639 static void test_func(IDispatchEx *obj)
640 {
641     DISPID id, propput_arg = DISPID_PROPERTYPUT;
642     IDispatchEx *dispex;
643     IDispatch *disp;
644     EXCEPINFO ei;
645     DISPPARAMS dp;
646     BSTR str;
647     VARIANT var;
648     HRESULT hres;
649
650     str = a2bstr("toString");
651     hres = IDispatchEx_GetDispID(obj, str, fdexNameCaseSensitive, &id);
652     SysFreeString(str);
653     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
654     ok(id == DISPID_IOMNAVIGATOR_TOSTRING, "id = %x\n", id);
655
656     memset(&dp, 0, sizeof(dp));
657     memset(&ei, 0, sizeof(ei));
658     VariantInit(&var);
659     hres = IDispatchEx_InvokeEx(obj, id, LOCALE_NEUTRAL, DISPATCH_PROPERTYGET, &dp, &var, &ei, NULL);
660     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
661     ok(V_VT(&var) == VT_DISPATCH, "V_VT(var)=%d\n", V_VT(&var));
662     ok(V_DISPATCH(&var) != NULL, "V_DISPATCH(var) == NULL\n");
663     disp = V_DISPATCH(&var);
664
665     memset(&dp, 0, sizeof(dp));
666     memset(&ei, 0, sizeof(ei));
667     VariantInit(&var);
668     hres = IDispatchEx_InvokeEx(obj, id, LOCALE_NEUTRAL, DISPATCH_PROPERTYGET, &dp, &var, &ei, NULL);
669     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
670     ok(V_VT(&var) == VT_DISPATCH, "V_VT(var)=%d\n", V_VT(&var));
671     ok(V_DISPATCH(&var) == disp, "V_DISPATCH(var) != disp\n");
672     VariantClear(&var);
673
674     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
675     IDispatch_Release(disp);
676     ok(hres == S_OK, "Could not get IDispatchEx iface: %08x\n", hres);
677
678     /* FIXME: Test InvokeEx(DISPATCH_METHOD) */
679
680     memset(&dp, 0, sizeof(dp));
681     memset(&ei, 0, sizeof(ei));
682     VariantInit(&var);
683     hres = IDispatchEx_Invoke(dispex, DISPID_VALUE, &IID_NULL, LOCALE_NEUTRAL, DISPATCH_METHOD, &dp, &var, &ei, NULL);
684     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
685     ok(V_VT(&var) == VT_BSTR, "V_VT(var)=%d\n", V_VT(&var));
686     ok(!strcmp_wa(V_BSTR(&var), "[object]"), "V_BSTR(var) = %s\n", wine_dbgstr_w(V_BSTR(&var)));
687     VariantClear(&var);
688
689     dp.cArgs = 1;
690     dp.rgvarg = &var;
691     dp.cNamedArgs = 1;
692     dp.rgdispidNamedArgs = &propput_arg;
693     V_VT(&var) = VT_I4;
694     V_I4(&var) = 100;
695     hres = IDispatchEx_InvokeEx(obj, id, LOCALE_NEUTRAL, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
696     ok(hres == E_NOTIMPL, "InvokeEx failed: %08x\n", hres);
697
698     IDispatchEx_Release(dispex);
699 }
700
701 static HRESULT WINAPI ActiveScriptParse_ParseScriptText(IActiveScriptParse *iface,
702         LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext,
703         LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLine,
704         DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo)
705 {
706     IDispatchEx *document, *dispex;
707     IHTMLWindow2 *window;
708     IOmNavigator *navigator;
709     IUnknown *unk;
710     VARIANT var, arg;
711     DISPPARAMS dp;
712     EXCEPINFO ei;
713     DISPID id, named_arg = DISPID_PROPERTYPUT;
714     BSTR tmp;
715     HRESULT hres;
716
717     static const WCHAR documentW[] = {'d','o','c','u','m','e','n','t',0};
718     static const WCHAR testW[] = {'t','e','s','t',0};
719     static const WCHAR funcW[] = {'f','u','n','c',0};
720
721     CHECK_EXPECT(ParseScriptText);
722
723     SET_EXPECT(GetScriptDispatch);
724
725     tmp = SysAllocString(documentW);
726     hres = IDispatchEx_GetDispID(window_dispex, tmp, fdexNameCaseSensitive, &id);
727     SysFreeString(tmp);
728     ok(hres == S_OK, "GetDispID(document) failed: %08x\n", hres);
729     ok(id == DISPID_IHTMLWINDOW2_DOCUMENT, "id=%x\n", id);
730
731     todo_wine CHECK_CALLED(GetScriptDispatch);
732
733     VariantInit(&var);
734     memset(&dp, 0, sizeof(dp));
735     memset(&ei, 0, sizeof(ei));
736
737     hres = IDispatchEx_InvokeEx(window_dispex, id, LOCALE_NEUTRAL, INVOKE_PROPERTYGET, &dp, &var, &ei, NULL);
738     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
739     ok(V_VT(&var) == VT_DISPATCH, "V_VT(var)=%d\n", V_VT(&var));
740     ok(V_DISPATCH(&var) != NULL, "V_DISPATCH(&var) == NULL\n");
741
742     hres = IDispatch_QueryInterface(V_DISPATCH(&var), &IID_IDispatchEx, (void**)&document);
743     VariantClear(&var);
744     ok(hres == S_OK, "Could not get DispatchEx: %08x\n", hres);
745
746     tmp = SysAllocString(testW);
747     hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive, &id);
748     ok(hres == DISP_E_UNKNOWNNAME, "GetDispID(document) failed: %08x, expected DISP_E_UNKNOWNNAME\n", hres);
749     hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive | fdexNameImplicit, &id);
750     ok(hres == DISP_E_UNKNOWNNAME, "GetDispID(document) failed: %08x, expected DISP_E_UNKNOWNNAME\n", hres);
751     SysFreeString(tmp);
752
753     id = 0;
754     tmp = SysAllocString(testW);
755     hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive|fdexNameEnsure, &id);
756     SysFreeString(tmp);
757     ok(hres == S_OK, "GetDispID(document) failed: %08x\n", hres);
758     ok(id, "id == 0\n");
759
760     dp.cArgs = 1;
761     dp.rgvarg = &var;
762     dp.cNamedArgs = 1;
763     dp.rgdispidNamedArgs = &named_arg;
764     V_VT(&var) = VT_I4;
765     V_I4(&var) = 100;
766
767     hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_PROPERTYPUT, &dp, NULL, &ei, NULL);
768     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
769
770     tmp = SysAllocString(testW);
771     hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive, &id);
772     SysFreeString(tmp);
773     ok(hres == S_OK, "GetDispID(document) failed: %08x\n", hres);
774
775     VariantInit(&var);
776     memset(&dp, 0, sizeof(dp));
777     memset(&ei, 0, sizeof(ei));
778     hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_PROPERTYGET, &dp, &var, &ei, NULL);
779     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
780     ok(V_VT(&var) == VT_I4, "V_VT(var)=%d\n", V_VT(&var));
781     ok(V_I4(&var) == 100, "V_I4(&var) == NULL\n");
782
783     unk = (void*)0xdeadbeef;
784     hres = IDispatchEx_GetNameSpaceParent(window_dispex, &unk);
785     ok(hres == S_OK, "GetNameSpaceParent failed: %08x\n", hres);
786     ok(!unk, "unk=%p, expected NULL\n", unk);
787
788     id = 0;
789     tmp = SysAllocString(funcW);
790     hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive|fdexNameEnsure, &id);
791     SysFreeString(tmp);
792     ok(hres == S_OK, "GetDispID(func) failed: %08x\n", hres);
793     ok(id, "id == 0\n");
794
795     dp.cArgs = 1;
796     dp.rgvarg = &var;
797     dp.cNamedArgs = 0;
798     dp.rgdispidNamedArgs = NULL;
799     V_VT(&var) = VT_DISPATCH;
800     V_DISPATCH(&var) = (IDispatch*)&funcDisp;
801
802     hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_PROPERTYPUT, &dp, NULL, &ei, NULL);
803     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
804
805     VariantInit(&var);
806     memset(&dp, 0, sizeof(dp));
807     memset(&ei, 0, sizeof(ei));
808     V_VT(&arg) = VT_BOOL;
809     V_BOOL(&arg) = VARIANT_TRUE;
810     dp.cArgs = 1;
811     dp.rgvarg = &arg;
812
813     SET_EXPECT(funcDisp);
814     hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_FUNC, &dp, &var, &ei, NULL);
815     CHECK_CALLED(funcDisp);
816
817     ok(hres == S_OK, "InvokeEx(INVOKE_FUNC) failed: %08x\n", hres);
818     ok(V_VT(&var) == VT_I4, "V_VT(var)=%d\n", V_VT(&var));
819     ok(V_I4(&var) == 100, "V_I4(&var) == NULL\n");
820
821     IDispatchEx_Release(document);
822
823     hres = IDispatchEx_QueryInterface(window_dispex, &IID_IHTMLWindow2, (void**)&window);
824     ok(hres == S_OK, "Could not get IHTMLWindow2 iface: %08x\n", hres);
825
826     hres = IHTMLWindow2_get_navigator(window, &navigator);
827     IHTMLWindow2_Release(window);
828     ok(hres == S_OK, "get_navigator failed: %08x\n", hres);
829
830     hres = IOmNavigator_QueryInterface(navigator, &IID_IDispatchEx, (void**)&dispex);
831     IOmNavigator_Release(navigator);
832     ok(hres == S_OK, "Could not get IDispatchEx iface: %08x\n", hres);
833
834     test_func(dispex);
835     IDispatchEx_Release(dispex);
836
837     return S_OK;
838 }
839
840 static const IActiveScriptParseVtbl ActiveScriptParseVtbl = {
841     ActiveScriptParse_QueryInterface,
842     ActiveScriptParse_AddRef,
843     ActiveScriptParse_Release,
844     ActiveScriptParse_InitNew,
845     ActiveScriptParse_AddScriptlet,
846     ActiveScriptParse_ParseScriptText
847 };
848
849 static IActiveScriptParse ActiveScriptParse = { &ActiveScriptParseVtbl };
850
851 static HRESULT WINAPI ActiveScript_QueryInterface(IActiveScript *iface, REFIID riid, void **ppv)
852 {
853     *ppv = NULL;
854
855     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IActiveScript, riid)) {
856         *ppv = iface;
857         return S_OK;
858     }
859
860     if(IsEqualGUID(&IID_IActiveScriptParse, riid)) {
861         *ppv = &ActiveScriptParse;
862         return S_OK;
863     }
864
865     if(IsEqualGUID(&IID_IActiveScriptParseProcedure2, riid)) {
866         *ppv = &ActiveScriptParseProcedure;
867         return S_OK;
868     }
869
870     if(IsEqualGUID(&IID_IActiveScriptProperty, riid)) {
871         *ppv = &ActiveScriptProperty;
872         return S_OK;
873     }
874
875     if(IsEqualGUID(&IID_IObjectSafety, riid)) {
876         *ppv = &ObjectSafety;
877         return S_OK;
878     }
879
880     ok(0, "unexpected riid %s\n", debugstr_guid(riid));
881     return E_NOINTERFACE;
882 }
883
884 static ULONG WINAPI ActiveScript_AddRef(IActiveScript *iface)
885 {
886     return 2;
887 }
888
889 static ULONG WINAPI ActiveScript_Release(IActiveScript *iface)
890 {
891     return 1;
892 }
893
894 static HRESULT WINAPI ActiveScript_SetScriptSite(IActiveScript *iface, IActiveScriptSite *pass)
895 {
896     IActiveScriptSiteInterruptPoll *poll;
897     IActiveScriptSiteDebug *debug;
898     IServiceProvider *service;
899     ICanHandleException *canexpection;
900     LCID lcid;
901     HRESULT hres;
902
903     CHECK_EXPECT(SetScriptSite);
904
905     ok(pass != NULL, "pass == NULL\n");
906
907     hres = IActiveScriptSite_QueryInterface(pass, &IID_IActiveScriptSiteInterruptPoll, (void**)&poll);
908     ok(hres == S_OK, "Could not get IActiveScriptSiteInterruptPoll interface: %08x\n", hres);
909     if(FAILED(hres))
910         IActiveScriptSiteInterruptPoll_Release(poll);
911
912     hres = IActiveScriptSite_GetLCID(pass, &lcid);
913     ok(hres == S_OK, "GetLCID failed: %08x\n", hres);
914
915     hres = IActiveScriptSite_OnStateChange(pass, (state = SCRIPTSTATE_INITIALIZED));
916     ok(hres == S_OK, "OnStateChange failed: %08x\n", hres);
917
918     hres = IActiveScriptSite_QueryInterface(pass, &IID_IActiveScriptSiteDebug, (void**)&debug);
919     ok(hres == S_OK, "Could not get IActiveScriptSiteDebug interface: %08x\n", hres);
920     if(SUCCEEDED(hres))
921         IActiveScriptSiteDebug32_Release(debug);
922
923     hres = IActiveScriptSite_QueryInterface(pass, &IID_ICanHandleException, (void**)&canexpection);
924     ok(hres == E_NOINTERFACE, "Could not get IID_ICanHandleException interface: %08x\n", hres);
925
926     hres = IActiveScriptSite_QueryInterface(pass, &IID_IServiceProvider, (void**)&service);
927     todo_wine ok(hres == S_OK, "Could not get IServiceProvider interface: %08x\n", hres);
928     if(SUCCEEDED(hres))
929         IServiceProvider_Release(service);
930
931     site = pass;
932     IActiveScriptSite_AddRef(site);
933     return S_OK;
934 }
935
936 static HRESULT WINAPI ActiveScript_GetScriptSite(IActiveScript *iface, REFIID riid,
937                                             void **ppvObject)
938 {
939     ok(0, "unexpected call\n");
940     return E_NOTIMPL;
941 }
942
943 static HRESULT WINAPI ActiveScript_SetScriptState(IActiveScript *iface, SCRIPTSTATE ss)
944 {
945     HRESULT hres;
946
947     switch(ss) {
948     case SCRIPTSTATE_STARTED:
949         CHECK_EXPECT(SetScriptState_STARTED);
950         break;
951     case SCRIPTSTATE_CONNECTED:
952         CHECK_EXPECT(SetScriptState_CONNECTED);
953         break;
954     case SCRIPTSTATE_DISCONNECTED:
955         CHECK_EXPECT(SetScriptState_DISCONNECTED);
956         break;
957     default:
958         ok(0, "unexpected state %d\n", ss);
959         return E_NOTIMPL;
960     }
961
962     hres = IActiveScriptSite_OnStateChange(site, (state = ss));
963     return S_OK;
964 }
965
966 static HRESULT WINAPI ActiveScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE *pssState)
967 {
968     CHECK_EXPECT(GetScriptState);
969
970     *pssState = state;
971     return S_OK;
972 }
973
974 static HRESULT WINAPI ActiveScript_Close(IActiveScript *iface)
975 {
976     CHECK_EXPECT(Close);
977     return E_NOTIMPL;
978 }
979
980 static HRESULT WINAPI ActiveScript_AddNamedItem(IActiveScript *iface,
981         LPCOLESTR pstrName, DWORD dwFlags)
982 {
983     IDispatch *disp;
984     IUnknown *unk = NULL, *unk2;
985     HRESULT hres;
986
987     static const WCHAR windowW[] = {'w','i','n','d','o','w',0};
988
989     static const IID unknown_iid = {0x719C3050,0xF9D3,0x11CF,{0xA4,0x93,0x00,0x40,0x05,0x23,0xA8,0xA0}};
990
991     CHECK_EXPECT(AddNamedItem);
992
993     ok(!lstrcmpW(pstrName, windowW), "pstrName=%s\n", wine_dbgstr_w(pstrName));
994     ok(dwFlags == (SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS), "dwFlags=%x\n", dwFlags);
995
996     hres = IActiveScriptSite_GetItemInfo(site, windowW, SCRIPTINFO_IUNKNOWN, &unk, NULL);
997     ok(hres == S_OK, "GetItemInfo failed: %08x\n", hres);
998     ok(unk != NULL, "unk == NULL\n");
999
1000     hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
1001     ok(hres == S_OK, "Could not get IDispatch interface: %08x\n", hres);
1002     if(SUCCEEDED(hres))
1003         IDispatch_Release(disp);
1004
1005     hres = IUnknown_QueryInterface(unk, &unknown_iid, (void**)&unk2);
1006     ok(hres == E_NOINTERFACE, "Got ?? interface: %p\n", unk2);
1007     if(SUCCEEDED(hres))
1008         IUnknown_Release(unk2);
1009
1010     hres = IUnknown_QueryInterface(unk, &IID_IDispatchEx, (void**)&window_dispex);
1011     ok(hres == S_OK, "Could not get IDispatchEx interface: %08x\n", hres);
1012
1013     IUnknown_Release(unk);
1014     return S_OK;
1015 }
1016
1017 static HRESULT WINAPI ActiveScript_AddTypeLib(IActiveScript *iface, REFGUID rguidTypeLib,
1018                                          DWORD dwMajor, DWORD dwMinor, DWORD dwFlags)
1019 {
1020     ok(0, "unexpected call\n");
1021     return E_NOTIMPL;
1022 }
1023
1024 static HRESULT WINAPI ActiveScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR pstrItemName,
1025                                                 IDispatch **ppdisp)
1026 {
1027     CHECK_EXPECT(GetScriptDispatch);
1028     return E_NOTIMPL;
1029 }
1030
1031 static HRESULT WINAPI ActiveScript_GetCurrentScriptThreadID(IActiveScript *iface,
1032                                                        SCRIPTTHREADID *pstridThread)
1033 {
1034     ok(0, "unexpected call\n");
1035     return E_NOTIMPL;
1036 }
1037
1038 static HRESULT WINAPI ActiveScript_GetScriptThreadID(IActiveScript *iface,
1039                                                 DWORD dwWin32ThreadId, SCRIPTTHREADID *pstidThread)
1040 {
1041     ok(0, "unexpected call\n");
1042     return E_NOTIMPL;
1043 }
1044
1045 static HRESULT WINAPI ActiveScript_GetScriptThreadState(IActiveScript *iface,
1046         SCRIPTTHREADID stidThread, SCRIPTTHREADSTATE *pstsState)
1047 {
1048     ok(0, "unexpected call\n");
1049     return E_NOTIMPL;
1050 }
1051
1052 static HRESULT WINAPI ActiveScript_InterruptScriptThread(IActiveScript *iface,
1053         SCRIPTTHREADID stidThread, const EXCEPINFO *pexcepinfo, DWORD dwFlags)
1054 {
1055     ok(0, "unexpected call\n");
1056     return E_NOTIMPL;
1057 }
1058
1059 static HRESULT WINAPI ActiveScript_Clone(IActiveScript *iface, IActiveScript **ppscript)
1060 {
1061     ok(0, "unexpected call\n");
1062     return E_NOTIMPL;
1063 }
1064
1065 static const IActiveScriptVtbl ActiveScriptVtbl = {
1066     ActiveScript_QueryInterface,
1067     ActiveScript_AddRef,
1068     ActiveScript_Release,
1069     ActiveScript_SetScriptSite,
1070     ActiveScript_GetScriptSite,
1071     ActiveScript_SetScriptState,
1072     ActiveScript_GetScriptState,
1073     ActiveScript_Close,
1074     ActiveScript_AddNamedItem,
1075     ActiveScript_AddTypeLib,
1076     ActiveScript_GetScriptDispatch,
1077     ActiveScript_GetCurrentScriptThreadID,
1078     ActiveScript_GetScriptThreadID,
1079     ActiveScript_GetScriptThreadState,
1080     ActiveScript_InterruptScriptThread,
1081     ActiveScript_Clone
1082 };
1083
1084 static IActiveScript ActiveScript = { &ActiveScriptVtbl };
1085
1086 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
1087 {
1088     *ppv = NULL;
1089
1090     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
1091         *ppv = iface;
1092         return S_OK;
1093     }
1094
1095     if(IsEqualGUID(&IID_IMarshal, riid))
1096         return E_NOINTERFACE;
1097     if(IsEqualGUID(&CLSID_IdentityUnmarshal, riid))
1098         return E_NOINTERFACE;
1099
1100     ok(0, "unexpected riid %s\n", debugstr_guid(riid));
1101     return E_NOTIMPL;
1102 }
1103
1104 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
1105 {
1106     return 2;
1107 }
1108
1109 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
1110 {
1111     return 1;
1112 }
1113
1114 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
1115 {
1116     CHECK_EXPECT(CreateInstance);
1117
1118     ok(!outer, "outer = %p\n", outer);
1119     ok(IsEqualGUID(&IID_IActiveScript, riid), "unexpected riid %s\n", debugstr_guid(riid));
1120     *ppv = &ActiveScript;
1121     return S_OK;
1122 }
1123
1124 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
1125 {
1126     ok(0, "unexpected call\n");
1127     return S_OK;
1128 }
1129
1130 static const IClassFactoryVtbl ClassFactoryVtbl = {
1131     ClassFactory_QueryInterface,
1132     ClassFactory_AddRef,
1133     ClassFactory_Release,
1134     ClassFactory_CreateInstance,
1135     ClassFactory_LockServer
1136 };
1137
1138 static IClassFactory script_cf = { &ClassFactoryVtbl };
1139
1140 static const char simple_script_str[] =
1141     "<html><head></head><body>"
1142     "<script language=\"TestScript\">simple script</script>"
1143     "</body></html>";
1144
1145 static void test_simple_script(void)
1146 {
1147     IHTMLDocument2 *doc;
1148
1149     SET_EXPECT(CreateInstance);
1150     SET_EXPECT(GetInterfaceSafetyOptions);
1151     SET_EXPECT(SetInterfaceSafetyOptions);
1152     SET_EXPECT(SetProperty_INVOKEVERSIONING); /* IE8 */
1153     SET_EXPECT(SetProperty_HACK_TRIDENTEVENTSINK);
1154     SET_EXPECT(InitNew);
1155     SET_EXPECT(SetScriptSite);
1156     SET_EXPECT(GetScriptState);
1157     SET_EXPECT(SetScriptState_STARTED);
1158     SET_EXPECT(AddNamedItem);
1159     SET_EXPECT(SetProperty_ABBREVIATE_GLOBALNAME_RESOLUTION); /* IE8 */
1160     SET_EXPECT(ParseScriptText);
1161     SET_EXPECT(SetScriptState_CONNECTED);
1162
1163     doc = create_and_load_doc(simple_script_str);
1164     if(!doc) return;
1165
1166     CHECK_CALLED(CreateInstance);
1167     CHECK_CALLED(GetInterfaceSafetyOptions);
1168     CHECK_CALLED(SetInterfaceSafetyOptions);
1169     CHECK_CALLED_BROKEN(SetProperty_INVOKEVERSIONING); /* IE8 */
1170     CHECK_CALLED(SetProperty_HACK_TRIDENTEVENTSINK);
1171     CHECK_CALLED(InitNew);
1172     CHECK_CALLED(SetScriptSite);
1173     CHECK_CALLED(GetScriptState);
1174     CHECK_CALLED(SetScriptState_STARTED);
1175     CHECK_CALLED(AddNamedItem);
1176     CHECK_CALLED_BROKEN(SetProperty_ABBREVIATE_GLOBALNAME_RESOLUTION); /* IE8 */
1177     CHECK_CALLED(ParseScriptText);
1178     CHECK_CALLED(SetScriptState_CONNECTED);
1179
1180     if(site)
1181         IActiveScriptSite_Release(site);
1182     if(window_dispex)
1183         IDispatchEx_Release(window_dispex);
1184
1185     SET_EXPECT(SetScriptState_DISCONNECTED);
1186     SET_EXPECT(Close);
1187
1188     IHTMLDocument2_Release(doc);
1189
1190     CHECK_CALLED(SetScriptState_DISCONNECTED);
1191     CHECK_CALLED(Close);
1192 }
1193
1194 static BOOL init_key(const char *key_name, const char *def_value, BOOL init)
1195 {
1196     HKEY hkey;
1197     DWORD res;
1198
1199     if(!init) {
1200         RegDeleteKey(HKEY_CLASSES_ROOT, key_name);
1201         return TRUE;
1202     }
1203
1204     res = RegCreateKeyA(HKEY_CLASSES_ROOT, key_name, &hkey);
1205     if(res != ERROR_SUCCESS)
1206         return FALSE;
1207
1208     if(def_value)
1209         res = RegSetValueA(hkey, NULL, REG_SZ, def_value, strlen(def_value));
1210
1211     RegCloseKey(hkey);
1212
1213     return res == ERROR_SUCCESS;
1214 }
1215
1216 static BOOL init_registry(BOOL init)
1217 {
1218     return init_key("TestScript\\CLSID", TESTSCRIPT_CLSID, init)
1219         && init_key("CLSID\\"TESTSCRIPT_CLSID"\\Implemented Categories\\{F0B7A1A1-9847-11CF-8F20-00805F2CD064}",
1220                     NULL, init)
1221         && init_key("CLSID\\"TESTSCRIPT_CLSID"\\Implemented Categories\\{F0B7A1A2-9847-11CF-8F20-00805F2CD064}",
1222                     NULL, init);
1223 }
1224
1225 static BOOL register_script_engine(void)
1226 {
1227     DWORD regid;
1228     HRESULT hres;
1229
1230     if(!init_registry(TRUE)) {
1231         init_registry(FALSE);
1232         return FALSE;
1233     }
1234
1235     hres = CoRegisterClassObject(&CLSID_TestScript, (IUnknown *)&script_cf,
1236                                  CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
1237     ok(hres == S_OK, "Could not register screipt engine: %08x\n", hres);
1238
1239     return TRUE;
1240 }
1241
1242 static void gecko_installer_workaround(BOOL disable)
1243 {
1244     HKEY hkey;
1245     DWORD res;
1246
1247     static BOOL has_url = FALSE;
1248     static char url[2048];
1249
1250     if(!disable && !has_url)
1251         return;
1252
1253     res = RegOpenKey(HKEY_CURRENT_USER, "Software\\Wine\\MSHTML", &hkey);
1254     if(res != ERROR_SUCCESS)
1255         return;
1256
1257     if(disable) {
1258         DWORD type, size = sizeof(url);
1259
1260         res = RegQueryValueEx(hkey, "GeckoUrl", NULL, &type, (PVOID)url, &size);
1261         if(res == ERROR_SUCCESS && type == REG_SZ)
1262             has_url = TRUE;
1263
1264         RegDeleteValue(hkey, "GeckoUrl");
1265     }else {
1266         RegSetValueEx(hkey, "GeckoUrl", 0, REG_SZ, (PVOID)url, lstrlenA(url)+1);
1267     }
1268
1269     RegCloseKey(hkey);
1270 }
1271
1272 /* Check if Internet Explorer is configured to run in "Enhanced Security Configuration" (aka hardened mode) */
1273 /* Note: this code is duplicated in dlls/mshtml/tests/dom.c, dlls/mshtml/tests/script.c and dlls/urlmon/tests/sec_mgr.c */
1274 static BOOL is_ie_hardened(void)
1275 {
1276     HKEY zone_map;
1277     DWORD ie_harden, type, size;
1278
1279     ie_harden = 0;
1280     if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap",
1281                     0, KEY_QUERY_VALUE, &zone_map) == ERROR_SUCCESS) {
1282         size = sizeof(DWORD);
1283         if (RegQueryValueEx(zone_map, "IEHarden", NULL, &type, (LPBYTE) &ie_harden, &size) != ERROR_SUCCESS ||
1284             type != REG_DWORD) {
1285             ie_harden = 0;
1286         }
1287     RegCloseKey(zone_map);
1288     }
1289
1290     return ie_harden != 0;
1291 }
1292
1293 START_TEST(script)
1294 {
1295     gecko_installer_workaround(TRUE);
1296     CoInitialize(NULL);
1297
1298     if(winetest_interactive || ! is_ie_hardened()) {
1299         if(register_script_engine()) {
1300             test_simple_script();
1301             init_registry(FALSE);
1302         }else {
1303             skip("Could not register TestScript engine\n");
1304         }
1305     }else {
1306         skip("IE running in Enhanced Security Configuration\n");
1307     }
1308
1309     CoUninitialize();
1310     gecko_installer_workaround(FALSE);
1311 }