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; }
113 public class TypeDescriptorTests
: Assertion
115 MyComponent com
= new MyComponent ();
116 MyComponent sitedcom
= new MyComponent (new MySite ());
119 public void TestCreateDesigner ()
121 IDesigner des
= TypeDescriptor
.CreateDesigner (com
, typeof(int));
122 Assert ("t1", des
is MyDesigner
);
124 des
= TypeDescriptor
.CreateDesigner (com
, typeof(string));
125 AssertNull ("t2", des
);
129 public void TestCreateEvent ()
131 EventDescriptor ed
= TypeDescriptor
.CreateEvent (typeof(MyComponent
), "AnEvent", typeof(EventHandler
), null);
132 AssertEquals ("t1", typeof(MyComponent
), ed
.ComponentType
);
133 AssertEquals ("t2", typeof(EventHandler
), ed
.EventType
);
134 AssertEquals ("t3", true, ed
.IsMulticast
);
135 AssertEquals ("t4", "AnEvent", ed
.Name
);
139 public void TestCreateProperty ()
141 PropertyDescriptor pd
= TypeDescriptor
.CreateProperty (typeof(MyComponent
), "TestProperty", typeof(string), null);
142 AssertEquals ("t1", typeof(MyComponent
), pd
.ComponentType
);
143 AssertEquals ("t2", "TestProperty", pd
.Name
);
144 AssertEquals ("t3", typeof(string), pd
.PropertyType
);
145 AssertEquals ("t4", false, pd
.IsReadOnly
);
147 pd
.SetValue (com
, "hi");
148 AssertEquals ("t5", "hi", pd
.GetValue(com
));
152 public void TestGetAttributes ()
154 AttributeCollection col
= TypeDescriptor
.GetAttributes (typeof(MyComponent
));
155 Assert ("t2", col
[typeof(DescriptionAttribute
)] != null);
156 Assert ("t3", col
[typeof(DesignerAttribute
)] != null);
157 Assert ("t4", col
[typeof(EditorAttribute
)] == null);
159 col
= TypeDescriptor
.GetAttributes (com
);
160 Assert ("t6", col
[typeof(DescriptionAttribute
)] != null);
161 Assert ("t7", col
[typeof(DesignerAttribute
)] != null);
162 Assert ("t8", col
[typeof(EditorAttribute
)] == null);
164 col
= TypeDescriptor
.GetAttributes (sitedcom
);
165 Assert ("t10", col
[typeof(DescriptionAttribute
)] != null);
166 Assert ("t11", col
[typeof(DesignerAttribute
)] != null);
167 Assert ("t12", col
[typeof(EditorAttribute
)] != null);
171 public void TestGetClassName ()
173 AssertEquals ("t1", typeof(MyComponent
).FullName
, TypeDescriptor
.GetClassName (com
));
177 public void TestGetComponentName ()
179 AssertNull ("t1", TypeDescriptor
.GetComponentName (com
));
180 AssertEquals ("t1", "TestName", TypeDescriptor
.GetComponentName (sitedcom
));
184 public void TestGetConverter ()
186 AssertEquals (typeof(BooleanConverter
), TypeDescriptor
.GetConverter (typeof (bool)).GetType());
187 AssertEquals (typeof(ByteConverter
), TypeDescriptor
.GetConverter (typeof (byte)).GetType());
188 AssertEquals (typeof(SByteConverter
), TypeDescriptor
.GetConverter (typeof (sbyte)).GetType());
189 AssertEquals (typeof(StringConverter
), TypeDescriptor
.GetConverter (typeof (string)).GetType());
190 AssertEquals (typeof(CharConverter
), TypeDescriptor
.GetConverter (typeof (char)).GetType());
191 AssertEquals (typeof(Int16Converter
), TypeDescriptor
.GetConverter (typeof (short)).GetType());
192 AssertEquals (typeof(Int32Converter
), TypeDescriptor
.GetConverter (typeof (int)).GetType());
193 AssertEquals (typeof(Int64Converter
), TypeDescriptor
.GetConverter (typeof (long)).GetType());
194 AssertEquals (typeof(UInt16Converter
), TypeDescriptor
.GetConverter (typeof (ushort)).GetType());
195 AssertEquals (typeof(UInt32Converter
), TypeDescriptor
.GetConverter (typeof (uint)).GetType());
196 AssertEquals (typeof(UInt64Converter
), TypeDescriptor
.GetConverter (typeof (ulong)).GetType());
197 AssertEquals (typeof(SingleConverter
), TypeDescriptor
.GetConverter (typeof (float)).GetType());
198 AssertEquals (typeof(DoubleConverter
), TypeDescriptor
.GetConverter (typeof (double)).GetType());
199 AssertEquals (typeof(DecimalConverter
), TypeDescriptor
.GetConverter (typeof (decimal)).GetType());
200 AssertEquals (typeof(ArrayConverter
), TypeDescriptor
.GetConverter (typeof (Array
)).GetType());
201 AssertEquals (typeof(CultureInfoConverter
), TypeDescriptor
.GetConverter (typeof (CultureInfo
)).GetType());
202 AssertEquals (typeof(DateTimeConverter
), TypeDescriptor
.GetConverter (typeof (DateTime
)).GetType());
203 AssertEquals (typeof(GuidConverter
), TypeDescriptor
.GetConverter (typeof (Guid
)).GetType());
204 AssertEquals (typeof(TimeSpanConverter
), TypeDescriptor
.GetConverter (typeof (TimeSpan
)).GetType());
205 AssertEquals (typeof(CollectionConverter
), TypeDescriptor
.GetConverter (typeof (ICollection
)).GetType());
209 public void TestGetDefaultEvent ()
211 EventDescriptor des
= TypeDescriptor
.GetDefaultEvent (typeof(MyComponent
));
212 AssertNull ("t1", des
);
214 des
= TypeDescriptor
.GetDefaultEvent (com
);
215 AssertNull ("t2", des
);
217 des
= TypeDescriptor
.GetDefaultEvent (sitedcom
);
218 AssertNotNull ("t3", des
);
219 AssertEquals ("t4", "AnEvent", des
.Name
);
223 public void TestGetDefaultProperty ()
225 PropertyDescriptor des
= TypeDescriptor
.GetDefaultProperty (typeof(MyComponent
));
226 AssertNull ("t1", des
);
228 des
= TypeDescriptor
.GetDefaultProperty (com
);
229 AssertNull ("t2", des
);
231 des
= TypeDescriptor
.GetDefaultProperty (sitedcom
);
232 AssertNotNull ("t3", des
);
233 AssertEquals ("t4", "TestProperty", des
.Name
);
237 public void TestGetEvents ()
239 EventDescriptorCollection col
= TypeDescriptor
.GetEvents (typeof(MyComponent
));
241 AssertEquals ("t1.1", 3, col
.Count
);
242 Assert ("t1.2", col
.Find ("AnEvent", true) != null);
243 Assert ("t1.3", col
.Find ("AnotherEvent", true) != null);
244 Assert ("t1.4", col
.Find ("Disposed", true) != null);
246 col
= TypeDescriptor
.GetEvents (com
);
247 AssertEquals ("t2.1", 3, col
.Count
);
248 Assert ("t2.2", col
.Find ("AnEvent", true) != null);
249 Assert ("t2.3", col
.Find ("AnotherEvent", true) != null);
250 Assert ("t2.4", col
.Find ("Disposed", true) != null);
252 col
= TypeDescriptor
.GetEvents (sitedcom
);
253 AssertEquals ("t3.1", 2, col
.Count
);
254 Assert ("t3.2", col
.Find ("AnotherEvent", true) != null);
255 Assert ("t3.3", col
.Find ("Disposed", true) != null);
257 Attribute
[] filter
= new Attribute
[] { new DescriptionAttribute ("test") }
;
259 col
= TypeDescriptor
.GetEvents (typeof(MyComponent
), filter
);
260 AssertEquals ("t4.1", 1, col
.Count
);
261 Assert ("t4.2", col
.Find ("AnEvent", true) != null);
263 col
= TypeDescriptor
.GetEvents (com
, filter
);
264 AssertEquals ("t5.1", 1, col
.Count
);
265 Assert ("t5.2", col
.Find ("AnEvent", true) != null);
267 col
= TypeDescriptor
.GetEvents (sitedcom
, filter
);
268 AssertEquals ("t6", 0, col
.Count
);
272 public void TestGetProperties ()
274 PropertyDescriptorCollection col
= TypeDescriptor
.GetProperties (typeof(MyComponent
));
275 Assert ("t1.1", col
.Find ("TestProperty", true) != null);
276 Assert ("t1.2", col
.Find ("AnotherProperty", true) != null);
278 col
= TypeDescriptor
.GetProperties (com
);
279 Assert ("t2.1", col
.Find ("TestProperty", true) != null);
280 Assert ("t2.2", col
.Find ("AnotherProperty", true) != null);
282 col
= TypeDescriptor
.GetProperties (sitedcom
);
283 Assert ("t3.1", col
.Find ("TestProperty", true) == null);
284 Assert ("t3.2", col
.Find ("AnotherProperty", true) != null);
286 Attribute
[] filter
= new Attribute
[] { new DescriptionAttribute ("test") }
;
288 col
= TypeDescriptor
.GetProperties (typeof(MyComponent
), filter
);
289 Assert ("t4.1", col
.Find ("TestProperty", true) != null);
290 Assert ("t4.2", col
.Find ("AnotherProperty", true) == null);
292 col
= TypeDescriptor
.GetProperties (com
, filter
);
293 Assert ("t5.1", col
.Find ("TestProperty", true) != null);
294 Assert ("t5.2", col
.Find ("AnotherProperty", true) == null);
296 col
= TypeDescriptor
.GetProperties (sitedcom
, filter
);
297 Assert ("t6.1", col
.Find ("TestProperty", true) == null);
298 Assert ("t6.2", col
.Find ("AnotherProperty", true) == null);