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;
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 [TypeConverter("System.ComponentModel.CharConverter, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
408 public virtual Version WithConverterNamedAssmQuald {
412 public int WithDefaultConverter {
417 class ConverterSubclassNotOverridenProperties_test : Converter_test
421 class ConverterSubclassOverridenProperties_test : Converter_test
423 public override Version WithConverter {
427 public override Version WithConverterNamed {
432 class ConverterEmptyConvertersOnOveriddenProperties : Converter_test
435 public override Version WithConverter {
440 public override Version WithConverterNamed {
445 private ArrayList _invokedHandlers;
450 _invokedHandlers = new ArrayList ();
455 _invokedHandlers.Clear ();
459 public void Attributes ()
461 PropertyDescriptorCollection properties;
462 PropertyDescriptor pd;
464 properties = TypeDescriptor.GetProperties (typeof (TestBase));
466 pd = properties ["PropBase3"];
467 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#A1");
468 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#A2");
470 pd = properties ["PropBase2"];
471 Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#B1");
472 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#B2");
474 pd = properties ["PropBase1"];
475 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#C1");
476 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#C2");
478 properties = TypeDescriptor.GetProperties (typeof (TestSub));
480 pd = properties ["PropBase3"];
481 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#D1");
482 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#D2");
484 pd = properties ["PropBase2"];
485 Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#E1");
486 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#E2");
488 pd = properties ["PropBase1"];
489 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#F1");
490 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#F2");
494 public void VirtualPropertyDontOverrideSetter ()
496 VirtPropChildNoSetter c = new VirtPropChildNoSetter ();
497 PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties (c);
498 foreach (PropertyDescriptor pd in pdc) {
499 if (pd.Name != "SomeProperty")
501 pd.SetValue (c, "testing2");
507 public void VirtualPropertyDontOverrideGetter ()
509 VirtPropChildNoGetter c = new VirtPropChildNoGetter ();
510 PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties (c);
511 foreach (PropertyDescriptor pd in pdc) {
512 if (pd.Name != "SomeProperty")
514 pd.SetValue (c, "testing2");
520 public void MissingTypeConverter ()
522 PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["Prop"];
523 PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["IntProp"];
524 PropertyDescriptor p3 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["StringProp"];
526 Assert.AreEqual (typeof (TypeConverter), p1.Converter.GetType (), "1");
527 Assert.AreEqual (typeof (Int32Converter), p2.Converter.GetType (), "2");
528 Assert.AreEqual (typeof (StringConverter), p3.Converter.GetType (), "3");
532 public void ConverterTest ()
534 Assert.AreEqual (typeof (TypeConverter),
535 TypeDescriptor.GetProperties (typeof (Converter_test))["NoConverter"].Converter.GetType (), "#1");
536 Assert.AreEqual (typeof (MyVersionTypeConverter),
537 TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverter"].Converter.GetType (), "#2");
538 Assert.AreEqual (typeof (MyVersionTypeConverter),
539 TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverterNamed"].Converter.GetType (), "#3");
540 Assert.AreEqual (typeof (CharConverter),
541 TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#4");
542 Assert.AreEqual (typeof (Int32Converter),
543 TypeDescriptor.GetProperties (typeof (Converter_test))["WithDefaultConverter"].Converter.GetType (), "#5");
545 Assert.AreEqual (typeof (TypeConverter),
546 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["NoConverter"].Converter.GetType (), "#6");
547 Assert.AreEqual (typeof (MyVersionTypeConverter),
548 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverter"].Converter.GetType (), "#7");
549 Assert.AreEqual (typeof (MyVersionTypeConverter),
550 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverterNamed"].Converter.GetType (), "#8");
551 Assert.AreEqual (typeof (CharConverter),
552 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#9");
553 Assert.AreEqual (typeof (Int32Converter),
554 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithDefaultConverter"].Converter.GetType (), "#10");
556 Assert.AreEqual (typeof (TypeConverter),
557 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["NoConverter"].Converter.GetType (), "#11");
558 Assert.AreEqual (typeof (MyVersionTypeConverter),
559 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverter"].Converter.GetType (), "#12");
560 Assert.AreEqual (typeof (MyVersionTypeConverter),
561 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverterNamed"].Converter.GetType (), "#13");
562 Assert.AreEqual (typeof (CharConverter),
563 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#14");
564 Assert.AreEqual (typeof (Int32Converter),
565 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithDefaultConverter"].Converter.GetType (), "#15");
567 Assert.AreEqual (typeof (TypeConverter),
568 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["NoConverter"].Converter.GetType (), "#116");
569 Assert.AreEqual (typeof (TypeConverter),
570 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverter"].Converter.GetType (), "#17");
571 Assert.AreEqual (typeof (TypeConverter),
572 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverterNamed"].Converter.GetType (), "#18");
573 Assert.AreEqual (typeof (CharConverter),
574 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverterNamedAssmQuald"].Converter.GetType (), "#19");
575 Assert.AreEqual (typeof (Int32Converter),
576 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithDefaultConverter"].Converter.GetType (), "#20");
580 public void ShouldSerializeTest_public ()
582 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
583 ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
585 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
589 public void ShouldSerializeTest_protected ()
591 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
592 ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
594 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
598 public void ShouldSerializeTest_private ()
600 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
601 ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
603 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
607 public void ShouldSerializeTest_No_Default ()
609 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_No_Default)) ["Prop"];
610 ShouldSerialize_No_Default test = new ShouldSerialize_No_Default ();
612 Assert.IsTrue (p.ShouldSerializeValue (test), "#1");
613 test.Prop = "whatever";
614 Assert.IsTrue (p.ShouldSerializeValue (test), "#2");
618 public void ShouldSerializeTest_Null_Default ()
620 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_Null_Default)) ["Prop"];
621 ShouldSerialize_Null_Default test = new ShouldSerialize_Null_Default ();
623 Assert.IsFalse (p.ShouldSerializeValue (test), "#1");
624 test.SerializeProp = true;
625 Assert.IsFalse (p.ShouldSerializeValue (test), "#2");
626 test.Prop = "whatever";
627 Assert.IsTrue (p.ShouldSerializeValue (test), "#3");
628 test.SerializeProp = false;
629 Assert.IsTrue (p.ShouldSerializeValue (test), "#4");
633 public void ShouldSerializeTest_ReadOnly ()
635 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (
636 typeof (ShouldSerialize_ReadOnly));
637 ShouldSerialize_ReadOnly test = new ShouldSerialize_ReadOnly ();
639 PropertyDescriptor prop1PD = properties ["Prop1"];
640 PropertyDescriptor prop2PD = properties ["Prop2"];
641 PropertyDescriptor prop3PD = properties ["Prop3"];
642 PropertyDescriptor prop4PD = properties ["Prop4"];
643 PropertyDescriptor prop5PD = properties ["Prop5"];
644 PropertyDescriptor prop6PD = properties ["Prop6"];
645 PropertyDescriptor prop7PD = properties ["Prop7"];
646 PropertyDescriptor prop8PD = properties ["Prop8"];
647 PropertyDescriptor prop9PD = properties ["Prop9"];
649 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#A1");
650 Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#A2");
651 Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#A3");
652 Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#A4");
653 Assert.IsFalse (prop5PD.ShouldSerializeValue (test), "#A5");
654 Assert.IsFalse (prop6PD.ShouldSerializeValue (test), "#A6");
655 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#A7");
657 test.Prop1 = "whatever";
658 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#B1");
659 test.Prop2 = "whatever";
660 Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#B2");
661 test.Prop3 = "whatever";
662 Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#B3");
663 test.Prop4 = "whatever";
664 Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#B4");
665 test.Prop7 = "whatever";
666 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#B5");
669 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#C1");
670 test.SerializeProp3 = true;
671 Assert.IsTrue (prop3PD.ShouldSerializeValue (test), "#C2");
672 test.SerializeProp4 = true;
673 Assert.IsTrue (prop4PD.ShouldSerializeValue (test), "#C3");
674 test.SerializeProp5 = true;
675 Assert.IsTrue (prop5PD.ShouldSerializeValue (test), "#C4");
676 test.SerializeProp6 = true;
677 Assert.IsTrue (prop6PD.ShouldSerializeValue (test), "#C5");
679 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#C6");
680 test.SerializeProp7 = true;
681 Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C7");
683 Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C8");
685 // has both DesignerSerializationVisibility.Content and ShouldSerialize { return false }
686 Assert.IsFalse (prop8PD.ShouldSerializeValue (test), "#D1");
687 // has DesignerSerializationVisibility.Content, no ShouldSerialize
688 Assert.IsTrue (prop9PD.ShouldSerializeValue (test), "#D2");
692 public void CanResetTest_public ()
694 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
695 CanReset_public_test test = new CanReset_public_test ();
697 Assert.IsTrue (p.CanResetValue (test), "1");
698 Assert.AreEqual (5, test.Prop, "2");
700 Assert.AreEqual (10, test.Prop, "3");
704 public void CanResetTest_protected ()
706 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
707 CanReset_protected_test test = new CanReset_protected_test ();
709 Assert.IsTrue (p.CanResetValue (test), "1");
710 Assert.AreEqual (5, test.Prop, "2");
712 Assert.AreEqual (10, test.Prop, "3");
716 public void CanResetTest_private ()
718 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
719 CanReset_private_test test = new CanReset_private_test ();
721 Assert.IsTrue (p.CanResetValue (test), "1");
722 Assert.AreEqual (5, test.Prop, "2");
724 Assert.AreEqual (10, test.Prop, "3");
728 public void CanResetTestNoSetterTest ()
730 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
731 CanResetNoSetter_test test = new CanResetNoSetter_test ();
733 Assert.IsFalse (p.CanResetValue (test), "1");
734 Assert.AreEqual (5, test.Prop, "2");
736 Assert.AreEqual (10, test.Prop, "3");
740 public void NoSerializeOrResetPropTest ()
742 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
743 NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
745 Assert.IsFalse (p.CanResetValue (test), "1");
746 Assert.IsFalse (p.ShouldSerializeValue (test), "2");
750 public void ShouldSerializeFalseEffectOnCanResetTest ()
752 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
753 ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
755 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
756 Assert.IsFalse (p.CanResetValue (test), "2");
760 public void ReadOnlyPropertyTest ()
762 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
763 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
767 public void ReadOnlyAttributeTest ()
769 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
770 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
774 public void ReadOnlyConflictingTest ()
776 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
777 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
781 public void DisplayNameTest ()
783 PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Explicit"];
784 PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Implicit"];
786 Assert.AreEqual ("An explicit displayname", p1.DisplayName, "#1");
787 Assert.AreEqual ("Implicit", p2.DisplayName, "#2");
791 public void AddValueChanged ()
793 MockPropertyDescriptor pd = new MockPropertyDescriptor (
794 "Name", new Attribute [0]);
795 object compA = new object ();
796 object compB = new object ();
797 EventHandler handlerA = new EventHandler (ValueChanged1);
798 EventHandler handlerB = new EventHandler (ValueChanged1);
799 EventHandler handlerC = new EventHandler (ValueChanged2);
801 pd.AddValueChanged (compA, handlerA);
802 pd.AddValueChanged (compA, handlerC);
803 pd.AddValueChanged (compA, handlerC);
804 pd.AddValueChanged (compA, handlerB);
806 pd.FireValueChanged (compA, new EventArgs ());
807 Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
808 Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#A1");
809 Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#A2");
810 Assert.AreEqual ("ValueChanged2", _invokedHandlers [2], "#A3");
811 Assert.AreEqual ("ValueChanged1", _invokedHandlers [3], "#A4");
815 pd.FireValueChanged (compB, new EventArgs ());
816 Assert.AreEqual (0, _invokedHandlers.Count, "#B");
820 public void AddValueChanged_Component_Null ()
822 MockPropertyDescriptor pd = new MockPropertyDescriptor (
823 "Name", new Attribute [0]);
825 pd.AddValueChanged (null, new EventHandler (ValueChanged1));
827 } catch (ArgumentNullException ex) {
828 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
829 Assert.IsNull (ex.InnerException, "#3");
830 Assert.IsNotNull (ex.Message, "#4");
831 Assert.IsNotNull (ex.ParamName, "#5");
832 Assert.AreEqual ("component", ex.ParamName, "#6");
837 public void AddValueChanged_Handler_Null ()
839 MockPropertyDescriptor pd = new MockPropertyDescriptor (
840 "Name", new Attribute [0]);
842 pd.AddValueChanged (new object (), (EventHandler) null);
844 } catch (ArgumentNullException ex) {
845 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
846 Assert.IsNull (ex.InnerException, "#3");
847 Assert.IsNotNull (ex.Message, "#4");
848 Assert.IsNotNull (ex.ParamName, "#5");
849 Assert.AreEqual ("handler", ex.ParamName, "#6");
854 public void GetInvocationTarget_Instance_Null ()
856 MockPropertyDescriptor pd = new MockPropertyDescriptor (
857 "Name", new Attribute [0]);
859 pd.GetInvocationTarget (typeof (int), null);
861 } catch (ArgumentNullException ex) {
862 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
863 Assert.IsNull (ex.InnerException, "#3");
864 Assert.IsNotNull (ex.Message, "#4");
865 Assert.IsNotNull (ex.ParamName, "#5");
866 Assert.AreEqual ("instance", ex.ParamName, "#6");
871 public void GetInvocationTarget_Type_Null ()
873 MockPropertyDescriptor pd = new MockPropertyDescriptor (
874 "Name", new Attribute [0]);
876 pd.GetInvocationTarget ((Type) null, new object ());
878 } catch (ArgumentNullException ex) {
879 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
880 Assert.IsNull (ex.InnerException, "#3");
881 Assert.IsNotNull (ex.Message, "#4");
882 Assert.IsNotNull (ex.ParamName, "#5");
883 Assert.AreEqual ("type", ex.ParamName, "#6");
888 public void GetValueChangedHandler ()
890 object compA = new object ();
891 object compB = new object ();
892 EventHandler handlerA = new EventHandler (ValueChanged1);
893 EventHandler handlerB = new EventHandler (ValueChanged1);
894 EventHandler handlerC = new EventHandler (ValueChanged2);
896 MockPropertyDescriptor pd = new MockPropertyDescriptor (
897 "Name", new Attribute [0]);
898 Assert.IsNull (pd.GetValueChangedHandler (null), "#A1");
899 Assert.IsNull (pd.GetValueChangedHandler (compA), "#A2");
900 Assert.IsNull (pd.GetValueChangedHandler (compB), "#A3");
902 pd.AddValueChanged (compA, handlerA);
904 Assert.IsNull (pd.GetValueChangedHandler (null), "#B1");
905 Assert.AreSame (handlerA, pd.GetValueChangedHandler (compA), "#B2");
906 Assert.IsNull (pd.GetValueChangedHandler (compB), "#B3");
908 pd.AddValueChanged (compA, handlerB);
910 Assert.IsNull (pd.GetValueChangedHandler (null), "#C1");
911 EventHandler handler = pd.GetValueChangedHandler (compA);
912 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#C2");
913 Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#C3");
914 Assert.AreEqual (handlerB, handler.GetInvocationList () [1], "#C4");
915 Assert.IsNull (pd.GetValueChangedHandler (compB), "#C5");
917 pd.AddValueChanged (compB, handlerA);
919 Assert.IsNull (pd.GetValueChangedHandler (null), "#D1");
920 handler = pd.GetValueChangedHandler (compA);
921 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#D2");
922 Assert.AreSame (handlerA, pd.GetValueChangedHandler (compB), "#D3");
924 pd.RemoveValueChanged (compB, handlerB);
926 Assert.IsNull (pd.GetValueChangedHandler (null), "#E1");
927 handler = pd.GetValueChangedHandler (compA);
928 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#E2");
929 Assert.IsNull (pd.GetValueChangedHandler (compB), "#E3");
931 pd.RemoveValueChanged (compB, handlerB);
933 Assert.IsNull (pd.GetValueChangedHandler (null), "#F1");
934 handler = pd.GetValueChangedHandler (compA);
935 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#F2");
936 Assert.IsNull (pd.GetValueChangedHandler (compB), "#F3");
938 pd.RemoveValueChanged (compA, handlerC);
940 Assert.IsNull (pd.GetValueChangedHandler (null), "#G1");
941 handler = pd.GetValueChangedHandler (compA);
942 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#G2");
943 Assert.IsNull (pd.GetValueChangedHandler (compB), "#G3");
945 pd.AddValueChanged (compA, handlerC);
947 Assert.IsNull (pd.GetValueChangedHandler (null), "#H1");
948 handler = pd.GetValueChangedHandler (compA);
949 Assert.AreEqual (3, handler.GetInvocationList ().Length, "#H2");
950 Assert.IsNull (pd.GetValueChangedHandler (compB), "#H3");
952 pd.RemoveValueChanged (compA, handlerB);
954 Assert.IsNull (pd.GetValueChangedHandler (null), "#I1");
955 handler = pd.GetValueChangedHandler (compA);
956 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#I2");
957 Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#I3");
958 Assert.AreEqual (handlerC, handler.GetInvocationList () [1], "#I4");
959 Assert.IsNull (pd.GetValueChangedHandler (compB), "#I5");
963 public void RemoveValueChanged ()
965 MockPropertyDescriptor pd = new MockPropertyDescriptor (
966 "Name", new Attribute [0]);
967 object compA = new object ();
968 object compB = new object ();
969 EventHandler handlerA = new EventHandler (ValueChanged1);
970 EventHandler handlerB = new EventHandler (ValueChanged1);
971 EventHandler handlerC = new EventHandler (ValueChanged2);
973 pd.AddValueChanged (compA, handlerA);
974 pd.AddValueChanged (compA, handlerC);
975 pd.AddValueChanged (compA, handlerC);
976 pd.AddValueChanged (compA, handlerB);
977 pd.AddValueChanged (compB, handlerC);
979 pd.FireValueChanged (compA, new EventArgs ());
980 Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
981 pd.RemoveValueChanged (new object (), handlerC);
982 pd.FireValueChanged (compA, new EventArgs ());
983 Assert.AreEqual (8, _invokedHandlers.Count, "#A2");
986 pd.RemoveValueChanged (compA, handlerC);
988 pd.FireValueChanged (compA, new EventArgs ());
989 Assert.AreEqual (3, _invokedHandlers.Count, "#B1");
990 Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#B2");
991 Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#B3");
992 Assert.AreEqual ("ValueChanged1", _invokedHandlers [2], "#B4");
996 pd.FireValueChanged (compB, new EventArgs ());
997 Assert.AreEqual (1, _invokedHandlers.Count, "#C1");
998 Assert.AreEqual ("ValueChanged2", _invokedHandlers [0], "#C2");
1001 pd.RemoveValueChanged (compB, handlerC);
1003 pd.FireValueChanged (compB, new EventArgs ());
1004 Assert.AreEqual (0, _invokedHandlers.Count, "#D");
1008 public void RemoveValueChanged_Component_Null ()
1010 MockPropertyDescriptor pd = new MockPropertyDescriptor (
1011 "Name", new Attribute [0]);
1013 pd.RemoveValueChanged (null, new EventHandler (ValueChanged1));
1015 } catch (ArgumentNullException ex) {
1016 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1017 Assert.IsNull (ex.InnerException, "#3");
1018 Assert.IsNotNull (ex.Message, "#4");
1019 Assert.IsNotNull (ex.ParamName, "#5");
1020 Assert.AreEqual ("component", ex.ParamName, "#6");
1025 public void RemoveValueChanged_Handler_Null ()
1027 MockPropertyDescriptor pd = new MockPropertyDescriptor (
1028 "Name", new Attribute [0]);
1030 pd.RemoveValueChanged (new object (), (EventHandler) null);
1032 } catch (ArgumentNullException ex) {
1033 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1034 Assert.IsNull (ex.InnerException, "#3");
1035 Assert.IsNotNull (ex.Message, "#4");
1036 Assert.IsNotNull (ex.ParamName, "#5");
1037 Assert.AreEqual ("handler", ex.ParamName, "#6");
1041 void ValueChanged1 (object sender, EventArgs e)
1043 _invokedHandlers.Add ("ValueChanged1");
1046 void ValueChanged2 (object sender, EventArgs e)
1048 _invokedHandlers.Add ("ValueChanged2");
1051 static Attribute FindAttribute (PropertyDescriptor pd, Type type)
1053 foreach (Attribute attr in pd.Attributes)
1054 if (attr.GetType () == type)
1059 class GetEditor_test
1061 [Editor (typeof (UIEditor), typeof (UITypeEditor))]
1062 public string Property {
1063 get { return "abc"; }
1068 class UIEditor : UITypeEditor
1073 public void GetEditorTest ()
1075 PropertyDescriptorCollection col;
1076 PropertyDescriptor pd;
1079 col = TypeDescriptor.GetProperties (typeof (GetEditor_test));
1081 ed = pd.GetEditor (typeof (UITypeEditor)) as UITypeEditor;
1083 Assert.IsNotNull (ed, "#01");
1084 Assert.AreEqual (ed.GetType ().Name, "UIEditor", "#02");
1088 class MockPropertyDescriptor : PropertyDescriptor
1090 public MockPropertyDescriptor (MemberDescriptor reference)
1095 public MockPropertyDescriptor (MemberDescriptor reference, Attribute [] attrs)
1096 : base (reference, attrs)
1100 public MockPropertyDescriptor (string name, Attribute [] attrs)
1101 : base (name, attrs)
1105 public override Type ComponentType {
1106 get { return typeof (int); }
1109 public override bool IsReadOnly {
1110 get { return false; }
1113 public override Type PropertyType{
1114 get { return typeof (DateTime); }
1117 public override object GetValue (object component)
1122 public override void SetValue (object component, object value)
1126 public override void ResetValue (object component)
1130 public override bool CanResetValue (object component)
1135 public override bool ShouldSerializeValue (object component)
1140 public void FireValueChanged (object component, EventArgs e)
1142 base.OnValueChanged (component, e);
1145 public new object GetInvocationTarget (Type type, object instance)
1147 return base.GetInvocationTarget (type, instance);
1150 public new EventHandler GetValueChangedHandler (object component)
1152 return base.GetValueChangedHandler (component);
1156 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
1157 public class PropTestAttribute : Attribute
1159 public PropTestAttribute ()
1164 public class TestBase
1167 public int PropBase1
1174 [Description ("whatever")]
1175 public string PropBase2
1182 public virtual string PropBase3
1189 public class TestSub : TestBase
1199 public string PropSub2
1205 public override string PropBase3