// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Drawing; using System.IO; using System.Linq; using System.Windows.Forms; using Core.Common.Base.Data; using Core.Common.Controls.TreeView; using Core.Common.Gui; using Core.Common.Gui.ContextMenu; using Core.Common.Gui.Forms.MainWindow; using Core.Common.Gui.Forms.ViewHost; 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.TestUtil; using Ringtoets.HydraRing.Calculation.TestUtil.Calculator; using Ringtoets.HydraRing.Data; using Ringtoets.Integration.Data; using Ringtoets.Integration.Forms.PresentationObjects; using Ringtoets.Integration.Plugin; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; namespace Ringtoets.Integration.Forms.Test.TreeNodeInfos { [TestFixture] public class WaveHeightLocationsContextTreeNodeInfoTest : NUnitFormTest { private const int contextMenuRunWaveHeightCalculationsIndex = 2; private MockRepository mockRepository; private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Integration.Service, "HydraRingCalculation"); [SetUp] public void SetUp() { mockRepository = new MockRepository(); } [Test] public void Initialized_Always_ExpectedPropertiesSet() { // Setup using (var plugin = new RingtoetsPlugin()) { TreeNodeInfo info = GetInfo(plugin); // Assert Assert.AreEqual(typeof(WaveHeightLocationsContext), info.TagType); 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); Assert.IsNull(info.EnsureVisibleOnCreate); Assert.IsNull(info.ChildNodeObjects); Assert.IsNull(info.CanRename); Assert.IsNull(info.OnNodeRenamed); Assert.IsNull(info.CanRemove); Assert.IsNull(info.OnNodeRemoved); } } [Test] public void Text_Always_ReturnsSetName() { // Setup using (var plugin = new RingtoetsPlugin()) { TreeNodeInfo info = GetInfo(plugin); // Call string text = info.Text(null); // Assert const string expectedName = "Golfhoogtes"; Assert.AreEqual(expectedName, text); } } [Test] public void Image_Always_ReturnsGenericIcon() { // Setup using (var plugin = new RingtoetsPlugin()) { TreeNodeInfo info = GetInfo(plugin); // Call Image image = info.Image(null); // Assert TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.GenericInputOutputIcon, image); } } [Test] public void ContextMenuStrip_Always_CallsContextMenuBuilderMethods() { // Setup var guiMock = mockRepository.StrictMock(); var menuBuilderMock = mockRepository.StrictMock(); var assessmentSectionMock = mockRepository.Stub(); var nodeData = new WaveHeightLocationsContext(assessmentSectionMock); menuBuilderMock.Expect(mb => mb.AddOpenItem()).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.AddPropertiesItem()).Return(menuBuilderMock); menuBuilderMock.Expect(mb => mb.Build()).Return(null); guiMock.Stub(g => g.ProjectOpened += null).IgnoreArguments(); guiMock.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilderMock); mockRepository.ReplayAll(); using (var plugin = new RingtoetsPlugin()) { TreeNodeInfo info = GetInfo(plugin); plugin.Gui = guiMock; // Call info.ContextMenuStrip(nodeData, null, treeViewControl); } } // Assert mockRepository.VerifyAll(); } [Test] public void ContextMenuStrip_NoHydraulicBoundaryDatabaseSet_ContextMenuItemCalculateDisabled() { // Setup var guiMock = mockRepository.StrictMock(); var assessmentSectionMock = mockRepository.Stub(); var nodeData = new WaveHeightLocationsContext(assessmentSectionMock); guiMock.Stub(g => g.ProjectOpened += null).IgnoreArguments(); guiMock.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); mockRepository.ReplayAll(); using (var plugin = new RingtoetsPlugin()) { TreeNodeInfo info = GetInfo(plugin); plugin.Gui = guiMock; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert const string expectedItemText = "Alles be&rekenen"; const string expectedItemTooltip = "Er is geen hydraulische randvoorwaardendatabase beschikbaar om de golfhoogtes te berekenen."; TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuRunWaveHeightCalculationsIndex, expectedItemText, expectedItemTooltip, RingtoetsCommonFormsResources.CalculateAllIcon, false); } } } mockRepository.VerifyAll(); // Expect no calls on arguments } [Test] public void ContextMenuStrip_HydraulicBoundaryDatabaseSet_ContextMenuItemCalculateEnabled() { // Setup var guiMock = mockRepository.StrictMock(); var assessmentSectionMock = mockRepository.Stub(); var nodeData = new WaveHeightLocationsContext(assessmentSectionMock) { WrappedData = { HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase() } }; guiMock.Stub(g => g.ProjectOpened += null).IgnoreArguments(); guiMock.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); mockRepository.ReplayAll(); using (var plugin = new RingtoetsPlugin()) { TreeNodeInfo info = GetInfo(plugin); plugin.Gui = guiMock; // Call using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) { // Assert const string expectedItemText = "Alles be&rekenen"; const string expectedItemTooltip = "Alle golfhoogtes berekenen."; TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuRunWaveHeightCalculationsIndex, expectedItemText, expectedItemTooltip, RingtoetsCommonFormsResources.CalculateAllIcon); } } } mockRepository.VerifyAll(); // Expect no calls on arguments } [Test] public void ForeColor_ContextHasNoHydraulicBoundaryDatabase_ReturnDisabledColor() { // Setup var assessmentSectionMock = mockRepository.Stub(); mockRepository.ReplayAll(); var context = new WaveHeightLocationsContext(assessmentSectionMock); using (var plugin = new RingtoetsPlugin()) { TreeNodeInfo info = GetInfo(plugin); // Call Color color = info.ForeColor(context); // Assert Assert.AreEqual(Color.FromKnownColor(KnownColor.GrayText), color); } mockRepository.VerifyAll(); } [Test] public void ForeColor_ContextHasHydraulicBoundaryDatabase_ReturnControlColor() { // Setup var assessmentSectionMock = mockRepository.Stub(); assessmentSectionMock.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); mockRepository.ReplayAll(); var context = new WaveHeightLocationsContext(assessmentSectionMock); using (var plugin = new RingtoetsPlugin()) { TreeNodeInfo info = GetInfo(plugin); // Call Color color = info.ForeColor(context); // Assert Assert.AreEqual(Color.FromKnownColor(KnownColor.ControlText), color); } mockRepository.VerifyAll(); } [Test] [RequiresSTA] public void GivenHydraulicBoundaryDatabaseWithNonExistingFilePath_WhenCalculatingWaveHeightFromContextMenu_ThenLogMessagesAddedPreviousOutputNotAffected() { // Given var guiMock = mockRepository.DynamicMock(); RoundedDouble waveHeight = (RoundedDouble) 4.2; var hydraulicBoundaryLocation1 = new HydraulicBoundaryLocation(100001, "", 1.1, 2.2); var hydraulicBoundaryLocation2 = new HydraulicBoundaryLocation(100002, "", 3.3, 4.4) { WaveHeight = waveHeight }; var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase { Locations = { hydraulicBoundaryLocation1, hydraulicBoundaryLocation2 }, FilePath = "D:/nonExistingDirectory/nonExistingFile", Version = "random" }; var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike) { HydraulicBoundaryDatabase = hydraulicBoundaryDatabase }; var context = new WaveHeightLocationsContext(assessmentSection); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(g => g.Get(context, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); guiMock.Expect(g => g.MainWindow).Return(mockRepository.Stub()); guiMock.Expect(g => g.DocumentViewController).Return(mockRepository.Stub()); mockRepository.ReplayAll(); using (var plugin = new RingtoetsPlugin()) { TreeNodeInfo info = GetInfo(plugin); plugin.Gui = guiMock; plugin.Activate(); using (ContextMenuStrip contextMenuAdapter = info.ContextMenuStrip(context, null, treeViewControl)) { // When Action action = () => contextMenuAdapter.Items[contextMenuRunWaveHeightCalculationsIndex].PerformClick(); // Then string message = string.Format("Berekeningen konden niet worden gestart. Fout bij het lezen van bestand '{0}': Het bestand bestaat niet.", hydraulicBoundaryDatabase.FilePath); TestHelper.AssertLogMessageWithLevelIsGenerated(action, new Tuple(message, LogLevelConstant.Error)); Assert.IsNaN(hydraulicBoundaryLocation1.WaveHeight); // No result set // Previous result not cleared Assert.AreEqual(waveHeight, hydraulicBoundaryLocation2.WaveHeight, hydraulicBoundaryLocation2.WaveHeight.GetAccuracy()); } } } mockRepository.VerifyAll(); } [Test] [RequiresSTA] public void GivenHydraulicBoundaryLocationThatSucceeds_CalculatingWaveHeightFromContextMenu_ThenLogMessagesAddedPreviousOutputAffected() { // Given var guiMock = mockRepository.DynamicMock(); const string locationName = "locationName"; var location = new HydraulicBoundaryLocation(1, locationName, 1.1, 2.2); var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike) { HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase { Locations = { location }, FilePath = Path.Combine(testDataPath, "HRD ijsselmeer.sqlite") }, Id = string.Empty }; var context = new WaveHeightLocationsContext(assessmentSection); using (var treeViewControl = new TreeViewControl()) { guiMock.Expect(g => g.Get(context, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); guiMock.Expect(g => g.MainWindow).Return(mockRepository.Stub()); guiMock.Expect(g => g.DocumentViewController).Return(mockRepository.Stub()); mockRepository.ReplayAll(); DialogBoxHandler = (name, wnd) => { // Expect an activity dialog which is automatically closed }; using (var plugin = new RingtoetsPlugin()) { TreeNodeInfo info = GetInfo(plugin); plugin.Gui = guiMock; plugin.Activate(); using (ContextMenuStrip contextMenuAdapter = info.ContextMenuStrip(context, null, treeViewControl)) using (new HydraRingCalculatorFactoryConfig()) { // When Action action = () => contextMenuAdapter.Items[contextMenuRunWaveHeightCalculationsIndex].PerformClick(); // Then TestHelper.AssertLogMessages(action, messages => { var msgs = messages.ToArray(); Assert.AreEqual(7, msgs.Length); StringAssert.StartsWith(string.Format("Validatie van 'Golfhoogte berekenen voor locatie '{0}'' gestart om:", locationName), msgs[0]); StringAssert.StartsWith(string.Format("Validatie van 'Golfhoogte berekenen voor locatie '{0}'' beëindigd om:", locationName), msgs[1]); StringAssert.StartsWith(string.Format("Berekening van 'Golfhoogte berekenen voor locatie '{0}'' gestart om:", locationName), msgs[2]); StringAssert.StartsWith(string.Format("Golfhoogte berekening voor locatie {0} is niet geconvergeerd.", locationName), msgs[3]); StringAssert.StartsWith("Golfhoogte berekening is uitgevoerd op de tijdelijke locatie:", msgs[4]); StringAssert.StartsWith(string.Format("Berekening van 'Golfhoogte berekenen voor locatie '{0}'' beëindigd om:", locationName), msgs[5]); StringAssert.StartsWith(string.Format("Uitvoeren van 'Golfhoogte berekenen voor locatie '{0}'' is gelukt.", locationName), msgs[6]); }); Assert.AreEqual(CalculationConvergence.CalculatedNotConverged, location.WaveHeightCalculationConvergence); } } } mockRepository.VerifyAll(); } private static TreeNodeInfo GetInfo(RingtoetsPlugin plugin) { return plugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(WaveHeightLocationsContext)); } } }