comctl32: We can now store binary files in the repository.
[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_str1[] = "<html><body>test</body></html>";
33 static const char doc_str2[] =
34     "<html><body>test a<font size=\"2\">bc 123<br />it's </font>text<br /></body></html>";
35
36 static WCHAR characterW[] = {'c','h','a','r','a','c','t','e','r',0};
37 static WCHAR wordW[] = {'w','o','r','d',0};
38
39 static const char *dbgstr_w(LPCWSTR str)
40 {
41     static char buf[512];
42     if(!str)
43         return "(null)";
44     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
45     return buf;
46 }
47
48 static int strcmp_wa(LPCWSTR strw, const char *stra)
49 {
50     WCHAR buf[512];
51     MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
52     return lstrcmpW(strw, buf);
53 }
54
55 static IHTMLDocument2 *create_document(void)
56 {
57     IHTMLDocument2 *doc;
58     HRESULT hres;
59
60     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
61             &IID_IHTMLDocument2, (void**)&doc);
62     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
63
64     return doc;
65 }
66
67 #define test_node_name(u,n) _test_node_name(__LINE__,u,n)
68 static void _test_node_name(unsigned line, IUnknown *unk, const char *exname)
69 {
70     IHTMLDOMNode *node;
71     BSTR name;
72     HRESULT hres;
73
74     hres = IUnknown_QueryInterface(unk, &IID_IHTMLDOMNode, (void**)&node);
75     ok_(__FILE__, line) (hres == S_OK, "QueryInterface(IID_IHTMLNode) failed: %08x\n", hres);
76
77     hres = IHTMLDOMNode_get_nodeName(node, &name);
78     IHTMLDOMNode_Release(node);
79     ok_(__FILE__, line) (hres == S_OK, "get_nodeName failed: %08x\n", hres);
80     ok_(__FILE__, line) (!strcmp_wa(name, exname), "got name: %s, expected HTML\n", dbgstr_w(name));
81
82     SysFreeString(name);
83 }
84
85 static void test_doc_elem(IHTMLDocument2 *doc)
86 {
87     IHTMLElement *elem;
88     IHTMLDocument3 *doc3;
89     HRESULT hres;
90
91     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
92     ok(hres == S_OK, "QueryInterface(IID_IHTMLDocument3) failed: %08x\n", hres);
93
94     hres = IHTMLDocument3_get_documentElement(doc3, &elem);
95     IHTMLDocument3_Release(doc3);
96     ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
97
98     test_node_name((IUnknown*)elem, "HTML");
99     IHTMLElement_Release(elem);
100
101     hres = IHTMLDocument2_get_body(doc, &elem);
102     test_node_name((IUnknown*)elem, "BODY");
103     IHTMLElement_Release(elem);
104 }
105
106 #define test_range_text(r,t) _test_range_text(__LINE__,r,t)
107 static void _test_range_text(unsigned line, IHTMLTxtRange *range, const char *extext)
108 {
109     BSTR text;
110     HRESULT hres;
111
112     hres = IHTMLTxtRange_get_text(range, &text);
113     ok_(__FILE__, line) (hres == S_OK, "get_text failed: %08x\n", hres);
114
115     if(extext) {
116         ok_(__FILE__, line) (text != NULL, "text == NULL\n");
117         ok_(__FILE__, line) (!strcmp_wa(text, extext), "text=\"%s\", expected \"%s\"\n", dbgstr_w(text), extext);
118     }else {
119         ok_(__FILE__, line) (text == NULL, "text=\"%s\", expected NULL\n", dbgstr_w(text));
120     }
121
122     SysFreeString(text);
123
124 }
125
126 #define test_range_collapse(r,b) _test_range_collapse(__LINE__,r,b)
127 static void _test_range_collapse(unsigned line, IHTMLTxtRange *range, BOOL b)
128 {
129     HRESULT hres;
130
131     hres = IHTMLTxtRange_collapse(range, b);
132     ok_(__FILE__, line) (hres == S_OK, "collapse failed: %08x\n", hres);
133     _test_range_text(line, range, NULL);
134 }
135
136 #define test_range_expand(r,u,b,t) _test_range_expand(__LINE__,r,u,b,t)
137 static void _test_range_expand(unsigned line, IHTMLTxtRange *range, LPWSTR unit,
138         VARIANT_BOOL exb, const char *extext)
139 {
140     VARIANT_BOOL b = 0xe0e0;
141     HRESULT hres;
142
143     hres = IHTMLTxtRange_expand(range, unit, &b);
144     ok_(__FILE__,line) (hres == S_OK, "expand failed: %08x\n", hres);
145     ok_(__FILE__,line) (b == exb, "b=%x, expected %x\n", b, exb);
146     _test_range_text(line, range, extext);
147 }
148
149 #define test_range_move(r,u,c,e) _test_range_move(__LINE__,r,u,c,e)
150 static void _test_range_move(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
151 {
152     long c = 0xdeadbeef;
153     HRESULT hres;
154
155     hres = IHTMLTxtRange_move(range, unit, cnt, &c);
156     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
157     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
158     _test_range_text(line, range, NULL);
159 }
160
161 #define test_range_moveend(r,u,c,e) _test_range_moveend(__LINE__,r,u,c,e)
162 static void _test_range_moveend(unsigned line, IHTMLTxtRange *range, LPWSTR unit, long cnt, long excnt)
163 {
164     long c = 0xdeadbeef;
165     HRESULT hres;
166
167     hres = IHTMLTxtRange_moveEnd(range, unit, cnt, &c);
168     ok_(__FILE__,line) (hres == S_OK, "move failed: %08x\n", hres);
169     ok_(__FILE__,line) (c == excnt, "count=%ld, expected %ld\n", c, excnt);
170 }
171
172 #define test_range_put_text(r,t) _test_range_put_text(__LINE__,r,t)
173 static void _test_range_put_text(unsigned line, IHTMLTxtRange *range, LPCWSTR text)
174 {
175     HRESULT hres;
176
177     hres = IHTMLTxtRange_put_text(range, (BSTR)text);
178     ok_(__FILE__,line) (hres == S_OK, "put_text failed: %08x\n", hres);
179     _test_range_text(line, range, NULL);
180 }
181
182 #define test_range_inrange(r1,r2,b) _test_range_inrange(__LINE__,r1,r2,b)
183 static void _test_range_inrange(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
184 {
185     VARIANT_BOOL b;
186     HRESULT hres;
187
188     b = 0xe0e0;
189     hres = IHTMLTxtRange_inRange(range1, range2, &b);
190     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
191     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
192 }
193
194 #define test_range_isequal(r1,r2,b) _test_range_isequal(__LINE__,r1,r2,b)
195 static void _test_range_isequal(unsigned line, IHTMLTxtRange *range1, IHTMLTxtRange *range2, VARIANT_BOOL exb)
196 {
197     VARIANT_BOOL b;
198     HRESULT hres;
199
200     b = 0xe0e0;
201     hres = IHTMLTxtRange_isEqual(range1, range2, &b);
202     ok_(__FILE__,line) (hres == S_OK, "(1->2) isEqual failed: %08x\n", hres);
203     ok_(__FILE__,line) (b == exb, "(1->2) b=%x, expected %x\n", b, exb);
204
205     b = 0xe0e0;
206     hres = IHTMLTxtRange_isEqual(range2, range1, &b);
207     ok_(__FILE__,line) (hres == S_OK, "(2->1) isEqual failed: %08x\n", hres);
208     ok_(__FILE__,line) (b == exb, "(2->1) b=%x, expected %x\n", b, exb);
209
210     if(exb) {
211         test_range_inrange(range1, range2, VARIANT_TRUE);
212         test_range_inrange(range2, range1, VARIANT_TRUE);
213     }
214 }
215
216 static void test_txtrange(IHTMLDocument2 *doc)
217 {
218     IHTMLElement *elem;
219     IHTMLBodyElement *body;
220     IHTMLTxtRange *body_range, *range, *range2;
221     HRESULT hres;
222
223     hres = IHTMLDocument2_get_body(doc, &elem);
224     ok(hres == S_OK, "get_body failed: %08x\n", hres);
225
226     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
227     IHTMLElement_Release(elem);
228
229     hres = IHTMLBodyElement_createTextRange(body, &body_range);
230     IHTMLBodyElement_Release(body);
231     ok(hres == S_OK, "createTextRange failed: %08x\n", hres);
232
233     test_range_text(body_range, "test abc 123\r\nit's text");
234
235     hres = IHTMLTxtRange_duplicate(body_range, &range);
236     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
237
238     hres = IHTMLTxtRange_duplicate(body_range, &range2);
239     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
240     test_range_isequal(range, range2, VARIANT_TRUE);
241
242     test_range_text(range, "test abc 123\r\nit's text");
243     test_range_text(body_range, "test abc 123\r\nit's text");
244
245     test_range_collapse(range, TRUE);
246     test_range_isequal(range, range2, VARIANT_FALSE);
247     test_range_inrange(range, range2, VARIANT_FALSE);
248     test_range_inrange(range2, range, VARIANT_TRUE);
249     IHTMLTxtRange_Release(range2);
250
251     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
252     test_range_expand(range, wordW, VARIANT_FALSE, "test ");
253     test_range_move(range, characterW, 2, 2);
254     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
255
256     test_range_collapse(range, FALSE);
257     test_range_expand(range, wordW, VARIANT_TRUE, "abc ");
258
259     test_range_collapse(range, FALSE);
260     test_range_expand(range, wordW, VARIANT_TRUE, "123");
261     test_range_expand(range, wordW, VARIANT_FALSE, "123");
262     test_range_move(range, characterW, 2, 2);
263     test_range_expand(range, wordW, VARIANT_TRUE, "123");
264
265     IHTMLTxtRange_Release(range);
266
267     hres = IHTMLTxtRange_duplicate(body_range, &range);
268     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
269
270     test_range_text(range, "test abc 123\r\nit's text");
271     test_range_move(range, characterW, 3, 3);
272     test_range_moveend(range, characterW, 1, 1);
273     test_range_text(range, "t");
274     test_range_moveend(range, characterW, 3, 3);
275     test_range_text(range, "t ab");
276     test_range_moveend(range, characterW, -2, -2);
277     test_range_text(range, "t ");
278     test_range_move(range, characterW, 6, 6);
279     test_range_moveend(range, characterW, 3, 3);
280     test_range_text(range, "123");
281     test_range_moveend(range, characterW, 2, 2);
282     test_range_text(range, "123\r\ni");
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_move(range, wordW, 1, 1);
290     test_range_moveend(range, characterW, 2, 2);
291     test_range_text(range, "ab");
292
293     test_range_move(range, characterW, -2, -2);
294     test_range_moveend(range, characterW, 2, 2);
295     test_range_text(range, "t ");
296
297     test_range_move(range, wordW, 3, 3);
298     test_range_move(range, wordW, -2, -2);
299     test_range_moveend(range, characterW, 2, 2);
300     test_range_text(range, "ab");
301
302     test_range_move(range, characterW, -6, -5);
303     test_range_moveend(range, characterW, -1, 0);
304     test_range_moveend(range, characterW, -6, 0);
305     test_range_move(range, characterW, 2, 2);
306     test_range_moveend(range, characterW, 2, 2);
307     test_range_text(range, "st");
308     test_range_moveend(range, characterW, -6, -4);
309     test_range_moveend(range, characterW, 2, 2);
310
311     IHTMLTxtRange_Release(range);
312
313     hres = IHTMLTxtRange_duplicate(body_range, &range);
314     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
315
316     test_range_move(range, wordW, 2, 2);
317     test_range_moveend(range, characterW, 2, 2);
318     test_range_text(range, "12");
319
320     test_range_move(range, characterW, 15, 14);
321     test_range_move(range, characterW, -2, -2);
322     test_range_moveend(range, characterW, 3, 2);
323     test_range_text(range, "t");
324     test_range_moveend(range, characterW, -1, -1);
325     test_range_text(range, "t");
326     test_range_expand(range, wordW, VARIANT_TRUE, "text");
327     test_range_move(range, characterW, -2, -2);
328     test_range_moveend(range, characterW, 2, 2);
329     test_range_text(range, "s ");
330     test_range_move(range, characterW, 100, 7);
331     test_range_move(range, wordW, 1, 0);
332     test_range_move(range, characterW, -2, -2);
333     test_range_moveend(range, characterW, 3, 2);
334     test_range_text(range, "t");
335
336     IHTMLTxtRange_Release(range);
337
338     hres = IHTMLTxtRange_duplicate(body_range, &range);
339     ok(hres == S_OK, "duplicate failed: %08x\n", hres);
340
341     test_range_collapse(range, TRUE);
342     test_range_expand(range, wordW, VARIANT_TRUE, "test ");
343     test_range_put_text(range, wordW);
344     test_range_text(body_range, "wordabc 123\r\nit's text");
345
346     IHTMLTxtRange_Release(range);
347     IHTMLTxtRange_Release(body_range);
348 }
349
350 static IHTMLDocument2 *notif_doc;
351 static BOOL doc_complete;
352
353 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
354         REFIID riid, void**ppv)
355 {
356     if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
357         *ppv = iface;
358         return S_OK;
359     }
360
361     ok(0, "unexpected call\n");
362     return E_NOINTERFACE;
363 }
364
365 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
366 {
367     return 2;
368 }
369
370 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
371 {
372     return 1;
373 }
374
375 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
376 {
377     if(dispID == DISPID_READYSTATE){
378         BSTR state;
379         HRESULT hres;
380
381         static const WCHAR completeW[] = {'c','o','m','p','l','e','t','e',0};
382
383         hres = IHTMLDocument2_get_readyState(notif_doc, &state);
384         ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
385
386         if(!lstrcmpW(state, completeW))
387             doc_complete = TRUE;
388
389         SysFreeString(state);        
390     }
391
392     return S_OK;
393 }
394
395 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
396 {
397     ok(0, "unexpected call\n");
398     return E_NOTIMPL;
399 }
400
401 static IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
402     PropertyNotifySink_QueryInterface,
403     PropertyNotifySink_AddRef,
404     PropertyNotifySink_Release,
405     PropertyNotifySink_OnChanged,
406     PropertyNotifySink_OnRequestEdit
407 };
408
409 static IPropertyNotifySink PropertyNotifySink = { &PropertyNotifySinkVtbl };
410
411 static IHTMLDocument2 *create_doc_with_string(const char *str)
412 {
413     IPersistStreamInit *init;
414     IStream *stream;
415     IHTMLDocument2 *doc;
416     HGLOBAL mem;
417     SIZE_T len;
418
419     notif_doc = doc = create_document();
420     if(!doc)
421         return NULL;
422
423     doc_complete = FALSE;
424     len = strlen(str);
425     mem = GlobalAlloc(0, len);
426     memcpy(mem, str, len);
427     CreateStreamOnHGlobal(mem, TRUE, &stream);
428
429     IHTMLDocument2_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&init);
430
431     IPersistStreamInit_Load(init, stream);
432     IPersistStreamInit_Release(init);
433     IStream_Release(stream);
434
435     return doc;
436 }
437
438 static void do_advise(IUnknown *unk, REFIID riid, IUnknown *unk_advise)
439 {
440     IConnectionPointContainer *container;
441     IConnectionPoint *cp;
442     DWORD cookie;
443     HRESULT hres;
444
445     hres = IUnknown_QueryInterface(unk, &IID_IConnectionPointContainer, (void**)&container);
446     ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
447
448     hres = IConnectionPointContainer_FindConnectionPoint(container, riid, &cp);
449     IConnectionPointContainer_Release(container);
450     ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
451
452     hres = IConnectionPoint_Advise(cp, unk_advise, &cookie);
453     IConnectionPoint_Release(cp);
454     ok(hres == S_OK, "Advise failed: %08x\n", hres);
455 }
456
457 typedef void (*domtest_t)(IHTMLDocument2*);
458
459 static void run_domtest(const char *str, domtest_t test)
460 {
461     IHTMLDocument2 *doc;
462     ULONG ref;
463     MSG msg;
464
465     doc = create_doc_with_string(str);
466     do_advise((IUnknown*)doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
467
468     while(!doc_complete && GetMessage(&msg, NULL, 0, 0)) {
469         TranslateMessage(&msg);
470         DispatchMessage(&msg);
471     }
472
473     test(doc);
474
475     ref = IHTMLDocument2_Release(doc);
476     ok(!ref, "ref = %d\n", ref);
477 }
478
479 static void gecko_installer_workaround(BOOL disable)
480 {
481     HKEY hkey;
482     DWORD res;
483
484     static BOOL has_url = FALSE;
485     static char url[2048];
486
487     if(!disable && !has_url)
488         return;
489
490     res = RegOpenKey(HKEY_CURRENT_USER, "Software\\Wine\\MSHTML", &hkey);
491     if(res != ERROR_SUCCESS)
492         return;
493
494     if(disable) {
495         DWORD type, size = sizeof(url);
496
497         res = RegQueryValueEx(hkey, "GeckoUrl", NULL, &type, (PVOID)url, &size);
498         if(res == ERROR_SUCCESS && type == REG_SZ)
499             has_url = TRUE;
500
501         RegDeleteValue(hkey, "GeckoUrl");
502     }else {
503         RegSetValueEx(hkey, "GeckoUrl", 0, REG_SZ, (PVOID)url, lstrlenA(url)+1);
504     }
505
506     RegCloseKey(hkey);
507 }
508
509 START_TEST(dom)
510 {
511     gecko_installer_workaround(TRUE);
512     CoInitialize(NULL);
513
514     run_domtest(doc_str1, test_doc_elem);
515     run_domtest(doc_str2, test_txtrange);
516
517     CoUninitialize();
518     gecko_installer_workaround(FALSE);
519 }