1 | /*
|
---|
2 | * Copyright (c) 2023 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTvvhhzccgggrERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <errno.h>
|
---|
30 | #include <mem.h>
|
---|
31 | #include <io/kbd_event.h>
|
---|
32 | #include <io/pos_event.h>
|
---|
33 | #include <pcut/pcut.h>
|
---|
34 | #include <ui/control.h>
|
---|
35 | #include <stdbool.h>
|
---|
36 | #include <types/ui/event.h>
|
---|
37 |
|
---|
38 | PCUT_INIT;
|
---|
39 |
|
---|
40 | PCUT_TEST_SUITE(control);
|
---|
41 |
|
---|
42 | static void test_ctl_destroy(void *);
|
---|
43 | static errno_t test_ctl_paint(void *);
|
---|
44 | static ui_evclaim_t test_ctl_kbd_event(void *, kbd_event_t *);
|
---|
45 | static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
|
---|
46 | static void test_ctl_unfocus(void *, unsigned);
|
---|
47 |
|
---|
48 | static ui_control_ops_t test_ctl_ops = {
|
---|
49 | .destroy = test_ctl_destroy,
|
---|
50 | .paint = test_ctl_paint,
|
---|
51 | .kbd_event = test_ctl_kbd_event,
|
---|
52 | .pos_event = test_ctl_pos_event,
|
---|
53 | .unfocus = test_ctl_unfocus
|
---|
54 | };
|
---|
55 |
|
---|
56 | /** Test response */
|
---|
57 | typedef struct {
|
---|
58 | /** Claim to return */
|
---|
59 | ui_evclaim_t claim;
|
---|
60 | /** Result code to return */
|
---|
61 | errno_t rc;
|
---|
62 |
|
---|
63 | /** @c true iff destroy was called */
|
---|
64 | bool destroy;
|
---|
65 |
|
---|
66 | /** @c true iff paint was called */
|
---|
67 | bool paint;
|
---|
68 |
|
---|
69 | /** @c true iff kbd_event was called */
|
---|
70 | bool kbd;
|
---|
71 | /** Keyboard event that was sent */
|
---|
72 | kbd_event_t kevent;
|
---|
73 |
|
---|
74 | /** @c true iff pos_event was called */
|
---|
75 | bool pos;
|
---|
76 | /** Position event that was sent */
|
---|
77 | pos_event_t pevent;
|
---|
78 |
|
---|
79 | /** @c true iff unfocus was called */
|
---|
80 | bool unfocus;
|
---|
81 | /** Number of remaining foci that was sent */
|
---|
82 | unsigned unfocus_nfocus;
|
---|
83 | } test_resp_t;
|
---|
84 |
|
---|
85 | /** Allocate and deallocate control */
|
---|
86 | PCUT_TEST(new_delete)
|
---|
87 | {
|
---|
88 | ui_control_t *control = NULL;
|
---|
89 | errno_t rc;
|
---|
90 |
|
---|
91 | rc = ui_control_new(&test_ctl_ops, NULL, &control);
|
---|
92 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
93 | PCUT_ASSERT_NOT_NULL(control);
|
---|
94 |
|
---|
95 | ui_control_delete(control);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** ui_control_delete() can take NULL argument (no-op) */
|
---|
99 | PCUT_TEST(delete_null)
|
---|
100 | {
|
---|
101 | ui_control_delete(NULL);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Test sending destroy request to control */
|
---|
105 | PCUT_TEST(destroy)
|
---|
106 | {
|
---|
107 | ui_control_t *control = NULL;
|
---|
108 | test_resp_t resp;
|
---|
109 | errno_t rc;
|
---|
110 |
|
---|
111 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
112 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
113 | PCUT_ASSERT_NOT_NULL(control);
|
---|
114 |
|
---|
115 | resp.rc = EOK;
|
---|
116 | resp.destroy = false;
|
---|
117 |
|
---|
118 | ui_control_destroy(control);
|
---|
119 | PCUT_ASSERT_TRUE(resp.destroy);
|
---|
120 | }
|
---|
121 |
|
---|
122 | /** Test sending paint request to control */
|
---|
123 | PCUT_TEST(paint)
|
---|
124 | {
|
---|
125 | ui_control_t *control = NULL;
|
---|
126 | test_resp_t resp;
|
---|
127 | errno_t rc;
|
---|
128 |
|
---|
129 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
130 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
131 | PCUT_ASSERT_NOT_NULL(control);
|
---|
132 |
|
---|
133 | resp.rc = EOK;
|
---|
134 | resp.paint = false;
|
---|
135 |
|
---|
136 | rc = ui_control_paint(control);
|
---|
137 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
---|
138 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
139 |
|
---|
140 | resp.rc = EINVAL;
|
---|
141 | resp.paint = false;
|
---|
142 |
|
---|
143 | rc = ui_control_paint(control);
|
---|
144 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
---|
145 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
146 |
|
---|
147 | ui_control_delete(control);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Test sending keyboard event to control */
|
---|
151 | PCUT_TEST(kbd_event)
|
---|
152 | {
|
---|
153 | ui_control_t *control = NULL;
|
---|
154 | test_resp_t resp;
|
---|
155 | kbd_event_t event;
|
---|
156 | ui_evclaim_t claim;
|
---|
157 | errno_t rc;
|
---|
158 |
|
---|
159 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
160 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
161 | PCUT_ASSERT_NOT_NULL(control);
|
---|
162 |
|
---|
163 | resp.claim = ui_claimed;
|
---|
164 | resp.kbd = false;
|
---|
165 | event.type = KEY_PRESS;
|
---|
166 | event.key = KC_2;
|
---|
167 | event.mods = KM_LSHIFT;
|
---|
168 | event.c = '@';
|
---|
169 |
|
---|
170 | claim = ui_control_kbd_event(control, &event);
|
---|
171 | PCUT_ASSERT_EQUALS(resp.claim, claim);
|
---|
172 | PCUT_ASSERT_TRUE(resp.kbd);
|
---|
173 | PCUT_ASSERT_EQUALS(resp.kevent.type, event.type);
|
---|
174 | PCUT_ASSERT_INT_EQUALS(resp.kevent.key, event.key);
|
---|
175 | PCUT_ASSERT_INT_EQUALS(resp.kevent.mods, event.mods);
|
---|
176 | PCUT_ASSERT_INT_EQUALS(resp.kevent.c, event.c);
|
---|
177 |
|
---|
178 | ui_control_delete(control);
|
---|
179 | }
|
---|
180 |
|
---|
181 | /** Test sending position event to control */
|
---|
182 | PCUT_TEST(pos_event)
|
---|
183 | {
|
---|
184 | ui_control_t *control = NULL;
|
---|
185 | test_resp_t resp;
|
---|
186 | pos_event_t event;
|
---|
187 | ui_evclaim_t claim;
|
---|
188 | errno_t rc;
|
---|
189 |
|
---|
190 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
191 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
192 | PCUT_ASSERT_NOT_NULL(control);
|
---|
193 |
|
---|
194 | resp.claim = ui_claimed;
|
---|
195 | resp.pos = false;
|
---|
196 | event.pos_id = 1;
|
---|
197 | event.type = POS_PRESS;
|
---|
198 | event.btn_num = 2;
|
---|
199 | event.hpos = 3;
|
---|
200 | event.vpos = 4;
|
---|
201 |
|
---|
202 | claim = ui_control_pos_event(control, &event);
|
---|
203 | PCUT_ASSERT_EQUALS(resp.claim, claim);
|
---|
204 | PCUT_ASSERT_TRUE(resp.pos);
|
---|
205 | PCUT_ASSERT_INT_EQUALS(resp.pevent.pos_id, event.pos_id);
|
---|
206 | PCUT_ASSERT_EQUALS(resp.pevent.type, event.type);
|
---|
207 | PCUT_ASSERT_INT_EQUALS(resp.pevent.btn_num, event.btn_num);
|
---|
208 | PCUT_ASSERT_INT_EQUALS(resp.pevent.hpos, event.hpos);
|
---|
209 | PCUT_ASSERT_INT_EQUALS(resp.pevent.vpos, event.vpos);
|
---|
210 |
|
---|
211 | ui_control_delete(control);
|
---|
212 | }
|
---|
213 |
|
---|
214 | /** Test sending unfocus to control */
|
---|
215 | PCUT_TEST(unfocus)
|
---|
216 | {
|
---|
217 | ui_control_t *control = NULL;
|
---|
218 | test_resp_t resp;
|
---|
219 | errno_t rc;
|
---|
220 |
|
---|
221 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
222 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
223 | PCUT_ASSERT_NOT_NULL(control);
|
---|
224 |
|
---|
225 | resp.unfocus = false;
|
---|
226 |
|
---|
227 | ui_control_unfocus(control, 42);
|
---|
228 | PCUT_ASSERT_TRUE(resp.unfocus);
|
---|
229 | PCUT_ASSERT_INT_EQUALS(42, resp.unfocus_nfocus);
|
---|
230 |
|
---|
231 | ui_control_delete(control);
|
---|
232 | }
|
---|
233 |
|
---|
234 | static void test_ctl_destroy(void *arg)
|
---|
235 | {
|
---|
236 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
237 |
|
---|
238 | resp->destroy = true;
|
---|
239 | }
|
---|
240 |
|
---|
241 | static errno_t test_ctl_paint(void *arg)
|
---|
242 | {
|
---|
243 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
244 |
|
---|
245 | resp->paint = true;
|
---|
246 | return resp->rc;
|
---|
247 | }
|
---|
248 |
|
---|
249 | static ui_evclaim_t test_ctl_kbd_event(void *arg, kbd_event_t *event)
|
---|
250 | {
|
---|
251 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
252 |
|
---|
253 | resp->kbd = true;
|
---|
254 | resp->kevent = *event;
|
---|
255 |
|
---|
256 | return resp->claim;
|
---|
257 | }
|
---|
258 |
|
---|
259 | static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
260 | {
|
---|
261 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
262 |
|
---|
263 | resp->pos = true;
|
---|
264 | resp->pevent = *event;
|
---|
265 |
|
---|
266 | return resp->claim;
|
---|
267 | }
|
---|
268 |
|
---|
269 | static void test_ctl_unfocus(void *arg, unsigned nfocus)
|
---|
270 | {
|
---|
271 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
272 |
|
---|
273 | resp->unfocus = true;
|
---|
274 | resp->unfocus_nfocus = nfocus;
|
---|
275 | }
|
---|
276 |
|
---|
277 | PCUT_EXPORT(control);
|
---|