d3d9: Implement IDirect3DVertexBuffer9 private data handling on top of wined3d_resource.
[wine] / dlls / gdiplus / tests / metafile.c
1 /*
2  * Unit test suite for metafiles
3  *
4  * Copyright (C) 2011 Vincent Povirk for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "windows.h"
22 #include <stdio.h>
23 #include "gdiplus.h"
24 #include "wine/test.h"
25
26 #define expect(expected, got) ok(got == expected, "Expected %.8x, got %.8x\n", expected, got)
27
28 typedef struct emfplus_record
29 {
30     ULONG todo;
31     ULONG record_type;
32 } emfplus_record;
33
34 typedef struct emfplus_check_state
35 {
36     const char *desc;
37     int count;
38     const struct emfplus_record *expected;
39 } emfplus_check_state;
40
41 static void check_record(int count, const char *desc, const struct emfplus_record *expected, const struct emfplus_record *actual)
42 {
43     if (expected->todo)
44         todo_wine ok(expected->record_type == actual->record_type,
45             "%s.%i: Expected record type 0x%x, got 0x%x\n", desc, count,
46             expected->record_type, actual->record_type);
47     else
48         ok(expected->record_type == actual->record_type,
49             "%s.%i: Expected record type 0x%x, got 0x%x\n", desc, count,
50             expected->record_type, actual->record_type);
51 }
52
53 typedef struct EmfPlusRecordHeader
54 {
55     WORD Type;
56     WORD Flags;
57     DWORD Size;
58     DWORD DataSize;
59 } EmfPlusRecordHeader;
60
61 static int CALLBACK enum_emf_proc(HDC hDC, HANDLETABLE *lpHTable, const ENHMETARECORD *lpEMFR,
62     int nObj, LPARAM lpData)
63 {
64     emfplus_check_state *state = (emfplus_check_state*)lpData;
65     emfplus_record actual;
66
67     if (lpEMFR->iType == EMR_GDICOMMENT)
68     {
69         const EMRGDICOMMENT *comment = (const EMRGDICOMMENT*)lpEMFR;
70
71         if (comment->cbData >= 4 && memcmp(comment->Data, "EMF+", 4) == 0)
72         {
73             int offset = 4;
74
75             while (offset + sizeof(EmfPlusRecordHeader) <= comment->cbData)
76             {
77                 const EmfPlusRecordHeader *record = (const EmfPlusRecordHeader*)&comment->Data[offset];
78
79                 ok(record->Size == record->DataSize + sizeof(EmfPlusRecordHeader),
80                     "%s: EMF+ record datasize %u and size %u mismatch\n", state->desc, record->DataSize, record->Size);
81
82                 ok(offset + record->DataSize <= comment->cbData,
83                     "%s: EMF+ record truncated\n", state->desc);
84
85                 if (offset + record->DataSize > comment->cbData)
86                     return 0;
87
88                 if (state->expected[state->count].record_type)
89                 {
90                     actual.todo = 0;
91                     actual.record_type = record->Type;
92
93                     check_record(state->count, state->desc, &state->expected[state->count], &actual);
94
95                     state->count++;
96                 }
97                 else
98                 {
99                     ok(0, "%s: Unexpected EMF+ 0x%x record\n", state->desc, record->Type);
100                 }
101
102                 offset += record->Size;
103             }
104
105             ok(offset == comment->cbData, "%s: truncated EMF+ record data?\n", state->desc);
106
107             return 1;
108         }
109     }
110
111     if (state->expected[state->count].record_type)
112     {
113         actual.todo = 0;
114         actual.record_type = lpEMFR->iType;
115
116         check_record(state->count, state->desc, &state->expected[state->count], &actual);
117
118         state->count++;
119     }
120     else
121     {
122         ok(0, "%s: Unexpected EMF 0x%x record\n", state->desc, lpEMFR->iType);
123     }
124
125     return 1;
126 }
127
128 static void check_emfplus(HENHMETAFILE hemf, const emfplus_record *expected, const char *desc)
129 {
130     emfplus_check_state state;
131
132     state.desc = desc;
133     state.count = 0;
134     state.expected = expected;
135
136     EnumEnhMetaFile(0, hemf, enum_emf_proc, &state, NULL);
137
138     if (expected[state.count].todo)
139         todo_wine ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
140     else
141         ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
142 }
143
144 static BOOL CALLBACK enum_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
145     unsigned int dataSize, const unsigned char *pStr, void *userdata)
146 {
147     emfplus_check_state *state = (emfplus_check_state*)userdata;
148     emfplus_record actual;
149
150     actual.todo = 0;
151     actual.record_type = record_type;
152
153     if (dataSize == 0)
154         ok(pStr == NULL, "non-NULL pStr\n");
155
156     if (state->expected[state->count].record_type)
157     {
158         check_record(state->count, state->desc, &state->expected[state->count], &actual);
159
160         state->count++;
161     }
162     else
163     {
164         ok(0, "%s: Unexpected EMF 0x%x record\n", state->desc, record_type);
165     }
166
167     return TRUE;
168 }
169
170 static void check_metafile(GpMetafile *metafile, const emfplus_record *expected, const char *desc,
171     const GpPointF *dst_points, const GpRectF *src_rect, Unit src_unit)
172 {
173     GpStatus stat;
174     HDC hdc;
175     GpGraphics *graphics;
176     emfplus_check_state state;
177
178     state.desc = desc;
179     state.count = 0;
180     state.expected = expected;
181
182     hdc = CreateCompatibleDC(0);
183
184     stat = GdipCreateFromHDC(hdc, &graphics);
185     expect(Ok, stat);
186
187     stat = GdipEnumerateMetafileSrcRectDestPoints(graphics, metafile, dst_points,
188         3, src_rect, src_unit, enum_metafile_proc, &state, NULL);
189     expect(Ok, stat);
190
191     if (expected[state.count].todo)
192         todo_wine ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
193     else
194         ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
195
196     GdipDeleteGraphics(graphics);
197
198     DeleteDC(hdc);
199 }
200
201 static const emfplus_record empty_records[] = {
202     {0, EMR_HEADER},
203     {0, EmfPlusRecordTypeHeader},
204     {0, EmfPlusRecordTypeEndOfFile},
205     {0, EMR_EOF},
206     {0}
207 };
208
209 static void test_empty(void)
210 {
211     GpStatus stat;
212     GpMetafile *metafile;
213     GpGraphics *graphics;
214     HDC hdc;
215     HENHMETAFILE hemf, dummy;
216     BOOL ret;
217     static const GpRectF frame = {0.0, 0.0, 100.0, 100.0};
218     static const GpPointF dst_points[3] = {{0.0,0.0},{100.0,0.0},{0.0,100.0}};
219     static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
220
221     hdc = CreateCompatibleDC(0);
222
223     stat = GdipRecordMetafile(NULL, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
224     expect(InvalidParameter, stat);
225
226     stat = GdipRecordMetafile(hdc, MetafileTypeInvalid, &frame, MetafileFrameUnitPixel, description, &metafile);
227     expect(InvalidParameter, stat);
228
229     stat = GdipRecordMetafile(hdc, MetafileTypeWmf, &frame, MetafileFrameUnitPixel, description, &metafile);
230     expect(InvalidParameter, stat);
231
232     stat = GdipRecordMetafile(hdc, MetafileTypeWmfPlaceable, &frame, MetafileFrameUnitPixel, description, &metafile);
233     expect(InvalidParameter, stat);
234
235     stat = GdipRecordMetafile(hdc, MetafileTypeEmfPlusDual+1, &frame, MetafileFrameUnitPixel, description, &metafile);
236     expect(InvalidParameter, stat);
237
238     stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, NULL);
239     expect(InvalidParameter, stat);
240
241     stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
242     expect(Ok, stat);
243
244     DeleteDC(hdc);
245
246     if (stat != Ok)
247         return;
248
249     stat = GdipGetHemfFromMetafile(metafile, &hemf);
250     expect(InvalidParameter, stat);
251
252     stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
253     expect(Ok, stat);
254
255     stat = GdipGetHemfFromMetafile(metafile, &hemf);
256     expect(InvalidParameter, stat);
257
258     stat = GdipDeleteGraphics(graphics);
259     expect(Ok, stat);
260
261     check_metafile(metafile, empty_records, "empty metafile", dst_points, &frame, UnitPixel);
262
263     stat = GdipGetHemfFromMetafile(metafile, &hemf);
264     expect(Ok, stat);
265
266     stat = GdipGetHemfFromMetafile(metafile, &dummy);
267     expect(InvalidParameter, stat);
268
269     stat = GdipDisposeImage((GpImage*)metafile);
270     expect(Ok, stat);
271
272     check_emfplus(hemf, empty_records, "empty emf");
273
274     ret = DeleteEnhMetaFile(hemf);
275     ok(ret != 0, "Failed to delete enhmetafile %p\n", hemf);
276 }
277
278 static const emfplus_record getdc_records[] = {
279     {0, EMR_HEADER},
280     {0, EmfPlusRecordTypeHeader},
281     {1, EmfPlusRecordTypeGetDC},
282     {1, EMR_CREATEBRUSHINDIRECT},
283     {1, EMR_SELECTOBJECT},
284     {0, EMR_RECTANGLE},
285     {0, EMR_SELECTOBJECT},
286     {0, EMR_DELETEOBJECT},
287     {0, EmfPlusRecordTypeEndOfFile},
288     {0, EMR_EOF},
289     {0}
290 };
291
292 static void test_getdc(void)
293 {
294     GpStatus stat;
295     GpMetafile *metafile;
296     GpGraphics *graphics;
297     HDC hdc, metafile_dc;
298     HENHMETAFILE hemf;
299     BOOL ret;
300     static const GpRectF frame = {0.0, 0.0, 100.0, 100.0};
301     static const GpPointF dst_points[3] = {{0.0,0.0},{100.0,0.0},{0.0,100.0}};
302     static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
303     HBRUSH hbrush, holdbrush;
304
305     hdc = CreateCompatibleDC(0);
306
307     stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
308     expect(Ok, stat);
309
310     DeleteDC(hdc);
311
312     if (stat != Ok)
313         return;
314
315     stat = GdipGetHemfFromMetafile(metafile, &hemf);
316     expect(InvalidParameter, stat);
317
318     stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
319     expect(Ok, stat);
320
321     stat = GdipGetDC(graphics, &metafile_dc);
322     expect(Ok, stat);
323
324     if (stat != Ok)
325     {
326         GdipDeleteGraphics(graphics);
327         GdipDisposeImage((GpImage*)metafile);
328         return;
329     }
330
331     hbrush = CreateSolidBrush(0xff0000);
332
333     holdbrush = SelectObject(metafile_dc, hbrush);
334
335     Rectangle(metafile_dc, 25, 25, 75, 75);
336
337     SelectObject(metafile_dc, holdbrush);
338
339     DeleteObject(hbrush);
340
341     stat = GdipReleaseDC(graphics, metafile_dc);
342     expect(Ok, stat);
343
344     stat = GdipDeleteGraphics(graphics);
345     expect(Ok, stat);
346
347     check_metafile(metafile, getdc_records, "getdc metafile", dst_points, &frame, UnitPixel);
348
349     stat = GdipGetHemfFromMetafile(metafile, &hemf);
350     expect(Ok, stat);
351
352     stat = GdipDisposeImage((GpImage*)metafile);
353     expect(Ok, stat);
354
355     check_emfplus(hemf, getdc_records, "getdc emf");
356
357     ret = DeleteEnhMetaFile(hemf);
358     ok(ret != 0, "Failed to delete enhmetafile %p\n", hemf);
359 }
360
361 START_TEST(metafile)
362 {
363     struct GdiplusStartupInput gdiplusStartupInput;
364     ULONG_PTR gdiplusToken;
365
366     gdiplusStartupInput.GdiplusVersion              = 1;
367     gdiplusStartupInput.DebugEventCallback          = NULL;
368     gdiplusStartupInput.SuppressBackgroundThread    = 0;
369     gdiplusStartupInput.SuppressExternalCodecs      = 0;
370
371     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
372
373     test_empty();
374     test_getdc();
375
376     GdiplusShutdown(gdiplusToken);
377 }