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

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

UI label

  • Property mode set to 100644
File size: 6.3 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>
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 rectangle sets internal field */
124PCUT_TEST(set_text)
125{
126 ui_label_t *label;
127 gfx_rect_t rect;
128 errno_t rc;
129
130 rc = ui_label_create(NULL, "Hello", &label);
131 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
132
133 rect.p0.x = 1;
134 rect.p0.y = 2;
135 rect.p1.x = 3;
136 rect.p1.y = 4;
137
138 ui_label_set_rect(label, &rect);
139 PCUT_ASSERT_INT_EQUALS(rect.p0.x, label->rect.p0.x);
140 PCUT_ASSERT_INT_EQUALS(rect.p0.y, label->rect.p0.y);
141 PCUT_ASSERT_INT_EQUALS(rect.p1.x, label->rect.p1.x);
142 PCUT_ASSERT_INT_EQUALS(rect.p1.y, label->rect.p1.y);
143
144 ui_label_destroy(label);
145}
146
147/** Paint button */
148PCUT_TEST(paint)
149{
150 errno_t rc;
151 gfx_context_t *gc = NULL;
152 test_gc_t tgc;
153 ui_resource_t *resource = NULL;
154 ui_label_t *label;
155
156 memset(&tgc, 0, sizeof(tgc));
157 rc = gfx_context_new(&ops, &tgc, &gc);
158 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
159
160 rc = ui_resource_create(gc, &resource);
161 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
162 PCUT_ASSERT_NOT_NULL(resource);
163
164 rc = ui_label_create(resource, "Hello", &label);
165 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
166
167 rc = ui_label_paint(label);
168 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
169
170 ui_label_destroy(label);
171 ui_resource_destroy(resource);
172
173 rc = gfx_context_delete(gc);
174 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
175}
176
177static errno_t testgc_set_color(void *arg, gfx_color_t *color)
178{
179 (void) arg;
180 (void) color;
181 return EOK;
182}
183
184static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
185{
186 (void) arg;
187 (void) rect;
188 return EOK;
189}
190
191static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
192 gfx_bitmap_alloc_t *alloc, void **rbm)
193{
194 test_gc_t *tgc = (test_gc_t *) arg;
195 testgc_bitmap_t *tbm;
196
197 tbm = calloc(1, sizeof(testgc_bitmap_t));
198 if (tbm == NULL)
199 return ENOMEM;
200
201 if (alloc == NULL) {
202 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
203 sizeof(uint32_t);
204 tbm->alloc.off0 = 0;
205 tbm->alloc.pixels = calloc(sizeof(uint32_t),
206 (params->rect.p1.x - params->rect.p0.x) *
207 (params->rect.p1.y - params->rect.p0.y));
208 tbm->myalloc = true;
209 if (tbm->alloc.pixels == NULL) {
210 free(tbm);
211 return ENOMEM;
212 }
213 } else {
214 tbm->alloc = *alloc;
215 }
216
217 tbm->tgc = tgc;
218 tgc->bm_created = true;
219 tgc->bm_params = *params;
220 tgc->bm_pixels = tbm->alloc.pixels;
221 *rbm = (void *)tbm;
222 return EOK;
223}
224
225static errno_t testgc_bitmap_destroy(void *bm)
226{
227 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
228 if (tbm->myalloc)
229 free(tbm->alloc.pixels);
230 tbm->tgc->bm_destroyed = true;
231 free(tbm);
232 return EOK;
233}
234
235static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
236 gfx_coord2_t *offs)
237{
238 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
239 tbm->tgc->bm_rendered = true;
240 tbm->tgc->bm_srect = *srect;
241 tbm->tgc->bm_offs = *offs;
242 return EOK;
243}
244
245static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
246{
247 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
248 *alloc = tbm->alloc;
249 tbm->tgc->bm_got_alloc = true;
250 return EOK;
251}
252
253PCUT_EXPORT(label);
Note: See TracBrowser for help on using the repository browser.