-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton_test.go
More file actions
62 lines (53 loc) · 1.47 KB
/
button_test.go
File metadata and controls
62 lines (53 loc) · 1.47 KB
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
package main
import (
"testing"
"time"
)
func testBoolChan(b chan bool, timeout time.Duration) bool {
for {
select {
case val := <-b:
return val
case <-time.After(timeout):
return false
}
}
}
func TestFakeButton(t *testing.T) {
Info("Running %s", t.Name())
timeout := 50 * time.Millisecond
pushed := make(chan bool)
pin, _ := NewTestPin(99).(*TestPin)
pin.sleepTime = time.Second * 20 // Don't accidentally wake up and send a signal
button := newButton(pin, func() {
pushed <- true
})
button.Start()
t.Run("FalsePush", func(t *testing.T) {
pin.state = Low // Not a push
button.pushed = time.Now().Add(-1 * button.bouncetime) // Not a bounce
pin.wake <- true
if testBoolChan(pushed, timeout) != true {
t.Errorf("Expected pushed(true), found false")
}
})
t.Run("QuickPush", func(t *testing.T) {
pin.state = High // Push
button.pushed = time.Now().Add((-1 * button.bouncetime) / 2) // Bounce
pin.wake <- true
if testBoolChan(pushed, timeout) != false {
t.Errorf("Expected pushed(false), found true")
}
})
t.Run("Push", func(t *testing.T) {
tm := time.Now().Add(-1 * button.bouncetime)
pin.state = High // Push
button.pushed = tm // Not a bounce
pin.wake <- true
if testBoolChan(pushed, timeout) != false {
t.Errorf("Expected pushed(false), found true, pushed_t(%s), now(%s)",
timeStr(button.pushed), timeStr(tm))
}
})
button.Stop()
}