[XMLLITE_WINETEST] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / rostests / winetests / xmllite / writer.c
index 608cbb1..4ca8b64 100644 (file)
@@ -22,6 +22,7 @@
 #define _INC_WINDOWS
 #define COM_NO_WINDOWS_H
 
+#define CONST_VTABLE
 #define COBJMACROS
 
 #include <stdarg.h>
 
 #include <windef.h>
 #include <winbase.h>
+#include <winnls.h>
 #include <objbase.h>
-//#include "ole2.h"
+#include <ole2.h>
 #include <xmllite.h>
 #include <wine/test.h>
 
+#include <initguid.h>
+DEFINE_GUID(IID_IXmlWriterOutput, 0xc1131708, 0x0f59, 0x477f, 0x93, 0x59, 0x7d, 0x33, 0x24, 0x51, 0xbc, 0x1a);
+
 static HRESULT (WINAPI *pCreateXmlWriter)(REFIID riid, void **ppvObject, IMalloc *pMalloc);
+static HRESULT (WINAPI *pCreateXmlWriterOutputWithEncodingName)(IUnknown *stream,
+                                                                IMalloc *imalloc,
+                                                                LPCWSTR encoding_name,
+                                                                IXmlWriterOutput **output);
+static HRESULT (WINAPI *pCreateXmlWriterOutputWithEncodingCodePage)(IUnknown *stream,
+                                                                    IMalloc *imalloc,
+                                                                    UINT codepage,
+                                                                    IXmlWriterOutput **output);
+
+static HRESULT WINAPI testoutput_QueryInterface(IUnknown *iface, REFIID riid, void **obj)
+{
+    if (IsEqualGUID(riid, &IID_IUnknown)) {
+        *obj = iface;
+        return S_OK;
+    }
+    else {
+        ok(0, "unknown riid=%s\n", wine_dbgstr_guid(riid));
+        return E_NOINTERFACE;
+    }
+}
+
+static ULONG WINAPI testoutput_AddRef(IUnknown *iface)
+{
+    return 2;
+}
+
+static ULONG WINAPI testoutput_Release(IUnknown *iface)
+{
+    return 1;
+}
+
+static const IUnknownVtbl testoutputvtbl = {
+    testoutput_QueryInterface,
+    testoutput_AddRef,
+    testoutput_Release
+};
+
+static IUnknown testoutput = { &testoutputvtbl };
+
+static HRESULT WINAPI teststream_QueryInterface(ISequentialStream *iface, REFIID riid, void **obj)
+{
+    if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_ISequentialStream))
+    {
+        *obj = iface;
+        return S_OK;
+    }
+
+    *obj = NULL;
+    return E_NOINTERFACE;
+}
+
+static ULONG WINAPI teststream_AddRef(ISequentialStream *iface)
+{
+    return 2;
+}
+
+static ULONG WINAPI teststream_Release(ISequentialStream *iface)
+{
+    return 1;
+}
+
+static HRESULT WINAPI teststream_Read(ISequentialStream *iface, void *pv, ULONG cb, ULONG *pread)
+{
+    ok(0, "unexpected call\n");
+    return E_NOTIMPL;
+}
+
+static ULONG g_write_len;
+static HRESULT WINAPI teststream_Write(ISequentialStream *iface, const void *pv, ULONG cb, ULONG *written)
+{
+    g_write_len = cb;
+    *written = cb;
+    return S_OK;
+}
+
+static const ISequentialStreamVtbl teststreamvtbl =
+{
+    teststream_QueryInterface,
+    teststream_AddRef,
+    teststream_Release,
+    teststream_Read,
+    teststream_Write
+};
+
+static ISequentialStream teststream = { &teststreamvtbl };
 
 static void test_writer_create(void)
 {
     HRESULT hr;
     IXmlWriter *writer;
+    LONG_PTR value;
 
     /* crashes native */
     if (0)
     {
         pCreateXmlWriter(&IID_IXmlWriter, NULL, NULL);
-        pCreateXmlWriter(NULL, (LPVOID*)&writer, NULL);
+        pCreateXmlWriter(NULL, (void**)&writer, NULL);
     }
 
-    hr = pCreateXmlWriter(&IID_IXmlWriter, (LPVOID*)&writer, NULL);
+    hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
-    if(hr == S_OK)
-    {
-        IXmlWriter_Release(writer);
-    }
+
+    /* check default properties values */
+    value = 0;
+    hr = IXmlWriter_GetProperty(writer, XmlWriterProperty_ByteOrderMark, &value);
+    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
+    ok(value == TRUE, "got %ld\n", value);
+
+    value = TRUE;
+    hr = IXmlWriter_GetProperty(writer, XmlWriterProperty_Indent, &value);
+    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
+    ok(value == FALSE, "got %ld\n", value);
+
+    value = TRUE;
+    hr = IXmlWriter_GetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, &value);
+    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
+    ok(value == FALSE, "got %ld\n", value);
+
+    value = XmlConformanceLevel_Auto;
+    hr = IXmlWriter_GetProperty(writer, XmlWriterProperty_ConformanceLevel, &value);
+    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
+    ok(value == XmlConformanceLevel_Document, "got %ld\n", value);
+
+    IXmlWriter_Release(writer);
 }
 
 static BOOL init_pointers(void)
