// 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.Linq; using System.Windows.Forms; using Core.Common.Controls; using Core.Common.Util.Reflection; using NUnit.Extensions.Forms; using NUnit.Framework; using Ringtoets.AssemblyTool.Data; using Ringtoets.AssemblyTool.KernelWrapper.Calculators; using Ringtoets.AssemblyTool.KernelWrapper.TestUtil.Calculators; using Ringtoets.AssemblyTool.KernelWrapper.TestUtil.Calculators.Assembly; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.TestUtil; using Ringtoets.Common.Forms.Controls; using Ringtoets.Common.Forms.Views; namespace Ringtoets.Common.Forms.TestUtil { /// /// Test fixture class for testing data and styling in a view with a . /// /// The type of the view to test. /// The type of the failure mechanism the view belongs to. /// The type of the section results shown in the view. /// The type of the presentation objects used in the view. [TestFixture] public abstract class FailureMechanismAssemblyCategoryGroupControlTestFixture where TView : FailureMechanismResultView where TFailureMechanism : IFailureMechanism, IHasSectionResults, new() where TSectionResult : FailureMechanismSectionResult where TResultRow : FailureMechanismSectionResultRow { private Form testForm; [SetUp] public void Setup() { testForm = new Form(); } [TearDown] public void TearDown() { testForm.Dispose(); } [Test] public void FailureMechanismResultsView_Always_FailureMechanismResultControlCorrectlyInitialized() { // Setup var failureMechanism = new TFailureMechanism(); failureMechanism.AddSection(FailureMechanismSectionTestFactory.CreateFailureMechanismSection()); // Call using (new AssemblyToolCalculatorFactoryConfig()) using (ShowFailureMechanismResultsView(failureMechanism)) { // Assert var assemblyResultPanel = (TableLayoutPanel) new ControlTester("TableLayoutPanel").TheObject; var assemblyResultControl = (FailureMechanismAssemblyCategoryGroupControl) assemblyResultPanel.GetControlFromPosition(1, 0); Assert.IsInstanceOf(assemblyResultControl); Assert.AreEqual(DockStyle.Left, assemblyResultControl.Dock); } } [Test] public void GivenFailureMechanismResultsView_WhenCalculatorThrowsException_ThenErrorSetToControl() { // Given var failureMechanism = new TFailureMechanism(); using (new AssemblyToolCalculatorFactoryConfig()) using (ShowFailureMechanismResultsView(failureMechanism)) { // Precondition FailureMechanismAssemblyCategoryGroupControl assemblyControl = GetFailureMechanismAssemblyCategoryGroupControl(); ErrorProvider errorProvider = GetErrorProvider(assemblyControl); Assert.IsEmpty(errorProvider.GetError(assemblyControl)); // When var calculatorfactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance; FailureMechanismAssemblyCalculatorStub calculator = calculatorfactory.LastCreatedFailureMechanismAssemblyCalculator; calculator.ThrowExceptionOnCalculate = true; failureMechanism.NotifyObservers(); // Assert Assert.AreEqual("Message", errorProvider.GetError(assemblyControl)); } } [Test] public void GivenFailureMechanismResultsViewWithAssemblyResult_WhenCalculatorThrowsException_ThenFailureMechanismAssemblyResultCleared() { // Given var failureMechanism = new TFailureMechanism(); using (new AssemblyToolCalculatorFactoryConfig()) using (ShowFailureMechanismResultsView(failureMechanism)) { // Precondition var assemblyGroupLabel = (BorderedLabel) new ControlTester("GroupLabel").TheObject; Assert.AreEqual("IIt", assemblyGroupLabel.Text); // When var calculatorfactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance; FailureMechanismAssemblyCalculatorStub calculator = calculatorfactory.LastCreatedFailureMechanismAssemblyCalculator; calculator.ThrowExceptionOnCalculate = true; failureMechanism.NotifyObservers(); // Assert Assert.IsEmpty(assemblyGroupLabel.Text); } } [Test] public void GivenFailureMechanismResultsViewWithError_WhenNoExceptionThrownByCalculator_ThenErrorCleared() { // Given var failureMechanism = new TFailureMechanism(); using (new AssemblyToolCalculatorFactoryConfig()) { var calculatorfactory = (TestAssemblyToolCalculatorFactory)AssemblyToolCalculatorFactory.Instance; FailureMechanismAssemblyCalculatorStub calculator = calculatorfactory.LastCreatedFailureMechanismAssemblyCalculator; calculator.ThrowExceptionOnCalculate = true; using (ShowFailureMechanismResultsView(failureMechanism)) { // Precondition FailureMechanismAssemblyCategoryGroupControl assemblyControl = GetFailureMechanismAssemblyCategoryGroupControl(); ErrorProvider errorProvider = GetErrorProvider(assemblyControl); Assert.AreEqual("Message", errorProvider.GetError(assemblyControl)); // When calculator.ThrowExceptionOnCalculate = false; failureMechanism.NotifyObservers(); // Then Assert.IsEmpty(errorProvider.GetError(assemblyControl)); } } } [Test] public void GivenFailureMechanismResultsViewWithAssemblyResult_WhenFailureMechanismAssemblyResultChangedAndSectionResultNotified_ThenFailureMechanismAssemblyResultUpdated() { // Given var failureMechanism = new TFailureMechanism(); failureMechanism.AddSection(FailureMechanismSectionTestFactory.CreateFailureMechanismSection()); using (new AssemblyToolCalculatorFactoryConfig()) using (ShowFailureMechanismResultsView(failureMechanism)) { // Precondition var assemblyGroupLabel = (BorderedLabel) new ControlTester("GroupLabel").TheObject; Assert.AreEqual("IIt", assemblyGroupLabel.Text); // When var calculatorfactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance; FailureMechanismAssemblyCalculatorStub calculator = calculatorfactory.LastCreatedFailureMechanismAssemblyCalculator; calculator.FailureMechanismAssemblyCategoryGroupOutput = FailureMechanismAssemblyCategoryGroup.VIt; failureMechanism.SectionResults.Single().NotifyObservers(); // Then Assert.AreEqual("VIt", assemblyGroupLabel.Text); } } /// /// Method for creating an instance of . /// /// The failure mechanism to create the view for. /// A new . protected abstract TView CreateResultView(TFailureMechanism failureMechanism); private TView ShowFailureMechanismResultsView(TFailureMechanism failureMechanism) { TView failureMechanismResultView = CreateResultView(failureMechanism); testForm.Controls.Add(failureMechanismResultView); testForm.Show(); return failureMechanismResultView; } private static ErrorProvider GetErrorProvider(FailureMechanismAssemblyCategoryGroupControl control) { return TypeUtils.GetField(control, "errorProvider"); } private static FailureMechanismAssemblyCategoryGroupControl GetFailureMechanismAssemblyCategoryGroupControl() { return (FailureMechanismAssemblyCategoryGroupControl) ((TableLayoutPanel) new ControlTester("TableLayoutPanel").TheObject).GetControlFromPosition(1, 0); } } }