mshtml: Wine Gecko 1.9 release.
[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     ULONG playback_todo;
33 } emfplus_record;
34
35 typedef struct emfplus_check_state
36 {
37     const char *desc;
38     int count;
39     const struct emfplus_record *expected;
40     GpMetafile *metafile;
41 } emfplus_check_state;
42
43 static void check_record(int count, const char *desc, const struct emfplus_record *expected, const struct emfplus_record *actual)
44 {
45     if (expected->todo)
46         todo_wine ok(expected->record_type == actual->record_type,
47             "%s.%i: Expected record type 0x%x, got 0x%x\n", desc, count,
48             expected->record_type, actual->record_type);
49     else
50         ok(expected->record_type == actual->record_type,
51             "%s.%i: Expected record type 0x%x, got 0x%x\n", desc, count,
52             expected->record_type, actual->record_type);
53 }
54
55 typedef struct EmfPlusRecordHeader
56 {
57     WORD Type;
58     WORD Flags;
59     DWORD Size;
60     DWORD DataSize;
61 } EmfPlusRecordHeader;
62
63 static int CALLBACK enum_emf_proc(HDC hDC, HANDLETABLE *lpHTable, const ENHMETARECORD *lpEMFR,
64     int nObj, LPARAM lpData)
65 {
66     emfplus_check_state *state = (emfplus_check_state*)lpData;
67     emfplus_record actual;
68
69     if (lpEMFR->iType == EMR_GDICOMMENT)
70     {
71         const EMRGDICOMMENT *comment = (const EMRGDICOMMENT*)lpEMFR;
72
73         if (comment->cbData >= 4 && memcmp(comment->Data, "EMF+", 4) == 0)
74         {
75             int offset = 4;
76
77             while (offset + sizeof(EmfPlusRecordHeader) <= comment->cbData)
78             {
79                 const EmfPlusRecordHeader *record = (const EmfPlusRecordHeader*)&comment->Data[offset];
80
81                 ok(record->Size == record->DataSize + sizeof(EmfPlusRecordHeader),
82                     "%s: EMF+ record datasize %u and size %u mismatch\n", state->desc, record->DataSize, record->Size);
83
84                 ok(offset + record->DataSize <= comment->cbData,
85                     "%s: EMF+ record truncated\n", state->desc);
86
87                 if (offset + record->DataSize > comment->cbData)
88                     return 0;
89
90                 if (state->expected[state->count].record_type)
91                 {
92                     actual.todo = 0;
93                     actual.record_type = record->Type;
94
95                     check_record(state->count, state->desc, &state->expected[state->count], &actual);
96
97                     state->count++;
98                 }
99                 else
100                 {
101                     ok(0, "%s: Unexpected EMF+ 0x%x record\n", state->desc, record->Type);
102                 }
103
104                 offset += record->Size;
105             }
106
107             ok(offset == comment->cbData, "%s: truncated EMF+ record data?\n", state->desc);
108
109             return 1;
110         }
111     }
112
113     if (state->expected[state->count].record_type)
114     {
115         actual.todo = 0;
116         actual.record_type = lpEMFR->iType;
117
118         check_record(state->count, state->desc, &state->expected[state->count], &actual);
119
120         state->count++;
121     }
122     else
123     {
124         ok(0, "%s: Unexpected EMF 0x%x record\n", state->desc, lpEMFR->iType);
125     }
126
127     return 1;
128 }
129
130 static void check_emfplus(HENHMETAFILE hemf, const emfplus_record *expected, const char *desc)
131 {
132     emfplus_check_state state;
133
134     state.desc = desc;
135     state.count = 0;
136     state.expected = expected;
137
138     EnumEnhMetaFile(0, hemf, enum_emf_proc, &state, NULL);
139
140     if (expected[state.count].todo)
141         todo_wine ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
142     else
143         ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
144 }
145
146 static BOOL CALLBACK enum_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
147     unsigned int dataSize, const unsigned char *pStr, void *userdata)
148 {
149     emfplus_check_state *state = (emfplus_check_state*)userdata;
150     emfplus_record actual;
151
152     actual.todo = 0;
153     actual.record_type = record_type;
154
155     if (dataSize == 0)
156         ok(pStr == NULL, "non-NULL pStr\n");
157
158     if (state->expected[state->count].record_type)
159     {
160         check_record(state->count, state->desc, &state->expected[state->count], &actual);
161
162         state->count++;
163     }
164     else
165     {
166         ok(0, "%s: Unexpected EMF 0x%x record\n", state->desc, record_type);
167     }
168
169     return TRUE;
170 }
171
172 static void check_metafile(GpMetafile *metafile, const emfplus_record *expected, const char *desc,
173     const GpPointF *dst_points, const GpRectF *src_rect, Unit src_unit)
174 {
175     GpStatus stat;
176     HDC hdc;
177     GpGraphics *graphics;
178     emfplus_check_state state;
179
180     state.desc = desc;
181     state.count = 0;
182     state.expected = expected;
183     state.metafile = metafile;
184
185     hdc = CreateCompatibleDC(0);
186
187     stat = GdipCreateFromHDC(hdc, &graphics);
188     expect(Ok, stat);
189
190     stat = GdipEnumerateMetafileSrcRectDestPoints(graphics, metafile, dst_points,
191         3, src_rect, src_unit, enum_metafile_proc, &state, NULL);
192     expect(Ok, stat);
193
194     if (expected[state.count].todo)
195         todo_wine ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
196     else
197         ok(expected[state.count].record_type == 0, "%s: Got %i records, expecting more\n", desc, state.count);
198
199     GdipDeleteGraphics(graphics);
200
201     DeleteDC(hdc);
202 }
203
204 static BOOL CALLBACK play_metafile_proc(EmfPlusRecordType record_type, unsigned int flags,
205     unsigned int dataSize, const unsigned char *pStr, void *userdata)
206 {
207     emfplus_check_state *state = (emfplus_check_state*)userdata;
208     GpStatus stat;
209
210     stat = GdipPlayMetafileRecord(state->metafile, record_type, flags, dataSize, pStr);
211
212     if (state->expected[state->count].record_type)
213     {
214         if (state->expected[state->count].playback_todo)
215             todo_wine ok(stat == Ok, "%s.%i: GdipPlayMetafileRecord failed with stat %i\n", state->desc, state->count, stat);
216         else
217             ok(stat == Ok, "%s.%i: GdipPlayMetafileRecord failed with stat %i\n", state->desc, state->count, stat);
218         state->count++;
219     }
220     else
221     {
222         if (state->expected[state->count].playback_todo)
223             todo_wine ok(0, "%s: too many records\n", state->desc);
224         else
225             ok(0, "%s: too many records\n", state->desc);
226
227         return FALSE;
228     }
229
230     return TRUE;
231 }
232
233 static void play_metafile(GpMetafile *metafile, GpGraphics *graphics, const emfplus_record *expected,
234     const char *desc, const GpPointF *dst_points, const GpRectF *src_rect, Unit src_unit)
235 {
236     GpStatus stat;
237     emfplus_check_state state;
238
239     state.desc = desc;
240     state.count = 0;
241     state.expected = expected;
242     state.metafile = metafile;
243
244     stat = GdipEnumerateMetafileSrcRectDestPoints(graphics, metafile, dst_points,
245         3, src_rect, src_unit, play_metafile_proc, &state, NULL);
246     expect(Ok, stat);
247 }
248
249 static const emfplus_record empty_records[] = {
250     {0, EMR_HEADER},
251     {0, EmfPlusRecordTypeHeader},
252     {0, EmfPlusRecordTypeEndOfFile},
253     {0, EMR_EOF},
254     {0}
255 };
256
257 static void test_empty(void)
258 {
259     GpStatus stat;
260     GpMetafile *metafile;
261     GpGraphics *graphics;
262     HDC hdc;
263     HENHMETAFILE hemf, dummy;
264     BOOL ret;
265     static const GpRectF frame = {0.0, 0.0, 100.0, 100.0};
266     static const GpPointF dst_points[3] = {{0.0,0.0},{100.0,0.0},{0.0,100.0}};
267     static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
268
269     hdc = CreateCompatibleDC(0);
270
271     stat = GdipRecordMetafile(NULL, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
272     expect(InvalidParameter, stat);
273
274     stat = GdipRecordMetafile(hdc, MetafileTypeInvalid, &frame, MetafileFrameUnitPixel, description, &metafile);
275     expect(InvalidParameter, stat);
276
277     stat = GdipRecordMetafile(hdc, MetafileTypeWmf, &frame, MetafileFrameUnitPixel, description, &metafile);
278     expect(InvalidParameter, stat);
279
280     stat = GdipRecordMetafile(hdc, MetafileTypeWmfPlaceable, &frame, MetafileFrameUnitPixel, description, &metafile);
281     expect(InvalidParameter, stat);
282
283     stat = GdipRecordMetafile(hdc, MetafileTypeEmfPlusDual+1, &frame, MetafileFrameUnitPixel, description, &metafile);
284     expect(InvalidParameter, stat);
285
286     stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, NULL);
287     expect(InvalidParameter, stat);
288
289     stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
290     expect(Ok, stat);
291
292     DeleteDC(hdc);
293
294     if (stat != Ok)
295         return;
296
297     stat = GdipGetHemfFromMetafile(metafile, &hemf);
298     expect(InvalidParameter, stat);
299
300     stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
301     expect(Ok, stat);
302
303     stat = GdipGetHemfFromMetafile(metafile, &hemf);
304     expect(InvalidParameter, stat);
305
306     stat = GdipDeleteGraphics(graphics);
307     expect(Ok, stat);
308
309     check_metafile(metafile, empty_records, "empty metafile", dst_points, &frame, UnitPixel);
310
311     stat = GdipGetHemfFromMetafile(metafile, &hemf);
312     expect(Ok, stat);
313
314     stat = GdipGetHemfFromMetafile(metafile, &dummy);
315     expect(InvalidParameter, stat);
316
317     stat = GdipDisposeImage((GpImage*)metafile);
318     expect(Ok, stat);
319
320     check_emfplus(hemf, empty_records, "empty emf");
321
322     ret = DeleteEnhMetaFile(hemf);
323     ok(ret != 0, "Failed to delete enhmetafile %p\n", hemf);
324 }
325
326 static const emfplus_record getdc_records[] = {
327     {0, EMR_HEADER},
328     {0, EmfPlusRecordTypeHeader},
329     {0, EmfPlusRecordTypeGetDC},
330     {0, EMR_CREATEBRUSHINDIRECT},
331     {0, EMR_SELECTOBJECT},
332     {0, EMR_RECTANGLE},
333     {0, EMR_SELECTOBJECT},
334     {0, EMR_DELETEOBJECT},
335     {0, EmfPlusRecordTypeEndOfFile},
336     {0, EMR_EOF},
337     {0}
338 };
339
340 static void test_getdc(void)
341 {
342     GpStatus stat;
343     GpMetafile *metafile;
344     GpGraphics *graphics;
345     HDC hdc, metafile_dc;
346     HENHMETAFILE hemf;
347     BOOL ret;
348     static const GpRectF frame = {0.0, 0.0, 100.0, 100.0};
349     static const GpPointF dst_points[3] = {{0.0,0.0},{100.0,0.0},{0.0,100.0}};
350     static const GpPointF dst_points_half[3] = {{0.0,0.0},{50.0,0.0},{0.0,50.0}};
351     static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
352     HBRUSH hbrush, holdbrush;
353     GpBitmap *bitmap;
354     ARGB color;
355
356     hdc = CreateCompatibleDC(0);
357
358     stat = GdipRecordMetafile(hdc, EmfTypeEmfPlusOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
359     expect(Ok, stat);
360
361     DeleteDC(hdc);
362
363     if (stat != Ok)
364         return;
365
366     stat = GdipGetHemfFromMetafile(metafile, &hemf);
367     expect(InvalidParameter, stat);
368
369     stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
370     expect(Ok, stat);
371
372     stat = GdipGetDC(graphics, &metafile_dc);
373     expect(Ok, stat);
374
375     if (stat != Ok)
376     {
377         GdipDeleteGraphics(graphics);
378         GdipDisposeImage((GpImage*)metafile);
379         return;
380     }
381
382     hbrush = CreateSolidBrush(0xff0000);
383
384     holdbrush = SelectObject(metafile_dc, hbrush);
385
386     Rectangle(metafile_dc, 25, 25, 75, 75);
387
388     SelectObject(metafile_dc, holdbrush);
389
390     DeleteObject(hbrush);
391
392     stat = GdipReleaseDC(graphics, metafile_dc);
393     expect(Ok, stat);
394
395     stat = GdipDeleteGraphics(graphics);
396     expect(Ok, stat);
397
398     check_metafile(metafile, getdc_records, "getdc metafile", dst_points, &frame, UnitPixel);
399
400     stat = GdipCreateBitmapFromScan0(100, 100, 0, PixelFormat32bppARGB, NULL, &bitmap);
401     expect(Ok, stat);
402
403     stat = GdipGetImageGraphicsContext((GpImage*)bitmap, &graphics);
404     expect(Ok, stat);
405
406     play_metafile(metafile, graphics, getdc_records, "getdc playback", dst_points, &frame, UnitPixel);
407
408     stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
409     expect(Ok, stat);
410     expect(0, color);
411
412     stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
413     expect(Ok, stat);
414     expect(0xff0000ff, color);
415
416     stat = GdipBitmapSetPixel(bitmap, 50, 50, 0);
417     expect(Ok, stat);
418
419     play_metafile(metafile, graphics, getdc_records, "getdc playback", dst_points_half, &frame, UnitPixel);
420
421     stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
422     expect(Ok, stat);
423     expect(0xff0000ff, color);
424
425     stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
426     expect(Ok, stat);
427     expect(0, color);
428
429     stat = GdipBitmapSetPixel(bitmap, 15, 15, 0);
430     expect(Ok, stat);
431
432     stat = GdipDrawImagePointsRect(graphics, (GpImage*)metafile, dst_points, 3,
433         0.0, 0.0, 100.0, 100.0, UnitPixel, NULL, NULL, NULL);
434     expect(Ok, stat);
435
436     stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
437     expect(Ok, stat);
438     expect(0, color);
439
440     stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
441     expect(Ok, stat);
442     expect(0xff0000ff, color);
443
444     stat = GdipDeleteGraphics(graphics);
445     expect(Ok, stat);
446
447     stat = GdipDisposeImage((GpImage*)bitmap);
448     expect(Ok, stat);
449
450     stat = GdipGetHemfFromMetafile(metafile, &hemf);
451     expect(Ok, stat);
452
453     stat = GdipDisposeImage((GpImage*)metafile);
454     expect(Ok, stat);
455
456     check_emfplus(hemf, getdc_records, "getdc emf");
457
458     ret = DeleteEnhMetaFile(hemf);
459     ok(ret != 0, "Failed to delete enhmetafile %p\n", hemf);
460 }
461
462 static const emfplus_record emfonly_records[] = {
463     {0, EMR_HEADER},
464     {0, EMR_CREATEBRUSHINDIRECT},
465     {0, EMR_SELECTOBJECT},
466     {0, EMR_RECTANGLE},
467     {0, EMR_SELECTOBJECT},
468     {0, EMR_DELETEOBJECT},
469     {0, EMR_EOF},
470     {0}
471 };
472
473 static void test_emfonly(void)
474 {
475     GpStatus stat;
476     GpMetafile *metafile;
477     GpImage *clone;
478     GpGraphics *graphics;
479     HDC hdc, metafile_dc;
480     HENHMETAFILE hemf;
481     BOOL ret;
482     static const GpRectF frame = {0.0, 0.0, 100.0, 100.0};
483     static const GpPointF dst_points[3] = {{0.0,0.0},{100.0,0.0},{0.0,100.0}};
484     static const WCHAR description[] = {'w','i','n','e','t','e','s','t',0};
485     HBRUSH hbrush, holdbrush;
486     GpBitmap *bitmap;
487     ARGB color;
488
489     hdc = CreateCompatibleDC(0);
490
491     stat = GdipRecordMetafile(hdc, EmfTypeEmfOnly, &frame, MetafileFrameUnitPixel, description, &metafile);
492     expect(Ok, stat);
493
494     DeleteDC(hdc);
495
496     if (stat != Ok)
497         return;
498
499     stat = GdipGetHemfFromMetafile(metafile, &hemf);
500     expect(InvalidParameter, stat);
501
502     stat = GdipGetImageGraphicsContext((GpImage*)metafile, &graphics);
503     expect(Ok, stat);
504
505     stat = GdipGetDC(graphics, &metafile_dc);
506     expect(Ok, stat);
507
508     if (stat != Ok)
509     {
510         GdipDeleteGraphics(graphics);
511         GdipDisposeImage((GpImage*)metafile);
512         return;
513     }
514
515     hbrush = CreateSolidBrush(0xff0000);
516
517     holdbrush = SelectObject(metafile_dc, hbrush);
518
519     Rectangle(metafile_dc, 25, 25, 75, 75);
520
521     SelectObject(metafile_dc, holdbrush);
522
523     DeleteObject(hbrush);
524
525     stat = GdipReleaseDC(graphics, metafile_dc);
526     expect(Ok, stat);
527
528     stat = GdipDeleteGraphics(graphics);
529     expect(Ok, stat);
530
531     check_metafile(metafile, emfonly_records, "emfonly metafile", dst_points, &frame, UnitPixel);
532
533     stat = GdipCreateBitmapFromScan0(100, 100, 0, PixelFormat32bppARGB, NULL, &bitmap);
534     expect(Ok, stat);
535
536     stat = GdipGetImageGraphicsContext((GpImage*)bitmap, &graphics);
537     expect(Ok, stat);
538
539     play_metafile(metafile, graphics, emfonly_records, "emfonly playback", dst_points, &frame, UnitPixel);
540
541     stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
542     expect(Ok, stat);
543     expect(0, color);
544
545     stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
546     expect(Ok, stat);
547     expect(0xff0000ff, color);
548
549     stat = GdipBitmapSetPixel(bitmap, 50, 50, 0);
550     expect(Ok, stat);
551
552     stat = GdipDrawImagePointsRect(graphics, (GpImage*)metafile, dst_points, 3,
553         0.0, 0.0, 100.0, 100.0, UnitPixel, NULL, NULL, NULL);
554     expect(Ok, stat);
555
556     stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
557     expect(Ok, stat);
558     expect(0, color);
559
560     stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
561     expect(Ok, stat);
562     expect(0xff0000ff, color);
563
564     stat = GdipCloneImage((GpImage*)metafile, &clone);
565     expect(Ok, stat);
566
567     if (stat == Ok)
568     {
569         stat = GdipBitmapSetPixel(bitmap, 50, 50, 0);
570         expect(Ok, stat);
571
572         stat = GdipDrawImagePointsRect(graphics, clone, dst_points, 3,
573             0.0, 0.0, 100.0, 100.0, UnitPixel, NULL, NULL, NULL);
574         expect(Ok, stat);
575
576         stat = GdipBitmapGetPixel(bitmap, 15, 15, &color);
577         expect(Ok, stat);
578         expect(0, color);
579
580         stat = GdipBitmapGetPixel(bitmap, 50, 50, &color);
581         expect(Ok, stat);
582         expect(0xff0000ff, color);
583
584         GdipDisposeImage(clone);
585     }
586
587     stat = GdipDeleteGraphics(graphics);
588     expect(Ok, stat);
589
590     stat = GdipDisposeImage((GpImage*)bitmap);
591     expect(Ok, stat);
592
593     stat = GdipGetHemfFromMetafile(metafile, &hemf);
594     expect(Ok, stat);
595
596     stat = GdipDisposeImage((GpImage*)metafile);
597     expect(Ok, stat);
598
599     check_emfplus(hemf, emfonly_records, "emfonly emf");
600
601     ret = DeleteEnhMetaFile(hemf);
602     ok(ret != 0, "Failed to delete enhmetafile %p\n", hemf);
603 }
604
605 START_TEST(metafile)
606 {
607     struct GdiplusStartupInput gdiplusStartupInput;
608     ULONG_PTR gdiplusToken;
609
610     gdiplusStartupInput.GdiplusVersion              = 1;
611     gdiplusStartupInput.DebugEventCallback          = NULL;
612     gdiplusStartupInput.SuppressBackgroundThread    = 0;
613     gdiplusStartupInput.SuppressExternalCodecs      = 0;
614
615     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
616
617     test_empty();
618     test_getdc();
619     test_emfonly();
620
621     GdiplusShutdown(gdiplusToken);
622 }