Merge pull request #1695 from gregoryyoung/master
[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 #elif NET_4_0
404                         [TypeConverter("System.ComponentModel.CharConverter, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
405 #else
406                         [TypeConverter("System.ComponentModel.CharConverter, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
407 #endif
408                         public virtual Version WithConverterNamedAssmQuald {
409                                 get { return null; }
410                         }
411
412                         public int WithDefaultConverter {
413                                 get { return 0; }
414                         }
415                 }
416                 
417                 class ConverterSubclassNotOverridenProperties_test : Converter_test
418                 { 
419                 }
420                 
421                 class ConverterSubclassOverridenProperties_test : Converter_test
422                 {
423                         public override Version WithConverter {
424                                 get { return null; }
425                         }
426
427                         public override Version WithConverterNamed {
428                                 get { return null; }
429                         }
430                 }
431
432                 class ConverterEmptyConvertersOnOveriddenProperties : Converter_test
433                 {
434                         [TypeConverter]
435                         public override Version WithConverter {
436                                 get { return null; }
437                         }
438
439                         [TypeConverter]
440                         public override Version WithConverterNamed {
441                                 get { return null; }
442                         }
443                 }
444                 
445                 private ArrayList _invokedHandlers;
446
447                 [SetUp]
448                 public void SetUp ()
449                 {
450                         _invokedHandlers = new ArrayList ();
451                 }
452
453                 void Reset ()
454                 {
455                         _invokedHandlers.Clear ();
456                 }
457
458                 [Test]
459                 public void Attributes ()
460                 {
461                         PropertyDescriptorCollection properties;
462                         PropertyDescriptor pd;
463
464                         properties = TypeDescriptor.GetProperties (typeof (TestBase));
465
466                         pd = properties ["PropBase3"];
467                         Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#A1");
468                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#A2");
469
470                         pd = properties ["PropBase2"];
471                         Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#B1");
472                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#B2");
473
474                         pd = properties ["PropBase1"];
475                         Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#C1");
476                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#C2");
477
478                         properties = TypeDescriptor.GetProperties (typeof (TestSub));
479
480                         pd = properties ["PropBase3"];
481                         Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#D1");
482                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#D2");
483
484                         pd = properties ["PropBase2"];
485                         Assert.IsNotNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#E1");
486                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#E2");
487
488                         pd = properties ["PropBase1"];
489                         Assert.IsNull (FindAttribute (pd, typeof (DescriptionAttribute)), "#F1");
490                         Assert.IsNotNull (FindAttribute (pd, typeof (PropTestAttribute)), "#F2");
491                 }
492
493                 [Test]
494                 public void VirtualPropertyDontOverrideSetter ()
495                 {
496                         VirtPropChildNoSetter c = new VirtPropChildNoSetter ();
497                         PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties (c);
498                         foreach (PropertyDescriptor pd in pdc) {
499                                 if (pd.Name != "SomeProperty")
500                                         continue;
501                                 pd.SetValue (c, "testing2");
502                                 pd.GetValue (c);
503                         }
504                 }
505
506                 [Test]
507                 public void VirtualPropertyDontOverrideGetter ()
508                 {
509                         VirtPropChildNoGetter c = new VirtPropChildNoGetter ();
510                         PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties (c);
511                         foreach (PropertyDescriptor pd in pdc) {
512                                 if (pd.Name != "SomeProperty")
513                                         continue;
514                                 pd.SetValue (c, "testing2");
515                                 pd.GetValue (c);
516                         }
517                 }
518
519                 [Test]
520                 public void MissingTypeConverter ()
521                 {
522                         PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["Prop"];
523                         PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["IntProp"];
524                         PropertyDescriptor p3 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["StringProp"];
525
526                         Assert.AreEqual (typeof (TypeConverter), p1.Converter.GetType (), "1");
527                         Assert.AreEqual (typeof (Int32Converter), p2.Converter.GetType (), "2");
528                         Assert.AreEqual (typeof (StringConverter), p3.Converter.GetType (), "3");
529                 }
530
531                 [Test]
532                 public void ConverterTest ()
533                 {
534                         Assert.AreEqual (typeof (TypeConverter), 
535                                          TypeDescriptor.GetProperties (typeof (Converter_test))["NoConverter"].Converter.GetType (), "#1");
536                         Assert.AreEqual (typeof (MyVersionTypeConverter), 
537                                          TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverter"].Converter.GetType (), "#2");
538                         Assert.AreEqual (typeof (MyVersionTypeConverter), 
539                                          TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverterNamed"].Converter.GetType (), "#3");
540                         Assert.AreEqual (typeof (CharConverter), 
541                                          TypeDescriptor.GetProperties (typeof (Converter_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#4");
542                         Assert.AreEqual (typeof (Int32Converter), 
543                                          TypeDescriptor.GetProperties (typeof (Converter_test))["WithDefaultConverter"].Converter.GetType (), "#5");
544
545                         Assert.AreEqual (typeof (TypeConverter), 
546                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["NoConverter"].Converter.GetType (), "#6");
547                         Assert.AreEqual (typeof (MyVersionTypeConverter), 
548                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverter"].Converter.GetType (), "#7");
549                         Assert.AreEqual (typeof (MyVersionTypeConverter), 
550                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverterNamed"].Converter.GetType (), "#8");
551                         Assert.AreEqual (typeof (CharConverter), 
552                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#9");
553                         Assert.AreEqual (typeof (Int32Converter), 
554                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassNotOverridenProperties_test))["WithDefaultConverter"].Converter.GetType (), "#10");
555
556                         Assert.AreEqual (typeof (TypeConverter), 
557                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["NoConverter"].Converter.GetType (), "#11");
558                         Assert.AreEqual (typeof (MyVersionTypeConverter), 
559                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverter"].Converter.GetType (), "#12");
560                         Assert.AreEqual (typeof (MyVersionTypeConverter), 
561                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverterNamed"].Converter.GetType (), "#13");
562                         Assert.AreEqual (typeof (CharConverter), 
563                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithConverterNamedAssmQuald"].Converter.GetType (), "#14");
564                         Assert.AreEqual (typeof (Int32Converter), 
565                                          TypeDescriptor.GetProperties (typeof (ConverterSubclassOverridenProperties_test))["WithDefaultConverter"].Converter.GetType (), "#15");
566
567                         Assert.AreEqual (typeof (TypeConverter), 
568                                          TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["NoConverter"].Converter.GetType (), "#116");
569                         Assert.AreEqual (typeof (TypeConverter), 
570                                          TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverter"].Converter.GetType (), "#17");
571                         Assert.AreEqual (typeof (TypeConverter), 
572                                          TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverterNamed"].Converter.GetType (), "#18");
573                         Assert.AreEqual (typeof (CharConverter), 
574                                          TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithConverterNamedAssmQuald"].Converter.GetType (), "#19");
575                         Assert.AreEqual (typeof (Int32Converter), 
576                                          TypeDescriptor.GetProperties (typeof (ConverterEmptyConvertersOnOveriddenProperties))["WithDefaultConverter"].Converter.GetType (), "#20");
577                 }
578
579                 [Test]
580                 public void ShouldSerializeTest_public ()
581                 {
582                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
583                         ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
584
585                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
586                 }
587
588                 [Test]
589                 public void ShouldSerializeTest_protected ()
590                 {
591                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
592                         ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
593
594                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
595                 }
596
597                 [Test]
598                 public void ShouldSerializeTest_private ()
599                 {
600                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
601                         ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
602
603                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
604                 }
605
606                 [Test]
607                 public void ShouldSerializeTest_No_Default ()
608                 {
609                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_No_Default)) ["Prop"];
610                         ShouldSerialize_No_Default test = new ShouldSerialize_No_Default ();
611
612                         Assert.IsTrue (p.ShouldSerializeValue (test), "#1");
613                         test.Prop = "whatever";
614                         Assert.IsTrue (p.ShouldSerializeValue (test), "#2");
615                 }
616
617                 [Test]
618                 public void ShouldSerializeTest_Null_Default ()
619                 {
620                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_Null_Default)) ["Prop"];
621                         ShouldSerialize_Null_Default test = new ShouldSerialize_Null_Default ();
622
623                         Assert.IsFalse (p.ShouldSerializeValue (test), "#1");
624                         test.SerializeProp = true;
625                         Assert.IsFalse (p.ShouldSerializeValue (test), "#2");
626                         test.Prop = "whatever";
627                         Assert.IsTrue (p.ShouldSerializeValue (test), "#3");
628                         test.SerializeProp = false;
629                         Assert.IsTrue (p.ShouldSerializeValue (test), "#4");
630                 }
631
632                 [Test]
633                 public void ShouldSerializeTest_ReadOnly ()
634                 {
635                         PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (
636                                 typeof (ShouldSerialize_ReadOnly));
637                         ShouldSerialize_ReadOnly test = new ShouldSerialize_ReadOnly ();
638
639                         PropertyDescriptor prop1PD = properties ["Prop1"];
640                         PropertyDescriptor prop2PD = properties ["Prop2"];
641                         PropertyDescriptor prop3PD = properties ["Prop3"];
642                         PropertyDescriptor prop4PD = properties ["Prop4"];
643                         PropertyDescriptor prop5PD = properties ["Prop5"];
644                         PropertyDescriptor prop6PD = properties ["Prop6"];
645                         PropertyDescriptor prop7PD = properties ["Prop7"];
646                         PropertyDescriptor prop8PD = properties ["Prop8"];
647                         PropertyDescriptor prop9PD = properties ["Prop9"];
648
649                         Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#A1");
650                         Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#A2");
651                         Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#A3");
652                         Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#A4");
653                         Assert.IsFalse (prop5PD.ShouldSerializeValue (test), "#A5");
654                         Assert.IsFalse (prop6PD.ShouldSerializeValue (test), "#A6");
655                         Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#A7");
656
657                         test.Prop1 = "whatever";
658                         Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#B1");
659                         test.Prop2 = "whatever";
660                         Assert.IsTrue (prop2PD.ShouldSerializeValue (test), "#B2");
661                         test.Prop3 = "whatever";
662                         Assert.IsFalse (prop3PD.ShouldSerializeValue (test), "#B3");
663                         test.Prop4 = "whatever";
664                         Assert.IsFalse (prop4PD.ShouldSerializeValue (test), "#B4");
665                         test.Prop7 = "whatever";
666                         Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#B5");
667
668                         test.Prop1 = "ok";
669                         Assert.IsFalse (prop1PD.ShouldSerializeValue (test), "#C1");
670                         test.SerializeProp3 = true;
671                         Assert.IsTrue (prop3PD.ShouldSerializeValue (test), "#C2");
672                         test.SerializeProp4 = true;
673                         Assert.IsTrue (prop4PD.ShouldSerializeValue (test), "#C3");
674                         test.SerializeProp5 = true;
675                         Assert.IsTrue (prop5PD.ShouldSerializeValue (test), "#C4");
676                         test.SerializeProp6 = true;
677                         Assert.IsTrue (prop6PD.ShouldSerializeValue (test), "#C5");
678                         test.Prop7 = "good";
679                         Assert.IsFalse (prop7PD.ShouldSerializeValue (test), "#C6");
680                         test.SerializeProp7 = true;
681                         Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C7");
682                         test.Prop7 = "good";
683                         Assert.IsTrue (prop7PD.ShouldSerializeValue (test), "#C8");
684
685                         // has both DesignerSerializationVisibility.Content and ShouldSerialize { return false }
686                         Assert.IsFalse (prop8PD.ShouldSerializeValue (test), "#D1");
687                         // has DesignerSerializationVisibility.Content, no ShouldSerialize
688                         Assert.IsTrue (prop9PD.ShouldSerializeValue (test), "#D2");
689                 }
690
691                 [Test]
692                 public void CanResetTest_public ()
693                 {
694                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
695                         CanReset_public_test test = new CanReset_public_test ();
696
697                         Assert.IsTrue (p.CanResetValue (test), "1");
698                         Assert.AreEqual (5, test.Prop, "2");
699                         p.ResetValue (test);
700                         Assert.AreEqual (10, test.Prop, "3");
701                 }
702
703                 [Test]
704                 public void CanResetTest_protected ()
705                 {
706                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
707                         CanReset_protected_test test = new CanReset_protected_test ();
708
709                         Assert.IsTrue (p.CanResetValue (test), "1");
710                         Assert.AreEqual (5, test.Prop, "2");
711                         p.ResetValue (test);
712                         Assert.AreEqual (10, test.Prop, "3");
713                 }
714
715                 [Test]
716                 public void CanResetTest_private ()
717                 {
718                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
719                         CanReset_private_test test = new CanReset_private_test ();
720
721                         Assert.IsTrue (p.CanResetValue (test), "1");
722                         Assert.AreEqual (5, test.Prop, "2");
723                         p.ResetValue (test);
724                         Assert.AreEqual (10, test.Prop, "3");
725                 }
726
727                 [Test]
728                 public void CanResetTestNoSetterTest ()
729                 {
730                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
731                         CanResetNoSetter_test test = new CanResetNoSetter_test ();
732
733                         Assert.IsFalse (p.CanResetValue (test), "1");
734                         Assert.AreEqual (5, test.Prop, "2");
735                         p.ResetValue (test);
736                         Assert.AreEqual (10, test.Prop, "3");
737                 }
738
739                 [Test]
740                 public void NoSerializeOrResetPropTest ()
741                 {
742                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
743                         NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
744
745                         Assert.IsFalse (p.CanResetValue (test), "1");
746                         Assert.IsFalse (p.ShouldSerializeValue (test), "2");
747                 }
748
749                 [Test]
750                 public void ShouldSerializeFalseEffectOnCanResetTest ()
751                 {
752                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
753                         ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
754
755                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
756                         Assert.IsFalse (p.CanResetValue (test), "2");
757                 }
758
759                 [Test]
760                 public void ReadOnlyPropertyTest ()
761                 {
762                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
763                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
764                 }
765
766                 [Test]
767                 public void ReadOnlyAttributeTest ()
768                 {
769                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
770                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
771                 }
772
773                 [Test]
774                 public void ReadOnlyConflictingTest ()
775                 {
776                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
777                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
778                 }
779
780                 [Test] // bug #80292
781                 public void DisplayNameTest ()
782                 {
783                         PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Explicit"];
784                         PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Implicit"];
785
786                         Assert.AreEqual ("An explicit displayname", p1.DisplayName, "#1");
787                         Assert.AreEqual ("Implicit", p2.DisplayName, "#2");
788                 }
789
790                 [Test]
791                 public void AddValueChanged ()
792                 {
793                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
794                                 "Name", new Attribute [0]);
795                         object compA = new object ();
796                         object compB = new object ();
797                         EventHandler handlerA = new EventHandler (ValueChanged1);
798                         EventHandler handlerB = new EventHandler (ValueChanged1);
799                         EventHandler handlerC = new EventHandler (ValueChanged2);
800
801                         pd.AddValueChanged (compA, handlerA);
802                         pd.AddValueChanged (compA, handlerC);
803                         pd.AddValueChanged (compA, handlerC);
804                         pd.AddValueChanged (compA, handlerB);
805
806                         pd.FireValueChanged (compA, new EventArgs ());
807                         Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
808                         Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#A1");
809                         Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#A2");
810                         Assert.AreEqual ("ValueChanged2", _invokedHandlers [2], "#A3");
811                         Assert.AreEqual ("ValueChanged1", _invokedHandlers [3], "#A4");
812
813                         Reset ();
814
815                         pd.FireValueChanged (compB, new EventArgs ());
816                         Assert.AreEqual (0, _invokedHandlers.Count, "#B");
817                 }
818
819                 [Test]
820                 public void AddValueChanged_Component_Null ()
821                 {
822                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
823                                 "Name", new Attribute [0]);
824                         try {
825                                 pd.AddValueChanged (null, new EventHandler (ValueChanged1));
826                                 Assert.Fail ("#1");
827                         } catch (ArgumentNullException ex) {
828                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
829                                 Assert.IsNull (ex.InnerException, "#3");
830                                 Assert.IsNotNull (ex.Message, "#4");
831                                 Assert.IsNotNull (ex.ParamName, "#5");
832                                 Assert.AreEqual ("component", ex.ParamName, "#6");
833                         }
834                 }
835
836                 [Test]
837                 public void AddValueChanged_Handler_Null ()
838                 {
839                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
840                                 "Name", new Attribute [0]);
841                         try {
842                                 pd.AddValueChanged (new object (), (EventHandler) null);
843                                 Assert.Fail ("#1");
844                         } catch (ArgumentNullException ex) {
845                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
846                                 Assert.IsNull (ex.InnerException, "#3");
847                                 Assert.IsNotNull (ex.Message, "#4");
848                                 Assert.IsNotNull (ex.ParamName, "#5");
849                                 Assert.AreEqual ("handler", ex.ParamName, "#6");
850                         }
851                 }
852
853                 [Test]
854                 public void GetInvocationTarget_Instance_Null ()
855                 {
856                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
857                                 "Name", new Attribute [0]);
858                         try {
859                                 pd.GetInvocationTarget (typeof (int), null);
860                                 Assert.Fail ("#1");
861                         } catch (ArgumentNullException ex) {
862                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
863                                 Assert.IsNull (ex.InnerException, "#3");
864                                 Assert.IsNotNull (ex.Message, "#4");
865                                 Assert.IsNotNull (ex.ParamName, "#5");
866                                 Assert.AreEqual ("instance", ex.ParamName, "#6");
867                         }
868                 }
869
870                 [Test]
871                 public void GetInvocationTarget_Type_Null ()
872                 {
873                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
874                                 "Name", new Attribute [0]);
875                         try {
876                                 pd.GetInvocationTarget ((Type) null, new object ());
877                                 Assert.Fail ("#1");
878                         } catch (ArgumentNullException ex) {
879                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
880                                 Assert.IsNull (ex.InnerException, "#3");
881                                 Assert.IsNotNull (ex.Message, "#4");
882                                 Assert.IsNotNull (ex.ParamName, "#5");
883                                 Assert.AreEqual ("type", ex.ParamName, "#6");
884                         }
885                 }
886
887                 [Test]
888                 public void GetValueChangedHandler ()
889                 {
890                         object compA = new object ();
891                         object compB = new object ();
892                         EventHandler handlerA = new EventHandler (ValueChanged1);
893                         EventHandler handlerB = new EventHandler (ValueChanged1);
894                         EventHandler handlerC = new EventHandler (ValueChanged2);
895
896                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
897                                 "Name", new Attribute [0]);
898                         Assert.IsNull (pd.GetValueChangedHandler (null), "#A1");
899                         Assert.IsNull (pd.GetValueChangedHandler (compA), "#A2");
900                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#A3");
901
902                         pd.AddValueChanged (compA, handlerA);
903
904                         Assert.IsNull (pd.GetValueChangedHandler (null), "#B1");
905                         Assert.AreSame (handlerA, pd.GetValueChangedHandler (compA), "#B2");
906                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#B3");
907
908                         pd.AddValueChanged (compA, handlerB);
909
910                         Assert.IsNull (pd.GetValueChangedHandler (null), "#C1");
911                         EventHandler handler = pd.GetValueChangedHandler (compA);
912                         Assert.AreEqual (2, handler.GetInvocationList ().Length, "#C2");
913                         Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#C3");
914                         Assert.AreEqual (handlerB, handler.GetInvocationList () [1], "#C4");
915                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#C5");
916
917                         pd.AddValueChanged (compB, handlerA);
918
919                         Assert.IsNull (pd.GetValueChangedHandler (null), "#D1");
920                         handler = pd.GetValueChangedHandler (compA);
921                         Assert.AreEqual (2, handler.GetInvocationList ().Length, "#D2");
922                         Assert.AreSame (handlerA, pd.GetValueChangedHandler (compB), "#D3");
923
924                         pd.RemoveValueChanged (compB, handlerB);
925
926                         Assert.IsNull (pd.GetValueChangedHandler (null), "#E1");
927                         handler = pd.GetValueChangedHandler (compA);
928                         Assert.AreEqual (2, handler.GetInvocationList ().Length, "#E2");
929                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#E3");
930
931                         pd.RemoveValueChanged (compB, handlerB);
932
933                         Assert.IsNull (pd.GetValueChangedHandler (null), "#F1");
934                         handler = pd.GetValueChangedHandler (compA);
935                         Assert.AreEqual (2, handler.GetInvocationList ().Length, "#F2");
936                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#F3");
937
938                         pd.RemoveValueChanged (compA, handlerC);
939
940                         Assert.IsNull (pd.GetValueChangedHandler (null), "#G1");
941                         handler = pd.GetValueChangedHandler (compA);
942                         Assert.AreEqual (2, handler.GetInvocationList ().Length, "#G2");
943                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#G3");
944
945                         pd.AddValueChanged (compA, handlerC);
946
947                         Assert.IsNull (pd.GetValueChangedHandler (null), "#H1");
948                         handler = pd.GetValueChangedHandler (compA);
949                         Assert.AreEqual (3, handler.GetInvocationList ().Length, "#H2");
950                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#H3");
951
952                         pd.RemoveValueChanged (compA, handlerB);
953
954                         Assert.IsNull (pd.GetValueChangedHandler (null), "#I1");
955                         handler = pd.GetValueChangedHandler (compA);
956                         Assert.AreEqual (2, handler.GetInvocationList ().Length, "#I2");
957                         Assert.AreEqual (handlerA, handler.GetInvocationList () [0], "#I3");
958                         Assert.AreEqual (handlerC, handler.GetInvocationList () [1], "#I4");
959                         Assert.IsNull (pd.GetValueChangedHandler (compB), "#I5");
960                 }
961
962                 [Test]
963                 public void RemoveValueChanged ()
964                 {
965                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
966                                 "Name", new Attribute [0]);
967                         object compA = new object ();
968                         object compB = new object ();
969                         EventHandler handlerA = new EventHandler (ValueChanged1);
970                         EventHandler handlerB = new EventHandler (ValueChanged1);
971                         EventHandler handlerC = new EventHandler (ValueChanged2);
972
973                         pd.AddValueChanged (compA, handlerA);
974                         pd.AddValueChanged (compA, handlerC);
975                         pd.AddValueChanged (compA, handlerC);
976                         pd.AddValueChanged (compA, handlerB);
977                         pd.AddValueChanged (compB, handlerC);
978
979                         pd.FireValueChanged (compA, new EventArgs ());
980                         Assert.AreEqual (4, _invokedHandlers.Count, "#A1");
981                         pd.RemoveValueChanged (new object (), handlerC);
982                         pd.FireValueChanged (compA, new EventArgs ());
983                         Assert.AreEqual (8, _invokedHandlers.Count, "#A2");
984
985                         Reset ();
986                         pd.RemoveValueChanged (compA, handlerC);
987
988                         pd.FireValueChanged (compA, new EventArgs ());
989                         Assert.AreEqual (3, _invokedHandlers.Count, "#B1");
990                         Assert.AreEqual ("ValueChanged1", _invokedHandlers [0], "#B2");
991                         Assert.AreEqual ("ValueChanged2", _invokedHandlers [1], "#B3");
992                         Assert.AreEqual ("ValueChanged1", _invokedHandlers [2], "#B4");
993
994                         Reset ();
995
996                         pd.FireValueChanged (compB, new EventArgs ());
997                         Assert.AreEqual (1, _invokedHandlers.Count, "#C1");
998                         Assert.AreEqual ("ValueChanged2", _invokedHandlers [0], "#C2");
999
1000                         Reset ();
1001                         pd.RemoveValueChanged (compB, handlerC);
1002
1003                         pd.FireValueChanged (compB, new EventArgs ());
1004                         Assert.AreEqual (0, _invokedHandlers.Count, "#D");
1005                 }
1006
1007                 [Test]
1008                 public void RemoveValueChanged_Component_Null ()
1009                 {
1010                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
1011                                 "Name", new Attribute [0]);
1012                         try {
1013                                 pd.RemoveValueChanged (null, new EventHandler (ValueChanged1));
1014                                 Assert.Fail ("#1");
1015                         } catch (ArgumentNullException ex) {
1016                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1017                                 Assert.IsNull (ex.InnerException, "#3");
1018                                 Assert.IsNotNull (ex.Message, "#4");
1019                                 Assert.IsNotNull (ex.ParamName, "#5");
1020                                 Assert.AreEqual ("component", ex.ParamName, "#6");
1021                         }
1022                 }
1023
1024                 [Test]
1025                 public void RemoveValueChanged_Handler_Null ()
1026                 {
1027                         MockPropertyDescriptor pd = new MockPropertyDescriptor (
1028                                 "Name", new Attribute [0]);
1029                         try {
1030                                 pd.RemoveValueChanged (new object (), (EventHandler) null);
1031                                 Assert.Fail ("#1");
1032                         } catch (ArgumentNullException ex) {
1033                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
1034                                 Assert.IsNull (ex.InnerException, "#3");
1035                                 Assert.IsNotNull (ex.Message, "#4");
1036                                 Assert.IsNotNull (ex.ParamName, "#5");
1037                                 Assert.AreEqual ("handler", ex.ParamName, "#6");
1038                         }
1039                 }
1040
1041                 void ValueChanged1 (object sender, EventArgs e)
1042                 {
1043                         _invokedHandlers.Add ("ValueChanged1");
1044                 }
1045
1046                 void ValueChanged2 (object sender, EventArgs e)
1047                 {
1048                         _invokedHandlers.Add ("ValueChanged2");
1049                 }
1050
1051                 static Attribute FindAttribute (PropertyDescriptor pd, Type type)
1052                 {
1053                         foreach (Attribute attr in pd.Attributes)
1054                                 if (attr.GetType () == type)
1055                                         return attr;
1056                         return null;
1057                 }
1058 #if !MOBILE
1059                 class GetEditor_test 
1060                 {
1061                         [Editor (typeof (UIEditor), typeof (UITypeEditor))]
1062                         public string Property {
1063                                 get { return "abc"; }
1064                                 set { }
1065                         }
1066                 }
1067
1068                 class UIEditor : UITypeEditor
1069                 {               
1070                 }
1071
1072                 [Test]
1073                 public void GetEditorTest ()
1074                 {
1075                         PropertyDescriptorCollection col;
1076                         PropertyDescriptor pd;
1077                         UITypeEditor ed;
1078                         
1079                         col = TypeDescriptor.GetProperties (typeof (GetEditor_test));
1080                         pd = col [0];
1081                         ed = pd.GetEditor (typeof (UITypeEditor)) as UITypeEditor;
1082                         
1083                         Assert.IsNotNull (ed, "#01");
1084                         Assert.AreEqual (ed.GetType ().Name, "UIEditor", "#02");
1085                 }
1086 #endif
1087
1088                 class MockPropertyDescriptor : PropertyDescriptor
1089                 {
1090                         public MockPropertyDescriptor (MemberDescriptor reference)
1091                                 : base (reference)
1092                         {
1093                         }
1094
1095                         public MockPropertyDescriptor (MemberDescriptor reference, Attribute [] attrs)
1096                                 : base (reference, attrs)
1097                         {
1098                         }
1099
1100                         public MockPropertyDescriptor (string name, Attribute [] attrs)
1101                                 : base (name, attrs)
1102                         {
1103                         }
1104
1105                         public override Type ComponentType {
1106                                 get { return typeof (int); }
1107                         }
1108
1109                         public override bool IsReadOnly {
1110                                 get { return false; }
1111                         }
1112
1113                         public override Type PropertyType{
1114                                 get { return typeof (DateTime); }
1115                         }
1116
1117                         public override object GetValue (object component)
1118                         {
1119                                 return null;
1120                         }
1121
1122                         public override void SetValue (object component, object value)
1123                         {
1124                         }
1125
1126                         public override void ResetValue (object component)
1127                         {
1128                         }
1129
1130                         public override bool CanResetValue (object component)
1131                         {
1132                                 return true;
1133                         }
1134
1135                         public override bool ShouldSerializeValue (object component)
1136                         {
1137                                 return true;
1138                         }
1139
1140                         public void FireValueChanged (object component, EventArgs e)
1141                         {
1142                                 base.OnValueChanged (component, e);
1143                         }
1144
1145                         public new object GetInvocationTarget (Type type, object instance)
1146                         {
1147                                 return base.GetInvocationTarget (type, instance);
1148                         }
1149
1150                         public new EventHandler GetValueChangedHandler (object component)
1151                         {
1152                                 return base.GetValueChangedHandler (component);
1153                         }
1154                 }
1155
1156                 [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property)]
1157                 public class PropTestAttribute : Attribute
1158                 {
1159                         public PropTestAttribute ()
1160                         {
1161                         }
1162                 }
1163
1164                 public class TestBase
1165                 {
1166                         [PropTest]
1167                         public int PropBase1
1168                         {
1169                                 get { return 0; }
1170                                 set { }
1171                         }
1172
1173                         [PropTest]
1174                         [Description ("whatever")]
1175                         public string PropBase2
1176                         {
1177                                 get { return ""; }
1178                                 set { }
1179                         }
1180
1181                         [PropTest]
1182                         public virtual string PropBase3
1183                         {
1184                                 get { return ""; }
1185                                 set { }
1186                         }
1187                 }
1188
1189                 public class TestSub : TestBase
1190                 {
1191                         [PropTest]
1192                         public int PropSub1
1193                         {
1194                                 get { return 0; }
1195                                 set { }
1196                         }
1197
1198                         [PropTest]
1199                         public string PropSub2
1200                         {
1201                                 get { return ""; }
1202                                 set { }
1203                         }
1204
1205                         public override string PropBase3
1206                         {
1207                                 get { return ""; }
1208                                 set { }
1209                         }
1210                 }
1211         }
1212 }