PaddingConverterTest.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2006 Novell, Inc.
  21. //
  22. #if NET_2_0
  23. using System;
  24. using System.ComponentModel;
  25. using System.Globalization;
  26. using System.Windows.Forms;
  27. using System.Windows.Forms.Layout;
  28. using NUnit.Framework;
  29. using System.Collections;
  30. using System.ComponentModel.Design.Serialization;
  31. using System.Collections.Generic;
  32. namespace MonoTests.System.Windows.Forms
  33. {
  34. [TestFixture]
  35. public class PaddingConverterTest : TestHelper
  36. {
  37. [Test]
  38. public void CanConvertFrom ()
  39. {
  40. PaddingConverter c = new PaddingConverter ();
  41. Assert.IsTrue (c.CanConvertFrom (null, typeof (string)), "1");
  42. Assert.IsFalse (c.CanConvertFrom (null, typeof (int)), "2");
  43. Assert.IsFalse (c.CanConvertFrom (null, typeof (float)), "3");
  44. Assert.IsFalse (c.CanConvertFrom (null, typeof (object)), "4");
  45. }
  46. [Test]
  47. public void CanConvertTo ()
  48. {
  49. PaddingConverter c = new PaddingConverter ();
  50. Assert.IsTrue (c.CanConvertTo (null, typeof (string)), "1");
  51. Assert.IsFalse (c.CanConvertTo (null, typeof (int)), "2");
  52. Assert.IsFalse (c.CanConvertTo (null, typeof (float)), "3");
  53. Assert.IsFalse (c.CanConvertTo (null, typeof (object)), "4");
  54. }
  55. [Test]
  56. public void RoundTrip ()
  57. {
  58. Padding p1 = new Padding (1, 2, 3, 4);
  59. Padding p2 = new Padding (1);
  60. Padding p3 = new Padding ();
  61. Assert.AreEqual (p1, RoundTripPadding (p1), "B1");
  62. Assert.AreEqual (p2, RoundTripPadding (p2), "B2");
  63. Assert.AreEqual (p3, RoundTripPadding (p3), "B3");
  64. }
  65. [Test]
  66. public void ConvertFrom ()
  67. {
  68. string listSeparator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
  69. PaddingConverter pc = new PaddingConverter ();
  70. Assert.AreEqual (new Padding (1, 2, 3, 4), pc.ConvertFrom (
  71. string.Format ("1{0} 2{0} 3{0} 4", listSeparator)), "A1");
  72. Assert.AreEqual (new Padding (1, 2, 3, 4), pc.ConvertFrom (
  73. string.Format ("1{0}2{0}3{0}4", listSeparator)), "A2");
  74. Assert.AreEqual (new Padding (1, 2, 3, 4), pc.ConvertFrom (
  75. string.Format ("1{0} 2{0} 3{0} 4", listSeparator)), "A3");
  76. Assert.AreEqual (new Padding (1), pc.ConvertFrom (string.Format (
  77. "1{0} 1{0} 1{0} 1", listSeparator)), "A4");
  78. Assert.AreEqual (new Padding (), pc.ConvertFrom (string.Format (
  79. "0{0} 0{0} 0{0} 0", listSeparator)), "A5");
  80. }
  81. [Test]
  82. public void ConvertTo ()
  83. {
  84. PaddingConverter pc = new PaddingConverter ();
  85. Assert.AreEqual (string.Format (CultureInfo.CurrentCulture,
  86. "1{0} 2{0} 3{0} 4", CultureInfo.CurrentCulture.TextInfo.ListSeparator),
  87. (string) pc.ConvertTo (new Padding (1, 2, 3, 4), typeof (string)), "A1");
  88. Assert.AreEqual (string.Format (CultureInfo.CurrentCulture,
  89. "1{0} 1{0} 1{0} 1", CultureInfo.CurrentCulture.TextInfo.ListSeparator),
  90. (string) pc.ConvertTo (new Padding (1), typeof (string)), "A2");
  91. Assert.AreEqual (string.Format (CultureInfo.CurrentCulture,
  92. "0{0} 0{0} 0{0} 0", CultureInfo.CurrentCulture.TextInfo.ListSeparator),
  93. (string) pc.ConvertTo (Padding.Empty, typeof (string)), "A3");
  94. }
  95. private Padding RoundTripPadding (Padding p)
  96. {
  97. PaddingConverter pc = new PaddingConverter ();
  98. string s = (string)pc.ConvertTo (p, typeof (string));
  99. return (Padding)pc.ConvertFrom (s);
  100. }
  101. [Test]
  102. public void CreateInstanceSupported ()
  103. {
  104. PaddingConverter pc = new PaddingConverter ();
  105. Assert.AreEqual (true, pc.GetCreateInstanceSupported (null), "A1");
  106. Assert.AreEqual (true, pc.GetPropertiesSupported (null), "A2");
  107. }
  108. [Test]
  109. public void ConvertTo_InstanceDescriptor()
  110. {
  111. PaddingConverter c = new PaddingConverter();
  112. Padding originalPadding = new Padding (1, 10, 5, 9);
  113. InstanceDescriptor instanceDescriptor = (InstanceDescriptor) c.ConvertTo (originalPadding,
  114. typeof (InstanceDescriptor));
  115. Padding resultedPadding = (Padding) instanceDescriptor.Invoke ();
  116. Assert.AreEqual (originalPadding, resultedPadding, "#1");
  117. originalPadding = new Padding (99);
  118. instanceDescriptor = (InstanceDescriptor) c.ConvertTo (originalPadding,
  119. typeof (InstanceDescriptor));
  120. resultedPadding = (Padding) instanceDescriptor.Invoke ();
  121. Assert.AreEqual (originalPadding, resultedPadding, "#2");
  122. }
  123. #region FakeITypeDescriptorContext
  124. class FakeITypeDescriptorContext : ITypeDescriptorContext
  125. {
  126. // Only the Instance and PropertyDescriptor members are required for testing.
  127. //
  128. PropertyDescriptor propertyDescriptor;
  129. Object instance;
  130. internal FakeITypeDescriptorContext (PropertyDescriptor pd, object instance)
  131. {
  132. if (pd == null)
  133. throw new ArgumentNullException ("pd");
  134. if (instance == null)
  135. throw new ArgumentNullException ("instance");
  136. propertyDescriptor = pd;
  137. this.instance = instance;
  138. }
  139. #region ITypeDescriptorContext Members
  140. IContainer ITypeDescriptorContext.Container {
  141. get { throw new NotImplementedException (); }
  142. }
  143. object ITypeDescriptorContext.Instance {
  144. get { return instance; }
  145. }
  146. void ITypeDescriptorContext.OnComponentChanged ()
  147. {
  148. throw new NotImplementedException ();
  149. }
  150. bool ITypeDescriptorContext.OnComponentChanging ()
  151. {
  152. throw new NotImplementedException ();
  153. }
  154. PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor {
  155. get { return propertyDescriptor; }
  156. }
  157. #endregion
  158. #region IServiceProvider Members
  159. object IServiceProvider.GetService (Type serviceType)
  160. {
  161. throw new NotImplementedException ();
  162. }
  163. #endregion
  164. }
  165. #endregion
  166. class MyObjectWithMarginProperty
  167. {
  168. private Padding margin;
  169. public Padding Margin {
  170. get { return margin; }
  171. set { margin = value; }
  172. }
  173. }
  174. private static ITypeDescriptorContext GetTypeDescriptorContext (Padding paddingValue)
  175. {
  176. MyObjectWithMarginProperty obj = new MyObjectWithMarginProperty();
  177. obj.Margin = paddingValue;
  178. PropertyDescriptor pd = TypeDescriptor.GetProperties (obj)["Margin"];
  179. return new FakeITypeDescriptorContext (pd, obj);
  180. }
  181. private static Hashtable GetPropertiesTable (int all, int left, int top, int right, int bottom)
  182. {
  183. Hashtable newValues = new Hashtable();
  184. newValues.Add ("All", all);
  185. newValues.Add ("Left", left);
  186. newValues.Add ("Right", right);
  187. newValues.Add ("Top", top);
  188. newValues.Add ("Bottom", bottom);
  189. return newValues;
  190. }
  191. [Test]
  192. public void CreateInstance ()
  193. {
  194. PaddingConverter c = new PaddingConverter();
  195. Padding modified, expected;
  196. // Non-"All" Tests
  197. //
  198. ITypeDescriptorContext context = GetTypeDescriptorContext (new Padding (1, 2, 30, 40));
  199. modified = (Padding) c.CreateInstance (context, GetPropertiesTable (-1, 1, 2, 30, 40));
  200. expected = new Padding (1, 2, 30, 40);
  201. Assert.AreEqual (expected, modified, "NonAll_NoChange");
  202. modified = (Padding) c.CreateInstance (context, GetPropertiesTable (-1, 111, 2, 30, 40));
  203. expected = new Padding (111, 2, 30, 40);
  204. Assert.AreEqual (expected, modified, "NonAll_ChangeLeft");
  205. modified = (Padding) c.CreateInstance (context, GetPropertiesTable (-1, 1, 222, 30, 40));
  206. expected = new Padding (1, 222, 30, 40);
  207. Assert.AreEqual (expected, modified, "NonAll_ChangeTop");
  208. modified = (Padding) c.CreateInstance (context, GetPropertiesTable (555, 1, 2, 30, 40));
  209. expected = new Padding (555);
  210. Assert.AreEqual (expected, modified, "NonAll_ChangeAll");
  211. // "All" tests
  212. //
  213. context = GetTypeDescriptorContext (new Padding (1));
  214. modified = (Padding) c.CreateInstance (context, GetPropertiesTable (1, 1, 1, 1, 1));
  215. expected = new Padding (1, 1, 1, 1);
  216. Assert.AreEqual (expected, modified, "All_NoChange");
  217. modified = (Padding) c.CreateInstance (context, GetPropertiesTable (1, 111, 1, 1, 1));
  218. expected = new Padding (111, 1, 1, 1);
  219. Assert.AreEqual (expected, modified, "All_ChangeLeft");
  220. modified = (Padding) c.CreateInstance (context, GetPropertiesTable (1, 1, 222, 1, 1));
  221. expected = new Padding (1, 222, 1, 1);
  222. Assert.AreEqual (expected, modified, "All_ChangeTop");
  223. modified = (Padding) c.CreateInstance (context, GetPropertiesTable (555, 1, 1, 1, 1));
  224. expected = new Padding (555);
  225. Assert.AreEqual (expected, modified, "All_ChangeAll");
  226. }
  227. [Test]
  228. public void CreateInstance_NullArguments ()
  229. {
  230. PaddingConverter c = new PaddingConverter ();
  231. try {
  232. c.CreateInstance (null, GetPropertiesTable (1, 1, 1, 1, 1));
  233. Assert.Fail ("#1");
  234. } catch (ArgumentNullException ex) { }
  235. try {
  236. c.CreateInstance (GetTypeDescriptorContext (Padding.Empty), null);
  237. Assert.Fail ("#2");
  238. } catch (ArgumentNullException ex) { }
  239. }
  240. }
  241. }
  242. #endif