tbf/TBFTests/Rig/DataEntry/Uni/TestStartEndFormTest.cs

168 lines
6.2 KiB
C#
Raw Normal View History

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using Common;
using Config.Entities;
using JetBrains.Annotations;
using Results.Entities;
using TBF.Rig.DataEntry;
using TBF.Rig.DataEntry.Uni;
using TBF.Rig.Generic;
using TBF.Rig.GenericDevices;
namespace TBFTests.Rig.DataEntry.Uni
{
[TestClass]
[TestSubject(typeof(TestStartEndForm))]
public class TestStartEndFormTest
{
[TestMethod]
public void PopulateVolume_TestRollOver()
{
Exception staEx = null;
var t = new Thread(() =>
{
try
{
// Arrange
double volRange = GetConstDouble(typeof(TestStartEndForm), "VOL_RANGE_LITERS");
// Example: begin = R-10, v=10 => expectedRaw = begin + 20 (shortest jump across modulo)
double begin = volRange - 10.0;
double entered = 10.0;
double expectedRaw = begin + 20.0;
var reader = new FakeRegReader
{
Name = "R1",
BeginWMState = begin
};
var waterMeters = new List<WaterMeter>
{
new WaterMeter { Disabled = false }
};
// Use your DEItem class exactly:
// - Content must be Ct.EndState (because isEnd==true)
// - Action must not be LoadReadOnly, otherwise textbox may be disabled (not critical for setting Text,
// but keep it editable)
IList<DEItem> colItems = new List<DEItem>
{
new DEItem(Ct.EndState, "End", Ac.Clear, 120)
};
// Force isEnd=true (constructor: isEnd = (wmStartStateStr != null))
// Length must match (isCompound?2:1)*waterMeters.Count -> here 1*1 = 1.
string[] wmStartStateStr = new[] { "x" };
var sut = new TestStartEndForm(
waterMeters: waterMeters,
regReaders: new IRegReader[] { reader },
_lineSize: 1,
title: "test",
sz: FontSz.S,
isStartBoxAlwaysEn: false,
isLrOrder: false,
formCloseKeys: "",
colItems: colItems,
isCompound: false,
initialVolumeUnit: Unit.l, // keep conversion identity
isCameraPicture: false,
startImages: Array.Empty<string>(),
endImages: Array.Empty<string>(),
ocrVidi: null,
ocrMessage: "",
streamName: "",
bAutoRead: false,
iAutocloseGap: 0,
wmStartStateStr: wmStartStateStr,
refVolume: 0,
errLimLo: 0,
errLimHi: 0
);
// Act
sut.PopulateVolume(reader, entered);
// Assert
var tb = GetTextBox(sut, row: 0, col: 0);
Assert.IsNotNull(tb, "Expected TextBox[0,0] to exist.");
Assert.AreEqual(expectedRaw.ToString(), tb.Text);
}
catch (Exception ex)
{
staEx = ex;
}
});
t.SetApartmentState(ApartmentState.STA); // WinForms UI objects => STA is safest
t.Start();
t.Join();
if (staEx != null)
throw new AssertFailedException("PopulateVolume_TestRollOver failed.", staEx);
}
// ----- helpers -----
private static double GetConstDouble(Type type, string fieldName)
{
var f = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
if (f == null) throw new MissingFieldException(type.FullName, fieldName);
object v = f.IsLiteral ? f.GetRawConstantValue() : f.GetValue(null);
return Convert.ToDouble(v);
}
private static System.Windows.Forms.TextBox GetTextBox(TestStartEndForm sut, int row, int col)
{
var f = typeof(TestStartEndForm).GetField("textBoxes", BindingFlags.Instance | BindingFlags.NonPublic);
if (f == null) throw new MissingFieldException(typeof(TestStartEndForm).FullName, "textBoxes");
var grid = (System.Windows.Forms.TextBox[,])f.GetValue(sut);
return grid[row, col];
}
private sealed class FakeRegReader : IRegReader
{
public string Name { get; set; }
public string ClassName { get; }
public string ParentName { get; }
public DebugMode DebugLevel { get; }
public LogLevel LogLevel { get; }
public IComponentCfg Cfg { get; }
public IList<MeasurementCorrection> Corrections { get; set; }
public IList<Uncertainty> Uncertainties { get; set; }
public void Initialize()
{
throw new NotImplementedException();
}
public void StartChangeHandler()
{
throw new NotImplementedException();
}
public void StopChangeHandler()
{
throw new NotImplementedException();
}
public RegisterReaderType RegisterReaderType { get; }
public int Position { get; }
public double PulsesPerLtr { get; set; }
public double LtrsPerPulse { get; }
public string QuantityUnits { get; set; }
public int WMPulses { get; }
public int WMRefPulses { get; }
public double WMVolume { get; }
public double BeginWMState { get; set; }
public double EndWMState { get; }
}
}
}