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

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

Movement events from input server, display pointer, focus by click again

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