[Concept,13/19] test: Add editenv test for init/poll/uninit functions

Message ID 20260130035849.3580212-14-simon.glass@canonical.com
State New
Headers
Series Enhanced command-line editing with undo/redo support |

Commit Message

Simon Glass Jan. 30, 2026, 3:58 a.m. UTC
  Add a test for the expo_editenv_init(), expo_editenv_poll() and
expo_editenv_uninit() functions which allow more flexible use of the
environment editor. The test uses a helper function editenv_send() to
send keys directly to the expo and verify the result.

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 test/boot/editenv.c | 84 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 84 insertions(+)
  

Patch

diff --git a/test/boot/editenv.c b/test/boot/editenv.c
index 02bc025e216..62a33f1ba0f 100644
--- a/test/boot/editenv.c
+++ b/test/boot/editenv.c
@@ -9,11 +9,64 @@ 
 #include <dm.h>
 #include <env.h>
 #include <expo.h>
+#include <menu.h>
 #include <video.h>
+#include <video_console.h>
 #include <test/ut.h>
 #include <test/video.h>
 #include "bootstd_common.h"
 
+static const char initial[] =
+	"This is a long string that will wrap to multiple lines "
+	"when displayed in the textedit widget. It needs to be "
+	"long enough to span several lines so that the up and down "
+	"arrow keys can be tested properly.\n"
+	"The arrow keys should "
+	"move the cursor between lines in the multiline editor.";
+
+/**
+ * editenv_send() - Send a key to the editenv expo
+ *
+ * Arranges and renders the scene, sends the key, then checks for any
+ * resulting action.
+ *
+ * @info: Editenv info
+ * @key: Key to send (ASCII or BKEY_...)
+ * Return: 0 if OK, 1 if editing is complete, -ECANCELED if user quit,
+ *	other -ve on error
+ */
+static int editenv_send(struct editenv_info *info, int key)
+{
+	struct expo_action act;
+	int ret;
+
+	ret = expo_send_key(info->exp, key);
+	if (ret)
+		return ret;
+
+	ret = scene_arrange(info->scn);
+	if (ret)
+		return ret;
+
+	ret = expo_render(info->exp);
+	if (ret)
+		return ret;
+
+	ret = expo_action_get(info->exp, &act);
+	if (ret == -EAGAIN)
+		return 0;
+	if (ret)
+		return ret;
+
+	if (act.type == EXPOACT_QUIT)
+		return -ECANCELED;
+
+	if (act.type == EXPOACT_CLOSE)
+		return 1;
+
+	return 0;
+}
+
 /* Check expo_editenv() basic functionality */
 static int editenv_test_base(struct unit_test_state *uts)
 {
@@ -92,3 +145,34 @@  static int editenv_test_video(struct unit_test_state *uts)
 	return 0;
 }
 BOOTSTD_TEST(editenv_test_video, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);
+
+/* Check the init/poll/uninit functions work correctly */
+static int editenv_test_funcs(struct unit_test_state *uts)
+{
+	struct editenv_info info;
+	struct udevice *dev, *con;
+
+	ut_assertok(uclass_first_device_err(UCLASS_VIDEO, &dev));
+	ut_assertok(uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &con));
+
+	/* Set font size to 30 */
+	ut_assertok(vidconsole_select_font(con, NULL, NULL, 30));
+
+	ut_assertok(expo_editenv_init("testvar", initial, &info));
+	ut_asserteq(16611, ut_check_video(uts, "init"));
+
+	/* Type a character and press Enter to accept */
+	ut_assertok(editenv_send(&info, '*'));
+	ut_asserteq(16689, ut_check_video(uts, "insert"));
+
+	ut_asserteq(1, editenv_send(&info, BKEY_SELECT));
+
+	/* The '*' should be appended to the initial text */
+	ut_assert(strstr(expo_editenv_result(&info), "editor.*"));
+	ut_asserteq(16689, ut_check_video(uts, "save"));
+
+	expo_editenv_uninit(&info);
+
+	return 0;
+}
+BOOTSTD_TEST(editenv_test_funcs, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE);