2 * Copyright 2007 Jacek Caban for CodeWeavers
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.
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.
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
22 #include <wine/test.h>
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>";
37 static const WCHAR noneW[] = {'N','o','n','e',0};
39 static WCHAR characterW[] = {'c','h','a','r','a','c','t','e','r',0};
40 static WCHAR wordW[] = {'w','o','r','d',0};
42 static const char *dbgstr_w(LPCWSTR str)
47 WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
51 static int strcmp_wa(LPCWSTR strw, const char *stra)
54 MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, sizeof(buf)/sizeof(WCHAR));
55 return lstrcmpW(strw, buf);
58 static IHTMLDocument2 *create_document(void)
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);
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)
77 hres = IUnknown_QueryInterface(unk, &IID_IHTMLDOMNode, (void**)&node);
78 ok_(__FILE__, line) (hres == S_OK, "QueryInterface(IID_IHTMLNode) failed: %08x\n", hres);
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);
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)
95 hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement, (void**)&elem);
96 ok_(__FILE__, line) (hres == S_OK, "QueryInterface(IID_IHTMLElement) failed: %08x\n", hres);
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);
106 static void test_doc_elem(IHTMLDocument2 *doc)
109 IHTMLDocument3 *doc3;
112 hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
113 ok(hres == S_OK, "QueryInterface(IID_IHTMLDocument3) failed: %08x\n", hres);
115 hres = IHTMLDocument3_get_documentElement(doc3, &elem);
116 IHTMLDocument3_Release(doc3);
117 ok(hres == S_OK, "get_documentElement failed: %08x\n", hres);
119 test_node_name((IUnknown*)elem, "HTML");
120 test_elem_tag((IUnknown*)elem, "HTML");
122 IHTMLElement_Release(elem);
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)
131 hres = IHTMLTxtRange_get_text(range, &text);
132 ok_(__FILE__, line) (hres == S_OK, "get_text failed: %08x\n", hres);
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);
138 ok_(__FILE__, line) (text == NULL, "text=\"%s\", expected NULL\n", dbgstr_w(text));
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)
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);
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)
159 VARIANT_BOOL b = 0xe0e0;
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);
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)
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);
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)
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);
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)
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);
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)
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);
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)
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);
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);
230 test_range_inrange(range1, range2, VARIANT_TRUE);
231 test_range_inrange(range2, range1, VARIANT_TRUE);
235 static void test_txtrange(IHTMLDocument2 *doc)
238 IHTMLBodyElement *body;
239 IHTMLTxtRange *body_range, *range, *range2;
242 hres = IHTMLDocument2_get_body(doc, &elem);
243 ok(hres == S_OK, "get_body failed: %08x\n", hres);
245 hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLBodyElement, (void**)&body);
246 IHTMLElement_Release(elem);
248 hres = IHTMLBodyElement_createTextRange(body, &body_range);
249 IHTMLBodyElement_Release(body);
250 ok(hres == S_OK, "createTextRange failed: %08x\n", hres);
252 test_range_text(body_range, "test abc 123\r\nit's text");
254 hres = IHTMLTxtRange_duplicate(body_range, &range);
255 ok(hres == S_OK, "duplicate failed: %08x\n", hres);
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);
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");
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);
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 ");
275 test_range_collapse(range, FALSE);
276 test_range_expand(range, wordW, VARIANT_TRUE, "abc ");
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");
284 IHTMLTxtRange_Release(range);
286 hres = IHTMLTxtRange_duplicate(body_range, &range);
287 ok(hres == S_OK, "duplicate failed: %08x\n", hres);
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");
303 IHTMLTxtRange_Release(range);
305 hres = IHTMLTxtRange_duplicate(body_range, &range);
306 ok(hres == S_OK, "duplicate failed: %08x\n", hres);
308 test_range_move(range, wordW, 1, 1);
309 test_range_moveend(range, characterW, 2, 2);
310 test_range_text(range, "ab");
312 test_range_move(range, characterW, -2, -2);
313 test_range_moveend(range, characterW, 2, 2);
314 test_range_text(range, "t ");
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");
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);
330 IHTMLTxtRange_Release(range);
332 hres = IHTMLTxtRange_duplicate(body_range, &range);
333 ok(hres == S_OK, "duplicate failed: %08x\n", hres);
335 test_range_move(range, wordW, 2, 2);
336 test_range_moveend(range, characterW, 2, 2);
337 test_range_text(range, "12");
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");
355 IHTMLTxtRange_Release(range);
357 hres = IHTMLTxtRange_duplicate(body_range, &range);
358 ok(hres == S_OK, "duplicate failed: %08x\n", hres);
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");
365 IHTMLTxtRange_Release(range);
366 IHTMLTxtRange_Release(body_range);
369 static void test_compatmode(IHTMLDocument2 *doc)
371 IHTMLDocument5 *doc5;
375 hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument5, (void**)&doc5);
376 ok(hres == S_OK, "Could not get IHTMLDocument5 interface: %08x\n", hres);
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));
387 static void test_default_style(IHTMLStyle *style)
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));
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));
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)));
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)));
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);
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);
427 static void test_default_selection(IHTMLDocument2 *doc)
429 IHTMLSelectionObject *selection;
430 IHTMLTxtRange *range;
435 hres = IHTMLDocument2_get_selection(doc, &selection);
436 ok(hres == S_OK, "get_selection failed: %08x\n", hres);
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));
443 hres = IHTMLSelectionObject_createRange(selection, &disp);
444 IHTMLSelectionObject_Release(selection);
445 ok(hres == S_OK, "createRange failed: %08x\n", hres);
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);
451 test_range_text(range, NULL);
452 IHTMLTxtRange_Release(range);
455 static void test_defaults(IHTMLDocument2 *doc)
457 IHTMLStyleSheetsCollection *stylesheetcol;
463 hres = IHTMLDocument2_get_body(doc, &elem);
464 ok(hres == S_OK, "get_body failed: %08x\n", hres);
466 hres = IHTMLElement_get_style(elem, &style);
467 IHTMLElement_Release(elem);
468 ok(hres == S_OK, "get_style failed: %08x\n", hres);
470 test_default_style(style);
471 test_compatmode(doc);
473 IHTMLStyle_Release(style);
475 hres = IHTMLDocument2_get_styleSheets(doc, &stylesheetcol);
476 ok(hres == S_OK, "get_styleSheets failed: %08x\n", hres);
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);
483 IHTMLStyleSheetsCollection_Release(stylesheetcol);
485 test_default_selection(doc);
488 static IHTMLDocument2 *notif_doc;
489 static BOOL doc_complete;
491 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
492 REFIID riid, void**ppv)
494 if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
499 ok(0, "unexpected call\n");
500 return E_NOINTERFACE;
503 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
508 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
513 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
515 if(dispID == DISPID_READYSTATE){
519 static const WCHAR completeW[] = {'c','o','m','p','l','e','t','e',0};
521 hres = IHTMLDocument2_get_readyState(notif_doc, &state);
522 ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
524 if(!lstrcmpW(state, completeW))
527 SysFreeString(state);
533 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
535 ok(0, "unexpected call\n");
539 static IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
540 PropertyNotifySink_QueryInterface,
541 PropertyNotifySink_AddRef,
542 PropertyNotifySink_Release,
543 PropertyNotifySink_OnChanged,
544 PropertyNotifySink_OnRequestEdit
547 static IPropertyNotifySink PropertyNotifySink = { &PropertyNotifySinkVtbl };
549 static IHTMLDocument2 *create_doc_with_string(const char *str)
551 IPersistStreamInit *init;
557 notif_doc = doc = create_document();
561 doc_complete = FALSE;
563 mem = GlobalAlloc(0, len);
564 memcpy(mem, str, len);
565 CreateStreamOnHGlobal(mem, TRUE, &stream);
567 IHTMLDocument2_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&init);
569 IPersistStreamInit_Load(init, stream);
570 IPersistStreamInit_Release(init);
571 IStream_Release(stream);
576 static void do_advise(IUnknown *unk, REFIID riid, IUnknown *unk_advise)
578 IConnectionPointContainer *container;
579 IConnectionPoint *cp;
583 hres = IUnknown_QueryInterface(unk, &IID_IConnectionPointContainer, (void**)&container);
584 ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
586 hres = IConnectionPointContainer_FindConnectionPoint(container, riid, &cp);
587 IConnectionPointContainer_Release(container);
588 ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
590 hres = IConnectionPoint_Advise(cp, unk_advise, &cookie);
591 IConnectionPoint_Release(cp);
592 ok(hres == S_OK, "Advise failed: %08x\n", hres);
595 typedef void (*domtest_t)(IHTMLDocument2*);
597 static void run_domtest(const char *str, domtest_t test)
600 IHTMLElement *body = NULL;
605 doc = create_doc_with_string(str);
606 do_advise((IUnknown*)doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
608 while(!doc_complete && GetMessage(&msg, NULL, 0, 0)) {
609 TranslateMessage(&msg);
610 DispatchMessage(&msg);
613 hres = IHTMLDocument2_get_body(doc, &body);
614 ok(hres == S_OK, "get_body failed: %08x\n", hres);
617 IHTMLElement_Release(body);
620 skip("Could not get document body. Assuming no Gecko installed.\n");
623 ref = IHTMLDocument2_Release(doc);
624 ok(!ref, "ref = %d\n", ref);
627 static void gecko_installer_workaround(BOOL disable)
632 static BOOL has_url = FALSE;
633 static char url[2048];
635 if(!disable && !has_url)
638 res = RegOpenKey(HKEY_CURRENT_USER, "Software\\Wine\\MSHTML", &hkey);
639 if(res != ERROR_SUCCESS)
643 DWORD type, size = sizeof(url);
645 res = RegQueryValueEx(hkey, "GeckoUrl", NULL, &type, (PVOID)url, &size);
646 if(res == ERROR_SUCCESS && type == REG_SZ)
649 RegDeleteValue(hkey, "GeckoUrl");
651 RegSetValueEx(hkey, "GeckoUrl", 0, REG_SZ, (PVOID)url, lstrlenA(url)+1);
659 gecko_installer_workaround(TRUE);
662 run_domtest(doc_str1, test_doc_elem);
663 run_domtest(doc_str2, test_txtrange);
664 run_domtest(doc_blank, test_defaults);
667 gecko_installer_workaround(FALSE);