2 * Copyright (c) 2021 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include <gfx/context.h>
30 #include <gfx/coord.h>
32 #include <pcut/pcut.h>
34 #include <ui/control.h>
36 #include <ui/resource.h>
37 #include "../private/label.h"
41 PCUT_TEST_SUITE(label
);
43 static errno_t
testgc_set_clip_rect(void *, gfx_rect_t
*);
44 static errno_t
testgc_set_color(void *, gfx_color_t
*);
45 static errno_t
testgc_fill_rect(void *, gfx_rect_t
*);
46 static errno_t
testgc_update(void *);
47 static errno_t
testgc_bitmap_create(void *, gfx_bitmap_params_t
*,
48 gfx_bitmap_alloc_t
*, void **);
49 static errno_t
testgc_bitmap_destroy(void *);
50 static errno_t
testgc_bitmap_render(void *, gfx_rect_t
*, gfx_coord2_t
*);
51 static errno_t
testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t
*);
53 static 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
67 gfx_bitmap_params_t bm_params
;
77 gfx_bitmap_alloc_t alloc
;
85 /** Create and destroy label */
86 PCUT_TEST(create_destroy
)
88 ui_label_t
*label
= NULL
;
91 rc
= ui_label_create(NULL
, "Hello", &label
);
92 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
93 PCUT_ASSERT_NOT_NULL(label
);
95 ui_label_destroy(label
);
98 /** ui_label_destroy() can take NULL argument (no-op) */
99 PCUT_TEST(destroy_null
)
101 ui_label_destroy(NULL
);
104 /** ui_label_ctl() returns control that has a working virtual destructor */
108 ui_control_t
*control
;
111 rc
= ui_label_create(NULL
, "Hello", &label
);
112 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
114 control
= ui_label_ctl(label
);
115 PCUT_ASSERT_NOT_NULL(control
);
117 ui_control_destroy(control
);
120 /** Set label rectangle sets internal field */
127 rc
= ui_label_create(NULL
, "Hello", &label
);
128 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
135 ui_label_set_rect(label
, &rect
);
136 PCUT_ASSERT_INT_EQUALS(rect
.p0
.x
, label
->rect
.p0
.x
);
137 PCUT_ASSERT_INT_EQUALS(rect
.p0
.y
, label
->rect
.p0
.y
);
138 PCUT_ASSERT_INT_EQUALS(rect
.p1
.x
, label
->rect
.p1
.x
);
139 PCUT_ASSERT_INT_EQUALS(rect
.p1
.y
, label
->rect
.p1
.y
);
141 ui_label_destroy(label
);
144 /** Set label text horizontal alignment sets internal field */
145 PCUT_TEST(set_halign
)
150 rc
= ui_label_create(NULL
, "Hello", &label
);
151 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
153 ui_label_set_halign(label
, gfx_halign_left
);
154 PCUT_ASSERT_EQUALS(gfx_halign_left
, label
->halign
);
155 ui_label_set_halign(label
, gfx_halign_center
);
156 PCUT_ASSERT_EQUALS(gfx_halign_center
, label
->halign
);
158 ui_label_destroy(label
);
161 /** Set label rectangle sets internal field */
168 rc
= ui_label_create(NULL
, "Hello", &label
);
169 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
176 ui_label_set_rect(label
, &rect
);
177 PCUT_ASSERT_INT_EQUALS(rect
.p0
.x
, label
->rect
.p0
.x
);
178 PCUT_ASSERT_INT_EQUALS(rect
.p0
.y
, label
->rect
.p0
.y
);
179 PCUT_ASSERT_INT_EQUALS(rect
.p1
.x
, label
->rect
.p1
.x
);
180 PCUT_ASSERT_INT_EQUALS(rect
.p1
.y
, label
->rect
.p1
.y
);
182 ui_label_destroy(label
);
189 gfx_context_t
*gc
= NULL
;
191 ui_resource_t
*resource
= NULL
;
194 memset(&tgc
, 0, sizeof(tgc
));
195 rc
= gfx_context_new(&ops
, &tgc
, &gc
);
196 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
198 rc
= ui_resource_create(gc
, false, &resource
);
199 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
200 PCUT_ASSERT_NOT_NULL(resource
);
202 rc
= ui_label_create(resource
, "Hello", &label
);
203 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
205 rc
= ui_label_paint(label
);
206 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
208 ui_label_destroy(label
);
209 ui_resource_destroy(resource
);
211 rc
= gfx_context_delete(gc
);
212 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
215 static errno_t
testgc_set_clip_rect(void *arg
, gfx_rect_t
*rect
)
222 static errno_t
testgc_set_color(void *arg
, gfx_color_t
*color
)
229 static errno_t
testgc_fill_rect(void *arg
, gfx_rect_t
*rect
)
236 static errno_t
testgc_update(void *arg
)
242 static errno_t
testgc_bitmap_create(void *arg
, gfx_bitmap_params_t
*params
,
243 gfx_bitmap_alloc_t
*alloc
, void **rbm
)
245 test_gc_t
*tgc
= (test_gc_t
*) arg
;
246 testgc_bitmap_t
*tbm
;
248 tbm
= calloc(1, sizeof(testgc_bitmap_t
));
253 tbm
->alloc
.pitch
= (params
->rect
.p1
.x
- params
->rect
.p0
.x
) *
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
));
260 if (tbm
->alloc
.pixels
== NULL
) {
269 tgc
->bm_created
= true;
270 tgc
->bm_params
= *params
;
271 tgc
->bm_pixels
= tbm
->alloc
.pixels
;
276 static errno_t
testgc_bitmap_destroy(void *bm
)
278 testgc_bitmap_t
*tbm
= (testgc_bitmap_t
*)bm
;
280 free(tbm
->alloc
.pixels
);
281 tbm
->tgc
->bm_destroyed
= true;
286 static errno_t
testgc_bitmap_render(void *bm
, gfx_rect_t
*srect
,
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
;
296 static errno_t
testgc_bitmap_get_alloc(void *bm
, gfx_bitmap_alloc_t
*alloc
)
298 testgc_bitmap_t
*tbm
= (testgc_bitmap_t
*)bm
;
300 tbm
->tgc
->bm_got_alloc
= true;