#141 Implemented INotifyCollectionChanged on ProtocolList and created unit tests

This commit is contained in:
David Sparer
2016-10-05 11:47:22 -06:00
parent 192a718d08
commit 913e0dd3e4
3 changed files with 216 additions and 5 deletions

View File

@@ -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);
}
}
}

View File

@@ -115,6 +115,7 @@
<Compile Include="Config\Serializers\RemoteDesktopConnectionManagerDeserializerTests.cs" />
<Compile Include="Connection\AbstractConnectionInfoDataTests.cs" />
<Compile Include="Connection\ConnectionInfoComparerTests.cs" />
<Compile Include="Connection\Protocol\ProtocolListTests.cs" />
<Compile Include="Tools\ExternalToolsArgumentParserTests.cs" />
<Compile Include="Tree\ConnectionTreeDragAndDropHandlerTests.cs" />
<Compile Include="Tree\ConnectionTreeModelTests.cs" />

View File

@@ -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);
}
}
}