Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2022 Google LLC |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | b8c2655 | 2023-06-01 10:23:03 -0600 | [diff] [blame] | 8 | #include <command.h> |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <expo.h> |
| 11 | #include <menu.h> |
| 12 | #include <video.h> |
| 13 | #include <linux/input.h> |
| 14 | #include <test/suites.h> |
| 15 | #include <test/ut.h> |
| 16 | #include "bootstd_common.h" |
Simon Glass | 6130072 | 2023-06-01 10:23:01 -0600 | [diff] [blame] | 17 | #include <test/cedit-test.h> |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 18 | #include "../../boot/scene_internal.h" |
| 19 | |
| 20 | enum { |
| 21 | /* scenes */ |
| 22 | SCENE1 = 7, |
| 23 | SCENE2, |
| 24 | |
| 25 | /* objects */ |
| 26 | OBJ_LOGO, |
| 27 | OBJ_TEXT, |
| 28 | OBJ_TEXT2, |
| 29 | OBJ_MENU, |
| 30 | OBJ_MENU_TITLE, |
| 31 | |
| 32 | /* strings */ |
Simon Glass | ea274b6 | 2023-06-01 10:22:27 -0600 | [diff] [blame] | 33 | STR_SCENE_TITLE, |
| 34 | |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 35 | STR_TEXT, |
| 36 | STR_TEXT2, |
| 37 | STR_MENU_TITLE, |
| 38 | STR_POINTER_TEXT, |
| 39 | |
| 40 | STR_ITEM1_LABEL, |
| 41 | STR_ITEM1_DESC, |
| 42 | STR_ITEM1_KEY, |
| 43 | STR_ITEM1_PREVIEW, |
| 44 | |
| 45 | STR_ITEM2_LABEL, |
| 46 | STR_ITEM2_DESC, |
| 47 | STR_ITEM2_KEY, |
| 48 | STR_ITEM2_PREVIEW, |
| 49 | |
| 50 | /* menu items */ |
| 51 | ITEM1, |
| 52 | ITEM1_LABEL, |
| 53 | ITEM1_DESC, |
| 54 | ITEM1_KEY, |
| 55 | ITEM1_PREVIEW, |
| 56 | |
| 57 | ITEM2, |
| 58 | ITEM2_LABEL, |
| 59 | ITEM2_DESC, |
| 60 | ITEM2_KEY, |
| 61 | ITEM2_PREVIEW, |
| 62 | |
| 63 | /* pointer to current item */ |
| 64 | POINTER_TEXT, |
| 65 | }; |
| 66 | |
| 67 | #define BAD_POINTER ((void *)1) |
| 68 | |
| 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" |
| 75 | |
| 76 | /* Check base expo support */ |
| 77 | static int expo_base(struct unit_test_state *uts) |
| 78 | { |
| 79 | struct udevice *dev; |
| 80 | struct expo *exp; |
| 81 | ulong start_mem; |
| 82 | char name[100]; |
| 83 | int i; |
| 84 | |
| 85 | ut_assertok(uclass_first_device_err(UCLASS_VIDEO, &dev)); |
| 86 | |
| 87 | start_mem = ut_check_free(); |
| 88 | |
| 89 | exp = NULL; |
| 90 | strcpy(name, EXPO_NAME); |
| 91 | ut_assertok(expo_new(name, NULL, &exp)); |
| 92 | *name = '\0'; |
| 93 | ut_assertnonnull(exp); |
| 94 | ut_asserteq(0, exp->scene_id); |
| 95 | ut_asserteq(0, exp->next_id); |
| 96 | |
| 97 | /* Make sure the name was allocated */ |
| 98 | ut_assertnonnull(exp->name); |
| 99 | ut_asserteq_str(EXPO_NAME, exp->name); |
| 100 | |
| 101 | ut_assertok(expo_set_display(exp, dev)); |
| 102 | expo_destroy(exp); |
| 103 | ut_assertok(ut_check_delta(start_mem)); |
| 104 | |
| 105 | /* test handling out-of-memory conditions */ |
| 106 | for (i = 0; i < 2; i++) { |
| 107 | struct expo *exp2; |
| 108 | |
| 109 | malloc_enable_testing(i); |
| 110 | exp2 = BAD_POINTER; |
| 111 | ut_asserteq(-ENOMEM, expo_new(EXPO_NAME, NULL, &exp2)); |
| 112 | ut_asserteq_ptr(BAD_POINTER, exp2); |
| 113 | malloc_disable_testing(); |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | BOOTSTD_TEST(expo_base, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 119 | |
| 120 | /* Check creating a scene */ |
| 121 | static int expo_scene(struct unit_test_state *uts) |
| 122 | { |
| 123 | struct scene *scn; |
| 124 | struct expo *exp; |
| 125 | ulong start_mem; |
| 126 | char name[100]; |
Simon Glass | ea274b6 | 2023-06-01 10:22:27 -0600 | [diff] [blame] | 127 | int id, title_id; |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 128 | |
| 129 | start_mem = ut_check_free(); |
| 130 | |
| 131 | ut_assertok(expo_new(EXPO_NAME, NULL, &exp)); |
| 132 | |
| 133 | scn = NULL; |
| 134 | ut_asserteq(0, exp->next_id); |
| 135 | strcpy(name, SCENE_NAME1); |
| 136 | id = scene_new(exp, name, SCENE1, &scn); |
| 137 | *name = '\0'; |
| 138 | ut_assertnonnull(scn); |
| 139 | ut_asserteq(SCENE1, id); |
| 140 | ut_asserteq(SCENE1 + 1, exp->next_id); |
| 141 | ut_asserteq_ptr(exp, scn->expo); |
| 142 | |
| 143 | /* Make sure the name was allocated */ |
| 144 | ut_assertnonnull(scn->name); |
| 145 | ut_asserteq_str(SCENE_NAME1, scn->name); |
| 146 | |
| 147 | /* Set the title */ |
Simon Glass | ea274b6 | 2023-06-01 10:22:27 -0600 | [diff] [blame] | 148 | title_id = expo_str(exp, "title", STR_SCENE_TITLE, SCENE_TITLE); |
| 149 | ut_assert(title_id >= 0); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 150 | |
Simon Glass | ea274b6 | 2023-06-01 10:22:27 -0600 | [diff] [blame] | 151 | /* Use an allocated ID - this will be allocated after the title str */ |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 152 | scn = NULL; |
| 153 | id = scene_new(exp, SCENE_NAME2, 0, &scn); |
| 154 | ut_assertnonnull(scn); |
Simon Glass | ea274b6 | 2023-06-01 10:22:27 -0600 | [diff] [blame] | 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); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 158 | ut_asserteq_ptr(exp, scn->expo); |
| 159 | |
| 160 | ut_asserteq_str(SCENE_NAME2, scn->name); |
Simon Glass | ea274b6 | 2023-06-01 10:22:27 -0600 | [diff] [blame] | 161 | ut_asserteq(title_id, scn->title_id); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 162 | |
| 163 | expo_destroy(exp); |
| 164 | |
| 165 | ut_assertok(ut_check_delta(start_mem)); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | BOOTSTD_TEST(expo_scene, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 170 | |
| 171 | /* Check creating a scene with objects */ |
| 172 | static int expo_object(struct unit_test_state *uts) |
| 173 | { |
| 174 | struct scene_obj_img *img; |
| 175 | struct scene_obj_txt *txt; |
| 176 | struct scene *scn; |
| 177 | struct expo *exp; |
| 178 | ulong start_mem; |
| 179 | char name[100]; |
| 180 | char *data; |
| 181 | int id; |
| 182 | |
| 183 | start_mem = ut_check_free(); |
| 184 | |
| 185 | ut_assertok(expo_new(EXPO_NAME, NULL, &exp)); |
| 186 | id = scene_new(exp, SCENE_NAME1, SCENE1, &scn); |
| 187 | ut_assert(id > 0); |
| 188 | |
| 189 | ut_asserteq(0, scene_obj_count(scn)); |
| 190 | |
| 191 | data = NULL; |
| 192 | strcpy(name, LOGO_NAME); |
| 193 | id = scene_img(scn, name, OBJ_LOGO, data, &img); |
| 194 | ut_assert(id > 0); |
| 195 | *name = '\0'; |
| 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); |
| 201 | |
| 202 | ut_asserteq_ptr(data, img->data); |
| 203 | |
| 204 | /* Make sure the name was allocated */ |
| 205 | ut_assertnonnull(scn->name); |
| 206 | ut_asserteq_str(SCENE_NAME1, scn->name); |
| 207 | |
| 208 | ut_asserteq(1, scene_obj_count(scn)); |
| 209 | |
| 210 | id = scene_txt_str(scn, "text", OBJ_TEXT, STR_TEXT, "my string", &txt); |
| 211 | ut_assert(id > 0); |
| 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)); |
| 216 | |
| 217 | /* Check passing NULL as the final parameter */ |
| 218 | id = scene_txt_str(scn, "text2", OBJ_TEXT2, STR_TEXT2, "another string", |
| 219 | NULL); |
| 220 | ut_assert(id > 0); |
| 221 | ut_asserteq(3, scene_obj_count(scn)); |
| 222 | |
| 223 | expo_destroy(exp); |
| 224 | |
| 225 | ut_assertok(ut_check_delta(start_mem)); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | BOOTSTD_TEST(expo_object, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 230 | |
Simon Glass | c999e17 | 2023-06-01 10:22:53 -0600 | [diff] [blame] | 231 | /* Check setting object attributes and using themes */ |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 232 | static int expo_object_attr(struct unit_test_state *uts) |
| 233 | { |
| 234 | struct scene_obj_menu *menu; |
| 235 | struct scene_obj_img *img; |
| 236 | struct scene_obj_txt *txt; |
| 237 | struct scene *scn; |
| 238 | struct expo *exp; |
| 239 | ulong start_mem; |
| 240 | char name[100]; |
Simon Glass | c999e17 | 2023-06-01 10:22:53 -0600 | [diff] [blame] | 241 | ofnode node; |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 242 | char *data; |
| 243 | int id; |
| 244 | |
| 245 | start_mem = ut_check_free(); |
| 246 | |
| 247 | ut_assertok(expo_new(EXPO_NAME, NULL, &exp)); |
| 248 | id = scene_new(exp, SCENE_NAME1, SCENE1, &scn); |
| 249 | ut_assert(id > 0); |
| 250 | |
| 251 | data = NULL; |
| 252 | id = scene_img(scn, LOGO_NAME, OBJ_LOGO, data, &img); |
| 253 | ut_assert(id > 0); |
| 254 | |
| 255 | ut_assertok(scene_obj_set_pos(scn, OBJ_LOGO, 123, 456)); |
Simon Glass | 7b04395 | 2023-06-01 10:22:49 -0600 | [diff] [blame] | 256 | ut_asserteq(123, img->obj.dim.x); |
| 257 | ut_asserteq(456, img->obj.dim.y); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 258 | |
| 259 | ut_asserteq(-ENOENT, scene_obj_set_pos(scn, OBJ_TEXT2, 0, 0)); |
| 260 | |
| 261 | id = scene_txt_str(scn, "text", OBJ_TEXT, STR_TEXT, "my string", &txt); |
| 262 | ut_assert(id > 0); |
| 263 | |
| 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); |
| 268 | |
| 269 | ut_asserteq(-ENOENT, scene_txt_set_font(scn, OBJ_TEXT2, name, 42)); |
| 270 | |
| 271 | id = scene_menu(scn, "main", OBJ_MENU, &menu); |
| 272 | ut_assert(id > 0); |
| 273 | |
| 274 | ut_assertok(scene_menu_set_title(scn, OBJ_MENU, OBJ_TEXT)); |
| 275 | |
| 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)); |
| 278 | |
Simon Glass | c999e17 | 2023-06-01 10:22:53 -0600 | [diff] [blame] | 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); |
| 283 | |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 284 | expo_destroy(exp); |
| 285 | |
| 286 | ut_assertok(ut_check_delta(start_mem)); |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | BOOTSTD_TEST(expo_object_attr, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 291 | |
| 292 | /* Check creating a scene with a menu */ |
| 293 | static int expo_object_menu(struct unit_test_state *uts) |
| 294 | { |
| 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; |
| 299 | struct scene *scn; |
| 300 | struct expo *exp; |
| 301 | ulong start_mem; |
| 302 | |
| 303 | start_mem = ut_check_free(); |
| 304 | |
| 305 | ut_assertok(expo_new(EXPO_NAME, NULL, &exp)); |
| 306 | id = scene_new(exp, SCENE_NAME1, SCENE1, &scn); |
| 307 | ut_assert(id > 0); |
| 308 | |
| 309 | id = scene_menu(scn, "main", OBJ_MENU, &menu); |
| 310 | ut_assert(id > 0); |
| 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); |
| 316 | |
| 317 | ut_assertok(scene_obj_set_pos(scn, OBJ_MENU, 50, 400)); |
Simon Glass | 7b04395 | 2023-06-01 10:22:49 -0600 | [diff] [blame] | 318 | ut_asserteq(50, menu->obj.dim.x); |
| 319 | ut_asserteq(400, menu->obj.dim.y); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 320 | |
| 321 | id = scene_txt_str(scn, "title", OBJ_MENU_TITLE, STR_MENU_TITLE, |
| 322 | "Main Menu", &tit); |
| 323 | ut_assert(id > 0); |
| 324 | ut_assertok(scene_menu_set_title(scn, OBJ_MENU, OBJ_MENU_TITLE)); |
| 325 | ut_asserteq(OBJ_MENU_TITLE, menu->title_id); |
| 326 | |
| 327 | pointer_id = scene_txt_str(scn, "cur_item", POINTER_TEXT, |
| 328 | STR_POINTER_TEXT, ">", &ptr); |
| 329 | ut_assert(pointer_id > 0); |
| 330 | |
| 331 | ut_assertok(scene_menu_set_pointer(scn, OBJ_MENU, POINTER_TEXT)); |
| 332 | ut_asserteq(POINTER_TEXT, menu->pointer_id); |
| 333 | |
| 334 | label_id = scene_txt_str(scn, "label1", ITEM1_LABEL, STR_ITEM1_LABEL, |
| 335 | "Play", &name1); |
| 336 | ut_assert(label_id > 0); |
| 337 | |
| 338 | desc_id = scene_txt_str(scn, "desc1", ITEM1_DESC, STR_ITEM1_DESC, |
| 339 | "Lord Melchett", &desc1); |
| 340 | ut_assert(desc_id > 0); |
| 341 | |
| 342 | key_id = scene_txt_str(scn, "item1-key", ITEM1_KEY, STR_ITEM1_KEY, "1", |
| 343 | &key1); |
| 344 | ut_assert(key_id > 0); |
| 345 | |
| 346 | preview_id = scene_txt_str(scn, "item1-preview", ITEM1_PREVIEW, |
| 347 | STR_ITEM1_PREVIEW, "(preview1)", &prev1); |
| 348 | ut_assert(preview_id > 0); |
| 349 | |
| 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); |
| 358 | |
Simon Glass | d7e32a8 | 2023-06-01 10:22:35 -0600 | [diff] [blame] | 359 | ut_assertok(scene_arrange(scn)); |
| 360 | |
| 361 | /* arranging the scene should cause the first item to become current */ |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 362 | ut_asserteq(id, menu->cur_item_id); |
| 363 | |
| 364 | /* the title should be at the top */ |
Simon Glass | 7b04395 | 2023-06-01 10:22:49 -0600 | [diff] [blame] | 365 | ut_asserteq(menu->obj.dim.x, tit->obj.dim.x); |
| 366 | ut_asserteq(menu->obj.dim.y, tit->obj.dim.y); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 367 | |
| 368 | /* the first item should be next */ |
Simon Glass | 7b04395 | 2023-06-01 10:22:49 -0600 | [diff] [blame] | 369 | ut_asserteq(menu->obj.dim.x, name1->obj.dim.x); |
| 370 | ut_asserteq(menu->obj.dim.y + 32, name1->obj.dim.y); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 371 | |
Simon Glass | 7b04395 | 2023-06-01 10:22:49 -0600 | [diff] [blame] | 372 | ut_asserteq(menu->obj.dim.x + 230, key1->obj.dim.x); |
| 373 | ut_asserteq(menu->obj.dim.y + 32, key1->obj.dim.y); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 374 | |
Simon Glass | 7b04395 | 2023-06-01 10:22:49 -0600 | [diff] [blame] | 375 | ut_asserteq(menu->obj.dim.x + 200, ptr->obj.dim.x); |
| 376 | ut_asserteq(menu->obj.dim.y + 32, ptr->obj.dim.y); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 377 | |
Simon Glass | 7b04395 | 2023-06-01 10:22:49 -0600 | [diff] [blame] | 378 | ut_asserteq(menu->obj.dim.x + 280, desc1->obj.dim.x); |
| 379 | ut_asserteq(menu->obj.dim.y + 32, desc1->obj.dim.y); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 380 | |
Simon Glass | 7b04395 | 2023-06-01 10:22:49 -0600 | [diff] [blame] | 381 | ut_asserteq(-4, prev1->obj.dim.x); |
| 382 | ut_asserteq(menu->obj.dim.y + 32, prev1->obj.dim.y); |
Simon Glass | c55eeba | 2023-06-01 10:22:54 -0600 | [diff] [blame] | 383 | ut_asserteq(true, prev1->obj.flags & SCENEOF_HIDE); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 384 | |
| 385 | expo_destroy(exp); |
| 386 | |
| 387 | ut_assertok(ut_check_delta(start_mem)); |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | BOOTSTD_TEST(expo_object_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
| 392 | |
| 393 | /* Check rendering a scene */ |
| 394 | static int expo_render_image(struct unit_test_state *uts) |
| 395 | { |
| 396 | struct scene_obj_menu *menu; |
| 397 | struct scene *scn, *scn2; |
| 398 | struct expo_action act; |
| 399 | struct scene_obj *obj; |
| 400 | struct udevice *dev; |
| 401 | struct expo *exp; |
| 402 | int id; |
| 403 | |
| 404 | console_record_reset_enable(); |
| 405 | ut_assertok(uclass_first_device_err(UCLASS_VIDEO, &dev)); |
| 406 | |
| 407 | ut_assertok(expo_new(EXPO_NAME, NULL, &exp)); |
| 408 | id = scene_new(exp, SCENE_NAME1, SCENE1, &scn); |
| 409 | ut_assert(id > 0); |
| 410 | ut_assertok(expo_set_display(exp, dev)); |
| 411 | |
| 412 | id = scene_img(scn, "logo", OBJ_LOGO, video_get_u_boot_logo(), NULL); |
| 413 | ut_assert(id > 0); |
| 414 | ut_assertok(scene_obj_set_pos(scn, OBJ_LOGO, 50, 20)); |
| 415 | |
| 416 | id = scene_txt_str(scn, "text", OBJ_TEXT, STR_TEXT, "my string", NULL); |
| 417 | ut_assert(id > 0); |
| 418 | ut_assertok(scene_txt_set_font(scn, OBJ_TEXT, "cantoraone_regular", |
| 419 | 40)); |
| 420 | ut_assertok(scene_obj_set_pos(scn, OBJ_TEXT, 400, 100)); |
| 421 | |
| 422 | id = scene_txt_str(scn, "text", OBJ_TEXT2, STR_TEXT2, "another string", |
| 423 | NULL); |
| 424 | ut_assert(id > 0); |
| 425 | ut_assertok(scene_txt_set_font(scn, OBJ_TEXT2, "nimbus_sans_l_regular", |
| 426 | 60)); |
| 427 | ut_assertok(scene_obj_set_pos(scn, OBJ_TEXT2, 200, 600)); |
| 428 | |
| 429 | id = scene_menu(scn, "main", OBJ_MENU, &menu); |
| 430 | ut_assert(id > 0); |
| 431 | |
| 432 | id = scene_txt_str(scn, "title", OBJ_MENU_TITLE, STR_MENU_TITLE, |
| 433 | "Main Menu", NULL); |
| 434 | ut_assert(id > 0); |
| 435 | ut_assertok(scene_menu_set_title(scn, OBJ_MENU, OBJ_MENU_TITLE)); |
| 436 | |
| 437 | id = scene_txt_str(scn, "cur_item", POINTER_TEXT, STR_POINTER_TEXT, ">", |
| 438 | NULL); |
| 439 | ut_assert(id > 0); |
| 440 | ut_assertok(scene_menu_set_pointer(scn, OBJ_MENU, POINTER_TEXT)); |
| 441 | |
| 442 | id = scene_txt_str(scn, "label1", ITEM1_LABEL, STR_ITEM1_LABEL, "Play", |
| 443 | NULL); |
| 444 | ut_assert(id > 0); |
| 445 | id = scene_txt_str(scn, "item1 txt", ITEM1_DESC, STR_ITEM1_DESC, |
| 446 | "Lord Melchett", NULL); |
| 447 | ut_assert(id > 0); |
| 448 | id = scene_txt_str(scn, "item1-key", ITEM1_KEY, STR_ITEM1_KEY, "1", |
| 449 | NULL); |
| 450 | ut_assert(id > 0); |
| 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); |
| 455 | ut_assert(id > 0); |
| 456 | |
| 457 | id = scene_txt_str(scn, "label2", ITEM2_LABEL, STR_ITEM2_LABEL, "Now", |
| 458 | NULL); |
| 459 | ut_assert(id > 0); |
| 460 | id = scene_txt_str(scn, "item2 txt", ITEM2_DESC, STR_ITEM2_DESC, |
| 461 | "Lord Percy", NULL); |
| 462 | ut_assert(id > 0); |
| 463 | id = scene_txt_str(scn, "item2-key", ITEM2_KEY, STR_ITEM2_KEY, "2", |
| 464 | NULL); |
| 465 | ut_assert(id > 0); |
| 466 | id = scene_img(scn, "item2-preview", ITEM2_PREVIEW, |
| 467 | video_get_u_boot_logo(), NULL); |
| 468 | ut_assert(id > 0); |
| 469 | |
| 470 | id = scene_menuitem(scn, OBJ_MENU, "item2", ITEM2, ITEM2_KEY, |
| 471 | ITEM2_LABEL, ITEM2_DESC, ITEM2_PREVIEW, 0, NULL); |
| 472 | ut_assert(id > 0); |
| 473 | |
| 474 | ut_assertok(scene_obj_set_pos(scn, OBJ_MENU, 50, 400)); |
| 475 | |
| 476 | scn2 = expo_lookup_scene_id(exp, SCENE1); |
| 477 | ut_asserteq_ptr(scn, scn2); |
| 478 | scn2 = expo_lookup_scene_id(exp, SCENE2); |
| 479 | ut_assertnull(scn2); |
| 480 | |
| 481 | /* render without a scene */ |
| 482 | ut_asserteq(-ECHILD, expo_render(exp)); |
| 483 | |
Simon Glass | 7a96005 | 2023-06-01 10:22:52 -0600 | [diff] [blame] | 484 | ut_assertok(expo_calc_dims(exp)); |
| 485 | ut_assertok(scene_arrange(scn)); |
| 486 | |
| 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); |
| 494 | |
| 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); |
| 502 | |
| 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); |
| 510 | |
| 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); |
| 517 | |
| 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); |
| 525 | |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 526 | /* render it */ |
| 527 | expo_set_scene_id(exp, SCENE1); |
| 528 | ut_assertok(expo_render(exp)); |
| 529 | |
| 530 | /* move down */ |
| 531 | ut_assertok(expo_send_key(exp, BKEY_DOWN)); |
| 532 | |
| 533 | ut_assertok(expo_action_get(exp, &act)); |
| 534 | |
Simon Glass | 719a3c6 | 2023-06-01 10:22:56 -0600 | [diff] [blame] | 535 | ut_asserteq(EXPOACT_POINT_ITEM, act.type); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 536 | ut_asserteq(ITEM2, act.select.id); |
| 537 | ut_assertok(expo_render(exp)); |
| 538 | |
| 539 | /* make sure only the preview for the second item is shown */ |
| 540 | obj = scene_obj_find(scn, ITEM1_PREVIEW, SCENEOBJT_NONE); |
Simon Glass | 6081b0f | 2023-06-01 10:22:50 -0600 | [diff] [blame] | 541 | ut_asserteq(true, obj->flags & SCENEOF_HIDE); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 542 | |
| 543 | obj = scene_obj_find(scn, ITEM2_PREVIEW, SCENEOBJT_NONE); |
Simon Glass | 6081b0f | 2023-06-01 10:22:50 -0600 | [diff] [blame] | 544 | ut_asserteq(false, obj->flags & SCENEOF_HIDE); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 545 | |
| 546 | /* select it */ |
| 547 | ut_assertok(expo_send_key(exp, BKEY_SELECT)); |
| 548 | |
| 549 | ut_assertok(expo_action_get(exp, &act)); |
| 550 | ut_asserteq(EXPOACT_SELECT, act.type); |
| 551 | ut_asserteq(ITEM2, act.select.id); |
| 552 | |
| 553 | /* make sure the action doesn't come again */ |
| 554 | ut_asserteq(-EAGAIN, expo_action_get(exp, &act)); |
| 555 | |
| 556 | /* make sure there was no console output */ |
| 557 | ut_assert_console_end(); |
| 558 | |
| 559 | /* now try in text mode */ |
Simon Glass | b2c4034 | 2023-06-01 10:22:37 -0600 | [diff] [blame] | 560 | expo_set_text_mode(exp, true); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 561 | ut_assertok(expo_render(exp)); |
| 562 | |
| 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"); |
| 569 | |
| 570 | /* Move back up to the first item */ |
| 571 | ut_assertok(expo_send_key(exp, BKEY_UP)); |
| 572 | |
| 573 | ut_assertok(expo_action_get(exp, &act)); |
| 574 | |
Simon Glass | 719a3c6 | 2023-06-01 10:22:56 -0600 | [diff] [blame] | 575 | ut_asserteq(EXPOACT_POINT_ITEM, act.type); |
Simon Glass | 6592499 | 2023-01-06 08:52:39 -0600 | [diff] [blame] | 576 | ut_asserteq(ITEM1, act.select.id); |
| 577 | |
| 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"); |
| 585 | |
| 586 | ut_assert_console_end(); |
| 587 | |
| 588 | expo_destroy(exp); |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | BOOTSTD_TEST(expo_render_image, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |
Simon Glass | 6130072 | 2023-06-01 10:23:01 -0600 | [diff] [blame] | 593 | |
| 594 | /* Check building an expo from a devicetree description */ |
| 595 | static int expo_test_build(struct unit_test_state *uts) |
| 596 | { |
| 597 | struct scene_obj_menu *menu; |
| 598 | struct scene_menitem *item; |
| 599 | struct scene_obj_txt *txt; |
| 600 | struct scene_obj *obj; |
| 601 | struct scene *scn; |
| 602 | struct expo *exp; |
| 603 | int count; |
| 604 | ofnode node; |
| 605 | |
| 606 | node = ofnode_path("/cedit"); |
| 607 | ut_assert(ofnode_valid(node)); |
| 608 | ut_assertok(expo_build(node, &exp)); |
| 609 | |
| 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); |
| 614 | |
| 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); |
| 622 | |
| 623 | /* check the title */ |
| 624 | txt = scene_obj_find(scn, scn->title_id, SCENEOBJT_NONE); |
| 625 | ut_assertnonnull(txt); |
| 626 | obj = &txt->obj; |
| 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)); |
| 633 | |
| 634 | /* check the menu */ |
| 635 | menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_NONE); |
| 636 | obj = &menu->obj; |
| 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); |
| 642 | |
| 643 | txt = scene_obj_find(scn, menu->title_id, SCENEOBJT_NONE); |
| 644 | ut_asserteq_str("CPU speed", expo_get_str(exp, txt->str_id)); |
| 645 | |
| 646 | ut_asserteq(0, menu->cur_item_id); |
| 647 | ut_asserteq(0, menu->pointer_id); |
| 648 | |
| 649 | /* check the items */ |
| 650 | item = list_first_entry(&menu->item_head, struct scene_menitem, |
| 651 | sibling); |
| 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); |
| 658 | |
| 659 | txt = scene_obj_find(scn, item->label_id, SCENEOBJT_NONE); |
| 660 | ut_asserteq_str("2 GHz", expo_get_str(exp, txt->str_id)); |
| 661 | |
| 662 | count = 0; |
| 663 | list_for_each_entry(item, &menu->item_head, sibling) |
| 664 | count++; |
| 665 | ut_asserteq(3, count); |
| 666 | |
| 667 | expo_destroy(exp); |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | BOOTSTD_TEST(expo_test_build, UT_TESTF_DM); |
Simon Glass | b8c2655 | 2023-06-01 10:23:03 -0600 | [diff] [blame] | 672 | |
| 673 | /* Check the cedit command */ |
| 674 | static int expo_cedit(struct unit_test_state *uts) |
| 675 | { |
| 676 | extern struct expo *cur_exp; |
| 677 | struct scene_obj_menu *menu; |
| 678 | struct scene_obj_txt *txt; |
| 679 | struct expo *exp; |
| 680 | struct scene *scn; |
| 681 | |
| 682 | if (!IS_ENABLED(CONFIG_CMD_CEDIT)) |
| 683 | return -EAGAIN; |
| 684 | |
| 685 | ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0)); |
| 686 | |
| 687 | console_record_reset_enable(); |
| 688 | |
| 689 | /* |
| 690 | * ^N Move down to second menu |
| 691 | * ^M Open menu |
| 692 | * ^N Move down to second item |
| 693 | * ^M Select item |
| 694 | * \e Quit |
| 695 | */ |
| 696 | console_in_puts("\x0e\x0d\x0e\x0d\e"); |
| 697 | ut_assertok(run_command("cedit run", 0)); |
| 698 | |
| 699 | exp = cur_exp; |
| 700 | scn = expo_lookup_scene_id(exp, exp->scene_id); |
| 701 | ut_assertnonnull(scn); |
| 702 | |
| 703 | menu = scene_obj_find(scn, scn->highlight_id, SCENEOBJT_NONE); |
| 704 | ut_assertnonnull(menu); |
| 705 | |
| 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)); |
| 709 | |
| 710 | ut_asserteq(ID_AC_ON, menu->cur_item_id); |
| 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | BOOTSTD_TEST(expo_cedit, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |