// 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.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.Forms.MainWindow; 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.Forms.PresentationObjects; using Ringtoets.GrassCoverErosionInwards.Data; using Ringtoets.GrassCoverErosionInwards.Forms.PresentationObjects; using Ringtoets.GrassCoverErosionInwards.Plugin; using Ringtoets.GrassCoverErosionInwards.Plugin.Properties; using Ringtoets.HydraRing.Data; using GrassCoverErosionInwardsFormsResources = Ringtoets.GrassCoverErosionInwards.Forms.Properties.Resources; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; namespace Ringtoets.GrassCoverErosionInwards.Forms.Test.TreeNodeInfos { [TestFixture] public class GrassCoverErosionInwardsCalculationContextTreeNodeInfoTest : NUnitFormTest { private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.HydraRing.IO, "HydraulicBoundaryLocationReader"); private MockRepository mocks; private GrassCoverErosionInwardsGuiPlugin plugin; private TreeNodeInfo info; [SetUp] public void SetUp() { mocks = new MockRepository(); plugin = new GrassCoverErosionInwardsGuiPlugin(); info = plugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(GrassCoverErosionInwardsCalculationContext)); } [Test] public void Initialized_Always_ExpectedPropertiesSet() { // Assert Assert.AreEqual(typeof(GrassCoverErosionInwardsCalculationContext), info.TagType); 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() { // Call var image = info.Image(null); // Assert TestHelper.AssertImagesAreEqual(GrassCoverErosionInwardsFormsResources.CalculationIcon, image); } [Test] public void ChildNodeObjects_WithOutputData_ReturnOutputChildNode() { var assessmentSectionMock = mocks.StrictMock(); mocks.ReplayAll(); var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput()); var failureMechanism = new GrassCoverErosionInwardsFailureMechanism(); var calculationContext = new GrassCoverErosionInwardsCalculationContext(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.CommentContainer); var grassCoverErosionInwardsCalculationContext = (GrassCoverErosionInwardsInputContext) children[1]; Assert.AreSame(calculationContext.WrappedData.InputParameters, grassCoverErosionInwardsCalculationContext.WrappedData); var emptyOutput = (EmptyGrassCoverErosionInwardsOutput) children[2]; Assert.IsNotNull(emptyOutput); mocks.VerifyAll(); } [Test] public void ContextMenuStrip_Always_CallsContextMenuBuilderMethods() { // Setup var gui = mocks.StrictMock(); var treeViewControlMock = mocks.StrictMock(); var menuBuilderMock = mocks.Stub(); 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.AddImportItem()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddExportItem()).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); var failureMechanism = new GrassCoverErosionInwardsFailureMechanism(); var assessmentSectionMock = mocks.StrictMock(); var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput()); var nodeData = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock); gui.Expect(cmp => cmp.Get(nodeData, treeViewControlMock)).Return(menuBuilderMock); mocks.ReplayAll(); plugin.Gui = gui; // Call info.ContextMenuStrip(nodeData, null, treeViewControlMock); // Assert mocks.VerifyAll(); // Expect no calls on arguments } [Test] public void ContextMenuStrip_NoFailureMechanismSections_ContextMenuItemPerformCalculationDisabledAndTooltipSet() { // Setup var gui = mocks.StrictMock(); var treeViewControlMock = mocks.StrictMock(); var failureMechanism = new GrassCoverErosionInwardsFailureMechanism(); var assessmentSectionMock = mocks.StrictMock(); var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput()); var nodeData = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock); gui.Expect(cmp => cmp.Get(nodeData, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = gui; // Call var contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControlMock); // Assert mocks.VerifyAll(); // Expect no calls on arguments TestHelper.AssertContextMenuStripContainsItem(contextMenu, 0, RingtoetsCommonFormsResources.Calculate, Resources.GrassCoverErosionInwardsGuiPlugin_AllDataAvailable_No_failure_mechanism_sections_imported, RingtoetsCommonFormsResources.CalculateIcon, false); } [Test] public void ContextMenuStrip_FailureMechanismSectionsSetNoHydraulicBoundaryDatabase_ContextMenuItemPerformCalculationDisabledAndTooltipSet() { // Setup var gui = mocks.StrictMock(); var treeViewControlMock = mocks.StrictMock(); var failureMechanism = new GrassCoverErosionInwardsFailureMechanism(); failureMechanism.AddSection(new FailureMechanismSection("test", new[] { new Point2D(0, 0) })); var assessmentSectionMock = mocks.StrictMock(); assessmentSectionMock.Expect(asm => asm.HydraulicBoundaryDatabase).Return(null); var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput()); var nodeData = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock); gui.Expect(cmp => cmp.Get(nodeData, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = gui; // Call var contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControlMock); // Assert mocks.VerifyAll(); // Expect no calls on arguments TestHelper.AssertContextMenuStripContainsItem(contextMenu, 0, RingtoetsCommonFormsResources.Calculate, Resources.GrassCoverErosionInwardsGuiPlugin_AllDataAvailable_No_hydraulic_boundary_database_imported, RingtoetsCommonFormsResources.CalculateIcon, false); } [Test] public void ContextMenuStrip_FailureMechanismSetHydraulicBoundaryDatabaseNotValid_ContextMenuItemPerformCalculationDisabledAndTooltipSet() { // Setup var gui = mocks.StrictMock(); var treeViewControlMock = mocks.StrictMock(); var failureMechanism = new GrassCoverErosionInwardsFailureMechanism(); failureMechanism.AddSection(new FailureMechanismSection("test", new[] { new Point2D(0, 0) })); var assessmentSectionMock = mocks.StrictMock(); assessmentSectionMock.Stub(asm => asm.HydraulicBoundaryDatabase).Return(new HydraulicBoundaryDatabase()); var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput()); var nodeData = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock); gui.Expect(cmp => cmp.Get(nodeData, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = gui; // Call ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControlMock); // Assert mocks.VerifyAll(); // Expect no calls on arguments ToolStripItem contextMenuItem = contextMenu.Items[0]; Assert.AreEqual(RingtoetsCommonFormsResources.Calculate, contextMenuItem.Text); StringAssert.Contains(String.Format(RingtoetsCommonFormsResources.GuiPlugin_VerifyHydraulicBoundaryDatabasePath_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 gui = mocks.StrictMock(); var treeViewControlMock = mocks.StrictMock(); string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase { FilePath = validFilePath, Version = "1.0" }; var failureMechanism = new GrassCoverErosionInwardsFailureMechanism(); failureMechanism.AddSection(new FailureMechanismSection("test", new[] { new Point2D(0, 0) })); var assessmentSectionMock = mocks.StrictMock(); assessmentSectionMock.Stub(asm => asm.HydraulicBoundaryDatabase).Return(hydraulicBoundaryDatabase); var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput()); var nodeData = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock); gui.Expect(cmp => cmp.Get(nodeData, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = gui; // Call var contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControlMock); // Assert mocks.VerifyAll(); // Expect no calls on arguments TestHelper.AssertContextMenuStripContainsItem(contextMenu, 0, RingtoetsCommonFormsResources.Calculate, RingtoetsCommonFormsResources.Calculate_ToolTip, RingtoetsCommonFormsResources.CalculateIcon); } [Test] public void ContextMenuStrip_CalculationWithoutOutput_ContextMenuItemClearOutputDisabled() { // Setup var gui = mocks.StrictMock(); var treeViewControlMock = mocks.StrictMock(); var failureMechanism = new GrassCoverErosionInwardsFailureMechanism(); var assessmentSectionMock = mocks.StrictMock(); var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput()); var nodeData = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock); gui.Expect(cmp => cmp.Get(nodeData, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = gui; // Call var contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControlMock); // Assert mocks.VerifyAll(); // Expect no calls on arguments TestHelper.AssertContextMenuStripContainsItem(contextMenu, 1, RingtoetsCommonFormsResources.Clear_output, RingtoetsCommonFormsResources.ClearOutput_No_output_to_clear, RingtoetsCommonFormsResources.ClearIcon, false); } [Test] public void ContextMenuStrip_CalculationWithOutput_ContextMenuItemClearOutputEnabled() { var gui = mocks.StrictMock(); var treeViewControlMock = mocks.StrictMock(); var failureMechanism = new GrassCoverErosionInwardsFailureMechanism(); var assessmentSectionMock = mocks.StrictMock(); var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput()) { Output = new GrassCoverErosionInwardsOutput(0, 0, 0, 0, 0) }; var nodeData = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock); gui.Expect(cmp => cmp.Get(nodeData, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder()); mocks.ReplayAll(); plugin.Gui = gui; // Call var contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControlMock); // Assert mocks.VerifyAll(); // Expect no calls on arguments TestHelper.AssertContextMenuStripContainsItem(contextMenu, 1, RingtoetsCommonFormsResources.Clear_output, RingtoetsCommonFormsResources.Clear_output_ToolTip, RingtoetsCommonFormsResources.ClearIcon); } [Test] [TestCase(true)] [TestCase(false)] public void GivenCalculationWithOutput_WhenClearingOutput_ThenCalculationClearedAndNotified(bool confirm) { // Given var gui = mocks.DynamicMock(); var observer = mocks.StrictMock(); var treeViewControlMock = mocks.StrictMock(); var failureMechanism = new GrassCoverErosionInwardsFailureMechanism(); var assessmentSectionMock = mocks.StrictMock(); var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput()); var calculationContext = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock); gui.Expect(cmp => cmp.Get(calculationContext, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder()); int clearOutputItemPosition = 1; if (confirm) { observer.Expect(o => o.UpdateObserver()); } mocks.ReplayAll(); plugin.Gui = gui; calculation.Output = new GrassCoverErosionInwardsOutput(0, 0, 0, 0, 0); calculation.Attach(observer); var contextMenuAdapter = info.ContextMenuStrip(calculationContext, null, treeViewControlMock); string messageBoxText = null, messageBoxTitle = null; DialogBoxHandler = (name, wnd) => { var messageBox = new MessageBoxTester(wnd); messageBoxText = messageBox.Text; messageBoxTitle = messageBox.Title; if (confirm) { messageBox.ClickOk(); } else { messageBox.ClickCancel(); } }; // When contextMenuAdapter.Items[clearOutputItemPosition].PerformClick(); // Then Assert.AreNotEqual(confirm, calculation.HasOutput); Assert.AreEqual("Bevestigen", messageBoxTitle); Assert.AreEqual("Weet u zeker dat u de uitvoer van deze berekening wilt wissen?", messageBoxText); mocks.VerifyAll(); } [Test] public void GivenCalculationWithNonExistingFilePath_WhenCalculatingFromContextMenu_ThenOutputClearedLogMessagesAddedObserversNotNotified() { // Given var gui = mocks.DynamicMock(); var mainWindow = mocks.DynamicMock(); var observer = mocks.StrictMock(); var treeViewControlMock = mocks.StrictMock(); var calculateContextMenuItemIndex = 0; var section = new FailureMechanismSection("A", new[] { new Point2D(1, 2), new Point2D(3, 4) }); var failureMechanism = new GrassCoverErosionInwardsFailureMechanism(); failureMechanism.AddSection(section); string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase { FilePath = validFilePath, Version = "random" }; var assessmentSectionMock = mocks.StrictMock(); assessmentSectionMock.Stub(asm => asm.HydraulicBoundaryDatabase).Return(hydraulicBoundaryDatabase); var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput()) { Output = new GrassCoverErosionInwardsOutput(double.NaN, double.NaN, double.NaN, double.NaN, double.NaN) }; var calculationContext = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock); gui.Expect(g => g.Get(calculationContext, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder()); gui.Expect(g => g.MainWindow).Return(mainWindow); mocks.ReplayAll(); plugin.Gui = gui; calculation.Attach(observer); var contextMenuAdapter = info.ContextMenuStrip(calculationContext, null, treeViewControlMock); DialogBoxHandler = (name, wnd) => { // Don't care about dialogs in this test. }; // When Action action = () => { contextMenuAdapter.Items[calculateContextMenuItemIndex].PerformClick(); }; // Then TestHelper.AssertLogMessages(action, messages => { var msgs = messages.GetEnumerator(); Assert.IsTrue(msgs.MoveNext()); StringAssert.StartsWith("Er is een fout opgetreden tijdens de berekening.", msgs.Current); }); Assert.IsNull(calculation.Output); mocks.VerifyAll(); } [Test] [TestCase(true)] [TestCase(false)] public void OnNodeRemoved_ParentIsCalculationGroupContext_RemoveCalculationFromGroup(bool groupNameEditable) { // Setup var observer = mocks.StrictMock(); observer.Expect(o => o.UpdateObserver()); var elementToBeRemoved = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput()); var group = new CalculationGroup("", groupNameEditable); group.Children.Add(elementToBeRemoved); group.Children.Add(new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput())); group.Attach(observer); var failureMechanism = new GrassCoverErosionInwardsFailureMechanism(); var assessmentSectionMock = mocks.StrictMock(); mocks.ReplayAll(); var calculationContext = new GrassCoverErosionInwardsCalculationContext(elementToBeRemoved, failureMechanism, assessmentSectionMock); var groupContext = new GrassCoverErosionInwardsCalculationGroupContext(group, failureMechanism, assessmentSectionMock); // 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); mocks.VerifyAll(); } } }