// 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.Linq;
using System.Windows.Forms;
using Core.Common.Base.Geometry;
using NUnit.Extensions.Forms;
using NUnit.Framework;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.Hydraulics;
using Ringtoets.Common.Data.TestUtil;
using Ringtoets.Common.Forms.Views;
namespace Ringtoets.Common.Forms.Test.Views
{
[TestFixture]
public class HydraulicBoundaryLocationsViewTest
{
private const int locationCalculateColumnIndex = 0;
private const int locationNameColumnIndex = 1;
private const int locationIdColumnIndex = 2;
private const int locationColumnIndex = 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 TestHydraulicBoundaryLocationsView())
{
// Assert
Assert.IsInstanceOf>(view);
Assert.IsNull(view.Data);
}
}
[Test]
public void Constructor_DataGridViewCorrectlyInitialized()
{
// Setup & Call
ShowTestHydraulicBoundaryLocationsView();
// Assert
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
Assert.AreEqual(4, dataGridView.ColumnCount);
var locationCalculateColumn = (DataGridViewCheckBoxColumn) dataGridView.Columns[locationCalculateColumnIndex];
const string expectedLocationCalculateHeaderText = "Berekenen";
Assert.AreEqual(expectedLocationCalculateHeaderText, locationCalculateColumn.HeaderText);
var locationNameColumn = (DataGridViewTextBoxColumn) dataGridView.Columns[locationNameColumnIndex];
const string expectedLocationNameHeaderText = "Naam";
Assert.AreEqual(expectedLocationNameHeaderText, locationNameColumn.HeaderText);
var locationIdColumn = (DataGridViewTextBoxColumn) dataGridView.Columns[locationIdColumnIndex];
const string expectedLocationIdHeaderText = "ID";
Assert.AreEqual(expectedLocationIdHeaderText, locationIdColumn.HeaderText);
var locationColumn = (DataGridViewTextBoxColumn) dataGridView.Columns[locationColumnIndex];
const string expectedLocationHeaderText = "Coördinaten [m]";
Assert.AreEqual(expectedLocationHeaderText, locationColumn.HeaderText);
var buttonTester = new ButtonTester("CalculateForSelectedButton", testForm);
var button = (Button) buttonTester.TheObject;
Assert.IsFalse(button.Enabled);
}
[Test]
public void Data_IAssessmentSection_DataSet()
{
// Setup
using (var view = new TestHydraulicBoundaryLocationsView())
{
var hydraulicBoundaryLocations = Enumerable.Empty();
// Call
view.Data = hydraulicBoundaryLocations;
// Assert
Assert.AreSame(hydraulicBoundaryLocations, view.Data);
}
}
[Test]
public void Data_OtherThanIAssessmentSection_DataNull()
{
// Setup
using (var view = new TestHydraulicBoundaryLocationsView())
{
var data = new object();
// Call
view.Data = data;
// Assert
Assert.IsNull(view.Data);
}
}
[Test]
public void HydraulicBoundaryLocationsView_AssessmentSectionWithData_DataGridViewCorrectlyInitialized()
{
// Setup & Call
ShowFullyConfiguredTestHydraulicBoundaryLocationsView();
// Assert
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
var rows = dataGridView.Rows;
Assert.AreEqual(3, rows.Count);
var cells = rows[0].Cells;
Assert.AreEqual(4, cells.Count);
Assert.AreEqual(false, cells[locationCalculateColumnIndex].FormattedValue);
Assert.AreEqual("1", cells[locationNameColumnIndex].FormattedValue);
Assert.AreEqual("1", cells[locationIdColumnIndex].FormattedValue);
Assert.AreEqual(new Point2D(1, 1).ToString(), cells[locationColumnIndex].FormattedValue);
cells = rows[1].Cells;
Assert.AreEqual(4, cells.Count);
Assert.AreEqual(false, cells[locationCalculateColumnIndex].FormattedValue);
Assert.AreEqual("2", cells[locationNameColumnIndex].FormattedValue);
Assert.AreEqual("2", cells[locationIdColumnIndex].FormattedValue);
Assert.AreEqual(new Point2D(2, 2).ToString(), cells[locationColumnIndex].FormattedValue);
cells = rows[2].Cells;
Assert.AreEqual(4, cells.Count);
Assert.AreEqual(false, cells[locationCalculateColumnIndex].FormattedValue);
Assert.AreEqual("3", cells[locationNameColumnIndex].FormattedValue);
Assert.AreEqual("3", cells[locationIdColumnIndex].FormattedValue);
Assert.AreEqual(new Point2D(3, 3).ToString(), cells[locationColumnIndex].FormattedValue);
}
[Test]
public void CalculateForSelectedButton_OneSelectedButCalculationGuiServiceNotSet_DoesNotThrowException()
{
// Setup
ShowFullyConfiguredTestHydraulicBoundaryLocationsView();
var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;
var rows = dataGridView.Rows;
rows[0].Cells[locationCalculateColumnIndex].Value = true;
var button = new ButtonTester("CalculateForSelectedButton", testForm);
// Call
TestDelegate test = () => button.Click();
// Assert
Assert.DoesNotThrow(test);
}
private TestHydraulicBoundaryLocationsView ShowTestHydraulicBoundaryLocationsView()
{
var view = new TestHydraulicBoundaryLocationsView();
testForm.Controls.Add(view);
testForm.Show();
return view;
}
private TestHydraulicBoundaryLocationsView ShowFullyConfiguredTestHydraulicBoundaryLocationsView()
{
var view = ShowTestHydraulicBoundaryLocationsView();
var assessmentSection = new ObservableTestAssessmentSectionStub
{
HydraulicBoundaryDatabase = new TestHydraulicBoundaryDatabase()
};
view.Data = assessmentSection.HydraulicBoundaryDatabase.Locations;
return view;
}
private class TestHydraulicBoundaryDatabase : HydraulicBoundaryDatabase
{
public TestHydraulicBoundaryDatabase()
{
Locations.Add(new HydraulicBoundaryLocation(1, "1", 1.0, 1.0));
Locations.Add(new HydraulicBoundaryLocation(2, "2", 2.0, 2.0)
{
DesignWaterLevelOutput = new TestHydraulicBoundaryLocationOutput(1.23)
});
Locations.Add(new HydraulicBoundaryLocation(3, "3", 3.0, 3.0)
{
WaveHeightOutput = new TestHydraulicBoundaryLocationOutput(2.45)
});
}
}
private class TestHydraulicBoundaryLocationRow : HydraulicBoundaryLocationRow
{
public TestHydraulicBoundaryLocationRow(HydraulicBoundaryLocation hydraulicBoundaryLocation)
: base(hydraulicBoundaryLocation) {}
}
private sealed class TestHydraulicBoundaryLocationsView : HydraulicBoundaryLocationsView
{
public override IAssessmentSection AssessmentSection { get; set; }
protected override TestHydraulicBoundaryLocationRow CreateNewRow(HydraulicBoundaryLocation location)
{
return new TestHydraulicBoundaryLocationRow(location);
}
protected override void HandleCalculateSelectedLocations(IEnumerable locations)
{
throw new NotImplementedException();
}
protected override object CreateSelectedItemFromCurrentRow()
{
throw new NotImplementedException();
}
}
}
}