Editor needs to hide cursor when menu bar is activated
[helenos.git] / uspace / lib / ui / test / fixed.c
blobc33bff601753a92f02b44aee0c21cc454cccd85c
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 <errno.h>
30 #include <pcut/pcut.h>
31 #include <stddef.h>
32 #include <ui/control.h>
33 #include <ui/fixed.h>
34 #include "../private/fixed.h"
36 PCUT_INIT;
38 PCUT_TEST_SUITE(fixed);
40 static void test_ctl_destroy(void *);
41 static errno_t test_ctl_paint(void *);
42 static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
43 static void test_ctl_unfocus(void *, unsigned);
45 static ui_control_ops_t test_ctl_ops = {
46 .destroy = test_ctl_destroy,
47 .paint = test_ctl_paint,
48 .pos_event = test_ctl_pos_event,
49 .unfocus = test_ctl_unfocus
52 /** Test response */
53 typedef struct {
54 /** Claim to return */
55 ui_evclaim_t claim;
56 /** Result code to return */
57 errno_t rc;
58 /** @c true iff destroy was called */
59 bool destroy;
60 /** @c true iff paint was called */
61 bool paint;
62 /** @c true iff pos_event was called */
63 bool pos;
64 /** Position event that was sent */
65 pos_event_t pevent;
66 /** @c true iff unfocus was called */
67 bool unfocus;
68 /** Number of remaining foci */
69 unsigned unfocus_nfocus;
70 } test_resp_t;
72 /** Create and destroy button */
73 PCUT_TEST(create_destroy)
75 ui_fixed_t *fixed = NULL;
76 errno_t rc;
78 rc = ui_fixed_create(&fixed);
79 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
80 PCUT_ASSERT_NOT_NULL(fixed);
82 ui_fixed_destroy(fixed);
85 /** ui_fixed_destroy() can take NULL argument (no-op) */
86 PCUT_TEST(destroy_null)
88 ui_fixed_destroy(NULL);
91 /** ui_fixed_ctl() returns control that has a working virtual destructor */
92 PCUT_TEST(ctl)
94 ui_fixed_t *fixed;
95 ui_control_t *control;
96 errno_t rc;
98 rc = ui_fixed_create(&fixed);
99 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
101 control = ui_fixed_ctl(fixed);
102 PCUT_ASSERT_NOT_NULL(control);
104 ui_control_destroy(control);
107 /** ui_fixed_add() / ui_fixed_remove() adds/removes control */
108 PCUT_TEST(add_remove)
110 ui_fixed_t *fixed = NULL;
111 ui_control_t *control;
112 ui_fixed_elem_t *e;
113 errno_t rc;
115 rc = ui_fixed_create(&fixed);
116 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
118 e = ui_fixed_first(fixed);
119 PCUT_ASSERT_NULL(e);
121 rc = ui_control_new(NULL, NULL, &control);
122 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
124 rc = ui_fixed_add(fixed, control);
125 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
127 e = ui_fixed_first(fixed);
128 PCUT_ASSERT_NOT_NULL(e);
129 PCUT_ASSERT_EQUALS(control, e->control);
130 e = ui_fixed_next(e);
131 PCUT_ASSERT_NULL(e);
133 ui_fixed_remove(fixed, control);
135 e = ui_fixed_first(fixed);
136 PCUT_ASSERT_NULL(e);
138 ui_fixed_destroy(fixed);
139 ui_control_delete(control);
142 /** ui_fixed_destroy() delivers destroy request to control */
143 PCUT_TEST(destroy)
145 ui_fixed_t *fixed = NULL;
146 ui_control_t *control;
147 test_resp_t resp;
148 errno_t rc;
150 rc = ui_fixed_create(&fixed);
151 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
153 rc = ui_control_new(&test_ctl_ops, (void *) &resp, &control);
154 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
156 rc = ui_fixed_add(fixed, control);
157 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
159 resp.destroy = false;
161 ui_fixed_destroy(fixed);
162 PCUT_ASSERT_TRUE(resp.destroy);
165 /** ui_fixed_paint() delivers paint request to control */
166 PCUT_TEST(paint)
168 ui_fixed_t *fixed = NULL;
169 ui_control_t *control;
170 test_resp_t resp;
171 errno_t rc;
173 rc = ui_fixed_create(&fixed);
174 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
176 rc = ui_control_new(&test_ctl_ops, (void *) &resp, &control);
177 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
179 rc = ui_fixed_add(fixed, control);
180 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
182 resp.paint = false;
183 resp.rc = EOK;
185 rc = ui_fixed_paint(fixed);
186 PCUT_ASSERT_EQUALS(resp.rc, rc);
187 PCUT_ASSERT_TRUE(resp.paint);
189 resp.paint = false;
190 resp.rc = EINVAL;
192 rc = ui_fixed_paint(fixed);
193 PCUT_ASSERT_EQUALS(resp.rc, rc);
194 PCUT_ASSERT_TRUE(resp.paint);
196 ui_fixed_destroy(fixed);
199 /** ui_fixed_pos_event() delivers position event to control */
200 PCUT_TEST(pos_event)
202 ui_fixed_t *fixed = NULL;
203 ui_control_t *control;
204 ui_evclaim_t claim;
205 pos_event_t event;
206 test_resp_t resp;
207 errno_t rc;
209 rc = ui_fixed_create(&fixed);
210 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
212 rc = ui_control_new(&test_ctl_ops, (void *) &resp, &control);
213 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
215 rc = ui_fixed_add(fixed, control);
216 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
218 resp.claim = ui_claimed;
219 resp.pos = false;
220 event.pos_id = 1;
221 event.type = POS_PRESS;
222 event.btn_num = 2;
223 event.hpos = 3;
224 event.vpos = 4;
226 claim = ui_fixed_pos_event(fixed, &event);
227 PCUT_ASSERT_EQUALS(resp.claim, claim);
228 PCUT_ASSERT_TRUE(resp.pos);
229 PCUT_ASSERT_INT_EQUALS(resp.pevent.pos_id, event.pos_id);
230 PCUT_ASSERT_EQUALS(resp.pevent.type, event.type);
231 PCUT_ASSERT_INT_EQUALS(resp.pevent.btn_num, event.btn_num);
232 PCUT_ASSERT_INT_EQUALS(resp.pevent.hpos, event.hpos);
233 PCUT_ASSERT_INT_EQUALS(resp.pevent.vpos, event.vpos);
235 ui_fixed_destroy(fixed);
238 /** ui_fixed_unfocus() delivers unfocus notification to control */
239 PCUT_TEST(unfocus)
241 ui_fixed_t *fixed = NULL;
242 ui_control_t *control;
243 test_resp_t resp;
244 errno_t rc;
246 rc = ui_fixed_create(&fixed);
247 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
249 rc = ui_control_new(&test_ctl_ops, (void *) &resp, &control);
250 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
252 rc = ui_fixed_add(fixed, control);
253 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
255 resp.unfocus = false;
257 ui_fixed_unfocus(fixed, 42);
258 PCUT_ASSERT_TRUE(resp.unfocus);
259 PCUT_ASSERT_INT_EQUALS(42, resp.unfocus_nfocus);
261 ui_fixed_destroy(fixed);
264 static void test_ctl_destroy(void *arg)
266 test_resp_t *resp = (test_resp_t *) arg;
268 resp->destroy = true;
271 static errno_t test_ctl_paint(void *arg)
273 test_resp_t *resp = (test_resp_t *) arg;
275 resp->paint = true;
276 return resp->rc;
279 static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
281 test_resp_t *resp = (test_resp_t *) arg;
283 resp->pos = true;
284 resp->pevent = *event;
286 return resp->claim;
289 static void test_ctl_unfocus(void *arg, unsigned nfocus)
291 test_resp_t *resp = (test_resp_t *) arg;
293 resp->unfocus = true;
294 resp->unfocus_nfocus = nfocus;
297 PCUT_EXPORT(fixed);