diff options
Diffstat (limited to 'tests')
29 files changed, 1984 insertions, 107 deletions
diff --git a/tests/basic/test_action_layer.cpp b/tests/basic/test_action_layer.cpp index b7ecfa52ef..0aa4b78007 100644 --- a/tests/basic/test_action_layer.cpp +++ b/tests/basic/test_action_layer.cpp @@ -365,9 +365,10 @@ TEST_F(ActionLayer, LayerTapReleasedBeforeKeypressReleaseWithModifiers) { InSequence s; KeymapKey layer_0_key_0 = KeymapKey{0, 0, 0, LT(1, KC_T)}; + KeymapKey layer_0_key_1 = KeymapKey{0, 1, 0, KC_X}; KeymapKey layer_1_key_1 = KeymapKey{1, 1, 0, RALT(KC_9)}; - set_keymap({layer_0_key_0, layer_1_key_1}); + set_keymap({layer_0_key_0, layer_0_key_1, layer_1_key_1}); /* Press layer tap and wait for tapping term to switch to layer 1 */ EXPECT_NO_REPORT(driver); diff --git a/tests/basic/test_one_shot_keys.cpp b/tests/basic/test_one_shot_keys.cpp index 2401c2c837..2a3434bf16 100644 --- a/tests/basic/test_one_shot_keys.cpp +++ b/tests/basic/test_one_shot_keys.cpp @@ -160,6 +160,150 @@ INSTANTIATE_TEST_CASE_P( )); // clang-format on +TEST_F(OneShot, OSMChainingTwoOSMs) { + TestDriver driver; + InSequence s; + KeymapKey osm_key1 = KeymapKey{0, 0, 0, OSM(MOD_LSFT), KC_LSFT}; + KeymapKey osm_key2 = KeymapKey{0, 0, 1, OSM(MOD_LCTL), KC_LCTL}; + KeymapKey regular_key = KeymapKey{0, 1, 0, KC_A}; + + set_keymap({osm_key1, osm_key2, regular_key}); + + /* Press and release OSM1 */ + EXPECT_NO_REPORT(driver); + osm_key1.press(); + run_one_scan_loop(); + osm_key1.release(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); + + /* Press and relesea OSM2 */ + EXPECT_NO_REPORT(driver); + osm_key2.press(); + run_one_scan_loop(); + osm_key2.release(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); + + /* Press regular key */ + EXPECT_REPORT(driver, (osm_key1.report_code, osm_key2.report_code, regular_key.report_code)).Times(1); + regular_key.press(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); + + /* Release regular key */ + EXPECT_EMPTY_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); +} + +TEST_F(OneShot, OSMDoubleTapNotLockingOSMs) { + TestDriver driver; + InSequence s; + KeymapKey osm_key1 = KeymapKey{0, 0, 0, OSM(MOD_LSFT), KC_LSFT}; + KeymapKey osm_key2 = KeymapKey{0, 0, 1, OSM(MOD_LCTL), KC_LCTL}; + KeymapKey regular_key = KeymapKey{0, 1, 0, KC_A}; + + set_keymap({osm_key1, osm_key2, regular_key}); + + /* Press and release OSM1 */ + EXPECT_NO_REPORT(driver); + osm_key1.press(); + run_one_scan_loop(); + osm_key1.release(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); + + /* Press and release OSM2 twice */ + EXPECT_NO_REPORT(driver); + osm_key2.press(); + run_one_scan_loop(); + osm_key2.release(); + run_one_scan_loop(); + osm_key2.press(); + run_one_scan_loop(); + osm_key2.release(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); + + /* Press regular key */ + EXPECT_REPORT(driver, (osm_key1.report_code, osm_key2.report_code, regular_key.report_code)).Times(1); + regular_key.press(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); + + /* Release regular key */ + EXPECT_EMPTY_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); + + /* Press regular key */ + EXPECT_REPORT(driver, (regular_key.report_code)).Times(1); + regular_key.press(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); + + /* Release regular key */ + EXPECT_EMPTY_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); +} + +TEST_F(OneShot, OSMHoldNotLockingOSMs) { + TestDriver driver; + InSequence s; + KeymapKey osm_key1 = KeymapKey{0, 0, 0, OSM(MOD_LSFT), KC_LSFT}; + KeymapKey osm_key2 = KeymapKey{0, 0, 1, OSM(MOD_LCTL), KC_LCTL}; + KeymapKey regular_key = KeymapKey{0, 1, 0, KC_A}; + + set_keymap({osm_key1, osm_key2, regular_key}); + + /* Press and release OSM1 */ + EXPECT_NO_REPORT(driver); + osm_key1.press(); + run_one_scan_loop(); + osm_key1.release(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); + + /* Press and hold OSM2 */ + EXPECT_REPORT(driver, (osm_key1.report_code, osm_key2.report_code)).Times(1); + osm_key2.press(); + run_one_scan_loop(); + idle_for(TAPPING_TERM); + VERIFY_AND_CLEAR(driver); + + /* Press and release regular key */ + EXPECT_REPORT(driver, (osm_key1.report_code, osm_key2.report_code, regular_key.report_code)).Times(1); + EXPECT_REPORT(driver, (osm_key2.report_code)).Times(1); + regular_key.press(); + run_one_scan_loop(); + regular_key.release(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); + + /* Release OSM2 */ + EXPECT_EMPTY_REPORT(driver); + osm_key2.release(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); + + /* Press regular key */ + EXPECT_REPORT(driver, (regular_key.report_code)).Times(1); + regular_key.press(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); + + /* Release regular key */ + EXPECT_EMPTY_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + VERIFY_AND_CLEAR(driver); +} + TEST_F(OneShot, OSLWithAdditionalKeypress) { TestDriver driver; InSequence s; diff --git a/tests/caps_word/caps_word_combo/test.mk b/tests/caps_word/caps_word_combo/test.mk index 9f2e157189..c294864113 100644 --- a/tests/caps_word/caps_word_combo/test.mk +++ b/tests/caps_word/caps_word_combo/test.mk @@ -17,3 +17,4 @@ CAPS_WORD_ENABLE = yes COMBO_ENABLE = yes AUTO_SHIFT_ENABLE = yes +INTROSPECTION_KEYMAP_C = test_combos.c diff --git a/tests/caps_word/caps_word_combo/test_caps_word_combo.cpp b/tests/caps_word/caps_word_combo/test_caps_word_combo.cpp index 0876cc91a3..2cee203dfd 100644 --- a/tests/caps_word/caps_word_combo/test_caps_word_combo.cpp +++ b/tests/caps_word/caps_word_combo/test_caps_word_combo.cpp @@ -38,29 +38,6 @@ using ::testing::AnyOf; using ::testing::InSequence; using ::testing::TestParamInfo; -extern "C" { -// Define some combos to use for the test, including overlapping combos and -// combos that chord tap-hold keys. -enum combo_events { AB_COMBO, BC_COMBO, AD_COMBO, DE_COMBO, FGHI_COMBO, COMBO_LENGTH }; -uint16_t COMBO_LEN = COMBO_LENGTH; - -const uint16_t ab_combo[] PROGMEM = {KC_A, KC_B, COMBO_END}; -const uint16_t bc_combo[] PROGMEM = {KC_B, KC_C, COMBO_END}; -const uint16_t ad_combo[] PROGMEM = {KC_A, LCTL_T(KC_D), COMBO_END}; -const uint16_t de_combo[] PROGMEM = {LCTL_T(KC_D), LT(1, KC_E), COMBO_END}; -const uint16_t fghi_combo[] PROGMEM = {KC_F, KC_G, KC_H, KC_I, COMBO_END}; - -// clang-format off -combo_t key_combos[] = { - [AB_COMBO] = COMBO(ab_combo, KC_SPC), // KC_A + KC_B = KC_SPC - [BC_COMBO] = COMBO(bc_combo, KC_X), // KC_B + KC_C = KC_X - [AD_COMBO] = COMBO(ad_combo, KC_Y), // KC_A + LCTL_T(KC_D) = KC_Y - [DE_COMBO] = COMBO(de_combo, KC_Z), // LCTL_T(KC_D) + LT(1, KC_E) = KC_Z - [FGHI_COMBO] = COMBO(fghi_combo, KC_W) // KC_F + KC_G + KC_H + KC_I = KC_W -}; -// clang-format on -} // extern "C" - namespace { // To test combos thorougly, we test them with pressing the chord keys with diff --git a/tests/caps_word/caps_word_combo/test_combos.c b/tests/caps_word/caps_word_combo/test_combos.c new file mode 100644 index 0000000000..1d07118d50 --- /dev/null +++ b/tests/caps_word/caps_word_combo/test_combos.c @@ -0,0 +1,20 @@ +#include <quantum.h> + +// Define some combos to use for the test, including overlapping combos and +// combos that chord tap-hold keys. +enum combo_events { AB_COMBO, BC_COMBO, AD_COMBO, DE_COMBO, FGHI_COMBO }; + +const uint16_t ab_combo[] PROGMEM = {KC_A, KC_B, COMBO_END}; +const uint16_t bc_combo[] PROGMEM = {KC_B, KC_C, COMBO_END}; +const uint16_t ad_combo[] PROGMEM = {KC_A, LCTL_T(KC_D), COMBO_END}; +const uint16_t de_combo[] PROGMEM = {LCTL_T(KC_D), LT(1, KC_E), COMBO_END}; +const uint16_t fghi_combo[] PROGMEM = {KC_F, KC_G, KC_H, KC_I, COMBO_END}; + +// clang-format off +combo_t key_combos[] = { + [AB_COMBO] = COMBO(ab_combo, KC_SPC), // KC_A + KC_B = KC_SPC + [BC_COMBO] = COMBO(bc_combo, KC_X), // KC_B + KC_C = KC_X + [AD_COMBO] = COMBO(ad_combo, KC_Y), // KC_A + LCTL_T(KC_D) = KC_Y + [DE_COMBO] = COMBO(de_combo, KC_Z), // LCTL_T(KC_D) + LT(1, KC_E) = KC_Z + [FGHI_COMBO] = COMBO(fghi_combo, KC_W) // KC_F + KC_G + KC_H + KC_I = KC_W +}; diff --git a/tests/caps_word/caps_word_invert_on_shift/config.h b/tests/caps_word/caps_word_invert_on_shift/config.h new file mode 100644 index 0000000000..7a3ec846f9 --- /dev/null +++ b/tests/caps_word/caps_word_invert_on_shift/config.h @@ -0,0 +1,21 @@ +// Copyright 2023 Google LLC +// +// 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/>. + +#pragma once + +#include "test_common.h" + +#define CAPS_WORD_INVERT_ON_SHIFT +#define PERMISSIVE_HOLD diff --git a/tests/caps_word/caps_word_invert_on_shift/test.mk b/tests/caps_word/caps_word_invert_on_shift/test.mk new file mode 100644 index 0000000000..319c04d67a --- /dev/null +++ b/tests/caps_word/caps_word_invert_on_shift/test.mk @@ -0,0 +1,17 @@ +# Copyright 2023 Google LLC +# +# 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/>. + +CAPS_WORD_ENABLE = yes + diff --git a/tests/caps_word/caps_word_invert_on_shift/test_caps_word_invert_on_shift.cpp b/tests/caps_word/caps_word_invert_on_shift/test_caps_word_invert_on_shift.cpp new file mode 100644 index 0000000000..d322448181 --- /dev/null +++ b/tests/caps_word/caps_word_invert_on_shift/test_caps_word_invert_on_shift.cpp @@ -0,0 +1,215 @@ +// Copyright 2023 Google LLC +// +// 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 "test_fixture.hpp" +#include "test_keymap_key.hpp" + +using ::testing::_; +using ::testing::AnyNumber; +using ::testing::AnyOf; +using ::testing::InSequence; +using ::testing::TestParamInfo; + +namespace { + +struct ShiftKeyParams { + std::string name; + uint16_t keycode; + uint16_t report_shift_code; + + static const std::string& GetName(const TestParamInfo<ShiftKeyParams>& info) { + return info.param.name; + } +}; + +class CapsWordInvertOnShift : public ::testing::WithParamInterface<ShiftKeyParams>, public TestFixture { + void SetUp() override { + caps_word_off(); + } +}; + +// With Caps Word on, type "A, 4, Shift(A, 4, A), A, Shift(A), 4". +TEST_P(CapsWordInvertOnShift, ShiftWithinWord) { + TestDriver driver; + KeymapKey key_shift(0, 0, 0, GetParam().keycode); + KeymapKey key_a(0, 1, 0, KC_A); + KeymapKey key_4(0, 2, 0, KC_4); + set_keymap({key_shift, key_a, key_4}); + + // Allow any number of reports with no keys or only KC_LSFT. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT)))) + .Times(AnyNumber()); + // clang-format on + + { // Expect: "A4a$aAa4" + InSequence s; + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_4)); + EXPECT_REPORT(driver, (KC_A)); + EXPECT_REPORT(driver, (KC_LSFT, KC_4)); + EXPECT_REPORT(driver, (KC_A)); + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_A)); + EXPECT_REPORT(driver, (KC_4)); + } + + caps_word_on(); + tap_keys(key_a, key_4); // Type "A, 4". + + key_shift.press(); // Type "Shift(A, 4, A)". + run_one_scan_loop(); + tap_keys(key_a, key_4, key_a); + key_shift.release(); + run_one_scan_loop(); + + tap_key(key_a); // Type "A". + + key_shift.press(); // Type "Shift(A)". + run_one_scan_loop(); + tap_key(key_a); + key_shift.release(); + run_one_scan_loop(); + + tap_key(key_4); // Type "4". + + VERIFY_AND_CLEAR(driver); +} + +TEST_P(CapsWordInvertOnShift, ShiftHeldAtWordEnd) { + TestDriver driver; + KeymapKey key_shift(0, 0, 0, GetParam().keycode); + KeymapKey key_a(0, 1, 0, KC_A); + KeymapKey key_slsh(0, 2, 0, KC_SLSH); + set_keymap({key_shift, key_a, key_slsh}); + + // Allow any number of reports with no keys or only KC_LSFT. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT), + KeyboardReport(KC_RSFT)))) + .Times(AnyNumber()); + // clang-format on + + { // Expect: "Aa?A" + InSequence s; + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_A)); + EXPECT_REPORT(driver, (GetParam().report_shift_code, KC_SLSH)); + EXPECT_REPORT(driver, (GetParam().report_shift_code, KC_A)); + } + + caps_word_on(); + tap_key(key_a); + + key_shift.press(); // Press Shift. + run_one_scan_loop(); + + EXPECT_EQ(get_mods(), 0); + + tap_key(key_a); + tap_key(key_slsh); // Tap '/' key, which is word breaking, ending Caps Word. + + EXPECT_FALSE(is_caps_word_on()); + EXPECT_EQ(get_mods(), MOD_BIT(GetParam().report_shift_code)); + + tap_key(key_a); + key_shift.release(); // Release Shift. + run_one_scan_loop(); + + EXPECT_EQ(get_mods(), 0); + VERIFY_AND_CLEAR(driver); +} + +TEST_P(CapsWordInvertOnShift, TwoShiftsHeld) { + TestDriver driver; + KeymapKey key_shift1(0, 0, 0, GetParam().keycode); + KeymapKey key_shift2(0, 1, 0, GetParam().report_shift_code); + KeymapKey key_a(0, 2, 0, KC_A); + KeymapKey key_slsh(0, 3, 0, KC_SLSH); + set_keymap({key_shift1, key_shift2, key_a, key_slsh}); + + // Allow any number of reports with no keys or only KC_LSFT. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT), + KeyboardReport(KC_RSFT)))) + .Times(AnyNumber()); + // clang-format on + + { // Expect: "Aa?a" + InSequence s; + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_A)); + EXPECT_REPORT(driver, (GetParam().report_shift_code, KC_SLSH)); + EXPECT_REPORT(driver, (KC_A)); + } + + caps_word_on(); + tap_key(key_a); + + key_shift1.press(); // Press shift1. + run_one_scan_loop(); + + EXPECT_EQ(get_mods(), 0); + + tap_key(key_a); + tap_key(key_slsh); // Tap '/' key, which is word breaking, ending Caps Word. + + EXPECT_FALSE(is_caps_word_on()); + EXPECT_EQ(get_mods(), MOD_BIT(GetParam().report_shift_code)); + + key_shift2.press(); // Press shift2. + run_one_scan_loop(); + + EXPECT_EQ(get_mods(), MOD_BIT(GetParam().report_shift_code)); + + key_shift1.release(); // Release shift1. + run_one_scan_loop(); + + EXPECT_EQ(get_mods(), 0); + tap_key(key_a); + + key_shift2.release(); // Release shift2. + run_one_scan_loop(); + + EXPECT_EQ(get_mods(), 0); + VERIFY_AND_CLEAR(driver); +} + +// clang-format off +INSTANTIATE_TEST_CASE_P( + Shifts, + CapsWordInvertOnShift, + ::testing::Values( + ShiftKeyParams{"KC_LSFT", KC_LSFT, KC_LSFT}, + ShiftKeyParams{"KC_RSFT", KC_RSFT, KC_RSFT}, + ShiftKeyParams{"LSFT_T", LSFT_T(KC_A), KC_LSFT}, + ShiftKeyParams{"RSFT_T", RSFT_T(KC_A), KC_RSFT}, + ShiftKeyParams{"OSM_LSFT", OSM(MOD_LSFT), KC_LSFT}, + ShiftKeyParams{"OSM_RSFT", OSM(MOD_RSFT), KC_RSFT} + ), + ShiftKeyParams::GetName + ); +// clang-format on + +} // namespace diff --git a/tests/caps_word/test_caps_word.cpp b/tests/caps_word/test_caps_word.cpp index 6d38b383f3..802f1e960e 100644 --- a/tests/caps_word/test_caps_word.cpp +++ b/tests/caps_word/test_caps_word.cpp @@ -371,6 +371,11 @@ INSTANTIATE_TEST_CASE_P( "OSL", OSL(1), 1, KC_NO, true}, CapsWordPressUserParams{ "LT_held", LT_1_KC_A, TAPPING_TERM + 1, KC_NO, true}, + // Tri-Layer keys are ignored and continue Caps Word. + CapsWordPressUserParams{ + "TL_LOWR", TL_LOWR, 1, KC_NO, true}, + CapsWordPressUserParams{ + "TL_UPPR", TL_UPPR, 1, KC_NO, true}, // AltGr keys are ignored and continue Caps Word. CapsWordPressUserParams{ "KC_RALT", KC_RALT, 1, KC_NO, true}, diff --git a/tests/combo/config.h b/tests/combo/config.h new file mode 100644 index 0000000000..8052932634 --- /dev/null +++ b/tests/combo/config.h @@ -0,0 +1,8 @@ +// Copyright 2023 Stefan Kerkmann (@KarlK90) +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "test_common.h" + +#define TAPPING_TERM 200 diff --git a/tests/combo/test.mk b/tests/combo/test.mk new file mode 100644 index 0000000000..4776b9d0c4 --- /dev/null +++ b/tests/combo/test.mk @@ -0,0 +1,6 @@ +# Copyright 2023 Stefan Kerkmann (@KarlK90) +# SPDX-License-Identifier: GPL-2.0-or-later + +COMBO_ENABLE = yes + +INTROSPECTION_KEYMAP_C = test_combos.c diff --git a/tests/combo/test_combo.cpp b/tests/combo/test_combo.cpp new file mode 100644 index 0000000000..ac852f9d16 --- /dev/null +++ b/tests/combo/test_combo.cpp @@ -0,0 +1,57 @@ +// Copyright 2023 Stefan Kerkmann (@KarlK90) +// Copyright 2023 @filterpaper +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "keyboard_report_util.hpp" +#include "quantum.h" +#include "keycode.h" +#include "test_common.h" +#include "test_driver.hpp" +#include "test_fixture.hpp" +#include "test_keymap_key.hpp" + +using testing::_; +using testing::InSequence; + +class Combo : public TestFixture {}; + +TEST_F(Combo, combo_modtest_tapped) { + TestDriver driver; + KeymapKey key_y(0, 0, 1, KC_Y); + KeymapKey key_u(0, 0, 2, KC_U); + set_keymap({key_y, key_u}); + + EXPECT_REPORT(driver, (KC_SPACE)); + EXPECT_EMPTY_REPORT(driver); + tap_combo({key_y, key_u}); + VERIFY_AND_CLEAR(driver); +} + +TEST_F(Combo, combo_modtest_held_longer_than_tapping_term) { + TestDriver driver; + KeymapKey key_y(0, 0, 1, KC_Y); + KeymapKey key_u(0, 0, 2, KC_U); + set_keymap({key_y, key_u}); + + EXPECT_REPORT(driver, (KC_RIGHT_SHIFT)); + EXPECT_EMPTY_REPORT(driver); + tap_combo({key_y, key_u}, TAPPING_TERM + 1); + VERIFY_AND_CLEAR(driver); +} + +TEST_F(Combo, combo_osmshift_tapped) { + TestDriver driver; + KeymapKey key_z(0, 0, 1, KC_Z); + KeymapKey key_x(0, 0, 2, KC_X); + KeymapKey key_i(0, 0, 3, KC_I); + set_keymap({key_z, key_x, key_i}); + + EXPECT_NO_REPORT(driver); + tap_combo({key_z, key_x}); + VERIFY_AND_CLEAR(driver); + + EXPECT_REPORT(driver, (KC_I, KC_LEFT_SHIFT)); + EXPECT_EMPTY_REPORT(driver); + tap_key(key_i); + VERIFY_AND_CLEAR(driver); +} diff --git a/tests/combo/test_combos.c b/tests/combo/test_combos.c new file mode 100644 index 0000000000..8dcb364c6e --- /dev/null +++ b/tests/combo/test_combos.c @@ -0,0 +1,17 @@ +// Copyright 2023 Stefan Kerkmann (@KarlK90) +// Copyright 2023 @filterpaper +// Copyright 2023 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later +#include "quantum.h" + +enum combos { modtest, osmshift }; + +uint16_t const modtest_combo[] = {KC_Y, KC_U, COMBO_END}; +uint16_t const osmshift_combo[] = {KC_Z, KC_X, COMBO_END}; + +// clang-format off +combo_t key_combos[] = { + [modtest] = COMBO(modtest_combo, RSFT_T(KC_SPACE)), + [osmshift] = COMBO(osmshift_combo, OSM(MOD_LSFT)) +}; +// clang-format on diff --git a/tests/repeat_key/alt_repeat_key/config.h b/tests/repeat_key/alt_repeat_key/config.h new file mode 100644 index 0000000000..d0c4ddadbd --- /dev/null +++ b/tests/repeat_key/alt_repeat_key/config.h @@ -0,0 +1,18 @@ +// Copyright 2023 Google LLC +// +// 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/>. + +#pragma once + +#include "test_common.h" diff --git a/tests/repeat_key/alt_repeat_key/test.mk b/tests/repeat_key/alt_repeat_key/test.mk new file mode 100644 index 0000000000..080871c816 --- /dev/null +++ b/tests/repeat_key/alt_repeat_key/test.mk @@ -0,0 +1,18 @@ +# Copyright 2023 Google LLC +# +# 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/>. + +REPEAT_KEY_ENABLE = yes + +EXTRAKEY_ENABLE = yes diff --git a/tests/repeat_key/alt_repeat_key/test_alt_repeat_key.cpp b/tests/repeat_key/alt_repeat_key/test_alt_repeat_key.cpp new file mode 100644 index 0000000000..ae525acb45 --- /dev/null +++ b/tests/repeat_key/alt_repeat_key/test_alt_repeat_key.cpp @@ -0,0 +1,523 @@ +// Copyright 2023 Google LLC +// +// 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 <functional> + +#include "keyboard_report_util.hpp" +#include "keycode.h" +#include "test_common.hpp" +#include "test_fixture.hpp" +#include "test_keymap_key.hpp" + +using ::testing::AnyNumber; +using ::testing::InSequence; + +namespace { + +bool process_record_user_default(uint16_t keycode, keyrecord_t* record) { + return true; +} + +bool remember_last_key_user_default(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) { + return true; +} + +uint16_t get_alt_repeat_key_keycode_user_default(uint16_t keycode, uint8_t mods) { + return KC_TRNS; +} + +// Indirections so that process_record_user() can be replaced with other +// functions in the test cases below. +std::function<bool(uint16_t, keyrecord_t*)> process_record_user_fun = process_record_user_default; +std::function<bool(uint16_t, keyrecord_t*, uint8_t*)> remember_last_key_user_fun = remember_last_key_user_default; +std::function<uint16_t(uint16_t, uint8_t)> get_alt_repeat_key_keycode_user_fun = get_alt_repeat_key_keycode_user_default; + +extern "C" bool process_record_user(uint16_t keycode, keyrecord_t* record) { + return process_record_user_fun(keycode, record); +} + +extern "C" bool remember_last_key_user(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) { + return remember_last_key_user_fun(keycode, record, remembered_mods); +} + +extern "C" uint16_t get_alt_repeat_key_keycode_user(uint16_t keycode, uint8_t mods) { + return get_alt_repeat_key_keycode_user_fun(keycode, mods); +} + +class AltRepeatKey : public TestFixture { + public: + bool process_record_user_was_called_; + + void SetUp() override { + process_record_user_fun = process_record_user_default; + remember_last_key_user_fun = remember_last_key_user_default; + get_alt_repeat_key_keycode_user_fun = get_alt_repeat_key_keycode_user_default; + } + + void ExpectProcessRecordUserCalledWith(bool expected_press, uint16_t expected_keycode, int8_t expected_repeat_key_count) { + process_record_user_was_called_ = false; + process_record_user_fun = [=](uint16_t keycode, keyrecord_t* record) { + EXPECT_EQ(record->event.pressed, expected_press); + EXPECT_KEYCODE_EQ(keycode, expected_keycode); + EXPECT_EQ(get_repeat_key_count(), expected_repeat_key_count); + // Tests below use this to verify process_record_user() was called. + process_record_user_was_called_ = true; + return true; + }; + } + + // Expects that the characters of `s` are sent. + // NOTE: This implementation is limited to chars a-z, A-Z. + void ExpectString(TestDriver& driver, const std::string& s) { + InSequence seq; + for (int c : s) { + switch (c) { + case 'a' ... 'z': { // Lowercase letter. + uint16_t keycode = c - ('a' - KC_A); + EXPECT_REPORT(driver, (keycode)); + } break; + + case 'A' ... 'Z': { // Capital letter = KC_LSFT + letter key. + uint16_t keycode = c - ('A' - KC_A); + EXPECT_REPORT(driver, (KC_LSFT, keycode)); + } break; + } + } + } +}; + +TEST_F(AltRepeatKey, AlternateBasic) { + TestDriver driver; + KeymapKey key_bspc(0, 0, 0, KC_BSPC); + KeymapKey key_pgdn(0, 1, 0, KC_PGDN); + KeymapKey key_pgup(0, 2, 0, KC_PGUP); + KeymapKey key_repeat(0, 4, 0, QK_REP); + KeymapKey key_alt_repeat(0, 5, 0, QK_AREP); + set_keymap({key_bspc, key_pgdn, key_pgup, key_repeat, key_alt_repeat}); + + // Allow any number of empty reports. + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + { + InSequence seq; + EXPECT_REPORT(driver, (KC_BSPC)); + EXPECT_REPORT(driver, (KC_DEL)); + EXPECT_REPORT(driver, (KC_DEL)); + EXPECT_REPORT(driver, (KC_BSPC)); + EXPECT_REPORT(driver, (KC_DEL)); + EXPECT_REPORT(driver, (KC_PGDN)); + EXPECT_REPORT(driver, (KC_PGUP)); + EXPECT_REPORT(driver, (KC_PGUP)); + EXPECT_REPORT(driver, (KC_PGDN)); + } + + tap_key(key_bspc); + + for (int n = 1; n <= 2; ++n) { // Tap the Alternate Repeat Key twice. + ExpectProcessRecordUserCalledWith(true, KC_DEL, -n); + key_alt_repeat.press(); // Press the Alternate Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + // Expect the corresponding release event. + ExpectProcessRecordUserCalledWith(false, KC_DEL, -n); + key_alt_repeat.release(); // Release the Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + } + + process_record_user_fun = process_record_user_default; + tap_keys(key_repeat, key_alt_repeat); + tap_keys(key_pgdn, key_alt_repeat); + tap_keys(key_pgup, key_alt_repeat); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +struct TestParamsAlternateKeyCodes { + uint16_t keycode; + uint8_t mods; + uint16_t expected_alt_keycode; +}; + +// Tests `get_alt_repeat_key_keycode()` for various keycodes. +TEST_F(AltRepeatKey, GetAltRepeatKeyKeycode) { + for (const auto& params : std::vector<TestParamsAlternateKeyCodes>({ + // clang-format off + // Each line tests one call to `get_alt_repeat_key_keycode()`: + // {keycode, mods, expected_alt_keycode}. + // Arrows. + {KC_LEFT, 0, KC_RGHT}, + {KC_RGHT, 0, KC_LEFT}, + {KC_LEFT, MOD_BIT(KC_LSFT), LSFT(KC_RGHT)}, + {KC_LEFT, MOD_BIT(KC_RSFT), RSFT(KC_RGHT)}, + {KC_LEFT, MOD_BIT(KC_LCTL) | MOD_BIT(KC_LSFT), C(S(KC_RGHT))}, + {KC_LEFT, MOD_BIT(KC_LGUI), LGUI(KC_RGHT)}, + {C(KC_LEFT), MOD_BIT(KC_LSFT), C(S(KC_RGHT))}, + {KC_UP, 0, KC_DOWN}, + // Navigation keys. + {KC_PGUP, 0, KC_PGDN}, + {KC_HOME, 0, KC_END }, + // Media keys. + {KC_WBAK, 0, KC_WFWD}, + {KC_MNXT, 0, KC_MPRV}, + {KC_MRWD, 0, KC_MFFD}, + {KC_VOLU, 0, KC_VOLD}, + {KC_BRIU, 0, KC_BRID}, + // Emacs navigation. + {KC_N, MOD_BIT(KC_LCTL), C(KC_P)}, + {KC_B, MOD_BIT(KC_LCTL), LCTL(KC_F)}, + {KC_B, MOD_BIT(KC_RCTL), RCTL(KC_F)}, + {KC_B, MOD_BIT(KC_LALT), LALT(KC_F)}, + {KC_F, MOD_BIT(KC_LCTL), C(KC_B)}, + {KC_A, MOD_BIT(KC_LCTL), C(KC_E)}, + {KC_D, MOD_BIT(KC_LCTL), C(KC_U)}, + // Vim navigation. + {KC_J, 0, KC_K}, + {KC_K, 0, KC_J}, + {KC_H, 0, KC_L}, + {KC_B, 0, KC_W}, + {KC_W, 0, KC_B}, + {KC_E, 0, KC_B}, + {KC_B, MOD_BIT(KC_LSFT), S(KC_W)}, + {KC_W, MOD_BIT(KC_LSFT), S(KC_B)}, + {KC_E, MOD_BIT(KC_LSFT), S(KC_B)}, + {KC_O, MOD_BIT(KC_LCTL), C(KC_I)}, + {KC_I, MOD_BIT(KC_LCTL), C(KC_O)}, + // Other. + {KC_DEL, 0, KC_BSPC}, + {KC_LBRC, 0, KC_RBRC}, + {KC_LCBR, 0, KC_RCBR}, + // Some keys where the last key is a tap-hold key. + {LSFT_T(KC_F), MOD_BIT(KC_RCTL), RCTL(KC_B)}, + {LT(1, KC_A), MOD_BIT(KC_RGUI), RGUI(KC_E)}, + {RALT_T(KC_J), 0, KC_K}, + // Some keys where no alternate is defined. + {KC_A, 0, KC_NO}, + {KC_F1, 0, KC_NO}, + {QK_LEAD, 0, KC_NO}, + {MO(1), 0, KC_NO}, + // clang-format on + })) { + SCOPED_TRACE(std::string("Input keycode: ") + get_keycode_identifier_or_default(params.keycode)); + set_last_keycode(params.keycode); + set_last_mods(params.mods); + + const uint16_t actual = get_alt_repeat_key_keycode(); + + EXPECT_KEYCODE_EQ(get_alt_repeat_key_keycode(), params.expected_alt_keycode); + } +} + +// Test adding to and overriding the above through the +// `get_alt_repeat_key_keycode_user()` callback. +TEST_F(AltRepeatKey, GetAltRepeatKeyKeycodeUser) { + get_alt_repeat_key_keycode_user_fun = [](uint16_t keycode, uint8_t mods) -> uint16_t { + bool shifted = (mods & MOD_MASK_SHIFT); + switch (keycode) { + case KC_LEFT: + return KC_ENT; + case MO(1): + return TG(1); + case KC_TAB: // Tab <-> Shift + Tab example. + if (shifted) { + return KC_TAB; + } else { + return S(KC_TAB); + } + } + + // Ctrl + Y <-> Ctrl + Z example. + if ((mods & MOD_MASK_CTRL)) { + switch (keycode) { + case KC_Y: + return C(KC_Z); + case KC_Z: + return C(KC_Y); + } + } + + return KC_NO; + }; + + set_last_keycode(KC_LEFT); + EXPECT_KEYCODE_EQ(get_alt_repeat_key_keycode(), KC_ENT); + + set_last_keycode(MO(1)); + EXPECT_KEYCODE_EQ(get_alt_repeat_key_keycode(), TG(1)); + + set_last_keycode(KC_TAB); + EXPECT_KEYCODE_EQ(get_alt_repeat_key_keycode(), S(KC_TAB)); + + set_last_keycode(KC_TAB); + set_last_mods(MOD_BIT(KC_LSFT)); + EXPECT_KEYCODE_EQ(get_alt_repeat_key_keycode(), KC_TAB); + + set_last_keycode(KC_Z); + set_last_mods(MOD_BIT(KC_LCTL)); + EXPECT_KEYCODE_EQ(get_alt_repeat_key_keycode(), C(KC_Y)); + + set_last_keycode(KC_Y); + set_last_mods(MOD_BIT(KC_LCTL)); + EXPECT_KEYCODE_EQ(get_alt_repeat_key_keycode(), C(KC_Z)); +} + +// Tests rolling from a key to Alternate Repeat. +TEST_F(AltRepeatKey, RollingToAltRepeat) { + TestDriver driver; + KeymapKey key_left(0, 0, 0, KC_LEFT); + KeymapKey key_alt_repeat(0, 1, 0, QK_AREP); + set_keymap({key_left, key_alt_repeat}); + + { + InSequence seq; + EXPECT_REPORT(driver, (KC_LEFT)); + EXPECT_REPORT(driver, (KC_LEFT, KC_RGHT)); + EXPECT_REPORT(driver, (KC_RGHT)); + EXPECT_EMPTY_REPORT(driver); + EXPECT_REPORT(driver, (KC_RGHT)); + EXPECT_EMPTY_REPORT(driver); + } + + // Perform a rolled press from Left to Alternate Repeat. + + ExpectProcessRecordUserCalledWith(true, KC_LEFT, 0); + key_left.press(); + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + ExpectProcessRecordUserCalledWith(true, KC_RGHT, -1); + key_alt_repeat.press(); // Press the Alternate Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + ExpectProcessRecordUserCalledWith(false, KC_LEFT, 0); + key_left.release(); + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + ExpectProcessRecordUserCalledWith(false, KC_RGHT, -1); + key_alt_repeat.release(); // Release the Alternate Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + process_record_user_fun = process_record_user_default; + tap_key(key_alt_repeat); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests rolling from Alternate Repeat to another key. +TEST_F(AltRepeatKey, RollingFromAltRepeat) { + TestDriver driver; + KeymapKey key_left(0, 0, 0, KC_LEFT); + KeymapKey key_up(0, 1, 0, KC_UP); + KeymapKey key_alt_repeat(0, 2, 0, QK_AREP); + set_keymap({key_left, key_up, key_alt_repeat}); + + { + InSequence seq; + EXPECT_REPORT(driver, (KC_LEFT)); + EXPECT_EMPTY_REPORT(driver); + EXPECT_REPORT(driver, (KC_RGHT)); + EXPECT_REPORT(driver, (KC_RGHT, KC_UP)); + EXPECT_REPORT(driver, (KC_UP)); + EXPECT_EMPTY_REPORT(driver); + EXPECT_REPORT(driver, (KC_DOWN)); + EXPECT_EMPTY_REPORT(driver); + } + + tap_key(key_left); + + // Perform a rolled press from Alternate Repeat to Up. + + ExpectProcessRecordUserCalledWith(true, KC_RGHT, -1); + key_alt_repeat.press(); // Press the Alternate Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + ExpectProcessRecordUserCalledWith(true, KC_UP, 0); + key_up.press(); + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + EXPECT_KEYCODE_EQ(get_last_keycode(), KC_UP); + + ExpectProcessRecordUserCalledWith(false, KC_RGHT, -1); + key_alt_repeat.release(); // Release the Alternate Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + ExpectProcessRecordUserCalledWith(false, KC_UP, 0); + key_up.release(); + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + process_record_user_fun = process_record_user_default; + tap_key(key_alt_repeat); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests using the Alternate Repeat Key on a macro that doesn't have an +// alternate keycode defined. +TEST_F(AltRepeatKey, AlternateUnsupportedMacro) { + TestDriver driver; + KeymapKey key_foo(0, 0, 0, QK_USER_0); + KeymapKey key_alt_repeat(0, 1, 0, QK_AREP); + set_keymap({key_foo, key_alt_repeat}); + + process_record_user_fun = [=](uint16_t keycode, keyrecord_t* record) { + process_record_user_was_called_ = true; + switch (keycode) { + case QK_USER_0: + if (record->event.pressed) { + SEND_STRING("foo"); + } + break; + } + return true; + }; + + // Allow any number of empty reports. + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + ExpectString(driver, "foofoo"); + + process_record_user_was_called_ = false; + tap_key(key_foo); + + EXPECT_TRUE(process_record_user_was_called_); + EXPECT_KEYCODE_EQ(get_last_keycode(), QK_USER_0); + EXPECT_KEYCODE_EQ(get_alt_repeat_key_keycode(), KC_NO); + + process_record_user_was_called_ = false; + key_alt_repeat.press(); // Press Alternate Repeat. + run_one_scan_loop(); + + EXPECT_FALSE(process_record_user_was_called_); + + process_record_user_was_called_ = false; + key_alt_repeat.release(); // Release Alternate Repeat. + run_one_scan_loop(); + + EXPECT_FALSE(process_record_user_was_called_); + + process_record_user_was_called_ = false; + tap_key(key_foo); + + EXPECT_TRUE(process_record_user_was_called_); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests a macro with custom alternate behavior. +TEST_F(AltRepeatKey, MacroCustomAlternate) { + TestDriver driver; + KeymapKey key_foo(0, 0, 0, QK_USER_0); + KeymapKey key_alt_repeat(0, 1, 0, QK_AREP); + set_keymap({key_foo, key_alt_repeat}); + + get_alt_repeat_key_keycode_user_fun = [](uint16_t keycode, uint8_t mods) -> uint16_t { + switch (keycode) { + case QK_USER_0: + return QK_USER_0; // QK_USER_0 handles its own alternate. + default: + return KC_NO; // No key by default. + } + }; + process_record_user_fun = [=](uint16_t keycode, keyrecord_t* record) { + process_record_user_was_called_ = true; + switch (keycode) { + case QK_USER_0: + if (record->event.pressed) { + if (get_repeat_key_count() >= 0) { + SEND_STRING("foo"); + } else { // Key is being alternate repeated. + SEND_STRING("bar"); + } + } + break; + } + return true; + }; + + // Allow any number of empty reports. + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + ExpectString(driver, "foobarbar"); + + tap_keys(key_foo, key_alt_repeat, key_alt_repeat); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests the Additional "Alternate" keys example from the documentation page. +TEST_F(AltRepeatKey, AdditionalAlternateKeysExample) { + TestDriver driver; + KeymapKey key_a(0, 0, 0, KC_A); + KeymapKey key_w(0, 1, 0, KC_W); + KeymapKey key_altrep2(0, 2, 0, QK_USER_0); + KeymapKey key_altrep3(0, 3, 0, QK_USER_1); + set_keymap({key_a, key_w, key_altrep2, key_altrep3}); + + remember_last_key_user_fun = [](uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) { + switch (keycode) { + case QK_USER_0: + case QK_USER_1: + return false; // Ignore ALTREP keys. + } + return true; // Other keys can be repeated. + }; + process_record_user_fun = [=](uint16_t keycode, keyrecord_t* record) { + switch (keycode) { + case QK_USER_0: + if (record->event.pressed) { + const uint16_t last_key = get_last_keycode(); + switch (last_key) { + case KC_A: + SEND_STRING(/*a*/ "tion"); + break; + case KC_W: + SEND_STRING(/*w*/ "hich"); + break; + } + } + return false; + case QK_USER_1: + if (record->event.pressed) { + const uint16_t last_key = get_last_keycode(); + switch (last_key) { + case KC_A: + SEND_STRING(/*a*/ "bout"); + break; + case KC_W: + SEND_STRING(/*w*/ "ould"); + break; + } + } + return false; + } + return true; + }; + + // Allow any number of empty reports. + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + ExpectString(driver, "ationwhichaboutwould"); + + tap_keys(key_a, key_altrep2, key_w, key_altrep2); + tap_keys(key_a, key_altrep3, key_w, key_altrep3); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +} // namespace diff --git a/tests/repeat_key/config.h b/tests/repeat_key/config.h new file mode 100644 index 0000000000..003d980c82 --- /dev/null +++ b/tests/repeat_key/config.h @@ -0,0 +1,20 @@ +// Copyright 2023 Google LLC +// +// 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/>. + +#pragma once + +#include "test_common.h" + +#define NO_ALT_REPEAT_KEY diff --git a/tests/repeat_key/repeat_key_combo/config.h b/tests/repeat_key/repeat_key_combo/config.h new file mode 100644 index 0000000000..d0c4ddadbd --- /dev/null +++ b/tests/repeat_key/repeat_key_combo/config.h @@ -0,0 +1,18 @@ +// Copyright 2023 Google LLC +// +// 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/>. + +#pragma once + +#include "test_common.h" diff --git a/tests/repeat_key/repeat_key_combo/test.mk b/tests/repeat_key/repeat_key_combo/test.mk new file mode 100644 index 0000000000..aac41acaeb --- /dev/null +++ b/tests/repeat_key/repeat_key_combo/test.mk @@ -0,0 +1,19 @@ +# Copyright 2023 Google LLC +# +# 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/>. + +REPEAT_KEY_ENABLE = yes + +COMBO_ENABLE = yes +INTROSPECTION_KEYMAP_C = test_combos.c diff --git a/tests/repeat_key/repeat_key_combo/test_combos.c b/tests/repeat_key/repeat_key_combo/test_combos.c new file mode 100644 index 0000000000..86de89e193 --- /dev/null +++ b/tests/repeat_key/repeat_key_combo/test_combos.c @@ -0,0 +1,8 @@ +// Copyright 2023 Stefan Kerkmann (@KarlK90) +// Copyright 2023 @filterpaper +// Copyright 2023 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later +#include "quantum.h" + +const uint16_t xy_combo[] PROGMEM = {KC_X, KC_Y, COMBO_END}; +combo_t key_combos[] = {COMBO(xy_combo, KC_Q)}; diff --git a/tests/repeat_key/repeat_key_combo/test_repeat_key_combo.cpp b/tests/repeat_key/repeat_key_combo/test_repeat_key_combo.cpp new file mode 100644 index 0000000000..c2d96a0dfe --- /dev/null +++ b/tests/repeat_key/repeat_key_combo/test_repeat_key_combo.cpp @@ -0,0 +1,60 @@ +// Copyright 2023 Google LLC +// +// 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 "test_fixture.hpp" +#include "test_keymap_key.hpp" + +using ::testing::AnyNumber; +using ::testing::InSequence; + +namespace { + +class RepeatKey : public TestFixture {}; + +// Tests repeating a combo, KC_X + KC_Y = KC_Q, by typing +// "X, Repeat, Repeat, {X Y}, Repeat, Repeat". This produces "xxxqqq". +TEST_F(RepeatKey, Combo) { + TestDriver driver; + KeymapKey key_x(0, 0, 0, KC_X); + KeymapKey key_y(0, 1, 0, KC_Y); + KeymapKey key_repeat(0, 2, 0, QK_REP); + set_keymap({key_x, key_y, key_repeat}); + + // Allow any number of empty reports. + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + { + InSequence seq; + EXPECT_REPORT(driver, (KC_X)); + EXPECT_REPORT(driver, (KC_X)); + EXPECT_REPORT(driver, (KC_X)); + EXPECT_REPORT(driver, (KC_Q)); + EXPECT_REPORT(driver, (KC_Q)); + EXPECT_REPORT(driver, (KC_Q)); + } + + tap_keys(key_x, key_repeat, key_repeat); + tap_combo({key_x, key_y}); + + EXPECT_KEYCODE_EQ(get_last_keycode(), KC_Q); + + tap_keys(key_repeat, key_repeat); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +} // namespace diff --git a/tests/repeat_key/test.mk b/tests/repeat_key/test.mk new file mode 100644 index 0000000000..aec8ff3bfb --- /dev/null +++ b/tests/repeat_key/test.mk @@ -0,0 +1,18 @@ +# Copyright 2023 Google LLC +# +# 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/>. + +REPEAT_KEY_ENABLE = yes + +AUTO_SHIFT_ENABLE = yes diff --git a/tests/repeat_key/test_repeat_key.cpp b/tests/repeat_key/test_repeat_key.cpp new file mode 100644 index 0000000000..eee44fc104 --- /dev/null +++ b/tests/repeat_key/test_repeat_key.cpp @@ -0,0 +1,754 @@ +// Copyright 2023 Google LLC +// +// 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 <functional> + +#include "keyboard_report_util.hpp" +#include "keycode.h" +#include "test_common.hpp" +#include "test_fixture.hpp" +#include "test_keymap_key.hpp" + +using ::testing::AnyNumber; +using ::testing::AnyOf; +using ::testing::InSequence; + +#define FOO_MACRO SAFE_RANGE + +namespace { + +bool process_record_user_default(uint16_t keycode, keyrecord_t* record) { + return true; +} + +bool remember_last_key_user_default(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) { + return true; +} + +// Indirection so that process_record_user() and remember_last_key_user() +// can be replaced with other functions in the test cases below. +std::function<bool(uint16_t, keyrecord_t*)> process_record_user_fun = process_record_user_default; +std::function<bool(uint16_t, keyrecord_t*, uint8_t*)> remember_last_key_user_fun = remember_last_key_user_default; + +extern "C" bool process_record_user(uint16_t keycode, keyrecord_t* record) { + return process_record_user_fun(keycode, record); +} +extern "C" bool remember_last_key_user(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) { + return remember_last_key_user_fun(keycode, record, remembered_mods); +} + +class RepeatKey : public TestFixture { + public: + bool process_record_user_was_called_; + + void SetUp() override { + autoshift_disable(); + process_record_user_fun = process_record_user_default; + remember_last_key_user_fun = remember_last_key_user_default; + } + + void ExpectProcessRecordUserCalledWith(bool expected_press, uint16_t expected_keycode, int8_t expected_repeat_key_count) { + process_record_user_was_called_ = false; + process_record_user_fun = [=](uint16_t keycode, keyrecord_t* record) { + EXPECT_EQ(record->event.pressed, expected_press); + EXPECT_KEYCODE_EQ(keycode, expected_keycode); + EXPECT_EQ(get_repeat_key_count(), expected_repeat_key_count); + // Tests below use this to verify process_record_user() was called. + process_record_user_was_called_ = true; + return true; + }; + } + + // Expects that the characters of `s` are sent. + // NOTE: This implementation is limited to chars a-z, A-Z. + void ExpectString(TestDriver& driver, const std::string& s) { + InSequence seq; + for (int c : s) { + switch (c) { + case 'a' ... 'z': { // Lowercase letter. + uint16_t keycode = c - ('a' - KC_A); + EXPECT_REPORT(driver, (keycode)); + } break; + + case 'A' ... 'Z': { // Capital letter = KC_LSFT + letter key. + uint16_t keycode = c - ('A' - KC_A); + EXPECT_REPORT(driver, (KC_LSFT, keycode)); + } break; + } + } + } +}; + +// Tests that "A, Repeat, Repeat, B, Repeat" produces "aaabb". +TEST_F(RepeatKey, Basic) { + TestDriver driver; + KeymapKey key_a(0, 0, 0, KC_A); + KeymapKey key_b(0, 1, 0, KC_B); + KeymapKey key_repeat(0, 2, 0, QK_REP); + set_keymap({key_a, key_b, key_repeat}); + + // Allow any number of empty reports. + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + ExpectString(driver, "aaabb"); + + // When KC_A is pressed, process_record_user() should be called + // with a press event with keycode == KC_A and repeat_key_count() == 0. + ExpectProcessRecordUserCalledWith(true, KC_A, 0); + key_a.press(); + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + // After pressing A, the keycode of the key to be repeated is KC_A. + EXPECT_KEYCODE_EQ(get_last_keycode(), KC_A); + EXPECT_EQ(get_last_mods(), 0); + + // Expect the corresponding release event when A is released. + ExpectProcessRecordUserCalledWith(false, KC_A, 0); + key_a.release(); + run_one_scan_loop(); + + for (int n = 1; n <= 2; ++n) { // Tap the Repeat Key twice. + // When Repeat is pressed, process_record_user() should be called with a + // press event with keycode == KC_A and repeat_key_count() == n. + ExpectProcessRecordUserCalledWith(true, KC_A, n); + key_repeat.press(); // Press the Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + // Expect the corresponding release event. + ExpectProcessRecordUserCalledWith(false, KC_A, n); + key_repeat.release(); // Release the Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + } + + process_record_user_fun = process_record_user_default; + tap_key(key_b); + // Then after tapping key_b, the keycode to be repeated becomes KC_B. + EXPECT_KEYCODE_EQ(get_last_keycode(), KC_B); + + tap_key(key_repeat); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests repeating a macro. The keycode FOO_MACRO sends "foo" when pressed. The +// test taps "FOO_MACRO, Repeat, Repeat", producing "foofoofoo". +TEST_F(RepeatKey, Macro) { + TestDriver driver; + KeymapKey key_foo(0, 0, 0, FOO_MACRO); + KeymapKey key_repeat(0, 1, 0, QK_REP); + set_keymap({key_foo, key_repeat}); + + // Define process_record_user() to handle FOO_MACRO. + process_record_user_fun = [](uint16_t keycode, keyrecord_t* record) { + switch (keycode) { + case FOO_MACRO: + if (record->event.pressed) { + SEND_STRING("foo"); + } + break; + } + return true; + }; + + // Allow any number of empty reports. + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + ExpectString(driver, "foofoofoo"); + + tap_key(key_foo); + + EXPECT_KEYCODE_EQ(get_last_keycode(), FOO_MACRO); + + tap_keys(key_repeat, key_repeat); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests a macro with customized repeat behavior: "foo" is sent normally, "bar" +// on the first repeat, and "baz" on subsequent repeats. The test taps +// "FOO_MACRO, Repeat, Repeat, FOO_MACRO, Repeat", producing "foobarbazfoobar". +TEST_F(RepeatKey, MacroCustomRepeat) { + TestDriver driver; + KeymapKey key_foo(0, 0, 0, FOO_MACRO); + KeymapKey key_repeat(0, 1, 0, QK_REP); + set_keymap({key_foo, key_repeat}); + + process_record_user_fun = [](uint16_t keycode, keyrecord_t* record) { + switch (keycode) { + case FOO_MACRO: + if (record->event.pressed) { + switch (get_repeat_key_count()) { + case 0: // When pressed normally. + SEND_STRING("foo"); + break; + case 1: // On first repeat. + SEND_STRING("bar"); + break; + default: // On subsequent repeats. + SEND_STRING("baz"); + break; + } + } + break; + } + return true; + }; + + // Allow any number of empty reports. + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + ExpectString(driver, "foobarbazfoobar"); + + tap_key(key_foo); + + EXPECT_KEYCODE_EQ(get_last_keycode(), FOO_MACRO); + + tap_keys(key_repeat, key_repeat, key_foo, key_repeat); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests repeating keys on different layers. A 2-layer keymap is defined: +// Layer 0: QK_REP , MO(1) , KC_A +// Layer 1: KC_TRNS, KC_TRNS, KC_B +// The test does the following, which should produce "bbbaaa": +// 1. Hold MO(1), switching to layer 1. +// 2. Tap KC_B on layer 1. +// 3. Release MO(1), switching back to layer 0. +// 4. Tap Repeat twice. +// 5. Tap KC_A on layer 0. +// 6. Hold MO(1), switching to layer 1. +// 7. Tap Repeat twice. +TEST_F(RepeatKey, AcrossLayers) { + TestDriver driver; + KeymapKey key_repeat(0, 0, 0, QK_REP); + KeymapKey key_mo_1(0, 1, 0, MO(1)); + KeymapKey regular_key(0, 2, 0, KC_A); + set_keymap({// Layer 0. + key_repeat, key_mo_1, regular_key, + // Layer 1. + KeymapKey{1, 0, 0, KC_TRNS}, KeymapKey{1, 1, 0, KC_TRNS}, KeymapKey{1, 2, 0, KC_B}}); + + // Allow any number of empty reports. + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + ExpectString(driver, "bbbaaa"); + + key_mo_1.press(); // Hold the MO(1) layer key. + run_one_scan_loop(); + tap_key(regular_key); // Taps the KC_B key on layer 1. + + EXPECT_KEYCODE_EQ(get_last_keycode(), KC_B); + + key_mo_1.release(); // Release the layer key. + run_one_scan_loop(); + tap_keys(key_repeat, key_repeat); + tap_key(regular_key); // Taps the KC_A key on layer 0. + + EXPECT_KEYCODE_EQ(get_last_keycode(), KC_A); + + key_mo_1.press(); // Hold the layer key. + run_one_scan_loop(); + tap_keys(key_repeat, key_repeat); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests "A(down), Repeat(down), A(up), Repeat(up), Repeat" produces "aaa". +TEST_F(RepeatKey, RollingToRepeat) { + TestDriver driver; + KeymapKey key_a(0, 0, 0, KC_A); + KeymapKey key_repeat(0, 1, 0, QK_REP); + set_keymap({key_a, key_repeat}); + + { + InSequence seq; + EXPECT_REPORT(driver, (KC_A)); + EXPECT_EMPTY_REPORT(driver); + EXPECT_REPORT(driver, (KC_A)); + EXPECT_EMPTY_REPORT(driver); + EXPECT_REPORT(driver, (KC_A)); + EXPECT_EMPTY_REPORT(driver); + } + + // Perform a rolled press from A to Repeat. + + ExpectProcessRecordUserCalledWith(true, KC_A, 0); + key_a.press(); + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + ExpectProcessRecordUserCalledWith(true, KC_A, 1); + key_repeat.press(); // Press the Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + ExpectProcessRecordUserCalledWith(false, KC_A, 0); + key_a.release(); + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + ExpectProcessRecordUserCalledWith(false, KC_A, 1); + key_repeat.release(); // Release the Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + process_record_user_fun = process_record_user_default; + tap_key(key_repeat); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests "A, Repeat(down), B(down), Repeat(up), B(up), Repeat" produces "aabb". +TEST_F(RepeatKey, RollingFromRepeat) { + TestDriver driver; + KeymapKey key_a(0, 0, 0, KC_A); + KeymapKey key_b(0, 1, 0, KC_B); + KeymapKey key_repeat(0, 2, 0, QK_REP); + set_keymap({key_a, key_b, key_repeat}); + + { + InSequence seq; + EXPECT_REPORT(driver, (KC_A)); + EXPECT_EMPTY_REPORT(driver); + EXPECT_REPORT(driver, (KC_A)); + EXPECT_REPORT(driver, (KC_A, KC_B)); + EXPECT_REPORT(driver, (KC_B)); + EXPECT_EMPTY_REPORT(driver); + EXPECT_REPORT(driver, (KC_B)); + EXPECT_EMPTY_REPORT(driver); + } + + tap_key(key_a); + + // Perform a rolled press from Repeat to B. + + ExpectProcessRecordUserCalledWith(true, KC_A, 1); + key_repeat.press(); // Press the Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + ExpectProcessRecordUserCalledWith(true, KC_B, 0); + key_b.press(); + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + EXPECT_KEYCODE_EQ(get_last_keycode(), KC_B); + + ExpectProcessRecordUserCalledWith(false, KC_A, 1); + key_repeat.release(); // Release the Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + ExpectProcessRecordUserCalledWith(false, KC_B, 0); + key_b.release(); + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + process_record_user_fun = process_record_user_default; + tap_key(key_repeat); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests Repeat Key with a modifier, types "AltGr+C, Repeat, Repeat, C". +TEST_F(RepeatKey, RecallMods) { + TestDriver driver; + KeymapKey key_c(0, 0, 0, KC_C); + KeymapKey key_altgr(0, 1, 0, KC_RALT); + KeymapKey key_repeat(0, 2, 0, QK_REP); + set_keymap({key_c, key_altgr, key_repeat}); + + // Allow any number of reports with no keys or only KC_RALT. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_RALT)))) + .Times(AnyNumber()); + // clang-format on + + { // Expect: "AltGr+C, AltGr+C, AltGr+C, C". + InSequence seq; + EXPECT_REPORT(driver, (KC_RALT, KC_C)); + EXPECT_REPORT(driver, (KC_RALT, KC_C)); + EXPECT_REPORT(driver, (KC_RALT, KC_C)); + EXPECT_REPORT(driver, (KC_C)); + } + + key_altgr.press(); + run_one_scan_loop(); + tap_key(key_c); + key_altgr.release(); + run_one_scan_loop(); + + EXPECT_KEYCODE_EQ(get_last_keycode(), KC_C); + EXPECT_EQ(get_last_mods(), MOD_BIT(KC_RALT)); + + tap_keys(key_repeat, key_repeat, key_c); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests that Repeat Key stacks mods, types +// "Ctrl+Left, Repeat, Shift+Repeat, Shift+Repeat, Repeat, Left". +TEST_F(RepeatKey, StackMods) { + TestDriver driver; + KeymapKey key_left(0, 0, 0, KC_LEFT); + KeymapKey key_shift(0, 1, 0, KC_LSFT); + KeymapKey key_ctrl(0, 2, 0, KC_LCTL); + KeymapKey key_repeat(0, 3, 0, QK_REP); + set_keymap({key_left, key_shift, key_ctrl, key_repeat}); + + // Allow any number of reports with no keys or only mods. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LCTL), + KeyboardReport(KC_LSFT), + KeyboardReport(KC_LCTL, KC_LSFT)))) + .Times(AnyNumber()); + // clang-format on + + { // Expect: "Ctrl+Left, Ctrl+Shift+Left". + InSequence seq; + EXPECT_REPORT(driver, (KC_LCTL, KC_LEFT)); + EXPECT_REPORT(driver, (KC_LCTL, KC_LEFT)); + EXPECT_REPORT(driver, (KC_LCTL, KC_LSFT, KC_LEFT)); + EXPECT_REPORT(driver, (KC_LCTL, KC_LSFT, KC_LEFT)); + EXPECT_REPORT(driver, (KC_LCTL, KC_LEFT)); + EXPECT_REPORT(driver, (KC_LEFT)); + } + + key_ctrl.press(); + run_one_scan_loop(); + tap_key(key_left); + run_one_scan_loop(); + key_ctrl.release(); + run_one_scan_loop(); + + EXPECT_KEYCODE_EQ(get_last_keycode(), KC_LEFT); + EXPECT_EQ(get_last_mods(), MOD_BIT(KC_LCTL)); + + tap_key(key_repeat); + + key_shift.press(); + run_one_scan_loop(); + tap_keys(key_repeat, key_repeat); + key_shift.release(); + run_one_scan_loop(); + + EXPECT_EQ(get_last_mods(), MOD_BIT(KC_LCTL)); + + tap_keys(key_repeat, key_left); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Types: "S(KC_1), Repeat, Ctrl+Repeat, Ctrl+Repeat, Repeat, KC_2". +TEST_F(RepeatKey, ShiftedKeycode) { + TestDriver driver; + KeymapKey key_exlm(0, 0, 0, S(KC_1)); + KeymapKey key_2(0, 1, 0, KC_2); + KeymapKey key_ctrl(0, 2, 0, KC_LCTL); + KeymapKey key_repeat(0, 3, 0, QK_REP); + set_keymap({key_exlm, key_2, key_ctrl, key_repeat}); + + // Allow any number of reports with no keys or only mods. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LCTL), + KeyboardReport(KC_LSFT), + KeyboardReport(KC_LCTL, KC_LSFT)))) + .Times(AnyNumber()); + // clang-format on + + { // Expect: "Shift+1, Shift+1, Ctrl+Shift+1, Ctrl+Shift+1, Shift+1, 2". + InSequence seq; + EXPECT_REPORT(driver, (KC_LSFT, KC_1)); + EXPECT_REPORT(driver, (KC_LSFT, KC_1)); + EXPECT_REPORT(driver, (KC_LCTL, KC_LSFT, KC_1)); + EXPECT_REPORT(driver, (KC_LCTL, KC_LSFT, KC_1)); + EXPECT_REPORT(driver, (KC_LSFT, KC_1)); + EXPECT_REPORT(driver, (KC_2)); + } + + tap_key(key_exlm); + + EXPECT_KEYCODE_EQ(get_last_keycode(), S(KC_1)); + + tap_key(key_repeat); + + key_ctrl.press(); + run_one_scan_loop(); + tap_keys(key_repeat, key_repeat); + key_ctrl.release(); + run_one_scan_loop(); + + tap_keys(key_repeat, key_2); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests Repeat Key with a one-shot Shift, types +// "A, OSM(MOD_LSFT), Repeat, Repeat". +TEST_F(RepeatKey, WithOneShotShift) { + TestDriver driver; + KeymapKey key_a(0, 0, 0, KC_A); + KeymapKey key_oneshot_shift(0, 1, 0, OSM(MOD_LSFT)); + KeymapKey key_repeat(0, 2, 0, QK_REP); + set_keymap({key_a, key_oneshot_shift, key_repeat}); + + // Allow any number of reports with no keys or only KC_RALT. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT)))) + .Times(AnyNumber()); + // clang-format on + ExpectString(driver, "aAa"); + + tap_keys(key_a, key_oneshot_shift, key_repeat, key_repeat); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests Repeat Key with a mod-tap key, types +// "A, Repeat, Repeat, A(down), Repeat, Repeat, A(up), Repeat". +TEST_F(RepeatKey, ModTap) { + TestDriver driver; + KeymapKey key_mt_a(0, 0, 0, LSFT_T(KC_A)); + KeymapKey key_repeat(0, 1, 0, QK_REP); + set_keymap({key_mt_a, key_repeat}); + + // Allow any number of reports with no keys or only KC_LSFT. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT)))) + .Times(AnyNumber()); + // clang-format on + ExpectString(driver, "aaaAAa"); + + tap_key(key_mt_a); + + EXPECT_KEYCODE_EQ(get_last_keycode(), LSFT_T(KC_A)); + + tap_keys(key_repeat, key_repeat); + key_mt_a.press(); + run_one_scan_loop(); + tap_key(key_repeat, TAPPING_TERM + 1); + tap_key(key_repeat); + key_mt_a.release(); + run_one_scan_loop(); + tap_key(key_repeat); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests with Auto Shift. When repeating an autoshiftable key, it does not +// matter how long the original key was held, rather, quickly tapping vs. +// long-pressing the Repeat Key determines whether the shifted key is repeated. +// +// The test does the following, which should produce "aaABbB": +// 1. Tap KC_A quickly. +// 2. Tap Repeat Key quickly. +// 3. Long-press Repeat Key. +// 4. Long-press KC_B. +// 5. Tap Repeat Key quickly. +// 6. Long-press Repeat Key. +TEST_F(RepeatKey, AutoShift) { + TestDriver driver; + KeymapKey key_a(0, 0, 0, KC_A); + KeymapKey key_b(0, 1, 0, KC_B); + KeymapKey key_repeat(0, 2, 0, QK_REP); + set_keymap({key_a, key_b, key_repeat}); + + autoshift_enable(); + + // Allow any number of reports with no keys or only KC_LSFT. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT)))) + .Times(AnyNumber()); + // clang-format on + ExpectString(driver, "aaABbB"); + + tap_key(key_a); // Tap A quickly. + + EXPECT_KEYCODE_EQ(get_last_keycode(), KC_A); + EXPECT_EQ(get_last_mods(), 0); + + tap_key(key_repeat); + tap_key(key_repeat, AUTO_SHIFT_TIMEOUT + 1); + + tap_key(key_b, AUTO_SHIFT_TIMEOUT + 1); // Long press B. + + EXPECT_KEYCODE_EQ(get_last_keycode(), KC_B); + EXPECT_EQ(get_last_mods(), 0); + + tap_key(key_repeat); + tap_key(key_repeat, AUTO_SHIFT_TIMEOUT + 1); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Defines `remember_last_key_user()` to forget the Shift mod and types: +// "Ctrl+A, Repeat, Shift+A, Repeat, Shift+Repeat". +TEST_F(RepeatKey, FilterRememberedMods) { + TestDriver driver; + KeymapKey key_a(0, 0, 0, KC_A); + KeymapKey key_ctrl(0, 1, 0, KC_LCTL); + KeymapKey key_shift(0, 2, 0, KC_LSFT); + KeymapKey key_repeat(0, 3, 0, QK_REP); + set_keymap({key_a, key_ctrl, key_shift, key_repeat}); + + remember_last_key_user_fun = [](uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) { + *remembered_mods &= ~MOD_MASK_SHIFT; + return true; + }; + + // Allow any number of reports with no keys or only mods. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LCTL), + KeyboardReport(KC_LSFT), + KeyboardReport(KC_LCTL, KC_LSFT)))) + .Times(AnyNumber()); + // clang-format on + + { // Expect: "Ctrl+A, Ctrl+A, Shift+A, A, Shift+A". + InSequence seq; + EXPECT_REPORT(driver, (KC_LCTL, KC_A)); + EXPECT_REPORT(driver, (KC_LCTL, KC_A)); + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_A)); + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + } + + key_ctrl.press(); + run_one_scan_loop(); + tap_key(key_a); + + EXPECT_EQ(get_last_mods(), MOD_BIT(KC_LCTL)); + + key_ctrl.release(); + run_one_scan_loop(); + + tap_key(key_repeat); + key_shift.press(); + run_one_scan_loop(); + tap_key(key_a); + + EXPECT_EQ(get_last_mods(), 0); // Shift should be forgotten. + + key_shift.release(); + run_one_scan_loop(); + + tap_key(key_repeat); + + key_shift.press(); + run_one_scan_loop(); + tap_key(key_repeat); + key_shift.release(); + run_one_scan_loop(); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests set_last_keycode() and set_last_mods(). +TEST_F(RepeatKey, SetRepeatKeyKeycode) { + TestDriver driver; + KeymapKey key_repeat(0, 0, 0, QK_REP); + set_keymap({key_repeat}); + + // Allow any number of reports with no keys or only KC_LSFT. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT)))) + .Times(AnyNumber()); + // clang-format on + ExpectString(driver, "aaBB"); + + set_last_keycode(KC_A); + EXPECT_KEYCODE_EQ(get_last_keycode(), KC_A); + + for (int n = 1; n <= 2; ++n) { // Tap the Repeat Key twice. + // When Repeat is pressed, process_record_user() should be called with a + // press event with keycode == KC_A and repeat_key_count() == n. + ExpectProcessRecordUserCalledWith(true, KC_A, n); + key_repeat.press(); // Press the Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + // Expect the corresponding release event. + ExpectProcessRecordUserCalledWith(false, KC_A, n); + key_repeat.release(); // Release the Repeat Key. + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + } + + process_record_user_fun = process_record_user_default; + set_last_keycode(KC_B); + set_last_mods(MOD_BIT(KC_LSFT)); + + tap_keys(key_repeat, key_repeat); + + set_last_keycode(KC_NO); + tap_keys(key_repeat, key_repeat); // Has no effect. + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests the `repeat_key_invoke()` function. +TEST_F(RepeatKey, RepeatKeyInvoke) { + TestDriver driver; + KeymapKey key_s(0, 0, 0, KC_S); + set_keymap({key_s}); + + // Allow any number of empty reports. + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + ExpectString(driver, "ss"); + + tap_key(key_s); + + EXPECT_KEYCODE_EQ(get_last_keycode(), KC_S); + + // Calling repeat_key_invoke() should result in process_record_user() + // getting a press event with keycode KC_S. + ExpectProcessRecordUserCalledWith(true, KC_S, 1); + keyevent_t event; + event.key = {0, 0}; + event.pressed = true; + event.time = timer_read(); + event.type = KEY_EVENT; + repeat_key_invoke(&event); + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + // Make the release event. + ExpectProcessRecordUserCalledWith(false, KC_S, 1); + event.pressed = false; + event.time = timer_read(); + repeat_key_invoke(&event); + run_one_scan_loop(); + EXPECT_TRUE(process_record_user_was_called_); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +} // namespace diff --git a/tests/tap_hold_configurations/default_mod_tap/config.h b/tests/tap_hold_configurations/default_mod_tap/config.h index f22448845e..6d872dd57b 100644 --- a/tests/tap_hold_configurations/default_mod_tap/config.h +++ b/tests/tap_hold_configurations/default_mod_tap/config.h @@ -17,5 +17,3 @@ #pragma once #include "test_common.h" - -#define IGNORE_MOD_TAP_INTERRUPT diff --git a/tests/tap_hold_configurations/quick_tap/test_quick_tap.cpp b/tests/tap_hold_configurations/quick_tap/test_quick_tap.cpp index 8ec6ea62a3..dda58463fb 100644 --- a/tests/tap_hold_configurations/quick_tap/test_quick_tap.cpp +++ b/tests/tap_hold_configurations/quick_tap/test_quick_tap.cpp @@ -27,86 +27,6 @@ using testing::InSequence; class QuickTap : public TestFixture {}; -TEST_F(QuickTap, tap_regular_key_while_mod_tap_key_is_held) { - TestDriver driver; - InSequence s; - auto mod_tap_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); - auto regular_key = KeymapKey(0, 2, 0, KC_A); - - set_keymap({mod_tap_key, regular_key}); - - /* Press mod-tap key. */ - EXPECT_NO_REPORT(driver); - mod_tap_key.press(); - run_one_scan_loop(); - VERIFY_AND_CLEAR(driver); - - /* Press regular key. */ - EXPECT_NO_REPORT(driver); - regular_key.press(); - run_one_scan_loop(); - VERIFY_AND_CLEAR(driver); - - /* Release regular key. */ - EXPECT_NO_REPORT(driver); - regular_key.release(); - run_one_scan_loop(); - VERIFY_AND_CLEAR(driver); - - /* Release mod-tap key. */ - EXPECT_REPORT(driver, (KC_LSFT)); - mod_tap_key.release(); - run_one_scan_loop(); - VERIFY_AND_CLEAR(driver); - - /* Idle for tapping term of mod tap hold key. */ - EXPECT_REPORT(driver, (KC_LSFT, KC_A)); - EXPECT_REPORT(driver, (KC_LSFT)); - EXPECT_EMPTY_REPORT(driver); - idle_for(TAPPING_TERM - 3); - VERIFY_AND_CLEAR(driver); -} - -TEST_F(QuickTap, tap_mod_tap_key_while_mod_tap_key_is_held) { - TestDriver driver; - InSequence s; - auto first_mod_tap_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); - auto second_mod_tap_key = KeymapKey(0, 2, 0, RSFT_T(KC_A)); - - set_keymap({first_mod_tap_key, second_mod_tap_key}); - - /* Press first mod-tap key */ - EXPECT_NO_REPORT(driver); - first_mod_tap_key.press(); - run_one_scan_loop(); - VERIFY_AND_CLEAR(driver); - - /* Press second mod-tap key */ - EXPECT_NO_REPORT(driver); - second_mod_tap_key.press(); - run_one_scan_loop(); - VERIFY_AND_CLEAR(driver); - - /* Release second tap-hold key */ - EXPECT_NO_REPORT(driver); - second_mod_tap_key.release(); - run_one_scan_loop(); - VERIFY_AND_CLEAR(driver); - - /* Release first mod-tap key */ - EXPECT_REPORT(driver, (KC_LSFT)); - first_mod_tap_key.release(); - run_one_scan_loop(); - VERIFY_AND_CLEAR(driver); - - /* Idle for tapping term of first mod-tap key. */ - EXPECT_REPORT(driver, (KC_LSFT, KC_A)); - EXPECT_REPORT(driver, (KC_LSFT)); - EXPECT_EMPTY_REPORT(driver); - idle_for(TAPPING_TERM - 3); - VERIFY_AND_CLEAR(driver); -} - TEST_F(QuickTap, tap_regular_key_while_layer_tap_key_is_held) { TestDriver driver; InSequence s; diff --git a/tests/test_common/keycode_table.cpp b/tests/test_common/keycode_table.cpp index d21630c01b..9ed80cdbcf 100644 --- a/tests/test_common/keycode_table.cpp +++ b/tests/test_common/keycode_table.cpp @@ -663,6 +663,8 @@ std::map<uint16_t, std::string> KEYCODE_ID_TABLE = { {QK_AUTOCORRECT_TOGGLE, "QK_AUTOCORRECT_TOGGLE"}, {QK_TRI_LAYER_LOWER, "QK_TRI_LAYER_LOWER"}, {QK_TRI_LAYER_UPPER, "QK_TRI_LAYER_UPPER"}, + {QK_REPEAT_KEY, "QK_REPEAT_KEY"}, + {QK_ALT_REPEAT_KEY, "QK_ALT_REPEAT_KEY"}, {QK_KB_0, "QK_KB_0"}, {QK_KB_1, "QK_KB_1"}, {QK_KB_2, "QK_KB_2"}, diff --git a/tests/test_common/keycode_util.hpp b/tests/test_common/keycode_util.hpp index d5a520d4b2..3143ab364e 100644 --- a/tests/test_common/keycode_util.hpp +++ b/tests/test_common/keycode_util.hpp @@ -1,5 +1,6 @@ #pragma once +#include <cstdint> #include <string> std::string get_keycode_identifier_or_default(uint16_t keycode); diff --git a/tests/test_common/test_driver.hpp b/tests/test_common/test_driver.hpp index 8d09e44840..d8a6885d0f 100644 --- a/tests/test_common/test_driver.hpp +++ b/tests/test_common/test_driver.hpp @@ -20,6 +20,7 @@ #include <stdint.h> #include "host.h" #include "keyboard_report_util.hpp" +#include "keycode_util.hpp" #include "test_logger.hpp" class TestDriver { @@ -98,6 +99,17 @@ class TestDriver { */ #define EXPECT_NO_REPORT(driver) EXPECT_ANY_REPORT(driver).Times(0) +/** @brief Tests whether keycode `actual` is equal to `expected`. */ +#define EXPECT_KEYCODE_EQ(actual, expected) EXPECT_THAT((actual), KeycodeEq((expected))) + +MATCHER_P(KeycodeEq, expected_keycode, "is equal to " + testing::PrintToString(expected_keycode) + ", keycode " + get_keycode_identifier_or_default(expected_keycode)) { + if (arg == expected_keycode) { + return true; + } + *result_listener << "keycode " << get_keycode_identifier_or_default(arg); + return false; +} + /** * @brief Verify and clear all gmock expectations that have been setup until * this point. diff --git a/tests/test_common/test_fixture.cpp b/tests/test_common/test_fixture.cpp index 76daa625ad..72763d0bc0 100644 --- a/tests/test_common/test_fixture.cpp +++ b/tests/test_common/test_fixture.cpp @@ -22,7 +22,6 @@ extern "C" { #include "debug.h" #include "eeconfig.h" #include "keyboard.h" -#include "keymap.h" void set_time(uint32_t t); void advance_time(uint32_t ms); |