aboutsummaryrefslogtreecommitdiff
path: root/tests/test_common
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_common')
-rw-r--r--tests/test_common/build.mk16
-rw-r--r--tests/test_common/keyboard_report_util.cpp21
-rw-r--r--tests/test_common/keymap.c33
-rw-r--r--tests/test_common/test_common.h4
-rw-r--r--tests/test_common/test_driver.cpp5
-rw-r--r--tests/test_common/test_driver.hpp29
-rw-r--r--tests/test_common/test_fixture.cpp142
-rw-r--r--tests/test_common/test_fixture.hpp21
-rw-r--r--tests/test_common/test_keymap_key.cpp30
-rw-r--r--tests/test_common/test_keymap_key.hpp46
-rw-r--r--tests/test_common/test_logger.cpp39
-rw-r--r--tests/test_common/test_logger.hpp35
12 files changed, 382 insertions, 39 deletions
diff --git a/tests/test_common/build.mk b/tests/test_common/build.mk
new file mode 100644
index 000000000..aeb305785
--- /dev/null
+++ b/tests/test_common/build.mk
@@ -0,0 +1,16 @@
1# Copyright 2021 Stefan Kerkmann
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16CUSTOM_MATRIX=yes \ No newline at end of file
diff --git a/tests/test_common/keyboard_report_util.cpp b/tests/test_common/keyboard_report_util.cpp
index f73cf239e..e148c76be 100644
--- a/tests/test_common/keyboard_report_util.cpp
+++ b/tests/test_common/keyboard_report_util.cpp
@@ -44,16 +44,21 @@ bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) {
44 return lhs.mods == rhs.mods && lhskeys == rhskeys; 44 return lhs.mods == rhs.mods && lhskeys == rhskeys;
45} 45}
46 46
47std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& value) { 47std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& report) {
48 stream << "Keyboard report:" << std::endl; 48 auto keys = get_keys(report);
49 stream << "Mods: " << (uint32_t)value.mods << std::endl; 49
50 stream << "Keys: ";
51 // TODO: This should probably print friendly names for the keys 50 // TODO: This should probably print friendly names for the keys
52 for (uint32_t k : get_keys(value)) { 51 stream << "Keyboard Report: Mods (" << (uint32_t)report.mods << ") Keys (";
53 stream << k << " "; 52
53 for (auto key = keys.cbegin(); key != keys.cend();) {
54 stream << +(*key);
55 key++;
56 if (key != keys.cend()) {
57 stream << ",";
58 }
54 } 59 }
55 stream << std::endl; 60
56 return stream; 61 return stream << ")" << std::endl;
57} 62}
58 63
59KeyboardReportMatcher::KeyboardReportMatcher(const std::vector<uint8_t>& keys) { 64KeyboardReportMatcher::KeyboardReportMatcher(const std::vector<uint8_t>& keys) {
diff --git a/tests/test_common/keymap.c b/tests/test_common/keymap.c
new file mode 100644
index 000000000..fc3a56a00
--- /dev/null
+++ b/tests/test_common/keymap.c
@@ -0,0 +1,33 @@
1/* Copyright 2017 Fred Sundvik
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "quantum.h"
18
19// clang-format off
20
21const uint16_t PROGMEM
22 keymaps[][MATRIX_ROWS][MATRIX_COLS] =
23 {
24 [0] =
25 {
26 {KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO},
27 {KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO},
28 {KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO},
29 {KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO},
30 },
31};
32
33// clang-format on
diff --git a/tests/test_common/test_common.h b/tests/test_common/test_common.h
new file mode 100644
index 000000000..19ffcddce
--- /dev/null
+++ b/tests/test_common/test_common.h
@@ -0,0 +1,4 @@
1#pragma once
2
3#define MATRIX_ROWS 4
4#define MATRIX_COLS 10 \ No newline at end of file
diff --git a/tests/test_common/test_driver.cpp b/tests/test_common/test_driver.cpp
index 84e249d83..2fa2b6a2e 100644
--- a/tests/test_common/test_driver.cpp
+++ b/tests/test_common/test_driver.cpp
@@ -27,7 +27,10 @@ TestDriver::~TestDriver() { m_this = nullptr; }
27 27
28uint8_t TestDriver::keyboard_leds(void) { return m_this->m_leds; } 28uint8_t TestDriver::keyboard_leds(void) { return m_this->m_leds; }
29 29
30void TestDriver::send_keyboard(report_keyboard_t* report) { m_this->send_keyboard_mock(*report); } 30void TestDriver::send_keyboard(report_keyboard_t* report) {
31 test_logger.trace() << *report;
32 m_this->send_keyboard_mock(*report);
33}
31 34
32void TestDriver::send_mouse(report_mouse_t* report) { m_this->send_mouse_mock(*report); } 35void TestDriver::send_mouse(report_mouse_t* report) { m_this->send_mouse_mock(*report); }
33 36
diff --git a/tests/test_common/test_driver.hpp b/tests/test_common/test_driver.hpp
index f86308df9..f9197b363 100644
--- a/tests/test_common/test_driver.hpp
+++ b/tests/test_common/test_driver.hpp
@@ -20,25 +20,26 @@
20#include <stdint.h> 20#include <stdint.h>
21#include "host.h" 21#include "host.h"
22#include "keyboard_report_util.hpp" 22#include "keyboard_report_util.hpp"
23 23#include "test_logger.hpp"
24 24
25class TestDriver { 25class TestDriver {
26public: 26 public:
27 TestDriver(); 27 TestDriver();
28 ~TestDriver(); 28 ~TestDriver();
29 void set_leds(uint8_t leds) { m_leds = leds; } 29 void set_leds(uint8_t leds) { m_leds = leds; }
30 30
31 MOCK_METHOD1(send_keyboard_mock, void (report_keyboard_t&)); 31 MOCK_METHOD1(send_keyboard_mock, void(report_keyboard_t&));
32 MOCK_METHOD1(send_mouse_mock, void (report_mouse_t&)); 32 MOCK_METHOD1(send_mouse_mock, void(report_mouse_t&));
33 MOCK_METHOD1(send_system_mock, void (uint16_t)); 33 MOCK_METHOD1(send_system_mock, void(uint16_t));
34 MOCK_METHOD1(send_consumer_mock, void (uint16_t)); 34 MOCK_METHOD1(send_consumer_mock, void(uint16_t));
35private: 35
36 static uint8_t keyboard_leds(void); 36 private:
37 static void send_keyboard(report_keyboard_t *report); 37 static uint8_t keyboard_leds(void);
38 static void send_mouse(report_mouse_t* report); 38 static void send_keyboard(report_keyboard_t* report);
39 static void send_system(uint16_t data); 39 static void send_mouse(report_mouse_t* report);
40 static void send_consumer(uint16_t data); 40 static void send_system(uint16_t data);
41 host_driver_t m_driver; 41 static void send_consumer(uint16_t data);
42 uint8_t m_leds = 0; 42 host_driver_t m_driver;
43 uint8_t m_leds = 0;
43 static TestDriver* m_this; 44 static TestDriver* m_this;
44}; 45};
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}
diff --git a/tests/test_common/test_fixture.hpp b/tests/test_common/test_fixture.hpp
index 340503665..73b5d8d3e 100644
--- a/tests/test_common/test_fixture.hpp
+++ b/tests/test_common/test_fixture.hpp
@@ -16,15 +16,34 @@
16 16
17#pragma once 17#pragma once
18 18
19#include <cstdint>
20#include <unordered_map>
21#include <optional>
19#include "gtest/gtest.h" 22#include "gtest/gtest.h"
23#include "keyboard.h"
24#include "test_keymap_key.hpp"
20 25
21class TestFixture : public testing::Test { 26class TestFixture : public testing::Test {
22public: 27 public:
28 static TestFixture* m_this;
29
23 TestFixture(); 30 TestFixture();
24 ~TestFixture(); 31 ~TestFixture();
25 static void SetUpTestCase(); 32 static void SetUpTestCase();
26 static void TearDownTestCase(); 33 static void TearDownTestCase();
27 34
35 void set_keymap(std::initializer_list<KeymapKey> keycodes);
36 void add_key(const KeymapKey key);
37
38 const KeymapKey* find_key(const layer_t layer_t, const keypos_t position) const;
39 void get_keycode(const layer_t layer, const keypos_t position, uint16_t* result) const;
40
28 void run_one_scan_loop(); 41 void run_one_scan_loop();
29 void idle_for(unsigned ms); 42 void idle_for(unsigned ms);
43
44 void expect_layer_state(layer_t layer) const;
45
46 protected:
47 void print_test_log() const;
48 std::vector<KeymapKey> keymap;
30}; 49};
diff --git a/tests/test_common/test_keymap_key.cpp b/tests/test_common/test_keymap_key.cpp
new file mode 100644
index 000000000..878ae097b
--- /dev/null
+++ b/tests/test_common/test_keymap_key.cpp
@@ -0,0 +1,30 @@
1/* Copyright 2021 Stefan Kerkmann
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "test_keymap_key.hpp"
18#include "test_logger.hpp"
19#include "gtest/gtest-message.h"
20#include "gtest/gtest.h"
21
22void KeymapKey::press() {
23 test_logger.trace() << "Key pressed: (" << +this->position.col << "," << +this->position.row << ")" << std::endl;
24 press_key(this->position.col, this->position.row);
25}
26
27void KeymapKey::release() {
28 test_logger.trace() << "Key released: (" << +this->position.col << "," << +this->position.row << ")" << std::endl;
29 release_key(this->position.col, this->position.row);
30} \ No newline at end of file
diff --git a/tests/test_common/test_keymap_key.hpp b/tests/test_common/test_keymap_key.hpp
new file mode 100644
index 000000000..7861cb4a3
--- /dev/null
+++ b/tests/test_common/test_keymap_key.hpp
@@ -0,0 +1,46 @@
1/* Copyright 2021 Stefan Kerkmann
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#pragma once
18
19extern "C" {
20#include "keyboard.h"
21#include "test_matrix.h"
22}
23
24#include <cassert>
25
26typedef uint8_t layer_t;
27
28struct KeymapKey {
29 KeymapKey(layer_t layer, uint8_t col, uint8_t row, uint16_t keycode) : layer(layer), position({.col = col, .row = row}), code(keycode), report_code(keycode) { validate(); }
30 KeymapKey(layer_t layer, uint8_t col, uint8_t row, uint16_t keycode, uint16_t report_code) : layer(layer), position({.col = col, .row = row}), code(keycode), report_code(report_code) { validate(); }
31
32 void press();
33 void release();
34
35 const layer_t layer;
36 const keypos_t position;
37 const uint16_t code;
38 /* Sometimes the keycode does not match the code that is send in the usb report, so we provide it here. */
39 const uint16_t report_code;
40
41 private:
42 void validate() {
43 assert(position.col <= MATRIX_COLS);
44 assert(position.row <= MATRIX_ROWS);
45 }
46}; \ No newline at end of file
diff --git a/tests/test_common/test_logger.cpp b/tests/test_common/test_logger.cpp
new file mode 100644
index 000000000..959fdde5e
--- /dev/null
+++ b/tests/test_common/test_logger.cpp
@@ -0,0 +1,39 @@
1/* Copyright 2021 Stefan Kerkmann
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <iostream>
18#include "test_logger.hpp"
19
20TestLogger test_logger;
21
22TestLogger& TestLogger::info() {
23 *this << "[ INFO ] ";
24 return *this;
25}
26
27TestLogger& TestLogger::trace() {
28 *this << "[ TRACE ] ";
29 return *this;
30}
31
32TestLogger& TestLogger::error() {
33 *this << "[ ERROR ] ";
34 return *this;
35}
36
37void TestLogger::reset() { this->m_log.str(""); };
38
39void TestLogger::print_log() { std::cerr << this->m_log.str(); }
diff --git a/tests/test_common/test_logger.hpp b/tests/test_common/test_logger.hpp
new file mode 100644
index 000000000..348af7fab
--- /dev/null
+++ b/tests/test_common/test_logger.hpp
@@ -0,0 +1,35 @@
1/* Copyright 2021 Stefan Kerkmann
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#pragma once
18
19#include <ostream>
20#include <sstream>
21
22class TestLogger : public std::ostream {
23 public:
24 TestLogger() : std::ostream(&m_log){};
25 TestLogger& info();
26 TestLogger& trace();
27 TestLogger& error();
28 void print_log();
29 void reset();
30
31 private:
32 std::stringbuf m_log;
33};
34
35extern TestLogger test_logger; \ No newline at end of file