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 |
|
---|
41 | PCUT_INIT;
|
---|
42 |
|
---|
43 | PCUT_TEST_SUITE(image);
|
---|
44 |
|
---|
45 | typedef struct {
|
---|
46 | bool clicked;
|
---|
47 | } test_cb_resp_t;
|
---|
48 |
|
---|
49 | /** Create and destroy image */
|
---|
50 | PCUT_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) */
|
---|
64 | PCUT_TEST(destroy_null)
|
---|
65 | {
|
---|
66 | ui_image_destroy(NULL);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** ui_image_ctl() returns control that has a working virtual destructor */
|
---|
70 | PCUT_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 */
|
---|
88 | PCUT_TEST(set_rect)
|
---|
89 | {
|
---|
90 | errno_t rc;
|
---|
91 | dummy_gc_t *dgc;
|
---|
92 | gfx_context_t *gc;
|
---|
93 | ui_resource_t *resource = NULL;
|
---|
94 | ui_image_t *image = NULL;
|
---|
95 | gfx_rect_t brect;
|
---|
96 | gfx_rect_t rect;
|
---|
97 |
|
---|
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);
|
---|
108 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
109 | PCUT_ASSERT_NOT_NULL(image);
|
---|
110 |
|
---|
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);
|
---|
123 | ui_resource_destroy(resource);
|
---|
124 | dummygc_destroy(dgc);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** Set image flags sets internal field */
|
---|
128 | PCUT_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 |
|
---|
146 | /** Set image bitmap */
|
---|
147 | PCUT_TEST(set_bmp)
|
---|
148 | {
|
---|
149 | ui_image_t *image = NULL;
|
---|
150 | gfx_rect_t brect;
|
---|
151 | gfx_rect_t rect;
|
---|
152 | gfx_bitmap_t *bitmap;
|
---|
153 | gfx_bitmap_params_t params;
|
---|
154 | dummy_gc_t *dgc;
|
---|
155 | gfx_context_t *gc;
|
---|
156 | ui_resource_t *resource = NULL;
|
---|
157 | errno_t rc;
|
---|
158 |
|
---|
159 | rc = dummygc_create(&dgc);
|
---|
160 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
161 |
|
---|
162 | gc = dummygc_get_ctx(dgc);
|
---|
163 |
|
---|
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);
|
---|
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 |
|
---|
183 | gfx_bitmap_params_init(¶ms);
|
---|
184 | rc = gfx_bitmap_create(gc, ¶ms, 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 |
|
---|
193 | ui_image_destroy(image);
|
---|
194 | ui_resource_destroy(resource);
|
---|
195 | dummygc_destroy(dgc);
|
---|
196 | }
|
---|
197 |
|
---|
198 | /** Paint image */
|
---|
199 | PCUT_TEST(paint)
|
---|
200 | {
|
---|
201 | dummy_gc_t *dgc;
|
---|
202 | gfx_context_t *gc;
|
---|
203 | gfx_bitmap_params_t params;
|
---|
204 | gfx_bitmap_t *bitmap;
|
---|
205 | ui_resource_t *resource = NULL;
|
---|
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 |
|
---|
215 | rc = ui_resource_create(gc, false, &resource);
|
---|
216 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
217 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
218 |
|
---|
219 | gfx_bitmap_params_init(¶ms);
|
---|
220 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
221 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
222 |
|
---|
223 | rc = ui_image_create(resource, bitmap, &brect, &image);
|
---|
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 |
|
---|
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 |
|
---|
237 | ui_image_destroy(image);
|
---|
238 | ui_resource_destroy(resource);
|
---|
239 | dummygc_destroy(dgc);
|
---|
240 | }
|
---|
241 |
|
---|
242 | PCUT_EXPORT(image);
|
---|