a24d330e32f2f076d25d5a0bd57bd1b0ead7d461
[mono.git] / mcs / class / System / Test / System.ComponentModel / PropertyDescriptorTests.cs
1 //
2 // System.ComponentModel.PropertyDescriptor test cases
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //      Gert Driesen (drieseng@users.sourceforge.net)
7 //
8 // (c) 2006 Novell, Inc. (http://www.novell.com/)
9 //
10
11 using System;
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;
19
20 using NUnit.Framework;
21
22 namespace MonoTests.System.ComponentModel
23 {
24         [TestFixture]
25         public class PropertyDescriptorTests
26         {
27                 class MissingConverterType_test
28                 {
29                         public class NestedClass { }
30
31                         [TypeConverter ("missing-type-name")]
32                         public NestedClass Prop {
33                                 get { return null; }
34                         }
35
36                         [TypeConverter ("missing-type-name")]
37                         public int IntProp {
38                                 get { return 5; }
39                         }
40
41                         [TypeConverter ("missing-type-name")]
42                         public string StringProp {
43                                 get { return ""; }
44                         }
45                 }
46
47                 class ReadOnlyProperty_test
48                 {
49                         public int Prop {
50                                 get { return 5; }
51                         }
52                 }
53
54                 class ReadOnlyAttribute_test
55                 {
56                         [ReadOnly (true)]
57                         public int Prop {
58                                 get { return 5; }
59                                 set { }
60                         }
61                 }
62
63                 class ConflictingReadOnly_test
64                 {
65                         [ReadOnly (false)]
66                         public int Prop {
67                                 get { return 5; }
68                         }
69                 }
70
71                 class ShouldSerialize_public_test
72                 {
73                         public int Prop {
74                                 get { return 5; }
75                         }
76
77                         public bool ShouldSerializeProp()
78                         {
79                                 return false;
80                         }
81                 }
82
83                 class ShouldSerialize_protected_test
84                 {
85                         public int Prop {
86                                 get { return 5; }
87                         }
88
89                         protected bool ShouldSerializeProp()
90                         {
91                                 return false;
92                         }
93                 }
94
95                 class ShouldSerialize_private_test
96                 {
97                         public int Prop {
98                                 get { return 5; }
99                         }
100
101                         private bool ShouldSerializeProp()
102                         {
103                                 return false;
104                         }
105                 }
106
107                 class ShouldSerializeFalseEffectOnCanReset_test
108                 {
109                         public int Prop {
110                                 get { return 5; }
111                                 set { }
112                         }
113
114                         public bool ShouldSerializeProp()
115                         {
116                                 return false;
117                         }
118
119                         public void ResetProp()
120                         {
121                         }
122                 }
123
124                 class ShouldSerialize_Null_Default
125                 {
126                         [DefaultValue (null)]
127                         public string Prop {
128                                 get { return _prop; }
129                                 set { _prop = value; }
130                         }
131
132                         public bool SerializeProp {
133                                 get { return _serializeProp; }
134                                 set { _serializeProp = value; }
135                         }
136
137                         public bool ShouldSerializeProp ()
138                         {
139                                 return _serializeProp;
140                         }
141
142                         private string _prop;
143                         private bool _serializeProp;
144                 }
145
146                 class ShouldSerialize_No_Default
147                 {
148                         public string Prop {
149                                 get { return _prop; }
150                                 set { _prop = value; }
151                         }
152
153                         private string _prop;
154                 }
155
156                 class ShouldSerialize_ReadOnly
157                 {
158                         [ReadOnly (true)]
159                         [DefaultValue ("ok")]
160                         public string Prop1 {
161                                 get { return _prop1; }
162                                 set { _prop1 = value; }
163                         }
164
165                         [ReadOnly (false)]
166                         public string Prop2 {
167                                 get { return _prop2; }
168                                 set { _prop2 = value; }
169                         }
170
171                         [ReadOnly (true)]
172                         public string Prop3 {
173                                 get { return _prop3; }
174                                 set { _prop3 = value; }
175                         }
176
177                         [ReadOnly (false)]
178                         public string Prop4 {
179                                 get { return _prop4; }
180                                 set { _prop4 = value; }
181                         }
182
183                         public string Prop5 {
184                                 get { return _prop5; }
185                         }
186
187                         [DefaultValue ("bad")]
188                         public string Prop6 {
189                                 get { return _prop6; }
190                         }
191
192                         [ReadOnly (true)]
193                         [DefaultValue ("good")]
194                         public string Prop7 {
195                                 get { return _prop7; }
196                                 set { _prop7 = value; }
197                         }
198
199                         [ReadOnly (true)]
200                         [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
201                         public string Prop8 {
202                                 get { return null; }
203                         }
204
205                         [ReadOnly (true)]
206                         [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
207                         public string Prop9 {
208                                 get { return null; }
209                         }
210
211                         public bool SerializeProp3 {
212                                 get { return _serializeProp3; }
213                                 set { _serializeProp3 = value; }
214                         }
215
216                         public bool SerializeProp4 {
217                                 get { return _serializeProp4; }
218                                 set { _serializeProp4 = value; }
219                         }
220
221                         public bool SerializeProp5 {
222                                 get { return _serializeProp5; }
223                                 set { _serializeProp5 = value; }
224                         }
225
226                         public bool SerializeProp6 {
227                                 get { return _serializeProp6; }
228                                 set { _serializeProp6 = value; }
229                         }
230
231                         public bool SerializeProp7 {
232                                 get { return _serializeProp7; }
233                                 set { _serializeProp7 = value; }
234                         }
235
236                         public bool ShouldSerializeProp3 ()
237                         {
238                                 return _serializeProp3;
239                         }
240
241                         public bool ShouldSerializeProp4 ()
242                         {
243                                 return _serializeProp4;
244                         }
245
246                         public bool ShouldSerializeProp5 ()
247                         {
248                                 return _serializeProp5;
249                         }
250
251                         public bool ShouldSerializeProp6 ()
252                         {
253                                 return _serializeProp6;
254                         }
255
256                         public bool ShouldSerializeProp7 ()
257                         {
258                                 return _serializeProp7;
259                         }
260
261                         public bool ShouldSerializeProp8 ()
262                         {
263                                 return false;
264                         }
265
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;
278                 }
279
280                 class NoSerializeOrResetProp_test
281                 {
282                         public int Prop {
283                                 get { return 5; }
284                         }
285                 }
286
287                 class CanReset_public_test
288                 {
289                         int prop = 5;
290                         public int Prop {
291                                 get { return prop; }
292                                 set { prop = value; }
293                         }
294
295                         public void ResetProp()
296                         {
297                                 prop = 10;
298                         }
299                 }
300
301                 class CanReset_protected_test
302                 {
303                         int prop = 5;
304                         public int Prop {
305                                 get { return prop; }
306                                 set { prop = value; }
307                         }
308
309                         protected void ResetProp()
310                         {
311                                 prop = 10;
312                         }
313                 }
314
315                 class CanReset_private_test
316                 {
317                         int prop = 5;
318                         public int Prop {
319                                 get { return prop; }
320                                 set { prop = value; }
321                         }
322
323                         private void ResetProp()
324                         {
325                                 prop = 10;
326                         }
327                 }
328
329                 class CanResetNoSetter_test
330                 {
331                         int prop = 5;
332                         public int Prop {
333                                 get { return prop; }
334                         }
335
336                         private void ResetProp()
337                         {
338                                 prop = 10;
339                         }
340                 }
341
342                 class DisplayName_test
343                 {
344 #if NET_2_0
345                         [DisplayName ("An explicit displayname")]
346 #endif
347                         public bool Explicit {
348                                 get { return false; }
349                         }
350
351                         public bool Implicit {
352                                 get { return false; }
353                         }
354                 }
355
356                 private ArrayList _invokedHandlers;
357
358                 [SetUp]
359                 public void SetUp ()
360                 {
361                         _invokedHandlers = new ArrayList ();
362                 }
363
364                 void Reset ()
365                 {
366                         _invokedHandlers.Clear ();
367                 }
368
369                 [Test]
370                 public void Attributes ()
371                 {
372                         PropertyDescriptorCollection properties;
373                         PropertyDescriptor pd;
374
375                         properties = TypeDescriptor.GetProperties (typeof (TestBase));
376
377                         pd = properties ["PropBase3"];
378                         Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#A1");
379                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#A2");
380
381                         pd = properties ["PropBase2"];
382                         Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#B1");
383                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#B2");
384
385                         pd = properties ["PropBase1"];
386                         Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#C1");
387                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#C2");
388
389                         properties = TypeDescriptor.GetProperties (typeof (TestSub));
390
391                         pd = properties ["PropBase3"];
392                         Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#D1");
393                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#D2");
394
395                         pd = properties ["PropBase2"];
396                         Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#E1");
397                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#E2");
398
399                         pd = properties ["PropBase1"];
400                         Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#F1");
401                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#F2");
402                 }
403
404                 [Test]
405                 public void MissingTypeConverter ()
406                 {
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"];
410
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");
414                 }
415
416                 [Test]
417                 public void ShouldSerializeTest_public ()
418                 {
419                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
420                         ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
421
422                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
423                 }
424
425                 [Test]
426                 public void ShouldSerializeTest_protected ()
427                 {
428                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
429                         ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
430
431                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
432                 }
433
434                 [Test]
435                 public void ShouldSerializeTest_private ()
436                 {
437                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
438                         ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
439
440                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
441                 }
442
443                 [Test]
444                 public void ShouldSerializeTest_No_Default ()
445                 {
446                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_No_Default)) ["Prop"];
447                         ShouldSerialize_No_Default test = new ShouldSerialize_No_Default ();
448
449                         Assert.IsTrue (p.ShouldSerializeValue (test), "#1");
450                         test.Prop = "whatever";
451                         Assert.IsTrue (p.ShouldSerializeValue (test), "#2");
452                 }
453
454                 [Test]
455                 public void ShouldSerializeTest_Null_Default ()
456                 {
457                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_Null_Default)) ["Prop"];
458                         ShouldSerialize_Null_Default test = new ShouldSerialize_Null_Default ();
459
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");
467                 }
468
469                 [Test]
470                 public void ShouldSerializeTest_ReadOnly ()
471                 {
472                         PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (
473                                 typeof (ShouldSerialize_ReadOnly));
474                         ShouldSerialize_ReadOnly test = new ShouldSerialize_ReadOnly ();
475
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"];
485
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");
493
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");
504
505                         test.Prop1 = "ok";
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");
515                         test.Prop7 = "good";
516                         Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#C6");
517                         test.SerializeProp7 = true;
518                         Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C7");
519                         test.Prop7 = "good";
520                         Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C8");
521
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");
526                 }
527
528                 [Test]
529                 public void CanResetTest_public ()
530                 {
531                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
532                         CanReset_public_test test = new CanReset_public_test ();
533
534                         Assert.IsTrue (p.CanResetValue (test), "1");
535                         Assert.AreEqual (5, test.Prop, "2");
536                         p.ResetValue (test);
537                         Assert.AreEqual (10, test.Prop, "3");
538                 }
539
540                 [Test]
541                 public void CanResetTest_protected ()
542                 {
543                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
544                         CanReset_protected_test test = new CanReset_protected_test ();
545
546                         Assert.IsTrue (p.CanResetValue (test), "1");
547                         Assert.AreEqual (5, test.Prop, "2");
548                         p.ResetValue (test);
549                         Assert.AreEqual (10, test.Prop, "3");
550                 }
551
552                 [Test]
553                 public void CanResetTest_private ()
554                 {
555                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
556                         CanReset_private_test test = new CanReset_private_test ();
557
558                         Assert.IsTrue (p.CanResetValue (test), "1");
559                         Assert.AreEqual (5, test.Prop, "2");
560                         p.ResetValue (test);
561                         Assert.AreEqual (10, test.Prop, "3");
562                 }
563
564                 [Test]
565                 public void CanResetTestNoSetterTest ()
566                 {
567                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
568                         CanResetNoSetter_test test = new CanResetNoSetter_test ();
569
570 #if NET_2_0
571                         Assert.IsFalse (p.CanResetValue (test), "1");
572 #else
573                         Assert.IsTrue (p.CanResetValue (test), "1");
574 #endif
575                         Assert.AreEqual (5, test.Prop, "2");
576                         p.ResetValue (test);
577                         Assert.AreEqual (10, test.Prop, "3");
578                 }
579
580                 [Test]
581                 public void NoSerializeOrResetPropTest ()
582                 {
583                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
584                         NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
585
586                         Assert.IsFalse (p.CanResetValue (test), "1");
587                         Assert.IsFalse (p.ShouldSerializeValue (test), "2");
588                 }
589
590                 [Test]
591                 public void ShouldSerializeFalseEffectOnCanResetTest ()
592                 {
593                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
594                         ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
595
596                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
597                         Assert.IsFalse (p.CanResetValue (test), "2");
598                 }
599
600                 [Test]
601                 public void ReadOnlyPropertyTest ()
602                 {
603                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
604                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
605                 }
606
607                 [Test]
608                 public void ReadOnlyAttributeTest ()
609                 {
610                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
611                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
612                 }
613
614                 [Test]
615                 public void ReadOnlyConflictingTest ()
616                 {
617                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
618                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
619                 }
620
621                 [Test] // bug #80292
622                 public void DisplayNameTest ()
623                 {
624                         PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Explicit"];
625                         PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Implicit"];
626
627 #if NET_2_0
628                         Assert.AreEqual ("An explicit displayname", p1.DisplayName, "#1");
629 #else
630                         Assert.AreEqual ("Explicit", p1.DisplayName, "#1");
631 #endif
632                         Assert.AreEqual ("Implicit", p2.DisplayName, "#2");
633                 }
634
635                 [Test]
636                 public void GetEditorTest ()
637                 {
638                         PropertyDescriptorCollection col;
639                         PropertyDescriptor pd;
640                         UITypeEditor ed;
641
642                         col = TypeDescriptor.GetProperties (typeof (GetEditor_test));
643                         pd = col [0];
644                         ed = pd.GetEditor (typeof (UITypeEditor)) as UITypeEditor;
645
646                         Assert.IsNotNull (ed, "#01");
647                         Assert.AreEqual (ed.GetType ().Name, "UIEditor", "#02");
648                 }
649
650                 [Test]
651                 public void AddValueChanged ()
652                 {
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);
660
661                         pd.AddValueChanged (compA, handlerA);
662                         pd.AddValueChanged (compA, handlerC);
663                         pd.AddValueChanged (compA, handlerC);
664                         pd.AddValueChanged (compA, handlerB);
665
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");
672
673                         Reset ();
674
675                         pd.FireValueChanged (compB, new EventArgs ());
676                         Assert.AreEqual (0, _invokedHandlers.Count, "#B");
677                 }
678
679                 [Test]
680                 public void AddValueChanged_Component_Null ()
681                 {
682                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
683                                 "Name", new Attribute [0]);
684                         try {
685                                 pd.AddValueChanged (null, new EventHandler (ValueChanged1));
686                                 Assert.Fail ("#1");
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");
693                         }
694                 }
695
696                 [Test]
697                 public void AddValueChanged_Handler_Null ()
698                 {
699                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
700                                 "Name", new Attribute [0]);
701                         try {
702                                 pd.AddValueChanged (new object (), (EventHandler) null);
703                                 Assert.Fail ("#1");
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");
710                         }
711                 }
712
713 #if NET_2_0
714                 [Test]
715                 public void GetInvocationTarget_Instance_Null ()
716                 {
717                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
718                                 "Name", new Attribute [0]);
719                         try {
720                                 pd.GetInvocationTarget (typeof (int), null);
721                                 Assert.Fail ("#1");
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");
728                         }
729                 }
730
731                 [Test]
732                 public void GetInvocationTarget_Type_Null ()
733                 {
734                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
735                                 "Name", new Attribute [0]);
736                         try {
737                                 pd.GetInvocationTarget ((Type) null, new object ());
738                                 Assert.Fail ("#1");
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");
745                         }
746                 }
747
748                 [Test]
749                 public void GetValueChangedHandler ()
750                 {
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);
756
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");
762
763                         pd.AddValueChanged (compA, handlerA);
764
765                         Assert.IsNull (pd.GetValueChangedHandler (null), "#B1");
766                         Assert.AreSame (handlerA, pd.GetValueChangedHandler (compA), "#B2");
767                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#B3");
768
769                         pd.AddValueChanged (compA, handlerB);
770
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");
777
778                         pd.AddValueChanged (compB, handlerA);
779
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");
784
785                         pd.RemoveValueChanged (compB, handlerB);
786
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");
791
792                         pd.RemoveValueChanged (compB, handlerB);
793
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");
798
799                         pd.RemoveValueChanged (compA, handlerC);
800
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");
805
806                         pd.AddValueChanged (compA, handlerC);
807
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");
812
813                         pd.RemoveValueChanged (compA, handlerB);
814
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");
821                 }
822 #endif
823
824                 [Test]
825                 public void RemoveValueChanged ()
826                 {
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);
834
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);
840
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");
846
847                         Reset ();
848                         pd.RemoveValueChanged (compA, handlerC);
849
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");
855
856                         Reset ();
857
858                         pd.FireValueChanged (compB, new EventArgs ());
859                         Assert.AreEqual (1, _invokedHandlers.Count, "#C1");
860                         Assert.AreEqual ("ValueChanged2", _invokedHandlers [0], "#C2");
861
862                         Reset ();
863                         pd.RemoveValueChanged (compB, handlerC);
864
865                         pd.FireValueChanged (compB, new EventArgs ());
866                         Assert.AreEqual (0, _invokedHandlers.Count, "#D");
867                 }
868
869                 [Test]
870                 public void RemoveValueChanged_Component_Null ()
871                 {
872                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
873                                 "Name", new Attribute [0]);
874                         try {
875                                 pd.RemoveValueChanged (null, new EventHandler (ValueChanged1));
876                                 Assert.Fail ("#1");
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");
883                         }
884                 }
885
886                 [Test]
887                 public void RemoveValueChanged_Handler_Null ()
888                 {
889                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
890                                 "Name", new Attribute [0]);
891                         try {
892                                 pd.RemoveValueChanged (new object (), (EventHandler) null);
893                                 Assert.Fail ("#1");
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");
900                         }
901                 }
902
903                 void ValueChanged1 (object sender, EventArgs e)
904                 {
905                         _invokedHandlers.Add ("ValueChanged1");
906                 }
907
908                 void ValueChanged2 (object sender, EventArgs e)
909                 {
910                         _invokedHandlers.Add ("ValueChanged2");
911                 }
912
913                 static Attribute FindAttribute (PropertyDescriptor pd, Type type)
914                 {
915                         foreach (Attribute attr in pd.Attributes)
916                                 if (attr.GetType () == type)
917                                         return attr;
918                         return null;
919                 }
920
921                 class GetEditor_test 
922                 {
923                         [Editor (typeof (UIEditor), typeof (UITypeEditor))]
924                         public string Property {
925                                 get { return "abc"; }
926                                 set { }
927                         }
928                 }
929
930                 class UIEditor : UITypeEditor
931                 {
932                         
933                 }
934
935                 class MockPropertyDescriptor : PropertyDescriptor
936                 {
937                         public MockPropertyDescriptor (MemberDescriptor reference)
938                                 : base (reference)
939                         {
940                         }
941
942                         public MockPropertyDescriptor (MemberDescriptor reference, Attribute [] attrs)
943                                 : base (reference, attrs)
944                         {
945                         }
946
947                         public MockPropertyDescriptor (string name, Attribute [] attrs)
948                                 : base (name, attrs)
949                         {
950                         }
951
952                         public override Type ComponentType {
953                                 get { return typeof (int); }
954                         }
955
956                         public override bool IsReadOnly {
957                                 get { return false; }
958                         }
959
960                         public override Type PropertyType{
961                                 get { return typeof (DateTime); }
962                         }
963
964                         public override object GetValue (object component)
965                         {
966                                 return null;
967                         }
968
969                         public override void SetValue (object component, object value)
970                         {
971                         }
972
973                         public override void ResetValue (object component)
974                         {
975                         }
976
977                         public override bool CanResetValue (object component)
978                         {
979                                 return true;
980                         }
981
982                         public override bool ShouldSerializeValue (object component)
983                         {
984                                 return true;
985                         }
986
987                         public void FireValueChanged (object component, EventArgs e)
988                         {
989                                 base.OnValueChanged (component, e);
990                         }
991
992 #if NET_2_0
993                         public new object GetInvocationTarget (Type type, object instance)
994                         {
995                                 return base.GetInvocationTarget (type, instance);
996                         }
997
998                         public new EventHandler GetValueChangedHandler (object component)
999                         {
1000                                 return base.GetValueChangedHandler (component);
1001                         }
1002 #endif
1003                 }
1004
1005                 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
1006                 public class PropTestAttribute : Attribute
1007                 {
1008                         public PropTestAttribute ()
1009                         {
1010                         }
1011                 }
1012
1013                 public class TestBase
1014                 {
1015                         [PropTest]
1016                         public int PropBase1
1017                         {
1018                                 get { return 0; }
1019                                 set { }
1020                         }
1021
1022                         [PropTest]
1023                         [Description ("whatever")]
1024                         public string PropBase2
1025                         {
1026                                 get { return ""; }
1027                                 set { }
1028                         }
1029
1030                         [PropTest]
1031                         public virtual string PropBase3
1032                         {
1033                                 get { return ""; }
1034                                 set { }
1035                         }
1036                 }
1037
1038                 public class TestSub : TestBase
1039                 {
1040                         [PropTest]
1041                         public int PropSub1
1042                         {
1043                                 get { return 0; }
1044                                 set { }
1045                         }
1046
1047                         [PropTest]
1048                         public string PropSub2
1049                         {
1050                                 get { return ""; }
1051                                 set { }
1052                         }
1053
1054                         public override string PropBase3
1055                         {
1056                                 get { return ""; }
1057                                 set { }
1058                         }
1059                 }
1060         }
1061 }