-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathble_client_adaptor_test.go
407 lines (395 loc) · 9.9 KB
/
ble_client_adaptor_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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package bleclient
import (
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
)
var (
_ gobot.Adaptor = (*Adaptor)(nil)
_ gobot.BLEConnector = (*Adaptor)(nil)
)
func TestNewAdaptor(t *testing.T) {
a := NewAdaptor("D7:99:5A:26:EC:38")
assert.Equal(t, "D7:99:5A:26:EC:38", a.Address())
assert.True(t, strings.HasPrefix(a.Name(), "BLEClient"))
}
func TestName(t *testing.T) {
a := NewAdaptor("D7:99:5A:26:EC:38")
a.SetName("awesome")
assert.Equal(t, "awesome", a.Name())
}
func TestConnect(t *testing.T) {
const (
scanTimeout = 5 * time.Millisecond
deviceName = "hello"
deviceAddress = "11:22:44:AA:BB:CC"
rssi = 56
)
tests := map[string]struct {
identifier string
extAdapter *btTestAdapter
extDevice btTestDevice
wantAddress string
wantName string
wantErr string
}{
"connect_by_address": {
identifier: deviceAddress,
extAdapter: &btTestAdapter{
deviceAddress: deviceAddress,
rssi: rssi,
payload: &btTestPayload{name: deviceName},
},
extDevice: btTestDevice{},
wantAddress: deviceAddress,
wantName: deviceName,
},
"connect_by_name": {
identifier: deviceName,
extAdapter: &btTestAdapter{
deviceAddress: deviceAddress,
rssi: rssi,
payload: &btTestPayload{name: deviceName},
},
extDevice: btTestDevice{},
wantAddress: deviceAddress,
wantName: deviceName,
},
"error_enable": {
extAdapter: &btTestAdapter{
simulateEnableErr: true,
},
wantName: "BLEClient",
wantErr: "can't get adapter default: adapter enable error",
},
"error_scan": {
extAdapter: &btTestAdapter{
simulateScanErr: true,
},
wantName: "BLEClient",
wantErr: "scan error",
},
"error_stop_scan": {
extAdapter: &btTestAdapter{
deviceAddress: deviceAddress,
payload: &btTestPayload{},
simulateStopScanErr: true,
},
wantName: "BLEClient",
wantErr: "stop scan error",
},
"error_timeout_long_delay": {
extAdapter: &btTestAdapter{
deviceAddress: deviceAddress,
payload: &btTestPayload{},
scanDelay: 2 * scanTimeout,
},
wantName: "BLEClient",
wantErr: "scan timeout (5ms) elapsed",
},
"error_timeout_bad_identifier": {
identifier: "bad_identifier",
extAdapter: &btTestAdapter{
deviceAddress: deviceAddress,
payload: &btTestPayload{},
},
wantAddress: "bad_identifier",
wantName: "BLEClient",
wantErr: "scan timeout (5ms) elapsed",
},
"error_connect": {
extAdapter: &btTestAdapter{
deviceAddress: deviceAddress,
payload: &btTestPayload{},
simulateConnectErr: true,
},
wantName: "BLEClient",
wantErr: "adapter connect error",
},
"error_discovery_services": {
identifier: "disco_err",
extAdapter: &btTestAdapter{
deviceAddress: deviceAddress,
payload: &btTestPayload{name: "disco_err"},
},
extDevice: btTestDevice{
simulateDiscoverServicesErr: true,
},
wantAddress: deviceAddress,
wantName: "disco_err",
wantErr: "device discover services error",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a := NewAdaptor(tc.identifier)
btdc := func(_ bluetoothExtDevicer, address, name string) *btDevice {
return &btDevice{extDevice: tc.extDevice, devAddress: address, devName: name}
}
btac := func(bluetoothExtAdapterer, bool) *btAdapter {
return &btAdapter{extAdapter: tc.extAdapter, btDeviceCreator: btdc}
}
a.btAdptCreator = btac
a.cfg.scanTimeout = scanTimeout // to speed up test
// act
err := a.Connect()
// assert
if tc.wantErr == "" {
require.NoError(t, err)
assert.Equal(t, tc.wantName, a.Name())
assert.Equal(t, tc.wantAddress, a.Address())
assert.Equal(t, rssi, a.RSSI())
assert.True(t, a.connected)
} else {
require.ErrorContains(t, err, tc.wantErr)
assert.Contains(t, a.Name(), tc.wantName)
assert.Equal(t, tc.wantAddress, a.Address())
assert.False(t, a.connected)
}
})
}
}
func TestReconnect(t *testing.T) {
const (
scanTimeout = 5 * time.Millisecond
deviceName = "hello"
deviceAddress = "11:22:44:AA:BB:CC"
rssi = 56
)
tests := map[string]struct {
extAdapter *btTestAdapter
extDevice *btTestDevice
wasConnected bool
wantErr string
}{
"reconnect_not_connected": {
extAdapter: &btTestAdapter{
deviceAddress: deviceAddress,
rssi: rssi,
payload: &btTestPayload{name: deviceName},
},
extDevice: &btTestDevice{},
},
"reconnect_was_connected": {
extAdapter: &btTestAdapter{
deviceAddress: deviceAddress,
rssi: rssi,
payload: &btTestPayload{name: deviceName},
},
extDevice: &btTestDevice{},
wasConnected: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a := NewAdaptor(deviceAddress)
btdc := func(_ bluetoothExtDevicer, address, name string) *btDevice {
return &btDevice{extDevice: tc.extDevice, devAddress: address, devName: name}
}
a.btAdpt = &btAdapter{extAdapter: tc.extAdapter, btDeviceCreator: btdc}
a.cfg.scanTimeout = scanTimeout // to speed up test in case of errors
a.cfg.sleepAfterDisconnect = 0 // to speed up test
if tc.wasConnected {
a.btDevice = btdc(nil, "", "")
a.connected = tc.wasConnected
}
// act
err := a.Reconnect()
// assert
if tc.wantErr == "" {
require.NoError(t, err)
assert.Equal(t, rssi, a.RSSI())
} else {
require.ErrorContains(t, err, tc.wantErr)
}
assert.True(t, a.connected)
})
}
}
func TestFinalize(t *testing.T) {
// this also tests Disconnect()
tests := map[string]struct {
extDevice *btTestDevice
wantErr string
}{
"disconnect": {
extDevice: &btTestDevice{},
},
"error_disconnect": {
extDevice: &btTestDevice{
simulateDisconnectErr: true,
},
wantErr: "device disconnect error",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a := NewAdaptor("")
a.cfg.sleepAfterDisconnect = 0 // to speed up test
a.btDevice = &btDevice{extDevice: tc.extDevice}
// act
err := a.Finalize()
// assert
if tc.wantErr == "" {
require.NoError(t, err)
} else {
require.ErrorContains(t, err, tc.wantErr)
}
assert.False(t, a.connected)
})
}
}
func TestReadCharacteristic(t *testing.T) {
const uuid = "00001234-0000-1000-8000-00805f9b34fb"
tests := map[string]struct {
inUUID string
chara *btTestChara
notConnected bool
want []byte
wantErr string
}{
"read_ok": {
inUUID: uuid,
chara: &btTestChara{readData: []byte{1, 2, 3}},
want: []byte{1, 2, 3},
},
"error_not_connected": {
notConnected: true,
wantErr: "cannot read from BLE device until connected",
},
"error_bad_chara": {
inUUID: "gag1",
wantErr: "'gag1' is not a valid 16-bit Bluetooth UUID",
},
"error_unknown_chara": {
inUUID: uuid,
wantErr: "unknown characteristic: 00001234-0000-1000-8000-00805f9b34fb",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a := NewAdaptor("")
if tc.chara != nil {
a.characteristics[uuid] = tc.chara
}
a.connected = !tc.notConnected
// act
got, err := a.ReadCharacteristic(tc.inUUID)
// assert
if tc.wantErr == "" {
require.NoError(t, err)
} else {
require.ErrorContains(t, err, tc.wantErr)
}
assert.Equal(t, tc.want, got)
})
}
}
func TestWriteCharacteristic(t *testing.T) {
const uuid = "00004321-0000-1000-8000-00805f9b34fb"
tests := map[string]struct {
inUUID string
inData []byte
notConnected bool
chara *btTestChara
want []byte
wantErr string
}{
"write_ok": {
inUUID: uuid,
inData: []byte{3, 2, 1},
chara: &btTestChara{},
want: []byte{3, 2, 1},
},
"error_not_connected": {
notConnected: true,
wantErr: "cannot write to BLE device until connected",
},
"error_bad_chara": {
inUUID: "gag2",
wantErr: "'gag2' is not a valid 16-bit Bluetooth UUID",
},
"error_unknown_chara": {
inUUID: uuid,
wantErr: "unknown characteristic: 00004321-0000-1000-8000-00805f9b34fb",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a := NewAdaptor("")
if tc.chara != nil {
a.characteristics[uuid] = tc.chara
}
a.connected = !tc.notConnected
// act
err := a.WriteCharacteristic(tc.inUUID, tc.inData)
// assert
if tc.wantErr == "" {
require.NoError(t, err)
assert.Equal(t, tc.want, tc.chara.writtenData)
} else {
require.ErrorContains(t, err, tc.wantErr)
}
})
}
}
func TestSubscribe(t *testing.T) {
const uuid = "00004321-0000-1000-8000-00805f9b34fb"
tests := map[string]struct {
inUUID string
notConnected bool
chara *btTestChara
want []byte
wantErr string
}{
"subscribe_ok": {
inUUID: uuid,
chara: &btTestChara{},
want: []byte{3, 4, 5},
},
"error_not_connected": {
notConnected: true,
wantErr: "cannot subscribe to BLE device until connected",
},
"error_bad_chara": {
inUUID: "gag2",
wantErr: "'gag2' is not a valid 16-bit Bluetooth UUID",
},
"error_unknown_chara": {
inUUID: uuid,
wantErr: "unknown characteristic: 00004321-0000-1000-8000-00805f9b34fb",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a := NewAdaptor("")
if tc.chara != nil {
a.characteristics[uuid] = tc.chara
}
a.connected = !tc.notConnected
var got []byte
notificationFunc := func(data []byte) {
got = append(got, data...)
}
// act
err := a.Subscribe(tc.inUUID, notificationFunc)
// assert
if tc.wantErr == "" {
require.NoError(t, err)
tc.chara.notificationFunc([]byte{3, 4, 5})
} else {
require.ErrorContains(t, err, tc.wantErr)
}
assert.Equal(t, tc.want, got)
})
}
}