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