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 <gfx/context.h>
|
---|
30 | #include <mem.h>
|
---|
31 | #include <pcut/pcut.h>
|
---|
32 | #include <stdbool.h>
|
---|
33 | #include <ui/resource.h>
|
---|
34 | #include "../private/resource.h"
|
---|
35 |
|
---|
36 | PCUT_INIT;
|
---|
37 |
|
---|
38 | PCUT_TEST_SUITE(resource);
|
---|
39 |
|
---|
40 | static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
41 | gfx_bitmap_alloc_t *, void **);
|
---|
42 | static errno_t testgc_bitmap_destroy(void *);
|
---|
43 | static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
44 | static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
45 |
|
---|
46 | static gfx_context_ops_t ops = {
|
---|
47 | .bitmap_create = testgc_bitmap_create,
|
---|
48 | .bitmap_destroy = testgc_bitmap_destroy,
|
---|
49 | .bitmap_render = testgc_bitmap_render,
|
---|
50 | .bitmap_get_alloc = testgc_bitmap_get_alloc
|
---|
51 | };
|
---|
52 |
|
---|
53 | typedef struct {
|
---|
54 | bool bm_created;
|
---|
55 | bool bm_destroyed;
|
---|
56 | gfx_bitmap_params_t bm_params;
|
---|
57 | void *bm_pixels;
|
---|
58 | gfx_rect_t bm_srect;
|
---|
59 | gfx_coord2_t bm_offs;
|
---|
60 | bool bm_rendered;
|
---|
61 | bool bm_got_alloc;
|
---|
62 | } test_gc_t;
|
---|
63 |
|
---|
64 | typedef struct {
|
---|
65 | test_gc_t *tgc;
|
---|
66 | gfx_bitmap_alloc_t alloc;
|
---|
67 | bool myalloc;
|
---|
68 | } testgc_bitmap_t;
|
---|
69 |
|
---|
70 | /** Create and destroy UI resource */
|
---|
71 | PCUT_TEST(create_destroy)
|
---|
72 | {
|
---|
73 | errno_t rc;
|
---|
74 | gfx_context_t *gc = NULL;
|
---|
75 | test_gc_t tgc;
|
---|
76 | ui_resource_t *resource = NULL;
|
---|
77 |
|
---|
78 | memset(&tgc, 0, sizeof(tgc));
|
---|
79 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
80 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
81 |
|
---|
82 | rc = ui_resource_create(gc, &resource);
|
---|
83 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
84 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
85 |
|
---|
86 | PCUT_ASSERT_NOT_NULL(resource->tface);
|
---|
87 | PCUT_ASSERT_NOT_NULL(resource->font);
|
---|
88 |
|
---|
89 | ui_resource_destroy(resource);
|
---|
90 |
|
---|
91 | rc = gfx_context_delete(gc);
|
---|
92 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /** ui_resource_destroy() can take NULL argument (no-op) */
|
---|
96 | PCUT_TEST(destroy_null)
|
---|
97 | {
|
---|
98 | ui_resource_destroy(NULL);
|
---|
99 | }
|
---|
100 |
|
---|
101 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
102 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
103 | {
|
---|
104 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
105 | testgc_bitmap_t *tbm;
|
---|
106 |
|
---|
107 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
108 | if (tbm == NULL)
|
---|
109 | return ENOMEM;
|
---|
110 |
|
---|
111 | if (alloc == NULL) {
|
---|
112 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
113 | sizeof(uint32_t);
|
---|
114 | tbm->alloc.off0 = 0;
|
---|
115 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
---|
116 | (params->rect.p1.x - params->rect.p0.x) *
|
---|
117 | (params->rect.p1.y - params->rect.p0.y));
|
---|
118 | tbm->myalloc = true;
|
---|
119 | if (tbm->alloc.pixels == NULL) {
|
---|
120 | free(tbm);
|
---|
121 | return ENOMEM;
|
---|
122 | }
|
---|
123 | } else {
|
---|
124 | tbm->alloc = *alloc;
|
---|
125 | }
|
---|
126 |
|
---|
127 | tbm->tgc = tgc;
|
---|
128 | tgc->bm_created = true;
|
---|
129 | tgc->bm_params = *params;
|
---|
130 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
131 | *rbm = (void *)tbm;
|
---|
132 | return EOK;
|
---|
133 | }
|
---|
134 |
|
---|
135 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
136 | {
|
---|
137 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
138 | if (tbm->myalloc)
|
---|
139 | free(tbm->alloc.pixels);
|
---|
140 | tbm->tgc->bm_destroyed = true;
|
---|
141 | free(tbm);
|
---|
142 | return EOK;
|
---|
143 | }
|
---|
144 |
|
---|
145 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
146 | gfx_coord2_t *offs)
|
---|
147 | {
|
---|
148 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
149 | tbm->tgc->bm_rendered = true;
|
---|
150 | tbm->tgc->bm_srect = *srect;
|
---|
151 | tbm->tgc->bm_offs = *offs;
|
---|
152 | return EOK;
|
---|
153 | }
|
---|
154 |
|
---|
155 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
156 | {
|
---|
157 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
158 | *alloc = tbm->alloc;
|
---|
159 | tbm->tgc->bm_got_alloc = true;
|
---|
160 | return EOK;
|
---|
161 | }
|
---|
162 |
|
---|
163 | PCUT_EXPORT(resource);
|
---|