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 append_str(char **str
, const char *data
)
40 static void create_xml_file(LPCSTR filename
)
44 DWORD dwNumberOfBytesWritten
;
45 HANDLE hf
= CreateFile(filename
, GENERIC_WRITE
, 0, NULL
,
46 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
48 append_str(&ptr
, "<?xml version=\"1.0\" ?>\n");
49 append_str(&ptr
, "<BankAccount>\n");
50 append_str(&ptr
, " <Number>1234</Number>\n");
51 append_str(&ptr
, " <Name>Captain Ahab</Name>\n");
52 append_str(&ptr
, "</BankAccount>");
54 WriteFile(hf
, data
, ptr
- data
, &dwNumberOfBytesWritten
, NULL
);
58 static void test_xmldoc(void)
61 IXMLDocument
*doc
= NULL
;
62 IXMLElement
*element
= NULL
, *child
= NULL
, *value
= NULL
;
63 IXMLElementCollection
*collection
= NULL
, *inner
= NULL
;
64 IPersistStreamInit
*psi
= NULL
;
65 IStream
*stream
= NULL
;
69 DWORD file_size
, read
;
72 VARIANT vIndex
, vName
;
75 static const WCHAR szBankAccount
[] = {'B','A','N','K','A','C','C','O','U','N','T',0};
76 static const WCHAR szNumber
[] = {'N','U','M','B','E','R',0};
77 static const WCHAR szNumVal
[] = {'1','2','3','4',0};
78 static const WCHAR szName
[] = {'N','A','M','E',0};
79 static const WCHAR szNameVal
[] = {'C','a','p','t','a','i','n',' ','A','h','a','b',0};
81 hr
= CoCreateInstance(&CLSID_XMLDocument
, NULL
, CLSCTX_INPROC_SERVER
,
82 &IID_IXMLDocument
, (LPVOID
*)&doc
);
85 skip("Failed to create XMLDocument instance\n");
89 create_xml_file("bank.xml");
90 GetFullPathNameA("bank.xml", MAX_PATH
, path
, NULL
);
92 hfile
= CreateFile(path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
93 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
94 ok(hfile
!= INVALID_HANDLE_VALUE
, "Expected a valid file handle\n");
95 file_size
= GetFileSize(hfile
, NULL
);
97 hglobal
= GlobalAlloc(GHND
, file_size
);
98 ptr
= GlobalLock(hglobal
);
100 ReadFile(hfile
, ptr
, file_size
, &read
, NULL
);
101 ok(file_size
== read
, "Expected to read the whole file, read %d\n", read
);
103 hr
= CreateStreamOnHGlobal(hglobal
, TRUE
, &stream
);
104 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
105 ok(stream
!= NULL
, "Expected non-NULL stream\n");
108 GlobalUnlock(hglobal
);
110 hr
= IXMLDocument_QueryInterface(doc
, &IID_IPersistStreamInit
, (LPVOID
*)&psi
);
111 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
112 ok(psi
!= NULL
, "Expected non-NULL psi\n");
114 hr
= IXMLDocument_get_root(doc
, &element
);
115 ok(hr
== E_FAIL
, "Expected E_FAIL, got %08x\n", hr
);
116 ok(element
== NULL
, "Expected NULL element\n");
118 hr
= IPersistStreamInit_Load(psi
, stream
);
119 ok(hr
== S_OK
|| hr
== XML_E_INVALIDATROOTLEVEL
, "Expected S_OK, got %08x\n", hr
);
120 if(hr
== XML_E_INVALIDATROOTLEVEL
)
123 ok(stream
!= NULL
, "Expected non-NULL stream\n");
125 hr
= IXMLDocument_get_root(doc
, &element
);
126 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
127 ok(element
!= NULL
, "Expected non-NULL element\n");
129 hr
= IXMLElement_get_type(element
, &type
);
130 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
131 ok(type
== XMLELEMTYPE_ELEMENT
, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type
);
133 hr
= IXMLElement_get_tagName(element
, &name
);
134 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
135 ok(!lstrcmpW(name
, szBankAccount
), "Expected BANKACCOUNT\n");
138 hr
= IXMLElement_get_children(element
, &collection
);
139 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
140 ok(collection
!= NULL
, "Expected non-NULL collection\n");
142 hr
= IXMLElementCollection_get_length(collection
, &num_child
);
143 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
144 ok(num_child
== 2, "Expected 2, got %ld\n", num_child
);
146 V_VT(&vIndex
) = VT_I4
;
148 V_VT(&vName
) = VT_ERROR
;
149 V_ERROR(&vName
) = DISP_E_PARAMNOTFOUND
;
150 hr
= IXMLElementCollection_item(collection
, vIndex
, vName
, (IDispatch
**)&child
);
151 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
152 ok(child
!= NULL
, "Expected non-NULL child\n");
154 hr
= IXMLElement_get_type(child
, &type
);
155 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
156 ok(type
== XMLELEMTYPE_ELEMENT
, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type
);
158 hr
= IXMLElement_get_tagName(child
, &name
);
159 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
160 ok(!lstrcmpW(name
, szNumber
), "Expected NUMBER\n");
163 hr
= IXMLElement_get_children(child
, &inner
);
164 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
165 ok(inner
!= NULL
, "Expected non-NULL inner\n");
167 hr
= IXMLElementCollection_get_length(inner
, &num_child
);
168 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
169 ok(num_child
== 1, "Expected 1, got %ld\n", num_child
);
171 hr
= IXMLElementCollection_item(inner
, vIndex
, vName
, (IDispatch
**)&value
);
172 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
173 ok(value
!= NULL
, "Expected non-NULL value\n");
175 hr
= IXMLElement_get_type(value
, &type
);
176 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
177 ok(type
== XMLELEMTYPE_TEXT
, "Expected XMLELEMTYPE_TEXT, got %ld\n", type
);
179 hr
= IXMLElement_get_text(value
, &name
);
180 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
181 ok(!lstrcmpW(name
, szNumVal
), "Expected '1234'\n");
184 IXMLElementCollection_Release(inner
);
186 inner
= (IXMLElementCollection
*)0xdeadbeef;
187 hr
= IXMLElement_get_children(value
, &inner
);
188 ok(hr
== 1, "Expected 1, got %08x\n", hr
);
189 ok(inner
== NULL
, "Expected NULL inner, got %p\n", inner
);
191 IXMLElement_Release(value
);
192 IXMLElement_Release(child
);
197 hr
= IXMLElementCollection_item(collection
, vIndex
, vName
, (IDispatch
**)&child
);
198 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
199 ok(child
!= NULL
, "Expected non-NULL child\n");
201 hr
= IXMLElement_get_type(child
, &type
);
202 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
203 ok(type
== XMLELEMTYPE_ELEMENT
, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type
);
205 hr
= IXMLElement_get_tagName(child
, &name
);
206 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
207 ok(!lstrcmpW(name
, szName
), "Expected NAME\n");
210 hr
= IXMLElement_get_children(child
, &inner
);
211 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
212 ok(inner
!= NULL
, "Expected non-NULL inner\n");
214 hr
= IXMLElementCollection_get_length(inner
, &num_child
);
215 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
216 ok(num_child
== 1, "Expected 1, got %ld\n", num_child
);
219 hr
= IXMLElementCollection_item(inner
, vIndex
, vName
, (IDispatch
**)&value
);
220 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
221 ok(value
!= NULL
, "Expected non-NULL value\n");
223 hr
= IXMLElement_get_type(value
, &type
);
224 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
225 ok(type
== XMLELEMTYPE_TEXT
, "Expected XMLELEMTYPE_TEXT, got %ld\n", type
);
227 hr
= IXMLElement_get_text(value
, &name
);
228 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
229 ok(!lstrcmpW(name
, szNameVal
), "Expected 'Captain Ahab'\n");
232 IXMLElementCollection_Release(inner
);
234 inner
= (IXMLElementCollection
*)0xdeadbeef;
235 hr
= IXMLElement_get_children(value
, &inner
);
236 ok(hr
== 1, "Expected 1, got %08x\n", hr
);
237 ok(inner
== NULL
, "Expected NULL inner, got %p\n", inner
);
239 IXMLElement_Release(value
);
240 IXMLElement_Release(child
);
241 IXMLElementCollection_Release(collection
);
242 IXMLElement_Release(element
);
244 IStream_Release(stream
);
245 IPersistStreamInit_Release(psi
);
246 IXMLDocument_Release(doc
);
248 DeleteFileA("bank.xml");
251 static void test_createElement(void)
254 IXMLDocument
*doc
= NULL
;
255 IXMLElement
*element
= NULL
, *root
= NULL
;
256 VARIANT vType
, vName
;
259 hr
= CoCreateInstance(&CLSID_XMLDocument
, NULL
, CLSCTX_INPROC_SERVER
,
260 &IID_IXMLDocument
, (LPVOID
*)&doc
);
263 skip("Failed to create XMLDocument instance\n");
267 /* invalid vType type */
268 V_VT(&vType
) = VT_NULL
;
269 V_VT(&vName
) = VT_NULL
;
270 element
= (IXMLElement
*)0xdeadbeef;
271 hr
= IXMLDocument_createElement(doc
, vType
, vName
, &element
);
272 ok(hr
== E_INVALIDARG
, "Expected E_INVALIDARG, got %08x\n", hr
);
273 ok(element
== NULL
, "Expected NULL element\n");
275 /* invalid vType value */
276 V_VT(&vType
) = VT_I4
;
278 V_VT(&vName
) = VT_NULL
;
279 hr
= IXMLDocument_createElement(doc
, vType
, vName
, &element
);
280 /* Up to and including SP7, createElement returns an element. */
283 ok(element
!= NULL
, "Expected element\n");
286 hr
= IXMLElement_get_type(element
, &type
);
287 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
288 /* SP7 returns an XMLELEMTYPE_ELEMENT */
289 ok(type
== XMLELEMTYPE_OTHER
|| type
== XMLELEMTYPE_ELEMENT
,
290 "Expected XMLELEMTYPE_OTHER || XMLELEMTYPE_ELEMENT, got %ld\n", type
);
292 IXMLElement_Release(element
);
297 ok(hr
== E_NOTIMPL
, "Expected E_NOTIMPL, got %08x\n", hr
);
298 ok(element
== NULL
, "Expected NULL element\n");
301 /* invalid vName type */
302 V_VT(&vType
) = VT_I4
;
303 V_I4(&vType
) = XMLELEMTYPE_ELEMENT
;
304 V_VT(&vName
) = VT_I4
;
305 hr
= IXMLDocument_createElement(doc
, vType
, vName
, &element
);
306 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
307 ok(element
!= NULL
, "Expected non-NULL element\n");
309 hr
= IXMLElement_get_type(element
, &type
);
310 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
311 ok(type
== XMLELEMTYPE_ELEMENT
, "Expected XMLELEMTYPE_ELEMENT, got %ld\n", type
);
313 IXMLElement_Release(element
);
316 V_VT(&vType
) = VT_I4
;
317 V_I4(&vType
) = XMLELEMTYPE_ELEMENT
;
318 V_VT(&vName
) = VT_I4
;
319 hr
= IXMLDocument_createElement(doc
, vType
, vName
, NULL
);
320 ok(hr
== E_INVALIDARG
, "Expected E_INVALIDARG, got %08x\n", hr
);
322 root
= (IXMLElement
*)0xdeadbeef;
323 hr
= IXMLDocument_get_root(doc
, &root
);
324 ok(hr
== E_FAIL
, "Expected E_FAIL, got %08x\n", hr
);
325 ok(root
== NULL
, "Expected NULL root\n");
327 V_VT(&vType
) = VT_I4
;
328 V_I4(&vType
) = XMLELEMTYPE_ELEMENT
;
329 V_VT(&vName
) = VT_NULL
;
330 hr
= IXMLDocument_createElement(doc
, vType
, vName
, &element
);
331 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
332 ok(element
!= NULL
, "Expected non-NULL element\n");
334 /* createElement does not set the new element as root */
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 IXMLElement_Release(element
);
341 IXMLDocument_Release(doc
);
348 hr
= CoInitialize(NULL
);
349 ok(hr
== S_OK
, "failed to init com\n");
352 test_createElement();