// 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;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Core.Common.Base;
using Core.Common.Gui;
using Core.Common.Gui.Commands;
using Core.Common.Gui.Forms.MainWindow;
using Core.Common.Gui.Forms.ViewHost;
using Core.Common.Gui.Plugin;
using Core.Common.TestUtil;
using NUnit.Framework;
using Rhino.Mocks;
using Ringtoets.Common.Data.Hydraulics;
using Ringtoets.Common.Data.TestUtil;
using Ringtoets.Common.Forms.GuiServices;
using Ringtoets.Common.Forms.TestUtil;
using Ringtoets.Integration.Forms.PresentationObjects;
using Ringtoets.Integration.Forms.Views;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
namespace Ringtoets.Integration.Plugin.Test.ViewInfos
{
[TestFixture]
public class DesignWaterLevelLocationsViewInfoTest
{
private const int locationDesignWaterlevelColumnIndex = 5;
private RingtoetsPlugin plugin;
private ViewInfo info;
[SetUp]
public void SetUp()
{
plugin = new RingtoetsPlugin();
info = plugin.GetViewInfos().First(tni => tni.ViewType == typeof(DesignWaterLevelLocationsView));
}
[TearDown]
public void TearDown()
{
plugin.Dispose();
}
[Test]
public void GetViewName_Always_ReturnsViewName()
{
// Setup
using (var view = new DesignWaterLevelLocationsView(new ObservableList(),
hbl => new HydraulicBoundaryLocationCalculation(),
new ObservableTestAssessmentSectionStub(),
() => 0.01))
{
// Call
string viewName = info.GetViewName(view, Enumerable.Empty());
// Assert
Assert.AreEqual("Toetspeilen", viewName);
}
}
[Test]
public void ViewDataType_Always_ReturnsViewDataType()
{
// Call
Type viewDataType = info.ViewDataType;
// Assert
Assert.AreEqual(typeof(IEnumerable), viewDataType);
}
[Test]
public void DataType_Always_ReturnsDataType()
{
// Call
Type dataType = info.DataType;
// Assert
Assert.AreEqual(typeof(DesignWaterLevelLocationsContext), dataType);
}
[Test]
public void Image_Always_ReturnsGenericInputOutputIcon()
{
// Call
Image image = info.Image;
// Assert
TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.GenericInputOutputIcon, image);
}
[Test]
public void GetViewData_Always_ReturnsHydraulicBoundaryLocations()
{
// Setup
var assessmentSection = new ObservableTestAssessmentSectionStub();
ObservableList locations = assessmentSection.HydraulicBoundaryDatabase.Locations;
var context = new DesignWaterLevelLocationsContext(locations,
assessmentSection,
hbl => new HydraulicBoundaryLocationCalculation(),
"Category");
// Call
object viewData = info.GetViewData(context);
// Assert
Assert.AreSame(locations, viewData);
}
[Test]
public void CreateInstance_WithContext_SetsExpectedData()
{
// Setup
var random = new Random();
var assessmentSection = new ObservableTestAssessmentSectionStub();
var hydraulicBoundaryLocations = new ObservableList();
var hydraulicBoundaryLocationsLookup = new Dictionary
{
{
new TestHydraulicBoundaryLocation(),
new HydraulicBoundaryLocationCalculation
{
Output = new TestHydraulicBoundaryLocationOutput(random.NextDouble())
}
},
{
new TestHydraulicBoundaryLocation(),
new HydraulicBoundaryLocationCalculation
{
Output = new TestHydraulicBoundaryLocationOutput(random.NextDouble())
}
}
};
hydraulicBoundaryLocations.AddRange(hydraulicBoundaryLocationsLookup.Keys);
var context = new DesignWaterLevelLocationsContext(hydraulicBoundaryLocations,
assessmentSection,
hbl => hydraulicBoundaryLocationsLookup[hbl],
"Category");
using (var ringtoetsPlugin = new RingtoetsPlugin())
{
info = ringtoetsPlugin.GetViewInfos().First(tni => tni.ViewType == typeof(DesignWaterLevelLocationsView));
// Call
var view = info.CreateInstance(context) as DesignWaterLevelLocationsView;
// Assert
Assert.AreSame(assessmentSection, view.AssessmentSection);
using (var testForm = new Form())
{
testForm.Controls.Add(view);
testForm.Show();
DataGridView locationsDataGridView = ControlTestHelper.GetDataGridView(view, "DataGridView");
DataGridViewRowCollection rows = locationsDataGridView.Rows;
Assert.AreEqual(2, rows.Count);
Assert.AreEqual(hydraulicBoundaryLocationsLookup.Values.ElementAt(0).Output.Result.ToString(), rows[0].Cells[locationDesignWaterlevelColumnIndex].FormattedValue);
Assert.AreEqual(hydraulicBoundaryLocationsLookup.Values.ElementAt(1).Output.Result.ToString(), rows[1].Cells[locationDesignWaterlevelColumnIndex].FormattedValue);
}
}
}
[Test]
[Apartment(ApartmentState.STA)]
public void AfterCreate_WithGuiSet_SetsSpecificPropertiesToView()
{
// Setup
var mocks = new MockRepository();
var gui = mocks.Stub();
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
gui.Stub(g => g.ViewCommands).Return(mocks.Stub());
gui.Stub(g => g.MainWindow).Return(mocks.Stub());
gui.Stub(g => g.DocumentViewController).Return(mocks.Stub());
mocks.ReplayAll();
var assessmentSection = new ObservableTestAssessmentSectionStub();
var locations = new ObservableList();
var context = new DesignWaterLevelLocationsContext(locations,
assessmentSection,
hbl => new HydraulicBoundaryLocationCalculation(),
"Category");
using (var view = new DesignWaterLevelLocationsView(locations,
hbl => new HydraulicBoundaryLocationCalculation(),
new ObservableTestAssessmentSectionStub(),
() => 0.01))
using (var ringtoetsPlugin = new RingtoetsPlugin())
{
info = ringtoetsPlugin.GetViewInfos().First(tni => tni.ViewType == typeof(DesignWaterLevelLocationsView));
ringtoetsPlugin.Gui = gui;
ringtoetsPlugin.Activate();
// Call
info.AfterCreate(view, context);
// Assert
Assert.IsInstanceOf(view.CalculationGuiService);
}
mocks.VerifyAll();
}
[Test]
public void CloseViewForData_ForMatchingAssessmentSection_ReturnsTrue()
{
// Setup
var assessmentSection = new ObservableTestAssessmentSectionStub();
using (var view = new DesignWaterLevelLocationsView(new ObservableList(),
hbl => new HydraulicBoundaryLocationCalculation(),
assessmentSection,
() => 0.01))
{
// Call
bool closeForData = info.CloseForData(view, assessmentSection);
// Assert
Assert.IsTrue(closeForData);
}
}
[Test]
public void CloseViewForData_ForNonMatchingAssessmentSection_ReturnsFalse()
{
// Setup
var assessmentSectionA = new ObservableTestAssessmentSectionStub();
var assessmentSectionB = new ObservableTestAssessmentSectionStub();
using (var view = new DesignWaterLevelLocationsView(new ObservableList(),
hbl => new HydraulicBoundaryLocationCalculation(),
assessmentSectionA,
() => 0.01))
{
// Call
bool closeForData = info.CloseForData(view, assessmentSectionB);
// Assert
Assert.IsFalse(closeForData);
}
}
[Test]
public void CloseViewForData_ForOtherObjectType_ReturnsFalse()
{
// Setup
var assessmentSectionA = new ObservableTestAssessmentSectionStub();
using (var view = new DesignWaterLevelLocationsView(new ObservableList(),
hbl => new HydraulicBoundaryLocationCalculation(),
assessmentSectionA,
() => 0.01))
{
// Call
bool closeForData = info.CloseForData(view, new object());
// Assert
Assert.IsFalse(closeForData);
}
}
}
}