Skip to content

Commit

Permalink
Merge pull request #60 from David032/57-hardware-services-tests
Browse files Browse the repository at this point in the history
Adding unit tests for Hardware services
  • Loading branch information
David032 authored Apr 5, 2024
2 parents 766ef4f + 0cd1e0d commit 55eafb9
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 10 deletions.
11 changes: 11 additions & 0 deletions ServoSkull.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "skullOS.HardwareServices",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "skullOS.Modules", "Source\skullOS.Modules\skullOS.Modules.csproj", "{A087C866-BC7F-4053-81F8-16BB634266D5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HardwareTests", "Source\Tests\HardwareTests\HardwareTests.csproj", "{B31034F4-34B1-4435-A984-2C087642436E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -112,6 +114,14 @@ Global
{A087C866-BC7F-4053-81F8-16BB634266D5}.Release|Any CPU.Build.0 = Release|Any CPU
{A087C866-BC7F-4053-81F8-16BB634266D5}.Release|ARM64.ActiveCfg = Release|Any CPU
{A087C866-BC7F-4053-81F8-16BB634266D5}.Release|ARM64.Build.0 = Release|Any CPU
{B31034F4-34B1-4435-A984-2C087642436E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B31034F4-34B1-4435-A984-2C087642436E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B31034F4-34B1-4435-A984-2C087642436E}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{B31034F4-34B1-4435-A984-2C087642436E}.Debug|ARM64.Build.0 = Debug|Any CPU
{B31034F4-34B1-4435-A984-2C087642436E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B31034F4-34B1-4435-A984-2C087642436E}.Release|Any CPU.Build.0 = Release|Any CPU
{B31034F4-34B1-4435-A984-2C087642436E}.Release|ARM64.ActiveCfg = Release|Any CPU
{B31034F4-34B1-4435-A984-2C087642436E}.Release|ARM64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -121,6 +131,7 @@ Global
{A3FCF3F9-2DB8-4F06-9BB7-91FCB1601250} = {B50B951E-DF9B-49DD-9CAC-ED8B01C6B1E7}
{80D9CD23-02F9-4E7D-B1E8-7F62FFA0162B} = {B50B951E-DF9B-49DD-9CAC-ED8B01C6B1E7}
{BDA3C9A7-E2A3-42C9-A553-BA5BD0385939} = {B50B951E-DF9B-49DD-9CAC-ED8B01C6B1E7}
{B31034F4-34B1-4435-A984-2C087642436E} = {B50B951E-DF9B-49DD-9CAC-ED8B01C6B1E7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FA7BE016-5699-47F3-A654-08A907CE1F11}
Expand Down
21 changes: 21 additions & 0 deletions Source/Tests/HardwareTests/BuzzerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Iot.Device.Buzzer;
using Moq;
using skullOS.HardwareServices;

namespace HardwareTests
{
public class BuzzerTest : IHardwareServiceTest
{
BuzzerService sut;

[Fact(Skip = "Buzzer hard creates gpio driver")]
public void CanCreate()
{
Mock<Buzzer> mockedBuzzer = new Mock<Buzzer>([0]);

sut = new BuzzerService(0, mockedBuzzer.Object);

Assert.NotNull(sut);
}
}
}
17 changes: 17 additions & 0 deletions Source/Tests/HardwareTests/CameraTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using skullOS.HardwareServices;

namespace HardwareTests
{
public class CameraTest : IHardwareServiceTest
{
CameraService sut;

[Fact]
public void CanCreate()
{
CameraService service = new CameraService();

Assert.NotNull(service);
}
}
}
1 change: 1 addition & 0 deletions Source/Tests/HardwareTests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
31 changes: 31 additions & 0 deletions Source/Tests/HardwareTests/HardwareTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\skullOS.HardwareServices\skullOS.HardwareServices.csproj" />
<ProjectReference Include="..\skullOS.Tests\skullOS.Tests.csproj" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions Source/Tests/HardwareTests/LEDTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Moq;
using skullOS.HardwareServices;
using skullOS.Tests;
using System.Device.Gpio;

namespace HardwareTests
{
public class LEDTest : IHardwareServiceTest
{
[Fact]
public void CanCreate()
{
Mock<MockableGpioDriver> gpioMock = new();
var ctrlr = new GpioController(PinNumberingScheme.Logical, gpioMock.Object);
Dictionary<string, int> testPins = new()
{
{ "An Led", 6 },
{"Another led", 26 }
};
LedService sut = new LedService(testPins, ctrlr);

Assert.NotNull(sut);
}
}
}
20 changes: 20 additions & 0 deletions Source/Tests/HardwareTests/MicrophoneTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Iot.Device.Media;
using Moq;
using skullOS.HardwareServices;

namespace HardwareTests
{
public class MicrophoneTest : IHardwareServiceTest
{
[Fact]
public void CanCreate()
{
Mock<SoundDevice> SoundDevice = new Mock<SoundDevice>();
Mock<SoundConnectionSettings> SoundConnectionSettings = new Mock<SoundConnectionSettings>();

var sut = new MicrophoneService(mic: SoundDevice.Object);

Assert.NotNull(sut);
}
}
}
21 changes: 21 additions & 0 deletions Source/Tests/HardwareTests/ProgrammableLedTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Iot.Device.Ws28xx;
using Moq;
using skullOS.HardwareServices;
using skullOS.Tests;

