4 * Copyright 2008 Piotr Caban
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
30 #include "wine/test.h"
34 CH_PUTDOCUMENTLOCATOR,
37 CH_STARTPREFIXMAPPING,
42 CH_IGNORABLEWHITESPACE,
43 CH_PROCESSINGINSTRUCTION,
47 static const WCHAR szSimpleXML[] = {
48 '<','?','x','m','l',' ','v','e','r','s','i','o','n','=','\"','1','.','0','\"',' ','?','>','\n',
49 '<','B','a','n','k','A','c','c','o','u','n','t','>','\n',
50 ' ',' ',' ','<','N','u','m','b','e','r','>','1','2','3','4','<','/','N','u','m','b','e','r','>','\n',
51 ' ',' ',' ','<','N','a','m','e','>','C','a','p','t','a','i','n',' ','A','h','a','b','<','/','N','a','m','e','>','\n',
52 '<','/','B','a','n','k','A','c','c','o','u','n','t','>','\n','\0'
55 static CHAR szTestXML[] =
56 "<?xml version=\"1.0\" ?>\n"
58 " <Number>1234</Number>\n"
59 " <Name>Captain Ahab</Name>\n"
62 typedef struct _contenthandlercheck {
69 } content_handler_test;
71 static content_handler_test contentHandlerTest1[] = {
72 { CH_PUTDOCUMENTLOCATOR, 0, 0 },
73 { CH_STARTDOCUMENT, 0, 0 },
74 { CH_STARTELEMENT, 2, 14, "", "BankAccount", "BankAccount" },
75 { CH_CHARACTERS, 2, 14, "\n " },
76 { CH_STARTELEMENT, 3, 12, "", "Number", "Number" },
77 { CH_CHARACTERS, 3, 12, "1234" },
78 { CH_ENDELEMENT, 3, 18, "", "Number", "Number" },
79 { CH_CHARACTERS, 3, 25, "\n " },
80 { CH_STARTELEMENT, 4, 10, "", "Name", "Name" },
81 { CH_CHARACTERS, 4, 10, "Captain Ahab" },
82 { CH_ENDELEMENT, 4, 24, "", "Name", "Name" },
83 { CH_CHARACTERS, 4, 29, "\n" },
84 { CH_ENDELEMENT, 5, 3, "", "BankAccount", "BankAccount" },
85 { CH_ENDDOCUMENT, 0, 0 },
89 static content_handler_test *expectCall;
90 static ISAXLocator *locator;
92 static const char *debugstr_wn(const WCHAR *szStr, int len)
94 static char buf[1024];
95 WideCharToMultiByte(CP_ACP, 0, szStr, len, buf, sizeof(buf), NULL, NULL);
99 static void test_saxstr(unsigned line, const WCHAR *szStr, int nStr, const char *szTest)
105 ok_(__FILE__,line) (szStr == NULL, "szStr != NULL\n");
106 ok_(__FILE__,line) (nStr == 0, "nStr = %d, expected 0\n", nStr);
110 len = strlen(szTest);
111 ok_(__FILE__,line) (len == nStr, "nStr = %d, expected %d (%s)\n", nStr, len, szTest);
115 MultiByteToWideChar(CP_ACP, 0, szTest, -1, buf, sizeof(buf));
116 ok_(__FILE__,line) (!memcmp(szStr, buf, len*sizeof(WCHAR)), "unexpected szStr %s, expected %s\n",
117 debugstr_wn(szStr, nStr), szTest);
120 static BOOL test_expect_call(CH id)
122 ok(expectCall->id == id, "unexpected call %d, expected %d\n", id, expectCall->id);
123 return expectCall->id == id;
126 static void test_locator(unsigned line, int loc_line, int loc_column)
129 ISAXLocator_getLineNumber(locator, &rline);
130 ISAXLocator_getColumnNumber(locator, &rcolumn);
132 ok_(__FILE__,line) (rline == loc_line,
133 "unexpected line %d, expected %d\n", rline, loc_line);
134 ok_(__FILE__,line) (rcolumn == loc_column,
135 "unexpected column %d, expected %d\n", rcolumn, loc_column);
138 static HRESULT WINAPI contentHandler_QueryInterface(
139 ISAXContentHandler* iface,
145 if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ISAXContentHandler))
151 return E_NOINTERFACE;
157 static ULONG WINAPI contentHandler_AddRef(
158 ISAXContentHandler* iface)
163 static ULONG WINAPI contentHandler_Release(
164 ISAXContentHandler* iface)
169 static HRESULT WINAPI contentHandler_putDocumentLocator(
170 ISAXContentHandler* iface,
171 ISAXLocator *pLocator)
173 if(!test_expect_call(CH_PUTDOCUMENTLOCATOR))
177 test_locator(__LINE__, expectCall->line, expectCall->column);
183 static HRESULT WINAPI contentHandler_startDocument(
184 ISAXContentHandler* iface)
186 if(!test_expect_call(CH_STARTDOCUMENT))
189 test_locator(__LINE__, expectCall->line, expectCall->column);
195 static HRESULT WINAPI contentHandler_endDocument(
196 ISAXContentHandler* iface)
198 if(!test_expect_call(CH_ENDDOCUMENT))
201 test_locator(__LINE__, expectCall->line, expectCall->column);
207 static HRESULT WINAPI contentHandler_startPrefixMapping(
208 ISAXContentHandler* iface,
209 const WCHAR *pPrefix,
214 if(!test_expect_call(CH_ENDDOCUMENT))
217 test_saxstr(__LINE__, pPrefix, nPrefix, expectCall->arg1);
218 test_saxstr(__LINE__, pUri, nUri, expectCall->arg2);
219 test_locator(__LINE__, expectCall->line, expectCall->column);
225 static HRESULT WINAPI contentHandler_endPrefixMapping(
226 ISAXContentHandler* iface,
227 const WCHAR *pPrefix,
230 if(!test_expect_call(CH_ENDPREFIXMAPPING))
233 test_saxstr(__LINE__, pPrefix, nPrefix, expectCall->arg1);
234 test_locator(__LINE__, expectCall->line, expectCall->column);
240 static HRESULT WINAPI contentHandler_startElement(
241 ISAXContentHandler* iface,
242 const WCHAR *pNamespaceUri,
244 const WCHAR *pLocalName,
248 ISAXAttributes *pAttr)
250 if(!test_expect_call(CH_STARTELEMENT))
253 test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, expectCall->arg1);
254 test_saxstr(__LINE__, pLocalName, nLocalName, expectCall->arg2);
255 test_saxstr(__LINE__, pQName, nQName, expectCall->arg3);
256 test_locator(__LINE__, expectCall->line, expectCall->column);
262 static HRESULT WINAPI contentHandler_endElement(
263 ISAXContentHandler* iface,
264 const WCHAR *pNamespaceUri,
266 const WCHAR *pLocalName,
271 if(!test_expect_call(CH_ENDELEMENT))
274 test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, expectCall->arg1);
275 test_saxstr(__LINE__, pLocalName, nLocalName, expectCall->arg2);
276 test_saxstr(__LINE__, pQName, nQName, expectCall->arg3);
277 test_locator(__LINE__, expectCall->line, expectCall->column);
283 static HRESULT WINAPI contentHandler_characters(
284 ISAXContentHandler* iface,
288 if(!test_expect_call(CH_CHARACTERS))
291 test_saxstr(__LINE__, pChars, nChars, expectCall->arg1);
292 test_locator(__LINE__, expectCall->line, expectCall->column);
298 static HRESULT WINAPI contentHandler_ignorableWhitespace(
299 ISAXContentHandler* iface,
303 if(!test_expect_call(CH_IGNORABLEWHITESPACE))
306 test_saxstr(__LINE__, pChars, nChars, expectCall->arg1);
307 test_locator(__LINE__, expectCall->line, expectCall->column);
313 static HRESULT WINAPI contentHandler_processingInstruction(
314 ISAXContentHandler* iface,
315 const WCHAR *pTarget,
320 if(!test_expect_call(CH_PROCESSINGINSTRUCTION))
323 test_saxstr(__LINE__, pTarget, nTarget, expectCall->arg1);
324 test_saxstr(__LINE__, pData, nData, expectCall->arg2);
325 test_locator(__LINE__, expectCall->line, expectCall->column);
331 static HRESULT WINAPI contentHandler_skippedEntity(
332 ISAXContentHandler* iface,
336 if(!test_expect_call(CH_SKIPPEDENTITY))
339 test_saxstr(__LINE__, pName, nName, expectCall->arg1);
340 test_locator(__LINE__, expectCall->line, expectCall->column);
347 static const ISAXContentHandlerVtbl contentHandlerVtbl =
349 contentHandler_QueryInterface,
350 contentHandler_AddRef,
351 contentHandler_Release,
352 contentHandler_putDocumentLocator,
353 contentHandler_startDocument,
354 contentHandler_endDocument,
355 contentHandler_startPrefixMapping,
356 contentHandler_endPrefixMapping,
357 contentHandler_startElement,
358 contentHandler_endElement,
359 contentHandler_characters,
360 contentHandler_ignorableWhitespace,
361 contentHandler_processingInstruction,
362 contentHandler_skippedEntity
365 static ISAXContentHandler contentHandler = { &contentHandlerVtbl };
367 static HRESULT WINAPI isaxerrorHandler_QueryInterface(
368 ISAXErrorHandler* iface,
374 if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ISAXErrorHandler))
380 return E_NOINTERFACE;
386 static ULONG WINAPI isaxerrorHandler_AddRef(
387 ISAXErrorHandler* iface)
392 static ULONG WINAPI isaxerrorHandler_Release(
393 ISAXErrorHandler* iface)
398 static HRESULT WINAPI isaxerrorHandler_error(
399 ISAXErrorHandler* iface,
400 ISAXLocator *pLocator,
401 const WCHAR *pErrorMessage,
407 static HRESULT WINAPI isaxerrorHandler_fatalError(
408 ISAXErrorHandler* iface,
409 ISAXLocator *pLocator,
410 const WCHAR *pErrorMessage,
416 static HRESULT WINAPI isaxerrorHanddler_ignorableWarning(
417 ISAXErrorHandler* iface,
418 ISAXLocator *pLocator,
419 const WCHAR *pErrorMessage,
425 static const ISAXErrorHandlerVtbl errorHandlerVtbl =
427 isaxerrorHandler_QueryInterface,
428 isaxerrorHandler_AddRef,
429 isaxerrorHandler_Release,
430 isaxerrorHandler_error,
431 isaxerrorHandler_fatalError,
432 isaxerrorHanddler_ignorableWarning
435 static ISAXErrorHandler errorHandler = { &errorHandlerVtbl };
437 static void test_saxreader(void)
440 ISAXXMLReader *reader = NULL;
442 ISAXContentHandler *lpContentHandler;
443 ISAXErrorHandler *lpErrorHandler;
445 SAFEARRAYBOUND SADim[1];
446 char *pSAData = NULL;
448 hr = CoCreateInstance(&CLSID_SAXXMLReader, NULL, CLSCTX_INPROC_SERVER,
449 &IID_ISAXXMLReader, (LPVOID*)&reader);
453 skip("Failed to create SAXXMLReader instance\n");
457 hr = ISAXXMLReader_getContentHandler(reader, NULL);
458 ok(hr == E_POINTER, "Expected E_POINTER, got %08x\n", hr);
460 hr = ISAXXMLReader_getErrorHandler(reader, NULL);
461 ok(hr == E_POINTER, "Expected E_POINTER, got %08x\n", hr);
463 hr = ISAXXMLReader_getContentHandler(reader, &lpContentHandler);
464 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
465 ok(lpContentHandler == NULL, "Expected %p, got %p\n", NULL, lpContentHandler);
467 hr = ISAXXMLReader_getErrorHandler(reader, &lpErrorHandler);
468 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
469 ok(lpErrorHandler == NULL, "Expected %p, got %p\n", NULL, lpErrorHandler);
471 hr = ISAXXMLReader_putContentHandler(reader, NULL);
472 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
474 hr = ISAXXMLReader_putContentHandler(reader, &contentHandler);
475 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
477 hr = ISAXXMLReader_putErrorHandler(reader, &errorHandler);
478 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
480 hr = ISAXXMLReader_getContentHandler(reader, &lpContentHandler);
481 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
482 ok(lpContentHandler == &contentHandler, "Expected %p, got %p\n", &contentHandler, lpContentHandler);
484 V_VT(&var) = VT_BSTR;
485 V_BSTR(&var) = SysAllocString(szSimpleXML);
487 expectCall = contentHandlerTest1;
488 hr = ISAXXMLReader_parse(reader, var);
489 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
490 test_expect_call(CH_ENDTEST);
493 SADim[0].cElements= sizeof(szTestXML)-1;
494 pSA = SafeArrayCreate(VT_UI1, 1, SADim);
495 SafeArrayAccessData(pSA, (void**)&pSAData);
496 memcpy(pSAData, szTestXML, sizeof(szTestXML)-1);
497 SafeArrayUnaccessData(pSA);
498 V_VT(&var) = VT_ARRAY|VT_UI1;
501 expectCall = contentHandlerTest1;
502 hr = ISAXXMLReader_parse(reader, var);
503 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
504 test_expect_call(CH_ENDTEST);
506 SafeArrayDestroy(pSA);
508 ISAXXMLReader_Release(reader);
511 START_TEST(saxreader)
515 hr = CoInitialize(NULL);
516 ok(hr == S_OK, "failed to init com\n");