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.Drawing.Design;
17 using System.Globalization;
18 using System.Reflection;
19 using System.Runtime.InteropServices;
21 using NUnit.Framework;
23 namespace MonoTests.System.ComponentModel
25 internal class MyVersionTypeConverter : TypeConverter
33 public virtual string SomeProperty {
34 get { return _someProperty; }
35 set { _someProperty = value; }
39 class VirtPropChildNoSetter : VirtPropParent
41 public override string SomeProperty {
42 get { return base.SomeProperty + ": modified"; }
46 class VirtPropChildNoGetter : VirtPropParent
48 public override string SomeProperty {
49 get { return base.SomeProperty + ": modified"; }
55 public class PropertyDescriptorTests
57 class MissingConverterType_test
59 public class NestedClass { }
61 [TypeConverter ("missing-type-name")]
62 public NestedClass Prop {
66 [TypeConverter ("missing-type-name")]
71 [TypeConverter ("missing-type-name")]
72 public string StringProp {
77 class ReadOnlyProperty_test
84 class ReadOnlyAttribute_test
93 class ConflictingReadOnly_test
101 class ShouldSerialize_public_test
107 public bool ShouldSerializeProp()
113 class ShouldSerialize_protected_test
119 protected bool ShouldSerializeProp()
125 class ShouldSerialize_private_test
131 private bool ShouldSerializeProp()
137 class ShouldSerializeFalseEffectOnCanReset_test
144 public bool ShouldSerializeProp()
149 public void ResetProp()
154 class ShouldSerialize_Null_Default
156 [DefaultValue (null)]
158 get { return _prop; }
159 set { _prop = value; }
162 public bool SerializeProp {
163 get { return _serializeProp; }
164 set { _serializeProp = value; }
167 public bool ShouldSerializeProp ()
169 return _serializeProp;
172 private string _prop;
173 private bool _serializeProp;
176 class ShouldSerialize_No_Default
179 get { return _prop; }
180 set { _prop = value; }
183 private string _prop;
186 class ShouldSerialize_ReadOnly
189 [DefaultValue ("ok")]
190 public string Prop1 {
191 get { return _prop1; }
192 set { _prop1 = value; }
196 public string Prop2 {
197 get { return _prop2; }
198 set { _prop2 = value; }
202 public string Prop3 {
203 get { return _prop3; }
204 set { _prop3 = value; }
208 public string Prop4 {
209 get { return _prop4; }
210 set { _prop4 = value; }
213 public string Prop5 {
214 get { return _prop5; }
217 [DefaultValue ("bad")]
218 public string Prop6 {
219 get { return _prop6; }
223 [DefaultValue ("good")]
224 public string Prop7 {
225 get { return _prop7; }
226 set { _prop7 = value; }
230 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
231 public string Prop8 {
236 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
237 public string Prop9 {
241 public bool SerializeProp3 {
242 get { return _serializeProp3; }
243 set { _serializeProp3 = value; }
246 public bool SerializeProp4 {
247 get { return _serializeProp4; }
248 set { _serializeProp4 = value; }
251 public bool SerializeProp5 {
252 get { return _serializeProp5; }
253 set { _serializeProp5 = value; }
256 public bool SerializeProp6 {
257 get { return _serializeProp6; }
258 set { _serializeProp6 = value; }
261 public bool SerializeProp7 {
262 get { return _serializeProp7; }
263 set { _serializeProp7 = value; }
266 public bool ShouldSerializeProp3 ()
268 return _serializeProp3;
271 public bool ShouldSerializeProp4 ()
273 return _serializeProp4;
276 public bool ShouldSerializeProp5 ()
278 return _serializeProp5;
281 public bool ShouldSerializeProp6 ()
283 return _serializeProp6;
286 public bool ShouldSerializeProp7 ()
288 return _serializeProp7;
291 public bool ShouldSerializeProp8 ()
296 private string _prop1;
297 private string _prop2;
298 private string _prop3;
299 private string _prop4;
300 private string _prop5 = "good";
301 private string _prop6 = "bad";
302 private string _prop7;
303 private bool _serializeProp3;
304 private bool _serializeProp4;
305 private bool _serializeProp5;
306 private bool _serializeProp6;
307 private bool _serializeProp7;
310 class NoSerializeOrResetProp_test
317 class CanReset_public_test
322 set { prop = value; }
325 public void ResetProp()
331 class CanReset_protected_test
336 set { prop = value; }
339 protected void ResetProp()
345 class CanReset_private_test
350 set { prop = value; }
353 private void ResetProp()
359 class CanResetNoSetter_test
366 private void ResetProp()
372 class DisplayName_test
375 [DisplayName ("An explicit displayname")]
377 public bool Explicit {
378 get { return false; }
381 public bool Implicit {
382 get { return false; }
388 public virtual Version NoConverter {
392 [TypeConverter (typeof(MyVersionTypeConverter))]
393 public virtual Version WithConverter {
397 [TypeConverter ("MonoTests.System.ComponentModel.MyVersionTypeConverter")]
398 public virtual Version WithConverterNamed {
402 [TypeConverter("System.ComponentModel.CharConverter, " + Consts.AssemblySystem)]
403 public virtual Version WithConverterNamedAssmQuald {
407 public int WithDefaultConverter {
412 class ConverterSubclassNotOverridenProperties_test : Converter_test
416 class ConverterSubclassOverridenProperties_test : Converter_test
418 public override Version WithConverter {
422 public override Version WithConverterNamed {
427 class ConverterEmptyConvertersOnOveriddenProperties : Converter_test
430 public override Version WithConverter {
435 public override Version WithConverterNamed {
440 private ArrayList _invokedHandlers;
445 _invokedHandlers = new ArrayList ();
450 _invokedHandlers.Clear ();
454 public void Attributes ()
456 PropertyDescriptorCollection properties;
457 PropertyDescriptor pd;
459 properties = TypeDescriptor.GetProperties (typeof (TestBase));
461 pd = properties ["PropBase3"];
462 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#A1");
463 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#A2");
465 pd = properties ["PropBase2"];
466 Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#B1");
467 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#B2");
469 pd = properties ["PropBase1"];
470 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#C1");
471 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#C2");
473 properties = TypeDescriptor.GetProperties (typeof (TestSub));
475 pd = properties ["PropBase3"];
476 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#D1");
477 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#D2");
479 pd = properties ["PropBase2"];
480 Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#E1");
481 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#E2");
483 pd = properties ["PropBase1"];
484 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#F1");
485 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#F2");
489 public void VirtualPropertyDontOverrideSetter ()
491 VirtPropChildNoSetter c = new VirtPropChildNoSetter ();
492 PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties (c);
493 foreach (PropertyDescriptor pd in pdc) {
494 if (pd.Name != "SomeProperty")
496 pd.SetValue (c, "testing2");
502 public void VirtualPropertyDontOverrideGetter ()
504 VirtPropChildNoGetter c = new VirtPropChildNoGetter ();
505 PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties (c);
506 foreach (PropertyDescriptor pd in pdc) {
507 if (pd.Name != "SomeProperty")
509 pd.SetValue (c, "testing2");
515 public void MissingTypeConverter ()
517 PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["Prop"];
518 PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["IntProp"];
519 PropertyDescriptor p3 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["StringProp"];
521 Assert.AreEqual (typeof (TypeConverter), p1.Converter.GetType (), "1");
522 Assert.AreEqual (typeof (Int32Converter), p2.Converter.GetType (), "2");
523 Assert.AreEqual (typeof (StringConverter), p3.Converter.GetType (), "3");
527 public void ConverterTest ()
529 Assert.AreEqual (typeof (TypeConverter),
530 TypeDescriptor.GetProperties (typeof (Converter_test))["NoConverter"].Converter.GetType (), "#1");
531 Assert.AreEqual (typeof (MyVersionTypeConverter),
532 TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverter"].Converter.GetType (), "#2");
533 Assert.AreEqual (typeof (MyVersionTypeConverter),
534 TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverterNamed"].Converter.GetType (), "#3");
535 Assert.AreEqual (typeof (CharConverter),
536 TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#4");
537 Assert.AreEqual (typeof (Int32Converter),
538 TypeDescriptor.GetProperties (typeof (Converter_test))["WithDefaultConverter"].Converter.GetType (), "#5");
540 Assert.AreEqual (typeof (TypeConverter),
541 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["NoConverter"].Converter.GetType (), "#6");
542 Assert.AreEqual (typeof (MyVersionTypeConverter),
543 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverter"].Converter.GetType (), "#7");
544 Assert.AreEqual (typeof (MyVersionTypeConverter),
545 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverterNamed"].Converter.GetType (), "#8");
546 Assert.AreEqual (typeof (CharConverter),
547 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#9");
548 Assert.AreEqual (typeof (Int32Converter),
549 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithDefaultConverter"].Converter.GetType (), "#10");
551 Assert.AreEqual (typeof (TypeConverter),
552 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["NoConverter"].Converter.GetType (), "#11");
553 Assert.AreEqual (typeof (MyVersionTypeConverter),
554 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverter"].Converter.GetType (), "#12");
555 Assert.AreEqual (typeof (MyVersionTypeConverter),
556 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverterNamed"].Converter.GetType (), "#13");
557 Assert.AreEqual (typeof (CharConverter),
558 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#14");
559 Assert.AreEqual (typeof (Int32Converter),
560 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithDefaultConverter"].Converter.GetType (), "#15");
562 Assert.AreEqual (typeof (TypeConverter),
563 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["NoConverter"].Converter.GetType (), "#116");
564 Assert.AreEqual (typeof (TypeConverter),
565 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverter"].Converter.GetType (), "#17");
566 Assert.AreEqual (typeof (TypeConverter),
567 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverterNamed"].Converter.GetType (), "#18");
568 Assert.AreEqual (typeof (CharConverter),
569 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverterNamedAssmQuald"].Converter.GetType (), "#19");
570 Assert.AreEqual (typeof (Int32Converter),
571 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithDefaultConverter"].Converter.GetType (), "#20");
575 public void ShouldSerializeTest_public ()
577 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
578 ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
580 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
584 public void ShouldSerializeTest_protected ()
586 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
587 ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
589 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
593 public void ShouldSerializeTest_private ()
595 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
596 ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
598 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
602 public void ShouldSerializeTest_No_Default ()
604 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_No_Default)) ["Prop"];
605 ShouldSerialize_No_Default test = new ShouldSerialize_No_Default ();
607 Assert.IsTrue (p.ShouldSerializeValue (test), "#1");
608 test.Prop = "whatever";
609 Assert.IsTrue (p.ShouldSerializeValue (test), "#2");
613 public void ShouldSerializeTest_Null_Default ()
615 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_Null_Default)) ["Prop"];
616 ShouldSerialize_Null_Default test = new ShouldSerialize_Null_Default ();
618 Assert.IsFalse (p.ShouldSerializeValue (test), "#1");
619 test.SerializeProp = true;
620 Assert.IsFalse (p.ShouldSerializeValue (test), "#2");
621 test.Prop = "whatever";
622 Assert.IsTrue (p.ShouldSerializeValue (test), "#3");
623 test.SerializeProp = false;
624 Assert.IsTrue (p.ShouldSerializeValue (test), "#4");
628 public void ShouldSerializeTest_ReadOnly ()
630 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (
631 typeof (ShouldSerialize_ReadOnly));
632 ShouldSerialize_ReadOnly test = new ShouldSerialize_ReadOnly ();
634 PropertyDescriptor prop1PD = properties ["Prop1"];
635 PropertyDescriptor prop2PD = properties ["Prop2"];
636 PropertyDescriptor prop3PD = properties ["Prop3"];
637 PropertyDescriptor prop4PD = properties ["Prop4"];
638 PropertyDescriptor prop5PD = properties ["Prop5"];
639 PropertyDescriptor prop6PD = properties ["Prop6"];
640 PropertyDescriptor prop7PD = properties ["Prop7"];
641 PropertyDescriptor prop8PD = properties ["Prop8"];
642 PropertyDescriptor prop9PD = properties ["Prop9"];
644 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#A1");
645 Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#A2");
646 Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#A3");
647 Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#A4");
648 Assert.IsFalse (prop5PD.ShouldSerializeValue (test), "#A5");
649 Assert.IsFalse (prop6PD.ShouldSerializeValue (test), "#A6");
650 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#A7");
652 test.Prop1 = "whatever";
653 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#B1");
654 test.Prop2 = "whatever";
655 Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#B2");
656 test.Prop3 = "whatever";
657 Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#B3");
658 test.Prop4 = "whatever";
659 Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#B4");
660 test.Prop7 = "whatever";
661 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#B5");
664 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#C1");
665 test.SerializeProp3 = true;
666 Assert.IsTrue (prop3PD.ShouldSerializeValue (test), "#C2");
667 test.SerializeProp4 = true;
668 Assert.IsTrue (prop4PD.ShouldSerializeValue (test), "#C3");
669 test.SerializeProp5 = true;
670 Assert.IsTrue (prop5PD.ShouldSerializeValue (test), "#C4");
671 test.SerializeProp6 = true;
672 Assert.IsTrue (prop6PD.ShouldSerializeValue (test), "#C5");
674 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#C6");
675 test.SerializeProp7 = true;
676 Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C7");
678 Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C8");
680 // has both DesignerSerializationVisibility.Content and ShouldSerialize { return false }
681 Assert.IsFalse (prop8PD.ShouldSerializeValue (test), "#D1");
682 // has DesignerSerializationVisibility.Content, no ShouldSerialize
683 Assert.IsTrue (prop9PD.ShouldSerializeValue (test), "#D2");
687 public void CanResetTest_public ()
689 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
690 CanReset_public_test test = new CanReset_public_test ();
692 Assert.IsTrue (p.CanResetValue (test), "1");
693 Assert.AreEqual (5, test.Prop, "2");
695 Assert.AreEqual (10, test.Prop, "3");
699 public void CanResetTest_protected ()
701 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
702 CanReset_protected_test test = new CanReset_protected_test ();
704 Assert.IsTrue (p.CanResetValue (test), "1");
705 Assert.AreEqual (5, test.Prop, "2");
707 Assert.AreEqual (10, test.Prop, "3");
711 public void CanResetTest_private ()
713 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
714 CanReset_private_test test = new CanReset_private_test ();
716 Assert.IsTrue (p.CanResetValue (test), "1");
717 Assert.AreEqual (5, test.Prop, "2");
719 Assert.AreEqual (10, test.Prop, "3");
723 public void CanResetTestNoSetterTest ()
725 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
726 CanResetNoSetter_test test = new CanResetNoSetter_test ();
729 Assert.IsFalse (p.CanResetValue (test), "1");
731 Assert.IsTrue (p.CanResetValue (test), "1");
733 Assert.AreEqual (5, test.Prop, "2");
735 Assert.AreEqual (10, test.Prop, "3");
739 public void NoSerializeOrResetPropTest ()
741 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
742 NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
744 Assert.IsFalse (p.CanResetValue (test), "1");
745 Assert.IsFalse (p.ShouldSerializeValue (test), "2");
749 public void ShouldSerializeFalseEffectOnCanResetTest ()
751 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
752 ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
754 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
755 Assert.IsFalse (p.CanResetValue (test), "2");
759 public void ReadOnlyPropertyTest ()
761 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
762 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
766 public void ReadOnlyAttributeTest ()
768 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
769 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
773 public void ReadOnlyConflictingTest ()
775 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
776 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
780 public void DisplayNameTest ()
782 PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Explicit"];
783 PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Implicit"];
786 Assert.AreEqual ("An explicit displayname", p1.DisplayName, "#1");
788 Assert.AreEqual ("Explicit", p1.DisplayName, "#1");
790 Assert.AreEqual ("Implicit", p2.DisplayName, "#2");
794 public void GetEditorTest ()
796 PropertyDescriptorCollection col;
797 PropertyDescriptor pd;
800 col = TypeDescriptor.GetProperties (typeof (GetEditor_test));
802 ed = pd.GetEditor (typeof (UITypeEditor)) as UITypeEditor;
804 Assert.IsNotNull (ed, "#01");
805 Assert.AreEqual (ed.GetType ().Name, "UIEditor", "#02");
809 public void AddValueChanged ()
811 MockPropertyDescriptor pd = new MockPropertyDescriptor (
812 "Name", new Attribute [0]);
813 object compA = new object ();
814 object compB = new object ();
815 EventHandler handlerA = new EventHandler (ValueChanged1);
816 EventHandler handlerB = new EventHandler (ValueChanged1);
817 EventHandler handlerC = new EventHandler (ValueChanged2);
819 pd.AddValueChanged (compA, handlerA);
820 pd.AddValueChanged (compA, handlerC);
821 pd.AddValueChanged (compA, handlerC);
822 pd.AddValueChanged (compA, handlerB);
824 pd.FireValueChanged (compA, new EventArgs ());
825 Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
826 Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#A1");
827 Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#A2");
828 Assert.AreEqual ("ValueChanged2", _invokedHandlers [2], "#A3");
829 Assert.AreEqual ("ValueChanged1", _invokedHandlers [3], "#A4");
833 pd.FireValueChanged (compB, new EventArgs ());
834 Assert.AreEqual (0, _invokedHandlers.Count, "#B");
838 public void AddValueChanged_Component_Null ()
840 MockPropertyDescriptor pd = new MockPropertyDescriptor (
841 "Name", new Attribute [0]);
843 pd.AddValueChanged (null, new EventHandler (ValueChanged1));
845 } catch (ArgumentNullException ex) {
846 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
847 Assert.IsNull (ex.InnerException, "#3");
848 Assert.IsNotNull (ex.Message, "#4");
849 Assert.IsNotNull (ex.ParamName, "#5");
850 Assert.AreEqual ("component", ex.ParamName, "#6");
855 public void AddValueChanged_Handler_Null ()
857 MockPropertyDescriptor pd = new MockPropertyDescriptor (
858 "Name", new Attribute [0]);
860 pd.AddValueChanged (new object (), (EventHandler) null);
862 } catch (ArgumentNullException ex) {
863 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
864 Assert.IsNull (ex.InnerException, "#3");
865 Assert.IsNotNull (ex.Message, "#4");
866 Assert.IsNotNull (ex.ParamName, "#5");
867 Assert.AreEqual ("handler", ex.ParamName, "#6");
873 public void GetInvocationTarget_Instance_Null ()
875 MockPropertyDescriptor pd = new MockPropertyDescriptor (
876 "Name", new Attribute [0]);
878 pd.GetInvocationTarget (typeof (int), null);
880 } catch (ArgumentNullException ex) {
881 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
882 Assert.IsNull (ex.InnerException, "#3");
883 Assert.IsNotNull (ex.Message, "#4");
884 Assert.IsNotNull (ex.ParamName, "#5");
885 Assert.AreEqual ("instance", ex.ParamName, "#6");
890 public void GetInvocationTarget_Type_Null ()
892 MockPropertyDescriptor pd = new MockPropertyDescriptor (
893 "Name", new Attribute [0]);
895 pd.GetInvocationTarget ((Type) null, new object ());
897 } catch (ArgumentNullException ex) {
898 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
899 Assert.IsNull (ex.InnerException, "#3");
900 Assert.IsNotNull (ex.Message, "#4");
901 Assert.IsNotNull (ex.ParamName, "#5");
902 Assert.AreEqual ("type", ex.ParamName, "#6");
907 public void GetValueChangedHandler ()
909 object compA = new object ();
910 object compB = new object ();
911 EventHandler handlerA = new EventHandler (ValueChanged1);
912 EventHandler handlerB = new EventHandler (ValueChanged1);
913 EventHandler handlerC = new EventHandler (ValueChanged2);
915 MockPropertyDescriptor pd = new MockPropertyDescriptor (
916 "Name", new Attribute [0]);
917 Assert.IsNull (pd.GetValueChangedHandler (null), "#A1");
918 Assert.IsNull (pd.GetValueChangedHandler (compA), "#A2");
919 Assert.IsNull (pd.GetValueChangedHandler (compB), "#A3");
921 pd.AddValueChanged (compA, handlerA);
923 Assert.IsNull (pd.GetValueChangedHandler (null), "#B1");
924 Assert.AreSame (handlerA, pd.GetValueChangedHandler (compA), "#B2");
925 Assert.IsNull (pd.GetValueChangedHandler (compB), "#B3");
927 pd.AddValueChanged (compA, handlerB);
929 Assert.IsNull (pd.GetValueChangedHandler (null), "#C1");
930 EventHandler handler = pd.GetValueChangedHandler (compA);
931 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#C2");
932 Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#C3");
933 Assert.AreEqual (handlerB, handler.GetInvocationList () [1], "#C4");
934 Assert.IsNull (pd.GetValueChangedHandler (compB), "#C5");
936 pd.AddValueChanged (compB, handlerA);
938 Assert.IsNull (pd.GetValueChangedHandler (null), "#D1");
939 handler = pd.GetValueChangedHandler (compA);
940 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#D2");
941 Assert.AreSame (handlerA, pd.GetValueChangedHandler (compB), "#D3");
943 pd.RemoveValueChanged (compB, handlerB);
945 Assert.IsNull (pd.GetValueChangedHandler (null), "#E1");
946 handler = pd.GetValueChangedHandler (compA);
947 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#E2");
948 Assert.IsNull (pd.GetValueChangedHandler (compB), "#E3");
950 pd.RemoveValueChanged (compB, handlerB);
952 Assert.IsNull (pd.GetValueChangedHandler (null), "#F1");
953 handler = pd.GetValueChangedHandler (compA);
954 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#F2");
955 Assert.IsNull (pd.GetValueChangedHandler (compB), "#F3");
957 pd.RemoveValueChanged (compA, handlerC);
959 Assert.IsNull (pd.GetValueChangedHandler (null), "#G1");
960 handler = pd.GetValueChangedHandler (compA);
961 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#G2");
962 Assert.IsNull (pd.GetValueChangedHandler (compB), "#G3");
964 pd.AddValueChanged (compA, handlerC);
966 Assert.IsNull (pd.GetValueChangedHandler (null), "#H1");
967 handler = pd.GetValueChangedHandler (compA);
968 Assert.AreEqual (3, handler.GetInvocationList ().Length, "#H2");
969 Assert.IsNull (pd.GetValueChangedHandler (compB), "#H3");
971 pd.RemoveValueChanged (compA, handlerB);
973 Assert.IsNull (pd.GetValueChangedHandler (null), "#I1");
974 handler = pd.GetValueChangedHandler (compA);
975 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#I2");
976 Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#I3");
977 Assert.AreEqual (handlerC, handler.GetInvocationList () [1], "#I4");
978 Assert.IsNull (pd.GetValueChangedHandler (compB), "#I5");
983 public void RemoveValueChanged ()
985 MockPropertyDescriptor pd = new MockPropertyDescriptor (
986 "Name", new Attribute [0]);
987 object compA = new object ();
988 object compB = new object ();
989 EventHandler handlerA = new EventHandler (ValueChanged1);
990 EventHandler handlerB = new EventHandler (ValueChanged1);
991 EventHandler handlerC = new EventHandler (ValueChanged2);
993 pd.AddValueChanged (compA, handlerA);
994 pd.AddValueChanged (compA, handlerC);
995 pd.AddValueChanged (compA, handlerC);
996 pd.AddValueChanged (compA, handlerB);
997 pd.AddValueChanged (compB, handlerC);
999 pd.FireValueChanged (compA, new EventArgs ());
1000 Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
1001 pd.RemoveValueChanged (new object (), handlerC);
1002 pd.FireValueChanged (compA, new EventArgs ());
1003 Assert.AreEqual (8, _invokedHandlers.Count, "#A2");
1006 pd.RemoveValueChanged (compA, handlerC);
1008 pd.FireValueChanged (compA, new EventArgs ());
1009 Assert.AreEqual (3, _invokedHandlers.Count, "#B1");
1010 Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#B2");
1011 Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#B3");
1012 Assert.AreEqual ("ValueChanged1", _invokedHandlers [2], "#B4");
1016 pd.FireValueChanged (compB, new EventArgs ());
1017 Assert.AreEqual (1, _invokedHandlers.Count, "#C1");
1018 Assert.AreEqual ("ValueChanged2", _invokedHandlers [0], "#C2");
1021 pd.RemoveValueChanged (compB, handlerC);
1023 pd.FireValueChanged (compB, new EventArgs ());
1024 Assert.AreEqual (0, _invokedHandlers.Count, "#D");
1028 public void RemoveValueChanged_Component_Null ()
1030 MockPropertyDescriptor pd = new MockPropertyDescriptor (
1031 "Name", new Attribute [0]);
1033 pd.RemoveValueChanged (null, new EventHandler (ValueChanged1));
1035 } catch (ArgumentNullException ex) {
1036 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1037 Assert.IsNull (ex.InnerException, "#3");
1038 Assert.IsNotNull (ex.Message, "#4");
1039 Assert.IsNotNull (ex.ParamName, "#5");
1040 Assert.AreEqual ("component", ex.ParamName, "#6");
1045 public void RemoveValueChanged_Handler_Null ()
1047 MockPropertyDescriptor pd = new MockPropertyDescriptor (
1048 "Name", new Attribute [0]);
1050 pd.RemoveValueChanged (new object (), (EventHandler) null);
1052 } catch (ArgumentNullException ex) {
1053 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1054 Assert.IsNull (ex.InnerException, "#3");
1055 Assert.IsNotNull (ex.Message, "#4");
1056 Assert.IsNotNull (ex.ParamName, "#5");
1057 Assert.AreEqual ("handler", ex.ParamName, "#6");
1061 void ValueChanged1 (object sender, EventArgs e)
1063 _invokedHandlers.Add ("ValueChanged1");
1066 void ValueChanged2 (object sender, EventArgs e)
1068 _invokedHandlers.Add ("ValueChanged2");
1071 static Attribute FindAttribute (PropertyDescriptor pd, Type type)
1073 foreach (Attribute attr in pd.Attributes)
1074 if (attr.GetType () == type)
1079 class GetEditor_test
1081 [Editor (typeof (UIEditor), typeof (UITypeEditor))]
1082 public string Property {
1083 get { return "abc"; }
1088 class UIEditor : UITypeEditor
1093 class MockPropertyDescriptor : PropertyDescriptor
1095 public MockPropertyDescriptor (MemberDescriptor reference)
1100 public MockPropertyDescriptor (MemberDescriptor reference, Attribute [] attrs)
1101 : base (reference, attrs)
1105 public MockPropertyDescriptor (string name, Attribute [] attrs)
1106 : base (name, attrs)
1110 public override Type ComponentType {
1111 get { return typeof (int); }
1114 public override bool IsReadOnly {
1115 get { return false; }
1118 public override Type PropertyType{
1119 get { return typeof (DateTime); }
1122 public override object GetValue (object component)
1127 public override void SetValue (object component, object value)
1131 public override void ResetValue (object component)
1135 public override bool CanResetValue (object component)
1140 public override bool ShouldSerializeValue (object component)
1145 public void FireValueChanged (object component, EventArgs e)
1147 base.OnValueChanged (component, e);
1151 public new object GetInvocationTarget (Type type, object instance)
1153 return base.GetInvocationTarget (type, instance);
1156 public new EventHandler GetValueChangedHandler (object component)
1158 return base.GetValueChangedHandler (component);
1163 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
1164 public class PropTestAttribute : Attribute
1166 public PropTestAttribute ()
1171 public class TestBase
1174 public int PropBase1
1181 [Description ("whatever")]
1182 public string PropBase2
1189 public virtual string PropBase3
1196 public class TestSub : TestBase
1206 public string PropSub2
1212 public override string PropBase3