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
29 #include "wine/test.h"
31 /* Deprecated Error Code */
32 #define XML_E_INVALIDATROOTLEVEL 0xc00ce556
34 static void create_xml_file(LPCSTR filename)
36 DWORD dwNumberOfBytesWritten;
37 HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
38 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
40 static const char data[] =
41 "<?xml version=\"1.0\" ?>\n"
42 "<!DOCTYPE BankAccount>\n"
44 " <Number>1234</Number>\n"
45 " <Name>Captain Ahab</Name>\n"
48 WriteFile(hf, data, sizeof(data) - 1, &dwNumberOfBytesWritten, NULL);
52 static void test_xmldoc(void)
55 IXMLDocument *doc = NULL;
56 IXMLElement *element = NULL, *child = NULL, *value = NULL;
57 IXMLElementCollection *collection = NULL, *inner = NULL;
58 IPersistStreamInit *psi = NULL;
59 IStream *stream = NULL;
63 DWORD file_size, read;
66 VARIANT vIndex, vName;
69 static const WCHAR szBankAccount[] = {'B','A','N','K','A','C','C','O','U','N','T',0};
70 static const WCHAR szNumber[] = {'N','U','M','B','E','R',0};
71 static const WCHAR szNumVal[] = {'1','2','3','4',0};
72 static const WCHAR szName[] = {'N','A','M','E',0};
73 static const WCHAR szNameVal[] = {'C','a','p','t','a','i','n',' ','A','h','a','b',0};
74 static const WCHAR szVersion[] = {'1','.','0',0};
76 hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
77 &IID_IXMLDocument, (LPVOID*)&doc);
80 skip("Failed to create XMLDocument instance\n");
84 create_xml_file("bank.xml");
85 GetFullPathNameA("bank.xml", MAX_PATH, path, NULL);
87 hfile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL,
88 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
89 ok(hfile != INVALID_HANDLE_VALUE, "Expected a valid file handle\n");
90 file_size = GetFileSize(hfile, NULL);
92 hglobal = GlobalAlloc(GHND, file_size);
93 ptr = GlobalLock(hglobal);
95 ReadFile(hfile, ptr, file_size, &read, NULL);
96 ok(file_size == read, "Expected to read the whole file, read %d\n", read);
98 hr = CreateStreamOnHGlobal(hglobal, TRUE, &stream);
99 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
100 ok(stream != NULL, "Expected non-NULL stream\n");
103 GlobalUnlock(hglobal);
105 hr = IXMLDocument_QueryInterface(doc, &IID_IPersistStreamInit, (LPVOID *)&psi);
106 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
107 ok(psi != NULL, "Expected non-NULL psi\n");
109 hr = IXMLDocument_get_root(doc, &element);
110 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
111 ok(element == NULL, "Expected NULL element\n");
113 hr = IPersistStreamInit_Load(psi, stream);
114 ok(hr == S_OK || hr == XML_E_INVALIDATROOTLEVEL, "Expected S_OK, got %08x\n", hr);
115 if(hr == XML_E_INVALIDATROOTLEVEL)
118 ok(stream != NULL, "Expected non-NULL stream\n");
121 hr = IXMLDocument_get_version(doc, NULL);
122 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
124 hr = IXMLDocument_get_version(doc, &name);
125 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
126 ok(!lstrcmpW(name, szVersion), "Expected 1.0, got %s\n", wine_dbgstr_w(name));
130 hr = IXMLDocument_get_doctype(doc, NULL);
131 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
133 hr = IXMLDocument_get_doctype(doc, &name);
134 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
135 ok(!lstrcmpW(name, szBankAccount), "Expected BANKACCOUNT, got %s\n", wine_dbgstr_w(name));
138 hr = IXMLDocument_get_root(doc, &element);
139 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
140 ok(element != NULL, "Expected non-NULL element\n");
142 hr = IXMLElement_get_type(element, &type);
143 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
144 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
146 hr = IXMLElement_get_tagName(element, &name);
147 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
148 ok(!lstrcmpW(name, szBankAccount), "Expected BANKACCOUNT\n");
151 hr = IXMLElement_get_children(element, &collection);
152 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
153 ok(collection != NULL, "Expected non-NULL collection\n");
155 hr = IXMLElementCollection_get_length(collection, &num_child);
156 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
157 ok(num_child == 2, "Expected 2, got %d\n", num_child);
159 V_VT(&vIndex) = VT_I4;
161 V_VT(&vName) = VT_ERROR;
162 V_ERROR(&vName) = DISP_E_PARAMNOTFOUND;
163 hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
164 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
165 ok(child != NULL, "Expected non-NULL child\n");
167 hr = IXMLElement_get_type(child, &type);
168 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
169 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
171 hr = IXMLElement_get_tagName(child, &name);
172 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
173 ok(!lstrcmpW(name, szNumber), "Expected NUMBER\n");
176 hr = IXMLElement_get_children(child, &inner);
177 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
178 ok(inner != NULL, "Expected non-NULL inner\n");
180 hr = IXMLElementCollection_get_length(inner, &num_child);
181 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
182 ok(num_child == 1, "Expected 1, got %d\n", num_child);
184 hr = IXMLElementCollection_item(inner, vIndex, vName, (IDispatch **)&value);
185 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
186 ok(value != NULL, "Expected non-NULL value\n");
188 hr = IXMLElement_get_type(value, &type);
189 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
190 ok(type == XMLELEMTYPE_TEXT, "Expected XMLELEMTYPE_TEXT, got %d\n", type);
192 hr = IXMLElement_get_text(value, &name);
193 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
194 ok(!lstrcmpW(name, szNumVal), "Expected '1234'\n");
197 IXMLElementCollection_Release(inner);
199 inner = (IXMLElementCollection *)0xdeadbeef;
200 hr = IXMLElement_get_children(value, &inner);
201 ok(hr == 1, "Expected 1, got %08x\n", hr);
202 ok(inner == NULL, "Expected NULL inner, got %p\n", inner);
204 IXMLElement_Release(value);
205 IXMLElement_Release(child);
210 hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
211 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
212 ok(child != NULL, "Expected non-NULL child\n");
214 hr = IXMLElement_get_type(child, &type);
215 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
216 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
218 hr = IXMLElement_get_tagName(child, &name);
219 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
220 ok(!lstrcmpW(name, szName), "Expected NAME\n");
223 hr = IXMLElement_get_children(child, &inner);
224 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
225 ok(inner != NULL, "Expected non-NULL inner\n");
227 hr = IXMLElementCollection_get_length(inner, &num_child);
228 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
229 ok(num_child == 1, "Expected 1, got %d\n", num_child);
232 hr = IXMLElementCollection_item(inner, vIndex, vName, (IDispatch **)&value);
233 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
234 ok(value != NULL, "Expected non-NULL value\n");
236 hr = IXMLElement_get_type(value, &type);
237 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
238 ok(type == XMLELEMTYPE_TEXT, "Expected XMLELEMTYPE_TEXT, got %d\n", type);
240 hr = IXMLElement_get_text(value, &name);
241 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
242 ok(!lstrcmpW(name, szNameVal), "Expected 'Captain Ahab'\n");
245 IXMLElementCollection_Release(inner);
247 inner = (IXMLElementCollection *)0xdeadbeef;
248 hr = IXMLElement_get_children(value, &inner);
249 ok(hr == 1, "Expected 1, got %08x\n", hr);
250 ok(inner == NULL, "Expected NULL inner, got %p\n", inner);
252 IXMLElement_Release(value);
253 IXMLElement_Release(child);
254 IXMLElementCollection_Release(collection);
255 IXMLElement_Release(element);
257 IStream_Release(stream);
258 IPersistStreamInit_Release(psi);
259 IXMLDocument_Release(doc);
261 DeleteFileA("bank.xml");
264 static void test_createElement(void)
267 IXMLDocument *doc = NULL;
268 IXMLElement *element = NULL, *root = NULL;
269 VARIANT vType, vName;
272 hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
273 &IID_IXMLDocument, (LPVOID*)&doc);
276 skip("Failed to create XMLDocument instance\n");
280 /* invalid vType type */
281 V_VT(&vType) = VT_NULL;
282 V_VT(&vName) = VT_NULL;
283 element = (IXMLElement *)0xdeadbeef;
284 hr = IXMLDocument_createElement(doc, vType, vName, &element);
285 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
286 ok(element == NULL, "Expected NULL element\n");
288 /* invalid vType value */
289 V_VT(&vType) = VT_I4;
291 V_VT(&vName) = VT_NULL;
292 hr = IXMLDocument_createElement(doc, vType, vName, &element);
293 /* Up to and including SP7, createElement returns an element. */
296 ok(element != NULL, "Expected element\n");
299 hr = IXMLElement_get_type(element, &type);
300 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
301 /* SP7 returns an XMLELEMTYPE_ELEMENT */
302 ok(type == XMLELEMTYPE_OTHER || type == XMLELEMTYPE_ELEMENT,
303 "Expected XMLELEMTYPE_OTHER || XMLELEMTYPE_ELEMENT, got %d\n", type);
305 IXMLElement_Release(element);
310 ok(hr == E_NOTIMPL, "Expected E_NOTIMPL, got %08x\n", hr);
311 ok(element == NULL, "Expected NULL element\n");
314 /* invalid vName type */
315 V_VT(&vType) = VT_I4;
316 V_I4(&vType) = XMLELEMTYPE_ELEMENT;
317 V_VT(&vName) = VT_I4;
318 hr = IXMLDocument_createElement(doc, vType, vName, &element);
319 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
320 ok(element != NULL, "Expected non-NULL element\n");
322 hr = IXMLElement_get_type(element, &type);
323 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
324 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
326 IXMLElement_Release(element);
329 V_VT(&vType) = VT_I4;
330 V_I4(&vType) = XMLELEMTYPE_ELEMENT;
331 V_VT(&vName) = VT_I4;
332 hr = IXMLDocument_createElement(doc, vType, vName, NULL);
333 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
335 root = (IXMLElement *)0xdeadbeef;
336 hr = IXMLDocument_get_root(doc, &root);
337 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
338 ok(root == NULL, "Expected NULL root\n");
340 V_VT(&vType) = VT_I4;
341 V_I4(&vType) = XMLELEMTYPE_ELEMENT;
342 V_VT(&vName) = VT_NULL;
343 hr = IXMLDocument_createElement(doc, vType, vName, &element);
344 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
345 ok(element != NULL, "Expected non-NULL element\n");
347 /* createElement does not set the new element as root */
348 root = (IXMLElement *)0xdeadbeef;
349 hr = IXMLDocument_get_root(doc, &root);
350 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
351 ok(root == NULL, "Expected NULL root\n");
353 IXMLElement_Release(element);
354 IXMLDocument_Release(doc);
361 hr = CoInitialize(NULL);
362 ok(hr == S_OK, "failed to init com\n");
365 test_createElement();