summaryrefslogtreecommitdiff
path: root/test/check-transform.c
blob: 3e2283fae0464dea6465c289cc52c4929acaa708 (plain)
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
#include <check.h>
#include <utouch/frame-mtdev.h>
#include <grail.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

struct test_data {
	struct evemu_device *evemu;
	utouch_frame_handle fh;
	grail_handle ge;
	FILE *fp;
	struct timeval evtime;
};

static int near(const struct grail_coord *p, float x, float y)
{
	double dx = p->x - x;
	double dy = p->y - y;
	return dx * dx + dy * dy < 1e-8 * (x * x + y * y);
}

static int init_test_data(struct test_data *data, const char *path)
{
	memset(data, 0, sizeof(*data));

	data->fp = fopen(path, "r");
	if (!data->fp)
		return -1;

	data->evemu = evemu_new(NULL);
	if (!data->evemu || evemu_read(data->evemu, data->fp) <= 0)
		return -1;

	data->fh = utouch_frame_new_engine(100, 32, 100);
	if (!data->fh || utouch_frame_init_mtdev(data->fh, data->evemu))
		return -1;

	data->ge = grail_new(data->fh, 10, 0);
	if (!data->ge)
		return -1;

	return 0;
}

static void term_test_data(struct test_data *data)
{
	grail_delete(data->ge);
	utouch_frame_delete_engine(data->fh);
	evemu_delete(data->evemu);
	fclose(data->fp);
}

static int get_event(struct test_data *data, struct input_event *ev)
{
	return evemu_read_event_realtime(data->fp, ev, &data->evtime) > 0;
}

static const struct grail_element *get_element(struct test_data *data,
					       const struct input_event *ev)
{
	const struct utouch_frame *touch;
	const struct grail_frame *frame;

	touch = utouch_frame_pump_mtdev(data->fh, ev);
	if (!touch)
		return 0;
	frame = grail_pump_frame(data->ge, touch);
	if (!frame || !frame->num_ongoing)
		return 0;

	return frame->ongoing[frame->num_ongoing - 1];
}

START_TEST(device_coordinates)
{
	struct test_data data;
	struct input_event ev;
	const struct grail_element *slot;
	struct grail_coord pos = { 0, 0 };

	fail_if(init_test_data(&data, "io/evemu/one-tap.evemu"));

	while (get_event(&data, &ev)) {
		slot = get_element(&data, &ev);
		if (slot)
			pos = slot->center;
	}

	fail_unless(near(&pos, 17087, 14790));

	term_test_data(&data);
}
END_TEST

START_TEST(mapped_coordinates)
{
	static const struct grail_coord min = { 0, 0 }, max = { 1.6, 1 };
	struct test_data data;
	struct input_event ev;
	const struct grail_element *slot;
	struct grail_coord pos = { 0, 0 };

	fail_if(init_test_data(&data, "io/evemu/one-tap.evemu"));
	grail_set_bbox(data.ge, &min, &max);

	while (get_event(&data, &ev)) {
		slot = get_element(&data, &ev);
		if (slot)
			pos = slot->center;
	}

	fail_unless(near(&pos, 0.834303, 0.451338));

	term_test_data(&data);
}
END_TEST

Suite *make_transform_suite()
{
	Suite *s = suite_create("grail-solver");
	TCase *test;

	test = tcase_create("device_coordinates");
	tcase_add_test(test, device_coordinates);
	suite_add_tcase(s, test);

	test = tcase_create("mapped_coordinates");
	tcase_add_test(test, mapped_coordinates);
	suite_add_tcase(s, test);

	return s;
}