// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using Core.Common.Base;
using Core.Common.Controls.TreeView;
using Core.Common.TestUtil;
using NUnit.Framework;
using Rhino.Mocks;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Forms.PresentationObjects;
using Ringtoets.Common.Forms.TreeNodeInfos;
using RingtoetsFormsResources = Ringtoets.Common.Forms.Properties.Resources;
namespace Ringtoets.Common.Forms.Test.TreeNodeInfos
{
[TestFixture]
public class CalculationTreeNodeInfoFactoryTest
{
[Test]
public void TextOfCalculationGroupContextTreeNodeInfo_Always_ReturnsWrappedDataName()
{
// Setup
var mocks = new MockRepository();
var failureMechanismMock = mocks.StrictMock();
mocks.ReplayAll();
var groupName = "testName";
var group = new CalculationGroup
{
Name = groupName
};
var groupContext = new TestCalculationGroupContext(group, failureMechanismMock);
var treeNodeInfo = CalculationTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(null, null, null);
// Call
var text = treeNodeInfo.Text(groupContext);
// Assert
Assert.AreEqual(groupName, text);
mocks.VerifyAll();
}
[Test]
public void ImageOfCalculationGroupContextTreeNodeInfo_Always_ReturnsFolderIcon()
{
// Setup
var treeNodeInfo = CalculationTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(null, null, null);
// Call
var image = treeNodeInfo.Image(null);
// Assert
TestHelper.AssertImagesAreEqual(RingtoetsFormsResources.GeneralFolderIcon, image);
}
[Test]
public void EnsureVisibleOnCreateOfCalculationGroupContextTreeNodeInfo_Always_ReturnsTrue()
{
// Setup
var treeNodeInfo = CalculationTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(null, null, null);
// Call
var result = treeNodeInfo.EnsureVisibleOnCreate(null);
// Assert
Assert.IsTrue(result);
}
[Test]
public void CanRenameNodeOfCalculationGroupContextTreeNodeInfo_NestedCalculationGroup_ReturnsTrue()
{
// Setup
var mocks = new MockRepository();
var calculationGroupMock = mocks.StrictMock();
var failureMechanismMock = mocks.StrictMock();
mocks.ReplayAll();
var groupContext = new TestCalculationGroupContext(calculationGroupMock, failureMechanismMock);
var treeNodeInfo = CalculationTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(null, null, null);
// Call
bool isRenamingAllowed = treeNodeInfo.CanRename(null, groupContext);
// Assert
Assert.IsTrue(isRenamingAllowed);
mocks.VerifyAll();
}
[Test]
public void CanRenameNodeOfCalculationGroupContextTreeNodeInfo_WithoutParentNodeDefaultBehavior_ReturnsFalse()
{
// Setup
var treeNodeInfo = CalculationTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(null, null, null);
// Call
bool isRenamingAllowed = treeNodeInfo.CanRename(null, null);
// Assert
Assert.IsFalse(isRenamingAllowed);
}
[Test]
public void OnNodeRenamedOfCalculationGroupContextTreeNodeInfo_WithData_RenameGroupAndNotifyObservers()
{
// Setup
var mocks = new MockRepository();
var observer = mocks.StrictMock();
var failureMechanismMock = mocks.StrictMock();
observer.Expect(o => o.UpdateObserver());
mocks.ReplayAll();
const string newName = "new name";
var group = new CalculationGroup();
var nodeData = new TestCalculationGroupContext(group, failureMechanismMock);
nodeData.Attach(observer);
var treeNodeInfo = CalculationTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(null, null, null);
// Call
treeNodeInfo.OnNodeRenamed(nodeData, newName);
// Assert
Assert.AreEqual(newName, group.Name);
mocks.VerifyAll();
}
[Test]
public void CanRemoveOfCalculationGroupContextTreeNodeInfo_NestedCalculationGroup_ReturnsTrue()
{
// Setup
var mocks = new MockRepository();
var failureMechanismMock = mocks.StrictMock();
mocks.ReplayAll();
var nodeData = new TestCalculationGroupContext(new CalculationGroup(), failureMechanismMock);
var parentNodeData = new TestCalculationGroupContext(new CalculationGroup(), failureMechanismMock);
var treeNodeInfo = CalculationTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(null, null, null);
// Call
bool isRemovalAllowed = treeNodeInfo.CanRemove(nodeData, parentNodeData);
// Assert
Assert.IsTrue(isRemovalAllowed);
mocks.VerifyAll();
}
[Test]
public void CanRemoveOfCalculationGroupContextTreeNodeInfo_WithoutParentNodeDefaultBehavior_ReturnsFalse()
{
// Setup
var mocks = new MockRepository();
var failureMechanismMock = mocks.StrictMock();
mocks.ReplayAll();
var nodeData = new TestCalculationGroupContext(new CalculationGroup(), failureMechanismMock);
var treeNodeInfo = CalculationTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(null, null, null);
// Call
bool isRemovalAllowed = treeNodeInfo.CanRemove(nodeData, null);
// Assert
Assert.IsFalse(isRemovalAllowed);
mocks.VerifyAll();
}
[Test]
public void CanDragOfCalculationGroupContextTreeNodeInfo_NestedCalculationGroup_ReturnsTrue()
{
// Setup
var mocks = new MockRepository();
var failureMechanismMock = mocks.StrictMock();
mocks.ReplayAll();
var groupContext = new TestCalculationGroupContext(new CalculationGroup(), failureMechanismMock);
var parentGroupContext = new TestCalculationGroupContext(new CalculationGroup(), failureMechanismMock);
var treeNodeInfo = CalculationTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(null, null, null);
// Call
var canDrag = treeNodeInfo.CanDrag(groupContext, parentGroupContext);
// Assert
Assert.IsTrue(canDrag);
}
[Test]
public void CanDragOfCalculationGroupContextTreeNodeInfo_WithoutParentNodeDefaultBehavior_ReturnsFalse()
{
// Setup
var mocks = new MockRepository();
var failureMechanismMock = mocks.StrictMock();
mocks.ReplayAll();
var groupContext = new TestCalculationGroupContext(new CalculationGroup(), failureMechanismMock);
var treeNodeInfo = CalculationTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(null, null, null);
// Call
var canDrag = treeNodeInfo.CanDrag(groupContext, null);
// Assert
Assert.IsFalse(canDrag);
}
[Test]
[Combinatorial]
public void CanDropOrCanInsertOfCalculationGroupContextTreeNodeInfo_DraggingCalculationItemContextOntoGroupNotContainingItem_ReturnsTrue(
[Values(DragDropTestMethod.CanDrop, DragDropTestMethod.CanInsert)] DragDropTestMethod methodToTest,
[Values(CalculationItemType.Calculation, CalculationItemType.Group)] CalculationItemType draggedItemType)
{
// Setup
var mocks = new MockRepository();
var failureMechanism = mocks.StrictMock();
mocks.ReplayAll();
object draggedItemContext;
ICalculationBase draggedItem;
CreateCalculationItemAndContext(draggedItemType, out draggedItem, out draggedItemContext, failureMechanism);
CalculationGroup targetGroup;
TestCalculationGroupContext targetGroupContext;
CreateCalculationGroupAndContext(out targetGroup, out targetGroupContext, failureMechanism);
var treeNodeInfo = CalculationTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(null, null, null);
switch (methodToTest)
{
case DragDropTestMethod.CanDrop:
// Call
var canDrop = treeNodeInfo.CanDrop(draggedItemContext, targetGroupContext);
// Assert
Assert.IsTrue(canDrop);
break;
case DragDropTestMethod.CanInsert:
// Call
bool canInsert = treeNodeInfo.CanInsert(draggedItemContext, targetGroupContext);
// Assert
Assert.IsTrue(canInsert);
break;
default:
Assert.Fail(methodToTest + " not supported.");
break;
}
mocks.VerifyAll();
}
[Test]
[Combinatorial]
public void CanDropOrInsertOfCalculationGroupContextTreeNodeInfo_DraggingCalculationItemContextOntoGroupNotContainingItemOtherFailureMechanism_ReturnsFalse(
[Values(DragDropTestMethod.CanDrop, DragDropTestMethod.CanInsert)] DragDropTestMethod methodToTest,
[Values(CalculationItemType.Calculation, CalculationItemType.Group)] CalculationItemType draggedItemType)
{
// Setup
var mocks = new MockRepository();
var sourceFailureMechanism = mocks.StrictMock();
var targetFailureMechanism = mocks.StrictMock();
mocks.ReplayAll();
object draggedItemContext;
ICalculationBase draggedItem;
CreateCalculationItemAndContext(draggedItemType, out draggedItem, out draggedItemContext, targetFailureMechanism);
CalculationGroup targetGroup;
TestCalculationGroupContext targetGroupContext;
CreateCalculationGroupAndContext(out targetGroup, out targetGroupContext, sourceFailureMechanism);
var treeNodeInfo = CalculationTreeNodeInfoFactory.CreateCalculationGroupContextTreeNodeInfo(null, null, null);
switch (methodToTest)
{
case DragDropTestMethod.CanDrop:
// Call
var canDrop = treeNodeInfo.CanDrop(draggedItemContext, targetGroupContext);
// Assert
Assert.IsFalse(canDrop);
break;
case DragDropTestMethod.CanInsert:
// Call
bool canInsert = treeNodeInfo.CanInsert(draggedItemContext, targetGroupContext);
// Assert
Assert.IsFalse(canInsert);
break;
default:
Assert.Fail(methodToTest + " not supported.");
break;
}
mocks.VerifyAll();
}
///
/// Creates an instance of and the corresponding .
///
/// The created group without any children.
/// The context object for , without any other data.
/// The failure mechanism the item and context it belong to.
private void CreateCalculationGroupAndContext(out CalculationGroup data, out TestCalculationGroupContext dataContext, IFailureMechanism failureMechanism)
{
data = new CalculationGroup();
dataContext = new TestCalculationGroupContext(data, failureMechanism);
}
///
/// Creates an instance of and the corresponding context.
///
/// Defines the implementation of to be constructed.
/// Output: The concrete create class based on .
/// Output: The context corresponding with .
/// The failure mechanism the item and context belong to.
/// Optional: The name of .
///
private static void CreateCalculationItemAndContext(CalculationItemType type, out ICalculationBase data, out object dataContext, IFailureMechanism failureMechanism, string initialName = null)
{
switch (type)
{
case CalculationItemType.Calculation:
var calculation = new TestCalculation();
if (initialName != null)
{
calculation.Name = initialName;
}
data = calculation;
dataContext = new TestCalculationContext(calculation, failureMechanism);
break;
case CalculationItemType.Group:
var group = new CalculationGroup();
if (initialName != null)
{
group.Name = initialName;
}
data = group;
dataContext = new TestCalculationGroupContext(group, failureMechanism);
break;
default:
throw new NotSupportedException();
}
}
# region Nested types
private class TestCalculationGroupContext : Observable, ICalculationContext
{
public TestCalculationGroupContext(CalculationGroup wrappedData, IFailureMechanism failureMechanism)
{
WrappedData = wrappedData;
FailureMechanism = failureMechanism;
}
public CalculationGroup WrappedData { get; private set; }
public IFailureMechanism FailureMechanism { get; private set; }
}
private class TestCalculationContext : Observable, ICalculationContext
{
public TestCalculationContext(TestCalculation wrappedData, IFailureMechanism failureMechanism)
{
WrappedData = wrappedData;
FailureMechanism = failureMechanism;
}
public TestCalculation WrappedData { get; private set; }
public IFailureMechanism FailureMechanism { get; private set; }
}
private class TestCalculation : Observable, ICalculation
{
public string Name { get; set; }
public string Comments { get; set; }
public bool HasOutput
{
get
{
return false;
}
}
public void ClearOutput()
{
}
public void ClearHydraulicBoundaryLocation()
{
}
public ICalculationInput GetObservableInput()
{
return null;
}
}
///
/// Type indicator for testing methods on .
///
public enum DragDropTestMethod
{
///
/// Indicates .
///
CanDrop,
///
/// Indicates .
///
CanInsert
}
///
/// Type indicator for implementations of to be created in a test.
///
public enum CalculationItemType
{
///
/// Indicates .
///
Calculation,
///
/// Indicates .
///
Group
}
# endregion
}
}