2009-06-06 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / PaddingConverterTest.cs
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
23 #if NET_2_0
24
25 using System;
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;
34
35 namespace MonoTests.System.Windows.Forms
36 {
37         [TestFixture]
38         public class PaddingConverterTest : TestHelper
39         {
40                 [Test]
41                 public void CanConvertFrom ()
42                 {
43                         PaddingConverter c = new PaddingConverter ();
44
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");
49                 }
50
51                 [Test]
52                 public void CanConvertTo ()
53                 {
54                         PaddingConverter c = new PaddingConverter ();
55
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");
60                 }
61                 
62                 [Test]
63                 public void RoundTrip ()
64                 {
65                         Padding p1 = new Padding (1, 2, 3, 4);
66                         Padding p2 = new Padding (1);
67                         Padding p3 = new Padding ();
68
69                         Assert.AreEqual (p1, RoundTripPadding (p1), "B1");
70                         Assert.AreEqual (p2, RoundTripPadding (p2), "B2");
71                         Assert.AreEqual (p3, RoundTripPadding (p3), "B3");
72                         
73                 }
74
75                 [Test]
76                 public void ConvertFrom ()
77                 {
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");
90                 }
91
92                 [Test]
93                 public void ConvertTo ()
94                 {
95                         PaddingConverter pc = new PaddingConverter ();
96
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");
106                 }
107
108                 private Padding RoundTripPadding (Padding p)
109                 {
110                         PaddingConverter pc = new PaddingConverter ();
111                         
112                         string s = (string)pc.ConvertTo (p, typeof (string));
113                         return (Padding)pc.ConvertFrom (s);
114                 }
115                 
116                 [Test]
117                 public void CreateInstanceSupported ()
118                 {
119                         PaddingConverter pc = new PaddingConverter ();
120                         
121                         Assert.AreEqual (true, pc.GetCreateInstanceSupported (null), "A1");
122                         Assert.AreEqual (true, pc.GetPropertiesSupported (null), "A2");
123                 }
124
125                 [Test]
126                 public void ConvertTo_InstanceDescriptor()
127                 {
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");
134
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");
140                 }
141
142                 #region FakeITypeDescriptorContext
143                 class FakeITypeDescriptorContext : ITypeDescriptorContext
144                 {
145                         // Only the Instance and PropertyDescriptor members are required for testing.
146                         //
147                         PropertyDescriptor propertyDescriptor;
148                         Object instance;
149
150                         internal FakeITypeDescriptorContext (PropertyDescriptor pd, object instance)
151                         {
152                                 if (pd == null)
153                                         throw new ArgumentNullException ("pd");
154                                 if (instance == null)
155                                         throw new ArgumentNullException ("instance");
156                                 propertyDescriptor = pd;
157                                 this.instance = instance;
158                         }
159
160                         #region ITypeDescriptorContext Members
161
162                         IContainer ITypeDescriptorContext.Container {
163                                 get { throw new NotImplementedException (); }
164                         }
165
166                         object ITypeDescriptorContext.Instance {
167                                 get { return instance; }
168                         }
169
170                         void ITypeDescriptorContext.OnComponentChanged ()
171                         {
172                                 throw new NotImplementedException ();
173                         }
174
175                         bool ITypeDescriptorContext.OnComponentChanging ()
176                         {
177                                 throw new NotImplementedException ();
178                         }
179
180                         PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor {
181                                 get { return propertyDescriptor; }
182                         }
183
184                         #endregion
185
186                         #region IServiceProvider Members
187
188                         object IServiceProvider.GetService (Type serviceType)
189                         {
190                                 throw new NotImplementedException ();
191                         }
192
193                         #endregion
194                 }
195                 #endregion
196
197                 class MyObjectWithMarginProperty
198                 {
199                         private Padding margin;
200
201                         public Padding Margin {
202                                 get { return margin; }
203                                 set { margin = value; }
204                         }
205                 }
206
207                 private static ITypeDescriptorContext GetTypeDescriptorContext (Padding paddingValue)
208                 {
209                         MyObjectWithMarginProperty obj = new MyObjectWithMarginProperty();
210                         obj.Margin = paddingValue;
211                         PropertyDescriptor pd = TypeDescriptor.GetProperties (obj)["Margin"];
212                         return new FakeITypeDescriptorContext (pd, obj);
213                 }
214
215                 private static Hashtable GetPropertiesTable (int all, int left, int top, int right, int bottom)
216                 {
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);
223                         return newValues;
224                 }
225
226                 [Test]
227                 public void CreateInstance ()
228                 {
229                         PaddingConverter c = new PaddingConverter();
230                         Padding modified, expected;
231
232                         // Non-"All" Tests
233                         //
234                         ITypeDescriptorContext context = GetTypeDescriptorContext (new Padding (1, 2, 30, 40));
235
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");
239
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");
243
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");
247
248                         modified = (Padding) c.CreateInstance (context, GetPropertiesTable (555, 1, 2, 30, 40));
249                         expected = new Padding (555);
250                         Assert.AreEqual (expected, modified, "NonAll_ChangeAll");
251
252                         // "All" tests
253                         //
254                         context = GetTypeDescriptorContext (new Padding (1));
255
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");
259
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");
263
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");
267
268                         modified = (Padding) c.CreateInstance (context, GetPropertiesTable (555, 1, 1, 1, 1));
269                         expected = new Padding (555);
270                         Assert.AreEqual (expected, modified, "All_ChangeAll");
271                 }
272
273                 [Test]
274                 public void CreateInstance_NullArguments ()
275                 {
276                         PaddingConverter c = new PaddingConverter ();
277                         try {
278                                 c.CreateInstance (null, GetPropertiesTable (1, 1, 1, 1, 1));
279                                 Assert.Fail ("#1");
280                         } catch (ArgumentNullException ex) { }
281                         try {
282                                 c.CreateInstance (GetTypeDescriptorContext (Padding.Empty), null);
283                                 Assert.Fail ("#2");
284                         } catch (ArgumentNullException ex) { }
285                 }
286         }
287 }
288 #endif