comdlg32: Use unicode APIs to react to the change printer combo.
[wine] / dlls / mshtml / tests / dom.c
1 /*
2  * Copyright 2007-2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #define COBJMACROS
20 #define CONST_VTABLE
21
22 #include <wine/test.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "ole2.h"
29 #include "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 onload=\"Testing()\">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\"><td>td1 text</td><td>td2 text</td></tr></tbody></table>"
49     "<script id=\"sc\" type=\"text/javascript\"><!--\nfunction Testing() {}\n// -->\n</script>"
50     "<test />"
51     "<img id=\"imgid\"/>"
52     "<iframe src=\"about:blank\" id=\"ifr\"></iframe>"
53     "</body></html>";
54 static const char indent_test_str[] =
55     "<html><head><title>test</title></head><body>abc<br /><a href=\"about:blank\">123</a></body></html>";
56 static const char cond_comment_str[] =
57     "<html><head><title>test</title></head><body>"
58     "<!--[if gte IE 4]> <br> <![endif]-->"
59     "</body></html>";
60
61 static const WCHAR noneW[] = {'N','o','n','e',0};
62
63 static WCHAR characterW[] = {'c','h','a','r','a','c','t','e','r',0};
64 static WCHAR texteditW[] = {'t','e','x','t','e','d','i','t',0};
65 static WCHAR wordW[] = {'w','o','r','d',0};
66
67 static const WCHAR text_javascriptW[] = {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
68
69 static const WCHAR idW[] = {'i','d',0};
70
71 typedef enum {
72     ET_NONE,
73     ET_HTML,
74     ET_HEAD,
75     ET_TITLE,
76     ET_BODY,
77     ET_A,
78     ET_INPUT,
79     ET_SELECT,
80     ET_TEXTAREA,
81     ET_OPTION,
82     ET_STYLE,
83     ET_BLOCKQUOTE,
84     ET_P,
85     ET_BR,
86     ET_TABLE,
87     ET_TBODY,
88     ET_SCRIPT,
89     ET_TEST,
90     ET_TESTG,
91     ET_COMMENT,
92     ET_IMG,
93     ET_TR,
94     ET_TD,
95     ET_IFRAME
96 } elem_type_t;
97
98 static const IID * const none_iids[] = {
99     &IID_IUnknown,
100     NULL
101 };
102
103 static const IID * const elem_iids[] = {
104     &IID_IHTMLDOMNode,
105     &IID_IHTMLDOMNode2,
106     &IID_IHTMLElement,
107     &IID_IHTMLElement2,
108     &IID_IHTMLElement3,
109     &IID_IDispatchEx,
110     &IID_IConnectionPointContainer,
111     NULL
112 };
113
114 static const IID * const body_iids[] = {
115     &IID_IHTMLDOMNode,
116     &IID_IHTMLDOMNode2,
117     &IID_IHTMLElement,
118     &IID_IHTMLElement2,
119     &IID_IHTMLElement3,
120     &IID_IHTMLTextContainer,
121     &IID_IHTMLBodyElement,
122     &IID_IDispatchEx,
123     &IID_IConnectionPointContainer,
124     NULL
125 };
126
127 static const IID * const anchor_iids[] = {
128     &IID_IHTMLDOMNode,
129     &IID_IHTMLDOMNode2,
130     &IID_IHTMLElement,
131     &IID_IHTMLElement2,
132     &IID_IHTMLElement3,
133     &IID_IHTMLAnchorElement,
134     &IID_IDispatchEx,
135     &IID_IConnectionPointContainer,
136     NULL
137 };
138
139 static const IID * const input_iids[] = {
140     &IID_IHTMLDOMNode,
141     &IID_IHTMLDOMNode2,
142     &IID_IHTMLElement,
143     &IID_IHTMLElement2,
144     &IID_IHTMLElement3,
145     &IID_IHTMLInputElement,
146     &IID_IHTMLInputTextElement,
147     &IID_IDispatchEx,
148     &IID_IConnectionPointContainer,
149     NULL
150 };
151
152 static const IID * const select_iids[] = {
153     &IID_IHTMLDOMNode,
154     &IID_IHTMLDOMNode2,
155     &IID_IHTMLElement,
156     &IID_IHTMLElement2,
157     &IID_IHTMLElement3,
158     &IID_IHTMLSelectElement,
159     &IID_IDispatchEx,
160     &IID_IConnectionPointContainer,
161     NULL
162 };
163
164 static const IID * const textarea_iids[] = {
165     &IID_IHTMLDOMNode,
166     &IID_IHTMLDOMNode2,
167     &IID_IHTMLElement,
168     &IID_IHTMLElement2,
169     &IID_IHTMLElement3,
170     &IID_IHTMLTextAreaElement,
171     &IID_IDispatchEx,
172     &IID_IConnectionPointContainer,
173     NULL
174 };
175
176 static const IID * const option_iids[] = {
177     &IID_IHTMLDOMNode,
178     &IID_IHTMLDOMNode2,
179     &IID_IHTMLElement,
180     &IID_IHTMLElement2,
181     &IID_IHTMLElement3,
182     &IID_IHTMLOptionElement,
183     &IID_IDispatchEx,
184     &IID_IConnectionPointContainer,
185     NULL
186 };
187
188 static const IID * const table_iids[] = {
189     &IID_IHTMLDOMNode,
190     &IID_IHTMLDOMNode2,
191     &IID_IHTMLElement,
192     &IID_IHTMLElement2,
193     &IID_IHTMLElement3,
194     &IID_IHTMLTable,
195     &IID_IDispatchEx,
196     &IID_IConnectionPointContainer,
197     NULL
198 };
199
200 static const IID * const script_iids[] = {
201     &IID_IHTMLDOMNode,
202     &IID_IHTMLDOMNode2,
203     &IID_IHTMLElement,
204     &IID_IHTMLElement2,
205     &IID_IHTMLElement3,
206     &IID_IHTMLScriptElement,
207     &IID_IDispatchEx,
208     &IID_IConnectionPointContainer,
209     NULL
210 };
211
212 static const IID * const text_iids[] = {
213     &IID_IHTMLDOMNode,
214     &IID_IHTMLDOMNode2,
215     &IID_IHTMLDOMTextNode,
216     NULL
217 };
218
219 static const IID * const location_iids[] = {
220     &IID_IDispatch,
221     &IID_IHTMLLocation,
222     NULL
223 };
224
225 static const IID * const window_iids[] = {
226     &IID_IDispatch,
227     &IID_IHTMLWindow2,
228     &IID_IHTMLWindow3,
229     &IID_IDispatchEx,
230     NULL
231 };
232
233 static const IID * const comment_iids[] = {
234     &IID_IHTMLDOMNode,
235     &IID_IHTMLDOMNode2,
236     &IID_IHTMLElement,
237     &IID_IHTMLElement2,
238     &IID_IHTMLElement3,
239     &IID_IHTMLCommentElement,
240     &IID_IDispatchEx,
241     &IID_IConnectionPointContainer,
242     NULL
243 };
244
245 static const IID * const img_iids[] = {
246     &IID_IHTMLDOMNode,
247     &IID_IHTMLDOMNode2,
248     &IID_IHTMLElement,
249     &IID_IHTMLElement2,
250     &IID_IHTMLElement3,
251     &IID_IDispatchEx,
252     &IID_IHTMLImgElement,
253     &IID_IConnectionPointContainer,
254     NULL
255 };
256
257 static const IID * const tr_iids[] = {
258     &IID_IHTMLDOMNode,
259     &IID_IHTMLDOMNode2,
260     &IID_IHTMLElement,
261     &IID_IHTMLElement2,
262     &IID_IHTMLElement3,
263     &IID_IDispatchEx,
264     &IID_IHTMLTableRow,
265     &IID_IConnectionPointContainer,
266     NULL
267 };
268
269 static const IID * const td_iids[] = {
270     &IID_IHTMLDOMNode,
271     &IID_IHTMLDOMNode2,
272     &IID_IHTMLElement,
273     &IID_IHTMLElement2,
274     &IID_IHTMLElement3,
275     &IID_IDispatchEx,
276     &IID_IConnectionPointContainer,
277     NULL
278 };
279
280 static const IID * const iframe_iids[] = {
281     &IID_IHTMLDOMNode,
282     &IID_IHTMLDOMNode2,
283     &IID_IHTMLElement,
284     &IID_IHTMLElement2,
285     &IID_IHTMLElement3,
286     &IID_IHTMLFrameBase2,
287     &IID_IDispatchEx,
288     &IID_IConnectionPointContainer,
289     NULL
290 };
291
292 static const IID * const generic_iids[] = {
293     &IID_IHTMLDOMNode,
294     &IID_IHTMLDOMNode2,
295     &IID_IHTMLElement,
296     &IID_IHTMLElement2,
297     &IID_IHTMLElement3,
298     &IID_IHTMLGenericElement,
299     &IID_IDispatchEx,
300     &IID_IConnectionPointContainer,
301     NULL
302 };
303
304 static const IID * const style_iids[] = {
305     &IID_IUnknown,
306     &IID_IDispatch,
307     &IID_IDispatchEx,
308     &IID_IHTMLStyle,
309     &IID_IHTMLStyle2,
310     NULL
311 };
312
313 static const IID * const cstyle_iids[] = {
314     &IID_IUnknown,
315     &IID_IDispatch,
316     &IID_IDispatchEx,
317     &IID_IHTMLCurrentStyle,
318     NULL
319 };
320
321 typedef struct {
322     const char *tag;
323     REFIID *iids;
324     const IID *dispiid;
325 } elem_type_info_t;
326
327 static const elem_type_info_t elem_type_infos[] = {
328     {"",          none_iids,        NULL},
329     {"HTML",      elem_iids,        NULL},
330     {"HEAD",      elem_iids,        NULL},
331     {"TITLE",     elem_iids,        NULL},
332     {"BODY",      body_iids,        &DIID_DispHTMLBody},
333     {"A",         anchor_iids,      NULL},
334     {"INPUT",     input_iids,       &DIID_DispHTMLInputElement},
335     {"SELECT",    select_iids,      &DIID_DispHTMLSelectElement},
336     {"TEXTAREA",  textarea_iids,    NULL},
337     {"OPTION",    option_iids,      &DIID_DispHTMLOptionElement},
338     {"STYLE",     elem_iids,        NULL},
339     {"BLOCKQUOTE",elem_iids,        NULL},
340     {"P",         elem_iids,        NULL},
341     {"BR",        elem_iids,        NULL},
342     {"TABLE",     table_iids,       &DIID_DispHTMLTable},
343     {"TBODY",     elem_iids,        NULL},
344     {"SCRIPT",    script_iids,      NULL},
345     {"TEST",      elem_iids,        &DIID_DispHTMLUnknownElement},
346     {"TEST",      generic_iids,     &DIID_DispHTMLGenericElement},
347     {"!",         comment_iids,     &DIID_DispHTMLCommentElement},
348     {"IMG",       img_iids,         &DIID_DispHTMLImg},
349     {"TR",        tr_iids,          &DIID_DispHTMLTableRow},
350     {"TD",        td_iids,          NULL},
351     {"IFRAME",    iframe_iids,      &DIID_DispHTMLIFrame}
352 };
353
354 static const char *dbgstr_w(LPCWSTR str)
355 {
356     static char buf[512];
357     if(!str)
358         return "(null)";
359     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
360     return buf;
361 }
362
363 static const char *dbgstr_guid(REFIID riid)
364 {
365     static char buf[50];
366
367     sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
368             riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
369             riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
370             riid->Data4[5], riid->Data4[6], riid->Data4[7]);
371
372     return buf;
373 }
374
375 static int strcmp_wa(LPCWSTR strw, const char *stra)
376 {
377     WCHAR buf[512];
378     MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
379     return lstrcmpW(strw, buf);
380 }
381
382 static BSTR a2bstr(const char *str)
383 {
384     BSTR ret;
385     int len;
386
387     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
388     ret = SysAllocStringLen(NULL, len);
389     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
390
391     return ret;
392 }
393
394 static BOOL iface_cmp(IUnknown *iface1, IUnknown *iface2)
395 {
396     IUnknown *unk1, *unk2;
397
398     if(iface1 == iface2)
399         return TRUE;
400
401     IUnknown_QueryInterface(iface1, &IID_IUnknown, (void**)&unk1);
402     IUnknown_Release(unk1);
403     IUnknown_QueryInterface(iface1, &IID_IUnknown, (void**)&unk2);
404     IUnknown_Release(unk2);
405
406     return unk1 == unk2;
407 }
408
409 static IHTMLDocument2 *create_document(void)
410 {
411     IHTMLDocument2 *doc;
412     IHTMLDocument5 *doc5;
413     HRESULT hres;
414
415     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
416             &IID_IHTMLDocument2, (void**)&doc);
417     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
418
419     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument5, (void**)&doc5);
420     if(FAILED(hres)) {
421         win_skip("Could not get IHTMLDocument5, probably too old IE\n");
422         IHTMLDocument2_Release(doc);
423         return NULL;
424     }
425
426     IHTMLDocument5_Release(doc5);
427     return doc;
428 }
429
430 #define test_ifaces(i,ids) _test_ifaces(__LINE__,i,ids)
431 static void _test_ifaces(unsigned line, IUnknown *iface, REFIID *iids)
432 {
433     const IID * const *piid;
434     IUnknown *unk;
435     HRESULT hres;
436
437      for(piid = iids; *piid; piid++) {
438         hres = IDispatch_QueryInterface(iface, *piid, (void**)&unk);
439         ok_(__FILE__,line) (hres == S_OK, "Could not get %s interface: %08x\n", dbgstr_guid(*piid), hres);
440         if(SUCCEEDED(hres))
441             IUnknown_Release(unk);
442     }
443 }
444
445 #define test_disp(u,id) _test_disp(__LINE__,u,id)
446 static void _test_disp(unsigned line, IUnknown *unk, const IID *diid)
447 {
448     IDispatchEx *dispex;
449     ITypeInfo *typeinfo;
450     UINT ticnt;
451     HRESULT hres;
452
453     hres = IUnknown_QueryInterface(unk, &IID_IDispatchEx, (void**)&dispex);
454     ok_(__FILE__,line) (hres == S_OK, "Could not get IDispatch: %08x\n", hres);
455     if(FAILED(hres))
456         return;
457
458     ticnt = 0xdeadbeef;
459     hres = IDispatchEx_GetTypeInfoCount(dispex, &ticnt);
460     ok_(__FILE__,line) (hres == S_OK, "GetTypeInfoCount failed: %08x\n", hres);
461     ok_(__FILE__,line) (ticnt == 1, "ticnt=%u\n", ticnt);
462
463     hres = IDispatchEx_GetTypeInfo(dispex, 0, 0, &typeinfo);
464     ok_(__FILE__,line) (hres == S_OK, "GetTypeInfo failed: %08x\n", hres);
465
466     if(SUCCEEDED(hres)) {
467         TYPEATTR *type_attr;
468
469         hres = ITypeInfo_GetTypeAttr(typeinfo, &type_attr);
470         ok_(__FILE__,line) (hres == S_OK, "GetTypeAttr failed: %08x\n", hres);
471         ok_(__FILE__,line) (IsEqualGUID(&type_attr->guid, diid), "unexpected guid %s\n", dbgstr_guid(&type_attr->guid));
472
473         ITypeInfo_ReleaseTypeAttr(typeinfo, type_attr);
474         ITypeInfo_Release(typeinfo);
475     }
476
477     IDispatchEx_Release(dispex);
478 }
479
480 #define get_elem_iface(u) _get_elem_iface(__LINE__,u)
481 static IHTMLElement *_get_elem_iface(unsigned line, IUnknown *unk)
482 {
483     IHTMLElement *elem;
484     HRESULT hres;
485
486     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement, (void**)&elem);
487     ok_(__FILE__,line) (hres == S_OK, "Coule not get IHTMLElement: %08x\n", hres);
488     return elem;
489 }
490
491 #define get_elem2_iface(u) _get_elem2_iface(__LINE__,u)
492 static IHTMLElement2 *_get_elem2_iface(unsigned line, IUnknown *unk)
493 {
494     IHTMLElement2 *elem;
495     HRESULT hres;
496
497     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement2, (void**)&elem);
498     ok_(__FILE__,line) (hres == S_OK, "Coule not get IHTMLElement2: %08x\n", hres);
499     return elem;
500 }
501
502 #define get_elem3_iface(u) _get_elem3_iface(__LINE__,u)
503 static IHTMLElement3 *_get_elem3_iface(unsigned line, IUnknown *unk)
504 {
505     IHTMLElement3 *elem;
506     HRESULT hres;
507
508     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement3, (void**)&elem);
509     ok_(__FILE__,line) (hres == S_OK, "Coule not get IHTMLElement3: %08x\n", hres);
510     return elem;
511 }
512
513 #define get_node_iface(u) _get_node_iface(__LINE__,u)
514 static IHTMLDOMNode *_get_node_iface(unsigned line, IUnknown *unk)
515 {
516     IHTMLDOMNode *node;
517     HRESULT hres;
518
519     hres = IUnknown_QueryInterface(unk, &IID_IHTMLDOMNode, (void**)&node);
520     ok_(__FILE__,line) (hres == S_OK, "Coule not get IHTMLDOMNode: %08x\n", hres);
521     return node;
522 }
523
524 #define get_img_iface(u) _get_img_iface(__LINE__,u)
525 static IHTMLImgElement *_get_img_iface(unsigned line, IUnknown *unk)
526 {
527     IHTMLImgElement *img;
528     HRESULT hres;
529
530     hres = IUnknown_QueryInterface(unk, &IID_IHTMLImgElement, (void**)&img);
531     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLImgElement: %08x\n", hres);
532     return img;
533 }
534
535 #define test_node_name(u,n) _test_node_name(__LINE__,u,n)
536 static void _test_node_name(unsigned line, IUnknown *unk, const char *exname)
537 {
538     IHTMLDOMNode *node = _get_node_iface(line, unk);
539     BSTR name;
540     HRESULT hres;
541
542     hres = IHTMLDOMNode_get_nodeName(node, &name);
543     IHTMLDOMNode_Release(node);
544     ok_(__FILE__, line) (hres == S_OK, "get_nodeName failed: %08x\n", hres);
545     ok_(__FILE__, line) (!strcmp_wa(name, exname), "got name: %s, expected %s\n", dbgstr_w(name), exname);
546
547     SysFreeString(name);
548 }
549
550 #define test_elem_tag(u,n) _test_elem_tag(__LINE__,u,n)
551 static void _test_elem_tag(unsigned line, IUnknown *unk, const char *extag)
552 {
553     IHTMLElement *elem = _get_elem_iface(line, unk);
554     BSTR tag;
555     HRESULT hres;
556
557     hres = IHTMLElement_get_tagName(elem, &tag);
558     IHTMLElement_Release(elem);
559     ok_(__FILE__, line) (hres == S_OK, "get_tagName failed: %08x\n", hres);
560     ok_(__FILE__, line) (!strcmp_wa(tag, extag), "got tag: %s, expected %s\n", dbgstr_w(tag), extag);
561
562     SysFreeString(tag);
563 }
564
565 #define test_elem_type(ifc,t) _test_elem_type(__LINE__,ifc,t)
566 static void _test_elem_type(unsigned line, IUnknown *unk, elem_type_t type)
567 {
568     _test_elem_tag(line, unk, elem_type_infos[type].tag);
569     _test_ifaces(line, unk, elem_type_infos[type].iids);
570
571     if(elem_type_infos[type].dispiid)
572         _test_disp(line, unk, elem_type_infos[type].dispiid);
573 }
574
575 #define get_node_type(n) _get_node_type(__LINE__,n)
576 static long _get_node_type(unsigned line, IUnknown *unk)
577 {
578     IHTMLDOMNode *node = _get_node_iface(line, unk);
579     long type = -1;
580     HRESULT hres;
581
582     hres = IHTMLDOMNode_get_nodeType(node, &type);
583     ok(hres == S_OK, "get_nodeType failed: %08x\n", hres);
584
585     IHTMLDOMNode_Release(node);
586
587     return type;
588 }
589
590 #define get_child_nodes(u) _get_child_nodes(__LINE__,u)
591 static IHTMLDOMChildrenCollection *_get_child_nodes(unsigned line, IUnknown *unk)
592 {
593     IHTMLDOMNode *node = _get_node_iface(line, unk);
594     IHTMLDOMChildrenCollection *col = NULL;
595     IDispatch *disp;
596     HRESULT hres;
597
598     hres = IHTMLDOMNode_get_childNodes(node, &disp);
599     IHTMLDOMNode_Release(node);
600     ok_(__FILE__,line) (hres == S_OK, "get_childNodes failed: %08x\n", hres);
601     if(FAILED(hres))
602         return NULL;
603
604     hres = IDispatch_QueryInterface(disp, &IID_IHTMLDOMChildrenCollection, (void**)&col);
605     IDispatch_Release(disp);
606     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDOMChildrenCollection: %08x\n", hres);
607
608     return col;
609 }
610
611 #define get_child_item(c,i) _get_child_item(__LINE__,c,i)
612 static IHTMLDOMNode *_get_child_item(unsigned line, IHTMLDOMChildrenCollection *col, long idx)
613 {
614     IHTMLDOMNode *node = NULL;
615     IDispatch *disp;
616     HRESULT hres;
617
618     hres = IHTMLDOMChildrenCollection_item(col, idx, &disp);
619     ok(hres == S_OK, "item failed: %08x\n", hres);
620
621     node = _get_node_iface(line, (IUnknown*)disp);
622     IDispatch_Release(disp);
623
624     return node;
625 }
626
627 #define test_elem_attr(e,n,v) _test_elem_attr(__LINE__,e,n,v)
628 static void _test_elem_attr(unsigned line, IHTMLElement *elem, LPCWSTR name, LPCWSTR exval)
629 {
630     VARIANT value;
631     BSTR tmp;
632     HRESULT hres;
633
634     VariantInit(&value);
635
636     tmp = SysAllocString(name);
637     hres = IHTMLElement_getAttribute(elem, tmp, 0, &value);
638     SysFreeString(tmp);
639     ok_(__FILE__,line) (hres == S_OK, "getAttribute failed: %08x\n", hres);
640
641     if(exval) {
642         ok_(__FILE__,line) (V_VT(&value) == VT_BSTR, "vt=%d\n", V_VT(&value));
643         ok_(__FILE__,line) (!lstrcmpW(exval, V_BSTR(&value)), "unexpected value %s\n", dbgstr_w(V_BSTR(&value)));
644     }else {
645         ok_(__FILE__,line) (V_VT(&value) == VT_NULL, "vt=%d\n", V_VT(&value));
646     }
647
648     VariantClear(&value);
649 }
650
651 #define test_elem_offset(u) _test_elem_offset(__LINE__,u)
652 static void _test_elem_offset(unsigned line, IUnknown *unk)
653 {
654     IHTMLElement *elem = _get_elem_iface(line, unk);
655     long l;
656     HRESULT hres;
657
658     hres = IHTMLElement_get_offsetTop(elem, &l);
659     ok_(__FILE__,line) (hres == S_OK, "get_offsetTop failed: %08x\n", hres);
660
661     hres = IHTMLElement_get_offsetHeight(elem, &l);
662     ok_(__FILE__,line) (hres == S_OK, "get_offsetHeight failed: %08x\n", hres);
663
664     hres = IHTMLElement_get_offsetWidth(elem, &l);
665     ok_(__FILE__,line) (hres == S_OK, "get_offsetWidth failed: %08x\n", hres);
666
667     IHTMLElement_Release(elem);
668 }
669
670 static void test_doc_elem(IHTMLDocument2 *doc)
671 {
672     IHTMLElement *elem;
673     IHTMLDocument3 *doc3;
674     HRESULT hres;
675
676     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
677     ok(hres == S_OK, "QueryInterface(IID_IHTMLDocument3) failed: %08x\n", hres);
678
679     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
680     IHTMLDocument3_Release(doc3);
681     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
682
683     test_node_name((IUnknown*)elem, "HTML");
684     test_elem_tag((IUnknown*)elem, "HTML");
685
686     IHTMLElement_Release(elem);
687 }
688
689 #define get_doc_elem(d) _get_doc_elem(__LINE__,d)
690 static IHTMLElement *_get_doc_elem(unsigned line, IHTMLDocument2 *doc)
691 {
692     IHTMLElement *elem;
693     IHTMLDocument3 *doc3;
694     HRESULT hres;
695
696     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
697     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDocument3 interface: %08x\n", hres);
698     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
699     ok_(__FILE__,line) (hres == S_OK, "get_documentElement failed: %08x\n", hres);
700     IHTMLDocument3_Release(doc3);
701
702     return elem;
703 }
704
705 #define test_option_text(o,t) _test_option_text(__LINE__,o,t)
706 static void _test_option_text(unsigned line, IHTMLOptionElement *option, const char *text)
707 {
708     BSTR bstr;
709     HRESULT hres;
710
711     hres = IHTMLOptionElement_get_text(option, &bstr);
712     ok_(__FILE__,line) (hres == S_OK, "get_text failed: %08x\n", hres);
713     ok_(__FILE__,line) (!strcmp_wa(bstr, text), "text=%s\n", dbgstr_w(bstr));
714     SysFreeString(bstr);
715 }
716
717 #define test_option_put_text(o,t) _test_option_put_text(__LINE__,o,t)
718 static void _test_option_put_text(unsigned line, IHTMLOptionElement *option, const char *text)
719 {
720     BSTR bstr;
721     HRESULT hres;
722
723     bstr = a2bstr(text);
724     hres = IHTMLOptionElement_put_text(option, bstr);
725     SysFreeString(bstr);
726     ok(hres == S_OK, "put_text failed: %08x\n", hres);
727
728     _test_option_text(line, option, text);
729 }
730
731 #define test_option_value(o,t) _test_option_value(__LINE__,o,t)
732 static void _test_option_value(unsigned line, IHTMLOptionElement *option, const char *value)
733 {
734     BSTR bstr;
735     HRESULT hres;
736
737     hres = IHTMLOptionElement_get_value(option, &bstr);
738     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
739     ok_(__FILE__,line) (!strcmp_wa(bstr, value), "value=%s\n", dbgstr_w(bstr));
740     SysFreeString(bstr);
741 }
742
743 #define test_option_put_value(o,t) _test_option_put_value(__LINE__,o,t)
744 static void _test_option_put_value(unsigned line, IHTMLOptionElement *option, const char *value)
745 {
746     BSTR bstr;
747     HRESULT hres;
748
749     bstr = a2bstr(value);
750     hres = IHTMLOptionElement_put_value(option, bstr);
751     SysFreeString(bstr);
752     ok(hres == S_OK, "put_value failed: %08x\n", hres);
753
754     _test_option_value(line, option, value);
755 }
756
757 #define create_option_elem(d,t,v) _create_option_elem(__LINE__,d,t,v)
758 static IHTMLOptionElement *_create_option_elem(unsigned line, IHTMLDocument2 *doc,
759         const char *txt, const char *val)
760 {
761     IHTMLOptionElementFactory *factory;
762     IHTMLOptionElement *option;
763     IHTMLWindow2 *window;
764     VARIANT text, value, empty;
765     HRESULT hres;
766
767     hres = IHTMLDocument2_get_parentWindow(doc, &window);
768     ok_(__FILE__,line) (hres == S_OK, "get_parentElement failed: %08x\n", hres);
769
770     hres = IHTMLWindow2_get_Option(window, &factory);
771     IHTMLWindow2_Release(window);
772     ok_(__FILE__,line) (hres == S_OK, "get_Option failed: %08x\n", hres);
773
774     V_VT(&text) = VT_BSTR;
775     V_BSTR(&text) = a2bstr(txt);
776     V_VT(&value) = VT_BSTR;
777     V_BSTR(&value) = a2bstr(val);
778     V_VT(&empty) = VT_EMPTY;
779
780     hres = IHTMLOptionElementFactory_create(factory, text, value, empty, empty, &option);
781     ok_(__FILE__,line) (hres == S_OK, "create failed: %08x\n", hres);
782
783     IHTMLOptionElementFactory_Release(factory);
784     VariantClear(&text);
785     VariantClear(&value);
786
787     _test_option_text(line, option, txt);
788     _test_option_value(line, option, val);
789
790     return option;
791 }
792
793 #define test_select_length(s,l) _test_select_length(__LINE__,s,l)
794 static void _test_select_length(unsigned line, IHTMLSelectElement *select, long length)
795 {
796     long len = 0xdeadbeef;
797     HRESULT hres;
798
799     hres = IHTMLSelectElement_get_length(select, &len);
800     ok_(__FILE__,line) (hres == S_OK, "get_length failed: %08x\n", hres);
801     ok_(__FILE__,line) (len == length, "len=%ld, expected %ld\n", len, length);
802 }
803
804 #define test_select_selidx(s,i) _test_select_selidx(__LINE__,s,i)
805 static void _test_select_selidx(unsigned line, IHTMLSelectElement *select, long index)
806 {
807     long idx = 0xdeadbeef;
808     HRESULT hres;
809
810     hres = IHTMLSelectElement_get_selectedIndex(select, &idx);
811     ok_(__FILE__,line) (hres == S_OK, "get_selectedIndex failed: %08x\n", hres);
812     ok_(__FILE__,line) (idx == index, "idx=%ld, expected %ld\n", idx, index);
813 }
814
815 #define test_select_put_selidx(s,i) _test_select_put_selidx(__LINE__,s,i)
816 static void _test_select_put_selidx(unsigned line, IHTMLSelectElement *select, long index)
817 {
818     HRESULT hres;
819
820     hres = IHTMLSelectElement_put_selectedIndex(select, index);
821     ok_(__FILE__,line) (hres == S_OK, "get_selectedIndex failed: %08x\n", hres);
822     _test_select_selidx(line, select, index);
823 }
824
825 #define test_select_value(s,v) _test_select_value(__LINE__,s,v)
826 static void _test_select_value(unsigned line, IHTMLSelectElement *select, const char *exval)
827 {
828     BSTR val;
829     HRESULT hres;
830
831     hres = IHTMLSelectElement_get_value(select, &val);
832     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
833     if(exval)
834         ok_(__FILE__,line) (!strcmp_wa(val, exval), "unexpected value %s\n", dbgstr_w(val));
835     else
836         ok_(__FILE__,line) (val == NULL, "val=%s, expected NULL\n", dbgstr_w(val));
837 }
838
839 #define test_select_set_value(s,v) _test_select_set_value(__LINE__,s,v)
840 static void _test_select_set_value(unsigned line, IHTMLSelectElement *select, const char *val)
841 {
842     BSTR bstr;
843     HRESULT hres;
844
845     bstr = a2bstr(val);
846     hres = IHTMLSelectElement_put_value(select, bstr);
847     SysFreeString(bstr);
848     ok_(__FILE__,line) (hres == S_OK, "put_value failed: %08x\n", hres);
849 }
850
851 #define test_select_type(s,t) _test_select_type(__LINE__,s,t)
852 static void _test_select_type(unsigned line, IHTMLSelectElement *select, const char *extype)
853 {
854     BSTR type;
855     HRESULT hres;
856
857     hres = IHTMLSelectElement_get_type(select, &type);
858     ok_(__FILE__,line) (hres == S_OK, "get_type failed: %08x\n", hres);
859     ok_(__FILE__,line) (!strcmp_wa(type, extype), "type=%s, expected %s\n", dbgstr_w(type), extype);
860 }
861
862 #define test_range_text(r,t) _test_range_text(__LINE__,r,t)
863 static void _test_range_text(unsigned line, IHTMLTxtRange *range, const char *extext)
864 {
865     BSTR text;
866     HRESULT hres;
867
868     hres = IHTMLTxtRange_get_text(range, &text);
869     ok_(__FILE__, line) (hres == S_OK, "get_text failed: %08x\n", hres);
870
871     if(extext) {
872         ok_(__FILE__, line) (text != NULL, "text == NULL\n");
873         ok_(__FILE__, line) (!strcmp_wa(text, extext), "text=\"%s\", expected \"%s\"\n", dbgstr_w(text), extext);
874     }else {
875         ok_(__FILE__, line) (text == NULL, "text=\"%s\", expected NULL\n", dbgstr_w(text));
876     }
877
878     SysFreeString(text);
879
880 }
881
882 #define test_range_collapse(r,b) _test_range_collapse(__LINE__,r,b)
883 static void _test_range_collapse(unsigned line, IHTMLTxtRange *range, BOOL b)
884 {
885     HRESULT hres;
886
887     hres = IHTMLTxtRange_collapse(range, b);
888     ok_(__FILE__, line) (hres == S_OK, "collapse failed: %08x\n", hres);
889     _test_range_text(line, range, NULL);
890 }
891
892 #define test_range_expand(r,u,b,t) _test_range_expand(__LINE__,r,u,b,t)
893 static void _test_range_expand(unsigned line, IHTMLTxtRange *range, LPWSTR unit,
894         VARIANT_BOOL exb, const char *extext)
895 {
896     VARIANT_BOOL b = 0xe0e0;
897     HRESULT hres;
898
899     hres = IHTMLTxtRange_expand(range, unit, &b);
900     ok_(__FILE__,line) (hres == S_OK, "expand failed: %08x\n", hres);
901     ok_(__FILE__,line) (b == exb, "b=%x, expected %x\n", b, exb);
902     _test_range_text(line, range, extext);
903 }
904
905 #define test_range_move(r,u,c,e) _test_range_move(__LINE__,r,u,c,e)
906 static void _test_range_move(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
907 {
908     long c = 0xdeadbeef;
909     HRESULT hres;
910
911     hres = IHTMLTxtRange_move(range, unit, cnt, &c);
912     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
913     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
914     _test_range_text(line, range, NULL);
915 }
916
917 #define test_range_movestart(r,u,c,e) _test_range_movestart(__LINE__,r,u,c,e)
918 static void _test_range_movestart(unsigned line, IHTMLTxtRange *range,
919         LPWSTR unit, long cnt, long excnt)
920 {
921     long c = 0xdeadbeef;
922     HRESULT hres;
923
924     hres = IHTMLTxtRange_moveStart(range, unit, cnt, &c);
925     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
926     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
927 }
928
929 #define test_range_moveend(r,u,c,e) _test_range_moveend(__LINE__,r,u,c,e)
930 static void _test_range_moveend(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
931 {
932     long c = 0xdeadbeef;
933     HRESULT hres;
934
935     hres = IHTMLTxtRange_moveEnd(range, unit, cnt, &c);
936     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
937     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
938 }
939
940 #define test_range_put_text(r,t) _test_range_put_text(__LINE__,r,t)
941 static void _test_range_put_text(unsigned line, IHTMLTxtRange *range, const char *text)
942 {
943     HRESULT hres;
944     BSTR bstr = a2bstr(text);
945
946     hres = IHTMLTxtRange_put_text(range, bstr);
947     ok_(__FILE__,line) (hres == S_OK, "put_text failed: %08x\n", hres);
948     SysFreeString(bstr);
949     _test_range_text(line, range, NULL);
950 }
951
952 #define test_range_inrange(r1,r2,b) _test_range_inrange(__LINE__,r1,r2,b)
953 static void _test_range_inrange(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
954 {
955     VARIANT_BOOL b;
956     HRESULT hres;
957
958     b = 0xe0e0;
959     hres = IHTMLTxtRange_inRange(range1, range2, &b);
960     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
961     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
962 }
963
964 #define test_range_isequal(r1,r2,b) _test_range_isequal(__LINE__,r1,r2,b)
965 static void _test_range_isequal(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
966 {
967     VARIANT_BOOL b;
968     HRESULT hres;
969
970     b = 0xe0e0;
971     hres = IHTMLTxtRange_isEqual(range1, range2, &b);
972     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
973     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
974
975     b = 0xe0e0;
976     hres = IHTMLTxtRange_isEqual(range2, range1, &b);
977     ok_(__FILE__,line) (hres == S_OK, "(2->1) isEqual failed: %08x\n", hres);
978     ok_(__FILE__,line) (b == exb, "(2->1) b=%x, expected %x\n", b, exb);
979
980     if(exb) {
981         test_range_inrange(range1, range2, VARIANT_TRUE);
982         test_range_inrange(range2, range1, VARIANT_TRUE);
983     }
984 }
985
986 #define test_range_parent(r,t) _test_range_parent(__LINE__,r,t)
987 static void _test_range_parent(unsigned line, IHTMLTxtRange *range, elem_type_t type)
988 {
989     IHTMLElement *elem;
990     HRESULT hres;
991
992     hres = IHTMLTxtRange_parentElement(range, &elem);
993     ok_(__FILE__,line) (hres == S_OK, "parentElement failed: %08x\n", hres);
994
995     _test_elem_type(line, (IUnknown*)elem, type);
996
997     IHTMLElement_Release(elem);
998 }
999
1000 #define test_elem_collection(c,t,l) _test_elem_collection(__LINE__,c,t,l)
1001 static void _test_elem_collection(unsigned line, IUnknown *unk,
1002         const elem_type_t *elem_types, long exlen)
1003 {
1004     IHTMLElementCollection *col;
1005     long len;
1006     DWORD i;
1007     VARIANT name, index;
1008     IDispatch *disp;
1009     HRESULT hres;
1010
1011     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElementCollection, (void**)&col);
1012     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLElementCollection: %08x\n", hres);
1013
1014     test_disp((IUnknown*)col, &DIID_DispHTMLElementCollection);
1015
1016     hres = IHTMLElementCollection_get_length(col, &len);
1017     ok_(__FILE__,line) (hres == S_OK, "get_length failed: %08x\n", hres);
1018     ok_(__FILE__,line) (len == exlen, "len=%ld, expected %ld\n", len, exlen);
1019
1020     if(len > exlen)
1021         len = exlen;
1022
1023     V_VT(&index) = VT_EMPTY;
1024     V_VT(&name) = VT_I4;
1025
1026     for(i=0; i<len; i++) {
1027         V_I4(&name) = i;
1028         disp = (void*)0xdeadbeef;
1029         hres = IHTMLElementCollection_item(col, name, index, &disp);
1030         ok_(__FILE__,line) (hres == S_OK, "item(%d) failed: %08x\n", i, hres);
1031         ok_(__FILE__,line) (disp != NULL, "item returned NULL\n");
1032         if(FAILED(hres) || !disp)
1033             continue;
1034
1035         _test_elem_type(line, (IUnknown*)disp, elem_types[i]);
1036         IDispatch_Release(disp);
1037     }
1038
1039     V_I4(&name) = len;
1040     disp = (void*)0xdeadbeef;
1041     hres = IHTMLElementCollection_item(col, name, index, &disp);
1042     ok_(__FILE__,line) (hres == S_OK, "item failed: %08x\n", hres);
1043     ok_(__FILE__,line) (disp == NULL, "disp != NULL\n");
1044
1045     V_I4(&name) = -1;
1046     disp = (void*)0xdeadbeef;
1047     hres = IHTMLElementCollection_item(col, name, index, &disp);
1048     ok_(__FILE__,line) (hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
1049     ok_(__FILE__,line) (disp == NULL, "disp != NULL\n");
1050
1051     IHTMLElementCollection_Release(col);
1052 }
1053
1054 #define test_elem_getelembytag(u,t,l) _test_elem_getelembytag(__LINE__,u,t,l)
1055 static void _test_elem_getelembytag(unsigned line, IUnknown *unk, elem_type_t type, long exlen)
1056 {
1057     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1058     IHTMLElementCollection *col = NULL;
1059     elem_type_t *types = NULL;
1060     BSTR tmp;
1061     int i;
1062     HRESULT hres;
1063
1064     tmp = a2bstr(elem_type_infos[type].tag);
1065     hres = IHTMLElement2_getElementsByTagName(elem, tmp, &col);
1066     SysFreeString(tmp);
1067     IHTMLElement2_Release(elem);
1068     ok_(__FILE__,line) (hres == S_OK, "getElementByTagName failed: %08x\n", hres);
1069     ok_(__FILE__,line) (col != NULL, "col == NULL\n");
1070
1071     if(exlen) {
1072         types = HeapAlloc(GetProcessHeap(), 0, exlen*sizeof(elem_type_t));
1073         for(i=0; i<exlen; i++)
1074             types[i] = type;
1075     }
1076
1077     _test_elem_collection(line, (IUnknown*)col, types, exlen);
1078
1079     HeapFree(GetProcessHeap(), 0, types);
1080 }
1081
1082 #define test_elem_innertext(e,t) _test_elem_innertext(__LINE__,e,t)
1083 static void _test_elem_innertext(unsigned line, IHTMLElement *elem, const char *extext)
1084 {
1085     BSTR text = NULL;
1086     HRESULT hres;
1087
1088     hres = IHTMLElement_get_innerText(elem, &text);
1089     ok_(__FILE__,line) (hres == S_OK, "get_innerText failed: %08x\n", hres);
1090     ok_(__FILE__,line) (!strcmp_wa(text, extext), "get_innerText returned %s expected %s\n",
1091                         dbgstr_w(text), extext);
1092     SysFreeString(text);
1093 }
1094
1095 #define test_elem_set_innertext(e,t) _test_elem_set_innertext(__LINE__,e,t)
1096 static void _test_elem_set_innertext(unsigned line, IHTMLElement *elem, const char *text)
1097 {
1098     IHTMLDOMChildrenCollection *col;
1099     BSTR str;
1100     HRESULT hres;
1101
1102     str = a2bstr(text);
1103     hres = IHTMLElement_put_innerText(elem, str);
1104     ok_(__FILE__,line) (hres == S_OK, "put_innerText failed: %08x\n", hres);
1105     SysFreeString(str);
1106
1107     _test_elem_innertext(line, elem, text);
1108
1109
1110     col = _get_child_nodes(line, (IUnknown*)elem);
1111     ok(col != NULL, "col == NULL\n");
1112     if(col) {
1113         long length = 0, type;
1114         IHTMLDOMNode *node;
1115
1116         hres = IHTMLDOMChildrenCollection_get_length(col, &length);
1117         ok(hres == S_OK, "get_length failed: %08x\n", hres);
1118         ok(length == 1, "length = %ld\n", length);
1119
1120         node = _get_child_item(line, col, 0);
1121         ok(node != NULL, "node == NULL\n");
1122         if(node) {
1123             type = _get_node_type(line, (IUnknown*)node);
1124             ok(type == 3, "type=%ld\n", type);
1125             IHTMLDOMNode_Release(node);
1126         }
1127
1128         IHTMLDOMChildrenCollection_Release(col);
1129     }
1130
1131 }
1132
1133 #define get_first_child(n) _get_first_child(__LINE__,n)
1134 static IHTMLDOMNode *_get_first_child(unsigned line, IUnknown *unk)
1135 {
1136     IHTMLDOMNode *node = _get_node_iface(line, unk);
1137     IHTMLDOMNode *child = NULL;
1138     HRESULT hres;
1139
1140     hres = IHTMLDOMNode_get_firstChild(node, &child);
1141     IHTMLDOMNode_Release(node);
1142     ok_(__FILE__,line) (hres == S_OK, "get_firstChild failed: %08x\n", hres);
1143
1144     return child;
1145 }
1146
1147 #define test_node_has_child(u,b) _test_node_has_child(__LINE__,u,b)
1148 static void _test_node_has_child(unsigned line, IUnknown *unk, VARIANT_BOOL exb)
1149 {
1150     IHTMLDOMNode *node = _get_node_iface(line, unk);
1151     VARIANT_BOOL b = 0xdead;
1152     HRESULT hres;
1153
1154     hres = IHTMLDOMNode_hasChildNodes(node, &b);
1155     ok_(__FILE__,line) (hres == S_OK, "hasChildNodes failed: %08x\n", hres);
1156     ok_(__FILE__,line) (b == exb, "hasChildNodes=%x, expected %x\n", b, exb);
1157
1158     IHTMLDOMNode_Release(node);
1159 }
1160
1161 #define test_node_get_parent(u) _test_node_get_parent(__LINE__,u)
1162 static IHTMLDOMNode *_test_node_get_parent(unsigned line, IUnknown *unk)
1163 {
1164     IHTMLDOMNode *node = _get_node_iface(line, unk);
1165     IHTMLDOMNode *parent;
1166     HRESULT hres;
1167
1168     hres = IHTMLDOMNode_get_parentNode(node, &parent);
1169     IHTMLDOMNode_Release(node);
1170     ok_(__FILE__,line) (hres == S_OK, "get_parentNode failed: %08x\n", hres);
1171
1172     return parent;
1173 }
1174
1175 #define test_elem_get_parent(u) _test_elem_get_parent(__LINE__,u)
1176 static IHTMLElement *_test_elem_get_parent(unsigned line, IUnknown *unk)
1177 {
1178     IHTMLElement *elem = _get_elem_iface(line, unk);
1179     IHTMLElement *parent;
1180     HRESULT hres;
1181
1182     hres = IHTMLElement_get_parentElement(elem, &parent);
1183     IHTMLElement_Release(elem);
1184     ok_(__FILE__,line) (hres == S_OK, "get_parentElement failed: %08x\n", hres);
1185
1186     return parent;
1187 }
1188
1189 #define test_elem3_get_disabled(i,b) _test_elem3_get_disabled(__LINE__,i,b)
1190 static void _test_elem3_get_disabled(unsigned line, IUnknown *unk, VARIANT_BOOL exb)
1191 {
1192     IHTMLElement3 *elem3 = _get_elem3_iface(line, unk);
1193     VARIANT_BOOL disabled = 100;
1194     HRESULT hres;
1195
1196     if (!elem3) return;
1197     hres = IHTMLElement3_get_disabled(elem3, &disabled);
1198     ok_(__FILE__,line) (hres == S_OK, "get_disabled failed: %08x\n", hres);
1199     ok_(__FILE__,line) (disabled == exb, "disabled=%x, expected %x\n", disabled, exb);
1200     IHTMLElement3_Release(elem3);
1201 }
1202
1203 #define test_elem3_set_disabled(i,b) _test_elem3_set_disabled(__LINE__,i,b)
1204 static void _test_elem3_set_disabled(unsigned line, IUnknown *unk, VARIANT_BOOL b)
1205 {
1206     IHTMLElement3 *elem3 = _get_elem3_iface(line, unk);
1207     HRESULT hres;
1208
1209     if (!elem3) return;
1210     hres = IHTMLElement3_put_disabled(elem3, b);
1211     ok_(__FILE__,line) (hres == S_OK, "get_disabled failed: %08x\n", hres);
1212
1213     IHTMLElement3_Release(elem3);
1214     _test_elem3_get_disabled(line, unk, b);
1215 }
1216
1217 #define test_select_get_disabled(i,b) _test_select_get_disabled(__LINE__,i,b)
1218 static void _test_select_get_disabled(unsigned line, IHTMLSelectElement *select, VARIANT_BOOL exb)
1219 {
1220     VARIANT_BOOL disabled = 100;
1221     HRESULT hres;
1222
1223     hres = IHTMLSelectElement_get_disabled(select, &disabled);
1224     ok_(__FILE__,line) (hres == S_OK, "get_disabled failed: %08x\n", hres);
1225     ok_(__FILE__,line) (disabled == exb, "disabled=%x, expected %x\n", disabled, exb);
1226
1227     _test_elem3_get_disabled(line, (IUnknown*)select, exb);
1228 }
1229
1230 #define test_select_set_disabled(i,b) _test_select_set_disabled(__LINE__,i,b)
1231 static void _test_select_set_disabled(unsigned line, IHTMLSelectElement *select, VARIANT_BOOL b)
1232 {
1233     HRESULT hres;
1234
1235     hres = IHTMLSelectElement_put_disabled(select, b);
1236     ok_(__FILE__,line) (hres == S_OK, "get_disabled failed: %08x\n", hres);
1237
1238     _test_select_get_disabled(line, select, b);
1239 }
1240
1241 #define elem_get_scroll_height(u) _elem_get_scroll_height(__LINE__,u)
1242 static long _elem_get_scroll_height(unsigned line, IUnknown *unk)
1243 {
1244     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1245     IHTMLTextContainer *txtcont;
1246     long l = -1, l2 = -1;
1247     HRESULT hres;
1248
1249     hres = IHTMLElement2_get_scrollHeight(elem, &l);
1250     ok_(__FILE__,line) (hres == S_OK, "get_scrollHeight failed: %08x\n", hres);
1251     IHTMLElement2_Release(elem);
1252
1253     hres = IUnknown_QueryInterface(unk, &IID_IHTMLTextContainer, (void**)&txtcont);
1254     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLTextContainer: %08x\n", hres);
1255
1256     hres = IHTMLTextContainer_get_scrollHeight(txtcont, &l2);
1257     IHTMLTextContainer_Release(txtcont);
1258     ok_(__FILE__,line) (hres == S_OK, "IHTMLTextContainer::get_scrollHeight failed: %ld\n", l2);
1259     ok_(__FILE__,line) (l == l2, "unexpected height %ld, expected %ld\n", l2, l);
1260
1261     return l;
1262 }
1263
1264 #define elem_get_scroll_width(u) _elem_get_scroll_width(__LINE__,u)
1265 static long _elem_get_scroll_width(unsigned line, IUnknown *unk)
1266 {
1267     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1268     IHTMLTextContainer *txtcont;
1269     long l = -1, l2 = -1;
1270     HRESULT hres;
1271
1272     hres = IHTMLElement2_get_scrollWidth(elem, &l);
1273     ok_(__FILE__,line) (hres == S_OK, "get_scrollWidth failed: %08x\n", hres);
1274     IHTMLElement2_Release(elem);
1275
1276     hres = IUnknown_QueryInterface(unk, &IID_IHTMLTextContainer, (void**)&txtcont);
1277     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLTextContainer: %08x\n", hres);
1278
1279     hres = IHTMLTextContainer_get_scrollWidth(txtcont, &l2);
1280     IHTMLTextContainer_Release(txtcont);
1281     ok_(__FILE__,line) (hres == S_OK, "IHTMLTextContainer::get_scrollWidth failed: %ld\n", l2);
1282     ok_(__FILE__,line) (l == l2, "unexpected width %ld, expected %ld\n", l2, l);
1283
1284     return l;
1285 }
1286
1287 #define elem_get_scroll_top(u) _elem_get_scroll_top(__LINE__,u)
1288 static long _elem_get_scroll_top(unsigned line, IUnknown *unk)
1289 {
1290     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1291     IHTMLTextContainer *txtcont;
1292     long l = -1, l2 = -1;
1293     HRESULT hres;
1294
1295     hres = IHTMLElement2_get_scrollTop(elem, &l);
1296     ok_(__FILE__,line) (hres == S_OK, "get_scrollTop failed: %08x\n", hres);
1297     IHTMLElement2_Release(elem);
1298
1299     hres = IUnknown_QueryInterface(unk, &IID_IHTMLTextContainer, (void**)&txtcont);
1300     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLTextContainer: %08x\n", hres);
1301
1302     hres = IHTMLTextContainer_get_scrollTop(txtcont, &l2);
1303     IHTMLTextContainer_Release(txtcont);
1304     ok_(__FILE__,line) (hres == S_OK, "IHTMLTextContainer::get_scrollTop failed: %ld\n", l2);
1305     ok_(__FILE__,line) (l == l2, "unexpected top %ld, expected %ld\n", l2, l);
1306
1307     return l;
1308 }
1309
1310 #define elem_get_scroll_left(u) _elem_get_scroll_left(__LINE__,u)
1311 static void _elem_get_scroll_left(unsigned line, IUnknown *unk)
1312 {
1313     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1314     IHTMLTextContainer *txtcont;
1315     long l = -1, l2 = -1;
1316     HRESULT hres;
1317
1318     hres = IHTMLElement2_get_scrollLeft(elem, NULL);
1319     ok(hres == E_INVALIDARG, "expect E_INVALIDARG got 0x%08x\n", hres);
1320
1321     hres = IHTMLElement2_get_scrollLeft(elem, &l);
1322     ok(hres == S_OK, "get_scrollTop failed: %08x\n", hres);
1323     IHTMLElement2_Release(elem);
1324
1325     hres = IUnknown_QueryInterface(unk, &IID_IHTMLTextContainer, (void**)&txtcont);
1326     ok(hres == S_OK, "Could not get IHTMLTextContainer: %08x\n", hres);
1327
1328     hres = IHTMLTextContainer_get_scrollLeft(txtcont, &l2);
1329     IHTMLTextContainer_Release(txtcont);
1330     ok(hres == S_OK, "IHTMLTextContainer::get_scrollLeft failed: %ld\n", l2);
1331     ok(l == l2, "unexpected left %ld, expected %ld\n", l2, l);
1332 }
1333
1334 #define test_img_src(i,s) _test_img_src(__LINE__,i,s)
1335 static void _test_img_src(unsigned line, IUnknown *unk, const char *exsrc)
1336 {
1337     IHTMLImgElement *img = _get_img_iface(line, unk);
1338     BSTR src;
1339     HRESULT hres;
1340
1341     hres = IHTMLImgElement_get_src(img, &src);
1342     IHTMLImgElement_Release(img);
1343     ok_(__FILE__,line) (hres == S_OK, "get_src failed: %08x\n", hres);
1344     ok_(__FILE__,line) (!strcmp_wa(src, exsrc), "get_src returned %s expected %s\n", dbgstr_w(src), exsrc);
1345     SysFreeString(src);
1346 }
1347
1348 #define test_img_set_src(u,s) _test_img_set_src(__LINE__,u,s)
1349 static void _test_img_set_src(unsigned line, IUnknown *unk, const char *src)
1350 {
1351     IHTMLImgElement *img = _get_img_iface(line, unk);
1352     BSTR tmp;
1353     HRESULT hres;
1354
1355     tmp = a2bstr(src);
1356     hres = IHTMLImgElement_put_src(img, tmp);
1357     IHTMLImgElement_Release(img);
1358     SysFreeString(tmp);
1359     ok_(__FILE__,line) (hres == S_OK, "put_src failed: %08x\n", hres);
1360
1361     _test_img_src(line, unk, src);
1362 }
1363
1364 #define test_img_alt(u,a) _test_img_alt(__LINE__,u,a)
1365 static void _test_img_alt(unsigned line, IUnknown *unk, const char *exalt)
1366 {
1367     IHTMLImgElement *img = _get_img_iface(line, unk);
1368     BSTR alt;
1369     HRESULT hres;
1370
1371     hres = IHTMLImgElement_get_alt(img, &alt);
1372     ok_(__FILE__,line) (hres == S_OK, "get_alt failed: %08x\n", hres);
1373     if(exalt)
1374         ok_(__FILE__,line) (!strcmp_wa(alt, exalt), "inexopected alt %s\n", dbgstr_w(alt));
1375     else
1376         ok_(__FILE__,line) (!alt, "alt != NULL\n");
1377     SysFreeString(alt);
1378 }
1379
1380 #define test_img_set_alt(u,a) _test_img_set_alt(__LINE__,u,a)
1381 static void _test_img_set_alt(unsigned line, IUnknown *unk, const char *alt)
1382 {
1383     IHTMLImgElement *img = _get_img_iface(line, unk);
1384     BSTR tmp;
1385     HRESULT hres;
1386
1387     tmp = a2bstr(alt);
1388     hres = IHTMLImgElement_put_alt(img, tmp);
1389     ok_(__FILE__,line) (hres == S_OK, "get_alt failed: %08x\n", hres);
1390     SysFreeString(tmp);
1391
1392     _test_img_alt(line, unk, alt);
1393 }
1394
1395 #define test_input_get_disabled(i,b) _test_input_get_disabled(__LINE__,i,b)
1396 static void _test_input_get_disabled(unsigned line, IHTMLInputElement *input, VARIANT_BOOL exb)
1397 {
1398     VARIANT_BOOL disabled = 100;
1399     HRESULT hres;
1400
1401     hres = IHTMLInputElement_get_disabled(input, &disabled);
1402     ok_(__FILE__,line) (hres == S_OK, "get_disabled failed: %08x\n", hres);
1403     ok_(__FILE__,line) (disabled == exb, "disabled=%x, expected %x\n", disabled, exb);
1404
1405     _test_elem3_get_disabled(line, (IUnknown*)input, exb);
1406 }
1407
1408 #define test_input_set_disabled(i,b) _test_input_set_disabled(__LINE__,i,b)
1409 static void _test_input_set_disabled(unsigned line, IHTMLInputElement *input, VARIANT_BOOL b)
1410 {
1411     HRESULT hres;
1412
1413     hres = IHTMLInputElement_put_disabled(input, b);
1414     ok_(__FILE__,line) (hres == S_OK, "get_disabled failed: %08x\n", hres);
1415
1416     _test_input_get_disabled(line, input, b);
1417 }
1418
1419 #define test_input_get_defaultchecked(i,b) _test_input_get_defaultchecked(__LINE__,i,b)
1420 static void _test_input_get_defaultchecked(unsigned line, IHTMLInputElement *input, VARIANT_BOOL exb)
1421 {
1422     VARIANT_BOOL checked = 100;
1423     HRESULT hres;
1424
1425     hres = IHTMLInputElement_get_defaultChecked(input, &checked);
1426     ok_(__FILE__,line) (hres == S_OK, "get_defaultChecked failed: %08x\n", hres);
1427     ok_(__FILE__,line) (checked == exb, "checked=%x, expected %x\n", checked, exb);
1428 }
1429
1430 #define test_input_set_defaultchecked(i,b) _test_input_set_defaultchecked(__LINE__,i,b)
1431 static void _test_input_set_defaultchecked(unsigned line, IHTMLInputElement *input, VARIANT_BOOL b)
1432 {
1433     HRESULT hres;
1434
1435     hres = IHTMLInputElement_put_defaultChecked(input, b);
1436     ok_(__FILE__,line) (hres == S_OK, "get_defaultChecked failed: %08x\n", hres);
1437
1438     _test_input_get_defaultchecked(line, input, b);
1439 }
1440
1441 #define test_input_get_checked(i,b) _test_input_get_checked(__LINE__,i,b)
1442 static void _test_input_get_checked(unsigned line, IHTMLInputElement *input, VARIANT_BOOL exb)
1443 {
1444     VARIANT_BOOL checked = 100;
1445     HRESULT hres;
1446
1447     hres = IHTMLInputElement_get_checked(input, &checked);
1448     ok_(__FILE__,line) (hres == S_OK, "get_checked failed: %08x\n", hres);
1449     ok_(__FILE__,line) (checked == exb, "checked=%x, expected %x\n", checked, exb);
1450 }
1451
1452 #define test_input_set_checked(i,b) _test_input_set_checked(__LINE__,i,b)
1453 static void _test_input_set_checked(unsigned line, IHTMLInputElement *input, VARIANT_BOOL b)
1454 {
1455     HRESULT hres;
1456
1457     hres = IHTMLInputElement_put_checked(input, b);
1458     ok_(__FILE__,line) (hres == S_OK, "get_checked failed: %08x\n", hres);
1459
1460     _test_input_get_checked(line, input, b);
1461 }
1462
1463 #define test_input_value(o,t) _test_input_value(__LINE__,o,t)
1464 static void _test_input_value(unsigned line, IUnknown *unk, const char *exval)
1465 {
1466     IHTMLInputElement *input;
1467     BSTR bstr;
1468     HRESULT hres;
1469
1470     hres = IUnknown_QueryInterface(unk, &IID_IHTMLInputElement, (void**)&input);
1471     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLInputElement: %08x\n", hres);
1472     if(FAILED(hres))
1473         return;
1474
1475     hres = IHTMLInputElement_get_value(input, &bstr);
1476     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
1477     if(exval)
1478         ok_(__FILE__,line) (!strcmp_wa(bstr, exval), "value=%s\n", dbgstr_w(bstr));
1479     else
1480         ok_(__FILE__,line) (!exval, "exval != NULL\n");
1481     SysFreeString(bstr);
1482     IHTMLInputElement_Release(input);
1483 }
1484
1485 #define test_input_put_value(o,v) _test_input_put_value(__LINE__,o,v)
1486 static void _test_input_put_value(unsigned line, IUnknown *unk, const char *val)
1487 {
1488     IHTMLInputElement *input;
1489     BSTR bstr;
1490     HRESULT hres;
1491
1492     hres = IUnknown_QueryInterface(unk, &IID_IHTMLInputElement, (void**)&input);
1493     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLInputElement: %08x\n", hres);
1494     if(FAILED(hres))
1495         return;
1496
1497     bstr = a2bstr(val);
1498     hres = IHTMLInputElement_get_value(input, &bstr);
1499     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
1500     SysFreeString(bstr);
1501     IHTMLInputElement_Release(input);
1502 }
1503
1504 #define test_elem_class(u,c) _test_elem_class(__LINE__,u,c)
1505 static void _test_elem_class(unsigned line, IUnknown *unk, const char *exclass)
1506 {
1507     IHTMLElement *elem = _get_elem_iface(line, unk);
1508     BSTR class = (void*)0xdeadbeef;
1509     HRESULT hres;
1510
1511     hres = IHTMLElement_get_className(elem, &class);
1512     IHTMLElement_Release(elem);
1513     ok_(__FILE__,line) (hres == S_OK, "get_className failed: %08x\n", hres);
1514     if(exclass)
1515         ok_(__FILE__,line) (!strcmp_wa(class, exclass), "unexpected className %s\n", dbgstr_w(class));
1516     else
1517         ok_(__FILE__,line) (!class, "class != NULL\n");
1518     SysFreeString(class);
1519 }
1520
1521 #define test_elem_tabindex(u,i) _test_elem_tabindex(__LINE__,u,i)
1522 static void _test_elem_tabindex(unsigned line, IUnknown *unk, short exindex)
1523 {
1524     IHTMLElement2 *elem2 = _get_elem2_iface(line, unk);
1525     short index = -3;
1526     HRESULT hres;
1527
1528     hres = IHTMLElement2_get_tabIndex(elem2, &index);
1529     IHTMLElement2_Release(elem2);
1530     ok_(__FILE__,line) (hres == S_OK, "get_tabIndex failed: %08x\n", hres);
1531     ok_(__FILE__,line) (index == exindex, "unexpected index %d\n", index);
1532 }
1533
1534 #define test_elem_set_tabindex(u,i) _test_elem_set_tabindex(__LINE__,u,i)
1535 static void _test_elem_set_tabindex(unsigned line, IUnknown *unk, short index)
1536 {
1537     IHTMLElement2 *elem2 = _get_elem2_iface(line, unk);
1538     HRESULT hres;
1539
1540     hres = IHTMLElement2_put_tabIndex(elem2, index);
1541     IHTMLElement2_Release(elem2);
1542     ok_(__FILE__,line) (hres == S_OK, "get_tabIndex failed: %08x\n", hres);
1543
1544     _test_elem_tabindex(line, unk, index);
1545 }
1546
1547 #define test_elem_set_class(u,c) _test_elem_set_class(__LINE__,u,c)
1548 static void _test_elem_set_class(unsigned line, IUnknown *unk, const char *class)
1549 {
1550     IHTMLElement *elem = _get_elem_iface(line, unk);
1551     BSTR tmp;
1552     HRESULT hres;
1553
1554     tmp = class ? a2bstr(class) : NULL;
1555     hres = IHTMLElement_put_className(elem, tmp);
1556     IHTMLElement_Release(elem);
1557     ok_(__FILE__,line) (hres == S_OK, "put_className failed: %08x\n", hres);
1558     SysFreeString(tmp);
1559
1560     _test_elem_class(line, unk, class);
1561 }
1562
1563 #define test_elem_id(e,i) _test_elem_id(__LINE__,e,i)
1564 static void _test_elem_id(unsigned line, IUnknown *unk, const char *exid)
1565 {
1566     IHTMLElement *elem = _get_elem_iface(line, unk);
1567     BSTR id = (void*)0xdeadbeef;
1568     HRESULT hres;
1569
1570     hres = IHTMLElement_get_id(elem, &id);
1571     IHTMLElement_Release(elem);
1572     ok_(__FILE__,line) (hres == S_OK, "get_id failed: %08x\n", hres);
1573
1574     if(exid)
1575         ok_(__FILE__,line) (!strcmp_wa(id, exid), "unexpected id %s\n", dbgstr_w(id));
1576     else
1577         ok_(__FILE__,line) (!id, "id=%s\n", dbgstr_w(id));
1578
1579     SysFreeString(id);
1580 }
1581
1582 #define test_elem_put_id(u,i) _test_elem_put_id(__LINE__,u,i)
1583 static void _test_elem_put_id(unsigned line, IUnknown *unk, const char *new_id)
1584 {
1585     IHTMLElement *elem = _get_elem_iface(line, unk);
1586     BSTR tmp = a2bstr(new_id);
1587     HRESULT hres;
1588
1589     hres = IHTMLElement_put_id(elem, tmp);
1590     IHTMLElement_Release(elem);
1591     SysFreeString(tmp);
1592     ok_(__FILE__,line) (hres == S_OK, "put_id failed: %08x\n", hres);
1593
1594     _test_elem_id(line, unk, new_id);
1595 }
1596
1597 #define test_elem_title(u,t) _test_elem_title(__LINE__,u,t)
1598 static void _test_elem_title(unsigned line, IUnknown *unk, const char *extitle)
1599 {
1600     IHTMLElement *elem = _get_elem_iface(line, unk);
1601     BSTR title;
1602     HRESULT hres;
1603
1604     hres = IHTMLElement_get_title(elem, &title);
1605     IHTMLElement_Release(elem);
1606     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1607     if(extitle)
1608         ok_(__FILE__,line) (!strcmp_wa(title, extitle), "unexpected title %s\n", dbgstr_w(title));
1609     else
1610         ok_(__FILE__,line) (!title, "title=%s, expected NULL\n", dbgstr_w(title));
1611
1612     SysFreeString(title);
1613 }
1614
1615 #define test_elem_set_title(u,t) _test_elem_set_title(__LINE__,u,t)
1616 static void _test_elem_set_title(unsigned line, IUnknown *unk, const char *title)
1617 {
1618     IHTMLElement *elem = _get_elem_iface(line, unk);
1619     BSTR tmp;
1620     HRESULT hres;
1621
1622     tmp = a2bstr(title);
1623     hres = IHTMLElement_put_title(elem, tmp);
1624     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1625
1626     IHTMLElement_Release(elem);
1627     SysFreeString(tmp);
1628 }
1629
1630 #define test_node_get_value_str(u,e) _test_node_get_value_str(__LINE__,u,e)
1631 static void _test_node_get_value_str(unsigned line, IUnknown *unk, const char *exval)
1632 {
1633     IHTMLDOMNode *node = _get_node_iface(line, unk);
1634     VARIANT var;
1635     HRESULT hres;
1636
1637     hres = IHTMLDOMNode_get_nodeValue(node, &var);
1638     IHTMLDOMNode_Release(node);
1639     ok_(__FILE__,line) (hres == S_OK, "get_nodeValue failed: %08x, expected VT_BSTR\n", hres);
1640
1641     if(exval) {
1642         ok_(__FILE__,line) (V_VT(&var) == VT_BSTR, "vt=%d\n", V_VT(&var));
1643         ok_(__FILE__,line) (!strcmp_wa(V_BSTR(&var), exval), "unexpected value %s\n", dbgstr_w(V_BSTR(&var)));
1644     }else {
1645         ok_(__FILE__,line) (V_VT(&var) == VT_NULL, "vt=%d, expected VT_NULL\n", V_VT(&var));
1646     }
1647
1648     VariantClear(&var);
1649 }
1650
1651 #define test_node_put_value_str(u,v) _test_node_put_value_str(__LINE__,u,v)
1652 static void _test_node_put_value_str(unsigned line, IUnknown *unk, const char *val)
1653 {
1654     IHTMLDOMNode *node = _get_node_iface(line, unk);
1655     VARIANT var;
1656     HRESULT hres;
1657
1658     V_VT(&var) = VT_BSTR;
1659     V_BSTR(&var) = a2bstr(val);
1660
1661     hres = IHTMLDOMNode_put_nodeValue(node, var);
1662     ok_(__FILE__,line) (hres == S_OK, "get_nodeValue failed: %08x, expected VT_BSTR\n", hres);
1663     IHTMLDOMNode_Release(node);
1664     VariantClear(&var);
1665 }
1666
1667 #define test_elem_client_size(u) _test_elem_client_size(__LINE__,u)
1668 static void _test_elem_client_size(unsigned line, IUnknown *unk)
1669 {
1670     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
1671     long l;
1672     HRESULT hres;
1673
1674     hres = IHTMLElement2_get_clientWidth(elem, &l);
1675     ok_(__FILE__,line) (hres == S_OK, "get_clientWidth failed: %08x\n", hres);
1676     hres = IHTMLElement2_get_clientHeight(elem, &l);
1677     ok_(__FILE__,line) (hres == S_OK, "get_clientHeight failed: %08x\n", hres);
1678
1679     IHTMLElement2_Release(elem);
1680 }
1681
1682 #define test_create_elem(d,t) _test_create_elem(__LINE__,d,t)
1683 static IHTMLElement *_test_create_elem(unsigned line, IHTMLDocument2 *doc, const char *tag)
1684 {
1685     IHTMLElement *elem = NULL;
1686     BSTR tmp;
1687     HRESULT hres;
1688
1689     tmp = a2bstr(tag);
1690     hres = IHTMLDocument2_createElement(doc, tmp, &elem);
1691     ok_(__FILE__,line) (hres == S_OK, "createElement failed: %08x\n", hres);
1692     ok_(__FILE__,line) (elem != NULL, "elem == NULL\n");
1693
1694     return elem;
1695 }
1696
1697 #define test_create_text(d,t) _test_create_text(__LINE__,d,t)
1698 static IHTMLDOMNode *_test_create_text(unsigned line, IHTMLDocument2 *doc, const char *text)
1699 {
1700     IHTMLDocument3 *doc3;
1701     IHTMLDOMNode *node = NULL;
1702     BSTR tmp;
1703     HRESULT hres;
1704
1705     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
1706     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDocument3: %08x\n", hres);
1707
1708     tmp = a2bstr(text);
1709     hres = IHTMLDocument3_createTextNode(doc3, tmp, &node);
1710     IHTMLDocument3_Release(doc3);
1711     ok_(__FILE__,line) (hres == S_OK, "createElement failed: %08x\n", hres);
1712     ok_(__FILE__,line) (node != NULL, "node == NULL\n");
1713
1714     return node;
1715 }
1716
1717 #define test_node_append_child(n,c) _test_node_append_child(__LINE__,n,c)
1718 static IHTMLDOMNode *_test_node_append_child(unsigned line, IUnknown *node_unk, IUnknown *child_unk)
1719 {
1720     IHTMLDOMNode *node = _get_node_iface(line, node_unk);
1721     IHTMLDOMNode *child = _get_node_iface(line, child_unk);
1722     IHTMLDOMNode *new_child = NULL;
1723     HRESULT hres;
1724
1725     hres = IHTMLDOMNode_appendChild(node, child, &new_child);
1726     ok_(__FILE__,line) (hres == S_OK, "appendChild failed: %08x\n", hres);
1727     ok_(__FILE__,line) (new_child != NULL, "new_child == NULL\n");
1728     /* TODO  ok_(__FILE__,line) (new_child != child, "new_child == child\n"); */
1729
1730     IHTMLDOMNode_Release(node);
1731     IHTMLDOMNode_Release(child);
1732
1733     return new_child;
1734 }
1735
1736 #define test_node_insertbefore(n,c,v) _test_node_insertbefore(__LINE__,n,c,v)
1737 static IHTMLDOMNode *_test_node_insertbefore(unsigned line, IUnknown *node_unk, IHTMLDOMNode *child, VARIANT *var)
1738 {
1739     IHTMLDOMNode *node = _get_node_iface(line, node_unk);
1740     IHTMLDOMNode *new_child = NULL;
1741     HRESULT hres;
1742
1743     hres = IHTMLDOMNode_insertBefore(node, child, *var, &new_child);
1744     ok_(__FILE__,line) (hres == S_OK, "insertBefore failed: %08x\n", hres);
1745     ok_(__FILE__,line) (new_child != NULL, "new_child == NULL\n");
1746     /* TODO  ok_(__FILE__,line) (new_child != child, "new_child == child\n"); */
1747
1748     IHTMLDOMNode_Release(node);
1749
1750     return new_child;
1751 }
1752
1753 #define test_node_remove_child(n,c) _test_node_remove_child(__LINE__,n,c)
1754 static void _test_node_remove_child(unsigned line, IUnknown *unk, IHTMLDOMNode *child)
1755 {
1756     IHTMLDOMNode *node = _get_node_iface(line, unk);
1757     IHTMLDOMNode *new_node = NULL;
1758     HRESULT hres;
1759
1760     hres = IHTMLDOMNode_removeChild(node, child, &new_node);
1761     ok_(__FILE__,line) (hres == S_OK, "removeChild failed: %08x\n", hres);
1762     ok_(__FILE__,line) (new_node != NULL, "new_node == NULL\n");
1763     /* TODO ok_(__FILE__,line) (new_node != child, "new_node == child\n"); */
1764
1765     IHTMLDOMNode_Release(node);
1766     IHTMLDOMNode_Release(new_node);
1767 }
1768
1769 #define test_doc_title(d,t) _test_doc_title(__LINE__,d,t)
1770 static void _test_doc_title(unsigned line, IHTMLDocument2 *doc, const char *extitle)
1771 {
1772     BSTR title = NULL;
1773     HRESULT hres;
1774
1775     hres = IHTMLDocument2_get_title(doc, &title);
1776     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1777     ok_(__FILE__,line) (!strcmp_wa(title, extitle), "unexpected title %s\n", dbgstr_w(title));
1778     SysFreeString(title);
1779 }
1780
1781 #define test_doc_set_title(d,t) _test_doc_set_title(__LINE__,d,t)
1782 static void _test_doc_set_title(unsigned line, IHTMLDocument2 *doc, const char *title)
1783 {
1784     BSTR tmp;
1785     HRESULT hres;
1786
1787     tmp = a2bstr(title);
1788     hres = IHTMLDocument2_put_title(doc, tmp);
1789     ok_(__FILE__,line) (hres == S_OK, "get_title failed: %08x\n", hres);
1790     SysFreeString(tmp);
1791 }
1792
1793 #define test_border_styles(p, n) _test_border_styles(__LINE__, p, n)
1794 static void _test_border_styles(unsigned line, IHTMLStyle *pStyle, BSTR Name)
1795 {
1796     HRESULT hres;
1797     DISPID dispid;
1798
1799     hres = IHTMLStyle_GetIDsOfNames(pStyle, &IID_NULL, (LPOLESTR*)&Name, 1,
1800                         LOCALE_USER_DEFAULT, &dispid);
1801     ok_(__FILE__,line) (hres == S_OK, "GetIDsOfNames: %08x\n", hres);
1802     if(hres == S_OK)
1803     {
1804         DISPPARAMS params = {NULL,NULL,0,0};
1805         DISPID dispidNamed = DISPID_PROPERTYPUT;
1806         VARIANT ret;
1807         VARIANT vDefault;
1808         VARIANTARG arg;
1809
1810         hres = IHTMLStyle_Invoke(pStyle, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
1811             DISPATCH_PROPERTYGET, &params, &vDefault, NULL, NULL);
1812         ok_(__FILE__,line) (hres == S_OK, "get_default. ret: %08x\n", hres);
1813
1814         params.cArgs = 1;
1815         params.cNamedArgs = 1;
1816         params.rgdispidNamedArgs = &dispidNamed;
1817         V_VT(&arg) = VT_BSTR;
1818         V_BSTR(&arg) = a2bstr("none");
1819         params.rgvarg = &arg;
1820         hres = IHTMLStyle_Invoke(pStyle, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
1821             DISPATCH_PROPERTYPUT, &params, &ret, NULL, NULL);
1822         ok_(__FILE__,line) (hres == S_OK, "none. ret: %08x\n", hres);
1823         VariantClear(&arg);
1824
1825         V_VT(&arg) = VT_BSTR;
1826         V_BSTR(&arg) = a2bstr("dotted");
1827         hres = IHTMLStyle_Invoke(pStyle, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
1828             DISPATCH_PROPERTYPUT, &params, &ret, NULL, NULL);
1829         ok_(__FILE__,line) (hres == S_OK, "dotted. ret: %08x\n", hres);
1830         VariantClear(&arg);
1831
1832         V_VT(&arg) = VT_BSTR;
1833         V_BSTR(&arg) = a2bstr("dashed");
1834         hres = IHTMLStyle_Invoke(pStyle, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
1835         DISPATCH_PROPERTYPUT, &params, &ret, NULL, NULL);
1836         ok_(__FILE__,line) (hres == S_OK, "dashed. ret: %08x\n", hres);
1837         VariantClear(&arg);
1838
1839         V_VT(&arg) = VT_BSTR;
1840         V_BSTR(&arg) = a2bstr("solid");
1841         hres = IHTMLStyle_Invoke(pStyle, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
1842             DISPATCH_PROPERTYPUT, &params, &ret, NULL, NULL);
1843         ok_(__FILE__,line) (hres == S_OK, "solid. ret: %08x\n", hres);
1844         VariantClear(&arg);
1845
1846         V_VT(&arg) = VT_BSTR;
1847         V_BSTR(&arg) = a2bstr("double");
1848         hres = IHTMLStyle_Invoke(pStyle, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
1849             DISPATCH_PROPERTYPUT, &params, &ret, NULL, NULL);
1850         ok_(__FILE__,line) (hres == S_OK, "double. ret: %08x\n", hres);
1851         VariantClear(&arg);
1852
1853         V_VT(&arg) = VT_BSTR;
1854         V_BSTR(&arg) = a2bstr("groove");
1855         hres = IHTMLStyle_Invoke(pStyle, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
1856             DISPATCH_PROPERTYPUT, &params, &ret, NULL, NULL);
1857         ok_(__FILE__,line) (hres == S_OK, "groove. ret: %08x\n", hres);
1858         VariantClear(&arg);
1859
1860         V_VT(&arg) = VT_BSTR;
1861         V_BSTR(&arg) = a2bstr("ridge");
1862         hres = IHTMLStyle_Invoke(pStyle, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
1863             DISPATCH_PROPERTYPUT, &params, &ret, NULL, NULL);
1864         ok_(__FILE__,line) (hres == S_OK, "ridge. ret: %08x\n", hres);
1865         VariantClear(&arg);
1866
1867         V_VT(&arg) = VT_BSTR;
1868         V_BSTR(&arg) = a2bstr("inset");
1869         hres = IHTMLStyle_Invoke(pStyle, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
1870             DISPATCH_PROPERTYPUT, &params, &ret, NULL, NULL);
1871         ok_(__FILE__,line) (hres == S_OK, "inset. ret: %08x\n", hres);
1872         VariantClear(&arg);
1873
1874         V_VT(&arg) = VT_BSTR;
1875         V_BSTR(&arg) = a2bstr("outset");
1876         hres = IHTMLStyle_Invoke(pStyle, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
1877             DISPATCH_PROPERTYPUT, &params, &ret, NULL, NULL);
1878         ok_(__FILE__,line) (hres == S_OK, "outset. ret: %08x\n", hres);
1879         VariantClear(&arg);
1880
1881         V_VT(&arg) = VT_BSTR;
1882         V_BSTR(&arg) = a2bstr("invalid");
1883         hres = IHTMLStyle_Invoke(pStyle, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
1884             DISPATCH_PROPERTYPUT, &params, &ret, NULL, NULL);
1885         ok_(__FILE__,line) (FAILED(hres), "invalid value passed.\n");
1886         VariantClear(&arg);
1887
1888         params.rgvarg = &vDefault;
1889         hres = IHTMLStyle_Invoke(pStyle, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
1890             DISPATCH_PROPERTYPUT, &params, &ret, NULL, NULL);
1891         ok_(__FILE__,line) (hres == S_OK, "default. ret: %08x\n", hres);
1892     }
1893 }
1894
1895 static void test_elem_col_item(IHTMLElementCollection *col, LPCWSTR n,
1896         const elem_type_t *elem_types, long len)
1897 {
1898     IDispatch *disp;
1899     VARIANT name, index;
1900     DWORD i;
1901     HRESULT hres;
1902
1903     V_VT(&index) = VT_EMPTY;
1904     V_VT(&name) = VT_BSTR;
1905     V_BSTR(&name) = SysAllocString(n);
1906
1907     hres = IHTMLElementCollection_item(col, name, index, &disp);
1908     ok(hres == S_OK, "item failed: %08x\n", hres);
1909
1910     test_elem_collection((IUnknown*)disp, elem_types, len);
1911     IDispatch_Release(disp);
1912     ok(hres == S_OK, "Could not get IHTMLElementCollection interface: %08x\n", hres);
1913     if(hres != S_OK)
1914         goto cleanup;
1915
1916     V_VT(&index) = VT_I4;
1917
1918     for(i=0; i<len; i++) {
1919         V_I4(&index) = i;
1920         disp = (void*)0xdeadbeef;
1921         hres = IHTMLElementCollection_item(col, name, index, &disp);
1922         ok(hres == S_OK, "item failed: %08x\n", hres);
1923         ok(disp != NULL, "disp == NULL\n");
1924         if(FAILED(hres) || !disp)
1925             continue;
1926
1927         test_elem_type((IUnknown*)disp, elem_types[i]);
1928
1929         IDispatch_Release(disp);
1930     }
1931
1932     V_I4(&index) = len;
1933     disp = (void*)0xdeadbeef;
1934     hres = IHTMLElementCollection_item(col, name, index, &disp);
1935     ok(hres == S_OK, "item failed: %08x\n", hres);
1936     ok(disp == NULL, "disp != NULL\n");
1937
1938     V_I4(&index) = -1;
1939     disp = (void*)0xdeadbeef;
1940     hres = IHTMLElementCollection_item(col, name, index, &disp);
1941     ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
1942     ok(disp == NULL, "disp != NULL\n");
1943
1944 cleanup:
1945     SysFreeString(V_BSTR(&name));
1946 }
1947
1948 static IHTMLElement *get_elem_by_id(IHTMLDocument2 *doc, LPCWSTR id, BOOL expect_success)
1949 {
1950     IHTMLElementCollection *col;
1951     IHTMLElement *elem;
1952     IDispatch *disp = (void*)0xdeadbeef;
1953     VARIANT name, index;
1954     HRESULT hres;
1955
1956     hres = IHTMLDocument2_get_all(doc, &col);
1957     ok(hres == S_OK, "get_all failed: %08x\n", hres);
1958     ok(col != NULL, "col == NULL\n");
1959     if(FAILED(hres) || !col)
1960         return NULL;
1961
1962     V_VT(&index) = VT_EMPTY;
1963     V_VT(&name) = VT_BSTR;
1964     V_BSTR(&name) = SysAllocString(id);
1965
1966     hres = IHTMLElementCollection_item(col, name, index, &disp);
1967     IHTMLElementCollection_Release(col);
1968     SysFreeString(V_BSTR(&name));
1969     ok(hres == S_OK, "item failed: %08x\n", hres);
1970     if(!expect_success) {
1971         ok(disp == NULL, "disp != NULL\n");
1972         return NULL;
1973     }
1974
1975     ok(disp != NULL, "disp == NULL\n");
1976     if(!disp)
1977         return NULL;
1978
1979     elem = get_elem_iface((IUnknown*)disp);
1980     IDispatch_Release(disp);
1981
1982     return elem;
1983 }
1984
1985 static IHTMLElement *get_doc_elem_by_id(IHTMLDocument2 *doc, LPCWSTR id)
1986 {
1987     IHTMLDocument3 *doc3;
1988     IHTMLElement *elem;
1989     BSTR tmp;
1990     HRESULT hres;
1991
1992     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
1993     ok(hres == S_OK, "Could not get IHTMLDocument3 iface: %08x\n", hres);
1994
1995     tmp = SysAllocString(id);
1996     hres = IHTMLDocument3_getElementById(doc3, tmp, &elem);
1997     SysFreeString(tmp);
1998     ok(hres == S_OK, "getElementById(%s) failed: %08x\n", dbgstr_w(id), hres);
1999
2000     IHTMLDocument3_Release(doc3);
2001
2002     return elem;
2003 }
2004
2005 static void test_select_elem(IHTMLSelectElement *select)
2006 {
2007     test_select_type(select, "select-one");
2008     test_select_length(select, 2);
2009     test_select_selidx(select, 0);
2010     test_select_put_selidx(select, 1);
2011
2012     test_select_set_value(select, "val1");
2013     test_select_value(select, "val1");
2014
2015     test_select_get_disabled(select, VARIANT_FALSE);
2016     test_select_set_disabled(select, VARIANT_TRUE);
2017     test_select_set_disabled(select, VARIANT_FALSE);
2018 }
2019
2020 static void test_create_option_elem(IHTMLDocument2 *doc)
2021 {
2022     IHTMLOptionElement *option;
2023
2024     option = create_option_elem(doc, "test text", "test value");
2025
2026     test_option_put_text(option, "new text");
2027     test_option_put_value(option, "new value");
2028
2029     IHTMLOptionElement_Release(option);
2030 }
2031
2032 static IHTMLTxtRange *test_create_body_range(IHTMLDocument2 *doc)
2033 {
2034     IHTMLBodyElement *body;
2035     IHTMLTxtRange *range;
2036     IHTMLElement *elem;
2037     HRESULT hres;
2038
2039     hres = IHTMLDocument2_get_body(doc, &elem);
2040     ok(hres == S_OK, "get_body failed: %08x\n", hres);
2041
2042     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
2043     IHTMLElement_Release(elem);
2044
2045     hres = IHTMLBodyElement_createTextRange(body, &range);
2046     IHTMLBodyElement_Release(body);
2047     ok(hres == S_OK, "createTextRange failed: %08x\n", hres);
2048
2049     return range;
2050 }
2051
2052 static void test_txtrange(IHTMLDocument2 *doc)
2053 {
2054     IHTMLTxtRange *body_range, *range, *range2;
2055     IHTMLSelectionObject *selection;
2056     IDispatch *disp_range;
2057     HRESULT hres;
2058
2059     body_range = test_create_body_range(doc);
2060
2061     test_range_text(body_range, "test abc 123\r\nit's text");
2062
2063     hres = IHTMLTxtRange_duplicate(body_range, &range);
2064     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
2065
2066     hres = IHTMLTxtRange_duplicate(body_range, &range2);
2067     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
2068     test_range_isequal(range, range2, VARIANT_TRUE);
2069
2070     test_range_text(range, "test abc 123\r\nit's text");
2071     test_range_text(body_range, "test abc 123\r\nit's text");
2072
2073     test_range_collapse(range, TRUE);
2074     test_range_isequal(range, range2, VARIANT_FALSE);
2075     test_range_inrange(range, range2, VARIANT_FALSE);
2076     test_range_inrange(range2, range, VARIANT_TRUE);
2077     IHTMLTxtRange_Release(range2);
2078
2079     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
2080     test_range_expand(range, wordW, VARIANT_FALSE, "test ");
2081     test_range_move(range, characterW, 2, 2);
2082     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
2083
2084     test_range_collapse(range, FALSE);
2085     test_range_expand(range, wordW, VARIANT_TRUE, "abc ");
2086
2087     test_range_collapse(range, FALSE);
2088     test_range_expand(range, wordW, VARIANT_TRUE, "123");
2089     test_range_expand(range, wordW, VARIANT_FALSE, "123");
2090     test_range_move(range, characterW, 2, 2);
2091     test_range_expand(range, wordW, VARIANT_TRUE, "123");
2092     test_range_moveend(range, characterW, -5, -5);
2093     test_range_text(range, NULL);
2094     test_range_moveend(range, characterW, 3, 3);
2095     test_range_text(range, "c 1");
2096     test_range_expand(range, texteditW, VARIANT_TRUE, "test abc 123\r\nit's text");
2097     test_range_collapse(range, TRUE);
2098     test_range_move(range, characterW, 4, 4);
2099     test_range_moveend(range, characterW, 1, 1);
2100     test_range_text(range, " ");
2101     test_range_move(range, wordW, 1, 1);
2102     test_range_moveend(range, characterW, 2, 2);
2103     test_range_text(range, "ab");
2104
2105     IHTMLTxtRange_Release(range);
2106
2107     hres = IHTMLTxtRange_duplicate(body_range, &range);
2108     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
2109
2110     test_range_text(range, "test abc 123\r\nit's text");
2111     test_range_move(range, characterW, 3, 3);
2112     test_range_moveend(range, characterW, 1, 1);
2113     test_range_text(range, "t");
2114     test_range_moveend(range, characterW, 3, 3);
2115     test_range_text(range, "t ab");
2116     test_range_moveend(range, characterW, -2, -2);
2117     test_range_text(range, "t ");
2118     test_range_move(range, characterW, 6, 6);
2119     test_range_moveend(range, characterW, 3, 3);
2120     test_range_text(range, "123");
2121     test_range_moveend(range, characterW, 2, 2);
2122     test_range_text(range, "123\r\ni");
2123
2124     IHTMLTxtRange_Release(range);
2125
2126     hres = IHTMLTxtRange_duplicate(body_range, &range);
2127     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
2128
2129     test_range_move(range, wordW, 1, 1);
2130     test_range_moveend(range, characterW, 2, 2);
2131     test_range_text(range, "ab");
2132
2133     test_range_move(range, characterW, -2, -2);
2134     test_range_moveend(range, characterW, 2, 2);
2135     test_range_text(range, "t ");
2136
2137     test_range_move(range, wordW, 3, 3);
2138     test_range_move(range, wordW, -2, -2);
2139     test_range_moveend(range, characterW, 2, 2);
2140     test_range_text(range, "ab");
2141
2142     test_range_move(range, characterW, -6, -5);
2143     test_range_moveend(range, characterW, -1, 0);
2144     test_range_moveend(range, characterW, -6, 0);
2145     test_range_move(range, characterW, 2, 2);
2146     test_range_moveend(range, characterW, 2, 2);
2147     test_range_text(range, "st");
2148     test_range_moveend(range, characterW, -6, -4);
2149     test_range_moveend(range, characterW, 2, 2);
2150
2151     IHTMLTxtRange_Release(range);
2152
2153     hres = IHTMLTxtRange_duplicate(body_range, &range);
2154     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
2155
2156     test_range_move(range, wordW, 2, 2);
2157     test_range_moveend(range, characterW, 2, 2);
2158     test_range_text(range, "12");
2159
2160     test_range_move(range, characterW, 15, 14);
2161     test_range_move(range, characterW, -2, -2);
2162     test_range_moveend(range, characterW, 3, 2);
2163     test_range_text(range, "t");
2164     test_range_moveend(range, characterW, -1, -1);
2165     test_range_text(range, "t");
2166     test_range_expand(range, wordW, VARIANT_TRUE, "text");
2167     test_range_move(range, characterW, -2, -2);
2168     test_range_moveend(range, characterW, 2, 2);
2169     test_range_text(range, "s ");
2170     test_range_move(range, characterW, 100, 7);
2171     test_range_move(range, wordW, 1, 0);
2172     test_range_move(range, characterW, -2, -2);
2173     test_range_moveend(range, characterW, 3, 2);
2174     test_range_text(range, "t");
2175
2176     IHTMLTxtRange_Release(range);
2177
2178     hres = IHTMLTxtRange_duplicate(body_range, &range);
2179     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
2180
2181     test_range_collapse(range, TRUE);
2182     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
2183     test_range_put_text(range, "word");
2184     test_range_text(body_range, "wordabc 123\r\nit's text");
2185     test_range_text(range, NULL);
2186     test_range_moveend(range, characterW, 3, 3);
2187     test_range_text(range, "abc");
2188     test_range_movestart(range, characterW, -2, -2);
2189     test_range_text(range, "rdabc");
2190     test_range_movestart(range, characterW, 3, 3);
2191     test_range_text(range, "bc");
2192     test_range_movestart(range, characterW, 4, 4);
2193     test_range_text(range, NULL);
2194     test_range_movestart(range, characterW, -3, -3);
2195     test_range_text(range, "c 1");
2196     test_range_movestart(range, characterW, -7, -6);
2197     test_range_text(range, "wordabc 1");
2198     test_range_movestart(range, characterW, 100, 22);
2199     test_range_text(range, NULL);
2200
2201     IHTMLTxtRange_Release(range);
2202     IHTMLTxtRange_Release(body_range);
2203
2204     hres = IHTMLDocument2_get_selection(doc, &selection);
2205     ok(hres == S_OK, "IHTMLDocument2_get_selection failed: %08x\n", hres);
2206
2207     hres = IHTMLSelectionObject_createRange(selection, &disp_range);
2208     ok(hres == S_OK, "IHTMLSelectionObject_createRange failed: %08x\n", hres);
2209     IHTMLSelectionObject_Release(selection);
2210
2211     hres = IDispatch_QueryInterface(disp_range, &IID_IHTMLTxtRange, (void **)&range);
2212     ok(hres == S_OK, "Could not get IID_IHTMLTxtRange interface: 0x%08x\n", hres);
2213     IDispatch_Release(disp_range);
2214
2215     test_range_text(range, NULL);
2216     test_range_moveend(range, characterW, 3, 3);
2217     test_range_text(range, "wor");
2218     test_range_parent(range, ET_BODY);
2219     test_range_expand(range, texteditW, VARIANT_TRUE, "wordabc 123\r\nit's text");
2220     test_range_expand(range, texteditW, VARIANT_TRUE, "wordabc 123\r\nit's text");
2221     test_range_move(range, characterW, 3, 3);
2222     test_range_expand(range, wordW, VARIANT_TRUE, "wordabc ");
2223     test_range_moveend(range, characterW, -4, -4);
2224     test_range_put_text(range, "abc def ");
2225     test_range_expand(range, texteditW, VARIANT_TRUE, "abc def abc 123\r\nit's text");
2226     test_range_move(range, wordW, 1, 1);
2227     test_range_movestart(range, characterW, -1, -1);
2228     test_range_text(range, " ");
2229     test_range_move(range, wordW, 1, 1);
2230     test_range_moveend(range, characterW, 3, 3);
2231     test_range_text(range, "def");
2232     test_range_put_text(range, "xyz");
2233     test_range_moveend(range, characterW, 1, 1);
2234     test_range_move(range, wordW, 1, 1);
2235     test_range_moveend(range, characterW, 2, 2);
2236     test_range_text(range, "ab");
2237
2238     IHTMLTxtRange_Release(range);
2239 }
2240
2241 static void test_txtrange2(IHTMLDocument2 *doc)
2242 {
2243     IHTMLTxtRange *range;
2244
2245     range = test_create_body_range(doc);
2246
2247     test_range_text(range, "abc\r\n\r\n123\r\n\r\n\r\ndef");
2248     test_range_move(range, characterW, 5, 5);
2249     test_range_moveend(range, characterW, 1, 1);
2250     test_range_text(range, "2");
2251     test_range_move(range, characterW, -3, -3);
2252     test_range_moveend(range, characterW, 3, 3);
2253     test_range_text(range, "c\r\n\r\n1");
2254     test_range_collapse(range, VARIANT_FALSE);
2255     test_range_moveend(range, characterW, 4, 4);
2256     test_range_text(range, "23");
2257     test_range_moveend(range, characterW, 1, 1);
2258     test_range_text(range, "23\r\n\r\n\r\nd");
2259     test_range_moveend(range, characterW, -1, -1);
2260     test_range_text(range, "23");
2261     test_range_moveend(range, characterW, -1, -1);
2262     test_range_text(range, "23");
2263     test_range_moveend(range, characterW, -2, -2);
2264     test_range_text(range, "2");
2265
2266     IHTMLTxtRange_Release(range);
2267 }
2268
2269 static void test_compatmode(IHTMLDocument2 *doc)
2270 {
2271     IHTMLDocument5 *doc5;
2272     BSTR mode;
2273     HRESULT hres;
2274
2275     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument5, (void**)&doc5);
2276     ok(hres == S_OK, "Could not get IHTMLDocument5 interface: %08x\n", hres);
2277     if(FAILED(hres))
2278         return;
2279
2280     hres = IHTMLDocument5_get_compatMode(doc5, &mode);
2281     IHTMLDocument5_Release(doc5);
2282     ok(hres == S_OK, "get_compatMode failed: %08x\n", hres);
2283     ok(!strcmp_wa(mode, "BackCompat"), "compatMode=%s\n", dbgstr_w(mode));
2284     SysFreeString(mode);
2285 }
2286
2287 static void test_location(IHTMLDocument2 *doc)
2288 {
2289     IHTMLLocation *location, *location2;
2290     IHTMLWindow2 *window;
2291     ULONG ref;
2292     HRESULT hres;
2293
2294     hres = IHTMLDocument2_get_location(doc, &location);
2295     ok(hres == S_OK, "get_location failed: %08x\n", hres);
2296
2297     hres = IHTMLDocument2_get_location(doc, &location2);
2298     ok(hres == S_OK, "get_location failed: %08x\n", hres);
2299
2300     ok(location == location2, "location != location2\n");
2301     IHTMLLocation_Release(location2);
2302
2303     hres = IHTMLDocument2_get_parentWindow(doc, &window);
2304     ok(hres == S_OK, "get_parentWindow failed: %08x\n", hres);
2305
2306     hres = IHTMLWindow2_get_location(window, &location2);
2307     ok(hres == S_OK, "get_location failed: %08x\n", hres);
2308     ok(location == location2, "location != location2\n");
2309     IHTMLLocation_Release(location2);
2310
2311     test_ifaces((IUnknown*)location, location_iids);
2312     test_disp((IUnknown*)location, &IID_IHTMLLocation);
2313
2314     ref = IHTMLLocation_Release(location);
2315     ok(!ref, "location chould be destroyed here\n");
2316 }
2317
2318 static void test_navigator(IHTMLDocument2 *doc)
2319 {
2320     IHTMLWindow2 *window;
2321     IOmNavigator *navigator, *navigator2;
2322     ULONG ref;
2323     BSTR bstr;
2324     HRESULT hres;
2325
2326     static const WCHAR v40[] = {'4','.','0'};
2327
2328     hres = IHTMLDocument2_get_parentWindow(doc, &window);
2329     ok(hres == S_OK, "parentWidnow failed: %08x\n", hres);
2330
2331     hres = IHTMLWindow2_get_navigator(window, &navigator);
2332     ok(hres == S_OK, "get_navigator failed: %08x\n", hres);
2333     ok(navigator != NULL, "navigator == NULL\n");
2334     test_disp((IUnknown*)navigator, &IID_IOmNavigator);
2335
2336     hres = IHTMLWindow2_get_navigator(window, &navigator2);
2337     ok(hres == S_OK, "get_navigator failed: %08x\n", hres);
2338     ok(navigator != navigator2, "navigator2 != navihgator\n");
2339
2340     IHTMLWindow2_Release(window);
2341     IOmNavigator_Release(navigator2);
2342
2343     hres = IOmNavigator_get_appCodeName(navigator, &bstr);
2344     ok(hres == S_OK, "get_appCodeName failed: %08x\n", hres);
2345     ok(!strcmp_wa(bstr, "Mozilla"), "Unexpected appCodeName %s\n", dbgstr_w(bstr));
2346     SysFreeString(bstr);
2347
2348     bstr = NULL;
2349     hres = IOmNavigator_get_platform(navigator, &bstr);
2350     ok(hres == S_OK, "get_platform failed: %08x\n", hres);
2351 #ifdef _WIN64
2352     ok(!strcmp_wa(bstr, "Win64"), "unexpected platform %s\n", dbgstr_w(bstr));
2353 #else
2354     ok(!strcmp_wa(bstr, "Win32"), "unexpected platform %s\n", dbgstr_w(bstr));
2355 #endif
2356     SysFreeString(bstr);
2357
2358     bstr = NULL;
2359     hres = IOmNavigator_get_appVersion(navigator, &bstr);
2360     ok(hres == S_OK, "get_appVersion failed: %08x\n", hres);
2361     ok(!memcmp(bstr, v40, sizeof(v40)), "appVersion is %s\n", dbgstr_w(bstr));
2362     SysFreeString(bstr);
2363
2364     ref = IOmNavigator_Release(navigator);
2365     ok(!ref, "navigator should be destroyed here\n");
2366 }
2367
2368 static void test_current_style(IHTMLCurrentStyle *current_style)
2369 {
2370     BSTR str;
2371     HRESULT hres;
2372
2373     test_disp((IUnknown*)current_style, &DIID_DispHTMLCurrentStyle);
2374     test_ifaces((IUnknown*)current_style, cstyle_iids);
2375
2376     hres = IHTMLCurrentStyle_get_display(current_style, &str);
2377     ok(hres == S_OK, "get_display failed: %08x\n", hres);
2378     ok(!strcmp_wa(str, "block"), "get_display returned %s\n", dbgstr_w(str));
2379     SysFreeString(str);
2380 }
2381
2382 static void test_style2(IHTMLStyle2 *style2)
2383 {
2384     BSTR str;
2385     HRESULT hres;
2386
2387     str = (void*)0xdeadbeef;
2388     hres = IHTMLStyle2_get_position(style2, &str);
2389     ok(hres == S_OK, "get_position failed: %08x\n", hres);
2390     ok(!str, "str != NULL\n");
2391
2392     str = a2bstr("absolute");
2393     hres = IHTMLStyle2_put_position(style2, str);
2394     ok(hres == S_OK, "get_position failed: %08x\n", hres);
2395     SysFreeString(str);
2396
2397     str = NULL;
2398     hres = IHTMLStyle2_get_position(style2, &str);
2399     ok(hres == S_OK, "get_position failed: %08x\n", hres);
2400     ok(!strcmp_wa(str, "absolute"), "get_position returned %s\n", dbgstr_w(str));
2401     SysFreeString(str);
2402 }
2403
2404 static void test_default_style(IHTMLStyle *style)
2405 {
2406     IHTMLStyle2 *style2;
2407     VARIANT_BOOL b;
2408     VARIANT v;
2409     BSTR str;
2410     HRESULT hres;
2411     float f;
2412     BSTR sOverflowDefault;
2413     BSTR sDefault;
2414
2415     test_disp((IUnknown*)style, &DIID_DispHTMLStyle);
2416     test_ifaces((IUnknown*)style, style_iids);
2417
2418     hres = IHTMLStyle_get_position(style, &str);
2419     ok(hres == S_OK, "get_position failed: %08x\n", hres);
2420     ok(!str, "str=%s\n", dbgstr_w(str));
2421
2422     str = (void*)0xdeadbeef;
2423     hres = IHTMLStyle_get_fontFamily(style, &str);
2424     ok(hres == S_OK, "get_fontFamily failed: %08x\n", hres);
2425     ok(!str, "fontFamily = %s\n", dbgstr_w(str));
2426
2427     str = (void*)0xdeadbeef;
2428     hres = IHTMLStyle_get_fontWeight(style, &str);
2429     ok(hres == S_OK, "get_fontWeight failed: %08x\n", hres);
2430     ok(!str, "fontWeight = %s\n", dbgstr_w(str));
2431
2432     /* font Variant */
2433     hres = IHTMLStyle_get_fontVariant(style, NULL);
2434     ok(hres == E_INVALIDARG, "get_fontVariant failed: %08x\n", hres);
2435
2436     hres = IHTMLStyle_get_fontVariant(style, &sDefault);
2437     ok(hres == S_OK, "get_fontVariant failed: %08x\n", hres);
2438
2439     str = a2bstr("test");
2440     hres = IHTMLStyle_put_fontVariant(style, str);
2441     ok(hres == E_INVALIDARG, "fontVariant failed: %08x\n", hres);
2442     SysFreeString(str);
2443
2444     str = a2bstr("small-caps");
2445     hres = IHTMLStyle_put_fontVariant(style, str);
2446     ok(hres == S_OK, "fontVariant failed: %08x\n", hres);
2447     SysFreeString(str);
2448
2449     str = a2bstr("normal");
2450     hres = IHTMLStyle_put_fontVariant(style, str);
2451     ok(hres == S_OK, "fontVariant failed: %08x\n", hres);
2452     SysFreeString(str);
2453
2454     hres = IHTMLStyle_put_fontVariant(style, sDefault);
2455     ok(hres == S_OK, "fontVariant failed: %08x\n", hres);
2456     SysFreeString(sDefault);
2457
2458     str = (void*)0xdeadbeef;
2459     hres = IHTMLStyle_get_display(style, &str);
2460     ok(hres == S_OK, "get_display failed: %08x\n", hres);
2461     ok(!str, "display = %s\n", dbgstr_w(str));
2462
2463     str = (void*)0xdeadbeef;
2464     hres = IHTMLStyle_get_visibility(style, &str);
2465     ok(hres == S_OK, "get_visibility failed: %08x\n", hres);
2466     ok(!str, "visibility = %s\n", dbgstr_w(str));
2467
2468     V_VT(&v) = VT_NULL;
2469     hres = IHTMLStyle_get_fontSize(style, &v);
2470     ok(hres == S_OK, "get_fontSize failed: %08x\n", hres);
2471     ok(V_VT(&v) == VT_BSTR, "V_VT(fontSize) = %d\n", V_VT(&v));
2472     ok(!V_BSTR(&v), "V_BSTR(fontSize) = %s\n", dbgstr_w(V_BSTR(&v)));
2473
2474     V_VT(&v) = VT_NULL;
2475     hres = IHTMLStyle_get_color(style, &v);
2476     ok(hres == S_OK, "get_color failed: %08x\n", hres);
2477     ok(V_VT(&v) == VT_BSTR, "V_VT(color) = %d\n", V_VT(&v));
2478     ok(!V_BSTR(&v), "V_BSTR(color) = %s\n", dbgstr_w(V_BSTR(&v)));
2479
2480     b = 0xfefe;
2481     hres = IHTMLStyle_get_textDecorationUnderline(style, &b);
2482     ok(hres == S_OK, "get_textDecorationUnderline failed: %08x\n", hres);
2483     ok(b == VARIANT_FALSE, "textDecorationUnderline = %x\n", b);
2484
2485     b = 0xfefe;
2486     hres = IHTMLStyle_get_textDecorationLineThrough(style, &b);
2487     ok(hres == S_OK, "get_textDecorationLineThrough failed: %08x\n", hres);
2488     ok(b == VARIANT_FALSE, "textDecorationLineThrough = %x\n", b);
2489
2490     hres = IHTMLStyle_get_posWidth(style, NULL);
2491     ok(hres == E_POINTER, "get_posWidth failed: %08x\n", hres);
2492
2493     hres = IHTMLStyle_get_posWidth(style, &f);
2494     ok(hres == S_OK, "get_posWidth failed: %08x\n", hres);
2495     ok(f == 0.0f, "f = %f\n", f);
2496
2497     V_VT(&v) = VT_EMPTY;
2498     hres = IHTMLStyle_get_width(style, &v);
2499     ok(hres == S_OK, "get_width failed: %08x\n", hres);
2500     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2501     ok(!V_BSTR(&v), "V_BSTR(v)=%p\n", V_BSTR(&v));
2502
2503     hres = IHTMLStyle_put_posWidth(style, 2.2);
2504     ok(hres == S_OK, "get_posWidth failed: %08x\n", hres);
2505
2506     hres = IHTMLStyle_get_posWidth(style, &f);
2507     ok(hres == S_OK, "get_posWidth failed: %08x\n", hres);
2508     ok(f == 2.0f, "f = %f\n", f);
2509
2510     V_VT(&v) = VT_BSTR;
2511     V_BSTR(&v) = a2bstr("auto");
2512     hres = IHTMLStyle_put_width(style, v);
2513     ok(hres == S_OK, "put_width failed: %08x\n", hres);
2514     VariantClear(&v);
2515
2516     V_VT(&v) = VT_EMPTY;
2517     hres = IHTMLStyle_get_width(style, &v);
2518     ok(hres == S_OK, "get_width failed: %08x\n", hres);
2519     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2520     ok(!strcmp_wa(V_BSTR(&v), "auto"), "V_BSTR(v)=%s\n", dbgstr_w(V_BSTR(&v)));
2521     VariantClear(&v);
2522
2523     /* margin tests */
2524     str = (void*)0xdeadbeef;
2525     hres = IHTMLStyle_get_margin(style, &str);
2526     ok(hres == S_OK, "get_margin failed: %08x\n", hres);
2527     ok(!str, "margin = %s\n", dbgstr_w(str));
2528
2529     str = a2bstr("1");
2530     hres = IHTMLStyle_put_margin(style, str);
2531     ok(hres == S_OK, "get_margin failed: %08x\n", hres);
2532     SysFreeString(str);
2533
2534     hres = IHTMLStyle_get_margin(style, &str);
2535     ok(hres == S_OK, "get_margin failed: %08x\n", hres);
2536     ok(strcmp_wa(str, "1"), "margin = %s\n", dbgstr_w(str));
2537
2538     hres = IHTMLStyle_put_margin(style, NULL);
2539     ok(hres == S_OK, "get_margin failed: %08x\n", hres);
2540
2541     str = NULL;
2542     hres = IHTMLStyle_get_border(style, &str);
2543     ok(hres == S_OK, "get_border failed: %08x\n", hres);
2544     ok(!str || !*str, "str is not empty\n");
2545     SysFreeString(str);
2546
2547     str = a2bstr("1px");
2548     hres = IHTMLStyle_put_border(style, str);
2549     ok(hres == S_OK, "get_border failed: %08x\n", hres);
2550     SysFreeString(str);
2551
2552     V_VT(&v) = VT_EMPTY;
2553     hres = IHTMLStyle_get_left(style, &v);
2554     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2555     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2556     ok(!V_BSTR(&v), "V_BSTR(v) != NULL\n");
2557     VariantClear(&v);
2558
2559     /* Test posLeft */
2560     hres = IHTMLStyle_get_posLeft(style, NULL);
2561     ok(hres == E_POINTER, "get_left failed: %08x\n", hres);
2562
2563     f = 1.0f;
2564     hres = IHTMLStyle_get_posLeft(style, &f);
2565     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2566     ok(f == 0.0, "expected 0.0 got %f\n", f);
2567
2568     hres = IHTMLStyle_put_posLeft(style, 4.9f);
2569     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2570
2571     hres = IHTMLStyle_get_posLeft(style, &f);
2572     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2573     ok(f == 4.0, "expected 4.0 got %f\n", f);
2574
2575     /* Ensure left is updated correctly. */
2576     V_VT(&v) = VT_EMPTY;
2577     hres = IHTMLStyle_get_left(style, &v);
2578     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2579     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2580     ok(!strcmp_wa(V_BSTR(&v), "4px"), "V_BSTR(v) = %s\n", dbgstr_w(V_BSTR(&v)));
2581     VariantClear(&v);
2582
2583     /* Test left */
2584     V_VT(&v) = VT_BSTR;
2585     V_BSTR(&v) = a2bstr("3px");
2586     hres = IHTMLStyle_put_left(style, v);
2587     ok(hres == S_OK, "put_left failed: %08x\n", hres);
2588     VariantClear(&v);
2589
2590     hres = IHTMLStyle_get_posLeft(style, &f);
2591     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2592     ok(f == 3.0, "expected 3.0 got %f\n", f);
2593
2594     V_VT(&v) = VT_EMPTY;
2595     hres = IHTMLStyle_get_left(style, &v);
2596     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2597     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2598     ok(!strcmp_wa(V_BSTR(&v), "3px"), "V_BSTR(v) = %s\n", dbgstr_w(V_BSTR(&v)));
2599     VariantClear(&v);
2600
2601     V_VT(&v) = VT_NULL;
2602     hres = IHTMLStyle_put_left(style, v);
2603     ok(hres == S_OK, "put_left failed: %08x\n", hres);
2604
2605     V_VT(&v) = VT_EMPTY;
2606     hres = IHTMLStyle_get_left(style, &v);
2607     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2608     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2609     ok(!V_BSTR(&v), "V_BSTR(v) != NULL\n");
2610     VariantClear(&v);
2611
2612     V_VT(&v) = VT_EMPTY;
2613     hres = IHTMLStyle_get_top(style, &v);
2614     ok(hres == S_OK, "get_top failed: %08x\n", hres);
2615     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2616     ok(!V_BSTR(&v), "V_BSTR(v) != NULL\n");
2617     VariantClear(&v);
2618
2619     /* Test posTop */
2620     hres = IHTMLStyle_get_posTop(style, NULL);
2621     ok(hres == E_POINTER, "get_left failed: %08x\n", hres);
2622
2623     f = 1.0f;
2624     hres = IHTMLStyle_get_posTop(style, &f);
2625     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2626     ok(f == 0.0, "expected 0.0 got %f\n", f);
2627
2628     hres = IHTMLStyle_put_posTop(style, 4.9f);
2629     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2630
2631     hres = IHTMLStyle_get_posTop(style, &f);
2632     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2633     ok(f == 4.0, "expected 4.0 got %f\n", f);
2634
2635     V_VT(&v) = VT_BSTR;
2636     V_BSTR(&v) = a2bstr("3px");
2637     hres = IHTMLStyle_put_top(style, v);
2638     ok(hres == S_OK, "put_top failed: %08x\n", hres);
2639     VariantClear(&v);
2640
2641     V_VT(&v) = VT_EMPTY;
2642     hres = IHTMLStyle_get_top(style, &v);
2643     ok(hres == S_OK, "get_top failed: %08x\n", hres);
2644     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2645     ok(!strcmp_wa(V_BSTR(&v), "3px"), "V_BSTR(v) = %s\n", dbgstr_w(V_BSTR(&v)));
2646     VariantClear(&v);
2647
2648     hres = IHTMLStyle_get_posTop(style, &f);
2649     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2650     ok(f == 3.0, "expected 3.0 got %f\n", f);
2651
2652     V_VT(&v) = VT_NULL;
2653     hres = IHTMLStyle_put_top(style, v);
2654     ok(hres == S_OK, "put_top failed: %08x\n", hres);
2655
2656     V_VT(&v) = VT_EMPTY;
2657     hres = IHTMLStyle_get_top(style, &v);
2658     ok(hres == S_OK, "get_top failed: %08x\n", hres);
2659     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2660     ok(!V_BSTR(&v), "V_BSTR(v) != NULL\n");
2661     VariantClear(&v);
2662
2663     /* Test posHeight */
2664     hres = IHTMLStyle_get_posHeight(style, NULL);
2665     ok(hres == E_POINTER, "get_left failed: %08x\n", hres);
2666
2667     V_VT(&v) = VT_EMPTY;
2668     hres = IHTMLStyle_get_height(style, &v);
2669     ok(hres == S_OK, "get_height failed: %08x\n", hres);
2670     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2671     ok(!V_BSTR(&v), "V_BSTR(v) != NULL\n");
2672     VariantClear(&v);
2673
2674     f = 1.0f;
2675     hres = IHTMLStyle_get_posHeight(style, &f);
2676     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2677     ok(f == 0.0, "expected 0.0 got %f\n", f);
2678
2679     hres = IHTMLStyle_put_posHeight(style, 4.9f);
2680     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2681
2682     hres = IHTMLStyle_get_posHeight(style, &f);
2683     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2684     ok(f == 4.0, "expected 4.0 got %f\n", f);
2685
2686     V_VT(&v) = VT_BSTR;
2687     V_BSTR(&v) = a2bstr("64px");
2688     hres = IHTMLStyle_put_height(style, v);
2689     ok(hres == S_OK, "put_height failed: %08x\n", hres);
2690     VariantClear(&v);
2691
2692     V_VT(&v) = VT_EMPTY;
2693     hres = IHTMLStyle_get_height(style, &v);
2694     ok(hres == S_OK, "get_height failed: %08x\n", hres);
2695     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2696     ok(!strcmp_wa(V_BSTR(&v), "64px"), "V_BSTR(v) = %s\n", dbgstr_w(V_BSTR(&v)));
2697     VariantClear(&v);
2698
2699     hres = IHTMLStyle_get_posHeight(style, &f);
2700     ok(hres == S_OK, "get_left failed: %08x\n", hres);
2701     ok(f == 64.0, "expected 64.0 got %f\n", f);
2702
2703     str = (void*)0xdeadbeef;
2704     hres = IHTMLStyle_get_cursor(style, &str);
2705     ok(hres == S_OK, "get_cursor failed: %08x\n", hres);
2706     ok(!str, "get_cursor != NULL\n");
2707     SysFreeString(str);
2708
2709     str = a2bstr("default");
2710     hres = IHTMLStyle_put_cursor(style, str);
2711     ok(hres == S_OK, "get_cursor failed: %08x\n", hres);
2712     SysFreeString(str);
2713
2714     str = NULL;
2715     hres = IHTMLStyle_get_cursor(style, &str);
2716     ok(hres == S_OK, "get_cursor failed: %08x\n", hres);
2717     ok(!strcmp_wa(str, "default"), "get_cursor returned %s\n", dbgstr_w(str));
2718     SysFreeString(str);
2719
2720     V_VT(&v) = VT_EMPTY;
2721     hres = IHTMLStyle_get_verticalAlign(style, &v);
2722     ok(hres == S_OK, "get_vertivalAlign failed: %08x\n", hres);
2723     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2724     ok(!V_BSTR(&v), "V_BSTR(v) != NULL\n");
2725     VariantClear(&v);
2726
2727     V_VT(&v) = VT_BSTR;
2728     V_BSTR(&v) = a2bstr("middle");
2729     hres = IHTMLStyle_put_verticalAlign(style, v);
2730     ok(hres == S_OK, "put_vertivalAlign failed: %08x\n", hres);
2731     VariantClear(&v);
2732
2733     V_VT(&v) = VT_EMPTY;
2734     hres = IHTMLStyle_get_verticalAlign(style, &v);
2735     ok(hres == S_OK, "get_verticalAlign failed: %08x\n", hres);
2736     ok(V_VT(&v) == VT_BSTR, "V_VT(v)=%d\n", V_VT(&v));
2737     ok(!strcmp_wa(V_BSTR(&v), "middle"), "V_BSTR(v) = %s\n", dbgstr_w(V_BSTR(&v)));
2738     VariantClear(&v);
2739
2740     str = (void*)0xdeadbeef;
2741     hres = IHTMLStyle_get_textAlign(style, &str);
2742     ok(hres == S_OK, "get_textAlign failed: %08x\n", hres);
2743     ok(!str, "textAlign != NULL\n");
2744
2745     str = a2bstr("center");
2746     hres = IHTMLStyle_put_textAlign(style, str);
2747     ok(hres == S_OK, "put_textAlign failed: %08x\n", hres);
2748     SysFreeString(str);
2749
2750     str = NULL;
2751     hres = IHTMLStyle_get_textAlign(style, &str);
2752     ok(hres == S_OK, "get_textAlign failed: %08x\n", hres);
2753     ok(!strcmp_wa(str, "center"), "textAlign = %s\n", dbgstr_w(V_BSTR(&v)));
2754     SysFreeString(str);
2755
2756     str = (void*)0xdeadbeef;
2757     hres = IHTMLStyle_get_filter(style, &str);
2758     ok(hres == S_OK, "get_filter failed: %08x\n", hres);
2759     ok(!str, "filter != NULL\n");
2760
2761     str = a2bstr("alpha(opacity=100)");
2762     hres = IHTMLStyle_put_filter(style, str);
2763     ok(hres == S_OK, "put_filter failed: %08x\n", hres);
2764     SysFreeString(str);
2765
2766     V_VT(&v) = VT_EMPTY;
2767     hres = IHTMLStyle_get_zIndex(style, &v);
2768     ok(hres == S_OK, "get_zIndex failed: %08x\n", hres);
2769     ok(V_VT(&v) == VT_I4, "V_VT(v)=%d\n", V_VT(&v));
2770     ok(!V_I4(&v), "V_I4(v) != 0\n");
2771     VariantClear(&v);
2772
2773     V_VT(&v) = VT_BSTR;
2774     V_BSTR(&v) = a2bstr("1");
2775     hres = IHTMLStyle_put_zIndex(style, v);
2776     ok(hres == S_OK, "put_zIndex failed: %08x\n", hres);
2777     VariantClear(&v);
2778
2779     V_VT(&v) = VT_EMPTY;
2780     hres = IHTMLStyle_get_zIndex(style, &v);
2781     ok(hres == S_OK, "get_zIndex failed: %08x\n", hres);
2782     ok(V_VT(&v) == VT_I4, "V_VT(v)=%d\n", V_VT(&v));
2783     ok(V_I4(&v) == 1, "V_I4(v) = %d\n", V_I4(&v));
2784     VariantClear(&v);
2785
2786     /* fontStyle */
2787     hres = IHTMLStyle_get_fontStyle(style, &sDefault);
2788     ok(hres == S_OK, "get_fontStyle failed: %08x\n", hres);
2789
2790     str = a2bstr("test");
2791     hres = IHTMLStyle_put_fontStyle(style, str);
2792     ok(hres == E_INVALIDARG, "put_fontStyle failed: %08x\n", hres);
2793     SysFreeString(str);
2794
2795     str = a2bstr("italic");
2796     hres = IHTMLStyle_put_fontStyle(style, str);
2797     ok(hres == S_OK, "put_fontStyle failed: %08x\n", hres);
2798     SysFreeString(str);
2799
2800     str = a2bstr("oblique");
2801     hres = IHTMLStyle_put_fontStyle(style, str);
2802     ok(hres == S_OK, "put_fontStyle failed: %08x\n", hres);
2803     SysFreeString(str);
2804
2805     str = a2bstr("normal");
2806     hres = IHTMLStyle_put_fontStyle(style, str);
2807     ok(hres == S_OK, "put_fontStyle failed: %08x\n", hres);
2808     SysFreeString(str);
2809
2810     hres = IHTMLStyle_put_fontStyle(style, sDefault);
2811     ok(hres == S_OK, "get_fontStyle failed: %08x\n", hres);
2812
2813     /* overflow */
2814     hres = IHTMLStyle_get_overflow(style, NULL);
2815     ok(hres == E_INVALIDARG, "get_overflow failed: %08x\n", hres);
2816
2817     hres = IHTMLStyle_get_overflow(style, &sOverflowDefault);
2818     ok(hres == S_OK, "get_overflow failed: %08x\n", hres);
2819
2820     str = a2bstr("test");
2821     hres = IHTMLStyle_put_overflow(style, str);
2822     ok(hres == E_INVALIDARG, "put_overflow failed: %08x\n", hres);
2823     SysFreeString(str);
2824
2825     str = a2bstr("visible");
2826     hres = IHTMLStyle_put_overflow(style, str);
2827     ok(hres == S_OK, "put_overflow failed: %08x\n", hres);
2828     SysFreeString(str);
2829
2830     str = a2bstr("scroll");
2831     hres = IHTMLStyle_put_overflow(style, str);
2832     ok(hres == S_OK, "put_overflow failed: %08x\n", hres);
2833     SysFreeString(str);
2834
2835     str = a2bstr("hidden");
2836     hres = IHTMLStyle_put_overflow(style, str);
2837     ok(hres == S_OK, "put_overflow failed: %08x\n", hres);
2838     SysFreeString(str);
2839
2840     str = a2bstr("auto");
2841     hres = IHTMLStyle_put_overflow(style, str);
2842     ok(hres == S_OK, "put_overflow failed: %08x\n", hres);
2843     SysFreeString(str);
2844
2845     hres = IHTMLStyle_get_overflow(style, &str);
2846     ok(hres == S_OK, "get_overflow failed: %08x\n", hres);
2847     ok(!strcmp_wa(str, "auto"), "str=%s\n", dbgstr_w(str));
2848     SysFreeString(str);
2849
2850     /* restore overflow default */
2851     hres = IHTMLStyle_put_overflow(style, sOverflowDefault);
2852     ok(hres == S_OK, "put_overflow failed: %08x\n", hres);
2853     SysFreeString(sOverflowDefault);
2854
2855     /* Attribute Tests*/
2856     hres = IHTMLStyle_getAttribute(style, NULL, 1, &v);
2857     ok(hres == E_INVALIDARG, "getAttribute failed: %08x\n", hres);
2858
2859     str = a2bstr("position");
2860     hres = IHTMLStyle_getAttribute(style, str, 1, NULL);
2861     ok(hres == E_INVALIDARG, "getAttribute failed: %08x\n", hres);
2862
2863     hres = IHTMLStyle_getAttribute(style, str, 1, &v);
2864     ok(hres == S_OK, "getAttribute failed: %08x\n", hres);
2865     ok(V_VT(&v) == VT_BSTR, "type failed: %d\n", V_VT(&v));
2866     VariantClear(&v);
2867
2868     hres = IHTMLStyle_setAttribute(style, NULL, v, 1);
2869     ok(hres == E_INVALIDARG, "getAttribute failed: %08x\n", hres);
2870
2871     V_VT(&v) = VT_BSTR;
2872     V_BSTR(&v) = a2bstr("absolute");
2873     hres = IHTMLStyle_setAttribute(style, str, v, 1);
2874     ok(hres == S_OK, "setAttribute failed: %08x\n", hres);
2875     VariantClear(&v);
2876
2877     hres = IHTMLStyle_getAttribute(style, str, 1, &v);
2878     ok(hres == S_OK, "getAttribute failed: %08x\n", hres);
2879     ok(V_VT(&v) == VT_BSTR, "type failed: %d\n", V_VT(&v));
2880     ok(!strcmp_wa(V_BSTR(&v), "absolute"), "str=%s\n", dbgstr_w(V_BSTR(&v)));
2881     VariantClear(&v);
2882
2883     V_VT(&v) = VT_BSTR;
2884     V_BSTR(&v) = NULL;
2885     hres = IHTMLStyle_setAttribute(style, str, v, 1);
2886     ok(hres == S_OK, "setAttribute failed: %08x\n", hres);
2887     VariantClear(&v);
2888
2889     SysFreeString(str);
2890
2891     str = a2bstr("borderLeftStyle");
2892     test_border_styles(style, str);
2893     SysFreeString(str);
2894
2895     str = a2bstr("borderbottomstyle");
2896     test_border_styles(style, str);
2897     SysFreeString(str);
2898
2899     str = a2bstr("borderrightstyle");
2900     test_border_styles(style, str);
2901     SysFreeString(str);
2902
2903     str = a2bstr("bordertopstyle");
2904     test_border_styles(style, str);
2905     SysFreeString(str);
2906
2907     hres = IHTMLStyle_QueryInterface(style, &IID_IHTMLStyle2, (void**)&style2);
2908     ok(hres == S_OK, "Could not get IHTMLStyle2 iface: %08x\n", hres);
2909     if(SUCCEEDED(hres)) {
2910         test_style2(style2);
2911         IHTMLStyle2_Release(style2);
2912     }
2913 }
2914
2915 static void test_default_selection(IHTMLDocument2 *doc)
2916 {
2917     IHTMLSelectionObject *selection;
2918     IHTMLTxtRange *range;
2919     IDispatch *disp;
2920     BSTR str;
2921     HRESULT hres;
2922
2923     hres = IHTMLDocument2_get_selection(doc, &selection);
2924     ok(hres == S_OK, "get_selection failed: %08x\n", hres);
2925
2926     hres = IHTMLSelectionObject_get_type(selection, &str);
2927     ok(hres == S_OK, "get_type failed: %08x\n", hres);
2928     ok(!lstrcmpW(str, noneW), "type = %s\n", dbgstr_w(str));
2929     SysFreeString(str);
2930
2931     hres = IHTMLSelectionObject_createRange(selection, &disp);
2932     IHTMLSelectionObject_Release(selection);
2933     ok(hres == S_OK, "createRange failed: %08x\n", hres);
2934
2935     hres = IDispatch_QueryInterface(disp, &IID_IHTMLTxtRange, (void**)&range);
2936     IDispatch_Release(disp);
2937     ok(hres == S_OK, "Could not get IHTMLTxtRange interface: %08x\n", hres);
2938
2939     test_range_text(range, NULL);
2940     IHTMLTxtRange_Release(range);
2941 }
2942
2943 static void test_default_body(IHTMLBodyElement *body)
2944 {
2945     long l;
2946     BSTR bstr;
2947     HRESULT hres;
2948     VARIANT v;
2949     WCHAR sBodyText[] = {'#','F','F','0','0','0','0',0};
2950     WCHAR sTextInvalid[] = {'I','n','v','a','l','i','d',0};
2951     WCHAR sResInvalid[] = {'#','0','0','a','0','d','0',0};
2952
2953     bstr = (void*)0xdeadbeef;
2954     hres = IHTMLBodyElement_get_background(body, &bstr);
2955     ok(hres == S_OK, "get_background failed: %08x\n", hres);
2956     ok(bstr == NULL, "bstr != NULL\n");
2957
2958     l = elem_get_scroll_height((IUnknown*)body);
2959     ok(l != -1, "scrollHeight == -1\n");
2960     l = elem_get_scroll_width((IUnknown*)body);
2961     ok(l != -1, "scrollWidth == -1\n");
2962     l = elem_get_scroll_top((IUnknown*)body);
2963     ok(!l, "scrollTop = %ld\n", l);
2964     elem_get_scroll_left((IUnknown*)body);
2965
2966     /* get_text tests */
2967     hres = IHTMLBodyElement_get_text(body, &v);
2968     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2969     ok(V_VT(&v) == VT_BSTR, "Expected VT_BSTR got %d\n", V_VT(&v));
2970     ok(bstr == NULL, "bstr != NULL\n");
2971
2972
2973     /* get_text - Invalid Text */
2974     V_VT(&v) = VT_BSTR;
2975     V_BSTR(&v) = SysAllocString(sTextInvalid);
2976     hres = IHTMLBodyElement_put_text(body, v);
2977     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2978
2979     V_VT(&v) = VT_NULL;
2980     hres = IHTMLBodyElement_get_text(body, &v);
2981     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2982     ok(V_VT(&v) == VT_BSTR, "Expected VT_BSTR got %d\n", V_VT(&v));
2983     ok(!lstrcmpW(sResInvalid, V_BSTR(&v)), "bstr != sResInvalid\n");
2984     VariantClear(&v);
2985
2986     /* get_text - Valid Text */
2987     V_VT(&v) = VT_BSTR;
2988     V_BSTR(&v) = SysAllocString(sBodyText);
2989     hres = IHTMLBodyElement_put_text(body, v);
2990     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2991
2992     V_VT(&v) = VT_NULL;
2993     hres = IHTMLBodyElement_get_text(body, &v);
2994     ok(hres == S_OK, "expect S_OK got 0x%08d\n", hres);
2995     ok(V_VT(&v) == VT_BSTR, "Expected VT_BSTR got %d\n", V_VT(&v));
2996     ok(lstrcmpW(bstr, V_BSTR(&v)), "bstr != V_BSTR(&v)\n");
2997     VariantClear(&v);
2998 }
2999
3000 static void test_body_funs(IHTMLBodyElement *body)
3001 {
3002     static WCHAR sRed[] = {'r','e','d',0};
3003     static WCHAR sRedbg[] = {'#','f','f','0','0','0','0',0};
3004     VARIANT vbg;
3005     VARIANT vDefaultbg;
3006     HRESULT hres;
3007
3008     hres = IHTMLBodyElement_get_bgColor(body, &vDefaultbg);
3009     ok(hres == S_OK, "get_background failed: %08x\n", hres);
3010     ok(V_VT(&vDefaultbg) == VT_BSTR, "bstr != NULL\n");
3011
3012     V_VT(&vbg) = VT_BSTR;
3013     V_BSTR(&vbg) = SysAllocString(sRed);
3014     hres = IHTMLBodyElement_put_bgColor(body, vbg);
3015     ok(hres == S_OK, "get_background failed: %08x\n", hres);
3016     VariantClear(&vbg);
3017
3018     hres = IHTMLBodyElement_get_bgColor(body, &vbg);
3019     ok(hres == S_OK, "get_background failed: %08x\n", hres);
3020     ok(V_VT(&vDefaultbg) == VT_BSTR, "V_VT(&vDefaultbg) != VT_BSTR\n");
3021     ok(!lstrcmpW(V_BSTR(&vbg), sRedbg), "Unexpected type %s\n", dbgstr_w(V_BSTR(&vbg)));
3022     VariantClear(&vbg);
3023
3024     /* Restore Originial */
3025     hres = IHTMLBodyElement_put_bgColor(body, vDefaultbg);
3026     ok(hres == S_OK, "get_background failed: %08x\n", hres);
3027     VariantClear(&vDefaultbg);
3028 }
3029
3030 static void test_window(IHTMLDocument2 *doc)
3031 {
3032     IHTMLWindow2 *window, *window2, *self;
3033     IHTMLDocument2 *doc2 = NULL;
3034     IDispatch *disp;
3035     HRESULT hres;
3036
3037     hres = IHTMLDocument2_get_parentWindow(doc, &window);
3038     ok(hres == S_OK, "get_parentElement failed: %08x\n", hres);
3039     test_ifaces((IUnknown*)window, window_iids);
3040     test_disp((IUnknown*)window, &DIID_DispHTMLWindow2);
3041
3042     hres = IHTMLWindow2_get_document(window, &doc2);
3043     ok(hres == S_OK, "get_document failed: %08x\n", hres);
3044     ok(doc2 != NULL, "doc2 == NULL\n");
3045
3046     IHTMLDocument_Release(doc2);
3047
3048     hres = IHTMLWindow2_get_window(window, &window2);
3049     ok(hres == S_OK, "get_window failed: %08x\n", hres);
3050     ok(window2 != NULL, "window2 == NULL\n");
3051
3052     hres = IHTMLWindow2_get_self(window, &self);
3053     ok(hres == S_OK, "get_window failed: %08x\n", hres);
3054     ok(window2 != NULL, "self == NULL\n");
3055
3056     ok(self == window2, "self != window2\n");
3057
3058     IHTMLWindow2_Release(window2);
3059     IHTMLWindow2_Release(self);
3060
3061     disp = NULL;
3062     hres = IHTMLDocument2_get_Script(doc, &disp);
3063     ok(hres == S_OK, "get_Script failed: %08x\n", hres);
3064     ok(disp == (void*)window, "disp != window\n");
3065     IDispatch_Release(disp);
3066
3067     IHTMLWindow2_Release(window);
3068 }
3069
3070 static void test_defaults(IHTMLDocument2 *doc)
3071 {
3072     IHTMLStyleSheetsCollection *stylesheetcol;
3073     IHTMLCurrentStyle *cstyle;
3074     IHTMLBodyElement *body;
3075     IHTMLElement2 *elem2;
3076     IHTMLElement *elem;
3077     IHTMLStyle *style;
3078     long l;
3079     HRESULT hres;
3080     IHTMLElementCollection *collection;
3081
3082     hres = IHTMLDocument2_get_body(doc, &elem);
3083     ok(hres == S_OK, "get_body failed: %08x\n", hres);
3084
3085     hres = IHTMLDocument2_get_images(doc, NULL);
3086     ok(hres == E_INVALIDARG, "hres %08x\n", hres);
3087
3088     hres = IHTMLDocument2_get_images(doc, &collection);
3089     ok(hres == S_OK, "get_images failed: %08x\n", hres);
3090     if(hres == S_OK)
3091     {
3092         test_elem_collection((IUnknown*)collection, NULL, 0);
3093         IHTMLElementCollection_Release(collection);
3094     }
3095
3096     hres = IHTMLDocument2_get_applets(doc, NULL);
3097     ok(hres == E_INVALIDARG, "hres %08x\n", hres);
3098
3099     hres = IHTMLDocument2_get_applets(doc, &collection);
3100     ok(hres == S_OK, "get_applets failed: %08x\n", hres);
3101     if(hres == S_OK)
3102     {
3103         test_elem_collection((IUnknown*)collection, NULL, 0);
3104         IHTMLElementCollection_Release(collection);
3105     }
3106
3107     hres = IHTMLDocument2_get_links(doc, NULL);
3108     ok(hres == E_INVALIDARG, "hres %08x\n", hres);
3109
3110     hres = IHTMLDocument2_get_links(doc, &collection);
3111     ok(hres == S_OK, "get_links failed: %08x\n", hres);
3112     if(hres == S_OK)
3113     {
3114         test_elem_collection((IUnknown*)collection, NULL, 0);
3115         IHTMLElementCollection_Release(collection);
3116     }
3117
3118     hres = IHTMLDocument2_get_forms(doc, NULL);
3119     ok(hres == E_INVALIDARG, "hres %08x\n", hres);
3120
3121     hres = IHTMLDocument2_get_forms(doc, &collection);
3122     ok(hres == S_OK, "get_forms failed: %08x\n", hres);
3123     if(hres == S_OK)
3124     {
3125         test_elem_collection((IUnknown*)collection, NULL, 0);
3126         IHTMLElementCollection_Release(collection);
3127     }
3128
3129     hres = IHTMLDocument2_get_anchors(doc, NULL);
3130     ok(hres == E_INVALIDARG, "hres %08x\n", hres);
3131
3132     hres = IHTMLDocument2_get_anchors(doc, &collection);
3133     ok(hres == S_OK, "get_anchors failed: %08x\n", hres);
3134     if(hres == S_OK)
3135     {
3136         test_elem_collection((IUnknown*)collection, NULL, 0);
3137         IHTMLElementCollection_Release(collection);
3138     }
3139
3140     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
3141     ok(hres == S_OK, "Could not get IHTMBodyElement: %08x\n", hres);
3142     test_default_body(body);
3143     test_body_funs(body);
3144     IHTMLBodyElement_Release(body);
3145
3146     hres = IHTMLElement_get_style(elem, &style);
3147     ok(hres == S_OK, "get_style failed: %08x\n", hres);
3148
3149     test_default_style(style);
3150     test_window(doc);
3151     test_compatmode(doc);
3152     test_location(doc);
3153     test_navigator(doc);
3154
3155     IHTMLStyle_Release(style);
3156
3157     elem2 = get_elem2_iface((IUnknown*)elem);
3158     hres = IHTMLElement2_get_currentStyle(elem2, &cstyle);
3159     ok(hres == S_OK, "get_currentStyle failed: %08x\n", hres);
3160     if(SUCCEEDED(hres)) {
3161         test_current_style(cstyle);
3162         IHTMLCurrentStyle_Release(cstyle);
3163     }
3164     IHTMLElement2_Release(elem2);
3165
3166     IHTMLElement_Release(elem);
3167
3168     hres = IHTMLDocument2_get_styleSheets(doc, &stylesheetcol);
3169     ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
3170
3171     l = 0xdeadbeef;
3172     hres = IHTMLStyleSheetsCollection_get_length(stylesheetcol, &l);
3173     ok(hres == S_OK, "get_length failed: %08x\n", hres);
3174     ok(l == 0, "length = %ld\n", l);
3175
3176     IHTMLStyleSheetsCollection_Release(stylesheetcol);
3177
3178     test_default_selection(doc);
3179     test_doc_title(doc, "");
3180 }
3181
3182 static void test_tr_elem(IHTMLElement *elem)
3183 {
3184     IHTMLElementCollection *col;
3185     IHTMLTableRow *row;
3186     HRESULT hres;
3187
3188     static const elem_type_t cell_types[] = {ET_TD,ET_TD};
3189
3190     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLTableRow, (void**)&row);
3191     ok(hres == S_OK, "Could not get IHTMLTableRow iface: %08x\n", hres);
3192     if(FAILED(hres))
3193         return;
3194
3195     col = NULL;
3196     hres = IHTMLTableRow_get_cells(row, &col);
3197     ok(hres == S_OK, "get_cells failed: %08x\n", hres);
3198     ok(col != NULL, "get_cells returned NULL\n");
3199
3200     test_elem_collection((IUnknown*)col, cell_types, sizeof(cell_types)/sizeof(*cell_types));
3201     IHTMLElementCollection_Release(col);
3202
3203     IHTMLTable_Release(row);
3204 }
3205
3206 static void test_table_elem(IHTMLElement *elem)
3207 {
3208     IHTMLElementCollection *col;
3209     IHTMLTable *table;
3210     HRESULT hres;
3211
3212     static const elem_type_t row_types[] = {ET_TR,ET_TR};
3213
3214     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLTable, (void**)&table);
3215     ok(hres == S_OK, "Could not get IHTMLTable iface: %08x\n", hres);
3216     if(FAILED(hres))
3217         return;
3218
3219     col = NULL;
3220     hres = IHTMLTable_get_rows(table, &col);
3221     ok(hres == S_OK, "get_rows failed: %08x\n", hres);
3222     ok(col != NULL, "get_ros returned NULL\n");
3223
3224     test_elem_collection((IUnknown*)col, row_types, sizeof(row_types)/sizeof(*row_types));
3225     IHTMLElementCollection_Release(col);
3226
3227     IHTMLTable_Release(table);
3228 }
3229
3230 static void doc_write(IHTMLDocument2 *doc, const char *text)
3231 {
3232     SAFEARRAYBOUND dim;
3233     SAFEARRAY *sa;
3234     VARIANT *var;
3235     BSTR str;
3236     HRESULT hres;
3237
3238     dim.lLbound = 0;
3239     dim.cElements = 1;
3240     sa = SafeArrayCreate(VT_VARIANT, 1, &dim);
3241     SafeArrayAccessData(sa, (void**)&var);
3242     V_VT(var) = VT_BSTR;
3243     V_BSTR(var) = str = a2bstr(text);
3244     SafeArrayUnaccessData(sa);
3245
3246     hres = IHTMLDocument2_write(doc, sa);
3247     ok(hres == S_OK, "write failed: %08x\n", hres);
3248
3249     SysFreeString(str);
3250     SafeArrayDestroy(sa);
3251 }
3252
3253 static void test_iframe_elem(IHTMLElement *elem)
3254 {
3255     IHTMLElementCollection *col;
3256     IHTMLDocument2 *content_doc;
3257     IHTMLWindow2 *content_window;
3258     IHTMLFrameBase2 *base2;
3259     IDispatch *disp;
3260     VARIANT errv;
3261     BSTR str;
3262     HRESULT hres;
3263
3264     static const elem_type_t all_types[] = {
3265         ET_HTML,
3266         ET_HEAD,
3267         ET_TITLE,
3268         ET_BODY,
3269         ET_BR
3270     };
3271
3272     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLFrameBase2, (void**)&base2);
3273     ok(hres == S_OK, "Could not get IHTMFrameBase2 iface: %08x\n", hres);
3274     if (!base2) return;
3275
3276     content_window = NULL;
3277     hres = IHTMLFrameBase2_get_contentWindow(base2, &content_window);
3278     IHTMLFrameBase2_Release(base2);
3279     ok(hres == S_OK, "get_contentWindow failed: %08x\n", hres);
3280     ok(content_window != NULL, "contentWindow = NULL\n");
3281
3282     content_doc = NULL;
3283     hres = IHTMLWindow2_get_document(content_window, &content_doc);
3284     IHTMLWindow2_Release(content_window);
3285     ok(hres == S_OK, "get_document failed: %08x\n", hres);
3286     ok(content_doc != NULL, "content_doc = NULL\n");
3287
3288     str = a2bstr("text/html");
3289     V_VT(&errv) = VT_ERROR;
3290     disp = NULL;
3291     hres = IHTMLDocument2_open(content_doc, str, errv, errv, errv, &disp);
3292     SysFreeString(str);
3293     ok(hres == S_OK, "open failed: %08x\n", hres);
3294     ok(disp != NULL, "disp == NULL\n");
3295     ok(iface_cmp((IUnknown*)disp, (IUnknown*)content_window), "disp != content_window\n");
3296     IDispatch_Release(disp);
3297
3298     doc_write(content_doc, "<html><head><title>test</title></head><body><br /></body></html>");
3299
3300     hres = IHTMLDocument2_get_all(content_doc, &col);
3301     ok(hres == S_OK, "get_all failed: %08x\n", hres);
3302     test_elem_collection((IUnknown*)col, all_types, sizeof(all_types)/sizeof(all_types[0]));
3303     IHTMLElementCollection_Release(col);
3304
3305     hres = IHTMLDocument2_close(content_doc);
3306     ok(hres == S_OK, "close failed: %08x\n", hres);
3307
3308     IHTMLDocument2_Release(content_doc);
3309 }
3310
3311 static void test_stylesheet(IDispatch *disp)
3312 {
3313     IHTMLStyleSheetRulesCollection *col = NULL;
3314     IHTMLStyleSheet *stylesheet;
3315     HRESULT hres;
3316
3317     hres = IDispatch_QueryInterface(disp, &IID_IHTMLStyleSheet, (void**)&stylesheet);
3318     ok(hres == S_OK, "Could not get IHTMLStyleSheet: %08x\n", hres);
3319
3320     hres = IHTMLStyleSheet_get_rules(stylesheet, &col);
3321     ok(hres == S_OK, "get_rules failed: %08x\n", hres);
3322     ok(col != NULL, "col == NULL\n");
3323
3324     IHTMLStyleSheetRulesCollection_Release(col);
3325     IHTMLStyleSheet_Release(stylesheet);
3326 }
3327
3328 static void test_stylesheets(IHTMLDocument2 *doc)
3329 {
3330     IHTMLStyleSheetsCollection *col = NULL;
3331     VARIANT idx, res;
3332     long len = 0;
3333     HRESULT hres;
3334
3335     hres = IHTMLDocument2_get_styleSheets(doc, &col);
3336     ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
3337     ok(col != NULL, "col == NULL\n");
3338
3339     hres = IHTMLStyleSheetsCollection_get_length(col, &len);
3340     ok(hres == S_OK, "get_length failed: %08x\n", hres);
3341     ok(len == 1, "len=%ld\n", len);
3342
3343     VariantInit(&res);
3344     V_VT(&idx) = VT_I4;
3345     V_I4(&idx) = 0;
3346
3347     hres = IHTMLStyleSheetsCollection_item(col, &idx, &res);
3348     ok(hres == S_OK, "item failed: %08x\n", hres);
3349     ok(V_VT(&res) == VT_DISPATCH, "V_VT(res) = %d\n", V_VT(&res));
3350     ok(V_DISPATCH(&res) != NULL, "V_DISPATCH(&res) == NULL\n");
3351     test_stylesheet(V_DISPATCH(&res));
3352     VariantClear(&res);
3353
3354     V_VT(&res) = VT_I4;
3355     V_VT(&idx) = VT_I4;
3356     V_I4(&idx) = 1;
3357
3358     hres = IHTMLStyleSheetsCollection_item(col, &idx, &res);
3359     ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
3360     ok(V_VT(&res) == VT_EMPTY, "V_VT(res) = %d\n", V_VT(&res));
3361     ok(V_DISPATCH(&res) != NULL, "V_DISPATCH(&res) == NULL\n");
3362     VariantClear(&res);
3363
3364     IHTMLStyleSheetsCollection_Release(col);
3365 }
3366
3367 static void test_child_col_disp(IHTMLDOMChildrenCollection *col)
3368 {
3369     IDispatchEx *dispex;
3370     IHTMLDOMNode *node;
3371     DISPPARAMS dp = {NULL, NULL, 0, 0};
3372     VARIANT var;
3373     EXCEPINFO ei;
3374     long type;
3375     DISPID id;
3376     BSTR bstr;
3377     HRESULT hres;
3378
3379     static const WCHAR w0[] = {'0',0};
3380     static const WCHAR w100[] = {'1','0','0',0};
3381
3382     hres = IHTMLDOMChildrenCollection_QueryInterface(col, &IID_IDispatchEx, (void**)&dispex);
3383     ok(hres == S_OK, "Could not get IDispatchEx: %08x\n", hres);
3384
3385     bstr = SysAllocString(w0);
3386     hres = IDispatchEx_GetDispID(dispex, bstr, fdexNameCaseSensitive, &id);
3387     ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
3388     SysFreeString(bstr);
3389
3390     VariantInit(&var);
3391     hres = IDispatchEx_InvokeEx(dispex, id, LOCALE_NEUTRAL, INVOKE_PROPERTYGET, &dp, &var, &ei, NULL);
3392     ok(hres == S_OK, "InvokeEx failed: %08x\n", hres);
3393     ok(V_VT(&var) == VT_DISPATCH, "V_VT(var)=%d\n", V_VT(&var));
3394     ok(V_DISPATCH(&var) != NULL, "V_DISPATCH(var) == NULL\n");
3395     node = get_node_iface((IUnknown*)V_DISPATCH(&var));
3396     type = get_node_type((IUnknown*)node);
3397     ok(type == 3, "type=%ld\n", type);
3398     IHTMLDOMNode_Release(node);
3399     VariantClear(&var);
3400
3401     bstr = SysAllocString(w100);
3402     hres = IDispatchEx_GetDispID(dispex, bstr, fdexNameCaseSensitive, &id);
3403     ok(hres == DISP_E_UNKNOWNNAME, "GetDispID failed: %08x, expected DISP_E_UNKNOWNNAME\n", hres);
3404     SysFreeString(bstr);
3405
3406     IDispatchEx_Release(dispex);
3407 }
3408
3409
3410
3411 static void test_elems(IHTMLDocument2 *doc)
3412 {
3413     IHTMLElementCollection *col;
3414     IHTMLDOMChildrenCollection *child_col;
3415     IHTMLElement *elem, *elem2, *elem3;
3416     IHTMLDOMNode *node, *node2;
3417     IDispatch *disp;
3418     long type;
3419     HRESULT hres;
3420     IHTMLElementCollection *collection;
3421
3422     static const WCHAR imgidW[] = {'i','m','g','i','d',0};
3423     static const WCHAR inW[] = {'i','n',0};
3424     static const WCHAR xW[] = {'x',0};
3425     static const WCHAR sW[] = {'s',0};
3426     static const WCHAR scW[] = {'s','c',0};
3427     static const WCHAR xxxW[] = {'x','x','x',0};
3428     static const WCHAR tblW[] = {'t','b','l',0};
3429     static const WCHAR row2W[] = {'r','o','w','2',0};
3430     static const WCHAR ifrW[] = {'i','f','r',0};
3431
3432     static const elem_type_t all_types[] = {
3433         ET_HTML,
3434         ET_HEAD,
3435         ET_TITLE,
3436         ET_STYLE,
3437         ET_BODY,
3438         ET_COMMENT,
3439         ET_A,
3440         ET_INPUT,
3441         ET_SELECT,
3442         ET_OPTION,
3443         ET_OPTION,
3444         ET_TEXTAREA,
3445         ET_TABLE,
3446         ET_TBODY,
3447         ET_TR,
3448         ET_TR,
3449         ET_TD,
3450         ET_TD,
3451         ET_SCRIPT,
3452         ET_TEST,
3453         ET_IMG,
3454         ET_IFRAME
3455     };
3456
3457     static const elem_type_t item_types[] = {
3458         ET_A,
3459         ET_OPTION,
3460         ET_TEXTAREA
3461     };
3462
3463     hres = IHTMLDocument2_get_all(doc, &col);
3464     ok(hres == S_OK, "get_all failed: %08x\n", hres);
3465     test_elem_collection((IUnknown*)col, all_types, sizeof(all_types)/sizeof(all_types[0]));
3466     test_elem_col_item(col, xW, item_types, sizeof(item_types)/sizeof(item_types[0]));
3467     IHTMLElementCollection_Release(col);
3468
3469     hres = IHTMLDocument2_get_images(doc, &collection);
3470     ok(hres == S_OK, "get_images failed: %08x\n", hres);
3471     if(hres == S_OK)
3472     {
3473         static const elem_type_t images_types[] = {ET_IMG};
3474         test_elem_collection((IUnknown*)collection, images_types, 1);
3475
3476         IHTMLElementCollection_Release(collection);
3477     }
3478
3479     hres = IHTMLDocument2_get_links(doc, &collection);
3480     ok(hres == S_OK, "get_links failed: %08x\n", hres);
3481     if(hres == S_OK)
3482     {
3483         static const elem_type_t images_types[] = {ET_A};
3484         test_elem_collection((IUnknown*)collection, images_types, 1);
3485
3486         IHTMLElementCollection_Release(collection);
3487     }
3488
3489     hres = IHTMLDocument2_get_anchors(doc, &collection);
3490     ok(hres == S_OK, "get_anchors failed: %08x\n", hres);
3491     if(hres == S_OK)
3492     {
3493         static const elem_type_t anchor_types[] = {ET_A};
3494         test_elem_collection((IUnknown*)collection, anchor_types, 1);
3495
3496         IHTMLElementCollection_Release(collection);
3497     }
3498
3499     elem = get_doc_elem(doc);
3500     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
3501     hres = IHTMLElement_get_all(elem, &disp);
3502     IHTMLElement_Release(elem);
3503     ok(hres == S_OK, "get_all failed: %08x\n", hres);
3504
3505     hres = IDispatch_QueryInterface(disp, &IID_IHTMLElementCollection, (void**)&col);
3506     IDispatch_Release(disp);
3507     ok(hres == S_OK, "Could not get IHTMLElementCollection: %08x\n", hres);
3508     test_elem_collection((IUnknown*)col, all_types+1, sizeof(all_types)/sizeof(all_types[0])-1);
3509     IHTMLElementCollection_Release(col);
3510
3511     get_elem_by_id(doc, xxxW, FALSE);
3512     elem = get_doc_elem_by_id(doc, xxxW);
3513     ok(!elem, "elem != NULL\n");
3514
3515     elem = get_doc_elem_by_id(doc, sW);
3516     ok(elem != NULL, "elem == NULL\n");
3517     if(elem) {
3518         test_elem_type((IUnknown*)elem, ET_SELECT);
3519         test_elem_attr(elem, xxxW, NULL);
3520         test_elem_attr(elem, idW, sW);
3521         test_elem_class((IUnknown*)elem, NULL);
3522         test_elem_set_class((IUnknown*)elem, "cl");
3523         test_elem_set_class((IUnknown*)elem, NULL);
3524         test_elem_tabindex((IUnknown*)elem, 0);
3525         test_elem_set_tabindex((IUnknown*)elem, 1);
3526
3527         node = test_node_get_parent((IUnknown*)elem);
3528         ok(node != NULL, "node == NULL\n");
3529         test_node_name((IUnknown*)node, "BODY");
3530         node2 = test_node_get_parent((IUnknown*)node);
3531         IHTMLDOMNode_Release(node);
3532         ok(node2 != NULL, "node == NULL\n");
3533         test_node_name((IUnknown*)node2, "HTML");
3534         node = test_node_get_parent((IUnknown*)node2);
3535         IHTMLDOMNode_Release(node2);
3536         ok(node != NULL, "node == NULL\n");
3537         if (node)
3538         {
3539             test_node_name((IUnknown*)node, "#document");
3540             type = get_node_type((IUnknown*)node);
3541             ok(type == 9, "type=%ld, expected 9\n", type);
3542             node2 = test_node_get_parent((IUnknown*)node);
3543             IHTMLDOMNode_Release(node);
3544             ok(node2 == NULL, "node != NULL\n");
3545         }
3546
3547         elem2 = test_elem_get_parent((IUnknown*)elem);
3548         ok(elem2 != NULL, "elem2 == NULL\n");
3549         test_node_name((IUnknown*)elem2, "BODY");
3550         elem3 = test_elem_get_parent((IUnknown*)elem2);
3551         IHTMLElement_Release(elem2);
3552         ok(elem3 != NULL, "elem3 == NULL\n");
3553         test_node_name((IUnknown*)elem3, "HTML");
3554         elem2 = test_elem_get_parent((IUnknown*)elem3);
3555         IHTMLElement_Release(elem3);
3556         ok(elem2 == NULL, "elem2 != NULL\n");
3557
3558         test_elem_getelembytag((IUnknown*)elem, ET_OPTION, 2);
3559         test_elem_getelembytag((IUnknown*)elem, ET_SELECT, 0);
3560         test_elem_getelembytag((IUnknown*)elem, ET_HTML, 0);
3561
3562         test_elem_innertext(elem, "opt1opt2");
3563
3564         IHTMLElement_Release(elem);
3565     }
3566
3567     elem = get_elem_by_id(doc, sW, TRUE);
3568     if(elem) {
3569         IHTMLSelectElement *select;
3570
3571         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLSelectElement, (void**)&select);
3572         ok(hres == S_OK, "Could not get IHTMLSelectElement interface: %08x\n", hres);
3573
3574         test_select_elem(select);
3575
3576         test_elem_title((IUnknown*)select, NULL);
3577         test_elem_set_title((IUnknown*)select, "Title");
3578         test_elem_title((IUnknown*)select, "Title");
3579         test_elem_offset((IUnknown*)select);
3580
3581         node = get_first_child((IUnknown*)select);
3582         ok(node != NULL, "node == NULL\n");
3583         if(node) {
3584             test_elem_type((IUnknown*)node, ET_OPTION);
3585             IHTMLDOMNode_Release(node);
3586         }
3587
3588         type = get_node_type((IUnknown*)select);
3589         ok(type == 1, "type=%ld\n", type);
3590
3591         IHTMLSelectElement_Release(select);
3592
3593         hres = IHTMLElement_get_document(elem, &disp);
3594         ok(hres == S_OK, "get_document failed: %08x\n", hres);
3595         ok(iface_cmp((IUnknown*)disp, (IUnknown*)doc), "disp != doc\n");
3596
3597         IHTMLElement_Release(elem);
3598     }
3599
3600     elem = get_elem_by_id(doc, scW, TRUE);
3601     if(elem) {
3602         IHTMLScriptElement *script;
3603         BSTR type;
3604
3605         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLScriptElement, (void**)&script);
3606         ok(hres == S_OK, "Could not get IHTMLScriptElement interface: %08x\n", hres);
3607
3608         if(hres == S_OK)
3609         {
3610             VARIANT_BOOL vb;
3611
3612             hres = IHTMLScriptElement_get_type(script, &type);
3613             ok(hres == S_OK, "get_type failed: %08x\n", hres);
3614             ok(!lstrcmpW(type, text_javascriptW), "Unexpected type %s\n", dbgstr_w(type));
3615             SysFreeString(type);
3616
3617             /* test defer */
3618             hres = IHTMLScriptElement_put_defer(script, VARIANT_TRUE);
3619             ok(hres == S_OK, "get_type failed: %08x\n", hres);
3620
3621             hres = IHTMLScriptElement_get_defer(script, &vb);
3622             ok(hres == S_OK, "get_type failed: %08x\n", hres);
3623             ok(vb == VARIANT_TRUE, "get_type failed: %08x\n", hres);
3624
3625             hres = IHTMLScriptElement_put_defer(script, VARIANT_FALSE);
3626             ok(hres == S_OK, "get_type failed: %08x\n", hres);
3627         }
3628
3629         IHTMLScriptElement_Release(script);
3630     }
3631
3632     elem = get_elem_by_id(doc, inW, TRUE);
3633     if(elem) {
3634         IHTMLInputElement *input;
3635
3636         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLInputElement, (void**)&input);
3637         ok(hres == S_OK, "Could not get IHTMLInputElement: %08x\n", hres);
3638
3639         test_elem_id((IUnknown*)elem, "in");
3640         test_elem_put_id((IUnknown*)elem, "newin");
3641         test_input_get_disabled(input, VARIANT_FALSE);
3642         test_input_set_disabled(input, VARIANT_TRUE);
3643         test_input_set_disabled(input, VARIANT_FALSE);
3644         test_elem3_set_disabled((IUnknown*)input, VARIANT_TRUE);
3645         test_input_get_disabled(input, VARIANT_TRUE);
3646         test_elem3_set_disabled((IUnknown*)input, VARIANT_FALSE);
3647         test_input_get_disabled(input, VARIANT_FALSE);
3648         test_elem_client_size((IUnknown*)elem);
3649
3650         test_node_get_value_str((IUnknown*)elem, NULL);
3651         test_node_put_value_str((IUnknown*)elem, "test");
3652         test_node_get_value_str((IUnknown*)elem, NULL);
3653         test_input_value((IUnknown*)elem, NULL);
3654         test_input_put_value((IUnknown*)elem, "test");
3655         test_input_value((IUnknown*)elem, NULL);
3656         test_elem_class((IUnknown*)elem, "testclass");
3657         test_elem_tabindex((IUnknown*)elem, 2);
3658         test_elem_set_tabindex((IUnknown*)elem, 3);
3659         test_elem_title((IUnknown*)elem, "test title");
3660
3661         test_input_get_defaultchecked(input, VARIANT_FALSE);
3662         test_input_set_defaultchecked(input, VARIANT_TRUE);
3663         test_input_set_defaultchecked(input, VARIANT_FALSE);
3664
3665         test_input_get_checked(input, VARIANT_FALSE);
3666         test_input_set_checked(input, VARIANT_TRUE);
3667         test_input_set_checked(input, VARIANT_FALSE);
3668
3669         IHTMLInputElement_Release(input);
3670         IHTMLElement_Release(elem);
3671     }
3672
3673     elem = get_elem_by_id(doc, imgidW, TRUE);
3674     if(elem) {
3675         test_img_src((IUnknown*)elem, "");
3676         test_img_set_src((IUnknown*)elem, "about:blank");
3677         test_img_alt((IUnknown*)elem, NULL);
3678         test_img_set_alt((IUnknown*)elem, "alt test");
3679         IHTMLElement_Release(elem);
3680     }
3681
3682     elem = get_doc_elem_by_id(doc, tblW);
3683     ok(elem != NULL, "elem == NULL\n");
3684     if(elem) {
3685         test_table_elem(elem);
3686         IHTMLElement_Release(elem);
3687     }
3688
3689     elem = get_doc_elem_by_id(doc, row2W);
3690     ok(elem != NULL, "elem == NULL\n");
3691     if(elem) {
3692         test_tr_elem(elem);
3693         IHTMLElement_Release(elem);
3694     }
3695
3696     elem = get_doc_elem_by_id(doc, ifrW);
3697     ok(elem != NULL, "elem == NULL\n");
3698     if(elem) {
3699         test_iframe_elem(elem);
3700         IHTMLElement_Release(elem);
3701     }
3702
3703     hres = IHTMLDocument2_get_body(doc, &elem);
3704     ok(hres == S_OK, "get_body failed: %08x\n", hres);
3705
3706     node = get_first_child((IUnknown*)elem);
3707     ok(node != NULL, "node == NULL\n");
3708     if(node) {
3709         test_ifaces((IUnknown*)node, text_iids);
3710         test_disp((IUnknown*)node, &DIID_DispHTMLDOMTextNode);
3711
3712         node2 = get_first_child((IUnknown*)node);
3713         ok(!node2, "node2 != NULL\n");
3714
3715         type = get_node_type((IUnknown*)node);
3716         ok(type == 3, "type=%ld\n", type);
3717
3718         test_node_get_value_str((IUnknown*)node, "text test");
3719         test_node_put_value_str((IUnknown*)elem, "test text");
3720         test_node_get_value_str((IUnknown*)node, "text test");
3721
3722         IHTMLDOMNode_Release(node);
3723     }
3724
3725     child_col = get_child_nodes((IUnknown*)elem);
3726     ok(child_col != NULL, "child_coll == NULL\n");
3727     if(child_col) {
3728         long length = 0;
3729
3730         test_disp((IUnknown*)child_col, &DIID_DispDOMChildrenCollection);
3731
3732         hres = IHTMLDOMChildrenCollection_get_length(child_col, &length);
3733         ok(hres == S_OK, "get_length failed: %08x\n", hres);
3734         ok(length, "length=0\n");
3735
3736         node = get_child_item(child_col, 0);
3737         ok(node != NULL, "node == NULL\n");
3738         if(node) {
3739             type = get_node_type((IUnknown*)node);
3740             ok(type == 3, "type=%ld\n", type);
3741             IHTMLDOMNode_Release(node);
3742         }
3743
3744         node = get_child_item(child_col, 1);
3745         ok(node != NULL, "node == NULL\n");
3746         if(node) {
3747             type = get_node_type((IUnknown*)node);
3748             ok(type == 8, "type=%ld\n", type);
3749
3750             test_elem_id((IUnknown*)node, NULL);
3751             IHTMLDOMNode_Release(node);
3752         }
3753
3754         disp = (void*)0xdeadbeef;
3755         hres = IHTMLDOMChildrenCollection_item(child_col, 6000, &disp);
3756         ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
3757         ok(disp == (void*)0xdeadbeef, "disp=%p\n", disp);
3758
3759         disp = (void*)0xdeadbeef;
3760         hres = IHTMLDOMChildrenCollection_item(child_col, length, &disp);
3761         ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
3762         ok(disp == (void*)0xdeadbeef, "disp=%p\n", disp);
3763
3764         test_child_col_disp(child_col);
3765
3766         IHTMLDOMChildrenCollection_Release(child_col);
3767     }
3768
3769     test_elem3_get_disabled((IUnknown*)elem, VARIANT_FALSE);
3770     test_elem3_set_disabled((IUnknown*)elem, VARIANT_TRUE);
3771     test_elem3_set_disabled((IUnknown*)elem, VARIANT_FALSE);
3772
3773     IHTMLElement_Release(elem);
3774
3775     test_stylesheets(doc);
3776     test_create_option_elem(doc);
3777
3778     elem = get_doc_elem_by_id(doc, tblW);
3779     ok(elem != NULL, "elem = NULL\n");
3780     test_elem_set_innertext(elem, "inner text");
3781     IHTMLElement_Release(elem);
3782
3783     test_doc_title(doc, "test");
3784     test_doc_set_title(doc, "test title");
3785     test_doc_title(doc, "test title");
3786
3787     disp = NULL;
3788     hres = IHTMLDocument2_get_Script(doc, &disp);
3789     ok(hres == S_OK, "get_Script failed: %08x\n", hres);
3790     if(hres == S_OK)
3791     {
3792         IDispatchEx *dispex;
3793         hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
3794         ok(hres == S_OK, "IDispatch_QueryInterface failed: %08x\n", hres);
3795         if(hres == S_OK)
3796         {
3797             DISPID pid = -1;
3798             BSTR str = a2bstr("Testing");
3799             hres = IDispatchEx_GetDispID(dispex, str, 1, &pid);
3800             todo_wine ok(hres == S_OK, "GetDispID failed: %08x\n", hres);
3801             todo_wine ok(pid != -1, "pid == -1\n");
3802             SysFreeString(str);
3803             IDispatchEx_Release(dispex);
3804         }
3805     }
3806     IDispatch_Release(disp);
3807 }
3808
3809 static void test_create_elems(IHTMLDocument2 *doc)
3810 {
3811     IHTMLElement *elem, *body, *elem2;
3812     IHTMLDOMNode *node, *node2, *node3, *comment;
3813     IHTMLDocument5 *doc5;
3814     IDispatch *disp;
3815     VARIANT var;
3816     long type;
3817     HRESULT hres;
3818     BSTR str;
3819
3820     static const elem_type_t types1[] = { ET_TESTG };
3821
3822     elem = test_create_elem(doc, "TEST");
3823     test_elem_tag((IUnknown*)elem, "TEST");
3824     type = get_node_type((IUnknown*)elem);
3825     ok(type == 1, "type=%ld\n", type);
3826     test_ifaces((IUnknown*)elem, elem_iids);
3827     test_disp((IUnknown*)elem, &DIID_DispHTMLGenericElement);
3828
3829     hres = IHTMLDocument2_get_body(doc, &body);
3830     ok(hres == S_OK, "get_body failed: %08x\n", hres);
3831     test_node_has_child((IUnknown*)body, VARIANT_FALSE);
3832
3833     node = test_node_append_child((IUnknown*)body, (IUnknown*)elem);
3834     test_node_has_child((IUnknown*)body, VARIANT_TRUE);
3835     elem2 = get_elem_iface((IUnknown*)node);
3836     IHTMLElement_Release(elem2);
3837
3838     hres = IHTMLElement_get_all(body, &disp);
3839     ok(hres == S_OK, "get_all failed: %08x\n", hres);
3840     test_elem_collection((IUnknown*)disp, types1, sizeof(types1)/sizeof(types1[0]));
3841     IDispatch_Release(disp);
3842
3843     test_node_remove_child((IUnknown*)body, node);
3844
3845     hres = IHTMLElement_get_all(body, &disp);
3846     ok(hres == S_OK, "get_all failed: %08x\n", hres);
3847     test_elem_collection((IUnknown*)disp, NULL, 0);
3848     IDispatch_Release(disp);
3849     test_node_has_child((IUnknown*)body, VARIANT_FALSE);
3850
3851     IHTMLElement_Release(elem);
3852     IHTMLDOMNode_Release(node);
3853
3854     node = test_create_text(doc, "test");
3855     test_ifaces((IUnknown*)node, text_iids);
3856     test_disp((IUnknown*)node, &DIID_DispHTMLDOMTextNode);
3857
3858     V_VT(&var) = VT_NULL;
3859     node2 = test_node_insertbefore((IUnknown*)body, node, &var);
3860     IHTMLDOMNode_Release(node);
3861
3862     node = test_create_text(doc, "insert ");
3863
3864     V_VT(&var) = VT_DISPATCH;
3865     V_DISPATCH(&var) = (IDispatch*)node2;
3866     node3 = test_node_insertbefore((IUnknown*)body, node, &var);
3867     IHTMLDOMNode_Release(node);
3868     IHTMLDOMNode_Release(node2);
3869     IHTMLDOMNode_Release(node3);
3870
3871     test_elem_innertext(body, "insert test");
3872
3873     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument5, (void**)&doc5);
3874     if(hres == S_OK)
3875     {
3876         str = a2bstr("testing");
3877         hres = IHTMLDocument5_createComment(doc5, str, &comment);
3878         SysFreeString(str);
3879         ok(hres == S_OK, "createComment failed: %08x\n", hres);
3880         if(hres == S_OK)
3881         {
3882             type = get_node_type((IUnknown*)comment);
3883             ok(type == 8, "type=%ld, expected 8\n", type);
3884
3885             test_node_get_value_str((IUnknown*)comment, "testing");
3886
3887             IHTMLDOMNode_Release(comment);
3888         }
3889
3890         IHTMLDocument5_Release(doc5);
3891     }
3892
3893     IHTMLElement_Release(body);
3894 }
3895
3896 static void test_exec(IUnknown *unk, const GUID *grpid, DWORD cmdid, VARIANT *in, VARIANT *out)
3897 {
3898     IOleCommandTarget *cmdtrg;
3899     HRESULT hres;
3900
3901     hres = IHTMLTxtRange_QueryInterface(unk, &IID_IOleCommandTarget, (void**)&cmdtrg);
3902     ok(hres == S_OK, "Could not get IOleCommandTarget interface: %08x\n", hres);
3903
3904     hres = IOleCommandTarget_Exec(cmdtrg, grpid, cmdid, 0, in, out);
3905     ok(hres == S_OK, "Exec failed: %08x\n", hres);
3906
3907     IOleCommandTarget_Release(cmdtrg);
3908 }
3909
3910 static void test_indent(IHTMLDocument2 *doc)
3911 {
3912     IHTMLElementCollection *col;
3913     IHTMLTxtRange *range;
3914     HRESULT hres;
3915
3916     static const elem_type_t all_types[] = {
3917         ET_HTML,
3918         ET_HEAD,
3919         ET_TITLE,
3920         ET_BODY,
3921         ET_BR,
3922         ET_A,
3923     };
3924
3925     static const elem_type_t indent_types[] = {
3926         ET_HTML,
3927         ET_HEAD,
3928         ET_TITLE,
3929         ET_BODY,
3930         ET_BLOCKQUOTE,
3931         ET_P,
3932         ET_BR,
3933         ET_A,
3934     };
3935
3936     hres = IHTMLDocument2_get_all(doc, &col);
3937     ok(hres == S_OK, "get_all failed: %08x\n", hres);
3938     test_elem_collection((IUnknown*)col, all_types, sizeof(all_types)/sizeof(all_types[0]));
3939     IHTMLElementCollection_Release(col);
3940
3941     range = test_create_body_range(doc);
3942     test_exec((IUnknown*)range, &CGID_MSHTML, IDM_INDENT, NULL, NULL);
3943     IHTMLTxtRange_Release(range);
3944
3945     hres = IHTMLDocument2_get_all(doc, &col);
3946     ok(hres == S_OK, "get_all failed: %08x\n", hres);
3947     test_elem_collection((IUnknown*)col, indent_types, sizeof(indent_types)/sizeof(indent_types[0]));
3948     IHTMLElementCollection_Release(col);
3949 }
3950
3951 static void test_cond_comment(IHTMLDocument2 *doc)
3952 {
3953     IHTMLElementCollection *col;
3954     HRESULT hres;
3955
3956     static const elem_type_t all_types[] = {
3957         ET_HTML,
3958         ET_HEAD,
3959         ET_TITLE,
3960         ET_BODY,
3961         ET_BR
3962     };
3963
3964     hres = IHTMLDocument2_get_all(doc, &col);
3965     ok(hres == S_OK, "get_all failed: %08x\n", hres);
3966     test_elem_collection((IUnknown*)col, all_types, sizeof(all_types)/sizeof(all_types[0]));
3967     IHTMLElementCollection_Release(col);
3968 }
3969
3970 static IHTMLDocument2 *notif_doc;
3971 static BOOL doc_complete;
3972
3973 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
3974         REFIID riid, void**ppv)
3975 {
3976     if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
3977         *ppv = iface;
3978         return S_OK;
3979     }
3980
3981     ok(0, "unexpected call\n");
3982     return E_NOINTERFACE;
3983 }
3984
3985 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
3986 {
3987     return 2;
3988 }
3989
3990 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
3991 {
3992     return 1;
3993 }
3994
3995 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
3996 {
3997     if(dispID == DISPID_READYSTATE){
3998         BSTR state;
3999         HRESULT hres;
4000
4001         static const WCHAR completeW[] = {'c','o','m','p','l','e','t','e',0};
4002
4003         hres = IHTMLDocument2_get_readyState(notif_doc, &state);
4004         ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
4005
4006         if(!lstrcmpW(state, completeW))
4007             doc_complete = TRUE;
4008
4009         SysFreeString(state);
4010     }
4011
4012     return S_OK;
4013 }
4014
4015 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
4016 {
4017     ok(0, "unexpected call\n");
4018     return E_NOTIMPL;
4019 }
4020
4021 static IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
4022     PropertyNotifySink_QueryInterface,
4023     PropertyNotifySink_AddRef,
4024     PropertyNotifySink_Release,
4025     PropertyNotifySink_OnChanged,
4026     PropertyNotifySink_OnRequestEdit
4027 };
4028
4029 static IPropertyNotifySink PropertyNotifySink = { &PropertyNotifySinkVtbl };
4030
4031 static IHTMLDocument2 *create_doc_with_string(const char *str)
4032 {
4033     IPersistStreamInit *init;
4034     IStream *stream;
4035     IHTMLDocument2 *doc;
4036     HGLOBAL mem;
4037     SIZE_T len;
4038
4039     notif_doc = doc = create_document();
4040     if(!doc)
4041         return NULL;
4042
4043     doc_complete = FALSE;
4044     len = strlen(str);
4045     mem = GlobalAlloc(0, len);
4046     memcpy(mem, str, len);
4047     CreateStreamOnHGlobal(mem, TRUE, &stream);
4048
4049     IHTMLDocument2_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&init);
4050
4051     IPersistStreamInit_Load(init, stream);
4052     IPersistStreamInit_Release(init);
4053     IStream_Release(stream);
4054
4055     return doc;
4056 }
4057
4058 static void do_advise(IUnknown *unk, REFIID riid, IUnknown *unk_advise)
4059 {
4060     IConnectionPointContainer *container;
4061     IConnectionPoint *cp;
4062     DWORD cookie;
4063     HRESULT hres;
4064
4065     hres = IUnknown_QueryInterface(unk, &IID_IConnectionPointContainer, (void**)&container);
4066     ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
4067
4068     hres = IConnectionPointContainer_FindConnectionPoint(container, riid, &cp);
4069     IConnectionPointContainer_Release(container);
4070     ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
4071
4072     hres = IConnectionPoint_Advise(cp, unk_advise, &cookie);
4073     IConnectionPoint_Release(cp);
4074     ok(hres == S_OK, "Advise failed: %08x\n", hres);
4075 }
4076
4077 typedef void (*domtest_t)(IHTMLDocument2*);
4078
4079 static void run_domtest(const char *str, domtest_t test)
4080 {
4081     IHTMLDocument2 *doc;
4082     IHTMLElement *body = NULL;
4083     ULONG ref;
4084     MSG msg;
4085     HRESULT hres;
4086
4087     doc = create_doc_with_string(str);
4088     if(!doc)
4089         return;
4090
4091     do_advise((IUnknown*)doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
4092
4093     while(!doc_complete && GetMessage(&msg, NULL, 0, 0)) {
4094         TranslateMessage(&msg);
4095         DispatchMessage(&msg);
4096     }
4097
4098     hres = IHTMLDocument2_get_body(doc, &body);
4099     ok(hres == S_OK, "get_body failed: %08x\n", hres);
4100
4101     if(body) {
4102         IHTMLElement_Release(body);
4103         test(doc);
4104     }else {
4105         skip("Could not get document body. Assuming no Gecko installed.\n");
4106     }
4107
4108     ref = IHTMLDocument2_Release(doc);
4109     ok(!ref ||
4110        ref == 1, /* Vista */
4111        "ref = %d\n", ref);
4112 }
4113
4114 static void gecko_installer_workaround(BOOL disable)
4115 {
4116     HKEY hkey;
4117     DWORD res;
4118
4119     static BOOL has_url = FALSE;
4120     static char url[2048];
4121
4122     if(!disable && !has_url)
4123         return;
4124
4125     res = RegOpenKey(HKEY_CURRENT_USER, "Software\\Wine\\MSHTML", &hkey);
4126     if(res != ERROR_SUCCESS)
4127         return;
4128
4129     if(disable) {
4130         DWORD type, size = sizeof(url);
4131
4132         res = RegQueryValueEx(hkey, "GeckoUrl", NULL, &type, (PVOID)url, &size);
4133         if(res == ERROR_SUCCESS && type == REG_SZ)
4134             has_url = TRUE;
4135
4136         RegDeleteValue(hkey, "GeckoUrl");
4137     }else {
4138         RegSetValueEx(hkey, "GeckoUrl", 0, REG_SZ, (PVOID)url, lstrlenA(url)+1);
4139     }
4140
4141     RegCloseKey(hkey);
4142 }
4143
4144 /* Check if Internet Explorer is configured to run in "Enhanced Security Configuration" (aka hardened mode) */
4145 /* Note: this code is duplicated in dlls/mshtml/tests/dom.c, dlls/mshtml/tests/script.c and dlls/urlmon/tests/misc.c */
4146 static BOOL is_ie_hardened()
4147 {
4148     HKEY zone_map;
4149     DWORD ie_harden, type, size;
4150
4151     ie_harden = 0;
4152     if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap",
4153                     0, KEY_QUERY_VALUE, &zone_map) == ERROR_SUCCESS) {
4154         size = sizeof(DWORD);
4155         if (RegQueryValueEx(zone_map, "IEHarden", NULL, &type, (LPBYTE) &ie_harden, &size) != ERROR_SUCCESS ||
4156             type != REG_DWORD) {
4157             ie_harden = 0;
4158         }
4159     RegCloseKey(zone_map);
4160     }
4161
4162     return ie_harden != 0;
4163 }
4164
4165 START_TEST(dom)
4166 {
4167     gecko_installer_workaround(TRUE);
4168     CoInitialize(NULL);
4169
4170     run_domtest(doc_str1, test_doc_elem);
4171     run_domtest(range_test_str, test_txtrange);
4172     run_domtest(range_test2_str, test_txtrange2);
4173     if (winetest_interactive || ! is_ie_hardened()) {
4174         run_domtest(elem_test_str, test_elems);
4175     }else {
4176         skip("IE running in Enhanced Security Configuration\n");
4177     }
4178     run_domtest(doc_blank, test_create_elems);
4179     run_domtest(doc_blank, test_defaults);
4180     run_domtest(indent_test_str, test_indent);
4181     run_domtest(cond_comment_str, test_cond_comment);
4182
4183     CoUninitialize();
4184     gecko_installer_workaround(FALSE);
4185 }