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