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 readonly string InitialValueMismatchString = "Initial value mismatch"; private readonly string ObjectDidntReceivedValueString = "The object didn't receive the value"; private readonly string ClassFieldDoesNotExist = "Cannot find {0} in {1}"; private readonly string ClassFieldValueDoesntMatchExpected = "Field {0} has {1} type what is different from {2}"; 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 T GetPrivateField(object obj, string field) where T : class { var fi = obj.GetType().GetField(field, BindingFlags.NonPublic | BindingFlags.Instance); Assert.IsNotNull(fi, ClassFieldDoesNotExist, field, obj.GetType().Name); // even if we found the field it class can differ from expected var value = fi.GetValue(obj); Assert.IsInstanceOf(typeof (T), value, ClassFieldValueDoesntMatchExpected, field, value.GetType().Name, typeof(T).Name); return value as T; } 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); Assert.AreEqual(isVisibleExpected, edit.Visible, spinEditName + ": Visible-check failed"); Assert.AreEqual(isEnabledExpected, edit.Enabled, spinEditName + ": Enabled-check failed"); } 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); var initialValue = GetValue(target, outExpr); Assert.AreEqual(initialValue, edit.EditValue, InitialValueMismatchString); if (valueToTestWith != null) { edit.EditValue = valueToTestWith; Assert.AreEqual(valueToTestWith, GetValue(target, outExpr), ObjectDidntReceivedValueString); 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); if (isVisibleExpected) { var edit = GetPrivateField(parentControl, textEditName); var initialValue = GetValue(target, outExpr); Assert.AreEqual(initialValue, edit.EditValue, InitialValueMismatchString); if (newValue != null) { edit.EditValue = newValue; Assert.AreEqual(newValue, GetValue(target, outExpr), ObjectDidntReceivedValueString); edit.EditValue = initialValue; // restoring initial value - it might be still useful } } } protected void TestComboBoxVisibleEnabledValue(IPropertyControl parentControl, string textEditName, bool isVisibleExpected, bool isEnabledExpected, T target, Expression> outExpr, TEnum newValue = default(TEnum)) { TestControlVisibleEnabled(parentControl, textEditName, isVisibleExpected, isEnabledExpected); if (isVisibleExpected) { var edit = GetPrivateField(parentControl, textEditName); var initialValue = GetValue(target, outExpr); var editValue = ((DisplayItem) (edit.SelectedItem)).Value; // we need this hack, becuase edit.EditValue contains formatted and translated text Assert.AreEqual(initialValue, editValue, InitialValueMismatchString); if (newValue != null) { edit.EditValue = newValue; Assert.AreEqual(newValue, GetValue(target, outExpr), ObjectDidntReceivedValueString); 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); Assert.AreEqual(labelTextExpected, labelControl.Text, InitialValueMismatchString); } 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); Assert.AreEqual(rowCount, gridControl.gridView1.RowCount, "Row count mismatch"); Assert.AreEqual(colCount, gridControl.gridView1.Columns.Count, "Columns count mismatch"); } } }