wintrust: Add a helper function to initialize chain creation parameters.
[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 "docobj.h"
31
32 static const char doc_blank[] = "<html></html>";
33 static const char doc_str1[] = "<html><body>test</body></html>";
34 static const char doc_str2[] =
35     "<html><body>test a<font size=\"2\">bc 123<br />it's </font>text<br /></body></html>";
36
37 static const WCHAR noneW[] = {'N','o','n','e',0};
38
39 static WCHAR characterW[] = {'c','h','a','r','a','c','t','e','r',0};
40 static WCHAR wordW[] = {'w','o','r','d',0};
41
42 static const char *dbgstr_w(LPCWSTR str)
43 {
44     static char buf[512];
45     if(!str)
46         return "(null)";
47     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
48     return buf;
49 }
50
51 static int strcmp_wa(LPCWSTR strw, const char *stra)
52 {
53     WCHAR buf[512];
54     MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
55     return lstrcmpW(strw, buf);
56 }
57
58 static IHTMLDocument2 *create_document(void)
59 {
60     IHTMLDocument2 *doc;
61     HRESULT hres;
62
63     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
64             &IID_IHTMLDocument2, (void**)&doc);
65     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
66
67     return doc;
68 }
69
70 #define test_node_name(u,n) _test_node_name(__LINE__,u,n)
71 static void _test_node_name(unsigned line, IUnknown *unk, const char *exname)
72 {
73     IHTMLDOMNode *node;
74     BSTR name;
75     HRESULT hres;
76
77     hres = IUnknown_QueryInterface(unk, &IID_IHTMLDOMNode, (void**)&node);
78     ok_(__FILE__, line) (hres == S_OK, "QueryInterface(IID_IHTMLNode) failed: %08x\n", hres);
79
80     hres = IHTMLDOMNode_get_nodeName(node, &name);
81     IHTMLDOMNode_Release(node);
82     ok_(__FILE__, line) (hres == S_OK, "get_nodeName failed: %08x\n", hres);
83     ok_(__FILE__, line) (!strcmp_wa(name, exname), "got name: %s, expected %s\n", dbgstr_w(name), exname);
84
85     SysFreeString(name);
86 }
87
88 #define test_elem_tag(u,n) _test_elem_tag(__LINE__,u,n)
89 static void _test_elem_tag(unsigned line, IUnknown *unk, const char *extag)
90 {
91     IHTMLElement *elem;
92     BSTR tag;
93     HRESULT hres;
94
95     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement, (void**)&elem);
96     ok_(__FILE__, line) (hres == S_OK, "QueryInterface(IID_IHTMLElement) failed: %08x\n", hres);
97
98     hres = IHTMLElement_get_tagName(elem, &tag);
99     IHTMLElement_Release(elem);
100     ok_(__FILE__, line) (hres == S_OK, "get_tagName failed: %08x\n", hres);
101     ok_(__FILE__, line) (!strcmp_wa(tag, extag), "got tag: %s, expected %s\n", dbgstr_w(tag), extag);
102
103     SysFreeString(tag);
104 }
105
106 static void test_doc_elem(IHTMLDocument2 *doc)
107 {
108     IHTMLElement *elem;
109     IHTMLDocument3 *doc3;
110     HRESULT hres;
111
112     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
113     ok(hres == S_OK, "QueryInterface(IID_IHTMLDocument3) failed: %08x\n", hres);
114
115     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
116     IHTMLDocument3_Release(doc3);
117     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
118
119     test_node_name((IUnknown*)elem, "HTML");
120     test_elem_tag((IUnknown*)elem, "HTML");
121
122     IHTMLElement_Release(elem);
123 }
124
125 #define test_range_text(r,t) _test_range_text(__LINE__,r,t)
126 static void _test_range_text(unsigned line, IHTMLTxtRange *range, const char *extext)
127 {
128     BSTR text;
129     HRESULT hres;
130
131     hres = IHTMLTxtRange_get_text(range, &text);
132     ok_(__FILE__, line) (hres == S_OK, "get_text failed: %08x\n", hres);
133
134     if(extext) {
135         ok_(__FILE__, line) (text != NULL, "text == NULL\n");
136         ok_(__FILE__, line) (!strcmp_wa(text, extext), "text=\"%s\", expected \"%s\"\n", dbgstr_w(text), extext);
137     }else {
138         ok_(__FILE__, line) (text == NULL, "text=\"%s\", expected NULL\n", dbgstr_w(text));
139     }
140
141     SysFreeString(text);
142
143 }
144
145 #define test_range_collapse(r,b) _test_range_collapse(__LINE__,r,b)
146 static void _test_range_collapse(unsigned line, IHTMLTxtRange *range, BOOL b)
147 {
148     HRESULT hres;
149
150     hres = IHTMLTxtRange_collapse(range, b);
151     ok_(__FILE__, line) (hres == S_OK, "collapse failed: %08x\n", hres);
152     _test_range_text(line, range, NULL);
153 }
154
155 #define test_range_expand(r,u,b,t) _test_range_expand(__LINE__,r,u,b,t)
156 static void _test_range_expand(unsigned line, IHTMLTxtRange *range, LPWSTR unit,
157         VARIANT_BOOL exb, const char *extext)
158 {
159     VARIANT_BOOL b = 0xe0e0;
160     HRESULT hres;
161
162     hres = IHTMLTxtRange_expand(range, unit, &b);
163     ok_(__FILE__,line) (hres == S_OK, "expand failed: %08x\n", hres);
164     ok_(__FILE__,line) (b == exb, "b=%x, expected %x\n", b, exb);
165     _test_range_text(line, range, extext);
166 }
167
168 #define test_range_move(r,u,c,e) _test_range_move(__LINE__,r,u,c,e)
169 static void _test_range_move(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
170 {
171     long c = 0xdeadbeef;
172     HRESULT hres;
173
174     hres = IHTMLTxtRange_move(range, unit, cnt, &c);
175     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
176     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
177     _test_range_text(line, range, NULL);
178 }
179
180 #define test_range_moveend(r,u,c,e) _test_range_moveend(__LINE__,r,u,c,e)
181 static void _test_range_moveend(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
182 {
183     long c = 0xdeadbeef;
184     HRESULT hres;
185
186     hres = IHTMLTxtRange_moveEnd(range, unit, cnt, &c);
187     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
188     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
189 }
190
191 #define test_range_put_text(r,t) _test_range_put_text(__LINE__,r,t)
192 static void _test_range_put_text(unsigned line, IHTMLTxtRange *range, LPCWSTR text)
193 {
194     HRESULT hres;
195
196     hres = IHTMLTxtRange_put_text(range, (BSTR)text);
197     ok_(__FILE__,line) (hres == S_OK, "put_text failed: %08x\n", hres);
198     _test_range_text(line, range, NULL);
199 }
200
201 #define test_range_inrange(r1,r2,b) _test_range_inrange(__LINE__,r1,r2,b)
202 static void _test_range_inrange(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
203 {
204     VARIANT_BOOL b;
205     HRESULT hres;
206
207     b = 0xe0e0;
208     hres = IHTMLTxtRange_inRange(range1, range2, &b);
209     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
210     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
211 }
212
213 #define test_range_isequal(r1,r2,b) _test_range_isequal(__LINE__,r1,r2,b)
214 static void _test_range_isequal(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
215 {
216     VARIANT_BOOL b;
217     HRESULT hres;
218
219     b = 0xe0e0;
220     hres = IHTMLTxtRange_isEqual(range1, range2, &b);
221     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
222     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
223
224     b = 0xe0e0;
225     hres = IHTMLTxtRange_isEqual(range2, range1, &b);
226     ok_(__FILE__,line) (hres == S_OK, "(2->1) isEqual failed: %08x\n", hres);
227     ok_(__FILE__,line) (b == exb, "(2->1) b=%x, expected %x\n", b, exb);
228
229     if(exb) {
230         test_range_inrange(range1, range2, VARIANT_TRUE);
231         test_range_inrange(range2, range1, VARIANT_TRUE);
232     }
233 }
234
235 static void test_txtrange(IHTMLDocument2 *doc)
236 {
237     IHTMLElement *elem;
238     IHTMLBodyElement *body;
239     IHTMLTxtRange *body_range, *range, *range2;
240     HRESULT hres;
241
242     hres = IHTMLDocument2_get_body(doc, &elem);
243     ok(hres == S_OK, "get_body failed: %08x\n", hres);
244
245     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
246     IHTMLElement_Release(elem);
247
248     hres = IHTMLBodyElement_createTextRange(body, &body_range);
249     IHTMLBodyElement_Release(body);
250     ok(hres == S_OK, "createTextRange failed: %08x\n", hres);
251
252     test_range_text(body_range, "test abc 123\r\nit's text");
253
254     hres = IHTMLTxtRange_duplicate(body_range, &range);
255     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
256
257     hres = IHTMLTxtRange_duplicate(body_range, &range2);
258     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
259     test_range_isequal(range, range2, VARIANT_TRUE);
260
261     test_range_text(range, "test abc 123\r\nit's text");
262     test_range_text(body_range, "test abc 123\r\nit's text");
263
264     test_range_collapse(range, TRUE);
265     test_range_isequal(range, range2, VARIANT_FALSE);
266     test_range_inrange(range, range2, VARIANT_FALSE);
267     test_range_inrange(range2, range, VARIANT_TRUE);
268     IHTMLTxtRange_Release(range2);
269
270     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
271     test_range_expand(range, wordW, VARIANT_FALSE, "test ");
272     test_range_move(range, characterW, 2, 2);
273     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
274
275     test_range_collapse(range, FALSE);
276     test_range_expand(range, wordW, VARIANT_TRUE, "abc ");
277
278     test_range_collapse(range, FALSE);
279     test_range_expand(range, wordW, VARIANT_TRUE, "123");
280     test_range_expand(range, wordW, VARIANT_FALSE, "123");
281     test_range_move(range, characterW, 2, 2);
282     test_range_expand(range, wordW, VARIANT_TRUE, "123");
283
284     IHTMLTxtRange_Release(range);
285
286     hres = IHTMLTxtRange_duplicate(body_range, &range);
287     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
288
289     test_range_text(range, "test abc 123\r\nit's text");
290     test_range_move(range, characterW, 3, 3);
291     test_range_moveend(range, characterW, 1, 1);
292     test_range_text(range, "t");
293     test_range_moveend(range, characterW, 3, 3);
294     test_range_text(range, "t ab");
295     test_range_moveend(range, characterW, -2, -2);
296     test_range_text(range, "t ");
297     test_range_move(range, characterW, 6, 6);
298     test_range_moveend(range, characterW, 3, 3);
299     test_range_text(range, "123");
300     test_range_moveend(range, characterW, 2, 2);
301     test_range_text(range, "123\r\ni");
302
303     IHTMLTxtRange_Release(range);
304
305     hres = IHTMLTxtRange_duplicate(body_range, &range);
306     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
307
308     test_range_move(range, wordW, 1, 1);
309     test_range_moveend(range, characterW, 2, 2);
310     test_range_text(range, "ab");
311
312     test_range_move(range, characterW, -2, -2);
313     test_range_moveend(range, characterW, 2, 2);
314     test_range_text(range, "t ");
315
316     test_range_move(range, wordW, 3, 3);
317     test_range_move(range, wordW, -2, -2);
318     test_range_moveend(range, characterW, 2, 2);
319     test_range_text(range, "ab");
320
321     test_range_move(range, characterW, -6, -5);
322     test_range_moveend(range, characterW, -1, 0);
323     test_range_moveend(range, characterW, -6, 0);
324     test_range_move(range, characterW, 2, 2);
325     test_range_moveend(range, characterW, 2, 2);
326     test_range_text(range, "st");
327     test_range_moveend(range, characterW, -6, -4);
328     test_range_moveend(range, characterW, 2, 2);
329
330     IHTMLTxtRange_Release(range);
331
332     hres = IHTMLTxtRange_duplicate(body_range, &range);
333     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
334
335     test_range_move(range, wordW, 2, 2);
336     test_range_moveend(range, characterW, 2, 2);
337     test_range_text(range, "12");
338
339     test_range_move(range, characterW, 15, 14);
340     test_range_move(range, characterW, -2, -2);
341     test_range_moveend(range, characterW, 3, 2);
342     test_range_text(range, "t");
343     test_range_moveend(range, characterW, -1, -1);
344     test_range_text(range, "t");
345     test_range_expand(range, wordW, VARIANT_TRUE, "text");
346     test_range_move(range, characterW, -2, -2);
347     test_range_moveend(range, characterW, 2, 2);
348     test_range_text(range, "s ");
349     test_range_move(range, characterW, 100, 7);
350     test_range_move(range, wordW, 1, 0);
351     test_range_move(range, characterW, -2, -2);
352     test_range_moveend(range, characterW, 3, 2);
353     test_range_text(range, "t");
354
355     IHTMLTxtRange_Release(range);
356
357     hres = IHTMLTxtRange_duplicate(body_range, &range);
358     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
359
360     test_range_collapse(range, TRUE);
361     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
362     test_range_put_text(range, wordW);
363     test_range_text(body_range, "wordabc 123\r\nit's text");
364
365     IHTMLTxtRange_Release(range);
366     IHTMLTxtRange_Release(body_range);
367 }
368
369 static void test_compatmode(IHTMLDocument2 *doc)
370 {
371     IHTMLDocument5 *doc5;
372     BSTR mode;
373     HRESULT hres;
374
375     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument5, (void**)&doc5);
376     ok(hres == S_OK, "Could not get IHTMLDocument5 interface: %08x\n", hres);
377     if(FAILED(hres))
378         return;
379
380     hres = IHTMLDocument5_get_compatMode(doc5, &mode);
381     IHTMLDocument5_Release(doc5);
382     ok(hres == S_OK, "get_compatMode failed: %08x\n", hres);
383     ok(!strcmp_wa(mode, "BackCompat"), "compatMode=%s\n", dbgstr_w(mode));
384     SysFreeString(mode);
385 }
386
387 static void test_default_style(IHTMLStyle *style)
388 {
389     VARIANT_BOOL b;
390     VARIANT v;
391     BSTR str;
392     HRESULT hres;
393
394     str = (void*)0xdeadbeef;
395     hres = IHTMLStyle_get_fontFamily(style, &str);
396     ok(hres == S_OK, "get_fontFamily failed: %08x\n", hres);
397     ok(!str, "fontFamily = %s\n", dbgstr_w(str));
398
399     str = (void*)0xdeadbeef;
400     hres = IHTMLStyle_get_fontWeight(style, &str);
401     ok(hres == S_OK, "get_fontWeight failed: %08x\n", hres);
402     ok(!str, "fontWeight = %s\n", dbgstr_w(str));
403
404     V_VT(&v) = VT_NULL;
405     hres = IHTMLStyle_get_fontSize(style, &v);
406     ok(hres == S_OK, "get_fontSize failed: %08x\n", hres);
407     ok(V_VT(&v) == VT_BSTR, "V_VT(fontSize) = %d\n", V_VT(&v));
408     ok(!V_BSTR(&v), "V_BSTR(fontSize) = %s\n", dbgstr_w(V_BSTR(&v)));
409
410     V_VT(&v) = VT_NULL;
411     hres = IHTMLStyle_get_color(style, &v);
412     ok(hres == S_OK, "get_color failed: %08x\n", hres);
413     ok(V_VT(&v) == VT_BSTR, "V_VT(color) = %d\n", V_VT(&v));
414     ok(!V_BSTR(&v), "V_BSTR(color) = %s\n", dbgstr_w(V_BSTR(&v)));
415
416     b = 0xfefe;
417     hres = IHTMLStyle_get_textDecorationUnderline(style, &b);
418     ok(hres == S_OK, "get_textDecorationUnderline failed: %08x\n", hres);
419     ok(b == VARIANT_FALSE, "textDecorationUnderline = %x\n", b);
420
421     b = 0xfefe;
422     hres = IHTMLStyle_get_textDecorationLineThrough(style, &b);
423     ok(hres == S_OK, "get_textDecorationLineThrough failed: %08x\n", hres);
424     ok(b == VARIANT_FALSE, "textDecorationLineThrough = %x\n", b);
425 }
426
427 static void test_default_selection(IHTMLDocument2 *doc)
428 {
429     IHTMLSelectionObject *selection;
430     IHTMLTxtRange *range;
431     IDispatch *disp;
432     BSTR str;
433     HRESULT hres;
434
435     hres = IHTMLDocument2_get_selection(doc, &selection);
436     ok(hres == S_OK, "get_selection failed: %08x\n", hres);
437
438     hres = IHTMLSelectionObject_get_type(selection, &str);
439     ok(hres == S_OK, "get_type failed: %08x\n", hres);
440     ok(!lstrcmpW(str, noneW), "type = %s\n", dbgstr_w(str));
441     SysFreeString(str);
442
443     hres = IHTMLSelectionObject_createRange(selection, &disp);
444     IHTMLSelectionObject_Release(selection);
445     ok(hres == S_OK, "createRange failed: %08x\n", hres);
446
447     hres = IDispatch_QueryInterface(disp, &IID_IHTMLTxtRange, (void**)&range);
448     IDispatch_Release(disp);
449     ok(hres == S_OK, "Could not get IHTMLTxtRange interface: %08x\n", hres);
450
451     test_range_text(range, NULL);
452     IHTMLTxtRange_Release(range);
453 }
454
455 static void test_defaults(IHTMLDocument2 *doc)
456 {
457     IHTMLStyleSheetsCollection *stylesheetcol;
458     IHTMLElement *elem;
459     IHTMLStyle *style;
460     long l;
461     HRESULT hres;
462
463     hres = IHTMLDocument2_get_body(doc, &elem);
464     ok(hres == S_OK, "get_body failed: %08x\n", hres);
465
466     hres = IHTMLElement_get_style(elem, &style);
467     IHTMLElement_Release(elem);
468     ok(hres == S_OK, "get_style failed: %08x\n", hres);
469
470     test_default_style(style);
471     test_compatmode(doc);
472
473     IHTMLStyle_Release(style);
474
475     hres = IHTMLDocument2_get_styleSheets(doc, &stylesheetcol);
476     ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
477
478     l = 0xdeadbeef;
479     hres = IHTMLStyleSheetsCollection_get_length(stylesheetcol, &l);
480     ok(hres == S_OK, "get_length failed: %08x\n", hres);
481     ok(l == 0, "length = %ld\n", l);
482
483     IHTMLStyleSheetsCollection_Release(stylesheetcol);
484
485     test_default_selection(doc);
486 }
487
488 static IHTMLDocument2 *notif_doc;
489 static BOOL doc_complete;
490
491 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
492         REFIID riid, void**ppv)
493 {
494     if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
495         *ppv = iface;
496         return S_OK;
497     }
498
499     ok(0, "unexpected call\n");
500     return E_NOINTERFACE;
501 }
502
503 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
504 {
505     return 2;
506 }
507
508 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
509 {
510     return 1;
511 }
512
513 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
514 {
515     if(dispID == DISPID_READYSTATE){
516         BSTR state;
517         HRESULT hres;
518
519         static const WCHAR completeW[] = {'c','o','m','p','l','e','t','e',0};
520
521         hres = IHTMLDocument2_get_readyState(notif_doc, &state);
522         ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
523
524         if(!lstrcmpW(state, completeW))
525             doc_complete = TRUE;
526
527         SysFreeString(state);        
528     }
529
530     return S_OK;
531 }
532
533 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
534 {
535     ok(0, "unexpected call\n");
536     return E_NOTIMPL;
537 }
538
539 static IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
540     PropertyNotifySink_QueryInterface,
541     PropertyNotifySink_AddRef,
542     PropertyNotifySink_Release,
543     PropertyNotifySink_OnChanged,
544     PropertyNotifySink_OnRequestEdit
545 };
546
547 static IPropertyNotifySink PropertyNotifySink = { &PropertyNotifySinkVtbl };
548
549 static IHTMLDocument2 *create_doc_with_string(const char *str)
550 {
551     IPersistStreamInit *init;
552     IStream *stream;
553     IHTMLDocument2 *doc;
554     HGLOBAL mem;
555     SIZE_T len;
556
557     notif_doc = doc = create_document();
558     if(!doc)
559         return NULL;
560
561     doc_complete = FALSE;
562     len = strlen(str);
563     mem = GlobalAlloc(0, len);
564     memcpy(mem, str, len);
565     CreateStreamOnHGlobal(mem, TRUE, &stream);
566
567     IHTMLDocument2_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&init);
568
569     IPersistStreamInit_Load(init, stream);
570     IPersistStreamInit_Release(init);
571     IStream_Release(stream);
572
573     return doc;
574 }
575
576 static void do_advise(IUnknown *unk, REFIID riid, IUnknown *unk_advise)
577 {
578     IConnectionPointContainer *container;
579     IConnectionPoint *cp;
580     DWORD cookie;
581     HRESULT hres;
582
583     hres = IUnknown_QueryInterface(unk, &IID_IConnectionPointContainer, (void**)&container);
584     ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
585
586     hres = IConnectionPointContainer_FindConnectionPoint(container, riid, &cp);
587     IConnectionPointContainer_Release(container);
588     ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
589
590     hres = IConnectionPoint_Advise(cp, unk_advise, &cookie);
591     IConnectionPoint_Release(cp);
592     ok(hres == S_OK, "Advise failed: %08x\n", hres);
593 }
594
595 typedef void (*domtest_t)(IHTMLDocument2*);
596
597 static void run_domtest(const char *str, domtest_t test)
598 {
599     IHTMLDocument2 *doc;
600     IHTMLElement *body = NULL;
601     ULONG ref;
602     MSG msg;
603     HRESULT hres;
604
605     doc = create_doc_with_string(str);
606     do_advise((IUnknown*)doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
607
608     while(!doc_complete && GetMessage(&msg, NULL, 0, 0)) {
609         TranslateMessage(&msg);
610         DispatchMessage(&msg);
611     }
612
613     hres = IHTMLDocument2_get_body(doc, &body);
614     ok(hres == S_OK, "get_body failed: %08x\n", hres);
615
616     if(body) {
617         IHTMLElement_Release(body);
618         test(doc);
619     }else {
620         skip("Could not get document body. Assuming no Gecko installed.\n");
621     }
622
623     ref = IHTMLDocument2_Release(doc);
624     ok(!ref, "ref = %d\n", ref);
625 }
626
627 static void gecko_installer_workaround(BOOL disable)
628 {
629     HKEY hkey;
630     DWORD res;
631
632     static BOOL has_url = FALSE;
633     static char url[2048];
634
635     if(!disable && !has_url)
636         return;
637
638     res = RegOpenKey(HKEY_CURRENT_USER, "Software\\Wine\\MSHTML", &hkey);
639     if(res != ERROR_SUCCESS)
640         return;
641
642     if(disable) {
643         DWORD type, size = sizeof(url);
644
645         res = RegQueryValueEx(hkey, "GeckoUrl", NULL, &type, (PVOID)url, &size);
646         if(res == ERROR_SUCCESS && type == REG_SZ)
647             has_url = TRUE;
648
649         RegDeleteValue(hkey, "GeckoUrl");
650     }else {
651         RegSetValueEx(hkey, "GeckoUrl", 0, REG_SZ, (PVOID)url, lstrlenA(url)+1);
652     }
653
654     RegCloseKey(hkey);
655 }
656
657 START_TEST(dom)
658 {
659     gecko_installer_workaround(TRUE);
660     CoInitialize(NULL);
661
662     run_domtest(doc_str1, test_doc_elem);
663     run_domtest(doc_str2, test_txtrange);
664     run_domtest(doc_blank, test_defaults);
665
666     CoUninitialize();
667     gecko_installer_workaround(FALSE);
668 }