-
Notifications
You must be signed in to change notification settings - Fork 410
/
service_test.go
147 lines (138 loc) · 3.51 KB
/
service_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
package selenium
import (
"fmt"
"testing"
"time"
"github.com/BurntSushi/xgbutil"
"github.com/google/go-cmp/cmp"
)
func TestIsDisplay(t *testing.T) {
tests := []struct {
desc string
in string
valid bool
}{
{
desc: "valid with just display",
in: "2",
valid: true,
},
{
desc: "valid with display and screen",
in: "2.5",
valid: true,
},
{
desc: "invalid with non-numeric display",
in: "a",
valid: false,
},
{
desc: "invalid with non-numeric display and screen",
in: "a.5",
valid: false,
},
{
desc: "invalid with display and non-numeric screen",
in: "2.b",
valid: false,
},
{
desc: "invalid with display and blank screen",
in: "2.",
valid: false,
},
{
desc: "invalid with blank display and screen",
in: ".3",
valid: false,
},
{
desc: "invalid with blank display and blank screen",
in: ".",
valid: false,
},
{
desc: "blank string is invalid",
in: "",
valid: false,
},
{
desc: "malformed input",
in: "2.5.7",
valid: false,
},
}
for _, test := range tests {
if got, want := isDisplay(test.in), test.valid; got != want {
t.Errorf("%s: isDisplay = %t, want %t", test.desc, got, want)
}
}
}
func TestFrameBuffer(t *testing.T) {
// Note on FrameBuffer and xgb.Conn:
// There appears to be a race condition when closing a Conn instance before
// a FrameBuffer instance. A short sleep solves the problem.
t.Run("Default behavior", func(t *testing.T) {
// The default Xvfb screen size is "1280x1024x8".
frameBuffer, err := NewFrameBuffer()
if err != nil {
t.Fatalf("Could not create frame buffer: %s", err.Error())
}
defer frameBuffer.Stop()
if frameBuffer.Display == "" {
t.Fatalf("frameBuffer.Display is empty")
}
d, err := xgbutil.NewConnDisplay(":" + frameBuffer.Display)
if err != nil {
t.Fatalf("could not connect to display %q: %s", frameBuffer.Display, err.Error())
}
defer time.Sleep(time.Second * 2)
defer d.Conn().Close()
s := d.Screen()
if diff := cmp.Diff(1280, int(s.WidthInPixels)); diff != "" {
t.Fatalf("args returned diff (-want/+got):\n%s", diff)
}
if diff := cmp.Diff(1024, int(s.HeightInPixels)); diff != "" {
t.Fatalf("args returned diff (-want/+got):\n%s", diff)
}
})
t.Run("With bad screen size", func(t *testing.T) {
options := FrameBufferOptions{
ScreenSize: "not a screen size",
}
_, err := NewFrameBufferWithOptions(options)
if err == nil {
t.Fatalf("Expected an error about the screen size")
}
})
t.Run("With screen size", func(t *testing.T) {
desiredWidth := 1024
desiredHeight := 768
desiredDepth := 24
options := FrameBufferOptions{
ScreenSize: fmt.Sprintf("%dx%dx%d", desiredWidth, desiredHeight, desiredDepth),
}
frameBuffer, err := NewFrameBufferWithOptions(options)
if err != nil {
t.Fatalf("Could not create frame buffer: %s", err.Error())
}
defer frameBuffer.Stop()
if frameBuffer.Display == "" {
t.Fatalf("frameBuffer.Display is empty")
}
d, err := xgbutil.NewConnDisplay(":" + frameBuffer.Display)
if err != nil {
t.Fatalf("could not connect to display %q: %s", frameBuffer.Display, err.Error())
}
defer time.Sleep(time.Second * 2)
defer d.Conn().Close()
s := d.Screen()
if diff := cmp.Diff(desiredWidth, int(s.WidthInPixels)); diff != "" {
t.Fatalf("args returned diff (-want/+got):\n%s", diff)
}
if diff := cmp.Diff(desiredHeight, int(s.HeightInPixels)); diff != "" {
t.Fatalf("args returned diff (-want/+got):\n%s", diff)
}
})
}