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