Editor needs to hide cursor when menu bar is activated
[helenos.git] / uspace / lib / ui / test / image.c
blobf3ff82001b69dda0bb79d28ea1113fdce98bb246
1 /*
2 * Copyright (c) 2021 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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>
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"
41 PCUT_INIT;
43 PCUT_TEST_SUITE(image);
45 /** Create and destroy image */
46 PCUT_TEST(create_destroy)
48 ui_image_t *image = NULL;
49 gfx_rect_t brect;
50 errno_t rc;
52 rc = ui_image_create(NULL, NULL, &brect, &image);
53 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
54 PCUT_ASSERT_NOT_NULL(image);
56 ui_image_destroy(image);
59 /** ui_image_destroy() can take NULL argument (no-op) */
60 PCUT_TEST(destroy_null)
62 ui_image_destroy(NULL);
65 /** ui_image_ctl() returns control that has a working virtual destructor */
66 PCUT_TEST(ctl)
68 ui_image_t *image = NULL;
69 ui_control_t *control;
70 gfx_rect_t brect;
71 errno_t rc;
73 rc = ui_image_create(NULL, NULL, &brect, &image);
74 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
75 PCUT_ASSERT_NOT_NULL(image);
77 control = ui_image_ctl(image);
78 PCUT_ASSERT_NOT_NULL(control);
80 ui_control_destroy(control);
83 /** Set image rectangle sets internal field */
84 PCUT_TEST(set_rect)
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;
94 rc = dummygc_create(&dgc);
95 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
97 gc = dummygc_get_ctx(dgc);
99 rc = ui_resource_create(gc, false, &resource);
100 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
101 PCUT_ASSERT_NOT_NULL(resource);
103 rc = ui_image_create(resource, NULL, &brect, &image);
104 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
105 PCUT_ASSERT_NOT_NULL(image);
107 rect.p0.x = 1;
108 rect.p0.y = 2;
109 rect.p1.x = 3;
110 rect.p1.y = 4;
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);
118 ui_image_destroy(image);
119 ui_resource_destroy(resource);
120 dummygc_destroy(dgc);
123 /** Set image flags sets internal field */
124 PCUT_TEST(set_flags)
126 ui_image_t *image = NULL;
127 gfx_rect_t brect;
128 errno_t rc;
130 rc = ui_image_create(NULL, NULL, &brect, &image);
131 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
132 PCUT_ASSERT_NOT_NULL(image);
134 PCUT_ASSERT_INT_EQUALS(0, image->flags);
136 ui_image_set_flags(image, ui_imgf_frame);
137 PCUT_ASSERT_INT_EQUALS(ui_imgf_frame, image->flags);
139 ui_image_destroy(image);
142 /** Set image bitmap */
143 PCUT_TEST(set_bmp)
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;
155 rc = dummygc_create(&dgc);
156 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
158 gc = dummygc_get_ctx(dgc);
160 rc = ui_resource_create(gc, false, &resource);
161 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
162 PCUT_ASSERT_NOT_NULL(resource);
164 rc = ui_image_create(resource, NULL, &brect, &image);
165 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
166 PCUT_ASSERT_NOT_NULL(image);
168 rect.p0.x = 1;
169 rect.p0.y = 2;
170 rect.p1.x = 3;
171 rect.p1.y = 4;
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);
179 gfx_bitmap_params_init(&params);
180 rc = gfx_bitmap_create(gc, &params, NULL, &bitmap);
181 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
183 ui_image_set_bmp(image, bitmap, &brect);
184 PCUT_ASSERT_EQUALS(bitmap, image->bitmap);
186 rc = ui_image_paint(image);
187 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
189 ui_image_destroy(image);
190 ui_resource_destroy(resource);
191 dummygc_destroy(dgc);
194 /** Paint image */
195 PCUT_TEST(paint)
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;
206 rc = dummygc_create(&dgc);
207 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
209 gc = dummygc_get_ctx(dgc);
211 rc = ui_resource_create(gc, false, &resource);
212 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
213 PCUT_ASSERT_NOT_NULL(resource);
215 gfx_bitmap_params_init(&params);
216 rc = gfx_bitmap_create(gc, &params, NULL, &bitmap);
217 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
219 rc = ui_image_create(resource, bitmap, &brect, &image);
220 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
221 PCUT_ASSERT_NOT_NULL(image);
223 rc = ui_image_paint(image);
224 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
226 /* Check that we can paint image after setting bitmap to NULL */
228 ui_image_set_bmp(image, NULL, &brect);
230 rc = ui_image_paint(image);
231 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
233 ui_image_destroy(image);
234 ui_resource_destroy(resource);
235 dummygc_destroy(dgc);
238 PCUT_EXPORT(image);