// 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.Linq; using Core.Common.Controls.Views; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.TestUtil; using Ringtoets.Common.Forms.PresentationObjects; namespace Ringtoets.Common.Plugin.TestUtil { /// /// Class for testing for views related to a failure mechanism. /// [TestFixture] public abstract class ShouldCloseViewWithFailureMechanismTester { [Test] public void ShouldCloseMethod_ViewNotCorrespondingToRemovedAssessmentSection_ReturnsFalse() { // Setup var mocks = new MockRepository(); var assessmentSection = mocks.Stub(); assessmentSection.Stub(asm => asm.GetFailureMechanisms()).Return(Enumerable.Empty()); mocks.ReplayAll(); IFailureMechanism failureMechanism = GetFailureMechanism(); using (IView view = GetView(failureMechanism)) { // Call bool closeForData = ShouldCloseMethod(view, assessmentSection); // Assert Assert.IsFalse(closeForData); } mocks.VerifyAll(); } [Test] public void ShouldCloseMethod_ViewCorrespondingToRemovedAssessmentSection_ReturnsTrue() { // Setup IFailureMechanism failureMechanism = GetFailureMechanism(); var mocks = new MockRepository(); var assessmentSection = mocks.Stub(); assessmentSection.Stub(asm => asm.GetFailureMechanisms()).Return(new[] { failureMechanism }); mocks.ReplayAll(); using (IView view = GetView(failureMechanism)) { // Call bool closeForData = ShouldCloseMethod(view, assessmentSection); // Assert Assert.IsTrue(closeForData); } mocks.VerifyAll(); } [Test] public void ShouldCloseMethod_ViewNotCorrespondingToRemovedFailureMechanism_ReturnsFalse() { // Setup IFailureMechanism failureMechanism = GetFailureMechanism(); using (IView view = GetView(failureMechanism)) { // Call bool closeForData = ShouldCloseMethod(view, new TestFailureMechanism()); // Assert Assert.IsFalse(closeForData); } } [Test] public void ShouldCloseMethod_ViewCorrespondingToRemovedFailureMechanism_ReturnsTrue() { // Setup IFailureMechanism failureMechanism = GetFailureMechanism(); using (IView view = GetView(failureMechanism)) { // Call bool closeForData = ShouldCloseMethod(view, failureMechanism); // Assert Assert.IsTrue(closeForData); } } [Test] public void ShouldCloseMethod_ViewNotCorrespondingToRemovedFailureMechanismContext_ReturnsFalse() { // Setup var mocks = new MockRepository(); var assessmentSection = mocks.Stub(); mocks.ReplayAll(); IFailureMechanism failureMechanism = GetFailureMechanism(); var failureMechanismContext = new TestFailureMechanismContext(new TestFailureMechanism(), assessmentSection); using (IView view = GetView(failureMechanism)) { // Call bool closeForData = ShouldCloseMethod(view, failureMechanismContext); // Assert Assert.IsFalse(closeForData); } mocks.VerifyAll(); } [Test] public void ShouldCloseMethod_ViewCorrespondingToRemovedFailureMechanismContext_ReturnsTrue() { // Setup var mocks = new MockRepository(); var assessmentSection = mocks.Stub(); mocks.ReplayAll(); IFailureMechanism failureMechanism = GetFailureMechanism(); var failureMechanismContext = new TestFailureMechanismContext(failureMechanism, assessmentSection); using (IView view = GetView(failureMechanism)) { // Call bool closeForData = ShouldCloseMethod(view, failureMechanismContext); // Assert Assert.IsTrue(closeForData); } mocks.VerifyAll(); } /// /// Performs the method that must be tested. /// /// The failure mechanism sections view involved. /// The object involved. /// Whether the view should close or not. protected abstract bool ShouldCloseMethod(IView view, object o); /// /// Gets a view for testing purposes. /// /// The failure mechanism containing the data to set to the view. /// A view object. protected abstract IView GetView(IFailureMechanism failureMechanism); /// /// Gets a failure mechanism for testing purposes. /// /// An . protected abstract IFailureMechanism GetFailureMechanism(); private class TestFailureMechanismContext : FailureMechanismContext { public TestFailureMechanismContext(IFailureMechanism wrappedFailureMechanism, IAssessmentSection parent) : base(wrappedFailureMechanism, parent) {} } } }