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.
25 using System.ComponentModel;
26 using System.Globalization;
27 using System.Windows.Forms;
28 using System.Windows.Forms.Layout;
29 using NUnit.Framework;
30 using System.Collections;
31 using System.ComponentModel.Design.Serialization;
32 using System.Collections.Generic;
34 namespace MonoTests.System.Windows.Forms
37 public class PaddingConverterTest : TestHelper
40 public void CanConvertFrom ()
42 PaddingConverter c = new PaddingConverter ();
44 Assert.IsTrue (c.CanConvertFrom (null, typeof (string)), "1");
45 Assert.IsFalse (c.CanConvertFrom (null, typeof (int)), "2");
46 Assert.IsFalse (c.CanConvertFrom (null, typeof (float)), "3");
47 Assert.IsFalse (c.CanConvertFrom (null, typeof (object)), "4");
51 public void CanConvertTo ()
53 PaddingConverter c = new PaddingConverter ();
55 Assert.IsTrue (c.CanConvertTo (null, typeof (string)), "1");
56 Assert.IsFalse (c.CanConvertTo (null, typeof (int)), "2");
57 Assert.IsFalse (c.CanConvertTo (null, typeof (float)), "3");
58 Assert.IsFalse (c.CanConvertTo (null, typeof (object)), "4");
62 public void RoundTrip ()
64 Padding p1 = new Padding (1, 2, 3, 4);
65 Padding p2 = new Padding (1);
66 Padding p3 = new Padding ();
68 Assert.AreEqual (p1, RoundTripPadding (p1), "B1");
69 Assert.AreEqual (p2, RoundTripPadding (p2), "B2");
70 Assert.AreEqual (p3, RoundTripPadding (p3), "B3");
75 public void ConvertFrom ()
77 string listSeparator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
78 PaddingConverter pc = new PaddingConverter ();
79 Assert.AreEqual (new Padding (1, 2, 3, 4), pc.ConvertFrom (
80 string.Format ("1{0} 2{0} 3{0} 4", listSeparator)), "A1");
81 Assert.AreEqual (new Padding (1, 2, 3, 4), pc.ConvertFrom (
82 string.Format ("1{0}2{0}3{0}4", listSeparator)), "A2");
83 Assert.AreEqual (new Padding (1, 2, 3, 4), pc.ConvertFrom (
84 string.Format ("1{0} 2{0} 3{0} 4", listSeparator)), "A3");
85 Assert.AreEqual (new Padding (1), pc.ConvertFrom (string.Format (
86 "1{0} 1{0} 1{0} 1", listSeparator)), "A4");
87 Assert.AreEqual (new Padding (), pc.ConvertFrom (string.Format (
88 "0{0} 0{0} 0{0} 0", listSeparator)), "A5");
92 public void ConvertTo ()
94 PaddingConverter pc = new PaddingConverter ();
96 Assert.AreEqual (string.Format (CultureInfo.CurrentCulture,
97 "1{0} 2{0} 3{0} 4", CultureInfo.CurrentCulture.TextInfo.ListSeparator),
98 (string) pc.ConvertTo (new Padding (1, 2, 3, 4), typeof (string)), "A1");
99 Assert.AreEqual (string.Format (CultureInfo.CurrentCulture,
100 "1{0} 1{0} 1{0} 1", CultureInfo.CurrentCulture.TextInfo.ListSeparator),
101 (string) pc.ConvertTo (new Padding (1), typeof (string)), "A2");
102 Assert.AreEqual (string.Format (CultureInfo.CurrentCulture,
103 "0{0} 0{0} 0{0} 0", CultureInfo.CurrentCulture.TextInfo.ListSeparator),
104 (string) pc.ConvertTo (Padding.Empty, typeof (string)), "A3");
107 private Padding RoundTripPadding (Padding p)
109 PaddingConverter pc = new PaddingConverter ();
111 string s = (string)pc.ConvertTo (p, typeof (string));
112 return (Padding)pc.ConvertFrom (s);
116 public void CreateInstanceSupported ()
118 PaddingConverter pc = new PaddingConverter ();
120 Assert.AreEqual (true, pc.GetCreateInstanceSupported (null), "A1");
121 Assert.AreEqual (true, pc.GetPropertiesSupported (null), "A2");
125 public void ConvertTo_InstanceDescriptor()
127 PaddingConverter c = new PaddingConverter();
128 Padding originalPadding = new Padding (1, 10, 5, 9);
129 InstanceDescriptor instanceDescriptor = (InstanceDescriptor) c.ConvertTo (originalPadding,
130 typeof (InstanceDescriptor));
131 Padding resultedPadding = (Padding) instanceDescriptor.Invoke ();
132 Assert.AreEqual (originalPadding, resultedPadding, "#1");
134 originalPadding = new Padding (99);
135 instanceDescriptor = (InstanceDescriptor) c.ConvertTo (originalPadding,
136 typeof (InstanceDescriptor));
137 resultedPadding = (Padding) instanceDescriptor.Invoke ();
138 Assert.AreEqual (originalPadding, resultedPadding, "#2");
141 #region FakeITypeDescriptorContext
142 class FakeITypeDescriptorContext : ITypeDescriptorContext
144 // Only the Instance and PropertyDescriptor members are required for testing.
146 PropertyDescriptor propertyDescriptor;
149 internal FakeITypeDescriptorContext (PropertyDescriptor pd, object instance)
152 throw new ArgumentNullException ("pd");
153 if (instance == null)
154 throw new ArgumentNullException ("instance");
155 propertyDescriptor = pd;
156 this.instance = instance;
159 #region ITypeDescriptorContext Members
161 IContainer ITypeDescriptorContext.Container {
162 get { throw new NotImplementedException (); }
165 object ITypeDescriptorContext.Instance {
166 get { return instance; }
169 void ITypeDescriptorContext.OnComponentChanged ()
171 throw new NotImplementedException ();
174 bool ITypeDescriptorContext.OnComponentChanging ()
176 throw new NotImplementedException ();
179 PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor {
180 get { return propertyDescriptor; }
185 #region IServiceProvider Members
187 object IServiceProvider.GetService (Type serviceType)
189 throw new NotImplementedException ();
196 class MyObjectWithMarginProperty
198 private Padding margin;
200 public Padding Margin {
201 get { return margin; }
202 set { margin = value; }
206 private static ITypeDescriptorContext GetTypeDescriptorContext (Padding paddingValue)
208 MyObjectWithMarginProperty obj = new MyObjectWithMarginProperty();
209 obj.Margin = paddingValue;
210 PropertyDescriptor pd = TypeDescriptor.GetProperties (obj)["Margin"];
211 return new FakeITypeDescriptorContext (pd, obj);
214 private static Hashtable GetPropertiesTable (int all, int left, int top, int right, int bottom)
216 Hashtable newValues = new Hashtable();
217 newValues.Add ("All", all);
218 newValues.Add ("Left", left);
219 newValues.Add ("Right", right);
220 newValues.Add ("Top", top);
221 newValues.Add ("Bottom", bottom);
226 public void CreateInstance ()
228 PaddingConverter c = new PaddingConverter();
229 Padding modified, expected;
233 ITypeDescriptorContext context = GetTypeDescriptorContext (new Padding (1, 2, 30, 40));
235 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (-1, 1, 2, 30, 40));
236 expected = new Padding (1, 2, 30, 40);
237 Assert.AreEqual (expected, modified, "NonAll_NoChange");
239 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (-1, 111, 2, 30, 40));
240 expected = new Padding (111, 2, 30, 40);
241 Assert.AreEqual (expected, modified, "NonAll_ChangeLeft");
243 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (-1, 1, 222, 30, 40));
244 expected = new Padding (1, 222, 30, 40);
245 Assert.AreEqual (expected, modified, "NonAll_ChangeTop");
247 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (555, 1, 2, 30, 40));
248 expected = new Padding (555);
249 Assert.AreEqual (expected, modified, "NonAll_ChangeAll");
253 context = GetTypeDescriptorContext (new Padding (1));
255 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (1, 1, 1, 1, 1));
256 expected = new Padding (1, 1, 1, 1);
257 Assert.AreEqual (expected, modified, "All_NoChange");
259 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (1, 111, 1, 1, 1));
260 expected = new Padding (111, 1, 1, 1);
261 Assert.AreEqual (expected, modified, "All_ChangeLeft");
263 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (1, 1, 222, 1, 1));
264 expected = new Padding (1, 222, 1, 1);
265 Assert.AreEqual (expected, modified, "All_ChangeTop");
267 modified = (Padding) c.CreateInstance (context, GetPropertiesTable (555, 1, 1, 1, 1));
268 expected = new Padding (555);
269 Assert.AreEqual (expected, modified, "All_ChangeAll");
273 public void CreateInstance_NullArguments ()
275 PaddingConverter c = new PaddingConverter ();
277 c.CreateInstance (null, GetPropertiesTable (1, 1, 1, 1, 1));
279 } catch (ArgumentNullException ex) { }
281 c.CreateInstance (GetTypeDescriptorContext (Padding.Empty), null);
283 } catch (ArgumentNullException ex) { }