Browse Source

[Tests] Increase QMK test coverage take 2 (#15269)

* Add per-test keymaps

* Add better trace and info logs for failed unit-tests

* Add layer state assertion with tracing message

* Use individual test binaries configuration options

* Add basic qmk functionality tests

* Add tap hold configurations tests

* Add auto shift tests

Co-authored-by: Nick Brassel <nick@tzarc.org>
master
Stefan Kerkmann 7 months ago
committed by GitHub
parent
commit
a24bdccee0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      Makefile
  2. 23
      build_full_test.mk
  3. 19
      build_test.mk
  4. 4
      testlist.mk
  5. 19
      tests/auto_shift/config.h
  6. 20
      tests/auto_shift/test.mk
  7. 73
      tests/auto_shift/test_auto_shift.cpp
  8. 3
      tests/basic/config.h
  9. 45
      tests/basic/keymap.c
  10. 18
      tests/basic/test.mk
  11. 427
      tests/basic/test_action_layer.cpp
  12. 213
      tests/basic/test_keypress.cpp
  13. 19
      tests/basic/test_macro.cpp
  14. 197
      tests/basic/test_one_shot_keys.cpp
  15. 50
      tests/basic/test_tapping.cpp
  16. 21
      tests/tap_hold_configurations/default_mod_tap/config.h
  17. 18
      tests/tap_hold_configurations/default_mod_tap/test.mk
  18. 230
      tests/tap_hold_configurations/default_mod_tap/test_tap_hold.cpp
  19. 21
      tests/tap_hold_configurations/ignore_mod_tap_interrupt/config.h
  20. 18
      tests/tap_hold_configurations/ignore_mod_tap_interrupt/test.mk
  21. 136
      tests/tap_hold_configurations/ignore_mod_tap_interrupt/test_tap_hold.cpp
  22. 21
      tests/tap_hold_configurations/permissive_hold/config.h
  23. 18
      tests/tap_hold_configurations/permissive_hold/test.mk
  24. 76
      tests/tap_hold_configurations/permissive_hold/test_one_shot_keys.cpp
  25. 132
      tests/tap_hold_configurations/permissive_hold/test_tap_hold.cpp
  26. 22
      tests/tap_hold_configurations/permissive_hold_ignore_mod_tap_interrupt/config.h
  27. 18
      tests/tap_hold_configurations/permissive_hold_ignore_mod_tap_interrupt/test.mk
  28. 134
      tests/tap_hold_configurations/permissive_hold_ignore_mod_tap_interrupt/test_tap_hold.cpp
  29. 21
      tests/tap_hold_configurations/retro_tapping/config.h
  30. 18
      tests/tap_hold_configurations/retro_tapping/test.mk
  31. 52
      tests/tap_hold_configurations/retro_tapping/test_tap_hold.cpp
  32. 112
      tests/tap_hold_configurations/retro_tapping/test_tapping.cpp
  33. 21
      tests/tap_hold_configurations/tapping_force_hold/config.h
  34. 18
      tests/tap_hold_configurations/tapping_force_hold/test.mk
  35. 81
      tests/tap_hold_configurations/tapping_force_hold/test_action_layer.cpp
  36. 215
      tests/tap_hold_configurations/tapping_force_hold/test_tap_hold.cpp
  37. 4
      tests/test_common/build.mk
  38. 21
      tests/test_common/keyboard_report_util.cpp
  39. 33
      tests/test_common/keymap.c
  40. 4
      tests/test_common/test_common.h
  41. 5
      tests/test_common/test_driver.cpp
  42. 29
      tests/test_common/test_driver.hpp
  43. 142
      tests/test_common/test_fixture.cpp
  44. 21
      tests/test_common/test_fixture.hpp
  45. 30
      tests/test_common/test_keymap_key.cpp
  46. 46
      tests/test_common/test_keymap_key.hpp
  47. 39
      tests/test_common/test_logger.cpp
  48. 35
      tests/test_common/test_logger.hpp

7
Makefile

@ -378,11 +378,12 @@ define PARSE_ALL_KEYMAPS
endef
define BUILD_TEST
TEST_NAME := $1
TEST_PATH := $1
TEST_NAME := $$(notdir $$(TEST_PATH))
MAKE_TARGET := $2
COMMAND := $1
MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_test.mk $$(MAKE_TARGET)
MAKE_VARS := TEST=$$(TEST_NAME) FULL_TESTS="$$(FULL_TESTS)"
MAKE_VARS := TEST=$$(TEST_NAME) TEST_PATH=$$(TEST_PATH) FULL_TESTS="$$(FULL_TESTS)"
MAKE_MSG := $$(MSG_MAKE_TEST)
$$(eval $$(call BUILD))
ifneq ($$(MAKE_TARGET),clean)
@ -406,7 +407,7 @@ define PARSE_TEST
ifeq ($$(TEST_NAME),all)
MATCHED_TESTS := $$(TEST_LIST)
else
MATCHED_TESTS := $$(foreach TEST,$$(TEST_LIST),$$(if $$(findstring $$(TEST_NAME),$$(TEST)),$$(TEST),))
MATCHED_TESTS := $$(foreach TEST, $$(TEST_LIST),$$(if $$(findstring $$(TEST_NAME), $$(notdir $$(TEST))), $$(TEST),))
endif
$$(foreach TEST,$$(MATCHED_TESTS),$$(eval $$(call BUILD_TEST,$$(TEST),$$(TEST_TARGET))))
endef

23
build_full_test.mk

@ -13,21 +13,24 @@
# 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 $(TMK_PATH)/protocol.mk
$(TEST)_INC := \
tests\test_common\common_config.h
TEST_PATH=tests/$(TEST)
$(TEST)_SRC= \
$(TEST_PATH)/keymap.c \
$(TEST)_SRC := \
$(TMK_COMMON_SRC) \
$(QUANTUM_SRC) \
$(SRC) \
tests/test_common/keymap.c \
tests/test_common/matrix.c \
tests/test_common/test_driver.cpp \
tests/test_common/keyboard_report_util.cpp \
tests/test_common/test_fixture.cpp
$(TEST)_SRC += $(patsubst $(ROOTDIR)/%,%,$(wildcard $(TEST_PATH)/*.cpp))
tests/test_common/test_fixture.cpp \
tests/test_common/test_keymap_key.cpp \
tests/test_common/test_logger.cpp \
$(patsubst $(ROOTDIR)/%,%,$(wildcard $(TEST_PATH)/*.cpp))
$(TEST)_DEFS := $(TMK_COMMON_DEFS) $(OPT_DEFS)
$(TEST)_CONFIG := $(TEST_PATH)/config.h
$(TEST)_DEFS=$(TMK_COMMON_DEFS) $(OPT_DEFS)
$(TEST)_CONFIG=$(TEST_PATH)/config.h
VPATH+=$(TOP_DIR)/tests/test_common
VPATH += $(TOP_DIR)/tests/test_common

19
build_test.mk

@ -16,14 +16,14 @@ TEST_OBJ = $(BUILD_DIR)/test_obj
OUTPUTS := $(TEST_OBJ)/$(TEST) $(GTEST_OUTPUT)
GTEST_INC := \
$(LIB_PATH)/googletest/googletest/include\
$(LIB_PATH)/googletest/googlemock/include\
$(LIB_PATH)/googletest/googletest/include \
$(LIB_PATH)/googletest/googlemock/include
GTEST_INTERNAL_INC :=\
$(LIB_PATH)/googletest/googletest\
GTEST_INTERNAL_INC := \
$(LIB_PATH)/googletest/googletest \
$(LIB_PATH)/googletest/googlemock
$(GTEST_OUTPUT)_SRC :=\
$(GTEST_OUTPUT)_SRC := \
googletest/src/gtest-all.cc\
googlemock/src/gmock-all.cc
@ -33,9 +33,9 @@ $(GTEST_OUTPUT)_INC := $(GTEST_INC) $(GTEST_INTERNAL_INC)
LDFLAGS += -lstdc++ -lpthread -shared-libgcc
CREATE_MAP := no
VPATH +=\
$(LIB_PATH)/googletest\
$(LIB_PATH)/googlemock\
VPATH += \
$(LIB_PATH)/googletest \
$(LIB_PATH)/googlemock \
$(LIB_PATH)/printf
all: elf
@ -49,7 +49,8 @@ CONSOLE_ENABLE = yes
endif
ifneq ($(filter $(FULL_TESTS),$(TEST)),)
include tests/$(TEST)/rules.mk
include tests/test_common/build.mk
include $(TEST_PATH)/test.mk
endif
include common_features.mk

4
testlist.mk

@ -1,5 +1,5 @@
TEST_LIST = $(notdir $(patsubst %/rules.mk,%,$(wildcard $(ROOT_DIR)/tests/*/rules.mk)))
FULL_TESTS := $(TEST_LIST)
TEST_LIST = $(sort $(patsubst %/test.mk,%, $(shell find $(ROOT_DIR)tests -type f -name test.mk)))
FULL_TESTS := $(notdir $(TEST_LIST))
include $(QUANTUM_PATH)/debounce/tests/testlist.mk
include $(QUANTUM_PATH)/encoder/tests/testlist.mk

