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 INTERRUPTION) 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 <gfx/context.h>
|
---|
30 | #include <gfx/coord.h>
|
---|
31 | #include <gfx/render.h>
|
---|
32 | #include <io/kbd_event.h>
|
---|
33 | #include <io/pos_event.h>
|
---|
34 | #include <mem.h>
|
---|
35 | #include <pcut/pcut.h>
|
---|
36 | #include <stdbool.h>
|
---|
37 | #include <ui/control.h>
|
---|
38 | #include <ui/popup.h>
|
---|
39 | #include <ui/resource.h>
|
---|
40 | #include <ui/ui.h>
|
---|
41 | #include <ui/window.h>
|
---|
42 | #include "../private/popup.h"
|
---|
43 | #include "../private/window.h"
|
---|
44 |
|
---|
45 | PCUT_INIT;
|
---|
46 |
|
---|
47 | PCUT_TEST_SUITE(popup);
|
---|
48 |
|
---|
49 | static void test_popup_close(ui_popup_t *, void *);
|
---|
50 | static void test_popup_kbd(ui_popup_t *, void *, kbd_event_t *);
|
---|
51 | static errno_t test_popup_paint(ui_popup_t *, void *);
|
---|
52 | static void test_popup_pos(ui_popup_t *, void *, pos_event_t *);
|
---|
53 |
|
---|
54 | static ui_popup_cb_t test_popup_cb = {
|
---|
55 | .close = test_popup_close,
|
---|
56 | .kbd = test_popup_kbd,
|
---|
57 | .paint = test_popup_paint,
|
---|
58 | .pos = test_popup_pos,
|
---|
59 | };
|
---|
60 |
|
---|
61 | static ui_popup_cb_t dummy_popup_cb = {
|
---|
62 | };
|
---|
63 |
|
---|
64 | static errno_t test_ctl_paint(void *);
|
---|
65 | static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
|
---|
66 | static void test_ctl_unfocus(void *, unsigned);
|
---|
67 |
|
---|
68 | static ui_control_ops_t test_ctl_ops = {
|
---|
69 | .paint = test_ctl_paint,
|
---|
70 | .pos_event = test_ctl_pos_event,
|
---|
71 | .unfocus = test_ctl_unfocus
|
---|
72 | };
|
---|
73 |
|
---|
74 | typedef struct {
|
---|
75 | errno_t rc;
|
---|
76 | bool close;
|
---|
77 | bool focus;
|
---|
78 | bool kbd;
|
---|
79 | kbd_event_t kbd_event;
|
---|
80 | bool paint;
|
---|
81 | bool pos;
|
---|
82 | pos_event_t pos_event;
|
---|
83 | bool unfocus;
|
---|
84 | } test_cb_resp_t;
|
---|
85 |
|
---|
86 | typedef struct {
|
---|
87 | errno_t rc;
|
---|
88 | ui_evclaim_t claim;
|
---|
89 | bool paint;
|
---|
90 | bool pos;
|
---|
91 | pos_event_t pos_event;
|
---|
92 | bool unfocus;
|
---|
93 | unsigned unfocus_nfocus;
|
---|
94 | } test_ctl_resp_t;
|
---|
95 |
|
---|
96 | /** Create and destroy popup window */
|
---|
97 | PCUT_TEST(create_destroy)
|
---|
98 | {
|
---|
99 | errno_t rc;
|
---|
100 | ui_t *ui = NULL;
|
---|
101 | ui_wnd_params_t wparams;
|
---|
102 | ui_window_t *window = NULL;
|
---|
103 | ui_popup_params_t params;
|
---|
104 | ui_popup_t *popup = NULL;
|
---|
105 |
|
---|
106 | rc = ui_create_disp(NULL, &ui);
|
---|
107 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
108 |
|
---|
109 | ui_wnd_params_init(&wparams);
|
---|
110 | wparams.caption = "Hello";
|
---|
111 |
|
---|
112 | rc = ui_window_create(ui, &wparams, &window);
|
---|
113 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
114 | PCUT_ASSERT_NOT_NULL(window);
|
---|
115 |
|
---|
116 | ui_popup_params_init(¶ms);
|
---|
117 |
|
---|
118 | rc = ui_popup_create(ui, window, ¶ms, &popup);
|
---|
119 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
120 | PCUT_ASSERT_NOT_NULL(popup);
|
---|
121 |
|
---|
122 | ui_popup_destroy(popup);
|
---|
123 | ui_window_destroy(window);
|
---|
124 | ui_destroy(ui);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** ui_popup_destroy() can take NULL argument (no-op) */
|
---|
128 | PCUT_TEST(destroy_null)
|
---|
129 | {
|
---|
130 | ui_popup_destroy(NULL);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** ui_popup_add()/ui_popup_remove() ... */
|
---|
134 | PCUT_TEST(add_remove)
|
---|
135 | {
|
---|
136 | errno_t rc;
|
---|
137 | ui_t *ui = NULL;
|
---|
138 | ui_wnd_params_t wparams;
|
---|
139 | ui_window_t *window = NULL;
|
---|
140 | ui_popup_params_t params;
|
---|
141 | ui_popup_t *popup = NULL;
|
---|
142 | ui_control_t *control = NULL;
|
---|
143 | test_ctl_resp_t resp;
|
---|
144 |
|
---|
145 | rc = ui_create_disp(NULL, &ui);
|
---|
146 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
147 |
|
---|
148 | ui_wnd_params_init(&wparams);
|
---|
149 | wparams.caption = "Hello";
|
---|
150 |
|
---|
151 | rc = ui_window_create(ui, &wparams, &window);
|
---|
152 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
153 | PCUT_ASSERT_NOT_NULL(window);
|
---|
154 |
|
---|
155 | ui_popup_params_init(¶ms);
|
---|
156 |
|
---|
157 | rc = ui_popup_create(ui, window, ¶ms, &popup);
|
---|
158 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
159 | PCUT_ASSERT_NOT_NULL(popup);
|
---|
160 |
|
---|
161 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
162 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
163 |
|
---|
164 | /* Control not called since it hasn't been added yet */
|
---|
165 | resp.rc = ENOMEM;
|
---|
166 | resp.paint = false;
|
---|
167 | rc = ui_window_def_paint(popup->window);
|
---|
168 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
169 | PCUT_ASSERT_FALSE(resp.paint);
|
---|
170 |
|
---|
171 | ui_popup_add(popup, control);
|
---|
172 |
|
---|
173 | /* Now paint request should be delivered to control */
|
---|
174 | resp.rc = EOK;
|
---|
175 | resp.paint = false;
|
---|
176 | rc = ui_window_def_paint(popup->window);
|
---|
177 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
178 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
179 |
|
---|
180 | ui_popup_remove(popup, control);
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * After having removed the control the request should no longer
|
---|
184 | * be delivered to it.
|
---|
185 | */
|
---|
186 | resp.rc = ENOMEM;
|
---|
187 | resp.paint = false;
|
---|
188 | rc = ui_window_def_paint(popup->window);
|
---|
189 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
190 | PCUT_ASSERT_FALSE(resp.paint);
|
---|
191 |
|
---|
192 | ui_popup_destroy(popup);
|
---|
193 | ui_window_destroy(window);
|
---|
194 | ui_destroy(ui);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /** ui_popup_get_res/gc() return valid objects */
|
---|
198 | PCUT_TEST(get_res_gc)
|
---|
199 | {
|
---|
200 | errno_t rc;
|
---|
201 | ui_t *ui = NULL;
|
---|
202 | ui_wnd_params_t wparams;
|
---|
203 | ui_window_t *window = NULL;
|
---|
204 | ui_popup_params_t params;
|
---|
205 | ui_popup_t *popup = NULL;
|
---|
206 | ui_resource_t *res;
|
---|
207 | gfx_context_t *gc;
|
---|
208 |
|
---|
209 | rc = ui_create_disp(NULL, &ui);
|
---|
210 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
211 |
|
---|
212 | ui_wnd_params_init(&wparams);
|
---|
213 | wparams.caption = "Hello";
|
---|
214 |
|
---|
215 | rc = ui_window_create(ui, &wparams, &window);
|
---|
216 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
217 | PCUT_ASSERT_NOT_NULL(window);
|
---|
218 |
|
---|
219 | ui_popup_params_init(¶ms);
|
---|
220 |
|
---|
221 | rc = ui_popup_create(ui, window, ¶ms, &popup);
|
---|
222 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
223 | PCUT_ASSERT_NOT_NULL(popup);
|
---|
224 |
|
---|
225 | res = ui_popup_get_res(popup);
|
---|
226 | PCUT_ASSERT_NOT_NULL(res);
|
---|
227 |
|
---|
228 | gc = ui_popup_get_gc(popup);
|
---|
229 | PCUT_ASSERT_NOT_NULL(gc);
|
---|
230 |
|
---|
231 | ui_popup_destroy(popup);
|
---|
232 | ui_window_destroy(window);
|
---|
233 | ui_destroy(ui);
|
---|
234 | }
|
---|
235 |
|
---|
236 | /** Test position event callback set via ui_popup_set_cb() */
|
---|
237 | PCUT_TEST(send_pos)
|
---|
238 | {
|
---|
239 | errno_t rc;
|
---|
240 | ui_t *ui = NULL;
|
---|
241 | ui_wnd_params_t wparams;
|
---|
242 | ui_window_t *window = NULL;
|
---|
243 | ui_popup_params_t params;
|
---|
244 | ui_popup_t *popup = NULL;
|
---|
245 | pos_event_t pos_event;
|
---|
246 | test_cb_resp_t resp;
|
---|
247 |
|
---|
248 | rc = ui_create_disp(NULL, &ui);
|
---|
249 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
250 |
|
---|
251 | ui_wnd_params_init(&wparams);
|
---|
252 | wparams.caption = "Hello";
|
---|
253 |
|
---|
254 | rc = ui_window_create(ui, &wparams, &window);
|
---|
255 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
256 | PCUT_ASSERT_NOT_NULL(window);
|
---|
257 |
|
---|
258 | ui_popup_params_init(¶ms);
|
---|
259 |
|
---|
260 | rc = ui_popup_create(ui, window, ¶ms, &popup);
|
---|
261 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
262 | PCUT_ASSERT_NOT_NULL(popup);
|
---|
263 |
|
---|
264 | pos_event.pos_id = 1;
|
---|
265 | pos_event.type = POS_PRESS;
|
---|
266 | pos_event.btn_num = 2;
|
---|
267 | pos_event.hpos = 3;
|
---|
268 | pos_event.vpos = 4;
|
---|
269 |
|
---|
270 | /* Pos callback with no callbacks set */
|
---|
271 | ui_window_send_pos(popup->window, &pos_event);
|
---|
272 |
|
---|
273 | /* Pos callback with pos callback not implemented */
|
---|
274 | ui_popup_set_cb(popup, &dummy_popup_cb, NULL);
|
---|
275 | ui_window_send_pos(popup->window, &pos_event);
|
---|
276 |
|
---|
277 | /* Pos callback with real callback set */
|
---|
278 | resp.pos = false;
|
---|
279 | ui_popup_set_cb(popup, &test_popup_cb, &resp);
|
---|
280 | ui_window_send_pos(popup->window, &pos_event);
|
---|
281 | PCUT_ASSERT_TRUE(resp.pos);
|
---|
282 | PCUT_ASSERT_INT_EQUALS(pos_event.pos_id, resp.pos_event.pos_id);
|
---|
283 | PCUT_ASSERT_EQUALS(pos_event.type, resp.pos_event.type);
|
---|
284 | PCUT_ASSERT_INT_EQUALS(pos_event.btn_num, resp.pos_event.btn_num);
|
---|
285 | PCUT_ASSERT_INT_EQUALS(pos_event.hpos, resp.pos_event.hpos);
|
---|
286 | PCUT_ASSERT_INT_EQUALS(pos_event.vpos, resp.pos_event.vpos);
|
---|
287 |
|
---|
288 | ui_popup_destroy(popup);
|
---|
289 | ui_window_destroy(window);
|
---|
290 | ui_destroy(ui);
|
---|
291 | }
|
---|
292 |
|
---|
293 | static void test_popup_close(ui_popup_t *popup, void *arg)
|
---|
294 | {
|
---|
295 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
296 |
|
---|
297 | resp->close = true;
|
---|
298 | }
|
---|
299 |
|
---|
300 | static void test_popup_kbd(ui_popup_t *popup, void *arg,
|
---|
301 | kbd_event_t *event)
|
---|
302 | {
|
---|
303 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
304 |
|
---|
305 | resp->kbd = true;
|
---|
306 | resp->kbd_event = *event;
|
---|
307 | }
|
---|
308 |
|
---|
309 | static errno_t test_popup_paint(ui_popup_t *popup, void *arg)
|
---|
310 | {
|
---|
311 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
312 |
|
---|
313 | resp->paint = true;
|
---|
314 | return resp->rc;
|
---|
315 | }
|
---|
316 |
|
---|
317 | static void test_popup_pos(ui_popup_t *popup, void *arg,
|
---|
318 | pos_event_t *event)
|
---|
319 | {
|
---|
320 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
321 |
|
---|
322 | resp->pos = true;
|
---|
323 | resp->pos_event = *event;
|
---|
324 | }
|
---|
325 |
|
---|
326 | static errno_t test_ctl_paint(void *arg)
|
---|
327 | {
|
---|
328 | test_ctl_resp_t *resp = (test_ctl_resp_t *) arg;
|
---|
329 |
|
---|
330 | resp->paint = true;
|
---|
331 | return resp->rc;
|
---|
332 | }
|
---|
333 |
|
---|
334 | static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
335 | {
|
---|
336 | test_ctl_resp_t *resp = (test_ctl_resp_t *) arg;
|
---|
337 |
|
---|
338 | resp->pos = true;
|
---|
339 | resp->pos_event = *event;
|
---|
340 |
|
---|
341 | return resp->claim;
|
---|
342 | }
|
---|
343 |
|
---|
344 | static void test_ctl_unfocus(void *arg, unsigned nfocus)
|
---|
345 | {
|
---|
346 | test_ctl_resp_t *resp = (test_ctl_resp_t *) arg;
|
---|
347 |
|
---|
348 | resp->unfocus = true;
|
---|
349 | resp->unfocus_nfocus = nfocus;
|
---|
350 | }
|
---|
351 |
|
---|
352 | PCUT_EXPORT(popup);
|
---|