[4d8002d] | 1 | /*
|
---|
| 2 | * Copyright (c) 2020 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 <errno.h>
|
---|
[4912dd59] | 30 | #include <gfx/context.h>
|
---|
| 31 | #include <stdbool.h>
|
---|
[4d8002d] | 32 | #include <pcut/pcut.h>
|
---|
| 33 |
|
---|
| 34 | #include "../cursor.h"
|
---|
| 35 | #include "../cursimg.h"
|
---|
[4912dd59] | 36 | #include "../ddev.h"
|
---|
| 37 | #include "../display.h"
|
---|
[4d8002d] | 38 | #include "../display.h"
|
---|
| 39 |
|
---|
| 40 | PCUT_INIT;
|
---|
| 41 |
|
---|
| 42 | PCUT_TEST_SUITE(cursor);
|
---|
| 43 |
|
---|
[4912dd59] | 44 | static errno_t dummy_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
| 45 | gfx_bitmap_alloc_t *, void **);
|
---|
| 46 | static errno_t dummy_bitmap_destroy(void *);
|
---|
| 47 | static errno_t dummy_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
| 48 | static errno_t dummy_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
| 49 |
|
---|
| 50 | static gfx_context_ops_t dummy_ops = {
|
---|
| 51 | .bitmap_create = dummy_bitmap_create,
|
---|
| 52 | .bitmap_destroy = dummy_bitmap_destroy,
|
---|
| 53 | .bitmap_render = dummy_bitmap_render,
|
---|
| 54 | .bitmap_get_alloc = dummy_bitmap_get_alloc
|
---|
| 55 | };
|
---|
| 56 |
|
---|
| 57 | typedef struct {
|
---|
| 58 | bool render_called;
|
---|
| 59 | } test_response_t;
|
---|
| 60 |
|
---|
| 61 | typedef struct {
|
---|
| 62 | test_response_t *resp;
|
---|
| 63 | gfx_bitmap_alloc_t alloc;
|
---|
| 64 | } dummy_bitmap_t;
|
---|
| 65 |
|
---|
[4d8002d] | 66 | /** Test ds_cursor_create(), ds_cursor_destroy(). */
|
---|
| 67 | PCUT_TEST(cursor_create_destroy)
|
---|
| 68 | {
|
---|
| 69 | ds_display_t *disp;
|
---|
| 70 | ds_cursor_t *cursor;
|
---|
| 71 | errno_t rc;
|
---|
| 72 |
|
---|
| 73 | rc = ds_display_create(NULL, &disp);
|
---|
| 74 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 75 |
|
---|
| 76 | rc = ds_cursor_create(disp, &ds_cursimg[dcurs_arrow].rect,
|
---|
| 77 | ds_cursimg[dcurs_arrow].image, &cursor);
|
---|
| 78 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 79 |
|
---|
| 80 | ds_cursor_destroy(cursor);
|
---|
| 81 | ds_display_destroy(disp);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /** Test ds_cursor_paint(). */
|
---|
| 85 | PCUT_TEST(cursor_paint)
|
---|
| 86 | {
|
---|
[4912dd59] | 87 | gfx_context_t *gc;
|
---|
[4d8002d] | 88 | ds_display_t *disp;
|
---|
| 89 | ds_cursor_t *cursor;
|
---|
[4912dd59] | 90 | ds_ddev_t *ddev;
|
---|
| 91 | ddev_info_t ddinfo;
|
---|
| 92 | gfx_coord2_t pos;
|
---|
| 93 | test_response_t resp;
|
---|
[4d8002d] | 94 | errno_t rc;
|
---|
| 95 |
|
---|
[4912dd59] | 96 | rc = gfx_context_new(&dummy_ops, &resp, &gc);
|
---|
| 97 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 98 |
|
---|
| 99 | rc = ds_display_create(gc, &disp);
|
---|
| 100 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 101 |
|
---|
| 102 | ddev_info_init(&ddinfo);
|
---|
| 103 |
|
---|
| 104 | rc = ds_ddev_create(disp, NULL, &ddinfo, NULL, 0, gc, &ddev);
|
---|
[4d8002d] | 105 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 106 |
|
---|
| 107 | rc = ds_cursor_create(disp, &ds_cursimg[dcurs_arrow].rect,
|
---|
| 108 | ds_cursimg[dcurs_arrow].image, &cursor);
|
---|
| 109 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 110 |
|
---|
[4912dd59] | 111 | resp.render_called = false;
|
---|
| 112 |
|
---|
| 113 | pos.x = 0;
|
---|
| 114 | pos.y = 0;
|
---|
| 115 | ds_cursor_paint(cursor, &pos);
|
---|
| 116 |
|
---|
| 117 | PCUT_ASSERT_TRUE(resp.render_called);
|
---|
| 118 |
|
---|
[4d8002d] | 119 | ds_cursor_destroy(cursor);
|
---|
| 120 | ds_display_destroy(disp);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /** Test ds_cursor_get_rect() */
|
---|
| 124 | PCUT_TEST(cursor_get_rect)
|
---|
| 125 | {
|
---|
| 126 | ds_display_t *disp;
|
---|
| 127 | ds_cursor_t *cursor;
|
---|
[4912dd59] | 128 | gfx_coord2_t pos1, pos2;
|
---|
| 129 | gfx_rect_t rect1, rect2;
|
---|
[4d8002d] | 130 | errno_t rc;
|
---|
| 131 |
|
---|
| 132 | rc = ds_display_create(NULL, &disp);
|
---|
| 133 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 134 |
|
---|
| 135 | rc = ds_cursor_create(disp, &ds_cursimg[dcurs_arrow].rect,
|
---|
| 136 | ds_cursimg[dcurs_arrow].image, &cursor);
|
---|
| 137 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 138 |
|
---|
[4912dd59] | 139 | pos1.x = 10;
|
---|
| 140 | pos1.y = 11;
|
---|
| 141 |
|
---|
| 142 | pos2.x = 22;
|
---|
| 143 | pos2.y = 23;
|
---|
| 144 |
|
---|
| 145 | ds_cursor_get_rect(cursor, &pos1, &rect1);
|
---|
| 146 | ds_cursor_get_rect(cursor, &pos2, &rect2);
|
---|
| 147 |
|
---|
| 148 | PCUT_ASSERT_FALSE(gfx_rect_is_empty(&rect1));
|
---|
| 149 | PCUT_ASSERT_FALSE(gfx_rect_is_empty(&rect2));
|
---|
| 150 |
|
---|
| 151 | PCUT_ASSERT_INT_EQUALS(pos2.x - pos1.x, rect2.p0.x - rect1.p0.x);
|
---|
| 152 | PCUT_ASSERT_INT_EQUALS(pos2.y - pos1.y, rect2.p0.y - rect1.p0.y);
|
---|
| 153 | PCUT_ASSERT_INT_EQUALS(pos2.x - pos1.x, rect2.p1.x - rect1.p1.x);
|
---|
| 154 | PCUT_ASSERT_INT_EQUALS(pos2.y - pos1.y, rect2.p1.y - rect1.p1.y);
|
---|
| 155 |
|
---|
[4d8002d] | 156 | ds_cursor_destroy(cursor);
|
---|
| 157 | ds_display_destroy(disp);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[4912dd59] | 160 | static errno_t dummy_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
| 161 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
| 162 | {
|
---|
| 163 | test_response_t *resp = (test_response_t *) arg;
|
---|
| 164 | dummy_bitmap_t *bm;
|
---|
| 165 | gfx_coord2_t dims;
|
---|
| 166 |
|
---|
| 167 | gfx_rect_dims(¶ms->rect, &dims);
|
---|
| 168 | bm = calloc(1, sizeof(dummy_bitmap_t));
|
---|
| 169 | if (bm == NULL)
|
---|
| 170 | return ENOMEM;
|
---|
| 171 |
|
---|
| 172 | bm->resp = resp;
|
---|
| 173 | bm->alloc.pitch = dims.x * sizeof(uint32_t);
|
---|
| 174 | bm->alloc.off0 = 0;
|
---|
| 175 |
|
---|
| 176 | bm->alloc.pixels = malloc(bm->alloc.pitch * dims.y * sizeof(uint32_t));
|
---|
| 177 | if (bm->alloc.pixels == NULL) {
|
---|
| 178 | free(bm);
|
---|
| 179 | return ENOMEM;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | *rbm = (void *) bm;
|
---|
| 183 | return EOK;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | static errno_t dummy_bitmap_destroy(void *arg)
|
---|
| 187 | {
|
---|
| 188 | dummy_bitmap_t *bm = (dummy_bitmap_t *) arg;
|
---|
| 189 |
|
---|
| 190 | free(bm);
|
---|
| 191 | return EOK;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | static errno_t dummy_bitmap_render(void *arg, gfx_rect_t *rect,
|
---|
| 195 | gfx_coord2_t *dpos)
|
---|
| 196 | {
|
---|
| 197 | dummy_bitmap_t *bm = (dummy_bitmap_t *) arg;
|
---|
| 198 |
|
---|
| 199 | bm->resp->render_called = true;
|
---|
| 200 | return EOK;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | static errno_t dummy_bitmap_get_alloc(void *arg, gfx_bitmap_alloc_t *alloc)
|
---|
| 204 | {
|
---|
| 205 | dummy_bitmap_t *bm = (dummy_bitmap_t *) arg;
|
---|
| 206 |
|
---|
| 207 | *alloc = bm->alloc;
|
---|
| 208 | return EOK;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[4d8002d] | 211 | PCUT_EXPORT(cursor);
|
---|