diff options
| author | Stefan Kerkmann <karlk90@pm.me> | 2021-11-22 22:15:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-23 08:15:04 +1100 |
| commit | b6054c0206609f3755f71d819643644d250288b0 (patch) | |
| tree | 4f6f33040bb5184d47144090058eb089d2782b6c /tests/test_common/test_fixture.cpp | |
| parent | f4966a19d69a7f3bbefeea0537071d7d7c2abbdd (diff) | |
| download | qmk_firmware-b6054c0206609f3755f71d819643644d250288b0.tar.gz qmk_firmware-b6054c0206609f3755f71d819643644d250288b0.zip | |
[Tests] Increase QMK test coverage (#13789)
* 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
* `qmk format-c
* Fix tests
Co-authored-by: Nick Brassel <nick@tzarc.org>
Diffstat (limited to 'tests/test_common/test_fixture.cpp')
| -rw-r--r-- | tests/test_common/test_fixture.cpp | 142 |
1 files changed, 127 insertions, 15 deletions
diff --git a/tests/test_common/test_fixture.cpp b/tests/test_common/test_fixture.cpp index e041df712..0601b1719 100644 --- a/tests/test_common/test_fixture.cpp +++ b/tests/test_common/test_fixture.cpp | |||
| @@ -1,26 +1,48 @@ | |||
| 1 | #include "test_fixture.hpp" | 1 | #include "test_fixture.hpp" |
| 2 | #include <algorithm> | ||
| 3 | #include <cstdint> | ||
| 4 | #include <cstdio> | ||
| 5 | #include <cstdlib> | ||
| 6 | #include "gmock/gmock-cardinalities.h" | ||
| 2 | #include "gmock/gmock.h" | 7 | #include "gmock/gmock.h" |
| 8 | #include "gtest/gtest.h" | ||
| 9 | #include "keyboard_report_util.hpp" | ||
| 10 | #include "keycode.h" | ||
| 3 | #include "test_driver.hpp" | 11 | #include "test_driver.hpp" |
| 12 | #include "test_logger.hpp" | ||
| 4 | #include "test_matrix.h" | 13 | #include "test_matrix.h" |
| 5 | #include "keyboard.h" | 14 | #include "test_keymap_key.hpp" |
| 6 | #include "action.h" | ||
| 7 | #include "action_tapping.h" | ||
| 8 | 15 | ||
| 9 | extern "C" { | 16 | extern "C" { |
| 17 | #include "action.h" | ||
| 18 | #include "action_tapping.h" | ||
| 19 | #include "action_util.h" | ||
| 20 | #include "action_layer.h" | ||
| 10 | #include "debug.h" | 21 | #include "debug.h" |
| 11 | #include "eeconfig.h" | 22 | #include "eeconfig.h" |
| 12 | #include "action_layer.h" | 23 | #include "keyboard.h" |
| 24 | #include "keymap.h" | ||
| 13 | 25 | ||
| 14 | void set_time(uint32_t t); | 26 | void set_time(uint32_t t); |
| 15 | void advance_time(uint32_t ms); | 27 | void advance_time(uint32_t ms); |
| 16 | } | 28 | } |
| 17 | 29 | ||
| 18 | using testing::_; | 30 | using testing::_; |
| 19 | using testing::AnyNumber; | 31 | |
| 20 | using testing::Between; | 32 | /* This is used for dynamic dispatching keymap_key_to_keycode calls to the current active test_fixture. */ |
| 21 | using testing::Return; | 33 | TestFixture* TestFixture::m_this = nullptr; |
| 34 | |||
| 35 | /* Override weak QMK function to allow the usage of isolated per-test keymaps in unit-tests. | ||
| 36 | * The actual call is dynamicaly dispatched to the current active test fixture, which in turn has it's own keymap. */ | ||
| 37 | extern "C" uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t position) { | ||
| 38 | uint16_t keycode; | ||
| 39 | TestFixture::m_this->get_keycode(layer, position, &keycode); | ||
| 40 | return keycode; | ||
| 41 | } | ||
| 22 | 42 | ||
| 23 | void TestFixture::SetUpTestCase() { | 43 | void TestFixture::SetUpTestCase() { |
| 44 | test_logger.info() << "TestFixture setup-up start." << std::endl; | ||
| 45 | |||
| 24 | // The following is enough to bootstrap the values set in main | 46 | // The following is enough to bootstrap the values set in main |
| 25 | eeconfig_init_quantum(); | 47 | eeconfig_init_quantum(); |
| 26 | eeconfig_update_debug(debug_config.raw); | 48 | eeconfig_update_debug(debug_config.raw); |
| @@ -28,23 +50,99 @@ void TestFixture::SetUpTestCase() { | |||
| 28 | TestDriver driver; | 50 | TestDriver driver; |
| 29 | EXPECT_CALL(driver, send_keyboard_mock(_)); | 51 | EXPECT_CALL(driver, send_keyboard_mock(_)); |
| 30 | keyboard_init(); | 52 | keyboard_init(); |
| 53 | |||
| 54 | test_logger.info() << "TestFixture setup-up end." << std::endl; | ||
| 31 | } | 55 | } |
| 32 | 56 | ||
| 33 | void TestFixture::TearDownTestCase() {} | 57 | void TestFixture::TearDownTestCase() {} |
| 34 | 58 | ||
| 35 | TestFixture::TestFixture() {} | 59 | TestFixture::TestFixture() { m_this = this; } |
| 36 | 60 | ||
| 37 | TestFixture::~TestFixture() { | 61 | TestFixture::~TestFixture() { |
| 62 | test_logger.info() << "TestFixture clean-up start." << std::endl; | ||
| 38 | TestDriver driver; | 63 | TestDriver driver; |
| 39 | // Run for a while to make sure all keys are completely released | 64 | |
| 40 | EXPECT_CALL(driver, send_keyboard_mock(_)).Times(AnyNumber()); | 65 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(2); |
| 41 | layer_clear(); | 66 | |
| 67 | /* Reset keyboard state. */ | ||
| 42 | clear_all_keys(); | 68 | clear_all_keys(); |
| 43 | idle_for(TAPPING_TERM + 10); | 69 | |
| 70 | clear_keyboard(); | ||
| 71 | |||
| 72 | clear_oneshot_mods(); | ||
| 73 | clear_oneshot_locked_mods(); | ||
| 74 | reset_oneshot_layer(); | ||
| 75 | |||
| 76 | layer_clear(); | ||
| 77 | |||
| 78 | #if defined(SWAP_HANDS_ENABLE) | ||
| 79 | clear_oneshot_swaphands(); | ||
| 80 | #endif | ||
| 81 | |||
| 82 | idle_for(TAPPING_TERM * 10); | ||
| 83 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
| 84 | |||
| 85 | /* Verify that the matrix really is cleared */ | ||
| 86 | EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); | ||
| 87 | idle_for(TAPPING_TERM * 10); | ||
| 44 | testing::Mock::VerifyAndClearExpectations(&driver); | 88 | testing::Mock::VerifyAndClearExpectations(&driver); |
| 45 | // Verify that the matrix really is cleared | 89 | |
| 46 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(0); | 90 | m_this = nullptr; |
| 47 | idle_for(TAPPING_TERM + 10); | 91 | |
| 92 | test_logger.info() << "TestFixture clean-up end." << std::endl; | ||
| 93 | |||
| 94 | print_test_log(); | ||
| 95 | } | ||
| 96 | |||
| 97 | void TestFixture::add_key(KeymapKey key) { | ||
| 98 | if (this->find_key(key.layer, key.position)) { | ||
| 99 | FAIL() << "Key is already mapped for layer " << +key.layer << " and (column,row) (" << +key.position.col << "," << +key.position.row << ")"; | ||
| 100 | } | ||
| 101 | |||
| 102 | this->keymap.push_back(key); | ||
| 103 | } | ||
| 104 | |||
| 105 | void TestFixture::set_keymap(std::initializer_list<KeymapKey> keys) { | ||
| 106 | this->keymap.clear(); | ||
| 107 | for (auto& key : keys) { | ||
| 108 | add_key(key); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | const KeymapKey* TestFixture::find_key(layer_t layer, keypos_t position) const { | ||
| 113 | auto keymap_key_predicate = [&](KeymapKey candidate) { return candidate.layer == layer && candidate.position.col == position.col && candidate.position.row == position.row; }; | ||
| 114 | |||
| 115 | auto result = std::find_if(this->keymap.begin(), this->keymap.end(), keymap_key_predicate); | ||
| 116 | |||
| 117 | if (result != std::end(this->keymap)) { | ||
| 118 | return &(*result); | ||
| 119 | } | ||
| 120 | return nullptr; | ||
| 121 | } | ||
| 122 | |||
| 123 | void TestFixture::get_keycode(const layer_t layer, const keypos_t position, uint16_t* result) const { | ||
| 124 | bool key_is_out_of_bounds = position.col >= MATRIX_COLS && position.row >= MATRIX_ROWS; | ||
| 125 | |||
| 126 | if (key_is_out_of_bounds) { | ||
| 127 | /* See if this is done in hardware as well, because this is 100% out of bounds reads on all QMK keebs out there. */ | ||
| 128 | auto msg = [&]() { | ||
| 129 | std::stringstream msg; | ||
| 130 | msg << "Keycode for position (" << +position.col << "," << +position.row << ") requested! This is out of bounds." << std::endl; | ||
| 131 | return msg.str(); | ||
| 132 | }(); | ||
| 133 | |||
| 134 | *result = KC_NO; | ||
| 135 | test_logger.error() << msg; | ||
| 136 | EXPECT_FALSE(key_is_out_of_bounds) << msg; | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | |||
| 140 | if (auto key = this->find_key(layer, position)) { | ||
| 141 | *result = key->code; | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | |||
| 145 | FAIL() << "No key is mapped for layer " << +layer << " and (column,row) " << +position.col << "," << +position.row << ")"; | ||
| 48 | } | 146 | } |
| 49 | 147 | ||
| 50 | void TestFixture::run_one_scan_loop() { | 148 | void TestFixture::run_one_scan_loop() { |
| @@ -57,3 +155,17 @@ void TestFixture::idle_for(unsigned time) { | |||
| 57 | run_one_scan_loop(); | 155 | run_one_scan_loop(); |
| 58 | } | 156 | } |
| 59 | } | 157 | } |
| 158 | |||
| 159 | void TestFixture::print_test_log() const { | ||
| 160 | const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info(); | ||
| 161 | if (HasFailure()) { | ||
| 162 | std::cerr << test_info->test_case_name() << "." << test_info->name() << " failed!" << std::endl; | ||
| 163 | test_logger.print_log(); | ||
| 164 | } | ||
| 165 | test_logger.reset(); | ||
| 166 | } | ||
| 167 | |||
| 168 | void TestFixture::expect_layer_state(layer_t layer_state) const { | ||
| 169 | test_logger.trace() << "Layer state: (" << +layer_state << ") Highest layer bit: (" << +get_highest_layer(layer_state) << ")" << std::endl; | ||
| 170 | EXPECT_TRUE(layer_state_is(layer_state)); | ||
| 171 | } | ||
