Skip to content

Commit 87e0b20

Browse files
joperezrbuyaa-n
authored andcommitted
Adding a Xunit project for running tests for System.Device.Gpio library. (#151)
1 parent d594ce9 commit 87e0b20

File tree

8 files changed

+254
-4
lines changed

8 files changed

+254
-4
lines changed

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<PublishWindowsPdb>false</PublishWindowsPdb>
66
<ImportNetSdkFromRepoToolset>false</ImportNetSdkFromRepoToolset>
77
<Language>C#</Language>
8-
<TargetsWindows Condition="'$(RuntimeIdentifier)' == 'win'">true</TargetsWindows>
9-
<TargetsLinux Condition="'$(RuntimeIdentifier)' == 'linux'">true</TargetsLinux>
8+
<TargetsWindows Condition="$(RuntimeIdentifier.StartsWith('win'))">true</TargetsWindows>
9+
<TargetsLinux Condition="$(RuntimeIdentifier.StartsWith('linux'))">true</TargetsLinux>
1010
<LangVersion>Latest</LangVersion>
1111
</PropertyGroup>
1212

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project>
2+
<!--Project Configurations properties for VS-->
3+
<PropertyGroup>
4+
<RuntimeIdentifier Condition="'$(Configuration)' == 'Windows-Debug'">win-arm</RuntimeIdentifier>
5+
<RuntimeIdentifier Condition="'$(Configuration)' == 'Linux-Debug'">linux-arm</RuntimeIdentifier>
6+
</PropertyGroup>
7+
8+
<Import Project="..\..\Directory.Build.props" />
9+
</Project>
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics;
6+
using System.Threading;
7+
using Xunit;
8+
[assembly: CollectionBehavior(DisableTestParallelization = true)]
9+
10+
namespace System.Device.Gpio.Tests
11+
{
12+
public abstract class GpioControllerTestBase
13+
{
14+
private const int LedPin = 18;
15+
private const int OutputPin = 16;
16+
private const int InputPin = 12;
17+
18+
[Fact]
19+
public void ControllerCanTurnOnLEDs()
20+
{
21+
using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
22+
{
23+
controller.OpenPin(LedPin, PinMode.Output);
24+
Thread.Sleep(1_000);
25+
controller.Write(LedPin, PinValue.High);
26+
Thread.Sleep(TimeSpan.FromSeconds(1));
27+
controller.Write(LedPin, PinValue.Low);
28+
}
29+
}
30+
31+
[Fact]
32+
public void PinValueReturnsToLowAfterDispose()
33+
{
34+
using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
35+
{
36+
controller.OpenPin(OutputPin, PinMode.Output);
37+
controller.OpenPin(InputPin, PinMode.Input);
38+
controller.Write(OutputPin, PinValue.High);
39+
Thread.SpinWait(100);
40+
Assert.Equal(PinValue.High, controller.Read(InputPin));
41+
}
42+
43+
using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
44+
{
45+
controller.OpenPin(OutputPin, PinMode.Output);
46+
controller.OpenPin(InputPin, PinMode.Input);
47+
Assert.Equal(PinValue.Low, controller.Read(InputPin));
48+
}
49+
}
50+
51+
[Fact]
52+
public void PinValueReadAndWrite()
53+
{
54+
using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
55+
{
56+
controller.OpenPin(OutputPin, PinMode.Output);
57+
controller.OpenPin(InputPin, PinMode.Input);
58+
controller.Write(OutputPin, PinValue.High);
59+
Thread.SpinWait(100);
60+
Assert.Equal(PinValue.High, controller.Read(InputPin));
61+
controller.Write(OutputPin, PinValue.Low);
62+
Thread.SpinWait(100);
63+
Assert.Equal(PinValue.Low, controller.Read(InputPin));
64+
controller.Write(OutputPin, PinValue.High);
65+
Thread.SpinWait(100);
66+
Assert.Equal(PinValue.High, controller.Read(InputPin));
67+
}
68+
}
69+
70+
[Fact]
71+
public void ThrowsInvalidOperationExceptionWhenPinIsNotOpened()
72+
{
73+
using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
74+
{
75+
Assert.Throws<InvalidOperationException>(() => controller.Write(OutputPin, PinValue.High));
76+
Assert.Throws<InvalidOperationException>(() => controller.Read(InputPin));
77+
Assert.Throws<InvalidOperationException>(() => controller.ClosePin(OutputPin));
78+
Assert.Throws<InvalidOperationException>(() => controller.SetPinMode(OutputPin, PinMode.Output));
79+
Assert.Throws<InvalidOperationException>(() => controller.GetPinMode(OutputPin));
80+
}
81+
}
82+
83+
[Fact]
84+
public void IsPinOpenTest()
85+
{
86+
using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
87+
{
88+
Assert.False(controller.IsPinOpen(LedPin));
89+
controller.OpenPin(LedPin);
90+
Assert.True(controller.IsPinOpen(LedPin));
91+
controller.ClosePin(LedPin);
92+
Assert.False(controller.IsPinOpen(LedPin));
93+
}
94+
}
95+
96+
[Fact]
97+
public void ThrowsIfWritingOnInputPin()
98+
{
99+
using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
100+
{
101+
controller.OpenPin(InputPin, PinMode.Input);
102+
Assert.Throws<InvalidOperationException>(() => controller.Write(InputPin, PinValue.High));
103+
}
104+
}
105+
106+
[Fact]
107+
public void ThrowsIfReadingFromOutputPin()
108+
{
109+
using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
110+
{
111+
controller.OpenPin(OutputPin, PinMode.Output);
112+
Assert.Throws<InvalidOperationException>(() => controller.Read(OutputPin));
113+
}
114+
}
115+
116+
[Fact]
117+
public void OpenPinDefaultsModeToInput()
118+
{
119+
using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
120+
{
121+
controller.OpenPin(OutputPin);
122+
Assert.Equal(PinMode.Input, controller.GetPinMode(OutputPin));
123+
controller.SetPinMode(OutputPin, PinMode.Output);
124+
controller.ClosePin(OutputPin);
125+
controller.OpenPin(OutputPin);
126+
Assert.Equal(PinMode.Input, controller.GetPinMode(OutputPin));
127+
}
128+
}
129+
130+
[Fact]
131+
public void AddCallbackTest()
132+
{
133+
while (!Debugger.IsAttached)
134+
Thread.Sleep(1000);
135+
Debugger.Break();
136+
137+
ManualResetEvent mre = new ManualResetEvent(false);
138+
bool wasCalled = false;
139+
using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver()))
140+
{
141+
controller.OpenPin(InputPin, PinMode.Input);
142+
controller.OpenPin(OutputPin, PinMode.Output);
143+
controller.RegisterCallbackForPinValueChangedEvent(InputPin, PinEventTypes.Rising, callback);
144+
controller.Write(OutputPin, PinValue.High);
145+
mre.WaitOne(TimeSpan.FromSeconds(5));
146+
Assert.True(wasCalled);
147+
}
148+
149+
void callback(object sender, PinValueChangedEventArgs pinValueChangedEventArgs)
150+
{
151+
wasCalled = true;
152+
mre.Set();
153+
}
154+
}
155+
156+
protected abstract GpioDriver GetTestDriver();
157+
protected abstract PinNumberingScheme GetTestNumberingScheme();
158+
}
159+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Device.Gpio.Drivers;
6+
using Xunit;
7+
8+
namespace System.Device.Gpio.Tests
9+
{
10+
public class RaspberryPiDriverTests : GpioControllerTestBase
11+
{
12+
protected override GpioDriver GetTestDriver() => new RaspberryPi3Driver();
13+
14+
protected override PinNumberingScheme GetTestNumberingScheme() => PinNumberingScheme.Logical;
15+
}
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
<RuntimeIdentifiers>win-arm;linux-arm</RuntimeIdentifiers>
6+
<IsPackable>false</IsPackable>
7+
<Configurations>Debug;Release;Windows-Debug;Linux-Debug</Configurations>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="$(MSBuildThisFileDirectory)../System.Device.Gpio/System.Device.Gpio.csproj">
12+
<AdditionalProperties Condition="'$(RuntimeIdentifier)' != ''">RuntimeIdentifier=$(RuntimeIdentifier.SubString(0, $(RuntimeIdentifier.IndexOf('-'))))</AdditionalProperties>
13+
</ProjectReference>
14+
</ItemGroup>
15+
16+
<ItemGroup Condition="'$(TargetsLinux)' == 'true'">
17+
<!--Excluding Windows implementations-->
18+
<Compile Remove="**\*.Windows.cs" />
19+
</ItemGroup>
20+
21+
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
22+
<!--Excluding Linux implementations-->
23+
<Compile Remove="**\*.Linux.cs" />
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Device.Gpio.Drivers;
6+
7+
namespace System.Device.Gpio.Tests
8+
{
9+
public class UnixDriverTests : GpioControllerTestBase
10+
{
11+
protected override GpioDriver GetTestDriver() => new UnixDriver();
12+
13+
protected override PinNumberingScheme GetTestNumberingScheme() => PinNumberingScheme.Logical;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Device.Gpio.Drivers;
6+
7+
namespace System.Device.Gpio.Tests
8+
{
9+
public class WindowsDriverTests : GpioControllerTestBase
10+
{
11+
protected override GpioDriver GetTestDriver() => new Windows10Driver();
12+
13+
protected override PinNumberingScheme GetTestNumberingScheme() => PinNumberingScheme.Logical;
14+
}
15+
}

src/System.Device.Gpio/System.Device.Gpio.sln

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.28307.106
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.28509.92
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Device.Gpio", "System.Device.Gpio.csproj", "{35DCDFB2-150C-4ED9-B63F-22B3CEC87D54}"
77
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Device.Gpio.Tests", "..\System.Device.Gpio.Tests\System.Device.Gpio.Tests.csproj", "{B8423DF8-9DD2-40FE-990D-2B6543EB7F82}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,14 @@ Global
2123
{35DCDFB2-150C-4ED9-B63F-22B3CEC87D54}.Release|Any CPU.Build.0 = Release|Any CPU
2224
{35DCDFB2-150C-4ED9-B63F-22B3CEC87D54}.Windows-Debug|Any CPU.ActiveCfg = Windows-Debug|Any CPU
2325
{35DCDFB2-150C-4ED9-B63F-22B3CEC87D54}.Windows-Debug|Any CPU.Build.0 = Windows-Debug|Any CPU
26+
{B8423DF8-9DD2-40FE-990D-2B6543EB7F82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{B8423DF8-9DD2-40FE-990D-2B6543EB7F82}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{B8423DF8-9DD2-40FE-990D-2B6543EB7F82}.Linux-Debug|Any CPU.ActiveCfg = Linux-Debug|Any CPU
29+
{B8423DF8-9DD2-40FE-990D-2B6543EB7F82}.Linux-Debug|Any CPU.Build.0 = Linux-Debug|Any CPU
30+
{B8423DF8-9DD2-40FE-990D-2B6543EB7F82}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{B8423DF8-9DD2-40FE-990D-2B6543EB7F82}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{B8423DF8-9DD2-40FE-990D-2B6543EB7F82}.Windows-Debug|Any CPU.ActiveCfg = Windows-Debug|Any CPU
33+
{B8423DF8-9DD2-40FE-990D-2B6543EB7F82}.Windows-Debug|Any CPU.Build.0 = Windows-Debug|Any CPU
2434
EndGlobalSection
2535
GlobalSection(SolutionProperties) = preSolution
2636
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)