1 | /*
|
---|
2 | * Copyright (c) 2021 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 *);
|
---|
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 | } test_resp_t;
|
---|
82 |
|
---|
83 | /** Allocate and deallocate control */
|
---|
84 | PCUT_TEST(new_delete)
|
---|
85 | {
|
---|
86 | ui_control_t *control = NULL;
|
---|
87 | errno_t rc;
|
---|
88 |
|
---|
89 | rc = ui_control_new(&test_ctl_ops, NULL, &control);
|
---|
90 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
91 | PCUT_ASSERT_NOT_NULL(control);
|
---|
92 |
|
---|
93 | ui_control_delete(control);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** ui_control_delete() can take NULL argument (no-op) */
|
---|
97 | PCUT_TEST(delete_null)
|
---|
98 | {
|
---|
99 | ui_control_delete(NULL);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** Test sending destroy request to control */
|
---|
103 | PCUT_TEST(destroy)
|
---|
104 | {
|
---|
105 | ui_control_t *control = NULL;
|
---|
106 | test_resp_t resp;
|
---|
107 | errno_t rc;
|
---|
108 |
|
---|
109 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
110 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
111 | PCUT_ASSERT_NOT_NULL(control);
|
---|
112 |
|
---|
113 | resp.rc = EOK;
|
---|
114 | resp.destroy = false;
|
---|
115 |
|
---|
116 | ui_control_destroy(control);
|
---|
117 | PCUT_ASSERT_TRUE(resp.destroy);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Test sending paint request to control */
|
---|
121 | PCUT_TEST(paint)
|
---|
122 | {
|
---|
123 | ui_control_t *control = NULL;
|
---|
124 | test_resp_t resp;
|
---|
125 | errno_t rc;
|
---|
126 |
|
---|
127 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
128 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
129 | PCUT_ASSERT_NOT_NULL(control);
|
---|
130 |
|
---|
131 | resp.rc = EOK;
|
---|
132 | resp.paint = false;
|
---|
133 |
|
---|
134 | rc = ui_control_paint(control);
|
---|
135 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
---|
136 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
137 |
|
---|
138 | resp.rc = EINVAL;
|
---|
139 | resp.paint = false;
|
---|
140 |
|
---|
141 | rc = ui_control_paint(control);
|
---|
142 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
---|
143 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
144 |
|
---|
145 | ui_control_delete(control);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Test sending keyboard event to control */
|
---|
149 | PCUT_TEST(kbd_event)
|
---|
150 | {
|
---|
151 | ui_control_t *control = NULL;
|
---|
152 | test_resp_t resp;
|
---|
153 | kbd_event_t event;
|
---|
154 | ui_evclaim_t claim;
|
---|
155 | errno_t rc;
|
---|
156 |
|
---|
157 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
158 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
159 | PCUT_ASSERT_NOT_NULL(control);
|
---|
160 |
|
---|
161 | resp.claim = ui_claimed;
|
---|
162 | resp.kbd = false;
|
---|
163 | event.type = KEY_PRESS;
|
---|
164 | event.key = KC_2;
|
---|
165 | event.mods = KM_LSHIFT;
|
---|
166 | event.c = '@';
|
---|
167 |
|
---|
168 | claim = ui_control_kbd_event(control, &event);
|
---|
169 | PCUT_ASSERT_EQUALS(resp.claim, claim);
|
---|
170 | PCUT_ASSERT_TRUE(resp.kbd);
|
---|
171 | PCUT_ASSERT_EQUALS(resp.kevent.type, event.type);
|
---|
172 | PCUT_ASSERT_INT_EQUALS(resp.kevent.key, event.key);
|
---|
173 | PCUT_ASSERT_INT_EQUALS(resp.kevent.mods, event.mods);
|
---|
174 | PCUT_ASSERT_INT_EQUALS(resp.kevent.c, event.c);
|
---|
175 |
|
---|
176 | ui_control_delete(control);
|
---|
177 | }
|
---|
178 |
|
---|
179 | /** Test sending position event to control */
|
---|
180 | PCUT_TEST(pos_event)
|
---|
181 | {
|
---|
182 | ui_control_t *control = NULL;
|
---|
183 | test_resp_t resp;
|
---|
184 | pos_event_t event;
|
---|
185 | ui_evclaim_t claim;
|
---|
186 | errno_t rc;
|
---|
187 |
|
---|
188 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
189 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
190 | PCUT_ASSERT_NOT_NULL(control);
|
---|
191 |
|
---|
192 | resp.claim = ui_claimed;
|
---|
193 | resp.pos = false;
|
---|
194 | event.pos_id = 1;
|
---|
195 | event.type = POS_PRESS;
|
---|
196 | event.btn_num = 2;
|
---|
197 | event.hpos = 3;
|
---|
198 | event.vpos = 4;
|
---|
199 |
|
---|
200 | claim = ui_control_pos_event(control, &event);
|
---|
201 | PCUT_ASSERT_EQUALS(resp.claim, claim);
|
---|
202 | PCUT_ASSERT_TRUE(resp.pos);
|
---|
203 | PCUT_ASSERT_INT_EQUALS(resp.pevent.pos_id, event.pos_id);
|
---|
204 | PCUT_ASSERT_EQUALS(resp.pevent.type, event.type);
|
---|
205 | PCUT_ASSERT_INT_EQUALS(resp.pevent.btn_num, event.btn_num);
|
---|
206 | PCUT_ASSERT_INT_EQUALS(resp.pevent.hpos, event.hpos);
|
---|
207 | PCUT_ASSERT_INT_EQUALS(resp.pevent.vpos, event.vpos);
|
---|
208 |
|
---|
209 | ui_control_delete(control);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /** Test sending unfocus to control */
|
---|
213 | PCUT_TEST(unfocus)
|
---|
214 | {
|
---|
215 | ui_control_t *control = NULL;
|
---|
216 | test_resp_t resp;
|
---|
217 | errno_t rc;
|
---|
218 |
|
---|
219 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
220 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
221 | PCUT_ASSERT_NOT_NULL(control);
|
---|
222 |
|
---|
223 | resp.unfocus = false;
|
---|
224 |
|
---|
225 | ui_control_unfocus(control);
|
---|
226 | PCUT_ASSERT_TRUE(resp.unfocus);
|
---|
227 |
|
---|
228 | ui_control_delete(control);
|
---|
229 | }
|
---|
230 |
|
---|
231 | static void test_ctl_destroy(void *arg)
|
---|
232 | {
|
---|
233 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
234 |
|
---|
235 | resp->destroy = true;
|
---|
236 | }
|
---|
237 |
|
---|
238 | static errno_t test_ctl_paint(void *arg)
|
---|
239 | {
|
---|
240 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
241 |
|
---|
242 | resp->paint = true;
|
---|
243 | return resp->rc;
|
---|
244 | }
|
---|
245 |
|
---|
246 | static ui_evclaim_t test_ctl_kbd_event(void *arg, kbd_event_t *event)
|
---|
247 | {
|
---|
248 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
249 |
|
---|
250 | resp->kbd = true;
|
---|
251 | resp->kevent = *event;
|
---|
252 |
|
---|
253 | return resp->claim;
|
---|
254 | }
|
---|
255 |
|
---|
256 | static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
257 | {
|
---|
258 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
259 |
|
---|
260 | resp->pos = true;
|
---|
261 | resp->pevent = *event;
|
---|
262 |
|
---|
263 | return resp->claim;
|
---|
264 | }
|
---|
265 |
|
---|
266 | static void test_ctl_unfocus(void *arg)
|
---|
267 | {
|
---|
268 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
269 |
|
---|
270 | resp->unfocus = true;
|
---|
271 | }
|
---|
272 |
|
---|
273 | PCUT_EXPORT(control);
|
---|