summaryrefslogtreecommitdiff
path: root/quantum/encoder/tests/encoder_tests_split_role.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/encoder/tests/encoder_tests_split_role.cpp')
-rw-r--r--quantum/encoder/tests/encoder_tests_split_role.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/quantum/encoder/tests/encoder_tests_split_role.cpp b/quantum/encoder/tests/encoder_tests_split_role.cpp
new file mode 100644
index 0000000000..02264067f4
--- /dev/null
+++ b/quantum/encoder/tests/encoder_tests_split_role.cpp
@@ -0,0 +1,122 @@
1/* Copyright 2021 Balz Guenat
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 "gtest/gtest.h"
18#include "gmock/gmock.h"
19#include <vector>
20#include <algorithm>
21#include <stdio.h>
22
23extern "C" {
24#include "encoder.h"
25#include "encoder/tests/mock_split.h"
26}
27
28struct update {
29 int8_t index;
30 bool clockwise;
31};
32
33uint8_t num_updates = 0;
34
35bool isMaster;
36bool isLeftHand;
37
38bool is_keyboard_master(void) {
39 return isMaster;
40}
41
42bool encoder_update_kb(uint8_t index, bool clockwise) {
43 if (!isMaster) {
44 ADD_FAILURE() << "We shouldn't get here.";
45 }
46 num_updates++;
47 return true;
48}
49
50bool setAndRead(pin_t pin, bool val) {
51 setPin(pin, val);
52 return encoder_read();
53}
54
55class EncoderSplitTestRole : public ::testing::Test {
56 protected:
57 void SetUp() override {
58 num_updates = 0;
59 for (int i = 0; i < 32; i++) {
60 pinIsInputHigh[i] = 0;
61 pins[i] = 0;
62 }
63 }
64};
65
66TEST_F(EncoderSplitTestRole, TestPrimaryLeft) {
67 isMaster = true;
68 isLeftHand = true;
69 encoder_init();
70 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
71 setAndRead(0, false);
72 setAndRead(1, false);
73 setAndRead(0, true);
74 setAndRead(1, true);
75
76 EXPECT_EQ(num_updates, 1); // one update received
77}
78
79TEST_F(EncoderSplitTestRole, TestPrimaryRight) {
80 isMaster = true;
81 isLeftHand = false;
82 encoder_init();
83 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
84 setAndRead(6, false);
85 setAndRead(7, false);
86 setAndRead(6, true);
87 setAndRead(7, true);
88
89 uint8_t slave_state[32] = {0};
90 encoder_state_raw(slave_state);
91
92 EXPECT_EQ(num_updates, 1); // one update received
93}
94
95TEST_F(EncoderSplitTestRole, TestNotPrimaryLeft) {
96 isMaster = false;
97 isLeftHand = true;
98 encoder_init();
99 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
100 setAndRead(0, false);
101 setAndRead(1, false);
102 setAndRead(0, true);
103 setAndRead(1, true);
104
105 EXPECT_EQ(num_updates, 0); // zero updates received
106}
107
108TEST_F(EncoderSplitTestRole, TestNotPrimaryRight) {
109 isMaster = false;
110 isLeftHand = false;
111 encoder_init();
112 // send 4 pulses. with resolution 4, that's one step and we should get 1 update.
113 setAndRead(6, false);
114 setAndRead(7, false);
115 setAndRead(6, true);
116 setAndRead(7, true);
117
118 uint8_t slave_state[32] = {0};
119 encoder_state_raw(slave_state);
120
121 EXPECT_EQ(num_updates, 0); // zero updates received
122}