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