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

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

Deliver window focus and unfocus events

  • Property mode set to 100644
File size: 10.5 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_focus_event(). */
166PCUT_TEST(client_get_post_focus_event)
167{
168 ds_display_t *disp;
169 ds_client_t *client;
170 ds_window_t *wnd;
171 display_wnd_params_t params;
172 ds_window_t *rwindow;
173 display_wnd_ev_t revent;
174 bool called_cb = NULL;
175 errno_t rc;
176
177 rc = ds_display_create(NULL, &disp);
178 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
179
180 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
181 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
182
183 display_wnd_params_init(&params);
184 params.rect.p0.x = params.rect.p0.y = 0;
185 params.rect.p1.x = params.rect.p1.y = 1;
186
187 rc = ds_window_create(client, &params, &wnd);
188 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
189
190 PCUT_ASSERT_FALSE(called_cb);
191
192 rc = ds_client_get_event(client, &rwindow, &revent);
193 PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
194
195 rc = ds_client_post_focus_event(client, wnd);
196 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
197 PCUT_ASSERT_TRUE(called_cb);
198
199 rc = ds_client_get_event(client, &rwindow, &revent);
200 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
201 PCUT_ASSERT_EQUALS(wnd, rwindow);
202 PCUT_ASSERT_EQUALS(wev_focus, revent.etype);
203
204 rc = ds_client_get_event(client, &rwindow, &revent);
205 PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
206
207 ds_window_destroy(wnd);
208 ds_client_destroy(client);
209 ds_display_destroy(disp);
210}
211
212/** Test ds_client_get_event(), ds_client_post_kbd_event(). */
213PCUT_TEST(client_get_post_kbd_event)
214{
215 ds_display_t *disp;
216 ds_client_t *client;
217 ds_window_t *wnd;
218 display_wnd_params_t params;
219 kbd_event_t event;
220 ds_window_t *rwindow;
221 display_wnd_ev_t revent;
222 bool called_cb = NULL;
223 errno_t rc;
224
225 rc = ds_display_create(NULL, &disp);
226 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
227
228 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
229 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
230
231 display_wnd_params_init(&params);
232 params.rect.p0.x = params.rect.p0.y = 0;
233 params.rect.p1.x = params.rect.p1.y = 1;
234
235 rc = ds_window_create(client, &params, &wnd);
236 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
237
238 event.type = KEY_PRESS;
239 event.key = KC_ENTER;
240 event.mods = 0;
241 event.c = L'\0';
242
243 PCUT_ASSERT_FALSE(called_cb);
244
245 rc = ds_client_get_event(client, &rwindow, &revent);
246 PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
247
248 rc = ds_client_post_kbd_event(client, wnd, &event);
249 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
250 PCUT_ASSERT_TRUE(called_cb);
251
252 rc = ds_client_get_event(client, &rwindow, &revent);
253 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
254 PCUT_ASSERT_EQUALS(wnd, rwindow);
255 PCUT_ASSERT_EQUALS(wev_kbd, revent.etype);
256 PCUT_ASSERT_EQUALS(event.type, revent.ev.kbd.type);
257 PCUT_ASSERT_EQUALS(event.key, revent.ev.kbd.key);
258 PCUT_ASSERT_EQUALS(event.mods, revent.ev.kbd.mods);
259 PCUT_ASSERT_EQUALS(event.c, revent.ev.kbd.c);
260
261 rc = ds_client_get_event(client, &rwindow, &revent);
262 PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
263
264 ds_window_destroy(wnd);
265 ds_client_destroy(client);
266 ds_display_destroy(disp);
267}
268
269/** Test ds_client_get_event(), ds_client_post_pos_event(). */
270PCUT_TEST(client_get_post_pos_event)
271{
272 ds_display_t *disp;
273 ds_client_t *client;
274 ds_window_t *wnd;
275 display_wnd_params_t params;
276 pos_event_t event;
277 ds_window_t *rwindow;
278 display_wnd_ev_t revent;
279 bool called_cb = NULL;
280 errno_t rc;
281
282 rc = ds_display_create(NULL, &disp);
283 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
284
285 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
286 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
287
288 display_wnd_params_init(&params);
289 params.rect.p0.x = params.rect.p0.y = 0;
290 params.rect.p1.x = params.rect.p1.y = 1;
291
292 rc = ds_window_create(client, &params, &wnd);
293 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
294
295 event.type = POS_PRESS;
296 event.hpos = 1;
297 event.vpos = 2;
298
299 PCUT_ASSERT_FALSE(called_cb);
300
301 rc = ds_client_get_event(client, &rwindow, &revent);
302 PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
303
304 rc = ds_client_post_pos_event(client, wnd, &event);
305 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
306 PCUT_ASSERT_TRUE(called_cb);
307
308 rc = ds_client_get_event(client, &rwindow, &revent);
309 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
310 PCUT_ASSERT_EQUALS(wnd, rwindow);
311 PCUT_ASSERT_EQUALS(wev_pos, revent.etype);
312 PCUT_ASSERT_EQUALS(event.type, revent.ev.pos.type);
313 PCUT_ASSERT_EQUALS(event.hpos, revent.ev.pos.hpos);
314 PCUT_ASSERT_EQUALS(event.vpos, revent.ev.pos.vpos);
315
316 rc = ds_client_get_event(client, &rwindow, &revent);
317 PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
318
319 ds_window_destroy(wnd);
320 ds_client_destroy(client);
321 ds_display_destroy(disp);
322}
323
324/** Test ds_client_get_event(), ds_client_post_unfocus_event(). */
325PCUT_TEST(client_get_post_unfocus_event)
326{
327 ds_display_t *disp;
328 ds_client_t *client;
329 ds_window_t *wnd;
330 display_wnd_params_t params;
331 ds_window_t *rwindow;
332 display_wnd_ev_t revent;
333 bool called_cb = NULL;
334 errno_t rc;
335
336 rc = ds_display_create(NULL, &disp);
337 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
338
339 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
340 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
341
342 display_wnd_params_init(&params);
343 params.rect.p0.x = params.rect.p0.y = 0;
344 params.rect.p1.x = params.rect.p1.y = 1;
345
346 rc = ds_window_create(client, &params, &wnd);
347 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
348
349 PCUT_ASSERT_FALSE(called_cb);
350
351 rc = ds_client_get_event(client, &rwindow, &revent);
352 PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
353
354 rc = ds_client_post_unfocus_event(client, wnd);
355 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
356 PCUT_ASSERT_TRUE(called_cb);
357
358 rc = ds_client_get_event(client, &rwindow, &revent);
359 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
360 PCUT_ASSERT_EQUALS(wnd, rwindow);
361 PCUT_ASSERT_EQUALS(wev_unfocus, revent.etype);
362
363 rc = ds_client_get_event(client, &rwindow, &revent);
364 PCUT_ASSERT_ERRNO_VAL(ENOENT, rc);
365
366 ds_window_destroy(wnd);
367 ds_client_destroy(client);
368 ds_display_destroy(disp);
369}
370
371/** Test client being destroyed while still having a window.
372 *
373 * This can happen if client forgets to destroy window or if the client
374 * is disconnected (or terminated).
375 */
376PCUT_TEST(client_leftover_window)
377{
378 ds_display_t *disp;
379 ds_client_t *client;
380 ds_window_t *wnd;
381 display_wnd_params_t params;
382 errno_t rc;
383
384 rc = ds_display_create(NULL, &disp);
385 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
386
387 rc = ds_client_create(disp, NULL, NULL, &client);
388 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
389
390 display_wnd_params_init(&params);
391 params.rect.p0.x = params.rect.p0.y = 0;
392 params.rect.p1.x = params.rect.p1.y = 1;
393
394 rc = ds_window_create(client, &params, &wnd);
395 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
396
397 ds_client_destroy(client);
398 ds_display_destroy(disp);
399}
400
401PCUT_EXPORT(client);
Note: See TracBrowser for help on using the repository browser.