File: test-tablet-validity.c

package info (click to toggle)
libwacom 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,388 kB
  • sloc: ansic: 7,062; python: 2,527; sh: 65; makefile: 21
file content (516 lines) | stat: -rw-r--r-- 12,643 bytes parent folder | download
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
/*
 * Copyright © 2012 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.
 *
 * Authors:
 *	Peter Hutterer (peter.hutterer@redhat.com)
 */

#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"

typedef int (*NumModesFn)(const WacomDevice *device);

static gboolean
buttons_have_direction(WacomDevice *device)
{
	char button;
	int num_buttons;

	num_buttons = libwacom_get_num_buttons(device);
	if (num_buttons == 0)
		return TRUE;

	for (button = 'A'; button < 'A' + num_buttons; button++) {
		WacomButtonFlags flags;
		flags = libwacom_get_button_flag(device, button);
		if (!(flags & WACOM_BUTTON_DIRECTION))
			return FALSE;
	}

	return TRUE;
}

static gboolean
match_mode_switch(WacomDevice *device,
		  NumModesFn get_num_modes,
		  WacomButtonFlags flag)
{
	char button;
	int num_buttons;
	int num_switches;
	int num_modes;

	num_buttons = libwacom_get_num_buttons(device);
	num_modes = get_num_modes(device);
	num_switches = 0;

	for (button = 'A'; button < 'A' + num_buttons; button++) {
		WacomButtonFlags flags;
		flags = libwacom_get_button_flag(device, button);

		if (flags & flag)
			num_switches++;
	}

	/*
	 * If we have more than one mode-switch button, then the
	 * number of modes must match the number of mode-switch buttons.
	 */
	if (num_switches > 1 && num_modes != num_switches)
		return FALSE;

	/*
	 * If we have more than one mode, then we should find at least
	 * one mode-switch button.
	 */
	if (num_modes > 1 && num_switches == 0)
		return FALSE;

	return TRUE;
}

static gboolean
tablet_has_lr_buttons(WacomDevice *device)
{
	int nleft = 0;
	int nright = 0;
	int num_buttons;
	char button;

	num_buttons = libwacom_get_num_buttons(device);

	for (button = 'A'; button < 'A' + num_buttons; button++) {
		WacomButtonFlags f = libwacom_get_button_flag(device, button);
		if (f & WACOM_BUTTON_POSITION_LEFT)
			nleft++;
		if (f & WACOM_BUTTON_POSITION_RIGHT)
			nright++;
	}

	if (nleft > 0 || nright > 0)
		return TRUE;

	return FALSE;
}

static void
test_class(gconstpointer data)
{
	WacomDevice *device = (WacomDevice *)data;
	WacomClass cls;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
	cls = libwacom_get_class(device);
#pragma GCC diagnostic pop
	switch (cls) {
	case WCLASS_BAMBOO:
	case WCLASS_ISDV4:
	case WCLASS_PEN_DISPLAYS:
	case WCLASS_GRAPHIRE:
	case WCLASS_REMOTE:
	case WCLASS_INTUOS:
	case WCLASS_INTUOS2:
	case WCLASS_INTUOS3:
	case WCLASS_INTUOS4:
	case WCLASS_INTUOS5:
	case WCLASS_CINTIQ:
		break;
	default:
		g_test_fail();
		break;
	}
}

static void
test_name(gconstpointer data)
{
	WacomDevice *device = (WacomDevice *)data;

	g_assert_nonnull(libwacom_get_name(device));
	g_assert_cmpstr(libwacom_get_name(device), !=, "");
}

static void
assert_vidpid(WacomBusType bus,
	      int vid,
	      int pid)
{
	switch (bus) {
	case WBUSTYPE_SERIAL:
		g_assert_cmpint(vid, >=, 0);
		g_assert_cmpint(pid, >=, 0);
		break;
	case WBUSTYPE_USB:
		if (vid == 0x056A)
			g_assert_cmpint(pid, !=, 0x84); /* wireless dongle */
		g_assert_cmpint(vid, >, 0);
		g_assert_cmpint(pid, >, 0);
		break;
	case WBUSTYPE_BLUETOOTH:
	case WBUSTYPE_I2C:
		g_assert_cmpint(vid, >, 0);
		g_assert_cmpint(pid, >, 0);
		break;
	case WBUSTYPE_UNKNOWN:
	default:
		g_test_fail();
		break;
	}
}

static void
test_vidpid(gconstpointer data)
{
	WacomDevice *device = (WacomDevice *)data;
	WacomBusType bus = libwacom_get_bustype(device);

	assert_vidpid(bus,
		      libwacom_get_vendor_id(device),
		      libwacom_get_product_id(device));
}

