summaryrefslogtreecommitdiff
path: root/tests/tapdance/test_examples.cpp
blob: e67e6cb9076bf2833fa17d9df813383fff6643a7 (plain)
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
/* Copyright 2022 Jouke Witteveen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "keyboard_report_util.hpp"
#include "keycode.h"
#include "test_common.hpp"
#include "action_tapping.h"
#include "test_keymap_key.hpp"
#include "examples.h"

using testing::_;
using testing::InSequence;

class TapDance : public TestFixture {};

TEST_F(TapDance, DoubleTap) {
    TestDriver driver;
    InSequence s;
    auto       key_esc_caps = KeymapKey{0, 1, 0, TD(TD_ESC_CAPS)};

    set_keymap({key_esc_caps});

    /* The tap dance key does nothing on the first press */
    key_esc_caps.press();
    run_one_scan_loop();
    key_esc_caps.release();
    EXPECT_NO_REPORT(driver);

    /* We get the key press and the release on timeout */
    idle_for(TAPPING_TERM);
    EXPECT_REPORT(driver, (KC_ESC));
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();

    /* Double tap gets us the second key */
    tap_key(key_esc_caps);
    EXPECT_NO_REPORT(driver);
    key_esc_caps.press();
    EXPECT_REPORT(driver, (KC_CAPS));
    run_one_scan_loop();
    key_esc_caps.release();
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();
}

TEST_F(TapDance, DoubleTapWithMod) {
    TestDriver driver;
    InSequence s;
    auto       key_esc_caps = KeymapKey{0, 1, 0, TD(TD_ESC_CAPS)};
    auto       key_shift    = KeymapKey{0, 2, 0, KC_LSFT};

    set_keymap({key_esc_caps, key_shift});

    /* The tap dance key does nothing on the first press */
    key_shift.press();
    EXPECT_REPORT(driver, (KC_LSFT));
    run_one_scan_loop();
    key_esc_caps.press();
    run_one_scan_loop();

    key_esc_caps.release();
    key_shift.release();
    EXPECT_EMPTY_REPORT(driver);

    /* We get the key press and the release */
    idle_for(TAPPING_TERM);
    EXPECT_REPORT(driver, (KC_LSFT));
    EXPECT_REPORT(driver, (KC_LSFT, KC_ESC));
    EXPECT_REPORT(driver, (KC_LSFT));
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();

    /* Double tap gets us the second key */
    key_shift.press();
    EXPECT_REPORT(driver, (KC_LSFT));
    run_one_scan_loop();
    tap_key(key_esc_caps);
    EXPECT_NO_REPORT(driver);
    key_shift.release();
    key_esc_caps.press();
    EXPECT_REPORT(driver, (KC_LSFT, KC_CAPS));
    run_one_scan_loop();
    key_esc_caps.release();
    EXPECT_REPORT(driver, (KC_LSFT));
    run_one_scan_loop();
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();
}

TEST_F(TapDance, DoubleTapInterrupted) {
    TestDriver driver;
    InSequence s;
    auto       key_esc_caps = KeymapKey{0, 1, 0, TD(TD_ESC_CAPS)};
    auto       regular_key  = KeymapKey(0, 2, 0, KC_A);

    set_keymap({key_esc_caps, regular_key});

    /* Interrupted double tap */
    tap_key(key_esc_caps);
    regular_key.press();
    /* Immediate tap of the first key */
    EXPECT_REPORT(driver, (KC_ESC));
    EXPECT_EMPTY_REPORT(driver);
    /* Followed by the interrupting key */
    EXPECT_REPORT(driver, (KC_A));
    run_one_scan_loop();
    regular_key.release();
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();

    /* Second tap after being interrupted acts as a single tap */
    key_esc_caps.press();
    run_one_scan_loop();
    key_esc_caps.release();
    idle_for(TAPPING_TERM);
    EXPECT_REPORT(driver, (KC_ESC));
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();
}

TEST_F(TapDance, DanceFn) {
    TestDriver driver;
    InSequence s;
    auto       key_egg = KeymapKey(0, 1, 0, TD(CT_EGG));

    set_keymap({key_egg});

    /* 99 taps do nothing */
    for (int i = 0; i < 99; i++) {
        run_one_scan_loop();
        key_egg.press();
        run_one_scan_loop();
        key_egg.release();
    }
    idle_for(TAPPING_TERM);
    EXPECT_NO_REPORT(driver);
    run_one_scan_loop();

    /* 100 taps trigger the action */
    for (int i = 0; i < 100; i++) {
        run_one_scan_loop();
        key_egg.press();
        run_one_scan_loop();
        key_egg.release();
    }
    idle_for(TAPPING_TERM);
    EXPECT_REPORT(driver, (KC_C));
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();

    /* 250 taps act the same as 100 taps */
    /* Taps are counted in an uint8_t, so the count overflows after 255 taps */
    for (int i = 0; i < 250; i++) {
        run_one_scan_loop();
        key_egg.press();
        run_one_scan_loop();
        key_egg.release();
    }
    idle_for(TAPPING_TERM);
    EXPECT_REPORT(driver, (KC_C));
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();
}

