[Cleanup] Removed TARGET_JVM
[mono.git] / mcs / class / System / Test / System.ComponentModel / TypeConverterTests.cs
1 //
2 // System.ComponentModel.TypeConverter test cases
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (c) 2005 Novell, Inc. (http://www.ximian.com)
8 //
9
10 using System;
11 using System.ComponentModel;
12 using System.ComponentModel.Design.Serialization;
13 using System.Globalization;
14
15 using NUnit.Framework;
16
17 namespace MonoTests.System.ComponentModel
18 {
19         [TestFixture]
20         public class TypeConverterTests
21         {
22                 [Test]
23                 public void DefaultImplementation ()
24                 {
25                         BConverter converter = new BConverter ();
26                         C c = new C ();
27
28                         Assert.IsNull (converter.GetProperties (c), "#1");
29                         Assert.IsNull (converter.GetProperties (null, c), "#2");
30                         Assert.IsNull (converter.GetProperties (null, c, null), "#3");
31
32                         Assert.IsNull (converter.GetProperties (null), "#4");
33                         Assert.IsNull (converter.GetProperties (null, null), "#5");
34                         Assert.IsNull (converter.GetProperties (null, null, null), "#6");
35                         Assert.IsFalse (converter.GetCreateInstanceSupported (), "#7");
36                         Assert.IsFalse (converter.GetCreateInstanceSupported (null), "#8");
37                         Assert.IsFalse (converter.GetPropertiesSupported (), "#9");
38                         Assert.IsFalse (converter.GetPropertiesSupported (null), "#10");
39
40                         Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#11");
41                         Assert.IsTrue (converter.CanConvertFrom (null, typeof (InstanceDescriptor)), "#12");
42                         Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#13");
43                         Assert.IsTrue (converter.CanConvertTo (null, typeof (string)), "#14");
44                 }
45
46                 [Test]
47                 public void GetProperties ()
48                 {
49                         PropertyDescriptorCollection properties = null;
50                         C c = new C ();
51                         TypeConverter converter = TypeDescriptor.GetConverter (c);
52
53                         Assert.AreEqual (typeof (AConverter).FullName, converter.GetType ().FullName, "#1");
54
55                         properties = converter.GetProperties (c);
56                         Assert.AreEqual (1, properties.Count, "#2");
57                         Assert.AreEqual ("A", properties[0].Name, "#3");
58
59                         // ensure collection is read-only
60                         try {
61                                 properties.Clear ();
62                                 Assert.Fail ("#4");
63                         } catch (NotSupportedException) {
64                                 // read-only collection cannot be modified
65                         }
66
67                         properties = converter.GetProperties (null, c);
68                         Assert.AreEqual (1, properties.Count, "#5");
69                         Assert.AreEqual ("A", properties[0].Name, "#6");
70
71
72                         // ensure collection is read-only
73                         try {
74                                 properties.Clear ();
75                                 Assert.Fail ("#7");
76                         } catch (NotSupportedException) {
77                                 // read-only collection cannot be modified
78                         }
79
80                         properties = converter.GetProperties (null, c, null);
81                         Assert.AreEqual (2, properties.Count, "#8");
82
83                         // ensure collection is read-only
84                         try {
85                                 properties.Clear ();
86                                 Assert.Fail ("#9");
87                         } catch (NotSupportedException) {
88                                 // read-only collection cannot be modified
89                         }
90
91                         properties = converter.GetProperties (null);
92                         Assert.IsNull (properties, "#10");
93
94                         properties = converter.GetProperties (null, null);
95                         Assert.IsNull (properties, "#11");
96
97                         properties = converter.GetProperties (null, null, null);
98                         Assert.IsNull (properties, "#12");
99                 }
100
101                 [Test]
102                 public void GetConvertFromException ()
103                 {
104                         MockTypeConverter converter = new MockTypeConverter ();
105
106                         try {
107                                 converter.GetConvertFromException (null);
108                                 Assert.Fail ("#A1");
109                         } catch (NotSupportedException ex) {
110                                 // MockTypeConverter cannot convert from (null)
111                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
112                                 Assert.IsNull (ex.InnerException, "#A3");
113                                 Assert.IsNotNull (ex.Message, "#A4");
114                                 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#A5");
115                                 Assert.IsTrue (ex.Message.IndexOf ("(null)") != -1, "#A6");
116                         }
117
118                         try {
119                                 converter.GetConvertFromException ("B");
120                                 Assert.Fail ("#B1");
121                         } catch (NotSupportedException ex) {
122                                 // MockTypeConverter cannot convert from System.String
123                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
124                                 Assert.IsNull (ex.InnerException, "#B3");
125                                 Assert.IsNotNull (ex.Message, "#B4");
126                                 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#B5");
127                                 Assert.IsTrue (ex.Message.IndexOf (typeof (string).FullName) != -1, "#B6");
128                         }
129                 }
130
131                 [Test]
132                 public void GetConvertToException ()
133                 {
134                         MockTypeConverter converter = new MockTypeConverter ();
135
136                         try {
137                                 converter.GetConvertToException (null, typeof (DateTime));
138                                 Assert.Fail ("#A1");
139                         } catch (NotSupportedException ex) {
140                                 // 'MockTypeConverter' is unable to convert '(null)'
141                                 // to 'System.DateTime'
142                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
143                                 Assert.IsNull (ex.InnerException, "#A3");
144                                 Assert.IsNotNull (ex.Message, "#A4");
145                                 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (MockTypeConverter).Name + "'") != -1, "#A5");
146                                 Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#A6");
147                                 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (DateTime).FullName + "'") != -1, "#A7");
148                         }
149
150                         try {
151                                 converter.GetConvertToException ("B", typeof (DateTime));
152                                 Assert.Fail ("#B1");
153                         } catch (NotSupportedException ex) {
154                                 // 'MockTypeConverter' is unable to convert 'System.String'
155                                 // to 'System.DateTime'
156                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
157                                 Assert.IsNull (ex.InnerException, "#B3");
158                                 Assert.IsNotNull (ex.Message, "#B4");
159                                 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#B5");
160                                 Assert.IsTrue (ex.Message.IndexOf (typeof (string).FullName) != -1, "#B6");
161                         }
162                 }
163                 
164                 [Test]
165                 public void ConvertToWithCulture ()
166                 {
167                         var culture = CultureInfo.CreateSpecificCulture ("sv-se");
168                         
169                         var converter = TypeDescriptor.GetConverter (typeof (string));
170                         Assert.AreEqual ("0,5", (string) converter.ConvertTo (null, culture, 0.5, typeof (string)));
171                 }
172
173                 public class FooConverter<T> : TypeConverter
174                 {
175                 }
176
177                 [TypeConverter (typeof (FooConverter<string>))]
178                 public string FooProperty {
179                         get { return ""; }
180                 }
181
182                 [Test]
183                 public void TestGenericTypeConverterInstantiation ()
184                 {
185                         Assert.IsNotNull (GetType ().GetProperty ("FooProperty").GetCustomAttributes (false));
186                 }
187
188                 [ExpectedException (typeof (NullReferenceException))]
189                 public void GetConvertToException_DestinationType_Null ()
190                 {
191                         MockTypeConverter converter = new MockTypeConverter ();
192                         converter.GetConvertToException ("B", (Type) null);
193                 }
194
195                 [Test]
196                 public void IsValid ()
197                 {
198                         var tc = new TypeConverter ();
199                         Assert.IsFalse (tc.IsValid (null));
200                 }
201         }
202
203         [TypeConverter (typeof (AConverter))]
204         public class C
205         {
206                 [Browsable (true)]
207                 public int A {
208                         get { return 0; }
209                 }
210
211                 [Browsable (false)]
212                 public int B {
213                         get { return 0; }
214                 }
215         }
216
217         public class MockTypeConverter : TypeConverter
218         {
219                 public new Exception GetConvertFromException (object value)
220                 {
221                         return base.GetConvertFromException (value);
222                 }
223
224                 public new Exception GetConvertToException (object value, Type destinationType)
225                 {
226                         return base.GetConvertToException (value, destinationType);
227                 }
228         }
229
230         public class AConverter : TypeConverter
231         {
232                 public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Attribute[] attributes)
233                 {
234                         if (value is C) {
235                                 return TypeDescriptor.GetProperties (value, attributes);
236                         }
237                         return base.GetProperties (context, value, attributes);
238                 }
239
240                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
241                 {
242                         return true;
243                 }
244         }
245
246         public class BConverter : TypeConverter
247         {
248         }
249 }