Skip to content

Commit

Permalink
Structure Viewer UnitTest project (merge after #193) (#199)
Browse files Browse the repository at this point in the history
* Change invertdraw to skia xor blending

- Also added tests
- Refactored viewer tests

* Begin structuring viewer testing project

- readme for goals
- custom test for feature development
- zpl/png data folder copied to output
- factor out more to common

* Try to fix cannot copy data to output for linux build

* Copy data again

* Capitalization mistake
  • Loading branch information
reportingissue authored Apr 23, 2024
1 parent a41fc18 commit 2150dac
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand All @@ -19,5 +19,9 @@
<ItemGroup>
<ProjectReference Include="..\BinaryKits.Zpl.Viewer\BinaryKits.Zpl.Viewer.csproj" />
</ItemGroup>

<ItemGroup>
<Content Include="Data\**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
53 changes: 53 additions & 0 deletions src/BinaryKits.Zpl.Viewer.UnitTest/Common.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.IO;
using BinaryKits.Zpl.Viewer.ElementDrawers;

namespace BinaryKits.Zpl.Viewer.UnitTest
{
internal class Common
{
/// <summary>
/// Load zpl strings from files in the Data/Zpl directory
/// </summary>
/// <param name="name">name of of the file, .zpl2 extension optional</param>
/// <returns></returns>
public static string LoadZPL(string name) {
if (!name.Contains(".zpl2"))
{
name += ".zpl2"; ;
}
var path = Path.Combine("Data", "Zpl", name);
return System.IO.File.ReadAllText(path);
}

/// <summary>
/// Generic printer to test zpl -> png output
/// </summary>
/// <param name="zpl"></param>
/// <param name="outputFilename">PNG filename ex: "file.png"</param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="ppmm"></param>
/// <param name="options"></param>
public static void DefaultPrint(
string zpl,
string outputFilename,
double width = 101.6,
double height = 152.4,
int ppmm = 8,
DrawerOptions options = null
)
{
IPrinterStorage printerStorage = new PrinterStorage();
var drawer = new ZplElementDrawer(printerStorage, options);

var analyzer = new ZplAnalyzer(printerStorage);
var analyzeInfo = analyzer.Analyze(zpl);

foreach (var labelInfo in analyzeInfo.LabelInfos)
{
var imageData = drawer.Draw(labelInfo.ZplElements, width, height, ppmm);
File.WriteAllBytes(outputFilename, imageData);
}
}
}
}
15 changes: 15 additions & 0 deletions src/BinaryKits.Zpl.Viewer.UnitTest/CustomTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BinaryKits.Zpl.Viewer.UnitTest
{
[TestClass]
public class CustomTest
{
[TestMethod]
public void Custom()
{
string zplString = Common.LoadZPL("custom");
Common.DefaultPrint(zplString, "custom.png");
}
}
}
Empty file.
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions src/BinaryKits.Zpl.Viewer.UnitTest/Data/Zpl/font-assign.zpl2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
^XA
^FO20, 20
^A1N,40, 30 ^FD西瓜^FS
^FO20, 50
^A0N,40, 30 ^FDABCDEFG^FS
^XZ
14 changes: 14 additions & 0 deletions src/BinaryKits.Zpl.Viewer.UnitTest/Data/Zpl/invert1.zpl2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
^XA
^FO10,100
^GB70,70,70,,3^FS
^FO110,100
^GB70,70,70,,3^FS
^FO210,100
^GB70,70,70,,3^FS
^FO310,100
^GB70,70,70,,3^FS
^FO17,110
^CF0,70,93
^FR
^FDREVERSE^FS
^XZ
46 changes: 46 additions & 0 deletions src/BinaryKits.Zpl.Viewer.UnitTest/Data/Zpl/invert2.zpl2
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
^XA
^FR
^FO50,50
^GB100,100,10,W,5^FS
^FR
^FO200,50
^GB100,100,10,W,5^FS
^FO100,120
^GB30,25,10,B,2^FS
^FO250,120
^GB30,25,10,B,2^FS
^FO130,180
^GB90,90,45,B,8^FS
^FR
^FO75,300
^GB30,20,10,W,0^FS
^FR
^FO265,300
^GB30,20,10,W,0^FS
^FR
^FO105,320
^GB160,20,10,W,0^FS
^FR
^FO120,310
^GB10,20,5,B,0^FS
^FR
^FO140,310
^GB10,20,5,B,0^FS
^FR
^FO160,310
^GB10,20,5,B,0^FS
^FR
^FO180,310
^GB10,20,5,B,0^FS
^FR
^FO200,310
^GB10,20,5,B,0^FS
^FR
^FO220,310
^GB10,20,5,B,0^FS
^FR
^FO240,310
^GB10,20,5,B,0^FS
^FO150,330
^GB70,90,45,B,0^FS
^XZ
21 changes: 21 additions & 0 deletions src/BinaryKits.Zpl.Viewer.UnitTest/Data/Zpl/merge.zpl2
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
^XA
^DFETIQUE-1^FS
^PRC
^LH0,0^FS
^LL408
^MD0
^MNY
^LH0,0^FS
^FO120,141^A0N,27,23^CI13^FR^FN999^FS
^BY2,3.0^FO213,7^BCN,80,N,Y,N^FR^FN997^FS
^FO313,95^A0N,35,23^CI13^FR^FB105,2,0,L^FN997^FS
^FO40,141^A0N,27,33^CI13^FR^FDP/N :^FS
^XZ

^XA
^XFETIQUE-1.ZPL
^FN999^FDC19755BA01:F9111^FS
^FN997^FD3758292^FS
^PQ1,0,1,N
^XZ
^FX
134 changes: 10 additions & 124 deletions src/BinaryKits.Zpl.Viewer.UnitTest/DrawerTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using BinaryKits.Zpl.Viewer.ElementDrawers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.IO;


namespace BinaryKits.Zpl.Viewer.UnitTest
Expand All @@ -14,13 +11,7 @@ public class DrawerTest
[TestMethod]
public void FontAssignment()
{
string zplString = @"
^XA
^FO20, 20
^A1N,40, 30 ^FD西瓜^FS
^FO20, 50
^A0N,40, 30 ^FDABCDEFG^FS
^XZ";
string zplString = Common.LoadZPL("font-assign");

var drawOptions = new DrawerOptions()
{
Expand All @@ -39,131 +30,26 @@ public void FontAssignment()
return SKTypeface.Default;
}
};
DefaultPrint(zplString, "font-assign.png", 300, 300, 8, drawOptions);
Common.DefaultPrint(zplString, "font-assign.png", 300, 300, 8, drawOptions);
}

