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

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

Close menu when window is unfocused

This of course means we need to do all the plumbing for delivering
unfocus event to UI controls.

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