source: mainline/uspace/lib/tbarcfg/test/tbarcfg.c@ ee3b28a9

Last change on this file since ee3b28a9 was ee3b28a9, checked in by Jiri Svoboda <jiri@…>, 16 months ago

Notify taskbar when start menu changes

  • Property mode set to 100644
File size: 13.7 KB
Line 
1/*
2 * Copyright (c) 2024 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 <tbarcfg/tbarcfg.h>
32#include <stdbool.h>
33#include <stdio.h>
34
35PCUT_INIT;
36
37PCUT_TEST_SUITE(tbarcfg);
38
39typedef struct {
40 bool notified;
41} tbarcfg_test_resp_t;
42
43static void test_cb(void *);
44
45/** Creating, opening and closing taskbar configuration */
46PCUT_TEST(create_open_close)
47{
48 errno_t rc;
49 tbarcfg_t *tbcfg;
50 char fname[L_tmpnam], *p;
51
52 p = tmpnam(fname);
53 PCUT_ASSERT_NOT_NULL(p);
54
55 /* Create new repository */
56 rc = tbarcfg_create(fname, &tbcfg);
57 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
58
59 tbarcfg_close(tbcfg);
60
61 /* Re-open the repository */
62
63 rc = tbarcfg_open(fname, &tbcfg);
64 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
65
66 tbarcfg_close(tbcfg);
67 remove(fname);
68}
69
70/** Iterating over start menu entries */
71PCUT_TEST(first_next)
72{
73 errno_t rc;
74 tbarcfg_t *tbcfg;
75 char fname[L_tmpnam], *p;
76 smenu_entry_t *e1 = NULL, *e2 = NULL;
77 smenu_entry_t *e;
78
79 p = tmpnam(fname);
80 PCUT_ASSERT_NOT_NULL(p);
81
82 rc = tbarcfg_create(fname, &tbcfg);
83 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
84
85 rc = smenu_entry_create(tbcfg, "A", "a", false, &e1);
86 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
87 PCUT_ASSERT_NOT_NULL(e1);
88
89 rc = smenu_entry_create(tbcfg, "B", "b", false, &e2);
90 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
91 PCUT_ASSERT_NOT_NULL(e2);
92
93 /* Create entry without getting a pointer to it */
94 rc = smenu_entry_create(tbcfg, "C", "c", false, NULL);
95 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
96
97 e = tbarcfg_smenu_first(tbcfg);
98 PCUT_ASSERT_EQUALS(e1, e);
99 e = tbarcfg_smenu_next(e);
100 PCUT_ASSERT_EQUALS(e2, e);
101 e = tbarcfg_smenu_next(e);
102 PCUT_ASSERT_NOT_NULL(e);
103 e = tbarcfg_smenu_next(e);
104 PCUT_ASSERT_NULL(e);
105
106 tbarcfg_close(tbcfg);
107 remove(fname);
108}
109
110/** Iterating over start menu entries backwards */
111PCUT_TEST(last_prev)
112{
113 errno_t rc;
114 tbarcfg_t *tbcfg;
115 char fname[L_tmpnam], *p;
116 smenu_entry_t *e1 = NULL, *e2 = NULL;
117 smenu_entry_t *e;
118
119 p = tmpnam(fname);
120 PCUT_ASSERT_NOT_NULL(p);
121
122 rc = tbarcfg_create(fname, &tbcfg);
123 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
124
125 rc = smenu_entry_create(tbcfg, "A", "a", false, &e1);
126 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
127 PCUT_ASSERT_NOT_NULL(e1);
128
129 rc = smenu_entry_create(tbcfg, "B", "b", false, &e2);
130 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
131 PCUT_ASSERT_NOT_NULL(e2);
132
133 e = tbarcfg_smenu_last(tbcfg);
134 PCUT_ASSERT_EQUALS(e2, e);
135 e = tbarcfg_smenu_prev(e);
136 PCUT_ASSERT_EQUALS(e1, e);
137 e = tbarcfg_smenu_prev(e);
138 PCUT_ASSERT_NULL(e);
139
140 tbarcfg_close(tbcfg);
141 remove(fname);
142}
143
144/** Separator entry */
145PCUT_TEST(separator)
146{
147 errno_t rc;
148 tbarcfg_t *tbcfg;
149 char fname[L_tmpnam], *p;
150 const char *caption;
151 const char *cmd;
152 smenu_entry_t *e1 = NULL, *e2 = NULL;
153 smenu_entry_t *e;
154
155 p = tmpnam(fname);
156 PCUT_ASSERT_NOT_NULL(p);
157
158 rc = tbarcfg_create(fname, &tbcfg);
159 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
160
161 rc = smenu_entry_create(tbcfg, "A", "a", false, &e1);
162 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
163 PCUT_ASSERT_NOT_NULL(e1);
164
165 rc = smenu_entry_sep_create(tbcfg, &e2);
166 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
167 PCUT_ASSERT_NOT_NULL(e2);
168
169 PCUT_ASSERT_FALSE(smenu_entry_get_separator(e1));
170 PCUT_ASSERT_TRUE(smenu_entry_get_separator(e2));
171
172 tbarcfg_close(tbcfg);
173
174 /* Re-open repository */
175
176 rc = tbarcfg_open(fname, &tbcfg);
177 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
178
179 e = tbarcfg_smenu_first(tbcfg);
180 PCUT_ASSERT_NOT_NULL(e);
181
182 /* Check that new values of properties have persisted */
183 PCUT_ASSERT_FALSE(smenu_entry_get_separator(e));
184 caption = smenu_entry_get_caption(e);
185 PCUT_ASSERT_STR_EQUALS("A", caption);
186 cmd = smenu_entry_get_cmd(e);
187 PCUT_ASSERT_STR_EQUALS("a", cmd);
188
189 e = tbarcfg_smenu_next(e);
190
191 /* Check that entry is still a separator */
192 PCUT_ASSERT_TRUE(smenu_entry_get_separator(e));
193
194 tbarcfg_close(tbcfg);
195 remove(fname);
196}
197
198/** Getting menu entry properties */
199PCUT_TEST(get_caption_cmd_term)
200{
201 errno_t rc;
202 tbarcfg_t *tbcfg;
203 char fname[L_tmpnam], *p;
204 smenu_entry_t *e;
205 const char *caption;
206 const char *cmd;
207 bool terminal;
208
209 p = tmpnam(fname);
210 PCUT_ASSERT_NOT_NULL(p);
211
212 rc = tbarcfg_create(fname, &tbcfg);
213 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
214
215 rc = smenu_entry_create(tbcfg, "A", "a", false, &e);
216 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
217
218 caption = smenu_entry_get_caption(e);
219 PCUT_ASSERT_STR_EQUALS("A", caption);
220 cmd = smenu_entry_get_cmd(e);
221 PCUT_ASSERT_STR_EQUALS("a", cmd);
222 terminal = smenu_entry_get_terminal(e);
223 PCUT_ASSERT_FALSE(terminal);
224
225 tbarcfg_close(tbcfg);
226 remove(fname);
227}
228
229/** Setting menu entry properties */
230PCUT_TEST(set_caption_cmd_term)
231{
232 errno_t rc;
233 tbarcfg_t *tbcfg;
234 char fname[L_tmpnam], *p;
235 smenu_entry_t *e;
236 const char *caption;
237 const char *cmd;
238 bool terminal;
239
240 p = tmpnam(fname);
241 PCUT_ASSERT_NOT_NULL(p);
242
243 rc = tbarcfg_create(fname, &tbcfg);
244 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
245
246 rc = smenu_entry_create(tbcfg, "A", "a", false, &e);
247 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
248
249 caption = smenu_entry_get_caption(e);
250 PCUT_ASSERT_STR_EQUALS("A", caption);
251 cmd = smenu_entry_get_cmd(e);
252 PCUT_ASSERT_STR_EQUALS("a", cmd);
253 terminal = smenu_entry_get_terminal(e);
254 PCUT_ASSERT_FALSE(terminal);
255
256 /* Set properties */
257 rc = smenu_entry_set_caption(e, "B");
258 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
259 rc = smenu_entry_set_cmd(e, "b");
260 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
261 smenu_entry_set_terminal(e, true);
262
263 rc = smenu_entry_save(e);
264 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
265
266 /* Check that properties have been set */
267 caption = smenu_entry_get_caption(e);
268 PCUT_ASSERT_STR_EQUALS("B", caption);
269 cmd = smenu_entry_get_cmd(e);
270 PCUT_ASSERT_STR_EQUALS("b", cmd);
271 terminal = smenu_entry_get_terminal(e);
272 PCUT_ASSERT_TRUE(terminal);
273
274 tbarcfg_close(tbcfg);
275
276 /* Re-open repository */
277
278 rc = tbarcfg_open(fname, &tbcfg);
279 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
280
281 e = tbarcfg_smenu_first(tbcfg);
282 PCUT_ASSERT_NOT_NULL(e);
283
284 /* Check that new values of properties have persisted */
285 caption = smenu_entry_get_caption(e);
286 PCUT_ASSERT_STR_EQUALS("B", caption);
287 cmd = smenu_entry_get_cmd(e);
288 PCUT_ASSERT_STR_EQUALS("b", cmd);
289
290 tbarcfg_close(tbcfg);
291 remove(fname);
292}
293
294/** Create start menu entry */
295PCUT_TEST(entry_create)
296{
297 errno_t rc;
298 tbarcfg_t *tbcfg;
299 char fname[L_tmpnam], *p;
300 smenu_entry_t *e;
301 const char *caption;
302 const char *cmd;
303 bool terminal;
304
305 p = tmpnam(fname);
306 PCUT_ASSERT_NOT_NULL(p);
307
308 rc = tbarcfg_create(fname, &tbcfg);
309 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
310
311 rc = smenu_entry_create(tbcfg, "A", "a", false, &e);
312 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
313 PCUT_ASSERT_NOT_NULL(e);
314
315 caption = smenu_entry_get_caption(e);
316 PCUT_ASSERT_STR_EQUALS("A", caption);
317 cmd = smenu_entry_get_cmd(e);
318 PCUT_ASSERT_STR_EQUALS("a", cmd);
319 terminal = smenu_entry_get_terminal(e);
320 PCUT_ASSERT_FALSE(terminal);
321
322 smenu_entry_destroy(e);
323
324 rc = smenu_entry_create(tbcfg, "B", "b", true, &e);
325 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
326 PCUT_ASSERT_NOT_NULL(e);
327
328 caption = smenu_entry_get_caption(e);
329 PCUT_ASSERT_STR_EQUALS("B", caption);
330 cmd = smenu_entry_get_cmd(e);
331 PCUT_ASSERT_STR_EQUALS("b", cmd);
332 terminal = smenu_entry_get_terminal(e);
333 PCUT_ASSERT_TRUE(terminal);
334
335 smenu_entry_destroy(e);
336
337 tbarcfg_close(tbcfg);
338 remove(fname);
339}
340
341/** Destroy start menu entry */
342PCUT_TEST(entry_destroy)
343{
344 errno_t rc;
345 tbarcfg_t *tbcfg;
346 char fname[L_tmpnam], *p;
347 smenu_entry_t *e, *f;
348
349 p = tmpnam(fname);
350 PCUT_ASSERT_NOT_NULL(p);
351
352 rc = tbarcfg_create(fname, &tbcfg);
353 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
354
355 rc = smenu_entry_create(tbcfg, "A", "a", false, &e);
356 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
357
358 f = tbarcfg_smenu_first(tbcfg);
359 PCUT_ASSERT_EQUALS(e, f);
360
361 rc = smenu_entry_destroy(e);
362 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
363
364 f = tbarcfg_smenu_first(tbcfg);
365 PCUT_ASSERT_NULL(f);
366
367 tbarcfg_close(tbcfg);
368 remove(fname);
369}
370
371/** Move start menu entry up */
372PCUT_TEST(entry_move_up)
373{
374 errno_t rc;
375 tbarcfg_t *tbcfg;
376 char fname[L_tmpnam], *p;
377 smenu_entry_t *e1, *e2, *e3;
378 smenu_entry_t *f;
379 const char *caption;
380 const char *cmd;
381
382 p = tmpnam(fname);
383 PCUT_ASSERT_NOT_NULL(p);
384
385 rc = tbarcfg_create(fname, &tbcfg);
386 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
387
388 rc = smenu_entry_create(tbcfg, "A", "a", false, &e1);
389 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
390
391 rc = smenu_entry_create(tbcfg, "B", "b", false, &e2);
392 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
393
394 rc = smenu_entry_create(tbcfg, "C", "c", false, &e3);
395 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
396
397 f = tbarcfg_smenu_first(tbcfg);
398 PCUT_ASSERT_EQUALS(e1, f);
399
400 /* Moving the first entry up should have no effect */
401
402 rc = smenu_entry_move_up(e1);
403 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
404
405 f = tbarcfg_smenu_first(tbcfg);
406 PCUT_ASSERT_EQUALS(e1, f);
407
408 /* Moving the second entry up should move it to first position */
409
410 rc = smenu_entry_move_up(e2);
411 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
412
413 f = tbarcfg_smenu_first(tbcfg);
414 PCUT_ASSERT_EQUALS(e2, f);
415
416 /* Moving the last entry up should move it to second position */
417
418 rc = smenu_entry_move_up(e3);
419 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
420
421 f = tbarcfg_smenu_first(tbcfg);
422 PCUT_ASSERT_EQUALS(e2, f);
423
424 f = tbarcfg_smenu_next(f);
425 PCUT_ASSERT_EQUALS(e3, f);
426
427 f = tbarcfg_smenu_next(f);
428 PCUT_ASSERT_EQUALS(e1, f);
429
430 tbarcfg_close(tbcfg);
431
432 /* Re-open repository */
433
434 rc = tbarcfg_open(fname, &tbcfg);
435 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
436
437 /* Check that new order of entries persisted */
438
439 f = tbarcfg_smenu_first(tbcfg);
440 PCUT_ASSERT_NOT_NULL(f);
441
442 caption = smenu_entry_get_caption(f);
443 PCUT_ASSERT_STR_EQUALS("B", caption);
444 cmd = smenu_entry_get_cmd(f);
445 PCUT_ASSERT_STR_EQUALS("b", cmd);
446
447 f = tbarcfg_smenu_next(f);
448 PCUT_ASSERT_NOT_NULL(f);
449
450 caption = smenu_entry_get_caption(f);
451 PCUT_ASSERT_STR_EQUALS("C", caption);
452 cmd = smenu_entry_get_cmd(f);
453 PCUT_ASSERT_STR_EQUALS("c", cmd);
454
455 f = tbarcfg_smenu_next(f);
456 PCUT_ASSERT_NOT_NULL(f);
457
458 caption = smenu_entry_get_caption(f);
459 PCUT_ASSERT_STR_EQUALS("A", caption);
460 cmd = smenu_entry_get_cmd(f);
461 PCUT_ASSERT_STR_EQUALS("a", cmd);
462
463 tbarcfg_close(tbcfg);
464 remove(fname);
465}
466
467/** Move start menu entry down */
468PCUT_TEST(entry_move_down)
469{
470 errno_t rc;
471 tbarcfg_t *tbcfg;
472 char fname[L_tmpnam], *p;
473 smenu_entry_t *e1, *e2, *e3;
474 smenu_entry_t *f;
475 const char *caption;
476 const char *cmd;
477
478 p = tmpnam(fname);
479 PCUT_ASSERT_NOT_NULL(p);
480
481 rc = tbarcfg_create(fname, &tbcfg);
482 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
483
484 rc = smenu_entry_create(tbcfg, "A", "a", false, &e1);
485 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
486
487 rc = smenu_entry_create(tbcfg, "B", "b", false, &e2);
488 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
489
490 rc = smenu_entry_create(tbcfg, "C", "c", false, &e3);
491 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
492
493 f = tbarcfg_smenu_last(tbcfg);
494 PCUT_ASSERT_EQUALS(e3, f);
495
496 /* Moving the last entry down should have no effect */
497
498 rc = smenu_entry_move_down(e3);
499 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
500
501 f = tbarcfg_smenu_last(tbcfg);
502 PCUT_ASSERT_EQUALS(e3, f);
503
504 /* Moving the second entry down should move it to last position */
505
506 rc = smenu_entry_move_down(e2);
507 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
508
509 f = tbarcfg_smenu_last(tbcfg);
510 PCUT_ASSERT_EQUALS(e2, f);
511
512 /* Moving the first entry down should move it to second position */
513
514 rc = smenu_entry_move_down(e1);
515 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
516
517 f = tbarcfg_smenu_last(tbcfg);
518 PCUT_ASSERT_EQUALS(e2, f);
519
520 f = tbarcfg_smenu_prev(f);
521 PCUT_ASSERT_EQUALS(e1, f);
522
523 f = tbarcfg_smenu_prev(f);
524 PCUT_ASSERT_EQUALS(e3, f);
525
526 tbarcfg_close(tbcfg);
527
528 /* Re-open repository */
529
530 rc = tbarcfg_open(fname, &tbcfg);
531 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
532
533 /* Check that new order of entries persisted */
534
535 f = tbarcfg_smenu_first(tbcfg);
536 PCUT_ASSERT_NOT_NULL(f);
537
538 caption = smenu_entry_get_caption(f);
539 PCUT_ASSERT_STR_EQUALS("C", caption);
540 cmd = smenu_entry_get_cmd(f);
541 PCUT_ASSERT_STR_EQUALS("c", cmd);
542
543 f = tbarcfg_smenu_next(f);
544 PCUT_ASSERT_NOT_NULL(f);
545
546 caption = smenu_entry_get_caption(f);
547 PCUT_ASSERT_STR_EQUALS("A", caption);
548 cmd = smenu_entry_get_cmd(f);
549 PCUT_ASSERT_STR_EQUALS("a", cmd);
550
551 f = tbarcfg_smenu_next(f);
552 PCUT_ASSERT_NOT_NULL(f);
553
554 caption = smenu_entry_get_caption(f);
555 PCUT_ASSERT_STR_EQUALS("B", caption);
556 cmd = smenu_entry_get_cmd(f);
557 PCUT_ASSERT_STR_EQUALS("b", cmd);
558
559 tbarcfg_close(tbcfg);
560 remove(fname);
561}
562
563/** Notifications can be delivered from tbarcfg_notify() to a listener. */
564PCUT_TEST(notify)
565{
566 errno_t rc;
567 tbarcfg_listener_t *lst;
568 tbarcfg_test_resp_t test_resp;
569
570 test_resp.notified = false;
571
572 printf("create listener resp=%p\n", (void *)&test_resp);
573 rc = tbarcfg_listener_create(TBARCFG_NOTIFY_DEFAULT,
574 test_cb, &test_resp, &lst);
575 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
576
577 rc = tbarcfg_notify(TBARCFG_NOTIFY_DEFAULT);
578 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
579
580 PCUT_ASSERT_TRUE(test_resp.notified);
581 tbarcfg_listener_destroy(lst);
582}
583
584static void test_cb(void *arg)
585{
586 tbarcfg_test_resp_t *resp = (tbarcfg_test_resp_t *)arg;
587
588 printf("test_cb: executing resp=%p\n", (void *)resp);
589 resp->notified = true;
590}
591
592PCUT_EXPORT(tbarcfg);
Note: See TracBrowser for help on using the repository browser.