19
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"

20
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

73
tests/auto_shift/test_auto_shift.cpp

@ -0,0 +1,73 @@
/* 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_CALL(driver, send_keyboard_mock(_)).Times(0);
regular_key.press();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
/* Release regular key */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
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_CALL(driver, send_keyboard_mock(_)).Times(0);
regular_key.press();
idle_for(AUTO_SHIFT_TIMEOUT);
testing::Mock::VerifyAndClearExpectations(&driver);
/* Release regular key */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_A)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
regular_key.release();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
}

3
tests/basic/config.h

@ -16,5 +16,4 @@
#pragma once
#define MATRIX_ROWS 4
#define MATRIX_COLS 10
#include "test_common.h"

45
tests/basic/keymap.c

@ -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;
};

18
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
# --------------------------------------------------------------------------------

427
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,339 @@ 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. */
/* TODO: QMK currently sends an empty report even if nothing needs to be reported to the host! */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(1);
layer_key.press();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
/* TODO: QMK currently sends an empty report even if nothing needs to be reported to the host! */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(1);
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. */
/* TODO: QMK currently sends an empty report even if nothing needs to be reported to the host! */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(1);
layer_key.press();
run_one_scan_loop();
EXPECT_TRUE(layer_state_is(1));
testing::Mock::VerifyAndClearExpectations(&driver);
/* Press key on layer 1 */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(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_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(1);
regular_key.release();
run_one_scan_loop();
EXPECT_TRUE(layer_state_is(1));
testing::Mock::VerifyAndClearExpectations(&driver);
/* Release MO */
/* TODO: QMK currently sends an empty report even if nothing needs to be reported to the host! */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(1);
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_CALL(driver, send_keyboard_mock(_)).Times(0);
layer_key.press();
run_one_scan_loop();
EXPECT_TRUE(layer_state_is(1));
testing::Mock::VerifyAndClearExpectations(&driver);
/* Release TG. */
/* TODO: QMK currently sends an empty report even if nothing needs to be reported to the host! */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(1);
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_CALL(driver, send_keyboard_mock(_)).Times(0);
toggle_layer_1_on_layer_0.press();
run_one_scan_loop();
EXPECT_TRUE(layer_state_is(1));
testing::Mock::VerifyAndClearExpectations(&driver);
/* TODO: QMK currently sends an empty report even if nothing needs to be reported to the host! */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(1);
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_CALL(driver, send_keyboard_mock(_)).Times(0);
toggle_layer_0_on_layer_1.press();
run_one_scan_loop();
EXPECT_TRUE(layer_state_is(0));
testing::Mock::VerifyAndClearExpectations(&driver);
/* TODO: QMK currently sends an empty report even if nothing needs to be reported to the host! */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(1);
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_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(0);
layer_key.press();
run_one_scan_loop();
EXPECT_TRUE(layer_state_is(1));
testing::Mock::VerifyAndClearExpectations(&driver);
/* TODO: QMK currently sends an empty report even if nothing needs to be reported to the host! */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(2);
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. */
/* TODO: QMK currently sends an empty report even if nothing needs to be reported to the host! */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(0);
layer_key.press();
run_one_scan_loop();
EXPECT_TRUE(layer_state_is(1));
testing::Mock::VerifyAndClearExpectations(&driver);
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_B))).Times(1);
regular_key.press();
run_one_scan_loop();
EXPECT_TRUE(layer_state_is(1));
testing::Mock::VerifyAndClearExpectations(&driver);
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(1);
regular_key.release();
run_one_scan_loop();
EXPECT_TRUE(layer_state_is(1));
testing::Mock::VerifyAndClearExpectations(&driver);
/* TODO: QMK currently sends an empty report even if nothing needs to be reported to the host! */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(1);
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 . */
/* TODO: QMK currently sends an empty report even if nothing needs to be reported to the host! */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(9);
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_CALL(driver, send_keyboard_mock(KeyboardReport(KC_B))).Times(1);
regular_key.press();
run_one_scan_loop();
EXPECT_TRUE(layer_state_is(1));
testing::Mock::VerifyAndClearExpectations(&driver);
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(1);
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_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(0);
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_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RALT))).Times(1);
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(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_CALL(driver, send_keyboard_mock(_)).Times(0);
layer_0_key_0.release();
run_one_scan_loop();
EXPECT_TRUE(layer_state_is(0));
testing::Mock::VerifyAndClearExpectations(&driver);
/* Unregister keycode and modifier. */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RALT))).Times(1);
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(1);
layer_1_key_1.release();
run_one_scan_loop();
EXPECT_TRUE(layer_state_is(0));
testing::Mock::VerifyAndClearExpectations(&driver);
}

