diff options
Diffstat (limited to 'tests')
57 files changed, 3912 insertions, 339 deletions
diff --git a/tests/auto_shift/config.h b/tests/auto_shift/config.h new file mode 100644 index 0000000000..4f343b4529 --- /dev/null +++ b/tests/auto_shift/config.h @@ -0,0 +1,19 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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"
\ No newline at end of file diff --git a/tests/auto_shift/test.mk b/tests/auto_shift/test.mk new file mode 100644 index 0000000000..4259c606e4 --- /dev/null +++ b/tests/auto_shift/test.mk @@ -0,0 +1,20 @@ +# Copyright 2021 Stefan Kerkmann +# +# 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/>. + +# -------------------------------------------------------------------------------- +# Keep this file, even if it is empty, as a marker that this folder contains tests +# -------------------------------------------------------------------------------- + +AUTO_SHIFT_ENABLE = yes
\ No newline at end of file diff --git a/tests/auto_shift/test_auto_shift.cpp b/tests/auto_shift/test_auto_shift.cpp new file mode 100644 index 0000000000..a83f436c33 --- /dev/null +++ b/tests/auto_shift/test_auto_shift.cpp @@ -0,0 +1,70 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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_fixture.hpp" +#include "test_keymap_key.hpp" + +using testing::_; +using testing::InSequence; + +class AutoShift : public TestFixture {}; + +TEST_F(AutoShift, key_release_before_timeout) { + TestDriver driver; + InSequence s; + auto regular_key = KeymapKey(0, 2, 0, KC_A); + + set_keymap({regular_key}); + + /* Press regular key */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_REPORT(driver, (KC_A)); + EXPECT_EMPTY_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(AutoShift, key_release_after_timeout) { + TestDriver driver; + InSequence s; + auto regular_key = KeymapKey(0, 2, 0, KC_A); + + set_keymap({regular_key}); + + /* Press regular key */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + idle_for(AUTO_SHIFT_TIMEOUT); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_LSFT)); + EXPECT_EMPTY_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/basic/config.h b/tests/basic/config.h index 99bd62d991..85fa9d691d 100644 --- a/tests/basic/config.h +++ b/tests/basic/config.h @@ -16,5 +16,4 @@ #pragma once -#define MATRIX_ROWS 4 -#define MATRIX_COLS 10 +#include "test_common.h"
\ No newline at end of file diff --git a/tests/basic/keymap.c b/tests/basic/keymap.c deleted file mode 100644 index 2b5747abb7..0000000000 --- a/tests/basic/keymap.c +++ /dev/null @@ -1,45 +0,0 @@ -/* Copyright 2017 Fred Sundvik - * - * 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 "quantum.h" - -// Don't rearrange keys as existing tests might rely on the order -// Col2, Row 0 has to be KC_NO, because tests rely on it - -#define COMBO1 RSFT(LCTL(KC_O)) - -const uint16_t PROGMEM - keymaps[][MATRIX_ROWS][MATRIX_COLS] = - { - [0] = - { - // 0 1 2 3 4 5 6 7 8 9 - {KC_A, KC_B, KC_NO, KC_LSFT, KC_RSFT, KC_LCTL, COMBO1, SFT_T(KC_P), M(0), KC_NO}, - {KC_EQL, KC_PLUS, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO}, - {KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO}, - {KC_C, KC_D, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO}, - }, -}; - -const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) { - if (record->event.pressed) { - switch (id) { - case 0: - return MACRO(D(LSFT), T(H), U(LSFT), T(E), T(L), T(L), T(O), T(SPACE), W(100), D(LSFT), T(W), U(LSFT), I(10), T(O), T(R), T(L), T(D), D(LSFT), T(1), U(LSFT), END); - } - } - return MACRO_NONE; -}; diff --git a/tests/basic/test.mk b/tests/basic/test.mk new file mode 100644 index 0000000000..29690d1adf --- /dev/null +++ b/tests/basic/test.mk @@ -0,0 +1,18 @@ +# Copyright 2017 Fred Sundvik +# +# 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/>. + +# -------------------------------------------------------------------------------- +# Keep this file, even if it is empty, as a marker that this folder contains tests +# --------------------------------------------------------------------------------
\ No newline at end of file diff --git a/tests/basic/test_action_layer.cpp b/tests/basic/test_action_layer.cpp index d00a0859b9..fa339a3375 100644 --- a/tests/basic/test_action_layer.cpp +++ b/tests/basic/test_action_layer.cpp @@ -14,39 +14,54 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "gtest/gtest.h" +#include "keyboard_report_util.hpp" #include "test_common.hpp" using testing::_; -using testing::Return; +using testing::InSequence; class ActionLayer : public TestFixture {}; -// TEST_F(ActionLayer, LayerStateDBG) { -// layer_state_set(0); -// } - -// TEST_F(ActionLayer, LayerStateSet) { -// layer_state_set(0); -// EXPECT_EQ(layer_state, 0); -// layer_state_set(0b001100); -// EXPECT_EQ(layer_state, 0b001100); -// } - -// TEST_F(ActionLayer, LayerStateIs) { -// layer_state_set(0); -// EXPECT_EQ(layer_state_is(0), true); -// EXPECT_EQ(layer_state_is(1), true); -// layer_state_set(1); -// EXPECT_EQ(layer_state_is(0), true); -// EXPECT_EQ(layer_state_is(1), true); -// layer_state_set(2); -// EXPECT_EQ(layer_state_is(0), false); -// EXPECT_EQ(layer_state_is(1), false); -// EXPECT_EQ(layer_state_is(2), true); -// } +TEST_F(ActionLayer, LayerStateDBG) { + TestDriver driver; + + layer_state_set(0); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(ActionLayer, LayerStateSet) { + TestDriver driver; + + layer_state_set(0); + EXPECT_EQ(layer_state, 0); + layer_state_set(0b001100); + EXPECT_EQ(layer_state, 0b001100); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(ActionLayer, LayerStateIs) { + TestDriver driver; + + layer_state_set(0); + EXPECT_EQ(layer_state_is(0), true); + EXPECT_EQ(layer_state_is(1), false); + layer_state_set(1); + EXPECT_EQ(layer_state_is(0), true); + EXPECT_EQ(layer_state_is(1), false); + layer_state_set(2); + EXPECT_EQ(layer_state_is(0), false); + EXPECT_EQ(layer_state_is(1), true); + EXPECT_EQ(layer_state_is(2), false); + + testing::Mock::VerifyAndClearExpectations(&driver); +} TEST_F(ActionLayer, LayerStateCmp) { - uint32_t prev_layer; + TestDriver driver; + uint32_t prev_layer; prev_layer = 0; EXPECT_EQ(layer_state_cmp(prev_layer, 0), true); @@ -60,33 +75,328 @@ TEST_F(ActionLayer, LayerStateCmp) { EXPECT_EQ(layer_state_cmp(prev_layer, 0), false); EXPECT_EQ(layer_state_cmp(prev_layer, 1), true); EXPECT_EQ(layer_state_cmp(prev_layer, 2), false); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(ActionLayer, LayerClear) { + TestDriver driver; + + layer_clear(); + EXPECT_EQ(layer_state, 0); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(ActionLayer, LayerMove) { + TestDriver driver; + + layer_move(0); + EXPECT_EQ(layer_state, 1); + layer_move(3); + EXPECT_EQ(layer_state, 0b1000); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(ActionLayer, LayerOn) { + TestDriver driver; + + layer_clear(); + layer_on(1); + layer_on(3); + layer_on(3); + EXPECT_EQ(layer_state, 0b1010); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(ActionLayer, LayerOff) { + TestDriver driver; + + layer_clear(); + layer_on(1); + layer_on(3); + layer_off(3); + layer_off(2); + EXPECT_EQ(layer_state, 0b0010); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(ActionLayer, MomentaryLayerDoesNothing) { + TestDriver driver; + KeymapKey layer_key = KeymapKey{0, 0, 0, MO(1)}; + + set_keymap({layer_key}); + + /* Press and release MO, nothing should happen. */ + EXPECT_NO_REPORT(driver); + layer_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + EXPECT_NO_REPORT(driver); + layer_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(ActionLayer, MomentaryLayerWithKeypress) { + TestDriver driver; + KeymapKey layer_key = KeymapKey{0, 0, 0, MO(1)}; + + /* These keys must have the same position in the matrix, only the layer is different. */ + KeymapKey regular_key = KeymapKey{0, 1, 0, KC_A}; + set_keymap({layer_key, regular_key, KeymapKey{1, 1, 0, KC_B}}); + + /* Press MO. */ + EXPECT_NO_REPORT(driver); + layer_key.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press key on layer 1 */ + EXPECT_REPORT(driver, (KC_B)).Times(1); + regular_key.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release key on layer 1 */ + EXPECT_EMPTY_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release MO */ + EXPECT_NO_REPORT(driver); + layer_key.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(0)); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(ActionLayer, ToggleLayerDoesNothing) { + GTEST_SKIP() << "TODO: Toggle layer does not activate the expected layer on key press but on release."; + + TestDriver driver; + KeymapKey layer_key = KeymapKey{0, 0, 0, TG(1)}; + + set_keymap({layer_key}); + + /* Press TG. Layer state should not change as it's applied on release. */ + EXPECT_NO_REPORT(driver); + layer_key.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release TG. */ + EXPECT_NO_REPORT(driver); + layer_key.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(ActionLayer, ToggleLayerUpAndDown) { + GTEST_SKIP() << "TODO: Toggle layer does not activate the expected layer on key press but on release."; + + TestDriver driver; + KeymapKey toggle_layer_1_on_layer_0 = KeymapKey{0, 0, 0, TG(1)}; + KeymapKey toggle_layer_0_on_layer_1 = KeymapKey{1, 1, 0, TG(0)}; + + set_keymap({toggle_layer_1_on_layer_0, toggle_layer_0_on_layer_1}); + + /* Toggle Layer 1. */ + EXPECT_NO_REPORT(driver); + toggle_layer_1_on_layer_0.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); + + EXPECT_NO_REPORT(driver); + toggle_layer_1_on_layer_0.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Toggle Layer 0. */ + EXPECT_NO_REPORT(driver); + toggle_layer_0_on_layer_1.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(0)); + testing::Mock::VerifyAndClearExpectations(&driver); + + EXPECT_NO_REPORT(driver); + toggle_layer_0_on_layer_1.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(0)); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(ActionLayer, LayerTapToggleDoesNothing) { + GTEST_SKIP() << "TODO: Tap toggle layer does not activate the expected layer on key press."; + + TestDriver driver; + KeymapKey layer_key = KeymapKey{0, 0, 0, TT(1)}; + + set_keymap({layer_key}); + + /* Press and release TT. */ + EXPECT_NO_REPORT(driver); + layer_key.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); + + EXPECT_NO_REPORT(driver); + layer_key.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(0)); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(ActionLayer, LayerTapToggleWithKeypress) { + GTEST_SKIP() << "TODO: Tap toggle layer does not activate the expected layer on key press."; + + TestDriver driver; + KeymapKey layer_key = KeymapKey{0, 0, 0, TT(1)}; + + /* These keys must have the same position in the matrix, only the layer is different. */ + KeymapKey regular_key = KeymapKey{0, 1, 0, KC_A}; + set_keymap({layer_key, regular_key, KeymapKey{1, 1, 0, KC_B}}); + + /* Press TT. */ + EXPECT_NO_REPORT(driver); + layer_key.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); + + EXPECT_REPORT(driver, (KC_B)).Times(1); + regular_key.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); + + EXPECT_EMPTY_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); + + EXPECT_NO_REPORT(driver); + layer_key.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(0)); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(ActionLayer, LayerTapToggleWithToggleWithKeypress) { + GTEST_SKIP() << "TODO: Tap toggle layer does not activate the expected layer on key press."; + + TestDriver driver; + KeymapKey layer_key = KeymapKey{0, 0, 0, TT(1)}; + + /* These keys must have the same position in the matrix, only the layer is different. */ + KeymapKey regular_key = KeymapKey{0, 1, 0, KC_A}; + set_keymap({layer_key, regular_key, KeymapKey{1, 1, 0, KC_B}}); + + /* Tap TT five times . */ + EXPECT_NO_REPORT(driver); + + layer_key.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + layer_key.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(0)); + + layer_key.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + layer_key.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(0)); + + layer_key.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + layer_key.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(0)); + + layer_key.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + layer_key.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(0)); + + layer_key.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + layer_key.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + + testing::Mock::VerifyAndClearExpectations(&driver); + + EXPECT_REPORT(driver, (KC_B)).Times(1); + regular_key.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); + + EXPECT_EMPTY_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); } -// TEST_F(ActionLayer, LayerClear) { -// layer_clear(); -// EXPECT_EQ(layer_state, 0); -// } - -// TEST_F(ActionLayer, LayerMove) { -// layer_move(0); -// EXPECT_EQ(layer_state, 1); -// layer_move(3); -// EXPECT_EQ(layer_state, 0b1000); -// } - -// TEST_F(ActionLayer, LayerOn) { -// layer_clear(); -// layer_on(1); -// layer_on(3); -// layer_on(3); -// EXPECT_EQ(layer_state, 0b1010); -// } - -// TEST_F(ActionLayer, LayerOff) { -// layer_clear(); -// layer_on(1); -// layer_on(3); -// layer_off(3); -// layer_off(2); -// EXPECT_EQ(layer_state, 0b1000); -// } +TEST_F(ActionLayer, LayerTapReleasedBeforeKeypressReleaseWithModifiers) { + GTEST_SKIP() << "TODO: Modifiers are erroneously discarded on layer changes, although a key that introduced the modifier is still held."; + TestDriver driver; + InSequence s; + + KeymapKey layer_0_key_0 = KeymapKey{0, 0, 0, LT(1, KC_T)}; + KeymapKey layer_1_key_1 = KeymapKey{1, 1, 0, RALT(KC_9)}; + + set_keymap({layer_0_key_0, layer_1_key_1}); + + /* Press layer tap and wait for tapping term to switch to layer 1 */ + EXPECT_NO_REPORT(driver); + layer_0_key_0.press(); + idle_for(TAPPING_TERM); + EXPECT_TRUE(layer_state_is(0)); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press key with layer 1 mapping, result basically expected + * altough more reports are send then necessary. */ + EXPECT_REPORT(driver, (KC_RALT)).Times(1); + EXPECT_REPORT(driver, (KC_RALT, KC_9)).Times(1); + layer_1_key_1.press(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(1)); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release layer tap key, no report is send because key is still held. */ + EXPECT_NO_REPORT(driver); + layer_0_key_0.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(0)); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Unregister keycode and modifier. */ + EXPECT_REPORT(driver, (KC_RALT)).Times(1); + EXPECT_EMPTY_REPORT(driver); + layer_1_key_1.release(); + run_one_scan_loop(); + EXPECT_TRUE(layer_state_is(0)); + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/basic/test_keypress.cpp b/tests/basic/test_keypress.cpp index ecd6e98e44..bb68ced557 100644 --- a/tests/basic/test_keypress.cpp +++ b/tests/basic/test_keypress.cpp @@ -14,139 +14,204 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "keycode.h" #include "test_common.hpp" using testing::_; using testing::InSequence; -using testing::Return; class KeyPress : public TestFixture {}; TEST_F(KeyPress, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { TestDriver driver; - EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); + EXPECT_NO_REPORT(driver); keyboard_task(); } TEST_F(KeyPress, CorrectKeyIsReportedWhenPressed) { TestDriver driver; - press_key(0, 0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A))); + auto key = KeymapKey(0, 0, 0, KC_A); + + set_keymap({key}); + + key.press(); + EXPECT_REPORT(driver, (key.report_code)); + keyboard_task(); + + key.release(); + EXPECT_EMPTY_REPORT(driver); + keyboard_task(); +} + +TEST_F(KeyPress, ANonMappedKeyDoesNothing) { + TestDriver driver; + auto key = KeymapKey(0, 0, 0, KC_NO); + + set_keymap({key}); + + key.press(); + EXPECT_NO_REPORT(driver); keyboard_task(); - release_key(0, 0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); keyboard_task(); } TEST_F(KeyPress, CorrectKeysAreReportedWhenTwoKeysArePressed) { TestDriver driver; - press_key(1, 0); - press_key(0, 3); + auto key_b = KeymapKey(0, 0, 0, KC_B); + auto key_c = KeymapKey(0, 1, 1, KC_C); + + set_keymap({key_b, key_c}); + + key_b.press(); + key_c.press(); // Note that QMK only processes one key at a time // See issue #1476 for more information - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_B))); + EXPECT_REPORT(driver, (key_b.report_code)); keyboard_task(); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_B, KC_C))); + + EXPECT_REPORT(driver, (key_b.report_code, key_c.report_code)); keyboard_task(); - release_key(1, 0); - release_key(0, 3); + + key_b.release(); + key_c.release(); // Note that the first key released is the first one in the matrix order - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_C))); - keyboard_task(); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + EXPECT_REPORT(driver, (key_c.report_code)); keyboard_task(); -} -TEST_F(KeyPress, ANonMappedKeyDoesNothing) { - TestDriver driver; - press_key(2, 0); - EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); - keyboard_task(); + EXPECT_EMPTY_REPORT(driver); keyboard_task(); } TEST_F(KeyPress, LeftShiftIsReportedCorrectly) { TestDriver driver; - press_key(3, 0); - press_key(0, 0); + auto key_a = KeymapKey(0, 0, 0, KC_A); + auto key_lsft = KeymapKey(0, 3, 0, KC_LEFT_SHIFT); + + set_keymap({key_a, key_lsft}); + + key_lsft.press(); + key_a.press(); + // Unfortunately modifiers are also processed in the wrong order // See issue #1476 for more information - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A))); + EXPECT_REPORT(driver, (key_a.report_code)); keyboard_task(); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A, KC_LSFT))); + EXPECT_REPORT(driver, (key_a.report_code, key_lsft.report_code)); keyboard_task(); - release_key(0, 0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); + + key_a.release(); + EXPECT_REPORT(driver, (key_lsft.report_code)); keyboard_task(); - release_key(3, 0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + + key_lsft.release(); + EXPECT_EMPTY_REPORT(driver); keyboard_task(); } TEST_F(KeyPress, PressLeftShiftAndControl) { TestDriver driver; - press_key(3, 0); - press_key(5, 0); + auto key_lsft = KeymapKey(0, 3, 0, KC_LEFT_SHIFT); + auto key_lctrl = KeymapKey(0, 5, 0, KC_LEFT_CTRL); + + set_keymap({key_lctrl, key_lsft}); + + key_lsft.press(); + key_lctrl.press(); + // Unfortunately modifiers are also processed in the wrong order // See issue #1476 for more information - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); + EXPECT_REPORT(driver, (key_lsft.report_code)); + keyboard_task(); + + EXPECT_REPORT(driver, (key_lsft.report_code, key_lctrl.report_code)); + keyboard_task(); + + key_lsft.release(); + key_lctrl.release(); + + EXPECT_REPORT(driver, (key_lctrl.report_code)); keyboard_task(); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_LCTRL))); + + EXPECT_EMPTY_REPORT(driver); keyboard_task(); } TEST_F(KeyPress, LeftAndRightShiftCanBePressedAtTheSameTime) { TestDriver driver; - press_key(3, 0); - press_key(4, 0); + auto key_lsft = KeymapKey(0, 3, 0, KC_LEFT_SHIFT); + auto key_rsft = KeymapKey(0, 4, 0, KC_RIGHT_SHIFT); + + set_keymap({key_rsft, key_lsft}); + + key_lsft.press(); + key_rsft.press(); // Unfortunately modifiers are also processed in the wrong order // See issue #1476 for more information - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); + EXPECT_REPORT(driver, (key_lsft.report_code)); + keyboard_task(); + + EXPECT_REPORT(driver, (key_lsft.report_code, key_rsft.report_code)); + keyboard_task(); + + key_lsft.release(); + key_rsft.release(); + + EXPECT_REPORT(driver, (key_rsft.report_code)); keyboard_task(); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_RSFT))); + + EXPECT_EMPTY_REPORT(driver); keyboard_task(); } TEST_F(KeyPress, RightShiftLeftControlAndCharWithTheSameKey) { TestDriver driver; - press_key(6, 0); + auto combo_key = KeymapKey(0, 0, 0, RSFT(LCTL(KC_O))); + + set_keymap({combo_key}); + // BUG: The press is split into two reports // BUG: It reports RSFT instead of LSFT // See issue #524 for more information // The underlying cause is that we use only one bit to represent the right hand // modifiers. - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL))); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL, KC_O))); + combo_key.press(); + EXPECT_REPORT(driver, (KC_RIGHT_SHIFT, KC_RIGHT_CTRL)); + EXPECT_REPORT(driver, (KC_RIGHT_SHIFT, KC_RIGHT_CTRL, KC_O)); keyboard_task(); - release_key(6, 0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL))); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + + combo_key.release(); + EXPECT_REPORT(driver, (KC_RIGHT_SHIFT, KC_RIGHT_CTRL)); + EXPECT_EMPTY_REPORT(driver); keyboard_task(); } TEST_F(KeyPress, PressPlusEqualReleaseBeforePress) { TestDriver driver; InSequence s; + auto key_plus = KeymapKey(0, 1, 1, KC_PLUS); + auto key_eql = KeymapKey(0, 0, 1, KC_EQUAL); + + set_keymap({key_plus, key_eql}); - press_key(1, 1); // KC_PLUS - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL))); + key_plus.press(); + EXPECT_REPORT(driver, (KC_LEFT_SHIFT)); + EXPECT_REPORT(driver, (KC_LEFT_SHIFT, KC_EQUAL)); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); - release_key(1, 1); // KC_PLUS - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + key_plus.release(); + EXPECT_REPORT(driver, (KC_LEFT_SHIFT)); + EXPECT_EMPTY_REPORT(driver); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); - press_key(0, 1); // KC_EQL - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL))); + key_eql.press(); + EXPECT_REPORT(driver, (key_eql.report_code)); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); - release_key(0, 1); // KC_EQL - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + key_eql.release(); + EXPECT_EMPTY_REPORT(driver); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); } @@ -154,28 +219,31 @@ TEST_F(KeyPress, PressPlusEqualReleaseBeforePress) { TEST_F(KeyPress, PressPlusEqualDontReleaseBeforePress) { TestDriver driver; InSequence s; + auto key_plus = KeymapKey(0, 1, 1, KC_PLUS); + auto key_eql = KeymapKey(0, 0, 1, KC_EQUAL); - press_key(1, 1); // KC_PLUS - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL))); + set_keymap({key_plus, key_eql}); + + key_plus.press(); + EXPECT_REPORT(driver, (KC_LEFT_SHIFT)); + EXPECT_REPORT(driver, (KC_LEFT_SHIFT, KC_EQUAL)); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); - press_key(0, 1); // KC_EQL - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL))); + key_eql.press(); + EXPECT_EMPTY_REPORT(driver); + EXPECT_REPORT(driver, (KC_EQUAL)); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); - release_key(1, 1); // KC_PLS - // BUG: Should really still return KC_EQL, but this is fine too - // It's also called twice for some reason - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(2); + key_plus.release(); + // BUG: Should really still return KC_EQUAL, but this is fine too + EXPECT_EMPTY_REPORT(driver); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); - release_key(0, 1); // KC_EQL - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + key_eql.release(); + EXPECT_NO_REPORT(driver); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); } @@ -183,26 +251,30 @@ TEST_F(KeyPress, PressPlusEqualDontReleaseBeforePress) { TEST_F(KeyPress, PressEqualPlusReleaseBeforePress) { TestDriver driver; InSequence s; + auto key_plus = KeymapKey(0, 1, 1, KC_PLUS); + auto key_eql = KeymapKey(0, 0, 1, KC_EQUAL); + + set_keymap({key_plus, key_eql}); - press_key(0, 1); // KC_EQL - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL))); + key_eql.press(); + EXPECT_REPORT(driver, (KC_EQUAL)); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); - release_key(0, 1); // KQ_EQL - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + key_eql.release(); + EXPECT_EMPTY_REPORT(driver); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); - press_key(1, 1); // KC_PLUS - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL))); + key_plus.press(); + EXPECT_REPORT(driver, (KC_LEFT_SHIFT)); + EXPECT_REPORT(driver, (KC_LEFT_SHIFT, KC_EQUAL)); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); - release_key(1, 1); // KC_PLUS - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + key_plus.release(); + EXPECT_REPORT(driver, (KC_LEFT_SHIFT)); + EXPECT_EMPTY_REPORT(driver); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); } @@ -210,31 +282,33 @@ TEST_F(KeyPress, PressEqualPlusReleaseBeforePress) { TEST_F(KeyPress, PressEqualPlusDontReleaseBeforePress) { TestDriver driver; InSequence s; + auto key_plus = KeymapKey(0, 1, 1, KC_PLUS); + auto key_eql = KeymapKey(0, 0, 1, KC_EQUAL); + + set_keymap({key_plus, key_eql}); - press_key(0, 1); // KC_EQL - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL))); + key_eql.press(); + EXPECT_REPORT(driver, (KC_EQUAL)); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); - press_key(1, 1); // KC_PLUS + key_plus.press(); // BUG: The sequence is a bit strange, but it works, the end result is that // KC_PLUS is sent - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL))); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL))); + EXPECT_REPORT(driver, (KC_LEFT_SHIFT, KC_EQUAL)); + EXPECT_REPORT(driver, (KC_LEFT_SHIFT)); + EXPECT_REPORT(driver, (KC_LEFT_SHIFT, KC_EQUAL)); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); - release_key(0, 1); // KC_EQL + key_eql.release(); // I guess it's fine to still report shift here - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); + EXPECT_REPORT(driver, (KC_LEFT_SHIFT)); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); - release_key(1, 1); // KC_PLUS - // This report is not needed - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + key_plus.release(); + EXPECT_EMPTY_REPORT(driver); run_one_scan_loop(); testing::Mock::VerifyAndClearExpectations(&driver); } diff --git a/tests/basic/test_macro.cpp b/tests/basic/test_macro.cpp deleted file mode 100644 index dc4a77796a..0000000000 --- a/tests/basic/test_macro.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* Copyright 2017 Fred Sundvik - * - * 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 "test_common.hpp" -#include "time.h" - -using testing::InSequence; -using testing::InvokeWithoutArgs; - -class Macro : public TestFixture {}; - -#define AT_TIME(t) WillOnce(InvokeWithoutArgs([current_time]() { EXPECT_EQ(timer_elapsed32(current_time), t); })) - -TEST_F(Macro, PlayASimpleMacro) { - TestDriver driver; - InSequence s; - press_key(8, 0); - uint32_t current_time = timer_read32(); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))).AT_TIME(0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_H))).AT_TIME(0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))).AT_TIME(0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).AT_TIME(0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_E))).AT_TIME(0); - // The macro system could actually skip these empty keyboard reports - // it should be enough to just send a report with the next key down - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).AT_TIME(0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_L))).AT_TIME(0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).AT_TIME(0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_L))).AT_TIME(0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).AT_TIME(0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_O))).AT_TIME(0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).AT_TIME(0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_SPACE))).AT_TIME(0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).AT_TIME(0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))).AT_TIME(100); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_W))).AT_TIME(100); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))).AT_TIME(100); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).AT_TIME(100); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_O))) - // BUG: The timer should not really have advanced 10 ms here - // See issue #1477 - .AT_TIME(110); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())) - // BUG: The timer should not advance on both keydown and key-up - // See issue #1477 - .AT_TIME(120); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_R))).AT_TIME(130); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).AT_TIME(140); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_L))).AT_TIME(150); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).AT_TIME(160); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_D))).AT_TIME(170); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).AT_TIME(180); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))).AT_TIME(190); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_1))).AT_TIME(200); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))).AT_TIME(210); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).AT_TIME(220); - run_one_scan_loop(); -} diff --git a/tests/basic/test_one_shot_keys.cpp b/tests/basic/test_one_shot_keys.cpp new file mode 100644 index 0000000000..bb14221140 --- /dev/null +++ b/tests/basic/test_one_shot_keys.cpp @@ -0,0 +1,195 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 "action_util.h" +#include "keyboard_report_util.hpp" +#include "test_common.hpp" + +using testing::_; +using testing::InSequence; + +class OneShot : public TestFixture {}; +class OneShotParametrizedTestFixture : public ::testing::WithParamInterface<std::pair<KeymapKey, KeymapKey>>, public OneShot {}; + +TEST_F(OneShot, OSMWithoutAdditionalKeypressDoesNothing) { + TestDriver driver; + auto osm_key = KeymapKey(0, 0, 0, OSM(MOD_LSFT), KC_LSFT); + + set_keymap({osm_key}); + + /* Press and release OSM key*/ + EXPECT_NO_REPORT(driver); + osm_key.press(); + run_one_scan_loop(); + osm_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* OSM are added when an actual report is send */ + EXPECT_REPORT(driver, (osm_key.report_code)); + send_keyboard_report(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Make unit-test pass */ + clear_oneshot_mods(); +} + +#if defined(ONESHOT_TIMEOUT) + +TEST_P(OneShotParametrizedTestFixture, OSMExpiredDoesNothing) { + TestDriver driver; + KeymapKey osm_key = GetParam().first; + KeymapKey regular_key = GetParam().second; + + set_keymap({osm_key, regular_key}); + + /* Press and release OSM */ + EXPECT_NO_REPORT(driver); + osm_key.press(); + run_one_scan_loop(); + osm_key.release(); + idle_for(ONESHOT_TIMEOUT); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key */ + EXPECT_REPORT(driver, (regular_key.report_code)).Times(1); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_EMPTY_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +#endif + +TEST_P(OneShotParametrizedTestFixture, OSMWithAdditionalKeypress) { + TestDriver driver; + KeymapKey osm_key = GetParam().first; + KeymapKey regular_key = GetParam().second; + + set_keymap({osm_key, regular_key}); + + /* Press and release OSM */ + EXPECT_NO_REPORT(driver); + osm_key.press(); + run_one_scan_loop(); + osm_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key */ + EXPECT_REPORT(driver, (osm_key.report_code, regular_key.report_code)).Times(1); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_EMPTY_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_P(OneShotParametrizedTestFixture, OSMAsRegularModifierWithAdditionalKeypress) { + TestDriver driver; + testing::InSequence s; + + KeymapKey osm_key = GetParam().first; + KeymapKey regular_key = GetParam().second; + + set_keymap({osm_key, regular_key}); + + /* Press OSM */ + EXPECT_NO_REPORT(driver); + osm_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_NO_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release OSM */ + EXPECT_REPORT(driver, (regular_key.report_code, osm_key.report_code)).Times(1); + EXPECT_EMPTY_REPORT(driver); + osm_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// clang-format off + +INSTANTIATE_TEST_CASE_P( + OneShotModifierTests, + OneShotParametrizedTestFixture, + ::testing::Values( + /* first is osm key, second is regular key. */ + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_LSFT), KC_LSFT}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_LCTL), KC_LCTL}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_LALT), KC_LALT}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_LGUI), KC_LGUI}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_RCTL), KC_RCTL}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_RSFT), KC_RSFT}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_RALT), KC_RALT}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_RGUI), KC_RGUI}, KeymapKey{0, 1, 1, KC_A}) + )); +// clang-format on + +TEST_F(OneShot, OSLWithAdditionalKeypress) { + TestDriver driver; + InSequence s; + KeymapKey osl_key = KeymapKey{0, 0, 0, OSL(1)}; + KeymapKey regular_key = KeymapKey{1, 1, 0, KC_A}; + + set_keymap({osl_key, regular_key}); + + /* Press OSL key */ + EXPECT_NO_REPORT(driver); + osl_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release OSL key */ + EXPECT_NO_REPORT(driver); + osl_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key */ + EXPECT_REPORT(driver, (regular_key.report_code)).Times(1); + EXPECT_EMPTY_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_NO_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/basic/test_tapping.cpp b/tests/basic/test_tapping.cpp index 30d032e9fd..6ff9cfe22b 100644 --- a/tests/basic/test_tapping.cpp +++ b/tests/basic/test_tapping.cpp @@ -14,8 +14,11 @@ * 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" using testing::_; using testing::InSequence; @@ -25,28 +28,42 @@ class Tapping : public TestFixture {}; TEST_F(Tapping, TapA_SHFT_T_KeyReportsKey) { TestDriver driver; InSequence s; + auto key_shift_hold_p_tap = KeymapKey(0, 7, 0, SFT_T(KC_P)); + + set_keymap({key_shift_hold_p_tap}); - press_key(7, 0); // Tapping keys does nothing on press - EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); + key_shift_hold_p_tap.press(); + EXPECT_NO_REPORT(driver); run_one_scan_loop(); - release_key(7, 0); + // First we get the key press - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_P))); + key_shift_hold_p_tap.release(); + EXPECT_REPORT(driver, (KC_P)); + // Then the release - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + EXPECT_EMPTY_REPORT(driver); run_one_scan_loop(); } TEST_F(Tapping, HoldA_SHFT_T_KeyReportsShift) { TestDriver driver; InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 7, 0, SFT_T(KC_P)); + + set_keymap({mod_tap_hold_key}); + + mod_tap_hold_key.press(); - press_key(7, 0); // Tapping keys does nothing on press - EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); + EXPECT_NO_REPORT(driver); idle_for(TAPPING_TERM); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))); + + EXPECT_REPORT(driver, (KC_LSFT)); + run_one_scan_loop(); + + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); run_one_scan_loop(); } @@ -54,44 +71,53 @@ TEST_F(Tapping, ANewTapWithinTappingTermIsBuggy) { // See issue #1478 for more information TestDriver driver; InSequence s; + auto key_shift_hold_p_tap = KeymapKey(0, 7, 0, SFT_T(KC_P)); + + set_keymap({key_shift_hold_p_tap}); - press_key(7, 0); // Tapping keys does nothing on press - EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); + key_shift_hold_p_tap.press(); + EXPECT_NO_REPORT(driver); run_one_scan_loop(); - release_key(7, 0); + key_shift_hold_p_tap.release(); + // First we get the key press - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_P))); + EXPECT_REPORT(driver, (KC_P)); // Then the release - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + EXPECT_EMPTY_REPORT(driver); run_one_scan_loop(); // This sends KC_P, even if it should do nothing - press_key(7, 0); + key_shift_hold_p_tap.press(); // This test should not succed if everything works correctly - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_P))); + EXPECT_REPORT(driver, (KC_P)); run_one_scan_loop(); - release_key(7, 0); - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + + key_shift_hold_p_tap.release(); + EXPECT_EMPTY_REPORT(driver); idle_for(TAPPING_TERM + 1); // On the other hand, nothing is sent if we are outside the tapping term - press_key(7, 0); - EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); + key_shift_hold_p_tap.press(); + EXPECT_NO_REPORT(driver); run_one_scan_loop(); - release_key(7, 0); + key_shift_hold_p_tap.release(); // First we get the key press - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_P))); + EXPECT_REPORT(driver, (KC_P)); // Then the release - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + EXPECT_EMPTY_REPORT(driver); idle_for(TAPPING_TERM + 1); // Now we are geting into strange territory, as the hold registers too early here // But the stranges part is: // If TAPPING_TERM + 1 above is changed to TAPPING_TERM or TAPPING_TERM + 2 it doesn't - press_key(7, 0); + key_shift_hold_p_tap.press(); // Shouldn't be called here really - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT))).Times(1); + EXPECT_REPORT(driver, (KC_LEFT_SHIFT)).Times(1); idle_for(TAPPING_TERM); + + EXPECT_EMPTY_REPORT(driver); + key_shift_hold_p_tap.release(); + run_one_scan_loop(); } diff --git a/tests/caps_word/caps_word_autoshift/config.h b/tests/caps_word/caps_word_autoshift/config.h new file mode 100644 index 0000000000..b80f53b9dd --- /dev/null +++ b/tests/caps_word/caps_word_autoshift/config.h @@ -0,0 +1,22 @@ +// Copyright 2022 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 TAPPING_TERM 200 +#define AUTO_SHIFT_TIMEOUT 150 +#define RETRO_SHIFT 500 diff --git a/tests/caps_word/caps_word_autoshift/test.mk b/tests/caps_word/caps_word_autoshift/test.mk new file mode 100644 index 0000000000..7f717d7fc1 --- /dev/null +++ b/tests/caps_word/caps_word_autoshift/test.mk @@ -0,0 +1,18 @@ +# Copyright 2022 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 +AUTO_SHIFT_ENABLE = yes + diff --git a/tests/caps_word/caps_word_autoshift/test_caps_word_autoshift.cpp b/tests/caps_word/caps_word_autoshift/test_caps_word_autoshift.cpp new file mode 100644 index 0000000000..deb4d95766 --- /dev/null +++ b/tests/caps_word/caps_word_autoshift/test_caps_word_autoshift.cpp @@ -0,0 +1,101 @@ +// Copyright 2022 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; + +class CapsWord : public TestFixture { + public: + void SetUp() override { + caps_word_off(); + } +}; + +// Tests that with Auto Shift, letter keys are shifted by Caps Word +// regardless of whether they are released before AUTO_SHIFT_TIMEOUT. +TEST_F(CapsWord, AutoShiftKeys) { + TestDriver driver; + KeymapKey key_a(0, 0, 0, KC_A); + KeymapKey key_spc(0, 1, 0, KC_SPC); + set_keymap({key_a, key_spc}); + + // 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: "A, A, space, a". + InSequence s; + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_SPC)); + EXPECT_REPORT(driver, (KC_A)); + } + + // Turn on Caps Word and type "A (quick tap), A (long press), space, A". + caps_word_on(); + + tap_key(key_a); // Tap A quickly. + tap_key(key_a, AUTO_SHIFT_TIMEOUT + 1); // Long press A. + tap_key(key_spc); + tap_key(key_a); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests that with tap-hold keys with Retro Shift, letter keys are shifted by +// Caps Word regardless of whether they are retroshifted. +TEST_F(CapsWord, RetroShiftKeys) { + TestDriver driver; + KeymapKey key_modtap_a(0, 0, 0, LCTL_T(KC_A)); + KeymapKey key_layertap_b(0, 1, 0, LT(1, KC_B)); + set_keymap({key_modtap_a, key_layertap_b}); + + // 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: "B, A, B, A". + InSequence s; + EXPECT_REPORT(driver, (KC_LSFT, KC_B)); + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_LSFT, KC_B)); + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + } + + // Turn on Caps Word and type "B, A (long press), B (long press), A". + caps_word_on(); + + tap_key(key_layertap_b); // Tap B quickly. + tap_key(key_modtap_a, TAPPING_TERM + 1); // Long press A. + tap_key(key_layertap_b, TAPPING_TERM + 1); // Long press B. + tap_key(key_modtap_a); // Tap A quickly. + + EXPECT_EQ(is_caps_word_on(), true); + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/caps_word/caps_word_unicodemap/config.h b/tests/caps_word/caps_word_unicodemap/config.h new file mode 100644 index 0000000000..89fd7924d4 --- /dev/null +++ b/tests/caps_word/caps_word_unicodemap/config.h @@ -0,0 +1,20 @@ +// Copyright 2022 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 UNICODE_SELECTED_MODES UC_LNX diff --git a/tests/caps_word/caps_word_unicodemap/test.mk b/tests/caps_word/caps_word_unicodemap/test.mk new file mode 100644 index 0000000000..92bcba762c --- /dev/null +++ b/tests/caps_word/caps_word_unicodemap/test.mk @@ -0,0 +1,18 @@ +# Copyright 2022 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 +UNICODEMAP_ENABLE = yes + diff --git a/tests/caps_word/caps_word_unicodemap/test_caps_word_unicodemap.cpp b/tests/caps_word/caps_word_unicodemap/test_caps_word_unicodemap.cpp new file mode 100644 index 0000000000..fb8f9333bb --- /dev/null +++ b/tests/caps_word/caps_word_unicodemap/test_caps_word_unicodemap.cpp @@ -0,0 +1,121 @@ +// Copyright 2022 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; + +extern "C" { +enum unicode_names { + ENDASH, + EMDASH, + DELTA_LOWERCASE, + DELTA_UPPERCASE, +}; + +const uint32_t unicode_map[] PROGMEM = { + [ENDASH] = 0x2013, + [EMDASH] = 0x2014, + [DELTA_LOWERCASE] = 0x03b4, + [DELTA_UPPERCASE] = 0x0394, +}; + +#define U_DASH XP(ENDASH, EMDASH) +#define U_DELTA XP(DELTA_LOWERCASE, DELTA_UPPERCASE) + +bool caps_word_press_user(uint16_t keycode) { + switch (keycode) { + // Keycodes that continue Caps Word, with shift applied. + case U_DELTA: + add_weak_mods(MOD_BIT(KC_LSFT)); // Apply shift to next key. + return true; + + // Keycodes that continue Caps Word, without shifting. + case U_DASH: + return true; + + default: + return false; // Deactivate Caps Word. + } +} +} // extern "C" + +class CapsWord : public TestFixture { + public: + void SetUp() override { + caps_word_off(); + } +}; + +// Tests that typing U_DELTA while Caps Word is on sends the uppercase Delta. +TEST_F(CapsWord, ShiftedUnicodeMapKey) { + TestDriver driver; + KeymapKey key_delta(0, 0, 0, U_DELTA); + KeymapKey key_spc(0, 1, 0, KC_SPC); + set_keymap({key_delta, key_spc}); + + // Allow any number of reports with no keys or only KC_LSFT and KC_LCTL. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT), + KeyboardReport(KC_LCTL, KC_LSFT)))) + .Times(AnyNumber()); + // clang-format on + { // Expect: "Uppercase Delta, space, lowercase delta". + InSequence s; + EXPECT_UNICODE(driver, unicode_map[DELTA_UPPERCASE]); + EXPECT_REPORT(driver, (KC_SPC)); + EXPECT_UNICODE(driver, unicode_map[DELTA_LOWERCASE]); + } + + // Turn on Caps Word and tap "delta, space, delta". + caps_word_on(); + tap_keys(key_delta, key_spc, key_delta); + + EXPECT_EQ(is_caps_word_on(), false); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests typing U_ENDASH while Caps Word is on. +TEST_F(CapsWord, UnshiftedUnicodeMapKey) { + TestDriver driver; + KeymapKey key_dash(0, 0, 0, U_DASH); + set_keymap({key_dash}); + + // Allow any number of reports with no keys or only KC_LSFT and KC_LCTL. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT), + KeyboardReport(KC_LCTL, KC_LSFT)))) + .Times(AnyNumber()); + // clang-format on + EXPECT_UNICODE(driver, unicode_map[ENDASH]); + + // Turn on Caps Word and tap U_DASH key. + caps_word_on(); + tap_key(key_dash); + + EXPECT_EQ(is_caps_word_on(), true); + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/caps_word/config.h b/tests/caps_word/config.h new file mode 100644 index 0000000000..0d5cebd778 --- /dev/null +++ b/tests/caps_word/config.h @@ -0,0 +1,21 @@ +// Copyright 2022 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 BOTH_SHIFTS_TURNS_ON_CAPS_WORD +#define DOUBLE_TAP_SHIFT_TURNS_ON_CAPS_WORD diff --git a/tests/caps_word/test.mk b/tests/caps_word/test.mk new file mode 100644 index 0000000000..2509b01858 --- /dev/null +++ b/tests/caps_word/test.mk @@ -0,0 +1,19 @@ +# Copyright 2022 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 +COMMAND_ENABLE = no +SPACE_CADET_ENABLE = yes + diff --git a/tests/caps_word/test_caps_word.cpp b/tests/caps_word/test_caps_word.cpp new file mode 100644 index 0000000000..0af4b0175d --- /dev/null +++ b/tests/caps_word/test_caps_word.cpp @@ -0,0 +1,437 @@ +// Copyright 2022 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; + +class CapsWord : public TestFixture { + public: + void SetUp() override { + caps_word_off(); + } +}; + +// Tests caps_word_on(), _off(), and _toggle() functions. +TEST_F(CapsWord, OnOffToggleFuns) { + TestDriver driver; + + EXPECT_EQ(is_caps_word_on(), false); + + caps_word_on(); + EXPECT_EQ(is_caps_word_on(), true); + caps_word_on(); + EXPECT_EQ(is_caps_word_on(), true); + + caps_word_off(); + EXPECT_EQ(is_caps_word_on(), false); + caps_word_off(); + EXPECT_EQ(is_caps_word_on(), false); + + caps_word_toggle(); + EXPECT_EQ(is_caps_word_on(), true); + caps_word_toggle(); + EXPECT_EQ(is_caps_word_on(), false); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests the default `caps_word_press_user()` function. +TEST_F(CapsWord, DefaultCapsWordPressUserFun) { + // Spot check some keycodes that continue Caps Word, with shift applied. + for (uint16_t keycode : {KC_A, KC_B, KC_Z, KC_MINS}) { + SCOPED_TRACE("keycode: " + testing::PrintToString(keycode)); + clear_weak_mods(); + EXPECT_TRUE(caps_word_press_user(keycode)); + EXPECT_EQ(get_weak_mods(), MOD_BIT(KC_LSFT)); + } + + // Some keycodes that continue Caps Word, without shifting. + for (uint16_t keycode : {KC_1, KC_9, KC_0, KC_BSPC, KC_DEL}) { + SCOPED_TRACE("keycode: " + testing::PrintToString(keycode)); + clear_weak_mods(); + EXPECT_TRUE(caps_word_press_user(keycode)); + EXPECT_EQ(get_weak_mods(), 0); + } + + // Some keycodes that turn off Caps Word. + for (uint16_t keycode : {KC_SPC, KC_DOT, KC_COMM, KC_TAB, KC_ESC, KC_ENT}) { + SCOPED_TRACE("keycode: " + testing::PrintToString(keycode)); + EXPECT_FALSE(caps_word_press_user(keycode)); + } +} + +// Tests that `CAPSWRD` key toggles Caps Word. +TEST_F(CapsWord, CapswrdKey) { + TestDriver driver; + KeymapKey key_capswrd(0, 0, 0, CAPSWRD); + set_keymap({key_capswrd}); + + // No keyboard reports should be sent. + EXPECT_NO_REPORT(driver); + + tap_key(key_capswrd); // Tap the CAPSWRD key. + EXPECT_EQ(is_caps_word_on(), true); + + tap_key(key_capswrd); // Tap the CAPSWRD key again. + EXPECT_EQ(is_caps_word_on(), false); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests that being idle for CAPS_WORD_IDLE_TIMEOUT turns off Caps Word. +TEST_F(CapsWord, IdleTimeout) { + TestDriver driver; + KeymapKey key_a(0, 0, 0, KC_A); + set_keymap({key_a}); + + // 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 "Shift+A". + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + + // Turn on Caps Word and tap "A". + caps_word_on(); + tap_key(key_a); + + testing::Mock::VerifyAndClearExpectations(&driver); + + idle_for(CAPS_WORD_IDLE_TIMEOUT); + run_one_scan_loop(); + + // Caps Word should be off and mods should be clear. + EXPECT_EQ(is_caps_word_on(), false); + EXPECT_EQ(get_mods() | get_weak_mods(), 0); + + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + // Expect unshifted "A". + EXPECT_REPORT(driver, (KC_A)); + tap_key(key_a); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests that typing "A, 4, A, 4" produces "Shift+A, 4, Shift+A, 4". +TEST_F(CapsWord, ShiftsLettersButNotDigits) { + TestDriver driver; + KeymapKey key_a(0, 0, 0, KC_A); + KeymapKey key_4(0, 1, 0, KC_4); + set_keymap({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: "Shift+A, 4, Shift+A, 4". + InSequence s; + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_4)); + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_4)); + } + + // Turn on Caps Word and tap "A, 4, A, 4". + caps_word_on(); + tap_keys(key_a, key_4, key_a, key_4); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests that typing "A, Space, A" produces "Shift+A, Space, A". +TEST_F(CapsWord, SpaceTurnsOffCapsWord) { + TestDriver driver; + KeymapKey key_a(0, 0, 0, KC_A); + KeymapKey key_spc(0, 1, 0, KC_SPC); + set_keymap({key_a, key_spc}); + + // 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: "Shift+A, Space, A". + InSequence seq; + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_SPC)); + EXPECT_REPORT(driver, (KC_A)); + } + + // Turn on Caps Word and tap "A, Space, A". + caps_word_on(); + tap_keys(key_a, key_spc, key_a); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Tests that typing "AltGr + A" produces "Shift + AltGr + A". +TEST_F(CapsWord, ShiftsAltGrSymbols) { + TestDriver driver; + KeymapKey key_a(0, 0, 0, KC_A); + KeymapKey key_altgr(0, 1, 0, KC_RALT); + set_keymap({key_a, key_altgr}); + + // Allow any number of reports with no keys or only modifiers. + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_RALT), + KeyboardReport(KC_LSFT, KC_RALT)))) + .Times(AnyNumber()); + // Expect "Shift + AltGr + A". + EXPECT_REPORT(driver, (KC_LSFT, KC_RALT, KC_A)); + // clang-format on + + // Turn on Caps Word and type "AltGr + A". + caps_word_on(); + + key_altgr.press(); + run_one_scan_loop(); + tap_key(key_a); + run_one_scan_loop(); + key_altgr.release(); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +struct CapsWordBothShiftsParams { + std::string name; + uint16_t left_shift_keycode; + uint16_t right_shift_keycode; + + static const std::string& GetName(const TestParamInfo<CapsWordBothShiftsParams>& info) { + return info.param.name; + } +}; + +// Tests the BOTH_SHIFTS_TURNS_ON_CAPS_WORD method to turn on Caps Word. +class CapsWordBothShifts : public ::testing::WithParamInterface<CapsWordBothShiftsParams>, public CapsWord {}; + +// Pressing shifts as "Left down, Right down, Left up, Right up". +TEST_P(CapsWordBothShifts, PressLRLR) { + TestDriver driver; + KeymapKey left_shift(0, 0, 0, GetParam().left_shift_keycode); + KeymapKey right_shift(0, 1, 0, GetParam().right_shift_keycode); + set_keymap({left_shift, right_shift}); + + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT), + KeyboardReport(KC_RSFT), + KeyboardReport(KC_LSFT, KC_RSFT)))) + .Times(AnyNumber()); + // clang-format on + + EXPECT_EQ(is_caps_word_on(), false); + + left_shift.press(); // Press both shifts. + run_one_scan_loop(); + right_shift.press(); + + // For mod-tap and Space Cadet keys, wait for the tapping term. + if (left_shift.code == LSFT_T(KC_A) || left_shift.code == KC_LSPO) { + idle_for(TAPPING_TERM); + } + + run_one_scan_loop(); + left_shift.release(); // Release both. + run_one_scan_loop(); + right_shift.release(); + run_one_scan_loop(); + + EXPECT_EQ(is_caps_word_on(), true); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Pressing shifts as "Left down, Right down, Right up, Left up". +TEST_P(CapsWordBothShifts, PressLRRL) { + TestDriver driver; + KeymapKey left_shift(0, 0, 0, GetParam().left_shift_keycode); + KeymapKey right_shift(0, 1, 0, GetParam().right_shift_keycode); + set_keymap({left_shift, right_shift}); + + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT), + KeyboardReport(KC_RSFT), + KeyboardReport(KC_LSFT, KC_RSFT)))) + .Times(AnyNumber()); + // clang-format on + + EXPECT_EQ(is_caps_word_on(), false); + + left_shift.press(); // Press both shifts. + run_one_scan_loop(); + right_shift.press(); + + if (left_shift.code == LSFT_T(KC_A) || left_shift.code == KC_LSPO) { + idle_for(TAPPING_TERM); + } + run_one_scan_loop(); + + right_shift.release(); // Release both. + run_one_scan_loop(); + left_shift.release(); + run_one_scan_loop(); + + EXPECT_EQ(is_caps_word_on(), true); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// clang-format off +INSTANTIATE_TEST_CASE_P( + ShiftPairs, + CapsWordBothShifts, + ::testing::Values( + CapsWordBothShiftsParams{ + "PlainShifts", KC_LSFT, KC_RSFT}, + CapsWordBothShiftsParams{ + "OneshotShifts", OSM(MOD_LSFT), OSM(MOD_RSFT)}, + CapsWordBothShiftsParams{ + "SpaceCadetShifts", KC_LSPO, KC_RSPC}, + CapsWordBothShiftsParams{ + "ModTapShifts", LSFT_T(KC_A), RSFT_T(KC_B)} + ), + CapsWordBothShiftsParams::GetName + ); +// clang-format on + +struct CapsWordDoubleTapShiftParams { + std::string name; + uint16_t left_shift_keycode; + + static const std::string& GetName(const TestParamInfo<CapsWordDoubleTapShiftParams>& info) { + return info.param.name; + } +}; + +// Tests the DOUBLE_TAP_SHIFT_TURNS_ON_CAPS_WORD method to turn on Caps Word. +class CapsWordDoubleTapShift : public ::testing::WithParamInterface<CapsWordDoubleTapShiftParams>, public CapsWord {}; + +// Tests that double tapping activates Caps Word. +TEST_P(CapsWordDoubleTapShift, Activation) { + TestDriver driver; + KeymapKey left_shift(0, 0, 0, GetParam().left_shift_keycode); + set_keymap({left_shift}); + + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT)))) + .Times(AnyNumber()); + // clang-format on + + EXPECT_EQ(is_caps_word_on(), false); + + // Tapping shift twice within the tapping term turns on Caps Word. + tap_key(left_shift); + idle_for(TAPPING_TERM - 10); + tap_key(left_shift); + + EXPECT_EQ(is_caps_word_on(), true); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Double tap doesn't count if another key is pressed between the taps. +TEST_P(CapsWordDoubleTapShift, Interrupted) { + TestDriver driver; + KeymapKey left_shift(0, 0, 0, GetParam().left_shift_keycode); + KeymapKey key_a(0, 1, 0, KC_A); + set_keymap({left_shift, key_a}); + + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT), + KeyboardReport(KC_LSFT, KC_A)))) + .Times(AnyNumber()); + // clang-format on + + left_shift.press(); + run_one_scan_loop(); + + tap_key(key_a); // 'A' key interrupts the double tap. + + left_shift.release(); + run_one_scan_loop(); + + idle_for(TAPPING_TERM - 10); + tap_key(left_shift); + + EXPECT_EQ(is_caps_word_on(), false); // Caps Word is still off. + clear_oneshot_mods(); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// Double tap doesn't count if taps are more than tapping term apart. +TEST_P(CapsWordDoubleTapShift, SlowTaps) { + TestDriver driver; + KeymapKey left_shift(0, 0, 0, GetParam().left_shift_keycode); + set_keymap({left_shift}); + + // clang-format off + EXPECT_CALL(driver, send_keyboard_mock(AnyOf( + KeyboardReport(), + KeyboardReport(KC_LSFT)))) + .Times(AnyNumber()); + // clang-format on + + tap_key(left_shift); + idle_for(TAPPING_TERM + 1); + tap_key(left_shift); + + EXPECT_EQ(is_caps_word_on(), false); // Caps Word is still off. + clear_oneshot_mods(); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// clang-format off +INSTANTIATE_TEST_CASE_P( + Shifts, + CapsWordDoubleTapShift, + ::testing::Values( + CapsWordDoubleTapShiftParams{"PlainShift", KC_LSFT}, + CapsWordDoubleTapShiftParams{"OneshotShift", OSM(MOD_LSFT)} + ), + CapsWordDoubleTapShiftParams::GetName + ); +// clang-format on diff --git a/tests/secure/config.h b/tests/secure/config.h new file mode 100644 index 0000000000..3cfbc6cb14 --- /dev/null +++ b/tests/secure/config.h @@ -0,0 +1,32 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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" + +// clang-format off +#define SECURE_UNLOCK_SEQUENCE \ + { \ + {0, 1}, \ + {0, 2}, \ + {0, 3}, \ + {0, 4} \ + } +// clang-format on + +#define SECURE_UNLOCK_TIMEOUT 20 +#define SECURE_IDLE_TIMEOUT 50 diff --git a/tests/secure/test.mk b/tests/secure/test.mk new file mode 100644 index 0000000000..ea406493be --- /dev/null +++ b/tests/secure/test.mk @@ -0,0 +1,20 @@ +# Copyright 2021 Stefan Kerkmann +# +# 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/>. + +# -------------------------------------------------------------------------------- +# Keep this file, even if it is empty, as a marker that this folder contains tests +# -------------------------------------------------------------------------------- + +SECURE_ENABLE = yes diff --git a/tests/secure/test_secure.cpp b/tests/secure/test_secure.cpp new file mode 100644 index 0000000000..6ca98d78f3 --- /dev/null +++ b/tests/secure/test_secure.cpp @@ -0,0 +1,260 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 "gtest/gtest.h" +#include "keyboard_report_util.hpp" +#include "test_common.hpp" + +using testing::_; +using testing::AnyNumber; +using testing::InSequence; + +class Secure : public TestFixture { + public: + void SetUp() override { + secure_lock(); + } +}; + +TEST_F(Secure, test_lock) { + TestDriver driver; + + // Don't allow empty reports. + EXPECT_NO_REPORT(driver); + + EXPECT_FALSE(secure_is_unlocked()); + secure_unlock(); + EXPECT_TRUE(secure_is_unlocked()); + run_one_scan_loop(); + EXPECT_TRUE(secure_is_unlocked()); + secure_lock(); + EXPECT_FALSE(secure_is_unlocked()); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(Secure, test_unlock_timeout) { + TestDriver driver; + + // Don't allow empty reports. + EXPECT_NO_REPORT(driver); + + EXPECT_FALSE(secure_is_unlocked()); + secure_unlock(); + EXPECT_TRUE(secure_is_unlocked()); + idle_for(SECURE_IDLE_TIMEOUT + 1); + EXPECT_FALSE(secure_is_unlocked()); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(Secure, test_unlock_request) { + TestDriver driver; + auto key_mo = KeymapKey(0, 0, 0, MO(1)); + auto key_a = KeymapKey(0, 1, 0, KC_A); + auto key_b = KeymapKey(0, 2, 0, KC_B); + auto key_c = KeymapKey(0, 3, 0, KC_C); + auto key_d = KeymapKey(0, 4, 0, KC_D); + + set_keymap({key_mo, key_a, key_b, key_c, key_d}); + + // Don't allow empty reports. + EXPECT_NO_REPORT(driver); + + EXPECT_TRUE(secure_is_locked()); + secure_request_unlock(); + EXPECT_TRUE(secure_is_unlocking()); + tap_keys(key_a, key_b, key_c, key_d); + EXPECT_TRUE(secure_is_unlocked()); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(Secure, test_unlock_request_fail) { + TestDriver driver; + auto key_e = KeymapKey(0, 0, 0, KC_E); + auto key_a = KeymapKey(0, 1, 0, KC_A); + auto key_b = KeymapKey(0, 2, 0, KC_B); + auto key_c = KeymapKey(0, 3, 0, KC_C); + auto key_d = KeymapKey(0, 4, 0, KC_D); + + set_keymap({key_e, key_a, key_b, key_c, key_d}); + + // Allow any number of empty reports. + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + { // Expect the following reports in this order. + InSequence s; + EXPECT_REPORT(driver, (KC_A)); + EXPECT_REPORT(driver, (KC_B)); + EXPECT_REPORT(driver, (KC_C)); + EXPECT_REPORT(driver, (KC_D)); + } + EXPECT_TRUE(secure_is_locked()); + secure_request_unlock(); + EXPECT_TRUE(secure_is_unlocking()); + tap_keys(key_e, key_a, key_b, key_c, key_d); + EXPECT_FALSE(secure_is_unlocked()); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(Secure, test_unlock_request_timeout) { + TestDriver driver; + + // Don't allow empty reports. + EXPECT_NO_REPORT(driver); + + EXPECT_FALSE(secure_is_unlocked()); + secure_request_unlock(); + EXPECT_TRUE(secure_is_unlocking()); + idle_for(SECURE_UNLOCK_TIMEOUT + 1); + EXPECT_FALSE(secure_is_unlocking()); + EXPECT_FALSE(secure_is_unlocked()); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(Secure, test_unlock_request_fail_mid) { + TestDriver driver; + auto key_e = KeymapKey(0, 0, 0, KC_E); + auto key_a = KeymapKey(0, 1, 0, KC_A); + auto key_b = KeymapKey(0, 2, 0, KC_B); + auto key_c = KeymapKey(0, 3, 0, KC_C); + auto key_d = KeymapKey(0, 4, 0, KC_D); + + set_keymap({key_e, key_a, key_b, key_c, key_d}); + + // Allow any number of empty reports. + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + { // Expect the following reports in this order. + InSequence s; + EXPECT_REPORT(driver, (KC_C)); + EXPECT_REPORT(driver, (KC_D)); + } + EXPECT_FALSE(secure_is_unlocked()); + secure_request_unlock(); + EXPECT_TRUE(secure_is_unlocking()); + tap_keys(key_a, key_b, key_e, key_c, key_d); + EXPECT_FALSE(secure_is_unlocking()); + EXPECT_FALSE(secure_is_unlocked()); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(Secure, test_unlock_request_fail_out_of_order) { + TestDriver driver; + auto key_e = KeymapKey(0, 0, 0, KC_E); + auto key_a = KeymapKey(0, 1, 0, KC_A); + auto key_b = KeymapKey(0, 2, 0, KC_B); + auto key_c = KeymapKey(0, 3, 0, KC_C); + auto key_d = KeymapKey(0, 4, 0, KC_D); + + set_keymap({key_e, key_a, key_b, key_c, key_d}); + + // Allow any number of empty reports. + EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + { // Expect the following reports in this order. + InSequence s; + EXPECT_REPORT(driver, (KC_B)); + EXPECT_REPORT(driver, (KC_C)); + } + EXPECT_FALSE(secure_is_unlocked()); + secure_request_unlock(); + EXPECT_TRUE(secure_is_unlocking()); + tap_keys(key_a, key_d, key_b, key_c); + EXPECT_TRUE(secure_is_locked()); + EXPECT_FALSE(secure_is_unlocking()); + EXPECT_FALSE(secure_is_unlocked()); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(Secure, test_unlock_request_on_layer) { + TestDriver driver; + auto key_mo = KeymapKey(0, 0, 0, MO(1)); + auto key_a = KeymapKey(0, 1, 0, KC_A); + auto key_b = KeymapKey(0, 2, 0, KC_B); + auto key_c = KeymapKey(0, 3, 0, KC_C); + auto key_d = KeymapKey(0, 4, 0, KC_D); + + set_keymap({key_mo, key_a, key_b, key_c, key_d}); + + // Don't allow empty reports. + EXPECT_NO_REPORT(driver); + + EXPECT_TRUE(secure_is_locked()); + key_mo.press(); + run_one_scan_loop(); + secure_request_unlock(); + key_mo.release(); + run_one_scan_loop(); + EXPECT_TRUE(secure_is_unlocking()); + tap_keys(key_a, key_b, key_c, key_d); + EXPECT_TRUE(secure_is_unlocked()); + EXPECT_FALSE(layer_state_is(1)); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(Secure, test_unlock_request_mid_stroke) { + TestDriver driver; + auto key_e = KeymapKey(0, 0, 0, KC_E); + auto key_a = KeymapKey(0, 1, 0, KC_A); + auto key_b = KeymapKey(0, 2, 0, KC_B); + auto key_c = KeymapKey(0, 3, 0, KC_C); + auto key_d = KeymapKey(0, 4, 0, KC_D); + + set_keymap({key_e, key_a, key_b, key_c, key_d}); + + EXPECT_REPORT(driver, (KC_E)); + EXPECT_EMPTY_REPORT(driver); + EXPECT_TRUE(secure_is_locked()); + key_e.press(); + run_one_scan_loop(); + secure_request_unlock(); + key_e.release(); + run_one_scan_loop(); + EXPECT_TRUE(secure_is_unlocking()); + tap_keys(key_a, key_b, key_c, key_d); + EXPECT_TRUE(secure_is_unlocked()); + + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(Secure, test_unlock_request_mods) { + TestDriver driver; + auto key_lsft = KeymapKey(0, 0, 0, KC_LSFT); + auto key_a = KeymapKey(0, 1, 0, KC_A); + auto key_b = KeymapKey(0, 2, 0, KC_B); + auto key_c = KeymapKey(0, 3, 0, KC_C); + auto key_d = KeymapKey(0, 4, 0, KC_D); + + set_keymap({key_lsft, key_a, key_b, key_c, key_d}); + + EXPECT_REPORT(driver, (key_lsft.report_code)); + EXPECT_EMPTY_REPORT(driver); + EXPECT_TRUE(secure_is_locked()); + key_lsft.press(); + run_one_scan_loop(); + secure_request_unlock(); + key_lsft.release(); + run_one_scan_loop(); + EXPECT_TRUE(secure_is_unlocking()); + tap_keys(key_a, key_b, key_c, key_d); + EXPECT_TRUE(secure_is_unlocked()); + + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/tap_hold_configurations/default_mod_tap/config.h b/tests/tap_hold_configurations/default_mod_tap/config.h new file mode 100644 index 0000000000..5955b8600a --- /dev/null +++ b/tests/tap_hold_configurations/default_mod_tap/config.h @@ -0,0 +1,21 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 IGNORE_MOD_TAP_INTERRUPT
\ No newline at end of file diff --git a/tests/tap_hold_configurations/default_mod_tap/test.mk b/tests/tap_hold_configurations/default_mod_tap/test.mk new file mode 100644 index 0000000000..cfab996e0e --- /dev/null +++ b/tests/tap_hold_configurations/default_mod_tap/test.mk @@ -0,0 +1,18 @@ +# Copyright 2021 Stefan Kerkmann +# +# 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/>. + +# -------------------------------------------------------------------------------- +# Keep this file, even if it is empty, as a marker that this folder contains tests +# --------------------------------------------------------------------------------
\ No newline at end of file diff --git a/tests/tap_hold_configurations/default_mod_tap/test_tap_hold.cpp b/tests/tap_hold_configurations/default_mod_tap/test_tap_hold.cpp new file mode 100644 index 0000000000..687a4e0318 --- /dev/null +++ b/tests/tap_hold_configurations/default_mod_tap/test_tap_hold.cpp @@ -0,0 +1,230 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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_fixture.hpp" +#include "test_keymap_key.hpp" + +using testing::_; +using testing::InSequence; + +class DefaultTapHold : public TestFixture {}; + +TEST_F(DefaultTapHold, tap_regular_key_while_mod_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + auto regular_key = KeymapKey(0, 2, 0, KC_A); + + set_keymap({mod_tap_hold_key, regular_key}); + + /* Press mod-tap-hold key. */ + EXPECT_NO_REPORT(driver); + mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key. */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key. */ + EXPECT_NO_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key. */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_REPORT(driver, (KC_P, KC_A)); + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Idle for tapping term of mod tap hold key. */ + idle_for(TAPPING_TERM - 3); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(DefaultTapHold, tap_mod_tap_key_while_mod_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto first_mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + auto second_mod_tap_hold_key = KeymapKey(0, 2, 0, RSFT_T(KC_A)); + + set_keymap({first_mod_tap_hold_key, second_mod_tap_hold_key}); + + /* Press first mod-tap-hold key */ + EXPECT_NO_REPORT(driver); + first_mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press second tap-hold key */ + EXPECT_NO_REPORT(driver); + second_mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release second tap-hold key */ + EXPECT_NO_REPORT(driver); + second_mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release first mod-tap-hold key */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_REPORT(driver, (KC_P, KC_A)); + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + first_mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(DefaultTapHold, tap_regular_key_while_layer_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto layer_tap_hold_key = KeymapKey(0, 1, 0, LT(1, KC_P)); + auto regular_key = KeymapKey(0, 2, 0, KC_A); + auto layer_key = KeymapKey(1, 2, 0, KC_B); + + set_keymap({layer_tap_hold_key, regular_key, layer_key}); + + /* Press layer-tap-hold key */ + EXPECT_NO_REPORT(driver); + layer_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_NO_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release layer-tap-hold key */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_REPORT(driver, (KC_P, KC_A)); + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + layer_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(DefaultTapHold, tap_mod_tap_hold_key_two_times) { + GTEST_SKIP() << "TODO:Holding a modtap key results in out of bounds access to the keymap, this is a bug in QMK."; + + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + + set_keymap({mod_tap_hold_key}); + + /* Press mod-tap-hold key. */ + EXPECT_NO_REPORT(driver); + mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key. */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press mod-tap-hold key again. */ + EXPECT_REPORT(driver, (KC_P)); + mod_tap_hold_key.press(); + idle_for(TAPPING_TERM); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key. */ + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(DefaultTapHold, tap_mod_tap_hold_key_twice_and_hold_on_second_time) { + GTEST_SKIP() << "TODO:Holding a modtap key results in out of bounds access to the keymap, this is a bug in QMK."; + + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + + set_keymap({mod_tap_hold_key}); + + /* Press mod-tap-hold key. */ + EXPECT_NO_REPORT(driver); + mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key. */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press mod-tap-hold key again. */ + EXPECT_REPORT(driver, (KC_P)); + mod_tap_hold_key.press(); + idle_for(TAPPING_TERM); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key. */ + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(DefaultTapHold, tap_and_hold_mod_tap_hold_key) { + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + + set_keymap({mod_tap_hold_key}); + + /* Press mod-tap-hold key. */ + EXPECT_REPORT(driver, (KC_LSHIFT)); + mod_tap_hold_key.press(); + idle_for(TAPPING_TERM + 1); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key. */ + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/tap_hold_configurations/ignore_mod_tap_interrupt/config.h b/tests/tap_hold_configurations/ignore_mod_tap_interrupt/config.h new file mode 100644 index 0000000000..5955b8600a --- /dev/null +++ b/tests/tap_hold_configurations/ignore_mod_tap_interrupt/config.h @@ -0,0 +1,21 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 IGNORE_MOD_TAP_INTERRUPT
\ No newline at end of file diff --git a/tests/tap_hold_configurations/ignore_mod_tap_interrupt/test.mk b/tests/tap_hold_configurations/ignore_mod_tap_interrupt/test.mk new file mode 100644 index 0000000000..efecca2c22 --- /dev/null +++ b/tests/tap_hold_configurations/ignore_mod_tap_interrupt/test.mk @@ -0,0 +1,18 @@ +# Copyright 2021 Stefan Kerkmann +# +# 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/>. + +# -------------------------------------------------------------------------------- +# Keep this file, even if it is empty, as a marker that this folder contains tests +# -------------------------------------------------------------------------------- diff --git a/tests/tap_hold_configurations/ignore_mod_tap_interrupt/test_tap_hold.cpp b/tests/tap_hold_configurations/ignore_mod_tap_interrupt/test_tap_hold.cpp new file mode 100644 index 0000000000..319de61070 --- /dev/null +++ b/tests/tap_hold_configurations/ignore_mod_tap_interrupt/test_tap_hold.cpp @@ -0,0 +1,136 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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_fixture.hpp" +#include "test_keymap_key.hpp" + +using testing::_; +using testing::InSequence; + +class IgnoreModTapInterrupt : public TestFixture {}; + +TEST_F(IgnoreModTapInterrupt, tap_regular_key_while_mod_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + auto regular_key = KeymapKey(0, 2, 0, KC_A); + + set_keymap({mod_tap_hold_key, regular_key}); + + /* Press mod-tap-hold key */ + EXPECT_NO_REPORT(driver); + mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_NO_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_REPORT(driver, (KC_A, KC_P)); + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(IgnoreModTapInterrupt, tap_mod_tap_key_while_mod_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto first_mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + auto second_mod_tap_hold_key = KeymapKey(0, 2, 0, RSFT_T(KC_A)); + + set_keymap({first_mod_tap_hold_key, second_mod_tap_hold_key}); + + /* Press first mod-tap-hold key */ + EXPECT_NO_REPORT(driver); + first_mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press second tap-hold key */ + EXPECT_NO_REPORT(driver); + second_mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release second tap-hold key */ + EXPECT_NO_REPORT(driver); + second_mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release first mod-tap-hold key */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_REPORT(driver, (KC_A, KC_P)); + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + first_mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(IgnoreModTapInterrupt, tap_regular_key_while_layer_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto layer_tap_hold_key = KeymapKey(0, 1, 0, LT(1, KC_P)); + auto regular_key = KeymapKey(0, 2, 0, KC_A); + auto layer_key = KeymapKey(1, 2, 0, KC_B); + + set_keymap({layer_tap_hold_key, regular_key, layer_key}); + + /* Press layer-tap-hold key */ + EXPECT_NO_REPORT(driver); + layer_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_NO_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release layer-tap-hold key */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_REPORT(driver, (KC_P, regular_key.report_code)); + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + layer_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/tap_hold_configurations/permissive_hold/config.h b/tests/tap_hold_configurations/permissive_hold/config.h new file mode 100644 index 0000000000..2d5a9849e7 --- /dev/null +++ b/tests/tap_hold_configurations/permissive_hold/config.h @@ -0,0 +1,21 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 PERMISSIVE_HOLD
\ No newline at end of file diff --git a/tests/tap_hold_configurations/permissive_hold/test.mk b/tests/tap_hold_configurations/permissive_hold/test.mk new file mode 100644 index 0000000000..efecca2c22 --- /dev/null +++ b/tests/tap_hold_configurations/permissive_hold/test.mk @@ -0,0 +1,18 @@ +# Copyright 2021 Stefan Kerkmann +# +# 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/>. + +# -------------------------------------------------------------------------------- +# Keep this file, even if it is empty, as a marker that this folder contains tests +# -------------------------------------------------------------------------------- diff --git a/tests/tap_hold_configurations/permissive_hold/test_one_shot_keys.cpp b/tests/tap_hold_configurations/permissive_hold/test_one_shot_keys.cpp new file mode 100644 index 0000000000..1328b5fc0f --- /dev/null +++ b/tests/tap_hold_configurations/permissive_hold/test_one_shot_keys.cpp @@ -0,0 +1,76 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 "action_util.h" +#include "keyboard_report_util.hpp" +#include "test_common.hpp" + +using testing::_; +using testing::InSequence; + +class OneShot : public TestFixture {}; +class OneShotParametrizedTestFixture : public ::testing::WithParamInterface<std::pair<KeymapKey, KeymapKey>>, public OneShot {}; + +TEST_P(OneShotParametrizedTestFixture, OSMAsRegularModifierWithAdditionalKeypress) { + TestDriver driver; + KeymapKey osm_key = GetParam().first; + KeymapKey regular_key = GetParam().second; + + set_keymap({osm_key, regular_key}); + + /* Press OSM */ + EXPECT_NO_REPORT(driver); + osm_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_REPORT(driver, (osm_key.report_code)).Times(2); + EXPECT_REPORT(driver, (regular_key.report_code, osm_key.report_code)).Times(1); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release OSM */ + EXPECT_EMPTY_REPORT(driver).Times(1); + osm_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +// clang-format off + +INSTANTIATE_TEST_CASE_P( + OneShotModifierTests, + OneShotParametrizedTestFixture, + ::testing::Values( + /* first is osm key, second is regular key. */ + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_LSFT), KC_LSFT}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_LCTL), KC_LCTL}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_LALT), KC_LALT}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_LGUI), KC_LGUI}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_RCTL), KC_RCTL}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_RSFT), KC_RSFT}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_RALT), KC_RALT}, KeymapKey{0, 1, 1, KC_A}), + std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_RGUI), KC_RGUI}, KeymapKey{0, 1, 1, KC_A}) + )); +// clang-format on diff --git a/tests/tap_hold_configurations/permissive_hold/test_tap_hold.cpp b/tests/tap_hold_configurations/permissive_hold/test_tap_hold.cpp new file mode 100644 index 0000000000..ef8d9a9c7f --- /dev/null +++ b/tests/tap_hold_configurations/permissive_hold/test_tap_hold.cpp @@ -0,0 +1,131 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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_fixture.hpp" +#include "test_keymap_key.hpp" + +using testing::_; +using testing::InSequence; +class PermissiveHold : public TestFixture {}; + +TEST_F(PermissiveHold, tap_regular_key_while_mod_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + auto regular_key = KeymapKey(0, 2, 0, KC_A); + + set_keymap({mod_tap_hold_key, regular_key}); + + /* Press mod-tap-hold key */ + EXPECT_NO_REPORT(driver); + mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_REPORT(driver, (KC_LSHIFT)); + EXPECT_REPORT(driver, (KC_LSHIFT, regular_key.report_code)); + EXPECT_REPORT(driver, (KC_LSHIFT)); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key */ + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(PermissiveHold, tap_mod_tap_key_while_mod_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto first_mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + auto second_mod_tap_hold_key = KeymapKey(0, 2, 0, RSFT_T(KC_A)); + + set_keymap({first_mod_tap_hold_key, second_mod_tap_hold_key}); + + /* Press first mod-tap-hold key */ + EXPECT_NO_REPORT(driver); + first_mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press second mod-tap-hold key */ + EXPECT_NO_REPORT(driver); + second_mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release second mod-tap-hold key */ + EXPECT_REPORT(driver, (KC_LSHIFT)); + EXPECT_REPORT(driver, (KC_LSHIFT, second_mod_tap_hold_key.report_code)); + EXPECT_REPORT(driver, (KC_LSHIFT)); + second_mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release first mod-tap-hold key */ + EXPECT_EMPTY_REPORT(driver); + first_mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(PermissiveHold, tap_regular_key_while_layer_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto layer_tap_hold_key = KeymapKey(0, 1, 0, LT(1, KC_P)); + auto regular_key = KeymapKey(0, 2, 0, KC_A); + auto layer_key = KeymapKey(1, 2, 0, KC_B); + + set_keymap({layer_tap_hold_key, regular_key, layer_key}); + + /* Press layer-tap-hold key */ + EXPECT_NO_REPORT(driver); + layer_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_REPORT(driver, (layer_key.report_code)); + EXPECT_EMPTY_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release layer-tap-hold key */ + EXPECT_NO_REPORT(driver); + layer_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/tap_hold_configurations/permissive_hold_ignore_mod_tap_interrupt/config.h b/tests/tap_hold_configurations/permissive_hold_ignore_mod_tap_interrupt/config.h new file mode 100644 index 0000000000..a6abd50bbe --- /dev/null +++ b/tests/tap_hold_configurations/permissive_hold_ignore_mod_tap_interrupt/config.h @@ -0,0 +1,22 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 IGNORE_MOD_TAP_INTERRUPT +#define PERMISSIVE_HOLD
\ No newline at end of file diff --git a/tests/tap_hold_configurations/permissive_hold_ignore_mod_tap_interrupt/test.mk b/tests/tap_hold_configurations/permissive_hold_ignore_mod_tap_interrupt/test.mk new file mode 100644 index 0000000000..efecca2c22 --- /dev/null +++ b/tests/tap_hold_configurations/permissive_hold_ignore_mod_tap_interrupt/test.mk @@ -0,0 +1,18 @@ +# Copyright 2021 Stefan Kerkmann +# +# 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/>. + +# -------------------------------------------------------------------------------- +# Keep this file, even if it is empty, as a marker that this folder contains tests +# -------------------------------------------------------------------------------- diff --git a/tests/tap_hold_configurations/permissive_hold_ignore_mod_tap_interrupt/test_tap_hold.cpp b/tests/tap_hold_configurations/permissive_hold_ignore_mod_tap_interrupt/test_tap_hold.cpp new file mode 100644 index 0000000000..ee7e707c94 --- /dev/null +++ b/tests/tap_hold_configurations/permissive_hold_ignore_mod_tap_interrupt/test_tap_hold.cpp @@ -0,0 +1,133 @@ + +/* Copyright 2021 Stefan Kerkmann + * + * 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_fixture.hpp" +#include "test_keymap_key.hpp" + +using testing::_; +using testing::InSequence; + +class PermissiveHold_IgnoreModTapInterrupt : public TestFixture {}; + +TEST_F(PermissiveHold_IgnoreModTapInterrupt, tap_regular_key_while_mod_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + auto regular_key = KeymapKey(0, 2, 0, KC_A); + + set_keymap({mod_tap_hold_key, regular_key}); + + /* Press mod-tap-hold key */ + EXPECT_NO_REPORT(driver); + mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_REPORT(driver, (KC_LSFT)); + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_LSFT)); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key */ + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(PermissiveHold_IgnoreModTapInterrupt, tap_mod_tap_key_while_mod_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto first_mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + auto second_mod_tap_hold_key = KeymapKey(0, 2, 0, RSFT_T(KC_A)); + + set_keymap({first_mod_tap_hold_key, second_mod_tap_hold_key}); + + /* Press first mod-tap-hold key */ + EXPECT_NO_REPORT(driver); + first_mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press second tap-hold key */ + EXPECT_NO_REPORT(driver); + second_mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release second tap-hold key */ + EXPECT_REPORT(driver, (KC_LSFT)); + EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + EXPECT_REPORT(driver, (KC_LSFT)); + second_mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release first mod-tap-hold key */ + EXPECT_EMPTY_REPORT(driver); + first_mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(PermissiveHold_IgnoreModTapInterrupt, tap_regular_key_while_layer_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto layer_tap_hold_key = KeymapKey(0, 1, 0, LT(1, KC_P)); + auto regular_key = KeymapKey(0, 2, 0, KC_A); + auto layer_key = KeymapKey(1, 2, 0, KC_B); + + set_keymap({layer_tap_hold_key, regular_key, layer_key}); + + /* Press layer-tap-hold key */ + EXPECT_NO_REPORT(driver); + layer_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_REPORT(driver, (KC_B)); + EXPECT_EMPTY_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release layer-tap-hold key */ + EXPECT_NO_REPORT(driver); + layer_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/tap_hold_configurations/retro_tapping/config.h b/tests/tap_hold_configurations/retro_tapping/config.h new file mode 100644 index 0000000000..4b38f2644b --- /dev/null +++ b/tests/tap_hold_configurations/retro_tapping/config.h @@ -0,0 +1,21 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 RETRO_TAPPING
\ No newline at end of file diff --git a/tests/tap_hold_configurations/retro_tapping/test.mk b/tests/tap_hold_configurations/retro_tapping/test.mk new file mode 100644 index 0000000000..efecca2c22 --- /dev/null +++ b/tests/tap_hold_configurations/retro_tapping/test.mk @@ -0,0 +1,18 @@ +# Copyright 2021 Stefan Kerkmann +# +# 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/>. + +# -------------------------------------------------------------------------------- +# Keep this file, even if it is empty, as a marker that this folder contains tests +# -------------------------------------------------------------------------------- diff --git a/tests/tap_hold_configurations/retro_tapping/test_tap_hold.cpp b/tests/tap_hold_configurations/retro_tapping/test_tap_hold.cpp new file mode 100644 index 0000000000..dc0de0e44d --- /dev/null +++ b/tests/tap_hold_configurations/retro_tapping/test_tap_hold.cpp @@ -0,0 +1,52 @@ + +/* Copyright 2021 Stefan Kerkmann + * + * 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_fixture.hpp" +#include "test_keymap_key.hpp" + +using testing::_; +using testing::InSequence; + +class RetroTapping : public TestFixture {}; + +TEST_F(RetroTapping, tap_and_hold_mod_tap_hold_key) { + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + + set_keymap({mod_tap_hold_key}); + + /* Press mod-tap-hold key. */ + EXPECT_NO_REPORT(driver); + mod_tap_hold_key.press(); + idle_for(TAPPING_TERM); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key. */ + /* TODO: Why is LSHIFT send at all? */ + EXPECT_REPORT(driver, (KC_LSHIFT)); + EXPECT_EMPTY_REPORT(driver); + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/tap_hold_configurations/retro_tapping/test_tapping.cpp b/tests/tap_hold_configurations/retro_tapping/test_tapping.cpp new file mode 100644 index 0000000000..42139d50da --- /dev/null +++ b/tests/tap_hold_configurations/retro_tapping/test_tapping.cpp @@ -0,0 +1,112 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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" + +using testing::_; +using testing::InSequence; + +class Tapping : public TestFixture {}; + +TEST_F(Tapping, HoldA_SHFT_T_KeyReportsShift) { + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 7, 0, SFT_T(KC_P)); + + set_keymap({mod_tap_hold_key}); + + EXPECT_NO_REPORT(driver); + mod_tap_hold_key.press(); + idle_for(TAPPING_TERM); + testing::Mock::VerifyAndClearExpectations(&driver); + + EXPECT_REPORT(driver, (KC_LSFT)); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + EXPECT_EMPTY_REPORT(driver); + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(Tapping, ANewTapWithinTappingTermIsBuggy) { + TestDriver driver; + InSequence s; + auto key_shift_hold_p_tap = KeymapKey(0, 7, 0, SFT_T(KC_P)); + + set_keymap({key_shift_hold_p_tap}); + + /* Press mod_tap_hold key */ + EXPECT_NO_REPORT(driver); + key_shift_hold_p_tap.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod_tap_hold key */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + key_shift_hold_p_tap.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press mod_tap_hold key again */ + EXPECT_REPORT(driver, (KC_P)); + key_shift_hold_p_tap.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod_tap_hold key again */ + EXPECT_EMPTY_REPORT(driver); + key_shift_hold_p_tap.release(); + idle_for(TAPPING_TERM + 1); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press mod_tap_hold key again */ + EXPECT_NO_REPORT(driver); + key_shift_hold_p_tap.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod_tap_hold key again */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + key_shift_hold_p_tap.release(); + idle_for(TAPPING_TERM + 1); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press mod_tap_hold key again */ + EXPECT_NO_REPORT(driver); + key_shift_hold_p_tap.press(); + idle_for(TAPPING_TERM); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod_tap_hold key again */ + /* TODO: Why is KC_LSFT send? */ + EXPECT_REPORT(driver, (KC_LSFT)); + EXPECT_EMPTY_REPORT(driver); + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + key_shift_hold_p_tap.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/tap_hold_configurations/tapping_force_hold/config.h b/tests/tap_hold_configurations/tapping_force_hold/config.h new file mode 100644 index 0000000000..3b4646338a --- /dev/null +++ b/tests/tap_hold_configurations/tapping_force_hold/config.h @@ -0,0 +1,21 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 TAPPING_FORCE_HOLD
\ No newline at end of file diff --git a/tests/tap_hold_configurations/tapping_force_hold/test.mk b/tests/tap_hold_configurations/tapping_force_hold/test.mk new file mode 100644 index 0000000000..efecca2c22 --- /dev/null +++ b/tests/tap_hold_configurations/tapping_force_hold/test.mk @@ -0,0 +1,18 @@ +# Copyright 2021 Stefan Kerkmann +# +# 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/>. + +# -------------------------------------------------------------------------------- +# Keep this file, even if it is empty, as a marker that this folder contains tests +# -------------------------------------------------------------------------------- diff --git a/tests/tap_hold_configurations/tapping_force_hold/test_action_layer.cpp b/tests/tap_hold_configurations/tapping_force_hold/test_action_layer.cpp new file mode 100644 index 0000000000..965c702d7a --- /dev/null +++ b/tests/tap_hold_configurations/tapping_force_hold/test_action_layer.cpp @@ -0,0 +1,80 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 "test_common.hpp" + +using testing::_; +using testing::InSequence; + +class ActionLayer : public TestFixture {}; + +TEST_F(ActionLayer, LayerTapToggleWithToggleWithKeypress) { + TestDriver driver; + KeymapKey layer_key = KeymapKey{0, 0, 0, TT(1)}; + + /* These keys must have the same position in the matrix, only the layer is different. */ + KeymapKey regular_key = KeymapKey{0, 1, 0, KC_A}; + set_keymap({layer_key, regular_key, KeymapKey{1, 1, 0, KC_B}}); + + /* Tap TT five times . */ + /* TODO: Tapping Force Hold breaks TT */ + EXPECT_NO_REPORT(driver); + + layer_key.press(); + run_one_scan_loop(); + layer_key.release(); + run_one_scan_loop(); + expect_layer_state(0); + + layer_key.press(); + run_one_scan_loop(); + layer_key.release(); + run_one_scan_loop(); + expect_layer_state(0); + + layer_key.press(); + run_one_scan_loop(); + layer_key.release(); + run_one_scan_loop(); + expect_layer_state(0); + + layer_key.press(); + run_one_scan_loop(); + layer_key.release(); + run_one_scan_loop(); + expect_layer_state(0); + + layer_key.press(); + run_one_scan_loop(); + layer_key.release(); + run_one_scan_loop(); + expect_layer_state(0); + + testing::Mock::VerifyAndClearExpectations(&driver); + + EXPECT_REPORT(driver, (KC_A)).Times(1); + regular_key.press(); + run_one_scan_loop(); + expect_layer_state(0); + testing::Mock::VerifyAndClearExpectations(&driver); + + EXPECT_EMPTY_REPORT(driver).Times(1); + regular_key.release(); + run_one_scan_loop(); + expect_layer_state(0); + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/tap_hold_configurations/tapping_force_hold/test_tap_hold.cpp b/tests/tap_hold_configurations/tapping_force_hold/test_tap_hold.cpp new file mode 100644 index 0000000000..2671862f2d --- /dev/null +++ b/tests/tap_hold_configurations/tapping_force_hold/test_tap_hold.cpp @@ -0,0 +1,213 @@ + +/* Copyright 2021 Stefan Kerkmann + * + * 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_fixture.hpp" +#include "test_keymap_key.hpp" + +using testing::_; +using testing::InSequence; + +class TappingForceHold : public TestFixture {}; + +TEST_F(TappingForceHold, tap_regular_key_while_mod_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + auto regular_key = KeymapKey(0, 2, 0, KC_A); + + set_keymap({mod_tap_hold_key, regular_key}); + + /* Press mod-tap-hold key. */ + EXPECT_NO_REPORT(driver); + mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key. */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key. */ + EXPECT_NO_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key. */ + EXPECT_REPORT(driver, (KC_LSFT)); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&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); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(TappingForceHold, tap_mod_tap_key_while_mod_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto first_mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + auto second_mod_tap_hold_key = KeymapKey(0, 2, 0, RSFT_T(KC_A)); + + set_keymap({first_mod_tap_hold_key, second_mod_tap_hold_key}); + + /* Press first mod-tap-hold key */ + EXPECT_NO_REPORT(driver); + first_mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press second tap-hold key */ + EXPECT_NO_REPORT(driver); + second_mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release second tap-hold key */ + EXPECT_NO_REPORT(driver); + second_mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release first mod-tap-hold key */ + EXPECT_REPORT(driver, (KC_LSFT)); + first_mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Idle for tapping term of first 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); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(TappingForceHold, tap_regular_key_while_layer_tap_key_is_held) { + TestDriver driver; + InSequence s; + auto layer_tap_hold_key = KeymapKey(0, 1, 0, LT(1, KC_P)); + auto regular_key = KeymapKey(0, 2, 0, KC_A); + auto layer_key = KeymapKey(1, 2, 0, KC_B); + + set_keymap({layer_tap_hold_key, regular_key, layer_key}); + + /* Press layer-tap-hold key */ + EXPECT_NO_REPORT(driver); + layer_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press regular key */ + EXPECT_NO_REPORT(driver); + regular_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release regular key */ + EXPECT_NO_REPORT(driver); + regular_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release layer-tap-hold key */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_REPORT(driver, (KC_A, KC_P)); + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + layer_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(TappingForceHold, tap_mod_tap_hold_key_two_times) { + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + + set_keymap({mod_tap_hold_key}); + + /* Press mod-tap-hold key. */ + EXPECT_NO_REPORT(driver); + mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key. */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press mod-tap-hold key again. */ + EXPECT_NO_REPORT(driver); + mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key. */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} + +TEST_F(TappingForceHold, tap_mod_tap_hold_key_twice_and_hold_on_second_time) { + TestDriver driver; + InSequence s; + auto mod_tap_hold_key = KeymapKey(0, 1, 0, SFT_T(KC_P)); + + set_keymap({mod_tap_hold_key}); + + /* Press mod-tap-hold key. */ + EXPECT_NO_REPORT(driver); + mod_tap_hold_key.press(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key. */ + EXPECT_REPORT(driver, (KC_P)); + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Press mod-tap-hold key again. */ + EXPECT_NO_REPORT(driver); + mod_tap_hold_key.press(); + idle_for(TAPPING_TERM); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Release mod-tap-hold key. */ + EXPECT_REPORT(driver, (KC_LSHIFT)); + EXPECT_EMPTY_REPORT(driver); + mod_tap_hold_key.release(); + run_one_scan_loop(); + testing::Mock::VerifyAndClearExpectations(&driver); +} diff --git a/tests/basic/rules.mk b/tests/test_common/build.mk index 9fb5d43610..aeb3057854 100644 --- a/tests/basic/rules.mk +++ b/tests/test_common/build.mk @@ -1,4 +1,4 @@ -# Copyright 2017 Fred Sundvik +# Copyright 2021 Stefan Kerkmann # # 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 @@ -13,4 +13,4 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -CUSTOM_MATRIX=yes +CUSTOM_MATRIX=yes
\ No newline at end of file diff --git a/tests/test_common/keyboard_report_util.cpp b/tests/test_common/keyboard_report_util.cpp index f73cf239e0..7908e64f7f 100644 --- a/tests/test_common/keyboard_report_util.cpp +++ b/tests/test_common/keyboard_report_util.cpp @@ -36,7 +36,7 @@ std::vector<uint8_t> get_keys(const report_keyboard_t& report) { std::sort(result.begin(), result.end()); return result; } -} // namespace +} // namespace bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) { auto lhskeys = get_keys(lhs); @@ -44,16 +44,21 @@ bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) { return lhs.mods == rhs.mods && lhskeys == rhskeys; } -std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& value) { - stream << "Keyboard report:" << std::endl; - stream << "Mods: " << (uint32_t)value.mods << std::endl; - stream << "Keys: "; +std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& report) { + auto keys = get_keys(report); + // TODO: This should probably print friendly names for the keys - for (uint32_t k : get_keys(value)) { - stream << k << " "; + stream << "Keyboard Report: Mods (" << (uint32_t)report.mods << ") Keys ("; + + for (auto key = keys.cbegin(); key != keys.cend();) { + stream << +(*key); + key++; + if (key != keys.cend()) { + stream << ","; + } } - stream << std::endl; - return stream; + + return stream << ")" << std::endl; } KeyboardReportMatcher::KeyboardReportMatcher(const std::vector<uint8_t>& keys) { @@ -67,8 +72,14 @@ KeyboardReportMatcher::KeyboardReportMatcher(const std::vector<uint8_t>& keys) { } } -bool KeyboardReportMatcher::MatchAndExplain(report_keyboard_t& report, MatchResultListener* listener) const { return m_report == report; } +bool KeyboardReportMatcher::MatchAndExplain(report_keyboard_t& report, MatchResultListener* listener) const { + return m_report == report; +} -void KeyboardReportMatcher::DescribeTo(::std::ostream* os) const { *os << "is equal to " << m_report; } +void KeyboardReportMatcher::DescribeTo(::std::ostream* os) const { + *os << "is equal to " << m_report; +} -void KeyboardReportMatcher::DescribeNegationTo(::std::ostream* os) const { *os << "is not equal to " << m_report; } +void KeyboardReportMatcher::DescribeNegationTo(::std::ostream* os) const { + *os << "is not equal to " << m_report; +} diff --git a/tests/test_common/keymap.c b/tests/test_common/keymap.c new file mode 100644 index 0000000000..fc3a56a007 --- /dev/null +++ b/tests/test_common/keymap.c @@ -0,0 +1,33 @@ +/* Copyright 2017 Fred Sundvik + * + * 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 "quantum.h" + +// clang-format off + +const uint16_t PROGMEM + keymaps[][MATRIX_ROWS][MATRIX_COLS] = + { + [0] = + { + {KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO}, + {KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO}, + {KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO}, + {KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO}, + }, +}; + +// clang-format on diff --git a/tests/test_common/matrix.c b/tests/test_common/matrix.c index 9a92a801c7..7b24d560e3 100644 --- a/tests/test_common/matrix.c +++ b/tests/test_common/matrix.c @@ -30,7 +30,9 @@ uint8_t matrix_scan(void) { return 1; } -matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } +matrix_row_t matrix_get_row(uint8_t row) { + return matrix[row]; +} void matrix_print(void) {} @@ -38,10 +40,16 @@ void matrix_init_kb(void) {} void matrix_scan_kb(void) {} -void press_key(uint8_t col, uint8_t row) { matrix[row] |= 1 << col; } +void press_key(uint8_t col, uint8_t row) { + matrix[row] |= 1 << col; +} -void release_key(uint8_t col, uint8_t row) { matrix[row] &= ~(1 << col); } +void release_key(uint8_t col, uint8_t row) { + matrix[row] &= ~(1 << col); +} -void clear_all_keys(void) { memset(matrix, 0, sizeof(matrix)); } +void clear_all_keys(void) { + memset(matrix, 0, sizeof(matrix)); +} void led_set(uint8_t usb_led) {} diff --git a/tests/test_common/test_common.h b/tests/test_common/test_common.h new file mode 100644 index 0000000000..19ffcddcea --- /dev/null +++ b/tests/test_common/test_common.h @@ -0,0 +1,4 @@ +#pragma once + +#define MATRIX_ROWS 4 +#define MATRIX_COLS 10
\ No newline at end of file diff --git a/tests/test_common/test_driver.cpp b/tests/test_common/test_driver.cpp index 84e249d838..86644ab6bd 100644 --- a/tests/test_common/test_driver.cpp +++ b/tests/test_common/test_driver.cpp @@ -18,19 +18,67 @@ TestDriver* TestDriver::m_this = nullptr; +namespace { +// Given a hex digit between 0 and 15, returns the corresponding keycode. +uint8_t hex_digit_to_keycode(uint8_t digit) { + // clang-format off + static const uint8_t hex_keycodes[] = { + KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, + KC_8, KC_9, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F + }; + // clang-format on + return hex_keycodes[digit]; +} +} // namespace + TestDriver::TestDriver() : m_driver{&TestDriver::keyboard_leds, &TestDriver::send_keyboard, &TestDriver::send_mouse, &TestDriver::send_system, &TestDriver::send_consumer} { host_set_driver(&m_driver); m_this = this; } -TestDriver::~TestDriver() { m_this = nullptr; } +TestDriver::~TestDriver() { + m_this = nullptr; +} -uint8_t TestDriver::keyboard_leds(void) { return m_this->m_leds; } +uint8_t TestDriver::keyboard_leds(void) { + return m_this->m_leds; +} -void TestDriver::send_keyboard(report_keyboard_t* report) { m_this->send_keyboard_mock(*report); } +void TestDriver::send_keyboard(report_keyboard_t* report) { + test_logger.trace() << *report; + m_this->send_keyboard_mock(*report); +} -void TestDriver::send_mouse(report_mouse_t* report) { m_this->send_mouse_mock(*report); } +void TestDriver::send_mouse(report_mouse_t* report) { + m_this->send_mouse_mock(*report); +} -void TestDriver::send_system(uint16_t data) { m_this->send_system_mock(data); } +void TestDriver::send_system(uint16_t data) { + m_this->send_system_mock(data); +} -void TestDriver::send_consumer(uint16_t data) { m_this->send_consumer(data); } +void TestDriver::send_consumer(uint16_t data) { + m_this->send_consumer(data); +} + +namespace internal { +void expect_unicode_code_point(TestDriver& driver, uint32_t code_point) { + testing::InSequence seq; + EXPECT_REPORT(driver, (KC_LCTL, KC_LSFT, KC_U)); + + bool print_zero = false; + for (int i = 7; i >= 0; --i) { + if (i <= 3) { + print_zero = true; + } + + const uint8_t digit = (code_point >> (i * 4)) & 0xf; + if (digit || print_zero) { + EXPECT_REPORT(driver, (hex_digit_to_keycode(digit))); + print_zero = true; + } + } + + EXPECT_REPORT(driver, (KC_SPC)); +} +} // namespace internal diff --git a/tests/test_common/test_driver.hpp b/tests/test_common/test_driver.hpp index f86308df95..b58cfd1ebc 100644 --- a/tests/test_common/test_driver.hpp +++ b/tests/test_common/test_driver.hpp @@ -20,25 +20,86 @@ #include <stdint.h> #include "host.h" #include "keyboard_report_util.hpp" - +#include "test_logger.hpp" class TestDriver { -public: + public: TestDriver(); ~TestDriver(); - void set_leds(uint8_t leds) { m_leds = leds; } - - MOCK_METHOD1(send_keyboard_mock, void (report_keyboard_t&)); - MOCK_METHOD1(send_mouse_mock, void (report_mouse_t&)); - MOCK_METHOD1(send_system_mock, void (uint16_t)); - MOCK_METHOD1(send_consumer_mock, void (uint16_t)); -private: - static uint8_t keyboard_leds(void); - static void send_keyboard(report_keyboard_t *report); - static void send_mouse(report_mouse_t* report); - static void send_system(uint16_t data); - static void send_consumer(uint16_t data); - host_driver_t m_driver; - uint8_t m_leds = 0; + void set_leds(uint8_t leds) { + m_leds = leds; + } + + MOCK_METHOD1(send_keyboard_mock, void(report_keyboard_t&)); + MOCK_METHOD1(send_mouse_mock, void(report_mouse_t&)); + MOCK_METHOD1(send_system_mock, void(uint16_t)); + MOCK_METHOD1(send_consumer_mock, void(uint16_t)); + + private: + static uint8_t keyboard_leds(void); + static void send_keyboard(report_keyboard_t* report); + static void send_mouse(report_mouse_t* report); + static void send_system(uint16_t data); + static void send_consumer(uint16_t data); + host_driver_t m_driver; + uint8_t m_leds = 0; static TestDriver* m_this; }; + +/** + * @brief Sets gmock expectation that a keyboard report of `report` keys will be sent. + * For this macro to parse correctly, the `report` arg must be surrounded by + * parentheses ( ). For instance, + * + * // Expect that a report of "KC_LSFT + KC_A" is sent to the host. + * EXPECT_REPORT(driver, (KC_LSFT, KC_A)); + * + * is shorthand for + * + * EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_A))); + * + * It is possible to use .Times() and other gmock APIS with EXPECT_REPORT, for instance, + * allow only single report to be sent: + * + * EXPECT_REPORT(driver, (KC_LSFT, KC_A)).Times(1); + */ +#define EXPECT_REPORT(driver, report) EXPECT_CALL((driver), send_keyboard_mock(KeyboardReport report)) + +/** + * @brief Sets gmock expectation that Unicode `code_point` is sent with UC_LNX input + * mode. For instance for U+2013, + * + * EXPECT_UNICODE(driver, 0x2013); + * + * expects the sequence of keys: + * + * "Ctrl+Shift+U, 2, 0, 1, 3, space". + */ +#define EXPECT_UNICODE(driver, code_point) internal::expect_unicode_code_point((driver), (code_point)) + +/** + * @brief Sets gmock expectation that a empty keyboard report will be sent. + * It is possible to use .Times() and other gmock APIS with EXPECT_EMPTY_REPORT, for instance, + * allow any number of empty reports with: + * + * EXPECT_EMPTY_REPORT(driver).Times(AnyNumber()); + */ +#define EXPECT_EMPTY_REPORT(driver) EXPECT_REPORT(driver, ()) + +/** + * @brief Sets gmock expectation that a keyboard report will be sent, without matching its content. + * It is possible to use .Times() and other gmock APIS with EXPECT_ANY_REPORT, for instance, + * allow a single arbitrary report with: + * + * EXPECT_ANY_REPORT(driver).Times(1); + */ +#define EXPECT_ANY_REPORT(driver) EXPECT_CALL((driver), send_keyboard_mock(_)) + +/** + * @brief Sets gmock expectation that no keyboard report will be sent at all. + */ +#define EXPECT_NO_REPORT(driver) EXPECT_ANY_REPORT(driver).Times(0) + +namespace internal { +void expect_unicode_code_point(TestDriver& driver, uint32_t code_point); +} // namespace internal diff --git a/tests/test_common/test_fixture.cpp b/tests/test_common/test_fixture.cpp index e041df7128..5fc6964054 100644 --- a/tests/test_common/test_fixture.cpp +++ b/tests/test_common/test_fixture.cpp @@ -1,50 +1,154 @@ #include "test_fixture.hpp" +#include <algorithm> +#include <cstdint> +#include <cstdio> +#include <cstdlib> +#include "gmock/gmock-cardinalities.h" #include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "keyboard_report_util.hpp" +#include "keycode.h" #include "test_driver.hpp" +#include "test_logger.hpp" #include "test_matrix.h" -#include "keyboard.h" -#include "action.h" -#include "action_tapping.h" +#include "test_keymap_key.hpp" extern "C" { +#include "action.h" +#include "action_tapping.h" +#include "action_util.h" +#include "action_layer.h" #include "debug.h" #include "eeconfig.h" -#include "action_layer.h" +#include "keyboard.h" +#include "keymap.h" void set_time(uint32_t t); void advance_time(uint32_t ms); } using testing::_; -using testing::AnyNumber; -using testing::Between; -using testing::Return; + +/* This is used for dynamic dispatching keymap_key_to_keycode calls to the current active test_fixture. */ +TestFixture* TestFixture::m_this = nullptr; + +/* Override weak QMK function to allow the usage of isolated per-test keymaps in unit-tests. + * The actual call is dynamicaly dispatched to the current active test fixture, which in turn has it's own keymap. */ +extern "C" uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t position) { + uint16_t keycode; + TestFixture::m_this->get_keycode(layer, position, &keycode); + return keycode; +} void TestFixture::SetUpTestCase() { + test_logger.info() << "TestFixture setup-up start." << std::endl; + // The following is enough to bootstrap the values set in main eeconfig_init_quantum(); eeconfig_update_debug(debug_config.raw); TestDriver driver; - EXPECT_CALL(driver, send_keyboard_mock(_)); keyboard_init(); + + test_logger.info() << "TestFixture setup-up end." << std::endl; } void TestFixture::TearDownTestCase() {} -TestFixture::TestFixture() {} +TestFixture::TestFixture() { + m_this = this; +} TestFixture::~TestFixture() { + test_logger.info() << "TestFixture clean-up start." << std::endl; TestDriver driver; - // Run for a while to make sure all keys are completely released - EXPECT_CALL(driver, send_keyboard_mock(_)).Times(AnyNumber()); - layer_clear(); + + /* Reset keyboard state. */ clear_all_keys(); - idle_for(TAPPING_TERM + 10); + + clear_keyboard(); + + clear_oneshot_mods(); + clear_oneshot_locked_mods(); + reset_oneshot_layer(); + + layer_clear(); + +#if defined(SWAP_HANDS_ENABLE) + clear_oneshot_swaphands(); +#endif + + idle_for(TAPPING_TERM * 10); + testing::Mock::VerifyAndClearExpectations(&driver); + + /* Verify that the matrix really is cleared */ + EXPECT_NO_REPORT(driver); + idle_for(TAPPING_TERM * 10); testing::Mock::VerifyAndClearExpectations(&driver); - // Verify that the matrix really is cleared - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(0); - idle_for(TAPPING_TERM + 10); + + m_this = nullptr; + + test_logger.info() << "TestFixture clean-up end." << std::endl; + + print_test_log(); +} + +void TestFixture::add_key(KeymapKey key) { + if (this->find_key(key.layer, key.position)) { + FAIL() << "Key is already mapped for layer " << +key.layer << " and (column,row) (" << +key.position.col << "," << +key.position.row << ")"; + } + + this->keymap.push_back(key); +} + +void TestFixture::tap_key(KeymapKey key, unsigned delay_ms) { + key.press(); + idle_for(delay_ms); + key.release(); + run_one_scan_loop(); +} + +void TestFixture::set_keymap(std::initializer_list<KeymapKey> keys) { + this->keymap.clear(); + for (auto& key : keys) { + add_key(key); + } +} + +const KeymapKey* TestFixture::find_key(layer_t layer, keypos_t position) const { + auto keymap_key_predicate = [&](KeymapKey candidate) { return candidate.layer == layer && candidate.position.col == position.col && candidate.position.row == position.row; }; + + auto result = std::find_if(this->keymap.begin(), this->keymap.end(), keymap_key_predicate); + + if (result != std::end(this->keymap)) { + return &(*result); + } + return nullptr; +} + +void TestFixture::get_keycode(const layer_t layer, const keypos_t position, uint16_t* result) const { + bool key_is_out_of_bounds = position.col >= MATRIX_COLS && position.row >= MATRIX_ROWS; + + if (key_is_out_of_bounds) { + /* See if this is done in hardware as well, because this is 100% out of bounds reads on all QMK keebs out there. */ + auto msg = [&]() { + std::stringstream msg; + msg << "Keycode for position (" << +position.col << "," << +position.row << ") requested! This is out of bounds." << std::endl; + return msg.str(); + }(); + + *result = KC_NO; + test_logger.error() << msg; + EXPECT_FALSE(key_is_out_of_bounds) << msg; + return; + } + + if (auto key = this->find_key(layer, position)) { + *result = key->code; + return; + } + + FAIL() << "No key is mapped for layer " << +layer << " and (column,row) " << +position.col << "," << +position.row << ")"; } void TestFixture::run_one_scan_loop() { @@ -57,3 +161,17 @@ void TestFixture::idle_for(unsigned time) { run_one_scan_loop(); } } + +void TestFixture::print_test_log() const { + const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info(); + if (HasFailure()) { + std::cerr << test_info->test_case_name() << "." << test_info->name() << " failed!" << std::endl; + test_logger.print_log(); + } + test_logger.reset(); +} + +void TestFixture::expect_layer_state(layer_t layer_state) const { + test_logger.trace() << "Layer state: (" << +layer_state << ") Highest layer bit: (" << +get_highest_layer(layer_state) << ")" << std::endl; + EXPECT_TRUE(layer_state_is(layer_state)); +} diff --git a/tests/test_common/test_fixture.hpp b/tests/test_common/test_fixture.hpp index 340503665b..81906f76c7 100644 --- a/tests/test_common/test_fixture.hpp +++ b/tests/test_common/test_fixture.hpp @@ -16,15 +16,49 @@ #pragma once +#include <cstdint> +#include <unordered_map> +#include <optional> #include "gtest/gtest.h" +#include "keyboard.h" +#include "test_keymap_key.hpp" class TestFixture : public testing::Test { -public: + public: + static TestFixture* m_this; + TestFixture(); ~TestFixture(); static void SetUpTestCase(); static void TearDownTestCase(); + void set_keymap(std::initializer_list<KeymapKey> keycodes); + void add_key(const KeymapKey key); + + const KeymapKey* find_key(const layer_t layer_t, const keypos_t position) const; + void get_keycode(const layer_t layer, const keypos_t position, uint16_t* result) const; + + /** + * @brief Taps `key` with `delay_ms` delay between press and release. + */ + void tap_key(KeymapKey key, unsigned delay_ms = 1); + + /** + * @brief Taps multiple KeymapKey keys in order, e.g. `tap_keys(key_a, key_b)`. + */ + template <typename... Ts> + void tap_keys(Ts... keys) { + for (KeymapKey key : {keys...}) { + tap_key(key); + } + } + void run_one_scan_loop(); void idle_for(unsigned ms); + + void expect_layer_state(layer_t layer) const; + + protected: + void print_test_log() const; + std::vector<KeymapKey> keymap; }; diff --git a/tests/test_common/test_keymap_key.cpp b/tests/test_common/test_keymap_key.cpp new file mode 100644 index 0000000000..878ae097bf --- /dev/null +++ b/tests/test_common/test_keymap_key.cpp @@ -0,0 +1,30 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 "test_keymap_key.hpp" +#include "test_logger.hpp" +#include "gtest/gtest-message.h" +#include "gtest/gtest.h" + +void KeymapKey::press() { + test_logger.trace() << "Key pressed: (" << +this->position.col << "," << +this->position.row << ")" << std::endl; + press_key(this->position.col, this->position.row); +} + +void KeymapKey::release() { + test_logger.trace() << "Key released: (" << +this->position.col << "," << +this->position.row << ")" << std::endl; + release_key(this->position.col, this->position.row); +}
\ No newline at end of file diff --git a/tests/test_common/test_keymap_key.hpp b/tests/test_common/test_keymap_key.hpp new file mode 100644 index 0000000000..7861cb4a32 --- /dev/null +++ b/tests/test_common/test_keymap_key.hpp @@ -0,0 +1,46 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 + +extern "C" { +#include "keyboard.h" +#include "test_matrix.h" +} + +#include <cassert> + +typedef uint8_t layer_t; + +struct KeymapKey { + KeymapKey(layer_t layer, uint8_t col, uint8_t row, uint16_t keycode) : layer(layer), position({.col = col, .row = row}), code(keycode), report_code(keycode) { validate(); } + KeymapKey(layer_t layer, uint8_t col, uint8_t row, uint16_t keycode, uint16_t report_code) : layer(layer), position({.col = col, .row = row}), code(keycode), report_code(report_code) { validate(); } + + void press(); + void release(); + + const layer_t layer; + const keypos_t position; + const uint16_t code; + /* Sometimes the keycode does not match the code that is send in the usb report, so we provide it here. */ + const uint16_t report_code; + + private: + void validate() { + assert(position.col <= MATRIX_COLS); + assert(position.row <= MATRIX_ROWS); + } +};
\ No newline at end of file diff --git a/tests/test_common/test_logger.cpp b/tests/test_common/test_logger.cpp new file mode 100644 index 0000000000..efc7719d13 --- /dev/null +++ b/tests/test_common/test_logger.cpp @@ -0,0 +1,43 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 <iostream> +#include "test_logger.hpp" + +TestLogger test_logger; + +TestLogger& TestLogger::info() { + *this << "[ INFO ] "; + return *this; +} + +TestLogger& TestLogger::trace() { + *this << "[ TRACE ] "; + return *this; +} + +TestLogger& TestLogger::error() { + *this << "[ ERROR ] "; + return *this; +} + +void TestLogger::reset() { + this->m_log.str(""); +}; + +void TestLogger::print_log() { + std::cerr << this->m_log.str(); +} diff --git a/tests/test_common/test_logger.hpp b/tests/test_common/test_logger.hpp new file mode 100644 index 0000000000..348af7fab8 --- /dev/null +++ b/tests/test_common/test_logger.hpp @@ -0,0 +1,35 @@ +/* Copyright 2021 Stefan Kerkmann + * + * 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 <ostream> +#include <sstream> + +class TestLogger : public std::ostream { + public: + TestLogger() : std::ostream(&m_log){}; + TestLogger& info(); + TestLogger& trace(); + TestLogger& error(); + void print_log(); + void reset(); + + private: + std::stringbuf m_log; +}; + +extern TestLogger test_logger;
\ No newline at end of file |