diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/basic/test.cpp | 8 | ||||
| -rw-r--r-- | tests/test_common/keyboard_report_util.cpp | 47 | ||||
| -rw-r--r-- | tests/test_common/keyboard_report_util.h | 39 | ||||
| -rw-r--r-- | tests/test_common/test_driver.cpp | 6 | ||||
| -rw-r--r-- | tests/test_common/test_driver.h | 5 |
5 files changed, 98 insertions, 7 deletions
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 @@ | |||
| 21 | #include "keyboard.h" | 21 | #include "keyboard.h" |
| 22 | #include "test_driver.h" | 22 | #include "test_driver.h" |
| 23 | #include "test_matrix.h" | 23 | #include "test_matrix.h" |
| 24 | #include "keyboard_report_util.h" | ||
| 24 | 25 | ||
| 25 | using testing::_; | 26 | using testing::_; |
| 26 | using testing::Return; | 27 | using testing::Return; |
| @@ -32,7 +33,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { | |||
| 32 | }, | 33 | }, |
| 33 | }; | 34 | }; |
| 34 | 35 | ||
| 35 | TEST(Basic, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { | 36 | TEST(KeyPress, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { |
| 36 | TestDriver driver; | 37 | TestDriver driver; |
| 37 | EXPECT_CALL(driver, send_keyboard_mock(_)); | 38 | EXPECT_CALL(driver, send_keyboard_mock(_)); |
| 38 | keyboard_init(); | 39 | keyboard_init(); |
| @@ -41,12 +42,15 @@ TEST(Basic, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { | |||
| 41 | keyboard_task(); | 42 | keyboard_task(); |
| 42 | } | 43 | } |
| 43 | 44 | ||
| 44 | TEST(Basic, SendKeyboardIsCalledWhenAKeyIsPressed) { | 45 | TEST(KeyPress, CorrectKeyIsReportedWhenPressed) { |
| 45 | TestDriver driver; | 46 | TestDriver driver; |
| 46 | EXPECT_CALL(driver, send_keyboard_mock(_)); | 47 | EXPECT_CALL(driver, send_keyboard_mock(_)); |
| 47 | keyboard_init(); | 48 | keyboard_init(); |
| 48 | press_key(0, 0); | 49 | press_key(0, 0); |
| 49 | EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); | 50 | EXPECT_CALL(driver, keyboard_leds_mock()).WillRepeatedly(Return(0)); |
| 51 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A))); | ||
| 52 | keyboard_task(); | ||
| 53 | } | ||
| 50 | EXPECT_CALL(driver, send_keyboard_mock(_)); | 54 | EXPECT_CALL(driver, send_keyboard_mock(_)); |
| 51 | keyboard_task(); | 55 | keyboard_task(); |
| 52 | } | 56 | } |
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 @@ | |||
| 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 "keyboard_report_util.h" | ||
| 18 | using namespace testing; | ||
| 19 | |||
| 20 | bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) { | ||
| 21 | return memcmp(lhs.raw, rhs.raw, sizeof(lhs.raw))==0; | ||
| 22 | } | ||
| 23 | |||
| 24 | std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& value) { | ||
| 25 | stream << "Keyboard report:" << std::endl; | ||
| 26 | stream << (uint32_t)value.keys[0] << std::endl; | ||
| 27 | return stream; | ||
| 28 | } | ||
| 29 | |||
| 30 | KeyboardReportMatcher::KeyboardReportMatcher(const std::vector<uint8_t>& keys) { | ||
| 31 | memset(m_report.raw, 0, sizeof(m_report.raw)); | ||
| 32 | for (auto k: keys) { | ||
| 33 | add_key_to_report(&m_report, k); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | bool KeyboardReportMatcher::MatchAndExplain(report_keyboard_t& report, MatchResultListener* listener) const { | ||
| 38 | return m_report == report; | ||
| 39 | } | ||
| 40 | |||
| 41 | void KeyboardReportMatcher::DescribeTo(::std::ostream* os) const { | ||
| 42 | *os << "is equal to " << m_report; | ||
| 43 | } | ||
| 44 | |||
| 45 | void KeyboardReportMatcher::DescribeNegationTo(::std::ostream* os) const { | ||
| 46 | *os << "is not equal to " << m_report; | ||
| 47 | } \ 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 @@ | |||
| 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 | #pragma once | ||
| 18 | #include "report.h" | ||
| 19 | #include <ostream> | ||
| 20 | #include "gmock/gmock.h" | ||
| 21 | |||
| 22 | bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs); | ||
| 23 | std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& value); | ||
| 24 | |||
| 25 | class KeyboardReportMatcher : public testing::MatcherInterface<report_keyboard_t&> { | ||
| 26 | public: | ||
| 27 | KeyboardReportMatcher(const std::vector<uint8_t>& keys); | ||
| 28 | virtual bool MatchAndExplain(report_keyboard_t& report, testing::MatchResultListener* listener) const override; | ||
| 29 | virtual void DescribeTo(::std::ostream* os) const override; | ||
| 30 | virtual void DescribeNegationTo(::std::ostream* os) const override; | ||
| 31 | private: | ||
| 32 | report_keyboard_t m_report; | ||
| 33 | }; | ||
| 34 | |||
| 35 | |||
| 36 | template<typename... Ts> | ||
| 37 | inline testing::Matcher<report_keyboard_t&> KeyboardReport(Ts... keys) { | ||
| 38 | return testing::MakeMatcher(new KeyboardReportMatcher(std::vector<uint8_t>({keys...}))); | ||
| 39 | } \ 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) { | |||
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | void TestDriver::send_keyboard(report_keyboard_t* report) { | 43 | void TestDriver::send_keyboard(report_keyboard_t* report) { |
| 44 | m_this->send_keyboard_mock(report); | 44 | m_this->send_keyboard_mock(*report); |
| 45 | 45 | ||
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | void TestDriver::send_mouse(report_mouse_t* report) { | 48 | void TestDriver::send_mouse(report_mouse_t* report) { |
| 49 | m_this->send_mouse_mock(report); | 49 | m_this->send_mouse_mock(*report); |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | void TestDriver::send_system(uint16_t data) { | 52 | void TestDriver::send_system(uint16_t data) { |
| @@ -54,5 +54,5 @@ void TestDriver::send_system(uint16_t data) { | |||
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | void TestDriver::send_consumer(uint16_t data) { | 56 | void TestDriver::send_consumer(uint16_t data) { |
| 57 | m_this->send_consumer_mock(data); | 57 | m_this->send_consumer(data); |
| 58 | } | 58 | } |
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 @@ | |||
| 20 | #include "gmock/gmock.h" | 20 | #include "gmock/gmock.h" |
| 21 | #include <stdint.h> | 21 | #include <stdint.h> |
| 22 | #include "host.h" | 22 | #include "host.h" |
| 23 | #include "keyboard_report_util.h" | ||
| 23 | 24 | ||
| 24 | 25 | ||
| 25 | class TestDriver { | 26 | class TestDriver { |
| @@ -27,8 +28,8 @@ public: | |||
| 27 | TestDriver(); | 28 | TestDriver(); |
| 28 | ~TestDriver(); | 29 | ~TestDriver(); |
| 29 | MOCK_METHOD0(keyboard_leds_mock, uint8_t ()); | 30 | MOCK_METHOD0(keyboard_leds_mock, uint8_t ()); |
| 30 | MOCK_METHOD1(send_keyboard_mock, void (report_keyboard_t*)); | 31 | MOCK_METHOD1(send_keyboard_mock, void (report_keyboard_t&)); |
| 31 | MOCK_METHOD1(send_mouse_mock, void (report_mouse_t*)); | 32 | MOCK_METHOD1(send_mouse_mock, void (report_mouse_t&)); |
| 32 | MOCK_METHOD1(send_system_mock, void (uint16_t)); | 33 | MOCK_METHOD1(send_system_mock, void (uint16_t)); |
| 33 | MOCK_METHOD1(send_consumer_mock, void (uint16_t)); | 34 | MOCK_METHOD1(send_consumer_mock, void (uint16_t)); |
| 34 | private: | 35 | private: |
