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

Last change on this file since b4b4dafe was b4b4dafe, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Popup windows event delivery is special

Popup windows don't get focus, yet they still receive 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 <str.h>
33
34#include "../client.h"
35#include "../display.h"
36#include "../seat.h"
37#include "../window.h"
38
39PCUT_INIT;
40
41PCUT_TEST_SUITE(display);
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 *called_cb = true;
53}
54
55/** Display creation and destruction. */
56PCUT_TEST(display_create_destroy)
57{
58 ds_display_t *disp;
59 errno_t rc;
60
61 rc = ds_display_create(NULL, df_none, &disp);
62 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
63
64 ds_display_destroy(disp);
65}
66
67/** Basic client operation. */
68PCUT_TEST(display_client)
69{
70 ds_display_t *disp;
71 ds_client_t *client;
72 ds_client_t *c0, *c1;
73 errno_t rc;
74
75 rc = ds_display_create(NULL, df_none, &disp);
76 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
77
78 rc = ds_client_create(disp, &test_ds_client_cb, NULL, &client);
79 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
80
81 c0 = ds_display_first_client(disp);
82 PCUT_ASSERT_EQUALS(c0, client);
83
84 c1 = ds_display_next_client(c0);
85 PCUT_ASSERT_NULL(c1);
86
87 ds_client_destroy(client);
88 ds_display_destroy(disp);
89}
90
91/** Test ds_display_find_window(). */
92PCUT_TEST(display_find_window)
93{
94 ds_display_t *disp;
95 ds_client_t *client;
96 ds_seat_t *seat;
97 ds_window_t *w0;
98 ds_window_t *w1;
99 ds_window_t *wnd;
100 display_wnd_params_t params;
101 bool called_cb = false;
102 errno_t rc;
103
104 rc = ds_display_create(NULL, df_none, &disp);
105 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
106
107 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
108 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
109
110 rc = ds_seat_create(disp, &seat);
111 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
112
113 display_wnd_params_init(&params);
114 params.rect.p0.x = params.rect.p0.y = 0;
115 params.rect.p1.x = params.rect.p1.y = 1;
116
117 rc = ds_window_create(client, &params, &w1);
118 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
119
120 rc = ds_window_create(client, &params, &w0);
121 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
122
123 wnd = ds_display_first_window(disp);
124 PCUT_ASSERT_EQUALS(w0, wnd);
125
126 wnd = ds_display_next_window(wnd);
127 PCUT_ASSERT_EQUALS(w1, wnd);
128
129 wnd = ds_display_next_window(wnd);
130 PCUT_ASSERT_NULL(wnd);
131
132 wnd = ds_display_last_window(disp);
133 PCUT_ASSERT_EQUALS(w1, wnd);
134
135 wnd = ds_display_prev_window(wnd);
136 PCUT_ASSERT_EQUALS(w0, wnd);
137
138 wnd = ds_display_prev_window(wnd);
139 PCUT_ASSERT_NULL(wnd);
140
141 wnd = ds_display_find_window(disp, w0->id);
142 PCUT_ASSERT_EQUALS(w0, wnd);
143
144 wnd = ds_display_find_window(disp, w1->id);
145 PCUT_ASSERT_EQUALS(w1, wnd);
146
147 wnd = ds_display_find_window(disp, 0);
148 PCUT_ASSERT_NULL(wnd);
149
150 wnd = ds_display_find_window(disp, w0->id + 1);
151 PCUT_ASSERT_NULL(wnd);
152
153 ds_window_destroy(w0);
154 ds_window_destroy(w1);
155 ds_seat_destroy(seat);
156 ds_client_destroy(client);
157 ds_display_destroy(disp);
158}
159
160/** Test ds_display_window_by_pos(). */
161PCUT_TEST(display_window_by_pos)
162{
163 ds_display_t *disp;
164 ds_client_t *client;
165 ds_seat_t *seat;
166 ds_window_t *w0;
167 ds_window_t *w1;
168 ds_window_t *wnd;
169 display_wnd_params_t params;
170 gfx_coord2_t pos;
171 bool called_cb = false;
172 errno_t rc;
173
174 rc = ds_display_create(NULL, df_none, &disp);
175 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
176
177 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
178 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
179
180 rc = ds_seat_create(disp, &seat);
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 = 100;
186
187 rc = ds_window_create(client, &params, &w0);
188 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
189
190 rc = ds_window_create(client, &params, &w1);
191 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
192
193 w0->dpos.x = 10;
194 w0->dpos.y = 10;
195
196 w1->dpos.x = 400;
197 w1->dpos.y = 400;
198
199 pos.x = 10;
200 pos.y = 10;
201 wnd = ds_display_window_by_pos(disp, &pos);
202 PCUT_ASSERT_EQUALS(w0, wnd);
203
204 pos.x = 400;
205 pos.y = 400;
206 wnd = ds_display_window_by_pos(disp, &pos);
207 PCUT_ASSERT_EQUALS(w1, wnd);
208
209 ds_window_destroy(w0);
210 ds_window_destroy(w1);
211 ds_seat_destroy(seat);
212 ds_client_destroy(client);
213 ds_display_destroy(disp);
214}
215
216/** Basic seat operation. */
217PCUT_TEST(display_seat)
218{
219 ds_display_t *disp;
220 ds_seat_t *seat;
221 ds_seat_t *s0, *s1;
222 errno_t rc;
223
224 rc = ds_display_create(NULL, df_none, &disp);
225 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
226
227 rc = ds_seat_create(disp, &seat);
228 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
229
230 s0 = ds_display_first_seat(disp);
231 PCUT_ASSERT_EQUALS(s0, seat);
232
233 s1 = ds_display_next_seat(s0);
234 PCUT_ASSERT_NULL(s1);
235
236 ds_seat_destroy(seat);
237 ds_display_destroy(disp);
238}
239
240/** Test ds_display_post_kbd_event() delivers event to client callback.
241 */
242PCUT_TEST(display_post_kbd_event)
243{
244 ds_display_t *disp;
245 ds_seat_t *seat;
246 ds_client_t *client;
247 ds_window_t *wnd;
248 display_wnd_params_t params;
249 kbd_event_t event;
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_seat_create(disp, &seat);
257 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
258
259 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
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 = 1;
265
266 rc = ds_window_create(client, &params, &wnd);
267 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
268
269 ds_seat_set_focus(seat, wnd);
270
271 event.type = KEY_PRESS;
272 event.key = KC_ENTER;
273 event.mods = 0;
274 event.c = L'\0';
275
276 called_cb = false;
277
278 rc = ds_display_post_kbd_event(disp, &event);
279 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
280 PCUT_ASSERT_TRUE(called_cb);
281
282 ds_window_destroy(wnd);
283 ds_client_destroy(client);
284 ds_seat_destroy(seat);
285 ds_display_destroy(disp);
286}
287
288/** Test ds_display_post_kbd_event() with Alt-Tab switches focus.
289 */
290PCUT_TEST(display_post_kbd_event_alt_tab)
291{
292 ds_display_t *disp;
293 ds_seat_t *seat;
294 ds_client_t *client;
295 ds_window_t *w0, *w1;
296 display_wnd_params_t params;
297 kbd_event_t event;
298 bool called_cb = false;
299 errno_t rc;
300
301 rc = ds_display_create(NULL, df_none, &disp);
302 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
303
304 rc = ds_seat_create(disp, &seat);
305 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
306
307 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
308 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
309
310 display_wnd_params_init(&params);
311 params.rect.p0.x = params.rect.p0.y = 0;
312 params.rect.p1.x = params.rect.p1.y = 1;
313
314 rc = ds_window_create(client, &params, &w0);
315 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
316
317 rc = ds_window_create(client, &params, &w1);
318 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
319
320 ds_seat_set_focus(seat, w0);
321
322 event.type = KEY_PRESS;
323 event.key = KC_TAB;
324 event.mods = KM_ALT;
325 event.c = L'\0';
326
327 called_cb = false;
328
329 rc = ds_display_post_kbd_event(disp, &event);
330 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
331
332 /* Got gocus/unfocus events */
333 PCUT_ASSERT_TRUE(called_cb);
334
335 /* Next window should be focused */
336 PCUT_ASSERT_EQUALS(w1, seat->focus);
337
338 called_cb = false;
339
340 rc = ds_display_post_kbd_event(disp, &event);
341 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
342
343 /* Got gocus/unfocus events */
344 PCUT_ASSERT_TRUE(called_cb);
345
346 /* Focus should be back to the first window */
347 PCUT_ASSERT_EQUALS(w0, seat->focus);
348
349 ds_window_destroy(w0);
350 ds_window_destroy(w1);
351 ds_client_destroy(client);
352 ds_seat_destroy(seat);
353 ds_display_destroy(disp);
354}
355
356/** Test ds_display_post_ptd_event() with click on window switches focus
357 */
358PCUT_TEST(display_post_ptd_event_wnd_switch)
359{
360 ds_display_t *disp;
361 ds_seat_t *seat;
362 ds_client_t *client;
363 ds_window_t *w0, *w1;
364 display_wnd_params_t params;
365 ptd_event_t event;
366 bool called_cb = false;
367 errno_t rc;
368
369 rc = ds_display_create(NULL, df_none, &disp);
370 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
371
372 rc = ds_seat_create(disp, &seat);
373 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
374
375 rc = ds_client_create(disp, &test_ds_client_cb, &called_cb, &client);
376 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
377
378 /*
379 * For PTD_MOVE to work we need to set display dimensions (as pointer
380 * move is clipped to the display rectangle. Here we do it directly
381 * instead of adding a display device.
382 */
383 disp->rect.p0.x = 0;
384 disp->rect.p0.y = 0;
385 disp->rect.p1.x = 500;
386 disp->rect.p1.y = 500;
387
388 display_wnd_params_init(&params);
389 params.rect.p0.x = params.rect.p0.y = 0;
390 params.rect.p1.x = params.rect.p1.y = 1;
391
392 rc = ds_window_create(client, &params, &w0);
393 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
394
395 rc = ds_window_create(client, &params, &w1);
396 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
397
398 w0->dpos.x = 10;
399 w0->dpos.y = 10;
400
401 w1->dpos.x = 400;
402 w1->dpos.y = 400;
403
404 ds_seat_set_focus(seat, w0);
405
406 event.type = PTD_MOVE;
407 event.dmove.x = 400;
408 event.dmove.y = 400;
409 rc = ds_display_post_ptd_event(disp, &event);
410 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
411
412 event.type = PTD_PRESS;
413 event.btn_num = 1;
414 rc = ds_display_post_ptd_event(disp, &event);
415 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
416
417 PCUT_ASSERT_EQUALS(w1, seat->focus);
418
419 event.type = PTD_RELEASE;
420 event.btn_num = 1;
421 rc = ds_display_post_ptd_event(disp, &event);
422 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
423
424 event.type = PTD_MOVE;
425 event.dmove.x = -400 + 10;
426 event.dmove.y = -400 + 10;
427 rc = ds_display_post_ptd_event(disp, &event);
428 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
429
430 event.type = PTD_PRESS;
431 event.btn_num = 1;
432 rc = ds_display_post_ptd_event(disp, &event);
433 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
434
435 PCUT_ASSERT_EQUALS(w0, seat->focus);
436
437 ds_window_destroy(w0);
438 ds_window_destroy(w1);
439 ds_client_destroy(client);
440 ds_seat_destroy(seat);
441 ds_display_destroy(disp);
442}
443
444PCUT_EXPORT(display);
Note: See TracBrowser for help on using the repository browser.