[TestMethod]
public void FormatHandling()
{
string zplString = @"
^XA
^DFETIQUE-1^FS
^PRC
^LH0,0^FS
^LL408
^MD0
^MNY
^LH0,0^FS
^FO120,141^A0N,27,23^CI13^FR^FN999^FS
^BY2,3.0^FO213,7^BCN,80,N,Y,N^FR^FN997^FS
^FO313,95^A0N,35,23^CI13^FR^FB105,2,0,L^FN997^FS
^FO40,141^A0N,27,33^CI13^FR^FDP/N :^FS
^XZ
^XA
^XFETIQUE-1.ZPL
^FN999^FDC19755BA01:F9111^FS
^FN997^FD3758292^FS
^PQ1,0,1,N
^XZ
^FX";
DefaultPrint(zplString, "merge-test.png", 100, 100, 8);
string zplString = Common.LoadZPL("merge");
Common.DefaultPrint(zplString, "merge.png", 100, 100, 8);
}

[TestMethod]
public void InvertColor()
{
// Example in ZPL manual
string test1 = @"
^XA
^FO10,100
^GB70,70,70,,3^FS
^FO110,100
^GB70,70,70,,3^FS
^FO210,100
^GB70,70,70,,3^FS
^FO310,100
^GB70,70,70,,3^FS
^FO17,110
^CF0,70,93
^FR
^FDREVERSE^FS
^XZ
";
string test1 = Common.LoadZPL("invert1");
// from https://github.com/BinaryKits/BinaryKits.Zpl/pull/64
string test2 = @"
^XA
^FR
^FO50,50
^GB100,100,10,W,5^FS
^FR
^FO200,50
^GB100,100,10,W,5^FS
^FO100,120
^GB30,25,10,B,2^FS
^FO250,120
^GB30,25,10,B,2^FS
^FO130,180
^GB90,90,45,B,8^FS
^FR
^FO75,300
^GB30,20,10,W,0^FS
^FR
^FO265,300
^GB30,20,10,W,0^FS
^FR
^FO105,320
^GB160,20,10,W,0^FS
^FR
^FO120,310
^GB10,20,5,B,0^FS
^FR
^FO140,310
^GB10,20,5,B,0^FS
^FR
^FO160,310
^GB10,20,5,B,0^FS
^FR
^FO180,310
^GB10,20,5,B,0^FS
^FR
^FO200,310
^GB10,20,5,B,0^FS
^FR
^FO220,310
^GB10,20,5,B,0^FS
^FR
^FO240,310
^GB10,20,5,B,0^FS
^FO150,330
^GB70,90,45,B,0^FS
^XZ
";
DefaultPrint(test1, "inverted1.png", 100, 100, 8);
DefaultPrint(test2, "inverted2.png");
}
string test2 = Common.LoadZPL("invert2");

