// 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.IO; using System.Linq; using System.Windows.Forms; using Core.Common.Base; using Core.Common.Base.Geometry; 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.Extensions.Forms; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.Common.Data; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.Probability; using Ringtoets.Common.Forms.PresentationObjects; using Ringtoets.HydraRing.Data; using Ringtoets.StabilityPointStructures.Data; using Ringtoets.StabilityPointStructures.Forms.PresentationObjects; using Ringtoets.StabilityPointStructures.Plugin; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; using RingtoetsCommonServiceResources = Ringtoets.Common.Service.Properties.Resources; using RingtoetsCommonServicesResources = Ringtoets.Common.Service.Properties.Resources; namespace Ringtoets.StabilityPointStructures.Forms.Test.TreeNodeInfos { [TestFixture] public class StabilityPointStructuresCalculationContextTreeNodeInfoTest : NUnitFormTest { private const int contextMenuValidateIndex = 0; private const int contextMenuCalculateIndex = 1; private const int contextMenuClearIndex = 2; private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.HydraRing.IO, "HydraulicBoundaryLocationReader"); private MockRepository mocks; private StabilityPointStructuresPlugin plugin; private TreeNodeInfo info; [SetUp] public void SetUp() { mocks = new MockRepository(); plugin = new StabilityPointStructuresPlugin(); info = plugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(StabilityPointStructuresCalculationContext)); } [Test] public void Initialized_Always_ExpectedPropertiesSet() { // Setup mocks.ReplayAll(); // Assert Assert.AreEqual(typeof(StabilityPointStructuresCalculationContext), info.TagType); Assert.IsNotNull(info.Text); Assert.IsNotNull(info.Image); Assert.IsNotNull(info.EnsureVisibleOnCreate); Assert.IsNotNull(info.ChildNodeObjects); Assert.IsNotNull(info.ContextMenuStrip); Assert.IsNotNull(info.CanRename); Assert.IsNotNull(info.OnNodeRenamed); Assert.IsNotNull(info.CanRemove); Assert.IsNotNull(info.OnNodeRemoved); Assert.IsNotNull(info.CanDrag); Assert.IsNull(info.ForeColor); Assert.IsNull(info.CanCheck); Assert.IsNull(info.IsChecked); Assert.IsNull(info.OnNodeChecked); Assert.IsNull(info.CanDrop); Assert.IsNull(info.CanInsert); Assert.IsNull(info.OnDrop); } [Test] public void Image_Always_ReturnsCalculationIcon() { // Setup mocks.ReplayAll(); // Call var image = info.Image(null); // Assert TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.CalculationIcon, image); } [Test] public void ChildNodeObjects_CalculationWithoutOutput_ReturnCollectionWithEmptyOutputObject() { var assessmentSectionMock = mocks.Stub(); mocks.ReplayAll(); var calculation = new StabilityPointStructuresCalculation(); var failureMechanism = new StabilityPointStructuresFailureMechanism(); var calculationContext = new StabilityPointStructuresCalculationContext(calculation, failureMechanism, assessmentSectionMock); // Call var children = info.ChildNodeObjects(calculationContext).ToArray(); // Assert Assert.AreEqual(3, children.Length); var commentContext = children[0] as CommentContext; Assert.IsNotNull(commentContext); Assert.AreSame(calculationContext.WrappedData, commentContext.WrappedData); var stabilityPointStructuresInputContext = children[1] as StabilityPointStructuresInputContext; Assert.IsNotNull(stabilityPointStructuresInputContext); Assert.AreSame(calculationContext.WrappedData.InputParameters, stabilityPointStructuresInputContext.WrappedData); var emptyOutput = children[2] as EmptyProbabilityAssessmentOutput; Assert.IsNotNull(emptyOutput); } [Test] public void ChildNodeObjects_CalculationWithOutput_ReturnCollectionWithOutputObject() { var assessmentSectionMock = mocks.Stub(); mocks.ReplayAll(); var calculation = new StabilityPointStructuresCalculation { Output = new ProbabilityAssessmentOutput(0, 0, 0, 0, 0) }; var failureMechanism = new StabilityPointStructuresFailureMechanism(); var calculationContext = new StabilityPointStructuresCalculationContext(calculation, failureMechanism, assessmentSectionMock); // Call var children = info.ChildNodeObjects(calculationContext).ToArray(); // Assert Assert.AreEqual(3, children.Length); var commentContext = children[0] as CommentContext; Assert.IsNotNull(commentContext); Assert.AreSame(calculationContext.WrappedData, commentContext.WrappedData); var stabilityPointStructuresInputContext = children[1] as StabilityPointStructuresInputContext; Assert.IsNotNull(stabilityPointStructuresInputContext); Assert.AreSame(calculationContext.WrappedData.InputParameters, stabilityPointStructuresInputContext.WrappedData); var output = children[2] as ProbabilityAssessmentOutput; Assert.IsNotNull(output); } [Test] public void ContextMenuStrip_Always_CallsContextMenuBuilderMethods() { // Setup var guiMock = mocks.StrictMock(); var failureMechanism = new StabilityPointStructuresFailureMechanism(); var assessmentSectionMock = mocks.Stub(); var calculation = new StabilityPointStructuresCalculation(); var nodeData = new StabilityPointStructuresCalculationContext(calculation, failureMechanism, assessmentSectionMock); var menuBuilderMock = mocks.StrictMock(); menuBuilderMock.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddSeparator()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddRenameItem()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddDeleteItem()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddSeparator()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddExpandAllItem()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddCollapseAllItem()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddSeparator()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddPropertiesItem()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.Build()).Return(null); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilderMock); mocks.ReplayAll(); plugin.Gui = guiMock; // Call info.ContextMenuStrip(nodeData, null, treeViewControl); } // Assert // Assert expectancies called in TearDown() } [Test] public void ContextMenuStrip_Always_AddCustomItems() { // Setup var guiMock = mocks.StrictMock(); var failureMechanism = new StabilityPointStructuresFailureMechanism(); var assessmentSectionMock = mocks.StrictMock(); var calculation = new StabilityPointStructuresCalculation(); var nodeData = new StabilityPointStructuresCalculationContext(calculation, failureMechanism, assessmentSectionMock); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder); assessmentSectionMock.Stub(asm => asm.HydraulicBoundaryDatabase).Return(new HydraulicBoundaryDatabase { FilePath = validFilePath, Version = "random" }); mocks.ReplayAll(); plugin.Gui = guiMock; failureMechanism.AddSection(new FailureMechanismSection("test", new[] { new Point2D(0, 0) })); // Call using (ContextMenuStrip menu = info.ContextMenuStrip(nodeData, assessmentSectionMock, treeViewControl)) { // Assert Assert.AreEqual(6, menu.Items.Count); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuValidateIndex, RingtoetsCommonFormsResources.Validate, RingtoetsCommonFormsResources.Validate_ToolTip, RingtoetsCommonFormsResources.ValidateIcon); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuCalculateIndex, RingtoetsCommonFormsResources.Calculate, RingtoetsCommonFormsResources.Calculate_ToolTip, RingtoetsCommonFormsResources.CalculateIcon); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuClearIndex, RingtoetsCommonFormsResources.Clear_output, RingtoetsCommonFormsResources.ClearOutput_No_output_to_clear, RingtoetsCommonFormsResources.ClearIcon, false); } } } [Test] public void ContextMenuStrip_NoFailureMechanismSections_ContextMenuItemPerformCalculationDisabledAndTooltipSet() { // Setup var failureMechanism = new StabilityPointStructuresFailureMechanism(); var assessmentSectionMock = mocks.Stub(); var calculation = new StabilityPointStructuresCalculation(); var nodeData = new StabilityPointStructuresCalculationContext(calculation, failureMechanism, assessmentSectionMock); var guiMock = mocks.StrictMock(); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = guiMock; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuCalculateIndex, RingtoetsCommonFormsResources.Calculate, RingtoetsCommonFormsResources.Plugin_AllDataAvailable_No_failure_mechanism_sections_imported, RingtoetsCommonFormsResources.CalculateIcon, false); } } } [Test] public void ContextMenuStrip_FailureMechanismSectionsSetNoHydraulicBoundaryDatabase_ContextMenuItemPerformCalculationDisabledAndTooltipSet() { // Setup var guiMock = mocks.StrictMock(); var failureMechanism = new StabilityPointStructuresFailureMechanism(); failureMechanism.AddSection(new FailureMechanismSection("test", new[] { new Point2D(0, 0) })); var assessmentSectionMock = mocks.Stub(); var calculation = new StabilityPointStructuresCalculation(); var nodeData = new StabilityPointStructuresCalculationContext(calculation, failureMechanism, assessmentSectionMock); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = guiMock; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuCalculateIndex, RingtoetsCommonFormsResources.Calculate, RingtoetsCommonFormsResources.Plugin_AllDataAvailable_No_hydraulic_boundary_database_imported, RingtoetsCommonFormsResources.CalculateIcon, false); } } } [Test] public void ContextMenuStrip_FailureMechanismSectionsSetHydraulicBoundaryDatabaseNotValid_ContextMenuItemPerformCalculationDisabledAndTooltipSet() { // Setup var guiMock = mocks.StrictMock(); var failureMechanism = new StabilityPointStructuresFailureMechanism(); failureMechanism.AddSection(new FailureMechanismSection("test", new[] { new Point2D(0, 0) })); var assessmentSectionMock = mocks.Stub(); assessmentSectionMock.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); var calculation = new StabilityPointStructuresCalculation(); var nodeData = new StabilityPointStructuresCalculationContext(calculation, failureMechanism, assessmentSectionMock); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = guiMock; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert ToolStripItem contextMenuItem = contextMenu.Items[contextMenuCalculateIndex]; Assert.AreEqual(RingtoetsCommonFormsResources.Calculate, contextMenuItem.Text); StringAssert.Contains(string.Format(RingtoetsCommonServicesResources.Hydraulic_boundary_database_connection_failed_0_, ""), contextMenuItem.ToolTipText); TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.CalculateIcon, contextMenuItem.Image); Assert.IsFalse(contextMenuItem.Enabled); } } } [Test] public void ContextMenuStrip_FailureMechanismSectionsAndHydraulicBoundaryDatabaseSet_ContextMenuItemPerformCalculationEnabled() { // Setup var guiMock = mocks.StrictMock(); string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase { FilePath = validFilePath, Version = "1.0" }; var failureMechanism = new StabilityPointStructuresFailureMechanism(); failureMechanism.AddSection(new FailureMechanismSection("test", new[] { new Point2D(0, 0) })); var assessmentSectionMock = mocks.Stub(); assessmentSectionMock.HydraulicBoundaryDatabase = hydraulicBoundaryDatabase; var calculation = new StabilityPointStructuresCalculation(); var nodeData = new StabilityPointStructuresCalculationContext(calculation, failureMechanism, assessmentSectionMock); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = guiMock; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuCalculateIndex, RingtoetsCommonFormsResources.Calculate, RingtoetsCommonFormsResources.Calculate_ToolTip, RingtoetsCommonFormsResources.CalculateIcon); } } } [Test] public void ContextMenuStrip_NoFailureMechanismSections_ContextMenuItemValidateCalculationDisabledAndTooltipSet() { // Setup var failureMechanism = new StabilityPointStructuresFailureMechanism(); var assessmentSectionMock = mocks.Stub(); var calculation = new StabilityPointStructuresCalculation(); var nodeData = new StabilityPointStructuresCalculationContext(calculation, failureMechanism, assessmentSectionMock); var guiMock = mocks.StrictMock(); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = guiMock; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateIndex, RingtoetsCommonFormsResources.Validate, RingtoetsCommonFormsResources.Plugin_AllDataAvailable_No_failure_mechanism_sections_imported, RingtoetsCommonFormsResources.ValidateIcon, false); } } } [Test] public void ContextMenuStrip_FailureMechanismSectionsSetNoHydraulicBoundaryDatabase_ContextMenuItemValidateCalculationDisabledAndTooltipSet() { // Setup var guiMock = mocks.StrictMock(); var failureMechanism = new StabilityPointStructuresFailureMechanism(); failureMechanism.AddSection(new FailureMechanismSection("test", new[] { new Point2D(0, 0) })); var assessmentSectionMock = mocks.Stub(); var calculation = new StabilityPointStructuresCalculation(); var nodeData = new StabilityPointStructuresCalculationContext(calculation, failureMechanism, assessmentSectionMock); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = guiMock; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateIndex, RingtoetsCommonFormsResources.Validate, RingtoetsCommonFormsResources.Plugin_AllDataAvailable_No_hydraulic_boundary_database_imported, RingtoetsCommonFormsResources.ValidateIcon, false); } } } [Test] public void ContextMenuStrip_FailureMechanismSectionsSetHydraulicBoundaryDatabaseNotValid_ContextMenuItemValidateCalculationDisabledAndTooltipSet() { // Setup var guiMock = mocks.StrictMock(); var failureMechanism = new StabilityPointStructuresFailureMechanism(); failureMechanism.AddSection(new FailureMechanismSection("test", new[] { new Point2D(0, 0) })); var assessmentSectionMock = mocks.Stub(); assessmentSectionMock.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); var calculation = new StabilityPointStructuresCalculation(); var nodeData = new StabilityPointStructuresCalculationContext(calculation, failureMechanism, assessmentSectionMock); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = guiMock; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert ToolStripItem contextMenuItem = contextMenu.Items[contextMenuValidateIndex]; Assert.AreEqual(RingtoetsCommonFormsResources.Validate, contextMenuItem.Text); StringAssert.Contains(string.Format(RingtoetsCommonServicesResources.Hydraulic_boundary_database_connection_failed_0_, ""), contextMenuItem.ToolTipText); TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.ValidateIcon, contextMenuItem.Image); Assert.IsFalse(contextMenuItem.Enabled); } } } [Test] public void ContextMenuStrip_FailureMechanismSectionsAndHydraulicBoundaryDatabaseSet_ContextMenuItemValidateCalculationEnabled() { // Setup var guiMock = mocks.StrictMock(); string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase { FilePath = validFilePath, Version = "1.0" }; var failureMechanism = new StabilityPointStructuresFailureMechanism(); failureMechanism.AddSection(new FailureMechanismSection("test", new[] { new Point2D(0, 0) })); var assessmentSectionMock = mocks.Stub(); assessmentSectionMock.HydraulicBoundaryDatabase = hydraulicBoundaryDatabase; var calculation = new StabilityPointStructuresCalculation(); var nodeData = new StabilityPointStructuresCalculationContext(calculation, failureMechanism, assessmentSectionMock); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = guiMock; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateIndex, RingtoetsCommonFormsResources.Validate, RingtoetsCommonFormsResources.Validate_ToolTip, RingtoetsCommonFormsResources.ValidateIcon); } } } [Test] public void OnNodeRemoved_ParentIsCalculationGroupContext_RemoveCalculationFromGroup() { // Setup var group = new CalculationGroup(); var elementToBeRemoved = new StabilityPointStructuresCalculation(); var failureMechanism = new StabilityPointStructuresFailureMechanism(); var observerMock = mocks.StrictMock(); var assessmentSectionMock = mocks.Stub(); var calculationContext = new StabilityPointStructuresCalculationContext(elementToBeRemoved, failureMechanism, assessmentSectionMock); var groupContext = new StabilityPointStructuresCalculationGroupContext(group, failureMechanism, assessmentSectionMock); observerMock.Expect(o => o.UpdateObserver()); mocks.ReplayAll(); group.Children.Add(elementToBeRemoved); group.Children.Add(new StabilityPointStructuresCalculation()); group.Attach(observerMock); // Precondition Assert.IsTrue(info.CanRemove(calculationContext, groupContext)); Assert.AreEqual(2, group.Children.Count); // Call info.OnNodeRemoved(calculationContext, groupContext); // Assert Assert.AreEqual(1, group.Children.Count); CollectionAssert.DoesNotContain(group.Children, elementToBeRemoved); } public override void TearDown() { plugin.Dispose(); mocks.VerifyAll(); base.TearDown(); } } }