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

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

Add UI image

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