xmldoc.c 38.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * IXMLDocument tests
 *
 * Copyright 2007 James Hawkins
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define COBJMACROS

#include <stdio.h>
#include "windows.h"
#include "ole2.h"
#include "msxml2.h"
27
#include "msxml2did.h"
28 29 30 31
#include "ocidl.h"

#include "wine/test.h"

32 33 34
#define EXPECT_HR(hr,hr_exp) \
    ok(hr == hr_exp, "got 0x%08x, expected 0x%08x\n", hr, hr_exp)

35 36 37
/* Deprecated Error Code */
#define XML_E_INVALIDATROOTLEVEL    0xc00ce556

38 39 40
static void create_xml_file(LPCSTR filename)
{
    DWORD dwNumberOfBytesWritten;
41 42
    HANDLE hf = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
43

44 45
    static const char data[] =
        "<?xml version=\"1.0\" ?>\n"
46
        "<!DOCTYPE BankAccount>\n"
47 48 49 50
        "<BankAccount>\n"
        "  <Number>1234</Number>\n"
        "  <Name>Captain Ahab</Name>\n"
        "</BankAccount>";
51

52
    WriteFile(hf, data, sizeof(data) - 1, &dwNumberOfBytesWritten, NULL);
53 54 55
    CloseHandle(hf);
}

56 57 58 59 60 61 62 63
static void create_stream_on_file(IStream **stream, LPCSTR path)
{
    HANDLE hfile;
    HGLOBAL hglobal;
    LPVOID ptr;
    HRESULT hr;
    DWORD file_size, read;

64 65
    hfile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL,
                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    ok(hfile != INVALID_HANDLE_VALUE, "Expected a valid file handle\n");
    file_size = GetFileSize(hfile, NULL);

    hglobal = GlobalAlloc(GHND, file_size);
    ptr = GlobalLock(hglobal);

    ReadFile(hfile, ptr, file_size, &read, NULL);
    ok(file_size == read, "Expected to read the whole file, read %d\n", read);

    hr = CreateStreamOnHGlobal(hglobal, TRUE, stream);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(*stream != NULL, "Expected non-NULL stream\n");

    CloseHandle(hfile);
    GlobalUnlock(hglobal);
}

83 84 85 86 87
static void test_xmldoc(void)
{
    IXMLElement *element = NULL, *child = NULL, *value = NULL;
    IXMLElementCollection *collection = NULL, *inner = NULL;
    IPersistStreamInit *psi = NULL;
88
    IXMLDocument *doc = NULL;
89 90
    IStream *stream = NULL;
    VARIANT vIndex, vName;
91 92 93 94 95 96
    LONG type, num_child;
    CHAR path[MAX_PATH];
    IDispatch *disp;
    ITypeInfo *ti;
    HRESULT hr;
    BSTR name;
97 98 99 100 101 102

    static const WCHAR szBankAccount[] = {'B','A','N','K','A','C','C','O','U','N','T',0};
    static const WCHAR szNumber[] = {'N','U','M','B','E','R',0};
    static const WCHAR szNumVal[] = {'1','2','3','4',0};
    static const WCHAR szName[] = {'N','A','M','E',0};
    static const WCHAR szNameVal[] = {'C','a','p','t','a','i','n',' ','A','h','a','b',0};
103
    static const WCHAR szVersion[] = {'1','.','0',0};
104
    static const WCHAR rootW[] = {'r','o','o','t',0};
105 106

    hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
                          &IID_IXMLDocument, (void**)&doc);
    EXPECT_HR(hr, S_OK);

    /* IDispatch */
    hr = IXMLDocument_QueryInterface(doc, &IID_IDispatch, (void**)&disp);
    EXPECT_HR(hr, S_OK);

    /* just to make sure we're on right type data */
    hr = IDispatch_GetTypeInfo(disp, 0, 0, &ti);
    EXPECT_HR(hr, S_OK);
    name = NULL;
    hr = ITypeInfo_GetDocumentation(ti, DISPID_XMLDOCUMENT_ROOT, &name, NULL, NULL, NULL);
    EXPECT_HR(hr, S_OK);
    ok(!lstrcmpW(name, rootW), "got name %s\n", wine_dbgstr_w(name));
    SysFreeString(name);

    ITypeInfo_Release(ti);
    IDispatch_Release(disp);

    hr = IXMLDocument_QueryInterface(doc, &IID_IXMLDOMDocument, (void**)&disp);
    EXPECT_HR(hr, E_NOINTERFACE);
128 129 130

    create_xml_file("bank.xml");
    GetFullPathNameA("bank.xml", MAX_PATH, path, NULL);
131
    create_stream_on_file(&stream, path);
132

133
    hr = IXMLDocument_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&psi);
134
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
135 136 137
    ok(psi != NULL, "Expected non-NULL psi\n");

    hr = IXMLDocument_get_root(doc, &element);
138
    ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
139 140 141
    ok(element == NULL, "Expected NULL element\n");

    hr = IPersistStreamInit_Load(psi, stream);
