blob: 10cb7b246f3f1aa08129e11d7cc88bff00a2dcda [file] [log] [blame]
Simon Glass65924992023-01-06 08:52:39 -06001// 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>
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"
16#include "../../boot/scene_internal.h"
17
18enum {
19 /* scenes */
20 SCENE1 = 7,
21 SCENE2,
22
23 /* objects */
24 OBJ_LOGO,
25 OBJ_TEXT,
26 OBJ_TEXT2,
27 OBJ_MENU,
28 OBJ_MENU_TITLE,
29
30 /* strings */
Simon Glassea274b62023-06-01 10:22:27 -060031 STR_SCENE_TITLE,
32
Simon Glass65924992023-01-06 08:52:39 -060033 STR_TEXT,
34 STR_TEXT2,
35 STR_MENU_TITLE,
36 STR_POINTER_TEXT,
37
38 STR_ITEM1_LABEL,
39 STR_ITEM1_DESC,
40 STR_ITEM1_KEY,
41 STR_ITEM1_PREVIEW,
42
43 STR_ITEM2_LABEL,
44 STR_ITEM2_DESC,
45 STR_ITEM2_KEY,
46 STR_ITEM2_PREVIEW,
47
48 /* menu items */
49 ITEM1,
50 ITEM1_LABEL,
51 ITEM1_DESC,
52 ITEM1_KEY,
53 ITEM1_PREVIEW,
54
55 ITEM2,
56 ITEM2_LABEL,
57 ITEM2_DESC,
58 ITEM2_KEY,
59 ITEM2_PREVIEW,
60
61 /* pointer to current item */
62 POINTER_TEXT,
63};
64
65#define BAD_POINTER ((void *)1)
66
67/* names for various things */
68#define EXPO_NAME "my menus"
69#define SCENE_NAME1 "main"
70#define SCENE_NAME2 "second"
71#define SCENE_TITLE "Main Menu"
72#define LOGO_NAME "logo"
73
74/* Check base expo support */
75static int expo_base(struct unit_test_state *uts)
76{
77 struct udevice *dev;
78 struct expo *exp;
79 ulong start_mem;
80 char name[100];
81 int i;
82
83 ut_assertok(uclass_first_device_err(UCLASS_VIDEO, &dev));
84
85 start_mem = ut_check_free();
86
87 exp = NULL;
88 strcpy(name, EXPO_NAME);
89 ut_assertok(expo_new(name, NULL, &exp));
90 *name = '\0';
91 ut_assertnonnull(exp);
92 ut_asserteq(0, exp->scene_id);
93 ut_asserteq(0, exp->next_id);
94
95 /* Make sure the name was allocated */
96 ut_assertnonnull(exp->name);
97 ut_asserteq_str(EXPO_NAME, exp->name);
98
99 ut_assertok(expo_set_display(exp, dev));
100 expo_destroy(exp);
101 ut_assertok(ut_check_delta(start_mem));
102
103 /* test handling out-of-memory conditions */
104 for (i = 0; i < 2; i++) {
105 struct expo *exp2;
106
107 malloc_enable_testing(i);
108 exp2 = BAD_POINTER;
109 ut_asserteq(-ENOMEM, expo_new(EXPO_NAME, NULL, &exp2));
110 ut_asserteq_ptr(BAD_POINTER, exp2);
111 malloc_disable_testing();
112 }
113
114 return 0;
115}
116BOOTSTD_TEST(expo_base, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
117
118/* Check creating a scene */
119static int expo_scene(struct unit_test_state *uts)
120{
121 struct scene *scn;
122 struct expo *exp;
123 ulong start_mem;
124 char name[100];
Simon Glassea274b62023-06-01 10:22:27 -0600125 int id, title_id;
Simon Glass65924992023-01-06 08:52:39 -0600126
127 start_mem = ut_check_free();
128
129 ut_assertok(expo_new(EXPO_NAME, NULL, &exp));
130
131 scn = NULL;
132 ut_asserteq(0, exp->next_id);
133 strcpy(name, SCENE_NAME1);
134 id = scene_new(exp, name, SCENE1, &scn);
135 *name = '\0';
136 ut_assertnonnull(scn);
137 ut_asserteq(SCENE1, id);
138 ut_asserteq(SCENE1 + 1, exp->next_id);
139 ut_asserteq_ptr(exp, scn->expo);
140
141 /* Make sure the name was allocated */
142 ut_assertnonnull(scn->name);
143 ut_asserteq_str(SCENE_NAME1, scn->name);
144
145 /* Set the title */
Simon Glassea274b62023-06-01 10:22:27 -0600146 title_id = expo_str(exp, "title", STR_SCENE_TITLE, SCENE_TITLE);
147 ut_assert(title_id >= 0);
Simon Glass65924992023-01-06 08:52:39 -0600148
Simon Glassea274b62023-06-01 10:22:27 -0600149 /* Use an allocated ID - this will be allocated after the title str */
Simon Glass65924992023-01-06 08:52:39 -0600150 scn = NULL;
151 id = scene_new(exp, SCENE_NAME2, 0, &scn);
152 ut_assertnonnull(scn);
Simon Glassea274b62023-06-01 10:22:27 -0600153 ut_assertok(scene_title_set(scn, title_id));
154 ut_asserteq(STR_SCENE_TITLE + 1, id);
155 ut_asserteq(STR_SCENE_TITLE + 2, exp->next_id);
Simon Glass65924992023-01-06 08:52:39 -0600156 ut_asserteq_ptr(exp, scn->expo);
157
158 ut_asserteq_str(SCENE_NAME2, scn->name);
Simon Glassea274b62023-06-01 10:22:27 -0600159 ut_asserteq(title_id, scn->title_id);
Simon Glass65924992023-01-06 08:52:39 -0600160
161 expo_destroy(exp);
162
163 ut_assertok(ut_check_delta(start_mem));
164
165 return 0;
166}
167BOOTSTD_TEST(expo_scene, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
168
169/* Check creating a scene with objects */
170static int expo_object(struct unit_test_state *uts)
171{
172 struct scene_obj_img *img;
173 struct scene_obj_txt *txt;
174 struct scene *scn;
175 struct expo *exp;
176 ulong start_mem;
177 char name[100];
178 char *data;
179 int id;
180
181 start_mem = ut_check_free();
182
183 ut_assertok(expo_new(EXPO_NAME, NULL, &exp));
184 id = scene_new(exp, SCENE_NAME1, SCENE1, &scn);
185 ut_assert(id > 0);
186
187 ut_asserteq(0, scene_obj_count(scn));
188
189 data = NULL;
190 strcpy(name, LOGO_NAME);
191 id = scene_img(scn, name, OBJ_LOGO, data, &img);
192 ut_assert(id > 0);
193 *name = '\0';
194 ut_assertnonnull(img);
195 ut_asserteq(OBJ_LOGO, id);
196 ut_asserteq(OBJ_LOGO + 1, exp->next_id);
197 ut_asserteq_ptr(scn, img->obj.scene);
198 ut_asserteq(SCENEOBJT_IMAGE, img->obj.type);
199
200 ut_asserteq_ptr(data, img->data);
201
202 /* Make sure the name was allocated */
203 ut_assertnonnull(scn->name);
204 ut_asserteq_str(SCENE_NAME1, scn->name);
205
206 ut_asserteq(1, scene_obj_count(scn));
207
208 id = scene_txt_str(scn, "text", OBJ_TEXT, STR_TEXT, "my string", &txt);
209 ut_assert(id > 0);
210 ut_assertnonnull(txt);
211 ut_asserteq(OBJ_TEXT, id);
212 ut_asserteq(SCENEOBJT_TEXT, txt->obj.type);
213 ut_asserteq(2, scene_obj_count(scn));
214
215 /* Check passing NULL as the final parameter */
216 id = scene_txt_str(scn, "text2", OBJ_TEXT2, STR_TEXT2, "another string",
217 NULL);
218 ut_assert(id > 0);
219 ut_asserteq(3, scene_obj_count(scn));
220
221 expo_destroy(exp);
222
223 ut_assertok(ut_check_delta(start_mem));
224
225 return 0;
226}
227BOOTSTD_TEST(expo_object, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
228
229/* Check setting object attributes */
230static int expo_object_attr(struct unit_test_state *uts)
231{
232 struct scene_obj_menu *menu;
233 struct scene_obj_img *img;
234 struct scene_obj_txt *txt;
235 struct scene *scn;
236 struct expo *exp;
237 ulong start_mem;
238 char name[100];
239 char *data;
240 int id;
241
242 start_mem = ut_check_free();
243
244 ut_assertok(expo_new(EXPO_NAME, NULL, &exp));
245 id = scene_new(exp, SCENE_NAME1, SCENE1, &scn);
246 ut_assert(id > 0);
247
248 data = NULL;
249 id = scene_img(scn, LOGO_NAME, OBJ_LOGO, data, &img);
250 ut_assert(id > 0);
251
252 ut_assertok(scene_obj_set_pos(scn, OBJ_LOGO, 123, 456));
Simon Glass7b043952023-06-01 10:22:49 -0600253 ut_asserteq(123, img->obj.dim.x);
254 ut_asserteq(456, img->obj.dim.y);
Simon Glass65924992023-01-06 08:52:39 -0600255
256 ut_asserteq(-ENOENT, scene_obj_set_pos(scn, OBJ_TEXT2, 0, 0));
257
258 id = scene_txt_str(scn, "text", OBJ_TEXT, STR_TEXT, "my string", &txt);
259 ut_assert(id > 0);
260
261 strcpy(name, "font2");
262 ut_assertok(scene_txt_set_font(scn, OBJ_TEXT, name, 42));
263 ut_asserteq_ptr(name, txt->font_name);
264 ut_asserteq(42, txt->font_size);
265
266 ut_asserteq(-ENOENT, scene_txt_set_font(scn, OBJ_TEXT2, name, 42));
267
268 id = scene_menu(scn, "main", OBJ_MENU, &menu);
269 ut_assert(id > 0);
270
271 ut_assertok(scene_menu_set_title(scn, OBJ_MENU, OBJ_TEXT));
272
273 ut_asserteq(-ENOENT, scene_menu_set_title(scn, OBJ_TEXT2, OBJ_TEXT));
274 ut_asserteq(-EINVAL, scene_menu_set_title(scn, OBJ_MENU, OBJ_TEXT2));
275
276 expo_destroy(exp);
277
278 ut_assertok(ut_check_delta(start_mem));
279
280 return 0;
281}
282BOOTSTD_TEST(expo_object_attr, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
283
284/* Check creating a scene with a menu */
285static int expo_object_menu(struct unit_test_state *uts)
286{
287 struct scene_obj_menu *menu;
288 struct scene_menitem *item;
289 int id, label_id, desc_id, key_id, pointer_id, preview_id;
290 struct scene_obj_txt *ptr, *name1, *desc1, *key1, *tit, *prev1;
291 struct scene *scn;
292 struct expo *exp;
293 ulong start_mem;
294
295 start_mem = ut_check_free();
296
297 ut_assertok(expo_new(EXPO_NAME, NULL, &exp));
298 id = scene_new(exp, SCENE_NAME1, SCENE1, &scn);
299 ut_assert(id > 0);
300
301 id = scene_menu(scn, "main", OBJ_MENU, &menu);
302 ut_assert(id > 0);
303 ut_assertnonnull(menu);
304 ut_asserteq(OBJ_MENU, id);
305 ut_asserteq(SCENEOBJT_MENU, menu->obj.type);
306 ut_asserteq(0, menu->title_id);
307 ut_asserteq(0, menu->pointer_id);
308
309 ut_assertok(scene_obj_set_pos(scn, OBJ_MENU, 50, 400));
Simon Glass7b043952023-06-01 10:22:49 -0600310 ut_asserteq(50, menu->obj.dim.x);
311 ut_asserteq(400, menu->obj.dim.y);
Simon Glass65924992023-01-06 08:52:39 -0600312
313 id = scene_txt_str(scn, "title", OBJ_MENU_TITLE, STR_MENU_TITLE,
314 "Main Menu", &tit);
315 ut_assert(id > 0);
316 ut_assertok(scene_menu_set_title(scn, OBJ_MENU, OBJ_MENU_TITLE));
317 ut_asserteq(OBJ_MENU_TITLE, menu->title_id);
318
319 pointer_id = scene_txt_str(scn, "cur_item", POINTER_TEXT,
320 STR_POINTER_TEXT, ">", &ptr);
321 ut_assert(pointer_id > 0);
322
323 ut_assertok(scene_menu_set_pointer(scn, OBJ_MENU, POINTER_TEXT));
324 ut_asserteq(POINTER_TEXT, menu->pointer_id);
325
326 label_id = scene_txt_str(scn, "label1", ITEM1_LABEL, STR_ITEM1_LABEL,
327 "Play", &name1);
328 ut_assert(label_id > 0);
329
330 desc_id = scene_txt_str(scn, "desc1", ITEM1_DESC, STR_ITEM1_DESC,
331 "Lord Melchett", &desc1);
332 ut_assert(desc_id > 0);
333
334 key_id = scene_txt_str(scn, "item1-key", ITEM1_KEY, STR_ITEM1_KEY, "1",
335 &key1);
336 ut_assert(key_id > 0);
337
338 preview_id = scene_txt_str(scn, "item1-preview", ITEM1_PREVIEW,
339 STR_ITEM1_PREVIEW, "(preview1)", &prev1);
340 ut_assert(preview_id > 0);
341
342 id = scene_menuitem(scn, OBJ_MENU, "linux", ITEM1, ITEM1_KEY,
343 ITEM1_LABEL, ITEM1_DESC, ITEM1_PREVIEW, 0, &item);
344 ut_asserteq(ITEM1, id);
345 ut_asserteq(id, item->id);
346 ut_asserteq(key_id, item->key_id);
347 ut_asserteq(label_id, item->label_id);
348 ut_asserteq(desc_id, item->desc_id);
349 ut_asserteq(preview_id, item->preview_id);
350
Simon Glassd7e32a82023-06-01 10:22:35 -0600351 ut_assertok(scene_arrange(scn));
352
353 /* arranging the scene should cause the first item to become current */
Simon Glass65924992023-01-06 08:52:39 -0600354 ut_asserteq(id, menu->cur_item_id);
355
356 /* the title should be at the top */
Simon Glass7b043952023-06-01 10:22:49 -0600357 ut_asserteq(menu->obj.dim.x, tit->obj.dim.x);
358 ut_asserteq(menu->obj.dim.y, tit->obj.dim.y);
Simon Glass65924992023-01-06 08:52:39 -0600359
360 /* the first item should be next */
Simon Glass7b043952023-06-01 10:22:49 -0600361 ut_asserteq(menu->obj.dim.x, name1->obj.dim.x);
362 ut_asserteq(menu->obj.dim.y + 32, name1->obj.dim.y);
Simon Glass65924992023-01-06 08:52:39 -0600363
Simon Glass7b043952023-06-01 10:22:49 -0600364 ut_asserteq(menu->obj.dim.x + 230, key1->obj.dim.x);
365 ut_asserteq(menu->obj.dim.y + 32, key1->obj.dim.y);
Simon Glass65924992023-01-06 08:52:39 -0600366
Simon Glass7b043952023-06-01 10:22:49 -0600367 ut_asserteq(menu->obj.dim.x + 200, ptr->obj.dim.x);
368 ut_asserteq(menu->obj.dim.y + 32, ptr->obj.dim.y);
Simon Glass65924992023-01-06 08:52:39 -0600369
Simon Glass7b043952023-06-01 10:22:49 -0600370 ut_asserteq(menu->obj.dim.x + 280, desc1->obj.dim.x);
371 ut_asserteq(menu->obj.dim.y + 32, desc1->obj.dim.y);
Simon Glass65924992023-01-06 08:52:39 -0600372
Simon Glass7b043952023-06-01 10:22:49 -0600373 ut_asserteq(-4, prev1->obj.dim.x);
374 ut_asserteq(menu->obj.dim.y + 32, prev1->obj.dim.y);
Simon Glass65924992023-01-06 08:52:39 -0600375 ut_asserteq(false, prev1->obj.hide);
376
377 expo_destroy(exp);
378
379 ut_assertok(ut_check_delta(start_mem));
380
381 return 0;
382}
383BOOTSTD_TEST(expo_object_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
384
385/* Check rendering a scene */
386static int expo_render_image(struct unit_test_state *uts)
387{
388 struct scene_obj_menu *menu;
389 struct scene *scn, *scn2;
390 struct expo_action act;
391 struct scene_obj *obj;
392 struct udevice *dev;
393 struct expo *exp;
394 int id;
395
396 console_record_reset_enable();
397 ut_assertok(uclass_first_device_err(UCLASS_VIDEO, &dev));
398
399 ut_assertok(expo_new(EXPO_NAME, NULL, &exp));
400 id = scene_new(exp, SCENE_NAME1, SCENE1, &scn);
401 ut_assert(id > 0);
402 ut_assertok(expo_set_display(exp, dev));
403
404 id = scene_img(scn, "logo", OBJ_LOGO, video_get_u_boot_logo(), NULL);
405 ut_assert(id > 0);
406 ut_assertok(scene_obj_set_pos(scn, OBJ_LOGO, 50, 20));
407
408 id = scene_txt_str(scn, "text", OBJ_TEXT, STR_TEXT, "my string", NULL);
409 ut_assert(id > 0);
410 ut_assertok(scene_txt_set_font(scn, OBJ_TEXT, "cantoraone_regular",
411 40));
412 ut_assertok(scene_obj_set_pos(scn, OBJ_TEXT, 400, 100));
413
414 id = scene_txt_str(scn, "text", OBJ_TEXT2, STR_TEXT2, "another string",
415 NULL);
416 ut_assert(id > 0);
417 ut_assertok(scene_txt_set_font(scn, OBJ_TEXT2, "nimbus_sans_l_regular",
418 60));
419 ut_assertok(scene_obj_set_pos(scn, OBJ_TEXT2, 200, 600));
420
421 id = scene_menu(scn, "main", OBJ_MENU, &menu);
422 ut_assert(id > 0);
423
424 id = scene_txt_str(scn, "title", OBJ_MENU_TITLE, STR_MENU_TITLE,
425 "Main Menu", NULL);
426 ut_assert(id > 0);
427 ut_assertok(scene_menu_set_title(scn, OBJ_MENU, OBJ_MENU_TITLE));
428
429 id = scene_txt_str(scn, "cur_item", POINTER_TEXT, STR_POINTER_TEXT, ">",
430 NULL);
431 ut_assert(id > 0);
432 ut_assertok(scene_menu_set_pointer(scn, OBJ_MENU, POINTER_TEXT));
433
434 id = scene_txt_str(scn, "label1", ITEM1_LABEL, STR_ITEM1_LABEL, "Play",
435 NULL);
436 ut_assert(id > 0);
437 id = scene_txt_str(scn, "item1 txt", ITEM1_DESC, STR_ITEM1_DESC,
438 "Lord Melchett", NULL);
439 ut_assert(id > 0);
440 id = scene_txt_str(scn, "item1-key", ITEM1_KEY, STR_ITEM1_KEY, "1",
441 NULL);
442 ut_assert(id > 0);
443 id = scene_img(scn, "item1-preview", ITEM1_PREVIEW,
444 video_get_u_boot_logo(), NULL);
445 id = scene_menuitem(scn, OBJ_MENU, "item1", ITEM1, ITEM1_KEY,
446 ITEM1_LABEL, ITEM1_DESC, ITEM1_PREVIEW, 0, NULL);
447 ut_assert(id > 0);
448
449 id = scene_txt_str(scn, "label2", ITEM2_LABEL, STR_ITEM2_LABEL, "Now",
450 NULL);
451 ut_assert(id > 0);
452 id = scene_txt_str(scn, "item2 txt", ITEM2_DESC, STR_ITEM2_DESC,
453 "Lord Percy", NULL);
454 ut_assert(id > 0);
455 id = scene_txt_str(scn, "item2-key", ITEM2_KEY, STR_ITEM2_KEY, "2",
456 NULL);
457 ut_assert(id > 0);
458 id = scene_img(scn, "item2-preview", ITEM2_PREVIEW,
459 video_get_u_boot_logo(), NULL);
460 ut_assert(id > 0);
461
462 id = scene_menuitem(scn, OBJ_MENU, "item2", ITEM2, ITEM2_KEY,
463 ITEM2_LABEL, ITEM2_DESC, ITEM2_PREVIEW, 0, NULL);
464 ut_assert(id > 0);
465
466 ut_assertok(scene_obj_set_pos(scn, OBJ_MENU, 50, 400));
467
468 scn2 = expo_lookup_scene_id(exp, SCENE1);
469 ut_asserteq_ptr(scn, scn2);
470 scn2 = expo_lookup_scene_id(exp, SCENE2);
471 ut_assertnull(scn2);
472
473 /* render without a scene */
474 ut_asserteq(-ECHILD, expo_render(exp));
475
476 /* render it */
477 expo_set_scene_id(exp, SCENE1);
478 ut_assertok(expo_render(exp));
479
480 /* move down */
481 ut_assertok(expo_send_key(exp, BKEY_DOWN));
482
483 ut_assertok(expo_action_get(exp, &act));
484
485 ut_asserteq(EXPOACT_POINT, act.type);
486 ut_asserteq(ITEM2, act.select.id);
487 ut_assertok(expo_render(exp));
488
489 /* make sure only the preview for the second item is shown */
490 obj = scene_obj_find(scn, ITEM1_PREVIEW, SCENEOBJT_NONE);
491 ut_asserteq(true, obj->hide);
492
493 obj = scene_obj_find(scn, ITEM2_PREVIEW, SCENEOBJT_NONE);
494 ut_asserteq(false, obj->hide);
495
496 /* select it */
497 ut_assertok(expo_send_key(exp, BKEY_SELECT));
498
499 ut_assertok(expo_action_get(exp, &act));
500 ut_asserteq(EXPOACT_SELECT, act.type);
501 ut_asserteq(ITEM2, act.select.id);
502
503 /* make sure the action doesn't come again */
504 ut_asserteq(-EAGAIN, expo_action_get(exp, &act));
505
506 /* make sure there was no console output */
507 ut_assert_console_end();
508
509 /* now try in text mode */
Simon Glassb2c40342023-06-01 10:22:37 -0600510 expo_set_text_mode(exp, true);
Simon Glass65924992023-01-06 08:52:39 -0600511 ut_assertok(expo_render(exp));
512
513 ut_assert_nextline("U-Boot : Boot Menu");
514 ut_assert_nextline("%s", "");
515 ut_assert_nextline("Main Menu");
516 ut_assert_nextline("%s", "");
517 ut_assert_nextline(" 1 Play Lord Melchett");
518 ut_assert_nextline(" > 2 Now Lord Percy");
519
520 /* Move back up to the first item */
521 ut_assertok(expo_send_key(exp, BKEY_UP));
522
523 ut_assertok(expo_action_get(exp, &act));
524
525 ut_asserteq(EXPOACT_POINT, act.type);
526 ut_asserteq(ITEM1, act.select.id);
527
528 ut_assertok(expo_render(exp));
529 ut_assert_nextline("U-Boot : Boot Menu");
530 ut_assert_nextline("%s", "");
531 ut_assert_nextline("Main Menu");
532 ut_assert_nextline("%s", "");
533 ut_assert_nextline(" > 1 Play Lord Melchett");
534 ut_assert_nextline(" 2 Now Lord Percy");
535
536 ut_assert_console_end();
537
538 expo_destroy(exp);
539
540 return 0;
541}
542BOOTSTD_TEST(expo_render_image, UT_TESTF_DM | UT_TESTF_SCAN_FDT);