Changeset 4ed00d3 in mainline
- Timestamp:
- 2020-10-15T09:16:24Z (4 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- faca61b8
- Parents:
- c9a7adc
- git-author:
- Jiri Svoboda <jiri@…> (2020-10-14 18:15:54)
- git-committer:
- Jiri Svoboda <jiri@…> (2020-10-15 09:16:24)
- Location:
- uspace/lib/ui
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ui/src/resource.c
rc9a7adc r4ed00d3 93 93 /** Destroy UI resource. 94 94 * 95 * @param resource Push buttonor @c NULL95 * @param resource UI resource or @c NULL 96 96 */ 97 97 void ui_resource_destroy(ui_resource_t *resource) -
uspace/lib/ui/test/pbutton.c
rc9a7adc r4ed00d3 27 27 */ 28 28 29 #include <errno.h> 29 #include <gfx/context.h> 30 #include <gfx/coord.h> 31 #include <mem.h> 30 32 #include <pcut/pcut.h> 33 #include <stdbool.h> 31 34 #include <ui/pbutton.h> 35 #include <ui/resource.h> 36 #include "../private/pbutton.h" 32 37 33 38 PCUT_INIT; … … 35 40 PCUT_TEST_SUITE(pbutton); 36 41 42 static errno_t testgc_set_color(void *, gfx_color_t *); 43 static errno_t testgc_fill_rect(void *, gfx_rect_t *); 44 static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *, 45 gfx_bitmap_alloc_t *, void **); 46 static errno_t testgc_bitmap_destroy(void *); 47 static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *); 48 static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *); 49 50 static gfx_context_ops_t ops = { 51 .set_color = testgc_set_color, 52 .fill_rect = testgc_fill_rect, 53 .bitmap_create = testgc_bitmap_create, 54 .bitmap_destroy = testgc_bitmap_destroy, 55 .bitmap_render = testgc_bitmap_render, 56 .bitmap_get_alloc = testgc_bitmap_get_alloc 57 }; 58 59 typedef struct { 60 bool bm_created; 61 bool bm_destroyed; 62 gfx_bitmap_params_t bm_params; 63 void *bm_pixels; 64 gfx_rect_t bm_srect; 65 gfx_coord2_t bm_offs; 66 bool bm_rendered; 67 bool bm_got_alloc; 68 } test_gc_t; 69 70 typedef struct { 71 test_gc_t *tgc; 72 gfx_bitmap_alloc_t alloc; 73 bool myalloc; 74 } testgc_bitmap_t; 75 76 /** Create and destroy button */ 37 77 PCUT_TEST(create_destroy) 38 78 { 39 ui_pbutton_t *pbutton; 40 errno_t rc; 41 42 rc = ui_pbutton_create(NULL, "Hello", &pbutton); 43 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 44 45 ui_pbutton_destroy(pbutton); 79 ui_pbutton_t *pbutton = NULL; 80 errno_t rc; 81 82 rc = ui_pbutton_create(NULL, "Hello", &pbutton); 83 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 84 PCUT_ASSERT_NOT_NULL(pbutton); 85 86 ui_pbutton_destroy(pbutton); 87 } 88 89 /** ui_pbutton_destroy() can take NULL argument (no-op) */ 90 PCUT_TEST(destroy_null) 91 { 92 ui_pbutton_destroy(NULL); 93 } 94 95 /** Set button rectangle sets internal field */ 96 PCUT_TEST(set_rect) 97 { 98 ui_pbutton_t *pbutton; 99 gfx_rect_t rect; 100 errno_t rc; 101 102 rc = ui_pbutton_create(NULL, "Hello", &pbutton); 103 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 104 105 rect.p0.x = 1; 106 rect.p0.y = 2; 107 rect.p1.x = 3; 108 rect.p1.y = 4; 109 110 ui_pbutton_set_rect(pbutton, &rect); 111 PCUT_ASSERT_INT_EQUALS(rect.p0.x, pbutton->rect.p0.x); 112 PCUT_ASSERT_INT_EQUALS(rect.p0.y, pbutton->rect.p0.y); 113 PCUT_ASSERT_INT_EQUALS(rect.p1.x, pbutton->rect.p1.x); 114 PCUT_ASSERT_INT_EQUALS(rect.p1.y, pbutton->rect.p1.y); 115 116 ui_pbutton_destroy(pbutton); 117 } 118 119 /** Set default flag sets internal field */ 120 PCUT_TEST(set_default) 121 { 122 ui_pbutton_t *pbutton; 123 errno_t rc; 124 125 rc = ui_pbutton_create(NULL, "Hello", &pbutton); 126 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 127 128 ui_pbutton_set_default(pbutton, true); 129 PCUT_ASSERT_TRUE(pbutton->isdefault); 130 131 ui_pbutton_set_default(pbutton, false); 132 PCUT_ASSERT_FALSE(pbutton->isdefault); 133 134 ui_pbutton_destroy(pbutton); 135 } 136 137 /** Paint button */ 138 PCUT_TEST(paint) 139 { 140 errno_t rc; 141 gfx_context_t *gc = NULL; 142 test_gc_t tgc; 143 ui_resource_t *resource = NULL; 144 ui_pbutton_t *pbutton; 145 146 memset(&tgc, 0, sizeof(tgc)); 147 rc = gfx_context_new(&ops, &tgc, &gc); 148 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 149 150 rc = ui_resource_create(gc, &resource); 151 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 152 PCUT_ASSERT_NOT_NULL(resource); 153 154 rc = ui_pbutton_create(resource, "Hello", &pbutton); 155 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 156 157 rc = ui_pbutton_paint(pbutton); 158 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 159 160 ui_pbutton_destroy(pbutton); 161 ui_resource_destroy(resource); 162 163 rc = gfx_context_delete(gc); 164 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 165 } 166 167 /** ui_pbutton_press()/release() sets/clears internal held flag */ 168 PCUT_TEST(press_release) 169 { 170 ui_pbutton_t *pbutton; 171 errno_t rc; 172 173 rc = ui_pbutton_create(NULL, "Hello", &pbutton); 174 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 175 176 PCUT_ASSERT_FALSE(pbutton->held); 177 178 ui_pbutton_press(pbutton); 179 PCUT_ASSERT_TRUE(pbutton->held); 180 181 ui_pbutton_release(pbutton); 182 PCUT_ASSERT_FALSE(pbutton->held); 183 184 ui_pbutton_destroy(pbutton); 185 } 186 187 static errno_t testgc_set_color(void *arg, gfx_color_t *color) 188 { 189 (void) arg; 190 (void) color; 191 return EOK; 192 } 193 194 static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect) 195 { 196 (void) arg; 197 (void) rect; 198 return EOK; 199 } 200 201 static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params, 202 gfx_bitmap_alloc_t *alloc, void **rbm) 203 { 204 test_gc_t *tgc = (test_gc_t *) arg; 205 testgc_bitmap_t *tbm; 206 207 tbm = calloc(1, sizeof(testgc_bitmap_t)); 208 if (tbm == NULL) 209 return ENOMEM; 210 211 if (alloc == NULL) { 212 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) * 213 sizeof(uint32_t); 214 tbm->alloc.off0 = 0; 215 tbm->alloc.pixels = calloc(sizeof(uint32_t), 216 (params->rect.p1.x - params->rect.p0.x) * 217 (params->rect.p1.y - params->rect.p0.y)); 218 tbm->myalloc = true; 219 if (tbm->alloc.pixels == NULL) { 220 free(tbm); 221 return ENOMEM; 222 } 223 } else { 224 tbm->alloc = *alloc; 225 } 226 227 tbm->tgc = tgc; 228 tgc->bm_created = true; 229 tgc->bm_params = *params; 230 tgc->bm_pixels = tbm->alloc.pixels; 231 *rbm = (void *)tbm; 232 return EOK; 233 } 234 235 static errno_t testgc_bitmap_destroy(void *bm) 236 { 237 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm; 238 if (tbm->myalloc) 239 free(tbm->alloc.pixels); 240 tbm->tgc->bm_destroyed = true; 241 free(tbm); 242 return EOK; 243 } 244 245 static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect, 246 gfx_coord2_t *offs) 247 { 248 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm; 249 tbm->tgc->bm_rendered = true; 250 tbm->tgc->bm_srect = *srect; 251 tbm->tgc->bm_offs = *offs; 252 return EOK; 253 } 254 255 static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc) 256 { 257 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm; 258 *alloc = tbm->alloc; 259 tbm->tgc->bm_got_alloc = true; 260 return EOK; 46 261 } 47 262 -
uspace/lib/ui/test/resource.c
rc9a7adc r4ed00d3 27 27 */ 28 28 29 #include <errno.h> 29 #include <gfx/context.h> 30 #include <mem.h> 30 31 #include <pcut/pcut.h> 32 #include <stdbool.h> 31 33 #include <ui/resource.h> 34 #include "../private/resource.h" 32 35 33 36 PCUT_INIT; … … 35 38 PCUT_TEST_SUITE(resource); 36 39 37 PCUT_TEST(dummy) 40 static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *, 41 gfx_bitmap_alloc_t *, void **); 42 static errno_t testgc_bitmap_destroy(void *); 43 static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *); 44 static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *); 45 46 static gfx_context_ops_t ops = { 47 .bitmap_create = testgc_bitmap_create, 48 .bitmap_destroy = testgc_bitmap_destroy, 49 .bitmap_render = testgc_bitmap_render, 50 .bitmap_get_alloc = testgc_bitmap_get_alloc 51 }; 52 53 typedef struct { 54 bool bm_created; 55 bool bm_destroyed; 56 gfx_bitmap_params_t bm_params; 57 void *bm_pixels; 58 gfx_rect_t bm_srect; 59 gfx_coord2_t bm_offs; 60 bool bm_rendered; 61 bool bm_got_alloc; 62 } test_gc_t; 63 64 typedef struct { 65 test_gc_t *tgc; 66 gfx_bitmap_alloc_t alloc; 67 bool myalloc; 68 } testgc_bitmap_t; 69 70 /** Create and destroy UI resource */ 71 PCUT_TEST(create_destroy) 38 72 { 73 errno_t rc; 74 gfx_context_t *gc = NULL; 75 test_gc_t tgc; 76 ui_resource_t *resource = NULL; 77 78 memset(&tgc, 0, sizeof(tgc)); 79 rc = gfx_context_new(&ops, &tgc, &gc); 80 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 81 82 rc = ui_resource_create(gc, &resource); 83 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 84 PCUT_ASSERT_NOT_NULL(resource); 85 86 PCUT_ASSERT_NOT_NULL(resource->tface); 87 PCUT_ASSERT_NOT_NULL(resource->font); 88 89 ui_resource_destroy(resource); 90 91 rc = gfx_context_delete(gc); 92 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 93 } 94 95 /** ui_resource_destroy() can take NULL argument (no-op) */ 96 PCUT_TEST(destroy_null) 97 { 98 ui_resource_destroy(NULL); 99 } 100 101 static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params, 102 gfx_bitmap_alloc_t *alloc, void **rbm) 103 { 104 test_gc_t *tgc = (test_gc_t *) arg; 105 testgc_bitmap_t *tbm; 106 107 tbm = calloc(1, sizeof(testgc_bitmap_t)); 108 if (tbm == NULL) 109 return ENOMEM; 110 111 if (alloc == NULL) { 112 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) * 113 sizeof(uint32_t); 114 tbm->alloc.off0 = 0; 115 tbm->alloc.pixels = calloc(sizeof(uint32_t), 116 (params->rect.p1.x - params->rect.p0.x) * 117 (params->rect.p1.y - params->rect.p0.y)); 118 tbm->myalloc = true; 119 if (tbm->alloc.pixels == NULL) { 120 free(tbm); 121 return ENOMEM; 122 } 123 } else { 124 tbm->alloc = *alloc; 125 } 126 127 tbm->tgc = tgc; 128 tgc->bm_created = true; 129 tgc->bm_params = *params; 130 tgc->bm_pixels = tbm->alloc.pixels; 131 *rbm = (void *)tbm; 132 return EOK; 133 } 134 135 static errno_t testgc_bitmap_destroy(void *bm) 136 { 137 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm; 138 if (tbm->myalloc) 139 free(tbm->alloc.pixels); 140 tbm->tgc->bm_destroyed = true; 141 free(tbm); 142 return EOK; 143 } 144 145 static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect, 146 gfx_coord2_t *offs) 147 { 148 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm; 149 tbm->tgc->bm_rendered = true; 150 tbm->tgc->bm_srect = *srect; 151 tbm->tgc->bm_offs = *offs; 152 return EOK; 153 } 154 155 static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc) 156 { 157 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm; 158 *alloc = tbm->alloc; 159 tbm->tgc->bm_got_alloc = true; 160 return EOK; 39 161 } 40 162
Note:
See TracChangeset
for help on using the changeset viewer.