forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiichuck_driver_test.go
320 lines (240 loc) · 8.44 KB
/
wiichuck_driver_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package i2c
import (
"errors"
"strings"
"testing"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*WiichuckDriver)(nil)
// --------- HELPERS
func initTestWiichuckDriver() (driver *WiichuckDriver) {
driver, _ = initTestWiichuckDriverWithStubbedAdaptor()
return
}
func initTestWiichuckDriverWithStubbedAdaptor() (*WiichuckDriver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor()
return NewWiichuckDriver(adaptor), adaptor
}
// --------- TESTS
func TestNewWiichuckDriver(t *testing.T) {
// Does it return a pointer to an instance of WiichuckDriver?
var bm interface{} = NewWiichuckDriver(newI2cTestAdaptor())
_, ok := bm.(*WiichuckDriver)
if !ok {
t.Errorf("NewWiichuckDriver() should have returned a *WiichuckDriver")
}
}
func TestWiichuckDriver(t *testing.T) {
wii := initTestWiichuckDriver()
gobottest.Refute(t, wii.Connection(), nil)
gobottest.Assert(t, wii.interval, 10*time.Millisecond)
wii = NewWiichuckDriver(newI2cTestAdaptor())
gobottest.Assert(t, strings.HasPrefix(wii.Name(), "Wiichuck"), true)
}
func TestWiichuckDriverStart(t *testing.T) {
sem := make(chan bool)
wii, adaptor := initTestWiichuckDriverWithStubbedAdaptor()
adaptor.Testi2cReadImpl(func(b []byte) (int, error) {
copy(b, []byte{1, 2, 3, 4, 5, 6})
return 6, nil
})
numberOfCyclesForEvery := 3
wii.interval = 1 * time.Millisecond
gobottest.Assert(t, wii.Start(), nil)
go func() {
for {
time.Sleep(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
j := wii.Joystick()
if (j["sy_origin"] == float64(44)) &&
(j["sx_origin"] == float64(45)) {
sem <- true
return
}
}
}()
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("origin not read correctly")
}
}
func TestWiichuckStartConnectError(t *testing.T) {
d, adaptor := initTestWiichuckDriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestWiichuckDriverHalt(t *testing.T) {
wii := initTestWiichuckDriver()
gobottest.Assert(t, wii.Halt(), nil)
}
func TestWiichuckDriverCanParse(t *testing.T) {
wii := initTestWiichuckDriver()
// ------ When value is not encrypted
decryptedValue := []byte{1, 2, 3, 4, 5, 6}
wii.update(decryptedValue)
// - This should be done by WiichuckDriver.parse
gobottest.Assert(t, wii.data["sx"], float64(45))
gobottest.Assert(t, wii.data["sy"], float64(44))
gobottest.Assert(t, wii.data["z"], float64(0))
gobottest.Assert(t, wii.data["c"], float64(0))
}
func TestWiichuckDriverCanAdjustOrigins(t *testing.T) {
wii := initTestWiichuckDriver()
// ------ When value is not encrypted
decryptedValue := []byte{1, 2, 3, 4, 5, 6}
wii.update(decryptedValue)
// - This should be done by WiichuckDriver.adjustOrigins
gobottest.Assert(t, wii.Joystick()["sx_origin"], float64(45))
gobottest.Assert(t, wii.Joystick()["sy_origin"], float64(44))
}
func TestWiichuckDriverCButton(t *testing.T) {
wii := initTestWiichuckDriver()
// ------ When value is not encrypted
decryptedValue := []byte{1, 2, 3, 4, 5, 6}
wii.update(decryptedValue)
// - This should be done by WiichuckDriver.updateButtons
done := make(chan bool)
wii.On(wii.Event(C), func(data interface{}) {
gobottest.Assert(t, data, true)
done <- true
})
wii.update(decryptedValue)
select {
case <-done:
case <-time.After(10 * time.Second):
t.Errorf("Did not receive 'C' event")
}
}
func TestWiichuckDriverZButton(t *testing.T) {
wii := initTestWiichuckDriver()
// ------ When value is not encrypted
decryptedValue := []byte{1, 2, 3, 4, 5, 6}
wii.update(decryptedValue)
done := make(chan bool)
wii.On(wii.Event(Z), func(data interface{}) {
gobottest.Assert(t, data, true)
done <- true
})
wii.update(decryptedValue)
select {
case <-done:
case <-time.After(10 * time.Second):
t.Errorf("Did not receive 'Z' event")
}
}
func TestWiichuckDriverUpdateJoystick(t *testing.T) {
wii := initTestWiichuckDriver()
// ------ When value is not encrypted
decryptedValue := []byte{1, 2, 3, 4, 5, 6}
// - This should be done by WiichuckDriver.updateJoystick
expectedData := map[string]float64{
"x": float64(0),
"y": float64(0),
}
done := make(chan bool)
wii.On(wii.Event(Joystick), func(data interface{}) {
gobottest.Assert(t, data, expectedData)
done <- true
})
wii.update(decryptedValue)
select {
case <-done:
case <-time.After(10 * time.Second):
t.Errorf("Did not receive 'Joystick' event")
}
}
func TestWiichuckDriverEncrypted(t *testing.T) {
wii := initTestWiichuckDriver()
// ------ When value is encrypted
wii = initTestWiichuckDriver()
encryptedValue := []byte{1, 1, 2, 2, 3, 3}
wii.update(encryptedValue)
gobottest.Assert(t, wii.data["sx"], float64(0))
gobottest.Assert(t, wii.data["sy"], float64(0))
gobottest.Assert(t, wii.data["z"], float64(0))
gobottest.Assert(t, wii.data["c"], float64(0))
gobottest.Assert(t, wii.Joystick()["sx_origin"], float64(-1))
gobottest.Assert(t, wii.Joystick()["sy_origin"], float64(-1))
}
func TestWiichuckDriverSetJoystickDefaultValue(t *testing.T) {
wii := initTestWiichuckDriver()
gobottest.Assert(t, wii.Joystick()["sy_origin"], float64(-1))
wii.setJoystickDefaultValue("sy_origin", float64(2))
gobottest.Assert(t, wii.Joystick()["sy_origin"], float64(2))
// when current default value is not -1 it keeps the current value
wii.setJoystickDefaultValue("sy_origin", float64(20))
gobottest.Assert(t, wii.Joystick()["sy_origin"], float64(2))
}
func TestWiichuckDriverCalculateJoystickValue(t *testing.T) {
wii := initTestWiichuckDriver()
gobottest.Assert(t, wii.calculateJoystickValue(float64(20), float64(5)), float64(15))
gobottest.Assert(t, wii.calculateJoystickValue(float64(1), float64(2)), float64(-1))
gobottest.Assert(t, wii.calculateJoystickValue(float64(10), float64(5)), float64(5))
gobottest.Assert(t, wii.calculateJoystickValue(float64(5), float64(10)), float64(-5))
}
func TestWiichuckDriverIsEncrypted(t *testing.T) {
wii := initTestWiichuckDriver()
encryptedValue := []byte{1, 1, 2, 2, 3, 3}
gobottest.Assert(t, wii.isEncrypted(encryptedValue), true)
encryptedValue = []byte{42, 42, 24, 24, 30, 30}
gobottest.Assert(t, wii.isEncrypted(encryptedValue), true)
decryptedValue := []byte{1, 2, 3, 4, 5, 6}
gobottest.Assert(t, wii.isEncrypted(decryptedValue), false)
decryptedValue = []byte{1, 1, 2, 2, 5, 6}
gobottest.Assert(t, wii.isEncrypted(decryptedValue), false)
decryptedValue = []byte{1, 1, 2, 3, 3, 3}
gobottest.Assert(t, wii.isEncrypted(decryptedValue), false)
}
func TestWiichuckDriverDecode(t *testing.T) {
wii := initTestWiichuckDriver()
gobottest.Assert(t, wii.decode(byte(0)), float64(46))
gobottest.Assert(t, wii.decode(byte(100)), float64(138))
gobottest.Assert(t, wii.decode(byte(200)), float64(246))
gobottest.Assert(t, wii.decode(byte(254)), float64(0))
}
func TestWiichuckDriverParse(t *testing.T) {
wii := initTestWiichuckDriver()
gobottest.Assert(t, wii.data["sx"], float64(0))
gobottest.Assert(t, wii.data["sy"], float64(0))
gobottest.Assert(t, wii.data["z"], float64(0))
gobottest.Assert(t, wii.data["c"], float64(0))
// First pass
wii.parse([]byte{12, 23, 34, 45, 56, 67})
gobottest.Assert(t, wii.data["sx"], float64(50))
gobottest.Assert(t, wii.data["sy"], float64(23))
gobottest.Assert(t, wii.data["z"], float64(1))
gobottest.Assert(t, wii.data["c"], float64(2))
// Second pass
wii.parse([]byte{70, 81, 92, 103, 204, 205})
gobottest.Assert(t, wii.data["sx"], float64(104))
gobottest.Assert(t, wii.data["sy"], float64(93))
gobottest.Assert(t, wii.data["z"], float64(1))
gobottest.Assert(t, wii.data["c"], float64(0))
}
func TestWiichuckDriverAdjustOrigins(t *testing.T) {
wii := initTestWiichuckDriver()
gobottest.Assert(t, wii.Joystick()["sy_origin"], float64(-1))
gobottest.Assert(t, wii.Joystick()["sx_origin"], float64(-1))
// First pass
wii.parse([]byte{1, 2, 3, 4, 5, 6})
wii.adjustOrigins()
gobottest.Assert(t, wii.Joystick()["sy_origin"], float64(44))
gobottest.Assert(t, wii.Joystick()["sx_origin"], float64(45))
// Second pass
wii = initTestWiichuckDriver()
wii.parse([]byte{61, 72, 83, 94, 105, 206})
wii.adjustOrigins()
gobottest.Assert(t, wii.Joystick()["sy_origin"], float64(118))
gobottest.Assert(t, wii.Joystick()["sx_origin"], float64(65))
}
func TestWiichuckDriverSetName(t *testing.T) {
d := initTestWiichuckDriver()
d.SetName("TESTME")
gobottest.Assert(t, d.Name(), "TESTME")
}
func TestWiichuckDriverOptions(t *testing.T) {
d := NewWiichuckDriver(newI2cTestAdaptor(), WithBus(2))
gobottest.Assert(t, d.GetBusOrDefault(1), 2)
}