Editor needs to hide cursor when menu bar is activated
[helenos.git] / uspace / lib / ui / test / popup.c
blobf6955494dfce75ce5f81bf8f9fbd9a63adfcf36b
1 /*
2 * Copyright (c) 2023 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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>
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"
45 PCUT_INIT;
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
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;
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;
96 /** Create and destroy popup window */
97 PCUT_TEST(create_destroy)
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;
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(&params);
118 rc = ui_popup_create(ui, window, &params, &popup);
119 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
120 PCUT_ASSERT_NOT_NULL(popup);
122 ui_popup_destroy(popup);
123 ui_window_destroy(window);
124 ui_destroy(ui);
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)
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;
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(&params);
157 rc = ui_popup_create(ui, window, &params, &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 */
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);
171 ui_popup_add(popup, control);
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);
180 ui_popup_remove(popup, control);
183 * After having removed the control the request should no longer
184 * be delivered to it.
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);
192 ui_popup_destroy(popup);
193 ui_window_destroy(window);
194 ui_destroy(ui);
197 /** ui_popup_get_res/gc() return valid objects */
198 PCUT_TEST(get_res_gc)
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;
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(&params);
221 rc = ui_popup_create(ui, window, &params, &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);
233 ui_destroy(ui);
236 /** Test position event callback set via ui_popup_set_cb() */
237 PCUT_TEST(send_pos)
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;
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(&params);
260 rc = ui_popup_create(ui, window, &params, &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;
267 pos_event.hpos = 3;
268 pos_event.vpos = 4;
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 */
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);
288 ui_popup_destroy(popup);
289 ui_window_destroy(window);
290 ui_destroy(ui);
293 static void test_popup_close(ui_popup_t *popup, void *arg)
295 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
297 resp->close = true;
300 static void test_popup_kbd(ui_popup_t *popup, void *arg,
301 kbd_event_t *event)
303 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
305 resp->kbd = true;
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;
313 resp->paint = true;
314 return resp->rc;
317 static void test_popup_pos(ui_popup_t *popup, void *arg,
318 pos_event_t *event)
320 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
322 resp->pos = true;
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;
330 resp->paint = true;
331 return resp->rc;
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;
338 resp->pos = true;
339 resp->pos_event = *event;
341 return resp->claim;
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;
352 PCUT_EXPORT(popup);