forked from alexbrainman/printer
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
printers_test.go
178 lines (161 loc) · 3.54 KB
/
printers_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package printers
import (
"encoding/json"
"fmt"
"testing"
)
func TestPrinter(t *testing.T) {
name, err := GetDefault()
if err != nil {
t.Fatalf("GetDefault failed: %v", err)
}
t.Logf("GetDefault: %s", name)
p, err := Open(name)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
defer func(p *Printer) {
_ = p.Close()
}(p)
err = p.StartDocument("my document", "RAW")
if err != nil {
t.Fatalf("StartDocument failed: %v", err)
}
defer func(p *Printer) {
_ = p.EndDocument()
}(p)
err = p.StartPage()
if err != nil {
t.Fatalf("StartPage failed: %v", err)
}
_, _ = fmt.Fprintf(p, "Hello %q\n", name)
err = p.EndPage()
if err != nil {
t.Fatalf("EndPage failed: %v", err)
}
}
func TestReadNames(t *testing.T) {
names, err := ReadNames()
if err != nil {
t.Fatalf("ReadNames failed: %v", err)
}
jsonNames, _ := json.MarshalIndent(names, "", " ")
t.Logf("ReadNames: %s", jsonNames)
name, err := GetDefault()
if err != nil {
t.Fatalf("GetDefault failed: %v", err)
}
t.Logf("GetDefault: %s", name)
// make sure default printer is listed
for _, v := range names {
if v == name {
return
}
}
t.Fatalf("Default printed %q is not listed amongst printers returned by ReadNames %q", name, names)
}
func TestDriverInfo(t *testing.T) {
name, err := GetDefault()
if err != nil {
t.Fatalf("GetDefault failed: %v", err)
}
t.Logf("GetDefault: %s", name)
p, err := Open(name)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
defer func(p *Printer) {
_ = p.Close()
}(p)
di, err := p.DriverInfo()
if err != nil {
t.Fatalf("DriverInfo failed: %v", err)
}
t.Logf("%+v", di)
}
func TestJobs(t *testing.T) {
names, err := ReadNames()
if err != nil {
t.Fatalf("ReadNames failed: %v", err)
}
for _, name := range names {
t.Log("Printer Name:", name)
p, err := Open(name)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
pj, err := p.Jobs()
if err != nil {
closePrinter(p)
t.Fatalf("Jobs failed: %v", err)
}
if len(pj) > 0 {
t.Log("Print Jobs:", len(pj))
for _, j := range pj {
b, err := json.MarshalIndent(j, "", " ")
if err == nil && len(b) > 0 {
t.Log(string(b))
}
}
}
closePrinter(p)
}
}
func closePrinter(p *Printer) {
if p == nil {
return
}
_ = p.Close()
}
func TestSetDefault(t *testing.T) {
type args struct {
printer string
}
tests := []struct {
name string
args args
wantErr bool
}{
{"PDF", args{"Microsoft Print To PDF"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := SetDefault(tt.args.printer); (err != nil) != tt.wantErr {
t.Errorf("SetDefault() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestPrinter_Forms(t *testing.T) {
tests := []struct {
name string
printer string
wantErr bool
}{
{"Fax", "Fax", false},
{"PDF", "Microsoft Print To PDF", false},
{"XPS", "Microsoft XPS Document Writer", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p, err := Open(tt.printer)
if err != nil {
t.Errorf("Open got error: %v", err)
return
}
var gotForms []FormInfo
if gotForms, err = p.Forms(); (err != nil) != tt.wantErr {
t.Errorf("Forms() error = %v, wantErr %v", err, tt.wantErr)
return
}
for i, form := range gotForms {
//formBytes, _ := json.Marshal(form)
//t.Logf("%d. %s", i+1, formBytes)
t.Logf("%d. %#v", i+1, form)
}
})
}
}