213
tests/basic/test_keypress.cpp

@ -14,11 +14,11 @@
* 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 {};
@ -30,96 +30,157 @@ TEST_F(KeyPress, SendKeyboardIsNotCalledWhenNoKeyIsPressed) {
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_CALL(driver, send_keyboard_mock(KeyboardReport(key.report_code)));
keyboard_task();
release_key(0, 0);
key.release();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
keyboard_task();
}
TEST_F(KeyPress, ANonMappedKeyDoesNothing) {
TestDriver driver;
auto key = KeymapKey(0, 0, 0, KC_NO);
set_keymap({key});
key.press();
EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
keyboard_task();
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_CALL(driver, send_keyboard_mock(KeyboardReport(key_b.report_code)));
keyboard_task();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_B, KC_C)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(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_CALL(driver, send_keyboard_mock(KeyboardReport(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_CALL(driver, send_keyboard_mock(KeyboardReport()));
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_LSFT);
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_CALL(driver, send_keyboard_mock(KeyboardReport(key_a.report_code)));
keyboard_task();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A, KC_LEFT_SHIFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_a.report_code, key_lsft.report_code)));
keyboard_task();
release_key(0, 0);
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT)));
key_a.release();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_lsft.report_code)));
keyboard_task();
release_key(3, 0);
key_lsft.release();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
keyboard_task();
}
TEST_F(KeyPress, PressLeftShiftAndControl) {
TestDriver driver;
press_key(3, 0);
press_key(5, 0);
auto key_lsft = KeymapKey(0, 3, 0, KC_LSFT);
auto key_lctrl = KeymapKey(0, 5, 0, KC_LCTRL);
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_LEFT_SHIFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_lsft.report_code)));
keyboard_task();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_lsft.report_code, key_lctrl.report_code)));
keyboard_task();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT, KC_LEFT_CTRL)));
key_lsft.release();
key_lctrl.release();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_lctrl.report_code)));
keyboard_task();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
keyboard_task();
}
TEST_F(KeyPress, LeftAndRightShiftCanBePressedAtTheSameTime) {
TestDriver driver;
press_key(3, 0);
press_key(4, 0);
auto key_lsft = KeymapKey(0, 3, 0, KC_LSFT);
auto key_rsft = KeymapKey(0, 4, 0, KC_RSFT);
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_LEFT_SHIFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_lsft.report_code)));
keyboard_task();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT, KC_RIGHT_SHIFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_lsft.report_code, key_rsft.report_code)));
keyboard_task();
key_lsft.release();
key_rsft.release();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_rsft.report_code)));
keyboard_task();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
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_RIGHT_SHIFT, KC_RIGHT_CTRL)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RIGHT_SHIFT, KC_RIGHT_CTRL, KC_O)));
combo_key.press();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL, KC_O)));
keyboard_task();
release_key(6, 0);
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RIGHT_SHIFT, KC_RIGHT_CTRL)));
combo_key.release();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
keyboard_task();
}
@ -127,25 +188,29 @@ TEST_F(KeyPress, RightShiftLeftControlAndCharWithTheSameKey) {
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_EQL);
press_key(1, 1); // KC_PLUS
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT, KC_EQUAL)));
set_keymap({key_plus, key_eql});
key_plus.press();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL)));
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
release_key(1, 1); // KC_PLUS
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT)));
key_plus.release();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
press_key(0, 1); // KC_EQUAL
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQUAL)));
key_eql.press();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(key_eql.report_code)));
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
release_key(0, 1); // KC_EQUAL
key_eql.release();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
@ -154,27 +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_EQL);
press_key(1, 1); // KC_PLUS
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT, KC_EQUAL)));
set_keymap({key_plus, key_eql});
key_plus.press();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL)));
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
press_key(0, 1); // KC_EQUAL
key_eql.press();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQUAL)));
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
release_key(1, 1); // KC_PLUS
// BUG: Should really still return KC_EQUAL, but this is fine too
key_plus.release();
// 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);
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
release_key(0, 1); // KC_EQUAL
key_eql.release();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
@ -183,25 +252,29 @@ 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_EQL);
press_key(0, 1); // KC_EQUAL
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQUAL)));
set_keymap({key_plus, key_eql});
key_eql.press();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL)));
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
release_key(0, 1); // KC_EQUAL
key_eql.release();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
press_key(1, 1); // KC_PLUS
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT, KC_EQUAL)));
key_plus.press();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL)));
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
release_key(1, 1); // KC_PLUS
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT)));
key_plus.release();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
@ -210,13 +283,17 @@ 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_EQL);
press_key(0, 1); // KC_EQUAL
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQUAL)));
set_keymap({key_plus, key_eql});
key_eql.press();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL)));
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_LEFT_SHIFT, KC_EQUAL)));
@ -225,16 +302,16 @@ TEST_F(KeyPress, PressEqualPlusDontReleaseBeforePress) {
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
release_key(0, 1); // KC_EQUAL
key_eql.release();
// I guess it's fine to still report shift here
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT)));
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
release_key(1, 1); // KC_PLUS
key_plus.release();
// This report is not needed
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
}
}

