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