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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2006 Novell, Inc.
26 using System.ComponentModel;
27 using System.Globalization;
28 using System.Windows.Forms;
29 using System.Windows.Forms.Layout;
30 using NUnit.Framework;
31 using System.Collections;
32 using System.ComponentModel.Design.Serialization;
33 using System.Collections.Generic;
35 namespace MonoTests.System.Windows.Forms
38 public class PaddingConverterTest : TestHelper
41 public void CanConvertFrom ()
43 PaddingConverter c = new PaddingConverter ();
45 Assert.IsTrue (c.CanConvertFrom (null, typeof (string)), "1");
46 Assert.IsFalse (c.CanConvertFrom (null, typeof (int)), "2");
47 Assert.IsFalse (c.CanConvertFrom (null, typeof (float)), "3");
48 Assert.IsFalse (c.CanConvertFrom (null, typeof (object)), "4");
52 public void CanConvertTo ()
54 PaddingConverter c = new PaddingConverter ();
56 Assert.IsTrue (c.CanConvertTo (null, typeof (string)), "1");
57 Assert.IsFalse (c.CanConvertTo (null, typeof (int)), "2");
58 Assert.IsFalse (c.CanConvertTo (null, typeof (float)), "3");
59 Assert.IsFalse (c.CanConvertTo (null, typeof (object)), "4");
63 public void RoundTrip ()
65 Padding p1 = new Padding (1, 2, 3, 4);
66 Padding p2 = new Padding (1);
67 Padding p3 = new Padding ();
69 Assert.AreEqual (p1, RoundTripPadding (p1), "B1");
70 Assert.AreEqual (p2, RoundTripPadding (p2), "B2");
71 Assert.AreEqual (p3, RoundTripPadding (p3), "B3");
76 public void ConvertFrom ()
78 string listSeparator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
79 PaddingConverter pc = new PaddingConverter ();
80 Assert.AreEqual (new Padding (1, 2, 3, 4), pc.ConvertFrom (
81 string.Format ("1{0} 2{0} 3{0} 4", listSeparator)), "A1");
82 Assert.AreEqual (new Padding (1, 2, 3, 4), pc.ConvertFrom (
83 string.Format ("1{0}2{0}3{0}4", listSeparator)), "A2");
84 Assert.AreEqual (new Padding (1, 2, 3, 4), pc.ConvertFrom (
85 string.Format ("1{0} 2{0} 3{0} 4", listSeparator)), "A3");
86 Assert.AreEqual (new Padding (1), pc.ConvertFrom (string.Format (
87 "1{0} 1{0} 1{0} 1", listSeparator)), "A4");
88 Assert.AreEqual (new Padding (), pc.ConvertFrom (string.Format (
89 "0{0} 0{0} 0{0} 0", listSeparator)), "A5");
93 public void ConvertTo ()
95 PaddingConverter pc = new PaddingConverter ();
97 Assert.AreEqual (string.Format (CultureInfo.CurrentCulture,
98 "1{0} 2{0} 3{0} 4", CultureInfo.CurrentCulture.TextInfo.ListSeparator),
99 (string) pc.ConvertTo (new Padding (1, 2, 3, 4), typeof (string)), "A1");
100 Assert.AreEqual (string.Format (CultureInfo.CurrentCulture,
101 "1{0} 1{0} 1{0} 1", CultureInfo.CurrentCulture.TextInfo.ListSeparator),
102 (string) pc.ConvertTo (new Padding (1), typeof (string)), "A2");
103 Assert.AreEqual (string.Format (CultureInfo.CurrentCulture,
104 "0{0} 0{0} 0{0} 0", CultureInfo.CurrentCulture.TextInfo.ListSeparator),
105 (string) pc.ConvertTo (Padding.Empty, typeof (string)), "A3");
108 private Padding RoundTripPadding (Padding p)
110 PaddingConverter pc = new PaddingConverter ();
112 string s = (string)pc.ConvertTo (p, typeof (string));
113 return (Padding)pc.ConvertFrom (s);
117 public void CreateInstanceSupported ()
119 PaddingConverter pc = new PaddingConverter ();
121 Assert.AreEqual (true, pc.GetCreateInstanceSupported (null), "A1");
122 Assert.AreEqual (true, pc.GetPropertiesSupported (null), "A2");
126 public void ConvertTo_InstanceDescriptor()
128 PaddingConverter c = new PaddingConverter();
129 Padding originalPadding = new Padding (1, 10, 5, 9);
130 InstanceDescriptor instanceDescriptor = (InstanceDescriptor) c.ConvertTo (originalPadding,
131 typeof (InstanceDescriptor));
132 Padding resultedPadding = (Padding) instanceDescriptor.Invoke ();
133 Assert.AreEqual (originalPadding, resultedPadding, "#1");
135 originalPadding = new Padding (99);
136 instanceDescriptor = (InstanceDescriptor) c.ConvertTo (originalPadding,
137 typeof (InstanceDescriptor));
138 resultedPadding = (Padding) instanceDescriptor.Invoke ();
139 Assert.AreEqual (originalPadding, resultedPadding, "#2");
142 #region FakeITypeDescriptorContext
143 class FakeITypeDescriptorContext : ITypeDescriptorContext
145 // Only the Instance and PropertyDescriptor members are required for testing.
147 PropertyDescriptor propertyDescriptor;
150 internal FakeITypeDescriptorContext (PropertyDescriptor pd, object instance)
153 throw new ArgumentNullException ("pd");
154 if (instance == null)
155 throw new ArgumentNullException ("instance");
156 propertyDescriptor = pd;
157 this.instance = instance;
160 #region ITypeDescriptorContext Members
162 IContainer ITypeDescriptorContext.Container {
163 get { throw new NotImplementedException (); }
166 object ITypeDescriptorContext.Instance {
167 get { return instance; }
170 void ITypeDescriptorContext.OnComponentChanged ()
172 throw new NotImplementedException ();
175 bool ITypeDescriptorContext.OnComponentChanging ()
177 throw new NotImplementedException ();
180 PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor {
181 get { return propertyDescriptor; }
186 #region IServiceProvider Members
188 object IServiceProvider.GetService (Type serviceType)
190 throw new NotImplementedException ();
197 class MyObjectWithMarginProperty
199 private Padding margin;
201 public Padding Margin {
202 get { return margin; }
203 set { margin = value; }
207 private static ITypeDescriptorContext GetTypeDescriptorContext (Padding paddingValue)
209 MyObjectWithMarginProperty obj = new MyObjectWithMarginProperty();
210 obj.Margin = paddingValue;
211 PropertyDescriptor pd = TypeDescriptor.GetProperties (obj)["Margin"];
212 return new FakeITypeDescriptorContext (pd, obj);
215 private static Hashtable GetPropertiesTable (int all, int left, int top, int right, int bottom)
217 Hashtable newValues = new Hashtable();
218 newValues.Add ("All", all);
219 newValues.Add ("Left", left);
220 newValues.Add ("Right", right);
221 newValues.Add ("Top", top);
222 newValues.Add ("Bottom", bottom);
227 public void CreateInstance ()
229 PaddingConverter c = new PaddingConverter();
230 Padding modified, expected;
234 ITypeDescriptorContext context = GetTypeDescriptorContext (new Padding (1, 2, 30, 40));
236 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (-1, 1, 2, 30, 40));
237 expected = new Padding (1, 2, 30, 40);
238 Assert.AreEqual (expected, modified, "NonAll_NoChange");
240 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (-1, 111, 2, 30, 40));
241 expected = new Padding (111, 2, 30, 40);
242 Assert.AreEqual (expected, modified, "NonAll_ChangeLeft");
244 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (-1, 1, 222, 30, 40));
245 expected = new Padding (1, 222, 30, 40);
246 Assert.AreEqual (expected, modified, "NonAll_ChangeTop");
248 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (555, 1, 2, 30, 40));
249 expected = new Padding (555);
250 Assert.AreEqual (expected, modified, "NonAll_ChangeAll");
254 context = GetTypeDescriptorContext (new Padding (1));
256 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (1, 1, 1, 1, 1));
257 expected = new Padding (1, 1, 1, 1);
258 Assert.AreEqual (expected, modified, "All_NoChange");
260 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (1, 111, 1, 1, 1));
261 expected = new Padding (111, 1, 1, 1);
262 Assert.AreEqual (expected, modified, "All_ChangeLeft");
264 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (1, 1, 222, 1, 1));
265 expected = new Padding (1, 222, 1, 1);
266 Assert.AreEqual (expected, modified, "All_ChangeTop");
268 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (555, 1, 1, 1, 1));
269 expected = new Padding (555);
270 Assert.AreEqual (expected, modified, "All_ChangeAll");
274 public void CreateInstance_NullArguments ()
276 PaddingConverter c = new PaddingConverter ();
278 c.CreateInstance (null, GetPropertiesTable (1, 1, 1, 1, 1));
280 } catch (ArgumentNullException ex) { }
282 c.CreateInstance (GetTypeDescriptorContext (Padding.Empty), null);
284 } catch (ArgumentNullException ex) { }