source: mainline/uspace/lib/ui/test/fixed.c@ ed1a948

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ed1a948 was 46a47c0, checked in by Jiri Svoboda <jiri@…>, 2 years ago

Make sure window is only show as inactive when it loses last focus

This currently affects the title bar and also the cursor in Terminal.

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 * Copyright (c) 2023 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 <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"
35
36PCUT_INIT;
37
38PCUT_TEST_SUITE(fixed);
39
40static void test_ctl_destroy(void *);
41static errno_t test_ctl_paint(void *);
42static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
43static void test_ctl_unfocus(void *, unsigned);
44
45static 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
50};
51
52/** Test response */
53typedef 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;
71
72/** Create and destroy button */
73PCUT_TEST(create_destroy)
74{
75 ui_fixed_t *fixed = NULL;
76 errno_t rc;
77
78 rc = ui_fixed_create(&fixed);
79 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
80 PCUT_ASSERT_NOT_NULL(fixed);
81
82 ui_fixed_destroy(fixed);
83}
84
85/** ui_fixed_destroy() can take NULL argument (no-op) */
86PCUT_TEST(destroy_null)
87{
88 ui_fixed_destroy(NULL);
89}
90
91/** ui_fixed_ctl() returns control that has a working virtual destructor */
92PCUT_TEST(ctl)
93{
94 ui_fixed_t *fixed;
95 ui_control_t *control;
96 errno_t rc;
97
98 rc = ui_fixed_create(&fixed);
99 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
100
101 control = ui_fixed_ctl(fixed);
102 PCUT_ASSERT_NOT_NULL(control);
103
104 ui_control_destroy(control);
105}
106
107/** ui_fixed_add() / ui_fixed_remove() adds/removes control */
108PCUT_TEST(add_remove)
109{
110 ui_fixed_t *fixed = NULL;
111 ui_control_t *control;
112 ui_fixed_elem_t *e;
113 errno_t rc;
114
115 rc = ui_fixed_create(&fixed);
116 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
117
118 e = ui_fixed_first(fixed);
119 PCUT_ASSERT_NULL(e);
120
121 rc = ui_control_new(NULL, NULL, &control);
122 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
123
124 rc = ui_fixed_add(fixed, control);
125 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
126
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);
132
133 ui_fixed_remove(fixed, control);
134
135 e = ui_fixed_first(fixed);
136 PCUT_ASSERT_NULL(e);
137
138 ui_fixed_destroy(fixed);
139 ui_control_delete(control);
140}
141
142/** ui_fixed_destroy() delivers destroy request to control */
143PCUT_TEST(destroy)
144{
145 ui_fixed_t *fixed = NULL;
146 ui_control_t *control;
147 test_resp_t resp;
148 errno_t rc;
149
150 rc = ui_fixed_create(&fixed);
151 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
152
153 rc = ui_control_new(&test_ctl_ops, (void *) &resp, &control);
154 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
155
156 rc = ui_fixed_add(fixed, control);
157 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
158
159 resp.destroy = false;
160
161 ui_fixed_destroy(fixed);
162 PCUT_ASSERT_TRUE(resp.destroy);
163}
164
165/** ui_fixed_paint() delivers paint request to control */
166PCUT_TEST(paint)
167{
168 ui_fixed_t *fixed = NULL;
169 ui_control_t *control;
170 test_resp_t resp;
171 errno_t rc;
172
173 rc = ui_fixed_create(&fixed);
174 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
175
176 rc = ui_control_new(&test_ctl_ops, (void *) &resp, &control);
177 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
178
179 rc = ui_fixed_add(fixed, control);
180 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
181
182 resp.paint = false;
183 resp.rc = EOK;
184
185 rc = ui_fixed_paint(fixed);
186 PCUT_ASSERT_EQUALS(resp.rc, rc);
187 PCUT_ASSERT_TRUE(resp.paint);
188
189 resp.paint = false;
190 resp.rc = EINVAL;
191
192 rc = ui_fixed_paint(fixed);
193 PCUT_ASSERT_EQUALS(resp.rc, rc);
194 PCUT_ASSERT_TRUE(resp.paint);
195
196 ui_fixed_destroy(fixed);
197}
198
199/** ui_fixed_pos_event() delivers position event to control */
200PCUT_TEST(pos_event)
201{
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;
208
209 rc = ui_fixed_create(&fixed);
210 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
211
212 rc = ui_control_new(&test_ctl_ops, (void *) &resp, &control);
213 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
214
215 rc = ui_fixed_add(fixed, control);
216 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
217
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;
225
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);
234
235 ui_fixed_destroy(fixed);
236}
237
238/** ui_fixed_unfocus() delivers unfocus notification to control */
239PCUT_TEST(unfocus)
240{
241 ui_fixed_t *fixed = NULL;
242 ui_control_t *control;
243 test_resp_t resp;
244 errno_t rc;
245
246 rc = ui_fixed_create(&fixed);
247 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
248
249 rc = ui_control_new(&test_ctl_ops, (void *) &resp, &control);
250 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
251
252 rc = ui_fixed_add(fixed, control);
253 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
254
255 resp.unfocus = false;
256
257 ui_fixed_unfocus(fixed, 42);
258 PCUT_ASSERT_TRUE(resp.unfocus);
259 PCUT_ASSERT_INT_EQUALS(42, resp.unfocus_nfocus);
260
261 ui_fixed_destroy(fixed);
262}
263
264static void test_ctl_destroy(void *arg)
265{
266 test_resp_t *resp = (test_resp_t *) arg;
267
268 resp->destroy = true;
269}
270
271static errno_t test_ctl_paint(void *arg)
272{
273 test_resp_t *resp = (test_resp_t *) arg;
274
275 resp->paint = true;
276 return resp->rc;
277}
278
279static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
280{
281 test_resp_t *resp = (test_resp_t *) arg;
282
283 resp->pos = true;
284 resp->pevent = *event;
285
286 return resp->claim;
287}
288
289static void test_ctl_unfocus(void *arg, unsigned nfocus)
290{
291 test_resp_t *resp = (test_resp_t *) arg;
292
293 resp->unfocus = true;
294 resp->unfocus_nfocus = nfocus;
295}
296
297PCUT_EXPORT(fixed);
Note: See TracBrowser for help on using the repository browser.