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 static void append_str(char **str, const char *data)
37 static void create_xml_file(LPCSTR filename)
41 DWORD dwNumberOfBytesWritten;
42 HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
43 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
45 append_str(&ptr, "<?xml version=\"1.0\" ?>\n");
46 append_str(&ptr, "<BankAccount>\n");
47 append_str(&ptr, " <Number>1234</Number>\n");
48 append_str(&ptr, " <Name>Captain Ahab</Name>\n");
49 append_str(&ptr, "</BankAccount>");
51 WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
55 static void test_xmldoc(void)
58 IXMLDocument *doc = NULL;
59 IXMLElement *element = NULL, *child = NULL, *value = NULL;
60 IXMLElementCollection *collection = NULL, *inner = NULL;
61 IPersistStreamInit *psi = NULL;
62 IStream *stream = NULL;
66 DWORD file_size, read;
69 VARIANT vIndex, vName;
72 static const WCHAR szBankAccount[] = {'B','A','N','K','A','C','C','O','U','N','T',0};
73 static const WCHAR szNumber[] = {'N','U','M','B','E','R',0};
74 static const WCHAR szNumVal[] = {'1','2','3','4',0};
75 static const WCHAR szName[] = {'N','A','M','E',0};
76 static const WCHAR szNameVal[] = {'C','a','p','t','a','i','n',' ','A','h','a','b',0};
78 hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
79 &IID_IXMLDocument, (LPVOID*)&doc);
82 skip("Failed to create XMLDocument instance\n");
86 create_xml_file("bank.xml");
87 GetFullPathNameA("bank.xml", MAX_PATH, path, NULL);
89 hfile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL,
90 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
91 ok(hfile != INVALID_HANDLE_VALUE, "Expected a valid file handle\n");
92 file_size = GetFileSize(hfile, NULL);
94 hglobal = GlobalAlloc(GHND, file_size);
95 ptr = GlobalLock(hglobal);
97 ReadFile(hfile, ptr, file_size, &read, NULL);
98 ok(file_size == read, "Expected to read the whole file, read %d\n", read);
100 hr = CreateStreamOnHGlobal(hglobal, TRUE, &stream);
101 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
102 ok(stream != NULL, "Expected non-NULL stream\n");
105 GlobalUnlock(hglobal);
107 hr = IXMLDocument_QueryInterface(doc, &IID_IPersistStreamInit, (LPVOID *)&psi);
108 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
109 ok(psi != NULL, "Expected non-NULL psi\n");
111 hr = IXMLDocument_get_root(doc, &element);
112 ok(hr == E_FAIL, "Expected E_FAIL, got %d\n", hr);
113 ok(element == NULL, "Expected NULL element\n");
115 hr = IPersistStreamInit_Load(psi, stream);
116 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
117 ok(stream != NULL, "Expected non-NULL stream\n");
119 hr = IXMLDocument_get_root(doc, &element);
120 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
121 ok(element != NULL, "Expected non-NULL element\n");
123 hr = IXMLElement_get_type(element, &type);
124 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
125 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type);
127 hr = IXMLElement_get_tagName(element, &name);
128 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
129 ok(!lstrcmpW(name, szBankAccount), "Expected BANKACCOUNT\n");
132 hr = IXMLElement_get_children(element, &collection);
133 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
134 ok(collection != NULL, "Expected non-NULL collection\n");
136 hr = IXMLElementCollection_get_length(collection, &num_child);
137 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
138 ok(num_child == 2, "Expected 2, got %ld\n", num_child);
140 V_VT(&vIndex) = VT_I4;
142 V_VT(&vName) = VT_ERROR;
143 V_ERROR(&vName) = DISP_E_PARAMNOTFOUND;
144 hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
145 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
146 ok(child != NULL, "Expected non-NULL child\n");
148 hr = IXMLElement_get_type(child, &type);
149 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
150 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type);
152 hr = IXMLElement_get_tagName(child, &name);
153 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
154 ok(!lstrcmpW(name, szNumber), "Expected NUMBER\n");
157 hr = IXMLElement_get_children(child, &inner);
158 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
159 ok(inner != NULL, "Expected non-NULL inner\n");
161 hr = IXMLElementCollection_get_length(inner, &num_child);
162 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
163 ok(num_child == 1, "Expected 1, got %ld\n", num_child);
165 hr = IXMLElementCollection_item(inner, vIndex, vName, (IDispatch **)&value);
166 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
167 ok(value != NULL, "Expected non-NULL value\n");
169 hr = IXMLElement_get_type(value, &type);
170 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
171 ok(type == XMLELEMTYPE_TEXT, "Expected XMLELEMTYPE_TEXT, got %ld\n", type);
173 hr = IXMLElement_get_text(value, &name);
174 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
175 ok(!lstrcmpW(name, szNumVal), "Expected '1234'\n");
178 IXMLElementCollection_Release(inner);
180 inner = (IXMLElementCollection *)0xdeadbeef;
181 hr = IXMLElement_get_children(value, &inner);
182 ok(hr == 1, "Expected 1, got %d\n", hr);
183 ok(inner == NULL, "Expected NULL inner, got %p\n", inner);
185 IXMLElement_Release(value);
186 IXMLElement_Release(child);
191 hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
192 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
193 ok(child != NULL, "Expected non-NULL child\n");
195 hr = IXMLElement_get_type(child, &type);
196 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
197 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type);
199 hr = IXMLElement_get_tagName(child, &name);
200 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
201 ok(!lstrcmpW(name, szName), "Expected NAME\n");
204 hr = IXMLElement_get_children(child, &inner);
205 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
206 ok(inner != NULL, "Expected non-NULL inner\n");
208 hr = IXMLElementCollection_get_length(inner, &num_child);
209 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
210 ok(num_child == 1, "Expected 1, got %ld\n", num_child);
213 hr = IXMLElementCollection_item(inner, vIndex, vName, (IDispatch **)&value);
214 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
215 ok(value != NULL, "Expected non-NULL value\n");
217 hr = IXMLElement_get_type(value, &type);
218 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
219 ok(type == XMLELEMTYPE_TEXT, "Expected XMLELEMTYPE_TEXT, got %ld\n", type);
221 hr = IXMLElement_get_text(value, &name);
222 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
223 ok(!lstrcmpW(name, szNameVal), "Expected 'Captain Ahab'\n");
226 IXMLElementCollection_Release(inner);
228 inner = (IXMLElementCollection *)0xdeadbeef;
229 hr = IXMLElement_get_children(value, &inner);
230 ok(hr == 1, "Expected 1, got %d\n", hr);
231 ok(inner == NULL, "Expected NULL inner, got %p\n", inner);
233 IXMLElement_Release(value);
234 IXMLElement_Release(child);
235 IXMLElementCollection_Release(collection);
236 IXMLElement_Release(element);
237 IStream_Release(stream);
238 IPersistStreamInit_Release(psi);
239 IXMLDocument_Release(doc);
241 DeleteFileA("bank.xml");
244 static void test_createElement(void)
247 IXMLDocument *doc = NULL;
248 IXMLElement *element = NULL, *root = NULL;
249 VARIANT vType, vName;
252 hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
253 &IID_IXMLDocument, (LPVOID*)&doc);
256 skip("Failed to create XMLDocument instance\n");
260 /* invalid vType type */
261 V_VT(&vType) = VT_NULL;
262 V_VT(&vName) = VT_NULL;
263 element = (IXMLElement *)0xdeadbeef;
264 hr = IXMLDocument_createElement(doc, vType, vName, &element);
265 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
266 ok(element == NULL, "Expected NULL element\n");
268 /* invalid vType value */
269 V_VT(&vType) = VT_I4;
271 V_VT(&vName) = VT_NULL;
272 hr = IXMLDocument_createElement(doc, vType, vName, &element);
273 /* Up to and including SP7, createElement returns an element. */
276 ok(element != NULL, "Expected element\n");
279 hr = IXMLElement_get_type(element, &type);
280 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
281 /* SP7 returns an XMLELEMTYPE_ELEMENT */
282 ok(type == XMLELEMTYPE_OTHER || type == XMLELEMTYPE_ELEMENT,
283 "Expected XMLELEMTYPE_OTHER || XMLELEMTYPE_ELEMENT, got %ld\n", type);
285 IXMLElement_Release(element);
290 ok(hr == E_NOTIMPL, "Expected E_NOTIMPL, got %d\n", hr);
291 ok(element == NULL, "Expected NULL element\n");
294 /* invalid vName type */
295 V_VT(&vType) = VT_I4;
296 V_I4(&vType) = XMLELEMTYPE_ELEMENT;
297 V_VT(&vName) = VT_I4;
298 hr = IXMLDocument_createElement(doc, vType, vName, &element);
299 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
300 ok(element != NULL, "Expected non-NULL element\n");
302 hr = IXMLElement_get_type(element, &type);
303 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
304 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type);
306 IXMLElement_Release(element);
309 V_VT(&vType) = VT_I4;
310 V_I4(&vType) = XMLELEMTYPE_ELEMENT;
311 V_VT(&vName) = VT_I4;
312 hr = IXMLDocument_createElement(doc, vType, vName, NULL);
313 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
315 root = (IXMLElement *)0xdeadbeef;
316 hr = IXMLDocument_get_root(doc, &root);
317 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
318 ok(root == NULL, "Expected NULL root\n");
320 V_VT(&vType) = VT_I4;
321 V_I4(&vType) = XMLELEMTYPE_ELEMENT;
322 V_VT(&vName) = VT_NULL;
323 hr = IXMLDocument_createElement(doc, vType, vName, &element);
324 ok(hr == S_OK, "Expected S_OK, got %d\n", hr);
325 ok(element != NULL, "Expected non-NULL element\n");
327 /* createElement does not set the new element as root */
328 root = (IXMLElement *)0xdeadbeef;
329 hr = IXMLDocument_get_root(doc, &root);
330 ok(hr == E_FAIL, "Expected E_FAIL, got %d\n", hr);
331 ok(root == NULL, "Expected NULL root\n");
333 IXMLElement_Release(element);
334 IXMLDocument_Release(doc);
341 hr = CoInitialize(NULL);
342 ok(hr == S_OK, "failed to init com\n");
345 test_createElement();