summaryrefslogtreecommitdiff
path: root/tests/test_common/test_fixture.cpp
diff options
context:
space:
mode:
authorStefan Kerkmann <karlk90@pm.me>2021-11-23 03:31:01 +0100
committerGitHub <noreply@github.com>2021-11-23 13:31:01 +1100
commita24bdccee0580d1263733bc7e66e4e4f97713f19 (patch)
tree868cb12a436a87b39c936292f442bcc266ae0224 /tests/test_common/test_fixture.cpp
parente20bc76a1e05d02c15a452e51fa76d9ec39b0369 (diff)
[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>
Diffstat (limited to 'tests/test_common/test_fixture.cpp')
-rw-r--r--tests/test_common/test_fixture.cpp142
1 files changed, 127 insertions, 15 deletions
diff --git a/tests/test_common/test_fixture.cpp b/tests/test_common/test_fixture.cpp
index e041df7128..0601b17191 100644
--- a/tests/test_common/test_fixture.cpp
+++ b/tests/test_common/test_fixture.cpp
@@ -1,26 +1,48 @@
#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);
@@ -28,23 +50,99 @@ void TestFixture::SetUpTestCase() {
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();
+
+ EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(2);
+
+ /* 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_CALL(driver, send_keyboard_mock(_)).Times(0);
+ 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::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 +155,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));
+}