[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,
|
---|
[c6f00b40] | 23 | * DATA, OR PROFITS; OR BUSINESS INTvvhhzccgggrERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
[8009dc27] | 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 <mem.h>
|
---|
| 31 | #include <io/pos_event.h>
|
---|
| 32 | #include <pcut/pcut.h>
|
---|
| 33 | #include <ui/control.h>
|
---|
| 34 | #include <stdbool.h>
|
---|
| 35 | #include <types/ui/event.h>
|
---|
| 36 |
|
---|
| 37 | PCUT_INIT;
|
---|
| 38 |
|
---|
| 39 | PCUT_TEST_SUITE(control);
|
---|
| 40 |
|
---|
[c6f00b40] | 41 | static void test_ctl_destroy(void *);
|
---|
[4df6607] | 42 | static errno_t test_ctl_paint(void *);
|
---|
[8009dc27] | 43 | static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
|
---|
| 44 |
|
---|
| 45 | static ui_control_ops_t test_ctl_ops = {
|
---|
[c6f00b40] | 46 | .destroy = test_ctl_destroy,
|
---|
[4df6607] | 47 | .paint = test_ctl_paint,
|
---|
[8009dc27] | 48 | .pos_event = test_ctl_pos_event
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | /** Test response */
|
---|
| 52 | typedef struct {
|
---|
| 53 | /** Claim to return */
|
---|
| 54 | ui_evclaim_t claim;
|
---|
[4df6607] | 55 | /** Result code to return */
|
---|
| 56 | errno_t rc;
|
---|
| 57 |
|
---|
[c6f00b40] | 58 | /** @c true iff destroy was called */
|
---|
| 59 | bool destroy;
|
---|
| 60 |
|
---|
[4df6607] | 61 | /** @c true iff paint was called */
|
---|
| 62 | bool paint;
|
---|
[8009dc27] | 63 |
|
---|
| 64 | /** @c true iff pos_event was called */
|
---|
| 65 | bool pos;
|
---|
| 66 | /** Position event that was sent */
|
---|
| 67 | pos_event_t pevent;
|
---|
| 68 | } test_resp_t;
|
---|
| 69 |
|
---|
| 70 | /** Allocate and deallocate control */
|
---|
| 71 | PCUT_TEST(new_delete)
|
---|
| 72 | {
|
---|
| 73 | ui_control_t *control = NULL;
|
---|
| 74 | errno_t rc;
|
---|
| 75 |
|
---|
| 76 | rc = ui_control_new(&test_ctl_ops, NULL, &control);
|
---|
| 77 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 78 | PCUT_ASSERT_NOT_NULL(control);
|
---|
| 79 |
|
---|
| 80 | ui_control_delete(control);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /** ui_control_delete() can take NULL argument (no-op) */
|
---|
| 84 | PCUT_TEST(delete_null)
|
---|
| 85 | {
|
---|
| 86 | ui_control_delete(NULL);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[c6f00b40] | 89 | /** Test sending destroy request to control */
|
---|
| 90 | PCUT_TEST(destroy)
|
---|
| 91 | {
|
---|
| 92 | ui_control_t *control = NULL;
|
---|
| 93 | test_resp_t resp;
|
---|
| 94 | errno_t rc;
|
---|
| 95 |
|
---|
| 96 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
| 97 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 98 | PCUT_ASSERT_NOT_NULL(control);
|
---|
| 99 |
|
---|
| 100 | resp.rc = EOK;
|
---|
| 101 | resp.destroy = false;
|
---|
| 102 |
|
---|
| 103 | ui_control_destroy(control);
|
---|
| 104 | PCUT_ASSERT_TRUE(resp.destroy);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[4df6607] | 107 | /** Test sending paint request to control */
|
---|
| 108 | PCUT_TEST(paint)
|
---|
| 109 | {
|
---|
| 110 | ui_control_t *control = NULL;
|
---|
| 111 | test_resp_t resp;
|
---|
| 112 | errno_t rc;
|
---|
| 113 |
|
---|
| 114 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
| 115 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 116 | PCUT_ASSERT_NOT_NULL(control);
|
---|
| 117 |
|
---|
| 118 | resp.rc = EOK;
|
---|
| 119 | resp.paint = false;
|
---|
| 120 |
|
---|
| 121 | rc = ui_control_paint(control);
|
---|
| 122 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
---|
| 123 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
| 124 |
|
---|
| 125 | resp.rc = EINVAL;
|
---|
| 126 | resp.paint = false;
|
---|
| 127 |
|
---|
| 128 | rc = ui_control_paint(control);
|
---|
| 129 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
---|
| 130 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
| 131 |
|
---|
| 132 | ui_control_delete(control);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[8009dc27] | 135 | /** Test sending position event to control */
|
---|
| 136 | PCUT_TEST(pos_event)
|
---|
| 137 | {
|
---|
| 138 | ui_control_t *control = NULL;
|
---|
| 139 | test_resp_t resp;
|
---|
| 140 | pos_event_t event;
|
---|
| 141 | ui_evclaim_t claim;
|
---|
| 142 | errno_t rc;
|
---|
| 143 |
|
---|
| 144 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
| 145 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 146 | PCUT_ASSERT_NOT_NULL(control);
|
---|
| 147 |
|
---|
| 148 | resp.claim = ui_claimed;
|
---|
| 149 | resp.pos = false;
|
---|
| 150 | event.pos_id = 1;
|
---|
| 151 | event.type = POS_PRESS;
|
---|
| 152 | event.btn_num = 2;
|
---|
| 153 | event.hpos = 3;
|
---|
| 154 | event.vpos = 4;
|
---|
| 155 |
|
---|
| 156 | claim = ui_control_pos_event(control, &event);
|
---|
| 157 | PCUT_ASSERT_EQUALS(resp.claim, claim);
|
---|
| 158 | PCUT_ASSERT_TRUE(resp.pos);
|
---|
| 159 | PCUT_ASSERT_INT_EQUALS(resp.pevent.pos_id, event.pos_id);
|
---|
| 160 | PCUT_ASSERT_EQUALS(resp.pevent.type, event.type);
|
---|
| 161 | PCUT_ASSERT_INT_EQUALS(resp.pevent.btn_num, event.btn_num);
|
---|
| 162 | PCUT_ASSERT_INT_EQUALS(resp.pevent.hpos, event.hpos);
|
---|
| 163 | PCUT_ASSERT_INT_EQUALS(resp.pevent.vpos, event.vpos);
|
---|
| 164 |
|
---|
| 165 | ui_control_delete(control);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[c6f00b40] | 168 | static void test_ctl_destroy(void *arg)
|
---|
| 169 | {
|
---|
| 170 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
| 171 |
|
---|
| 172 | resp->destroy = true;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[4df6607] | 175 | static errno_t test_ctl_paint(void *arg)
|
---|
| 176 | {
|
---|
| 177 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
| 178 |
|
---|
| 179 | resp->paint = true;
|
---|
| 180 | return resp->rc;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[8009dc27] | 183 | static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
| 184 | {
|
---|
| 185 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
| 186 |
|
---|
| 187 | resp->pos = true;
|
---|
| 188 | resp->pevent = *event;
|
---|
| 189 |
|
---|
| 190 | return resp->claim;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | PCUT_EXPORT(control);
|
---|