142 143 144 145
    ok(hr == S_OK || hr == XML_E_INVALIDATROOTLEVEL, "Expected S_OK, got %08x\n", hr);
    if(hr == XML_E_INVALIDATROOTLEVEL)
        goto cleanup;

146 147
    ok(stream != NULL, "Expected non-NULL stream\n");

148 149 150 151
    /* version field */
    hr = IXMLDocument_get_version(doc, NULL);
    ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);

152
    name = NULL;
153 154 155 156
    hr = IXMLDocument_get_version(doc, &name);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(!lstrcmpW(name, szVersion), "Expected 1.0, got %s\n", wine_dbgstr_w(name));
    SysFreeString(name);
157 158 159 160 161 162 163 164 165

    /* doctype */
    hr = IXMLDocument_get_doctype(doc, NULL);
    ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);

    hr = IXMLDocument_get_doctype(doc, &name);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(!lstrcmpW(name, szBankAccount), "Expected BANKACCOUNT, got %s\n", wine_dbgstr_w(name));
    SysFreeString(name);
166

167
    hr = IXMLDocument_get_root(doc, &element);
168
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
169 170
    ok(element != NULL, "Expected non-NULL element\n");

171 172 173 174 175 176 177
    /* ::root() returns new instance each time */
    hr = IXMLDocument_get_root(doc, &child);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(child != NULL, "Expected non-NULL element\n");
    ok(child != element, "Expected new element instance\n");
    IXMLElement_Release(child);

178
    hr = IXMLElement_get_type(element, &type);
179
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
180
    ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
181 182

    hr = IXMLElement_get_tagName(element, &name);
183
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
184
    ok(!lstrcmpW(name, szBankAccount), "Expected BANKACCOUNT\n");
185
    SysFreeString(name);
186 187

    hr = IXMLElement_get_children(element, &collection);
188
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
189 190 191
    ok(collection != NULL, "Expected non-NULL collection\n");

    hr = IXMLElementCollection_get_length(collection, &num_child);
192
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
193
    ok(num_child == 2, "Expected 2, got %d\n", num_child);
194 195 196 197 198 199

    V_VT(&vIndex) = VT_I4;
    V_I4(&vIndex) = 0;
    V_VT(&vName) = VT_ERROR;
    V_ERROR(&vName) = DISP_E_PARAMNOTFOUND;
    hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
200
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
201 202 203
    ok(child != NULL, "Expected non-NULL child\n");

    hr = IXMLElement_get_type(child, &type);
204
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
205
    ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
206 207

    hr = IXMLElement_get_tagName(child, &name);
208
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
209
    ok(!lstrcmpW(name, szNumber), "Expected NUMBER\n");
210
    SysFreeString(name);
211 212

    hr = IXMLElement_get_children(child, &inner);
213
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
214 215 216
    ok(inner != NULL, "Expected non-NULL inner\n");

    hr = IXMLElementCollection_get_length(inner, &num_child);
217
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
218
    ok(num_child == 1, "Expected 1, got %d\n", num_child);
219 220

    hr = IXMLElementCollection_item(inner, vIndex, vName, (IDispatch **)&value);
221
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
222 223 224
    ok(value != NULL, "Expected non-NULL value\n");

    hr = IXMLElement_get_type(value, &type);
225
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
226
    ok(type == XMLELEMTYPE_TEXT, "Expected XMLELEMTYPE_TEXT, got %d\n", type);
227 228

    hr = IXMLElement_get_text(value, &name);
229
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
230
    ok(!lstrcmpW(name, szNumVal), "Expected '1234'\n");
231
    SysFreeString(name);
232 233 234 235 236

    IXMLElementCollection_Release(inner);

    inner = (IXMLElementCollection *)0xdeadbeef;
    hr = IXMLElement_get_children(value, &inner);
237
    ok(hr == 1, "Expected 1, got %08x\n", hr);
238 239 240 241 242 243 244 245 246
    ok(inner == NULL, "Expected NULL inner, got %p\n", inner);

    IXMLElement_Release(value);
    IXMLElement_Release(child);
    value = NULL;
    child = NULL;

    V_I4(&vIndex) = 1;
    hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
247
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
248 249 250
    ok(child != NULL, "Expected non-NULL child\n");

    hr = IXMLElement_get_type(child, &type);
251
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
252
    ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
253 254

    hr = IXMLElement_get_tagName(child, &name);
255
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
256
    ok(!lstrcmpW(name, szName), "Expected NAME\n");
257
    SysFreeString(name);
258 259

    hr = IXMLElement_get_children(child, &inner);
260
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
261 262 263
    ok(inner != NULL, "Expected non-NULL inner\n");

    hr = IXMLElementCollection_get_length(inner, &num_child);
264
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
265
    ok(num_child == 1, "Expected 1, got %d\n", num_child);
266 267 268

    V_I4(&vIndex) = 0;
    hr = IXMLElementCollection_item(inner, vIndex, vName, (IDispatch **)&value);
269
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
270 271 272
    ok(value != NULL, "Expected non-NULL value\n");

    hr = IXMLElement_get_type(value, &type);
