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