Fix IXMLDOMDocument error handling.
[wine/gsoc_dplay.git] / dlls / msxml3 / tests / domdoc.c
blobeacb747791d9d1cc7cc5757b23afb144739269a3
1 /*
2 * XML test
4 * Copyright 2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define COBJMACROS
24 #include "windows.h"
25 #include "ole2.h"
26 #include "msxml.h"
27 #include "xmldom.h"
28 #include <stdio.h>
30 #include "wine/test.h"
32 static const WCHAR szEmpty[] = { 0 };
33 static const WCHAR szIncomplete[] = {
34 '<','?','x','m','l',' ',
35 'v','e','r','s','i','o','n','=','\'','1','.','0','\'','?','>','\n',0
37 static const WCHAR szComplete1[] = {
38 '<','?','x','m','l',' ',
39 'v','e','r','s','i','o','n','=','\'','1','.','0','\'','?','>','\n',
40 '<','o','p','e','n','>','<','/','o','p','e','n','>','\n',0
42 static const WCHAR szComplete2[] = {
43 '<','?','x','m','l',' ',
44 'v','e','r','s','i','o','n','=','\'','1','.','0','\'','?','>','\n',
45 '<','o','>','<','/','o','>','\n',0
47 static const WCHAR szComplete3[] = {
48 '<','?','x','m','l',' ',
49 'v','e','r','s','i','o','n','=','\'','1','.','0','\'','?','>','\n',
50 '<','a','>','<','/','a','>','\n',0
52 static const WCHAR szComplete4[] = {
53 '<','?','x','m','l',' ',
54 'v','e','r','s','i','o','n','=','\'','1','.','0','\'','?','>',
55 '<','l','c',' ','d','l','=','\'','s','t','r','1','\'','>',
56 '<','b','s',' ','v','r','=','\'','s','t','r','2','\'',' ',
57 's','z','=','\'','1','2','3','4','\'','>','f','n','1','.','t','x','t','<','/','b','s','>',
58 '<','p','r',' ','i','d','=','\'','s','t','r','3','\'',' ',
59 'v','r','=','\'','1','.','2','.','3','\'',' ',
60 'p','n','=','\'','w','i','n','e',' ','2','0','0','5','0','8','0','4','\'','>',
61 'f','n','2','.','t','x','t','<','/','p','r','>',
62 '<','/','l','c','>','\n',0
64 static const WCHAR szNonExistentFile[] = {
65 'c', ':', '\\', 'N', 'o', 'n', 'e', 'x', 'i', 's', 't', 'e', 'n', 't', '.', 'x', 'm', 'l', 0
68 static const WCHAR szOpen[] = { 'o','p','e','n',0 };
69 static const WCHAR szdl[] = { 'd','l',0 };
70 static const WCHAR szlc[] = { 'l','c',0 };
71 static const WCHAR szbs[] = { 'b','s',0 };
73 const GUID CLSID_DOMDocument =
74 { 0x2933BF90, 0x7B36, 0x11d2, {0xB2,0x0E,0x00,0xC0,0x4F,0x98,0x3E,0x60}};
75 const GUID IID_IXMLDOMDocument =
76 { 0x2933BF81, 0x7B36, 0x11d2, {0xB2,0x0E,0x00,0xC0,0x4F,0x98,0x3E,0x60}};
78 void test_domdoc( void )
80 HRESULT r;
81 IXMLDOMDocument *doc = NULL;
82 IXMLDOMElement *element = NULL;
83 VARIANT_BOOL b;
84 VARIANT var;
85 BSTR str;
87 r = CoCreateInstance( &CLSID_DOMDocument, NULL,
88 CLSCTX_INPROC_SERVER, &IID_IXMLDOMDocument, (LPVOID*)&doc );
89 if( r != S_OK )
90 return;
92 /* try some stupid things */
93 r = IXMLDOMDocument_loadXML( doc, NULL, NULL );
94 ok( r == S_FALSE, "loadXML failed\n");
96 b = VARIANT_TRUE;
97 r = IXMLDOMDocument_loadXML( doc, NULL, &b );
98 ok( r == S_FALSE, "loadXML failed\n");
99 ok( b == VARIANT_FALSE, "failed to load XML string\n");
101 /* try to laod an document from an non-existent file */
102 b = VARIANT_TRUE;
103 str = SysAllocString ( szNonExistentFile );
104 VariantInit(&var);
105 V_VT(&var) = VT_BSTR;
106 V_BSTR(&var) = str;
108 r = IXMLDOMDocument_load( doc, var, &b);
109 ok( r == S_FALSE, "load (from file) failed\n");
110 ok( b == VARIANT_FALSE, "failed to load XML file\n");
111 SysFreeString( str );
113 /* try load an empty document */
114 b = VARIANT_TRUE;
115 str = SysAllocString( szEmpty );
116 r = IXMLDOMDocument_loadXML( doc, str, &b );
117 ok( r == S_FALSE, "loadXML failed\n");
118 ok( b == VARIANT_FALSE, "failed to load XML string\n");
119 SysFreeString( str );
121 /* check that there's no document element */
122 element = NULL;
123 r = IXMLDOMDocument_get_documentElement( doc, &element );
124 ok( r == S_FALSE, "should be no document element\n");
126 b = VARIANT_TRUE;
127 str = SysAllocString( szIncomplete );
128 r = IXMLDOMDocument_loadXML( doc, str, &b );
129 ok( r == S_FALSE, "loadXML failed\n");
130 ok( b == VARIANT_FALSE, "failed to load XML string\n");
131 SysFreeString( str );
133 /* check that there's no document element */
134 element = (IXMLDOMElement*)1;
135 r = IXMLDOMDocument_get_documentElement( doc, &element );
136 ok( r == S_FALSE, "should be no document element\n");
137 ok( element == NULL, "Element should be NULL\n");
139 /* try to load something valid */
140 b = VARIANT_FALSE;
141 str = SysAllocString( szComplete1 );
142 r = IXMLDOMDocument_loadXML( doc, str, &b );
143 ok( r == S_OK, "loadXML failed\n");
144 ok( b == VARIANT_TRUE, "failed to load XML string\n");
145 SysFreeString( str );
147 /* try with a null out pointer - crashes */
148 if (0)
150 r = IXMLDOMDocument_get_documentElement( doc, NULL );
151 ok( r == S_OK, "should be no document element\n");
154 /* check that there's no document element */
155 element = NULL;
156 r = IXMLDOMDocument_get_documentElement( doc, &element );
157 ok( r == S_OK, "should be a document element\n");
158 if( element )
160 BSTR tag = NULL;
162 /* check if the tag is correct */
163 r = IXMLDOMElement_get_tagName( element, &tag );
164 ok( r == S_OK, "couldn't get tag name\n");
165 ok( tag != NULL, "tag was null\n");
166 ok( !lstrcmpW( tag, szOpen ), "incorrect tag name\n");
167 SysFreeString( tag );
169 /* figure out what happens if we try to reload the document */
170 str = SysAllocString( szComplete2 );
171 r = IXMLDOMDocument_loadXML( doc, str, &b );
172 ok( r == S_OK, "loadXML failed\n");
173 ok( b == VARIANT_TRUE, "failed to load XML string\n");
174 SysFreeString( str );
176 /* check if the tag is still correct */
177 tag = NULL;
178 r = IXMLDOMElement_get_tagName( element, &tag );
179 ok( r == S_OK, "couldn't get tag name\n");
180 ok( tag != NULL, "tag was null\n");
181 ok( !lstrcmpW( tag, szOpen ), "incorrect tag name\n");
182 SysFreeString( tag );
184 IXMLDOMElement_Release( element );
185 element = NULL;
188 /* as soon as we call loadXML again, the document element will disappear */
189 b = 2;
190 r = IXMLDOMDocument_loadXML( doc, NULL, NULL );
191 ok( r == S_FALSE, "loadXML failed\n");
192 ok( b == 2, "variant modified\n");
193 r = IXMLDOMDocument_get_documentElement( doc, &element );
194 ok( r == S_FALSE, "should be no document element\n");
196 /* try to load something else simple and valid */
197 b = VARIANT_FALSE;
198 str = SysAllocString( szComplete3 );
199 r = IXMLDOMDocument_loadXML( doc, str, &b );
200 ok( r == S_OK, "loadXML failed\n");
201 ok( b == VARIANT_TRUE, "failed to load XML string\n");
202 SysFreeString( str );
204 /* try something a little more complicated */
205 b = FALSE;
206 str = SysAllocString( szComplete4 );
207 r = IXMLDOMDocument_loadXML( doc, str, &b );
208 ok( r == S_OK, "loadXML failed\n");
209 ok( b == VARIANT_TRUE, "failed to load XML string\n");
210 SysFreeString( str );
212 r = IXMLDocument_Release( doc );
213 ok( r == 0, "document ref count incorrect\n");
217 void test_domnode( void )
219 HRESULT r;
220 IXMLDOMDocument *doc = NULL, *owner = NULL;
221 IXMLDOMElement *element = NULL;
222 IXMLDOMNamedNodeMap *map = NULL;
223 IXMLDOMNode *node = NULL, *next = NULL;
224 IXMLDOMNodeList *list = NULL;
225 DOMNodeType type = NODE_INVALID;
226 VARIANT_BOOL b;
227 BSTR str;
228 VARIANT var;
230 r = CoCreateInstance( &CLSID_DOMDocument, NULL,
231 CLSCTX_INPROC_SERVER, &IID_IXMLDOMDocument, (LPVOID*)&doc );
232 if( r != S_OK )
233 return;
235 b = FALSE;
236 str = SysAllocString( szComplete4 );
237 r = IXMLDOMDocument_loadXML( doc, str, &b );
238 ok( r == S_OK, "loadXML failed\n");
239 ok( b == VARIANT_TRUE, "failed to load XML string\n");
240 SysFreeString( str );
242 if (doc)
244 b = 1;
245 r = IXMLDOMNode_hasChildNodes( doc, &b );
246 ok( r == S_OK, "hasChildNoes bad return\n");
247 ok( b == VARIANT_TRUE, "hasChildNoes wrong result\n");
249 r = IXMLDOMDocument_get_documentElement( doc, &element );
250 ok( r == S_OK, "should be a document element\n");
251 ok( element != NULL, "should be an element\n");
253 else
254 ok( FALSE, "no document\n");
256 VariantInit(&var);
257 ok( V_VT(&var) == VT_EMPTY, "variant init failed\n");
258 r = IXMLDOMNode_get_nodeValue( doc, &var );
259 ok( r == S_FALSE, "nextNode returned wrong code\n");
260 ok( V_VT(&var) == VT_NULL, "variant wasn't empty\n");
261 ok( V_BSTR(&var) == NULL, "variant value wasn't null\n");
263 if (element)
265 owner = NULL;
266 r = IXMLDOMNode_get_ownerDocument( element, &owner );
267 todo_wine {
268 ok( r == S_OK, "get_ownerDocument return code\n");
270 ok( owner != doc, "get_ownerDocument return\n");
272 type = NODE_INVALID;
273 r = IXMLDOMNode_get_nodeType( element, &type);
274 ok( r == S_OK, "getNamedItem returned wrong code\n");
275 ok( type == NODE_ELEMENT, "node not an element\n");
277 str = NULL;
278 r = IXMLDOMNode_get_baseName( element, &str );
279 ok( r == S_OK, "get_baseName returned wrong code\n");
280 ok( lstrcmpW(str,szlc) == 0, "basename was wrong\n");
282 r = IXMLDOMElement_get_attributes( element, &map );
283 ok( r == S_OK, "get_attributes returned wrong code\n");
284 ok( map != NULL, "should be attributes\n");
286 b = 1;
287 r = IXMLDOMNode_hasChildNodes( element, &b );
288 ok( r == S_OK, "hasChildNoes bad return\n");
289 ok( b == VARIANT_TRUE, "hasChildNoes wrong result\n");
291 else
292 ok( FALSE, "no element\n");
294 if (map)
296 str = SysAllocString( szdl );
297 r = IXMLDOMNamedNodeMap_getNamedItem( map, str, &node );
298 ok( r == S_OK, "getNamedItem returned wrong code\n");
299 ok( node != NULL, "should be attributes\n");
301 else
302 ok( FALSE, "no map\n");
304 if (node)
306 type = NODE_INVALID;
307 r = IXMLDOMNode_get_nodeType( node, &type);
308 ok( r == S_OK, "getNamedItem returned wrong code\n");
309 ok( type == NODE_ATTRIBUTE, "node not an attribute\n");
311 str = NULL;
312 r = IXMLDOMNode_get_baseName( node, NULL );
313 ok( r == E_INVALIDARG, "get_baseName returned wrong code\n");
315 str = NULL;
316 r = IXMLDOMNode_get_baseName( node, &str );
317 ok( r == S_OK, "get_baseName returned wrong code\n");
318 ok( lstrcmpW(str,szdl) == 0, "basename was wrong\n");
320 r = IXMLDOMNode_get_childNodes( node, NULL );
321 ok( r == E_INVALIDARG, "get_childNodes returned wrong code\n");
323 r = IXMLDOMNode_get_childNodes( node, &list );
324 ok( r == S_OK, "get_childNodes returned wrong code\n");
326 if (list)
328 r = IXMLDOMNodeList_nextNode( list, &next );
329 ok( r == S_OK, "nextNode returned wrong code\n");
331 else
332 ok( FALSE, "no childlist\n");
334 if (next)
336 b = 1;
337 r = IXMLDOMNode_hasChildNodes( next, &b );
338 ok( r == S_FALSE, "hasChildNoes bad return\n");
339 ok( b == VARIANT_FALSE, "hasChildNoes wrong result\n");
341 type = NODE_INVALID;
342 r = IXMLDOMNode_get_nodeType( next, &type);
343 ok( r == S_OK, "getNamedItem returned wrong code\n");
344 ok( type == NODE_TEXT, "node not text\n");
346 str = (BSTR) 1;
347 r = IXMLDOMNode_get_baseName( next, &str );
348 ok( r == S_FALSE, "get_baseName returned wrong code\n");
349 ok( str == NULL, "basename was wrong\n");
351 else
352 ok( FALSE, "no next\n");
354 if (next)
355 IXMLDOMNode_Release( next );
356 next = NULL;
357 if (list)
358 IXMLDOMNodeList_Release( list );
359 list = NULL;
360 if (node)
361 IXMLDOMNode_Release( node );
363 else
364 ok( FALSE, "no node\n");
365 node = NULL;
367 if (map)
368 IXMLDOMNamedNodeMap_Release( map );
370 /* now traverse the tree from the root node */
371 if (element)
373 r = IXMLDOMNode_get_childNodes( element, &list );
374 ok( r == S_OK, "get_childNodes returned wrong code\n");
376 else
377 ok( FALSE, "no element\n");
379 if (list)
381 r = IXMLDOMNodeList_nextNode( list, &node );
382 ok( r == S_OK, "nextNode returned wrong code\n");
384 else
385 ok( FALSE, "no list\n");
387 if (node)
389 type = NODE_INVALID;
390 r = IXMLDOMNode_get_nodeType( node, &type);
391 ok( r == S_OK, "getNamedItem returned wrong code\n");
392 ok( type == NODE_ELEMENT, "node not text\n");
394 VariantInit(&var);
395 ok( V_VT(&var) == VT_EMPTY, "variant init failed\n");
396 r = IXMLDOMNode_get_nodeValue( node, &var );
397 ok( r == S_FALSE, "nextNode returned wrong code\n");
398 ok( V_VT(&var) == VT_NULL, "variant wasn't empty\n");
399 ok( V_BSTR(&var) == NULL, "variant value wasn't null\n");
401 r = IXMLDOMNode_hasChildNodes( node, NULL );
402 ok( r == E_INVALIDARG, "hasChildNoes bad return\n");
404 b = 1;
405 r = IXMLDOMNode_hasChildNodes( node, &b );
406 ok( r == S_OK, "hasChildNoes bad return\n");
407 ok( b == VARIANT_TRUE, "hasChildNoes wrong result\n");
409 str = NULL;
410 r = IXMLDOMNode_get_baseName( node, &str );
411 ok( r == S_OK, "get_baseName returned wrong code\n");
412 ok( lstrcmpW(str,szbs) == 0, "basename was wrong\n");
414 else
415 ok( FALSE, "no node\n");
417 if (node)
418 IXMLDOMNode_Release( node );
419 if (list)
420 IXMLDOMNodeList_Release( list );
421 if (element)
422 IXMLDOMElement_Release( element );
423 if (doc)
424 IXMLDocument_Release( doc );
427 START_TEST(domdoc)
429 HRESULT r;
431 r = CoInitialize( NULL );
432 ok( r == S_OK, "failed to init com\n");
434 test_domdoc();
435 test_domnode();
437 CoUninitialize();