summaryrefslogtreecommitdiff
path: root/quantum/encoder
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/encoder')
-rw-r--r--quantum/encoder/tests/config_mock_split_role.h26
-rw-r--r--quantum/encoder/tests/encoder_tests_split_role.cpp122
-rw-r--r--quantum/encoder/tests/mock.c4
-rw-r--r--quantum/encoder/tests/mock_split.c4
-rw-r--r--quantum/encoder/tests/rules.mk10
-rw-r--r--quantum/encoder/tests/testlist.mk3
6 files changed, 168 insertions, 1 deletions
diff --git a/quantum/encoder/tests/config_mock_split_role.h b/quantum/encoder/tests/config_mock_split_role.h
new file mode 100644
index 0000000000..c80ac4d519
--- /dev/null
+++ b/quantum/encoder/tests/config_mock_split_role.h
@@ -0,0 +1,26 @@
1// Copyright 2022 Nick Brassel (@tzarc)
2// SPDX-License-Identifier: GPL-2.0-or-later
3#pragma once
4
5#define MATRIX_ROWS 1
6#define MATRIX_COLS 1
7
8/* Here, "pins" from 0 to 31 are allowed. */
9#define ENCODERS_PAD_A \
10 { 0, 2 }
11#define ENCODERS_PAD_B \
12 { 1, 3 }
13#define ENCODERS_PAD_A_RIGHT \
14 { 4, 6 }
15#define ENCODERS_PAD_B_RIGHT \
16 { 5, 7 }
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22#include "mock_split.h"
23
24#ifdef __cplusplus
25};
26#endif
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}
diff --git a/quantum/encoder/tests/mock.c b/quantum/encoder/tests/mock.c
index 10a00cb8f2..61f2f8294d 100644
--- a/quantum/encoder/tests/mock.c
+++ b/quantum/encoder/tests/mock.c
@@ -34,3 +34,7 @@ bool setPin(pin_t pin, bool val) {
34 pins[pin] = val; 34 pins[pin] = val;
35 return val; 35 return val;
36} 36}
37
38__attribute__((weak)) bool is_keyboard_master(void) {
39 return true;
40}
diff --git a/quantum/encoder/tests/mock_split.c b/quantum/encoder/tests/mock_split.c
index dd3c26d958..5cc6cd19e1 100644
--- a/quantum/encoder/tests/mock_split.c
+++ b/quantum/encoder/tests/mock_split.c
@@ -36,3 +36,7 @@ bool setPin(pin_t pin, bool val) {
36} 36}
37 37
38void last_encoder_activity_trigger(void) {} 38void last_encoder_activity_trigger(void) {}
39
40__attribute__((weak)) bool is_keyboard_master(void) {
41 return true;
42}
diff --git a/quantum/encoder/tests/rules.mk b/quantum/encoder/tests/rules.mk
index 6a2611952c..d01c1c66ee 100644
--- a/quantum/encoder/tests/rules.mk
+++ b/quantum/encoder/tests/rules.mk
@@ -56,3 +56,13 @@ encoder_split_no_right_SRC := \
56 $(QUANTUM_PATH)/encoder/tests/mock_split.c \ 56 $(QUANTUM_PATH)/encoder/tests/mock_split.c \
57 $(QUANTUM_PATH)/encoder/tests/encoder_tests_split_no_right.cpp \ 57 $(QUANTUM_PATH)/encoder/tests/encoder_tests_split_no_right.cpp \
58 $(QUANTUM_PATH)/encoder.c 58 $(QUANTUM_PATH)/encoder.c
59
60encoder_split_role_DEFS := -DENCODER_TESTS -DENCODER_ENABLE -DENCODER_MOCK_SPLIT
61encoder_split_role_INC := $(QUANTUM_PATH)/split_common
62encoder_split_role_CONFIG := $(QUANTUM_PATH)/encoder/tests/config_mock_split_role.h
63
64encoder_split_role_SRC := \
65 platforms/test/timer.c \
66 $(QUANTUM_PATH)/encoder/tests/mock_split.c \
67 $(QUANTUM_PATH)/encoder/tests/encoder_tests_split_role.cpp \
68 $(QUANTUM_PATH)/encoder.c
diff --git a/quantum/encoder/tests/testlist.mk b/quantum/encoder/tests/testlist.mk
index 6b2fd84d96..a407f1fadd 100644
--- a/quantum/encoder/tests/testlist.mk
+++ b/quantum/encoder/tests/testlist.mk
@@ -4,4 +4,5 @@ TEST_LIST += \
4 encoder_split_left_gt_right \ 4 encoder_split_left_gt_right \
5 encoder_split_left_lt_right \ 5 encoder_split_left_lt_right \
6 encoder_split_no_left \ 6 encoder_split_no_left \
7 encoder_split_no_right 7 encoder_split_no_right \
8 encoder_split_role \