source: mainline/uspace/lib/ui/test/popup.c@ 241ab7e

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 241ab7e was c68c18b9, checked in by jxsvoboda <5887334+jxsvoboda@…>, 4 years ago

Specify parent window when creating popup

This will be used in conjunction with ui_wnd_popup_params_t.place
(a rectangle relative to the parent window) to determine where on
the screen the popup window should appear.

  • Property mode set to 100644
File size: 7.6 KB
Line 
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/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
45PCUT_INIT;
46
47PCUT_TEST_SUITE(popup);
48
49static void test_popup_close(ui_popup_t *, void *);
50static void test_popup_kbd(ui_popup_t *, void *, kbd_event_t *);
51static errno_t test_popup_paint(ui_popup_t *, void *);
52static void test_popup_pos(ui_popup_t *, void *, pos_event_t *);
53
54static 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
61static ui_popup_cb_t dummy_popup_cb = {
62};
63
64static errno_t test_ctl_paint(void *);
65static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
66static void test_ctl_unfocus(void *);
67
68static 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
74typedef 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
86typedef 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} test_ctl_resp_t;
94
95/** Create and destroy popup window */
96PCUT_TEST(create_destroy)
97{
98 errno_t rc;
99 ui_t *ui = NULL;
100 ui_popup_params_t params;
101 ui_popup_t *popup = NULL;
102
103 rc = ui_create_disp(NULL, &ui);
104 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
105
106 ui_popup_params_init(&params);
107
108 rc = ui_popup_create(ui, NULL, &params, &popup);
109 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
110 PCUT_ASSERT_NOT_NULL(popup);
111
112 ui_popup_destroy(popup);
113 ui_destroy(ui);
114}
115
116/** ui_popup_destroy() can take NULL argument (no-op) */
117PCUT_TEST(destroy_null)
118{
119 ui_popup_destroy(NULL);
120}
121
122/** ui_popup_add()/ui_popup_remove() ... */
123PCUT_TEST(add_remove)
124{
125 errno_t rc;
126 ui_t *ui = NULL;
127 ui_popup_params_t params;
128 ui_popup_t *popup = 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_popup_params_init(&params);
136
137 rc = ui_popup_create(ui, NULL, &params, &popup);
138 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
139 PCUT_ASSERT_NOT_NULL(popup);
140
141 rc = ui_control_new(&test_ctl_ops, &resp, &control);
142 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
143
144 /* Control not called since it hasn't been added yet */
145 resp.rc = ENOMEM;
146 resp.paint = false;
147 rc = ui_window_def_paint(popup->window);
148 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
149 PCUT_ASSERT_FALSE(resp.paint);
150
151 ui_popup_add(popup, control);
152
153 /* Now paint request should be delivered to control */
154 resp.rc = EOK;
155 resp.paint = false;
156 rc = ui_window_def_paint(popup->window);
157 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
158 PCUT_ASSERT_TRUE(resp.paint);
159
160 ui_popup_remove(popup, control);
161
162 /*
163 * After having removed the control the request should no longer
164 * be delivered to it.
165 */
166 resp.rc = ENOMEM;
167 resp.paint = false;
168 rc = ui_window_def_paint(popup->window);
169 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
170 PCUT_ASSERT_FALSE(resp.paint);
171
172 ui_popup_destroy(popup);
173 ui_destroy(ui);
174}
175
176/** ui_popup_get_res/gc() return valid objects */
177PCUT_TEST(get_res_gc)
178{
179 errno_t rc;
180 ui_t *ui = NULL;
181 ui_wnd_params_t params;
182 ui_window_t *window = NULL;
183 ui_resource_t *res;
184 gfx_context_t *gc;
185 gfx_rect_t rect;
186
187 rc = ui_create_disp(NULL, &ui);
188 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
189
190 ui_wnd_params_init(&params);
191 params.caption = "Hello";
192
193 rc = ui_window_create(ui, &params, &window);
194 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
195 PCUT_ASSERT_NOT_NULL(window);
196
197 res = ui_window_get_res(window);
198 PCUT_ASSERT_NOT_NULL(res);
199
200 gc = ui_window_get_gc(window);
201 PCUT_ASSERT_NOT_NULL(gc);
202
203 ui_window_get_app_rect(window, &rect);
204
205 ui_window_destroy(window);
206 ui_destroy(ui);
207}
208
209/** Test position event callback set via ui_popup_set_cb() */
210PCUT_TEST(send_pos)
211{
212 errno_t rc;
213 ui_t *ui = NULL;
214 ui_popup_params_t params;
215 ui_popup_t *popup = NULL;
216 pos_event_t pos_event;
217 test_cb_resp_t resp;
218
219 rc = ui_create_disp(NULL, &ui);
220 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
221
222 ui_popup_params_init(&params);
223
224 rc = ui_popup_create(ui, NULL, &params, &popup);
225 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
226 PCUT_ASSERT_NOT_NULL(popup);
227
228 pos_event.pos_id = 1;
229 pos_event.type = POS_PRESS;
230 pos_event.btn_num = 2;
231 pos_event.hpos = 3;
232 pos_event.vpos = 4;
233
234 /* Pos callback with no callbacks set */
235 ui_window_send_pos(popup->window, &pos_event);
236
237 /* Pos callback with pos callback not implemented */
238 ui_popup_set_cb(popup, &dummy_popup_cb, NULL);
239 ui_window_send_pos(popup->window, &pos_event);
240
241 /* Pos callback with real callback set */
242 resp.pos = false;
243 ui_popup_set_cb(popup, &test_popup_cb, &resp);
244 ui_window_send_pos(popup->window, &pos_event);
245 PCUT_ASSERT_TRUE(resp.pos);
246 PCUT_ASSERT_INT_EQUALS(pos_event.pos_id, resp.pos_event.pos_id);
247 PCUT_ASSERT_EQUALS(pos_event.type, resp.pos_event.type);
248 PCUT_ASSERT_INT_EQUALS(pos_event.btn_num, resp.pos_event.btn_num);
249 PCUT_ASSERT_INT_EQUALS(pos_event.hpos, resp.pos_event.hpos);
250 PCUT_ASSERT_INT_EQUALS(pos_event.vpos, resp.pos_event.vpos);
251
252 ui_popup_destroy(popup);
253 ui_destroy(ui);
254}
255
256static void test_popup_close(ui_popup_t *popup, void *arg)
257{
258 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
259
260 resp->close = true;
261}
262
263static void test_popup_kbd(ui_popup_t *popup, void *arg,
264 kbd_event_t *event)
265{
266 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
267
268 resp->kbd = true;
269 resp->kbd_event = *event;
270}
271
272static errno_t test_popup_paint(ui_popup_t *popup, void *arg)
273{
274 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
275
276 resp->paint = true;
277 return resp->rc;
278}
279
280static void test_popup_pos(ui_popup_t *popup, void *arg,
281 pos_event_t *event)
282{
283 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
284
285 resp->pos = true;
286 resp->pos_event = *event;
287}
288
289static errno_t test_ctl_paint(void *arg)
290{
291 test_ctl_resp_t *resp = (test_ctl_resp_t *) arg;
292
293 resp->paint = true;
294 return resp->rc;
295}
296
297static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
298{
299 test_ctl_resp_t *resp = (test_ctl_resp_t *) arg;
300
301 resp->pos = true;
302 resp->pos_event = *event;
303
304 return resp->claim;
305}
306
307static void test_ctl_unfocus(void *arg)
308{
309 test_ctl_resp_t *resp = (test_ctl_resp_t *) arg;
310
311 resp->unfocus = true;
312}
313
314PCUT_EXPORT(popup);
Note: See TracBrowser for help on using the repository browser.