1738b2ce50ba60342de78be37a64376b7cc6ca08
[reactos.git] / rostests / winetests / msxml3 / xmlelem.c
1 /*
2 * IXMLElement tests
3 *
4 * Copyright 2007 James Hawkins
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #define COBJMACROS
22
23 #include <stdio.h>
24
25 #include "windows.h"
26 #include "ole2.h"
27 #include "xmldom.h"
28 #include "msxml2.h"
29 #include "ocidl.h"
30
31 #include "wine/test.h"
32
33 #define ERROR_URL_NOT_FOUND 0x800c0006
34
35 static void test_xmlelem(void)
36 {
37 HRESULT hr;
38 IXMLDocument *doc = NULL;
39 IXMLElement *element = NULL, *parent;
40 IXMLElement *child, *child2;
41 IXMLElementCollection *children;
42 VARIANT vType, vName;
43 VARIANT vIndex, vValue;
44 BSTR str, val;
45 LONG type, num_child;
46
47 static const WCHAR propName[] = {'p','r','o','p',0};
48 static const WCHAR propVal[] = {'v','a','l',0};
49 static const WCHAR nextVal[] = {'n','e','x','t',0};
50 static const WCHAR noexist[] = {'n','o','e','x','i','s','t',0};
51 static const WCHAR crazyCase1[] = {'C','R','a','z','Y','c','A','S','E',0};
52 static const WCHAR crazyCase2[] = {'C','R','A','Z','Y','C','A','S','E',0};
53
54 hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
55 &IID_IXMLDocument, (LPVOID*)&doc);
56 if (FAILED(hr))
57 {
58 skip("Failed to create XMLDocument instance\n");
59 return;
60 }
61
62 V_VT(&vType) = VT_I4;
63 V_I4(&vType) = XMLELEMTYPE_ELEMENT;
64 V_VT(&vName) = VT_NULL;
65 hr = IXMLDocument_createElement(doc, vType, vName, &element);
66 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
67 ok(element != NULL, "Expected non-NULL element\n");
68
69 hr = IXMLElement_get_tagName(element, &str);
70 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
71 ok(lstrlenW(str) == 0, "Expected empty tag name\n");
72 SysFreeString(str);
73
74 parent = (IXMLElement *)0xdeadbeef;
75 hr = IXMLElement_get_parent(element, &parent);
76 ok(hr == 1, "Expected 1, got %08x\n", hr);
77 ok(parent == NULL, "Expected NULL parent\n");
78
79 str = SysAllocString(noexist);
80 hr = IXMLElement_getAttribute(element, str, &vValue);
81 ok(hr == S_FALSE, "Expected S_FALSE, got %08x\n", hr);
82 ok(V_VT(&vValue) == VT_EMPTY, "Expected VT_EMPTY, got %d\n", V_VT(&vValue));
83 ok(V_BSTR(&vValue) == NULL, "Expected null value\n");
84 VariantClear(&vValue);
85 SysFreeString(str);
86
87 str = SysAllocString(crazyCase1);
88 val = SysAllocString(propVal);
89 V_VT(&vValue) = VT_BSTR;
90 V_BSTR(&vValue) = val;
91 hr = IXMLElement_setAttribute(element, str, vValue);
92 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
93 SysFreeString(str);
94 SysFreeString(val);
95
96 str = SysAllocString(crazyCase2);
97 hr = IXMLElement_getAttribute(element, str, &vValue);
98 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
99 ok(V_VT(&vValue) == VT_BSTR, "Expected VT_BSTR, got %d\n", V_VT(&vValue));
100 ok(!lstrcmpW(V_BSTR(&vValue), propVal), "Expected 'val'\n");
101 VariantClear(&vValue);
102 SysFreeString(str);
103
104 str = SysAllocString(propName);
105 val = SysAllocString(propVal);
106 V_VT(&vValue) = VT_BSTR;
107 V_BSTR(&vValue) = val;
108 hr = IXMLElement_setAttribute(element, str, vValue);
109 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
110 SysFreeString(val);
111
112 hr = IXMLElement_getAttribute(element, str, &vValue);
113 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
114 ok(V_VT(&vValue) == VT_BSTR, "Expected VT_BSTR, got %d\n", V_VT(&vValue));
115 ok(!lstrcmpW(V_BSTR(&vValue), propVal), "Expected 'val'\n");
116 VariantClear(&vValue);
117
118 hr = IXMLElement_removeAttribute(element, str);
119 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
120
121 /* remove now nonexistent attribute */
122 hr = IXMLElement_removeAttribute(element, str);
123 ok(hr == S_FALSE, "Expected S_FALSE, got %08x\n", hr);
124
125 hr = IXMLElement_getAttribute(element, str, &vValue);
126 ok(hr == 1, "Expected 1, got %08x\n", hr);
127 ok(V_VT(&vValue) == VT_EMPTY, "Expected VT_EMPTY, got %d\n", V_VT(&vValue));
128 ok(V_BSTR(&vValue) == NULL, "Expected null value\n");
129 SysFreeString(str);
130 VariantClear(&vValue);
131
132 children = (IXMLElementCollection *)0xdeadbeef;
133 hr = IXMLElement_get_children(element, &children);
134 ok(hr == 1, "Expected 1, got %08x\n", hr);
135 ok(children == NULL, "Expected NULL collection\n");
136
137 hr = IXMLElement_get_type(element, &type);
138 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
139 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
140
141 hr = IXMLElement_get_text(element, &str);
142 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
143 ok(lstrlenW(str) == 0, "Expected empty text\n");
144 SysFreeString(str);
145
146 /* put_text with an ELEMENT */
147 str = SysAllocString(propVal);
148 hr = IXMLElement_put_text(element, str);
149 ok(hr == E_NOTIMPL, "Expected E_NOTIMPL, got %08x\n", hr);
150 SysFreeString(str);
151
152 V_VT(&vType) = VT_I4;
153 V_I4(&vType) = XMLELEMTYPE_TEXT;
154 V_VT(&vName) = VT_NULL;
155 hr = IXMLDocument_createElement(doc, vType, vName, &child);
156 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
157 ok(child != NULL, "Expected non-NULL child\n");
158
159 hr = IXMLElement_addChild(element, child, 0, -1);
160 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
161
162 str = SysAllocString(propVal);
163 hr = IXMLElement_put_text(child, str);
164 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
165 SysFreeString(str);
166
167 parent = (IXMLElement *)0xdeadbeef;
168 hr = IXMLElement_get_parent(child, &parent);
169 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
170 ok(parent != element, "Expected parent != element\n");
171
172 hr = IXMLElement_get_type(parent, &type);
173 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
174 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
175
176 children = (IXMLElementCollection *)0xdeadbeef;
177 hr = IXMLElement_get_children(element, &children);
178 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
179 ok(children != NULL, "Expected non-NULL collection\n");
180
181 hr = IXMLElementCollection_get_length(children, &num_child);
182 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
183 ok(num_child == 1, "Expected 1, got %d\n", num_child);
184
185 V_VT(&vIndex) = VT_I4;
186 V_I4(&vIndex) = 0;
187 V_VT(&vName) = VT_ERROR;
188 V_ERROR(&vName) = DISP_E_PARAMNOTFOUND;
189 hr = IXMLElementCollection_item(children, vIndex, vName, (IDispatch **)&child2);
190 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
191 ok(child2 != NULL, "Expected non-NULL child\n");
192
193 hr = IXMLElement_get_type(child2, &type);
194 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
195 ok(type == XMLELEMTYPE_TEXT, "Expected XMLELEMTYPE_TEXT, got %d\n", type);
196
197 hr = IXMLElement_get_text(element, &str);
198 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
199 ok(!lstrcmpW(str, propVal), "Expected 'val'\n");
200 SysFreeString(str);
201
202 hr = IXMLElement_get_text(child2, &str);
203 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
204 ok(!lstrcmpW(str, propVal), "Expected 'val'\n");
205 SysFreeString(str);
206
207 /* try put_text on ELEMENT again, now that it has a text child */
208 str = SysAllocString(nextVal);
209 hr = IXMLElement_put_text(element, str);
210 ok(hr == E_NOTIMPL, "Expected E_NOTIMPL, got %08x\n", hr);
211 SysFreeString(str);
212
213 str = SysAllocString(nextVal);
214 hr = IXMLElement_put_text(child2, str);
215 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
216 SysFreeString(str);
217
218 hr = IXMLElement_get_text(element, &str);
219 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
220 ok(!lstrcmpW(str, nextVal), "Expected 'val'\n");
221 SysFreeString(str);
222
223 IXMLElement_Release(child2);
224 IXMLElementCollection_Release(children);
225 IXMLElement_Release(parent);
226 IXMLElement_Release(child);
227 IXMLElement_Release(element);
228 IXMLDocument_Release(doc);
229 }
230
231 static void create_xml_file(LPCSTR filename)
232 {
233 DWORD dwNumberOfBytesWritten;
234 HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
235 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
236
237 static const char data[] =
238 "<?xml version=\"1.0\" ?>\n"
239 "<BankAccount>\n"
240 " <Number>1234</Number>\n"
241 " <Name>Captain Ahab</Name>\n"
242 "</BankAccount>\n";
243
244 WriteFile(hf, data, sizeof(data) - 1, &dwNumberOfBytesWritten, NULL);
245 CloseHandle(hf);
246 }
247
248 static void test_xmlelem_collection(void)
249 {
250 HRESULT hr;
251 IUnknown *unk = NULL;
252 IXMLDocument *doc = NULL;
253 IXMLElement *element = NULL, *child;
254 IXMLElementCollection *collection = NULL;
255 IEnumVARIANT *enumVar = NULL;
256 CHAR pathA[MAX_PATH];
257 WCHAR path[MAX_PATH];
258 LONG length, type;
259 ULONG num_vars;
260 VARIANT var, vIndex, vName;
261 BSTR url, str;
262 static const CHAR szBankXML[] = "bank.xml";
263 static const WCHAR szNumber[] = {'N','U','M','B','E','R',0};
264 static const WCHAR szName[] = {'N','A','M','E',0};
265
266 hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
267 &IID_IXMLDocument, (LPVOID*)&doc);
268 if (FAILED(hr))
269 {
270 skip("Failed to create XMLDocument instance\n");
271 return;
272 }
273
274 create_xml_file(szBankXML);
275 GetFullPathNameA(szBankXML, MAX_PATH, pathA, NULL);
276 MultiByteToWideChar(CP_ACP, 0, pathA, -1, path, MAX_PATH);
277
278 url = SysAllocString(path);
279 hr = IXMLDocument_put_URL(doc, url);
280 ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
281 SysFreeString(url);
282
283 if(hr != S_OK)
284 goto cleanup;
285
286 hr = IXMLDocument_get_root(doc, &element);
287 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
288 ok(element != NULL, "Expected non-NULL element\n");
289
290 hr = IXMLElement_get_children(element, &collection);
291 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
292 ok(collection != NULL, "Expected non-NULL collection\n");
293
294 hr = IXMLElementCollection_get_length(collection, NULL);
295 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
296
297 hr = IXMLElementCollection_get_length(collection, &length);
298 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
299 ok(length == 2, "Expected 2, got %d\n", length);
300
301 /* IXMLElementCollection:put_length does nothing */
302 hr = IXMLElementCollection_put_length(collection, -1);
303 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
304
305 hr = IXMLElementCollection_put_length(collection, 0);
306 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
307
308 hr = IXMLElementCollection_put_length(collection, 1);
309 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
310
311 hr = IXMLElementCollection_put_length(collection, 2);
312 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
313
314 hr = IXMLElementCollection_put_length(collection, 3);
315 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
316
317 hr = IXMLElementCollection_put_length(collection, 50);
318 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
319
320 /* make sure the length hasn't changed */
321 hr = IXMLElementCollection_get_length(collection, &length);
322 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
323 ok(length == 2, "Expected 2, got %d\n", length);
324
325 /* IXMLElementCollection implements IEnumVARIANT */
326 hr = IXMLElementCollection_QueryInterface(collection, &IID_IEnumVARIANT, (LPVOID *)&enumVar);
327 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
328 ok(enumVar != NULL, "Expected non-NULL enumVar\n");
329 IEnumVARIANT_Release(enumVar);
330
331 hr = IXMLElementCollection_get__newEnum(collection, &unk);
332 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
333 ok(unk != NULL, "Expected non-NULL unk\n");
334
335 hr = IUnknown_QueryInterface(unk, &IID_IEnumVARIANT, (LPVOID *)&enumVar);
336 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
337 ok(enumVar != NULL, "Expected non-NULL enumVar\n");
338 IUnknown_Release(unk);
339
340 /* <Number>1234</Number> */
341 hr = IEnumVARIANT_Next(enumVar, 1, &var, &num_vars);
342 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
343 ok(V_VT(&var) == VT_DISPATCH, "Expected VT_DISPATCH, got %d\n", V_VT(&var));
344 ok(num_vars == 1, "Expected 1, got %d\n", num_vars);
345
346 hr = IUnknown_QueryInterface(V_DISPATCH(&var), &IID_IXMLElement, (LPVOID *)&child);
347 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
348 ok(child != NULL, "Expected non-NULL child\n");
349
350 VariantClear(&var);
351
352 hr = IXMLElement_get_type(child, &type);
353 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
354 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
355
356 hr = IXMLElement_get_tagName(child, &str);
357 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
358 ok(!lstrcmpW(str, szNumber), "Expected NUMBER\n");
359 SysFreeString(str);
360 IXMLElement_Release(child);
361
362 /* <Name>Captain Ahab</Name> */
363 hr = IEnumVARIANT_Next(enumVar, 1, &var, &num_vars);
364 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
365 ok(V_VT(&var) == VT_DISPATCH, "Expected VT_DISPATCH, got %d\n", V_VT(&var));
366 ok(num_vars == 1, "Expected 1, got %d\n", num_vars);
367
368 hr = IUnknown_QueryInterface(V_DISPATCH(&var), &IID_IXMLElement, (LPVOID *)&child);
369 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
370 ok(child != NULL, "Expected non-NULL child\n");
371
372 VariantClear(&var);
373
374 hr = IXMLElement_get_type(child, &type);
375 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
376 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
377
378 hr = IXMLElement_get_tagName(child, &str);
379 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
380 ok(!lstrcmpW(str, szName), "Expected NAME\n");
381 SysFreeString(str);
382 IXMLElement_Release(child);
383
384 /* <Number>1234</Number> */
385 V_VT(&vIndex) = VT_I4;
386 V_I4(&vIndex) = 0;
387 V_VT(&vName) = VT_ERROR;
388 V_ERROR(&vName) = DISP_E_PARAMNOTFOUND;
389 hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
390 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
391 ok(child != NULL, "Expected non-NULL child\n");
392
393 hr = IXMLElement_get_type(child, &type);
394 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
395 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
396
397 hr = IXMLElement_get_tagName(child, &str);
398 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
399 ok(!lstrcmpW(str, szNumber), "Expected NUMBER\n");
400 SysFreeString(str);
401 IXMLElement_Release(child);
402
403 /* <Name>Captain Ahab</Name> */
404 V_VT(&vIndex) = VT_I4;
405 V_I4(&vIndex) = 1;
406 V_VT(&vName) = VT_ERROR;
407 V_ERROR(&vName) = DISP_E_PARAMNOTFOUND;
408 hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
409 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
410 ok(child != NULL, "Expected non-NULL child\n");
411
412 hr = IXMLElement_get_type(child, &type);
413 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
414 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
415
416 hr = IXMLElement_get_tagName(child, &str);
417 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
418 ok(!lstrcmpW(str, szName), "Expected NAME\n");
419 SysFreeString(str);
420 IXMLElement_Release(child);
421
422 V_I4(&vIndex) = 100;
423 hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
424 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
425 ok(child == NULL, "Expected NULL child\n");
426
427 V_I4(&vIndex) = -1;
428 child = (IXMLElement *)0xdeadbeef;
429 hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
430 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
431 ok(child == NULL, "Expected NULL child\n");
432
433 IEnumVARIANT_Release(enumVar);
434 IXMLElement_Release(element);
435 IXMLElementCollection_Release(collection);
436 cleanup:
437 IXMLDocument_Release(doc);
438 DeleteFileA("bank.xml");
439 }
440
441 START_TEST(xmlelem)
442 {
443 HRESULT hr;
444
445 hr = CoInitialize(NULL);
446 ok(hr == S_OK, "failed to init com\n");
447
448 test_xmlelem();
449 test_xmlelem_collection();
450
451 CoUninitialize();
452 }