source: mainline/uspace/lib/ui/test/label.c@ dcfd422

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dcfd422 was 58a67050, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Support different label text alignment

  • Property mode set to 100644
File size: 6.8 KB
Line 
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 <gfx/coord.h>
31#include <mem.h>
32#include <pcut/pcut.h>
33#include <stdbool.h>
34#include <ui/label.h>
35#include <ui/resource.h>
36#include "../private/label.h"
37
38PCUT_INIT;
39
40PCUT_TEST_SUITE(label);
41
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_color = testgc_set_color,
52 .fill_rect = testgc_fill_rect,
53 .bitmap_create = testgc_bitmap_create,
54 .bitmap_destroy = testgc_bitmap_destroy,
55 .bitmap_render = testgc_bitmap_render,
56 .bitmap_get_alloc = testgc_bitmap_get_alloc
57};
58
59typedef struct {
60 bool bm_created;
61 bool bm_destroyed;
62 gfx_bitmap_params_t bm_params;
63 void *bm_pixels;
64 gfx_rect_t bm_srect;
65 gfx_coord2_t bm_offs;
66 bool bm_rendered;
67 bool bm_got_alloc;
68} test_gc_t;
69
70typedef struct {
71 test_gc_t *tgc;
72 gfx_bitmap_alloc_t alloc;
73 bool myalloc;
74} testgc_bitmap_t;
75
76typedef struct {
77 bool clicked;
78} test_cb_resp_t;
79
80/** Create and destroy button */
81PCUT_TEST(create_destroy)
82{
83 ui_label_t *label = NULL;
84 errno_t rc;
85
86 rc = ui_label_create(NULL, "Hello", &label);
87 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
88 PCUT_ASSERT_NOT_NULL(label);
89
90 ui_label_destroy(label);
91}
92
93/** ui_label_destroy() can take NULL argument (no-op) */
94PCUT_TEST(destroy_null)
95{
96 ui_label_destroy(NULL);
97}
98
99/** Set button rectangle sets internal field */
100PCUT_TEST(set_rect)
101{
102 ui_label_t *label;
103 gfx_rect_t rect;
104 errno_t rc;
105
106 rc = ui_label_create(NULL, "Hello", &label);
107 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
108
109 rect.p0.x = 1;
110 rect.p0.y = 2;
111 rect.p1.x = 3;
112 rect.p1.y = 4;
113
114 ui_label_set_rect(label, &rect);
115 PCUT_ASSERT_INT_EQUALS(rect.p0.x, label->rect.p0.x);
116 PCUT_ASSERT_INT_EQUALS(rect.p0.y, label->rect.p0.y);
117 PCUT_ASSERT_INT_EQUALS(rect.p1.x, label->rect.p1.x);
118 PCUT_ASSERT_INT_EQUALS(rect.p1.y, label->rect.p1.y);
119
120 ui_label_destroy(label);
121}
122
123/** Set button text horizontal alignment sets internal field */
124PCUT_TEST(set_halign)
125{
126 ui_label_t *label;
127 errno_t rc;
128
129 rc = ui_label_create(NULL, "Hello", &label);
130 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
131
132 ui_label_set_halign(label, gfx_halign_left);
133 PCUT_ASSERT_EQUALS(gfx_halign_left, label->halign);
134 ui_label_set_halign(label, gfx_halign_center);
135 PCUT_ASSERT_EQUALS(gfx_halign_center, label->halign);
136
137 ui_label_destroy(label);
138}
139
140/** Set button rectangle sets internal field */
141PCUT_TEST(set_text)
142{
143 ui_label_t *label;
144 gfx_rect_t rect;
145 errno_t rc;
146
147 rc = ui_label_create(NULL, "Hello", &label);
148 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
149
150 rect.p0.x = 1;
151 rect.p0.y = 2;
152 rect.p1.x = 3;
153 rect.p1.y = 4;
154
155 ui_label_set_rect(label, &rect);
156 PCUT_ASSERT_INT_EQUALS(rect.p0.x, label->rect.p0.x);
157 PCUT_ASSERT_INT_EQUALS(rect.p0.y, label->rect.p0.y);
158 PCUT_ASSERT_INT_EQUALS(rect.p1.x, label->rect.p1.x);
159 PCUT_ASSERT_INT_EQUALS(rect.p1.y, label->rect.p1.y);
160
161 ui_label_destroy(label);
162}
163
164/** Paint button */
165PCUT_TEST(paint)
166{
167 errno_t rc;
168 gfx_context_t *gc = NULL;
169 test_gc_t tgc;
170 ui_resource_t *resource = NULL;
171 ui_label_t *label;
172
173 memset(&tgc, 0, sizeof(tgc));
174 rc = gfx_context_new(&ops, &tgc, &gc);
175 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
176
177 rc = ui_resource_create(gc, &resource);
178 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
179 PCUT_ASSERT_NOT_NULL(resource);
180
181 rc = ui_label_create(resource, "Hello", &label);
182 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
183
184 rc = ui_label_paint(label);
185 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
186
187 ui_label_destroy(label);
188 ui_resource_destroy(resource);
189
190 rc = gfx_context_delete(gc);
191 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
192}
193
194static errno_t testgc_set_color(void *arg, gfx_color_t *color)
195{
196 (void) arg;
197 (void) color;
198 return EOK;
199}
200
201static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
202{
203 (void) arg;
204 (void) rect;
205 return EOK;
206}
207
208static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
209 gfx_bitmap_alloc_t *alloc, void **rbm)
210{
211 test_gc_t *tgc = (test_gc_t *) arg;
212 testgc_bitmap_t *tbm;
213
214 tbm = calloc(1, sizeof(testgc_bitmap_t));
215 if (tbm == NULL)
216 return ENOMEM;
217
218 if (alloc == NULL) {
219 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
220 sizeof(uint32_t);
221 tbm->alloc.off0 = 0;
222 tbm->alloc.pixels = calloc(sizeof(uint32_t),
223 (params->rect.p1.x - params->rect.p0.x) *
224 (params->rect.p1.y - params->rect.p0.y));
225 tbm->myalloc = true;
226 if (tbm->alloc.pixels == NULL) {
227 free(tbm);
228 return ENOMEM;
229 }
230 } else {
231 tbm->alloc = *alloc;
232 }
233
234 tbm->tgc = tgc;
235 tgc->bm_created = true;
236 tgc->bm_params = *params;
237 tgc->bm_pixels = tbm->alloc.pixels;
238 *rbm = (void *)tbm;
239 return EOK;
240}
241
242static errno_t testgc_bitmap_destroy(void *bm)
243{
244 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
245 if (tbm->myalloc)
246 free(tbm->alloc.pixels);
247 tbm->tgc->bm_destroyed = true;
248 free(tbm);
249 return EOK;
250}
251
252static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
253 gfx_coord2_t *offs)
254{
255 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
256 tbm->tgc->bm_rendered = true;
257 tbm->tgc->bm_srect = *srect;
258 tbm->tgc->bm_offs = *offs;
259 return EOK;
260}
261
262static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
263{
264 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
265 *alloc = tbm->alloc;
266 tbm->tgc->bm_got_alloc = true;
267 return EOK;
268}
269
270PCUT_EXPORT(label);
Note: See TracBrowser for help on using the repository browser.