Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / xmllite / tests / reader.c
blobcb55181b5d68b94aff311ba42b485288facb70e6
1 /*
2 * XMLLite IXmlReader tests
4 * Copyright 2010 (C) Nikolay Sivov
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
21 #define COBJMACROS
23 #include <stdarg.h>
24 #include <stdio.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "initguid.h"
29 #include "ole2.h"
30 #include "xmllite.h"
31 #include "wine/test.h"
33 DEFINE_GUID(IID_IXmlReaderInput, 0x0b3ccc9b, 0x9214, 0x428b, 0xa2, 0xae, 0xef, 0x3a, 0xa8, 0x71, 0xaf, 0xda);
35 HRESULT WINAPI (*pCreateXmlReader)(REFIID riid, void **ppvObject, IMalloc *pMalloc);
36 HRESULT WINAPI (*pCreateXmlReaderInputWithEncodingName)(IUnknown *stream,
37 IMalloc *pMalloc,
38 LPCWSTR encoding,
39 BOOL hint,
40 LPCWSTR base_uri,
41 IXmlReaderInput **ppInput);
42 static const char *debugstr_guid(REFIID riid)
44 static char buf[50];
46 sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
47 riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
48 riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
49 riid->Data4[5], riid->Data4[6], riid->Data4[7]);
51 return buf;
54 typedef struct input_iids_t {
55 IID iids[10];
56 int count;
57 } input_iids_t;
59 static const IID *setinput_full[] = {
60 &IID_IXmlReaderInput,
61 &IID_ISequentialStream,
62 &IID_IStream
65 static input_iids_t input_iids;
67 static void ok_iids_(const input_iids_t *iids, const IID **expected, int size, int todo, int line)
69 int i;
71 if (todo) {
72 todo_wine
73 ok_(__FILE__, line)(iids->count == size, "Sequence size mismatch (%d), got (%d)\n", size, iids->count);
75 else
76 ok_(__FILE__, line)(iids->count == size, "Sequence size mismatch (%d), got (%d)\n", size, iids->count);
78 if (iids->count != size) return;
80 for (i = 0; i < size; i++) {
81 ok_(__FILE__, line)(IsEqualGUID(&iids->iids[i], expected[i]),
82 "Wrong IID(%d), got (%s)\n", i, debugstr_guid(&iids->iids[i]));
85 #define ok_iids(got, exp, size, todo) ok_iids_(got, exp, size, todo, __LINE__)
87 typedef struct _testinput
89 const IUnknownVtbl *lpVtbl;
90 LONG ref;
91 } testinput;
93 static inline testinput *impl_from_IUnknown(IUnknown *iface)
95 return (testinput *)((char*)iface - FIELD_OFFSET(testinput, lpVtbl));
98 static HRESULT WINAPI testinput_QueryInterface(IUnknown *iface, REFIID riid, void** ppvObj)
100 if (IsEqualGUID( riid, &IID_IUnknown ))
102 *ppvObj = iface;
103 IUnknown_AddRef(iface);
104 return S_OK;
107 input_iids.iids[input_iids.count++] = *riid;
109 *ppvObj = NULL;
111 return E_NOINTERFACE;
114 static ULONG WINAPI testinput_AddRef(IUnknown *iface)
116 testinput *This = impl_from_IUnknown(iface);
117 return InterlockedIncrement(&This->ref);
120 static ULONG WINAPI testinput_Release(IUnknown *iface)
122 testinput *This = impl_from_IUnknown(iface);
123 LONG ref;
125 ref = InterlockedDecrement(&This->ref);
126 if (ref == 0)
128 HeapFree(GetProcessHeap(), 0, This);
131 return ref;
134 static const struct IUnknownVtbl testinput_vtbl =
136 testinput_QueryInterface,
137 testinput_AddRef,
138 testinput_Release
141 static HRESULT testinput_createinstance(void **ppObj)
143 testinput *input;
145 input = HeapAlloc(GetProcessHeap(), 0, sizeof (*input));
146 if(!input) return E_OUTOFMEMORY;
148 input->lpVtbl = &testinput_vtbl;
149 input->ref = 1;
151 *ppObj = &input->lpVtbl;
153 return S_OK;
156 static BOOL init_pointers(void)
158 /* don't free module here, it's to be unloaded on exit */
159 HMODULE mod = LoadLibraryA("xmllite.dll");
161 if (!mod)
163 win_skip("xmllite library not available\n");
164 return FALSE;
167 #define MAKEFUNC(f) if (!(p##f = (void*)GetProcAddress(mod, #f))) return FALSE;
168 MAKEFUNC(CreateXmlReader);
169 MAKEFUNC(CreateXmlReaderInputWithEncodingName);
170 #undef MAKEFUNC
172 return TRUE;
175 static void test_reader_create(void)
177 HRESULT hr;
178 IXmlReader *reader;
179 IUnknown *input;
181 /* crashes native */
182 if (0)
184 hr = pCreateXmlReader(&IID_IXmlReader, NULL, NULL);
185 hr = pCreateXmlReader(NULL, (LPVOID*)&reader, NULL);
188 hr = pCreateXmlReader(&IID_IXmlReader, (LPVOID*)&reader, NULL);
189 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
191 /* Null input pointer, releases previous input */
192 hr = IXmlReader_SetInput(reader, NULL);
193 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
195 /* test input interface selection sequence */
196 hr = testinput_createinstance((void**)&input);
197 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
199 input_iids.count = 0;
200 hr = IXmlReader_SetInput(reader, input);
201 ok(hr == E_NOINTERFACE, "Expected E_NOINTERFACE, got %08x\n", hr);
202 ok_iids(&input_iids, setinput_full, sizeof(setinput_full)/sizeof(REFIID), FALSE);
204 IUnknown_Release(input);
206 IXmlReader_Release(reader);
209 static void test_readerinput(void)
211 IXmlReaderInput *reader_input;
212 IUnknown *obj;
213 IStream *stream;
214 HRESULT hr;
215 LONG ref;
217 hr = pCreateXmlReaderInputWithEncodingName(NULL, NULL, NULL, FALSE, NULL, NULL);
218 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
219 hr = pCreateXmlReaderInputWithEncodingName(NULL, NULL, NULL, FALSE, NULL, &reader_input);
220 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
222 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
223 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
225 ref = IStream_AddRef(stream);
226 ok(ref == 2, "Expected 2, got %d\n", ref);
227 IStream_Release(stream);
228 hr = pCreateXmlReaderInputWithEncodingName((IUnknown*)stream, NULL, NULL, FALSE, NULL, &reader_input);
229 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
231 /* IXmlReader grabs a stream reference */
232 ref = IStream_AddRef(stream);
233 todo_wine ok(ref == 3, "Expected 3, got %d\n", ref);
234 IStream_Release(stream);
236 /* IID_IXmlReaderInput */
237 /* it returns a kind of private undocumented vtable incompatible with IUnknown,
238 so it's not a COM interface actually.
239 Such query will be used only to check if input is really IXmlReaderInput */
240 obj = (IUnknown*)0xdeadbeef;
241 hr = IUnknown_QueryInterface(reader_input, &IID_IXmlReaderInput, (void**)&obj);
242 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
243 ref = IUnknown_AddRef(reader_input);
244 ok(ref == 3, "Expected 3, got %d\n", ref);
245 IUnknown_Release(reader_input);
247 IUnknown_Release(reader_input);
248 IStream_Release(stream);
251 START_TEST(reader)
253 HRESULT r;
255 r = CoInitialize( NULL );
256 ok( r == S_OK, "failed to init com\n");
258 if (!init_pointers())
260 CoUninitialize();
261 return;
264 test_reader_create();
265 test_readerinput();
267 CoUninitialize();