source: mainline/uspace/lib/ui/test/entry.c@ 252d03c

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 252d03c was f0ccb2ab, checked in by jxsvoboda <5887334+jxsvoboda@…>, 4 years ago

Fix libui unit tests

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