2010-05-21 Sebastien Pouliot <sebastien@ximian.com>
[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 DescriptionAttribute = System.ComponentModel.DescriptionAttribute;
15 using System.ComponentModel.Design;
16 using System.Drawing.Design;
17 using System.Globalization;
18 using System.Reflection;
19 using System.Runtime.InteropServices;
20
21 using NUnit.Framework;
22
23 namespace MonoTests.System.ComponentModel
24 {
25         internal class MyVersionTypeConverter : TypeConverter
26         {
27         }
28
29         class VirtPropParent
30         {
31                 string _someProperty;
32
33                 public virtual string SomeProperty {
34                         get { return _someProperty; }
35                         set { _someProperty = value; }
36                 }
37         }
38
39         class VirtPropChildNoSetter : VirtPropParent
40         {
41                 public override string SomeProperty {
42                         get { return base.SomeProperty + ": modified"; }
43                 }
44         }
45
46         class VirtPropChildNoGetter : VirtPropParent
47         {
48                 public override string SomeProperty {
49                         get { return base.SomeProperty + ": modified"; }
50                 }
51         }
52
53
54         [TestFixture]
55         public class PropertyDescriptorTests
56         {
57                 class MissingConverterType_test
58                 {
59                         public class NestedClass { }
60
61                         [TypeConverter ("missing-type-name")]
62                         public NestedClass Prop {
63                                 get { return null; }
64                         }
65
66                         [TypeConverter ("missing-type-name")]
67                         public int IntProp {
68                                 get { return 5; }
69                         }
70
71                         [TypeConverter ("missing-type-name")]
72                         public string StringProp {
73                                 get { return ""; }
74                         }
75                 }
76
77                 class ReadOnlyProperty_test
78                 {
79                         public int Prop {
80                                 get { return 5; }
81                         }
82                 }
83
84                 class ReadOnlyAttribute_test
85                 {
86                         [ReadOnly (true)]
87                         public int Prop {
88                                 get { return 5; }
89                                 set { }
90                         }
91                 }
92
93                 class ConflictingReadOnly_test
94                 {
95                         [ReadOnly (false)]
96                         public int Prop {
97                                 get { return 5; }
98                         }
99                 }
100
101                 class ShouldSerialize_public_test
102                 {
103                         public int Prop {
104                                 get { return 5; }
105                         }
106
107                         public bool ShouldSerializeProp()
108                         {
109                                 return false;
110                         }
111                 }
112
113                 class ShouldSerialize_protected_test
114                 {
115                         public int Prop {
116                                 get { return 5; }
117                         }
118
119                         protected bool ShouldSerializeProp()
120                         {
121                                 return false;
122                         }
123                 }
124
125                 class ShouldSerialize_private_test
126                 {
127                         public int Prop {
128                                 get { return 5; }
129                         }
130
131                         private bool ShouldSerializeProp()
132                         {
133                                 return false;
134                         }
135                 }
136
137                 class ShouldSerializeFalseEffectOnCanReset_test
138                 {
139                         public int Prop {
140                                 get { return 5; }
141                                 set { }
142                         }
143
144                         public bool ShouldSerializeProp()
145                         {
146                                 return false;
147                         }
148
149                         public void ResetProp()
150                         {
151                         }
152                 }
153
154                 class ShouldSerialize_Null_Default
155                 {
156                         [DefaultValue (null)]
157                         public string Prop {
158                                 get { return _prop; }
159                                 set { _prop = value; }
160                         }
161
162                         public bool SerializeProp {
163                                 get { return _serializeProp; }
164                                 set { _serializeProp = value; }
165                         }
166
167                         public bool ShouldSerializeProp ()
168                         {
169                                 return _serializeProp;
170                         }
171
172                         private string _prop;
173                         private bool _serializeProp;
174                 }
175
176                 class ShouldSerialize_No_Default
177                 {
178                         public string Prop {
179                                 get { return _prop; }
180                                 set { _prop = value; }
181                         }
182
183                         private string _prop;
184                 }
185
186                 class ShouldSerialize_ReadOnly
187                 {
188                         [ReadOnly (true)]
189                         [DefaultValue ("ok")]
190                         public string Prop1 {
191                                 get { return _prop1; }
192                                 set { _prop1 = value; }
193                         }
194
195                         [ReadOnly (false)]
196                         public string Prop2 {
197                                 get { return _prop2; }
198                                 set { _prop2 = value; }
199                         }
200
201                         [ReadOnly (true)]
202                         public string Prop3 {
203                                 get { return _prop3; }
204                                 set { _prop3 = value; }
205                         }
206
207                         [ReadOnly (false)]
208                         public string Prop4 {
209                                 get { return _prop4; }
210                                 set { _prop4 = value; }
211                         }
212
213                         public string Prop5 {
214                                 get { return _prop5; }
215                         }
216
217                         [DefaultValue ("bad")]
218                         public string Prop6 {
219                                 get { return _prop6; }
220                         }
221
222                         [ReadOnly (true)]
223                         [DefaultValue ("good")]
224                         public string Prop7 {
225                                 get { return _prop7; }
226                                 set { _prop7 = value; }
227                         }
228
229                         [ReadOnly (true)]
230                         [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
231                         public string Prop8 {
232                                 get { return null; }
233                         }
234
235                         [ReadOnly (true)]
236                         [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
237                         public string Prop9 {
238                                 get { return null; }
239                         }
240
241                         public bool SerializeProp3 {
242                                 get { return _serializeProp3; }
243                                 set { _serializeProp3 = value; }
244                         }
245
246                         public bool SerializeProp4 {
247                                 get { return _serializeProp4; }
248                                 set { _serializeProp4 = value; }
249                         }
250
251                         public bool SerializeProp5 {
252                                 get { return _serializeProp5; }
253                                 set { _serializeProp5 = value; }
254                         }
255
256                         public bool SerializeProp6 {
257                                 get { return _serializeProp6; }
258                                 set { _serializeProp6 = value; }
259                         }
260
261                         public bool SerializeProp7 {
262                                 get { return _serializeProp7; }
263                                 set { _serializeProp7 = value; }
264                         }
265
266                         public bool ShouldSerializeProp3 ()
267                         {
268                                 return _serializeProp3;
269                         }
270
271                         public bool ShouldSerializeProp4 ()
272                         {
273                                 return _serializeProp4;
274                         }
275
276                         public bool ShouldSerializeProp5 ()
277                         {
278                                 return _serializeProp5;
279                         }
280
281                         public bool ShouldSerializeProp6 ()
282                         {
283                                 return _serializeProp6;
284                         }
285
286                         public bool ShouldSerializeProp7 ()
287                         {
288                                 return _serializeProp7;
289                         }
290
291                         public bool ShouldSerializeProp8 ()
292                         {
293                                 return false;
294                         }
295
296                         private string _prop1;
297                         private string _prop2;
298                         private string _prop3;
299                         private string _prop4;
300                         private string _prop5 = "good";
301                         private string _prop6 = "bad";
302                         private string _prop7;
303                         private bool _serializeProp3;
304                         private bool _serializeProp4;
305                         private bool _serializeProp5;
306                         private bool _serializeProp6;
307                         private bool _serializeProp7;
308                 }
309
310                 class NoSerializeOrResetProp_test
311                 {
312                         public int Prop {
313                                 get { return 5; }
314                         }
315                 }
316
317                 class CanReset_public_test
318                 {
319                         int prop = 5;
320                         public int Prop {
321                                 get { return prop; }
322                                 set { prop = value; }
323                         }
324
325                         public void ResetProp()
326                         {
327                                 prop = 10;
328                         }
329                 }
330
331                 class CanReset_protected_test
332                 {
333                         int prop = 5;
334                         public int Prop {
335                                 get { return prop; }
336                                 set { prop = value; }
337                         }
338
339                         protected void ResetProp()
340                         {
341                                 prop = 10;
342                         }
343                 }
344
345                 class CanReset_private_test
346                 {
347                         int prop = 5;
348                         public int Prop {
349                                 get { return prop; }
350                                 set { prop = value; }
351                         }
352
353                         private void ResetProp()
354                         {
355                                 prop = 10;
356                         }
357                 }
358
359                 class CanResetNoSetter_test
360                 {
361                         int prop = 5;
362                         public int Prop {
363                                 get { return prop; }
364                         }
365
366                         private void ResetProp()
367                         {
368                                 prop = 10;
369                         }
370                 }
371
372                 class DisplayName_test
373                 {
374 #if NET_2_0
375                         [DisplayName ("An explicit displayname")]
376 #endif
377                         public bool Explicit {
378                                 get { return false; }
379                         }
380
381                         public bool Implicit {
382                                 get { return false; }
383                         }
384                 }
385
386                 class Converter_test
387                 {
388                         public virtual Version NoConverter {
389                                 get { return null; }
390                         }
391
392                         [TypeConverter (typeof(MyVersionTypeConverter))]
393                         public virtual Version WithConverter {
394                                 get { return null; }
395                         }
396
397                         [TypeConverter ("MonoTests.System.ComponentModel.MyVersionTypeConverter")]
398                         public virtual Version WithConverterNamed {
399                                 get { return null; }
400                         }
401
402                         [TypeConverter("System.ComponentModel.CharConverter, " + Consts.AssemblySystem)]
403                         public virtual Version WithConverterNamedAssmQuald {
404                                 get { return null; }
405                         }
406
407                         public int WithDefaultConverter {
408                                 get { return 0; }
409                         }
410                 }
411                 
412                 class ConverterSubclassNotOverridenProperties_test : Converter_test
413                 { 
414                 }
415                 
416                 class ConverterSubclassOverridenProperties_test : Converter_test
417                 {
418                         public override Version WithConverter {
419                                 get { return null; }
420                         }
421
422                         public override Version WithConverterNamed {
423                                 get { return null; }
424                         }
425                 }
426
427                 class ConverterEmptyConvertersOnOveriddenProperties : Converter_test
428                 {
429                         [TypeConverter]
430                         public override Version WithConverter {
431                                 get { return null; }
432                         }
433
434                         [TypeConverter]
435                         public override Version WithConverterNamed {
436                                 get { return null; }
437                         }
438                 }
439                 
440                 private ArrayList _invokedHandlers;
441
442                 [SetUp]
443                 public void SetUp ()
444                 {
445                         _invokedHandlers = new ArrayList ();
446                 }
447
448                 void Reset ()
449                 {
450                         _invokedHandlers.Clear ();
451                 }
452
453                 [Test]
454                 public void Attributes ()
455                 {
456                         PropertyDescriptorCollection properties;
457                         PropertyDescriptor pd;
458
459                         properties = TypeDescriptor.GetProperties (typeof (TestBase));
460
461                         pd = properties ["PropBase3"];
462                         Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#A1");
463                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#A2");
464
465                         pd = properties ["PropBase2"];
466                         Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#B1");
467                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#B2");
468
469                         pd = properties ["PropBase1"];
470                         Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#C1");
471                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#C2");
472
473                         properties = TypeDescriptor.GetProperties (typeof (TestSub));
474
475                         pd = properties ["PropBase3"];
476                         Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#D1");
477                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#D2");
478
479                         pd = properties ["PropBase2"];
480                         Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#E1");
481                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#E2");
482
483                         pd = properties ["PropBase1"];
484                         Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#F1");
485                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#F2");
486                 }
487
488                 [Test]
489                 public void VirtualPropertyDontOverrideSetter ()
490                 {
491                         VirtPropChildNoSetter c = new VirtPropChildNoSetter ();
492                         PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties (c);
493                         foreach (PropertyDescriptor pd in pdc) {
494                                 if (pd.Name != "SomeProperty")
495                                         continue;
496                                 pd.SetValue (c, "testing2");
497                                 pd.GetValue (c);
498                         }
499                 }
500
501                 [Test]
502                 public void VirtualPropertyDontOverrideGetter ()
503                 {
504                         VirtPropChildNoGetter c = new VirtPropChildNoGetter ();
505                         PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties (c);
506                         foreach (PropertyDescriptor pd in pdc) {
507                                 if (pd.Name != "SomeProperty")
508                                         continue;
509                                 pd.SetValue (c, "testing2");
510                                 pd.GetValue (c);
511                         }
512                 }
513
514                 [Test]
515                 public void MissingTypeConverter ()
516                 {
517                         PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["Prop"];
518                         PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["IntProp"];
519                         PropertyDescriptor p3 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["StringProp"];
520
521                         Assert.AreEqual (typeof (TypeConverter), p1.Converter.GetType (), "1");
522                         Assert.AreEqual (typeof (Int32Converter), p2.Converter.GetType (), "2");
523                         Assert.AreEqual (typeof (StringConverter), p3.Converter.GetType (), "3");
524                 }
525
526                 [Test]
527                 public void ConverterTest ()
528                 {
529                         Assert.AreEqual (typeof (TypeConverter), 
530                                          TypeDescriptor.GetProperties (typeof (Converter_test))["NoConverter"].Converter.GetType (), "#1");
531                         Assert.AreEqual (typeof (MyVersionTypeConverter), 
532                                          TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverter"].Converter.GetType (), "#2");
533                         Assert.AreEqual (typeof (MyVersionTypeConverter), 
534                                          TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverterNamed"].Converter.GetType (), "#3");
535                         Assert.AreEqual (typeof (CharConverter), 
536                                          TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#4");
537                         Assert.AreEqual (typeof (Int32Converter), 
538                                          TypeDescriptor.GetProperties (typeof (Converter_test))["WithDefaultConverter"].Converter.GetType (), "#5");
539
540                         Assert.AreEqual (typeof (TypeConverter), 
541                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["NoConverter"].Converter.GetType (), "#6");
542                         Assert.AreEqual (typeof (MyVersionTypeConverter), 
543                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverter"].Converter.GetType (), "#7");
544                         Assert.AreEqual (typeof (MyVersionTypeConverter), 
545                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverterNamed"].Converter.GetType (), "#8");
546                         Assert.AreEqual (typeof (CharConverter), 
547                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#9");
548                         Assert.AreEqual (typeof (Int32Converter), 
549                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithDefaultConverter"].Converter.GetType (), "#10");
550
551                         Assert.AreEqual (typeof (TypeConverter), 
552                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["NoConverter"].Converter.GetType (), "#11");
553                         Assert.AreEqual (typeof (MyVersionTypeConverter), 
554                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverter"].Converter.GetType (), "#12");
555                         Assert.AreEqual (typeof (MyVersionTypeConverter), 
556                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverterNamed"].Converter.GetType (), "#13");
557                         Assert.AreEqual (typeof (CharConverter), 
558                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#14");
559                         Assert.AreEqual (typeof (Int32Converter), 
560                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithDefaultConverter"].Converter.GetType (), "#15");
561
562                         Assert.AreEqual (typeof (TypeConverter), 
563                                          TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["NoConverter"].Converter.GetType (), "#116");
564                         Assert.AreEqual (typeof (TypeConverter), 
565                                          TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverter"].Converter.GetType (), "#17");
566                         Assert.AreEqual (typeof (TypeConverter), 
567                                          TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverterNamed"].Converter.GetType (), "#18");
568                         Assert.AreEqual (typeof (CharConverter), 
569                                          TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverterNamedAssmQuald"].Converter.GetType (), "#19");
570                         Assert.AreEqual (typeof (Int32Converter), 
571                                          TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithDefaultConverter"].Converter.GetType (), "#20");
572                 }
573
574                 [Test]
575                 public void ShouldSerializeTest_public ()
576                 {
577                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
578                         ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
579
580                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
581                 }
582
583                 [Test]
584                 public void ShouldSerializeTest_protected ()
585                 {
586                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
587                         ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
588
589                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
590                 }
591
592                 [Test]
593                 public void ShouldSerializeTest_private ()
594                 {
595                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
596                         ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
597
598                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
599                 }
600
601                 [Test]
602                 public void ShouldSerializeTest_No_Default ()
603                 {
604                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_No_Default)) ["Prop"];
605                         ShouldSerialize_No_Default test = new ShouldSerialize_No_Default ();
606
607                         Assert.IsTrue (p.ShouldSerializeValue (test), "#1");
608                         test.Prop = "whatever";
609                         Assert.IsTrue (p.ShouldSerializeValue (test), "#2");
610                 }
611
612                 [Test]
613                 public void ShouldSerializeTest_Null_Default ()
614                 {
615                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_Null_Default)) ["Prop"];
616                         ShouldSerialize_Null_Default test = new ShouldSerialize_Null_Default ();
617
618                         Assert.IsFalse (p.ShouldSerializeValue (test), "#1");
619                         test.SerializeProp = true;
620                         Assert.IsFalse (p.ShouldSerializeValue (test), "#2");
621                         test.Prop = "whatever";
622                         Assert.IsTrue (p.ShouldSerializeValue (test), "#3");
623                         test.SerializeProp = false;
624                         Assert.IsTrue (p.ShouldSerializeValue (test), "#4");
625                 }
626
627                 [Test]
628                 public void ShouldSerializeTest_ReadOnly ()
629                 {
630                         PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (
631                                 typeof (ShouldSerialize_ReadOnly));
632                         ShouldSerialize_ReadOnly test = new ShouldSerialize_ReadOnly ();
633
634                         PropertyDescriptor prop1PD = properties ["Prop1"];
635                         PropertyDescriptor prop2PD = properties ["Prop2"];
636                         PropertyDescriptor prop3PD = properties ["Prop3"];
637                         PropertyDescriptor prop4PD = properties ["Prop4"];
638                         PropertyDescriptor prop5PD = properties ["Prop5"];
639                         PropertyDescriptor prop6PD = properties ["Prop6"];
640                         PropertyDescriptor prop7PD = properties ["Prop7"];
641                         PropertyDescriptor prop8PD = properties ["Prop8"];
642                         PropertyDescriptor prop9PD = properties ["Prop9"];
643
644                         Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#A1");
645                         Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#A2");
646                         Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#A3");
647                         Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#A4");
648                         Assert.IsFalse (prop5PD.ShouldSerializeValue (test), "#A5");
649                         Assert.IsFalse (prop6PD.ShouldSerializeValue (test), "#A6");
650                         Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#A7");
651
652                         test.Prop1 = "whatever";
653                         Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#B1");
654                         test.Prop2 = "whatever";
655                         Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#B2");
656                         test.Prop3 = "whatever";
657                         Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#B3");
658                         test.Prop4 = "whatever";
659                         Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#B4");
660                         test.Prop7 = "whatever";
661                         Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#B5");
662
663                         test.Prop1 = "ok";
664                         Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#C1");
665                         test.SerializeProp3 = true;
666                         Assert.IsTrue (prop3PD.ShouldSerializeValue (test), "#C2");
667                         test.SerializeProp4 = true;
668                         Assert.IsTrue (prop4PD.ShouldSerializeValue (test), "#C3");
669                         test.SerializeProp5 = true;
670                         Assert.IsTrue (prop5PD.ShouldSerializeValue (test), "#C4");
671                         test.SerializeProp6 = true;
672                         Assert.IsTrue (prop6PD.ShouldSerializeValue (test), "#C5");
673                         test.Prop7 = "good";
674                         Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#C6");
675                         test.SerializeProp7 = true;
676                         Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C7");
677                         test.Prop7 = "good";
678                         Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C8");
679
680                         // has both DesignerSerializationVisibility.Content and ShouldSerialize { return false }
681                         Assert.IsFalse (prop8PD.ShouldSerializeValue (test), "#D1");
682                         // has DesignerSerializationVisibility.Content, no ShouldSerialize
683                         Assert.IsTrue (prop9PD.ShouldSerializeValue (test), "#D2");
684                 }
685
686                 [Test]
687                 public void CanResetTest_public ()
688                 {
689                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
690                         CanReset_public_test test = new CanReset_public_test ();
691
692                         Assert.IsTrue (p.CanResetValue (test), "1");
693                         Assert.AreEqual (5, test.Prop, "2");
694                         p.ResetValue (test);
695                         Assert.AreEqual (10, test.Prop, "3");
696                 }
697
698                 [Test]
699                 public void CanResetTest_protected ()
700                 {
701                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
702                         CanReset_protected_test test = new CanReset_protected_test ();
703
704                         Assert.IsTrue (p.CanResetValue (test), "1");
705                         Assert.AreEqual (5, test.Prop, "2");
706                         p.ResetValue (test);
707                         Assert.AreEqual (10, test.Prop, "3");
708                 }
709
710                 [Test]
711                 public void CanResetTest_private ()
712                 {
713                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
714                         CanReset_private_test test = new CanReset_private_test ();
715
716                         Assert.IsTrue (p.CanResetValue (test), "1");
717                         Assert.AreEqual (5, test.Prop, "2");
718                         p.ResetValue (test);
719                         Assert.AreEqual (10, test.Prop, "3");
720                 }
721
722                 [Test]
723                 public void CanResetTestNoSetterTest ()
724                 {
725                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
726                         CanResetNoSetter_test test = new CanResetNoSetter_test ();
727
728 #if NET_2_0
729                         Assert.IsFalse (p.CanResetValue (test), "1");
730 #else
731                         Assert.IsTrue (p.CanResetValue (test), "1");
732 #endif
733                         Assert.AreEqual (5, test.Prop, "2");
734                         p.ResetValue (test);
735                         Assert.AreEqual (10, test.Prop, "3");
736                 }
737
738                 [Test]
739                 public void NoSerializeOrResetPropTest ()
740                 {
741                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
742                         NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
743
744                         Assert.IsFalse (p.CanResetValue (test), "1");
745                         Assert.IsFalse (p.ShouldSerializeValue (test), "2");
746                 }
747
748                 [Test]
749                 public void ShouldSerializeFalseEffectOnCanResetTest ()
750                 {
751                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
752                         ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
753
754                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
755                         Assert.IsFalse (p.CanResetValue (test), "2");
756                 }
757
758                 [Test]
759                 public void ReadOnlyPropertyTest ()
760                 {
761                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
762                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
763                 }
764
765                 [Test]
766                 public void ReadOnlyAttributeTest ()
767                 {
768                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
769                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
770                 }
771
772                 [Test]
773                 public void ReadOnlyConflictingTest ()
774                 {
775                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
776                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
777                 }
778
779                 [Test] // bug #80292
780                 public void DisplayNameTest ()
781                 {
782                         PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Explicit"];
783                         PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Implicit"];
784
785 #if NET_2_0
786                         Assert.AreEqual ("An explicit displayname", p1.DisplayName, "#1");
787 #else
788                         Assert.AreEqual ("Explicit", p1.DisplayName, "#1");
789 #endif
790                         Assert.AreEqual ("Implicit", p2.DisplayName, "#2");
791                 }
792
793                 [Test]
794                 public void GetEditorTest ()
795                 {
796                         PropertyDescriptorCollection col;
797                         PropertyDescriptor pd;
798                         UITypeEditor ed;
799
800                         col = TypeDescriptor.GetProperties (typeof (GetEditor_test));
801                         pd = col [0];
802                         ed = pd.GetEditor (typeof (UITypeEditor)) as UITypeEditor;
803
804                         Assert.IsNotNull (ed, "#01");
805                         Assert.AreEqual (ed.GetType ().Name, "UIEditor", "#02");
806                 }
807
808                 [Test]
809                 public void AddValueChanged ()
810                 {
811                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
812                                 "Name", new Attribute [0]);
813                         object compA = new object ();
814                         object compB = new object ();
815                         EventHandler handlerA = new EventHandler (ValueChanged1);
816                         EventHandler handlerB = new EventHandler (ValueChanged1);
817                         EventHandler handlerC = new EventHandler (ValueChanged2);
818
819                         pd.AddValueChanged (compA, handlerA);
820                         pd.AddValueChanged (compA, handlerC);
821                         pd.AddValueChanged (compA, handlerC);
822                         pd.AddValueChanged (compA, handlerB);
823
824                         pd.FireValueChanged (compA, new EventArgs ());
825                         Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
826                         Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#A1");
827                         Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#A2");
828                         Assert.AreEqual ("ValueChanged2", _invokedHandlers [2], "#A3");
829                         Assert.AreEqual ("ValueChanged1", _invokedHandlers [3], "#A4");
830
831                         Reset ();
832
833                         pd.FireValueChanged (compB, new EventArgs ());
834                         Assert.AreEqual (0, _invokedHandlers.Count, "#B");
835                 }
836
837                 [Test]
838                 public void AddValueChanged_Component_Null ()
839                 {
840                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
841                                 "Name", new Attribute [0]);
842                         try {
843                                 pd.AddValueChanged (null, new EventHandler (ValueChanged1));
844                                 Assert.Fail ("#1");
845                         } catch (ArgumentNullException ex) {
846                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
847                                 Assert.IsNull (ex.InnerException, "#3");
848                                 Assert.IsNotNull (ex.Message, "#4");
849                                 Assert.IsNotNull (ex.ParamName, "#5");
850                                 Assert.AreEqual ("component", ex.ParamName, "#6");
851                         }
852                 }
853
854                 [Test]
855                 public void AddValueChanged_Handler_Null ()
856                 {
857                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
858                                 "Name", new Attribute [0]);
859                         try {
860                                 pd.AddValueChanged (new object (), (EventHandler) null);
861                                 Assert.Fail ("#1");
862                         } catch (ArgumentNullException ex) {
863                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
864                                 Assert.IsNull (ex.InnerException, "#3");
865                                 Assert.IsNotNull (ex.Message, "#4");
866                                 Assert.IsNotNull (ex.ParamName, "#5");
867                                 Assert.AreEqual ("handler", ex.ParamName, "#6");
868                         }
869                 }
870
871 #if NET_2_0
872                 [Test]
873                 public void GetInvocationTarget_Instance_Null ()
874                 {
875                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
876                                 "Name", new Attribute [0]);
877                         try {
878                                 pd.GetInvocationTarget (typeof (int), null);
879                                 Assert.Fail ("#1");
880                         } catch (ArgumentNullException ex) {
881                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
882                                 Assert.IsNull (ex.InnerException, "#3");
883                                 Assert.IsNotNull (ex.Message, "#4");
884                                 Assert.IsNotNull (ex.ParamName, "#5");
885                                 Assert.AreEqual ("instance", ex.ParamName, "#6");
886                         }
887                 }
888
889                 [Test]
890                 public void GetInvocationTarget_Type_Null ()
891                 {
892                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
893                                 "Name", new Attribute [0]);
894                         try {
895                                 pd.GetInvocationTarget ((Type) null, new object ());
896                                 Assert.Fail ("#1");
897                         } catch (ArgumentNullException ex) {
898                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
899                                 Assert.IsNull (ex.InnerException, "#3");
900                                 Assert.IsNotNull (ex.Message, "#4");
901                                 Assert.IsNotNull (ex.ParamName, "#5");
902                                 Assert.AreEqual ("type", ex.ParamName, "#6");
903                         }
904                 }
905
906                 [Test]
907                 public void GetValueChangedHandler ()
908                 {
909                         object compA = new object ();
910                         object compB = new object ();
911                         EventHandler handlerA = new EventHandler (ValueChanged1);
912                         EventHandler handlerB = new EventHandler (ValueChanged1);
913                         EventHandler handlerC = new EventHandler (ValueChanged2);
914
915                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
916                                 "Name", new Attribute [0]);
917                         Assert.IsNull (pd.GetValueChangedHandler (null), "#A1");
918                         Assert.IsNull (pd.GetValueChangedHandler (compA), "#A2");
919                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#A3");
920
921                         pd.AddValueChanged (compA, handlerA);
922
923                         Assert.IsNull (pd.GetValueChangedHandler (null), "#B1");
924                         Assert.AreSame (handlerA, pd.GetValueChangedHandler (compA), "#B2");
925                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#B3");
926
927                         pd.AddValueChanged (compA, handlerB);
928
929                         Assert.IsNull (pd.GetValueChangedHandler (null), "#C1");
930                         EventHandler handler = pd.GetValueChangedHandler (compA);
931                         Assert.AreEqual (2, handler.GetInvocationList ().Length, "#C2");
932                         Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#C3");
933                         Assert.AreEqual (handlerB, handler.GetInvocationList () [1], "#C4");
934                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#C5");
935
936                         pd.AddValueChanged (compB, handlerA);
937
938                         Assert.IsNull (pd.GetValueChangedHandler (null), "#D1");
939                         handler = pd.GetValueChangedHandler (compA);
940                         Assert.AreEqual (2, handler.GetInvocationList ().Length, "#D2");
941                         Assert.AreSame (handlerA, pd.GetValueChangedHandler (compB), "#D3");
942
943                         pd.RemoveValueChanged (compB, handlerB);
944
945                         Assert.IsNull (pd.GetValueChangedHandler (null), "#E1");
946                         handler = pd.GetValueChangedHandler (compA);
947                         Assert.AreEqual (2, handler.GetInvocationList ().Length, "#E2");
948                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#E3");
949
950                         pd.RemoveValueChanged (compB, handlerB);
951
952                         Assert.IsNull (pd.GetValueChangedHandler (null), "#F1");
953                         handler = pd.GetValueChangedHandler (compA);
954                         Assert.AreEqual (2, handler.GetInvocationList ().Length, "#F2");
955                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#F3");
956
957                         pd.RemoveValueChanged (compA, handlerC);
958
959                         Assert.IsNull (pd.GetValueChangedHandler (null), "#G1");
960                         handler = pd.GetValueChangedHandler (compA);
961                         Assert.AreEqual (2, handler.GetInvocationList ().Length, "#G2");
962                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#G3");
963
964                         pd.AddValueChanged (compA, handlerC);
965
966                         Assert.IsNull (pd.GetValueChangedHandler (null), "#H1");
967                         handler = pd.GetValueChangedHandler (compA);
968                         Assert.AreEqual (3, handler.GetInvocationList ().Length, "#H2");
969                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#H3");
970
971                         pd.RemoveValueChanged (compA, handlerB);
972
973                         Assert.IsNull (pd.GetValueChangedHandler (null), "#I1");
974                         handler = pd.GetValueChangedHandler (compA);
975                         Assert.AreEqual (2, handler.GetInvocationList ().Length, "#I2");
976                         Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#I3");
977                         Assert.AreEqual (handlerC, handler.GetInvocationList () [1], "#I4");
978                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#I5");
979                 }
980 #endif
981
982                 [Test]
983                 public void RemoveValueChanged ()
984                 {
985                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
986                                 "Name", new Attribute [0]);
987                         object compA = new object ();
988                         object compB = new object ();
989                         EventHandler handlerA = new EventHandler (ValueChanged1);
990                         EventHandler handlerB = new EventHandler (ValueChanged1);
991                         EventHandler handlerC = new EventHandler (ValueChanged2);
992
993                         pd.AddValueChanged (compA, handlerA);
994                         pd.AddValueChanged (compA, handlerC);
995                         pd.AddValueChanged (compA, handlerC);
996                         pd.AddValueChanged (compA, handlerB);
997                         pd.AddValueChanged (compB, handlerC);
998
999                         pd.FireValueChanged (compA, new EventArgs ());
1000                         Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
1001                         pd.RemoveValueChanged (new object (), handlerC);
1002                         pd.FireValueChanged (compA, new EventArgs ());
1003                         Assert.AreEqual (8, _invokedHandlers.Count, "#A2");
1004
1005                         Reset ();
1006                         pd.RemoveValueChanged (compA, handlerC);
1007
1008                         pd.FireValueChanged (compA, new EventArgs ());
1009                         Assert.AreEqual (3, _invokedHandlers.Count, "#B1");
1010                         Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#B2");
1011                         Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#B3");
1012                         Assert.AreEqual ("ValueChanged1", _invokedHandlers [2], "#B4");
1013
1014                         Reset ();
1015
1016                         pd.FireValueChanged (compB, new EventArgs ());
1017                         Assert.AreEqual (1, _invokedHandlers.Count, "#C1");
1018                         Assert.AreEqual ("ValueChanged2", _invokedHandlers [0], "#C2");
1019
1020                         Reset ();
1021                         pd.RemoveValueChanged (compB, handlerC);
1022
1023                         pd.FireValueChanged (compB, new EventArgs ());
1024                         Assert.AreEqual (0, _invokedHandlers.Count, "#D");
1025                 }
1026
1027                 [Test]
1028                 public void RemoveValueChanged_Component_Null ()
1029                 {
1030                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
1031                                 "Name", new Attribute [0]);
1032                         try {
1033                                 pd.RemoveValueChanged (null, new EventHandler (ValueChanged1));
1034                                 Assert.Fail ("#1");
1035                         } catch (ArgumentNullException ex) {
1036                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1037                                 Assert.IsNull (ex.InnerException, "#3");
1038                                 Assert.IsNotNull (ex.Message, "#4");
1039                                 Assert.IsNotNull (ex.ParamName, "#5");
1040                                 Assert.AreEqual ("component", ex.ParamName, "#6");
1041                         }
1042                 }
1043
1044                 [Test]
1045                 public void RemoveValueChanged_Handler_Null ()
1046                 {
1047                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
1048                                 "Name", new Attribute [0]);
1049                         try {
1050                                 pd.RemoveValueChanged (new object (), (EventHandler) null);
1051                                 Assert.Fail ("#1");
1052                         } catch (ArgumentNullException ex) {
1053                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1054                                 Assert.IsNull (ex.InnerException, "#3");
1055                                 Assert.IsNotNull (ex.Message, "#4");
1056                                 Assert.IsNotNull (ex.ParamName, "#5");
1057                                 Assert.AreEqual ("handler", ex.ParamName, "#6");
1058                         }
1059                 }
1060
1061                 void ValueChanged1 (object sender, EventArgs e)
1062                 {
1063                         _invokedHandlers.Add ("ValueChanged1");
1064                 }
1065
1066                 void ValueChanged2 (object sender, EventArgs e)
1067                 {
1068                         _invokedHandlers.Add ("ValueChanged2");
1069                 }
1070
1071                 static Attribute FindAttribute (PropertyDescriptor pd, Type type)
1072                 {
1073                         foreach (Attribute attr in pd.Attributes)
1074                                 if (attr.GetType () == type)
1075                                         return attr;
1076                         return null;
1077                 }
1078
1079                 class GetEditor_test 
1080                 {
1081                         [Editor (typeof (UIEditor), typeof (UITypeEditor))]
1082                         public string Property {
1083                                 get { return "abc"; }
1084                                 set { }
1085                         }
1086                 }
1087
1088                 class UIEditor : UITypeEditor
1089                 {
1090                         
1091                 }
1092
1093                 class MockPropertyDescriptor : PropertyDescriptor
1094                 {
1095                         public MockPropertyDescriptor (MemberDescriptor reference)
1096                                 : base (reference)
1097                         {
1098                         }
1099
1100                         public MockPropertyDescriptor (MemberDescriptor reference, Attribute [] attrs)
1101                                 : base (reference, attrs)
1102                         {
1103                         }
1104
1105                         public MockPropertyDescriptor (string name, Attribute [] attrs)
1106                                 : base (name, attrs)
1107                         {
1108                         }
1109
1110                         public override Type ComponentType {
1111                                 get { return typeof (int); }
1112                         }
1113
1114                         public override bool IsReadOnly {
1115                                 get { return false; }
1116                         }
1117
1118                         public override Type PropertyType{
1119                                 get { return typeof (DateTime); }
1120                         }
1121
1122                         public override object GetValue (object component)
1123                         {
1124                                 return null;
1125                         }
1126
1127                         public override void SetValue (object component, object value)
1128                         {
1129                         }
1130
1131                         public override void ResetValue (object component)
1132                         {
1133                         }
1134
1135                         public override bool CanResetValue (object component)
1136                         {
1137                                 return true;
1138                         }
1139
1140                         public override bool ShouldSerializeValue (object component)
1141                         {
1142                                 return true;
1143                         }
1144
1145                         public void FireValueChanged (object component, EventArgs e)
1146                         {
1147                                 base.OnValueChanged (component, e);
1148                         }
1149
1150 #if NET_2_0
1151                         public new object GetInvocationTarget (Type type, object instance)
1152                         {
1153                                 return base.GetInvocationTarget (type, instance);
1154                         }
1155
1156                         public new EventHandler GetValueChangedHandler (object component)
1157                         {
1158                                 return base.GetValueChangedHandler (component);
1159                         }
1160 #endif
1161                 }
1162
1163                 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
1164                 public class PropTestAttribute : Attribute
1165                 {
1166                         public PropTestAttribute ()
1167                         {
1168                         }
1169                 }
1170
1171                 public class TestBase
1172                 {
1173                         [PropTest]
1174                         public int PropBase1
1175                         {
1176                                 get { return 0; }
1177                                 set { }
1178                         }
1179
1180                         [PropTest]
1181                         [Description ("whatever")]
1182                         public string PropBase2
1183                         {
1184                                 get { return ""; }
1185                                 set { }
1186                         }
1187
1188                         [PropTest]
1189                         public virtual string PropBase3
1190                         {
1191                                 get { return ""; }
1192                                 set { }
1193                         }
1194                 }
1195
1196                 public class TestSub : TestBase
1197                 {
1198                         [PropTest]
1199                         public int PropSub1
1200                         {
1201                                 get { return 0; }
1202                                 set { }
1203                         }
1204
1205                         [PropTest]
1206                         public string PropSub2
1207                         {
1208                                 get { return ""; }
1209                                 set { }
1210                         }
1211
1212                         public override string PropBase3
1213                         {
1214                                 get { return ""; }
1215                                 set { }
1216                         }
1217                 }
1218         }
1219 }