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