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 () { }
37 public IComponent Component { get { return null; } }
39 public IContainer Container { get { return null; } }
41 public bool DesignMode { get { return true; } }
43 public string Name { get { return "TestName"; } set { } }
45 public object GetService (Type t)
47 if (t == typeof(ITypeDescriptorFilterService)) return new MyFilter ();
52 class MyFilter: ITypeDescriptorFilterService
54 public bool FilterAttributes (IComponent component,IDictionary attributes)
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;
65 public bool FilterEvents (IComponent component, IDictionary events)
67 events.Remove ("AnEvent");
71 public bool FilterProperties (IComponent component, IDictionary properties)
73 properties.Remove ("TestProperty");
78 class AnotherSite: ISite
80 public IComponent Component { get { return null; } }
82 public IContainer Container { get { return null; } }
84 public bool DesignMode { get { return true; } }
86 public string Name { get { return "TestName"; } set { } }
88 public object GetService (Type t)
90 if (t == typeof(ITypeDescriptorFilterService)) {
91 return new AnotherFilter ();
97 class AnotherFilter: ITypeDescriptorFilterService
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;
109 public bool FilterEvents (IComponent component, IDictionary events) {
113 public bool FilterProperties (IComponent component, IDictionary properties) {
118 [DescriptionAttribute ("my test component")]
119 [DesignerAttribute (typeof(MyDesigner), typeof(int))]
120 public class MyComponent: Component
124 [DescriptionAttribute ("test")]
125 public event EventHandler AnEvent;
127 public event EventHandler AnotherEvent;
129 public MyComponent ()
133 public MyComponent (ISite site)
138 [DescriptionAttribute ("test")]
139 public string TestProperty
142 set { prop = value; }
145 public string AnotherProperty
148 set { prop = value; }
152 [DefaultProperty("AnotherProperty")]
153 [DefaultEvent("AnotherEvent")]
154 [DescriptionAttribute ("my test component")]
155 [DesignerAttribute (typeof(MyDesigner), typeof(int))]
156 public class AnotherComponent: Component {
159 [DescriptionAttribute ("test")]
160 public event EventHandler AnEvent;
162 public event EventHandler AnotherEvent;
164 public AnotherComponent () {
167 public AnotherComponent (ISite site) {
171 [DescriptionAttribute ("test")]
172 public string TestProperty {
174 set { prop = value; }
177 public string AnotherProperty {
179 set { prop = value; }
183 public interface ITestInterface
185 void TestFunction ();
188 public class TestClass
197 public struct TestStruct
203 public class TypeDescriptorTests: Assertion
205 MyComponent com = new MyComponent ();
206 MyComponent sitedcom = new MyComponent (new MySite ());
207 AnotherComponent anothercom = new AnotherComponent ();
210 public void TestCreateDesigner ()
212 IDesigner des = TypeDescriptor.CreateDesigner (com, typeof(int));
213 Assert ("t1", des is MyDesigner);
215 des = TypeDescriptor.CreateDesigner (com, typeof(string));
216 AssertNull ("t2", des);
220 public void TestCreateEvent ()
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);
230 public void TestCreateProperty ()
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);
238 pd.SetValue (com, "hi");
239 AssertEquals ("t5", "hi", pd.GetValue(com));
243 public void TestGetAttributes ()
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);
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);
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);
262 public void TestGetClassName ()
264 AssertEquals ("t1", typeof(MyComponent).FullName, TypeDescriptor.GetClassName (com));
268 public void TestGetComponentName ()
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));
278 // in MS.NET 2.0, GetComponentName no longer returns
279 // the type name if there's no custom typedescriptor
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));
288 AssertEquals ("t7", "TestName", TypeDescriptor.GetComponentName (sitedcom));
289 AssertEquals ("t8", "TestName", TypeDescriptor.GetComponentName (sitedcom));
293 public void TestGetConverter ()
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());
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());
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());
327 // Test from bug #76686
328 AssertEquals (typeof (Int32Converter), TypeDescriptor.GetConverter ((int?) 1).GetType ());
333 public void TestGetDefaultEvent ()
335 EventDescriptor des = TypeDescriptor.GetDefaultEvent (typeof(MyComponent));
336 AssertNull ("t1", des);
338 des = TypeDescriptor.GetDefaultEvent (com);
339 AssertNull ("t2", des);
341 des = TypeDescriptor.GetDefaultEvent (typeof(AnotherComponent));
342 AssertNotNull ("t3", des);
343 AssertEquals ("t4", "AnotherEvent", des.Name);
345 des = TypeDescriptor.GetDefaultEvent (anothercom);
346 AssertNotNull ("t5", des);
347 AssertEquals ("t6", "AnotherEvent", des.Name);
349 des = TypeDescriptor.GetDefaultEvent (sitedcom);
351 AssertNull ("t7", des);
353 AssertNotNull ("t7/1", des);
354 AssertEquals ("t7/2", "AnotherEvent", des.Name);
357 des = TypeDescriptor.GetDefaultEvent (new MyComponent(new AnotherSite ()));
358 AssertNotNull ("t8", des);
359 AssertEquals ("t9", "AnEvent", des.Name);
361 des = TypeDescriptor.GetDefaultEvent (new AnotherComponent(new AnotherSite ()));
362 AssertNotNull ("t10", des);
363 AssertEquals ("t11", "AnEvent", des.Name);
367 public void TestGetDefaultProperty ()
369 PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (typeof(MyComponent));
370 AssertNull ("t1", des);
372 des = TypeDescriptor.GetDefaultProperty (com);
373 AssertNull ("t2", des);
375 des = TypeDescriptor.GetDefaultProperty (typeof(AnotherComponent));
376 AssertNotNull ("t1", des);
377 AssertEquals ("t2", "AnotherProperty", des.Name);
379 des = TypeDescriptor.GetDefaultProperty (anothercom);
380 AssertNotNull ("t1", des);
381 AssertEquals ("t2", "AnotherProperty", des.Name);
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")]
390 public void TestGetDefaultProperty2 ()
392 PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (sitedcom);
393 AssertNull ("t1", des);
395 des = TypeDescriptor.GetDefaultProperty (new MyComponent (new AnotherSite ()));
396 AssertNotNull ("t2", des);
397 AssertEquals ("t3", "TestProperty", des.Name);
399 des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new AnotherSite ()));
400 AssertNotNull ("t4", des);
401 AssertEquals ("t5", "TestProperty", des.Name);
403 des = TypeDescriptor.GetDefaultProperty (new AnotherComponent (new MySite ()));
404 AssertNull ("t6", des);
408 public void TestGetEvents ()
410 EventDescriptorCollection col = TypeDescriptor.GetEvents (typeof(MyComponent));
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);
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);
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);
428 Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
430 col = TypeDescriptor.GetEvents (typeof(MyComponent), filter);
431 AssertEquals ("t4.1", 1, col.Count);
432 Assert ("t4.2", col.Find ("AnEvent", true) != null);
434 col = TypeDescriptor.GetEvents (com, filter);
435 AssertEquals ("t5.1", 1, col.Count);
436 Assert ("t5.2", col.Find ("AnEvent", true) != null);
438 col = TypeDescriptor.GetEvents (sitedcom, filter);
439 AssertEquals ("t6", 0, col.Count);
443 public void TestGetProperties ()
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);
449 col = TypeDescriptor.GetProperties (com);
450 Assert ("t2.1", col.Find ("TestProperty", true) != null);
451 Assert ("t2.2", col.Find ("AnotherProperty", true) != null);
453 Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
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);
459 col = TypeDescriptor.GetProperties (com, filter);
460 Assert ("t5.1", col.Find ("TestProperty", true) != null);
461 Assert ("t5.2", col.Find ("AnotherProperty", true) == null);
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")]
471 public void TestGetProperties2 ()
473 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (sitedcom);
474 Assert ("t3.1", col.Find ("TestProperty", true) == null);
475 Assert ("t3.2", col.Find ("AnotherProperty", true) != null);
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);
483 [TypeConverter (typeof (TestConverter))]
484 class TestConverterClass {
487 class TestConverter : TypeConverter {
490 public TestConverter (Type type)
497 public void TestConverterCtorWithArgument ()
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);