aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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= \
22 $(TMK_COMMON_SRC) \ 22 $(TMK_COMMON_SRC) \
23 $(QUANTUM_SRC) \ 23 $(QUANTUM_SRC) \
24 tests/test_common/matrix.c \ 24 tests/test_common/matrix.c \
25 tests/test_common/test_driver.cpp 25 tests/test_common/test_driver.cpp \
26 tests/test_common/keyboard_report_util.cpp
26$(TEST)_DEFS=$(TMK_COMMON_DEFS) 27$(TEST)_DEFS=$(TMK_COMMON_DEFS)
27$(TEST)_CONFIG=$(TEST_PATH)/config.h 28$(TEST)_CONFIG=$(TEST_PATH)/config.h
28VPATH+=$(TOP_DIR)/tests/test_common 29VPATH+=$(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 @@
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
25using testing::_; 26using testing::_;
26using testing::Return; 27using testing::Return;
@@ -32,7 +33,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
32 }, 33 },
33}; 34};
34 35
35TEST(Basic, SendKeyboardIsNotCalledWhenNoKeyIsPressed) { 36TEST(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
44TEST(Basic, SendKeyboardIsCalledWhenAKeyIsPressed) { 45TEST(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
20bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) {
21 return memcmp(lhs.raw, rhs.raw, sizeof(lhs.raw))==0;
22}
23
24std::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
30KeyboardReportMatcher::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
37bool KeyboardReportMatcher::MatchAndExplain(report_keyboard_t& report, MatchResultListener* listener) const {
38 return m_report == report;
39}
40
41void KeyboardReportMatcher::DescribeTo(::std::ostream* os) const {
42 *os << "is equal to " << m_report;
43}
44
45void 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
22bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs);
23std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& value);
24
25class 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;
31private:
32 report_keyboard_t m_report;
33};
34
35
36template<typename... Ts>
37inline 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
43void TestDriver::send_keyboard(report_keyboard_t* report) { 43void 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
48void TestDriver::send_mouse(report_mouse_t* report) { 48void 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
52void TestDriver::send_system(uint16_t data) { 52void TestDriver::send_system(uint16_t data) {
@@ -54,5 +54,5 @@ void TestDriver::send_system(uint16_t data) {
54} 54}
55 55
56void TestDriver::send_consumer(uint16_t data) { 56void 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
25class TestDriver { 26class 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));
34private: 35private: