2006-04-25 Atsushi Enomoto <atsushi@ximian.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         class AnotherSite: ISite
79         { 
80                 public IComponent Component { get {  return null; } }
81
82                 public IContainer Container { get {  return null; } }
83
84                 public bool DesignMode { get {  return true; } }
85
86                 public string Name { get { return "TestName"; } set { } }
87
88                 public object GetService (Type t)
89                 {
90                         if (t == typeof(ITypeDescriptorFilterService)) {
91                                 return new AnotherFilter ();
92                         }
93                         return null;
94                 }
95         }
96
97         class AnotherFilter: ITypeDescriptorFilterService
98         {
99                 public bool FilterAttributes (IComponent component,IDictionary attributes) {
100                         Attribute ea = new DefaultEventAttribute ("AnEvent");
101                         attributes [ea.TypeId] = ea;
102                         ea = new DefaultPropertyAttribute ("TestProperty");
103                         attributes [ea.TypeId] = ea;
104                         ea = new EditorAttribute ();
105                         attributes [ea.TypeId] = ea;
106                         return true;
107                 }
108
109                 public bool FilterEvents (IComponent component, IDictionary events) {
110                         return true;
111                 }
112
113                 public bool FilterProperties (IComponent component, IDictionary properties) {
114                         return true;
115                 }
116         }
117
118         [DescriptionAttribute ("my test component")]
119         [DesignerAttribute (typeof(MyDesigner), typeof(int))]
120         public class MyComponent: Component
121         {
122                 string prop;
123                 
124                 [DescriptionAttribute ("test")]
125                 public event EventHandler AnEvent;
126                 
127                 public event EventHandler AnotherEvent;
128                 
129                 public MyComponent  ()
130                 {
131                 }
132                 
133                 public MyComponent (ISite site)
134                 {
135                         Site = site;
136                 }
137                 
138                 [DescriptionAttribute ("test")]
139                 public string TestProperty
140                 {
141                         get { return prop; }
142                         set { prop = value; }
143                 }
144                 
145                 public string AnotherProperty
146                 {
147                         get { return prop; }
148                         set { prop = value; }
149                 }
150         }
151
152         [DefaultProperty("AnotherProperty")]
153         [DefaultEvent("AnotherEvent")]
154         [DescriptionAttribute ("my test component")]
155         [DesignerAttribute (typeof(MyDesigner), typeof(int))]
156         public class AnotherComponent: Component {
157                 string prop;
158                 
159                 [DescriptionAttribute ("test")]
160                 public event EventHandler AnEvent;
161                 
162                 public event EventHandler AnotherEvent;
163                 
164                 public AnotherComponent () {
165                 }
166                 
167                 public AnotherComponent (ISite site) {
168                         Site = site;
169                 }
170                 
171                 [DescriptionAttribute ("test")]
172                 public string TestProperty {
173                         get { return prop; }
174                         set { prop = value; }
175                 }
176                 
177                 public string AnotherProperty {
178                         get { return prop; }
179                         set { prop = value; }
180                 }
181         }
182
183         public interface ITestInterface
184         {
185                 void TestFunction ();
186         }
187         
188         public class TestClass
189         {
190                 public TestClass()
191                 {}
192                         
193                 void TestFunction ()
194                 {}
195         }
196         
197         public struct TestStruct
198         {
199                 public int TestVal;
200         }
201         
202         [TestFixture]
203         public class TypeDescriptorTests: Assertion
204         {
205                 MyComponent com = new MyComponent ();
206                 MyComponent sitedcom = new MyComponent (new MySite ());
207                 AnotherComponent anothercom = new AnotherComponent ();
208                 
209                 [Test]
210                 public void TestCreateDesigner ()
211                 {
212                         IDesigner des = TypeDescriptor.CreateDesigner (com, typeof(int));
213                         Assert ("t1", des is MyDesigner);
214                         
215                         des = TypeDescriptor.CreateDesigner (com, typeof(string));
216                         AssertNull ("t2", des);
217                 }
218                 
219                 [Test]
220                 public void TestCreateEvent ()
221                 {
222                         EventDescriptor ed = TypeDescriptor.CreateEvent (typeof(MyComponent), "AnEvent", typeof(EventHandler), null);
223                         AssertEquals ("t1", typeof(MyComponent), ed.ComponentType);
224                         AssertEquals ("t2", typeof(EventHandler), ed.EventType);
225                         AssertEquals ("t3", true, ed.IsMulticast);
226                         AssertEquals ("t4", "AnEvent", ed.Name);
227                 }
228                 
229                 [Test]
230                 public void TestCreateProperty ()
231                 {
232                         PropertyDescriptor pd = TypeDescriptor.CreateProperty (typeof(MyComponent), "TestProperty", typeof(string), null);
233                         AssertEquals ("t1", typeof(MyComponent), pd.ComponentType);
234                         AssertEquals ("t2", "TestProperty", pd.Name);
235                         AssertEquals ("t3", typeof(string), pd.PropertyType);
236                         AssertEquals ("t4", false, pd.IsReadOnly);
237                         
238                         pd.SetValue (com, "hi");
239                         AssertEquals ("t5", "hi", pd.GetValue(com));
240                 }
241                 
242                 [Test]
243                 public void TestGetAttributes ()
244                 {
245                         AttributeCollection col = TypeDescriptor.GetAttributes (typeof(MyComponent));
246                         Assert ("t2", col[typeof(DescriptionAttribute)] != null);
247                         Assert ("t3", col[typeof(DesignerAttribute)] != null);
248                         Assert ("t4", col[typeof(EditorAttribute)] == null);
249                         
250                         col = TypeDescriptor.GetAttributes (com);
251                         Assert ("t6", col[typeof(DescriptionAttribute)] != null);
252                         Assert ("t7", col[typeof(DesignerAttribute)] != null);
253                         Assert ("t8", col[typeof(EditorAttribute)] == null);
254                         
255                         col = TypeDescriptor.GetAttributes (sitedcom);
256                         Assert ("t10", col[typeof(DescriptionAttribute)] != null);
257                         Assert ("t11", col[typeof(DesignerAttribute)] != null);
258                         Assert ("t12", col[typeof(EditorAttribute)] != null);
259                 }
260                 
261                 [Test]
262                 public void TestGetClassName ()
263                 {
264                         AssertEquals ("t1", typeof(MyComponent).FullName, TypeDescriptor.GetClassName (com));
265                 }
266                 
267                 [Test]
268                 public void TestGetComponentName ()
269                 {
270 #if !NET_2_0
271                         AssertEquals ("t1", "MyComponent", TypeDescriptor.GetComponentName (com));
272                         AssertEquals ("t2", "MyComponent", TypeDescriptor.GetComponentName (com, false));
273                         AssertEquals ("t3", "Exception", TypeDescriptor.GetComponentName (new Exception ()));
274                         AssertEquals ("t4", "Exception", TypeDescriptor.GetComponentName (new Exception (), false));
275                         AssertNotNull ("t5", TypeDescriptor.GetComponentName (typeof (Exception)));
276                         AssertNotNull ("t6", TypeDescriptor.GetComponentName (typeof (Exception), false));
277 #else
278                         // in MS.NET 2.0, GetComponentName no longer returns
279                         // the type name if there's no custom typedescriptor
280                         // and no site
281                         AssertNull ("t1", TypeDescriptor.GetComponentName (com));
282                         AssertNull ("t2", TypeDescriptor.GetComponentName (com, false));
283                         AssertNull ("t3", TypeDescriptor.GetComponentName (new Exception ()));
284                         AssertNull ("t4", TypeDescriptor.GetComponentName (new Exception (), false));
285                         AssertNull ("t5", TypeDescriptor.GetComponentName (typeof (Exception)));
286                         AssertNull ("t6", TypeDescriptor.GetComponentName (typeof (Exception), false));
287 #endif
288                         AssertEquals ("t7", "TestName", TypeDescriptor.GetComponentName (sitedcom));
289                         AssertEquals ("t8", "TestName", TypeDescriptor.GetComponentName (sitedcom));
290                 }
291                 
292                 [Test]
293                 public void TestGetConverter ()
294                 {
295                         AssertEquals (typeof(BooleanConverter), TypeDescriptor.GetConverter (typeof (bool)).GetType());
296                         AssertEquals (typeof(ByteConverter), TypeDescriptor.GetConverter (typeof (byte)).GetType());
297                         AssertEquals (typeof(SByteConverter), TypeDescriptor.GetConverter (typeof (sbyte)).GetType());
298                         AssertEquals (typeof(StringConverter), TypeDescriptor.GetConverter (typeof (string)).GetType());
299                         AssertEquals (typeof(CharConverter), TypeDescriptor.GetConverter (typeof (char)).GetType());
300                         AssertEquals (typeof(Int16Converter), TypeDescriptor.GetConverter (typeof (short)).GetType());
301                         AssertEquals (typeof(Int32Converter), TypeDescriptor.GetConverter (typeof (int)).GetType());
302                         AssertEquals (typeof(Int64Converter), TypeDescriptor.GetConverter (typeof (long)).GetType());
303                         AssertEquals (typeof(UInt16Converter), TypeDescriptor.GetConverter (typeof (ushort)).GetType());
304                         AssertEquals (typeof(UInt32Converter), TypeDescriptor.GetConverter (typeof (uint)).GetType());
305                         AssertEquals (typeof(UInt64Converter), TypeDescriptor.GetConverter (typeof (ulong)).GetType());
306                         AssertEquals (typeof(SingleConverter), TypeDescriptor.GetConverter (typeof (float)).GetType());
307                         AssertEquals (typeof(DoubleConverter), TypeDescriptor.GetConverter (typeof (double)).GetType());
308                         AssertEquals (typeof(DecimalConverter), TypeDescriptor.GetConverter (typeof (decimal)).GetType());
309                         AssertEquals (typeof(ArrayConverter), TypeDescriptor.GetConverter (typeof (Array)).GetType());
310                         AssertEquals (typeof(CultureInfoConverter), TypeDescriptor.GetConverter (typeof (CultureInfo)).GetType());
311                         AssertEquals (typeof(DateTimeConverter), TypeDescriptor.GetConverter (typeof (DateTime)).GetType());
312                         AssertEquals (typeof(GuidConverter), TypeDescriptor.GetConverter (typeof (Guid)).GetType());
313                         AssertEquals (typeof(TimeSpanConverter), TypeDescriptor.GetConverter (typeof (TimeSpan)).GetType());
314                         AssertEquals (typeof(CollectionConverter), TypeDescriptor.GetConverter (typeof (ICollection)).GetType());
315
316                         // Tests from bug #71444
317                         AssertEquals (typeof(CollectionConverter), TypeDescriptor.GetConverter (typeof (IDictionary)).GetType());
318                         AssertEquals (typeof(ReferenceConverter), TypeDescriptor.GetConverter (typeof (ITestInterface)).GetType());
319                         AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (typeof (TestClass)).GetType());
320                         AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (typeof (TestStruct)).GetType());
321                         
322                         AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (new TestClass ()).GetType());
323                         AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (new TestStruct ()).GetType());
324                         AssertEquals (typeof(CollectionConverter), TypeDescriptor.GetConverter (new Hashtable ()).GetType());
325
326 #if NET_2_0
327                         // Test from bug #76686
328                         AssertEquals (typeof (Int32Converter), TypeDescriptor.GetConverter ((int?) 1).GetType ());
329 #endif
330                 }
331                 
332                 [Test]
333                 public void TestGetDefaultEvent ()
334                 {
335                         EventDescriptor des = TypeDescriptor.GetDefaultEvent (typeof(MyComponent));
336                         AssertNull ("t1", des);
337                         
338                         des = TypeDescriptor.GetDefaultEvent (com);
339                         AssertNull ("t2", des);
340
341                         des = TypeDescriptor.GetDefaultEvent (typeof(AnotherComponent));
342                         AssertNotNull ("t3", des);
343                         AssertEquals ("t4", "AnotherEvent", des.Name);
344
345                         des = TypeDescriptor.GetDefaultEvent (anothercom);
346                         AssertNotNull ("t5", des);
347                         AssertEquals ("t6", "AnotherEvent", des.Name);
348
349                         des = TypeDescriptor.GetDefaultEvent (sitedcom);
350 #if NET_2_0
351                         AssertNull ("t7", des);
352 #else
353                         AssertNotNull ("t7/1", des);
354                         AssertEquals ("t7/2", "AnotherEvent", des.Name);
355 #endif
356
357                         des = TypeDescriptor.GetDefaultEvent (new MyComponent(new AnotherSite ()));
358                         AssertNotNull ("t8", des);
359                         AssertEquals ("t9", "AnEvent", des.Name);
360
361                         des = TypeDescriptor.GetDefaultEvent (new AnotherComponent(new AnotherSite ()));
362                         AssertNotNull ("t10", des);
363                         AssertEquals ("t11", "AnEvent", des.Name);
364                 }
365                 
366                 [Test]
367                 public void TestGetDefaultProperty ()
368                 {
369                         PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (typeof(MyComponent));
370                         AssertNull ("t1", des);
371                         
372                         des = TypeDescriptor.GetDefaultProperty (com);
373                         AssertNull ("t2", des);
374
375                         des = TypeDescriptor.GetDefaultProperty (typeof(AnotherComponent));
376                         AssertNotNull ("t1", des);
377                         AssertEquals ("t2", "AnotherProperty", des.Name);
378
379                         des = TypeDescriptor.GetDefaultProperty (anothercom);
380                         AssertNotNull ("t1", des);
381                         AssertEquals ("t2", "AnotherProperty", des.Name);
382                 }
383                 
384                 [Test]
385 #if !NET_2_0
386                 // throws NullReferenceException on MS.NET 1.x due to bug
387                 // which is fixed in MS.NET 2.0
388                 [NUnit.Framework.Category("NotDotNet")]
389 #endif
390                 public void TestGetDefaultProperty2 ()
391                 {
392                         PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (sitedcom);
393                         AssertNull ("t1", des);
394
395                         des = TypeDescriptor.GetDefaultProperty (new MyComponent (new AnotherSite ()));
396                         AssertNotNull ("t2", des);
397                         AssertEquals ("t3", "TestProperty", des.Name);
398
399                         des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new AnotherSite ()));
400                         AssertNotNull ("t4", des);
401                         AssertEquals ("t5", "TestProperty", des.Name);
402
403                         des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new MySite ()));
404                         AssertNull ("t6", des);
405                 }
406
407                 [Test]
408                 public void TestGetEvents ()
409                 {
410                         EventDescriptorCollection col = TypeDescriptor.GetEvents (typeof(MyComponent));
411                                 
412                         AssertEquals ("t1.1", 3, col.Count);
413                         Assert ("t1.2", col.Find ("AnEvent", true) != null);
414                         Assert ("t1.3", col.Find ("AnotherEvent", true) != null);
415                         Assert ("t1.4", col.Find ("Disposed", true) != null);
416                         
417                         col = TypeDescriptor.GetEvents (com);
418                         AssertEquals ("t2.1", 3, col.Count);
419                         Assert ("t2.2", col.Find ("AnEvent", true) != null);
420                         Assert ("t2.3", col.Find ("AnotherEvent", true) != null);
421                         Assert ("t2.4", col.Find ("Disposed", true) != null);
422                         
423                         col = TypeDescriptor.GetEvents (sitedcom);
424                         AssertEquals ("t3.1", 2, col.Count);
425                         Assert ("t3.2", col.Find ("AnotherEvent", true) != null);
426                         Assert ("t3.3", col.Find ("Disposed", true) != null);
427                         
428                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
429                         
430                         col = TypeDescriptor.GetEvents (typeof(MyComponent), filter);
431                         AssertEquals ("t4.1", 1, col.Count);
432                         Assert ("t4.2", col.Find ("AnEvent", true) != null);
433                         
434                         col = TypeDescriptor.GetEvents (com, filter);
435                         AssertEquals ("t5.1", 1, col.Count);
436                         Assert ("t5.2", col.Find ("AnEvent", true) != null);
437                         
438                         col = TypeDescriptor.GetEvents (sitedcom, filter);
439                         AssertEquals ("t6", 0, col.Count);
440                 }
441                 
442                 [Test]
443                 public void TestGetProperties ()
444                 {
445                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof(MyComponent));
446                         Assert ("t1.1", col.Find ("TestProperty", true) != null);
447                         Assert ("t1.2", col.Find ("AnotherProperty", true) != null);
448                         
449                         col = TypeDescriptor.GetProperties (com);
450                         Assert ("t2.1", col.Find ("TestProperty", true) != null);
451                         Assert ("t2.2", col.Find ("AnotherProperty", true) != null);
452                         
453                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
454                         
455                         col = TypeDescriptor.GetProperties (typeof(MyComponent), filter);
456                         Assert ("t4.1", col.Find ("TestProperty", true) != null);
457                         Assert ("t4.2", col.Find ("AnotherProperty", true) == null);
458                         
459                         col = TypeDescriptor.GetProperties (com, filter);
460                         Assert ("t5.1", col.Find ("TestProperty", true) != null);
461                         Assert ("t5.2", col.Find ("AnotherProperty", true) == null);
462                         
463                 }
464
465                 [Test]
466 #if !NET_2_0
467                 // throws NullReferenceException on MS.NET 1.x due to bug
468                 // which is fixed in MS.NET 2.0
469                 [NUnit.Framework.Category("NotDotNet")]
470 #endif
471                 public void TestGetProperties2 ()
472                 {
473                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (sitedcom);
474                         Assert ("t3.1", col.Find ("TestProperty", true) == null);
475                         Assert ("t3.2", col.Find ("AnotherProperty", true) != null);
476
477                         Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
478                         col = TypeDescriptor.GetProperties (sitedcom, filter);
479                         Assert ("t6.1", col.Find ("TestProperty", true) == null);
480                         Assert ("t6.2", col.Find ("AnotherProperty", true) == null);
481                 }
482
483                 [TypeConverter (typeof (TestConverter))]
484                 class TestConverterClass {
485                 }
486
487                 class TestConverter : TypeConverter {
488                         public Type Type;
489
490                         public TestConverter (Type type)
491                         {
492                                 this.Type = type;
493                         }
494                 }
495
496                 [Test]
497                 public void TestConverterCtorWithArgument ()
498                 {
499                         TypeConverter t = TypeDescriptor.GetConverter (typeof (TestConverterClass));
500                         Assert ("#01", null != t.GetType ());
501                         AssertEquals ("#02", typeof (TestConverter), t.GetType ());
502                         TestConverter converter = (TestConverter) t;
503                         AssertEquals ("#03", typeof (TestConverterClass), converter.Type);
504                 }
505         }
506 }
507