using System; using System.Linq.Expressions; using System.Reflection; using System.Windows.Forms; using Deltares.Standard.Forms; using Deltares.Standard.Forms.DExpress; using Deltares.Standard.Language; using Deltares.Standard.Units; using DevExpress.XtraEditors; using NUnit.Framework; namespace Deltares.DSoilModel.Forms.Tests { public class ControlTester { private KnownUnits knownUnits; private bool oldBypass; private LanguageType oldLanguage; public IPropertyControl PropertyControl { get; private set; } protected virtual IPropertyControl CreatePropertyControl() { throw new Exception("CreatePropertyControl() should return an instance. This instanse can be found in PropertyControl public property."); } [TestFixtureSetUp] public virtual void TestFixtureSetUp() { knownUnits = new KnownUnits(); oldBypass = BindSupport.BypassTimerForUnitTest; BindSupport.BypassTimerForUnitTest = true; oldLanguage = LocalizationManager.CurrentLanguage; LocalizationManager.CurrentLanguage = LanguageType.Dutch; PropertyControl = CreatePropertyControl(); } [TestFixtureTearDown] public virtual void TestFixtureTearDown() { PropertyControl.Dispose(); PropertyControl = null; BindSupport.ClearCaches(); knownUnits = null; BindSupport.BypassTimerForUnitTest = oldBypass; LocalizationManager.CurrentLanguage = oldLanguage; } protected object GetPrivateField(object obj, string field) { return obj.GetType().GetField(field, BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj); } private object GetValue(T target, Expression> outExpr) { var expr = (MemberExpression)outExpr.Body; var prop = (PropertyInfo)expr.Member; return prop.GetValue(target, null); } protected void TestControlVisibleEnabled(IPropertyControl parentControl, string spinEditName, bool isVisibleExpected, bool isEnabledExpected) { var edit = GetPrivateField(parentControl, spinEditName) as Control; Assert.IsNotNull(edit); Assert.AreEqual(isVisibleExpected, edit.Visible); Assert.AreEqual(isEnabledExpected, edit.Enabled); } protected void TestSpinEditVisibleEnabledValue(IPropertyControl parentControl, string spinEditName, bool isVisibleExpected, bool isEnabledExpected, T target, Expression> outExpr, double? valueToTestWith = null) { TestControlVisibleEnabled(parentControl, spinEditName, isVisibleExpected, isEnabledExpected); if (isVisibleExpected) { var edit = GetPrivateField(parentControl, spinEditName) as SpinEdit; Assert.IsNotNull(edit); var initialValue = GetValue(target, outExpr); Assert.AreEqual(initialValue, edit.Value); if (valueToTestWith == null) { return; } edit.EditValue = valueToTestWith; Assert.AreEqual(valueToTestWith, GetValue(target, outExpr)); edit.EditValue = initialValue; // restoring initial value - it might be still useful } } protected void TestTextEditVisibleEnabledValue(IPropertyControl parentControl, string textEditName, bool isVisibleExpected, bool isEnabledExpected, T target, Expression> outExpr, string newValue = null) { TestControlVisibleEnabled(parentControl, textEditName, isVisibleExpected, isEnabledExpected); var edit = GetPrivateField(parentControl, textEditName) as TextEdit; Assert.IsNotNull(edit); var initialValue = GetValue(target, outExpr); Assert.AreEqual(initialValue, edit.EditValue); if (newValue == null) { return; } edit.EditValue = newValue; Assert.AreEqual(newValue, GetValue(target, outExpr)); edit.EditValue = initialValue; // restoring initial value - it might be still useful } protected void TestLabelControlVisibleValue(IPropertyControl parentControl, string labelControlName, bool isVisibleExpected, string labelTextExpected) { TestControlVisibleEnabled(parentControl, labelControlName, isVisibleExpected, true); // labels are always enabled in DSL projects var labelControl = GetPrivateField(parentControl, labelControlName) as LabelControl; Assert.IsNotNull(labelControl); Assert.AreEqual(labelTextExpected, labelControl.Text); } protected void TestGridVisibleEnabledSize(IPropertyControl parentControl, string gridName, bool isVisibleExpected, bool isEnabledExpected, int rowCount, int colCount) { TestControlVisibleEnabled(parentControl, gridName, isVisibleExpected, isEnabledExpected); var gridControl = GetPrivateField(parentControl, gridName) as GridViewControl; Assert.IsNotNull(gridControl); Assert.AreEqual(rowCount, gridControl.gridView1.RowCount); Assert.AreEqual(colCount, gridControl.gridView1.Columns.Count); } } }