273
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
274
    ok(type == XMLELEMTYPE_TEXT, "Expected XMLELEMTYPE_TEXT, got %d\n", type);
275 276

    hr = IXMLElement_get_text(value, &name);
277
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
278
    ok(!lstrcmpW(name, szNameVal), "Expected 'Captain Ahab'\n");
279
    SysFreeString(name);
280 281 282 283 284

    IXMLElementCollection_Release(inner);

    inner = (IXMLElementCollection *)0xdeadbeef;
    hr = IXMLElement_get_children(value, &inner);
285
    ok(hr == 1, "Expected 1, got %08x\n", hr);
286 287 288 289 290 291
    ok(inner == NULL, "Expected NULL inner, got %p\n", inner);

    IXMLElement_Release(value);
    IXMLElement_Release(child);
    IXMLElementCollection_Release(collection);
    IXMLElement_Release(element);
292
cleanup:
293 294 295 296 297 298 299 300 301 302 303 304 305
    IStream_Release(stream);
    IPersistStreamInit_Release(psi);
    IXMLDocument_Release(doc);

    DeleteFileA("bank.xml");
}

static void test_createElement(void)
{
    HRESULT hr;
    IXMLDocument *doc = NULL;
    IXMLElement *element = NULL, *root = NULL;
    VARIANT vType, vName;
306
    LONG type;
307 308 309

    hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
                          &IID_IXMLDocument, (LPVOID*)&doc);
310
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
311 312 313 314 315 316

    /* invalid vType type */
    V_VT(&vType) = VT_NULL;
    V_VT(&vName) = VT_NULL;
    element = (IXMLElement *)0xdeadbeef;
    hr = IXMLDocument_createElement(doc, vType, vName, &element);
317
    ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
318 319 320 321 322 323 324
    ok(element == NULL, "Expected NULL element\n");

    /* invalid vType value */
    V_VT(&vType) = VT_I4;
    V_I4(&vType) = -1;
    V_VT(&vName) = VT_NULL;
    hr = IXMLDocument_createElement(doc, vType, vName, &element);
325
    /* Up to and including SP7, createElement returns an element. */
326
    if(hr == S_OK)
327
    {
328 329 330 331
        ok(element != NULL, "Expected element\n");
        if (element != NULL)
        {
            hr = IXMLElement_get_type(element, &type);
332
            ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
333 334
            /* SP7 returns an XMLELEMTYPE_ELEMENT */
            ok(type == XMLELEMTYPE_OTHER || type == XMLELEMTYPE_ELEMENT,
335
                         "Expected XMLELEMTYPE_OTHER || XMLELEMTYPE_ELEMENT, got %d\n", type);
336 337 338 339 340 341

            IXMLElement_Release(element);
        }
    }
    else
    {
342
        ok(hr == E_NOTIMPL, "Expected E_NOTIMPL, got %08x\n", hr);
343
        ok(element == NULL, "Expected NULL element\n");
344
    }
345 346 347 348 349 350

    /* invalid vName type */
    V_VT(&vType) = VT_I4;
    V_I4(&vType) = XMLELEMTYPE_ELEMENT;
    V_VT(&vName) = VT_I4;
    hr = IXMLDocument_createElement(doc, vType, vName, &element);
351
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
352 353 354
    ok(element != NULL, "Expected non-NULL element\n");

    hr = IXMLElement_get_type(element, &type);
355
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
356
    ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

    IXMLElement_Release(element);

    /* NULL element */
    V_VT(&vType) = VT_I4;
    V_I4(&vType) = XMLELEMTYPE_ELEMENT;
    V_VT(&vName) = VT_I4;
    hr = IXMLDocument_createElement(doc, vType, vName, NULL);
    ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);

    root = (IXMLElement *)0xdeadbeef;
    hr = IXMLDocument_get_root(doc, &root);
    ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
    ok(root == NULL, "Expected NULL root\n");

    V_VT(&vType) = VT_I4;
    V_I4(&vType) = XMLELEMTYPE_ELEMENT;
    V_VT(&vName) = VT_NULL;
    hr = IXMLDocument_createElement(doc, vType, vName, &element);
376
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
377 378 379 380 381
    ok(element != NULL, "Expected non-NULL element\n");

    /* createElement does not set the new element as root */
    root = (IXMLElement *)0xdeadbeef;
    hr = IXMLDocument_get_root(doc, &root);
382
    ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
383 384 385 386 387 388
    ok(root == NULL, "Expected NULL root\n");

    IXMLElement_Release(element);
    IXMLDocument_Release(doc);
}

389 390 391
static void test_persiststreaminit(void)
{
    IXMLDocument *doc = NULL;
392
    IXMLElement *element = NULL;
393 394 395 396 397 398
    IPersistStreamInit *psi = NULL;
    IStream *stream = NULL;
    STATSTG stat;
    HRESULT hr;
    ULARGE_INTEGER size;
    CHAR path[MAX_PATH];
399
    CLSID id;
400 401
    BSTR str;
    static const WCHAR testW[] = {'t','e','s','t',0};
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420

    hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
                          &IID_IXMLDocument, (LPVOID*)&doc);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

    hr = IXMLDocument_QueryInterface(doc, &IID_IPersistStreamInit, (LPVOID *)&psi);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(psi != NULL, "Expected non-NULL psi\n");

    /* null arguments */
    hr = IPersistStreamInit_GetSizeMax(psi, NULL);
    ok(hr == E_NOTIMPL, "Expected E_NOTIMPL, got %08x\n", hr);

    hr = IPersistStreamInit_Load(psi, NULL);
    ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);

    hr = IPersistStreamInit_Save(psi, NULL, FALSE);
    todo_wine ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);

