summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFred Sundvik <fsundvik@gmail.com>2017-06-17 17:18:15 +0200
committerFred Sundvik <fsundvik@gmail.com>2017-06-18 20:22:22 +0200
commitbd1729d5be376fecae67ab5ddc92eab82416a5c1 (patch)
treedb71a57159556156b282292b5c76fa257a4241e2
parentfb95d86b39074b581455fb35a17477cce79a3738 (diff)
downloadqmk_firmware-bd1729d5be376fecae67ab5ddc92eab82416a5c1.tar.gz
qmk_firmware-bd1729d5be376fecae67ab5ddc92eab82416a5c1.tar.xz
Actually test for correct key presses
-rw-r--r--build_full_test.mk3
-rw-r--r--tests/basic/test.cpp8
-rw-r--r--tests/test_common/keyboard_report_util.cpp47
-rw-r--r--tests/test_common/keyboard_report_util.h39
-rw-r--r--tests/test_common/test_driver.cpp6
-rw-r--r--tests/test_common/test_driver.h5
6 files changed, 100 insertions, 8 deletions
diff --git a/build_full_test.mk b/build_full_test.mk
index 5f9bbe5e6..67c1ca5e5 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 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 <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;
+} \ 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 <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...})));
+} \ 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 <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: