dinput: Silence incorrect warning and move it to a valid place.
[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
34 static const char doc_blank[] = "<html></html>";
35 static const char doc_str1[] = "<html><body>test</body></html>";
36 static const char doc_str2[] =
37     "<html><body>test \na<font size=\"2\">bc\t123<br /> it's\r\n  \t</font>text<br /></body></html>";
38 static const char elem_test_str[] =
39     "<html><head><title>test</title><style>.body { margin-right: 0px; }</style>"
40     "<body><a href=\"http://test\" name=\"x\">link</a><input />"
41     "<select id=\"s\"><option id=\"x\">opt1</option><option id=\"y\">opt2</option></select>"
42     "<textarea id=\"X\">text text</textarea>"
43     "<table><tbody></tbody></table>"
44     "</body></html>";
45 static const char indent_test_str[] =
46     "<html><head><title>test</title></head><body>abc<br /><a href=\"about:blank\">123</a></body></html>";
47
48 static const WCHAR noneW[] = {'N','o','n','e',0};
49
50 static WCHAR characterW[] = {'c','h','a','r','a','c','t','e','r',0};
51 static WCHAR texteditW[] = {'t','e','x','t','e','d','i','t',0};
52 static WCHAR wordW[] = {'w','o','r','d',0};
53
54 typedef enum {
55     ET_NONE,
56     ET_HTML,
57     ET_HEAD,
58     ET_TITLE,
59     ET_BODY,
60     ET_A,
61     ET_INPUT,
62     ET_SELECT,
63     ET_TEXTAREA,
64     ET_OPTION,
65     ET_STYLE,
66     ET_BLOCKQUOTE,
67     ET_P,
68     ET_BR,
69     ET_TABLE,
70     ET_TBODY
71 } elem_type_t;
72
73 static REFIID const none_iids[] = {
74     &IID_IUnknown,
75     NULL
76 };
77
78 static REFIID const elem_iids[] = {
79     &IID_IHTMLDOMNode,
80     &IID_IHTMLElement,
81     &IID_IHTMLElement2,
82     &IID_IConnectionPointContainer,
83     NULL
84 };
85
86 static REFIID const body_iids[] = {
87     &IID_IHTMLDOMNode,
88     &IID_IHTMLElement,
89     &IID_IHTMLElement2,
90     &IID_IHTMLTextContainer,
91     &IID_IHTMLBodyElement,
92     &IID_IConnectionPointContainer,
93     NULL
94 };
95
96 static REFIID const anchor_iids[] = {
97     &IID_IHTMLDOMNode,
98     &IID_IHTMLElement,
99     &IID_IHTMLElement2,
100     &IID_IHTMLAnchorElement,
101     &IID_IConnectionPointContainer,
102     NULL
103 };
104
105 static REFIID const input_iids[] = {
106     &IID_IHTMLDOMNode,
107     &IID_IHTMLElement,
108     &IID_IHTMLElement2,
109     &IID_IHTMLInputElement,
110     &IID_IHTMLInputTextElement,
111     &IID_IConnectionPointContainer,
112     NULL
113 };
114
115 static REFIID const select_iids[] = {
116     &IID_IHTMLDOMNode,
117     &IID_IHTMLElement,
118     &IID_IHTMLElement2,
119     &IID_IHTMLSelectElement,
120     &IID_IConnectionPointContainer,
121     NULL
122 };
123
124 static REFIID const textarea_iids[] = {
125     &IID_IHTMLDOMNode,
126     &IID_IHTMLElement,
127     &IID_IHTMLElement2,
128     &IID_IHTMLTextAreaElement,
129     &IID_IConnectionPointContainer,
130     NULL
131 };
132
133 static REFIID const option_iids[] = {
134     &IID_IHTMLDOMNode,
135     &IID_IHTMLElement,
136     &IID_IHTMLElement2,
137     &IID_IHTMLOptionElement,
138     &IID_IConnectionPointContainer,
139     NULL
140 };
141
142 static REFIID const table_iids[] = {
143     &IID_IHTMLDOMNode,
144     &IID_IHTMLElement,
145     &IID_IHTMLElement2,
146     &IID_IHTMLTable,
147     &IID_IConnectionPointContainer,
148     NULL
149 };
150
151 typedef struct {
152     const char *tag;
153     REFIID *iids;
154 } elem_type_info_t;
155
156 static const elem_type_info_t elem_type_infos[] = {
157     {"",          none_iids},
158     {"HTML",      elem_iids},
159     {"HEAD",      elem_iids},
160     {"TITLE",     elem_iids},
161     {"BODY",      body_iids},
162     {"A",         anchor_iids},
163     {"INPUT",     input_iids},
164     {"SELECT",    select_iids},
165     {"TEXTAREA",  textarea_iids},
166     {"OPTION",    option_iids},
167     {"STYLE",     elem_iids},
168     {"BLOCKQUOTE",elem_iids},
169     {"P",         elem_iids},
170     {"BR",        elem_iids},
171     {"TABLE",     table_iids},
172     {"TBODY",     elem_iids}
173 };
174
175 static const char *dbgstr_w(LPCWSTR str)
176 {
177     static char buf[512];
178     if(!str)
179         return "(null)";
180     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
181     return buf;
182 }
183
184 static const char *dbgstr_guid(REFIID riid)
185 {
186     static char buf[50];
187
188     sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
189             riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
190             riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
191             riid->Data4[5], riid->Data4[6], riid->Data4[7]);
192
193     return buf;
194 }
195
196 static int strcmp_wa(LPCWSTR strw, const char *stra)
197 {
198     WCHAR buf[512];
199     MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
200     return lstrcmpW(strw, buf);
201 }
202
203 static BSTR a2bstr(const char *str)
204 {
205     BSTR ret;
206     int len;
207
208     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
209     ret = SysAllocStringLen(NULL, len);
210     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
211
212     return ret;
213 }
214
215 static IHTMLDocument2 *create_document(void)
216 {
217     IHTMLDocument2 *doc;
218     HRESULT hres;
219
220     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
221             &IID_IHTMLDocument2, (void**)&doc);
222     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
223
224     return doc;
225 }
226
227 #define test_ifaces(i,ids) _test_ifaces(__LINE__,i,ids)
228 static void _test_ifaces(unsigned line, IUnknown *iface, REFIID *iids)
229 {
230     const IID * const *piid;
231     IUnknown *unk;
232     HRESULT hres;
233
234      for(piid = iids; *piid; piid++) {
235         hres = IDispatch_QueryInterface(iface, *piid, (void**)&unk);
236         ok_(__FILE__,line) (hres == S_OK, "Could not get %s interface: %08x\n", dbgstr_guid(*piid), hres);
237         if(SUCCEEDED(hres))
238             IUnknown_Release(unk);
239     }
240 }
241
242 #define test_node_name(u,n) _test_node_name(__LINE__,u,n)
243 static void _test_node_name(unsigned line, IUnknown *unk, const char *exname)
244 {
245     IHTMLDOMNode *node;
246     BSTR name;
247     HRESULT hres;
248
249     hres = IUnknown_QueryInterface(unk, &IID_IHTMLDOMNode, (void**)&node);
250     ok_(__FILE__, line) (hres == S_OK, "QueryInterface(IID_IHTMLNode) failed: %08x\n", hres);
251
252     hres = IHTMLDOMNode_get_nodeName(node, &name);
253     IHTMLDOMNode_Release(node);
254     ok_(__FILE__, line) (hres == S_OK, "get_nodeName failed: %08x\n", hres);
255     ok_(__FILE__, line) (!strcmp_wa(name, exname), "got name: %s, expected %s\n", dbgstr_w(name), exname);
256
257     SysFreeString(name);
258 }
259
260 #define test_elem_tag(u,n) _test_elem_tag(__LINE__,u,n)
261 static void _test_elem_tag(unsigned line, IUnknown *unk, const char *extag)
262 {
263     IHTMLElement *elem;
264     BSTR tag;
265     HRESULT hres;
266
267     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement, (void**)&elem);
268     ok_(__FILE__, line) (hres == S_OK, "QueryInterface(IID_IHTMLElement) failed: %08x\n", hres);
269
270     hres = IHTMLElement_get_tagName(elem, &tag);
271     IHTMLElement_Release(elem);
272     ok_(__FILE__, line) (hres == S_OK, "get_tagName failed: %08x\n", hres);
273     ok_(__FILE__, line) (!strcmp_wa(tag, extag), "got tag: %s, expected %s\n", dbgstr_w(tag), extag);
274
275     SysFreeString(tag);
276 }
277
278 #define test_elem_type(ifc,t) _test_elem_type(__LINE__,ifc,t)
279 static void _test_elem_type(unsigned line, IUnknown *unk, elem_type_t type)
280 {
281     _test_elem_tag(line, unk, elem_type_infos[type].tag);
282     _test_ifaces(line, unk, elem_type_infos[type].iids);
283 }
284
285 static void test_doc_elem(IHTMLDocument2 *doc)
286 {
287     IHTMLElement *elem;
288     IHTMLDocument3 *doc3;
289     HRESULT hres;
290
291     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
292     ok(hres == S_OK, "QueryInterface(IID_IHTMLDocument3) failed: %08x\n", hres);
293
294     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
295     IHTMLDocument3_Release(doc3);
296     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
297
298     test_node_name((IUnknown*)elem, "HTML");
299     test_elem_tag((IUnknown*)elem, "HTML");
300
301     IHTMLElement_Release(elem);
302 }
303
304 #define get_doc_elem(d) _get_doc_elem(__LINE__,d)
305 static IHTMLElement *_get_doc_elem(unsigned line, IHTMLDocument2 *doc)
306 {
307     IHTMLElement *elem;
308     IHTMLDocument3 *doc3;
309     HRESULT hres;
310
311     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
312     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDocument3 interface: %08x\n", hres);
313     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
314     ok_(__FILE__,line) (hres == S_OK, "get_documentElement failed: %08x\n", hres);
315     IHTMLDocument3_Release(doc3);
316
317     return elem;
318 }
319
320 #define test_option_text(o,t) _test_option_text(__LINE__,o,t)
321 static void _test_option_text(unsigned line, IHTMLOptionElement *option, const char *text)
322 {
323     BSTR bstr;
324     HRESULT hres;
325
326     hres = IHTMLOptionElement_get_text(option, &bstr);
327     ok_(__FILE__,line) (hres == S_OK, "get_text failed: %08x\n", hres);
328     ok_(__FILE__,line) (!strcmp_wa(bstr, text), "text=%s\n", dbgstr_w(bstr));
329     SysFreeString(bstr);
330 }
331
332 #define test_option_put_text(o,t) _test_option_put_text(__LINE__,o,t)
333 static void _test_option_put_text(unsigned line, IHTMLOptionElement *option, const char *text)
334 {
335     BSTR bstr;
336     HRESULT hres;
337
338     bstr = a2bstr(text);
339     hres = IHTMLOptionElement_put_text(option, bstr);
340     SysFreeString(bstr);
341     ok(hres == S_OK, "put_text failed: %08x\n", hres);
342
343     _test_option_text(line, option, text);
344 }
345
346 #define test_option_value(o,t) _test_option_value(__LINE__,o,t)
347 static void _test_option_value(unsigned line, IHTMLOptionElement *option, const char *value)
348 {
349     BSTR bstr;
350     HRESULT hres;
351
352     hres = IHTMLOptionElement_get_value(option, &bstr);
353     ok_(__FILE__,line) (hres == S_OK, "get_value failed: %08x\n", hres);
354     ok_(__FILE__,line) (!strcmp_wa(bstr, value), "value=%s\n", dbgstr_w(bstr));
355     SysFreeString(bstr);
356 }
357
358 #define test_option_put_value(o,t) _test_option_put_value(__LINE__,o,t)
359 static void _test_option_put_value(unsigned line, IHTMLOptionElement *option, const char *value)
360 {
361     BSTR bstr;
362     HRESULT hres;
363
364     bstr = a2bstr(value);
365     hres = IHTMLOptionElement_put_value(option, bstr);
366     SysFreeString(bstr);
367     ok(hres == S_OK, "put_value failed: %08x\n", hres);
368
369     _test_option_value(line, option, value);
370 }
371
372 #define create_option_elem(d,t,v) _create_option_elem(__LINE__,d,t,v)
373 static IHTMLOptionElement *_create_option_elem(unsigned line, IHTMLDocument2 *doc,
374         const char *txt, const char *val)
375 {
376     IHTMLOptionElementFactory *factory;
377     IHTMLOptionElement *option;
378     IHTMLWindow2 *window;
379     VARIANT text, value, empty;
380     HRESULT hres;
381
382     hres = IHTMLDocument2_get_parentWindow(doc, &window);
383     ok_(__FILE__,line) (hres == S_OK, "get_parentElement failed: %08x\n", hres);
384
385     hres = IHTMLWindow2_get_Option(window, &factory);
386     IHTMLWindow2_Release(window);
387     ok_(__FILE__,line) (hres == S_OK, "get_Option failed: %08x\n", hres);
388
389     V_VT(&text) = VT_BSTR;
390     V_BSTR(&text) = a2bstr(txt);
391     V_VT(&value) = VT_BSTR;
392     V_BSTR(&value) = a2bstr(val);
393     V_VT(&empty) = VT_EMPTY;
394
395     hres = IHTMLOptionElementFactory_create(factory, text, value, empty, empty, &option);
396     ok_(__FILE__,line) (hres == S_OK, "create failed: %08x\n", hres);
397
398     IHTMLOptionElementFactory_Release(factory);
399     VariantClear(&text);
400     VariantClear(&value);
401
402     _test_option_text(line, option, txt);
403     _test_option_value(line, option, val);
404
405     return option;
406 }
407
408 #define test_select_length(s,l) _test_select_length(__LINE__,s,l)
409 static void _test_select_length(unsigned line, IHTMLSelectElement *select, long length)
410 {
411     long len = 0xdeadbeef;
412     HRESULT hres;
413
414     hres = IHTMLSelectElement_get_length(select, &len);
415     ok_(__FILE__,line) (hres == S_OK, "get_length failed: %08x\n", hres);
416     ok_(__FILE__,line) (len == length, "len=%ld, expected %ld\n", len, length);
417 }
418
419 #define test_select_selidx(s,i) _test_select_selidx(__LINE__,s,i)
420 static void _test_select_selidx(unsigned line, IHTMLSelectElement *select, long index)
421 {
422     long idx = 0xdeadbeef;
423     HRESULT hres;
424
425     hres = IHTMLSelectElement_get_selectedIndex(select, &idx);
426     ok_(__FILE__,line) (hres == S_OK, "get_selectedIndex failed: %08x\n", hres);
427     ok_(__FILE__,line) (idx == index, "idx=%ld, expected %ld\n", idx, index);
428 }
429
430 #define test_select_put_selidx(s,i) _test_select_put_selidx(__LINE__,s,i)
431 static void _test_select_put_selidx(unsigned line, IHTMLSelectElement *select, long index)
432 {
433     HRESULT hres;
434
435     hres = IHTMLSelectElement_put_selectedIndex(select, index);
436     ok_(__FILE__,line) (hres == S_OK, "get_selectedIndex failed: %08x\n", hres);
437     _test_select_selidx(line, select, index);
438 }
439
440 #define test_range_text(r,t) _test_range_text(__LINE__,r,t)
441 static void _test_range_text(unsigned line, IHTMLTxtRange *range, const char *extext)
442 {
443     BSTR text;
444     HRESULT hres;
445
446     hres = IHTMLTxtRange_get_text(range, &text);
447     ok_(__FILE__, line) (hres == S_OK, "get_text failed: %08x\n", hres);
448
449     if(extext) {
450         ok_(__FILE__, line) (text != NULL, "text == NULL\n");
451         ok_(__FILE__, line) (!strcmp_wa(text, extext), "text=\"%s\", expected \"%s\"\n", dbgstr_w(text), extext);
452     }else {
453         ok_(__FILE__, line) (text == NULL, "text=\"%s\", expected NULL\n", dbgstr_w(text));
454     }
455
456     SysFreeString(text);
457
458 }
459
460 #define test_range_collapse(r,b) _test_range_collapse(__LINE__,r,b)
461 static void _test_range_collapse(unsigned line, IHTMLTxtRange *range, BOOL b)
462 {
463     HRESULT hres;
464
465     hres = IHTMLTxtRange_collapse(range, b);
466     ok_(__FILE__, line) (hres == S_OK, "collapse failed: %08x\n", hres);
467     _test_range_text(line, range, NULL);
468 }
469
470 #define test_range_expand(r,u,b,t) _test_range_expand(__LINE__,r,u,b,t)
471 static void _test_range_expand(unsigned line, IHTMLTxtRange *range, LPWSTR unit,
472         VARIANT_BOOL exb, const char *extext)
473 {
474     VARIANT_BOOL b = 0xe0e0;
475     HRESULT hres;
476
477     hres = IHTMLTxtRange_expand(range, unit, &b);
478     ok_(__FILE__,line) (hres == S_OK, "expand failed: %08x\n", hres);
479     ok_(__FILE__,line) (b == exb, "b=%x, expected %x\n", b, exb);
480     _test_range_text(line, range, extext);
481 }
482
483 #define test_range_move(r,u,c,e) _test_range_move(__LINE__,r,u,c,e)
484 static void _test_range_move(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
485 {
486     long c = 0xdeadbeef;
487     HRESULT hres;
488
489     hres = IHTMLTxtRange_move(range, unit, cnt, &c);
490     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
491     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
492     _test_range_text(line, range, NULL);
493 }
494
495 #define test_range_movestart(r,u,c,e) _test_range_movestart(__LINE__,r,u,c,e)
496 static void _test_range_movestart(unsigned line, IHTMLTxtRange *range,
497         LPWSTR unit, long cnt, long excnt)
498 {
499     long c = 0xdeadbeef;
500     HRESULT hres;
501
502     hres = IHTMLTxtRange_moveStart(range, unit, cnt, &c);
503     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
504     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
505 }
506
507 #define test_range_moveend(r,u,c,e) _test_range_moveend(__LINE__,r,u,c,e)
508 static void _test_range_moveend(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
509 {
510     long c = 0xdeadbeef;
511     HRESULT hres;
512
513     hres = IHTMLTxtRange_moveEnd(range, unit, cnt, &c);
514     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
515     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
516 }
517
518 #define test_range_put_text(r,t) _test_range_put_text(__LINE__,r,t)
519 static void _test_range_put_text(unsigned line, IHTMLTxtRange *range, const char *text)
520 {
521     HRESULT hres;
522     BSTR bstr = a2bstr(text);
523
524     hres = IHTMLTxtRange_put_text(range, bstr);
525     ok_(__FILE__,line) (hres == S_OK, "put_text failed: %08x\n", hres);
526     SysFreeString(bstr);
527     _test_range_text(line, range, NULL);
528 }
529
530 #define test_range_inrange(r1,r2,b) _test_range_inrange(__LINE__,r1,r2,b)
531 static void _test_range_inrange(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
532 {
533     VARIANT_BOOL b;
534     HRESULT hres;
535
536     b = 0xe0e0;
537     hres = IHTMLTxtRange_inRange(range1, range2, &b);
538     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
539     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
540 }
541
542 #define test_range_isequal(r1,r2,b) _test_range_isequal(__LINE__,r1,r2,b)
543 static void _test_range_isequal(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
544 {
545     VARIANT_BOOL b;
546     HRESULT hres;
547
548     b = 0xe0e0;
549     hres = IHTMLTxtRange_isEqual(range1, range2, &b);
550     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
551     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
552
553     b = 0xe0e0;
554     hres = IHTMLTxtRange_isEqual(range2, range1, &b);
555     ok_(__FILE__,line) (hres == S_OK, "(2->1) isEqual failed: %08x\n", hres);
556     ok_(__FILE__,line) (b == exb, "(2->1) b=%x, expected %x\n", b, exb);
557
558     if(exb) {
559         test_range_inrange(range1, range2, VARIANT_TRUE);
560         test_range_inrange(range2, range1, VARIANT_TRUE);
561     }
562 }
563
564 #define test_range_parent(r,t) _test_range_parent(__LINE__,r,t)
565 static void _test_range_parent(unsigned line, IHTMLTxtRange *range, elem_type_t type)
566 {
567     IHTMLElement *elem;
568     HRESULT hres;
569
570     hres = IHTMLTxtRange_parentElement(range, &elem);
571     ok_(__FILE__,line) (hres == S_OK, "parentElement failed: %08x\n", hres);
572
573     _test_elem_type(line, (IUnknown*)elem, type);
574
575     IHTMLElement_Release(elem);
576 }
577
578 #define test_elem_collection(c,t,l) _test_elem_collection(__LINE__,c,t,l)
579 static void _test_elem_collection(unsigned line, IHTMLElementCollection *col,
580         const elem_type_t *elem_types, long exlen)
581 {
582     long len;
583     DWORD i;
584     VARIANT name, index;
585     IDispatch *disp;
586     HRESULT hres;
587
588     hres = IHTMLElementCollection_get_length(col, &len);
589     ok_(__FILE__,line) (hres == S_OK, "get_length failed: %08x\n", hres);
590     ok_(__FILE__,line) (len == exlen, "len=%ld, expected %ld\n", len, exlen);
591
592     if(len > exlen)
593         len = exlen;
594
595     V_VT(&index) = VT_EMPTY;
596     V_VT(&name) = VT_I4;
597
598     for(i=0; i<len; i++) {
599         V_I4(&name) = i;
600         disp = (void*)0xdeadbeef;
601         hres = IHTMLElementCollection_item(col, name, index, &disp);
602         ok_(__FILE__,line) (hres == S_OK, "item(%d) failed: %08x\n", i, hres);
603         ok_(__FILE__,line) (disp != NULL, "item returned NULL\n");
604         if(FAILED(hres) || !disp)
605             continue;
606
607         _test_elem_type(line, (IUnknown*)disp, elem_types[i]);
608         IDispatch_Release(disp);
609     }
610
611     V_I4(&name) = len;
612     disp = (void*)0xdeadbeef;
613     hres = IHTMLElementCollection_item(col, name, index, &disp);
614     ok_(__FILE__,line) (hres == S_OK, "item failed: %08x\n", hres);
615     ok_(__FILE__,line) (disp == NULL, "disp != NULL\n");
616
617     V_I4(&name) = -1;
618     disp = (void*)0xdeadbeef;
619     hres = IHTMLElementCollection_item(col, name, index, &disp);
620     ok_(__FILE__,line) (hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
621     ok_(__FILE__,line) (disp == NULL, "disp != NULL\n");
622 }
623
624 static void test_elem_col_item(IHTMLElementCollection *col, LPCWSTR n,
625         const elem_type_t *elem_types, long len)
626 {
627     IHTMLElementCollection *elcol;
628     IDispatch *disp;
629     VARIANT name, index;
630     DWORD i;
631     HRESULT hres;
632
633     V_VT(&index) = VT_EMPTY;
634     V_VT(&name) = VT_BSTR;
635     V_BSTR(&name) = SysAllocString(n);
636
637     hres = IHTMLElementCollection_item(col, name, index, &disp);
638     ok(hres == S_OK, "item failed: %08x\n", hres);
639
640     hres = IDispatch_QueryInterface(disp, &IID_IHTMLElementCollection, (void**)&elcol);
641     IDispatch_Release(disp);
642     ok(hres == S_OK, "Could not get IHTMLElementCollection interface: %08x\n", hres);
643     test_elem_collection(elcol, elem_types, len);
644     IHTMLElementCollection_Release(elcol);
645
646     V_VT(&index) = VT_I4;
647
648     for(i=0; i<len; i++) {
649         V_I4(&index) = i;
650         disp = (void*)0xdeadbeef;
651         hres = IHTMLElementCollection_item(col, name, index, &disp);
652         ok(hres == S_OK, "item failed: %08x\n", hres);
653         ok(disp != NULL, "disp == NULL\n");
654         if(FAILED(hres) || !disp)
655             continue;
656
657         test_elem_type((IUnknown*)disp, elem_types[i]);
658
659         IDispatch_Release(disp);
660     }
661
662     V_I4(&index) = len;
663     disp = (void*)0xdeadbeef;
664     hres = IHTMLElementCollection_item(col, name, index, &disp);
665     ok(hres == S_OK, "item failed: %08x\n", hres);
666     ok(disp == NULL, "disp != NULL\n");
667
668     V_I4(&index) = -1;
669     disp = (void*)0xdeadbeef;
670     hres = IHTMLElementCollection_item(col, name, index, &disp);
671     ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
672     ok(disp == NULL, "disp != NULL\n");
673
674     SysFreeString(V_BSTR(&name));
675 }
676
677 static IHTMLElement *get_elem_by_id(IHTMLDocument2 *doc, LPCWSTR id, BOOL expect_success)
678 {
679     IHTMLElementCollection *col;
680     IHTMLElement *elem;
681     IDispatch *disp = (void*)0xdeadbeef;
682     VARIANT name, index;
683     HRESULT hres;
684
685     hres = IHTMLDocument2_get_all(doc, &col);
686     ok(hres == S_OK, "get_all failed: %08x\n", hres);
687     ok(col != NULL, "col == NULL\n");
688     if(FAILED(hres) || !col)
689         return NULL;
690
691     V_VT(&index) = VT_EMPTY;
692     V_VT(&name) = VT_BSTR;
693     V_BSTR(&name) = SysAllocString(id);
694
695     hres = IHTMLElementCollection_item(col, name, index, &disp);
696     IHTMLElementCollection_Release(col);
697     SysFreeString(V_BSTR(&name));
698     ok(hres == S_OK, "item failed: %08x\n", hres);
699     if(!expect_success) {
700         ok(disp == NULL, "disp != NULL\n");
701         return NULL;
702     }
703
704     ok(disp != NULL, "disp == NULL\n");
705     if(!disp)
706         return NULL;
707
708     hres = IDispatch_QueryInterface(disp, &IID_IHTMLElement, (void**)&elem);
709     IDispatch_Release(disp);
710     ok(hres == S_OK, "Could not get IHTMLElement interface: %08x\n", hres);
711
712     return elem;
713 }
714
715 static void test_select_elem(IHTMLSelectElement *select)
716 {
717     test_select_length(select, 2);
718     test_select_selidx(select, 0);
719     test_select_put_selidx(select, 1);
720 }
721
722 static void test_create_option_elem(IHTMLDocument2 *doc)
723 {
724     IHTMLOptionElement *option;
725
726     option = create_option_elem(doc, "test text", "test value");
727
728     test_option_put_text(option, "new text");
729     test_option_put_value(option, "new value");
730
731     IHTMLOptionElement_Release(option);
732 }
733
734 static IHTMLTxtRange *test_create_body_range(IHTMLDocument2 *doc)
735 {
736     IHTMLBodyElement *body;
737     IHTMLTxtRange *range;
738     IHTMLElement *elem;
739     HRESULT hres;
740
741     hres = IHTMLDocument2_get_body(doc, &elem);
742     ok(hres == S_OK, "get_body failed: %08x\n", hres);
743
744     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
745     IHTMLElement_Release(elem);
746
747     hres = IHTMLBodyElement_createTextRange(body, &range);
748     IHTMLBodyElement_Release(body);
749     ok(hres == S_OK, "createTextRange failed: %08x\n", hres);
750
751     return range;
752 }
753
754 static void test_txtrange(IHTMLDocument2 *doc)
755 {
756     IHTMLTxtRange *body_range, *range, *range2;
757     IHTMLSelectionObject *selection;
758     IDispatch *disp_range;
759     HRESULT hres;
760
761     body_range = test_create_body_range(doc);
762
763     test_range_text(body_range, "test abc 123\r\nit's text");
764
765     hres = IHTMLTxtRange_duplicate(body_range, &range);
766     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
767
768     hres = IHTMLTxtRange_duplicate(body_range, &range2);
769     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
770     test_range_isequal(range, range2, VARIANT_TRUE);
771
772     test_range_text(range, "test abc 123\r\nit's text");
773     test_range_text(body_range, "test abc 123\r\nit's text");
774
775     test_range_collapse(range, TRUE);
776     test_range_isequal(range, range2, VARIANT_FALSE);
777     test_range_inrange(range, range2, VARIANT_FALSE);
778     test_range_inrange(range2, range, VARIANT_TRUE);
779     IHTMLTxtRange_Release(range2);
780
781     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
782     test_range_expand(range, wordW, VARIANT_FALSE, "test ");
783     test_range_move(range, characterW, 2, 2);
784     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
785
786     test_range_collapse(range, FALSE);
787     test_range_expand(range, wordW, VARIANT_TRUE, "abc ");
788
789     test_range_collapse(range, FALSE);
790     test_range_expand(range, wordW, VARIANT_TRUE, "123");
791     test_range_expand(range, wordW, VARIANT_FALSE, "123");
792     test_range_move(range, characterW, 2, 2);
793     test_range_expand(range, wordW, VARIANT_TRUE, "123");
794     test_range_moveend(range, characterW, -5, -5);
795     test_range_text(range, NULL);
796     test_range_moveend(range, characterW, 3, 3);
797     test_range_text(range, "c 1");
798     test_range_expand(range, texteditW, VARIANT_TRUE, "test abc 123\r\nit's text");
799     test_range_collapse(range, TRUE);
800     test_range_move(range, characterW, 4, 4);
801     test_range_moveend(range, characterW, 1, 1);
802     test_range_text(range, " ");
803     test_range_move(range, wordW, 1, 1);
804     test_range_moveend(range, characterW, 2, 2);
805     test_range_text(range, "ab");
806
807     IHTMLTxtRange_Release(range);
808
809     hres = IHTMLTxtRange_duplicate(body_range, &range);
810     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
811
812     test_range_text(range, "test abc 123\r\nit's text");
813     test_range_move(range, characterW, 3, 3);
814     test_range_moveend(range, characterW, 1, 1);
815     test_range_text(range, "t");
816     test_range_moveend(range, characterW, 3, 3);
817     test_range_text(range, "t ab");
818     test_range_moveend(range, characterW, -2, -2);
819     test_range_text(range, "t ");
820     test_range_move(range, characterW, 6, 6);
821     test_range_moveend(range, characterW, 3, 3);
822     test_range_text(range, "123");
823     test_range_moveend(range, characterW, 2, 2);
824     test_range_text(range, "123\r\ni");
825
826     IHTMLTxtRange_Release(range);
827
828     hres = IHTMLTxtRange_duplicate(body_range, &range);
829     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
830
831     test_range_move(range, wordW, 1, 1);
832     test_range_moveend(range, characterW, 2, 2);
833     test_range_text(range, "ab");
834
835     test_range_move(range, characterW, -2, -2);
836     test_range_moveend(range, characterW, 2, 2);
837     test_range_text(range, "t ");
838
839     test_range_move(range, wordW, 3, 3);
840     test_range_move(range, wordW, -2, -2);
841     test_range_moveend(range, characterW, 2, 2);
842     test_range_text(range, "ab");
843
844     test_range_move(range, characterW, -6, -5);
845     test_range_moveend(range, characterW, -1, 0);
846     test_range_moveend(range, characterW, -6, 0);
847     test_range_move(range, characterW, 2, 2);
848     test_range_moveend(range, characterW, 2, 2);
849     test_range_text(range, "st");
850     test_range_moveend(range, characterW, -6, -4);
851     test_range_moveend(range, characterW, 2, 2);
852
853     IHTMLTxtRange_Release(range);
854
855     hres = IHTMLTxtRange_duplicate(body_range, &range);
856     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
857
858     test_range_move(range, wordW, 2, 2);
859     test_range_moveend(range, characterW, 2, 2);
860     test_range_text(range, "12");
861
862     test_range_move(range, characterW, 15, 14);
863     test_range_move(range, characterW, -2, -2);
864     test_range_moveend(range, characterW, 3, 2);
865     test_range_text(range, "t");
866     test_range_moveend(range, characterW, -1, -1);
867     test_range_text(range, "t");
868     test_range_expand(range, wordW, VARIANT_TRUE, "text");
869     test_range_move(range, characterW, -2, -2);
870     test_range_moveend(range, characterW, 2, 2);
871     test_range_text(range, "s ");
872     test_range_move(range, characterW, 100, 7);
873     test_range_move(range, wordW, 1, 0);
874     test_range_move(range, characterW, -2, -2);
875     test_range_moveend(range, characterW, 3, 2);
876     test_range_text(range, "t");
877
878     IHTMLTxtRange_Release(range);
879
880     hres = IHTMLTxtRange_duplicate(body_range, &range);
881     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
882
883     test_range_collapse(range, TRUE);
884     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
885     test_range_put_text(range, "word");
886     test_range_text(body_range, "wordabc 123\r\nit's text");
887     test_range_text(range, NULL);
888     test_range_moveend(range, characterW, 3, 3);
889     test_range_text(range, "abc");
890     test_range_movestart(range, characterW, -2, -2);
891     test_range_text(range, "rdabc");
892     test_range_movestart(range, characterW, 3, 3);
893     test_range_text(range, "bc");
894     test_range_movestart(range, characterW, 4, 4);
895     test_range_text(range, NULL);
896     test_range_movestart(range, characterW, -3, -3);
897     test_range_text(range, "c 1");
898     test_range_movestart(range, characterW, -7, -6);
899     test_range_text(range, "wordabc 1");
900     test_range_movestart(range, characterW, 100, 22);
901     test_range_text(range, NULL);
902
903     IHTMLTxtRange_Release(range);
904     IHTMLTxtRange_Release(body_range);
905
906     hres = IHTMLDocument2_get_selection(doc, &selection);
907     ok(hres == S_OK, "IHTMLDocument2_get_selection failed: %08x\n", hres);
908
909     hres = IHTMLSelectionObject_createRange(selection, &disp_range);
910     ok(hres == S_OK, "IHTMLSelectionObject_createRange failed: %08x\n", hres);
911     IHTMLSelectionObject_Release(selection);
912
913     hres = IDispatch_QueryInterface(disp_range, &IID_IHTMLTxtRange, (void **)&range);
914     ok(hres == S_OK, "Could not get IID_IHTMLTxtRange interface: 0x%08x\n", hres);
915     IDispatch_Release(disp_range);
916
917     test_range_text(range, NULL);
918     test_range_moveend(range, characterW, 3, 3);
919     test_range_text(range, "wor");
920     test_range_parent(range, ET_BODY);
921     test_range_expand(range, texteditW, VARIANT_TRUE, "wordabc 123\r\nit's text");
922     test_range_expand(range, texteditW, VARIANT_TRUE, "wordabc 123\r\nit's text");
923     test_range_move(range, characterW, 3, 3);
924     test_range_expand(range, wordW, VARIANT_TRUE, "wordabc ");
925     test_range_moveend(range, characterW, -4, -4);
926     test_range_put_text(range, "abc def ");
927     test_range_expand(range, texteditW, VARIANT_TRUE, "abc def abc 123\r\nit's text");
928     test_range_move(range, wordW, 1, 1);
929     test_range_movestart(range, characterW, -1, -1);
930     test_range_text(range, " ");
931     test_range_move(range, wordW, 1, 1);
932     test_range_moveend(range, characterW, 3, 3);
933     test_range_text(range, "def");
934     test_range_put_text(range, "xyz");
935     test_range_moveend(range, characterW, 1, 1);
936     test_range_move(range, wordW, 1, 1);
937     test_range_moveend(range, characterW, 2, 2);
938     test_range_text(range, "ab");
939
940     IHTMLTxtRange_Release(range);
941 }
942
943 static void test_compatmode(IHTMLDocument2 *doc)
944 {
945     IHTMLDocument5 *doc5;
946     BSTR mode;
947     HRESULT hres;
948
949     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument5, (void**)&doc5);
950     ok(hres == S_OK, "Could not get IHTMLDocument5 interface: %08x\n", hres);
951     if(FAILED(hres))
952         return;
953
954     hres = IHTMLDocument5_get_compatMode(doc5, &mode);
955     IHTMLDocument5_Release(doc5);
956     ok(hres == S_OK, "get_compatMode failed: %08x\n", hres);
957     ok(!strcmp_wa(mode, "BackCompat"), "compatMode=%s\n", dbgstr_w(mode));
958     SysFreeString(mode);
959 }
960
961 static void test_default_style(IHTMLStyle *style)
962 {
963     VARIANT_BOOL b;
964     VARIANT v;
965     BSTR str;
966     HRESULT hres;
967
968     str = (void*)0xdeadbeef;
969     hres = IHTMLStyle_get_fontFamily(style, &str);
970     ok(hres == S_OK, "get_fontFamily failed: %08x\n", hres);
971     ok(!str, "fontFamily = %s\n", dbgstr_w(str));
972
973     str = (void*)0xdeadbeef;
974     hres = IHTMLStyle_get_fontWeight(style, &str);
975     ok(hres == S_OK, "get_fontWeight failed: %08x\n", hres);
976     ok(!str, "fontWeight = %s\n", dbgstr_w(str));
977
978     str = (void*)0xdeadbeef;
979     hres = IHTMLStyle_get_display(style, &str);
980     ok(hres == S_OK, "get_display failed: %08x\n", hres);
981     ok(!str, "display = %s\n", dbgstr_w(str));
982
983     str = (void*)0xdeadbeef;
984     hres = IHTMLStyle_get_visibility(style, &str);
985     ok(hres == S_OK, "get_visibility failed: %08x\n", hres);
986     ok(!str, "visibility = %s\n", dbgstr_w(str));
987
988     V_VT(&v) = VT_NULL;
989     hres = IHTMLStyle_get_fontSize(style, &v);
990     ok(hres == S_OK, "get_fontSize failed: %08x\n", hres);
991     ok(V_VT(&v) == VT_BSTR, "V_VT(fontSize) = %d\n", V_VT(&v));
992     ok(!V_BSTR(&v), "V_BSTR(fontSize) = %s\n", dbgstr_w(V_BSTR(&v)));
993
994     V_VT(&v) = VT_NULL;
995     hres = IHTMLStyle_get_color(style, &v);
996     ok(hres == S_OK, "get_color failed: %08x\n", hres);
997     ok(V_VT(&v) == VT_BSTR, "V_VT(color) = %d\n", V_VT(&v));
998     ok(!V_BSTR(&v), "V_BSTR(color) = %s\n", dbgstr_w(V_BSTR(&v)));
999
1000     b = 0xfefe;
1001     hres = IHTMLStyle_get_textDecorationUnderline(style, &b);
1002     ok(hres == S_OK, "get_textDecorationUnderline failed: %08x\n", hres);
1003     ok(b == VARIANT_FALSE, "textDecorationUnderline = %x\n", b);
1004
1005     b = 0xfefe;
1006     hres = IHTMLStyle_get_textDecorationLineThrough(style, &b);
1007     ok(hres == S_OK, "get_textDecorationLineThrough failed: %08x\n", hres);
1008     ok(b == VARIANT_FALSE, "textDecorationLineThrough = %x\n", b);
1009 }
1010
1011 static void test_default_selection(IHTMLDocument2 *doc)
1012 {
1013     IHTMLSelectionObject *selection;
1014     IHTMLTxtRange *range;
1015     IDispatch *disp;
1016     BSTR str;
1017     HRESULT hres;
1018
1019     hres = IHTMLDocument2_get_selection(doc, &selection);
1020     ok(hres == S_OK, "get_selection failed: %08x\n", hres);
1021
1022     hres = IHTMLSelectionObject_get_type(selection, &str);
1023     ok(hres == S_OK, "get_type failed: %08x\n", hres);
1024     ok(!lstrcmpW(str, noneW), "type = %s\n", dbgstr_w(str));
1025     SysFreeString(str);
1026
1027     hres = IHTMLSelectionObject_createRange(selection, &disp);
1028     IHTMLSelectionObject_Release(selection);
1029     ok(hres == S_OK, "createRange failed: %08x\n", hres);
1030
1031     hres = IDispatch_QueryInterface(disp, &IID_IHTMLTxtRange, (void**)&range);
1032     IDispatch_Release(disp);
1033     ok(hres == S_OK, "Could not get IHTMLTxtRange interface: %08x\n", hres);
1034
1035     test_range_text(range, NULL);
1036     IHTMLTxtRange_Release(range);
1037 }
1038
1039 static void test_default_body(IHTMLBodyElement *body)
1040 {
1041     BSTR bstr;
1042     HRESULT hres;
1043
1044     bstr = (void*)0xdeadbeef;
1045     hres = IHTMLBodyElement_get_background(body, &bstr);
1046     ok(hres == S_OK, "get_background failed: %08x\n", hres);
1047     ok(bstr == NULL, "bstr != NULL\n");
1048 }
1049
1050 static void test_defaults(IHTMLDocument2 *doc)
1051 {
1052     IHTMLStyleSheetsCollection *stylesheetcol;
1053     IHTMLBodyElement *body;
1054     IHTMLElement *elem;
1055     IHTMLStyle *style;
1056     long l;
1057     HRESULT hres;
1058
1059     hres = IHTMLDocument2_get_body(doc, &elem);
1060     ok(hres == S_OK, "get_body failed: %08x\n", hres);
1061
1062     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
1063     ok(hres == S_OK, "Could not get IHTMBodyElement: %08x\n", hres);
1064     test_default_body(body);
1065     IHTMLBodyElement_Release(body);
1066
1067     hres = IHTMLElement_get_style(elem, &style);
1068     IHTMLElement_Release(elem);
1069     ok(hres == S_OK, "get_style failed: %08x\n", hres);
1070
1071     test_default_style(style);
1072     test_compatmode(doc);
1073
1074     IHTMLStyle_Release(style);
1075
1076     hres = IHTMLDocument2_get_styleSheets(doc, &stylesheetcol);
1077     ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
1078
1079     l = 0xdeadbeef;
1080     hres = IHTMLStyleSheetsCollection_get_length(stylesheetcol, &l);
1081     ok(hres == S_OK, "get_length failed: %08x\n", hres);
1082     ok(l == 0, "length = %ld\n", l);
1083
1084     IHTMLStyleSheetsCollection_Release(stylesheetcol);
1085
1086     test_default_selection(doc);
1087 }
1088
1089 static void test_stylesheet(IDispatch *disp)
1090 {
1091     IHTMLStyleSheetRulesCollection *col = NULL;
1092     IHTMLStyleSheet *stylesheet;
1093     HRESULT hres;
1094
1095     hres = IDispatch_QueryInterface(disp, &IID_IHTMLStyleSheet, (void**)&stylesheet);
1096     ok(hres == S_OK, "Could not get IHTMLStyleSheet: %08x\n", hres);
1097
1098     hres = IHTMLStyleSheet_get_rules(stylesheet, &col);
1099     ok(hres == S_OK, "get_rules failed: %08x\n", hres);
1100     ok(col != NULL, "col == NULL\n");
1101
1102     IHTMLStyleSheetRulesCollection_Release(col);
1103     IHTMLStyleSheet_Release(stylesheet);
1104 }
1105
1106 static void test_stylesheets(IHTMLDocument2 *doc)
1107 {
1108     IHTMLStyleSheetsCollection *col = NULL;
1109     VARIANT idx, res;
1110     long len = 0;
1111     HRESULT hres;
1112
1113     hres = IHTMLDocument2_get_styleSheets(doc, &col);
1114     ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
1115     ok(col != NULL, "col == NULL\n");
1116
1117     hres = IHTMLStyleSheetsCollection_get_length(col, &len);
1118     ok(hres == S_OK, "get_length failed: %08x\n", hres);
1119     ok(len == 1, "len=%ld\n", len);
1120
1121     VariantInit(&res);
1122     V_VT(&idx) = VT_I4;
1123     V_I4(&idx) = 0;
1124
1125     hres = IHTMLStyleSheetsCollection_item(col, &idx, &res);
1126     ok(hres == S_OK, "item failed: %08x\n", hres);
1127     ok(V_VT(&res) == VT_DISPATCH, "V_VT(res) = %d\n", V_VT(&res));
1128     ok(V_DISPATCH(&res) != NULL, "V_DISPATCH(&res) == NULL\n");
1129     test_stylesheet(V_DISPATCH(&res));
1130     VariantClear(&res);
1131
1132     V_VT(&res) = VT_I4;
1133     V_VT(&idx) = VT_I4;
1134     V_I4(&idx) = 1;
1135
1136     hres = IHTMLStyleSheetsCollection_item(col, &idx, &res);
1137     ok(hres == E_INVALIDARG, "item failed: %08x, expected E_INVALIDARG\n", hres);
1138     ok(V_VT(&res) == VT_EMPTY, "V_VT(res) = %d\n", V_VT(&res));
1139     ok(V_DISPATCH(&res) != NULL, "V_DISPATCH(&res) == NULL\n");
1140     VariantClear(&res);
1141
1142     IHTMLStyleSheetsCollection_Release(col);
1143 }
1144
1145 static void test_elems(IHTMLDocument2 *doc)
1146 {
1147     IHTMLElementCollection *col;
1148     IHTMLElement *elem;
1149     IDispatch *disp;
1150     HRESULT hres;
1151
1152     static const WCHAR xW[] = {'x',0};
1153     static const WCHAR sW[] = {'s',0};
1154     static const WCHAR xxxW[] = {'x','x','x',0};
1155
1156     static const elem_type_t all_types[] = {
1157         ET_HTML,
1158         ET_HEAD,
1159         ET_TITLE,
1160         ET_STYLE,
1161         ET_BODY,
1162         ET_A,
1163         ET_INPUT,
1164         ET_SELECT,
1165         ET_OPTION,
1166         ET_OPTION,
1167         ET_TEXTAREA,
1168         ET_TABLE,
1169         ET_TBODY
1170     };
1171
1172     static const elem_type_t item_types[] = {
1173         ET_A,
1174         ET_OPTION,
1175         ET_TEXTAREA
1176     };
1177
1178     hres = IHTMLDocument2_get_all(doc, &col);
1179     ok(hres == S_OK, "get_all failed: %08x\n", hres);
1180     test_elem_collection(col, all_types, sizeof(all_types)/sizeof(all_types[0]));
1181     test_elem_col_item(col, xW, item_types, sizeof(item_types)/sizeof(item_types[0]));
1182     IHTMLElementCollection_Release(col);
1183
1184     elem = get_doc_elem(doc);
1185     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
1186     hres = IHTMLElement_get_all(elem, &disp);
1187     IHTMLElement_Release(elem);
1188     ok(hres == S_OK, "get_all failed: %08x\n", hres);
1189
1190     hres = IDispatch_QueryInterface(disp, &IID_IHTMLElementCollection, (void**)&col);
1191     IDispatch_Release(disp);
1192     ok(hres == S_OK, "Could not get IHTMLElementCollection: %08x\n", hres);
1193     test_elem_collection(col, all_types+1, sizeof(all_types)/sizeof(all_types[0])-1);
1194     IHTMLElementCollection_Release(col);
1195
1196     get_elem_by_id(doc, xxxW, FALSE);
1197     elem = get_elem_by_id(doc, sW, TRUE);
1198     if(elem) {
1199         IHTMLSelectElement *select;
1200
1201         hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLSelectElement, (void**)&select);
1202         ok(hres == S_OK, "Could not get IHTMLSelectElement interface: %08x\n", hres);
1203
1204         test_select_elem(select);
1205
1206         IHTMLSelectElement_Release(select);
1207         IHTMLElement_Release(elem);
1208     }
1209
1210     test_stylesheets(doc);
1211     test_create_option_elem(doc);
1212 }
1213
1214 static void test_exec(IUnknown *unk, const GUID *grpid, DWORD cmdid, VARIANT *in, VARIANT *out)
1215 {
1216     IOleCommandTarget *cmdtrg;
1217     HRESULT hres;
1218
1219     hres = IHTMLTxtRange_QueryInterface(unk, &IID_IOleCommandTarget, (void**)&cmdtrg);
1220     ok(hres == S_OK, "Could not get IOleCommandTarget interface: %08x\n", hres);
1221
1222     hres = IOleCommandTarget_Exec(cmdtrg, grpid, cmdid, 0, in, out);
1223     ok(hres == S_OK, "Exec failed: %08x\n", hres);
1224
1225     IOleCommandTarget_Release(cmdtrg);
1226 }
1227
1228 static void test_indent(IHTMLDocument2 *doc)
1229 {
1230     IHTMLElementCollection *col;
1231     IHTMLTxtRange *range;
1232     HRESULT hres;
1233
1234     static const elem_type_t all_types[] = {
1235         ET_HTML,
1236         ET_HEAD,
1237         ET_TITLE,
1238         ET_BODY,
1239         ET_BR,
1240         ET_A,
1241     };
1242
1243     static const elem_type_t indent_types[] = {
1244         ET_HTML,
1245         ET_HEAD,
1246         ET_TITLE,
1247         ET_BODY,
1248         ET_BLOCKQUOTE,
1249         ET_P,
1250         ET_BR,
1251         ET_A,
1252     };
1253
1254     hres = IHTMLDocument2_get_all(doc, &col);
1255     ok(hres == S_OK, "get_all failed: %08x\n", hres);
1256     test_elem_collection(col, all_types, sizeof(all_types)/sizeof(all_types[0]));
1257     IHTMLElementCollection_Release(col);
1258
1259     range = test_create_body_range(doc);
1260     test_exec((IUnknown*)range, &CGID_MSHTML, IDM_INDENT, NULL, NULL);
1261     IHTMLTxtRange_Release(range);
1262
1263     hres = IHTMLDocument2_get_all(doc, &col);
1264     ok(hres == S_OK, "get_all failed: %08x\n", hres);
1265     test_elem_collection(col, indent_types, sizeof(indent_types)/sizeof(indent_types[0]));
1266     IHTMLElementCollection_Release(col);
1267 }
1268
1269 static IHTMLDocument2 *notif_doc;
1270 static BOOL doc_complete;
1271
1272 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
1273         REFIID riid, void**ppv)
1274 {
1275     if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
1276         *ppv = iface;
1277         return S_OK;
1278     }
1279
1280     ok(0, "unexpected call\n");
1281     return E_NOINTERFACE;
1282 }
1283
1284 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
1285 {
1286     return 2;
1287 }
1288
1289 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
1290 {
1291     return 1;
1292 }
1293
1294 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
1295 {
1296     if(dispID == DISPID_READYSTATE){
1297         BSTR state;
1298         HRESULT hres;
1299
1300         static const WCHAR completeW[] = {'c','o','m','p','l','e','t','e',0};
1301
1302         hres = IHTMLDocument2_get_readyState(notif_doc, &state);
1303         ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
1304
1305         if(!lstrcmpW(state, completeW))
1306             doc_complete = TRUE;
1307
1308         SysFreeString(state);        
1309     }
1310
1311     return S_OK;
1312 }
1313
1314 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
1315 {
1316     ok(0, "unexpected call\n");
1317     return E_NOTIMPL;
1318 }
1319
1320 static IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
1321     PropertyNotifySink_QueryInterface,
1322     PropertyNotifySink_AddRef,
1323     PropertyNotifySink_Release,
1324     PropertyNotifySink_OnChanged,
1325     PropertyNotifySink_OnRequestEdit
1326 };
1327
1328 static IPropertyNotifySink PropertyNotifySink = { &PropertyNotifySinkVtbl };
1329
1330 static IHTMLDocument2 *create_doc_with_string(const char *str)
1331 {
1332     IPersistStreamInit *init;
1333     IStream *stream;
1334     IHTMLDocument2 *doc;
1335     HGLOBAL mem;
1336     SIZE_T len;
1337
1338     notif_doc = doc = create_document();
1339     if(!doc)
1340         return NULL;
1341
1342     doc_complete = FALSE;
1343     len = strlen(str);
1344     mem = GlobalAlloc(0, len);
1345     memcpy(mem, str, len);
1346     CreateStreamOnHGlobal(mem, TRUE, &stream);
1347
1348     IHTMLDocument2_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&init);
1349
1350     IPersistStreamInit_Load(init, stream);
1351     IPersistStreamInit_Release(init);
1352     IStream_Release(stream);
1353
1354     return doc;
1355 }
1356
1357 static void do_advise(IUnknown *unk, REFIID riid, IUnknown *unk_advise)
1358 {
1359     IConnectionPointContainer *container;
1360     IConnectionPoint *cp;
1361     DWORD cookie;
1362     HRESULT hres;
1363
1364     hres = IUnknown_QueryInterface(unk, &IID_IConnectionPointContainer, (void**)&container);
1365     ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
1366
1367     hres = IConnectionPointContainer_FindConnectionPoint(container, riid, &cp);
1368     IConnectionPointContainer_Release(container);
1369     ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
1370
1371     hres = IConnectionPoint_Advise(cp, unk_advise, &cookie);
1372     IConnectionPoint_Release(cp);
1373     ok(hres == S_OK, "Advise failed: %08x\n", hres);
1374 }
1375
1376 typedef void (*domtest_t)(IHTMLDocument2*);
1377
1378 static void run_domtest(const char *str, domtest_t test)
1379 {
1380     IHTMLDocument2 *doc;
1381     IHTMLElement *body = NULL;
1382     ULONG ref;
1383     MSG msg;
1384     HRESULT hres;
1385
1386     doc = create_doc_with_string(str);
1387     do_advise((IUnknown*)doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
1388
1389     while(!doc_complete && GetMessage(&msg, NULL, 0, 0)) {
1390         TranslateMessage(&msg);
1391         DispatchMessage(&msg);
1392     }
1393
1394     hres = IHTMLDocument2_get_body(doc, &body);
1395     ok(hres == S_OK, "get_body failed: %08x\n", hres);
1396
1397     if(body) {
1398         IHTMLElement_Release(body);
1399         test(doc);
1400     }else {
1401         skip("Could not get document body. Assuming no Gecko installed.\n");
1402     }
1403
1404     ref = IHTMLDocument2_Release(doc);
1405     ok(!ref, "ref = %d\n", ref);
1406 }
1407
1408 static void gecko_installer_workaround(BOOL disable)
1409 {
1410     HKEY hkey;
1411     DWORD res;
1412
1413     static BOOL has_url = FALSE;
1414     static char url[2048];
1415
1416     if(!disable && !has_url)
1417         return;
1418
1419     res = RegOpenKey(HKEY_CURRENT_USER, "Software\\Wine\\MSHTML", &hkey);
1420     if(res != ERROR_SUCCESS)
1421         return;
1422
1423     if(disable) {
1424         DWORD type, size = sizeof(url);
1425
1426         res = RegQueryValueEx(hkey, "GeckoUrl", NULL, &type, (PVOID)url, &size);
1427         if(res == ERROR_SUCCESS && type == REG_SZ)
1428             has_url = TRUE;
1429
1430         RegDeleteValue(hkey, "GeckoUrl");
1431     }else {
1432         RegSetValueEx(hkey, "GeckoUrl", 0, REG_SZ, (PVOID)url, lstrlenA(url)+1);
1433     }
1434
1435     RegCloseKey(hkey);
1436 }
1437
1438 START_TEST(dom)
1439 {
1440     gecko_installer_workaround(TRUE);
1441     CoInitialize(NULL);
1442
1443     run_domtest(doc_str1, test_doc_elem);
1444     run_domtest(doc_str2, test_txtrange);
1445     run_domtest(elem_test_str, test_elems);
1446     run_domtest(doc_blank, test_defaults);
1447     run_domtest(indent_test_str, test_indent);
1448
1449     CoUninitialize();
1450     gecko_installer_workaround(FALSE);
1451 }