421
    hr = IPersistStreamInit_GetClassID(psi, NULL);
422 423 424 425
    ok(hr == E_POINTER, "Expected E_POINTER, got %08x\n", hr);

    hr = IPersistStreamInit_IsDirty(psi);
    todo_wine ok(hr == S_FALSE, "Expected S_FALSE, got %08x\n", hr);
426

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    create_xml_file("bank.xml");
    GetFullPathNameA("bank.xml", MAX_PATH, path, NULL);
    create_stream_on_file(&stream, path);

    /* GetSizeMax not implemented */
    size.QuadPart = 0;
    hr = IPersistStreamInit_GetSizeMax(psi, &size);
    ok(hr == E_NOTIMPL, "Expected E_NOTIMPL, got %08x\n", hr);
    ok(size.QuadPart == 0, "Expected 0\n");

    hr = IPersistStreamInit_Load(psi, stream);
    IStream_Release(stream);
    ok(hr == S_OK || hr == XML_E_INVALIDATROOTLEVEL, "Expected S_OK, got %08x\n", hr);
    if(hr == XML_E_INVALIDATROOTLEVEL)
        goto cleanup;

443 444 445
    hr = IPersistStreamInit_IsDirty(psi);
    todo_wine ok(hr == S_FALSE, "Expected S_FALSE, got %08x\n", hr);

446 447 448 449 450 451 452 453 454 455 456 457 458
    /* try to save document */
    stream = NULL;
    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    hr = IPersistStreamInit_Save(psi, stream, FALSE);
    todo_wine ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

    stat.cbSize.QuadPart = 0;
    hr = IStream_Stat(stream, &stat, 0);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    todo_wine ok(stat.cbSize.QuadPart > 0, "Expected >0\n");
    IStream_Release(stream);

459 460 461 462
    str = SysAllocString(testW);
    hr = IXMLDocument_get_root(doc, &element);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    hr = IXMLElement_put_text(element, str);
463
    ok(hr == E_NOTIMPL, "Expected E_NOTIMPL, got %08x\n", hr);
464
    IXMLElement_Release(element);
465 466 467 468 469
    SysFreeString(str);

    hr = IPersistStreamInit_IsDirty(psi);
    todo_wine ok(hr == S_FALSE, "Expected S_FALSE, got %08x\n", hr);

470 471 472 473 474 475 476 477
    create_stream_on_file(&stream, path);
    hr = IPersistStreamInit_Load(psi, stream);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    IStream_Release(stream);

    hr = IPersistStreamInit_IsDirty(psi);
    todo_wine ok(hr == S_FALSE, "Expected S_FALSE, got %08x\n", hr);

478 479
    /* reset internal stream */
    hr = IPersistStreamInit_InitNew(psi);
480 481 482 483
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

    hr = IPersistStreamInit_IsDirty(psi);
    todo_wine ok(hr == S_FALSE, "Expected S_FALSE, got %08x\n", hr);
484 485 486 487 488 489 490 491 492 493 494 495 496 497

    stream = NULL;
    hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

    hr = IPersistStreamInit_Save(psi, stream, FALSE);
    todo_wine ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

    stat.cbSize.QuadPart = 0;
    hr = IStream_Stat(stream, &stat, 0);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    todo_wine ok(stat.cbSize.QuadPart > 0, "Expected >0\n");
    IStream_Release(stream);

498 499 500 501 502
    memset(&id, 0, sizeof(id));
    hr = IPersistStreamInit_GetClassID(psi, &id);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(IsEqualCLSID(&id, &CLSID_XMLDocument), "Expected CLSID_XMLDocument\n");

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
cleanup:
    IPersistStreamInit_Release(psi);
    IXMLDocument_Release(doc);
    DeleteFileA("bank.xml");
}

static BOOL test_try_xmldoc(void)
{
    IXMLDocument *doc = NULL;
    HRESULT hr;

    hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
                          &IID_IXMLDocument, (LPVOID*)&doc);
    if (FAILED(hr))
    {
518
        win_skip("Failed to create XMLDocument instance\n");
519 520 521 522 523 524 525
        return FALSE;
    }

    IXMLDocument_Release(doc);
    return TRUE;
}

