-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This API is now stable and no breaking changes will be made in the 1.0 releases
- Loading branch information
Showing
45 changed files
with
1,579 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -262,3 +262,4 @@ __pycache__/ | |
/PTIRelianceLib/build/* | ||
_site/ | ||
nuget/ | ||
/RelianceCLI/build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace PTIRelianceLib.Tests.IO.Internal | ||
{ | ||
using PTIRelianceLib.IO.Internal; | ||
|
||
public class HidDeviceInfoTests | ||
{ | ||
[Fact] | ||
public void TestToString() | ||
{ | ||
var devinfo = new HidDeviceInfo | ||
{ | ||
VendorId = 0x1234, | ||
ProductId = 0x5678, | ||
ManufacturerString = "Company", | ||
ProductString = "Product", | ||
Path = "/some/device&1234_5678/4325EACF" | ||
}; | ||
|
||
var str = devinfo.ToString(); | ||
Assert.NotNull(str); | ||
|
||
Assert.Contains(devinfo.ManufacturerString, str); | ||
Assert.Contains(devinfo.ProductString, str); | ||
Assert.Contains(devinfo.VendorId.ToString("X4"), str); | ||
Assert.Contains(devinfo.ProductId.ToString("X4"), str); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace PTIRelianceLib.Tests.IO | ||
{ | ||
using PTIRelianceLib.IO; | ||
using PTIRelianceLib.IO.Internal; | ||
using PTIRelianceLib.Transport; | ||
|
||
public class StructuredReaderTests | ||
{ | ||
private static readonly object MTestLock = new object(); | ||
|
||
private readonly HidDeviceConfig _mConfig; | ||
private readonly FakeNativeMethods _mNativeMock; | ||
private readonly HidPort<MutableReliancePacket> _mPort; | ||
public const int VendorId = 0x0425; | ||
public const int ProductId = 0x8147; | ||
|
||
public StructuredReaderTests() | ||
{ | ||
_mNativeMock = new FakeNativeMethods(); | ||
|
||
// Reliance will "always" use report lengths of 34 bytes | ||
_mConfig = new HidDeviceConfig | ||
{ | ||
VendorId = VendorId, | ||
ProductId = ProductId, | ||
InReportLength = 34, | ||
OutReportLength = 34, | ||
InReportId = 2, | ||
OutReportId = 1, | ||
NativeHid = _mNativeMock | ||
}; | ||
|
||
_mPort = new HidPort<MutableReliancePacket>(_mConfig); | ||
} | ||
|
||
[Fact] | ||
public void TestCtor() | ||
{ | ||
lock (MTestLock) | ||
{ | ||
_mNativeMock.GetNextResponse = (d) => GenerateHidData(0xAA); | ||
new StructuredReader(_mPort); | ||
} | ||
} | ||
|
||
|
||
[Fact] | ||
public void TestReadNak() | ||
{ | ||
lock (MTestLock) | ||
{ | ||
_mNativeMock.GetNextResponse = (d) => GenerateHidData(0xAC); | ||
var reader = new StructuredReader(_mPort); | ||
var resp = reader.Read(1, 2, 3); | ||
Assert.True(resp.IsEmpty); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void TestReadAck() | ||
{ | ||
lock (MTestLock) | ||
{ | ||
_mNativeMock.GetNextResponse = (d) => GenerateHidData(0xAA, 1, 2, 3); | ||
var reader = new StructuredReader(_mPort); | ||
var resp = reader.Read(3, 1, 2, 3); | ||
Assert.False(resp.IsEmpty); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Generates a valid inreport | ||
/// </summary> | ||
/// <param name="payload">Payload to pack</param> | ||
/// <returns></returns> | ||
private byte[] GenerateHidData(params byte[] payload) | ||
{ | ||
var packet = new ReliancePacket(payload); | ||
payload = packet.Package().GetBytes(); | ||
|
||
var buff = new byte[_mConfig.InReportLength]; | ||
buff[0] = _mConfig.InReportId; | ||
buff[1] = (byte) payload.Length; | ||
Array.Copy(payload, 0, buff, 2, Math.Min(payload.Length, buff.Length - 2)); | ||
return buff; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,59 @@ | ||
using PTIRelianceLib.Imaging; | ||
using System; | ||
using Xunit; | ||
|
||
namespace PTIRelianceLib.Tests.Imaging | ||
{ | ||
using System.ComponentModel; | ||
using Properties; | ||
using PTIRelianceLib.Imaging; | ||
|
||
public class BasePrintLogoTests | ||
{ | ||
[Fact()] | ||
[Category("BMP")] | ||
public void ApplyColorInversionTest() | ||
[Fact] | ||
public void TestCtor() | ||
{ | ||
// Input are expected are provided as resources, dithered is what | ||
// we are testing | ||
var bitmap = BinaryFile.From(Resources.black_bitmap); | ||
var logo = new BasePrintLogo(bitmap); | ||
|
||
var input = BinaryFile.From(Properties.Resources.white_bitmap); | ||
// Dimensions should be unchanged, 48x48 | ||
Assert.Equal(48, logo.IdealHeight); | ||
Assert.Equal(48, logo.IdealWidth); | ||
|
||
var logo = new BasePrintLogo(input); | ||
// Max width/height were not specified, should be 0 | ||
Assert.Equal(0, logo.MaxHeight); | ||
Assert.Equal(0, logo.MaxWidth); | ||
|
||
Assert.False(logo.IsInverted); | ||
// Valid data was passed in, ImageData should be valid too | ||
Assert.NotNull(logo.ImageData); | ||
} | ||
|
||
[Fact] | ||
public void TestCtorNullBitmap() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => new BasePrintLogo(null)); | ||
} | ||
|
||
[Fact] | ||
public void TestApplyDithering() | ||
{ | ||
var bitmap = BinaryFile.From(Resources.gray_bitmap); | ||
var logo = new BasePrintLogo(bitmap); | ||
|
||
logo.ApplyColorInversion(); | ||
var startw = logo.IdealWidth; | ||
var starth = logo.IdealHeight; | ||
var predither = Crc32.ComputeChecksum(logo.ToBuffer()); | ||
|
||
var inverted = logo.ImageData; | ||
var expected = new BasePrintLogo(BinaryFile.From(Properties.Resources.black_bitmap)).ImageData; | ||
// If this fails, someone dispose of the bitmap along the way. | ||
// Look for "using" statements to fix | ||
logo.ApplyDithering(DitherAlgorithms.Atkinson); | ||
|
||
// White should ivnert to black | ||
Assert.True(ImageTestHelpers.CompareCrc32(expected, inverted)); | ||
Assert.True(logo.IsInverted); | ||
// Dimensions should be unchanged, 48x48 | ||
Assert.Equal(starth, logo.IdealHeight); | ||
Assert.Equal(startw, logo.IdealWidth); | ||
|
||
// Flip back to white, test that the inversion flag is cleared | ||
logo.ApplyColorInversion(); | ||
Assert.False(logo.IsInverted); | ||
// Valid data was passed in, ImageData should be valid too | ||
Assert.NotNull(logo.ImageData); | ||
var postdither = Crc32.ComputeChecksum(logo.ToBuffer()); | ||
Assert.NotEqual(predither, postdither); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Xunit; | ||
|
||
namespace PTIRelianceLib.Tests.Imaging | ||
{ | ||
using PTIRelianceLib.Imaging; | ||
|
||
public class LogoStorageConfigTests | ||
{ | ||
[Fact] | ||
public void FailIfDefaultsChange() | ||
{ | ||
var config = LogoStorageConfig.Default; | ||
Assert.Equal(640, config.MaxPixelWidth); | ||
Assert.Equal(127, config.Threshold); | ||
Assert.Equal(DitherAlgorithms.None, config.Algorithm); | ||
|
||
// Make sure properties stay editable | ||
config.MaxPixelWidth = 0; | ||
config.Threshold = 0; | ||
config.Algorithm = DitherAlgorithms.Burkes; | ||
} | ||
|
||
[Fact] | ||
public void FailIfDefaultsAreReadonly() | ||
{ | ||
var config = LogoStorageConfig.Default; | ||
|
||
// Make sure properties stay editable | ||
config.MaxPixelWidth = 0; | ||
config.Threshold = 0; | ||
config.Algorithm = DitherAlgorithms.Burkes; | ||
|
||
|
||
Assert.Equal(0, config.MaxPixelWidth); | ||
Assert.Equal(0, config.Threshold); | ||
Assert.Equal(DitherAlgorithms.Burkes, config.Algorithm); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Xunit; | ||
|
||
namespace PTIRelianceLib.Tests.Internal | ||
{ | ||
using System.Collections.Generic; | ||
|
||
public class ExtensionsTests | ||
{ | ||
[Fact] | ||
public void ToUshortBE() | ||
{ | ||
var data = new Dictionary<byte[], ushort> | ||
{ | ||
{new byte[] {0, 0}, 0}, | ||
{new byte[] {1, 0}, 1}, | ||
{new byte[] {2, 1}, 258}, | ||
{new byte[] {3, 2}, 515}, | ||
{new byte[] {4, 3}, 772}, | ||
{new byte[] {255, 255}, 0xFFFF} | ||
}; | ||
|
||
foreach (var kv in data) | ||
{ | ||
var actual1 = kv.Key.ToUshortBE(); | ||
Assert.Equal(kv.Value, actual1); | ||
|
||
// Run the reverse test on the same data | ||
var actual2 = actual1.ToBytesBE(); | ||
Assert.Equal(kv.Key, actual2); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace PTIRelianceLib.Tests.Internal | ||
{ | ||
public class FixedArrayTests | ||
{ | ||
[Fact] | ||
public void TestCtor() | ||
{ | ||
var farr = new FixedArray<DateTime>(10); | ||
Assert.Equal(10, farr.Size); | ||
Assert.Equal(0, farr.Count); | ||
Assert.True(string.IsNullOrEmpty(farr.Content)); | ||
} | ||
|
||
[Fact] | ||
public void TestSetData() | ||
{ | ||
var farr = new FixedArray<BinaryFile>(3); | ||
|
||
farr.SetData( | ||
BinaryFile.From(new byte[0]), | ||
BinaryFile.From(new byte[1]), | ||
BinaryFile.From(new byte[2]), | ||
BinaryFile.From(new byte[3]) // This one should be silently discarded | ||
); | ||
|
||
Assert.Equal(farr.Count, farr.Size); | ||
|
||
var data = farr.GetData(); | ||
Assert.Equal(data.Length, farr.Size); | ||
|
||
// Test that each length is what we expect | ||
var len = 0; | ||
foreach (var bf in data) | ||
{ | ||
Assert.Equal(len++, bf.Length); | ||
} | ||
Assert.False(string.IsNullOrEmpty(farr.Content)); | ||
} | ||
|
||
[Fact] | ||
public void TestIndexer() | ||
{ | ||
var farr = new FixedArray<object>(3); | ||
var obj1 = new object(); | ||
var obj2 = new object(); | ||
var obj3 = new object(); | ||
|
||
farr.SetData(obj1, obj2, obj3); | ||
|
||
Assert.Equal(obj1, farr[0]); | ||
Assert.Equal(obj2, farr[1]); | ||
Assert.Equal(obj3, farr[2]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.