| 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 <mem.h>
|
|---|
| 31 | #include <pcut/pcut.h>
|
|---|
| 32 | #include <stdbool.h>
|
|---|
| 33 | #include <ui/resource.h>
|
|---|
| 34 | #include "../private/resource.h"
|
|---|
| 35 |
|
|---|
| 36 | PCUT_INIT;
|
|---|
| 37 |
|
|---|
| 38 | PCUT_TEST_SUITE(resource);
|
|---|
| 39 |
|
|---|
| 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 void test_expose(void *);
|
|---|
| 47 |
|
|---|
| 48 | static gfx_context_ops_t ops = {
|
|---|
| 49 | .bitmap_create = testgc_bitmap_create,
|
|---|
| 50 | .bitmap_destroy = testgc_bitmap_destroy,
|
|---|
| 51 | .bitmap_render = testgc_bitmap_render,
|
|---|
| 52 | .bitmap_get_alloc = testgc_bitmap_get_alloc
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | typedef struct {
|
|---|
| 56 | bool bm_created;
|
|---|
| 57 | bool bm_destroyed;
|
|---|
| 58 | gfx_bitmap_params_t bm_params;
|
|---|
| 59 | void *bm_pixels;
|
|---|
| 60 | gfx_rect_t bm_srect;
|
|---|
| 61 | gfx_coord2_t bm_offs;
|
|---|
| 62 | bool bm_rendered;
|
|---|
| 63 | bool bm_got_alloc;
|
|---|
| 64 | } test_gc_t;
|
|---|
| 65 |
|
|---|
| 66 | typedef struct {
|
|---|
| 67 | test_gc_t *tgc;
|
|---|
| 68 | gfx_bitmap_alloc_t alloc;
|
|---|
| 69 | bool myalloc;
|
|---|
| 70 | } testgc_bitmap_t;
|
|---|
| 71 |
|
|---|
| 72 | typedef struct {
|
|---|
| 73 | bool expose;
|
|---|
| 74 | } test_resp_t;
|
|---|
| 75 |
|
|---|
| 76 | /** Create and destroy UI resource */
|
|---|
| 77 | PCUT_TEST(create_destroy)
|
|---|
| 78 | {
|
|---|
| 79 | errno_t rc;
|
|---|
| 80 | gfx_context_t *gc = NULL;
|
|---|
| 81 | test_gc_t tgc;
|
|---|
| 82 | ui_resource_t *resource = NULL;
|
|---|
| 83 |
|
|---|
| 84 | memset(&tgc, 0, sizeof(tgc));
|
|---|
| 85 | rc = gfx_context_new(&ops, &tgc, &gc);
|
|---|
| 86 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 87 |
|
|---|
| 88 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 89 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 90 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 91 |
|
|---|
| 92 | PCUT_ASSERT_NOT_NULL(resource->tface);
|
|---|
| 93 | PCUT_ASSERT_NOT_NULL(resource->font);
|
|---|
| 94 |
|
|---|
| 95 | ui_resource_destroy(resource);
|
|---|
| 96 |
|
|---|
| 97 | rc = gfx_context_delete(gc);
|
|---|
| 98 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /** ui_resource_destroy() can take NULL argument (no-op) */
|
|---|
| 102 | PCUT_TEST(destroy_null)
|
|---|
| 103 | {
|
|---|
| 104 | ui_resource_destroy(NULL);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /** ui_resource_set_expose_cb() / ui_resource_expose() */
|
|---|
| 108 | PCUT_TEST(set_expose_cb_expose)
|
|---|
| 109 | {
|
|---|
| 110 | errno_t rc;
|
|---|
| 111 | gfx_context_t *gc = NULL;
|
|---|
| 112 | test_gc_t tgc;
|
|---|
| 113 | ui_resource_t *resource = NULL;
|
|---|
| 114 | test_resp_t resp;
|
|---|
| 115 |
|
|---|
| 116 | memset(&tgc, 0, sizeof(tgc));
|
|---|
| 117 | rc = gfx_context_new(&ops, &tgc, &gc);
|
|---|
| 118 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 119 |
|
|---|
| 120 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 121 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 122 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 123 |
|
|---|
| 124 | ui_resource_set_expose_cb(resource, test_expose, &resp);
|
|---|
| 125 |
|
|---|
| 126 | resp.expose = false;
|
|---|
| 127 | ui_resource_expose(resource);
|
|---|
| 128 | PCUT_ASSERT_TRUE(resp.expose);
|
|---|
| 129 |
|
|---|
| 130 | ui_resource_destroy(resource);
|
|---|
| 131 |
|
|---|
| 132 | rc = gfx_context_delete(gc);
|
|---|
| 133 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
|---|
| 137 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
|---|
| 138 | {
|
|---|
| 139 | test_gc_t *tgc = (test_gc_t *) arg;
|
|---|
| 140 | testgc_bitmap_t *tbm;
|
|---|
| 141 |
|
|---|
| 142 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
|---|
| 143 | if (tbm == NULL)
|
|---|
| 144 | return ENOMEM;
|
|---|
| 145 |
|
|---|
| 146 | if (alloc == NULL) {
|
|---|
| 147 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
|---|
| 148 | sizeof(uint32_t);
|
|---|
| 149 | tbm->alloc.off0 = 0;
|
|---|
| 150 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
|---|
| 151 | (params->rect.p1.x - params->rect.p0.x) *
|
|---|
| 152 | (params->rect.p1.y - params->rect.p0.y));
|
|---|
| 153 | tbm->myalloc = true;
|
|---|
| 154 | if (tbm->alloc.pixels == NULL) {
|
|---|
| 155 | free(tbm);
|
|---|
| 156 | return ENOMEM;
|
|---|
| 157 | }
|
|---|
| 158 | } else {
|
|---|
| 159 | tbm->alloc = *alloc;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | tbm->tgc = tgc;
|
|---|
| 163 | tgc->bm_created = true;
|
|---|
| 164 | tgc->bm_params = *params;
|
|---|
| 165 | tgc->bm_pixels = tbm->alloc.pixels;
|
|---|
| 166 | *rbm = (void *)tbm;
|
|---|
| 167 | return EOK;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | static errno_t testgc_bitmap_destroy(void *bm)
|
|---|
| 171 | {
|
|---|
| 172 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
|---|
| 173 | if (tbm->myalloc)
|
|---|
| 174 | free(tbm->alloc.pixels);
|
|---|
| 175 | tbm->tgc->bm_destroyed = true;
|
|---|
| 176 | free(tbm);
|
|---|
| 177 | return EOK;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
|---|
| 181 | gfx_coord2_t *offs)
|
|---|
| 182 | {
|
|---|
| 183 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
|---|
| 184 | tbm->tgc->bm_rendered = true;
|
|---|
| 185 | tbm->tgc->bm_srect = *srect;
|
|---|
| 186 | tbm->tgc->bm_offs = *offs;
|
|---|
| 187 | return EOK;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
|---|
| 191 | {
|
|---|
| 192 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
|---|
| 193 | *alloc = tbm->alloc;
|
|---|
| 194 | tbm->tgc->bm_got_alloc = true;
|
|---|
| 195 | return EOK;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | static void test_expose(void *arg)
|
|---|
| 199 | {
|
|---|
| 200 | test_resp_t *resp = (test_resp_t *) arg;
|
|---|
| 201 |
|
|---|
| 202 | resp->expose = true;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | PCUT_EXPORT(resource);
|
|---|