diff --git a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs index e29273f..2749b4d 100644 --- a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs +++ b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs @@ -1,5 +1,5 @@ -using ASCOM; -using ASCOM.Astrometry; +using System; +using ASCOM; using ASCOM.Astrometry.AstroUtils; using ASCOM.Meade.net; using ASCOM.Meade.net.AstroMaths; @@ -34,9 +34,12 @@ namespace Meade.net.Telescope.UnitTests _astroUtilsMock = new Mock(); _sharedResourcesWrapperMock = new Mock(); + _sharedResourcesWrapperMock.Setup(x => x.SendString(":GZ#")).Returns("DDD*MM’SS#"); _sharedResourcesWrapperMock.Setup(x => x.AUTOSTAR497).Returns(() => "AUTOSTAR497"); _sharedResourcesWrapperMock.Setup(x => x.AUTOSTAR497_31EE).Returns(() => "31EE"); + _sharedResourcesWrapperMock.Setup(x => x.Lock(It.IsAny())).Callback(action => { action(); }); + _sharedResourcesWrapperMock.Setup(x => x.ReadProfile()).Returns(_profileProperties); _astroMathsMock = new Mock(); @@ -93,10 +96,12 @@ namespace Meade.net.Telescope.UnitTests { _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); - _telescope.Connected = true; string expectedResult = "test result string"; - _sharedResourcesWrapperMock.Setup(x => x.SendString(It.IsAny())).Returns(expectedResult); + _sharedResourcesWrapperMock.Setup(x => x.SendString(":ED#")).Returns(expectedResult); + _telescope.Connected = true; + + var actualResult = _telescope.Action("handbox", "readdisplay"); @@ -127,6 +132,7 @@ namespace Meade.net.Telescope.UnitTests { _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + _telescope.Connected = true; _telescope.Action("handbox", action); @@ -232,5 +238,64 @@ namespace Meade.net.Telescope.UnitTests _sharedResourcesWrapperMock.Verify(x => x.SendString(sendMessage), Times.Once); Assert.That(actualMessage, Is.EqualTo(expectedMessage)); } + + [TestCase(true)] + [TestCase(false)] + public void Connected_Get_ReturnsExpectedValue(bool expectedConnected) + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + _telescope.Connected = expectedConnected; + + Assert.That(_telescope.Connected, Is.EqualTo(expectedConnected)); + } + + + [Test] + public void Connected_Set_SettingTrueWhenTrue_ThenDoesNothing() + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + _telescope.Connected = true; + _sharedResourcesWrapperMock.Verify( x => x.Connect(It.IsAny()),Times.Once); + + //act + _telescope.Connected = true; + + //assert + _sharedResourcesWrapperMock.Verify(x => x.Connect(It.IsAny()), Times.Once); + } + + [Test] + public void Connected_Set_SettingFalseWhenTrue_ThenDisconnects() + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + _telescope.Connected = true; + _sharedResourcesWrapperMock.Verify(x => x.Connect(It.IsAny()), Times.Once); + + //act + _telescope.Connected = false; + + //assert + _sharedResourcesWrapperMock.Verify(x => x.Disconnect(It.IsAny()), Times.Once()); + } + + [Test] + public void Connected_Set_WhenFailsToConnect_ThenDisconnects() + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + + _sharedResourcesWrapperMock.Setup(x => x.SendString(It.IsAny())).Throws(new Exception("TestFailed")); + + //act + _telescope.Connected = true; + + //assert + _sharedResourcesWrapperMock.Verify(x => x.Disconnect(It.IsAny()), Times.Once()); + } + + } } diff --git a/Meade.net.Telescope/Telescope.cs b/Meade.net.Telescope/Telescope.cs index 433d6d4..e4ea0d3 100644 --- a/Meade.net.Telescope/Telescope.cs +++ b/Meade.net.Telescope/Telescope.cs @@ -65,7 +65,7 @@ namespace ASCOM.Meade.net /// /// Variable to hold the trace logger object (creates a diagnostic log file with information that you specify) /// - internal static TraceLogger tl; + private TraceLogger tl; private readonly ISharedResourcesWrapper _sharedResourcesWrapper; @@ -275,6 +275,9 @@ namespace ASCOM.Meade.net public void Dispose() { + if (Connected) + Connected = false; + // Clean up the tracelogger and util objects tl.Enabled = false; tl.Dispose(); @@ -375,6 +378,11 @@ namespace ASCOM.Meade.net private void SelectSite(int site) { + if (site < 1) + throw new ArgumentOutOfRangeException("site cannot be lower than 1"); + else if (site > 4) + throw new ArgumentOutOfRangeException("site cannot be higher than 4"); + _sharedResourcesWrapper.SendBlind($":W{site}#"); //:W# //Set current site to, an ASCII digit in the range 1..4 @@ -2000,7 +2008,7 @@ namespace ASCOM.Meade.net /// /// /// - internal static void LogMessage(string identifier, string message, params object[] args) + internal void LogMessage(string identifier, string message, params object[] args) { var msg = string.Format(message, args); tl.LogMessage(identifier, msg);