Skip to content

Commit

Permalink
Add timestamps to test messages and log keypress duration
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlK90 committed Dec 14, 2022
1 parent b4990d8 commit 1ec9505
Show file tree
Hide file tree
Showing 20 changed files with 1,009 additions and 41 deletions.
2 changes: 2 additions & 0 deletions builddefs/build_full_test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ $(TEST)_SRC := \
tests/test_common/matrix.c \
tests/test_common/test_driver.cpp \
tests/test_common/keyboard_report_util.cpp \
tests/test_common/keycode_util.cpp \
tests/test_common/keycode_table.cpp \
tests/test_common/test_fixture.cpp \
tests/test_common/test_keymap_key.cpp \
tests/test_common/test_logger.cpp \
Expand Down
1 change: 1 addition & 0 deletions lib/python/qmk/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
'qmk.cli.generate.keyboard_c',
'qmk.cli.generate.keyboard_h',
'qmk.cli.generate.keycodes',
'qmk.cli.generate.keycodes_tests',
'qmk.cli.generate.rgb_breathe_table',
'qmk.cli.generate.rules_mk',
'qmk.cli.generate.version_h',
Expand Down
39 changes: 39 additions & 0 deletions lib/python/qmk/cli/generate/keycodes_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Used by the make system to generate a keycode lookup table from keycodes_{version}.json
"""
from milc import cli

from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
from qmk.commands import dump_lines
from qmk.path import normpath
from qmk.keycodes import load_spec


def _generate_defines(lines, keycodes):
lines.append('')
lines.append('std::map<uint16_t, std::string> KEYCODE_ID_TABLE = {')
for key, value in keycodes["keycodes"].items():
lines.append(f' {{{value.get("key")}, "{value.get("key")}"}},')
lines.append('};')


@cli.argument('-v', '--version', arg_only=True, required=True, help='Version of keycodes to generate.')
@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
@cli.subcommand('Used by the make system to generate a keycode lookup table from keycodes_{version}.json', hidden=True)
def generate_keycodes_tests(cli):
"""Generates a keycode to identifier lookup table for unit test output.
"""

# Build the keycodes.h file.
keycodes_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '// clang-format off']
keycodes_h_lines.append('extern "C" {\n#include <keycode.h>\n}')
keycodes_h_lines.append('#include <map>')
keycodes_h_lines.append('#include <string>')
keycodes_h_lines.append('#include <cstdint>')

keycodes = load_spec(cli.args.version)

_generate_defines(keycodes_h_lines, keycodes)

# Show the results
dump_lines(cli.args.output, keycodes_h_lines, cli.args.quiet)
3 changes: 2 additions & 1 deletion platforms/test/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

#include "timer.h"
#include <stdatomic.h>

static uint32_t current_time = 0;
static atomic_uint_least32_t current_time = 0;

void timer_init(void) {
current_time = 0;
Expand Down
4 changes: 2 additions & 2 deletions quantum/action_tapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ void action_tapping_process(keyrecord_t record) {
# define TAP_GET_RETRO_TAPPING true
# endif
# define MAYBE_RETRO_SHIFTING(ev) (TAP_GET_RETRO_TAPPING && (RETRO_SHIFT + 0) != 0 && TIMER_DIFF_16((ev).time, tapping_key.event.time) < (RETRO_SHIFT + 0))
# define TAP_IS_LT IS_LT(tapping_keycode)
# define TAP_IS_MT IS_MT(tapping_keycode)
# define TAP_IS_LT IS_QK_LAYER_TAP(tapping_keycode)
# define TAP_IS_MT IS_QK_MOD_TAP(tapping_keycode)
# define TAP_IS_RETRO IS_RETRO(tapping_keycode)
# else
# define TAP_GET_RETRO_TAPPING false
Expand Down
2 changes: 1 addition & 1 deletion quantum/process_keycode/process_auto_shift.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ bool process_auto_shift(uint16_t keycode, keyrecord_t *record) {
# elif defined(IGNORE_MOD_TAP_INTERRUPT)
const bool is_hold_on_interrupt = false;
# else
const bool is_hold_on_interrupt = IS_MT(keycode);
const bool is_hold_on_interrupt = IS_QK_MOD_TAP(keycode);
# endif
# endif
if (IS_RETRO(keycode)
Expand Down
5 changes: 2 additions & 3 deletions quantum/process_keycode/process_auto_shift.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
# define AUTO_SHIFT_TIMEOUT 175
#endif

#define IS_LT(kc) ((kc) >= QK_LAYER_TAP && (kc) <= QK_LAYER_TAP_MAX)
#define IS_MT(kc) ((kc) >= QK_MOD_TAP && (kc) <= QK_MOD_TAP_MAX)
#define IS_RETRO(kc) (IS_MT(kc) || IS_LT(kc))
#define IS_RETRO(kc) (IS_QK_MOD_TAP(kc) || IS_QK_LAYER_TAP(kc))

#define DO_GET_AUTOSHIFT_TIMEOUT(keycode, record, ...) record
// clang-format off
#define AUTO_SHIFT_ALPHA KC_A ... KC_Z
Expand Down
52 changes: 52 additions & 0 deletions tests/basic/test_keycode_util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2022 Stefan Kerkmann
// SPDX-License-Identifier: GPL-2.0-or-later

#include "test_common.hpp"

class KeycodeToIdentifierSuite : public ::testing::TestWithParam<std::pair<std::uint16_t, std::string>> {};

TEST_P(KeycodeToIdentifierSuite, ConversionTests) {
ASSERT_EQ(get_keycode_identifier_or_default(GetParam().first), GetParam().second);
}

INSTANTIATE_TEST_CASE_P(ConversionTestsP, KeycodeToIdentifierSuite,
// clang-format off
::testing::Values(
// Goto layer
std::make_pair(TO(0), "TO(0)"),
std::make_pair(TO(0x1F), "TO(31)"),
// Momentary switch layer
std::make_pair(MO(0), "MO(0)"),
std::make_pair(MO(0x1F), "MO(31)"),
// Set default layer
std::make_pair(DF(0), "DF(0)"),
std::make_pair(DF(0x1F), "DF(31)"),
// Toggle layer
std::make_pair(TG(0), "TG(0)"),
std::make_pair(TG(0x1F), "TG(31)"),
// One-shot layer
std::make_pair(OSL(0), "OSL(0)"),
std::make_pair(OSL(0x1F), "OSL(31)"),
// One-shot mod
std::make_pair(OSM(MOD_LSFT), "OSM(MOD_LSFT)"),
std::make_pair(OSM(MOD_LSFT | MOD_LCTL), "OSM(MOD_LCTL | MOD_LSFT)"),
// Layer Mod
std::make_pair(LM(0, MOD_LSFT), "LM(0, MOD_LSFT)"),
std::make_pair(LM(0xF, MOD_LSFT), "LM(15, MOD_LSFT)"),
std::make_pair(LM(0xF, MOD_LSFT | MOD_LCTL), "LM(15, MOD_LCTL | MOD_LSFT)"),
// Layer tap toggle
std::make_pair(TT(0), "TT(0)"),
std::make_pair(TT(0x1F), "TT(31)"),
// Layer tap
std::make_pair(LT(0, KC_A), "LT(0, KC_A)"),
std::make_pair(LT(0xF, KC_SPACE), "LT(15, KC_SPACE)"),
std::make_pair(LT(1, KC_SPC), "LT(1, KC_SPACE)"),
// Mod tap
std::make_pair(MT(MOD_LCTL, KC_A), "MT(MOD_LCTL, KC_A)"),
std::make_pair(MT(MOD_LCTL | MOD_LSFT, KC_A), "MT(MOD_LCTL | MOD_LSFT, KC_A)"),
std::make_pair(ALT_T(KC_TAB), "MT(MOD_LALT, KC_TAB)"),
// Mods
std::make_pair(LCTL(KC_A), "QK_MODS(KC_A, QK_LCTL)"),
std::make_pair(HYPR(KC_SPACE), "QK_MODS(KC_SPACE, QK_LCTL | QK_LSFT | QK_LALT | QK_LGUI)")
));
// clang-format on
9 changes: 8 additions & 1 deletion tests/caps_word/test_caps_word.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,8 @@ class CapsWordDoubleTapShift : public ::testing::WithParamInterface<CapsWordDoub
TEST_P(CapsWordDoubleTapShift, Activation) {
TestDriver driver;
KeymapKey left_shift(0, 0, 0, GetParam().left_shift_keycode);
set_keymap({left_shift});
KeymapKey esc(0, 0, 1, KC_ESCAPE);
set_keymap({left_shift, esc});

// clang-format off
EXPECT_CALL(driver, send_keyboard_mock(AnyOf(
Expand All @@ -524,6 +525,12 @@ TEST_P(CapsWordDoubleTapShift, Activation) {
EXPECT_EQ(is_caps_word_on(), true);

testing::Mock::VerifyAndClearExpectations(&driver);

// We have to manually reset the internal state of the caps word state
// machine at this point. This due to imperfect test isolation which can't
// reset the caps word double shift timer on test case setup.
idle_for(CAPS_WORD_IDLE_TIMEOUT);
tap_key(esc);
}

// Double tap doesn't count if another key is pressed between the taps.
Expand Down
45 changes: 39 additions & 6 deletions tests/test_common/keyboard_report_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
*/

#include "keyboard_report_util.hpp"
#include <cstdint>
#include <vector>
#include <algorithm>

using namespace testing;

extern std::map<uint16_t, std::string> KEYCODE_ID_TABLE;

namespace {

std::vector<uint8_t> get_keys(const report_keyboard_t& report) {
std::vector<uint8_t> result;
#if defined(NKRO_ENABLE)
Expand All @@ -36,6 +41,19 @@ std::vector<uint8_t> get_keys(const report_keyboard_t& report) {
std::sort(result.begin(), result.end());
return result;
}

std::vector<uint8_t> get_mods(const report_keyboard_t& report) {
std::vector<uint8_t> result;
for (size_t i = 0; i < 8; i++) {
if (report.mods & (1 << i)) {
uint8_t code = KC_LEFT_CTRL + i;
result.emplace_back(code);
}
}
std::sort(result.begin(), result.end());
return result;
}

} // namespace

bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) {
Expand All @@ -44,21 +62,36 @@ 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& report) {
std::ostream& operator<<(std::ostream& os, const report_keyboard_t& report) {
auto keys = get_keys(report);
auto mods = get_mods(report);

// TODO: This should probably print friendly names for the keys
stream << "Keyboard Report: Mods (" << (uint32_t)report.mods << ") Keys (";
os << std::setw(10) << std::left << "report: ";

if (!keys.size() && !mods.size()) {
return os << "empty" << std::endl;
}

os << "(";
for (auto key = keys.cbegin(); key != keys.cend();) {
stream << +(*key);
os << KEYCODE_ID_TABLE.at(*key);
key++;
if (key != keys.cend()) {
stream << ",";
os << ", ";
}
}

os << ") [";

for (auto mod = mods.cbegin(); mod != mods.cend();) {
os << KEYCODE_ID_TABLE.at(*mod);
mod++;
if (mod != mods.cend()) {
os << ", ";
}
}

return stream << ")" << std::endl;
return os << "]" << std::endl;
}

KeyboardReportMatcher::KeyboardReportMatcher(const std::vector<uint8_t>& keys) {
Expand Down
Loading

0 comments on commit 1ec9505

Please sign in to comment.