]> git.donarmstrong.com Git - qmk_firmware.git/commitdiff
Actually test for correct key presses
authorFred Sundvik <fsundvik@gmail.com>
Sat, 17 Jun 2017 15:18:15 +0000 (18:18 +0300)
committerFred Sundvik <fsundvik@gmail.com>
Sun, 18 Jun 2017 18:22:22 +0000 (21:22 +0300)
build_full_test.mk
tests/basic/test.cpp
tests/test_common/keyboard_report_util.cpp [new file with mode: 0644]
tests/test_common/keyboard_report_util.h [new file with mode: 0644]
tests/test_common/test_driver.cpp
tests/test_common/test_driver.h

index 5f9bbe5e649626e81aa228f53b28a357720a3891..67c1ca5e585c8aa694b3919d1954f588ee4b6e29 100644 (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
index e3190085d6309bab47057a6d171325b616ced142..804642eae6b831a3b667acddb6c58d9c7ab4e236 100644 (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();
 }
diff --git a/tests/test_common/keyboard_report_util.cpp b/tests/test_common/keyboard_report_util.cpp
new file mode 100644 (file)
index 0000000..70fc1c0
--- /dev/null
@@ -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 (file)
index 0000000..48543c2
--- /dev/null
@@ -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
index 7c67f5776387b73890b33cca839b496c92cc1ed5..9e618aa978260552522a994db9e07707febaef17 100644 (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);
 }
index d5b83188470a9618597b27a70a658a331c9dcda7..b1b95fbccbba4eff163d7a2e4b44adfc30b5f6bc 100644 (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: