| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- //
- // System.ComponentModel.PropertyDescriptorCollection test cases
- //
- // Authors:
- // Gert Driesen ([email protected])
- //
- // (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;
- }
- }
- }
- }
- }
|