121 lines
2.9 KiB
Go
121 lines
2.9 KiB
Go
package printer
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"math"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// Test 1: MockDriver.Print(validBitmap) saves a PNG to SaveDir and returns nil.
|
|
func TestMockDriverPrintSavesPNG(t *testing.T) {
|
|
dir := t.TempDir()
|
|
m := NewMockDriver()
|
|
m.SaveDir = dir
|
|
|
|
// Create a small 8x4 1-bit bitmap (all black).
|
|
rowBytes := int(math.Ceil(8.0 / 8.0))
|
|
bitmap := make([]byte, rowBytes*4)
|
|
for i := range bitmap {
|
|
bitmap[i] = 0xFF // all black
|
|
}
|
|
|
|
if err := m.Print(bitmap, 8, 4); err != nil {
|
|
t.Fatalf("Print returned unexpected error: %v", err)
|
|
}
|
|
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
t.Fatalf("ReadDir: %v", err)
|
|
}
|
|
found := false
|
|
for _, e := range entries {
|
|
if strings.HasPrefix(e.Name(), "hwlab-label-") && strings.HasSuffix(e.Name(), ".png") {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("expected hwlab-label-*.png to be created in temp dir")
|
|
}
|
|
}
|
|
|
|
// Test 2: MockDriver.Print(nil) returns ErrEmptyBitmap without panicking.
|
|
func TestMockDriverPrintNilBitmap(t *testing.T) {
|
|
m := NewMockDriver()
|
|
err := m.Print(nil, 0, 0)
|
|
if err != ErrEmptyBitmap {
|
|
t.Fatalf("expected ErrEmptyBitmap, got %v", err)
|
|
}
|
|
}
|
|
|
|
// Test 3: MockDriver.Connect() returns nil; Disconnect() returns nil.
|
|
func TestMockDriverConnectDisconnect(t *testing.T) {
|
|
m := NewMockDriver()
|
|
if err := m.Connect(); err != nil {
|
|
t.Fatalf("Connect: %v", err)
|
|
}
|
|
if err := m.Disconnect(); err != nil {
|
|
t.Fatalf("Disconnect: %v", err)
|
|
}
|
|
}
|
|
|
|
// Test 4: ImageToRawBitmap returns byte slice of length ceil(width*height/8).
|
|
func TestImageToRawBitmapLength(t *testing.T) {
|
|
cases := []struct {
|
|
w, h int
|
|
}{
|
|
{8, 4},
|
|
{7, 3},
|
|
{16, 1},
|
|
{384, 120},
|
|
}
|
|
for _, c := range cases {
|
|
img := image.NewGray(image.Rect(0, 0, c.w, c.h))
|
|
// Fill with a pattern: top half black, bottom half white.
|
|
for y := 0; y < c.h/2; y++ {
|
|
for x := 0; x < c.w; x++ {
|
|
img.SetGray(x, y, color.Gray{Y: 0})
|
|
}
|
|
}
|
|
|
|
bitmap, w, h := ImageToRawBitmap(img)
|
|
if w != c.w || h != c.h {
|
|
t.Errorf("expected dimensions %dx%d, got %dx%d", c.w, c.h, w, h)
|
|
}
|
|
rowBytes := int(math.Ceil(float64(c.w) / 8.0))
|
|
want := rowBytes * c.h
|
|
if len(bitmap) != want {
|
|
t.Errorf("w=%d h=%d: expected bitmap len %d, got %d", c.w, c.h, want, len(bitmap))
|
|
}
|
|
}
|
|
}
|
|
|
|
// Test 5: PrtQutieDriver.Connect() returns ErrNoDevice (not a panic).
|
|
func TestPrtQutieConnectReturnsErrNoDevice(t *testing.T) {
|
|
d := NewPrtQutieDriver(9600)
|
|
err := d.Connect()
|
|
if err != ErrNoDevice {
|
|
t.Fatalf("expected ErrNoDevice, got %v", err)
|
|
}
|
|
}
|
|
|
|
// Helper: create a dummy file path in temp dir to verify SaveDir is honoured.
|
|
func TestMockDriverSaveDirHonoured(t *testing.T) {
|
|
dir := t.TempDir()
|
|
m := NewMockDriver()
|
|
m.SaveDir = dir
|
|
|
|
bitmap := []byte{0xFF, 0xFF} // 2 bytes → 16 pixels wide, 1 row
|
|
if err := m.Print(bitmap, 16, 1); err != nil {
|
|
t.Fatalf("Print: %v", err)
|
|
}
|
|
|
|
matches, _ := filepath.Glob(filepath.Join(dir, "hwlab-label-*.png"))
|
|
if len(matches) == 0 {
|
|
t.Fatal("no PNG saved in SaveDir")
|
|
}
|
|
}
|