// Copyright (C) Stichting Deltares 2018. 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.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.TestUtil.ContextMenu;
using Core.Common.TestUtil;
using NUnit.Framework;
using Rhino.Mocks;
using Ringtoets.Common.Data;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Forms.PresentationObjects;
using Ringtoets.Integration.Data.StandAlone;
using Ringtoets.Integration.Data.StandAlone.SectionResults;
using Ringtoets.Integration.Forms.PresentationObjects.StandAlone;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
namespace Ringtoets.Integration.Plugin.Test.TreeNodeInfos
{
[TestFixture]
public class GrassCoverSlipOffInwardsFailureMechanismContextTreeNodeInfoTest
{
private const int contextMenuRelevancyIndexWhenNotRelevant = 0;
private const int contextMenuRelevancyIndexWhenRelevant = 2;
private MockRepository mocks;
private TreeNodeInfo info;
private RingtoetsPlugin plugin;
[SetUp]
public void SetUp()
{
mocks = new MockRepository();
plugin = new RingtoetsPlugin();
info = plugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(GrassCoverSlipOffInwardsFailureMechanismContext));
}
[TearDown]
public void TearDown()
{
plugin.Dispose();
mocks.VerifyAll();
}
[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.CheckedState);
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 failureMechanism = new GrassCoverSlipOffInwardsFailureMechanism();
var failureMechanismContext = new GrassCoverSlipOffInwardsFailureMechanismContext(failureMechanism, assessmentSection);
// Call
string text = info.Text(failureMechanismContext);
// Assert
Assert.AreEqual(failureMechanism.Name, text);
}
[Test]
public void Image_Always_ReturnsSetImage()
{
// Setup
mocks.ReplayAll();
// Call
Image image = info.Image(null);
// Assert
TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.FailureMechanismIcon, image);
}
[Test]
public void ForeColor_Always_ReturnsControlText()
{
// Setup
var assessmentSection = mocks.Stub();
mocks.ReplayAll();
var failureMechanism = new GrassCoverSlipOffInwardsFailureMechanism();
var context = new GrassCoverSlipOffInwardsFailureMechanismContext(failureMechanism, assessmentSection);
// Call
Color textColor = info.ForeColor(context);
// Assert
Assert.AreEqual(Color.FromKnownColor(KnownColor.ControlText), textColor);
}
[Test]
public void ChildNodeObjects_FailureMechanismIsRelevant_ReturnChildDataNodes()
{
// Setup
var assessmentSection = mocks.Stub();
mocks.ReplayAll();
var failureMechanism = new GrassCoverSlipOffInwardsFailureMechanism();
var failureMechanismContext = new GrassCoverSlipOffInwardsFailureMechanismContext(failureMechanism, assessmentSection);
// Call
object[] children = info.ChildNodeObjects(failureMechanismContext).ToArray();
// Assert
Assert.AreEqual(2, children.Length);
var inputFolder = (CategoryTreeFolder) children[0];
Assert.AreEqual(2, inputFolder.Contents.Count());
Assert.AreEqual("Invoer", inputFolder.Name);
Assert.AreEqual(TreeFolderCategory.Input, inputFolder.Category);
var failureMechanismSectionsContext = (GrassCoverSlipOffInwardsFailureMechanismSectionsContext) inputFolder.Contents.ElementAt(0);
Assert.AreSame(failureMechanism, failureMechanismSectionsContext.WrappedData);
Assert.AreSame(assessmentSection, failureMechanismSectionsContext.AssessmentSection);
var inputComment = (Comment) inputFolder.Contents.ElementAt(1);
Assert.AreSame(failureMechanism.InputComments, inputComment);
var outputFolder = (CategoryTreeFolder) children[1];
Assert.AreEqual(2, outputFolder.Contents.Count());
Assert.AreEqual("Oordeel", outputFolder.Name);
Assert.AreEqual(TreeFolderCategory.Output, outputFolder.Category);
var failureMechanismResultsContext = (FailureMechanismSectionResultContext)
outputFolder.Contents.ElementAt(0);
Assert.AreSame(failureMechanism, failureMechanismResultsContext.FailureMechanism);
Assert.AreSame(failureMechanism.SectionResults, failureMechanismResultsContext.WrappedData);
var outputComment = (Comment) outputFolder.Contents.ElementAt(1);
Assert.AreSame(failureMechanism.OutputComments, outputComment);
}
[Test]
public void ChildNodeObjects_FailureMechanismIsNotRelevant_ReturnOnlyFailureMechanismNotRelevantComments()
{
// Setup
var assessmentSection = mocks.Stub();
mocks.ReplayAll();
var failureMechanism = new GrassCoverSlipOffInwardsFailureMechanism
{
IsRelevant = false
};
var failureMechanismContext = new GrassCoverSlipOffInwardsFailureMechanismContext(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]
public void ContextMenuStrip_FailureMechanismIsRelevant_CallsContextMenuBuilderMethods()
{
// Setup
using (var treeView = new TreeViewControl())
{
var failureMechanism = new GrassCoverSlipOffInwardsFailureMechanism();
var assessmentSection = mocks.Stub();
var context = new GrassCoverSlipOffInwardsFailureMechanismContext(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.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(context, treeView)).Return(menuBuilder);
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
mocks.ReplayAll();
plugin.Gui = gui;
// Call
info.ContextMenuStrip(context, assessmentSection, treeView);
// Assert
}
}
[Test]
public void ContextMenuStrip_FailureMechanismIsNotRelevant_CallsContextMenuBuilderMethods()
{
// Setup
var failureMechanism = new GrassCoverSlipOffInwardsFailureMechanism
{
IsRelevant = false
};
var assessmentSection = mocks.Stub();
var context = new GrassCoverSlipOffInwardsFailureMechanismContext(failureMechanism, assessmentSection);
using (var treeView = new TreeViewControl())
{
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.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(context, treeView)).Return(menuBuilder);
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
mocks.ReplayAll();
plugin.Gui = gui;
// Call
info.ContextMenuStrip(context, assessmentSection, treeView);
// Assert
}
}
[Test]
public void ContextMenuStrip_FailureMechanismIsRelevant_IsRelevantEnabled()
{
// Setup
using (var treeView = new TreeViewControl())
{
var assessmentSection = mocks.Stub();
var failureMechanism = new GrassCoverSlipOffInwardsFailureMechanism();
var context = new GrassCoverSlipOffInwardsFailureMechanismContext(failureMechanism, assessmentSection);
var menuBuilder = new CustomItemsOnlyContextMenuBuilder();
var gui = mocks.Stub();
gui.Stub(cmp => cmp.Get(context, treeView)).Return(menuBuilder);
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
mocks.ReplayAll();
plugin.Gui = gui;
// Call
using (ContextMenuStrip menu = info.ContextMenuStrip(context, assessmentSection, treeView))
{
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuRelevancyIndexWhenRelevant,
"I&s relevant",
"Geeft aan of dit toetsspoor relevant is of niet.",
RingtoetsCommonFormsResources.Checkbox_ticked);
}
// Assert
}
}
[Test]
public void ContextMenuStrip_FailureMechanismIsRelevantAndClickOnIsRelevantItem_MakeFailureMechanismNotRelevant()
{
// Setup
var failureMechanismObserver = mocks.Stub();
failureMechanismObserver.Expect(o => o.UpdateObserver());
var failureMechanism = new GrassCoverSlipOffInwardsFailureMechanism
{
IsRelevant = true
};
failureMechanism.Attach(failureMechanismObserver);
var assessmentSection = mocks.Stub();
var failureMechanismContext = new GrassCoverSlipOffInwardsFailureMechanismContext(failureMechanism, assessmentSection);
var viewCommands = mocks.StrictMock();
viewCommands.Expect(vs => vs.RemoveAllViewsForItem(failureMechanismContext));
using (var treeViewControl = new TreeViewControl())
{
var menuBuilder = new CustomItemsOnlyContextMenuBuilder();
var gui = mocks.Stub();
gui.Stub(g => g.ViewCommands).Return(viewCommands);
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
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
var failureMechanismObserver = mocks.Stub();
failureMechanismObserver.Expect(o => o.UpdateObserver());
var failureMechanism = new GrassCoverSlipOffInwardsFailureMechanism
{
IsRelevant = false
};
failureMechanism.Attach(failureMechanismObserver);
var assessmentSection = mocks.Stub();
var failureMechanismContext = new GrassCoverSlipOffInwardsFailureMechanismContext(failureMechanism, assessmentSection);
var viewCommands = mocks.StrictMock();
viewCommands.Expect(vs => vs.RemoveAllViewsForItem(failureMechanismContext));
var menuBuilder = new CustomItemsOnlyContextMenuBuilder();
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(g => g.ViewCommands).Return(viewCommands);
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
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);
}
}
}
}
}