Skip to content

Commit

Permalink
v1.0 Released to master
Browse files Browse the repository at this point in the history
This API is now stable and no breaking changes will be made in the 1.0 releases
  • Loading branch information
Cory Todd committed Jul 13, 2018
2 parents 7aac5ac + daf3fde commit 5fb9cc9
Show file tree
Hide file tree
Showing 45 changed files with 1,579 additions and 137 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,4 @@ __pycache__/
/PTIRelianceLib/build/*
_site/
nuget/
/RelianceCLI/build/
33 changes: 33 additions & 0 deletions PTIRelianceLib.Tests/IO/Internal/HidDeviceInfoTests.cs
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);
}
}
}

92 changes: 92 additions & 0 deletions PTIRelianceLib.Tests/IO/StructuredReaderTests.cs
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;
}
}
}
60 changes: 41 additions & 19 deletions PTIRelianceLib.Tests/Imaging/BasePrintLogoTests.cs
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);
}
}
}
9 changes: 8 additions & 1 deletion PTIRelianceLib.Tests/Imaging/DitherFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ public void GetDithererFloydSteinbergFact()
Assert.True(ImageTestHelpers.CompareCrc32(expected, dithered));
}

// TODO test false floyd steinberg
[Fact()]
public void GetDithererFloydSteinbergFalseFact()
{
var input = new BasePrintLogo(BinaryFile.From(Properties.Resources.gray_bitmap)).ImageData;
var expected = new BasePrintLogo(BinaryFile.From(Properties.Resources.gray_floydsteinbergsfalse)).ImageData;
var dithered = DitherFactory.GetDitherer(DitherAlgorithms.FloydSteinbergFalse).GenerateDithered(input);
Assert.True(ImageTestHelpers.CompareCrc32(expected, dithered));
}

[Fact()]
public void GetDithererJarvisJudiceNinkeFact()
Expand Down
6 changes: 6 additions & 0 deletions PTIRelianceLib.Tests/Imaging/ImageExtTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ public void BitmapToLogoBufferSimpleFact()
Assert.Equal(expectedBuff, actualBuff);
}

[Fact]
public void TestNullBitmapToBuffer()
{
Assert.Empty(ImageExt.ToBuffer(null));
}

/// <summary>
/// Returns a list of type T with value repeat count times
/// </summary>
Expand Down
39 changes: 39 additions & 0 deletions PTIRelianceLib.Tests/Imaging/LogoStorageConfigTests.cs
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);
}
}
}
33 changes: 33 additions & 0 deletions PTIRelianceLib.Tests/Internal/ExtensionsTests.cs
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);
}
}
}
}
59 changes: 59 additions & 0 deletions PTIRelianceLib.Tests/Internal/FixedArrayTests.cs
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]);
}
}
}
4 changes: 4 additions & 0 deletions PTIRelianceLib.Tests/MutableReliancePacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ namespace PTIRelianceLib.Tests

internal class MutableReliancePacket : ReliancePacket
{
public MutableReliancePacket()
{
}

internal MutableReliancePacket(byte[] data) : base(data)
{
}
Expand Down
Loading

0 comments on commit 5fb9cc9

Please sign in to comment.