// 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.Util.Extensions;
using Core.Common.Util.Reflection;
using NUnit.Extensions.Forms;
using NUnit.Framework;
using Ringtoets.AssemblyTool.KernelWrapper.Calculators;
using Ringtoets.AssemblyTool.KernelWrapper.TestUtil.Calculators;
using Ringtoets.AssemblyTool.KernelWrapper.TestUtil.Calculators.Assembly;
using Ringtoets.Common.Data.AssemblyTool;
using Ringtoets.Common.Data.TestUtil;
using Ringtoets.Common.Forms.Controls;
using Ringtoets.Common.Forms.TestUtil;
using Ringtoets.Common.Forms.Views;
using Ringtoets.Common.Primitives;
using Ringtoets.Integration.Data.StandAlone;
using Ringtoets.Integration.Data.StandAlone.SectionResults;
using Ringtoets.Integration.Forms.Views.SectionResultRows;
using Ringtoets.Integration.Forms.Views.SectionResultViews;
namespace Ringtoets.Integration.Forms.Test.Views.SectionResultViews
{
[TestFixture]
public class PipingStructureResultViewTest
{
private const int nameColumnIndex = 0;
private const int simpleAssessmentResultIndex = 1;
private const int detailedAssessmentResultIndex = 2;
private const int tailorMadeAssessmentResultIndex = 3;
private const int simpleAssemblyCategoryGroupIndex = 4;
private const int detailedAssemblyCategoryGroupIndex = 5;
private const int tailorMadeAssemblyCategoryGroupIndex = 6;
private const int combinedAssemblyCategoryGroupIndex = 7;
private const int useManualAssemblyCategoryGroupIndex = 8;
private const int manualAssemblyCategoryGroupIndex = 9;
private const int columnCount = 10;
private Form testForm;
[SetUp]
public void Setup()
{
testForm = new Form();
}
[TearDown]
public void TearDown()
{
testForm.Dispose();
}
[Test]
public void Constructor_ExpectedValues()
{
// Setup
var failureMechanism = new PipingStructureFailureMechanism();
// Call
using (var view = new PipingStructureResultView(failureMechanism.SectionResults, failureMechanism))
{
// Assert
Assert.IsInstanceOf>(view);
Assert.IsNull(view.Data);
Assert.AreSame(failureMechanism, view.FailureMechanism);
}
}
[Test]
public void GivenFormWithFailureMechanismResultView_ThenExpectedColumnsAreAdded()
{
// Given
using (ShowFailureMechanismResultsView(new PipingStructureFailureMechanism()))
{
// Then
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
Assert.AreEqual(columnCount, dataGridView.ColumnCount);
Assert.IsInstanceOf(dataGridView.Columns[nameColumnIndex]);
Assert.IsInstanceOf(dataGridView.Columns[simpleAssessmentResultIndex]);
Assert.IsInstanceOf(dataGridView.Columns[detailedAssessmentResultIndex]);
Assert.IsInstanceOf(dataGridView.Columns[tailorMadeAssessmentResultIndex]);
Assert.IsInstanceOf(dataGridView.Columns[simpleAssemblyCategoryGroupIndex]);
Assert.IsInstanceOf(dataGridView.Columns[detailedAssemblyCategoryGroupIndex]);
Assert.IsInstanceOf(dataGridView.Columns[tailorMadeAssemblyCategoryGroupIndex]);
Assert.IsInstanceOf(dataGridView.Columns[combinedAssemblyCategoryGroupIndex]);
Assert.IsInstanceOf(dataGridView.Columns[useManualAssemblyCategoryGroupIndex]);
Assert.IsInstanceOf(dataGridView.Columns[manualAssemblyCategoryGroupIndex]);
Assert.AreEqual("Vak", dataGridView.Columns[nameColumnIndex].HeaderText);
Assert.AreEqual("Eenvoudige toets", dataGridView.Columns[simpleAssessmentResultIndex].HeaderText);
Assert.AreEqual("Gedetailleerde toets per vak", dataGridView.Columns[detailedAssessmentResultIndex].HeaderText);
Assert.AreEqual("Toets op maat", dataGridView.Columns[tailorMadeAssessmentResultIndex].HeaderText);
Assert.AreEqual("Toetsoordeel\r\neenvoudige toets", dataGridView.Columns[simpleAssemblyCategoryGroupIndex].HeaderText);
Assert.AreEqual("Toetsoordeel\r\ngedetailleerde toets per vak", dataGridView.Columns[detailedAssemblyCategoryGroupIndex].HeaderText);
Assert.AreEqual("Toetsoordeel\r\ntoets op maat", dataGridView.Columns[tailorMadeAssemblyCategoryGroupIndex].HeaderText);
Assert.AreEqual("Toetsoordeel\r\ngecombineerd", dataGridView.Columns[combinedAssemblyCategoryGroupIndex].HeaderText);
Assert.AreEqual("Overschrijf\r\ntoetsoordeel", dataGridView.Columns[useManualAssemblyCategoryGroupIndex].HeaderText);
Assert.AreEqual("Toetsoordeel\r\nhandmatig", dataGridView.Columns[manualAssemblyCategoryGroupIndex].HeaderText);
Assert.IsTrue(dataGridView.Columns[nameColumnIndex].ReadOnly);
Assert.IsFalse(dataGridView.Columns[simpleAssessmentResultIndex].ReadOnly);
Assert.IsFalse(dataGridView.Columns[detailedAssessmentResultIndex].ReadOnly);
Assert.IsFalse(dataGridView.Columns[tailorMadeAssessmentResultIndex].ReadOnly);
Assert.IsTrue(dataGridView.Columns[simpleAssemblyCategoryGroupIndex].ReadOnly);
Assert.IsTrue(dataGridView.Columns[detailedAssemblyCategoryGroupIndex].ReadOnly);
Assert.IsTrue(dataGridView.Columns[tailorMadeAssemblyCategoryGroupIndex].ReadOnly);
Assert.IsTrue(dataGridView.Columns[combinedAssemblyCategoryGroupIndex].ReadOnly);
Assert.IsFalse(dataGridView.Columns[useManualAssemblyCategoryGroupIndex].ReadOnly);
Assert.IsFalse(dataGridView.Columns[manualAssemblyCategoryGroupIndex].ReadOnly);
Assert.AreEqual(DataGridViewAutoSizeColumnsMode.AllCells, dataGridView.AutoSizeColumnsMode);
Assert.AreEqual(DataGridViewContentAlignment.MiddleCenter, dataGridView.ColumnHeadersDefaultCellStyle.Alignment);
}
}
[Test]
public void FailureMechanismResultsView_AllDataSet_DataGridViewCorrectlyInitialized()
{
// Setup
var failureMechanism = new PipingStructureFailureMechanism();
FailureMechanismTestHelper.SetSections(failureMechanism, new[]
{
FailureMechanismSectionTestFactory.CreateFailureMechanismSection("Section 1")
});
// Call
using (new AssemblyToolCalculatorFactoryConfig())
using (ShowFailureMechanismResultsView(failureMechanism))
{
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
// Assert
Assert.AreEqual(columnCount, dataGridView.ColumnCount);
DataGridViewRowCollection rows = dataGridView.Rows;
Assert.AreEqual(1, rows.Count);
DataGridViewCellCollection cells = rows[0].Cells;
Assert.AreEqual("Section 1", cells[nameColumnIndex].FormattedValue);
Assert.AreEqual(SimpleAssessmentResultType.None, cells[simpleAssessmentResultIndex].Value);
Assert.AreEqual(DetailedAssessmentResultType.None, cells[detailedAssessmentResultIndex].Value);
Assert.AreEqual(TailorMadeAssessmentResultType.None, cells[tailorMadeAssessmentResultIndex].Value);
Assert.AreEqual("Iv", cells[simpleAssemblyCategoryGroupIndex].Value);
Assert.AreEqual("IIv", cells[detailedAssemblyCategoryGroupIndex].Value);
Assert.AreEqual("IIv", cells[tailorMadeAssemblyCategoryGroupIndex].Value);
Assert.AreEqual("IIv", cells[combinedAssemblyCategoryGroupIndex].Value);
Assert.AreEqual(false, cells[useManualAssemblyCategoryGroupIndex].Value);
Assert.AreEqual(ManualFailureMechanismSectionAssemblyCategoryGroup.None, cells[manualAssemblyCategoryGroupIndex].Value);
}
}
[Test]
public void FailureMechanismResultView_WithFailureMechanismWithManualSectionAssemblyResults_ThenWarningSet()
{
// Setup
var failureMechanism = new PipingStructureFailureMechanism();
FailureMechanismTestHelper.AddSections(failureMechanism, 2);
failureMechanism.SectionResults.First().UseManualAssemblyCategoryGroup = true;
using (var form = new Form())
using (var view = new PipingStructureResultView(failureMechanism.SectionResults,
failureMechanism))
{
form.Controls.Add(view);
form.Show();
FailureMechanismAssemblyCategoryGroupControl failureMechanismAssemblyControl = GetFailureMechanismAssemblyControl();
ErrorProvider manualAssemblyWarningProvider = GetManualAssemblyWarningProvider(failureMechanismAssemblyControl);
// Call
string warningMessage = manualAssemblyWarningProvider.GetError(failureMechanismAssemblyControl);
// Assert
Assert.AreEqual("Toetsoordeel is (deels) gebaseerd op handmatig overschreven toetsoordelen.", warningMessage);
}
}
[Test]
public void GivenFailureMechanismResultsViewWithWarnings_WhenFailureMechanismWithoutManualSectionAssemblyResultsAndFailureMechanismNotifiesObservers_ThenWarningCleared()
{
// Given
var failureMechanism = new PipingStructureFailureMechanism();
FailureMechanismTestHelper.AddSections(failureMechanism, 2);
failureMechanism.SectionResults.First().UseManualAssemblyCategoryGroup = true;
using (var form = new Form())
using (var view = new PipingStructureResultView(failureMechanism.SectionResults,
failureMechanism))
{
form.Controls.Add(view);
form.Show();
FailureMechanismAssemblyCategoryGroupControl failureMechanismAssemblyControl = GetFailureMechanismAssemblyControl();
ErrorProvider manualAssemblyWarningProvider = GetManualAssemblyWarningProvider(failureMechanismAssemblyControl);
// Precondition
Assert.AreEqual("Toetsoordeel is (deels) gebaseerd op handmatig overschreven toetsoordelen.", manualAssemblyWarningProvider.GetError(failureMechanismAssemblyControl));
// When
failureMechanism.SectionResults.ForEachElementDo(sr => sr.UseManualAssemblyCategoryGroup = false);
failureMechanism.NotifyObservers();
// Then
Assert.IsEmpty(manualAssemblyWarningProvider.GetError(failureMechanismAssemblyControl));
}
}
[Test]
public void GivenFailureMechanismResultsViewWithManualAssembly_WhenShown_ThenManualAssemblyUsed()
{
// Given
var failureMechanism = new PipingStructureFailureMechanism();
FailureMechanismTestHelper.SetSections(failureMechanism, new[]
{
FailureMechanismSectionTestFactory.CreateFailureMechanismSection()
});
PipingStructureFailureMechanismSectionResult sectionResult = failureMechanism.SectionResults.Single();
sectionResult.ManualAssemblyCategoryGroup = ManualFailureMechanismSectionAssemblyCategoryGroup.Iv;
sectionResult.UseManualAssemblyCategoryGroup = true;
// When
using (new AssemblyToolCalculatorFactoryConfig())
using (ShowFailureMechanismResultsView(failureMechanism))
{
// Then
var calculatorFactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance;
FailureMechanismAssemblyCalculatorStub calculator = calculatorFactory.LastCreatedFailureMechanismAssemblyCalculator;
Assert.AreEqual(ManualFailureMechanismSectionAssemblyCategoryGroupConverter.Convert(sectionResult.ManualAssemblyCategoryGroup),
calculator.FailureMechanismSectionCategories.Single());
}
}
[TestFixture]
public class PipingStructureFailureMechanismResultControlTest : FailureMechanismAssemblyCategoryGroupControlTestFixture<
PipingStructureResultView,
PipingStructureFailureMechanism,
PipingStructureFailureMechanismSectionResult,
PipingStructureSectionResultRow>
{
protected override PipingStructureResultView CreateResultView(PipingStructureFailureMechanism failureMechanism)
{
return new PipingStructureResultView(failureMechanism.SectionResults,
failureMechanism);
}
}
private static FailureMechanismAssemblyCategoryGroupControl GetFailureMechanismAssemblyControl()
{
var control = (FailureMechanismAssemblyCategoryGroupControl) ((TableLayoutPanel) new ControlTester("TableLayoutPanel").TheObject).GetControlFromPosition(1, 0);
return control;
}
private PipingStructureResultView ShowFailureMechanismResultsView(
PipingStructureFailureMechanism failureMechanism)
{
var failureMechanismResultView = new PipingStructureResultView(failureMechanism.SectionResults,
failureMechanism);
testForm.Controls.Add(failureMechanismResultView);
testForm.Show();
return failureMechanismResultView;
}
private static ErrorProvider GetManualAssemblyWarningProvider(FailureMechanismAssemblyCategoryGroupControl control)
{
return TypeUtils.GetField(control, "manualAssemblyWarningProvider");
}
}
}