From bd1729d5be376fecae67ab5ddc92eab82416a5c1 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 17 Jun 2017 18:18:15 +0300 Subject: [PATCH] Actually test for correct key presses --- build_full_test.mk | 3 +- 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 ++- 6 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 tests/test_common/keyboard_report_util.cpp create mode 100644 tests/test_common/keyboard_report_util.h diff --git a/build_full_test.mk b/build_full_test.mk index 5f9bbe5e64..67c1ca5e58 100644 --- a/build_full_test.mk +++ b/build_full_test.mk @@ -22,7 +22,8 @@ $(TEST)_SRC= \ $(TMK_COMMON_SRC) \ $(QUANTUM_SRC) \ tests/test_common/matrix.c \ - tests/test_common/test_driver.cpp + tests/test_common/test_driver.cpp \ + tests/test_common/keyboard_report_util.cpp $(TEST)_DEFS=$(TMK_COMMON_DEFS) $(TEST)_CONFIG=$(TEST_PATH)/config.h VPATH+=$(TOP_DIR)/tests/test_common diff --git a/tests/basic/test.cpp b/tests/basic/test.cpp index e3190085d6..804642eae6 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 0000000000..70fc1c0489 --- /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 0000000000..48543c2053 --- /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 7c67f57763..9e618aa978 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 d5b8318847..b1b95fbccb 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: