TestImageConverter.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. //
  2. // Tests for System.Drawing.ImageConverter.cs
  3. //
  4. // Author:
  5. // Sanjay Gupta ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.Drawing;
  32. using System.Drawing.Imaging;
  33. using System.Collections;
  34. using System.ComponentModel;
  35. using System.Globalization;
  36. using System.IO;
  37. namespace MonoTests.System.Drawing
  38. {
  39. [TestFixture]
  40. public class ImageConverterTest
  41. {
  42. Image image;
  43. ImageConverter imgConv;
  44. ImageConverter imgConvFrmTD;
  45. String imageStr;
  46. byte [] imageBytes;
  47. [TearDown]
  48. public void TearDown () {}
  49. [SetUp]
  50. public void SetUp ()
  51. {
  52. image = Image.FromFile (TestBitmap.getInFile ("bitmaps/almogaver24bits.bmp"));
  53. imageStr = image.ToString ();
  54. imgConv = new ImageConverter();
  55. imgConvFrmTD = (ImageConverter) TypeDescriptor.GetConverter (image);
  56. Stream stream = new FileStream (TestBitmap.getInFile ("bitmaps/almogaver24bits1.bmp"), FileMode.Open);
  57. int length = (int) stream.Length;
  58. imageBytes = new byte [length];
  59. try {
  60. if (stream.Read (imageBytes, 0, length) != length)
  61. Assert.Fail ("SU#1: Read Failure");
  62. } catch (Exception e) {
  63. Assert.Fail ("SU#2 Exception thrown while reading. Exception is: "+e.Message);
  64. } finally {
  65. stream.Close ();
  66. }
  67. stream.Close ();
  68. }
  69. [Test]
  70. public void TestCanConvertFrom ()
  71. {
  72. Assert.IsTrue (imgConv.CanConvertFrom (typeof (byte [])), "CCF#1");
  73. Assert.IsTrue (imgConv.CanConvertFrom (null, typeof (byte [])), "CCF#1a");
  74. Assert.IsTrue (imgConv.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1b");
  75. Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (String)), "CCF#2");
  76. Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
  77. Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Point)), "CCF#4");
  78. Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (PointF)), "CCF#5");
  79. Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Size)), "CCF#6");
  80. Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (SizeF)), "CCF#7");
  81. Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Object)), "CCF#8");
  82. Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (int)), "CCF#9");
  83. Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Metafile)), "CCF#10");
  84. Assert.IsTrue (imgConvFrmTD.CanConvertFrom (typeof (byte [])), "CCF#1A");
  85. Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, typeof (byte [])), "CCF#1aA");
  86. Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1bA");
  87. Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#2A");
  88. Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Rectangle)), "CCF#3A");
  89. Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Point)), "CCF#4A");
  90. Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (PointF)), "CCF#5A");
  91. Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Size)), "CCF#6A");
  92. Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (SizeF)), "CCF#7A");
  93. Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#8A");
  94. Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#9A");
  95. Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Metafile)), "CCF#10A");
  96. }
  97. [Test]
  98. public void TestCanConvertTo ()
  99. {
  100. Assert.IsTrue (imgConv.CanConvertTo (typeof (String)), "CCT#1");
  101. Assert.IsTrue (imgConv.CanConvertTo (null, typeof (String)), "CCT#1a");
  102. Assert.IsTrue (imgConv.CanConvertTo (null, imageStr.GetType ()), "CCT#1b");
  103. Assert.IsTrue (imgConv.CanConvertTo (typeof (byte [])), "CCT#2");
  104. Assert.IsTrue (imgConv.CanConvertTo (null, typeof (byte [])), "CCT#2a");
  105. Assert.IsTrue (imgConv.CanConvertTo (null, imageBytes.GetType ()), "CCT#2b");
  106. Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
  107. Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Point)), "CCT#4");
  108. Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (PointF)), "CCT#5");
  109. Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Size)), "CCT#6");
  110. Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (SizeF)), "CCT#7");
  111. Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Object)), "CCT#8");
  112. Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (int)), "CCT#9");
  113. Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
  114. Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
  115. Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageStr.GetType ()), "CCT#1bA");
  116. Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (byte [])), "CCT#2A");
  117. Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (byte [])), "CCT#2aA");
  118. Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageBytes.GetType ()), "CCT#2bA");
  119. Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Rectangle)), "CCT#3A");
  120. Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Point)), "CCT#4A");
  121. Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (PointF)), "CCT#5A");
  122. Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Size)), "CCT#6A");
  123. Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (SizeF)), "CCT#7A");
  124. Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#8A");
  125. Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#9A");
  126. }
  127. [Test]
  128. public void TestConvertFrom ()
  129. {
  130. Image newImage = (Image) imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
  131. Assert.AreEqual (image.Height, newImage.Height, "CF#1");
  132. Assert.AreEqual (image.Width, newImage.Width, "CF#1a");
  133. try {
  134. imgConv.ConvertFrom ("System.Drawing.String");
  135. Assert.Fail ("CF#2: must throw NotSupportedException");
  136. } catch (Exception e) {
  137. Assert.IsTrue (e is NotSupportedException, "CF#2");
  138. }
  139. try {
  140. imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
  141. "System.Drawing.String");
  142. Assert.Fail ("CF#2a: must throw NotSupportedException");
  143. } catch (Exception e) {
  144. Assert.IsTrue (e is NotSupportedException, "CF#2a");
  145. }
  146. try {
  147. imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
  148. new Bitmap (20, 20));
  149. Assert.Fail ("CF#3: must throw NotSupportedException");
  150. } catch (Exception e) {
  151. Assert.IsTrue (e is NotSupportedException, "CF#3");
  152. }
  153. try {
  154. imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
  155. new Point (10, 10));
  156. Assert.Fail ("CF#4: must throw NotSupportedException");
  157. } catch (Exception e) {
  158. Assert.IsTrue (e is NotSupportedException, "CF#4");
  159. }
  160. try {
  161. imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
  162. new SizeF (10, 10));
  163. Assert.Fail ("CF#5: must throw NotSupportedException");
  164. } catch (Exception e) {
  165. Assert.IsTrue (e is NotSupportedException, "CF#5");
  166. }
  167. try {
  168. imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
  169. new Object ());
  170. Assert.Fail ("CF#6: must throw NotSupportedException");
  171. } catch (Exception e) {
  172. Assert.IsTrue (e is NotSupportedException, "CF#6");
  173. }
  174. newImage = (Image) imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
  175. Assert.AreEqual (image.Height, newImage.Height, "CF#1A");
  176. Assert.AreEqual (image.Width, newImage.Width, "CF#1aA");
  177. try {
  178. imgConvFrmTD.ConvertFrom ("System.Drawing.String");
  179. Assert.Fail ("CF#2A: must throw NotSupportedException");
  180. } catch (Exception e) {
  181. Assert.IsTrue (e is NotSupportedException, "CF#2A");
  182. }
  183. try {
  184. imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
  185. "System.Drawing.String");
  186. Assert.Fail ("CF#2aA: must throw NotSupportedException");
  187. } catch (Exception e) {
  188. Assert.IsTrue (e is NotSupportedException, "CF#2aA");
  189. }
  190. try {
  191. imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
  192. new Bitmap (20, 20));
  193. Assert.Fail ("CF#3A: must throw NotSupportedException");
  194. } catch (Exception e) {
  195. Assert.IsTrue (e is NotSupportedException, "CF#3A");
  196. }
  197. try {
  198. imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
  199. new Point (10, 10));
  200. Assert.Fail ("CF#4A: must throw NotSupportedException");
  201. } catch (Exception e) {
  202. Assert.IsTrue (e is NotSupportedException, "CF#4A");
  203. }
  204. try {
  205. imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
  206. new SizeF (10, 10));
  207. Assert.Fail ("CF#5A: must throw NotSupportedException");
  208. } catch (Exception e) {
  209. Assert.IsTrue (e is NotSupportedException, "CF#5A");
  210. }
  211. try {
  212. imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
  213. new Object ());
  214. Assert.Fail ("CF#6A: must throw NotSupportedException");
  215. } catch (Exception e) {
  216. Assert.IsTrue (e is NotSupportedException, "CF#6A");
  217. }
  218. }
  219. [Test]
  220. public void TestConvertTo ()
  221. {
  222. Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (null,
  223. CultureInfo.InvariantCulture,
  224. image, typeof (String)), "CT#1");
  225. Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (image,
  226. typeof (String)), "CT#1a");
  227. /*byte [] newImageBytes = (byte []) imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
  228. image, imageBytes.GetType ());
  229. Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2");
  230. newImageBytes = (byte []) imgConv.ConvertTo (image, imageBytes.GetType ());
  231. Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2a");
  232. try {
  233. imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
  234. image, typeof (Rectangle));
  235. Assert.Fail ("CT#3: must throw NotSupportedException");
  236. } catch (Exception e) {
  237. Assert.IsTrue (e is NotSupportedException, "CT#3");
  238. }
  239. try {
  240. imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
  241. image, image.GetType ());
  242. Assert.Fail ("CT#4: must throw NotSupportedException");
  243. } catch (Exception e) {
  244. Assert.IsTrue (e is NotSupportedException, "CT#4");
  245. }
  246. try {
  247. imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
  248. image, typeof (Size));
  249. Assert.Fail ("CT#5: must throw NotSupportedException");
  250. } catch (Exception e) {
  251. Assert.IsTrue (e is NotSupportedException, "CT#5");
  252. }
  253. try {
  254. imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
  255. image, typeof (Bitmap));
  256. Assert.Fail ("CT#6: must throw NotSupportedException");
  257. } catch (Exception e) {
  258. Assert.IsTrue (e is NotSupportedException, "CT#6");
  259. }
  260. try {
  261. imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
  262. image, typeof (Point));
  263. Assert.Fail ("CT#7: must throw NotSupportedException");
  264. } catch (Exception e) {
  265. Assert.IsTrue (e is NotSupportedException, "CT#7");
  266. }
  267. try {
  268. imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
  269. image, typeof (Metafile));
  270. Assert.Fail ("CT#8: must throw NotSupportedException");
  271. } catch (Exception e) {
  272. Assert.IsTrue (e is NotSupportedException, "CT#8");
  273. }
  274. try {
  275. imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
  276. image, typeof (Object));
  277. Assert.Fail ("CT#9: must throw NotSupportedException");
  278. } catch (Exception e) {
  279. Assert.IsTrue (e is NotSupportedException, "CT#9");
  280. }
  281. try {
  282. imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
  283. image, typeof (int));
  284. Assert.Fail ("CT#10: must throw NotSupportedException");
  285. } catch (Exception e) {
  286. Assert.IsTrue (e is NotSupportedException, "CT#10");
  287. }
  288. */
  289. Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (null,
  290. CultureInfo.InvariantCulture,
  291. image, typeof (String)), "CT#1A");
  292. Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (image,
  293. typeof (String)), "CT#1aA");
  294. /*newImageBytes = (byte []) imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  295. image, imageBytes.GetType ());
  296. Assert.AreEqual ( imageBytes.Length, newImageBytes.Length, "CT#2A");
  297. newImageBytes = (byte []) imgConvFrmTD.ConvertTo (image, imageBytes.GetType ());
  298. Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2aA");
  299. try {
  300. imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  301. image, typeof (Rectangle));
  302. Assert.Fail ("CT#3A: must throw NotSupportedException");
  303. } catch (Exception e) {
  304. Assert.IsTrue (e is NotSupportedException, "CT#3A");
  305. }
  306. try {
  307. imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  308. image, image.GetType ());
  309. Assert.Fail ("CT#4A: must throw NotSupportedException");
  310. } catch (Exception e) {
  311. Assert.IsTrue (e is NotSupportedException, "CT#4A");
  312. }
  313. try {
  314. imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  315. image, typeof (Size));
  316. Assert.Fail ("CT#5A: must throw NotSupportedException");
  317. } catch (Exception e) {
  318. Assert.IsTrue (e is NotSupportedException, "CT#5A");
  319. }
  320. try {
  321. imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  322. image, typeof (Bitmap));
  323. Assert.Fail ("CT#6A: must throw NotSupportedException");
  324. } catch (Exception e) {
  325. Assert.IsTrue (e is NotSupportedException, "CT#6A");
  326. }
  327. try {
  328. imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  329. image, typeof (Point));
  330. Assert.Fail ("CT#7A: must throw NotSupportedException");
  331. } catch (Exception e) {
  332. Assert.IsTrue (e is NotSupportedException, "CT#7A");
  333. }
  334. try {
  335. imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  336. image, typeof (Metafile));
  337. Assert.Fail ("CT#8A: must throw NotSupportedException");
  338. } catch (Exception e) {
  339. Assert.IsTrue (e is NotSupportedException, "CT#8A");
  340. }
  341. try {
  342. imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  343. image, typeof (Object));
  344. Assert.Fail ("CT#9A: must throw NotSupportedException");
  345. } catch (Exception e) {
  346. Assert.IsTrue (e is NotSupportedException, "CT#9A");
  347. }
  348. try {
  349. imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  350. image, typeof (int));
  351. Assert.Fail ("CT#10A: must throw NotSupportedException");
  352. } catch (Exception e) {
  353. Assert.IsTrue (e is NotSupportedException, "CT#10A");
  354. }*/
  355. }
  356. [Test]
  357. public void TestGetPropertiesSupported ()
  358. {
  359. Assert.IsTrue (imgConv.GetPropertiesSupported (), "GPS#1");
  360. Assert.IsTrue (imgConv.GetPropertiesSupported (null), "GPS#2");
  361. }
  362. [Test]
  363. [Ignore ("This test fails because of bug #58435")]
  364. public void TestGetProperties ()
  365. {
  366. PropertyDescriptorCollection propsColl;
  367. propsColl = imgConv.GetProperties (null, image, null);
  368. Assert.AreEqual (13, propsColl.Count, "GP1#1");
  369. propsColl = imgConv.GetProperties (null, image);
  370. Assert.AreEqual (6, propsColl.Count, "GP1#2");
  371. propsColl = imgConv.GetProperties (image);
  372. Assert.AreEqual (6, propsColl.Count, "GP1#3");
  373. propsColl = TypeDescriptor.GetProperties (typeof (Image));
  374. Assert.AreEqual (13, propsColl.Count, "GP1#4");
  375. propsColl = imgConvFrmTD.GetProperties (null, image, null);
  376. Assert.AreEqual (13, propsColl.Count, "GP1#1A");
  377. propsColl = imgConvFrmTD.GetProperties (null, image);
  378. Assert.AreEqual (6, propsColl.Count, "GP1#2A");
  379. propsColl = imgConvFrmTD.GetProperties (image);
  380. Assert.AreEqual (6, propsColl.Count, "GP1#3A");
  381. }
  382. }
  383. }