-
Notifications
You must be signed in to change notification settings - Fork 38
/
printing_test.go
120 lines (108 loc) · 3.4 KB
/
printing_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package terminal
import (
_ "embed"
"testing"
"unicode/utf16"
"fyne.io/fyne/v2"
"github.com/stretchr/testify/assert"
)
func TestHandleOutput_PrintMode(t *testing.T) {
tests := map[string]struct {
inputSeq string
}{
"pdf printing": {
inputSeq: esc("_set printer:_editor:tmp/DBRRPT-20231026-022112-27.3446959766687.pdf") + "\000" + esc("\\"),
},
}
// Iterate through the test cases
for name, test := range tests {
t.Run(name, func(t *testing.T) {
terminal := New()
terminal.handleOutput([]byte(test.inputSeq))
})
}
}
func TestHandleOutput_Printing(t *testing.T) {
tests := map[string]struct {
inputSeq []byte
expectedPrintingState bool
expectedPrintData []byte
expectedSpooledData []byte
expectedScreenData string
}{
"start printing": {
inputSeq: []byte(esc("[5ithisshouldbeprinted")),
expectedPrintData: []byte("thisshouldbeprinted"),
expectedPrintingState: true,
},
"complete printing": {
inputSeq: []byte(esc("[5i") + "thisshouldbeprinted" + esc("[4i")),
expectedSpooledData: []byte("thisshouldbeprinted"),
expectedPrintingState: false,
},
"printing with embedded esc": {
inputSeq: []byte(esc("[5i") + esc("����B�") + esc("[4i")),
expectedSpooledData: []byte(esc("����B�")),
expectedPrintingState: false,
},
"UTF-8 Content": {
inputSeq: []byte(esc("[5i") + "Hello, 世界!" + esc("[4i")),
expectedSpooledData: []byte("Hello, 世界!"),
expectedPrintingState: false,
},
"UTF-16 Content": {
inputSeq: []byte(esc("[5i") + string(utf16.Decode([]uint16{
0x0048, 0x0065, 0x006c, 0x006c, 0x006f, 0x002c, 0x0020, 0x4e16, 0x754c, 0x0021,
})) + esc("[4i")),
expectedSpooledData: []byte("Hello, 世界!"),
expectedPrintingState: false,
},
"ISO-8859-1 Content": {
inputSeq: []byte(esc("[5iH") + "\xe9llo, W\xf6rld!" + esc("[4i")),
expectedSpooledData: []byte{0x48, 0xe9, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0xf6, 0x72, 0x6c, 0x64, 0x21},
expectedPrintingState: false,
},
"when print is mid buffer": {
inputSeq: []byte("abc" + esc("[5i") + "def" + esc("[4i") + "ghi"),
expectedScreenData: "abcghi",
expectedSpooledData: []byte("def"),
},
}
// Iterate through the test cases
for name, test := range tests {
t.Run(name, func(t *testing.T) {
terminal := New()
terminal.Resize(fyne.NewSize(50, 50))
var spooledData []byte
terminal.printer = PrinterFunc(func(d []byte) {
spooledData = d
})
terminal.handleOutput(test.inputSeq)
assert.Equal(t, test.expectedScreenData, terminal.content.Text())
assert.Equal(t, test.expectedPrintingState, terminal.state.printing)
assert.Equal(t, test.expectedSpooledData, spooledData)
assert.Equal(t, test.expectedPrintData, terminal.printData)
})
}
}
//go:embed test_data/chn.pdf
var examplePDFData []byte
func TestHandleOutput_Printing_PDF(t *testing.T) {
terminal := New()
var spooledData []byte
terminal.printer = PrinterFunc(func(d []byte) {
spooledData = d
})
data := []byte{asciiEscape, '[', '5', 'i'}
data = append(data, examplePDFData...)
data = append(data, []byte{asciiEscape, '[', '4', 'i'}...)
for i := 0; i < len(data); i += bufLen {
end := i + bufLen
if end > len(data) {
end = len(data)
}
t.Logf("sending chunk")
terminal.handleOutput(data[i:end])
}
assert.Equal(t, spooledData, examplePDFData)
}