// 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.TestUtil; 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()) { var dataGridView = (DataGridView)new ControlTester("dataGridView").TheObject; var points = new[] { new Point2D(1, 2), new Point2D(3, 4) }; var section = new FailureMechanismSection("test", points); var sectionResult = new StabilityPointStructuresFailureMechanismSectionResult(section); var testData = new List { sectionResult }; // 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_DataNullAndDataGridViewEmtpy() { // 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] 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 = new FailureMechanismSection("Section 1", new[] { new Point2D(0, 0) }); var section2 = new FailureMechanismSection("Section 2", new[] { new Point2D(0, 0) }); 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); } } 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; } } }