Actually test for correct key presses

daktil_manuform
Fred Sundvik 2017-06-17 18:18:15 +03:00
parent fb95d86b39
commit bd1729d5be
6 changed files with 100 additions and 8 deletions

View File

@ -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

View File

@ -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();
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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<uint8_t>& 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;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "report.h"
#include <ostream>
#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<report_keyboard_t&> {
public:
KeyboardReportMatcher(const std::vector<uint8_t>& 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<typename... Ts>
inline testing::Matcher<report_keyboard_t&> KeyboardReport(Ts... keys) {
return testing::MakeMatcher(new KeyboardReportMatcher(std::vector<uint8_t>({keys...})));
}

View File

@ -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);
}

View File

@ -20,6 +20,7 @@
#include "gmock/gmock.h"
#include <stdint.h>
#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: