2 * Copyright (c) 2023 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
30 #include <pcut/pcut.h>
32 #include <ui/control.h>
34 #include "../private/fixed.h"
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
54 /** Claim to return */
56 /** Result code to return */
58 /** @c true iff destroy was called */
60 /** @c true iff paint was called */
62 /** @c true iff pos_event was called */
64 /** Position event that was sent */
66 /** @c true iff unfocus was called */
68 /** Number of remaining foci */
69 unsigned unfocus_nfocus
;
72 /** Create and destroy button */
73 PCUT_TEST(create_destroy
)
75 ui_fixed_t
*fixed
= NULL
;
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 */
95 ui_control_t
*control
;
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
;
115 rc
= ui_fixed_create(&fixed
);
116 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
118 e
= ui_fixed_first(fixed
);
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
);
133 ui_fixed_remove(fixed
, control
);
135 e
= ui_fixed_first(fixed
);
138 ui_fixed_destroy(fixed
);
139 ui_control_delete(control
);
142 /** ui_fixed_destroy() delivers destroy request to control */
145 ui_fixed_t
*fixed
= NULL
;
146 ui_control_t
*control
;
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 */
168 ui_fixed_t
*fixed
= NULL
;
169 ui_control_t
*control
;
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
);
185 rc
= ui_fixed_paint(fixed
);
186 PCUT_ASSERT_EQUALS(resp
.rc
, rc
);
187 PCUT_ASSERT_TRUE(resp
.paint
);
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 */
202 ui_fixed_t
*fixed
= NULL
;
203 ui_control_t
*control
;
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
;
221 event
.type
= POS_PRESS
;
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 */
241 ui_fixed_t
*fixed
= NULL
;
242 ui_control_t
*control
;
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
;
279 static ui_evclaim_t
test_ctl_pos_event(void *arg
, pos_event_t
*event
)
281 test_resp_t
*resp
= (test_resp_t
*) arg
;
284 resp
->pevent
= *event
;
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
;