526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
static void test_xmlelem_children(void)
{
    IXMLDocument *doc = NULL;
    IXMLElement *element = NULL, *child = NULL, *child2 = NULL;
    IXMLElementCollection *collection = NULL;
    VARIANT vType, vName, vIndex;
    LONG length;
    HRESULT hr;

    hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
                          &IID_IXMLDocument, (LPVOID*)&doc);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

    V_VT(&vType) = VT_I4;
    V_I4(&vType) = XMLELEMTYPE_ELEMENT;
    V_VT(&vName) = VT_NULL;
    hr = IXMLDocument_createElement(doc, vType, vName, &element);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(element != NULL, "Expected non-NULL element\n");

    V_VT(&vType) = VT_I4;
    V_I4(&vType) = XMLELEMTYPE_TEXT;
    V_VT(&vName) = VT_NULL;
    hr = IXMLDocument_createElement(doc, vType, vName, &child);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(child != NULL, "Expected non-NULL child\n");

    V_VT(&vType) = VT_I4;
    V_I4(&vType) = XMLELEMTYPE_TEXT;
    V_VT(&vName) = VT_NULL;
    hr = IXMLDocument_createElement(doc, vType, vName, &child2);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(child2 != NULL, "Expected non-NULL child\n");

    hr = IXMLElement_addChild(element, child, 0, -1);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

    hr = IXMLElement_get_children(element, &collection);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(collection != NULL, "Expected non-NULL collection\n");

    length = 0;
    hr = IXMLElementCollection_get_length(collection, &length);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(length == 1, "Expected 1, got %08x\n", length);

    /* remove/add child and check what happens with collection */
    hr = IXMLElement_removeChild(element, child);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

    length = -1;
    hr = IXMLElementCollection_get_length(collection, &length);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(length == 0, "Expected 0, got %08x\n", length);
    IXMLElementCollection_Release(collection);

    hr = IXMLElement_AddRef(child);
    ok(hr == 2, "Expected 2, got %08x\n", hr);
    IXMLElement_Release(child);
    hr = IXMLElement_addChild(element, child, 0, -1);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    hr = IXMLElement_AddRef(child);
    ok(hr == 2, "Expected 2, got %08x\n", hr);
    IXMLElement_Release(child);
    hr = IXMLElement_addChild(element, child2, 0, -1);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

    hr = IXMLElement_get_children(element, &collection);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(collection != NULL, "Expected non-NULL collection\n");

    hr = IXMLElement_AddRef(child);
    ok(hr == 2, "Expected 2, got %08x\n", hr);
    IXMLElement_Release(child);

    length = 0;
    hr = IXMLElementCollection_get_length(collection, &length);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(length == 2, "Expected 2, got %08x\n", length);

    IXMLElement_Release(child2);

    length = 0;
    hr = IXMLElementCollection_get_length(collection, &length);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(length == 2, "Expected 2, got %08x\n", length);

    V_VT(&vIndex) = VT_I4;
    V_I4(&vIndex) = 1;
    hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child2);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(child2 != NULL, "Expected not NULL child\n");
    IXMLElementCollection_Release(collection);
    IXMLElement_Release(child2);

    /* add element->child->child2 structure, then remove child2 from node */
    V_VT(&vType) = VT_I4;
    V_I4(&vType) = XMLELEMTYPE_TEXT;
    V_VT(&vName) = VT_NULL;
    hr = IXMLDocument_createElement(doc, vType, vName, &child2);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(child2 != NULL, "Expected non-NULL child\n");

    hr = IXMLElement_addChild(child, child2, 0, -1);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

    hr = IXMLElement_removeChild(element, child2);
    ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);

    hr = IXMLElement_removeChild(child, child2);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

    hr = IXMLElement_removeChild(child, NULL);
    ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);

    IXMLElement_Release(element);
    IXMLElement_Release(child);
    IXMLElement_Release(child2);
    IXMLDocument_Release(doc);
}

static void test_xmlelem_collection(void)
{
    HRESULT hr;
    IUnknown *unk = NULL;
    IXMLDocument *doc = NULL;
    IXMLElement *element = NULL, *child;
    IXMLElementCollection *collection = NULL;
    IEnumVARIANT *enumVar = NULL;
    CHAR pathA[MAX_PATH];
    WCHAR path[MAX_PATH];
    LONG length, type;
    ULONG num_vars;
659
    VARIANT var, dummy, vIndex, vName;
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
    BSTR url, str;
    static const CHAR szBankXML[] = "bank.xml";
    static const WCHAR szNumber[] = {'N','U','M','B','E','R',0};
    static const WCHAR szName[] = {'N','A','M','E',0};

    hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
                          &IID_IXMLDocument, (LPVOID*)&doc);
    ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);

    create_xml_file(szBankXML);
    GetFullPathNameA(szBankXML, MAX_PATH, pathA, NULL);
    MultiByteToWideChar(CP_ACP, 0, pathA, -1, path, MAX_PATH);

    url = SysAllocString(path);
    hr = IXMLDocument_put_URL(doc, url);
    ok(hr == S_OK, "Expected S_OK, got 0x%08x\n", hr);
    SysFreeString(url);

    if(hr != S_OK)
        goto cleanup;

    hr = IXMLDocument_get_root(doc, &element);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(element != NULL, "Expected non-NULL element\n");

    hr = IXMLElement_get_children(element, &collection);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(collection != NULL, "Expected non-NULL collection\n");

    hr = IXMLElementCollection_get_length(collection, NULL);
    ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);

    hr = IXMLElementCollection_get_length(collection, &length);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(length == 2, "Expected 2, got %d\n", length);

    /* IXMLElementCollection:put_length does nothing */
    hr = IXMLElementCollection_put_length(collection, -1);
    ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);

    hr = IXMLElementCollection_put_length(collection, 0);
    ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);

    hr = IXMLElementCollection_put_length(collection, 1);
    ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);

    hr = IXMLElementCollection_put_length(collection, 2);
    ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);

    hr = IXMLElementCollection_put_length(collection, 3);
    ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);

    hr = IXMLElementCollection_put_length(collection, 50);
    ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);

    /* make sure the length hasn't changed */
    hr = IXMLElementCollection_get_length(collection, &length);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(length == 2, "Expected 2, got %d\n", length);

    /* IXMLElementCollection implements IEnumVARIANT */
    hr = IXMLElementCollection_QueryInterface(collection, &IID_IEnumVARIANT, (LPVOID *)&enumVar);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(enumVar != NULL, "Expected non-NULL enumVar\n");
    IEnumVARIANT_Release(enumVar);

    hr = IXMLElementCollection_get__newEnum(collection, &unk);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(unk != NULL, "Expected non-NULL unk\n");

