tbf/TBFTests/Rig/RegisterReaders/PoseidonCmdStartStop/PoseidonDialogTransferTest.cs

146 lines
6.3 KiB
C#

using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TBF.Rig.DataEntry.PoseidonCmd;
using TBF.Rig.RegisterReaders.PoseidonCmdStartStop;
using CmdPoseidonReader = TBF.Rig.RegisterReaders.PoseidonCmdStartStop.PoseidonReader;
namespace TBFTests.Rig.RegisterReaders.PoseidonCmdStartStop
{
[TestClass]
public class PoseidonDialogTransferTest
{
// Exact non-zero reading shape returned by HatCliDemo in the PT50 customer log.
private const string CustomerCliResponse =
"{\"Reading\":\"002433.50\",\"DeviceId\":\"1000000279\",\"ProductType\":74,\"ReadingComplete\":true,\"NfcTagDetected\":true,\"CliVersion\":\"2.0.0.2\"}";
[TestMethod]
public void CustomerCliValue_IsPrefilledConfirmedAndTransferred_ForStartAndEnd()
{
Exception failure = null;
var staThread = new Thread(() =>
{
try
{
RunDialogTransferScenario();
}
catch (Exception exception)
{
failure = exception;
}
});
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join(TimeSpan.FromSeconds(15));
Assert.IsFalse(staThread.IsAlive, "The Poseidon dialog test did not finish.");
if (failure != null)
throw failure;
}
private static void RunDialogTransferScenario()
{
var cliRunner = new CliRunner(false);
JsonDataFromPoseidon response;
Assert.IsTrue(cliRunner.TryJsonStringDeserialize(CustomerCliResponse, out response));
double cliValueLitres;
string failureReason;
Assert.IsTrue(CmdPoseidonReader.TryGetDialogValue(response, out cliValueLitres, out failureReason), failureReason);
Assert.AreNotEqual(0d, cliValueLitres);
VerifyStartDialogTransfer(cliValueLitres);
VerifyEndDialogTransfer(cliValueLitres);
}
private static void VerifyStartDialogTransfer(double expectedValue)
{
using (var dialog = new TestStartEndForm(1, null, new bool[1]))
{
dialog.CreateControl();
dialog.WMStartState[0] = expectedValue;
dialog.WMStartStateStr[0] = expectedValue.ToString();
dialog.UpdateValues(true, true);
TextBox textBox = FindTextBox(dialog, "startTextBox1");
Assert.AreEqual(dialog.WMStartStateStr[0], textBox.Text, "CLI value was not prefilled into the Start dialog.");
ConfirmDialog(dialog);
Assert.IsTrue(dialog.Completed);
Assert.AreEqual(expectedValue, dialog.WMStartState[0], 0.000001, "Start dialog did not parse the confirmed value.");
var entryForm = CreateEntryFormForTransfer(EntryForm.CurrentOp.ReadDatastream_StartStates);
TransferAcceptedDialogValues(entryForm, dialog);
Assert.AreEqual(expectedValue, entryForm.WMStartState(0), 0.000001,
"EntryForm did not receive the Start value confirmed in the dialog.");
}
}
private static void VerifyEndDialogTransfer(double expectedValue)
{
using (var dialog = new TestStartEndForm(1, null, new[] { expectedValue.ToString() }, new bool[1], 0, 0, 0))
{
dialog.CreateControl();
dialog.WMEndState[0] = expectedValue;
dialog.UpdateValues(false, true);
TextBox textBox = FindTextBox(dialog, "endTextBox1");
Assert.AreEqual(expectedValue.ToString(), textBox.Text, "CLI value was not prefilled into the End dialog.");
ConfirmDialog(dialog);
Assert.IsTrue(dialog.Completed);
Assert.AreEqual(expectedValue, dialog.WMEndState[0], 0.000001, "End dialog did not parse the confirmed value.");
var entryForm = CreateEntryFormForTransfer(EntryForm.CurrentOp.ReadDatastream_EndStates);
TransferAcceptedDialogValues(entryForm, dialog);
Assert.AreEqual(expectedValue, entryForm.WMEndState(0), 0.000001,
"EntryForm did not receive the End value confirmed in the dialog.");
}
}
private static TextBox FindTextBox(Control dialog, string name)
{
TextBox textBox = dialog.Controls.Find(name, true).OfType<TextBox>().FirstOrDefault();
Assert.IsNotNull(textBox, "Expected dialog control was not found: " + name);
return textBox;
}
private static void ConfirmDialog(TestStartEndForm dialog)
{
MethodInfo okHandler = typeof(TestStartEndForm).GetMethod("okButton_Click",
BindingFlags.Instance | BindingFlags.NonPublic);
Assert.IsNotNull(okHandler);
okHandler.Invoke(dialog, new object[] { dialog, EventArgs.Empty });
}
private static EntryForm CreateEntryFormForTransfer(EntryForm.CurrentOp currentOp)
{
var entryForm = new EntryForm();
SetPrivateField(entryForm, "currentOp", currentOp);
SetPrivateField(entryForm, "wmStartState", new double[1]);
SetPrivateField(entryForm, "wmStartStateStr", new string[1]);
SetPrivateField(entryForm, "wmEndState", new double[1]);
return entryForm;
}
private static void TransferAcceptedDialogValues(EntryForm entryForm, TestStartEndForm dialog)
{
MethodInfo transferMethod = typeof(EntryForm).GetMethod("StoreAcceptedDialogValues",
BindingFlags.Instance | BindingFlags.NonPublic);
Assert.IsNotNull(transferMethod);
transferMethod.Invoke(entryForm, new object[] { dialog });
}
private static void SetPrivateField(object instance, string name, object value)
{
FieldInfo field = instance.GetType().GetField(name,
BindingFlags.Instance | BindingFlags.NonPublic);
Assert.IsNotNull(field, "Expected field was not found: " + name);
field.SetValue(instance, value);
}
}
}