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 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/resource.h>
|
---|
39 | #include <ui/ui.h>
|
---|
40 | #include <ui/window.h>
|
---|
41 | #include "../private/window.h"
|
---|
42 |
|
---|
43 | PCUT_INIT;
|
---|
44 |
|
---|
45 | PCUT_TEST_SUITE(window);
|
---|
46 |
|
---|
47 | static void test_window_close(ui_window_t *, void *);
|
---|
48 | static void test_window_focus(ui_window_t *, void *);
|
---|
49 | static void test_window_kbd(ui_window_t *, void *, kbd_event_t *);
|
---|
50 | static errno_t test_window_paint(ui_window_t *, void *);
|
---|
51 | static void test_window_pos(ui_window_t *, void *, pos_event_t *);
|
---|
52 | static void test_window_unfocus(ui_window_t *, void *);
|
---|
53 |
|
---|
54 | static ui_window_cb_t test_window_cb = {
|
---|
55 | .close = test_window_close,
|
---|
56 | .focus = test_window_focus,
|
---|
57 | .kbd = test_window_kbd,
|
---|
58 | .paint = test_window_paint,
|
---|
59 | .pos = test_window_pos,
|
---|
60 | .unfocus = test_window_unfocus
|
---|
61 | };
|
---|
62 |
|
---|
63 | static ui_window_cb_t dummy_window_cb = {
|
---|
64 | };
|
---|
65 |
|
---|
66 | static errno_t test_ctl_paint(void *);
|
---|
67 | static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
|
---|
68 | static void test_ctl_unfocus(void *);
|
---|
69 |
|
---|
70 | static ui_control_ops_t test_ctl_ops = {
|
---|
71 | .paint = test_ctl_paint,
|
---|
72 | .pos_event = test_ctl_pos_event,
|
---|
73 | .unfocus = test_ctl_unfocus
|
---|
74 | };
|
---|
75 |
|
---|
76 | typedef struct {
|
---|
77 | errno_t rc;
|
---|
78 | bool close;
|
---|
79 | bool focus;
|
---|
80 | bool kbd;
|
---|
81 | kbd_event_t kbd_event;
|
---|
82 | bool paint;
|
---|
83 | bool pos;
|
---|
84 | pos_event_t pos_event;
|
---|
85 | bool unfocus;
|
---|
86 | } test_cb_resp_t;
|
---|
87 |
|
---|
88 | typedef struct {
|
---|
89 | errno_t rc;
|
---|
90 | ui_evclaim_t claim;
|
---|
91 | bool paint;
|
---|
92 | bool pos;
|
---|
93 | pos_event_t pos_event;
|
---|
94 | bool unfocus;
|
---|
95 | } test_ctl_resp_t;
|
---|
96 |
|
---|
97 | /** Create and destroy window */
|
---|
98 | PCUT_TEST(create_destroy)
|
---|
99 | {
|
---|
100 | errno_t rc;
|
---|
101 | ui_t *ui = NULL;
|
---|
102 | ui_wnd_params_t params;
|
---|
103 | ui_window_t *window = NULL;
|
---|
104 |
|
---|
105 | rc = ui_create_disp(NULL, &ui);
|
---|
106 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
107 |
|
---|
108 | ui_wnd_params_init(¶ms);
|
---|
109 | params.caption = "Hello";
|
---|
110 |
|
---|
111 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
112 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
113 | PCUT_ASSERT_NOT_NULL(window);
|
---|
114 |
|
---|
115 | ui_window_destroy(window);
|
---|
116 | ui_destroy(ui);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** ui_window_destroy() can take NULL argument (no-op) */
|
---|
120 | PCUT_TEST(destroy_null)
|
---|
121 | {
|
---|
122 | ui_window_destroy(NULL);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /** ui_window_add()/ui_window_remove() ... */
|
---|
126 | PCUT_TEST(add_remove)
|
---|
127 | {
|
---|
128 | errno_t rc;
|
---|
129 | ui_t *ui = NULL;
|
---|
130 | ui_wnd_params_t params;
|
---|
131 | ui_window_t *window = NULL;
|
---|
132 | ui_control_t *control = NULL;
|
---|
133 | test_ctl_resp_t resp;
|
---|
134 |
|
---|
135 | rc = ui_create_disp(NULL, &ui);
|
---|
136 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
137 |
|
---|
138 | ui_wnd_params_init(¶ms);
|
---|
139 | params.caption = "Hello";
|
---|
140 |
|
---|
141 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
142 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
143 | PCUT_ASSERT_NOT_NULL(window);
|
---|
144 |
|
---|
145 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
146 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
147 |
|
---|
148 | /* Control not called since it hasn't been added yet */
|
---|
149 | resp.rc = ENOMEM;
|
---|
150 | resp.paint = false;
|
---|
151 | rc = ui_window_def_paint(window);
|
---|
152 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
153 | PCUT_ASSERT_FALSE(resp.paint);
|
---|
154 |
|
---|
155 | ui_window_add(window, control);
|
---|
156 |
|
---|
157 | /* Now paint request should be delivered to control */
|
---|
158 | resp.rc = EOK;
|
---|
159 | resp.paint = false;
|
---|
160 | rc = ui_window_def_paint(window);
|
---|
161 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
162 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
163 |
|
---|
164 | ui_window_remove(window, control);
|
---|
165 |
|
---|
166 | /*
|
---|
167 | * After having removed the control the request should no longer
|
---|
168 | * be delivered to it.
|
---|
169 | */
|
---|
170 | resp.rc = ENOMEM;
|
---|
171 | resp.paint = false;
|
---|
172 | rc = ui_window_def_paint(window);
|
---|
173 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
174 | PCUT_ASSERT_FALSE(resp.paint);
|
---|
175 |
|
---|
176 | ui_window_destroy(window);
|
---|
177 | ui_destroy(ui);
|
---|
178 | }
|
---|
179 |
|
---|
180 | /** ui_window_resize */
|
---|
181 | PCUT_TEST(resize)
|
---|
182 | {
|
---|
183 | errno_t rc;
|
---|
184 | ui_t *ui = NULL;
|
---|
185 | ui_wnd_params_t params;
|
---|
186 | ui_window_t *window = NULL;
|
---|
187 | gfx_rect_t nrect;
|
---|
188 |
|
---|
189 | rc = ui_create_disp(NULL, &ui);
|
---|
190 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
191 |
|
---|
192 | ui_wnd_params_init(¶ms);
|
---|
193 | params.caption = "Hello";
|
---|
194 | params.rect.p0.x = 0;
|
---|
195 | params.rect.p0.y = 0;
|
---|
196 | params.rect.p1.x = 1;
|
---|
197 | params.rect.p1.y = 1;
|
---|
198 |
|
---|
199 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
200 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
201 | PCUT_ASSERT_NOT_NULL(window);
|
---|
202 |
|
---|
203 | nrect.p0.x = -1;
|
---|
204 | nrect.p0.y = -1;
|
---|
205 | nrect.p1.x = 2;
|
---|
206 | nrect.p1.y = 2;
|
---|
207 | rc = ui_window_resize(window, &nrect);
|
---|
208 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
209 |
|
---|
210 | ui_window_destroy(window);
|
---|
211 | ui_destroy(ui);
|
---|
212 | }
|
---|
213 |
|
---|
214 | /** ui_window_get_res/gc/rect() return valid objects */
|
---|
215 | PCUT_TEST(get_res_gc_rect)
|
---|
216 | {
|
---|
217 | errno_t rc;
|
---|
218 | ui_t *ui = NULL;
|
---|
219 | ui_wnd_params_t params;
|
---|
220 | ui_window_t *window = NULL;
|
---|
221 | ui_resource_t *res;
|
---|
222 | gfx_context_t *gc;
|
---|
223 | gfx_rect_t rect;
|
---|
224 |
|
---|
225 | rc = ui_create_disp(NULL, &ui);
|
---|
226 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
227 |
|
---|
228 | ui_wnd_params_init(¶ms);
|
---|
229 | params.caption = "Hello";
|
---|
230 |
|
---|
231 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
232 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
233 | PCUT_ASSERT_NOT_NULL(window);
|
---|
234 |
|
---|
235 | res = ui_window_get_res(window);
|
---|
236 | PCUT_ASSERT_NOT_NULL(res);
|
---|
237 |
|
---|
238 | gc = ui_window_get_gc(window);
|
---|
239 | PCUT_ASSERT_NOT_NULL(gc);
|
---|
240 |
|
---|
241 | ui_window_get_app_rect(window, &rect);
|
---|
242 |
|
---|
243 | ui_window_destroy(window);
|
---|
244 | ui_destroy(ui);
|
---|
245 | }
|
---|
246 |
|
---|
247 | /** ui_window_get_app_gc() return valid GC */
|
---|
248 | PCUT_TEST(get_app_gc)
|
---|
249 | {
|
---|
250 | errno_t rc;
|
---|
251 | ui_t *ui = NULL;
|
---|
252 | ui_wnd_params_t params;
|
---|
253 | ui_window_t *window = NULL;
|
---|
254 | gfx_context_t *gc;
|
---|
255 |
|
---|
256 | rc = ui_create_disp(NULL, &ui);
|
---|
257 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
258 |
|
---|
259 | ui_wnd_params_init(¶ms);
|
---|
260 | params.caption = "Hello";
|
---|
261 | params.rect.p0.x = 0;
|
---|
262 | params.rect.p0.y = 0;
|
---|
263 | params.rect.p0.x = 100;
|
---|
264 | params.rect.p0.y = 100;
|
---|
265 |
|
---|
266 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
267 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
268 | PCUT_ASSERT_NOT_NULL(window);
|
---|
269 |
|
---|
270 | rc = ui_window_get_app_gc(window, &gc);
|
---|
271 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
272 | PCUT_ASSERT_NOT_NULL(gc);
|
---|
273 |
|
---|
274 | rc = gfx_fill_rect(gc, ¶ms.rect);
|
---|
275 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
276 |
|
---|
277 | ui_window_destroy(window);
|
---|
278 | ui_destroy(ui);
|
---|
279 | }
|
---|
280 |
|
---|
281 | /** Test ui_window_paint() */
|
---|
282 | PCUT_TEST(paint)
|
---|
283 | {
|
---|
284 | errno_t rc;
|
---|
285 | ui_t *ui = NULL;
|
---|
286 | ui_wnd_params_t params;
|
---|
287 | ui_window_t *window = NULL;
|
---|
288 |
|
---|
289 | rc = ui_create_disp(NULL, &ui);
|
---|
290 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
291 |
|
---|
292 | ui_wnd_params_init(¶ms);
|
---|
293 | params.caption = "Hello";
|
---|
294 |
|
---|
295 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
296 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
297 | PCUT_ASSERT_NOT_NULL(window);
|
---|
298 |
|
---|
299 | ui_window_paint(window);
|
---|
300 |
|
---|
301 | ui_window_destroy(window);
|
---|
302 | ui_destroy(ui);
|
---|
303 | }
|
---|
304 |
|
---|
305 | /** Test ui_window_def_paint() */
|
---|
306 | PCUT_TEST(def_paint)
|
---|
307 | {
|
---|
308 | errno_t rc;
|
---|
309 | ui_t *ui = NULL;
|
---|
310 | ui_wnd_params_t params;
|
---|
311 | ui_window_t *window = NULL;
|
---|
312 | ui_control_t *control = NULL;
|
---|
313 | test_ctl_resp_t resp;
|
---|
314 |
|
---|
315 | rc = ui_create_disp(NULL, &ui);
|
---|
316 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
317 |
|
---|
318 | ui_wnd_params_init(¶ms);
|
---|
319 | params.caption = "Hello";
|
---|
320 |
|
---|
321 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
322 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
323 | PCUT_ASSERT_NOT_NULL(window);
|
---|
324 |
|
---|
325 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
326 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
327 |
|
---|
328 | ui_window_add(window, control);
|
---|
329 |
|
---|
330 | resp.rc = EOK;
|
---|
331 | resp.paint = false;
|
---|
332 | rc = ui_window_def_paint(window);
|
---|
333 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
---|
334 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
335 |
|
---|
336 | resp.rc = ENOMEM;
|
---|
337 | resp.paint = false;
|
---|
338 | rc = ui_window_def_paint(window);
|
---|
339 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
---|
340 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
341 |
|
---|
342 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
343 |
|
---|
344 | /* Need to remove first because we didn't implement the destructor */
|
---|
345 | ui_window_remove(window, control);
|
---|
346 |
|
---|
347 | ui_window_destroy(window);
|
---|
348 | ui_destroy(ui);
|
---|
349 | }
|
---|
350 |
|
---|
351 | /** ui_window_def_pos() delivers position event to control in window */
|
---|
352 | PCUT_TEST(def_pos)
|
---|
353 | {
|
---|
354 | errno_t rc;
|
---|
355 | ui_t *ui = NULL;
|
---|
356 | ui_wnd_params_t params;
|
---|
357 | ui_window_t *window = NULL;
|
---|
358 | ui_control_t *control = NULL;
|
---|
359 | test_ctl_resp_t resp;
|
---|
360 | pos_event_t event;
|
---|
361 |
|
---|
362 | rc = ui_create_disp(NULL, &ui);
|
---|
363 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
364 |
|
---|
365 | ui_wnd_params_init(¶ms);
|
---|
366 | params.caption = "Hello";
|
---|
367 |
|
---|
368 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
369 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
370 |
|
---|
371 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
372 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
373 |
|
---|
374 | ui_window_add(window, control);
|
---|
375 |
|
---|
376 | event.pos_id = 1;
|
---|
377 | event.type = POS_PRESS;
|
---|
378 | event.btn_num = 2;
|
---|
379 | event.hpos = 3;
|
---|
380 | event.vpos = 4;
|
---|
381 |
|
---|
382 | resp.pos = false;
|
---|
383 | resp.claim = ui_claimed;
|
---|
384 |
|
---|
385 | ui_window_def_pos(window, &event);
|
---|
386 |
|
---|
387 | PCUT_ASSERT_TRUE(resp.pos);
|
---|
388 | PCUT_ASSERT_INT_EQUALS(event.pos_id, resp.pos_event.pos_id);
|
---|
389 | PCUT_ASSERT_INT_EQUALS(event.type, resp.pos_event.type);
|
---|
390 | PCUT_ASSERT_INT_EQUALS(event.btn_num, resp.pos_event.btn_num);
|
---|
391 | PCUT_ASSERT_INT_EQUALS(event.hpos, resp.pos_event.hpos);
|
---|
392 | PCUT_ASSERT_INT_EQUALS(event.vpos, resp.pos_event.vpos);
|
---|
393 |
|
---|
394 | /* Need to remove first because we didn't implement the destructor */
|
---|
395 | ui_window_remove(window, control);
|
---|
396 |
|
---|
397 | ui_window_destroy(window);
|
---|
398 | ui_destroy(ui);
|
---|
399 | }
|
---|
400 |
|
---|
401 | /** ui_window_def_unfocus() delivers unfocus event to control in window */
|
---|
402 | PCUT_TEST(def_unfocus)
|
---|
403 | {
|
---|
404 | errno_t rc;
|
---|
405 | ui_t *ui = NULL;
|
---|
406 | ui_wnd_params_t params;
|
---|
407 | ui_window_t *window = NULL;
|
---|
408 | ui_control_t *control = NULL;
|
---|
409 | test_ctl_resp_t resp;
|
---|
410 |
|
---|
411 | rc = ui_create_disp(NULL, &ui);
|
---|
412 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
413 |
|
---|
414 | ui_wnd_params_init(¶ms);
|
---|
415 | params.caption = "Hello";
|
---|
416 |
|
---|
417 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
418 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
419 |
|
---|
420 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
421 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
422 |
|
---|
423 | ui_window_add(window, control);
|
---|
424 |
|
---|
425 | resp.unfocus = false;
|
---|
426 |
|
---|
427 | ui_window_def_unfocus(window);
|
---|
428 | PCUT_ASSERT_TRUE(resp.unfocus);
|
---|
429 |
|
---|
430 | /* Need to remove first because we didn't implement the destructor */
|
---|
431 | ui_window_remove(window, control);
|
---|
432 |
|
---|
433 | ui_window_destroy(window);
|
---|
434 | ui_destroy(ui);
|
---|
435 | }
|
---|
436 |
|
---|
437 | /** ui_window_send_close() calls close callback set via ui_window_set_cb() */
|
---|
438 | PCUT_TEST(send_close)
|
---|
439 | {
|
---|
440 | errno_t rc;
|
---|
441 | ui_t *ui = NULL;
|
---|
442 | ui_wnd_params_t params;
|
---|
443 | ui_window_t *window = NULL;
|
---|
444 | test_cb_resp_t resp;
|
---|
445 |
|
---|
446 | rc = ui_create_disp(NULL, &ui);
|
---|
447 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
448 |
|
---|
449 | ui_wnd_params_init(¶ms);
|
---|
450 | params.caption = "Hello";
|
---|
451 |
|
---|
452 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
453 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
454 | PCUT_ASSERT_NOT_NULL(window);
|
---|
455 |
|
---|
456 | /* Close callback with no callbacks set */
|
---|
457 | ui_window_send_close(window);
|
---|
458 |
|
---|
459 | /* Close callback with close callback not implemented */
|
---|
460 | ui_window_set_cb(window, &dummy_window_cb, NULL);
|
---|
461 | ui_window_send_close(window);
|
---|
462 |
|
---|
463 | /* Close callback with real callback set */
|
---|
464 | resp.close = false;
|
---|
465 | ui_window_set_cb(window, &test_window_cb, &resp);
|
---|
466 | ui_window_send_close(window);
|
---|
467 | PCUT_ASSERT_TRUE(resp.close);
|
---|
468 |
|
---|
469 | ui_window_destroy(window);
|
---|
470 | ui_destroy(ui);
|
---|
471 | }
|
---|
472 |
|
---|
473 | /** ui_window_send_focus() calls focus callback set via ui_window_set_cb() */
|
---|
474 | PCUT_TEST(send_focus)
|
---|
475 | {
|
---|
476 | errno_t rc;
|
---|
477 | ui_t *ui = NULL;
|
---|
478 | ui_wnd_params_t params;
|
---|
479 | ui_window_t *window = NULL;
|
---|
480 | test_cb_resp_t resp;
|
---|
481 |
|
---|
482 | rc = ui_create_disp(NULL, &ui);
|
---|
483 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
484 |
|
---|
485 | ui_wnd_params_init(¶ms);
|
---|
486 | params.caption = "Hello";
|
---|
487 |
|
---|
488 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
489 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
490 | PCUT_ASSERT_NOT_NULL(window);
|
---|
491 |
|
---|
492 | /* Focus callback with no callbacks set */
|
---|
493 | ui_window_send_focus(window);
|
---|
494 |
|
---|
495 | /* Focus callback with focus callback not implemented */
|
---|
496 | ui_window_set_cb(window, &dummy_window_cb, NULL);
|
---|
497 | ui_window_send_focus(window);
|
---|
498 |
|
---|
499 | /* Focus callback with real callback set */
|
---|
500 | resp.focus = false;
|
---|
501 | ui_window_set_cb(window, &test_window_cb, &resp);
|
---|
502 | ui_window_send_focus(window);
|
---|
503 | PCUT_ASSERT_TRUE(resp.focus);
|
---|
504 |
|
---|
505 | ui_window_destroy(window);
|
---|
506 | ui_destroy(ui);
|
---|
507 | }
|
---|
508 |
|
---|
509 | /** ui_window_send_kbd() calls kbd callback set via ui_window_set_cb() */
|
---|
510 | PCUT_TEST(send_kbd)
|
---|
511 | {
|
---|
512 | errno_t rc;
|
---|
513 | ui_t *ui = NULL;
|
---|
514 | ui_wnd_params_t params;
|
---|
515 | ui_window_t *window = NULL;
|
---|
516 | kbd_event_t kbd_event;
|
---|
517 | test_cb_resp_t resp;
|
---|
518 |
|
---|
519 | rc = ui_create_disp(NULL, &ui);
|
---|
520 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
521 |
|
---|
522 | ui_wnd_params_init(¶ms);
|
---|
523 | params.caption = "Hello";
|
---|
524 |
|
---|
525 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
526 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
527 | PCUT_ASSERT_NOT_NULL(window);
|
---|
528 |
|
---|
529 | kbd_event.type = POS_PRESS;
|
---|
530 | kbd_event.key = KC_X;
|
---|
531 | kbd_event.mods = 0;
|
---|
532 | kbd_event.c = 'x';
|
---|
533 |
|
---|
534 | /* Kbd callback with no callbacks set */
|
---|
535 | ui_window_send_kbd(window, &kbd_event);
|
---|
536 |
|
---|
537 | /* Kbd callback with kbd callback not implemented */
|
---|
538 | ui_window_set_cb(window, &dummy_window_cb, NULL);
|
---|
539 | ui_window_send_kbd(window, &kbd_event);
|
---|
540 |
|
---|
541 | /* Kbd callback with real callback set */
|
---|
542 | resp.kbd = false;
|
---|
543 | ui_window_set_cb(window, &test_window_cb, &resp);
|
---|
544 | ui_window_send_kbd(window, &kbd_event);
|
---|
545 | PCUT_ASSERT_TRUE(resp.kbd);
|
---|
546 | PCUT_ASSERT_EQUALS(kbd_event.type, resp.kbd_event.type);
|
---|
547 | PCUT_ASSERT_INT_EQUALS(kbd_event.key, resp.kbd_event.key);
|
---|
548 | PCUT_ASSERT_INT_EQUALS(kbd_event.mods, resp.kbd_event.mods);
|
---|
549 | PCUT_ASSERT_INT_EQUALS(kbd_event.c, resp.kbd_event.c);
|
---|
550 |
|
---|
551 | ui_window_destroy(window);
|
---|
552 | ui_destroy(ui);
|
---|
553 | }
|
---|
554 |
|
---|
555 | /** ui_window_send_paint() calls paint callback set via ui_window_set_cb() */
|
---|
556 | PCUT_TEST(send_paint)
|
---|
557 | {
|
---|
558 | errno_t rc;
|
---|
559 | ui_t *ui = NULL;
|
---|
560 | ui_wnd_params_t params;
|
---|
561 | ui_window_t *window = NULL;
|
---|
562 | test_cb_resp_t resp;
|
---|
563 |
|
---|
564 | rc = ui_create_disp(NULL, &ui);
|
---|
565 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
566 |
|
---|
567 | ui_wnd_params_init(¶ms);
|
---|
568 | params.caption = "Hello";
|
---|
569 |
|
---|
570 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
571 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
572 | PCUT_ASSERT_NOT_NULL(window);
|
---|
573 |
|
---|
574 | /* Paint callback with no callbacks set */
|
---|
575 | ui_window_send_paint(window);
|
---|
576 |
|
---|
577 | /* Paint callback with paint callback not implemented */
|
---|
578 | ui_window_set_cb(window, &dummy_window_cb, NULL);
|
---|
579 | ui_window_send_paint(window);
|
---|
580 |
|
---|
581 | /* Paint callback with real callback set */
|
---|
582 | resp.paint = false;
|
---|
583 | resp.rc = EOK;
|
---|
584 | ui_window_set_cb(window, &test_window_cb, &resp);
|
---|
585 | rc = ui_window_send_paint(window);
|
---|
586 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
---|
587 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
588 |
|
---|
589 | ui_window_destroy(window);
|
---|
590 | ui_destroy(ui);
|
---|
591 | }
|
---|
592 |
|
---|
593 | /** ui_window_send_pos() calls pos callback set via ui_window_set_cb() */
|
---|
594 | PCUT_TEST(send_pos)
|
---|
595 | {
|
---|
596 | errno_t rc;
|
---|
597 | ui_t *ui = NULL;
|
---|
598 | ui_wnd_params_t params;
|
---|
599 | ui_window_t *window = NULL;
|
---|
600 | pos_event_t pos_event;
|
---|
601 | test_cb_resp_t resp;
|
---|
602 |
|
---|
603 | rc = ui_create_disp(NULL, &ui);
|
---|
604 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
605 |
|
---|
606 | ui_wnd_params_init(¶ms);
|
---|
607 | params.caption = "Hello";
|
---|
608 |
|
---|
609 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
610 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
611 | PCUT_ASSERT_NOT_NULL(window);
|
---|
612 |
|
---|
613 | pos_event.pos_id = 1;
|
---|
614 | pos_event.type = POS_PRESS;
|
---|
615 | pos_event.btn_num = 2;
|
---|
616 | pos_event.hpos = 3;
|
---|
617 | pos_event.vpos = 4;
|
---|
618 |
|
---|
619 | /* Pos callback with no callbacks set */
|
---|
620 | ui_window_send_pos(window, &pos_event);
|
---|
621 |
|
---|
622 | /* Pos callback with pos callback not implemented */
|
---|
623 | ui_window_set_cb(window, &dummy_window_cb, NULL);
|
---|
624 | ui_window_send_pos(window, &pos_event);
|
---|
625 |
|
---|
626 | /* Pos callback with real callback set */
|
---|
627 | resp.pos = false;
|
---|
628 | ui_window_set_cb(window, &test_window_cb, &resp);
|
---|
629 | ui_window_send_pos(window, &pos_event);
|
---|
630 | PCUT_ASSERT_TRUE(resp.pos);
|
---|
631 | PCUT_ASSERT_INT_EQUALS(pos_event.pos_id, resp.pos_event.pos_id);
|
---|
632 | PCUT_ASSERT_EQUALS(pos_event.type, resp.pos_event.type);
|
---|
633 | PCUT_ASSERT_INT_EQUALS(pos_event.btn_num, resp.pos_event.btn_num);
|
---|
634 | PCUT_ASSERT_INT_EQUALS(pos_event.hpos, resp.pos_event.hpos);
|
---|
635 | PCUT_ASSERT_INT_EQUALS(pos_event.vpos, resp.pos_event.vpos);
|
---|
636 |
|
---|
637 | ui_window_destroy(window);
|
---|
638 | ui_destroy(ui);
|
---|
639 | }
|
---|
640 |
|
---|
641 | /** ui_window_send_unfocus() calls unfocus callback set via ui_window_set_cb() */
|
---|
642 | PCUT_TEST(send_unfocus)
|
---|
643 | {
|
---|
644 | errno_t rc;
|
---|
645 | ui_t *ui = NULL;
|
---|
646 | ui_wnd_params_t params;
|
---|
647 | ui_window_t *window = NULL;
|
---|
648 | test_cb_resp_t resp;
|
---|
649 |
|
---|
650 | rc = ui_create_disp(NULL, &ui);
|
---|
651 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
652 |
|
---|
653 | ui_wnd_params_init(¶ms);
|
---|
654 | params.caption = "Hello";
|
---|
655 |
|
---|
656 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
657 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
658 | PCUT_ASSERT_NOT_NULL(window);
|
---|
659 |
|
---|
660 | /* Unfocus callback with no callbacks set */
|
---|
661 | ui_window_send_unfocus(window);
|
---|
662 |
|
---|
663 | /* Unfocus callback with unfocus callback not implemented */
|
---|
664 | ui_window_set_cb(window, &dummy_window_cb, NULL);
|
---|
665 | ui_window_send_unfocus(window);
|
---|
666 |
|
---|
667 | /* Unfocus callback with real callback set */
|
---|
668 | resp.close = false;
|
---|
669 | ui_window_set_cb(window, &test_window_cb, &resp);
|
---|
670 | ui_window_send_unfocus(window);
|
---|
671 | PCUT_ASSERT_TRUE(resp.unfocus);
|
---|
672 |
|
---|
673 | ui_window_destroy(window);
|
---|
674 | ui_destroy(ui);
|
---|
675 | }
|
---|
676 |
|
---|
677 | static void test_window_close(ui_window_t *window, void *arg)
|
---|
678 | {
|
---|
679 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
680 |
|
---|
681 | resp->close = true;
|
---|
682 | }
|
---|
683 |
|
---|
684 | static void test_window_focus(ui_window_t *window, void *arg)
|
---|
685 | {
|
---|
686 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
687 |
|
---|
688 | resp->focus = true;
|
---|
689 | }
|
---|
690 |
|
---|
691 | static void test_window_kbd(ui_window_t *window, void *arg,
|
---|
692 | kbd_event_t *event)
|
---|
693 | {
|
---|
694 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
695 |
|
---|
696 | resp->kbd = true;
|
---|
697 | resp->kbd_event = *event;
|
---|
698 | }
|
---|
699 |
|
---|
700 | static errno_t test_window_paint(ui_window_t *window, void *arg)
|
---|
701 | {
|
---|
702 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
703 |
|
---|
704 | resp->paint = true;
|
---|
705 | return resp->rc;
|
---|
706 | }
|
---|
707 |
|
---|
708 | static void test_window_pos(ui_window_t *window, void *arg,
|
---|
709 | pos_event_t *event)
|
---|
710 | {
|
---|
711 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
712 |
|
---|
713 | resp->pos = true;
|
---|
714 | resp->pos_event = *event;
|
---|
715 | }
|
---|
716 |
|
---|
717 | static void test_window_unfocus(ui_window_t *window, void *arg)
|
---|
718 | {
|
---|
719 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
720 |
|
---|
721 | resp->unfocus = true;
|
---|
722 | }
|
---|
723 |
|
---|
724 | static errno_t test_ctl_paint(void *arg)
|
---|
725 | {
|
---|
726 | test_ctl_resp_t *resp = (test_ctl_resp_t *) arg;
|
---|
727 |
|
---|
728 | resp->paint = true;
|
---|
729 | return resp->rc;
|
---|
730 | }
|
---|
731 |
|
---|
732 | static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
733 | {
|
---|
734 | test_ctl_resp_t *resp = (test_ctl_resp_t *) arg;
|
---|
735 |
|
---|
736 | resp->pos = true;
|
---|
737 | resp->pos_event = *event;
|
---|
738 |
|
---|
739 | return resp->claim;
|
---|
740 | }
|
---|
741 |
|
---|
742 | static void test_ctl_unfocus(void *arg)
|
---|
743 | {
|
---|
744 | test_ctl_resp_t *resp = (test_ctl_resp_t *) arg;
|
---|
745 |
|
---|
746 | resp->unfocus = true;
|
---|
747 | }
|
---|
748 |
|
---|
749 | PCUT_EXPORT(window);
|
---|