// 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.Collections.Generic; using System.IO; using System.Linq; using System.Windows.Forms; using Core.Common.Base; using Core.Common.Base.Geometry; using Core.Common.Controls.DataGrid; 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.Extensions.Forms; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.Contribution; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Data.Structures; using Ringtoets.Common.Data.TestUtil; using Ringtoets.Common.Forms; using Ringtoets.Common.Forms.Helpers; using Ringtoets.HeightStructures.Data; using Ringtoets.HeightStructures.Data.TestUtil; using Ringtoets.HeightStructures.Forms.PresentationObjects; using Ringtoets.HydraRing.Calculation.TestUtil.Calculator; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; namespace Ringtoets.HeightStructures.Plugin.Test.TreeNodeInfos { [TestFixture] public class HeightStructuresCalculationGroupContextTreeNodeInfoTest : NUnitFormTest { private const int contextMenuGenerateCalculationsIndexRootGroup = 3; private const int contextMenuAddCalculationGroupIndexRootGroup = 5; private const int contextMenuAddCalculationIndexRootGroup = 6; private const int contextMenuValidateAllIndexRootGroup = 8; private const int contextMenuCalculateAllIndexRootGroup = 9; private const int contextMenuClearAllIndexRootGroup = 11; private const int contextMenuAddCalculationGroupIndexNestedGroup = 3; private const int contextMenuAddCalculationIndexNestedGroup = 4; private const int contextMenuValidateAllIndexNestedGroup = 8; private const int contextMenuCalculateAllIndexNestedGroup = 9; private const int contextMenuClearAllIndexNestedGroup = 11; private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "HydraulicBoundaryDatabaseImporter"); private IGui guiStub; private TreeNodeInfo info; private MockRepository mocks; private HeightStructuresPlugin plugin; [SetUp] public void SetUp() { mocks = new MockRepository(); guiStub = mocks.Stub(); plugin = new HeightStructuresPlugin { Gui = guiStub }; info = plugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(HeightStructuresCalculationGroupContext)); } [Test] public void Initialized_Always_ExpectedPropertiesSet() { // Setup mocks.ReplayAll(); // Assert Assert.IsNotNull(info.Text); Assert.IsNull(info.ForeColor); Assert.IsNotNull(info.Image); Assert.IsNotNull(info.ContextMenuStrip); Assert.IsNotNull(info.EnsureVisibleOnCreate); Assert.IsNull(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.IsNotNull(info.CanDrag); Assert.IsNotNull(info.CanDrop); Assert.IsNotNull(info.CanInsert); Assert.IsNotNull(info.OnDrop); } [Test] public void ChildNodeObjects_EmptyGroup_ReturnEmpty() { // Setup var group = new CalculationGroup(); var failureMechanism = new HeightStructuresFailureMechanism(); var assessmentSection = mocks.Stub(); mocks.ReplayAll(); var groupContext = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); // Call object[] children = info.ChildNodeObjects(groupContext); // Assert CollectionAssert.IsEmpty(children); } [Test] public void ChildNodeObjects_GroupWithMixedContents_ReturnChildren() { // Setup var assessmentSection = mocks.Stub(); var calculationItem = mocks.Stub(); mocks.ReplayAll(); var failureMechanism = new HeightStructuresFailureMechanism(); var group = new CalculationGroup(); var childGroup = new CalculationGroup(); var childCalculation = new StructuresCalculation(); group.Children.Add(childGroup); group.Children.Add(calculationItem); group.Children.Add(childCalculation); var groupContext = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); // Call object[] children = info.ChildNodeObjects(groupContext).ToArray(); // Assert Assert.AreEqual(group.Children.Count, children.Length); var calculationGroupContext = (HeightStructuresCalculationGroupContext) children[0]; Assert.AreSame(childGroup, calculationGroupContext.WrappedData); Assert.AreSame(failureMechanism, calculationGroupContext.FailureMechanism); Assert.AreSame(assessmentSection, calculationGroupContext.AssessmentSection); Assert.AreSame(calculationItem, children[1]); var calculationContext = (HeightStructuresCalculationContext) children[2]; Assert.AreSame(childCalculation, calculationContext.WrappedData); Assert.AreSame(assessmentSection, calculationContext.AssessmentSection); } [Test] public void ContextMenuStrip_WithoutParentNodeDefaultBehavior_CallsContextMenuBuilderMethods() { // Setup var group = new CalculationGroup(); var failureMechanism = new HeightStructuresFailureMechanism(); var assessmentSection = mocks.Stub(); var groupContext = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var menuBuilderMock = mocks.StrictMock(); using (mocks.Ordered()) { 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.AddCustomItem(null)).IgnoreArguments().Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddSeparator()).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.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.AddCustomItem(null)).IgnoreArguments().Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddDeleteChildrenItem()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddSeparator()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddCollapseAllItem()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddExpandAllItem()).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()) { guiStub.Stub(cmp => cmp.Get(groupContext, treeViewControl)).Return(menuBuilderMock); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); // Call info.ContextMenuStrip(groupContext, null, treeViewControl); } // Assert // Assert expectancies called in TearDown() } [Test] public void ContextMenuStrip_WithoutParentNodeDefaultBehavior_AddCustomItems() { // Setup var group = new CalculationGroup(); var failureMechanism = new HeightStructuresFailureMechanism(); var assessmentSection = mocks.Stub(); var groupContext = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(g => g.Get(groupContext, treeViewControl)).Return(menuBuilder); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); // Call using (ContextMenuStrip menu = info.ContextMenuStrip(groupContext, null, treeViewControl)) { // Assert Assert.AreEqual(18, menu.Items.Count); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuGenerateCalculationsIndexRootGroup, "Genereer &berekeningen...", "Er zijn geen kunstwerken beschikbaar om berekeningen voor te genereren.", RingtoetsCommonFormsResources.GenerateScenariosIcon, false); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuAddCalculationGroupIndexRootGroup, "&Map toevoegen", "Voeg een nieuwe berekeningsmap toe aan deze berekeningsmap.", RingtoetsCommonFormsResources.AddFolderIcon); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuAddCalculationIndexRootGroup, "Berekening &toevoegen", "Voeg een nieuwe berekening toe aan deze berekeningsmap.", RingtoetsCommonFormsResources.FailureMechanismIcon); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuValidateAllIndexRootGroup, "Alles &valideren", "Er zijn geen berekeningen om te valideren.", RingtoetsCommonFormsResources.ValidateAllIcon, false); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuCalculateAllIndexRootGroup, "Alles be&rekenen", "Er zijn geen berekeningen om uit te voeren.", RingtoetsCommonFormsResources.CalculateAllIcon, false); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuClearAllIndexRootGroup, "&Wis alle uitvoer...", "Er zijn geen berekeningen met uitvoer om te wissen.", RingtoetsCommonFormsResources.ClearIcon, false); } } } [Test] public void ContextMenuStrip_WithoutParentNodeWithStructuresImported_GenerateItemEnabledWithTooltip() { // Setup var group = new CalculationGroup(); var failureMechanism = new HeightStructuresFailureMechanism(); failureMechanism.HeightStructures.Add(new TestHeightStructure()); var assessmentSection = mocks.Stub(); assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); var groupContext = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(g => g.Get(groupContext, treeViewControl)).Return(menuBuilder); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); // Call using (ContextMenuStrip menu = info.ContextMenuStrip(groupContext, null, treeViewControl)) { // Assert Assert.AreEqual(18, menu.Items.Count); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuGenerateCalculationsIndexRootGroup, "Genereer &berekeningen...", "Genereer berekeningen op basis van geselecteerde kunstwerken.", RingtoetsCommonFormsResources.GenerateScenariosIcon); } } } [Test] public void ContextMenuStrip_NestedCalculationGroup_CallsContextMenuBuilderMethods() { // Setup var group = new CalculationGroup(); var parentGroup = new CalculationGroup(); var failureMechanism = new HeightStructuresFailureMechanism(); var assessmentSection = mocks.Stub(); var groupContext = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var parentGroupContext = new HeightStructuresCalculationGroupContext(parentGroup, failureMechanism, assessmentSection); var menuBuilderMock = mocks.StrictMock(); using (mocks.Ordered()) { 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.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.AddSeparator()).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.AddCustomItem(null)).IgnoreArguments().Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddDeleteItem()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddSeparator()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddCollapseAllItem()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.AddExpandAllItem()).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()) { guiStub.Stub(cmp => cmp.Get(groupContext, treeViewControl)).Return(menuBuilderMock); mocks.ReplayAll(); // Call info.ContextMenuStrip(groupContext, parentGroupContext, treeViewControl); } // Assert // Assert expectancies called in TearDown() } [Test] public void ContextMenuStrip_NestedCalculationGroup_AddCustomItems() { // Setup var group = new CalculationGroup(); var parentGroup = new CalculationGroup(); var failureMechanism = new HeightStructuresFailureMechanism(); var assessmentSection = mocks.Stub(); var groupContext = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var parentGroupContext = new HeightStructuresCalculationGroupContext(parentGroup, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); mocks.ReplayAll(); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(g => g.Get(groupContext, treeViewControl)).Return(menuBuilder); // Call using (ContextMenuStrip menu = info.ContextMenuStrip(groupContext, parentGroupContext, treeViewControl)) { // Assert Assert.AreEqual(18, menu.Items.Count); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuAddCalculationGroupIndexNestedGroup, "&Map toevoegen", "Voeg een nieuwe berekeningsmap toe aan deze berekeningsmap.", RingtoetsCommonFormsResources.AddFolderIcon); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuAddCalculationIndexNestedGroup, "Berekening &toevoegen", "Voeg een nieuwe berekening toe aan deze berekeningsmap.", RingtoetsCommonFormsResources.FailureMechanismIcon); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuValidateAllIndexNestedGroup, "Alles &valideren", "Er zijn geen berekeningen om te valideren.", RingtoetsCommonFormsResources.ValidateAllIcon, false); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuCalculateAllIndexNestedGroup, "Alles be&rekenen", "Er zijn geen berekeningen om uit te voeren.", RingtoetsCommonFormsResources.CalculateAllIcon, false); TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuClearAllIndexNestedGroup, "&Wis alle uitvoer...", "Er zijn geen berekeningen met uitvoer om te wissen.", RingtoetsCommonFormsResources.ClearIcon, false); } } } [Test] public void ContextMenuStrip_NoHydraulicBoundaryDatabase_ContextMenuItemCalculateAllDisabledAndTooltipSet() { // Setup var group = new CalculationGroup { Children = { new StructuresCalculation() } }; var failureMechanism = new TestHeightStructuresFailureMechanism(); failureMechanism.CalculationsGroup.Children.Add(new StructuresCalculation()); var assessmentSection = mocks.Stub(); var nodeData = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(g => g.Get(nodeData, treeViewControl)).Return(menuBuilder); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuCalculateAllIndexRootGroup, "Alles be&rekenen", "Er is geen hydraulische randvoorwaardendatabase geïmporteerd.", RingtoetsCommonFormsResources.CalculateAllIcon, false); } } } [Test] public void ContextMenuStrip_HydraulicBoundaryDatabaseNotValid_ContextMenuItemCalculateAllDisabledAndTooltipSet() { // Setup var group = new CalculationGroup { Children = { new StructuresCalculation() } }; var failureMechanism = new TestHeightStructuresFailureMechanism(); failureMechanism.CalculationsGroup.Children.Add(new StructuresCalculation()); var assessmentSection = mocks.Stub(); var nodeData = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(g => g.Get(nodeData, treeViewControl)).Return(menuBuilder); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert ToolStripItem contextMenuItem = contextMenu.Items[contextMenuCalculateAllIndexRootGroup]; Assert.AreEqual("Alles be&rekenen", contextMenuItem.Text); StringAssert.Contains(string.Format("Herstellen van de verbinding met de hydraulische randvoorwaardendatabase is mislukt. {0}", ""), contextMenuItem.ToolTipText); TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.CalculateAllIcon, contextMenuItem.Image); Assert.IsFalse(contextMenuItem.Enabled); } } } [Test] public void ContextMenuStrip_FailureMechanismContributionZero_ContextMenuItemCalculateAllDisabledAndTooltipSet() { // Setup var group = new CalculationGroup { Children = { new StructuresCalculation() } }; string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); var failureMechanism = new HeightStructuresFailureMechanism(); failureMechanism.CalculationsGroup.Children.Add(new StructuresCalculation()); var assessmentSection = mocks.Stub(); assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase { FilePath = validFilePath, Version = "1.0" }; var nodeData = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(g => g.Get(nodeData, treeViewControl)).Return(menuBuilder); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); // Call using (ContextMenuStrip menu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuCalculateAllIndexRootGroup, "Alles be&rekenen", "De bijdrage van dit toetsspoor is nul.", RingtoetsCommonFormsResources.CalculateAllIcon, false); } } } [Test] public void ContextMenuStrip_AllRequiredInputSet_ContextMenuItemCalculateAllEnabled() { // Setup var group = new CalculationGroup { Children = { new StructuresCalculation() } }; string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); var failureMechanism = new TestHeightStructuresFailureMechanism(); failureMechanism.CalculationsGroup.Children.Add(new StructuresCalculation()); var assessmentSection = mocks.Stub(); assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase { FilePath = validFilePath, Version = "1.0" }; var nodeData = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(g => g.Get(nodeData, treeViewControl)).Return(menuBuilder); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); // Call using (ContextMenuStrip menu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuCalculateAllIndexRootGroup, "Alles be&rekenen", "Voer alle berekeningen binnen deze berekeningsmap uit.", RingtoetsCommonFormsResources.CalculateAllIcon); } } } [Test] public void ContextMenuStrip_NoHydraulicBoundaryDatabase_ContextMenuItemValidateAllDisabledAndTooltipSet() { // Setup var group = new CalculationGroup { Children = { new StructuresCalculation() } }; var failureMechanism = new TestHeightStructuresFailureMechanism(); failureMechanism.CalculationsGroup.Children.Add(new StructuresCalculation()); var assessmentSection = mocks.Stub(); var nodeData = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(g => g.Get(nodeData, treeViewControl)).Return(menuBuilder); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateAllIndexRootGroup, "Alles &valideren", "Er is geen hydraulische randvoorwaardendatabase geïmporteerd.", RingtoetsCommonFormsResources.ValidateAllIcon, false); } } } [Test] public void ContextMenuStrip_HydraulicBoundaryDatabaseNotValid_ContextMenuItemValidateAllDisabledAndTooltipSet() { // Setup var group = new CalculationGroup { Children = { new StructuresCalculation() } }; var failureMechanism = new TestHeightStructuresFailureMechanism(); failureMechanism.CalculationsGroup.Children.Add(new StructuresCalculation()); var assessmentSection = mocks.Stub(); assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); var nodeData = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(g => g.Get(nodeData, treeViewControl)).Return(menuBuilder); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert ToolStripItem contextMenuItem = contextMenu.Items[contextMenuValidateAllIndexRootGroup]; Assert.AreEqual("Alles &valideren", contextMenuItem.Text); StringAssert.Contains(string.Format("Herstellen van de verbinding met de hydraulische randvoorwaardendatabase is mislukt. {0}", ""), contextMenuItem.ToolTipText); TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.ValidateAllIcon, contextMenuItem.Image); Assert.IsFalse(contextMenuItem.Enabled); } } } [Test] public void ContextMenuStrip_FailureMechanismContributionZero_ContextMenuItemValidateAllDisabledAndTooltipSet() { // Setup var group = new CalculationGroup { Children = { new StructuresCalculation() } }; string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); var failureMechanism = new HeightStructuresFailureMechanism(); failureMechanism.CalculationsGroup.Children.Add(new StructuresCalculation()); var assessmentSection = mocks.Stub(); assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase { FilePath = validFilePath, Version = "1.0" }; var nodeData = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(g => g.Get(nodeData, treeViewControl)).Return(menuBuilder); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); // Call using (ContextMenuStrip menu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuValidateAllIndexRootGroup, "Alles &valideren", "De bijdrage van dit toetsspoor is nul.", RingtoetsCommonFormsResources.ValidateAllIcon, false); } } } [Test] public void ContextMenuStrip_AllRequiredInputSet_ContextMenuItemValidateAllEnabled() { // Setup var group = new CalculationGroup { Children = { new StructuresCalculation() } }; string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); var failureMechanism = new TestHeightStructuresFailureMechanism(); failureMechanism.CalculationsGroup.Children.Add(new StructuresCalculation()); var assessmentSection = mocks.Stub(); assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase { FilePath = validFilePath, Version = "1.0" }; var nodeData = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(g => g.Get(nodeData, treeViewControl)).Return(menuBuilder); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); // Call using (ContextMenuStrip menu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuValidateAllIndexRootGroup, "Alles &valideren", "Valideer alle berekeningen binnen deze berekeningsmap.", RingtoetsCommonFormsResources.ValidateAllIcon); } } } [Test] public void ContextMenuStrip_ClickOnCalculateAllItem_ScheduleAllChildCalculations() { // Setup var mainWindowStub = mocks.Stub(); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var failureMechanism = new TestHeightStructuresFailureMechanism(); failureMechanism.CalculationsGroup.Children.Add(new TestHeightStructuresCalculation { Name = "A", InputParameters = { HydraulicBoundaryLocation = new TestHydraulicBoundaryLocation() } }); failureMechanism.CalculationsGroup.Children.Add(new TestHeightStructuresCalculation { Name = "B", InputParameters = { HydraulicBoundaryLocation = new TestHydraulicBoundaryLocation() } }); string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); var hydraulicBoundaryDatabase = mocks.Stub(); hydraulicBoundaryDatabase.FilePath = validFilePath; var assessmentSection = mocks.Stub(); assessmentSection.Stub(a => a.Id).Return(string.Empty); assessmentSection.Stub(a => a.FailureMechanismContribution).Return(new FailureMechanismContribution(Enumerable.Empty(), 1, 1)); var groupContext = new HeightStructuresCalculationGroupContext(failureMechanism.CalculationsGroup, failureMechanism, assessmentSection); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(g => g.Get(groupContext, treeViewControl)).Return(menuBuilder); guiStub.Stub(g => g.MainWindow).Return(mainWindowStub); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); assessmentSection.HydraulicBoundaryDatabase = hydraulicBoundaryDatabase; plugin.Gui = guiStub; DialogBoxHandler = (name, wnd) => { // Expect an activity dialog which is automatically closed }; using (ContextMenuStrip contextMenu = info.ContextMenuStrip(groupContext, null, treeViewControl)) using (new HydraRingCalculatorFactoryConfig()) { // Call TestHelper.AssertLogMessages(() => contextMenu.Items[contextMenuCalculateAllIndexRootGroup].PerformClick(), messages => { List messageList = messages.ToList(); // Assert Assert.AreEqual(12, messageList.Count); StringAssert.StartsWith("Validatie van 'A' gestart om: ", messageList[0]); StringAssert.StartsWith("Validatie van 'A' beëindigd om: ", messageList[1]); StringAssert.StartsWith("Berekening van 'A' gestart om: ", messageList[2]); StringAssert.StartsWith("Hoogte kunstwerk berekening is uitgevoerd op de tijdelijke locatie", messageList[3]); StringAssert.StartsWith("Berekening van 'A' beëindigd om: ", messageList[4]); StringAssert.StartsWith("Validatie van 'B' gestart om: ", messageList[5]); StringAssert.StartsWith("Validatie van 'B' beëindigd om: ", messageList[6]); StringAssert.StartsWith("Berekening van 'B' gestart om: ", messageList[7]); StringAssert.StartsWith("Hoogte kunstwerk berekening is uitgevoerd op de tijdelijke locatie", messageList[8]); StringAssert.StartsWith("Berekening van 'B' beëindigd om: ", messageList[9]); Assert.AreEqual("Uitvoeren van 'A' is gelukt.", messageList[10]); Assert.AreEqual("Uitvoeren van 'B' is gelukt.", messageList[11]); }); } } } [Test] public void ContextMenuStrip_ClickOnValidateAllItem_ScheduleAllChildCalculations() { // Setup var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var failureMechanism = new TestHeightStructuresFailureMechanism(); failureMechanism.CalculationsGroup.Children.Add(new TestHeightStructuresCalculation { Name = "A", InputParameters = { HydraulicBoundaryLocation = new TestHydraulicBoundaryLocation() } }); failureMechanism.CalculationsGroup.Children.Add(new TestHeightStructuresCalculation { Name = "B", InputParameters = { HydraulicBoundaryLocation = new TestHydraulicBoundaryLocation() } }); string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); var hydraulicBoundaryDatabase = mocks.Stub(); hydraulicBoundaryDatabase.FilePath = validFilePath; var assessmentSection = mocks.Stub(); assessmentSection.HydraulicBoundaryDatabase = hydraulicBoundaryDatabase; var groupContext = new HeightStructuresCalculationGroupContext(failureMechanism.CalculationsGroup, failureMechanism, assessmentSection); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(g => g.Get(groupContext, treeViewControl)).Return(menuBuilder); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); plugin.Gui = guiStub; using (ContextMenuStrip contextMenu = info.ContextMenuStrip(groupContext, null, treeViewControl)) { // Call TestHelper.AssertLogMessages(() => contextMenu.Items[contextMenuValidateAllIndexRootGroup].PerformClick(), messages => { List messageList = messages.ToList(); // Assert Assert.AreEqual(4, messageList.Count); StringAssert.StartsWith("Validatie van 'A' gestart om: ", messageList[0]); StringAssert.StartsWith("Validatie van 'A' beëindigd om: ", messageList[1]); StringAssert.StartsWith("Validatie van 'B' gestart om: ", messageList[2]); StringAssert.StartsWith("Validatie van 'B' beëindigd om: ", messageList[3]); }); } } } [Test] public void ContextMenuStrip_ClickOnAddGroupItem_AddGroupToCalculationGroupAndNotifyObservers() { // Setup var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var group = new CalculationGroup(); var failureMechanism = new HeightStructuresFailureMechanism(); var assessmentSection = mocks.Stub(); var nodeData = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var calculationGroup = new CalculationGroup { Name = "Nieuwe map" }; var observer = mocks.StrictMock(); observer.Expect(o => o.UpdateObserver()); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); group.Children.Add(calculationGroup); nodeData.Attach(observer); using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Precondition Assert.AreEqual(1, group.Children.Count); // Call contextMenu.Items[contextMenuAddCalculationGroupIndexRootGroup].PerformClick(); // Assert Assert.AreEqual(2, group.Children.Count); ICalculationBase 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."); } } } [Test] public void ContextMenuStrip_ClickOnAddCalculationItem_AddCalculationToCalculationGroupAndNotifyObservers() { // Setup var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var group = new CalculationGroup(); var failureMechanism = new HeightStructuresFailureMechanism(); var assessmentSection = mocks.Stub(); var nodeData = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var calculation = new StructuresCalculation { Name = "Nieuwe berekening" }; var observer = mocks.StrictMock(); observer.Expect(o => o.UpdateObserver()); using (var treeViewControl = new TreeViewControl()) { guiStub.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); group.Children.Add(calculation); nodeData.Attach(observer); using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Precondition Assert.AreEqual(1, group.Children.Count); // Call contextMenu.Items[contextMenuAddCalculationIndexRootGroup].PerformClick(); // Assert Assert.AreEqual(2, group.Children.Count); ICalculationBase 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."); } } } [Test] public void GivenCalculationsViewGenerateScenariosButtonClicked_WhenHeightStructureSelectedAndDialogClosed_ThenCalculationsAddedWithHeightStructureAssigned() { // Given using (var treeViewControl = new TreeViewControl()) { var assessmentSection = mocks.Stub(); HeightStructure structure1 = new TestHeightStructure("Structure 1"); HeightStructure structure2 = new TestHeightStructure("Structure 2"); var existingCalculationGroup = new CalculationGroup(); var existingCalculation = new StructuresCalculation(); var failureMechanism = new HeightStructuresFailureMechanism { HeightStructures = { structure1, structure2 }, CalculationsGroup = { Children = { existingCalculationGroup, existingCalculation } } }; var nodeData = new HeightStructuresCalculationGroupContext(failureMechanism.CalculationsGroup, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var mainWindow = mocks.Stub(); guiStub.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder); guiStub.Stub(g => g.MainWindow).Return(mainWindow); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); plugin.Gui = guiStub; DialogBoxHandler = (name, wnd) => { var selectionDialog = (StructureSelectionDialog) new FormTester(name).TheObject; var grid = (DataGridViewControl) new ControlTester("DataGridViewControl", selectionDialog).TheObject; grid.Rows[0].Cells[0].Value = true; new ButtonTester("DoForSelectedButton", selectionDialog).Click(); }; using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // When contextMenu.Items[contextMenuGenerateCalculationsIndexRootGroup].PerformClick(); // Then Assert.AreEqual(3, failureMechanism.CalculationsGroup.Children.Count); Assert.AreSame(existingCalculationGroup, failureMechanism.CalculationsGroup.Children[0]); Assert.AreSame(existingCalculation, failureMechanism.CalculationsGroup.Children[1]); var generatedCalculation = failureMechanism.CalculationsGroup.Children[2] as StructuresCalculation; Assert.IsNotNull(generatedCalculation); Assert.AreSame(structure1, generatedCalculation.InputParameters.Structure); } } } [Test] public void GivenCalculationsViewGenerateScenariosButtonClicked_WhenCancelButtonClickedAndDialogClosed_ThenCalculationsNotUpdated() { // Given using (var treeViewControl = new TreeViewControl()) { var assessmentSection = mocks.Stub(); HeightStructure structure1 = new TestHeightStructure("Structure 1"); HeightStructure structure2 = new TestHeightStructure("Structure 2"); var failureMechanism = new HeightStructuresFailureMechanism { HeightStructures = { structure1, structure2 } }; var nodeData = new HeightStructuresCalculationGroupContext(failureMechanism.CalculationsGroup, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var mainWindow = mocks.Stub(); guiStub.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder); guiStub.Stub(g => g.MainWindow).Return(mainWindow); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); plugin.Gui = guiStub; DialogBoxHandler = (name, wnd) => { var selectionDialog = (StructureSelectionDialog) new FormTester(name).TheObject; var grid = (DataGridViewControl) new ControlTester("DataGridViewControl", selectionDialog).TheObject; grid.Rows[0].Cells[0].Value = true; new ButtonTester("CustomCancelButton", selectionDialog).Click(); }; using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // When contextMenu.Items[contextMenuGenerateCalculationsIndexRootGroup].PerformClick(); // Then Assert.AreEqual(0, failureMechanism.Calculations.OfType>().Count()); } } } [Test] public void GivenScenariosWithExistingCalculationWithSameName_WhenOkButtonClickedAndDialogClosed_ThenCalculationWithUniqueNameAdded() { // Given using (var treeViewControl = new TreeViewControl()) { var assessmentSection = mocks.Stub(); const string existingCalculationName = "Height structure"; HeightStructure heightStructure = new TestHeightStructure(existingCalculationName); var failureMechanism = new HeightStructuresFailureMechanism { HeightStructures = { heightStructure }, CalculationsGroup = { Children = { new StructuresCalculation { Name = existingCalculationName } } } }; var nodeData = new HeightStructuresCalculationGroupContext(failureMechanism.CalculationsGroup, failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var mainWindow = mocks.Stub(); guiStub.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder); guiStub.Stub(g => g.MainWindow).Return(mainWindow); guiStub.Stub(cmp => cmp.ViewCommands).Return(mocks.Stub()); mocks.ReplayAll(); plugin.Gui = guiStub; DialogBoxHandler = (name, wnd) => { var selectionDialog = (StructureSelectionDialog) new FormTester(name).TheObject; var grid = (DataGridViewControl) new ControlTester("DataGridViewControl", selectionDialog).TheObject; grid.Rows[0].Cells[0].Value = true; new ButtonTester("DoForSelectedButton", selectionDialog).Click(); }; using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { string expectedNewName = NamingHelper.GetUniqueName(failureMechanism.CalculationsGroup.Children, existingCalculationName, c => c.Name); // When contextMenu.Items[contextMenuGenerateCalculationsIndexRootGroup].PerformClick(); // Then StructuresCalculation[] heightStructuresCalculations = failureMechanism.Calculations.OfType>().ToArray(); Assert.AreEqual(2, heightStructuresCalculations.Length); Assert.AreEqual(expectedNewName, heightStructuresCalculations[1].Name); } } } [Test] public void OnNodeRemoved_NestedCalculationGroup_RemoveGroupAndNotifyObservers() { // Setup var observer = mocks.StrictMock(); var failureMechanism = new HeightStructuresFailureMechanism(); var assessmentSection = mocks.Stub(); var group = new CalculationGroup(); var parentGroup = new CalculationGroup(); var nodeData = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var parentNodeData = new HeightStructuresCalculationGroupContext(parentGroup, failureMechanism, assessmentSection); observer.Expect(o => o.UpdateObserver()); mocks.ReplayAll(); parentGroup.Children.Add(group); parentNodeData.Attach(observer); // Precondition Assert.IsTrue(info.CanRemove(nodeData, parentNodeData)); // Call info.OnNodeRemoved(nodeData, parentNodeData); // Assert CollectionAssert.DoesNotContain(parentGroup.Children, group); } [Test] public void OnNodeRemoved_CalculationInGroupAssignedToSection_CalculationDetachedFromSection() { // Setup var failureMechanism = new HeightStructuresFailureMechanism(); failureMechanism.AddSection(new FailureMechanismSection("section", new[] { new Point2D(0, 0) })); var assessmentSection = mocks.Stub(); var group = new CalculationGroup(); var parentGroup = new CalculationGroup(); var nodeData = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var parentNodeData = new HeightStructuresCalculationGroupContext(parentGroup, failureMechanism, assessmentSection); mocks.ReplayAll(); parentGroup.Children.Add(group); var calculation = new StructuresCalculation(); group.Children.Add(calculation); HeightStructuresFailureMechanismSectionResult result = failureMechanism.SectionResults.First(); result.Calculation = calculation; // Call info.OnNodeRemoved(nodeData, parentNodeData); // Assert Assert.IsNull(result.Calculation); } [Test] public void OnNodeRemoved_NestedCalculationGroupContainingCalculations_RemoveGroupAndCalculationsAndNotifyObservers() { // Setup var observer = mocks.StrictMock(); var assessmentSection = mocks.Stub(); var failureMechanism = new HeightStructuresFailureMechanism(); var group = new CalculationGroup(); var parentGroup = new CalculationGroup(); var nodeData = new HeightStructuresCalculationGroupContext(group, failureMechanism, assessmentSection); var parentNodeData = new HeightStructuresCalculationGroupContext(parentGroup, failureMechanism, assessmentSection); var calculation = new StructuresCalculation(); observer.Expect(o => o.UpdateObserver()); mocks.ReplayAll(); group.Children.Add(calculation); parentGroup.Children.Add(group); parentNodeData.Attach(observer); // Precondition Assert.IsTrue(info.CanRemove(nodeData, parentNodeData)); // Call info.OnNodeRemoved(nodeData, parentNodeData); // Assert CollectionAssert.DoesNotContain(parentGroup.Children, group); } public override void TearDown() { plugin.Dispose(); mocks.VerifyAll(); base.TearDown(); } } }