Debian Codegraph @

test/test-stylus-validity.c

raw
1 /*
2 * Copyright © 2019 Red Hat, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software
5 udo y and its documentation for any purpose is hereby granted without
6 * fee, provided that the above copyright notice appear in all copies
7 * and that both that copyright notice and this permission notice
8 * appear in supporting documentation, and that the name of Red Hat
9 * not be used in advertising or publicity pertaining to distribution
10 * of the software without specific, written prior permission. Red
11 * Hat makes no representations about the suitability of this software
12 * for any purpose. It is provided "as is" without express or implied
13 * warranty.
14 *
15 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
17 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
19 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24 #include "config.h"
25
26 #define _GNU_SOURCE
27 #include <dirent.h>
28 #include <fcntl.h>
29 #include <glib.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #include "libwacom.h"
38
39 static const WacomStylus **all_styli;
40
41 static void
42 test_type(gconstpointer data)
43 {
44 const WacomStylus *stylus = data;
45
46 switch (libwacom_stylus_get_type(stylus)) {
47 case WSTYLUS_GENERAL:
48 case WSTYLUS_INKING:
49 case WSTYLUS_AIRBRUSH:
50 case WSTYLUS_CLASSIC:
51 case WSTYLUS_MARKER:
52 case WSTYLUS_STROKE:
53 case WSTYLUS_PUCK:
54 case WSTYLUS_3D:
55 case WSTYLUS_MOBILE:
56 break;
57 case WSTYLUS_UNKNOWN:
58 default:
59 g_test_fail();
60 }
61 }
62
63 static void
64 test_mobile(gconstpointer data)
65 {
66 const WacomStylus *stylus = data;
67
68 g_assert_cmpint(libwacom_stylus_get_type(stylus), ==, WSTYLUS_MOBILE);
69 }
70
71 static void
72 test_eraser_type(gconstpointer data)
73 {
74 const WacomStylus *stylus = data;
75
76 switch (libwacom_stylus_get_eraser_type(stylus)) {
77 case WACOM_ERASER_NONE:
78 case WACOM_ERASER_INVERT:
79 case WACOM_ERASER_BUTTON:
80 break;
81 case WACOM_ERASER_UNKNOWN:
82 default:
83 g_test_fail();
84 }
85 }
86
87 static void
88 test_has_eraser(gconstpointer data)
89 {
90 const WacomStylus *stylus = data;
91 gboolean matching_eraser_found = FALSE;
92 g_autofree const WacomStylus **paired = NULL;
93 int count;
94 int i;
95
96 /* A stylus cannot be an eraser and have an eraser at the same time */
97 g_assert_true(libwacom_stylus_has_eraser(stylus));
98 g_assert_false(libwacom_stylus_is_eraser(stylus));
99
100 /* Search for the linked eraser */
101 paired = libwacom_stylus_get_paired_styli(stylus, &count);
102 g_assert_cmpint(count, >, 0);
103
104 for (i = 0; i < count; i++) {
105 const WacomStylus *s = paired[i];
106 if (libwacom_stylus_is_eraser(s)) {
107 matching_eraser_found = TRUE;
108 break;
109 }
110 }
111
112 g_assert_true(matching_eraser_found);
113 }
114
115 static void
116 test_eraser_link(const WacomStylus *stylus,
117 gboolean linked)
118 {
119 gboolean matching_stylus_found = FALSE;
120 g_autofree const WacomStylus **paired = NULL;
121 int count;
122 int i;
123
124 /* A stylus cannot be an eraser and have an eraser at the same time */
125 g_assert_false(libwacom_stylus_has_eraser(stylus));
126 g_assert_true(libwacom_stylus_is_eraser(stylus));
127
128 /* Verify the link count */
129 paired = libwacom_stylus_get_paired_styli(stylus, &count);
130 if (!linked) {
131 g_assert_cmpint(count, ==, 0);
132 return;
133 }
134
135 /* If we're supposed to be linked, ensure its to a non-eraser */
136 g_assert_cmpint(count, >, 0);
137 for (i = 0; i < count; i++) {
138 const WacomStylus *s = paired[i];
139 if (libwacom_stylus_has_eraser(s)) {
140 matching_stylus_found = TRUE;
141 break;
142 }
143 }
144
145 g_assert_true(matching_stylus_found);
146 }
147
148 static void
149 test_is_eraser_unlinked(gconstpointer data)
150 {
151 const WacomStylus *stylus = data;
152
153 test_eraser_link(stylus, FALSE);
154 }
155
156 static void
157 test_is_eraser_linked(gconstpointer data)
158 {
159 const WacomStylus *stylus = data;
160
161 test_eraser_link(stylus, TRUE);
162 }
163
164 static void
165 test_eraser_inverted(gconstpointer data)
166 {
167 const WacomStylus *stylus = data;
168 WacomEraserType eraser_type = libwacom_stylus_get_eraser_type(stylus);
169
170 g_assert_cmpint(eraser_type, ==, WACOM_ERASER_INVERT);
171 }
172
173 static void
174 test_eraser_button(gconstpointer data)
175 {
176 const WacomStylus *stylus = data;
177 WacomEraserType eraser_type = libwacom_stylus_get_eraser_type(stylus);
178
179 g_assert_cmpint(eraser_type, ==, WACOM_ERASER_BUTTON);
180 }
181
182 static void
183 test_puck(gconstpointer data)
184 {
185 const WacomStylus *stylus = data;
186 int has_wheel = libwacom_stylus_has_wheel(stylus);
187 int has_lens = libwacom_stylus_has_lens(stylus);
188
189 /* 4D mouse is the only one with neither, everything
190 * else has either wheel or lens */
191 if (libwacom_stylus_get_id(stylus) == 0x94) {
192 g_assert_false(has_wheel);
193 g_assert_false(has_lens);
194 } else {
195 g_assert_true(has_wheel != has_lens);
196 }
197 }
198
199 static void
200 test_tilt(gconstpointer data)
201 {
202 const WacomStylus *stylus = data;
203 gboolean has_tilt = libwacom_stylus_get_axes(stylus) & WACOM_AXIS_TYPE_TILT;
204 g_assert_true(has_tilt);
205 }
206
207 static void
208 test_no_tilt(gconstpointer data)
209 {
210 const WacomStylus *stylus = data;
211 gboolean has_tilt = libwacom_stylus_get_axes(stylus) & WACOM_AXIS_TYPE_TILT;
212 g_assert_false(has_tilt);
213 }
214
215 static void
216 test_pressure(gconstpointer data)
217 {
218 const WacomStylus *stylus = data;
219 gboolean has_pressure =
220 libwacom_stylus_get_axes(stylus) & WACOM_AXIS_TYPE_PRESSURE;
221 g_assert_true(has_pressure);
222 }
223
224 static void
225 test_no_pressure(gconstpointer data)
226 {
227 const WacomStylus *stylus = data;
228 gboolean has_pressure =
229 libwacom_stylus_get_axes(stylus) & WACOM_AXIS_TYPE_PRESSURE;
230 g_assert_false(has_pressure);
231 }
232
233 static void
234 test_distance(gconstpointer data)
235 {
236 const WacomStylus *stylus = data;
237 gboolean has_distance =
238 libwacom_stylus_get_axes(stylus) & WACOM_AXIS_TYPE_DISTANCE;
239 g_assert_true(has_distance);
240 }
241
242 static void
243 test_no_distance(gconstpointer data)
244 {
245 const WacomStylus *stylus = data;
246 gboolean has_distance =
247 libwacom_stylus_get_axes(stylus) & WACOM_AXIS_TYPE_DISTANCE;
248 g_assert_false(has_distance);
249 }
250
251 static void
252 test_name(gconstpointer data)
253 {
254 const WacomStylus *stylus = data;
255
256 g_assert_nonnull(libwacom_stylus_get_name(stylus));
257 }
258
259 static void
260 test_generic(gconstpointer data)
261 {
262 const WacomStylus *stylus = data;
263
264 g_assert_true(libwacom_stylus_is_generic(stylus));
265 }
266
267 static void
268 test_not_generic(gconstpointer data)
269 {
270 const WacomStylus *stylus = data;
271
272 g_assert_false(libwacom_stylus_is_generic(stylus));
273 }
274
275 static void
276 test_buttons(gconstpointer data)
277 {
278 const WacomStylus *stylus = data;
279
280 g_assert_cmpint(libwacom_stylus_get_num_buttons(stylus), >, 0);
281 }
282
283 static void
284 test_no_buttons(gconstpointer data)
285 {
286 const WacomStylus *stylus = data;
287
288 g_assert_cmpint(libwacom_stylus_get_num_buttons(stylus), ==, 0);
289 }
290
291 static void
292 test_mutually_paired(gconstpointer data)
293 {
294 const WacomStylus *stylus = data;
295 g_autofree const WacomStylus **stylus_pairings = NULL;
296 int count;
297
298 stylus_pairings = libwacom_stylus_get_paired_styli(stylus, &count);
299
300 for (int i = 0; i < count; i++) {
301 const WacomStylus *paired = stylus_pairings[i];
302 g_autofree const WacomStylus **counter_paired = NULL;
303 gboolean match_found = FALSE;
304 int pair_count;
305
306 g_assert_true(paired != stylus); /* can't be paired with itself */
307
308 counter_paired = libwacom_stylus_get_paired_styli(paired, &pair_count);
309 for (int j = 0; j < pair_count; j++) {
310 const WacomStylus *back_paired = counter_paired[j];
311 if (back_paired == stylus) {
312 match_found = TRUE;
313 break;
314 }
315 }
316 g_assert_true(match_found);
317 }
318 }
319
320 /* Wrapper function to make adding tests simpler. g_test requires
321 * a unique test case name so we assemble that from the test function and
322 * the stylus data.
323 */
324 static inline void
325 _add_test(const WacomStylus *stylus,
326 GTestDataFunc func,
327 const char *funcname)
328 {
329 char buf[128];
330 const char *prefix;
331
332 /* tests must be test_foobar */
333 g_assert(strncmp(funcname, "test_", 5) == 0);
334 prefix = &funcname[5];
335
336 snprintf(buf,
337 128,
338 "/stylus/%s/%03x-%s",
339 prefix,
340 libwacom_stylus_get_id(stylus),
341 libwacom_stylus_get_name(stylus));
342 g_test_add_data_func(buf, stylus, func);
343 }
344 #define add_test(stylus, func_) \
345 _add_test(stylus, func_, #func_)
346
347 static void
348 setup_aes_tests(const WacomStylus *stylus)
349 {
350 add_test(stylus, test_mobile);
351
352 add_test(stylus, test_pressure);
353 add_test(stylus, test_no_distance);
354
355 if (libwacom_stylus_get_id(stylus) < 0x8000) {
356 add_test(stylus, test_no_tilt);
357 } else {
358 add_test(stylus, test_tilt);
359 }
360
361 if (libwacom_stylus_is_eraser(stylus)) {
362 add_test(stylus, test_is_eraser_unlinked);
363 add_test(stylus, test_eraser_button);
364 }
365 }
366
367 static void
368 setup_emr_tests(const WacomStylus *stylus)
369 {
370 switch (libwacom_stylus_get_id(stylus)) {
371 case 0xfffff: /* GENERIC_PEN_WITH_ERASER */
372 case 0xffffe: /* GENERIC_ERASER */
373 add_test(stylus, test_generic);
374 add_test(stylus, test_pressure);
375 add_test(stylus, test_distance);
376 add_test(stylus, test_tilt);
377 break;
378 case 0xffffd: /* GENERIC_PEN_NO_ERASER */
379 add_test(stylus, test_generic);
380 add_test(stylus, test_pressure);
381 add_test(stylus, test_no_distance);
382 add_test(stylus, test_no_tilt);
383 break;
384 case 0x006:
385 case 0x096:
386 case 0x097:
387 add_test(stylus, test_not_generic);
388 add_test(stylus, test_no_pressure);
389 add_test(stylus, test_distance);
390 add_test(stylus, test_no_tilt);
391 break;
392 case 0x007:
393 case 0x017:
394 case 0x094:
395 case 0x806:
396 add_test(stylus, test_not_generic);
397 add_test(stylus, test_no_pressure);
398 add_test(stylus, test_distance);
399 add_test(stylus, test_tilt);
400 break;
401 case 0x021:
402 case 0x8e2:
403 case 0x862:
404 add_test(stylus, test_not_generic);
405 add_test(stylus, test_pressure);
406 add_test(stylus, test_distance);
407 add_test(stylus, test_no_tilt);
408 break;
409 default:
410 add_test(stylus, test_not_generic);
411 add_test(stylus, test_pressure);
412 add_test(stylus, test_tilt);
413 add_test(stylus, test_distance);
414 break;
415 }
416
417 if (libwacom_stylus_is_eraser(stylus)) {
418 add_test(stylus, test_is_eraser_linked);
419 add_test(stylus, test_eraser_inverted);
420 }
421 }
422
423 static void
424 setup_tests(const WacomStylus *stylus)
425 {
426 add_test(stylus, test_name);
427 add_test(stylus, test_type);
428
429 /* Button checks */
430 switch (libwacom_stylus_get_type(stylus)) {
431 case WSTYLUS_PUCK:
432 add_test(stylus, test_puck);
433 add_test(stylus, test_buttons);
434 break;
435 case WSTYLUS_INKING:
436 case WSTYLUS_STROKE:
437 add_test(stylus, test_no_buttons);
438 break;
439 default:
440 switch (libwacom_stylus_get_id(stylus)) {
441 case 0x885:
442 case 0x8051:
443 add_test(stylus, test_no_buttons);
444 break;
445 default:
446 add_test(stylus, test_buttons);
447 }
448 }
449
450 /* Technology-specific tests */
451 if (libwacom_stylus_get_type(stylus) == WSTYLUS_MOBILE) {
452 setup_aes_tests(stylus);
453 } else {
454 setup_emr_tests(stylus);
455 }
456
457 if (libwacom_stylus_has_eraser(stylus))
458 add_test(stylus, test_has_eraser);
459
460 if (libwacom_stylus_is_eraser(stylus))
461 add_test(stylus, test_eraser_type);
462
463 add_test(stylus, test_mutually_paired);
464 }
465
466 /**
467 * Return a NULL-terminated list of all styli.
468 *
469 * libwacom only gives us the styli per-device so this is a bit more
470 * complicated than it should be.
471 */
472 static const WacomStylus **
473 assemble_styli(WacomDeviceDatabase *db)
474 {
475 g_autofree WacomDevice **devices =
476 libwacom_list_devices_from_database(db, NULL);
477 g_autoptr(GHashTable) all = g_hash_table_new(g_direct_hash, g_direct_equal);
478 const WacomStylus **all_styli = NULL;
479
480 g_assert(devices);
481
482 for (WacomDevice **d = devices; *d; d++) {
483 g_autofree const WacomStylus **styli = NULL;
484 int nstyli;
485
486 styli = libwacom_get_styli(*d, &nstyli);
487 for (int i = 0; i < nstyli; i++) {
488 g_hash_table_add(all, (gpointer)styli[i]);
489 }
490 }
491
492 all_styli = (const WacomStylus **)g_hash_table_get_keys_as_array(all, NULL);
493 g_hash_table_steal_all(all);
494
495 return all_styli;
496 }
497
498 static WacomDeviceDatabase *
499 load_database(void)
500 {
501 WacomDeviceDatabase *db;
502 const char *datadir;
503
504 datadir = getenv("LIBWACOM_DATA_DIR");
505 if (!datadir)
506 datadir = TOPSRCDIR "/data";
507
508 db = libwacom_database_new_for_path(datadir);
509 if (!db)
510 printf("Failed to load data from %s", datadir);
511
512 g_assert(db);
513 return db;
514 }
515
516 int
517 main(int argc,
518 char **argv)
519 {
520 WacomDeviceDatabase *db;
521 int rc;
522
523 g_test_init(&argc, &argv, NULL);
524 g_test_set_nonfatal_assertions();
525
526 db = load_database();
527 all_styli = assemble_styli(db);
528
529 for (const WacomStylus **s = all_styli; *s; s++)
530 setup_tests(*s);
531
532 rc = g_test_run();
533
534 free(all_styli);
535 libwacom_database_destroy(db);
536
537 return rc;
538 }
539
540 /* vim: set noexpandtab tabstop=8 shiftwidth=8: */