2005-01-20 Joerg Rosenkranz (joergr@voelcker.com)
[mono.git] / mcs / class / System / Test / System.ComponentModel / TypeDescriptorTests.cs
1 //
2 // System.ComponentModel.TypeDescriptorTests test cases
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // (c) 2004 Novell, Inc. (http://www.ximian.com)
8 //
9 using NUnit.Framework;
10 using System;
11 using System.Collections;
12 using System.ComponentModel;
13 using System.ComponentModel.Design;
14 using System.Globalization;
15
16 namespace MonoTests.System.ComponentModel
17 {
18         class MyDesigner: IDesigner
19         {
20                 public MyDesigner()
21                 {
22                 }
23
24                 public IComponent Component {get{return null; }}
25
26                 public DesignerVerbCollection Verbs {get{return null; }}
27
28                 public void DoDefaultAction () { }
29
30                 public void Initialize (IComponent component) { }
31
32                 public void Dispose () { }
33         }
34
35         class MySite: ISite
36         { 
37                 public IComponent Component { get {  return null; } }
38
39                 public IContainer Container { get {  return null; } }
40
41                 public bool DesignMode { get {  return true; } }
42
43                 public string Name { get { return "TestName"; } set { } }       
44
45                 public object GetService (Type t)
46                 {
47                         if (t == typeof(ITypeDescriptorFilterService)) return new MyFilter ();
48                         return null;
49                 }
50         }
51         
52         class MyFilter: ITypeDescriptorFilterService
53         {
54                 public bool FilterAttributes (IComponent component,IDictionary attributes)
55                 {
56                         Attribute ea = new DefaultEventAttribute ("AnEvent");
57                         attributes [ea.TypeId] = ea;
58                         ea = new DefaultPropertyAttribute ("TestProperty");
59                         attributes [ea.TypeId] = ea;
60                         ea = new EditorAttribute ();
61                         attributes [ea.TypeId] = ea;
62                         return true;
63                 }
64                 
65                 public bool FilterEvents (IComponent component, IDictionary events)
66                 {
67                         events.Remove ("AnEvent");
68                         return true;
69                 }
70                 
71                 public bool FilterProperties (IComponent component, IDictionary properties)
72                 {
73                         properties.Remove ("TestProperty");
74                         return true;
75                 }
76         }
77
78         [DescriptionAttribute ("my test component")]
79         [DesignerAttribute (typeof(MyDesigner), typeof(int))]
80         public class MyComponent: Component
81         {
82                 string prop;
83                 
84                 [DescriptionAttribute ("test")]
85                 public event EventHandler AnEvent;
86                 
87                 public event EventHandler AnotherEvent;
88                 
89                 public MyComponent  ()
90                 {
91                 }
92                 
93                 public MyComponent (ISite site)
94                 {
95                         Site = site;
96                 }
97                 
98                 [DescriptionAttribute ("test")]
99                 public string TestProperty
100                 {
101                         get { return prop; }
102                         set { prop = value; }
103                 }
104                 
105                 public string AnotherProperty
106                 {
107                         get { return prop; }
108                         set { prop = value; }
109                 }
110         }
111         
112         public interface ITestInterface
113         {
114                 void TestFunction ();
115         }
116         
117         public class TestClass
118         {
119                 public TestClass()
120                 {}
121                         
122                 void TestFunction ()
123                 {}
124         }
125         
126         public struct TestStruct
127         {
128                 public int TestVal;
129         }
130         
131         [TestFixture]
132         public class TypeDescriptorTests: Assertion
133         {
134                 MyComponent com = new MyComponent ();
135                 MyComponent sitedcom = new MyComponent (new MySite ());
136                 
137                 [Test]
138                 public void TestCreateDesigner ()
139                 {
140                         IDesigner des = TypeDescriptor.CreateDesigner (com, typeof(int));
141                         Assert ("t1", des is MyDesigner);
142                         
143                         des = TypeDescriptor.CreateDesigner (com, typeof(string));
144                         AssertNull ("t2", des);
145                 }
146                 
147                 [Test]
148                 public void TestCreateEvent ()
149                 {
150                         EventDescriptor ed = TypeDescriptor.CreateEvent (typeof(MyComponent), "AnEvent", typeof(EventHandler), null);
151                         AssertEquals ("t1", typeof(MyComponent), ed.ComponentType);
152                         AssertEquals ("t2", typeof(EventHandler), ed.EventType);
153                         AssertEquals ("t3", true, ed.IsMulticast);
154                         AssertEquals ("t4", "AnEvent", ed.Name);
155                 }
156                 
157                 [Test]
158                 public void TestCreateProperty ()
159                 {
160                         PropertyDescriptor pd = TypeDescriptor.CreateProperty (typeof(MyComponent), "TestProperty", typeof(string), null);
161                         AssertEquals ("t1", typeof(MyComponent), pd.ComponentType);
162                         AssertEquals ("t2", "TestProperty", pd.Name);
163                         AssertEquals ("t3", typeof(string), pd.PropertyType);
164                         AssertEquals ("t4", false, pd.IsReadOnly);
165                         
166                         pd.SetValue (com, "hi");
167                         AssertEquals ("t5", "hi", pd.GetValue(com));
168                 }
169                 
170                 [Test]
171                 public void TestGetAttributes ()
172                 {
173                         AttributeCollection col = TypeDescriptor.GetAttributes (typeof(MyComponent));
174                         Assert ("t2", col[typeof(DescriptionAttribute)] != null);
175                         Assert ("t3", col[typeof(DesignerAttribute)] != null);
176                         Assert ("t4", col[typeof(EditorAttribute)] == null);
177                         
178                         col = TypeDescriptor.GetAttributes (com);
179                         Assert ("t6", col[typeof(DescriptionAttribute)] != null);
180                         Assert ("t7", col[typeof(DesignerAttribute)] != null);
181                         Assert ("t8", col[typeof(EditorAttribute)] == null);
182                         
183                         col = TypeDescriptor.GetAttributes (sitedcom);
184                         Assert ("t10", col[typeof(DescriptionAttribute)] != null);
185                         Assert ("t11", col[typeof(DesignerAttribute)] != null);
186                         Assert ("t12", col[typeof(EditorAttribute)] != null);
187                 }
188                 
189                 [Test]
190                 public void TestGetClassName ()
191                 {
192                         AssertEquals ("t1", typeof(MyComponent).FullName, TypeDescriptor.GetClassName (com));
193                 }
194                 
195                 [Test]
196                 public void TestGetComponentName ()
197                 {
198                         AssertNotNull ("t1", TypeDescriptor.GetComponentName (com));
199                         AssertEquals ("t2", "MyComponent", TypeDescriptor.GetComponentName (com));
200                         AssertEquals ("t3", "TestName", TypeDescriptor.GetComponentName (sitedcom));
201                 }
202                 
203                 [Test]
204                 public void TestGetConverter ()
205                 {
206                         AssertEquals (typeof(BooleanConverter), TypeDescriptor.GetConverter (typeof (bool)).GetType());
207                         AssertEquals (typeof(ByteConverter), TypeDescriptor.GetConverter (typeof (byte)).GetType());
208                         AssertEquals (typeof(SByteConverter), TypeDescriptor.GetConverter (typeof (sbyte)).GetType());
209                         AssertEquals (typeof(StringConverter), TypeDescriptor.GetConverter (typeof (string)).GetType());
210                         AssertEquals (typeof(CharConverter), TypeDescriptor.GetConverter (typeof (char)).GetType());
211                         AssertEquals (typeof(Int16Converter), TypeDescriptor.GetConverter (typeof (short)).GetType());
212                         AssertEquals (typeof(Int32Converter), TypeDescriptor.GetConverter (typeof (int)).GetType());
213                         AssertEquals (typeof(Int64Converter), TypeDescriptor.GetConverter (typeof (long)).GetType());
214                         AssertEquals (typeof(UInt16Converter), TypeDescriptor.GetConverter (typeof (ushort)).GetType());
215                         AssertEquals (typeof(UInt32Converter), TypeDescriptor.GetConverter (typeof (uint)).GetType());
216                         AssertEquals (typeof(UInt64Converter), TypeDescriptor.GetConverter (typeof (ulong)).GetType());
217                         AssertEquals (typeof(SingleConverter), TypeDescriptor.GetConverter (typeof (float)).GetType());
218                         AssertEquals (typeof(DoubleConverter), TypeDescriptor.GetConverter (typeof (double)).GetType());
219                         AssertEquals (typeof(DecimalConverter), TypeDescriptor.GetConverter (typeof (decimal)).GetType());
220                         AssertEquals (typeof(ArrayConverter), TypeDescriptor.GetConverter (typeof (Array)).GetType());
221                         AssertEquals (typeof(CultureInfoConverter), TypeDescriptor.GetConverter (typeof (CultureInfo)).GetType());
222                         AssertEquals (typeof(DateTimeConverter), TypeDescriptor.GetConverter (typeof (DateTime)).GetType());
223                         AssertEquals (typeof(GuidConverter), TypeDescriptor.GetConverter (typeof (Guid)).GetType());
224                         AssertEquals (typeof(TimeSpanConverter), TypeDescriptor.GetConverter (typeof (TimeSpan)).GetType());
225                         AssertEquals (typeof(CollectionConverter), TypeDescriptor.GetConverter (typeof (ICollection)).GetType());
226
227                         // Tests from bug #71444
228                         AssertEquals (typeof(CollectionConverter), TypeDescriptor.GetConverter (typeof (IDictionary)).GetType());
229                         AssertEquals (typeof(ReferenceConverter), TypeDescriptor.GetConverter (typeof (ITestInterface)).GetType());
230                         AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (typeof (TestClass)).GetType());
231                         AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (typeof (TestStruct)).GetType());
232                         
233                         AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (new TestClass ()).GetType());
234                         AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (new TestStruct ()).GetType());
235                         AssertEquals (typeof(CollectionConverter), TypeDescriptor.GetConverter (new Hashtable ()).GetType());
236                 }
237                 
238                 [Test]
239                 public void TestGetDefaultEvent ()
240                 {
241                         EventDescriptor des = TypeDescriptor.GetDefaultEvent (typeof(MyComponent));
242                         AssertNull ("t1", des);
243                         
244                         des = TypeDescriptor.GetDefaultEvent (com);
245                         AssertNull ("t2", des);
246                         
247                         des = TypeDescriptor.GetDefaultEvent (sitedcom);
248                         AssertNotNull ("t3", des);
249                         AssertEquals ("t4", "AnotherEvent", des.Name);
250                 }
251                 
252                 [Test]
253                 public void TestGetDefaultProperty ()
254                 {
255                         PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (typeof(MyComponent));
256                         AssertNull ("t1", des);
257                         
258                         des = TypeDescriptor.GetDefaultProperty (com);
259                         AssertNull ("t2", des);
260                         
261                 }
262                 
263                 [Test]
264                 [Ignore("Fails on .NET")]
265                 public void TestGetDefaultProperty2 ()
266                 {
267                         PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (sitedcom);
268                         AssertNotNull ("t3", des);
269                         AssertEquals ("t4", "TestProperty", des.Name);
270                 }
271                 
272                 [Test]
273                 public void TestGetEvents ()
274                 {
275                         EventDescriptorCollection col = TypeDescriptor.GetEvents (typeof(MyComponent));
276                                 
277                         AssertEquals ("t1.1", 3, col.Count);
278                         Assert ("t1.2", col.Find ("AnEvent", true) != null);
279                         Assert ("t1.3", col.Find ("AnotherEvent", true) != null);
280                         Assert ("t1.4", col.Find ("Disposed", true) != null);
281                         
282                         col = TypeDescriptor.GetEvents (com);
283                         AssertEquals ("t2.1", 3, col.Count);
284                         Assert ("t2.2", col.Find ("AnEvent", true) != null);
285                         Assert ("t2.3", col.Find ("AnotherEvent", true) != null);
286                         Assert ("t2.4", col.Find ("Disposed", true) != null);
287                         
288                         col = TypeDescriptor.GetEvents (sitedcom);
289                         AssertEquals ("t3.1", 2, col.Count);
290                         Assert ("t3.2", col.Find ("AnotherEvent", true) != null);
291                         Assert ("t3.3", col.Find ("Disposed", true) != null);
292                         
293                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
294                         
295                         col = TypeDescriptor.GetEvents (typeof(MyComponent), filter);
296                         AssertEquals ("t4.1", 1, col.Count);
297                         Assert ("t4.2", col.Find ("AnEvent", true) != null);
298                         
299                         col = TypeDescriptor.GetEvents (com, filter);
300                         AssertEquals ("t5.1", 1, col.Count);
301                         Assert ("t5.2", col.Find ("AnEvent", true) != null);
302                         
303                         col = TypeDescriptor.GetEvents (sitedcom, filter);
304                         AssertEquals ("t6", 0, col.Count);
305                 }
306                 
307                 [Test]
308                 public void TestGetProperties ()
309                 {
310                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof(MyComponent));
311                         Assert ("t1.1", col.Find ("TestProperty", true) != null);
312                         Assert ("t1.2", col.Find ("AnotherProperty", true) != null);
313                         
314                         col = TypeDescriptor.GetProperties (com);
315                         Assert ("t2.1", col.Find ("TestProperty", true) != null);
316                         Assert ("t2.2", col.Find ("AnotherProperty", true) != null);
317                         
318                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
319                         
320                         col = TypeDescriptor.GetProperties (typeof(MyComponent), filter);
321                         Assert ("t4.1", col.Find ("TestProperty", true) != null);
322                         Assert ("t4.2", col.Find ("AnotherProperty", true) == null);
323                         
324                         col = TypeDescriptor.GetProperties (com, filter);
325                         Assert ("t5.1", col.Find ("TestProperty", true) != null);
326                         Assert ("t5.2", col.Find ("AnotherProperty", true) == null);
327                         
328                 }
329
330                 [Test]
331                 [Ignore("Fails on .NET")]
332                 public void TestGetProperties2 ()
333                 {
334                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (sitedcom);
335                         Assert ("t3.1", col.Find ("TestProperty", true) == null);
336                         Assert ("t3.2", col.Find ("AnotherProperty", true) != null);
337
338                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
339                         col = TypeDescriptor.GetProperties (sitedcom, filter);
340                         Assert ("t6.1", col.Find ("TestProperty", true) == null);
341                         Assert ("t6.2", col.Find ("AnotherProperty", true) == null);
342                 }
343
344         }
345 }
346