/// <summary>
/// Generic printer to test zpl -> png output
/// </summary>
/// <param name="zpl"></param>
/// <param name="outputFilename">PNG filename ex: "file.png"</param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="ppmm"></param>
/// <param name="options"></param>
private void DefaultPrint(string zpl, string outputFilename, double width=101.6, double height=152.4, int ppmm=8, DrawerOptions options=null) {
IPrinterStorage printerStorage = new PrinterStorage();
var drawer = new ZplElementDrawer(printerStorage, options);

var analyzer = new ZplAnalyzer(printerStorage);
var analyzeInfo = analyzer.Analyze(zpl);

foreach (var labelInfo in analyzeInfo.LabelInfos)
{
var imageData = drawer.Draw(labelInfo.ZplElements, width, height, ppmm);
File.WriteAllBytes(outputFilename, imageData);
}
Common.DefaultPrint(test1, "inverted1.png", 100, 100, 8);
Common.DefaultPrint(test2, "inverted2.png");
}
}
}
}
23 changes: 23 additions & 0 deletions src/BinaryKits.Zpl.Viewer.UnitTest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Viewer Unit Test

This project (should) supports the Viewer project in multiple ways.

## Standard unit testing
Mostly testing the features of the virtual printer(s) and analyzers.

## Detect changes and regressions in png outputs
Given a set of known good png output files, we should be able to detect when new output changes.
This would be done by loading 2 pngs into skia bitmaps and xor-ing them. We can then count the amount of black pixels and output a new png showing the difference.

## Feature development
The `CustomTest.cs` exists for quick development. Load your zpl data in `Data/Zpl/custom.zpl2` and test. Changes to these files are not tracked under source control. Use `git update-index --no-skip-worktree custom.zpl2` and `git update-index --skip-worktree custom.zpl2` (see https://stackoverflow.com/a/39776107) to enable tracking changes to the files.

## Benchmarks
Performance isn't stellar yet. We can use features in Visual Studio, but other tools might be more suitable like [BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet).

Note that a lot of time is spent inside 3rd party libraries like Skia, BarcodeLib and Zxing.net and Drawing.

## Todo
- [ ] Linux compatibility
- [ ] Benchmarking
- [ ] Xor diffing pngs

0 comments on commit 2150dac

Please sign in to comment.