[8009dc27] | 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 |
|
---|
[4df6607] | 29 | #include <errno.h>
|
---|
[8009dc27] | 30 | #include <pcut/pcut.h>
|
---|
| 31 | #include <stddef.h>
|
---|
| 32 | #include <ui/control.h>
|
---|
| 33 | #include <ui/fixed.h>
|
---|
| 34 | #include "../private/fixed.h"
|
---|
| 35 |
|
---|
| 36 | PCUT_INIT;
|
---|
| 37 |
|
---|
| 38 | PCUT_TEST_SUITE(fixed);
|
---|
| 39 |
|
---|
[c6f00b40] | 40 | static void test_ctl_destroy(void *);
|
---|
[4df6607] | 41 | static errno_t test_ctl_paint(void *);
|
---|
[8009dc27] | 42 | static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
|
---|
[62223ec] | 43 | static void test_ctl_unfocus(void *);
|
---|
[8009dc27] | 44 |
|
---|
| 45 | static ui_control_ops_t test_ctl_ops = {
|
---|
[c6f00b40] | 46 | .destroy = test_ctl_destroy,
|
---|
[4df6607] | 47 | .paint = test_ctl_paint,
|
---|
[62223ec] | 48 | .pos_event = test_ctl_pos_event,
|
---|
| 49 | .unfocus = test_ctl_unfocus
|
---|
[8009dc27] | 50 | };
|
---|
| 51 |
|
---|
| 52 | /** Test response */
|
---|
| 53 | typedef struct {
|
---|
| 54 | /** Claim to return */
|
---|
| 55 | ui_evclaim_t claim;
|
---|
[4df6607] | 56 | /** Result code to return */
|
---|
| 57 | errno_t rc;
|
---|
[c6f00b40] | 58 | /** @c true iff destroy was called */
|
---|
| 59 | bool destroy;
|
---|
[4df6607] | 60 | /** @c true iff paint was called */
|
---|
| 61 | bool paint;
|
---|
[8009dc27] | 62 | /** @c true iff pos_event was called */
|
---|
| 63 | bool pos;
|
---|
| 64 | /** Position event that was sent */
|
---|
| 65 | pos_event_t pevent;
|
---|
[62223ec] | 66 | /** @c true iff unfocus was called */
|
---|
| 67 | bool unfocus;
|
---|
[8009dc27] | 68 | } test_resp_t;
|
---|
| 69 |
|
---|
| 70 | /** Create and destroy button */
|
---|
| 71 | PCUT_TEST(create_destroy)
|
---|
| 72 | {
|
---|
| 73 | ui_fixed_t *fixed = NULL;
|
---|
| 74 | errno_t rc;
|
---|
| 75 |
|
---|
| 76 | rc = ui_fixed_create(&fixed);
|
---|
| 77 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 78 | PCUT_ASSERT_NOT_NULL(fixed);
|
---|
| 79 |
|
---|
| 80 | ui_fixed_destroy(fixed);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /** ui_fixed_destroy() can take NULL argument (no-op) */
|
---|
| 84 | PCUT_TEST(destroy_null)
|
---|
| 85 | {
|
---|
| 86 | ui_fixed_destroy(NULL);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[b71c0fc] | 89 | /** ui_fixed_ctl() returns control that has a working virtual destructor */
|
---|
| 90 | PCUT_TEST(ctl)
|
---|
| 91 | {
|
---|
| 92 | ui_fixed_t *fixed;
|
---|
| 93 | ui_control_t *control;
|
---|
| 94 | errno_t rc;
|
---|
| 95 |
|
---|
| 96 | rc = ui_fixed_create(&fixed);
|
---|
| 97 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 98 |
|
---|
| 99 | control = ui_fixed_ctl(fixed);
|
---|
| 100 | PCUT_ASSERT_NOT_NULL(control);
|
---|
| 101 |
|
---|
| 102 | ui_control_destroy(control);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[8009dc27] | 105 | /** ui_fixed_add() / ui_fixed_remove() adds/removes control */
|
---|
| 106 | PCUT_TEST(add_remove)
|
---|
| 107 | {
|
---|
| 108 | ui_fixed_t *fixed = NULL;
|
---|
| 109 | ui_control_t *control;
|
---|
| 110 | ui_fixed_elem_t *e;
|
---|
| 111 | errno_t rc;
|
---|
| 112 |
|
---|
| 113 | rc = ui_fixed_create(&fixed);
|
---|
| 114 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 115 |
|
---|
| 116 | e = ui_fixed_first(fixed);
|
---|
| 117 | PCUT_ASSERT_NULL(e);
|
---|
| 118 |
|
---|
| 119 | rc = ui_control_new(NULL, NULL, &control);
|
---|
| 120 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 121 |
|
---|
| 122 | rc = ui_fixed_add(fixed, control);
|
---|
| 123 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 124 |
|
---|
| 125 | e = ui_fixed_first(fixed);
|
---|
| 126 | PCUT_ASSERT_NOT_NULL(e);
|
---|
| 127 | PCUT_ASSERT_EQUALS(control, e->control);
|
---|
| 128 | e = ui_fixed_next(e);
|
---|
| 129 | PCUT_ASSERT_NULL(e);
|
---|
| 130 |
|
---|
| 131 | ui_fixed_remove(fixed, control);
|
---|
| 132 |
|
---|
| 133 | e = ui_fixed_first(fixed);
|
---|
| 134 | PCUT_ASSERT_NULL(e);
|
---|
| 135 |
|
---|
| 136 | ui_fixed_destroy(fixed);
|
---|
[c6f00b40] | 137 | ui_control_delete(control);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /** ui_fixed_destroy() delivers destroy request to control */
|
---|
| 141 | PCUT_TEST(destroy)
|
---|
| 142 | {
|
---|
| 143 | ui_fixed_t *fixed = NULL;
|
---|
| 144 | ui_control_t *control;
|
---|
| 145 | test_resp_t resp;
|
---|
| 146 | errno_t rc;
|
---|
| 147 |
|
---|
| 148 | rc = ui_fixed_create(&fixed);
|
---|
| 149 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 150 |
|
---|
| 151 | rc = ui_control_new(&test_ctl_ops, (void *) &resp, &control);
|
---|
| 152 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 153 |
|
---|
| 154 | rc = ui_fixed_add(fixed, control);
|
---|
| 155 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 156 |
|
---|
| 157 | resp.destroy = false;
|
---|
| 158 |
|
---|
| 159 | ui_fixed_destroy(fixed);
|
---|
| 160 | PCUT_ASSERT_TRUE(resp.destroy);
|
---|
[8009dc27] | 161 | }
|
---|
| 162 |
|
---|
[4df6607] | 163 | /** ui_fixed_paint() delivers paint request to control */
|
---|
| 164 | PCUT_TEST(paint)
|
---|
| 165 | {
|
---|
| 166 | ui_fixed_t *fixed = NULL;
|
---|
| 167 | ui_control_t *control;
|
---|
| 168 | test_resp_t resp;
|
---|
| 169 | errno_t rc;
|
---|
| 170 |
|
---|
| 171 | rc = ui_fixed_create(&fixed);
|
---|
| 172 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 173 |
|
---|
| 174 | rc = ui_control_new(&test_ctl_ops, (void *) &resp, &control);
|
---|
| 175 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 176 |
|
---|
| 177 | rc = ui_fixed_add(fixed, control);
|
---|
| 178 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 179 |
|
---|
| 180 | resp.paint = false;
|
---|
| 181 | resp.rc = EOK;
|
---|
| 182 |
|
---|
| 183 | rc = ui_fixed_paint(fixed);
|
---|
| 184 | PCUT_ASSERT_EQUALS(resp.rc, rc);
|
---|
| 185 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
| 186 |
|
---|
| 187 | resp.paint = false;
|
---|
| 188 | resp.rc = EINVAL;
|
---|
| 189 |
|
---|
| 190 | rc = ui_fixed_paint(fixed);
|
---|
| 191 | PCUT_ASSERT_EQUALS(resp.rc, rc);
|
---|
| 192 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
| 193 |
|
---|
| 194 | ui_fixed_destroy(fixed);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /** ui_fixed_pos_event() delivers position event to control */
|
---|
[8009dc27] | 198 | PCUT_TEST(pos_event)
|
---|
| 199 | {
|
---|
| 200 | ui_fixed_t *fixed = NULL;
|
---|
| 201 | ui_control_t *control;
|
---|
| 202 | ui_evclaim_t claim;
|
---|
| 203 | pos_event_t event;
|
---|
| 204 | test_resp_t resp;
|
---|
| 205 | errno_t rc;
|
---|
| 206 |
|
---|
| 207 | rc = ui_fixed_create(&fixed);
|
---|
| 208 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 209 |
|
---|
| 210 | rc = ui_control_new(&test_ctl_ops, (void *) &resp, &control);
|
---|
| 211 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 212 |
|
---|
| 213 | rc = ui_fixed_add(fixed, control);
|
---|
| 214 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 215 |
|
---|
| 216 | resp.claim = ui_claimed;
|
---|
| 217 | resp.pos = false;
|
---|
| 218 | event.pos_id = 1;
|
---|
| 219 | event.type = POS_PRESS;
|
---|
| 220 | event.btn_num = 2;
|
---|
| 221 | event.hpos = 3;
|
---|
| 222 | event.vpos = 4;
|
---|
| 223 |
|
---|
| 224 | claim = ui_fixed_pos_event(fixed, &event);
|
---|
| 225 | PCUT_ASSERT_EQUALS(resp.claim, claim);
|
---|
| 226 | PCUT_ASSERT_TRUE(resp.pos);
|
---|
| 227 | PCUT_ASSERT_INT_EQUALS(resp.pevent.pos_id, event.pos_id);
|
---|
| 228 | PCUT_ASSERT_EQUALS(resp.pevent.type, event.type);
|
---|
| 229 | PCUT_ASSERT_INT_EQUALS(resp.pevent.btn_num, event.btn_num);
|
---|
| 230 | PCUT_ASSERT_INT_EQUALS(resp.pevent.hpos, event.hpos);
|
---|
| 231 | PCUT_ASSERT_INT_EQUALS(resp.pevent.vpos, event.vpos);
|
---|
| 232 |
|
---|
| 233 | ui_fixed_destroy(fixed);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[62223ec] | 236 | /** ui_fixed_unfocus() delivers unfocus notification to control */
|
---|
| 237 | PCUT_TEST(unfocus)
|
---|
| 238 | {
|
---|
| 239 | ui_fixed_t *fixed = NULL;
|
---|
| 240 | ui_control_t *control;
|
---|
| 241 | test_resp_t resp;
|
---|
| 242 | errno_t rc;
|
---|
| 243 |
|
---|
| 244 | rc = ui_fixed_create(&fixed);
|
---|
| 245 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 246 |
|
---|
| 247 | rc = ui_control_new(&test_ctl_ops, (void *) &resp, &control);
|
---|
| 248 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 249 |
|
---|
| 250 | rc = ui_fixed_add(fixed, control);
|
---|
| 251 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 252 |
|
---|
| 253 | resp.unfocus = false;
|
---|
| 254 |
|
---|
| 255 | ui_fixed_unfocus(fixed);
|
---|
| 256 | PCUT_ASSERT_TRUE(resp.unfocus);
|
---|
| 257 |
|
---|
| 258 | ui_fixed_destroy(fixed);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[c6f00b40] | 261 | static void test_ctl_destroy(void *arg)
|
---|
| 262 | {
|
---|
| 263 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
| 264 |
|
---|
| 265 | resp->destroy = true;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[4df6607] | 268 | static errno_t test_ctl_paint(void *arg)
|
---|
| 269 | {
|
---|
| 270 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
| 271 |
|
---|
| 272 | resp->paint = true;
|
---|
| 273 | return resp->rc;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[8009dc27] | 276 | static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
| 277 | {
|
---|
| 278 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
| 279 |
|
---|
| 280 | resp->pos = true;
|
---|
| 281 | resp->pevent = *event;
|
---|
| 282 |
|
---|
| 283 | return resp->claim;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[62223ec] | 286 | static void test_ctl_unfocus(void *arg)
|
---|
| 287 | {
|
---|
| 288 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
| 289 |
|
---|
| 290 | resp->unfocus = true;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
[8009dc27] | 293 | PCUT_EXPORT(fixed);
|
---|