19
tests/basic/test_macro.cpp

@ -24,10 +24,25 @@ class Macro : public TestFixture {};
#define AT_TIME(t) WillOnce(InvokeWithoutArgs([current_time]() { EXPECT_EQ(timer_elapsed32(current_time), t); }))
extern "C" 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;
};
TEST_F(Macro, PlayASimpleMacro) {
TestDriver driver;
InSequence s;
press_key(8, 0);
auto key_macro = KeymapKey(0, 8, 0, M(0));
set_keymap({key_macro});
key_macro.press();
uint32_t current_time = timer_read32();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT))).AT_TIME(0);
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT, KC_H))).AT_TIME(0);
@ -68,4 +83,6 @@ TEST_F(Macro, PlayASimpleMacro) {
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT))).AT_TIME(210);
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).AT_TIME(220);
run_one_scan_loop();
key_macro.release();
}

197
tests/basic/test_one_shot_keys.cpp

@ -0,0 +1,197 @@
/* 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_CALL(driver, send_keyboard_mock(_)).Times(0);
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_CALL(driver, send_keyboard_mock(KeyboardReport(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_CALL(driver, send_keyboard_mock(_)).Times(0);
osm_key.press();
run_one_scan_loop();
osm_key.release();
idle_for(ONESHOT_TIMEOUT);
testing::Mock::VerifyAndClearExpectations(&driver);
/* Press regular key */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(regular_key.report_code))).Times(1);
regular_key.press();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
/* Release regular key */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
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_CALL(driver, send_keyboard_mock(_)).Times(0);
osm_key.press();
run_one_scan_loop();
osm_key.release();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
/* Press regular key */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(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_CALL(driver, send_keyboard_mock(KeyboardReport()));
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_CALL(driver, send_keyboard_mock(_)).Times(0);
osm_key.press();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
/* Press regular key */
EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
regular_key.press();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
/* Release regular key */
EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
regular_key.release();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
/* Release OSM */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(regular_key.report_code, osm_key.report_code))).Times(1);
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).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
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_CALL(driver, send_keyboard_mock(_)).Times(0);
osl_key.press();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
/* Release OSL key */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(2);
osl_key.release();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
/* Press regular key */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(regular_key.report_code))).Times(2);
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
regular_key.press();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
/* Release regular key */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
regular_key.release();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
}

50
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,14 +28,19 @@ 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
key_shift_hold_p_tap.press();
EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
run_one_scan_loop();
release_key(7, 0);
// First we get the key press
key_shift_hold_p_tap.release();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_P)));
// Then the release
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
run_one_scan_loop();
@ -41,12 +49,21 @@ TEST_F(Tapping, TapA_SHFT_T_KeyReportsKey) {
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);
idle_for(TAPPING_TERM);
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LEFT_SHIFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
run_one_scan_loop();
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
mod_tap_hold_key.release();
run_one_scan_loop();
}
@ -54,12 +71,16 @@ 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
key_shift_hold_p_tap.press();
EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
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)));
// Then the release
@ -67,19 +88,20 @@ TEST_F(Tapping, ANewTapWithinTappingTermIsBuggy) {
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)));
run_one_scan_loop();
release_key(7, 0);