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

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

Add GC operation to set clipping rectangle

The number of changed files is due to the proliferation of GC
implementations, mostly these are just dummies in unit tests.
Definitely need to tame those in the future.

  • Property mode set to 100644
File size: 7.5 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_clip_rect(void *, gfx_rect_t *);
44static errno_t testgc_set_color(void *, gfx_color_t *);
45static errno_t testgc_fill_rect(void *, gfx_rect_t *);
46static errno_t testgc_update(void *);
47static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
48 gfx_bitmap_alloc_t *, void **);
49static errno_t testgc_bitmap_destroy(void *);
50static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
51static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
52
53static 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
62};
63
64typedef 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;
74
75typedef struct {
76 test_gc_t *tgc;
77 gfx_bitmap_alloc_t alloc;
78 bool myalloc;
79} testgc_bitmap_t;
80
81typedef struct {
82 bool clicked;
83} test_cb_resp_t;
84
85/** Create and destroy text entry */
86PCUT_TEST(create_destroy)
87{
88 ui_entry_t *entry = NULL;
89 errno_t rc;
90
91 rc = ui_entry_create(NULL, "Hello", &entry);
92 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
93 PCUT_ASSERT_NOT_NULL(entry);
94
95 ui_entry_destroy(entry);
96}
97
98/** ui_entry_destroy() can take NULL argument (no-op) */
99PCUT_TEST(destroy_null)
100{
101 ui_entry_destroy(NULL);
102}
103
104/** ui_entry_ctl() returns control that has a working virtual destructor */
105PCUT_TEST(ctl)
106{
107 ui_entry_t *entry;
108 ui_control_t *control;
109 errno_t rc;
110
111 rc = ui_entry_create(NULL, "Hello", &entry);
112 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
113
114 control = ui_entry_ctl(entry);
115 PCUT_ASSERT_NOT_NULL(control);
116
117 ui_control_destroy(control);
118}
119
120/** Set text entry rectangle sets internal field */
121PCUT_TEST(set_rect)
122{
123 ui_entry_t *entry;
124 gfx_rect_t rect;
125 errno_t rc;
126
127 rc = ui_entry_create(NULL, "Hello", &entry);
128 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
129
130 rect.p0.x = 1;
131 rect.p0.y = 2;
132 rect.p1.x = 3;
133 rect.p1.y = 4;
134
135 ui_entry_set_rect(entry, &rect);
136 PCUT_ASSERT_INT_EQUALS(rect.p0.x, entry->rect.p0.x);
137 PCUT_ASSERT_INT_EQUALS(rect.p0.y, entry->rect.p0.y);
138 PCUT_ASSERT_INT_EQUALS(rect.p1.x, entry->rect.p1.x);
139 PCUT_ASSERT_INT_EQUALS(rect.p1.y, entry->rect.p1.y);
140
141 ui_entry_destroy(entry);
142}
143
144/** Set entry text horizontal alignment sets internal field */
145PCUT_TEST(set_halign)
146{
147 ui_entry_t *entry;
148 errno_t rc;
149
150 rc = ui_entry_create(NULL, "Hello", &entry);
151 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
152
153 ui_entry_set_halign(entry, gfx_halign_left);
154 PCUT_ASSERT_EQUALS(gfx_halign_left, entry->halign);
155 ui_entry_set_halign(entry, gfx_halign_center);
156 PCUT_ASSERT_EQUALS(gfx_halign_center, entry->halign);
157
158 ui_entry_destroy(entry);
159}
160
161/** Set text entry rectangle sets internal field */
162PCUT_TEST(set_text)
163{
164 ui_entry_t *entry;
165 gfx_rect_t rect;
166 errno_t rc;
167
168 rc = ui_entry_create(NULL, "Hello", &entry);
169 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
170
171 rect.p0.x = 1;
172 rect.p0.y = 2;
173 rect.p1.x = 3;
174 rect.p1.y = 4;
175
176 ui_entry_set_rect(entry, &rect);
177 PCUT_ASSERT_INT_EQUALS(rect.p0.x, entry->rect.p0.x);
178 PCUT_ASSERT_INT_EQUALS(rect.p0.y, entry->rect.p0.y);
179 PCUT_ASSERT_INT_EQUALS(rect.p1.x, entry->rect.p1.x);
180 PCUT_ASSERT_INT_EQUALS(rect.p1.y, entry->rect.p1.y);
181
182 ui_entry_destroy(entry);
183}
184
185/** Paint text entry */
186PCUT_TEST(paint)
187{
188 errno_t rc;
189 gfx_context_t *gc = NULL;
190 test_gc_t tgc;
191 ui_resource_t *resource = NULL;
192 ui_entry_t *entry;
193
194 memset(&tgc, 0, sizeof(tgc));
195 rc = gfx_context_new(&ops, &tgc, &gc);
196 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
197
198 rc = ui_resource_create(gc, false, &resource);
199 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
200 PCUT_ASSERT_NOT_NULL(resource);
201
202 rc = ui_entry_create(resource, "Hello", &entry);
203 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
204
205 rc = ui_entry_paint(entry);
206 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
207
208 ui_entry_destroy(entry);
209 ui_resource_destroy(resource);
210
211 rc = gfx_context_delete(gc);
212 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
213}
214
215static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
216{
217 (void) arg;
218 (void) rect;
219 return EOK;
220}
221
222static errno_t testgc_set_color(void *arg, gfx_color_t *color)
223{
224 (void) arg;
225 (void) color;
226 return EOK;
227}
228
229static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
230{
231 (void) arg;
232 (void) rect;
233 return EOK;
234}
235
236static errno_t testgc_update(void *arg)
237{
238 (void) arg;
239 return EOK;
240}
241
242static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
243 gfx_bitmap_alloc_t *alloc, void **rbm)
244{
245 test_gc_t *tgc = (test_gc_t *) arg;
246 testgc_bitmap_t *tbm;
247
248 tbm = calloc(1, sizeof(testgc_bitmap_t));
249 if (tbm == NULL)
250 return ENOMEM;
251
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;
263 }
264 } else {
265 tbm->alloc = *alloc;
266 }
267
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;
274}
275
276static errno_t testgc_bitmap_destroy(void *bm)
277{
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;
284}
285
286static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
287 gfx_coord2_t *offs)
288{
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;
294}
295
296static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
297{
298 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
299 *alloc = tbm->alloc;
300 tbm->tgc->bm_got_alloc = true;
301 return EOK;
302}
303
304PCUT_EXPORT(entry);
Note: See TracBrowser for help on using the repository browser.