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 System.ComponentModel.Design;
15 using System.Drawing.Design;
16 using System.Globalization;
17 using System.Reflection;
18 using System.Runtime.InteropServices;
20 using NUnit.Framework;
22 namespace MonoTests.System.ComponentModel
24 internal class MyVersionTypeConverter : TypeConverter
29 public class PropertyDescriptorTests
31 class MissingConverterType_test
33 public class NestedClass { }
35 [TypeConverter ("missing-type-name")]
36 public NestedClass Prop {
40 [TypeConverter ("missing-type-name")]
45 [TypeConverter ("missing-type-name")]
46 public string StringProp {
51 class ReadOnlyProperty_test
58 class ReadOnlyAttribute_test
67 class ConflictingReadOnly_test
75 class ShouldSerialize_public_test
81 public bool ShouldSerializeProp()
87 class ShouldSerialize_protected_test
93 protected bool ShouldSerializeProp()
99 class ShouldSerialize_private_test
105 private bool ShouldSerializeProp()
111 class ShouldSerializeFalseEffectOnCanReset_test
118 public bool ShouldSerializeProp()
123 public void ResetProp()
128 class ShouldSerialize_Null_Default
130 [DefaultValue (null)]
132 get { return _prop; }
133 set { _prop = value; }
136 public bool SerializeProp {
137 get { return _serializeProp; }
138 set { _serializeProp = value; }
141 public bool ShouldSerializeProp ()
143 return _serializeProp;
146 private string _prop;
147 private bool _serializeProp;
150 class ShouldSerialize_No_Default
153 get { return _prop; }
154 set { _prop = value; }
157 private string _prop;
160 class ShouldSerialize_ReadOnly
163 [DefaultValue ("ok")]
164 public string Prop1 {
165 get { return _prop1; }
166 set { _prop1 = value; }
170 public string Prop2 {
171 get { return _prop2; }
172 set { _prop2 = value; }
176 public string Prop3 {
177 get { return _prop3; }
178 set { _prop3 = value; }
182 public string Prop4 {
183 get { return _prop4; }
184 set { _prop4 = value; }
187 public string Prop5 {
188 get { return _prop5; }
191 [DefaultValue ("bad")]
192 public string Prop6 {
193 get { return _prop6; }
197 [DefaultValue ("good")]
198 public string Prop7 {
199 get { return _prop7; }
200 set { _prop7 = value; }
204 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
205 public string Prop8 {
210 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
211 public string Prop9 {
215 public bool SerializeProp3 {
216 get { return _serializeProp3; }
217 set { _serializeProp3 = value; }
220 public bool SerializeProp4 {
221 get { return _serializeProp4; }
222 set { _serializeProp4 = value; }
225 public bool SerializeProp5 {
226 get { return _serializeProp5; }
227 set { _serializeProp5 = value; }
230 public bool SerializeProp6 {
231 get { return _serializeProp6; }
232 set { _serializeProp6 = value; }
235 public bool SerializeProp7 {
236 get { return _serializeProp7; }
237 set { _serializeProp7 = value; }
240 public bool ShouldSerializeProp3 ()
242 return _serializeProp3;
245 public bool ShouldSerializeProp4 ()
247 return _serializeProp4;
250 public bool ShouldSerializeProp5 ()
252 return _serializeProp5;
255 public bool ShouldSerializeProp6 ()
257 return _serializeProp6;
260 public bool ShouldSerializeProp7 ()
262 return _serializeProp7;
265 public bool ShouldSerializeProp8 ()
270 private string _prop1;
271 private string _prop2;
272 private string _prop3;
273 private string _prop4;
274 private string _prop5 = "good";
275 private string _prop6 = "bad";
276 private string _prop7;
277 private bool _serializeProp3;
278 private bool _serializeProp4;
279 private bool _serializeProp5;
280 private bool _serializeProp6;
281 private bool _serializeProp7;
284 class NoSerializeOrResetProp_test
291 class CanReset_public_test
296 set { prop = value; }
299 public void ResetProp()
305 class CanReset_protected_test
310 set { prop = value; }
313 protected void ResetProp()
319 class CanReset_private_test
324 set { prop = value; }
327 private void ResetProp()
333 class CanResetNoSetter_test
340 private void ResetProp()
346 class DisplayName_test
349 [DisplayName ("An explicit displayname")]
351 public bool Explicit {
352 get { return false; }
355 public bool Implicit {
356 get { return false; }
362 public virtual Version NoConverter {
366 [TypeConverter (typeof(MyVersionTypeConverter))]
367 public virtual Version WithConverter {
371 [TypeConverter ("MonoTests.System.ComponentModel.MyVersionTypeConverter")]
372 public virtual Version WithConverterNamed {
376 [TypeConverter("System.ComponentModel.CharConverter, " + Consts.AssemblySystem)]
377 public virtual Version WithConverterNamedAssmQuald {
381 public int WithDefaultConverter {
386 class ConverterSubclassNotOverridenProperties_test : Converter_test
390 class ConverterSubclassOverridenProperties_test : Converter_test
392 public override Version WithConverter {
396 public override Version WithConverterNamed {
401 class ConverterEmptyConvertersOnOveriddenProperties : Converter_test
404 public override Version WithConverter {
409 public override Version WithConverterNamed {
414 private ArrayList _invokedHandlers;
419 _invokedHandlers = new ArrayList ();
424 _invokedHandlers.Clear ();
428 public void Attributes ()
430 PropertyDescriptorCollection properties;
431 PropertyDescriptor pd;
433 properties = TypeDescriptor.GetProperties (typeof (TestBase));
435 pd = properties ["PropBase3"];
436 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#A1");
437 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#A2");
439 pd = properties ["PropBase2"];
440 Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#B1");
441 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#B2");
443 pd = properties ["PropBase1"];
444 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#C1");
445 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#C2");
447 properties = TypeDescriptor.GetProperties (typeof (TestSub));
449 pd = properties ["PropBase3"];
450 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#D1");
451 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#D2");
453 pd = properties ["PropBase2"];
454 Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#E1");
455 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#E2");
457 pd = properties ["PropBase1"];
458 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#F1");
459 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#F2");
463 public void MissingTypeConverter ()
465 PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["Prop"];
466 PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["IntProp"];
467 PropertyDescriptor p3 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["StringProp"];
469 Assert.AreEqual (typeof (TypeConverter), p1.Converter.GetType (), "1");
470 Assert.AreEqual (typeof (Int32Converter), p2.Converter.GetType (), "2");
471 Assert.AreEqual (typeof (StringConverter), p3.Converter.GetType (), "3");
475 public void ConverterTest ()
477 Assert.AreEqual (typeof (TypeConverter),
478 TypeDescriptor.GetProperties (typeof (Converter_test))["NoConverter"].Converter.GetType (), "#1");
479 Assert.AreEqual (typeof (MyVersionTypeConverter),
480 TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverter"].Converter.GetType (), "#2");
481 Assert.AreEqual (typeof (MyVersionTypeConverter),
482 TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverterNamed"].Converter.GetType (), "#3");
483 Assert.AreEqual (typeof (CharConverter),
484 TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#4");
485 Assert.AreEqual (typeof (Int32Converter),
486 TypeDescriptor.GetProperties (typeof (Converter_test))["WithDefaultConverter"].Converter.GetType (), "#5");
488 Assert.AreEqual (typeof (TypeConverter),
489 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["NoConverter"].Converter.GetType (), "#6");
490 Assert.AreEqual (typeof (MyVersionTypeConverter),
491 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverter"].Converter.GetType (), "#7");
492 Assert.AreEqual (typeof (MyVersionTypeConverter),
493 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverterNamed"].Converter.GetType (), "#8");
494 Assert.AreEqual (typeof (CharConverter),
495 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#9");
496 Assert.AreEqual (typeof (Int32Converter),
497 TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithDefaultConverter"].Converter.GetType (), "#10");
499 Assert.AreEqual (typeof (TypeConverter),
500 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["NoConverter"].Converter.GetType (), "#11");
501 Assert.AreEqual (typeof (MyVersionTypeConverter),
502 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverter"].Converter.GetType (), "#12");
503 Assert.AreEqual (typeof (MyVersionTypeConverter),
504 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverterNamed"].Converter.GetType (), "#13");
505 Assert.AreEqual (typeof (CharConverter),
506 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#14");
507 Assert.AreEqual (typeof (Int32Converter),
508 TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithDefaultConverter"].Converter.GetType (), "#15");
510 Assert.AreEqual (typeof (TypeConverter),
511 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["NoConverter"].Converter.GetType (), "#116");
512 Assert.AreEqual (typeof (TypeConverter),
513 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverter"].Converter.GetType (), "#17");
514 Assert.AreEqual (typeof (TypeConverter),
515 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverterNamed"].Converter.GetType (), "#18");
516 Assert.AreEqual (typeof (CharConverter),
517 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverterNamedAssmQuald"].Converter.GetType (), "#19");
518 Assert.AreEqual (typeof (Int32Converter),
519 TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithDefaultConverter"].Converter.GetType (), "#20");
523 public void ShouldSerializeTest_public ()
525 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
526 ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
528 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
532 public void ShouldSerializeTest_protected ()
534 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
535 ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
537 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
541 public void ShouldSerializeTest_private ()
543 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
544 ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
546 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
550 public void ShouldSerializeTest_No_Default ()
552 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_No_Default)) ["Prop"];
553 ShouldSerialize_No_Default test = new ShouldSerialize_No_Default ();
555 Assert.IsTrue (p.ShouldSerializeValue (test), "#1");
556 test.Prop = "whatever";
557 Assert.IsTrue (p.ShouldSerializeValue (test), "#2");
561 public void ShouldSerializeTest_Null_Default ()
563 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_Null_Default)) ["Prop"];
564 ShouldSerialize_Null_Default test = new ShouldSerialize_Null_Default ();
566 Assert.IsFalse (p.ShouldSerializeValue (test), "#1");
567 test.SerializeProp = true;
568 Assert.IsFalse (p.ShouldSerializeValue (test), "#2");
569 test.Prop = "whatever";
570 Assert.IsTrue (p.ShouldSerializeValue (test), "#3");
571 test.SerializeProp = false;
572 Assert.IsTrue (p.ShouldSerializeValue (test), "#4");
576 public void ShouldSerializeTest_ReadOnly ()
578 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (
579 typeof (ShouldSerialize_ReadOnly));
580 ShouldSerialize_ReadOnly test = new ShouldSerialize_ReadOnly ();
582 PropertyDescriptor prop1PD = properties ["Prop1"];
583 PropertyDescriptor prop2PD = properties ["Prop2"];
584 PropertyDescriptor prop3PD = properties ["Prop3"];
585 PropertyDescriptor prop4PD = properties ["Prop4"];
586 PropertyDescriptor prop5PD = properties ["Prop5"];
587 PropertyDescriptor prop6PD = properties ["Prop6"];
588 PropertyDescriptor prop7PD = properties ["Prop7"];
589 PropertyDescriptor prop8PD = properties ["Prop8"];
590 PropertyDescriptor prop9PD = properties ["Prop9"];
592 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#A1");
593 Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#A2");
594 Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#A3");
595 Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#A4");
596 Assert.IsFalse (prop5PD.ShouldSerializeValue (test), "#A5");
597 Assert.IsFalse (prop6PD.ShouldSerializeValue (test), "#A6");
598 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#A7");
600 test.Prop1 = "whatever";
601 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#B1");
602 test.Prop2 = "whatever";
603 Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#B2");
604 test.Prop3 = "whatever";
605 Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#B3");
606 test.Prop4 = "whatever";
607 Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#B4");
608 test.Prop7 = "whatever";
609 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#B5");
612 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#C1");
613 test.SerializeProp3 = true;
614 Assert.IsTrue (prop3PD.ShouldSerializeValue (test), "#C2");
615 test.SerializeProp4 = true;
616 Assert.IsTrue (prop4PD.ShouldSerializeValue (test), "#C3");
617 test.SerializeProp5 = true;
618 Assert.IsTrue (prop5PD.ShouldSerializeValue (test), "#C4");
619 test.SerializeProp6 = true;
620 Assert.IsTrue (prop6PD.ShouldSerializeValue (test), "#C5");
622 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#C6");
623 test.SerializeProp7 = true;
624 Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C7");
626 Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C8");
628 // has both DesignerSerializationVisibility.Content and ShouldSerialize { return false }
629 Assert.IsFalse (prop8PD.ShouldSerializeValue (test), "#D1");
630 // has DesignerSerializationVisibility.Content, no ShouldSerialize
631 Assert.IsTrue (prop9PD.ShouldSerializeValue (test), "#D2");
635 public void CanResetTest_public ()
637 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
638 CanReset_public_test test = new CanReset_public_test ();
640 Assert.IsTrue (p.CanResetValue (test), "1");
641 Assert.AreEqual (5, test.Prop, "2");
643 Assert.AreEqual (10, test.Prop, "3");
647 public void CanResetTest_protected ()
649 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
650 CanReset_protected_test test = new CanReset_protected_test ();
652 Assert.IsTrue (p.CanResetValue (test), "1");
653 Assert.AreEqual (5, test.Prop, "2");
655 Assert.AreEqual (10, test.Prop, "3");
659 public void CanResetTest_private ()
661 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
662 CanReset_private_test test = new CanReset_private_test ();
664 Assert.IsTrue (p.CanResetValue (test), "1");
665 Assert.AreEqual (5, test.Prop, "2");
667 Assert.AreEqual (10, test.Prop, "3");
671 public void CanResetTestNoSetterTest ()
673 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
674 CanResetNoSetter_test test = new CanResetNoSetter_test ();
677 Assert.IsFalse (p.CanResetValue (test), "1");
679 Assert.IsTrue (p.CanResetValue (test), "1");
681 Assert.AreEqual (5, test.Prop, "2");
683 Assert.AreEqual (10, test.Prop, "3");
687 public void NoSerializeOrResetPropTest ()
689 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
690 NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
692 Assert.IsFalse (p.CanResetValue (test), "1");
693 Assert.IsFalse (p.ShouldSerializeValue (test), "2");
697 public void ShouldSerializeFalseEffectOnCanResetTest ()
699 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
700 ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
702 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
703 Assert.IsFalse (p.CanResetValue (test), "2");
707 public void ReadOnlyPropertyTest ()
709 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
710 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
714 public void ReadOnlyAttributeTest ()
716 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
717 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
721 public void ReadOnlyConflictingTest ()
723 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
724 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
728 public void DisplayNameTest ()
730 PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Explicit"];
731 PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Implicit"];
734 Assert.AreEqual ("An explicit displayname", p1.DisplayName, "#1");
736 Assert.AreEqual ("Explicit", p1.DisplayName, "#1");
738 Assert.AreEqual ("Implicit", p2.DisplayName, "#2");
742 public void GetEditorTest ()
744 PropertyDescriptorCollection col;
745 PropertyDescriptor pd;
748 col = TypeDescriptor.GetProperties (typeof (GetEditor_test));
750 ed = pd.GetEditor (typeof (UITypeEditor)) as UITypeEditor;
752 Assert.IsNotNull (ed, "#01");
753 Assert.AreEqual (ed.GetType ().Name, "UIEditor", "#02");
757 public void AddValueChanged ()
759 MockPropertyDescriptor pd = new MockPropertyDescriptor (
760 "Name", new Attribute [0]);
761 object compA = new object ();
762 object compB = new object ();
763 EventHandler handlerA = new EventHandler (ValueChanged1);
764 EventHandler handlerB = new EventHandler (ValueChanged1);
765 EventHandler handlerC = new EventHandler (ValueChanged2);
767 pd.AddValueChanged (compA, handlerA);
768 pd.AddValueChanged (compA, handlerC);
769 pd.AddValueChanged (compA, handlerC);
770 pd.AddValueChanged (compA, handlerB);
772 pd.FireValueChanged (compA, new EventArgs ());
773 Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
774 Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#A1");
775 Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#A2");
776 Assert.AreEqual ("ValueChanged2", _invokedHandlers [2], "#A3");
777 Assert.AreEqual ("ValueChanged1", _invokedHandlers [3], "#A4");
781 pd.FireValueChanged (compB, new EventArgs ());
782 Assert.AreEqual (0, _invokedHandlers.Count, "#B");
786 public void AddValueChanged_Component_Null ()
788 MockPropertyDescriptor pd = new MockPropertyDescriptor (
789 "Name", new Attribute [0]);
791 pd.AddValueChanged (null, new EventHandler (ValueChanged1));
793 } catch (ArgumentNullException ex) {
794 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
795 Assert.IsNull (ex.InnerException, "#3");
796 Assert.IsNotNull (ex.Message, "#4");
797 Assert.IsNotNull (ex.ParamName, "#5");
798 Assert.AreEqual ("component", ex.ParamName, "#6");
803 public void AddValueChanged_Handler_Null ()
805 MockPropertyDescriptor pd = new MockPropertyDescriptor (
806 "Name", new Attribute [0]);
808 pd.AddValueChanged (new object (), (EventHandler) null);
810 } catch (ArgumentNullException ex) {
811 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
812 Assert.IsNull (ex.InnerException, "#3");
813 Assert.IsNotNull (ex.Message, "#4");
814 Assert.IsNotNull (ex.ParamName, "#5");
815 Assert.AreEqual ("handler", ex.ParamName, "#6");
821 public void GetInvocationTarget_Instance_Null ()
823 MockPropertyDescriptor pd = new MockPropertyDescriptor (
824 "Name", new Attribute [0]);
826 pd.GetInvocationTarget (typeof (int), null);
828 } catch (ArgumentNullException ex) {
829 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
830 Assert.IsNull (ex.InnerException, "#3");
831 Assert.IsNotNull (ex.Message, "#4");
832 Assert.IsNotNull (ex.ParamName, "#5");
833 Assert.AreEqual ("instance", ex.ParamName, "#6");
838 public void GetInvocationTarget_Type_Null ()
840 MockPropertyDescriptor pd = new MockPropertyDescriptor (
841 "Name", new Attribute [0]);
843 pd.GetInvocationTarget ((Type) null, new object ());
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 ("type", ex.ParamName, "#6");
855 public void GetValueChangedHandler ()
857 object compA = new object ();
858 object compB = new object ();
859 EventHandler handlerA = new EventHandler (ValueChanged1);
860 EventHandler handlerB = new EventHandler (ValueChanged1);
861 EventHandler handlerC = new EventHandler (ValueChanged2);
863 MockPropertyDescriptor pd = new MockPropertyDescriptor (
864 "Name", new Attribute [0]);
865 Assert.IsNull (pd.GetValueChangedHandler (null), "#A1");
866 Assert.IsNull (pd.GetValueChangedHandler (compA), "#A2");
867 Assert.IsNull (pd.GetValueChangedHandler (compB), "#A3");
869 pd.AddValueChanged (compA, handlerA);
871 Assert.IsNull (pd.GetValueChangedHandler (null), "#B1");
872 Assert.AreSame (handlerA, pd.GetValueChangedHandler (compA), "#B2");
873 Assert.IsNull (pd.GetValueChangedHandler (compB), "#B3");
875 pd.AddValueChanged (compA, handlerB);
877 Assert.IsNull (pd.GetValueChangedHandler (null), "#C1");
878 EventHandler handler = pd.GetValueChangedHandler (compA);
879 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#C2");
880 Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#C3");
881 Assert.AreEqual (handlerB, handler.GetInvocationList () [1], "#C4");
882 Assert.IsNull (pd.GetValueChangedHandler (compB), "#C5");
884 pd.AddValueChanged (compB, handlerA);
886 Assert.IsNull (pd.GetValueChangedHandler (null), "#D1");
887 handler = pd.GetValueChangedHandler (compA);
888 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#D2");
889 Assert.AreSame (handlerA, pd.GetValueChangedHandler (compB), "#D3");
891 pd.RemoveValueChanged (compB, handlerB);
893 Assert.IsNull (pd.GetValueChangedHandler (null), "#E1");
894 handler = pd.GetValueChangedHandler (compA);
895 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#E2");
896 Assert.IsNull (pd.GetValueChangedHandler (compB), "#E3");
898 pd.RemoveValueChanged (compB, handlerB);
900 Assert.IsNull (pd.GetValueChangedHandler (null), "#F1");
901 handler = pd.GetValueChangedHandler (compA);
902 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#F2");
903 Assert.IsNull (pd.GetValueChangedHandler (compB), "#F3");
905 pd.RemoveValueChanged (compA, handlerC);
907 Assert.IsNull (pd.GetValueChangedHandler (null), "#G1");
908 handler = pd.GetValueChangedHandler (compA);
909 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#G2");
910 Assert.IsNull (pd.GetValueChangedHandler (compB), "#G3");
912 pd.AddValueChanged (compA, handlerC);
914 Assert.IsNull (pd.GetValueChangedHandler (null), "#H1");
915 handler = pd.GetValueChangedHandler (compA);
916 Assert.AreEqual (3, handler.GetInvocationList ().Length, "#H2");
917 Assert.IsNull (pd.GetValueChangedHandler (compB), "#H3");
919 pd.RemoveValueChanged (compA, handlerB);
921 Assert.IsNull (pd.GetValueChangedHandler (null), "#I1");
922 handler = pd.GetValueChangedHandler (compA);
923 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#I2");
924 Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#I3");
925 Assert.AreEqual (handlerC, handler.GetInvocationList () [1], "#I4");
926 Assert.IsNull (pd.GetValueChangedHandler (compB), "#I5");
931 public void RemoveValueChanged ()
933 MockPropertyDescriptor pd = new MockPropertyDescriptor (
934 "Name", new Attribute [0]);
935 object compA = new object ();
936 object compB = new object ();
937 EventHandler handlerA = new EventHandler (ValueChanged1);
938 EventHandler handlerB = new EventHandler (ValueChanged1);
939 EventHandler handlerC = new EventHandler (ValueChanged2);
941 pd.AddValueChanged (compA, handlerA);
942 pd.AddValueChanged (compA, handlerC);
943 pd.AddValueChanged (compA, handlerC);
944 pd.AddValueChanged (compA, handlerB);
945 pd.AddValueChanged (compB, handlerC);
947 pd.FireValueChanged (compA, new EventArgs ());
948 Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
949 pd.RemoveValueChanged (new object (), handlerC);
950 pd.FireValueChanged (compA, new EventArgs ());
951 Assert.AreEqual (8, _invokedHandlers.Count, "#A2");
954 pd.RemoveValueChanged (compA, handlerC);
956 pd.FireValueChanged (compA, new EventArgs ());
957 Assert.AreEqual (3, _invokedHandlers.Count, "#B1");
958 Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#B2");
959 Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#B3");
960 Assert.AreEqual ("ValueChanged1", _invokedHandlers [2], "#B4");
964 pd.FireValueChanged (compB, new EventArgs ());
965 Assert.AreEqual (1, _invokedHandlers.Count, "#C1");
966 Assert.AreEqual ("ValueChanged2", _invokedHandlers [0], "#C2");
969 pd.RemoveValueChanged (compB, handlerC);
971 pd.FireValueChanged (compB, new EventArgs ());
972 Assert.AreEqual (0, _invokedHandlers.Count, "#D");
976 public void RemoveValueChanged_Component_Null ()
978 MockPropertyDescriptor pd = new MockPropertyDescriptor (
979 "Name", new Attribute [0]);
981 pd.RemoveValueChanged (null, new EventHandler (ValueChanged1));
983 } catch (ArgumentNullException ex) {
984 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
985 Assert.IsNull (ex.InnerException, "#3");
986 Assert.IsNotNull (ex.Message, "#4");
987 Assert.IsNotNull (ex.ParamName, "#5");
988 Assert.AreEqual ("component", ex.ParamName, "#6");
993 public void RemoveValueChanged_Handler_Null ()
995 MockPropertyDescriptor pd = new MockPropertyDescriptor (
996 "Name", new Attribute [0]);
998 pd.RemoveValueChanged (new object (), (EventHandler) null);
1000 } catch (ArgumentNullException ex) {
1001 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1002 Assert.IsNull (ex.InnerException, "#3");
1003 Assert.IsNotNull (ex.Message, "#4");
1004 Assert.IsNotNull (ex.ParamName, "#5");
1005 Assert.AreEqual ("handler", ex.ParamName, "#6");
1009 void ValueChanged1 (object sender, EventArgs e)
1011 _invokedHandlers.Add ("ValueChanged1");
1014 void ValueChanged2 (object sender, EventArgs e)
1016 _invokedHandlers.Add ("ValueChanged2");
1019 static Attribute FindAttribute (PropertyDescriptor pd, Type type)
1021 foreach (Attribute attr in pd.Attributes)
1022 if (attr.GetType () == type)
1027 class GetEditor_test
1029 [Editor (typeof (UIEditor), typeof (UITypeEditor))]
1030 public string Property {
1031 get { return "abc"; }
1036 class UIEditor : UITypeEditor
1041 class MockPropertyDescriptor : PropertyDescriptor
1043 public MockPropertyDescriptor (MemberDescriptor reference)
1048 public MockPropertyDescriptor (MemberDescriptor reference, Attribute [] attrs)
1049 : base (reference, attrs)
1053 public MockPropertyDescriptor (string name, Attribute [] attrs)
1054 : base (name, attrs)
1058 public override Type ComponentType {
1059 get { return typeof (int); }
1062 public override bool IsReadOnly {
1063 get { return false; }
1066 public override Type PropertyType{
1067 get { return typeof (DateTime); }
1070 public override object GetValue (object component)
1075 public override void SetValue (object component, object value)
1079 public override void ResetValue (object component)
1083 public override bool CanResetValue (object component)
1088 public override bool ShouldSerializeValue (object component)
1093 public void FireValueChanged (object component, EventArgs e)
1095 base.OnValueChanged (component, e);
1099 public new object GetInvocationTarget (Type type, object instance)
1101 return base.GetInvocationTarget (type, instance);
1104 public new EventHandler GetValueChangedHandler (object component)
1106 return base.GetValueChangedHandler (component);
1111 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
1112 public class PropTestAttribute : Attribute
1114 public PropTestAttribute ()
1119 public class TestBase
1122 public int PropBase1
1129 [Description ("whatever")]
1130 public string PropBase2
1137 public virtual string PropBase3
1144 public class TestSub : TestBase
1154 public string PropSub2
1160 public override string PropBase3