// 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.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Windows.Forms; using Core.Common.Base; using Core.Common.Base.Data; using Core.Common.Controls.TreeView; using Core.Common.Gui; using Core.Common.Gui.Commands; using Core.Common.Gui.ContextMenu; using Core.Common.Gui.Forms.MainWindow; using Core.Common.Gui.TestUtil.ContextMenu; using Core.Common.TestUtil; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.AssemblyTool.KernelWrapper.Calculators; using Ringtoets.AssemblyTool.KernelWrapper.TestUtil.Calculators; using Ringtoets.AssemblyTool.KernelWrapper.TestUtil.Calculators.Categories; using Ringtoets.Common.Data; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Data.TestUtil; using Ringtoets.Common.Forms.PresentationObjects; using Ringtoets.HydraRing.Calculation.Calculator.Factory; using Ringtoets.HydraRing.Calculation.TestUtil.Calculator; using Ringtoets.Revetment.Data; using Ringtoets.StabilityStoneCover.Data; using Ringtoets.StabilityStoneCover.Forms.PresentationObjects; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; namespace Ringtoets.StabilityStoneCover.Plugin.Test.TreeNodeInfos { [TestFixture] public class StabilityStoneCoverFailureMechanismContextTreeNodeInfoTest { private const int contextMenuRelevancyIndexWhenRelevant = 2; private const int contextMenuRelevancyIndexWhenNotRelevant = 0; private const int contextMenuCalculateAllIndex = 4; private readonly string validFilePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Integration.Service, Path.Combine("HydraRingCalculation", "HRD ijsselmeer.sqlite")); private MockRepository mocks; private StabilityStoneCoverPlugin plugin; private TreeNodeInfo info; [SetUp] public void SetUp() { mocks = new MockRepository(); plugin = new StabilityStoneCoverPlugin(); info = plugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(StabilityStoneCoverFailureMechanismContext)); } [TearDown] public void TearDown() { plugin.Dispose(); mocks.VerifyAll(); } [Test] public void Initialized_Always_ExpectedPropertiesSet() { // Setup mocks.ReplayAll(); // Assert Assert.IsNotNull(info.Text); Assert.IsNotNull(info.ForeColor); Assert.IsNotNull(info.Image); Assert.IsNotNull(info.ContextMenuStrip); Assert.IsNull(info.EnsureVisibleOnCreate); Assert.IsNull(info.ExpandOnCreate); Assert.IsNotNull(info.ChildNodeObjects); Assert.IsNull(info.CanRename); Assert.IsNull(info.OnNodeRenamed); Assert.IsNull(info.CanRemove); Assert.IsNull(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_ReturnName() { // Setup var assessmentSection = mocks.Stub(); mocks.ReplayAll(); var failureMechanism = new StabilityStoneCoverFailureMechanism(); var failureMechanismContext = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); // Call string nodeText = info.Text(failureMechanismContext); // Assert Assert.AreEqual(failureMechanism.Name, nodeText); } [Test] public void ForeColor_FailureMechanismIsRelevant_ReturnControlText() { // Setup var assessmentSection = mocks.Stub(); mocks.ReplayAll(); var failureMechanism = new StabilityStoneCoverFailureMechanism { IsRelevant = true }; var failureMechanismContext = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); // Call Color foreColor = info.ForeColor(failureMechanismContext); // Assert Assert.AreEqual(Color.FromKnownColor(KnownColor.ControlText), foreColor); } [Test] public void ForeColor_FailureMechanismIsNotRelevant_ReturnGrayText() { // Setup var assessmentSection = mocks.Stub(); mocks.ReplayAll(); var failureMechanism = new StabilityStoneCoverFailureMechanism { IsRelevant = false }; var failureMechanismContext = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); // Call Color foreColor = info.ForeColor(failureMechanismContext); // Assert Assert.AreEqual(Color.FromKnownColor(KnownColor.GrayText), foreColor); } [Test] public void Image_Always_ReturnFailureMechanismIcon() { // Setup var assessmentSection = mocks.Stub(); mocks.ReplayAll(); var failureMechanism = new StabilityStoneCoverFailureMechanism(); var failureMechanismContext = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); // Call Image icon = info.Image(failureMechanismContext); // Assert TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.FailureMechanismIcon, icon); } [Test] public void ChildNodeObjects_FailureMechanismIsRelevant_ReturnChildDataNodes() { // Setup var assessmentSection = new AssessmentSectionStub(); var failureMechanism = new StabilityStoneCoverFailureMechanism { IsRelevant = true }; var failureMechanismContext = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); // Call object[] children = info.ChildNodeObjects(failureMechanismContext).ToArray(); // Assert Assert.AreEqual(3, children.Length); var inputsFolder = (CategoryTreeFolder) children[0]; Assert.AreEqual("Invoer", inputsFolder.Name); Assert.AreEqual(TreeFolderCategory.Input, inputsFolder.Category); Assert.AreEqual(3, inputsFolder.Contents.Count()); var failureMechanismSectionsContext = (StabilityStoneCoverFailureMechanismSectionsContext) inputsFolder.Contents.ElementAt(0); Assert.AreSame(failureMechanism, failureMechanismSectionsContext.WrappedData); Assert.AreSame(assessmentSection, failureMechanismSectionsContext.AssessmentSection); var profilesContext = (ForeshoreProfilesContext) inputsFolder.Contents.ElementAt(1); Assert.AreSame(failureMechanism.ForeshoreProfiles, profilesContext.WrappedData); Assert.AreSame(failureMechanism, profilesContext.ParentFailureMechanism); Assert.AreSame(assessmentSection, profilesContext.ParentAssessmentSection); var comment = (Comment) inputsFolder.Contents.ElementAt(2); Assert.AreSame(failureMechanism.InputComments, comment); var hydraulicBoundariesCalculationGroup = (StabilityStoneCoverWaveConditionsCalculationGroupContext) children[1]; Assert.AreSame(failureMechanism.WaveConditionsCalculationGroup, hydraulicBoundariesCalculationGroup.WrappedData); Assert.IsNull(hydraulicBoundariesCalculationGroup.Parent); Assert.AreSame(failureMechanism, hydraulicBoundariesCalculationGroup.FailureMechanism); Assert.AreSame(assessmentSection, hydraulicBoundariesCalculationGroup.AssessmentSection); var outputsFolder = (CategoryTreeFolder) children[2]; Assert.AreEqual("Oordeel", outputsFolder.Name); Assert.AreEqual(TreeFolderCategory.Output, outputsFolder.Category); Assert.AreEqual(3, outputsFolder.Contents.Count()); var failureMechanismAssemblyCategoriesContext = (FailureMechanismAssemblyCategoriesContext) outputsFolder.Contents.ElementAt(0); Assert.AreSame(failureMechanism, failureMechanismAssemblyCategoriesContext.WrappedData); Assert.AreSame(assessmentSection, failureMechanismAssemblyCategoriesContext.AssessmentSection); using (new AssemblyToolCalculatorFactoryConfig()) { var calculatorFactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance; AssemblyCategoriesCalculatorStub calculator = calculatorFactory.LastCreatedAssemblyCategoriesCalculator; failureMechanismAssemblyCategoriesContext.GetFailureMechanismSectionAssemblyCategoriesFunc(); Assert.AreEqual(failureMechanism.GeneralInput.N, calculator.AssemblyCategoriesInput.N); } var failureMechanismResultsContext = (FailureMechanismSectionResultContext) outputsFolder.Contents.ElementAt(1); Assert.AreSame(failureMechanism, failureMechanismResultsContext.FailureMechanism); Assert.AreSame(failureMechanism.SectionResults, failureMechanismResultsContext.WrappedData); var outputComment = (Comment) outputsFolder.Contents.ElementAt(2); Assert.AreSame(failureMechanism.OutputComments, outputComment); } [Test] public void ChildNodeObjects_FailureMechanismIsNotRelevant_ReturnOnlyFailureMechanismNotRelevantComments() { // Setup var assessmentSection = mocks.Stub(); mocks.ReplayAll(); var failureMechanism = new StabilityStoneCoverFailureMechanism { IsRelevant = false }; var failureMechanismContext = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); // Call object[] children = info.ChildNodeObjects(failureMechanismContext).ToArray(); // Assert Assert.AreEqual(1, children.Length); var comment = (Comment) children[0]; Assert.AreSame(failureMechanism.NotRelevantComments, comment); } [Test] public void ContextMenuStrip_FailureMechanismIsRelevant_CallsContextMenuBuilderMethods() { // Setup using (var treeViewControl = new TreeViewControl()) { var failureMechanism = new StabilityStoneCoverFailureMechanism { IsRelevant = true }; var assessmentSection = mocks.Stub(); var failureMechanismContext = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); 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.AddCustomItem(null)).IgnoreArguments().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); } var gui = mocks.Stub(); gui.Stub(cmp => cmp.Get(failureMechanismContext, treeViewControl)).Return(menuBuilder); mocks.ReplayAll(); plugin.Gui = gui; // Call info.ContextMenuStrip(failureMechanismContext, null, treeViewControl); } // Assert // Assert expectancies are called in TearDown() } [Test] public void ContextMenuStrip_FailureMechanismIsNotRelevant_CallsContextMenuBuilderMethods() { // Setup var treeViewControl = new TreeViewControl(); { var failureMechanism = new StabilityStoneCoverFailureMechanism { IsRelevant = false }; var assessmentSection = mocks.Stub(); var failureMechanismContext = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); var menuBuilder = mocks.StrictMock(); using (mocks.Ordered()) { menuBuilder.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().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); } var gui = mocks.Stub(); gui.Stub(cmp => cmp.Get(failureMechanismContext, treeViewControl)).Return(menuBuilder); mocks.ReplayAll(); plugin.Gui = gui; // Call info.ContextMenuStrip(failureMechanismContext, null, treeViewControl); } // Assert // Assert expectancies are called in TearDown() } [Test] public void ContextMenuStrip_FailureMechanismIsRelevant_AddCustomItems() { // Setup using (var treeView = new TreeViewControl()) { var assessmentSection = mocks.Stub(); var failureMechanism = new StabilityStoneCoverFailureMechanism(); var failureMechanismContext = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var gui = mocks.Stub(); gui.Stub(cmp => cmp.Get(failureMechanismContext, treeView)).Return(menuBuilder); gui.Stub(g => g.ProjectOpened += null).IgnoreArguments(); gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); mocks.ReplayAll(); plugin.Gui = gui; // Call using (ContextMenuStrip menu = info.ContextMenuStrip(failureMechanismContext, assessmentSection, treeView)) { // Assert Assert.AreEqual(10, menu.Items.Count); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuRelevancyIndexWhenRelevant, "I&s relevant", "Geeft aan of dit toetsspoor relevant is of niet.", RingtoetsCommonFormsResources.Checkbox_ticked); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuCalculateAllIndex, "Alles be&rekenen", "Er zijn geen berekeningen om uit te voeren.", RingtoetsCommonFormsResources.CalculateAllIcon, false); } } } [Test] public void ContextMenuStrip_FailureMechanismIsNotRelevant_AddCustomItems() { // Setup using (var treeView = new TreeViewControl()) { var assessmentSection = mocks.Stub(); var failureMechanism = new StabilityStoneCoverFailureMechanism { IsRelevant = false }; var failureMechanismContext = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var gui = mocks.Stub(); gui.Stub(cmp => cmp.Get(failureMechanismContext, treeView)).Return(menuBuilder); gui.Stub(g => g.ProjectOpened += null).IgnoreArguments(); gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); mocks.ReplayAll(); plugin.Gui = gui; // Call using (ContextMenuStrip menu = info.ContextMenuStrip(failureMechanismContext, assessmentSection, treeView)) { // Assert Assert.AreEqual(6, menu.Items.Count); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuRelevancyIndexWhenNotRelevant, "I&s relevant", "Geeft aan of dit toetsspoor relevant is of niet.", RingtoetsCommonFormsResources.Checkbox_empty); } } } [Test] public void ContextMenuStrip_FailureMechanismIsRelevantAndClickOnIsRelevantItem_MakeFailureMechanismNotRelevant() { // Setup using (var treeViewControl = new TreeViewControl()) { var failureMechanismObserver = mocks.Stub(); failureMechanismObserver.Expect(o => o.UpdateObserver()); var failureMechanism = new StabilityStoneCoverFailureMechanism { IsRelevant = true }; failureMechanism.Attach(failureMechanismObserver); var assessmentSection = mocks.Stub(); var failureMechanismContext = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); var viewCommands = mocks.StrictMock(); viewCommands.Expect(vs => vs.RemoveAllViewsForItem(failureMechanismContext)); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var gui = mocks.Stub(); gui.Stub(g => g.ViewCommands).Return(viewCommands); gui.Stub(g => g.Get(failureMechanismContext, treeViewControl)).Return(menuBuilder); mocks.ReplayAll(); plugin.Gui = gui; using (ContextMenuStrip contextMenu = info.ContextMenuStrip(failureMechanismContext, null, treeViewControl)) { // Call contextMenu.Items[contextMenuRelevancyIndexWhenRelevant].PerformClick(); // Assert Assert.IsFalse(failureMechanism.IsRelevant); } } } [Test] public void ContextMenuStrip_FailureMechanismIsNotRelevantAndClickOnIsRelevantItem_MakeFailureMechanismRelevant() { // Setup using (var treeViewControl = new TreeViewControl()) { var failureMechanismObserver = mocks.Stub(); failureMechanismObserver.Expect(o => o.UpdateObserver()); var failureMechanism = new StabilityStoneCoverFailureMechanism { IsRelevant = false }; failureMechanism.Attach(failureMechanismObserver); var assessmentSection = mocks.Stub(); var failureMechanismContext = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); var viewCommands = mocks.StrictMock(); viewCommands.Expect(vs => vs.RemoveAllViewsForItem(failureMechanismContext)); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var gui = mocks.Stub(); gui.Stub(g => g.ViewCommands).Return(viewCommands); gui.Stub(g => g.Get(failureMechanismContext, treeViewControl)).Return(menuBuilder); mocks.ReplayAll(); plugin.Gui = gui; using (ContextMenuStrip contextMenu = info.ContextMenuStrip(failureMechanismContext, null, treeViewControl)) { // Call contextMenu.Items[contextMenuRelevancyIndexWhenNotRelevant].PerformClick(); // Assert Assert.IsTrue(failureMechanism.IsRelevant); } } } [Test] public void ContextMenuStrip_HydraulicBoundaryDatabaseNotLinked_CalculateAllDisabled() { // Setup using (var treeViewControl = new TreeViewControl()) { var failureMechanism = new StabilityStoneCoverFailureMechanism(); failureMechanism.WaveConditionsCalculationGroup.Children.Add(new StabilityStoneCoverWaveConditionsCalculation()); IAssessmentSection assessmentSection = AssessmentSectionTestHelper.CreateAssessmentSectionStub(mocks); var nodeData = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var gui = mocks.Stub(); gui.Stub(g => g.Get(nodeData, treeViewControl)).Return(menuBuilder); gui.Stub(g => g.MainWindow).Return(mocks.Stub()); mocks.ReplayAll(); plugin.Gui = gui; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert ToolStripItem calculateItem = contextMenu.Items[contextMenuCalculateAllIndex]; Assert.IsFalse(calculateItem.Enabled); Assert.AreEqual("Er is geen hydraulische belastingendatabase geïmporteerd.", calculateItem.ToolTipText); } } } [Test] public void GivenValidCalculations_WhenCalculatingAllFromContextMenu_ThenAllCalculationsScheduled() { // Given var failureMechanism = new StabilityStoneCoverFailureMechanism(); IAssessmentSection assessmentSection = CreateAssessmentSectionWithHydraulicBoundaryOutput(); HydraulicBoundaryLocation hydraulicBoundaryLocation = assessmentSection.HydraulicBoundaryDatabase.Locations.First(); StabilityStoneCoverWaveConditionsCalculation calculationA = GetValidCalculation(hydraulicBoundaryLocation); StabilityStoneCoverWaveConditionsCalculation calculationB = GetValidCalculation(hydraulicBoundaryLocation); List calculations = failureMechanism.WaveConditionsCalculationGroup.Children; calculations.AddRange(new[] { calculationA, calculationB }); var nodeData = new StabilityStoneCoverFailureMechanismContext(failureMechanism, assessmentSection); using (var treeViewControl = new TreeViewControl()) { var mainWindow = mocks.Stub(); var gui = mocks.Stub(); gui.Stub(g => g.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); gui.Stub(g => g.MainWindow).Return(mainWindow); var calculatorFactory = mocks.Stub(); calculatorFactory.Expect(cf => cf.CreateWaveConditionsCosineCalculator(Path.GetDirectoryName(validFilePath), string.Empty)) .Return(new TestWaveConditionsCosineCalculator()).Repeat.Times(12); mocks.ReplayAll(); plugin.Gui = gui; using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // When contextMenu.Items[contextMenuCalculateAllIndex].PerformClick(); // Then Assert.AreEqual(3, calculationA.Output.BlocksOutput.Count()); Assert.AreEqual(3, calculationA.Output.ColumnsOutput.Count()); Assert.AreEqual(3, calculationB.Output.BlocksOutput.Count()); Assert.AreEqual(3, calculationB.Output.ColumnsOutput.Count()); } } } private IAssessmentSection CreateAssessmentSectionWithHydraulicBoundaryOutput() { var hydraulicBoundaryLocation = new HydraulicBoundaryLocation(1300001, string.Empty, 0, 0); var assessmentSection = new AssessmentSectionStub { HydraulicBoundaryDatabase = { FilePath = validFilePath, Locations = { hydraulicBoundaryLocation } } }; assessmentSection.SetHydraulicBoundaryLocationCalculations(new[] { hydraulicBoundaryLocation }); assessmentSection.WaterLevelCalculationsForLowerLimitNorm.First().Output = new TestHydraulicBoundaryLocationCalculationOutput(9.3); return assessmentSection; } private static StabilityStoneCoverWaveConditionsCalculation GetValidCalculation(HydraulicBoundaryLocation hydraulicBoundaryLocation) { return new StabilityStoneCoverWaveConditionsCalculation { InputParameters = { HydraulicBoundaryLocation = hydraulicBoundaryLocation, CategoryType = AssessmentSectionCategoryType.LowerLimitNorm, ForeshoreProfile = new TestForeshoreProfile(true), UseForeshore = true, UseBreakWater = true, StepSize = WaveConditionsInputStepSize.Half, LowerBoundaryRevetment = (RoundedDouble) 4, UpperBoundaryRevetment = (RoundedDouble) 10, UpperBoundaryWaterLevels = (RoundedDouble) 8, LowerBoundaryWaterLevels = (RoundedDouble) 7.1 } }; } } }