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