From 913e0dd3e495ba51228f408d9f76c7da5163916c Mon Sep 17 00:00:00 2001 From: David Sparer Date: Wed, 5 Oct 2016 11:47:22 -0600 Subject: [PATCH] #141 Implemented INotifyCollectionChanged on ProtocolList and created unit tests --- .../Connection/Protocol/ProtocolListTests.cs | 199 ++++++++++++++++++ mRemoteNGTests/mRemoteNGTests.csproj | 1 + mRemoteV1/Connection/Protocol/ProtocolList.cs | 21 +- 3 files changed, 216 insertions(+), 5 deletions(-) create mode 100644 mRemoteNGTests/Connection/Protocol/ProtocolListTests.cs diff --git a/mRemoteNGTests/Connection/Protocol/ProtocolListTests.cs b/mRemoteNGTests/Connection/Protocol/ProtocolListTests.cs new file mode 100644 index 00000000..25effb61 --- /dev/null +++ b/mRemoteNGTests/Connection/Protocol/ProtocolListTests.cs @@ -0,0 +1,199 @@ +using System.Collections; +using System.Collections.Specialized; +using mRemoteNG.Connection.Protocol; +using mRemoteNG.Connection.Protocol.SSH; +using mRemoteNG.Connection.Protocol.Telnet; +using mRemoteNG.Connection.Protocol.VNC; +using NUnit.Framework; + + +namespace mRemoteNGTests.Connection.Protocol +{ + public class ProtocolListTests + { + private ProtocolList _protocolList; + private ProtocolBase _protocol1; + private ProtocolBase _protocol2; + private ProtocolBase _protocol3; + + + [SetUp] + public void Setup() + { + _protocolList = new ProtocolList(); + _protocol1 = new ProtocolTelnet(); + _protocol2 = new ProtocolSSH2(); + _protocol3 = new ProtocolVNC(); + } + + [TearDown] + public void Teardown() + { + _protocolList = null; + _protocol1 = null; + _protocol2 = null; + _protocol3 = null; + } + + [Test] + public void EmptyWhenInitialized() + { + Assert.That(_protocolList.Count == 0); + } + + [Test] + public void AddAddsObjectToList() + { + _protocolList.Add(_protocol1); + Assert.That(_protocolList[0] == _protocol1); + } + + [Test] + public void AddRangeAddsAllObjects() + { + var protArray = new[] {_protocol1, _protocol2, _protocol3}; + _protocolList.AddRange(protArray); + Assert.That(_protocolList, Is.EquivalentTo(protArray)); + } + + [Test] + public void CountUpdatesToReflectCurrentList() + { + var protArray = new[] { _protocol1, _protocol2, _protocol3 }; + _protocolList.AddRange(protArray); + Assert.That(_protocolList.Count == protArray.Length); + } + + [Test] + public void RemoveRemovesObjectFromList() + { + _protocolList.Add(_protocol1); + _protocolList.Remove(_protocol1); + Assert.That(_protocolList.Count == 0); + } + + [Test] + public void ClearResetsList() + { + var protArray = new[] { _protocol1, _protocol2, _protocol3 }; + _protocolList.AddRange(protArray); + _protocolList.Clear(); + Assert.That(_protocolList.Count == 0); + } + + [Test] + public void IntIndexerReturnsCorrectObject() + { + var protArray = new[] { _protocol1, _protocol2, _protocol3 }; + _protocolList.AddRange(protArray); + Assert.That(_protocolList[1], Is.EqualTo(protArray[1])); + } + + [Test] + public void ObjectIndexerReturnsCorrectObject() + { + var protArray = new[] { _protocol1, _protocol2, _protocol3 }; + _protocolList.AddRange(protArray); + Assert.That(_protocolList[_protocol3], Is.EqualTo(_protocol3)); + } + + [Test] + public void RemovingNonexistantObjectFromListDoesNothing() + { + Assert.DoesNotThrow(()=> _protocolList.Remove(_protocol1)); + } + + [Test] + public void AddRaisesCollectionChangedEvent() + { + var eventWasCalled = false; + _protocolList.CollectionChanged += (sender, args) => eventWasCalled = true; + _protocolList.Add(_protocol1); + Assert.That(eventWasCalled); + } + + [Test] + public void AddCollectionChangedEventContainsAddedObject() + { + IList nodeListFromEvent = new ArrayList(); + _protocolList.CollectionChanged += (sender, args) => nodeListFromEvent = args.NewItems; + _protocolList.Add(_protocol1); + Assert.That(nodeListFromEvent, Is.EquivalentTo(new[] {_protocol1})); + } + + [Test] + public void AddRangeCollectionChangedEventContainsAddedObjects() + { + var protArray = new[] { _protocol1, _protocol2, _protocol3 }; + IList nodeListFromEvent = new ArrayList(); + _protocolList.CollectionChanged += (sender, args) => nodeListFromEvent = args.NewItems; + _protocolList.AddRange(protArray); + Assert.That(nodeListFromEvent, Is.EquivalentTo(protArray)); + } + + [Test] + public void RemoveCollectionChangedEventContainsRemovedObject() + { + IList nodeListFromEvent = new ArrayList(); + _protocolList.Add(_protocol1); + _protocolList.CollectionChanged += (sender, args) => nodeListFromEvent = args.OldItems; + _protocolList.Remove(_protocol1); + Assert.That(nodeListFromEvent, Is.EquivalentTo(new[] { _protocol1 })); + } + + [Test] + public void AttemptingToRemoveNonexistantObjectDoesNotRaiseCollectionChangedEvent() + { + var eventWasCalled = false; + _protocolList.CollectionChanged += (sender, args) => eventWasCalled = true; + _protocolList.Remove(_protocol1); + Assert.That(eventWasCalled == false); + } + + [Test] + public void ClearRaisesCollectionChangedEventWithCorrectAction() + { + var eventWasCalled = false; + _protocolList.CollectionChanged += (sender, args) => eventWasCalled = true; + _protocolList.Clear(); + Assert.That(eventWasCalled); + } + + [Test] + public void AddCollectionChangedEventHasCorrectAction() + { + NotifyCollectionChangedAction collectionChangedAction = NotifyCollectionChangedAction.Move; + _protocolList.CollectionChanged += (sender, args) => collectionChangedAction = args.Action; + _protocolList.Add(_protocol1); + Assert.That(collectionChangedAction == NotifyCollectionChangedAction.Add); + } + + [Test] + public void AddRangeCollectionChangedEventHasCorrectAction() + { + NotifyCollectionChangedAction collectionChangedAction = NotifyCollectionChangedAction.Move; + _protocolList.CollectionChanged += (sender, args) => collectionChangedAction = args.Action; + _protocolList.AddRange(new []{_protocol1}); + Assert.That(collectionChangedAction == NotifyCollectionChangedAction.Add); + } + + [Test] + public void RemoveCollectionChangedEventHasCorrectAction() + { + NotifyCollectionChangedAction collectionChangedAction = NotifyCollectionChangedAction.Move; + _protocolList.Add(_protocol1); + _protocolList.CollectionChanged += (sender, args) => collectionChangedAction = args.Action; + _protocolList.Remove(_protocol1); + Assert.That(collectionChangedAction == NotifyCollectionChangedAction.Remove); + } + + [Test] + public void ClearCollectionChangedEventHasCorrectAction() + { + NotifyCollectionChangedAction collectionChangedAction = NotifyCollectionChangedAction.Move; + _protocolList.CollectionChanged += (sender, args) => collectionChangedAction = args.Action; + _protocolList.Clear(); + Assert.That(collectionChangedAction == NotifyCollectionChangedAction.Reset); + } + } +} \ No newline at end of file diff --git a/mRemoteNGTests/mRemoteNGTests.csproj b/mRemoteNGTests/mRemoteNGTests.csproj index 4ae677eb..8067db7e 100644 --- a/mRemoteNGTests/mRemoteNGTests.csproj +++ b/mRemoteNGTests/mRemoteNGTests.csproj @@ -115,6 +115,7 @@ + diff --git a/mRemoteV1/Connection/Protocol/ProtocolList.cs b/mRemoteV1/Connection/Protocol/ProtocolList.cs index 2239bb94..ab37b04a 100644 --- a/mRemoteV1/Connection/Protocol/ProtocolList.cs +++ b/mRemoteV1/Connection/Protocol/ProtocolList.cs @@ -1,10 +1,11 @@ using System; using System.Collections; +using System.Collections.Specialized; namespace mRemoteNG.Connection.Protocol { - public class ProtocolList : CollectionBase + public class ProtocolList : CollectionBase, INotifyCollectionChanged { public ProtocolBase this[object index] { @@ -13,7 +14,7 @@ namespace mRemoteNG.Connection.Protocol var @base = index as ProtocolBase; if (@base != null) return @base; - return ((ProtocolBase) (List[Convert.ToInt32(index)])); + return (ProtocolBase) List[Convert.ToInt32(index)]; } } @@ -23,7 +24,8 @@ namespace mRemoteNG.Connection.Protocol public void Add(ProtocolBase cProt) { List.Add(cProt); - } + RaiseCollectionChangedEvent(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, cProt)); + } public void AddRange(ProtocolBase[] cProt) { @@ -31,6 +33,7 @@ namespace mRemoteNG.Connection.Protocol { List.Add(cP); } + RaiseCollectionChangedEvent(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, cProt)); } public void Remove(ProtocolBase cProt) @@ -38,7 +41,8 @@ namespace mRemoteNG.Connection.Protocol try { List.Remove(cProt); - } + RaiseCollectionChangedEvent(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, cProt)); + } catch (Exception) { } @@ -47,6 +51,13 @@ namespace mRemoteNG.Connection.Protocol public new void Clear() { List.Clear(); + RaiseCollectionChangedEvent(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } - } + + public event NotifyCollectionChangedEventHandler CollectionChanged; + private void RaiseCollectionChangedEvent(object sender, NotifyCollectionChangedEventArgs args) + { + CollectionChanged?.Invoke(sender, args); + } + } } \ No newline at end of file