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