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 <gfx/context.h>
|
---|
30 | #include <gfx/coord.h>
|
---|
31 | #include <mem.h>
|
---|
32 | #include <pcut/pcut.h>
|
---|
33 | #include <stdbool.h>
|
---|
34 | #include <ui/control.h>
|
---|
35 | #include <ui/pbutton.h>
|
---|
36 | #include <ui/resource.h>
|
---|
37 | #include "../private/pbutton.h"
|
---|
38 | #include "../private/testgc.h"
|
---|
39 |
|
---|
40 | PCUT_INIT;
|
---|
41 |
|
---|
42 | PCUT_TEST_SUITE(pbutton);
|
---|
43 |
|
---|
44 | static void test_pbutton_clicked(ui_pbutton_t *, void *);
|
---|
45 | static void test_pbutton_down(ui_pbutton_t *, void *);
|
---|
46 | static void test_pbutton_up(ui_pbutton_t *, void *);
|
---|
47 |
|
---|
48 | static ui_pbutton_cb_t test_pbutton_cb = {
|
---|
49 | .clicked = test_pbutton_clicked,
|
---|
50 | .down = test_pbutton_down,
|
---|
51 | .up = test_pbutton_up
|
---|
52 | };
|
---|
53 |
|
---|
54 | static ui_pbutton_cb_t dummy_pbutton_cb = {
|
---|
55 | };
|
---|
56 |
|
---|
57 | typedef struct {
|
---|
58 | bool clicked;
|
---|
59 | bool down;
|
---|
60 | bool up;
|
---|
61 | } test_cb_resp_t;
|
---|
62 |
|
---|
63 | /** Create and destroy button */
|
---|
64 | PCUT_TEST(create_destroy)
|
---|
65 | {
|
---|
66 | ui_pbutton_t *pbutton = NULL;
|
---|
67 | errno_t rc;
|
---|
68 |
|
---|
69 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
70 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
71 | PCUT_ASSERT_NOT_NULL(pbutton);
|
---|
72 |
|
---|
73 | ui_pbutton_destroy(pbutton);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** ui_pbutton_destroy() can take NULL argument (no-op) */
|
---|
77 | PCUT_TEST(destroy_null)
|
---|
78 | {
|
---|
79 | ui_pbutton_destroy(NULL);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** ui_pbutton_ctl() returns control that has a working virtual destructor */
|
---|
83 | PCUT_TEST(ctl)
|
---|
84 | {
|
---|
85 | ui_pbutton_t *pbutton;
|
---|
86 | ui_control_t *control;
|
---|
87 | errno_t rc;
|
---|
88 |
|
---|
89 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
90 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
91 |
|
---|
92 | control = ui_pbutton_ctl(pbutton);
|
---|
93 | PCUT_ASSERT_NOT_NULL(control);
|
---|
94 |
|
---|
95 | ui_control_destroy(control);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Set flags sets internal field */
|
---|
99 | PCUT_TEST(set_flags)
|
---|
100 | {
|
---|
101 | ui_pbutton_t *pbutton;
|
---|
102 | errno_t rc;
|
---|
103 |
|
---|
104 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
105 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
106 |
|
---|
107 | ui_pbutton_set_flags(pbutton, ui_pbf_no_text_depress);
|
---|
108 | PCUT_ASSERT_INT_EQUALS(ui_pbf_no_text_depress, pbutton->flags);
|
---|
109 |
|
---|
110 | ui_pbutton_destroy(pbutton);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /** Set button rectangle sets internal field */
|
---|
114 | PCUT_TEST(set_rect)
|
---|
115 | {
|
---|
116 | ui_pbutton_t *pbutton;
|
---|
117 | gfx_rect_t rect;
|
---|
118 | errno_t rc;
|
---|
119 |
|
---|
120 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
121 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
122 |
|
---|
123 | rect.p0.x = 1;
|
---|
124 | rect.p0.y = 2;
|
---|
125 | rect.p1.x = 3;
|
---|
126 | rect.p1.y = 4;
|
---|
127 |
|
---|
128 | ui_pbutton_set_rect(pbutton, &rect);
|
---|
129 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, pbutton->rect.p0.x);
|
---|
130 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, pbutton->rect.p0.y);
|
---|
131 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, pbutton->rect.p1.x);
|
---|
132 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, pbutton->rect.p1.y);
|
---|
133 |
|
---|
134 | ui_pbutton_destroy(pbutton);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /** Set default flag sets internal field */
|
---|
138 | PCUT_TEST(set_default)
|
---|
139 | {
|
---|
140 | ui_pbutton_t *pbutton;
|
---|
141 | errno_t rc;
|
---|
142 |
|
---|
143 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
144 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
145 |
|
---|
146 | ui_pbutton_set_default(pbutton, true);
|
---|
147 | PCUT_ASSERT_TRUE(pbutton->isdefault);
|
---|
148 |
|
---|
149 | ui_pbutton_set_default(pbutton, false);
|
---|
150 | PCUT_ASSERT_FALSE(pbutton->isdefault);
|
---|
151 |
|
---|
152 | ui_pbutton_destroy(pbutton);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /** Get light gets internal field */
|
---|
156 | PCUT_TEST(get_light)
|
---|
157 | {
|
---|
158 | ui_pbutton_t *pbutton;
|
---|
159 | errno_t rc;
|
---|
160 |
|
---|
161 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
162 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
163 |
|
---|
164 | pbutton->light = true;
|
---|
165 | PCUT_ASSERT_TRUE(ui_pbutton_get_light(pbutton));
|
---|
166 |
|
---|
167 | pbutton->light = false;
|
---|
168 | PCUT_ASSERT_FALSE(ui_pbutton_get_light(pbutton));
|
---|
169 |
|
---|
170 | ui_pbutton_destroy(pbutton);
|
---|
171 | }
|
---|
172 |
|
---|
173 | /** Set light sets internal field */
|
---|
174 | PCUT_TEST(set_light)
|
---|
175 | {
|
---|
176 | ui_pbutton_t *pbutton;
|
---|
177 | errno_t rc;
|
---|
178 |
|
---|
179 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
180 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
181 |
|
---|
182 | ui_pbutton_set_light(pbutton, true);
|
---|
183 | PCUT_ASSERT_TRUE(pbutton->light);
|
---|
184 |
|
---|
185 | ui_pbutton_set_light(pbutton, false);
|
---|
186 | PCUT_ASSERT_FALSE(pbutton->light);
|
---|
187 |
|
---|
188 | ui_pbutton_destroy(pbutton);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /** Set caption sets internal field */
|
---|
192 | PCUT_TEST(set_caption)
|
---|
193 | {
|
---|
194 | ui_pbutton_t *pbutton;
|
---|
195 | errno_t rc;
|
---|
196 |
|
---|
197 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
198 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
199 |
|
---|
200 | PCUT_ASSERT_STR_EQUALS("Hello", pbutton->caption);
|
---|
201 |
|
---|
202 | rc = ui_pbutton_set_caption(pbutton, "World");
|
---|
203 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
204 |
|
---|
205 | PCUT_ASSERT_STR_EQUALS("World", pbutton->caption);
|
---|
206 |
|
---|
207 | ui_pbutton_destroy(pbutton);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /** Paint button */
|
---|
211 | PCUT_TEST(paint)
|
---|
212 | {
|
---|
213 | errno_t rc;
|
---|
214 | gfx_context_t *gc = NULL;
|
---|
215 | test_gc_t tgc;
|
---|
216 | ui_resource_t *resource = NULL;
|
---|
217 | ui_pbutton_t *pbutton;
|
---|
218 |
|
---|
219 | memset(&tgc, 0, sizeof(tgc));
|
---|
220 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
221 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
222 |
|
---|
223 | rc = ui_resource_create(gc, false, &resource);
|
---|
224 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
225 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
226 |
|
---|
227 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
228 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
229 |
|
---|
230 | rc = ui_pbutton_paint(pbutton);
|
---|
231 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
232 |
|
---|
233 | ui_pbutton_destroy(pbutton);
|
---|
234 | ui_resource_destroy(resource);
|
---|
235 |
|
---|
236 | rc = gfx_context_delete(gc);
|
---|
237 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
238 | }
|
---|
239 |
|
---|
240 | /** Test ui_pbutton_clicked() */
|
---|
241 | PCUT_TEST(clicked)
|
---|
242 | {
|
---|
243 | errno_t rc;
|
---|
244 | ui_pbutton_t *pbutton;
|
---|
245 | test_cb_resp_t resp;
|
---|
246 |
|
---|
247 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
248 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
249 |
|
---|
250 | /* Clicked with no callbacks set */
|
---|
251 | ui_pbutton_clicked(pbutton);
|
---|
252 |
|
---|
253 | /* Clicked with callback not implementing clicked */
|
---|
254 | ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
|
---|
255 | ui_pbutton_clicked(pbutton);
|
---|
256 |
|
---|
257 | /* Clicked with real callback set */
|
---|
258 | resp.clicked = false;
|
---|
259 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
260 | ui_pbutton_clicked(pbutton);
|
---|
261 | PCUT_ASSERT_TRUE(resp.clicked);
|
---|
262 |
|
---|
263 | ui_pbutton_destroy(pbutton);
|
---|
264 | }
|
---|
265 |
|
---|
266 | /** Test ui_pbutton_down() */
|
---|
267 | PCUT_TEST(down)
|
---|
268 | {
|
---|
269 | errno_t rc;
|
---|
270 | ui_pbutton_t *pbutton;
|
---|
271 | test_cb_resp_t resp;
|
---|
272 |
|
---|
273 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
274 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
275 |
|
---|
276 | /* Down with no callbacks set */
|
---|
277 | ui_pbutton_clicked(pbutton);
|
---|
278 |
|
---|
279 | /* Down with callback not implementing down */
|
---|
280 | ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
|
---|
281 | ui_pbutton_down(pbutton);
|
---|
282 |
|
---|
283 | /* Down with real callback set */
|
---|
284 | resp.down = false;
|
---|
285 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
286 | ui_pbutton_down(pbutton);
|
---|
287 | PCUT_ASSERT_TRUE(resp.down);
|
---|
288 |
|
---|
289 | ui_pbutton_destroy(pbutton);
|
---|
290 | }
|
---|
291 |
|
---|
292 | /** Test ui_pbutton_up() */
|
---|
293 | PCUT_TEST(up)
|
---|
294 | {
|
---|
295 | errno_t rc;
|
---|
296 | ui_pbutton_t *pbutton;
|
---|
297 | test_cb_resp_t resp;
|
---|
298 |
|
---|
299 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
300 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
301 |
|
---|
302 | /* Up with no callbacks set */
|
---|
303 | ui_pbutton_clicked(pbutton);
|
---|
304 |
|
---|
305 | /* Up with callback not implementing up */
|
---|
306 | ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
|
---|
307 | ui_pbutton_up(pbutton);
|
---|
308 |
|
---|
309 | /* Up with real callback set */
|
---|
310 | resp.up = false;
|
---|
311 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
312 | ui_pbutton_up(pbutton);
|
---|
313 | PCUT_ASSERT_TRUE(resp.up);
|
---|
314 |
|
---|
315 | ui_pbutton_destroy(pbutton);
|
---|
316 | }
|
---|
317 |
|
---|
318 | /** Press and release button */
|
---|
319 | PCUT_TEST(press_release)
|
---|
320 | {
|
---|
321 | errno_t rc;
|
---|
322 | gfx_context_t *gc = NULL;
|
---|
323 | test_gc_t tgc;
|
---|
324 | ui_resource_t *resource = NULL;
|
---|
325 | ui_pbutton_t *pbutton;
|
---|
326 | test_cb_resp_t resp;
|
---|
327 |
|
---|
328 | memset(&tgc, 0, sizeof(tgc));
|
---|
329 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
330 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
331 |
|
---|
332 | rc = ui_resource_create(gc, false, &resource);
|
---|
333 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
334 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
335 |
|
---|
336 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
337 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
338 |
|
---|
339 | resp.clicked = false;
|
---|
340 | resp.down = false;
|
---|
341 | resp.up = false;
|
---|
342 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
343 |
|
---|
344 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
345 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
346 |
|
---|
347 | ui_pbutton_press(pbutton);
|
---|
348 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
349 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
350 | PCUT_ASSERT_TRUE(resp.down);
|
---|
351 | PCUT_ASSERT_FALSE(resp.up);
|
---|
352 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
353 |
|
---|
354 | ui_pbutton_release(pbutton);
|
---|
355 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
356 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
357 | PCUT_ASSERT_TRUE(resp.up);
|
---|
358 | PCUT_ASSERT_TRUE(resp.clicked);
|
---|
359 |
|
---|
360 | ui_pbutton_destroy(pbutton);
|
---|
361 | ui_resource_destroy(resource);
|
---|
362 |
|
---|
363 | rc = gfx_context_delete(gc);
|
---|
364 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
365 | }
|
---|
366 |
|
---|
367 | /** Press, leave and release button */
|
---|
368 | PCUT_TEST(press_leave_release)
|
---|
369 | {
|
---|
370 | errno_t rc;
|
---|
371 | gfx_context_t *gc = NULL;
|
---|
372 | test_gc_t tgc;
|
---|
373 | ui_resource_t *resource = NULL;
|
---|
374 | ui_pbutton_t *pbutton;
|
---|
375 | test_cb_resp_t resp;
|
---|
376 |
|
---|
377 | memset(&tgc, 0, sizeof(tgc));
|
---|
378 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
379 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
380 |
|
---|
381 | rc = ui_resource_create(gc, false, &resource);
|
---|
382 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
383 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
384 |
|
---|
385 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
386 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
387 |
|
---|
388 | resp.clicked = false;
|
---|
389 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
390 |
|
---|
391 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
392 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
393 |
|
---|
394 | ui_pbutton_press(pbutton);
|
---|
395 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
396 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
397 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
398 |
|
---|
399 | ui_pbutton_leave(pbutton);
|
---|
400 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
401 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
402 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
403 |
|
---|
404 | ui_pbutton_release(pbutton);
|
---|
405 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
406 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
407 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
408 |
|
---|
409 | ui_pbutton_destroy(pbutton);
|
---|
410 | ui_resource_destroy(resource);
|
---|
411 |
|
---|
412 | rc = gfx_context_delete(gc);
|
---|
413 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
414 | }
|
---|
415 |
|
---|
416 | /** Press, leave, enter and release button */
|
---|
417 | PCUT_TEST(press_leave_enter_release)
|
---|
418 | {
|
---|
419 | errno_t rc;
|
---|
420 | gfx_context_t *gc = NULL;
|
---|
421 | test_gc_t tgc;
|
---|
422 | ui_resource_t *resource = NULL;
|
---|
423 | ui_pbutton_t *pbutton;
|
---|
424 | test_cb_resp_t resp;
|
---|
425 |
|
---|
426 | memset(&tgc, 0, sizeof(tgc));
|
---|
427 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
428 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
429 |
|
---|
430 | rc = ui_resource_create(gc, false, &resource);
|
---|
431 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
432 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
433 |
|
---|
434 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
435 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
436 |
|
---|
437 | resp.clicked = false;
|
---|
438 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
439 |
|
---|
440 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
441 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
442 |
|
---|
443 | ui_pbutton_press(pbutton);
|
---|
444 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
445 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
446 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
447 |
|
---|
448 | ui_pbutton_leave(pbutton);
|
---|
449 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
450 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
451 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
452 |
|
---|
453 | ui_pbutton_enter(pbutton);
|
---|
454 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
455 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
456 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
457 |
|
---|
458 | ui_pbutton_release(pbutton);
|
---|
459 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
460 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
461 | PCUT_ASSERT_TRUE(resp.clicked);
|
---|
462 |
|
---|
463 | ui_pbutton_destroy(pbutton);
|
---|
464 | ui_resource_destroy(resource);
|
---|
465 |
|
---|
466 | rc = gfx_context_delete(gc);
|
---|
467 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
468 | }
|
---|
469 |
|
---|
470 | /** ui_pos_event() correctly translates POS_PRESS/POS_RELEASE */
|
---|
471 | PCUT_TEST(pos_event_press_release)
|
---|
472 | {
|
---|
473 | errno_t rc;
|
---|
474 | gfx_context_t *gc = NULL;
|
---|
475 | test_gc_t tgc;
|
---|
476 | ui_resource_t *resource = NULL;
|
---|
477 | ui_pbutton_t *pbutton;
|
---|
478 | ui_evclaim_t claim;
|
---|
479 | pos_event_t event;
|
---|
480 | gfx_rect_t rect;
|
---|
481 |
|
---|
482 | memset(&tgc, 0, sizeof(tgc));
|
---|
483 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
484 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
485 |
|
---|
486 | rc = ui_resource_create(gc, false, &resource);
|
---|
487 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
488 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
489 |
|
---|
490 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
491 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
492 |
|
---|
493 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
494 |
|
---|
495 | rect.p0.x = 10;
|
---|
496 | rect.p0.y = 20;
|
---|
497 | rect.p1.x = 30;
|
---|
498 | rect.p1.y = 40;
|
---|
499 | ui_pbutton_set_rect(pbutton, &rect);
|
---|
500 |
|
---|
501 | /* Press outside is not claimed and does nothing */
|
---|
502 | event.type = POS_PRESS;
|
---|
503 | event.hpos = 9;
|
---|
504 | event.vpos = 20;
|
---|
505 | claim = ui_pbutton_pos_event(pbutton, &event);
|
---|
506 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
507 | PCUT_ASSERT_EQUALS(ui_unclaimed, claim);
|
---|
508 |
|
---|
509 | /* Press inside is claimed and depresses button */
|
---|
510 | event.type = POS_PRESS;
|
---|
511 | event.hpos = 10;
|
---|
512 | event.vpos = 20;
|
---|
513 | claim = ui_pbutton_pos_event(pbutton, &event);
|
---|
514 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
515 | PCUT_ASSERT_EQUALS(ui_claimed, claim);
|
---|
516 |
|
---|
517 | /* Release outside (or anywhere) is claimed and relases button */
|
---|
518 | event.type = POS_RELEASE;
|
---|
519 | event.hpos = 9;
|
---|
520 | event.vpos = 20;
|
---|
521 | claim = ui_pbutton_pos_event(pbutton, &event);
|
---|
522 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
523 | PCUT_ASSERT_EQUALS(ui_claimed, claim);
|
---|
524 |
|
---|
525 | ui_pbutton_destroy(pbutton);
|
---|
526 | ui_resource_destroy(resource);
|
---|
527 |
|
---|
528 | rc = gfx_context_delete(gc);
|
---|
529 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
530 | }
|
---|
531 |
|
---|
532 | /** ui_pos_event() correctly translates POS_UPDATE to enter/leave */
|
---|
533 | PCUT_TEST(pos_event_enter_leave)
|
---|
534 | {
|
---|
535 | errno_t rc;
|
---|
536 | gfx_context_t *gc = NULL;
|
---|
537 | test_gc_t tgc;
|
---|
538 | ui_resource_t *resource = NULL;
|
---|
539 | ui_pbutton_t *pbutton;
|
---|
540 | pos_event_t event;
|
---|
541 | gfx_rect_t rect;
|
---|
542 |
|
---|
543 | memset(&tgc, 0, sizeof(tgc));
|
---|
544 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
545 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
546 |
|
---|
547 | rc = ui_resource_create(gc, false, &resource);
|
---|
548 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
549 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
550 |
|
---|
551 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
552 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
553 |
|
---|
554 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
555 |
|
---|
556 | rect.p0.x = 10;
|
---|
557 | rect.p0.y = 20;
|
---|
558 | rect.p1.x = 30;
|
---|
559 | rect.p1.y = 40;
|
---|
560 | ui_pbutton_set_rect(pbutton, &rect);
|
---|
561 |
|
---|
562 | /* Moving outside does nothing */
|
---|
563 | event.type = POS_UPDATE;
|
---|
564 | event.hpos = 9;
|
---|
565 | event.vpos = 20;
|
---|
566 | ui_pbutton_pos_event(pbutton, &event);
|
---|
567 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
568 |
|
---|
569 | /* Moving inside sets inside flag */
|
---|
570 | event.type = POS_UPDATE;
|
---|
571 | event.hpos = 10;
|
---|
572 | event.vpos = 20;
|
---|
573 | ui_pbutton_pos_event(pbutton, &event);
|
---|
574 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
575 |
|
---|
576 | /* Moving outside clears inside flag */
|
---|
577 | event.type = POS_UPDATE;
|
---|
578 | event.hpos = 9;
|
---|
579 | event.vpos = 20;
|
---|
580 | ui_pbutton_pos_event(pbutton, &event);
|
---|
581 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
582 |
|
---|
583 | ui_pbutton_destroy(pbutton);
|
---|
584 | ui_resource_destroy(resource);
|
---|
585 |
|
---|
586 | rc = gfx_context_delete(gc);
|
---|
587 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
588 | }
|
---|
589 |
|
---|
590 | static void test_pbutton_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
591 | {
|
---|
592 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
593 |
|
---|
594 | resp->clicked = true;
|
---|
595 | }
|
---|
596 |
|
---|
597 | static void test_pbutton_down(ui_pbutton_t *pbutton, void *arg)
|
---|
598 | {
|
---|
599 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
600 |
|
---|
601 | resp->down = true;
|
---|
602 | }
|
---|
603 |
|
---|
604 | static void test_pbutton_up(ui_pbutton_t *pbutton, void *arg)
|
---|
605 | {
|
---|
606 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
607 |
|
---|
608 | resp->up = true;
|
---|
609 | }
|
---|
610 |
|
---|
611 | PCUT_EXPORT(pbutton);
|
---|