aboutsummaryrefslogtreecommitdiff
path: root/tests/test_common/keyboard_report_util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_common/keyboard_report_util.cpp')
-rw-r--r--tests/test_common/keyboard_report_util.cpp79
1 files changed, 79 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..e148c76be
--- /dev/null
+++ b/tests/test_common/keyboard_report_util.cpp
@@ -0,0 +1,79 @@
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.hpp"
18#include <vector>
19#include <algorithm>
20using namespace testing;
21
22namespace {
23std::vector<uint8_t> get_keys(const report_keyboard_t& report) {
24 std::vector<uint8_t> result;
25#if defined(NKRO_ENABLE)
26# error NKRO support not implemented yet
27#elif defined(RING_BUFFERED_6KRO_REPORT_ENABLE)
28# error 6KRO support not implemented yet
29#else
30 for (size_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
31 if (report.keys[i]) {
32 result.emplace_back(report.keys[i]);
33 }
34 }
35#endif
36 std::sort(result.begin(), result.end());
37 return result;
38}
39} // namespace
40
41bool operator==(const report_keyboard_t& lhs, const report_keyboard_t& rhs) {
42 auto lhskeys = get_keys(lhs);
43 auto rhskeys = get_keys(rhs);
44 return lhs.mods == rhs.mods && lhskeys == rhskeys;
45}
46
47std::ostream& operator<<(std::ostream& stream, const report_keyboard_t& report) {
48 auto keys = get_keys(report);
49
50 // TODO: This should probably print friendly names for the keys
51 stream << "Keyboard Report: Mods (" << (uint32_t)report.mods << ") Keys (";
52
53 for (auto key = keys.cbegin(); key != keys.cend();) {
54 stream << +(*key);
55 key++;
56 if (key != keys.cend()) {
57 stream << ",";
58 }
59 }
60
61 return stream << ")" << std::endl;
62}
63
64KeyboardReportMatcher::KeyboardReportMatcher(const std::vector<uint8_t>& keys) {
65 memset(m_report.raw, 0, sizeof(m_report.raw));
66 for (auto k : keys) {
67 if (IS_MOD(k)) {
68 m_report.mods |= MOD_BIT(k);
69 } else {
70 add_key_to_report(&m_report, k);
71 }
72 }
73}
74
75bool KeyboardReportMatcher::MatchAndExplain(report_keyboard_t& report, MatchResultListener* listener) const { return m_report == report; }
76
77void KeyboardReportMatcher::DescribeTo(::std::ostream* os) const { *os << "is equal to " << m_report; }
78
79void KeyboardReportMatcher::DescribeNegationTo(::std::ostream* os) const { *os << "is not equal to " << m_report; }