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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
|
/*
* Copyright © 2019 Red Hat, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software
udo y and its documentation for any purpose is hereby granted without
* fee, provided that the above copyright notice appear in all copies
* and that both that copyright notice and this permission notice
* appear in supporting documentation, and that the name of Red Hat
* not be used in advertising or publicity pertaining to distribution
* of the software without specific, written prior permission. Red
* Hat makes no representations about the suitability of this software
* for any purpose. It is provided "as is" without express or implied
* warranty.
*
* THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
* NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#define _GNU_SOURCE
#include <dirent.h>
#include <fcntl.h>
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "libwacom.h"
static const WacomStylus **all_styli;
static void
test_type(gconstpointer data)
{
const WacomStylus *stylus = data;
switch (libwacom_stylus_get_type(stylus)) {
case WSTYLUS_GENERAL:
case WSTYLUS_INKING:
case WSTYLUS_AIRBRUSH:
case WSTYLUS_CLASSIC:
case WSTYLUS_MARKER:
case WSTYLUS_STROKE:
case WSTYLUS_PUCK:
case WSTYLUS_3D:
case WSTYLUS_MOBILE:
break;
case WSTYLUS_UNKNOWN:
default:
g_test_fail();
}
}
static void
test_mobile(gconstpointer data)
{
const WacomStylus *stylus = data;
g_assert_cmpint(libwacom_stylus_get_type(stylus), ==, WSTYLUS_MOBILE);
}
static void
test_eraser_type(gconstpointer data)
{
const WacomStylus *stylus = data;
switch (libwacom_stylus_get_eraser_type(stylus)) {
case WACOM_ERASER_NONE:
case WACOM_ERASER_INVERT:
case WACOM_ERASER_BUTTON:
break;
case WACOM_ERASER_UNKNOWN:
default:
g_test_fail();
}
}
static void
test_has_eraser(gconstpointer data)
{
const WacomStylus *stylus = data;
gboolean matching_eraser_found = FALSE;
g_autofree const WacomStylus **paired = NULL;
int count;
int i;
/* A stylus cannot be an eraser and have an eraser at the same time */
g_assert_true(libwacom_stylus_has_eraser(stylus));
g_assert_false(libwacom_stylus_is_eraser(stylus));
/* Search for the linked eraser */
paired = libwacom_stylus_get_paired_styli(stylus, &count);
g_assert_cmpint(count, >, 0);
for (i = 0; i < count; i++) {
const WacomStylus *s = paired[i];
if (libwacom_stylus_is_eraser(s)) {
matching_eraser_found = TRUE;
break;
}
}
g_assert_true(matching_eraser_found);
}
static void
test_eraser_link(const WacomStylus *stylus,
gboolean linked)
{
gboolean matching_stylus_found = FALSE;
g_autofree const WacomStylus **paired = NULL;
int count;
int i;
/* A stylus cannot be an eraser and have an eraser at the same time */
g_assert_false(libwacom_stylus_has_eraser(stylus));
g_assert_true(libwacom_stylus_is_eraser(stylus));
/* Verify the link count */
paired = libwacom_stylus_get_paired_styli(stylus, &count);
if (!linked) {
g_assert_cmpint(count, ==, 0);
return;
}
/* If we're supposed to be linked, ensure its to a non-eraser */
g_assert_cmpint(count, >, 0);
for (i = 0; i < count; i++) {
const WacomStylus *s = paired[i];
if (libwacom_stylus_has_eraser(s)) {
matching_stylus_found = TRUE;
break;
}
}
g_assert_true(matching_stylus_found);
}
static void
test_is_eraser_unlinked(gconstpointer data)
{
const WacomStylus *stylus = data;
test_eraser_link(stylus, FALSE);
}
static void
test_is_eraser_linked(gconstpointer data)
{
const WacomStylus *stylus = data;
test_eraser_link(stylus, TRUE);
}
static void
test_eraser_inverted(gconstpointer data)
{
const WacomStylus *stylus = data;
WacomEraserType eraser_type = libwacom_stylus_get_eraser_type(stylus);
g_assert_cmpint(eraser_type, ==, WACOM_ERASER_INVERT);
}
static void
test_eraser_button(gconstpointer data)
{
const WacomStylus *stylus = data;
WacomEraserType eraser_type = libwacom_stylus_get_eraser_type(stylus);
g_assert_cmpint(eraser_type, ==, WACOM_ERASER_BUTTON);
}
static void
test_puck(gconstpointer data)
{
const WacomStylus *stylus = data;
int has_wheel = libwacom_stylus_has_wheel(stylus);
int has_lens = libwacom_stylus_has_lens(stylus);
/* 4D mouse is the only one with neither, everything
* else has either wheel or lens */
if (libwacom_stylus_get_id(stylus) == 0x94) {
g_assert_false(has_wheel);
g_assert_false(has_lens);
} else {
g_assert_true(has_wheel != has_lens);
}
}
static void
test_tilt(gconstpointer data)
{
const WacomStylus *stylus = data;
gboolean has_tilt = libwacom_stylus_get_axes(stylus) & WACOM_AXIS_TYPE_TILT;
g_assert_true(has_tilt);
}
static void
test_no_tilt(gconstpointer data)
{
const WacomStylus *stylus = data;
gboolean has_tilt = libwacom_stylus_get_axes(stylus) & WACOM_AXIS_TYPE_TILT;
g_assert_false(has_tilt);
}
static void
test_pressure(gconstpointer data)
{
const WacomStylus *stylus = data;
gboolean has_pressure =
libwacom_stylus_get_axes(stylus) & WACOM_AXIS_TYPE_PRESSURE;
g_assert_true(has_pressure);
}
static void
test_no_pressure(gconstpointer data)
{
const WacomStylus *stylus = data;
gboolean has_pressure =
libwacom_stylus_get_axes(stylus) & WACOM_AXIS_TYPE_PRESSURE;
g_assert_false(has_pressure);
}
static void
test_distance(gconstpointer data)
{
const WacomStylus *stylus = data;
gboolean has_distance =
libwacom_stylus_get_axes(stylus) & WACOM_AXIS_TYPE_DISTANCE;
g_assert_true(has_distance);
}
static void
test_no_distance(gconstpointer data)
{
const WacomStylus *stylus = data;
gboolean has_distance =
libwacom_stylus_get_axes(stylus) & WACOM_AXIS_TYPE_DISTANCE;
g_assert_false(has_distance);
}
static void
test_name(gconstpointer data)
{
const WacomStylus *stylus = data;
g_assert_nonnull(libwacom_stylus_get_name(stylus));
}
static void
test_generic(gconstpointer data)
{
const WacomStylus *stylus = data;
g_assert_true(libwacom_stylus_is_generic(stylus));
}
static void
test_not_generic(gconstpointer data)
{
const WacomStylus *stylus = data;
g_assert_false(libwacom_stylus_is_generic(stylus));
}
static void
test_buttons(gconstpointer data)
{
const WacomStylus *stylus = data;
g_assert_cmpint(libwacom_stylus_get_num_buttons(stylus), >, 0);
}
static void
test_no_buttons(gconstpointer data)
{
const WacomStylus *stylus = data;
g_assert_cmpint(libwacom_stylus_get_num_buttons(stylus), ==, 0);
}
static void
test_mutually_paired(gconstpointer data)
{
const WacomStylus *stylus = data;
g_autofree const WacomStylus **stylus_pairings = NULL;
int count;
stylus_pairings = libwacom_stylus_get_paired_styli(stylus, &count);
for (int i = 0; i < count; i++) {
const WacomStylus *paired = stylus_pairings[i];
g_autofree const WacomStylus **counter_paired = NULL;
gboolean match_found = FALSE;
int pair_count;
g_assert_true(paired != stylus); /* can't be paired with itself */
counter_paired = libwacom_stylus_get_paired_styli(paired, &pair_count);
for (int j = 0; j < pair_count; j++) {
const WacomStylus *back_paired = counter_paired[j];
if (back_paired == stylus) {
match_found = TRUE;
break;
}
}
g_assert_true(match_found);
}
}
/* Wrapper function to make adding tests simpler. g_test requires
* a unique test case name so we assemble that from the test function and
* the stylus data.
*/
static inline void
_add_test(const WacomStylus *stylus,
GTestDataFunc func,
const char *funcname)
{
char buf[128];
const char *prefix;
/* tests must be test_foobar */
g_assert(strncmp(funcname, "test_", 5) == 0);
prefix = &funcname[5];
snprintf(buf,
128,
"/stylus/%s/%03x-%s",
prefix,
libwacom_stylus_get_id(stylus),
libwacom_stylus_get_name(stylus));
g_test_add_data_func(buf, stylus, func);
}
#define add_test(stylus, func_) \
_add_test(stylus, func_, #func_)
static void
setup_aes_tests(const WacomStylus *stylus)
{
add_test(stylus, test_mobile);
add_test(stylus, test_pressure);
add_test(stylus, test_no_distance);
if (libwacom_stylus_get_id(stylus) < 0x8000) {
add_test(stylus, test_no_tilt);
} else {
add_test(stylus, test_tilt);
}
if (libwacom_stylus_is_eraser(stylus)) {
add_test(stylus, test_is_eraser_unlinked);
add_test(stylus, test_eraser_button);
}
}
static void
setup_emr_tests(const WacomStylus *stylus)
{
switch (libwacom_stylus_get_id(stylus)) {
case 0xfffff: /* GENERIC_PEN_WITH_ERASER */
case 0xffffe: /* GENERIC_ERASER */
add_test(stylus, test_generic);
add_test(stylus, test_pressure);
add_test(stylus, test_distance);
add_test(stylus, test_tilt);
break;
case 0xffffd: /* GENERIC_PEN_NO_ERASER */
add_test(stylus, test_generic);
add_test(stylus, test_pressure);
add_test(stylus, test_no_distance);
add_test(stylus, test_no_tilt);
break;
case 0x006:
case 0x096:
case 0x097:
add_test(stylus, test_not_generic);
add_test(stylus, test_no_pressure);
add_test(stylus, test_distance);
add_test(stylus, test_no_tilt);
break;
case 0x007:
case 0x017:
case 0x094:
case 0x806:
add_test(stylus, test_not_generic);
add_test(stylus, test_no_pressure);
add_test(stylus, test_distance);
add_test(stylus, test_tilt);
break;
case 0x021:
case 0x8e2:
case 0x862:
add_test(stylus, test_not_generic);
add_test(stylus, test_pressure);
add_test(stylus, test_distance);
add_test(stylus, test_no_tilt);
break;
default:
add_test(stylus, test_not_generic);
add_test(stylus, test_pressure);
add_test(stylus, test_tilt);
add_test(stylus, test_distance);
break;
}
if (libwacom_stylus_is_eraser(stylus)) {
add_test(stylus, test_is_eraser_linked);
add_test(stylus, test_eraser_inverted);
}
}
static void
setup_tests(const WacomStylus *stylus)
{
add_test(stylus, test_name);
add_test(stylus, test_type);
/* Button checks */
switch (libwacom_stylus_get_type(stylus)) {
case WSTYLUS_PUCK:
add_test(stylus, test_puck);
add_test(stylus, test_buttons);
break;
case WSTYLUS_INKING:
case WSTYLUS_STROKE:
add_test(stylus, test_no_buttons);
break;
default:
switch (libwacom_stylus_get_id(stylus)) {
case 0x885:
case 0x8051:
add_test(stylus, test_no_buttons);
break;
default:
add_test(stylus, test_buttons);
}
}
/* Technology-specific tests */
if (libwacom_stylus_get_type(stylus) == WSTYLUS_MOBILE) {
setup_aes_tests(stylus);
} else {
setup_emr_tests(stylus);
}
if (libwacom_stylus_has_eraser(stylus))
add_test(stylus, test_has_eraser);
if (libwacom_stylus_is_eraser(stylus))
add_test(stylus, test_eraser_type);
add_test(stylus, test_mutually_paired);
}
/**
* Return a NULL-terminated list of all styli.
*
* libwacom only gives us the styli per-device so this is a bit more
* complicated than it should be.
*/
static const WacomStylus **
assemble_styli(WacomDeviceDatabase *db)
{
g_autofree WacomDevice **devices =
libwacom_list_devices_from_database(db, NULL);
g_autoptr(GHashTable) all = g_hash_table_new(g_direct_hash, g_direct_equal);
const WacomStylus **all_styli = NULL;
g_assert(devices);
for (WacomDevice **d = devices; *d; d++) {
g_autofree const WacomStylus **styli = NULL;
int nstyli;
styli = libwacom_get_styli(*d, &nstyli);
for (int i = 0; i < nstyli; i++) {
g_hash_table_add(all, (gpointer)styli[i]);
}
}
all_styli = (const WacomStylus **)g_hash_table_get_keys_as_array(all, NULL);
g_hash_table_steal_all(all);
return all_styli;
}
static WacomDeviceDatabase *
load_database(void)
{
WacomDeviceDatabase *db;
const char *datadir;
datadir = getenv("LIBWACOM_DATA_DIR");
if (!datadir)
datadir = TOPSRCDIR "/data";
db = libwacom_database_new_for_path(datadir);
if (!db)
printf("Failed to load data from %s", datadir);
g_assert(db);
return db;
}
int
main(int argc,
char **argv)
{
WacomDeviceDatabase *db;
int rc;
g_test_init(&argc, &argv, NULL);
g_test_set_nonfatal_assertions();
db = load_database();
all_styli = assemble_styli(db);
for (const WacomStylus **s = all_styli; *s; s++)
setup_tests(*s);
rc = g_test_run();
free(all_styli);
libwacom_database_destroy(db);
return rc;
}
/* vim: set noexpandtab tabstop=8 shiftwidth=8: */
|