TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / xmllite / tests / writer.c
blob577b3307c23bba1cbb83028c99140b43f2d27380
1 /*
2 * XMLLite IXmlWriter tests
4 * Copyright 2011 (C) Alistair Leslie-Hughes
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
20 #define COBJMACROS
22 #include <stdarg.h>
23 #include <stdio.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "ole2.h"
28 #include "xmllite.h"
29 #include "wine/test.h"
31 #include "initguid.h"
32 DEFINE_GUID(IID_IXmlWriterOutput, 0xc1131708, 0x0f59, 0x477f, 0x93, 0x59, 0x7d, 0x33, 0x24, 0x51, 0xbc, 0x1a);
34 static HRESULT (WINAPI *pCreateXmlWriter)(REFIID riid, void **ppvObject, IMalloc *pMalloc);
35 static HRESULT (WINAPI *pCreateXmlWriterOutputWithEncodingName)(IUnknown *stream,
36 IMalloc *imalloc,
37 LPCWSTR encoding_name,
38 IXmlWriterOutput **output);
39 static HRESULT (WINAPI *pCreateXmlWriterOutputWithEncodingCodePage)(IUnknown *stream,
40 IMalloc *imalloc,
41 UINT codepage,
42 IXmlWriterOutput **output);
44 static HRESULT WINAPI testoutput_QueryInterface(IUnknown *iface, REFIID riid, void **obj)
46 if (IsEqualGUID(riid, &IID_IUnknown)) {
47 *obj = iface;
48 return S_OK;
50 else {
51 ok(0, "unknown riid=%s\n", wine_dbgstr_guid(riid));
52 return E_NOINTERFACE;
56 static ULONG WINAPI testoutput_AddRef(IUnknown *iface)
58 return 2;
61 static ULONG WINAPI testoutput_Release(IUnknown *iface)
63 return 1;
66 static const IUnknownVtbl testoutputvtbl = {
67 testoutput_QueryInterface,
68 testoutput_AddRef,
69 testoutput_Release
72 static IUnknown testoutput = { &testoutputvtbl };
74 static HRESULT WINAPI teststream_QueryInterface(ISequentialStream *iface, REFIID riid, void **obj)
76 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_ISequentialStream))
78 *obj = iface;
79 return S_OK;
82 *obj = NULL;
83 return E_NOINTERFACE;
86 static ULONG WINAPI teststream_AddRef(ISequentialStream *iface)
88 return 2;
91 static ULONG WINAPI teststream_Release(ISequentialStream *iface)
93 return 1;
96 static HRESULT WINAPI teststream_Read(ISequentialStream *iface, void *pv, ULONG cb, ULONG *pread)
98 ok(0, "unexpected call\n");
99 return E_NOTIMPL;
102 static ULONG g_write_len;
103 static HRESULT WINAPI teststream_Write(ISequentialStream *iface, const void *pv, ULONG cb, ULONG *written)
105 g_write_len = cb;
106 *written = cb;
107 return S_OK;
110 static const ISequentialStreamVtbl teststreamvtbl =
112 teststream_QueryInterface,
113 teststream_AddRef,
114 teststream_Release,
115 teststream_Read,
116 teststream_Write
119 static ISequentialStream teststream = { &teststreamvtbl };
121 static void test_writer_create(void)
123 HRESULT hr;
124 IXmlWriter *writer;
125 LONG_PTR value;
127 /* crashes native */
128 if (0)
130 pCreateXmlWriter(&IID_IXmlWriter, NULL, NULL);
131 pCreateXmlWriter(NULL, (void**)&writer, NULL);
134 hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
135 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
137 /* check default properties values */
138 value = 0;
139 hr = IXmlWriter_GetProperty(writer, XmlWriterProperty_ByteOrderMark, &value);
140 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
141 ok(value == TRUE, "got %ld\n", value);
143 value = TRUE;
144 hr = IXmlWriter_GetProperty(writer, XmlWriterProperty_Indent, &value);
145 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
146 ok(value == FALSE, "got %ld\n", value);
148 value = TRUE;
149 hr = IXmlWriter_GetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, &value);
150 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
151 ok(value == FALSE, "got %ld\n", value);
153 value = XmlConformanceLevel_Auto;
154 hr = IXmlWriter_GetProperty(writer, XmlWriterProperty_ConformanceLevel, &value);
155 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
156 ok(value == XmlConformanceLevel_Document, "got %ld\n", value);
158 IXmlWriter_Release(writer);
161 static BOOL init_pointers(void)
163 /* don't free module here, it's to be unloaded on exit */
164 HMODULE mod = LoadLibraryA("xmllite.dll");
166 if (!mod)
168 win_skip("xmllite library not available\n");
169 return FALSE;
172 #define MAKEFUNC(f) if (!(p##f = (void*)GetProcAddress(mod, #f))) return FALSE;
173 MAKEFUNC(CreateXmlWriter);
174 MAKEFUNC(CreateXmlWriterOutputWithEncodingName);
175 MAKEFUNC(CreateXmlWriterOutputWithEncodingCodePage);
176 #undef MAKEFUNC
178 return TRUE;
181 static void test_writeroutput(void)
183 static const WCHAR utf16W[] = {'u','t','f','-','1','6',0};
184 IXmlWriterOutput *output;
185 IUnknown *unk;
186 HRESULT hr;
188 output = NULL;
189 hr = pCreateXmlWriterOutputWithEncodingName(&testoutput, NULL, NULL, &output);
190 ok(hr == S_OK, "got %08x\n", hr);
191 IUnknown_Release(output);
193 hr = pCreateXmlWriterOutputWithEncodingName(&testoutput, NULL, utf16W, &output);
194 ok(hr == S_OK, "got %08x\n", hr);
195 unk = NULL;
196 hr = IUnknown_QueryInterface(output, &IID_IXmlWriterOutput, (void**)&unk);
197 ok(hr == S_OK, "got %08x\n", hr);
198 ok(unk != NULL, "got %p\n", unk);
199 /* releasing 'unk' crashes on native */
200 IUnknown_Release(output);
202 output = NULL;
203 hr = pCreateXmlWriterOutputWithEncodingCodePage(&testoutput, NULL, ~0u, &output);
204 ok(hr == S_OK, "got %08x\n", hr);
205 IUnknown_Release(output);
207 hr = pCreateXmlWriterOutputWithEncodingCodePage(&testoutput, NULL, CP_UTF8, &output);
208 ok(hr == S_OK, "got %08x\n", hr);
209 unk = NULL;
210 hr = IUnknown_QueryInterface(output, &IID_IXmlWriterOutput, (void**)&unk);
211 ok(hr == S_OK, "got %08x\n", hr);
212 ok(unk != NULL, "got %p\n", unk);
213 /* releasing 'unk' crashes on native */
214 IUnknown_Release(output);
217 static void test_writestartdocument(void)
219 static const char fullprolog[] = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
220 static const char prologversion[] = "<?xml version=\"1.0\"?>";
221 static const WCHAR versionW[] = {'v','e','r','s','i','o','n','=','"','1','.','0','"',0};
222 static const WCHAR xmlW[] = {'x','m','l',0};
223 IXmlWriter *writer;
224 HGLOBAL hglobal;
225 IStream *stream;
226 HRESULT hr;
227 char *ptr;
229 hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
230 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
232 /* output not set */
233 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
234 ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr);
236 hr = IXmlWriter_WriteProcessingInstruction(writer, xmlW, versionW);
237 ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr);
239 hr = IXmlWriter_Flush(writer);
240 ok(hr == S_OK, "got 0x%08x\n", hr);
242 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
243 ok(hr == S_OK, "got 0x%08x\n", hr);
245 hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
246 ok(hr == S_OK, "got 0x%08x\n", hr);
248 /* nothing written yet */
249 hr = IXmlWriter_Flush(writer);
250 ok(hr == S_OK, "got 0x%08x\n", hr);
252 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
253 ok(hr == S_OK, "got 0x%08x\n", hr);
255 hr = IXmlWriter_Flush(writer);
256 ok(hr == S_OK, "got 0x%08x\n", hr);
258 hr = GetHGlobalFromStream(stream, &hglobal);
259 ok(hr == S_OK, "got 0x%08x\n", hr);
261 ptr = GlobalLock(hglobal);
262 ok(!strncmp(ptr, fullprolog, strlen(fullprolog)), "got %s, expected %s\n", ptr, fullprolog);
263 GlobalUnlock(hglobal);
265 /* one more time */
266 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
267 ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
268 IStream_Release(stream);
270 /* now add PI manually, and try to start a document */
271 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
272 ok(hr == S_OK, "got 0x%08x\n", hr);
274 hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
275 ok(hr == S_OK, "got 0x%08x\n", hr);
277 hr = IXmlWriter_WriteProcessingInstruction(writer, xmlW, versionW);
278 ok(hr == S_OK, "got 0x%08x\n", hr);
280 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
281 ok(hr == S_OK, "got 0x%08x\n", hr);
283 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
284 ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
286 /* another attempt to add 'xml' PI */
287 hr = IXmlWriter_WriteProcessingInstruction(writer, xmlW, versionW);
288 ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
290 hr = IXmlWriter_Flush(writer);
291 ok(hr == S_OK, "got 0x%08x\n", hr);
293 hr = GetHGlobalFromStream(stream, &hglobal);
294 ok(hr == S_OK, "got 0x%08x\n", hr);
296 ptr = GlobalLock(hglobal);
297 ok(!strncmp(ptr, prologversion, strlen(prologversion)), "got %s\n", ptr);
298 GlobalUnlock(hglobal);
300 IStream_Release(stream);
301 IXmlWriter_Release(writer);
304 static void test_flush(void)
306 IXmlWriter *writer;
307 HRESULT hr;
309 hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
310 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
312 hr = IXmlWriter_SetOutput(writer, (IUnknown*)&teststream);
313 ok(hr == S_OK, "got 0x%08x\n", hr);
315 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
316 ok(hr == S_OK, "got 0x%08x\n", hr);
318 g_write_len = 0;
319 hr = IXmlWriter_Flush(writer);
320 ok(hr == S_OK, "got 0x%08x\n", hr);
321 ok(g_write_len > 0, "got %d\n", g_write_len);
323 g_write_len = 1;
324 hr = IXmlWriter_Flush(writer);
325 ok(hr == S_OK, "got 0x%08x\n", hr);
326 ok(g_write_len == 0, "got %d\n", g_write_len);
328 /* Release() flushes too */
329 g_write_len = 1;
330 IXmlWriter_Release(writer);
331 ok(g_write_len == 0, "got %d\n", g_write_len);
334 static void test_omitxmldeclaration(void)
336 static const char prologversion[] = "<?xml version=\"1.0\"?>";
337 static const WCHAR versionW[] = {'v','e','r','s','i','o','n','=','"','1','.','0','"',0};
338 static const WCHAR xmlW[] = {'x','m','l',0};
339 IXmlWriter *writer;
340 HGLOBAL hglobal;
341 IStream *stream;
342 HRESULT hr;
343 char *ptr;
345 hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
346 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
348 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
349 ok(hr == S_OK, "got 0x%08x\n", hr);
351 hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
352 ok(hr == S_OK, "got 0x%08x\n", hr);
354 hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, TRUE);
355 ok(hr == S_OK, "got 0x%08x\n", hr);
357 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
358 ok(hr == S_OK, "got 0x%08x\n", hr);
360 hr = IXmlWriter_Flush(writer);
361 ok(hr == S_OK, "got 0x%08x\n", hr);
363 hr = GetHGlobalFromStream(stream, &hglobal);
364 ok(hr == S_OK, "got 0x%08x\n", hr);
366 ptr = GlobalLock(hglobal);
367 ok(!ptr, "got %p\n", ptr);
368 GlobalUnlock(hglobal);
370 /* one more time */
371 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
372 ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
374 IStream_Release(stream);
376 /* now add PI manually, and try to start a document */
377 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
378 ok(hr == S_OK, "got 0x%08x\n", hr);
380 hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
381 ok(hr == S_OK, "got 0x%08x\n", hr);
383 hr = IXmlWriter_WriteProcessingInstruction(writer, xmlW, versionW);
384 ok(hr == S_OK, "got 0x%08x\n", hr);
386 hr = IXmlWriter_Flush(writer);
387 ok(hr == S_OK, "got 0x%08x\n", hr);
389 hr = GetHGlobalFromStream(stream, &hglobal);
390 ok(hr == S_OK, "got 0x%08x\n", hr);
392 ptr = GlobalLock(hglobal);
393 ok(!strncmp(ptr, prologversion, strlen(prologversion)), "got %s\n", ptr);
394 GlobalUnlock(hglobal);
396 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
397 ok(hr == S_OK, "got 0x%08x\n", hr);
399 hr = IXmlWriter_Flush(writer);
400 ok(hr == S_OK, "got 0x%08x\n", hr);
402 ptr = GlobalLock(hglobal);
403 ok(!strncmp(ptr, prologversion, strlen(prologversion)), "got %s\n", ptr);
404 GlobalUnlock(hglobal);
406 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
407 ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
409 hr = IXmlWriter_Flush(writer);
410 ok(hr == S_OK, "got 0x%08x\n", hr);
412 ptr = GlobalLock(hglobal);
413 ok(!strncmp(ptr, prologversion, strlen(prologversion)), "got %s\n", ptr);
414 GlobalUnlock(hglobal);
416 /* another attempt to add 'xml' PI */
417 hr = IXmlWriter_WriteProcessingInstruction(writer, xmlW, versionW);
418 ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
420 hr = IXmlWriter_Flush(writer);
421 ok(hr == S_OK, "got 0x%08x\n", hr);
423 IStream_Release(stream);
424 IXmlWriter_Release(writer);
427 static void test_bom(void)
429 static const WCHAR versionW[] = {'v','e','r','s','i','o','n','=','"','1','.','0','"',0};
430 static const WCHAR utf16W[] = {'u','t','f','-','1','6',0};
431 static const WCHAR xmlW[] = {'x','m','l',0};
432 static const WCHAR aW[] = {'a',0};
433 IXmlWriterOutput *output;
434 unsigned char *ptr;
435 IXmlWriter *writer;
436 IStream *stream;
437 HGLOBAL hglobal;
438 HRESULT hr;
440 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
441 ok(hr == S_OK, "got 0x%08x\n", hr);
443 hr = pCreateXmlWriterOutputWithEncodingName((IUnknown*)stream, NULL, utf16W, &output);
444 ok(hr == S_OK, "got %08x\n", hr);
446 hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
447 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
449 hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, TRUE);
450 ok(hr == S_OK, "got 0x%08x\n", hr);
452 hr = IXmlWriter_SetOutput(writer, output);
453 ok(hr == S_OK, "got 0x%08x\n", hr);
455 /* BOM is on by default */
456 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
457 ok(hr == S_OK, "got 0x%08x\n", hr);
459 hr = IXmlWriter_Flush(writer);
460 ok(hr == S_OK, "got 0x%08x\n", hr);
462 hr = GetHGlobalFromStream(stream, &hglobal);
463 ok(hr == S_OK, "got 0x%08x\n", hr);
465 ptr = GlobalLock(hglobal);
466 ok(ptr[0] == 0xff && ptr[1] == 0xfe, "got %x,%x\n", ptr[0], ptr[1]);
467 GlobalUnlock(hglobal);
469 IStream_Release(stream);
470 IUnknown_Release(output);
472 /* start with PI */
473 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
474 ok(hr == S_OK, "got 0x%08x\n", hr);
476 hr = pCreateXmlWriterOutputWithEncodingName((IUnknown*)stream, NULL, utf16W, &output);
477 ok(hr == S_OK, "got %08x\n", hr);
479 hr = IXmlWriter_SetOutput(writer, output);
480 ok(hr == S_OK, "got 0x%08x\n", hr);
482 hr = IXmlWriter_WriteProcessingInstruction(writer, xmlW, versionW);
483 ok(hr == S_OK, "got 0x%08x\n", hr);
485 hr = IXmlWriter_Flush(writer);
486 ok(hr == S_OK, "got 0x%08x\n", hr);
488 hr = GetHGlobalFromStream(stream, &hglobal);
489 ok(hr == S_OK, "got 0x%08x\n", hr);
491 ptr = GlobalLock(hglobal);
492 ok(ptr[0] == 0xff && ptr[1] == 0xfe, "got %x,%x\n", ptr[0], ptr[1]);
493 GlobalUnlock(hglobal);
495 IUnknown_Release(output);
496 IStream_Release(stream);
498 /* start with element */
499 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
500 ok(hr == S_OK, "got 0x%08x\n", hr);
502 hr = pCreateXmlWriterOutputWithEncodingName((IUnknown*)stream, NULL, utf16W, &output);
503 ok(hr == S_OK, "got %08x\n", hr);
505 hr = IXmlWriter_SetOutput(writer, output);
506 ok(hr == S_OK, "got 0x%08x\n", hr);
508 hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
509 ok(hr == S_OK, "got 0x%08x\n", hr);
511 hr = IXmlWriter_Flush(writer);
512 ok(hr == S_OK, "got 0x%08x\n", hr);
514 hr = GetHGlobalFromStream(stream, &hglobal);
515 ok(hr == S_OK, "got 0x%08x\n", hr);
517 ptr = GlobalLock(hglobal);
518 ok(ptr[0] == 0xff && ptr[1] == 0xfe, "got %x,%x\n", ptr[0], ptr[1]);
519 GlobalUnlock(hglobal);
521 IUnknown_Release(output);
522 IStream_Release(stream);
524 /* WriteElementString */
525 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
526 ok(hr == S_OK, "got 0x%08x\n", hr);
528 hr = pCreateXmlWriterOutputWithEncodingName((IUnknown*)stream, NULL, utf16W, &output);
529 ok(hr == S_OK, "got %08x\n", hr);
531 hr = IXmlWriter_SetOutput(writer, output);
532 ok(hr == S_OK, "got 0x%08x\n", hr);
534 hr = IXmlWriter_WriteElementString(writer, NULL, aW, NULL, NULL);
535 ok(hr == S_OK, "got 0x%08x\n", hr);
537 hr = IXmlWriter_Flush(writer);
538 ok(hr == S_OK, "got 0x%08x\n", hr);
540 hr = GetHGlobalFromStream(stream, &hglobal);
541 ok(hr == S_OK, "got 0x%08x\n", hr);
543 ptr = GlobalLock(hglobal);
544 ok(ptr[0] == 0xff && ptr[1] == 0xfe, "got %x,%x\n", ptr[0], ptr[1]);
545 GlobalUnlock(hglobal);
547 IUnknown_Release(output);
548 IStream_Release(stream);
550 IXmlWriter_Release(writer);
553 static void test_writestartelement(void)
555 static const WCHAR valueW[] = {'v','a','l','u','e',0};
556 static const char *str = "<a><b>value</b>";
557 static const WCHAR aW[] = {'a',0};
558 static const WCHAR bW[] = {'b',0};
559 char *ptr;
560 IXmlWriter *writer;
561 IStream *stream;
562 HGLOBAL hglobal;
563 HRESULT hr;
565 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
566 ok(hr == S_OK, "got 0x%08x\n", hr);
568 hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
569 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
571 hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
572 ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr);
574 hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
575 ok(hr == S_OK, "got 0x%08x\n", hr);
577 hr = IXmlWriter_WriteStartElement(writer, aW, NULL, NULL);
578 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
580 hr = IXmlWriter_WriteStartElement(writer, NULL, NULL, NULL);
581 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
583 hr = IXmlWriter_WriteStartElement(writer, NULL, NULL, aW);
584 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
586 hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
587 ok(hr == S_OK, "got 0x%08x\n", hr);
589 hr = GetHGlobalFromStream(stream, &hglobal);
590 ok(hr == S_OK, "got 0x%08x\n", hr);
592 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
593 ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
595 hr = IXmlWriter_Flush(writer);
596 ok(hr == S_OK, "got 0x%08x\n", hr);
598 ptr = GlobalLock(hglobal);
599 ok(!strncmp(ptr, "<a", 2), "got %s\n", ptr);
600 GlobalUnlock(hglobal);
602 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
603 ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
605 hr = IXmlWriter_WriteStartElement(writer, NULL, NULL, NULL);
606 ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
608 hr = IXmlWriter_WriteProcessingInstruction(writer, aW, aW);
609 ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
611 IStream_Release(stream);
612 IXmlWriter_Release(writer);
614 /* WriteElementString */
615 hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
616 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
618 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
619 ok(hr == S_OK, "got 0x%08x\n", hr);
621 hr = IXmlWriter_WriteElementString(writer, NULL, bW, NULL, valueW);
622 ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr);
624 hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
625 ok(hr == S_OK, "got 0x%08x\n", hr);
627 hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
628 ok(hr == S_OK, "got 0x%08x\n", hr);
630 hr = IXmlWriter_WriteElementString(writer, NULL, bW, NULL, valueW);
631 ok(hr == S_OK, "got 0x%08x\n", hr);
633 hr = IXmlWriter_Flush(writer);
634 ok(hr == S_OK, "got 0x%08x\n", hr);
636 hr = GetHGlobalFromStream(stream, &hglobal);
637 ok(hr == S_OK, "got 0x%08x\n", hr);
639 ptr = GlobalLock(hglobal);
640 ok(!strncmp(ptr, str, strlen(str)), "got %s\n", ptr);
641 GlobalUnlock(hglobal);
643 IStream_Release(stream);
644 IXmlWriter_Release(writer);
647 static void test_writeendelement(void)
649 static const WCHAR aW[] = {'a',0};
650 static const WCHAR bW[] = {'b',0};
651 char *ptr;
652 IXmlWriter *writer;
653 IStream *stream;
654 HGLOBAL hglobal;
655 HRESULT hr;
657 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
658 ok(hr == S_OK, "got 0x%08x\n", hr);
660 hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
661 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
663 hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
664 ok(hr == S_OK, "got 0x%08x\n", hr);
666 hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
667 ok(hr == S_OK, "got 0x%08x\n", hr);
669 hr = IXmlWriter_WriteStartElement(writer, NULL, bW, NULL);
670 ok(hr == S_OK, "got 0x%08x\n", hr);
672 hr = IXmlWriter_WriteEndElement(writer);
673 ok(hr == S_OK, "got 0x%08x\n", hr);
675 hr = IXmlWriter_WriteEndElement(writer);
676 ok(hr == S_OK, "got 0x%08x\n", hr);
678 hr = GetHGlobalFromStream(stream, &hglobal);
679 ok(hr == S_OK, "got 0x%08x\n", hr);
681 hr = IXmlWriter_Flush(writer);
682 ok(hr == S_OK, "got 0x%08x\n", hr);
684 ptr = GlobalLock(hglobal);
685 ok(!strncmp(ptr, "<a><b /></a>", 12), "got %s\n", ptr);
686 GlobalUnlock(hglobal);
688 IXmlWriter_Release(writer);
689 IStream_Release(stream);
692 static void test_writeenddocument(void)
694 static const WCHAR aW[] = {'a',0};
695 static const WCHAR bW[] = {'b',0};
696 IXmlWriter *writer;
697 IStream *stream;
698 HGLOBAL hglobal;
699 HRESULT hr;
700 char *ptr;
702 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
703 ok(hr == S_OK, "got 0x%08x\n", hr);
705 hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
706 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
708 hr = IXmlWriter_WriteEndDocument(writer);
709 ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr);
711 hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
712 ok(hr == S_OK, "got 0x%08x\n", hr);
714 /* WriteEndDocument resets it to initial state */
715 hr = IXmlWriter_WriteEndDocument(writer);
716 ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
718 hr = IXmlWriter_WriteEndDocument(writer);
719 ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
721 hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Omit);
722 ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
724 hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
725 ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
727 hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
728 ok(hr == S_OK, "got 0x%08x\n", hr);
730 hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
731 ok(hr == S_OK, "got 0x%08x\n", hr);
733 hr = IXmlWriter_WriteStartElement(writer, NULL, bW, NULL);
734 ok(hr == S_OK, "got 0x%08x\n", hr);
736 hr = IXmlWriter_WriteEndDocument(writer);
737 ok(hr == S_OK, "got 0x%08x\n", hr);
739 hr = GetHGlobalFromStream(stream, &hglobal);
740 ok(hr == S_OK, "got 0x%08x\n", hr);
742 ptr = GlobalLock(hglobal);
743 ok(ptr == NULL, "got %p\n", ptr);
745 /* we still need to flush manually, WriteEndDocument doesn't do that */
746 hr = IXmlWriter_Flush(writer);
747 ok(hr == S_OK, "got 0x%08x\n", hr);
749 ptr = GlobalLock(hglobal);
750 ok(ptr != NULL, "got %p\n", ptr);
751 ok(!strncmp(ptr, "<a><b /></a>", 12), "got %s\n", ptr);
752 GlobalUnlock(hglobal);
754 IXmlWriter_Release(writer);
755 IStream_Release(stream);
758 START_TEST(writer)
760 if (!init_pointers())
761 return;
763 test_writer_create();
764 test_writeroutput();
765 test_writestartdocument();
766 test_writestartelement();
767 test_writeendelement();
768 test_flush();
769 test_omitxmldeclaration();
770 test_bom();
771 test_writeenddocument();