121 lines
3.5 KiB
C#
121 lines
3.5 KiB
C#
using Moq;
|
|
using Prism.Events;
|
|
using Xunit;
|
|
using XP.Common.Logging.Interfaces;
|
|
using XP.Hardware.RaySource.Abstractions.Enums;
|
|
using XP.Hardware.RaySource.Abstractions.Events;
|
|
using XP.Hardware.RaySource.Implementations;
|
|
|
|
namespace XplorePlane.Tests.Hardware
|
|
{
|
|
public class SimulatedXRaySourceTests
|
|
{
|
|
private readonly Mock<IEventAggregator> _mockEventAggregator;
|
|
private readonly Mock<ILoggerService> _mockLogger;
|
|
private readonly Mock<RaySourceStatusChangedEvent> _mockStatusEvent;
|
|
private readonly SimulatedXRaySource _source;
|
|
|
|
public SimulatedXRaySourceTests()
|
|
{
|
|
_mockEventAggregator = new Mock<IEventAggregator>();
|
|
_mockLogger = new Mock<ILoggerService>();
|
|
_mockStatusEvent = new Mock<RaySourceStatusChangedEvent>();
|
|
|
|
// Setup logger to return itself for ForModule<T>()
|
|
_mockLogger.Setup(l => l.ForModule<SimulatedXRaySource>()).Returns(_mockLogger.Object);
|
|
|
|
// Setup event aggregator to return the mock event
|
|
_mockEventAggregator
|
|
.Setup(ea => ea.GetEvent<RaySourceStatusChangedEvent>())
|
|
.Returns(_mockStatusEvent.Object);
|
|
|
|
_source = new SimulatedXRaySource(_mockEventAggregator.Object, _mockLogger.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public void Initialize_SetsInitializedTrue()
|
|
{
|
|
// Act
|
|
var initResult = _source.Initialize();
|
|
var connectResult = _source.ConnectVariables();
|
|
|
|
// Assert
|
|
Assert.True(initResult.Success);
|
|
Assert.True(connectResult.Success);
|
|
Assert.True(_source.IsConnected);
|
|
}
|
|
|
|
[Fact]
|
|
public void TurnOn_PublishesOpenedEvent()
|
|
{
|
|
// Arrange
|
|
_source.Initialize();
|
|
_source.ConnectVariables();
|
|
|
|
// Act
|
|
var result = _source.TurnOn();
|
|
|
|
// Assert
|
|
Assert.True(result.Success);
|
|
_mockStatusEvent.Verify(
|
|
e => e.Publish(RaySourceStatus.Opened),
|
|
Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public void TurnOff_PublishesClosedEvent()
|
|
{
|
|
// Arrange
|
|
_source.Initialize();
|
|
_source.ConnectVariables();
|
|
_source.TurnOn();
|
|
|
|
// Act
|
|
var result = _source.TurnOff();
|
|
|
|
// Assert
|
|
Assert.True(result.Success);
|
|
_mockStatusEvent.Verify(
|
|
e => e.Publish(RaySourceStatus.Closed),
|
|
Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadVoltage_ReturnsWithinTwoPercentOfSetValue()
|
|
{
|
|
// Arrange
|
|
_source.Initialize();
|
|
_source.ConnectVariables();
|
|
_source.SetVoltage(100f);
|
|
|
|
// Act & Assert - read multiple times to verify range
|
|
for (int i = 0; i < 20; i++)
|
|
{
|
|
var result = _source.ReadVoltage();
|
|
Assert.True(result.Success);
|
|
|
|
float voltage = result.GetFloat();
|
|
Assert.InRange(voltage, 98f, 102f);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void CloseOff_ResetsState()
|
|
{
|
|
// Arrange
|
|
_source.Initialize();
|
|
_source.ConnectVariables();
|
|
_source.TurnOn();
|
|
|
|
Assert.True(_source.IsConnected);
|
|
|
|
// Act
|
|
var result = _source.CloseOff();
|
|
|
|
// Assert
|
|
Assert.True(result.Success);
|
|
Assert.False(_source.IsConnected);
|
|
}
|
|
}
|
|
}
|