source: mainline/uspace/srv/hid/display/test/display.c@ 3a6d44b7

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3a6d44b7 was 913add60, checked in by Jiri Svoboda <jiri@…>, 3 years ago

Deliver WM events for windows being added and removed

  • Property mode set to 100644
File size: 12.3 KB
Line 
1/*
2 * Copyright (c) 2022 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 <str.h>
33
34#include "../client.h"
35#include "../display.h"
36#include "../seat.h"
37#include "../window.h"
38#include "../wmclient.h"
39
40PCUT_INIT;
41
42PCUT_TEST_SUITE(display);
43
44static void test_ds_ev_pending(void *);
45
46static ds_client_cb_t test_ds_client_cb = {
47 .ev_pending = test_ds_ev_pending
48};
49
50static void test_ds_wmev_pending(void *);
51
52static ds_wmclient_cb_t test_ds_wmclient_cb = {
53 .ev_pending = test_ds_wmev_pending
54};
55
56static void test_ds_ev_pending(void *arg)
57{
58 bool *called_cb = (bool *) arg;
59 *called_cb = true;
60}
61
62static void test_ds_wmev_pending(void *arg)
63{
64 bool *called_cb = (bool *) arg;
65 *called_cb = true;
66}
67
68/** Display creation and destruction. */
69PCUT_TEST(display_create_destroy)
70{
71 ds_display_t *disp;
72 errno_t rc;
73
74 rc = ds_display_create(NULL, df_none, &disp);
75 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
76
77 ds_display_destroy(disp);
78}
79
80/** Basic client operation. */
81PCUT_TEST(display_client)
82{
83 ds_display_t *disp;
84 ds_client_t *client;
85 ds_client_t *c0, *c1;
86 errno_t rc;
87
88 rc = ds_display_create(NULL, df_none, &disp);
89 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
90
91 rc = ds_client_create(disp, &test_ds_client_cb, NULL, &client);
92 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
93
94 c0 = ds_display_first_client(disp);
95 PCUT_ASSERT_EQUALS(c0, client);
96
97 c1 = ds_display_next_client(c0);
98 PCUT_ASSERT_NULL(c1);
99
100 ds_client_destroy(client);
101 ds_display_destroy(disp);
102}
103
104/** Basic WM client operation. */
105PCUT_TEST(display_wmclient)
106{
107 ds_display_t *disp;
108 ds_wmclient_t *wmclient;
109 ds_wmclient_t *c0, *c1;
110 errno_t rc;
111
112 rc = ds_display_create(NULL, df_none, &disp);
113 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
114
115 rc = ds_wmclient_create(disp, &test_ds_wmclient_cb, NULL, &wmclient);
116 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
117
118 c0 = ds_display_first_wmclient(disp);
119 PCUT_ASSERT_EQUALS(c0, wmclient);
120
121 c1 = ds_display_next_wmclient(c0);
122 PCUT_ASSERT_NULL(c1);
123
124 ds_wmclient_destroy(wmclient);
125 ds_display_destroy(disp);
126}
127
128/** Test ds_display_find_window(). */
129PCUT_TEST(display_find_window)
130{
131 ds_display_t *disp;
132 ds_client_t *client;
133 ds_seat_t *seat;
134 ds_window_t *w0;
135 ds_window_t *w1;
136 ds_window_t *wnd;
137 display_wnd_params_t params;
138 bool called_cb = false;
139 errno_t rc;
140
141 rc = ds_display_create(NULL, df_none, &disp);
142 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
143
144 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
145 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
146
147 rc = ds_seat_create(disp, &seat);
148 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
149
150 display_wnd_params_init(&params);
151 params.rect.p0.x = params.rect.p0.y = 0;
152 params.rect.p1.x = params.rect.p1.y = 1;
153
154 rc = ds_window_create(client, &params, &w1);
155 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
156
157 rc = ds_window_create(client, &params, &w0);
158 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
159
160 wnd = ds_display_first_window(disp);
161 PCUT_ASSERT_EQUALS(w0, wnd);
162
163 wnd = ds_display_next_window(wnd);
164 PCUT_ASSERT_EQUALS(w1, wnd);
165
166 wnd = ds_display_next_window(wnd);
167 PCUT_ASSERT_NULL(wnd);
168
169 wnd = ds_display_last_window(disp);
170 PCUT_ASSERT_EQUALS(w1, wnd);
171
172 wnd = ds_display_prev_window(wnd);
173 PCUT_ASSERT_EQUALS(w0, wnd);
174
175 wnd = ds_display_prev_window(wnd);
176 PCUT_ASSERT_NULL(wnd);
177
178 wnd = ds_display_find_window(disp, w0->id);
179 PCUT_ASSERT_EQUALS(w0, wnd);
180
181 wnd = ds_display_find_window(disp, w1->id);
182 PCUT_ASSERT_EQUALS(w1, wnd);
183
184 wnd = ds_display_find_window(disp, 0);
185 PCUT_ASSERT_NULL(wnd);
186
187 wnd = ds_display_find_window(disp, w0->id + 1);
188 PCUT_ASSERT_NULL(wnd);
189
190 ds_window_destroy(w0);
191 ds_window_destroy(w1);
192 ds_seat_destroy(seat);
193 ds_client_destroy(client);
194 ds_display_destroy(disp);
195}
196
197/** Test ds_display_window_to_top() */
198PCUT_TEST(display_window_to_top)
199{
200 ds_display_t *disp;
201 ds_client_t *client;
202 ds_seat_t *seat;
203 ds_window_t *w0;
204 ds_window_t *w1;
205 display_wnd_params_t params;
206 bool called_cb = false;
207 errno_t rc;
208
209 rc = ds_display_create(NULL, df_none, &disp);
210 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
211
212 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
213 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
214
215 rc = ds_seat_create(disp, &seat);
216 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
217
218 display_wnd_params_init(&params);
219 params.rect.p0.x = params.rect.p0.y = 0;
220 params.rect.p1.x = params.rect.p1.y = 100;
221
222 rc = ds_window_create(client, &params, &w0);
223 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
224
225 rc = ds_window_create(client, &params, &w1);
226 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
227
228 PCUT_ASSERT_EQUALS(w1, ds_display_first_window(disp));
229 ds_display_window_to_top(w0);
230 PCUT_ASSERT_EQUALS(w0, ds_display_first_window(disp));
231
232 ds_window_destroy(w0);
233 ds_window_destroy(w1);
234 ds_seat_destroy(seat);
235 ds_client_destroy(client);
236 ds_display_destroy(disp);
237}
238
239/** Test ds_display_window_by_pos(). */
240PCUT_TEST(display_window_by_pos)
241{
242 ds_display_t *disp;
243 ds_client_t *client;
244 ds_seat_t *seat;
245 ds_window_t *w0;
246 ds_window_t *w1;
247 ds_window_t *wnd;
248 display_wnd_params_t params;
249 gfx_coord2_t pos;
250 bool called_cb = false;
251 errno_t rc;
252
253 rc = ds_display_create(NULL, df_none, &disp);
254 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
255
256 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
257 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
258
259 rc = ds_seat_create(disp, &seat);
260 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
261
262 display_wnd_params_init(&params);
263 params.rect.p0.x = params.rect.p0.y = 0;
264 params.rect.p1.x = params.rect.p1.y = 100;
265
266 rc = ds_window_create(client, &params, &w0);
267 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
268
269 rc = ds_window_create(client, &params, &w1);
270 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
271
272 w0->dpos.x = 10;
273 w0->dpos.y = 10;
274
275 w1->dpos.x = 400;
276 w1->dpos.y = 400;
277
278 pos.x = 10;
279 pos.y = 10;
280 wnd = ds_display_window_by_pos(disp, &pos);
281 PCUT_ASSERT_EQUALS(w0, wnd);
282
283 pos.x = 400;
284 pos.y = 400;
285 wnd = ds_display_window_by_pos(disp, &pos);
286 PCUT_ASSERT_EQUALS(w1, wnd);
287
288 ds_window_destroy(w0);
289 ds_window_destroy(w1);
290 ds_seat_destroy(seat);
291 ds_client_destroy(client);
292 ds_display_destroy(disp);
293}
294
295/** Basic seat operation. */
296PCUT_TEST(display_seat)
297{
298 ds_display_t *disp;
299 ds_seat_t *seat;
300 ds_seat_t *s0, *s1;
301 errno_t rc;
302
303 rc = ds_display_create(NULL, df_none, &disp);
304 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
305
306 rc = ds_seat_create(disp, &seat);
307 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
308
309 s0 = ds_display_first_seat(disp);
310 PCUT_ASSERT_EQUALS(s0, seat);
311
312 s1 = ds_display_next_seat(s0);
313 PCUT_ASSERT_NULL(s1);
314
315 ds_seat_destroy(seat);
316 ds_display_destroy(disp);
317}
318
319/** Test ds_display_post_kbd_event() delivers event to client callback.
320 */
321PCUT_TEST(display_post_kbd_event)
322{
323 ds_display_t *disp;
324 ds_seat_t *seat;
325 ds_client_t *client;
326 ds_window_t *wnd;
327 display_wnd_params_t params;
328 kbd_event_t event;
329 bool called_cb = false;
330 errno_t rc;
331
332 rc = ds_display_create(NULL, df_none, &disp);
333 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
334
335 rc = ds_seat_create(disp, &seat);
336 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
337
338 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
339 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
340
341 display_wnd_params_init(&params);
342 params.rect.p0.x = params.rect.p0.y = 0;
343 params.rect.p1.x = params.rect.p1.y = 1;
344
345 rc = ds_window_create(client, &params, &wnd);
346 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
347
348 ds_seat_set_focus(seat, wnd);
349
350 event.type = KEY_PRESS;
351 event.key = KC_ENTER;
352 event.mods = 0;
353 event.c = L'\0';
354
355 called_cb = false;
356
357 rc = ds_display_post_kbd_event(disp, &event);
358 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
359 PCUT_ASSERT_TRUE(called_cb);
360
361 ds_window_destroy(wnd);
362 ds_client_destroy(client);
363 ds_seat_destroy(seat);
364 ds_display_destroy(disp);
365}
366
367/** Test ds_display_post_kbd_event() with Alt-Tab switches focus.
368 */
369PCUT_TEST(display_post_kbd_event_alt_tab)
370{
371 ds_display_t *disp;
372 ds_seat_t *seat;
373 ds_client_t *client;
374 ds_window_t *w0, *w1;
375 display_wnd_params_t params;
376 kbd_event_t event;
377 bool called_cb = false;
378 errno_t rc;
379
380 rc = ds_display_create(NULL, df_none, &disp);
381 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
382
383 rc = ds_seat_create(disp, &seat);
384 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
385
386 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
387 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
388
389 display_wnd_params_init(&params);
390 params.rect.p0.x = params.rect.p0.y = 0;
391 params.rect.p1.x = params.rect.p1.y = 1;
392
393 rc = ds_window_create(client, &params, &w0);
394 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
395
396 rc = ds_window_create(client, &params, &w1);
397 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
398
399 ds_seat_set_focus(seat, w0);
400
401 event.type = KEY_PRESS;
402 event.key = KC_TAB;
403 event.mods = KM_ALT;
404 event.c = L'\0';
405
406 called_cb = false;
407
408 rc = ds_display_post_kbd_event(disp, &event);
409 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
410
411 /* Got gocus/unfocus events */
412 PCUT_ASSERT_TRUE(called_cb);
413
414 /* Next window should be focused */
415 PCUT_ASSERT_EQUALS(w1, seat->focus);
416
417 called_cb = false;
418
419 rc = ds_display_post_kbd_event(disp, &event);
420 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
421
422 /* Got gocus/unfocus events */
423 PCUT_ASSERT_TRUE(called_cb);
424
425 /* Focus should be back to the first window */
426 PCUT_ASSERT_EQUALS(w0, seat->focus);
427
428 ds_window_destroy(w0);
429 ds_window_destroy(w1);
430 ds_client_destroy(client);
431 ds_seat_destroy(seat);
432 ds_display_destroy(disp);
433}
434
435/** Test ds_display_post_ptd_event() with click on window switches focus
436 */
437PCUT_TEST(display_post_ptd_event_wnd_switch)
438{
439 ds_display_t *disp;
440 ds_seat_t *seat;
441 ds_client_t *client;
442 ds_window_t *w0, *w1;
443 display_wnd_params_t params;
444 ptd_event_t event;
445 bool called_cb = false;
446 errno_t rc;
447
448 rc = ds_display_create(NULL, df_none, &disp);
449 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
450
451 rc = ds_seat_create(disp, &seat);
452 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
453
454 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
455 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
456
457 /*
458 * For PTD_MOVE to work we need to set display dimensions (as pointer
459 * move is clipped to the display rectangle. Here we do it directly
460 * instead of adding a display device.
461 */
462 disp->rect.p0.x = 0;
463 disp->rect.p0.y = 0;
464 disp->rect.p1.x = 500;
465 disp->rect.p1.y = 500;
466
467 display_wnd_params_init(&params);
468 params.rect.p0.x = params.rect.p0.y = 0;
469 params.rect.p1.x = params.rect.p1.y = 1;
470
471 rc = ds_window_create(client, &params, &w0);
472 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
473
474 rc = ds_window_create(client, &params, &w1);
475 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
476
477 w0->dpos.x = 10;
478 w0->dpos.y = 10;
479
480 w1->dpos.x = 400;
481 w1->dpos.y = 400;
482
483 ds_seat_set_focus(seat, w0);
484
485 event.type = PTD_MOVE;
486 event.dmove.x = 400;
487 event.dmove.y = 400;
488 rc = ds_display_post_ptd_event(disp, &event);
489 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
490
491 event.type = PTD_PRESS;
492 event.btn_num = 1;
493 rc = ds_display_post_ptd_event(disp, &event);
494 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
495
496 PCUT_ASSERT_EQUALS(w1, seat->focus);
497
498 event.type = PTD_RELEASE;
499 event.btn_num = 1;
500 rc = ds_display_post_ptd_event(disp, &event);
501 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
502
503 event.type = PTD_MOVE;
504 event.dmove.x = -400 + 10;
505 event.dmove.y = -400 + 10;
506 rc = ds_display_post_ptd_event(disp, &event);
507 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
508
509 event.type = PTD_PRESS;
510 event.btn_num = 1;
511 rc = ds_display_post_ptd_event(disp, &event);
512 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
513
514 PCUT_ASSERT_EQUALS(w0, seat->focus);
515
516 ds_window_destroy(w0);
517 ds_window_destroy(w1);
518 ds_client_destroy(client);
519 ds_seat_destroy(seat);
520 ds_display_destroy(disp);
521}
522
523PCUT_EXPORT(display);
Note: See TracBrowser for help on using the repository browser.