forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakey_button_driver_test.go
117 lines (94 loc) · 2.45 KB
/
makey_button_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
package gpio
import (
"errors"
"testing"
"time"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/gobottest"
)
var _ gobot.Driver = (*MakeyButtonDriver)(nil)
const makeyTestDelay = 250
func initTestMakeyButtonDriver() *MakeyButtonDriver {
return NewMakeyButtonDriver(newGpioTestAdaptor(), "1")
}
func TestMakeyButtonDriverHalt(t *testing.T) {
d := initTestMakeyButtonDriver()
done := make(chan struct{})
go func() {
<-d.halt
close(done)
}()
gobottest.Assert(t, d.Halt(), nil)
select {
case <-done:
case <-time.After(makeyTestDelay * time.Millisecond):
t.Errorf("MakeyButton was not halted")
}
}
func TestMakeyButtonDriver(t *testing.T) {
d := initTestMakeyButtonDriver()
gobottest.Assert(t, d.Pin(), "1")
gobottest.Refute(t, d.Connection(), nil)
gobottest.Assert(t, d.interval, 10*time.Millisecond)
d = NewMakeyButtonDriver(newGpioTestAdaptor(), "1", 30*time.Second)
gobottest.Assert(t, d.interval, 30*time.Second)
}
func TestMakeyButtonDriverStart(t *testing.T) {
sem := make(chan bool)
a := newGpioTestAdaptor()
d := NewMakeyButtonDriver(a, "1")
gobottest.Assert(t, d.Start(), nil)
d.Once(ButtonPush, func(data interface{}) {
gobottest.Assert(t, d.Active, true)
sem <- true
})
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
val = 0
return
})
select {
case <-sem:
case <-time.After(makeyTestDelay * time.Millisecond):
t.Errorf("MakeyButton Event \"Push\" was not published")
}
d.Once(ButtonRelease, func(data interface{}) {
gobottest.Assert(t, d.Active, false)
sem <- true
})
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
val = 1
return
})
select {
case <-sem:
case <-time.After(makeyTestDelay * time.Millisecond):
t.Errorf("MakeyButton Event \"Release\" was not published")
}
d.Once(Error, func(data interface{}) {
gobottest.Assert(t, data.(error).Error(), "digital read error")
sem <- true
})
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
err = errors.New("digital read error")
return
})
select {
case <-sem:
case <-time.After(makeyTestDelay * time.Millisecond):
t.Errorf("MakeyButton Event \"Error\" was not published")
}
// send a halt message
d.Once(ButtonRelease, func(data interface{}) {
sem <- true
})
a.TestAdaptorDigitalRead(func(string) (val int, err error) {
val = 1
return
})
d.halt <- true
select {
case <-sem:
t.Errorf("MakeyButton Event should not have been published")
case <-time.After(makeyTestDelay * time.Millisecond):
}
}