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