// 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 System.Linq;
using System.Windows.Forms;
using Core.Common.Base;
using Core.Common.Controls.TreeView;
using Core.Common.Gui;
using Core.Common.Gui.Commands;
using Core.Common.Gui.ContextMenu;
using Core.Common.Gui.TestUtil.ContextMenu;
using Core.Common.TestUtil;
using NUnit.Framework;
using Rhino.Mocks;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.GrassCoverErosionInwards.Data;
using Ringtoets.GrassCoverErosionInwards.Forms.PresentationObjects;
using Ringtoets.GrassCoverErosionInwards.Plugin;
using CoreCommonGuiResources = Core.Common.Gui.Properties.Resources;
using RingtoetsFormsResources = Ringtoets.Common.Forms.Properties.Resources;
using GrassCoverErosionInwardsFormResources = Ringtoets.GrassCoverErosionInwards.Forms.Properties.Resources;
namespace Ringtoets.GrassCoverErosionInwards.Forms.Test.TreeNodeInfos
{
[TestFixture]
public class GrassCoverErosionInwardsCalculationGroupContextTreeNodeInfoTest
{
private IGui gui;
private TreeNodeInfo info;
private MockRepository mocks;
private GrassCoverErosionInwardsGuiPlugin plugin;
[SetUp]
public void SetUp()
{
mocks = new MockRepository();
gui = mocks.StrictMock();
plugin = new GrassCoverErosionInwardsGuiPlugin
{
Gui = gui
};
info = plugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(GrassCoverErosionInwardsCalculationGroupContext));
}
[Test]
public void Initialized_Always_ExpectedPropertiesSet()
{
// Assert
Assert.AreEqual(typeof(GrassCoverErosionInwardsCalculationGroupContext), info.TagType);
Assert.IsNull(info.ForeColor);
Assert.IsNull(info.CanCheck);
Assert.IsNull(info.IsChecked);
Assert.IsNull(info.OnNodeChecked);
}
[Test]
public void ChildNodeObjects_EmptyGroup_ReturnEmpty()
{
// Setup
var group = new CalculationGroup();
var failureMechanismMock = mocks.StrictMock();
var assessmentSectionMock = mocks.StrictMock();
mocks.ReplayAll();
var groupContext = new GrassCoverErosionInwardsCalculationGroupContext(group,
failureMechanismMock,
assessmentSectionMock);
// Call
var children = info.ChildNodeObjects(groupContext);
// Assert
CollectionAssert.IsEmpty(children);
mocks.VerifyAll();
}
[Test]
public void ChildNodeObjects_GroupWithMixedContents_ReturnChildren()
{
// Setup
var failureMechanismMock = mocks.StrictMock();
var assessmentSectionMock = mocks.StrictMock();
mocks.ReplayAll();
var group = new CalculationGroup();
var childGroup = new CalculationGroup();
var calculationItem = mocks.StrictMock();
var childCalculation = new GrassCoverErosionInwardsCalculation(failureMechanismMock.GeneralInput);
group.Children.Add(childGroup);
group.Children.Add(calculationItem);
group.Children.Add(childCalculation);
var groupContext = new GrassCoverErosionInwardsCalculationGroupContext(group,
failureMechanismMock,
assessmentSectionMock);
// Call
var children = info.ChildNodeObjects(groupContext).ToArray();
// Assert
Assert.AreEqual(group.Children.Count, children.Length);
var calculationGroupContext = (GrassCoverErosionInwardsCalculationGroupContext) children[0];
Assert.AreSame(childGroup, calculationGroupContext.WrappedData);
Assert.AreSame(failureMechanismMock, calculationGroupContext.FailureMechanism);
Assert.AreSame(assessmentSectionMock, calculationGroupContext.AssessmentSection);
Assert.AreSame(calculationItem, children[1]);
var calculationContext = (GrassCoverErosionInwardsCalculationContext) children[2];
Assert.AreSame(childCalculation, calculationContext.WrappedData);
Assert.AreSame(assessmentSectionMock, calculationContext.AssessmentSection);
}
[Test]
public void ContextmenuStrip_FailureMechanismContextParent_ReturnContextMenuWithoutRenameRemove()
{
// Setup
var group = new CalculationGroup();
var failureMechanismMock = mocks.StrictMock();
var assessmentSectionMock = mocks.StrictMock();
var parentData = new GrassCoverErosionInwardsFailureMechanismContext(failureMechanismMock, assessmentSectionMock);
var nodeData = new GrassCoverErosionInwardsCalculationGroupContext(group,
failureMechanismMock,
assessmentSectionMock);
var applicationFeatureCommandHandler = mocks.Stub();
var exportImportHandler = mocks.Stub();
var viewCommandsHandler = mocks.StrictMock();
var treeViewControl = mocks.StrictMock();
var menuBuilder = new ContextMenuBuilder(applicationFeatureCommandHandler, exportImportHandler, viewCommandsHandler, nodeData, treeViewControl);
gui.Expect(g => g.Get(nodeData, treeViewControl)).Return(menuBuilder);
treeViewControl.Expect(tvc => tvc.CanExpandOrCollapseForData(nodeData)).Repeat.Twice().Return(false);
viewCommandsHandler.Expect(vc => vc.CanOpenViewFor(nodeData)).Return(false);
mocks.ReplayAll();
// Call
ContextMenuStrip menu = info.ContextMenuStrip(nodeData, parentData, treeViewControl);
// Assert
var mainCalculationGroupContextItemOffset = 2;
Assert.AreEqual(12, menu.Items.Count);
TestHelper.AssertContextMenuStripContainsItem(menu, 0,
CoreCommonGuiResources.Open,
CoreCommonGuiResources.Open_ToolTip,
CoreCommonGuiResources.OpenIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuAddCalculationGroupIndex + mainCalculationGroupContextItemOffset,
RingtoetsFormsResources.CalculationGroup_Add_CalculationGroup,
"Voeg een nieuwe berekeningsmap toe aan deze berekeningsmap.",
RingtoetsFormsResources.AddFolderIcon);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuAddCalculationItemIndex + mainCalculationGroupContextItemOffset,
RingtoetsFormsResources.CalculationGroup_Add_Calculation,
"Voeg een nieuwe berekening toe aan deze berekeningsmap.",
GrassCoverErosionInwardsFormResources.CalculationIcon);
TestHelper.AssertContextMenuStripContainsItem(menu, 5,
CoreCommonGuiResources.Import,
CoreCommonGuiResources.Import_ToolTip,
CoreCommonGuiResources.ImportIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(menu, 6,
CoreCommonGuiResources.Export,
CoreCommonGuiResources.Export_ToolTip,
CoreCommonGuiResources.ExportIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(menu, 8,
CoreCommonGuiResources.Expand_all,
CoreCommonGuiResources.Expand_all_ToolTip,
CoreCommonGuiResources.ExpandAllIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(menu, 9,
CoreCommonGuiResources.Collapse_all,
CoreCommonGuiResources.Collapse_all_ToolTip,
CoreCommonGuiResources.CollapseAllIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(menu, 11,
CoreCommonGuiResources.Properties,
CoreCommonGuiResources.Properties_ToolTip,
CoreCommonGuiResources.PropertiesHS,
false);
CollectionAssert.AllItemsAreInstancesOfType(new[]
{
menu.Items[1],
menu.Items[4],
menu.Items[7],
menu.Items[10]
}, typeof(ToolStripSeparator));
mocks.VerifyAll();
}
[Test]
public void ContextmenuStrip_ChildOfGroup_ReturnContextMenuWithAllItems()
{
// Setup
var parentGroup = new CalculationGroup();
var group = new CalculationGroup();
parentGroup.Children.Add(group);
var failureMechanismMock = mocks.StrictMock();
var assessmentSectionMock = mocks.StrictMock();
var parentData = new GrassCoverErosionInwardsCalculationGroupContext(parentGroup,
failureMechanismMock,
assessmentSectionMock);
var nodeData = new GrassCoverErosionInwardsCalculationGroupContext(group,
failureMechanismMock,
assessmentSectionMock);
var applicationFeatureCommandHandler = mocks.Stub();
var exportImportHandler = mocks.Stub();
var viewCommandsHandler = mocks.StrictMock();
var treeViewControl = mocks.StrictMock();
var menuBuilder = new ContextMenuBuilder(applicationFeatureCommandHandler, exportImportHandler, viewCommandsHandler, nodeData, treeViewControl);
gui.Expect(g => g.Get(nodeData, treeViewControl)).Return(menuBuilder);
treeViewControl.Expect(tvc => tvc.CanRenameNodeForData(nodeData)).Return(true);
treeViewControl.Expect(tvc => tvc.CanRemoveNodeForData(nodeData)).Return(true);
treeViewControl.Expect(tvc => tvc.CanExpandOrCollapseForData(nodeData)).Repeat.Twice().Return(false);
mocks.ReplayAll();
// Call
ContextMenuStrip menu = info.ContextMenuStrip(nodeData, parentData, treeViewControl);
// Assert
Assert.AreEqual(13, menu.Items.Count);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuAddCalculationGroupIndex,
RingtoetsFormsResources.CalculationGroup_Add_CalculationGroup,
"Voeg een nieuwe berekeningsmap toe aan deze berekeningsmap.",
RingtoetsFormsResources.AddFolderIcon);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuAddCalculationItemIndex,
RingtoetsFormsResources.CalculationGroup_Add_Calculation,
"Voeg een nieuwe berekening toe aan deze berekeningsmap.",
GrassCoverErosionInwardsFormResources.CalculationIcon);
TestHelper.AssertContextMenuStripContainsItem(menu, 3,
CoreCommonGuiResources.Rename,
CoreCommonGuiResources.Rename_ToolTip,
CoreCommonGuiResources.RenameIcon);
TestHelper.AssertContextMenuStripContainsItem(menu, 4,
CoreCommonGuiResources.Delete,
CoreCommonGuiResources.Delete_ToolTip,
CoreCommonGuiResources.DeleteIcon);
TestHelper.AssertContextMenuStripContainsItem(menu, 6,
CoreCommonGuiResources.Import,
CoreCommonGuiResources.Import_ToolTip,
CoreCommonGuiResources.ImportIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(menu, 7,
CoreCommonGuiResources.Export,
CoreCommonGuiResources.Export_ToolTip,
CoreCommonGuiResources.ExportIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(menu, 9,
CoreCommonGuiResources.Expand_all,
CoreCommonGuiResources.Expand_all_ToolTip,
CoreCommonGuiResources.ExpandAllIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(menu, 10,
CoreCommonGuiResources.Collapse_all,
CoreCommonGuiResources.Collapse_all_ToolTip,
CoreCommonGuiResources.CollapseAllIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(menu, 12,
CoreCommonGuiResources.Properties,
CoreCommonGuiResources.Properties_ToolTip,
CoreCommonGuiResources.PropertiesHS,
false);
CollectionAssert.AllItemsAreInstancesOfType(new[]
{
menu.Items[2],
menu.Items[5],
menu.Items[8],
menu.Items[11]
}, typeof(ToolStripSeparator));
mocks.VerifyAll();
}
[Test]
public void ContextMenuStrip_ClickOnAddGroupItem_AddGroupToCalculationGroupAndNotifyObservers()
{
// Setup
var menuBuilder = new CustomItemsOnlyContextMenuBuilder();
var group = new CalculationGroup();
var parentGroup = new CalculationGroup();
var failureMechanismMock = mocks.StrictMock();
var assessmentSectionMock = mocks.StrictMock();
var nodeData = new GrassCoverErosionInwardsCalculationGroupContext(group,
failureMechanismMock,
assessmentSectionMock);
var parentNodeData = new GrassCoverErosionInwardsCalculationGroupContext(parentGroup,
failureMechanismMock,
assessmentSectionMock);
var calculationItem = mocks.Stub();
calculationItem.Stub(ci => ci.Name).Return("Nieuwe map");
var observer = mocks.StrictMock();
observer.Expect(o => o.UpdateObserver());
var treeViewControl = mocks.StrictMock();
gui.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder);
mocks.ReplayAll();
group.Children.Add(calculationItem);
nodeData.Attach(observer);
ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, parentNodeData, treeViewControl);
// Precondition
Assert.AreEqual(1, group.Children.Count);
// Call
contextMenu.Items[contextMenuAddCalculationGroupIndex].PerformClick();
// Assert
Assert.AreEqual(2, group.Children.Count);
var newlyAddedItem = group.Children.Last();
Assert.IsInstanceOf(newlyAddedItem);
Assert.AreEqual("Nieuwe map (1)", newlyAddedItem.Name,
"An item with the same name default name already exists, therefore '(1)' needs to be appended.");
mocks.VerifyAll();
}
[Test]
public void ContextMenuStrip_ClickOnAddCalculationItem_AddCalculationToCalculationGroupAndNotifyObservers()
{
// Setup
var menuBuilder = new CustomItemsOnlyContextMenuBuilder();
var group = new CalculationGroup();
var parentGroup = new CalculationGroup();
var failureMechanismMock = mocks.StrictMock();
var assessmentSectionMock = mocks.StrictMock();
var nodeData = new GrassCoverErosionInwardsCalculationGroupContext(group,
failureMechanismMock,
assessmentSectionMock);
var parentNodeData = new GrassCoverErosionInwardsCalculationGroupContext(parentGroup,
failureMechanismMock,
assessmentSectionMock);
var calculationItem = mocks.Stub();
calculationItem.Stub(ci => ci.Name).Return("Nieuwe berekening");
var observer = mocks.StrictMock();
observer.Expect(o => o.UpdateObserver());
var treeViewControl = mocks.StrictMock();
gui.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder);
mocks.ReplayAll();
group.Children.Add(calculationItem);
nodeData.Attach(observer);
var contextMenu = info.ContextMenuStrip(nodeData, parentNodeData, treeViewControl);
// Precondition
Assert.AreEqual(1, group.Children.Count);
// Call
contextMenu.Items[contextMenuAddCalculationItemIndex].PerformClick();
// Assert
Assert.AreEqual(2, group.Children.Count);
var newlyAddedItem = group.Children.Last();
Assert.IsInstanceOf(newlyAddedItem);
Assert.AreEqual("Nieuwe berekening (1)", newlyAddedItem.Name,
"An item with the same name default name already exists, therefore '(1)' needs to be appended.");
mocks.VerifyAll();
}
[Test]
public void OnNodeRemoved_ParentIsGrassCoverErosionInwardsCalculationGroupContainingGroup_RemoveGroupAndNotifyObservers()
{
// Setup
var observer = mocks.StrictMock();
observer.Expect(o => o.UpdateObserver());
var group = new CalculationGroup();
var failureMechanismMock = mocks.StrictMock();
var assessmentSectionMock = mocks.StrictMock();
mocks.ReplayAll();
var nodeData = new GrassCoverErosionInwardsCalculationGroupContext(group,
failureMechanismMock,
assessmentSectionMock);
var parentGroup = new CalculationGroup();
parentGroup.Children.Add(group);
var parentNodeData = new GrassCoverErosionInwardsCalculationGroupContext(parentGroup,
failureMechanismMock,
assessmentSectionMock);
parentNodeData.Attach(observer);
// Precondition
Assert.IsTrue(info.CanRemove(nodeData, parentNodeData));
// Call
info.OnNodeRemoved(nodeData, parentNodeData);
// Assert
CollectionAssert.DoesNotContain(parentGroup.Children, group);
mocks.VerifyAll();
}
[Test]
public void OnNodeRemoved_ParentIsGrassCoverErosionInwardsCalculationGroupContainingGroupContainingCalculations_RemoveGroupAndCalculationsAndNotifyObservers()
{
// Setup
var observer = mocks.StrictMock();
observer.Expect(o => o.UpdateObserver());
var group = new CalculationGroup();
var failureMechanismMock = mocks.StrictMock();
var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput());
group.Children.Add(calculation);
var assessmentSectionMock = mocks.StrictMock();
mocks.ReplayAll();
var nodeData = new GrassCoverErosionInwardsCalculationGroupContext(group,
failureMechanismMock,
assessmentSectionMock);
var parentGroup = new CalculationGroup();
parentGroup.Children.Add(group);
var parentNodeData = new GrassCoverErosionInwardsCalculationGroupContext(parentGroup,
failureMechanismMock,
assessmentSectionMock);
parentNodeData.Attach(observer);
// Precondition
Assert.IsTrue(info.CanRemove(nodeData, parentNodeData));
// Call
info.OnNodeRemoved(nodeData, parentNodeData);
// Assert
CollectionAssert.DoesNotContain(parentGroup.Children, group);
mocks.VerifyAll();
}
[Test]
[Combinatorial]
public void CanDropOrCanInsert_DraggingPipingCalculationItemContextOntoGroupNotContainingItem_ReturnTrue(
[Values(DragDropTestMethod.CanDrop, DragDropTestMethod.CanInsert)] DragDropTestMethod methodToTest,
[Values(CalculationType.Calculation, CalculationType.Group)] CalculationType draggedItemType)
{
// Setup
ICalculationBase draggedItem;
object draggedItemContext;
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
var assessmentSection = mocks.StrictMock();
mocks.ReplayAll();
CreateCalculationAndContext(draggedItemType, out draggedItem, out draggedItemContext, failureMechanism, assessmentSection);
CalculationGroup targetGroup;
GrassCoverErosionInwardsCalculationGroupContext targetGroupContext;
CreateCalculationGroupAndContext(out targetGroup, out targetGroupContext, failureMechanism, assessmentSection);
failureMechanism.CalculationsGroup.Children.Add(draggedItem);
failureMechanism.CalculationsGroup.Children.Add(targetGroup);
switch (methodToTest)
{
case DragDropTestMethod.CanDrop:
// Call
var canDrop = info.CanDrop(draggedItemContext, targetGroupContext);
// Assert
Assert.IsTrue(canDrop);
break;
case DragDropTestMethod.CanInsert:
// Call
bool canInsert = info.CanInsert(draggedItemContext, targetGroupContext);
// Assert
Assert.IsTrue(canInsert);
break;
default:
Assert.Fail(methodToTest + " not supported.");
break;
}
mocks.VerifyAll();
}
[Test]
[Combinatorial]
public void CanDropOrInsert_DraggingCalculationItemContextOntoGroupNotContainingItemOtherFailureMechanism_ReturnFalse(
[Values(DragDropTestMethod.CanDrop, DragDropTestMethod.CanInsert)] DragDropTestMethod methodToTest,
[Values(CalculationType.Calculation, CalculationType.Group)] CalculationType draggedItemType)
{
// Setup
ICalculationBase draggedItem;
object draggedItemContext;
var targetFailureMechanism = new GrassCoverErosionInwardsFailureMechanism();
var assessmentSection = mocks.StrictMock();
mocks.ReplayAll();
CreateCalculationAndContext(draggedItemType, out draggedItem, out draggedItemContext, targetFailureMechanism, assessmentSection);
var sourceFailureMechanism = new GrassCoverErosionInwardsFailureMechanism();
sourceFailureMechanism.CalculationsGroup.Children.Add(draggedItem);
CalculationGroup targetGroup;
GrassCoverErosionInwardsCalculationGroupContext targetGroupContext;
CreateCalculationGroupAndContext(out targetGroup, out targetGroupContext, sourceFailureMechanism, assessmentSection);
targetFailureMechanism.CalculationsGroup.Children.Add(targetGroup);
switch (methodToTest)
{
case DragDropTestMethod.CanDrop:
// Call
var canDrop = info.CanDrop(draggedItemContext, targetGroupContext);
// Assert
Assert.IsFalse(canDrop);
break;
case DragDropTestMethod.CanInsert:
// Call
bool canInsert = info.CanInsert(draggedItemContext, targetGroupContext);
// Assert
Assert.IsFalse(canInsert);
break;
default:
Assert.Fail(methodToTest + " not supported.");
break;
}
mocks.VerifyAll();
}
[Test]
[Combinatorial]
public void OnDrop_DraggingPipingCalculationItemContextOntoGroupEnd_MoveCalculationItemInstanceToNewGroup(
[Values(CalculationType.Calculation, CalculationType.Group)] CalculationType draggedItemType)
{
// Setup
var treeViewControlMock = mocks.StrictMock();
var pipingFailureMechanismMock = mocks.StrictMock();
var assessmentSection = mocks.StrictMock();
ICalculationBase draggedItem;
object draggedItemContext;
CreateCalculationAndContext(draggedItemType, out draggedItem, out draggedItemContext, pipingFailureMechanismMock, assessmentSection);
CalculationGroup originalOwnerGroup;
GrassCoverErosionInwardsCalculationGroupContext originalOwnerGroupContext;
CreateCalculationGroupAndContext(out originalOwnerGroup, out originalOwnerGroupContext, pipingFailureMechanismMock, assessmentSection);
originalOwnerGroup.Children.Add(draggedItem);
CalculationGroup newOwnerGroup;
GrassCoverErosionInwardsCalculationGroupContext newOwnerGroupContext;
CreateCalculationGroupAndContext(out newOwnerGroup, out newOwnerGroupContext, pipingFailureMechanismMock, assessmentSection);
var originalOwnerObserver = mocks.StrictMock();
originalOwnerObserver.Expect(o => o.UpdateObserver());
var newOwnerObserver = mocks.StrictMock();
newOwnerObserver.Expect(o => o.UpdateObserver());
mocks.ReplayAll();
originalOwnerGroup.Attach(originalOwnerObserver);
newOwnerGroup.Attach(newOwnerObserver);
// Precondition
CollectionAssert.Contains(originalOwnerGroup.Children, draggedItem);
CollectionAssert.DoesNotContain(newOwnerGroup.Children, draggedItem);
// Call
info.OnDrop(draggedItemContext, newOwnerGroupContext, originalOwnerGroupContext, 0, treeViewControlMock);
// Assert
CollectionAssert.DoesNotContain(originalOwnerGroup.Children, draggedItem);
CollectionAssert.Contains(newOwnerGroup.Children, draggedItem);
Assert.AreSame(draggedItem, newOwnerGroup.Children.Last(),
"Dragging node at the end of the target PipingCalculationGroup should put the dragged data at the end of 'newOwnerGroup'.");
mocks.VerifyAll();
}
[Test]
[Combinatorial]
public void OnDrop_InsertingPipingCalculationItemContextAtDifferentLocationWithinSameGroup_ChangeItemIndexOfCalculationItem(
[Values(CalculationType.Calculation, CalculationType.Group)] CalculationType draggedItemType,
[Values(0, 2)] int newIndex)
{
// Setup
var treeViewControlMock = mocks.StrictMock();
var pipingFailureMechanismMock = mocks.StrictMock();
var assessmentSection = mocks.StrictMock();
const string name = "Very cool name";
ICalculationBase draggedItem;
object draggedItemContext;
CreateCalculationAndContext(draggedItemType, out draggedItem, out draggedItemContext, pipingFailureMechanismMock, assessmentSection, name);
var existingItemStub = mocks.Stub();
existingItemStub.Stub(ci => ci.Name).Return("");
CalculationGroup originalOwnerGroup;
GrassCoverErosionInwardsCalculationGroupContext originalOwnerGroupContext;
CreateCalculationGroupAndContext(out originalOwnerGroup, out originalOwnerGroupContext, pipingFailureMechanismMock, assessmentSection);
originalOwnerGroup.Children.Add(existingItemStub);
originalOwnerGroup.Children.Add(draggedItem);
originalOwnerGroup.Children.Add(existingItemStub);
var originalOwnerObserver = mocks.StrictMock();
originalOwnerObserver.Expect(o => o.UpdateObserver());
mocks.ReplayAll();
originalOwnerGroup.Attach(originalOwnerObserver);
// Precondition
CollectionAssert.Contains(originalOwnerGroup.Children, draggedItem);
// Call
info.OnDrop(draggedItemContext, originalOwnerGroupContext, originalOwnerGroupContext, newIndex, treeViewControlMock);
// Assert
CollectionAssert.Contains(originalOwnerGroup.Children, draggedItem);
Assert.AreNotSame(draggedItem, originalOwnerGroup.Children[1],
"Should have removed 'draggedItem' from its original location in the collection.");
Assert.AreSame(draggedItem, originalOwnerGroup.Children[newIndex],
"Dragging node to specific location within owning PipingCalculationGroup should put the dragged data at that index.");
Assert.AreEqual(name, draggedItem.Name,
"No renaming should occur when dragging within the same PipingCalculationGroup.");
mocks.VerifyAll();
}
[Test]
[Combinatorial]
public void OnDrop_DraggingPipingCalculationItemContextOntoGroupWithSameNamedItem_MoveCalculationItemInstanceToNewGroupAndRename(
[Values(CalculationType.Calculation, CalculationType.Group)] CalculationType draggedItemType)
{
// Setup
var treeViewControlMock = mocks.StrictMock();
var pipingFailureMechanismMock = mocks.StrictMock();
var assessmentSection = mocks.StrictMock();
ICalculationBase draggedItem;
object draggedItemContext;
CreateCalculationAndContext(draggedItemType, out draggedItem, out draggedItemContext, pipingFailureMechanismMock, assessmentSection);
CalculationGroup originalOwnerGroup;
GrassCoverErosionInwardsCalculationGroupContext originalOwnerGroupContext;
CreateCalculationGroupAndContext(out originalOwnerGroup, out originalOwnerGroupContext, pipingFailureMechanismMock, assessmentSection);
originalOwnerGroup.Children.Add(draggedItem);
CalculationGroup newOwnerGroup;
GrassCoverErosionInwardsCalculationGroupContext newOwnerGroupContext;
CreateCalculationGroupAndContext(out newOwnerGroup, out newOwnerGroupContext, pipingFailureMechanismMock, assessmentSection);
var sameNamedItem = mocks.Stub();
sameNamedItem.Stub(sni => sni.Name).Return(draggedItem.Name);
var originalOwnerObserver = mocks.StrictMock();
originalOwnerObserver.Expect(o => o.UpdateObserver());
var newOwnerObserver = mocks.StrictMock();
newOwnerObserver.Expect(o => o.UpdateObserver());
treeViewControlMock.Expect(tvc => tvc.TryRenameNodeForData(draggedItemContext));
mocks.ReplayAll();
newOwnerGroup.Children.Add(sameNamedItem);
originalOwnerGroup.Attach(originalOwnerObserver);
newOwnerGroup.Attach(newOwnerObserver);
// Precondition
CollectionAssert.Contains(originalOwnerGroup.Children, draggedItem);
CollectionAssert.DoesNotContain(newOwnerGroup.Children, draggedItem);
CollectionAssert.Contains(newOwnerGroup.Children.Select(c => c.Name), draggedItem.Name,
"Name of the dragged item should already exist in new owner.");
// Call
info.OnDrop(draggedItemContext, newOwnerGroupContext, originalOwnerGroupContext, 0, treeViewControlMock);
// Assert
CollectionAssert.DoesNotContain(originalOwnerGroup.Children, draggedItem);
CollectionAssert.Contains(newOwnerGroup.Children, draggedItem);
Assert.AreSame(draggedItem, newOwnerGroup.Children.First(),
"Dragging to insert node at start of newOwnerGroup should place the node at the start of the list.");
switch (draggedItemType)
{
case CalculationType.Calculation:
Assert.AreEqual("Nieuwe berekening", draggedItem.Name);
break;
case CalculationType.Group:
Assert.AreEqual("Nieuwe map", draggedItem.Name);
break;
}
mocks.VerifyAll();
}
private const int contextMenuAddCalculationGroupIndex = 0;
private const int contextMenuAddCalculationItemIndex = 1;
///
/// Creates an instance of and the corresponding
/// .
///
/// The created group without any children.
/// The context object for , without any other data.
/// The grass cover erosion inwards failure mechanism the item and context belong to.
/// The assessment section the item and context belong to.
private void CreateCalculationGroupAndContext(out CalculationGroup data, out GrassCoverErosionInwardsCalculationGroupContext dataContext, GrassCoverErosionInwardsFailureMechanism failureMechanism, IAssessmentSection assessmentSection)
{
data = new CalculationGroup();
dataContext = new GrassCoverErosionInwardsCalculationGroupContext(data,
failureMechanism,
assessmentSection);
}
///
/// Creates an instance of and the corresponding context.
///
/// Defines the implementation of to be constructed.
/// Output: The concrete create class based on .
/// Output: The corresponding with .
/// The grass cover erosion inwards failure mechanism the item and context belong to.
/// The assessment section the item and context belong to.
/// Optional: The name of .
///
private static void CreateCalculationAndContext(CalculationType type, out ICalculationBase data, out object dataContext, GrassCoverErosionInwardsFailureMechanism failureMechanism, IAssessmentSection assessmentSection, string initialName = null)
{
switch (type)
{
case CalculationType.Calculation:
var calculation = new GrassCoverErosionInwardsCalculation(failureMechanism.GeneralInput);
if (initialName != null)
{
calculation.Name = initialName;
}
data = calculation;
dataContext = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSection);
break;
case CalculationType.Group:
var group = new CalculationGroup();
if (initialName != null)
{
group.Name = initialName;
}
data = group;
dataContext = new GrassCoverErosionInwardsCalculationGroupContext(group,
failureMechanism,
assessmentSection);
break;
default:
throw new NotSupportedException();
}
}
///
/// 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 CalculationType
{
///
/// Indicates .
///
Calculation,
///
/// Indicates .
///
Group
}
}
}