| 1 | /*
|
|---|
| 2 | * Copyright (c) 2023 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 INTvvhhzccgggrERRUPTION) 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 <mem.h>
|
|---|
| 31 | #include <io/kbd_event.h>
|
|---|
| 32 | #include <io/pos_event.h>
|
|---|
| 33 | #include <pcut/pcut.h>
|
|---|
| 34 | #include <ui/control.h>
|
|---|
| 35 | #include <ui/testctl.h>
|
|---|
| 36 | #include <stdbool.h>
|
|---|
| 37 | #include <types/ui/event.h>
|
|---|
| 38 | #include "../private/testctl.h"
|
|---|
| 39 |
|
|---|
| 40 | PCUT_INIT;
|
|---|
| 41 |
|
|---|
| 42 | PCUT_TEST_SUITE(control);
|
|---|
| 43 |
|
|---|
| 44 | /** Allocate and deallocate control */
|
|---|
| 45 | PCUT_TEST(new_delete)
|
|---|
| 46 | {
|
|---|
| 47 | ui_control_t *control = NULL;
|
|---|
| 48 | errno_t rc;
|
|---|
| 49 |
|
|---|
| 50 | rc = ui_control_new(&ui_test_ctl_ops, NULL, &control);
|
|---|
| 51 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 52 | PCUT_ASSERT_NOT_NULL(control);
|
|---|
| 53 |
|
|---|
| 54 | ui_control_delete(control);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /** ui_control_delete() can take NULL argument (no-op) */
|
|---|
| 58 | PCUT_TEST(delete_null)
|
|---|
| 59 | {
|
|---|
| 60 | ui_control_delete(NULL);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /** Test sending destroy request to control */
|
|---|
| 64 | PCUT_TEST(destroy)
|
|---|
| 65 | {
|
|---|
| 66 | ui_test_ctl_t *testctl = NULL;
|
|---|
| 67 | ui_tc_resp_t resp;
|
|---|
| 68 | errno_t rc;
|
|---|
| 69 |
|
|---|
| 70 | rc = ui_test_ctl_create(&resp, &testctl);
|
|---|
| 71 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 72 | PCUT_ASSERT_NOT_NULL(testctl);
|
|---|
| 73 |
|
|---|
| 74 | resp.rc = EOK;
|
|---|
| 75 | resp.destroy = false;
|
|---|
| 76 |
|
|---|
| 77 | ui_control_destroy(ui_test_ctl_ctl(testctl));
|
|---|
| 78 | PCUT_ASSERT_TRUE(resp.destroy);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /** Test sending paint request to control */
|
|---|
| 82 | PCUT_TEST(paint)
|
|---|
| 83 | {
|
|---|
| 84 | ui_test_ctl_t *testctl = NULL;
|
|---|
| 85 | ui_tc_resp_t resp;
|
|---|
| 86 | errno_t rc;
|
|---|
| 87 |
|
|---|
| 88 | rc = ui_test_ctl_create(&resp, &testctl);
|
|---|
| 89 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 90 | PCUT_ASSERT_NOT_NULL(testctl);
|
|---|
| 91 |
|
|---|
| 92 | resp.rc = EOK;
|
|---|
| 93 | resp.paint = false;
|
|---|
| 94 |
|
|---|
| 95 | rc = ui_control_paint(ui_test_ctl_ctl(testctl));
|
|---|
| 96 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
|---|
| 97 | PCUT_ASSERT_TRUE(resp.paint);
|
|---|
| 98 |
|
|---|
| 99 | resp.rc = EINVAL;
|
|---|
| 100 | resp.paint = false;
|
|---|
| 101 |
|
|---|
| 102 | rc = ui_control_paint(ui_test_ctl_ctl(testctl));
|
|---|
| 103 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
|---|
| 104 | PCUT_ASSERT_TRUE(resp.paint);
|
|---|
| 105 |
|
|---|
| 106 | ui_test_ctl_destroy(testctl);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /** Test sending keyboard event to control */
|
|---|
| 110 | PCUT_TEST(kbd_event)
|
|---|
| 111 | {
|
|---|
| 112 | ui_test_ctl_t *testctl = NULL;
|
|---|
| 113 | ui_tc_resp_t resp;
|
|---|
| 114 | kbd_event_t event;
|
|---|
| 115 | ui_evclaim_t claim;
|
|---|
| 116 | errno_t rc;
|
|---|
| 117 |
|
|---|
| 118 | rc = ui_test_ctl_create(&resp, &testctl);
|
|---|
| 119 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 120 | PCUT_ASSERT_NOT_NULL(testctl);
|
|---|
| 121 |
|
|---|
| 122 | resp.claim = ui_claimed;
|
|---|
| 123 | resp.kbd = false;
|
|---|
| 124 | event.type = KEY_PRESS;
|
|---|
| 125 | event.key = KC_2;
|
|---|
| 126 | event.mods = KM_LSHIFT;
|
|---|
| 127 | event.c = '@';
|
|---|
| 128 |
|
|---|
| 129 | claim = ui_control_kbd_event(ui_test_ctl_ctl(testctl), &event);
|
|---|
| 130 | PCUT_ASSERT_EQUALS(resp.claim, claim);
|
|---|
| 131 | PCUT_ASSERT_TRUE(resp.kbd);
|
|---|
| 132 | PCUT_ASSERT_EQUALS(resp.kevent.type, event.type);
|
|---|
| 133 | PCUT_ASSERT_INT_EQUALS(resp.kevent.key, event.key);
|
|---|
| 134 | PCUT_ASSERT_INT_EQUALS(resp.kevent.mods, event.mods);
|
|---|
| 135 | PCUT_ASSERT_INT_EQUALS(resp.kevent.c, event.c);
|
|---|
| 136 |
|
|---|
| 137 | ui_test_ctl_destroy(testctl);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /** Test sending position event to control */
|
|---|
| 141 | PCUT_TEST(pos_event)
|
|---|
| 142 | {
|
|---|
| 143 | ui_test_ctl_t *testctl = NULL;
|
|---|
| 144 | ui_tc_resp_t resp;
|
|---|
| 145 | pos_event_t event;
|
|---|
| 146 | ui_evclaim_t claim;
|
|---|
| 147 | errno_t rc;
|
|---|
| 148 |
|
|---|
| 149 | rc = ui_test_ctl_create(&resp, &testctl);
|
|---|
| 150 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 151 | PCUT_ASSERT_NOT_NULL(testctl);
|
|---|
| 152 |
|
|---|
| 153 | resp.claim = ui_claimed;
|
|---|
| 154 | resp.pos = false;
|
|---|
| 155 | event.pos_id = 1;
|
|---|
| 156 | event.type = POS_PRESS;
|
|---|
| 157 | event.btn_num = 2;
|
|---|
| 158 | event.hpos = 3;
|
|---|
| 159 | event.vpos = 4;
|
|---|
| 160 |
|
|---|
| 161 | claim = ui_control_pos_event(ui_test_ctl_ctl(testctl), &event);
|
|---|
| 162 | PCUT_ASSERT_EQUALS(resp.claim, claim);
|
|---|
| 163 | PCUT_ASSERT_TRUE(resp.pos);
|
|---|
| 164 | PCUT_ASSERT_INT_EQUALS(resp.pevent.pos_id, event.pos_id);
|
|---|
| 165 | PCUT_ASSERT_EQUALS(resp.pevent.type, event.type);
|
|---|
| 166 | PCUT_ASSERT_INT_EQUALS(resp.pevent.btn_num, event.btn_num);
|
|---|
| 167 | PCUT_ASSERT_INT_EQUALS(resp.pevent.hpos, event.hpos);
|
|---|
| 168 | PCUT_ASSERT_INT_EQUALS(resp.pevent.vpos, event.vpos);
|
|---|
| 169 |
|
|---|
| 170 | ui_test_ctl_destroy(testctl);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /** Test sending unfocus to control */
|
|---|
| 174 | PCUT_TEST(unfocus)
|
|---|
| 175 | {
|
|---|
| 176 | ui_test_ctl_t *testctl = NULL;
|
|---|
| 177 | ui_tc_resp_t resp;
|
|---|
| 178 | errno_t rc;
|
|---|
| 179 |
|
|---|
| 180 | rc = ui_test_ctl_create(&resp, &testctl);
|
|---|
| 181 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 182 | PCUT_ASSERT_NOT_NULL(testctl);
|
|---|
| 183 |
|
|---|
| 184 | resp.unfocus = false;
|
|---|
| 185 |
|
|---|
| 186 | ui_control_unfocus(ui_test_ctl_ctl(testctl), 42);
|
|---|
| 187 | PCUT_ASSERT_TRUE(resp.unfocus);
|
|---|
| 188 | PCUT_ASSERT_INT_EQUALS(42, resp.unfocus_nfocus);
|
|---|
| 189 |
|
|---|
| 190 | ui_test_ctl_destroy(testctl);
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | PCUT_EXPORT(control);
|
|---|