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 [DescriptionAttribute ("my test component")]
79 [DesignerAttribute (typeof(MyDesigner), typeof(int))]
80 public class MyComponent: Component
84 [DescriptionAttribute ("test")]
85 public event EventHandler AnEvent;
87 public event EventHandler AnotherEvent;
93 public MyComponent (ISite site)
98 [DescriptionAttribute ("test")]
99 public string TestProperty
102 set { prop = value; }
105 public string AnotherProperty
108 set { prop = value; }
112 public interface ITestInterface
114 void TestFunction ();
117 public class TestClass
126 public struct TestStruct
132 public class TypeDescriptorTests: Assertion
134 MyComponent com = new MyComponent ();
135 MyComponent sitedcom = new MyComponent (new MySite ());
138 public void TestCreateDesigner ()
140 IDesigner des = TypeDescriptor.CreateDesigner (com, typeof(int));
141 Assert ("t1", des is MyDesigner);
143 des = TypeDescriptor.CreateDesigner (com, typeof(string));
144 AssertNull ("t2", des);
148 public void TestCreateEvent ()
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);
158 public void TestCreateProperty ()
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);
166 pd.SetValue (com, "hi");
167 AssertEquals ("t5", "hi", pd.GetValue(com));
171 public void TestGetAttributes ()
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);
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);
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);
190 public void TestGetClassName ()
192 AssertEquals ("t1", typeof(MyComponent).FullName, TypeDescriptor.GetClassName (com));
196 public void TestGetComponentName ()
198 AssertNotNull ("t1", TypeDescriptor.GetComponentName (com));
199 AssertEquals ("t2", "MyComponent", TypeDescriptor.GetComponentName (com));
200 AssertEquals ("t3", "TestName", TypeDescriptor.GetComponentName (sitedcom));
204 public void TestGetConverter ()
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());
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());
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());
239 public void TestGetDefaultEvent ()
241 EventDescriptor des = TypeDescriptor.GetDefaultEvent (typeof(MyComponent));
242 AssertNull ("t1", des);
244 des = TypeDescriptor.GetDefaultEvent (com);
245 AssertNull ("t2", des);
247 des = TypeDescriptor.GetDefaultEvent (sitedcom);
248 AssertNotNull ("t3", des);
249 AssertEquals ("t4", "AnotherEvent", des.Name);
253 public void TestGetDefaultProperty ()
255 PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (typeof(MyComponent));
256 AssertNull ("t1", des);
258 des = TypeDescriptor.GetDefaultProperty (com);
259 AssertNull ("t2", des);
264 [Ignore("Fails on .NET")]
265 public void TestGetDefaultProperty2 ()
267 PropertyDescriptor des = TypeDescriptor.GetDefaultProperty (sitedcom);
268 AssertNotNull ("t3", des);
269 AssertEquals ("t4", "TestProperty", des.Name);
273 public void TestGetEvents ()
275 EventDescriptorCollection col = TypeDescriptor.GetEvents (typeof(MyComponent));
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);
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);
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);
293 Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
295 col = TypeDescriptor.GetEvents (typeof(MyComponent), filter);
296 AssertEquals ("t4.1", 1, col.Count);
297 Assert ("t4.2", col.Find ("AnEvent", true) != null);
299 col = TypeDescriptor.GetEvents (com, filter);
300 AssertEquals ("t5.1", 1, col.Count);
301 Assert ("t5.2", col.Find ("AnEvent", true) != null);
303 col = TypeDescriptor.GetEvents (sitedcom, filter);
304 AssertEquals ("t6", 0, col.Count);
308 public void TestGetProperties ()
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);
314 col = TypeDescriptor.GetProperties (com);
315 Assert ("t2.1", col.Find ("TestProperty", true) != null);
316 Assert ("t2.2", col.Find ("AnotherProperty", true) != null);
318 Attribute[] filter = new Attribute[] { new DescriptionAttribute ("test") };
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);
324 col = TypeDescriptor.GetProperties (com, filter);
325 Assert ("t5.1", col.Find ("TestProperty", true) != null);
326 Assert ("t5.2", col.Find ("AnotherProperty", true) == null);
331 [Ignore("Fails on .NET")]
332 public void TestGetProperties2 ()
334 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (sitedcom);
335 Assert ("t3.1", col.Find ("TestProperty", true) == null);
336 Assert ("t3.2", col.Find ("AnotherProperty", true) != null);
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);