static void
test_matches(gconstpointer data)
{
	WacomDevice *device = (WacomDevice *)data;

	g_assert_nonnull(libwacom_get_match(device));
	g_assert_nonnull(libwacom_get_matches(device));
}

static void
test_matches_vidpid(gconstpointer data)
{
	WacomDevice *device = (WacomDevice *)data;
	const WacomMatch **match = libwacom_get_matches(device);

	while (*match) {
		WacomBusType bus = libwacom_match_get_bustype(*match);
		assert_vidpid(bus,
			      libwacom_match_get_vendor_id(*match),
			      libwacom_match_get_product_id(*match));
		match++;
	}
}

static void
test_dimensions(gconstpointer data)
{
	WacomDevice *device = (WacomDevice *)data;
	unsigned int flags = libwacom_get_integration_flags(device);

	switch (flags) {
	case WACOM_DEVICE_INTEGRATED_REMOTE:
		g_assert_cmpint(libwacom_get_width(device), ==, 0);
		g_assert_cmpint(libwacom_get_height(device), ==, 0);
		break;
	case WACOM_DEVICE_INTEGRATED_SYSTEM | WACOM_DEVICE_INTEGRATED_DISPLAY:
		/* ISDV4 devices may not always have a width/height set */
		break;
	default:
		g_assert_cmpint(libwacom_get_width(device), >, 0);
		g_assert_cmpint(libwacom_get_height(device), >, 0);
		break;
	}
}

static void
test_buttons(gconstpointer data)
{
	WacomDevice *device = (WacomDevice *)data;

	g_assert_cmpint(libwacom_get_num_buttons(device), >=, 0);
	g_assert_true(buttons_have_direction(device));

	if (libwacom_is_reversible(device) && libwacom_get_num_buttons(device) > 0)
		g_assert_true(tablet_has_lr_buttons(device));
}

static void
test_styli(gconstpointer data)
{
	WacomDevice *device = (WacomDevice *)data;
	int nstylus_ids, nstyli;
	g_autofree const WacomStylus **styli = NULL;
	const int *stylus_ids;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
	stylus_ids = libwacom_get_supported_styli(device, &nstylus_ids);
#pragma GCC diagnostic pop
	styli = libwacom_get_styli(device, &nstyli);

	g_assert_cmpint(nstyli, >, 0);
	g_assert_cmpint(nstyli, ==, nstylus_ids);
	g_assert_nonnull(styli);
	g_assert_nonnull(stylus_ids);
	g_assert_null(styli[nstyli]); /* NULL-terminated list */

	for (int i = 0; i < nstyli; i++) {
		const WacomStylus *stylus = styli[i];
		gboolean found = FALSE;
		for (int j = 0; !found && j < nstylus_ids; j++) {
			found = stylus_ids[j] == libwacom_stylus_get_id(stylus);
		}
		g_assert_true(found);
	}
}

static void
test_no_styli(gconstpointer data)
{
	WacomDevice *device = (WacomDevice *)data;
	int nstylus_ids, nstyli;
	g_autofree const WacomStylus **styli = NULL;
	const int *stylus_ids;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
	stylus_ids = libwacom_get_supported_styli(device, &nstylus_ids);
#pragma GCC diagnostic pop
	styli = libwacom_get_styli(device, &nstyli);

	g_assert_cmpint(nstyli, ==, 0);
	g_assert_cmpint(nstylus_ids, ==, 0);
	g_assert_nonnull(styli);
	g_assert_null(stylus_ids);
	g_assert_null(styli[0]); /* NULL-terminated list */
}

static void
test_realstylus(gconstpointer data)
{
	WacomDevice *device = (WacomDevice *)data;
	int nstylus_ids, nstyli;
	g_autofree const WacomStylus **styli = NULL;
	const int *stylus_ids;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
	stylus_ids = libwacom_get_supported_styli(device, &nstylus_ids);
#pragma GCC diagnostic pop
	styli = libwacom_get_styli(device, &nstyli);

	for (int i = 0; i < nstylus_ids; i++) {
		g_assert_cmpint(stylus_ids[i], !=, WACOM_STYLUS_FALLBACK_ID);
		g_assert_cmpint(stylus_ids[i], !=, WACOM_ERASER_FALLBACK_ID);
	}

	for (int i = 0; i < nstyli; i++) {
		const WacomStylus *stylus = styli[i];
		g_assert_cmpint(libwacom_stylus_get_id(stylus),
				!=,
				WACOM_STYLUS_FALLBACK_ID);
		g_assert_cmpint(libwacom_stylus_get_id(stylus),
				!=,
				WACOM_ERASER_FALLBACK_ID);
	}
}

