2017-07-02 20:46:35 +02:00
|
|
|
#include "test_fixture.hpp"
|
2021-11-23 03:31:01 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include "gmock/gmock-cardinalities.h"
|
2017-06-18 22:49:38 +02:00
|
|
|
#include "gmock/gmock.h"
|
2021-11-23 03:31:01 +01:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "keyboard_report_util.hpp"
|
|
|
|
#include "keycode.h"
|
2017-07-02 20:46:35 +02:00
|
|
|
#include "test_driver.hpp"
|
2021-11-23 03:31:01 +01:00
|
|
|
#include "test_logger.hpp"
|
2017-06-18 22:49:38 +02:00
|
|
|
#include "test_matrix.h"
|
2021-11-23 03:31:01 +01:00
|
|
|
#include "test_keymap_key.hpp"
|
2021-11-22 23:54:04 +01:00
|
|
|
|
|
|
|
extern "C" {
|
2021-11-23 03:31:01 +01:00
|
|
|
#include "action.h"
|
|
|
|
#include "action_tapping.h"
|
|
|
|
#include "action_util.h"
|
|
|
|
#include "action_layer.h"
|
2021-07-25 18:18:09 +02:00
|
|
|
#include "debug.h"
|
|
|
|
#include "eeconfig.h"
|
2021-11-23 03:31:01 +01:00
|
|
|
#include "keyboard.h"
|
|
|
|
#include "keymap.h"
|
2017-12-15 00:15:52 +01:00
|
|
|
|
2019-08-30 20:19:03 +02:00
|
|
|
void set_time(uint32_t t);
|
|
|
|
void advance_time(uint32_t ms);
|
2017-07-01 21:25:06 +02:00
|
|
|
}
|
2017-06-18 22:49:38 +02:00
|
|
|
|
|
|
|
using testing::_;
|
2021-11-23 03:31:01 +01:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
2017-06-18 22:49:38 +02:00
|
|
|
|
|
|
|
void TestFixture::SetUpTestCase() {
|
2021-11-23 03:31:01 +01:00
|
|
|
test_logger.info() << "TestFixture setup-up start." << std::endl;
|
|
|
|
|
2021-07-25 18:18:09 +02:00
|
|
|
// The following is enough to bootstrap the values set in main
|
|
|
|
eeconfig_init_quantum();
|
|
|
|
eeconfig_update_debug(debug_config.raw);
|
|
|
|
|
2017-06-18 22:49:38 +02:00
|
|
|
TestDriver driver;
|
|
|
|
keyboard_init();
|
2021-11-23 03:31:01 +01:00
|
|
|
|
|
|
|
test_logger.info() << "TestFixture setup-up end." << std::endl;
|
2017-06-18 22:49:38 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 20:19:03 +02:00
|
|
|
void TestFixture::TearDownTestCase() {}
|
2017-06-18 22:49:38 +02:00
|
|
|
|
2022-02-12 19:29:31 +01:00
|
|
|
TestFixture::TestFixture() {
|
|
|
|
m_this = this;
|
|
|
|
}
|
2017-06-18 22:49:38 +02:00
|
|
|
|
|
|
|
TestFixture::~TestFixture() {
|
2021-11-23 03:31:01 +01:00
|
|
|
test_logger.info() << "TestFixture clean-up start." << std::endl;
|
2017-06-18 22:49:38 +02:00
|
|
|
TestDriver driver;
|
2021-11-23 03:31:01 +01:00
|
|
|
|
|
|
|
/* Reset keyboard state. */
|
2021-11-22 23:54:04 +01:00
|
|
|
clear_all_keys();
|
2021-11-23 03:31:01 +01:00
|
|
|
|
|
|
|
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 */
|
Fix and add unit tests for Caps Word to work with Unicode Map, Auto Shift, Retro Shift. (#17284)
* Fix Caps Word and Unicode Map
* Tests for Caps Word + Auto Shift and Unicode Map.
* Fix formatting
* Add additional keyboard report expectation macros
This commit defines five test utilities, EXPECT_REPORT, EXPECT_UNICODE,
EXPECT_EMPTY_REPORT, EXPECT_ANY_REPORT and EXPECT_NO_REPORT for use with
TestDriver.
EXPECT_REPORT sets a gmock expectation that a given keyboard report will
be sent. For instance,
EXPECT_REPORT(driver, (KC_LSFT, KC_A));
is shorthand for
EXPECT_CALL(driver,
send_keyboard_mock(KeyboardReport(KC_LSFT, KC_A)));
EXPECT_UNICODE sets a gmock expectation that a given Unicode code point
will be sent using 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".
EXPECT_EMPTY_REPORT sets a gmock expectation that a given keyboard
report will be sent. For instance
EXPECT_EMPTY_REPORT(driver);
expects a single report without keypresses or modifiers.
EXPECT_ANY_REPORT sets a gmock expectation that a arbitrary keyboard
report will be sent, without matching its contents. For instance
EXPECT_ANY_REPORT(driver).Times(1);
expects a single arbitrary keyboard report will be sent.
EXPECT_NO_REPORT sets a gmock expectation that no keyboard report will
be sent at all.
* Add tap_key() and tap_keys() to TestFixture.
This commit adds a `tap_key(key)` method to TestFixture that taps a
given KeymapKey, optionally with a specified delay between press and
release.
Similarly, the method `tap_keys(key_a, key_b, key_c)` taps a sequence of
KeymapKeys.
* Use EXPECT_REPORT, tap_keys, etc. in most tests.
This commit uses EXPECT_REPORT, EXPECT_UNICODE, EXPECT_EMPTY_REPORT,
EXPECT_NO_REPORT, tap_key() and tap_keys() test utilities from the
previous two commits in most tests. Particularly the EXPECT_REPORT
macro is frequently useful and makes a nice reduction in boilerplate
needed to express many tests.
Co-authored-by: David Kosorin <david@kosorin.net>
2022-06-05 09:14:02 +02:00
|
|
|
EXPECT_NO_REPORT(driver);
|
2021-11-23 03:31:01 +01:00
|
|
|
idle_for(TAPPING_TERM * 10);
|
2017-12-15 00:15:52 +01:00
|
|
|
testing::Mock::VerifyAndClearExpectations(&driver);
|
2021-11-23 03:31:01 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
Fix and add unit tests for Caps Word to work with Unicode Map, Auto Shift, Retro Shift. (#17284)
* Fix Caps Word and Unicode Map
* Tests for Caps Word + Auto Shift and Unicode Map.
* Fix formatting
* Add additional keyboard report expectation macros
This commit defines five test utilities, EXPECT_REPORT, EXPECT_UNICODE,
EXPECT_EMPTY_REPORT, EXPECT_ANY_REPORT and EXPECT_NO_REPORT for use with
TestDriver.
EXPECT_REPORT sets a gmock expectation that a given keyboard report will
be sent. For instance,
EXPECT_REPORT(driver, (KC_LSFT, KC_A));
is shorthand for
EXPECT_CALL(driver,
send_keyboard_mock(KeyboardReport(KC_LSFT, KC_A)));
EXPECT_UNICODE sets a gmock expectation that a given Unicode code point
will be sent using 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".
EXPECT_EMPTY_REPORT sets a gmock expectation that a given keyboard
report will be sent. For instance
EXPECT_EMPTY_REPORT(driver);
expects a single report without keypresses or modifiers.
EXPECT_ANY_REPORT sets a gmock expectation that a arbitrary keyboard
report will be sent, without matching its contents. For instance
EXPECT_ANY_REPORT(driver).Times(1);
expects a single arbitrary keyboard report will be sent.
EXPECT_NO_REPORT sets a gmock expectation that no keyboard report will
be sent at all.
* Add tap_key() and tap_keys() to TestFixture.
This commit adds a `tap_key(key)` method to TestFixture that taps a
given KeymapKey, optionally with a specified delay between press and
release.
Similarly, the method `tap_keys(key_a, key_b, key_c)` taps a sequence of
KeymapKeys.
* Use EXPECT_REPORT, tap_keys, etc. in most tests.
This commit uses EXPECT_REPORT, EXPECT_UNICODE, EXPECT_EMPTY_REPORT,
EXPECT_NO_REPORT, tap_key() and tap_keys() test utilities from the
previous two commits in most tests. Particularly the EXPECT_REPORT
macro is frequently useful and makes a nice reduction in boilerplate
needed to express many tests.
Co-authored-by: David Kosorin <david@kosorin.net>
2022-06-05 09:14:02 +02:00
|
|
|
void TestFixture::tap_key(KeymapKey key, unsigned delay_ms) {
|
|
|
|
key.press();
|
|
|
|
idle_for(delay_ms);
|
|
|
|
key.release();
|
|
|
|
run_one_scan_loop();
|
|
|
|
}
|
|
|
|
|
2022-08-13 15:48:51 +02:00
|
|
|
void TestFixture::tap_combo(const std::vector<KeymapKey>& chord_keys, unsigned delay_ms) {
|
|
|
|
for (KeymapKey key : chord_keys) { // Press each key.
|
|
|
|
key.press();
|
|
|
|
run_one_scan_loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delay_ms > 1) {
|
|
|
|
idle_for(delay_ms - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (KeymapKey key : chord_keys) { // Release each key.
|
|
|
|
key.release();
|
|
|
|
run_one_scan_loop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 03:31:01 +01:00
|
|
|
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 << ")";
|
2017-07-01 21:25:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestFixture::run_one_scan_loop() {
|
|
|
|
keyboard_task();
|
|
|
|
advance_time(1);
|
|
|
|
}
|
|
|
|
|
2017-07-11 18:41:04 +02:00
|
|
|
void TestFixture::idle_for(unsigned time) {
|
2019-08-30 20:19:03 +02:00
|
|
|
for (unsigned i = 0; i < time; i++) {
|
2017-07-01 21:25:06 +02:00
|
|
|
run_one_scan_loop();
|
|
|
|
}
|
2017-12-15 00:15:52 +01:00
|
|
|
}
|
2021-11-23 03:31:01 +01:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|