2 * Copyright (c) 2023 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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>
35 #include <pcut/pcut.h>
37 #include <ui/control.h>
39 #include <ui/resource.h>
41 #include <ui/window.h>
42 #include "../private/popup.h"
43 #include "../private/window.h"
47 PCUT_TEST_SUITE(popup
);
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
*);
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
,
61 static ui_popup_cb_t dummy_popup_cb
= {
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);
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
79 kbd_event_t kbd_event
;
82 pos_event_t pos_event
;
91 pos_event_t pos_event
;
93 unsigned unfocus_nfocus
;
96 /** Create and destroy popup window */
97 PCUT_TEST(create_destroy
)
101 ui_wnd_params_t wparams
;
102 ui_window_t
*window
= NULL
;
103 ui_popup_params_t params
;
104 ui_popup_t
*popup
= NULL
;
106 rc
= ui_create_disp(NULL
, &ui
);
107 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
109 ui_wnd_params_init(&wparams
);
110 wparams
.caption
= "Hello";
112 rc
= ui_window_create(ui
, &wparams
, &window
);
113 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
114 PCUT_ASSERT_NOT_NULL(window
);
116 ui_popup_params_init(¶ms
);
118 rc
= ui_popup_create(ui
, window
, ¶ms
, &popup
);
119 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
120 PCUT_ASSERT_NOT_NULL(popup
);
122 ui_popup_destroy(popup
);
123 ui_window_destroy(window
);
127 /** ui_popup_destroy() can take NULL argument (no-op) */
128 PCUT_TEST(destroy_null
)
130 ui_popup_destroy(NULL
);
133 /** ui_popup_add()/ui_popup_remove() ... */
134 PCUT_TEST(add_remove
)
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
;
145 rc
= ui_create_disp(NULL
, &ui
);
146 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
148 ui_wnd_params_init(&wparams
);
149 wparams
.caption
= "Hello";
151 rc
= ui_window_create(ui
, &wparams
, &window
);
152 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
153 PCUT_ASSERT_NOT_NULL(window
);
155 ui_popup_params_init(¶ms
);
157 rc
= ui_popup_create(ui
, window
, ¶ms
, &popup
);
158 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
159 PCUT_ASSERT_NOT_NULL(popup
);
161 rc
= ui_control_new(&test_ctl_ops
, &resp
, &control
);
162 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
164 /* Control not called since it hasn't been added yet */
167 rc
= ui_window_def_paint(popup
->window
);
168 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
169 PCUT_ASSERT_FALSE(resp
.paint
);
171 ui_popup_add(popup
, control
);
173 /* Now paint request should be delivered to control */
176 rc
= ui_window_def_paint(popup
->window
);
177 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
178 PCUT_ASSERT_TRUE(resp
.paint
);
180 ui_popup_remove(popup
, control
);
183 * After having removed the control the request should no longer
184 * be delivered to it.
188 rc
= ui_window_def_paint(popup
->window
);
189 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
190 PCUT_ASSERT_FALSE(resp
.paint
);
192 ui_popup_destroy(popup
);
193 ui_window_destroy(window
);
197 /** ui_popup_get_res/gc() return valid objects */
198 PCUT_TEST(get_res_gc
)
202 ui_wnd_params_t wparams
;
203 ui_window_t
*window
= NULL
;
204 ui_popup_params_t params
;
205 ui_popup_t
*popup
= NULL
;
209 rc
= ui_create_disp(NULL
, &ui
);
210 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
212 ui_wnd_params_init(&wparams
);
213 wparams
.caption
= "Hello";
215 rc
= ui_window_create(ui
, &wparams
, &window
);
216 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
217 PCUT_ASSERT_NOT_NULL(window
);
219 ui_popup_params_init(¶ms
);
221 rc
= ui_popup_create(ui
, window
, ¶ms
, &popup
);
222 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
223 PCUT_ASSERT_NOT_NULL(popup
);
225 res
= ui_popup_get_res(popup
);
226 PCUT_ASSERT_NOT_NULL(res
);
228 gc
= ui_popup_get_gc(popup
);
229 PCUT_ASSERT_NOT_NULL(gc
);
231 ui_popup_destroy(popup
);
232 ui_window_destroy(window
);
236 /** Test position event callback set via ui_popup_set_cb() */
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
;
248 rc
= ui_create_disp(NULL
, &ui
);
249 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
251 ui_wnd_params_init(&wparams
);
252 wparams
.caption
= "Hello";
254 rc
= ui_window_create(ui
, &wparams
, &window
);
255 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
256 PCUT_ASSERT_NOT_NULL(window
);
258 ui_popup_params_init(¶ms
);
260 rc
= ui_popup_create(ui
, window
, ¶ms
, &popup
);
261 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
262 PCUT_ASSERT_NOT_NULL(popup
);
264 pos_event
.pos_id
= 1;
265 pos_event
.type
= POS_PRESS
;
266 pos_event
.btn_num
= 2;
270 /* Pos callback with no callbacks set */
271 ui_window_send_pos(popup
->window
, &pos_event
);
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
);
277 /* Pos callback with real callback set */
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
);
288 ui_popup_destroy(popup
);
289 ui_window_destroy(window
);
293 static void test_popup_close(ui_popup_t
*popup
, void *arg
)
295 test_cb_resp_t
*resp
= (test_cb_resp_t
*) arg
;
300 static void test_popup_kbd(ui_popup_t
*popup
, void *arg
,
303 test_cb_resp_t
*resp
= (test_cb_resp_t
*) arg
;
306 resp
->kbd_event
= *event
;
309 static errno_t
test_popup_paint(ui_popup_t
*popup
, void *arg
)
311 test_cb_resp_t
*resp
= (test_cb_resp_t
*) arg
;
317 static void test_popup_pos(ui_popup_t
*popup
, void *arg
,
320 test_cb_resp_t
*resp
= (test_cb_resp_t
*) arg
;
323 resp
->pos_event
= *event
;
326 static errno_t
test_ctl_paint(void *arg
)
328 test_ctl_resp_t
*resp
= (test_ctl_resp_t
*) arg
;
334 static ui_evclaim_t
test_ctl_pos_event(void *arg
, pos_event_t
*event
)
336 test_ctl_resp_t
*resp
= (test_ctl_resp_t
*) arg
;
339 resp
->pos_event
= *event
;
344 static void test_ctl_unfocus(void *arg
, unsigned nfocus
)
346 test_ctl_resp_t
*resp
= (test_ctl_resp_t
*) arg
;
348 resp
->unfocus
= true;
349 resp
->unfocus_nfocus
= nfocus
;