From a155e27eacf3b1d847c48e68422e892b84fc6434 Mon Sep 17 00:00:00 2001 From: Colin Date: Mon, 21 Oct 2019 21:16:09 +0100 Subject: [PATCH] Upgraded the Profile code to be contained inside a wrapper and started adding unit tests to cover. --- Meade.net.Telescope/Telescope.cs | 9 +- .../SharedResourcesUnitTests.cs | 65 ++++++++ Meade.net.focuser/Focuser.cs | 9 +- Meade.net/Meade.net.csproj | 2 + Meade.net/ProfileFactory.cs | 18 +++ Meade.net/SharedResources.cs | 14 +- Meade.net/Wrapper/IProfileWrapper.cs | 139 ++++++++++++++++++ 7 files changed, 251 insertions(+), 5 deletions(-) create mode 100644 Meade.net/ProfileFactory.cs create mode 100644 Meade.net/Wrapper/IProfileWrapper.cs diff --git a/Meade.net.Telescope/Telescope.cs b/Meade.net.Telescope/Telescope.cs index e89649b..7dab9c8 100644 --- a/Meade.net.Telescope/Telescope.cs +++ b/Meade.net.Telescope/Telescope.cs @@ -2289,6 +2289,13 @@ namespace ASCOM.Meade.net #region ASCOM Registration + private static IProfileFactory _profileFactory; + public static IProfileFactory ProfileFactory + { + get => _profileFactory ?? (_profileFactory = new ProfileFactory()); + set => _profileFactory = value; + } + // Register or unregister driver for ASCOM. This is harmless if already // registered or unregistered. // @@ -2299,7 +2306,7 @@ namespace ASCOM.Meade.net /// If true, registers the driver, otherwise unregisters it. private static void RegUnregAscom(bool bRegister) { - using (var p = new Profile()) + using (IProfileWrapper p = ProfileFactory.Create()) { p.DeviceType = "Telescope"; if (bRegister) diff --git a/Meade.net.UnitTests/SharedResourcesUnitTests.cs b/Meade.net.UnitTests/SharedResourcesUnitTests.cs index 05890d2..a7a81c8 100644 --- a/Meade.net.UnitTests/SharedResourcesUnitTests.cs +++ b/Meade.net.UnitTests/SharedResourcesUnitTests.cs @@ -1,5 +1,7 @@  +using System.Globalization; using ASCOM.Meade.net; +using ASCOM.Meade.net.Wrapper; using ASCOM.Utilities.Interfaces; using Moq; using NUnit.Framework; @@ -90,5 +92,68 @@ namespace Meade.net.UnitTests _serialMock.Verify(x => x.ReceiveCounted(numberOfCharacters), Times.Once); } + + [Test] + public void WriteProfile_WhenCalled_WritesExpectedProfileSettings() + { + string DriverId = "ASCOM.MeadeGeneric.Telescope"; + + Mock profileWrapperMock = new Mock(); + profileWrapperMock.SetupAllProperties(); + + IProfileWrapper profeWrapper = profileWrapperMock.Object; + + Mock profileFactoryMock = new Mock(); + profileFactoryMock.Setup(x => x.Create()).Returns(profileWrapperMock.Object); + + SharedResources.ProfileFactory = profileFactoryMock.Object; + + var profileProperties = new ProfileProperties(); + profileProperties.TraceLogger = false; + profileProperties.ComPort = "TestComPort"; + + + SharedResources.WriteProfile(profileProperties); + + Assert.That(profeWrapper.DeviceType, Is.EqualTo("Telescope")); + profileWrapperMock.Verify( x => x.WriteValue(DriverId, "Trace Level", profileProperties.TraceLogger.ToString()), Times.Once); + profileWrapperMock.Verify(x => x.WriteValue(DriverId, "COM Port", profileProperties.ComPort), Times.Once); + profileWrapperMock.Verify(x => x.WriteValue(DriverId, "Guide Rate Arc Seconds Per Second", profileProperties.GuideRateArcSecondsPerSecond.ToString(CultureInfo.CurrentCulture)), Times.Once); + profileWrapperMock.Verify(x => x.WriteValue(DriverId, "Precision", profileProperties.Precision), Times.Once); + } + + [Test] + public void ReadProfile_WhenCalled_ReturnsExpectedDefaultValues() + { + string DriverId = "ASCOM.MeadeGeneric.Telescope"; + + string ComPortDefault = "COM1"; + string TraceStateDefault = "false"; + string GuideRateProfileNameDefault = "10.077939"; //67% of sidereal rate + string PrecisionDefault = "Unchanged"; + + Mock profileWrapperMock = new Mock(); + profileWrapperMock.SetupAllProperties(); + + profileWrapperMock.Setup(x => x.GetValue(DriverId, "Trace Level", string.Empty, TraceStateDefault)).Returns(TraceStateDefault); + profileWrapperMock.Setup(x => x.GetValue(DriverId, "COM Port", string.Empty, ComPortDefault)).Returns(ComPortDefault); + profileWrapperMock.Setup(x => x.GetValue(DriverId, "Guide Rate Arc Seconds Per Second", string.Empty, GuideRateProfileNameDefault)).Returns(GuideRateProfileNameDefault); + profileWrapperMock.Setup(x => x.GetValue(DriverId, "Precision", string.Empty, PrecisionDefault)).Returns(PrecisionDefault); + + IProfileWrapper profeWrapper = profileWrapperMock.Object; + + Mock profileFactoryMock = new Mock(); + profileFactoryMock.Setup(x => x.Create()).Returns(profileWrapperMock.Object); + + SharedResources.ProfileFactory = profileFactoryMock.Object; + + var profileProperties = SharedResources.ReadProfile(); + + Assert.That(profeWrapper.DeviceType, Is.EqualTo("Telescope")); + Assert.That(profileProperties.ComPort, Is.EqualTo(ComPortDefault)); + Assert.That(profileProperties.GuideRateArcSecondsPerSecond, Is.EqualTo(double.Parse(GuideRateProfileNameDefault))); + Assert.That(profileProperties.TraceLogger, Is.EqualTo(bool.Parse(TraceStateDefault))); + Assert.That(profileProperties.Precision, Is.EqualTo(PrecisionDefault)); + } } } diff --git a/Meade.net.focuser/Focuser.cs b/Meade.net.focuser/Focuser.cs index fbf09ec..51787bf 100644 --- a/Meade.net.focuser/Focuser.cs +++ b/Meade.net.focuser/Focuser.cs @@ -451,6 +451,13 @@ namespace ASCOM.Meade.net #region ASCOM Registration + private static IProfileFactory _profileFactory; + public static IProfileFactory ProfileFactory + { + get => _profileFactory ?? (_profileFactory = new ProfileFactory()); + set => _profileFactory = value; + } + // Register or unregister driver for ASCOM. This is harmless if already // registered or unregistered. // @@ -461,7 +468,7 @@ namespace ASCOM.Meade.net /// If true, registers the driver, otherwise unregisters it. private static void RegUnregAscom(bool bRegister) { - using (var p = new Profile()) + using (IProfileWrapper p = ProfileFactory.Create()) { p.DeviceType = "Focuser"; if (bRegister) diff --git a/Meade.net/Meade.net.csproj b/Meade.net/Meade.net.csproj index 1e87465..dbc67d3 100644 --- a/Meade.net/Meade.net.csproj +++ b/Meade.net/Meade.net.csproj @@ -134,10 +134,12 @@ + + Designer diff --git a/Meade.net/ProfileFactory.cs b/Meade.net/ProfileFactory.cs new file mode 100644 index 0000000..b80935f --- /dev/null +++ b/Meade.net/ProfileFactory.cs @@ -0,0 +1,18 @@ +using ASCOM.Meade.net.Wrapper; +using ASCOM.Utilities; + +namespace ASCOM.Meade.net +{ + public interface IProfileFactory + { + IProfileWrapper Create(); + } + + public class ProfileFactory : IProfileFactory + { + public IProfileWrapper Create() + { + return new ProfileWrapper(); + } + } +} diff --git a/Meade.net/SharedResources.cs b/Meade.net/SharedResources.cs index 2eb5f19..21ae49c 100644 --- a/Meade.net/SharedResources.cs +++ b/Meade.net/SharedResources.cs @@ -18,6 +18,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Windows.Forms; +using ASCOM.Meade.net.Wrapper; using ASCOM.Utilities; using ASCOM.Utilities.Interfaces; @@ -68,6 +69,12 @@ namespace ASCOM.Meade.net set => _sSharedSerial = value; } + public static IProfileFactory ProfileFactory + { + get => _profileFactory ?? ( _profileFactory = new ProfileFactory()); + set => _profileFactory = value; + } + public static void SendBlind(string message) { lock (LockObject) @@ -132,12 +139,12 @@ namespace ASCOM.Meade.net private const string TraceStateProfileName = "Trace Level"; private const string GuideRateProfileName = "Guide Rate Arc Seconds Per Second"; private const string PrecisionProfileName = "Precision"; - + public static void WriteProfile(ProfileProperties profileProperties) { lock (LockObject) { - using (Profile driverProfile = new Profile()) + using (IProfileWrapper driverProfile = ProfileFactory.Create()) { driverProfile.DeviceType = "Telescope"; driverProfile.WriteValue(DriverId, TraceStateProfileName, profileProperties.TraceLogger.ToString()); @@ -158,7 +165,7 @@ namespace ASCOM.Meade.net lock (LockObject) { ProfileProperties profileProperties = new ProfileProperties(); - using (Profile driverProfile = new Profile()) + using (IProfileWrapper driverProfile = ProfileFactory.Create()) { driverProfile.DeviceType = "Telescope"; profileProperties.ComPort = driverProfile.GetValue(DriverId, ComPortProfileName, string.Empty, ComPortDefault); @@ -222,6 +229,7 @@ namespace ASCOM.Meade.net private static readonly Dictionary ConnectedDevices = new Dictionary(); private static readonly Dictionary ConnectedDeviceIds = new Dictionary(); + private static IProfileFactory _profileFactory ; /// diff --git a/Meade.net/Wrapper/IProfileWrapper.cs b/Meade.net/Wrapper/IProfileWrapper.cs new file mode 100644 index 0000000..f9b274a --- /dev/null +++ b/Meade.net/Wrapper/IProfileWrapper.cs @@ -0,0 +1,139 @@ +using System; +using System.Collections; +using ASCOM.Utilities; +using ASCOM.Utilities.Interfaces; + +namespace ASCOM.Meade.net.Wrapper +{ + public interface IProfileWrapper : IProfile, IProfileExtra, IDisposable + { + + } + + public class ProfileWrapper : IProfileWrapper + { + private readonly Profile _profile = new Profile(); + + public ArrayList RegisteredDevices(string DeviceType) + { + return _profile.RegisteredDevices(DeviceType); + } + + public bool IsRegistered(string DriverID) + { + return _profile.IsRegistered(DriverID); + } + + public void Register(string DriverID, string DescriptiveName) + { + _profile.Register(DriverID, DescriptiveName); + } + + public void Unregister(string DriverID) + { + _profile.Unregister(DriverID); + } + + public string GetValue(string DriverID, string Name, string SubKey, string DefaultValue) + { + return _profile.GetValue(DriverID, Name, SubKey, DefaultValue); + } + + public void WriteValue(string DriverID, string Name, string Value, string SubKey) + { + _profile.WriteValue(DriverID, Name, Value); + } + + public ArrayList Values(string DriverID, string SubKey) + { + return _profile.Values(DriverID, SubKey); + } + + public void DeleteValue(string DriverID, string Name, string SubKey) + { + _profile.DeleteValue(DriverID, Name, SubKey); + } + + public void CreateSubKey(string DriverID, string SubKey) + { + _profile.CreateSubKey(DriverID, SubKey); + } + + public ArrayList SubKeys(string DriverID, string SubKey) + { + return _profile.SubKeys(DriverID, SubKey); + } + + public void DeleteSubKey(string DriverID, string SubKey) + { + _profile.DeleteSubKey(DriverID, SubKey); + } + + public string GetProfileXML(string deviceId) + { + return _profile.GetProfileXML(deviceId); + } + + public void SetProfileXML(string deviceId, string xml) + { + _profile.SetProfileXML(deviceId, xml); + } + + public string DeviceType + { + get => _profile.DeviceType; + set => _profile.DeviceType = value; + } + public ArrayList RegisteredDeviceTypes => _profile.RegisteredDeviceTypes; + + public void MigrateProfile(string CurrentPlatformVersion) + { + _profile.MigrateProfile(CurrentPlatformVersion); + } + + public void DeleteValue(string DriverID, string Name) + { + _profile.DeleteValue(DriverID, Name); + } + + public string GetValue(string DriverID, string Name) + { + return _profile.GetValue(DriverID, Name); + } + + public string GetValue(string DriverID, string Name, string SubKey) + { + return _profile.GetValue(DriverID, Name, SubKey); + } + + public ArrayList SubKeys(string DriverID) + { + return _profile.SubKeys(DriverID); + } + + public ArrayList Values(string DriverID) + { + return _profile.Values(DriverID); + } + + public void WriteValue(string DriverID, string Name, string Value) + { + _profile.WriteValue(DriverID, Name, Value); + } + + public ASCOMProfile GetProfile(string DriverId) + { + return _profile.GetProfile(DriverId); + } + + public void SetProfile(string DriverId, ASCOMProfile XmlProfileKey) + { + _profile.SetProfile(DriverId, XmlProfileKey); + } + + public void Dispose() + { + _profile.Dispose(); + } + } +}