source: mainline/uspace/lib/ui/test/image.c

Last change on this file was 95a9cbc, checked in by jxsvoboda <5887334+jxsvoboda@…>, 4 years ago

UI menu unit tests

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