aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_common
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_common')
-rw-r--r--tests/test_common/keyboard_report_util.cpp76
-rw-r--r--tests/test_common/keyboard_report_util.h39
-rw-r--r--tests/test_common/matrix.c60
-rw-r--r--tests/test_common/test_driver.cpp57
-rw-r--r--tests/test_common/test_driver.h48
-rw-r--r--tests/test_common/test_fixture.cpp36
-rw-r--r--tests/test_common/test_fixture.h28
-rw-r--r--tests/test_common/test_matrix.h32
8 files changed, 376 insertions, 0 deletions
diff --git a/tests/test_common/keyboard_report_util.cpp b/tests/test_common/keyboard_report_util.cpp
new file mode 100644
index 000000000..aca4433dd
--- /dev/null
+++ b/tests/test_common/keyboard_report_util.cpp
@@ -0,0 +1,76 @@
+/* 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"
+ #include <vector>
+ #include <algorithm>
+ using namespace testing;
+
+ namespace
+ {
+ std::vector<uint8_t> get_keys(const report_keyboard_t& report) {
+ std::vector<uint8_t> result;
+ #if defined(NKRO_ENABLE)
+ #error NKRO support not implemented yet
+ #elif defined(USB_6KRO_ENABLE)
+ #error 6KRO support not implemented yet
+ #else
+ for(size_t i=0; i<KEYBOARD_REPORT_KEYS; i++) {
+ if (report.keys[i]) {
+ result.emplace_back(report.keys[i]);
+ }
+ }
+ #endif
+ std::sort(result.begin(), result.end());
+ return result;
+ }
+ }
+
+bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) {
+ auto lhskeys = get_keys(lhs);
+ auto rhskeys = get_keys(rhs);
+ return lhs.mods == rhs.mods && lhskeys == rhskeys;
+}
+
+std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& value) {
+ stream << "Keyboard report:" << std::endl;
+ stream << "Mods: " << value.mods << std::endl;
+ // TODO: This should probably print friendly names for the keys
+ for (uint32_t k: get_keys(value)) {
+ stream << k << std::endl;
+ }
+ return stream;
+}
+
+KeyboardReportMatcher::KeyboardReportMatcher(const std::vector<uint8_t>& keys) {
+ // TODO: Support modifiers
+ 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/matrix.c b/tests/test_common/matrix.c
new file mode 100644
index 000000000..0d9fa68b0
--- /dev/null
+++ b/tests/test_common/matrix.c
@@ -0,0 +1,60 @@
+/* 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 "matrix.h"
+#include "test_matrix.h"
+#include <string.h>
+
+static matrix_row_t matrix[MATRIX_ROWS] = {};
+
+void matrix_init(void) {
+ clear_all_keys();
+ matrix_init_quantum();
+}
+
+uint8_t matrix_scan(void) {
+ matrix_scan_quantum();
+ return 1;
+}
+
+matrix_row_t matrix_get_row(uint8_t row) {
+ return matrix[row];
+}
+
+void matrix_print(void) {
+
+}
+
+void matrix_init_kb(void) {
+
+}
+
+void matrix_scan_kb(void) {
+
+}
+
+void press_key(uint8_t col, uint8_t row) {
+ matrix[row] |= 1 << col;
+}
+
+void release_key(uint8_t col, uint8_t row) {
+ matrix[row] &= ~(1 << col);
+}
+
+void clear_all_keys(void) {
+ memset(matrix, 0, sizeof(matrix));
+}
diff --git a/tests/test_common/test_driver.cpp b/tests/test_common/test_driver.cpp
new file mode 100644
index 000000000..feb80563a
--- /dev/null
+++ b/tests/test_common/test_driver.cpp
@@ -0,0 +1,57 @@
+/* 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 "test_driver.h"
+
+TestDriver* TestDriver::m_this = nullptr;
+
+TestDriver::TestDriver()
+ : m_driver{
+ &TestDriver::keyboard_leds,
+ &TestDriver::send_keyboard,
+ &TestDriver::send_mouse,
+ &TestDriver::send_system,
+ &TestDriver::send_consumer
+ }
+{
+ host_set_driver(&m_driver);
+ m_this = this;
+}
+
+TestDriver::~TestDriver() {
+ m_this = nullptr;
+}
+
+uint8_t TestDriver::keyboard_leds(void) {
+ return m_this->m_leds;
+}
+
+void TestDriver::send_keyboard(report_keyboard_t* report) {
+ m_this->send_keyboard_mock(*report);
+
+}
+
+void TestDriver::send_mouse(report_mouse_t* report) {
+ m_this->send_mouse_mock(*report);
+}
+
+void TestDriver::send_system(uint16_t data) {
+ m_this->send_system_mock(data);
+}
+
+void TestDriver::send_consumer(uint16_t data) {
+ m_this->send_consumer(data);
+}
diff --git a/tests/test_common/test_driver.h b/tests/test_common/test_driver.h
new file mode 100644
index 000000000..0123fd539
--- /dev/null
+++ b/tests/test_common/test_driver.h
@@ -0,0 +1,48 @@
+/* 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/>.
+ */
+
+#ifndef TESTS_TEST_COMMON_TEST_DRIVER_H_
+#define TESTS_TEST_COMMON_TEST_DRIVER_H_
+
+#include "gmock/gmock.h"
+#include <stdint.h>
+#include "host.h"
+#include "keyboard_report_util.h"
+
+
+class TestDriver {
+public:
+ TestDriver();
+ ~TestDriver();
+ void set_leds(uint8_t leds) { m_leds = leds; }
+
+ 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:
+ static uint8_t keyboard_leds(void);
+ static void send_keyboard(report_keyboard_t *report);
+ static void send_mouse(report_mouse_t* report);
+ static void send_system(uint16_t data);
+ static void send_consumer(uint16_t data);
+ host_driver_t m_driver;
+ uint8_t m_leds = 0;
+ static TestDriver* m_this;
+};
+
+
+#endif /* TESTS_TEST_COMMON_TEST_DRIVER_H_ */
diff --git a/tests/test_common/test_fixture.cpp b/tests/test_common/test_fixture.cpp
new file mode 100644
index 000000000..eef9b854b
--- /dev/null
+++ b/tests/test_common/test_fixture.cpp
@@ -0,0 +1,36 @@
+#include "test_fixture.h"
+#include "gmock/gmock.h"
+#include "test_driver.h"
+#include "test_matrix.h"
+#include "keyboard.h"
+
+using testing::_;
+using testing::AnyNumber;
+using testing::Return;
+using testing::Between;
+
+void TestFixture::SetUpTestCase() {
+ TestDriver driver;
+ EXPECT_CALL(driver, send_keyboard_mock(_));
+ keyboard_init();
+}
+
+void TestFixture::TearDownTestCase() {
+}
+
+TestFixture::TestFixture() {
+}
+
+TestFixture::~TestFixture() {
+ TestDriver driver;
+ clear_all_keys();
+ // Run for a while to make sure all keys are completely released
+ // Should probably wait until tapping term etc, has timed out
+ EXPECT_CALL(driver, send_keyboard_mock(_)).Times(AnyNumber());
+ for (int i=0; i<100; i++) {
+ keyboard_task();
+ }
+ testing::Mock::VerifyAndClearExpectations(&driver);
+ // Verify that the matrix really is cleared
+ EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(Between(0, 1));
+} \ No newline at end of file
diff --git a/tests/test_common/test_fixture.h b/tests/test_common/test_fixture.h
new file mode 100644
index 000000000..a775a425a
--- /dev/null
+++ b/tests/test_common/test_fixture.h
@@ -0,0 +1,28 @@
+/* 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 "gtest/gtest.h"
+
+class TestFixture : public testing::Test {
+public:
+ TestFixture();
+ ~TestFixture();
+ static void SetUpTestCase();
+ static void TearDownTestCase();
+
+}; \ No newline at end of file
diff --git a/tests/test_common/test_matrix.h b/tests/test_common/test_matrix.h
new file mode 100644
index 000000000..174fc4f22
--- /dev/null
+++ b/tests/test_common/test_matrix.h
@@ -0,0 +1,32 @@
+/* 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/>.
+ */
+
+#ifndef TESTS_TEST_COMMON_TEST_MATRIX_H_
+#define TESTS_TEST_COMMON_TEST_MATRIX_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void press_key(uint8_t col, uint8_t row);
+void release_key(uint8_t col, uint8_t row);
+void clear_all_keys(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* TESTS_TEST_COMMON_TEST_MATRIX_H_ */