summaryrefslogtreecommitdiff
path: root/test/check-transform.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/check-transform.c')
-rw-r--r--test/check-transform.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/test/check-transform.c b/test/check-transform.c
new file mode 100644
index 0000000..3e2283f
--- /dev/null
+++ b/test/check-transform.c
@@ -0,0 +1,137 @@
1#include <check.h>
2#include <utouch/frame-mtdev.h>
3#include <grail.h>
4#include <string.h>
5#include <stdio.h>
6#include <unistd.h>
7#include <fcntl.h>
8#include <stdlib.h>
9#include <time.h>
10#include <math.h>
11
12struct test_data {
13 struct evemu_device *evemu;
14 utouch_frame_handle fh;
15 grail_handle ge;
16 FILE *fp;
17 struct timeval evtime;
18};
19
20static int near(const struct grail_coord *p, float x, float y)
21{
22 double dx = p->x - x;
23 double dy = p->y - y;
24 return dx * dx + dy * dy < 1e-8 * (x * x + y * y);
25}
26
27static int init_test_data(struct test_data *data, const char *path)
28{
29 memset(data, 0, sizeof(*data));
30
31 data->fp = fopen(path, "r");
32 if (!data->fp)
33 return -1;
34
35 data->evemu = evemu_new(NULL);
36 if (!data->evemu || evemu_read(data->evemu, data->fp) <= 0)
37 return -1;
38
39 data->fh = utouch_frame_new_engine(100, 32, 100);
40 if (!data->fh || utouch_frame_init_mtdev(data->fh, data->evemu))
41 return -1;
42
43 data->ge = grail_new(data->fh, 10, 0);
44 if (!data->ge)
45 return -1;
46
47 return 0;
48}
49
50static void term_test_data(struct test_data *data)
51{
52 grail_delete(data->ge);
53 utouch_frame_delete_engine(data->fh);
54 evemu_delete(data->evemu);
55 fclose(data->fp);
56}
57
58static int get_event(struct test_data *data, struct input_event *ev)
59{
60 return evemu_read_event_realtime(data->fp, ev, &data->evtime) > 0;
61}
62
63static const struct grail_element *get_element(struct test_data *data,
64 const struct input_event *ev)
65{
66 const struct utouch_frame *touch;
67 const struct grail_frame *frame;
68
69 touch = utouch_frame_pump_mtdev(data->fh, ev);
70 if (!touch)
71 return 0;
72 frame = grail_pump_frame(data->ge, touch);
73 if (!frame || !frame->num_ongoing)
74 return 0;
75
76 return frame->ongoing[frame->num_ongoing - 1];
77}
78
79START_TEST(device_coordinates)
80{
81 struct test_data data;
82 struct input_event ev;
83 const struct grail_element *slot;
84 struct grail_coord pos = { 0, 0 };
85
86 fail_if(init_test_data(&data, "io/evemu/one-tap.evemu"));
87
88 while (get_event(&data, &ev)) {
89 slot = get_element(&data, &ev);
90 if (slot)
91 pos = slot->center;
92 }
93
94 fail_unless(near(&pos, 17087, 14790));
95
96 term_test_data(&data);
97}
98END_TEST
99
100START_TEST(mapped_coordinates)
101{
102 static const struct grail_coord min = { 0, 0 }, max = { 1.6, 1 };
103 struct test_data data;
104 struct input_event ev;
105 const struct grail_element *slot;
106 struct grail_coord pos = { 0, 0 };
107
108 fail_if(init_test_data(&data, "io/evemu/one-tap.evemu"));
109 grail_set_bbox(data.ge, &min, &max);
110
111 while (get_event(&data, &ev)) {
112 slot = get_element(&data, &ev);
113 if (slot)
114 pos = slot->center;
115 }
116
117 fail_unless(near(&pos, 0.834303, 0.451338));
118
119 term_test_data(&data);
120}
121END_TEST
122
123Suite *make_transform_suite()
124{
125 Suite *s = suite_create("grail-solver");
126 TCase *test;
127
128 test = tcase_create("device_coordinates");
129 tcase_add_test(test, device_coordinates);
130 suite_add_tcase(s, test);
131
132 test = tcase_create("mapped_coordinates");
133 tcase_add_test(test, mapped_coordinates);
134 suite_add_tcase(s, test);
135
136 return s;
137}