From 30f22d450da19c65584e6deb8515a1fa5b074e07 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Fri, 16 Jun 2017 20:21:28 +0300 Subject: Add basic infrastrucure for building full tests --- tests/basic/config.h | 22 ++++++++++++++++++++++ tests/basic/rules.mk | 14 ++++++++++++++ tests/basic/test.c | 17 +++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 tests/basic/config.h create mode 100644 tests/basic/rules.mk create mode 100644 tests/basic/test.c (limited to 'tests') diff --git a/tests/basic/config.h b/tests/basic/config.h new file mode 100644 index 000000000..c24fdcf3f --- /dev/null +++ b/tests/basic/config.h @@ -0,0 +1,22 @@ +/* Copyright 2017 Fred Sundvik + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef TESTS_BASIC_CONFIG_H_ +#define TESTS_BASIC_CONFIG_H_ + + + +#endif /* TESTS_BASIC_CONFIG_H_ */ diff --git a/tests/basic/rules.mk b/tests/basic/rules.mk new file mode 100644 index 000000000..0405b9d0f --- /dev/null +++ b/tests/basic/rules.mk @@ -0,0 +1,14 @@ +# Copyright 2017 Fred Sundvik +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . \ No newline at end of file diff --git a/tests/basic/test.c b/tests/basic/test.c new file mode 100644 index 000000000..01d1930ea --- /dev/null +++ b/tests/basic/test.c @@ -0,0 +1,17 @@ +/* Copyright 2017 Fred Sundvik + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + -- cgit v1.2.3-24-g4f1b From 585d64612811a61f8476380dac77199c04246085 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Fri, 16 Jun 2017 21:39:00 +0300 Subject: Define MATRIX_COLS and MATRIX_ROWS --- tests/basic/config.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests') diff --git a/tests/basic/config.h b/tests/basic/config.h index c24fdcf3f..c5578b83f 100644 --- a/tests/basic/config.h +++ b/tests/basic/config.h @@ -17,6 +17,8 @@ #ifndef TESTS_BASIC_CONFIG_H_ #define TESTS_BASIC_CONFIG_H_ +#define MATRIX_ROWS 1 +#define MATRIX_COLS 2 #endif /* TESTS_BASIC_CONFIG_H_ */ -- cgit v1.2.3-24-g4f1b From 1c0fe956ac242b1c85a9b024b9b3966020b862ed Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Fri, 16 Jun 2017 22:14:47 +0300 Subject: Split common.mk into common.mk and common_features.mk To support including keyboard/keymap specific rules before it --- tests/basic/rules.mk | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/basic/rules.mk b/tests/basic/rules.mk index 0405b9d0f..8a906807c 100644 --- a/tests/basic/rules.mk +++ b/tests/basic/rules.mk @@ -11,4 +11,6 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License -# along with this program. If not, see . \ No newline at end of file +# along with this program. If not, see . + +CUSTOM_MATRIX=yes \ No newline at end of file -- cgit v1.2.3-24-g4f1b From a72e8f0865e54777a152652c5d46280ceb58efbc Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Fri, 16 Jun 2017 22:30:07 +0300 Subject: Dummy test matrix implementation --- tests/test_common/matrix.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/test_common/matrix.c (limited to 'tests') diff --git a/tests/test_common/matrix.c b/tests/test_common/matrix.c new file mode 100644 index 000000000..940f28f35 --- /dev/null +++ b/tests/test_common/matrix.c @@ -0,0 +1,43 @@ +/* Copyright 2017 Fred Sundvik + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "matrix.h" + +void matrix_init(void) { + matrix_init_quantum(); +} + +uint8_t matrix_scan(void) { + matrix_scan_quantum(); + return 1; +} + +matrix_row_t matrix_get_row(uint8_t row) { + return 0; +} + +void matrix_print(void) { + +} + +void matrix_init_kb(void) { + +} + +void matrix_scan_kb(void) { + +} -- cgit v1.2.3-24-g4f1b From 617578399cf1a0fff70ceeb21d63a06524c801a3 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Fri, 16 Jun 2017 22:39:56 +0300 Subject: Add keymap --- tests/basic/config.h | 2 +- tests/basic/test.c | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/basic/config.h b/tests/basic/config.h index c5578b83f..4da8d0425 100644 --- a/tests/basic/config.h +++ b/tests/basic/config.h @@ -17,7 +17,7 @@ #ifndef TESTS_BASIC_CONFIG_H_ #define TESTS_BASIC_CONFIG_H_ -#define MATRIX_ROWS 1 +#define MATRIX_ROWS 2 #define MATRIX_COLS 2 diff --git a/tests/basic/test.c b/tests/basic/test.c index 01d1930ea..2afb4d6a9 100644 --- a/tests/basic/test.c +++ b/tests/basic/test.c @@ -14,4 +14,11 @@ * along with this program. If not, see . */ +#include "quantum.h" +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { + [0] = { + {KC_A, KC_B}, + {KC_C, KC_D} + }, +}; -- cgit v1.2.3-24-g4f1b From 3bb0586acc6472380365d78ee81a332aca468239 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Fri, 16 Jun 2017 23:50:44 +0300 Subject: Add a couple of basic tests and implement driver mock --- tests/basic/test.c | 24 ---------------- tests/basic/test.cpp | 50 +++++++++++++++++++++++++++++++++ tests/test_common/matrix.c | 13 ++++++++- tests/test_common/test_driver.cpp | 58 +++++++++++++++++++++++++++++++++++++++ tests/test_common/test_driver.h | 45 ++++++++++++++++++++++++++++++ tests/test_common/test_matrix.h | 31 +++++++++++++++++++++ 6 files changed, 196 insertions(+), 25 deletions(-) delete mode 100644 tests/basic/test.c create mode 100644 tests/basic/test.cpp create mode 100644 tests/test_common/test_driver.cpp create mode 100644 tests/test_common/test_driver.h create mode 100644 tests/test_common/test_matrix.h (limited to 'tests') diff --git a/tests/basic/test.c b/tests/basic/test.c deleted file mode 100644 index 2afb4d6a9..000000000 --- a/tests/basic/test.c +++ /dev/null @@ -1,24 +0,0 @@ -/* Copyright 2017 Fred Sundvik - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "quantum.h" - -const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { - [0] = { - {KC_A, KC_B}, - {KC_C, KC_D} - }, -}; diff --git a/tests/basic/test.cpp b/tests/basic/test.cpp new file mode 100644 index 000000000..ce9a0cd72 --- /dev/null +++ b/tests/basic/test.cpp @@ -0,0 +1,50 @@ +/* Copyright 2017 Fred Sundvik + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "gtest/gtest.h" +#include "gmock/gmock.h" + +#include "quantum.h" +#include "keyboard.h" +#include "test_driver.h" +#include "test_matrix.h" + +using testing::_; +using testing::Return; + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { + [0] = { + {KC_A, KC_B}, + {KC_C, KC_D} + }, +}; + +TEST(Basic, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { + keyboard_init(); + TestDriver driver; + EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); + EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); + keyboard_task(); +} + +TEST(Basic, SendKeyboardIsCalledWhenAKeyIsPressed) { + keyboard_init(); + TestDriver driver; + press_key(0, 0); + EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); + EXPECT_CALL(driver, send_keyboard_mock(_)); + keyboard_task(); +} diff --git a/tests/test_common/matrix.c b/tests/test_common/matrix.c index 940f28f35..85556e2c4 100644 --- a/tests/test_common/matrix.c +++ b/tests/test_common/matrix.c @@ -16,6 +16,9 @@ #include "matrix.h" +#include "test_matrix.h" + +static matrix_row_t matrix[MATRIX_ROWS] = {}; void matrix_init(void) { matrix_init_quantum(); @@ -27,7 +30,7 @@ uint8_t matrix_scan(void) { } matrix_row_t matrix_get_row(uint8_t row) { - return 0; + return matrix[row]; } void matrix_print(void) { @@ -41,3 +44,11 @@ void matrix_init_kb(void) { void matrix_scan_kb(void) { } + +void press_key(uint8_t col, uint8_t row) { + matrix[row] |= 1 << col; +} + +void release_key(uint8_t col, uint8_t row) { + matrix[row] &= ~(1 << col); +} diff --git a/tests/test_common/test_driver.cpp b/tests/test_common/test_driver.cpp new file mode 100644 index 000000000..7c67f5776 --- /dev/null +++ b/tests/test_common/test_driver.cpp @@ -0,0 +1,58 @@ +/* Copyright 2017 Fred Sundvik + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "test_driver.h" + +TestDriver* TestDriver::m_this = nullptr; + +TestDriver::TestDriver() + : m_driver{ + &TestDriver::keyboard_leds, + &TestDriver::send_keyboard, + &TestDriver::send_mouse, + &TestDriver::send_system, + &TestDriver::send_consumer + } +{ + + host_set_driver(&m_driver); + m_this = this; +} + +TestDriver::~TestDriver() { + m_this = nullptr; +} + +uint8_t TestDriver::keyboard_leds(void) { + return m_this->keyboard_leds_mock(); +} + +void TestDriver::send_keyboard(report_keyboard_t* report) { + m_this->send_keyboard_mock(report); + +} + +void TestDriver::send_mouse(report_mouse_t* report) { + m_this->send_mouse_mock(report); +} + +void TestDriver::send_system(uint16_t data) { + m_this->send_system_mock(data); +} + +void TestDriver::send_consumer(uint16_t data) { + m_this->send_consumer_mock(data); +} diff --git a/tests/test_common/test_driver.h b/tests/test_common/test_driver.h new file mode 100644 index 000000000..d5b831884 --- /dev/null +++ b/tests/test_common/test_driver.h @@ -0,0 +1,45 @@ +/* Copyright 2017 Fred Sundvik + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef TESTS_TEST_COMMON_TEST_DRIVER_H_ +#define TESTS_TEST_COMMON_TEST_DRIVER_H_ + +#include "gmock/gmock.h" +#include +#include "host.h" + + +class TestDriver { +public: + TestDriver(); + ~TestDriver(); + MOCK_METHOD0(keyboard_leds_mock, uint8_t ()); + MOCK_METHOD1(send_keyboard_mock, void (report_keyboard_t*)); + MOCK_METHOD1(send_mouse_mock, void (report_mouse_t*)); + MOCK_METHOD1(send_system_mock, void (uint16_t)); + MOCK_METHOD1(send_consumer_mock, void (uint16_t)); +private: + static uint8_t keyboard_leds(void); + static void send_keyboard(report_keyboard_t *report); + static void send_mouse(report_mouse_t* report); + static void send_system(uint16_t data); + static void send_consumer(uint16_t data); + host_driver_t m_driver; + static TestDriver* m_this; +}; + + +#endif /* TESTS_TEST_COMMON_TEST_DRIVER_H_ */ diff --git a/tests/test_common/test_matrix.h b/tests/test_common/test_matrix.h new file mode 100644 index 000000000..afc65c556 --- /dev/null +++ b/tests/test_common/test_matrix.h @@ -0,0 +1,31 @@ +/* Copyright 2017 Fred Sundvik + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef TESTS_TEST_COMMON_TEST_MATRIX_H_ +#define TESTS_TEST_COMMON_TEST_MATRIX_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +void press_key(uint8_t col, uint8_t row); +void release_key(uint8_t col, uint8_t row); + +#ifdef __cplusplus +} +#endif + +#endif /* TESTS_TEST_COMMON_TEST_MATRIX_H_ */ -- cgit v1.2.3-24-g4f1b From c38589d8615ba3ff93736cb02d12b3c8ceb68094 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 17 Jun 2017 15:18:30 +0300 Subject: Fix the order of init and driver creation --- tests/basic/test.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/basic/test.cpp b/tests/basic/test.cpp index ce9a0cd72..e3190085d 100644 --- a/tests/basic/test.cpp +++ b/tests/basic/test.cpp @@ -33,16 +33,18 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { }; TEST(Basic, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { - keyboard_init(); TestDriver driver; + EXPECT_CALL(driver, send_keyboard_mock(_)); + keyboard_init(); EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); keyboard_task(); } TEST(Basic, SendKeyboardIsCalledWhenAKeyIsPressed) { - keyboard_init(); TestDriver driver; + EXPECT_CALL(driver, send_keyboard_mock(_)); + keyboard_init(); press_key(0, 0); EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); EXPECT_CALL(driver, send_keyboard_mock(_)); -- cgit v1.2.3-24-g4f1b From bd1729d5be376fecae67ab5ddc92eab82416a5c1 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 17 Jun 2017 18:18:15 +0300 Subject: Actually test for correct key presses --- tests/basic/test.cpp | 8 +++-- tests/test_common/keyboard_report_util.cpp | 47 ++++++++++++++++++++++++++++++ tests/test_common/keyboard_report_util.h | 39 +++++++++++++++++++++++++ tests/test_common/test_driver.cpp | 6 ++-- tests/test_common/test_driver.h | 5 ++-- 5 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 tests/test_common/keyboard_report_util.cpp create mode 100644 tests/test_common/keyboard_report_util.h (limited to 'tests') diff --git a/tests/basic/test.cpp b/tests/basic/test.cpp index e3190085d..804642eae 100644 --- a/tests/basic/test.cpp +++ b/tests/basic/test.cpp @@ -21,6 +21,7 @@ #include "keyboard.h" #include "test_driver.h" #include "test_matrix.h" +#include "keyboard_report_util.h" using testing::_; using testing::Return; @@ -32,7 +33,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { }, }; -TEST(Basic, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { +TEST(KeyPress, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { TestDriver driver; EXPECT_CALL(driver, send_keyboard_mock(_)); keyboard_init(); @@ -41,12 +42,15 @@ TEST(Basic, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { keyboard_task(); } -TEST(Basic, SendKeyboardIsCalledWhenAKeyIsPressed) { +TEST(KeyPress, CorrectKeyIsReportedWhenPressed) { TestDriver driver; EXPECT_CALL(driver, send_keyboard_mock(_)); keyboard_init(); press_key(0, 0); EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); + EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A))); + keyboard_task(); +} EXPECT_CALL(driver, send_keyboard_mock(_)); keyboard_task(); } diff --git a/tests/test_common/keyboard_report_util.cpp b/tests/test_common/keyboard_report_util.cpp new file mode 100644 index 000000000..70fc1c048 --- /dev/null +++ b/tests/test_common/keyboard_report_util.cpp @@ -0,0 +1,47 @@ +/* Copyright 2017 Fred Sundvik + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + #include "keyboard_report_util.h" + using namespace testing; + +bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) { + return memcmp(lhs.raw, rhs.raw, sizeof(lhs.raw))==0; +} + +std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& value) { + stream << "Keyboard report:" << std::endl; + stream << (uint32_t)value.keys[0] << std::endl; + return stream; +} + +KeyboardReportMatcher::KeyboardReportMatcher(const std::vector& keys) { + memset(m_report.raw, 0, sizeof(m_report.raw)); + for (auto k: keys) { + add_key_to_report(&m_report, k); + } +} + +bool KeyboardReportMatcher::MatchAndExplain(report_keyboard_t& report, MatchResultListener* listener) const { + return m_report == report; +} + +void KeyboardReportMatcher::DescribeTo(::std::ostream* os) const { + *os << "is equal to " << m_report; +} + +void KeyboardReportMatcher::DescribeNegationTo(::std::ostream* os) const { + *os << "is not equal to " << m_report; +} \ No newline at end of file diff --git a/tests/test_common/keyboard_report_util.h b/tests/test_common/keyboard_report_util.h new file mode 100644 index 000000000..48543c205 --- /dev/null +++ b/tests/test_common/keyboard_report_util.h @@ -0,0 +1,39 @@ +/* Copyright 2017 Fred Sundvik + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include "report.h" +#include +#include "gmock/gmock.h" + +bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs); +std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& value); + +class KeyboardReportMatcher : public testing::MatcherInterface { + public: + KeyboardReportMatcher(const std::vector& keys); + virtual bool MatchAndExplain(report_keyboard_t& report, testing::MatchResultListener* listener) const override; + virtual void DescribeTo(::std::ostream* os) const override; + virtual void DescribeNegationTo(::std::ostream* os) const override; +private: + report_keyboard_t m_report; +}; + + +template +inline testing::Matcher KeyboardReport(Ts... keys) { + return testing::MakeMatcher(new KeyboardReportMatcher(std::vector({keys...}))); +} \ No newline at end of file diff --git a/tests/test_common/test_driver.cpp b/tests/test_common/test_driver.cpp index 7c67f5776..9e618aa97 100644 --- a/tests/test_common/test_driver.cpp +++ b/tests/test_common/test_driver.cpp @@ -41,12 +41,12 @@ uint8_t TestDriver::keyboard_leds(void) { } void TestDriver::send_keyboard(report_keyboard_t* report) { - m_this->send_keyboard_mock(report); + m_this->send_keyboard_mock(*report); } void TestDriver::send_mouse(report_mouse_t* report) { - m_this->send_mouse_mock(report); + m_this->send_mouse_mock(*report); } void TestDriver::send_system(uint16_t data) { @@ -54,5 +54,5 @@ void TestDriver::send_system(uint16_t data) { } void TestDriver::send_consumer(uint16_t data) { - m_this->send_consumer_mock(data); + m_this->send_consumer(data); } diff --git a/tests/test_common/test_driver.h b/tests/test_common/test_driver.h index d5b831884..b1b95fbcc 100644 --- a/tests/test_common/test_driver.h +++ b/tests/test_common/test_driver.h @@ -20,6 +20,7 @@ #include "gmock/gmock.h" #include #include "host.h" +#include "keyboard_report_util.h" class TestDriver { @@ -27,8 +28,8 @@ public: TestDriver(); ~TestDriver(); MOCK_METHOD0(keyboard_leds_mock, uint8_t ()); - MOCK_METHOD1(send_keyboard_mock, void (report_keyboard_t*)); - MOCK_METHOD1(send_mouse_mock, void (report_mouse_t*)); + MOCK_METHOD1(send_keyboard_mock, void (report_keyboard_t&)); + MOCK_METHOD1(send_mouse_mock, void (report_mouse_t&)); MOCK_METHOD1(send_system_mock, void (uint16_t)); MOCK_METHOD1(send_consumer_mock, void (uint16_t)); private: -- cgit v1.2.3-24-g4f1b From e85b1857968d4c0378b9778650c30b9d2bca3ea9 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 18 Jun 2017 14:40:22 +0300 Subject: Test two keys pressed at once --- tests/basic/test.cpp | 16 +++++++++++++++- tests/test_common/keyboard_report_util.cpp | 30 ++++++++++++++++++++++++++++-- tests/test_common/matrix.c | 2 ++ 3 files changed, 45 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/basic/test.cpp b/tests/basic/test.cpp index 804642eae..398063fca 100644 --- a/tests/basic/test.cpp +++ b/tests/basic/test.cpp @@ -51,6 +51,20 @@ TEST(KeyPress, CorrectKeyIsReportedWhenPressed) { EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A))); keyboard_task(); } - EXPECT_CALL(driver, send_keyboard_mock(_)); + +TEST(KeyPress, CorrectKeysAreReportedWhenTwoKeysArePressed) { + TestDriver driver; + EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + keyboard_init(); + press_key(1, 0); + press_key(0, 1); + EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); + //TODO: This is a left-over from the previous test and need to be fixed + EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); + keyboard_task(); + //Note that QMK only processes one key at a time + EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_B))); + keyboard_task(); + EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_B, KC_C))); keyboard_task(); } diff --git a/tests/test_common/keyboard_report_util.cpp b/tests/test_common/keyboard_report_util.cpp index 70fc1c048..34e53cd4c 100644 --- a/tests/test_common/keyboard_report_util.cpp +++ b/tests/test_common/keyboard_report_util.cpp @@ -15,15 +15,41 @@ */ #include "keyboard_report_util.h" + #include + #include using namespace testing; + namespace + { + std::vector get_keys(const report_keyboard_t& report) { + std::vector result; + #if defined(NKRO_ENABLE) + #error NKRO support not implemented yet + #elif defined(USB_6KRO_ENABLE) + #error 6KRO support not implemented yet + #else + for(size_t i=0; i static matrix_row_t matrix[MATRIX_ROWS] = {}; void matrix_init(void) { + memset(matrix, 0, sizeof(matrix)); matrix_init_quantum(); } -- cgit v1.2.3-24-g4f1b From 36f820be7e80bc8f1f5489373708356cf142c269 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 18 Jun 2017 23:49:38 +0300 Subject: Add a test fixture It only initializes QMK once, and clears the matrix after each test. --- tests/basic/test.cpp | 19 ++++++------------- tests/test_common/matrix.c | 6 +++++- tests/test_common/test_fixture.cpp | 38 ++++++++++++++++++++++++++++++++++++++ tests/test_common/test_fixture.h | 28 ++++++++++++++++++++++++++++ tests/test_common/test_matrix.h | 1 + 5 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 tests/test_common/test_fixture.cpp create mode 100644 tests/test_common/test_fixture.h (limited to 'tests') diff --git a/tests/basic/test.cpp b/tests/basic/test.cpp index 398063fca..26a09585c 100644 --- a/tests/basic/test.cpp +++ b/tests/basic/test.cpp @@ -18,10 +18,10 @@ #include "gmock/gmock.h" #include "quantum.h" -#include "keyboard.h" #include "test_driver.h" #include "test_matrix.h" #include "keyboard_report_util.h" +#include "test_fixture.h" using testing::_; using testing::Return; @@ -33,35 +33,28 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { }, }; -TEST(KeyPress, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { +class KeyPress : public TestFixture {}; + +TEST_F(KeyPress, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { TestDriver driver; - EXPECT_CALL(driver, send_keyboard_mock(_)); - keyboard_init(); EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); keyboard_task(); } -TEST(KeyPress, CorrectKeyIsReportedWhenPressed) { +TEST_F(KeyPress, CorrectKeyIsReportedWhenPressed) { TestDriver driver; - EXPECT_CALL(driver, send_keyboard_mock(_)); - keyboard_init(); press_key(0, 0); EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A))); keyboard_task(); } -TEST(KeyPress, CorrectKeysAreReportedWhenTwoKeysArePressed) { +TEST_F(KeyPress, CorrectKeysAreReportedWhenTwoKeysArePressed) { TestDriver driver; - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); - keyboard_init(); press_key(1, 0); press_key(0, 1); EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); - //TODO: This is a left-over from the previous test and need to be fixed - EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); - keyboard_task(); //Note that QMK only processes one key at a time EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_B))); keyboard_task(); diff --git a/tests/test_common/matrix.c b/tests/test_common/matrix.c index 5ab5bac6c..0d9fa68b0 100644 --- a/tests/test_common/matrix.c +++ b/tests/test_common/matrix.c @@ -22,7 +22,7 @@ static matrix_row_t matrix[MATRIX_ROWS] = {}; void matrix_init(void) { - memset(matrix, 0, sizeof(matrix)); + clear_all_keys(); matrix_init_quantum(); } @@ -54,3 +54,7 @@ void press_key(uint8_t col, uint8_t row) { void release_key(uint8_t col, uint8_t row) { matrix[row] &= ~(1 << col); } + +void clear_all_keys(void) { + memset(matrix, 0, sizeof(matrix)); +} diff --git a/tests/test_common/test_fixture.cpp b/tests/test_common/test_fixture.cpp new file mode 100644 index 000000000..aff518d21 --- /dev/null +++ b/tests/test_common/test_fixture.cpp @@ -0,0 +1,38 @@ +#include "test_fixture.h" +#include "gmock/gmock.h" +#include "test_driver.h" +#include "test_matrix.h" +#include "keyboard.h" + +using testing::_; +using testing::AnyNumber; +using testing::Return; +using testing::Between; + +void TestFixture::SetUpTestCase() { + TestDriver driver; + EXPECT_CALL(driver, send_keyboard_mock(_)); + keyboard_init(); +} + +void TestFixture::TearDownTestCase() { +} + +TestFixture::TestFixture() { +} + +TestFixture::~TestFixture() { + TestDriver driver; + clear_all_keys(); + // Run for a while to make sure all keys are completely released + // Should probably wait until tapping term etc, has timed out + EXPECT_CALL(driver, send_keyboard_mock(_)).Times(AnyNumber()); + EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); + for (int i=0; i<100; i++) { + keyboard_task(); + } + testing::Mock::VerifyAndClearExpectations(&driver); + // Verify that the matrix really is cleared + EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(Between(0, 1)); + EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); +} \ No newline at end of file diff --git a/tests/test_common/test_fixture.h b/tests/test_common/test_fixture.h new file mode 100644 index 000000000..a775a425a --- /dev/null +++ b/tests/test_common/test_fixture.h @@ -0,0 +1,28 @@ +/* Copyright 2017 Fred Sundvik + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + #pragma once + +#include "gtest/gtest.h" + +class TestFixture : public testing::Test { +public: + TestFixture(); + ~TestFixture(); + static void SetUpTestCase(); + static void TearDownTestCase(); + +}; \ No newline at end of file diff --git a/tests/test_common/test_matrix.h b/tests/test_common/test_matrix.h index afc65c556..174fc4f22 100644 --- a/tests/test_common/test_matrix.h +++ b/tests/test_common/test_matrix.h @@ -23,6 +23,7 @@ extern "C" { void press_key(uint8_t col, uint8_t row); void release_key(uint8_t col, uint8_t row); +void clear_all_keys(void); #ifdef __cplusplus } -- cgit v1.2.3-24-g4f1b From 43d8fa5bf1248ce5c1ce5f9cb0d238d794b4475d Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Mon, 19 Jun 2017 00:19:09 +0300 Subject: More natural interface for setting keyboard leds --- tests/basic/test.cpp | 3 --- tests/test_common/test_driver.cpp | 3 +-- tests/test_common/test_driver.h | 4 +++- tests/test_common/test_fixture.cpp | 2 -- 4 files changed, 4 insertions(+), 8 deletions(-) (limited to 'tests') diff --git a/tests/basic/test.cpp b/tests/basic/test.cpp index 26a09585c..1bd5c2762 100644 --- a/tests/basic/test.cpp +++ b/tests/basic/test.cpp @@ -37,7 +37,6 @@ class KeyPress : public TestFixture {}; TEST_F(KeyPress, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { TestDriver driver; - EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); keyboard_task(); } @@ -45,7 +44,6 @@ TEST_F(KeyPress, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { TEST_F(KeyPress, CorrectKeyIsReportedWhenPressed) { TestDriver driver; press_key(0, 0); - EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A))); keyboard_task(); } @@ -54,7 +52,6 @@ TEST_F(KeyPress, CorrectKeysAreReportedWhenTwoKeysArePressed) { TestDriver driver; press_key(1, 0); press_key(0, 1); - EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); //Note that QMK only processes one key at a time EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_B))); keyboard_task(); diff --git a/tests/test_common/test_driver.cpp b/tests/test_common/test_driver.cpp index 9e618aa97..feb80563a 100644 --- a/tests/test_common/test_driver.cpp +++ b/tests/test_common/test_driver.cpp @@ -27,7 +27,6 @@ TestDriver::TestDriver() &TestDriver::send_consumer } { - host_set_driver(&m_driver); m_this = this; } @@ -37,7 +36,7 @@ TestDriver::~TestDriver() { } uint8_t TestDriver::keyboard_leds(void) { - return m_this->keyboard_leds_mock(); + return m_this->m_leds; } void TestDriver::send_keyboard(report_keyboard_t* report) { diff --git a/tests/test_common/test_driver.h b/tests/test_common/test_driver.h index b1b95fbcc..0123fd539 100644 --- a/tests/test_common/test_driver.h +++ b/tests/test_common/test_driver.h @@ -27,7 +27,8 @@ class TestDriver { public: TestDriver(); ~TestDriver(); - MOCK_METHOD0(keyboard_leds_mock, uint8_t ()); + void set_leds(uint8_t leds) { m_leds = leds; } + MOCK_METHOD1(send_keyboard_mock, void (report_keyboard_t&)); MOCK_METHOD1(send_mouse_mock, void (report_mouse_t&)); MOCK_METHOD1(send_system_mock, void (uint16_t)); @@ -39,6 +40,7 @@ private: static void send_system(uint16_t data); static void send_consumer(uint16_t data); host_driver_t m_driver; + uint8_t m_leds = 0; static TestDriver* m_this; }; diff --git a/tests/test_common/test_fixture.cpp b/tests/test_common/test_fixture.cpp index aff518d21..eef9b854b 100644 --- a/tests/test_common/test_fixture.cpp +++ b/tests/test_common/test_fixture.cpp @@ -27,12 +27,10 @@ TestFixture::~TestFixture() { // Run for a while to make sure all keys are completely released // Should probably wait until tapping term etc, has timed out EXPECT_CALL(driver, send_keyboard_mock(_)).Times(AnyNumber()); - EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); for (int i=0; i<100; i++) { keyboard_task(); } testing::Mock::VerifyAndClearExpectations(&driver); // Verify that the matrix really is cleared EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(Between(0, 1)); - EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From fb9b8dadb97e69f2c206a96e879a9350b535fe98 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Mon, 19 Jun 2017 00:22:12 +0300 Subject: Add a couple of todos to keyboard_report_util.cpp Also print the modifier state --- tests/test_common/keyboard_report_util.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'tests') diff --git a/tests/test_common/keyboard_report_util.cpp b/tests/test_common/keyboard_report_util.cpp index 34e53cd4c..aca4433dd 100644 --- a/tests/test_common/keyboard_report_util.cpp +++ b/tests/test_common/keyboard_report_util.cpp @@ -47,6 +47,8 @@ bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) { std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& value) { stream << "Keyboard report:" << std::endl; + stream << "Mods: " << value.mods << std::endl; + // TODO: This should probably print friendly names for the keys for (uint32_t k: get_keys(value)) { stream << k << std::endl; } @@ -54,6 +56,7 @@ std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& value) { } KeyboardReportMatcher::KeyboardReportMatcher(const std::vector& keys) { + // TODO: Support modifiers memset(m_report.raw, 0, sizeof(m_report.raw)); for (auto k: keys) { add_key_to_report(&m_report, k); -- cgit v1.2.3-24-g4f1b