@@ -13,6 +13,8 @@ const CONFIG: ButtonConfig = ButtonConfig {
13
13
mode : Mode :: PullUp ,
14
14
} ;
15
15
16
+ const IMMEDIATELY : Duration = Duration :: from_millis ( 0 ) ;
17
+
16
18
#[ tokio:: test( start_paused = true ) ]
17
19
async fn start_idle ( ) {
18
20
let expectations = [ Transaction :: new ( Kind :: Get ( PinState :: High ) ) ] ;
@@ -42,9 +44,9 @@ async fn start_pressed() {
42
44
#[ tokio:: test]
43
45
async fn single_press ( ) {
44
46
let expectations = [
45
- Transaction :: wait_for_state ( PinState :: Low , Duration :: from_millis ( 10 ) ) ,
47
+ Transaction :: wait_for_state ( PinState :: Low , IMMEDIATELY ) ,
46
48
Transaction :: get ( PinState :: Low ) ,
47
- Transaction :: wait_for_state ( PinState :: High , Duration :: from_millis ( 10 ) ) ,
49
+ Transaction :: wait_for_state ( PinState :: High , IMMEDIATELY ) ,
48
50
Transaction :: get ( PinState :: High ) ,
49
51
] ;
50
52
let pin = Mock :: new ( & expectations) ;
@@ -66,10 +68,10 @@ async fn single_press() {
66
68
#[ tokio:: test]
67
69
async fn long_press ( ) {
68
70
let expectations = [
69
- Transaction :: wait_for_state ( PinState :: Low , Duration :: from_millis ( 10 ) ) ,
71
+ Transaction :: wait_for_state ( PinState :: Low , IMMEDIATELY ) ,
70
72
Transaction :: get ( PinState :: Low ) ,
71
73
Transaction :: wait_for_state ( PinState :: High , Duration :: from_millis ( 250 ) ) ,
72
- Transaction :: wait_for_state ( PinState :: High , Duration :: from_millis ( 10 ) ) ,
74
+ Transaction :: wait_for_state ( PinState :: High , IMMEDIATELY ) ,
73
75
Transaction :: get ( PinState :: High ) ,
74
76
] ;
75
77
let pin = Mock :: new ( & expectations) ;
@@ -94,7 +96,7 @@ async fn long_press() {
94
96
#[ tokio:: test]
95
97
async fn debounce_press ( ) {
96
98
let expectations = [
97
- Transaction :: wait_for_state ( PinState :: Low , Duration :: from_millis ( 10 ) ) ,
99
+ Transaction :: wait_for_state ( PinState :: Low , IMMEDIATELY ) ,
98
100
Transaction :: get ( PinState :: High ) ,
99
101
] ;
100
102
let pin = Mock :: new ( & expectations) ;
@@ -111,7 +113,7 @@ async fn debounce_press() {
111
113
#[ tokio:: test]
112
114
async fn debounce_release ( ) {
113
115
let expectations = [
114
- Transaction :: wait_for_state ( PinState :: High , Duration :: from_millis ( 10 ) ) ,
116
+ Transaction :: wait_for_state ( PinState :: High , IMMEDIATELY ) ,
115
117
Transaction :: get ( PinState :: Low ) ,
116
118
] ;
117
119
let pin = Mock :: new ( & expectations) ;
@@ -124,3 +126,43 @@ async fn debounce_release() {
124
126
125
127
button. pin . done ( ) ;
126
128
}
129
+
130
+ #[ tokio:: test]
131
+ async fn double_click ( ) {
132
+ let expectations = [
133
+ Transaction :: wait_for_state ( PinState :: Low , IMMEDIATELY ) ,
134
+ Transaction :: get ( PinState :: Low ) ,
135
+ Transaction :: wait_for_state ( PinState :: High , IMMEDIATELY ) ,
136
+ Transaction :: get ( PinState :: High ) ,
137
+ Transaction :: wait_for_state ( PinState :: Low , IMMEDIATELY ) ,
138
+ Transaction :: get ( PinState :: Low ) ,
139
+ Transaction :: wait_for_state ( PinState :: High , IMMEDIATELY ) ,
140
+ Transaction :: get ( PinState :: High ) ,
141
+ Transaction :: wait_for_state ( PinState :: Low , Duration :: from_millis ( 250 ) ) ,
142
+ ] ;
143
+ let pin = Mock :: new ( & expectations) ;
144
+ let mut button = Button :: new ( pin, CONFIG ) ;
145
+ button. state = State :: Idle ;
146
+
147
+ let event = button. update_step ( ) . await ;
148
+ assert_none ! ( event) ;
149
+ assert_matches ! ( button. state, State :: Pressed ) ;
150
+
151
+ let event = button. update_step ( ) . await ;
152
+ assert_none ! ( event) ;
153
+ assert_matches ! ( button. state, State :: Released ) ;
154
+
155
+ let event = button. update_step ( ) . await ;
156
+ assert_none ! ( event) ;
157
+ assert_matches ! ( button. state, State :: Pressed ) ;
158
+
159
+ let event = button. update_step ( ) . await ;
160
+ assert_none ! ( event) ;
161
+ assert_matches ! ( button. state, State :: Released ) ;
162
+
163
+ let event = button. update_step ( ) . await ;
164
+ assert_some_eq ! ( event, ButtonEvent :: ShortPress { count: 2 } ) ;
165
+ assert_matches ! ( button. state, State :: Idle ) ;
166
+
167
+ button. pin . done ( ) ;
168
+ }
0 commit comments