+
+ [Test] // bug #80292
+ public void DisplayNameTest ()
+ {
+ PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Explicit"];
+ PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Implicit"];
+
+ Assert.AreEqual ("An explicit displayname", p1.DisplayName, "#1");
+ Assert.AreEqual ("Implicit", p2.DisplayName, "#2");
+ }
+
+ [Test]
+ public void AddValueChanged ()
+ {
+ MockPropertyDescriptor pd = new MockPropertyDescriptor (
+ "Name", new Attribute [0]);
+ object compA = new object ();
+ object compB = new object ();
+ EventHandler handlerA = new EventHandler (ValueChanged1);
+ EventHandler handlerB = new EventHandler (ValueChanged1);
+ EventHandler handlerC = new EventHandler (ValueChanged2);
+
+ pd.AddValueChanged (compA, handlerA);
+ pd.AddValueChanged (compA, handlerC);
+ pd.AddValueChanged (compA, handlerC);
+ pd.AddValueChanged (compA, handlerB);
+
+ pd.FireValueChanged (compA, new EventArgs ());
+ Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
+ Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#A1");
+ Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#A2");
+ Assert.AreEqual ("ValueChanged2", _invokedHandlers [2], "#A3");
+ Assert.AreEqual ("ValueChanged1", _invokedHandlers [3], "#A4");
+
+ Reset ();
+
+ pd.FireValueChanged (compB, new EventArgs ());
+ Assert.AreEqual (0, _invokedHandlers.Count, "#B");
+ }
+
+ [Test]
+ public void AddValueChanged_Component_Null ()
+ {
+ MockPropertyDescriptor pd = new MockPropertyDescriptor (
+ "Name", new Attribute [0]);
+ try {
+ pd.AddValueChanged (null, new EventHandler (ValueChanged1));
+ Assert.Fail ("#1");
+ } catch (ArgumentNullException ex) {
+ Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNotNull (ex.ParamName, "#5");
+ Assert.AreEqual ("component", ex.ParamName, "#6");
+ }
+ }
+
+ [Test]
+ public void AddValueChanged_Handler_Null ()
+ {
+ MockPropertyDescriptor pd = new MockPropertyDescriptor (
+ "Name", new Attribute [0]);
+ try {
+ pd.AddValueChanged (new object (), (EventHandler) null);
+ Assert.Fail ("#1");
+ } catch (ArgumentNullException ex) {
+ Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNotNull (ex.ParamName, "#5");
+ Assert.AreEqual ("handler", ex.ParamName, "#6");
+ }
+ }
+
+ [Test]
+ public void GetInvocationTarget_Instance_Null ()
+ {
+ MockPropertyDescriptor pd = new MockPropertyDescriptor (
+ "Name", new Attribute [0]);
+ try {
+ pd.GetInvocationTarget (typeof (int), null);
+ Assert.Fail ("#1");
+ } catch (ArgumentNullException ex) {
+ Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNotNull (ex.ParamName, "#5");
+ Assert.AreEqual ("instance", ex.ParamName, "#6");
+ }
+ }
+
+ [Test]
+ public void GetInvocationTarget_Type_Null ()
+ {
+ MockPropertyDescriptor pd = new MockPropertyDescriptor (
+ "Name", new Attribute [0]);
+ try {
+ pd.GetInvocationTarget ((Type) null, new object ());
+ Assert.Fail ("#1");
+ } catch (ArgumentNullException ex) {
+ Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNotNull (ex.ParamName, "#5");
+ Assert.AreEqual ("type", ex.ParamName, "#6");
+ }
+ }
+
+ [Test]
+ public void GetValueChangedHandler ()
+ {
+ object compA = new object ();
+ object compB = new object ();
+ EventHandler handlerA = new EventHandler (ValueChanged1);
+ EventHandler handlerB = new EventHandler (ValueChanged1);
+ EventHandler handlerC = new EventHandler (ValueChanged2);
+
+ MockPropertyDescriptor pd = new MockPropertyDescriptor (
+ "Name", new Attribute [0]);
+ Assert.IsNull (pd.GetValueChangedHandler (null), "#A1");
+ Assert.IsNull (pd.GetValueChangedHandler (compA), "#A2");
+ Assert.IsNull (pd.GetValueChangedHandler (compB), "#A3");
+
+ pd.AddValueChanged (compA, handlerA);
+
+ Assert.IsNull (pd.GetValueChangedHandler (null), "#B1");
+ Assert.AreSame (handlerA, pd.GetValueChangedHandler (compA), "#B2");
+ Assert.IsNull (pd.GetValueChangedHandler (compB), "#B3");
+
+ pd.AddValueChanged (compA, handlerB);
+
+ Assert.IsNull (pd.GetValueChangedHandler (null), "#C1");
+ EventHandler handler = pd.GetValueChangedHandler (compA);
+ Assert.AreEqual (2, handler.GetInvocationList ().Length, "#C2");
+ Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#C3");
+ Assert.AreEqual (handlerB, handler.GetInvocationList () [1], "#C4");
+ Assert.IsNull (pd.GetValueChangedHandler (compB), "#C5");
+
+ pd.AddValueChanged (compB, handlerA);
+
+ Assert.IsNull (pd.GetValueChangedHandler (null), "#D1");
+ handler = pd.GetValueChangedHandler (compA);
+ Assert.AreEqual (2, handler.GetInvocationList ().Length, "#D2");
+ Assert.AreSame (handlerA, pd.GetValueChangedHandler (compB), "#D3");
+
+ pd.RemoveValueChanged (compB, handlerB);
+
+ Assert.IsNull (pd.GetValueChangedHandler (null), "#E1");
+ handler = pd.GetValueChangedHandler (compA);
+ Assert.AreEqual (2, handler.GetInvocationList ().Length, "#E2");
+ Assert.IsNull (pd.GetValueChangedHandler (compB), "#E3");
+
+ pd.RemoveValueChanged (compB, handlerB);
+
+ Assert.IsNull (pd.GetValueChangedHandler (null), "#F1");
+ handler = pd.GetValueChangedHandler (compA);
+ Assert.AreEqual (2, handler.GetInvocationList ().Length, "#F2");
+ Assert.IsNull (pd.GetValueChangedHandler (compB), "#F3");
+
+ pd.RemoveValueChanged (compA, handlerC);
+
+ Assert.IsNull (pd.GetValueChangedHandler (null), "#G1");
+ handler = pd.GetValueChangedHandler (compA);
+ Assert.AreEqual (2, handler.GetInvocationList ().Length, "#G2");
+ Assert.IsNull (pd.GetValueChangedHandler (compB), "#G3");
+
+ pd.AddValueChanged (compA, handlerC);
+
+ Assert.IsNull (pd.GetValueChangedHandler (null), "#H1");
+ handler = pd.GetValueChangedHandler (compA);
+ Assert.AreEqual (3, handler.GetInvocationList ().Length, "#H2");
+ Assert.IsNull (pd.GetValueChangedHandler (compB), "#H3");
+
+ pd.RemoveValueChanged (compA, handlerB);
+
+ Assert.IsNull (pd.GetValueChangedHandler (null), "#I1");
+ handler = pd.GetValueChangedHandler (compA);
+ Assert.AreEqual (2, handler.GetInvocationList ().Length, "#I2");
+ Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#I3");
+ Assert.AreEqual (handlerC, handler.GetInvocationList () [1], "#I4");
+ Assert.IsNull (pd.GetValueChangedHandler (compB), "#I5");
+ }
+
+ [Test]
+ public void RemoveValueChanged ()
+ {
+ MockPropertyDescriptor pd = new MockPropertyDescriptor (
+ "Name", new Attribute [0]);
+ object compA = new object ();
+ object compB = new object ();
+ EventHandler handlerA = new EventHandler (ValueChanged1);
+ EventHandler handlerB = new EventHandler (ValueChanged1);
+ EventHandler handlerC = new EventHandler (ValueChanged2);
+
+ pd.AddValueChanged (compA, handlerA);
+ pd.AddValueChanged (compA, handlerC);
+ pd.AddValueChanged (compA, handlerC);
+ pd.AddValueChanged (compA, handlerB);
+ pd.AddValueChanged (compB, handlerC);
+
+ pd.FireValueChanged (compA, new EventArgs ());
+ Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
+ pd.RemoveValueChanged (new object (), handlerC);
+ pd.FireValueChanged (compA, new EventArgs ());
+ Assert.AreEqual (8, _invokedHandlers.Count, "#A2");
+
+ Reset ();
+ pd.RemoveValueChanged (compA, handlerC);
+
+ pd.FireValueChanged (compA, new EventArgs ());
+ Assert.AreEqual (3, _invokedHandlers.Count, "#B1");
+ Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#B2");
+ Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#B3");
+ Assert.AreEqual ("ValueChanged1", _invokedHandlers [2], "#B4");
+
+ Reset ();
+
+ pd.FireValueChanged (compB, new EventArgs ());
+ Assert.AreEqual (1, _invokedHandlers.Count, "#C1");
+ Assert.AreEqual ("ValueChanged2", _invokedHandlers [0], "#C2");
+
+ Reset ();
+ pd.RemoveValueChanged (compB, handlerC);
+
+ pd.FireValueChanged (compB, new EventArgs ());
+ Assert.AreEqual (0, _invokedHandlers.Count, "#D");
+ }
+
+ [Test]
+ public void RemoveValueChanged_Component_Null ()
+ {
+ MockPropertyDescriptor pd = new MockPropertyDescriptor (
+ "Name", new Attribute [0]);
+ try {
+ pd.RemoveValueChanged (null, new EventHandler (ValueChanged1));
+ Assert.Fail ("#1");
+ } catch (ArgumentNullException ex) {
+ Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNotNull (ex.ParamName, "#5");
+ Assert.AreEqual ("component", ex.ParamName, "#6");
+ }
+ }
+
+ [Test]
+ public void RemoveValueChanged_Handler_Null ()
+ {
+ MockPropertyDescriptor pd = new MockPropertyDescriptor (
+ "Name", new Attribute [0]);
+ try {
+ pd.RemoveValueChanged (new object (), (EventHandler) null);
+ Assert.Fail ("#1");
+ } catch (ArgumentNullException ex) {
+ Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNotNull (ex.ParamName, "#5");
+ Assert.AreEqual ("handler", ex.ParamName, "#6");
+ }
+ }
+
+ void ValueChanged1 (object sender, EventArgs e)
+ {
+ _invokedHandlers.Add ("ValueChanged1");
+ }
+
+ void ValueChanged2 (object sender, EventArgs e)
+ {
+ _invokedHandlers.Add ("ValueChanged2");
+ }
+
+ static Attribute FindAttribute (PropertyDescriptor pd, Type type)
+ {
+ foreach (Attribute attr in pd.Attributes)
+ if (attr.GetType () == type)
+ return attr;
+ return null;
+ }
+#if !MOBILE
+ class GetEditor_test
+ {
+ [Editor (typeof (UIEditor), typeof (UITypeEditor))]
+ public string Property {
+ get { return "abc"; }
+ set { }
+ }
+ }
+
+ class UIEditor : UITypeEditor
+ {
+ }
+
+ [Test]
+ public void GetEditorTest ()
+ {
+ PropertyDescriptorCollection col;
+ PropertyDescriptor pd;
+ UITypeEditor ed;
+
+ col = TypeDescriptor.GetProperties (typeof (GetEditor_test));
+ pd = col [0];
+ ed = pd.GetEditor (typeof (UITypeEditor)) as UITypeEditor;
+
+ Assert.IsNotNull (ed, "#01");
+ Assert.AreEqual (ed.GetType ().Name, "UIEditor", "#02");
+ }
+#endif
+
+ class MockPropertyDescriptor : PropertyDescriptor
+ {
+ public MockPropertyDescriptor (MemberDescriptor reference)
+ : base (reference)
+ {
+ }
+
+ public MockPropertyDescriptor (MemberDescriptor reference, Attribute [] attrs)
+ : base (reference, attrs)
+ {
+ }
+
+ public MockPropertyDescriptor (string name, Attribute [] attrs)
+ : base (name, attrs)
+ {
+ }
+
+ public override Type ComponentType {
+ get { return typeof (int); }
+ }
+
+ public override bool IsReadOnly {
+ get { return false; }
+ }
+
+ public override Type PropertyType{
+ get { return typeof (DateTime); }
+ }
+
+ public override object GetValue (object component)
+ {
+ return null;
+ }
+
+ public override void SetValue (object component, object value)
+ {
+ }
+
+ public override void ResetValue (object component)
+ {
+ }
+
+ public override bool CanResetValue (object component)
+ {
+ return true;
+ }
+
+ public override bool ShouldSerializeValue (object component)
+ {
+ return true;
+ }
+
+ public void FireValueChanged (object component, EventArgs e)
+ {
+ base.OnValueChanged (component, e);
+ }
+
+ public new object GetInvocationTarget (Type type, object instance)
+ {
+ return base.GetInvocationTarget (type, instance);
+ }
+
+ public new EventHandler GetValueChangedHandler (object component)
+ {
+ return base.GetValueChangedHandler (component);
+ }
+ }
+
+ [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
+ public class PropTestAttribute : Attribute
+ {
+ public PropTestAttribute ()
+ {
+ }
+ }
+
+ public class TestBase
+ {
+ [PropTest]
+ public int PropBase1
+ {
+ get { return 0; }
+ set { }
+ }
+
+ [PropTest]
+ [Description ("whatever")]
+ public string PropBase2
+ {
+ get { return ""; }
+ set { }
+ }
+
+ [PropTest]
+ public virtual string PropBase3
+ {
+ get { return ""; }
+ set { }
+ }
+ }
+
+ public class TestSub : TestBase
+ {
+ [PropTest]
+ public int PropSub1
+ {
+ get { return 0; }
+ set { }
+ }
+
+ [PropTest]
+ public string PropSub2
+ {
+ get { return ""; }
+ set { }
+ }
+
+ public override string PropBase3
+ {
+ get { return ""; }
+ set { }
+ }
+ }