diff --git a/mRemoteNGTests/Container/ContainerInfoTests.cs b/mRemoteNGTests/Container/ContainerInfoTests.cs index 94ee11d8..59b42524 100644 --- a/mRemoteNGTests/Container/ContainerInfoTests.cs +++ b/mRemoteNGTests/Container/ContainerInfoTests.cs @@ -1,4 +1,5 @@ -using mRemoteNG.Connection; +using System.Linq; +using mRemoteNG.Connection; using mRemoteNG.Container; using NUnit.Framework; @@ -231,5 +232,102 @@ namespace mRemoteNGTests.Container var targetsNewIndex = _containerInfo.Children.IndexOf(_con3); Assert.That(targetsNewIndex, Is.EqualTo(targetsIndexBeforeMove)); } + + [Test] + public void WhenChildAlreadyPresentAddChildAtDoesNothing() + { + _containerInfo.AddChild(_con1); + _containerInfo.AddChild(_con2); + _containerInfo.AddChild(_con3); + var indexBeforeAttemptedMove = _containerInfo.Children.IndexOf(_con1); + _containerInfo.AddChildAt(_con1, 2); + var indexAfterAttemptedMove = _containerInfo.Children.IndexOf(_con1); + Assert.That(indexAfterAttemptedMove, Is.EqualTo(indexBeforeAttemptedMove)); + } + + [Test] + public void RemoveChildDoesNothingIfChildNotInList() + { + _containerInfo.AddChild(_con1); + var childListBeforeRemoval = _containerInfo.Children; + _containerInfo.RemoveChild(_con2); + var childListAfterRemoval = _containerInfo.Children; + Assert.That(childListAfterRemoval, Is.EquivalentTo(childListBeforeRemoval)); + } + + [Test] + public void ClonedContainerHasNewConstantId() + { + var clone = _containerInfo.Clone(); + Assert.That(clone.ConstantID, Is.Not.EqualTo(_containerInfo.ConstantID)); + } + + [Test] + public void ClonedContainerContainsClonedChildren() + { + _containerInfo.AddChild(_con1); + _containerInfo.AddChild(_con2); + _containerInfo.AddChild(_con3); + var clone = _containerInfo.Clone() as ContainerInfo; + var clonedChildNames = clone?.Children.Select((node) => node.Name); + var originalChildNames = _containerInfo?.Children.Select((node) => node.Name); + Assert.That(clonedChildNames, Is.EquivalentTo(originalChildNames)); + } + + [Test] + public void HasChildrenReturnsFalseForNoChildren() + { + var hasChildren = _containerInfo.HasChildren(); + Assert.That(hasChildren, Is.False); + } + + [Test] + public void HasChildrenReturnsTrueWhenChildrenPresent() + { + _containerInfo.AddChild(_con1); + var hasChildren = _containerInfo.HasChildren(); + Assert.That(hasChildren, Is.True); + } + + [Test] + public void AddChildAbovePutsNewChildInCorrectLocation() + { + _containerInfo.AddChild(_con1); + var referenceChildIndexBeforeInsertion = _containerInfo.Children.IndexOf(_con1); + _containerInfo.AddChildAbove(_con2, _con1); + var newChildIndex = _containerInfo.Children.IndexOf(_con2); + Assert.That(newChildIndex, Is.EqualTo(referenceChildIndexBeforeInsertion)); + } + + [Test] + public void AddChildAbovePutsNewChildAtEndOfListIfReferenceChildNotInList() + { + _containerInfo.AddChild(_con1); + _containerInfo.AddChildAbove(_con2, _con3); + var newChildIndex = _containerInfo.Children.IndexOf(_con2); + var lastIndex = _containerInfo.Children.Count - 1; + Assert.That(newChildIndex, Is.EqualTo(lastIndex)); + } + + [Test] + public void AddChildBelowPutsNewChildInCorrectLocation() + { + _containerInfo.AddChild(_con1); + _containerInfo.AddChild(_con2); + var referenceChildIndexBeforeInsertion = _containerInfo.Children.IndexOf(_con1); + _containerInfo.AddChildBelow(_con3, _con1); + var newChildIndex = _containerInfo.Children.IndexOf(_con3); + Assert.That(newChildIndex, Is.EqualTo(referenceChildIndexBeforeInsertion + 1)); + } + + [Test] + public void AddChildBelowPutsNewChildAtEndOfListIfReferenceChildNotInList() + { + _containerInfo.AddChild(_con1); + _containerInfo.AddChildBelow(_con2, _con3); + var newChildIndex = _containerInfo.Children.IndexOf(_con2); + var lastIndex = _containerInfo.Children.Count - 1; + Assert.That(newChildIndex, Is.EqualTo(lastIndex)); + } } } \ No newline at end of file diff --git a/mRemoteV1/Container/ContainerInfo.cs b/mRemoteV1/Container/ContainerInfo.cs index ecccd08c..c07bb01f 100644 --- a/mRemoteV1/Container/ContainerInfo.cs +++ b/mRemoteV1/Container/ContainerInfo.cs @@ -14,7 +14,7 @@ namespace mRemoteNG.Container public class ContainerInfo : ConnectionInfo, INotifyCollectionChanged { [Browsable(false)] - public List Children { get; set; } = new List(); + public List Children { get; } = new List(); [Category(""), Browsable(false), ReadOnly(false), Bindable(false), DefaultValue(""), DesignOnly(false)] public bool IsExpanded { get; set; } @@ -52,8 +52,8 @@ namespace mRemoteNG.Container public void AddChildBelow(ConnectionInfo newChildItem, ConnectionInfo reference) { var newChildIndex = Children.IndexOf(reference) + 1; - if (newChildIndex > Children.Count) - newChildIndex = 0; + if (newChildIndex > Children.Count || newChildIndex < 1) + newChildIndex = Children.Count; AddChildAt(newChildItem, newChildIndex); }