wintrust: Use helper function for setting confidence in SoftpubCheckCert.
[wine] / dlls / mshtml / tests / dom.c
1 /*
2  * Copyright 2007 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 "mshtml.h"
30 #include "mshtmcid.h"
31 #include "mshtmhst.h"
32 #include "docobj.h"
33 #include "dispex.h"
34
35 static const char doc_blank[] = "<html></html>";
36 static const char doc_str1[] = "<html><body>test</body></html>";
37 static const char range_test_str[] =
38     "<html><body>test \na<font size=\"2\">bc\t123<br /> it's\r\n  \t</font>text<br /></body></html>";
39 static const char range_test2_str[] =
40     "<html><body>abc<hr />123<br /><hr />def</body></html>";
41 static const char elem_test_str[] =
42     "<html><head><title>test</title><style>.body { margin-right: 0px; }</style>"
43     "<body>text test<!-- a comment -->"
44     "<a href=\"http://test\" name=\"x\">link</a>"
45     "<input id=\"in\" class=\"testclass\" tabIndex=\"2\" title=\"test title\" />"
46     "<select id=\"s\"><option id=\"x\" value=\"val1\">opt1</option><option id=\"y\">opt2</option></select>"
47     "<textarea id=\"X\">text text</textarea>"
48     "<table><tbody></tbody></table>"
49     "<script id=\"sc\" type=\"text/javascript\"></script>"
50     "<test />"
51     "<img id=\"imgid\"/>"
52     "</body></html>";
53 static const char indent_test_str[] =
54     "<html><head><title>test</title></head><body>abc<br /><a href=\"about:blank\">123</a></body></html>";
55
56 static const WCHAR noneW[] = {'N','o','n','e',0};
57
58 static WCHAR characterW[] = {'c','h','a','r','a','c','t','e','r',0};
59 static WCHAR texteditW[] = {'t','e','x','t','e','d','i','t',0};
60 static WCHAR wordW[] = {'w','o','r','d',0};
61
62 static const WCHAR text_javascriptW[] = {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
63
64 static const WCHAR idW[] = {'i','d',0};
65
66 typedef enum {
67     ET_NONE,
68     ET_HTML,
69     ET_HEAD,
70     ET_TITLE,
71     ET_BODY,
72     ET_A,
73     ET_INPUT,
74     ET_SELECT,
75     ET_TEXTAREA,
76     ET_OPTION,
77     ET_STYLE,
78     ET_BLOCKQUOTE,
79     ET_P,
80     ET_BR,
81     ET_TABLE,
82     ET_TBODY,
83     ET_SCRIPT,
84     ET_TEST,
85     ET_TESTG,
86     ET_COMMENT,
87     ET_IMG
88 } elem_type_t;
89
90 static const IID * const none_iids[] = {
91     &IID_IUnknown,
92     NULL
93 };
94
95 static const IID * const elem_iids[] = {
96     &IID_IHTMLDOMNode,
97     &IID_IHTMLDOMNode2,
98     &IID_IHTMLElement,
99     &IID_IHTMLElement2,
100     &IID_IDispatchEx,
101     &IID_IConnectionPointContainer,
102     NULL
103 };
104
105 static const IID * const body_iids[] = {
106     &IID_IHTMLDOMNode,
107     &IID_IHTMLDOMNode2,
108     &IID_IHTMLElement,
109     &IID_IHTMLElement2,
110     &IID_IHTMLTextContainer,
111     &IID_IHTMLBodyElement,
112     &IID_IDispatchEx,
113     &IID_IConnectionPointContainer,
114     NULL
115 };
116
117 static const IID * const anchor_iids[] = {
118     &IID_IHTMLDOMNode,
119     &IID_IHTMLDOMNode2,
120     &IID_IHTMLElement,
121     &IID_IHTMLElement2,
122     &IID_IHTMLAnchorElement,
123     &IID_IDispatchEx,
124     &IID_IConnectionPointContainer,
125     NULL
126 };
127
128 static const IID * const input_iids[] = {
129     &IID_IHTMLDOMNode,
130     &IID_IHTMLDOMNode2,
131     &IID_IHTMLElement,
132     &IID_IHTMLElement2,
133     &IID_IHTMLInputElement,
134     &IID_IHTMLInputTextElement,
135     &IID_IDispatchEx,
136     &IID_IConnectionPointContainer,
137     NULL
138 };
139
140 static const IID * const select_iids[] = {
141     &IID_IHTMLDOMNode,
142     &IID_IHTMLDOMNode2,
143     &IID_IHTMLElement,
144     &IID_IHTMLElement2,
145     &IID_IHTMLSelectElement,
146     &IID_IDispatchEx,
147     &IID_IConnectionPointContainer,
148     NULL
149 };
150
151 static const IID * const textarea_iids[] = {
152     &IID_IHTMLDOMNode,
153     &IID_IHTMLDOMNode2,
154     &IID_IHTMLElement,
155     &IID_IHTMLElement2,
156     &IID_IHTMLTextAreaElement,
157     &IID_IDispatchEx,
158     &IID_IConnectionPointContainer,
159     NULL
160 };
161
162 static const IID * const option_iids[] = {
163     &IID_IHTMLDOMNode,
164     &IID_IHTMLDOMNode2,
165     &IID_IHTMLElement,
166     &IID_IHTMLElement2,
167     &IID_IHTMLOptionElement,
168     &IID_IDispatchEx,
169     &IID_IConnectionPointContainer,
170     NULL
171 };
172
173 static const IID * const table_iids[] = {
174     &IID_IHTMLDOMNode,
175     &IID_IHTMLDOMNode2,
176     &IID_IHTMLElement,
177     &IID_IHTMLElement2,
178     &IID_IHTMLTable,
179     &IID_IDispatchEx,
180     &IID_IConnectionPointContainer,
181     NULL
182 };
183
184 static const IID * const script_iids[] = {
185     &IID_IHTMLDOMNode,
186     &IID_IHTMLDOMNode2,
187     &IID_IHTMLElement,
188     &IID_IHTMLElement2,
189     &IID_IHTMLScriptElement,
190     &IID_IDispatchEx,
191     &IID_IConnectionPointContainer,
192     NULL
193 };
194
195 static const IID * const text_iids[] = {
196     &IID_IHTMLDOMNode,
197     &IID_IHTMLDOMNode2,
198     &IID_IHTMLDOMTextNode,
199     NULL
200 };
201
202 static const IID * const location_iids[] = {
203     &IID_IDispatch,
204     &IID_IHTMLLocation,
205     NULL
206 };
207
208 static const IID * const window_iids[] = {
209     &IID_IDispatch,
210     &IID_IHTMLWindow2,
211     &IID_IHTMLWindow3,
212     &IID_IDispatchEx,
213     NULL
214 };
215
216 static const IID * const comment_iids[] = {
217     &IID_IHTMLDOMNode,
218     &IID_IHTMLDOMNode2,
219     &IID_IHTMLElement,
220     &IID_IHTMLElement2,
221     &IID_IHTMLCommentElement,
222     &IID_IDispatchEx,
223     &IID_IConnectionPointContainer,
224     NULL
225 };
226
227 static const IID * const img_iids[] = {
228     &IID_IHTMLDOMNode,
229     &IID_IHTMLDOMNode2,
230     &IID_IHTMLElement,
231     &IID_IHTMLElement2,
232     &IID_IDispatchEx,
233     &IID_IHTMLImgElement,
234     &IID_IConnectionPointContainer,
235     NULL
236 };
237
238 static const IID * const generic_iids[] = {
239     &IID_IHTMLDOMNode,
240     &IID_IHTMLDOMNode2,
241     &IID_IHTMLElement,
242     &IID_IHTMLElement2,
243     &IID_IHTMLGenericElement,
244     &IID_IDispatchEx,
245     &IID_IConnectionPointContainer,
246     NULL
247 };
248
249 typedef struct {
250     const char *tag;
251     REFIID *iids;
252     const IID *dispiid;
253 } elem_type_info_t;
254
255 static const elem_type_info_t elem_type_infos[] = {
256     {"",          none_iids,        NULL},
257     {"HTML",      elem_iids,        NULL},
258     {"HEAD",      elem_iids,        NULL},
259     {"TITLE",     elem_iids,        NULL},
260     {"BODY",      body_iids,        &DIID_DispHTMLBody},
261     {"A",         anchor_iids,      NULL},
262     {"INPUT",     input_iids,       &DIID_DispHTMLInputElement},
263     {"SELECT",    select_iids,      &DIID_DispHTMLSelectElement},
264     {"TEXTAREA",  textarea_iids,    NULL},
265     {"OPTION",    option_iids,      &DIID_DispHTMLOptionElement},
266     {"STYLE",     elem_iids,        NULL},
267     {"BLOCKQUOTE",elem_iids,        NULL},
268     {"P",         elem_iids,        NULL},
269     {"BR",        elem_iids,        NULL},
270     {"TABLE",     table_iids,       NULL},
271     {"TBODY",     elem_iids,        NULL},
272     {"SCRIPT",    script_iids,      NULL},
273     {"TEST",      elem_iids,        &DIID_DispHTMLUnknownElement},
274     {"TEST",      generic_iids,     &DIID_DispHTMLGenericElement},
275     {"!",         comment_iids,     &DIID_DispHTMLCommentElement},
276     {"IMG",       img_iids,         &DIID_DispHTMLImg}
277 };
278
279 static const char *dbgstr_w(LPCWSTR str)
280 {
281     static char buf[512];
282     if(!str)
283         return "(null)";
284     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
285     return buf;
286 }
287
288 static const char *dbgstr_guid(REFIID riid)
289 {
290     static char buf[50];
291
292     sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
293             riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
294             riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
295             riid->Data4[5], riid->Data4[6], riid->Data4[7]);
296
297     return buf;
298 }
299
300 static int strcmp_wa(LPCWSTR strw, const char *stra)
301 {
302     WCHAR buf[512];
303     MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
304     return lstrcmpW(strw, buf);
305 }
306
307 static BSTR a2bstr(const char *str)
308 {
309     BSTR ret;
310     int len;
311
312     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
313     ret = SysAllocStringLen(NULL, len);
314     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
315
316     return ret;
317 }
318
319 static IHTMLDocument2 *create_document(void)
320 {
321     IHTMLDocument2 *doc;
322     HRESULT hres;
323
324     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
325             &IID_IHTMLDocument2, (void**)&doc);
326     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
327
328     return doc;
329 }
330
331 #define test_ifaces(i,ids) _test_ifaces(__LINE__,i,ids)
332 static void _test_ifaces(unsigned line, IUnknown *iface, REFIID *iids)
333 {
334     const IID * const *piid;
335     IUnknown *unk;
336     HRESULT hres;
337
338      for(piid = iids; *piid; piid++) {
339         hres = IDispatch_QueryInterface(iface, *piid, (void**)&unk);
340         ok_(__FILE__,line) (hres == S_OK, "Could not get %s interface: %08x\n", dbgstr_guid(*piid), hres);
341         if(SUCCEEDED(hres))
342             IUnknown_Release(unk);
343     }
344 }
345
346 #define test_disp(u,id) _test_disp(__LINE__,u,id)
347 static void _test_disp(unsigned line, IUnknown *unk, const IID *diid)
348 {
349     IDispatchEx *dispex;
350     ITypeInfo *typeinfo;
351     UINT ticnt;
352     HRESULT hres;
353
354     hres = IUnknown_QueryInterface(unk, &IID_IDispatchEx, (void**)&dispex);
355     ok_(__FILE__,line) (hres == S_OK, "Could not get IDispatch: %08x\n", hres);
356     if(FAILED(hres))
357         return;
358
359     ticnt = 0xdeadbeef;
360     hres = IDispatchEx_GetTypeInfoCount(dispex, &ticnt);
361     ok_(__FILE__,line) (hres == S_OK, "GetTypeInfoCount failed: %08x\n", hres);
362     ok_(__FILE__,line) (ticnt == 1, "ticnt=%u\n", ticnt);
363
364     hres = IDispatchEx_GetTypeInfo(dispex, 0, 0, &typeinfo);
365     ok_(__FILE__,line) (hres == S_OK, "GetTypeInfo failed: %08x\n", hres);
366
367     if(SUCCEEDED(hres)) {
368         TYPEATTR *type_attr;
369
370         hres = ITypeInfo_GetTypeAttr(typeinfo, &type_attr);
371         ok_(__FILE__,line) (hres == S_OK, "GetTypeAttr failed: %08x\n", hres);
372         ok_(__FILE__,line) (IsEqualGUID(&type_attr->guid, diid), "unexpected guid %s\n", dbgstr_guid(&type_attr->guid));
373
374         ITypeInfo_ReleaseTypeAttr(typeinfo, type_attr);
375         ITypeInfo_Release(typeinfo);
376     }
377
378     IDispatchEx_Release(dispex);
379 }
380
381 #define get_elem_iface(u) _get_elem_iface(__LINE__,u)
382 static IHTMLElement *_get_elem_iface(unsigned line, IUnknown *unk)
383 {
384     IHTMLElement *elem;
385     HRESULT hres;
386
387     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement, (void**)&elem);
388     ok_(__FILE__,line) (hres == S_OK, "Coule not get IHTMLElement: %08x\n", hres);
389     return elem;
390 }
391
392 #define get_elem2_iface(u) _get_elem2_iface(__LINE__,u)
393 static IHTMLElement2 *_get_elem2_iface(unsigned line, IUnknown *unk)
394 {
395     IHTMLElement2 *elem;
396     HRESULT hres;
397
398     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement2, (void**)&elem);
399     ok_(__FILE__,line) (hres == S_OK, "Coule not get IHTMLElement2: %08x\n", hres);
400     return elem;
401 }
402
403 #define get_node_iface(u) _get_node_iface(__LINE__,u)
404 static IHTMLDOMNode *_get_node_iface(unsigned line, IUnknown *unk)
405 {
406     IHTMLDOMNode *node;
407     HRESULT hres;
408
409     hres = IUnknown_QueryInterface(unk, &IID_IHTMLDOMNode, (void**)&node);
410     ok_(__FILE__,line) (hres == S_OK, "Coule not get IHTMLDOMNode: %08x\n", hres);
411     return node;
412 }
413
414 #define get_img_iface(u) _get_img_iface(__LINE__,u)
415 static IHTMLImgElement *_get_img_iface(unsigned line, IUnknown *unk)
416 {
417     IHTMLImgElement *img;
418     HRESULT hres;
419
420     hres = IUnknown_QueryInterface(unk, &IID_IHTMLImgElement, (void**)&img);
421     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLImgElement: %08x\n", hres);
422     return img;
423 }
424
425 #define test_node_name(u,n) _test_node_name(__LINE__,u,n)
426 static void _test_node_name(unsigned line, IUnknown *unk, const char *exname)
427 {
428     IHTMLDOMNode *node = _get_node_iface(line, unk);
429     BSTR name;
430     HRESULT hres;
431
432     hres = IHTMLDOMNode_get_nodeName(node, &name);
433     IHTMLDOMNode_Release(node);
434     ok_(__FILE__, line) (hres == S_OK, "get_nodeName failed: %08x\n", hres);
435     ok_(__FILE__, line) (!strcmp_wa(name, exname), "got name: %s, expected %s\n", dbgstr_w(name), exname);
436
437     SysFreeString(name);
438 }
439
440 #define test_elem_tag(u,n) _test_elem_tag(__LINE__,u,n)
441 static void _test_elem_tag(unsigned line, IUnknown *unk, const char *extag)
442 {
443     IHTMLElement *elem = _get_elem_iface(line, unk);
444     BSTR tag;
445     HRESULT hres;
446
447     hres = IHTMLElement_get_tagName(elem, &tag);
448     IHTMLElement_Release(elem);
449     ok_(__FILE__, line) (hres == S_OK, "get_tagName failed: %08x\n", hres);
450     ok_(__FILE__, line) (!strcmp_wa(tag, extag), "got tag: %s, expected %s\n", dbgstr_w(tag), extag);
451
452     SysFreeString(tag);
453 }
454
455 #define test_elem_type(ifc,t) _test_elem_type(__LINE__,ifc,t)
456 static void _test_elem_type(unsigned line, IUnknown *unk, elem_type_t type)
457 {
458     _test_elem_tag(line, unk, elem_type_infos[type].tag);
459     _test_ifaces(line, unk, elem_type_infos[type].iids);
460
461     if(elem_type_infos[type].dispiid)
462         _test_disp(line, unk, elem_type_infos[type].dispiid);
463 }
464
465 #define test_elem_attr(e,n,v) _test_elem_attr(__LINE__,e,n,v)
466 static void _test_elem_attr(unsigned line, IHTMLElement *elem, LPCWSTR name, LPCWSTR exval)
467 {
468     VARIANT value;
469     BSTR tmp;
470     HRESULT hres;
471
472     VariantInit(&value);
473
474     tmp = SysAllocString(name);
475     hres = IHTMLElement_getAttribute(elem, tmp, 0, &value);
476     SysFreeString(tmp);
477     ok_(__FILE__,line) (hres == S_OK, "getAttribute failed: %08x\n", hres);
478
479     if(exval) {
480         ok_(__FILE__,line) (V_VT(&value) == VT_BSTR, "vt=%d\n", V_VT(&value));
481         ok_(__FILE__,line) (!lstrcmpW(exval, V_BSTR(&value)), "unexpected value %s\n", dbgstr_w(V_BSTR(&value)));
482     }else {
483         ok_(__FILE__,line) (V_VT(&value) == VT_NULL, "vt=%d\n", V_VT(&value));
484     }
485
486     VariantClear(&value);
487 }
488
489 #define test_elem_offset(u) _test_elem_offset(__LINE__,u)
490 static void _test_elem_offset(unsigned line, IUnknown *unk)
491 {
492     IHTMLElement *elem = _get_elem_iface(line, unk);
493     long l;
494     HRESULT hres;
495
496     hres = IHTMLElement_get_offsetTop(elem, &l);
497     ok_(__FILE__,line) (hres == S_OK, "get_offsetTop failed: %08x\n", hres);
498
499     hres = IHTMLElement_get_offsetHeight(elem, &l);
500     ok_(__FILE__,line) (hres == S_OK, "get_offsetHeight failed: %08x\n", hres);
501
502     IHTMLElement_Release(elem);
503 }
504
505 static void test_doc_elem(IHTMLDocument2 *doc)
506 {
507     IHTMLElement *elem;
508     IHTMLDocument3 *doc3;
509     HRESULT hres;
510
511     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
512     ok(hres == S_OK, "QueryInterface(IID_IHTMLDocument3) failed: %08x\n", hres);
513
514     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
515     IHTMLDocument3_Release(doc3);
516     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
517
518     test_node_name((IUnknown*)elem, "HTML");
519     test_elem_tag((IUnknown*)elem, "HTML");
520
521     IHTMLElement_Release(elem);
522 }
523
524 #define get_doc_elem(d) _get_doc_elem(__LINE__,d)
525 static IHTMLElement *_get_doc_elem(unsigned line, IHTMLDocument2 *doc)
526 {
527     IHTMLElement *elem;
528     IHTMLDocument3 *doc3;
529     HRESULT hres;
530
531     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
532     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDocument3 interface: %08x\n", hres);
533     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
534     ok_(__FILE__,line) (hres == S_OK, "get_documentElement failed: %08x\n", hres);
535     IHTMLDocument3_Release(doc3);
536
537     return elem;
538 }
539
540 #define test_option_text(o,t) _test_option_text(__LINE__,o,t)
541 static void _test_option_text(unsigned line, IHTMLOptionElement *option, const char *text)
542 {
543     BSTR bstr;
544     HRESULT hres;
545
546     hres = IHTMLOptionElement_get_text(option, &bstr);
547     ok_(__FILE__,line) (hres == S_OK, "get_text failed: %08x\n", hres);
548     ok_(__FILE__,line) (!strcmp_wa(bstr, text), "text=%s\n", dbgstr_w(bstr));
549     SysFreeString(bstr);
550 }
551
552 #define test_option_put_text(o,t) _test_option_put_text(__LINE__,o,t)
553 static void _test_option_put_text(unsigned line, IHTMLOptionElement *option, const char *text)
554 {
555     BSTR bstr;
556     HRESULT hres;
557
558     bstr = a2bstr(text);
559     hres = IHTMLOptionElement_put_text(option, bstr);
560     SysFreeString(bstr);
561     ok(hres == S_OK, "put_text failed: %08x\n", hres);
562
563     _test_option_text(line, option, text);
564 }
565
566 #define test_option_value(o,t) _test_option_value(__LINE__,o,t)
567 static void _test_option_value(unsigned line, IHTMLOptionElement *option, const char *value)
568 {
569     BSTR bstr;
570     HRESULT hres;
571
572     hres = IHTMLOptionElement_get_value(option, &bstr);
573     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
574     ok_(__FILE__,line) (!strcmp_wa(bstr, value), "value=%s\n", dbgstr_w(bstr));
575     SysFreeString(bstr);
576 }
577
578 #define test_option_put_value(o,t) _test_option_put_value(__LINE__,o,t)
579 static void _test_option_put_value(unsigned line, IHTMLOptionElement *option, const char *value)
580 {
581     BSTR bstr;
582     HRESULT hres;
583
584     bstr = a2bstr(value);
585     hres = IHTMLOptionElement_put_value(option, bstr);
586     SysFreeString(bstr);
587     ok(hres == S_OK, "put_value failed: %08x\n", hres);
588
589     _test_option_value(line, option, value);
590 }
591
592 #define create_option_elem(d,t,v) _create_option_elem(__LINE__,d,t,v)
593 static IHTMLOptionElement *_create_option_elem(unsigned line, IHTMLDocument2 *doc,
594         const char *txt, const char *val)
595 {
596     IHTMLOptionElementFactory *factory;
597     IHTMLOptionElement *option;
598     IHTMLWindow2 *window;
599     VARIANT text, value, empty;
600     HRESULT hres;
601
602     hres = IHTMLDocument2_get_parentWindow(doc, &window);
603     ok_(__FILE__,line) (hres == S_OK, "get_parentElement failed: %08x\n", hres);
604
605     hres = IHTMLWindow2_get_Option(window, &factory);
606     IHTMLWindow2_Release(window);
607     ok_(__FILE__,line) (hres == S_OK, "get_Option failed: %08x\n", hres);
608
609     V_VT(&text) = VT_BSTR;
610     V_BSTR(&text) = a2bstr(txt);
611     V_VT(&value) = VT_BSTR;
612     V_BSTR(&value) = a2bstr(val);
613     V_VT(&empty) = VT_EMPTY;
614
615     hres = IHTMLOptionElementFactory_create(factory, text, value, empty, empty, &option);
616     ok_(__FILE__,line) (hres == S_OK, "create failed: %08x\n", hres);
617
618     IHTMLOptionElementFactory_Release(factory);
619     VariantClear(&text);
620     VariantClear(&value);
621
622     _test_option_text(line, option, txt);
623     _test_option_value(line, option, val);
624
625     return option;
626 }
627
628 #define test_select_length(s,l) _test_select_length(__LINE__,s,l)
629 static void _test_select_length(unsigned line, IHTMLSelectElement *select, long length)
630 {
631     long len = 0xdeadbeef;
632     HRESULT hres;
633
634     hres = IHTMLSelectElement_get_length(select, &len);
635     ok_(__FILE__,line) (hres == S_OK, "get_length failed: %08x\n", hres);
636     ok_(__FILE__,line) (len == length, "len=%ld, expected %ld\n", len, length);
637 }
638
639 #define test_select_selidx(s,i) _test_select_selidx(__LINE__,s,i)
640 static void _test_select_selidx(unsigned line, IHTMLSelectElement *select, long index)
641 {
642     long idx = 0xdeadbeef;
643     HRESULT hres;
644
645     hres = IHTMLSelectElement_get_selectedIndex(select, &idx);
646     ok_(__FILE__,line) (hres == S_OK, "get_selectedIndex failed: %08x\n", hres);
647     ok_(__FILE__,line) (idx == index, "idx=%ld, expected %ld\n", idx, index);
648 }
649
650 #define test_select_put_selidx(s,i) _test_select_put_selidx(__LINE__,s,i)
651 static void _test_select_put_selidx(unsigned line, IHTMLSelectElement *select, long index)
652 {
653     HRESULT hres;
654
655     hres = IHTMLSelectElement_put_selectedIndex(select, index);
656     ok_(__FILE__,line) (hres == S_OK, "get_selectedIndex failed: %08x\n", hres);
657     _test_select_selidx(line, select, index);
658 }
659
660 #define test_select_value(s,v) _test_select_value(__LINE__,s,v)
661 static void _test_select_value(unsigned line, IHTMLSelectElement *select, const char *exval)
662 {
663     BSTR val;
664     HRESULT hres;
665
666     hres = IHTMLSelectElement_get_value(select, &val);
667     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
668     if(exval)
669         ok_(__FILE__,line) (!strcmp_wa(val, exval), "unexpected value %s\n", dbgstr_w(val));
670     else
671         ok_(__FILE__,line) (val == NULL, "val=%s, expected NULL\n", dbgstr_w(val));
672 }
673
674 #define test_select_set_value(s,v) _test_select_set_value(__LINE__,s,v)
675 static void _test_select_set_value(unsigned line, IHTMLSelectElement *select, const char *val)
676 {
677     BSTR bstr;
678     HRESULT hres;
679
680     bstr = a2bstr(val);
681     hres = IHTMLSelectElement_put_value(select, bstr);
682     SysFreeString(bstr);
683     ok_(__FILE__,line) (hres == S_OK, "put_value failed: %08x\n", hres);
684 }
685
686 #define test_select_type(s,t) _test_select_type(__LINE__,s,t)
687 static void _test_select_type(unsigned line, IHTMLSelectElement *select, const char *extype)
688 {
689     BSTR type;
690     HRESULT hres;
691
692     hres = IHTMLSelectElement_get_type(select, &type);
693     ok_(__FILE__,line) (hres == S_OK, "get_type failed: %08x\n", hres);
694     ok_(__FILE__,line) (!strcmp_wa(type, extype), "type=%s, expected %s\n", dbgstr_w(type), extype);
695 }
696
697 #define test_range_text(r,t) _test_range_text(__LINE__,r,t)
698 static void _test_range_text(unsigned line, IHTMLTxtRange *range, const char *extext)
699 {
700     BSTR text;
701     HRESULT hres;
702
703     hres = IHTMLTxtRange_get_text(range, &text);
704     ok_(__FILE__, line) (hres == S_OK, "get_text failed: %08x\n", hres);
705
706     if(extext) {
707         ok_(__FILE__, line) (text != NULL, "text == NULL\n");
708         ok_(__FILE__, line) (!strcmp_wa(text, extext), "text=\"%s\", expected \"%s\"\n", dbgstr_w(text), extext);
709     }else {
710         ok_(__FILE__, line) (text == NULL, "text=\"%s\", expected NULL\n", dbgstr_w(text));
711     }
712
713     SysFreeString(text);
714
715 }
716
717 #define test_range_collapse(r,b) _test_range_collapse(__LINE__,r,b)
718 static void _test_range_collapse(unsigned line, IHTMLTxtRange *range, BOOL b)
719 {
720     HRESULT hres;
721
722     hres = IHTMLTxtRange_collapse(range, b);
723     ok_(__FILE__, line) (hres == S_OK, "collapse failed: %08x\n", hres);
724     _test_range_text(line, range, NULL);
725 }
726
727 #define test_range_expand(r,u,b,t) _test_range_expand(__LINE__,r,u,b,t)
728 static void _test_range_expand(unsigned line, IHTMLTxtRange *range, LPWSTR unit,
729         VARIANT_BOOL exb, const char *extext)
730 {
731     VARIANT_BOOL b = 0xe0e0;
732     HRESULT hres;
733
734     hres = IHTMLTxtRange_expand(range, unit, &b);
735     ok_(__FILE__,line) (hres == S_OK, "expand failed: %08x\n", hres);
736     ok_(__FILE__,line) (b == exb, "b=%x, expected %x\n", b, exb);
737     _test_range_text(line, range, extext);
738 }
739
740 #define test_range_move(r,u,c,e) _test_range_move(__LINE__,r,u,c,e)
741 static void _test_range_move(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
742 {
743     long c = 0xdeadbeef;
744     HRESULT hres;
745
746     hres = IHTMLTxtRange_move(range, unit, cnt, &c);
747     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
748     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
749     _test_range_text(line, range, NULL);
750 }
751
752 #define test_range_movestart(r,u,c,e) _test_range_movestart(__LINE__,r,u,c,e)
753 static void _test_range_movestart(unsigned line, IHTMLTxtRange *range,
754         LPWSTR unit, long cnt, long excnt)
755 {
756     long c = 0xdeadbeef;
757     HRESULT hres;
758
759     hres = IHTMLTxtRange_moveStart(range, unit, cnt, &c);
760     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
761     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
762 }
763
764 #define test_range_moveend(r,u,c,e) _test_range_moveend(__LINE__,r,u,c,e)
765 static void _test_range_moveend(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
766 {
767     long c = 0xdeadbeef;
768     HRESULT hres;
769
770     hres = IHTMLTxtRange_moveEnd(range, unit, cnt, &c);
771     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
772     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
773 }
774
775 #define test_range_put_text(r,t) _test_range_put_text(__LINE__,r,t)
776 static void _test_range_put_text(unsigned line, IHTMLTxtRange *range, const char *text)
777 {
778     HRESULT hres;
779     BSTR bstr = a2bstr(text);
780
781     hres = IHTMLTxtRange_put_text(range, bstr);
782     ok_(__FILE__,line) (hres == S_OK, "put_text failed: %08x\n", hres);
783     SysFreeString(bstr);
784     _test_range_text(line, range, NULL);
785 }
786
787 #define test_range_inrange(r1,r2,b) _test_range_inrange(__LINE__,r1,r2,b)
788 static void _test_range_inrange(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
789 {
790     VARIANT_BOOL b;
791     HRESULT hres;
792
793     b = 0xe0e0;
794     hres = IHTMLTxtRange_inRange(range1, range2, &b);
795     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
796     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
797 }
798
799 #define test_range_isequal(r1,r2,b) _test_range_isequal(__LINE__,r1,r2,b)
800 static void _test_range_isequal(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
801 {
802     VARIANT_BOOL b;
803     HRESULT hres;
804
805     b = 0xe0e0;
806     hres = IHTMLTxtRange_isEqual(range1, range2, &b);
807     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
808     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
809
810     b = 0xe0e0;
811     hres = IHTMLTxtRange_isEqual(range2, range1, &b);
812     ok_(__FILE__,line) (hres == S_OK, "(2->1) isEqual failed: %08x\n", hres);
813     ok_(__FILE__,line) (b == exb, "(2->1) b=%x, expected %x\n", b, exb);
814
815     if(exb) {
816         test_range_inrange(range1, range2, VARIANT_TRUE);
817         test_range_inrange(range2, range1, VARIANT_TRUE);
818     }
819 }
820
821 #define test_range_parent(r,t) _test_range_parent(__LINE__,r,t)
822 static void _test_range_parent(unsigned line, IHTMLTxtRange *range, elem_type_t type)
823 {
824     IHTMLElement *elem;
825     HRESULT hres;
826
827     hres = IHTMLTxtRange_parentElement(range, &elem);
828     ok_(__FILE__,line) (hres == S_OK, "parentElement failed: %08x\n", hres);
829
830     _test_elem_type(line, (IUnknown*)elem, type);
831
832     IHTMLElement_Release(elem);
833 }
834
835 #define test_elem_collection(c,t,l) _test_elem_collection(__LINE__,c,t,l)
836 static void _test_elem_collection(unsigned line, IUnknown *unk,
837         const elem_type_t *elem_types, long exlen)
838 {
839     IHTMLElementCollection *col;
840     long len;
841     DWORD i;
842     VARIANT name, index;
843     IDispatch *disp;
844     HRESULT hres;
845
846     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElementCollection, (void**)&col);
847     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLElementCollection: %08x\n", hres);
848
849     test_disp((IUnknown*)col, &DIID_DispHTMLElementCollection);
850
851     hres = IHTMLElementCollection_get_length(col, &len);
852     ok_(__FILE__,line) (hres == S_OK, "get_length failed: %08x\n", hres);
853     ok_(__FILE__,line) (len == exlen, "len=%ld, expected %ld\n", len, exlen);
854
855     if(len > exlen)
856         len = exlen;
857
858     V_VT(&index) = VT_EMPTY;
859     V_VT(&name) = VT_I4;
860
861     for(i=0; i<len; i++) {
862         V_I4(&name) = i;
863         disp = (void*)0xdeadbeef;
864         hres = IHTMLElementCollection_item(col, name, index, &disp);
865         ok_(__FILE__,line) (hres == S_OK, "item(%d) failed: %08x\n", i, hres);
866         ok_(__FILE__,line) (disp != NULL, "item returned NULL\n");
867         if(FAILED(hres) || !disp)
868             continue;
869
870         _test_elem_type(line, (IUnknown*)disp, elem_types[i]);
871         IDispatch_Release(disp);
872     }
873
874     V_I4(&name) = len;
875     disp = (void*)0xdeadbeef;
876     hres = IHTMLElementCollection_item(col, name, index, &disp);
877     ok_(__FILE__,line) (hres == S_OK, "item failed: %08x\n", hres);
878     ok_(__FILE__,line) (disp == NULL, "disp != NULL\n");
879
880     V_I4(&name) = -1;
881     disp = (void*)0xdeadbeef;
882     hres = IHTMLElementCollection_item(col, name, index, &disp);
883     ok_(__FILE__,line) (hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
884     ok_(__FILE__,line) (disp == NULL, "disp != NULL\n");
885
886     IHTMLElementCollection_Release(col);
887 }
888
889 #define test_elem_getelembytag(u,t,l) _test_elem_getelembytag(__LINE__,u,t,l)
890 static void _test_elem_getelembytag(unsigned line, IUnknown *unk, elem_type_t type, long exlen)
891 {
892     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
893     IHTMLElementCollection *col = NULL;
894     elem_type_t *types = NULL;
895     BSTR tmp;
896     int i;
897     HRESULT hres;
898
899     tmp = a2bstr(elem_type_infos[type].tag);
900     hres = IHTMLElement2_getElementsByTagName(elem, tmp, &col);
901     SysFreeString(tmp);
902     IHTMLElement2_Release(elem);
903     ok_(__FILE__,line) (hres == S_OK, "getElementByTagName failed: %08x\n", hres);
904     ok_(__FILE__,line) (col != NULL, "col == NULL\n");
905
906     if(exlen) {
907         types = HeapAlloc(GetProcessHeap(), 0, exlen*sizeof(elem_type_t));
908         for(i=0; i<exlen; i++)
909             types[i] = type;
910     }
911
912     _test_elem_collection(line, (IUnknown*)col, types, exlen);
913
914     HeapFree(GetProcessHeap(), 0, types);
915 }
916
917 #define get_first_child(n) _get_first_child(__LINE__,n)
918 static IHTMLDOMNode *_get_first_child(unsigned line, IUnknown *unk)
919 {
920     IHTMLDOMNode *node = _get_node_iface(line, unk);
921     IHTMLDOMNode *child = NULL;
922     HRESULT hres;
923
924     hres = IHTMLDOMNode_get_firstChild(node, &child);
925     IHTMLDOMNode_Release(node);
926     ok_(__FILE__,line) (hres == S_OK, "get_firstChild failed: %08x\n", hres);
927
928     return child;
929 }
930
931 #define test_node_has_child(u,b) _test_node_has_child(__LINE__,u,b)
932 static void _test_node_has_child(unsigned line, IUnknown *unk, VARIANT_BOOL exb)
933 {
934     IHTMLDOMNode *node = _get_node_iface(line, unk);
935     VARIANT_BOOL b = 0xdead;
936     HRESULT hres;
937
938     hres = IHTMLDOMNode_hasChildNodes(node, &b);
939     ok_(__FILE__,line) (hres == S_OK, "hasChildNodes failed: %08x\n", hres);
940     ok_(__FILE__,line) (b == exb, "hasChildNodes=%x, expected %x\n", b, exb);
941
942     IHTMLDOMNode_Release(node);
943 }
944
945 #define test_node_get_parent(u) _test_node_get_parent(__LINE__,u)
946 static IHTMLDOMNode *_test_node_get_parent(unsigned line, IUnknown *unk)
947 {
948     IHTMLDOMNode *node = _get_node_iface(line, unk);
949     IHTMLDOMNode *parent;
950     HRESULT hres;
951
952     hres = IHTMLDOMNode_get_parentNode(node, &parent);
953     IHTMLDOMNode_Release(node);
954     ok_(__FILE__,line) (hres == S_OK, "get_parentNode failed: %08x\n", hres);
955
956     return parent;
957 }
958
959 #define get_node_type(n) _get_node_type(__LINE__,n)
960 static long _get_node_type(unsigned line, IUnknown *unk)
961 {
962     IHTMLDOMNode *node = _get_node_iface(line, unk);
963     long type = -1;
964     HRESULT hres;
965
966     hres = IHTMLDOMNode_get_nodeType(node, &type);
967     ok(hres == S_OK, "get_nodeType failed: %08x\n", hres);
968
969     IHTMLDOMNode_Release(node);
970
971     return type;
972 }
973
974 #define test_elem_get_parent(u) _test_elem_get_parent(__LINE__,u)
975 static IHTMLElement *_test_elem_get_parent(unsigned line, IUnknown *unk)
976 {
977     IHTMLElement *elem = _get_elem_iface(line, unk);
978     IHTMLElement *parent;
979     HRESULT hres;
980
981     hres = IHTMLElement_get_parentElement(elem, &parent);
982     IHTMLElement_Release(elem);
983     ok_(__FILE__,line) (hres == S_OK, "get_parentElement failed: %08x\n", hres);
984
985     return parent;
986 }
987
988 #define elem_get_scroll_height(u) _elem_get_scroll_height(__LINE__,u)
989 static long _elem_get_scroll_height(unsigned line, IUnknown *unk)
990 {
991     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
992     IHTMLTextContainer *txtcont;
993     long l = -1, l2 = -1;
994     HRESULT hres;
995
996     hres = IHTMLElement2_get_scrollHeight(elem, &l);
997     ok_(__FILE__,line) (hres == S_OK, "get_scrollHeight failed: %08x\n", hres);
998     IHTMLElement2_Release(elem);
999
1000     hres = IUnknown_QueryInterface(unk, &IID_IHTMLTextContainer, (void**)&txtcont);
1001     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLTextContainer: %08x\n", hres);
1002
1003     hres = IHTMLTextContainer_get_scrollHeight(txtcont, &l2);
1004     IHTMLTextContainer_Release(txtcont);
1005     ok_(__FILE__,line) (hres == S_OK, "IHTMLTextContainer::get_scrollHeight failed: %ld\n", l2);
1006     ok_(__FILE__,line) (l == l2, "unexpected height %ld, expected %ld\n", l2, l);
1007
1008     return l;
1009 }
1010
1011 #define elem_get_scroll_width(u) _elem_get_scroll_width(__LINE__,u)
1012 static long _elem_get_scroll_width(unsigned line, IUnknown *unk)
1013 {
1014     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1015     IHTMLTextContainer *txtcont;
1016     long l = -1, l2 = -1;
1017     HRESULT hres;
1018
1019     hres = IHTMLElement2_get_scrollWidth(elem, &l);
1020     ok_(__FILE__,line) (hres == S_OK, "get_scrollWidth failed: %08x\n", hres);
1021     IHTMLElement2_Release(elem);
1022
1023     hres = IUnknown_QueryInterface(unk, &IID_IHTMLTextContainer, (void**)&txtcont);
1024     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLTextContainer: %08x\n", hres);
1025
1026     hres = IHTMLTextContainer_get_scrollWidth(txtcont, &l2);
1027     IHTMLTextContainer_Release(txtcont);
1028     ok_(__FILE__,line) (hres == S_OK, "IHTMLTextContainer::get_scrollWidth failed: %ld\n", l2);
1029     ok_(__FILE__,line) (l == l2, "unexpected width %ld, expected %ld\n", l2, l);
1030
1031     return l;
1032 }
1033
1034 #define elem_get_scroll_top(u) _elem_get_scroll_top(__LINE__,u)
1035 static long _elem_get_scroll_top(unsigned line, IUnknown *unk)
1036 {
1037     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1038     IHTMLTextContainer *txtcont;
1039     long l = -1, l2 = -1;
1040     HRESULT hres;
1041
1042     hres = IHTMLElement2_get_scrollTop(elem, &l);
1043     ok_(__FILE__,line) (hres == S_OK, "get_scrollTop failed: %08x\n", hres);
1044     IHTMLElement2_Release(elem);
1045
1046     hres = IUnknown_QueryInterface(unk, &IID_IHTMLTextContainer, (void**)&txtcont);
1047     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLTextContainer: %08x\n", hres);
1048
1049     hres = IHTMLTextContainer_get_scrollTop(txtcont, &l2);
1050     IHTMLTextContainer_Release(txtcont);
1051     ok_(__FILE__,line) (hres == S_OK, "IHTMLTextContainer::get_scrollTop failed: %ld\n", l2);
1052     ok_(__FILE__,line) (l == l2, "unexpected top %ld, expected %ld\n", l2, l);
1053
1054     return l;
1055 }
1056
1057 #define elem_get_scroll_left(u) _elem_get_scroll_left(__LINE__,u)
1058 static void _elem_get_scroll_left(unsigned line, IUnknown *unk)
1059 {
1060     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1061     IHTMLTextContainer *txtcont;
1062     long l = -1, l2 = -1;
1063     HRESULT hres;
1064
1065     hres = IHTMLElement2_get_scrollLeft(elem, NULL);
1066     ok(hres == E_INVALIDARG, "expect E_INVALIDARG got 0x%08x\n", hres);
1067
1068     hres = IHTMLElement2_get_scrollLeft(elem, &l);
1069     ok(hres == S_OK, "get_scrollTop failed: %08x\n", hres);
1070     IHTMLElement2_Release(elem);
1071
1072     hres = IUnknown_QueryInterface(unk, &IID_IHTMLTextContainer, (void**)&txtcont);
1073     ok(hres == S_OK, "Could not get IHTMLTextContainer: %08x\n", hres);
1074
1075     hres = IHTMLTextContainer_get_scrollLeft(txtcont, &l2);
1076     IHTMLTextContainer_Release(txtcont);
1077     ok(hres == S_OK, "IHTMLTextContainer::get_scrollLeft failed: %ld\n", l2);
1078     ok(l == l2, "unexpected left %ld, expected %ld\n", l2, l);
1079 }
1080
1081 #define test_img_set_src(u,s) _test_img_set_src(__LINE__,u,s)
1082 static void _test_img_set_src(unsigned line, IUnknown *unk, const char *src)
1083 {
1084     IHTMLImgElement *img = _get_img_iface(line, unk);
1085     BSTR tmp;
1086     HRESULT hres;
1087
1088     tmp = a2bstr(src);
1089     hres = IHTMLImgElement_put_src(img, tmp);
1090     IHTMLImgElement_Release(img);
1091     SysFreeString(tmp);
1092     ok_(__FILE__,line) (hres == S_OK, "put_src failed: %08x\n", hres);
1093 }
1094
1095 #define test_img_alt(u,a) _test_img_alt(__LINE__,u,a)
1096 static void _test_img_alt(unsigned line, IUnknown *unk, const char *exalt)
1097 {
1098     IHTMLImgElement *img = _get_img_iface(line, unk);
1099     BSTR alt;
1100     HRESULT hres;
1101
1102     hres = IHTMLImgElement_get_alt(img, &alt);
1103     ok_(__FILE__,line) (hres == S_OK, "get_alt failed: %08x\n", hres);
1104     if(exalt)
1105         ok_(__FILE__,line) (!strcmp_wa(alt, exalt), "inexopected alt %s\n", dbgstr_w(alt));
1106     else
1107         ok_(__FILE__,line) (!alt, "alt != NULL\n");
1108     SysFreeString(alt);
1109 }
1110
1111 #define test_img_set_alt(u,a) _test_img_set_alt(__LINE__,u,a)
1112 static void _test_img_set_alt(unsigned line, IUnknown *unk, const char *alt)
1113 {
1114     IHTMLImgElement *img = _get_img_iface(line, unk);
1115     BSTR tmp;
1116     HRESULT hres;
1117
1118     tmp = a2bstr(alt);
1119     hres = IHTMLImgElement_put_alt(img, tmp);
1120     ok_(__FILE__,line) (hres == S_OK, "get_alt failed: %08x\n", hres);
1121     SysFreeString(tmp);
1122
1123     _test_img_alt(line, unk, alt);
1124 }
1125
1126 #define test_input_get_disabled(i,b) _test_input_get_disabled(__LINE__,i,b)
1127 static void _test_input_get_disabled(unsigned line, IHTMLInputElement *input, VARIANT_BOOL exb)
1128 {
1129     VARIANT_BOOL disabled = 100;
1130     HRESULT hres;
1131
1132     hres = IHTMLInputElement_get_disabled(input, &disabled);
1133     ok_(__FILE__,line) (hres == S_OK, "get_disabled failed: %08x\n", hres);
1134     ok_(__FILE__,line) (disabled == exb, "disabled=%x, expected %x\n", disabled, exb);
1135 }
1136
1137 #define test_input_set_disabled(i,b) _test_input_set_disabled(__LINE__,i,b)
1138 static void _test_input_set_disabled(unsigned line, IHTMLInputElement *input, VARIANT_BOOL b)
1139 {
1140     HRESULT hres;
1141
1142     hres = IHTMLInputElement_put_disabled(input, b);
1143     ok_(__FILE__,line) (hres == S_OK, "get_disabled failed: %08x\n", hres);
1144
1145     _test_input_get_disabled(line, input, b);
1146 }
1147
1148 #define test_input_value(o,t) _test_input_value(__LINE__,o,t)
1149 static void _test_input_value(unsigned line, IUnknown *unk, const char *exval)
1150 {
1151     IHTMLInputElement *input;
1152     BSTR bstr;
1153     HRESULT hres;
1154
1155     hres = IUnknown_QueryInterface(unk, &IID_IHTMLInputElement, (void**)&input);
1156     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLInputElement: %08x\n", hres);
1157     if(FAILED(hres))
1158         return;
1159
1160     hres = IHTMLInputElement_get_value(input, &bstr);
1161     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
1162     if(exval)
1163         ok_(__FILE__,line) (!strcmp_wa(bstr, exval), "value=%s\n", dbgstr_w(bstr));
1164     else
1165         ok_(__FILE__,line) (!exval, "exval != NULL\n");
1166     SysFreeString(bstr);
1167     IHTMLInputElement_Release(input);
1168 }
1169
1170 #define test_input_put_value(o,v) _test_input_put_value(__LINE__,o,v)
1171 static void _test_input_put_value(unsigned line, IUnknown *unk, const char *val)
1172 {
1173     IHTMLInputElement *input;
1174     BSTR bstr;
1175     HRESULT hres;
1176
1177     hres = IUnknown_QueryInterface(unk, &IID_IHTMLInputElement, (void**)&input);
1178     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLInputElement: %08x\n", hres);
1179     if(FAILED(hres))
1180         return;
1181
1182     bstr = a2bstr(val);
1183     hres = IHTMLInputElement_get_value(input, &bstr);
1184     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
1185     SysFreeString(bstr);
1186     IHTMLInputElement_Release(input);
1187 }
1188
1189 #define get_child_nodes(u) _get_child_nodes(__LINE__,u)
1190 static IHTMLDOMChildrenCollection *_get_child_nodes(unsigned line, IUnknown *unk)
1191 {
1192     IHTMLDOMNode *node = _get_node_iface(line, unk);
1193     IHTMLDOMChildrenCollection *col = NULL;
1194     IDispatch *disp;
1195     HRESULT hres;
1196
1197     hres = IHTMLDOMNode_get_childNodes(node, &disp);
1198     IHTMLDOMNode_Release(node);
1199     ok_(__FILE__,line) (hres == S_OK, "get_childNodes failed: %08x\n", hres);
1200     if(FAILED(hres))
1201         return NULL;
1202
1203     hres = IDispatch_QueryInterface(disp, &IID_IHTMLDOMChildrenCollection, (void**)&col);
1204     IDispatch_Release(disp);
1205     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDOMChildrenCollection: %08x\n", hres);
1206
1207     return col;
1208 }
1209
1210 #define test_elem_class(u,c) _test_elem_class(__LINE__,u,c)
1211 static void _test_elem_class(unsigned line, IUnknown *unk, const char *exclass)
1212 {
1213     IHTMLElement *elem = _get_elem_iface(line, unk);
1214     BSTR class = (void*)0xdeadbeef;
1215     HRESULT hres;
1216
1217     hres = IHTMLElement_get_className(elem, &class);
1218     IHTMLElement_Release(elem);
1219     ok_(__FILE__,line) (hres == S_OK, "get_className failed: %08x\n", hres);
1220     if(exclass)
1221         ok_(__FILE__,line) (!strcmp_wa(class, exclass), "unexpected className %s\n", dbgstr_w(class));
1222     else
1223         ok_(__FILE__,line) (!class, "class != NULL\n");
1224     SysFreeString(class);
1225 }
1226
1227 #define test_elem_tabindex(u,i) _test_elem_tabindex(__LINE__,u,i)
1228 static void _test_elem_tabindex(unsigned line, IUnknown *unk, short exindex)
1229 {
1230     IHTMLElement2 *elem2 = _get_elem2_iface(line, unk);
1231     short index = -3;
1232     HRESULT hres;
1233
1234     hres = IHTMLElement2_get_tabIndex(elem2, &index);
1235     IHTMLElement2_Release(elem2);
1236     ok_(__FILE__,line) (hres == S_OK, "get_tabIndex failed: %08x\n", hres);
1237     ok_(__FILE__,line) (index == exindex, "unexpected index %d\n", index);
1238 }
1239
1240 #define test_elem_set_tabindex(u,i) _test_elem_set_tabindex(__LINE__,u,i)
1241 static void _test_elem_set_tabindex(unsigned line, IUnknown *unk, short index)
1242 {
1243     IHTMLElement2 *elem2 = _get_elem2_iface(line, unk);
1244     HRESULT hres;
1245
1246     hres = IHTMLElement2_put_tabIndex(elem2, index);
1247     IHTMLElement2_Release(elem2);
1248     ok_(__FILE__,line) (hres == S_OK, "get_tabIndex failed: %08x\n", hres);
1249
1250     _test_elem_tabindex(line, unk, index);
1251 }
1252
1253 #define test_elem_set_class(u,c) _test_elem_set_class(__LINE__,u,c)
1254 static void _test_elem_set_class(unsigned line, IUnknown *unk, const char *class)
1255 {
1256     IHTMLElement *elem = _get_elem_iface(line, unk);
1257     BSTR tmp;
1258     HRESULT hres;
1259
1260     tmp = class ? a2bstr(class) : NULL;
1261     hres = IHTMLElement_put_className(elem, tmp);
1262     IHTMLElement_Release(elem);
1263     ok_(__FILE__,line) (hres == S_OK, "put_className failed: %08x\n", hres);
1264     SysFreeString(tmp);
1265
1266     _test_elem_class(line, unk, class);
1267 }
1268
1269 #define get_child_item(c,i) _get_child_item(__LINE__,c,i)
1270 static IHTMLDOMNode *_get_child_item(unsigned line, IHTMLDOMChildrenCollection *col, long idx)
1271 {
1272     IHTMLDOMNode *node = NULL;
1273     IDispatch *disp;
1274     HRESULT hres;
1275
1276     hres = IHTMLDOMChildrenCollection_item(col, idx, &disp);
1277     ok(hres == S_OK, "item failed: %08x\n", hres);
1278
1279     node = _get_node_iface(line, (IUnknown*)disp);
1280     IDispatch_Release(disp);
1281
1282     return node;
1283 }
1284
1285 #define test_elem_id(e,i) _test_elem_id(__LINE__,e,i)
1286 static void _test_elem_id(unsigned line, IUnknown *unk, const char *exid)
1287 {
1288     IHTMLElement *elem = _get_elem_iface(line, unk);
1289     BSTR id = (void*)0xdeadbeef;
1290     HRESULT hres;
1291
1292     hres = IHTMLElement_get_id(elem, &id);
1293     IHTMLElement_Release(elem);
1294     ok_(__FILE__,line) (hres == S_OK, "get_id failed: %08x\n", hres);
1295
1296     if(exid)
1297         ok_(__FILE__,line) (!strcmp_wa(id, exid), "unexpected id %s\n", dbgstr_w(id));
1298     else
1299         ok_(__FILE__,line) (!id, "id=%s\n", dbgstr_w(id));
1300
1301     SysFreeString(id);
1302 }
1303
1304 #define test_elem_put_id(u,i) _test_elem_put_id(__LINE__,u,i)
1305 static void _test_elem_put_id(unsigned line, IUnknown *unk, const char *new_id)
1306 {
1307     IHTMLElement *elem = _get_elem_iface(line, unk);
1308     BSTR tmp = a2bstr(new_id);
1309     HRESULT hres;
1310
1311     hres = IHTMLElement_put_id(elem, tmp);
1312     IHTMLElement_Release(elem);
1313     SysFreeString(tmp);
1314     ok_(__FILE__,line) (hres == S_OK, "put_id failed: %08x\n", hres);
1315
1316     _test_elem_id(line, unk, new_id);
1317 }
1318
1319 #define test_elem_title(u,t) _test_elem_title(__LINE__,u,t)
1320 static void _test_elem_title(unsigned line, IUnknown *unk, const char *extitle)
1321 {
1322     IHTMLElement *elem = _get_elem_iface(line, unk);
1323     BSTR title;
1324     HRESULT hres;
1325
1326     hres = IHTMLElement_get_title(elem, &title);
1327     IHTMLElement_Release(elem);
1328     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1329     if(extitle)
1330         ok_(__FILE__,line) (!strcmp_wa(title, extitle), "unexpected title %s\n", dbgstr_w(title));
1331     else
1332         ok_(__FILE__,line) (!title, "title=%s, expected NULL\n", dbgstr_w(title));
1333
1334     SysFreeString(title);
1335 }
1336
1337 #define test_elem_set_title(u,t) _test_elem_set_title(__LINE__,u,t)
1338 static void _test_elem_set_title(unsigned line, IUnknown *unk, const char *title)
1339 {
1340     IHTMLElement *elem = _get_elem_iface(line, unk);
1341     BSTR tmp;
1342     HRESULT hres;
1343
1344     tmp = a2bstr(title);
1345     hres = IHTMLElement_put_title(elem, tmp);
1346     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1347
1348     IHTMLElement_Release(elem);
1349     SysFreeString(tmp);
1350 }
1351
1352 #define test_node_get_value_str(u,e) _test_node_get_value_str(__LINE__,u,e)
1353 static void _test_node_get_value_str(unsigned line, IUnknown *unk, const char *exval)
1354 {
1355     IHTMLDOMNode *node = _get_node_iface(line, unk);
1356     VARIANT var;
1357     HRESULT hres;
1358
1359     hres = IHTMLDOMNode_get_nodeValue(node, &var);
1360     IHTMLDOMNode_Release(node);
1361     ok_(__FILE__,line) (hres == S_OK, "get_nodeValue failed: %08x, expected VT_BSTR\n", hres);
1362
1363     if(exval) {
1364         ok_(__FILE__,line) (V_VT(&var) == VT_BSTR, "vt=%d\n", V_VT(&var));
1365         ok_(__FILE__,line) (!strcmp_wa(V_BSTR(&var), exval), "unexpected value %s\n", dbgstr_w(V_BSTR(&var)));
1366     }else {
1367         ok_(__FILE__,line) (V_VT(&var) == VT_NULL, "vt=%d, expected VT_NULL\n", V_VT(&var));
1368     }
1369
1370     VariantClear(&var);
1371 }
1372
1373 #define test_node_put_value_str(u,v) _test_node_put_value_str(__LINE__,u,v)
1374 static void _test_node_put_value_str(unsigned line, IUnknown *unk, const char *val)
1375 {
1376     IHTMLDOMNode *node = _get_node_iface(line, unk);
1377     VARIANT var;
1378     HRESULT hres;
1379
1380     V_VT(&var) = VT_BSTR;
1381     V_BSTR(&var) = a2bstr(val);
1382
1383     hres = IHTMLDOMNode_put_nodeValue(node, var);
1384     ok_(__FILE__,line) (hres == S_OK, "get_nodeValue failed: %08x, expected VT_BSTR\n", hres);
1385     IHTMLDOMNode_Release(node);
1386     VariantClear(&var);
1387 }
1388
1389 #define test_elem_client_size(u) _test_elem_client_size(__LINE__,u)
1390 static void _test_elem_client_size(unsigned line, IUnknown *unk)
1391 {
1392     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1393     long l;
1394     HRESULT hres;
1395
1396     hres = IHTMLElement2_get_clientWidth(elem, &l);
1397     ok_(__FILE__,line) (hres == S_OK, "get_clientWidth failed: %08x\n", hres);
1398     hres = IHTMLElement2_get_clientHeight(elem, &l);
1399     ok_(__FILE__,line) (hres == S_OK, "get_clientHeight failed: %08x\n", hres);
1400
1401     IHTMLElement2_Release(elem);
1402 }
1403
1404 #define test_create_elem(d,t) _test_create_elem(__LINE__,d,t)
1405 static IHTMLElement *_test_create_elem(unsigned line, IHTMLDocument2 *doc, const char *tag)
1406 {
1407     IHTMLElement *elem = NULL;
1408     BSTR tmp;
1409     HRESULT hres;
1410
1411     tmp = a2bstr(tag);
1412     hres = IHTMLDocument2_createElement(doc, tmp, &elem);
1413     ok_(__FILE__,line) (hres == S_OK, "createElement failed: %08x\n", hres);
1414     ok_(__FILE__,line) (elem != NULL, "elem == NULL\n");
1415
1416     return elem;
1417 }
1418
1419 #define test_create_text(d,t) _test_create_text(__LINE__,d,t)
1420 static IHTMLDOMNode *_test_create_text(unsigned line, IHTMLDocument2 *doc, const char *text)
1421 {
1422     IHTMLDocument3 *doc3;
1423     IHTMLDOMNode *node = NULL;
1424     BSTR tmp;
1425     HRESULT hres;
1426
1427     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
1428     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDocument3: %08x\n", hres);
1429
1430     tmp = a2bstr(text);
1431     hres = IHTMLDocument3_createTextNode(doc3, tmp, &node);
1432     IHTMLDocument3_Release(doc3);
1433     ok_(__FILE__,line) (hres == S_OK, "createElement failed: %08x\n", hres);
1434     ok_(__FILE__,line) (node != NULL, "node == NULL\n");
1435
1436     return node;
1437 }
1438
1439 #define test_node_append_child(n,c) _test_node_append_child(__LINE__,n,c)
1440 static IHTMLDOMNode *_test_node_append_child(unsigned line, IUnknown *node_unk, IUnknown *child_unk)
1441 {
1442     IHTMLDOMNode *node = _get_node_iface(line, node_unk);
1443     IHTMLDOMNode *child = _get_node_iface(line, child_unk);
1444     IHTMLDOMNode *new_child = NULL;
1445     HRESULT hres;
1446
1447     hres = IHTMLDOMNode_appendChild(node, child, &new_child);
1448     ok_(__FILE__,line) (hres == S_OK, "appendChild failed: %08x\n", hres);
1449     ok_(__FILE__,line) (new_child != NULL, "new_child == NULL\n");
1450     /* TODO  ok_(__FILE__,line) (new_child != child, "new_child == child\n"); */
1451
1452     IHTMLDOMNode_Release(node);
1453     IHTMLDOMNode_Release(child);
1454
1455     return new_child;
1456 }
1457
1458 #define test_node_remove_child(n,c) _test_node_remove_child(__LINE__,n,c)
1459 static void _test_node_remove_child(unsigned line, IUnknown *unk, IHTMLDOMNode *child)
1460 {
1461     IHTMLDOMNode *node = _get_node_iface(line, unk);
1462     IHTMLDOMNode *new_node = NULL;
1463     HRESULT hres;
1464
1465     hres = IHTMLDOMNode_removeChild(node, child, &new_node);
1466     ok_(__FILE__,line) (hres == S_OK, "appendChild failed: %08x\n", hres);
1467     ok_(__FILE__,line) (new_node != NULL, "new_node == NULL\n");
1468     /* TODO ok_(__FILE__,line) (new_node != child, "new_node == child\n"); */
1469
1470     IHTMLDOMNode_Release(node);
1471     IHTMLDOMNode_Release(new_node);
1472 }
1473
1474 #define test_doc_title(d,t) _test_doc_title(__LINE__,d,t)
1475 static void _test_doc_title(unsigned line, IHTMLDocument2 *doc, const char *extitle)
1476 {
1477     BSTR title = NULL;
1478     HRESULT hres;
1479
1480     hres = IHTMLDocument2_get_title(doc, &title);
1481     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1482     ok_(__FILE__,line) (!strcmp_wa(title, extitle), "unexpected title %s\n", dbgstr_w(title));
1483     SysFreeString(title);
1484 }
1485
1486 #define test_doc_set_title(d,t) _test_doc_set_title(__LINE__,d,t)
1487 static void _test_doc_set_title(unsigned line, IHTMLDocument2 *doc, const char *title)
1488 {
1489     BSTR tmp;
1490     HRESULT hres;
1491
1492     tmp = a2bstr(title);
1493     hres = IHTMLDocument2_put_title(doc, tmp);
1494     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1495     SysFreeString(tmp);
1496 }
1497
1498 static void test_elem_col_item(IHTMLElementCollection *col, LPCWSTR n,
1499         const elem_type_t *elem_types, long len)
1500 {
1501     IDispatch *disp;
1502     VARIANT name, index;
1503     DWORD i;
1504     HRESULT hres;
1505
1506     V_VT(&index) = VT_EMPTY;
1507     V_VT(&name) = VT_BSTR;
1508     V_BSTR(&name) = SysAllocString(n);
1509
1510     hres = IHTMLElementCollection_item(col, name, index, &disp);
1511     ok(hres == S_OK, "item failed: %08x\n", hres);
1512
1513     test_elem_collection((IUnknown*)disp, elem_types, len);
1514     IDispatch_Release(disp);
1515     ok(hres == S_OK, "Could not get IHTMLElementCollection interface: %08x\n", hres);
1516     if(hres != S_OK)
1517         goto cleanup;
1518
1519     V_VT(&index) = VT_I4;
1520
1521     for(i=0; i<len; i++) {
1522         V_I4(&index) = i;
1523         disp = (void*)0xdeadbeef;
1524         hres = IHTMLElementCollection_item(col, name, index, &disp);
1525         ok(hres == S_OK, "item failed: %08x\n", hres);
1526         ok(disp != NULL, "disp == NULL\n");
1527         if(FAILED(hres) || !disp)
1528             continue;
1529
1530         test_elem_type((IUnknown*)disp, elem_types[i]);
1531
1532         IDispatch_Release(disp);
1533     }
1534
1535     V_I4(&index) = len;
1536     disp = (void*)0xdeadbeef;
1537     hres = IHTMLElementCollection_item(col, name, index, &disp);
1538     ok(hres == S_OK, "item failed: %08x\n", hres);
1539     ok(disp == NULL, "disp != NULL\n");
1540
1541     V_I4(&index) = -1;
1542     disp = (void*)0xdeadbeef;
1543     hres = IHTMLElementCollection_item(col, name, index, &disp);
1544     ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
1545     ok(disp == NULL, "disp != NULL\n");
1546
1547 cleanup:
1548     SysFreeString(V_BSTR(&name));
1549 }
1550
1551 static IHTMLElement *get_elem_by_id(IHTMLDocument2 *doc, LPCWSTR id, BOOL expect_success)
1552 {
1553     IHTMLElementCollection *col;
1554     IHTMLElement *elem;
1555     IDispatch *disp = (void*)0xdeadbeef;
1556     VARIANT name, index;
1557     HRESULT hres;
1558
1559     hres = IHTMLDocument2_get_all(doc, &col);
1560     ok(hres == S_OK, "get_all failed: %08x\n", hres);
1561     ok(col != NULL, "col == NULL\n");
1562     if(FAILED(hres) || !col)
1563         return NULL;
1564
1565     V_VT(&index) = VT_EMPTY;
1566     V_VT(&name) = VT_BSTR;
1567     V_BSTR(&name) = SysAllocString(id);
1568
1569     hres = IHTMLElementCollection_item(col, name, index, &disp);
1570     IHTMLElementCollection_Release(col);
1571     SysFreeString(V_BSTR(&name));
1572     ok(hres == S_OK, "item failed: %08x\n", hres);
1573     if(!expect_success) {
1574         ok(disp == NULL, "disp != NULL\n");
1575         return NULL;
1576     }
1577
1578     ok(disp != NULL, "disp == NULL\n");
1579     if(!disp)
1580         return NULL;
1581
1582     elem = get_elem_iface((IUnknown*)disp);
1583     IDispatch_Release(disp);
1584
1585     return elem;
1586 }
1587
1588 static IHTMLElement *get_doc_elem_by_id(IHTMLDocument2 *doc, LPCWSTR id)
1589 {
1590     IHTMLDocument3 *doc3;
1591     IHTMLElement *elem;
1592     BSTR tmp;
1593     HRESULT hres;
1594
1595     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
1596     ok(hres == S_OK, "Could not get IHTMLDocument3 iface: %08x\n", hres);
1597
1598     tmp = SysAllocString(id);
1599     hres = IHTMLDocument3_getElementById(doc3, tmp, &elem);
1600     SysFreeString(tmp);
1601     ok(hres == S_OK, "getElementById(%s) failed: %08x\n", dbgstr_w(id), hres);
1602
1603     IHTMLDocument3_Release(doc3);
1604
1605     return elem;
1606 }
1607
1608 static void test_select_elem(IHTMLSelectElement *select)
1609 {
1610     test_select_type(select, "select-one");
1611     test_select_length(select, 2);
1612     test_select_selidx(select, 0);
1613     test_select_put_selidx(select, 1);
1614
1615     test_select_set_value(select, "val1");
1616     test_select_value(select, "val1");
1617 }
1618
1619 static void test_create_option_elem(IHTMLDocument2 *doc)
1620 {
1621     IHTMLOptionElement *option;
1622
1623     option = create_option_elem(doc, "test text", "test value");
1624
1625     test_option_put_text(option, "new text");
1626     test_option_put_value(option, "new value");
1627
1628     IHTMLOptionElement_Release(option);
1629 }
1630
1631 static IHTMLTxtRange *test_create_body_range(IHTMLDocument2 *doc)
1632 {
1633     IHTMLBodyElement *body;
1634     IHTMLTxtRange *range;
1635     IHTMLElement *elem;
1636     HRESULT hres;
1637
1638     hres = IHTMLDocument2_get_body(doc, &elem);
1639     ok(hres == S_OK, "get_body failed: %08x\n", hres);
1640
1641     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
1642     IHTMLElement_Release(elem);
1643
1644     hres = IHTMLBodyElement_createTextRange(body, &range);
1645     IHTMLBodyElement_Release(body);
1646     ok(hres == S_OK, "createTextRange failed: %08x\n", hres);
1647
1648     return range;
1649 }
1650
1651 static void test_txtrange(IHTMLDocument2 *doc)
1652 {
1653     IHTMLTxtRange *body_range, *range, *range2;
1654     IHTMLSelectionObject *selection;
1655     IDispatch *disp_range;
1656     HRESULT hres;
1657
1658     body_range = test_create_body_range(doc);
1659
1660     test_range_text(body_range, "test abc 123\r\nit's text");
1661
1662     hres = IHTMLTxtRange_duplicate(body_range, &range);
1663     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1664
1665     hres = IHTMLTxtRange_duplicate(body_range, &range2);
1666     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1667     test_range_isequal(range, range2, VARIANT_TRUE);
1668
1669     test_range_text(range, "test abc 123\r\nit's text");
1670     test_range_text(body_range, "test abc 123\r\nit's text");
1671
1672     test_range_collapse(range, TRUE);
1673     test_range_isequal(range, range2, VARIANT_FALSE);
1674     test_range_inrange(range, range2, VARIANT_FALSE);
1675     test_range_inrange(range2, range, VARIANT_TRUE);
1676     IHTMLTxtRange_Release(range2);
1677
1678     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
1679     test_range_expand(range, wordW, VARIANT_FALSE, "test ");
1680     test_range_move(range, characterW, 2, 2);
1681     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
1682
1683     test_range_collapse(range, FALSE);
1684     test_range_expand(range, wordW, VARIANT_TRUE, "abc ");
1685
1686     test_range_collapse(range, FALSE);
1687     test_range_expand(range, wordW, VARIANT_TRUE, "123");
1688     test_range_expand(range, wordW, VARIANT_FALSE, "123");
1689     test_range_move(range, characterW, 2, 2);
1690     test_range_expand(range, wordW, VARIANT_TRUE, "123");
1691     test_range_moveend(range, characterW, -5, -5);
1692     test_range_text(range, NULL);
1693     test_range_moveend(range, characterW, 3, 3);
1694     test_range_text(range, "c 1");
1695     test_range_expand(range, texteditW, VARIANT_TRUE, "test abc 123\r\nit's text");
1696     test_range_collapse(range, TRUE);
1697     test_range_move(range, characterW, 4, 4);
1698     test_range_moveend(range, characterW, 1, 1);
1699     test_range_text(range, " ");
1700     test_range_move(range, wordW, 1, 1);
1701     test_range_moveend(range, characterW, 2, 2);
1702     test_range_text(range, "ab");
1703
1704     IHTMLTxtRange_Release(range);
1705
1706     hres = IHTMLTxtRange_duplicate(body_range, &range);
1707     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1708
1709     test_range_text(range, "test abc 123\r\nit's text");
1710     test_range_move(range, characterW, 3, 3);
1711     test_range_moveend(range, characterW, 1, 1);
1712     test_range_text(range, "t");
1713     test_range_moveend(range, characterW, 3, 3);
1714     test_range_text(range, "t ab");
1715     test_range_moveend(range, characterW, -2, -2);
1716     test_range_text(range, "t ");
1717     test_range_move(range, characterW, 6, 6);
1718     test_range_moveend(range, characterW, 3, 3);
1719     test_range_text(range, "123");
1720     test_range_moveend(range, characterW, 2, 2);
1721     test_range_text(range, "123\r\ni");
1722
1723     IHTMLTxtRange_Release(range);
1724
1725     hres = IHTMLTxtRange_duplicate(body_range, &range);
1726     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1727
1728     test_range_move(range, wordW, 1, 1);
1729     test_range_moveend(range, characterW, 2, 2);
1730     test_range_text(range, "ab");
1731
1732     test_range_move(range, characterW, -2, -2);
1733     test_range_moveend(range, characterW, 2, 2);
1734     test_range_text(range, "t ");
1735
1736     test_range_move(range, wordW, 3, 3);
1737     test_range_move(range, wordW, -2, -2);
1738     test_range_moveend(range, characterW, 2, 2);
1739     test_range_text(range, "ab");
1740
1741     test_range_move(range, characterW, -6, -5);
1742     test_range_moveend(range, characterW, -1, 0);
1743     test_range_moveend(range, characterW, -6, 0);
1744     test_range_move(range, characterW, 2, 2);
1745     test_range_moveend(range, characterW, 2, 2);
1746     test_range_text(range, "st");
1747     test_range_moveend(range, characterW, -6, -4);
1748     test_range_moveend(range, characterW, 2, 2);
1749
1750     IHTMLTxtRange_Release(range);
1751
1752     hres = IHTMLTxtRange_duplicate(body_range, &range);
1753     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1754
1755     test_range_move(range, wordW, 2, 2);
1756     test_range_moveend(range, characterW, 2, 2);
1757     test_range_text(range, "12");
1758
1759     test_range_move(range, characterW, 15, 14);
1760     test_range_move(range, characterW, -2, -2);
1761     test_range_moveend(range, characterW, 3, 2);
1762     test_range_text(range, "t");
1763     test_range_moveend(range, characterW, -1, -1);
1764     test_range_text(range, "t");
1765     test_range_expand(range, wordW, VARIANT_TRUE, "text");
1766     test_range_move(range, characterW, -2, -2);
1767     test_range_moveend(range, characterW, 2, 2);
1768     test_range_text(range, "s ");
1769     test_range_move(range, characterW, 100, 7);
1770     test_range_move(range, wordW, 1, 0);
1771     test_range_move(range, characterW, -2, -2);
1772     test_range_moveend(range, characterW, 3, 2);
1773     test_range_text(range, "t");
1774
1775     IHTMLTxtRange_Release(range);
1776
1777     hres = IHTMLTxtRange_duplicate(body_range, &range);
1778     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
1779
1780     test_range_collapse(range, TRUE);
1781     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
1782     test_range_put_text(range, "word");
1783     test_range_text(body_range, "wordabc 123\r\nit's text");
1784     test_range_text(range, NULL);
1785     test_range_moveend(range, characterW, 3, 3);
1786     test_range_text(range, "abc");
1787     test_range_movestart(range, characterW, -2, -2);
1788     test_range_text(range, "rdabc");
1789     test_range_movestart(range, characterW, 3, 3);
1790     test_range_text(range, "bc");
1791     test_range_movestart(range, characterW, 4, 4);
1792     test_range_text(range, NULL);
1793     test_range_movestart(range, characterW, -3, -3);
1794     test_range_text(range, "c 1");
1795     test_range_movestart(range, characterW, -7, -6);
1796     test_range_text(range, "wordabc 1");
1797     test_range_movestart(range, characterW, 100, 22);
1798     test_range_text(range, NULL);
1799
1800     IHTMLTxtRange_Release(range);
1801     IHTMLTxtRange_Release(body_range);
1802
1803     hres = IHTMLDocument2_get_selection(doc, &selection);
1804     ok(hres == S_OK, "IHTMLDocument2_get_selection failed: %08x\n", hres);
1805
1806     hres = IHTMLSelectionObject_createRange(selection, &disp_range);
1807     ok(hres == S_OK, "IHTMLSelectionObject_createRange failed: %08x\n", hres);
1808     IHTMLSelectionObject_Release(selection);
1809
1810     hres = IDispatch_QueryInterface(disp_range, &IID_IHTMLTxtRange, (void **)&range);
1811     ok(hres == S_OK, "Could not get IID_IHTMLTxtRange interface: 0x%08x\n", hres);
1812     IDispatch_Release(disp_range);
1813
1814     test_range_text(range, NULL);
1815     test_range_moveend(range, characterW, 3, 3);
1816     test_range_text(range, "wor");
1817     test_range_parent(range, ET_BODY);
1818     test_range_expand(range, texteditW, VARIANT_TRUE, "wordabc 123\r\nit's text");
1819     test_range_expand(range, texteditW, VARIANT_TRUE, "wordabc 123\r\nit's text");
1820     test_range_move(range, characterW, 3, 3);
1821     test_range_expand(range, wordW, VARIANT_TRUE, "wordabc ");
1822     test_range_moveend(range, characterW, -4, -4);
1823     test_range_put_text(range, "abc def ");
1824     test_range_expand(range, texteditW, VARIANT_TRUE, "abc def abc 123\r\nit's text");
1825     test_range_move(range, wordW, 1, 1);
1826     test_range_movestart(range, characterW, -1, -1);
1827     test_range_text(range, " ");
1828     test_range_move(range, wordW, 1, 1);
1829     test_range_moveend(range, characterW, 3, 3);
1830     test_range_text(range, "def");
1831     test_range_put_text(range, "xyz");
1832     test_range_moveend(range, characterW, 1, 1);
1833     test_range_move(range, wordW, 1, 1);
1834     test_range_moveend(range, characterW, 2, 2);
1835     test_range_text(range, "ab");
1836
1837     IHTMLTxtRange_Release(range);
1838 }
1839
1840 static void test_txtrange2(IHTMLDocument2 *doc)
1841 {
1842     IHTMLTxtRange *range;
1843
1844     range = test_create_body_range(doc);
1845
1846     test_range_text(range, "abc\r\n\r\n123\r\n\r\n\r\ndef");
1847     test_range_move(range, characterW, 5, 5);
1848     test_range_moveend(range, characterW, 1, 1);
1849     test_range_text(range, "2");
1850     test_range_move(range, characterW, -3, -3);
1851     test_range_moveend(range, characterW, 3, 3);
1852     test_range_text(range, "c\r\n\r\n1");
1853     test_range_collapse(range, VARIANT_FALSE);
1854     test_range_moveend(range, characterW, 4, 4);
1855     test_range_text(range, "23");
1856     test_range_moveend(range, characterW, 1, 1);
1857     test_range_text(range, "23\r\n\r\n\r\nd");
1858     test_range_moveend(range, characterW, -1, -1);
1859     test_range_text(range, "23");
1860     test_range_moveend(range, characterW, -1, -1);
1861     test_range_text(range, "23");
1862     test_range_moveend(range, characterW, -2, -2);
1863     test_range_text(range, "2");
1864
1865     IHTMLTxtRange_Release(range);
1866 }
1867
1868 static void test_compatmode(IHTMLDocument2 *doc)
1869 {
1870     IHTMLDocument5 *doc5;
1871     BSTR mode;
1872     HRESULT hres;
1873
1874     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument5, (void**)&doc5);
1875     ok(hres == S_OK, "Could not get IHTMLDocument5 interface: %08x\n", hres);
1876     if(FAILED(hres))
1877         return;
1878
1879     hres = IHTMLDocument5_get_compatMode(doc5, &mode);
1880     IHTMLDocument5_Release(doc5);
1881     ok(hres == S_OK, "get_compatMode failed: %08x\n", hres);
1882     ok(!strcmp_wa(mode, "BackCompat"), "compatMode=%s\n", dbgstr_w(mode));
1883     SysFreeString(mode);
1884 }
1885
1886 static void test_location(IHTMLDocument2 *doc)
1887 {
1888     IHTMLLocation *location, *location2;
1889     ULONG ref;
1890     HRESULT hres;
1891
1892     hres = IHTMLDocument2_get_location(doc, &location);
1893     ok(hres == S_OK, "get_location failed: %08x\n", hres);
1894
1895     hres = IHTMLDocument2_get_location(doc, &location2);
1896     ok(hres == S_OK, "get_location failed: %08x\n", hres);
1897
1898     ok(location == location2, "location != location2\n");
1899
1900     test_ifaces((IUnknown*)location, location_iids);
1901
1902     IHTMLLocation_Release(location2);
1903     ref = IHTMLLocation_Release(location);
1904     ok(!ref, "location chould be destroyed here\n");
1905 }
1906
1907 static void test_navigator(IHTMLDocument2 *doc)
1908 {
1909     IHTMLWindow2 *window;
1910     IOmNavigator *navigator, *navigator2;
1911     ULONG ref;
1912     BSTR bstr;
1913     HRESULT hres;
1914
1915     hres = IHTMLDocument2_get_parentWindow(doc, &window);
1916     ok(hres == S_OK, "parentWidnow failed: %08x\n", hres);
1917
1918     hres = IHTMLWindow2_get_navigator(window, &navigator);
1919     ok(hres == S_OK, "get_navigator failed: %08x\n", hres);
1920     ok(navigator != NULL, "navigator == NULL\n");
1921     test_disp((IUnknown*)navigator, &IID_IOmNavigator);
1922
1923     hres = IHTMLWindow2_get_navigator(window, &navigator2);
1924     ok(hres == S_OK, "get_navigator failed: %08x\n", hres);
1925     ok(navigator != navigator2, "navigator2 != navihgator\n");
1926
1927     IHTMLWindow2_Release(window);
1928     IOmNavigator_Release(navigator2);
1929
1930     hres = IOmNavigator_get_appCodeName(navigator, &bstr);
1931     ok(hres == S_OK, "get_appCodeName failed: %08x\n", hres);
1932     ok(!strcmp_wa(bstr, "Mozilla"), "Unexpected appCodeName %s\n", dbgstr_w(bstr));
1933     SysFreeString(bstr);
1934
1935     bstr = NULL;
1936     hres = IOmNavigator_get_platform(navigator, &bstr);
1937     ok(hres == S_OK, "get_appMinorVersion failed: %08x\n", hres);
1938     ok(!strcmp_wa(bstr, "Win32"), "unexpected platform %s\n", dbgstr_w(bstr));
1939     SysFreeString(bstr);
1940
1941     ref = IOmNavigator_Release(navigator);
1942     ok(!ref, "navigator should be destroyed here\n");
1943 }
1944
1945 static void test_default_style(IHTMLStyle *style)
1946 {
1947     VARIANT_BOOL b;
1948     VARIANT v;
1949     BSTR str;
1950     HRESULT hres;
1951
1952     test_disp((IUnknown*)style, &DIID_DispHTMLStyle);
1953
1954     str = (void*)0xdeadbeef;
1955     hres = IHTMLStyle_get_fontFamily(style, &str);
1956     ok(hres == S_OK, "get_fontFamily failed: %08x\n", hres);
1957     ok(!str, "fontFamily = %s\n", dbgstr_w(str));
1958
1959     str = (void*)0xdeadbeef;
1960     hres = IHTMLStyle_get_fontWeight(style, &str);
1961     ok(hres == S_OK, "get_fontWeight failed: %08x\n", hres);
1962     ok(!str, "fontWeight = %s\n", dbgstr_w(str));
1963
1964     str = (void*)0xdeadbeef;
1965     hres = IHTMLStyle_get_display(style, &str);
1966     ok(hres == S_OK, "get_display failed: %08x\n", hres);
1967     ok(!str, "display = %s\n", dbgstr_w(str));
1968
1969     str = (void*)0xdeadbeef;
1970     hres = IHTMLStyle_get_visibility(style, &str);
1971     ok(hres == S_OK, "get_visibility failed: %08x\n", hres);
1972     ok(!str, "visibility = %s\n", dbgstr_w(str));
1973
1974     V_VT(&v) = VT_NULL;
1975     hres = IHTMLStyle_get_fontSize(style, &v);
1976     ok(hres == S_OK, "get_fontSize failed: %08x\n", hres);
1977     ok(V_VT(&v) == VT_BSTR, "V_VT(fontSize) = %d\n", V_VT(&v));
1978     ok(!V_BSTR(&v), "V_BSTR(fontSize) = %s\n", dbgstr_w(V_BSTR(&v)));
1979
1980     V_VT(&v) = VT_NULL;
1981     hres = IHTMLStyle_get_color(style, &v);
1982     ok(hres == S_OK, "get_color failed: %08x\n", hres);
1983     ok(V_VT(&v) == VT_BSTR, "V_VT(color) = %d\n", V_VT(&v));
1984     ok(!V_BSTR(&v), "V_BSTR(color) = %s\n", dbgstr_w(V_BSTR(&v)));
1985
1986     b = 0xfefe;
1987     hres = IHTMLStyle_get_textDecorationUnderline(style, &b);
1988     ok(hres == S_OK, "get_textDecorationUnderline failed: %08x\n", hres);
1989     ok(b == VARIANT_FALSE, "textDecorationUnderline = %x\n", b);
1990
1991     b = 0xfefe;
1992     hres = IHTMLStyle_get_textDecorationLineThrough(style, &b);
1993     ok(hres == S_OK, "get_textDecorationLineThrough failed: %08x\n", hres);
1994     ok(b == VARIANT_FALSE, "textDecorationLineThrough = %x\n", b);
1995
1996     V_VT(&v) = VT_EMPTY;
1997     hres = IHTMLStyle_get_width(style, &v);
1998     ok(hres == S_OK, "get_width failed: %08x\n", hres);
1999     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2000     ok(!V_BSTR(&v), "V_BSTR(v)=%p\n", V_BSTR(&v));
2001
2002     V_VT(&v) = VT_BSTR;
2003     V_BSTR(&v) = a2bstr("auto");
2004     hres = IHTMLStyle_put_width(style, v);
2005     ok(hres == S_OK, "put_width failed: %08x\n", hres);
2006     VariantClear(&v);
2007
2008     V_VT(&v) = VT_EMPTY;
2009     hres = IHTMLStyle_get_width(style, &v);
2010     ok(hres == S_OK, "get_width failed: %08x\n", hres);
2011     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2012     ok(!strcmp_wa(V_BSTR(&v), "auto"), "V_BSTR(v)=%s\n", dbgstr_w(V_BSTR(&v)));
2013     VariantClear(&v);
2014
2015     /* margin tests */
2016     str = (void*)0xdeadbeef;
2017     hres = IHTMLStyle_get_margin(style, &str);
2018     ok(hres == S_OK, "get_margin failed: %08x\n", hres);
2019     ok(!str, "margin = %s\n", dbgstr_w(str));
2020
2021     str = a2bstr("1");
2022     hres = IHTMLStyle_put_margin(style, str);
2023     ok(hres == S_OK, "get_margin failed: %08x\n", hres);
2024     SysFreeString(str);
2025
2026     hres = IHTMLStyle_get_margin(style, &str);
2027     ok(hres == S_OK, "get_margin failed: %08x\n", hres);
2028     ok(strcmp_wa(str, "1"), "margin = %s\n", dbgstr_w(str));
2029
2030     hres = IHTMLStyle_put_margin(style, NULL);
2031     ok(hres == S_OK, "get_margin failed: %08x\n", hres);
2032 }
2033
2034 static void test_default_selection(IHTMLDocument2 *doc)
2035 {
2036     IHTMLSelectionObject *selection;
2037     IHTMLTxtRange *range;
2038     IDispatch *disp;
2039     BSTR str;
2040     HRESULT hres;
2041
2042     hres = IHTMLDocument2_get_selection(doc, &selection);
2043     ok(hres == S_OK, "get_selection failed: %08x\n", hres);
2044
2045     hres = IHTMLSelectionObject_get_type(selection, &str);
2046     ok(hres == S_OK, "get_type failed: %08x\n", hres);
2047     ok(!lstrcmpW(str, noneW), "type = %s\n", dbgstr_w(str));
2048     SysFreeString(str);
2049
2050     hres = IHTMLSelectionObject_createRange(selection, &disp);
2051     IHTMLSelectionObject_Release(selection);
2052     ok(hres == S_OK, "createRange failed: %08x\n", hres);
2053
2054     hres = IDispatch_QueryInterface(disp, &IID_IHTMLTxtRange, (void**)&range);
2055     IDispatch_Release(disp);
2056     ok(hres == S_OK, "Could not get IHTMLTxtRange interface: %08x\n", hres);
2057
2058     test_range_text(range, NULL);
2059     IHTMLTxtRange_Release(range);
2060 }
2061
2062 static void test_default_body(IHTMLBodyElement *body)
2063 {
2064     long l;
2065     BSTR bstr;
2066     HRESULT hres;
2067     VARIANT v;
2068     WCHAR sBodyText[] = {'#','F','F','0','0','0','0',0};
2069     WCHAR sTextInvalid[] = {'I','n','v','a','l','i','d',0};
2070     WCHAR sResInvalid[] = {'#','0','0','a','0','d','0',0};
2071
2072     bstr = (void*)0xdeadbeef;
2073     hres = IHTMLBodyElement_get_background(body, &bstr);
2074     ok(hres == S_OK, "get_background failed: %08x\n", hres);
2075     ok(bstr == NULL, "bstr != NULL\n");
2076
2077     l = elem_get_scroll_height((IUnknown*)body);
2078     ok(l != -1, "scrollHeight == -1\n");
2079     l = elem_get_scroll_width((IUnknown*)body);
2080     ok(l != -1, "scrollWidth == -1\n");
2081     l = elem_get_scroll_top((IUnknown*)body);
2082     ok(!l, "scrollTop = %ld\n", l);
2083     elem_get_scroll_left((IUnknown*)body);
2084
2085     /* get_text tests */
2086     hres = IHTMLBodyElement_get_text(body, &v);
2087     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2088     ok(V_VT(&v) == VT_BSTR, "Expected VT_BSTR got %d\n", V_VT(&v));
2089     ok(bstr == NULL, "bstr != NULL\n");
2090
2091
2092     /* get_text - Invalid Text */
2093     V_VT(&v) = VT_BSTR;
2094     V_BSTR(&v) = SysAllocString(sTextInvalid);
2095     hres = IHTMLBodyElement_put_text(body, v);
2096     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2097
2098     V_VT(&v) = VT_NULL;
2099     hres = IHTMLBodyElement_get_text(body, &v);
2100     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2101     ok(V_VT(&v) == VT_BSTR, "Expected VT_BSTR got %d\n", V_VT(&v));
2102     ok(!lstrcmpW(sResInvalid, V_BSTR(&v)), "bstr != sResInvalid\n");
2103     VariantClear(&v);
2104
2105     /* get_text - Valid Text */
2106     V_VT(&v) = VT_BSTR;
2107     V_BSTR(&v) = SysAllocString(sBodyText);
2108     hres = IHTMLBodyElement_put_text(body, v);
2109     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2110
2111     V_VT(&v) = VT_NULL;
2112     hres = IHTMLBodyElement_get_text(body, &v);
2113     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2114     ok(V_VT(&v) == VT_BSTR, "Expected VT_BSTR got %d\n", V_VT(&v));
2115     ok(lstrcmpW(bstr, V_BSTR(&v)), "bstr != V_BSTR(&v)\n");
2116     VariantClear(&v);
2117 }
2118
2119 static void test_body_funs(IHTMLBodyElement *body)
2120 {
2121     static WCHAR sRed[] = {'r','e','d',0};
2122     static WCHAR sRedbg[] = {'#','f','f','0','0','0','0',0};
2123     VARIANT vbg;
2124     VARIANT vDefaultbg;
2125     HRESULT hres;
2126
2127     hres = IHTMLBodyElement_get_bgColor(body, &vDefaultbg);
2128     ok(hres == S_OK, "get_background failed: %08x\n", hres);
2129     ok(V_VT(&vDefaultbg) == VT_BSTR, "bstr != NULL\n");
2130
2131     V_VT(&vbg) = VT_BSTR;
2132     V_BSTR(&vbg) = SysAllocString(sRed);
2133     hres = IHTMLBodyElement_put_bgColor(body, vbg);
2134     ok(hres == S_OK, "get_background failed: %08x\n", hres);
2135     VariantClear(&vbg);
2136
2137     hres = IHTMLBodyElement_get_bgColor(body, &vbg);
2138     ok(hres == S_OK, "get_background failed: %08x\n", hres);
2139     ok(V_VT(&vDefaultbg) == VT_BSTR, "V_VT(&vDefaultbg) != VT_BSTR\n");
2140     ok(!lstrcmpW(V_BSTR(&vbg), sRedbg), "Unexpected type %s\n", dbgstr_w(V_BSTR(&vbg)));
2141     VariantClear(&vbg);
2142
2143     /* Restore Originial */
2144     hres = IHTMLBodyElement_put_bgColor(body, vDefaultbg);
2145     ok(hres == S_OK, "get_background failed: %08x\n", hres);
2146     VariantClear(&vDefaultbg);
2147 }
2148
2149 static void test_window(IHTMLDocument2 *doc)
2150 {
2151     IHTMLWindow2 *window, *window2, *self;
2152     IHTMLDocument2 *doc2 = NULL;
2153     IDispatch *disp;
2154     HRESULT hres;
2155
2156     hres = IHTMLDocument2_get_parentWindow(doc, &window);
2157     ok(hres == S_OK, "get_parentElement failed: %08x\n", hres);
2158     test_ifaces((IUnknown*)window, window_iids);
2159     test_disp((IUnknown*)window, &DIID_DispHTMLWindow2);
2160
2161     hres = IHTMLWindow2_get_document(window, &doc2);
2162     ok(hres == S_OK, "get_document failed: %08x\n", hres);
2163     ok(doc2 != NULL, "doc2 == NULL\n");
2164
2165     IHTMLDocument_Release(doc2);
2166
2167     hres = IHTMLWindow2_get_window(window, &window2);
2168     ok(hres == S_OK, "get_window failed: %08x\n", hres);
2169     ok(window2 != NULL, "window2 == NULL\n");
2170
2171     hres = IHTMLWindow2_get_self(window, &self);
2172     ok(hres == S_OK, "get_window failed: %08x\n", hres);
2173     ok(window2 != NULL, "self == NULL\n");
2174
2175     ok(self == window2, "self != window2\n");
2176
2177     IHTMLWindow2_Release(window2);
2178     IHTMLWindow2_Release(self);
2179
2180     disp = NULL;
2181     hres = IHTMLDocument2_get_Script(doc, &disp);
2182     ok(hres == S_OK, "get_Script failed: %08x\n", hres);
2183     ok(disp == (void*)window, "disp != window\n");
2184     IDispatch_Release(disp);
2185
2186     IHTMLWindow2_Release(window);
2187 }
2188
2189 static void test_defaults(IHTMLDocument2 *doc)
2190 {
2191     IHTMLStyleSheetsCollection *stylesheetcol;
2192     IHTMLBodyElement *body;
2193     IHTMLElement *elem;
2194     IHTMLStyle *style;
2195     long l;
2196     HRESULT hres;
2197
2198     hres = IHTMLDocument2_get_body(doc, &elem);
2199     ok(hres == S_OK, "get_body failed: %08x\n", hres);
2200
2201     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
2202     ok(hres == S_OK, "Could not get IHTMBodyElement: %08x\n", hres);
2203     test_default_body(body);
2204     test_body_funs(body);
2205     IHTMLBodyElement_Release(body);
2206
2207     hres = IHTMLElement_get_style(elem, &style);
2208     IHTMLElement_Release(elem);
2209     ok(hres == S_OK, "get_style failed: %08x\n", hres);
2210
2211     test_default_style(style);
2212     test_window(doc);
2213     test_compatmode(doc);
2214     test_location(doc);
2215     test_navigator(doc);
2216
2217     IHTMLStyle_Release(style);
2218
2219     hres = IHTMLDocument2_get_styleSheets(doc, &stylesheetcol);
2220     ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
2221
2222     l = 0xdeadbeef;
2223     hres = IHTMLStyleSheetsCollection_get_length(stylesheetcol, &l);
2224     ok(hres == S_OK, "get_length failed: %08x\n", hres);
2225     ok(l == 0, "length = %ld\n", l);
2226
2227     IHTMLStyleSheetsCollection_Release(stylesheetcol);
2228
2229     test_default_selection(doc);
2230     test_doc_title(doc, "");
2231 }
2232
2233 static void test_stylesheet(IDispatch *disp)
2234 {
2235     IHTMLStyleSheetRulesCollection *col = NULL;
2236     IHTMLStyleSheet *stylesheet;
2237     HRESULT hres;
2238
2239     hres = IDispatch_QueryInterface(disp, &IID_IHTMLStyleSheet, (void**)&stylesheet);
2240     ok(hres == S_OK, "Could not get IHTMLStyleSheet: %08x\n", hres);
2241
2242     hres = IHTMLStyleSheet_get_rules(stylesheet, &col);
2243     ok(hres == S_OK, "get_rules failed: %08x\n", hres);
2244     ok(col != NULL, "col == NULL\n");
2245
2246     IHTMLStyleSheetRulesCollection_Release(col);
2247     IHTMLStyleSheet_Release(stylesheet);
2248 }
2249
2250 static void test_stylesheets(IHTMLDocument2 *doc)
2251 {
2252     IHTMLStyleSheetsCollection *col = NULL;
2253     VARIANT idx, res;
2254     long len = 0;
2255     HRESULT hres;
2256
2257     hres = IHTMLDocument2_get_styleSheets(doc, &col);
2258     ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
2259     ok(col != NULL, "col == NULL\n");
2260
2261     hres = IHTMLStyleSheetsCollection_get_length(col, &len);
2262     ok(hres == S_OK, "get_length failed: %08x\n", hres);
2263     ok(len == 1, "len=%ld\n", len);
2264
2265     VariantInit(&res);
2266     V_VT(&idx) = VT_I4;
2267     V_I4(&idx) = 0;
2268
2269     hres = IHTMLStyleSheetsCollection_item(col, &idx, &res);
2270     ok(hres == S_OK, "item failed: %08x\n", hres);
2271     ok(V_VT(&res) == VT_DISPATCH, "V_VT(res) = %d\n", V_VT(&res));
2272     ok(V_DISPATCH(&res) != NULL, "V_DISPATCH(&res) == NULL\n");
2273     test_stylesheet(V_DISPATCH(&res));
2274     VariantClear(&res);
2275
2276     V_VT(&res) = VT_I4;
2277     V_VT(&idx) = VT_I4;
2278     V_I4(&idx) = 1;
2279
2280     hres = IHTMLStyleSheetsCollection_item(col, &idx, &res);
2281     ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
2282     ok(V_VT(&res) == VT_EMPTY, "V_VT(res) = %d\n", V_VT(&res));
2283     ok(V_DISPATCH(&res) != NULL, "V_DISPATCH(&res) == NULL\n");
2284     VariantClear(&res);
2285
2286     IHTMLStyleSheetsCollection_Release(col);
2287 }
2288
2289 static void test_child_col_disp(IHTMLDOMChildrenCollection *col)
2290 {
2291     IDispatchEx *dispex;
2292     IHTMLDOMNode *node;
2293     DISPPARAMS dp = {NULL, NULL, 0, 0};
2294     VARIANT var;
2295     EXCEPINFO ei;
2296     long type;
2297     DISPID id;
2298     BSTR bstr;
2299     HRESULT hres;
2300
2301     static const WCHAR w0[] = {'0',0};
2302     static const WCHAR w100[] = {'1','0','0',0};
2303
2304     hres = IHTMLDOMChildrenCollection_QueryInterface(col, &IID_IDispatchEx, (void**)&dispex);
2305     ok(hres == S_OK, "Could not get IDispatchEx: %08x\n", hres);
2306
2307     bstr = SysAllocString(w0);
2308     hres = IDispatchEx_GetDispID(dispex, bstr, fdexNameCaseSensitive, &id);
2309     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
2310     SysFreeString(bstr);
2311
2312     VariantInit(&var);
2313     hres = IDispatchEx_InvokeEx(dispex, id, LOCALE_NEUTRAL, INVOKE_PROPERTYGET, &dp, &var, &ei, NULL);
2314     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
2315     ok(V_VT(&var) == VT_DISPATCH, "V_VT(var)=%d\n", V_VT(&var));
2316     ok(V_DISPATCH(&var) != NULL, "V_DISPATCH(var) == NULL\n");
2317     node = get_node_iface((IUnknown*)V_DISPATCH(&var));
2318     type = get_node_type((IUnknown*)node);
2319     ok(type == 3, "type=%ld\n", type);
2320     IHTMLDOMNode_Release(node);
2321     VariantClear(&var);
2322
2323     bstr = SysAllocString(w100);
2324     hres = IDispatchEx_GetDispID(dispex, bstr, fdexNameCaseSensitive, &id);
2325     ok(hres == DISP_E_UNKNOWNNAME, "GetDispID failed: %08x, expected DISP_E_UNKNOWNNAME\n", hres);
2326     SysFreeString(bstr);
2327
2328     IDispatchEx_Release(dispex);
2329 }
2330
2331
2332
2333 static void test_elems(IHTMLDocument2 *doc)
2334 {
2335     IHTMLElementCollection *col;
2336     IHTMLDOMChildrenCollection *child_col;
2337     IHTMLElement *elem, *elem2, *elem3;
2338     IHTMLDOMNode *node, *node2;
2339     IDispatch *disp;
2340     long type;
2341     HRESULT hres;
2342
2343     static const WCHAR imgidW[] = {'i','m','g','i','d',0};
2344     static const WCHAR inW[] = {'i','n',0};
2345     static const WCHAR xW[] = {'x',0};
2346     static const WCHAR sW[] = {'s',0};
2347     static const WCHAR scW[] = {'s','c',0};
2348     static const WCHAR xxxW[] = {'x','x','x',0};
2349
2350     static const elem_type_t all_types[] = {
2351         ET_HTML,
2352         ET_HEAD,
2353         ET_TITLE,
2354         ET_STYLE,
2355         ET_BODY,
2356         ET_COMMENT,
2357         ET_A,
2358         ET_INPUT,
2359         ET_SELECT,
2360         ET_OPTION,
2361         ET_OPTION,
2362         ET_TEXTAREA,
2363         ET_TABLE,
2364         ET_TBODY,
2365         ET_SCRIPT,
2366         ET_TEST,
2367         ET_IMG
2368     };
2369
2370     static const elem_type_t item_types[] = {
2371         ET_A,
2372         ET_OPTION,
2373         ET_TEXTAREA
2374     };
2375
2376     hres = IHTMLDocument2_get_all(doc, &col);
2377     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2378     test_elem_collection((IUnknown*)col, all_types, sizeof(all_types)/sizeof(all_types[0]));
2379     test_elem_col_item(col, xW, item_types, sizeof(item_types)/sizeof(item_types[0]));
2380     IHTMLElementCollection_Release(col);
2381
2382     elem = get_doc_elem(doc);
2383     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
2384     hres = IHTMLElement_get_all(elem, &disp);
2385     IHTMLElement_Release(elem);
2386     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2387
2388     hres = IDispatch_QueryInterface(disp, &IID_IHTMLElementCollection, (void**)&col);
2389     IDispatch_Release(disp);
2390     ok(hres == S_OK, "Could not get IHTMLElementCollection: %08x\n", hres);
2391     test_elem_collection((IUnknown*)col, all_types+1, sizeof(all_types)/sizeof(all_types[0])-1);
2392     IHTMLElementCollection_Release(col);
2393
2394     get_elem_by_id(doc, xxxW, FALSE);
2395     elem = get_doc_elem_by_id(doc, xxxW);
2396     ok(!elem, "elem != NULL\n");
2397
2398     elem = get_doc_elem_by_id(doc, sW);
2399     ok(elem != NULL, "elem == NULL\n");
2400     if(elem) {
2401         test_elem_type((IUnknown*)elem, ET_SELECT);
2402         test_elem_attr(elem, xxxW, NULL);
2403         test_elem_attr(elem, idW, sW);
2404         test_elem_class((IUnknown*)elem, NULL);
2405         test_elem_set_class((IUnknown*)elem, "cl");
2406         test_elem_set_class((IUnknown*)elem, NULL);
2407         test_elem_tabindex((IUnknown*)elem, 0);
2408         test_elem_set_tabindex((IUnknown*)elem, 1);
2409
2410         node = test_node_get_parent((IUnknown*)elem);
2411         ok(node != NULL, "node == NULL\n");
2412         test_node_name((IUnknown*)node, "BODY");
2413         node2 = test_node_get_parent((IUnknown*)node);
2414         IHTMLDOMNode_Release(node);
2415         ok(node2 != NULL, "node == NULL\n");
2416         test_node_name((IUnknown*)node2, "HTML");
2417         node = test_node_get_parent((IUnknown*)node2);
2418         IHTMLDOMNode_Release(node2);
2419         ok(node != NULL, "node == NULL\n");
2420         test_node_name((IUnknown*)node, "#document");
2421         type = get_node_type((IUnknown*)node);
2422         ok(type == 9, "type=%ld, expected 9\n", type);
2423         node2 = test_node_get_parent((IUnknown*)node);
2424         IHTMLDOMNode_Release(node);
2425         ok(node2 == NULL, "node != NULL\n");
2426
2427         elem2 = test_elem_get_parent((IUnknown*)elem);
2428         ok(elem2 != NULL, "elem2 == NULL\n");
2429         test_node_name((IUnknown*)elem2, "BODY");
2430         elem3 = test_elem_get_parent((IUnknown*)elem2);
2431         IHTMLElement_Release(elem2);
2432         ok(elem3 != NULL, "elem3 == NULL\n");
2433         test_node_name((IUnknown*)elem3, "HTML");
2434         elem2 = test_elem_get_parent((IUnknown*)elem3);
2435         IHTMLElement_Release(elem3);
2436         ok(elem2 == NULL, "elem2 != NULL\n");
2437
2438         test_elem_getelembytag((IUnknown*)elem, ET_OPTION, 2);
2439         test_elem_getelembytag((IUnknown*)elem, ET_SELECT, 0);
2440         test_elem_getelembytag((IUnknown*)elem, ET_HTML, 0);
2441
2442         IHTMLElement_Release(elem);
2443     }
2444
2445     elem = get_elem_by_id(doc, sW, TRUE);
2446     if(elem) {
2447         IHTMLSelectElement *select;
2448
2449         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLSelectElement, (void**)&select);
2450         ok(hres == S_OK, "Could not get IHTMLSelectElement interface: %08x\n", hres);
2451
2452         test_select_elem(select);
2453
2454         test_elem_title((IUnknown*)select, NULL);
2455         test_elem_set_title((IUnknown*)select, "Title");
2456         test_elem_title((IUnknown*)select, "Title");
2457         test_elem_offset((IUnknown*)select);
2458
2459         node = get_first_child((IUnknown*)select);
2460         ok(node != NULL, "node == NULL\n");
2461         if(node) {
2462             test_elem_type((IUnknown*)node, ET_OPTION);
2463             IHTMLDOMNode_Release(node);
2464         }
2465
2466         type = get_node_type((IUnknown*)select);
2467         ok(type == 1, "type=%ld\n", type);
2468
2469         IHTMLSelectElement_Release(select);
2470         IHTMLElement_Release(elem);
2471     }
2472
2473     elem = get_elem_by_id(doc, scW, TRUE);
2474     if(elem) {
2475         IHTMLScriptElement *script;
2476         BSTR type;
2477
2478         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLScriptElement, (void**)&script);
2479         ok(hres == S_OK, "Could not get IHTMLScriptElement interface: %08x\n", hres);
2480
2481         hres = IHTMLScriptElement_get_type(script, &type);
2482         ok(hres == S_OK, "get_type failed: %08x\n", hres);
2483         ok(!lstrcmpW(type, text_javascriptW), "Unexpected type %s\n", dbgstr_w(type));
2484         SysFreeString(type);
2485
2486         IHTMLScriptElement_Release(script);
2487     }
2488
2489     elem = get_elem_by_id(doc, inW, TRUE);
2490     if(elem) {
2491         IHTMLInputElement *input;
2492
2493         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLInputElement, (void**)&input);
2494         ok(hres == S_OK, "Could not get IHTMLInputElement: %08x\n", hres);
2495
2496         test_elem_id((IUnknown*)elem, "in");
2497         test_elem_put_id((IUnknown*)elem, "newin");
2498         test_input_get_disabled(input, VARIANT_FALSE);
2499         test_input_set_disabled(input, VARIANT_TRUE);
2500         test_input_set_disabled(input, VARIANT_FALSE);
2501         test_elem_client_size((IUnknown*)elem);
2502
2503         test_node_get_value_str((IUnknown*)elem, NULL);
2504         test_node_put_value_str((IUnknown*)elem, "test");
2505         test_node_get_value_str((IUnknown*)elem, NULL);
2506         test_input_value((IUnknown*)elem, NULL);
2507         test_input_put_value((IUnknown*)elem, "test");
2508         test_input_value((IUnknown*)elem, NULL);
2509         test_elem_class((IUnknown*)elem, "testclass");
2510         test_elem_tabindex((IUnknown*)elem, 2);
2511         test_elem_set_tabindex((IUnknown*)elem, 3);
2512         test_elem_title((IUnknown*)elem, "test title");
2513
2514         IHTMLInputElement_Release(input);
2515         IHTMLElement_Release(elem);
2516     }
2517
2518     elem = get_elem_by_id(doc, imgidW, TRUE);
2519     if(elem) {
2520         test_img_set_src((IUnknown*)elem, "about:blank");
2521         test_img_alt((IUnknown*)elem, NULL);
2522         test_img_set_alt((IUnknown*)elem, "alt test");
2523         IHTMLElement_Release(elem);
2524     }
2525
2526     hres = IHTMLDocument2_get_body(doc, &elem);
2527     ok(hres == S_OK, "get_body failed: %08x\n", hres);
2528
2529     node = get_first_child((IUnknown*)elem);
2530     ok(node != NULL, "node == NULL\n");
2531     if(node) {
2532         test_ifaces((IUnknown*)node, text_iids);
2533         test_disp((IUnknown*)node, &DIID_DispHTMLDOMTextNode);
2534
2535         node2 = get_first_child((IUnknown*)node);
2536         ok(!node2, "node2 != NULL\n");
2537
2538         type = get_node_type((IUnknown*)node);
2539         ok(type == 3, "type=%ld\n", type);
2540
2541         test_node_get_value_str((IUnknown*)node, "text test");
2542         test_node_put_value_str((IUnknown*)elem, "test text");
2543         test_node_get_value_str((IUnknown*)node, "text test");
2544
2545         IHTMLDOMNode_Release(node);
2546     }
2547
2548     child_col = get_child_nodes((IUnknown*)elem);
2549     ok(child_col != NULL, "child_coll == NULL\n");
2550     if(child_col) {
2551         long length = 0;
2552
2553         test_disp((IUnknown*)child_col, &DIID_DispDOMChildrenCollection);
2554
2555         hres = IHTMLDOMChildrenCollection_get_length(child_col, &length);
2556         ok(hres == S_OK, "get_length failed: %08x\n", hres);
2557         ok(length, "length=0\n");
2558
2559         node = get_child_item(child_col, 0);
2560         ok(node != NULL, "node == NULL\n");
2561         if(node) {
2562             type = get_node_type((IUnknown*)node);
2563             ok(type == 3, "type=%ld\n", type);
2564             IHTMLDOMNode_Release(node);
2565         }
2566
2567         node = get_child_item(child_col, 1);
2568         ok(node != NULL, "node == NULL\n");
2569         if(node) {
2570             type = get_node_type((IUnknown*)node);
2571             ok(type == 8, "type=%ld\n", type);
2572
2573             test_elem_id((IUnknown*)node, NULL);
2574             IHTMLDOMNode_Release(node);
2575         }
2576
2577         disp = (void*)0xdeadbeef;
2578         hres = IHTMLDOMChildrenCollection_item(child_col, 6000, &disp);
2579         ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
2580         ok(disp == (void*)0xdeadbeef, "disp=%p\n", disp);
2581
2582         disp = (void*)0xdeadbeef;
2583         hres = IHTMLDOMChildrenCollection_item(child_col, length, &disp);
2584         ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
2585         ok(disp == (void*)0xdeadbeef, "disp=%p\n", disp);
2586
2587         test_child_col_disp(child_col);
2588
2589         IHTMLDOMChildrenCollection_Release(child_col);
2590     }
2591
2592     IHTMLElement_Release(elem);
2593
2594     test_stylesheets(doc);
2595     test_create_option_elem(doc);
2596
2597     test_doc_title(doc, "test");
2598     test_doc_set_title(doc, "test title");
2599     test_doc_title(doc, "test title");
2600 }
2601
2602 static void test_create_elems(IHTMLDocument2 *doc)
2603 {
2604     IHTMLElement *elem, *body, *elem2;
2605     IHTMLDOMNode *node;
2606     IDispatch *disp;
2607     long type;
2608     HRESULT hres;
2609
2610     static const elem_type_t types1[] = { ET_TESTG };
2611
2612     elem = test_create_elem(doc, "TEST");
2613     test_elem_tag((IUnknown*)elem, "TEST");
2614     type = get_node_type((IUnknown*)elem);
2615     ok(type == 1, "type=%ld\n", type);
2616     test_ifaces((IUnknown*)elem, elem_iids);
2617     test_disp((IUnknown*)elem, &DIID_DispHTMLGenericElement);
2618
2619     hres = IHTMLDocument2_get_body(doc, &body);
2620     ok(hres == S_OK, "get_body failed: %08x\n", hres);
2621     test_node_has_child((IUnknown*)body, VARIANT_FALSE);
2622
2623     node = test_node_append_child((IUnknown*)body, (IUnknown*)elem);
2624     test_node_has_child((IUnknown*)body, VARIANT_TRUE);
2625     elem2 = get_elem_iface((IUnknown*)node);
2626     IHTMLElement_Release(elem2);
2627
2628     hres = IHTMLElement_get_all(body, &disp);
2629     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2630     test_elem_collection((IUnknown*)disp, types1, sizeof(types1)/sizeof(types1[0]));
2631     IDispatch_Release(disp);
2632
2633     test_node_remove_child((IUnknown*)body, node);
2634
2635     hres = IHTMLElement_get_all(body, &disp);
2636     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2637     test_elem_collection((IUnknown*)disp, NULL, 0);
2638     IDispatch_Release(disp);
2639     test_node_has_child((IUnknown*)body, VARIANT_FALSE);
2640
2641     IHTMLElement_Release(body);
2642     IHTMLElement_Release(elem);
2643     IHTMLDOMNode_Release(node);
2644
2645     node = test_create_text(doc, "test");
2646     test_ifaces((IUnknown*)node, text_iids);
2647     test_disp((IUnknown*)node, &DIID_DispHTMLDOMTextNode);
2648     IHTMLDOMNode_Release(node);
2649 }
2650
2651 static void test_exec(IUnknown *unk, const GUID *grpid, DWORD cmdid, VARIANT *in, VARIANT *out)
2652 {
2653     IOleCommandTarget *cmdtrg;
2654     HRESULT hres;
2655
2656     hres = IHTMLTxtRange_QueryInterface(unk, &IID_IOleCommandTarget, (void**)&cmdtrg);
2657     ok(hres == S_OK, "Could not get IOleCommandTarget interface: %08x\n", hres);
2658
2659     hres = IOleCommandTarget_Exec(cmdtrg, grpid, cmdid, 0, in, out);
2660     ok(hres == S_OK, "Exec failed: %08x\n", hres);
2661
2662     IOleCommandTarget_Release(cmdtrg);
2663 }
2664
2665 static void test_indent(IHTMLDocument2 *doc)
2666 {
2667     IHTMLElementCollection *col;
2668     IHTMLTxtRange *range;
2669     HRESULT hres;
2670
2671     static const elem_type_t all_types[] = {
2672         ET_HTML,
2673         ET_HEAD,
2674         ET_TITLE,
2675         ET_BODY,
2676         ET_BR,
2677         ET_A,
2678     };
2679
2680     static const elem_type_t indent_types[] = {
2681         ET_HTML,
2682         ET_HEAD,
2683         ET_TITLE,
2684         ET_BODY,
2685         ET_BLOCKQUOTE,
2686         ET_P,
2687         ET_BR,
2688         ET_A,
2689     };
2690
2691     hres = IHTMLDocument2_get_all(doc, &col);
2692     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2693     test_elem_collection((IUnknown*)col, all_types, sizeof(all_types)/sizeof(all_types[0]));
2694     IHTMLElementCollection_Release(col);
2695
2696     range = test_create_body_range(doc);
2697     test_exec((IUnknown*)range, &CGID_MSHTML, IDM_INDENT, NULL, NULL);
2698     IHTMLTxtRange_Release(range);
2699
2700     hres = IHTMLDocument2_get_all(doc, &col);
2701     ok(hres == S_OK, "get_all failed: %08x\n", hres);
2702     test_elem_collection((IUnknown*)col, indent_types, sizeof(indent_types)/sizeof(indent_types[0]));
2703     IHTMLElementCollection_Release(col);
2704 }
2705
2706 static IHTMLDocument2 *notif_doc;
2707 static BOOL doc_complete;
2708
2709 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
2710         REFIID riid, void**ppv)
2711 {
2712     if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
2713         *ppv = iface;
2714         return S_OK;
2715     }
2716
2717     ok(0, "unexpected call\n");
2718     return E_NOINTERFACE;
2719 }
2720
2721 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
2722 {
2723     return 2;
2724 }
2725
2726 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
2727 {
2728     return 1;
2729 }
2730
2731 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
2732 {
2733     if(dispID == DISPID_READYSTATE){
2734         BSTR state;
2735         HRESULT hres;
2736
2737         static const WCHAR completeW[] = {'c','o','m','p','l','e','t','e',0};
2738
2739         hres = IHTMLDocument2_get_readyState(notif_doc, &state);
2740         ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
2741
2742         if(!lstrcmpW(state, completeW))
2743             doc_complete = TRUE;
2744
2745         SysFreeString(state);
2746     }
2747
2748     return S_OK;
2749 }
2750
2751 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
2752 {
2753     ok(0, "unexpected call\n");
2754     return E_NOTIMPL;
2755 }
2756
2757 static IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
2758     PropertyNotifySink_QueryInterface,
2759     PropertyNotifySink_AddRef,
2760     PropertyNotifySink_Release,
2761     PropertyNotifySink_OnChanged,
2762     PropertyNotifySink_OnRequestEdit
2763 };
2764
2765 static IPropertyNotifySink PropertyNotifySink = { &PropertyNotifySinkVtbl };
2766
2767 static IHTMLDocument2 *create_doc_with_string(const char *str)
2768 {
2769     IPersistStreamInit *init;
2770     IStream *stream;
2771     IHTMLDocument2 *doc;
2772     HGLOBAL mem;
2773     SIZE_T len;
2774
2775     notif_doc = doc = create_document();
2776     if(!doc)
2777         return NULL;
2778
2779     doc_complete = FALSE;
2780     len = strlen(str);
2781     mem = GlobalAlloc(0, len);
2782     memcpy(mem, str, len);
2783     CreateStreamOnHGlobal(mem, TRUE, &stream);
2784
2785     IHTMLDocument2_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&init);
2786
2787     IPersistStreamInit_Load(init, stream);
2788     IPersistStreamInit_Release(init);
2789     IStream_Release(stream);
2790
2791     return doc;
2792 }
2793
2794 static void do_advise(IUnknown *unk, REFIID riid, IUnknown *unk_advise)
2795 {
2796     IConnectionPointContainer *container;
2797     IConnectionPoint *cp;
2798     DWORD cookie;
2799     HRESULT hres;
2800
2801     hres = IUnknown_QueryInterface(unk, &IID_IConnectionPointContainer, (void**)&container);
2802     ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
2803
2804     hres = IConnectionPointContainer_FindConnectionPoint(container, riid, &cp);
2805     IConnectionPointContainer_Release(container);
2806     ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
2807
2808     hres = IConnectionPoint_Advise(cp, unk_advise, &cookie);
2809     IConnectionPoint_Release(cp);
2810     ok(hres == S_OK, "Advise failed: %08x\n", hres);
2811 }
2812
2813 typedef void (*domtest_t)(IHTMLDocument2*);
2814
2815 static void run_domtest(const char *str, domtest_t test)
2816 {
2817     IHTMLDocument2 *doc;
2818     IHTMLElement *body = NULL;
2819     ULONG ref;
2820     MSG msg;
2821     HRESULT hres;
2822
2823     doc = create_doc_with_string(str);
2824     do_advise((IUnknown*)doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
2825
2826     while(!doc_complete && GetMessage(&msg, NULL, 0, 0)) {
2827         TranslateMessage(&msg);
2828         DispatchMessage(&msg);
2829     }
2830
2831     hres = IHTMLDocument2_get_body(doc, &body);
2832     ok(hres == S_OK, "get_body failed: %08x\n", hres);
2833
2834     if(body) {
2835         IHTMLElement_Release(body);
2836         test(doc);
2837     }else {
2838         skip("Could not get document body. Assuming no Gecko installed.\n");
2839     }
2840
2841     ref = IHTMLDocument2_Release(doc);
2842     ok(!ref ||
2843        ref == 1, /* Vista */
2844        "ref = %d\n", ref);
2845 }
2846
2847 static void gecko_installer_workaround(BOOL disable)
2848 {
2849     HKEY hkey;
2850     DWORD res;
2851
2852     static BOOL has_url = FALSE;
2853     static char url[2048];
2854
2855     if(!disable && !has_url)
2856         return;
2857
2858     res = RegOpenKey(HKEY_CURRENT_USER, "Software\\Wine\\MSHTML", &hkey);
2859     if(res != ERROR_SUCCESS)
2860         return;
2861
2862     if(disable) {
2863         DWORD type, size = sizeof(url);
2864
2865         res = RegQueryValueEx(hkey, "GeckoUrl", NULL, &type, (PVOID)url, &size);
2866         if(res == ERROR_SUCCESS && type == REG_SZ)
2867             has_url = TRUE;
2868
2869         RegDeleteValue(hkey, "GeckoUrl");
2870     }else {
2871         RegSetValueEx(hkey, "GeckoUrl", 0, REG_SZ, (PVOID)url, lstrlenA(url)+1);
2872     }
2873
2874     RegCloseKey(hkey);
2875 }
2876
2877 START_TEST(dom)
2878 {
2879     gecko_installer_workaround(TRUE);
2880     CoInitialize(NULL);
2881
2882     run_domtest(doc_str1, test_doc_elem);
2883     run_domtest(range_test_str, test_txtrange);
2884     run_domtest(range_test2_str, test_txtrange2);
2885     run_domtest(elem_test_str, test_elems);
2886     run_domtest(doc_blank, test_create_elems);
2887     run_domtest(doc_blank, test_defaults);
2888     run_domtest(indent_test_str, test_indent);
2889
2890     CoUninitialize();
2891     gecko_installer_workaround(FALSE);
2892 }