TestIconConverter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //
  2. // Tests for System.Drawing.IconConverter.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. using System.Security.Permissions;
  38. namespace MonoTests.System.Drawing
  39. {
  40. [TestFixture]
  41. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  42. public class IconConverterTest
  43. {
  44. Icon icon;
  45. IconConverter icoConv;
  46. IconConverter icoConvFrmTD;
  47. String iconStr;
  48. byte [] iconBytes;
  49. [TearDown]
  50. public void TearDown () {}
  51. [SetUp]
  52. public void SetUp ()
  53. {
  54. icon = new Icon (TestBitmap.getInFile ("bitmaps/VisualPng.ico"));
  55. iconStr = icon.ToString ();
  56. icoConv = new IconConverter();
  57. icoConvFrmTD = (IconConverter) TypeDescriptor.GetConverter (icon);
  58. Stream stream = new FileStream (TestBitmap.getInFile ("bitmaps/VisualPng1.ico"), FileMode.Open);
  59. int length = (int) stream.Length;
  60. iconBytes = new byte [length];
  61. try {
  62. if (stream.Read (iconBytes, 0, length) != length)
  63. Assert.Fail ("SU#1: Read Failure");
  64. } catch (Exception e) {
  65. Assert.Fail ("SU#2 Exception thrown while reading. Exception is: "+e.Message);
  66. } finally {
  67. stream.Close ();
  68. }
  69. stream.Close ();
  70. }
  71. [Test]
  72. public void TestCanConvertFrom ()
  73. {
  74. Assert.IsTrue (icoConv.CanConvertFrom (typeof (byte [])), "CCF#1");
  75. Assert.IsTrue (icoConv.CanConvertFrom (null, typeof (byte [])), "CCF#1a");
  76. Assert.IsTrue (icoConv.CanConvertFrom (null, iconBytes.GetType ()), "CCF#1b");
  77. Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (String)), "CCF#2");
  78. Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
  79. Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Point)), "CCF#4");
  80. Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (PointF)), "CCF#5");
  81. Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Size)), "CCF#6");
  82. Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (SizeF)), "CCF#7");
  83. Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Object)), "CCF#8");
  84. Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (int)), "CCF#9");
  85. Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Metafile)), "CCF#10");
  86. Assert.IsTrue (icoConvFrmTD.CanConvertFrom (typeof (byte [])), "CCF#1A");
  87. Assert.IsTrue (icoConvFrmTD.CanConvertFrom (null, typeof (byte [])), "CCF#1aA");
  88. Assert.IsTrue (icoConvFrmTD.CanConvertFrom (null, iconBytes.GetType ()), "CCF#1bA");
  89. Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#2A");
  90. Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Rectangle)), "CCF#3A");
  91. Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Point)), "CCF#4A");
  92. Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (PointF)), "CCF#5A");
  93. Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Size)), "CCF#6A");
  94. Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (SizeF)), "CCF#7A");
  95. Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#8A");
  96. Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#9A");
  97. Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Metafile)), "CCF#10A");
  98. }
  99. [Test]
  100. public void TestCanConvertTo ()
  101. {
  102. Assert.IsTrue (icoConv.CanConvertTo (typeof (String)), "CCT#1");
  103. Assert.IsTrue (icoConv.CanConvertTo (null, typeof (String)), "CCT#1a");
  104. Assert.IsTrue (icoConv.CanConvertTo (null, iconStr.GetType ()), "CCT#1b");
  105. Assert.IsTrue (icoConv.CanConvertTo (typeof (byte [])), "CCT#2");
  106. Assert.IsTrue (icoConv.CanConvertTo (null, typeof (byte [])), "CCT#2a");
  107. Assert.IsTrue (icoConv.CanConvertTo (null, iconBytes.GetType ()), "CCT#2b");
  108. Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
  109. Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (Point)), "CCT#4");
  110. Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (PointF)), "CCT#5");
  111. Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (Size)), "CCT#6");
  112. Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (SizeF)), "CCT#7");
  113. Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (Object)), "CCT#8");
  114. Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (int)), "CCT#9");
  115. Assert.IsTrue (icoConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
  116. Assert.IsTrue (icoConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
  117. Assert.IsTrue (icoConvFrmTD.CanConvertTo (null, iconStr.GetType ()), "CCT#1bA");
  118. Assert.IsTrue (icoConvFrmTD.CanConvertTo (typeof (byte [])), "CCT#2A");
  119. Assert.IsTrue (icoConvFrmTD.CanConvertTo (null, typeof (byte [])), "CCT#2aA");
  120. Assert.IsTrue (icoConvFrmTD.CanConvertTo (null, iconBytes.GetType ()), "CCT#2bA");
  121. Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (Rectangle)), "CCT#3A");
  122. Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (Point)), "CCT#4A");
  123. Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (PointF)), "CCT#5A");
  124. Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (Size)), "CCT#6A");
  125. Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (SizeF)), "CCT#7A");
  126. Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#8A");
  127. Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#9A");
  128. }
  129. [Test]
  130. public void TestConvertFrom ()
  131. {
  132. Icon newIcon = (Icon) icoConv.ConvertFrom (null, CultureInfo.InvariantCulture, iconBytes);
  133. Assert.AreEqual (icon.Height, newIcon.Height, "CF#1");
  134. Assert.AreEqual (icon.Width, newIcon.Width, "CF#1a" );
  135. try {
  136. icoConv.ConvertFrom ("System.Drawing.String");
  137. Assert.Fail ("CF#2: must throw NotSupportedException");
  138. } catch (Exception e) {
  139. Assert.IsTrue (e is NotSupportedException, "CF#2");
  140. }
  141. try {
  142. icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
  143. "System.Drawing.String");
  144. Assert.Fail ("CF#2a: must throw NotSupportedException");
  145. } catch (Exception e) {
  146. Assert.IsTrue (e is NotSupportedException, "CF#2a");
  147. }
  148. try {
  149. icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
  150. new Bitmap (20, 20));
  151. Assert.Fail ("CF#3: must throw NotSupportedException");
  152. } catch (Exception e) {
  153. Assert.IsTrue (e is NotSupportedException, "CF#3");
  154. }
  155. try {
  156. icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
  157. new Point (10, 10));
  158. Assert.Fail ("CF#4: must throw NotSupportedException");
  159. } catch (Exception e) {
  160. Assert.IsTrue (e is NotSupportedException, "CF#4");
  161. }
  162. try {
  163. icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
  164. new SizeF (10, 10));
  165. Assert.Fail ("CF#5: must throw NotSupportedException");
  166. } catch (Exception e) {
  167. Assert.IsTrue (e is NotSupportedException, "CF#5");
  168. }
  169. try {
  170. icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
  171. new Object ());
  172. Assert.Fail ("CF#6: must throw NotSupportedException");
  173. } catch (Exception e) {
  174. Assert.IsTrue (e is NotSupportedException, "CF#6");
  175. }
  176. newIcon = (Icon) icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, iconBytes);
  177. Assert.AreEqual (icon.Height, newIcon.Height, "CF#1A");
  178. Assert.AreEqual (icon.Width, newIcon.Width, "CF#1Aa");
  179. try {
  180. icoConvFrmTD.ConvertFrom ("System.Drawing.String");
  181. Assert.Fail ("CF#2A: must throw NotSupportedException");
  182. } catch (Exception e) {
  183. Assert.IsTrue (e is NotSupportedException, "CF#2A");
  184. }
  185. try {
  186. icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
  187. "System.Drawing.String");
  188. Assert.Fail ("CF#2aA: must throw NotSupportedException");
  189. } catch (Exception e) {
  190. Assert.IsTrue (e is NotSupportedException, "CF#2aA");
  191. }
  192. try {
  193. icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
  194. new Bitmap (20, 20));
  195. Assert.Fail ("CF#3A: must throw NotSupportedException");
  196. } catch (Exception e) {
  197. Assert.IsTrue (e is NotSupportedException, "CF#3A");
  198. }
  199. try {
  200. icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
  201. new Point (10, 10));
  202. Assert.Fail ("CF#4A: must throw NotSupportedException");
  203. } catch (Exception e) {
  204. Assert.IsTrue (e is NotSupportedException, "CF#4A");
  205. }
  206. try {
  207. icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
  208. new SizeF (10, 10));
  209. Assert.Fail ("CF#5A: must throw NotSupportedException");
  210. } catch (Exception e) {
  211. Assert.IsTrue (e is NotSupportedException, "CF#5A");
  212. }
  213. try {
  214. icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
  215. new Object ());
  216. Assert.Fail ("CF#6A: must throw NotSupportedException");
  217. } catch (Exception e) {
  218. Assert.IsTrue (e is NotSupportedException, "CF#6A");
  219. }
  220. }
  221. [Test]
  222. public void TestConvertTo ()
  223. {
  224. Assert.AreEqual (iconStr, (String) icoConv.ConvertTo (null,
  225. CultureInfo.InvariantCulture,
  226. icon, typeof (String)), "CT#1");
  227. Assert.AreEqual (iconStr, (String) icoConv.ConvertTo (icon,
  228. typeof (String)), "CT#1a");
  229. /*byte [] newIconBytes = (byte []) icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
  230. icon, iconBytes.GetType ());
  231. Assert.AreEqual (iconBytes.Length, newIconBytes.Length, "CT#2");
  232. newIconBytes = (byte []) icoConv.ConvertTo (icon, iconBytes.GetType ());
  233. Assert.AreEqual (iconBytes.Length, newIconBytes.Length, "CT#2a");
  234. try {
  235. icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
  236. icon, typeof (Rectangle));
  237. Assert.Fail ("CT#3: must throw NotSupportedException");
  238. } catch (Exception e) {
  239. Assert.IsTrue ( e is NotSupportedException, "CT#3");
  240. }
  241. try {
  242. icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
  243. icon, icon.GetType ());
  244. Assert.Fail ("CT#4: must throw NotSupportedException");
  245. } catch (Exception e) {
  246. Assert.IsTrue (e is NotSupportedException, "CT#4");
  247. }
  248. try {
  249. icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
  250. icon, typeof (Size));
  251. Assert.Fail ("CT#5: must throw NotSupportedException");
  252. } catch (Exception e) {
  253. Assert.IsTrue (e is NotSupportedException, "CT#5");
  254. }
  255. try {
  256. icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
  257. icon, typeof (Bitmap));
  258. Assert.Fail ("CT#6: must throw NotSupportedException");
  259. } catch (Exception e) {
  260. Assert.IsTrue ( e is NotSupportedException, "CT#6");
  261. }
  262. try {
  263. icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
  264. icon, typeof (Point));
  265. Assert.Fail ("CT#7: must throw NotSupportedException");
  266. } catch (Exception e) {
  267. Assert.IsTrue (e is NotSupportedException, "CT#7");
  268. }
  269. try {
  270. icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
  271. icon, typeof (Metafile));
  272. Assert.Fail ("CT#8: must throw NotSupportedException");
  273. } catch (Exception e) {
  274. Assert.IsTrue (e is NotSupportedException, "CT#8");
  275. }
  276. try {
  277. icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
  278. icon, typeof (Object));
  279. Assert.Fail ("CT#9: must throw NotSupportedException");
  280. } catch (Exception e) {
  281. Assert.IsTrue (e is NotSupportedException, "CT#9");
  282. }
  283. try {
  284. icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
  285. icon, typeof (int));
  286. Assert.Fail ("CT#10: must throw NotSupportedException");
  287. } catch (Exception e) {
  288. Assert.IsTrue (e is NotSupportedException, "CT#10");
  289. }*/
  290. Assert.AreEqual (iconStr, (String) icoConvFrmTD.ConvertTo (null,
  291. CultureInfo.InvariantCulture,
  292. icon, typeof (String)), "CT#1A");
  293. Assert.AreEqual (iconStr, (String) icoConvFrmTD.ConvertTo (icon,
  294. typeof (String)), "CT#1aA");
  295. /*newIconBytes = (byte []) icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  296. icon, iconBytes.GetType ());
  297. Assert.AreEqual (iconBytes.Length, newIconBytes.Length, "CT#2A");
  298. newIconBytes = (byte []) icoConvFrmTD.ConvertTo (icon, iconBytes.GetType ());
  299. Assert.AreEqual (iconBytes.Length, newIconBytes.Length, "CT#2aA");
  300. try {
  301. icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  302. icon, typeof (Rectangle));
  303. Assert.Fail ("CT#3A: must throw NotSupportedException");
  304. } catch (Exception e) {
  305. Assert.IsTrue (e is NotSupportedException, "CT#3A");
  306. }
  307. try {
  308. icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  309. icon, icon.GetType ());
  310. Assert.Fail ("CT#4A: must throw NotSupportedException");
  311. } catch (Exception e) {
  312. Assert.IsTrue (e is NotSupportedException, "CT#4A");
  313. }
  314. try {
  315. icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  316. icon, typeof (Size));
  317. Assert.Fail ("CT#5A: must throw NotSupportedException");
  318. } catch (Exception e) {
  319. Assert.IsTrue (e is NotSupportedException, "CT#5A");
  320. }
  321. try {
  322. icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  323. icon, typeof (Bitmap));
  324. Assert.Fail ("CT#6A: must throw NotSupportedException");
  325. } catch (Exception e) {
  326. Assert.IsTrue (e is NotSupportedException, "CT#6A");
  327. }
  328. try {
  329. icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  330. icon, typeof (Point));
  331. Assert.Fail ("CT#7A: must throw NotSupportedException");
  332. } catch (Exception e) {
  333. Assert.IsTrue (e is NotSupportedException, "CT#7A");
  334. }
  335. try {
  336. icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  337. icon, typeof (Metafile));
  338. Assert.Fail ("CT#8A: must throw NotSupportedException");
  339. } catch (Exception e) {
  340. Assert.IsTrue (e is NotSupportedException, "CT#8A");
  341. }
  342. try {
  343. icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  344. icon, typeof (Object));
  345. Assert.Fail ("CT#9A: must throw NotSupportedException");
  346. } catch (Exception e) {
  347. Assert.IsTrue (e is NotSupportedException, "CT#9A");
  348. }
  349. try {
  350. icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
  351. icon, typeof (int));
  352. Assert.Fail ("CT#10A: must throw NotSupportedException");
  353. } catch (Exception e) {
  354. Assert.IsTrue (e is NotSupportedException, "CT#10A");
  355. }*/
  356. }
  357. }
  358. }