Upgraded the Profile code to be contained inside a wrapper and started adding unit tests to cover.

This commit is contained in:
2019-10-21 21:16:09 +01:00
parent 438ec779b4
commit a155e27eac
7 changed files with 251 additions and 5 deletions
+8 -1
View File
@@ -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
/// <param name="bRegister">If <c>true</c>, registers the driver, otherwise unregisters it.</param>
private static void RegUnregAscom(bool bRegister)
{
using (var p = new Profile())
using (IProfileWrapper p = ProfileFactory.Create())
{
p.DeviceType = "Telescope";
if (bRegister)
@@ -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<IProfileWrapper> profileWrapperMock = new Mock<IProfileWrapper>();
profileWrapperMock.SetupAllProperties();
IProfileWrapper profeWrapper = profileWrapperMock.Object;
Mock<IProfileFactory> profileFactoryMock = new Mock<IProfileFactory>();
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<IProfileWrapper> profileWrapperMock = new Mock<IProfileWrapper>();
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<IProfileFactory> profileFactoryMock = new Mock<IProfileFactory>();
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));
}
}
}
+8 -1
View File
@@ -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
/// <param name="bRegister">If <c>true</c>, registers the driver, otherwise unregisters it.</param>
private static void RegUnregAscom(bool bRegister)
{
using (var p = new Profile())
using (IProfileWrapper p = ProfileFactory.Create())
{
p.DeviceType = "Focuser";
if (bRegister)
+2
View File
@@ -134,10 +134,12 @@
</Compile>
<Compile Include="GarbageCollection.cs" />
<Compile Include="LocalServer.cs" />
<Compile Include="ProfileFactory.cs" />
<Compile Include="ProfileProperties.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TelescopeList.cs" />
<Compile Include="Win32Utilities.cs" />
<Compile Include="Wrapper\IProfileWrapper.cs" />
<Compile Include="Wrapper\SharedResourcesWrapper.cs" />
<EmbeddedResource Include="frmMain.resx">
<SubType>Designer</SubType>
+18
View File
@@ -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();
}
}
}
+11 -3
View File
@@ -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<string, DeviceHardware> ConnectedDevices = new Dictionary<string, DeviceHardware>();
private static readonly Dictionary<string, DeviceHardware> ConnectedDeviceIds = new Dictionary<string, DeviceHardware>();
private static IProfileFactory _profileFactory ;
/// <summary>
+139
View File
@@ -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();
}
}
}