mshtml: Added JavaScript tests architecture and simple tests.
[wine] / dlls / mshtml / tests / script.c
1 /*
2  * Copyright 2008-2009 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 "wininet.h"
30 #include "docobj.h"
31 #include "dispex.h"
32 #include "hlink.h"
33 #include "mshtml.h"
34 #include "mshtmhst.h"
35 #include "initguid.h"
36 #include "activscp.h"
37 #include "activdbg.h"
38 #include "objsafe.h"
39 #include "mshtmdid.h"
40 #include "mshtml_test.h"
41
42 DEFINE_GUID(CLSID_IdentityUnmarshal,0x0000001b,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
43
44 /* Defined as extern in urlmon.idl, but not exported by uuid.lib */
45 const GUID GUID_CUSTOM_CONFIRMOBJECTSAFETY =
46     {0x10200490,0xfa38,0x11d0,{0xac,0x0e,0x00,0xa0,0xc9,0xf,0xff,0xc0}};
47
48 #ifdef _WIN64
49
50 #define CTXARG_T DWORDLONG
51 #define IActiveScriptParseVtbl IActiveScriptParse64Vtbl
52 #define IActiveScriptParseProcedure2Vtbl IActiveScriptParseProcedure2_64Vtbl
53
54 #else
55
56 #define CTXARG_T DWORD
57 #define IActiveScriptParseVtbl IActiveScriptParse32Vtbl
58 #define IActiveScriptParseProcedure2Vtbl IActiveScriptParseProcedure2_32Vtbl
59
60 #endif
61
62 #define DEFINE_EXPECT(func) \
63     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
64
65 #define SET_EXPECT(func) \
66     do { called_ ## func = FALSE; expect_ ## func = TRUE; } while(0)
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 CHECK_CALLED_BROKEN(func) \
87     do { \
88         ok(called_ ## func || broken(!called_ ## func), "expected " #func "\n"); \
89         expect_ ## func = called_ ## func = FALSE; \
90     }while(0)
91
92 #define CHECK_NOT_CALLED(func) \
93     do { \
94         ok(!called_ ## func, "unexpected " #func "\n"); \
95         expect_ ## func = called_ ## func = FALSE; \
96     }while(0)
97
98 #define CLEAR_CALLED(func) \
99     expect_ ## func = called_ ## func = FALSE
100
101
102 DEFINE_EXPECT(CreateInstance);
103 DEFINE_EXPECT(GetInterfaceSafetyOptions);
104 DEFINE_EXPECT(SetInterfaceSafetyOptions);
105 DEFINE_EXPECT(InitNew);
106 DEFINE_EXPECT(Close);
107 DEFINE_EXPECT(SetProperty_HACK_TRIDENTEVENTSINK);
108 DEFINE_EXPECT(SetProperty_INVOKEVERSIONING);
109 DEFINE_EXPECT(SetProperty_ABBREVIATE_GLOBALNAME_RESOLUTION);
110 DEFINE_EXPECT(SetScriptSite);
111 DEFINE_EXPECT(GetScriptState);
112 DEFINE_EXPECT(SetScriptState_STARTED);
113 DEFINE_EXPECT(SetScriptState_CONNECTED);
114 DEFINE_EXPECT(SetScriptState_DISCONNECTED);
115 DEFINE_EXPECT(AddNamedItem);
116 DEFINE_EXPECT(ParseScriptText);
117 DEFINE_EXPECT(GetScriptDispatch);
118 DEFINE_EXPECT(funcDisp);
119 DEFINE_EXPECT(script_divid_d);
120 DEFINE_EXPECT(script_testprop_d);
121 DEFINE_EXPECT(script_testprop_i);
122 DEFINE_EXPECT(script_testprop2_d);
123 DEFINE_EXPECT(AXQueryInterface_IActiveScript);
124 DEFINE_EXPECT(AXQueryInterface_IObjectSafety);
125 DEFINE_EXPECT(AXGetInterfaceSafetyOptions);
126 DEFINE_EXPECT(AXSetInterfaceSafetyOptions);
127 DEFINE_EXPECT(external_success);
128
129 #define TESTSCRIPT_CLSID "{178fc163-f585-4e24-9c13-4bb7faf80746}"
130 #define TESTACTIVEX_CLSID "{178fc163-f585-4e24-9c13-4bb7faf80646}"
131
132 #define DISPID_SCRIPT_TESTPROP   0x100000
133 #define DISPID_SCRIPT_TESTPROP2  0x100001
134
135 #define DISPID_EXTERNAL_OK             0x300000
136 #define DISPID_EXTERNAL_TRACE          0x300001
137 #define DISPID_EXTERNAL_REPORTSUCCESS  0x300002
138
139 static const GUID CLSID_TestScript =
140     {0x178fc163,0xf585,0x4e24,{0x9c,0x13,0x4b,0xb7,0xfa,0xf8,0x07,0x46}};
141 static const GUID CLSID_TestActiveX =
142     {0x178fc163,0xf585,0x4e24,{0x9c,0x13,0x4b,0xb7,0xfa,0xf8,0x06,0x46}};
143
144 static IHTMLDocument2 *notif_doc;
145 static IOleDocumentView *view;
146 static IDispatchEx *window_dispex;
147 static BOOL doc_complete;
148 static IDispatch *script_disp;
149 static BOOL ax_objsafe;
150 static HWND container_hwnd;
151
152 static const char *debugstr_guid(REFIID riid)
153 {
154     static char buf[50];
155
156     sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
157             riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
158             riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
159             riid->Data4[5], riid->Data4[6], riid->Data4[7]);
160
161     return buf;
162 }
163
164 static int strcmp_wa(LPCWSTR strw, const char *stra)
165 {
166     CHAR buf[512];
167     WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), NULL, NULL);
168     return lstrcmpA(stra, buf);
169 }
170
171 static BSTR a2bstr(const char *str)
172 {
173     BSTR ret;
174     int len;
175
176     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
177     ret = SysAllocStringLen(NULL, len);
178     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
179
180     return ret;
181 }
182
183 static BOOL init_key(const char *key_name, const char *def_value, BOOL init)
184 {
185     HKEY hkey;
186     DWORD res;
187
188     if(!init) {
189         RegDeleteKey(HKEY_CLASSES_ROOT, key_name);
190         return TRUE;
191     }
192
193     res = RegCreateKeyA(HKEY_CLASSES_ROOT, key_name, &hkey);
194     if(res != ERROR_SUCCESS)
195         return FALSE;
196
197     if(def_value)
198         res = RegSetValueA(hkey, NULL, REG_SZ, def_value, strlen(def_value));
199
200     RegCloseKey(hkey);
201
202     return res == ERROR_SUCCESS;
203 }
204
205 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
206         REFIID riid, void**ppv)
207 {
208     if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
209         *ppv = iface;
210         return S_OK;
211     }
212
213     return E_NOINTERFACE;
214 }
215
216 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
217 {
218     return 2;
219 }
220
221 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
222 {
223     return 1;
224 }
225
226 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
227 {
228     if(dispID == DISPID_READYSTATE){
229         BSTR state;
230         HRESULT hres;
231
232         static const WCHAR completeW[] = {'c','o','m','p','l','e','t','e',0};
233
234         hres = IHTMLDocument2_get_readyState(notif_doc, &state);
235         ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
236
237         if(!lstrcmpW(state, completeW))
238             doc_complete = TRUE;
239
240         SysFreeString(state);
241     }
242
243     return S_OK;
244 }
245
246 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
247 {
248     ok(0, "unexpected call\n");
249     return E_NOTIMPL;
250 }
251
252 static IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
253     PropertyNotifySink_QueryInterface,
254     PropertyNotifySink_AddRef,
255     PropertyNotifySink_Release,
256     PropertyNotifySink_OnChanged,
257     PropertyNotifySink_OnRequestEdit
258 };
259
260 static IPropertyNotifySink PropertyNotifySink = { &PropertyNotifySinkVtbl };
261
262 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
263 {
264     *ppv = NULL;
265
266     if(IsEqualGUID(riid, &IID_IUnknown)
267        || IsEqualGUID(riid, &IID_IDispatch)
268        || IsEqualGUID(riid, &IID_IDispatchEx))
269         *ppv = iface;
270     else
271         return E_NOINTERFACE;
272
273     return S_OK;
274 }
275
276 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
277 {
278     return 2;
279 }
280
281 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
282 {
283     return 1;
284 }
285
286 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
287 {
288     ok(0, "unexpected call\n");
289     return E_NOTIMPL;
290 }
291
292 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
293                                               LCID lcid, ITypeInfo **ppTInfo)
294 {
295     ok(0, "unexpected call\n");
296     return E_NOTIMPL;
297 }
298
299 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
300                                                 LPOLESTR *rgszNames, UINT cNames,
301                                                 LCID lcid, DISPID *rgDispId)
302 {
303     ok(0, "unexpected call\n");
304     return E_NOTIMPL;
305 }
306
307 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
308                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
309                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
310 {
311     ok(0, "unexpected call\n");
312     return E_NOTIMPL;
313 }
314
315 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
316 {
317     ok(0, "unexpected call %s %x\n", wine_dbgstr_w(bstrName), grfdex);
318     return E_NOTIMPL;
319 }
320
321 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
322 {
323     ok(0, "unexpected call\n");
324     return E_NOTIMPL;
325 }
326
327 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
328 {
329     ok(0, "unexpected call\n");
330     return E_NOTIMPL;
331 }
332
333 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
334 {
335     ok(0, "unexpected call\n");
336     return E_NOTIMPL;
337 }
338
339 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
340 {
341     ok(0, "unexpected call\n");
342     return E_NOTIMPL;
343 }
344
345 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
346 {
347     ok(0, "unexpected call\n");
348     return E_NOTIMPL;
349 }
350
351 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
352 {
353     ok(0, "unexpected call\n");
354     return E_NOTIMPL;
355 }
356
357 static HRESULT WINAPI funcDisp_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
358         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
359 {
360     CHECK_EXPECT(funcDisp);
361
362     ok(id == DISPID_VALUE, "id = %d\n", id);
363     ok(lcid == 0, "lcid = %x\n", lcid);
364     ok(wFlags == DISPATCH_METHOD, "wFlags = %x\n", wFlags);
365     ok(pdp != NULL, "pdp == NULL\n");
366     ok(pdp->cArgs == 2, "pdp->cArgs = %d\n", pdp->cArgs);
367     ok(pdp->cNamedArgs == 1, "pdp->cNamedArgs = %d\n", pdp->cNamedArgs);
368     ok(pdp->rgdispidNamedArgs[0] == DISPID_THIS, "pdp->rgdispidNamedArgs[0] = %d\n", pdp->rgdispidNamedArgs[0]);
369     ok(V_VT(pdp->rgvarg) == VT_DISPATCH, "V_VT(rgvarg) = %d\n", V_VT(pdp->rgvarg));
370     ok(V_VT(pdp->rgvarg+1) == VT_BOOL, "V_VT(rgvarg[1]) = %d\n", V_VT(pdp->rgvarg));
371     ok(V_BOOL(pdp->rgvarg+1) == VARIANT_TRUE, "V_BOOL(rgvarg[1]) = %x\n", V_BOOL(pdp->rgvarg));
372     ok(pvarRes != NULL, "pvarRes == NULL\n");
373     ok(pei != NULL, "pei == NULL\n");
374     ok(!pspCaller, "pspCaller != NULL\n");
375
376     V_VT(pvarRes) = VT_I4;
377     V_I4(pvarRes) = 100;
378     return S_OK;
379 }
380
381 static IDispatchExVtbl testObjVtbl = {
382     DispatchEx_QueryInterface,
383     DispatchEx_AddRef,
384     DispatchEx_Release,
385     DispatchEx_GetTypeInfoCount,
386     DispatchEx_GetTypeInfo,
387     DispatchEx_GetIDsOfNames,
388     DispatchEx_Invoke,
389     DispatchEx_GetDispID,
390     funcDisp_InvokeEx,
391     DispatchEx_DeleteMemberByName,
392     DispatchEx_DeleteMemberByDispID,
393     DispatchEx_GetMemberProperties,
394     DispatchEx_GetMemberName,
395     DispatchEx_GetNextDispID,
396     DispatchEx_GetNameSpaceParent
397 };
398
399 static IDispatchEx funcDisp = { &testObjVtbl };
400
401 static HRESULT WINAPI scriptDisp_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
402 {
403     if(!strcmp_wa(bstrName, "testProp")) {
404         CHECK_EXPECT(script_testprop_d);
405         ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
406         *pid = DISPID_SCRIPT_TESTPROP;
407         return S_OK;
408     }
409
410     if(!strcmp_wa(bstrName, "testProp2")) {
411         CHECK_EXPECT(script_testprop2_d);
412         ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
413         *pid = DISPID_SCRIPT_TESTPROP2;
414         return S_OK;
415     }
416
417     if(!strcmp_wa(bstrName, "divid")) {
418         CHECK_EXPECT(script_divid_d);
419         ok(grfdex == fdexNameCaseSensitive, "grfdex = %x\n", grfdex);
420         return E_FAIL;
421     }
422
423     ok(0, "unexpected call\n");
424     return E_NOTIMPL;
425 }
426
427 static HRESULT WINAPI scriptDisp_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
428         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
429 {
430     switch(id) {
431     case DISPID_SCRIPT_TESTPROP:
432         CHECK_EXPECT(script_testprop_i);
433
434         ok(lcid == 0, "lcid = %x\n", lcid);
435         ok(wFlags == DISPATCH_PROPERTYGET, "wFlags = %x\n", wFlags);
436         ok(pdp != NULL, "pdp == NULL\n");
437         ok(pdp->cArgs == 0, "pdp->cArgs = %d\n", pdp->cArgs);
438         ok(pdp->cNamedArgs == 0, "pdp->cNamedArgs = %d\n", pdp->cNamedArgs);
439         ok(!pdp->rgdispidNamedArgs, "pdp->rgdispidNamedArgs != NULL\n");
440         ok(!pdp->rgvarg, "rgvarg != NULL\n");
441         ok(pvarRes != NULL, "pvarRes == NULL\n");
442         ok(pei != NULL, "pei == NULL\n");
443         ok(!pspCaller, "pspCaller != NULL\n");
444
445         V_VT(pvarRes) = VT_NULL;
446         break;
447     default:
448         ok(0, "unexpected call\n");
449         return E_NOTIMPL;
450     }
451
452     return S_OK;
453 }
454
455 static IDispatchExVtbl scriptDispVtbl = {
456     DispatchEx_QueryInterface,
457     DispatchEx_AddRef,
458     DispatchEx_Release,
459     DispatchEx_GetTypeInfoCount,
460     DispatchEx_GetTypeInfo,
461     DispatchEx_GetIDsOfNames,
462     DispatchEx_Invoke,
463     scriptDisp_GetDispID,
464     scriptDisp_InvokeEx,
465     DispatchEx_DeleteMemberByName,
466     DispatchEx_DeleteMemberByDispID,
467     DispatchEx_GetMemberProperties,
468     DispatchEx_GetMemberName,
469     DispatchEx_GetNextDispID,
470     DispatchEx_GetNameSpaceParent
471 };
472
473 static IDispatchEx scriptDisp = { &scriptDispVtbl };
474
475 static HRESULT WINAPI externalDisp_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
476 {
477     if(!strcmp_wa(bstrName, "ok")) {
478         *pid = DISPID_EXTERNAL_OK;
479         return S_OK;
480     }
481     if(!strcmp_wa(bstrName, "trace")) {
482         *pid = DISPID_EXTERNAL_TRACE;
483         return S_OK;
484     }
485     if(!strcmp_wa(bstrName, "reportSuccess")) {
486         *pid = DISPID_EXTERNAL_REPORTSUCCESS;
487         return S_OK;
488     }
489
490     ok(0, "unexpected name %s\n", wine_dbgstr_w(bstrName));
491     return DISP_E_UNKNOWNNAME;
492 }
493
494 static HRESULT WINAPI externalDisp_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
495         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
496 {
497     switch(id) {
498     case DISPID_EXTERNAL_OK:
499         ok(wFlags == INVOKE_FUNC || wFlags == (INVOKE_FUNC|INVOKE_PROPERTYGET), "wFlags = %x\n", wFlags);
500         ok(pdp != NULL, "pdp == NULL\n");
501         ok(pdp->rgvarg != NULL, "rgvarg == NULL\n");
502         ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
503         ok(pdp->cArgs == 2, "cArgs = %d\n", pdp->cArgs);
504         ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
505         ok(pei != NULL, "pei == NULL\n");
506
507         ok(V_VT(pdp->rgvarg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(pdp->rgvarg));
508         ok(V_VT(pdp->rgvarg+1) == VT_BOOL, "V_VT(psp->rgvargs+1) = %d\n", V_VT(pdp->rgvarg));
509         ok(V_BOOL(pdp->rgvarg+1), "%s\n", wine_dbgstr_w(V_BSTR(pdp->rgvarg)));
510
511         return S_OK;
512
513      case DISPID_EXTERNAL_TRACE:
514         ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
515         ok(pdp != NULL, "pdp == NULL\n");
516         ok(pdp->rgvarg != NULL, "rgvarg == NULL\n");
517         ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
518         ok(pdp->cArgs == 1, "cArgs = %d\n", pdp->cArgs);
519         ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
520         ok(!pvarRes, "pvarRes != NULL\n");
521         ok(pei != NULL, "pei == NULL\n");
522
523         ok(V_VT(pdp->rgvarg) == VT_BSTR, "V_VT(psp->rgvargs) = %d\n", V_VT(pdp->rgvarg));
524         if(V_VT(pdp->rgvarg) == VT_BSTR)
525             trace("%s\n", wine_dbgstr_w(V_BSTR(pdp->rgvarg)));
526
527         return S_OK;
528
529     case DISPID_EXTERNAL_REPORTSUCCESS:
530         CHECK_EXPECT(external_success);
531
532         ok(wFlags == INVOKE_FUNC, "wFlags = %x\n", wFlags);
533         ok(pdp != NULL, "pdp == NULL\n");
534         ok(!pdp->rgdispidNamedArgs, "rgdispidNamedArgs != NULL\n");
535         ok(pdp->cArgs == 0, "cArgs = %d\n", pdp->cArgs);
536         ok(!pdp->cNamedArgs, "cNamedArgs = %d\n", pdp->cNamedArgs);
537         ok(!pvarRes, "pvarRes != NULL\n");
538         ok(pei != NULL, "pei == NULL\n");
539
540         return S_OK;
541
542     default:
543         ok(0, "unexpected call\n");
544         return E_NOTIMPL;
545     }
546
547     return S_OK;
548 }
549
550 static IDispatchExVtbl externalDispVtbl = {
551     DispatchEx_QueryInterface,
552     DispatchEx_AddRef,
553     DispatchEx_Release,
554     DispatchEx_GetTypeInfoCount,
555     DispatchEx_GetTypeInfo,
556     DispatchEx_GetIDsOfNames,
557     DispatchEx_Invoke,
558     externalDisp_GetDispID,
559     externalDisp_InvokeEx,
560     DispatchEx_DeleteMemberByName,
561     DispatchEx_DeleteMemberByDispID,
562     DispatchEx_GetMemberProperties,
563     DispatchEx_GetMemberName,
564     DispatchEx_GetNextDispID,
565     DispatchEx_GetNameSpaceParent
566 };
567
568 static IDispatchEx externalDisp = { &externalDispVtbl };
569
570 static HRESULT QueryInterface(REFIID,void**);
571
572 static HRESULT WINAPI DocHostUIHandler_QueryInterface(IDocHostUIHandler2 *iface, REFIID riid, void **ppv)
573 {
574     return QueryInterface(riid, ppv);
575 }
576
577 static ULONG WINAPI DocHostUIHandler_AddRef(IDocHostUIHandler2 *iface)
578 {
579     return 2;
580 }
581
582 static ULONG WINAPI DocHostUIHandler_Release(IDocHostUIHandler2 *iface)
583 {
584     return 1;
585 }
586
587 static HRESULT WINAPI DocHostUIHandler_ShowContextMenu(IDocHostUIHandler2 *iface, DWORD dwID, POINT *ppt,
588         IUnknown *pcmdtReserved, IDispatch *pdicpReserved)
589 {
590     return E_NOTIMPL;
591 }
592
593 static HRESULT WINAPI DocHostUIHandler_GetHostInfo(IDocHostUIHandler2 *iface, DOCHOSTUIINFO *pInfo)
594 {
595     return E_NOTIMPL;
596 }
597
598 static HRESULT WINAPI DocHostUIHandler_ShowUI(IDocHostUIHandler2 *iface, DWORD dwID,
599         IOleInPlaceActiveObject *pActiveObject, IOleCommandTarget *pCommandTarget,
600         IOleInPlaceFrame *pFrame, IOleInPlaceUIWindow *pDoc)
601 {
602     return S_OK;
603 }
604
605 static HRESULT WINAPI DocHostUIHandler_HideUI(IDocHostUIHandler2 *iface)
606 {
607     return S_OK;
608 }
609
610 static HRESULT WINAPI DocHostUIHandler_UpdateUI(IDocHostUIHandler2 *iface)
611 {
612     return S_OK;
613 }
614
615 static HRESULT WINAPI DocHostUIHandler_EnableModeless(IDocHostUIHandler2 *iface, BOOL fEnable)
616 {
617     return E_NOTIMPL;
618 }
619
620 static HRESULT WINAPI DocHostUIHandler_OnDocWindowActivate(IDocHostUIHandler2 *iface, BOOL fActivate)
621 {
622     return E_NOTIMPL;
623 }
624
625 static HRESULT WINAPI DocHostUIHandler_OnFrameWindowActivate(IDocHostUIHandler2 *iface, BOOL fActivate)
626 {
627     return S_OK;
628 }
629
630 static HRESULT WINAPI DocHostUIHandler_ResizeBorder(IDocHostUIHandler2 *iface, LPCRECT prcBorder,
631         IOleInPlaceUIWindow *pUIWindow, BOOL fRameWindow)
632 {
633     return E_NOTIMPL;
634 }
635
636 static HRESULT WINAPI DocHostUIHandler_TranslateAccelerator(IDocHostUIHandler2 *iface, LPMSG lpMsg,
637         const GUID *pguidCmdGroup, DWORD nCmdID)
638 {
639     return E_NOTIMPL;
640 }
641
642 static HRESULT WINAPI DocHostUIHandler_GetOptionKeyPath(IDocHostUIHandler2 *iface,
643         LPOLESTR *pchKey, DWORD dw)
644 {
645     return S_OK;
646 }
647
648 static HRESULT WINAPI DocHostUIHandler_GetDropTarget(IDocHostUIHandler2 *iface,
649         IDropTarget *pDropTarget, IDropTarget **ppDropTarget)
650 {
651     return E_NOTIMPL;
652 }
653
654 static HRESULT WINAPI DocHostUIHandler_GetExternal(IDocHostUIHandler2 *iface, IDispatch **ppDispatch)
655 {
656     *ppDispatch = (IDispatch*)&externalDisp;
657     return S_OK;
658 }
659
660 static HRESULT WINAPI DocHostUIHandler_TranslateUrl(IDocHostUIHandler2 *iface, DWORD dwTranslate,
661         OLECHAR *pchURLIn, OLECHAR **ppchURLOut)
662 {
663     return S_FALSE;
664 }
665
666 static HRESULT WINAPI DocHostUIHandler_FilterDataObject(IDocHostUIHandler2 *iface, IDataObject *pDO,
667         IDataObject **ppPORet)
668 {
669     return E_NOTIMPL;
670 }
671
672 static HRESULT WINAPI DocHostUIHandler_GetOverrideKeyPath(IDocHostUIHandler2 *iface,
673         LPOLESTR *pchKey, DWORD dw)
674 {
675     return E_NOTIMPL;
676 }
677
678 static const IDocHostUIHandler2Vtbl DocHostUIHandlerVtbl = {
679     DocHostUIHandler_QueryInterface,
680     DocHostUIHandler_AddRef,
681     DocHostUIHandler_Release,
682     DocHostUIHandler_ShowContextMenu,
683     DocHostUIHandler_GetHostInfo,
684     DocHostUIHandler_ShowUI,
685     DocHostUIHandler_HideUI,
686     DocHostUIHandler_UpdateUI,
687     DocHostUIHandler_EnableModeless,
688     DocHostUIHandler_OnDocWindowActivate,
689     DocHostUIHandler_OnFrameWindowActivate,
690     DocHostUIHandler_ResizeBorder,
691     DocHostUIHandler_TranslateAccelerator,
692     DocHostUIHandler_GetOptionKeyPath,
693     DocHostUIHandler_GetDropTarget,
694     DocHostUIHandler_GetExternal,
695     DocHostUIHandler_TranslateUrl,
696     DocHostUIHandler_FilterDataObject,
697     DocHostUIHandler_GetOverrideKeyPath
698 };
699
700 static IDocHostUIHandler2 DocHostUIHandler = { &DocHostUIHandlerVtbl };
701
702 static HRESULT WINAPI InPlaceFrame_QueryInterface(IOleInPlaceFrame *iface, REFIID riid, void **ppv)
703 {
704     return E_NOINTERFACE;
705 }
706
707 static ULONG WINAPI InPlaceFrame_AddRef(IOleInPlaceFrame *iface)
708 {
709     return 2;
710 }
711
712 static ULONG WINAPI InPlaceFrame_Release(IOleInPlaceFrame *iface)
713 {
714     return 1;
715 }
716
717 static HRESULT WINAPI InPlaceFrame_GetWindow(IOleInPlaceFrame *iface, HWND *phwnd)
718 {
719     return E_NOTIMPL;
720 }
721
722 static HRESULT WINAPI InPlaceFrame_ContextSensitiveHelp(IOleInPlaceFrame *iface, BOOL fEnterMode)
723 {
724     return E_NOTIMPL;
725 }
726
727 static HRESULT WINAPI InPlaceFrame_GetBorder(IOleInPlaceFrame *iface, LPRECT lprectBorder)
728 {
729     return E_NOTIMPL;
730 }
731
732 static HRESULT WINAPI InPlaceFrame_RequestBorderSpace(IOleInPlaceFrame *iface,
733         LPCBORDERWIDTHS pborderwidths)
734 {
735     return E_NOTIMPL;
736 }
737
738 static HRESULT WINAPI InPlaceFrame_SetBorderSpace(IOleInPlaceFrame *iface,
739         LPCBORDERWIDTHS pborderwidths)
740 {
741     return S_OK;
742 }
743
744 static HRESULT WINAPI InPlaceFrame_SetActiveObject(IOleInPlaceFrame *iface,
745         IOleInPlaceActiveObject *pActiveObject, LPCOLESTR pszObjName)
746 {
747     return S_OK;
748 }
749
750 static HRESULT WINAPI InPlaceFrame_InsertMenus(IOleInPlaceFrame *iface, HMENU hmenuShared,
751         LPOLEMENUGROUPWIDTHS lpMenuWidths)
752 {
753     return E_NOTIMPL;
754 }
755
756 static HRESULT WINAPI InPlaceFrame_SetMenu(IOleInPlaceFrame *iface, HMENU hmenuShared,
757         HOLEMENU holemenu, HWND hwndActiveObject)
758 {
759     ok(0, "unexpected call\n");
760     return E_NOTIMPL;
761 }
762
763 static HRESULT WINAPI InPlaceFrame_RemoveMenus(IOleInPlaceFrame *iface, HMENU hmenuShared)
764 {
765     ok(0, "unexpected call\n");
766     return E_NOTIMPL;
767 }
768
769 static HRESULT WINAPI InPlaceFrame_SetStatusText(IOleInPlaceFrame *iface, LPCOLESTR pszStatusText)
770 {
771     return S_OK;
772 }
773
774 static HRESULT WINAPI InPlaceFrame_EnableModeless(IOleInPlaceFrame *iface, BOOL fEnable)
775 {
776     return E_NOTIMPL;
777 }
778
779 static HRESULT WINAPI InPlaceFrame_TranslateAccelerator(IOleInPlaceFrame *iface, LPMSG lpmsg, WORD wID)
780 {
781     ok(0, "unexpected call\n");
782     return E_NOTIMPL;
783 }
784
785 static const IOleInPlaceFrameVtbl InPlaceFrameVtbl = {
786     InPlaceFrame_QueryInterface,
787     InPlaceFrame_AddRef,
788     InPlaceFrame_Release,
789     InPlaceFrame_GetWindow,
790     InPlaceFrame_ContextSensitiveHelp,
791     InPlaceFrame_GetBorder,
792     InPlaceFrame_RequestBorderSpace,
793     InPlaceFrame_SetBorderSpace,
794     InPlaceFrame_SetActiveObject,
795     InPlaceFrame_InsertMenus,
796     InPlaceFrame_SetMenu,
797     InPlaceFrame_RemoveMenus,
798     InPlaceFrame_SetStatusText,
799     InPlaceFrame_EnableModeless,
800     InPlaceFrame_TranslateAccelerator
801 };
802
803 static IOleInPlaceFrame InPlaceFrame = { &InPlaceFrameVtbl };
804
805 static HRESULT WINAPI InPlaceSite_QueryInterface(IOleInPlaceSite *iface, REFIID riid, void **ppv)
806 {
807     return QueryInterface(riid, ppv);
808 }
809
810 static ULONG WINAPI InPlaceSite_AddRef(IOleInPlaceSite *iface)
811 {
812     return 2;
813 }
814
815 static ULONG WINAPI InPlaceSite_Release(IOleInPlaceSite *iface)
816 {
817     return 1;
818 }
819
820 static HRESULT WINAPI InPlaceSite_GetWindow(IOleInPlaceSite *iface, HWND *phwnd)
821 {
822     *phwnd = container_hwnd;
823     return S_OK;
824 }
825
826 static HRESULT WINAPI InPlaceSite_ContextSensitiveHelp(IOleInPlaceSite *iface, BOOL fEnterMode)
827 {
828     ok(0, "unexpected call\n");
829     return E_NOTIMPL;
830 }
831
832 static HRESULT WINAPI InPlaceSite_CanInPlaceActivate(IOleInPlaceSite *iface)
833 {
834     return S_OK;
835 }
836
837 static HRESULT WINAPI InPlaceSite_OnInPlaceActivate(IOleInPlaceSite *iface)
838 {
839     return S_OK;
840 }
841
842 static HRESULT WINAPI InPlaceSite_OnUIActivate(IOleInPlaceSite *iface)
843 {
844     return S_OK;
845 }
846
847 static HRESULT WINAPI InPlaceSite_GetWindowContext(IOleInPlaceSite *iface,
848         IOleInPlaceFrame **ppFrame, IOleInPlaceUIWindow **ppDoc, LPRECT lprcPosRect,
849         LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo)
850 {
851     static const RECT rect = {0,0,300,300};
852
853     *ppFrame = &InPlaceFrame;
854     *ppDoc = (IOleInPlaceUIWindow*)&InPlaceFrame;
855     *lprcPosRect = rect;
856     *lprcClipRect = rect;
857
858     lpFrameInfo->cb = sizeof(*lpFrameInfo);
859     lpFrameInfo->fMDIApp = FALSE;
860     lpFrameInfo->hwndFrame = container_hwnd;
861     lpFrameInfo->haccel = NULL;
862     lpFrameInfo->cAccelEntries = 0;
863
864     return S_OK;
865 }
866
867 static HRESULT WINAPI InPlaceSite_Scroll(IOleInPlaceSite *iface, SIZE scrollExtant)
868 {
869     return E_NOTIMPL;
870 }
871
872 static HRESULT WINAPI InPlaceSite_OnUIDeactivate(IOleInPlaceSite *iface, BOOL fUndoable)
873 {
874     return S_OK;
875 }
876
877 static HRESULT WINAPI InPlaceSite_OnInPlaceDeactivate(IOleInPlaceSite *iface)
878 {
879     return S_OK;
880 }
881
882 static HRESULT WINAPI InPlaceSite_DiscardUndoState(IOleInPlaceSite *iface)
883 {
884     return E_NOTIMPL;
885 }
886
887 static HRESULT WINAPI InPlaceSite_DeactivateAndUndo(IOleInPlaceSite *iface)
888 {
889     return E_NOTIMPL;
890 }
891
892 static HRESULT WINAPI InPlaceSite_OnPosRectChange(IOleInPlaceSite *iface, LPCRECT lprcPosRect)
893 {
894     return E_NOTIMPL;
895 }
896
897 static const IOleInPlaceSiteVtbl InPlaceSiteVtbl = {
898     InPlaceSite_QueryInterface,
899     InPlaceSite_AddRef,
900     InPlaceSite_Release,
901     InPlaceSite_GetWindow,
902     InPlaceSite_ContextSensitiveHelp,
903     InPlaceSite_CanInPlaceActivate,
904     InPlaceSite_OnInPlaceActivate,
905     InPlaceSite_OnUIActivate,
906     InPlaceSite_GetWindowContext,
907     InPlaceSite_Scroll,
908     InPlaceSite_OnUIDeactivate,
909     InPlaceSite_OnInPlaceDeactivate,
910     InPlaceSite_DiscardUndoState,
911     InPlaceSite_DeactivateAndUndo,
912     InPlaceSite_OnPosRectChange,
913 };
914
915 static IOleInPlaceSite InPlaceSite = { &InPlaceSiteVtbl };
916
917 static HRESULT WINAPI ClientSite_QueryInterface(IOleClientSite *iface, REFIID riid, void **ppv)
918 {
919     return QueryInterface(riid, ppv);
920 }
921
922 static ULONG WINAPI ClientSite_AddRef(IOleClientSite *iface)
923 {
924     return 2;
925 }
926
927 static ULONG WINAPI ClientSite_Release(IOleClientSite *iface)
928 {
929     return 1;
930 }
931
932 static HRESULT WINAPI ClientSite_SaveObject(IOleClientSite *iface)
933 {
934     ok(0, "unexpected call\n");
935     return E_NOTIMPL;
936 }
937
938 static HRESULT WINAPI ClientSite_GetMoniker(IOleClientSite *iface, DWORD dwAssign, DWORD dwWhichMoniker,
939         IMoniker **ppmon)
940 {
941     ok(0, "unexpected call\n");
942     return E_NOTIMPL;
943 }
944
945 static HRESULT WINAPI ClientSite_GetContainer(IOleClientSite *iface, IOleContainer **ppContainer)
946 {
947     return E_NOTIMPL;
948 }
949
950 static HRESULT WINAPI ClientSite_ShowObject(IOleClientSite *iface)
951 {
952     ok(0, "unexpected call\n");
953     return E_NOTIMPL;
954 }
955
956 static HRESULT WINAPI ClientSite_OnShowWindow(IOleClientSite *iface, BOOL fShow)
957 {
958     ok(0, "unexpected call\n");
959     return E_NOTIMPL;
960 }
961
962 static HRESULT WINAPI ClientSite_RequestNewObjectLayout(IOleClientSite *iface)
963 {
964     ok(0, "unexpected call\n");
965     return E_NOTIMPL;
966 }
967
968 static const IOleClientSiteVtbl ClientSiteVtbl = {
969     ClientSite_QueryInterface,
970     ClientSite_AddRef,
971     ClientSite_Release,
972     ClientSite_SaveObject,
973     ClientSite_GetMoniker,
974     ClientSite_GetContainer,
975     ClientSite_ShowObject,
976     ClientSite_OnShowWindow,
977     ClientSite_RequestNewObjectLayout
978 };
979
980 static IOleClientSite ClientSite = { &ClientSiteVtbl };
981
982 static HRESULT WINAPI DocumentSite_QueryInterface(IOleDocumentSite *iface, REFIID riid, void **ppv)
983 {
984     return QueryInterface(riid, ppv);
985 }
986
987 static ULONG WINAPI DocumentSite_AddRef(IOleDocumentSite *iface)
988 {
989     return 2;
990 }
991
992 static ULONG WINAPI DocumentSite_Release(IOleDocumentSite *iface)
993 {
994     return 1;
995 }
996
997 static HRESULT WINAPI DocumentSite_ActivateMe(IOleDocumentSite *iface, IOleDocumentView *pViewToActivate)
998 {
999     RECT rect = {0,0,300,300};
1000     IOleDocument *document;
1001     HRESULT hres;
1002
1003     hres = IOleDocumentView_QueryInterface(pViewToActivate, &IID_IOleDocument, (void**)&document);
1004     ok(hres == S_OK, "could not get IOleDocument: %08x\n", hres);
1005
1006     hres = IOleDocument_CreateView(document, &InPlaceSite, NULL, 0, &view);
1007     IOleDocument_Release(document);
1008     ok(hres == S_OK, "CreateView failed: %08x\n", hres);
1009
1010     hres = IOleDocumentView_SetInPlaceSite(view, &InPlaceSite);
1011     ok(hres == S_OK, "SetInPlaceSite failed: %08x\n", hres);
1012
1013     hres = IOleDocumentView_UIActivate(view, TRUE);
1014     ok(hres == S_OK, "UIActivate failed: %08x\n", hres);
1015
1016     hres = IOleDocumentView_SetRect(view, &rect);
1017     ok(hres == S_OK, "SetRect failed: %08x\n", hres);
1018
1019     hres = IOleDocumentView_Show(view, TRUE);
1020     ok(hres == S_OK, "Show failed: %08x\n", hres);
1021
1022     return S_OK;
1023 }
1024
1025 static const IOleDocumentSiteVtbl DocumentSiteVtbl = {
1026     DocumentSite_QueryInterface,
1027     DocumentSite_AddRef,
1028     DocumentSite_Release,
1029     DocumentSite_ActivateMe
1030 };
1031
1032 static IOleDocumentSite DocumentSite = { &DocumentSiteVtbl };
1033
1034 static HRESULT QueryInterface(REFIID riid, void **ppv)
1035 {
1036     *ppv = NULL;
1037
1038     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IOleClientSite, riid))
1039         *ppv = &ClientSite;
1040     else if(IsEqualGUID(&IID_IOleDocumentSite, riid))
1041         *ppv = &DocumentSite;
1042     else if(IsEqualGUID(&IID_IOleWindow, riid) || IsEqualGUID(&IID_IOleInPlaceSite, riid))
1043         *ppv = &InPlaceSite;
1044     else if(IsEqualGUID(&IID_IDocHostUIHandler, riid) || IsEqualGUID(&IID_IDocHostUIHandler2, riid))
1045         *ppv = &DocHostUIHandler;
1046
1047     return *ppv ? S_OK : E_NOINTERFACE;
1048 }
1049
1050 static IHTMLDocument2 *create_document(void)
1051 {
1052     IHTMLDocument2 *doc;
1053     HRESULT hres;
1054
1055     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
1056             &IID_IHTMLDocument2, (void**)&doc);
1057     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
1058
1059     return doc;
1060 }
1061
1062 static void load_string(IHTMLDocument2 *doc, const char *str)
1063 {
1064     IPersistStreamInit *init;
1065     IStream *stream;
1066     HGLOBAL mem;
1067     SIZE_T len;
1068
1069     doc_complete = FALSE;
1070     len = strlen(str);
1071     mem = GlobalAlloc(0, len);
1072     memcpy(mem, str, len);
1073     CreateStreamOnHGlobal(mem, TRUE, &stream);
1074
1075     IHTMLDocument2_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&init);
1076
1077     IPersistStreamInit_Load(init, stream);
1078     IPersistStreamInit_Release(init);
1079     IStream_Release(stream);
1080 }
1081
1082 static void do_advise(IHTMLDocument2 *doc, REFIID riid, IUnknown *unk_advise)
1083 {
1084     IConnectionPointContainer *container;
1085     IConnectionPoint *cp;
1086     DWORD cookie;
1087     HRESULT hres;
1088
1089     hres = IHTMLDocument2_QueryInterface(doc, &IID_IConnectionPointContainer, (void**)&container);
1090     ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
1091
1092     hres = IConnectionPointContainer_FindConnectionPoint(container, riid, &cp);
1093     IConnectionPointContainer_Release(container);
1094     ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
1095
1096     notif_doc = doc;
1097
1098     hres = IConnectionPoint_Advise(cp, unk_advise, &cookie);
1099     IConnectionPoint_Release(cp);
1100     ok(hres == S_OK, "Advise failed: %08x\n", hres);
1101 }
1102
1103 static void set_client_site(IHTMLDocument2 *doc, BOOL set)
1104 {
1105     IOleObject *oleobj;
1106     HRESULT hres;
1107
1108     if(!set && view) {
1109         IOleDocumentView_Show(view, FALSE);
1110         IOleDocumentView_CloseView(view, 0);
1111         IOleDocumentView_SetInPlaceSite(view, NULL);
1112         IOleDocumentView_Release(view);
1113         view = NULL;
1114     }
1115
1116     hres = IHTMLDocument2_QueryInterface(doc, &IID_IOleObject, (void**)&oleobj);
1117     ok(hres == S_OK, "Could not et IOleObject: %08x\n", hres);
1118
1119     hres = IOleObject_SetClientSite(oleobj, set ? &ClientSite : NULL);
1120     ok(hres == S_OK, "SetClientSite failed: %08x\n", hres);
1121
1122     if(set) {
1123         IHlinkTarget *hlink;
1124
1125         hres = IOleObject_QueryInterface(oleobj, &IID_IHlinkTarget, (void**)&hlink);
1126         ok(hres == S_OK, "Could not get IHlinkTarget iface: %08x\n", hres);
1127
1128         hres = IHlinkTarget_Navigate(hlink, 0, NULL);
1129         ok(hres == S_OK, "Navgate failed: %08x\n", hres);
1130
1131         IHlinkTarget_Release(hlink);
1132     }
1133
1134     IOleObject_Release(oleobj);
1135 }
1136
1137 typedef void (*domtest_t)(IHTMLDocument2*);
1138
1139 static void load_doc(IHTMLDocument2 *doc, const char *str)
1140 {
1141     IHTMLElement *body = NULL;
1142     MSG msg;
1143     HRESULT hres;
1144     static const WCHAR ucPtr[] = {'b','a','c','k','g','r','o','u','n','d',0};
1145     DISPID dispID = -1;
1146     OLECHAR *name;
1147
1148     load_string(doc, str);
1149     do_advise(doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
1150
1151     while(!doc_complete && GetMessage(&msg, NULL, 0, 0)) {
1152         TranslateMessage(&msg);
1153         DispatchMessage(&msg);
1154     }
1155
1156     hres = IHTMLDocument2_get_body(doc, &body);
1157     ok(hres == S_OK, "get_body failed: %08x\n", hres);
1158
1159     /* Check we can query for function on the IHTMLElementBody interface */
1160     name = (WCHAR*)ucPtr;
1161     hres = IHTMLElement_GetIDsOfNames(body, &IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &dispID);
1162     ok(hres == S_OK, "GetIDsOfNames(background) failed %08x\n", hres);
1163     ok(dispID == DISPID_IHTMLBODYELEMENT_BACKGROUND, "Incorrect dispID got (%d)\n", dispID);
1164
1165     IHTMLElement_Release(body);
1166 }
1167
1168 static IActiveScriptSite *site;
1169 static SCRIPTSTATE state;
1170
1171 static HRESULT WINAPI ObjectSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
1172 {
1173     *ppv = NULL;
1174     ok(0, "unexpected call %s\n", debugstr_guid(riid));
1175     return E_NOINTERFACE;
1176 }
1177
1178 static ULONG WINAPI ObjectSafety_AddRef(IObjectSafety *iface)
1179 {
1180     return 2;
1181 }
1182
1183 static ULONG WINAPI ObjectSafety_Release(IObjectSafety *iface)
1184 {
1185     return 1;
1186 }
1187
1188 static HRESULT WINAPI ObjectSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1189         DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
1190 {
1191     CHECK_EXPECT(GetInterfaceSafetyOptions);
1192
1193     ok(IsEqualGUID(&IID_IActiveScriptParse, riid), "unexpected riid %s\n", debugstr_guid(riid));
1194     ok(pdwSupportedOptions != NULL, "pdwSupportedOptions == NULL\n");
1195     ok(pdwEnabledOptions != NULL, "pdwEnabledOptions == NULL\n");
1196
1197     *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER;
1198     *pdwEnabledOptions = INTERFACE_USES_DISPEX;
1199
1200     return S_OK;
1201 }
1202
1203 static HRESULT WINAPI ObjectSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1204         DWORD dwOptionSetMask, DWORD dwEnabledOptions)
1205 {
1206     CHECK_EXPECT(SetInterfaceSafetyOptions);
1207
1208     ok(IsEqualGUID(&IID_IActiveScriptParse, riid), "unexpected riid %s\n", debugstr_guid(riid));
1209
1210     ok(dwOptionSetMask == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
1211        "dwOptionSetMask=%x\n", dwOptionSetMask);
1212     ok(dwEnabledOptions == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
1213        "dwEnabledOptions=%x\n", dwOptionSetMask);
1214
1215     return S_OK;
1216 }
1217
1218 static const IObjectSafetyVtbl ObjectSafetyVtbl = {
1219     ObjectSafety_QueryInterface,
1220     ObjectSafety_AddRef,
1221     ObjectSafety_Release,
1222     ObjectSafety_GetInterfaceSafetyOptions,
1223     ObjectSafety_SetInterfaceSafetyOptions
1224 };
1225
1226 static IObjectSafety ObjectSafety = { &ObjectSafetyVtbl };
1227
1228 static HRESULT WINAPI AXObjectSafety_QueryInterface(IObjectSafety *iface, REFIID riid, void **ppv)
1229 {
1230     *ppv = NULL;
1231
1232     if(IsEqualGUID(&IID_IActiveScript, riid)) {
1233         CHECK_EXPECT(AXQueryInterface_IActiveScript);
1234         return E_NOINTERFACE;
1235     }
1236
1237     if(IsEqualGUID(&IID_IObjectSafety, riid)) {
1238         CHECK_EXPECT(AXQueryInterface_IObjectSafety);
1239         if(!ax_objsafe)
1240             return E_NOINTERFACE;
1241         *ppv = iface;
1242         return S_OK;
1243     }
1244
1245     ok(0, "unexpected call %s\n", debugstr_guid(riid));
1246     return E_NOINTERFACE;
1247 }
1248
1249 static HRESULT WINAPI AXObjectSafety_GetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1250         DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
1251 {
1252     CHECK_EXPECT(AXGetInterfaceSafetyOptions);
1253
1254     ok(IsEqualGUID(&IID_IDispatchEx, riid), "unexpected riid %s\n", debugstr_guid(riid));
1255     ok(pdwSupportedOptions != NULL, "pdwSupportedOptions == NULL\n");
1256     ok(pdwEnabledOptions != NULL, "pdwEnabledOptions == NULL\n");
1257
1258     *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER;
1259     *pdwEnabledOptions = INTERFACE_USES_DISPEX;
1260
1261     return S_OK;
1262 }
1263
1264 static HRESULT WINAPI AXObjectSafety_SetInterfaceSafetyOptions(IObjectSafety *iface, REFIID riid,
1265         DWORD dwOptionSetMask, DWORD dwEnabledOptions)
1266 {
1267     CHECK_EXPECT(AXSetInterfaceSafetyOptions);
1268
1269     ok(IsEqualGUID(&IID_IDispatchEx, riid), "unexpected riid %s\n", debugstr_guid(riid));
1270
1271     ok(dwOptionSetMask == (INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACE_USES_SECURITY_MANAGER),
1272        "dwOptionSetMask=%x\n", dwOptionSetMask);
1273     ok(dwEnabledOptions == (INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACE_USES_SECURITY_MANAGER),
1274        "dwEnabledOptions=%x\n", dwOptionSetMask);
1275
1276     return S_OK;
1277 }
1278
1279 static const IObjectSafetyVtbl AXObjectSafetyVtbl = {
1280     AXObjectSafety_QueryInterface,
1281     ObjectSafety_AddRef,
1282     ObjectSafety_Release,
1283     AXObjectSafety_GetInterfaceSafetyOptions,
1284     AXObjectSafety_SetInterfaceSafetyOptions
1285 };
1286
1287 static IObjectSafety AXObjectSafety = { &AXObjectSafetyVtbl };
1288
1289 static BOOL set_safe_reg(BOOL init)
1290 {
1291     return init_key("CLSID\\"TESTACTIVEX_CLSID"\\Implemented Categories\\{7dd95801-9882-11cf-9fa9-00aa006c42c4}",
1292                     NULL, init);
1293 }
1294
1295 static void test_security(void)
1296 {
1297     IInternetHostSecurityManager *sec_mgr;
1298     IServiceProvider *sp;
1299     DWORD policy, policy_size;
1300     struct CONFIRMSAFETY cs;
1301     BYTE *ppolicy;
1302     HRESULT hres;
1303
1304     hres = IActiveScriptSite_QueryInterface(site, &IID_IServiceProvider, (void**)&sp);
1305     ok(hres == S_OK, "Could not get IServiceProvider iface: %08x\n", hres);
1306
1307     hres = IServiceProvider_QueryService(sp, &SID_SInternetHostSecurityManager,
1308             &IID_IInternetHostSecurityManager, (void**)&sec_mgr);
1309     IServiceProvider_Release(sp);
1310     ok(hres == S_OK, "QueryService failed: %08x\n", hres);
1311
1312     hres = IInternetHostSecurityManager_ProcessUrlAction(sec_mgr, URLACTION_ACTIVEX_RUN, (BYTE*)&policy, sizeof(policy),
1313                                                          (BYTE*)&CLSID_TestActiveX, sizeof(CLSID), 0, 0);
1314     ok(hres == S_OK, "ProcessUrlAction failed: %08x\n", hres);
1315     ok(policy == URLPOLICY_ALLOW, "policy = %x\n", policy);
1316
1317     cs.clsid = CLSID_TestActiveX;
1318     cs.pUnk = (IUnknown*)&AXObjectSafety;
1319     cs.dwFlags = 0;
1320
1321     ax_objsafe = TRUE;
1322     SET_EXPECT(AXQueryInterface_IActiveScript);
1323     SET_EXPECT(AXQueryInterface_IObjectSafety);
1324     SET_EXPECT(AXGetInterfaceSafetyOptions);
1325     SET_EXPECT(AXSetInterfaceSafetyOptions);
1326     hres = IInternetHostSecurityManager_QueryCustomPolicy(sec_mgr, &GUID_CUSTOM_CONFIRMOBJECTSAFETY,
1327             &ppolicy, &policy_size, (BYTE*)&cs, sizeof(cs), 0);
1328     CHECK_CALLED(AXQueryInterface_IActiveScript);
1329     CHECK_CALLED(AXQueryInterface_IObjectSafety);
1330     CHECK_CALLED(AXGetInterfaceSafetyOptions);
1331     CHECK_CALLED(AXSetInterfaceSafetyOptions);
1332
1333     ok(hres == S_OK, "QueryCusromPolicy failed: %08x\n", hres);
1334     ok(policy_size == sizeof(DWORD), "policy_size = %d\n", policy_size);
1335     ok(*(DWORD*)ppolicy == URLPOLICY_ALLOW, "policy = %x\n", *(DWORD*)ppolicy);
1336     CoTaskMemFree(ppolicy);
1337
1338     ax_objsafe = FALSE;
1339     SET_EXPECT(AXQueryInterface_IActiveScript);
1340     SET_EXPECT(AXQueryInterface_IObjectSafety);
1341     hres = IInternetHostSecurityManager_QueryCustomPolicy(sec_mgr, &GUID_CUSTOM_CONFIRMOBJECTSAFETY,
1342             &ppolicy, &policy_size, (BYTE*)&cs, sizeof(cs), 0);
1343     CHECK_CALLED(AXQueryInterface_IActiveScript);
1344     CHECK_CALLED(AXQueryInterface_IObjectSafety);
1345
1346     ok(hres == S_OK, "QueryCusromPolicy failed: %08x\n", hres);
1347     ok(policy_size == sizeof(DWORD), "policy_size = %d\n", policy_size);
1348     ok(*(DWORD*)ppolicy == URLPOLICY_DISALLOW, "policy = %x\n", *(DWORD*)ppolicy);
1349     CoTaskMemFree(ppolicy);
1350
1351     if(set_safe_reg(TRUE)) {
1352         ax_objsafe = FALSE;
1353         SET_EXPECT(AXQueryInterface_IActiveScript);
1354         SET_EXPECT(AXQueryInterface_IObjectSafety);
1355         hres = IInternetHostSecurityManager_QueryCustomPolicy(sec_mgr, &GUID_CUSTOM_CONFIRMOBJECTSAFETY,
1356                  &ppolicy, &policy_size, (BYTE*)&cs, sizeof(cs), 0);
1357         CHECK_CALLED(AXQueryInterface_IActiveScript);
1358         CHECK_CALLED(AXQueryInterface_IObjectSafety);
1359
1360         ok(hres == S_OK, "QueryCusromPolicy failed: %08x\n", hres);
1361         ok(policy_size == sizeof(DWORD), "policy_size = %d\n", policy_size);
1362         ok(*(DWORD*)ppolicy == URLPOLICY_ALLOW, "policy = %x\n", *(DWORD*)ppolicy);
1363         CoTaskMemFree(ppolicy);
1364
1365         ax_objsafe = TRUE;
1366         SET_EXPECT(AXQueryInterface_IActiveScript);
1367         SET_EXPECT(AXQueryInterface_IObjectSafety);
1368         SET_EXPECT(AXGetInterfaceSafetyOptions);
1369         SET_EXPECT(AXSetInterfaceSafetyOptions);
1370         hres = IInternetHostSecurityManager_QueryCustomPolicy(sec_mgr, &GUID_CUSTOM_CONFIRMOBJECTSAFETY,
1371                 &ppolicy, &policy_size, (BYTE*)&cs, sizeof(cs), 0);
1372         CHECK_CALLED(AXQueryInterface_IActiveScript);
1373         CHECK_CALLED(AXQueryInterface_IObjectSafety);
1374         CHECK_CALLED(AXGetInterfaceSafetyOptions);
1375         CHECK_CALLED(AXSetInterfaceSafetyOptions);
1376
1377         ok(hres == S_OK, "QueryCusromPolicy failed: %08x\n", hres);
1378         ok(policy_size == sizeof(DWORD), "policy_size = %d\n", policy_size);
1379         ok(*(DWORD*)ppolicy == URLPOLICY_ALLOW, "policy = %x\n", *(DWORD*)ppolicy);
1380         CoTaskMemFree(ppolicy);
1381
1382         set_safe_reg(FALSE);
1383     }else {
1384         skip("Could not set safety registry\n");
1385     }
1386
1387     IInternetHostSecurityManager_Release(sec_mgr);
1388 }
1389
1390 static HRESULT WINAPI ActiveScriptProperty_QueryInterface(IActiveScriptProperty *iface, REFIID riid, void **ppv)
1391 {
1392     *ppv = NULL;
1393     ok(0, "unexpected call\n");
1394     return E_NOINTERFACE;
1395 }
1396
1397 static ULONG WINAPI ActiveScriptProperty_AddRef(IActiveScriptProperty *iface)
1398 {
1399     return 2;
1400 }
1401
1402 static ULONG WINAPI ActiveScriptProperty_Release(IActiveScriptProperty *iface)
1403 {
1404     return 1;
1405 }
1406
1407 static HRESULT WINAPI ActiveScriptProperty_GetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
1408         VARIANT *pvarIndex, VARIANT *pvarValue)
1409 {
1410     ok(0, "unexpected call\n");
1411     return E_NOTIMPL;
1412 }
1413
1414 static HRESULT WINAPI ActiveScriptProperty_SetProperty(IActiveScriptProperty *iface, DWORD dwProperty,
1415         VARIANT *pvarIndex, VARIANT *pvarValue)
1416 {
1417     switch(dwProperty) {
1418     case SCRIPTPROP_HACK_TRIDENTEVENTSINK:
1419         CHECK_EXPECT(SetProperty_HACK_TRIDENTEVENTSINK);
1420         ok(V_VT(pvarValue) == VT_BOOL, "V_VT(pvarValue)=%d\n", V_VT(pvarValue));
1421         ok(V_BOOL(pvarValue) == VARIANT_TRUE, "V_BOOL(pvarValue)=%x\n", V_BOOL(pvarValue));
1422         break;
1423     case SCRIPTPROP_INVOKEVERSIONING:
1424         CHECK_EXPECT(SetProperty_INVOKEVERSIONING);
1425         ok(V_VT(pvarValue) == VT_I4, "V_VT(pvarValue)=%d\n", V_VT(pvarValue));
1426         ok(V_I4(pvarValue) == 1, "V_I4(pvarValue)=%d\n", V_I4(pvarValue));
1427         break;
1428     case SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION:
1429         CHECK_EXPECT(SetProperty_ABBREVIATE_GLOBALNAME_RESOLUTION);
1430         ok(V_VT(pvarValue) == VT_BOOL, "V_VT(pvarValue)=%d\n", V_VT(pvarValue));
1431         ok(V_BOOL(pvarValue) == VARIANT_TRUE, "V_BOOL(pvarValue)=%x\n", V_BOOL(pvarValue));
1432         break;
1433     default:
1434         ok(0, "unexpected property %x\n", dwProperty);
1435         return E_NOTIMPL;
1436     }
1437
1438     ok(!pvarIndex, "pvarIndex != NULL\n");
1439     ok(pvarValue != NULL, "pvarValue == NULL\n");
1440
1441     return S_OK;
1442 }
1443
1444 static const IActiveScriptPropertyVtbl ActiveScriptPropertyVtbl = {
1445     ActiveScriptProperty_QueryInterface,
1446     ActiveScriptProperty_AddRef,
1447     ActiveScriptProperty_Release,
1448     ActiveScriptProperty_GetProperty,
1449     ActiveScriptProperty_SetProperty
1450 };
1451
1452 static IActiveScriptProperty ActiveScriptProperty = { &ActiveScriptPropertyVtbl };
1453
1454 static HRESULT WINAPI ActiveScriptParseProcedure_QueryInterface(IActiveScriptParseProcedure2 *iface, REFIID riid, void **ppv)
1455 {
1456     *ppv = NULL;
1457     ok(0, "unexpected call\n");
1458     return E_NOINTERFACE;
1459 }
1460
1461 static ULONG WINAPI ActiveScriptParseProcedure_AddRef(IActiveScriptParseProcedure2 *iface)
1462 {
1463     return 2;
1464 }
1465
1466 static ULONG WINAPI ActiveScriptParseProcedure_Release(IActiveScriptParseProcedure2 *iface)
1467 {
1468     return 1;
1469 }
1470
1471 static HRESULT WINAPI ActiveScriptParseProcedure_ParseProcedureText(IActiveScriptParseProcedure2 *iface,
1472         LPCOLESTR pstrCode, LPCOLESTR pstrFormalParams, LPCOLESTR pstrProcedureName,
1473         LPCOLESTR pstrItemName, IUnknown *punkContext, LPCOLESTR pstrDelimiter,
1474         CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags, IDispatch **ppdisp)
1475 {
1476     ok(0, "unexpected call\n");
1477     return E_NOTIMPL;
1478 }
1479
1480 static const IActiveScriptParseProcedure2Vtbl ActiveScriptParseProcedureVtbl = {
1481     ActiveScriptParseProcedure_QueryInterface,
1482     ActiveScriptParseProcedure_AddRef,
1483     ActiveScriptParseProcedure_Release,
1484     ActiveScriptParseProcedure_ParseProcedureText
1485 };
1486
1487 static IActiveScriptParseProcedure2 ActiveScriptParseProcedure = { &ActiveScriptParseProcedureVtbl };
1488
1489 static HRESULT WINAPI ActiveScriptParse_QueryInterface(IActiveScriptParse *iface, REFIID riid, void **ppv)
1490 {
1491     *ppv = NULL;
1492     ok(0, "unexpected call\n");
1493     return E_NOINTERFACE;
1494 }
1495
1496 static ULONG WINAPI ActiveScriptParse_AddRef(IActiveScriptParse *iface)
1497 {
1498     return 2;
1499 }
1500
1501 static ULONG WINAPI ActiveScriptParse_Release(IActiveScriptParse *iface)
1502 {
1503     return 1;
1504 }
1505
1506 static HRESULT WINAPI ActiveScriptParse_InitNew(IActiveScriptParse *iface)
1507 {
1508     CHECK_EXPECT(InitNew);
1509     return S_OK;
1510 }
1511
1512 static HRESULT WINAPI ActiveScriptParse_AddScriptlet(IActiveScriptParse *iface,
1513         LPCOLESTR pstrDefaultName, LPCOLESTR pstrCode, LPCOLESTR pstrItemName,
1514         LPCOLESTR pstrSubItemName, LPCOLESTR pstrEventName, LPCOLESTR pstrDelimiter,
1515         CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags,
1516         BSTR *pbstrName, EXCEPINFO *pexcepinfo)
1517 {
1518     ok(0, "unexpected call\n");
1519     return E_NOTIMPL;
1520 }
1521
1522 static HRESULT dispex_propput(IDispatchEx *obj, DISPID id, DWORD flags, VARIANT *var)
1523 {
1524     DISPID propput_arg = DISPID_PROPERTYPUT;
1525     DISPPARAMS dp = {var, &propput_arg, 1, 1};
1526     EXCEPINFO ei = {0};
1527
1528     return IDispatchEx_InvokeEx(obj, id, LOCALE_NEUTRAL, DISPATCH_PROPERTYPUT|flags, &dp, NULL, &ei, NULL);
1529 }
1530
1531 static void test_func(IDispatchEx *obj)
1532 {
1533     DISPID id;
1534     IDispatchEx *dispex;
1535     IDispatch *disp;
1536     EXCEPINFO ei;
1537     DISPPARAMS dp;
1538     BSTR str;
1539     VARIANT var;
1540     HRESULT hres;
1541
1542     str = a2bstr("toString");
1543     hres = IDispatchEx_GetDispID(obj, str, fdexNameCaseSensitive, &id);
1544     SysFreeString(str);
1545     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
1546     ok(id == DISPID_IOMNAVIGATOR_TOSTRING, "id = %x\n", id);
1547
1548     memset(&dp, 0, sizeof(dp));
1549     memset(&ei, 0, sizeof(ei));
1550     VariantInit(&var);
1551     hres = IDispatchEx_InvokeEx(obj, id, LOCALE_NEUTRAL, DISPATCH_PROPERTYGET, &dp, &var, &ei, NULL);
1552     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
1553     ok(V_VT(&var) == VT_DISPATCH, "V_VT(var)=%d\n", V_VT(&var));
1554     ok(V_DISPATCH(&var) != NULL, "V_DISPATCH(var) == NULL\n");
1555     disp = V_DISPATCH(&var);
1556
1557     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1558     IDispatch_Release(disp);
1559     ok(hres == S_OK, "Could not get IDispatchEx iface: %08x\n", hres);
1560
1561     /* FIXME: Test InvokeEx(DISPATCH_METHOD) */
1562
1563     memset(&dp, 0, sizeof(dp));
1564     memset(&ei, 0, sizeof(ei));
1565     VariantInit(&var);
1566     hres = IDispatchEx_Invoke(dispex, DISPID_VALUE, &IID_NULL, LOCALE_NEUTRAL, DISPATCH_METHOD, &dp, &var, &ei, NULL);
1567     ok(hres == S_OK || broken(E_ACCESSDENIED), "InvokeEx failed: %08x\n", hres);
1568     if(SUCCEEDED(hres)) {
1569         ok(V_VT(&var) == VT_BSTR, "V_VT(var)=%d\n", V_VT(&var));
1570         ok(!strcmp_wa(V_BSTR(&var), "[object]"), "V_BSTR(var) = %s\n", wine_dbgstr_w(V_BSTR(&var)));
1571         VariantClear(&var);
1572     }
1573
1574     V_VT(&var) = VT_I4;
1575     V_I4(&var) = 100;
1576     hres = dispex_propput(obj, id, 0, &var);
1577     ok(hres == E_NOTIMPL, "InvokeEx failed: %08x\n", hres);
1578
1579     IDispatchEx_Release(dispex);
1580 }
1581
1582 static void test_nextdispid(IDispatchEx *dispex)
1583 {
1584     DISPID last_id = DISPID_STARTENUM, id, dyn_id;
1585     BSTR name;
1586     VARIANT var;
1587     HRESULT hres;
1588
1589     name = a2bstr("dynVal");
1590     hres = IDispatchEx_GetDispID(dispex, name, fdexNameCaseSensitive|fdexNameEnsure, &dyn_id);
1591     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
1592     SysFreeString(name);
1593
1594     V_VT(&var) = VT_EMPTY;
1595     hres = dispex_propput(dispex, dyn_id, 0, &var);
1596
1597     while(last_id != dyn_id) {
1598         hres = IDispatchEx_GetNextDispID(dispex, fdexEnumAll, last_id, &id);
1599         ok(hres == S_OK, "GetNextDispID returned: %08x\n", hres);
1600         ok(id != DISPID_STARTENUM, "id == DISPID_STARTENUM\n");
1601         ok(id != DISPID_IOMNAVIGATOR_TOSTRING, "id == DISPID_IOMNAVIGATOR_TOSTRING\n");
1602
1603         hres = IDispatchEx_GetMemberName(dispex, id, &name);
1604         ok(hres == S_OK, "GetMemberName failed: %08x\n", hres);
1605
1606         if(id == dyn_id)
1607             ok(!strcmp_wa(name, "dynVal"), "name = %s\n", wine_dbgstr_w(name));
1608         else if(id == DISPID_IOMNAVIGATOR_PLATFORM)
1609             ok(!strcmp_wa(name, "platform"), "name = %s\n", wine_dbgstr_w(name));
1610
1611         SysFreeString(name);
1612         last_id = id;
1613     }
1614
1615     hres = IDispatchEx_GetNextDispID(dispex, 0, id, &id);
1616     ok(hres == S_FALSE, "GetNextDispID returned: %08x\n", hres);
1617     ok(id == DISPID_STARTENUM, "id != DISPID_STARTENUM\n");
1618 }
1619
1620 static void test_global_id(void)
1621 {
1622     VARIANT var;
1623     DISPPARAMS dp;
1624     EXCEPINFO ei;
1625     BSTR tmp;
1626     DISPID id;
1627     HRESULT hres;
1628
1629     SET_EXPECT(GetScriptDispatch);
1630     SET_EXPECT(script_divid_d);
1631     tmp = a2bstr("divid");
1632     hres = IDispatchEx_GetDispID(window_dispex, tmp, fdexNameCaseSensitive, &id);
1633     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
1634     SysFreeString(tmp);
1635     CHECK_CALLED(GetScriptDispatch);
1636     CHECK_CALLED(script_divid_d);
1637
1638     VariantInit(&var);
1639     memset(&ei, 0, sizeof(ei));
1640     memset(&dp, 0, sizeof(dp));
1641     hres = IDispatchEx_InvokeEx(window_dispex, id, 0, DISPATCH_PROPERTYGET, &dp, &var, &ei, NULL);
1642     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
1643     ok(V_VT(&var) == VT_DISPATCH, "V_VT(var) = %d\n", V_VT(&var));
1644     VariantClear(&var);
1645 }
1646
1647 static HRESULT WINAPI ActiveScriptParse_ParseScriptText(IActiveScriptParse *iface,
1648         LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext,
1649         LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLine,
1650         DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo)
1651 {
1652     IDispatchEx *document, *dispex;
1653     IHTMLWindow2 *window;
1654     IOmNavigator *navigator;
1655     IUnknown *unk;
1656     VARIANT var, arg;
1657     DISPPARAMS dp;
1658     EXCEPINFO ei;
1659     DISPID id;
1660     BSTR tmp;
1661     HRESULT hres;
1662
1663     static const WCHAR documentW[] = {'d','o','c','u','m','e','n','t',0};
1664     static const WCHAR testW[] = {'t','e','s','t',0};
1665     static const WCHAR funcW[] = {'f','u','n','c',0};
1666
1667     CHECK_EXPECT(ParseScriptText);
1668
1669     SET_EXPECT(GetScriptDispatch);
1670
1671     tmp = SysAllocString(documentW);
1672     hres = IDispatchEx_GetDispID(window_dispex, tmp, fdexNameCaseSensitive, &id);
1673     SysFreeString(tmp);
1674     ok(hres == S_OK, "GetDispID(document) failed: %08x\n", hres);
1675     ok(id == DISPID_IHTMLWINDOW2_DOCUMENT, "id=%x\n", id);
1676
1677     CHECK_CALLED(GetScriptDispatch);
1678
1679     VariantInit(&var);
1680     memset(&dp, 0, sizeof(dp));
1681     memset(&ei, 0, sizeof(ei));
1682
1683     hres = IDispatchEx_InvokeEx(window_dispex, id, LOCALE_NEUTRAL, INVOKE_PROPERTYGET, &dp, &var, &ei, NULL);
1684     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
1685     ok(V_VT(&var) == VT_DISPATCH, "V_VT(var)=%d\n", V_VT(&var));
1686     ok(V_DISPATCH(&var) != NULL, "V_DISPATCH(&var) == NULL\n");
1687
1688     hres = IDispatch_QueryInterface(V_DISPATCH(&var), &IID_IDispatchEx, (void**)&document);
1689     VariantClear(&var);
1690     ok(hres == S_OK, "Could not get DispatchEx: %08x\n", hres);
1691
1692     tmp = SysAllocString(testW);
1693     hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive, &id);
1694     ok(hres == DISP_E_UNKNOWNNAME, "GetDispID(document) failed: %08x, expected DISP_E_UNKNOWNNAME\n", hres);
1695     hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive | fdexNameImplicit, &id);
1696     ok(hres == DISP_E_UNKNOWNNAME, "GetDispID(document) failed: %08x, expected DISP_E_UNKNOWNNAME\n", hres);
1697     SysFreeString(tmp);
1698
1699     id = 0;
1700     tmp = SysAllocString(testW);
1701     hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive|fdexNameEnsure, &id);
1702     SysFreeString(tmp);
1703     ok(hres == S_OK, "GetDispID(document) failed: %08x\n", hres);
1704     ok(id, "id == 0\n");
1705
1706     V_VT(&var) = VT_I4;
1707     V_I4(&var) = 100;
1708     hres = dispex_propput(document, id, 0, &var);
1709     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
1710
1711     tmp = SysAllocString(testW);
1712     hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive, &id);
1713     SysFreeString(tmp);
1714     ok(hres == S_OK, "GetDispID(document) failed: %08x\n", hres);
1715
1716     VariantInit(&var);
1717     memset(&dp, 0, sizeof(dp));
1718     memset(&ei, 0, sizeof(ei));
1719     hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_PROPERTYGET, &dp, &var, &ei, NULL);
1720     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
1721     ok(V_VT(&var) == VT_I4, "V_VT(var)=%d\n", V_VT(&var));
1722     ok(V_I4(&var) == 100, "V_I4(&var) = %d\n", V_I4(&var));
1723
1724     V_VT(&var) = VT_I4;
1725     V_I4(&var) = 200;
1726     hres = dispex_propput(document, id, DISPATCH_PROPERTYPUTREF, &var);
1727     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
1728
1729     VariantInit(&var);
1730     memset(&dp, 0, sizeof(dp));
1731     memset(&ei, 0, sizeof(ei));
1732     hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_PROPERTYGET, &dp, &var, &ei, NULL);
1733     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
1734     ok(V_VT(&var) == VT_I4, "V_VT(var)=%d\n", V_VT(&var));
1735     ok(V_I4(&var) == 200, "V_I4(&var) = %d\n", V_I4(&var));
1736
1737     memset(&dp, 0, sizeof(dp));
1738     memset(&ei, 0, sizeof(ei));
1739     V_VT(&var) = VT_I4;
1740     V_I4(&var) = 300;
1741     dp.cArgs = 1;
1742     dp.rgvarg = &var;
1743     hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_PROPERTYPUT, &dp, NULL, &ei, NULL);
1744     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
1745
1746     VariantInit(&var);
1747     memset(&dp, 0, sizeof(dp));
1748     memset(&ei, 0, sizeof(ei));
1749     hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_PROPERTYGET, &dp, &var, &ei, NULL);
1750     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
1751     ok(V_VT(&var) == VT_I4, "V_VT(var)=%d\n", V_VT(&var));
1752     ok(V_I4(&var) == 300, "V_I4(&var) = %d\n", V_I4(&var));
1753
1754     unk = (void*)0xdeadbeef;
1755     hres = IDispatchEx_GetNameSpaceParent(window_dispex, &unk);
1756     ok(hres == S_OK, "GetNameSpaceParent failed: %08x\n", hres);
1757     ok(!unk, "unk=%p, expected NULL\n", unk);
1758
1759     id = 0;
1760     tmp = SysAllocString(funcW);
1761     hres = IDispatchEx_GetDispID(document, tmp, fdexNameCaseSensitive|fdexNameEnsure, &id);
1762     SysFreeString(tmp);
1763     ok(hres == S_OK, "GetDispID(func) failed: %08x\n", hres);
1764     ok(id, "id == 0\n");
1765
1766     dp.cArgs = 1;
1767     dp.rgvarg = &var;
1768     dp.cNamedArgs = 0;
1769     dp.rgdispidNamedArgs = NULL;
1770     V_VT(&var) = VT_DISPATCH;
1771     V_DISPATCH(&var) = (IDispatch*)&funcDisp;
1772     hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_PROPERTYPUT, &dp, NULL, &ei, NULL);
1773     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
1774
1775     VariantInit(&var);
1776     memset(&dp, 0, sizeof(dp));
1777     memset(&ei, 0, sizeof(ei));
1778     V_VT(&arg) = VT_BOOL;
1779     V_BOOL(&arg) = VARIANT_TRUE;
1780     dp.cArgs = 1;
1781     dp.rgvarg = &arg;
1782
1783     SET_EXPECT(funcDisp);
1784     hres = IDispatchEx_InvokeEx(document, id, LOCALE_NEUTRAL, INVOKE_FUNC, &dp, &var, &ei, NULL);
1785     CHECK_CALLED(funcDisp);
1786
1787     ok(hres == S_OK, "InvokeEx(INVOKE_FUNC) failed: %08x\n", hres);
1788     ok(V_VT(&var) == VT_I4, "V_VT(var)=%d\n", V_VT(&var));
1789     ok(V_I4(&var) == 100, "V_I4(&var) == NULL\n");
1790
1791     IDispatchEx_Release(document);
1792
1793     hres = IDispatchEx_QueryInterface(window_dispex, &IID_IHTMLWindow2, (void**)&window);
1794     ok(hres == S_OK, "Could not get IHTMLWindow2 iface: %08x\n", hres);
1795
1796     hres = IHTMLWindow2_get_navigator(window, &navigator);
1797     IHTMLWindow2_Release(window);
1798     ok(hres == S_OK, "get_navigator failed: %08x\n", hres);
1799
1800     hres = IOmNavigator_QueryInterface(navigator, &IID_IDispatchEx, (void**)&dispex);
1801     IOmNavigator_Release(navigator);
1802     ok(hres == S_OK, "Could not get IDispatchEx iface: %08x\n", hres);
1803
1804     test_func(dispex);
1805     test_nextdispid(dispex);
1806     IDispatchEx_Release(dispex);
1807
1808     script_disp = (IDispatch*)&scriptDisp;
1809
1810     SET_EXPECT(GetScriptDispatch);
1811     SET_EXPECT(script_testprop_d);
1812     tmp = a2bstr("testProp");
1813     hres = IDispatchEx_GetDispID(window_dispex, tmp, fdexNameCaseSensitive, &id);
1814     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
1815     ok(id != DISPID_SCRIPT_TESTPROP, "id == DISPID_SCRIPT_TESTPROP\n");
1816     CHECK_CALLED(GetScriptDispatch);
1817     CHECK_CALLED(script_testprop_d);
1818     SysFreeString(tmp);
1819
1820     tmp = a2bstr("testProp");
1821     hres = IDispatchEx_GetDispID(window_dispex, tmp, fdexNameCaseSensitive, &id);
1822     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
1823     ok(id != DISPID_SCRIPT_TESTPROP, "id == DISPID_SCRIPT_TESTPROP\n");
1824     SysFreeString(tmp);
1825
1826     SET_EXPECT(GetScriptDispatch);
1827     SET_EXPECT(script_testprop_i);
1828     memset(&ei, 0, sizeof(ei));
1829     memset(&dp, 0, sizeof(dp));
1830     hres = IDispatchEx_InvokeEx(window_dispex, id, 0, DISPATCH_PROPERTYGET, &dp, &var, &ei, NULL);
1831     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
1832     ok(V_VT(&var) == VT_NULL, "V_VT(var) = %d\n", V_VT(&var));
1833     CHECK_CALLED(GetScriptDispatch);
1834     CHECK_CALLED(script_testprop_i);
1835
1836     SET_EXPECT(GetScriptDispatch);
1837     SET_EXPECT(script_testprop2_d);
1838     tmp = a2bstr("testProp2");
1839     hres = IDispatchEx_GetDispID(window_dispex, tmp, fdexNameCaseSensitive|fdexNameEnsure, &id);
1840     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
1841     ok(id != DISPID_SCRIPT_TESTPROP2, "id == DISPID_SCRIPT_TESTPROP2\n");
1842     CHECK_CALLED(GetScriptDispatch);
1843     CHECK_CALLED(script_testprop2_d);
1844     SysFreeString(tmp);
1845
1846     test_global_id();
1847
1848     test_security();
1849
1850     return S_OK;
1851 }
1852
1853 static const IActiveScriptParseVtbl ActiveScriptParseVtbl = {
1854     ActiveScriptParse_QueryInterface,
1855     ActiveScriptParse_AddRef,
1856     ActiveScriptParse_Release,
1857     ActiveScriptParse_InitNew,
1858     ActiveScriptParse_AddScriptlet,
1859     ActiveScriptParse_ParseScriptText
1860 };
1861
1862 static IActiveScriptParse ActiveScriptParse = { &ActiveScriptParseVtbl };
1863
1864 static HRESULT WINAPI ActiveScript_QueryInterface(IActiveScript *iface, REFIID riid, void **ppv)
1865 {
1866     *ppv = NULL;
1867
1868     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IActiveScript, riid)) {
1869         *ppv = iface;
1870         return S_OK;
1871     }
1872
1873     if(IsEqualGUID(&IID_IActiveScriptParse, riid)) {
1874         *ppv = &ActiveScriptParse;
1875         return S_OK;
1876     }
1877
1878     if(IsEqualGUID(&IID_IActiveScriptParseProcedure2, riid)) {
1879         *ppv = &ActiveScriptParseProcedure;
1880         return S_OK;
1881     }
1882
1883     if(IsEqualGUID(&IID_IActiveScriptProperty, riid)) {
1884         *ppv = &ActiveScriptProperty;
1885         return S_OK;
1886     }
1887
1888     if(IsEqualGUID(&IID_IObjectSafety, riid)) {
1889         *ppv = &ObjectSafety;
1890         return S_OK;
1891     }
1892
1893     if(IsEqualGUID(&IID_IActiveScriptDebug, riid))
1894         return E_NOINTERFACE;
1895
1896     ok(0, "unexpected riid %s\n", debugstr_guid(riid));
1897     return E_NOINTERFACE;
1898 }
1899
1900 static ULONG WINAPI ActiveScript_AddRef(IActiveScript *iface)
1901 {
1902     return 2;
1903 }
1904
1905 static ULONG WINAPI ActiveScript_Release(IActiveScript *iface)
1906 {
1907     return 1;
1908 }
1909
1910 static HRESULT WINAPI ActiveScript_SetScriptSite(IActiveScript *iface, IActiveScriptSite *pass)
1911 {
1912     IActiveScriptSiteInterruptPoll *poll;
1913     IActiveScriptSiteDebug *debug;
1914     IServiceProvider *service;
1915     ICanHandleException *canexpection;
1916     LCID lcid;
1917     HRESULT hres;
1918
1919     CHECK_EXPECT(SetScriptSite);
1920
1921     ok(pass != NULL, "pass == NULL\n");
1922
1923     hres = IActiveScriptSite_QueryInterface(pass, &IID_IActiveScriptSiteInterruptPoll, (void**)&poll);
1924     ok(hres == S_OK, "Could not get IActiveScriptSiteInterruptPoll interface: %08x\n", hres);
1925     if(FAILED(hres))
1926         IActiveScriptSiteInterruptPoll_Release(poll);
1927
1928     hres = IActiveScriptSite_GetLCID(pass, &lcid);
1929     ok(hres == S_OK, "GetLCID failed: %08x\n", hres);
1930
1931     hres = IActiveScriptSite_OnStateChange(pass, (state = SCRIPTSTATE_INITIALIZED));
1932     ok(hres == S_OK, "OnStateChange failed: %08x\n", hres);
1933
1934     hres = IActiveScriptSite_QueryInterface(pass, &IID_IActiveScriptSiteDebug, (void**)&debug);
1935     ok(hres == S_OK, "Could not get IActiveScriptSiteDebug interface: %08x\n", hres);
1936     if(SUCCEEDED(hres))
1937         IActiveScriptSiteDebug32_Release(debug);
1938
1939     hres = IActiveScriptSite_QueryInterface(pass, &IID_ICanHandleException, (void**)&canexpection);
1940     ok(hres == E_NOINTERFACE, "Could not get IID_ICanHandleException interface: %08x\n", hres);
1941
1942     hres = IActiveScriptSite_QueryInterface(pass, &IID_IServiceProvider, (void**)&service);
1943     ok(hres == S_OK, "Could not get IServiceProvider interface: %08x\n", hres);
1944     if(SUCCEEDED(hres))
1945         IServiceProvider_Release(service);
1946
1947     site = pass;
1948     IActiveScriptSite_AddRef(site);
1949     return S_OK;
1950 }
1951
1952 static HRESULT WINAPI ActiveScript_GetScriptSite(IActiveScript *iface, REFIID riid,
1953                                             void **ppvObject)
1954 {
1955     ok(0, "unexpected call\n");
1956     return E_NOTIMPL;
1957 }
1958
1959 static HRESULT WINAPI ActiveScript_SetScriptState(IActiveScript *iface, SCRIPTSTATE ss)
1960 {
1961     HRESULT hres;
1962
1963     switch(ss) {
1964     case SCRIPTSTATE_STARTED:
1965         CHECK_EXPECT(SetScriptState_STARTED);
1966         break;
1967     case SCRIPTSTATE_CONNECTED:
1968         CHECK_EXPECT(SetScriptState_CONNECTED);
1969         break;
1970     case SCRIPTSTATE_DISCONNECTED:
1971         CHECK_EXPECT(SetScriptState_DISCONNECTED);
1972         break;
1973     default:
1974         ok(0, "unexpected state %d\n", ss);
1975         return E_NOTIMPL;
1976     }
1977
1978     hres = IActiveScriptSite_OnStateChange(site, (state = ss));
1979     return S_OK;
1980 }
1981
1982 static HRESULT WINAPI ActiveScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE *pssState)
1983 {
1984     CHECK_EXPECT(GetScriptState);
1985
1986     *pssState = state;
1987     return S_OK;
1988 }
1989
1990 static HRESULT WINAPI ActiveScript_Close(IActiveScript *iface)
1991 {
1992     CHECK_EXPECT(Close);
1993     return E_NOTIMPL;
1994 }
1995
1996 static HRESULT WINAPI ActiveScript_AddNamedItem(IActiveScript *iface,
1997         LPCOLESTR pstrName, DWORD dwFlags)
1998 {
1999     IDispatch *disp;
2000     IUnknown *unk = NULL, *unk2;
2001     HRESULT hres;
2002
2003     static const WCHAR windowW[] = {'w','i','n','d','o','w',0};
2004
2005     static const IID unknown_iid = {0x719C3050,0xF9D3,0x11CF,{0xA4,0x93,0x00,0x40,0x05,0x23,0xA8,0xA0}};
2006
2007     CHECK_EXPECT(AddNamedItem);
2008
2009     ok(!lstrcmpW(pstrName, windowW), "pstrName=%s\n", wine_dbgstr_w(pstrName));
2010     ok(dwFlags == (SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS), "dwFlags=%x\n", dwFlags);
2011
2012     hres = IActiveScriptSite_GetItemInfo(site, windowW, SCRIPTINFO_IUNKNOWN, &unk, NULL);
2013     ok(hres == S_OK, "GetItemInfo failed: %08x\n", hres);
2014     ok(unk != NULL, "unk == NULL\n");
2015
2016     hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&disp);
2017     ok(hres == S_OK, "Could not get IDispatch interface: %08x\n", hres);
2018     if(SUCCEEDED(hres))
2019         IDispatch_Release(disp);
2020
2021     hres = IUnknown_QueryInterface(unk, &unknown_iid, (void**)&unk2);
2022     ok(hres == E_NOINTERFACE, "Got ?? interface: %p\n", unk2);
2023     if(SUCCEEDED(hres))
2024         IUnknown_Release(unk2);
2025
2026     hres = IUnknown_QueryInterface(unk, &IID_IDispatchEx, (void**)&window_dispex);
2027     ok(hres == S_OK, "Could not get IDispatchEx interface: %08x\n", hres);
2028
2029     IUnknown_Release(unk);
2030     return S_OK;
2031 }
2032
2033 static HRESULT WINAPI ActiveScript_AddTypeLib(IActiveScript *iface, REFGUID rguidTypeLib,
2034                                          DWORD dwMajor, DWORD dwMinor, DWORD dwFlags)
2035 {
2036     ok(0, "unexpected call\n");
2037     return E_NOTIMPL;
2038 }
2039
2040 static HRESULT WINAPI ActiveScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR pstrItemName,
2041                                                 IDispatch **ppdisp)
2042 {
2043     CHECK_EXPECT(GetScriptDispatch);
2044
2045     ok(!strcmp_wa(pstrItemName, "window"), "pstrItemName = %s\n", wine_dbgstr_w(pstrItemName));
2046
2047     if(!script_disp)
2048         return E_NOTIMPL;
2049
2050     *ppdisp = script_disp;
2051     return S_OK;
2052 }
2053
2054 static HRESULT WINAPI ActiveScript_GetCurrentScriptThreadID(IActiveScript *iface,
2055                                                        SCRIPTTHREADID *pstridThread)
2056 {
2057     ok(0, "unexpected call\n");
2058     return E_NOTIMPL;
2059 }
2060
2061 static HRESULT WINAPI ActiveScript_GetScriptThreadID(IActiveScript *iface,
2062                                                 DWORD dwWin32ThreadId, SCRIPTTHREADID *pstidThread)
2063 {
2064     ok(0, "unexpected call\n");
2065     return E_NOTIMPL;
2066 }
2067
2068 static HRESULT WINAPI ActiveScript_GetScriptThreadState(IActiveScript *iface,
2069         SCRIPTTHREADID stidThread, SCRIPTTHREADSTATE *pstsState)
2070 {
2071     ok(0, "unexpected call\n");
2072     return E_NOTIMPL;
2073 }
2074
2075 static HRESULT WINAPI ActiveScript_InterruptScriptThread(IActiveScript *iface,
2076         SCRIPTTHREADID stidThread, const EXCEPINFO *pexcepinfo, DWORD dwFlags)
2077 {
2078     ok(0, "unexpected call\n");
2079     return E_NOTIMPL;
2080 }
2081
2082 static HRESULT WINAPI ActiveScript_Clone(IActiveScript *iface, IActiveScript **ppscript)
2083 {
2084     ok(0, "unexpected call\n");
2085     return E_NOTIMPL;
2086 }
2087
2088 static const IActiveScriptVtbl ActiveScriptVtbl = {
2089     ActiveScript_QueryInterface,
2090     ActiveScript_AddRef,
2091     ActiveScript_Release,
2092     ActiveScript_SetScriptSite,
2093     ActiveScript_GetScriptSite,
2094     ActiveScript_SetScriptState,
2095     ActiveScript_GetScriptState,
2096     ActiveScript_Close,
2097     ActiveScript_AddNamedItem,
2098     ActiveScript_AddTypeLib,
2099     ActiveScript_GetScriptDispatch,
2100     ActiveScript_GetCurrentScriptThreadID,
2101     ActiveScript_GetScriptThreadID,
2102     ActiveScript_GetScriptThreadState,
2103     ActiveScript_InterruptScriptThread,
2104     ActiveScript_Clone
2105 };
2106
2107 static IActiveScript ActiveScript = { &ActiveScriptVtbl };
2108
2109 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
2110 {
2111     *ppv = NULL;
2112
2113     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid)) {
2114         *ppv = iface;
2115         return S_OK;
2116     }
2117
2118     if(IsEqualGUID(&IID_IMarshal, riid))
2119         return E_NOINTERFACE;
2120     if(IsEqualGUID(&CLSID_IdentityUnmarshal, riid))
2121         return E_NOINTERFACE;
2122
2123     ok(0, "unexpected riid %s\n", debugstr_guid(riid));
2124     return E_NOTIMPL;
2125 }
2126
2127 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
2128 {
2129     return 2;
2130 }
2131
2132 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
2133 {
2134     return 1;
2135 }
2136
2137 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
2138 {
2139     CHECK_EXPECT(CreateInstance);
2140
2141     ok(!outer, "outer = %p\n", outer);
2142     ok(IsEqualGUID(&IID_IActiveScript, riid), "unexpected riid %s\n", debugstr_guid(riid));
2143     *ppv = &ActiveScript;
2144     return S_OK;
2145 }
2146
2147 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
2148 {
2149     ok(0, "unexpected call\n");
2150     return S_OK;
2151 }
2152
2153 static const IClassFactoryVtbl ClassFactoryVtbl = {
2154     ClassFactory_QueryInterface,
2155     ClassFactory_AddRef,
2156     ClassFactory_Release,
2157     ClassFactory_CreateInstance,
2158     ClassFactory_LockServer
2159 };
2160
2161 static IClassFactory script_cf = { &ClassFactoryVtbl };
2162
2163 static const char simple_script_str[] =
2164     "<html><head></head><body>"
2165     "<div id=\"divid\"></div>"
2166     "<script language=\"TestScript\">simple script</script>"
2167     "</body></html>";
2168
2169 static void test_simple_script(void)
2170 {
2171     IHTMLDocument2 *doc;
2172
2173     doc = create_document();
2174     if(!doc)
2175         return;
2176
2177     SET_EXPECT(CreateInstance);
2178     SET_EXPECT(GetInterfaceSafetyOptions);
2179     SET_EXPECT(SetInterfaceSafetyOptions);
2180     SET_EXPECT(SetProperty_INVOKEVERSIONING); /* IE8 */
2181     SET_EXPECT(SetProperty_HACK_TRIDENTEVENTSINK);
2182     SET_EXPECT(InitNew);
2183     SET_EXPECT(SetScriptSite);
2184     SET_EXPECT(GetScriptState);
2185     SET_EXPECT(SetScriptState_STARTED);
2186     SET_EXPECT(AddNamedItem);
2187     SET_EXPECT(SetProperty_ABBREVIATE_GLOBALNAME_RESOLUTION); /* IE8 */
2188     SET_EXPECT(ParseScriptText);
2189     SET_EXPECT(SetScriptState_CONNECTED);
2190
2191     load_doc(doc, simple_script_str);
2192
2193     CHECK_CALLED(CreateInstance);
2194     CHECK_CALLED(GetInterfaceSafetyOptions);
2195     CHECK_CALLED(SetInterfaceSafetyOptions);
2196     CHECK_CALLED_BROKEN(SetProperty_INVOKEVERSIONING); /* IE8 */
2197     CHECK_CALLED(SetProperty_HACK_TRIDENTEVENTSINK);
2198     CHECK_CALLED(InitNew);
2199     CHECK_CALLED(SetScriptSite);
2200     CHECK_CALLED(GetScriptState);
2201     CHECK_CALLED(SetScriptState_STARTED);
2202     CHECK_CALLED(AddNamedItem);
2203     CHECK_CALLED_BROKEN(SetProperty_ABBREVIATE_GLOBALNAME_RESOLUTION); /* IE8 */
2204     CHECK_CALLED(ParseScriptText);
2205     CHECK_CALLED(SetScriptState_CONNECTED);
2206
2207     if(site)
2208         IActiveScriptSite_Release(site);
2209     if(window_dispex)
2210         IDispatchEx_Release(window_dispex);
2211
2212     SET_EXPECT(SetScriptState_DISCONNECTED);
2213     SET_EXPECT(Close);
2214
2215     IHTMLDocument2_Release(doc);
2216
2217     CHECK_CALLED(SetScriptState_DISCONNECTED);
2218     CHECK_CALLED(Close);
2219 }
2220
2221 static void run_js_script(const char *test_name)
2222 {
2223     WCHAR url[INTERNET_MAX_URL_LENGTH];
2224     IPersistMoniker *persist;
2225     IHTMLDocument2 *doc;
2226     IMoniker *mon;
2227     WCHAR *ptr;
2228     MSG msg;
2229     HRESULT hres;
2230
2231     static const WCHAR resW[] = {'r','e','s',':','/','/'};
2232
2233     trace("running %s...\n", test_name);
2234
2235     doc = create_document();
2236     if(!doc)
2237         return;
2238
2239     set_client_site(doc, TRUE);
2240     do_advise(doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
2241
2242     memcpy(url, resW, sizeof(resW));
2243     ptr = url + sizeof(resW)/sizeof(WCHAR);
2244     GetModuleFileNameW(NULL, ptr, sizeof(url)/sizeof(WCHAR) - (ptr-url));
2245     ptr += lstrlenW(ptr);
2246     *ptr++ = '/';
2247     MultiByteToWideChar(CP_ACP, 0, test_name, -1, ptr, sizeof(url)/sizeof(WCHAR) - (ptr-url));
2248
2249     hres = CreateURLMoniker(NULL, url, &mon);
2250     ok(hres == S_OK, "CreateURLMoniker failed: %08x\n", hres);
2251
2252     hres = IHTMLDocument2_QueryInterface(doc, &IID_IPersistMoniker, (void**)&persist);
2253     ok(hres == S_OK, "Could not get IPersistMoniker iface: %08x\n", hres);
2254
2255     hres = IPersistMoniker_Load(persist, FALSE, mon, NULL, 0);
2256     ok(hres == S_OK, "Load failed: %08x\n", hres);
2257
2258     IMoniker_Release(mon);
2259     IPersistMoniker_Release(persist);
2260
2261     SET_EXPECT(external_success);
2262
2263     while(!called_external_success && GetMessage(&msg, NULL, 0, 0)) {
2264         TranslateMessage(&msg);
2265         DispatchMessage(&msg);
2266     }
2267
2268     CHECK_CALLED(external_success);
2269
2270     set_client_site(doc, FALSE);
2271     IHTMLDocument2_Release(doc);
2272 }
2273
2274 static void run_js_tests(void)
2275 {
2276     run_js_script("jstest.html");
2277 }
2278
2279 static BOOL init_registry(BOOL init)
2280 {
2281     return init_key("TestScript\\CLSID", TESTSCRIPT_CLSID, init)
2282         && init_key("CLSID\\"TESTSCRIPT_CLSID"\\Implemented Categories\\{F0B7A1A1-9847-11CF-8F20-00805F2CD064}",
2283                     NULL, init)
2284         && init_key("CLSID\\"TESTSCRIPT_CLSID"\\Implemented Categories\\{F0B7A1A2-9847-11CF-8F20-00805F2CD064}",
2285                     NULL, init);
2286 }
2287
2288 static BOOL register_script_engine(void)
2289 {
2290     DWORD regid;
2291     HRESULT hres;
2292
2293     if(!init_registry(TRUE)) {
2294         init_registry(FALSE);
2295         return FALSE;
2296     }
2297
2298     hres = CoRegisterClassObject(&CLSID_TestScript, (IUnknown *)&script_cf,
2299                                  CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
2300     ok(hres == S_OK, "Could not register screipt engine: %08x\n", hres);
2301
2302     return TRUE;
2303 }
2304
2305 static LRESULT WINAPI wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2306 {
2307     return DefWindowProc(hwnd, msg, wParam, lParam);
2308 }
2309
2310 static HWND create_container_window(void)
2311 {
2312     static const CHAR szHTMLDocumentTest[] = "HTMLDocumentTest";
2313     static WNDCLASSEXA wndclass = {
2314         sizeof(WNDCLASSEXA),
2315         0,
2316         wnd_proc,
2317         0, 0, NULL, NULL, NULL, NULL, NULL,
2318         szHTMLDocumentTest,
2319         NULL
2320     };
2321
2322     RegisterClassExA(&wndclass);
2323     return CreateWindowA(szHTMLDocumentTest, szHTMLDocumentTest,
2324             WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
2325             300, 300, NULL, NULL, NULL, NULL);
2326 }
2327
2328 START_TEST(script)
2329 {
2330     CoInitialize(NULL);
2331     container_hwnd = create_container_window();
2332
2333     if(winetest_interactive || ! is_ie_hardened()) {
2334         if(register_script_engine()) {
2335             test_simple_script();
2336             run_js_tests();
2337             init_registry(FALSE);
2338         }else {
2339             skip("Could not register TestScript engine\n");
2340         }
2341     }else {
2342         skip("IE running in Enhanced Security Configuration\n");
2343     }
2344
2345     DestroyWindow(container_hwnd);
2346     CoUninitialize();
2347 }