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
25 public class PropertyDescriptorTests
27 class MissingConverterType_test
29 public class NestedClass { }
31 [TypeConverter ("missing-type-name")]
32 public NestedClass Prop {
36 [TypeConverter ("missing-type-name")]
41 [TypeConverter ("missing-type-name")]
42 public string StringProp {
47 class ReadOnlyProperty_test
54 class ReadOnlyAttribute_test
63 class ConflictingReadOnly_test
71 class ShouldSerialize_public_test
77 public bool ShouldSerializeProp()
83 class ShouldSerialize_protected_test
89 protected bool ShouldSerializeProp()
95 class ShouldSerialize_private_test
101 private bool ShouldSerializeProp()
107 class ShouldSerializeFalseEffectOnCanReset_test
114 public bool ShouldSerializeProp()
119 public void ResetProp()
124 class ShouldSerialize_Null_Default
126 [DefaultValue (null)]
128 get { return _prop; }
129 set { _prop = value; }
132 public bool SerializeProp {
133 get { return _serializeProp; }
134 set { _serializeProp = value; }
137 public bool ShouldSerializeProp ()
139 return _serializeProp;
142 private string _prop;
143 private bool _serializeProp;
146 class ShouldSerialize_No_Default
149 get { return _prop; }
150 set { _prop = value; }
153 private string _prop;
156 class ShouldSerialize_ReadOnly
159 [DefaultValue ("ok")]
160 public string Prop1 {
161 get { return _prop1; }
162 set { _prop1 = value; }
166 public string Prop2 {
167 get { return _prop2; }
168 set { _prop2 = value; }
172 public string Prop3 {
173 get { return _prop3; }
174 set { _prop3 = value; }
178 public string Prop4 {
179 get { return _prop4; }
180 set { _prop4 = value; }
183 public string Prop5 {
184 get { return _prop5; }
187 [DefaultValue ("bad")]
188 public string Prop6 {
189 get { return _prop6; }
193 [DefaultValue ("good")]
194 public string Prop7 {
195 get { return _prop7; }
196 set { _prop7 = value; }
200 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
201 public string Prop8 {
206 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
207 public string Prop9 {
211 public bool SerializeProp3 {
212 get { return _serializeProp3; }
213 set { _serializeProp3 = value; }
216 public bool SerializeProp4 {
217 get { return _serializeProp4; }
218 set { _serializeProp4 = value; }
221 public bool SerializeProp5 {
222 get { return _serializeProp5; }
223 set { _serializeProp5 = value; }
226 public bool SerializeProp6 {
227 get { return _serializeProp6; }
228 set { _serializeProp6 = value; }
231 public bool SerializeProp7 {
232 get { return _serializeProp7; }
233 set { _serializeProp7 = value; }
236 public bool ShouldSerializeProp3 ()
238 return _serializeProp3;
241 public bool ShouldSerializeProp4 ()
243 return _serializeProp4;
246 public bool ShouldSerializeProp5 ()
248 return _serializeProp5;
251 public bool ShouldSerializeProp6 ()
253 return _serializeProp6;
256 public bool ShouldSerializeProp7 ()
258 return _serializeProp7;
261 public bool ShouldSerializeProp8 ()
266 private string _prop1;
267 private string _prop2;
268 private string _prop3;
269 private string _prop4;
270 private string _prop5 = "good";
271 private string _prop6 = "bad";
272 private string _prop7;
273 private bool _serializeProp3;
274 private bool _serializeProp4;
275 private bool _serializeProp5;
276 private bool _serializeProp6;
277 private bool _serializeProp7;
280 class NoSerializeOrResetProp_test
287 class CanReset_public_test
292 set { prop = value; }
295 public void ResetProp()
301 class CanReset_protected_test
306 set { prop = value; }
309 protected void ResetProp()
315 class CanReset_private_test
320 set { prop = value; }
323 private void ResetProp()
329 class CanResetNoSetter_test
336 private void ResetProp()
342 class DisplayName_test
345 [DisplayName ("An explicit displayname")]
347 public bool Explicit {
348 get { return false; }
351 public bool Implicit {
352 get { return false; }
356 private ArrayList _invokedHandlers;
361 _invokedHandlers = new ArrayList ();
366 _invokedHandlers.Clear ();
370 public void Attributes ()
372 PropertyDescriptorCollection properties;
373 PropertyDescriptor pd;
375 properties = TypeDescriptor.GetProperties (typeof (TestBase));
377 pd = properties ["PropBase3"];
378 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#A1");
379 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#A2");
381 pd = properties ["PropBase2"];
382 Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#B1");
383 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#B2");
385 pd = properties ["PropBase1"];
386 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#C1");
387 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#C2");
389 properties = TypeDescriptor.GetProperties (typeof (TestSub));
391 pd = properties ["PropBase3"];
392 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#D1");
393 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#D2");
395 pd = properties ["PropBase2"];
396 Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#E1");
397 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#E2");
399 pd = properties ["PropBase1"];
400 Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#F1");
401 Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#F2");
405 public void MissingTypeConverter ()
407 PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["Prop"];
408 PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["IntProp"];
409 PropertyDescriptor p3 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["StringProp"];
411 Assert.AreEqual (typeof (TypeConverter), p1.Converter.GetType (), "1");
412 Assert.AreEqual (typeof (Int32Converter), p2.Converter.GetType (), "2");
413 Assert.AreEqual (typeof (StringConverter), p3.Converter.GetType (), "3");
417 public void ShouldSerializeTest_public ()
419 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
420 ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
422 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
426 public void ShouldSerializeTest_protected ()
428 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
429 ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
431 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
435 public void ShouldSerializeTest_private ()
437 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
438 ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
440 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
444 public void ShouldSerializeTest_No_Default ()
446 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_No_Default)) ["Prop"];
447 ShouldSerialize_No_Default test = new ShouldSerialize_No_Default ();
449 Assert.IsTrue (p.ShouldSerializeValue (test), "#1");
450 test.Prop = "whatever";
451 Assert.IsTrue (p.ShouldSerializeValue (test), "#2");
455 public void ShouldSerializeTest_Null_Default ()
457 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_Null_Default)) ["Prop"];
458 ShouldSerialize_Null_Default test = new ShouldSerialize_Null_Default ();
460 Assert.IsFalse (p.ShouldSerializeValue (test), "#1");
461 test.SerializeProp = true;
462 Assert.IsFalse (p.ShouldSerializeValue (test), "#2");
463 test.Prop = "whatever";
464 Assert.IsTrue (p.ShouldSerializeValue (test), "#3");
465 test.SerializeProp = false;
466 Assert.IsTrue (p.ShouldSerializeValue (test), "#4");
470 public void ShouldSerializeTest_ReadOnly ()
472 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (
473 typeof (ShouldSerialize_ReadOnly));
474 ShouldSerialize_ReadOnly test = new ShouldSerialize_ReadOnly ();
476 PropertyDescriptor prop1PD = properties ["Prop1"];
477 PropertyDescriptor prop2PD = properties ["Prop2"];
478 PropertyDescriptor prop3PD = properties ["Prop3"];
479 PropertyDescriptor prop4PD = properties ["Prop4"];
480 PropertyDescriptor prop5PD = properties ["Prop5"];
481 PropertyDescriptor prop6PD = properties ["Prop6"];
482 PropertyDescriptor prop7PD = properties ["Prop7"];
483 PropertyDescriptor prop8PD = properties ["Prop8"];
484 PropertyDescriptor prop9PD = properties ["Prop9"];
486 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#A1");
487 Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#A2");
488 Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#A3");
489 Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#A4");
490 Assert.IsFalse (prop5PD.ShouldSerializeValue (test), "#A5");
491 Assert.IsFalse (prop6PD.ShouldSerializeValue (test), "#A6");
492 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#A7");
494 test.Prop1 = "whatever";
495 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#B1");
496 test.Prop2 = "whatever";
497 Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#B2");
498 test.Prop3 = "whatever";
499 Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#B3");
500 test.Prop4 = "whatever";
501 Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#B4");
502 test.Prop7 = "whatever";
503 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#B5");
506 Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#C1");
507 test.SerializeProp3 = true;
508 Assert.IsTrue (prop3PD.ShouldSerializeValue (test), "#C2");
509 test.SerializeProp4 = true;
510 Assert.IsTrue (prop4PD.ShouldSerializeValue (test), "#C3");
511 test.SerializeProp5 = true;
512 Assert.IsTrue (prop5PD.ShouldSerializeValue (test), "#C4");
513 test.SerializeProp6 = true;
514 Assert.IsTrue (prop6PD.ShouldSerializeValue (test), "#C5");
516 Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#C6");
517 test.SerializeProp7 = true;
518 Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C7");
520 Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C8");
522 // has both DesignerSerializationVisibility.Content and ShouldSerialize { return false }
523 Assert.IsFalse (prop8PD.ShouldSerializeValue (test), "#D1");
524 // has DesignerSerializationVisibility.Content, no ShouldSerialize
525 Assert.IsTrue (prop9PD.ShouldSerializeValue (test), "#D2");
529 public void CanResetTest_public ()
531 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
532 CanReset_public_test test = new CanReset_public_test ();
534 Assert.IsTrue (p.CanResetValue (test), "1");
535 Assert.AreEqual (5, test.Prop, "2");
537 Assert.AreEqual (10, test.Prop, "3");
541 public void CanResetTest_protected ()
543 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
544 CanReset_protected_test test = new CanReset_protected_test ();
546 Assert.IsTrue (p.CanResetValue (test), "1");
547 Assert.AreEqual (5, test.Prop, "2");
549 Assert.AreEqual (10, test.Prop, "3");
553 public void CanResetTest_private ()
555 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
556 CanReset_private_test test = new CanReset_private_test ();
558 Assert.IsTrue (p.CanResetValue (test), "1");
559 Assert.AreEqual (5, test.Prop, "2");
561 Assert.AreEqual (10, test.Prop, "3");
565 public void CanResetTestNoSetterTest ()
567 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
568 CanResetNoSetter_test test = new CanResetNoSetter_test ();
571 Assert.IsFalse (p.CanResetValue (test), "1");
573 Assert.IsTrue (p.CanResetValue (test), "1");
575 Assert.AreEqual (5, test.Prop, "2");
577 Assert.AreEqual (10, test.Prop, "3");
581 public void NoSerializeOrResetPropTest ()
583 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
584 NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
586 Assert.IsFalse (p.CanResetValue (test), "1");
587 Assert.IsFalse (p.ShouldSerializeValue (test), "2");
591 public void ShouldSerializeFalseEffectOnCanResetTest ()
593 PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
594 ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
596 Assert.IsFalse (p.ShouldSerializeValue (test), "1");
597 Assert.IsFalse (p.CanResetValue (test), "2");
601 public void ReadOnlyPropertyTest ()
603 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
604 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
608 public void ReadOnlyAttributeTest ()
610 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
611 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
615 public void ReadOnlyConflictingTest ()
617 PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
618 Assert.IsTrue (col["Prop"].IsReadOnly, "1");
622 public void DisplayNameTest ()
624 PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Explicit"];
625 PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Implicit"];
628 Assert.AreEqual ("An explicit displayname", p1.DisplayName, "#1");
630 Assert.AreEqual ("Explicit", p1.DisplayName, "#1");
632 Assert.AreEqual ("Implicit", p2.DisplayName, "#2");
636 public void GetEditorTest ()
638 PropertyDescriptorCollection col;
639 PropertyDescriptor pd;
642 col = TypeDescriptor.GetProperties (typeof (GetEditor_test));
644 ed = pd.GetEditor (typeof (UITypeEditor)) as UITypeEditor;
646 Assert.IsNotNull (ed, "#01");
647 Assert.AreEqual (ed.GetType ().Name, "UIEditor", "#02");
651 public void AddValueChanged ()
653 MockPropertyDescriptor pd = new MockPropertyDescriptor (
654 "Name", new Attribute [0]);
655 object compA = new object ();
656 object compB = new object ();
657 EventHandler handlerA = new EventHandler (ValueChanged1);
658 EventHandler handlerB = new EventHandler (ValueChanged1);
659 EventHandler handlerC = new EventHandler (ValueChanged2);
661 pd.AddValueChanged (compA, handlerA);
662 pd.AddValueChanged (compA, handlerC);
663 pd.AddValueChanged (compA, handlerC);
664 pd.AddValueChanged (compA, handlerB);
666 pd.FireValueChanged (compA, new EventArgs ());
667 Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
668 Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#A1");
669 Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#A2");
670 Assert.AreEqual ("ValueChanged2", _invokedHandlers [2], "#A3");
671 Assert.AreEqual ("ValueChanged1", _invokedHandlers [3], "#A4");
675 pd.FireValueChanged (compB, new EventArgs ());
676 Assert.AreEqual (0, _invokedHandlers.Count, "#B");
680 public void AddValueChanged_Component_Null ()
682 MockPropertyDescriptor pd = new MockPropertyDescriptor (
683 "Name", new Attribute [0]);
685 pd.AddValueChanged (null, new EventHandler (ValueChanged1));
687 } catch (ArgumentNullException ex) {
688 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
689 Assert.IsNull (ex.InnerException, "#3");
690 Assert.IsNotNull (ex.Message, "#4");
691 Assert.IsNotNull (ex.ParamName, "#5");
692 Assert.AreEqual ("component", ex.ParamName, "#6");
697 public void AddValueChanged_Handler_Null ()
699 MockPropertyDescriptor pd = new MockPropertyDescriptor (
700 "Name", new Attribute [0]);
702 pd.AddValueChanged (new object (), (EventHandler) null);
704 } catch (ArgumentNullException ex) {
705 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
706 Assert.IsNull (ex.InnerException, "#3");
707 Assert.IsNotNull (ex.Message, "#4");
708 Assert.IsNotNull (ex.ParamName, "#5");
709 Assert.AreEqual ("handler", ex.ParamName, "#6");
715 public void GetInvocationTarget_Instance_Null ()
717 MockPropertyDescriptor pd = new MockPropertyDescriptor (
718 "Name", new Attribute [0]);
720 pd.GetInvocationTarget (typeof (int), null);
722 } catch (ArgumentNullException ex) {
723 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
724 Assert.IsNull (ex.InnerException, "#3");
725 Assert.IsNotNull (ex.Message, "#4");
726 Assert.IsNotNull (ex.ParamName, "#5");
727 Assert.AreEqual ("instance", ex.ParamName, "#6");
732 public void GetInvocationTarget_Type_Null ()
734 MockPropertyDescriptor pd = new MockPropertyDescriptor (
735 "Name", new Attribute [0]);
737 pd.GetInvocationTarget ((Type) null, new object ());
739 } catch (ArgumentNullException ex) {
740 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
741 Assert.IsNull (ex.InnerException, "#3");
742 Assert.IsNotNull (ex.Message, "#4");
743 Assert.IsNotNull (ex.ParamName, "#5");
744 Assert.AreEqual ("type", ex.ParamName, "#6");
749 public void GetValueChangedHandler ()
751 object compA = new object ();
752 object compB = new object ();
753 EventHandler handlerA = new EventHandler (ValueChanged1);
754 EventHandler handlerB = new EventHandler (ValueChanged1);
755 EventHandler handlerC = new EventHandler (ValueChanged2);
757 MockPropertyDescriptor pd = new MockPropertyDescriptor (
758 "Name", new Attribute [0]);
759 Assert.IsNull (pd.GetValueChangedHandler (null), "#A1");
760 Assert.IsNull (pd.GetValueChangedHandler (compA), "#A2");
761 Assert.IsNull (pd.GetValueChangedHandler (compB), "#A3");
763 pd.AddValueChanged (compA, handlerA);
765 Assert.IsNull (pd.GetValueChangedHandler (null), "#B1");
766 Assert.AreSame (handlerA, pd.GetValueChangedHandler (compA), "#B2");
767 Assert.IsNull (pd.GetValueChangedHandler (compB), "#B3");
769 pd.AddValueChanged (compA, handlerB);
771 Assert.IsNull (pd.GetValueChangedHandler (null), "#C1");
772 EventHandler handler = pd.GetValueChangedHandler (compA);
773 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#C2");
774 Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#C3");
775 Assert.AreEqual (handlerB, handler.GetInvocationList () [1], "#C4");
776 Assert.IsNull (pd.GetValueChangedHandler (compB), "#C5");
778 pd.AddValueChanged (compB, handlerA);
780 Assert.IsNull (pd.GetValueChangedHandler (null), "#D1");
781 handler = pd.GetValueChangedHandler (compA);
782 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#D2");
783 Assert.AreSame (handlerA, pd.GetValueChangedHandler (compB), "#D3");
785 pd.RemoveValueChanged (compB, handlerB);
787 Assert.IsNull (pd.GetValueChangedHandler (null), "#E1");
788 handler = pd.GetValueChangedHandler (compA);
789 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#E2");
790 Assert.IsNull (pd.GetValueChangedHandler (compB), "#E3");
792 pd.RemoveValueChanged (compB, handlerB);
794 Assert.IsNull (pd.GetValueChangedHandler (null), "#F1");
795 handler = pd.GetValueChangedHandler (compA);
796 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#F2");
797 Assert.IsNull (pd.GetValueChangedHandler (compB), "#F3");
799 pd.RemoveValueChanged (compA, handlerC);
801 Assert.IsNull (pd.GetValueChangedHandler (null), "#G1");
802 handler = pd.GetValueChangedHandler (compA);
803 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#G2");
804 Assert.IsNull (pd.GetValueChangedHandler (compB), "#G3");
806 pd.AddValueChanged (compA, handlerC);
808 Assert.IsNull (pd.GetValueChangedHandler (null), "#H1");
809 handler = pd.GetValueChangedHandler (compA);
810 Assert.AreEqual (3, handler.GetInvocationList ().Length, "#H2");
811 Assert.IsNull (pd.GetValueChangedHandler (compB), "#H3");
813 pd.RemoveValueChanged (compA, handlerB);
815 Assert.IsNull (pd.GetValueChangedHandler (null), "#I1");
816 handler = pd.GetValueChangedHandler (compA);
817 Assert.AreEqual (2, handler.GetInvocationList ().Length, "#I2");
818 Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#I3");
819 Assert.AreEqual (handlerC, handler.GetInvocationList () [1], "#I4");
820 Assert.IsNull (pd.GetValueChangedHandler (compB), "#I5");
825 public void RemoveValueChanged ()
827 MockPropertyDescriptor pd = new MockPropertyDescriptor (
828 "Name", new Attribute [0]);
829 object compA = new object ();
830 object compB = new object ();
831 EventHandler handlerA = new EventHandler (ValueChanged1);
832 EventHandler handlerB = new EventHandler (ValueChanged1);
833 EventHandler handlerC = new EventHandler (ValueChanged2);
835 pd.AddValueChanged (compA, handlerA);
836 pd.AddValueChanged (compA, handlerC);
837 pd.AddValueChanged (compA, handlerC);
838 pd.AddValueChanged (compA, handlerB);
839 pd.AddValueChanged (compB, handlerC);
841 pd.FireValueChanged (compA, new EventArgs ());
842 Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
843 pd.RemoveValueChanged (new object (), handlerC);
844 pd.FireValueChanged (compA, new EventArgs ());
845 Assert.AreEqual (8, _invokedHandlers.Count, "#A2");
848 pd.RemoveValueChanged (compA, handlerC);
850 pd.FireValueChanged (compA, new EventArgs ());
851 Assert.AreEqual (3, _invokedHandlers.Count, "#B1");
852 Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#B2");
853 Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#B3");
854 Assert.AreEqual ("ValueChanged1", _invokedHandlers [2], "#B4");
858 pd.FireValueChanged (compB, new EventArgs ());
859 Assert.AreEqual (1, _invokedHandlers.Count, "#C1");
860 Assert.AreEqual ("ValueChanged2", _invokedHandlers [0], "#C2");
863 pd.RemoveValueChanged (compB, handlerC);
865 pd.FireValueChanged (compB, new EventArgs ());
866 Assert.AreEqual (0, _invokedHandlers.Count, "#D");
870 public void RemoveValueChanged_Component_Null ()
872 MockPropertyDescriptor pd = new MockPropertyDescriptor (
873 "Name", new Attribute [0]);
875 pd.RemoveValueChanged (null, new EventHandler (ValueChanged1));
877 } catch (ArgumentNullException ex) {
878 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
879 Assert.IsNull (ex.InnerException, "#3");
880 Assert.IsNotNull (ex.Message, "#4");
881 Assert.IsNotNull (ex.ParamName, "#5");
882 Assert.AreEqual ("component", ex.ParamName, "#6");
887 public void RemoveValueChanged_Handler_Null ()
889 MockPropertyDescriptor pd = new MockPropertyDescriptor (
890 "Name", new Attribute [0]);
892 pd.RemoveValueChanged (new object (), (EventHandler) null);
894 } catch (ArgumentNullException ex) {
895 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
896 Assert.IsNull (ex.InnerException, "#3");
897 Assert.IsNotNull (ex.Message, "#4");
898 Assert.IsNotNull (ex.ParamName, "#5");
899 Assert.AreEqual ("handler", ex.ParamName, "#6");
903 void ValueChanged1 (object sender, EventArgs e)
905 _invokedHandlers.Add ("ValueChanged1");
908 void ValueChanged2 (object sender, EventArgs e)
910 _invokedHandlers.Add ("ValueChanged2");
913 static Attribute FindAttribute (PropertyDescriptor pd, Type type)
915 foreach (Attribute attr in pd.Attributes)
916 if (attr.GetType () == type)
923 [Editor (typeof (UIEditor), typeof (UITypeEditor))]
924 public string Property {
925 get { return "abc"; }
930 class UIEditor : UITypeEditor
935 class MockPropertyDescriptor : PropertyDescriptor
937 public MockPropertyDescriptor (MemberDescriptor reference)
942 public MockPropertyDescriptor (MemberDescriptor reference, Attribute [] attrs)
943 : base (reference, attrs)
947 public MockPropertyDescriptor (string name, Attribute [] attrs)
952 public override Type ComponentType {
953 get { return typeof (int); }
956 public override bool IsReadOnly {
957 get { return false; }
960 public override Type PropertyType{
961 get { return typeof (DateTime); }
964 public override object GetValue (object component)
969 public override void SetValue (object component, object value)
973 public override void ResetValue (object component)
977 public override bool CanResetValue (object component)
982 public override bool ShouldSerializeValue (object component)
987 public void FireValueChanged (object component, EventArgs e)
989 base.OnValueChanged (component, e);
993 public new object GetInvocationTarget (Type type, object instance)
995 return base.GetInvocationTarget (type, instance);
998 public new EventHandler GetValueChangedHandler (object component)
1000 return base.GetValueChangedHandler (component);
1005 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
1006 public class PropTestAttribute : Attribute
1008 public PropTestAttribute ()
1013 public class TestBase
1016 public int PropBase1
1023 [Description ("whatever")]
1024 public string PropBase2
1031 public virtual string PropBase3
1038 public class TestSub : TestBase
1048 public string PropSub2
1054 public override string PropBase3