aboutsummaryrefslogtreecommitdiff
path: root/tests/test_common/test_fixture.cpp
diff options
context:
space:
mode:
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 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
9extern "C" { 16extern "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
14void set_time(uint32_t t); 26void set_time(uint32_t t);
15void advance_time(uint32_t ms); 27void advance_time(uint32_t ms);
16} 28}
17 29
18using testing::_; 30using testing::_;
19using testing::AnyNumber; 31
20using testing::Between; 32/* This is used for dynamic dispatching keymap_key_to_keycode calls to the current active test_fixture. */
21using testing::Return; 33TestFixture* 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. */
37extern "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
23void TestFixture::SetUpTestCase() { 43void 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
33void TestFixture::TearDownTestCase() {} 57void TestFixture::TearDownTestCase() {}
34 58
35TestFixture::TestFixture() {} 59TestFixture::TestFixture() { m_this = this; }
36 60
37TestFixture::~TestFixture() { 61TestFixture::~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
97void 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
105void TestFixture::set_keymap(std::initializer_list<KeymapKey> keys) {
106 this->keymap.clear();
107 for (auto& key : keys) {
108 add_key(key);
109 }
110}
111
112const 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
123void 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
50void TestFixture::run_one_scan_loop() { 148void 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
159void 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
168void 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}