test_driver.hpp
6004 bytes
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
19#include "gmock/gmock.h"
20#include <stdint.h>
21#include "host.h"
22#include "keyboard_report_util.hpp"
23#include "keycode_util.hpp"
24#include "test_logger.hpp"
25
26class TestDriver {
27 public:
28 TestDriver();
29 ~TestDriver();
30 void set_leds(uint8_t leds) {
31 m_leds = leds;
32 }
33
34 MOCK_METHOD1(send_keyboard_mock, void(report_keyboard_t&));
35 MOCK_METHOD1(send_nkro_mock, void(report_nkro_t&));
36 MOCK_METHOD1(send_mouse_mock, void(report_mouse_t&));
37 MOCK_METHOD1(send_extra_mock, void(report_extra_t&));
38
39 private:
40 static uint8_t keyboard_leds(void);
41 static void send_keyboard(report_keyboard_t* report);
42 static void send_nkro(report_nkro_t* report);
43 static void send_mouse(report_mouse_t* report);
44 static void send_extra(report_extra_t* report);
45 host_driver_t m_driver;
46 uint8_t m_leds = 0;
47 static TestDriver* m_this;
48};
49
50/**
51 * @brief Sets gmock expectation that a keyboard report of `report` keys will be sent.
52 * For this macro to parse correctly, the `report` arg must be surrounded by
53 * parentheses ( ). For instance,
54 *
55 * // Expect that a report of "KC_LSFT + KC_A" is sent to the host.
56 * EXPECT_REPORT(driver, (KC_LSFT, KC_A));
57 *
58 * is shorthand for
59 *
60 * EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_A)));
61 *
62 * It is possible to use .Times() and other gmock APIS with EXPECT_REPORT, for instance,
63 * allow only single report to be sent:
64 *
65 * EXPECT_REPORT(driver, (KC_LSFT, KC_A)).Times(1);
66 */
67#define EXPECT_REPORT(driver, report) EXPECT_CALL((driver), send_keyboard_mock(KeyboardReport report))
68
69/**
70 * @brief Sets gmock expectation that a mouse report of `report` will be sent.
71 * For this macro to parse correctly, the `report` arg must be surrounded by
72 * parentheses ( ). For instance,
73 *
74 * // Expect that a report of "X:-10 Y:0 H:0 V:10 BTN:1 " is sent to the host.
75 * EXPECT_REPORT(driver, (-10, 0, 0, 0, 1));
76 *
77 * is shorthand for
78 *
79 * EXPECT_CALL(driver, send_mouse_mock(MouseReport(-10, 0, 0, 0, 1)));
80 *
81 * It is possible to use .Times() and other gmock APIS with EXPECT_REPORT, for instance,
82 * allow only single report to be sent:
83 *
84 * EXPECT_REPORT(driver, (-10, 0, 0, 0, 1)).Times(1);
85 */
86#define EXPECT_MOUSE_REPORT(driver, report) EXPECT_CALL((driver), send_mouse_mock(MouseReport report))
87
88/**
89 * @brief Sets gmock expectation that Unicode `code_point` is sent with UNICODE_MODE_LINUX input
90 * mode. For instance for U+2013,
91 *
92 * EXPECT_UNICODE(driver, 0x2013);
93 *
94 * expects the sequence of keys:
95 *
96 * "Ctrl+Shift+U, 2, 0, 1, 3, space".
97 */
98#define EXPECT_UNICODE(driver, code_point) internal::expect_unicode_code_point((driver), (code_point))
99
100/**
101 * @brief Sets gmock expectation that a empty keyboard report will be sent.
102 * It is possible to use .Times() and other gmock APIS with EXPECT_EMPTY_REPORT, for instance,
103 * allow any number of empty reports with:
104 *
105 * EXPECT_EMPTY_REPORT(driver).Times(AnyNumber());
106 */
107#define EXPECT_EMPTY_REPORT(driver) EXPECT_REPORT(driver, ())
108
109/**
110 * @brief Sets gmock expectation that a empty keyboard report will be sent.
111 * It is possible to use .Times() and other gmock APIS with EXPECT_EMPTY_MOUSE_REPORT, for instance,
112 * allow any number of empty reports with:
113 *
114 * EXPECT_EMPTY_MOUSE_REPORT(driver).Times(AnyNumber());
115 */
116#define EXPECT_EMPTY_MOUSE_REPORT(driver) EXPECT_MOUSE_REPORT(driver, (0, 0, 0, 0, 0))
117
118/**
119 * @brief Sets gmock expectation that a keyboard report will be sent, without matching its content.
120 * It is possible to use .Times() and other gmock APIS with EXPECT_ANY_REPORT, for instance,
121 * allow a single arbitrary report with:
122 *
123 * EXPECT_ANY_REPORT(driver).Times(1);
124 */
125#define EXPECT_ANY_REPORT(driver) EXPECT_CALL((driver), send_keyboard_mock(_))
126
127/**
128 * @brief Sets gmock expectation that a mouse report will be sent, without matching its content.
129 * It is possible to use .Times() and other gmock APIS with EXPECT_ANY_MOUSE_REPORT, for instance,
130 * allow a single arbitrary report with:
131 *
132 * EXPECT_ANY_MOUSE_REPORT(driver).Times(1);
133 */
134#define EXPECT_ANY_MOUSE_REPORT(driver) EXPECT_CALL((driver), send_mouse_mock(_))
135
136/**
137 * @brief Sets gmock expectation that no keyboard report will be sent at all.
138 */
139#define EXPECT_NO_REPORT(driver) EXPECT_ANY_REPORT(driver).Times(0)
140
141/**
142 * @brief Sets gmock expectation that no keyboard report will be sent at all.
143 */
144#define EXPECT_NO_MOUSE_REPORT(driver) EXPECT_ANY_MOUSE_REPORT(driver).Times(0)
145
146/** @brief Tests whether keycode `actual` is equal to `expected`. */
147#define EXPECT_KEYCODE_EQ(actual, expected) EXPECT_THAT((actual), KeycodeEq((expected)))
148
149MATCHER_P(KeycodeEq, expected_keycode, "is equal to " + testing::PrintToString(expected_keycode) + ", keycode " + get_keycode_identifier_or_default(expected_keycode)) {
150 if (arg == expected_keycode) {
151 return true;
152 }
153 *result_listener << "keycode " << get_keycode_identifier_or_default(arg);
154 return false;
155}
156
157/**
158 * @brief Verify and clear all gmock expectations that have been setup until
159 * this point.
160 */
161#define VERIFY_AND_CLEAR(driver) testing::Mock::VerifyAndClearExpectations(&driver)
162
163namespace internal {
164void expect_unicode_code_point(TestDriver& driver, uint32_t code_point);
165} // namespace internal