diff options
Diffstat (limited to 'tests/test_common/test_fixture.cpp')
-rw-r--r-- | tests/test_common/test_fixture.cpp | 142 |
1 files changed, 15 insertions, 127 deletions
diff --git a/tests/test_common/test_fixture.cpp b/tests/test_common/test_fixture.cpp index 0601b1719..e041df712 100644 --- a/tests/test_common/test_fixture.cpp +++ b/tests/test_common/test_fixture.cpp | |||
@@ -1,48 +1,26 @@ | |||
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" | ||
7 | #include "gmock/gmock.h" | 2 | #include "gmock/gmock.h" |
8 | #include "gtest/gtest.h" | ||
9 | #include "keyboard_report_util.hpp" | ||
10 | #include "keycode.h" | ||
11 | #include "test_driver.hpp" | 3 | #include "test_driver.hpp" |
12 | #include "test_logger.hpp" | ||
13 | #include "test_matrix.h" | 4 | #include "test_matrix.h" |
14 | #include "test_keymap_key.hpp" | 5 | #include "keyboard.h" |
15 | |||
16 | extern "C" { | ||
17 | #include "action.h" | 6 | #include "action.h" |
18 | #include "action_tapping.h" | 7 | #include "action_tapping.h" |
19 | #include "action_util.h" | 8 | |
20 | #include "action_layer.h" | 9 | extern "C" { |
21 | #include "debug.h" | 10 | #include "debug.h" |
22 | #include "eeconfig.h" | 11 | #include "eeconfig.h" |
23 | #include "keyboard.h" | 12 | #include "action_layer.h" |
24 | #include "keymap.h" | ||
25 | 13 | ||
26 | void set_time(uint32_t t); | 14 | void set_time(uint32_t t); |
27 | void advance_time(uint32_t ms); | 15 | void advance_time(uint32_t ms); |
28 | } | 16 | } |
29 | 17 | ||
30 | using testing::_; | 18 | using testing::_; |
31 | 19 | using testing::AnyNumber; | |
32 | /* This is used for dynamic dispatching keymap_key_to_keycode calls to the current active test_fixture. */ | 20 | using testing::Between; |
33 | TestFixture* TestFixture::m_this = nullptr; | 21 | using testing::Return; |
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 | } | ||
42 | 22 | ||
43 | void TestFixture::SetUpTestCase() { | 23 | void TestFixture::SetUpTestCase() { |
44 | test_logger.info() << "TestFixture setup-up start." << std::endl; | ||
45 | |||
46 | // The following is enough to bootstrap the values set in main | 24 | // The following is enough to bootstrap the values set in main |
47 | eeconfig_init_quantum(); | 25 | eeconfig_init_quantum(); |
48 | eeconfig_update_debug(debug_config.raw); | 26 | eeconfig_update_debug(debug_config.raw); |
@@ -50,99 +28,23 @@ void TestFixture::SetUpTestCase() { | |||
50 | TestDriver driver; | 28 | TestDriver driver; |
51 | EXPECT_CALL(driver, send_keyboard_mock(_)); | 29 | EXPECT_CALL(driver, send_keyboard_mock(_)); |
52 | keyboard_init(); | 30 | keyboard_init(); |
53 | |||
54 | test_logger.info() << "TestFixture setup-up end." << std::endl; | ||
55 | } | 31 | } |
56 | 32 | ||
57 | void TestFixture::TearDownTestCase() {} | 33 | void TestFixture::TearDownTestCase() {} |
58 | 34 | ||
59 | TestFixture::TestFixture() { m_this = this; } | 35 | TestFixture::TestFixture() {} |
60 | 36 | ||
61 | TestFixture::~TestFixture() { | 37 | TestFixture::~TestFixture() { |
62 | test_logger.info() << "TestFixture clean-up start." << std::endl; | ||
63 | TestDriver driver; | 38 | TestDriver driver; |
64 | 39 | // Run for a while to make sure all keys are completely released | |
65 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(2); | 40 | EXPECT_CALL(driver, send_keyboard_mock(_)).Times(AnyNumber()); |
66 | |||
67 | /* Reset keyboard state. */ | ||
68 | clear_all_keys(); | ||
69 | |||
70 | clear_keyboard(); | ||
71 | |||
72 | clear_oneshot_mods(); | ||
73 | clear_oneshot_locked_mods(); | ||
74 | reset_oneshot_layer(); | ||
75 | |||
76 | layer_clear(); | 41 | layer_clear(); |
77 | 42 | clear_all_keys(); | |
78 | #if defined(SWAP_HANDS_ENABLE) | 43 | idle_for(TAPPING_TERM + 10); |
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); | ||
88 | testing::Mock::VerifyAndClearExpectations(&driver); | 44 | testing::Mock::VerifyAndClearExpectations(&driver); |
89 | 45 | // Verify that the matrix really is cleared | |
90 | m_this = nullptr; | 46 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(0); |
91 | 47 | idle_for(TAPPING_TERM + 10); | |
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 << ")"; | ||
146 | } | 48 | } |
147 | 49 | ||
148 | void TestFixture::run_one_scan_loop() { | 50 | void TestFixture::run_one_scan_loop() { |
@@ -155,17 +57,3 @@ void TestFixture::idle_for(unsigned time) { | |||
155 | run_one_scan_loop(); | 57 | run_one_scan_loop(); |
156 | } | 58 | } |
157 | } | 59 | } |
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 | } | ||