TestPointConverter.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // Tests for System.Drawing.PointConverter.cs
  3. //
  4. // Author:
  5. // Ravindra ([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.Collections;
  33. using System.ComponentModel;
  34. using System.Globalization;
  35. namespace MonoTests.System.Drawing
  36. {
  37. [TestFixture]
  38. public class PointConverterTest : Assertion
  39. {
  40. Point pt;
  41. Point ptneg;
  42. PointConverter ptconv;
  43. String ptStr;
  44. String ptnegStr;
  45. [TearDown]
  46. public void TearDown () {}
  47. [SetUp]
  48. public void SetUp ()
  49. {
  50. pt = new Point (1, 2);
  51. ptStr = pt.X + ", " + pt.Y;
  52. ptneg = new Point (-2, -3);
  53. ptnegStr = ptneg.X + ", " + ptneg.Y;
  54. ptconv = (PointConverter) TypeDescriptor.GetConverter (pt);
  55. }
  56. [Test]
  57. public void TestCanConvertFrom ()
  58. {
  59. Assert ("CCF#1", ptconv.CanConvertFrom (typeof (String)));
  60. Assert ("CCF#1a", ptconv.CanConvertFrom (null, typeof (String)));
  61. Assert ("CCF#2", ! ptconv.CanConvertFrom (null, typeof (Rectangle)));
  62. Assert ("CCF#3", ! ptconv.CanConvertFrom (null, typeof (RectangleF)));
  63. Assert ("CCF#4", ! ptconv.CanConvertFrom (null, typeof (Point)));
  64. Assert ("CCF#5", ! ptconv.CanConvertFrom (null, typeof (PointF)));
  65. Assert ("CCF#6", ! ptconv.CanConvertFrom (null, typeof (Size)));
  66. Assert ("CCF#7", ! ptconv.CanConvertFrom (null, typeof (SizeF)));
  67. Assert ("CCF#8", ! ptconv.CanConvertFrom (null, typeof (Object)));
  68. Assert ("CCF#9", ! ptconv.CanConvertFrom (null, typeof (int)));
  69. }
  70. [Test]
  71. public void TestCanConvertTo ()
  72. {
  73. Assert ("CCT#1", ptconv.CanConvertTo (typeof (String)));
  74. Assert ("CCT#1a", ptconv.CanConvertTo (null, typeof (String)));
  75. Assert ("CCT#2", ! ptconv.CanConvertTo (null, typeof (Rectangle)));
  76. Assert ("CCT#3", ! ptconv.CanConvertTo (null, typeof (RectangleF)));
  77. Assert ("CCT#4", ! ptconv.CanConvertTo (null, typeof (Point)));
  78. Assert ("CCT#5", ! ptconv.CanConvertTo (null, typeof (PointF)));
  79. Assert ("CCT#6", ! ptconv.CanConvertTo (null, typeof (Size)));
  80. Assert ("CCT#7", ! ptconv.CanConvertTo (null, typeof (SizeF)));
  81. Assert ("CCT#8", ! ptconv.CanConvertTo (null, typeof (Object)));
  82. Assert ("CCT#9", ! ptconv.CanConvertTo (null, typeof (int)));
  83. }
  84. [Test]
  85. public void TestConvertFrom ()
  86. {
  87. AssertEquals ("CF#1", pt, (Point) ptconv.ConvertFrom (null,
  88. CultureInfo.InvariantCulture,
  89. "1, 2"));
  90. AssertEquals ("CF#2", ptneg, (Point) ptconv.ConvertFrom (null,
  91. CultureInfo.InvariantCulture,
  92. "-2, -3"));
  93. try {
  94. ptconv.ConvertFrom (null, CultureInfo.InvariantCulture, "1");
  95. Fail ("CF#3: must throw ArgumentException");
  96. } catch (Exception e) {
  97. Assert ("CF#3", e is ArgumentException);
  98. }
  99. try {
  100. ptconv.ConvertFrom ("1");
  101. Fail ("CF#3a: must throw ArgumentException");
  102. } catch (Exception e) {
  103. Assert ("CF#3a", e is ArgumentException);
  104. }
  105. try {
  106. ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
  107. "1, 1, 1");
  108. Fail ("CF#4: must throw ArgumentException");
  109. } catch (Exception e) {
  110. Assert ("CF#4", e is ArgumentException);
  111. }
  112. try {
  113. ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
  114. "*1, 1");
  115. Fail ("CF#5: must throw Exception");
  116. } catch {
  117. }
  118. try {
  119. ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
  120. new Point (1, 1));
  121. Fail ("CF#6: must throw NotSupportedException");
  122. } catch (Exception e) {
  123. Assert ("CF#6", e is NotSupportedException);
  124. }
  125. try {
  126. ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
  127. new PointF (1, 1));
  128. Fail ("CF#7: must throw NotSupportedException");
  129. } catch (Exception e) {
  130. Assert ("CF#7", e is NotSupportedException);
  131. }
  132. try {
  133. ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
  134. new Size (1, 1));
  135. Fail ("CF#8: must throw NotSupportedException");
  136. } catch (Exception e) {
  137. Assert ("CF#8", e is NotSupportedException);
  138. }
  139. try {
  140. ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
  141. new SizeF (1, 1));
  142. Fail ("CF#9: must throw NotSupportedException");
  143. } catch (Exception e) {
  144. Assert ("CF#9", e is NotSupportedException);
  145. }
  146. try {
  147. ptconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
  148. Fail ("CF#10: must throw NotSupportedException");
  149. } catch (Exception e) {
  150. Assert ("CF#10", e is NotSupportedException);
  151. }
  152. }
  153. [Test]
  154. public void TestConvertTo ()
  155. {
  156. AssertEquals ("CT#1", ptStr, (String) ptconv.ConvertTo (null,
  157. CultureInfo.InvariantCulture,
  158. pt, typeof (String)));
  159. AssertEquals ("CT#2", ptnegStr, (String) ptconv.ConvertTo (null,
  160. CultureInfo.InvariantCulture,
  161. ptneg, typeof (String)));
  162. try {
  163. ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
  164. typeof (Size));
  165. Fail ("CT#3: must throw NotSupportedException");
  166. } catch (Exception e) {
  167. Assert ("CT#3", e is NotSupportedException);
  168. }
  169. try {
  170. ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
  171. typeof (SizeF));
  172. Fail ("CT#4: must throw NotSupportedException");
  173. } catch (Exception e) {
  174. Assert ("CT#4", e is NotSupportedException);
  175. }
  176. try {
  177. ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
  178. typeof (Point));
  179. Fail ("CT#5: must throw NotSupportedException");
  180. } catch (Exception e) {
  181. Assert ("CT#5", e is NotSupportedException);
  182. }
  183. try {
  184. ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
  185. typeof (PointF));
  186. Fail ("CT#6: must throw NotSupportedException");
  187. } catch (Exception e) {
  188. Assert ("CT#6", e is NotSupportedException);
  189. }
  190. try {
  191. ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
  192. typeof (int));
  193. Fail ("CT#7: must throw NotSupportedException");
  194. } catch (Exception e) {
  195. Assert ("CT#7", e is NotSupportedException);
  196. }
  197. }
  198. [Test]
  199. public void TestGetCreateInstanceSupported ()
  200. {
  201. Assert ("GCIS#1", ptconv.GetCreateInstanceSupported ());
  202. Assert ("GCIS#2", ptconv.GetCreateInstanceSupported (null));
  203. }
  204. [Test]
  205. public void TestCreateInstance ()
  206. {
  207. Point ptInstance;
  208. Hashtable ht = new Hashtable ();
  209. ht.Add ("X", 1); ht.Add ("Y", 2);
  210. ptInstance = (Point) ptconv.CreateInstance (ht);
  211. AssertEquals ("CI#1", pt, ptInstance);
  212. ht.Clear ();
  213. ht.Add ("X", -2); ht.Add ("Y", -3);
  214. ptInstance = (Point) ptconv.CreateInstance (null, ht);
  215. AssertEquals ("CI#2", ptneg, ptInstance);
  216. // Property names are case-sensitive. It should throw
  217. // NullRefExc if any of the property names does not match
  218. ht.Clear ();
  219. ht.Add ("x", 2); ht.Add ("Y", 3);
  220. try {
  221. ptInstance = (Point) ptconv.CreateInstance (null, ht);
  222. Fail ("CI#3: must throw NullReferenceException");
  223. } catch (Exception e) {
  224. Assert ("CI#3", e is NullReferenceException);
  225. }
  226. }
  227. [Test]
  228. public void TestGetPropertiesSupported ()
  229. {
  230. Assert ("GPS#1", ptconv.GetPropertiesSupported ());
  231. Assert ("GPS#2", ptconv.GetPropertiesSupported (null));
  232. }
  233. [Test]
  234. [Ignore ("This test fails because of bug #58435")]
  235. public void TestGetProperties ()
  236. {
  237. Attribute [] attrs;
  238. PropertyDescriptorCollection propsColl;
  239. propsColl = ptconv.GetProperties (pt);
  240. AssertEquals ("GP1#1", 2, propsColl.Count);
  241. AssertEquals ("GP1#2", pt.X, propsColl ["X"].GetValue (pt));
  242. AssertEquals ("GP1#3", pt.Y, propsColl ["Y"].GetValue (pt));
  243. propsColl = ptconv.GetProperties (null, ptneg);
  244. AssertEquals ("GP2#1", 2, propsColl.Count);
  245. AssertEquals ("GP2#2", ptneg.X, propsColl ["X"].GetValue (ptneg));
  246. AssertEquals ("GP2#3", ptneg.Y, propsColl ["Y"].GetValue (ptneg));
  247. propsColl = ptconv.GetProperties (null, pt, null);
  248. AssertEquals ("GP3#1", 3, propsColl.Count);
  249. AssertEquals ("GP3#2", pt.X, propsColl ["X"].GetValue (pt));
  250. AssertEquals ("GP3#3", pt.Y, propsColl ["Y"].GetValue (pt));
  251. AssertEquals ("GP3#4", pt.IsEmpty, propsColl ["IsEmpty"].GetValue (pt));
  252. Type type = typeof (Point);
  253. attrs = Attribute.GetCustomAttributes (type, true);
  254. propsColl = ptconv.GetProperties (null, pt, attrs);
  255. AssertEquals ("GP3#5", 0, propsColl.Count);
  256. }
  257. }
  258. }