TEST_F(TapDance, DanceFnAdvanced) {
    TestDriver driver;
    InSequence s;
    auto       key_flsh = KeymapKey(0, 1, 0, TD(CT_FLSH));

    set_keymap({key_flsh});

    /* Three taps don't trigger a reset */
    EXPECT_REPORT(driver, (KC_3));
    EXPECT_REPORT(driver, (KC_3, KC_2));
    EXPECT_REPORT(driver, (KC_3, KC_2, KC_1));
    for (int i = 0; i < 3; i++) {
        run_one_scan_loop();
        key_flsh.press();
        run_one_scan_loop();
        key_flsh.release();
    }
    idle_for(TAPPING_TERM);
    EXPECT_REPORT(driver, (KC_3, KC_2));
    EXPECT_REPORT(driver, (KC_3));
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();

    /* Four taps trigger a reset */
    EXPECT_REPORT(driver, (KC_3));
    EXPECT_REPORT(driver, (KC_3, KC_2));
    EXPECT_REPORT(driver, (KC_3, KC_2, KC_1));
    EXPECT_REPORT(driver, (KC_2, KC_1));
    EXPECT_REPORT(driver, (KC_1));
    EXPECT_EMPTY_REPORT(driver);
    for (int i = 0; i < 4; i++) {
        run_one_scan_loop();
        key_flsh.press();
        run_one_scan_loop();
        key_flsh.release();
    }
    idle_for(TAPPING_TERM);
    EXPECT_REPORT(driver, (KC_R));
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();
}

TEST_F(TapDance, TapHold) {
    TestDriver driver;
    InSequence s;
    auto       key_cln = KeymapKey{0, 1, 0, TD(CT_CLN)};

    set_keymap({key_cln});

    /* Short taps fire on release */
    key_cln.press();
    run_one_scan_loop();
    key_cln.release();
    EXPECT_REPORT(driver, (KC_LSFT));
    EXPECT_REPORT(driver, (KC_LSFT, KC_SCLN));
    EXPECT_REPORT(driver, (KC_LSFT));
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();

    /* Holds immediate following a tap apply to the tap key */
    key_cln.press();
    EXPECT_REPORT(driver, (KC_LSFT));
    EXPECT_REPORT(driver, (KC_LSFT, KC_SCLN));
    idle_for(TAPPING_TERM * 2);
    key_cln.release();
    EXPECT_REPORT(driver, (KC_LSFT));
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();

    /* Holds trigger the hold key */
    key_cln.press();
    idle_for(TAPPING_TERM);
    run_one_scan_loop();
    EXPECT_REPORT(driver, (KC_SCLN));
    run_one_scan_loop();
    key_cln.release();
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();
}

TEST_F(TapDance, QuadFunction) {
    TestDriver driver;
    InSequence s;
    auto       key_quad    = KeymapKey{0, 1, 0, TD(X_CTL)};
    auto       regular_key = KeymapKey(0, 2, 0, KC_A);

    set_keymap({key_quad, regular_key});

    /* Single tap */
    key_quad.press();
    run_one_scan_loop();
    key_quad.release();
    idle_for(TAPPING_TERM);
    EXPECT_REPORT(driver, (KC_X));
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();

    /* Single hold */
    key_quad.press();
    run_one_scan_loop();
    idle_for(TAPPING_TERM);
    EXPECT_REPORT(driver, (KC_LCTL));
    run_one_scan_loop();
    key_quad.release();
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();

    /* Double tap */
    tap_key(key_quad);
    key_quad.press();
    run_one_scan_loop();
    key_quad.release();
    idle_for(TAPPING_TERM);
    EXPECT_REPORT(driver, (KC_ESC));
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();

    /* Double tap and hold */
    tap_key(key_quad);
    key_quad.press();
    run_one_scan_loop();
    idle_for(TAPPING_TERM);
    EXPECT_REPORT(driver, (KC_LALT));
    run_one_scan_loop();
    key_quad.release();
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();

    /* Double single tap */
    tap_key(key_quad);
    tap_key(key_quad);
    regular_key.press();
    EXPECT_REPORT(driver, (KC_X));
    EXPECT_EMPTY_REPORT(driver);
    EXPECT_REPORT(driver, (KC_X));
    EXPECT_EMPTY_REPORT(driver);
    EXPECT_REPORT(driver, (KC_A));
    run_one_scan_loop();
    regular_key.release();
    EXPECT_EMPTY_REPORT(driver);
    run_one_scan_loop();
}