Index: Ringtoets/Common/src/Ringtoets.Common.Forms/TreeNodeInfos/RingtoetsContextMenuBuilder.cs =================================================================== diff -u -r9c0c1c0f3dee207a8f0c7e9e24df67a8e0b83a8e -r22c5092409b8d4cf1be4d3ebfdd41169c0e64c14 --- Ringtoets/Common/src/Ringtoets.Common.Forms/TreeNodeInfos/RingtoetsContextMenuBuilder.cs (.../RingtoetsContextMenuBuilder.cs) (revision 9c0c1c0f3dee207a8f0c7e9e24df67a8e0b83a8e) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/TreeNodeInfos/RingtoetsContextMenuBuilder.cs (.../RingtoetsContextMenuBuilder.cs) (revision 22c5092409b8d4cf1be4d3ebfdd41169c0e64c14) @@ -127,6 +127,24 @@ } /// + /// Adds an item to the , which duplicates a calculation. + /// + /// The type of the calculation. + /// The type of the calculation context. + /// The calculation to duplicate. + /// The calculation context belonging to the calculation. + /// The itself. + public RingtoetsContextMenuBuilder AddDuplicateCalculationItem( + TCalculation calculation, + TCalculationContext calculationContext) + where TCalculationContext : ICalculationContext + where TCalculation : ICalculation, ICloneable + { + contextMenuBuilder.AddCustomItem(RingtoetsContextMenuItemFactory.CreateDuplicateCalculationItem(calculation, calculationContext)); + return this; + } + + /// /// Adds an item to the , which performs a calculation. /// /// The type of the calculation. @@ -151,24 +169,6 @@ } /// - /// Adds an item to the , which duplicates a calculation. - /// - /// The type of the calculation. - /// The type of the calculation context. - /// The calculation to duplicate. - /// The calculation context belonging to the calculation. - /// The itself. - public RingtoetsContextMenuBuilder AddDuplicateCalculationItem( - TCalculation calculation, - TCalculationContext calculationContext) - where TCalculationContext : ICalculationContext - where TCalculation : ICalculation, ICloneable - { - contextMenuBuilder.AddCustomItem(RingtoetsContextMenuItemFactory.CreateDuplicateCalculationItem(calculation, calculationContext)); - return this; - } - - /// /// Adds an item to the , which validates a calculation. /// /// The type of the calculation context. Index: Ringtoets/Common/src/Ringtoets.Common.Forms/TreeNodeInfos/RingtoetsContextMenuItemFactory.cs =================================================================== diff -u -ra71d7ff366aba9e2090ba8b8c0dbc32f5c815ad0 -r22c5092409b8d4cf1be4d3ebfdd41169c0e64c14 --- Ringtoets/Common/src/Ringtoets.Common.Forms/TreeNodeInfos/RingtoetsContextMenuItemFactory.cs (.../RingtoetsContextMenuItemFactory.cs) (revision a71d7ff366aba9e2090ba8b8c0dbc32f5c815ad0) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/TreeNodeInfos/RingtoetsContextMenuItemFactory.cs (.../RingtoetsContextMenuItemFactory.cs) (revision 22c5092409b8d4cf1be4d3ebfdd41169c0e64c14) @@ -174,36 +174,6 @@ } /// - /// Creates a which is bound to the action of performing a calculation. - /// - /// The type of the calculation. - /// The type of the calculation context. - /// The calculation to perform. - /// The calculation context belonging to the calculation. - /// The action that performs the calculation. - /// The function which determines whether the item should be enabled. If the - /// item should not be enabled, then the reason for that should be returned by the function and will be shown as a tooltip. - /// If the item should be enabled then the function should return a null or empty string. - /// The created . - public static StrictContextMenuItem CreatePerformCalculationItem( - TCalculation calculation, - TCalculationContext calculationContext, - Action calculateAction, - Func enableMenuItemFunction) - where TCalculationContext : ICalculationContext - where TCalculation : ICalculation - { - var menuItem = new StrictContextMenuItem( - Resources.Calculate, - Resources.Calculate_ToolTip, - Resources.CalculateIcon, - (o, args) => calculateAction(calculation, calculationContext)); - - SetStateWithEnableFunction(calculationContext, enableMenuItemFunction, menuItem); - return menuItem; - } - - /// /// Creates a which is bound to the action of duplicating a calculation. /// /// The type of the calculation. @@ -238,6 +208,36 @@ } /// + /// Creates a which is bound to the action of performing a calculation. + /// + /// The type of the calculation. + /// The type of the calculation context. + /// The calculation to perform. + /// The calculation context belonging to the calculation. + /// The action that performs the calculation. + /// The function which determines whether the item should be enabled. If the + /// item should not be enabled, then the reason for that should be returned by the function and will be shown as a tooltip. + /// If the item should be enabled then the function should return a null or empty string. + /// The created . + public static StrictContextMenuItem CreatePerformCalculationItem( + TCalculation calculation, + TCalculationContext calculationContext, + Action calculateAction, + Func enableMenuItemFunction) + where TCalculationContext : ICalculationContext + where TCalculation : ICalculation + { + var menuItem = new StrictContextMenuItem( + Resources.Calculate, + Resources.Calculate_ToolTip, + Resources.CalculateIcon, + (o, args) => calculateAction(calculation, calculationContext)); + + SetStateWithEnableFunction(calculationContext, enableMenuItemFunction, menuItem); + return menuItem; + } + + /// /// Creates a which is bound to the action of validating the input of a calculation. /// /// The type of the calculation context. Index: Ringtoets/Common/test/Ringtoets.Common.Forms.Test/TreeNodeInfos/RingtoetsContextMenuItemFactoryTest.cs =================================================================== diff -u -r8b60c9e846480f3ffeeb263bfea5d3367bf6bee3 -r22c5092409b8d4cf1be4d3ebfdd41169c0e64c14 --- Ringtoets/Common/test/Ringtoets.Common.Forms.Test/TreeNodeInfos/RingtoetsContextMenuItemFactoryTest.cs (.../RingtoetsContextMenuItemFactoryTest.cs) (revision 8b60c9e846480f3ffeeb263bfea5d3367bf6bee3) +++ Ringtoets/Common/test/Ringtoets.Common.Forms.Test/TreeNodeInfos/RingtoetsContextMenuItemFactoryTest.cs (.../RingtoetsContextMenuItemFactoryTest.cs) (revision 22c5092409b8d4cf1be4d3ebfdd41169c0e64c14) @@ -20,6 +20,7 @@ // All rights reserved. using System; +using System.Collections.Generic; using System.Drawing; using System.Linq; using Core.Common.Base; @@ -102,7 +103,7 @@ var calculationGroupContext = new TestCalculationGroupContext(calculationGroup, parent, failureMechanism); // Call - StrictContextMenuItem toolStripItem = RingtoetsContextMenuItemFactory.CreateAddCalculationItem(calculationGroupContext, context => { }); + StrictContextMenuItem toolStripItem = RingtoetsContextMenuItemFactory.CreateAddCalculationItem(calculationGroupContext, context => {}); // Assert Assert.AreEqual("Berekening &toevoegen", toolStripItem.Text); @@ -598,6 +599,141 @@ mocks.VerifyAll(); } + #region CreateDuplicateCalculationItem + + private static IEnumerable CalculationGroupConfigurations + { + get + { + var calculation = new TestCloneableCalculation + { + Name = "Nieuwe berekening" + }; + + yield return new TestCaseData(calculation, + new CalculationGroup + { + Children = + { + calculation + } + }, + "Kopie van Nieuwe berekening") + .SetName("NameOfStandardCopyUnique"); + yield return new TestCaseData(calculation, + new CalculationGroup + { + Children = + { + calculation, + new TestCalculation + { + Name = "Kopie van Nieuwe berekening" + } + } + }, + "Kopie van Nieuwe berekening (1)") + .SetName("NameOfStandardCopySameAsOtherCalculation"); + yield return new TestCaseData(calculation, + new CalculationGroup + { + Children = + { + new CalculationGroup + { + Name = "Kopie van Nieuwe berekening" + }, + calculation + } + }, + "Kopie van Nieuwe berekening (1)") + .SetName("NameOfStandardCopySameAsOtherCalculationGroup"); + } + } + + [Test] + public void CreateDuplicateCalculationItem_Always_CreatesDecoratedAndEnabledItem() + { + // Setup + var mocks = new MockRepository(); + var calculation = mocks.Stub(); + var calculationContext = mocks.Stub>(); + mocks.ReplayAll(); + + // Call + StrictContextMenuItem toolStripItem = RingtoetsContextMenuItemFactory.CreateDuplicateCalculationItem(calculation, calculationContext); + + // Assert + Assert.AreEqual("D&upliceren", toolStripItem.Text); + Assert.AreEqual("Dupliceer deze berekening.", toolStripItem.ToolTipText); + TestHelper.AssertImagesAreEqual(RingtoetsFormsResources.CopyHS, toolStripItem.Image); + Assert.IsTrue(toolStripItem.Enabled); + + mocks.VerifyAll(); + } + + [Test] + [TestCaseSource("CalculationGroupConfigurations")] + public void CreateDuplicateCalculationItem_PerformClickOnCreatedItem_DuplicatesCalculationWithExpectedNameAndPosition(TestCloneableCalculation calculation, + CalculationGroup calculationGroup, + string expectedCalculationName) + { + // Setup + var mocks = new MockRepository(); + var calculationContext = mocks.Stub>(); + + calculationContext.Stub(c => c.Parent).Return(calculationGroup); + + mocks.ReplayAll(); + + StrictContextMenuItem toolStripItem = RingtoetsContextMenuItemFactory.CreateDuplicateCalculationItem(calculation, calculationContext); + + List originalChildren = calculationGroup.Children.ToList(); + + // Call + toolStripItem.PerformClick(); + + // Assert + ICalculationBase copiedItem = calculationGroup.Children.Except(originalChildren).SingleOrDefault(); + Assert.IsNotNull(copiedItem); + Assert.AreEqual(expectedCalculationName, copiedItem.Name); + Assert.AreEqual(originalChildren.IndexOf(calculation) + 1, calculationGroup.Children.IndexOf(copiedItem)); + + mocks.VerifyAll(); + } + + public interface ICloneableCalculation : ICalculation, ICloneable {} + + public class TestCloneableCalculation : Observable, ICloneableCalculation + { + public string Name { get; set; } + + public bool HasOutput + { + get + { + return false; + } + } + + public Comment Comments + { + get + { + return null; + } + } + + public void ClearOutput() {} + + public object Clone() + { + return MemberwiseClone(); + } + } + + #endregion + #region CreateUpdateForeshoreProfileOfCalculationItem [Test] @@ -616,7 +752,7 @@ // Call StrictContextMenuItem toolStripItem = RingtoetsContextMenuItemFactory.CreateUpdateForeshoreProfileOfCalculationItem( calculation, - inquiryHelper, c => { }); + inquiryHelper, c => {}); // Assert Assert.AreEqual("&Bijwerken voorlandprofiel...", toolStripItem.Text); @@ -648,7 +784,7 @@ // Call StrictContextMenuItem toolStripItem = RingtoetsContextMenuItemFactory.CreateUpdateForeshoreProfileOfCalculationItem( calculation, - inquiryHelper, c => { }); + inquiryHelper, c => {}); // Assert Assert.AreEqual("&Bijwerken voorlandprofiel...", toolStripItem.Text); @@ -756,7 +892,7 @@ { calculation }, - inquiryHelper, c => { }); + inquiryHelper, c => {}); // Assert Assert.AreEqual("&Bijwerken voorlandprofielen...", toolStripItem.Text);