keyboard stuff
at master 271 lines 8.0 kB view raw
1/* Copyright 2021 Simon Arlott 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17#include "gtest/gtest.h" 18 19#include "debounce_test_common.h" 20 21TEST_F(DebounceTest, OneKeyShort1) { 22 addEvents({ 23 /* Time, Inputs, Outputs */ 24 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 25 {1, {{0, 1, UP}}, {}}, 26 27 {5, {}, {{0, 1, UP}}}, 28 /* Press key again after 1ms delay (debounce has not yet finished) */ 29 {6, {{0, 1, DOWN}}, {}}, 30 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 31 }); 32 runEvents(); 33} 34 35TEST_F(DebounceTest, OneKeyShort2) { 36 addEvents({ 37 /* Time, Inputs, Outputs */ 38 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 39 {1, {{0, 1, UP}}, {}}, 40 41 {5, {}, {{0, 1, UP}}}, 42 /* Press key again after 2ms delay (debounce has not yet finished) */ 43 {7, {{0, 1, DOWN}}, {}}, 44 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 45 }); 46 runEvents(); 47} 48 49TEST_F(DebounceTest, OneKeyShort3) { 50 addEvents({ 51 /* Time, Inputs, Outputs */ 52 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 53 {1, {{0, 1, UP}}, {}}, 54 55 {5, {}, {{0, 1, UP}}}, 56 /* Press key again after 3ms delay (debounce has not yet finished) */ 57 {8, {{0, 1, DOWN}}, {}}, 58 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 59 }); 60 runEvents(); 61} 62 63TEST_F(DebounceTest, OneKeyShort4) { 64 addEvents({ 65 /* Time, Inputs, Outputs */ 66 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 67 {1, {{0, 1, UP}}, {}}, 68 69 {5, {}, {{0, 1, UP}}}, 70 /* Press key again after 4ms delay (debounce has not yet finished) */ 71 {9, {{0, 1, DOWN}}, {}}, 72 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 73 }); 74 runEvents(); 75} 76 77TEST_F(DebounceTest, OneKeyShort5) { 78 addEvents({ 79 /* Time, Inputs, Outputs */ 80 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 81 {1, {{0, 1, UP}}, {}}, 82 83 {5, {}, {{0, 1, UP}}}, 84 /* Press key again after 5ms delay (debounce has finished) */ 85 {10, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 86 }); 87 runEvents(); 88} 89 90TEST_F(DebounceTest, OneKeyShort6) { 91 addEvents({ 92 /* Time, Inputs, Outputs */ 93 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 94 {1, {{0, 1, UP}}, {}}, 95 96 {5, {}, {{0, 1, UP}}}, 97 /* Press key after after 6ms delay (debounce has finished) */ 98 {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 99 }); 100 runEvents(); 101} 102 103TEST_F(DebounceTest, OneKeyBouncing1) { 104 addEvents({ 105 /* Time, Inputs, Outputs */ 106 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 107 {1, {{0, 1, UP}}, {}}, 108 {2, {{0, 1, DOWN}}, {}}, 109 {3, {{0, 1, UP}}, {}}, 110 {4, {{0, 1, DOWN}}, {}}, 111 {5, {{0, 1, UP}}, {{0, 1, UP}}}, 112 /* Press key again after 1ms delay (debounce has not yet finished) */ 113 {6, {{0, 1, DOWN}}, {}}, 114 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 115 }); 116 runEvents(); 117} 118 119TEST_F(DebounceTest, OneKeyBouncing2) { 120 addEvents({ 121 /* Time, Inputs, Outputs */ 122 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 123 /* Change twice in the same time period */ 124 {1, {{0, 1, UP}}, {}}, 125 {1, {{0, 1, DOWN}}, {}}, 126 /* Change three times in the same time period */ 127 {2, {{0, 1, UP}}, {}}, 128 {2, {{0, 1, DOWN}}, {}}, 129 {2, {{0, 1, UP}}, {}}, 130 /* Change three times in the same time period */ 131 {3, {{0, 1, DOWN}}, {}}, 132 {3, {{0, 1, UP}}, {}}, 133 {3, {{0, 1, DOWN}}, {}}, 134 /* Change twice in the same time period */ 135 {4, {{0, 1, UP}}, {}}, 136 {4, {{0, 1, DOWN}}, {}}, 137 {5, {{0, 1, UP}}, {{0, 1, UP}}}, 138 /* Press key again after 1ms delay (debounce has not yet finished) */ 139 {6, {{0, 1, DOWN}}, {}}, 140 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 141 }); 142 runEvents(); 143} 144 145TEST_F(DebounceTest, OneKeyLong) { 146 addEvents({ 147 /* Time, Inputs, Outputs */ 148 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 149 150 {25, {{0, 1, UP}}, {{0, 1, UP}}}, 151 152 {50, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 153 }); 154 runEvents(); 155} 156 157TEST_F(DebounceTest, TwoKeysShort) { 158 addEvents({ 159 /* Time, Inputs, Outputs */ 160 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 161 {1, {{0, 1, UP}}, {}}, 162 {2, {{0, 2, DOWN}}, {{0, 2, DOWN}}}, 163 {3, {{0, 2, UP}}, {}}, 164 165 {5, {}, {{0, 1, UP}}}, 166 /* Press key again after 1ms delay (debounce has not yet finished) */ 167 {6, {{0, 1, DOWN}}, {}}, 168 {7, {}, {{0, 2, UP}}}, 169 170 /* Press key again after 1ms delay (debounce has not yet finished) */ 171 {9, {{0, 2, DOWN}}, {}}, 172 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 173 174 {12, {}, {{0, 2, DOWN}}}, /* 5ms after UP at time 7 */ 175 }); 176 runEvents(); 177} 178 179TEST_F(DebounceTest, OneKeyDelayedScan1) { 180 addEvents({ 181 /* Time, Inputs, Outputs */ 182 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 183 184 /* Processing is very late but the change will now be accepted */ 185 {300, {{0, 1, UP}}, {{0, 1, UP}}}, 186 }); 187 time_jumps_ = true; 188 runEvents(); 189} 190 191TEST_F(DebounceTest, OneKeyDelayedScan2) { 192 addEvents({ 193 /* Time, Inputs, Outputs */ 194 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 195 196 /* Processing is very late but the change will now be accepted even with a 1 scan delay */ 197 {300, {}, {}}, 198 {300, {{0, 1, UP}}, {{0, 1, UP}}}, 199 }); 200 time_jumps_ = true; 201 runEvents(); 202} 203 204TEST_F(DebounceTest, OneKeyDelayedScan3) { 205 addEvents({ 206 /* Time, Inputs, Outputs */ 207 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 208 209 /* Processing is very late but the change will now be accepted even with a 1ms delay */ 210 {300, {}, {}}, 211 {301, {{0, 1, UP}}, {{0, 1, UP}}}, 212 }); 213 time_jumps_ = true; 214 runEvents(); 215} 216 217TEST_F(DebounceTest, OneKeyDelayedScan4) { 218 addEvents({ 219 /* Time, Inputs, Outputs */ 220 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 221 222 /* Processing is a bit late but the change will now be accepted */ 223 {50, {{0, 1, UP}}, {{0, 1, UP}}}, 224 }); 225 time_jumps_ = true; 226 runEvents(); 227} 228 229TEST_F(DebounceTest, OneKeyDelayedScan5) { 230 addEvents({ 231 /* Time, Inputs, Outputs */ 232 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 233 234 /* Processing is very late but the change will now be accepted even with a 1 scan delay */ 235 {50, {}, {}}, 236 {50, {{0, 1, UP}}, {{0, 1, UP}}}, 237 }); 238 time_jumps_ = true; 239 runEvents(); 240} 241 242TEST_F(DebounceTest, OneKeyDelayedScan6) { 243 addEvents({ 244 /* Time, Inputs, Outputs */ 245 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 246 247 /* Processing is very late but the change will now be accepted even with a 1ms delay */ 248 {50, {}, {}}, 249 {51, {{0, 1, UP}}, {{0, 1, UP}}}, 250 }); 251 time_jumps_ = true; 252 runEvents(); 253} 254 255TEST_F(DebounceTest, AsyncTickOneKeyShort1) { 256 addEvents({ 257 /* Time, Inputs, Outputs */ 258 {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, 259 {1, {{0, 1, UP}}, {}}, 260 261 {5, {}, {{0, 1, UP}}}, 262 /* Press key again after 1ms delay (debounce has not yet finished) */ 263 {6, {{0, 1, DOWN}}, {}}, 264 {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ 265 }); 266 /* 267 * Debounce implementations should never read the timer more than once per invocation 268 */ 269 async_time_jumps_ = DEBOUNCE; 270 runEvents(); 271}