source: mainline/uspace/srv/hid/display/test/client.c@ b43edabe

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b43edabe was f7fb2b21, checked in by Jiri Svoboda <jiri@…>, 6 years ago

Propagate position event to display clients

  • Property mode set to 100644
File size: 8.1 KB
Line 
1/*
2 * Copyright (c) 2019 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#include <disp_srv.h>
30#include <errno.h>
31#include <pcut/pcut.h>
32#include <stdio.h>
33#include <str.h>
34
35#include "../client.h"
36#include "../display.h"
37#include "../window.h"
38
39PCUT_INIT;
40
41PCUT_TEST_SUITE(client);
42
43static void test_ds_ev_pending(void *);
44
45static ds_client_cb_t test_ds_client_cb = {
46 .ev_pending = test_ds_ev_pending
47};
48
49static void test_ds_ev_pending(void *arg)
50{
51 bool *called_cb = (bool *) arg;
52 printf("test_ds_ev_pending\n");
53 *called_cb = true;
54
55}
56
57/** Client creation and destruction. */
58PCUT_TEST(client_create_destroy)
59{
60 ds_display_t *disp;
61 ds_client_t *client;
62 errno_t rc;
63
64 rc = ds_display_create(NULL, &disp);
65 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
66
67 rc = ds_client_create(disp, &test_ds_client_cb, NULL, &client);
68 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
69
70 ds_client_destroy(client);
71 ds_display_destroy(disp);
72}
73
74/** Test ds_client_find_window().
75 *
76 * ds_client_add_window() and ds_client_remove_window() are indirectly
77 * tested too as part of creating and destroying the window
78 */
79PCUT_TEST(client_find_window)
80{
81 ds_display_t *disp;
82 ds_client_t *client;
83 ds_window_t *w0;
84 ds_window_t *w1;
85 ds_window_t *wnd;
86 display_wnd_params_t params;
87 errno_t rc;
88
89 rc = ds_display_create(NULL, &disp);
90 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
91
92 rc = ds_client_create(disp, &test_ds_client_cb, NULL, &client);
93 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
94
95 display_wnd_params_init(&params);
96 params.rect.p0.x = params.rect.p0.y = 0;
97 params.rect.p1.x = params.rect.p1.y = 1;
98
99 rc = ds_window_create(client, &params, &w0);
100 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
101
102 rc = ds_window_create(client, &params, &w1);
103 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
104
105 wnd = ds_client_find_window(client, w0->id);
106 PCUT_ASSERT_EQUALS(w0, wnd);
107
108 wnd = ds_client_find_window(client, w1->id);
109 PCUT_ASSERT_EQUALS(w1, wnd);
110
111 wnd = ds_client_find_window(client, 0);
112 PCUT_ASSERT_NULL(wnd);
113
114 wnd = ds_client_find_window(client, w1->id + 1);
115 PCUT_ASSERT_NULL(wnd);
116
117 ds_window_destroy(w0);
118 ds_window_destroy(w1);
119 ds_client_destroy(client);
120 ds_display_destroy(disp);
121}
122
123/** Test ds_client_first_window() / ds_client_next_window. */
124PCUT_TEST(client_first_next_window)
125{
126 ds_display_t *disp;
127 ds_client_t *client;
128 ds_window_t *w0;
129 ds_window_t *w1;
130 ds_window_t *wnd;
131 display_wnd_params_t params;
132 errno_t rc;
133
134 rc = ds_display_create(NULL, &disp);
135 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
136
137 rc = ds_client_create(disp, &test_ds_client_cb, NULL, &client);
138 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
139
140 display_wnd_params_init(&params);
141 params.rect.p0.x = params.rect.p0.y = 0;
142 params.rect.p1.x = params.rect.p1.y = 1;
143
144 rc = ds_window_create(client, &params, &w0);
145 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
146
147 rc = ds_window_create(client, &params, &w1);
148 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
149
150 wnd = ds_client_first_window(client);
151 PCUT_ASSERT_EQUALS(w0, wnd);
152
153 wnd = ds_client_next_window(w0);
154 PCUT_ASSERT_EQUALS(w1, wnd);
155
156 wnd = ds_client_next_window(w1);
157 PCUT_ASSERT_NULL(wnd);
158
159 ds_window_destroy(w0);
160 ds_window_destroy(w1);
161 ds_client_destroy(client);
162 ds_display_destroy(disp);
163}
164
165/** Test ds_client_get_event(), ds_client_post_kbd_event(). */
166PCUT_TEST(client_get_post_kbd_event)
167{
168 ds_display_t *disp;
169 ds_client_t *client;
170 ds_window_t *wnd;
171 display_wnd_params_t params;
172 kbd_event_t event;
173 ds_window_t *rwindow;
174 display_wnd_ev_t revent;
175 bool called_cb = NULL;
176 errno_t rc;
177
178 rc = ds_display_create(NULL, &disp);
179 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
180
181 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
182 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
183
184 display_wnd_params_init(&params);
185 params.rect.p0.x = params.rect.p0.y = 0;
186 params.rect.p1.x = params.rect.p1.y = 1;
187
188 rc = ds_window_create(client, &params, &wnd);
189 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
190
191 event.type = KEY_PRESS;
192 event.key = KC_ENTER;
193 event.mods = 0;
194 event.c = L'\0';
195
196 PCUT_ASSERT_FALSE(called_cb);
197
198 rc = ds_client_get_event(client, &rwindow, &revent);
199 PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
200
201 rc = ds_client_post_kbd_event(client, wnd, &event);
202 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
203 PCUT_ASSERT_TRUE(called_cb);
204
205 rc = ds_client_get_event(client, &rwindow, &revent);
206 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
207 PCUT_ASSERT_EQUALS(wnd, rwindow);
208 PCUT_ASSERT_EQUALS(wev_kbd, revent.etype);
209 PCUT_ASSERT_EQUALS(event.type, revent.ev.kbd.type);
210 PCUT_ASSERT_EQUALS(event.key, revent.ev.kbd.key);
211 PCUT_ASSERT_EQUALS(event.mods, revent.ev.kbd.mods);
212 PCUT_ASSERT_EQUALS(event.c, revent.ev.kbd.c);
213
214 rc = ds_client_get_event(client, &rwindow, &revent);
215 PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
216
217 ds_window_destroy(wnd);
218 ds_client_destroy(client);
219 ds_display_destroy(disp);
220}
221
222/** Test ds_client_get_event(), ds_client_post_pos_event(). */
223PCUT_TEST(client_get_post_pos_event)
224{
225 ds_display_t *disp;
226 ds_client_t *client;
227 ds_window_t *wnd;
228 display_wnd_params_t params;
229 pos_event_t event;
230 ds_window_t *rwindow;
231 display_wnd_ev_t revent;
232 bool called_cb = NULL;
233 errno_t rc;
234
235 rc = ds_display_create(NULL, &disp);
236 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
237
238 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
239 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
240
241 display_wnd_params_init(&params);
242 params.rect.p0.x = params.rect.p0.y = 0;
243 params.rect.p1.x = params.rect.p1.y = 1;
244
245 rc = ds_window_create(client, &params, &wnd);
246 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
247
248 event.type = POS_PRESS;
249 event.hpos = 1;
250 event.vpos = 2;
251
252 PCUT_ASSERT_FALSE(called_cb);
253
254 rc = ds_client_get_event(client, &rwindow, &revent);
255 PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
256
257 rc = ds_client_post_pos_event(client, wnd, &event);
258 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
259 PCUT_ASSERT_TRUE(called_cb);
260
261 rc = ds_client_get_event(client, &rwindow, &revent);
262 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
263 PCUT_ASSERT_EQUALS(wnd, rwindow);
264 PCUT_ASSERT_EQUALS(wev_pos, revent.etype);
265 PCUT_ASSERT_EQUALS(event.type, revent.ev.pos.type);
266 PCUT_ASSERT_EQUALS(event.hpos, revent.ev.pos.hpos);
267 PCUT_ASSERT_EQUALS(event.vpos, revent.ev.pos.vpos);
268
269 rc = ds_client_get_event(client, &rwindow, &revent);
270 PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
271
272 ds_window_destroy(wnd);
273 ds_client_destroy(client);
274 ds_display_destroy(disp);
275}
276
277/** Test client being destroyed while still having a window.
278 *
279 * This can happen if client forgets to destroy window or if the client
280 * is disconnected (or terminated).
281 */
282PCUT_TEST(client_leftover_window)
283{
284 ds_display_t *disp;
285 ds_client_t *client;
286 ds_window_t *wnd;
287 display_wnd_params_t params;
288 errno_t rc;
289
290 rc = ds_display_create(NULL, &disp);
291 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
292
293 rc = ds_client_create(disp, NULL, NULL, &client);
294 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
295
296 display_wnd_params_init(&params);
297 params.rect.p0.x = params.rect.p0.y = 0;
298 params.rect.p1.x = params.rect.p1.y = 1;
299
300 rc = ds_window_create(client, &params, &wnd);
301 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
302
303 ds_client_destroy(client);
304 ds_display_destroy(disp);
305}
306
307PCUT_EXPORT(client);
Note: See TracBrowser for help on using the repository browser.