730 731 732 733 734
    enumVar = (void *)0xdeadbeef;
    hr = IUnknown_QueryInterface(unk, &IID_IXMLElementCollection, (LPVOID *)&enumVar);
    ok(hr == E_NOINTERFACE, "Expected E_NOINTERFACE, got %08x\n", hr);
    ok(enumVar == NULL || broken(enumVar == (void *)0xdeadbeef) /* XP */, "Expected NULL, got %p\n", enumVar);

735 736 737 738 739 740 741 742 743 744 745
    hr = IUnknown_QueryInterface(unk, &IID_IEnumVARIANT, (LPVOID *)&enumVar);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(enumVar != NULL, "Expected non-NULL enumVar\n");
    IUnknown_Release(unk);

    /* <Number>1234</Number> */
    hr = IEnumVARIANT_Next(enumVar, 1, &var, &num_vars);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(V_VT(&var) == VT_DISPATCH, "Expected VT_DISPATCH, got %d\n", V_VT(&var));
    ok(num_vars == 1, "Expected 1, got %d\n", num_vars);

746
    hr = IDispatch_QueryInterface(V_DISPATCH(&var), &IID_IXMLElement, (LPVOID *)&child);
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(child != NULL, "Expected non-NULL child\n");

    VariantClear(&var);

    hr = IXMLElement_get_type(child, &type);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);

    hr = IXMLElement_get_tagName(child, &str);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(!lstrcmpW(str, szNumber), "Expected NUMBER\n");
    SysFreeString(str);
    IXMLElement_Release(child);

    /* <Name>Captain Ahab</Name> */
    hr = IEnumVARIANT_Next(enumVar, 1, &var, &num_vars);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(V_VT(&var) == VT_DISPATCH, "Expected VT_DISPATCH, got %d\n", V_VT(&var));
    ok(num_vars == 1, "Expected 1, got %d\n", num_vars);

    /* try advance further, no children left */
769 770
    V_VT(&dummy) = VT_I4;
    hr = IEnumVARIANT_Next(enumVar, 1, &dummy, &num_vars);
771
    ok(hr == S_FALSE, "Expected S_FALSE, got %08x\n", hr);
772
    ok(V_VT(&dummy) == VT_EMPTY, "Expected 0, got %d\n", V_VT(&dummy));
773 774
    ok(num_vars == 0, "Expected 0, got %d\n", num_vars);

775 776
    V_VT(&dummy) = VT_I4;
    hr = IEnumVARIANT_Next(enumVar, 1, &dummy, NULL);
777
    ok(hr == S_FALSE, "Expected S_FALSE, got %08x\n", hr);
778
    ok(V_VT(&dummy) == VT_EMPTY, "Expected 0, got %d\n", V_VT(&dummy));
779

780
    hr = IDispatch_QueryInterface(V_DISPATCH(&var), &IID_IXMLElement, (LPVOID *)&child);
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(child != NULL, "Expected non-NULL child\n");

    VariantClear(&var);

    hr = IXMLElement_get_type(child, &type);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);

    hr = IXMLElement_get_tagName(child, &str);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(!lstrcmpW(str, szName), "Expected NAME\n");
    SysFreeString(str);
    IXMLElement_Release(child);

    /* <Number>1234</Number> */
    V_VT(&vIndex) = VT_I4;
    V_I4(&vIndex) = 0;
    V_VT(&vName) = VT_ERROR;
    V_ERROR(&vName) = DISP_E_PARAMNOTFOUND;
    hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(child != NULL, "Expected non-NULL child\n");

    hr = IXMLElement_get_type(child, &type);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);

    hr = IXMLElement_get_tagName(child, &str);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(!lstrcmpW(str, szNumber), "Expected NUMBER\n");
    SysFreeString(str);
    IXMLElement_Release(child);

    /* <Name>Captain Ahab</Name> */
    V_VT(&vIndex) = VT_I4;
    V_I4(&vIndex) = 1;
    V_VT(&vName) = VT_ERROR;
    V_ERROR(&vName) = DISP_E_PARAMNOTFOUND;
    hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(child != NULL, "Expected non-NULL child\n");

    hr = IXMLElement_get_type(child, &type);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);

    hr = IXMLElement_get_tagName(child, &str);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(!lstrcmpW(str, szName), "Expected NAME\n");
    SysFreeString(str);
    IXMLElement_Release(child);

    V_I4(&vIndex) = 100;
    hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
    ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
    ok(child == NULL, "Expected NULL child\n");

    V_I4(&vIndex) = -1;
    child = (IXMLElement *)0xdeadbeef;
    hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
    ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
    ok(child == NULL, "Expected NULL child\n");

    IEnumVARIANT_Release(enumVar);
    IXMLElement_Release(element);
    IXMLElementCollection_Release(collection);
