New test.
[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                 public void GetProperties ()
47                 {
48                         PropertyDescriptorCollection properties = null;
49                         C c = new C ();
50                         TypeConverter converter = TypeDescriptor.GetConverter (c);
51
52                         Assert.AreEqual (typeof (AConverter).FullName, converter.GetType ().FullName, "#1");
53
54                         properties = converter.GetProperties (c);
55                         Assert.AreEqual (1, properties.Count, "#2");
56                         Assert.AreEqual ("A", properties[0].Name, "#3");
57
58                         // ensure collection is read-only
59                         try {
60                                 properties.Clear ();
61                                 Assert.Fail ("#4");
62                         } catch (NotSupportedException) {
63                                 // read-only collection cannot be modified
64                         }
65
66                         properties = converter.GetProperties (null, c);
67                         Assert.AreEqual (1, properties.Count, "#5");
68                         Assert.AreEqual ("A", properties[0].Name, "#6");
69
70
71                         // ensure collection is read-only
72                         try {
73                                 properties.Clear ();
74                                 Assert.Fail ("#7");
75                         } catch (NotSupportedException) {
76                                 // read-only collection cannot be modified
77                         }
78
79                         properties = converter.GetProperties (null, c, null);
80                         Assert.AreEqual (2, properties.Count, "#8");
81
82                         // ensure collection is read-only
83                         try {
84                                 properties.Clear ();
85                                 Assert.Fail ("#9");
86                         } catch (NotSupportedException) {
87                                 // read-only collection cannot be modified
88                         }
89
90                         properties = converter.GetProperties (null);
91                         Assert.IsNull (properties, "#10");
92
93                         properties = converter.GetProperties (null, null);
94                         Assert.IsNull (properties, "#11");
95
96                         properties = converter.GetProperties (null, null, null);
97                         Assert.IsNull (properties, "#12");
98                 }
99         }
100
101         [TypeConverter (typeof (AConverter))]
102         public class C
103         {
104                 [Browsable (true)]
105                 public int A {
106                         get { return 0; }
107                 }
108
109                 [Browsable (false)]
110                 public int B {
111                         get { return 0; }
112                 }
113         }
114
115         public class AConverter : TypeConverter
116         {
117                 public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Attribute[] attributes)
118                 {
119                         if (value is C) {
120                                 return TypeDescriptor.GetProperties (value, attributes);
121                         }
122                         return base.GetProperties (context, value, attributes);
123                 }
124
125                 public override bool GetPropertiesSupported (ITypeDescriptorContext context)
126                 {
127                         return true;
128                 }
129         }
130
131         public class BConverter : TypeConverter
132         {
133         }
134 }