blob: 3c0bc78bb7a4b254f4515fd5bb24b53cc3991aa0 [file] [log] [blame]
Simon Glassfe93c142023-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 Glassdef898c2023-06-01 10:22:27 -060031 STR_SCENE_TITLE,
32
Simon Glassfe93c142023-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 Glassdef898c2023-06-01 10:22:27 -0600125 int id, title_id;
Simon Glassfe93c142023-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 Glassdef898c2023-06-01 10:22:27 -0600146 title_id = expo_str(exp, "title", STR_SCENE_TITLE, SCENE_TITLE);
147 ut_assert(title_id >= 0);
Simon Glassfe93c142023-01-06 08:52:39 -0600148
Simon Glassdef898c2023-06-01 10:22:27 -0600149 /* Use an allocated ID - this will be allocated after the title str */
Simon Glassfe93c142023-01-06 08:52:39 -0600150 scn = NULL;
151 id = scene_new(exp, SCENE_NAME2, 0, &scn);
152 ut_assertnonnull(scn);
Simon Glassdef898c2023-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 Glassfe93c142023-01-06 08:52:39 -0600156 ut_asserteq_ptr(exp, scn->expo);
157
158 ut_asserteq_str(SCENE_NAME2, scn->name);
Simon Glassdef898c2023-06-01 10:22:27 -0600159 ut_asserteq(title_id, scn->title_id);
Simon Glassfe93c142023-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));
253 ut_asserteq(123, img->obj.x);
254 ut_asserteq(456, img->obj.y);
255
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));
310 ut_asserteq(50, menu->obj.x);
311 ut_asserteq(400, menu->obj.y);
312
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
351 /* adding an item should cause the first item to become current */
352 ut_asserteq(id, menu->cur_item_id);
353
354 /* the title should be at the top */
355 ut_asserteq(menu->obj.x, tit->obj.x);
356 ut_asserteq(menu->obj.y, tit->obj.y);
357
358 /* the first item should be next */
359 ut_asserteq(menu->obj.x, name1->obj.x);
360 ut_asserteq(menu->obj.y + 32, name1->obj.y);
361
362 ut_asserteq(menu->obj.x + 230, key1->obj.x);
363 ut_asserteq(menu->obj.y + 32, key1->obj.y);
364
365 ut_asserteq(menu->obj.x + 200, ptr->obj.x);
366 ut_asserteq(menu->obj.y + 32, ptr->obj.y);
367
368 ut_asserteq(menu->obj.x + 280, desc1->obj.x);
369 ut_asserteq(menu->obj.y + 32, desc1->obj.y);
370
371 ut_asserteq(-4, prev1->obj.x);
372 ut_asserteq(menu->obj.y + 32, prev1->obj.y);
373 ut_asserteq(false, prev1->obj.hide);
374
375 expo_destroy(exp);
376
377 ut_assertok(ut_check_delta(start_mem));
378
379 return 0;
380}
381BOOTSTD_TEST(expo_object_menu, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
382
383/* Check rendering a scene */
384static int expo_render_image(struct unit_test_state *uts)
385{
386 struct scene_obj_menu *menu;
387 struct scene *scn, *scn2;
388 struct expo_action act;
389 struct scene_obj *obj;
390 struct udevice *dev;
391 struct expo *exp;
392 int id;
393
394 console_record_reset_enable();
395 ut_assertok(uclass_first_device_err(UCLASS_VIDEO, &dev));
396
397 ut_assertok(expo_new(EXPO_NAME, NULL, &exp));
398 id = scene_new(exp, SCENE_NAME1, SCENE1, &scn);
399 ut_assert(id > 0);
400 ut_assertok(expo_set_display(exp, dev));
401
402 id = scene_img(scn, "logo", OBJ_LOGO, video_get_u_boot_logo(), NULL);
403 ut_assert(id > 0);
404 ut_assertok(scene_obj_set_pos(scn, OBJ_LOGO, 50, 20));
405
406 id = scene_txt_str(scn, "text", OBJ_TEXT, STR_TEXT, "my string", NULL);
407 ut_assert(id > 0);
408 ut_assertok(scene_txt_set_font(scn, OBJ_TEXT, "cantoraone_regular",
409 40));
410 ut_assertok(scene_obj_set_pos(scn, OBJ_TEXT, 400, 100));
411
412 id = scene_txt_str(scn, "text", OBJ_TEXT2, STR_TEXT2, "another string",
413 NULL);
414 ut_assert(id > 0);
415 ut_assertok(scene_txt_set_font(scn, OBJ_TEXT2, "nimbus_sans_l_regular",
416 60));
417 ut_assertok(scene_obj_set_pos(scn, OBJ_TEXT2, 200, 600));
418
419 id = scene_menu(scn, "main", OBJ_MENU, &menu);
420 ut_assert(id > 0);
421
422 id = scene_txt_str(scn, "title", OBJ_MENU_TITLE, STR_MENU_TITLE,
423 "Main Menu", NULL);
424 ut_assert(id > 0);
425 ut_assertok(scene_menu_set_title(scn, OBJ_MENU, OBJ_MENU_TITLE));
426
427 id = scene_txt_str(scn, "cur_item", POINTER_TEXT, STR_POINTER_TEXT, ">",
428 NULL);
429 ut_assert(id > 0);
430 ut_assertok(scene_menu_set_pointer(scn, OBJ_MENU, POINTER_TEXT));
431
432 id = scene_txt_str(scn, "label1", ITEM1_LABEL, STR_ITEM1_LABEL, "Play",
433 NULL);
434 ut_assert(id > 0);
435 id = scene_txt_str(scn, "item1 txt", ITEM1_DESC, STR_ITEM1_DESC,
436 "Lord Melchett", NULL);
437 ut_assert(id > 0);
438 id = scene_txt_str(scn, "item1-key", ITEM1_KEY, STR_ITEM1_KEY, "1",
439 NULL);
440 ut_assert(id > 0);
441 id = scene_img(scn, "item1-preview", ITEM1_PREVIEW,
442 video_get_u_boot_logo(), NULL);
443 id = scene_menuitem(scn, OBJ_MENU, "item1", ITEM1, ITEM1_KEY,
444 ITEM1_LABEL, ITEM1_DESC, ITEM1_PREVIEW, 0, NULL);
445 ut_assert(id > 0);
446
447 id = scene_txt_str(scn, "label2", ITEM2_LABEL, STR_ITEM2_LABEL, "Now",
448 NULL);
449 ut_assert(id > 0);
450 id = scene_txt_str(scn, "item2 txt", ITEM2_DESC, STR_ITEM2_DESC,
451 "Lord Percy", NULL);
452 ut_assert(id > 0);
453 id = scene_txt_str(scn, "item2-key", ITEM2_KEY, STR_ITEM2_KEY, "2",
454 NULL);
455 ut_assert(id > 0);
456 id = scene_img(scn, "item2-preview", ITEM2_PREVIEW,
457 video_get_u_boot_logo(), NULL);
458 ut_assert(id > 0);
459
460 id = scene_menuitem(scn, OBJ_MENU, "item2", ITEM2, ITEM2_KEY,
461 ITEM2_LABEL, ITEM2_DESC, ITEM2_PREVIEW, 0, NULL);
462 ut_assert(id > 0);
463
464 ut_assertok(scene_obj_set_pos(scn, OBJ_MENU, 50, 400));
465
466 scn2 = expo_lookup_scene_id(exp, SCENE1);
467 ut_asserteq_ptr(scn, scn2);
468 scn2 = expo_lookup_scene_id(exp, SCENE2);
469 ut_assertnull(scn2);
470
471 /* render without a scene */
472 ut_asserteq(-ECHILD, expo_render(exp));
473
474 /* render it */
475 expo_set_scene_id(exp, SCENE1);
476 ut_assertok(expo_render(exp));
477
478 /* move down */
479 ut_assertok(expo_send_key(exp, BKEY_DOWN));
480
481 ut_assertok(expo_action_get(exp, &act));
482
483 ut_asserteq(EXPOACT_POINT, act.type);
484 ut_asserteq(ITEM2, act.select.id);
485 ut_assertok(expo_render(exp));
486
487 /* make sure only the preview for the second item is shown */
488 obj = scene_obj_find(scn, ITEM1_PREVIEW, SCENEOBJT_NONE);
489 ut_asserteq(true, obj->hide);
490
491 obj = scene_obj_find(scn, ITEM2_PREVIEW, SCENEOBJT_NONE);
492 ut_asserteq(false, obj->hide);
493
494 /* select it */
495 ut_assertok(expo_send_key(exp, BKEY_SELECT));
496
497 ut_assertok(expo_action_get(exp, &act));
498 ut_asserteq(EXPOACT_SELECT, act.type);
499 ut_asserteq(ITEM2, act.select.id);
500
501 /* make sure the action doesn't come again */
502 ut_asserteq(-EAGAIN, expo_action_get(exp, &act));
503
504 /* make sure there was no console output */
505 ut_assert_console_end();
506
507 /* now try in text mode */
508 exp_set_text_mode(exp, true);
509 ut_assertok(expo_render(exp));
510
511 ut_assert_nextline("U-Boot : Boot Menu");
512 ut_assert_nextline("%s", "");
513 ut_assert_nextline("Main Menu");
514 ut_assert_nextline("%s", "");
515 ut_assert_nextline(" 1 Play Lord Melchett");
516 ut_assert_nextline(" > 2 Now Lord Percy");
517
518 /* Move back up to the first item */
519 ut_assertok(expo_send_key(exp, BKEY_UP));
520
521 ut_assertok(expo_action_get(exp, &act));
522
523 ut_asserteq(EXPOACT_POINT, act.type);
524 ut_asserteq(ITEM1, act.select.id);
525
526 ut_assertok(expo_render(exp));
527 ut_assert_nextline("U-Boot : Boot Menu");
528 ut_assert_nextline("%s", "");
529 ut_assert_nextline("Main Menu");
530 ut_assert_nextline("%s", "");
531 ut_assert_nextline(" > 1 Play Lord Melchett");
532 ut_assert_nextline(" 2 Now Lord Percy");
533
534 ut_assert_console_end();
535
536 expo_destroy(exp);
537
538 return 0;
539}
540BOOTSTD_TEST(expo_render_image, UT_TESTF_DM | UT_TESTF_SCAN_FDT);