c803c93cddcc91c8d721b3027f96f9ca916a315d
[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
15 using NUnit.Framework;
16
17 namespace MonoTests.System.ComponentModel
18 {
19         [TestFixture]
20         public class PropertyDescriptorTests
21         {
22                 class ReadOnlyProperty_test
23                 {
24                         public int Prop {
25                                 get { return 5; }
26                         }
27                 }
28
29                 class ReadOnlyAttribute_test
30                 {
31                         [ReadOnly (true)]
32                         public int Prop {
33                                 get { return 5; }
34                                 set { }
35                         }
36                 }
37
38                 class ConflictingReadOnly_test
39                 {
40                         [ReadOnly (false)]
41                         public int Prop {
42                                 get { return 5; }
43                         }
44                 }
45
46                 class ShouldSerialize_public_test
47                 {
48                         public int Prop {
49                                 get { return 5; }
50                         }
51
52                         public bool ShouldSerializeProp()
53                         {
54                                 return false;
55                         }
56                 }
57
58                 class ShouldSerialize_protected_test
59                 {
60                         public int Prop {
61                                 get { return 5; }
62                         }
63
64                         protected bool ShouldSerializeProp()
65                         {
66                                 return false;
67                         }
68                 }
69
70                 class ShouldSerialize_private_test
71                 {
72                         public int Prop {
73                                 get { return 5; }
74                         }
75
76                         private bool ShouldSerializeProp()
77                         {
78                                 return false;
79                         }
80                 }
81
82                 class ShouldSerializeFalseEffectOnCanReset_test
83                 {
84                         public int Prop {
85                                 get { return 5; }
86                                 set { }
87                         }
88
89                         public bool ShouldSerializeProp()
90                         {
91                                 return false;
92                         }
93
94                         public void ResetProp()
95                         {
96                         }
97                 }
98
99                 class NoSerializeOrResetProp_test
100                 {
101                         public int Prop {
102                                 get { return 5; }
103                         }
104                 }
105
106                 class CanReset_public_test
107                 {
108                         int prop = 5;
109                         public int Prop {
110                                 get { return prop; }
111                                 set { prop = value; }
112                         }
113
114                         public void ResetProp()
115                         {
116                                 prop = 10;
117                         }
118                 }
119
120                 class CanReset_protected_test
121                 {
122                         int prop = 5;
123                         public int Prop {
124                                 get { return prop; }
125                                 set { prop = value; }
126                         }
127
128                         protected void ResetProp()
129                         {
130                                 prop = 10;
131                         }
132                 }
133
134                 class CanReset_private_test
135                 {
136                         int prop = 5;
137                         public int Prop {
138                                 get { return prop; }
139                                 set { prop = value; }
140                         }
141
142                         private void ResetProp()
143                         {
144                                 prop = 10;
145                         }
146                 }
147
148                 class CanResetNoSetter_test
149                 {
150                         int prop = 5;
151                         public int Prop {
152                                 get { return prop; }
153                         }
154
155                         private void ResetProp()
156                         {
157                                 prop = 10;
158                         }
159                 }
160
161                 [Test]
162                 public void ShouldSerializeTest_public ()
163                 {
164                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_public_test))["Prop"];
165                         ShouldSerialize_public_test test = new ShouldSerialize_public_test ();
166
167                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
168                 }
169
170                 [Test]
171                 public void ShouldSerializeTest_protected ()
172                 {
173                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
174                         ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
175
176                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
177                 }
178
179                 [Test]
180                 public void ShouldSerializeTest_private ()
181                 {
182                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerialize_protected_test))["Prop"];
183                         ShouldSerialize_protected_test test = new ShouldSerialize_protected_test ();
184
185                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
186                 }
187
188                 [Test]
189                 public void CanResetTest_public ()
190                 {
191                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_public_test))["Prop"];
192                         CanReset_public_test test = new CanReset_public_test ();
193
194                         Assert.IsTrue (p.CanResetValue (test), "1");
195                         Assert.AreEqual (5, test.Prop, "2");
196                         p.ResetValue (test);
197                         Assert.AreEqual (10, test.Prop, "3");
198                 }
199
200                 [Test]
201                 public void CanResetTest_protected ()
202                 {
203                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_protected_test))["Prop"];
204                         CanReset_protected_test test = new CanReset_protected_test ();
205
206                         Assert.IsTrue (p.CanResetValue (test), "1");
207                         Assert.AreEqual (5, test.Prop, "2");
208                         p.ResetValue (test);
209                         Assert.AreEqual (10, test.Prop, "3");
210                 }
211
212                 [Test]
213                 public void CanResetTest_private ()
214                 {
215                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanReset_private_test))["Prop"];
216                         CanReset_private_test test = new CanReset_private_test ();
217
218                         Assert.IsTrue (p.CanResetValue (test), "1");
219                         Assert.AreEqual (5, test.Prop, "2");
220                         p.ResetValue (test);
221                         Assert.AreEqual (10, test.Prop, "3");
222                 }
223
224                 [Test]
225                 public void CanResetTestNoSetterTest ()
226                 {
227                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (CanResetNoSetter_test))["Prop"];
228                         CanResetNoSetter_test test = new CanResetNoSetter_test ();
229
230 #if NET_2_0
231                         Assert.IsFalse (p.CanResetValue (test), "1");
232 #else
233                         Assert.IsTrue (p.CanResetValue (test), "1");
234 #endif
235                         Assert.AreEqual (5, test.Prop, "2");
236                         p.ResetValue (test);
237                         Assert.AreEqual (10, test.Prop, "3");
238                 }
239
240                 [Test]
241                 public void NoSerializeOrResetPropTest ()
242                 {
243                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (NoSerializeOrResetProp_test))["Prop"];
244                         NoSerializeOrResetProp_test test = new NoSerializeOrResetProp_test ();
245
246                         Assert.IsFalse (p.CanResetValue (test), "1");
247                         Assert.IsFalse (p.ShouldSerializeValue (test), "2");
248                 }
249
250                 [Test]
251                 public void ShouldSerializeFalseEffectOnCanResetTest ()
252                 {
253                         PropertyDescriptor p = TypeDescriptor.GetProperties (typeof (ShouldSerializeFalseEffectOnCanReset_test))["Prop"];
254                         ShouldSerializeFalseEffectOnCanReset_test test = new ShouldSerializeFalseEffectOnCanReset_test ();
255
256                         Assert.IsFalse (p.ShouldSerializeValue (test), "1");
257                         Assert.IsFalse (p.CanResetValue (test), "2");
258                 }
259
260                 [Test]
261                 public void ReadOnlyPropertyTest ()
262                 {
263                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyProperty_test));
264                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
265                 }
266
267                 [Test]
268                 public void ReadOnlyAttributeTest ()
269                 {
270                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ReadOnlyAttribute_test));
271                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
272                 }
273
274                 [Test]
275                 public void ReadOnlyConflictingTest ()
276                 {
277                         PropertyDescriptorCollection col = TypeDescriptor.GetProperties (typeof (ConflictingReadOnly_test));
278                         Assert.IsTrue (col["Prop"].IsReadOnly, "1");
279                 }
280         }
281 }