source: mainline/uspace/lib/ui/test/paint.c@ 35cffea

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

Maximizing/unmaximizing a window

  • Property mode set to 100644
File size: 13.8 KB
Line 
1/*
2 * Copyright (c) 2022 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/paint.h>
35#include <ui/resource.h>
36
37PCUT_INIT;
38
39PCUT_TEST_SUITE(paint);
40
41static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
42static errno_t testgc_set_color(void *, gfx_color_t *);
43static errno_t testgc_fill_rect(void *, gfx_rect_t *);
44static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
45 gfx_bitmap_alloc_t *, void **);
46static errno_t testgc_bitmap_destroy(void *);
47static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
48static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
49
50static gfx_context_ops_t ops = {
51 .set_clip_rect = testgc_set_clip_rect,
52 .set_color = testgc_set_color,
53 .fill_rect = testgc_fill_rect,
54 .bitmap_create = testgc_bitmap_create,
55 .bitmap_destroy = testgc_bitmap_destroy,
56 .bitmap_render = testgc_bitmap_render,
57 .bitmap_get_alloc = testgc_bitmap_get_alloc
58};
59
60typedef struct {
61 bool bm_created;
62 bool bm_destroyed;
63 gfx_bitmap_params_t bm_params;
64 void *bm_pixels;
65 gfx_rect_t bm_srect;
66 gfx_coord2_t bm_offs;
67 bool bm_rendered;
68 bool bm_got_alloc;
69} test_gc_t;
70
71typedef struct {
72 test_gc_t *tgc;
73 gfx_bitmap_alloc_t alloc;
74 bool myalloc;
75} testgc_bitmap_t;
76
77/** Paint bevel */
78PCUT_TEST(bevel)
79{
80 errno_t rc;
81 gfx_context_t *gc = NULL;
82 test_gc_t tgc;
83 gfx_rect_t rect;
84 gfx_color_t *color1;
85 gfx_color_t *color2;
86 gfx_rect_t inside;
87
88 memset(&tgc, 0, sizeof(tgc));
89 rc = gfx_context_new(&ops, &tgc, &gc);
90 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
91
92 rc = gfx_color_new_rgb_i16(1, 2, 3, &color1);
93 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
94
95 rc = gfx_color_new_rgb_i16(4, 5, 6, &color2);
96 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
97
98 rect.p0.x = 10;
99 rect.p0.y = 20;
100 rect.p1.x = 30;
101 rect.p1.y = 40;
102
103 /* Paint bevel with NULL 'inside' output parameter */
104 rc = ui_paint_bevel(gc, &rect, color1, color2, 2, NULL);
105 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
106
107 /* Paint bevel with valid 'inside' output parameter */
108 rc = ui_paint_bevel(gc, &rect, color1, color2, 2, &inside);
109 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
110
111 gfx_color_delete(color2);
112 gfx_color_delete(color1);
113 rc = gfx_context_delete(gc);
114 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
115}
116
117/** Get bevel inside */
118PCUT_TEST(get_bevel_inside)
119{
120 errno_t rc;
121 gfx_context_t *gc = NULL;
122 test_gc_t tgc;
123 gfx_rect_t rect;
124 gfx_rect_t inside;
125
126 memset(&tgc, 0, sizeof(tgc));
127 rc = gfx_context_new(&ops, &tgc, &gc);
128 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
129
130 rect.p0.x = 10;
131 rect.p0.y = 20;
132 rect.p1.x = 30;
133 rect.p1.y = 40;
134
135 ui_paint_get_bevel_inside(gc, &rect, 2, &inside);
136 PCUT_ASSERT_INT_EQUALS(12, inside.p0.x);
137 PCUT_ASSERT_INT_EQUALS(22, inside.p0.y);
138 PCUT_ASSERT_INT_EQUALS(28, inside.p1.x);
139 PCUT_ASSERT_INT_EQUALS(38, inside.p1.y);
140
141 rc = gfx_context_delete(gc);
142 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
143}
144
145/** Paint inset frame */
146PCUT_TEST(inset_frame)
147{
148 errno_t rc;
149 gfx_context_t *gc = NULL;
150 ui_resource_t *resource = NULL;
151 test_gc_t tgc;
152 gfx_rect_t rect;
153 gfx_rect_t inside;
154
155 memset(&tgc, 0, sizeof(tgc));
156 rc = gfx_context_new(&ops, &tgc, &gc);
157 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
158
159 rc = ui_resource_create(gc, false, &resource);
160 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
161 PCUT_ASSERT_NOT_NULL(resource);
162
163 rect.p0.x = 10;
164 rect.p0.y = 20;
165 rect.p1.x = 30;
166 rect.p1.y = 40;
167
168 /* Paint inset frame with NULL 'inside' output parameter */
169 rc = ui_paint_inset_frame(resource, &rect, NULL);
170 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
171
172 /* Paint inset frame with valid 'inside' output parameter */
173 rc = ui_paint_inset_frame(resource, &rect, &inside);
174 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
175
176 ui_resource_destroy(resource);
177 rc = gfx_context_delete(gc);
178 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
179}
180
181/** Get inset frame inside */
182PCUT_TEST(get_inset_frame_inside)
183{
184 errno_t rc;
185 gfx_context_t *gc = NULL;
186 ui_resource_t *resource = NULL;
187 test_gc_t tgc;
188 gfx_rect_t rect;
189 gfx_rect_t inside;
190
191 memset(&tgc, 0, sizeof(tgc));
192 rc = gfx_context_new(&ops, &tgc, &gc);
193 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
194
195 rc = ui_resource_create(gc, false, &resource);
196 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
197 PCUT_ASSERT_NOT_NULL(resource);
198
199 rect.p0.x = 10;
200 rect.p0.y = 20;
201 rect.p1.x = 30;
202 rect.p1.y = 40;
203
204 ui_paint_get_inset_frame_inside(resource, &rect, &inside);
205 PCUT_ASSERT_INT_EQUALS(12, inside.p0.x);
206 PCUT_ASSERT_INT_EQUALS(22, inside.p0.y);
207 PCUT_ASSERT_INT_EQUALS(28, inside.p1.x);
208 PCUT_ASSERT_INT_EQUALS(38, inside.p1.y);
209
210 ui_resource_destroy(resource);
211 rc = gfx_context_delete(gc);
212 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
213}
214
215/** Paint filled circle */
216PCUT_TEST(filled_circle)
217{
218 errno_t rc;
219 gfx_context_t *gc = NULL;
220 test_gc_t tgc;
221 gfx_coord2_t center;
222
223 memset(&tgc, 0, sizeof(tgc));
224 rc = gfx_context_new(&ops, &tgc, &gc);
225 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
226
227 center.x = 0;
228 center.y = 0;
229
230 /* Paint filled circle / upper-left half */
231 rc = ui_paint_filled_circle(gc, &center, 10, ui_fcircle_upleft);
232 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
233
234 /* Paint filled circle / lower-right half */
235 rc = ui_paint_filled_circle(gc, &center, 10, ui_fcircle_lowright);
236 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
237
238 /* Paint entire filled circle */
239 rc = ui_paint_filled_circle(gc, &center, 10, ui_fcircle_entire);
240 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
241
242 rc = gfx_context_delete(gc);
243 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
244}
245
246/** Paint up pointing triangle */
247PCUT_TEST(up_triangle)
248{
249 errno_t rc;
250 gfx_context_t *gc = NULL;
251 test_gc_t tgc;
252 gfx_coord2_t center;
253
254 memset(&tgc, 0, sizeof(tgc));
255 rc = gfx_context_new(&ops, &tgc, &gc);
256 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
257
258 center.x = 0;
259 center.y = 0;
260
261 rc = ui_paint_up_triangle(gc, &center, 5);
262 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
263
264 rc = gfx_context_delete(gc);
265 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
266}
267
268/** Paint down pointing triangle */
269PCUT_TEST(down_triangle)
270{
271 errno_t rc;
272 gfx_context_t *gc = NULL;
273 test_gc_t tgc;
274 gfx_coord2_t center;
275
276 memset(&tgc, 0, sizeof(tgc));
277 rc = gfx_context_new(&ops, &tgc, &gc);
278 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
279
280 center.x = 0;
281 center.y = 0;
282
283 rc = ui_paint_down_triangle(gc, &center, 5);
284 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
285
286 rc = gfx_context_delete(gc);
287 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
288}
289
290/** Paint left pointing triangle */
291PCUT_TEST(left_triangle)
292{
293 errno_t rc;
294 gfx_context_t *gc = NULL;
295 test_gc_t tgc;
296 gfx_coord2_t center;
297
298 memset(&tgc, 0, sizeof(tgc));
299 rc = gfx_context_new(&ops, &tgc, &gc);
300 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
301
302 center.x = 0;
303 center.y = 0;
304
305 rc = ui_paint_left_triangle(gc, &center, 5);
306 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
307
308 rc = gfx_context_delete(gc);
309 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
310}
311
312/** Paint right pointing triangle */
313PCUT_TEST(right_triangle)
314{
315 errno_t rc;
316 gfx_context_t *gc = NULL;
317 test_gc_t tgc;
318 gfx_coord2_t center;
319
320 memset(&tgc, 0, sizeof(tgc));
321 rc = gfx_context_new(&ops, &tgc, &gc);
322 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
323
324 center.x = 0;
325 center.y = 0;
326
327 rc = ui_paint_right_triangle(gc, &center, 5);
328 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
329
330 rc = gfx_context_delete(gc);
331 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
332}
333
334/** Paint diagonal cross (X) */
335PCUT_TEST(cross)
336{
337 errno_t rc;
338 gfx_context_t *gc = NULL;
339 test_gc_t tgc;
340 gfx_coord2_t center;
341
342 memset(&tgc, 0, sizeof(tgc));
343 rc = gfx_context_new(&ops, &tgc, &gc);
344 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
345
346 center.x = 0;
347 center.y = 0;
348
349 rc = ui_paint_cross(gc, &center, 5, 1, 2);
350 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
351
352 rc = gfx_context_delete(gc);
353 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
354}
355
356/** Paint maximize icon */
357PCUT_TEST(maxicon)
358{
359 errno_t rc;
360 gfx_context_t *gc = NULL;
361 ui_resource_t *resource = NULL;
362 test_gc_t tgc;
363 gfx_coord2_t center;
364
365 memset(&tgc, 0, sizeof(tgc));
366 rc = gfx_context_new(&ops, &tgc, &gc);
367 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
368
369 rc = ui_resource_create(gc, false, &resource);
370 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
371 PCUT_ASSERT_NOT_NULL(resource);
372
373 center.x = 0;
374 center.y = 0;
375
376 rc = ui_paint_maxicon(resource, &center, 8, 6);
377 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
378
379 ui_resource_destroy(resource);
380 rc = gfx_context_delete(gc);
381 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
382}
383
384/** Paint unmaximize icon */
385PCUT_TEST(unmaxicon)
386{
387 errno_t rc;
388 gfx_context_t *gc = NULL;
389 ui_resource_t *resource = NULL;
390 test_gc_t tgc;
391 gfx_coord2_t center;
392
393 memset(&tgc, 0, sizeof(tgc));
394 rc = gfx_context_new(&ops, &tgc, &gc);
395 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
396
397 rc = ui_resource_create(gc, false, &resource);
398 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
399 PCUT_ASSERT_NOT_NULL(resource);
400
401 center.x = 0;
402 center.y = 0;
403
404 rc = ui_paint_unmaxicon(resource, &center, 8, 8, 3, 3);
405 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
406
407 ui_resource_destroy(resource);
408 rc = gfx_context_delete(gc);
409 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
410}
411
412/** Paint text box */
413PCUT_TEST(text_box)
414{
415 errno_t rc;
416 gfx_context_t *gc = NULL;
417 ui_resource_t *resource = NULL;
418 gfx_color_t *color = NULL;
419 test_gc_t tgc;
420 gfx_rect_t rect;
421
422 memset(&tgc, 0, sizeof(tgc));
423 rc = gfx_context_new(&ops, &tgc, &gc);
424 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
425
426 rc = ui_resource_create(gc, false, &resource);
427 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
428 PCUT_ASSERT_NOT_NULL(resource);
429
430 rc = gfx_color_new_rgb_i16(1, 2, 3, &color);
431 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
432
433 rect.p0.x = 10;
434 rect.p0.y = 20;
435 rect.p1.x = 30;
436 rect.p1.y = 40;
437
438 /* Paint text box */
439 rc = ui_paint_text_box(resource, &rect, ui_box_single, color);
440 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
441
442 gfx_color_delete(color);
443 ui_resource_destroy(resource);
444 rc = gfx_context_delete(gc);
445 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
446}
447
448/** Paint text horizontal brace */
449PCUT_TEST(text_hbrace)
450{
451 errno_t rc;
452 gfx_context_t *gc = NULL;
453 ui_resource_t *resource = NULL;
454 gfx_color_t *color = NULL;
455 test_gc_t tgc;
456 gfx_rect_t rect;
457
458 memset(&tgc, 0, sizeof(tgc));
459 rc = gfx_context_new(&ops, &tgc, &gc);
460 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
461
462 rc = ui_resource_create(gc, false, &resource);
463 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
464 PCUT_ASSERT_NOT_NULL(resource);
465
466 rc = gfx_color_new_rgb_i16(1, 2, 3, &color);
467 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
468
469 rect.p0.x = 10;
470 rect.p0.y = 20;
471 rect.p1.x = 30;
472 rect.p1.y = 40;
473
474 /* Paint text horizontal brace */
475 rc = ui_paint_text_hbrace(resource, &rect, ui_box_single,
476 color);
477
478 gfx_color_delete(color);
479 ui_resource_destroy(resource);
480 rc = gfx_context_delete(gc);
481 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
482}
483
484/** Paint text rectangle */
485PCUT_TEST(text_rect)
486{
487 errno_t rc;
488 gfx_context_t *gc = NULL;
489 ui_resource_t *resource = NULL;
490 gfx_color_t *color = NULL;
491 test_gc_t tgc;
492 gfx_rect_t rect;
493
494 memset(&tgc, 0, sizeof(tgc));
495 rc = gfx_context_new(&ops, &tgc, &gc);
496 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
497
498 rc = ui_resource_create(gc, false, &resource);
499 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
500 PCUT_ASSERT_NOT_NULL(resource);
501
502 rc = gfx_color_new_rgb_i16(1, 2, 3, &color);
503 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
504
505 rect.p0.x = 10;
506 rect.p0.y = 20;
507 rect.p1.x = 30;
508 rect.p1.y = 40;
509
510 /* Paint text box */
511 rc = ui_paint_text_rect(resource, &rect, color, "A");
512 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
513
514 gfx_color_delete(color);
515 ui_resource_destroy(resource);
516 rc = gfx_context_delete(gc);
517 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
518}
519
520static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
521{
522 (void) arg;
523 (void) rect;
524 return EOK;
525}
526
527static errno_t testgc_set_color(void *arg, gfx_color_t *color)
528{
529 (void) arg;
530 (void) color;
531 return EOK;
532}
533
534static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
535{
536 (void) arg;
537 (void) rect;
538 return EOK;
539}
540
541static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
542 gfx_bitmap_alloc_t *alloc, void **rbm)
543{
544 test_gc_t *tgc = (test_gc_t *) arg;
545 testgc_bitmap_t *tbm;
546
547 tbm = calloc(1, sizeof(testgc_bitmap_t));
548 if (tbm == NULL)
549 return ENOMEM;
550
551 if (alloc == NULL) {
552 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
553 sizeof(uint32_t);
554 tbm->alloc.off0 = 0;
555 tbm->alloc.pixels = calloc(sizeof(uint32_t),
556 (params->rect.p1.x - params->rect.p0.x) *
557 (params->rect.p1.y - params->rect.p0.y));
558 tbm->myalloc = true;
559 if (tbm->alloc.pixels == NULL) {
560 free(tbm);
561 return ENOMEM;
562 }
563 } else {
564 tbm->alloc = *alloc;
565 }
566
567 tbm->tgc = tgc;
568 tgc->bm_created = true;
569 tgc->bm_params = *params;
570 tgc->bm_pixels = tbm->alloc.pixels;
571 *rbm = (void *)tbm;
572 return EOK;
573}
574
575static errno_t testgc_bitmap_destroy(void *bm)
576{
577 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
578 if (tbm->myalloc)
579 free(tbm->alloc.pixels);
580 tbm->tgc->bm_destroyed = true;
581 free(tbm);
582 return EOK;
583}
584
585static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
586 gfx_coord2_t *offs)
587{
588 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
589 tbm->tgc->bm_rendered = true;
590 tbm->tgc->bm_srect = *srect;
591 tbm->tgc->bm_offs = *offs;
592 return EOK;
593}
594
595static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
596{
597 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
598 *alloc = tbm->alloc;
599 tbm->tgc->bm_got_alloc = true;
600 return EOK;
601}
602
603PCUT_EXPORT(paint);
Note: See TracBrowser for help on using the repository browser.