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