// 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; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using Core.Common.Base; using Core.Common.Base.Geometry; using Core.Components.Gis.Data; using Core.Components.Gis.Forms; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Data.TestUtil; using Ringtoets.Common.Forms.TestUtil; using Ringtoets.Common.Forms.Views; using Ringtoets.Integration.Forms.Views; namespace Ringtoets.Integration.Forms.Test.Views { [TestFixture] public class AssessmentSectionViewTest { private const int referenceLineIndex = 0; private const int hydraulicBoundaryLocationsIndex = 1; [Test] public void Constructor_ExpectedValues() { // Setup var assessmentSection = new AssessmentSectionStub(); // Call using (var view = new AssessmentSectionView(assessmentSection)) { // Assert Assert.IsInstanceOf(view); Assert.IsInstanceOf(view); Assert.IsNull(view.Data); Assert.AreEqual(1, view.Controls.Count); Assert.IsInstanceOf(view.Controls[0]); Assert.AreSame(view.Map, ((RingtoetsMapControl) view.Controls[0]).MapControl); Assert.AreEqual(DockStyle.Fill, ((Control) view.Map).Dock); AssertEmptyMapData(view.Map.Data); } } [Test] public void Constructor_AssessmentSectionNull_ThrowsArgumentNullException() { // Call TestDelegate call = () => new AssessmentSectionView(null); // Assert var exception = Assert.Throws(call); Assert.AreEqual("assessmentSection", exception.ParamName); } [Test] public void Constructor_AssessmentSectionWithBackgroundData_BackgroundDataSet() { // Setup var assessmentSection = new AssessmentSectionStub(); // Call using (var view = new AssessmentSectionView(assessmentSection)) { // Assert MapDataTestHelper.AssertImageBasedMapData(assessmentSection.BackgroundData, view.Map.BackgroundMapData); } } [Test] public void Constructor_WithReferenceLineAndHydraulicBoundaryDatabase_DataUpdatedToCollectionOfFilledMapData() { // Setup var referenceLine = new ReferenceLine(); referenceLine.SetGeometry(new[] { new Point2D(1.0, 2.0), new Point2D(2.0, 1.0) }); var assessmentSection = new AssessmentSectionStub { ReferenceLine = referenceLine }; assessmentSection.SetHydraulicBoundaryLocationCalculations(new[] { new HydraulicBoundaryLocation(1, "test", 1.0, 2.0) }); // Call using (var view = new AssessmentSectionView(assessmentSection)) { // Assert Assert.IsInstanceOf(view.Map.Data); MapDataCollection mapData = view.Map.Data; Assert.IsNotNull(mapData); MapData hydraulicBoundaryLocationsMapData = mapData.Collection.ElementAt(hydraulicBoundaryLocationsIndex); MapDataTestHelper.AssertHydraulicBoundaryLocationsMapData(assessmentSection, hydraulicBoundaryLocationsMapData); MapData referenceLineMapData = mapData.Collection.ElementAt(referenceLineIndex); AssertReferenceLineMapData(referenceLine, referenceLineMapData); } } [Test] [TestCaseSource(typeof(MapViewTestHelper), nameof(MapViewTestHelper.GetCalculationFuncs))] public void GivenViewWithHydraulicBoundaryLocationsData_WhenHydraulicBoundaryLocationCalculationUpdatedAndNotified_ThenMapDataUpdated( Func getCalculationFunc) { // Given var hydraulicBoundaryLocation = new HydraulicBoundaryLocation(1, "test1", 1.0, 2.0); var assessmentSection = new AssessmentSectionStub(); assessmentSection.SetHydraulicBoundaryLocationCalculations(new[] { hydraulicBoundaryLocation }); using (var view = new AssessmentSectionView(assessmentSection)) { IMapControl map = ((RingtoetsMapControl) view.Controls[0]).MapControl; var mocks = new MockRepository(); IObserver[] observers = AttachMapDataObservers(mocks, map.Data.Collection); observers[hydraulicBoundaryLocationsIndex].Expect(obs => obs.UpdateObserver()); mocks.ReplayAll(); MapData hydraulicBoundaryLocationsMapData = map.Data.Collection.ElementAt(hydraulicBoundaryLocationsIndex); // Precondition MapDataTestHelper.AssertHydraulicBoundaryLocationsMapData(assessmentSection, hydraulicBoundaryLocationsMapData); // When HydraulicBoundaryLocationCalculation calculation = getCalculationFunc(assessmentSection); calculation.Output = new TestHydraulicBoundaryLocationCalculationOutput(new Random(21).NextDouble()); calculation.NotifyObservers(); // Then MapDataTestHelper.AssertHydraulicBoundaryLocationsMapData(assessmentSection, hydraulicBoundaryLocationsMapData); mocks.VerifyAll(); } } [Test] public void GivenViewWithHydraulicBoundaryLocationsDatabase_WhenChangingHydraulicBoundaryLocationsDataAndObserversNotified_ThenMapDataUpdated() { // Given var assessmentSection = new AssessmentSectionStub(); assessmentSection.SetHydraulicBoundaryLocationCalculations(new[] { new HydraulicBoundaryLocation(1, "test1", 1.0, 2.0) }); using (var view = new AssessmentSectionView(assessmentSection)) { IMapControl map = ((RingtoetsMapControl) view.Controls[0]).MapControl; var mocks = new MockRepository(); IObserver[] observers = AttachMapDataObservers(mocks, map.Data.Collection); observers[hydraulicBoundaryLocationsIndex].Expect(obs => obs.UpdateObserver()); mocks.ReplayAll(); MapData hydraulicBoundaryLocationsMapData = map.Data.Collection.ElementAt(hydraulicBoundaryLocationsIndex); // Precondition MapDataTestHelper.AssertHydraulicBoundaryLocationsMapData(assessmentSection, hydraulicBoundaryLocationsMapData); // When assessmentSection.SetHydraulicBoundaryLocationCalculations(new[] { new HydraulicBoundaryLocation(2, "test2", 2.0, 3.0) }); assessmentSection.HydraulicBoundaryDatabase.Locations.NotifyObservers(); // Then MapDataTestHelper.AssertHydraulicBoundaryLocationsMapData(assessmentSection, hydraulicBoundaryLocationsMapData); mocks.VerifyAll(); } } [Test] public void UpdateObserver_ReferenceLineUpdated_MapDataUpdated() { // Setup var referenceLine = new ReferenceLine(); referenceLine.SetGeometry(new List { new Point2D(1.0, 2.0), new Point2D(2.0, 1.0) }); var assessmentSection = new AssessmentSectionStub { ReferenceLine = referenceLine }; using (var view = new AssessmentSectionView(assessmentSection)) { IMapControl map = ((RingtoetsMapControl) view.Controls[0]).MapControl; var mocks = new MockRepository(); IObserver[] observers = AttachMapDataObservers(mocks, map.Data.Collection); observers[referenceLineIndex].Expect(obs => obs.UpdateObserver()); mocks.ReplayAll(); MapData referenceLineMapData = map.Data.Collection.ElementAt(referenceLineIndex); // Precondition AssertReferenceLineMapData(assessmentSection.ReferenceLine, referenceLineMapData); // Call assessmentSection.ReferenceLine.SetGeometry(new List { new Point2D(2.0, 5.0), new Point2D(4.0, 3.0) }); assessmentSection.NotifyObservers(); // Assert AssertReferenceLineMapData(assessmentSection.ReferenceLine, referenceLineMapData); mocks.VerifyAll(); } } [Test] public void UpdateObserver_DataUpdated_MapLayersSameOrder() { // Setup var referenceLine = new ReferenceLine(); referenceLine.SetGeometry(new[] { new Point2D(1.0, 2.0), new Point2D(2.0, 1.0) }); var assessmentSection = new AssessmentSectionStub { ReferenceLine = referenceLine }; assessmentSection.SetHydraulicBoundaryLocationCalculations(new[] { new HydraulicBoundaryLocation(1, "test1", 1.0, 2.0) }); using (var view = new AssessmentSectionView(assessmentSection)) { MapDataCollection mapData = view.Map.Data; MapData dataToMove = mapData.Collection.ElementAt(0); mapData.Remove(dataToMove); mapData.Add(dataToMove); // Precondition var referenceLineMapData = (MapLineData) mapData.Collection.ElementAt(referenceLineIndex + 1); Assert.AreEqual("Referentielijn", referenceLineMapData.Name); var hrLocationsMapData = (MapPointData) mapData.Collection.ElementAt(hydraulicBoundaryLocationsIndex - 1); Assert.AreEqual("Hydraulische belastingen", hrLocationsMapData.Name); // Call assessmentSection.SetHydraulicBoundaryLocationCalculations(new[] { new HydraulicBoundaryLocation(2, "test2", 2.0, 3.0) }); assessmentSection.HydraulicBoundaryDatabase.Locations.NotifyObservers(); // Assert var actualReferenceLineMapData = (MapLineData) mapData.Collection.ElementAt(referenceLineIndex + 1); Assert.AreEqual("Referentielijn", actualReferenceLineMapData.Name); var actualHrLocationsMapData = (MapPointData) mapData.Collection.ElementAt(hydraulicBoundaryLocationsIndex - 1); Assert.AreEqual("Hydraulische belastingen", actualHrLocationsMapData.Name); } } private static void AssertReferenceLineMapData(ReferenceLine referenceLine, MapData referenceLineMapData) { MapDataTestHelper.AssertReferenceLineMapData(referenceLine, referenceLineMapData); Assert.IsTrue(referenceLineMapData.IsVisible); } private static void AssertEmptyMapData(MapDataCollection mapDataCollection) { Assert.AreEqual("Trajectkaart", mapDataCollection.Name); List mapDataList = mapDataCollection.Collection.ToList(); Assert.AreEqual(2, mapDataList.Count); var referenceLineMapData = (MapLineData) mapDataList[referenceLineIndex]; var hydraulicBoundaryLocationsMapData = (MapPointData) mapDataList[hydraulicBoundaryLocationsIndex]; CollectionAssert.IsEmpty(referenceLineMapData.Features); CollectionAssert.IsEmpty(hydraulicBoundaryLocationsMapData.Features); Assert.AreEqual("Referentielijn", referenceLineMapData.Name); Assert.AreEqual("Hydraulische belastingen", hydraulicBoundaryLocationsMapData.Name); } /// /// Attaches mocked observers to all map data components. /// /// The . /// The map data collection containing the /// elements. /// An array of mocked observers attached to the data in . private static IObserver[] AttachMapDataObservers(MockRepository mocks, IEnumerable mapData) { MapData[] mapDataArray = mapData.ToArray(); var referenceLineMapDataObserver = mocks.StrictMock(); mapDataArray[referenceLineIndex].Attach(referenceLineMapDataObserver); var hydraulicBoundaryLocationsMapDataObserver = mocks.StrictMock(); mapDataArray[hydraulicBoundaryLocationsIndex].Attach(hydraulicBoundaryLocationsMapDataObserver); return new[] { referenceLineMapDataObserver, hydraulicBoundaryLocationsMapDataObserver }; } } }