cleanup:
    IXMLDocument_Release(doc);
    DeleteFileA("bank.xml");
}

static void test_xmlelem(void)
{
    HRESULT hr;
    IXMLDocument *doc = NULL;
    IXMLElement *element = NULL, *parent;
    IXMLElement *child, *child2;
    IXMLElementCollection *children;
    VARIANT vType, vName;
    VARIANT vIndex, vValue;
862
    BSTR str, val, name;
863
    LONG type, num_child;
864 865
    IDispatch *disp;
    ITypeInfo *ti;
866 867 868 869 870 871 872 873 874 875

    static const WCHAR propName[] = {'p','r','o','p',0};
    static const WCHAR propVal[] = {'v','a','l',0};
    static const WCHAR nextVal[] = {'n','e','x','t',0};
    static const WCHAR noexist[] = {'n','o','e','x','i','s','t',0};
    static const WCHAR crazyCase1[] = {'C','R','a','z','Y','c','A','S','E',0};
    static const WCHAR crazyCase2[] = {'C','R','A','Z','Y','C','A','S','E',0};

    hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
                          &IID_IXMLDocument, (LPVOID*)&doc);
876
    EXPECT_HR(hr, S_OK);
877 878 879 880 881

    V_VT(&vType) = VT_I4;
    V_I4(&vType) = XMLELEMTYPE_ELEMENT;
    V_VT(&vName) = VT_NULL;
    hr = IXMLDocument_createElement(doc, vType, vName, &element);
882
    EXPECT_HR(hr, S_OK);
883 884
    ok(element != NULL, "Expected non-NULL element\n");

885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
    /* test for IDispatch */
    disp = NULL;
    hr = IXMLElement_QueryInterface(element, &IID_IDispatch, (void**)&disp);
    EXPECT_HR(hr, S_OK);

    hr = IDispatch_GetTypeInfo(disp, 0, 0, &ti);
    EXPECT_HR(hr, S_OK);

    name = NULL;
    hr = ITypeInfo_GetDocumentation(ti, DISPID_XMLELEMENT_TAGNAME, &name, NULL, NULL, NULL);
    EXPECT_HR(hr, S_OK);
    SysFreeString(name);

    ITypeInfo_Release(ti);
    IDispatch_Release(disp);

901
    hr = IXMLElement_get_tagName(element, &str);
902
    EXPECT_HR(hr, S_OK);
