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