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