push 52cba0a224aad01bcbdb26d79e43229ba950650e
[wine/hacks.git] / dlls / d3dxof / tests / d3dxof.c
blobaec29b048b9d5703ce0dc662caa2091281ccfc3c
1 /*
2 * Some unit tests for d3dxof
4 * Copyright (C) 2008 Christian Costa
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 <assert.h>
23 #include <stdio.h>
24 #include "wine/test.h"
25 #include "initguid.h"
26 #include "dxfile.h"
28 static inline void debugstr_guid( char* buf, CONST GUID *id )
30 sprintf(buf, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
31 id->Data1, id->Data2, id->Data3,
32 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
33 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
36 static HMODULE hd3dxof;
37 static HRESULT (WINAPI *pDirectXFileCreate)(LPDIRECTXFILE*);
39 char template[] =
40 "xof 0302txt 0064\n"
41 "template Header\n"
42 "{\n"
43 "<3D82AB43-62DA-11CF-AB39-0020AF71E433>\n"
44 "WORD major;\n"
45 "WORD minor;\n"
46 "DWORD flags;\n"
47 "}\n";
49 char object[] =
50 "xof 0302txt 0064\n"
51 "Header Object\n"
52 "{\n"
53 "1; 2; 3;\n"
54 "}\n";
56 static void init_function_pointers(void)
58 /* We have to use LoadLibrary as no d3dxof functions are referenced directly */
59 hd3dxof = LoadLibraryA("d3dxof.dll");
61 pDirectXFileCreate = (void *)GetProcAddress(hd3dxof, "DirectXFileCreate");
64 static ULONG getRefcount(IUnknown *iface)
66 IUnknown_AddRef(iface);
67 return IUnknown_Release(iface);
70 static void test_refcount(void)
72 HRESULT hr;
73 ULONG ref;
74 LPDIRECTXFILE lpDirectXFile = NULL;
75 LPDIRECTXFILEENUMOBJECT lpdxfeo;
76 LPDIRECTXFILEDATA lpdxfd;
77 DXFILELOADMEMORY dxflm;
79 if (!pDirectXFileCreate)
81 win_skip("DirectXFileCreate is not available\n");
82 return;
85 hr = pDirectXFileCreate(&lpDirectXFile);
86 ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
87 if(!lpDirectXFile)
89 skip("Couldn't create DirectXFile interface\n");
90 return;
93 ref = getRefcount( (IUnknown *) lpDirectXFile);
94 ok(ref == 1, "Got refcount %d, expected 1\n", ref);
95 ref = IDirectXFile_AddRef(lpDirectXFile);
96 ok(ref == 2, "Got refcount %d, expected 1\n", ref);
97 ref = IDirectXFile_Release(lpDirectXFile);
98 ok(ref == 1, "Got refcount %d, expected 1\n", ref);
100 hr = IDirectXFile_RegisterTemplates(lpDirectXFile, template, strlen(template));
101 ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
103 dxflm.lpMemory = &object;
104 dxflm.dSize = strlen(object);
105 hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
106 ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
107 ref = getRefcount( (IUnknown *) lpDirectXFile);
108 ok(ref == 1, "Got refcount %d, expected 1\n", ref);
109 ref = getRefcount( (IUnknown *) lpdxfeo);
110 ok(ref == 1, "Got refcount %d, expected 1\n", ref);
112 hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
113 ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
114 ref = getRefcount( (IUnknown *) lpDirectXFile);
115 ok(ref == 1, "Got refcount %d, expected 1\n", ref);
116 ref = getRefcount( (IUnknown *) lpdxfeo);
117 ok(ref == 1, "Got refcount %d, expected 1\n", ref);
118 /* Enum object gets references to all top level objects */
119 ref = getRefcount( (IUnknown *) lpdxfd);
120 ok(ref == 2, "Got refcount %d, expected 2\n", ref);
122 ref = IDirectXFile_Release(lpDirectXFile);
123 ok(ref == 0, "Got refcount %d, expected 0\n", ref);
124 /* Nothing changes for all other objects */
125 ref = getRefcount( (IUnknown *) lpdxfeo);
126 ok(ref == 1, "Got refcount %d, expected 1\n", ref);
127 ref = getRefcount( (IUnknown *) lpdxfd);
128 ok(ref == 2, "Got refcount %d, expected 1\n", ref);
130 ref = IDirectXFileEnumObject_Release(lpdxfeo);
131 ok(ref == 0, "Got refcount %d, expected 0\n", ref);
132 /* Enum object releases references to all top level objects */
133 ref = getRefcount( (IUnknown *) lpdxfd);
134 ok(ref == 1, "Got refcount %d, expected 1\n", ref);
136 ref = IDirectXFileData_Release(lpdxfd);
137 ok(ref == 0, "Got refcount %d, expected 0\n", ref);
140 /* Set it to 1 to expand the string when dumping the object. This is useful when there is
141 * only one string in a sub-object (very common). Use with care, this may lead to a crash. */
142 #define EXPAND_STRING 0
144 static void process_data(LPDIRECTXFILEDATA lpDirectXFileData, int* plevel)
146 HRESULT hr;
147 char name[100];
148 GUID clsid;
149 CONST GUID* clsid_type = NULL;
150 char str_clsid[40];
151 char str_clsid_type[40];
152 DWORD len= 100;
153 LPDIRECTXFILEOBJECT pChildObj;
154 int i;
155 int j = 0;
156 LPBYTE pData;
157 DWORD k, size;
159 hr = IDirectXFileData_GetId(lpDirectXFileData, &clsid);
160 ok(hr == DXFILE_OK, "IDirectXFileData_GetId: %x\n", hr);
161 hr = IDirectXFileData_GetName(lpDirectXFileData, name, &len);
162 ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
163 hr = IDirectXFileData_GetType(lpDirectXFileData, &clsid_type);
164 ok(hr == DXFILE_OK, "IDirectXFileData_GetType: %x\n", hr);
165 hr = IDirectXFileData_GetData(lpDirectXFileData, NULL, &size, (void**)&pData);
166 ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
167 for (i = 0; i < *plevel; i++)
168 printf(" ");
169 debugstr_guid(str_clsid, &clsid);
170 debugstr_guid(str_clsid_type, clsid_type);
171 printf("Found object '%s' - %s - %s - %d\n", name, str_clsid, str_clsid_type, size);
173 if (EXPAND_STRING && size == 4)
175 char * str = *(char**)pData;
176 printf("string %s\n", str);
178 else if (size)
180 for (k = 0; k < size; k++)
182 if (k && !(k%16))
183 printf("\n");
184 printf("%02x ", pData[k]);
186 printf("\n");
188 (*plevel)++;
189 while (SUCCEEDED(hr = IDirectXFileData_GetNextObject(lpDirectXFileData, &pChildObj)))
191 LPDIRECTXFILEDATA p1;
192 LPDIRECTXFILEDATAREFERENCE p2;
193 LPDIRECTXFILEBINARY p3;
194 j++;
196 hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileData, (void **) &p1);
197 if (SUCCEEDED(hr))
199 for (i = 0; i < *plevel; i++)
200 printf(" ");
201 printf("Found Data (%d)\n", j);
202 process_data(p1, plevel);
203 IDirectXFileData_Release(p1);
205 hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileDataReference, (void **) &p2);
206 if (SUCCEEDED(hr))
208 LPDIRECTXFILEDATA pfdo;
209 for (i = 0; i < *plevel; i++)
210 printf(" ");
211 printf("Found Data Reference (%d)\n", j);
212 #if 0
213 hr = IDirectXFileDataReference_GetId(lpDirectXFileData, &clsid);
214 ok(hr == DXFILE_OK, "IDirectXFileData_GetId: %x\n", hr);
215 hr = IDirectXFileDataReference_GetName(lpDirectXFileData, name, &len);
216 ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
217 #endif
218 IDirectXFileDataReference_Resolve(p2, &pfdo);
219 process_data(pfdo, plevel);
220 IDirectXFileData_Release(pfdo);
221 IDirectXFileDataReference_Release(p2);
223 hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileBinary, (void **) &p3);
224 if (SUCCEEDED(hr))
226 for (i = 0; i < *plevel; i++)
227 printf(" ");
228 printf("Found Binary (%d)\n", j);
229 IDirectXFileBinary_Release(p3);
232 (*plevel)--;
233 ok(hr == DXFILE_OK || hr == DXFILEERR_NOMOREOBJECTS, "IDirectXFileData_GetNextObject: %x\n", hr);
236 static void test_dump(void)
238 HRESULT hr;
239 ULONG ref;
240 LPDIRECTXFILE lpDirectXFile = NULL;
241 LPDIRECTXFILEENUMOBJECT lpDirectXFileEnumObject = NULL;
242 LPDIRECTXFILEDATA lpDirectXFileData = NULL;
243 HANDLE hFile;
244 LPVOID pvData = NULL;
245 DWORD cbSize;
247 if (!pDirectXFileCreate)
249 win_skip("DirectXFileCreate is not available\n");
250 goto exit;
253 /* Dump data only if there is an object and a template */
254 hFile = CreateFileA("objects.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
255 if (hFile == INVALID_HANDLE_VALUE)
256 return;
257 CloseHandle(hFile);
259 hFile = CreateFileA("templates.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
260 if (hFile == INVALID_HANDLE_VALUE)
261 return;
263 pvData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 10000);
265 if (!ReadFile(hFile, pvData, 10000, &cbSize, NULL))
267 skip("Template file is too big\n");
268 goto exit;
271 printf("Load %d bytes\n", cbSize);
273 hr = pDirectXFileCreate(&lpDirectXFile);
274 ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
275 if(!lpDirectXFile)
277 skip("Couldn't create DirectXFile interface\n");
278 goto exit;
281 hr = IDirectXFile_RegisterTemplates(lpDirectXFile, pvData, strlen(pvData));
282 ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
284 hr = IDirectXFile_CreateEnumObject(lpDirectXFile, (LPVOID)"objects.txt", DXFILELOAD_FROMFILE, &lpDirectXFileEnumObject);
285 ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
287 while (SUCCEEDED(hr = IDirectXFileEnumObject_GetNextDataObject(lpDirectXFileEnumObject, &lpDirectXFileData)))
289 int level = 0;
290 printf("\n");
291 process_data(lpDirectXFileData, &level);
292 IDirectXFileData_Release(lpDirectXFileData);
294 ok(hr == DXFILE_OK || hr == DXFILEERR_NOMOREOBJECTS, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
296 ref = IDirectXFile_Release(lpDirectXFileEnumObject);
297 ok(ref == 0, "Got refcount %d, expected 0\n", ref);
299 ref = IDirectXFile_Release(lpDirectXFile);
300 ok(ref == 0, "Got refcount %d, expected 0\n", ref);
302 CloseHandle(hFile);
304 exit:
305 HeapFree(GetProcessHeap(), 0, pvData);
308 START_TEST(d3dxof)
310 init_function_pointers();
312 test_refcount();
313 test_dump();
315 FreeLibrary(hd3dxof);