// Copyright (C) Stichting Deltares 2017. 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.Drawing;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Base;
using Core.Common.Controls.TreeView;
using Core.Common.Gui;
using Core.Common.Gui.ContextMenu;
using Core.Common.Gui.TestUtil.ContextMenu;
using Core.Common.TestUtil;
using NUnit.Framework;
using Rhino.Mocks;
using Ringtoets.ClosingStructures.Forms.PresentationObjects;
using Ringtoets.Common.Data;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Forms.PresentationObjects;
using Ringtoets.DuneErosion.Forms.PresentationObjects;
using Ringtoets.GrassCoverErosionInwards.Forms.PresentationObjects;
using Ringtoets.GrassCoverErosionOutwards.Forms.PresentationObjects;
using Ringtoets.HeightStructures.Forms.PresentationObjects;
using Ringtoets.Integration.Data;
using Ringtoets.Integration.Forms.PresentationObjects;
using Ringtoets.Integration.Forms.PresentationObjects.StandAlone;
using Ringtoets.MacroStabilityInwards.Forms.PresentationObjects;
using Ringtoets.Piping.Forms.PresentationObjects;
using Ringtoets.StabilityPointStructures.Forms.PresentationObjects;
using Ringtoets.StabilityStoneCover.Forms.PresentationObjects;
using Ringtoets.WaveImpactAsphaltCover.Forms.PresentationObjects;
using CoreCommonGuiResources = Core.Common.Gui.Properties.Resources;
using RingtoetsIntegrationFormsResources = Ringtoets.Integration.Forms.Properties.Resources;
namespace Ringtoets.Integration.Plugin.Test.TreeNodeInfos
{
[TestFixture]
public class AssessmentSectionTreeNodeInfoTest
{
private MockRepository mocks;
[SetUp]
public void SetUp()
{
mocks = new MockRepository();
}
[Test]
public void Initialized_Always_ExpectedPropertiesSet()
{
// Setup
using (var plugin = new RingtoetsPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Assert
Assert.IsNotNull(info.Text);
Assert.IsNull(info.ForeColor);
Assert.IsNotNull(info.Image);
Assert.IsNotNull(info.ContextMenuStrip);
Assert.IsNotNull(info.EnsureVisibleOnCreate);
Assert.IsNotNull(info.ExpandOnCreate);
Assert.IsNotNull(info.ChildNodeObjects);
Assert.IsNotNull(info.CanRename);
Assert.IsNotNull(info.OnNodeRenamed);
Assert.IsNotNull(info.CanRemove);
Assert.IsNotNull(info.OnNodeRemoved);
Assert.IsNull(info.CanCheck);
Assert.IsNull(info.IsChecked);
Assert.IsNull(info.OnNodeChecked);
Assert.IsNull(info.CanDrag);
Assert.IsNull(info.CanDrop);
Assert.IsNull(info.CanInsert);
Assert.IsNull(info.OnDrop);
}
}
[Test]
public void Text_Always_ReturnsName()
{
// Setup
const string testName = "ttt";
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike)
{
Name = testName
};
using (var plugin = new RingtoetsPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
string text = info.Text(assessmentSection);
// Assert
Assert.AreEqual(testName, text);
}
}
[Test]
public void Image_Always_ReturnsSetImage()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
using (var plugin = new RingtoetsPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
Image image = info.Image(assessmentSection);
// Assert
TestHelper.AssertImagesAreEqual(RingtoetsIntegrationFormsResources.AssessmentSectionFolderIcon, image);
}
}
[Test]
public void EnsureVisibleOnCreate_Always_ReturnsTrue()
{
// Setup
mocks.ReplayAll();
using (var plugin = new RingtoetsPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
bool result = info.EnsureVisibleOnCreate(null, null);
// Assert
Assert.IsTrue(result);
}
mocks.VerifyAll();
}
[Test]
public void ExpandOnCreate_Always_ReturnsTrue()
{
// Setup
mocks.ReplayAll();
using (var plugin = new RingtoetsPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
bool result = info.ExpandOnCreate(null);
// Assert
Assert.IsTrue(result);
}
mocks.VerifyAll();
}
[Test]
public void ChildNodeObjects_Always_ReturnsChildrenOfData()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
using (var plugin = new RingtoetsPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
object[] objects = info.ChildNodeObjects(assessmentSection).ToArray();
// Assert
Assert.AreEqual(25, objects.Length);
var referenceLineContext = (ReferenceLineContext) objects[0];
Assert.AreSame(assessmentSection, referenceLineContext.WrappedData);
var normContext = (NormContext) objects[1];
Assert.AreSame(assessmentSection.FailureMechanismContribution, normContext.WrappedData);
Assert.AreSame(assessmentSection, normContext.AssessmentSection);
var contributionContext = (FailureMechanismContributionContext) objects[2];
Assert.AreSame(assessmentSection.FailureMechanismContribution, contributionContext.WrappedData);
Assert.AreSame(assessmentSection, contributionContext.Parent);
var context = (HydraulicBoundaryDatabaseContext) objects[3];
Assert.AreSame(assessmentSection.HydraulicBoundaryDatabase, context.WrappedData);
Assert.AreSame(assessmentSection, context.AssessmentSection);
var backgroundData = (BackgroundData) objects[4];
Assert.AreSame(assessmentSection.BackgroundData, backgroundData);
var comment = (Comment) objects[5];
Assert.AreSame(assessmentSection.Comments, comment);
var pipingFailureMechanismContext = (PipingFailureMechanismContext) objects[6];
Assert.AreSame(assessmentSection.Piping, pipingFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, pipingFailureMechanismContext.Parent);
var grassCoverErosionInwardsFailureMechanismContext = (GrassCoverErosionInwardsFailureMechanismContext) objects[7];
Assert.AreSame(assessmentSection.GrassCoverErosionInwards, grassCoverErosionInwardsFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, grassCoverErosionInwardsFailureMechanismContext.Parent);
var macroStabilityInwardsFailureMechanismContext = (MacroStabilityInwardsFailureMechanismContext) objects[8];
Assert.AreSame(assessmentSection.MacroStabilityInwards, macroStabilityInwardsFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, macroStabilityInwardsFailureMechanismContext.Parent);
var macroStabilityOutwardsFailureMechanismContext = (MacroStabilityOutwardsFailureMechanismContext) objects[9];
Assert.AreSame(assessmentSection.MacroStabilityOutwards, macroStabilityOutwardsFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, macroStabilityOutwardsFailureMechanismContext.Parent);
var microstabilityFailureMechanismContext = (FailureMechanismContext) objects[10];
Assert.AreSame(assessmentSection.Microstability, microstabilityFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, microstabilityFailureMechanismContext.Parent);
var stabilityStoneCoverFailureMechanismContext = (StabilityStoneCoverFailureMechanismContext) objects[11];
Assert.AreSame(assessmentSection.StabilityStoneCover, stabilityStoneCoverFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, stabilityStoneCoverFailureMechanismContext.Parent);
var waveImpactAsphaltCoverFailureMechanismContext = (WaveImpactAsphaltCoverFailureMechanismContext) objects[12];
Assert.AreSame(assessmentSection.WaveImpactAsphaltCover, waveImpactAsphaltCoverFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, waveImpactAsphaltCoverFailureMechanismContext.Parent);
var waterPressureAsphaltCoverFailureMechanismContext = (FailureMechanismContext) objects[13];
Assert.AreSame(assessmentSection.WaterPressureAsphaltCover, waterPressureAsphaltCoverFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, waterPressureAsphaltCoverFailureMechanismContext.Parent);
var grassCoverErosionOutwardsFailureMechanismContext = (GrassCoverErosionOutwardsFailureMechanismContext) objects[14];
Assert.AreSame(assessmentSection.GrassCoverErosionOutwards, grassCoverErosionOutwardsFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, grassCoverErosionOutwardsFailureMechanismContext.Parent);
var grassCoverSlipOffOutwardsFailureMechanismContext = (FailureMechanismContext) objects[15];
Assert.AreSame(assessmentSection.GrassCoverSlipOffOutwards, grassCoverSlipOffOutwardsFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, grassCoverSlipOffOutwardsFailureMechanismContext.Parent);
var grassCoverSlipOffInwardsFailureMechanismContext = (FailureMechanismContext) objects[16];
Assert.AreSame(assessmentSection.GrassCoverSlipOffInwards, grassCoverSlipOffInwardsFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, grassCoverSlipOffInwardsFailureMechanismContext.Parent);
var heightStructuresFailureMechanismContext = (HeightStructuresFailureMechanismContext) objects[17];
Assert.AreSame(assessmentSection.HeightStructures, heightStructuresFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, heightStructuresFailureMechanismContext.Parent);
var closingStructuresFailureMechanismContext = (ClosingStructuresFailureMechanismContext) objects[18];
Assert.AreSame(assessmentSection.ClosingStructures, closingStructuresFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, closingStructuresFailureMechanismContext.Parent);
var pipingStructureFailureMechanismContext = (PipingStructureFailureMechanismContext) objects[19];
Assert.AreSame(assessmentSection.PipingStructure, pipingStructureFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, pipingStructureFailureMechanismContext.Parent);
var stabilityPointStructuresFailureMechanismContext = (StabilityPointStructuresFailureMechanismContext) objects[20];
Assert.AreSame(assessmentSection.StabilityPointStructures, stabilityPointStructuresFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, stabilityPointStructuresFailureMechanismContext.Parent);
var strengthStabilityLengthwiseConstructionFailureMechanismContext = (FailureMechanismContext) objects[21];
Assert.AreSame(assessmentSection.StrengthStabilityLengthwiseConstruction, strengthStabilityLengthwiseConstructionFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, strengthStabilityLengthwiseConstructionFailureMechanismContext.Parent);
var duneErosionFailureMechanismContext = (DuneErosionFailureMechanismContext) objects[22];
Assert.AreSame(assessmentSection.DuneErosion, duneErosionFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, duneErosionFailureMechanismContext.Parent);
var technicalInnovationFailureMechanismContext = (FailureMechanismContext) objects[23];
Assert.AreSame(assessmentSection.TechnicalInnovation, technicalInnovationFailureMechanismContext.WrappedData);
Assert.AreSame(assessmentSection, technicalInnovationFailureMechanismContext.Parent);
var assemblyResultCategoryTreeFolder = (CategoryTreeFolder) objects[24];
Assert.AreEqual("Assemblage", assemblyResultCategoryTreeFolder.Name);
Assert.AreEqual(TreeFolderCategory.General, assemblyResultCategoryTreeFolder.Category);
Assert.AreEqual(2, assemblyResultCategoryTreeFolder.Contents.Count());
var assemblyResultTotalContext = (AssemblyResultTotalContext) assemblyResultCategoryTreeFolder.Contents.ElementAt(0);
Assert.AreSame(assessmentSection, assemblyResultTotalContext.WrappedData);
var assemblyResultPerSectionContext = (AssemblyResultPerSectionContext) assemblyResultCategoryTreeFolder.Contents.ElementAt(1);
Assert.AreSame(assessmentSection, assemblyResultPerSectionContext.WrappedData);
}
}
[Test]
public void ContextMenuStrip_Always_CallsBuilder()
{
// Setup
var menuBuilder = mocks.StrictMock();
using (mocks.Ordered())
{
menuBuilder.Expect(mb => mb.AddOpenItem()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddSeparator()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddSeparator()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddRenameItem()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddSeparator()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddDeleteItem()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddSeparator()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddCollapseAllItem()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddExpandAllItem()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddSeparator()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddPropertiesItem()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.Build()).Return(null);
}
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(g => g.Get(null, treeViewControl)).Return(menuBuilder);
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
mocks.ReplayAll();
using (var plugin = new RingtoetsPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
plugin.Gui = gui;
// Call
info.ContextMenuStrip(null, null, treeViewControl);
}
}
// Assert
mocks.VerifyAll();
}
[Test]
public void ContextMenuStrip_Always_ContextMenuItemImportAssessmentSectionEnabled()
{
// Setup
var section = new AssessmentSection(AssessmentSectionComposition.Dike);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(g => g.Get(section, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
mocks.ReplayAll();
using (var plugin = new RingtoetsPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
plugin.Gui = gui;
// Call
using (ContextMenuStrip contextMenu = info.ContextMenuStrip(section, section, treeViewControl))
{
const string expectedItemText = "&Importeren...";
const string expectedItemTooltip = "Importeer de gegevens vanuit een bestand.";
TestHelper.AssertContextMenuStripContainsItem(contextMenu, 2,
expectedItemText, expectedItemTooltip,
CoreCommonGuiResources.ImportIcon);
}
}
}
// Assert
mocks.VerifyAll();
}
[Test]
public void CanRename_Always_ReturnsTrue()
{
// Setup
mocks.ReplayAll();
using (var plugin = new RingtoetsPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
bool canRename = info.CanRename(null, null);
// Assert
Assert.IsTrue(canRename);
}
mocks.VerifyAll();
}
[Test]
public void OnNodeRenamed_WithData_SetProjectNameWithNotification()
{
// Setup
var observer = mocks.Stub();
observer.Expect(o => o.UpdateObserver());
mocks.ReplayAll();
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
assessmentSection.Attach(observer);
using (var plugin = new RingtoetsPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
const string newName = "New Name";
info.OnNodeRenamed(assessmentSection, newName);
// Assert
Assert.AreEqual(newName, assessmentSection.Name);
}
mocks.VerifyAll();
}
[Test]
public void CanRemove_Always_ReturnsTrue()
{
// Setup
mocks.ReplayAll();
using (var plugin = new RingtoetsPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
bool canRemove = info.CanRemove(null, null);
// Assert
Assert.IsTrue(canRemove);
}
mocks.VerifyAll();
}
[Test]
public void RemoveNodeData_ProjectWithAssessmentSection_ReturnTrueAndRemoveAssessmentSection()
{
// Setup
var observer = mocks.StrictMock();
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
observer.Expect(o => o.UpdateObserver());
mocks.ReplayAll();
var project = new RingtoetsProject();
project.AssessmentSections.Add(assessmentSection);
project.Attach(observer);
using (var plugin = new RingtoetsPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
info.OnNodeRemoved(assessmentSection, project);
// Assert
CollectionAssert.DoesNotContain(project.AssessmentSections, assessmentSection);
}
mocks.VerifyAll();
}
private TreeNodeInfo GetInfo(RingtoetsPlugin plugin)
{
return plugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(AssessmentSection));
}
}
}