forked from ililim/dual-key-remap
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.c
307 lines (271 loc) · 7.81 KB
/
tests.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "input.h"
#include "keys.c"
#include "remap.c"
struct Output
{
int scan_code;
int virt_code;
enum Direction dir;
struct Output * next;
};
struct Output * g_output_list = NULL;
register_output(int scan_code, int virt_code, enum Direction dir)
{
struct Output * output = malloc(sizeof(struct Output));
output->scan_code = scan_code;
output->virt_code = virt_code;
output->dir = dir;
output->next = NULL;
if (g_output_list) {
struct Output * tail = g_output_list;
while (tail->next) tail = tail->next;
tail->next = output;
} else {
g_output_list = output;
}
}
// Simulate input and pass it to our handler. If key is not swallowed, register
// it for later test inspection.
void simulate_input(int scan_code, int virt_code, enum Direction dir, int is_injected)
{
int swallow_input = handle_input(scan_code, virt_code, dir, is_injected);
if (!swallow_input) {
register_output(scan_code, virt_code, dir);
}
}
// Mock sending input through winapi
void send_input(int scan_code, int virt_code, enum Direction dir)
{
simulate_input(scan_code, virt_code, dir, 1);
}
// Simulates user input, notably has no is_injected flag set
void user_input(int scan_code, int virt_code, enum Direction dir)
{
simulate_input(scan_code, virt_code, dir, 0);
}
dump_outputs(char * msg)
{
struct Output * output = g_output_list;
printf(msg);
int i = 0;
while (output) {
KEY_DEF * key = find_key_def_by_virt_code(output->virt_code);
if (key) {
printf("%d) %s %s\n", i, key->name, fmt_dir(output->dir));
} else {
printf("%d) 0x%02x %s\n", i, output->virt_code, fmt_dir(output->dir));
}
output = output->next;
i++;
}
if (i == 0) {
printf("<EMPTY>\n");
}
}
// Test actions & assertions
// --------------------------------------
void IN(KEY_DEF * key, enum Direction dir)
{
user_input(key->scan_code, key->virt_code, dir);
}
void IN_MANUAL(int scan_code, int virt_code, int dir)
{
user_input(scan_code, virt_code, dir);
}
void SEE(KEY_DEF * key, enum Direction dir)
{
struct Output * head = g_output_list;
char * msg = malloc(255);
sprintf(msg, "Expected top output to be %s %s but found:\n", key->name, fmt_dir(dir));
if (!head) {
dump_outputs((char *)msg);
assert(("NOT EMPTY", head));
}
if (head->virt_code != key->virt_code) {
dump_outputs((char *)msg);
assert(("VIRT CODE", head->virt_code == key->virt_code));
}
if (head->dir != dir) {
dump_outputs((char *)msg);
assert(("OUT DIR", head->dir == dir));
}
g_output_list = head->next;
head = NULL;
free(head);
}
void EMPTY()
{
if (!g_output_list == 0) {
dump_outputs("Expected empty but found:\n");
assert(("EMPTY", g_output_list == 0));
}
}
void OK()
{
printf("OK\n");
}
void SECTION(char * msg) {
printf("\n");
printf(msg);
printf("\n----------------------------------------------\n");
}
void main()
{
SECTION("Passthrough keys if no config");
EMPTY();
IN(ESC, DOWN);
IN(SHIFT, DOWN);
IN(ESC, UP);
SEE(ESC, DOWN);
SEE(SHIFT, DOWN);
SEE(ESC, UP);
EMPTY();
OK();
SECTION("Helpful error messages");
assert(1 == load_config_line("invalid_setting=ESCAPE", 1));
assert(1 == load_config_line("remap_key=INVALID_KEY", 2));
assert(1 == load_config_line("remap_key::ESCAPE", 3));
assert(0 == load_config_line("remap_key=ESCAPE", 999));
assert(("Incomplete key", 1 == load_config_line("remap_key=ESCAPE", 4)));
reset_config();
OK();
SECTION("Registers remappings from config");
assert(("debug off by default", g_debug == 0));
assert(0 == load_config_line("debug=1", 0));
assert(("config turns debug on", g_debug == 1));
assert(0 == load_config_line("debug=0", 0));
assert(("config turns debug off", g_debug == 0));
assert(0 == load_config_line("# Comments are ignored", 0));
assert(0 == load_config_line("", 0));
assert(0 == load_config_line("remap_key=CAPSLOCK", 0));
assert(0 == load_config_line("when_alone=ESCAPE", 0));
assert(0 == load_config_line("with_other=CTRL", 0));
assert(0 == load_config_line("remap_key=TAB", 0));
assert(0 == load_config_line("when_alone=TAB", 0));
assert(0 == load_config_line("with_other=ALT", 0));
assert(0 == load_config_line("remap_key=SHIFT", 0));
assert(0 == load_config_line("when_alone=SPACE", 0));
assert(0 == load_config_line("with_other=SHIFT", 0));
assert(("registered first", g_remap_list->from == CAPS));
assert(("registered first", g_remap_list->to_when_alone == ESC));
assert(("registered first", g_remap_list->to_with_other == CTRL));
assert(("registered second", g_remap_list->next->from == TAB));
assert(("registered second", g_remap_list->next->to_when_alone == TAB));
assert(("registered second", g_remap_list->next->to_with_other == ALT));
assert(("registered third", g_remap_list->next->next->from == SHIFT));
assert(("registered third", g_remap_list->next->next->to_when_alone == SPACE));
assert(("registered third", g_remap_list->next->next->to_with_other == SHIFT));
printf("OK\n");
SECTION("Passthrough unmapped");
IN(ENTER, DOWN);
SEE(ENTER, DOWN);
IN(ENTER, UP);
SEE(ENTER, UP);
EMPTY();
OK();
SECTION("Remap when alone");
IN(CAPS, DOWN);
EMPTY();
IN(CAPS, UP);
SEE(ESC, DOWN);
SEE(ESC, UP);
EMPTY();
OK();
SECTION("Remap with other");
IN(CAPS, DOWN);
EMPTY();
IN(ENTER, DOWN);
SEE(CTRL, DOWN);
SEE(ENTER, DOWN);
EMPTY();
IN(CAPS, UP);
SEE(CTRL, UP);
EMPTY();
OK();
SECTION("Remap to self (when alone)");
IN(TAB, DOWN);
EMPTY();
IN(TAB, UP);
SEE(TAB, DOWN);
SEE(TAB, UP);
EMPTY();
IN(TAB, DOWN);
IN(ENTER, DOWN);
SEE(ALT, DOWN);
SEE(ENTER, DOWN);
EMPTY();
IN(TAB, UP);
SEE(ALT, UP);
EMPTY();
OK();
SECTION("Remap to self (with other)");
IN(SHIFT, DOWN);
EMPTY();
IN(SHIFT, UP);
SEE(SPACE, DOWN);
SEE(SPACE, UP);
EMPTY();
IN(SHIFT, DOWN);
IN(ENTER, DOWN);
SEE(SHIFT, DOWN);
SEE(ENTER, DOWN);
EMPTY();
IN(SHIFT, UP);
SEE(SHIFT, UP);
EMPTY();
OK();
SECTION("Handle duplicate same key down");
IN(SHIFT, DOWN);
IN(SHIFT, DOWN);
EMPTY();
IN(SHIFT, UP);
SEE(SPACE, DOWN);
SEE(SPACE, UP);
EMPTY();
IN(SHIFT, UP);
SEE(SPACE, DOWN);
SEE(SPACE, UP);
EMPTY();
OK();
SECTION("Handle two remappings triggering each-other (tap first key)");
IN(TAB, DOWN);
IN(CAPS, DOWN);
EMPTY();
IN(TAB, UP);
SEE(CTRL, DOWN);
SEE(TAB, DOWN);
SEE(TAB, UP);
EMPTY();
IN(CAPS, UP);
SEE(CTRL, UP);
EMPTY();
OK();
SECTION("Handle two remappings triggering each-other (tap second key)");
IN(CAPS, DOWN);
IN(TAB, DOWN);
EMPTY();
IN(TAB, UP);
SEE(CTRL, DOWN);
SEE(TAB, DOWN);
SEE(TAB, UP);
EMPTY();
IN(CAPS, UP);
SEE(CTRL, UP);
EMPTY();
OK();
SECTION("Handle left/right modifier sending the same scan code");
IN_MANUAL(SK_LEFT_SHIFT, VK_LEFT_SHIFT, DOWN);
IN_MANUAL(SK_LEFT_SHIFT, VK_LEFT_SHIFT, UP);
SEE(SPACE, DOWN);
SEE(SPACE, UP);
EMPTY();
IN_MANUAL(SK_LEFT_SHIFT, VK_RIGHT_SHIFT, DOWN);
IN_MANUAL(SK_LEFT_SHIFT, VK_RIGHT_SHIFT, UP);
SEE(RSHIFT, DOWN);
SEE(RSHIFT, UP);
EMPTY();
printf("\nGreat! All test passed successfully.\n");
}