blob: 97ca63b3082f1515951410c615980e8a9861e0f4 [file] [log] [blame]
Simon Glass44e4ec72023-08-14 16:40:26 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2023 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
Simon Glass44e4ec72023-08-14 16:40:26 -06007#include <cedit.h>
Simon Glassee440642025-05-02 08:46:19 -06008#include <dm.h>
Simon Glass237f3752023-08-14 16:40:35 -06009#include <env.h>
Simon Glass44e4ec72023-08-14 16:40:26 -060010#include <expo.h>
Simon Glass28bf4352023-08-14 16:40:33 -060011#include <mapmem.h>
12#include <dm/ofnode.h>
Simon Glass44e4ec72023-08-14 16:40:26 -060013#include <test/ut.h>
Simon Glassee440642025-05-02 08:46:19 -060014#include <test/video.h>
Simon Glass44e4ec72023-08-14 16:40:26 -060015#include "bootstd_common.h"
16#include <test/cedit-test.h>
17#include "../../boot/scene_internal.h"
18
19/* Check the cedit command */
20static int cedit_base(struct unit_test_state *uts)
21{
22 extern struct expo *cur_exp;
23 struct scene_obj_menu *menu;
24 struct scene_obj_txt *txt;
25 struct expo *exp;
26 struct scene *scn;
27
28 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
29
Simon Glass44e4ec72023-08-14 16:40:26 -060030 /*
31 * ^N Move down to second menu
32 * ^M Open menu
33 * ^N Move down to second item
34 * ^M Select item
35 * \e Quit
Simon Glass53a0a2f2024-10-14 16:31:57 -060036 *
37 * cedit_run() returns -EACCESS so this command returns CMD_RET_FAILURE
Simon Glass44e4ec72023-08-14 16:40:26 -060038 */
39 console_in_puts("\x0e\x0d\x0e\x0d\e");
Simon Glass53a0a2f2024-10-14 16:31:57 -060040 ut_asserteq(1, run_command("cedit run", 0));
Simon Glass44e4ec72023-08-14 16:40:26 -060041
42 exp = cur_exp;
43 scn = expo_lookup_scene_id(exp, exp->scene_id);
44 ut_assertnonnull(scn);
45
46 menu = scene_obj_find(scn, scn->highlight_id, SCENEOBJT_NONE);
47 ut_assertnonnull(menu);
48
49 txt = scene_obj_find(scn, menu->title_id, SCENEOBJT_NONE);
50 ut_assertnonnull(txt);
51 ut_asserteq_str("AC Power", expo_get_str(exp, txt->str_id));
52
53 ut_asserteq(ID_AC_ON, menu->cur_item_id);
54
55 return 0;
56}
Simon Glassf0425022024-08-22 07:57:54 -060057BOOTSTD_TEST(cedit_base, UTF_CONSOLE);
Simon Glass28bf4352023-08-14 16:40:33 -060058
Simon Glassb1cd32b2023-08-14 16:40:34 -060059/* Check the cedit write_fdt and read_fdt commands */
Simon Glass28bf4352023-08-14 16:40:33 -060060static int cedit_fdt(struct unit_test_state *uts)
61{
Simon Glass70c579b2023-10-01 19:13:39 -060062 struct scene_obj_textline *tline;
Simon Glass28bf4352023-08-14 16:40:33 -060063 struct video_priv *vid_priv;
64 extern struct expo *cur_exp;
Simon Glassb1cd32b2023-08-14 16:40:34 -060065 struct scene_obj_menu *menu;
Simon Glass736bc0d2025-05-02 08:46:22 -060066 struct udevice *dev;
Simon Glass28bf4352023-08-14 16:40:33 -060067 ulong addr = 0x1000;
68 struct ofprop prop;
69 struct scene *scn;
70 oftree tree;
71 ofnode node;
Simon Glass70c579b2023-10-01 19:13:39 -060072 char *str;
Simon Glass28bf4352023-08-14 16:40:33 -060073 void *fdt;
74 int i;
75
Simon Glass736bc0d2025-05-02 08:46:22 -060076 ut_assertok(uclass_first_device_err(UCLASS_VIDEO, &dev));
77 vid_priv = dev_get_uclass_priv(dev);
78
Simon Glass28bf4352023-08-14 16:40:33 -060079 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
80
Simon Glass736bc0d2025-05-02 08:46:22 -060081 ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, dev, &scn));
Simon Glass28bf4352023-08-14 16:40:33 -060082
Simon Glassb1cd32b2023-08-14 16:40:34 -060083 /* get a menu to fiddle with */
84 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU);
85 ut_assertnonnull(menu);
86 menu->cur_item_id = ID_CPU_SPEED_2;
87
Simon Glass70c579b2023-10-01 19:13:39 -060088 /* get a textline to fiddle with too */
89 tline = scene_obj_find(scn, ID_MACHINE_NAME, SCENEOBJT_TEXTLINE);
90 ut_assertnonnull(tline);
91 str = abuf_data(&tline->buf);
92 strcpy(str, "my-machine");
93
Simon Glass28bf4352023-08-14 16:40:33 -060094 ut_assertok(run_command("cedit write_fdt hostfs - settings.dtb", 0));
95 ut_assertok(run_commandf("load hostfs - %lx settings.dtb", addr));
96 ut_assert_nextlinen("1024 bytes read");
97
98 fdt = map_sysmem(addr, 1024);
99 tree = oftree_from_fdt(fdt);
100 node = ofnode_find_subnode(oftree_root(tree), CEDIT_NODE_NAME);
Simon Glass70c579b2023-10-01 19:13:39 -0600101 ut_assert(ofnode_valid(node));
Simon Glass28bf4352023-08-14 16:40:33 -0600102
Simon Glassb1cd32b2023-08-14 16:40:34 -0600103 ut_asserteq(ID_CPU_SPEED_2,
Simon Glass28bf4352023-08-14 16:40:33 -0600104 ofnode_read_u32_default(node, "cpu-speed", 0));
Simon Glass6f3e87a2024-10-14 16:32:00 -0600105 ut_asserteq(3,
106 ofnode_read_u32_default(node, "cpu-speed-value", 0));
Simon Glassb1cd32b2023-08-14 16:40:34 -0600107 ut_asserteq_str("2.5 GHz", ofnode_read_string(node, "cpu-speed-str"));
Simon Glass70c579b2023-10-01 19:13:39 -0600108 ut_asserteq_str("my-machine", ofnode_read_string(node, "machine-name"));
Simon Glass28bf4352023-08-14 16:40:33 -0600109
Simon Glass6f3e87a2024-10-14 16:32:00 -0600110 /* There should only be 7 properties */
Simon Glass28bf4352023-08-14 16:40:33 -0600111 for (i = 0, ofnode_first_property(node, &prop); ofprop_valid(&prop);
112 i++, ofnode_next_property(&prop))
113 ;
Simon Glass6f3e87a2024-10-14 16:32:00 -0600114 ut_asserteq(7, i);
Simon Glass28bf4352023-08-14 16:40:33 -0600115
Simon Glassb1cd32b2023-08-14 16:40:34 -0600116 ut_assert_console_end();
117
118 /* reset the expo */
119 menu->cur_item_id = ID_CPU_SPEED_1;
Simon Glass70c579b2023-10-01 19:13:39 -0600120 *str = '\0';
Simon Glassb1cd32b2023-08-14 16:40:34 -0600121
122 /* load in the settings and make sure they update */
123 ut_assertok(run_command("cedit read_fdt hostfs - settings.dtb", 0));
124 ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id);
Simon Glass70c579b2023-10-01 19:13:39 -0600125 ut_asserteq_str("my-machine", ofnode_read_string(node, "machine-name"));
Simon Glassb1cd32b2023-08-14 16:40:34 -0600126
127 ut_assertnonnull(menu);
Simon Glass28bf4352023-08-14 16:40:33 -0600128 ut_assert_console_end();
129
130 return 0;
131}
Simon Glassf0425022024-08-22 07:57:54 -0600132BOOTSTD_TEST(cedit_fdt, UTF_CONSOLE);
Simon Glass237f3752023-08-14 16:40:35 -0600133
Simon Glass0f2e5a62023-08-14 16:40:36 -0600134/* Check the cedit write_env and read_env commands */
Simon Glass237f3752023-08-14 16:40:35 -0600135static int cedit_env(struct unit_test_state *uts)
136{
Simon Glass70c579b2023-10-01 19:13:39 -0600137 struct scene_obj_textline *tline;
Simon Glass237f3752023-08-14 16:40:35 -0600138 struct video_priv *vid_priv;
139 extern struct expo *cur_exp;
140 struct scene_obj_menu *menu;
Simon Glass736bc0d2025-05-02 08:46:22 -0600141 struct udevice *dev;
Simon Glass237f3752023-08-14 16:40:35 -0600142 struct scene *scn;
Simon Glass70c579b2023-10-01 19:13:39 -0600143 char *str;
Simon Glass237f3752023-08-14 16:40:35 -0600144
Simon Glass237f3752023-08-14 16:40:35 -0600145 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
146
Simon Glass736bc0d2025-05-02 08:46:22 -0600147 ut_assertok(uclass_first_device_err(UCLASS_VIDEO, &dev));
148 vid_priv = dev_get_uclass_priv(dev);
149
150 ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, dev, &scn));
Simon Glass237f3752023-08-14 16:40:35 -0600151
152 /* get a menu to fiddle with */
153 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU);
154 ut_assertnonnull(menu);
155 menu->cur_item_id = ID_CPU_SPEED_2;
156
Simon Glass70c579b2023-10-01 19:13:39 -0600157 /* get a textline to fiddle with too */
158 tline = scene_obj_find(scn, ID_MACHINE_NAME, SCENEOBJT_TEXTLINE);
159 ut_assertnonnull(tline);
160 str = abuf_data(&tline->buf);
161 strcpy(str, "my-machine");
162
Simon Glass237f3752023-08-14 16:40:35 -0600163 ut_assertok(run_command("cedit write_env -v", 0));
Simon Glass53a0a2f2024-10-14 16:31:57 -0600164 ut_assert_nextlinen("c.cpu-speed=11");
Simon Glass237f3752023-08-14 16:40:35 -0600165 ut_assert_nextlinen("c.cpu-speed-str=2.5 GHz");
Simon Glass6f3e87a2024-10-14 16:32:00 -0600166 ut_assert_nextlinen("c.cpu-speed-value=3");
Simon Glass53a0a2f2024-10-14 16:31:57 -0600167 ut_assert_nextlinen("c.power-loss=14");
Simon Glass237f3752023-08-14 16:40:35 -0600168 ut_assert_nextlinen("c.power-loss-str=Always Off");
Simon Glass6f3e87a2024-10-14 16:32:00 -0600169 ut_assert_nextlinen("c.power-loss-value=0");
Simon Glass70c579b2023-10-01 19:13:39 -0600170 ut_assert_nextlinen("c.machine-name=my-machine");
Simon Glass237f3752023-08-14 16:40:35 -0600171 ut_assert_console_end();
172
Simon Glass53a0a2f2024-10-14 16:31:57 -0600173 ut_asserteq(11, env_get_ulong("c.cpu-speed", 10, 0));
Simon Glass237f3752023-08-14 16:40:35 -0600174 ut_asserteq_str("2.5 GHz", env_get("c.cpu-speed-str"));
Simon Glass70c579b2023-10-01 19:13:39 -0600175 ut_asserteq_str("my-machine", env_get("c.machine-name"));
Simon Glass237f3752023-08-14 16:40:35 -0600176
Simon Glass0f2e5a62023-08-14 16:40:36 -0600177 /* reset the expo */
178 menu->cur_item_id = ID_CPU_SPEED_1;
Simon Glass70c579b2023-10-01 19:13:39 -0600179 *str = '\0';
Simon Glass0f2e5a62023-08-14 16:40:36 -0600180
181 ut_assertok(run_command("cedit read_env -v", 0));
Simon Glass53a0a2f2024-10-14 16:31:57 -0600182 ut_assert_nextlinen("c.cpu-speed=11");
183 ut_assert_nextlinen("c.power-loss=14");
Simon Glass70c579b2023-10-01 19:13:39 -0600184 ut_assert_nextlinen("c.machine-name=my-machine");
Simon Glass0f2e5a62023-08-14 16:40:36 -0600185 ut_assert_console_end();
186
187 ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id);
Simon Glass70c579b2023-10-01 19:13:39 -0600188 ut_asserteq_str("my-machine", env_get("c.machine-name"));
Simon Glass0f2e5a62023-08-14 16:40:36 -0600189
Simon Glass237f3752023-08-14 16:40:35 -0600190 return 0;
191}
Simon Glassf0425022024-08-22 07:57:54 -0600192BOOTSTD_TEST(cedit_env, UTF_CONSOLE);
Simon Glass2b91ca62023-08-14 16:40:37 -0600193
194/* Check the cedit write_cmos and read_cmos commands */
195static int cedit_cmos(struct unit_test_state *uts)
196{
197 struct scene_obj_menu *menu, *menu2;
198 struct video_priv *vid_priv;
199 extern struct expo *cur_exp;
Simon Glass736bc0d2025-05-02 08:46:22 -0600200 struct udevice *dev;
Simon Glass2b91ca62023-08-14 16:40:37 -0600201 struct scene *scn;
202
Simon Glass2b91ca62023-08-14 16:40:37 -0600203 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
204
Simon Glass736bc0d2025-05-02 08:46:22 -0600205 ut_assertok(uclass_first_device_err(UCLASS_VIDEO, &dev));
206 vid_priv = dev_get_uclass_priv(dev);
207 ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, dev, &scn));
Simon Glass2b91ca62023-08-14 16:40:37 -0600208
209 /* get the menus to fiddle with */
210 menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU);
211 ut_assertnonnull(menu);
212 menu->cur_item_id = ID_CPU_SPEED_2;
213
214 menu2 = scene_obj_find(scn, ID_POWER_LOSS, SCENEOBJT_MENU);
215 ut_assertnonnull(menu2);
216 menu2->cur_item_id = ID_AC_MEMORY;
217
218 ut_assertok(run_command("cedit write_cmos -v", 0));
219 ut_assert_nextlinen("Write 2 bytes from offset 80 to 84");
220 ut_assert_console_end();
221
Simon Glass4462fa32023-08-14 16:40:38 -0600222 /* reset the expo */
223 menu->cur_item_id = ID_CPU_SPEED_1;
224 menu2->cur_item_id = ID_AC_OFF;
225
226 ut_assertok(run_command("cedit read_cmos -v", 0));
227 ut_assert_nextlinen("Read 2 bytes from offset 80 to 84");
228 ut_assert_console_end();
229
230 ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id);
231 ut_asserteq(ID_AC_MEMORY, menu2->cur_item_id);
232
Simon Glass2b91ca62023-08-14 16:40:37 -0600233 return 0;
234}
Simon Glassf0425022024-08-22 07:57:54 -0600235BOOTSTD_TEST(cedit_cmos, UTF_CONSOLE);
Simon Glassee440642025-05-02 08:46:19 -0600236
Simon Glass38fdda02025-05-02 08:46:23 -0600237/* Check the cedit displays correctly */
Simon Glassee440642025-05-02 08:46:19 -0600238static int cedit_render(struct unit_test_state *uts)
239{
Simon Glass14bd4132025-05-02 08:46:21 -0600240 struct scene_obj_menu *menu;
Simon Glassee440642025-05-02 08:46:19 -0600241 struct video_priv *vid_priv;
242 extern struct expo *cur_exp;
Simon Glass38fdda02025-05-02 08:46:23 -0600243 struct expo_action evt;
Simon Glass14bd4132025-05-02 08:46:21 -0600244 struct expo_action act;
Simon Glass38fdda02025-05-02 08:46:23 -0600245 struct udevice *dev, *con;
246 struct stdio_dev *sdev;
Simon Glassee440642025-05-02 08:46:19 -0600247 struct scene *scn;
248 struct expo *exp;
Simon Glass38fdda02025-05-02 08:46:23 -0600249 int i;
Simon Glassee440642025-05-02 08:46:19 -0600250
251 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
252
253 exp = cur_exp;
Simon Glass38fdda02025-05-02 08:46:23 -0600254 sdev = stdio_get_by_name("vidconsole");
255 ut_assertnonnull(sdev);
256 con = sdev->priv;
257
258 dev = dev_get_parent(con);
Simon Glass736bc0d2025-05-02 08:46:22 -0600259 vid_priv = dev_get_uclass_priv(dev);
260 ut_asserteq(ID_SCENE1, cedit_prepare(exp, dev, &scn));
Simon Glass14bd4132025-05-02 08:46:21 -0600261
262 menu = scene_obj_find(scn, ID_POWER_LOSS, SCENEOBJT_MENU);
263 ut_assertnonnull(menu);
264 ut_asserteq(ID_AC_OFF, menu->cur_item_id);
265
Simon Glassee440642025-05-02 08:46:19 -0600266 ut_assertok(expo_render(exp));
267 ut_asserteq(4929, video_compress_fb(uts, dev, false));
268 ut_assertok(video_check_copy_fb(uts, dev));
Simon Glass14bd4132025-05-02 08:46:21 -0600269
270 /* move to the second menu */
271 act.type = EXPOACT_POINT_OBJ;
272 act.select.id = ID_POWER_LOSS;
273 ut_assertok(cedit_do_action(exp, scn, vid_priv, &act));
274 ut_assertok(expo_render(exp));
275 ut_asserteq(4986, video_compress_fb(uts, dev, false));
276
277 /* open the menu */
278 act.type = EXPOACT_OPEN;
279 act.select.id = ID_POWER_LOSS;
280 ut_assertok(cedit_do_action(exp, scn, vid_priv, &act));
281 ut_assertok(expo_render(exp));
282 ut_asserteq(5393, video_compress_fb(uts, dev, false));
283
284 /* close the menu */
285 act.type = EXPOACT_CLOSE;
286 act.select.id = ID_POWER_LOSS;
287 ut_assertok(cedit_do_action(exp, scn, vid_priv, &act));
288 ut_assertok(expo_render(exp));
289 ut_asserteq(4986, video_compress_fb(uts, dev, false));
290
291 /* open the menu again to check it looks the same */
292 act.type = EXPOACT_OPEN;
293 act.select.id = ID_POWER_LOSS;
294 ut_assertok(cedit_do_action(exp, scn, vid_priv, &act));
295 ut_assertok(expo_render(exp));
296 ut_asserteq(5393, video_compress_fb(uts, dev, false));
297
298 /* close the menu */
299 act.type = EXPOACT_CLOSE;
300 act.select.id = ID_POWER_LOSS;
301 ut_assertok(cedit_do_action(exp, scn, vid_priv, &act));
302 ut_assertok(expo_render(exp));
303 ut_asserteq(4986, video_compress_fb(uts, dev, false));
304
Simon Glass38fdda02025-05-02 08:46:23 -0600305 /* move to the line-edit field */
306 act.type = EXPOACT_POINT_OBJ;
307 act.select.id = ID_MACHINE_NAME;
308 ut_assertok(cedit_do_action(exp, scn, vid_priv, &act));
309 ut_assertok(expo_render(exp));
310 ut_asserteq(4872, video_compress_fb(uts, dev, false));
311
312 /* open it */
313 act.type = EXPOACT_OPEN;
314 act.select.id = ID_MACHINE_NAME;
315 ut_assertok(cedit_do_action(exp, scn, vid_priv, &act));
316 ut_assertok(expo_render(exp));
317 ut_asserteq(4841, video_compress_fb(uts, dev, false));
318
319 /*
320 * Send some keypresses. Note that the console must be enabled so that
321 * the characters actually reach the putc_xy() in console_truetype,
322 * since in scene_textline_send_key(), the lineedit restores the
323 * vidconsole state, outputs the character and then saves the state
324 * again. If the character is never output, then the state won't be
325 * updated and the lineedit will be inconsistent.
326 */
327 ut_unsilence_console(uts);
328 for (i = 'a'; i < 'd'; i++)
329 ut_assertok(scene_send_key(scn, i, &evt));
330 ut_silence_console(uts);
331 ut_assertok(cedit_arange(exp, vid_priv, scn->id));
332 ut_assertok(expo_render(exp));
333 ut_asserteq(5009, video_compress_fb(uts, dev, false));
334
Simon Glassee440642025-05-02 08:46:19 -0600335 expo_destroy(exp);
336 cur_exp = NULL;
337
338 return 0;
339}
340BOOTSTD_TEST(cedit_render, UTF_DM | UTF_SCAN_FDT);
Simon Glass38fdda02025-05-02 08:46:23 -0600341
342/* Check the cedit displays lineedits correctly */
343static int cedit_render_lineedit(struct unit_test_state *uts)
344{
345 struct scene_obj_textline *tline;
346 struct video_priv *vid_priv;
347 extern struct expo *cur_exp;
348 struct expo_action evt;
349 struct expo_action act;
350 struct udevice *dev, *con;
351 struct stdio_dev *sdev;
352 struct scene *scn;
353 struct expo *exp;
354 char *str;
355 int i;
356
357 ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0));
358
359 exp = cur_exp;
360 sdev = stdio_get_by_name("vidconsole");
361 ut_assertnonnull(sdev);
362 con = sdev->priv;
363
364 dev = dev_get_parent(con);
365 vid_priv = dev_get_uclass_priv(dev);
366 ut_asserteq(ID_SCENE1, cedit_prepare(exp, dev, &scn));
367
368 /* set up an initial value for the textline */
369 tline = scene_obj_find(scn, ID_MACHINE_NAME, SCENEOBJT_TEXTLINE);
370 ut_assertnonnull(tline);
371 str = abuf_data(&tline->buf);
372 strcpy(str, "my-machine");
373 ut_asserteq(20, tline->pos);
374
375 ut_assertok(expo_render(exp));
376 ut_asserteq(5336, video_compress_fb(uts, dev, false));
377 ut_assertok(video_check_copy_fb(uts, dev));
378
379 /* move to the line-edit field */
380 act.type = EXPOACT_POINT_OBJ;
381 act.select.id = ID_MACHINE_NAME;
382 ut_assertok(cedit_do_action(exp, scn, vid_priv, &act));
383 ut_assertok(expo_render(exp));
384 ut_asserteq(5363, video_compress_fb(uts, dev, false));
385
386 /* open it */
387 act.type = EXPOACT_OPEN;
388 act.select.id = ID_MACHINE_NAME;
389 ut_assertok(cedit_do_action(exp, scn, vid_priv, &act));
390 // ut_asserteq(0, tline->pos);
391 ut_assertok(expo_render(exp));
392 ut_asserteq(5283, video_compress_fb(uts, dev, false));
393
394 /* delete some characters */
395 ut_unsilence_console(uts);
396 for (i = 0; i < 3; i++)
397 ut_assertok(scene_send_key(scn, '\b', &evt));
398 ut_silence_console(uts);
399 ut_asserteq_str("my-mach", str);
400
401 ut_assertok(cedit_arange(exp, vid_priv, scn->id));
402 ut_assertok(expo_render(exp));
403 ut_asserteq(5170, video_compress_fb(uts, dev, false));
404
405 expo_destroy(exp);
406 cur_exp = NULL;
407
408 return 0;
409}
410BOOTSTD_TEST(cedit_render_lineedit, UTF_DM | UTF_SCAN_FDT);