903
    ok(!str, "Expected empty tag name, got %s\n", wine_dbgstr_w(str));
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
    SysFreeString(str);

    parent = (IXMLElement *)0xdeadbeef;
    hr = IXMLElement_get_parent(element, &parent);
    ok(hr == 1, "Expected 1, got %08x\n", hr);
    ok(parent == NULL, "Expected NULL parent\n");

    str = SysAllocString(noexist);
    hr = IXMLElement_getAttribute(element, str, &vValue);
    ok(hr == S_FALSE, "Expected S_FALSE, got %08x\n", hr);
    ok(V_VT(&vValue) == VT_EMPTY, "Expected VT_EMPTY, got %d\n", V_VT(&vValue));
    ok(V_BSTR(&vValue) == NULL, "Expected null value\n");
    VariantClear(&vValue);
    SysFreeString(str);

    str = SysAllocString(crazyCase1);
    val = SysAllocString(propVal);
    V_VT(&vValue) = VT_BSTR;
    V_BSTR(&vValue) = val;
    hr = IXMLElement_setAttribute(element, str, vValue);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    SysFreeString(str);
    SysFreeString(val);

    str = SysAllocString(crazyCase2);
    hr = IXMLElement_getAttribute(element, str, &vValue);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(V_VT(&vValue) == VT_BSTR, "Expected VT_BSTR, got %d\n", V_VT(&vValue));
    ok(!lstrcmpW(V_BSTR(&vValue), propVal), "Expected 'val'\n");
    VariantClear(&vValue);
    SysFreeString(str);

    str = SysAllocString(propName);
    val = SysAllocString(propVal);
    V_VT(&vValue) = VT_BSTR;
    V_BSTR(&vValue) = val;
    hr = IXMLElement_setAttribute(element, str, vValue);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    SysFreeString(val);

    hr = IXMLElement_getAttribute(element, str, &vValue);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(V_VT(&vValue) == VT_BSTR, "Expected VT_BSTR, got %d\n", V_VT(&vValue));
    ok(!lstrcmpW(V_BSTR(&vValue), propVal), "Expected 'val'\n");
    VariantClear(&vValue);

    hr = IXMLElement_removeAttribute(element, str);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

    /* remove now nonexistent attribute */
    hr = IXMLElement_removeAttribute(element, str);
    ok(hr == S_FALSE, "Expected S_FALSE, got %08x\n", hr);

    hr = IXMLElement_getAttribute(element, str, &vValue);
    ok(hr == 1, "Expected 1, got %08x\n", hr);
    ok(V_VT(&vValue) == VT_EMPTY, "Expected VT_EMPTY, got %d\n", V_VT(&vValue));
    ok(V_BSTR(&vValue) == NULL, "Expected null value\n");
    SysFreeString(str);
    VariantClear(&vValue);

    children = (IXMLElementCollection *)0xdeadbeef;
    hr = IXMLElement_get_children(element, &children);
    ok(hr == 1, "Expected 1, got %08x\n", hr);
    ok(children == NULL, "Expected NULL collection\n");

    hr = IXMLElement_get_type(element, &type);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);

    hr = IXMLElement_get_text(element, &str);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(lstrlenW(str) == 0, "Expected empty text\n");
    SysFreeString(str);

    /* put_text with an ELEMENT */
    str = SysAllocString(propVal);
    hr = IXMLElement_put_text(element, str);
    ok(hr == E_NOTIMPL, "Expected E_NOTIMPL, got %08x\n", hr);
    SysFreeString(str);

    V_VT(&vType) = VT_I4;
    V_I4(&vType) = XMLELEMTYPE_TEXT;
    V_VT(&vName) = VT_NULL;
    hr = IXMLDocument_createElement(doc, vType, vName, &child);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(child != NULL, "Expected non-NULL child\n");

    hr = IXMLElement_addChild(element, child, 0, -1);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);

    str = SysAllocString(propVal);
    hr = IXMLElement_put_text(child, str);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    SysFreeString(str);

    parent = (IXMLElement *)0xdeadbeef;
    hr = IXMLElement_get_parent(child, &parent);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(parent != element, "Expected parent != element\n");

    hr = IXMLElement_get_type(parent, &type);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);

    children = (IXMLElementCollection *)0xdeadbeef;
    hr = IXMLElement_get_children(element, &children);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(children != NULL, "Expected non-NULL collection\n");

    hr = IXMLElementCollection_get_length(children, &num_child);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(num_child == 1, "Expected 1, got %d\n", num_child);

    V_VT(&vIndex) = VT_I4;
    V_I4(&vIndex) = 0;
    V_VT(&vName) = VT_ERROR;
    V_ERROR(&vName) = DISP_E_PARAMNOTFOUND;
    hr = IXMLElementCollection_item(children, vIndex, vName, (IDispatch **)&child2);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(child2 != NULL, "Expected non-NULL child\n");

    hr = IXMLElement_get_type(child2, &type);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(type == XMLELEMTYPE_TEXT, "Expected XMLELEMTYPE_TEXT, got %d\n", type);

    hr = IXMLElement_get_text(element, &str);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(!lstrcmpW(str, propVal), "Expected 'val'\n");
    SysFreeString(str);

    hr = IXMLElement_get_text(child2, &str);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(!lstrcmpW(str, propVal), "Expected 'val'\n");
    SysFreeString(str);

    /* try put_text on ELEMENT again, now that it has a text child */
    str = SysAllocString(nextVal);
    hr = IXMLElement_put_text(element, str);
    ok(hr == E_NOTIMPL, "Expected E_NOTIMPL, got %08x\n", hr);
    SysFreeString(str);

    str = SysAllocString(nextVal);
    hr = IXMLElement_put_text(child2, str);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    SysFreeString(str);

    hr = IXMLElement_get_text(element, &str);
    ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
    ok(!lstrcmpW(str, nextVal), "Expected 'val'\n");
    SysFreeString(str);

    IXMLElement_Release(child2);
    IXMLElementCollection_Release(children);
    IXMLElement_Release(parent);
    IXMLElement_Release(child);
    IXMLElement_Release(element);
    IXMLDocument_Release(doc);
}

1063 1064 1065 1066 1067 1068 1069
START_TEST(xmldoc)
{
    HRESULT hr;

    hr = CoInitialize(NULL);
    ok(hr == S_OK, "failed to init com\n");

1070 1071 1072 1073 1074 1075
    if (!test_try_xmldoc())
    {
        CoUninitialize();
        return;
    }

1076 1077
    test_xmldoc();
    test_createElement();
1078
    test_persiststreaminit();
1079 1080 1081
    test_xmlelem();
    test_xmlelem_collection();
    test_xmlelem_children();
1082 1083

    CoUninitialize();
1084
}