diff options
author | Alex Ghiondea <ghiondea.alexandru@microsoft.com> | 2016-09-28 02:49:21 +0300 |
---|---|---|
committer | Alex Ghiondea <ghiondea.alexandru@microsoft.com> | 2016-10-05 03:03:04 +0300 |
commit | 7564675bf1cbedfdfc043938512294857694debd (patch) | |
tree | 5b7ee98a3d291525f4000d21f584148392b2d733 /src/System.ComponentModel.TypeConverter | |
parent | f1b7585fa848a105a5ab30ee4619ce06706cdf3d (diff) |
Add tests for System.ComponentModel
These were ported over from Mono.
Diffstat (limited to 'src/System.ComponentModel.TypeConverter')
8 files changed, 2543 insertions, 2 deletions
diff --git a/src/System.ComponentModel.TypeConverter/tests/BindingListTests.cs b/src/System.ComponentModel.TypeConverter/tests/BindingListTests.cs new file mode 100644 index 0000000000..018e7767b2 --- /dev/null +++ b/src/System.ComponentModel.TypeConverter/tests/BindingListTests.cs @@ -0,0 +1,679 @@ +// Licensed to the .NET Foundation under one or more agreements. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Xunit; + +namespace System.ComponentModel.Tests +{ + public class BindingListTest + { + [Fact] + public void BindingListDefaults() + { + BindingList<string> l = new BindingList<string>(); + IBindingList ibl = (IBindingList)l; + + Assert.True(l.AllowEdit, "1"); + Assert.False(l.AllowNew, "2"); + Assert.True(l.AllowRemove, "3"); + Assert.True(l.RaiseListChangedEvents, "4"); + + Assert.False(ibl.IsSorted, "5"); + Assert.Equal(ibl.SortDirection, ListSortDirection.Ascending); + Assert.True(ibl.SupportsChangeNotification, "7"); + Assert.False(ibl.SupportsSearching, "8"); + Assert.False(ibl.SupportsSorting, "9"); + Assert.False(((IRaiseItemChangedEvents)l).RaisesItemChangedEvents, "10"); + } + + [Fact] + public void BindingListDefaults_FixedSizeList() + { + string[] arr = new string[10]; + BindingList<string> l = new BindingList<string>(arr); + IBindingList ibl = (IBindingList)l; + + Assert.True(l.AllowEdit, "1"); + Assert.False(l.AllowNew, "2"); + Assert.True(l.AllowRemove, "3"); + Assert.True(l.RaiseListChangedEvents, "4"); + + Assert.False(ibl.IsSorted, "5"); + Assert.Equal(ibl.SortDirection, ListSortDirection.Ascending); + Assert.True(ibl.SupportsChangeNotification, "7"); + Assert.False(ibl.SupportsSearching, "8"); + Assert.False(ibl.SupportsSorting, "9"); + Assert.False(((IRaiseItemChangedEvents)l).RaisesItemChangedEvents, "10"); + } + + [Fact] + public void BindingListDefaults_NonFixedSizeList() + { + List<string> list = new List<string>(); + BindingList<string> l = new BindingList<string>(list); + IBindingList ibl = (IBindingList)l; + + Assert.True(l.AllowEdit, "1"); + Assert.False(l.AllowNew, "2"); + Assert.True(l.AllowRemove, "3"); + Assert.True(l.RaiseListChangedEvents, "4"); + + Assert.False(ibl.IsSorted, "5"); + Assert.Equal(ibl.SortDirection, ListSortDirection.Ascending); + Assert.True(ibl.SupportsChangeNotification, "7"); + Assert.False(ibl.SupportsSearching, "8"); + Assert.False(ibl.SupportsSorting, "9"); + Assert.False(((IRaiseItemChangedEvents)l).RaisesItemChangedEvents, "10"); + } + + [Fact] + public void BindingListDefaults_ReadOnlyList() + { + List<string> list = new List<string>(); + BindingList<string> l = new BindingList<string>(list); + IBindingList ibl = (IBindingList)l; + + Assert.True(l.AllowEdit, "1"); + Assert.False(l.AllowNew, "2"); + Assert.True(l.AllowRemove, "3"); + Assert.True(l.RaiseListChangedEvents, "4"); + + Assert.False(ibl.IsSorted, "5"); + Assert.Equal(ibl.SortDirection, ListSortDirection.Ascending); + Assert.True(ibl.SupportsChangeNotification, "7"); + Assert.False(ibl.SupportsSearching, "8"); + Assert.False(ibl.SupportsSorting, "9"); + Assert.False(((IRaiseItemChangedEvents)l).RaisesItemChangedEvents, "10"); + } + + [Fact] + public void TestAllowNew() + { + // Object has a default ctor + BindingList<object> l1 = new BindingList<object>(); + Assert.True(l1.AllowNew, "1"); + + // string has no default ctor + BindingList<string> l2 = new BindingList<string>(); + Assert.False(l2.AllowNew, "2"); + + // adding a delegate to AddingNew fixes that + l2.AddingNew += delegate (object sender, AddingNewEventArgs e) { }; + Assert.True(l2.AllowNew, "3"); + + l1 = new BindingList<object>(); + + bool list_changed = false; + bool expected = false; + + l1.ListChanged += delegate (object sender, ListChangedEventArgs e) + { + list_changed = true; + Assert.Equal(-1, e.NewIndex); + Assert.Equal(ListChangedType.Reset, e.ListChangedType); + Assert.Equal(expected, l1.AllowNew); + }; + + expected = false; + l1.AllowNew = false; + + Assert.True(list_changed, "7"); + + //the default for T=object is true, so check + //if we enter the block for raising the event + //if we explicitly set it to the value it + //currently has. + l1 = new BindingList<object>(); + + list_changed = false; + + l1.ListChanged += delegate (object sender, ListChangedEventArgs e) + { + list_changed = true; + Assert.Equal(-1, e.NewIndex); + Assert.Equal(ListChangedType.Reset, e.ListChangedType); + Assert.Equal(expected, l1.AllowNew); + }; + + expected = true; + l1.AllowNew = true; + + //turns out it doesn't raise the event, so the check must only be for "allow_new == value" + Assert.False(list_changed, "11"); + } + + [Fact] + public void TestResetBindings() + { + BindingList<object> l = new BindingList<object>(); + + bool list_changed = false; + + l.ListChanged += delegate (object sender, ListChangedEventArgs e) + { + list_changed = true; + Assert.Equal(-1, e.NewIndex); + Assert.Equal(ListChangedType.Reset, e.ListChangedType); + }; + + l.ResetBindings(); + + Assert.True(list_changed, "3"); + } + + [Fact] + public void TestResetItem() + { + List<object> list = new List<object>(); + list.Add(new object()); + + BindingList<object> l = new BindingList<object>(list); + + bool item_changed = false; + + l.ListChanged += delegate (object sender, ListChangedEventArgs e) + { + item_changed = true; + Assert.Equal(0, e.NewIndex); + Assert.Equal(ListChangedType.ItemChanged, e.ListChangedType); + }; + + l.ResetItem(0); + + Assert.True(item_changed, "3"); + } + + [Fact] + public void TestRemoveItem() + { + List<object> list = new List<object>(); + list.Add(new object()); + + BindingList<object> l = new BindingList<object>(list); + + bool item_deleted = false; + + l.ListChanged += delegate (object sender, ListChangedEventArgs e) + { + item_deleted = true; + Assert.Equal(0, e.NewIndex); + Assert.Equal(ListChangedType.ItemDeleted, e.ListChangedType); + Assert.Equal(0, l.Count); // to show the event is raised after the removal + }; + + l.RemoveAt(0); + + Assert.True(item_deleted, "4"); + } + + [Fact] + public void TestRemoveItem_AllowRemoveFalse() + { + List<object> list = new List<object>(); + list.Add(new object()); + + BindingList<object> l = new BindingList<object>(list); + + l.AllowRemove = false; + + Assert.Throws<NotSupportedException>(() => l.RemoveAt(0)); + + } + + [Fact] + public void TestAllowEditEvent() + { + BindingList<object> l = new BindingList<object>(); + + bool event_raised = false; + bool expected = false; + + l.ListChanged += delegate (object sender, ListChangedEventArgs e) + { + event_raised = true; + Assert.Equal(-1, e.NewIndex); + Assert.Equal(ListChangedType.Reset, e.ListChangedType); + Assert.Equal(expected, l.AllowEdit); + }; + + expected = false; + l.AllowEdit = false; + + Assert.True(event_raised, "4"); + + // check to see if RaiseListChangedEvents affects AllowEdit's event. + l.RaiseListChangedEvents = false; + + event_raised = false; + expected = true; + l.AllowEdit = true; + + Assert.False(event_raised, "5"); + } + + [Fact] + public void TestAllowRemove() + { + BindingList<object> l = new BindingList<object>(); + + bool event_raised = false; + bool expected = false; + + l.ListChanged += delegate (object sender, ListChangedEventArgs e) + { + event_raised = true; + Assert.Equal(-1, e.NewIndex); + Assert.Equal(ListChangedType.Reset, e.ListChangedType); + Assert.Equal(expected, l.AllowRemove); + }; + + expected = false; + l.AllowRemove = false; + + Assert.True(event_raised, "4"); + + // check to see if RaiseListChangedEvents affects AllowRemove's event. + l.RaiseListChangedEvents = false; + + event_raised = false; + expected = true; + l.AllowRemove = true; + + Assert.False(event_raised, "5"); + } + + [Fact] + public void TestAddNew_SettingArgsNewObject() + { + BindingList<object> l = new BindingList<object>(); + + bool adding_event_raised = false; + object o = new object(); + + l.AddingNew += delegate (object sender, AddingNewEventArgs e) + { + adding_event_raised = true; + Assert.Null(e.NewObject); + e.NewObject = o; + }; + + object rv = l.AddNew(); + Assert.True(adding_event_raised, "2"); + Assert.Same(o, rv); + } + + [Fact] + public void TestAddNew() + { + BindingList<object> l = new BindingList<object>(); + + bool adding_event_raised = false; + object o = new object(); + + l.AddingNew += delegate (object sender, AddingNewEventArgs e) + { + adding_event_raised = true; + Assert.Null(e.NewObject); + }; + + object rv = l.AddNew(); + Assert.True(adding_event_raised, "2"); + Assert.NotNull(rv); + } + + [Fact] + public void TestAddNew_Cancel() + { + BindingList<object> l = new BindingList<object>(); + + bool adding_event_raised = false; + object o = new object(); + + bool list_changed = false; + ListChangedType change_type = ListChangedType.Reset; + int list_changed_index = -1; + + l.AddingNew += delegate (object sender, AddingNewEventArgs e) + { + adding_event_raised = true; + Assert.Null(e.NewObject); + }; + + l.ListChanged += delegate (object sender, ListChangedEventArgs e) + { + list_changed = true; + change_type = e.ListChangedType; + list_changed_index = e.NewIndex; + }; + + object rv = l.AddNew(); + Assert.True(adding_event_raised, "2"); + Assert.NotNull(rv); + + Assert.Equal(1, l.Count); + Assert.Equal(0, l.IndexOf(rv)); + Assert.True(list_changed, "6"); + Assert.Equal(ListChangedType.ItemAdded, change_type); + Assert.Equal(0, list_changed_index); + + list_changed = false; + + l.CancelNew(0); + + Assert.Equal(0, l.Count); + Assert.True(list_changed, "10"); + Assert.Equal(ListChangedType.ItemDeleted, change_type); + Assert.Equal(0, list_changed_index); + } + + [Fact] + public void TestAddNew_CancelDifferentIndex() + { + List<object> list = new List<object>(); + + list.Add(new object()); + list.Add(new object()); + + BindingList<object> l = new BindingList<object>(list); + + bool adding_event_raised = false; + object o = new object(); + + bool list_changed = false; + ListChangedType change_type = ListChangedType.Reset; + int list_changed_index = -1; + + l.AddingNew += delegate (object sender, AddingNewEventArgs e) + { + adding_event_raised = true; + Assert.Null(e.NewObject); + }; + + l.ListChanged += delegate (object sender, ListChangedEventArgs e) + { + list_changed = true; + change_type = e.ListChangedType; + list_changed_index = e.NewIndex; + }; + + object rv = l.AddNew(); + Assert.True(adding_event_raised, "2"); + Assert.NotNull(rv); + + Assert.Equal(3, l.Count); + Assert.Equal(2, l.IndexOf(rv)); + Assert.True(list_changed, "6"); + Assert.Equal(ListChangedType.ItemAdded, change_type); + Assert.Equal(2, list_changed_index); + + list_changed = false; + + l.CancelNew(0); + + Assert.False(list_changed, "9"); + Assert.Equal(3, l.Count); + + l.CancelNew(2); + + Assert.True(list_changed, "11"); + Assert.Equal(ListChangedType.ItemDeleted, change_type); + Assert.Equal(2, list_changed_index); + Assert.Equal(2, l.Count); + } + + [Fact] + public void TestAddNew_End() + { + BindingList<object> l = new BindingList<object>(); + + bool adding_event_raised = false; + object o = new object(); + + bool list_changed = false; + ListChangedType change_type = ListChangedType.Reset; + int list_changed_index = -1; + + l.AddingNew += delegate (object sender, AddingNewEventArgs e) + { + adding_event_raised = true; + Assert.Null(e.NewObject); + }; + + l.ListChanged += delegate (object sender, ListChangedEventArgs e) + { + list_changed = true; + change_type = e.ListChangedType; + list_changed_index = e.NewIndex; + }; + + object rv = l.AddNew(); + Assert.True(adding_event_raised, "2"); + Assert.NotNull(rv); + + Assert.Equal(1, l.Count); + Assert.Equal(0, l.IndexOf(rv)); + Assert.True(list_changed, "6"); + Assert.Equal(ListChangedType.ItemAdded, change_type); + Assert.Equal(0, list_changed_index); + + list_changed = false; + + l.EndNew(0); + + Assert.Equal(1, l.Count); + Assert.False(list_changed, "10"); + } + + [Fact] + public void TestAddNew_CancelDifferentIndexThenEnd() + { + BindingList<object> l = new BindingList<object>(); + + bool adding_event_raised = false; + object o = new object(); + + bool list_changed = false; + ListChangedType change_type = ListChangedType.Reset; + int list_changed_index = -1; + + l.AddingNew += delegate (object sender, AddingNewEventArgs e) + { + adding_event_raised = true; + Assert.Null(e.NewObject); + }; + + l.ListChanged += delegate (object sender, ListChangedEventArgs e) + { + list_changed = true; + change_type = e.ListChangedType; + list_changed_index = e.NewIndex; + }; + + object rv = l.AddNew(); + Assert.True(adding_event_raised, "2"); + Assert.NotNull(rv); + + Assert.Equal(1, l.Count); + Assert.Equal(0, l.IndexOf(rv)); + Assert.True(list_changed, "6"); + Assert.Equal(ListChangedType.ItemAdded, change_type); + Assert.Equal(0, list_changed_index); + + list_changed = false; + + l.CancelNew(2); + + Assert.Equal(1, l.Count); + Assert.False(list_changed, "10"); + + l.EndNew(0); + + Assert.Equal(1, l.Count); + Assert.False(list_changed, "12"); + } + + [Fact] + public void TestAddNew_EndDifferentIndexThenCancel() + { + BindingList<object> l = new BindingList<object>(); + + bool adding_event_raised = false; + object o = new object(); + + bool list_changed = false; + ListChangedType change_type = ListChangedType.Reset; + int list_changed_index = -1; + + l.AddingNew += delegate (object sender, AddingNewEventArgs e) + { + adding_event_raised = true; + Assert.Null(e.NewObject); + }; + + l.ListChanged += delegate (object sender, ListChangedEventArgs e) + { + list_changed = true; + change_type = e.ListChangedType; + list_changed_index = e.NewIndex; + }; + + object rv = l.AddNew(); + Assert.True(adding_event_raised, "2"); + Assert.NotNull(rv); + + Assert.Equal(1, l.Count); + Assert.Equal(0, l.IndexOf(rv)); + Assert.True(list_changed, "6"); + Assert.Equal(ListChangedType.ItemAdded, change_type); + Assert.Equal(0, list_changed_index); + + list_changed = false; + + l.EndNew(2); + + Assert.Equal(1, l.Count); + Assert.False(list_changed, "10"); + + l.CancelNew(0); + + Assert.True(list_changed, "11"); + Assert.Equal(ListChangedType.ItemDeleted, change_type); + Assert.Equal(0, list_changed_index); + } + + class BindingListPoker : BindingList<object> + { + public object DoAddNewCore() + { + return base.AddNewCore(); + } + } + + // test to make sure that the events are raised in AddNewCore and not in AddNew + [Fact] + public void TestAddNewCore_Insert() + { + BindingListPoker poker = new BindingListPoker(); + + bool adding_event_raised = false; + + bool list_changed = false; + ListChangedType change_type = ListChangedType.Reset; + int list_changed_index = -1; + + poker.AddingNew += delegate (object sender, AddingNewEventArgs e) + { + adding_event_raised = true; + }; + + poker.ListChanged += delegate (object sender, ListChangedEventArgs e) + { + list_changed = true; + change_type = e.ListChangedType; + list_changed_index = e.NewIndex; + }; + + object o = poker.DoAddNewCore(); + + Assert.True(adding_event_raised, "1"); + Assert.True(list_changed, "2"); + Assert.Equal(ListChangedType.ItemAdded, change_type); + Assert.Equal(0, list_changed_index); + Assert.Equal(1, poker.Count); + } + + private class Item : INotifyPropertyChanged + { + + public event PropertyChangedEventHandler PropertyChanged; + + string _name; + + public string Name + { + get { return _name; } + set + { + if (_name != value) + { + _name = value; + OnPropertyChanged("Name"); + } + } + } + + void OnPropertyChanged(string name) + { + var fn = PropertyChanged; + if (fn != null) + fn(this, new PropertyChangedEventArgs(name)); + } + } + + [Fact] + public void Test_InsertNull() + { + var list = new BindingList<Item>(); + list.Insert(0, null); + var count = list.Count; + + Assert.Equal(1, count); + } + + private class Person : INotifyPropertyChanged + { + private string _lastName; + private string _firstName; + + public string FirstName + { + get { return _firstName; } + set + { + _firstName = value; + OnPropertyChanged("FirstName"); // string matches property name + } + } + + public string LastName + { + get { return _lastName; } + set + { + _lastName = value; + OnPropertyChanged("NotTheName"); // string doesn't match property name + } + } + + public event PropertyChangedEventHandler PropertyChanged; + + protected virtual void OnPropertyChanged(string propertyName = null) + { + PropertyChangedEventHandler handler = PropertyChanged; + if (handler != null) + handler(this, new PropertyChangedEventArgs(propertyName)); + } + } + } +} diff --git a/src/System.ComponentModel.TypeConverter/tests/ContainerTests.cs b/src/System.ComponentModel.TypeConverter/tests/ContainerTests.cs new file mode 100644 index 0000000000..534e4e4336 --- /dev/null +++ b/src/System.ComponentModel.TypeConverter/tests/ContainerTests.cs @@ -0,0 +1,742 @@ +// Licensed to the .NET Foundation under one or more agreements. +// See the LICENSE file in the project root for more information. + +// +// System.ComponentModel.Container test cases +// +// Authors: +// Gonzalo Paniagua Javier (gonzalo@ximian.com) +// Ivan N. Zlatev (contact i-nZ.net) + +// Copyright (c) 2006 Novell, Inc. (http://www.novell.com) +// Copyright (c) 2006 Ivan N. Zlatev +// + +using System.ComponentModel.Design; +using Xunit; + +namespace System.ComponentModel.Tests +{ + internal class TestService + { + } + + internal class TestContainer : Container + { + ServiceContainer _services = new ServiceContainer(); + bool allowDuplicateNames; + + public TestContainer() + { + _services.AddService(typeof(TestService), new TestService()); + } + + public bool AllowDuplicateNames + { + get { return allowDuplicateNames; } + set { allowDuplicateNames = value; } + } + + protected override object GetService(Type serviceType) + { + return _services.GetService(serviceType); + } + + public new void RemoveWithoutUnsiting(IComponent component) + { + base.RemoveWithoutUnsiting(component); + } + + public void InvokeValidateName(IComponent component, string name) + { + ValidateName(component, name); + } + + protected override void ValidateName(IComponent component, string name) + { + if (AllowDuplicateNames) + return; + base.ValidateName(component, name); + } + + public bool Contains(IComponent component) + { + bool found = false; + + foreach (IComponent c in Components) + { + if (component.Equals(c)) + { + found = true; + break; + } + } + return found; + } + + public new void Dispose(bool disposing) + { + base.Dispose(disposing); + } + } + + internal class TestComponent : Component + { + public override ISite Site + { + get + { + return base.Site; + } + set + { + base.Site = value; + if (value != null) + { + Assert.NotNull(value.GetService(typeof(ISite))); + Assert.NotNull(value.GetService(typeof(TestService))); + } + } + } + + public bool IsDisposed + { + get { return disposed; } + } + + public bool ThrowOnDispose + { + get { return throwOnDispose; } + set { throwOnDispose = value; } + } + + protected override void Dispose(bool disposing) + { + if (ThrowOnDispose) + throw new InvalidOperationException(); + + base.Dispose(disposing); + disposed = true; + } + + private bool disposed; + private bool throwOnDispose; + } + + + public class ContainerTest + { + private TestContainer _container; + + public ContainerTest() + { + _container = new TestContainer(); + } + + [Fact] // Add (IComponent) + public void Add1() + { + TestContainer containerA = new TestContainer(); + TestContainer containerB = new TestContainer(); + + ISite siteA; + ISite siteB; + + TestComponent compA = new TestComponent(); + Assert.Null(compA.Site); + TestComponent compB = new TestComponent(); + Assert.Null(compB.Site); + Assert.Equal(0, containerA.Components.Count); + Assert.Equal(0, containerB.Components.Count); + + containerA.Add(compA); + siteA = compA.Site; + Assert.NotNull(siteA); + Assert.Same(compA, siteA.Component); + Assert.Same(containerA, siteA.Container); + Assert.False(siteA.DesignMode); + Assert.Null(siteA.Name); + containerA.Add(compB); + siteB = compB.Site; + Assert.NotNull(siteB); + Assert.Same(compB, siteB.Component); + Assert.Same(containerA, siteB.Container); + Assert.False(siteB.DesignMode); + Assert.Null(siteB.Name); + + Assert.False(object.ReferenceEquals(siteA, siteB)); + Assert.Equal(2, containerA.Components.Count); + Assert.Equal(0, containerB.Components.Count); + Assert.Same(compA, containerA.Components[0]); + Assert.Same(compB, containerA.Components[1]); + + // check effect of adding component that is already member of + // another container + containerB.Add(compA); + Assert.False(object.ReferenceEquals(siteA, compA.Site)); + siteA = compA.Site; + Assert.NotNull(siteA); + Assert.Same(compA, siteA.Component); + Assert.Same(containerB, siteA.Container); + Assert.False(siteA.DesignMode); + Assert.Null(siteA.Name); + + Assert.Equal(1, containerA.Components.Count); + Assert.Equal(1, containerB.Components.Count); + Assert.Same(compB, containerA.Components[0]); + Assert.Same(compA, containerB.Components[0]); + + // check effect of add component twice to same container + containerB.Add(compA); + Assert.Same(siteA, compA.Site); + + Assert.Equal(1, containerA.Components.Count); + Assert.Equal(1, containerB.Components.Count); + Assert.Same(compB, containerA.Components[0]); + Assert.Same(compA, containerB.Components[0]); + } + + [Fact] + public void Add1_Component_Null() + { + _container.Add((IComponent)null); + Assert.Equal(0, _container.Components.Count); + } + + [Fact] // Add (IComponent, String) + public void Add2() + { + TestContainer containerA = new TestContainer(); + TestContainer containerB = new TestContainer(); + + ISite siteA; + ISite siteB; + + TestComponent compA = new TestComponent(); + Assert.Null(compA.Site); + TestComponent compB = new TestComponent(); + Assert.Null(compB.Site); + Assert.Equal(0, containerA.Components.Count); + Assert.Equal(0, containerB.Components.Count); + + containerA.Add(compA, "A"); + siteA = compA.Site; + Assert.NotNull(siteA); + Assert.Same(compA, siteA.Component); + Assert.Same(containerA, siteA.Container); + Assert.False(siteA.DesignMode); + Assert.Equal("A", siteA.Name); + containerA.Add(compB, "B"); + siteB = compB.Site; + Assert.NotNull(siteB); + Assert.Same(compB, siteB.Component); + Assert.Same(containerA, siteB.Container); + Assert.False(siteB.DesignMode); + Assert.Equal("B", siteB.Name); + + Assert.False(object.ReferenceEquals(siteA, siteB)); + Assert.Equal(2, containerA.Components.Count); + Assert.Equal(0, containerB.Components.Count); + Assert.Same(compA, containerA.Components[0]); + Assert.Same(compB, containerA.Components[1]); + + // check effect of adding component that is already member of + // another container + containerB.Add(compA, "A2"); + Assert.False(object.ReferenceEquals(siteA, compA.Site)); + siteA = compA.Site; + Assert.NotNull(siteA); + Assert.Same(compA, siteA.Component); + Assert.Same(containerB, siteA.Container); + Assert.False(siteA.DesignMode); + Assert.Equal("A2", siteA.Name); + + Assert.Equal(1, containerA.Components.Count); + Assert.Equal(1, containerB.Components.Count); + Assert.Same(compB, containerA.Components[0]); + Assert.Same(compA, containerB.Components[0]); + + // check effect of add component twice to same container + containerB.Add(compA, "A2"); + Assert.Same(siteA, compA.Site); + Assert.Equal("A2", siteA.Name); + + Assert.Equal(1, containerA.Components.Count); + Assert.Equal(1, containerB.Components.Count); + Assert.Same(compB, containerA.Components[0]); + Assert.Same(compA, containerB.Components[0]); + + // add again with different name + containerB.Add(compA, "A3"); + Assert.Same(siteA, compA.Site); + Assert.Equal("A2", siteA.Name); + + Assert.Equal(1, containerA.Components.Count); + Assert.Equal(1, containerB.Components.Count); + Assert.Same(compB, containerA.Components[0]); + Assert.Same(compA, containerB.Components[0]); + + // check effect of add component twice to same container + containerB.Add(compA, "A2"); + Assert.Same(siteA, compA.Site); + Assert.Equal("A2", siteA.Name); + } + + [Fact] + public void Add2_Component_Null() + { + _container.Add((IComponent)null, "A"); + Assert.Equal(0, _container.Components.Count); + _container.Add(new TestComponent(), "A"); + Assert.Equal(1, _container.Components.Count); + _container.Add((IComponent)null, "A"); + Assert.Equal(1, _container.Components.Count); + } + + [Fact] + public void Add2_Name_Duplicate() + { + TestContainer container = new TestContainer(); + TestComponent c1 = new TestComponent(); + container.Add(c1, "dup"); + + // new component, same case + TestComponent c2 = new TestComponent(); + ArgumentException ex; + + ex = Assert.Throws<ArgumentException>(() => container.Add(c2, "dup")); + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.True(ex.Message.IndexOf("'dup'") != -1); + Assert.Null(ex.ParamName); + Assert.Equal(1, container.Components.Count); + + // new component, different case + TestComponent c3 = new TestComponent(); + ex = Assert.Throws<ArgumentException>(() => container.Add(c3, "duP")); + // Duplicate component name 'duP'. Component names must be + // unique and case-insensitive + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.True(ex.Message.IndexOf("'duP'") != -1); + Assert.Null(ex.ParamName); + Assert.Equal(1, container.Components.Count); + + // existing component, same case + TestComponent c4 = new TestComponent(); + container.Add(c4, "C4"); + Assert.Equal(2, container.Components.Count); + container.Add(c4, "dup"); + Assert.Equal(2, container.Components.Count); + Assert.Equal("C4", c4.Site.Name); + + // component of other container, same case + TestContainer container2 = new TestContainer(); + TestComponent c5 = new TestComponent(); + container2.Add(c5, "C5"); + ex = Assert.Throws<ArgumentException>(() => container.Add(c5, "dup")); + // Duplicate component name 'dup'. Component names must be + // unique and case-insensitive + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.True(ex.Message.IndexOf("'dup'") != -1); + Assert.Null(ex.ParamName); + Assert.Equal(2, container.Components.Count); + Assert.Equal(1, container2.Components.Count); + Assert.Same(c5, container2.Components[0]); + + container.AllowDuplicateNames = true; + TestComponent c6 = new TestComponent(); + container.Add(c6, "dup"); + Assert.Equal(3, container.Components.Count); + Assert.NotNull(c1.Site); + Assert.Equal("dup", c1.Site.Name); + Assert.NotNull(c6.Site); + Assert.Equal("dup", c6.Site.Name); + Assert.False(object.ReferenceEquals(c1.Site, c6.Site)); + } + + [Fact] + public void AddRemove() + { + TestComponent component = new TestComponent(); + + _container.Add(component); + Assert.NotNull(component.Site); + Assert.True(_container.Contains(component)); + + _container.Remove(component); + Assert.Null(component.Site); + Assert.False(_container.Contains(component)); + } + + [Fact] // Dispose () + public void Dispose1() + { + TestComponent compA; + TestComponent compB; + + compA = new TestComponent(); + _container.Add(compA); + compB = new TestComponent(); + _container.Add(compB); + + _container.Dispose(); + + Assert.Equal(0, _container.Components.Count); + Assert.True(compA.IsDisposed); + Assert.Null(compA.Site); + Assert.True(compB.IsDisposed); + Assert.Null(compB.Site); + + _container = new TestContainer(); + compA = new TestComponent(); + compA.ThrowOnDispose = true; + _container.Add(compA); + compB = new TestComponent(); + _container.Add(compB); + + Assert.Throws<InvalidOperationException>(() => _container.Dispose()); + // assert that component is not removed from components until after + // Dispose of component has succeeded + Assert.Equal(0, _container.Components.Count); + Assert.False(compA.IsDisposed); + Assert.Null(compA.Site); + Assert.True(compB.IsDisposed); + Assert.Null(compB.Site); + + compA.ThrowOnDispose = false; + + _container = new TestContainer(); + compA = new TestComponent(); + _container.Add(compA); + compB = new TestComponent(); + compB.ThrowOnDispose = true; + _container.Add(compB); + + Assert.Throws<InvalidOperationException>(() => _container.Dispose()); + Assert.Equal(1, _container.Components.Count); + Assert.Same(compA, _container.Components[0]); + Assert.False(compA.IsDisposed); + Assert.NotNull(compA.Site); + Assert.False(compB.IsDisposed); + Assert.Null(compB.Site); + compB.ThrowOnDispose = false; + } + + [Fact] // Dispose (Boolean) + public void Dispose2() + { + TestComponent compA; + TestComponent compB; + + compA = new TestComponent(); + _container.Add(compA); + compB = new TestComponent(); + _container.Add(compB); + + _container.Dispose(false); + + Assert.Equal(2, _container.Components.Count); + Assert.False(compA.IsDisposed); + Assert.NotNull(compA.Site); + Assert.False(compB.IsDisposed); + Assert.NotNull(compB.Site); + + _container.Dispose(true); + + Assert.Equal(0, _container.Components.Count); + Assert.True(compA.IsDisposed); + Assert.Null(compA.Site); + Assert.True(compB.IsDisposed); + Assert.Null(compB.Site); + + compA = new TestComponent(); + _container.Add(compA); + compB = new TestComponent(); + _container.Add(compB); + + Assert.Equal(2, _container.Components.Count); + Assert.False(compA.IsDisposed); + Assert.NotNull(compA.Site); + Assert.False(compB.IsDisposed); + Assert.NotNull(compB.Site); + + _container.Dispose(true); + + Assert.Equal(0, _container.Components.Count); + Assert.True(compA.IsDisposed); + Assert.Null(compA.Site); + Assert.True(compB.IsDisposed); + Assert.Null(compB.Site); + } + + [Fact] + public void Dispose_Recursive() + { + MyComponent comp = new MyComponent(); + Container container = comp.CreateContainer(); + comp.Dispose(); + Assert.Equal(0, container.Components.Count); + } + + [Fact] + public void GetService() + { + object service; + + GetServiceContainer container = new GetServiceContainer(); + container.Add(new MyComponent()); + service = container.GetService(typeof(MyComponent)); + Assert.Null(service); + service = container.GetService(typeof(Component)); + Assert.Null(service); + service = container.GetService(typeof(IContainer)); + Assert.Same(container, service); + service = container.GetService((Type)null); + Assert.Null(service); + } + + [Fact] + public void Remove() + { + TestComponent compA; + TestComponent compB; + ISite siteA; + ISite siteB; + + compA = new TestComponent(); + _container.Add(compA); + siteA = compA.Site; + compB = new TestComponent(); + _container.Add(compB); + siteB = compB.Site; + _container.Remove(compB); + Assert.Same(siteA, compA.Site); + Assert.Null(compB.Site); + Assert.Equal(1, _container.Components.Count); + Assert.Same(compA, _container.Components[0]); + + // remove component with no site + compB = new TestComponent(); + _container.Remove(compB); + Assert.Same(siteA, compA.Site); + Assert.Null(compB.Site); + Assert.Equal(1, _container.Components.Count); + Assert.Same(compA, _container.Components[0]); + + // remove component associated with other container + TestContainer container2 = new TestContainer(); + compB = new TestComponent(); + container2.Add(compB); + siteB = compB.Site; + _container.Remove(compB); + Assert.Same(siteA, compA.Site); + Assert.Same(siteB, compB.Site); + Assert.Equal(1, _container.Components.Count); + Assert.Same(compA, _container.Components[0]); + Assert.Equal(1, container2.Components.Count); + Assert.Same(compB, container2.Components[0]); + } + + [Fact] + public void Remove_Component_Null() + { + _container.Add(new TestComponent()); + _container.Remove((IComponent)null); + Assert.Equal(1, _container.Components.Count); + } + + [Fact] + public void RemoveWithoutUnsiting() + { + TestComponent compA; + TestComponent compB; + ISite siteA; + ISite siteB; + + compA = new TestComponent(); + _container.Add(compA); + siteA = compA.Site; + compB = new TestComponent(); + _container.Add(compB); + siteB = compB.Site; + _container.RemoveWithoutUnsiting(compB); + Assert.Same(siteA, compA.Site); + Assert.Same(siteB, compB.Site); + Assert.Equal(1, _container.Components.Count); + Assert.Same(compA, _container.Components[0]); + + // remove component with no site + compB = new TestComponent(); + _container.RemoveWithoutUnsiting(compB); + Assert.Same(siteA, compA.Site); + Assert.Null(compB.Site); + Assert.Equal(1, _container.Components.Count); + Assert.Same(compA, _container.Components[0]); + + // remove component associated with other container + TestContainer container2 = new TestContainer(); + compB = new TestComponent(); + container2.Add(compB); + siteB = compB.Site; + _container.RemoveWithoutUnsiting(compB); + Assert.Same(siteA, compA.Site); + Assert.Same(siteB, compB.Site); + Assert.Equal(1, _container.Components.Count); + Assert.Same(compA, _container.Components[0]); + Assert.Equal(1, container2.Components.Count); + Assert.Same(compB, container2.Components[0]); + } + + [Fact] + public void RemoveWithoutUnsiting_Component_Null() + { + ISite site; + TestComponent component; + + component = new TestComponent(); + _container.Add(component); + site = component.Site; + _container.RemoveWithoutUnsiting((IComponent)null); + Assert.Same(site, component.Site); + Assert.Equal(1, _container.Components.Count); + Assert.Same(component, _container.Components[0]); + } + + [Fact] + public void ValidateName_Component_Null() + { + ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => _container.InvokeValidateName((IComponent)null, "A")); + Assert.Equal(typeof(ArgumentNullException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.Equal("component", ex.ParamName); + } + + [Fact] + public void ValidateName_Name_Null() + { + TestComponent compA = new TestComponent(); + _container.Add(compA, (string)null); + TestComponent compB = new TestComponent(); + _container.InvokeValidateName(compB, (string)null); + } + + [Fact] + public void ValidateName_Name_Duplicate() + { + TestComponent compA = new TestComponent(); + _container.Add(compA, "dup"); + + // same component, same case + _container.InvokeValidateName(compA, "dup"); + + // existing component, same case + TestComponent compB = new TestComponent(); + _container.Add(compB, "B"); + + ArgumentException ex; + ex = Assert.Throws<ArgumentException>(() => _container.InvokeValidateName(compB, "dup")); + // Duplicate component name 'duP'. Component names must be + // unique and case-insensitive + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.True(ex.Message.IndexOf("'dup'") != -1); + Assert.Null(ex.ParamName); + Assert.Equal(2, _container.Components.Count); + _container.InvokeValidateName(compB, "whatever"); + + // new component, different case + TestComponent compC = new TestComponent(); + ex = Assert.Throws<ArgumentException>(() => _container.InvokeValidateName(compC, "dup")); + // Duplicate component name 'duP'. Component names must be + // unique and case-insensitive + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.True(ex.Message.IndexOf("'dup'") != -1); + Assert.Null(ex.ParamName); + Assert.Equal(2, _container.Components.Count); + _container.InvokeValidateName(compC, "whatever"); + + // component of other container, different case + TestContainer container2 = new TestContainer(); + TestComponent compD = new TestComponent(); + container2.Add(compD, "B"); + ex = Assert.Throws<ArgumentException>(() => _container.InvokeValidateName(compD, "dup")); + // Duplicate component name 'duP'. Component names must be + // unique and case-insensitive + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.True(ex.Message.IndexOf("'dup'") != -1); + Assert.Null(ex.ParamName); + Assert.Equal(2, _container.Components.Count); + _container.InvokeValidateName(compD, "whatever"); + Assert.Equal(1, container2.Components.Count); + Assert.Same(compD, container2.Components[0]); + } + + private class MyComponent : Component + { + private Container container; + + protected override void Dispose(bool disposing) + { + if (container != null) + container.Dispose(); + base.Dispose(disposing); + } + + public Container CreateContainer() + { + if (container != null) + throw new InvalidOperationException(); + container = new Container(); + container.Add(new MyComponent()); + container.Add(this); + return container; + } + } + + private class MyContainer : IContainer + { + private ComponentCollection components = new ComponentCollection( + new Component[0]); + + public ComponentCollection Components + { + get { return components; } + } + + public void Add(IComponent component) + { + } + + public void Add(IComponent component, string name) + { + } + + public void Remove(IComponent component) + { + } + + public void Dispose() + { + } + } + + public class GetServiceContainer : Container + { + public new object GetService(Type service) + { + return base.GetService(service); + } + } + } +}
\ No newline at end of file diff --git a/src/System.ComponentModel.TypeConverter/tests/ContextStackTests.cs b/src/System.ComponentModel.TypeConverter/tests/ContextStackTests.cs new file mode 100644 index 0000000000..d9f5f1a0fe --- /dev/null +++ b/src/System.ComponentModel.TypeConverter/tests/ContextStackTests.cs @@ -0,0 +1,186 @@ +// Licensed to the .NET Foundation under one or more agreements. +// See the LICENSE file in the project root for more information. + +// +// ContextStackTest.cs - Unit tests for +// System.ComponentModel.Design.Serialization.ContextStack +// +// Author: +// Ivan N. Zlatev <contact@i-nz.net> +// +// Copyright (C) 2007 Ivan N. Zlatev <contact@i-nz.net> +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + + +using System; +using System.ComponentModel.Design.Serialization; +using Xunit; + +namespace System.ComponentModel.Tests +{ + public class ContextStackTest + { + [Fact] + public void IntegrityTest() + { + ContextStack stack = new ContextStack(); + + string one = "one"; + string two = "two"; + stack.Push(two); + stack.Push(one); + Assert.Same(one, stack[typeof(string)]); + Assert.Same(one, stack[0]); + Assert.Same(one, stack.Current); + + Assert.Same(one, stack.Pop()); + + Assert.Same(two, stack[typeof(string)]); + Assert.Same(two, stack[0]); + Assert.Same(two, stack.Current); + + string three = "three"; + stack.Append(three); + + Assert.Same(two, stack[typeof(string)]); + Assert.Same(two, stack[0]); + Assert.Same(two, stack.Current); + + Assert.Same(two, stack.Pop()); + + Assert.Same(three, stack[typeof(string)]); + Assert.Same(three, stack[0]); + Assert.Same(three, stack.Current); + Assert.Same(three, stack.Pop()); + + Assert.Null(stack.Pop()); + Assert.Null(stack.Current); + } + + [Fact] + public void Append_Context_Null() + { + ContextStack stack = new ContextStack(); + ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => stack.Append(null)); + Assert.Equal(typeof(ArgumentNullException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.Equal("context", ex.ParamName); + } + + [Fact] // Item (Int32) + public void Indexer1() + { + ContextStack stack = new ContextStack(); + string one = "one"; + string two = "two"; + + stack.Push(one); + stack.Push(two); + + Assert.Same(two, stack[0]); + Assert.Same(one, stack[1]); + Assert.Null(stack[2]); + Assert.Same(two, stack.Pop()); + Assert.Same(one, stack[0]); + Assert.Null(stack[1]); + Assert.Same(one, stack.Pop()); + Assert.Null(stack[0]); + Assert.Null(stack[1]); + } + + [Fact] // Item (Int32) + public void Indexer1_Level_Negative() + { + ContextStack stack = new ContextStack(); + stack.Push(new Foo()); + ArgumentOutOfRangeException ex; + + ex = Assert.Throws<ArgumentOutOfRangeException>(() => stack[-1]); + Assert.Equal(typeof(ArgumentOutOfRangeException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.Equal(new ArgumentOutOfRangeException("level").Message, ex.Message); + Assert.Equal("level", ex.ParamName); + + + ex = Assert.Throws<ArgumentOutOfRangeException>(() => stack[-5]); + Assert.Equal(typeof(ArgumentOutOfRangeException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.Equal(new ArgumentOutOfRangeException("level").Message, ex.Message); + Assert.Equal("level", ex.ParamName); + } + + [Fact] // Item (Type) + public void Indexer2() + { + ContextStack stack = new ContextStack(); + + Foo foo = new Foo(); + FooBar foobar = new FooBar(); + + stack.Push(foobar); + stack.Push(foo); + Assert.Same(foo, stack[typeof(Foo)]); + Assert.Same(foo, stack[typeof(IFoo)]); + Assert.Same(foo, stack.Pop()); + Assert.Same(foobar, stack[typeof(Foo)]); + Assert.Same(foobar, stack[typeof(FooBar)]); + Assert.Same(foobar, stack[typeof(IFoo)]); + Assert.Null(stack[typeof(string)]); + } + + [Fact] // Item (Type) + public void Indexer2_Type_Null() + { + ContextStack stack = new ContextStack(); + stack.Push(new Foo()); + ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => stack[(Type)null]); + Assert.Equal(typeof(ArgumentNullException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.Equal("type", ex.ParamName); + } + + [Fact] + public void Push_Context_Null() + { + ContextStack stack = new ContextStack(); + ArgumentNullException ex= Assert.Throws<ArgumentNullException>(()=> stack.Push(null)); + Assert.Equal(typeof(ArgumentNullException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.Equal("context", ex.ParamName); + } + + public interface IFoo + { + } + + public class Foo : IFoo + { + } + + public class FooBar : Foo + { + } + } +}
\ No newline at end of file diff --git a/src/System.ComponentModel.TypeConverter/tests/CultureInfoConverterTests.cs b/src/System.ComponentModel.TypeConverter/tests/CultureInfoConverterTests.cs new file mode 100644 index 0000000000..74c1a60a39 --- /dev/null +++ b/src/System.ComponentModel.TypeConverter/tests/CultureInfoConverterTests.cs @@ -0,0 +1,172 @@ +// Licensed to the .NET Foundation under one or more agreements. +// See the LICENSE file in the project root for more information. + +// +// System.ComponentModel.CultureInfoConverter test cases +// +// Authors: +// Gert Driesen (drieseng@users.sourceforge.net) +// +// (c) 2008 Gert Driesen +// + +using System.ComponentModel.Design.Serialization; +using System.Globalization; +using Xunit; + +namespace System.ComponentModel.Tests +{ + public class CultureInfoConverterTest + { + private CultureInfoConverter converter; + + public CultureInfoConverterTest() + { + converter = new CultureInfoConverter(); + } + + [Fact] + public void CanConvertFrom() + { + Assert.True(converter.CanConvertFrom(typeof(string))); + Assert.False(converter.CanConvertFrom(typeof(CultureInfo))); + Assert.False(converter.CanConvertFrom(typeof(object))); + Assert.False(converter.CanConvertFrom(typeof(int))); + } + + [Fact] + public void CanConvertTo() + { + Assert.True(converter.CanConvertTo(typeof(string))); + Assert.False(converter.CanConvertTo(typeof(object))); + Assert.False(converter.CanConvertTo(typeof(CultureInfo))); + Assert.False(converter.CanConvertTo(typeof(int))); + Assert.True(converter.CanConvertTo(typeof(InstanceDescriptor))); + } + + [ActiveIssue(11611, TestPlatforms.AnyUnix)] + [Fact] + public void ConvertFrom_String() + { + CultureInfo c; + + c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture, + String.Empty); + Assert.Equal(CultureInfo.InvariantCulture, c); + + c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture, + "nl-BE"); + Assert.Equal(new CultureInfo("nl-BE"), c); + + Assert.Throws<ArgumentException>(() => c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture, "Dutch (Bel")); + Assert.Throws<ArgumentException>(() => c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture, "duTcH (Bel")); + + c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture, "(Default)"); + Assert.Equal(CultureInfo.InvariantCulture, c); + } + + [ActiveIssue(11611, TestPlatforms.AnyUnix)] + [Fact] + public void ConvertFrom_String_IncompleteName() + { + converter.ConvertFrom(null, CultureInfo.InvariantCulture, + "nl-B"); + } + + [ActiveIssue(11611, TestPlatforms.AnyUnix)] + [Fact] + public void ConvertFrom_String_InvalidCulture() + { + ArgumentException ex; + + ex = Assert.Throws<ArgumentException>(() => converter.ConvertFrom(null, CultureInfo.InvariantCulture, "(default)")); + // The (default) culture cannot be converted to + // a CultureInfo object on this computer + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.True(ex.Message.IndexOf(typeof(CultureInfo).Name) != -1); + Assert.True(ex.Message.IndexOf("(default)") != -1); + Assert.Null(ex.ParamName); + + ex = Assert.Throws<ArgumentException>(() => converter.ConvertFrom(null, CultureInfo.InvariantCulture, " ")); + // The culture cannot be converted to + // a CultureInfo object on this computer + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.True(ex.Message.IndexOf(typeof(CultureInfo).Name) != -1); + Assert.True(ex.Message.IndexOf(" ") != -1); + Assert.Null(ex.ParamName); + + ex = Assert.Throws<ArgumentException>(() => converter.ConvertFrom(null, CultureInfo.InvariantCulture, "\r\n")); + // The \r\n culture cannot be converted to + // a CultureInfo object on this computer + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.True(ex.Message.IndexOf(typeof(CultureInfo).Name) != -1); + Assert.True(ex.Message.IndexOf("\r\n") != -1); + Assert.Null(ex.ParamName); + } + + [Fact] + public void ConvertFrom_Value_Null() + { + NotSupportedException ex = Assert.Throws<NotSupportedException>(() => converter.ConvertFrom(null, CultureInfo.InvariantCulture, (string)null)); + // CultureInfoConverter cannot convert from (null) + Assert.Equal(typeof(NotSupportedException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.True(ex.Message.IndexOf(typeof(CultureInfoConverter).Name) != -1); + Assert.True(ex.Message.IndexOf("(null)") != -1); + } + + [Fact] + public void ConvertToString() + { + Assert.Equal("nl-BE", converter.ConvertToString(null, CultureInfo.InvariantCulture, new MyCultureInfo())); + Assert.Equal("(Default)", converter.ConvertToString(null, CultureInfo.InvariantCulture, null)); + Assert.Equal("(Default)", converter.ConvertToString(null, CultureInfo.InvariantCulture, CultureInfo.InvariantCulture)); + Assert.Equal("nl-BE", converter.ConvertToString(null, CultureInfo.InvariantCulture, new CultureInfo("nl-BE"))); + } + + [Serializable] + private sealed class MyCultureInfo : CultureInfo + { + internal MyCultureInfo() : base("nl-BE") + { + } + + public override string DisplayName + { + get { return "display"; } + } + + public override string EnglishName + { + get { return "english"; } + } + } + + [Fact] + public void GetCultureName() + { + CustomCultureInfoConverter custom_converter = new CustomCultureInfoConverter(); + + CultureInfo fr_culture = CultureInfo.GetCultureInfo("fr-FR"); + Assert.Equal(fr_culture.Name, custom_converter.GetCultureName(fr_culture)); + + CultureInfo es_culture = CultureInfo.GetCultureInfo("es-MX"); + Assert.Equal(es_culture.Name, custom_converter.GetCultureName(es_culture)); + } + + private class CustomCultureInfoConverter : CultureInfoConverter + { + public new string GetCultureName(CultureInfo culture) + { + return base.GetCultureName(culture); + } + } + } +}
\ No newline at end of file diff --git a/src/System.ComponentModel.TypeConverter/tests/InstanceDescriptorTests.cs b/src/System.ComponentModel.TypeConverter/tests/InstanceDescriptorTests.cs new file mode 100644 index 0000000000..1150f8de10 --- /dev/null +++ b/src/System.ComponentModel.TypeConverter/tests/InstanceDescriptorTests.cs @@ -0,0 +1,218 @@ +// Licensed to the .NET Foundation under one or more agreements. +// See the LICENSE file in the project root for more information. + +// +// InstanceDescriptorTest.cs - Unit tests for +// System.ComponentModel.Design.Serialization.InstanceDescriptor +// +// Author: +// Sebastien Pouliot <sebastien@ximian.com> +// +// Copyright (C) 2005 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using Xunit; +using System.ComponentModel.Design.Serialization; +using System.Reflection; +using System.Threading; + +namespace System.ComponentModel.Tests +{ + public class InstanceDescriptorTest + { + private const string url = "http://www.mono-project.com/"; + private ConstructorInfo ci; + + public InstanceDescriptorTest() + { + ci = typeof(Uri).GetConstructor(new Type[1] { typeof(string) }); + } + + [Fact] + public void Constructor0_Arguments_Mismatch() + { + ArgumentException ex = Assert.Throws<ArgumentException>(() => new InstanceDescriptor(ci, null)); + // Length mismatch + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.Null(ex.ParamName); + } + + [Fact] + public void Constructor_MemberInfo_ICollection() + { + InstanceDescriptor id = new InstanceDescriptor(ci, new object[] { url }); + Assert.Equal(1, id.Arguments.Count); + Assert.True(id.IsComplete); + Assert.Same(ci, id.MemberInfo); + Uri uri = (Uri)id.Invoke(); + Assert.Equal(url, uri.AbsoluteUri); + } + + [Fact] + public void Constructor_MemberInfo_Null_Boolean() + { + Assert.Throws<ArgumentException>(() => new InstanceDescriptor(ci, null, false)); + // mismatch for required parameters + } + + [Fact] + public void Constructor_MemberInfo_ICollection_Boolean() + { + InstanceDescriptor id = new InstanceDescriptor(ci, new object[] { url }, false); + Assert.Equal(1, id.Arguments.Count); + Assert.False(id.IsComplete); + Assert.Same(ci, id.MemberInfo); + Uri uri = (Uri)id.Invoke(); + Assert.Equal(url, uri.AbsoluteUri); + } + + [Fact] + public void Field_Arguments_Empty() + { + FieldInfo fi = typeof(Uri).GetField("SchemeDelimiter"); + + InstanceDescriptor id = new InstanceDescriptor(fi, new object[0]); + Assert.Equal(0, id.Arguments.Count); + Assert.True(id.IsComplete); + Assert.Same(fi, id.MemberInfo); + Assert.NotNull(id.Invoke()); + } + + [Fact] + public void Field_Arguments_Mismatch() + { + FieldInfo fi = typeof(Uri).GetField("SchemeDelimiter"); + + ArgumentException ex = Assert.Throws<ArgumentException>(() => new InstanceDescriptor(fi, new object[] { url })); + // Parameter must be static + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.Null(ex.ParamName); + } + + [Fact] + public void Field_Arguments_Null() + { + FieldInfo fi = typeof(Uri).GetField("SchemeDelimiter"); + + InstanceDescriptor id = new InstanceDescriptor(fi, null); + Assert.Equal(0, id.Arguments.Count); + Assert.True(id.IsComplete); + Assert.Same(fi, id.MemberInfo); + Assert.NotNull(id.Invoke()); + } + + [Fact] + public void Field_MemberInfo_NonStatic() + { + FieldInfo fi = typeof(InstanceField).GetField("Name"); + + ArgumentException ex = Assert.Throws<ArgumentException>(() => new InstanceDescriptor(fi, null)); + // Parameter must be static + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.Null(ex.ParamName); + } + + [Fact] + public void Property_Arguments_Mismatch() + { + PropertyInfo pi = typeof(Thread).GetProperty("CurrentThread"); + + InstanceDescriptor id = new InstanceDescriptor(pi, new object[] { url }); + Assert.Equal(1, id.Arguments.Count); + object[] arguments = new object[id.Arguments.Count]; + id.Arguments.CopyTo(arguments, 0); + Assert.Same(url, arguments[0]); + Assert.True(id.IsComplete); + Assert.Same(pi, id.MemberInfo); + + Assert.Throws<TargetParameterCountException>(() => id.Invoke()); + } + + [Fact] + public void Property_Arguments_Null() + { + PropertyInfo pi = typeof(Thread).GetProperty("CurrentThread"); + + InstanceDescriptor id = new InstanceDescriptor(pi, null); + Assert.Equal(0, id.Arguments.Count); + Assert.True(id.IsComplete); + Assert.Same(pi, id.MemberInfo); + Assert.NotNull(id.Invoke()); + } + + [Fact] + public void Property_MemberInfo_NonStatic() + { + PropertyInfo pi = typeof(Uri).GetProperty("Host"); + + ArgumentException ex; + + ex = Assert.Throws<ArgumentException>(() => new InstanceDescriptor(pi, null)); + // Parameter must be static + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.Null(ex.ParamName); + + ex = Assert.Throws<ArgumentException>(() => new InstanceDescriptor(pi, null, false)); + // Parameter must be static + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.Null(ex.ParamName); + } + + [Fact] + public void Property_MemberInfo_WriteOnly() + { + PropertyInfo pi = typeof(WriteOnlyProperty).GetProperty("Name"); + + ArgumentException ex = Assert.Throws<ArgumentException>(() => new InstanceDescriptor(pi, null)); + // Parameter must be readable + Assert.Equal(typeof(ArgumentException), ex.GetType()); + Assert.Null(ex.InnerException); + Assert.NotNull(ex.Message); + Assert.Null(ex.ParamName); + } + + private class WriteOnlyProperty + { + public static string Name + { + set + { + } + } + } + + public class InstanceField + { + public string Name; + } + } +}
\ No newline at end of file diff --git a/src/System.ComponentModel.TypeConverter/tests/LicenseManagerTests.cs b/src/System.ComponentModel.TypeConverter/tests/LicenseManagerTests.cs new file mode 100644 index 0000000000..adc6a95286 --- /dev/null +++ b/src/System.ComponentModel.TypeConverter/tests/LicenseManagerTests.cs @@ -0,0 +1,290 @@ +// Licensed to the .NET Foundation under one or more agreements. +// See the LICENSE file in the project root for more information. + +// +// System.ComponentModel.LicenseManagerTests test cases +// +// Authors: +// Ivan Hamilton (ivan@chimerical.com.au) +// Gonzalo Paniagua Javier (gonzalo@ximian.com) +// Martin Willemoes Hansen (mwh@sysrq.dk) +// +// (c) 2002 Ximian, Inc. (http://www.ximian.com) +// (c) 2003 Martin Willemoes Hansen +// (c) 2004 Ivan Hamilton + +using Xunit; +using System.ComponentModel.Design; + +namespace System.ComponentModel.Tests +{ + public class UnlicensedObject + { + } + + [LicenseProvider(typeof(TestLicenseProvider))] + public class LicensedObject + { + } + + [LicenseProvider(typeof(TestLicenseProvider))] + public class InvalidLicensedObject + { + } + + [LicenseProvider(typeof(TestLicenseProvider))] + public class RuntimeLicensedObject + { + public RuntimeLicensedObject() + { + LicenseManager.Validate(typeof(RuntimeLicensedObject)); + } + public RuntimeLicensedObject(int a) : this() { } + } + + [LicenseProvider(typeof(TestLicenseProvider))] + public class DesigntimeLicensedObject + { + public DesigntimeLicensedObject() + { + LicenseManager.Validate(typeof(DesigntimeLicensedObject)); + } + } + + public class TestLicenseProvider : LicenseProvider + { + + private class TestLicense : License + { + public override void Dispose() + { + } + + public override string LicenseKey + { + get { return "YourLicenseKey"; } + } + } + + public TestLicenseProvider() : base() + { + } + + public override License GetLicense(LicenseContext context, + Type type, + object instance, + bool allowExceptions) + { + if (type.Name.Equals("RuntimeLicensedObject")) + { + if (context.UsageMode != LicenseUsageMode.Runtime) + if (allowExceptions) + throw new LicenseException(type, instance, "License fails because this is a Runtime only license"); + else + return null; + return new TestLicense(); + } + + if (type.Name.Equals("DesigntimeLicensedObject")) + { + if (context.UsageMode != LicenseUsageMode.Designtime) + if (allowExceptions) + throw new LicenseException(type, instance, "License fails because this is a Designtime only license"); + else + return null; + return new TestLicense(); + } + + if (type.Name.Equals("LicensedObject")) + return new TestLicense(); + + if (allowExceptions) + throw new LicenseException(type, instance, "License fails because of class name."); + else + return null; + } + } + + public class LicenseManagerTests + { + [Fact] + public void Test() + { + object lockObject = new object(); + //**DEFAULT CONTEXT & LicenseUsageMode** + //Get CurrentContext, check default type + Assert.Equal("System.ComponentModel.Design.RuntimeLicenseContext", LicenseManager.CurrentContext.GetType().ToString()); + //Read default LicenseUsageMode, check against CurrentContext (LicCont).UsageMode + Assert.Equal(LicenseManager.CurrentContext.UsageMode, LicenseManager.UsageMode); + + //**CHANGING CONTEXT** + //Change the context and check it changes + LicenseContext oldcontext = LicenseManager.CurrentContext; + LicenseContext newcontext = new DesigntimeLicenseContext(); + LicenseManager.CurrentContext = newcontext; + Assert.Equal(newcontext, LicenseManager.CurrentContext); + //Check the UsageMode changed too + Assert.Equal(newcontext.UsageMode, LicenseManager.UsageMode); + //Set Context back to original + LicenseManager.CurrentContext = oldcontext; + //Check it went back + Assert.Equal(oldcontext, LicenseManager.CurrentContext); + //Check the UsageMode changed too + Assert.Equal(oldcontext.UsageMode, LicenseManager.UsageMode); + + //**CONTEXT LOCKING** + //Lock the context + LicenseManager.LockContext(lockObject); + //Try and set new context again, should throw System.InvalidOperationException: The CurrentContext property of the LicenseManager is currently locked and cannot be changed. + bool exceptionThrown = false; + try + { + LicenseManager.CurrentContext = newcontext; + } + catch (Exception e) + { + Assert.Equal(typeof(InvalidOperationException), e.GetType()); + exceptionThrown = true; + } + //Check the exception was thrown + Assert.Equal(true, exceptionThrown); + //Check the context didn't change + Assert.Equal(oldcontext, LicenseManager.CurrentContext); + //Unlock it + LicenseManager.UnlockContext(lockObject); + //Now's unlocked, change it + LicenseManager.CurrentContext = newcontext; + Assert.Equal(newcontext, LicenseManager.CurrentContext); + //Change it back + LicenseManager.CurrentContext = oldcontext; + + + //Lock the context + LicenseManager.LockContext(lockObject); + //Unlock with different "user" should throw System.ArgumentException: The CurrentContext property of the LicenseManager can only be unlocked with the same contextUser. + object wrongLockObject = new object(); + exceptionThrown = false; + try + { + LicenseManager.UnlockContext(wrongLockObject); + } + catch (Exception e) + { + Assert.Equal(typeof(ArgumentException), e.GetType()); + exceptionThrown = true; + } + Assert.Equal(true, exceptionThrown); + //Unlock it + LicenseManager.UnlockContext(lockObject); + + //** bool IsValid(Type); + Assert.Equal(true, LicenseManager.IsLicensed(typeof(UnlicensedObject))); + Assert.Equal(true, LicenseManager.IsLicensed(typeof(LicensedObject))); + Assert.Equal(false, LicenseManager.IsLicensed(typeof(InvalidLicensedObject))); + + Assert.Equal(true, LicenseManager.IsValid(typeof(UnlicensedObject))); + Assert.Equal(true, LicenseManager.IsValid(typeof(LicensedObject))); + Assert.Equal(false, LicenseManager.IsValid(typeof(InvalidLicensedObject))); + + UnlicensedObject unlicensedObject = new UnlicensedObject(); + LicensedObject licensedObject = new LicensedObject(); + InvalidLicensedObject invalidLicensedObject = new InvalidLicensedObject(); + + //** bool IsValid(Type, object, License); + License license = null; + Assert.Equal(true, LicenseManager.IsValid(unlicensedObject.GetType(), unlicensedObject, out license)); + Assert.Equal(null, license); + + license = null; + Assert.Equal(true, LicenseManager.IsValid(licensedObject.GetType(), licensedObject, out license)); + Assert.Equal("TestLicense", license.GetType().Name); + + license = null; + Assert.Equal(false, LicenseManager.IsValid(invalidLicensedObject.GetType(), invalidLicensedObject, out license)); + Assert.Equal(null, license); + + //** void Validate(Type); + //Shouldn't throw exception + LicenseManager.Validate(typeof(UnlicensedObject)); + //Shouldn't throw exception + LicenseManager.Validate(typeof(LicensedObject)); + //Should throw exception + exceptionThrown = false; + try + { + LicenseManager.Validate(typeof(InvalidLicensedObject)); + } + catch (Exception e) + { + Assert.Equal(typeof(LicenseException), e.GetType()); + exceptionThrown = true; + } + //Check the exception was thrown + Assert.Equal(true, exceptionThrown); + + //** License Validate(Type, object); + //Shouldn't throw exception, returns null license + license = LicenseManager.Validate(typeof(UnlicensedObject), unlicensedObject); + Assert.Equal(null, license); + + //Shouldn't throw exception, returns TestLicense license + license = LicenseManager.Validate(typeof(LicensedObject), licensedObject); + Assert.Equal("TestLicense", license.GetType().Name); + + //Should throw exception, returns null license + exceptionThrown = false; + try + { + license = null; + license = LicenseManager.Validate(typeof(InvalidLicensedObject), invalidLicensedObject); + } + catch (Exception e) + { + Assert.Equal(typeof(LicenseException), e.GetType()); + exceptionThrown = true; + } + //Check the exception was thrown + Assert.Equal(true, exceptionThrown); + Assert.Equal(null, license); + + + //** object CreateWithContext (Type, LicenseContext); + object cwc = null; + //Test we can create an unlicensed object with no context + cwc = LicenseManager.CreateWithContext(typeof(UnlicensedObject), null); + Assert.Equal("UnlicensedObject", cwc.GetType().Name); + //Test we can create RunTime with CurrentContext (runtime) + cwc = null; + cwc = LicenseManager.CreateWithContext(typeof(RuntimeLicensedObject), + LicenseManager.CurrentContext); + Assert.Equal("RuntimeLicensedObject", cwc.GetType().Name); + //Test we can't create DesignTime with CurrentContext (runtime) + exceptionThrown = false; + try + { + cwc = null; + cwc = LicenseManager.CreateWithContext(typeof(DesigntimeLicensedObject), LicenseManager.CurrentContext); + } + catch (Exception e) + { + Assert.Equal(typeof(LicenseException), e.GetType()); + exceptionThrown = true; + } + //Check the exception was thrown + Assert.Equal(true, exceptionThrown); + //Test we can create DesignTime with A new DesignTimeContext + cwc = null; + cwc = LicenseManager.CreateWithContext(typeof(DesigntimeLicensedObject), + new DesigntimeLicenseContext()); + Assert.Equal("DesigntimeLicensedObject", cwc.GetType().Name); + + //** object CreateWithContext(Type, LicenseContext, object[]); + //Test we can create RunTime with CurrentContext (runtime) + cwc = null; + cwc = LicenseManager.CreateWithContext(typeof(RuntimeLicensedObject), + LicenseManager.CurrentContext, new object[] { 7 }); + Assert.Equal("RuntimeLicensedObject", cwc.GetType().Name); + + } + } +}
\ No newline at end of file diff --git a/src/System.ComponentModel.TypeConverter/tests/ReferenceConverterTests.cs b/src/System.ComponentModel.TypeConverter/tests/ReferenceConverterTests.cs new file mode 100644 index 0000000000..257c780d34 --- /dev/null +++ b/src/System.ComponentModel.TypeConverter/tests/ReferenceConverterTests.cs @@ -0,0 +1,249 @@ +// Licensed to the .NET Foundation under one or more agreements. +// See the LICENSE file in the project root for more information. + +// +// MonoTests.System.ComponentModel.ReferenceConverterTest +// +// Author: +// Ivan N. Zlatev <contact@i-nz.net> +// +// Copyright (C) 2008 Ivan N. Zlatev +// + +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System.Collections.Generic; +using System.ComponentModel.Design; +using Xunit; + +namespace System.ComponentModel.Tests +{ + public class ReferenceConverterTest + { + + class TestReferenceService : IReferenceService + { + private Dictionary<string, object> references; + + public TestReferenceService() + { + references = new Dictionary<string, object>(); + } + + public void AddReference(string name, object reference) + { + references[name] = reference; + } + + public void ClearReferences() + { + references.Clear(); + } + + public IComponent GetComponent(object reference) + { + return null; + } + + public string GetName(object reference) + { + foreach (KeyValuePair<string, object> entry in references) + { + if (entry.Value == reference) + return entry.Key; + } + + return null; + } + + public object GetReference(string name) + { + if (!references.ContainsKey(name)) + return null; + return references[name]; + } + + public object[] GetReferences() + { + object[] array = new object[references.Values.Count]; + references.Values.CopyTo(array, 0); + return array; + } + + public object[] GetReferences(Type baseType) + { + object[] references = GetReferences(); + + List<object> filtered = new List<object>(); + foreach (object reference in references) + { + if (baseType.IsInstanceOfType(reference)) + filtered.Add(reference); + } + + return filtered.ToArray(); + } + } + + private class TestTypeDescriptorContext : ITypeDescriptorContext + { + private IReferenceService reference_service = null; + private IContainer container = null; + + public TestTypeDescriptorContext() + { + } + + public TestTypeDescriptorContext(IReferenceService referenceService) + { + reference_service = referenceService; + } + + + public IContainer Container + { + get { return container; } + set { container = value; } + } + + public object Instance + { + get { return null; } + } + + public PropertyDescriptor PropertyDescriptor + { + get { return null; } + } + + public void OnComponentChanged() + { + } + + public bool OnComponentChanging() + { + return true; + } + + public object GetService(Type serviceType) + { + if (serviceType == typeof(IReferenceService)) + return reference_service; + return null; + } + } + + private interface ITestInterface + { + } + + private class TestComponent : Component + { + } + + [Fact] + public void CanConvertFrom() + { + ReferenceConverter converter = new ReferenceConverter(typeof(ITestInterface)); + // without context + Assert.False(converter.CanConvertFrom(null, typeof(string))); + // with context + Assert.True(converter.CanConvertFrom(new TestTypeDescriptorContext(), typeof(string))); + } + + [Fact] + public void ConvertFrom() + { + ReferenceConverter converter = new ReferenceConverter(typeof(ITestInterface)); + string referenceName = "reference name"; + // no context + Assert.Null(converter.ConvertFrom(null, null, referenceName)); + + TestComponent component = new TestComponent(); + + // context with IReferenceService + TestReferenceService referenceService = new TestReferenceService(); + referenceService.AddReference(referenceName, component); + TestTypeDescriptorContext context = new TestTypeDescriptorContext(referenceService); + Assert.Same(component, converter.ConvertFrom(context, null, referenceName)); + + // context with Component without IReferenceService + Container container = new Container(); + container.Add(component, referenceName); + context = new TestTypeDescriptorContext(); + context.Container = container; + Assert.Same(component, converter.ConvertFrom(context, null, referenceName)); + } + + [Fact] + public void ConvertTo() + { + ReferenceConverter converter = new ReferenceConverter(typeof(ITestInterface)); + string referenceName = "reference name"; + + Assert.Equal("(none)", (string)converter.ConvertTo(null, null, null, typeof(string))); + + TestComponent component = new TestComponent(); + + // no context + Assert.Equal(String.Empty, (string)converter.ConvertTo(null, null, component, typeof(string))); + + // context with IReferenceService + TestReferenceService referenceService = new TestReferenceService(); + referenceService.AddReference(referenceName, component); + TestTypeDescriptorContext context = new TestTypeDescriptorContext(referenceService); + Assert.Equal(referenceName, (string)converter.ConvertTo(context, null, component, typeof(string))); + + // context with Component without IReferenceService + Container container = new Container(); + container.Add(component, referenceName); + context = new TestTypeDescriptorContext(); + context.Container = container; + Assert.Equal(referenceName, (string)converter.ConvertTo(context, null, component, typeof(string))); + } + + [Fact] + public void CanConvertTo() + { + ReferenceConverter converter = new ReferenceConverter(typeof(ITestInterface)); + Assert.True(converter.CanConvertTo(new TestTypeDescriptorContext(), typeof(string))); + } + + [Fact] + public void GetStandardValues() + { + ReferenceConverter converter = new ReferenceConverter(typeof(TestComponent)); + + TestComponent component1 = new TestComponent(); + TestComponent component2 = new TestComponent(); + TestReferenceService referenceService = new TestReferenceService(); + referenceService.AddReference("reference name 1", component1); + referenceService.AddReference("reference name 2", component2); + ITypeDescriptorContext context = new TestTypeDescriptorContext(referenceService); + + TypeConverter.StandardValuesCollection values = converter.GetStandardValues(context); + Assert.NotNull(values); + // 2 components + 1 null value + Assert.Equal(3, values.Count); + } + } +}
\ No newline at end of file diff --git a/src/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj b/src/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj index 80568fc9de..f7487d4f4d 100644 --- a/src/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj +++ b/src/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj @@ -78,9 +78,14 @@ <Compile Include="Drawing\SizeConverterTests.cs" /> <Compile Include="Drawing\SizeFConverterTests.cs" /> <Compile Include="Drawing\StringTypeConverterTestBase.cs" /> - </ItemGroup> - <ItemGroup Condition="'$(TargetGroup)'==''"> <Compile Include="TimerTests.cs" /> + <Compile Include="BindingListTests.cs" /> + <Compile Include="ContainerTests.cs" /> + <Compile Include="CultureInfoConverterTests.cs" /> + <Compile Include="LicenseManagerTests.cs" /> + <Compile Include="ReferenceConverterTests.cs" /> + <Compile Include="ContextStackTests.cs" /> + <Compile Include="InstanceDescriptorTests.cs" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\pkg\System.ComponentModel.TypeConverter.pkgproj"> |