Merge pull request #268 from pcc/menudeactivate
[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 #if TARGET_JVM
48                 [Ignore ("TD BUG ID: 7229")]
49 #endif
50                 public void GetProperties ()
51                 {
52                         PropertyDescriptorCollection properties = null;
53                         C c = new C ();
54                         TypeConverter converter = TypeDescriptor.GetConverter (c);
55
56                         Assert.AreEqual (typeof (AConverter).FullName, converter.GetType ().FullName, "#1");
57
58                         properties = converter.GetProperties (c);
59                         Assert.AreEqual (1, properties.Count, "#2");
60                         Assert.AreEqual ("A", properties[0].Name, "#3");
61
62                         // ensure collection is read-only
63                         try {
64                                 properties.Clear ();
65                                 Assert.Fail ("#4");
66                         } catch (NotSupportedException) {
67                                 // read-only collection cannot be modified
68                         }
69
70                         properties = converter.GetProperties (null, c);
71                         Assert.AreEqual (1, properties.Count, "#5");
72                         Assert.AreEqual ("A", properties[0].Name, "#6");
73
74
75                         // ensure collection is read-only
76                         try {
77                                 properties.Clear ();
78                                 Assert.Fail ("#7");
79                         } catch (NotSupportedException) {
80                                 // read-only collection cannot be modified
81                         }
82
83                         properties = converter.GetProperties (null, c, null);
84                         Assert.AreEqual (2, properties.Count, "#8");
85
86                         // ensure collection is read-only
87                         try {
88                                 properties.Clear ();
89                                 Assert.Fail ("#9");
90                         } catch (NotSupportedException) {
91                                 // read-only collection cannot be modified
92                         }
93
94                         properties = converter.GetProperties (null);
95                         Assert.IsNull (properties, "#10");
96
97                         properties = converter.GetProperties (null, null);
98                         Assert.IsNull (properties, "#11");
99
100                         properties = converter.GetProperties (null, null, null);
101                         Assert.IsNull (properties, "#12");
102                 }
103
104                 [Test]
105                 public void GetConvertFromException ()
106                 {
107                         MockTypeConverter converter = new MockTypeConverter ();
108
109                         try {
110                                 converter.GetConvertFromException (null);
111                                 Assert.Fail ("#A1");
112                         } catch (NotSupportedException ex) {
113                                 // MockTypeConverter cannot convert from (null)
114                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
115                                 Assert.IsNull (ex.InnerException, "#A3");
116                                 Assert.IsNotNull (ex.Message, "#A4");
117                                 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#A5");
118                                 Assert.IsTrue (ex.Message.IndexOf ("(null)") != -1, "#A6");
119                         }
120
121                         try {
122                                 converter.GetConvertFromException ("B");
123                                 Assert.Fail ("#B1");
124                         } catch (NotSupportedException ex) {
125                                 // MockTypeConverter cannot convert from System.String
126                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
127                                 Assert.IsNull (ex.InnerException, "#B3");
128                                 Assert.IsNotNull (ex.Message, "#B4");
129                                 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#B5");
130                                 Assert.IsTrue (ex.Message.IndexOf (typeof (string).FullName) != -1, "#B6");
131                         }
132                 }
133
134                 [Test]
135                 public void GetConvertToException ()
136                 {
137                         MockTypeConverter converter = new MockTypeConverter ();
138
139                         try {
140                                 converter.GetConvertToException (null, typeof (DateTime));
141                                 Assert.Fail ("#A1");
142                         } catch (NotSupportedException ex) {
143                                 // 'MockTypeConverter' is unable to convert '(null)'
144                                 // to 'System.DateTime'
145                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
146                                 Assert.IsNull (ex.InnerException, "#A3");
147                                 Assert.IsNotNull (ex.Message, "#A4");
148                                 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (MockTypeConverter).Name + "'") != -1, "#A5");
149                                 Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#A6");
150                                 Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (DateTime).FullName + "'") != -1, "#A7");
151                         }
152
153                         try {
154                                 converter.GetConvertToException ("B", typeof (DateTime));
155                                 Assert.Fail ("#B1");
156                         } catch (NotSupportedException ex) {
157                                 // 'MockTypeConverter' is unable to convert 'System.String'
158                                 // to 'System.DateTime'
159                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
160                                 Assert.IsNull (ex.InnerException, "#B3");
161                                 Assert.IsNotNull (ex.Message, "#B4");
162                                 Assert.IsTrue (ex.Message.IndexOf (typeof (MockTypeConverter).Name) != -1, "#B5");
163                                 Assert.IsTrue (ex.Message.IndexOf (typeof (string).FullName) != -1, "#B6");
164                         }
165                 }
166                 
167                 [Test]
168                 public void ConvertToWithCulture ()
169                 {
170                         var culture = CultureInfo.CreateSpecificCulture ("sv-se");
171                         
172                         var converter = TypeDescriptor.GetConverter (typeof (string));
173                         Assert.AreEqual ("0,5", (string) converter.ConvertTo (null, culture, 0.5, typeof (string)));
174                 }
175
176                 public class FooConverter<T> : TypeConverter
177                 {
178                 }
179
180                 [TypeConverter (typeof (FooConverter<string>))]
181                 public string FooProperty {
182                         get { return ""; }
183                 }
184
185                 [Test]
186                 public void TestGenericTypeConverterInstantiation ()
187                 {
188                         Assert.IsNotNull (GetType ().GetProperty ("FooProperty").GetCustomAttributes (false));
189                 }
190
191                 [ExpectedException (typeof (NullReferenceException))]
192                 public void GetConvertToException_DestinationType_Null ()
193                 {
194                         MockTypeConverter converter = new MockTypeConverter ();
195                         converter.GetConvertToException ("B", (Type) null);
196                 }
197
198                 [Test]
199                 public void IsValid ()
200                 {
201                         var tc = new TypeConverter ();
202                         Assert.IsFalse (tc.IsValid (null));
203                 }
204         }
205
206         [TypeConverter (typeof (AConverter))]
207         public class C
208         {
209                 [Browsable (true)]
210                 public int A {
211                         get { return 0; }
212                 }
213
214                 [Browsable (false)]
215                 public int B {
216                         get { return 0; }
217                 }
218         }
219
220         public class MockTypeConverter : TypeConverter
221         {
222                 public new Exception GetConvertFromException (object value)
223                 {
224                         return base.GetConvertFromException (value);
225                 }
226
227                 public new Exception GetConvertToException (object value, Type destinationType)
228                 {
229                         return base.GetConvertToException (value, destinationType);
230                 }
231         }
232
233         public class AConverter : TypeConverter
234         {
235                 public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Attribute[] attributes)
236                 {
237                         if (value is C) {
238                                 return TypeDescriptor.GetProperties (value, attributes);
239                         }
240                         return base.GetProperties (context, value, attributes);
241                 }
242
243                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
244                 {
245                         return true;
246                 }
247         }
248
249         public class BConverter : TypeConverter
250         {
251         }
252 }