diff options
Diffstat (limited to 'tests/basic/test_one_shot_keys.cpp')
-rw-r--r-- | tests/basic/test_one_shot_keys.cpp | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/tests/basic/test_one_shot_keys.cpp b/tests/basic/test_one_shot_keys.cpp new file mode 100644 index 000000000..98178912e --- /dev/null +++ b/tests/basic/test_one_shot_keys.cpp | |||
@@ -0,0 +1,197 @@ | |||
1 | /* Copyright 2021 Stefan Kerkmann | ||
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 "action_util.h" | ||
18 | #include "keyboard_report_util.hpp" | ||
19 | #include "test_common.hpp" | ||
20 | |||
21 | using testing::_; | ||
22 | using testing::InSequence; | ||
23 | |||
24 | class OneShot : public TestFixture {}; | ||
25 | class OneShotParametrizedTestFixture : public ::testing::WithParamInterface<std::pair<KeymapKey, KeymapKey>>, public OneShot {}; | ||
26 | |||
27 | TEST_F(OneShot, OSMWithoutAdditionalKeypressDoesNothing) { | ||
28 | TestDriver driver; | ||
29 | auto osm_key = KeymapKey(0, 0, 0, OSM(MOD_LSFT), KC_LSFT); | ||
30 | |||
31 | set_keymap({osm_key}); | ||
32 | |||
33 | /* Press and release OSM key*/ | ||
34 | EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); | ||
35 | osm_key.press(); | ||
36 | run_one_scan_loop(); | ||
37 | osm_key.release(); | ||
38 | run_one_scan_loop(); | ||
39 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
40 | |||
41 | /* OSM are added when an actual report is send */ | ||
42 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(osm_key.report_code))); | ||
43 | send_keyboard_report(); | ||
44 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
45 | |||
46 | /* Make unit-test pass */ | ||
47 | clear_oneshot_mods(); | ||
48 | } | ||
49 | |||
50 | #if defined(ONESHOT_TIMEOUT) | ||
51 | |||
52 | TEST_P(OneShotParametrizedTestFixture, OSMExpiredDoesNothing) { | ||
53 | TestDriver driver; | ||
54 | KeymapKey osm_key = GetParam().first; | ||
55 | KeymapKey regular_key = GetParam().second; | ||
56 | |||
57 | set_keymap({osm_key, regular_key}); | ||
58 | |||
59 | /* Press and release OSM */ | ||
60 | EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); | ||
61 | osm_key.press(); | ||
62 | run_one_scan_loop(); | ||
63 | osm_key.release(); | ||
64 | idle_for(ONESHOT_TIMEOUT); | ||
65 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
66 | |||
67 | /* Press regular key */ | ||
68 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(regular_key.report_code))).Times(1); | ||
69 | regular_key.press(); | ||
70 | run_one_scan_loop(); | ||
71 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
72 | |||
73 | /* Release regular key */ | ||
74 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
75 | regular_key.release(); | ||
76 | run_one_scan_loop(); | ||
77 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
78 | } | ||
79 | |||
80 | #endif | ||
81 | |||
82 | TEST_P(OneShotParametrizedTestFixture, OSMWithAdditionalKeypress) { | ||
83 | TestDriver driver; | ||
84 | KeymapKey osm_key = GetParam().first; | ||
85 | KeymapKey regular_key = GetParam().second; | ||
86 | |||
87 | set_keymap({osm_key, regular_key}); | ||
88 | |||
89 | /* Press and release OSM */ | ||
90 | EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); | ||
91 | osm_key.press(); | ||
92 | run_one_scan_loop(); | ||
93 | osm_key.release(); | ||
94 | run_one_scan_loop(); | ||
95 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
96 | |||
97 | /* Press regular key */ | ||
98 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(osm_key.report_code, regular_key.report_code))).Times(1); | ||
99 | regular_key.press(); | ||
100 | run_one_scan_loop(); | ||
101 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
102 | |||
103 | /* Release regular key */ | ||
104 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
105 | regular_key.release(); | ||
106 | run_one_scan_loop(); | ||
107 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
108 | } | ||
109 | |||
110 | TEST_P(OneShotParametrizedTestFixture, OSMAsRegularModifierWithAdditionalKeypress) { | ||
111 | TestDriver driver; | ||
112 | testing::InSequence s; | ||
113 | |||
114 | KeymapKey osm_key = GetParam().first; | ||
115 | KeymapKey regular_key = GetParam().second; | ||
116 | |||
117 | set_keymap({osm_key, regular_key}); | ||
118 | |||
119 | /* Press OSM */ | ||
120 | EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); | ||
121 | osm_key.press(); | ||
122 | run_one_scan_loop(); | ||
123 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
124 | |||
125 | /* Press regular key */ | ||
126 | EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); | ||
127 | regular_key.press(); | ||
128 | run_one_scan_loop(); | ||
129 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
130 | |||
131 | /* Release regular key */ | ||
132 | EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); | ||
133 | regular_key.release(); | ||
134 | run_one_scan_loop(); | ||
135 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
136 | |||
137 | /* Release OSM */ | ||
138 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(regular_key.report_code, osm_key.report_code))).Times(1); | ||
139 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(1); | ||
140 | osm_key.release(); | ||
141 | run_one_scan_loop(); | ||
142 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
143 | } | ||
144 | |||
145 | // clang-format off | ||
146 | |||
147 | INSTANTIATE_TEST_CASE_P( | ||
148 | OneShotModifierTests, | ||
149 | OneShotParametrizedTestFixture, | ||
150 | ::testing::Values( | ||
151 | /* first is osm key, second is regular key. */ | ||
152 | std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_LSFT), KC_LSFT}, KeymapKey{0, 1, 1, KC_A}), | ||
153 | std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_LCTL), KC_LCTL}, KeymapKey{0, 1, 1, KC_A}), | ||
154 | std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_LALT), KC_LALT}, KeymapKey{0, 1, 1, KC_A}), | ||
155 | std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_LGUI), KC_LGUI}, KeymapKey{0, 1, 1, KC_A}), | ||
156 | std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_RCTL), KC_RCTL}, KeymapKey{0, 1, 1, KC_A}), | ||
157 | std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_RSFT), KC_RSFT}, KeymapKey{0, 1, 1, KC_A}), | ||
158 | std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_RALT), KC_RALT}, KeymapKey{0, 1, 1, KC_A}), | ||
159 | std::make_pair(KeymapKey{0, 0, 0, OSM(MOD_RGUI), KC_RGUI}, KeymapKey{0, 1, 1, KC_A}) | ||
160 | )); | ||
161 | // clang-format on | ||
162 | |||
163 | TEST_F(OneShot, OSLWithAdditionalKeypress) { | ||
164 | TestDriver driver; | ||
165 | InSequence s; | ||
166 | KeymapKey osl_key = KeymapKey{0, 0, 0, OSL(1)}; | ||
167 | KeymapKey regular_key = KeymapKey{1, 1, 0, KC_A}; | ||
168 | |||
169 | set_keymap({osl_key, regular_key}); | ||
170 | |||
171 | /* Press OSL key */ | ||
172 | EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0); | ||
173 | osl_key.press(); | ||
174 | run_one_scan_loop(); | ||
175 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
176 | |||
177 | /* Release OSL key */ | ||
178 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(2); | ||
179 | osl_key.release(); | ||
180 | run_one_scan_loop(); | ||
181 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
182 | |||
183 | /* Press regular key */ | ||
184 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
185 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(regular_key.report_code))).Times(2); | ||
186 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
187 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
188 | regular_key.press(); | ||
189 | run_one_scan_loop(); | ||
190 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
191 | |||
192 | /* Release regular key */ | ||
193 | EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); | ||
194 | regular_key.release(); | ||
195 | run_one_scan_loop(); | ||
196 | testing::Mock::VerifyAndClearExpectations(&driver); | ||
197 | } | ||