// 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; using System.Drawing; using System.Linq; using System.Windows.Forms; using Core.Common.Base; 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; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.TestUtil; using Ringtoets.Common.Forms.PresentationObjects; using Ringtoets.Common.Service.TestUtil; using Ringtoets.MacroStabilityInwards.Data; using Ringtoets.MacroStabilityInwards.Data.TestUtil; using Ringtoets.MacroStabilityInwards.Forms.PresentationObjects; using Ringtoets.MacroStabilityInwards.KernelWrapper.TestUtil.Calculators; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; using CoreCommonGuiResources = Core.Common.Gui.Properties.Resources; namespace Ringtoets.MacroStabilityInwards.Plugin.Test.TreeNodeInfos { [TestFixture] public class MacroStabilityInwardsFailureMechanismContextTreeNodeInfoTest : NUnitFormTest { private const int contextMenuRelevancyIndexWhenRelevant = 2; private const int contextMenuRelevancyIndexWhenNotRelevant = 0; private const int contextMenuValidateAllIndex = 4; private const int contextMenuCalculateAllIndex = 5; private const int contextMenuClearIndex = 7; private MockRepository mocks; private MacroStabilityInwardsPlugin plugin; private TreeNodeInfo info; [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_ReturnsName() { // Setup var assessmentSection = mocks.Stub(); mocks.ReplayAll(); var mechanism = new MacroStabilityInwardsFailureMechanism(); var mechanismContext = new MacroStabilityInwardsFailureMechanismContext(mechanism, assessmentSection); // Call string text = info.Text(mechanismContext); // Assert Assert.AreEqual("Dijken en dammen - Macrostabiliteit binnenwaarts", text); } [Test] public void Image_Always_ReturnsMacroStabilityInwardsIcon() { // Setup mocks.ReplayAll(); // Call Image image = info.Image(null); // Assert TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.CalculationIcon, image); } [Test] public void ChildNodeObjects_FailureMechanismIsRelevant_ReturnChildDataNodes() { // Setup var assessmentSection = mocks.Stub(); mocks.ReplayAll(); var failureMechanism = new MacroStabilityInwardsFailureMechanism(); failureMechanism.CalculationsGroup.Children.Add(new MacroStabilityInwardsCalculationScenario()); failureMechanism.CalculationsGroup.Children.Add(new MacroStabilityInwardsCalculationScenario()); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(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(4, inputsFolder.Contents.Count()); var failureMechanismSectionsContext = (FailureMechanismSectionsContext) inputsFolder.Contents.ElementAt(0); Assert.AreSame(failureMechanism, failureMechanismSectionsContext.WrappedData); Assert.AreSame(assessmentSection, failureMechanismSectionsContext.AssessmentSection); var surfaceLinesContext = (MacroStabilityInwardsSurfaceLinesContext) inputsFolder.Contents.ElementAt(1); Assert.AreSame(failureMechanism.SurfaceLines, surfaceLinesContext.WrappedData); Assert.AreSame(failureMechanism, surfaceLinesContext.FailureMechanism); Assert.AreSame(assessmentSection, surfaceLinesContext.AssessmentSection); var stochasticSoilModelContext = (MacroStabilityInwardsStochasticSoilModelCollectionContext) inputsFolder.Contents.ElementAt(2); Assert.AreSame(failureMechanism, stochasticSoilModelContext.FailureMechanism); Assert.AreSame(failureMechanism, stochasticSoilModelContext.FailureMechanism); Assert.AreSame(assessmentSection, stochasticSoilModelContext.AssessmentSection); var comment = (Comment) inputsFolder.Contents.ElementAt(3); Assert.AreSame(failureMechanism.InputComments, comment); var calculationsFolder = (MacroStabilityInwardsCalculationGroupContext) children[1]; Assert.AreSame(failureMechanism.CalculationsGroup, calculationsFolder.WrappedData); Assert.IsNull(calculationsFolder.Parent); Assert.AreSame(failureMechanism.SurfaceLines, calculationsFolder.AvailableMacroStabilityInwardsSurfaceLines); Assert.AreSame(failureMechanism.StochasticSoilModels, calculationsFolder.AvailableStochasticSoilModels); Assert.AreSame(failureMechanism, calculationsFolder.FailureMechanism); var outputsFolder = (CategoryTreeFolder) children[2]; Assert.AreEqual("Oordeel", outputsFolder.Name); Assert.AreEqual(TreeFolderCategory.Output, outputsFolder.Category); Assert.AreEqual(3, outputsFolder.Contents.Count()); var failureMechanismScenariosContext = (MacroStabilityInwardsScenariosContext) outputsFolder.Contents.ElementAt(0); Assert.AreSame(failureMechanism, failureMechanismScenariosContext.FailureMechanism); Assert.AreSame(failureMechanism.CalculationsGroup, failureMechanismScenariosContext.WrappedData); Assert.AreSame(assessmentSection, failureMechanismScenariosContext.AssessmentSection); var failureMechanismResultsContext = (ProbabilityFailureMechanismSectionResultContext) outputsFolder.Contents.ElementAt(1); Assert.AreSame(failureMechanism, failureMechanismResultsContext.FailureMechanism); Assert.AreSame(failureMechanism.SectionResults, failureMechanismResultsContext.WrappedData); Assert.AreSame(assessmentSection, failureMechanismResultsContext.AssessmentSection); var commentContext = (Comment) outputsFolder.Contents.ElementAt(2); Assert.AreSame(failureMechanism.OutputComments, commentContext); } [Test] public void ChildNodeObjects_FailureMechanismIsNotRelevant_ReturnOnlyFailureMechanismNotRelevantComments() { // Setup var assessmentSection = mocks.Stub(); mocks.ReplayAll(); var failureMechanism = new MacroStabilityInwardsFailureMechanism { IsRelevant = false }; failureMechanism.CalculationsGroup.Children.Add(new MacroStabilityInwardsCalculationScenario()); failureMechanism.CalculationsGroup.Children.Add(new MacroStabilityInwardsCalculationScenario()); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(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] [TestCase(false, TestName = "GivenMultipleCalculations_WhenClearingOutputFromContextMenu_ThenOutputCleared(false)")] [TestCase(true, TestName = "GivenMultipleCalculations_WhenClearingOutputFromContextMenu_ThenOutputCleared(true)")] public void GivenMultipleCalculationsWithOutput_WhenClearingOutputFromContextMenu_ThenOutputCleared(bool confirm) { // Given using (var treeViewControl = new TreeViewControl()) { var calculation1 = new MacroStabilityInwardsCalculationScenario { Output = MacroStabilityInwardsOutputTestFactory.CreateOutput() }; var calculation2 = new MacroStabilityInwardsCalculationScenario { Output = MacroStabilityInwardsOutputTestFactory.CreateOutput() }; var observer = mocks.StrictMock(); if (confirm) { observer.Expect(o => o.UpdateObserver()).Repeat.Twice(); } var failureMechanism = new MacroStabilityInwardsFailureMechanism(); failureMechanism.CalculationsGroup.Children.Add(calculation1); failureMechanism.CalculationsGroup.Children.Add(calculation2); var assessmentSection = mocks.Stub(); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var gui = mocks.Stub(); gui.Stub(cmp => cmp.Get(failureMechanismContext, treeViewControl)).Return(menuBuilder); mocks.ReplayAll(); plugin.Gui = gui; failureMechanism.CalculationsGroup.Children.Add(calculation1); failureMechanism.CalculationsGroup.Children.Add(calculation2); failureMechanism.CalculationsGroup.Children.ElementAt(0).Attach(observer); failureMechanism.CalculationsGroup.Children.ElementAt(1).Attach(observer); string messageBoxTitle = null, messageBoxText = null; DialogBoxHandler = (name, wnd) => { var messageBox = new MessageBoxTester(wnd); messageBoxText = messageBox.Text; messageBoxTitle = messageBox.Title; if (confirm) { messageBox.ClickOk(); } else { messageBox.ClickCancel(); } }; using (ContextMenuStrip contextMenuStrip = info.ContextMenuStrip(failureMechanismContext, null, treeViewControl)) { // When contextMenuStrip.Items[contextMenuClearIndex].PerformClick(); // Then foreach (ICalculation calc in failureMechanism.CalculationsGroup.Children.OfType()) { Assert.AreNotEqual(confirm, calc.HasOutput); } Assert.AreEqual("Bevestigen", messageBoxTitle); Assert.AreEqual("Weet u zeker dat u alle uitvoer wilt wissen?", messageBoxText); } } } [Test] public void ContextMenuStrip_HasCalculationWithOutput_ReturnsContextMenuWithCommonItems() { // Setup var failureMechanism = new TestMacroStabilityInwardsFailureMechanism(); var calculation = new MacroStabilityInwardsCalculationScenario { Output = MacroStabilityInwardsOutputTestFactory.CreateOutput() }; failureMechanism.CalculationsGroup.Children.Add(calculation); var assessmentSection = mocks.Stub(); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(failureMechanism, assessmentSection); var applicationFeatureCommandHandler = mocks.Stub(); var importCommandHandler = mocks.Stub(); var exportCommandHandler = mocks.Stub(); var updateCommandHandler = mocks.Stub(); var viewCommandsHandler = mocks.Stub(); using (var treeViewControl = new TreeViewControl()) { var menuBuilder = new ContextMenuBuilder(applicationFeatureCommandHandler, importCommandHandler, exportCommandHandler, updateCommandHandler, viewCommandsHandler, failureMechanismContext, treeViewControl); var gui = mocks.Stub(); gui.Stub(cmp => cmp.Get(failureMechanismContext, treeViewControl)).Return(menuBuilder); mocks.ReplayAll(); plugin.Gui = gui; // Call using (ContextMenuStrip menu = info.ContextMenuStrip(failureMechanismContext, null, treeViewControl)) { // Assert Assert.AreEqual(13, menu.Items.Count); TestHelper.AssertContextMenuStripContainsItem(menu, 0, "&Openen", "Open de gegevens in een nieuw documentvenster.", CoreCommonGuiResources.OpenIcon, false); TestHelper.AssertContextMenuStripContainsItem(menu, 2, "I&s relevant", "Geeft aan of dit toetsspoor relevant is of niet.", RingtoetsCommonFormsResources.Checkbox_ticked); TestHelper.AssertContextMenuStripContainsItem(menu, 4, "Alles &valideren", "Valideer alle berekeningen binnen dit toetsspoor.", RingtoetsCommonFormsResources.ValidateAllIcon); TestHelper.AssertContextMenuStripContainsItem(menu, 5, "Alles be&rekenen", "Voer alle berekeningen binnen dit toetsspoor uit.", RingtoetsCommonFormsResources.CalculateAllIcon); TestHelper.AssertContextMenuStripContainsItem(menu, 7, "&Wis alle uitvoer...", "Wis de uitvoer van alle berekeningen binnen dit toetsspoor.", RingtoetsCommonFormsResources.ClearIcon); TestHelper.AssertContextMenuStripContainsItem(menu, 9, "Alles i&nklappen", "Klap dit element en alle onderliggende elementen in.", CoreCommonGuiResources.CollapseAllIcon, false); TestHelper.AssertContextMenuStripContainsItem(menu, 10, "Alles ui&tklappen", "Klap dit element en alle onderliggende elementen uit.", CoreCommonGuiResources.ExpandAllIcon, false); TestHelper.AssertContextMenuStripContainsItem(menu, 12, "Ei&genschappen", "Toon de eigenschappen in het Eigenschappenpaneel.", CoreCommonGuiResources.PropertiesHS, false); CollectionAssert.AllItemsAreInstancesOfType(new[] { menu.Items[1], menu.Items[3], menu.Items[8], menu.Items[11] }, typeof(ToolStripSeparator)); } } } [Test] public void ContextMenuStrip_MacroStabilityInwardsFailureMechanismNoOutput_ContextMenuItemClearAllOutputDisabled() { // Setup using (var treeViewControl = new TreeViewControl()) { var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var data = mocks.StrictMock(); data.Stub(dm => dm.Calculations).Return(new ICalculation[0]); var assessmentSection = mocks.Stub(); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(data, assessmentSection); var gui = mocks.Stub(); gui.Stub(cmp => cmp.Get(failureMechanismContext, treeViewControl)).Return(menuBuilder); mocks.ReplayAll(); plugin.Gui = gui; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(failureMechanismContext, null, treeViewControl)) { // Assert ToolStripItem clearOutputItem = contextMenu.Items[contextMenuClearIndex]; Assert.IsFalse(clearOutputItem.Enabled); Assert.AreEqual("Er zijn geen berekeningen met uitvoer om te wissen.", clearOutputItem.ToolTipText); } } } [Test] public void ContextMenuStrip_MacroStabilityInwardsFailureMechanismWithOutput_ContextMenuItemClearAllOutputEnabled() { // Setup using (var treeViewControl = new TreeViewControl()) { var calculation = new MacroStabilityInwardsCalculationScenario { Output = MacroStabilityInwardsOutputTestFactory.CreateOutput() }; var failureMechanism = new MacroStabilityInwardsFailureMechanism(); failureMechanism.CalculationsGroup.Children.Add(calculation); var assessmentSection = mocks.Stub(); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var gui = mocks.Stub(); gui.Stub(cmp => cmp.Get(failureMechanismContext, treeViewControl)).Return(menuBuilder); mocks.ReplayAll(); plugin.Gui = gui; failureMechanism.CalculationsGroup.Children.Add(calculation); // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(failureMechanismContext, null, treeViewControl)) { // Assert ToolStripItem clearOutputItem = contextMenu.Items[contextMenuClearIndex]; Assert.IsTrue(clearOutputItem.Enabled); Assert.AreEqual("Wis de uitvoer van alle berekeningen binnen dit toetsspoor.", clearOutputItem.ToolTipText); } } } [Test] public void ContextMenuStrip_FailureMechanismContributionZero_ContextMenuItemCalculateAllAndValidateAllDisabledAndTooltipSet() { // Setup using (var treeViewControl = new TreeViewControl()) { var failureMechanism = new MacroStabilityInwardsFailureMechanism { CalculationsGroup = { Children = { MacroStabilityInwardsCalculationScenarioFactory.CreateMacroStabilityInwardsCalculationScenarioWithValidInput() } } }; var assessmentSection = mocks.Stub(); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var gui = mocks.Stub(); gui.Stub(cmp => cmp.Get(failureMechanismContext, treeViewControl)).Return(menuBuilder); mocks.ReplayAll(); plugin.Gui = gui; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(failureMechanismContext, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuCalculateAllIndex, "Alles be&rekenen", "De bijdrage van dit toetsspoor is nul.", RingtoetsCommonFormsResources.CalculateAllIcon, false); TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateAllIndex, "Alles &valideren", "De bijdrage van dit toetsspoor is nul.", RingtoetsCommonFormsResources.ValidateAllIcon, false); } } } [Test] public void ContextMenuStrip_AllRequiredInputSet_ContextMenuItemCalculateAllAndValidateAllEnabled() { // Setup using (var treeViewControl = new TreeViewControl()) { var failureMechanism = new TestMacroStabilityInwardsFailureMechanism { CalculationsGroup = { Children = { MacroStabilityInwardsCalculationScenarioFactory.CreateMacroStabilityInwardsCalculationScenarioWithValidInput() } } }; var assessmentSection = mocks.Stub(); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var gui = mocks.Stub(); gui.Stub(cmp => cmp.Get(failureMechanismContext, treeViewControl)).Return(menuBuilder); mocks.ReplayAll(); plugin.Gui = gui; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(failureMechanismContext, null, treeViewControl)) { // Assert TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuCalculateAllIndex, "Alles be&rekenen", "Voer alle berekeningen binnen dit toetsspoor uit.", RingtoetsCommonFormsResources.CalculateAllIcon); TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateAllIndex, "Alles &valideren", "Valideer alle berekeningen binnen dit toetsspoor.", RingtoetsCommonFormsResources.ValidateAllIcon); } } } [Test] public void ContextMenuStrip_FailureMechanismIsRelevant_CallsContextMenuBuilderMethods() { // Setup using (var treeViewControl = new TreeViewControl()) { var failureMechanism = new MacroStabilityInwardsFailureMechanism(); var assessmentSection = mocks.Stub(); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(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.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 MacroStabilityInwardsFailureMechanism { IsRelevant = false }; var assessmentSection = mocks.Stub(); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(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.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_ClickOnValidateAllItem_ValidateAllChildCalculations() { // Setup using (var treeViewControl = new TreeViewControl()) { var failureMechanism = new TestMacroStabilityInwardsFailureMechanism(); MacroStabilityInwardsCalculationScenario validCalculation = MacroStabilityInwardsCalculationScenarioFactory.CreateMacroStabilityInwardsCalculationScenarioWithValidInput(); validCalculation.Name = "A"; MacroStabilityInwardsCalculationScenario invalidCalculation = MacroStabilityInwardsCalculationScenarioFactory.CreateMacroStabilityInwardsCalculationScenarioWithInvalidInput(); invalidCalculation.Name = "B"; failureMechanism.CalculationsGroup.Children.Add(validCalculation); failureMechanism.CalculationsGroup.Children.Add(invalidCalculation); var assessmentSection = mocks.Stub(); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(failureMechanism, assessmentSection); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var gui = mocks.Stub(); gui.Stub(g => g.Get(failureMechanismContext, treeViewControl)).Return(menuBuilder); mocks.ReplayAll(); plugin.Gui = gui; using (ContextMenuStrip contextMenu = info.ContextMenuStrip(failureMechanismContext, null, treeViewControl)) using (new MacroStabilityInwardsCalculatorFactoryConfig()) { // Call Action call = () => contextMenu.Items[contextMenuValidateAllIndex].PerformClick(); // Assert TestHelper.AssertLogMessages(call, messages => { string[] msgs = messages.ToArray(); Assert.AreEqual(7, msgs.Length); CalculationServiceTestHelper.AssertValidationStartMessage(msgs[0]); CalculationServiceTestHelper.AssertValidationEndMessage(msgs[1]); CalculationServiceTestHelper.AssertValidationStartMessage(msgs[2]); CalculationServiceTestHelper.AssertValidationEndMessage(msgs[6]); }); } } } [Test] public void ContextMenuStrip_ClickOnCalculateAllItem_ScheduleAllChildCalculations() { // Setup using (var treeViewControl = new TreeViewControl()) { var failureMechanism = new TestMacroStabilityInwardsFailureMechanism(); MacroStabilityInwardsCalculationScenario validCalculation = MacroStabilityInwardsCalculationScenarioFactory.CreateMacroStabilityInwardsCalculationScenarioWithValidInput(); validCalculation.Name = "A"; MacroStabilityInwardsCalculationScenario invalidCalculation = MacroStabilityInwardsCalculationScenarioFactory.CreateMacroStabilityInwardsCalculationScenarioWithInvalidInput(); invalidCalculation.Name = "B"; failureMechanism.CalculationsGroup.Children.Add(validCalculation); failureMechanism.CalculationsGroup.Children.Add(invalidCalculation); IAssessmentSection assessmentSection = AssessmentSectionHelper.CreateAssessmentSectionStub(failureMechanism, mocks); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(failureMechanism, assessmentSection); var mainWindow = mocks.Stub(); var menuBuilder = new CustomItemsOnlyContextMenuBuilder(); var gui = mocks.Stub(); gui.Stub(g => g.Get(failureMechanismContext, treeViewControl)).Return(menuBuilder); gui.Stub(g => g.MainWindow).Return(mainWindow); mocks.ReplayAll(); plugin.Gui = gui; DialogBoxHandler = (name, wnd) => { // Expect an activity dialog which is automatically closed }; using (ContextMenuStrip contextMenu = info.ContextMenuStrip(failureMechanismContext, null, treeViewControl)) { // Call contextMenu.Items[contextMenuCalculateAllIndex].PerformClick(); } } // Assert // Assert expectancies are called in TearDown() } [Test] public void ContextMenuStrip_FailureMechanismIsRelevantAndClickOnIsRelevantItem_MakeFailureMechanismNotRelevant() { // Setup using (var treeViewControl = new TreeViewControl()) { var failureMechanismObserver = mocks.Stub(); failureMechanismObserver.Expect(o => o.UpdateObserver()); var failureMechanism = new MacroStabilityInwardsFailureMechanism(); failureMechanism.Attach(failureMechanismObserver); var assessmentSection = mocks.Stub(); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(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 MacroStabilityInwardsFailureMechanism { IsRelevant = false }; failureMechanism.Attach(failureMechanismObserver); var assessmentSection = mocks.Stub(); var failureMechanismContext = new MacroStabilityInwardsFailureMechanismContext(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); } } } public override void Setup() { mocks = new MockRepository(); plugin = new MacroStabilityInwardsPlugin(); info = plugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(MacroStabilityInwardsFailureMechanismContext)); } public override void TearDown() { plugin.Dispose(); mocks.VerifyAll(); base.TearDown(); } } }