2 // System.ComponentModel.PropertyDescriptor test cases
5 // Chris Toshok (toshok@ximian.com)
6 // Gert Driesen (drieseng@users.sourceforge.net)
8 // (c) 2006 Novell, Inc. (http://www.novell.com/)
12 using System.Collections;
13 using System.ComponentModel;
14 using DescriptionAttribute = System.ComponentModel.DescriptionAttribute;
15 using System.ComponentModel.Design;
16 using System.Globalization;
17 using System.Reflection;
18 using System.Runtime.InteropServices;
19 #if !MOBILE && !MONOMAC
20 using System.Drawing.Design;
22 using NUnit.Framework;
24 namespace MonoTests.System.ComponentModel
26 internal class MyVersionTypeConverter : TypeConverter
34 public virtual string SomeProperty {
35 get { return _someProperty; }
36 set { _someProperty = value; }
40 class VirtPropChildNoSetter : VirtPropParent
42 public override string SomeProperty {
43 get { return base.SomeProperty + ": modified"; }
47 class VirtPropChildNoGetter : VirtPropParent
49 public override string SomeProperty {
50 get { return base.SomeProperty + ": modified"; }
56 public class PropertyDescriptorTests
58 class MissingConverterType_test
60 public class NestedClass { }
62 [TypeConverter ("missing-type-name")]
63 public NestedClass Prop {
67 [TypeConverter ("missing-type-name")]
72 [TypeConverter ("missing-type-name")]
73 public string StringProp {
78 class ReadOnlyProperty_test
85 class ReadOnlyAttribute_test
94 class ConflictingReadOnly_test
102 class ShouldSerialize_public_test
108 public bool ShouldSerializeProp()
114 class ShouldSerialize_protected_test
120 protected bool ShouldSerializeProp()
126 class ShouldSerialize_private_test
132 private bool ShouldSerializeProp()
138 class ShouldSerializeFalseEffectOnCanReset_test
145 public bool ShouldSerializeProp()
150 public void ResetProp()
155 class ShouldSerialize_Null_Default
157 [DefaultValue (null)]
159 get { return _prop; }
160 set { _prop = value; }
163 public bool SerializeProp {
164 get { return _serializeProp; }
165 set { _serializeProp = value; }
168 public bool ShouldSerializeProp ()
170 return _serializeProp;
173 private string _prop;
174 private bool _serializeProp;
177 class ShouldSerialize_No_Default
180 get { return _prop; }
181 set { _prop = value; }
184 private string _prop;
187 class ShouldSerialize_ReadOnly
190 [DefaultValue ("ok")]
191 public string Prop1 {
192 get { return _prop1; }
193 set { _prop1 = value; }
197 public string Prop2 {
198 get { return _prop2; }
199 set { _prop2 = value; }
203 public string Prop3 {
204 get { return _prop3; }
205 set { _prop3 = value; }
209 public string Prop4 {
210 get { return _prop4; }
211 set { _prop4 = value; }
214 public string Prop5 {
215 get { return _prop5; }
218 [DefaultValue ("bad")]
219 public string Prop6 {
220 get { return _prop6; }
224 [DefaultValue ("good")]
225 public string Prop7 {
226 get { return _prop7; }
227 set { _prop7 = value; }
231 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
232 public string Prop8 {
237 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
238 public string Prop9 {
242 public bool SerializeProp3 {
243 get { return _serializeProp3; }
244 set { _serializeProp3 = value; }
247 public bool SerializeProp4 {
248 get { return _serializeProp4; }
249 set { _serializeProp4 = value; }
252 public bool SerializeProp5 {
253 get { return _serializeProp5; }
254 set { _serializeProp5 = value; }
257 public bool SerializeProp6 {
258 get { return _serializeProp6; }
259 set { _serializeProp6 = value; }
262 public bool SerializeProp7 {
263 get { return _serializeProp7; }
264 set { _serializeProp7 = value; }
267 public bool ShouldSerializeProp3 ()
269 return _serializeProp3;
272 public bool ShouldSerializeProp4 ()
274 return _serializeProp4;
277 public bool ShouldSerializeProp5 ()
279 return _serializeProp5;
282 public bool ShouldSerializeProp6 ()
284 return _serializeProp6;
287 public bool ShouldSerializeProp7 ()
289 return _serializeProp7;
292 public bool ShouldSerializeProp8 ()
297 private string _prop1;
298 private string _prop2;
299 private string _prop3;
300 private string _prop4;
301 private string _prop5 = "good";
302 private string _prop6 = "bad";
303 private string _prop7;
304 private bool _serializeProp3;
305 private bool _serializeProp4;
306 private bool _serializeProp5;
307 private bool _serializeProp6;
308 private bool _serializeProp7;
311 class NoSerializeOrResetProp_test
318 class CanReset_public_test
323 set { prop = value; }
326 public void ResetProp()
332 class CanReset_protected_test
337 set { prop = value; }
340 protected void ResetProp()
346 class CanReset_private_test
351 set { prop = value; }
354 private void ResetProp()
360 class CanResetNoSetter_test
367 private void ResetProp()
373 class DisplayName_test
375 [DisplayName ("An explicit displayname")]
376 public bool Explicit {
377 get { return false; }
380 public bool Implicit {
381 get { return false; }
387 public virtual Version NoConverter {
391 [TypeConverter (typeof(MyVersionTypeConverter))]
392 public virtual Version WithConverter {
396 [TypeConverter ("MonoTests.System.ComponentModel.MyVersionTypeConverter")]
397 public virtual Version WithConverterNamed {
402 [TypeConverter("System.ComponentModel.CharConverter, System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
404 [TypeConverter("System.ComponentModel.CharConverter, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
406 public virtual Version WithConverterNamedAssmQuald {
410 public int WithDefaultConverter {
415 class ConverterSubclassNotOverridenProperties_test : Converter_test
419 class ConverterSubclassOverridenProperties_test : Converter_test
421 public override Version WithConverter {
425 public override Version WithConverterNamed {
430 class ConverterEmptyConvertersOnOveriddenProperties : Converter_test
433 public override Version WithConverter {
438 public override Version WithConverterNamed {
443 private ArrayList _invokedHandlers;
448 _invokedHandlers = new ArrayList ();
453 _invokedHandlers.Clear ();
457 public void Attributes ()
459 PropertyDescriptorCollection properties;
460 PropertyDescriptor pd;
462 properties = TypeDescriptor.GetProperties (typeof (TestBase));
464 pd = properties ["PropBase3"];
465 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#A1");
466 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#A2");
468 pd = properties ["PropBase2"];
469 Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#B1");
470 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#B2");
472 pd = properties ["PropBase1"];
473 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#C1");
474 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#C2");
476 properties = TypeDescriptor.GetProperties (typeof (TestSub));
478 pd = properties ["PropBase3"];
479 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#D1");
480 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#D2");
482 pd = properties ["PropBase2"];
483 Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#E1");
484 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#E2");
486 pd = properties ["PropBase1"];
487 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#F1");
488 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#F2");
492 public void VirtualPropertyDontOverrideSetter ()
494 VirtPropChildNoSetter c = new VirtPropChildNoSetter ();
495 PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties (c);
496 foreach (PropertyDescriptor pd in pdc) {
497 if (pd.Name != "SomeProperty")
499 pd.SetValue (c, "testing2");
505 public void VirtualPropertyDontOverrideGetter ()
507 VirtPropChildNoGetter c = new VirtPropChildNoGetter ();
508 PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties (c);
509 foreach (PropertyDescriptor pd in pdc) {
510 if (pd.Name != "SomeProperty")
512 pd.SetValue (c, "testing2");
518 public void MissingTypeConverter ()
520 PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["Prop"];
521 PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["IntProp"];
522 PropertyDescriptor p3 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["StringProp"];
524 Assert.AreEqual (typeof (TypeConverter), p1.Converter.GetType (), "1");
525 Assert.AreEqual (typeof (Int32Converter), p2.Converter.GetType (), "2");
526 Assert.AreEqual (typeof (StringConverter), p3.Converter.GetType (), "3");
530 public void ConverterTest ()
532 Assert.AreEqual (typeof (TypeConverter),
533 TypeDescriptor.GetProperties (typeof (Converter_test))["NoConverter"].Converter.GetType (), "#1");
534 Assert.AreEqual (typeof (MyVersionTypeConverter),
535 TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverter"].Converter.GetType (), "#2");
536 Assert.AreEqual (typeof (MyVersionTypeConverter),
537 TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverterNamed"].Converter.GetType (), "#3");
538 Assert.AreEqual (typeof (CharConverter),
539 TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#4");
540 Assert.AreEqual (typeof (Int32Converter),
541 TypeDescriptor.GetProperties (typeof (Converter_test))["WithDefaultConverter"].Converter.GetType (), "#5");
543 Assert.AreEqual (typeof (TypeConverter),
544 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["NoConverter"].Converter.GetType (), "#6");
545 Assert.AreEqual (typeof (MyVersionTypeConverter),
546 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverter"].Converter.GetType (), "#7");
547 Assert.AreEqual (typeof (MyVersionTypeConverter),
548 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverterNamed"].Converter.GetType (), "#8");
549 Assert.AreEqual (typeof (CharConverter),
550 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#9");
551 Assert.AreEqual (typeof (Int32Converter),
552 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithDefaultConverter"].Converter.GetType (), "#10");
554 Assert.AreEqual (typeof (TypeConverter),
555 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["NoConverter"].Converter.GetType (), "#11");
556 Assert.AreEqual (typeof (MyVersionTypeConverter),
557 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverter"].Converter.GetType (), "#12");
558 Assert.AreEqual (typeof (MyVersionTypeConverter),
559 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverterNamed"].Converter.GetType (), "#13");
560 Assert.AreEqual (typeof (CharConverter),
561 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#14");
562 Assert.AreEqual (typeof (Int32Converter),
563 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithDefaultConverter"].Converter.GetType (), "#15");
565 Assert.AreEqual (typeof (TypeConverter),
566 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["NoConverter"].Converter.GetType (), "#116");
567 Assert.AreEqual (typeof (TypeConverter),
568 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverter"].Converter.GetType (), "#17");
569 Assert.AreEqual (typeof (TypeConverter),
570 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverterNamed"].Converter.GetType (), "#18");
571 Assert.AreEqual (typeof (CharConverter),
572 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverterNamedAssmQuald"].Converter.GetType (), "#19");
573 Assert.AreEqual (typeof (Int32Converter),
574 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithDefaultConverter"].Converter.GetType (), "#20");
578 public void ShouldSerializeTest_public ()
580 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
581 ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
583 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
587 public void ShouldSerializeTest_protected ()
589 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
590 ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
592 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
596 public void ShouldSerializeTest_private ()
598 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
599 ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
601 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
605 public void ShouldSerializeTest_No_Default ()
607 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_No_Default)) ["Prop"];
608 ShouldSerialize_No_Default test = new ShouldSerialize_No_Default ();
610 Assert.IsTrue (p.ShouldSerializeValue (test), "#1");
611 test.Prop = "whatever";
612 Assert.IsTrue (p.ShouldSerializeValue (test), "#2");
616 public void ShouldSerializeTest_Null_Default ()
618 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_Null_Default)) ["Prop"];
619 ShouldSerialize_Null_Default test = new ShouldSerialize_Null_Default ();
621 Assert.IsFalse (p.ShouldSerializeValue (test), "#1");
622 test.SerializeProp = true;
623 Assert.IsFalse (p.ShouldSerializeValue (test), "#2");
624 test.Prop = "whatever";
625 Assert.IsTrue (p.ShouldSerializeValue (test), "#3");
626 test.SerializeProp = false;
627 Assert.IsTrue (p.ShouldSerializeValue (test), "#4");
631 public void ShouldSerializeTest_ReadOnly ()
633 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (
634 typeof (ShouldSerialize_ReadOnly));
635 ShouldSerialize_ReadOnly test = new ShouldSerialize_ReadOnly ();
637 PropertyDescriptor prop1PD = properties ["Prop1"];
638 PropertyDescriptor prop2PD = properties ["Prop2"];
639 PropertyDescriptor prop3PD = properties ["Prop3"];
640 PropertyDescriptor prop4PD = properties ["Prop4"];
641 PropertyDescriptor prop5PD = properties ["Prop5"];
642 PropertyDescriptor prop6PD = properties ["Prop6"];
643 PropertyDescriptor prop7PD = properties ["Prop7"];
644 PropertyDescriptor prop8PD = properties ["Prop8"];
645 PropertyDescriptor prop9PD = properties ["Prop9"];
647 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#A1");
648 Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#A2");
649 Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#A3");
650 Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#A4");
651 Assert.IsFalse (prop5PD.ShouldSerializeValue (test), "#A5");
652 Assert.IsFalse (prop6PD.ShouldSerializeValue (test), "#A6");
653 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#A7");
655 test.Prop1 = "whatever";
656 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#B1");
657 test.Prop2 = "whatever";
658 Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#B2");
659 test.Prop3 = "whatever";
660 Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#B3");
661 test.Prop4 = "whatever";
662 Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#B4");
663 test.Prop7 = "whatever";
664 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#B5");
667 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#C1");
668 test.SerializeProp3 = true;
669 Assert.IsTrue (prop3PD.ShouldSerializeValue (test), "#C2");
670 test.SerializeProp4 = true;
671 Assert.IsTrue (prop4PD.ShouldSerializeValue (test), "#C3");
672 test.SerializeProp5 = true;
673 Assert.IsTrue (prop5PD.ShouldSerializeValue (test), "#C4");
674 test.SerializeProp6 = true;
675 Assert.IsTrue (prop6PD.ShouldSerializeValue (test), "#C5");
677 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#C6");
678 test.SerializeProp7 = true;
679 Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C7");
681 Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C8");
683 // has both DesignerSerializationVisibility.Content and ShouldSerialize { return false }
684 Assert.IsFalse (prop8PD.ShouldSerializeValue (test), "#D1");
685 // has DesignerSerializationVisibility.Content, no ShouldSerialize
686 Assert.IsTrue (prop9PD.ShouldSerializeValue (test), "#D2");
690 public void CanResetTest_public ()
692 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
693 CanReset_public_test test = new CanReset_public_test ();
695 Assert.IsTrue (p.CanResetValue (test), "1");
696 Assert.AreEqual (5, test.Prop, "2");
698 Assert.AreEqual (10, test.Prop, "3");
702 public void CanResetTest_protected ()
704 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
705 CanReset_protected_test test = new CanReset_protected_test ();
707 Assert.IsTrue (p.CanResetValue (test), "1");
708 Assert.AreEqual (5, test.Prop, "2");
710 Assert.AreEqual (10, test.Prop, "3");
714 public void CanResetTest_private ()
716 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
717 CanReset_private_test test = new CanReset_private_test ();
719 Assert.IsTrue (p.CanResetValue (test), "1");
720 Assert.AreEqual (5, test.Prop, "2");
722 Assert.AreEqual (10, test.Prop, "3");
726 public void CanResetTestNoSetterTest ()
728 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
729 CanResetNoSetter_test test = new CanResetNoSetter_test ();
731 Assert.IsFalse (p.CanResetValue (test), "1");
732 Assert.AreEqual (5, test.Prop, "2");
734 Assert.AreEqual (10, test.Prop, "3");
738 public void NoSerializeOrResetPropTest ()
740 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
741 NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
743 Assert.IsFalse (p.CanResetValue (test), "1");
744 Assert.IsFalse (p.ShouldSerializeValue (test), "2");
748 public void ShouldSerializeFalseEffectOnCanResetTest ()
750 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
751 ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
753 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
754 Assert.IsFalse (p.CanResetValue (test), "2");
758 public void ReadOnlyPropertyTest ()
760 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
761 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
765 public void ReadOnlyAttributeTest ()
767 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
768 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
772 public void ReadOnlyConflictingTest ()
774 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
775 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
779 public void DisplayNameTest ()
781 PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Explicit"];
782 PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Implicit"];
784 Assert.AreEqual ("An explicit displayname", p1.DisplayName, "#1");
785 Assert.AreEqual ("Implicit", p2.DisplayName, "#2");
789 public void AddValueChanged ()
791 MockPropertyDescriptor pd = new MockPropertyDescriptor (
792 "Name", new Attribute [0]);
793 object compA = new object ();
794 object compB = new object ();
795 EventHandler handlerA = new EventHandler (ValueChanged1);
796 EventHandler handlerB = new EventHandler (ValueChanged1);
797 EventHandler handlerC = new EventHandler (ValueChanged2);
799 pd.AddValueChanged (compA, handlerA);
800 pd.AddValueChanged (compA, handlerC);
801 pd.AddValueChanged (compA, handlerC);
802 pd.AddValueChanged (compA, handlerB);
804 pd.FireValueChanged (compA, new EventArgs ());
805 Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
806 Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#A1");
807 Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#A2");
808 Assert.AreEqual ("ValueChanged2", _invokedHandlers [2], "#A3");
809 Assert.AreEqual ("ValueChanged1", _invokedHandlers [3], "#A4");
813 pd.FireValueChanged (compB, new EventArgs ());
814 Assert.AreEqual (0, _invokedHandlers.Count, "#B");
818 public void AddValueChanged_Component_Null ()
820 MockPropertyDescriptor pd = new MockPropertyDescriptor (
821 "Name", new Attribute [0]);
823 pd.AddValueChanged (null, new EventHandler (ValueChanged1));
825 } catch (ArgumentNullException ex) {
826 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
827 Assert.IsNull (ex.InnerException, "#3");
828 Assert.IsNotNull (ex.Message, "#4");
829 Assert.IsNotNull (ex.ParamName, "#5");
830 Assert.AreEqual ("component", ex.ParamName, "#6");
835 public void AddValueChanged_Handler_Null ()
837 MockPropertyDescriptor pd = new MockPropertyDescriptor (
838 "Name", new Attribute [0]);
840 pd.AddValueChanged (new object (), (EventHandler) null);
842 } catch (ArgumentNullException ex) {
843 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
844 Assert.IsNull (ex.InnerException, "#3");
845 Assert.IsNotNull (ex.Message, "#4");
846 Assert.IsNotNull (ex.ParamName, "#5");
847 Assert.AreEqual ("handler", ex.ParamName, "#6");
852 public void GetInvocationTarget_Instance_Null ()
854 MockPropertyDescriptor pd = new MockPropertyDescriptor (
855 "Name", new Attribute [0]);
857 pd.GetInvocationTarget (typeof (int), null);
859 } catch (ArgumentNullException ex) {
860 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
861 Assert.IsNull (ex.InnerException, "#3");
862 Assert.IsNotNull (ex.Message, "#4");
863 Assert.IsNotNull (ex.ParamName, "#5");
864 Assert.AreEqual ("instance", ex.ParamName, "#6");
869 public void GetInvocationTarget_Type_Null ()
871 MockPropertyDescriptor pd = new MockPropertyDescriptor (
872 "Name", new Attribute [0]);
874 pd.GetInvocationTarget ((Type) null, new object ());
876 } catch (ArgumentNullException ex) {
877 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
878 Assert.IsNull (ex.InnerException, "#3");
879 Assert.IsNotNull (ex.Message, "#4");
880 Assert.IsNotNull (ex.ParamName, "#5");
881 Assert.AreEqual ("type", ex.ParamName, "#6");
886 public void GetValueChangedHandler ()
888 object compA = new object ();
889 object compB = new object ();
890 EventHandler handlerA = new EventHandler (ValueChanged1);
891 EventHandler handlerB = new EventHandler (ValueChanged1);
892 EventHandler handlerC = new EventHandler (ValueChanged2);
894 MockPropertyDescriptor pd = new MockPropertyDescriptor (
895 "Name", new Attribute [0]);
896 Assert.IsNull (pd.GetValueChangedHandler (null), "#A1");
897 Assert.IsNull (pd.GetValueChangedHandler (compA), "#A2");
898 Assert.IsNull (pd.GetValueChangedHandler (compB), "#A3");
900 pd.AddValueChanged (compA, handlerA);
902 Assert.IsNull (pd.GetValueChangedHandler (null), "#B1");
903 Assert.AreSame (handlerA, pd.GetValueChangedHandler (compA), "#B2");
904 Assert.IsNull (pd.GetValueChangedHandler (compB), "#B3");
906 pd.AddValueChanged (compA, handlerB);
908 Assert.IsNull (pd.GetValueChangedHandler (null), "#C1");
909 EventHandler handler = pd.GetValueChangedHandler (compA);
910 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#C2");
911 Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#C3");
912 Assert.AreEqual (handlerB, handler.GetInvocationList () [1], "#C4");
913 Assert.IsNull (pd.GetValueChangedHandler (compB), "#C5");
915 pd.AddValueChanged (compB, handlerA);
917 Assert.IsNull (pd.GetValueChangedHandler (null), "#D1");
918 handler = pd.GetValueChangedHandler (compA);
919 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#D2");
920 Assert.AreSame (handlerA, pd.GetValueChangedHandler (compB), "#D3");
922 pd.RemoveValueChanged (compB, handlerB);
924 Assert.IsNull (pd.GetValueChangedHandler (null), "#E1");
925 handler = pd.GetValueChangedHandler (compA);
926 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#E2");
927 Assert.IsNull (pd.GetValueChangedHandler (compB), "#E3");
929 pd.RemoveValueChanged (compB, handlerB);
931 Assert.IsNull (pd.GetValueChangedHandler (null), "#F1");
932 handler = pd.GetValueChangedHandler (compA);
933 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#F2");
934 Assert.IsNull (pd.GetValueChangedHandler (compB), "#F3");
936 pd.RemoveValueChanged (compA, handlerC);
938 Assert.IsNull (pd.GetValueChangedHandler (null), "#G1");
939 handler = pd.GetValueChangedHandler (compA);
940 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#G2");
941 Assert.IsNull (pd.GetValueChangedHandler (compB), "#G3");
943 pd.AddValueChanged (compA, handlerC);
945 Assert.IsNull (pd.GetValueChangedHandler (null), "#H1");
946 handler = pd.GetValueChangedHandler (compA);
947 Assert.AreEqual (3, handler.GetInvocationList ().Length, "#H2");
948 Assert.IsNull (pd.GetValueChangedHandler (compB), "#H3");
950 pd.RemoveValueChanged (compA, handlerB);
952 Assert.IsNull (pd.GetValueChangedHandler (null), "#I1");
953 handler = pd.GetValueChangedHandler (compA);
954 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#I2");
955 Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#I3");
956 Assert.AreEqual (handlerC, handler.GetInvocationList () [1], "#I4");
957 Assert.IsNull (pd.GetValueChangedHandler (compB), "#I5");
961 public void RemoveValueChanged ()
963 MockPropertyDescriptor pd = new MockPropertyDescriptor (
964 "Name", new Attribute [0]);
965 object compA = new object ();
966 object compB = new object ();
967 EventHandler handlerA = new EventHandler (ValueChanged1);
968 EventHandler handlerB = new EventHandler (ValueChanged1);
969 EventHandler handlerC = new EventHandler (ValueChanged2);
971 pd.AddValueChanged (compA, handlerA);
972 pd.AddValueChanged (compA, handlerC);
973 pd.AddValueChanged (compA, handlerC);
974 pd.AddValueChanged (compA, handlerB);
975 pd.AddValueChanged (compB, handlerC);
977 pd.FireValueChanged (compA, new EventArgs ());
978 Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
979 pd.RemoveValueChanged (new object (), handlerC);
980 pd.FireValueChanged (compA, new EventArgs ());
981 Assert.AreEqual (8, _invokedHandlers.Count, "#A2");
984 pd.RemoveValueChanged (compA, handlerC);
986 pd.FireValueChanged (compA, new EventArgs ());
987 Assert.AreEqual (3, _invokedHandlers.Count, "#B1");
988 Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#B2");
989 Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#B3");
990 Assert.AreEqual ("ValueChanged1", _invokedHandlers [2], "#B4");
994 pd.FireValueChanged (compB, new EventArgs ());
995 Assert.AreEqual (1, _invokedHandlers.Count, "#C1");
996 Assert.AreEqual ("ValueChanged2", _invokedHandlers [0], "#C2");
999 pd.RemoveValueChanged (compB, handlerC);
1001 pd.FireValueChanged (compB, new EventArgs ());
1002 Assert.AreEqual (0, _invokedHandlers.Count, "#D");
1006 public void RemoveValueChanged_Component_Null ()
1008 MockPropertyDescriptor pd = new MockPropertyDescriptor (
1009 "Name", new Attribute [0]);
1011 pd.RemoveValueChanged (null, new EventHandler (ValueChanged1));
1013 } catch (ArgumentNullException ex) {
1014 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1015 Assert.IsNull (ex.InnerException, "#3");
1016 Assert.IsNotNull (ex.Message, "#4");
1017 Assert.IsNotNull (ex.ParamName, "#5");
1018 Assert.AreEqual ("component", ex.ParamName, "#6");
1023 public void RemoveValueChanged_Handler_Null ()
1025 MockPropertyDescriptor pd = new MockPropertyDescriptor (
1026 "Name", new Attribute [0]);
1028 pd.RemoveValueChanged (new object (), (EventHandler) null);
1030 } catch (ArgumentNullException ex) {
1031 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1032 Assert.IsNull (ex.InnerException, "#3");
1033 Assert.IsNotNull (ex.Message, "#4");
1034 Assert.IsNotNull (ex.ParamName, "#5");
1035 Assert.AreEqual ("handler", ex.ParamName, "#6");
1039 void ValueChanged1 (object sender, EventArgs e)
1041 _invokedHandlers.Add ("ValueChanged1");
1044 void ValueChanged2 (object sender, EventArgs e)
1046 _invokedHandlers.Add ("ValueChanged2");
1049 static Attribute FindAttribute (PropertyDescriptor pd, Type type)
1051 foreach (Attribute attr in pd.Attributes)
1052 if (attr.GetType () == type)
1056 #if !MOBILE && !MONOMAC
1057 class GetEditor_test
1059 [Editor (typeof (UIEditor), typeof (UITypeEditor))]
1060 public string Property {
1061 get { return "abc"; }
1066 class UIEditor : UITypeEditor
1071 public void GetEditorTest ()
1073 PropertyDescriptorCollection col;
1074 PropertyDescriptor pd;
1077 col = TypeDescriptor.GetProperties (typeof (GetEditor_test));
1079 ed = pd.GetEditor (typeof (UITypeEditor)) as UITypeEditor;
1081 Assert.IsNotNull (ed, "#01");
1082 Assert.AreEqual (ed.GetType ().Name, "UIEditor", "#02");
1086 class MockPropertyDescriptor : PropertyDescriptor
1088 public MockPropertyDescriptor (MemberDescriptor reference)
1093 public MockPropertyDescriptor (MemberDescriptor reference, Attribute [] attrs)
1094 : base (reference, attrs)
1098 public MockPropertyDescriptor (string name, Attribute [] attrs)
1099 : base (name, attrs)
1103 public override Type ComponentType {
1104 get { return typeof (int); }
1107 public override bool IsReadOnly {
1108 get { return false; }
1111 public override Type PropertyType{
1112 get { return typeof (DateTime); }
1115 public override object GetValue (object component)
1120 public override void SetValue (object component, object value)
1124 public override void ResetValue (object component)
1128 public override bool CanResetValue (object component)
1133 public override bool ShouldSerializeValue (object component)
1138 public void FireValueChanged (object component, EventArgs e)
1140 base.OnValueChanged (component, e);
1143 public new object GetInvocationTarget (Type type, object instance)
1145 return base.GetInvocationTarget (type, instance);
1148 public new EventHandler GetValueChangedHandler (object component)
1150 return base.GetValueChangedHandler (component);
1154 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
1155 public class PropTestAttribute : Attribute
1157 public PropTestAttribute ()
1162 public class TestBase
1165 public int PropBase1
1172 [Description ("whatever")]
1173 public string PropBase2
1180 public virtual string PropBase3
1187 public class TestSub : TestBase
1197 public string PropSub2
1203 public override string PropBase3