[1eaead4] | 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 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 | /** @addtogroup libui
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file Test control
|
---|
| 34 | *
|
---|
| 35 | * Test control allows to read the arguments of and inject the responses
|
---|
| 36 | * to all control methods.
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
| 41 | #include <str.h>
|
---|
| 42 | #include <ui/control.h>
|
---|
| 43 | #include <ui/testctl.h>
|
---|
| 44 | #include "../private/testctl.h"
|
---|
| 45 |
|
---|
| 46 | static void test_ctl_destroy(void *);
|
---|
| 47 | static errno_t test_ctl_paint(void *);
|
---|
| 48 | static ui_evclaim_t test_ctl_kbd_event(void *, kbd_event_t *);
|
---|
| 49 | static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
|
---|
| 50 | static void test_ctl_unfocus(void *, unsigned);
|
---|
| 51 |
|
---|
| 52 | ui_control_ops_t ui_test_ctl_ops = {
|
---|
| 53 | .destroy = test_ctl_destroy,
|
---|
| 54 | .paint = test_ctl_paint,
|
---|
| 55 | .kbd_event = test_ctl_kbd_event,
|
---|
| 56 | .pos_event = test_ctl_pos_event,
|
---|
| 57 | .unfocus = test_ctl_unfocus
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | static void test_ctl_destroy(void *arg)
|
---|
| 61 | {
|
---|
| 62 | ui_test_ctl_t *testctl = (ui_test_ctl_t *)arg;
|
---|
| 63 | ui_tc_resp_t *resp = testctl->resp;
|
---|
| 64 |
|
---|
| 65 | resp->destroy = true;
|
---|
| 66 | ui_test_ctl_destroy(testctl);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | static errno_t test_ctl_paint(void *arg)
|
---|
| 70 | {
|
---|
| 71 | ui_test_ctl_t *testctl = (ui_test_ctl_t *)arg;
|
---|
| 72 | ui_tc_resp_t *resp = testctl->resp;
|
---|
| 73 |
|
---|
| 74 | resp->paint = true;
|
---|
| 75 | return resp->rc;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static ui_evclaim_t test_ctl_kbd_event(void *arg, kbd_event_t *event)
|
---|
| 79 | {
|
---|
| 80 | ui_test_ctl_t *testctl = (ui_test_ctl_t *)arg;
|
---|
| 81 | ui_tc_resp_t *resp = testctl->resp;
|
---|
| 82 |
|
---|
| 83 | resp->kbd = true;
|
---|
| 84 | resp->kevent = *event;
|
---|
| 85 |
|
---|
| 86 | return resp->claim;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
| 90 | {
|
---|
| 91 | ui_test_ctl_t *testctl = (ui_test_ctl_t *)arg;
|
---|
| 92 | ui_tc_resp_t *resp = testctl->resp;
|
---|
| 93 |
|
---|
| 94 | resp->pos = true;
|
---|
| 95 | resp->pevent = *event;
|
---|
| 96 |
|
---|
| 97 | return resp->claim;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | static void test_ctl_unfocus(void *arg, unsigned nfocus)
|
---|
| 101 | {
|
---|
| 102 | ui_test_ctl_t *testctl = (ui_test_ctl_t *)arg;
|
---|
| 103 | ui_tc_resp_t *resp = testctl->resp;
|
---|
| 104 |
|
---|
| 105 | resp->unfocus = true;
|
---|
| 106 | resp->unfocus_nfocus = nfocus;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /** Create new test control.
|
---|
| 110 | *
|
---|
| 111 | * @param resp Response structure
|
---|
| 112 | * @param rtest Place to store pointer to new test control
|
---|
| 113 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 114 | */
|
---|
| 115 | errno_t ui_test_ctl_create(ui_tc_resp_t *resp, ui_test_ctl_t **rtest)
|
---|
| 116 | {
|
---|
| 117 | ui_test_ctl_t *test;
|
---|
| 118 | errno_t rc;
|
---|
| 119 |
|
---|
| 120 | test = calloc(1, sizeof(ui_test_ctl_t));
|
---|
| 121 | if (test == NULL)
|
---|
| 122 | return ENOMEM;
|
---|
| 123 |
|
---|
| 124 | rc = ui_control_new(&ui_test_ctl_ops, (void *)test, &test->control);
|
---|
| 125 | if (rc != EOK) {
|
---|
| 126 | free(test);
|
---|
| 127 | return rc;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | test->resp = resp;
|
---|
| 131 | *rtest = test;
|
---|
| 132 | return EOK;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /** Destroy test control.
|
---|
| 136 | *
|
---|
| 137 | * @param test Test control or @c NULL
|
---|
| 138 | */
|
---|
| 139 | void ui_test_ctl_destroy(ui_test_ctl_t *test)
|
---|
| 140 | {
|
---|
| 141 | if (test == NULL)
|
---|
| 142 | return;
|
---|
| 143 |
|
---|
| 144 | ui_control_delete(test->control);
|
---|
| 145 | free(test);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /** Get base control from test control.
|
---|
| 149 | *
|
---|
| 150 | * @param test Test control
|
---|
| 151 | * @return Control
|
---|
| 152 | */
|
---|
| 153 | ui_control_t *ui_test_ctl_ctl(ui_test_ctl_t *test)
|
---|
| 154 | {
|
---|
| 155 | return test->control;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /** @}
|
---|
| 159 | */
|
---|