@@ -69,25 +179,602 @@ static BOOL init_pointers(void)
 
 #define MAKEFUNC(f) if (!(p##f = (void*)GetProcAddress(mod, #f))) return FALSE;
     MAKEFUNC(CreateXmlWriter);
+    MAKEFUNC(CreateXmlWriterOutputWithEncodingName);
+    MAKEFUNC(CreateXmlWriterOutputWithEncodingCodePage);
 #undef MAKEFUNC
 
     return TRUE;
 }
 
-START_TEST(writer)
+static void test_writeroutput(void)
+{
+    static const WCHAR utf16W[] = {'u','t','f','-','1','6',0};
+    IXmlWriterOutput *output;
+    IUnknown *unk;
+    HRESULT hr;
+
+    output = NULL;
+    hr = pCreateXmlWriterOutputWithEncodingName(&testoutput, NULL, NULL, &output);
+    ok(hr == S_OK, "got %08x\n", hr);
+    IUnknown_Release(output);
+
+    hr = pCreateXmlWriterOutputWithEncodingName(&testoutput, NULL, utf16W, &output);
+    ok(hr == S_OK, "got %08x\n", hr);
+    unk = NULL;
+    hr = IUnknown_QueryInterface(output, &IID_IXmlWriterOutput, (void**)&unk);
+    ok(hr == S_OK, "got %08x\n", hr);
+    ok(unk != NULL, "got %p\n", unk);
+    /* releasing 'unk' crashes on native */
+    IUnknown_Release(output);
+
+    output = NULL;
+    hr = pCreateXmlWriterOutputWithEncodingCodePage(&testoutput, NULL, ~0u, &output);
+    ok(hr == S_OK, "got %08x\n", hr);
+    IUnknown_Release(output);
+
+    hr = pCreateXmlWriterOutputWithEncodingCodePage(&testoutput, NULL, CP_UTF8, &output);
+    ok(hr == S_OK, "got %08x\n", hr);
+    unk = NULL;
+    hr = IUnknown_QueryInterface(output, &IID_IXmlWriterOutput, (void**)&unk);
+    ok(hr == S_OK, "got %08x\n", hr);
+    ok(unk != NULL, "got %p\n", unk);
+    /* releasing 'unk' crashes on native */
+    IUnknown_Release(output);
+}
+
+static void test_writestartdocument(void)
+{
+    static const char fullprolog[] = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
+    static const char prologversion[] = "<?xml version=\"1.0\"?>";
+    static const WCHAR versionW[] = {'v','e','r','s','i','o','n','=','"','1','.','0','"',0};
+    static const WCHAR xmlW[] = {'x','m','l',0};
+    IXmlWriter *writer;
+    HGLOBAL hglobal;
+    IStream *stream;
+    HRESULT hr;
+    char *ptr;
+
+    hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
+    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
+
+    /* output not set */
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
+    ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteProcessingInstruction(writer, xmlW, versionW);
+    ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    /* nothing written yet */
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = GetHGlobalFromStream(stream, &hglobal);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(!strncmp(ptr, fullprolog, strlen(fullprolog)), "got %s, expected %s\n", ptr, fullprolog);
+    GlobalUnlock(hglobal);
+
+    /* one more time */
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
+    ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
+    IStream_Release(stream);
+
+    /* now add PI manually, and try to start a document */
+    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteProcessingInstruction(writer, xmlW, versionW);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
+    ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
+
+    /* another attempt to add 'xml' PI */
+    hr = IXmlWriter_WriteProcessingInstruction(writer, xmlW, versionW);
+    ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = GetHGlobalFromStream(stream, &hglobal);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(!strncmp(ptr, prologversion, strlen(prologversion)), "got %s\n", ptr);
+    GlobalUnlock(hglobal);
+
+    IStream_Release(stream);
+    IXmlWriter_Release(writer);
+}
+
+static void test_flush(void)
+{
+    IXmlWriter *writer;
+    HRESULT hr;
+
+    hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
+    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, (IUnknown*)&teststream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    g_write_len = 0;
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+    ok(g_write_len > 0, "got %d\n", g_write_len);
+
+    g_write_len = 1;
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+    ok(g_write_len == 0, "got %d\n", g_write_len);
+
+    /* Release() flushes too */
+    g_write_len = 1;
+    IXmlWriter_Release(writer);
+    ok(g_write_len == 0, "got %d\n", g_write_len);
+}
+
+static void test_omitxmldeclaration(void)
+{
+    static const char prologversion[] = "<?xml version=\"1.0\"?>";
+    static const WCHAR versionW[] = {'v','e','r','s','i','o','n','=','"','1','.','0','"',0};
+    static const WCHAR xmlW[] = {'x','m','l',0};
+    IXmlWriter *writer;
+    HGLOBAL hglobal;
+    IStream *stream;
+    HRESULT hr;
+    char *ptr;
+
+    hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
+    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
+
+    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, TRUE);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = GetHGlobalFromStream(stream, &hglobal);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(!ptr, "got %p\n", ptr);
+    GlobalUnlock(hglobal);
+
+    /* one more time */
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
+    ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
+
+    IStream_Release(stream);
+
+    /* now add PI manually, and try to start a document */
+    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteProcessingInstruction(writer, xmlW, versionW);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = GetHGlobalFromStream(stream, &hglobal);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(!strncmp(ptr, prologversion, strlen(prologversion)), "got %s\n", ptr);
+    GlobalUnlock(hglobal);
+
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(!strncmp(ptr, prologversion, strlen(prologversion)), "got %s\n", ptr);
+    GlobalUnlock(hglobal);
+
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
+    ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(!strncmp(ptr, prologversion, strlen(prologversion)), "got %s\n", ptr);
+    GlobalUnlock(hglobal);
+
+    /* another attempt to add 'xml' PI */
+    hr = IXmlWriter_WriteProcessingInstruction(writer, xmlW, versionW);
+    ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    IStream_Release(stream);
+    IXmlWriter_Release(writer);
+}
+
+static void test_bom(void)
 {
-    HRESULT r;
+    static const WCHAR versionW[] = {'v','e','r','s','i','o','n','=','"','1','.','0','"',0};
+    static const WCHAR utf16W[] = {'u','t','f','-','1','6',0};
+    static const WCHAR xmlW[] = {'x','m','l',0};
+    static const WCHAR aW[] = {'a',0};
+    IXmlWriterOutput *output;
+    unsigned char *ptr;
+    IXmlWriter *writer;
+    IStream *stream;
+    HGLOBAL hglobal;
+    HRESULT hr;
+
+    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = pCreateXmlWriterOutputWithEncodingName((IUnknown*)stream, NULL, utf16W, &output);
+    ok(hr == S_OK, "got %08x\n", hr);
+
+    hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
+    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
+
+    hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, TRUE);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, output);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    /* BOM is on by default */
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = GetHGlobalFromStream(stream, &hglobal);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(ptr[0] == 0xff && ptr[1] == 0xfe, "got %x,%x\n", ptr[0], ptr[1]);
+    GlobalUnlock(hglobal);
+
+    IStream_Release(stream);
+    IUnknown_Release(output);
+
+    /* start with PI */
+    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = pCreateXmlWriterOutputWithEncodingName((IUnknown*)stream, NULL, utf16W, &output);
+    ok(hr == S_OK, "got %08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, output);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteProcessingInstruction(writer, xmlW, versionW);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = GetHGlobalFromStream(stream, &hglobal);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(ptr[0] == 0xff && ptr[1] == 0xfe, "got %x,%x\n", ptr[0], ptr[1]);
+    GlobalUnlock(hglobal);
+
+    IUnknown_Release(output);
+    IStream_Release(stream);
+
+    /* start with element */
+    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = pCreateXmlWriterOutputWithEncodingName((IUnknown*)stream, NULL, utf16W, &output);
+    ok(hr == S_OK, "got %08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, output);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = GetHGlobalFromStream(stream, &hglobal);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(ptr[0] == 0xff && ptr[1] == 0xfe, "got %x,%x\n", ptr[0], ptr[1]);
+    GlobalUnlock(hglobal);
+
+    IUnknown_Release(output);
+    IStream_Release(stream);
+
+    /* WriteElementString */
+    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = pCreateXmlWriterOutputWithEncodingName((IUnknown*)stream, NULL, utf16W, &output);
+    ok(hr == S_OK, "got %08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, output);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteElementString(writer, NULL, aW, NULL, NULL);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = GetHGlobalFromStream(stream, &hglobal);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(ptr[0] == 0xff && ptr[1] == 0xfe, "got %x,%x\n", ptr[0], ptr[1]);
+    GlobalUnlock(hglobal);
+
+    IUnknown_Release(output);
+    IStream_Release(stream);
+
+    IXmlWriter_Release(writer);
+}
+
+static void test_writestartelement(void)
+{
+    static const WCHAR valueW[] = {'v','a','l','u','e',0};
+    static const char *str = "<a><b>value</b>";
+    static const WCHAR aW[] = {'a',0};
+    static const WCHAR bW[] = {'b',0};
+    char *ptr;
+    IXmlWriter *writer;
+    IStream *stream;
+    HGLOBAL hglobal;
+    HRESULT hr;
+
+    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
+    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
+
+    hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
+    ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartElement(writer, aW, NULL, NULL);
+    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartElement(writer, NULL, NULL, NULL);
+    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartElement(writer, NULL, NULL, aW);
+    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = GetHGlobalFromStream(stream, &hglobal);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
+    ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(!strncmp(ptr, "<a", 2), "got %s\n", ptr);
+    GlobalUnlock(hglobal);
 
-    r = CoInitialize( NULL );
-    ok( r == S_OK, "failed to init com\n");
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
+    ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
 
+    hr = IXmlWriter_WriteStartElement(writer, NULL, NULL, NULL);
+    ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteProcessingInstruction(writer, aW, aW);
+    ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
+
+    IStream_Release(stream);
+    IXmlWriter_Release(writer);
+
+    /* WriteElementString */
+    hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
+    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
+
+    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteElementString(writer, NULL, bW, NULL, valueW);
+    ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteElementString(writer, NULL, bW, NULL, valueW);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = GetHGlobalFromStream(stream, &hglobal);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(!strncmp(ptr, str, strlen(str)), "got %s\n", ptr);
+    GlobalUnlock(hglobal);
+
+    IStream_Release(stream);
+    IXmlWriter_Release(writer);
+}
+
+static void test_writeendelement(void)
+{
+    static const WCHAR aW[] = {'a',0};
+    static const WCHAR bW[] = {'b',0};
+    char *ptr;
+    IXmlWriter *writer;
+    IStream *stream;
+    HGLOBAL hglobal;
+    HRESULT hr;
+
+    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
+    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartElement(writer, NULL, bW, NULL);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteEndElement(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteEndElement(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = GetHGlobalFromStream(stream, &hglobal);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(!strncmp(ptr, "<a><b /></a>", 12), "got %s\n", ptr);
+    GlobalUnlock(hglobal);
+
+    IXmlWriter_Release(writer);
+    IStream_Release(stream);
+}
+
+static void test_writeenddocument(void)
+{
+    static const WCHAR aW[] = {'a',0};
+    static const WCHAR bW[] = {'b',0};
+    IXmlWriter *writer;
+    IStream *stream;
+    HGLOBAL hglobal;
+    HRESULT hr;
+    char *ptr;
+
+    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
+    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
+
+    hr = IXmlWriter_WriteEndDocument(writer);
+    ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    /* WriteEndDocument resets it to initial state */
+    hr = IXmlWriter_WriteEndDocument(writer);
+    ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteEndDocument(writer);
+    ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Omit);
+    ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
+    ok(hr == WR_E_INVALIDACTION, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartElement(writer, NULL, aW, NULL);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteStartElement(writer, NULL, bW, NULL);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = IXmlWriter_WriteEndDocument(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    hr = GetHGlobalFromStream(stream, &hglobal);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(ptr == NULL, "got %p\n", ptr);
+
+    /* we still need to flush manually, WriteEndDocument doesn't do that */
+    hr = IXmlWriter_Flush(writer);
+    ok(hr == S_OK, "got 0x%08x\n", hr);
+
+    ptr = GlobalLock(hglobal);
+    ok(ptr != NULL, "got %p\n", ptr);
+    ok(!strncmp(ptr, "<a><b /></a>", 12), "got %s\n", ptr);
+    GlobalUnlock(hglobal);
+
+    IXmlWriter_Release(writer);
+    IStream_Release(stream);
+}
+
+START_TEST(writer)
+{
     if (!init_pointers())
-    {
-       CoUninitialize();
        return;
-    }
 
     test_writer_create();
-
-    CoUninitialize();
+    test_writeroutput();
+    test_writestartdocument();
+    test_writestartelement();
+    test_writeendelement();
+    test_flush();
+    test_omitxmldeclaration();
+    test_bom();
+    test_writeenddocument();
 }