1 // 2 // Image class testing unit 3 // 4 // Authors: 5 // Jordi Mas i Hernàndez (jmas@softcatala.org> 6 // Sebastien Pouliot <sebastien@ximian.com> 7 // 8 // (C) 2005 Ximian, Inc. http://www.ximian.com 9 // Copyright (C) 2005-2007 Novell, Inc (http://www.novell.com) 10 // 11 // Permission is hereby granted, free of charge, to any person obtaining 12 // a copy of this software and associated documentation files (the 13 // "Software"), to deal in the Software without restriction, including 14 // without limitation the rights to use, copy, modify, merge, publish, 15 // distribute, sublicense, and/or sell copies of the Software, and to 16 // permit persons to whom the Software is furnished to do so, subject to 17 // the following conditions: 18 // 19 // The above copyright notice and this permission notice shall be 20 // included in all copies or substantial portions of the Software. 21 // 22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 // 30 31 using System; 32 using System.Drawing; 33 using System.Drawing.Imaging; 34 using System.IO; 35 using System.Reflection; 36 using System.Security.Permissions; 37 using System.Xml.Serialization; 38 using NUnit.Framework; 39 40 namespace MonoTests.System.Drawing{ 41 42 [TestFixture] 43 public class ImageTest { 44 45 private string fname; 46 private bool callback; 47 48 [TestFixtureSetUp] FixtureSetup()49 public void FixtureSetup () 50 { 51 fname = Path.GetTempFileName (); 52 } 53 54 [TestFixtureTearDown] FixtureTearDown()55 public void FixtureTearDown () 56 { 57 try { 58 File.Delete (fname); 59 } 60 catch { 61 } 62 } 63 64 [SetUp] SetUp()65 public void SetUp () 66 { 67 callback = false; 68 } 69 70 [Test] FileDoesNotExists()71 public void FileDoesNotExists () 72 { 73 Assert.Throws<FileNotFoundException> (() => Image.FromFile ("FileDoesNotExists.jpg")); 74 } 75 CallbackTrue()76 private bool CallbackTrue () 77 { 78 callback = true; 79 return true; 80 } 81 CallbackFalse()82 private bool CallbackFalse () 83 { 84 callback = true; 85 return false; 86 } 87 88 [Test] GetThumbnailImage_NullCallback_Tiff()89 public void GetThumbnailImage_NullCallback_Tiff () 90 { 91 using (Bitmap bmp = new Bitmap (10, 10)) { 92 // according to documentation a callback is mandatory 93 Image tn = bmp.GetThumbnailImage (10, 5, null, IntPtr.Zero); 94 Assert.AreEqual (5, tn.Height, "Height"); 95 Assert.AreEqual (10, tn.Width, "Width"); 96 Assert.IsFalse (callback, "Callback called"); 97 tn.Save (fname, ImageFormat.Tiff); 98 } 99 } 100 101 [Test] GetThumbnailImage_Height_Zero()102 public void GetThumbnailImage_Height_Zero () 103 { 104 using (Bitmap bmp = new Bitmap (10, 10)) { 105 Assert.Throws<OutOfMemoryException> (() => bmp.GetThumbnailImage (5, 0, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero)); 106 } 107 } 108 109 [Test] GetThumbnailImage_Width_Negative()110 public void GetThumbnailImage_Width_Negative () 111 { 112 using (Bitmap bmp = new Bitmap (10, 10)) { 113 Assert.Throws<OutOfMemoryException> (() => bmp.GetThumbnailImage (-5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero)); 114 } 115 } 116 117 [Test] GetThumbnailImage_CallbackData_Invalid()118 public void GetThumbnailImage_CallbackData_Invalid () 119 { 120 using (Bitmap bmp = new Bitmap (10, 10)) { 121 // according to documentation IntPtr.Zero must be supplied as data 122 Image tn = bmp.GetThumbnailImage (5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), (IntPtr)Int32.MaxValue); 123 Assert.AreEqual (5, tn.Height, "Height"); 124 Assert.AreEqual (5, tn.Width, "Width"); 125 Assert.IsFalse (callback, "Callback called"); 126 tn.Save (fname, ImageFormat.Tiff); 127 } 128 } 129 130 [Test] GetThumbnailImage_SameSize_Bmp()131 public void GetThumbnailImage_SameSize_Bmp () 132 { 133 using (Bitmap bmp = new Bitmap (10, 10)) { 134 Image tn = bmp.GetThumbnailImage (10, 10, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero); 135 Assert.AreEqual (10, tn.Height, "Height"); 136 Assert.AreEqual (10, tn.Width, "Width"); 137 Assert.IsFalse (callback, "Callback called"); 138 tn.Save (fname, ImageFormat.Bmp); 139 } 140 } 141 142 [Test] GetThumbnailImage_Smaller_Gif()143 public void GetThumbnailImage_Smaller_Gif () 144 { 145 using (Bitmap bmp = new Bitmap (10, 10)) { 146 Image tn = bmp.GetThumbnailImage (4, 4, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero); 147 Assert.AreEqual (4, tn.Height, "Height"); 148 Assert.AreEqual (4, tn.Width, "Width"); 149 Assert.IsFalse (callback, "Callback called"); 150 tn.Save (fname, ImageFormat.Gif); 151 } 152 } 153 154 [Test] GetThumbnailImage_Bigger_Png()155 public void GetThumbnailImage_Bigger_Png () 156 { 157 using (Bitmap bmp = new Bitmap (10, 10)) { 158 Image tn = bmp.GetThumbnailImage (40, 40, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero); 159 Assert.AreEqual (40, tn.Height, "Height"); 160 Assert.AreEqual (40, tn.Width, "Width"); 161 Assert.IsFalse (callback, "Callback called"); 162 tn.Save (fname, ImageFormat.Png); 163 } 164 } 165 166 [Test] Stream_Unlocked()167 public void Stream_Unlocked () 168 { 169 try { 170 Image img = null; 171 using (MemoryStream ms = new MemoryStream ()) { 172 using (Bitmap bmp = new Bitmap (10, 10)) { 173 bmp.Save (ms, ImageFormat.Png); 174 } 175 ms.Position = 0; 176 img = Image.FromStream (ms); 177 } 178 // stream isn't available anymore 179 ((Bitmap) img).MakeTransparent (Color.Transparent); 180 } 181 catch (OutOfMemoryException) { 182 int p = (int) Environment.OSVersion.Platform; 183 // libgdiplus (UNIX) doesn't lazy load the image so the 184 // stream may be freed (and this exception will never occur) 185 if ((p == 4) || (p == 128) || (p == 6)) 186 throw; 187 } 188 } 189 190 [Test] Stream_Locked()191 public void Stream_Locked () 192 { 193 Image img = null; 194 using (MemoryStream ms = new MemoryStream ()) { 195 using (Bitmap bmp = new Bitmap (10, 10)) { 196 bmp.Save (ms, ImageFormat.Png); 197 } 198 ms.Position = 0; 199 img = Image.FromStream (ms); 200 // stream is available 201 ((Bitmap) img).MakeTransparent (Color.Transparent); 202 } 203 } 204 205 [Test] 206 [Category ("NotWorking")] // http://bugzilla.ximian.com/show_bug.cgi?id=80558 XmlSerialize()207 public void XmlSerialize () 208 { 209 new XmlSerializer (typeof (Image)); 210 } 211 Wmf(Image img)212 private void Wmf (Image img) 213 { 214 Assert.IsFalse (img is Bitmap, "Bitmap"); 215 Assert.IsTrue (img is Metafile, "Metafile"); 216 // as Image 217 Assert.AreEqual (327683, img.Flags, "Flags"); 218 Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Wmf), "Wmf"); 219 Assert.IsNull (img.Tag, "Tag"); 220 } 221 222 [Test] FromFile_Metafile_Wmf()223 public void FromFile_Metafile_Wmf () 224 { 225 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf"); 226 using (Image img = Image.FromFile (filename)) { 227 Wmf (img); 228 } 229 } 230 231 [Test] FromStream_Metafile_Wmf()232 public void FromStream_Metafile_Wmf () 233 { 234 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf"); 235 using (FileStream fs = File.OpenRead (filename)) { 236 using (Image img = Image.FromStream (fs)) { 237 Wmf (img); 238 } 239 } 240 } 241 242 [Test] 243 [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779 FromStream_Metafile_Wmf_NotOrigin()244 public void FromStream_Metafile_Wmf_NotOrigin () 245 { 246 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf"); 247 using (FileStream fs = File.OpenRead (filename)) { 248 fs.Position = fs.Length / 2; 249 Assert.Throws<ArgumentException> (() => Image.FromStream (fs)); 250 } 251 } 252 Emf(Image img)253 private void Emf (Image img) 254 { 255 Assert.IsFalse (img is Bitmap, "Bitmap"); 256 Assert.IsTrue (img is Metafile, "Metafile"); 257 // as Image 258 Assert.AreEqual (327683, img.Flags, "Flags"); 259 Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Emf), "Emf"); 260 Assert.IsNull (img.Tag, "Tag"); 261 } 262 263 [Test] FromFile_Metafile_Emf()264 public void FromFile_Metafile_Emf () 265 { 266 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf"); 267 using (Image img = Image.FromFile (filename)) { 268 Emf (img); 269 } 270 } 271 272 [Test] FromStream_Metafile_Emf()273 public void FromStream_Metafile_Emf () 274 { 275 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf"); 276 using (FileStream fs = File.OpenRead (filename)) { 277 using (Image img = Image.FromStream (fs)) { 278 Emf (img); 279 } 280 } 281 } 282 283 [Test] 284 [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779 FromStream_Metafile_Emf_NotOrigin()285 public void FromStream_Metafile_Emf_NotOrigin () 286 { 287 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf"); 288 using (FileStream fs = File.OpenRead (filename)) { 289 fs.Position = fs.Length / 2; 290 Assert.Throws<ArgumentException> (() => Image.FromStream (fs)); 291 } 292 } 293 294 [Test] FromFile_Invalid()295 public void FromFile_Invalid () 296 { 297 string filename = Assembly.GetExecutingAssembly ().Location; 298 Assert.Throws<OutOfMemoryException> (() => Image.FromFile (filename)); 299 } 300 301 [Test] FromStream_Invalid()302 public void FromStream_Invalid () 303 { 304 string filename = Assembly.GetExecutingAssembly ().Location; 305 using (FileStream fs = File.OpenRead (filename)) { 306 Assert.Throws<ArgumentException> (() => Image.FromStream (fs)); 307 } 308 } 309 GetBitmap()310 private Bitmap GetBitmap () 311 { 312 Bitmap bmp = new Bitmap (20, 10, PixelFormat.Format24bppRgb); 313 using (Graphics g = Graphics.FromImage (bmp)) 314 { 315 Pen pen = new Pen (Color.Black, 3); 316 g.DrawRectangle (pen, 0, 0, 5, 10); 317 } 318 return bmp; 319 } 320 321 [Test] StreamSaveLoad()322 public void StreamSaveLoad () 323 { 324 using (MemoryStream ms = new MemoryStream ()) { 325 using (Bitmap bmp = GetBitmap ()) { 326 Assert.AreEqual (0, ms.Position, "Position-1"); 327 bmp.Save (ms, ImageFormat.Bmp); 328 Assert.IsTrue (ms.Position > 0, "Position-2"); 329 330 ms.Position = ms.Length; 331 Assert.AreEqual (ms.Length, ms.Position, "Position-3"); 332 333 Bitmap bmp2 = (Bitmap)Image.FromStream (ms); 334 Assert.IsTrue (ms.Position > 20, "Position-4"); 335 336 Assert.IsTrue (bmp2.RawFormat.Equals (ImageFormat.Bmp), "Bmp"); 337 338 Assert.AreEqual (bmp.GetPixel (0, 0), bmp2.GetPixel (0, 0), "0,0"); 339 Assert.AreEqual (bmp.GetPixel (10, 0), bmp2.GetPixel (10, 0), "10,0"); 340 } 341 } 342 } 343 344 [Test] StreamJunkSaveLoad()345 public void StreamJunkSaveLoad () 346 { 347 using (MemoryStream ms = new MemoryStream ()) { 348 // junk 349 ms.WriteByte (0xff); 350 ms.WriteByte (0xef); 351 Assert.AreEqual (2, ms.Position, "Position-1"); 352 353 using (Bitmap bmp = GetBitmap ()) { 354 bmp.Save (ms, ImageFormat.Bmp); 355 Assert.IsTrue (ms.Position > 2, "Position-2"); 356 // exception here 357 Assert.Throws<ArgumentException> (() => Image.FromStream (ms)); 358 } 359 } 360 } 361 362 [Test] XmlSerialization()363 public void XmlSerialization () 364 { 365 new XmlSerializer (typeof (Image)); 366 } 367 } 368 } 369