diff --git a/FlightStreamDeck.Logics/Actions/NavCom/AdfHandler.cs b/FlightStreamDeck.Logics/Actions/NavCom/AdfHandler.cs index 4d6c514..f82aa1f 100644 --- a/FlightStreamDeck.Logics/Actions/NavCom/AdfHandler.cs +++ b/FlightStreamDeck.Logics/Actions/NavCom/AdfHandler.cs @@ -8,12 +8,21 @@ public class AdfHandler : HzHandler public AdfHandler( IFlightConnector flightConnector, IEventRegistrar eventRegistrar, IEventDispatcher eventDispatcher, TOGGLE_VALUE active, TOGGLE_VALUE? standby, TOGGLE_VALUE? batteryVariable, TOGGLE_VALUE? avionicsVariable, - KnownEvents? toggle, KnownEvents? set, - string minPattern, string mask) : - base(flightConnector, eventRegistrar, eventDispatcher, active, standby, batteryVariable, avionicsVariable, toggle, set, minPattern, mask) + KnownEvents? toggle, KnownEvents? set) : + base( + flightConnector, eventRegistrar, eventDispatcher, active, standby, batteryVariable, avionicsVariable, toggle, set, + "0100", + "" + ) { } + protected override string AddDefaultPattern(string value) + { + // no-op + return value; + } + /// /// Convert from default MHz to kHz /// diff --git a/FlightStreamDeck.Logics/Actions/NavCom/NavComHandler.cs b/FlightStreamDeck.Logics/Actions/NavCom/NavComHandler.cs index 871bfa2..3a84df5 100644 --- a/FlightStreamDeck.Logics/Actions/NavCom/NavComHandler.cs +++ b/FlightStreamDeck.Logics/Actions/NavCom/NavComHandler.cs @@ -66,11 +66,7 @@ public void DeRegisterSimValues() public async Task TriggerAsync(string value, bool swap) { - if (value.Length < MinPattern.Length) - { - // Add the default mask - value += MinPattern.Substring(value.Length); - } + value = AddDefaultPattern(value); uint data = FormatValueForSimConnect(value); eventDispatcher.Trigger(set.ToString(), data); @@ -118,6 +114,16 @@ public void SwapFrequencies() } } + protected virtual string AddDefaultPattern(string value) + { + if (value.Length < MinPattern.Length) + { + value += MinPattern.Substring(value.Length); + } + + return value; + } + protected abstract uint FormatValueForSimConnect(string value); protected virtual string FormatValueForDisplay(double value, TOGGLE_VALUE simvar) diff --git a/FlightStreamDeck.Logics/Actions/NavComAction.cs b/FlightStreamDeck.Logics/Actions/NavComAction.cs index 77bc2ab..2c1164f 100644 --- a/FlightStreamDeck.Logics/Actions/NavComAction.cs +++ b/FlightStreamDeck.Logics/Actions/NavComAction.cs @@ -36,7 +36,6 @@ public class NavComAction : BaseAction, EmbedLinkLogic.IAction private const string maxComVal = "136990"; private const string minXpdrVal = "0000"; private const string maxXpdrVal = "7777"; - private const string minAdfVal = "0100"; private readonly RegistrationParameters registration; private readonly ILogger logger; @@ -323,9 +322,7 @@ private void SwitchTo(string? type, TOGGLE_VALUE? batteryVariable, TOGGLE_VALUE? batteryVariable, avionicsVariable, KnownEvents.ADF1_RADIO_SWAP, - KnownEvents.ADF_STBY_SET, - minAdfVal, - "" + KnownEvents.ADF_STBY_SET ); break; case "ADF2": @@ -338,9 +335,7 @@ private void SwitchTo(string? type, TOGGLE_VALUE? batteryVariable, TOGGLE_VALUE? batteryVariable, avionicsVariable, KnownEvents.ADF2_RADIO_SWAP, - KnownEvents.ADF2_STBY_SET, - minAdfVal, - "" + KnownEvents.ADF2_STBY_SET ); break; default: diff --git a/FlightStreamDeck.Logics/IEventRegistrar.cs b/FlightStreamDeck.Logics/IEventRegistrar.cs index 4950203..4975926 100644 --- a/FlightStreamDeck.Logics/IEventRegistrar.cs +++ b/FlightStreamDeck.Logics/IEventRegistrar.cs @@ -1,8 +1,10 @@ -namespace FlightStreamDeck.Logics +using static FlightStreamDeck.Logics.SimEventManager; + +namespace FlightStreamDeck.Logics { public interface IEventRegistrar { - void RegisterEvent(string? eventName); + (EventEnum eventEnum, bool isValid)? RegisterEvent(string? eventName); void ReInitializeEvents(); } } diff --git a/FlightStreamDeck.Logics/SimEventManager.cs b/FlightStreamDeck.Logics/SimEventManager.cs index a30baa7..b0163eb 100644 --- a/FlightStreamDeck.Logics/SimEventManager.cs +++ b/FlightStreamDeck.Logics/SimEventManager.cs @@ -27,14 +27,14 @@ public SimEventManager(ILogger logger, IFlightConnector flightC flightConnector.InvalidEventRegistered += FlightConnector_InvalidEventRegistered; } - public void RegisterEvent(string? eventName) + public (EventEnum eventEnum, bool isValid)? RegisterEvent(string? eventName) { if (IsValid(eventName)) { eventName = NormalizeEventName(eventName); lock (registeredEvents) { - registeredEvents.AddOrUpdate( + return registeredEvents.AddOrUpdate( eventName, addValueFactory: eventName => { @@ -68,6 +68,7 @@ public void RegisterEvent(string? eventName) ); } } + return null; } public void ReInitializeEvents() diff --git a/FlightStreamDeck.LogicsTests/Actions/NavCom/AdfHandlerTests.cs b/FlightStreamDeck.LogicsTests/Actions/NavCom/AdfHandlerTests.cs new file mode 100644 index 0000000..8778556 --- /dev/null +++ b/FlightStreamDeck.LogicsTests/Actions/NavCom/AdfHandlerTests.cs @@ -0,0 +1,67 @@ +using FlightStreamDeck.Logics.Actions.NavCom; + +namespace FlightStreamDeck.Logics.Tests.Actions.NavCom; + +[TestClass] +public class AdfHandlerTests +{ + [TestMethod] + public async Task TestSetAdf1200() + { + Mock mockFlightConnector = new Mock(); + + var eventManager = new SimEventManager( + new LoggerFactory().CreateLogger(), + mockFlightConnector.Object + ); + + var registration = eventManager.RegisterEvent(KnownEvents.ADF_STBY_SET.ToString()); + Assert.IsNotNull(registration); + + var adfHandler = new AdfHandler( + mockFlightConnector.Object, + eventManager, + eventManager, + Core.TOGGLE_VALUE.ADF_ACTIVE_FREQUENCY__1, + Core.TOGGLE_VALUE.ADF_STANDBY_FREQUENCY__1, + null, + null, + KnownEvents.ADF1_RADIO_SWAP, + KnownEvents.ADF_STBY_SET + ); + + await adfHandler.TriggerAsync("1200", false); + + mockFlightConnector.Verify(connector => connector.Trigger(registration.Value.eventEnum, 301989888)); + } + + [TestMethod] + public async Task TestSetAdf120() + { + Mock mockFlightConnector = new Mock(); + + var eventManager = new SimEventManager( + new LoggerFactory().CreateLogger(), + mockFlightConnector.Object + ); + + var registration = eventManager.RegisterEvent(KnownEvents.ADF_STBY_SET.ToString()); + Assert.IsNotNull(registration); + + var adfHandler = new AdfHandler( + mockFlightConnector.Object, + eventManager, + eventManager, + Core.TOGGLE_VALUE.ADF_ACTIVE_FREQUENCY__1, + Core.TOGGLE_VALUE.ADF_STANDBY_FREQUENCY__1, + null, + null, + KnownEvents.ADF1_RADIO_SWAP, + KnownEvents.ADF_STBY_SET + ); + + await adfHandler.TriggerAsync("120", false); + + mockFlightConnector.Verify(connector => connector.Trigger(registration.Value.eventEnum, 18874368)); + } +} diff --git a/FlightStreamDeck.LogicsTests/ComparisonEvaluatorTests.cs b/FlightStreamDeck.LogicsTests/ComparisonEvaluatorTests.cs index 3b92088..a91657c 100644 --- a/FlightStreamDeck.LogicsTests/ComparisonEvaluatorTests.cs +++ b/FlightStreamDeck.LogicsTests/ComparisonEvaluatorTests.cs @@ -1,5 +1,4 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; namespace FlightStreamDeck.Logics.Tests diff --git a/FlightStreamDeck.LogicsTests/EnumConverterTests.cs b/FlightStreamDeck.LogicsTests/EnumConverterTests.cs index f03c7f9..287cbb8 100644 --- a/FlightStreamDeck.LogicsTests/EnumConverterTests.cs +++ b/FlightStreamDeck.LogicsTests/EnumConverterTests.cs @@ -1,5 +1,4 @@ using FlightStreamDeck.Core; -using Microsoft.VisualStudio.TestTools.UnitTesting; namespace FlightStreamDeck.Logics.Tests { diff --git a/FlightStreamDeck.LogicsTests/FlightStreamDeck.LogicsTests.csproj b/FlightStreamDeck.LogicsTests/FlightStreamDeck.LogicsTests.csproj index 74a980b..277bb24 100644 --- a/FlightStreamDeck.LogicsTests/FlightStreamDeck.LogicsTests.csproj +++ b/FlightStreamDeck.LogicsTests/FlightStreamDeck.LogicsTests.csproj @@ -7,6 +7,7 @@ + diff --git a/FlightStreamDeck.LogicsTests/GlobalImports.cs b/FlightStreamDeck.LogicsTests/GlobalImports.cs new file mode 100644 index 0000000..17cd01c --- /dev/null +++ b/FlightStreamDeck.LogicsTests/GlobalImports.cs @@ -0,0 +1,4 @@ +global using Microsoft.Extensions.Logging; +global using Microsoft.VisualStudio.TestTools.UnitTesting; +global using Moq; +global using System.Threading.Tasks; diff --git a/FlightStreamDeck.LogicsTests/ImageLogicTests.cs b/FlightStreamDeck.LogicsTests/ImageLogicTests.cs index 0f20dd8..137a1cb 100644 --- a/FlightStreamDeck.LogicsTests/ImageLogicTests.cs +++ b/FlightStreamDeck.LogicsTests/ImageLogicTests.cs @@ -1,13 +1,12 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; +using System; using System.IO; namespace FlightStreamDeck.Logics.Tests { - [TestClass()] + [TestClass] public class ImageLogicTests { - [TestMethod()] + [TestMethod] public void GetHorizonImageTest() { ImageLogic images = new ImageLogic(); @@ -28,7 +27,7 @@ public void GetHorizonImageTest() } } - [TestMethod()] + [TestMethod] public void GetHorizonImageBoundaryTest() { ImageLogic images = new ImageLogic(); @@ -43,7 +42,7 @@ public void GetHorizonImageBoundaryTest() images.GetHorizonImage(10, -20, -3000); } - [TestMethod()] + [TestMethod] public void GetGaugeImageTest() { ImageLogic images = new ImageLogic(); @@ -64,7 +63,7 @@ public void GetGaugeImageTest() } } - [TestMethod()] + [TestMethod] public void GetCustomImageTest_NoCoordinateRangeError() { ImageLogic images = new ImageLogic();