// 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.Linq;
using Core.Common.Controls.TreeView;
using Core.Common.Gui;
using Core.Common.Gui.ContextMenu;
using Core.Common.Gui.TestUtil.ContextMenu;
using Core.Common.TestUtil;
using NUnit.Framework;
using Rhino.Mocks;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.HydraRing.Data;
using Ringtoets.Integration.Data;
using Ringtoets.Integration.Forms.PresentationObjects;
using Ringtoets.Integration.Plugin;
using RingtoetsFormsResources = Ringtoets.Integration.Forms.Properties.Resources;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
namespace Ringtoets.Integration.Forms.Test.TreeNodeInfos
{
[TestFixture]
public class HydraulicBoundaryDatabaseContextTreeNodeInfoTest
{
private MockRepository mocks;
private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Integration.Forms, "HydraulicBoundaryDatabase");
[SetUp]
public void SetUp()
{
mocks = new MockRepository();
}
[Test]
public void Initialized_Always_ExpectedPropertiesSet()
{
// Setup
mocks.ReplayAll();
using (var plugin = new RingtoetsGuiPlugin())
{
var info = GetInfo(plugin);
// Assert
Assert.AreEqual(typeof(HydraulicBoundaryDatabaseContext), 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);
}
mocks.VerifyAll();
}
[Test]
public void Text_Always_ReturnsSetName()
{
// Setup
var name = "Hydraulische randvoorwaarden";
var assessmentSectionMock = mocks.StrictMock();
mocks.ReplayAll();
var context = new HydraulicBoundaryDatabaseContext(assessmentSectionMock);
using (var plugin = new RingtoetsGuiPlugin())
{
var info = GetInfo(plugin);
// Call
var text = info.Text(context);
// Assert
Assert.AreEqual(name, text);
}
mocks.VerifyAll();
}
[Test]
public void Image_Always_ReturnsGenericIcon()
{
// Setup
mocks.ReplayAll();
using (var plugin = new RingtoetsGuiPlugin())
{
var info = GetInfo(plugin);
// Call
var image = info.Image(null);
// Assert
TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.GenericInputOutputIcon, image);
}
mocks.VerifyAll();
}
[Test]
public void CanRenameNode_Always_ReturnsFalse()
{
// Setup
mocks.ReplayAll();
using (var plugin = new RingtoetsGuiPlugin())
{
var info = GetInfo(plugin);
// Call
var renameAllowed = info.CanRename(null, null);
// Assert
Assert.IsFalse(renameAllowed);
}
mocks.VerifyAll();
}
[Test]
public void ContextMenuStrip_Always_CallsContextMenuBuilderMethods()
{
// Setup
var guiMock = mocks.StrictMock();
var menuBuilderMock = mocks.StrictMock();
var assessmentSectionMock = mocks.Stub();
var nodeData = new HydraulicBoundaryDatabaseContext(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.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.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);
mocks.ReplayAll();
using (var plugin = new RingtoetsGuiPlugin())
{
var info = GetInfo(plugin);
plugin.Gui = guiMock;
// Call
info.ContextMenuStrip(nodeData, null, treeViewControl);
}
}
// Assert
mocks.VerifyAll();
}
[Test]
public void ContextMenuStrip_NoHydraulicBoundaryDatabaseSet_ContextMenuItemCalculateToetspeilenDisabled()
{
// Setup
var guiMock = mocks.StrictMock();
var assessmentSectionMock = mocks.Stub();
var nodeData = new HydraulicBoundaryDatabaseContext(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());
mocks.ReplayAll();
using (var plugin = new RingtoetsGuiPlugin())
{
var info = GetInfo(plugin);
plugin.Gui = guiMock;
// Call
var contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl);
const string expectedItemText = "&Toetspeilen berekenen";
const string expectedItemTooltip = "Er is geen hydraulische randvoorwaardendatabase beschikbaar om de toetspeilen te berekenen.";
TestHelper.AssertContextMenuStripContainsItem(contextMenu, 3, expectedItemText, expectedItemTooltip, RingtoetsCommonFormsResources.FailureMechanismIcon, false);
}
}
// Assert
mocks.VerifyAll(); // Expect no calls on arguments
}
[Test]
public void ContextMenuStrip_HydraulicBoundaryDatabaseSet_ContextMenuItemCalculateToetspeilenEnabled()
{
// Setup
var guiMock = mocks.StrictMock();
var assessmentSectionMock = mocks.Stub();
var nodeData = new HydraulicBoundaryDatabaseContext(assessmentSectionMock);
nodeData.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());
mocks.ReplayAll();
using (var plugin = new RingtoetsGuiPlugin())
{
var info = GetInfo(plugin);
plugin.Gui = guiMock;
// Call
nodeData.WrappedData.HydraulicBoundaryDatabase.FilePath = testDataPath;
var contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl);
const string expectedItemText = "&Toetspeilen berekenen";
const string expectedItemTooltip = "Bereken de toetspeilen";
TestHelper.AssertContextMenuStripContainsItem(contextMenu, 3, expectedItemText, expectedItemTooltip, RingtoetsCommonFormsResources.FailureMechanismIcon);
}
}
// Assert
mocks.VerifyAll(); // Expect no calls on arguments
}
[Test]
public void GivenHydraulicBoundaryDatabaseWithNonExistingFilePath_WhenCalculatingAssessmentLevelFromContextMenu_ThenLogMessagesAddedPreviousOutputNotAffected()
{
// Given
var gui = mocks.DynamicMock();
var contextMenuRunAssessmentLevelCalculationsIndex = 3;
var hydraulicBoundaryLocation1 = new HydraulicBoundaryLocation(100001, "", 1.1, 2.2);
var hydraulicBoundaryLocation2 = new HydraulicBoundaryLocation(100002, "", 3.3, 4.4)
{
DesignWaterLevel = 4.2
};
var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase
{
Locations =
{
hydraulicBoundaryLocation1,
hydraulicBoundaryLocation2
},
FilePath = "D:/nonExistingDirectory/nonExistingFile",
Version = "random"
};
var assessmentSectionMock = new AssessmentSection(AssessmentSectionComposition.Dike)
{
HydraulicBoundaryDatabase = hydraulicBoundaryDatabase
};
var hydraulicBoundaryDatabaseContext = new HydraulicBoundaryDatabaseContext(assessmentSectionMock);
using (var treeViewControl = new TreeViewControl())
{
gui.Expect(cmp => cmp.Get(hydraulicBoundaryDatabaseContext, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
mocks.ReplayAll();
using (var plugin = new RingtoetsGuiPlugin())
{
var info = GetInfo(plugin);
plugin.Gui = gui;
var contextMenuAdapter = info.ContextMenuStrip(hydraulicBoundaryDatabaseContext, null, treeViewControl);
// When
Action action = () => { contextMenuAdapter.Items[contextMenuRunAssessmentLevelCalculationsIndex].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.DesignWaterLevel); // No result set
Assert.AreEqual(4.2, hydraulicBoundaryLocation2.DesignWaterLevel); // Previous result not cleared
}
}
mocks.VerifyAll();
}
[Test]
public void ForeColor_ContextHasNoReferenceLine_ReturnDisabledColor()
{
// Setup
var assessmentSection = mocks.Stub();
mocks.ReplayAll();
var hydraulicBoundaryDatabaseContext = new HydraulicBoundaryDatabaseContext(assessmentSection);
using (var plugin = new RingtoetsGuiPlugin())
{
var info = GetInfo(plugin);
// Call
Color color = info.ForeColor(hydraulicBoundaryDatabaseContext);
// Assert
Assert.AreEqual(Color.FromKnownColor(KnownColor.GrayText), color);
}
mocks.VerifyAll();
}
[Test]
public void ForeColor_ContextHasReferenceLineData_ReturnControlText()
{
// Setup
var assessmentSection = mocks.Stub();
assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase();
mocks.ReplayAll();
var hydraulicBoundaryDatabaseContext = new HydraulicBoundaryDatabaseContext(assessmentSection);
using (var plugin = new RingtoetsGuiPlugin())
{
var info = GetInfo(plugin);
// Call
Color color = info.ForeColor(hydraulicBoundaryDatabaseContext);
// Assert
Assert.AreEqual(Color.FromKnownColor(KnownColor.ControlText), color);
}
mocks.VerifyAll();
}
private TreeNodeInfo GetInfo(RingtoetsGuiPlugin guiPlugin)
{
return guiPlugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(HydraulicBoundaryDatabaseContext));
}
}
}