2 * Copyright (c) 2023 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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>
37 #include <types/ui/event.h>
38 #include "../private/testctl.h"
42 PCUT_TEST_SUITE(control
);
44 /** Allocate and deallocate control */
47 ui_control_t
*control
= NULL
;
50 rc
= ui_control_new(&ui_test_ctl_ops
, NULL
, &control
);
51 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
52 PCUT_ASSERT_NOT_NULL(control
);
54 ui_control_delete(control
);
57 /** ui_control_delete() can take NULL argument (no-op) */
58 PCUT_TEST(delete_null
)
60 ui_control_delete(NULL
);
63 /** Test sending destroy request to control */
66 ui_test_ctl_t
*testctl
= NULL
;
70 rc
= ui_test_ctl_create(&resp
, &testctl
);
71 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
72 PCUT_ASSERT_NOT_NULL(testctl
);
77 ui_control_destroy(ui_test_ctl_ctl(testctl
));
78 PCUT_ASSERT_TRUE(resp
.destroy
);
81 /** Test sending paint request to control */
84 ui_test_ctl_t
*testctl
= NULL
;
88 rc
= ui_test_ctl_create(&resp
, &testctl
);
89 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
90 PCUT_ASSERT_NOT_NULL(testctl
);
95 rc
= ui_control_paint(ui_test_ctl_ctl(testctl
));
96 PCUT_ASSERT_ERRNO_VAL(resp
.rc
, rc
);
97 PCUT_ASSERT_TRUE(resp
.paint
);
102 rc
= ui_control_paint(ui_test_ctl_ctl(testctl
));
103 PCUT_ASSERT_ERRNO_VAL(resp
.rc
, rc
);
104 PCUT_ASSERT_TRUE(resp
.paint
);
106 ui_test_ctl_destroy(testctl
);
109 /** Test sending keyboard event to control */
112 ui_test_ctl_t
*testctl
= NULL
;
118 rc
= ui_test_ctl_create(&resp
, &testctl
);
119 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
120 PCUT_ASSERT_NOT_NULL(testctl
);
122 resp
.claim
= ui_claimed
;
124 event
.type
= KEY_PRESS
;
126 event
.mods
= KM_LSHIFT
;
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
);
137 ui_test_ctl_destroy(testctl
);
140 /** Test sending position event to control */
143 ui_test_ctl_t
*testctl
= NULL
;
149 rc
= ui_test_ctl_create(&resp
, &testctl
);
150 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
151 PCUT_ASSERT_NOT_NULL(testctl
);
153 resp
.claim
= ui_claimed
;
156 event
.type
= POS_PRESS
;
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
);
170 ui_test_ctl_destroy(testctl
);
173 /** Test sending unfocus to control */
176 ui_test_ctl_t
*testctl
= NULL
;
180 rc
= ui_test_ctl_create(&resp
, &testctl
);
181 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
182 PCUT_ASSERT_NOT_NULL(testctl
);
184 resp
.unfocus
= false;
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
);
190 ui_test_ctl_destroy(testctl
);
193 PCUT_EXPORT(control
);