Editor needs to hide cursor when menu bar is activated
[helenos.git] / uspace / lib / ui / test / resource.c
blob1e487d1e12bbd16c105667253ca00ba9d484cf28
1 /*
2 * Copyright (c) 2021 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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"
36 PCUT_INIT;
38 PCUT_TEST_SUITE(resource);
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 *);
46 static void test_expose(void *);
48 static gfx_context_ops_t ops = {
49 .bitmap_create = testgc_bitmap_create,
50 .bitmap_destroy = testgc_bitmap_destroy,
51 .bitmap_render = testgc_bitmap_render,
52 .bitmap_get_alloc = testgc_bitmap_get_alloc
55 typedef struct {
56 bool bm_created;
57 bool bm_destroyed;
58 gfx_bitmap_params_t bm_params;
59 void *bm_pixels;
60 gfx_rect_t bm_srect;
61 gfx_coord2_t bm_offs;
62 bool bm_rendered;
63 bool bm_got_alloc;
64 } test_gc_t;
66 typedef struct {
67 test_gc_t *tgc;
68 gfx_bitmap_alloc_t alloc;
69 bool myalloc;
70 } testgc_bitmap_t;
72 typedef struct {
73 bool expose;
74 } test_resp_t;
76 /** Create and destroy UI resource */
77 PCUT_TEST(create_destroy)
79 errno_t rc;
80 gfx_context_t *gc = NULL;
81 test_gc_t tgc;
82 ui_resource_t *resource = NULL;
84 memset(&tgc, 0, sizeof(tgc));
85 rc = gfx_context_new(&ops, &tgc, &gc);
86 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
88 rc = ui_resource_create(gc, false, &resource);
89 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
90 PCUT_ASSERT_NOT_NULL(resource);
92 PCUT_ASSERT_NOT_NULL(resource->tface);
93 PCUT_ASSERT_NOT_NULL(resource->font);
95 ui_resource_destroy(resource);
97 rc = gfx_context_delete(gc);
98 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
101 /** ui_resource_destroy() can take NULL argument (no-op) */
102 PCUT_TEST(destroy_null)
104 ui_resource_destroy(NULL);
107 /** ui_resource_set_expose_cb() / ui_resource_expose() */
108 PCUT_TEST(set_expose_cb_expose)
110 errno_t rc;
111 gfx_context_t *gc = NULL;
112 test_gc_t tgc;
113 ui_resource_t *resource = NULL;
114 test_resp_t resp;
116 memset(&tgc, 0, sizeof(tgc));
117 rc = gfx_context_new(&ops, &tgc, &gc);
118 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
120 rc = ui_resource_create(gc, false, &resource);
121 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
122 PCUT_ASSERT_NOT_NULL(resource);
124 ui_resource_set_expose_cb(resource, test_expose, &resp);
126 resp.expose = false;
127 ui_resource_expose(resource);
128 PCUT_ASSERT_TRUE(resp.expose);
130 ui_resource_destroy(resource);
132 rc = gfx_context_delete(gc);
133 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
136 /** ui_resource_get_font() returns the font */
137 PCUT_TEST(get_font)
139 errno_t rc;
140 gfx_context_t *gc = NULL;
141 test_gc_t tgc;
142 ui_resource_t *resource = NULL;
143 gfx_font_t *font;
145 memset(&tgc, 0, sizeof(tgc));
146 rc = gfx_context_new(&ops, &tgc, &gc);
147 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
149 rc = ui_resource_create(gc, false, &resource);
150 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
151 PCUT_ASSERT_NOT_NULL(resource);
153 font = ui_resource_get_font(resource);
154 PCUT_ASSERT_EQUALS(resource->font, font);
156 ui_resource_destroy(resource);
158 rc = gfx_context_delete(gc);
159 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
162 /** ui_resource_is_textmode() returns the textmode flag */
163 PCUT_TEST(is_textmode)
165 errno_t rc;
166 gfx_context_t *gc = NULL;
167 test_gc_t tgc;
168 ui_resource_t *resource = NULL;
170 memset(&tgc, 0, sizeof(tgc));
171 rc = gfx_context_new(&ops, &tgc, &gc);
172 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
174 rc = ui_resource_create(gc, false, &resource);
175 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
176 PCUT_ASSERT_NOT_NULL(resource);
178 /* To make sure let's test both true and false case */
179 resource->textmode = true;
180 PCUT_ASSERT_TRUE(ui_resource_is_textmode(resource));
181 resource->textmode = false;
182 PCUT_ASSERT_FALSE(ui_resource_is_textmode(resource));
184 ui_resource_destroy(resource);
186 rc = gfx_context_delete(gc);
187 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
190 /** ui_resource_get_wnd_face_color() returns window face color */
191 PCUT_TEST(get_wnd_face_color)
193 errno_t rc;
194 gfx_context_t *gc = NULL;
195 test_gc_t tgc;
196 ui_resource_t *resource = NULL;
197 gfx_color_t *color;
199 memset(&tgc, 0, sizeof(tgc));
200 rc = gfx_context_new(&ops, &tgc, &gc);
201 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
203 rc = ui_resource_create(gc, false, &resource);
204 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
205 PCUT_ASSERT_NOT_NULL(resource);
207 color = ui_resource_get_wnd_face_color(resource);
208 PCUT_ASSERT_EQUALS(resource->wnd_face_color, color);
210 ui_resource_destroy(resource);
212 rc = gfx_context_delete(gc);
213 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
216 /** ui_resource_get_wnd_text_color() returns window text color */
217 PCUT_TEST(get_wnd_text_color)
219 errno_t rc;
220 gfx_context_t *gc = NULL;
221 test_gc_t tgc;
222 ui_resource_t *resource = NULL;
223 gfx_color_t *color;
225 memset(&tgc, 0, sizeof(tgc));
226 rc = gfx_context_new(&ops, &tgc, &gc);
227 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
229 rc = ui_resource_create(gc, false, &resource);
230 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
231 PCUT_ASSERT_NOT_NULL(resource);
233 color = ui_resource_get_wnd_text_color(resource);
234 PCUT_ASSERT_EQUALS(resource->wnd_text_color, color);
236 ui_resource_destroy(resource);
238 rc = gfx_context_delete(gc);
239 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
242 static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
243 gfx_bitmap_alloc_t *alloc, void **rbm)
245 test_gc_t *tgc = (test_gc_t *) arg;
246 testgc_bitmap_t *tbm;
248 tbm = calloc(1, sizeof(testgc_bitmap_t));
249 if (tbm == NULL)
250 return ENOMEM;
252 if (alloc == NULL) {
253 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
254 sizeof(uint32_t);
255 tbm->alloc.off0 = 0;
256 tbm->alloc.pixels = calloc(sizeof(uint32_t),
257 (params->rect.p1.x - params->rect.p0.x) *
258 (params->rect.p1.y - params->rect.p0.y));
259 tbm->myalloc = true;
260 if (tbm->alloc.pixels == NULL) {
261 free(tbm);
262 return ENOMEM;
264 } else {
265 tbm->alloc = *alloc;
268 tbm->tgc = tgc;
269 tgc->bm_created = true;
270 tgc->bm_params = *params;
271 tgc->bm_pixels = tbm->alloc.pixels;
272 *rbm = (void *)tbm;
273 return EOK;
276 static errno_t testgc_bitmap_destroy(void *bm)
278 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
279 if (tbm->myalloc)
280 free(tbm->alloc.pixels);
281 tbm->tgc->bm_destroyed = true;
282 free(tbm);
283 return EOK;
286 static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
287 gfx_coord2_t *offs)
289 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
290 tbm->tgc->bm_rendered = true;
291 tbm->tgc->bm_srect = *srect;
292 tbm->tgc->bm_offs = *offs;
293 return EOK;
296 static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
298 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
299 *alloc = tbm->alloc;
300 tbm->tgc->bm_got_alloc = true;
301 return EOK;
304 static void test_expose(void *arg)
306 test_resp_t *resp = (test_resp_t *) arg;
308 resp->expose = true;
311 PCUT_EXPORT(resource);