1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2022 Google LLC
13 #include <linux/input.h>
14 #include <test/suites.h>
16 #include "bootstd_common.h"
17 #include <test/cedit-test.h>
18 #include "../../boot/scene_internal.h"
63 /* pointer to current item */
67 #define BAD_POINTER ((void *)1)
69 /* names for various things */
70 #define EXPO_NAME "my menus"
71 #define SCENE_NAME1 "main"
72 #define SCENE_NAME2 "second"
73 #define SCENE_TITLE "Main Menu"
74 #define LOGO_NAME "logo"
76 /* Check base expo support */
77 static int expo_base(struct unit_test_state *uts)
85 ut_assertok(uclass_first_device_err(UCLASS_VIDEO, &dev));
87 start_mem = ut_check_free();
90 strcpy(name, EXPO_NAME);
91 ut_assertok(expo_new(name, NULL, &exp));
93 ut_assertnonnull(exp);
94 ut_asserteq(0, exp->scene_id);
95 ut_asserteq(0, exp->next_id);
97 /* Make sure the name was allocated */
98 ut_assertnonnull(exp->name);
99 ut_asserteq_str(EXPO_NAME, exp->name);
101 ut_assertok(expo_set_display(exp, dev));
103 ut_assertok(ut_check_delta(start_mem));
105 /* test handling out-of-memory conditions */
106 for (i = 0; i < 2; i++) {
109 malloc_enable_testing(i);
111 ut_asserteq(-ENOMEM, expo_new(EXPO_NAME, NULL, &exp2));
112 ut_asserteq_ptr(BAD_POINTER, exp2);
113 malloc_disable_testing();
118 BOOTSTD_TEST(expo_base, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
120 /* Check creating a scene */
121 static int expo_scene(struct unit_test_state *uts)
129 start_mem = ut_check_free();
131 ut_assertok(expo_new(EXPO_NAME, NULL, &exp));
134 ut_asserteq(0, exp->next_id);
135 strcpy(name, SCENE_NAME1);
136 id = scene_new(exp, name, SCENE1, &scn);
138 ut_assertnonnull(scn);
139 ut_asserteq(SCENE1, id);
140 ut_asserteq(SCENE1 + 1, exp->next_id);
141 ut_asserteq_ptr(exp, scn->expo);
143 /* Make sure the name was allocated */
144 ut_assertnonnull(scn->name);
145 ut_asserteq_str(SCENE_NAME1, scn->name);
148 title_id = expo_str(exp, "title", STR_SCENE_TITLE, SCENE_TITLE);
149 ut_assert(title_id >= 0);
151 /* Use an allocated ID - this will be allocated after the title str */
153 id = scene_new(exp, SCENE_NAME2, 0, &scn);
154 ut_assertnonnull(scn);
155 ut_assertok(scene_title_set(scn, title_id));
156 ut_asserteq(STR_SCENE_TITLE + 1, id);
157 ut_asserteq(STR_SCENE_TITLE + 2, exp->next_id);
158 ut_asserteq_ptr(exp, scn->expo);
160 ut_asserteq_str(SCENE_NAME2, scn->name);
161 ut_asserteq(title_id, scn->title_id);
165 ut_assertok(ut_check_delta(start_mem));
169 BOOTSTD_TEST(expo_scene, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
171 /* Check creating a scene with objects */
172 static int expo_object(struct unit_test_state *uts)
174 struct scene_obj_img *img;
175 struct scene_obj_txt *txt;
183 start_mem = ut_check_free();
185 ut_assertok(expo_new(EXPO_NAME, NULL, &exp));
186 id = scene_new(exp, SCENE_NAME1, SCENE1, &scn);
189 ut_asserteq(0, scene_obj_count(scn));
192 strcpy(name, LOGO_NAME);
193 id = scene_img(scn, name, OBJ_LOGO, data, &img);
196 ut_assertnonnull(img);
197 ut_asserteq(OBJ_LOGO, id);
198 ut_asserteq(OBJ_LOGO + 1, exp->next_id);
199 ut_asserteq_ptr(scn, img->obj.scene);
200 ut_asserteq(SCENEOBJT_IMAGE, img->obj.type);
202 ut_asserteq_ptr(data, img->data);
204 /* Make sure the name was allocated */
205 ut_assertnonnull(scn->name);
206 ut_asserteq_str(SCENE_NAME1, scn->name);
208 ut_asserteq(1, scene_obj_count(scn));
210 id = scene_txt_str(scn, "text", OBJ_TEXT, STR_TEXT, "my string", &txt);
212 ut_assertnonnull(txt);
213 ut_asserteq(OBJ_TEXT, id);
214 ut_asserteq(SCENEOBJT_TEXT, txt->obj.type);
215 ut_asserteq(2, scene_obj_count(scn));
217 /* Check passing NULL as the final parameter */
218 id = scene_txt_str(scn, "text2", OBJ_TEXT2, STR_TEXT2, "another string",
221 ut_asserteq(3, scene_obj_count(scn));
225 ut_assertok(ut_check_delta(start_mem));
229 BOOTSTD_TEST(expo_object, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
231 /* Check setting object attributes and using themes */
232 static int expo_object_attr(struct unit_test_state *uts)
234 struct scene_obj_menu *menu;
235 struct scene_obj_img *img;
236 struct scene_obj_txt *txt;
245 start_mem = ut_check_free();
247 ut_assertok(expo_new(EXPO_NAME, NULL, &exp));
248 id = scene_new(exp, SCENE_NAME1, SCENE1, &scn);
252 id = scene_img(scn, LOGO_NAME, OBJ_LOGO, data, &img);
255 ut_assertok(scene_obj_set_pos(scn, OBJ_LOGO, 123, 456));
256 ut_asserteq(123, img->obj.dim.x);
257 ut_asserteq(456, img->obj.dim.y);
259 ut_asserteq(-ENOENT, scene_obj_set_pos(scn, OBJ_TEXT2, 0, 0));
261 id = scene_txt_str(scn, "text", OBJ_TEXT, STR_TEXT, "my string", &txt);
264 strcpy(name, "font2");
265 ut_assertok(scene_txt_set_font(scn, OBJ_TEXT, name, 42));
266 ut_asserteq_ptr(name, txt->font_name);
267 ut_asserteq(42, txt->font_size);
269 ut_asserteq(-ENOENT, scene_txt_set_font(scn, OBJ_TEXT2, name, 42));
271 id = scene_menu(scn, "main", OBJ_MENU, &menu);
274 ut_assertok(scene_menu_set_title(scn, OBJ_MENU, OBJ_TEXT));
276 ut_asserteq(-ENOENT, scene_menu_set_title(scn, OBJ_TEXT2, OBJ_TEXT));
277 ut_asserteq(-EINVAL, scene_menu_set_title(scn, OBJ_MENU, OBJ_TEXT2));
279 node = ofnode_path("/bootstd/theme");
280 ut_assert(ofnode_valid(node));
281 ut_assertok(expo_apply_theme(exp, node));
282 ut_asserteq(30, txt->font_size);
286 ut_assertok(ut_check_delta(start_mem));
290 BOOTSTD_TEST(expo_object_attr, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
292 /* Check creating a scene with a menu */
293 static int expo_object_menu(struct unit_test_state *uts)
295 struct scene_obj_menu *menu;
296 struct scene_menitem *item;
297 int id, label_id, desc_id, key_id, pointer_id, preview_id;
298 struct scene_obj_txt *ptr, *name1, *desc1, *key1, *tit, *prev1;
303 start_mem = ut_check_free();
305 ut_assertok(expo_new(EXPO_NAME, NULL, &exp));
306 id = scene_new(exp, SCENE_NAME1, SCENE1, &scn);
309 id = scene_menu(scn, "main", OBJ_MENU, &menu);
311 ut_assertnonnull(menu);
312 ut_asserteq(OBJ_MENU, id);
313 ut_asserteq(SCENEOBJT_MENU, menu->obj.type);
314 ut_asserteq(0, menu->title_id);
315 ut_asserteq(0, menu->pointer_id);
317 ut_assertok(scene_obj_set_pos(scn, OBJ_MENU, 50, 400));
318 ut_asserteq(50, menu->obj.dim.x);
319 ut_asserteq(400, menu->obj.dim.y);
321 id = scene_txt_str(scn, "title", OBJ_MENU_TITLE, STR_MENU_TITLE,
324 ut_assertok(scene_menu_set_title(scn, OBJ_MENU, OBJ_MENU_TITLE));
325 ut_asserteq(OBJ_MENU_TITLE, menu->title_id);
327 pointer_id = scene_txt_str(scn, "cur_item", POINTER_TEXT,
328 STR_POINTER_TEXT, ">", &ptr);
329 ut_assert(pointer_id > 0);
331 ut_assertok(scene_menu_set_pointer(scn, OBJ_MENU, POINTER_TEXT));
332 ut_asserteq(POINTER_TEXT, menu->pointer_id);
334 label_id = scene_txt_str(scn, "label1", ITEM1_LABEL, STR_ITEM1_LABEL,
336 ut_assert(label_id > 0);
338 desc_id = scene_txt_str(scn, "desc1", ITEM1_DESC, STR_ITEM1_DESC,
339 "Lord Melchett", &desc1);
340 ut_assert(desc_id > 0);
342 key_id = scene_txt_str(scn, "item1-key", ITEM1_KEY, STR_ITEM1_KEY, "1",
344 ut_assert(key_id > 0);
346 preview_id = scene_txt_str(scn, "item1-preview", ITEM1_PREVIEW,
347 STR_ITEM1_PREVIEW, "(preview1)", &prev1);
348 ut_assert(preview_id > 0);
350 id = scene_menuitem(scn, OBJ_MENU, "linux", ITEM1, ITEM1_KEY,
351 ITEM1_LABEL, ITEM1_DESC, ITEM1_PREVIEW, 0, &item);
352 ut_asserteq(ITEM1, id);
353 ut_asserteq(id, item->id);
354 ut_asserteq(key_id, item->key_id);
355 ut_asserteq(label_id, item->label_id);
356 ut_asserteq(desc_id, item->desc_id);
357 ut_asserteq(preview_id, item->preview_id);
359 ut_assertok(scene_arrange(scn));
361 /* arranging the scene should cause the first item to become current */
362 ut_asserteq(id, menu->cur_item_id);
364 /* the title should be at the top */
365 ut_asserteq(menu->obj.dim.x, tit->obj.dim.x);
366 ut_asserteq(menu->obj.dim.y, tit->obj.dim.y);
368 /* the first item should be next */
369 ut_asserteq(menu->obj.dim.x, name1->obj.dim.x);
370 ut_asserteq(menu->obj.dim.y + 32, name1->obj.dim.y);
372 ut_asserteq(menu->obj.dim.x + 230, key1->obj.dim.x);
373 ut_asserteq(menu->obj.dim.y + 32, key1->obj.dim.y);
375 ut_asserteq(menu->obj.dim.x + 200, ptr->obj.dim.x);
376 ut_asserteq(menu->obj.dim.y + 32, ptr->obj.dim.y);
378 ut_asserteq(menu->obj.dim.x + 280, desc1->obj.dim.x);
379 ut_asserteq(menu->obj.dim.y + 32, desc1->obj.dim.y);
381 ut_asserteq(-4, prev1->obj.dim.x);
382 ut_asserteq(menu->obj.dim.y + 32, prev1->obj.dim.y);
383 ut_asserteq(true, prev1->obj.flags & SCENEOF_HIDE);
387 ut_assertok(ut_check_delta(start_mem));
391 BOOTSTD_TEST(expo_object_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
393 /* Check rendering a scene */
394 static int expo_render_image(struct unit_test_state *uts)
396 struct scene_obj_menu *menu;
397 struct scene *scn, *scn2;
398 struct expo_action act;
399 struct scene_obj *obj;
404 console_record_reset_enable();
405 ut_assertok(uclass_first_device_err(UCLASS_VIDEO, &dev));
407 ut_assertok(expo_new(EXPO_NAME, NULL, &exp));
408 id = scene_new(exp, SCENE_NAME1, SCENE1, &scn);
410 ut_assertok(expo_set_display(exp, dev));
412 id = scene_img(scn, "logo", OBJ_LOGO, video_get_u_boot_logo(), NULL);
414 ut_assertok(scene_obj_set_pos(scn, OBJ_LOGO, 50, 20));
416 id = scene_txt_str(scn, "text", OBJ_TEXT, STR_TEXT, "my string", NULL);
418 ut_assertok(scene_txt_set_font(scn, OBJ_TEXT, "cantoraone_regular",
420 ut_assertok(scene_obj_set_pos(scn, OBJ_TEXT, 400, 100));
422 id = scene_txt_str(scn, "text", OBJ_TEXT2, STR_TEXT2, "another string",
425 ut_assertok(scene_txt_set_font(scn, OBJ_TEXT2, "nimbus_sans_l_regular",
427 ut_assertok(scene_obj_set_pos(scn, OBJ_TEXT2, 200, 600));
429 id = scene_menu(scn, "main", OBJ_MENU, &menu);
432 id = scene_txt_str(scn, "title", OBJ_MENU_TITLE, STR_MENU_TITLE,
435 ut_assertok(scene_menu_set_title(scn, OBJ_MENU, OBJ_MENU_TITLE));
437 id = scene_txt_str(scn, "cur_item", POINTER_TEXT, STR_POINTER_TEXT, ">",
440 ut_assertok(scene_menu_set_pointer(scn, OBJ_MENU, POINTER_TEXT));
442 id = scene_txt_str(scn, "label1", ITEM1_LABEL, STR_ITEM1_LABEL, "Play",
445 id = scene_txt_str(scn, "item1 txt", ITEM1_DESC, STR_ITEM1_DESC,
446 "Lord Melchett", NULL);
448 id = scene_txt_str(scn, "item1-key", ITEM1_KEY, STR_ITEM1_KEY, "1",
451 id = scene_img(scn, "item1-preview", ITEM1_PREVIEW,
452 video_get_u_boot_logo(), NULL);
453 id = scene_menuitem(scn, OBJ_MENU, "item1", ITEM1, ITEM1_KEY,
454 ITEM1_LABEL, ITEM1_DESC, ITEM1_PREVIEW, 0, NULL);
457 id = scene_txt_str(scn, "label2", ITEM2_LABEL, STR_ITEM2_LABEL, "Now",
460 id = scene_txt_str(scn, "item2 txt", ITEM2_DESC, STR_ITEM2_DESC,
463 id = scene_txt_str(scn, "item2-key", ITEM2_KEY, STR_ITEM2_KEY, "2",
466 id = scene_img(scn, "item2-preview", ITEM2_PREVIEW,
467 video_get_u_boot_logo(), NULL);
470 id = scene_menuitem(scn, OBJ_MENU, "item2", ITEM2, ITEM2_KEY,
471 ITEM2_LABEL, ITEM2_DESC, ITEM2_PREVIEW, 0, NULL);
474 ut_assertok(scene_obj_set_pos(scn, OBJ_MENU, 50, 400));
476 scn2 = expo_lookup_scene_id(exp, SCENE1);
477 ut_asserteq_ptr(scn, scn2);
478 scn2 = expo_lookup_scene_id(exp, SCENE2);
481 /* render without a scene */
482 ut_asserteq(-ECHILD, expo_render(exp));
484 ut_assertok(expo_calc_dims(exp));
485 ut_assertok(scene_arrange(scn));
487 /* check dimensions of text */
488 obj = scene_obj_find(scn, OBJ_TEXT, SCENEOBJT_NONE);
489 ut_assertnonnull(obj);
490 ut_asserteq(400, obj->dim.x);
491 ut_asserteq(100, obj->dim.y);
492 ut_asserteq(126, obj->dim.w);
493 ut_asserteq(40, obj->dim.h);
495 /* check dimensions of image */
496 obj = scene_obj_find(scn, OBJ_LOGO, SCENEOBJT_NONE);
497 ut_assertnonnull(obj);
498 ut_asserteq(50, obj->dim.x);
499 ut_asserteq(20, obj->dim.y);
500 ut_asserteq(160, obj->dim.w);
501 ut_asserteq(160, obj->dim.h);
503 /* check dimensions of menu labels - both should be the same width */
504 obj = scene_obj_find(scn, ITEM1_LABEL, SCENEOBJT_NONE);
505 ut_assertnonnull(obj);
506 ut_asserteq(50, obj->dim.x);
507 ut_asserteq(436, obj->dim.y);
508 ut_asserteq(29, obj->dim.w);
509 ut_asserteq(18, obj->dim.h);
511 obj = scene_obj_find(scn, ITEM2_LABEL, SCENEOBJT_NONE);
512 ut_assertnonnull(obj);
513 ut_asserteq(50, obj->dim.x);
514 ut_asserteq(454, obj->dim.y);
515 ut_asserteq(29, obj->dim.w);
516 ut_asserteq(18, obj->dim.h);
518 /* check dimensions of menu */
519 obj = scene_obj_find(scn, OBJ_MENU, SCENEOBJT_NONE);
520 ut_assertnonnull(obj);
521 ut_asserteq(50, obj->dim.x);
522 ut_asserteq(400, obj->dim.y);
523 ut_asserteq(160, obj->dim.w);
524 ut_asserteq(160, obj->dim.h);
527 expo_set_scene_id(exp, SCENE1);
528 ut_assertok(expo_render(exp));
531 ut_assertok(expo_send_key(exp, BKEY_DOWN));
533 ut_assertok(expo_action_get(exp, &act));
535 ut_asserteq(EXPOACT_POINT_ITEM, act.type);
536 ut_asserteq(ITEM2, act.select.id);
537 ut_assertok(expo_render(exp));
539 /* make sure only the preview for the second item is shown */
540 obj = scene_obj_find(scn, ITEM1_PREVIEW, SCENEOBJT_NONE);
541 ut_asserteq(true, obj->flags & SCENEOF_HIDE);
543 obj = scene_obj_find(scn, ITEM2_PREVIEW, SCENEOBJT_NONE);
544 ut_asserteq(false, obj->flags & SCENEOF_HIDE);
547 ut_assertok(expo_send_key(exp, BKEY_SELECT));
549 ut_assertok(expo_action_get(exp, &act));
550 ut_asserteq(EXPOACT_SELECT, act.type);
551 ut_asserteq(ITEM2, act.select.id);
553 /* make sure the action doesn't come again */
554 ut_asserteq(-EAGAIN, expo_action_get(exp, &act));
556 /* make sure there was no console output */
557 ut_assert_console_end();
559 /* now try in text mode */
560 expo_set_text_mode(exp, true);
561 ut_assertok(expo_render(exp));
563 ut_assert_nextline("U-Boot : Boot Menu");
564 ut_assert_nextline("%s", "");
565 ut_assert_nextline("Main Menu");
566 ut_assert_nextline("%s", "");
567 ut_assert_nextline(" 1 Play Lord Melchett");
568 ut_assert_nextline(" > 2 Now Lord Percy");
570 /* Move back up to the first item */
571 ut_assertok(expo_send_key(exp, BKEY_UP));
573 ut_assertok(expo_action_get(exp, &act));
575 ut_asserteq(EXPOACT_POINT_ITEM, act.type);
576 ut_asserteq(ITEM1, act.select.id);
578 ut_assertok(expo_render(exp));
579 ut_assert_nextline("U-Boot : Boot Menu");
580 ut_assert_nextline("%s", "");
581 ut_assert_nextline("Main Menu");
582 ut_assert_nextline("%s", "");
583 ut_assert_nextline(" > 1 Play Lord Melchett");
584 ut_assert_nextline(" 2 Now Lord Percy");
586 ut_assert_console_end();
592 BOOTSTD_TEST(expo_render_image, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
594 /* Check building an expo from a devicetree description */
595 static int expo_test_build(struct unit_test_state *uts)
597 struct scene_obj_menu *menu;
598 struct scene_menitem *item;
599 struct scene_obj_txt *txt;
600 struct scene_obj *obj;
606 node = ofnode_path("/cedit");
607 ut_assert(ofnode_valid(node));
608 ut_assertok(expo_build(node, &exp));
610 ut_asserteq_str("name", exp->name);
611 ut_asserteq(0, exp->scene_id);
612 ut_asserteq(ID_DYNAMIC_START + 20, exp->next_id);
613 ut_asserteq(false, exp->popup);
615 /* check the scene */
616 scn = expo_lookup_scene_id(exp, ID_SCENE1);
617 ut_assertnonnull(scn);
618 ut_asserteq_str("main", scn->name);
619 ut_asserteq(ID_SCENE1, scn->id);
620 ut_asserteq(ID_DYNAMIC_START + 1, scn->title_id);
621 ut_asserteq(0, scn->highlight_id);
623 /* check the title */
624 txt = scene_obj_find(scn, scn->title_id, SCENEOBJT_NONE);
625 ut_assertnonnull(txt);
627 ut_asserteq_ptr(scn, obj->scene);
628 ut_asserteq_str("title", obj->name);
629 ut_asserteq(scn->title_id, obj->id);
630 ut_asserteq(SCENEOBJT_TEXT, obj->type);
631 ut_asserteq(0, obj->flags);
632 ut_asserteq_str("Test Configuration", expo_get_str(exp, txt->str_id));
635 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_NONE);
637 ut_asserteq_ptr(scn, obj->scene);
638 ut_asserteq_str("cpu-speed", obj->name);
639 ut_asserteq(ID_CPU_SPEED, obj->id);
640 ut_asserteq(SCENEOBJT_MENU, obj->type);
641 ut_asserteq(0, obj->flags);
643 txt = scene_obj_find(scn, menu->title_id, SCENEOBJT_NONE);
644 ut_asserteq_str("CPU speed", expo_get_str(exp, txt->str_id));
646 ut_asserteq(0, menu->cur_item_id);
647 ut_asserteq(0, menu->pointer_id);
649 /* check the items */
650 item = list_first_entry(&menu->item_head, struct scene_menitem,
652 ut_asserteq_str("00", item->name);
653 ut_asserteq(ID_CPU_SPEED_1, item->id);
654 ut_asserteq(0, item->key_id);
655 ut_asserteq(0, item->desc_id);
656 ut_asserteq(0, item->preview_id);
657 ut_asserteq(0, item->flags);
659 txt = scene_obj_find(scn, item->label_id, SCENEOBJT_NONE);
660 ut_asserteq_str("2 GHz", expo_get_str(exp, txt->str_id));
663 list_for_each_entry(item, &menu->item_head, sibling)
665 ut_asserteq(3, count);
671 BOOTSTD_TEST(expo_test_build, UT_TESTF_DM);
673 /* Check the cedit command */
674 static int expo_cedit(struct unit_test_state *uts)
676 extern struct expo *cur_exp;
677 struct scene_obj_menu *menu;
678 struct scene_obj_txt *txt;
682 if (!IS_ENABLED(CONFIG_CMD_CEDIT))
685 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
687 console_record_reset_enable();
690 * ^N Move down to second menu
692 * ^N Move down to second item
696 console_in_puts("\x0e\x0d\x0e\x0d\e");
697 ut_assertok(run_command("cedit run", 0));
700 scn = expo_lookup_scene_id(exp, exp->scene_id);
701 ut_assertnonnull(scn);
703 menu = scene_obj_find(scn, scn->highlight_id, SCENEOBJT_NONE);
704 ut_assertnonnull(menu);
706 txt = scene_obj_find(scn, menu->title_id, SCENEOBJT_NONE);
707 ut_assertnonnull(txt);
708 ut_asserteq_str("AC Power", expo_get_str(exp, txt->str_id));
710 ut_asserteq(ID_AC_ON, menu->cur_item_id);
714 BOOTSTD_TEST(expo_cedit, UT_TESTF_DM | UT_TESTF_SCAN_FDT);