-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathui_internal_test.go
185 lines (154 loc) · 3.94 KB
/
ui_internal_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
179
180
181
182
183
184
185
package glass
import (
"encoding/json"
"errors"
"io"
"strings"
"testing"
"github.com/hamba/logger/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/zserge/lorca"
)
func TestNewUI(t *testing.T) {
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Error)
cfg := UIConfig{
Width: 1024,
Height: 764,
Fullscreen: true,
CustomCSS: []string{
"testdata/custom.css",
},
}
ui := &MockLorcaUI{}
ui.On("Eval", mock.MatchedBy(func(js string) bool {
return strings.HasPrefix(js, "loadCSS(`fonts`")
})).Once().Return(NewValue("", nil))
ui.On("Eval", "loadCSS(`customCSS1`, `custom css`);").Once().Return(NewValue("", nil))
ui.On("Bind", mock.AnythingOfType("string"), mock.Anything).Return(nil)
oldNewFunc := newFunc
t.Cleanup(func() {
newFunc = oldNewFunc
})
newFunc = func(url, dir string, width, height int, customArgs ...string) (lorca.UI, error) {
assert.Equal(t, 1024, width)
assert.Equal(t, 764, height)
assert.Equal(t, 1024, width)
assert.Contains(t, customArgs, "--start-fullscreen")
return ui, nil
}
got, err := NewUI(cfg, log)
require.NoError(t, err)
assert.IsType(t, (*UI)(nil), got)
ui.AssertExpectations(t)
}
func TestNewUI_HandlesWindowError(t *testing.T) {
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Error)
cfg := UIConfig{
Width: 1024,
Height: 764,
}
oldNewFunc := newFunc
t.Cleanup(func() {
newFunc = oldNewFunc
})
newFunc = func(url, dir string, width, height int, customArgs ...string) (lorca.UI, error) {
return nil, errors.New("test error")
}
_, err := NewUI(cfg, log)
assert.Error(t, err)
assert.EqualError(t, err, "could not create window: test error")
}
func TestUI_Done(t *testing.T) {
ch := make(chan struct{})
t.Cleanup(func() {
close(ch)
})
win := &MockLorcaUI{}
win.On("Done").Return(ch)
ui := &UI{win: win}
got := ui.Done()
if ch != got {
assert.Fail(t, "incorrect channel")
return
}
win.AssertExpectations(t)
}
func TestUI_Close(t *testing.T) {
win := &MockLorcaUI{}
win.On("Close").Return(nil)
ui := &UI{win: win}
err := ui.Close()
assert.NoError(t, err)
win.AssertExpectations(t)
}
type MockLorcaUI struct {
mock.Mock
}
func (m *MockLorcaUI) Load(url string) error {
args := m.Called(url)
return args.Error(0)
}
func (m *MockLorcaUI) Bounds() (lorca.Bounds, error) {
args := m.Called()
return args.Get(0).(lorca.Bounds), args.Error(1)
}
func (m *MockLorcaUI) SetBounds(bounds lorca.Bounds) error {
args := m.Called(bounds)
return args.Error(0)
}
func (m *MockLorcaUI) Bind(name string, f any) error {
args := m.Called(name, f)
return args.Error(0)
}
func (m *MockLorcaUI) Eval(js string) lorca.Value {
args := m.Called(js)
return args.Get(0).(lorca.Value)
}
func (m *MockLorcaUI) Done() <-chan struct{} {
args := m.Called()
return args.Get(0).(chan struct{})
}
func (m *MockLorcaUI) Close() error {
args := m.Called()
return args.Error(0)
}
type Value struct {
err error
raw json.RawMessage
}
func NewValue(val string, err error) Value {
v := json.RawMessage{}
if val != "" {
v = json.RawMessage(val)
}
return Value{
raw: v,
err: err,
}
}
func (v Value) Err() error { return v.err }
func (v Value) To(x any) error { return json.Unmarshal(v.raw, x) }
func (v Value) Float() (f float32) { v.To(&f); return f }
func (v Value) Int() (i int) { v.To(&i); return i }
func (v Value) String() (s string) { v.To(&s); return s }
func (v Value) Bool() (b bool) { v.To(&b); return b }
func (v Value) Bytes() []byte { return v.raw }
func (v Value) Array() (values []lorca.Value) {
array := []json.RawMessage{}
_ = v.To(&array)
for _, el := range array {
values = append(values, Value{raw: el})
}
return values
}
func (v Value) Object() (object map[string]lorca.Value) {
object = map[string]lorca.Value{}
kv := map[string]json.RawMessage{}
_ = v.To(&kv)
for k, v := range kv {
object[k] = Value{raw: v}
}
return object
}