source: mainline/uspace/lib/ui/test/image.c@ 1746ede

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

Fix libui unit tests

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[f93e4e3]1/*
[f0ccb2ab]2 * Copyright (c) 2021 Jiri Svoboda
[f93e4e3]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/image.h>
[f0ccb2ab]36#include <ui/resource.h>
[f93e4e3]37#include <ui/ui.h>
38#include "../private/dummygc.h"
39#include "../private/image.h"
40
41PCUT_INIT;
42
43PCUT_TEST_SUITE(image);
44
45typedef struct {
46 bool clicked;
47} test_cb_resp_t;
48
49/** Create and destroy image */
50PCUT_TEST(create_destroy)
51{
52 ui_image_t *image = NULL;
53 gfx_rect_t brect;
54 errno_t rc;
55
56 rc = ui_image_create(NULL, NULL, &brect, &image);
57 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
58 PCUT_ASSERT_NOT_NULL(image);
59
60 ui_image_destroy(image);
61}
62
63/** ui_image_destroy() can take NULL argument (no-op) */
64PCUT_TEST(destroy_null)
65{
66 ui_image_destroy(NULL);
67}
68
69/** ui_image_ctl() returns control that has a working virtual destructor */
70PCUT_TEST(ctl)
71{
72 ui_image_t *image = NULL;
73 ui_control_t *control;
74 gfx_rect_t brect;
75 errno_t rc;
76
77 rc = ui_image_create(NULL, NULL, &brect, &image);
78 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
79 PCUT_ASSERT_NOT_NULL(image);
80
81 control = ui_image_ctl(image);
82 PCUT_ASSERT_NOT_NULL(control);
83
84 ui_control_destroy(control);
85}
86
87/** Set image rectangle sets internal field */
88PCUT_TEST(set_rect)
89{
[f0ccb2ab]90 errno_t rc;
91 dummy_gc_t *dgc;
92 gfx_context_t *gc;
93 ui_resource_t *resource = NULL;
[12008adf]94 ui_image_t *image = NULL;
95 gfx_rect_t brect;
96 gfx_rect_t rect;
97
[f0ccb2ab]98 rc = dummygc_create(&dgc);
99 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
100
101 gc = dummygc_get_ctx(dgc);
102
103 rc = ui_resource_create(gc, false, &resource);
104 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
105 PCUT_ASSERT_NOT_NULL(resource);
106
107 rc = ui_image_create(resource, NULL, &brect, &image);
[12008adf]108 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
109 PCUT_ASSERT_NOT_NULL(image);
[f93e4e3]110
[12008adf]111 rect.p0.x = 1;
112 rect.p0.y = 2;
113 rect.p1.x = 3;
114 rect.p1.y = 4;
115
116 ui_image_set_rect(image, &rect);
117 PCUT_ASSERT_INT_EQUALS(rect.p0.x, image->rect.p0.x);
118 PCUT_ASSERT_INT_EQUALS(rect.p0.y, image->rect.p0.y);
119 PCUT_ASSERT_INT_EQUALS(rect.p1.x, image->rect.p1.x);
120 PCUT_ASSERT_INT_EQUALS(rect.p1.y, image->rect.p1.y);
121
122 ui_image_destroy(image);
[f0ccb2ab]123 ui_resource_destroy(resource);
124 dummygc_destroy(dgc);
[12008adf]125}
126
[d8ddf7a]127/** Set image flags sets internal field */
128PCUT_TEST(set_flags)
129{
130 ui_image_t *image = NULL;
131 gfx_rect_t brect;
132 errno_t rc;
133
134 rc = ui_image_create(NULL, NULL, &brect, &image);
135 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
136 PCUT_ASSERT_NOT_NULL(image);
137
138 PCUT_ASSERT_INT_EQUALS(0, image->flags);
139
140 ui_image_set_flags(image, ui_imgf_frame);
141 PCUT_ASSERT_INT_EQUALS(ui_imgf_frame, image->flags);
142
143 ui_image_destroy(image);
144}
145
[12008adf]146/** Set image bitmap */
147PCUT_TEST(set_bmp)
148{
[f93e4e3]149 ui_image_t *image = NULL;
150 gfx_rect_t brect;
151 gfx_rect_t rect;
[12008adf]152 gfx_bitmap_t *bitmap;
153 gfx_bitmap_params_t params;
154 dummy_gc_t *dgc;
155 gfx_context_t *gc;
[f0ccb2ab]156 ui_resource_t *resource = NULL;
[f93e4e3]157 errno_t rc;
158
[12008adf]159 rc = dummygc_create(&dgc);
160 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
161
162 gc = dummygc_get_ctx(dgc);
163
[f0ccb2ab]164 rc = ui_resource_create(gc, false, &resource);
165 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
166 PCUT_ASSERT_NOT_NULL(resource);
167
168 rc = ui_image_create(resource, NULL, &brect, &image);
[f93e4e3]169 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
170 PCUT_ASSERT_NOT_NULL(image);
171
172 rect.p0.x = 1;
173 rect.p0.y = 2;
174 rect.p1.x = 3;
175 rect.p1.y = 4;
176
177 ui_image_set_rect(image, &rect);
178 PCUT_ASSERT_INT_EQUALS(rect.p0.x, image->rect.p0.x);
179 PCUT_ASSERT_INT_EQUALS(rect.p0.y, image->rect.p0.y);
180 PCUT_ASSERT_INT_EQUALS(rect.p1.x, image->rect.p1.x);
181 PCUT_ASSERT_INT_EQUALS(rect.p1.y, image->rect.p1.y);
182
[12008adf]183 gfx_bitmap_params_init(&params);
184 rc = gfx_bitmap_create(gc, &params, NULL, &bitmap);
185 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
186
187 ui_image_set_bmp(image, bitmap, &brect);
188 PCUT_ASSERT_EQUALS(bitmap, image->bitmap);
189
190 rc = ui_image_paint(image);
191 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
192
[f93e4e3]193 ui_image_destroy(image);
[f0ccb2ab]194 ui_resource_destroy(resource);
195 dummygc_destroy(dgc);
[f93e4e3]196}
197
198/** Paint image */
199PCUT_TEST(paint)
200{
201 dummy_gc_t *dgc;
202 gfx_context_t *gc;
203 gfx_bitmap_params_t params;
204 gfx_bitmap_t *bitmap;
[f0ccb2ab]205 ui_resource_t *resource = NULL;
[f93e4e3]206 ui_image_t *image = NULL;
207 gfx_rect_t brect;
208 errno_t rc;
209
210 rc = dummygc_create(&dgc);
211 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
212
213 gc = dummygc_get_ctx(dgc);
214
[f0ccb2ab]215 rc = ui_resource_create(gc, false, &resource);
216 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
217 PCUT_ASSERT_NOT_NULL(resource);
218
[f93e4e3]219 gfx_bitmap_params_init(&params);
220 rc = gfx_bitmap_create(gc, &params, NULL, &bitmap);
221 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
222
[f0ccb2ab]223 rc = ui_image_create(resource, bitmap, &brect, &image);
[f93e4e3]224 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
225 PCUT_ASSERT_NOT_NULL(image);
226
227 rc = ui_image_paint(image);
228 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
229
[12008adf]230 /* Check that we can paint image after setting bitmap to NULL */
231
232 ui_image_set_bmp(image, NULL, &brect);
233
234 rc = ui_image_paint(image);
235 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
236
[f93e4e3]237 ui_image_destroy(image);
[f0ccb2ab]238 ui_resource_destroy(resource);
239 dummygc_destroy(dgc);
[f93e4e3]240}
241
242PCUT_EXPORT(image);
Note: See TracBrowser for help on using the repository browser.