Remove CAS from System.Drawing unit tests
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Imaging / MetafileTest.cs
1 //
2 // Metafile class unit tests
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Drawing;
31 using System.Drawing.Drawing2D;
32 using System.Drawing.Imaging;
33 using System.IO;
34 using System.Runtime.InteropServices;
35 using System.Security.Permissions;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Drawing.Imaging {
39
40         [TestFixture]
41         public class MetafileTest {
42
43                 public const string Bitmap = "bitmaps/non-inverted.bmp";
44                 public const string WmfPlaceable = "bitmaps/telescope_01.wmf";
45                 public const string Emf = "bitmaps/milkmateya01.emf";
46
47                 // Get the input directory depending on the runtime
48                 static public string getInFile (string file)
49                 {
50                         string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
51
52                         if (!File.Exists (sRslt))
53                                 sRslt = "Test/System.Drawing/" + file;
54
55                         return sRslt;
56                 }
57
58                 [Test]
59                 public void Metafile_Stream_Null ()
60                 {
61                         Assert.Throws<ArgumentException> (() => new Metafile ((Stream)null));
62                 }
63
64                 [Test]
65                 public void Metafile_String_Null ()
66                 {
67                         Assert.Throws<ArgumentNullException> (() => new Metafile ((string) null));
68                 }
69
70                 [Test]
71                 public void Metafile_String_Empty ()
72                 {
73                         Assert.Throws<ArgumentException> (() => new Metafile (String.Empty));
74                 }
75
76                 [Test]
77                 public void Metafile_String_FileDoesNotExists ()
78                 {
79                         string filename = getInFile ("telescope_02.wmf");
80                         Assert.Throws<ExternalException> (() => new Metafile (filename));
81                 }
82
83                 [Test]
84                 public void Metafile_String ()
85                 {
86                         string filename = getInFile (WmfPlaceable);
87                         Metafile mf = new Metafile (filename);
88                         Metafile clone = (Metafile) mf.Clone ();
89                 }
90
91                 [Test]
92                 public void GetMetafileHeader_Bitmap ()
93                 {
94                         Assert.Throws<ExternalException> (() => new Metafile (getInFile (Bitmap)));
95                 }
96
97                 static public void Check_MetaHeader_WmfPlaceable (MetaHeader mh)
98                 {
99                         Assert.AreEqual (9, mh.HeaderSize, "HeaderSize");
100                         Assert.AreEqual (98, mh.MaxRecord, "MaxRecord");
101                         Assert.AreEqual (3, mh.NoObjects, "NoObjects");
102                         Assert.AreEqual (0, mh.NoParameters, "NoParameters");
103                         Assert.AreEqual (1737, mh.Size, "Size");
104                         Assert.AreEqual (1, mh.Type, "Type");
105                         Assert.AreEqual (0x300, mh.Version, "Version");
106                 }
107
108                 public static void Check_MetafileHeader_WmfPlaceable (MetafileHeader header)
109                 {
110                         Assert.AreEqual (MetafileType.WmfPlaceable, header.Type, "Type");
111                         Assert.AreEqual (0x300, header.Version, "Version");
112                         // filesize - 22, which happens to be the size (22) of a PLACEABLEMETAHEADER struct
113                         Assert.AreEqual (3474, header.MetafileSize, "MetafileSize");
114
115                         Assert.AreEqual (-30, header.Bounds.X, "Bounds.X");
116                         Assert.AreEqual (-40, header.Bounds.Y, "Bounds.Y");
117                         Assert.AreEqual (3096, header.Bounds.Width, "Bounds.Width");
118                         Assert.AreEqual (4127, header.Bounds.Height, "Bounds.Height");
119                         Assert.AreEqual (606, header.DpiX, "DpiX");
120                         Assert.AreEqual (606, header.DpiY, "DpiY");
121                         Assert.AreEqual (0, header.EmfPlusHeaderSize, "EmfPlusHeaderSize");
122                         Assert.AreEqual (0, header.LogicalDpiX, "LogicalDpiX");
123                         Assert.AreEqual (0, header.LogicalDpiY, "LogicalDpiY");
124
125                         Assert.IsNotNull (header.WmfHeader, "WmfHeader");
126                         Check_MetaHeader_WmfPlaceable (header.WmfHeader);
127
128                         Assert.IsFalse (header.IsDisplay (), "IsDisplay");
129                         Assert.IsFalse (header.IsEmf (), "IsEmf");
130                         Assert.IsFalse (header.IsEmfOrEmfPlus (), "IsEmfOrEmfPlus");
131                         Assert.IsFalse (header.IsEmfPlus (), "IsEmfPlus");
132                         Assert.IsFalse (header.IsEmfPlusDual (), "IsEmfPlusDual");
133                         Assert.IsFalse (header.IsEmfPlusOnly (), "IsEmfPlusOnly");
134                         Assert.IsTrue (header.IsWmf (), "IsWmf");
135                         Assert.IsTrue (header.IsWmfPlaceable (), "IsWmfPlaceable");
136                 }
137
138                 [Test]
139                 public void GetMetafileHeader_WmfPlaceable ()
140                 {
141                         using (Metafile mf = new Metafile (getInFile (WmfPlaceable))) {
142                                 MetafileHeader header1 = mf.GetMetafileHeader ();
143                                 Check_MetafileHeader_WmfPlaceable (header1);
144
145                                 MetafileHeader header2 = mf.GetMetafileHeader ();
146                                 Assert.IsFalse (Object.ReferenceEquals (header1, header2), "Same object");
147                         }
148                 }
149
150                 [Test]
151                 public void GetMetafileHeader_FromFile_WmfPlaceable ()
152                 {
153                         using (Metafile mf = new Metafile (getInFile (WmfPlaceable))) {
154                                 MetafileHeader header1 = mf.GetMetafileHeader ();
155                                 Check_MetafileHeader_WmfPlaceable (header1);
156
157                                 MetaHeader mh1 = header1.WmfHeader;
158                                 Check_MetaHeader_WmfPlaceable (mh1);
159
160                                 MetaHeader mh2 = mf.GetMetafileHeader ().WmfHeader;
161                                 Assert.IsFalse (Object.ReferenceEquals (mh1, mh2), "Same object");
162                         }
163                 }
164
165                 [Test]
166                 public void GetMetafileHeader_FromFileStream_WmfPlaceable ()
167                 {
168                         using (FileStream fs = File.OpenRead (getInFile (WmfPlaceable))) {
169                                 using (Metafile mf = new Metafile (fs)) {
170                                         MetafileHeader header1 = mf.GetMetafileHeader ();
171                                         Check_MetafileHeader_WmfPlaceable (header1);
172
173                                         MetaHeader mh1 = header1.WmfHeader;
174                                         Check_MetaHeader_WmfPlaceable (mh1);
175
176                                         MetaHeader mh2 = mf.GetMetafileHeader ().WmfHeader;
177                                         Assert.IsFalse (Object.ReferenceEquals (mh1, mh2), "Same object");
178                                 }
179                         }
180                 }
181
182                 [Test]
183                 public void GetMetafileHeader_FromMemoryStream_WmfPlaceable ()
184                 {
185                         MemoryStream ms;
186                         string filename = getInFile (WmfPlaceable);
187                         using (FileStream fs = File.OpenRead (filename)) {
188                                 byte[] data = new byte[fs.Length];
189                                 fs.Read (data, 0, data.Length);
190                                 ms = new MemoryStream (data);
191                         }
192                         using (Metafile mf = new Metafile (ms)) {
193                                 MetafileHeader header1 = mf.GetMetafileHeader ();
194                                 Check_MetafileHeader_WmfPlaceable (header1);
195
196                                 MetaHeader mh1 = header1.WmfHeader;
197                                 Check_MetaHeader_WmfPlaceable (mh1);
198
199                                 MetaHeader mh2 = mf.GetMetafileHeader ().WmfHeader;
200                                 Assert.IsFalse (Object.ReferenceEquals (mh1, mh2), "Same object");
201                         }
202                         ms.Close ();
203                 }
204
205                 public static void Check_MetafileHeader_Emf (MetafileHeader header)
206                 {
207                         Assert.AreEqual (MetafileType.Emf, header.Type, "Type");
208                         Assert.AreEqual (65536, header.Version, "Version");
209                         // extactly the filesize
210                         Assert.AreEqual (20456, header.MetafileSize, "MetafileSize");
211
212                         Assert.AreEqual (0, header.Bounds.X, "Bounds.X");
213                         Assert.AreEqual (0, header.Bounds.Y, "Bounds.Y");
214 #if false
215                         Assert.AreEqual (759, header.Bounds.Width, "Bounds.Width");
216                         Assert.AreEqual (1073, header.Bounds.Height, "Bounds.Height");
217                         Assert.AreEqual (96f, header.DpiX, 0.5f, "DpiX");
218                         Assert.AreEqual (96f, header.DpiY, 0.5f, "DpiY");
219                         Assert.AreEqual (6619188, header.EmfPlusHeaderSize, "EmfPlusHeaderSize");
220                         Assert.AreEqual (3670064, header.LogicalDpiX, "LogicalDpiX");
221                         Assert.AreEqual (3670064, header.LogicalDpiY, "LogicalDpiY");
222 #endif
223                         try {
224                                 Assert.IsNotNull (header.WmfHeader, "WmfHeader");
225                                 Assert.Fail ("WmfHeader didn't throw an ArgumentException");
226                         }
227                         catch (ArgumentException) {
228                         }
229                         catch (Exception e) {
230                                 Assert.Fail ("WmfHeader didn't throw an ArgumentException but: {0}.", e.ToString ());
231                         }
232
233                         Assert.IsFalse (header.IsDisplay (), "IsDisplay");
234                         Assert.IsTrue (header.IsEmf (), "IsEmf");
235                         Assert.IsTrue (header.IsEmfOrEmfPlus (), "IsEmfOrEmfPlus");
236                         Assert.IsFalse (header.IsEmfPlus (), "IsEmfPlus");
237                         Assert.IsFalse (header.IsEmfPlusDual (), "IsEmfPlusDual");
238                         Assert.IsFalse (header.IsEmfPlusOnly (), "IsEmfPlusOnly");
239                         Assert.IsFalse (header.IsWmf (), "IsWmf");
240                         Assert.IsFalse (header.IsWmfPlaceable (), "IsWmfPlaceable");
241                 }
242
243                 [Test]
244                 public void GetMetafileHeader_FromFile_Emf ()
245                 {
246                         using (Metafile mf = new Metafile (getInFile (Emf))) {
247                                 MetafileHeader header1 = mf.GetMetafileHeader ();
248                                 Check_MetafileHeader_Emf (header1);
249                         }
250                 }
251
252                 [Test]
253                 public void GetMetafileHeader_FromFileStream_Emf ()
254                 {
255                         using (FileStream fs = File.OpenRead (getInFile (Emf))) {
256                                 using (Metafile mf = new Metafile (fs)) {
257                                         MetafileHeader header1 = mf.GetMetafileHeader ();
258                                         Check_MetafileHeader_Emf (header1);
259                                 }
260                         }
261                 }
262
263                 [Test]
264                 public void GetMetafileHeader_FromMemoryStream_Emf ()
265                 {
266                         MemoryStream ms;
267                         string filename = getInFile (Emf);
268                         using (FileStream fs = File.OpenRead (filename)) {
269                                 byte[] data = new byte[fs.Length];
270                                 fs.Read (data, 0, data.Length);
271                                 ms = new MemoryStream (data);
272                         }
273                         using (Metafile mf = new Metafile (ms)) {
274                                 MetafileHeader header1 = mf.GetMetafileHeader ();
275                                 Check_MetafileHeader_Emf (header1);
276                         }
277                         ms.Close ();
278                 }
279
280                 [Test]
281                 public void Static_GetMetafileHeader_Stream_Null ()
282                 {
283                         Assert.Throws<NullReferenceException> (() => Metafile.GetMetafileHeader ((Stream)null));
284                 }
285
286                 [Test]
287                 public void Static_GetMetafileHeader_Stream ()
288                 {
289                         string filename = getInFile (WmfPlaceable);
290                         using (FileStream fs = File.OpenRead (filename)) {
291                                 MetafileHeader header = Metafile.GetMetafileHeader (fs);
292                                 Check_MetafileHeader_WmfPlaceable (header);
293                         }
294                 }
295
296                 [Test]
297                 public void Static_GetMetafileHeader_Filename_Null ()
298                 {
299                         Assert.Throws<ArgumentNullException> (() => Metafile.GetMetafileHeader ((string) null));
300                 }
301
302                 [Test]
303                 public void Static_GetMetafileHeader_Filename ()
304                 {
305                         string filename = getInFile (WmfPlaceable);
306                         MetafileHeader header = Metafile.GetMetafileHeader (filename);
307                         Check_MetafileHeader_WmfPlaceable (header);
308                 }
309         }
310
311         [TestFixture]
312         public class MetafileFulltrustTest {
313
314                 private Font test_font;
315
316                 [TestFixtureSetUp]
317                 public void FixtureSetUp ()
318                 {
319                         try {
320                                 test_font = new Font (FontFamily.GenericMonospace, 12);
321                         }
322                         catch (ArgumentException) {
323                         }
324                 }
325
326                 [Test]
327                 public void Static_GetMetafileHeader_IntPtr_Zero ()
328                 {
329                         Assert.Throws<ArgumentException> (() => Metafile.GetMetafileHeader (IntPtr.Zero));
330                 }
331
332                 [Test]
333                 public void Static_GetMetafileHeader_IntPtr ()
334                 {
335                         string filename = MetafileTest.getInFile (MetafileTest.WmfPlaceable);
336                         using (Metafile mf = new Metafile (filename)) {
337
338                                 IntPtr hemf = mf.GetHenhmetafile ();
339                                 Assert.IsTrue (hemf != IntPtr.Zero, "GetHenhmetafile");
340
341                                 Assert.Throws<ArgumentException> (() => Metafile.GetMetafileHeader (hemf));
342                         }
343                 }
344
345                 [Test]
346                 public void Metafile_IntPtrBool_Zero ()
347                 {
348                         Assert.Throws<ArgumentException> (() => new Metafile (IntPtr.Zero, false));
349                 }
350
351                 [Test]
352                 public void Metafile_IntPtrEmfType_Zero ()
353                 {
354                         Assert.Throws<ArgumentException> (() => new Metafile (IntPtr.Zero, EmfType.EmfOnly));
355                 }
356
357                 private void CheckEmptyHeader (Metafile mf, EmfType type)
358                 {
359                         MetafileHeader mh = mf.GetMetafileHeader ();
360                         Assert.AreEqual (0, mh.Bounds.X, "Bounds.X");
361                         Assert.AreEqual (0, mh.Bounds.Y, "Bounds.Y");
362                         Assert.AreEqual (0, mh.Bounds.Width, "Bounds.Width");
363                         Assert.AreEqual (0, mh.Bounds.Height, "Bounds.Height");
364                         Assert.AreEqual (0, mh.MetafileSize, "MetafileSize");
365                         switch (type) {
366                         case EmfType.EmfOnly:
367                                 Assert.AreEqual (MetafileType.Emf, mh.Type, "Type");
368                                 break;
369                         case EmfType.EmfPlusDual:
370                                 Assert.AreEqual (MetafileType.EmfPlusDual, mh.Type, "Type");
371                                 break;
372                         case EmfType.EmfPlusOnly:
373                                 Assert.AreEqual (MetafileType.EmfPlusOnly, mh.Type, "Type");
374                                 break;
375                         default:
376                                 Assert.Fail ("Unknown EmfType '{0}'", type);
377                                 break;
378                         }
379                 }
380
381                 private void Metafile_IntPtrEmfType (EmfType type)
382                 {
383                         using (Bitmap bmp = new Bitmap (10, 10, PixelFormat.Format32bppArgb)) {
384                                 using (Graphics g = Graphics.FromImage (bmp)) {
385                                         IntPtr hdc = g.GetHdc ();
386                                         try {
387                                                 Metafile mf = new Metafile (hdc, type);
388                                                 CheckEmptyHeader (mf, type);
389                                         }
390                                         finally {
391                                                 g.ReleaseHdc (hdc);
392                                         }
393                                 }
394                         }
395                 }
396
397                 [Test]
398                 public void Metafile_IntPtrEmfType_Invalid ()
399                 {
400                         Assert.Throws<ArgumentException> (() => Metafile_IntPtrEmfType ((EmfType)Int32.MinValue));
401                 }
402
403                 [Test]
404                 public void Metafile_IntPtrEmfType_EmfOnly ()
405                 {
406                         Metafile_IntPtrEmfType (EmfType.EmfOnly);
407                 }
408
409                 [Test]
410                 public void Metafile_IntPtrEmfType_EmfPlusDual ()
411                 {
412                         Metafile_IntPtrEmfType (EmfType.EmfPlusDual);
413                 }
414
415                 [Test]
416                 public void Metafile_IntPtrEmfType_EmfPlusOnly ()
417                 {
418                         Metafile_IntPtrEmfType (EmfType.EmfPlusOnly);
419                 }
420
421                 [Test]
422                 public void Metafile_IntPtrRectangle_Zero ()
423                 {
424                         Assert.Throws<ArgumentException> (() => new Metafile (IntPtr.Zero, new Rectangle (1, 2, 3, 4)));
425                 }
426
427                 [Test]
428                 public void Metafile_IntPtrRectangle_Empty ()
429                 {
430                         using (Bitmap bmp = new Bitmap (10, 10, PixelFormat.Format32bppArgb)) {
431                                 using (Graphics g = Graphics.FromImage (bmp)) {
432                                         IntPtr hdc = g.GetHdc ();
433                                         try {
434                                                 Metafile mf = new Metafile (hdc, new Rectangle ());
435                                                 CheckEmptyHeader (mf, EmfType.EmfPlusDual);
436                                         }
437                                         finally {
438                                                 g.ReleaseHdc (hdc);
439                                         }
440                                 }
441                         }
442                 }
443
444                 [Test]
445                 public void Metafile_IntPtrRectangleF_Zero ()
446                 {
447                         Assert.Throws<ArgumentException> (() => new Metafile (IntPtr.Zero, new RectangleF (1, 2, 3, 4)));
448                 }
449
450                 [Test]
451                 public void Metafile_IntPtrRectangleF_Empty ()
452                 {
453                         using (Bitmap bmp = new Bitmap (10, 10, PixelFormat.Format32bppArgb)) {
454                                 using (Graphics g = Graphics.FromImage (bmp)) {
455                                         IntPtr hdc = g.GetHdc ();
456                                         try {
457                                                 Metafile mf = new Metafile (hdc, new RectangleF ());
458                                                 CheckEmptyHeader (mf, EmfType.EmfPlusDual);
459                                         }
460                                         finally {
461                                                 g.ReleaseHdc (hdc);
462                                         }
463                                 }
464                         }
465                 }
466
467                 private void Metafile_StreamEmfType (Stream stream, EmfType type)
468                 {
469                         using (Bitmap bmp = new Bitmap (10, 10, PixelFormat.Format32bppArgb)) {
470                                 using (Graphics g = Graphics.FromImage (bmp)) {
471                                         IntPtr hdc = g.GetHdc ();
472                                         try {
473                                                 Metafile mf = new Metafile (stream, hdc, type);
474                                                 CheckEmptyHeader (mf, type);
475                                         }
476                                         finally {
477                                                 g.ReleaseHdc (hdc);
478                                         }
479                                 }
480                         }
481                 }
482
483                 [Test]
484                 public void Metafile_StreamIntPtrEmfType_Null ()
485                 {
486                         Assert.Throws<NullReferenceException> (() => Metafile_StreamEmfType (null, EmfType.EmfOnly));
487                 }
488
489                 [Test]
490                 public void Metafile_StreamIntPtrEmfType_EmfOnly ()
491                 {
492                         using (MemoryStream ms = new MemoryStream ()) {
493                                 Metafile_StreamEmfType (ms, EmfType.EmfOnly);
494                         }
495                 }
496
497                 [Test]
498                 public void Metafile_StreamIntPtrEmfType_Invalid ()
499                 {
500                         using (MemoryStream ms = new MemoryStream ()) {
501                                 Assert.Throws<ArgumentException> (() => Metafile_StreamEmfType (ms, (EmfType)Int32.MinValue));
502                         }
503                 }
504
505                 private void CreateFilename (EmfType type, bool single)
506                 {
507                         string name = String.Format ("{0}-{1}.emf", type, single ? "Single" : "Multiple");
508                         string filename = Path.Combine (Path.GetTempPath (), name);
509                         Metafile mf;
510                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppArgb)) {
511                                 using (Graphics g = Graphics.FromImage (bmp)) {
512                                         IntPtr hdc = g.GetHdc ();
513                                         try {
514                                                 mf = new Metafile (filename, hdc, type);
515                                                 Assert.AreEqual (0, new FileInfo (filename).Length, "Empty");
516                                         }
517                                         finally {
518                                                 g.ReleaseHdc (hdc);
519                                         }
520                                 }
521                                 long size = 0;
522                                 using (Graphics g = Graphics.FromImage (mf)) {
523                                         g.FillRectangle (Brushes.BlueViolet, 10, 10, 80, 80);
524                                         size = new FileInfo (filename).Length;
525                                         Assert.AreEqual (0, size, "Still-Empty");
526                                 }
527 // FIXME / doesn't work on mono yet
528 //                              size = new FileInfo (filename).Length;
529 //                              Assert.IsTrue (size > 0, "Non-Empty/GraphicsDisposed");
530                                 if (!single) {
531                                         // can we append stuff ?
532                                         using (Graphics g = Graphics.FromImage (mf)) {
533                                                 g.DrawRectangle (Pens.Azure, 10, 10, 80, 80);
534                                                 // happily no :)
535                                         }
536                                 }
537                                 mf.Dispose ();
538                                 Assert.AreEqual (size, new FileInfo (filename).Length, "Non-Empty/MetafileDisposed");
539                         }
540                 }
541
542                 [Test]
543                 public void CreateFilename_SingleGraphics_EmfOnly ()
544                 {
545                         CreateFilename (EmfType.EmfOnly, true);
546                 }
547
548                 [Test]
549                 public void CreateFilename_SingleGraphics_EmfPlusDual ()
550                 {
551                         CreateFilename (EmfType.EmfPlusDual, true);
552                 }
553
554                 [Test]
555                 public void CreateFilename_SingleGraphics_EmfPlusOnly ()
556                 {
557                         CreateFilename (EmfType.EmfPlusOnly, true);
558                 }
559
560                 [Test]
561                 public void CreateFilename_MultipleGraphics_EmfOnly ()
562                 {
563                         Assert.Throws<OutOfMemoryException> (() => CreateFilename (EmfType.EmfOnly, false));
564                 }
565
566                 [Test]
567                 public void CreateFilename_MultipleGraphics_EmfPlusDual ()
568                 {
569                         Assert.Throws<OutOfMemoryException> (() => CreateFilename (EmfType.EmfPlusDual, false));
570                 }
571
572                 [Test]
573                 public void CreateFilename_MultipleGraphics_EmfPlusOnly ()
574                 {
575                         Assert.Throws<OutOfMemoryException> (() => CreateFilename (EmfType.EmfPlusOnly, false));
576                 }
577
578                 [Test]
579                 public void Measure ()
580                 {
581                         if (test_font == null)
582                                 Assert.Ignore ("No font family could be found.");
583
584                         Metafile mf;
585                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppArgb)) {
586                                 using (Graphics g = Graphics.FromImage (bmp)) {
587                                         IntPtr hdc = g.GetHdc ();
588                                         try {
589                                                 mf = new Metafile (hdc, EmfType.EmfPlusOnly);
590                                         }
591                                         finally {
592                                                 g.ReleaseHdc (hdc);
593                                         }
594                                 }
595                                 using (Graphics g = Graphics.FromImage (mf)) {
596                                         string text = "this\nis a test";
597                                         CharacterRange[] ranges = new CharacterRange[2];
598                                         ranges[0] = new CharacterRange (0, 5);
599                                         ranges[1] = new CharacterRange (5, 9);
600
601                                         SizeF size = g.MeasureString (text, test_font);
602                                         Assert.IsFalse (size.IsEmpty, "MeasureString");
603
604                                         StringFormat sf = new StringFormat ();
605                                         sf.FormatFlags = StringFormatFlags.NoClip;
606                                         sf.SetMeasurableCharacterRanges (ranges);
607
608                                         RectangleF rect = new RectangleF (0, 0, size.Width, size.Height);
609                                         Region[] region = g.MeasureCharacterRanges (text, test_font, rect, sf);
610                                         Assert.AreEqual (2, region.Length, "MeasureCharacterRanges");
611                                 }
612                                 mf.Dispose ();
613                         }
614                 }
615
616                 [Test]
617                 public void WorldTransforms ()
618                 {
619                         Metafile mf;
620                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppArgb)) {
621                                 using (Graphics g = Graphics.FromImage (bmp)) {
622                                         IntPtr hdc = g.GetHdc ();
623                                         try {
624                                                 mf = new Metafile (hdc, EmfType.EmfPlusOnly);
625                                         }
626                                         finally {
627                                                 g.ReleaseHdc (hdc);
628                                         }
629                                 }
630                                 using (Graphics g = Graphics.FromImage (mf)) {
631                                         Assert.IsTrue (g.Transform.IsIdentity, "Initial/IsIdentity");
632                                         g.ScaleTransform (2f, 0.5f);
633                                         Assert.IsFalse (g.Transform.IsIdentity, "Scale/IsIdentity");
634                                         g.RotateTransform (90);
635                                         g.TranslateTransform (-2, 2);
636                                         Matrix m = g.Transform;
637                                         g.MultiplyTransform (m);
638                                         // check
639                                         float[] elements = g.Transform.Elements;
640                                         Assert.AreEqual (-1f, elements[0], 0.00001f, "a0");
641                                         Assert.AreEqual (0f, elements[1], 0.00001f, "a1");
642                                         Assert.AreEqual (0f, elements[2], 0.00001f, "a2");
643                                         Assert.AreEqual (-1f, elements[3], 0.00001f, "a3");
644                                         Assert.AreEqual (-2f, elements[4], 0.00001f, "a4");
645                                         Assert.AreEqual (-3f, elements[5], 0.00001f, "a5");
646
647                                         g.Transform = m;
648                                         elements = g.Transform.Elements;
649                                         Assert.AreEqual (0f, elements[0], 0.00001f, "b0");
650                                         Assert.AreEqual (0.5f, elements[1], 0.00001f, "b1");
651                                         Assert.AreEqual (-2f, elements[2], 0.00001f, "b2");
652                                         Assert.AreEqual (0f, elements[3], 0.00001f, "b3");
653                                         Assert.AreEqual (-4f, elements[4], 0.00001f, "b4");
654                                         Assert.AreEqual (-1f, elements[5], 0.00001f, "b5");
655
656                                         g.ResetTransform ();
657                                         Assert.IsTrue (g.Transform.IsIdentity, "Reset/IsIdentity");
658                                 }
659                                 mf.Dispose ();
660                         }
661                 }
662         }
663 }