Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | // SPDX-License-Identifier: GPL-2.0+ /* * Test for expo environment editor * * Copyright 2025 Google LLC * Written by Simon Glass <sjg@chromium.org> */ #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) { char buf[256]; int ret; /* * Type "test" then press Ctrl-S to accept (Enter inserts newline in * multiline mode) */ console_in_puts("test\x13"); ret = expo_editenv("myvar", NULL, buf, sizeof(buf)); ut_assertok(ret); ut_asserteq_str("test", buf); return 0; } BOOTSTD_TEST(editenv_test_base, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE); /* Check expo_editenv() with initial value - prepend text */ static int editenv_test_initial(struct unit_test_state *uts) { char buf[256]; int ret; /* * Start with "world", go to start with Ctrl-A, type "hello ", then * press Ctrl-S to accept */ console_in_puts("\x01hello \x13"); ret = expo_editenv("myvar", "world", buf, sizeof(buf)); ut_assertok(ret); ut_asserteq_str("hello world", buf); return 0; } BOOTSTD_TEST(editenv_test_initial, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE); /* Check expo_editenv() escape closes editor (accepts current value) */ static int editenv_test_escape(struct unit_test_state *uts) { char buf[256]; int ret; /* * Press Escape immediately - this closes the editor and accepts * the current (initial) value */ console_in_puts("\x1b"); ret = expo_editenv("myvar", "unchanged", buf, sizeof(buf)); ut_assertok(ret); ut_asserteq_str("unchanged", buf); return 0; } BOOTSTD_TEST(editenv_test_escape, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE); /* Check expo_editenv() renders correctly with multiline text and navigation */ static int editenv_test_video(struct unit_test_state *uts) { struct udevice *dev, *con; char buf[512]; int ret; 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)); /* * Navigate with up arrow, insert text, then press Ctrl-S to save. * The up arrow should be converted to Ctrl-P by scene_txtin_send_key(). * \x1b[A is the escape sequence for up arrow, \x13 is Ctrl-S (save) */ console_in_puts("\x1b[A!\x13"); ret = expo_editenv("testvar", initial, buf, sizeof(buf)); ut_assertok(ret); /* The '!' should be inserted one visual line up from the end */ ut_assert(strstr(buf, "tes!ted")); /* Check the framebuffer has expected content */ ut_asserteq(16829, video_compress_fb(uts, dev, false)); 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")); /* Navigate up to previous line */ ut_assertok(editenv_send(&info, BKEY_UP)); ut_asserteq(16684, ut_check_video(uts, "up")); /* Navigate back down */ ut_assertok(editenv_send(&info, BKEY_DOWN)); ut_asserteq(16611, ut_check_video(uts, "down")); /* Navigate with up arrow and insert '*' */ ut_assertok(editenv_send(&info, BKEY_UP)); ut_asserteq(16684, ut_check_video(uts, "up2")); ut_assertok(editenv_send(&info, '*')); ut_asserteq(16877, ut_check_video(uts, "insert")); /* Use Ctrl-K to kill to end of line (stops at the existing newline) */ ut_assertok(editenv_send(&info, CTL_CH('k'))); ut_asserteq(16033, ut_check_video(uts, "kill")); /* Test undo - should restore the killed text */ ut_assertok(editenv_send(&info, CTL_CH('z'))); ut_asserteq(16877, ut_check_video(uts, "undo")); /* Kill again and yank it back - text should be restored */ ut_assertok(editenv_send(&info, CTL_CH('k'))); ut_asserteq(16033, ut_check_video(uts, "kill2")); ut_assertok(editenv_send(&info, CTL_CH('y'))); ut_asserteq(16808, ut_check_video(uts, "yank")); /* Test Home - should go to start of current line */ ut_assertok(editenv_send(&info, CTL_CH('a'))); ut_asserteq(0, info.ted->tin.cls.num); ut_asserteq(16845, ut_check_video(uts, "home")); /* Test End - should go to end of current line */ ut_assertok(editenv_send(&info, CTL_CH('e'))); ut_asserteq(16808, ut_check_video(uts, "end")); /* Go left two words with Ctrl+R */ ut_assertok(editenv_send(&info, CTL_CH('r'))); ut_asserteq(16838, ut_check_video(uts, "left1")); ut_assertok(editenv_send(&info, CTL_CH('r'))); ut_asserteq(16812, ut_check_video(uts, "left2")); /* Delete three words with Ctrl+W */ ut_assertok(editenv_send(&info, CTL_CH('w'))); ut_asserteq(16691, ut_check_video(uts, "delw1")); ut_assertok(editenv_send(&info, CTL_CH('w'))); ut_asserteq(16445, ut_check_video(uts, "delw2")); ut_assertok(editenv_send(&info, CTL_CH('w'))); ut_asserteq(16118, ut_check_video(uts, "delw3")); /* Undo to restore one deleted word */ ut_assertok(editenv_send(&info, CTL_CH('z'))); ut_asserteq(16445, ut_check_video(uts, "undo1")); /* Type a character - this clears the redo buffer */ ut_assertok(editenv_send(&info, '!')); ut_asserteq(16469, ut_check_video(uts, "type")); /* Redo (Ctrl+G) should do nothing since typing cleared the redo buffer */ ut_assertok(editenv_send(&info, CTL_CH('g'))); ut_asserteq(16469, ut_check_video(uts, "redo")); /* Press Ctrl-S to save */ ut_asserteq(1, editenv_send(&info, BKEY_SAVE)); /* The '*' and '!' are inserted; redo did nothing since it was cleared */ ut_assert(strstr(expo_editenv_result(&info), "tes*ted")); ut_asserteq(16469, ut_check_video(uts, "save")); expo_editenv_uninit(&info); return 0; } BOOTSTD_TEST(editenv_test_funcs, UTF_DM | UTF_SCAN_FDT | UTF_CONSOLE); |