1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
//
// System.ComponentModel.PropertyDescriptorCollection test cases
//
// Authors:
// Gert Driesen (drieseng@users.sourceforge.net)
//
// (c) 2005 Novell, Inc. (http://www.ximian.com)
//
using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using NUnit.Framework;
namespace MonoTests.System.ComponentModel
{
[TestFixture]
public class PropertyDescriptorCollectionTests
{
[Test]
public void Empty ()
{
PropertyDescriptorCollection descriptors = PropertyDescriptorCollection.Empty;
AssertReadOnly (descriptors, "Empty");
}
[Test]
public void Find ()
{
PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection (
new PropertyDescriptor[] { new MockPropertyDescriptor("A", 1),
new MockPropertyDescriptor("b", 2)});
Assert.IsNotNull (descriptors.Find ("A", false), "#1");
Assert.IsNotNull (descriptors.Find ("b", false), "#2");
Assert.IsNull (descriptors.Find ("a", false), "#3");
Assert.IsNotNull (descriptors.Find ("a", true), "#4");
}
[Test]
[ExpectedException (typeof(ArgumentNullException))]
public void Find_NullKey ()
{
PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection (
new PropertyDescriptor[] { new MockPropertyDescriptor("A", 1),
new MockPropertyDescriptor("b", 2)});
descriptors.Find (null, false);
}
[Test]
public void IList ()
{
IList list = ((IList) new PropertyDescriptorCollection (null));
Assert.AreEqual (0, list.Count, "#1");
#if NET_2_0
Assert.IsFalse (list.IsFixedSize, "#2");
#else
Assert.IsTrue (list.IsFixedSize, "#2");
#endif
Assert.IsFalse (list.IsReadOnly, "#3");
Assert.IsFalse (list.IsSynchronized, "#4");
Assert.IsNull (list.SyncRoot, "#5");
}
[Test]
public void IList_Add_Null ()
{
IList list = ((IList) new PropertyDescriptorCollection (null));
Assert.AreEqual (0, list.Count, "#1");
list.Add (null);
Assert.AreEqual (1, list.Count, "#2");
}
[Test]
[ExpectedException (typeof (InvalidCastException))]
public void IList_Add_NoPropertyDescriptor ()
{
IList list = ((IList) new PropertyDescriptorCollection (null));
list.Add (5);
}
[Test]
public void IDictionary ()
{
IDictionary dictionary = ((IDictionary) new PropertyDescriptorCollection (null));
Assert.AreEqual (0, dictionary.Count, "#1");
#if NET_2_0
Assert.IsFalse (dictionary.IsFixedSize, "#2");
#else
Assert.IsTrue (dictionary.IsFixedSize, "#2");
#endif
Assert.IsFalse (dictionary.IsReadOnly, "#3");
Assert.IsFalse (dictionary.IsSynchronized, "#4");
Assert.IsNull (dictionary.SyncRoot, "#5");
}
[Test]
[ExpectedException (typeof(ArgumentException))]
public void IDictionary_Add_Null ()
{
IDictionary dictionary = ((IDictionary) new PropertyDescriptorCollection (null));
dictionary.Add ("whatever", null);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void IDictionary_Add_NoPropertyDescriptor ()
{
IDictionary dictionary = ((IDictionary) new PropertyDescriptorCollection (null));
dictionary.Add ("whatever", 5);
}
#if NET_2_0
public void ReadOnly ()
{
PropertyDescriptorCollection descriptors = new PropertyDescriptorCollection(null, true);
AssertReadOnly (descriptors, "ReadOnly");
}
#endif
private void AssertReadOnly (PropertyDescriptorCollection descriptors, string testCase)
{
MockPropertyDescriptor mockPropertyDescr = new MockPropertyDescriptor (
"Date", DateTime.Now);
try {
descriptors.Add (mockPropertyDescr);
Assert.Fail (testCase + "#1");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
// ensure read-only check if performed before value is checked
try {
descriptors.Add (null);
Assert.Fail (testCase + "#2");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
try {
descriptors.Clear ();
Assert.Fail (testCase + "#3");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
try {
descriptors.Insert (0, mockPropertyDescr);
Assert.Fail (testCase + "#4");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
// ensure read-only check if performed before value is checked
try {
descriptors.Insert (0, null);
Assert.Fail (testCase + "#5");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
try {
descriptors.Remove (mockPropertyDescr);
Assert.Fail (testCase + "#6");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
// ensure read-only check if performed before value is checked
try {
descriptors.Remove (null);
Assert.Fail (testCase + "#7");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
try {
descriptors.RemoveAt (0);
Assert.Fail (testCase + "#8");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
IList list = (IList) descriptors;
Assert.IsTrue (((IList) descriptors).IsReadOnly, testCase + "#9");
#if NET_2_0
Assert.IsTrue (((IList) descriptors).IsFixedSize, testCase + "#10");
#else
Assert.IsFalse (((IList) descriptors).IsFixedSize, testCase + "#10");
#endif
try {
list.Add (mockPropertyDescr);
Assert.Fail (testCase + "#11");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
// ensure read-only check if performed before value is checked
try {
list.Add (null);
Assert.Fail (testCase + "#12");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
try {
list.Clear ();
Assert.Fail (testCase + "#13");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
try {
list.Insert (0, mockPropertyDescr);
Assert.Fail (testCase + "#14");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
// ensure read-only check if performed before value is checked
try {
list.Insert (0, null);
Assert.Fail (testCase + "#15");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
try {
list.Remove (mockPropertyDescr);
Assert.Fail (testCase + "#16");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
// ensure read-only check if performed before value is checked
try {
list.Remove (null);
Assert.Fail (testCase + "#17");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
try {
list.RemoveAt (0);
Assert.Fail (testCase + "#18");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
try {
list[0] = mockPropertyDescr;
Assert.Fail (testCase + "#19");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
// ensure read-only check if performed before value is checked
try {
list[0] = null;
Assert.Fail (testCase + "#20");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
IDictionary dictionary = (IDictionary) descriptors;
Assert.IsTrue (dictionary.IsReadOnly, testCase + "#21");
#if NET_2_0
Assert.IsTrue (dictionary.IsFixedSize, testCase + "#22");
#else
Assert.IsFalse (dictionary.IsFixedSize, testCase + "#22");
#endif
try {
dictionary.Add ("test", mockPropertyDescr);
Assert.Fail (testCase + "#23");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
// value is checked before read-only check
try {
dictionary.Add ("test", null);
Assert.Fail (testCase + "#24");
} catch (ArgumentException) {
// read-only collection cannot be modified
}
try {
dictionary.Clear ();
Assert.Fail (testCase + "#25");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
try {
dictionary[0] = mockPropertyDescr;
Assert.Fail (testCase + "#26");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
// ensure read-only check if performed before value is checked
try {
dictionary[0] = null;
Assert.Fail (testCase + "#27");
} catch (NotSupportedException) {
// read-only collection cannot be modified
}
}
private class MockPropertyDescriptor : PropertyDescriptor
{
private object _value;
public MockPropertyDescriptor (string name, object value) : base (name, null)
{
_value = value;
}
public override bool CanResetValue (object component)
{
return true;
}
public override object GetValue (object component)
{
return _value;
}
public override void ResetValue (object component)
{
_value = null;
}
public override void SetValue (object component, object value)
{
_value = value;
}
public override bool ShouldSerializeValue (object component)
{
return false;
}
public override Type ComponentType {
get {
if (_value != null) {
return _value.GetType ();
}
return null;
}
}
public override bool IsReadOnly {
get {
return false;
}
}
public override Type PropertyType {
get {
return ComponentType;
}
}
}
}
}
|