static void
test_rings(gconstpointer data)
{
	WacomDevice *device = (WacomDevice *)data;

	g_assert_cmpint(libwacom_get_ring_num_modes(device), >=, 0);
	g_assert_cmpint(libwacom_get_ring2_num_modes(device), >=, 0);

	if (libwacom_get_num_rings(device) >= 1)
		g_assert_true(match_mode_switch(device,
						libwacom_get_ring_num_modes,
						WACOM_BUTTON_RING_MODESWITCH));
	if (libwacom_get_num_rings(device) >= 2)
		g_assert_true(match_mode_switch(device,
						libwacom_get_ring2_num_modes,
						WACOM_BUTTON_RING2_MODESWITCH));
}

static void
test_strips(gconstpointer data)
{
	WacomDevice *device = (WacomDevice *)data;

	g_assert_cmpint(libwacom_get_num_strips(device), >=, 0);
	g_assert_cmpint(libwacom_get_strips_num_modes(device), >=, 0);

	if (libwacom_get_num_strips(device) > 0)
		g_assert_true(match_mode_switch(device,
						libwacom_get_strips_num_modes,
						WACOM_BUTTON_TOUCHSTRIP_MODESWITCH));
}

static void
test_dials(gconstpointer data)
{
	WacomDevice *device = (WacomDevice *)data;

	g_assert_cmpint(libwacom_get_num_dials(device), >=, 0);
	g_assert_cmpint(libwacom_get_dial_num_modes(device), >=, 0);

	if (libwacom_get_num_dials(device) > 0)
		g_assert_true(match_mode_switch(device,
						libwacom_get_dial_num_modes,
						WACOM_BUTTON_DIAL_MODESWITCH));

	if (libwacom_get_num_dials(device) > 1)
		g_assert_true(match_mode_switch(device,
						libwacom_get_dial2_num_modes,
						WACOM_BUTTON_DIAL2_MODESWITCH));
}

/* 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 tablet data.
 */
static inline void
_add_test(WacomDevice *device,
	  GTestDataFunc func,
	  const char *funcname)
{
	char buf[128];
	static int count; /* guarantee unique test case names */
	const char *prefix;

	/* tests must be test_foobar */
	g_assert(strncmp(funcname, "test_", 5) == 0);
	prefix = &funcname[5];

	snprintf(buf,
		 128,
		 "/tablet/%s/%03d/%04x:%04x-%s",
		 prefix,
		 ++count,
		 libwacom_get_vendor_id(device),
		 libwacom_get_product_id(device),
		 libwacom_get_name(device));
	g_test_add_data_func(buf, device, func);
}
#define add_test(device_, func_) \
	_add_test(device_, func_, #func_)

static void
setup_tests(WacomDevice *device)
{
	const char *name;
	WacomClass cls;

	name = libwacom_get_name(device);
	if (g_str_equal(name, "Generic"))
		return;

	add_test(device, test_class);
	add_test(device, test_name);
	add_test(device, test_vidpid);
	add_test(device, test_matches);
	add_test(device, test_matches_vidpid);
	add_test(device, test_buttons);
	add_test(device, test_rings);
	add_test(device, test_strips);
	add_test(device, test_dials);

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
	cls = libwacom_get_class(device);
#pragma GCC diagnostic pop

	add_test(device, test_dimensions);

	/* FIXME: we force the generic pen for these, should add a test */
	if (libwacom_has_stylus(device))
		add_test(device, test_styli);
	else
		add_test(device, test_no_styli);

	switch (cls) {
	case WCLASS_INTUOS:
	case WCLASS_INTUOS2:
	case WCLASS_INTUOS3:
	case WCLASS_INTUOS4:
	case WCLASS_INTUOS5:
	case WCLASS_CINTIQ:
		add_test(device, test_realstylus);
		break;
	default:
		break;
	}
}

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;
	WacomDevice **devices;
	int rc;

	g_test_init(&argc, &argv, NULL);
	g_test_set_nonfatal_assertions();

	db = load_database();

	devices = libwacom_list_devices_from_database(db, NULL);
	g_assert(devices);
	g_assert(*devices);

	for (WacomDevice **device = devices; *device; device++)
		setup_tests(*device);

	rc = g_test_run();

	libwacom_database_destroy(db);
	free(devices);

	return rc;
}

/* vim: set noexpandtab tabstop=8 shiftwidth=8: */