namespace HardwareTests
{
public class ProgrammableLedTest : IHardwareServiceTest
{
[Fact(Skip = "SPI issue")]
public void CanCreate()
{
Mock<MockableSpiDevice> spiDevice = new Mock<MockableSpiDevice>();
Mock<Ws2812b> mockLed = new Mock<Ws2812b>();

var sut = new ProgrammableLedService(spiDevice.Object, 9, mockLed.Object);

Assert.NotNull(sut);
}
}
}
15 changes: 15 additions & 0 deletions Source/Tests/HardwareTests/SpeakerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using skullOS.HardwareServices;

namespace HardwareTests
{
public class SpeakerTest : IHardwareServiceTest
{
[Fact]
public void CanCreate()
{
var sut = new SpeakerService();

Assert.NotNull(sut);
}
}
}
7 changes: 7 additions & 0 deletions Source/Tests/skullOS.Tests/IHardwareServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace HardwareTests
{
public interface IHardwareServiceTest
{
void CanCreate();
}
}
55 changes: 55 additions & 0 deletions Source/Tests/skullOS.Tests/MockableSpiDevice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Device.Spi;

namespace skullOS.Tests
{
//Taken from: https://github.com/dotnet/iot/blob/main/src/devices/Board/tests/SpiDummyDevice.cs
public class MockableSpiDevice : SpiDevice
{
private bool _disposed;
public MockableSpiDevice(SpiConnectionSettings connectionSettings, int[] pins)
{
ConnectionSettings = connectionSettings;
Pins = pins;
_disposed = false;
}

public override SpiConnectionSettings ConnectionSettings { get; }
public int[] Pins { get; }

public override byte ReadByte()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(MockableSpiDevice));
}

return 0xF8;
}

public override void Read(Span<byte> buffer)
{
throw new NotImplementedException();
}

public override void WriteByte(byte value)
{
throw new NotImplementedException();
}

public override void Write(ReadOnlySpan<byte> buffer)
{
throw new NotImplementedException();
}

public override void TransferFullDuplex(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer)
{
throw new NotImplementedException();
}

protected override void Dispose(bool disposing)
{
_disposed = true;
base.Dispose(disposing);
}
}
}
11 changes: 9 additions & 2 deletions Source/skullOS.HardwareServices/BuzzerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ namespace skullOS.HardwareServices
public class BuzzerService : IBuzzerService
{
public Buzzer Buzzer { get; private set; }
public BuzzerService(int pinNumber)
public BuzzerService(int pinNumber, Buzzer buzz = null)

Check warning on line 9 in Source/skullOS.HardwareServices/BuzzerService.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
Buzzer = new Buzzer(pinNumber);
if (buzz == null)
{
Buzzer = new Buzzer(pinNumber);
}
else
{
Buzzer = buzz;
}
}

public void SetBuzzer(int pinNumber)
Expand Down
1 change: 0 additions & 1 deletion Source/skullOS.HardwareServices/CameraService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class CameraService : ICameraService

public CameraService(int x = 2592, int y = 1944)

Check warning on line 16 in Source/skullOS.HardwareServices/CameraService.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_processSettings' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_processSettings = ProcessSettingsFactory.CreateForLibcamerastill();
xResolution = x;
yResolution = y;
}
Expand Down
11 changes: 9 additions & 2 deletions Source/skullOS.HardwareServices/LedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ public class LedService : ILedService
public Dictionary<string, int> LEDs { get; private set; }
GpioController controller;

public LedService(Dictionary<string, int> leds)
public LedService(Dictionary<string, int> leds, GpioController controller = null)

Check warning on line 11 in Source/skullOS.HardwareServices/LedService.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
LEDs = leds;
controller = new GpioController();
if (controller == null)
{
this.controller = new GpioController();
}
else
{
this.controller = controller;
}
}

public async void BlinkLight(string light)
Expand Down
14 changes: 11 additions & 3 deletions Source/skullOS.HardwareServices/MicrophoneService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ public class MicrophoneService : IMicrophoneService
{
public SoundDevice Microphone { get; private set; }

public MicrophoneService(SoundConnectionSettings? micSettings = null)
public MicrophoneService(SoundConnectionSettings? micSettings = null, SoundDevice mic = null)

Check warning on line 10 in Source/skullOS.HardwareServices/MicrophoneService.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
micSettings ??= new SoundConnectionSettings();
Microphone = SoundDevice.Create(micSettings); //Can't create a microphone if there's no mic!
if (mic == null)
{
micSettings ??= new SoundConnectionSettings();
Microphone = SoundDevice.Create(micSettings); //Can't create a microphone if there's no mic!
}
else
{
Microphone = mic;
}

}
}
}
11 changes: 9 additions & 2 deletions Source/skullOS.HardwareServices/ProgrammableLedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ public class ProgrammableLedService : IProgrammableLedService
{
public Ws2812b LedArray { get; private set; }

public ProgrammableLedService(SpiDevice spi, int width = 9)
public ProgrammableLedService(SpiDevice spi, int width = 9, Ws2812b LedArray = null)

Check warning on line 11 in Source/skullOS.HardwareServices/ProgrammableLedService.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
LedArray = new Ws2812b(spi, width);
if (LedArray == null)
{
this.LedArray = new Ws2812b(spi, width);
}
else
{
this.LedArray = LedArray;
}
}
}
}

0 comments on commit 55eafb9

Please sign in to comment.