* PropertyDescriptorTests.cs: Added GetEditorTest.
[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 //
7 // (c) 2006 Novell, Inc. (http://www.novell.com/)
8 //
9
10 using System;
11 using System.Collections;
12 using System.ComponentModel;
13 using System.Globalization;
14 using System.Drawing.Design;
15 using System.ComponentModel.Design;
16
17 using NUnit.Framework;
18
19 namespace MonoTests.System.ComponentModel
20 {
21         [TestFixture]
22         public class PropertyDescriptorTests
23         {
24                 class MissingConverterType_test
25                 {
26                         public class NestedClass { }
27
28                         [TypeConverter ("missing-type-name")]
29                         public NestedClass Prop {
30                                 get { return null; }
31                         }
32
33                         [TypeConverter ("missing-type-name")]
34                         public int IntProp {
35                                 get { return 5; }
36                         }
37
38                         [TypeConverter ("missing-type-name")]
39                         public string StringProp {
40                                 get { return ""; }
41                         }
42                 }
43
44                 class ReadOnlyProperty_test
45                 {
46                         public int Prop {
47                                 get { return 5; }
48                         }
49                 }
50
51                 class ReadOnlyAttribute_test
52                 {
53                         [ReadOnly (true)]
54                         public int Prop {
55                                 get { return 5; }
56                                 set { }
57                         }
58                 }
59
60                 class ConflictingReadOnly_test
61                 {
62                         [ReadOnly (false)]
63                         public int Prop {
64                                 get { return 5; }
65                         }
66                 }
67
68                 class ShouldSerialize_public_test
69                 {
70                         public int Prop {
71                                 get { return 5; }
72                         }
73
74                         public bool ShouldSerializeProp()
75                         {
76                                 return false;
77                         }
78                 }
79
80                 class ShouldSerialize_protected_test
81                 {
82                         public int Prop {
83                                 get { return 5; }
84                         }
85
86                         protected bool ShouldSerializeProp()
87                         {
88                                 return false;
89                         }
90                 }
91
92                 class ShouldSerialize_private_test
93                 {
94                         public int Prop {
95                                 get { return 5; }
96                         }
97
98                         private bool ShouldSerializeProp()
99                         {
100                                 return false;
101                         }
102                 }
103
104                 class ShouldSerializeFalseEffectOnCanReset_test
105                 {
106                         public int Prop {
107                                 get { return 5; }
108                                 set { }
109                         }
110
111                         public bool ShouldSerializeProp()
112                         {
113                                 return false;
114                         }
115
116                         public void ResetProp()
117                         {
118                         }
119                 }
120
121                 class NoSerializeOrResetProp_test
122                 {
123                         public int Prop {
124                                 get { return 5; }
125                         }
126                 }
127
128                 class CanReset_public_test
129                 {
130                         int prop = 5;
131                         public int Prop {
132                                 get { return prop; }
133                                 set { prop = value; }
134                         }
135
136                         public void ResetProp()
137                         {
138                                 prop = 10;
139                         }
140                 }
141
142                 class CanReset_protected_test
143                 {
144                         int prop = 5;
145                         public int Prop {
146                                 get { return prop; }
147                                 set { prop = value; }
148                         }
149
150                         protected void ResetProp()
151                         {
152                                 prop = 10;
153                         }
154                 }
155
156                 class CanReset_private_test
157                 {
158                         int prop = 5;
159                         public int Prop {
160                                 get { return prop; }
161                                 set { prop = value; }
162                         }
163
164                         private void ResetProp()
165                         {
166                                 prop = 10;
167                         }
168                 }
169
170                 class CanResetNoSetter_test
171                 {
172                         int prop = 5;
173                         public int Prop {
174                                 get { return prop; }
175                         }
176
177                         private void ResetProp()
178                         {
179                                 prop = 10;
180                         }
181                 }
182
183                 class DisplayName_test
184                 {
185 #if NET_2_0
186                         [DisplayName ("An explicit displayname")]
187 #endif
188                         public bool Explicit {
189                                 get { return false; }
190                         }
191
192                         public bool Implicit {
193                                 get { return false; }
194                         }
195                 }
196
197                 [Test]
198                 public void MissingTypeConverter ()
199                 {
200                         PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["Prop"];
201                         PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["IntProp"];
202                         PropertyDescriptor p3 = TypeDescriptor.GetProperties (typeof (MissingConverterType_test))["StringProp"];
203
204                         Assert.AreEqual (typeof (TypeConverter), p1.Converter.GetType (), "1");
205                         Assert.AreEqual (typeof (Int32Converter), p2.Converter.GetType (), "2");
206                         Assert.AreEqual (typeof (StringConverter), p3.Converter.GetType (), "3");
207                 }
208
209                 [Test]
210                 public void ShouldSerializeTest_public ()
211                 {
212                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
213                         ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
214
215                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
216                 }
217
218                 [Test]
219                 public void ShouldSerializeTest_protected ()
220                 {
221                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
222                         ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
223
224                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
225                 }
226
227                 [Test]
228                 public void ShouldSerializeTest_private ()
229                 {
230                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
231                         ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
232
233                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
234                 }
235
236                 [Test]
237                 public void CanResetTest_public ()
238                 {
239                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
240                         CanReset_public_test test = new CanReset_public_test ();
241
242                         Assert.IsTrue (p.CanResetValue (test), "1");
243                         Assert.AreEqual (5, test.Prop, "2");
244                         p.ResetValue (test);
245                         Assert.AreEqual (10, test.Prop, "3");
246                 }
247
248                 [Test]
249                 public void CanResetTest_protected ()
250                 {
251                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
252                         CanReset_protected_test test = new CanReset_protected_test ();
253
254                         Assert.IsTrue (p.CanResetValue (test), "1");
255                         Assert.AreEqual (5, test.Prop, "2");
256                         p.ResetValue (test);
257                         Assert.AreEqual (10, test.Prop, "3");
258                 }
259
260                 [Test]
261                 public void CanResetTest_private ()
262                 {
263                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
264                         CanReset_private_test test = new CanReset_private_test ();
265
266                         Assert.IsTrue (p.CanResetValue (test), "1");
267                         Assert.AreEqual (5, test.Prop, "2");
268                         p.ResetValue (test);
269                         Assert.AreEqual (10, test.Prop, "3");
270                 }
271
272                 [Test]
273                 public void CanResetTestNoSetterTest ()
274                 {
275                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
276                         CanResetNoSetter_test test = new CanResetNoSetter_test ();
277
278 #if NET_2_0
279                         Assert.IsFalse (p.CanResetValue (test), "1");
280 #else
281                         Assert.IsTrue (p.CanResetValue (test), "1");
282 #endif
283                         Assert.AreEqual (5, test.Prop, "2");
284                         p.ResetValue (test);
285                         Assert.AreEqual (10, test.Prop, "3");
286                 }
287
288                 [Test]
289                 public void NoSerializeOrResetPropTest ()
290                 {
291                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
292                         NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
293
294                         Assert.IsFalse (p.CanResetValue (test), "1");
295                         Assert.IsFalse (p.ShouldSerializeValue (test), "2");
296                 }
297
298                 [Test]
299                 public void ShouldSerializeFalseEffectOnCanResetTest ()
300                 {
301                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
302                         ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
303
304                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
305                         Assert.IsFalse (p.CanResetValue (test), "2");
306                 }
307
308                 [Test]
309                 public void ReadOnlyPropertyTest ()
310                 {
311                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
312                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
313                 }
314
315                 [Test]
316                 public void ReadOnlyAttributeTest ()
317                 {
318                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
319                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
320                 }
321
322                 [Test]
323                 public void ReadOnlyConflictingTest ()
324                 {
325                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
326                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
327                 }
328
329                 [Test] // bug #80292
330                 public void DisplayNameTest ()
331                 {
332                         PropertyDescriptor p1 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Explicit"];
333                         PropertyDescriptor p2 = TypeDescriptor.GetProperties (typeof (DisplayName_test)) ["Implicit"];
334
335 #if NET_2_0
336                         Assert.AreEqual ("An explicit displayname", p1.DisplayName, "#1");
337 #else
338                         Assert.AreEqual ("Explicit", p1.DisplayName, "#1");
339 #endif
340                         Assert.AreEqual ("Implicit", p2.DisplayName, "#2");
341                 }
342
343                 [Test]
344                 public void GetEditorTest ()
345                 {
346                         PropertyDescriptorCollection col;
347                         PropertyDescriptor pd;
348                         UITypeEditor ed;
349
350                         col = TypeDescriptor.GetProperties (typeof (GetEditor_test));
351                         pd = col [0];
352                         ed = pd.GetEditor (typeof (UITypeEditor)) as UITypeEditor;
353
354                         Assert.IsNotNull (ed, "#01");
355                         Assert.AreEqual (ed.GetType ().Name, "UIEditor", "#02");
356                 
357                 }
358
359                 class GetEditor_test 
360                 {
361                         [Editor (typeof (UIEditor), typeof (UITypeEditor))]
362                         public string Property {
363                                 get { return "abc"; }
364                                 set { }
365                         }
366                 }
367
368                 class UIEditor : UITypeEditor
369                 {
370                         
371                 }
372         }
373 }