// 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.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Core.Common.Base.Data;
using Core.Common.Base.Geometry;
using NUnit.Extensions.Forms;
using NUnit.Framework;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Data.Probability;
using Ringtoets.Common.Data.Structures;
using Ringtoets.Common.Data.TestUtil;
using Ringtoets.Common.Forms.Helpers;
using Ringtoets.Common.Forms.Views;
using Ringtoets.StabilityPointStructures.Data;
using Ringtoets.StabilityPointStructures.Forms.Views;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
namespace Ringtoets.StabilityPointStructures.Forms.Test.Views
{
[TestFixture]
public class StabilityPointStructuresFailureMechanismResultViewTest
{
private const int nameColumnIndex = 0;
private const int assessmentLayerOneIndex = 1;
private const int assessmentLayerTwoAIndex = 2;
private const int assessmentLayerThreeIndex = 3;
private Form testForm;
[SetUp]
public void Setup()
{
testForm = new Form();
}
[TearDown]
public void TearDown()
{
testForm.Dispose();
}
[Test]
public void DefaultConstructor_DefaultValues()
{
// Call
using (var view = new StabilityPointStructuresFailureMechanismResultView())
{
// Assert
Assert.IsInstanceOf>(view);
Assert.IsNull(view.Data);
}
}
[Test]
public void Data_DataAlreadySetNewDataSet_DataSetAndDataGridViewUpdated()
{
// Setup
using (StabilityPointStructuresFailureMechanismResultView view = CreateConfiguredFailureMechanismResultsView())
{
FailureMechanismSection section = CreateSimpleFailureMechanismSection();
var sectionResult = new StabilityPointStructuresFailureMechanismSectionResult(section);
var testData = new List
{
sectionResult
};
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
// Precondition
Assert.AreEqual(2, dataGridView.RowCount);
// Call
view.Data = testData;
// Assert
Assert.AreSame(testData, view.Data);
Assert.AreEqual(testData.Count, dataGridView.RowCount);
Assert.AreEqual(sectionResult.Section.Name, dataGridView.Rows[0].Cells[0].Value);
}
}
[Test]
public void Data_SetOtherThanFailureMechanismSectionResultListData_DataNullAndDataGridViewEmpty()
{
// Setup
var testData = new object();
using (StabilityPointStructuresFailureMechanismResultView view = CreateConfiguredFailureMechanismResultsView())
{
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
// Call
view.Data = testData;
// Assert
Assert.IsNull(view.Data);
Assert.AreEqual(0, dataGridView.RowCount);
}
}
[Test]
public void GivenFailureMechanismResultsView_WhenAllDataSet_ThenDataGridViewCorrectlyInitialized()
{
// Given
using (CreateConfiguredFailureMechanismResultsView())
{
// Then
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
DataGridViewRowCollection rows = dataGridView.Rows;
Assert.AreEqual(2, rows.Count);
DataGridViewCellCollection cells = rows[0].Cells;
Assert.AreEqual(4, cells.Count);
Assert.AreEqual("Section 1", cells[nameColumnIndex].FormattedValue);
Assert.IsFalse((bool) cells[assessmentLayerOneIndex].FormattedValue);
Assert.AreEqual("-", cells[assessmentLayerTwoAIndex].FormattedValue);
Assert.AreEqual("-", cells[assessmentLayerThreeIndex].FormattedValue);
cells = rows[1].Cells;
Assert.AreEqual(4, cells.Count);
Assert.AreEqual("Section 2", cells[nameColumnIndex].FormattedValue);
Assert.IsFalse((bool) cells[assessmentLayerOneIndex].FormattedValue);
Assert.AreEqual("-", cells[assessmentLayerTwoAIndex].FormattedValue);
Assert.AreEqual("-", cells[assessmentLayerThreeIndex].FormattedValue);
}
}
[Test]
[SetCulture("nl-NL")]
[TestCase(true)]
[TestCase(false)]
public void FailureMechanismResultsView_ChangeCheckBox_DataGridViewCorrectlySyncedAndStylingSet(bool checkBoxSelected)
{
// Setup
using (CreateConfiguredFailureMechanismResultsView())
{
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
// Call
dataGridView.Rows[0].Cells[assessmentLayerOneIndex].Value = checkBoxSelected;
// Assert
var rows = dataGridView.Rows;
var cells = rows[0].Cells;
Assert.AreEqual(4, cells.Count);
Assert.AreEqual("Section 1", cells[nameColumnIndex].FormattedValue);
var cellAssessmentLayerTwoA = cells[assessmentLayerTwoAIndex];
var cellAssessmentLayerThree = cells[assessmentLayerThreeIndex];
DataGridViewCell dataGridViewCell = cells[assessmentLayerOneIndex];
Assert.AreEqual(checkBoxSelected, (bool) dataGridViewCell.FormattedValue);
Assert.AreEqual("-", cellAssessmentLayerTwoA.FormattedValue);
Assert.AreEqual("-", cellAssessmentLayerThree.FormattedValue);
Assert.IsEmpty(dataGridViewCell.ErrorText);
var cellAssessmentLayerTwoABackColor = cellAssessmentLayerTwoA.Style.BackColor;
var cellAssessmentLayerTwoAForeColor = cellAssessmentLayerTwoA.Style.ForeColor;
var cellAssessmentLayerThreeBackColor = cellAssessmentLayerThree.Style.BackColor;
var cellAssessmentLayerThreeForeColor = cellAssessmentLayerThree.Style.ForeColor;
if (checkBoxSelected)
{
Assert.AreEqual(Color.FromKnownColor(KnownColor.DarkGray), cellAssessmentLayerTwoABackColor);
Assert.AreEqual(Color.FromKnownColor(KnownColor.GrayText), cellAssessmentLayerTwoAForeColor);
Assert.AreEqual(Color.FromKnownColor(KnownColor.DarkGray), cellAssessmentLayerThreeBackColor);
Assert.AreEqual(Color.FromKnownColor(KnownColor.GrayText), cellAssessmentLayerThreeForeColor);
}
else
{
Assert.AreEqual(Color.FromKnownColor(KnownColor.White), cellAssessmentLayerTwoABackColor);
Assert.AreEqual(Color.FromKnownColor(KnownColor.ControlText), cellAssessmentLayerTwoAForeColor);
Assert.AreEqual(Color.FromKnownColor(KnownColor.White), cellAssessmentLayerThreeBackColor);
Assert.AreEqual(Color.FromKnownColor(KnownColor.ControlText), cellAssessmentLayerThreeForeColor);
}
Assert.AreEqual(checkBoxSelected, cellAssessmentLayerThree.ReadOnly);
}
}
[Test]
public void GivenFormWithFailureMechanismResultView_ThenExpectedColumnsAreVisible()
{
// Given
using (ShowFailureMechanismResultsView())
{
// Then
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
Assert.AreEqual(4, dataGridView.ColumnCount);
Assert.IsInstanceOf(dataGridView.Columns[assessmentLayerOneIndex]);
Assert.IsInstanceOf(dataGridView.Columns[assessmentLayerTwoAIndex]);
Assert.IsInstanceOf(dataGridView.Columns[assessmentLayerThreeIndex]);
Assert.AreEqual(
RingtoetsCommonFormsResources.FailureMechanismResultView_InitializeDataGridView_Assessment_layer_one,
dataGridView.Columns[assessmentLayerOneIndex].HeaderText);
Assert.AreEqual(
RingtoetsCommonFormsResources.FailureMechanismResultView_InitializeDataGridView_Assessment_layer_two_a,
dataGridView.Columns[assessmentLayerTwoAIndex].HeaderText);
Assert.AreEqual(
RingtoetsCommonFormsResources.FailureMechanismResultView_InitializeDataGridView_Assessment_layer_three,
dataGridView.Columns[assessmentLayerThreeIndex].HeaderText);
Assert.AreEqual(DataGridViewAutoSizeColumnsMode.AllCells, dataGridView.AutoSizeColumnsMode);
Assert.AreEqual(DataGridViewContentAlignment.MiddleCenter, dataGridView.ColumnHeadersDefaultCellStyle.Alignment);
}
}
[Test]
public void GivenFormWithFailureMechanismResultView_WhenDataSourceWithFailureMechanismSectionResultAssigned_ThenSectionsAddedAsRows()
{
// Given
var section1 = new FailureMechanismSection("Section 1", new[]
{
new Point2D(0, 0)
});
var section2 = new FailureMechanismSection("Section 2", new[]
{
new Point2D(0, 0)
});
Random random = new Random(21);
var result1 = new StabilityPointStructuresFailureMechanismSectionResult(section1)
{
AssessmentLayerOne = true,
AssessmentLayerThree = (RoundedDouble) random.NextDouble()
};
var result2 = new StabilityPointStructuresFailureMechanismSectionResult(section2)
{
AssessmentLayerOne = false,
AssessmentLayerThree = (RoundedDouble) random.NextDouble()
};
using (var view = ShowFailureMechanismResultsView())
{
// When
view.Data = new[]
{
result1,
result2
};
// Then
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
DataGridViewRowCollection rows = dataGridView.Rows;
Assert.AreEqual(2, rows.Count);
DataGridViewCellCollection cells = rows[0].Cells;
Assert.AreEqual(4, cells.Count);
Assert.AreEqual("Section 1", cells[nameColumnIndex].FormattedValue);
Assert.AreEqual(result1.AssessmentLayerOne, cells[assessmentLayerOneIndex].Value);
Assert.AreEqual("-", cells[assessmentLayerTwoAIndex].FormattedValue);
Assert.AreEqual(result1.AssessmentLayerThree.ToString(), cells[assessmentLayerThreeIndex].FormattedValue);
AssertCellIsDisabled(cells[assessmentLayerTwoAIndex]);
AssertCellIsDisabled(cells[assessmentLayerThreeIndex]);
cells = rows[1].Cells;
Assert.AreEqual(4, cells.Count);
Assert.AreEqual("Section 2", cells[nameColumnIndex].FormattedValue);
Assert.AreEqual(result2.AssessmentLayerOne, cells[assessmentLayerOneIndex].Value);
Assert.AreEqual("-", cells[assessmentLayerTwoAIndex].FormattedValue);
Assert.AreEqual(result2.AssessmentLayerThree.ToString(), cells[assessmentLayerThreeIndex].FormattedValue);
AssertCellIsEnabled(cells[assessmentLayerTwoAIndex], true);
AssertCellIsEnabled(cells[assessmentLayerThreeIndex]);
}
}
[Test]
public void GivenFormWithFailureMechanismResultView_WhenSectionPassesLevel0AndListenersNotified_ThenRowsForSectionBecomesDisabled()
{
// Given
var section = new FailureMechanismSection("Section 1", new[]
{
new Point2D(0, 0)
});
Random random = new Random(21);
var result = new StabilityPointStructuresFailureMechanismSectionResult(section)
{
AssessmentLayerOne = false,
AssessmentLayerThree = (RoundedDouble) random.NextDouble()
};
using (var view = ShowFailureMechanismResultsView())
{
view.Data = new[]
{
result
};
// When
result.AssessmentLayerOne = true;
result.NotifyObservers();
// Then
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
DataGridViewRowCollection rows = dataGridView.Rows;
Assert.AreEqual(1, rows.Count);
DataGridViewCellCollection cells = rows[0].Cells;
Assert.AreEqual(4, cells.Count);
AssertCellIsDisabled(cells[assessmentLayerTwoAIndex]);
AssertCellIsDisabled(cells[assessmentLayerThreeIndex]);
}
}
[Test]
public void GivenFormWithFailureMechanismResultView_WhenDataSourceWithOtherFailureMechanismSectionResultAssigned_ThenSectionsNotAdded()
{
// Given
var section1 = CreateSimpleFailureMechanismSection();
var section2 = CreateSimpleFailureMechanismSection();
var result1 = new TestFailureMechanismSectionResult(section1);
var result2 = new TestFailureMechanismSectionResult(section2);
using (var view = ShowFailureMechanismResultsView())
{
// When
view.Data = new[]
{
result1,
result2
};
// Then
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
DataGridViewRowCollection rows = dataGridView.Rows;
Assert.AreEqual(0, rows.Count);
}
}
[Test]
public void GivenSectionResultWithoutCalculation_ThenLayerTwoAErrorTooltip()
{
// Given
using (StabilityPointStructuresFailureMechanismResultView view = ShowFailureMechanismResultsView())
{
FailureMechanismSection section = CreateSimpleFailureMechanismSection();
var sectionResult = new StabilityPointStructuresFailureMechanismSectionResult(section);
view.Data = new[]
{
sectionResult
};
var gridTester = new ControlTester("dataGridView");
var dataGridView = (DataGridView) gridTester.TheObject;
DataGridViewCell dataGridViewCell = dataGridView.Rows[0].Cells[assessmentLayerTwoAIndex];
// When
var formattedValue = dataGridViewCell.FormattedValue; // Need to do this to fire the CellFormatting event.
// Then
Assert.AreEqual("-", formattedValue);
Assert.AreEqual("Er moet een maatgevende berekening voor dit vak worden geselecteerd.", dataGridViewCell.ErrorText);
}
}
[Test]
public void GivenSectionResultAndCalculationNotCalculated_ThenLayerTwoAErrorTooltip()
{
// Given
using (StabilityPointStructuresFailureMechanismResultView view = ShowFailureMechanismResultsView())
{
var calculation = new StructuresCalculation();
FailureMechanismSection section = CreateSimpleFailureMechanismSection();
var sectionResult = new StabilityPointStructuresFailureMechanismSectionResult(section)
{
Calculation = calculation
};
view.Data = new[]
{
sectionResult
};
var gridTester = new ControlTester("dataGridView");
var dataGridView = (DataGridView) gridTester.TheObject;
DataGridViewCell dataGridViewCell = dataGridView.Rows[0].Cells[assessmentLayerTwoAIndex];
// When
var formattedValue = dataGridViewCell.FormattedValue; // Need to do this to fire the CellFormatting event.
// Then
Assert.AreEqual("-", formattedValue);
Assert.AreEqual("De maatgevende berekening voor dit vak moet nog worden uitgevoerd.", dataGridViewCell.ErrorText);
}
}
[Test]
public void GivenSectionResultAndFailedCalculation_ThenLayerTwoAErrorTooltip()
{
// Given
using (StabilityPointStructuresFailureMechanismResultView view = ShowFailureMechanismResultsView())
{
var calculation = new StructuresCalculation
{
Output = new ProbabilityAssessmentOutput(1.0, 1.0, double.NaN, 1.0, 1.0)
};
FailureMechanismSection section = CreateSimpleFailureMechanismSection();
var sectionResult = new StabilityPointStructuresFailureMechanismSectionResult(section)
{
Calculation = calculation
};
view.Data = new[]
{
sectionResult
};
var gridTester = new ControlTester("dataGridView");
var dataGridView = (DataGridView) gridTester.TheObject;
DataGridViewCell dataGridViewCell = dataGridView.Rows[0].Cells[assessmentLayerTwoAIndex];
// When
var formattedValue = dataGridViewCell.FormattedValue; // Need to do this to fire the CellFormatting event.
// Then
Assert.AreEqual("-", formattedValue);
Assert.AreEqual("De maatgevende berekening voor dit vak heeft geen geldige uitkomst.", dataGridViewCell.ErrorText);
}
}
[Test]
public void GivenSectionResultAndSuccessfulCalculation_ThenLayerTwoANoError()
{
// Given
using (StabilityPointStructuresFailureMechanismResultView view = ShowFailureMechanismResultsView())
{
const double probability = 0.56789;
var calculation = new StructuresCalculation
{
Output = new ProbabilityAssessmentOutput(1.0, 1.0, probability, 1.0, 1.0)
};
FailureMechanismSection section = CreateSimpleFailureMechanismSection();
var sectionResult = new StabilityPointStructuresFailureMechanismSectionResult(section)
{
Calculation = calculation
};
view.Data = new[]
{
sectionResult
};
var gridTester = new ControlTester("dataGridView");
var dataGridView = (DataGridView) gridTester.TheObject;
DataGridViewCell dataGridViewCell = dataGridView.Rows[0].Cells[assessmentLayerTwoAIndex];
// When
var formattedValue = dataGridViewCell.FormattedValue; // Need to do this to fire the CellFormatting event.
// Then
Assert.AreEqual(ProbabilityFormattingHelper.Format(probability), formattedValue);
Assert.IsEmpty(dataGridViewCell.ErrorText);
}
}
[Test]
public void GivenSectionResultAndSuccessfulCalculation_WhenChangingCalculationToFailed_ThenLayerTwoAHasError()
{
// Given
using (StabilityPointStructuresFailureMechanismResultView view = ShowFailureMechanismResultsView())
{
const double probability = 0.56789;
var successfulCalculation = new StructuresCalculation
{
Output = new ProbabilityAssessmentOutput(1.0, 1.0, probability, 1.0, 1.0)
};
var failedCalculation = new StructuresCalculation
{
Output = new ProbabilityAssessmentOutput(1.0, 1.0, double.NaN, 1.0, 1.0)
};
FailureMechanismSection section = CreateSimpleFailureMechanismSection();
var sectionResult = new StabilityPointStructuresFailureMechanismSectionResult(section)
{
Calculation = successfulCalculation
};
view.Data = new[]
{
sectionResult
};
var gridTester = new ControlTester("dataGridView");
var dataGridView = (DataGridView) gridTester.TheObject;
DataGridViewCell dataGridViewCell = dataGridView.Rows[0].Cells[assessmentLayerTwoAIndex];
// Precondition
var formattedValue = dataGridViewCell.FormattedValue; // Need to do this to fire the CellFormatting event.
Assert.AreEqual(ProbabilityFormattingHelper.Format(probability), formattedValue);
Assert.IsEmpty(dataGridViewCell.ErrorText);
// When
sectionResult.Calculation = failedCalculation;
formattedValue = dataGridViewCell.FormattedValue; // Need to do this to fire the CellFormatting event.
// Then
Assert.AreEqual("-", formattedValue);
Assert.AreEqual("De maatgevende berekening voor dit vak heeft geen geldige uitkomst.", dataGridViewCell.ErrorText);
}
}
private static FailureMechanismSection CreateSimpleFailureMechanismSection()
{
var section = new FailureMechanismSection("A",
new[]
{
new Point2D(1, 2),
new Point2D(3, 4)
});
return section;
}
private static void AssertCellIsDisabled(DataGridViewCell dataGridViewCell)
{
Assert.AreEqual(true, dataGridViewCell.ReadOnly);
Assert.AreEqual(Color.FromKnownColor(KnownColor.GrayText), dataGridViewCell.Style.ForeColor);
Assert.AreEqual(Color.FromKnownColor(KnownColor.DarkGray), dataGridViewCell.Style.BackColor);
}
private static void AssertCellIsEnabled(DataGridViewCell dataGridViewCell, bool readOnly = false)
{
Assert.AreEqual(readOnly, dataGridViewCell.ReadOnly);
Assert.AreEqual(Color.FromKnownColor(KnownColor.ControlText), dataGridViewCell.Style.ForeColor);
Assert.AreEqual(Color.FromKnownColor(KnownColor.White), dataGridViewCell.Style.BackColor);
}
private StabilityPointStructuresFailureMechanismResultView CreateConfiguredFailureMechanismResultsView()
{
var failureMechanism = new StabilityPointStructuresFailureMechanism();
failureMechanism.AddSection(new FailureMechanismSection("Section 1", new List
{
new Point2D(0.0, 0.0),
new Point2D(5.0, 0.0)
}));
failureMechanism.AddSection(new FailureMechanismSection("Section 2", new List
{
new Point2D(5.0, 0.0),
new Point2D(10.0, 0.0)
}));
var failureMechanismResultView = ShowFailureMechanismResultsView();
failureMechanismResultView.Data = failureMechanism.SectionResults;
failureMechanismResultView.FailureMechanism = failureMechanism;
return failureMechanismResultView;
}
private StabilityPointStructuresFailureMechanismResultView ShowFailureMechanismResultsView()
{
var failureMechanismResultView = new StabilityPointStructuresFailureMechanismResultView();
testForm.Controls.Add(failureMechanismResultView);
testForm.Show();
return failureMechanismResultView;
}
}
}