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

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

Propagate position event to display clients

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