4 * Copyright 2007 James Hawkins
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wine/test.h"
33 static void test_xmlelem(void)
36 IXMLDocument
*doc
= NULL
;
37 IXMLElement
*element
= NULL
, *parent
;
38 IXMLElement
*child
, *child2
;
39 IXMLElementCollection
*children
;
41 VARIANT vIndex
, vValue
;
45 static const WCHAR propName
[] = {'p','r','o','p',0};
46 static const WCHAR propVal
[] = {'v','a','l',0};
47 static const WCHAR nextVal
[] = {'n','e','x','t',0};
48 static const WCHAR noexist
[] = {'n','o','e','x','i','s','t',0};
49 static const WCHAR crazyCase1
[] = {'C','R','a','z','Y','c','A','S','E',0};
50 static const WCHAR crazyCase2
[] = {'C','R','A','Z','Y','C','A','S','E',0};
52 hr
= CoCreateInstance(&CLSID_XMLDocument
, NULL
, CLSCTX_INPROC_SERVER
,
53 &IID_IXMLDocument
, (LPVOID
*)&doc
);
56 skip("Failed to create XMLDocument instance\n");
61 V_I4(&vType
) = XMLELEMTYPE_ELEMENT
;
62 V_VT(&vName
) = VT_NULL
;
63 hr
= IXMLDocument_createElement(doc
, vType
, vName
, &element
);
64 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
65 ok(element
!= NULL
, "Expected non-NULL element\n");
67 hr
= IXMLElement_get_tagName(element
, &str
);
68 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
69 ok(lstrlenW(str
) == 0, "Expected empty tag name\n");
72 parent
= (IXMLElement
*)0xdeadbeef;
73 hr
= IXMLElement_get_parent(element
, &parent
);
74 ok(hr
== 1, "Expected 1, got %d\n", hr
);
75 ok(parent
== NULL
, "Expected NULL parent\n");
77 str
= SysAllocString(noexist
);
78 hr
= IXMLElement_getAttribute(element
, str
, &vValue
);
79 ok(hr
== S_FALSE
, "Expected S_FALSE, got %d\n", hr
);
80 ok(V_VT(&vValue
) == VT_EMPTY
, "Expected VT_EMPTY, got %d\n", V_VT(&vValue
));
81 ok(lstrlenW(V_BSTR(&vValue
)) == 0, "Expected empty value\n");
82 VariantClear(&vValue
);
85 str
= SysAllocString(crazyCase1
);
86 val
= SysAllocString(propVal
);
87 V_VT(&vValue
) = VT_BSTR
;
88 V_BSTR(&vValue
) = val
;
89 hr
= IXMLElement_setAttribute(element
, str
, vValue
);
90 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
94 str
= SysAllocString(crazyCase2
);
95 hr
= IXMLElement_getAttribute(element
, str
, &vValue
);
96 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
97 ok(V_VT(&vValue
) == VT_BSTR
, "Expected VT_BSTR, got %d\n", V_VT(&vValue
));
98 ok(!lstrcmpW(V_BSTR(&vValue
), propVal
), "Expected 'val'\n");
99 VariantClear(&vValue
);
102 str
= SysAllocString(propName
);
103 val
= SysAllocString(propVal
);
104 V_VT(&vValue
) = VT_BSTR
;
105 V_BSTR(&vValue
) = val
;
106 hr
= IXMLElement_setAttribute(element
, str
, vValue
);
107 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
110 hr
= IXMLElement_getAttribute(element
, str
, &vValue
);
111 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
112 ok(V_VT(&vValue
) == VT_BSTR
, "Expected VT_BSTR, got %d\n", V_VT(&vValue
));
113 ok(!lstrcmpW(V_BSTR(&vValue
), propVal
), "Expected 'val'\n");
114 VariantClear(&vValue
);
116 hr
= IXMLElement_removeAttribute(element
, str
);
117 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
119 /* remove now nonexistent attribute */
120 hr
= IXMLElement_removeAttribute(element
, str
);
121 ok(hr
== S_FALSE
, "Expected S_FALSE, got %d\n", hr
);
123 hr
= IXMLElement_getAttribute(element
, str
, &vValue
);
124 ok(hr
== 1, "Expected 1, got %d\n", hr
);
125 ok(V_VT(&vValue
) == VT_EMPTY
, "Expected VT_EMPTY, got %d\n", V_VT(&vValue
));
126 ok(!lstrlenW(V_BSTR(&vValue
)), "Expected empty value\n");
128 VariantClear(&vValue
);
130 children
= (IXMLElementCollection
*)0xdeadbeef;
131 hr
= IXMLElement_get_children(element
, &children
);
132 ok(hr
== 1, "Expected 1, got %d\n", hr
);
133 ok(children
== NULL
, "Expected NULL collection\n");
135 hr
= IXMLElement_get_type(element
, &type
);
136 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
137 ok(type
== XMLELEMTYPE_ELEMENT
, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type
);
139 hr
= IXMLElement_get_text(element
, &str
);
140 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
141 ok(lstrlenW(str
) == 0, "Expected empty text\n");
144 /* put_text with an ELEMENT */
145 str
= SysAllocString(propVal
);
146 hr
= IXMLElement_put_text(element
, str
);
147 ok(hr
== E_NOTIMPL
, "Expected E_NOTIMPL, got %d\n", hr
);
150 V_VT(&vType
) = VT_I4
;
151 V_I4(&vType
) = XMLELEMTYPE_TEXT
;
152 V_VT(&vName
) = VT_NULL
;
153 hr
= IXMLDocument_createElement(doc
, vType
, vName
, &child
);
154 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
155 ok(child
!= NULL
, "Expected non-NULL child\n");
157 hr
= IXMLElement_addChild(element
, child
, 0, -1);
158 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
160 str
= SysAllocString(propVal
);
161 hr
= IXMLElement_put_text(child
, str
);
162 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
165 parent
= (IXMLElement
*)0xdeadbeef;
166 hr
= IXMLElement_get_parent(child
, &parent
);
167 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
168 ok(parent
!= element
, "Expected parent != element\n");
170 hr
= IXMLElement_get_type(parent
, &type
);
171 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
172 ok(type
== XMLELEMTYPE_ELEMENT
, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type
);
174 children
= (IXMLElementCollection
*)0xdeadbeef;
175 hr
= IXMLElement_get_children(element
, &children
);
176 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
177 ok(children
!= NULL
, "Expected non-NULL collection\n");
179 hr
= IXMLElementCollection_get_length(children
, &num_child
);
180 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
181 ok(num_child
== 1, "Expected 1, got %ld\n", num_child
);
183 V_VT(&vIndex
) = VT_I4
;
185 V_VT(&vName
) = VT_ERROR
;
186 V_ERROR(&vName
) = DISP_E_PARAMNOTFOUND
;
187 hr
= IXMLElementCollection_item(children
, vIndex
, vName
, (IDispatch
**)&child2
);
188 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
189 ok(child2
!= NULL
, "Expected non-NULL child\n");
191 hr
= IXMLElement_get_type(child2
, &type
);
192 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
193 ok(type
== XMLELEMTYPE_TEXT
, "Expected XMLELEMTYPE_TEXT, got %ld\n", type
);
195 hr
= IXMLElement_get_text(element
, &str
);
196 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
197 ok(!lstrcmpW(str
, propVal
), "Expected 'val'\n");
200 hr
= IXMLElement_get_text(child2
, &str
);
201 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
202 ok(!lstrcmpW(str
, propVal
), "Expected 'val'\n");
205 /* try put_text on ELEMENT again, now that it has a text child */
206 str
= SysAllocString(nextVal
);
207 hr
= IXMLElement_put_text(element
, str
);
208 ok(hr
== E_NOTIMPL
, "Expected E_NOTIMPL, got %d\n", hr
);
211 str
= SysAllocString(nextVal
);
212 hr
= IXMLElement_put_text(child2
, str
);
213 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
216 hr
= IXMLElement_get_text(element
, &str
);
217 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
218 ok(!lstrcmpW(str
, nextVal
), "Expected 'val'\n");
221 IXMLElement_Release(child2
);
222 IXMLElementCollection_Release(children
);
223 IXMLElement_Release(parent
);
224 IXMLElement_Release(child
);
225 IXMLElement_Release(element
);
226 IXMLDocument_Release(doc
);
229 static void append_str(char **str
, const char *data
)
232 *str
+= strlen(*str
);
235 static void create_xml_file(LPCSTR filename
)
239 DWORD dwNumberOfBytesWritten
;
240 HANDLE hf
= CreateFile(filename
, GENERIC_WRITE
, 0, NULL
,
241 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
243 append_str(&ptr
, "<?xml version=\"1.0\" ?>\n");
244 append_str(&ptr
, "<BankAccount>\n");
245 append_str(&ptr
, " <Number>1234</Number>\n");
246 append_str(&ptr
, " <Name>Captain Ahab</Name>\n");
247 append_str(&ptr
, "</BankAccount>\n");
249 WriteFile(hf
, data
, ptr
- data
, &dwNumberOfBytesWritten
, NULL
);
253 static void test_xmlelem_collection(void)
256 IUnknown
*unk
= NULL
;
257 IXMLDocument
*doc
= NULL
;
258 IXMLElement
*element
= NULL
, *child
;
259 IXMLElementCollection
*collection
= NULL
;
260 IEnumVARIANT
*enumVar
= NULL
;
261 WCHAR path
[MAX_PATH
];
264 VARIANT var
, vIndex
, vName
;
267 static const WCHAR szBankXML
[] = {'b','a','n','k','.','x','m','l',0};
268 static const WCHAR szNumber
[] = {'N','U','M','B','E','R',0};
269 static const WCHAR szName
[] = {'N','A','M','E',0};
271 hr
= CoCreateInstance(&CLSID_XMLDocument
, NULL
, CLSCTX_INPROC_SERVER
,
272 &IID_IXMLDocument
, (LPVOID
*)&doc
);
275 skip("Failed to create XMLDocument instance\n");
279 create_xml_file("bank.xml");
280 GetFullPathNameW(szBankXML
, MAX_PATH
, path
, NULL
);
282 url
= SysAllocString(path
);
283 hr
= IXMLDocument_put_URL(doc
, url
);
284 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
287 hr
= IXMLDocument_get_root(doc
, &element
);
288 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
289 ok(element
!= NULL
, "Expected non-NULL element\n");
291 hr
= IXMLElement_get_children(element
, &collection
);
292 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
293 ok(collection
!= NULL
, "Expected non-NULL collection\n");
295 hr
= IXMLElementCollection_get_length(collection
, NULL
);
296 ok(hr
== E_INVALIDARG
, "Expected E_INVALIDARG, got %d\n", hr
);
298 hr
= IXMLElementCollection_get_length(collection
, &length
);
299 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
300 ok(length
== 2, "Expected 2, got %ld\n", length
);
302 /* IXMLElementCollection:put_length does nothing */
303 hr
= IXMLElementCollection_put_length(collection
, -1);
304 ok(hr
== E_FAIL
, "Expected E_FAIL, got %08x\n", hr
);
306 hr
= IXMLElementCollection_put_length(collection
, 0);
307 ok(hr
== E_FAIL
, "Expected E_FAIL, got %08x\n", hr
);
309 hr
= IXMLElementCollection_put_length(collection
, 1);
310 ok(hr
== E_FAIL
, "Expected E_FAIL, got %08x\n", hr
);
312 hr
= IXMLElementCollection_put_length(collection
, 2);
313 ok(hr
== E_FAIL
, "Expected E_FAIL, got %08x\n", hr
);
315 hr
= IXMLElementCollection_put_length(collection
, 3);
316 ok(hr
== E_FAIL
, "Expected E_FAIL, got %08x\n", hr
);
318 hr
= IXMLElementCollection_put_length(collection
, 50);
319 ok(hr
== E_FAIL
, "Expected E_FAIL, got %08x\n", hr
);
321 /* make sure the length hasn't changed */
322 hr
= IXMLElementCollection_get_length(collection
, &length
);
323 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
324 ok(length
== 2, "Expected 2, got %ld\n", length
);
326 /* IXMLElementCollection implements IEnumVARIANT */
327 hr
= IXMLElementCollection_QueryInterface(collection
, &IID_IEnumVARIANT
, (LPVOID
*)&enumVar
);
328 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
329 ok(enumVar
!= NULL
, "Expected non-NULL enumVar\n");
330 IEnumVARIANT_Release(enumVar
);
332 hr
= IXMLElementCollection_get__newEnum(collection
, &unk
);
333 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
334 ok(unk
!= NULL
, "Expected non-NULL unk\n");
336 hr
= IUnknown_QueryInterface(unk
, &IID_IEnumVARIANT
, (LPVOID
*)&enumVar
);
337 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
338 ok(enumVar
!= NULL
, "Expected non-NULL enumVar\n");
339 IUnknown_Release(unk
);
341 /* <Number>1234</Number> */
342 hr
= IEnumVARIANT_Next(enumVar
, 1, &var
, &num_vars
);
343 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
344 ok(V_VT(&var
) == VT_DISPATCH
, "Expected VT_DISPATCH, got %d\n", V_VT(&var
));
345 ok(num_vars
== 1, "Expected 1, got %d\n", 1);
347 hr
= IUnknown_QueryInterface(V_DISPATCH(&var
), &IID_IXMLElement
, (LPVOID
*)&child
);
348 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
349 ok(child
!= NULL
, "Expected non-NULL child\n");
353 hr
= IXMLElement_get_type(child
, &type
);
354 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
355 ok(type
== XMLELEMTYPE_ELEMENT
, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type
);
357 hr
= IXMLElement_get_tagName(child
, &str
);
358 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
359 ok(!lstrcmpW(str
, szNumber
), "Expected NUMBER\n");
361 IXMLElement_Release(child
);
363 /* <Name>Captain Ahab</Name> */
364 hr
= IEnumVARIANT_Next(enumVar
, 1, &var
, &num_vars
);
365 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
366 ok(V_VT(&var
) == VT_DISPATCH
, "Expected VT_DISPATCH, got %d\n", V_VT(&var
));
367 ok(num_vars
== 1, "Expected 1, got %d\n", 1);
369 hr
= IUnknown_QueryInterface(V_DISPATCH(&var
), &IID_IXMLElement
, (LPVOID
*)&child
);
370 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
371 ok(child
!= NULL
, "Expected non-NULL child\n");
375 hr
= IXMLElement_get_type(child
, &type
);
376 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
377 ok(type
== XMLELEMTYPE_ELEMENT
, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type
);
379 hr
= IXMLElement_get_tagName(child
, &str
);
380 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
381 ok(!lstrcmpW(str
, szName
), "Expected NAME\n");
383 IXMLElement_Release(child
);
385 /* <Number>1234</Number> */
386 V_VT(&vIndex
) = VT_I4
;
388 V_VT(&vName
) = VT_ERROR
;
389 V_ERROR(&vName
) = DISP_E_PARAMNOTFOUND
;
390 hr
= IXMLElementCollection_item(collection
, vIndex
, vName
, (IDispatch
**)&child
);
391 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
392 ok(child
!= NULL
, "Expected non-NULL child\n");
394 hr
= IXMLElement_get_type(child
, &type
);
395 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
396 ok(type
== XMLELEMTYPE_ELEMENT
, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type
);
398 hr
= IXMLElement_get_tagName(child
, &str
);
399 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
400 ok(!lstrcmpW(str
, szNumber
), "Expected NUMBER\n");
402 IXMLElement_Release(child
);
404 /* <Name>Captain Ahab</Name> */
405 V_VT(&vIndex
) = VT_I4
;
407 V_VT(&vName
) = VT_ERROR
;
408 V_ERROR(&vName
) = DISP_E_PARAMNOTFOUND
;
409 hr
= IXMLElementCollection_item(collection
, vIndex
, vName
, (IDispatch
**)&child
);
410 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
411 ok(child
!= NULL
, "Expected non-NULL child\n");
413 hr
= IXMLElement_get_type(child
, &type
);
414 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
415 ok(type
== XMLELEMTYPE_ELEMENT
, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type
);
417 hr
= IXMLElement_get_tagName(child
, &str
);
418 ok(hr
== S_OK
, "Expected S_OK, got %d\n", hr
);
419 ok(!lstrcmpW(str
, szName
), "Expected NAME\n");
421 IXMLElement_Release(child
);
424 hr
= IXMLElementCollection_item(collection
, vIndex
, vName
, (IDispatch
**)&child
);
425 ok(hr
== E_FAIL
, "Expected E_FAIL, got %08x\n", hr
);
426 ok(child
== NULL
, "Expected NULL child\n");
429 child
= (IXMLElement
*)0xdeadbeef;
430 hr
= IXMLElementCollection_item(collection
, vIndex
, vName
, (IDispatch
**)&child
);
431 ok(hr
== E_INVALIDARG
, "Expected E_INVALIDARG, got %d\n", hr
);
432 ok(child
== NULL
, "Expected NULL child\n");
434 IEnumVARIANT_Release(enumVar
);
435 IXMLElement_Release(element
);
436 IXMLElementCollection_Release(collection
);
437 IXMLDocument_Release(doc
);
438 DeleteFileA("bank.xml");
445 hr
= CoInitialize(NULL
);
446 ok(hr
== S_OK
, "failed to init com\n");
449 test_xmlelem_collection();