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