2 // System.ComponentModel.TypeDescriptorTests test cases
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // (c) 2004 Novell, Inc. (http://www.ximian.com)
11 using System.Collections;
12 using System.ComponentModel;
13 using System.ComponentModel.Design;
14 using System.Globalization;
16 namespace MonoTests.System.ComponentModel
18 class MyDesigner: IDesigner
24 public IComponent Component {get{return null; }}
26 public DesignerVerbCollection Verbs {get{return null; }}
28 public void DoDefaultAction () { }
30 public void Initialize (IComponent component) { }
32 public void Dispose () { }
35 class MyOtherDesigner: IDesigner
37 public MyOtherDesigner()
41 public IComponent Component {get {return null; } }
42 public DesignerVerbCollection Verbs { get {return null; } }
43 public void DoDefaultAction () { }
44 public void Initialize (IComponent component) { }
45 public void Dispose () { }
50 public IComponent Component { get { return null; } }
52 public IContainer Container { get { return null; } }
54 public bool DesignMode { get { return true; } }
56 public string Name { get { return "TestName"; } set { } }
58 public object GetService (Type t)
60 if (t == typeof(ITypeDescriptorFilterService)) return new MyFilter ();
65 class MyFilter: ITypeDescriptorFilterService
67 public bool FilterAttributes (IComponent component,IDictionary attributes)
69 Attribute ea = new DefaultEventAttribute ("AnEvent");
70 attributes [ea.TypeId] = ea;
71 ea = new DefaultPropertyAttribute ("TestProperty");
72 attributes [ea.TypeId] = ea;
73 ea = new EditorAttribute ();
74 attributes [ea.TypeId] = ea;
78 public bool FilterEvents (IComponent component, IDictionary events)
80 events.Remove ("AnEvent");
84 public bool FilterProperties (IComponent component, IDictionary properties)
86 properties.Remove ("TestProperty");
91 class AnotherSite: ISite
93 public IComponent Component { get { return null; } }
95 public IContainer Container { get { return null; } }
97 public bool DesignMode { get { return true; } }
99 public string Name { get { return "TestName"; } set { } }
101 public object GetService (Type t)
103 if (t == typeof(ITypeDescriptorFilterService)) {
104 return new AnotherFilter ();
110 class AnotherFilter: ITypeDescriptorFilterService
112 public bool FilterAttributes (IComponent component,IDictionary attributes) {
113 Attribute ea = new DefaultEventAttribute ("AnEvent");
114 attributes [ea.TypeId] = ea;
115 ea = new DefaultPropertyAttribute ("TestProperty");
116 attributes [ea.TypeId] = ea;
117 ea = new EditorAttribute ();
118 attributes [ea.TypeId] = ea;
122 public bool FilterEvents (IComponent component, IDictionary events) {
126 public bool FilterProperties (IComponent component, IDictionary properties) {
131 [DescriptionAttribute ("my test component")]
132 [DesignerAttribute (typeof(MyDesigner), typeof(int))]
133 public class MyComponent: Component
137 [DescriptionAttribute ("test")]
138 public event EventHandler AnEvent;
140 public event EventHandler AnotherEvent;
142 public MyComponent ()
146 public MyComponent (ISite site)
151 [DescriptionAttribute ("test")]
152 public virtual string TestProperty
155 set { prop = value; }
158 public string AnotherProperty
161 set { prop = value; }
165 [DescriptionAttribute ("my test derived component")]
166 [DesignerAttribute (typeof(MyOtherDesigner))]
167 public class MyDerivedComponent: MyComponent
171 public MyDerivedComponent ()
175 public MyDerivedComponent (ISite site) : base (site)
179 [DescriptionAttribute ("test derived")]
180 public override string TestProperty
183 set { prop = value; }
188 [DefaultProperty("AnotherProperty")]
189 [DefaultEvent("AnotherEvent")]
190 [DescriptionAttribute ("my test component")]
191 [DesignerAttribute (typeof(MyDesigner), typeof(int))]
192 public class AnotherComponent: Component {
195 [DescriptionAttribute ("test")]
196 public event EventHandler AnEvent;
198 public event EventHandler AnotherEvent;
200 public AnotherComponent () {
203 public AnotherComponent (ISite site) {
207 [DescriptionAttribute ("test")]
208 public string TestProperty {
210 set { prop = value; }
213 public string AnotherProperty {
215 set { prop = value; }
219 public interface ITestInterface
221 void TestFunction ();
224 public class TestClass
233 public struct TestStruct
239 public class TypeDescriptorTests: Assertion
241 MyComponent com = new MyComponent ();
242 MyComponent sitedcom = new MyComponent (new MySite ());
243 AnotherComponent anothercom = new AnotherComponent ();
246 public void TestCreateDesigner ()
248 IDesigner des = TypeDescriptor.CreateDesigner (com, typeof(int));
249 Assert ("t1", des is MyDesigner);
251 des = TypeDescriptor.CreateDesigner (com, typeof(string));
252 AssertNull ("t2", des);
256 public void TestCreateEvent ()
258 EventDescriptor ed = TypeDescriptor.CreateEvent (typeof(MyComponent), "AnEvent", typeof(EventHandler), null);
259 AssertEquals ("t1", typeof(MyComponent), ed.ComponentType);
260 AssertEquals ("t2", typeof(EventHandler), ed.EventType);
261 AssertEquals ("t3", true, ed.IsMulticast);
262 AssertEquals ("t4", "AnEvent", ed.Name);
266 public void TestCreateProperty ()
268 PropertyDescriptor pd = TypeDescriptor.CreateProperty (typeof(MyComponent), "TestProperty", typeof(string), null);
269 AssertEquals ("t1", typeof(MyComponent), pd.ComponentType);
270 AssertEquals ("t2", "TestProperty", pd.Name);
271 AssertEquals ("t3", typeof(string), pd.PropertyType);
272 AssertEquals ("t4", false, pd.IsReadOnly);
274 pd.SetValue (com, "hi");
275 AssertEquals ("t5", "hi", pd.GetValue(com));
279 public void TestGetAttributes ()
281 AttributeCollection col = TypeDescriptor.GetAttributes (typeof(MyComponent));
282 Assert ("t2", col[typeof(DescriptionAttribute)] != null);
283 Assert ("t3", col[typeof(DesignerAttribute)] != null);
284 Assert ("t4", col[typeof(EditorAttribute)] == null);
286 col = TypeDescriptor.GetAttributes (com);
287 Assert ("t6", col[typeof(DescriptionAttribute)] != null);
288 Assert ("t7", col[typeof(DesignerAttribute)] != null);
289 Assert ("t8", col[typeof(EditorAttribute)] == null);
291 col = TypeDescriptor.GetAttributes (sitedcom);
292 Assert ("t10", col[typeof(DescriptionAttribute)] != null);
293 Assert ("t11", col[typeof(DesignerAttribute)] != null);
294 Assert ("t12", col[typeof(EditorAttribute)] != null);
296 col = TypeDescriptor.GetAttributes (typeof (MyDerivedComponent));
297 Assert ("t13", col[typeof(DesignerAttribute)] != null);
298 Assert ("t14", col[typeof(DescriptionAttribute)] != null);
299 DesignerAttribute attribute = col[typeof(DesignerAttribute)] as DesignerAttribute;
300 Assert ("t15", attribute != null);
301 AssertEquals ("t16", attribute.DesignerTypeName, typeof(MyOtherDesigner).AssemblyQualifiedName);
305 public void TestGetClassName ()
307 AssertEquals ("t1", typeof(MyComponent).FullName, TypeDescriptor.GetClassName (com));
311 public void TestGetComponentName ()
314 AssertEquals ("t1", "MyComponent", TypeDescriptor.GetComponentName (com));
315 AssertEquals ("t2", "MyComponent", TypeDescriptor.GetComponentName (com, false));
316 AssertEquals ("t3", "Exception", TypeDescriptor.GetComponentName (new Exception ()));
317 AssertEquals ("t4", "Exception", TypeDescriptor.GetComponentName (new Exception (), false));
318 AssertNotNull ("t5", TypeDescriptor.GetComponentName (typeof (Exception)));
319 AssertNotNull ("t6", TypeDescriptor.GetComponentName (typeof (Exception), false));
321 // in MS.NET 2.0, GetComponentName no longer returns
322 // the type name if there's no custom typedescriptor
324 AssertNull ("t1", TypeDescriptor.GetComponentName (com));
325 AssertNull ("t2", TypeDescriptor.GetComponentName (com, false));
326 AssertNull ("t3", TypeDescriptor.GetComponentName (new Exception ()));
327 AssertNull ("t4", TypeDescriptor.GetComponentName (new Exception (), false));
328 AssertNull ("t5", TypeDescriptor.GetComponentName (typeof (Exception)));
329 AssertNull ("t6", TypeDescriptor.GetComponentName (typeof (Exception), false));
331 AssertEquals ("t7", "TestName", TypeDescriptor.GetComponentName (sitedcom));
332 AssertEquals ("t8", "TestName", TypeDescriptor.GetComponentName (sitedcom));
336 public void TestGetConverter ()
338 AssertEquals (typeof(BooleanConverter), TypeDescriptor.GetConverter (typeof (bool)).GetType());
339 AssertEquals (typeof(ByteConverter), TypeDescriptor.GetConverter (typeof (byte)).GetType());
340 AssertEquals (typeof(SByteConverter), TypeDescriptor.GetConverter (typeof (sbyte)).GetType());
341 AssertEquals (typeof(StringConverter), TypeDescriptor.GetConverter (typeof (string)).GetType());
342 AssertEquals (typeof(CharConverter), TypeDescriptor.GetConverter (typeof (char)).GetType());
343 AssertEquals (typeof(Int16Converter), TypeDescriptor.GetConverter (typeof (short)).GetType());
344 AssertEquals (typeof(Int32Converter), TypeDescriptor.GetConverter (typeof (int)).GetType());
345 AssertEquals (typeof(Int64Converter), TypeDescriptor.GetConverter (typeof (long)).GetType());
346 AssertEquals (typeof(UInt16Converter), TypeDescriptor.GetConverter (typeof (ushort)).GetType());
347 AssertEquals (typeof(UInt32Converter), TypeDescriptor.GetConverter (typeof (uint)).GetType());
348 AssertEquals (typeof(UInt64Converter), TypeDescriptor.GetConverter (typeof (ulong)).GetType());
349 AssertEquals (typeof(SingleConverter), TypeDescriptor.GetConverter (typeof (float)).GetType());
350 AssertEquals (typeof(DoubleConverter), TypeDescriptor.GetConverter (typeof (double)).GetType());
351 AssertEquals (typeof(DecimalConverter), TypeDescriptor.GetConverter (typeof (decimal)).GetType());
352 AssertEquals (typeof(ArrayConverter), TypeDescriptor.GetConverter (typeof (Array)).GetType());
353 AssertEquals (typeof(CultureInfoConverter), TypeDescriptor.GetConverter (typeof (CultureInfo)).GetType());
354 AssertEquals (typeof(DateTimeConverter), TypeDescriptor.GetConverter (typeof (DateTime)).GetType());
355 AssertEquals (typeof(GuidConverter), TypeDescriptor.GetConverter (typeof (Guid)).GetType());
356 AssertEquals (typeof(TimeSpanConverter), TypeDescriptor.GetConverter (typeof (TimeSpan)).GetType());
357 AssertEquals (typeof(CollectionConverter), TypeDescriptor.GetConverter (typeof (ICollection)).GetType());
359 // Tests from bug #71444
360 AssertEquals (typeof(CollectionConverter), TypeDescriptor.GetConverter (typeof (IDictionary)).GetType());
361 AssertEquals (typeof(ReferenceConverter), TypeDescriptor.GetConverter (typeof (ITestInterface)).GetType());
362 AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (typeof (TestClass)).GetType());
363 AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (typeof (TestStruct)).GetType());
365 AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (new TestClass ()).GetType());
366 AssertEquals (typeof(TypeConverter), TypeDescriptor.GetConverter (new TestStruct ()).GetType());
367 AssertEquals (typeof(CollectionConverter), TypeDescriptor.GetConverter (new Hashtable ()).GetType());
370 // Test from bug #76686
371 AssertEquals (typeof (Int32Converter), TypeDescriptor.GetConverter ((int?) 1).GetType ());
376 public void TestGetDefaultEvent ()
378 EventDescriptor des = TypeDescriptor.GetDefaultEvent (typeof(MyComponent));
379 AssertNull ("t1", des);
381 des = TypeDescriptor.GetDefaultEvent (com);
382 AssertNull ("t2", des);
384 des = TypeDescriptor.GetDefaultEvent (typeof(AnotherComponent));
385 AssertNotNull ("t3", des);
386 AssertEquals ("t4", "AnotherEvent", des.Name);
388 des = TypeDescriptor.GetDefaultEvent (anothercom);
389 AssertNotNull ("t5", des);
390 AssertEquals ("t6", "AnotherEvent", des.Name);
392 des = TypeDescriptor.GetDefaultEvent (sitedcom);
394 AssertNull ("t7", des);
396 AssertNotNull ("t7/1", des);
397 AssertEquals ("t7/2", "AnotherEvent", des.Name);
400 des = TypeDescriptor.GetDefaultEvent (new MyComponent(new AnotherSite ()));
401 AssertNotNull ("t8", des);
402 AssertEquals ("t9", "AnEvent", des.Name);
404 des = TypeDescriptor.GetDefaultEvent (new AnotherComponent(new AnotherSite ()));
405 AssertNotNull ("t10", des);
406 AssertEquals ("t11", "AnEvent", des.Name);
410 public void TestGetDefaultProperty ()
412 PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (typeof(MyComponent));
413 AssertNull ("t1", des);
415 des = TypeDescriptor.GetDefaultProperty (com);
416 AssertNull ("t2", des);
418 des = TypeDescriptor.GetDefaultProperty (typeof(AnotherComponent));
419 AssertNotNull ("t1", des);
420 AssertEquals ("t2", "AnotherProperty", des.Name);
422 des = TypeDescriptor.GetDefaultProperty (anothercom);
423 AssertNotNull ("t1", des);
424 AssertEquals ("t2", "AnotherProperty", des.Name);
429 // throws NullReferenceException on MS.NET 1.x due to bug
430 // which is fixed in MS.NET 2.0
431 [NUnit.Framework.Category("NotDotNet")]
433 public void TestGetDefaultProperty2 ()
435 PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (sitedcom);
436 AssertNull ("t1", des);
438 des = TypeDescriptor.GetDefaultProperty (new MyComponent (new AnotherSite ()));
439 AssertNotNull ("t2", des);
440 AssertEquals ("t3", "TestProperty", des.Name);
442 des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new AnotherSite ()));
443 AssertNotNull ("t4", des);
444 AssertEquals ("t5", "TestProperty", des.Name);
446 des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new MySite ()));
447 AssertNull ("t6", des);
451 public void TestGetEvents ()
453 EventDescriptorCollection col = TypeDescriptor.GetEvents (typeof(MyComponent));
455 AssertEquals ("t1.1", 3, col.Count);
456 Assert ("t1.2", col.Find ("AnEvent", true) != null);
457 Assert ("t1.3", col.Find ("AnotherEvent", true) != null);
458 Assert ("t1.4", col.Find ("Disposed", true) != null);
460 col = TypeDescriptor.GetEvents (com);
461 AssertEquals ("t2.1", 3, col.Count);
462 Assert ("t2.2", col.Find ("AnEvent", true) != null);
463 Assert ("t2.3", col.Find ("AnotherEvent", true) != null);
464 Assert ("t2.4", col.Find ("Disposed", true) != null);
466 col = TypeDescriptor.GetEvents (sitedcom);
467 AssertEquals ("t3.1", 2, col.Count);
468 Assert ("t3.2", col.Find ("AnotherEvent", true) != null);
469 Assert ("t3.3", col.Find ("Disposed", true) != null);
471 Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
473 col = TypeDescriptor.GetEvents (typeof(MyComponent), filter);
474 AssertEquals ("t4.1", 1, col.Count);
475 Assert ("t4.2", col.Find ("AnEvent", true) != null);
477 col = TypeDescriptor.GetEvents (com, filter);
478 AssertEquals ("t5.1", 1, col.Count);
479 Assert ("t5.2", col.Find ("AnEvent", true) != null);
481 col = TypeDescriptor.GetEvents (sitedcom, filter);
482 AssertEquals ("t6", 0, col.Count);
486 public void TestGetProperties ()
488 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof(MyComponent));
489 Assert ("t1.1", col.Find ("TestProperty", true) != null);
490 Assert ("t1.2", col.Find ("AnotherProperty", true) != null);
492 col = TypeDescriptor.GetProperties (com);
493 Assert ("t2.1", col.Find ("TestProperty", true) != null);
494 Assert ("t2.2", col.Find ("AnotherProperty", true) != null);
496 Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
498 col = TypeDescriptor.GetProperties (typeof(MyComponent), filter);
499 Assert ("t4.1", col.Find ("TestProperty", true) != null);
500 Assert ("t4.2", col.Find ("AnotherProperty", true) == null);
502 col = TypeDescriptor.GetProperties (com, filter);
503 Assert ("t5.1", col.Find ("TestProperty", true) != null);
504 Assert ("t5.2", col.Find ("AnotherProperty", true) == null);
510 // throws NullReferenceException on MS.NET 1.x due to bug
511 // which is fixed in MS.NET 2.0
512 [NUnit.Framework.Category("NotDotNet")]
514 public void TestGetProperties2 ()
516 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (sitedcom);
517 Assert ("t3.1", col.Find ("TestProperty", true) == null);
518 Assert ("t3.2", col.Find ("AnotherProperty", true) != null);
520 Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
521 col = TypeDescriptor.GetProperties (sitedcom, filter);
522 Assert ("t6.1", col.Find ("TestProperty", true) == null);
523 Assert ("t6.2", col.Find ("AnotherProperty", true) == null);
526 [TypeConverter (typeof (TestConverter))]
527 class TestConverterClass {
530 class TestConverter : TypeConverter {
533 public TestConverter (Type type)
540 public void TestConverterCtorWithArgument ()
542 TypeConverter t = TypeDescriptor.GetConverter (typeof (TestConverterClass));
543 Assert ("#01", null != t.GetType ());
544 AssertEquals ("#02", typeof (TestConverter), t.GetType ());
545 TestConverter converter = (TestConverter) t;
546 AssertEquals ("#03", typeof (TestConverterClass), converter.Type);
550 public void GetPropertiesIgnoreIndexers ()
552 PropertyDescriptorCollection pc =
553 TypeDescriptor.GetProperties (typeof (string));
554 // There are two string properties: Length and Chars.
555 // Chars is an indexer.
557 // Future version of CLI might contain some additional
558 // properties. In that case simply increase the
559 // number. (Also, it is fine to just remove #2.)
560 AssertEquals ("#1", 1, pc.Count);
561 AssertEquals ("#2", "Length", pc [0].Name);