[8009dc27] | 1 | /*
|
---|
[46a47c0] | 2 | * Copyright (c) 2023 Jiri Svoboda
|
---|
[8009dc27] | 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>
|
---|
[7481ee19] | 31 | #include <io/kbd_event.h>
|
---|
[8009dc27] | 32 | #include <io/pos_event.h>
|
---|
| 33 | #include <pcut/pcut.h>
|
---|
| 34 | #include <ui/control.h>
|
---|
[1eaead4] | 35 | #include <ui/testctl.h>
|
---|
[8009dc27] | 36 | #include <stdbool.h>
|
---|
| 37 | #include <types/ui/event.h>
|
---|
[1eaead4] | 38 | #include "../private/testctl.h"
|
---|
[8009dc27] | 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 |
|
---|
[1eaead4] | 50 | rc = ui_control_new(&ui_test_ctl_ops, NULL, &control);
|
---|
[8009dc27] | 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 |
|
---|
[c6f00b40] | 63 | /** Test sending destroy request to control */
|
---|
| 64 | PCUT_TEST(destroy)
|
---|
| 65 | {
|
---|
[1eaead4] | 66 | ui_test_ctl_t *testctl = NULL;
|
---|
| 67 | ui_tc_resp_t resp;
|
---|
[c6f00b40] | 68 | errno_t rc;
|
---|
| 69 |
|
---|
[1eaead4] | 70 | rc = ui_test_ctl_create(&resp, &testctl);
|
---|
[c6f00b40] | 71 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[1eaead4] | 72 | PCUT_ASSERT_NOT_NULL(testctl);
|
---|
[c6f00b40] | 73 |
|
---|
| 74 | resp.rc = EOK;
|
---|
| 75 | resp.destroy = false;
|
---|
| 76 |
|
---|
[1eaead4] | 77 | ui_control_destroy(ui_test_ctl_ctl(testctl));
|
---|
[c6f00b40] | 78 | PCUT_ASSERT_TRUE(resp.destroy);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[4df6607] | 81 | /** Test sending paint request to control */
|
---|
| 82 | PCUT_TEST(paint)
|
---|
| 83 | {
|
---|
[1eaead4] | 84 | ui_test_ctl_t *testctl = NULL;
|
---|
| 85 | ui_tc_resp_t resp;
|
---|
[4df6607] | 86 | errno_t rc;
|
---|
| 87 |
|
---|
[1eaead4] | 88 | rc = ui_test_ctl_create(&resp, &testctl);
|
---|
[4df6607] | 89 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[1eaead4] | 90 | PCUT_ASSERT_NOT_NULL(testctl);
|
---|
[4df6607] | 91 |
|
---|
| 92 | resp.rc = EOK;
|
---|
| 93 | resp.paint = false;
|
---|
| 94 |
|
---|
[1eaead4] | 95 | rc = ui_control_paint(ui_test_ctl_ctl(testctl));
|
---|
[4df6607] | 96 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
---|
| 97 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
| 98 |
|
---|
| 99 | resp.rc = EINVAL;
|
---|
| 100 | resp.paint = false;
|
---|
| 101 |
|
---|
[1eaead4] | 102 | rc = ui_control_paint(ui_test_ctl_ctl(testctl));
|
---|
[4df6607] | 103 | PCUT_ASSERT_ERRNO_VAL(resp.rc, rc);
|
---|
| 104 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
| 105 |
|
---|
[1eaead4] | 106 | ui_test_ctl_destroy(testctl);
|
---|
[4df6607] | 107 | }
|
---|
| 108 |
|
---|
[7481ee19] | 109 | /** Test sending keyboard event to control */
|
---|
| 110 | PCUT_TEST(kbd_event)
|
---|
| 111 | {
|
---|
[1eaead4] | 112 | ui_test_ctl_t *testctl = NULL;
|
---|
| 113 | ui_tc_resp_t resp;
|
---|
[7481ee19] | 114 | kbd_event_t event;
|
---|
| 115 | ui_evclaim_t claim;
|
---|
| 116 | errno_t rc;
|
---|
| 117 |
|
---|
[1eaead4] | 118 | rc = ui_test_ctl_create(&resp, &testctl);
|
---|
[7481ee19] | 119 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[1eaead4] | 120 | PCUT_ASSERT_NOT_NULL(testctl);
|
---|
[7481ee19] | 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 |
|
---|
[1eaead4] | 129 | claim = ui_control_kbd_event(ui_test_ctl_ctl(testctl), &event);
|
---|
[7481ee19] | 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 |
|
---|
[1eaead4] | 137 | ui_test_ctl_destroy(testctl);
|
---|
[7481ee19] | 138 | }
|
---|
| 139 |
|
---|
[8009dc27] | 140 | /** Test sending position event to control */
|
---|
| 141 | PCUT_TEST(pos_event)
|
---|
| 142 | {
|
---|
[1eaead4] | 143 | ui_test_ctl_t *testctl = NULL;
|
---|
| 144 | ui_tc_resp_t resp;
|
---|
[8009dc27] | 145 | pos_event_t event;
|
---|
| 146 | ui_evclaim_t claim;
|
---|
| 147 | errno_t rc;
|
---|
| 148 |
|
---|
[1eaead4] | 149 | rc = ui_test_ctl_create(&resp, &testctl);
|
---|
[8009dc27] | 150 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[1eaead4] | 151 | PCUT_ASSERT_NOT_NULL(testctl);
|
---|
[8009dc27] | 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 |
|
---|
[1eaead4] | 161 | claim = ui_control_pos_event(ui_test_ctl_ctl(testctl), &event);
|
---|
[8009dc27] | 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 |
|
---|
[1eaead4] | 170 | ui_test_ctl_destroy(testctl);
|
---|
[8009dc27] | 171 | }
|
---|
| 172 |
|
---|
[62223ec] | 173 | /** Test sending unfocus to control */
|
---|
| 174 | PCUT_TEST(unfocus)
|
---|
| 175 | {
|
---|
[1eaead4] | 176 | ui_test_ctl_t *testctl = NULL;
|
---|
| 177 | ui_tc_resp_t resp;
|
---|
[62223ec] | 178 | errno_t rc;
|
---|
| 179 |
|
---|
[1eaead4] | 180 | rc = ui_test_ctl_create(&resp, &testctl);
|
---|
[62223ec] | 181 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[1eaead4] | 182 | PCUT_ASSERT_NOT_NULL(testctl);
|
---|
[62223ec] | 183 |
|
---|
| 184 | resp.unfocus = false;
|
---|
| 185 |
|
---|
[1eaead4] | 186 | ui_control_unfocus(ui_test_ctl_ctl(testctl), 42);
|
---|
[62223ec] | 187 | PCUT_ASSERT_TRUE(resp.unfocus);
|
---|
[46a47c0] | 188 | PCUT_ASSERT_INT_EQUALS(42, resp.unfocus_nfocus);
|
---|
[62223ec] | 189 |
|
---|
[1eaead4] | 190 | ui_test_ctl_destroy(testctl);
|
---|
[62223ec] | 191 | }
|
---|
| 192 |
|
---|
[8009dc27] | 193 | PCUT_EXPORT(control);
|
---|