Editor needs to hide cursor when menu bar is activated
[helenos.git] / uspace / lib / ui / test / label.c
blob934ea51f347ff423b18aa0aff761d3c7921d0be8
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 <gfx/coord.h>
31 #include <mem.h>
32 #include <pcut/pcut.h>
33 #include <stdbool.h>
34 #include <ui/control.h>
35 #include <ui/label.h>
36 #include <ui/resource.h>
37 #include "../private/label.h"
39 PCUT_INIT;
41 PCUT_TEST_SUITE(label);
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 *);
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
64 typedef struct {
65 bool bm_created;
66 bool bm_destroyed;
67 gfx_bitmap_params_t bm_params;
68 void *bm_pixels;
69 gfx_rect_t bm_srect;
70 gfx_coord2_t bm_offs;
71 bool bm_rendered;
72 bool bm_got_alloc;
73 } test_gc_t;
75 typedef struct {
76 test_gc_t *tgc;
77 gfx_bitmap_alloc_t alloc;
78 bool myalloc;
79 } testgc_bitmap_t;
81 typedef struct {
82 bool clicked;
83 } test_cb_resp_t;
85 /** Create and destroy label */
86 PCUT_TEST(create_destroy)
88 ui_label_t *label = NULL;
89 errno_t rc;
91 rc = ui_label_create(NULL, "Hello", &label);
92 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
93 PCUT_ASSERT_NOT_NULL(label);
95 ui_label_destroy(label);
98 /** ui_label_destroy() can take NULL argument (no-op) */
99 PCUT_TEST(destroy_null)
101 ui_label_destroy(NULL);
104 /** ui_label_ctl() returns control that has a working virtual destructor */
105 PCUT_TEST(ctl)
107 ui_label_t *label;
108 ui_control_t *control;
109 errno_t rc;
111 rc = ui_label_create(NULL, "Hello", &label);
112 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
114 control = ui_label_ctl(label);
115 PCUT_ASSERT_NOT_NULL(control);
117 ui_control_destroy(control);
120 /** Set label rectangle sets internal field */
121 PCUT_TEST(set_rect)
123 ui_label_t *label;
124 gfx_rect_t rect;
125 errno_t rc;
127 rc = ui_label_create(NULL, "Hello", &label);
128 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
130 rect.p0.x = 1;
131 rect.p0.y = 2;
132 rect.p1.x = 3;
133 rect.p1.y = 4;
135 ui_label_set_rect(label, &rect);
136 PCUT_ASSERT_INT_EQUALS(rect.p0.x, label->rect.p0.x);
137 PCUT_ASSERT_INT_EQUALS(rect.p0.y, label->rect.p0.y);
138 PCUT_ASSERT_INT_EQUALS(rect.p1.x, label->rect.p1.x);
139 PCUT_ASSERT_INT_EQUALS(rect.p1.y, label->rect.p1.y);
141 ui_label_destroy(label);
144 /** Set label text horizontal alignment sets internal field */
145 PCUT_TEST(set_halign)
147 ui_label_t *label;
148 errno_t rc;
150 rc = ui_label_create(NULL, "Hello", &label);
151 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
153 ui_label_set_halign(label, gfx_halign_left);
154 PCUT_ASSERT_EQUALS(gfx_halign_left, label->halign);
155 ui_label_set_halign(label, gfx_halign_center);
156 PCUT_ASSERT_EQUALS(gfx_halign_center, label->halign);
158 ui_label_destroy(label);
161 /** Set label rectangle sets internal field */
162 PCUT_TEST(set_text)
164 ui_label_t *label;
165 gfx_rect_t rect;
166 errno_t rc;
168 rc = ui_label_create(NULL, "Hello", &label);
169 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
171 rect.p0.x = 1;
172 rect.p0.y = 2;
173 rect.p1.x = 3;
174 rect.p1.y = 4;
176 ui_label_set_rect(label, &rect);
177 PCUT_ASSERT_INT_EQUALS(rect.p0.x, label->rect.p0.x);
178 PCUT_ASSERT_INT_EQUALS(rect.p0.y, label->rect.p0.y);
179 PCUT_ASSERT_INT_EQUALS(rect.p1.x, label->rect.p1.x);
180 PCUT_ASSERT_INT_EQUALS(rect.p1.y, label->rect.p1.y);
182 ui_label_destroy(label);
185 /** Paint label */
186 PCUT_TEST(paint)
188 errno_t rc;
189 gfx_context_t *gc = NULL;
190 test_gc_t tgc;
191 ui_resource_t *resource = NULL;
192 ui_label_t *label;
194 memset(&tgc, 0, sizeof(tgc));
195 rc = gfx_context_new(&ops, &tgc, &gc);
196 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
198 rc = ui_resource_create(gc, false, &resource);
199 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
200 PCUT_ASSERT_NOT_NULL(resource);
202 rc = ui_label_create(resource, "Hello", &label);
203 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
205 rc = ui_label_paint(label);
206 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
208 ui_label_destroy(label);
209 ui_resource_destroy(resource);
211 rc = gfx_context_delete(gc);
212 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
215 static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
217 (void) arg;
218 (void) rect;
219 return EOK;
222 static errno_t testgc_set_color(void *arg, gfx_color_t *color)
224 (void) arg;
225 (void) color;
226 return EOK;
229 static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
231 (void) arg;
232 (void) rect;
233 return EOK;
236 static errno_t testgc_update(void *arg)
238 (void) arg;
239 return EOK;
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 PCUT_EXPORT(label);